sys_eeprom.c 14 KB

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