2 * flash_rom.c: Flash programming utility for SiS 630/950 M/Bs
5 * Copyright 2000 Silicon Integrated System Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * 1. SiS 630 Specification
24 * 2. SiS 950 Specification
39 #include "m29f400bt.h"
42 #include "am29f040b.h"
43 #include "sst28sf040.h"
45 #include "sst39sf020.h"
46 #include "sst49lf040.h"
47 #include "pm49fl004.h"
50 struct flashchip flashchips[] = {
51 {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64 * 1024,
52 probe_29f040b, erase_29f040b, write_29f040b, NULL},
53 {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256,
54 probe_jedec, erase_chip_jedec, write_jedec, NULL},
55 {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64 * 1024,
56 probe_29f002, erase_29f002, write_29f002, NULL},
57 {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128,
58 probe_jedec, erase_chip_jedec, write_jedec, NULL},
59 {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256,
60 probe_28sf040, erase_28sf040, write_28sf040, NULL},
61 {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096,
62 probe_jedec, erase_chip_jedec, write_39sf020,NULL},
63 {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
64 probe_jedec, erase_chip_jedec, write_39sf020,NULL},
65 {"SST49LF040", SST_ID, SST_49LF040, NULL, 512, 4096,
66 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
67 {"SST49LF080A", SST_ID, SST_49LF080A, NULL, 1024, 4096,
68 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
69 {"SST49LF002A", SST_ID, SST_49LF002A, NULL, 256, 4096,
70 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
71 {"SST49LF003A", SST_ID, SST_49LF003A, NULL, 384, 4096,
72 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
73 {"SST49LF004A", SST_ID, SST_49LF004A, NULL, 512, 4096,
74 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
75 {"SST49LF008A", SST_ID, SST_49LF008A, NULL, 1024, 4096,
76 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
77 {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64 * 1024,
78 probe_jedec, erase_chip_jedec, write_49fl004,NULL},
79 {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
80 probe_jedec, erase_chip_jedec, write_jedec, NULL},
81 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
82 probe_jedec, erase_chip_jedec, write_jedec, NULL},
83 {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
84 probe_jedec, erase_chip_jedec, write_49f002, NULL},
85 {"M29F400BT", ST_ID, ST_M29F400BT, NULL, 512, 64 * 1024,
86 probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL},
87 {"82802ab", 137, 173, NULL, 512, 64 * 1024,
88 probe_82802ab, erase_82802ab, write_82802ab, NULL},
89 {"82802ac", 137, 172, NULL, 1024, 64 * 1024,
90 probe_82802ab, erase_82802ab, write_82802ab, NULL},
91 {"MD-2802 (M-Systems DiskOnChip Millennium Module)",
92 MSYSTEMS_ID, MSYSTEMS_MD2802,
94 probe_md2802, erase_md2802, write_md2802, read_md2802},
98 char *chip_to_probe = NULL;
100 struct flashchip *probe_flash(struct flashchip *flash)
106 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
107 perror("Can not open /dev/mem");
111 while (flash->name != NULL) {
112 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
116 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
117 size = flash->total_size * 1024;
118 /* BUG? what happens if getpagesize() > size!?
119 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
120 if (getpagesize() > size) {
121 size = getpagesize();
122 printf("%s: warning: size: %d -> %ld\n",
123 __FUNCTION__, flash->total_size * 1024,
124 (unsigned long) size);
126 bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
127 fd_mem, (off_t) (0xffffffff - size + 1));
128 if (bios == MAP_FAILED) {
129 perror("Error MMAP /dev/mem");
132 flash->virt_addr = bios;
133 flash->fd_mem = fd_mem;
135 if (flash->probe(flash) == 1) {
136 printf("%s found at physical address: 0x%lx\n",
137 flash->name, (0xffffffff - size + 1));
140 munmap((void *) bios, size);
146 int verify_flash(struct flashchip *flash, char *buf, int verbose)
149 int total_size = flash->total_size * 1024;
150 volatile char *bios = flash->virt_addr;
152 printf("Verifying address: ");
153 for (i = 0; i < total_size; i++) {
156 if (*(bios + i) != *(buf + i)) {
161 printf("\b\b\b\b\b\b\b\b\b\b");
166 printf("VERIFIED\n");
171 void usage(const char *name)
173 printf("usage: %s [-rwv] [-c chipname][file]\n", name);
174 printf("-r: read flash and save into file\n"
175 "-w: write file into flash (default when file is specified)\n"
176 "-v: verify flash against file\n"
177 "-c: probe only for specified flash chip\n"
178 " If no file is specified, then all that happens\n"
179 " is that flash info is dumped\n");
183 int main(int argc, char *argv[])
188 struct flashchip *flash;
190 int read_it = 0, write_it = 0, verify_it = 0, verbose = 0;
191 char *filename = NULL;
193 setbuf(stdout, NULL);
195 while ((opt = getopt(argc, argv, "rwvVc:")) != EOF) {
207 chip_to_probe = strdup(optarg);
217 if (read_it && write_it) {
218 printf("-r and -w are mutually exclusive\n");
223 filename = argv[optind++];
225 printf("Calibrating timer since microsleep sucks ... takes a second\n");
226 myusec_calibrate_delay();
227 printf("OK, calibrated, now do the deed\n");
229 /* try to enable it. Failure IS an option, since not all motherboards
230 * really need this to be done, etc., etc. It sucks.
232 (void) enable_flash_write();
234 if ((flash = probe_flash(flashchips)) == NULL) {
235 printf("EEPROM not found\n");
239 printf("Part is %s\n", flash->name);
242 ("OK, only ENABLING flash write, but NOT FLASHING\n");
245 size = flash->total_size * 1024;
246 buf = (char *) calloc(size, sizeof(char));
249 if ((image = fopen(filename, "w")) == NULL) {
253 printf("Reading Flash...");
254 if (flash->read == NULL)
255 memcpy(buf, (const char *) flash->virt_addr, size);
257 flash->read(flash, buf);
258 fwrite(buf, sizeof(char), size, image);
262 if ((image = fopen(filename, "r")) == NULL) {
266 fread(buf, sizeof(char), size, image);
270 if (write_it || (!read_it && !verify_it))
271 flash->write(flash, buf);
273 verify_flash(flash, buf, verbose);