cmd_eeprom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * Support for read and write access to EEPROM like memory devices. This
  26. * includes regular EEPROM as well as FRAM (ferroelectic nonvolaile RAM).
  27. * FRAM devices read and write data at bus speed. In particular, there is no
  28. * write delay. Also, there is no limit imposed on the numer of bytes that can
  29. * be transferred with a single read or write.
  30. *
  31. * Use the following configuration options to ensure no unneeded performance
  32. * degradation (typical for EEPROM) is incured for FRAM memory:
  33. *
  34. * #define CONFIG_SYS_I2C_FRAM
  35. * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
  36. *
  37. */
  38. #include <common.h>
  39. #include <config.h>
  40. #include <command.h>
  41. #include <i2c.h>
  42. extern void eeprom_init (void);
  43. extern int eeprom_read (unsigned dev_addr, unsigned offset,
  44. uchar *buffer, unsigned cnt);
  45. extern int eeprom_write (unsigned dev_addr, unsigned offset,
  46. uchar *buffer, unsigned cnt);
  47. #if defined(CONFIG_SYS_EEPROM_WREN)
  48. extern int eeprom_write_enable (unsigned dev_addr, int state);
  49. #endif
  50. #if defined(CONFIG_SYS_EEPROM_X40430)
  51. /* Maximum number of times to poll for acknowledge after write */
  52. #define MAX_ACKNOWLEDGE_POLLS 10
  53. #endif
  54. /* ------------------------------------------------------------------------- */
  55. #if defined(CONFIG_CMD_EEPROM)
  56. int do_eeprom ( cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  57. {
  58. const char *const fmt =
  59. "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
  60. #if defined(CONFIG_SYS_I2C_MULTI_EEPROMS)
  61. if (argc == 6) {
  62. ulong dev_addr = simple_strtoul (argv[2], NULL, 16);
  63. ulong addr = simple_strtoul (argv[3], NULL, 16);
  64. ulong off = simple_strtoul (argv[4], NULL, 16);
  65. ulong cnt = simple_strtoul (argv[5], NULL, 16);
  66. #else
  67. if (argc == 5) {
  68. ulong dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
  69. ulong addr = simple_strtoul (argv[2], NULL, 16);
  70. ulong off = simple_strtoul (argv[3], NULL, 16);
  71. ulong cnt = simple_strtoul (argv[4], NULL, 16);
  72. #endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */
  73. # ifndef CONFIG_SPI
  74. eeprom_init ();
  75. # endif /* !CONFIG_SPI */
  76. if (strcmp (argv[1], "read") == 0) {
  77. int rcode;
  78. printf (fmt, dev_addr, argv[1], addr, off, cnt);
  79. rcode = eeprom_read (dev_addr, off, (uchar *) addr, cnt);
  80. puts ("done\n");
  81. return rcode;
  82. } else if (strcmp (argv[1], "write") == 0) {
  83. int rcode;
  84. printf (fmt, dev_addr, argv[1], addr, off, cnt);
  85. rcode = eeprom_write (dev_addr, off, (uchar *) addr, cnt);
  86. puts ("done\n");
  87. return rcode;
  88. }
  89. }
  90. cmd_usage(cmdtp);
  91. return 1;
  92. }
  93. #endif
  94. /*-----------------------------------------------------------------------
  95. *
  96. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
  97. * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
  98. *
  99. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
  100. * 0x00000nxx for EEPROM address selectors and page number at n.
  101. */
  102. #ifndef CONFIG_SPI
  103. #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1 || CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2
  104. #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
  105. #endif
  106. #endif
  107. int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  108. {
  109. unsigned end = offset + cnt;
  110. unsigned blk_off;
  111. int rcode = 0;
  112. /* Read data until done or would cross a page boundary.
  113. * We must write the address again when changing pages
  114. * because the next page may be in a different device.
  115. */
  116. while (offset < end) {
  117. unsigned alen, len;
  118. #if !defined(CONFIG_SYS_I2C_FRAM)
  119. unsigned maxlen;
  120. #endif
  121. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
  122. uchar addr[2];
  123. blk_off = offset & 0xFF; /* block offset */
  124. addr[0] = offset >> 8; /* block number */
  125. addr[1] = blk_off; /* block offset */
  126. alen = 2;
  127. #else
  128. uchar addr[3];
  129. blk_off = offset & 0xFF; /* block offset */
  130. addr[0] = offset >> 16; /* block number */
  131. addr[1] = offset >> 8; /* upper address octet */
  132. addr[2] = blk_off; /* lower address octet */
  133. alen = 3;
  134. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
  135. addr[0] |= dev_addr; /* insert device address */
  136. len = end - offset;
  137. /*
  138. * For a FRAM device there is no limit on the number of the
  139. * bytes that can be ccessed with the single read or write
  140. * operation.
  141. */
  142. #if !defined(CONFIG_SYS_I2C_FRAM)
  143. maxlen = 0x100 - blk_off;
  144. if (maxlen > I2C_RXTX_LEN)
  145. maxlen = I2C_RXTX_LEN;
  146. if (len > maxlen)
  147. len = maxlen;
  148. #endif
  149. #ifdef CONFIG_SPI
  150. spi_read (addr, alen, buffer, len);
  151. #else
  152. if (i2c_read (addr[0], offset, alen-1, buffer, len) != 0)
  153. rcode = 1;
  154. #endif
  155. buffer += len;
  156. offset += len;
  157. }
  158. return rcode;
  159. }
  160. /*-----------------------------------------------------------------------
  161. *
  162. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
  163. * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
  164. *
  165. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
  166. * 0x00000nxx for EEPROM address selectors and page number at n.
  167. */
  168. int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
  169. {
  170. unsigned end = offset + cnt;
  171. unsigned blk_off;
  172. int rcode = 0;
  173. #if defined(CONFIG_SYS_EEPROM_X40430)
  174. uchar contr_r_addr[2];
  175. uchar addr_void[2];
  176. uchar contr_reg[2];
  177. uchar ctrl_reg_v;
  178. int i;
  179. #endif
  180. #if defined(CONFIG_SYS_EEPROM_WREN)
  181. eeprom_write_enable (dev_addr,1);
  182. #endif
  183. /* Write data until done or would cross a write page boundary.
  184. * We must write the address again when changing pages
  185. * because the address counter only increments within a page.
  186. */
  187. while (offset < end) {
  188. unsigned alen, len;
  189. #if !defined(CONFIG_SYS_I2C_FRAM)
  190. unsigned maxlen;
  191. #endif
  192. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
  193. uchar addr[2];
  194. blk_off = offset & 0xFF; /* block offset */
  195. addr[0] = offset >> 8; /* block number */
  196. addr[1] = blk_off; /* block offset */
  197. alen = 2;
  198. #else
  199. uchar addr[3];
  200. blk_off = offset & 0xFF; /* block offset */
  201. addr[0] = offset >> 16; /* block number */
  202. addr[1] = offset >> 8; /* upper address octet */
  203. addr[2] = blk_off; /* lower address octet */
  204. alen = 3;
  205. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
  206. addr[0] |= dev_addr; /* insert device address */
  207. len = end - offset;
  208. /*
  209. * For a FRAM device there is no limit on the number of the
  210. * bytes that can be ccessed with the single read or write
  211. * operation.
  212. */
  213. #if !defined(CONFIG_SYS_I2C_FRAM)
  214. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
  215. #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
  216. #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
  217. maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
  218. #else
  219. maxlen = 0x100 - blk_off;
  220. #endif
  221. if (maxlen > I2C_RXTX_LEN)
  222. maxlen = I2C_RXTX_LEN;
  223. if (len > maxlen)
  224. len = maxlen;
  225. #endif
  226. #ifdef CONFIG_SPI
  227. spi_write (addr, alen, buffer, len);
  228. #else
  229. #if defined(CONFIG_SYS_EEPROM_X40430)
  230. /* Get the value of the control register.
  231. * Set current address (internal pointer in the x40430)
  232. * to 0x1ff.
  233. */
  234. contr_r_addr[0] = 9;
  235. contr_r_addr[1] = 0xff;
  236. addr_void[0] = 0;
  237. addr_void[1] = addr[1];
  238. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR
  239. contr_r_addr[0] |= CONFIG_SYS_I2C_EEPROM_ADDR;
  240. addr_void[0] |= CONFIG_SYS_I2C_EEPROM_ADDR;
  241. #endif
  242. contr_reg[0] = 0xff;
  243. if (i2c_read (contr_r_addr[0], contr_r_addr[1], 1, contr_reg, 1) != 0) {
  244. rcode = 1;
  245. }
  246. ctrl_reg_v = contr_reg[0];
  247. /* Are any of the eeprom blocks write protected?
  248. */
  249. if (ctrl_reg_v & 0x18) {
  250. ctrl_reg_v &= ~0x18; /* reset block protect bits */
  251. ctrl_reg_v |= 0x02; /* set write enable latch */
  252. ctrl_reg_v &= ~0x04; /* clear RWEL */
  253. /* Set write enable latch.
  254. */
  255. contr_reg[0] = 0x02;
  256. if (i2c_write (contr_r_addr[0], 0xff, 1, contr_reg, 1) != 0) {
  257. rcode = 1;
  258. }
  259. /* Set register write enable latch.
  260. */
  261. contr_reg[0] = 0x06;
  262. if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
  263. rcode = 1;
  264. }
  265. /* Modify ctrl register.
  266. */
  267. contr_reg[0] = ctrl_reg_v;
  268. if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
  269. rcode = 1;
  270. }
  271. /* The write (above) is an operation on NV memory.
  272. * These can take some time (~5ms), and the device
  273. * will not respond to further I2C messages till
  274. * it's completed the write.
  275. * So poll device for an I2C acknowledge.
  276. * When we get one we know we can continue with other
  277. * operations.
  278. */
  279. contr_reg[0] = 0;
  280. for (i = 0; i < MAX_ACKNOWLEDGE_POLLS; i++) {
  281. if (i2c_read (addr_void[0], addr_void[1], 1, contr_reg, 1) == 0)
  282. break; /* got ack */
  283. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
  284. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  285. #endif
  286. }
  287. if (i == MAX_ACKNOWLEDGE_POLLS) {
  288. puts ("EEPROM poll acknowledge failed\n");
  289. rcode = 1;
  290. }
  291. }
  292. /* Is the write enable latch on?.
  293. */
  294. else if (!(ctrl_reg_v & 0x02)) {
  295. /* Set write enable latch.
  296. */
  297. contr_reg[0] = 0x02;
  298. if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
  299. rcode = 1;
  300. }
  301. }
  302. /* Write is enabled ... now write eeprom value.
  303. */
  304. #endif
  305. if (i2c_write (addr[0], offset, alen-1, buffer, len) != 0)
  306. rcode = 1;
  307. #endif
  308. buffer += len;
  309. offset += len;
  310. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
  311. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  312. #endif
  313. }
  314. #if defined(CONFIG_SYS_EEPROM_WREN)
  315. eeprom_write_enable (dev_addr,0);
  316. #endif
  317. return rcode;
  318. }
  319. #ifndef CONFIG_SPI
  320. int
  321. eeprom_probe (unsigned dev_addr, unsigned offset)
  322. {
  323. unsigned char chip;
  324. /* Probe the chip address
  325. */
  326. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
  327. chip = offset >> 8; /* block number */
  328. #else
  329. chip = offset >> 16; /* block number */
  330. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
  331. chip |= dev_addr; /* insert device address */
  332. return (i2c_probe (chip));
  333. }
  334. #endif
  335. /*-----------------------------------------------------------------------
  336. * Set default values
  337. */
  338. #ifndef CONFIG_SYS_I2C_SPEED
  339. #define CONFIG_SYS_I2C_SPEED 50000
  340. #endif
  341. #ifndef CONFIG_SYS_I2C_SLAVE
  342. #define CONFIG_SYS_I2C_SLAVE 0xFE
  343. #endif
  344. void eeprom_init (void)
  345. {
  346. #if defined(CONFIG_SPI)
  347. spi_init_f ();
  348. #endif
  349. #if defined(CONFIG_HARD_I2C) || \
  350. defined(CONFIG_SOFT_I2C)
  351. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  352. #endif
  353. }
  354. /*-----------------------------------------------------------------------
  355. */
  356. /***************************************************/
  357. #if defined(CONFIG_CMD_EEPROM)
  358. #ifdef CONFIG_SYS_I2C_MULTI_EEPROMS
  359. U_BOOT_CMD(
  360. eeprom, 6, 1, do_eeprom,
  361. "EEPROM sub-system",
  362. "read devaddr addr off cnt\n"
  363. "eeprom write devaddr addr off cnt\n"
  364. " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'\n"
  365. );
  366. #else /* One EEPROM */
  367. U_BOOT_CMD(
  368. eeprom, 5, 1, do_eeprom,
  369. "EEPROM sub-system",
  370. "read addr off cnt\n"
  371. "eeprom write addr off cnt\n"
  372. " - read/write `cnt' bytes at EEPROM offset `off'\n"
  373. );
  374. #endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */
  375. #endif