]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_eeprom.c
eeprom: Pull out CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
[people/ms/u-boot.git] / common / cmd_eeprom.c
CommitLineData
3863585b
WD
1/*
2 * (C) Copyright 2000, 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
3863585b
WD
6 */
7
d4f5c728 8/*
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
e506a006 12 * write delay. Also, there is no limit imposed on the number of bytes that can
d4f5c728 13 * be transferred with a single read or write.
6617aae9 14 *
d4f5c728 15 * Use the following configuration options to ensure no unneeded performance
16 * degradation (typical for EEPROM) is incured for FRAM memory:
6617aae9 17 *
6d0f6bcf
JCPV
18 * #define CONFIG_SYS_I2C_FRAM
19 * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
d4f5c728 20 *
21 */
22
3863585b
WD
23#include <common.h>
24#include <config.h>
25#include <command.h>
26#include <i2c.h>
27
4f296d09
MV
28#ifndef CONFIG_SYS_I2C_SPEED
29#define CONFIG_SYS_I2C_SPEED 50000
98f4a3df 30#endif
3863585b 31
d738746c
MV
32#ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
33#define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS 0
34#endif
35
4f296d09 36/*
6d0f6bcf 37 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
3863585b
WD
38 * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
39 *
6d0f6bcf 40 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
3863585b
WD
41 * 0x00000nxx for EEPROM address selectors and page number at n.
42 */
548738b4 43#if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
4f296d09
MV
44#if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
45 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
46 (CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
6d0f6bcf 47#error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
3863585b
WD
48#endif
49#endif
50
52cd47c9
MV
51__weak int eeprom_write_enable(unsigned dev_addr, int state)
52{
53 return 0;
54}
4f296d09
MV
55
56void eeprom_init(void)
57{
58 /* SPI EEPROM */
59#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
60 spi_init_f ();
61#endif
62
63 /* I2C EEPROM */
64#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
65 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
66#endif
67}
68
02c321cf
MV
69static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
70{
71 unsigned blk_off;
72 int alen;
73
74 blk_off = offset & 0xff; /* block offset */
75#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
76 addr[0] = offset >> 8; /* block number */
77 addr[1] = blk_off; /* block offset */
78 alen = 2;
79#else
80 addr[0] = offset >> 16; /* block number */
81 addr[1] = offset >> 8; /* upper address octet */
82 addr[2] = blk_off; /* lower address octet */
83 alen = 3;
84#endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
85
86 addr[0] |= dev_addr; /* insert device address */
87
88 return alen;
89}
90
9132088b
MV
91static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
92 uchar *buffer, unsigned len, bool read)
93{
94 int ret = 0;
95
96 /* SPI */
97#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
98 if (read)
99 spi_read(addr, alen, buffer, len);
100 else
101 spi_write(addr, alen, buffer, len);
102#else /* I2C */
103
104#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
105 i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
106#endif
107
108 if (read)
109 ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
110 else
111 ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
112
113 if (ret)
114 ret = 1;
115#endif
116 return ret;
117}
118
3863585b
WD
119int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
120{
121 unsigned end = offset + cnt;
122 unsigned blk_off;
123 int rcode = 0;
02c321cf 124 uchar addr[3];
3863585b 125
4f296d09
MV
126 /*
127 * Read data until done or would cross a page boundary.
3863585b
WD
128 * We must write the address again when changing pages
129 * because the next page may be in a different device.
130 */
131 while (offset < end) {
d4f5c728 132 unsigned alen, len;
6d0f6bcf 133#if !defined(CONFIG_SYS_I2C_FRAM)
d4f5c728 134 unsigned maxlen;
135#endif
136
3863585b 137 blk_off = offset & 0xFF; /* block offset */
02c321cf 138 alen = eeprom_addr(dev_addr, offset, addr);
3863585b 139
d4f5c728 140 len = end - offset;
141
142 /*
143 * For a FRAM device there is no limit on the number of the
144 * bytes that can be ccessed with the single read or write
145 * operation.
146 */
6d0f6bcf 147#if !defined(CONFIG_SYS_I2C_FRAM)
3863585b
WD
148 maxlen = 0x100 - blk_off;
149 if (maxlen > I2C_RXTX_LEN)
150 maxlen = I2C_RXTX_LEN;
3863585b
WD
151 if (len > maxlen)
152 len = maxlen;
d4f5c728 153#endif
154
9132088b
MV
155 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 1);
156
3863585b
WD
157 buffer += len;
158 offset += len;
159 }
d4f5c728 160
3863585b
WD
161 return rcode;
162}
163
3863585b
WD
164int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
165{
166 unsigned end = offset + cnt;
167 unsigned blk_off;
168 int rcode = 0;
02c321cf 169 uchar addr[3];
3863585b 170
52cd47c9
MV
171 eeprom_write_enable(dev_addr, 1);
172
4f296d09
MV
173 /*
174 * Write data until done or would cross a write page boundary.
3863585b
WD
175 * We must write the address again when changing pages
176 * because the address counter only increments within a page.
177 */
178
179 while (offset < end) {
d4f5c728 180 unsigned alen, len;
6d0f6bcf 181#if !defined(CONFIG_SYS_I2C_FRAM)
d4f5c728 182 unsigned maxlen;
183#endif
184
3863585b 185 blk_off = offset & 0xFF; /* block offset */
02c321cf 186 alen = eeprom_addr(dev_addr, offset, addr);
3863585b 187
d4f5c728 188 len = end - offset;
189
190 /*
191 * For a FRAM device there is no limit on the number of the
f9a78b8d 192 * bytes that can be accessed with the single read or write
d4f5c728 193 * operation.
194 */
6d0f6bcf 195#if !defined(CONFIG_SYS_I2C_FRAM)
d4f5c728 196
6d0f6bcf 197#if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
3863585b 198
6d0f6bcf 199#define EEPROM_PAGE_SIZE (1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
3863585b
WD
200#define EEPROM_PAGE_OFFSET(x) ((x) & (EEPROM_PAGE_SIZE - 1))
201
202 maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
203#else
204 maxlen = 0x100 - blk_off;
205#endif
206 if (maxlen > I2C_RXTX_LEN)
207 maxlen = I2C_RXTX_LEN;
208
3863585b
WD
209 if (len > maxlen)
210 len = maxlen;
d4f5c728 211#endif
212
9132088b 213 rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 0);
3863585b 214
3863585b
WD
215 buffer += len;
216 offset += len;
217
6d0f6bcf 218 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
3863585b 219 }
52cd47c9
MV
220
221 eeprom_write_enable(dev_addr, 0);
222
3863585b
WD
223 return rcode;
224}
225
4f296d09 226static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
3863585b 227{
4f296d09
MV
228 const char *const fmt =
229 "\nEEPROM @0x%lX %s: addr %08lx off %04lx count %ld ... ";
e4f65d00
MV
230 char * const *args = &argv[2];
231 int rcode;
232 ulong dev_addr, addr, off, cnt;
233
234 switch (argc) {
235#ifdef CONFIG_SYS_DEF_EEPROM_ADDR
236 case 5:
237 dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
238 break;
239#endif
240 case 6:
241 dev_addr = simple_strtoul(*args++, NULL, 16);
242 break;
243 default:
244 return CMD_RET_USAGE;
245 }
548738b4 246
e4f65d00
MV
247 addr = simple_strtoul(*args++, NULL, 16);
248 off = simple_strtoul(*args++, NULL, 16);
249 cnt = simple_strtoul(*args++, NULL, 16);
548738b4 250
4f296d09 251# if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
e4f65d00 252 eeprom_init ();
4f296d09 253# endif /* !CONFIG_SPI */
90253178 254
e4f65d00
MV
255 if (strcmp (argv[1], "read") == 0) {
256 printf(fmt, dev_addr, argv[1], addr, off, cnt);
4f296d09 257
e4f65d00 258 rcode = eeprom_read(dev_addr, off, (uchar *) addr, cnt);
4f296d09 259
e4f65d00
MV
260 puts ("done\n");
261 return rcode;
262 } else if (strcmp (argv[1], "write") == 0) {
263 printf(fmt, dev_addr, argv[1], addr, off, cnt);
4f296d09 264
e4f65d00 265 rcode = eeprom_write(dev_addr, off, (uchar *) addr, cnt);
4f296d09 266
e4f65d00
MV
267 puts ("done\n");
268 return rcode;
4f296d09
MV
269 }
270
271 return CMD_RET_USAGE;
272}
8bde7f77 273
0d498393
WD
274U_BOOT_CMD(
275 eeprom, 6, 1, do_eeprom,
2fb2604d 276 "EEPROM sub-system",
8bde7f77
WD
277 "read devaddr addr off cnt\n"
278 "eeprom write devaddr addr off cnt\n"
a89c33db 279 " - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
0e350f81 280)