eeprom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000, 2001
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /*
  7. * Support for read and write access to EEPROM like memory devices. This
  8. * includes regular EEPROM as well as FRAM (ferroelectic nonvolaile RAM).
  9. * FRAM devices read and write data at bus speed. In particular, there is no
  10. * write delay. Also, there is no limit imposed on the number of bytes that can
  11. * be transferred with a single read or write.
  12. *
  13. * Use the following configuration options to ensure no unneeded performance
  14. * degradation (typical for EEPROM) is incured for FRAM memory:
  15. *
  16. * #define CONFIG_SYS_I2C_FRAM
  17. * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
  18. *
  19. */
  20. #include <common.h>
  21. #include <config.h>
  22. #include <command.h>
  23. #include <eeprom.h>
  24. #include <i2c.h>
  25. #include <eeprom_layout.h>
  26. #ifndef CONFIG_SYS_I2C_SPEED
  27. #define CONFIG_SYS_I2C_SPEED 50000
  28. #endif
  29. #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
  30. #define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
  31. #endif
  32. #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
  33. #define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8
  34. #endif
  35. #ifndef I2C_RXTX_LEN
  36. #define I2C_RXTX_LEN 128
  37. #endif
  38. #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
  39. #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
  40. /*
  41. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
  42. * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
  43. *
  44. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
  45. * 0x00000nxx for EEPROM address selectors and page number at n.
  46. */
  47. #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  48. #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
  49. (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
  50. (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
  51. #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
  52. #endif
  53. #endif
  54. #if defined(CONFIG_DM_I2C)
  55. int eeprom_i2c_bus;
  56. #endif
  57. __weak int eeprom_write_enable(unsigned dev_addr, int state)
  58. {
  59. return 0;
  60. }
  61. void eeprom_init(int bus)
  62. {
  63. /* I2C EEPROM */
  64. #if defined(CONFIG_DM_I2C)
  65. eeprom_i2c_bus = bus;
  66. #elif defined(CONFIG_SYS_I2C)
  67. if (bus >= 0)
  68. i2c_set_bus_num(bus);
  69. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  70. #endif
  71. }
  72. static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
  73. {
  74. unsigned blk_off;
  75. int alen;
  76. blk_off = offset & 0xff; /* block offset */
  77. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
  78. addr[0] = offset >> 8; /* block number */
  79. addr[1] = blk_off; /* block offset */
  80. alen = 2;
  81. #else
  82. addr[0] = offset >> 16; /* block number */
  83. addr[1] = offset >> 8; /* upper address octet */
  84. addr[2] = blk_off; /* lower address octet */
  85. alen = 3;
  86. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
  87. addr[0] |= dev_addr; /* insert device address */
  88. return alen;
  89. }
  90. static int eeprom_len(unsigned offset, unsigned end)
  91. {
  92. unsigned len = end - offset;
  93. /*
  94. * For a FRAM device there is no limit on the number of the
  95. * bytes that can be accessed with the single read or write
  96. * operation.
  97. */
  98. #if !defined(CONFIG_SYS_I2C_FRAM)
  99. unsigned blk_off = offset & 0xff;
  100. unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
  101. if (maxlen > I2C_RXTX_LEN)
  102. maxlen = I2C_RXTX_LEN;
  103. if (len > maxlen)
  104. len = maxlen;
  105. #endif
  106. return len;
  107. }
  108. static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
  109. uchar *buffer, unsigned len, bool read)
  110. {
  111. int ret = 0;
  112. #if defined(CONFIG_DM_I2C)
  113. struct udevice *dev;
  114. ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
  115. alen - 1, &dev);
  116. if (ret) {
  117. printf("%s: Cannot find udev for a bus %d\n", __func__,
  118. eeprom_i2c_bus);
  119. return CMD_RET_FAILURE;
  120. }
  121. if (read)
  122. ret = dm_i2c_read(dev, offset, buffer, len);
  123. else
  124. ret = dm_i2c_write(dev, offset, buffer, len);
  125. #else /* Non DM I2C support - will be removed */
  126. if (read)
  127. ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
  128. else
  129. ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
  130. #endif /* CONFIG_DM_I2C */
  131. if (ret)
  132. ret = CMD_RET_FAILURE;
  133. return ret;
  134. }
  135. static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
  136. unsigned cnt, bool read)
  137. {
  138. unsigned end = offset + cnt;
  139. unsigned alen, len;
  140. int rcode = 0;
  141. uchar addr[3];
  142. #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
  143. eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
  144. #endif
  145. while (offset < end) {
  146. alen = eeprom_addr(dev_addr, offset, addr);
  147. len = eeprom_len(offset, end);
  148. rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
  149. buffer += len;
  150. offset += len;
  151. if (!read)
  152. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  153. }
  154. return rcode;
  155. }
  156. int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  157. {
  158. /*
  159. * Read data until done or would cross a page boundary.
  160. * We must write the address again when changing pages
  161. * because the next page may be in a different device.
  162. */
  163. return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
  164. }
  165. int eeprom_write(unsigned dev_addr, unsigned offset,
  166. uchar *buffer, unsigned cnt)
  167. {
  168. int ret;
  169. eeprom_write_enable(dev_addr, 1);
  170. /*
  171. * Write data until done or would cross a write page boundary.
  172. * We must write the address again when changing pages
  173. * because the address counter only increments within a page.
  174. */
  175. ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
  176. eeprom_write_enable(dev_addr, 0);
  177. return ret;
  178. }
  179. static int parse_numeric_param(char *str)
  180. {
  181. char *endptr;
  182. int value = simple_strtol(str, &endptr, 16);
  183. return (*endptr != '\0') ? -1 : value;
  184. }
  185. /**
  186. * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
  187. *
  188. * @i2c_bus: address to store the i2c bus
  189. * @i2c_addr: address to store the device i2c address
  190. * @argc: count of command line arguments left to parse
  191. * @argv: command line arguments left to parse
  192. * @argc_no_bus_addr: argc value we expect to see when bus & addr aren't given
  193. *
  194. * @returns: number of arguments parsed or CMD_RET_USAGE if error
  195. */
  196. static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
  197. char * const argv[], int argc_no_bus_addr)
  198. {
  199. int argc_no_bus = argc_no_bus_addr + 1;
  200. int argc_bus_addr = argc_no_bus_addr + 2;
  201. #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
  202. if (argc == argc_no_bus_addr) {
  203. *i2c_bus = -1;
  204. *i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
  205. return 0;
  206. }
  207. #endif
  208. if (argc == argc_no_bus) {
  209. *i2c_bus = -1;
  210. *i2c_addr = parse_numeric_param(argv[0]);
  211. return 1;
  212. }
  213. if (argc == argc_bus_addr) {
  214. *i2c_bus = parse_numeric_param(argv[0]);
  215. *i2c_addr = parse_numeric_param(argv[1]);
  216. return 2;
  217. }
  218. return CMD_RET_USAGE;
  219. }
  220. #ifdef CONFIG_CMD_EEPROM_LAYOUT
  221. __weak int eeprom_parse_layout_version(char *str)
  222. {
  223. return LAYOUT_VERSION_UNRECOGNIZED;
  224. }
  225. static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
  226. #endif
  227. enum eeprom_action {
  228. EEPROM_READ,
  229. EEPROM_WRITE,
  230. EEPROM_PRINT,
  231. EEPROM_UPDATE,
  232. EEPROM_ACTION_INVALID,
  233. };
  234. static enum eeprom_action parse_action(char *cmd)
  235. {
  236. if (!strncmp(cmd, "read", 4))
  237. return EEPROM_READ;
  238. if (!strncmp(cmd, "write", 5))
  239. return EEPROM_WRITE;
  240. #ifdef CONFIG_CMD_EEPROM_LAYOUT
  241. if (!strncmp(cmd, "print", 5))
  242. return EEPROM_PRINT;
  243. if (!strncmp(cmd, "update", 6))
  244. return EEPROM_UPDATE;
  245. #endif
  246. return EEPROM_ACTION_INVALID;
  247. }
  248. static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
  249. ulong i2c_addr, int layout_ver, char *key,
  250. char *value, ulong addr, ulong off, ulong cnt)
  251. {
  252. int rcode = 0;
  253. const char *const fmt =
  254. "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
  255. #ifdef CONFIG_CMD_EEPROM_LAYOUT
  256. struct eeprom_layout layout;
  257. #endif
  258. if (action == EEPROM_ACTION_INVALID)
  259. return CMD_RET_USAGE;
  260. eeprom_init(i2c_bus);
  261. if (action == EEPROM_READ) {
  262. printf(fmt, i2c_addr, "read", addr, off, cnt);
  263. rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
  264. puts("done\n");
  265. return rcode;
  266. } else if (action == EEPROM_WRITE) {
  267. printf(fmt, i2c_addr, "write", addr, off, cnt);
  268. rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
  269. puts("done\n");
  270. return rcode;
  271. }
  272. #ifdef CONFIG_CMD_EEPROM_LAYOUT
  273. rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
  274. if (rcode < 0)
  275. return rcode;
  276. eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
  277. layout_ver);
  278. if (action == EEPROM_PRINT) {
  279. layout.print(&layout);
  280. return 0;
  281. }
  282. layout.update(&layout, key, value);
  283. rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
  284. #endif
  285. return rcode;
  286. }
  287. #define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
  288. int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  289. {
  290. int layout_ver = LAYOUT_VERSION_AUTODETECT;
  291. enum eeprom_action action = EEPROM_ACTION_INVALID;
  292. int i2c_bus = -1, index = 0;
  293. ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
  294. int ret;
  295. char *field_name = "";
  296. char *field_value = "";
  297. if (argc <= 1)
  298. return CMD_RET_USAGE;
  299. NEXT_PARAM(argc, index); /* Skip program name */
  300. action = parse_action(argv[index]);
  301. NEXT_PARAM(argc, index);
  302. if (action == EEPROM_ACTION_INVALID)
  303. return CMD_RET_USAGE;
  304. #ifdef CONFIG_CMD_EEPROM_LAYOUT
  305. if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
  306. if (!strcmp(argv[index], "-l")) {
  307. NEXT_PARAM(argc, index);
  308. layout_ver = eeprom_parse_layout_version(argv[index]);
  309. NEXT_PARAM(argc, index);
  310. }
  311. }
  312. #endif
  313. switch (action) {
  314. case EEPROM_READ:
  315. case EEPROM_WRITE:
  316. ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
  317. argv + index, 3);
  318. break;
  319. case EEPROM_PRINT:
  320. ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
  321. argv + index, 0);
  322. break;
  323. case EEPROM_UPDATE:
  324. ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
  325. argv + index, 2);
  326. break;
  327. default:
  328. /* Get compiler to stop whining */
  329. return CMD_RET_USAGE;
  330. }
  331. if (ret == CMD_RET_USAGE)
  332. return ret;
  333. while (ret--)
  334. NEXT_PARAM(argc, index);
  335. if (action == EEPROM_READ || action == EEPROM_WRITE) {
  336. addr = parse_numeric_param(argv[index]);
  337. NEXT_PARAM(argc, index);
  338. off = parse_numeric_param(argv[index]);
  339. NEXT_PARAM(argc, index);
  340. cnt = parse_numeric_param(argv[index]);
  341. }
  342. #ifdef CONFIG_CMD_EEPROM_LAYOUT
  343. if (action == EEPROM_UPDATE) {
  344. field_name = argv[index];
  345. NEXT_PARAM(argc, index);
  346. field_value = argv[index];
  347. NEXT_PARAM(argc, index);
  348. }
  349. #endif
  350. return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
  351. field_name, field_value, addr, off, cnt);
  352. }
  353. U_BOOT_CMD(
  354. eeprom, 8, 1, do_eeprom,
  355. "EEPROM sub-system",
  356. "read <bus> <devaddr> addr off cnt\n"
  357. "eeprom write <bus> <devaddr> addr off cnt\n"
  358. " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
  359. #ifdef CONFIG_CMD_EEPROM_LAYOUT
  360. "\n"
  361. "eeprom print [-l <layout_version>] <bus> <devaddr>\n"
  362. " - Print layout fields and their data in human readable format\n"
  363. "eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
  364. " - Update a specific eeprom field with new data.\n"
  365. " The new data must be written in the same human readable format as shown by the print command.\n"
  366. "\n"
  367. "LAYOUT VERSIONS\n"
  368. "The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
  369. "If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
  370. "The values which can be provided with the -l option are:\n"
  371. CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"
  372. #endif
  373. )