b3421052a5813475c5a3a4f1ccb5111c4e81cf0f
[oweals/u-boot.git] / board / freescale / common / sys_eeprom.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
4  * York Sun (yorksun@freescale.com)
5  * Haiying Wang (haiying.wang@freescale.com)
6  * Timur Tabi (timur@freescale.com)
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 <u-boot/crc.h>
16
17 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
18 #include "../common/eeprom.h"
19 #define MAX_NUM_PORTS   8
20 #endif
21
22 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
23 /* some boards with non-256-bytes EEPROM have special define */
24 /* for MAX_NUM_PORTS in board-specific file */
25 #ifndef MAX_NUM_PORTS
26 #define MAX_NUM_PORTS   16
27 #endif
28 #define NXID_VERSION    1
29 #endif
30
31 /**
32  * static eeprom: EEPROM layout for CCID or NXID formats
33  *
34  * See application note AN3638 for details.
35  */
36 static struct __attribute__ ((__packed__)) eeprom {
37 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
38         u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'CCID' */
39         u8 major;         /* 0x04        Board revision, major */
40         u8 minor;         /* 0x05        Board revision, minor */
41         u8 sn[10];        /* 0x06 - 0x0F Serial Number*/
42         u8 errata[2];     /* 0x10 - 0x11 Errata Level */
43         u8 date[6];       /* 0x12 - 0x17 Build Date */
44         u8 res_0[40];     /* 0x18 - 0x3f Reserved */
45         u8 mac_count;     /* 0x40        Number of MAC addresses */
46         u8 mac_flag;      /* 0x41        MAC table flags */
47         u8 mac[MAX_NUM_PORTS][6];     /* 0x42 - 0x71 MAC addresses */
48         u32 crc;          /* 0x72        CRC32 checksum */
49 #endif
50 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
51         u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'NXID' */
52         u8 sn[12];        /* 0x04 - 0x0F Serial Number */
53         u8 errata[5];     /* 0x10 - 0x14 Errata Level */
54         u8 date[6];       /* 0x15 - 0x1a Build Date */
55         u8 res_0;         /* 0x1b        Reserved */
56         u32 version;      /* 0x1c - 0x1f NXID Version */
57         u8 tempcal[8];    /* 0x20 - 0x27 Temperature Calibration Factors */
58         u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
59         u8 tempcalflags;  /* 0x2a        Temperature Calibration Flags */
60         u8 res_1[21];     /* 0x2b - 0x3f Reserved */
61         u8 mac_count;     /* 0x40        Number of MAC addresses */
62         u8 mac_flag;      /* 0x41        MAC table flags */
63         u8 mac[MAX_NUM_PORTS][6];     /* 0x42 - 0xa1 MAC addresses */
64         u8 res_2[90];     /* 0xa2 - 0xfb Reserved */    
65         u32 crc;          /* 0xfc - 0xff CRC32 checksum */
66 #endif
67 } e;
68
69 /* Set to 1 if we've read EEPROM into memory */
70 static int has_been_read = 0;
71
72 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
73 /* Is this a valid NXID EEPROM? */
74 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
75                   (e.id[2] == 'I') || (e.id[3] == 'D'))
76 #endif
77
78 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
79 /* Is this a valid CCID EEPROM? */
80 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
81                   (e.id[2] == 'I') || (e.id[3] == 'D'))
82 #endif
83
84 /**
85  * show_eeprom - display the contents of the EEPROM
86  */
87 static void show_eeprom(void)
88 {
89         int i;
90         unsigned int crc;
91
92         /* EEPROM tag ID, either CCID or NXID */
93 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
94         printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
95                be32_to_cpu(e.version));
96 #else
97         printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
98 #endif
99
100         /* Serial number */
101         printf("SN: %s\n", e.sn);
102
103         /* Errata level. */
104 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
105         printf("Errata: %s\n", e.errata);
106 #else
107         printf("Errata: %c%c\n",
108                 e.errata[0] ? e.errata[0] : '.',
109                 e.errata[1] ? e.errata[1] : '.');
110 #endif
111
112         /* Build date, BCD date values, as YYMMDDhhmmss */
113         printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
114                 e.date[0], e.date[1], e.date[2],
115                 e.date[3] & 0x7F, e.date[4], e.date[5],
116                 e.date[3] & 0x80 ? "PM" : "");
117
118         /* Show MAC addresses  */
119         for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
120
121                 u8 *p = e.mac[i];
122
123                 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
124                         p[0], p[1], p[2], p[3], p[4], p[5]);
125         }
126
127         crc = crc32(0, (void *)&e, sizeof(e) - 4);
128
129         if (crc == be32_to_cpu(e.crc))
130                 printf("CRC: %08x\n", be32_to_cpu(e.crc));
131         else
132                 printf("CRC: %08x (should be %08x)\n",
133                         be32_to_cpu(e.crc), crc);
134
135 #ifdef DEBUG
136         printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
137         for (i = 0; i < sizeof(e); i++) {
138                 if ((i % 16) == 0)
139                         printf("%02X: ", i);
140                 printf("%02X ", ((u8 *)&e)[i]);
141                 if (((i % 16) == 15) || (i == sizeof(e) - 1))
142                         printf("\n");
143         }
144 #endif
145 }
146
147 /**
148  * read_eeprom - read the EEPROM into memory
149  */
150 static int read_eeprom(void)
151 {
152         int ret;
153 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
154 #ifndef CONFIG_DM_I2C
155         unsigned int bus;
156 #endif
157 #endif
158
159         if (has_been_read)
160                 return 0;
161
162 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
163 #ifndef CONFIG_DM_I2C
164         bus = i2c_get_bus_num();
165         i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
166 #endif
167 #endif
168
169 #ifndef CONFIG_DM_I2C
170         ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
171                        CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
172                        (void *)&e, sizeof(e));
173 #else
174         struct udevice *dev;
175 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
176         ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
177                                       CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev);
178 #else
179         ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev);
180 #endif
181         if (!ret)
182                 ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e));
183 #endif
184
185 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
186 #ifndef CONFIG_DM_I2C
187         i2c_set_bus_num(bus);
188 #endif
189 #endif
190
191 #ifdef DEBUG
192         show_eeprom();
193 #endif
194
195         has_been_read = (ret == 0) ? 1 : 0;
196
197         return ret;
198 }
199
200 /**
201  *  update_crc - update the CRC
202  *
203  *  This function should be called after each update to the EEPROM structure,
204  *  to make sure the CRC is always correct.
205  */
206 static void update_crc(void)
207 {
208         u32 crc;
209
210         crc = crc32(0, (void *)&e, sizeof(e) - 4);
211         e.crc = cpu_to_be32(crc);
212 }
213
214 /**
215  * prog_eeprom - write the EEPROM from memory
216  */
217 static int prog_eeprom(void)
218 {
219         int ret = 0;
220         int i;
221         void *p;
222 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
223 #ifndef CONFIG_DM_I2C
224         unsigned int bus;
225 #endif
226 #endif
227
228         /* Set the reserved values to 0xFF   */
229 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
230         e.res_0 = 0xFF;
231         memset(e.res_1, 0xFF, sizeof(e.res_1));
232 #else
233         memset(e.res_0, 0xFF, sizeof(e.res_0));
234 #endif
235         update_crc();
236
237 #ifndef CONFIG_DM_I2C
238 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
239         bus = i2c_get_bus_num();
240         i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
241 #endif
242 #endif
243
244         /*
245          * The AT24C02 datasheet says that data can only be written in page
246          * mode, which means 8 bytes at a time, and it takes up to 5ms to
247          * complete a given write.
248          */
249         for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
250 #ifndef CONFIG_DM_I2C
251                 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i,
252                                 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
253                                 p, min((int)(sizeof(e) - i), 8));
254 #else
255                 struct udevice *dev;
256 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
257                 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
258                                               CONFIG_SYS_I2C_EEPROM_ADDR,
259                                               CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
260                                               &dev);
261 #else
262                 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
263                                               CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
264                                               &dev);
265 #endif
266                 if (!ret)
267                         ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i),
268                                                           8));
269 #endif
270                 if (ret)
271                         break;
272                 udelay(5000);   /* 5ms write cycle timing */
273         }
274
275         if (!ret) {
276                 /* Verify the write by reading back the EEPROM and comparing */
277                 struct eeprom e2;
278
279 #ifndef CONFIG_DM_I2C
280                 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
281                                CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
282                                (void *)&e2, sizeof(e2));
283 #else
284                 struct udevice *dev;
285 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
286                 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
287                                               CONFIG_SYS_I2C_EEPROM_ADDR,
288                                               CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
289                                               &dev);
290 #else
291                 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
292                                               CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
293                                               &dev);
294 #endif
295                 if (!ret)
296                         ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2));
297 #endif
298                 if (!ret && memcmp(&e, &e2, sizeof(e)))
299                         ret = -1;
300         }
301
302 #ifndef CONFIG_DM_I2C
303 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
304         i2c_set_bus_num(bus);
305 #endif
306 #endif
307
308         if (ret) {
309                 printf("Programming failed.\n");
310                 has_been_read = 0;
311                 return -1;
312         }
313
314         printf("Programming passed.\n");
315         return 0;
316 }
317
318 /**
319  * h2i - converts hex character into a number
320  *
321  * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
322  * the integer equivalent.
323  */
324 static inline u8 h2i(char p)
325 {
326         if ((p >= '0') && (p <= '9'))
327                 return p - '0';
328
329         if ((p >= 'A') && (p <= 'F'))
330                 return (p - 'A') + 10;
331
332         if ((p >= 'a') && (p <= 'f'))
333                 return (p - 'a') + 10;
334
335         return 0;
336 }
337
338 /**
339  * set_date - stores the build date into the EEPROM
340  *
341  * This function takes a pointer to a string in the format "YYMMDDhhmmss"
342  * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
343  * and stores it in the build date field of the EEPROM local copy.
344  */
345 static void set_date(const char *string)
346 {
347         unsigned int i;
348
349         if (strlen(string) != 12) {
350                 printf("Usage: mac date YYMMDDhhmmss\n");
351                 return;
352         }
353
354         for (i = 0; i < 6; i++)
355                 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
356
357         update_crc();
358 }
359
360 /**
361  * set_mac_address - stores a MAC address into the EEPROM
362  *
363  * This function takes a pointer to MAC address string
364  * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
365  * stores it in one of the MAC address fields of the EEPROM local copy.
366  */
367 static void set_mac_address(unsigned int index, const char *string)
368 {
369         char *p = (char *) string;
370         unsigned int i;
371
372         if ((index >= MAX_NUM_PORTS) || !string) {
373                 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
374                 return;
375         }
376
377         for (i = 0; *p && (i < 6); i++) {
378                 e.mac[index][i] = simple_strtoul(p, &p, 16);
379                 if (*p == ':')
380                         p++;
381         }
382
383         update_crc();
384 }
385
386 int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
387 {
388         char cmd;
389
390         if (argc == 1) {
391                 show_eeprom();
392                 return 0;
393         }
394
395         cmd = argv[1][0];
396
397         if (cmd == 'r') {
398                 read_eeprom();
399                 return 0;
400         }
401
402         if (cmd == 'i') {
403 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
404                 memcpy(e.id, "NXID", sizeof(e.id));
405                 e.version = cpu_to_be32(NXID_VERSION);
406 #else
407                 memcpy(e.id, "CCID", sizeof(e.id));
408 #endif
409                 update_crc();
410                 return 0;
411         }
412
413         if (!is_valid) {
414                 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
415                 return 0;
416         }
417
418         if (argc == 2) {
419                 switch (cmd) {
420                 case 's':       /* save */
421                         prog_eeprom();
422                         break;
423                 default:
424                         return cmd_usage(cmdtp);
425                 }
426
427                 return 0;
428         }
429
430         /* We know we have at least one parameter  */
431
432         switch (cmd) {
433         case 'n':       /* serial number */
434                 memset(e.sn, 0, sizeof(e.sn));
435                 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
436                 update_crc();
437                 break;
438         case 'e':       /* errata */
439 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
440                 memset(e.errata, 0, 5);
441                 strncpy((char *)e.errata, argv[2], 4);
442 #else
443                 e.errata[0] = argv[2][0];
444                 e.errata[1] = argv[2][1];
445 #endif
446                 update_crc();
447                 break;
448         case 'd':       /* date BCD format YYMMDDhhmmss */
449                 set_date(argv[2]);
450                 break;
451         case 'p':       /* MAC table size */
452                 e.mac_count = simple_strtoul(argv[2], NULL, 16);
453                 update_crc();
454                 break;
455         case '0' ... '9':       /* "mac 0" through "mac 22" */
456                 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
457                 break;
458         case 'h':       /* help */
459         default:
460                 return cmd_usage(cmdtp);
461         }
462
463         return 0;
464 }
465
466 /**
467  * mac_read_from_eeprom - read the MAC addresses from EEPROM
468  *
469  * This function reads the MAC addresses from EEPROM and sets the
470  * appropriate environment variables for each one read.
471  *
472  * The environment variables are only set if they haven't been set already.
473  * This ensures that any user-saved variables are never overwritten.
474  *
475  * This function must be called after relocation.
476  *
477  * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
478  * format.  In a v0 EEPROM, there are only eight MAC addresses and the CRC is
479  * located at a different offset.
480  */
481 int mac_read_from_eeprom(void)
482 {
483         unsigned int i;
484         u32 crc, crc_offset = offsetof(struct eeprom, crc);
485         u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
486
487         puts("EEPROM: ");
488
489         if (read_eeprom()) {
490                 printf("Read failed.\n");
491                 return 0;
492         }
493
494         if (!is_valid) {
495                 printf("Invalid ID (%02x %02x %02x %02x)\n",
496                        e.id[0], e.id[1], e.id[2], e.id[3]);
497                 return 0;
498         }
499
500 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
501         /*
502          * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
503          * to where it is in v0.
504          */
505         if (e.version == 0)
506                 crc_offset = 0x72;
507 #endif
508
509         crc = crc32(0, (void *)&e, crc_offset);
510         crcp = (void *)&e + crc_offset;
511         if (crc != be32_to_cpu(*crcp)) {
512                 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
513                 return 0;
514         }
515
516 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
517         /*
518          * MAC address #9 in v1 occupies the same position as the CRC in v0.
519          * Erase it so that it's not mistaken for a MAC address.  We'll
520          * update the CRC later.
521          */
522         if (e.version == 0)
523                 memset(e.mac[8], 0xff, 6);
524 #endif
525
526         for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
527                 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
528                     memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
529                         char ethaddr[18];
530                         char enetvar[9];
531
532                         sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
533                                 e.mac[i][0],
534                                 e.mac[i][1],
535                                 e.mac[i][2],
536                                 e.mac[i][3],
537                                 e.mac[i][4],
538                                 e.mac[i][5]);
539                         sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
540                         /* Only initialize environment variables that are blank
541                          * (i.e. have not yet been set)
542                          */
543                         if (!env_get(enetvar))
544                                 env_set(enetvar, ethaddr);
545                 }
546         }
547
548 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
549         printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
550                be32_to_cpu(e.version));
551 #else
552         printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
553 #endif
554
555 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
556         /*
557          * Now we need to upconvert the data into v1 format.  We do this last so
558          * that at boot time, U-Boot will still say "NXID v0".
559          */
560         if (e.version == 0) {
561                 e.version = cpu_to_be32(NXID_VERSION);
562                 update_crc();
563         }
564 #endif
565
566         return 0;
567 }
568
569 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
570
571 /**
572  * get_cpu_board_revision - get the CPU board revision on 85xx boards
573  *
574  * Read the EEPROM to determine the board revision.
575  *
576  * This function is called before relocation, so we need to read a private
577  * copy of the EEPROM into a local variable on the stack.
578  *
579  * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM.  The global
580  * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
581  * so that the SPD code will work.  This means that all pre-relocation I2C
582  * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus.  So if
583  * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
584  * this function is called.  Oh well.
585  */
586 unsigned int get_cpu_board_revision(void)
587 {
588         struct board_eeprom {
589                 u32 id;           /* 0x00 - 0x03 EEPROM Tag 'CCID' */
590                 u8 major;         /* 0x04        Board revision, major */
591                 u8 minor;         /* 0x05        Board revision, minor */
592         } be;
593
594 #ifndef CONFIG_DM_I2C
595         i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
596                 (void *)&be, sizeof(be));
597 #else
598         struct udevice *dev;
599         int ret;
600 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
601         ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
602                                       CONFIG_SYS_I2C_EEPROM_ADDR,
603                                       CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
604                                       &dev);
605 #else
606         ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
607                                       CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
608                                       &dev);
609 #endif
610         if (!ret)
611                 dm_i2c_read(dev, 0, (void *)&be, sizeof(be));
612 #endif
613
614         if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
615                 return MPC85XX_CPU_BOARD_REV(0, 0);
616
617         if ((be.major == 0xff) && (be.minor == 0xff))
618                 return MPC85XX_CPU_BOARD_REV(0, 0);
619
620         return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
621 }
622 #endif