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)
14 #include <linux/ctype.h>
15 #include <linux/delay.h>
16 #include <u-boot/crc.h>
18 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
19 #include "../common/eeprom.h"
20 #define MAX_NUM_PORTS 8
23 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
24 /* some boards with non-256-bytes EEPROM have special define */
25 /* for MAX_NUM_PORTS in board-specific file */
27 #define MAX_NUM_PORTS 16
29 #define NXID_VERSION 1
33 * static eeprom: EEPROM layout for CCID or NXID formats
35 * See application note AN3638 for details.
37 static struct __attribute__ ((__packed__)) eeprom {
38 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
39 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
40 u8 major; /* 0x04 Board revision, major */
41 u8 minor; /* 0x05 Board revision, minor */
42 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
43 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
44 u8 date[6]; /* 0x12 - 0x17 Build Date */
45 u8 res_0[40]; /* 0x18 - 0x3f Reserved */
46 u8 mac_count; /* 0x40 Number of MAC addresses */
47 u8 mac_flag; /* 0x41 MAC table flags */
48 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
49 u32 crc; /* 0x72 CRC32 checksum */
51 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
52 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
53 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
54 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
55 u8 date[6]; /* 0x15 - 0x1a Build Date */
56 u8 res_0; /* 0x1b Reserved */
57 u32 version; /* 0x1c - 0x1f NXID Version */
58 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
59 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
60 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
61 u8 res_1[21]; /* 0x2b - 0x3f Reserved */
62 u8 mac_count; /* 0x40 Number of MAC addresses */
63 u8 mac_flag; /* 0x41 MAC table flags */
64 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0xa1 MAC addresses */
65 u8 res_2[90]; /* 0xa2 - 0xfb Reserved */
66 u32 crc; /* 0xfc - 0xff CRC32 checksum */
70 /* Set to 1 if we've read EEPROM into memory */
71 static int has_been_read = 0;
73 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
74 /* Is this a valid NXID EEPROM? */
75 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
76 (e.id[2] == 'I') || (e.id[3] == 'D'))
79 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
80 /* Is this a valid CCID EEPROM? */
81 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
82 (e.id[2] == 'I') || (e.id[3] == 'D'))
86 * show_eeprom - display the contents of the EEPROM
88 static void show_eeprom(void)
93 /* EEPROM tag ID, either CCID or NXID */
94 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
95 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
96 be32_to_cpu(e.version));
98 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
102 printf("SN: %s\n", e.sn);
105 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
106 printf("Errata: %s\n", e.errata);
108 printf("Errata: %c%c\n",
109 e.errata[0] ? e.errata[0] : '.',
110 e.errata[1] ? e.errata[1] : '.');
113 /* Build date, BCD date values, as YYMMDDhhmmss */
114 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
115 e.date[0], e.date[1], e.date[2],
116 e.date[3] & 0x7F, e.date[4], e.date[5],
117 e.date[3] & 0x80 ? "PM" : "");
119 /* Show MAC addresses */
120 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
124 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
125 p[0], p[1], p[2], p[3], p[4], p[5]);
128 crc = crc32(0, (void *)&e, sizeof(e) - 4);
130 if (crc == be32_to_cpu(e.crc))
131 printf("CRC: %08x\n", be32_to_cpu(e.crc));
133 printf("CRC: %08x (should be %08x)\n",
134 be32_to_cpu(e.crc), crc);
137 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
138 for (i = 0; i < sizeof(e); i++) {
141 printf("%02X ", ((u8 *)&e)[i]);
142 if (((i % 16) == 15) || (i == sizeof(e) - 1))
149 * read_eeprom - read the EEPROM into memory
151 static int read_eeprom(void)
154 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
155 #ifndef CONFIG_DM_I2C
163 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
164 #ifndef CONFIG_DM_I2C
165 bus = i2c_get_bus_num();
166 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
170 #ifndef CONFIG_DM_I2C
171 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
172 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
173 (void *)&e, sizeof(e));
176 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
177 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
178 CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev);
180 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev);
183 ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e));
186 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
187 #ifndef CONFIG_DM_I2C
188 i2c_set_bus_num(bus);
196 has_been_read = (ret == 0) ? 1 : 0;
202 * update_crc - update the CRC
204 * This function should be called after each update to the EEPROM structure,
205 * to make sure the CRC is always correct.
207 static void update_crc(void)
211 crc = crc32(0, (void *)&e, sizeof(e) - 4);
212 e.crc = cpu_to_be32(crc);
216 * prog_eeprom - write the EEPROM from memory
218 static int prog_eeprom(void)
223 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
224 #ifndef CONFIG_DM_I2C
229 /* Set the reserved values to 0xFF */
230 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
232 memset(e.res_1, 0xFF, sizeof(e.res_1));
234 memset(e.res_0, 0xFF, sizeof(e.res_0));
238 #ifndef CONFIG_DM_I2C
239 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
240 bus = i2c_get_bus_num();
241 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
246 * The AT24C02 datasheet says that data can only be written in page
247 * mode, which means 8 bytes at a time, and it takes up to 5ms to
248 * complete a given write.
250 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
251 #ifndef CONFIG_DM_I2C
252 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i,
253 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
254 p, min((int)(sizeof(e) - i), 8));
257 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
258 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
259 CONFIG_SYS_I2C_EEPROM_ADDR,
260 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
263 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
264 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
268 ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i),
273 udelay(5000); /* 5ms write cycle timing */
277 /* Verify the write by reading back the EEPROM and comparing */
280 #ifndef CONFIG_DM_I2C
281 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
282 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
283 (void *)&e2, sizeof(e2));
286 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
287 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
288 CONFIG_SYS_I2C_EEPROM_ADDR,
289 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
292 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
293 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
297 ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2));
299 if (!ret && memcmp(&e, &e2, sizeof(e)))
303 #ifndef CONFIG_DM_I2C
304 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
305 i2c_set_bus_num(bus);
310 printf("Programming failed.\n");
315 printf("Programming passed.\n");
320 * h2i - converts hex character into a number
322 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
323 * the integer equivalent.
325 static inline u8 h2i(char p)
327 if ((p >= '0') && (p <= '9'))
330 if ((p >= 'A') && (p <= 'F'))
331 return (p - 'A') + 10;
333 if ((p >= 'a') && (p <= 'f'))
334 return (p - 'a') + 10;
340 * set_date - stores the build date into the EEPROM
342 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
343 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
344 * and stores it in the build date field of the EEPROM local copy.
346 static void set_date(const char *string)
350 if (strlen(string) != 12) {
351 printf("Usage: mac date YYMMDDhhmmss\n");
355 for (i = 0; i < 6; i++)
356 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
362 * set_mac_address - stores a MAC address into the EEPROM
364 * This function takes a pointer to MAC address string
365 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
366 * stores it in one of the MAC address fields of the EEPROM local copy.
368 static void set_mac_address(unsigned int index, const char *string)
370 char *p = (char *) string;
373 if ((index >= MAX_NUM_PORTS) || !string) {
374 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
378 for (i = 0; *p && (i < 6); i++) {
379 e.mac[index][i] = simple_strtoul(p, &p, 16);
387 int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
404 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
405 memcpy(e.id, "NXID", sizeof(e.id));
406 e.version = cpu_to_be32(NXID_VERSION);
408 memcpy(e.id, "CCID", sizeof(e.id));
415 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
425 return cmd_usage(cmdtp);
431 /* We know we have at least one parameter */
434 case 'n': /* serial number */
435 memset(e.sn, 0, sizeof(e.sn));
436 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
439 case 'e': /* errata */
440 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
441 memset(e.errata, 0, 5);
442 strncpy((char *)e.errata, argv[2], 4);
444 e.errata[0] = argv[2][0];
445 e.errata[1] = argv[2][1];
449 case 'd': /* date BCD format YYMMDDhhmmss */
452 case 'p': /* MAC table size */
453 e.mac_count = simple_strtoul(argv[2], NULL, 16);
456 case '0' ... '9': /* "mac 0" through "mac 22" */
457 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
461 return cmd_usage(cmdtp);
468 * mac_read_from_eeprom - read the MAC addresses from EEPROM
470 * This function reads the MAC addresses from EEPROM and sets the
471 * appropriate environment variables for each one read.
473 * The environment variables are only set if they haven't been set already.
474 * This ensures that any user-saved variables are never overwritten.
476 * This function must be called after relocation.
478 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
479 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
480 * located at a different offset.
482 int mac_read_from_eeprom(void)
485 u32 crc, crc_offset = offsetof(struct eeprom, crc);
486 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
491 printf("Read failed.\n");
496 printf("Invalid ID (%02x %02x %02x %02x)\n",
497 e.id[0], e.id[1], e.id[2], e.id[3]);
501 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
503 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
504 * to where it is in v0.
510 crc = crc32(0, (void *)&e, crc_offset);
511 crcp = (void *)&e + crc_offset;
512 if (crc != be32_to_cpu(*crcp)) {
513 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
517 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
519 * MAC address #9 in v1 occupies the same position as the CRC in v0.
520 * Erase it so that it's not mistaken for a MAC address. We'll
521 * update the CRC later.
524 memset(e.mac[8], 0xff, 6);
527 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
528 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
529 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
533 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
540 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
541 /* Only initialize environment variables that are blank
542 * (i.e. have not yet been set)
544 if (!env_get(enetvar))
545 env_set(enetvar, ethaddr);
549 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
550 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
551 be32_to_cpu(e.version));
553 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
556 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
558 * Now we need to upconvert the data into v1 format. We do this last so
559 * that at boot time, U-Boot will still say "NXID v0".
561 if (e.version == 0) {
562 e.version = cpu_to_be32(NXID_VERSION);
570 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
573 * get_cpu_board_revision - get the CPU board revision on 85xx boards
575 * Read the EEPROM to determine the board revision.
577 * This function is called before relocation, so we need to read a private
578 * copy of the EEPROM into a local variable on the stack.
580 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
581 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
582 * so that the SPD code will work. This means that all pre-relocation I2C
583 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
584 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
585 * this function is called. Oh well.
587 unsigned int get_cpu_board_revision(void)
589 struct board_eeprom {
590 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
591 u8 major; /* 0x04 Board revision, major */
592 u8 minor; /* 0x05 Board revision, minor */
595 #ifndef CONFIG_DM_I2C
596 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
597 (void *)&be, sizeof(be));
601 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
602 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
603 CONFIG_SYS_I2C_EEPROM_ADDR,
604 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
607 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
608 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
612 dm_i2c_read(dev, 0, (void *)&be, sizeof(be));
615 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
616 return MPC85XX_CPU_BOARD_REV(0, 0);
618 if ((be.major == 0xff) && (be.minor == 0xff))
619 return MPC85XX_CPU_BOARD_REV(0, 0);
621 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);