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