1
2
3
4
5
6
7
8
9#include <common.h>
10#include <command.h>
11#include <env.h>
12#include <i2c.h>
13#include <init.h>
14#include <linux/ctype.h>
15#include <linux/delay.h>
16#include <u-boot/crc.h>
17
18#ifdef CONFIG_SYS_I2C_EEPROM_CCID
19#include "../common/eeprom.h"
20#define MAX_NUM_PORTS 8
21#endif
22
23#ifdef CONFIG_SYS_I2C_EEPROM_NXID
24
25
26#ifndef MAX_NUM_PORTS
27#define MAX_NUM_PORTS 16
28#endif
29#define NXID_VERSION 1
30#endif
31
32
33
34
35
36
37static struct __attribute__ ((__packed__)) eeprom {
38#ifdef CONFIG_SYS_I2C_EEPROM_CCID
39 u8 id[4];
40 u8 major;
41 u8 minor;
42 u8 sn[10];
43 u8 errata[2];
44 u8 date[6];
45 u8 res_0[40];
46 u8 mac_count;
47 u8 mac_flag;
48 u8 mac[MAX_NUM_PORTS][6];
49 u32 crc;
50#endif
51#ifdef CONFIG_SYS_I2C_EEPROM_NXID
52 u8 id[4];
53 u8 sn[12];
54 u8 errata[5];
55 u8 date[6];
56 u8 res_0;
57 u32 version;
58 u8 tempcal[8];
59 u8 tempcalsys[2];
60 u8 tempcalflags;
61 u8 res_1[21];
62 u8 mac_count;
63 u8 mac_flag;
64 u8 mac[MAX_NUM_PORTS][6];
65 u8 res_2[90];
66 u32 crc;
67#endif
68} e;
69
70
71static int has_been_read = 0;
72
73#ifdef CONFIG_SYS_I2C_EEPROM_NXID
74
75#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
76 (e.id[2] == 'I') || (e.id[3] == 'D'))
77#endif
78
79#ifdef CONFIG_SYS_I2C_EEPROM_CCID
80
81#define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
82 (e.id[2] == 'I') || (e.id[3] == 'D'))
83#endif
84
85
86
87
88static void show_eeprom(void)
89{
90 int i;
91 unsigned int crc;
92
93
94#ifdef CONFIG_SYS_I2C_EEPROM_NXID
95 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
96 be32_to_cpu(e.version));
97#else
98 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
99#endif
100
101
102 printf("SN: %s\n", e.sn);
103
104
105#ifdef CONFIG_SYS_I2C_EEPROM_NXID
106 printf("Errata: %s\n", e.errata);
107#else
108 printf("Errata: %c%c\n",
109 e.errata[0] ? e.errata[0] : '.',
110 e.errata[1] ? e.errata[1] : '.');
111#endif
112
113
114 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
115 e.date[0], e.date[1], e.date[2],
116 e.date[3] & 0x7F, e.date[4], e.date[5],
117 e.date[3] & 0x80 ? "PM" : "");
118
119
120 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
121
122 u8 *p = e.mac[i];
123
124 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
125 p[0], p[1], p[2], p[3], p[4], p[5]);
126 }
127
128 crc = crc32(0, (void *)&e, sizeof(e) - 4);
129
130 if (crc == be32_to_cpu(e.crc))
131 printf("CRC: %08x\n", be32_to_cpu(e.crc));
132 else
133 printf("CRC: %08x (should be %08x)\n",
134 be32_to_cpu(e.crc), crc);
135
136#ifdef DEBUG
137 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
138 for (i = 0; i < sizeof(e); i++) {
139 if ((i % 16) == 0)
140 printf("%02X: ", i);
141 printf("%02X ", ((u8 *)&e)[i]);
142 if (((i % 16) == 15) || (i == sizeof(e) - 1))
143 printf("\n");
144 }
145#endif
146}
147
148
149
150
151static int read_eeprom(void)
152{
153 int ret;
154#ifdef CONFIG_SYS_EEPROM_BUS_NUM
155#if !CONFIG_IS_ENABLED(DM_I2C)
156 unsigned int bus;
157#endif
158#endif
159
160 if (has_been_read)
161 return 0;
162
163#ifdef CONFIG_SYS_EEPROM_BUS_NUM
164#if !CONFIG_IS_ENABLED(DM_I2C)
165 bus = i2c_get_bus_num();
166 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
167#endif
168#endif
169
170#if !CONFIG_IS_ENABLED(DM_I2C)
171 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
172 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
173 (void *)&e, sizeof(e));
174#else
175 struct udevice *dev;
176#ifdef CONFIG_SYS_EEPROM_BUS_NUM
177 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
178 CONFIG_SYS_I2C_EEPROM_ADDR,
179 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev);
180#else
181 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
182 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev);
183#endif
184 if (!ret)
185 ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e));
186#endif
187
188#ifdef CONFIG_SYS_EEPROM_BUS_NUM
189#if !CONFIG_IS_ENABLED(DM_I2C)
190 i2c_set_bus_num(bus);
191#endif
192#endif
193
194#ifdef DEBUG
195 show_eeprom();
196#endif
197
198 has_been_read = (ret == 0) ? 1 : 0;
199
200 return ret;
201}
202
203
204
205
206
207
208
209static void update_crc(void)
210{
211 u32 crc;
212
213 crc = crc32(0, (void *)&e, sizeof(e) - 4);
214 e.crc = cpu_to_be32(crc);
215}
216
217
218
219
220static int prog_eeprom(void)
221{
222 int ret = 0;
223 int i;
224 void *p;
225#ifdef CONFIG_SYS_EEPROM_BUS_NUM
226#if !CONFIG_IS_ENABLED(DM_I2C)
227 unsigned int bus;
228#endif
229#endif
230
231
232#ifdef CONFIG_SYS_I2C_EEPROM_NXID
233 e.res_0 = 0xFF;
234 memset(e.res_1, 0xFF, sizeof(e.res_1));
235#else
236 memset(e.res_0, 0xFF, sizeof(e.res_0));
237#endif
238 update_crc();
239
240#if !CONFIG_IS_ENABLED(DM_I2C)
241#ifdef CONFIG_SYS_EEPROM_BUS_NUM
242 bus = i2c_get_bus_num();
243 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
244#endif
245#endif
246
247
248
249
250
251
252 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
253#if !CONFIG_IS_ENABLED(DM_I2C)
254 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i,
255 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
256 p, min((int)(sizeof(e) - i), 8));
257#else
258 struct udevice *dev;
259#ifdef CONFIG_SYS_EEPROM_BUS_NUM
260 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
261 CONFIG_SYS_I2C_EEPROM_ADDR,
262 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
263 &dev);
264#else
265 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
266 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
267 &dev);
268#endif
269 if (!ret)
270 ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i),
271 8));
272#endif
273 if (ret)
274 break;
275 udelay(5000);
276 }
277
278 if (!ret) {
279
280 struct eeprom e2;
281
282#if !CONFIG_IS_ENABLED(DM_I2C)
283 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
284 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
285 (void *)&e2, sizeof(e2));
286#else
287 struct udevice *dev;
288#ifdef CONFIG_SYS_EEPROM_BUS_NUM
289 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
290 CONFIG_SYS_I2C_EEPROM_ADDR,
291 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
292 &dev);
293#else
294 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
295 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
296 &dev);
297#endif
298 if (!ret)
299 ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2));
300#endif
301 if (!ret && memcmp(&e, &e2, sizeof(e)))
302 ret = -1;
303 }
304
305#if !CONFIG_IS_ENABLED(DM_I2C)
306#ifdef CONFIG_SYS_EEPROM_BUS_NUM
307 i2c_set_bus_num(bus);
308#endif
309#endif
310
311 if (ret) {
312 printf("Programming failed.\n");
313 has_been_read = 0;
314 return -1;
315 }
316
317 printf("Programming passed.\n");
318 return 0;
319}
320
321
322
323
324
325
326
327static inline u8 h2i(char p)
328{
329 if ((p >= '0') && (p <= '9'))
330 return p - '0';
331
332 if ((p >= 'A') && (p <= 'F'))
333 return (p - 'A') + 10;
334
335 if ((p >= 'a') && (p <= 'f'))
336 return (p - 'a') + 10;
337
338 return 0;
339}
340
341
342
343
344
345
346
347
348static void set_date(const char *string)
349{
350 unsigned int i;
351
352 if (strlen(string) != 12) {
353 printf("Usage: mac date YYMMDDhhmmss\n");
354 return;
355 }
356
357 for (i = 0; i < 6; i++)
358 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
359
360 update_crc();
361}
362
363
364
365
366
367
368
369
370static void set_mac_address(unsigned int index, const char *string)
371{
372 char *p = (char *) string;
373 unsigned int i;
374
375 if ((index >= MAX_NUM_PORTS) || !string) {
376 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
377 return;
378 }
379
380 for (i = 0; *p && (i < 6); i++) {
381 e.mac[index][i] = hextoul(p, &p);
382 if (*p == ':')
383 p++;
384 }
385
386 update_crc();
387}
388
389int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
390{
391 char cmd;
392
393 if (argc == 1) {
394 show_eeprom();
395 return 0;
396 }
397
398 cmd = argv[1][0];
399
400 if (cmd == 'r') {
401 read_eeprom();
402 return 0;
403 }
404
405 if (cmd == 'i') {
406#ifdef CONFIG_SYS_I2C_EEPROM_NXID
407 memcpy(e.id, "NXID", sizeof(e.id));
408 e.version = cpu_to_be32(NXID_VERSION);
409#else
410 memcpy(e.id, "CCID", sizeof(e.id));
411#endif
412 update_crc();
413 return 0;
414 }
415
416 if (!is_valid) {
417 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
418 return 0;
419 }
420
421 if (argc == 2) {
422 switch (cmd) {
423 case 's':
424 prog_eeprom();
425 break;
426 default:
427 return cmd_usage(cmdtp);
428 }
429
430 return 0;
431 }
432
433
434
435 switch (cmd) {
436 case 'n':
437 memset(e.sn, 0, sizeof(e.sn));
438 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
439 update_crc();
440 break;
441 case 'e':
442#ifdef CONFIG_SYS_I2C_EEPROM_NXID
443 memset(e.errata, 0, 5);
444 strncpy((char *)e.errata, argv[2], 4);
445#else
446 e.errata[0] = argv[2][0];
447 e.errata[1] = argv[2][1];
448#endif
449 update_crc();
450 break;
451 case 'd':
452 set_date(argv[2]);
453 break;
454 case 'p':
455 e.mac_count = hextoul(argv[2], NULL);
456 update_crc();
457 break;
458 case '0' ... '9':
459 set_mac_address(dectoul(argv[1], NULL), argv[2]);
460 break;
461 case 'h':
462 default:
463 return cmd_usage(cmdtp);
464 }
465
466 return 0;
467}
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484int mac_read_from_eeprom(void)
485{
486 unsigned int i;
487 u32 crc, crc_offset = offsetof(struct eeprom, crc);
488 u32 *crcp;
489
490 puts("EEPROM: ");
491
492 if (read_eeprom()) {
493 printf("Read failed.\n");
494 return 0;
495 }
496
497 if (!is_valid) {
498 printf("Invalid ID (%02x %02x %02x %02x)\n",
499 e.id[0], e.id[1], e.id[2], e.id[3]);
500 return 0;
501 }
502
503#ifdef CONFIG_SYS_I2C_EEPROM_NXID
504
505
506
507
508 if (e.version == 0)
509 crc_offset = 0x72;
510#endif
511
512 crc = crc32(0, (void *)&e, crc_offset);
513 crcp = (void *)&e + crc_offset;
514 if (crc != be32_to_cpu(*crcp)) {
515 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
516 return 0;
517 }
518
519#ifdef CONFIG_SYS_I2C_EEPROM_NXID
520
521
522
523
524
525 if (e.version == 0)
526 memset(e.mac[8], 0xff, 6);
527#endif
528
529 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
530 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
531 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
532 char ethaddr[18];
533 char enetvar[9];
534
535 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
536 e.mac[i][0],
537 e.mac[i][1],
538 e.mac[i][2],
539 e.mac[i][3],
540 e.mac[i][4],
541 e.mac[i][5]);
542 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
543
544
545
546 if (!env_get(enetvar))
547 env_set(enetvar, ethaddr);
548 }
549 }
550
551#ifdef CONFIG_SYS_I2C_EEPROM_NXID
552 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
553 be32_to_cpu(e.version));
554#else
555 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
556#endif
557
558#ifdef CONFIG_SYS_I2C_EEPROM_NXID
559
560
561
562
563 if (e.version == 0) {
564 e.version = cpu_to_be32(NXID_VERSION);
565 update_crc();
566 }
567#endif
568
569 return 0;
570}
571
572#ifdef CONFIG_SYS_I2C_EEPROM_CCID
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589unsigned int get_cpu_board_revision(void)
590{
591 struct board_eeprom {
592 u32 id;
593 u8 major;
594 u8 minor;
595 } be;
596
597#if !CONFIG_IS_ENABLED(DM_I2C)
598 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
599 (void *)&be, sizeof(be));
600#else
601 struct udevice *dev;
602 int ret;
603#ifdef CONFIG_SYS_EEPROM_BUS_NUM
604 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
605 CONFIG_SYS_I2C_EEPROM_ADDR,
606 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
607 &dev);
608#else
609 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
610 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
611 &dev);
612#endif
613 if (!ret)
614 dm_i2c_read(dev, 0, (void *)&be, sizeof(be));
615#endif
616
617 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
618 return MPC85XX_CPU_BOARD_REV(0, 0);
619
620 if ((be.major == 0xff) && (be.minor == 0xff))
621 return MPC85XX_CPU_BOARD_REV(0, 0);
622
623 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
624}
625#endif
626