cmd_eeprom.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #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. #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
  35. #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
  36. /*
  37. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
  38. * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
  39. *
  40. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
  41. * 0x00000nxx for EEPROM address selectors and page number at n.
  42. */
  43. #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  44. #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
  45. (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
  46. (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
  47. #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
  48. #endif
  49. #endif
  50. __weak int eeprom_write_enable(unsigned dev_addr, int state)
  51. {
  52. return 0;
  53. }
  54. void eeprom_init(int bus)
  55. {
  56. /* SPI EEPROM */
  57. #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  58. spi_init_f();
  59. #endif
  60. /* I2C EEPROM */
  61. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
  62. #if defined(CONFIG_SYS_I2C)
  63. if (bus >= 0)
  64. i2c_set_bus_num(bus);
  65. #endif
  66. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  67. #endif
  68. }
  69. static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
  70. {
  71. unsigned blk_off;
  72. int alen;
  73. blk_off = offset & 0xff; /* block offset */
  74. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
  75. addr[0] = offset >> 8; /* block number */
  76. addr[1] = blk_off; /* block offset */
  77. alen = 2;
  78. #else
  79. addr[0] = offset >> 16; /* block number */
  80. addr[1] = offset >> 8; /* upper address octet */
  81. addr[2] = blk_off; /* lower address octet */
  82. alen = 3;
  83. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
  84. addr[0] |= dev_addr; /* insert device address */
  85. return alen;
  86. }
  87. static int eeprom_len(unsigned offset, unsigned end)
  88. {
  89. unsigned len = end - offset;
  90. /*
  91. * For a FRAM device there is no limit on the number of the
  92. * bytes that can be ccessed with the single read or write
  93. * operation.
  94. */
  95. #if !defined(CONFIG_SYS_I2C_FRAM)
  96. unsigned blk_off = offset & 0xff;
  97. unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
  98. if (maxlen > I2C_RXTX_LEN)
  99. maxlen = I2C_RXTX_LEN;
  100. if (len > maxlen)
  101. len = maxlen;
  102. #endif
  103. return len;
  104. }
  105. static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
  106. uchar *buffer, unsigned len, bool read)
  107. {
  108. int ret = 0;
  109. /* SPI */
  110. #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  111. if (read)
  112. spi_read(addr, alen, buffer, len);
  113. else
  114. spi_write(addr, alen, buffer, len);
  115. #else /* I2C */
  116. #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
  117. i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
  118. #endif
  119. if (read)
  120. ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
  121. else
  122. ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
  123. if (ret)
  124. ret = 1;
  125. #endif
  126. return ret;
  127. }
  128. static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
  129. unsigned cnt, bool read)
  130. {
  131. unsigned end = offset + cnt;
  132. unsigned alen, len;
  133. int rcode = 0;
  134. uchar addr[3];
  135. while (offset < end) {
  136. alen = eeprom_addr(dev_addr, offset, addr);
  137. len = eeprom_len(offset, end);
  138. rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
  139. buffer += len;
  140. offset += len;
  141. if (!read)
  142. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  143. }
  144. return rcode;
  145. }
  146. int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  147. {
  148. /*
  149. * Read data until done or would cross a page boundary.
  150. * We must write the address again when changing pages
  151. * because the next page may be in a different device.
  152. */
  153. return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
  154. }
  155. int eeprom_write(unsigned dev_addr, unsigned offset,
  156. uchar *buffer, unsigned cnt)
  157. {
  158. int ret;
  159. eeprom_write_enable(dev_addr, 1);
  160. /*
  161. * Write data until done or would cross a write page boundary.
  162. * We must write the address again when changing pages
  163. * because the address counter only increments within a page.
  164. */
  165. ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
  166. eeprom_write_enable(dev_addr, 0);
  167. return ret;
  168. }
  169. static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  170. {
  171. const char *const fmt =
  172. "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
  173. char * const *args = &argv[2];
  174. int rcode;
  175. ulong dev_addr, addr, off, cnt;
  176. int bus_addr;
  177. switch (argc) {
  178. #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
  179. case 5:
  180. bus_addr = -1;
  181. dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
  182. break;
  183. #endif
  184. case 6:
  185. bus_addr = -1;
  186. dev_addr = simple_strtoul(*args++, NULL, 16);
  187. break;
  188. case 7:
  189. bus_addr = simple_strtoul(*args++, NULL, 16);
  190. dev_addr = simple_strtoul(*args++, NULL, 16);
  191. break;
  192. default:
  193. return CMD_RET_USAGE;
  194. }
  195. addr = simple_strtoul(*args++, NULL, 16);
  196. off = simple_strtoul(*args++, NULL, 16);
  197. cnt = simple_strtoul(*args++, NULL, 16);
  198. eeprom_init(bus_addr);
  199. if (strcmp(argv[1], "read") == 0) {
  200. printf(fmt, dev_addr, argv[1], addr, off, cnt);
  201. rcode = eeprom_read(dev_addr, off, (uchar *)addr, cnt);
  202. puts("done\n");
  203. return rcode;
  204. } else if (strcmp(argv[1], "write") == 0) {
  205. printf(fmt, dev_addr, argv[1], addr, off, cnt);
  206. rcode = eeprom_write(dev_addr, off, (uchar *)addr, cnt);
  207. puts("done\n");
  208. return rcode;
  209. }
  210. return CMD_RET_USAGE;
  211. }
  212. U_BOOT_CMD(
  213. eeprom, 7, 1, do_eeprom,
  214. "EEPROM sub-system",
  215. "read <bus> <devaddr> addr off cnt\n"
  216. "eeprom write <bus> <devaddr> addr off cnt\n"
  217. " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
  218. )