eeprom.c 11 KB

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