blob: 5d79bde73319bcc8cc9b7d14cf8d4a2d924c9952 [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 */
Patrick Delaunay35d568f2019-02-27 17:01:13 +010058#define BSEC_OTP_RPN 1
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020059#define BSEC_OTP_SERIAL 13
Patrick Delaunay35d568f2019-02-27 17:01:13 +010060#define BSEC_OTP_PKG 16
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020061#define BSEC_OTP_MAC 57
62
Patrick Delaunay35d568f2019-02-27 17:01:13 +010063/* Device Part Number (RPN) = OTP_DATA1 lower 8 bits */
64#define RPN_SHIFT 0
65#define RPN_MASK GENMASK(7, 0)
66
67/* Package = bit 27:29 of OTP16
68 * - 100: LBGA448 (FFI) => AA = LFBGA 18x18mm 448 balls p. 0.8mm
69 * - 011: LBGA354 (LCI) => AB = LFBGA 16x16mm 359 balls p. 0.8mm
70 * - 010: TFBGA361 (FFC) => AC = TFBGA 12x12mm 361 balls p. 0.5mm
71 * - 001: TFBGA257 (LCC) => AD = TFBGA 10x10mm 257 balls p. 0.5mm
72 * - others: Reserved
73 */
74#define PKG_SHIFT 27
75#define PKG_MASK GENMASK(2, 0)
76
77#define PKG_AA_LBGA448 4
78#define PKG_AB_LBGA354 3
79#define PKG_AC_TFBGA361 2
80#define PKG_AD_TFBGA257 1
81
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010082#if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
Patrick Delaunayabf26782019-02-12 11:44:39 +010083#ifndef CONFIG_STM32MP1_TRUSTED
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010084static void security_init(void)
85{
86 /* Disable the backup domain write protection */
87 /* the protection is enable at each reset by hardware */
88 /* And must be disable by software */
89 setbits_le32(PWR_CR1, PWR_CR1_DBP);
90
91 while (!(readl(PWR_CR1) & PWR_CR1_DBP))
92 ;
93
94 /* If RTC clock isn't enable so this is a cold boot then we need
95 * to reset the backup domain
96 */
97 if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) {
98 setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
99 while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST))
100 ;
101 clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST);
102 }
103
104 /* allow non secure access in Write/Read for all peripheral */
105 writel(GENMASK(25, 0), ETZPC_DECPROT0);
106
107 /* Open SYSRAM for no secure access */
108 writel(0x0, ETZPC_TZMA1_SIZE);
109
110 /* enable TZC1 TZC2 clock */
111 writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR);
112
113 /* Region 0 set to no access by default */
114 /* bit 0 / 16 => nsaid0 read/write Enable
115 * bit 1 / 17 => nsaid1 read/write Enable
116 * ...
117 * bit 15 / 31 => nsaid15 read/write Enable
118 */
119 writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0);
120 /* bit 30 / 31 => Secure Global Enable : write/read */
121 /* bit 0 / 1 => Region Enable for filter 0/1 */
122 writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0);
123
124 /* Enable Filter 0 and 1 */
125 setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1));
126
127 /* RCC trust zone deactivated */
128 writel(0x0, RCC_TZCR);
129
130 /* TAMP: deactivate the internal tamper
131 * Bit 23 ITAMP8E: monotonic counter overflow
132 * Bit 20 ITAMP5E: RTC calendar overflow
133 * Bit 19 ITAMP4E: HSE monitoring
134 * Bit 18 ITAMP3E: LSE monitoring
135 * Bit 16 ITAMP1E: RTC power domain supply monitoring
136 */
137 writel(0x0, TAMP_CR1);
138}
Patrick Delaunayabf26782019-02-12 11:44:39 +0100139#endif /* CONFIG_STM32MP1_TRUSTED */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100140
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100141/*
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100142 * Debug init
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100143 */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100144static void dbgmcu_init(void)
145{
146 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
147
148 /* Freeze IWDG2 if Cortex-A7 is in debug mode */
149 setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2);
150}
151#endif /* !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) */
152
Patrick Delaunayabf26782019-02-12 11:44:39 +0100153#if !defined(CONFIG_STM32MP1_TRUSTED) && \
154 (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100155/* get bootmode from ROM code boot context: saved in TAMP register */
156static void update_bootmode(void)
157{
158 u32 boot_mode;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100159 u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR);
160 u32 bootrom_device, bootrom_instance;
161
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100162 /* enable TAMP clock = RTCAPBEN */
163 writel(BIT(8), RCC_MP_APB5ENSETR);
164
165 /* read bootrom context */
Patrick Delaunay08772f62018-03-20 10:54:53 +0100166 bootrom_device =
167 (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT;
168 bootrom_instance =
169 (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT;
170 boot_mode =
171 ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) |
172 ((bootrom_instance << BOOT_INSTANCE_SHIFT) &
173 BOOT_INSTANCE_MASK);
174
175 /* save the boot mode in TAMP backup register */
176 clrsetbits_le32(TAMP_BOOT_CONTEXT,
177 TAMP_BOOT_MODE_MASK,
178 boot_mode << TAMP_BOOT_MODE_SHIFT);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100179}
Patrick Delaunay08772f62018-03-20 10:54:53 +0100180#endif
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100181
182u32 get_bootmode(void)
183{
184 /* read bootmode from TAMP backup register */
185 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
186 TAMP_BOOT_MODE_SHIFT;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100187}
188
189/*
190 * Early system init
191 */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100192int arch_cpu_init(void)
193{
Patrick Delaunay320d2662018-05-17 14:50:46 +0200194 u32 boot_mode;
195
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100196 /* early armv7 timer init: needed for polling */
197 timer_init();
198
199#if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
200 dbgmcu_init();
Patrick Delaunayabf26782019-02-12 11:44:39 +0100201#ifndef CONFIG_STM32MP1_TRUSTED
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100202 security_init();
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100203 update_bootmode();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100204#endif
Patrick Delaunayabf26782019-02-12 11:44:39 +0100205#endif
Patrick Delaunay320d2662018-05-17 14:50:46 +0200206
Patrick Delaunay320d2662018-05-17 14:50:46 +0200207 boot_mode = get_bootmode();
208
209 if ((boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
210 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
211#if defined(CONFIG_DEBUG_UART) && \
Patrick Delaunayabf26782019-02-12 11:44:39 +0100212 !defined(CONFIG_STM32MP1_TRUSTED) && \
Patrick Delaunay320d2662018-05-17 14:50:46 +0200213 (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
214 else
215 debug_uart_init();
216#endif
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100217
218 return 0;
219}
220
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100221void enable_caches(void)
222{
223 /* Enable D-cache. I-cache is already enabled in start.S */
224 dcache_enable();
225}
226
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100227static u32 read_idc(void)
228{
229 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
230
231 return readl(DBGMCU_IDC);
232}
233
234u32 get_cpu_rev(void)
235{
236 return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
237}
238
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100239static u32 get_otp(int index, int shift, int mask)
240{
241 int ret;
242 struct udevice *dev;
243 u32 otp = 0;
244
245 ret = uclass_get_device_by_driver(UCLASS_MISC,
246 DM_GET_DRIVER(stm32mp_bsec),
247 &dev);
248
249 if (!ret)
250 ret = misc_read(dev, STM32_BSEC_SHADOW(index),
251 &otp, sizeof(otp));
252
253 return (otp >> shift) & mask;
254}
255
256/* Get Device Part Number (RPN) from OTP */
257static u32 get_cpu_rpn(void)
258{
259 return get_otp(BSEC_OTP_RPN, RPN_SHIFT, RPN_MASK);
260}
261
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100262u32 get_cpu_type(void)
263{
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100264 u32 id;
265
266 id = (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT;
267
268 return (id << 16) | get_cpu_rpn();
269}
270
271/* Get Package options from OTP */
272static u32 get_cpu_package(void)
273{
274 return get_otp(BSEC_OTP_PKG, PKG_SHIFT, PKG_MASK);
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100275}
276
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100277#if defined(CONFIG_DISPLAY_CPUINFO)
278int print_cpuinfo(void)
279{
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100280 char *cpu_s, *cpu_r, *pkg;
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100281
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100282 /* MPUs Part Numbers */
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100283 switch (get_cpu_type()) {
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100284 case CPU_STM32MP157Cxx:
285 cpu_s = "157C";
286 break;
287 case CPU_STM32MP157Axx:
288 cpu_s = "157A";
289 break;
290 case CPU_STM32MP153Cxx:
291 cpu_s = "153C";
292 break;
293 case CPU_STM32MP153Axx:
294 cpu_s = "153A";
295 break;
296 case CPU_STM32MP151Cxx:
297 cpu_s = "151C";
298 break;
299 case CPU_STM32MP151Axx:
300 cpu_s = "151A";
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100301 break;
302 default:
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100303 cpu_s = "????";
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100304 break;
305 }
306
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100307 /* Package */
308 switch (get_cpu_package()) {
309 case PKG_AA_LBGA448:
310 pkg = "AA";
311 break;
312 case PKG_AB_LBGA354:
313 pkg = "AB";
314 break;
315 case PKG_AC_TFBGA361:
316 pkg = "AC";
317 break;
318 case PKG_AD_TFBGA257:
319 pkg = "AD";
320 break;
321 default:
322 pkg = "??";
323 break;
324 }
325
326 /* REVISION */
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100327 switch (get_cpu_rev()) {
328 case CPU_REVA:
329 cpu_r = "A";
330 break;
331 case CPU_REVB:
332 cpu_r = "B";
333 break;
334 default:
335 cpu_r = "?";
336 break;
337 }
338
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100339 printf("CPU: STM32MP%s%s Rev.%s\n", cpu_s, pkg, cpu_r);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100340
341 return 0;
342}
343#endif /* CONFIG_DISPLAY_CPUINFO */
344
Patrick Delaunay08772f62018-03-20 10:54:53 +0100345static void setup_boot_mode(void)
346{
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100347 const u32 serial_addr[] = {
348 STM32_USART1_BASE,
349 STM32_USART2_BASE,
350 STM32_USART3_BASE,
351 STM32_UART4_BASE,
352 STM32_UART5_BASE,
353 STM32_USART6_BASE,
354 STM32_UART7_BASE,
355 STM32_UART8_BASE
356 };
Patrick Delaunay08772f62018-03-20 10:54:53 +0100357 char cmd[60];
358 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
359 u32 boot_mode =
360 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
361 int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100362 u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100363 struct udevice *dev;
364 int alias;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100365
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100366 pr_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n",
367 __func__, boot_ctx, boot_mode, instance, forced_mode);
Patrick Delaunay08772f62018-03-20 10:54:53 +0100368 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
369 case BOOT_SERIAL_UART:
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100370 if (instance > ARRAY_SIZE(serial_addr))
371 break;
372 /* serial : search associated alias in devicetree */
373 sprintf(cmd, "serial@%x", serial_addr[instance]);
374 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev))
375 break;
376 if (fdtdec_get_alias_seq(gd->fdt_blob, "serial",
377 dev_of_offset(dev), &alias))
378 break;
379 sprintf(cmd, "%d", alias);
380 env_set("boot_device", "serial");
Patrick Delaunay08772f62018-03-20 10:54:53 +0100381 env_set("boot_instance", cmd);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100382
383 /* restore console on uart when not used */
384 if (gd->cur_serial_dev != dev) {
385 gd->flags &= ~(GD_FLG_SILENT |
386 GD_FLG_DISABLE_CONSOLE);
387 printf("serial boot with console enabled!\n");
388 }
Patrick Delaunay08772f62018-03-20 10:54:53 +0100389 break;
390 case BOOT_SERIAL_USB:
391 env_set("boot_device", "usb");
392 env_set("boot_instance", "0");
393 break;
394 case BOOT_FLASH_SD:
395 case BOOT_FLASH_EMMC:
396 sprintf(cmd, "%d", instance);
397 env_set("boot_device", "mmc");
398 env_set("boot_instance", cmd);
399 break;
400 case BOOT_FLASH_NAND:
401 env_set("boot_device", "nand");
402 env_set("boot_instance", "0");
403 break;
404 case BOOT_FLASH_NOR:
405 env_set("boot_device", "nor");
406 env_set("boot_instance", "0");
407 break;
408 default:
409 pr_debug("unexpected boot mode = %x\n", boot_mode);
410 break;
411 }
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100412
413 switch (forced_mode) {
414 case BOOT_FASTBOOT:
415 printf("Enter fastboot!\n");
416 env_set("preboot", "env set preboot; fastboot 0");
417 break;
418 case BOOT_STM32PROG:
419 env_set("boot_device", "usb");
420 env_set("boot_instance", "0");
421 break;
422 case BOOT_UMS_MMC0:
423 case BOOT_UMS_MMC1:
424 case BOOT_UMS_MMC2:
425 printf("Enter UMS!\n");
426 instance = forced_mode - BOOT_UMS_MMC0;
427 sprintf(cmd, "env set preboot; ums 0 mmc %d", instance);
428 env_set("preboot", cmd);
429 break;
430 case BOOT_RECOVERY:
431 env_set("preboot", "env set preboot; run altbootcmd");
432 break;
433 case BOOT_NORMAL:
434 break;
435 default:
436 pr_debug("unexpected forced boot mode = %x\n", forced_mode);
437 break;
438 }
439
440 /* clear TAMP for next reboot */
441 clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
Patrick Delaunay08772f62018-03-20 10:54:53 +0100442}
443
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200444/*
445 * If there is no MAC address in the environment, then it will be initialized
446 * (silently) from the value in the OTP.
447 */
448static int setup_mac_address(void)
449{
450#if defined(CONFIG_NET)
451 int ret;
452 int i;
453 u32 otp[2];
454 uchar enetaddr[6];
455 struct udevice *dev;
456
457 /* MAC already in environment */
458 if (eth_env_get_enetaddr("ethaddr", enetaddr))
459 return 0;
460
461 ret = uclass_get_device_by_driver(UCLASS_MISC,
462 DM_GET_DRIVER(stm32mp_bsec),
463 &dev);
464 if (ret)
465 return ret;
466
467 ret = misc_read(dev, BSEC_OTP_MAC * 4 + STM32_BSEC_OTP_OFFSET,
468 otp, sizeof(otp));
Simon Glass8729b1a2018-11-06 15:21:39 -0700469 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200470 return ret;
471
472 for (i = 0; i < 6; i++)
473 enetaddr[i] = ((uint8_t *)&otp)[i];
474
475 if (!is_valid_ethaddr(enetaddr)) {
476 pr_err("invalid MAC address in OTP %pM", enetaddr);
477 return -EINVAL;
478 }
479 pr_debug("OTP MAC address = %pM\n", enetaddr);
480 ret = !eth_env_set_enetaddr("ethaddr", enetaddr);
481 if (!ret)
482 pr_err("Failed to set mac address %pM from OTP: %d\n",
483 enetaddr, ret);
484#endif
485
486 return 0;
487}
488
489static int setup_serial_number(void)
490{
491 char serial_string[25];
492 u32 otp[3] = {0, 0, 0 };
493 struct udevice *dev;
494 int ret;
495
496 if (env_get("serial#"))
497 return 0;
498
499 ret = uclass_get_device_by_driver(UCLASS_MISC,
500 DM_GET_DRIVER(stm32mp_bsec),
501 &dev);
502 if (ret)
503 return ret;
504
505 ret = misc_read(dev, BSEC_OTP_SERIAL * 4 + STM32_BSEC_OTP_OFFSET,
506 otp, sizeof(otp));
Simon Glass8729b1a2018-11-06 15:21:39 -0700507 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200508 return ret;
509
Patrick Delaunay8983ba22019-02-27 17:01:25 +0100510 sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]);
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200511 env_set("serial#", serial_string);
512
513 return 0;
514}
515
Patrick Delaunay08772f62018-03-20 10:54:53 +0100516int arch_misc_init(void)
517{
518 setup_boot_mode();
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200519 setup_mac_address();
520 setup_serial_number();
Patrick Delaunay08772f62018-03-20 10:54:53 +0100521
522 return 0;
523}