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