sys_eeprom.c 13 KB

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