]> git.ipfire.org Git - people/ms/u-boot.git/blame - board/freescale/common/sys_eeprom.c
powerpc/freescale: Change the return value of mac_read_from_eeprom()
[people/ms/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
5536aeb0
SL
21/* some boards with non-256-bytes EEPROM have special define */
22/* for MAX_NUM_PORTS in board-specific file */
23#ifndef MAX_NUM_PORTS
bfb70719 24#define MAX_NUM_PORTS 23
5536aeb0 25#endif
bfb70719
TT
26#define NXID_VERSION 1
27#endif
9a611089 28
e2d31fb3
TT
29/**
30 * static eeprom: EEPROM layout for CCID or NXID formats
31 *
32 * See application note AN3638 for details.
33 */
34static struct __attribute__ ((__packed__)) eeprom {
6d0f6bcf 35#ifdef CONFIG_SYS_I2C_EEPROM_CCID
e2d31fb3
TT
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 */
9a611089 45 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
e2d31fb3
TT
46 u32 crc; /* 0x72 CRC32 checksum */
47#endif
6d0f6bcf 48#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
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 */
bfb70719
TT
61 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */
62 u32 crc; /* x+1 CRC32 checksum */
e2d31fb3
TT
63#endif
64} e;
65
66/* Set to 1 if we've read EEPROM into memory */
67static int has_been_read = 0;
68
6d0f6bcf 69#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3 70/* Is this a valid NXID EEPROM? */
afb0b131
KG
71#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
72 (e.id[2] == 'I') || (e.id[3] == 'D'))
e2d31fb3
TT
73#endif
74
6d0f6bcf 75#ifdef CONFIG_SYS_I2C_EEPROM_CCID
e2d31fb3 76/* Is this a valid CCID EEPROM? */
afb0b131
KG
77#define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
78 (e.id[2] == 'I') || (e.id[3] == 'D'))
e2d31fb3
TT
79#endif
80
81/**
82 * show_eeprom - display the contents of the EEPROM
83 */
84static void show_eeprom(void)
bea3f28d
HW
85{
86 int i;
e2d31fb3
TT
87 unsigned int crc;
88
89 /* EEPROM tag ID, either CCID or NXID */
6d0f6bcf 90#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
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));
93#else
94 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
95#endif
96
97 /* Serial number */
98 printf("SN: %s\n", e.sn);
99
100 /* Errata level. */
6d0f6bcf 101#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
102 printf("Errata: %s\n", e.errata);
103#else
104 printf("Errata: %c%c\n",
105 e.errata[0] ? e.errata[0] : '.',
106 e.errata[1] ? e.errata[1] : '.');
107#endif
108
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" : "");
114
115 /* Show MAC addresses */
9a611089
HW
116 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
117
e2d31fb3 118 u8 *p = e.mac[i];
d59feffb 119
e2d31fb3
TT
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]);
d59feffb 122 }
bea3f28d 123
e2d31fb3
TT
124 crc = crc32(0, (void *)&e, sizeof(e) - 4);
125
126 if (crc == be32_to_cpu(e.crc))
127 printf("CRC: %08x\n", be32_to_cpu(e.crc));
128 else
129 printf("CRC: %08x (should be %08x)\n",
130 be32_to_cpu(e.crc), crc);
131
132#ifdef DEBUG
133 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
134 for (i = 0; i < sizeof(e); i++) {
135 if ((i % 16) == 0)
136 printf("%02X: ", i);
137 printf("%02X ", ((u8 *)&e)[i]);
138 if (((i % 16) == 15) || (i == sizeof(e) - 1))
139 printf("\n");
140 }
141#endif
bea3f28d
HW
142}
143
e2d31fb3
TT
144/**
145 * read_eeprom - read the EEPROM into memory
146 */
147static int read_eeprom(void)
bea3f28d 148{
e2d31fb3 149 int ret;
6d0f6bcf 150#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3
TT
151 unsigned int bus;
152#endif
bea3f28d 153
e2d31fb3
TT
154 if (has_been_read)
155 return 0;
bea3f28d 156
6d0f6bcf 157#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3 158 bus = i2c_get_bus_num();
6d0f6bcf 159 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
e2d31fb3
TT
160#endif
161
6d0f6bcf 162 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
e2d31fb3
TT
163 (void *)&e, sizeof(e));
164
6d0f6bcf 165#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3
TT
166 i2c_set_bus_num(bus);
167#endif
168
169#ifdef DEBUG
170 show_eeprom();
171#endif
172
173 has_been_read = (ret == 0) ? 1 : 0;
174
175 return ret;
bea3f28d
HW
176}
177
2d04db08
TT
178/**
179 * update_crc - update the CRC
180 *
181 * This function should be called after each update to the EEPROM structure,
182 * to make sure the CRC is always correct.
183 */
184static void update_crc(void)
185{
186 u32 crc;
187
188 crc = crc32(0, (void *)&e, sizeof(e) - 4);
189 e.crc = cpu_to_be32(crc);
190}
191
e2d31fb3
TT
192/**
193 * prog_eeprom - write the EEPROM from memory
194 */
195static int prog_eeprom(void)
bea3f28d 196{
3addcb93 197 int ret = 0;
9c671e70 198 int i;
e2d31fb3 199 void *p;
6d0f6bcf 200#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3
TT
201 unsigned int bus;
202#endif
203
204 /* Set the reserved values to 0xFF */
6d0f6bcf 205#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
206 e.res_0 = 0xFF;
207 memset(e.res_1, 0xFF, sizeof(e.res_1));
208#else
209 memset(e.res_0, 0xFF, sizeof(e.res_0));
210#endif
2d04db08 211 update_crc();
e2d31fb3 212
6d0f6bcf 213#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3 214 bus = i2c_get_bus_num();
6d0f6bcf 215 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
e2d31fb3
TT
216#endif
217
3addcb93
TT
218 /*
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.
222 */
2d04db08 223 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
6d0f6bcf 224 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
2d04db08 225 p, min((sizeof(e) - i), 8));
bea3f28d
HW
226 if (ret)
227 break;
e2d31fb3 228 udelay(5000); /* 5ms write cycle timing */
bea3f28d 229 }
e2d31fb3 230
3addcb93
TT
231 if (!ret) {
232 /* Verify the write by reading back the EEPROM and comparing */
233 struct eeprom e2;
234
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)))
238 ret = -1;
239 }
240
6d0f6bcf 241#ifdef CONFIG_SYS_EEPROM_BUS_NUM
e2d31fb3
TT
242 i2c_set_bus_num(bus);
243#endif
244
bea3f28d
HW
245 if (ret) {
246 printf("Programming failed.\n");
3addcb93 247 has_been_read = 0;
bea3f28d 248 return -1;
bea3f28d 249 }
e2d31fb3
TT
250
251 printf("Programming passed.\n");
bea3f28d
HW
252 return 0;
253}
254
e2d31fb3
TT
255/**
256 * h2i - converts hex character into a number
257 *
258 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
259 * the integer equivalent.
260 */
261static inline u8 h2i(char p)
262{
263 if ((p >= '0') && (p <= '9'))
264 return p - '0';
265
266 if ((p >= 'A') && (p <= 'F'))
267 return (p - 'A') + 10;
268
269 if ((p >= 'a') && (p <= 'f'))
270 return (p - 'a') + 10;
271
272 return 0;
273}
274
275/**
276 * set_date - stores the build date into the EEPROM
277 *
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.
281 */
282static void set_date(const char *string)
283{
284 unsigned int i;
285
286 if (strlen(string) != 12) {
287 printf("Usage: mac date YYMMDDhhmmss\n");
288 return;
289 }
290
291 for (i = 0; i < 6; i++)
292 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
2d04db08
TT
293
294 update_crc();
e2d31fb3
TT
295}
296
297/**
298 * set_mac_address - stores a MAC address into the EEPROM
299 *
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.
303 */
304static void set_mac_address(unsigned int index, const char *string)
305{
306 char *p = (char *) string;
307 unsigned int i;
308
bfb70719 309 if ((index >= MAX_NUM_PORTS) || !string) {
e2d31fb3
TT
310 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
311 return;
312 }
313
314 for (i = 0; *p && (i < 6); i++) {
315 e.mac[index][i] = simple_strtoul(p, &p, 16);
316 if (*p == ':')
317 p++;
318 }
2d04db08
TT
319
320 update_crc();
e2d31fb3
TT
321}
322
54841ab5 323int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
bea3f28d 324{
e2d31fb3 325 char cmd;
bea3f28d 326
e2d31fb3
TT
327 if (argc == 1) {
328 show_eeprom();
329 return 0;
330 }
331
332 cmd = argv[1][0];
333
334 if (cmd == 'r') {
335 read_eeprom();
336 return 0;
337 }
bea3f28d 338
2d04db08
TT
339 if (cmd == 'i') {
340#ifdef CONFIG_SYS_I2C_EEPROM_NXID
341 memcpy(e.id, "NXID", sizeof(e.id));
bfb70719 342 e.version = NXID_VERSION;
2d04db08
TT
343#else
344 memcpy(e.id, "CCID", sizeof(e.id));
345#endif
3fee334c 346 update_crc();
e2d31fb3
TT
347 return 0;
348 }
349
350 if (!is_valid) {
351 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
352 return 0;
353 }
354
355 if (argc == 2) {
bea3f28d 356 switch (cmd) {
80e955c7 357 case 's': /* save */
e2d31fb3 358 prog_eeprom();
80e955c7 359 break;
80e955c7 360 default:
47e26b1b 361 return cmd_usage(cmdtp);
bea3f28d 362 }
e2d31fb3
TT
363
364 return 0;
365 }
366
367 /* We know we have at least one parameter */
368
369 switch (cmd) {
370 case 'n': /* serial number */
371 memset(e.sn, 0, sizeof(e.sn));
372 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
2d04db08 373 update_crc();
e2d31fb3
TT
374 break;
375 case 'e': /* errata */
6d0f6bcf 376#ifdef CONFIG_SYS_I2C_EEPROM_NXID
e2d31fb3
TT
377 memset(e.errata, 0, 5);
378 strncpy((char *)e.errata, argv[2], 4);
379#else
380 e.errata[0] = argv[2][0];
381 e.errata[1] = argv[2][1];
382#endif
2d04db08 383 update_crc();
e2d31fb3
TT
384 break;
385 case 'd': /* date BCD format YYMMDDhhmmss */
386 set_date(argv[2]);
387 break;
388 case 'p': /* MAC table size */
389 e.mac_count = simple_strtoul(argv[2], NULL, 16);
2d04db08 390 update_crc();
e2d31fb3 391 break;
bfb70719
TT
392 case '0' ... '9': /* "mac 0" through "mac 22" */
393 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
e2d31fb3
TT
394 break;
395 case 'h': /* help */
396 default:
47e26b1b 397 return cmd_usage(cmdtp);
bea3f28d 398 }
e2d31fb3 399
bea3f28d
HW
400 return 0;
401}
402
e2d31fb3
TT
403/**
404 * mac_read_from_eeprom - read the MAC addresses from EEPROM
405 *
406 * This function reads the MAC addresses from EEPROM and sets the
407 * appropriate environment variables for each one read.
408 *
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.
411 *
412 * This function must be called after relocation.
f098c9c8
TT
413 *
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.
e2d31fb3 417 */
bea3f28d
HW
418int mac_read_from_eeprom(void)
419{
e2d31fb3 420 unsigned int i;
f098c9c8
TT
421 u32 crc, crc_offset = offsetof(struct eeprom, crc);
422 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
2d04db08
TT
423
424 puts("EEPROM: ");
e2d31fb3
TT
425
426 if (read_eeprom()) {
bea3f28d 427 printf("Read failed.\n");
bffac7ae 428 return 0;
bea3f28d
HW
429 }
430
e2d31fb3 431 if (!is_valid) {
2d04db08
TT
432 printf("Invalid ID (%02x %02x %02x %02x)\n",
433 e.id[0], e.id[1], e.id[2], e.id[3]);
bffac7ae 434 return 0;
e2d31fb3
TT
435 }
436
f098c9c8
TT
437#ifdef CONFIG_SYS_I2C_EEPROM_NXID
438 /*
439 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
440 * to where it is in v0.
441 */
442 if (e.version == 0)
443 crc_offset = 0x72;
444#endif
445
446 crc = crc32(0, (void *)&e, crc_offset);
447 crcp = (void *)&e + crc_offset;
448 if (crc != be32_to_cpu(*crcp)) {
2d04db08 449 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
bffac7ae 450 return 0;
9a611089
HW
451 }
452
f098c9c8
TT
453#ifdef CONFIG_SYS_I2C_EEPROM_NXID
454 /*
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.
458 */
459 if (e.version == 0)
460 memset(e.mac[8], 0xff, 6);
461#endif
462
2d04db08 463 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
e2d31fb3
TT
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)) {
466 char ethaddr[18];
467 char enetvar[9];
468
469 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
470 e.mac[i][0],
471 e.mac[i][1],
472 e.mac[i][2],
473 e.mac[i][3],
474 e.mac[i][4],
475 e.mac[i][5]);
476 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
477 /* Only initialize environment variables that are blank
478 * (i.e. have not yet been set)
479 */
480 if (!getenv(enetvar))
481 setenv(enetvar, ethaddr);
482 }
483 }
484
2d04db08
TT
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));
488#else
489 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
490#endif
491
f098c9c8
TT
492#ifdef CONFIG_SYS_I2C_EEPROM_NXID
493 /*
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".
496 */
497 if (e.version == 0) {
498 e.version = NXID_VERSION;
499 update_crc();
500 }
501#endif
502
bea3f28d
HW
503 return 0;
504}
e2d31fb3 505
6d0f6bcf 506#ifdef CONFIG_SYS_I2C_EEPROM_CCID
e2d31fb3
TT
507
508/**
509 * get_cpu_board_revision - get the CPU board revision on 85xx boards
510 *
511 * Read the EEPROM to determine the board revision.
512 *
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.
515 *
6d0f6bcf
JCPV
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,
e2d31fb3 518 * so that the SPD code will work. This means that all pre-relocation I2C
6d0f6bcf
JCPV
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
e2d31fb3
TT
521 * this function is called. Oh well.
522 */
523unsigned int get_cpu_board_revision(void)
524{
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 */
529 } be;
530
6d0f6bcf 531 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
e2d31fb3
TT
532 (void *)&be, sizeof(be));
533
534 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
535 return MPC85XX_CPU_BOARD_REV(0, 0);
536
537 if ((be.major == 0xff) && (be.minor == 0xff))
538 return MPC85XX_CPU_BOARD_REV(0, 0);
539
e46c7bfb 540 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
e2d31fb3 541}
e2d31fb3 542#endif