]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
87e29878 AF |
2 | /* |
3 | * Based on board/freescale/common/sys_eeprom.c | |
4 | * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor | |
5 | * | |
6 | * This defines the API for storing board information in the | |
7 | * eeprom. It has been adapted from an earlier version of the | |
8 | * Freescale API, but has a number of key differences. Because | |
9 | * the two APIs are independent and may diverge further, the | |
10 | * Varisys version of the API is implemented separately here. | |
87e29878 AF |
11 | */ |
12 | ||
13 | #include <common.h> | |
14 | #include <command.h> | |
9fb625ce | 15 | #include <env.h> |
87e29878 AF |
16 | #include <i2c.h> |
17 | #include <linux/ctype.h> | |
3db71108 | 18 | #include <u-boot/crc.h> |
87e29878 AF |
19 | |
20 | #include "eeprom.h" | |
21 | ||
22 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID_MAC | |
23 | #define MAX_NUM_PORTS CONFIG_SYS_I2C_EEPROM_NXID_MAC | |
24 | #else | |
25 | #define MAX_NUM_PORTS 8 | |
26 | #endif | |
27 | #define NXID_VERSION 0 | |
28 | ||
29 | /** | |
30 | * static eeprom: EEPROM layout for NXID formats | |
31 | * | |
32 | * See Freescale application note AN3638 for details. | |
33 | */ | |
34 | static struct __attribute__ ((__packed__)) eeprom { | |
35 | u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */ | |
36 | u8 sn[12]; /* 0x04 - 0x0F Serial Number */ | |
37 | u8 errata[5]; /* 0x10 - 0x14 Errata Level */ | |
38 | u8 date[6]; /* 0x15 - 0x1a Build Date */ | |
39 | u8 res_0; /* 0x1b Reserved */ | |
40 | u32 version; /* 0x1c - 0x1f NXID Version */ | |
41 | u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */ | |
42 | u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */ | |
43 | u8 tempcalflags; /* 0x2a Temperature Calibration Flags */ | |
44 | u8 res_1[21]; /* 0x2b - 0x3f Reserved */ | |
45 | u8 mac_count; /* 0x40 Number of MAC addresses */ | |
46 | u8 mac_flag; /* 0x41 MAC table flags */ | |
47 | u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */ | |
48 | u32 crc; /* x+1 CRC32 checksum */ | |
49 | } e; | |
50 | ||
51 | /* Set to 1 if we've read EEPROM into memory */ | |
52 | static int has_been_read; | |
53 | ||
54 | /* Is this a valid NXID EEPROM? */ | |
55 | #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \ | |
56 | (e.id[2] == 'I') || (e.id[3] == 'D')) | |
57 | ||
58 | /** Fixed ID field in EEPROM */ | |
59 | static unsigned char uid[16]; | |
60 | ||
61 | static int eeprom_bus_num = -1; | |
62 | static int eeprom_addr; | |
63 | static int eeprom_addr_len; | |
64 | ||
65 | /** | |
66 | * This must be called before any eeprom access. | |
67 | */ | |
68 | void init_eeprom(int bus_num, int addr, int addr_len) | |
69 | { | |
70 | eeprom_bus_num = bus_num; | |
71 | eeprom_addr = addr; | |
72 | eeprom_addr_len = addr_len; | |
73 | } | |
74 | ||
75 | /** | |
76 | * show_eeprom - display the contents of the EEPROM | |
77 | */ | |
78 | void show_eeprom(void) | |
79 | { | |
80 | int i; | |
81 | unsigned int crc; | |
82 | ||
83 | /* EEPROM tag ID, either CCID or NXID */ | |
84 | printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], | |
85 | be32_to_cpu(e.version)); | |
86 | ||
87 | /* Serial number */ | |
88 | printf("SN: %s\n", e.sn); | |
89 | ||
90 | printf("UID: "); | |
91 | for (i = 0; i < 16; i++) | |
92 | printf("%02x", uid[i]); | |
93 | printf("\n"); | |
94 | ||
95 | /* Errata level. */ | |
96 | printf("Errata: %s\n", e.errata); | |
97 | ||
98 | /* Build date, BCD date values, as YYMMDDhhmmss */ | |
99 | printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n", | |
100 | e.date[0], e.date[1], e.date[2], | |
101 | e.date[3] & 0x7F, e.date[4], e.date[5], | |
102 | e.date[3] & 0x80 ? "PM" : ""); | |
103 | ||
104 | /* Show MAC addresses */ | |
105 | for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) { | |
106 | u8 *p = e.mac[i]; | |
107 | ||
108 | printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i, | |
109 | p[0], p[1], p[2], p[3], p[4], p[5]); | |
110 | } | |
111 | ||
112 | crc = crc32(0, (void *)&e, sizeof(e) - 4); | |
113 | ||
114 | if (crc == be32_to_cpu(e.crc)) | |
115 | printf("CRC: %08x\n", be32_to_cpu(e.crc)); | |
116 | else | |
117 | printf("CRC: %08x (should be %08x)\n", | |
118 | be32_to_cpu(e.crc), crc); | |
119 | ||
120 | #ifdef DEBUG | |
121 | printf("EEPROM dump: (0x%x bytes)\n", sizeof(e)); | |
122 | for (i = 0; i < sizeof(e); i++) { | |
123 | if ((i % 16) == 0) | |
124 | printf("%02X: ", i); | |
125 | printf("%02X ", ((u8 *)&e)[i]); | |
126 | if (((i % 16) == 15) || (i == sizeof(e) - 1)) | |
127 | printf("\n"); | |
128 | } | |
129 | #endif | |
130 | } | |
131 | ||
132 | /** | |
133 | * read_eeprom - read the EEPROM into memory | |
134 | */ | |
135 | int read_eeprom(void) | |
136 | { | |
137 | int ret; | |
138 | unsigned int bus; | |
139 | ||
140 | if (eeprom_bus_num < 0) { | |
141 | printf("EEPROM not configured\n"); | |
142 | return -1; | |
143 | } | |
144 | ||
145 | if (has_been_read) | |
146 | return 0; | |
147 | ||
148 | bus = i2c_get_bus_num(); | |
149 | i2c_set_bus_num(eeprom_bus_num); | |
150 | ||
151 | ret = i2c_read(eeprom_addr, 0, eeprom_addr_len, | |
152 | (void *)&e, sizeof(e)); | |
153 | ||
154 | ||
155 | /* Fixed address of ID field */ | |
156 | i2c_read(0x5f, 0x80, 1, uid, 16); | |
157 | ||
158 | i2c_set_bus_num(bus); | |
159 | ||
160 | #ifdef DEBUG | |
161 | show_eeprom(); | |
162 | #endif | |
163 | ||
164 | has_been_read = (ret == 0) ? 1 : 0; | |
165 | ||
166 | return ret; | |
167 | } | |
168 | ||
169 | /** | |
170 | * update_crc - update the CRC | |
171 | * | |
172 | * This function should be called after each update to the EEPROM structure, | |
173 | * to make sure the CRC is always correct. | |
174 | */ | |
175 | static void update_crc(void) | |
176 | { | |
177 | u32 crc, crc_offset = offsetof(struct eeprom, crc); | |
178 | ||
179 | crc = crc32(0, (void *)&e, crc_offset); | |
180 | e.crc = cpu_to_be32(crc); | |
181 | } | |
182 | ||
183 | /** | |
184 | * prog_eeprom - write the EEPROM from memory | |
185 | */ | |
186 | static int prog_eeprom(void) | |
187 | { | |
188 | int ret = 0; | |
189 | int i; | |
190 | void *p; | |
191 | unsigned int bus; | |
192 | ||
193 | if (eeprom_bus_num < 0) { | |
194 | printf("EEPROM not configured\n"); | |
195 | return -1; | |
196 | } | |
197 | ||
198 | /* Set the reserved values to 0xFF */ | |
199 | e.res_0 = 0xFF; | |
200 | memset(e.res_1, 0xFF, sizeof(e.res_1)); | |
201 | update_crc(); | |
202 | ||
203 | bus = i2c_get_bus_num(); | |
204 | i2c_set_bus_num(eeprom_bus_num); | |
205 | ||
206 | /* | |
207 | * The AT24C02 datasheet says that data can only be written in page | |
208 | * mode, which means 8 bytes at a time, and it takes up to 5ms to | |
209 | * complete a given write. | |
210 | */ | |
211 | for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) { | |
212 | ret = i2c_write(eeprom_addr, i, eeprom_addr_len, | |
213 | p, min((int)(sizeof(e) - i), 8)); | |
214 | if (ret) | |
215 | break; | |
216 | udelay(5000); /* 5ms write cycle timing */ | |
217 | } | |
218 | ||
219 | if (!ret) { | |
220 | /* Verify the write by reading back the EEPROM and comparing */ | |
221 | struct eeprom e2; | |
222 | ||
223 | ret = i2c_read(eeprom_addr, 0, | |
224 | eeprom_addr_len, (void *)&e2, sizeof(e2)); | |
225 | if (!ret && memcmp(&e, &e2, sizeof(e))) | |
226 | ret = -1; | |
227 | } | |
228 | ||
229 | i2c_set_bus_num(bus); | |
230 | ||
231 | if (ret) { | |
232 | printf("Programming failed.\n"); | |
233 | has_been_read = 0; | |
234 | return -1; | |
235 | } | |
236 | ||
237 | printf("Programming passed.\n"); | |
238 | return 0; | |
239 | } | |
240 | ||
241 | /** | |
242 | * h2i - converts hex character into a number | |
243 | * | |
244 | * This function takes a hexadecimal character (e.g. '7' or 'C') and returns | |
245 | * the integer equivalent. | |
246 | */ | |
247 | static inline u8 h2i(char p) | |
248 | { | |
249 | if ((p >= '0') && (p <= '9')) | |
250 | return p - '0'; | |
251 | ||
252 | if ((p >= 'A') && (p <= 'F')) | |
253 | return (p - 'A') + 10; | |
254 | ||
255 | if ((p >= 'a') && (p <= 'f')) | |
256 | return (p - 'a') + 10; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | /** | |
262 | * set_date - stores the build date into the EEPROM | |
263 | * | |
264 | * This function takes a pointer to a string in the format "YYMMDDhhmmss" | |
265 | * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string, | |
266 | * and stores it in the build date field of the EEPROM local copy. | |
267 | */ | |
268 | static void set_date(const char *string) | |
269 | { | |
270 | unsigned int i; | |
271 | ||
272 | if (strlen(string) != 12) { | |
273 | printf("Usage: mac date YYMMDDhhmmss\n"); | |
274 | return; | |
275 | } | |
276 | ||
277 | for (i = 0; i < 6; i++) | |
278 | e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]); | |
279 | ||
280 | update_crc(); | |
281 | } | |
282 | ||
283 | /** | |
284 | * set_mac_address - stores a MAC address into the EEPROM | |
285 | * | |
286 | * This function takes a pointer to MAC address string | |
287 | * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and | |
288 | * stores it in one of the MAC address fields of the EEPROM local copy. | |
289 | */ | |
290 | static void set_mac_address(unsigned int index, const char *string) | |
291 | { | |
292 | char *p = (char *)string; | |
293 | unsigned int i; | |
294 | ||
295 | if ((index >= MAX_NUM_PORTS) || !string) { | |
296 | printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n"); | |
297 | return; | |
298 | } | |
299 | ||
300 | for (i = 0; *p && (i < 6); i++) { | |
301 | e.mac[index][i] = simple_strtoul(p, &p, 16); | |
302 | if (*p == ':') | |
303 | p++; | |
304 | } | |
305 | ||
306 | update_crc(); | |
307 | } | |
308 | ||
09140113 | 309 | int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
87e29878 AF |
310 | { |
311 | char cmd; | |
312 | ||
313 | if (argc == 1) { | |
314 | show_eeprom(); | |
315 | return 0; | |
316 | } | |
317 | ||
318 | cmd = argv[1][0]; | |
319 | ||
320 | if (cmd == 'r') { | |
321 | read_eeprom(); | |
322 | return 0; | |
323 | } | |
324 | ||
325 | if (cmd == 'i') { | |
326 | memcpy(e.id, "NXID", sizeof(e.id)); | |
327 | e.version = NXID_VERSION; | |
328 | update_crc(); | |
329 | return 0; | |
330 | } | |
331 | ||
332 | if (!is_valid) { | |
333 | printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n"); | |
334 | return 0; | |
335 | } | |
336 | ||
337 | if (argc == 2) { | |
338 | switch (cmd) { | |
339 | case 's': /* save */ | |
340 | prog_eeprom(); | |
341 | break; | |
342 | default: | |
343 | return cmd_usage(cmdtp); | |
344 | } | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
349 | /* We know we have at least one parameter */ | |
350 | ||
351 | switch (cmd) { | |
352 | case 'n': /* serial number */ | |
353 | memset(e.sn, 0, sizeof(e.sn)); | |
354 | strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1); | |
355 | update_crc(); | |
356 | break; | |
357 | case 'e': /* errata */ | |
358 | memset(e.errata, 0, 5); | |
359 | strncpy((char *)e.errata, argv[2], 4); | |
360 | update_crc(); | |
361 | break; | |
362 | case 'd': /* date BCD format YYMMDDhhmmss */ | |
363 | set_date(argv[2]); | |
364 | break; | |
365 | case 'p': /* MAC table size */ | |
366 | e.mac_count = simple_strtoul(argv[2], NULL, 16); | |
367 | update_crc(); | |
368 | break; | |
369 | case '0' ... '9': /* "mac 0" through "mac 22" */ | |
370 | set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]); | |
371 | break; | |
372 | case 'h': /* help */ | |
373 | default: | |
374 | return cmd_usage(cmdtp); | |
375 | } | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
380 | int mac_read_from_generic_eeprom(const char *envvar, int chip, | |
381 | int address, int mac_bus) | |
382 | { | |
383 | int ret; | |
384 | unsigned int bus; | |
385 | unsigned char mac[6]; | |
386 | char ethaddr[18]; | |
387 | ||
388 | bus = i2c_get_bus_num(); | |
389 | i2c_set_bus_num(mac_bus); | |
390 | ||
391 | ret = i2c_read(chip, address, 1, mac, 6); | |
392 | ||
393 | i2c_set_bus_num(bus); | |
394 | ||
395 | if (!ret) { | |
396 | sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", | |
397 | mac[0], | |
398 | mac[1], | |
399 | mac[2], | |
400 | mac[3], | |
401 | mac[4], | |
402 | mac[5]); | |
403 | ||
404 | printf("MAC: %s\n", ethaddr); | |
382bee57 | 405 | env_set(envvar, ethaddr); |
87e29878 AF |
406 | } |
407 | ||
408 | return ret; | |
409 | } | |
410 | ||
411 | void mac_read_from_fixed_id(void) | |
412 | { | |
413 | #ifdef CONFIG_SYS_I2C_MAC1_CHIP_ADDR | |
414 | mac_read_from_generic_eeprom("ethaddr", CONFIG_SYS_I2C_MAC1_CHIP_ADDR, | |
415 | CONFIG_SYS_I2C_MAC1_DATA_ADDR, CONFIG_SYS_I2C_MAC1_BUS); | |
416 | #endif | |
417 | #ifdef CONFIG_SYS_I2C_MAC2_CHIP_ADDR | |
418 | mac_read_from_generic_eeprom("eth1addr", CONFIG_SYS_I2C_MAC2_CHIP_ADDR, | |
419 | CONFIG_SYS_I2C_MAC2_DATA_ADDR, CONFIG_SYS_I2C_MAC2_BUS); | |
420 | #endif | |
421 | } | |
422 | ||
423 | /** | |
424 | * mac_read_from_eeprom - read the MAC addresses from EEPROM | |
425 | * | |
426 | * This function reads the MAC addresses from EEPROM and sets the | |
427 | * appropriate environment variables for each one read. | |
428 | * | |
429 | * The environment variables are only set if they haven't been set already. | |
430 | * This ensures that any user-saved variables are never overwritten. | |
431 | * | |
432 | * This function must be called after relocation. | |
433 | * | |
434 | * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0 | |
435 | * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is | |
436 | * located at a different offset. | |
437 | */ | |
438 | int mac_read_from_eeprom_common(void) | |
439 | { | |
440 | unsigned int i; | |
441 | u32 crc, crc_offset = offsetof(struct eeprom, crc); | |
442 | u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */ | |
443 | ||
444 | puts("EEPROM: "); | |
445 | ||
446 | if (read_eeprom()) { | |
447 | printf("Read failed.\n"); | |
448 | return 0; | |
449 | } | |
450 | ||
451 | if (!is_valid) { | |
452 | printf("Invalid ID (%02x %02x %02x %02x)\n", | |
453 | e.id[0], e.id[1], e.id[2], e.id[3]); | |
454 | return 0; | |
455 | } | |
456 | ||
457 | crc = crc32(0, (void *)&e, crc_offset); | |
458 | crcp = (void *)&e + crc_offset; | |
459 | if (crc != be32_to_cpu(*crcp)) { | |
460 | printf("CRC mismatch (%08x != %08x)\n", crc, | |
461 | be32_to_cpu(e.crc)); | |
462 | return 0; | |
463 | } | |
464 | ||
465 | /* | |
466 | * MAC address #9 in v1 occupies the same position as the CRC in v0. | |
467 | * Erase it so that it's not mistaken for a MAC address. We'll | |
468 | * update the CRC later. | |
469 | */ | |
470 | if (e.version == 0) | |
471 | memset(e.mac[8], 0xff, 6); | |
472 | ||
473 | for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) { | |
474 | if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) && | |
475 | memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) { | |
476 | char ethaddr[18]; | |
477 | char enetvar[9]; | |
478 | ||
479 | sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", | |
480 | e.mac[i][0], | |
481 | e.mac[i][1], | |
482 | e.mac[i][2], | |
483 | e.mac[i][3], | |
484 | e.mac[i][4], | |
485 | e.mac[i][5]); | |
486 | sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i); | |
487 | /* Only initialize environment variables that are blank | |
488 | * (i.e. have not yet been set) | |
489 | */ | |
00caae6d | 490 | if (!env_get(enetvar)) |
382bee57 | 491 | env_set(enetvar, ethaddr); |
87e29878 AF |
492 | } |
493 | } | |
494 | ||
495 | printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], | |
496 | be32_to_cpu(e.version)); | |
497 | ||
498 | return 0; | |
499 | } |