eeprom.c 6.4 KB

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