blob: 7797d0ec9c9c6446f961614f7baac53f64259378 [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 Vasut704a0962015-11-10 20:53:18 +010032/*
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020033 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
wdenk38635852002-08-27 05:55:31 +000034 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
35 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020036 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
wdenk38635852002-08-27 05:55:31 +000037 * 0x00000nxx for EEPROM address selectors and page number at n.
38 */
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010039#if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
Marek Vasut704a0962015-11-10 20:53:18 +010040#if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
41 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
42 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020043#error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
wdenk38635852002-08-27 05:55:31 +000044#endif
Marek Vasut704a0962015-11-10 20:53:18 +010045#endif
46
47#if defined(CONFIG_SYS_EEPROM_WREN)
48extern int eeprom_write_enable (unsigned dev_addr, int state);
49#endif
50
51void eeprom_init(void)
52{
53 /* SPI EEPROM */
54#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
55 spi_init_f ();
56#endif
57
58 /* I2C EEPROM */
59#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
60 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
wdenk38635852002-08-27 05:55:31 +000061#endif
Marek Vasut704a0962015-11-10 20:53:18 +010062}
wdenk38635852002-08-27 05:55:31 +000063
Marek Vasut78990c72015-11-10 20:53:23 +010064static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
65 uchar *buffer, unsigned len, bool read)
66{
67 int ret = 0;
68
69 /* SPI */
70#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
71 if (read)
72 spi_read(addr, alen, buffer, len);
73 else
74 spi_write(addr, alen, buffer, len);
75#else /* I2C */
76
77#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
78 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
79#endif
80
81 if (read)
82 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
83 else
84 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
85
86 if (ret)
87 ret = 1;
88#endif
89 return ret;
90}
91
wdenk38635852002-08-27 05:55:31 +000092int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
93{
94 unsigned end = offset + cnt;
95 unsigned blk_off;
96 int rcode = 0;
97
Marek Vasut704a0962015-11-10 20:53:18 +010098 /*
99 * Read data until done or would cross a page boundary.
wdenk38635852002-08-27 05:55:31 +0000100 * We must write the address again when changing pages
101 * because the next page may be in a different device.
102 */
103 while (offset < end) {
m8a484c602005-08-12 21:16:13 +0200104 unsigned alen, len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200105#if !defined(CONFIG_SYS_I2C_FRAM)
m8a484c602005-08-12 21:16:13 +0200106 unsigned maxlen;
107#endif
108
Marek Vasutae59bfc2015-11-10 20:53:22 +0100109#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
wdenk38635852002-08-27 05:55:31 +0000110 uchar addr[2];
111
112 blk_off = offset & 0xFF; /* block offset */
113
114 addr[0] = offset >> 8; /* block number */
115 addr[1] = blk_off; /* block offset */
116 alen = 2;
117#else
118 uchar addr[3];
119
120 blk_off = offset & 0xFF; /* block offset */
121
122 addr[0] = offset >> 16; /* block number */
123 addr[1] = offset >> 8; /* upper address octet */
124 addr[2] = blk_off; /* lower address octet */
125 alen = 3;
Marek Vasutae59bfc2015-11-10 20:53:22 +0100126#endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
wdenk38635852002-08-27 05:55:31 +0000127
128 addr[0] |= dev_addr; /* insert device address */
129
m8a484c602005-08-12 21:16:13 +0200130 len = end - offset;
131
132 /*
133 * For a FRAM device there is no limit on the number of the
134 * bytes that can be ccessed with the single read or write
135 * operation.
136 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200137#if !defined(CONFIG_SYS_I2C_FRAM)
wdenk38635852002-08-27 05:55:31 +0000138 maxlen = 0x100 - blk_off;
139 if (maxlen > I2C_RXTX_LEN)
140 maxlen = I2C_RXTX_LEN;
wdenk38635852002-08-27 05:55:31 +0000141 if (len > maxlen)
142 len = maxlen;
m8a484c602005-08-12 21:16:13 +0200143#endif
144
Marek Vasut78990c72015-11-10 20:53:23 +0100145 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 1);
146
wdenk38635852002-08-27 05:55:31 +0000147 buffer += len;
148 offset += len;
149 }
m8a484c602005-08-12 21:16:13 +0200150
wdenk38635852002-08-27 05:55:31 +0000151 return rcode;
152}
153
wdenk38635852002-08-27 05:55:31 +0000154int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
155{
156 unsigned end = offset + cnt;
157 unsigned blk_off;
158 int rcode = 0;
159
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200160#if defined(CONFIG_SYS_EEPROM_WREN)
Stefan Roesed9d97742005-09-22 09:04:17 +0200161 eeprom_write_enable (dev_addr,1);
162#endif
Marek Vasut704a0962015-11-10 20:53:18 +0100163 /*
164 * Write data until done or would cross a write page boundary.
wdenk38635852002-08-27 05:55:31 +0000165 * We must write the address again when changing pages
166 * because the address counter only increments within a page.
167 */
168
169 while (offset < end) {
m8a484c602005-08-12 21:16:13 +0200170 unsigned alen, len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200171#if !defined(CONFIG_SYS_I2C_FRAM)
m8a484c602005-08-12 21:16:13 +0200172 unsigned maxlen;
173#endif
174
Marek Vasutae59bfc2015-11-10 20:53:22 +0100175#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
wdenk38635852002-08-27 05:55:31 +0000176 uchar addr[2];
177
178 blk_off = offset & 0xFF; /* block offset */
179
180 addr[0] = offset >> 8; /* block number */
181 addr[1] = blk_off; /* block offset */
182 alen = 2;
183#else
184 uchar addr[3];
185
186 blk_off = offset & 0xFF; /* block offset */
187
188 addr[0] = offset >> 16; /* block number */
189 addr[1] = offset >> 8; /* upper address octet */
190 addr[2] = blk_off; /* lower address octet */
191 alen = 3;
Marek Vasutae59bfc2015-11-10 20:53:22 +0100192#endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
wdenk38635852002-08-27 05:55:31 +0000193
194 addr[0] |= dev_addr; /* insert device address */
195
m8a484c602005-08-12 21:16:13 +0200196 len = end - offset;
197
198 /*
199 * For a FRAM device there is no limit on the number of the
Michael Jones9c5ef8d2011-07-14 22:09:28 +0000200 * bytes that can be accessed with the single read or write
m8a484c602005-08-12 21:16:13 +0200201 * operation.
202 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200203#if !defined(CONFIG_SYS_I2C_FRAM)
m8a484c602005-08-12 21:16:13 +0200204
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200205#if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
wdenk38635852002-08-27 05:55:31 +0000206
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200207#define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
wdenk38635852002-08-27 05:55:31 +0000208#define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
209
210 maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
211#else
212 maxlen = 0x100 - blk_off;
213#endif
214 if (maxlen > I2C_RXTX_LEN)
215 maxlen = I2C_RXTX_LEN;
216
wdenk38635852002-08-27 05:55:31 +0000217 if (len > maxlen)
218 len = maxlen;
m8a484c602005-08-12 21:16:13 +0200219#endif
220
Marek Vasut78990c72015-11-10 20:53:23 +0100221 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 0);
wdenk38635852002-08-27 05:55:31 +0000222
wdenk38635852002-08-27 05:55:31 +0000223 buffer += len;
224 offset += len;
225
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200226#if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
227 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
wdenk38635852002-08-27 05:55:31 +0000228#endif
229 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200230#if defined(CONFIG_SYS_EEPROM_WREN)
Stefan Roesed9d97742005-09-22 09:04:17 +0200231 eeprom_write_enable (dev_addr,0);
232#endif
wdenk38635852002-08-27 05:55:31 +0000233 return rcode;
234}
235
Marek Vasut704a0962015-11-10 20:53:18 +0100236static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000237{
Marek Vasut704a0962015-11-10 20:53:18 +0100238 const char *const fmt =
239 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
Marek Vasutee541442015-11-10 20:53:19 +0100240 char * const *args = &argv[2];
241 int rcode;
242 ulong dev_addr, addr, off, cnt;
Heiko Schocher9bb0dd52010-01-07 08:55:40 +0100243
Marek Vasutee541442015-11-10 20:53:19 +0100244 switch (argc) {
245#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
246 case 5:
247 dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
248 break;
249#endif
250 case 6:
251 dev_addr = simple_strtoul(*args++, NULL, 16);
252 break;
253 default:
254 return CMD_RET_USAGE;
255 }
256
257 addr = simple_strtoul(*args++, NULL, 16);
258 off = simple_strtoul(*args++, NULL, 16);
259 cnt = simple_strtoul(*args++, NULL, 16);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +0100260
Marek Vasut704a0962015-11-10 20:53:18 +0100261# if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
Marek Vasutee541442015-11-10 20:53:19 +0100262 eeprom_init ();
Marek Vasut704a0962015-11-10 20:53:18 +0100263# endif /* !CONFIG_SPI */
Jon Loeligerd704d912007-07-10 11:02:44 -0500264
Marek Vasutee541442015-11-10 20:53:19 +0100265 if (strcmp (argv[1], "read") == 0) {
266 printf(fmt, dev_addr, argv[1], addr, off, cnt);
Marek Vasut704a0962015-11-10 20:53:18 +0100267
Marek Vasutee541442015-11-10 20:53:19 +0100268 rcode = eeprom_read(dev_addr, off, (uchar *) addr, cnt);
wdenk57b2d802003-06-27 21:31:46 +0000269
Marek Vasutee541442015-11-10 20:53:19 +0100270 puts ("done\n");
271 return rcode;
272 } else if (strcmp (argv[1], "write") == 0) {
273 printf(fmt, dev_addr, argv[1], addr, off, cnt);
Marek Vasut704a0962015-11-10 20:53:18 +0100274
Marek Vasutee541442015-11-10 20:53:19 +0100275 rcode = eeprom_write(dev_addr, off, (uchar *) addr, cnt);
Marek Vasut704a0962015-11-10 20:53:18 +0100276
Marek Vasutee541442015-11-10 20:53:19 +0100277 puts ("done\n");
278 return rcode;
Marek Vasut704a0962015-11-10 20:53:18 +0100279 }
280
281 return CMD_RET_USAGE;
282}
wdenk57b2d802003-06-27 21:31:46 +0000283
wdenkf287a242003-07-01 21:06:45 +0000284U_BOOT_CMD(
285 eeprom, 6, 1, do_eeprom,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600286 "EEPROM sub-system",
wdenk57b2d802003-06-27 21:31:46 +0000287 "read devaddr addr off cnt\n"
288 "eeprom write devaddr addr off cnt\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200289 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
Jeroen Hofsteef384fbf2014-06-23 00:22:08 +0200290)