eeprom.c 11 KB

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