blob: ed7d9f61dcd571386ef3bfd753c0dfe246d96f39 [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>
Simon Glass9edefc22019-11-14 12:57:37 -07007#include <cpu_func.h>
Patrick Delaunay320d2662018-05-17 14:50:46 +02008#include <debug_uart.h>
Simon Glass9fb625c2019-08-01 09:46:51 -06009#include <env.h>
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020010#include <misc.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010011#include <asm/io.h>
12#include <asm/arch/stm32.h>
Patrick Delaunay96583cd2018-03-19 19:09:21 +010013#include <asm/arch/sys_proto.h>
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020014#include <dm/device.h>
Patrick Delaunay08772f62018-03-20 10:54:53 +010015#include <dm/uclass.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010016
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010017/* RCC register */
18#define RCC_TZCR (STM32_RCC_BASE + 0x00)
19#define RCC_DBGCFGR (STM32_RCC_BASE + 0x080C)
20#define RCC_BDCR (STM32_RCC_BASE + 0x0140)
21#define RCC_MP_APB5ENSETR (STM32_RCC_BASE + 0x0208)
Patrick Delaunay59a54e32019-02-27 17:01:26 +010022#define RCC_MP_AHB5ENSETR (STM32_RCC_BASE + 0x0210)
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010023#define RCC_BDCR_VSWRST BIT(31)
24#define RCC_BDCR_RTCSRC GENMASK(17, 16)
25#define RCC_DBGCFGR_DBGCKEN BIT(8)
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010026
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010027/* Security register */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010028#define ETZPC_TZMA1_SIZE (STM32_ETZPC_BASE + 0x04)
29#define ETZPC_DECPROT0 (STM32_ETZPC_BASE + 0x10)
30
31#define TZC_GATE_KEEPER (STM32_TZC_BASE + 0x008)
32#define TZC_REGION_ATTRIBUTE0 (STM32_TZC_BASE + 0x110)
33#define TZC_REGION_ID_ACCESS0 (STM32_TZC_BASE + 0x114)
34
35#define TAMP_CR1 (STM32_TAMP_BASE + 0x00)
36
37#define PWR_CR1 (STM32_PWR_BASE + 0x00)
38#define PWR_CR1_DBP BIT(8)
39
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010040/* DBGMCU register */
Patrick Delaunay96583cd2018-03-19 19:09:21 +010041#define DBGMCU_IDC (STM32_DBGMCU_BASE + 0x00)
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +010042#define DBGMCU_APB4FZ1 (STM32_DBGMCU_BASE + 0x2C)
43#define DBGMCU_APB4FZ1_IWDG2 BIT(2)
Patrick Delaunay96583cd2018-03-19 19:09:21 +010044#define DBGMCU_IDC_DEV_ID_MASK GENMASK(11, 0)
45#define DBGMCU_IDC_DEV_ID_SHIFT 0
46#define DBGMCU_IDC_REV_ID_MASK GENMASK(31, 16)
47#define DBGMCU_IDC_REV_ID_SHIFT 16
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010048
Patrick Delaunay59a54e32019-02-27 17:01:26 +010049/* GPIOZ registers */
50#define GPIOZ_SECCFGR 0x54004030
51
Patrick Delaunay08772f62018-03-20 10:54:53 +010052/* boot interface from Bootrom
53 * - boot instance = bit 31:16
54 * - boot device = bit 15:0
55 */
56#define BOOTROM_PARAM_ADDR 0x2FFC0078
57#define BOOTROM_MODE_MASK GENMASK(15, 0)
58#define BOOTROM_MODE_SHIFT 0
59#define BOOTROM_INSTANCE_MASK GENMASK(31, 16)
60#define BOOTROM_INSTANCE_SHIFT 16
61
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020062/* BSEC OTP index */
Patrick Delaunay35d568f2019-02-27 17:01:13 +010063#define BSEC_OTP_RPN 1
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020064#define BSEC_OTP_SERIAL 13
Patrick Delaunay35d568f2019-02-27 17:01:13 +010065#define BSEC_OTP_PKG 16
Patrick Delaunay7f7deb02018-05-17 15:24:07 +020066#define BSEC_OTP_MAC 57
67
Patrick Delaunay35d568f2019-02-27 17:01:13 +010068/* Device Part Number (RPN) = OTP_DATA1 lower 8 bits */
69#define RPN_SHIFT 0
70#define RPN_MASK GENMASK(7, 0)
71
72/* Package = bit 27:29 of OTP16
73 * - 100: LBGA448 (FFI) => AA = LFBGA 18x18mm 448 balls p. 0.8mm
74 * - 011: LBGA354 (LCI) => AB = LFBGA 16x16mm 359 balls p. 0.8mm
75 * - 010: TFBGA361 (FFC) => AC = TFBGA 12x12mm 361 balls p. 0.5mm
76 * - 001: TFBGA257 (LCC) => AD = TFBGA 10x10mm 257 balls p. 0.5mm
77 * - others: Reserved
78 */
79#define PKG_SHIFT 27
80#define PKG_MASK GENMASK(2, 0)
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);
Patrick Delaunay59a54e32019-02-27 17:01:26 +0100138
139 /* GPIOZ: deactivate the security */
140 writel(BIT(0), RCC_MP_AHB5ENSETR);
141 writel(0x0, GPIOZ_SECCFGR);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100142}
Patrick Delaunayabf26782019-02-12 11:44:39 +0100143#endif /* CONFIG_STM32MP1_TRUSTED */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100144
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100145/*
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100146 * Debug init
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100147 */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100148static void dbgmcu_init(void)
149{
150 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
151
152 /* Freeze IWDG2 if Cortex-A7 is in debug mode */
153 setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2);
154}
155#endif /* !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) */
156
Patrick Delaunayabf26782019-02-12 11:44:39 +0100157#if !defined(CONFIG_STM32MP1_TRUSTED) && \
158 (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100159/* get bootmode from ROM code boot context: saved in TAMP register */
160static void update_bootmode(void)
161{
162 u32 boot_mode;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100163 u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR);
164 u32 bootrom_device, bootrom_instance;
165
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100166 /* enable TAMP clock = RTCAPBEN */
167 writel(BIT(8), RCC_MP_APB5ENSETR);
168
169 /* read bootrom context */
Patrick Delaunay08772f62018-03-20 10:54:53 +0100170 bootrom_device =
171 (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT;
172 bootrom_instance =
173 (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT;
174 boot_mode =
175 ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) |
176 ((bootrom_instance << BOOT_INSTANCE_SHIFT) &
177 BOOT_INSTANCE_MASK);
178
179 /* save the boot mode in TAMP backup register */
180 clrsetbits_le32(TAMP_BOOT_CONTEXT,
181 TAMP_BOOT_MODE_MASK,
182 boot_mode << TAMP_BOOT_MODE_SHIFT);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100183}
Patrick Delaunay08772f62018-03-20 10:54:53 +0100184#endif
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100185
186u32 get_bootmode(void)
187{
188 /* read bootmode from TAMP backup register */
189 return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >>
190 TAMP_BOOT_MODE_SHIFT;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100191}
192
193/*
194 * Early system init
195 */
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100196int arch_cpu_init(void)
197{
Patrick Delaunay320d2662018-05-17 14:50:46 +0200198 u32 boot_mode;
199
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100200 /* early armv7 timer init: needed for polling */
201 timer_init();
202
203#if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)
204 dbgmcu_init();
Patrick Delaunayabf26782019-02-12 11:44:39 +0100205#ifndef CONFIG_STM32MP1_TRUSTED
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100206 security_init();
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100207 update_bootmode();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100208#endif
Patrick Delaunayabf26782019-02-12 11:44:39 +0100209#endif
Patrick Delaunay320d2662018-05-17 14:50:46 +0200210
Patrick Delaunay320d2662018-05-17 14:50:46 +0200211 boot_mode = get_bootmode();
212
213 if ((boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART)
214 gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE;
215#if defined(CONFIG_DEBUG_UART) && \
Patrick Delaunayabf26782019-02-12 11:44:39 +0100216 !defined(CONFIG_STM32MP1_TRUSTED) && \
Patrick Delaunay320d2662018-05-17 14:50:46 +0200217 (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
218 else
219 debug_uart_init();
220#endif
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100221
222 return 0;
223}
224
Patrick Delaunaycda3dcb2018-03-19 19:09:20 +0100225void enable_caches(void)
226{
227 /* Enable D-cache. I-cache is already enabled in start.S */
228 dcache_enable();
229}
230
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100231static u32 read_idc(void)
232{
233 setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN);
234
235 return readl(DBGMCU_IDC);
236}
237
238u32 get_cpu_rev(void)
239{
240 return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT;
241}
242
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100243static u32 get_otp(int index, int shift, int mask)
244{
245 int ret;
246 struct udevice *dev;
247 u32 otp = 0;
248
249 ret = uclass_get_device_by_driver(UCLASS_MISC,
250 DM_GET_DRIVER(stm32mp_bsec),
251 &dev);
252
253 if (!ret)
254 ret = misc_read(dev, STM32_BSEC_SHADOW(index),
255 &otp, sizeof(otp));
256
257 return (otp >> shift) & mask;
258}
259
260/* Get Device Part Number (RPN) from OTP */
261static u32 get_cpu_rpn(void)
262{
263 return get_otp(BSEC_OTP_RPN, RPN_SHIFT, RPN_MASK);
264}
265
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100266u32 get_cpu_type(void)
267{
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100268 u32 id;
269
270 id = (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT;
271
272 return (id << 16) | get_cpu_rpn();
273}
274
275/* Get Package options from OTP */
Patrick Delaunay24cb4582019-07-05 17:20:13 +0200276u32 get_cpu_package(void)
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100277{
278 return get_otp(BSEC_OTP_PKG, PKG_SHIFT, PKG_MASK);
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100279}
280
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100281#if defined(CONFIG_DISPLAY_CPUINFO)
282int print_cpuinfo(void)
283{
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100284 char *cpu_s, *cpu_r, *pkg;
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100285
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100286 /* MPUs Part Numbers */
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100287 switch (get_cpu_type()) {
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100288 case CPU_STM32MP157Cxx:
289 cpu_s = "157C";
290 break;
291 case CPU_STM32MP157Axx:
292 cpu_s = "157A";
293 break;
294 case CPU_STM32MP153Cxx:
295 cpu_s = "153C";
296 break;
297 case CPU_STM32MP153Axx:
298 cpu_s = "153A";
299 break;
300 case CPU_STM32MP151Cxx:
301 cpu_s = "151C";
302 break;
303 case CPU_STM32MP151Axx:
304 cpu_s = "151A";
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100305 break;
306 default:
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100307 cpu_s = "????";
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100308 break;
309 }
310
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100311 /* Package */
312 switch (get_cpu_package()) {
313 case PKG_AA_LBGA448:
314 pkg = "AA";
315 break;
316 case PKG_AB_LBGA354:
317 pkg = "AB";
318 break;
319 case PKG_AC_TFBGA361:
320 pkg = "AC";
321 break;
322 case PKG_AD_TFBGA257:
323 pkg = "AD";
324 break;
325 default:
326 pkg = "??";
327 break;
328 }
329
330 /* REVISION */
Patrick Delaunay96583cd2018-03-19 19:09:21 +0100331 switch (get_cpu_rev()) {
332 case CPU_REVA:
333 cpu_r = "A";
334 break;
335 case CPU_REVB:
336 cpu_r = "B";
337 break;
338 default:
339 cpu_r = "?";
340 break;
341 }
342
Patrick Delaunay35d568f2019-02-27 17:01:13 +0100343 printf("CPU: STM32MP%s%s Rev.%s\n", cpu_s, pkg, cpu_r);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100344
345 return 0;
346}
347#endif /* CONFIG_DISPLAY_CPUINFO */
348
Patrick Delaunay08772f62018-03-20 10:54:53 +0100349static void setup_boot_mode(void)
350{
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100351 const u32 serial_addr[] = {
352 STM32_USART1_BASE,
353 STM32_USART2_BASE,
354 STM32_USART3_BASE,
355 STM32_UART4_BASE,
356 STM32_UART5_BASE,
357 STM32_USART6_BASE,
358 STM32_UART7_BASE,
359 STM32_UART8_BASE
360 };
Patrick Delaunay08772f62018-03-20 10:54:53 +0100361 char cmd[60];
362 u32 boot_ctx = readl(TAMP_BOOT_CONTEXT);
363 u32 boot_mode =
364 (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT;
Patrick Delaunaye609e132019-06-21 15:26:39 +0200365 unsigned int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1;
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100366 u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100367 struct udevice *dev;
368 int alias;
Patrick Delaunay08772f62018-03-20 10:54:53 +0100369
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100370 pr_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n",
371 __func__, boot_ctx, boot_mode, instance, forced_mode);
Patrick Delaunay08772f62018-03-20 10:54:53 +0100372 switch (boot_mode & TAMP_BOOT_DEVICE_MASK) {
373 case BOOT_SERIAL_UART:
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100374 if (instance > ARRAY_SIZE(serial_addr))
375 break;
376 /* serial : search associated alias in devicetree */
377 sprintf(cmd, "serial@%x", serial_addr[instance]);
378 if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev))
379 break;
380 if (fdtdec_get_alias_seq(gd->fdt_blob, "serial",
381 dev_of_offset(dev), &alias))
382 break;
383 sprintf(cmd, "%d", alias);
384 env_set("boot_device", "serial");
Patrick Delaunay08772f62018-03-20 10:54:53 +0100385 env_set("boot_instance", cmd);
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +0100386
387 /* restore console on uart when not used */
388 if (gd->cur_serial_dev != dev) {
389 gd->flags &= ~(GD_FLG_SILENT |
390 GD_FLG_DISABLE_CONSOLE);
391 printf("serial boot with console enabled!\n");
392 }
Patrick Delaunay08772f62018-03-20 10:54:53 +0100393 break;
394 case BOOT_SERIAL_USB:
395 env_set("boot_device", "usb");
396 env_set("boot_instance", "0");
397 break;
398 case BOOT_FLASH_SD:
399 case BOOT_FLASH_EMMC:
400 sprintf(cmd, "%d", instance);
401 env_set("boot_device", "mmc");
402 env_set("boot_instance", cmd);
403 break;
404 case BOOT_FLASH_NAND:
405 env_set("boot_device", "nand");
406 env_set("boot_instance", "0");
407 break;
408 case BOOT_FLASH_NOR:
409 env_set("boot_device", "nor");
410 env_set("boot_instance", "0");
411 break;
412 default:
413 pr_debug("unexpected boot mode = %x\n", boot_mode);
414 break;
415 }
Patrick Delaunay9a2ba282019-02-27 17:01:20 +0100416
417 switch (forced_mode) {
418 case BOOT_FASTBOOT:
419 printf("Enter fastboot!\n");
420 env_set("preboot", "env set preboot; fastboot 0");
421 break;
422 case BOOT_STM32PROG:
423 env_set("boot_device", "usb");
424 env_set("boot_instance", "0");
425 break;
426 case BOOT_UMS_MMC0:
427 case BOOT_UMS_MMC1:
428 case BOOT_UMS_MMC2:
429 printf("Enter UMS!\n");
430 instance = forced_mode - BOOT_UMS_MMC0;
431 sprintf(cmd, "env set preboot; ums 0 mmc %d", instance);
432 env_set("preboot", cmd);
433 break;
434 case BOOT_RECOVERY:
435 env_set("preboot", "env set preboot; run altbootcmd");
436 break;
437 case BOOT_NORMAL:
438 break;
439 default:
440 pr_debug("unexpected forced boot mode = %x\n", forced_mode);
441 break;
442 }
443
444 /* clear TAMP for next reboot */
445 clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL);
Patrick Delaunay08772f62018-03-20 10:54:53 +0100446}
447
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200448/*
449 * If there is no MAC address in the environment, then it will be initialized
450 * (silently) from the value in the OTP.
451 */
452static int setup_mac_address(void)
453{
454#if defined(CONFIG_NET)
455 int ret;
456 int i;
457 u32 otp[2];
458 uchar enetaddr[6];
459 struct udevice *dev;
460
461 /* MAC already in environment */
462 if (eth_env_get_enetaddr("ethaddr", enetaddr))
463 return 0;
464
465 ret = uclass_get_device_by_driver(UCLASS_MISC,
466 DM_GET_DRIVER(stm32mp_bsec),
467 &dev);
468 if (ret)
469 return ret;
470
Patrick Delaunay17f1f9b2019-02-27 17:01:29 +0100471 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_MAC),
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200472 otp, sizeof(otp));
Simon Glass8729b1a2018-11-06 15:21:39 -0700473 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200474 return ret;
475
476 for (i = 0; i < 6; i++)
477 enetaddr[i] = ((uint8_t *)&otp)[i];
478
479 if (!is_valid_ethaddr(enetaddr)) {
Manivannan Sadhasivambc9487d2019-05-02 13:26:45 +0530480 pr_err("invalid MAC address in OTP %pM\n", enetaddr);
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200481 return -EINVAL;
482 }
483 pr_debug("OTP MAC address = %pM\n", enetaddr);
484 ret = !eth_env_set_enetaddr("ethaddr", enetaddr);
485 if (!ret)
486 pr_err("Failed to set mac address %pM from OTP: %d\n",
487 enetaddr, ret);
488#endif
489
490 return 0;
491}
492
493static int setup_serial_number(void)
494{
495 char serial_string[25];
496 u32 otp[3] = {0, 0, 0 };
497 struct udevice *dev;
498 int ret;
499
500 if (env_get("serial#"))
501 return 0;
502
503 ret = uclass_get_device_by_driver(UCLASS_MISC,
504 DM_GET_DRIVER(stm32mp_bsec),
505 &dev);
506 if (ret)
507 return ret;
508
Patrick Delaunay17f1f9b2019-02-27 17:01:29 +0100509 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_SERIAL),
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200510 otp, sizeof(otp));
Simon Glass8729b1a2018-11-06 15:21:39 -0700511 if (ret < 0)
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200512 return ret;
513
Patrick Delaunay8983ba22019-02-27 17:01:25 +0100514 sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]);
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200515 env_set("serial#", serial_string);
516
517 return 0;
518}
519
Patrick Delaunay08772f62018-03-20 10:54:53 +0100520int arch_misc_init(void)
521{
522 setup_boot_mode();
Patrick Delaunay7f7deb02018-05-17 15:24:07 +0200523 setup_mac_address();
524 setup_serial_number();
Patrick Delaunay08772f62018-03-20 10:54:53 +0100525
526 return 0;
527}