1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
4 * York Sun (yorksun@freescale.com)
5 * Haiying Wang (haiying.wang@freescale.com)
6 * Timur Tabi (timur@freescale.com)
13 #include <linux/ctype.h>
14 #include <linux/delay.h>
15 #include <u-boot/crc.h>
17 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
18 #include "../common/eeprom.h"
19 #define MAX_NUM_PORTS 8
22 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
23 /* some boards with non-256-bytes EEPROM have special define */
24 /* for MAX_NUM_PORTS in board-specific file */
26 #define MAX_NUM_PORTS 16
28 #define NXID_VERSION 1
32 * static eeprom: EEPROM layout for CCID or NXID formats
34 * See application note AN3638 for details.
36 static struct __attribute__ ((__packed__)) eeprom {
37 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
38 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
39 u8 major; /* 0x04 Board revision, major */
40 u8 minor; /* 0x05 Board revision, minor */
41 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
42 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
43 u8 date[6]; /* 0x12 - 0x17 Build Date */
44 u8 res_0[40]; /* 0x18 - 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 - 0x71 MAC addresses */
48 u32 crc; /* 0x72 CRC32 checksum */
50 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
51 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
52 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
53 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
54 u8 date[6]; /* 0x15 - 0x1a Build Date */
55 u8 res_0; /* 0x1b Reserved */
56 u32 version; /* 0x1c - 0x1f NXID Version */
57 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
58 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
59 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
60 u8 res_1[21]; /* 0x2b - 0x3f Reserved */
61 u8 mac_count; /* 0x40 Number of MAC addresses */
62 u8 mac_flag; /* 0x41 MAC table flags */
63 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0xa1 MAC addresses */
64 u8 res_2[90]; /* 0xa2 - 0xfb Reserved */
65 u32 crc; /* 0xfc - 0xff CRC32 checksum */
69 /* Set to 1 if we've read EEPROM into memory */
70 static int has_been_read = 0;
72 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
73 /* Is this a valid NXID EEPROM? */
74 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
75 (e.id[2] == 'I') || (e.id[3] == 'D'))
78 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
79 /* Is this a valid CCID EEPROM? */
80 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
81 (e.id[2] == 'I') || (e.id[3] == 'D'))
85 * show_eeprom - display the contents of the EEPROM
87 static void show_eeprom(void)
92 /* EEPROM tag ID, either CCID or NXID */
93 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
94 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
95 be32_to_cpu(e.version));
97 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
101 printf("SN: %s\n", e.sn);
104 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
105 printf("Errata: %s\n", e.errata);
107 printf("Errata: %c%c\n",
108 e.errata[0] ? e.errata[0] : '.',
109 e.errata[1] ? e.errata[1] : '.');
112 /* Build date, BCD date values, as YYMMDDhhmmss */
113 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
114 e.date[0], e.date[1], e.date[2],
115 e.date[3] & 0x7F, e.date[4], e.date[5],
116 e.date[3] & 0x80 ? "PM" : "");
118 /* Show MAC addresses */
119 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
123 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
124 p[0], p[1], p[2], p[3], p[4], p[5]);
127 crc = crc32(0, (void *)&e, sizeof(e) - 4);
129 if (crc == be32_to_cpu(e.crc))
130 printf("CRC: %08x\n", be32_to_cpu(e.crc));
132 printf("CRC: %08x (should be %08x)\n",
133 be32_to_cpu(e.crc), crc);
136 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
137 for (i = 0; i < sizeof(e); i++) {
140 printf("%02X ", ((u8 *)&e)[i]);
141 if (((i % 16) == 15) || (i == sizeof(e) - 1))
148 * read_eeprom - read the EEPROM into memory
150 static int read_eeprom(void)
153 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
154 #if !CONFIG_IS_ENABLED(DM_I2C)
162 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
163 #if !CONFIG_IS_ENABLED(DM_I2C)
164 bus = i2c_get_bus_num();
165 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
169 #if !CONFIG_IS_ENABLED(DM_I2C)
170 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
171 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
172 (void *)&e, sizeof(e));
175 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
176 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
177 CONFIG_SYS_I2C_EEPROM_ADDR,
178 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev);
180 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
181 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev);
184 ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e));
187 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
188 #if !CONFIG_IS_ENABLED(DM_I2C)
189 i2c_set_bus_num(bus);
197 has_been_read = (ret == 0) ? 1 : 0;
203 * update_crc - update the CRC
205 * This function should be called after each update to the EEPROM structure,
206 * to make sure the CRC is always correct.
208 static void update_crc(void)
212 crc = crc32(0, (void *)&e, sizeof(e) - 4);
213 e.crc = cpu_to_be32(crc);
217 * prog_eeprom - write the EEPROM from memory
219 static int prog_eeprom(void)
224 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
225 #if !CONFIG_IS_ENABLED(DM_I2C)
230 /* Set the reserved values to 0xFF */
231 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
233 memset(e.res_1, 0xFF, sizeof(e.res_1));
235 memset(e.res_0, 0xFF, sizeof(e.res_0));
239 #if !CONFIG_IS_ENABLED(DM_I2C)
240 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
241 bus = i2c_get_bus_num();
242 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
247 * The AT24C02 datasheet says that data can only be written in page
248 * mode, which means 8 bytes at a time, and it takes up to 5ms to
249 * complete a given write.
251 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
252 #if !CONFIG_IS_ENABLED(DM_I2C)
253 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i,
254 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
255 p, min((int)(sizeof(e) - i), 8));
258 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
259 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
260 CONFIG_SYS_I2C_EEPROM_ADDR,
261 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
264 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
265 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
269 ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i),
274 udelay(5000); /* 5ms write cycle timing */
278 /* Verify the write by reading back the EEPROM and comparing */
281 #if !CONFIG_IS_ENABLED(DM_I2C)
282 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
283 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
284 (void *)&e2, sizeof(e2));
287 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
288 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
289 CONFIG_SYS_I2C_EEPROM_ADDR,
290 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
293 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
294 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
298 ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2));
300 if (!ret && memcmp(&e, &e2, sizeof(e)))
304 #if !CONFIG_IS_ENABLED(DM_I2C)
305 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
306 i2c_set_bus_num(bus);
311 printf("Programming failed.\n");
316 printf("Programming passed.\n");
321 * h2i - converts hex character into a number
323 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
324 * the integer equivalent.
326 static inline u8 h2i(char p)
328 if ((p >= '0') && (p <= '9'))
331 if ((p >= 'A') && (p <= 'F'))
332 return (p - 'A') + 10;
334 if ((p >= 'a') && (p <= 'f'))
335 return (p - 'a') + 10;
341 * set_date - stores the build date into the EEPROM
343 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
344 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
345 * and stores it in the build date field of the EEPROM local copy.
347 static void set_date(const char *string)
351 if (strlen(string) != 12) {
352 printf("Usage: mac date YYMMDDhhmmss\n");
356 for (i = 0; i < 6; i++)
357 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
363 * set_mac_address - stores a MAC address into the EEPROM
365 * This function takes a pointer to MAC address string
366 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
367 * stores it in one of the MAC address fields of the EEPROM local copy.
369 static void set_mac_address(unsigned int index, const char *string)
371 char *p = (char *) string;
374 if ((index >= MAX_NUM_PORTS) || !string) {
375 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
379 for (i = 0; *p && (i < 6); i++) {
380 e.mac[index][i] = hextoul(p, &p);
388 int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
405 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
406 memcpy(e.id, "NXID", sizeof(e.id));
407 e.version = cpu_to_be32(NXID_VERSION);
409 memcpy(e.id, "CCID", sizeof(e.id));
416 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
426 return CMD_RET_USAGE;
432 /* We know we have at least one parameter */
435 case 'n': /* serial number */
436 memset(e.sn, 0, sizeof(e.sn));
437 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
440 case 'e': /* errata */
441 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
442 memset(e.errata, 0, 5);
443 strncpy((char *)e.errata, argv[2], 4);
445 e.errata[0] = argv[2][0];
446 e.errata[1] = argv[2][1];
450 case 'd': /* date BCD format YYMMDDhhmmss */
453 case 'p': /* MAC table size */
454 e.mac_count = hextoul(argv[2], NULL);
457 case '0' ... '9': /* "mac 0" through "mac 22" */
458 set_mac_address(dectoul(argv[1], NULL), argv[2]);
462 return cmd_usage(cmdtp);
469 * mac_read_from_eeprom - read the MAC addresses from EEPROM
471 * This function reads the MAC addresses from EEPROM and sets the
472 * appropriate environment variables for each one read.
474 * The environment variables are only set if they haven't been set already.
475 * This ensures that any user-saved variables are never overwritten.
477 * This function must be called after relocation.
479 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
480 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
481 * located at a different offset.
483 int mac_read_from_eeprom(void)
486 u32 crc, crc_offset = offsetof(struct eeprom, crc);
487 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
492 printf("Read failed.\n");
497 printf("Invalid ID (%02x %02x %02x %02x)\n",
498 e.id[0], e.id[1], e.id[2], e.id[3]);
502 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
504 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
505 * to where it is in v0.
511 crc = crc32(0, (void *)&e, crc_offset);
512 crcp = (void *)&e + crc_offset;
513 if (crc != be32_to_cpu(*crcp)) {
514 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
518 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
520 * MAC address #9 in v1 occupies the same position as the CRC in v0.
521 * Erase it so that it's not mistaken for a MAC address. We'll
522 * update the CRC later.
525 memset(e.mac[8], 0xff, 6);
528 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
529 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
530 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
534 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
541 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
542 /* Only initialize environment variables that are blank
543 * (i.e. have not yet been set)
545 if (!env_get(enetvar))
546 env_set(enetvar, ethaddr);
550 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
551 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
552 be32_to_cpu(e.version));
554 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
557 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
559 * Now we need to upconvert the data into v1 format. We do this last so
560 * that at boot time, U-Boot will still say "NXID v0".
562 if (e.version == 0) {
563 e.version = cpu_to_be32(NXID_VERSION);
571 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
574 * get_cpu_board_revision - get the CPU board revision on 85xx boards
576 * Read the EEPROM to determine the board revision.
578 * This function is called before relocation, so we need to read a private
579 * copy of the EEPROM into a local variable on the stack.
581 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
582 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
583 * so that the SPD code will work. This means that all pre-relocation I2C
584 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
585 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
586 * this function is called. Oh well.
588 unsigned int get_cpu_board_revision(void)
590 struct board_eeprom {
591 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
592 u8 major; /* 0x04 Board revision, major */
593 u8 minor; /* 0x05 Board revision, minor */
596 #if !CONFIG_IS_ENABLED(DM_I2C)
597 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
598 (void *)&be, sizeof(be));
602 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
603 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
604 CONFIG_SYS_I2C_EEPROM_ADDR,
605 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
608 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
609 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
613 dm_i2c_read(dev, 0, (void *)&be, sizeof(be));
616 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
617 return MPC85XX_CPU_BOARD_REV(0, 0);
619 if ((be.major == 0xff) && (be.minor == 0xff))
620 return MPC85XX_CPU_BOARD_REV(0, 0);
622 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
627 "[read|save|id|num|errata|date|ports|port_number]\n"
629 " - read EEPROM content into memory data structure\n"
631 " - save memory data structure to the EEPROM\n"
633 " - program system id per hard coded value\n"
635 " - program system serial number to value string\n"
636 "mac errata string\n"
637 " - program errata data to value string\n"
638 "mac date YYMMDDhhmmss\n"
639 " - program date to string value YYMMDDhhmmss\n"
641 " - program the number of network ports to integer N\n"
643 " - program MAC addr for port X [X=0,1..] to colon separated string");
647 "display and program the system ID and MAC addresses in EEPROM",