hifive-platform-i2c-eeprom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 SiFive, Inc.
  4. *
  5. * Based on board/freescale/common/sys_eeprom.c:
  6. * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
  7. * York Sun (yorksun@freescale.com)
  8. * Haiying Wang (haiying.wang@freescale.com)
  9. * Timur Tabi (timur@freescale.com)
  10. */
  11. #include <common.h>
  12. #include <command.h>
  13. #include <env.h>
  14. #include <i2c.h>
  15. #include <init.h>
  16. #include <linux/ctype.h>
  17. #include <linux/delay.h>
  18. #include <u-boot/crc.h>
  19. #ifndef CONFIG_SYS_EEPROM_BUS_NUM
  20. #error Requires CONFIG_SYS_EEPROM_BUS_NUM to be defined
  21. #endif
  22. #define FORMAT_VERSION 0x1
  23. /* Options for the manuf_test_status field */
  24. #define SIFIVE_MANUF_TEST_STATUS_UNKNOWN 0
  25. #define SIFIVE_MANUF_TEST_STATUS_PASS 1
  26. #define SIFIVE_MANUF_TEST_STATUS_FAIL 2
  27. /*
  28. * BYTES_PER_EEPROM_PAGE: the AT24C02 datasheet says that data can
  29. * only be written in page mode, which means 8 bytes at a time
  30. */
  31. #define BYTES_PER_EEPROM_PAGE 8
  32. /*
  33. * EEPROM_WRITE_DELAY_MS: the AT24C02 datasheet says it takes up to
  34. * 5ms to complete a given write
  35. */
  36. #define EEPROM_WRITE_DELAY_MS 5000
  37. /*
  38. * MAGIC_NUMBER_BYTES: number of bytes used by the magic number
  39. */
  40. #define MAGIC_NUMBER_BYTES 4
  41. /*
  42. * SERIAL_NUMBER_BYTES: number of bytes used by the board serial
  43. * number
  44. */
  45. #define SERIAL_NUMBER_BYTES 16
  46. /*
  47. * MAC_ADDR_BYTES: number of bytes used by the Ethernet MAC address
  48. */
  49. #define MAC_ADDR_BYTES 6
  50. /*
  51. * MAC_ADDR_STRLEN: length of mac address string
  52. */
  53. #define MAC_ADDR_STRLEN 17
  54. /*
  55. * SiFive OUI. Registration Date is 2018-02-15
  56. */
  57. #define SIFIVE_OUI_PREFIX "70:B3:D5:92:F"
  58. /**
  59. * static eeprom: EEPROM layout for the SiFive platform I2C format
  60. */
  61. static struct __attribute__ ((__packed__)) sifive_eeprom {
  62. u8 magic[MAGIC_NUMBER_BYTES];
  63. u8 format_ver;
  64. u16 product_id;
  65. u8 pcb_revision;
  66. u8 bom_revision;
  67. u8 bom_variant;
  68. u8 serial[SERIAL_NUMBER_BYTES];
  69. u8 manuf_test_status;
  70. u8 mac_addr[MAC_ADDR_BYTES];
  71. u32 crc;
  72. } e;
  73. struct sifive_product {
  74. u16 id;
  75. const char *name;
  76. };
  77. /* Set to 1 if we've read EEPROM into memory */
  78. static int has_been_read;
  79. /* Magic number at the first four bytes of EEPROM */
  80. static const unsigned char magic[MAGIC_NUMBER_BYTES] = { 0xf1, 0x5e, 0x50, 0x45 };
  81. /* Does the magic number match that of a SiFive EEPROM? */
  82. static inline int is_match_magic(void)
  83. {
  84. return (memcmp(&e.magic, &magic, MAGIC_NUMBER_BYTES) == 0);
  85. }
  86. /* Calculate the current CRC */
  87. static inline u32 calculate_crc32(void)
  88. {
  89. return crc32(0, (void *)&e, sizeof(struct sifive_eeprom) - sizeof(e.crc));
  90. }
  91. /* This function should be called after each update to the EEPROM structure */
  92. static inline void update_crc(void)
  93. {
  94. e.crc = calculate_crc32();
  95. }
  96. static struct sifive_product sifive_products[] = {
  97. { 0, "Unknown"},
  98. { 2, "HiFive Unmatched" },
  99. };
  100. /**
  101. * dump_raw_eeprom - display the raw contents of the EEPROM
  102. */
  103. static void dump_raw_eeprom(void)
  104. {
  105. unsigned int i;
  106. printf("EEPROM dump: (0x%lx bytes)\n", sizeof(e));
  107. for (i = 0; i < sizeof(e); i++) {
  108. if ((i % 16) == 0)
  109. printf("%02X: ", i);
  110. printf("%02X ", ((u8 *)&e)[i]);
  111. if (((i % 16) == 15) || (i == sizeof(e) - 1))
  112. printf("\n");
  113. }
  114. }
  115. /**
  116. * show_eeprom - display the contents of the EEPROM
  117. */
  118. static void show_eeprom(void)
  119. {
  120. unsigned int i;
  121. u32 crc;
  122. const char *product_name = "Unknown";
  123. char board_serial[SERIAL_NUMBER_BYTES + 1] = { 0 };
  124. if (!is_match_magic()) {
  125. printf("Not a SiFive HiFive EEPROM data format - magic bytes don't match\n");
  126. dump_raw_eeprom();
  127. return;
  128. };
  129. snprintf(board_serial, sizeof(board_serial), "%s", e.serial);
  130. for (i = 0; i < ARRAY_SIZE(sifive_products); i++) {
  131. if (sifive_products[i].id == e.product_id) {
  132. product_name = sifive_products[i].name;
  133. break;
  134. }
  135. };
  136. printf("SiFive PCB EEPROM format v%u\n", e.format_ver);
  137. printf("Product ID: %04hx (%s)\n", e.product_id, product_name);
  138. printf("PCB revision: %x\n", e.pcb_revision);
  139. printf("BOM revision: %c\n", e.bom_revision);
  140. printf("BOM variant: %x\n", e.bom_variant);
  141. printf("Serial number: %s\n", board_serial);
  142. printf("Ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  143. e.mac_addr[0], e.mac_addr[1], e.mac_addr[2],
  144. e.mac_addr[3], e.mac_addr[4], e.mac_addr[5]);
  145. crc = calculate_crc32();
  146. if (crc == e.crc) {
  147. printf("CRC: %08x\n", e.crc);
  148. } else {
  149. printf("CRC: %08x (should be %08x)\n", e.crc, crc);
  150. dump_raw_eeprom();
  151. }
  152. }
  153. /**
  154. * read_eeprom() - read the EEPROM into memory, if it hasn't been read already
  155. */
  156. static int read_eeprom(void)
  157. {
  158. int ret;
  159. struct udevice *dev;
  160. if (has_been_read)
  161. return 0;
  162. ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
  163. CONFIG_SYS_I2C_EEPROM_ADDR,
  164. 1,
  165. &dev);
  166. if (!ret)
  167. dm_i2c_read(dev, 0, (void *)&e,
  168. sizeof(struct sifive_eeprom));
  169. show_eeprom();
  170. has_been_read = (ret == 0) ? 1 : 0;
  171. return ret;
  172. }
  173. /**
  174. * prog_eeprom() - write the EEPROM from memory
  175. */
  176. static int prog_eeprom(void)
  177. {
  178. int ret = 0;
  179. unsigned int i;
  180. void *p;
  181. if (!is_match_magic()) {
  182. printf("Please read the EEPROM ('read_eeprom') and/or initialize the EEPROM ('initialize') first.\n");
  183. return 0;
  184. }
  185. for (i = 0, p = &e; i < sizeof(e);
  186. i += BYTES_PER_EEPROM_PAGE, p += BYTES_PER_EEPROM_PAGE) {
  187. struct udevice *dev;
  188. ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
  189. CONFIG_SYS_I2C_EEPROM_ADDR,
  190. CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
  191. &dev);
  192. if (!ret)
  193. ret = dm_i2c_write(dev, i, p,
  194. min((int)(sizeof(e) - i),
  195. BYTES_PER_EEPROM_PAGE));
  196. if (ret)
  197. break;
  198. udelay(EEPROM_WRITE_DELAY_MS);
  199. }
  200. if (!ret) {
  201. /* Verify the write by reading back the EEPROM and comparing */
  202. struct sifive_eeprom e2;
  203. struct udevice *dev;
  204. ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
  205. CONFIG_SYS_I2C_EEPROM_ADDR,
  206. CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
  207. &dev);
  208. if (!ret)
  209. ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2));
  210. if (!ret && memcmp(&e, &e2, sizeof(e)))
  211. ret = -1;
  212. }
  213. if (ret) {
  214. printf("Programming failed.\n");
  215. has_been_read = 0;
  216. return -1;
  217. }
  218. printf("Programming passed.\n");
  219. return 0;
  220. }
  221. /**
  222. * set_mac_address() - stores a MAC address into the local EEPROM copy
  223. *
  224. * This function takes a pointer to MAC address string
  225. * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number),
  226. * stores it in the MAC address field of the EEPROM local copy, and
  227. * updates the local copy of the CRC.
  228. */
  229. static void set_mac_address(char *string)
  230. {
  231. unsigned int i;
  232. if (strncasecmp(SIFIVE_OUI_PREFIX, string, 13)) {
  233. printf("The MAC address doesn't match SiFive OUI %s\n",
  234. SIFIVE_OUI_PREFIX);
  235. return;
  236. }
  237. for (i = 0; *string && (i < MAC_ADDR_BYTES); i++) {
  238. e.mac_addr[i] = hextoul(string, &string);
  239. if (*string == ':')
  240. string++;
  241. }
  242. update_crc();
  243. }
  244. /**
  245. * set_manuf_test_status() - stores a test status byte into the in-memory copy
  246. *
  247. * Takes a pointer to a manufacturing test status string ("unknown",
  248. * "pass", "fail") and stores the corresponding numeric ID to the
  249. * manuf_test_status field of the EEPROM local copy, and updates the
  250. * CRC of the local copy.
  251. */
  252. static void set_manuf_test_status(char *string)
  253. {
  254. if (!strcasecmp(string, "unknown")) {
  255. e.manuf_test_status = SIFIVE_MANUF_TEST_STATUS_UNKNOWN;
  256. } else if (!strcasecmp(string, "pass")) {
  257. e.manuf_test_status = SIFIVE_MANUF_TEST_STATUS_PASS;
  258. } else if (!strcasecmp(string, "fail")) {
  259. e.manuf_test_status = SIFIVE_MANUF_TEST_STATUS_FAIL;
  260. } else {
  261. printf("Usage: mac manuf_test_status (unknown|pass|fail)\n");
  262. return;
  263. }
  264. update_crc();
  265. }
  266. /**
  267. * set_pcb_revision() - stores a SiFive PCB revision into the local EEPROM copy
  268. *
  269. * Takes a pointer to a string representing the numeric PCB revision in
  270. * decimal ("0" - "255"), stores it in the pcb_revision field of the
  271. * EEPROM local copy, and updates the CRC of the local copy.
  272. */
  273. static void set_pcb_revision(char *string)
  274. {
  275. unsigned long p;
  276. p = dectoul(string, &string);
  277. if (p > U8_MAX) {
  278. printf("%s must not be greater than %d\n", "PCB revision",
  279. U8_MAX);
  280. return;
  281. }
  282. e.pcb_revision = p;
  283. update_crc();
  284. }
  285. /**
  286. * set_bom_revision() - stores a SiFive BOM revision into the local EEPROM copy
  287. *
  288. * Takes a pointer to a uppercase ASCII character representing the BOM
  289. * revision ("A" - "Z"), stores it in the bom_revision field of the
  290. * EEPROM local copy, and updates the CRC of the local copy.
  291. */
  292. static void set_bom_revision(char *string)
  293. {
  294. if (string[0] < 'A' || string[0] > 'Z') {
  295. printf("BOM revision must be an uppercase letter between A and Z\n");
  296. return;
  297. }
  298. e.bom_revision = string[0];
  299. update_crc();
  300. }
  301. /**
  302. * set_bom_variant() - stores a SiFive BOM variant into the local EEPROM copy
  303. *
  304. * Takes a pointer to a string representing the numeric BOM variant in
  305. * decimal ("0" - "255"), stores it in the bom_variant field of the
  306. * EEPROM local copy, and updates the CRC of the local copy.
  307. */
  308. static void set_bom_variant(char *string)
  309. {
  310. unsigned long p;
  311. p = dectoul(string, &string);
  312. if (p > U8_MAX) {
  313. printf("%s must not be greater than %d\n", "BOM variant",
  314. U8_MAX);
  315. return;
  316. }
  317. e.bom_variant = p;
  318. update_crc();
  319. }
  320. /**
  321. * set_product_id() - stores a SiFive product ID into the local EEPROM copy
  322. *
  323. * Takes a pointer to a string representing the numeric product ID in
  324. * decimal ("0" - "65535"), stores it in the product ID field of the
  325. * EEPROM local copy, and updates the CRC of the local copy.
  326. */
  327. static void set_product_id(char *string)
  328. {
  329. unsigned long p;
  330. p = dectoul(string, &string);
  331. if (p > U16_MAX) {
  332. printf("%s must not be greater than %d\n", "Product ID",
  333. U16_MAX);
  334. return;
  335. }
  336. e.product_id = p;
  337. update_crc();
  338. }
  339. /**
  340. * init_local_copy() - initialize the in-memory EEPROM copy
  341. *
  342. * Initialize the in-memory EEPROM copy with the magic number. Must
  343. * be done when preparing to initialize a blank EEPROM, or overwrite
  344. * one with a corrupted magic number.
  345. */
  346. static void init_local_copy(void)
  347. {
  348. memset(&e, 0, sizeof(e));
  349. memcpy(e.magic, magic, sizeof(e.magic));
  350. e.format_ver = FORMAT_VERSION;
  351. update_crc();
  352. }
  353. int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  354. {
  355. char *cmd;
  356. if (argc == 1) {
  357. show_eeprom();
  358. return 0;
  359. }
  360. if (argc > 3)
  361. return cmd_usage(cmdtp);
  362. cmd = argv[1];
  363. /* Commands with no argument */
  364. if (!strcmp(cmd, "read_eeprom")) {
  365. read_eeprom();
  366. return 0;
  367. } else if (!strcmp(cmd, "initialize")) {
  368. init_local_copy();
  369. return 0;
  370. } else if (!strcmp(cmd, "write_eeprom")) {
  371. prog_eeprom();
  372. return 0;
  373. }
  374. if (argc != 3)
  375. return cmd_usage(cmdtp);
  376. if (!is_match_magic()) {
  377. printf("Please read the EEPROM ('read_eeprom') and/or initialize the EEPROM ('initialize') first.\n");
  378. return 0;
  379. }
  380. if (!strcmp(cmd, "manuf_test_status")) {
  381. set_manuf_test_status(argv[2]);
  382. return 0;
  383. } else if (!strcmp(cmd, "mac_address")) {
  384. set_mac_address(argv[2]);
  385. return 0;
  386. } else if (!strcmp(cmd, "pcb_revision")) {
  387. set_pcb_revision(argv[2]);
  388. return 0;
  389. } else if (!strcmp(cmd, "bom_variant")) {
  390. set_bom_variant(argv[2]);
  391. return 0;
  392. } else if (!strcmp(cmd, "bom_revision")) {
  393. set_bom_revision(argv[2]);
  394. return 0;
  395. } else if (!strcmp(cmd, "product_id")) {
  396. set_product_id(argv[2]);
  397. return 0;
  398. }
  399. return cmd_usage(cmdtp);
  400. }
  401. /**
  402. * mac_read_from_eeprom() - read the MAC address from EEPROM
  403. *
  404. * This function reads the MAC address from EEPROM and sets the
  405. * appropriate environment variables for each one read.
  406. *
  407. * The environment variables are only set if they haven't been set already.
  408. * This ensures that any user-saved variables are never overwritten.
  409. *
  410. * This function must be called after relocation.
  411. */
  412. int mac_read_from_eeprom(void)
  413. {
  414. u32 crc;
  415. char board_serial[SERIAL_NUMBER_BYTES + 1] = { 0 };
  416. puts("EEPROM: ");
  417. if (read_eeprom()) {
  418. printf("Read failed.\n");
  419. return 0;
  420. }
  421. if (!is_match_magic()) {
  422. printf("Invalid ID (%02x %02x %02x %02x)\n",
  423. e.magic[0], e.magic[1], e.magic[2], e.magic[3]);
  424. dump_raw_eeprom();
  425. return 0;
  426. }
  427. crc = calculate_crc32();
  428. if (crc != e.crc) {
  429. printf("CRC mismatch (%08x != %08x)\n", crc, e.crc);
  430. dump_raw_eeprom();
  431. return 0;
  432. }
  433. eth_env_set_enetaddr("ethaddr", e.mac_addr);
  434. if (!env_get("serial#")) {
  435. snprintf(board_serial, sizeof(board_serial), "%s", e.serial);
  436. env_set("serial#", board_serial);
  437. }
  438. return 0;
  439. }
  440. /**
  441. * get_pcb_revision_from_eeprom - get the PCB revision
  442. *
  443. * Read the EEPROM to determine the board revision.
  444. *
  445. * This function is called before relocation, so we need to read a private
  446. * copy of the EEPROM into a local variable on the stack.
  447. */
  448. u8 get_pcb_revision_from_eeprom(void)
  449. {
  450. struct __attribute__ ((__packed__)) board_eeprom {
  451. u8 magic[MAGIC_NUMBER_BYTES];
  452. u8 format_ver;
  453. u16 product_id;
  454. u8 pcb_revision;
  455. } be;
  456. int ret;
  457. struct udevice *dev;
  458. ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
  459. CONFIG_SYS_I2C_EEPROM_ADDR,
  460. 1,
  461. &dev);
  462. if (!ret)
  463. dm_i2c_read(dev, 0, (void *)&be,
  464. sizeof(struct board_eeprom));
  465. return be.pcb_revision;
  466. }