xref: /openbmc/u-boot/cmd/eeprom.c (revision 624656314f5684995fb9f499d38ad18d378802a5)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000, 2001
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 /*
8  * Support for read and write access to EEPROM like memory devices. This
9  * includes regular EEPROM as well as  FRAM (ferroelectic nonvolaile RAM).
10  * FRAM devices read and write data at bus speed. In particular, there is no
11  * write delay. Also, there is no limit imposed on the number of bytes that can
12  * be transferred with a single read or write.
13  *
14  * Use the following configuration options to ensure no unneeded performance
15  * degradation (typical for EEPROM) is incured for FRAM memory:
16  *
17  * #define CONFIG_SYS_I2C_FRAM
18  * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
19  *
20  */
21 
22 #include <common.h>
23 #include <config.h>
24 #include <command.h>
25 #include <eeprom.h>
26 #include <i2c.h>
27 #include <eeprom_layout.h>
28 
29 #ifndef	CONFIG_SYS_I2C_SPEED
30 #define	CONFIG_SYS_I2C_SPEED	50000
31 #endif
32 
33 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
34 #define CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS	0
35 #endif
36 
37 #ifndef CONFIG_SYS_EEPROM_PAGE_WRITE_BITS
38 #define CONFIG_SYS_EEPROM_PAGE_WRITE_BITS	8
39 #endif
40 
41 #ifndef	I2C_RXTX_LEN
42 #define I2C_RXTX_LEN	128
43 #endif
44 
45 #define	EEPROM_PAGE_SIZE	(1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
46 #define	EEPROM_PAGE_OFFSET(x)	((x) & (EEPROM_PAGE_SIZE - 1))
47 
48 /*
49  * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
50  *   0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
51  *
52  * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
53  *   0x00000nxx for EEPROM address selectors and page number at n.
54  */
55 #if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
56 #if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || \
57 	(CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1) || \
58 	(CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2)
59 #error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
60 #endif
61 #endif
62 
eeprom_write_enable(unsigned dev_addr,int state)63 __weak int eeprom_write_enable(unsigned dev_addr, int state)
64 {
65 	return 0;
66 }
67 
eeprom_init(int bus)68 void eeprom_init(int bus)
69 {
70 	/* I2C EEPROM */
71 #if defined(CONFIG_SYS_I2C)
72 	if (bus >= 0)
73 		i2c_set_bus_num(bus);
74 	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
75 #endif
76 }
77 
eeprom_addr(unsigned dev_addr,unsigned offset,uchar * addr)78 static int eeprom_addr(unsigned dev_addr, unsigned offset, uchar *addr)
79 {
80 	unsigned blk_off;
81 	int alen;
82 
83 	blk_off = offset & 0xff;	/* block offset */
84 #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1
85 	addr[0] = offset >> 8;		/* block number */
86 	addr[1] = blk_off;		/* block offset */
87 	alen = 2;
88 #else
89 	addr[0] = offset >> 16;		/* block number */
90 	addr[1] = offset >>  8;		/* upper address octet */
91 	addr[2] = blk_off;		/* lower address octet */
92 	alen = 3;
93 #endif	/* CONFIG_SYS_I2C_EEPROM_ADDR_LEN */
94 
95 	addr[0] |= dev_addr;		/* insert device address */
96 
97 	return alen;
98 }
99 
eeprom_len(unsigned offset,unsigned end)100 static int eeprom_len(unsigned offset, unsigned end)
101 {
102 	unsigned len = end - offset;
103 
104 	/*
105 	 * For a FRAM device there is no limit on the number of the
106 	 * bytes that can be ccessed with the single read or write
107 	 * operation.
108 	 */
109 #if !defined(CONFIG_SYS_I2C_FRAM)
110 	unsigned blk_off = offset & 0xff;
111 	unsigned maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
112 
113 	if (maxlen > I2C_RXTX_LEN)
114 		maxlen = I2C_RXTX_LEN;
115 
116 	if (len > maxlen)
117 		len = maxlen;
118 #endif
119 
120 	return len;
121 }
122 
eeprom_rw_block(unsigned offset,uchar * addr,unsigned alen,uchar * buffer,unsigned len,bool read)123 static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
124 			   uchar *buffer, unsigned len, bool read)
125 {
126 	int ret = 0;
127 
128 #if defined(CONFIG_DM_I2C) && defined(CONFIG_SYS_I2C_EEPROM_BUS)
129 	struct udevice *dev;
130 
131 	ret = i2c_get_chip_for_busnum(CONFIG_SYS_I2C_EEPROM_BUS, addr[0],
132 				      alen - 1, &dev);
133 	if (ret) {
134 		printf("%s: Cannot find udev for a bus %d\n", __func__,
135 		       CONFIG_SYS_I2C_EEPROM_BUS);
136 		return CMD_RET_FAILURE;
137 	}
138 
139 	if (read)
140 		ret = dm_i2c_read(dev, offset, buffer, len);
141 	else
142 		ret = dm_i2c_write(dev, offset, buffer, len);
143 
144 #else /* Non DM I2C support - will be removed */
145 #if defined(CONFIG_SYS_I2C_EEPROM_BUS)
146 	i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
147 #endif
148 
149 	if (read)
150 		ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
151 	else
152 		ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
153 #endif /* CONFIG_DM_I2C && CONFIG_SYS_I2C_EEPROM_BUS */
154 	if (ret)
155 		ret = CMD_RET_FAILURE;
156 
157 	return ret;
158 }
159 
eeprom_rw(unsigned dev_addr,unsigned offset,uchar * buffer,unsigned cnt,bool read)160 static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
161 		     unsigned cnt, bool read)
162 {
163 	unsigned end = offset + cnt;
164 	unsigned alen, len;
165 	int rcode = 0;
166 	uchar addr[3];
167 
168 	while (offset < end) {
169 		alen = eeprom_addr(dev_addr, offset, addr);
170 
171 		len = eeprom_len(offset, end);
172 
173 		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, read);
174 
175 		buffer += len;
176 		offset += len;
177 
178 		if (!read)
179 			udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
180 	}
181 
182 	return rcode;
183 }
184 
eeprom_read(unsigned dev_addr,unsigned offset,uchar * buffer,unsigned cnt)185 int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
186 {
187 	/*
188 	 * Read data until done or would cross a page boundary.
189 	 * We must write the address again when changing pages
190 	 * because the next page may be in a different device.
191 	 */
192 	return eeprom_rw(dev_addr, offset, buffer, cnt, 1);
193 }
194 
eeprom_write(unsigned dev_addr,unsigned offset,uchar * buffer,unsigned cnt)195 int eeprom_write(unsigned dev_addr, unsigned offset,
196 		 uchar *buffer, unsigned cnt)
197 {
198 	int ret;
199 
200 	eeprom_write_enable(dev_addr, 1);
201 
202 	/*
203 	 * Write data until done or would cross a write page boundary.
204 	 * We must write the address again when changing pages
205 	 * because the address counter only increments within a page.
206 	 */
207 	ret = eeprom_rw(dev_addr, offset, buffer, cnt, 0);
208 
209 	eeprom_write_enable(dev_addr, 0);
210 	return ret;
211 }
212 
parse_numeric_param(char * str)213 static int parse_numeric_param(char *str)
214 {
215 	char *endptr;
216 	int value = simple_strtol(str, &endptr, 16);
217 
218 	return (*endptr != '\0') ? -1 : value;
219 }
220 
221 /**
222  * parse_i2c_bus_addr - parse the i2c bus and i2c devaddr parameters
223  *
224  * @i2c_bus:	address to store the i2c bus
225  * @i2c_addr:	address to store the device i2c address
226  * @argc:	count of command line arguments left to parse
227  * @argv:	command line arguments left to parse
228  * @argc_no_bus_addr:	argc value we expect to see when bus & addr aren't given
229  *
230  * @returns:	number of arguments parsed or CMD_RET_USAGE if error
231  */
parse_i2c_bus_addr(int * i2c_bus,ulong * i2c_addr,int argc,char * const argv[],int argc_no_bus_addr)232 static int parse_i2c_bus_addr(int *i2c_bus, ulong *i2c_addr, int argc,
233 			      char * const argv[], int argc_no_bus_addr)
234 {
235 	int argc_no_bus = argc_no_bus_addr + 1;
236 	int argc_bus_addr = argc_no_bus_addr + 2;
237 
238 #ifdef CONFIG_SYS_DEF_EEPROM_ADDR
239 	if (argc == argc_no_bus_addr) {
240 		*i2c_bus = -1;
241 		*i2c_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
242 
243 		return 0;
244 	}
245 #endif
246 	if (argc == argc_no_bus) {
247 		*i2c_bus = -1;
248 		*i2c_addr = parse_numeric_param(argv[0]);
249 
250 		return 1;
251 	}
252 
253 	if (argc == argc_bus_addr) {
254 		*i2c_bus = parse_numeric_param(argv[0]);
255 		*i2c_addr = parse_numeric_param(argv[1]);
256 
257 		return 2;
258 	}
259 
260 	return CMD_RET_USAGE;
261 }
262 
263 #ifdef CONFIG_CMD_EEPROM_LAYOUT
264 
eeprom_parse_layout_version(char * str)265 __weak int eeprom_parse_layout_version(char *str)
266 {
267 	return LAYOUT_VERSION_UNRECOGNIZED;
268 }
269 
270 static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
271 
272 #endif
273 
274 enum eeprom_action {
275 	EEPROM_READ,
276 	EEPROM_WRITE,
277 	EEPROM_PRINT,
278 	EEPROM_UPDATE,
279 	EEPROM_ACTION_INVALID,
280 };
281 
parse_action(char * cmd)282 static enum eeprom_action parse_action(char *cmd)
283 {
284 	if (!strncmp(cmd, "read", 4))
285 		return EEPROM_READ;
286 	if (!strncmp(cmd, "write", 5))
287 		return EEPROM_WRITE;
288 #ifdef CONFIG_CMD_EEPROM_LAYOUT
289 	if (!strncmp(cmd, "print", 5))
290 		return EEPROM_PRINT;
291 	if (!strncmp(cmd, "update", 6))
292 		return EEPROM_UPDATE;
293 #endif
294 
295 	return EEPROM_ACTION_INVALID;
296 }
297 
eeprom_execute_command(enum eeprom_action action,int i2c_bus,ulong i2c_addr,int layout_ver,char * key,char * value,ulong addr,ulong off,ulong cnt)298 static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
299 				  ulong i2c_addr, int layout_ver, char *key,
300 				  char *value, ulong addr, ulong off, ulong cnt)
301 {
302 	int rcode = 0;
303 	const char *const fmt =
304 		"\nEEPROM @0x%lX %s: addr %08lx  off %04lx  count %ld ... ";
305 #ifdef CONFIG_CMD_EEPROM_LAYOUT
306 	struct eeprom_layout layout;
307 #endif
308 
309 	if (action == EEPROM_ACTION_INVALID)
310 		return CMD_RET_USAGE;
311 
312 	eeprom_init(i2c_bus);
313 	if (action == EEPROM_READ) {
314 		printf(fmt, i2c_addr, "read", addr, off, cnt);
315 
316 		rcode = eeprom_read(i2c_addr, off, (uchar *)addr, cnt);
317 
318 		puts("done\n");
319 		return rcode;
320 	} else if (action == EEPROM_WRITE) {
321 		printf(fmt, i2c_addr, "write", addr, off, cnt);
322 
323 		rcode = eeprom_write(i2c_addr, off, (uchar *)addr, cnt);
324 
325 		puts("done\n");
326 		return rcode;
327 	}
328 
329 #ifdef CONFIG_CMD_EEPROM_LAYOUT
330 	rcode = eeprom_read(i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
331 	if (rcode < 0)
332 		return rcode;
333 
334 	eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
335 			    layout_ver);
336 
337 	if (action == EEPROM_PRINT) {
338 		layout.print(&layout);
339 		return 0;
340 	}
341 
342 	layout.update(&layout, key, value);
343 
344 	rcode = eeprom_write(i2c_addr, 0, layout.data, CONFIG_SYS_EEPROM_SIZE);
345 #endif
346 
347 	return rcode;
348 }
349 
350 #define NEXT_PARAM(argc, index)	{ (argc)--; (index)++; }
do_eeprom(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])351 int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
352 {
353 	int layout_ver = LAYOUT_VERSION_AUTODETECT;
354 	enum eeprom_action action = EEPROM_ACTION_INVALID;
355 	int i2c_bus = -1, index = 0;
356 	ulong i2c_addr = -1, addr = 0, cnt = 0, off = 0;
357 	int ret;
358 	char *field_name = "";
359 	char *field_value = "";
360 
361 	if (argc <= 1)
362 		return CMD_RET_USAGE;
363 
364 	NEXT_PARAM(argc, index); /* Skip program name */
365 
366 	action = parse_action(argv[index]);
367 	NEXT_PARAM(argc, index);
368 
369 	if (action == EEPROM_ACTION_INVALID)
370 		return CMD_RET_USAGE;
371 
372 #ifdef CONFIG_CMD_EEPROM_LAYOUT
373 	if (action == EEPROM_PRINT || action == EEPROM_UPDATE) {
374 		if (!strcmp(argv[index], "-l")) {
375 			NEXT_PARAM(argc, index);
376 			layout_ver = eeprom_parse_layout_version(argv[index]);
377 			NEXT_PARAM(argc, index);
378 		}
379 	}
380 #endif
381 
382 	switch (action) {
383 	case EEPROM_READ:
384 	case EEPROM_WRITE:
385 		ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
386 					 argv + index, 3);
387 		break;
388 	case EEPROM_PRINT:
389 		ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
390 					 argv + index, 0);
391 		break;
392 	case EEPROM_UPDATE:
393 		ret = parse_i2c_bus_addr(&i2c_bus, &i2c_addr, argc,
394 					 argv + index, 2);
395 		break;
396 	default:
397 		/* Get compiler to stop whining */
398 		return CMD_RET_USAGE;
399 	}
400 
401 	if (ret == CMD_RET_USAGE)
402 		return ret;
403 
404 	while (ret--)
405 		NEXT_PARAM(argc, index);
406 
407 	if (action == EEPROM_READ || action == EEPROM_WRITE) {
408 		addr = parse_numeric_param(argv[index]);
409 		NEXT_PARAM(argc, index);
410 		off = parse_numeric_param(argv[index]);
411 		NEXT_PARAM(argc, index);
412 		cnt = parse_numeric_param(argv[index]);
413 	}
414 
415 #ifdef CONFIG_CMD_EEPROM_LAYOUT
416 	if (action == EEPROM_UPDATE) {
417 		field_name = argv[index];
418 		NEXT_PARAM(argc, index);
419 		field_value = argv[index];
420 		NEXT_PARAM(argc, index);
421 	}
422 #endif
423 
424 	return eeprom_execute_command(action, i2c_bus, i2c_addr, layout_ver,
425 				      field_name, field_value, addr, off, cnt);
426 }
427 
428 U_BOOT_CMD(
429 	eeprom,	8,	1,	do_eeprom,
430 	"EEPROM sub-system",
431 	"read  <bus> <devaddr> addr off cnt\n"
432 	"eeprom write <bus> <devaddr> addr off cnt\n"
433 	"       - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
434 #ifdef CONFIG_CMD_EEPROM_LAYOUT
435 	"\n"
436 	"eeprom print [-l <layout_version>] <bus> <devaddr>\n"
437 	"       - Print layout fields and their data in human readable format\n"
438 	"eeprom update [-l <layout_version>] <bus> <devaddr> field_name field_value\n"
439 	"       - Update a specific eeprom field with new data.\n"
440 	"         The new data must be written in the same human readable format as shown by the print command.\n"
441 	"\n"
442 	"LAYOUT VERSIONS\n"
443 	"The -l option can be used to force the command to interpret the EEPROM data using the chosen layout.\n"
444 	"If the -l option is omitted, the command will auto detect the layout based on the data in the EEPROM.\n"
445 	"The values which can be provided with the -l option are:\n"
446 	CONFIG_EEPROM_LAYOUT_HELP_STRING"\n"
447 #endif
448 )
449