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.
17 #include <linux/ctype.h>
18 #include <u-boot/crc.h>
22 #ifdef CONFIG_SYS_I2C_EEPROM_NXID_MAC
23 #define MAX_NUM_PORTS CONFIG_SYS_I2C_EEPROM_NXID_MAC
25 #define MAX_NUM_PORTS 8
27 #define NXID_VERSION 0
30 * static eeprom: EEPROM layout for NXID formats
32 * See Freescale application note AN3638 for details.
34 static struct __attribute__ ((__packed__)) eeprom {
35 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
36 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
37 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
38 u8 date[6]; /* 0x15 - 0x1a Build Date */
39 u8 res_0; /* 0x1b Reserved */
40 u32 version; /* 0x1c - 0x1f NXID Version */
41 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
42 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
43 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
44 u8 res_1[21]; /* 0x2b - 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 - x MAC addresses */
48 u32 crc; /* x+1 CRC32 checksum */
51 /* Set to 1 if we've read EEPROM into memory */
52 static int has_been_read;
54 /* Is this a valid NXID EEPROM? */
55 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
56 (e.id[2] == 'I') || (e.id[3] == 'D'))
58 /** Fixed ID field in EEPROM */
59 static unsigned char uid[16];
61 static int eeprom_bus_num = -1;
62 static int eeprom_addr;
63 static int eeprom_addr_len;
66 * This must be called before any eeprom access.
68 void init_eeprom(int bus_num, int addr, int addr_len)
70 eeprom_bus_num = bus_num;
72 eeprom_addr_len = addr_len;
76 * show_eeprom - display the contents of the EEPROM
78 void show_eeprom(void)
83 /* EEPROM tag ID, either CCID or NXID */
84 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
85 be32_to_cpu(e.version));
88 printf("SN: %s\n", e.sn);
91 for (i = 0; i < 16; i++)
92 printf("%02x", uid[i]);
96 printf("Errata: %s\n", e.errata);
98 /* Build date, BCD date values, as YYMMDDhhmmss */
99 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
100 e.date[0], e.date[1], e.date[2],
101 e.date[3] & 0x7F, e.date[4], e.date[5],
102 e.date[3] & 0x80 ? "PM" : "");
104 /* Show MAC addresses */
105 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
108 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
109 p[0], p[1], p[2], p[3], p[4], p[5]);
112 crc = crc32(0, (void *)&e, sizeof(e) - 4);
114 if (crc == be32_to_cpu(e.crc))
115 printf("CRC: %08x\n", be32_to_cpu(e.crc));
117 printf("CRC: %08x (should be %08x)\n",
118 be32_to_cpu(e.crc), crc);
121 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
122 for (i = 0; i < sizeof(e); i++) {
125 printf("%02X ", ((u8 *)&e)[i]);
126 if (((i % 16) == 15) || (i == sizeof(e) - 1))
133 * read_eeprom - read the EEPROM into memory
135 int read_eeprom(void)
140 if (eeprom_bus_num < 0) {
141 printf("EEPROM not configured\n");
148 bus = i2c_get_bus_num();
149 i2c_set_bus_num(eeprom_bus_num);
151 ret = i2c_read(eeprom_addr, 0, eeprom_addr_len,
152 (void *)&e, sizeof(e));
155 /* Fixed address of ID field */
156 i2c_read(0x5f, 0x80, 1, uid, 16);
158 i2c_set_bus_num(bus);
164 has_been_read = (ret == 0) ? 1 : 0;
170 * update_crc - update the CRC
172 * This function should be called after each update to the EEPROM structure,
173 * to make sure the CRC is always correct.
175 static void update_crc(void)
177 u32 crc, crc_offset = offsetof(struct eeprom, crc);
179 crc = crc32(0, (void *)&e, crc_offset);
180 e.crc = cpu_to_be32(crc);
184 * prog_eeprom - write the EEPROM from memory
186 static int prog_eeprom(void)
193 if (eeprom_bus_num < 0) {
194 printf("EEPROM not configured\n");
198 /* Set the reserved values to 0xFF */
200 memset(e.res_1, 0xFF, sizeof(e.res_1));
203 bus = i2c_get_bus_num();
204 i2c_set_bus_num(eeprom_bus_num);
207 * The AT24C02 datasheet says that data can only be written in page
208 * mode, which means 8 bytes at a time, and it takes up to 5ms to
209 * complete a given write.
211 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
212 ret = i2c_write(eeprom_addr, i, eeprom_addr_len,
213 p, min((int)(sizeof(e) - i), 8));
216 udelay(5000); /* 5ms write cycle timing */
220 /* Verify the write by reading back the EEPROM and comparing */
223 ret = i2c_read(eeprom_addr, 0,
224 eeprom_addr_len, (void *)&e2, sizeof(e2));
225 if (!ret && memcmp(&e, &e2, sizeof(e)))
229 i2c_set_bus_num(bus);
232 printf("Programming failed.\n");
237 printf("Programming passed.\n");
242 * h2i - converts hex character into a number
244 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
245 * the integer equivalent.
247 static inline u8 h2i(char p)
249 if ((p >= '0') && (p <= '9'))
252 if ((p >= 'A') && (p <= 'F'))
253 return (p - 'A') + 10;
255 if ((p >= 'a') && (p <= 'f'))
256 return (p - 'a') + 10;
262 * set_date - stores the build date into the EEPROM
264 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
265 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
266 * and stores it in the build date field of the EEPROM local copy.
268 static void set_date(const char *string)
272 if (strlen(string) != 12) {
273 printf("Usage: mac date YYMMDDhhmmss\n");
277 for (i = 0; i < 6; i++)
278 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
284 * set_mac_address - stores a MAC address into the EEPROM
286 * This function takes a pointer to MAC address string
287 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
288 * stores it in one of the MAC address fields of the EEPROM local copy.
290 static void set_mac_address(unsigned int index, const char *string)
292 char *p = (char *)string;
295 if ((index >= MAX_NUM_PORTS) || !string) {
296 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
300 for (i = 0; *p && (i < 6); i++) {
301 e.mac[index][i] = simple_strtoul(p, &p, 16);
309 int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
326 memcpy(e.id, "NXID", sizeof(e.id));
327 e.version = NXID_VERSION;
333 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
343 return cmd_usage(cmdtp);
349 /* We know we have at least one parameter */
352 case 'n': /* serial number */
353 memset(e.sn, 0, sizeof(e.sn));
354 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
357 case 'e': /* errata */
358 memset(e.errata, 0, 5);
359 strncpy((char *)e.errata, argv[2], 4);
362 case 'd': /* date BCD format YYMMDDhhmmss */
365 case 'p': /* MAC table size */
366 e.mac_count = simple_strtoul(argv[2], NULL, 16);
369 case '0' ... '9': /* "mac 0" through "mac 22" */
370 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
374 return cmd_usage(cmdtp);
380 int mac_read_from_generic_eeprom(const char *envvar, int chip,
381 int address, int mac_bus)
385 unsigned char mac[6];
388 bus = i2c_get_bus_num();
389 i2c_set_bus_num(mac_bus);
391 ret = i2c_read(chip, address, 1, mac, 6);
393 i2c_set_bus_num(bus);
396 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
404 printf("MAC: %s\n", ethaddr);
405 env_set(envvar, ethaddr);
411 void mac_read_from_fixed_id(void)
413 #ifdef CONFIG_SYS_I2C_MAC1_CHIP_ADDR
414 mac_read_from_generic_eeprom("ethaddr", CONFIG_SYS_I2C_MAC1_CHIP_ADDR,
415 CONFIG_SYS_I2C_MAC1_DATA_ADDR, CONFIG_SYS_I2C_MAC1_BUS);
417 #ifdef CONFIG_SYS_I2C_MAC2_CHIP_ADDR
418 mac_read_from_generic_eeprom("eth1addr", CONFIG_SYS_I2C_MAC2_CHIP_ADDR,
419 CONFIG_SYS_I2C_MAC2_DATA_ADDR, CONFIG_SYS_I2C_MAC2_BUS);
424 * mac_read_from_eeprom - read the MAC addresses from EEPROM
426 * This function reads the MAC addresses from EEPROM and sets the
427 * appropriate environment variables for each one read.
429 * The environment variables are only set if they haven't been set already.
430 * This ensures that any user-saved variables are never overwritten.
432 * This function must be called after relocation.
434 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
435 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
436 * located at a different offset.
438 int mac_read_from_eeprom_common(void)
441 u32 crc, crc_offset = offsetof(struct eeprom, crc);
442 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
447 printf("Read failed.\n");
452 printf("Invalid ID (%02x %02x %02x %02x)\n",
453 e.id[0], e.id[1], e.id[2], e.id[3]);
457 crc = crc32(0, (void *)&e, crc_offset);
458 crcp = (void *)&e + crc_offset;
459 if (crc != be32_to_cpu(*crcp)) {
460 printf("CRC mismatch (%08x != %08x)\n", crc,
466 * MAC address #9 in v1 occupies the same position as the CRC in v0.
467 * Erase it so that it's not mistaken for a MAC address. We'll
468 * update the CRC later.
471 memset(e.mac[8], 0xff, 6);
473 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
474 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
475 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
479 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
486 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
487 /* Only initialize environment variables that are blank
488 * (i.e. have not yet been set)
490 if (!env_get(enetvar))
491 env_set(enetvar, ethaddr);
495 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
496 be32_to_cpu(e.version));