2 * (C) Copyright 2000, 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * Support for read and write access to EEPROM like memory devices. This
10 * includes regular EEPROM as well as FRAM (ferroelectic nonvolaile RAM).
11 * FRAM devices read and write data at bus speed. In particular, there is no
12 * write delay. Also, there is no limit imposed on the number of bytes that can
13 * be transferred with a single read or write.
15 * Use the following configuration options to ensure no unneeded performance
16 * degradation (typical for EEPROM) is incured for FRAM memory:
18 * #define CONFIG_SYS_I2C_FRAM
19 * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
28 #ifndef CONFIG_SYS_I2C_SPEED
29 #define CONFIG_SYS_I2C_SPEED 50000
32 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
33 #define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
37 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
38 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
40 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
41 * 0x00000nxx for EEPROM address selectors and page number at n.
43 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
44 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
45 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
46 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
47 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
51 __weak
int eeprom_write_enable(unsigned dev_addr
, int state
)
56 void eeprom_init(void)
59 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
64 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
65 i2c_init(CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
);
69 static int eeprom_addr(unsigned dev_addr
, unsigned offset
, uchar
*addr
)
74 blk_off
= offset
& 0xff; /* block offset */
75 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
76 addr
[0] = offset
>> 8; /* block number */
77 addr
[1] = blk_off
; /* block offset */
80 addr
[0] = offset
>> 16; /* block number */
81 addr
[1] = offset
>> 8; /* upper address octet */
82 addr
[2] = blk_off
; /* lower address octet */
84 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
86 addr
[0] |= dev_addr
; /* insert device address */
91 static int eeprom_rw_block(unsigned offset
, uchar
*addr
, unsigned alen
,
92 uchar
*buffer
, unsigned len
, bool read
)
97 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
99 spi_read(addr
, alen
, buffer
, len
);
101 spi_write(addr
, alen
, buffer
, len
);
104 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
105 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS
);
109 ret
= i2c_read(addr
[0], offset
, alen
- 1, buffer
, len
);
111 ret
= i2c_write(addr
[0], offset
, alen
- 1, buffer
, len
);
119 int eeprom_read (unsigned dev_addr
, unsigned offset
, uchar
*buffer
, unsigned cnt
)
121 unsigned end
= offset
+ cnt
;
127 * Read data until done or would cross a page boundary.
128 * We must write the address again when changing pages
129 * because the next page may be in a different device.
131 while (offset
< end
) {
133 #if !defined(CONFIG_SYS_I2C_FRAM)
137 blk_off
= offset
& 0xFF; /* block offset */
138 alen
= eeprom_addr(dev_addr
, offset
, addr
);
143 * For a FRAM device there is no limit on the number of the
144 * bytes that can be ccessed with the single read or write
147 #if !defined(CONFIG_SYS_I2C_FRAM)
148 maxlen
= 0x100 - blk_off
;
149 if (maxlen
> I2C_RXTX_LEN
)
150 maxlen
= I2C_RXTX_LEN
;
155 rcode
= eeprom_rw_block(offset
, addr
, alen
, buffer
, len
, 1);
164 int eeprom_write (unsigned dev_addr
, unsigned offset
, uchar
*buffer
, unsigned cnt
)
166 unsigned end
= offset
+ cnt
;
171 eeprom_write_enable(dev_addr
, 1);
174 * Write data until done or would cross a write page boundary.
175 * We must write the address again when changing pages
176 * because the address counter only increments within a page.
179 while (offset
< end
) {
181 #if !defined(CONFIG_SYS_I2C_FRAM)
185 blk_off
= offset
& 0xFF; /* block offset */
186 alen
= eeprom_addr(dev_addr
, offset
, addr
);
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
195 #if !defined(CONFIG_SYS_I2C_FRAM)
197 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
199 #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
200 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
202 maxlen
= EEPROM_PAGE_SIZE
- EEPROM_PAGE_OFFSET(blk_off
);
204 maxlen
= 0x100 - blk_off
;
206 if (maxlen
> I2C_RXTX_LEN
)
207 maxlen
= I2C_RXTX_LEN
;
213 rcode
= eeprom_rw_block(offset
, addr
, alen
, buffer
, len
, 0);
218 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
* 1000);
221 eeprom_write_enable(dev_addr
, 0);
226 static int do_eeprom(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
228 const char *const fmt
=
229 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
230 char * const *args
= &argv
[2];
232 ulong dev_addr
, addr
, off
, cnt
;
235 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
237 dev_addr
= CONFIG_SYS_DEF_EEPROM_ADDR
;
241 dev_addr
= simple_strtoul(*args
++, NULL
, 16);
244 return CMD_RET_USAGE
;
247 addr
= simple_strtoul(*args
++, NULL
, 16);
248 off
= simple_strtoul(*args
++, NULL
, 16);
249 cnt
= simple_strtoul(*args
++, NULL
, 16);
253 if (strcmp (argv
[1], "read") == 0) {
254 printf(fmt
, dev_addr
, argv
[1], addr
, off
, cnt
);
256 rcode
= eeprom_read(dev_addr
, off
, (uchar
*) addr
, cnt
);
260 } else if (strcmp (argv
[1], "write") == 0) {
261 printf(fmt
, dev_addr
, argv
[1], addr
, off
, cnt
);
263 rcode
= eeprom_write(dev_addr
, off
, (uchar
*) addr
, cnt
);
269 return CMD_RET_USAGE
;
273 eeprom
, 6, 1, do_eeprom
,
275 "read devaddr addr off cnt\n"
276 "eeprom write devaddr addr off cnt\n"
277 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"