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
36 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
37 #define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8
40 #define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
41 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
44 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
45 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
47 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
48 * 0x00000nxx for EEPROM address selectors and page number at n.
50 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
51 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
52 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
53 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
54 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
58 __weak
int eeprom_write_enable(unsigned dev_addr
, int state
)
63 void eeprom_init(void)
66 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
71 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
72 i2c_init(CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
);
76 static int eeprom_addr(unsigned dev_addr
, unsigned offset
, uchar
*addr
)
81 blk_off
= offset
& 0xff; /* block offset */
82 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
83 addr
[0] = offset
>> 8; /* block number */
84 addr
[1] = blk_off
; /* block offset */
87 addr
[0] = offset
>> 16; /* block number */
88 addr
[1] = offset
>> 8; /* upper address octet */
89 addr
[2] = blk_off
; /* lower address octet */
91 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
93 addr
[0] |= dev_addr
; /* insert device address */
98 static int eeprom_len(unsigned offset
, unsigned end
)
100 unsigned len
= end
- offset
;
103 * For a FRAM device there is no limit on the number of the
104 * bytes that can be ccessed with the single read or write
107 #if !defined(CONFIG_SYS_I2C_FRAM)
108 unsigned blk_off
= offset
& 0xff;
109 unsigned maxlen
= EEPROM_PAGE_SIZE
- EEPROM_PAGE_OFFSET(blk_off
);
111 if (maxlen
> I2C_RXTX_LEN
)
112 maxlen
= I2C_RXTX_LEN
;
121 static int eeprom_rw_block(unsigned offset
, uchar
*addr
, unsigned alen
,
122 uchar
*buffer
, unsigned len
, bool read
)
127 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
129 spi_read(addr
, alen
, buffer
, len
);
131 spi_write(addr
, alen
, buffer
, len
);
134 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
135 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS
);
139 ret
= i2c_read(addr
[0], offset
, alen
- 1, buffer
, len
);
141 ret
= i2c_write(addr
[0], offset
, alen
- 1, buffer
, len
);
149 static int eeprom_rw(unsigned dev_addr
, unsigned offset
, uchar
*buffer
,
150 unsigned cnt
, bool read
)
152 unsigned end
= offset
+ cnt
;
157 while (offset
< end
) {
158 alen
= eeprom_addr(dev_addr
, offset
, addr
);
160 len
= eeprom_len(offset
, end
);
162 rcode
= eeprom_rw_block(offset
, addr
, alen
, buffer
, len
, read
);
168 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
* 1000);
174 int eeprom_read(unsigned dev_addr
, unsigned offset
, uchar
*buffer
, unsigned cnt
)
177 * Read data until done or would cross a page boundary.
178 * We must write the address again when changing pages
179 * because the next page may be in a different device.
181 return eeprom_rw(dev_addr
, offset
, buffer
, cnt
, 1);
184 int eeprom_write(unsigned dev_addr
, unsigned offset
, uchar
*buffer
, unsigned cnt
)
188 eeprom_write_enable(dev_addr
, 1);
191 * Write data until done or would cross a write page boundary.
192 * We must write the address again when changing pages
193 * because the address counter only increments within a page.
195 ret
= eeprom_rw(dev_addr
, offset
, buffer
, cnt
, 1);
197 eeprom_write_enable(dev_addr
, 0);
201 static int do_eeprom(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
203 const char *const fmt
=
204 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
205 char * const *args
= &argv
[2];
207 ulong dev_addr
, addr
, off
, cnt
;
210 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
212 dev_addr
= CONFIG_SYS_DEF_EEPROM_ADDR
;
216 dev_addr
= simple_strtoul(*args
++, NULL
, 16);
219 return CMD_RET_USAGE
;
222 addr
= simple_strtoul(*args
++, NULL
, 16);
223 off
= simple_strtoul(*args
++, NULL
, 16);
224 cnt
= simple_strtoul(*args
++, NULL
, 16);
228 if (strcmp (argv
[1], "read") == 0) {
229 printf(fmt
, dev_addr
, argv
[1], addr
, off
, cnt
);
231 rcode
= eeprom_read(dev_addr
, off
, (uchar
*) addr
, cnt
);
235 } else if (strcmp (argv
[1], "write") == 0) {
236 printf(fmt
, dev_addr
, argv
[1], addr
, off
, cnt
);
238 rcode
= eeprom_write(dev_addr
, off
, (uchar
*) addr
, cnt
);
244 return CMD_RET_USAGE
;
248 eeprom
, 6, 1, do_eeprom
,
250 "read devaddr addr off cnt\n"
251 "eeprom write devaddr addr off cnt\n"
252 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"