sys_eeprom.c 12 KB

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