cmd_eeprom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 defined(CONFIG_SYS_I2C_EEPROM_BUS)
  135. i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
  136. #endif
  137. if (i2c_read(addr[0], offset, alen - 1, buffer, len))
  138. rcode = 1;
  139. #endif
  140. buffer += len;
  141. offset += len;
  142. }
  143. return rcode;
  144. }
  145. /*-----------------------------------------------------------------------
  146. *
  147. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
  148. * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
  149. *
  150. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
  151. * 0x00000nxx for EEPROM address selectors and page number at n.
  152. */
  153. int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  154. {
  155. unsigned end = offset + cnt;
  156. unsigned blk_off;
  157. int rcode = 0;
  158. #if defined(CONFIG_SYS_EEPROM_X40430)
  159. uchar contr_r_addr[2];
  160. uchar addr_void[2];
  161. uchar contr_reg[2];
  162. uchar ctrl_reg_v;
  163. int i;
  164. #endif
  165. #if defined(CONFIG_SYS_EEPROM_WREN)
  166. eeprom_write_enable (dev_addr,1);
  167. #endif
  168. /* Write data until done or would cross a write page boundary.
  169. * We must write the address again when changing pages
  170. * because the address counter only increments within a page.
  171. */
  172. while (offset < end) {
  173. unsigned alen, len;
  174. #if !defined(CONFIG_SYS_I2C_FRAM)
  175. unsigned maxlen;
  176. #endif
  177. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
  178. uchar addr[2];
  179. blk_off = offset & 0xFF; /* block offset */
  180. addr[0] = offset >> 8; /* block number */
  181. addr[1] = blk_off; /* block offset */
  182. alen = 2;
  183. #else
  184. uchar addr[3];
  185. blk_off = offset & 0xFF; /* block offset */
  186. addr[0] = offset >> 16; /* block number */
  187. addr[1] = offset >> 8; /* upper address octet */
  188. addr[2] = blk_off; /* lower address octet */
  189. alen = 3;
  190. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
  191. addr[0] |= dev_addr; /* insert device address */
  192. len = end - offset;
  193. /*
  194. * For a FRAM device there is no limit on the number of the
  195. * bytes that can be accessed with the single read or write
  196. * operation.
  197. */
  198. #if !defined(CONFIG_SYS_I2C_FRAM)
  199. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
  200. #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
  201. #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
  202. maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
  203. #else
  204. maxlen = 0x100 - blk_off;
  205. #endif
  206. if (maxlen > I2C_RXTX_LEN)
  207. maxlen = I2C_RXTX_LEN;
  208. if (len > maxlen)
  209. len = maxlen;
  210. #endif
  211. #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  212. spi_write (addr, alen, buffer, len);
  213. #else
  214. #if defined(CONFIG_SYS_EEPROM_X40430)
  215. /* Get the value of the control register.
  216. * Set current address (internal pointer in the x40430)
  217. * to 0x1ff.
  218. */
  219. contr_r_addr[0] = 9;
  220. contr_r_addr[1] = 0xff;
  221. addr_void[0] = 0;
  222. addr_void[1] = addr[1];
  223. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR
  224. contr_r_addr[0] |= CONFIG_SYS_I2C_EEPROM_ADDR;
  225. addr_void[0] |= CONFIG_SYS_I2C_EEPROM_ADDR;
  226. #endif
  227. contr_reg[0] = 0xff;
  228. if (i2c_read (contr_r_addr[0], contr_r_addr[1], 1, contr_reg, 1) != 0) {
  229. rcode = 1;
  230. }
  231. ctrl_reg_v = contr_reg[0];
  232. /* Are any of the eeprom blocks write protected?
  233. */
  234. if (ctrl_reg_v & 0x18) {
  235. ctrl_reg_v &= ~0x18; /* reset block protect bits */
  236. ctrl_reg_v |= 0x02; /* set write enable latch */
  237. ctrl_reg_v &= ~0x04; /* clear RWEL */
  238. /* Set write enable latch.
  239. */
  240. contr_reg[0] = 0x02;
  241. if (i2c_write (contr_r_addr[0], 0xff, 1, contr_reg, 1) != 0) {
  242. rcode = 1;
  243. }
  244. /* Set register write enable latch.
  245. */
  246. contr_reg[0] = 0x06;
  247. if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
  248. rcode = 1;
  249. }
  250. /* Modify ctrl register.
  251. */
  252. contr_reg[0] = ctrl_reg_v;
  253. if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
  254. rcode = 1;
  255. }
  256. /* The write (above) is an operation on NV memory.
  257. * These can take some time (~5ms), and the device
  258. * will not respond to further I2C messages till
  259. * it's completed the write.
  260. * So poll device for an I2C acknowledge.
  261. * When we get one we know we can continue with other
  262. * operations.
  263. */
  264. contr_reg[0] = 0;
  265. for (i = 0; i < MAX_ACKNOWLEDGE_POLLS; i++) {
  266. if (i2c_read (addr_void[0], addr_void[1], 1, contr_reg, 1) == 0)
  267. break; /* got ack */
  268. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
  269. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  270. #endif
  271. }
  272. if (i == MAX_ACKNOWLEDGE_POLLS) {
  273. puts ("EEPROM poll acknowledge failed\n");
  274. rcode = 1;
  275. }
  276. }
  277. /* Is the write enable latch on?.
  278. */
  279. else if (!(ctrl_reg_v & 0x02)) {
  280. /* Set write enable latch.
  281. */
  282. contr_reg[0] = 0x02;
  283. if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
  284. rcode = 1;
  285. }
  286. }
  287. /* Write is enabled ... now write eeprom value.
  288. */
  289. #endif
  290. #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
  291. i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
  292. #endif
  293. if (i2c_write(addr[0], offset, alen - 1, buffer, len))
  294. rcode = 1;
  295. #endif
  296. buffer += len;
  297. offset += len;
  298. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
  299. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  300. #endif
  301. }
  302. #if defined(CONFIG_SYS_EEPROM_WREN)
  303. eeprom_write_enable (dev_addr,0);
  304. #endif
  305. return rcode;
  306. }
  307. #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  308. int
  309. eeprom_probe (unsigned dev_addr, unsigned offset)
  310. {
  311. unsigned char chip;
  312. /* Probe the chip address
  313. */
  314. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
  315. chip = offset >> 8; /* block number */
  316. #else
  317. chip = offset >> 16; /* block number */
  318. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
  319. chip |= dev_addr; /* insert device address */
  320. return (i2c_probe (chip));
  321. }
  322. #endif
  323. /*-----------------------------------------------------------------------
  324. * Set default values
  325. */
  326. #ifndef CONFIG_SYS_I2C_SPEED
  327. #define CONFIG_SYS_I2C_SPEED 50000
  328. #endif
  329. void eeprom_init (void)
  330. {
  331. #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
  332. spi_init_f ();
  333. #endif
  334. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
  335. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  336. #endif
  337. }
  338. /*-----------------------------------------------------------------------
  339. */
  340. /***************************************************/
  341. #if defined(CONFIG_CMD_EEPROM)
  342. #ifdef CONFIG_SYS_I2C_MULTI_EEPROMS
  343. U_BOOT_CMD(
  344. eeprom, 6, 1, do_eeprom,
  345. "EEPROM sub-system",
  346. "read devaddr addr off cnt\n"
  347. "eeprom write devaddr addr off cnt\n"
  348. " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
  349. )
  350. #else /* One EEPROM */
  351. U_BOOT_CMD(
  352. eeprom, 5, 1, do_eeprom,
  353. "EEPROM sub-system",
  354. "read addr off cnt\n"
  355. "eeprom write addr off cnt\n"
  356. " - read/write `cnt' bytes at EEPROM offset `off'"
  357. )
  358. #endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */
  359. #endif