sys_eeprom.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright 2006, 2008-2009, 2011 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. /* #define DEBUG */
  29. /*
  30. * static eeprom: EEPROM layout
  31. */
  32. static struct __attribute__ ((__packed__)) eeprom {
  33. u8 id[16]; /* 0x01 - 0x0F Type e.g. 100wG-5111 */
  34. u8 sn[10]; /* 0x10 - 0x19 Serial Number */
  35. u8 date[6]; /* 0x1A - 0x1F Build Date */
  36. u8 mac[6]; /* 0x20 - 0x25 MAC address */
  37. u8 reserved[10];/* 0x26 - 0x2f reserved */
  38. u32 crc; /* x+1 CRC32 checksum */
  39. } e;
  40. /* Set to 1 if we've read EEPROM into memory */
  41. static int has_been_read;
  42. /**
  43. * show_eeprom - display the contents of the EEPROM
  44. */
  45. static void show_eeprom(void)
  46. {
  47. unsigned int crc;
  48. char safe_string[16];
  49. #ifdef DEBUG
  50. int i;
  51. #endif
  52. u8 *p;
  53. /* ID */
  54. strncpy(safe_string, (char *)e.id, sizeof(e.id));
  55. safe_string[sizeof(e.id)-1] = 0;
  56. printf("ID: mvBlueLYNX-X%s\n", safe_string);
  57. /* Serial number */
  58. strncpy(safe_string, (char *)e.sn, sizeof(e.sn));
  59. safe_string[sizeof(e.sn)-1] = 0;
  60. printf("SN: %s\n", safe_string);
  61. /* Build date, BCD date values, as YYMMDDhhmmss */
  62. printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
  63. e.date[0], e.date[1], e.date[2],
  64. e.date[3] & 0x7F, e.date[4], e.date[5],
  65. e.date[3] & 0x80 ? "PM" : "");
  66. /* Show MAC address */
  67. p = e.mac;
  68. printf("Eth: %02x:%02x:%02x:%02x:%02x:%02x\n",
  69. p[0], p[1], p[2], p[3], p[4], p[5]);
  70. crc = crc32(0, (void *)&e, sizeof(e) - 4);
  71. if (crc == be32_to_cpu(e.crc))
  72. printf("CRC: %08x\n", be32_to_cpu(e.crc));
  73. else
  74. printf("CRC: %08x (should be %08x)\n", be32_to_cpu(e.crc), crc);
  75. #ifdef DEBUG
  76. printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
  77. for (i = 0; i < sizeof(e); i++) {
  78. if ((i % 16) == 0)
  79. printf("%02X: ", i);
  80. printf("%02X ", ((u8 *)&e)[i]);
  81. if (((i % 16) == 15) || (i == sizeof(e) - 1))
  82. printf("\n");
  83. }
  84. #endif
  85. }
  86. /**
  87. * read_eeprom - read the EEPROM into memory
  88. */
  89. static int read_eeprom(void)
  90. {
  91. int ret;
  92. #ifdef CONFIG_SYS_EEPROM_BUS_NUM
  93. unsigned int bus;
  94. #endif
  95. if (has_been_read)
  96. return 0;
  97. #ifdef CONFIG_SYS_EEPROM_BUS_NUM
  98. bus = i2c_get_bus_num();
  99. i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
  100. #endif
  101. ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
  102. (uchar *)&e, sizeof(e));
  103. #ifdef CONFIG_SYS_EEPROM_BUS_NUM
  104. i2c_set_bus_num(bus);
  105. #endif
  106. #ifdef DEBUG
  107. show_eeprom();
  108. #endif
  109. has_been_read = (ret == 0) ? 1 : 0;
  110. return ret;
  111. }
  112. /**
  113. * update_crc - update the CRC
  114. *
  115. * This function should be called after each update to the EEPROM structure,
  116. * to make sure the CRC is always correct.
  117. */
  118. static void update_crc(void)
  119. {
  120. u32 crc;
  121. crc = crc32(0, (void *)&e, sizeof(e) - 4);
  122. e.crc = cpu_to_be32(crc);
  123. }
  124. /**
  125. * prog_eeprom - write the EEPROM from memory
  126. */
  127. static int prog_eeprom(void)
  128. {
  129. int ret = 0;
  130. #ifdef CONFIG_SYS_EEPROM_BUS_NUM
  131. unsigned int bus;
  132. #endif
  133. update_crc();
  134. #ifdef CONFIG_SYS_EEPROM_BUS_NUM
  135. bus = i2c_get_bus_num();
  136. i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
  137. #endif
  138. ret = eeprom_write(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
  139. (uchar *)&e, sizeof(e));
  140. if (!ret) {
  141. /* Verify the write by reading back the EEPROM and comparing */
  142. struct eeprom e2;
  143. #ifdef DEBUG
  144. printf("%s verifying...\n", __func__);
  145. #endif
  146. ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
  147. (uchar *)&e2, sizeof(e2));
  148. if (!ret && memcmp(&e, &e2, sizeof(e)))
  149. ret = -1;
  150. }
  151. #ifdef CONFIG_SYS_EEPROM_BUS_NUM
  152. i2c_set_bus_num(bus);
  153. #endif
  154. if (ret) {
  155. printf("Programming failed.\n");
  156. has_been_read = 0;
  157. return -1;
  158. }
  159. printf("Programming passed.\n");
  160. return 0;
  161. }
  162. /**
  163. * h2i - converts hex character into a number
  164. *
  165. * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
  166. * the integer equivalent.
  167. */
  168. static inline u8 h2i(char p)
  169. {
  170. if ((p >= '0') && (p <= '9'))
  171. return p - '0';
  172. if ((p >= 'A') && (p <= 'F'))
  173. return (p - 'A') + 10;
  174. if ((p >= 'a') && (p <= 'f'))
  175. return (p - 'a') + 10;
  176. return 0;
  177. }
  178. /**
  179. * set_date - stores the build date into the EEPROM
  180. *
  181. * This function takes a pointer to a string in the format "YYMMDDhhmmss"
  182. * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
  183. * and stores it in the build date field of the EEPROM local copy.
  184. */
  185. static void set_date(const char *string)
  186. {
  187. unsigned int i;
  188. if (strlen(string) != 12) {
  189. printf("Usage: mac date YYMMDDhhmmss\n");
  190. return;
  191. }
  192. for (i = 0; i < 6; i++)
  193. e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
  194. update_crc();
  195. }
  196. /**
  197. * set_mac_address - stores a MAC address into the EEPROM
  198. *
  199. * This function takes a pointer to MAC address string
  200. * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
  201. * stores it in the MAC address field in the EEPROM local copy.
  202. */
  203. static void set_mac_address(const char *string)
  204. {
  205. char *p = (char *) string;
  206. unsigned int i;
  207. for (i = 0; *p && (i < 6); i++) {
  208. e.mac[i] = simple_strtoul(p, &p, 16);
  209. if (*p == ':')
  210. p++;
  211. }
  212. update_crc();
  213. }
  214. int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  215. {
  216. char cmd;
  217. if (argc == 1) {
  218. show_eeprom();
  219. return 0;
  220. }
  221. cmd = argv[1][0];
  222. if (cmd == 'r') {
  223. #ifdef DEBUG
  224. printf("%s read\n", __func__);
  225. #endif
  226. read_eeprom();
  227. return 0;
  228. }
  229. if (argc == 2) {
  230. switch (cmd) {
  231. case 's': /* save */
  232. #ifdef DEBUG
  233. printf("%s save\n", __func__);
  234. #endif
  235. prog_eeprom();
  236. break;
  237. default:
  238. return cmd_usage(cmdtp);
  239. }
  240. return 0;
  241. }
  242. /* We know we have at least one parameter */
  243. switch (cmd) {
  244. case 'n': /* serial number */
  245. #ifdef DEBUG
  246. printf("%s serial number\n", __func__);
  247. #endif
  248. memset(e.sn, 0, sizeof(e.sn));
  249. strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
  250. update_crc();
  251. break;
  252. case 'd': /* date BCD format YYMMDDhhmmss */
  253. set_date(argv[2]);
  254. break;
  255. case 'e': /* errata */
  256. printf("mac errata not implemented\n");
  257. break;
  258. case 'i': /* id */
  259. memset(e.id, 0, sizeof(e.id));
  260. strncpy((char *)e.id, argv[2], sizeof(e.id) - 1);
  261. update_crc();
  262. break;
  263. case 'p': /* ports */
  264. printf("mac ports not implemented (always 1 port)\n");
  265. break;
  266. case '0' ... '9':
  267. /* we only have "mac 0" but any digit can be used here */
  268. set_mac_address(argv[2]);
  269. break;
  270. case 'h': /* help */
  271. default:
  272. return cmd_usage(cmdtp);
  273. }
  274. return 0;
  275. }
  276. static inline int is_portrait(void)
  277. {
  278. int i;
  279. unsigned int orient_index = 0; /* idx of char which determines orientation */
  280. for (i = sizeof(e.id)/sizeof(*e.id) - 1; i>=0; i--) {
  281. if (e.id[i] == '-') {
  282. orient_index = i+1;
  283. break;
  284. }
  285. }
  286. return (orient_index &&
  287. (e.id[orient_index] >= '5') && (e.id[orient_index] <= '8'));
  288. }
  289. int mac_read_from_eeprom(void)
  290. {
  291. u32 crc, crc_offset = offsetof(struct eeprom, crc);
  292. u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
  293. #define FILENAME_LANDSCAPE "mvBlueLynx_X.rbf"
  294. #define FILENAME_PORTRAIT "mvBlueLynx_X_sensor_cd.rbf"
  295. if (read_eeprom()) {
  296. printf("EEPROM Read failed.\n");
  297. return -1;
  298. }
  299. crc = crc32(0, (void *)&e, crc_offset);
  300. crcp = (void *)&e + crc_offset;
  301. if (crc != be32_to_cpu(*crcp)) {
  302. printf("EEPROM CRC mismatch (%08x != %08x)\n", crc,
  303. be32_to_cpu(e.crc));
  304. return -1;
  305. }
  306. if (memcmp(&e.mac, "\0\0\0\0\0\0", 6) &&
  307. memcmp(&e.mac, "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
  308. char ethaddr[9];
  309. sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  310. e.mac[0],
  311. e.mac[1],
  312. e.mac[2],
  313. e.mac[3],
  314. e.mac[4],
  315. e.mac[5]);
  316. /* Only initialize environment variables that are blank
  317. * (i.e. have not yet been set)
  318. */
  319. if (!getenv("ethaddr"))
  320. setenv("ethaddr", ethaddr);
  321. }
  322. if (memcmp(&e.sn, "\0\0\0\0\0\0\0\0\0\0", 10) &&
  323. memcmp(&e.sn, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10)) {
  324. char serial_num[12];
  325. strncpy(serial_num, (char *)e.sn, sizeof(e.sn) - 1);
  326. /* Only initialize environment variables that are blank
  327. * (i.e. have not yet been set)
  328. */
  329. if (!getenv("serial#"))
  330. setenv("serial#", serial_num);
  331. }
  332. /* decide which fpga file to load depending on orientation */
  333. if (is_portrait())
  334. setenv("fpgafilename", FILENAME_PORTRAIT);
  335. else
  336. setenv("fpgafilename", FILENAME_LANDSCAPE);
  337. /* TODO should I calculate CRC here? */
  338. return 0;
  339. }
  340. #ifdef CONFIG_SERIAL_TAG
  341. void get_board_serial(struct tag_serialnr *serialnr)
  342. {
  343. char *serial = getenv("serial#");
  344. if (serial && (strlen(serial) > 3)) {
  345. /* use the numerical part of the serial number LXnnnnnn */
  346. serialnr->high = 0;
  347. serialnr->low = simple_strtoul(serial + 2, NULL, 10);
  348. } else {
  349. serialnr->high = 0;
  350. serialnr->low = 0;
  351. }
  352. }
  353. #endif