blob: 753ff3e4dbd1ccf48eb58d5c1c2b8830ad398144 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01004 */
5#include <common.h>
6#include <clk.h>
Patrick Delaunay320d2662018-05-17 14:50:46 +02007#include <debug_uart.h>
Patrick Delaunay7f7deb02018-05-17 15:24:07 +02008#include <environment.h>
9#include <misc.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010010#include <asm/io.h>
11#include <asm/arch/stm32.h>
Patrick Delaunay96583cd2018-03-19 19:09:21 +010012#include <asm/arch/sys_proto.h>
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020013#include <dm/device.h>
Patrick Delaunay08772f62018-03-20 10:54:53 +010014#include <dm/uclass.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010015
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010016/* RCC register */
17#define RCC_TZCR (STM32_RCC_BASE + 0x00)
18#define RCC_DBGCFGR (STM32_RCC_BASE + 0x080C)
19#define RCC_BDCR (STM32_RCC_BASE + 0x0140)
20#define RCC_MP_APB5ENSETR (STM32_RCC_BASE + 0x0208)
21#define RCC_BDCR_VSWRST BIT(31)
22#define RCC_BDCR_RTCSRC GENMASK(17, 16)
23#define RCC_DBGCFGR_DBGCKEN BIT(8)
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010024
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010025/* Security register */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010026#define ETZPC_TZMA1_SIZE (STM32_ETZPC_BASE + 0x04)
27#define ETZPC_DECPROT0 (STM32_ETZPC_BASE + 0x10)
28
29#define TZC_GATE_KEEPER (STM32_TZC_BASE + 0x008)
30#define TZC_REGION_ATTRIBUTE0 (STM32_TZC_BASE + 0x110)
31#define TZC_REGION_ID_ACCESS0 (STM32_TZC_BASE + 0x114)
32
33#define TAMP_CR1 (STM32_TAMP_BASE + 0x00)
34
35#define PWR_CR1 (STM32_PWR_BASE + 0x00)
36#define PWR_CR1_DBP BIT(8)
37
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010038/* DBGMCU register */
Patrick Delaunay96583cd2018-03-19 19:09:21 +010039#define DBGMCU_IDC (STM32_DBGMCU_BASE + 0x00)
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010040#define DBGMCU_APB4FZ1 (STM32_DBGMCU_BASE + 0x2C)
41#define DBGMCU_APB4FZ1_IWDG2 BIT(2)
Patrick Delaunay96583cd2018-03-19 19:09:21 +010042#define DBGMCU_IDC_DEV_ID_MASK GENMASK(11, 0)
43#define DBGMCU_IDC_DEV_ID_SHIFT 0
44#define DBGMCU_IDC_REV_ID_MASK GENMASK(31, 16)
45#define DBGMCU_IDC_REV_ID_SHIFT 16
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010046
Patrick Delaunay08772f62018-03-20 10:54:53 +010047/* boot interface from Bootrom
48 * - boot instance = bit 31:16
49 * - boot device = bit 15:0
50 */
51#define BOOTROM_PARAM_ADDR 0x2FFC0078
52#define BOOTROM_MODE_MASK GENMASK(15, 0)
53#define BOOTROM_MODE_SHIFT 0
54#define BOOTROM_INSTANCE_MASK GENMASK(31, 16)
55#define BOOTROM_INSTANCE_SHIFT 16
56
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020057/* BSEC OTP index */
58#define BSEC_OTP_SERIAL 13
59#define BSEC_OTP_MAC 57
60
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010061#if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
Patrick Delaunayabf26782019-02-12 11:44:39 +010062#ifndef CONFIG_STM32MP1_TRUSTED
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010063static void security_init(void)
64{
65 /* Disable the backup domain write protection */
66 /* the protection is enable at each reset by hardware */
67 /* And must be disable by software */
68 setbits_le32(PWR_CR1, PWR_CR1_DBP);
69
70 while (!(readl(PWR_CR1) & PWR_CR1_DBP))
71 ;
72
73 /* If RTC clock isn't enable so this is a cold boot then we need
74 * to reset the backup domain
75 */
76 if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) {
77 setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
78 while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST))
79 ;
80 clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
81 }
82
83 /* allow non secure access in Write/Read for all peripheral */
84 writel(GENMASK(25, 0), ETZPC_DECPROT0);
85
86 /* Open SYSRAM for no secure access */
87 writel(0x0, ETZPC_TZMA1_SIZE);
88
89 /* enable TZC1 TZC2 clock */
90 writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR);
91
92 /* Region 0 set to no access by default */
93 /* bit 0 / 16 => nsaid0 read/write Enable
94 * bit 1 / 17 => nsaid1 read/write Enable
95 * ...
96 * bit 15 / 31 => nsaid15 read/write Enable
97 */
98 writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0);
99 /* bit 30 / 31 => Secure Global Enable : write/read */
100 /* bit 0 / 1 => Region Enable for filter 0/1 */
101 writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0);
102
103 /* Enable Filter 0 and 1 */
104 setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1));
105
106 /* RCC trust zone deactivated */
107 writel(0x0, RCC_TZCR);
108
109 /* TAMP: deactivate the internal tamper
110 * Bit 23 ITAMP8E: monotonic counter overflow
111 * Bit 20 ITAMP5E: RTC calendar overflow
112 * Bit 19 ITAMP4E: HSE monitoring
113 * Bit 18 ITAMP3E: LSE monitoring
114 * Bit 16 ITAMP1E: RTC power domain supply monitoring
115 */
116 writel(0x0, TAMP_CR1);
117}
Patrick Delaunayabf26782019-02-12 11:44:39 +0100118#endif /* CONFIG_STM32MP1_TRUSTED */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100119
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100120/*
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100121 * Debug init
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100122 */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100123static void dbgmcu_init(void)
124{
125 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
126
127 /* Freeze IWDG2 if Cortex-A7 is in debug mode */
128 setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2);
129}
130#endif /* !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) */
131
Patrick Delaunayabf26782019-02-12 11:44:39 +0100132#if !defined(CONFIG_STM32MP1_TRUSTED) && \
133 (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100134/* get bootmode from ROM code boot context: saved in TAMP register */
135static void update_bootmode(void)
136{
137 u32 boot_mode;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100138 u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR);
139 u32 bootrom_device, bootrom_instance;
140
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100141 /* enable TAMP clock = RTCAPBEN */
142 writel(BIT(8), RCC_MP_APB5ENSETR);
143
144 /* read bootrom context */
Patrick Delaunay08772f62018-03-20 10:54:53 +0100145 bootrom_device =
146 (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT;
147 bootrom_instance =
148 (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT;
149 boot_mode =
150 ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) |
151 ((bootrom_instance << BOOT_INSTANCE_SHIFT) &
152 BOOT_INSTANCE_MASK);
153
154 /* save the boot mode in TAMP backup register */
155 clrsetbits_le32(TAMP_BOOT_CONTEXT,
156 TAMP_BOOT_MODE_MASK,
157 boot_mode << TAMP_BOOT_MODE_SHIFT);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100158}
Patrick Delaunay08772f62018-03-20 10:54:53 +0100159#endif
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100160
161u32 get_bootmode(void)
162{
163 /* read bootmode from TAMP backup register */
164 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
165 TAMP_BOOT_MODE_SHIFT;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100166}
167
168/*
169 * Early system init
170 */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100171int arch_cpu_init(void)
172{
Patrick Delaunay320d2662018-05-17 14:50:46 +0200173 u32 boot_mode;
174
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100175 /* early armv7 timer init: needed for polling */
176 timer_init();
177
178#if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
179 dbgmcu_init();
Patrick Delaunayabf26782019-02-12 11:44:39 +0100180#ifndef CONFIG_STM32MP1_TRUSTED
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100181 security_init();
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100182 update_bootmode();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100183#endif
Patrick Delaunayabf26782019-02-12 11:44:39 +0100184#endif
Patrick Delaunay320d2662018-05-17 14:50:46 +0200185
Patrick Delaunay320d2662018-05-17 14:50:46 +0200186 boot_mode = get_bootmode();
187
188 if ((boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
189 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
190#if defined(CONFIG_DEBUG_UART) && \
Patrick Delaunayabf26782019-02-12 11:44:39 +0100191 !defined(CONFIG_STM32MP1_TRUSTED) && \
Patrick Delaunay320d2662018-05-17 14:50:46 +0200192 (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
193 else
194 debug_uart_init();
195#endif
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100196
197 return 0;
198}
199
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100200void enable_caches(void)
201{
202 /* Enable D-cache. I-cache is already enabled in start.S */
203 dcache_enable();
204}
205
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100206static u32 read_idc(void)
207{
208 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
209
210 return readl(DBGMCU_IDC);
211}
212
213u32 get_cpu_rev(void)
214{
215 return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
216}
217
218u32 get_cpu_type(void)
219{
220 return (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT;
221}
222
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100223#if defined(CONFIG_DISPLAY_CPUINFO)
224int print_cpuinfo(void)
225{
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100226 char *cpu_s, *cpu_r;
227
228 switch (get_cpu_type()) {
229 case CPU_STMP32MP15x:
230 cpu_s = "15x";
231 break;
232 default:
233 cpu_s = "?";
234 break;
235 }
236
237 switch (get_cpu_rev()) {
238 case CPU_REVA:
239 cpu_r = "A";
240 break;
241 case CPU_REVB:
242 cpu_r = "B";
243 break;
244 default:
245 cpu_r = "?";
246 break;
247 }
248
249 printf("CPU: STM32MP%s.%s\n", cpu_s, cpu_r);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100250
251 return 0;
252}
253#endif /* CONFIG_DISPLAY_CPUINFO */
254
Patrick Delaunay08772f62018-03-20 10:54:53 +0100255static void setup_boot_mode(void)
256{
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100257 const u32 serial_addr[] = {
258 STM32_USART1_BASE,
259 STM32_USART2_BASE,
260 STM32_USART3_BASE,
261 STM32_UART4_BASE,
262 STM32_UART5_BASE,
263 STM32_USART6_BASE,
264 STM32_UART7_BASE,
265 STM32_UART8_BASE
266 };
Patrick Delaunay08772f62018-03-20 10:54:53 +0100267 char cmd[60];
268 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
269 u32 boot_mode =
270 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
271 int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100272 struct udevice *dev;
273 int alias;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100274
275 pr_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d\n",
276 __func__, boot_ctx, boot_mode, instance);
277
278 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
279 case BOOT_SERIAL_UART:
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100280 if (instance > ARRAY_SIZE(serial_addr))
281 break;
282 /* serial : search associated alias in devicetree */
283 sprintf(cmd, "serial@%x", serial_addr[instance]);
284 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev))
285 break;
286 if (fdtdec_get_alias_seq(gd->fdt_blob, "serial",
287 dev_of_offset(dev), &alias))
288 break;
289 sprintf(cmd, "%d", alias);
290 env_set("boot_device", "serial");
Patrick Delaunay08772f62018-03-20 10:54:53 +0100291 env_set("boot_instance", cmd);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100292
293 /* restore console on uart when not used */
294 if (gd->cur_serial_dev != dev) {
295 gd->flags &= ~(GD_FLG_SILENT |
296 GD_FLG_DISABLE_CONSOLE);
297 printf("serial boot with console enabled!\n");
298 }
Patrick Delaunay08772f62018-03-20 10:54:53 +0100299 break;
300 case BOOT_SERIAL_USB:
301 env_set("boot_device", "usb");
302 env_set("boot_instance", "0");
303 break;
304 case BOOT_FLASH_SD:
305 case BOOT_FLASH_EMMC:
306 sprintf(cmd, "%d", instance);
307 env_set("boot_device", "mmc");
308 env_set("boot_instance", cmd);
309 break;
310 case BOOT_FLASH_NAND:
311 env_set("boot_device", "nand");
312 env_set("boot_instance", "0");
313 break;
314 case BOOT_FLASH_NOR:
315 env_set("boot_device", "nor");
316 env_set("boot_instance", "0");
317 break;
318 default:
319 pr_debug("unexpected boot mode = %x\n", boot_mode);
320 break;
321 }
322}
323
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200324/*
325 * If there is no MAC address in the environment, then it will be initialized
326 * (silently) from the value in the OTP.
327 */
328static int setup_mac_address(void)
329{
330#if defined(CONFIG_NET)
331 int ret;
332 int i;
333 u32 otp[2];
334 uchar enetaddr[6];
335 struct udevice *dev;
336
337 /* MAC already in environment */
338 if (eth_env_get_enetaddr("ethaddr", enetaddr))
339 return 0;
340
341 ret = uclass_get_device_by_driver(UCLASS_MISC,
342 DM_GET_DRIVER(stm32mp_bsec),
343 &dev);
344 if (ret)
345 return ret;
346
347 ret = misc_read(dev, BSEC_OTP_MAC * 4 + STM32_BSEC_OTP_OFFSET,
348 otp, sizeof(otp));
Simon Glass8729b1a2018-11-06 15:21:39 -0700349 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200350 return ret;
351
352 for (i = 0; i < 6; i++)
353 enetaddr[i] = ((uint8_t *)&otp)[i];
354
355 if (!is_valid_ethaddr(enetaddr)) {
356 pr_err("invalid MAC address in OTP %pM", enetaddr);
357 return -EINVAL;
358 }
359 pr_debug("OTP MAC address = %pM\n", enetaddr);
360 ret = !eth_env_set_enetaddr("ethaddr", enetaddr);
361 if (!ret)
362 pr_err("Failed to set mac address %pM from OTP: %d\n",
363 enetaddr, ret);
364#endif
365
366 return 0;
367}
368
369static int setup_serial_number(void)
370{
371 char serial_string[25];
372 u32 otp[3] = {0, 0, 0 };
373 struct udevice *dev;
374 int ret;
375
376 if (env_get("serial#"))
377 return 0;
378
379 ret = uclass_get_device_by_driver(UCLASS_MISC,
380 DM_GET_DRIVER(stm32mp_bsec),
381 &dev);
382 if (ret)
383 return ret;
384
385 ret = misc_read(dev, BSEC_OTP_SERIAL * 4 + STM32_BSEC_OTP_OFFSET,
386 otp, sizeof(otp));
Simon Glass8729b1a2018-11-06 15:21:39 -0700387 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200388 return ret;
389
390 sprintf(serial_string, "%08x%08x%08x", otp[0], otp[1], otp[2]);
391 env_set("serial#", serial_string);
392
393 return 0;
394}
395
Patrick Delaunay08772f62018-03-20 10:54:53 +0100396int arch_misc_init(void)
397{
398 setup_boot_mode();
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200399 setup_mac_address();
400 setup_serial_number();
Patrick Delaunay08772f62018-03-20 10:54:53 +0100401
402 return 0;
403}