cmd_eeprom.c 12 KB

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