eeprom.c 11 KB

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