sys_eeprom.c 13 KB

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