blob: fa56b0d2e0f190a0b7f3e1b9a4536ffafd0d6f99 [file] [log] [blame]
Patrick Delaunay2f6030a2024-01-15 15:05:49 +01001// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause
2/*
3 * Copyright (C) 2024, STMicroelectronics - All Rights Reserved
4 */
5
6#include <env.h>
Patrice Chotard539fec32024-01-15 15:05:50 +01007#include <misc.h>
Patrick Delaunay3fa644b2024-01-15 15:05:51 +01008#include <net.h>
Patrick Delaunay2f6030a2024-01-15 15:05:49 +01009#include <asm/arch/sys_proto.h>
10#include <dm/device.h>
Patrice Chotard539fec32024-01-15 15:05:50 +010011#include <dm/uclass.h>
Patrick Delaunay2f6030a2024-01-15 15:05:49 +010012
Patrick Delaunay3fa644b2024-01-15 15:05:51 +010013/* max: 8 OTP for 5 mac address on stm32mp2*/
14#define MAX_NB_OTP 8
15
Patrick Delaunay2f6030a2024-01-15 15:05:49 +010016/* used when CONFIG_DISPLAY_CPUINFO is activated */
17int print_cpuinfo(void)
18{
19 char name[SOC_NAME_SIZE];
20
21 get_soc_name(name);
22 printf("CPU: %s\n", name);
23
24 return 0;
25}
Patrice Chotard539fec32024-01-15 15:05:50 +010026
27int setup_serial_number(void)
28{
29 char serial_string[25];
30 u32 otp[3] = {0, 0, 0 };
31 struct udevice *dev;
32 int ret;
33
34 if (env_get("serial#"))
35 return 0;
36
37 ret = uclass_get_device_by_driver(UCLASS_MISC,
38 DM_DRIVER_GET(stm32mp_bsec),
39 &dev);
40 if (ret)
41 return ret;
42
43 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_SERIAL),
44 otp, sizeof(otp));
45 if (ret < 0)
46 return ret;
47
48 sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]);
49 env_set("serial#", serial_string);
50
51 return 0;
52}
Patrick Delaunay3fa644b2024-01-15 15:05:51 +010053
54/*
55 * If there is no MAC address in the environment, then it will be initialized
56 * (silently) from the value in the OTP.
57 */
58__weak int setup_mac_address(void)
59{
60 int ret;
61 int i;
62 u32 otp[MAX_NB_OTP];
63 uchar enetaddr[ARP_HLEN];
64 struct udevice *dev;
65 int nb_eth, nb_otp, index;
66
67 if (!IS_ENABLED(CONFIG_NET))
68 return 0;
69
70 nb_eth = get_eth_nb();
71 if (!nb_eth)
72 return 0;
73
74 /* 6 bytes for each MAC addr and 4 bytes for each OTP */
75 nb_otp = DIV_ROUND_UP(ARP_HLEN * nb_eth, 4);
76 if (nb_otp > MAX_NB_OTP) {
77 log_err("invalid number of OTP = %d, max = %d\n", nb_otp, MAX_NB_OTP);
78 return -EINVAL;
79 }
80
81 ret = uclass_get_device_by_driver(UCLASS_MISC,
82 DM_DRIVER_GET(stm32mp_bsec),
83 &dev);
84 if (ret)
85 return ret;
86
87 ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_MAC), otp, 4 * nb_otp);
88 if (ret < 0)
89 return ret;
90
91 for (index = 0; index < nb_eth; index++) {
92 /* MAC already in environment */
93 if (eth_env_get_enetaddr_by_index("eth", index, enetaddr))
94 continue;
95
96 for (i = 0; i < ARP_HLEN; i++)
97 enetaddr[i] = ((uint8_t *)&otp)[i + ARP_HLEN * index];
98
99 /* skip FF:FF:FF:FF:FF:FF */
100 if (is_broadcast_ethaddr(enetaddr))
101 continue;
102
103 if (!is_valid_ethaddr(enetaddr)) {
104 log_err("invalid MAC address %d in OTP %pM\n",
105 index, enetaddr);
106 return -EINVAL;
107 }
108 log_debug("OTP MAC address %d = %pM\n", index, enetaddr);
109 ret = eth_env_set_enetaddr_by_index("eth", index, enetaddr);
110 if (ret) {
111 log_err("Failed to set mac address %pM from OTP: %d\n",
112 enetaddr, ret);
113 return ret;
114 }
115 }
116
117 return 0;
118}