cmd_eeprom.c 7.3 KB

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