sys_eeprom.c 11 KB

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