blob: e5457ba0cf44d82344517a1b9b4fbaa1d1853f96 [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2000, 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00006 */
7
m8a484c602005-08-12 21:16:13 +02008/*
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
Peter Meerwaldc48ae8c2012-02-08 05:31:53 +000012 * write delay. Also, there is no limit imposed on the number of bytes that can
m8a484c602005-08-12 21:16:13 +020013 * be transferred with a single read or write.
Wolfgang Denke4e5e4e2005-08-19 00:46:54 +020014 *
m8a484c602005-08-12 21:16:13 +020015 * Use the following configuration options to ensure no unneeded performance
16 * degradation (typical for EEPROM) is incured for FRAM memory:
Wolfgang Denke4e5e4e2005-08-19 00:46:54 +020017 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020018 * #define CONFIG_SYS_I2C_FRAM
19 * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
m8a484c602005-08-12 21:16:13 +020020 *
21 */
22
wdenk38635852002-08-27 05:55:31 +000023#include <common.h>
24#include <config.h>
25#include <command.h>
26#include <i2c.h>
27
Marek Vasut704a0962015-11-10 20:53:18 +010028#ifndef CONFIG_SYS_I2C_SPEED
29#define CONFIG_SYS_I2C_SPEED 50000
Stefan Roesed9d97742005-09-22 09:04:17 +020030#endif
wdenk38635852002-08-27 05:55:31 +000031
Marek Vasutd848b082015-11-10 20:53:26 +010032#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
33#define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
34#endif
35
Marek Vasut42086792015-11-10 20:53:28 +010036#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
37#define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS 8
38#endif
39
Mario Sixdf0a9142016-04-20 10:44:52 +020040#ifndef I2C_RXTX_LEN
41#define I2C_RXTX_LEN 128
42#endif
43
Marek Vasut42086792015-11-10 20:53:28 +010044#define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
45#define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
46
Marek Vasut704a0962015-11-10 20:53:18 +010047/*
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020048 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
wdenk38635852002-08-27 05:55:31 +000049 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
50 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020051 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
wdenk38635852002-08-27 05:55:31 +000052 * 0x00000nxx for EEPROM address selectors and page number at n.
53 */
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010054#if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
Marek Vasut704a0962015-11-10 20:53:18 +010055#if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
Marek Vasuta24ea7a2015-11-10 20:53:33 +010056 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
57 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020058#error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
wdenk38635852002-08-27 05:55:31 +000059#endif
Marek Vasut704a0962015-11-10 20:53:18 +010060#endif
61
Marek Vasut2a632ffc2015-11-10 20:53:25 +010062__weak int eeprom_write_enable(unsigned dev_addr, int state)
63{
64 return 0;
65}
Marek Vasut704a0962015-11-10 20:53:18 +010066
Marek Vasutf422ae72015-11-10 20:53:31 +010067void eeprom_init(int bus)
Marek Vasut704a0962015-11-10 20:53:18 +010068{
69 /* SPI EEPROM */
70#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
Marek Vasuta1d4aeb2015-11-10 20:53:27 +010071 spi_init_f();
Marek Vasut704a0962015-11-10 20:53:18 +010072#endif
73
74 /* I2C EEPROM */
75#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
Marek Vasutf422ae72015-11-10 20:53:31 +010076#if defined(CONFIG_SYS_I2C)
77 if (bus >= 0)
78 i2c_set_bus_num(bus);
79#endif
Marek Vasut704a0962015-11-10 20:53:18 +010080 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenk38635852002-08-27 05:55:31 +000081#endif
Marek Vasut704a0962015-11-10 20:53:18 +010082}
wdenk38635852002-08-27 05:55:31 +000083
Marek Vasut6ae59572015-11-10 20:53:24 +010084static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
85{
86 unsigned blk_off;
87 int alen;
88
89 blk_off = offset & 0xff; /* block offset */
90#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
91 addr[0] = offset >> 8; /* block number */
92 addr[1] = blk_off; /* block offset */
93 alen = 2;
94#else
95 addr[0] = offset >> 16; /* block number */
96 addr[1] = offset >> 8; /* upper address octet */
97 addr[2] = blk_off; /* lower address octet */
98 alen = 3;
99#endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
100
101 addr[0] |= dev_addr; /* insert device address */
102
103 return alen;
104}
105
Marek Vasutd062c482015-11-10 20:53:29 +0100106static int eeprom_len(unsigned offset, unsigned end)
107{
108 unsigned len = end - offset;
109
110 /*
111 * For a FRAM device there is no limit on the number of the
112 * bytes that can be ccessed with the single read or write
113 * operation.
114 */
115#if !defined(CONFIG_SYS_I2C_FRAM)
116 unsigned blk_off = offset & 0xff;
117 unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
118
119 if (maxlen > I2C_RXTX_LEN)
120 maxlen = I2C_RXTX_LEN;
121
122 if (len > maxlen)
123 len = maxlen;
124#endif
125
126 return len;
127}
128
Marek Vasut78990c72015-11-10 20:53:23 +0100129static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
130 uchar *buffer, unsigned len, bool read)
131{
132 int ret = 0;
133
134 /* SPI */
135#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
136 if (read)
137 spi_read(addr, alen, buffer, len);
138 else
139 spi_write(addr, alen, buffer, len);
140#else /* I2C */
141
142#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
143 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
144#endif
145
146 if (read)
147 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
148 else
149 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
150
151 if (ret)
152 ret = 1;
153#endif
154 return ret;
155}
156
Marek Vasut7a237212015-11-10 20:53:30 +0100157static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
158 unsigned cnt, bool read)
wdenk38635852002-08-27 05:55:31 +0000159{
160 unsigned end = offset + cnt;
Marek Vasut7a237212015-11-10 20:53:30 +0100161 unsigned alen, len;
wdenk38635852002-08-27 05:55:31 +0000162 int rcode = 0;
Marek Vasut6ae59572015-11-10 20:53:24 +0100163 uchar addr[3];
wdenk38635852002-08-27 05:55:31 +0000164
wdenk38635852002-08-27 05:55:31 +0000165 while (offset < end) {
Marek Vasut6ae59572015-11-10 20:53:24 +0100166 alen = eeprom_addr(dev_addr, offset, addr);
wdenk38635852002-08-27 05:55:31 +0000167
Marek Vasutd062c482015-11-10 20:53:29 +0100168 len = eeprom_len(offset, end);
m8a484c602005-08-12 21:16:13 +0200169
Marek Vasut7a237212015-11-10 20:53:30 +0100170 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
Marek Vasut78990c72015-11-10 20:53:23 +0100171
wdenk38635852002-08-27 05:55:31 +0000172 buffer += len;
173 offset += len;
Marek Vasut7a237212015-11-10 20:53:30 +0100174
175 if (!read)
176 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
wdenk38635852002-08-27 05:55:31 +0000177 }
m8a484c602005-08-12 21:16:13 +0200178
wdenk38635852002-08-27 05:55:31 +0000179 return rcode;
180}
181
Marek Vasut7a237212015-11-10 20:53:30 +0100182int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
wdenk38635852002-08-27 05:55:31 +0000183{
Marek Vasut7a237212015-11-10 20:53:30 +0100184 /*
185 * Read data until done or would cross a page boundary.
186 * We must write the address again when changing pages
187 * because the next page may be in a different device.
188 */
189 return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
190}
191
Marek Vasuta24ea7a2015-11-10 20:53:33 +0100192int eeprom_write(unsigned dev_addr, unsigned offset,
193 uchar *buffer, unsigned cnt)
Marek Vasut7a237212015-11-10 20:53:30 +0100194{
195 int ret;
wdenk38635852002-08-27 05:55:31 +0000196
Marek Vasut2a632ffc2015-11-10 20:53:25 +0100197 eeprom_write_enable(dev_addr, 1);
198
Marek Vasut704a0962015-11-10 20:53:18 +0100199 /*
200 * Write data until done or would cross a write page boundary.
wdenk38635852002-08-27 05:55:31 +0000201 * We must write the address again when changing pages
202 * because the address counter only increments within a page.
203 */
Alexey Brodkin90eed7f2015-12-14 18:45:34 +0300204 ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
Marek Vasut2a632ffc2015-11-10 20:53:25 +0100205
206 eeprom_write_enable(dev_addr, 0);
Marek Vasut7a237212015-11-10 20:53:30 +0100207 return ret;
wdenk38635852002-08-27 05:55:31 +0000208}
209
Marek Vasut704a0962015-11-10 20:53:18 +0100210static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000211{
Marek Vasut704a0962015-11-10 20:53:18 +0100212 const char *const fmt =
213 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
Marek Vasutee541442015-11-10 20:53:19 +0100214 char * const *args = &argv[2];
215 int rcode;
216 ulong dev_addr, addr, off, cnt;
Marek Vasut4d17f712015-11-10 20:53:32 +0100217 int bus_addr;
Heiko Schocher9bb0dd52010-01-07 08:55:40 +0100218
Marek Vasutee541442015-11-10 20:53:19 +0100219 switch (argc) {
220#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
221 case 5:
Marek Vasut4d17f712015-11-10 20:53:32 +0100222 bus_addr = -1;
Marek Vasutee541442015-11-10 20:53:19 +0100223 dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
224 break;
225#endif
226 case 6:
Marek Vasut4d17f712015-11-10 20:53:32 +0100227 bus_addr = -1;
Marek Vasutee541442015-11-10 20:53:19 +0100228 dev_addr = simple_strtoul(*args++, NULL, 16);
229 break;
Marek Vasut4d17f712015-11-10 20:53:32 +0100230 case 7:
231 bus_addr = simple_strtoul(*args++, NULL, 16);
232 dev_addr = simple_strtoul(*args++, NULL, 16);
233 break;
Marek Vasutee541442015-11-10 20:53:19 +0100234 default:
235 return CMD_RET_USAGE;
236 }
237
238 addr = simple_strtoul(*args++, NULL, 16);
239 off = simple_strtoul(*args++, NULL, 16);
240 cnt = simple_strtoul(*args++, NULL, 16);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +0100241
Marek Vasut4d17f712015-11-10 20:53:32 +0100242 eeprom_init(bus_addr);
Jon Loeligerd704d912007-07-10 11:02:44 -0500243
Marek Vasuta24ea7a2015-11-10 20:53:33 +0100244 if (strcmp(argv[1], "read") == 0) {
Marek Vasutee541442015-11-10 20:53:19 +0100245 printf(fmt, dev_addr, argv[1], addr, off, cnt);
Marek Vasut704a0962015-11-10 20:53:18 +0100246
Marek Vasuta24ea7a2015-11-10 20:53:33 +0100247 rcode = eeprom_read(dev_addr, off, (uchar *)addr, cnt);
wdenk57b2d802003-06-27 21:31:46 +0000248
Marek Vasuta24ea7a2015-11-10 20:53:33 +0100249 puts("done\n");
Marek Vasutee541442015-11-10 20:53:19 +0100250 return rcode;
Marek Vasuta24ea7a2015-11-10 20:53:33 +0100251 } else if (strcmp(argv[1], "write") == 0) {
Marek Vasutee541442015-11-10 20:53:19 +0100252 printf(fmt, dev_addr, argv[1], addr, off, cnt);
Marek Vasut704a0962015-11-10 20:53:18 +0100253
Marek Vasuta24ea7a2015-11-10 20:53:33 +0100254 rcode = eeprom_write(dev_addr, off, (uchar *)addr, cnt);
Marek Vasut704a0962015-11-10 20:53:18 +0100255
Marek Vasuta24ea7a2015-11-10 20:53:33 +0100256 puts("done\n");
Marek Vasutee541442015-11-10 20:53:19 +0100257 return rcode;
Marek Vasut704a0962015-11-10 20:53:18 +0100258 }
259
260 return CMD_RET_USAGE;
261}
wdenk57b2d802003-06-27 21:31:46 +0000262
wdenkf287a242003-07-01 21:06:45 +0000263U_BOOT_CMD(
Marek Vasut4d17f712015-11-10 20:53:32 +0100264 eeprom, 7, 1, do_eeprom,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600265 "EEPROM sub-system",
Marek Vasut4d17f712015-11-10 20:53:32 +0100266 "read <bus> <devaddr> addr off cnt\n"
267 "eeprom write <bus> <devaddr> addr off cnt\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200268 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +0200269)