cmd_eeprom.c 11.3 KB
Newer Older
W
wdenk 已提交
1 2 3 4
/*
 * (C) Copyright 2000, 2001
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
W
wdenk 已提交
6 7
 */

已提交
8 9 10 11
/*
 * Support for read and write access to EEPROM like memory devices. This
 * includes regular EEPROM as well as  FRAM (ferroelectic nonvolaile RAM).
 * FRAM devices read and write data at bus speed. In particular, there is no
P
Peter Meerwald 已提交
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 16
 * Use the following configuration options to ensure no unneeded performance
 * degradation (typical for EEPROM) is incured for FRAM memory:
17
 *
18 19
 * #define CONFIG_SYS_I2C_FRAM
 * #undef CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS
已提交
20 21 22
 *
 */

W
wdenk 已提交
23 24 25 26 27 28 29 30 31 32
#include <common.h>
#include <config.h>
#include <command.h>
#include <i2c.h>

extern void eeprom_init  (void);
extern int  eeprom_read  (unsigned dev_addr, unsigned offset,
			  uchar *buffer, unsigned cnt);
extern int  eeprom_write (unsigned dev_addr, unsigned offset,
			  uchar *buffer, unsigned cnt);
33
#if defined(CONFIG_SYS_EEPROM_WREN)
S
Stefan Roese 已提交
34 35
extern int eeprom_write_enable (unsigned dev_addr, int state);
#endif
W
wdenk 已提交
36 37


38
#if defined(CONFIG_SYS_EEPROM_X40430)
W
wdenk 已提交
39 40 41 42 43 44
	/* Maximum number of times to poll for acknowledge after write */
#define MAX_ACKNOWLEDGE_POLLS	10
#endif

/* ------------------------------------------------------------------------- */

45
#if defined(CONFIG_CMD_EEPROM)
46
static int do_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
W
wdenk 已提交
47 48 49 50
{
	const char *const fmt =
		"\nEEPROM @0x%lX %s: addr %08lx  off %04lx  count %ld ... ";

51
#if defined(CONFIG_SYS_I2C_MULTI_EEPROMS)
W
wdenk 已提交
52 53 54 55 56 57 58
	if (argc == 6) {
		ulong dev_addr = simple_strtoul (argv[2], NULL, 16);
		ulong addr = simple_strtoul (argv[3], NULL, 16);
		ulong off  = simple_strtoul (argv[4], NULL, 16);
		ulong cnt  = simple_strtoul (argv[5], NULL, 16);
#else
	if (argc == 5) {
59
		ulong dev_addr = CONFIG_SYS_DEF_EEPROM_ADDR;
W
wdenk 已提交
60 61 62
		ulong addr = simple_strtoul (argv[2], NULL, 16);
		ulong off  = simple_strtoul (argv[3], NULL, 16);
		ulong cnt  = simple_strtoul (argv[4], NULL, 16);
63
#endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */
W
wdenk 已提交
64

H
Heiko Schocher 已提交
65
# if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
W
wdenk 已提交
66 67 68 69 70 71 72 73 74 75
		eeprom_init ();
# endif /* !CONFIG_SPI */

		if (strcmp (argv[1], "read") == 0) {
			int rcode;

			printf (fmt, dev_addr, argv[1], addr, off, cnt);

			rcode = eeprom_read (dev_addr, off, (uchar *) addr, cnt);

76
			puts ("done\n");
W
wdenk 已提交
77 78 79 80 81 82 83 84
			return rcode;
		} else if (strcmp (argv[1], "write") == 0) {
			int rcode;

			printf (fmt, dev_addr, argv[1], addr, off, cnt);

			rcode = eeprom_write (dev_addr, off, (uchar *) addr, cnt);

85
			puts ("done\n");
W
wdenk 已提交
86 87 88 89
			return rcode;
		}
	}

90
	return CMD_RET_USAGE;
W
wdenk 已提交
91
}
92
#endif
W
wdenk 已提交
93 94 95

/*-----------------------------------------------------------------------
 *
96
 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
W
wdenk 已提交
97 98
 *   0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
 *
99
 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
W
wdenk 已提交
100 101 102
 *   0x00000nxx for EEPROM address selectors and page number at n.
 */

H
Heiko Schocher 已提交
103
#if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
104 105
#if !defined(CONFIG_SYS_I2C_EEPROM_ADDR_LEN) || CONFIG_SYS_I2C_EEPROM_ADDR_LEN < 1 || CONFIG_SYS_I2C_EEPROM_ADDR_LEN > 2
#error CONFIG_SYS_I2C_EEPROM_ADDR_LEN must be 1 or 2
W
wdenk 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119
#endif
#endif

int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
{
	unsigned end = offset + cnt;
	unsigned blk_off;
	int rcode = 0;

	/* Read data until done or would cross a page boundary.
	 * We must write the address again when changing pages
	 * because the next page may be in a different device.
	 */
	while (offset < end) {
已提交
120
		unsigned alen, len;
121
#if !defined(CONFIG_SYS_I2C_FRAM)
已提交
122 123 124
		unsigned maxlen;
#endif

125
#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
W
wdenk 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
		uchar addr[2];

		blk_off = offset & 0xFF;	/* block offset */

		addr[0] = offset >> 8;		/* block number */
		addr[1] = blk_off;		/* block offset */
		alen	= 2;
#else
		uchar addr[3];

		blk_off = offset & 0xFF;	/* block offset */

		addr[0] = offset >> 16;		/* block number */
		addr[1] = offset >>  8;		/* upper address octet */
		addr[2] = blk_off;		/* lower address octet */
		alen	= 3;
142
#endif	/* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
W
wdenk 已提交
143 144 145

		addr[0] |= dev_addr;		/* insert device address */

已提交
146 147 148 149 150 151 152
		len = end - offset;

		/*
		 * For a FRAM device there is no limit on the number of the
		 * bytes that can be ccessed with the single read or write
		 * operation.
		 */
153
#if !defined(CONFIG_SYS_I2C_FRAM)
W
wdenk 已提交
154 155 156 157 158
		maxlen = 0x100 - blk_off;
		if (maxlen > I2C_RXTX_LEN)
			maxlen = I2C_RXTX_LEN;
		if (len > maxlen)
			len = maxlen;
已提交
159 160
#endif

H
Heiko Schocher 已提交
161
#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
W
wdenk 已提交
162 163
		spi_read (addr, alen, buffer, len);
#else
164 165 166
#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
		i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
#endif
167
		if (i2c_read(addr[0], offset, alen - 1, buffer, len))
W
wdenk 已提交
168 169 170 171 172
			rcode = 1;
#endif
		buffer += len;
		offset += len;
	}
已提交
173

W
wdenk 已提交
174 175 176 177 178
	return rcode;
}

/*-----------------------------------------------------------------------
 *
179
 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
W
wdenk 已提交
180 181
 *   0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
 *
182
 * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
W
wdenk 已提交
183 184 185 186 187 188 189 190 191
 *   0x00000nxx for EEPROM address selectors and page number at n.
 */

int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
{
	unsigned end = offset + cnt;
	unsigned blk_off;
	int rcode = 0;

192
#if defined(CONFIG_SYS_EEPROM_X40430)
W
wdenk 已提交
193 194 195 196 197 198 199
	uchar	contr_r_addr[2];
	uchar	addr_void[2];
	uchar	contr_reg[2];
	uchar	ctrl_reg_v;
	int	i;
#endif

200
#if defined(CONFIG_SYS_EEPROM_WREN)
S
Stefan Roese 已提交
201 202
	eeprom_write_enable (dev_addr,1);
#endif
W
wdenk 已提交
203 204 205 206 207 208
	/* Write data until done or would cross a write page boundary.
	 * We must write the address again when changing pages
	 * because the address counter only increments within a page.
	 */

	while (offset < end) {
已提交
209
		unsigned alen, len;
210
#if !defined(CONFIG_SYS_I2C_FRAM)
已提交
211 212 213
		unsigned maxlen;
#endif

214
#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
W
wdenk 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
		uchar addr[2];

		blk_off = offset & 0xFF;	/* block offset */

		addr[0] = offset >> 8;		/* block number */
		addr[1] = blk_off;		/* block offset */
		alen	= 2;
#else
		uchar addr[3];

		blk_off = offset & 0xFF;	/* block offset */

		addr[0] = offset >> 16;		/* block number */
		addr[1] = offset >>  8;		/* upper address octet */
		addr[2] = blk_off;		/* lower address octet */
		alen	= 3;
231
#endif	/* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
W
wdenk 已提交
232 233 234

		addr[0] |= dev_addr;		/* insert device address */

已提交
235 236 237 238
		len = end - offset;

		/*
		 * For a FRAM device there is no limit on the number of the
M
Michael Jones 已提交
239
		 * bytes that can be accessed with the single read or write
已提交
240 241
		 * operation.
		 */
242
#if !defined(CONFIG_SYS_I2C_FRAM)
已提交
243

244
#if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
W
wdenk 已提交
245

246
#define	EEPROM_PAGE_SIZE	(1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)
W
wdenk 已提交
247 248 249 250 251 252 253 254 255 256 257
#define	EEPROM_PAGE_OFFSET(x)	((x) & (EEPROM_PAGE_SIZE - 1))

		maxlen = EEPROM_PAGE_SIZE - EEPROM_PAGE_OFFSET(blk_off);
#else
		maxlen = 0x100 - blk_off;
#endif
		if (maxlen > I2C_RXTX_LEN)
			maxlen = I2C_RXTX_LEN;

		if (len > maxlen)
			len = maxlen;
已提交
258 259
#endif

H
Heiko Schocher 已提交
260
#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
W
wdenk 已提交
261 262
		spi_write (addr, alen, buffer, len);
#else
263
#if defined(CONFIG_SYS_EEPROM_X40430)
W
wdenk 已提交
264 265 266 267 268 269 270 271
		/* Get the value of the control register.
		 * Set current address (internal pointer in the x40430)
		 * to 0x1ff.
		 */
		contr_r_addr[0] = 9;
		contr_r_addr[1] = 0xff;
		addr_void[0]    = 0;
		addr_void[1]    = addr[1];
272 273 274
#ifdef CONFIG_SYS_I2C_EEPROM_ADDR
		contr_r_addr[0] |= CONFIG_SYS_I2C_EEPROM_ADDR;
		addr_void[0]    |= CONFIG_SYS_I2C_EEPROM_ADDR;
W
wdenk 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
#endif
		contr_reg[0] = 0xff;
		if (i2c_read (contr_r_addr[0], contr_r_addr[1], 1, contr_reg, 1) != 0) {
			rcode = 1;
		}
		ctrl_reg_v = contr_reg[0];

		/* Are any of the eeprom blocks write protected?
		 */
		if (ctrl_reg_v & 0x18) {
			ctrl_reg_v &= ~0x18;   /* reset block protect bits  */
			ctrl_reg_v |=  0x02;   /* set write enable latch    */
			ctrl_reg_v &= ~0x04;   /* clear RWEL                */

			/* Set write enable latch.
			 */
			contr_reg[0] = 0x02;
			if (i2c_write (contr_r_addr[0], 0xff, 1, contr_reg, 1) != 0) {
				rcode = 1;
			}

			/* Set register write enable latch.
			 */
			contr_reg[0] = 0x06;
			if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
				rcode = 1;
			}

			/* Modify ctrl register.
			 */
			contr_reg[0] = ctrl_reg_v;
			if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
				rcode = 1;
			}

			/* The write (above) is an operation on NV memory.
			 * These can take some time (~5ms), and the device
			 * will not respond to further I2C messages till
			 * it's completed the write.
			 * So poll device for an I2C acknowledge.
			 * When we get one we know we can continue with other
			 * operations.
			 */
			contr_reg[0] = 0;
			for (i = 0; i < MAX_ACKNOWLEDGE_POLLS; i++) {
W
wdenk 已提交
320
				if (i2c_read (addr_void[0], addr_void[1], 1, contr_reg, 1) == 0)
W
wdenk 已提交
321
					break;	/* got ack */
322 323
#if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
				udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
W
wdenk 已提交
324 325 326
#endif
			}
			if (i == MAX_ACKNOWLEDGE_POLLS) {
327
				puts ("EEPROM poll acknowledge failed\n");
W
wdenk 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
				rcode = 1;
			}
		}

		/* Is the write enable latch on?.
		 */
		else if (!(ctrl_reg_v & 0x02)) {
			/* Set write enable latch.
			 */
			contr_reg[0] = 0x02;
			if (i2c_write (contr_r_addr[0], 0xFF, 1, contr_reg, 1) != 0) {
			       rcode = 1;
			}
		}
		/* Write is enabled ... now write eeprom value.
		 */
344 345 346
#endif
#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
		i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
W
wdenk 已提交
347
#endif
348
		if (i2c_write(addr[0], offset, alen - 1, buffer, len))
W
wdenk 已提交
349 350 351 352 353 354
			rcode = 1;

#endif
		buffer += len;
		offset += len;

355 356
#if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
		udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
W
wdenk 已提交
357 358
#endif
	}
359
#if defined(CONFIG_SYS_EEPROM_WREN)
S
Stefan Roese 已提交
360 361
	eeprom_write_enable (dev_addr,0);
#endif
W
wdenk 已提交
362 363 364
	return rcode;
}

H
Heiko Schocher 已提交
365
#if !defined(CONFIG_SPI) || defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
W
wdenk 已提交
366 367 368 369 370 371 372
int
eeprom_probe (unsigned dev_addr, unsigned offset)
{
	unsigned char chip;

	/* Probe the chip address
	 */
373
#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
W
wdenk 已提交
374 375 376
	chip = offset >> 8;		/* block number */
#else
	chip = offset >> 16;		/* block number */
377
#endif	/* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
W
wdenk 已提交
378 379 380 381 382 383 384

	chip |= dev_addr;		/* insert device address */

	return (i2c_probe (chip));
}
#endif

W
wdenk 已提交
385 386 387
/*-----------------------------------------------------------------------
 * Set default values
 */
388 389
#ifndef	CONFIG_SYS_I2C_SPEED
#define	CONFIG_SYS_I2C_SPEED	50000
W
wdenk 已提交
390 391 392 393
#endif

void eeprom_init  (void)
{
H
Heiko Schocher 已提交
394 395

#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
W
wdenk 已提交
396 397
	spi_init_f ();
#endif
398 399
#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C_SOFT)
	i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
W
wdenk 已提交
400 401
#endif
}
H
Heiko Schocher 已提交
402

W
wdenk 已提交
403 404
/*-----------------------------------------------------------------------
 */
405

W
wdenk 已提交
406 407
/***************************************************/

408
#if defined(CONFIG_CMD_EEPROM)
W
wdenk 已提交
409

410
#ifdef CONFIG_SYS_I2C_MULTI_EEPROMS
W
wdenk 已提交
411 412
U_BOOT_CMD(
	eeprom,	6,	1,	do_eeprom,
P
Peter Tyser 已提交
413
	"EEPROM sub-system",
W
wdenk 已提交
414 415
	"read  devaddr addr off cnt\n"
	"eeprom write devaddr addr off cnt\n"
W
Wolfgang Denk 已提交
416
	"       - read/write `cnt' bytes from `devaddr` EEPROM at offset `off'"
417
)
W
wdenk 已提交
418
#else /* One EEPROM */
W
wdenk 已提交
419 420
U_BOOT_CMD(
	eeprom,	5,	1,	do_eeprom,
P
Peter Tyser 已提交
421
	"EEPROM sub-system",
W
wdenk 已提交
422 423
	"read  addr off cnt\n"
	"eeprom write addr off cnt\n"
W
Wolfgang Denk 已提交
424
	"       - read/write `cnt' bytes at EEPROM offset `off'"
425
)
426
#endif /* CONFIG_SYS_I2C_MULTI_EEPROMS */
W
wdenk 已提交
427

428
#endif