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