Patrick Delaunay | 2f6030a | 2024-01-15 15:05:49 +0100 | [diff] [blame] | 1 | // 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 Chotard | 539fec3 | 2024-01-15 15:05:50 +0100 | [diff] [blame] | 7 | #include <misc.h> |
Patrick Delaunay | 3fa644b | 2024-01-15 15:05:51 +0100 | [diff] [blame] | 8 | #include <net.h> |
Patrick Delaunay | 2f6030a | 2024-01-15 15:05:49 +0100 | [diff] [blame] | 9 | #include <asm/arch/sys_proto.h> |
| 10 | #include <dm/device.h> |
Patrice Chotard | 539fec3 | 2024-01-15 15:05:50 +0100 | [diff] [blame] | 11 | #include <dm/uclass.h> |
Patrick Delaunay | 2f6030a | 2024-01-15 15:05:49 +0100 | [diff] [blame] | 12 | |
Patrick Delaunay | 3fa644b | 2024-01-15 15:05:51 +0100 | [diff] [blame] | 13 | /* max: 8 OTP for 5 mac address on stm32mp2*/ |
| 14 | #define MAX_NB_OTP 8 |
| 15 | |
Patrick Delaunay | 2f6030a | 2024-01-15 15:05:49 +0100 | [diff] [blame] | 16 | /* used when CONFIG_DISPLAY_CPUINFO is activated */ |
| 17 | int 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 Chotard | 539fec3 | 2024-01-15 15:05:50 +0100 | [diff] [blame] | 26 | |
| 27 | int 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 Delaunay | 3fa644b | 2024-01-15 15:05:51 +0100 | [diff] [blame] | 53 | |
| 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 | } |