xref: /optee_os/core/arch/riscv/plat-virt/main.c (revision 43730326be0aef1afbc3fa5dc00cbe65bb73b47f)
1e2f6d2fbSMarouene Boubakri // SPDX-License-Identifier: BSD-2-Clause
2e2f6d2fbSMarouene Boubakri /*
3e2f6d2fbSMarouene Boubakri  * Copyright 2022-2023 NXP
4e2f6d2fbSMarouene Boubakri  */
5e2f6d2fbSMarouene Boubakri 
6e2f6d2fbSMarouene Boubakri #include <console.h>
7*43730326SHuang Borong #include <drivers/aplic.h>
8*43730326SHuang Borong #include <drivers/imsic.h>
9e2f6d2fbSMarouene Boubakri #include <drivers/ns16550.h>
10a7c08b07SAlvin Chang #include <drivers/plic.h>
11e2f6d2fbSMarouene Boubakri #include <kernel/boot.h>
12e2f6d2fbSMarouene Boubakri #include <kernel/tee_common_otp.h>
13e2f6d2fbSMarouene Boubakri #include <platform_config.h>
14e2f6d2fbSMarouene Boubakri 
151729a810SAlvin Chang #ifdef CFG_16550_UART
16e2f6d2fbSMarouene Boubakri static struct ns16550_data console_data __nex_bss;
171729a810SAlvin Chang register_phys_mem_pgdir(MEM_AREA_IO_NSEC, UART0_BASE, CORE_MMU_PGDIR_SIZE);
181729a810SAlvin Chang #endif
19e2f6d2fbSMarouene Boubakri 
20e2f6d2fbSMarouene Boubakri register_ddr(DRAM_BASE, DRAM_SIZE);
21e2f6d2fbSMarouene Boubakri 
22*43730326SHuang Borong #if defined(CFG_RISCV_APLIC) || defined(CFG_RISCV_APLIC_MSI)
23*43730326SHuang Borong register_phys_mem_pgdir(MEM_AREA_IO_SEC, APLIC_BASE,
24*43730326SHuang Borong 			APLIC_SIZE);
25*43730326SHuang Borong #endif
26*43730326SHuang Borong #if defined(CFG_RISCV_APLIC_MSI) && defined(CFG_RISCV_IMSIC)
27*43730326SHuang Borong register_phys_mem_pgdir(MEM_AREA_IO_SEC, IMSIC_BASE,
28*43730326SHuang Borong 			IMSIC_SIZE);
29*43730326SHuang Borong #endif
30*43730326SHuang Borong 
31e2f6d2fbSMarouene Boubakri #ifdef CFG_RISCV_PLIC
boot_primary_init_intc(void)32a7c08b07SAlvin Chang void boot_primary_init_intc(void)
33e2f6d2fbSMarouene Boubakri {
34f33bc3efSAlvin Chang 	plic_init(PLIC_BASE);
35e2f6d2fbSMarouene Boubakri }
36e2f6d2fbSMarouene Boubakri 
boot_secondary_init_intc(void)37a7c08b07SAlvin Chang void boot_secondary_init_intc(void)
38e2f6d2fbSMarouene Boubakri {
39f33bc3efSAlvin Chang 	plic_hart_init();
40e2f6d2fbSMarouene Boubakri }
41e2f6d2fbSMarouene Boubakri #endif /* CFG_RISCV_PLIC */
42e2f6d2fbSMarouene Boubakri 
43*43730326SHuang Borong #ifdef CFG_RISCV_APLIC
boot_primary_init_intc(void)44*43730326SHuang Borong void boot_primary_init_intc(void)
45*43730326SHuang Borong {
46*43730326SHuang Borong 	aplic_init(APLIC_BASE);
47*43730326SHuang Borong }
48*43730326SHuang Borong 
boot_secondary_init_intc(void)49*43730326SHuang Borong void boot_secondary_init_intc(void)
50*43730326SHuang Borong {
51*43730326SHuang Borong 	aplic_init_per_hart();
52*43730326SHuang Borong }
53*43730326SHuang Borong #endif /* CFG_RISCV_APLIC */
54*43730326SHuang Borong 
55*43730326SHuang Borong #if defined(CFG_RISCV_APLIC_MSI) && defined(CFG_RISCV_IMSIC)
boot_primary_init_intc(void)56*43730326SHuang Borong void boot_primary_init_intc(void)
57*43730326SHuang Borong {
58*43730326SHuang Borong 	aplic_init(APLIC_BASE);
59*43730326SHuang Borong 	imsic_init(IMSIC_BASE);
60*43730326SHuang Borong }
61*43730326SHuang Borong 
boot_secondary_init_intc(void)62*43730326SHuang Borong void boot_secondary_init_intc(void)
63*43730326SHuang Borong {
64*43730326SHuang Borong 	aplic_init_per_hart();
65*43730326SHuang Borong 	imsic_init_per_hart();
66*43730326SHuang Borong }
67*43730326SHuang Borong #endif
68*43730326SHuang Borong 
691729a810SAlvin Chang #ifdef CFG_16550_UART
plat_console_init(void)7055ab8f06SAlvin Chang void plat_console_init(void)
71e2f6d2fbSMarouene Boubakri {
72e2f6d2fbSMarouene Boubakri 	ns16550_init(&console_data, UART0_BASE, IO_WIDTH_U8, 0);
73e2f6d2fbSMarouene Boubakri 	register_serial_console(&console_data.chip);
74e2f6d2fbSMarouene Boubakri }
751729a810SAlvin Chang #endif
76e2f6d2fbSMarouene Boubakri 
interrupt_main_handler(void)77e1aad7e9SEtienne Carriere void interrupt_main_handler(void)
78e2f6d2fbSMarouene Boubakri {
79e2f6d2fbSMarouene Boubakri 	if (IS_ENABLED(CFG_RISCV_PLIC))
80f33bc3efSAlvin Chang 		plic_it_handle();
81*43730326SHuang Borong 	else if (IS_ENABLED(CFG_RISCV_APLIC))
82*43730326SHuang Borong 		aplic_it_handle();
83*43730326SHuang Borong 	else if (IS_ENABLED(CFG_RISCV_APLIC_MSI) &&
84*43730326SHuang Borong 		 IS_ENABLED(CFG_RISCV_IMSIC))
85*43730326SHuang Borong 		imsic_it_handle();
86e2f6d2fbSMarouene Boubakri }
87