eeprom.c 11 KB

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