1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (c) 2014-2016, STMicroelectronics International N.V.
4 */
5
6 #include <arm32.h>
7 #include <console.h>
8 #include <drivers/gic.h>
9 #include <drivers/stih_asc.h>
10 #include <io.h>
11 #include <kernel/boot.h>
12 #include <kernel/interrupt.h>
13 #include <kernel/misc.h>
14 #include <kernel/panic.h>
15 #include <kernel/tz_ssvce_pl310.h>
16 #include <mm/core_memprot.h>
17 #include <mm/core_mmu.h>
18 #include <platform_config.h>
19 #include <stdint.h>
20 #include <tee/entry_fast.h>
21 #include <tee/entry_std.h>
22 #include <trace.h>
23 #include <util.h>
24
25 register_phys_mem_pgdir(MEM_AREA_IO_SEC, CPU_IOMEM_BASE, CPU_IOMEM_SIZE);
26 register_phys_mem_pgdir(MEM_AREA_IO_SEC, RNG_BASE, RNG_SIZE);
27 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, UART_CONSOLE_BASE, STIH_ASC_REG_SIZE);
28
29 #ifdef DRAM0_BASE
30 register_ddr(DRAM0_BASE, DRAM0_SIZE);
31 #endif
32 #ifdef DRAM1_BASE
33 register_ddr(DRAM1_BASE, DRAM1_SIZE);
34 #endif
35
36 static struct gic_data gic_data;
37 static struct stih_asc_pd console_data;
38
39 #if defined(PLATFORM_FLAVOR_b2260)
ns_resources_ready(void)40 static bool ns_resources_ready(void)
41 {
42 return true;
43 }
44 #else
45 /* some nonsecure resource might not be ready (uart) */
46 static int boot_is_completed;
ns_resources_ready(void)47 static bool ns_resources_ready(void)
48 {
49 return !!boot_is_completed;
50 }
51
52 /* Overriding the default __weak tee_entry_std() */
tee_entry_std(struct optee_msg_arg * arg,uint32_t num_params)53 TEE_Result tee_entry_std(struct optee_msg_arg *arg, uint32_t num_params)
54 {
55 boot_is_completed = 1;
56
57 return __tee_entry_std(arg, num_params);
58 }
59 #endif
60
console_init(void)61 void console_init(void)
62 {
63 stih_asc_init(&console_data, UART_CONSOLE_BASE);
64 }
65
console_putc(int ch)66 void console_putc(int ch)
67 {
68
69 if (ns_resources_ready()) {
70 struct serial_chip *cons = &console_data.chip;
71
72 if (ch == '\n')
73 cons->ops->putc(cons, '\r');
74 cons->ops->putc(cons, ch);
75 }
76 }
77
console_flush(void)78 void console_flush(void)
79 {
80 if (ns_resources_ready()) {
81 struct serial_chip *cons = &console_data.chip;
82
83 if (cons->ops->flush)
84 cons->ops->flush(cons);
85 }
86 }
87
pl310_base(void)88 vaddr_t pl310_base(void)
89 {
90 static void *va;
91
92 if (cpu_mmu_enabled()) {
93 if (!va)
94 va = phys_to_virt(PL310_BASE, MEM_AREA_IO_SEC, 1);
95 return (vaddr_t)va;
96 }
97 return PL310_BASE;
98 }
99
arm_cl2_config(vaddr_t pl310)100 void arm_cl2_config(vaddr_t pl310)
101 {
102 /* pl310 off */
103 io_write32(pl310 + PL310_CTRL, 0);
104
105 /* config PL310 */
106 io_write32(pl310 + PL310_TAG_RAM_CTRL, PL310_TAG_RAM_CTRL_INIT);
107 io_write32(pl310 + PL310_DATA_RAM_CTRL, PL310_DATA_RAM_CTRL_INIT);
108 io_write32(pl310 + PL310_AUX_CTRL, PL310_AUX_CTRL_INIT);
109 io_write32(pl310 + PL310_PREFETCH_CTRL, PL310_PREFETCH_CTRL_INIT);
110 io_write32(pl310 + PL310_POWER_CTRL, PL310_POWER_CTRL_INIT);
111
112 /* invalidate all pl310 cache ways */
113 arm_cl2_invbyway(pl310);
114 }
115
plat_primary_init_early(void)116 void plat_primary_init_early(void)
117 {
118 int i;
119
120 assert(!cpu_mmu_enabled());
121
122 io_write32(SCU_BASE + SCU_SAC, SCU_SAC_INIT);
123 io_write32(SCU_BASE + SCU_NSAC, SCU_NSAC_INIT);
124 io_write32(SCU_BASE + SCU_FILT_EA, CPU_PORT_FILT_END);
125 io_write32(SCU_BASE + SCU_FILT_SA, CPU_PORT_FILT_START);
126 io_write32(SCU_BASE + SCU_CTRL, SCU_CTRL_INIT);
127
128 io_write32(pl310_base() + PL310_ADDR_FILT_END, CPU_PORT_FILT_END);
129 io_write32(pl310_base() + PL310_ADDR_FILT_START,
130 CPU_PORT_FILT_START | PL310_CTRL_ENABLE_BIT);
131
132 /* TODO: gic_init scan fails, pre-init all SPIs are nonsecure */
133 for (i = 0; i < (31 * 4); i += 4)
134 io_write32(GIC_DIST_BASE + GIC_DIST_ISR1 + i, 0xFFFFFFFF);
135 }
136
main_init_gic(void)137 void main_init_gic(void)
138 {
139 gic_init(&gic_data, GIC_CPU_BASE, GIC_DIST_BASE);
140 itr_init(&gic_data.chip);
141 }
142
main_secondary_init_gic(void)143 void main_secondary_init_gic(void)
144 {
145 gic_cpu_init(&gic_data);
146 }
147
itr_core_handler(void)148 void itr_core_handler(void)
149 {
150 gic_it_handle(&gic_data);
151 }
152