2 * (C) Copyright 2000, 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 * Support for read and write access to EEPROM like memory devices. This
27 * includes regular EEPROM as well as FRAM (ferroelectic nonvolaile RAM).
28 * FRAM devices read and write data at bus speed. In particular, there is no
29 * write delay. Also, there is no limit imposed on the numer of bytes that can
30 * be transferred with a single read or write.
32 * Use the following configuration options to ensure no unneeded performance
33 * degradation (typical for EEPROM) is incured for FRAM memory:
35 * #define CONFIG_SYS_I2C_FRAM
36 * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
45 extern void eeprom_init (void);
46 extern int eeprom_read (unsigned dev_addr, unsigned offset,
47 uchar *buffer, unsigned cnt);
48 extern int eeprom_write (unsigned dev_addr, unsigned offset,
49 uchar *buffer, unsigned cnt);
50 #if defined(CONFIG_SYS_EEPROM_WREN)
51 extern int eeprom_write_enable (unsigned dev_addr, int state);
55 #if defined(CONFIG_SYS_EEPROM_X40430)
56 /* Maximum number of times to poll for acknowledge after write */
57 #define MAX_ACKNOWLEDGE_POLLS 10
60 /* ------------------------------------------------------------------------- */
62 #if defined(CONFIG_CMD_EEPROM)
63 int do_eeprom ( cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
65 const char *const fmt =
66 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
68 #if defined(CONFIG_SYS_I2C_MULTI_EEPROMS)
70 ulong dev_addr = simple_strtoul (argv[2], NULL, 16);
71 ulong addr = simple_strtoul (argv[3], NULL, 16);
72 ulong off = simple_strtoul (argv[4], NULL, 16);
73 ulong cnt = simple_strtoul (argv[5], NULL, 16);
76 ulong dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
77 ulong addr = simple_strtoul (argv[2], NULL, 16);
78 ulong off = simple_strtoul (argv[3], NULL, 16);
79 ulong cnt = simple_strtoul (argv[4], NULL, 16);
80 #endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */
82 # if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
84 # endif /* !CONFIG_SPI */
86 if (strcmp (argv[1], "read") == 0) {
89 printf (fmt, dev_addr, argv[1], addr, off, cnt);
91 rcode = eeprom_read (dev_addr, off, (uchar *) addr, cnt);
95 } else if (strcmp (argv[1], "write") == 0) {
98 printf (fmt, dev_addr, argv[1], addr, off, cnt);
100 rcode = eeprom_write (dev_addr, off, (uchar *) addr, cnt);
112 /*-----------------------------------------------------------------------
114 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
115 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
117 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
118 * 0x00000nxx for EEPROM address selectors and page number at n.
121 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
122 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1 || CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2
123 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
127 int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
129 unsigned end = offset + cnt;
133 /* Read data until done or would cross a page boundary.
134 * We must write the address again when changing pages
135 * because the next page may be in a different device.
137 while (offset < end) {
139 #if !defined(CONFIG_SYS_I2C_FRAM)
143 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
146 blk_off = offset & 0xFF; /* block offset */
148 addr[0] = offset >> 8; /* block number */
149 addr[1] = blk_off; /* block offset */
154 blk_off = offset & 0xFF; /* block offset */
156 addr[0] = offset >> 16; /* block number */
157 addr[1] = offset >> 8; /* upper address octet */
158 addr[2] = blk_off; /* lower address octet */
160 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
162 addr[0] |= dev_addr; /* insert device address */
167 * For a FRAM device there is no limit on the number of the
168 * bytes that can be ccessed with the single read or write
171 #if !defined(CONFIG_SYS_I2C_FRAM)
172 maxlen = 0x100 - blk_off;
173 if (maxlen > I2C_RXTX_LEN)
174 maxlen = I2C_RXTX_LEN;
179 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
180 spi_read (addr, alen, buffer, len);
182 if (i2c_read (addr[0], offset, alen-1, buffer, len) != 0)
192 /*-----------------------------------------------------------------------
194 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
195 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
197 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
198 * 0x00000nxx for EEPROM address selectors and page number at n.
201 int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
203 unsigned end = offset + cnt;
207 #if defined(CONFIG_SYS_EEPROM_X40430)
208 uchar contr_r_addr[2];
215 #if defined(CONFIG_SYS_EEPROM_WREN)
216 eeprom_write_enable (dev_addr,1);
218 /* Write data until done or would cross a write page boundary.
219 * We must write the address again when changing pages
220 * because the address counter only increments within a page.
223 while (offset < end) {
225 #if !defined(CONFIG_SYS_I2C_FRAM)
229 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
232 blk_off = offset & 0xFF; /* block offset */
234 addr[0] = offset >> 8; /* block number */
235 addr[1] = blk_off; /* block offset */
240 blk_off = offset & 0xFF; /* block offset */
242 addr[0] = offset >> 16; /* block number */
243 addr[1] = offset >> 8; /* upper address octet */
244 addr[2] = blk_off; /* lower address octet */
246 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
248 addr[0] |= dev_addr; /* insert device address */
253 * For a FRAM device there is no limit on the number of the
254 * bytes that can be ccessed with the single read or write
257 #if !defined(CONFIG_SYS_I2C_FRAM)
259 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
261 #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
262 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
264 maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
266 maxlen = 0x100 - blk_off;
268 if (maxlen > I2C_RXTX_LEN)
269 maxlen = I2C_RXTX_LEN;
275 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
276 spi_write (addr, alen, buffer, len);
278 #if defined(CONFIG_SYS_EEPROM_X40430)
279 /* Get the value of the control register.
280 * Set current address (internal pointer in the x40430)
284 contr_r_addr[1] = 0xff;
286 addr_void[1] = addr[1];
287 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR
288 contr_r_addr[0] |= CONFIG_SYS_I2C_EEPROM_ADDR;
289 addr_void[0] |= CONFIG_SYS_I2C_EEPROM_ADDR;
292 if (i2c_read (contr_r_addr[0], contr_r_addr[1], 1, contr_reg, 1) != 0) {
295 ctrl_reg_v = contr_reg[0];
297 /* Are any of the eeprom blocks write protected?
299 if (ctrl_reg_v & 0x18) {
300 ctrl_reg_v &= ~0x18; /* reset block protect bits */
301 ctrl_reg_v |= 0x02; /* set write enable latch */
302 ctrl_reg_v &= ~0x04; /* clear RWEL */
304 /* Set write enable latch.
307 if (i2c_write (contr_r_addr[0], 0xff, 1, contr_reg, 1) != 0) {
311 /* Set register write enable latch.
314 if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
318 /* Modify ctrl register.
320 contr_reg[0] = ctrl_reg_v;
321 if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
325 /* The write (above) is an operation on NV memory.
326 * These can take some time (~5ms), and the device
327 * will not respond to further I2C messages till
328 * it's completed the write.
329 * So poll device for an I2C acknowledge.
330 * When we get one we know we can continue with other
334 for (i = 0; i < MAX_ACKNOWLEDGE_POLLS; i++) {
335 if (i2c_read (addr_void[0], addr_void[1], 1, contr_reg, 1) == 0)
337 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
338 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
341 if (i == MAX_ACKNOWLEDGE_POLLS) {
342 puts ("EEPROM poll acknowledge failed\n");
347 /* Is the write enable latch on?.
349 else if (!(ctrl_reg_v & 0x02)) {
350 /* Set write enable latch.
353 if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
357 /* Write is enabled ... now write eeprom value.
360 if (i2c_write (addr[0], offset, alen-1, buffer, len) != 0)
367 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
368 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
371 #if defined(CONFIG_SYS_EEPROM_WREN)
372 eeprom_write_enable (dev_addr,0);
377 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
379 eeprom_probe (unsigned dev_addr, unsigned offset)
383 /* Probe the chip address
385 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
386 chip = offset >> 8; /* block number */
388 chip = offset >> 16; /* block number */
389 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
391 chip |= dev_addr; /* insert device address */
393 return (i2c_probe (chip));
397 /*-----------------------------------------------------------------------
400 #ifndef CONFIG_SYS_I2C_SPEED
401 #define CONFIG_SYS_I2C_SPEED 50000
404 void eeprom_init (void)
407 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
410 #if defined(CONFIG_HARD_I2C) || \
411 defined(CONFIG_SOFT_I2C)
412 i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
416 /*-----------------------------------------------------------------------
419 /***************************************************/
421 #if defined(CONFIG_CMD_EEPROM)
423 #ifdef CONFIG_SYS_I2C_MULTI_EEPROMS
425 eeprom, 6, 1, do_eeprom,
427 "read devaddr addr off cnt\n"
428 "eeprom write devaddr addr off cnt\n"
429 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
431 #else /* One EEPROM */
433 eeprom, 5, 1, do_eeprom,
435 "read addr off cnt\n"
436 "eeprom write addr off cnt\n"
437 " - read/write `cnt' bytes at EEPROM offset `off'"
439 #endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */