cmd_eeprom.c 6.1 KB

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