sys_eeprom.c 12 KB

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