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
33 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
34 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
36 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
37 * 0x00000nxx for EEPROM address selectors and page number at n.
39 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
40 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
41 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
42 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
43 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
47 #if defined(CONFIG_SYS_EEPROM_WREN)
48 extern int eeprom_write_enable (unsigned dev_addr, int state);
51 void eeprom_init(void)
54 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
59 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
60 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
64 static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
65 uchar *buffer, unsigned len, bool read)
70 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
72 spi_read(addr, alen, buffer, len);
74 spi_write(addr, alen, buffer, len);
77 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
78 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
82 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
84 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
92 int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
94 unsigned end = offset + cnt;
99 * Read data until done or would cross a page boundary.
100 * We must write the address again when changing pages
101 * because the next page may be in a different device.
103 while (offset < end) {
105 #if !defined(CONFIG_SYS_I2C_FRAM)
109 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
112 blk_off = offset & 0xFF; /* block offset */
114 addr[0] = offset >> 8; /* block number */
115 addr[1] = blk_off; /* block offset */
120 blk_off = offset & 0xFF; /* block offset */
122 addr[0] = offset >> 16; /* block number */
123 addr[1] = offset >> 8; /* upper address octet */
124 addr[2] = blk_off; /* lower address octet */
126 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
128 addr[0] |= dev_addr; /* insert device address */
133 * For a FRAM device there is no limit on the number of the
134 * bytes that can be ccessed with the single read or write
137 #if !defined(CONFIG_SYS_I2C_FRAM)
138 maxlen = 0x100 - blk_off;
139 if (maxlen > I2C_RXTX_LEN)
140 maxlen = I2C_RXTX_LEN;
145 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 1);
154 int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
156 unsigned end = offset + cnt;
160 #if defined(CONFIG_SYS_EEPROM_WREN)
161 eeprom_write_enable (dev_addr,1);
164 * Write data until done or would cross a write page boundary.
165 * We must write the address again when changing pages
166 * because the address counter only increments within a page.
169 while (offset < end) {
171 #if !defined(CONFIG_SYS_I2C_FRAM)
175 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
178 blk_off = offset & 0xFF; /* block offset */
180 addr[0] = offset >> 8; /* block number */
181 addr[1] = blk_off; /* block offset */
186 blk_off = offset & 0xFF; /* block offset */
188 addr[0] = offset >> 16; /* block number */
189 addr[1] = offset >> 8; /* upper address octet */
190 addr[2] = blk_off; /* lower address octet */
192 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
194 addr[0] |= dev_addr; /* insert device address */
199 * For a FRAM device there is no limit on the number of the
200 * bytes that can be accessed with the single read or write
203 #if !defined(CONFIG_SYS_I2C_FRAM)
205 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
207 #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
208 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
210 maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
212 maxlen = 0x100 - blk_off;
214 if (maxlen > I2C_RXTX_LEN)
215 maxlen = I2C_RXTX_LEN;
221 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 0);
226 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
227 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
230 #if defined(CONFIG_SYS_EEPROM_WREN)
231 eeprom_write_enable (dev_addr,0);
236 static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
238 const char *const fmt =
239 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
240 char * const *args = &argv[2];
242 ulong dev_addr, addr, off, cnt;
245 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
247 dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
251 dev_addr = simple_strtoul(*args++, NULL, 16);
254 return CMD_RET_USAGE;
257 addr = simple_strtoul(*args++, NULL, 16);
258 off = simple_strtoul(*args++, NULL, 16);
259 cnt = simple_strtoul(*args++, NULL, 16);
261 # if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
263 # endif /* !CONFIG_SPI */
265 if (strcmp (argv[1], "read") == 0) {
266 printf(fmt, dev_addr, argv[1], addr, off, cnt);
268 rcode = eeprom_read(dev_addr, off, (uchar *) addr, cnt);
272 } else if (strcmp (argv[1], "write") == 0) {
273 printf(fmt, dev_addr, argv[1], addr, off, cnt);
275 rcode = eeprom_write(dev_addr, off, (uchar *) addr, cnt);
281 return CMD_RET_USAGE;
285 eeprom, 6, 1, do_eeprom,
287 "read devaddr addr off cnt\n"
288 "eeprom write devaddr addr off cnt\n"
289 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"