sys_eeprom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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, i;
  178. void *p;
  179. #ifdef CONFIG_SYS_EEPROM_BUS_NUM
  180. unsigned int bus;
  181. #endif
  182. /* Set the reserved values to 0xFF */
  183. #ifdef CONFIG_SYS_I2C_EEPROM_NXID
  184. e.res_0 = 0xFF;
  185. memset(e.res_1, 0xFF, sizeof(e.res_1));
  186. #else
  187. memset(e.res_0, 0xFF, sizeof(e.res_0));
  188. #endif
  189. update_crc();
  190. #ifdef CONFIG_SYS_EEPROM_BUS_NUM
  191. bus = i2c_get_bus_num();
  192. i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
  193. #endif
  194. for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
  195. ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
  196. p, min((sizeof(e) - i), 8));
  197. if (ret)
  198. break;
  199. udelay(5000); /* 5ms write cycle timing */
  200. }
  201. #ifdef CONFIG_SYS_EEPROM_BUS_NUM
  202. i2c_set_bus_num(bus);
  203. #endif
  204. if (ret) {
  205. printf("Programming failed.\n");
  206. return -1;
  207. }
  208. printf("Programming passed.\n");
  209. return 0;
  210. }
  211. /**
  212. * h2i - converts hex character into a number
  213. *
  214. * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
  215. * the integer equivalent.
  216. */
  217. static inline u8 h2i(char p)
  218. {
  219. if ((p >= '0') && (p <= '9'))
  220. return p - '0';
  221. if ((p >= 'A') && (p <= 'F'))
  222. return (p - 'A') + 10;
  223. if ((p >= 'a') && (p <= 'f'))
  224. return (p - 'a') + 10;
  225. return 0;
  226. }
  227. /**
  228. * set_date - stores the build date into the EEPROM
  229. *
  230. * This function takes a pointer to a string in the format "YYMMDDhhmmss"
  231. * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
  232. * and stores it in the build date field of the EEPROM local copy.
  233. */
  234. static void set_date(const char *string)
  235. {
  236. unsigned int i;
  237. if (strlen(string) != 12) {
  238. printf("Usage: mac date YYMMDDhhmmss\n");
  239. return;
  240. }
  241. for (i = 0; i < 6; i++)
  242. e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
  243. update_crc();
  244. }
  245. /**
  246. * set_mac_address - stores a MAC address into the EEPROM
  247. *
  248. * This function takes a pointer to MAC address string
  249. * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
  250. * stores it in one of the MAC address fields of the EEPROM local copy.
  251. */
  252. static void set_mac_address(unsigned int index, const char *string)
  253. {
  254. char *p = (char *) string;
  255. unsigned int i;
  256. if (!string) {
  257. printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
  258. return;
  259. }
  260. for (i = 0; *p && (i < 6); i++) {
  261. e.mac[index][i] = simple_strtoul(p, &p, 16);
  262. if (*p == ':')
  263. p++;
  264. }
  265. update_crc();
  266. }
  267. int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  268. {
  269. char cmd;
  270. if (argc == 1) {
  271. show_eeprom();
  272. return 0;
  273. }
  274. cmd = argv[1][0];
  275. if (cmd == 'r') {
  276. read_eeprom();
  277. return 0;
  278. }
  279. if (cmd == 'i') {
  280. #ifdef CONFIG_SYS_I2C_EEPROM_NXID
  281. memcpy(e.id, "NXID", sizeof(e.id));
  282. e.version = 0;
  283. #else
  284. memcpy(e.id, "CCID", sizeof(e.id));
  285. #endif
  286. return 0;
  287. }
  288. if (!is_valid) {
  289. printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
  290. return 0;
  291. }
  292. if (argc == 2) {
  293. switch (cmd) {
  294. case 's': /* save */
  295. prog_eeprom();
  296. break;
  297. default:
  298. cmd_usage(cmdtp);
  299. break;
  300. }
  301. return 0;
  302. }
  303. /* We know we have at least one parameter */
  304. switch (cmd) {
  305. case 'n': /* serial number */
  306. memset(e.sn, 0, sizeof(e.sn));
  307. strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
  308. update_crc();
  309. break;
  310. case 'e': /* errata */
  311. #ifdef CONFIG_SYS_I2C_EEPROM_NXID
  312. memset(e.errata, 0, 5);
  313. strncpy((char *)e.errata, argv[2], 4);
  314. #else
  315. e.errata[0] = argv[2][0];
  316. e.errata[1] = argv[2][1];
  317. #endif
  318. update_crc();
  319. break;
  320. case 'd': /* date BCD format YYMMDDhhmmss */
  321. set_date(argv[2]);
  322. break;
  323. case 'p': /* MAC table size */
  324. e.mac_count = simple_strtoul(argv[2], NULL, 16);
  325. update_crc();
  326. break;
  327. case '0' ... '7': /* "mac 0" through "mac 7" */
  328. set_mac_address(cmd - '0', argv[2]);
  329. break;
  330. case 'h': /* help */
  331. default:
  332. cmd_usage(cmdtp);
  333. break;
  334. }
  335. return 0;
  336. }
  337. /**
  338. * mac_read_from_eeprom - read the MAC addresses from EEPROM
  339. *
  340. * This function reads the MAC addresses from EEPROM and sets the
  341. * appropriate environment variables for each one read.
  342. *
  343. * The environment variables are only set if they haven't been set already.
  344. * This ensures that any user-saved variables are never overwritten.
  345. *
  346. * This function must be called after relocation.
  347. */
  348. int mac_read_from_eeprom(void)
  349. {
  350. unsigned int i;
  351. u32 crc;
  352. puts("EEPROM: ");
  353. if (read_eeprom()) {
  354. printf("Read failed.\n");
  355. return -1;
  356. }
  357. if (!is_valid) {
  358. printf("Invalid ID (%02x %02x %02x %02x)\n",
  359. e.id[0], e.id[1], e.id[2], e.id[3]);
  360. return -1;
  361. }
  362. crc = crc32(0, (void *)&e, sizeof(e) - 4);
  363. if (crc != be32_to_cpu(e.crc)) {
  364. printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
  365. return -1;
  366. }
  367. for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
  368. if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
  369. memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
  370. char ethaddr[18];
  371. char enetvar[9];
  372. sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  373. e.mac[i][0],
  374. e.mac[i][1],
  375. e.mac[i][2],
  376. e.mac[i][3],
  377. e.mac[i][4],
  378. e.mac[i][5]);
  379. sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
  380. /* Only initialize environment variables that are blank
  381. * (i.e. have not yet been set)
  382. */
  383. if (!getenv(enetvar))
  384. setenv(enetvar, ethaddr);
  385. }
  386. }
  387. #ifdef CONFIG_SYS_I2C_EEPROM_NXID
  388. printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
  389. be32_to_cpu(e.version));
  390. #else
  391. printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
  392. #endif
  393. return 0;
  394. }
  395. #ifdef CONFIG_SYS_I2C_EEPROM_CCID
  396. /**
  397. * get_cpu_board_revision - get the CPU board revision on 85xx boards
  398. *
  399. * Read the EEPROM to determine the board revision.
  400. *
  401. * This function is called before relocation, so we need to read a private
  402. * copy of the EEPROM into a local variable on the stack.
  403. *
  404. * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
  405. * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
  406. * so that the SPD code will work. This means that all pre-relocation I2C
  407. * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
  408. * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
  409. * this function is called. Oh well.
  410. */
  411. unsigned int get_cpu_board_revision(void)
  412. {
  413. struct board_eeprom {
  414. u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
  415. u8 major; /* 0x04 Board revision, major */
  416. u8 minor; /* 0x05 Board revision, minor */
  417. } be;
  418. i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
  419. (void *)&be, sizeof(be));
  420. if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
  421. return MPC85XX_CPU_BOARD_REV(0, 0);
  422. if ((be.major == 0xff) && (be.minor == 0xff))
  423. return MPC85XX_CPU_BOARD_REV(0, 0);
  424. return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
  425. }
  426. #endif