xref: /openbmc/u-boot/arch/arm/mach-stm32mp/cpu.c (revision 9c260e86)
1 /*
2  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
3  *
4  * SPDX-License-Identifier:	GPL-2.0+	BSD-3-Clause
5  */
6 #include <common.h>
7 #include <clk.h>
8 #include <asm/io.h>
9 #include <asm/arch/stm32.h>
10 #include <asm/arch/sys_proto.h>
11 #include <dm/uclass.h>
12 
13 /* RCC register */
14 #define RCC_TZCR		(STM32_RCC_BASE + 0x00)
15 #define RCC_DBGCFGR		(STM32_RCC_BASE + 0x080C)
16 #define RCC_BDCR		(STM32_RCC_BASE + 0x0140)
17 #define RCC_MP_APB5ENSETR	(STM32_RCC_BASE + 0x0208)
18 #define RCC_BDCR_VSWRST		BIT(31)
19 #define RCC_BDCR_RTCSRC		GENMASK(17, 16)
20 #define RCC_DBGCFGR_DBGCKEN	BIT(8)
21 
22 /* Security register */
23 #define ETZPC_TZMA1_SIZE	(STM32_ETZPC_BASE + 0x04)
24 #define ETZPC_DECPROT0		(STM32_ETZPC_BASE + 0x10)
25 
26 #define TZC_GATE_KEEPER		(STM32_TZC_BASE + 0x008)
27 #define TZC_REGION_ATTRIBUTE0	(STM32_TZC_BASE + 0x110)
28 #define TZC_REGION_ID_ACCESS0	(STM32_TZC_BASE + 0x114)
29 
30 #define TAMP_CR1		(STM32_TAMP_BASE + 0x00)
31 
32 #define PWR_CR1			(STM32_PWR_BASE + 0x00)
33 #define PWR_CR1_DBP		BIT(8)
34 
35 /* DBGMCU register */
36 #define DBGMCU_IDC		(STM32_DBGMCU_BASE + 0x00)
37 #define DBGMCU_APB4FZ1		(STM32_DBGMCU_BASE + 0x2C)
38 #define DBGMCU_APB4FZ1_IWDG2	BIT(2)
39 #define DBGMCU_IDC_DEV_ID_MASK	GENMASK(11, 0)
40 #define DBGMCU_IDC_DEV_ID_SHIFT	0
41 #define DBGMCU_IDC_REV_ID_MASK	GENMASK(31, 16)
42 #define DBGMCU_IDC_REV_ID_SHIFT	16
43 
44 /* boot interface from Bootrom
45  * - boot instance = bit 31:16
46  * - boot device = bit 15:0
47  */
48 #define BOOTROM_PARAM_ADDR	0x2FFC0078
49 #define BOOTROM_MODE_MASK	GENMASK(15, 0)
50 #define BOOTROM_MODE_SHIFT	0
51 #define BOOTROM_INSTANCE_MASK	 GENMASK(31, 16)
52 #define BOOTROM_INSTANCE_SHIFT	16
53 
54 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
55 static void security_init(void)
56 {
57 	/* Disable the backup domain write protection */
58 	/* the protection is enable at each reset by hardware */
59 	/* And must be disable by software */
60 	setbits_le32(PWR_CR1, PWR_CR1_DBP);
61 
62 	while (!(readl(PWR_CR1) & PWR_CR1_DBP))
63 		;
64 
65 	/* If RTC clock isn't enable so this is a cold boot then we need
66 	 * to reset the backup domain
67 	 */
68 	if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) {
69 		setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
70 		while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST))
71 			;
72 		clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
73 	}
74 
75 	/* allow non secure access in Write/Read for all peripheral */
76 	writel(GENMASK(25, 0), ETZPC_DECPROT0);
77 
78 	/* Open SYSRAM for no secure access */
79 	writel(0x0, ETZPC_TZMA1_SIZE);
80 
81 	/* enable TZC1 TZC2 clock */
82 	writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR);
83 
84 	/* Region 0 set to no access by default */
85 	/* bit 0 / 16 => nsaid0 read/write Enable
86 	 * bit 1 / 17 => nsaid1 read/write Enable
87 	 * ...
88 	 * bit 15 / 31 => nsaid15 read/write Enable
89 	 */
90 	writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0);
91 	/* bit 30 / 31 => Secure Global Enable : write/read */
92 	/* bit 0 / 1 => Region Enable for filter 0/1 */
93 	writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0);
94 
95 	/* Enable Filter 0 and 1 */
96 	setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1));
97 
98 	/* RCC trust zone deactivated */
99 	writel(0x0, RCC_TZCR);
100 
101 	/* TAMP: deactivate the internal tamper
102 	 * Bit 23 ITAMP8E: monotonic counter overflow
103 	 * Bit 20 ITAMP5E: RTC calendar overflow
104 	 * Bit 19 ITAMP4E: HSE monitoring
105 	 * Bit 18 ITAMP3E: LSE monitoring
106 	 * Bit 16 ITAMP1E: RTC power domain supply monitoring
107 	 */
108 	writel(0x0, TAMP_CR1);
109 }
110 
111 /*
112  * Debug init
113  */
114 static void dbgmcu_init(void)
115 {
116 	setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
117 
118 	/* Freeze IWDG2 if Cortex-A7 is in debug mode */
119 	setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2);
120 }
121 #endif /* !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) */
122 
123 static u32 get_bootmode(void)
124 {
125 	u32 boot_mode;
126 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
127 	u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR);
128 	u32 bootrom_device, bootrom_instance;
129 
130 	bootrom_device =
131 		(bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT;
132 	bootrom_instance =
133 		(bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT;
134 	boot_mode =
135 		((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) |
136 		((bootrom_instance << BOOT_INSTANCE_SHIFT) &
137 		 BOOT_INSTANCE_MASK);
138 
139 	/* save the boot mode in TAMP backup register */
140 	clrsetbits_le32(TAMP_BOOT_CONTEXT,
141 			TAMP_BOOT_MODE_MASK,
142 			boot_mode << TAMP_BOOT_MODE_SHIFT);
143 #else
144 	/* read TAMP backup register */
145 	boot_mode = (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
146 		    TAMP_BOOT_MODE_SHIFT;
147 #endif
148 	return boot_mode;
149 }
150 
151 /*
152  * Early system init
153  */
154 int arch_cpu_init(void)
155 {
156 	/* early armv7 timer init: needed for polling */
157 	timer_init();
158 
159 #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
160 	dbgmcu_init();
161 
162 	security_init();
163 #endif
164 	/* get bootmode from BootRom context: saved in TAMP register */
165 	get_bootmode();
166 
167 	return 0;
168 }
169 
170 void enable_caches(void)
171 {
172 	/* Enable D-cache. I-cache is already enabled in start.S */
173 	dcache_enable();
174 }
175 
176 static u32 read_idc(void)
177 {
178 	setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
179 
180 	return readl(DBGMCU_IDC);
181 }
182 
183 u32 get_cpu_rev(void)
184 {
185 	return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
186 }
187 
188 u32 get_cpu_type(void)
189 {
190 	return (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT;
191 }
192 
193 #if defined(CONFIG_DISPLAY_CPUINFO)
194 int print_cpuinfo(void)
195 {
196 	char *cpu_s, *cpu_r;
197 
198 	switch (get_cpu_type()) {
199 	case CPU_STMP32MP15x:
200 		cpu_s = "15x";
201 		break;
202 	default:
203 		cpu_s = "?";
204 		break;
205 	}
206 
207 	switch (get_cpu_rev()) {
208 	case CPU_REVA:
209 		cpu_r = "A";
210 		break;
211 	case CPU_REVB:
212 		cpu_r = "B";
213 		break;
214 	default:
215 		cpu_r = "?";
216 		break;
217 	}
218 
219 	printf("CPU: STM32MP%s.%s\n", cpu_s, cpu_r);
220 
221 	return 0;
222 }
223 #endif /* CONFIG_DISPLAY_CPUINFO */
224 
225 static void setup_boot_mode(void)
226 {
227 	char cmd[60];
228 	u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
229 	u32 boot_mode =
230 		(boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
231 	int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
232 
233 	pr_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d\n",
234 		 __func__, boot_ctx, boot_mode, instance);
235 
236 	switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
237 	case BOOT_SERIAL_UART:
238 		sprintf(cmd, "%d", instance);
239 		env_set("boot_device", "uart");
240 		env_set("boot_instance", cmd);
241 		break;
242 	case BOOT_SERIAL_USB:
243 		env_set("boot_device", "usb");
244 		env_set("boot_instance", "0");
245 		break;
246 	case BOOT_FLASH_SD:
247 	case BOOT_FLASH_EMMC:
248 		sprintf(cmd, "%d", instance);
249 		env_set("boot_device", "mmc");
250 		env_set("boot_instance", cmd);
251 		break;
252 	case BOOT_FLASH_NAND:
253 		env_set("boot_device", "nand");
254 		env_set("boot_instance", "0");
255 		break;
256 	case BOOT_FLASH_NOR:
257 		env_set("boot_device", "nor");
258 		env_set("boot_instance", "0");
259 		break;
260 	default:
261 		pr_debug("unexpected boot mode = %x\n", boot_mode);
262 		break;
263 	}
264 }
265 
266 int arch_misc_init(void)
267 {
268 	setup_boot_mode();
269 
270 	return 0;
271 }
272