1 // SPDX-License-Identifier: GPL-2.0+
3 * Based on board/freescale/common/sys_eeprom.c
4 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
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.
16 #include <linux/ctype.h>
20 #ifdef CONFIG_SYS_I2C_EEPROM_NXID_MAC
21 #define MAX_NUM_PORTS CONFIG_SYS_I2C_EEPROM_NXID_MAC
23 #define MAX_NUM_PORTS 8
25 #define NXID_VERSION 0
28 * static eeprom: EEPROM layout for NXID formats
30 * See Freescale application note AN3638 for details.
32 static struct __attribute__ ((__packed__)) eeprom {
33 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
34 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
35 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
36 u8 date[6]; /* 0x15 - 0x1a Build Date */
37 u8 res_0; /* 0x1b Reserved */
38 u32 version; /* 0x1c - 0x1f NXID Version */
39 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
40 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
41 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
42 u8 res_1[21]; /* 0x2b - 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 - x MAC addresses */
46 u32 crc; /* x+1 CRC32 checksum */
49 /* Set to 1 if we've read EEPROM into memory */
50 static int has_been_read;
52 /* Is this a valid NXID EEPROM? */
53 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
54 (e.id[2] == 'I') || (e.id[3] == 'D'))
56 /** Fixed ID field in EEPROM */
57 static unsigned char uid[16];
59 static int eeprom_bus_num = -1;
60 static int eeprom_addr;
61 static int eeprom_addr_len;
64 * This must be called before any eeprom access.
66 void init_eeprom(int bus_num, int addr, int addr_len)
68 eeprom_bus_num = bus_num;
70 eeprom_addr_len = addr_len;
74 * show_eeprom - display the contents of the EEPROM
76 void show_eeprom(void)
81 /* EEPROM tag ID, either CCID or NXID */
82 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
83 be32_to_cpu(e.version));
86 printf("SN: %s\n", e.sn);
89 for (i = 0; i < 16; i++)
90 printf("%02x", uid[i]);
94 printf("Errata: %s\n", e.errata);
96 /* Build date, BCD date values, as YYMMDDhhmmss */
97 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
98 e.date[0], e.date[1], e.date[2],
99 e.date[3] & 0x7F, e.date[4], e.date[5],
100 e.date[3] & 0x80 ? "PM" : "");
102 /* Show MAC addresses */
103 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
106 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
107 p[0], p[1], p[2], p[3], p[4], p[5]);
110 crc = crc32(0, (void *)&e, sizeof(e) - 4);
112 if (crc == be32_to_cpu(e.crc))
113 printf("CRC: %08x\n", be32_to_cpu(e.crc));
115 printf("CRC: %08x (should be %08x)\n",
116 be32_to_cpu(e.crc), crc);
119 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
120 for (i = 0; i < sizeof(e); i++) {
123 printf("%02X ", ((u8 *)&e)[i]);
124 if (((i % 16) == 15) || (i == sizeof(e) - 1))
131 * read_eeprom - read the EEPROM into memory
133 int read_eeprom(void)
138 if (eeprom_bus_num < 0) {
139 printf("EEPROM not configured\n");
146 bus = i2c_get_bus_num();
147 i2c_set_bus_num(eeprom_bus_num);
149 ret = i2c_read(eeprom_addr, 0, eeprom_addr_len,
150 (void *)&e, sizeof(e));
153 /* Fixed address of ID field */
154 i2c_read(0x5f, 0x80, 1, uid, 16);
156 i2c_set_bus_num(bus);
162 has_been_read = (ret == 0) ? 1 : 0;
168 * update_crc - update the CRC
170 * This function should be called after each update to the EEPROM structure,
171 * to make sure the CRC is always correct.
173 static void update_crc(void)
175 u32 crc, crc_offset = offsetof(struct eeprom, crc);
177 crc = crc32(0, (void *)&e, crc_offset);
178 e.crc = cpu_to_be32(crc);
182 * prog_eeprom - write the EEPROM from memory
184 static int prog_eeprom(void)
191 if (eeprom_bus_num < 0) {
192 printf("EEPROM not configured\n");
196 /* Set the reserved values to 0xFF */
198 memset(e.res_1, 0xFF, sizeof(e.res_1));
201 bus = i2c_get_bus_num();
202 i2c_set_bus_num(eeprom_bus_num);
205 * The AT24C02 datasheet says that data can only be written in page
206 * mode, which means 8 bytes at a time, and it takes up to 5ms to
207 * complete a given write.
209 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
210 ret = i2c_write(eeprom_addr, i, eeprom_addr_len,
211 p, min((int)(sizeof(e) - i), 8));
214 udelay(5000); /* 5ms write cycle timing */
218 /* Verify the write by reading back the EEPROM and comparing */
221 ret = i2c_read(eeprom_addr, 0,
222 eeprom_addr_len, (void *)&e2, sizeof(e2));
223 if (!ret && memcmp(&e, &e2, sizeof(e)))
227 i2c_set_bus_num(bus);
230 printf("Programming failed.\n");
235 printf("Programming passed.\n");
240 * h2i - converts hex character into a number
242 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
243 * the integer equivalent.
245 static inline u8 h2i(char p)
247 if ((p >= '0') && (p <= '9'))
250 if ((p >= 'A') && (p <= 'F'))
251 return (p - 'A') + 10;
253 if ((p >= 'a') && (p <= 'f'))
254 return (p - 'a') + 10;
260 * set_date - stores the build date into the EEPROM
262 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
263 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
264 * and stores it in the build date field of the EEPROM local copy.
266 static void set_date(const char *string)
270 if (strlen(string) != 12) {
271 printf("Usage: mac date YYMMDDhhmmss\n");
275 for (i = 0; i < 6; i++)
276 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
282 * set_mac_address - stores a MAC address into the EEPROM
284 * This function takes a pointer to MAC address string
285 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
286 * stores it in one of the MAC address fields of the EEPROM local copy.
288 static void set_mac_address(unsigned int index, const char *string)
290 char *p = (char *)string;
293 if ((index >= MAX_NUM_PORTS) || !string) {
294 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
298 for (i = 0; *p && (i < 6); i++) {
299 e.mac[index][i] = simple_strtoul(p, &p, 16);
307 int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
324 memcpy(e.id, "NXID", sizeof(e.id));
325 e.version = NXID_VERSION;
331 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
341 return cmd_usage(cmdtp);
347 /* We know we have at least one parameter */
350 case 'n': /* serial number */
351 memset(e.sn, 0, sizeof(e.sn));
352 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
355 case 'e': /* errata */
356 memset(e.errata, 0, 5);
357 strncpy((char *)e.errata, argv[2], 4);
360 case 'd': /* date BCD format YYMMDDhhmmss */
363 case 'p': /* MAC table size */
364 e.mac_count = simple_strtoul(argv[2], NULL, 16);
367 case '0' ... '9': /* "mac 0" through "mac 22" */
368 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
372 return cmd_usage(cmdtp);
378 int mac_read_from_generic_eeprom(const char *envvar, int chip,
379 int address, int mac_bus)
383 unsigned char mac[6];
386 bus = i2c_get_bus_num();
387 i2c_set_bus_num(mac_bus);
389 ret = i2c_read(chip, address, 1, mac, 6);
391 i2c_set_bus_num(bus);
394 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
402 printf("MAC: %s\n", ethaddr);
403 env_set(envvar, ethaddr);
409 void mac_read_from_fixed_id(void)
411 #ifdef CONFIG_SYS_I2C_MAC1_CHIP_ADDR
412 mac_read_from_generic_eeprom("ethaddr", CONFIG_SYS_I2C_MAC1_CHIP_ADDR,
413 CONFIG_SYS_I2C_MAC1_DATA_ADDR, CONFIG_SYS_I2C_MAC1_BUS);
415 #ifdef CONFIG_SYS_I2C_MAC2_CHIP_ADDR
416 mac_read_from_generic_eeprom("eth1addr", CONFIG_SYS_I2C_MAC2_CHIP_ADDR,
417 CONFIG_SYS_I2C_MAC2_DATA_ADDR, CONFIG_SYS_I2C_MAC2_BUS);
422 * mac_read_from_eeprom - read the MAC addresses from EEPROM
424 * This function reads the MAC addresses from EEPROM and sets the
425 * appropriate environment variables for each one read.
427 * The environment variables are only set if they haven't been set already.
428 * This ensures that any user-saved variables are never overwritten.
430 * This function must be called after relocation.
432 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
433 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
434 * located at a different offset.
436 int mac_read_from_eeprom_common(void)
439 u32 crc, crc_offset = offsetof(struct eeprom, crc);
440 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
445 printf("Read failed.\n");
450 printf("Invalid ID (%02x %02x %02x %02x)\n",
451 e.id[0], e.id[1], e.id[2], e.id[3]);
455 crc = crc32(0, (void *)&e, crc_offset);
456 crcp = (void *)&e + crc_offset;
457 if (crc != be32_to_cpu(*crcp)) {
458 printf("CRC mismatch (%08x != %08x)\n", crc,
464 * MAC address #9 in v1 occupies the same position as the CRC in v0.
465 * Erase it so that it's not mistaken for a MAC address. We'll
466 * update the CRC later.
469 memset(e.mac[8], 0xff, 6);
471 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
472 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
473 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
477 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
484 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
485 /* Only initialize environment variables that are blank
486 * (i.e. have not yet been set)
488 if (!env_get(enetvar))
489 env_set(enetvar, ethaddr);
493 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
494 be32_to_cpu(e.version));