2 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
3 * York Sun (yorksun@freescale.com)
4 * Haiying Wang (haiying.wang@freescale.com)
5 * Timur Tabi (timur@freescale.com)
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/ctype.h>
15 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
16 #include "../common/eeprom.h"
17 #define MAX_NUM_PORTS 8
20 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
21 /* some boards with non-256-bytes EEPROM have special define */
22 /* for MAX_NUM_PORTS in board-specific file */
24 #define MAX_NUM_PORTS 23
26 #define NXID_VERSION 1
30 * static eeprom: EEPROM layout for CCID or NXID formats
32 * See application note AN3638 for details.
34 static struct __attribute__ ((__packed__
)) eeprom
{
35 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
36 u8 id
[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
37 u8 major
; /* 0x04 Board revision, major */
38 u8 minor
; /* 0x05 Board revision, minor */
39 u8 sn
[10]; /* 0x06 - 0x0F Serial Number*/
40 u8 errata
[2]; /* 0x10 - 0x11 Errata Level */
41 u8 date
[6]; /* 0x12 - 0x17 Build Date */
42 u8 res_0
[40]; /* 0x18 - 0x3f Reserved */
43 u8 mac_count
; /* 0x40 Number of MAC addresses */
44 u8 mac_flag
; /* 0x41 MAC table flags */
45 u8 mac
[MAX_NUM_PORTS
][6]; /* 0x42 - 0x71 MAC addresses */
46 u32 crc
; /* 0x72 CRC32 checksum */
48 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
49 u8 id
[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
50 u8 sn
[12]; /* 0x04 - 0x0F Serial Number */
51 u8 errata
[5]; /* 0x10 - 0x14 Errata Level */
52 u8 date
[6]; /* 0x15 - 0x1a Build Date */
53 u8 res_0
; /* 0x1b Reserved */
54 u32 version
; /* 0x1c - 0x1f NXID Version */
55 u8 tempcal
[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
56 u8 tempcalsys
[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
57 u8 tempcalflags
; /* 0x2a Temperature Calibration Flags */
58 u8 res_1
[21]; /* 0x2b - 0x3f Reserved */
59 u8 mac_count
; /* 0x40 Number of MAC addresses */
60 u8 mac_flag
; /* 0x41 MAC table flags */
61 u8 mac
[MAX_NUM_PORTS
][6]; /* 0x42 - x MAC addresses */
62 u32 crc
; /* x+1 CRC32 checksum */
66 /* Set to 1 if we've read EEPROM into memory */
67 static int has_been_read
= 0;
69 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
70 /* Is this a valid NXID EEPROM? */
71 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
72 (e.id[2] == 'I') || (e.id[3] == 'D'))
75 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
76 /* Is this a valid CCID EEPROM? */
77 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
78 (e.id[2] == 'I') || (e.id[3] == 'D'))
82 * show_eeprom - display the contents of the EEPROM
84 static void show_eeprom(void)
89 /* EEPROM tag ID, either CCID or NXID */
90 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
91 printf("ID: %c%c%c%c v%u\n", e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3],
92 be32_to_cpu(e
.version
));
94 printf("ID: %c%c%c%c\n", e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3]);
98 printf("SN: %s\n", e
.sn
);
101 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
102 printf("Errata: %s\n", e
.errata
);
104 printf("Errata: %c%c\n",
105 e
.errata
[0] ? e
.errata
[0] : '.',
106 e
.errata
[1] ? e
.errata
[1] : '.');
109 /* Build date, BCD date values, as YYMMDDhhmmss */
110 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
111 e
.date
[0], e
.date
[1], e
.date
[2],
112 e
.date
[3] & 0x7F, e
.date
[4], e
.date
[5],
113 e
.date
[3] & 0x80 ? "PM" : "");
115 /* Show MAC addresses */
116 for (i
= 0; i
< min(e
.mac_count
, MAX_NUM_PORTS
); i
++) {
120 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i
,
121 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5]);
124 crc
= crc32(0, (void *)&e
, sizeof(e
) - 4);
126 if (crc
== be32_to_cpu(e
.crc
))
127 printf("CRC: %08x\n", be32_to_cpu(e
.crc
));
129 printf("CRC: %08x (should be %08x)\n",
130 be32_to_cpu(e
.crc
), crc
);
133 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e
));
134 for (i
= 0; i
< sizeof(e
); i
++) {
137 printf("%02X ", ((u8
*)&e
)[i
]);
138 if (((i
% 16) == 15) || (i
== sizeof(e
) - 1))
145 * read_eeprom - read the EEPROM into memory
147 static int read_eeprom(void)
150 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
157 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
158 bus
= i2c_get_bus_num();
159 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM
);
162 ret
= i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR
, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
163 (void *)&e
, sizeof(e
));
165 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
166 i2c_set_bus_num(bus
);
173 has_been_read
= (ret
== 0) ? 1 : 0;
179 * update_crc - update the CRC
181 * This function should be called after each update to the EEPROM structure,
182 * to make sure the CRC is always correct.
184 static void update_crc(void)
188 crc
= crc32(0, (void *)&e
, sizeof(e
) - 4);
189 e
.crc
= cpu_to_be32(crc
);
193 * prog_eeprom - write the EEPROM from memory
195 static int prog_eeprom(void)
200 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
204 /* Set the reserved values to 0xFF */
205 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
207 memset(e
.res_1
, 0xFF, sizeof(e
.res_1
));
209 memset(e
.res_0
, 0xFF, sizeof(e
.res_0
));
213 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
214 bus
= i2c_get_bus_num();
215 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM
);
219 * The AT24C02 datasheet says that data can only be written in page
220 * mode, which means 8 bytes at a time, and it takes up to 5ms to
221 * complete a given write.
223 for (i
= 0, p
= &e
; i
< sizeof(e
); i
+= 8, p
+= 8) {
224 ret
= i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR
, i
, CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
225 p
, min((sizeof(e
) - i
), 8));
228 udelay(5000); /* 5ms write cycle timing */
232 /* Verify the write by reading back the EEPROM and comparing */
235 ret
= i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR
, 0,
236 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
, (void *)&e2
, sizeof(e2
));
237 if (!ret
&& memcmp(&e
, &e2
, sizeof(e
)))
241 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
242 i2c_set_bus_num(bus
);
246 printf("Programming failed.\n");
251 printf("Programming passed.\n");
256 * h2i - converts hex character into a number
258 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
259 * the integer equivalent.
261 static inline u8
h2i(char p
)
263 if ((p
>= '0') && (p
<= '9'))
266 if ((p
>= 'A') && (p
<= 'F'))
267 return (p
- 'A') + 10;
269 if ((p
>= 'a') && (p
<= 'f'))
270 return (p
- 'a') + 10;
276 * set_date - stores the build date into the EEPROM
278 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
279 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
280 * and stores it in the build date field of the EEPROM local copy.
282 static void set_date(const char *string
)
286 if (strlen(string
) != 12) {
287 printf("Usage: mac date YYMMDDhhmmss\n");
291 for (i
= 0; i
< 6; i
++)
292 e
.date
[i
] = h2i(string
[2 * i
]) << 4 | h2i(string
[2 * i
+ 1]);
298 * set_mac_address - stores a MAC address into the EEPROM
300 * This function takes a pointer to MAC address string
301 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
302 * stores it in one of the MAC address fields of the EEPROM local copy.
304 static void set_mac_address(unsigned int index
, const char *string
)
306 char *p
= (char *) string
;
309 if ((index
>= MAX_NUM_PORTS
) || !string
) {
310 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
314 for (i
= 0; *p
&& (i
< 6); i
++) {
315 e
.mac
[index
][i
] = simple_strtoul(p
, &p
, 16);
323 int do_mac(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
340 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
341 memcpy(e
.id
, "NXID", sizeof(e
.id
));
342 e
.version
= NXID_VERSION
;
344 memcpy(e
.id
, "CCID", sizeof(e
.id
));
351 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
361 return cmd_usage(cmdtp
);
367 /* We know we have at least one parameter */
370 case 'n': /* serial number */
371 memset(e
.sn
, 0, sizeof(e
.sn
));
372 strncpy((char *)e
.sn
, argv
[2], sizeof(e
.sn
) - 1);
375 case 'e': /* errata */
376 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
377 memset(e
.errata
, 0, 5);
378 strncpy((char *)e
.errata
, argv
[2], 4);
380 e
.errata
[0] = argv
[2][0];
381 e
.errata
[1] = argv
[2][1];
385 case 'd': /* date BCD format YYMMDDhhmmss */
388 case 'p': /* MAC table size */
389 e
.mac_count
= simple_strtoul(argv
[2], NULL
, 16);
392 case '0' ... '9': /* "mac 0" through "mac 22" */
393 set_mac_address(simple_strtoul(argv
[1], NULL
, 10), argv
[2]);
397 return cmd_usage(cmdtp
);
404 * mac_read_from_eeprom - read the MAC addresses from EEPROM
406 * This function reads the MAC addresses from EEPROM and sets the
407 * appropriate environment variables for each one read.
409 * The environment variables are only set if they haven't been set already.
410 * This ensures that any user-saved variables are never overwritten.
412 * This function must be called after relocation.
414 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
415 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
416 * located at a different offset.
418 int mac_read_from_eeprom(void)
421 u32 crc
, crc_offset
= offsetof(struct eeprom
, crc
);
422 u32
*crcp
; /* Pointer to the CRC in the data read from the EEPROM */
427 printf("Read failed.\n");
432 printf("Invalid ID (%02x %02x %02x %02x)\n",
433 e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3]);
437 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
439 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
440 * to where it is in v0.
446 crc
= crc32(0, (void *)&e
, crc_offset
);
447 crcp
= (void *)&e
+ crc_offset
;
448 if (crc
!= be32_to_cpu(*crcp
)) {
449 printf("CRC mismatch (%08x != %08x)\n", crc
, be32_to_cpu(e
.crc
));
453 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
455 * MAC address #9 in v1 occupies the same position as the CRC in v0.
456 * Erase it so that it's not mistaken for a MAC address. We'll
457 * update the CRC later.
460 memset(e
.mac
[8], 0xff, 6);
463 for (i
= 0; i
< min(e
.mac_count
, MAX_NUM_PORTS
); i
++) {
464 if (memcmp(&e
.mac
[i
], "\0\0\0\0\0\0", 6) &&
465 memcmp(&e
.mac
[i
], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
469 sprintf(ethaddr
, "%02X:%02X:%02X:%02X:%02X:%02X",
476 sprintf(enetvar
, i
? "eth%daddr" : "ethaddr", i
);
477 /* Only initialize environment variables that are blank
478 * (i.e. have not yet been set)
480 if (!getenv(enetvar
))
481 setenv(enetvar
, ethaddr
);
485 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
486 printf("%c%c%c%c v%u\n", e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3],
487 be32_to_cpu(e
.version
));
489 printf("%c%c%c%c\n", e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3]);
492 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
494 * Now we need to upconvert the data into v1 format. We do this last so
495 * that at boot time, U-Boot will still say "NXID v0".
497 if (e
.version
== 0) {
498 e
.version
= NXID_VERSION
;
506 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
509 * get_cpu_board_revision - get the CPU board revision on 85xx boards
511 * Read the EEPROM to determine the board revision.
513 * This function is called before relocation, so we need to read a private
514 * copy of the EEPROM into a local variable on the stack.
516 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
517 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
518 * so that the SPD code will work. This means that all pre-relocation I2C
519 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
520 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
521 * this function is called. Oh well.
523 unsigned int get_cpu_board_revision(void)
525 struct board_eeprom
{
526 u32 id
; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
527 u8 major
; /* 0x04 Board revision, major */
528 u8 minor
; /* 0x05 Board revision, minor */
531 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR
, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
532 (void *)&be
, sizeof(be
));
534 if (be
.id
!= (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
535 return MPC85XX_CPU_BOARD_REV(0, 0);
537 if ((be
.major
== 0xff) && (be
.minor
== 0xff))
538 return MPC85XX_CPU_BOARD_REV(0, 0);
540 return MPC85XX_CPU_BOARD_REV(be
.major
, be
.minor
);