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 /* Maximum number of times to poll for acknowledge after write */
33 #if defined(CONFIG_SYS_EEPROM_X40430)
34 #define MAX_ACKNOWLEDGE_POLLS 10
38 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
39 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
41 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
42 * 0x00000nxx for EEPROM address selectors and page number at n.
44 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
45 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
46 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
47 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
48 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
52 #if defined(CONFIG_SYS_EEPROM_WREN)
53 extern 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 int eeprom_read (unsigned dev_addr
, unsigned offset
, uchar
*buffer
, unsigned cnt
)
71 unsigned end
= offset
+ cnt
;
76 * Read data until done or would cross a page boundary.
77 * We must write the address again when changing pages
78 * because the next page may be in a different device.
80 while (offset
< end
) {
82 #if !defined(CONFIG_SYS_I2C_FRAM)
86 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
89 blk_off
= offset
& 0xFF; /* block offset */
91 addr
[0] = offset
>> 8; /* block number */
92 addr
[1] = blk_off
; /* block offset */
97 blk_off
= offset
& 0xFF; /* block offset */
99 addr
[0] = offset
>> 16; /* block number */
100 addr
[1] = offset
>> 8; /* upper address octet */
101 addr
[2] = blk_off
; /* lower address octet */
103 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
105 addr
[0] |= dev_addr
; /* insert device address */
110 * For a FRAM device there is no limit on the number of the
111 * bytes that can be ccessed with the single read or write
114 #if !defined(CONFIG_SYS_I2C_FRAM)
115 maxlen
= 0x100 - blk_off
;
116 if (maxlen
> I2C_RXTX_LEN
)
117 maxlen
= I2C_RXTX_LEN
;
122 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
123 spi_read (addr
, alen
, buffer
, len
);
125 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
126 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS
);
128 if (i2c_read(addr
[0], offset
, alen
- 1, buffer
, len
))
138 int eeprom_write (unsigned dev_addr
, unsigned offset
, uchar
*buffer
, unsigned cnt
)
140 unsigned end
= offset
+ cnt
;
144 #if defined(CONFIG_SYS_EEPROM_X40430)
145 uchar contr_r_addr
[2];
152 #if defined(CONFIG_SYS_EEPROM_WREN)
153 eeprom_write_enable (dev_addr
,1);
156 * Write data until done or would cross a write page boundary.
157 * We must write the address again when changing pages
158 * because the address counter only increments within a page.
161 while (offset
< end
) {
163 #if !defined(CONFIG_SYS_I2C_FRAM)
167 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
170 blk_off
= offset
& 0xFF; /* block offset */
172 addr
[0] = offset
>> 8; /* block number */
173 addr
[1] = blk_off
; /* block offset */
178 blk_off
= offset
& 0xFF; /* block offset */
180 addr
[0] = offset
>> 16; /* block number */
181 addr
[1] = offset
>> 8; /* upper address octet */
182 addr
[2] = blk_off
; /* lower address octet */
184 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
186 addr
[0] |= dev_addr
; /* insert device address */
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 #if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
214 spi_write (addr
, alen
, buffer
, len
);
216 #if defined(CONFIG_SYS_EEPROM_X40430)
217 /* Get the value of the control register.
218 * Set current address (internal pointer in the x40430)
222 contr_r_addr
[1] = 0xff;
224 addr_void
[1] = addr
[1];
225 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR
226 contr_r_addr
[0] |= CONFIG_SYS_I2C_EEPROM_ADDR
;
227 addr_void
[0] |= CONFIG_SYS_I2C_EEPROM_ADDR
;
230 if (i2c_read (contr_r_addr
[0], contr_r_addr
[1], 1, contr_reg
, 1) != 0) {
233 ctrl_reg_v
= contr_reg
[0];
235 /* Are any of the eeprom blocks write protected?
237 if (ctrl_reg_v
& 0x18) {
238 ctrl_reg_v
&= ~0x18; /* reset block protect bits */
239 ctrl_reg_v
|= 0x02; /* set write enable latch */
240 ctrl_reg_v
&= ~0x04; /* clear RWEL */
242 /* Set write enable latch.
245 if (i2c_write (contr_r_addr
[0], 0xff, 1, contr_reg
, 1) != 0) {
249 /* Set register write enable latch.
252 if (i2c_write (contr_r_addr
[0], 0xFF, 1, contr_reg
, 1) != 0) {
256 /* Modify ctrl register.
258 contr_reg
[0] = ctrl_reg_v
;
259 if (i2c_write (contr_r_addr
[0], 0xFF, 1, contr_reg
, 1) != 0) {
263 /* The write (above) is an operation on NV memory.
264 * These can take some time (~5ms), and the device
265 * will not respond to further I2C messages till
266 * it's completed the write.
267 * So poll device for an I2C acknowledge.
268 * When we get one we know we can continue with other
272 for (i
= 0; i
< MAX_ACKNOWLEDGE_POLLS
; i
++) {
273 if (i2c_read (addr_void
[0], addr_void
[1], 1, contr_reg
, 1) == 0)
275 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
276 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
* 1000);
279 if (i
== MAX_ACKNOWLEDGE_POLLS
) {
280 puts ("EEPROM poll acknowledge failed\n");
285 /* Is the write enable latch on?.
287 else if (!(ctrl_reg_v
& 0x02)) {
288 /* Set write enable latch.
291 if (i2c_write (contr_r_addr
[0], 0xFF, 1, contr_reg
, 1) != 0) {
295 /* Write is enabled ... now write eeprom value.
298 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
299 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS
);
301 if (i2c_write(addr
[0], offset
, alen
- 1, buffer
, len
))
308 #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
309 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
* 1000);
312 #if defined(CONFIG_SYS_EEPROM_WREN)
313 eeprom_write_enable (dev_addr
,0);
318 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
319 int eeprom_probe(unsigned dev_addr
, unsigned offset
)
323 /* Probe the chip address
325 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
326 chip
= offset
>> 8; /* block number */
328 chip
= offset
>> 16; /* block number */
329 #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
331 chip
|= dev_addr
; /* insert device address */
333 return (i2c_probe (chip
));
337 static int do_eeprom(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
339 const char *const fmt
=
340 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
342 #if defined(CONFIG_SYS_I2C_MULTI_EEPROMS)
344 ulong dev_addr
= simple_strtoul (argv
[2], NULL
, 16);
345 ulong addr
= simple_strtoul (argv
[3], NULL
, 16);
346 ulong off
= simple_strtoul (argv
[4], NULL
, 16);
347 ulong cnt
= simple_strtoul (argv
[5], NULL
, 16);
350 ulong dev_addr
= CONFIG_SYS_DEF_EEPROM_ADDR
;
351 ulong addr
= simple_strtoul (argv
[2], NULL
, 16);
352 ulong off
= simple_strtoul (argv
[3], NULL
, 16);
353 ulong cnt
= simple_strtoul (argv
[4], NULL
, 16);
354 #endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */
356 # if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
358 # endif /* !CONFIG_SPI */
360 if (strcmp (argv
[1], "read") == 0) {
363 printf (fmt
, dev_addr
, argv
[1], addr
, off
, cnt
);
365 rcode
= eeprom_read (dev_addr
, off
, (uchar
*) addr
, cnt
);
369 } else if (strcmp (argv
[1], "write") == 0) {
372 printf (fmt
, dev_addr
, argv
[1], addr
, off
, cnt
);
374 rcode
= eeprom_write (dev_addr
, off
, (uchar
*) addr
, cnt
);
381 return CMD_RET_USAGE
;
384 #ifdef CONFIG_SYS_I2C_MULTI_EEPROMS
386 eeprom
, 6, 1, do_eeprom
,
388 "read devaddr addr off cnt\n"
389 "eeprom write devaddr addr off cnt\n"
390 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
392 #else /* One EEPROM */
394 eeprom
, 5, 1, do_eeprom
,
396 "read addr off cnt\n"
397 "eeprom write addr off cnt\n"
398 " - read/write `cnt' bytes at EEPROM offset `off'"
400 #endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */