blob: 71010803f555c9f13d22c0f93b7fa2721111b814 [file] [log] [blame]
Philip Oberfichtner62a09922022-07-26 15:04:50 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022 Marek Vasut <marex@denx.de>
4 * Copyright 2022 DENX Software Engineering GmbH, Philip Oberfichtner <pro@denx.de>
5 */
6
Philip Oberfichtner62a09922022-07-26 15:04:50 +02007#include <dm.h>
8#include <i2c_eeprom.h>
9#include <net.h>
Christoph Niedermaier276458b2024-12-07 00:04:18 +010010#include <u-boot/crc.h>
Philip Oberfichtner62a09922022-07-26 15:04:50 +020011
12#include "dh_common.h"
13
Christoph Niedermaierb37608d2024-12-07 00:04:20 +010014static int on_dh_som_serial_number(const char *name, const char *value, enum env_op op,
15 int flags)
16{
17 env_set("SN", value);
18 return 0;
19}
20
21U_BOOT_ENV_CALLBACK(dh_som_serial_number, on_dh_som_serial_number);
22
23static int on_SN(const char *name, const char *value, enum env_op op, int flags)
24{
25 env_set("dh_som_serial_number", value);
26 return 0;
27}
28
29U_BOOT_ENV_CALLBACK(SN, on_SN);
30
Philip Oberfichtner62a09922022-07-26 15:04:50 +020031bool dh_mac_is_in_env(const char *env)
32{
33 unsigned char enetaddr[6];
34
35 return eth_env_get_enetaddr(env, enetaddr);
36}
37
Marek Vasut29ab1a92024-03-12 22:15:58 +010038int dh_get_mac_is_enabled(const char *alias)
39{
40 ofnode node = ofnode_path(alias);
41
42 if (!ofnode_valid(node))
43 return -EINVAL;
44
45 if (!ofnode_is_enabled(node))
46 return -ENODEV;
47
48 return 0;
49}
50
Christoph Niedermaier276458b2024-12-07 00:04:18 +010051int dh_read_eeprom_id_page(u8 *eeprom_buffer, const char *alias)
52{
53 struct eeprom_id_page *eip = (struct eeprom_id_page *)eeprom_buffer;
54 struct udevice *dev;
55 size_t payload_len;
56 int eeprom_size;
57 u16 crc16_calc;
58 u16 crc16_eip;
59 u8 crc8_calc;
60 ofnode node;
61 int ret;
62
63 node = ofnode_path(alias);
64
65 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, node, &dev);
66 if (ret)
67 return ret;
68
69 eeprom_size = i2c_eeprom_size(dev);
70 if (eeprom_size < 0) {
71 printf("%s: Error getting EEPROM ID page size! ret = %d\n", __func__, ret);
72 return eeprom_size;
73 }
74
75 if (eeprom_size == 0 || eeprom_size > DH_EEPROM_ID_PAGE_MAX_SIZE) {
76 eeprom_size = DH_EEPROM_ID_PAGE_MAX_SIZE;
77 printf("Get invalid EEPROM ID page size %d bytes! Try to read %d bytes.\n",
78 eeprom_size, DH_EEPROM_ID_PAGE_MAX_SIZE);
79 }
80
81 ret = i2c_eeprom_read(dev, 0x0, eeprom_buffer, eeprom_size);
82 if (ret) {
83 printf("%s: Error reading EEPROM ID page! ret = %d\n", __func__, ret);
84 return ret;
85 }
86
87 /* Validate header ID */
88 if (eip->hdr.id[0] != 'D' || eip->hdr.id[1] != 'H' || eip->hdr.id[2] != 'E') {
89 printf("%s: Error validating header ID! (got %c%c%c (0x%02x 0x%02x 0x%02x) != expected DHE)\n",
90 __func__, isprint(eip->hdr.id[0]) ? eip->hdr.id[0] : '.',
91 isprint(eip->hdr.id[1]) ? eip->hdr.id[1] : '.',
92 isprint(eip->hdr.id[2]) ? eip->hdr.id[2] : '.',
93 eip->hdr.id[0], eip->hdr.id[1], eip->hdr.id[2]);
94 return -EINVAL;
95 }
96
97 /* Validate header checksum */
98 crc8_calc = crc8(0xff, eeprom_buffer, offsetof(struct eeprom_id_page, hdr.crc8_hdr));
99 if (eip->hdr.crc8_hdr != crc8_calc) {
100 printf("%s: Error validating header checksum! (got 0x%02x != calc 0x%02x)\n",
101 __func__, eip->hdr.crc8_hdr, crc8_calc);
102 return -EINVAL;
103 }
104
105 /*
106 * Validate header version
107 * The payload is defined by the version specified in the header.
108 * Currently only version 0x10 is defined, so take the length of
109 * the only defined payload as the payload length.
110 */
111 if (eip->hdr.version != DH_EEPROM_ID_PAGE_V1_0) {
112 printf("%s: Error validating version! (0x%02X is not supported)\n",
113 __func__, eip->hdr.version);
114 return -EINVAL;
115 }
116 payload_len = sizeof(eip->pl);
117
118 /* Validate payload checksum */
119 crc16_eip = (eip->hdr.crc16_pl[1] << 8) | eip->hdr.crc16_pl[0];
120 crc16_calc = crc16(0xffff, eeprom_buffer + sizeof(eip->hdr), payload_len);
121 if (crc16_eip != crc16_calc) {
122 printf("%s: Error validating data checksum! (got 0x%02x != calc 0x%02x)\n",
123 __func__, crc16_eip, crc16_calc);
124 return -EINVAL;
125 }
126
127 return 0;
128}
129
130int dh_get_value_from_eeprom_buffer(enum eip_request_values request, u8 *data, int data_len,
131 struct eeprom_id_page *eip)
132{
133 const char fin_chr = (eip->pl.item_prefix & DH_ITEM_PREFIX_FIN_BIT) ?
134 DH_ITEM_PREFIX_FIN_FLASHED_CHR : DH_ITEM_PREFIX_FIN_HALF_CHR;
135 const u8 soc_coded = eip->pl.item_prefix & 0xf;
136 char soc_chr;
137
138 if (!eip)
139 return -EINVAL;
140
141 /* Copy requested data */
142 switch (request) {
143 case DH_MAC0:
144 if (!is_valid_ethaddr(eip->pl.mac0))
145 return -EINVAL;
146
147 if (data_len >= sizeof(eip->pl.mac0))
148 memcpy(data, eip->pl.mac0, sizeof(eip->pl.mac0));
149 else
150 return -EINVAL;
151 break;
152 case DH_MAC1:
153 if (!is_valid_ethaddr(eip->pl.mac1))
154 return -EINVAL;
155
156 if (data_len >= sizeof(eip->pl.mac1))
157 memcpy(data, eip->pl.mac1, sizeof(eip->pl.mac1));
158 else
159 return -EINVAL;
160 break;
161 case DH_ITEM_NUMBER:
162 if (data_len < 8) /* String length must be 7 characters + string termination */
163 return -EINVAL;
164
165 if (soc_coded == DH_ITEM_PREFIX_NXP)
166 soc_chr = DH_ITEM_PREFIX_NXP_CHR;
167 else if (soc_coded == DH_ITEM_PREFIX_ST)
168 soc_chr = DH_ITEM_PREFIX_ST_CHR;
169 else
170 return -EINVAL;
171
172 snprintf(data, data_len, "%c%c%05d", fin_chr, soc_chr,
173 (eip->pl.item_num[0] << 16) | (eip->pl.item_num[1] << 8) |
174 eip->pl.item_num[2]);
175 break;
176 case DH_SERIAL_NUMBER:
177 /*
178 * data_len must be greater than the size of eip->pl.serial,
179 * because there is a string termination needed.
180 */
181 if (data_len <= sizeof(eip->pl.serial))
182 return -EINVAL;
183
184 data[sizeof(eip->pl.serial)] = 0;
185 memcpy(data, eip->pl.serial, sizeof(eip->pl.serial));
186 break;
187 default:
188 return -EINVAL;
189 }
190
191 return 0;
192}
193
Philip Oberfichtner62a09922022-07-26 15:04:50 +0200194int dh_get_mac_from_eeprom(unsigned char *enetaddr, const char *alias)
195{
196 struct udevice *dev;
197 int ret;
198 ofnode node;
199
200 node = ofnode_path(alias);
201 if (!ofnode_valid(node)) {
202 printf("%s: ofnode for %s not found!", __func__, alias);
203 return -ENOENT;
204 }
205
206 ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, node, &dev);
207 if (ret) {
208 printf("%s: Cannot find EEPROM! ret = %d\n", __func__, ret);
209 return ret;
210 }
211
212 ret = i2c_eeprom_read(dev, 0xfa, enetaddr, 0x6);
213 if (ret) {
214 printf("%s: Error reading EEPROM! ret = %d\n", __func__, ret);
215 return ret;
216 }
217
218 if (!is_valid_ethaddr(enetaddr)) {
219 printf("%s: Address read from EEPROM is invalid!\n", __func__);
220 return -EINVAL;
221 }
222
223 return 0;
224}
225
Christoph Niedermaier276458b2024-12-07 00:04:18 +0100226__weak int dh_setup_mac_address(struct eeprom_id_page *eip)
Philip Oberfichtner62a09922022-07-26 15:04:50 +0200227{
228 unsigned char enetaddr[6];
229
230 if (dh_mac_is_in_env("ethaddr"))
231 return 0;
232
Marek Vasut29ab1a92024-03-12 22:15:58 +0100233 if (dh_get_mac_is_enabled("ethernet0"))
234 return 0;
235
Christoph Niedermaier276458b2024-12-07 00:04:18 +0100236 if (!dh_get_value_from_eeprom_buffer(DH_MAC0, enetaddr, sizeof(enetaddr), eip))
237 return eth_env_set_enetaddr("ethaddr", enetaddr);
238
Philip Oberfichtner62a09922022-07-26 15:04:50 +0200239 if (!dh_get_mac_from_eeprom(enetaddr, "eeprom0"))
240 return eth_env_set_enetaddr("ethaddr", enetaddr);
241
242 printf("%s: Unable to set mac address!\n", __func__);
243 return -ENXIO;
244}