sys_eeprom.c 8.4 KB

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