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
42 #include "m29f400bt.h"
45 #include "am29f040b.h"
46 #include "sst28sf040.h"
48 #include "sst39sf020.h"
49 #include "pm49fl004.h"
52 struct flashchip flashchips[] = {
54 {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024,
55 probe_29f040b, erase_29f040b, write_29f040b, NULL},
56 {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256,
57 probe_jedec, erase_jedec, write_jedec, NULL},
58 {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024,
59 probe_29f002, erase_29f002, write_29f002, NULL},
60 {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128,
61 probe_jedec, erase_jedec, write_jedec, NULL},
62 {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256,
63 probe_28sf040, erase_28sf040, write_28sf040, NULL},
64 {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096,
65 probe_39sf020, erase_39sf020, write_39sf020, NULL},
66 {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
67 probe_39sf020, erase_39sf020, write_39sf020, NULL},
70 {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64*1024,
71 probe_49fl004, erase_49fl004, write_49fl004, NULL},
74 {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
75 probe_jedec, erase_jedec, write_jedec, NULL},
76 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
77 probe_jedec, erase_jedec, write_jedec, NULL},
78 {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
79 probe_49f002, erase_49f002, write_49f002, NULL},
80 {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024,
81 probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL},
82 {"82802ab", 137, 173 , NULL, 512, 64*1024,
83 probe_82802ab, erase_82802ab, write_82802ab, NULL},
84 {"82802ac", 137, 172 , NULL, 1024, 64*1024,
85 probe_82802ab, erase_82802ab, write_82802ab, NULL},
86 {"MD-2802 (M-Systems DiskOnChip Millennium Module)",
87 MSYSTEMS_ID, MSYSTEMS_MD2802,
89 probe_md2802, erase_md2802, write_md2802, read_md2802},
94 char *chip_to_probe = NULL;
96 int enable_flash_sis630 (struct pci_dev *dev, char *name)
100 /* get io privilege access PCI configuration space */
102 perror("Can not set io priviliage");
106 /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
107 outl(0x80000840, 0x0cf8);
108 b = inb(0x0cfc) | 0x0b;
110 /* Flash write enable on SiS 540/630 */
111 outl(0x80000845, 0x0cf8);
112 b = inb(0x0cfd) | 0x40;
115 /* The same thing on SiS 950 SuperIO side */
121 if (inb(0x2f) != 0x87) {
126 if (inb(0x4f) != 0x87) {
127 printf("Can not access SiS 950\n");
131 b = inb(0x4f) | 0xfc;
139 printf("2f is %#x\n", inb(0x2f));
140 b = inb(0x2f) | 0xfc;
151 enable_flash_e7500(struct pci_dev *dev, char *name) {
152 /* register 4e.b gets or'ed with one */
153 unsigned char old, new;
154 /* if it fails, it fails. There are so many variations of broken mobos
155 * that it is hard to argue that we should quit at this point.
158 old = pci_read_byte(dev, 0x4e);
165 pci_write_byte(dev, 0x4e, new);
167 if (pci_read_byte(dev, 0x4e) != new) {
168 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
176 enable_flash_vt8235(struct pci_dev *dev, char *name) {
177 unsigned char old, new, val;
181 /* get io privilege access PCI configuration space */
183 perror("Can not set io priviliage");
187 old = pci_read_byte(dev, 0x40);
194 ok = pci_write_byte(dev, 0x40, new);
196 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
200 /* enable GPIO15 which is connected to write protect. */
201 base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8);
202 val = inb(base + 0x4d);
204 outb(val, base + 0x4d);
214 enable_flash_vt8231(struct pci_dev *dev, char *name) {
217 val = pci_read_byte(dev, 0x40);
219 pci_write_byte(dev, 0x40, val);
221 if (pci_read_byte(dev, 0x40) != val) {
222 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
230 enable_flash_cs5530(struct pci_dev *dev, char *name) {
233 pci_write_byte(dev, 0x52, 0xee);
235 new = pci_read_byte(dev, 0x52);
238 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
246 enable_flash_sc1100(struct pci_dev *dev, char *name) {
249 pci_write_byte(dev, 0x52, 0xee);
251 new = pci_read_byte(dev, 0x52);
254 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
262 enable_flash_sis5595(struct pci_dev *dev, char *name) {
263 unsigned char new, newer;
265 new = pci_read_byte(dev, 0x45);
272 pci_write_byte(dev, 0x45, new);
274 newer = pci_read_byte(dev, 0x45);
276 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
278 printf("Stuck at 0x%x\n", newer);
286 static void dump_pci_device(struct pci_dev *dev)
291 for(i = 0; i <= 255; i++) {
293 if ((i & 0x0f) == 0) {
296 val = pci_read_byte(dev, i);
297 printf(" 0x%02x",val);
298 if ((i & 0x0f) == 0x0f) {
307 enable_flash_amd8111(struct pci_dev *dev, char *name) {
308 /* register 4e.b gets or'ed with one */
309 unsigned char old, new;
310 /* if it fails, it fails. There are so many variations of broken mobos
311 * that it is hard to argue that we should quit at this point.
314 // dump_pci_device(dev);
316 old = pci_read_byte(dev, 0x43);
322 pci_write_byte(dev, 0x43, new);
324 if (pci_read_byte(dev, 0x43) != new) {
325 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
331 old = pci_read_byte(dev, 0x40);
338 pci_write_byte(dev, 0x40, new);
340 if (pci_read_byte(dev, 0x40) != new) {
341 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
349 struct flashchip * probe_flash(struct flashchip * flash)
352 volatile char * bios;
355 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
356 perror("Can not open /dev/mem");
360 while (flash->name != NULL) {
361 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
365 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
366 size = flash->total_size * 1024;
367 /* BUG? what happens if getpagesize() > size!?
368 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
369 if(getpagesize() > size)
371 size = getpagesize();
372 printf("%s: warning: size: %d -> %ld\n", __FUNCTION__,
373 flash->total_size * 1024, (unsigned long)size);
375 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
376 fd_mem, (off_t) (0-size));
377 if (bios == MAP_FAILED) {
378 perror("Error MMAP /dev/mem");
381 flash->virt_addr = bios;
382 flash->fd_mem = fd_mem;
384 if (flash->probe(flash) == 1) {
385 printf ("%s found at physical address: 0x%lx\n",
386 flash->name, (0 - size));
389 munmap ((void *) bios, size);
395 int verify_flash (struct flashchip * flash, char * buf, int verbose)
398 int total_size = flash->total_size *1024;
399 volatile char * bios = flash->virt_addr;
401 printf("Verifying address: ");
402 while (i++ < total_size) {
405 if (*(bios+i) != *(buf+i)) {
410 printf("\b\b\b\b\b\b\b\b\b\b");
415 printf("VERIFIED\n");
419 // count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
421 unsigned long micro = 1;
424 myusec_calibrate_delay()
427 unsigned long timeusec;
428 struct timeval start, end;
430 void myusec_delay(int time);
432 printf("Setting up microsecond timing loop\n");
434 //fprintf(stderr, "Try %d\n", count);
435 gettimeofday(&start, 0);
437 gettimeofday(&end, 0);
438 timeusec = 1000000 * (end.tv_sec - start.tv_sec ) +
439 (end.tv_usec - start.tv_usec);
440 //fprintf(stderr, "timeusec is %d\n", timeusec);
442 if (timeusec < 1000000/4)
447 // compute one microsecond. That will be count / time
448 micro = count / timeusec;
450 fprintf(stderr, "%ldM loops per second\n", (unsigned long)micro);
456 myusec_delay(int time)
458 volatile unsigned long i;
459 for(i = 0; i < time * micro; i++)
463 typedef struct penable {
464 unsigned short vendor, device;
466 int (*doit)(struct pci_dev *dev, char *name);
469 FLASH_ENABLE enables[] = {
471 {0x1, 0x1, "sis630 -- what's the ID?", enable_flash_sis630},
472 {0x8086, 0x2480, "E7500", enable_flash_e7500},
473 {0x1106, 0x8231, "VT8231", enable_flash_vt8231},
474 {0x1106, 0x3177, "VT8235", enable_flash_vt8235},
475 {0x1078, 0x0100, "CS5530", enable_flash_cs5530},
476 {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
477 {0x1039, 0x8, "SIS5595", enable_flash_sis5595},
479 {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
483 enable_flash_write() {
485 struct pci_access *pacc;
486 struct pci_dev *dev = 0;
487 FLASH_ENABLE *enable = 0;
489 pacc = pci_alloc(); /* Get the pci_access structure */
490 /* Set all options you want -- here we stick with the defaults */
491 pci_init(pacc); /* Initialize the PCI library */
492 pci_scan_bus(pacc); /* We want to get the list of devices */
494 /* now let's try to find the chipset we have ... */
495 for(i = 0; i < sizeof(enables)/sizeof(enables[0]) && (! dev); i++) {
498 /* the first param is unused. */
499 pci_filter_init((struct pci_access *) 0, &f);
500 f.vendor = enables[i].vendor;
501 f.device = enables[i].device;
502 for(z=pacc->devices; z; z=z->next)
503 if (pci_filter_match(&f, z)) {
504 enable = &enables[i];
509 /* now do the deed. */
511 printf("Enabling flash write on %s...", enable->name);
512 if (enable->doit(dev, enable->name) == 0)
518 void usage(const char *name)
520 printf("usage: %s [-rwv] [-c chipname][file]\n", name);
521 printf("-r: read flash and save into file\n"
522 "-w: write file into flash (default when file is specified)\n"
523 "-v: verify flash against file\n"
524 "-c: probe only for specified flash chip\n"
525 " If no file is specified, then all that happens\n"
526 " is that flash info is dumped\n");
531 main (int argc, char * argv[])
536 struct flashchip * flash;
538 int read_it = 0, write_it = 0, verify_it = 0;
539 char *filename = NULL;
541 setbuf(stdout, NULL);
543 while ((opt = getopt(argc, argv, "rwvc:")) != EOF) {
555 chip_to_probe = strdup(optarg);
562 if (read_it && write_it) {
563 printf("-r and -w are mutually exclusive\n");
568 filename = argv[optind++];
570 printf("Calibrating timer since microsleep sucks ... takes a second\n");
571 myusec_calibrate_delay();
572 printf("OK, calibrated, now do the deed\n");
574 /* try to enable it. Failure IS an option, since not all motherboards
575 * really need this to be done, etc., etc. It sucks.
577 (void) enable_flash_write();
579 if ((flash = probe_flash (flashchips)) == NULL) {
580 printf("EEPROM not found\n");
584 printf("Part is %s\n", flash->name);
586 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
589 size = flash->total_size * 1024;
590 buf = (char *) calloc (size, sizeof(char));
593 if ((image = fopen (filename, "w")) == NULL) {
597 printf("Reading Flash...");
598 if(flash->read == NULL)
599 memcpy(buf, (const char *) flash->virt_addr, size);
601 flash->read (flash, buf);
602 fwrite(buf, sizeof(char), size, image);
606 if ((image = fopen (filename, "r")) == NULL) {
610 fread (buf, sizeof(char), size, image);
614 if (write_it || (!read_it && !verify_it))
615 flash->write (flash, buf);
617 verify_flash (flash, buf, /* verbose = */ 0);