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,
30 #if (CONFIG_COMMANDS & CFG_CMD_EEPROM) || defined(CFG_ENV_IS_IN_EEPROM)
32 extern void eeprom_init (void);
33 extern int eeprom_read (unsigned dev_addr, unsigned offset,
34 uchar *buffer, unsigned cnt);
35 extern int eeprom_write (unsigned dev_addr, unsigned offset,
36 uchar *buffer, unsigned cnt);
40 #if defined(CFG_EEPROM_X40430)
41 /* Maximum number of times to poll for acknowledge after write */
42 #define MAX_ACKNOWLEDGE_POLLS 10
45 /* ------------------------------------------------------------------------- */
47 #if (CONFIG_COMMANDS & CFG_CMD_EEPROM)
48 int do_eeprom ( cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
50 const char *const fmt =
51 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
53 #if defined(CFG_I2C_MULTI_EEPROMS)
55 ulong dev_addr = simple_strtoul (argv[2], NULL, 16);
56 ulong addr = simple_strtoul (argv[3], NULL, 16);
57 ulong off = simple_strtoul (argv[4], NULL, 16);
58 ulong cnt = simple_strtoul (argv[5], NULL, 16);
61 ulong dev_addr = CFG_DEF_EEPROM_ADDR;
62 ulong addr = simple_strtoul (argv[2], NULL, 16);
63 ulong off = simple_strtoul (argv[3], NULL, 16);
64 ulong cnt = simple_strtoul (argv[4], NULL, 16);
65 #endif /* CFG_I2C_MULTI_EEPROMS */
69 # endif /* !CONFIG_SPI */
71 if (strcmp (argv[1], "read") == 0) {
74 printf (fmt, dev_addr, argv[1], addr, off, cnt);
76 rcode = eeprom_read (dev_addr, off, (uchar *) addr, cnt);
80 } else if (strcmp (argv[1], "write") == 0) {
83 printf (fmt, dev_addr, argv[1], addr, off, cnt);
85 rcode = eeprom_write (dev_addr, off, (uchar *) addr, cnt);
92 printf ("Usage:\n%s\n", cmdtp->usage);
95 #endif /* CFG_CMD_EEPROM */
97 /*-----------------------------------------------------------------------
99 * for CFG_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
100 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
102 * for CFG_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
103 * 0x00000nxx for EEPROM address selectors and page number at n.
106 #if (CONFIG_COMMANDS & CFG_CMD_EEPROM) || defined(CFG_ENV_IS_IN_EEPROM)
109 #if !defined(CFG_I2C_EEPROM_ADDR_LEN) || CFG_I2C_EEPROM_ADDR_LEN < 1 || CFG_I2C_EEPROM_ADDR_LEN > 2
110 #error CFG_I2C_EEPROM_ADDR_LEN must be 1 or 2
114 int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
116 unsigned end = offset + cnt;
120 /* Read data until done or would cross a page boundary.
121 * We must write the address again when changing pages
122 * because the next page may be in a different device.
124 while (offset < end) {
125 unsigned alen, len, maxlen;
126 #if CFG_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
129 blk_off = offset & 0xFF; /* block offset */
131 addr[0] = offset >> 8; /* block number */
132 addr[1] = blk_off; /* block offset */
137 blk_off = offset & 0xFF; /* block offset */
139 addr[0] = offset >> 16; /* block number */
140 addr[1] = offset >> 8; /* upper address octet */
141 addr[2] = blk_off; /* lower address octet */
143 #endif /* CFG_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
145 addr[0] |= dev_addr; /* insert device address */
147 maxlen = 0x100 - blk_off;
148 if (maxlen > I2C_RXTX_LEN)
149 maxlen = I2C_RXTX_LEN;
154 spi_read (addr, alen, buffer, len);
156 if (i2c_read (addr[0], offset, alen-1, buffer, len) != 0)
165 /*-----------------------------------------------------------------------
167 * for CFG_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
168 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
170 * for CFG_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
171 * 0x00000nxx for EEPROM address selectors and page number at n.
174 int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
176 unsigned end = offset + cnt;
180 #if defined(CFG_EEPROM_X40430)
181 uchar contr_r_addr[2];
188 /* Write data until done or would cross a write page boundary.
189 * We must write the address again when changing pages
190 * because the address counter only increments within a page.
193 while (offset < end) {
194 unsigned alen, len, maxlen;
195 #if CFG_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
198 blk_off = offset & 0xFF; /* block offset */
200 addr[0] = offset >> 8; /* block number */
201 addr[1] = blk_off; /* block offset */
206 blk_off = offset & 0xFF; /* block offset */
208 addr[0] = offset >> 16; /* block number */
209 addr[1] = offset >> 8; /* upper address octet */
210 addr[2] = blk_off; /* lower address octet */
212 #endif /* CFG_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
214 addr[0] |= dev_addr; /* insert device address */
216 #if defined(CFG_EEPROM_PAGE_WRITE_BITS)
218 #define EEPROM_PAGE_SIZE (1 << CFG_EEPROM_PAGE_WRITE_BITS)
219 #define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
221 maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
223 maxlen = 0x100 - blk_off;
225 if (maxlen > I2C_RXTX_LEN)
226 maxlen = I2C_RXTX_LEN;
232 spi_write (addr, alen, buffer, len);
234 #if defined(CFG_EEPROM_X40430)
235 /* Get the value of the control register.
236 * Set current address (internal pointer in the x40430)
240 contr_r_addr[1] = 0xff;
242 addr_void[1] = addr[1];
243 #ifdef CFG_I2C_EEPROM_ADDR
244 contr_r_addr[0] |= CFG_I2C_EEPROM_ADDR;
245 addr_void[0] |= CFG_I2C_EEPROM_ADDR;
248 if (i2c_read (contr_r_addr[0], contr_r_addr[1], 1, contr_reg, 1) != 0) {
251 ctrl_reg_v = contr_reg[0];
253 /* Are any of the eeprom blocks write protected?
255 if (ctrl_reg_v & 0x18) {
256 ctrl_reg_v &= ~0x18; /* reset block protect bits */
257 ctrl_reg_v |= 0x02; /* set write enable latch */
258 ctrl_reg_v &= ~0x04; /* clear RWEL */
260 /* Set write enable latch.
263 if (i2c_write (contr_r_addr[0], 0xff, 1, contr_reg, 1) != 0) {
267 /* Set register write enable latch.
270 if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
274 /* Modify ctrl register.
276 contr_reg[0] = ctrl_reg_v;
277 if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
281 /* The write (above) is an operation on NV memory.
282 * These can take some time (~5ms), and the device
283 * will not respond to further I2C messages till
284 * it's completed the write.
285 * So poll device for an I2C acknowledge.
286 * When we get one we know we can continue with other
290 for (i = 0; i < MAX_ACKNOWLEDGE_POLLS; i++) {
291 if (i2c_read (addr_void[0], addr_void[1], 1, contr_reg, 1) == 0)
293 #if defined(CFG_EEPROM_PAGE_WRITE_DELAY_MS)
294 udelay(CFG_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
297 if (i == MAX_ACKNOWLEDGE_POLLS) {
298 puts ("EEPROM poll acknowledge failed\n");
303 /* Is the write enable latch on?.
305 else if (!(ctrl_reg_v & 0x02)) {
306 /* Set write enable latch.
309 if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
313 /* Write is enabled ... now write eeprom value.
316 if (i2c_write (addr[0], offset, alen-1, buffer, len) != 0)
323 #if defined(CFG_EEPROM_PAGE_WRITE_DELAY_MS)
324 udelay(CFG_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
332 eeprom_probe (unsigned dev_addr, unsigned offset)
336 /* Probe the chip address
338 #if CFG_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
339 chip = offset >> 8; /* block number */
341 chip = offset >> 16; /* block number */
342 #endif /* CFG_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
344 chip |= dev_addr; /* insert device address */
346 return (i2c_probe (chip));
350 /*-----------------------------------------------------------------------
353 #ifndef CFG_I2C_SPEED
354 #define CFG_I2C_SPEED 50000
357 #ifndef CFG_I2C_SLAVE
358 #define CFG_I2C_SLAVE 0xFE
361 void eeprom_init (void)
363 #if defined(CONFIG_SPI)
366 #if defined(CONFIG_HARD_I2C) || \
367 defined(CONFIG_SOFT_I2C)
368 i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
371 /*-----------------------------------------------------------------------
373 #endif /* CFG_CMD_EEPROM */
374 /***************************************************/
376 #if (CONFIG_COMMANDS & CFG_CMD_EEPROM)
378 #ifdef CFG_I2C_MULTI_EEPROMS
380 eeprom, 6, 1, do_eeprom,
381 "eeprom - EEPROM sub-system\n",
382 "read devaddr addr off cnt\n"
383 "eeprom write devaddr addr off cnt\n"
384 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'\n"
386 #else /* One EEPROM */
388 eeprom, 5, 1, do_eeprom,
389 "eeprom - EEPROM sub-system\n",
390 "read addr off cnt\n"
391 "eeprom write addr off cnt\n"
392 " - read/write `cnt' bytes at EEPROM offset `off'\n"
394 #endif /* CFG_I2C_MULTI_EEPROMS */
396 #endif /* CFG_CMD_EEPROM */