File: libflash.c

package info (click to toggle)
nictools-pci 1.3.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 572 kB
  • ctags: 1,818
  • sloc: ansic: 11,827; makefile: 108; sh: 44
file content (394 lines) | stat: -rw-r--r-- 12,082 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* libflash.c: Flash memory programming library.

	Copyright 1998-2000 by Donald Becker.
	This software may be used and distributed according to the terms of
	the GNU General Public License (GPL), incorporated herein by reference.
	Contact the author for use under other terms.

	The author may be reached as becker@scyld.com, or C/O
	 Scyld Computing Corporation
	 410 Severn Ave., Suite 210
	 Annapolis MD 21403

	Support and updates available at
	http://www.scyld.com/diag/index.html

	References:
	http://www.atmel.com/atmel/products/prod9.htm
	http://www.ssti.com/products/products.html
*/
static const char version_msg[] =
"libflash.c:v2.03 4/19/2000 Copyright Donald Becker, becker@scyld.com";

/* External interface
      flash_show(), flash_dump() and flash_program()
	ADDR_IOADDR is a 32-bit I/O space register that sets the Flash ROM address.
		It is always accessed as  outl(val, ADDR_IOADDR)
	DATA_IOADDR is an 8-bit I/O register from which the Flash ROM data is read.
		It is always accessed with inb or outb  e.g.  outb(val, DATA_IOADDR)
	FILENAME is a filename where the Flash image is stored.
	The caller does any register setup required.
*/

/*  External interface to this library.  Functions and variables: */
/* Show the Flash contents on stdout. */
extern int flash_show   (long addr_ioaddr, long data_ioaddr);
/* Read or write the Flash, writing/reading FILENAME. */
extern int flash_program(long addr_ioaddr, long data_ioaddr, char *filename);
extern int flash_dump   (long addr_ioaddr, long data_ioaddr, char *filename);

extern int verbose;				/* Set to non-zero for info. */
extern int debug;				/* Currently unused. */

/* Callback hooks for accessing single words of the flash chip.
   Chips that have a I/O space 32 bit address register and do an 8 bit
   read/write register will not need this. */
int (*flash_in_hook)(long addr, int offset) = 0;
void (*flash_out_hook)(long addr, int offset, int val) = 0;

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <asm/io.h>

/* Size of read() for byte oriented parts.
   Page oriented parts read in units of the chip page size.
*/
#define RD_PAGE_SIZE 4096

enum flash_flags { ERASE_FIRST=1, };
struct flash_id_table {
	unsigned char vendor_id, device_id;
	enum flash_flags flags;
	int mem_size, page_size;
	char *part_name;
} static flash_id[] = {
	{ 0x12, 0x34, 0,   8*1024,   1, "Unknown Flash EEPROM", },
	/* Sector program, no erase needed parts. */
	{ 0xBF, 0x07, 0, 128*1024, 128, "Silicon Storage Technology SST29EE010", },
	{ 0xBF, 0x08, 0, 128*1024, 128, "Silicon Storage Technology SST29LE010", },
	{ 0x1F, 0xDC, 0,  32*1024,  64, "Atmel AT29C256", },
	{ 0x1F, 0xBC, 0,  32*1024,  64, "Atmel AT29LV256", },
	{ 0x1F, 0x5D, 0,  64*1024, 128, "Atmel AT29C512", },
	{ 0x1F, 0x3D, 0,  64*1024, 128, "Atmel AT29LV512", },
	{ 0x1F, 0xD5, 0, 128*1024, 128, "Atmel AT29C010A", },
	{ 0x1F, 0x35, 0, 128*1024, 128, "Atmel AT29LV010A", },
	{ 0x1F, 0xDA, 0, 256*1024, 256, "Atmel AT29C020", },
	{ 0x1F, 0xBA, 0, 256*1024, 256, "Atmel AT29LV020", },
	{ 0x1F, 0x5B, 0, 512*1024, 512, "Atmel AT29C040", },
	{ 0x1F, 0x3B, 0, 512*1024, 512, "Atmel AT29LV040", },
	{ 0x1F, 0xA4, 0, 512*1024, 256, "Atmel AT29C040A", },
	{ 0x1F, 0xC4, 0, 512*1024, 256, "Atmel AT29LV040A", },
	/* Bulk-erase, byte-program parts. */
	{ 0x1F, 0x03, ERASE_FIRST, 64*1024,   1, "Atmel AT49BV512", },
	{ 0,},
};

/* A few limits based on the table above. */
#define MAX_PAGE_SIZE 1024
#define MAX_PART_SIZE 512*1024

/* Index of internal functions. */
static int get_part_id(long addr_ioaddr, long data_ioaddr);
void sst_flash_clear(long addr_ioaddr, long data_ioaddr);
static void do_cmd(long addr_ioaddr, long data_ioaddr, int cmd);

static inline void flash_out(long addr_ioaddr, long data_ioaddr, int offset,
							 int val)
{
	if (flash_out_hook) {
		(*flash_out_hook)(addr_ioaddr, offset, val);
		return;
	}
	outl(offset, addr_ioaddr);
	outb(val, data_ioaddr);
}
static inline int flash_in(long addr_ioaddr, long data_ioaddr, int offset)
{
	if (flash_in_hook)
		return (*flash_in_hook)(addr_ioaddr, offset);
	outl(offset, addr_ioaddr);
	return inb(data_ioaddr) & 0xff;
}

static int get_part_id(long addr_ioaddr, long data_ioaddr)
{
	int id0, id1;
	int old[2];
	int i;

	old[0] = flash_in(addr_ioaddr, data_ioaddr, 0);
	old[1] = flash_in(addr_ioaddr, data_ioaddr, 1);

#if 0
	/* Check for an immediate-write Flash by checking for programming delay.
	   However this check is falsely triggered / unreliable. */
	flash_out(addr_ioaddr, data_ioaddr, 0, old[0]);
	if (flash_in(addr_ioaddr, data_ioaddr, 0) != old[0]) {
		if (verbose)
			printf("No BIOS ROM ID, immediate-write flash (%2.2x).\n",
				   flash_in(addr_ioaddr, data_ioaddr, 0));
		for (i = 10000; i > 0; i--)
			if (flash_in(addr_ioaddr, data_ioaddr, 0) == old[0])
				break;
		return -1;
	}
#endif

	do_cmd(addr_ioaddr, data_ioaddr, 0x90);
	/* usleep(10);*/
	id0 = flash_in(addr_ioaddr, data_ioaddr, 0);
	id1 = flash_in(addr_ioaddr, data_ioaddr, 1);
#if 0  /* Test code only. */
	for (i = 0; i < 1000; i++) {
		printf("Flash ID is %x %x\r", id0, id1);
		id0 = flash_in(addr_ioaddr, data_ioaddr, 0);
		id1 = flash_in(addr_ioaddr, data_ioaddr, 1);
	}
#endif
	if (old[0] == id0  &&  old[1] == id1) {
		/* Some SST parts only respond to the two-part ID command. */
		printf("Hmmm, no response to the ID command, trying again..\n");
		do_cmd(addr_ioaddr, data_ioaddr, 0x80);
		do_cmd(addr_ioaddr, data_ioaddr, 0x60);
		usleep(10);
		id0 = flash_in(addr_ioaddr, data_ioaddr, 0);
		id1 = flash_in(addr_ioaddr, data_ioaddr, 1);
		if (old[0] == id0  &&  old[1] == id1)
			printf("ACKKK, this may not be a programmable Flash part!\n");
	}

	if (verbose > 1)
		printf(" BIOS ROM ID %2.2X %2.2X, looking up vendor...\n", id0, id1);

	/* Exit ID mode. */
	do_cmd(addr_ioaddr, data_ioaddr, 0xF0);
	usleep(10);

	for (i = 0; flash_id[i].vendor_id; i++)
		if (flash_id[i].vendor_id == id0  &&  flash_id[i].device_id == id1)
			break;
	if (flash_id[i].vendor_id == 0) {
		printf("Unknown BIOS ROM ID %2.2X %2.2X.\n", id0, id1);
		return -1;
	}
	if (verbose)
		printf("BIOS ROM ID %2.2X %2.2X, vendor %s:\n", id0, id1,
			   flash_id[i].part_name ? flash_id[i].part_name : "");
	return i;
}

int flash_show(long addr_ioaddr, long data_ioaddr)
{
	unsigned char old_inval[16], inval[16];
	int addr, did_dots = 0;
	int i, part_id, mem_size;

	part_id = get_part_id(addr_ioaddr, data_ioaddr);
	mem_size = (part_id < 0) ? 64*1024 : flash_id[part_id].mem_size;

	for (addr = 0; addr < mem_size; addr+=16) {
		for (i = 0; i < 16; i++) {
			inval[i] = flash_in(addr_ioaddr, data_ioaddr, addr + i);
		}
		if (addr == 0 || memcmp(inval, old_inval, sizeof(old_inval))) {
			printf("0x%3.3x: ", addr);
			for (i = 0; i < 16; i++)
				printf("%2.2x ", inval[i]);
			for (i = 0; i < 16; i++)
				printf("%c", isalnum(inval[i]) ? inval[i] : '.');
			printf("\n");
			memcpy(old_inval, inval, sizeof(old_inval));
			did_dots = 0;
		} else if (! did_dots) {
			did_dots = 1;
			printf("...\n");
		}
	}
	printf("\nBIOS ROM dump done.\n");
	return 0;
}

int flash_dump(long addr_ioaddr, long data_ioaddr, char *filename)
{
	char wbuf[4096];
	int fd, i, offset, cnt;
	int	part_id = get_part_id(addr_ioaddr, data_ioaddr);
	int mem_size = (part_id < 0) ? 8*1024 : flash_id[part_id].mem_size;

	printf("Saving the boot flash ROM into file '%s'...", filename);
	fflush(stdout);
	if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0444)) < 0) {
		perror("flash_dump");
		return -1;
	}
	for (offset = 0; offset < mem_size; offset += cnt) {
		int blk_size = sizeof(wbuf) < 128*1024 - offset
			? sizeof(wbuf) : 128*1024 - offset;
		for (i = 0; i < blk_size; i++) {
			wbuf[i] = flash_in(addr_ioaddr, data_ioaddr, offset + i);
		}
		cnt = write(fd, wbuf, i);
		if (cnt < 0) {
			perror("save_flash");
			return -1;
		}
	}
	printf("done.\n");
	return 0;
}

/* Bulk erase the flash ROM. */
void sst_flash_clear(long addr_ioaddr, long data_ioaddr)
{
	do_cmd(addr_ioaddr, data_ioaddr, 0x80);
	do_cmd(addr_ioaddr, data_ioaddr, 0x10);
	usleep(20000);				/* 20 msec. */
}

int flash_program(long addr_ioaddr, long data_ioaddr, char *filename)
{
	int fd, i;
	int offset = 0, eofflag = 0;
	int	part_id = get_part_id(addr_ioaddr, data_ioaddr);
	int page_size = part_id > 0 ? flash_id[part_id].page_size : 128;
	int	flags = flash_id[part_id].flags;

	if (verbose)
		fprintf(stderr, "%s", version_msg);
	if (part_id < 0) {
		fprintf(stderr, "  This is an unknown flash chip, which cannot be "
				"programmed.\n");
		return -1;
	}

	if ((fd = open(filename, O_RDONLY)) < 0) {
		perror("flash_program");
		return -1;
	}

	if (page_size == 1) {		/* This is a byte-by-byte part. */
		unsigned char rbuf[RD_PAGE_SIZE];

		if (flags & ERASE_FIRST)
			sst_flash_clear(addr_ioaddr, data_ioaddr);

		do {
			int rcnt = read(fd, rbuf, sizeof(rbuf));
			int offset = 0;

			if (rcnt < 0) {
				perror("flash_load");
				fprintf(stderr, "read(%d,%d,%d)\n",
						fd, offset, sizeof(rbuf));
				return -1;
			} else if (rcnt == 0) {
				eofflag++;
				break;
			}
			for (i = 0; i < rcnt; i++) {
				int j;
				do_cmd(addr_ioaddr, data_ioaddr, 0xA0);
				flash_out(addr_ioaddr, data_ioaddr, offset++, rbuf[i]);
				/* Poll for the write to complete, look for stable read. */
				for (j = 0; j < 100000; j++)
					if (flash_in(addr_ioaddr, data_ioaddr, 0) ==
						flash_in(addr_ioaddr, data_ioaddr, 0))
						break;
				if (verbose > 2)
					printf("Write %2.2x to %d completed after %d ticks.\n",
						   rbuf[i], offset, j);
			}

		} while (! eofflag  &&  offset < MAX_PART_SIZE);
		printf("\rFinished programming %d bytes into the flash ROM.\n",
			   offset);
		return 0;
	}
			
	do {
		int rcnt, cnt = 0;
		unsigned char rbuf[MAX_PAGE_SIZE];

		/* The flash image should be in a file, and we should never
		   encounter short reads.  But it's a disaster if we do, so we guard
		   against it. */
		do {
			rcnt = read(fd, rbuf + cnt, page_size - cnt);
			if (rcnt < 0) {
				perror("flash_load");
				fprintf(stderr, "read(%d,%p,%d)\n",
						fd, rbuf + cnt, page_size - cnt);
				return -1;
			} else if (rcnt == 0) {
				eofflag++;
				break;
			}
			cnt += rcnt;
		} while (page_size - cnt > 0);

		if (cnt == 0  &&  eofflag)
			break;

		/* At this point rbuf[] is either a full block, or the tail of
		   data to be written.  We cannot allow blocks to span the 128 byte
		   flash pages. */
		do_cmd(addr_ioaddr, data_ioaddr, 0xA0);
		for (i = 0; i < cnt; i++) {
			flash_out(addr_ioaddr, data_ioaddr, offset + i, rbuf[i]);
		}
		if (verbose) {
			printf("\rWriting a block of %d bytes at offset %#x..",
				   cnt, offset);
			if (verbose > 1)
				printf("%2.2x %2.2x %2.2x %2.2x %2.2x .",
					   rbuf[0], rbuf[1], rbuf[2], rbuf[3], rbuf[4]);
			fflush(stdout);
		}
		/* Pause for 200usec. to allow the write to start. */
		usleep(200);
		/* And then poll for the write to complete, look for stable read. */
		for (i = 0; i < 100000; i++)
			if (flash_in(addr_ioaddr, data_ioaddr, 0) ==
 				flash_in(addr_ioaddr, data_ioaddr, 0))
				break;
		if (verbose > 2)
			printf("Write completed after %d ticks.\n", i);
		/* Verify the write. */
		for (i = 0; i < cnt; i++) {
			unsigned char val = flash_in(addr_ioaddr, data_ioaddr, offset + i);
			if (val != rbuf[i]) {
				fprintf(stderr, "\n Flash ROM write failed at offset %d, "
						"0x%2.2x vs. 0x%2.2x.\n", offset + i, val, rbuf[i]);
				return -1;
			}
		}
		offset += cnt;
	} while (! eofflag  &&  offset < MAX_PART_SIZE);

	printf("\rFinished programming %d bytes into the flash ROM.\n",
		   offset);
	return 0;
}

static void do_cmd(long addr_ioaddr, long data_ioaddr, int cmd)
{
	flash_out(addr_ioaddr, data_ioaddr, 0x5555, 0xAA);
	flash_out(addr_ioaddr, data_ioaddr, 0x2AAA, 0x55);
	flash_out(addr_ioaddr, data_ioaddr, 0x5555, cmd);
	/* Alternate command is simply
	   flash_out(addr_ioaddr, data_ioaddr, 0, cmd); */
}

/*
 * Local variables:
 *  compile-command: "cc -O -Wall -c libflash.c"
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */