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>
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 16
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 - 0xa1 MAC addresses */
62 u8 res_2
[90]; /* 0xa2 - 0xfb Reserved */
63 u32 crc
; /* 0xfc - 0xff CRC32 checksum */
67 /* Set to 1 if we've read EEPROM into memory */
68 static int has_been_read
= 0;
70 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
71 /* Is this a valid NXID EEPROM? */
72 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
73 (e.id[2] == 'I') || (e.id[3] == 'D'))
76 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
77 /* Is this a valid CCID EEPROM? */
78 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
79 (e.id[2] == 'I') || (e.id[3] == 'D'))
83 * show_eeprom - display the contents of the EEPROM
85 static void show_eeprom(void)
90 /* EEPROM tag ID, either CCID or NXID */
91 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
92 printf("ID: %c%c%c%c v%u\n", e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3],
93 be32_to_cpu(e
.version
));
95 printf("ID: %c%c%c%c\n", e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3]);
99 printf("SN: %s\n", e
.sn
);
102 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
103 printf("Errata: %s\n", e
.errata
);
105 printf("Errata: %c%c\n",
106 e
.errata
[0] ? e
.errata
[0] : '.',
107 e
.errata
[1] ? e
.errata
[1] : '.');
110 /* Build date, BCD date values, as YYMMDDhhmmss */
111 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
112 e
.date
[0], e
.date
[1], e
.date
[2],
113 e
.date
[3] & 0x7F, e
.date
[4], e
.date
[5],
114 e
.date
[3] & 0x80 ? "PM" : "");
116 /* Show MAC addresses */
117 for (i
= 0; i
< min(e
.mac_count
, (u8
)MAX_NUM_PORTS
); i
++) {
121 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i
,
122 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5]);
125 crc
= crc32(0, (void *)&e
, sizeof(e
) - 4);
127 if (crc
== be32_to_cpu(e
.crc
))
128 printf("CRC: %08x\n", be32_to_cpu(e
.crc
));
130 printf("CRC: %08x (should be %08x)\n",
131 be32_to_cpu(e
.crc
), crc
);
134 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e
));
135 for (i
= 0; i
< sizeof(e
); i
++) {
138 printf("%02X ", ((u8
*)&e
)[i
]);
139 if (((i
% 16) == 15) || (i
== sizeof(e
) - 1))
146 * read_eeprom - read the EEPROM into memory
148 static int read_eeprom(void)
151 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
152 #ifndef CONFIG_DM_I2C
160 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
161 #ifndef CONFIG_DM_I2C
162 bus
= i2c_get_bus_num();
163 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM
);
167 #ifndef CONFIG_DM_I2C
168 ret
= i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR
, 0,
169 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
170 (void *)&e
, sizeof(e
));
173 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
174 ret
= i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM
,
175 CONFIG_SYS_I2C_EEPROM_ADDR
, 1, &dev
);
177 ret
= i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR
, 1, &dev
);
180 ret
= dm_i2c_read(dev
, 0, (void *)&e
, sizeof(e
));
183 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
184 #ifndef CONFIG_DM_I2C
185 i2c_set_bus_num(bus
);
193 has_been_read
= (ret
== 0) ? 1 : 0;
199 * update_crc - update the CRC
201 * This function should be called after each update to the EEPROM structure,
202 * to make sure the CRC is always correct.
204 static void update_crc(void)
208 crc
= crc32(0, (void *)&e
, sizeof(e
) - 4);
209 e
.crc
= cpu_to_be32(crc
);
213 * prog_eeprom - write the EEPROM from memory
215 static int prog_eeprom(void)
220 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
221 #ifndef CONFIG_DM_I2C
226 /* Set the reserved values to 0xFF */
227 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
229 memset(e
.res_1
, 0xFF, sizeof(e
.res_1
));
231 memset(e
.res_0
, 0xFF, sizeof(e
.res_0
));
235 #ifndef CONFIG_DM_I2C
236 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
237 bus
= i2c_get_bus_num();
238 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM
);
243 * The AT24C02 datasheet says that data can only be written in page
244 * mode, which means 8 bytes at a time, and it takes up to 5ms to
245 * complete a given write.
247 for (i
= 0, p
= &e
; i
< sizeof(e
); i
+= 8, p
+= 8) {
248 #ifndef CONFIG_DM_I2C
249 ret
= i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR
, i
,
250 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
251 p
, min((int)(sizeof(e
) - i
), 8));
254 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
255 ret
= i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM
,
256 CONFIG_SYS_I2C_EEPROM_ADDR
,
257 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
260 ret
= i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR
,
261 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
265 ret
= dm_i2c_write(dev
, i
, p
, min((int)(sizeof(e
) - i
),
270 udelay(5000); /* 5ms write cycle timing */
274 /* Verify the write by reading back the EEPROM and comparing */
277 #ifndef CONFIG_DM_I2C
278 ret
= i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR
, 0,
279 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
280 (void *)&e2
, sizeof(e2
));
283 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
284 ret
= i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM
,
285 CONFIG_SYS_I2C_EEPROM_ADDR
,
286 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
289 ret
= i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR
,
290 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
294 ret
= dm_i2c_read(dev
, 0, (void *)&e2
, sizeof(e2
));
296 if (!ret
&& memcmp(&e
, &e2
, sizeof(e
)))
300 #ifndef CONFIG_DM_I2C
301 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
302 i2c_set_bus_num(bus
);
307 printf("Programming failed.\n");
312 printf("Programming passed.\n");
317 * h2i - converts hex character into a number
319 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
320 * the integer equivalent.
322 static inline u8
h2i(char p
)
324 if ((p
>= '0') && (p
<= '9'))
327 if ((p
>= 'A') && (p
<= 'F'))
328 return (p
- 'A') + 10;
330 if ((p
>= 'a') && (p
<= 'f'))
331 return (p
- 'a') + 10;
337 * set_date - stores the build date into the EEPROM
339 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
340 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
341 * and stores it in the build date field of the EEPROM local copy.
343 static void set_date(const char *string
)
347 if (strlen(string
) != 12) {
348 printf("Usage: mac date YYMMDDhhmmss\n");
352 for (i
= 0; i
< 6; i
++)
353 e
.date
[i
] = h2i(string
[2 * i
]) << 4 | h2i(string
[2 * i
+ 1]);
359 * set_mac_address - stores a MAC address into the EEPROM
361 * This function takes a pointer to MAC address string
362 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
363 * stores it in one of the MAC address fields of the EEPROM local copy.
365 static void set_mac_address(unsigned int index
, const char *string
)
367 char *p
= (char *) string
;
370 if ((index
>= MAX_NUM_PORTS
) || !string
) {
371 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
375 for (i
= 0; *p
&& (i
< 6); i
++) {
376 e
.mac
[index
][i
] = simple_strtoul(p
, &p
, 16);
384 int do_mac(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
401 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
402 memcpy(e
.id
, "NXID", sizeof(e
.id
));
403 e
.version
= cpu_to_be32(NXID_VERSION
);
405 memcpy(e
.id
, "CCID", sizeof(e
.id
));
412 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
422 return cmd_usage(cmdtp
);
428 /* We know we have at least one parameter */
431 case 'n': /* serial number */
432 memset(e
.sn
, 0, sizeof(e
.sn
));
433 strncpy((char *)e
.sn
, argv
[2], sizeof(e
.sn
) - 1);
436 case 'e': /* errata */
437 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
438 memset(e
.errata
, 0, 5);
439 strncpy((char *)e
.errata
, argv
[2], 4);
441 e
.errata
[0] = argv
[2][0];
442 e
.errata
[1] = argv
[2][1];
446 case 'd': /* date BCD format YYMMDDhhmmss */
449 case 'p': /* MAC table size */
450 e
.mac_count
= simple_strtoul(argv
[2], NULL
, 16);
453 case '0' ... '9': /* "mac 0" through "mac 22" */
454 set_mac_address(simple_strtoul(argv
[1], NULL
, 10), argv
[2]);
458 return cmd_usage(cmdtp
);
465 * mac_read_from_eeprom - read the MAC addresses from EEPROM
467 * This function reads the MAC addresses from EEPROM and sets the
468 * appropriate environment variables for each one read.
470 * The environment variables are only set if they haven't been set already.
471 * This ensures that any user-saved variables are never overwritten.
473 * This function must be called after relocation.
475 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
476 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
477 * located at a different offset.
479 int mac_read_from_eeprom(void)
482 u32 crc
, crc_offset
= offsetof(struct eeprom
, crc
);
483 u32
*crcp
; /* Pointer to the CRC in the data read from the EEPROM */
488 printf("Read failed.\n");
493 printf("Invalid ID (%02x %02x %02x %02x)\n",
494 e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3]);
498 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
500 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
501 * to where it is in v0.
507 crc
= crc32(0, (void *)&e
, crc_offset
);
508 crcp
= (void *)&e
+ crc_offset
;
509 if (crc
!= be32_to_cpu(*crcp
)) {
510 printf("CRC mismatch (%08x != %08x)\n", crc
, be32_to_cpu(e
.crc
));
514 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
516 * MAC address #9 in v1 occupies the same position as the CRC in v0.
517 * Erase it so that it's not mistaken for a MAC address. We'll
518 * update the CRC later.
521 memset(e
.mac
[8], 0xff, 6);
524 for (i
= 0; i
< min(e
.mac_count
, (u8
)MAX_NUM_PORTS
); i
++) {
525 if (memcmp(&e
.mac
[i
], "\0\0\0\0\0\0", 6) &&
526 memcmp(&e
.mac
[i
], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
530 sprintf(ethaddr
, "%02X:%02X:%02X:%02X:%02X:%02X",
537 sprintf(enetvar
, i
? "eth%daddr" : "ethaddr", i
);
538 /* Only initialize environment variables that are blank
539 * (i.e. have not yet been set)
541 if (!env_get(enetvar
))
542 env_set(enetvar
, ethaddr
);
546 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
547 printf("%c%c%c%c v%u\n", e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3],
548 be32_to_cpu(e
.version
));
550 printf("%c%c%c%c\n", e
.id
[0], e
.id
[1], e
.id
[2], e
.id
[3]);
553 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
555 * Now we need to upconvert the data into v1 format. We do this last so
556 * that at boot time, U-Boot will still say "NXID v0".
558 if (e
.version
== 0) {
559 e
.version
= cpu_to_be32(NXID_VERSION
);
567 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
570 * get_cpu_board_revision - get the CPU board revision on 85xx boards
572 * Read the EEPROM to determine the board revision.
574 * This function is called before relocation, so we need to read a private
575 * copy of the EEPROM into a local variable on the stack.
577 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
578 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
579 * so that the SPD code will work. This means that all pre-relocation I2C
580 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
581 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
582 * this function is called. Oh well.
584 unsigned int get_cpu_board_revision(void)
586 struct board_eeprom
{
587 u32 id
; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
588 u8 major
; /* 0x04 Board revision, major */
589 u8 minor
; /* 0x05 Board revision, minor */
592 #ifndef CONFIG_DM_I2C
593 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR
, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
594 (void *)&be
, sizeof(be
));
597 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
598 ret
= i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM
,
599 CONFIG_SYS_I2C_EEPROM_ADDR
,
600 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
603 ret
= i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR
,
604 CONFIG_SYS_I2C_EEPROM_ADDR_LEN
,
608 dm_i2c_read(dev
, 0, (void *)&be
, sizeof(be
));
611 if (be
.id
!= (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
612 return MPC85XX_CPU_BOARD_REV(0, 0);
614 if ((be
.major
== 0xff) && (be
.minor
== 0xff))
615 return MPC85XX_CPU_BOARD_REV(0, 0);
617 return MPC85XX_CPU_BOARD_REV(be
.major
, be
.minor
);