2002-08-27 05:55:31 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2000, 2001
|
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2002-08-27 05:55:31 +00:00
|
|
|
*/
|
|
|
|
|
2005-08-12 19:16:13 +00:00
|
|
|
/*
|
|
|
|
* Support for read and write access to EEPROM like memory devices. This
|
|
|
|
* includes regular EEPROM as well as FRAM (ferroelectic nonvolaile RAM).
|
|
|
|
* FRAM devices read and write data at bus speed. In particular, there is no
|
2012-02-08 05:31:53 +00:00
|
|
|
* write delay. Also, there is no limit imposed on the number of bytes that can
|
2005-08-12 19:16:13 +00:00
|
|
|
* be transferred with a single read or write.
|
2005-08-18 22:46:54 +00:00
|
|
|
*
|
2005-08-12 19:16:13 +00:00
|
|
|
* Use the following configuration options to ensure no unneeded performance
|
|
|
|
* degradation (typical for EEPROM) is incured for FRAM memory:
|
2005-08-18 22:46:54 +00:00
|
|
|
*
|
2008-10-16 13:01:15 +00:00
|
|
|
* #define CONFIG_SYS_I2C_FRAM
|
|
|
|
* #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
|
2005-08-12 19:16:13 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-08-27 05:55:31 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <config.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <i2c.h>
|
|
|
|
|
2015-11-10 19:53:18 +00:00
|
|
|
#ifndef CONFIG_SYS_I2C_SPEED
|
|
|
|
#define CONFIG_SYS_I2C_SPEED 50000
|
2005-09-22 07:04:17 +00:00
|
|
|
#endif
|
2002-08-27 05:55:31 +00:00
|
|
|
|
2015-11-10 19:53:26 +00:00
|
|
|
#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
|
|
|
|
#define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
|
|
|
|
#endif
|
|
|
|
|
2015-11-10 19:53:28 +00:00
|
|
|
#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
|
|
|
|
#define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8
|
|
|
|
#endif
|
|
|
|
|
2016-04-20 08:44:52 +00:00
|
|
|
#ifndef I2C_RXTX_LEN
|
|
|
|
#define I2C_RXTX_LEN 128
|
|
|
|
#endif
|
|
|
|
|
2015-11-10 19:53:28 +00:00
|
|
|
#define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
|
|
|
|
#define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
|
|
|
|
|
2015-11-10 19:53:18 +00:00
|
|
|
/*
|
2008-10-16 13:01:15 +00:00
|
|
|
* for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
|
2002-08-27 05:55:31 +00:00
|
|
|
* 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
|
|
|
|
*
|
2008-10-16 13:01:15 +00:00
|
|
|
* for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
|
2002-08-27 05:55:31 +00:00
|
|
|
* 0x00000nxx for EEPROM address selectors and page number at n.
|
|
|
|
*/
|
2010-01-07 07:55:40 +00:00
|
|
|
#if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
|
2015-11-10 19:53:18 +00:00
|
|
|
#if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
|
2015-11-10 19:53:33 +00:00
|
|
|
(CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
|
|
|
|
(CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
|
2008-10-16 13:01:15 +00:00
|
|
|
#error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
|
2002-08-27 05:55:31 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-11-10 19:53:25 +00:00
|
|
|
__weak int eeprom_write_enable(unsigned dev_addr, int state)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2015-11-10 19:53:18 +00:00
|
|
|
|
2015-11-10 19:53:31 +00:00
|
|
|
void eeprom_init(int bus)
|
2015-11-10 19:53:18 +00:00
|
|
|
{
|
|
|
|
/* SPI EEPROM */
|
|
|
|
#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
|
2015-11-10 19:53:27 +00:00
|
|
|
spi_init_f();
|
2015-11-10 19:53:18 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* I2C EEPROM */
|
2016-04-16 14:55:02 +00:00
|
|
|
#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
|
2015-11-10 19:53:31 +00:00
|
|
|
#if defined(CONFIG_SYS_I2C)
|
|
|
|
if (bus >= 0)
|
|
|
|
i2c_set_bus_num(bus);
|
|
|
|
#endif
|
2015-11-10 19:53:18 +00:00
|
|
|
i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-11-10 19:53:24 +00:00
|
|
|
static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
|
|
|
|
{
|
|
|
|
unsigned blk_off;
|
|
|
|
int alen;
|
|
|
|
|
|
|
|
blk_off = offset & 0xff; /* block offset */
|
|
|
|
#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
|
|
|
|
addr[0] = offset >> 8; /* block number */
|
|
|
|
addr[1] = blk_off; /* block offset */
|
|
|
|
alen = 2;
|
|
|
|
#else
|
|
|
|
addr[0] = offset >> 16; /* block number */
|
|
|
|
addr[1] = offset >> 8; /* upper address octet */
|
|
|
|
addr[2] = blk_off; /* lower address octet */
|
|
|
|
alen = 3;
|
|
|
|
#endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
|
|
|
|
|
|
|
|
addr[0] |= dev_addr; /* insert device address */
|
|
|
|
|
|
|
|
return alen;
|
|
|
|
}
|
|
|
|
|
2015-11-10 19:53:29 +00:00
|
|
|
static int eeprom_len(unsigned offset, unsigned end)
|
|
|
|
{
|
|
|
|
unsigned len = end - offset;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For a FRAM device there is no limit on the number of the
|
|
|
|
* bytes that can be ccessed with the single read or write
|
|
|
|
* operation.
|
|
|
|
*/
|
|
|
|
#if !defined(CONFIG_SYS_I2C_FRAM)
|
|
|
|
unsigned blk_off = offset & 0xff;
|
|
|
|
unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
|
|
|
|
|
|
|
|
if (maxlen > I2C_RXTX_LEN)
|
|
|
|
maxlen = I2C_RXTX_LEN;
|
|
|
|
|
|
|
|
if (len > maxlen)
|
|
|
|
len = maxlen;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-11-10 19:53:23 +00:00
|
|
|
static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
|
|
|
|
uchar *buffer, unsigned len, bool read)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* SPI */
|
|
|
|
#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
|
|
|
|
if (read)
|
|
|
|
spi_read(addr, alen, buffer, len);
|
|
|
|
else
|
|
|
|
spi_write(addr, alen, buffer, len);
|
|
|
|
#else /* I2C */
|
|
|
|
|
|
|
|
#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
|
|
|
|
i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (read)
|
|
|
|
ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
|
|
|
|
else
|
|
|
|
ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
ret = 1;
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-11-10 19:53:30 +00:00
|
|
|
static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
|
|
|
|
unsigned cnt, bool read)
|
2002-08-27 05:55:31 +00:00
|
|
|
{
|
|
|
|
unsigned end = offset + cnt;
|
2015-11-10 19:53:30 +00:00
|
|
|
unsigned alen, len;
|
2002-08-27 05:55:31 +00:00
|
|
|
int rcode = 0;
|
2015-11-10 19:53:24 +00:00
|
|
|
uchar addr[3];
|
2002-08-27 05:55:31 +00:00
|
|
|
|
|
|
|
while (offset < end) {
|
2015-11-10 19:53:24 +00:00
|
|
|
alen = eeprom_addr(dev_addr, offset, addr);
|
2002-08-27 05:55:31 +00:00
|
|
|
|
2015-11-10 19:53:29 +00:00
|
|
|
len = eeprom_len(offset, end);
|
2005-08-12 19:16:13 +00:00
|
|
|
|
2015-11-10 19:53:30 +00:00
|
|
|
rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
|
2015-11-10 19:53:23 +00:00
|
|
|
|
2002-08-27 05:55:31 +00:00
|
|
|
buffer += len;
|
|
|
|
offset += len;
|
2015-11-10 19:53:30 +00:00
|
|
|
|
|
|
|
if (!read)
|
|
|
|
udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
|
2002-08-27 05:55:31 +00:00
|
|
|
}
|
2005-08-12 19:16:13 +00:00
|
|
|
|
2002-08-27 05:55:31 +00:00
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
2015-11-10 19:53:30 +00:00
|
|
|
int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
|
2002-08-27 05:55:31 +00:00
|
|
|
{
|
2015-11-10 19:53:30 +00:00
|
|
|
/*
|
|
|
|
* Read data until done or would cross a page boundary.
|
|
|
|
* We must write the address again when changing pages
|
|
|
|
* because the next page may be in a different device.
|
|
|
|
*/
|
|
|
|
return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
|
|
|
|
}
|
|
|
|
|
2015-11-10 19:53:33 +00:00
|
|
|
int eeprom_write(unsigned dev_addr, unsigned offset,
|
|
|
|
uchar *buffer, unsigned cnt)
|
2015-11-10 19:53:30 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2002-08-27 05:55:31 +00:00
|
|
|
|
2015-11-10 19:53:25 +00:00
|
|
|
eeprom_write_enable(dev_addr, 1);
|
|
|
|
|
2015-11-10 19:53:18 +00:00
|
|
|
/*
|
|
|
|
* Write data until done or would cross a write page boundary.
|
2002-08-27 05:55:31 +00:00
|
|
|
* We must write the address again when changing pages
|
|
|
|
* because the address counter only increments within a page.
|
|
|
|
*/
|
2015-12-14 15:45:34 +00:00
|
|
|
ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
|
2015-11-10 19:53:25 +00:00
|
|
|
|
|
|
|
eeprom_write_enable(dev_addr, 0);
|
2015-11-10 19:53:30 +00:00
|
|
|
return ret;
|
2002-08-27 05:55:31 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 19:53:18 +00:00
|
|
|
static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
|
2002-08-27 05:55:31 +00:00
|
|
|
{
|
2015-11-10 19:53:18 +00:00
|
|
|
const char *const fmt =
|
|
|
|
"\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
|
2015-11-10 19:53:19 +00:00
|
|
|
char * const *args = &argv[2];
|
|
|
|
int rcode;
|
|
|
|
ulong dev_addr, addr, off, cnt;
|
2015-11-10 19:53:32 +00:00
|
|
|
int bus_addr;
|
2015-11-10 19:53:19 +00:00
|
|
|
|
|
|
|
switch (argc) {
|
|
|
|
#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
|
|
|
|
case 5:
|
2015-11-10 19:53:32 +00:00
|
|
|
bus_addr = -1;
|
2015-11-10 19:53:19 +00:00
|
|
|
dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 6:
|
2015-11-10 19:53:32 +00:00
|
|
|
bus_addr = -1;
|
|
|
|
dev_addr = simple_strtoul(*args++, NULL, 16);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
bus_addr = simple_strtoul(*args++, NULL, 16);
|
2015-11-10 19:53:19 +00:00
|
|
|
dev_addr = simple_strtoul(*args++, NULL, 16);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
2010-01-07 07:55:40 +00:00
|
|
|
|
2015-11-10 19:53:19 +00:00
|
|
|
addr = simple_strtoul(*args++, NULL, 16);
|
|
|
|
off = simple_strtoul(*args++, NULL, 16);
|
|
|
|
cnt = simple_strtoul(*args++, NULL, 16);
|
2010-01-07 07:55:40 +00:00
|
|
|
|
2015-11-10 19:53:32 +00:00
|
|
|
eeprom_init(bus_addr);
|
2007-07-10 16:02:44 +00:00
|
|
|
|
2015-11-10 19:53:33 +00:00
|
|
|
if (strcmp(argv[1], "read") == 0) {
|
2015-11-10 19:53:19 +00:00
|
|
|
printf(fmt, dev_addr, argv[1], addr, off, cnt);
|
2015-11-10 19:53:18 +00:00
|
|
|
|
2015-11-10 19:53:33 +00:00
|
|
|
rcode = eeprom_read(dev_addr, off, (uchar *)addr, cnt);
|
2015-11-10 19:53:18 +00:00
|
|
|
|
2015-11-10 19:53:33 +00:00
|
|
|
puts("done\n");
|
2015-11-10 19:53:19 +00:00
|
|
|
return rcode;
|
2015-11-10 19:53:33 +00:00
|
|
|
} else if (strcmp(argv[1], "write") == 0) {
|
2015-11-10 19:53:19 +00:00
|
|
|
printf(fmt, dev_addr, argv[1], addr, off, cnt);
|
2015-11-10 19:53:18 +00:00
|
|
|
|
2015-11-10 19:53:33 +00:00
|
|
|
rcode = eeprom_write(dev_addr, off, (uchar *)addr, cnt);
|
2015-11-10 19:53:18 +00:00
|
|
|
|
2015-11-10 19:53:33 +00:00
|
|
|
puts("done\n");
|
2015-11-10 19:53:19 +00:00
|
|
|
return rcode;
|
2015-11-10 19:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
2003-06-27 21:31:46 +00:00
|
|
|
|
2003-07-01 21:06:45 +00:00
|
|
|
U_BOOT_CMD(
|
2015-11-10 19:53:32 +00:00
|
|
|
eeprom, 7, 1, do_eeprom,
|
2009-01-28 00:03:12 +00:00
|
|
|
"EEPROM sub-system",
|
2015-11-10 19:53:32 +00:00
|
|
|
"read <bus> <devaddr> addr off cnt\n"
|
|
|
|
"eeprom write <bus> <devaddr> addr off cnt\n"
|
2009-05-24 15:06:54 +00:00
|
|
|
" - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
|
2014-06-22 22:22:08 +00:00
|
|
|
)
|