eeprom.c 11 KB

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