sys_eeprom.c 13 KB

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