sys_eeprom.c 11 KB

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