sys_eeprom.c 13 KB

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