sys_eeprom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Based on board/freescale/common/sys_eeprom.c
  4. * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
  5. *
  6. * This defines the API for storing board information in the
  7. * eeprom. It has been adapted from an earlier version of the
  8. * Freescale API, but has a number of key differences. Because
  9. * the two APIs are independent and may diverge further, the
  10. * Varisys version of the API is implemented separately here.
  11. */
  12. #include <common.h>
  13. #include <command.h>
  14. #include <i2c.h>
  15. #include <linux/ctype.h>
  16. #include "eeprom.h"
  17. #ifdef CONFIG_SYS_I2C_EEPROM_NXID_MAC
  18. #define MAX_NUM_PORTS CONFIG_SYS_I2C_EEPROM_NXID_MAC
  19. #else
  20. #define MAX_NUM_PORTS 8
  21. #endif
  22. #define NXID_VERSION 0
  23. /**
  24. * static eeprom: EEPROM layout for NXID formats
  25. *
  26. * See Freescale application note AN3638 for details.
  27. */
  28. static struct __attribute__ ((__packed__)) eeprom {
  29. u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
  30. u8 sn[12]; /* 0x04 - 0x0F Serial Number */
  31. u8 errata[5]; /* 0x10 - 0x14 Errata Level */
  32. u8 date[6]; /* 0x15 - 0x1a Build Date */
  33. u8 res_0; /* 0x1b Reserved */
  34. u32 version; /* 0x1c - 0x1f NXID Version */
  35. u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
  36. u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
  37. u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
  38. u8 res_1[21]; /* 0x2b - 0x3f Reserved */
  39. u8 mac_count; /* 0x40 Number of MAC addresses */
  40. u8 mac_flag; /* 0x41 MAC table flags */
  41. u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */
  42. u32 crc; /* x+1 CRC32 checksum */
  43. } e;
  44. /* Set to 1 if we've read EEPROM into memory */
  45. static int has_been_read;
  46. /* Is this a valid NXID EEPROM? */
  47. #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
  48. (e.id[2] == 'I') || (e.id[3] == 'D'))
  49. /** Fixed ID field in EEPROM */
  50. static unsigned char uid[16];
  51. static int eeprom_bus_num = -1;
  52. static int eeprom_addr;
  53. static int eeprom_addr_len;
  54. /**
  55. * This must be called before any eeprom access.
  56. */
  57. void init_eeprom(int bus_num, int addr, int addr_len)
  58. {
  59. eeprom_bus_num = bus_num;
  60. eeprom_addr = addr;
  61. eeprom_addr_len = addr_len;
  62. }
  63. /**
  64. * show_eeprom - display the contents of the EEPROM
  65. */
  66. void show_eeprom(void)
  67. {
  68. int i;
  69. unsigned int crc;
  70. /* EEPROM tag ID, either CCID or NXID */
  71. printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
  72. be32_to_cpu(e.version));
  73. /* Serial number */
  74. printf("SN: %s\n", e.sn);
  75. printf("UID: ");
  76. for (i = 0; i < 16; i++)
  77. printf("%02x", uid[i]);
  78. printf("\n");
  79. /* Errata level. */
  80. printf("Errata: %s\n", e.errata);
  81. /* Build date, BCD date values, as YYMMDDhhmmss */
  82. printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
  83. e.date[0], e.date[1], e.date[2],
  84. e.date[3] & 0x7F, e.date[4], e.date[5],
  85. e.date[3] & 0x80 ? "PM" : "");
  86. /* Show MAC addresses */
  87. for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
  88. u8 *p = e.mac[i];
  89. printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
  90. p[0], p[1], p[2], p[3], p[4], p[5]);
  91. }
  92. crc = crc32(0, (void *)&e, sizeof(e) - 4);
  93. if (crc == be32_to_cpu(e.crc))
  94. printf("CRC: %08x\n", be32_to_cpu(e.crc));
  95. else
  96. printf("CRC: %08x (should be %08x)\n",
  97. be32_to_cpu(e.crc), crc);
  98. #ifdef DEBUG
  99. printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
  100. for (i = 0; i < sizeof(e); i++) {
  101. if ((i % 16) == 0)
  102. printf("%02X: ", i);
  103. printf("%02X ", ((u8 *)&e)[i]);
  104. if (((i % 16) == 15) || (i == sizeof(e) - 1))
  105. printf("\n");
  106. }
  107. #endif
  108. }
  109. /**
  110. * read_eeprom - read the EEPROM into memory
  111. */
  112. int read_eeprom(void)
  113. {
  114. int ret;
  115. unsigned int bus;
  116. if (eeprom_bus_num < 0) {
  117. printf("EEPROM not configured\n");
  118. return -1;
  119. }
  120. if (has_been_read)
  121. return 0;
  122. bus = i2c_get_bus_num();
  123. i2c_set_bus_num(eeprom_bus_num);
  124. ret = i2c_read(eeprom_addr, 0, eeprom_addr_len,
  125. (void *)&e, sizeof(e));
  126. /* Fixed address of ID field */
  127. i2c_read(0x5f, 0x80, 1, uid, 16);
  128. i2c_set_bus_num(bus);
  129. #ifdef DEBUG
  130. show_eeprom();
  131. #endif
  132. has_been_read = (ret == 0) ? 1 : 0;
  133. return ret;
  134. }
  135. /**
  136. * update_crc - update the CRC
  137. *
  138. * This function should be called after each update to the EEPROM structure,
  139. * to make sure the CRC is always correct.
  140. */
  141. static void update_crc(void)
  142. {
  143. u32 crc, crc_offset = offsetof(struct eeprom, crc);
  144. crc = crc32(0, (void *)&e, crc_offset);
  145. e.crc = cpu_to_be32(crc);
  146. }
  147. /**
  148. * prog_eeprom - write the EEPROM from memory
  149. */
  150. static int prog_eeprom(void)
  151. {
  152. int ret = 0;
  153. int i;
  154. void *p;
  155. unsigned int bus;
  156. if (eeprom_bus_num < 0) {
  157. printf("EEPROM not configured\n");
  158. return -1;
  159. }
  160. /* Set the reserved values to 0xFF */
  161. e.res_0 = 0xFF;
  162. memset(e.res_1, 0xFF, sizeof(e.res_1));
  163. update_crc();
  164. bus = i2c_get_bus_num();
  165. i2c_set_bus_num(eeprom_bus_num);
  166. /*
  167. * The AT24C02 datasheet says that data can only be written in page
  168. * mode, which means 8 bytes at a time, and it takes up to 5ms to
  169. * complete a given write.
  170. */
  171. for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
  172. ret = i2c_write(eeprom_addr, i, eeprom_addr_len,
  173. p, min((int)(sizeof(e) - i), 8));
  174. if (ret)
  175. break;
  176. udelay(5000); /* 5ms write cycle timing */
  177. }
  178. if (!ret) {
  179. /* Verify the write by reading back the EEPROM and comparing */
  180. struct eeprom e2;
  181. ret = i2c_read(eeprom_addr, 0,
  182. eeprom_addr_len, (void *)&e2, sizeof(e2));
  183. if (!ret && memcmp(&e, &e2, sizeof(e)))
  184. ret = -1;
  185. }
  186. i2c_set_bus_num(bus);
  187. if (ret) {
  188. printf("Programming failed.\n");
  189. has_been_read = 0;
  190. return -1;
  191. }
  192. printf("Programming passed.\n");
  193. return 0;
  194. }
  195. /**
  196. * h2i - converts hex character into a number
  197. *
  198. * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
  199. * the integer equivalent.
  200. */
  201. static inline u8 h2i(char p)
  202. {
  203. if ((p >= '0') && (p <= '9'))
  204. return p - '0';
  205. if ((p >= 'A') && (p <= 'F'))
  206. return (p - 'A') + 10;
  207. if ((p >= 'a') && (p <= 'f'))
  208. return (p - 'a') + 10;
  209. return 0;
  210. }
  211. /**
  212. * set_date - stores the build date into the EEPROM
  213. *
  214. * This function takes a pointer to a string in the format "YYMMDDhhmmss"
  215. * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
  216. * and stores it in the build date field of the EEPROM local copy.
  217. */
  218. static void set_date(const char *string)
  219. {
  220. unsigned int i;
  221. if (strlen(string) != 12) {
  222. printf("Usage: mac date YYMMDDhhmmss\n");
  223. return;
  224. }
  225. for (i = 0; i < 6; i++)
  226. e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
  227. update_crc();
  228. }
  229. /**
  230. * set_mac_address - stores a MAC address into the EEPROM
  231. *
  232. * This function takes a pointer to MAC address string
  233. * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
  234. * stores it in one of the MAC address fields of the EEPROM local copy.
  235. */
  236. static void set_mac_address(unsigned int index, const char *string)
  237. {
  238. char *p = (char *)string;
  239. unsigned int i;
  240. if ((index >= MAX_NUM_PORTS) || !string) {
  241. printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
  242. return;
  243. }
  244. for (i = 0; *p && (i < 6); i++) {
  245. e.mac[index][i] = simple_strtoul(p, &p, 16);
  246. if (*p == ':')
  247. p++;
  248. }
  249. update_crc();
  250. }
  251. int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  252. {
  253. char cmd;
  254. if (argc == 1) {
  255. show_eeprom();
  256. return 0;
  257. }
  258. cmd = argv[1][0];
  259. if (cmd == 'r') {
  260. read_eeprom();
  261. return 0;
  262. }
  263. if (cmd == 'i') {
  264. memcpy(e.id, "NXID", sizeof(e.id));
  265. e.version = NXID_VERSION;
  266. update_crc();
  267. return 0;
  268. }
  269. if (!is_valid) {
  270. printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
  271. return 0;
  272. }
  273. if (argc == 2) {
  274. switch (cmd) {
  275. case 's': /* save */
  276. prog_eeprom();
  277. break;
  278. default:
  279. return cmd_usage(cmdtp);
  280. }
  281. return 0;
  282. }
  283. /* We know we have at least one parameter */
  284. switch (cmd) {
  285. case 'n': /* serial number */
  286. memset(e.sn, 0, sizeof(e.sn));
  287. strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
  288. update_crc();
  289. break;
  290. case 'e': /* errata */
  291. memset(e.errata, 0, 5);
  292. strncpy((char *)e.errata, argv[2], 4);
  293. update_crc();
  294. break;
  295. case 'd': /* date BCD format YYMMDDhhmmss */
  296. set_date(argv[2]);
  297. break;
  298. case 'p': /* MAC table size */
  299. e.mac_count = simple_strtoul(argv[2], NULL, 16);
  300. update_crc();
  301. break;
  302. case '0' ... '9': /* "mac 0" through "mac 22" */
  303. set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
  304. break;
  305. case 'h': /* help */
  306. default:
  307. return cmd_usage(cmdtp);
  308. }
  309. return 0;
  310. }
  311. int mac_read_from_generic_eeprom(const char *envvar, int chip,
  312. int address, int mac_bus)
  313. {
  314. int ret;
  315. unsigned int bus;
  316. unsigned char mac[6];
  317. char ethaddr[18];
  318. bus = i2c_get_bus_num();
  319. i2c_set_bus_num(mac_bus);
  320. ret = i2c_read(chip, address, 1, mac, 6);
  321. i2c_set_bus_num(bus);
  322. if (!ret) {
  323. sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  324. mac[0],
  325. mac[1],
  326. mac[2],
  327. mac[3],
  328. mac[4],
  329. mac[5]);
  330. printf("MAC: %s\n", ethaddr);
  331. env_set(envvar, ethaddr);
  332. }
  333. return ret;
  334. }
  335. void mac_read_from_fixed_id(void)
  336. {
  337. #ifdef CONFIG_SYS_I2C_MAC1_CHIP_ADDR
  338. mac_read_from_generic_eeprom("ethaddr", CONFIG_SYS_I2C_MAC1_CHIP_ADDR,
  339. CONFIG_SYS_I2C_MAC1_DATA_ADDR, CONFIG_SYS_I2C_MAC1_BUS);
  340. #endif
  341. #ifdef CONFIG_SYS_I2C_MAC2_CHIP_ADDR
  342. mac_read_from_generic_eeprom("eth1addr", CONFIG_SYS_I2C_MAC2_CHIP_ADDR,
  343. CONFIG_SYS_I2C_MAC2_DATA_ADDR, CONFIG_SYS_I2C_MAC2_BUS);
  344. #endif
  345. }
  346. /**
  347. * mac_read_from_eeprom - read the MAC addresses from EEPROM
  348. *
  349. * This function reads the MAC addresses from EEPROM and sets the
  350. * appropriate environment variables for each one read.
  351. *
  352. * The environment variables are only set if they haven't been set already.
  353. * This ensures that any user-saved variables are never overwritten.
  354. *
  355. * This function must be called after relocation.
  356. *
  357. * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
  358. * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
  359. * located at a different offset.
  360. */
  361. int mac_read_from_eeprom_common(void)
  362. {
  363. unsigned int i;
  364. u32 crc, crc_offset = offsetof(struct eeprom, crc);
  365. u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
  366. puts("EEPROM: ");
  367. if (read_eeprom()) {
  368. printf("Read failed.\n");
  369. return 0;
  370. }
  371. if (!is_valid) {
  372. printf("Invalid ID (%02x %02x %02x %02x)\n",
  373. e.id[0], e.id[1], e.id[2], e.id[3]);
  374. return 0;
  375. }
  376. crc = crc32(0, (void *)&e, crc_offset);
  377. crcp = (void *)&e + crc_offset;
  378. if (crc != be32_to_cpu(*crcp)) {
  379. printf("CRC mismatch (%08x != %08x)\n", crc,
  380. be32_to_cpu(e.crc));
  381. return 0;
  382. }
  383. /*
  384. * MAC address #9 in v1 occupies the same position as the CRC in v0.
  385. * Erase it so that it's not mistaken for a MAC address. We'll
  386. * update the CRC later.
  387. */
  388. if (e.version == 0)
  389. memset(e.mac[8], 0xff, 6);
  390. for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
  391. if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
  392. memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
  393. char ethaddr[18];
  394. char enetvar[9];
  395. sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  396. e.mac[i][0],
  397. e.mac[i][1],
  398. e.mac[i][2],
  399. e.mac[i][3],
  400. e.mac[i][4],
  401. e.mac[i][5]);
  402. sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
  403. /* Only initialize environment variables that are blank
  404. * (i.e. have not yet been set)
  405. */
  406. if (!env_get(enetvar))
  407. env_set(enetvar, ethaddr);
  408. }
  409. }
  410. printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
  411. be32_to_cpu(e.version));
  412. return 0;
  413. }