cmd_eeprom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. extern void eeprom_init (void);
  26. extern int eeprom_read (unsigned dev_addr, unsigned offset,
  27. uchar *buffer, unsigned cnt);
  28. extern int eeprom_write (unsigned dev_addr, unsigned offset,
  29. uchar *buffer, unsigned cnt);
  30. #if defined(CONFIG_SYS_EEPROM_WREN)
  31. extern int eeprom_write_enable (unsigned dev_addr, int state);
  32. #endif
  33. #if defined(CONFIG_SYS_EEPROM_X40430)
  34. /* Maximum number of times to poll for acknowledge after write */
  35. #define MAX_ACKNOWLEDGE_POLLS 10
  36. #endif
  37. /* ------------------------------------------------------------------------- */
  38. #if defined(CONFIG_CMD_EEPROM)
  39. static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  40. {
  41. const char *const fmt =
  42. "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
  43. #if defined(CONFIG_SYS_I2C_MULTI_EEPROMS)
  44. if (argc == 6) {
  45. ulong dev_addr = simple_strtoul (argv[2], NULL, 16);
  46. ulong addr = simple_strtoul (argv[3], NULL, 16);
  47. ulong off = simple_strtoul (argv[4], NULL, 16);
  48. ulong cnt = simple_strtoul (argv[5], NULL, 16);
  49. #else
  50. if (argc == 5) {
  51. ulong dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
  52. ulong addr = simple_strtoul (argv[2], NULL, 16);
  53. ulong off = simple_strtoul (argv[3], NULL, 16);
  54. ulong cnt = simple_strtoul (argv[4], NULL, 16);
  55. #endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */
  56. # if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  57. eeprom_init ();
  58. # endif /* !CONFIG_SPI */
  59. if (strcmp (argv[1], "read") == 0) {
  60. int rcode;
  61. printf (fmt, dev_addr, argv[1], addr, off, cnt);
  62. rcode = eeprom_read (dev_addr, off, (uchar *) addr, cnt);
  63. puts ("done\n");
  64. return rcode;
  65. } else if (strcmp (argv[1], "write") == 0) {
  66. int rcode;
  67. printf (fmt, dev_addr, argv[1], addr, off, cnt);
  68. rcode = eeprom_write (dev_addr, off, (uchar *) addr, cnt);
  69. puts ("done\n");
  70. return rcode;
  71. }
  72. }
  73. return CMD_RET_USAGE;
  74. }
  75. #endif
  76. /*-----------------------------------------------------------------------
  77. *
  78. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
  79. * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
  80. *
  81. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
  82. * 0x00000nxx for EEPROM address selectors and page number at n.
  83. */
  84. #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  85. #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1 || CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2
  86. #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
  87. #endif
  88. #endif
  89. int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  90. {
  91. unsigned end = offset + cnt;
  92. unsigned blk_off;
  93. int rcode = 0;
  94. /* Read data until done or would cross a page boundary.
  95. * We must write the address again when changing pages
  96. * because the next page may be in a different device.
  97. */
  98. while (offset < end) {
  99. unsigned alen, len;
  100. #if !defined(CONFIG_SYS_I2C_FRAM)
  101. unsigned maxlen;
  102. #endif
  103. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
  104. uchar addr[2];
  105. blk_off = offset & 0xFF; /* block offset */
  106. addr[0] = offset >> 8; /* block number */
  107. addr[1] = blk_off; /* block offset */
  108. alen = 2;
  109. #else
  110. uchar addr[3];
  111. blk_off = offset & 0xFF; /* block offset */
  112. addr[0] = offset >> 16; /* block number */
  113. addr[1] = offset >> 8; /* upper address octet */
  114. addr[2] = blk_off; /* lower address octet */
  115. alen = 3;
  116. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
  117. addr[0] |= dev_addr; /* insert device address */
  118. len = end - offset;
  119. /*
  120. * For a FRAM device there is no limit on the number of the
  121. * bytes that can be ccessed with the single read or write
  122. * operation.
  123. */
  124. #if !defined(CONFIG_SYS_I2C_FRAM)
  125. maxlen = 0x100 - blk_off;
  126. if (maxlen > I2C_RXTX_LEN)
  127. maxlen = I2C_RXTX_LEN;
  128. if (len > maxlen)
  129. len = maxlen;
  130. #endif
  131. #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  132. spi_read (addr, alen, buffer, len);
  133. #else
  134. if (i2c_read(addr[0], offset, alen - 1, buffer, len))
  135. rcode = 1;
  136. #endif
  137. buffer += len;
  138. offset += len;
  139. }
  140. return rcode;
  141. }
  142. /*-----------------------------------------------------------------------
  143. *
  144. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
  145. * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
  146. *
  147. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
  148. * 0x00000nxx for EEPROM address selectors and page number at n.
  149. */
  150. int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  151. {
  152. unsigned end = offset + cnt;
  153. unsigned blk_off;
  154. int rcode = 0;
  155. #if defined(CONFIG_SYS_EEPROM_X40430)
  156. uchar contr_r_addr[2];
  157. uchar addr_void[2];
  158. uchar contr_reg[2];
  159. uchar ctrl_reg_v;
  160. int i;
  161. #endif
  162. #if defined(CONFIG_SYS_EEPROM_WREN)
  163. eeprom_write_enable (dev_addr,1);
  164. #endif
  165. /* Write data until done or would cross a write page boundary.
  166. * We must write the address again when changing pages
  167. * because the address counter only increments within a page.
  168. */
  169. while (offset < end) {
  170. unsigned alen, len;
  171. #if !defined(CONFIG_SYS_I2C_FRAM)
  172. unsigned maxlen;
  173. #endif
  174. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
  175. uchar addr[2];
  176. blk_off = offset & 0xFF; /* block offset */
  177. addr[0] = offset >> 8; /* block number */
  178. addr[1] = blk_off; /* block offset */
  179. alen = 2;
  180. #else
  181. uchar addr[3];
  182. blk_off = offset & 0xFF; /* block offset */
  183. addr[0] = offset >> 16; /* block number */
  184. addr[1] = offset >> 8; /* upper address octet */
  185. addr[2] = blk_off; /* lower address octet */
  186. alen = 3;
  187. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
  188. addr[0] |= dev_addr; /* insert device address */
  189. len = end - offset;
  190. /*
  191. * For a FRAM device there is no limit on the number of the
  192. * bytes that can be accessed with the single read or write
  193. * operation.
  194. */
  195. #if !defined(CONFIG_SYS_I2C_FRAM)
  196. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
  197. #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
  198. #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
  199. maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
  200. #else
  201. maxlen = 0x100 - blk_off;
  202. #endif
  203. if (maxlen > I2C_RXTX_LEN)
  204. maxlen = I2C_RXTX_LEN;
  205. if (len > maxlen)
  206. len = maxlen;
  207. #endif
  208. #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  209. spi_write (addr, alen, buffer, len);
  210. #else
  211. #if defined(CONFIG_SYS_EEPROM_X40430)
  212. /* Get the value of the control register.
  213. * Set current address (internal pointer in the x40430)
  214. * to 0x1ff.
  215. */
  216. contr_r_addr[0] = 9;
  217. contr_r_addr[1] = 0xff;
  218. addr_void[0] = 0;
  219. addr_void[1] = addr[1];
  220. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR
  221. contr_r_addr[0] |= CONFIG_SYS_I2C_EEPROM_ADDR;
  222. addr_void[0] |= CONFIG_SYS_I2C_EEPROM_ADDR;
  223. #endif
  224. contr_reg[0] = 0xff;
  225. if (i2c_read (contr_r_addr[0], contr_r_addr[1], 1, contr_reg, 1) != 0) {
  226. rcode = 1;
  227. }
  228. ctrl_reg_v = contr_reg[0];
  229. /* Are any of the eeprom blocks write protected?
  230. */
  231. if (ctrl_reg_v & 0x18) {
  232. ctrl_reg_v &= ~0x18; /* reset block protect bits */
  233. ctrl_reg_v |= 0x02; /* set write enable latch */
  234. ctrl_reg_v &= ~0x04; /* clear RWEL */
  235. /* Set write enable latch.
  236. */
  237. contr_reg[0] = 0x02;
  238. if (i2c_write (contr_r_addr[0], 0xff, 1, contr_reg, 1) != 0) {
  239. rcode = 1;
  240. }
  241. /* Set register write enable latch.
  242. */
  243. contr_reg[0] = 0x06;
  244. if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
  245. rcode = 1;
  246. }
  247. /* Modify ctrl register.
  248. */
  249. contr_reg[0] = ctrl_reg_v;
  250. if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
  251. rcode = 1;
  252. }
  253. /* The write (above) is an operation on NV memory.
  254. * These can take some time (~5ms), and the device
  255. * will not respond to further I2C messages till
  256. * it's completed the write.
  257. * So poll device for an I2C acknowledge.
  258. * When we get one we know we can continue with other
  259. * operations.
  260. */
  261. contr_reg[0] = 0;
  262. for (i = 0; i < MAX_ACKNOWLEDGE_POLLS; i++) {
  263. if (i2c_read (addr_void[0], addr_void[1], 1, contr_reg, 1) == 0)
  264. break; /* got ack */
  265. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
  266. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  267. #endif
  268. }
  269. if (i == MAX_ACKNOWLEDGE_POLLS) {
  270. puts ("EEPROM poll acknowledge failed\n");
  271. rcode = 1;
  272. }
  273. }
  274. /* Is the write enable latch on?.
  275. */
  276. else if (!(ctrl_reg_v & 0x02)) {
  277. /* Set write enable latch.
  278. */
  279. contr_reg[0] = 0x02;
  280. if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
  281. rcode = 1;
  282. }
  283. }
  284. /* Write is enabled ... now write eeprom value.
  285. */
  286. #endif
  287. if (i2c_write(addr[0], offset, alen - 1, buffer, len))
  288. rcode = 1;
  289. #endif
  290. buffer += len;
  291. offset += len;
  292. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
  293. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  294. #endif
  295. }
  296. #if defined(CONFIG_SYS_EEPROM_WREN)
  297. eeprom_write_enable (dev_addr,0);
  298. #endif
  299. return rcode;
  300. }
  301. #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  302. int
  303. eeprom_probe (unsigned dev_addr, unsigned offset)
  304. {
  305. unsigned char chip;
  306. /* Probe the chip address
  307. */
  308. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
  309. chip = offset >> 8; /* block number */
  310. #else
  311. chip = offset >> 16; /* block number */
  312. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
  313. chip |= dev_addr; /* insert device address */
  314. return (i2c_probe (chip));
  315. }
  316. #endif
  317. /*-----------------------------------------------------------------------
  318. * Set default values
  319. */
  320. #ifndef CONFIG_SYS_I2C_SPEED
  321. #define CONFIG_SYS_I2C_SPEED 50000
  322. #endif
  323. void eeprom_init (void)
  324. {
  325. #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  326. spi_init_f ();
  327. #endif
  328. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT) || \
  329. defined(CONFIG_SYS_I2C)
  330. #ifdef CONFIG_SYS_I2C
  331. i2c_init_all();
  332. #else
  333. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  334. #endif
  335. #endif
  336. }
  337. /*-----------------------------------------------------------------------
  338. */
  339. /***************************************************/
  340. #if defined(CONFIG_CMD_EEPROM)
  341. #ifdef CONFIG_SYS_I2C_MULTI_EEPROMS
  342. U_BOOT_CMD(
  343. eeprom, 6, 1, do_eeprom,
  344. "EEPROM sub-system",
  345. "read devaddr addr off cnt\n"
  346. "eeprom write devaddr addr off cnt\n"
  347. " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
  348. )
  349. #else /* One EEPROM */
  350. U_BOOT_CMD(
  351. eeprom, 5, 1, do_eeprom,
  352. "EEPROM sub-system",
  353. "read addr off cnt\n"
  354. "eeprom write addr off cnt\n"
  355. " - read/write `cnt' bytes at EEPROM offset `off'"
  356. )
  357. #endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */
  358. #endif