]> git.ipfire.org Git - thirdparty/u-boot.git/blame - board/freescale/common/sys_eeprom.c
Add GPL-2.0+ SPDX-License-Identifier to source files
[thirdparty/u-boot.git] / board / freescale / common / sys_eeprom.c
CommitLineData
bea3f28d 1/*
f098c9c8 2 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
bea3f28d
HW
3 * York Sun (yorksun@freescale.com)
4 * Haiying Wang (haiying.wang@freescale.com)
e2d31fb3 5 * Timur Tabi (timur@freescale.com)
bea3f28d 6 *
1a459660 7 * SPDX-License-Identifier: GPL-2.0+
bea3f28d
HW
8 */
9
10#include <common.h>
11#include <command.h>
12#include <i2c.h>
13#include <linux/ctype.h>
14
bfb70719 15#ifdef CONFIG_SYS_I2C_EEPROM_CCID
e2d31fb3 16#include "../common/eeprom.h"
bfb70719
TT
17#define MAX_NUM_PORTS 8
18#endif
e2d31fb3 19
bfb70719 20#ifdef CONFIG_SYS_I2C_EEPROM_NXID
bfb70719
TT
21#define MAX_NUM_PORTS 23
22#define NXID_VERSION 1
23#endif
9a611089 24
e2d31fb3
TT
25/**
26 * static eeprom: EEPROM layout for CCID or NXID formats
27 *
28 * See application note AN3638 for details.
29 */
30static struct __attribute__ ((__packed__)) eeprom {
6d0f6bcf 31#ifdef CONFIG_SYS_I2C_EEPROM_CCID
e2d31fb3
TT
32 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
33 u8 major; /* 0x04 Board revision, major */
34 u8 minor; /* 0x05 Board revision, minor */
35 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
36 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
37 u8 date[6]; /* 0x12 - 0x17 Build Date */
38 u8 res_0[40]; /* 0x18 - 0x3f Reserved */
39 u8 mac_count; /* 0x40 Number of MAC addresses */
40 u8 mac_flag; /* 0x41 MAC table flags */
9a611089 41 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
e2d31fb3
TT
42 u32 crc; /* 0x72 CRC32 checksum */
43#endif
6d0f6bcf 44#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
45 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
46 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
47 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
48 u8 date[6]; /* 0x15 - 0x1a Build Date */
49 u8 res_0; /* 0x1b Reserved */
50 u32 version; /* 0x1c - 0x1f NXID Version */
51 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
52 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
53 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
54 u8 res_1[21]; /* 0x2b - 0x3f Reserved */
55 u8 mac_count; /* 0x40 Number of MAC addresses */
56 u8 mac_flag; /* 0x41 MAC table flags */
bfb70719
TT
57 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */
58 u32 crc; /* x+1 CRC32 checksum */
e2d31fb3
TT
59#endif
60} e;
61
62/* Set to 1 if we've read EEPROM into memory */
63static int has_been_read = 0;
64
6d0f6bcf 65#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3 66/* Is this a valid NXID EEPROM? */
afb0b131
KG
67#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
68 (e.id[2] == 'I') || (e.id[3] == 'D'))
e2d31fb3
TT
69#endif
70
6d0f6bcf 71#ifdef CONFIG_SYS_I2C_EEPROM_CCID
e2d31fb3 72/* Is this a valid CCID EEPROM? */
afb0b131
KG
73#define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
74 (e.id[2] == 'I') || (e.id[3] == 'D'))
e2d31fb3
TT
75#endif
76
77/**
78 * show_eeprom - display the contents of the EEPROM
79 */
80static void show_eeprom(void)
bea3f28d
HW
81{
82 int i;
e2d31fb3
TT
83 unsigned int crc;
84
85 /* EEPROM tag ID, either CCID or NXID */
6d0f6bcf 86#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
87 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
88 be32_to_cpu(e.version));
89#else
90 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
91#endif
92
93 /* Serial number */
94 printf("SN: %s\n", e.sn);
95
96 /* Errata level. */
6d0f6bcf 97#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
98 printf("Errata: %s\n", e.errata);
99#else
100 printf("Errata: %c%c\n",
101 e.errata[0] ? e.errata[0] : '.',
102 e.errata[1] ? e.errata[1] : '.');
103#endif
104
105 /* Build date, BCD date values, as YYMMDDhhmmss */
106 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
107 e.date[0], e.date[1], e.date[2],
108 e.date[3] & 0x7F, e.date[4], e.date[5],
109 e.date[3] & 0x80 ? "PM" : "");
110
111 /* Show MAC addresses */
9a611089
HW
112 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
113
e2d31fb3 114 u8 *p = e.mac[i];
d59feffb 115
e2d31fb3
TT
116 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
117 p[0], p[1], p[2], p[3], p[4], p[5]);
d59feffb 118 }
bea3f28d 119
e2d31fb3
TT
120 crc = crc32(0, (void *)&e, sizeof(e) - 4);
121
122 if (crc == be32_to_cpu(e.crc))
123 printf("CRC: %08x\n", be32_to_cpu(e.crc));
124 else
125 printf("CRC: %08x (should be %08x)\n",
126 be32_to_cpu(e.crc), crc);
127
128#ifdef DEBUG
129 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
130 for (i = 0; i < sizeof(e); i++) {
131 if ((i % 16) == 0)
132 printf("%02X: ", i);
133 printf("%02X ", ((u8 *)&e)[i]);
134 if (((i % 16) == 15) || (i == sizeof(e) - 1))
135 printf("\n");
136 }
137#endif
bea3f28d
HW
138}
139
e2d31fb3
TT
140/**
141 * read_eeprom - read the EEPROM into memory
142 */
143static int read_eeprom(void)
bea3f28d 144{
e2d31fb3 145 int ret;
6d0f6bcf 146#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3
TT
147 unsigned int bus;
148#endif
bea3f28d 149
e2d31fb3
TT
150 if (has_been_read)
151 return 0;
bea3f28d 152
6d0f6bcf 153#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3 154 bus = i2c_get_bus_num();
6d0f6bcf 155 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
e2d31fb3
TT
156#endif
157
6d0f6bcf 158 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
e2d31fb3
TT
159 (void *)&e, sizeof(e));
160
6d0f6bcf 161#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3
TT
162 i2c_set_bus_num(bus);
163#endif
164
165#ifdef DEBUG
166 show_eeprom();
167#endif
168
169 has_been_read = (ret == 0) ? 1 : 0;
170
171 return ret;
bea3f28d
HW
172}
173
2d04db08
TT
174/**
175 * update_crc - update the CRC
176 *
177 * This function should be called after each update to the EEPROM structure,
178 * to make sure the CRC is always correct.
179 */
180static void update_crc(void)
181{
182 u32 crc;
183
184 crc = crc32(0, (void *)&e, sizeof(e) - 4);
185 e.crc = cpu_to_be32(crc);
186}
187
e2d31fb3
TT
188/**
189 * prog_eeprom - write the EEPROM from memory
190 */
191static int prog_eeprom(void)
bea3f28d 192{
3addcb93 193 int ret = 0;
9c671e70 194 int i;
e2d31fb3 195 void *p;
6d0f6bcf 196#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3
TT
197 unsigned int bus;
198#endif
199
200 /* Set the reserved values to 0xFF */
6d0f6bcf 201#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
202 e.res_0 = 0xFF;
203 memset(e.res_1, 0xFF, sizeof(e.res_1));
204#else
205 memset(e.res_0, 0xFF, sizeof(e.res_0));
206#endif
2d04db08 207 update_crc();
e2d31fb3 208
6d0f6bcf 209#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3 210 bus = i2c_get_bus_num();
6d0f6bcf 211 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
e2d31fb3
TT
212#endif
213
3addcb93
TT
214 /*
215 * The AT24C02 datasheet says that data can only be written in page
216 * mode, which means 8 bytes at a time, and it takes up to 5ms to
217 * complete a given write.
218 */
2d04db08 219 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
6d0f6bcf 220 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
2d04db08 221 p, min((sizeof(e) - i), 8));
bea3f28d
HW
222 if (ret)
223 break;
e2d31fb3 224 udelay(5000); /* 5ms write cycle timing */
bea3f28d 225 }
e2d31fb3 226
3addcb93
TT
227 if (!ret) {
228 /* Verify the write by reading back the EEPROM and comparing */
229 struct eeprom e2;
230
231 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
232 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2));
233 if (!ret && memcmp(&e, &e2, sizeof(e)))
234 ret = -1;
235 }
236
6d0f6bcf 237#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3
TT
238 i2c_set_bus_num(bus);
239#endif
240
bea3f28d
HW
241 if (ret) {
242 printf("Programming failed.\n");
3addcb93 243 has_been_read = 0;
bea3f28d 244 return -1;
bea3f28d 245 }
e2d31fb3
TT
246
247 printf("Programming passed.\n");
bea3f28d
HW
248 return 0;
249}
250
e2d31fb3
TT
251/**
252 * h2i - converts hex character into a number
253 *
254 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
255 * the integer equivalent.
256 */
257static inline u8 h2i(char p)
258{
259 if ((p >= '0') && (p <= '9'))
260 return p - '0';
261
262 if ((p >= 'A') && (p <= 'F'))
263 return (p - 'A') + 10;
264
265 if ((p >= 'a') && (p <= 'f'))
266 return (p - 'a') + 10;
267
268 return 0;
269}
270
271/**
272 * set_date - stores the build date into the EEPROM
273 *
274 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
275 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
276 * and stores it in the build date field of the EEPROM local copy.
277 */
278static void set_date(const char *string)
279{
280 unsigned int i;
281
282 if (strlen(string) != 12) {
283 printf("Usage: mac date YYMMDDhhmmss\n");
284 return;
285 }
286
287 for (i = 0; i < 6; i++)
288 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
2d04db08
TT
289
290 update_crc();
e2d31fb3
TT
291}
292
293/**
294 * set_mac_address - stores a MAC address into the EEPROM
295 *
296 * This function takes a pointer to MAC address string
297 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
298 * stores it in one of the MAC address fields of the EEPROM local copy.
299 */
300static void set_mac_address(unsigned int index, const char *string)
301{
302 char *p = (char *) string;
303 unsigned int i;
304
bfb70719 305 if ((index >= MAX_NUM_PORTS) || !string) {
e2d31fb3
TT
306 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
307 return;
308 }
309
310 for (i = 0; *p && (i < 6); i++) {
311 e.mac[index][i] = simple_strtoul(p, &p, 16);
312 if (*p == ':')
313 p++;
314 }
2d04db08
TT
315
316 update_crc();
e2d31fb3
TT
317}
318
54841ab5 319int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
bea3f28d 320{
e2d31fb3 321 char cmd;
bea3f28d 322
e2d31fb3
TT
323 if (argc == 1) {
324 show_eeprom();
325 return 0;
326 }
327
328 cmd = argv[1][0];
329
330 if (cmd == 'r') {
331 read_eeprom();
332 return 0;
333 }
bea3f28d 334
2d04db08
TT
335 if (cmd == 'i') {
336#ifdef CONFIG_SYS_I2C_EEPROM_NXID
337 memcpy(e.id, "NXID", sizeof(e.id));
bfb70719 338 e.version = NXID_VERSION;
2d04db08
TT
339#else
340 memcpy(e.id, "CCID", sizeof(e.id));
341#endif
3fee334c 342 update_crc();
e2d31fb3
TT
343 return 0;
344 }
345
346 if (!is_valid) {
347 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
348 return 0;
349 }
350
351 if (argc == 2) {
bea3f28d 352 switch (cmd) {
80e955c7 353 case 's': /* save */
e2d31fb3 354 prog_eeprom();
80e955c7 355 break;
80e955c7 356 default:
47e26b1b 357 return cmd_usage(cmdtp);
bea3f28d 358 }
e2d31fb3
TT
359
360 return 0;
361 }
362
363 /* We know we have at least one parameter */
364
365 switch (cmd) {
366 case 'n': /* serial number */
367 memset(e.sn, 0, sizeof(e.sn));
368 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
2d04db08 369 update_crc();
e2d31fb3
TT
370 break;
371 case 'e': /* errata */
6d0f6bcf 372#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
373 memset(e.errata, 0, 5);
374 strncpy((char *)e.errata, argv[2], 4);
375#else
376 e.errata[0] = argv[2][0];
377 e.errata[1] = argv[2][1];
378#endif
2d04db08 379 update_crc();
e2d31fb3
TT
380 break;
381 case 'd': /* date BCD format YYMMDDhhmmss */
382 set_date(argv[2]);
383 break;
384 case 'p': /* MAC table size */
385 e.mac_count = simple_strtoul(argv[2], NULL, 16);
2d04db08 386 update_crc();
e2d31fb3 387 break;
bfb70719
TT
388 case '0' ... '9': /* "mac 0" through "mac 22" */
389 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
e2d31fb3
TT
390 break;
391 case 'h': /* help */
392 default:
47e26b1b 393 return cmd_usage(cmdtp);
bea3f28d 394 }
e2d31fb3 395
bea3f28d
HW
396 return 0;
397}
398
e2d31fb3
TT
399/**
400 * mac_read_from_eeprom - read the MAC addresses from EEPROM
401 *
402 * This function reads the MAC addresses from EEPROM and sets the
403 * appropriate environment variables for each one read.
404 *
405 * The environment variables are only set if they haven't been set already.
406 * This ensures that any user-saved variables are never overwritten.
407 *
408 * This function must be called after relocation.
f098c9c8
TT
409 *
410 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
411 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
412 * located at a different offset.
e2d31fb3 413 */
bea3f28d
HW
414int mac_read_from_eeprom(void)
415{
e2d31fb3 416 unsigned int i;
f098c9c8
TT
417 u32 crc, crc_offset = offsetof(struct eeprom, crc);
418 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
2d04db08
TT
419
420 puts("EEPROM: ");
e2d31fb3
TT
421
422 if (read_eeprom()) {
bea3f28d
HW
423 printf("Read failed.\n");
424 return -1;
425 }
426
e2d31fb3 427 if (!is_valid) {
2d04db08
TT
428 printf("Invalid ID (%02x %02x %02x %02x)\n",
429 e.id[0], e.id[1], e.id[2], e.id[3]);
bea3f28d 430 return -1;
e2d31fb3
TT
431 }
432
f098c9c8
TT
433#ifdef CONFIG_SYS_I2C_EEPROM_NXID
434 /*
435 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
436 * to where it is in v0.
437 */
438 if (e.version == 0)
439 crc_offset = 0x72;
440#endif
441
442 crc = crc32(0, (void *)&e, crc_offset);
443 crcp = (void *)&e + crc_offset;
444 if (crc != be32_to_cpu(*crcp)) {
2d04db08
TT
445 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
446 return -1;
9a611089
HW
447 }
448
f098c9c8
TT
449#ifdef CONFIG_SYS_I2C_EEPROM_NXID
450 /*
451 * MAC address #9 in v1 occupies the same position as the CRC in v0.
452 * Erase it so that it's not mistaken for a MAC address. We'll
453 * update the CRC later.
454 */
455 if (e.version == 0)
456 memset(e.mac[8], 0xff, 6);
457#endif
458
2d04db08 459 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
e2d31fb3
TT
460 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
461 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
462 char ethaddr[18];
463 char enetvar[9];
464
465 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
466 e.mac[i][0],
467 e.mac[i][1],
468 e.mac[i][2],
469 e.mac[i][3],
470 e.mac[i][4],
471 e.mac[i][5]);
472 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
473 /* Only initialize environment variables that are blank
474 * (i.e. have not yet been set)
475 */
476 if (!getenv(enetvar))
477 setenv(enetvar, ethaddr);
478 }
479 }
480
2d04db08
TT
481#ifdef CONFIG_SYS_I2C_EEPROM_NXID
482 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
483 be32_to_cpu(e.version));
484#else
485 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
486#endif
487
f098c9c8
TT
488#ifdef CONFIG_SYS_I2C_EEPROM_NXID
489 /*
490 * Now we need to upconvert the data into v1 format. We do this last so
491 * that at boot time, U-Boot will still say "NXID v0".
492 */
493 if (e.version == 0) {
494 e.version = NXID_VERSION;
495 update_crc();
496 }
497#endif
498
bea3f28d
HW
499 return 0;
500}
e2d31fb3 501
6d0f6bcf 502#ifdef CONFIG_SYS_I2C_EEPROM_CCID
e2d31fb3
TT
503
504/**
505 * get_cpu_board_revision - get the CPU board revision on 85xx boards
506 *
507 * Read the EEPROM to determine the board revision.
508 *
509 * This function is called before relocation, so we need to read a private
510 * copy of the EEPROM into a local variable on the stack.
511 *
6d0f6bcf
JCPV
512 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
513 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
e2d31fb3 514 * so that the SPD code will work. This means that all pre-relocation I2C
6d0f6bcf
JCPV
515 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
516 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
e2d31fb3
TT
517 * this function is called. Oh well.
518 */
519unsigned int get_cpu_board_revision(void)
520{
521 struct board_eeprom {
522 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
523 u8 major; /* 0x04 Board revision, major */
524 u8 minor; /* 0x05 Board revision, minor */
525 } be;
526
6d0f6bcf 527 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
e2d31fb3
TT
528 (void *)&be, sizeof(be));
529
530 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
531 return MPC85XX_CPU_BOARD_REV(0, 0);
532
533 if ((be.major == 0xff) && (be.minor == 0xff))
534 return MPC85XX_CPU_BOARD_REV(0, 0);
535
e46c7bfb 536 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
e2d31fb3 537}
e2d31fb3 538#endif