1
2
3
4
5
6
7
8
9
10
11
12
13#include <common.h>
14#include <command.h>
15#include <i2c.h>
16#include <linux/ctype.h>
17
18#include "eeprom.h"
19
20#ifdef CONFIG_SYS_I2C_EEPROM_NXID_MAC
21#define MAX_NUM_PORTS CONFIG_SYS_I2C_EEPROM_NXID_MAC
22#else
23#define MAX_NUM_PORTS 8
24#endif
25#define NXID_VERSION 0
26
27
28
29
30
31
32static struct __attribute__ ((__packed__)) eeprom {
33 u8 id[4];
34 u8 sn[12];
35 u8 errata[5];
36 u8 date[6];
37 u8 res_0;
38 u32 version;
39 u8 tempcal[8];
40 u8 tempcalsys[2];
41 u8 tempcalflags;
42 u8 res_1[21];
43 u8 mac_count;
44 u8 mac_flag;
45 u8 mac[MAX_NUM_PORTS][6];
46 u32 crc;
47} e;
48
49
50static int has_been_read;
51
52
53#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
54 (e.id[2] == 'I') || (e.id[3] == 'D'))
55
56
57static unsigned char uid[16];
58
59static int eeprom_bus_num = -1;
60static int eeprom_addr;
61static int eeprom_addr_len;
62
63
64
65
66void init_eeprom(int bus_num, int addr, int addr_len)
67{
68 eeprom_bus_num = bus_num;
69 eeprom_addr = addr;
70 eeprom_addr_len = addr_len;
71}
72
73
74
75
76void show_eeprom(void)
77{
78 int i;
79 unsigned int crc;
80
81
82 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
83 be32_to_cpu(e.version));
84
85
86 printf("SN: %s\n", e.sn);
87
88 printf("UID: ");
89 for (i = 0; i < 16; i++)
90 printf("%02x", uid[i]);
91 printf("\n");
92
93
94 printf("Errata: %s\n", e.errata);
95
96
97 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
98 e.date[0], e.date[1], e.date[2],
99 e.date[3] & 0x7F, e.date[4], e.date[5],
100 e.date[3] & 0x80 ? "PM" : "");
101
102
103 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
104 u8 *p = e.mac[i];
105
106 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
107 p[0], p[1], p[2], p[3], p[4], p[5]);
108 }
109
110 crc = crc32(0, (void *)&e, sizeof(e) - 4);
111
112 if (crc == be32_to_cpu(e.crc))
113 printf("CRC: %08x\n", be32_to_cpu(e.crc));
114 else
115 printf("CRC: %08x (should be %08x)\n",
116 be32_to_cpu(e.crc), crc);
117
118#ifdef DEBUG
119 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
120 for (i = 0; i < sizeof(e); i++) {
121 if ((i % 16) == 0)
122 printf("%02X: ", i);
123 printf("%02X ", ((u8 *)&e)[i]);
124 if (((i % 16) == 15) || (i == sizeof(e) - 1))
125 printf("\n");
126 }
127#endif
128}
129
130
131
132
133int read_eeprom(void)
134{
135 int ret;
136 unsigned int bus;
137
138 if (eeprom_bus_num < 0) {
139 printf("EEPROM not configured\n");
140 return -1;
141 }
142
143 if (has_been_read)
144 return 0;
145
146 bus = i2c_get_bus_num();
147 i2c_set_bus_num(eeprom_bus_num);
148
149 ret = i2c_read(eeprom_addr, 0, eeprom_addr_len,
150 (void *)&e, sizeof(e));
151
152
153
154 i2c_read(0x5f, 0x80, 1, uid, 16);
155
156 i2c_set_bus_num(bus);
157
158#ifdef DEBUG
159 show_eeprom();
160#endif
161
162 has_been_read = (ret == 0) ? 1 : 0;
163
164 return ret;
165}
166
167
168
169
170
171
172
173static void update_crc(void)
174{
175 u32 crc, crc_offset = offsetof(struct eeprom, crc);
176
177 crc = crc32(0, (void *)&e, crc_offset);
178 e.crc = cpu_to_be32(crc);
179}
180
181
182
183
184static int prog_eeprom(void)
185{
186 int ret = 0;
187 int i;
188 void *p;
189 unsigned int bus;
190
191 if (eeprom_bus_num < 0) {
192 printf("EEPROM not configured\n");
193 return -1;
194 }
195
196
197 e.res_0 = 0xFF;
198 memset(e.res_1, 0xFF, sizeof(e.res_1));
199 update_crc();
200
201 bus = i2c_get_bus_num();
202 i2c_set_bus_num(eeprom_bus_num);
203
204
205
206
207
208
209 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
210 ret = i2c_write(eeprom_addr, i, eeprom_addr_len,
211 p, min((int)(sizeof(e) - i), 8));
212 if (ret)
213 break;
214 udelay(5000);
215 }
216
217 if (!ret) {
218
219 struct eeprom e2;
220
221 ret = i2c_read(eeprom_addr, 0,
222 eeprom_addr_len, (void *)&e2, sizeof(e2));
223 if (!ret && memcmp(&e, &e2, sizeof(e)))
224 ret = -1;
225 }
226
227 i2c_set_bus_num(bus);
228
229 if (ret) {
230 printf("Programming failed.\n");
231 has_been_read = 0;
232 return -1;
233 }
234
235 printf("Programming passed.\n");
236 return 0;
237}
238
239
240
241
242
243
244
245static inline u8 h2i(char p)
246{
247 if ((p >= '0') && (p <= '9'))
248 return p - '0';
249
250 if ((p >= 'A') && (p <= 'F'))
251 return (p - 'A') + 10;
252
253 if ((p >= 'a') && (p <= 'f'))
254 return (p - 'a') + 10;
255
256 return 0;
257}
258
259
260
261
262
263
264
265
266static void set_date(const char *string)
267{
268 unsigned int i;
269
270 if (strlen(string) != 12) {
271 printf("Usage: mac date YYMMDDhhmmss\n");
272 return;
273 }
274
275 for (i = 0; i < 6; i++)
276 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
277
278 update_crc();
279}
280
281
282
283
284
285
286
287
288static void set_mac_address(unsigned int index, const char *string)
289{
290 char *p = (char *)string;
291 unsigned int i;
292
293 if ((index >= MAX_NUM_PORTS) || !string) {
294 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
295 return;
296 }
297
298 for (i = 0; *p && (i < 6); i++) {
299 e.mac[index][i] = simple_strtoul(p, &p, 16);
300 if (*p == ':')
301 p++;
302 }
303
304 update_crc();
305}
306
307int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
308{
309 char cmd;
310
311 if (argc == 1) {
312 show_eeprom();
313 return 0;
314 }
315
316 cmd = argv[1][0];
317
318 if (cmd == 'r') {
319 read_eeprom();
320 return 0;
321 }
322
323 if (cmd == 'i') {
324 memcpy(e.id, "NXID", sizeof(e.id));
325 e.version = NXID_VERSION;
326 update_crc();
327 return 0;
328 }
329
330 if (!is_valid) {
331 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
332 return 0;
333 }
334
335 if (argc == 2) {
336 switch (cmd) {
337 case 's':
338 prog_eeprom();
339 break;
340 default:
341 return cmd_usage(cmdtp);
342 }
343
344 return 0;
345 }
346
347
348
349 switch (cmd) {
350 case 'n':
351 memset(e.sn, 0, sizeof(e.sn));
352 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
353 update_crc();
354 break;
355 case 'e':
356 memset(e.errata, 0, 5);
357 strncpy((char *)e.errata, argv[2], 4);
358 update_crc();
359 break;
360 case 'd':
361 set_date(argv[2]);
362 break;
363 case 'p':
364 e.mac_count = simple_strtoul(argv[2], NULL, 16);
365 update_crc();
366 break;
367 case '0' ... '9':
368 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
369 break;
370 case 'h':
371 default:
372 return cmd_usage(cmdtp);
373 }
374
375 return 0;
376}
377
378int mac_read_from_generic_eeprom(const char *envvar, int chip,
379 int address, int mac_bus)
380{
381 int ret;
382 unsigned int bus;
383 unsigned char mac[6];
384 char ethaddr[18];
385
386 bus = i2c_get_bus_num();
387 i2c_set_bus_num(mac_bus);
388
389 ret = i2c_read(chip, address, 1, mac, 6);
390
391 i2c_set_bus_num(bus);
392
393 if (!ret) {
394 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
395 mac[0],
396 mac[1],
397 mac[2],
398 mac[3],
399 mac[4],
400 mac[5]);
401
402 printf("MAC: %s\n", ethaddr);
403 env_set(envvar, ethaddr);
404 }
405
406 return ret;
407}
408
409void mac_read_from_fixed_id(void)
410{
411#ifdef CONFIG_SYS_I2C_MAC1_CHIP_ADDR
412 mac_read_from_generic_eeprom("ethaddr", CONFIG_SYS_I2C_MAC1_CHIP_ADDR,
413 CONFIG_SYS_I2C_MAC1_DATA_ADDR, CONFIG_SYS_I2C_MAC1_BUS);
414#endif
415#ifdef CONFIG_SYS_I2C_MAC2_CHIP_ADDR
416 mac_read_from_generic_eeprom("eth1addr", CONFIG_SYS_I2C_MAC2_CHIP_ADDR,
417 CONFIG_SYS_I2C_MAC2_DATA_ADDR, CONFIG_SYS_I2C_MAC2_BUS);
418#endif
419}
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436int mac_read_from_eeprom_common(void)
437{
438 unsigned int i;
439 u32 crc, crc_offset = offsetof(struct eeprom, crc);
440 u32 *crcp;
441
442 puts("EEPROM: ");
443
444 if (read_eeprom()) {
445 printf("Read failed.\n");
446 return 0;
447 }
448
449 if (!is_valid) {
450 printf("Invalid ID (%02x %02x %02x %02x)\n",
451 e.id[0], e.id[1], e.id[2], e.id[3]);
452 return 0;
453 }
454
455 crc = crc32(0, (void *)&e, crc_offset);
456 crcp = (void *)&e + crc_offset;
457 if (crc != be32_to_cpu(*crcp)) {
458 printf("CRC mismatch (%08x != %08x)\n", crc,
459 be32_to_cpu(e.crc));
460 return 0;
461 }
462
463
464
465
466
467
468 if (e.version == 0)
469 memset(e.mac[8], 0xff, 6);
470
471 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
472 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
473 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
474 char ethaddr[18];
475 char enetvar[9];
476
477 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
478 e.mac[i][0],
479 e.mac[i][1],
480 e.mac[i][2],
481 e.mac[i][3],
482 e.mac[i][4],
483 e.mac[i][5]);
484 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
485
486
487
488 if (!env_get(enetvar))
489 env_set(enetvar, ethaddr);
490 }
491 }
492
493 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
494 be32_to_cpu(e.version));
495
496 return 0;
497}
498