2003-10-25 19:01:29 +02:00
|
|
|
/*
|
|
|
|
* flash_rom.c: Flash programming utility for SiS 630/950 M/Bs
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright 2000 Silicon Integrated System Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Reference:
|
|
|
|
* 1. SiS 630 Specification
|
|
|
|
* 2. SiS 950 Specification
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "flash.h"
|
|
|
|
#include "jedec.h"
|
|
|
|
#include "m29f400bt.h"
|
|
|
|
#include "82802ab.h"
|
|
|
|
#include "msys_doc.h"
|
|
|
|
#include "am29f040b.h"
|
|
|
|
#include "sst28sf040.h"
|
|
|
|
#include "w49f002u.h"
|
|
|
|
#include "sst39sf020.h"
|
2004-03-17 22:47:30 +01:00
|
|
|
#include "sst49lf040.h"
|
2004-02-10 22:34:18 +01:00
|
|
|
#include "pm49fl004.h"
|
2003-10-25 19:01:29 +02:00
|
|
|
#include "mx29f002.h"
|
2004-09-30 18:37:01 +02:00
|
|
|
#include "sst_fwhub.h"
|
2003-10-25 19:01:29 +02:00
|
|
|
|
|
|
|
struct flashchip flashchips[] = {
|
2004-03-27 01:18:15 +01:00
|
|
|
{"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64 * 1024,
|
|
|
|
probe_29f040b, erase_29f040b, write_29f040b, NULL},
|
|
|
|
{"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256,
|
|
|
|
probe_jedec, erase_chip_jedec, write_jedec, NULL},
|
|
|
|
{"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64 * 1024,
|
|
|
|
probe_29f002, erase_29f002, write_29f002, NULL},
|
|
|
|
{"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128,
|
|
|
|
probe_jedec, erase_chip_jedec, write_jedec, NULL},
|
|
|
|
{"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256,
|
|
|
|
probe_28sf040, erase_28sf040, write_28sf040, NULL},
|
|
|
|
{"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096,
|
|
|
|
probe_jedec, erase_chip_jedec, write_39sf020,NULL},
|
|
|
|
{"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
|
|
|
|
probe_jedec, erase_chip_jedec, write_39sf020,NULL},
|
|
|
|
{"SST49LF040", SST_ID, SST_49LF040, NULL, 512, 4096,
|
|
|
|
probe_jedec, erase_chip_jedec, write_49lf040,NULL},
|
|
|
|
{"SST49LF080A", SST_ID, SST_49LF080A, NULL, 1024, 4096,
|
|
|
|
probe_jedec, erase_chip_jedec, write_49lf040,NULL},
|
|
|
|
{"SST49LF002A", SST_ID, SST_49LF002A, NULL, 256, 4096,
|
|
|
|
probe_jedec, erase_chip_jedec, write_49lf040,NULL},
|
|
|
|
{"SST49LF003A", SST_ID, SST_49LF003A, NULL, 384, 4096,
|
|
|
|
probe_jedec, erase_chip_jedec, write_49lf040,NULL},
|
|
|
|
{"SST49LF004A", SST_ID, SST_49LF004A, NULL, 512, 4096,
|
|
|
|
probe_jedec, erase_chip_jedec, write_49lf040,NULL},
|
|
|
|
{"SST49LF008A", SST_ID, SST_49LF008A, NULL, 1024, 4096,
|
2004-09-30 18:37:01 +02:00
|
|
|
probe_sst_fwhub, erase_sst_fwhub, write_sst_fwhub, NULL},
|
2004-03-27 01:18:15 +01:00
|
|
|
{"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64 * 1024,
|
|
|
|
probe_jedec, erase_chip_jedec, write_49fl004,NULL},
|
|
|
|
{"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
|
|
|
|
probe_jedec, erase_chip_jedec, write_jedec, NULL},
|
|
|
|
{"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
|
|
|
|
probe_jedec, erase_chip_jedec, write_jedec, NULL},
|
|
|
|
{"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
|
|
|
|
probe_jedec, erase_chip_jedec, write_49f002, NULL},
|
|
|
|
{"M29F400BT", ST_ID, ST_M29F400BT, NULL, 512, 64 * 1024,
|
|
|
|
probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL},
|
|
|
|
{"82802ab", 137, 173, NULL, 512, 64 * 1024,
|
|
|
|
probe_82802ab, erase_82802ab, write_82802ab, NULL},
|
|
|
|
{"82802ac", 137, 172, NULL, 1024, 64 * 1024,
|
|
|
|
probe_82802ab, erase_82802ab, write_82802ab, NULL},
|
2004-03-17 23:22:08 +01:00
|
|
|
{"MD-2802 (M-Systems DiskOnChip Millennium Module)",
|
|
|
|
MSYSTEMS_ID, MSYSTEMS_MD2802,
|
2004-03-20 17:46:10 +01:00
|
|
|
NULL, 8, 8 * 1024,
|
2004-03-17 23:22:08 +01:00
|
|
|
probe_md2802, erase_md2802, write_md2802, read_md2802},
|
|
|
|
{NULL,}
|
2003-10-25 19:01:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
char *chip_to_probe = NULL;
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
struct flashchip *probe_flash(struct flashchip *flash)
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
2004-03-17 23:22:08 +01:00
|
|
|
int fd_mem;
|
2004-03-20 17:46:10 +01:00
|
|
|
volatile char *bios;
|
2004-03-17 23:22:08 +01:00
|
|
|
unsigned long size;
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
|
|
|
|
perror("Can not open /dev/mem");
|
|
|
|
exit(1);
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
while (flash->name != NULL) {
|
2004-03-27 01:18:15 +01:00
|
|
|
if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
|
2004-03-17 23:22:08 +01:00
|
|
|
flash++;
|
|
|
|
continue;
|
|
|
|
}
|
2004-03-27 01:18:15 +01:00
|
|
|
printf("Trying %s, %d KB\n", flash->name, flash->total_size);
|
2004-03-17 23:22:08 +01:00
|
|
|
size = flash->total_size * 1024;
|
|
|
|
/* BUG? what happens if getpagesize() > size!?
|
|
|
|
-> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
|
|
|
|
if (getpagesize() > size) {
|
|
|
|
size = getpagesize();
|
2004-03-20 17:46:10 +01:00
|
|
|
printf("%s: warning: size: %d -> %ld\n",
|
|
|
|
__FUNCTION__, flash->total_size * 1024,
|
|
|
|
(unsigned long) size);
|
2004-03-17 23:22:08 +01:00
|
|
|
}
|
2004-03-20 17:46:10 +01:00
|
|
|
bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
|
|
|
|
fd_mem, (off_t) (0xffffffff - size + 1));
|
2004-03-17 23:22:08 +01:00
|
|
|
if (bios == MAP_FAILED) {
|
|
|
|
perror("Error MMAP /dev/mem");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
flash->virt_addr = bios;
|
|
|
|
flash->fd_mem = fd_mem;
|
|
|
|
|
|
|
|
if (flash->probe(flash) == 1) {
|
2004-03-20 17:46:10 +01:00
|
|
|
printf("%s found at physical address: 0x%lx\n",
|
|
|
|
flash->name, (0xffffffff - size + 1));
|
2004-03-17 23:22:08 +01:00
|
|
|
return flash;
|
|
|
|
}
|
2004-03-20 17:46:10 +01:00
|
|
|
munmap((void *) bios, size);
|
2004-03-17 23:22:08 +01:00
|
|
|
flash++;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
2004-03-17 23:22:08 +01:00
|
|
|
return NULL;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
int verify_flash(struct flashchip *flash, char *buf, int verbose)
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
2004-03-20 18:05:01 +01:00
|
|
|
int i;
|
2004-03-20 17:46:10 +01:00
|
|
|
int total_size = flash->total_size * 1024;
|
|
|
|
volatile char *bios = flash->virt_addr;
|
2004-03-17 23:22:08 +01:00
|
|
|
|
|
|
|
printf("Verifying address: ");
|
2004-03-20 18:05:01 +01:00
|
|
|
for (i = 0; i < total_size; i++) {
|
2004-03-20 17:46:10 +01:00
|
|
|
if (verbose)
|
2004-03-17 23:22:08 +01:00
|
|
|
printf("0x%08x", i);
|
2004-03-20 17:46:10 +01:00
|
|
|
if (*(bios + i) != *(buf + i)) {
|
2004-03-17 23:22:08 +01:00
|
|
|
printf("FAILED\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-03-20 17:46:10 +01:00
|
|
|
if (verbose)
|
2004-03-17 23:22:08 +01:00
|
|
|
printf("\b\b\b\b\b\b\b\b\b\b");
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
2004-03-17 23:22:08 +01:00
|
|
|
if (verbose)
|
|
|
|
printf("\n");
|
|
|
|
else
|
|
|
|
printf("VERIFIED\n");
|
|
|
|
return 1;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void usage(const char *name)
|
|
|
|
{
|
2004-03-17 23:22:08 +01:00
|
|
|
printf("usage: %s [-rwv] [-c chipname][file]\n", name);
|
|
|
|
printf("-r: read flash and save into file\n"
|
|
|
|
"-w: write file into flash (default when file is specified)\n"
|
|
|
|
"-v: verify flash against file\n"
|
|
|
|
"-c: probe only for specified flash chip\n"
|
|
|
|
" If no file is specified, then all that happens\n"
|
|
|
|
" is that flash info is dumped\n");
|
|
|
|
exit(1);
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
int main(int argc, char *argv[])
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
2004-03-20 17:46:10 +01:00
|
|
|
char *buf;
|
2004-03-17 23:22:08 +01:00
|
|
|
unsigned long size;
|
2004-03-20 17:46:10 +01:00
|
|
|
FILE *image;
|
|
|
|
struct flashchip *flash;
|
2004-03-17 23:22:08 +01:00
|
|
|
int opt;
|
2004-03-18 21:27:33 +01:00
|
|
|
int read_it = 0, write_it = 0, verify_it = 0, verbose = 0;
|
2004-03-17 23:22:08 +01:00
|
|
|
char *filename = NULL;
|
|
|
|
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
|
2004-03-18 21:27:33 +01:00
|
|
|
while ((opt = getopt(argc, argv, "rwvVc:")) != EOF) {
|
2004-03-17 23:22:08 +01:00
|
|
|
switch (opt) {
|
|
|
|
case 'r':
|
|
|
|
read_it = 1;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
write_it = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verify_it = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
chip_to_probe = strdup(optarg);
|
|
|
|
break;
|
2004-03-18 21:27:33 +01:00
|
|
|
case 'V':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
2004-03-17 23:22:08 +01:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (read_it && write_it) {
|
|
|
|
printf("-r and -w are mutually exclusive\n");
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
if (optind < argc)
|
|
|
|
filename = argv[optind++];
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-27 01:18:15 +01:00
|
|
|
printf("Calibrating timer since microsleep sucks ... takes a second\n");
|
2004-03-17 23:22:08 +01:00
|
|
|
myusec_calibrate_delay();
|
|
|
|
printf("OK, calibrated, now do the deed\n");
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
/* try to enable it. Failure IS an option, since not all motherboards
|
|
|
|
* really need this to be done, etc., etc. It sucks.
|
|
|
|
*/
|
|
|
|
(void) enable_flash_write();
|
2004-03-20 17:46:10 +01:00
|
|
|
|
|
|
|
if ((flash = probe_flash(flashchips)) == NULL) {
|
2004-03-17 23:22:08 +01:00
|
|
|
printf("EEPROM not found\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
printf("Part is %s\n", flash->name);
|
2004-03-20 17:46:10 +01:00
|
|
|
if (!filename) {
|
2004-09-07 23:31:47 +02:00
|
|
|
printf("OK, only ENABLING flash write, but NOT FLASHING\n");
|
2004-03-17 23:22:08 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size = flash->total_size * 1024;
|
2004-03-20 17:46:10 +01:00
|
|
|
buf = (char *) calloc(size, sizeof(char));
|
2004-03-17 23:22:08 +01:00
|
|
|
|
|
|
|
if (read_it) {
|
2004-03-20 17:46:10 +01:00
|
|
|
if ((image = fopen(filename, "w")) == NULL) {
|
2004-03-17 23:22:08 +01:00
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("Reading Flash...");
|
2004-03-19 23:10:07 +01:00
|
|
|
if (flash->read == NULL)
|
2004-03-17 23:22:08 +01:00
|
|
|
memcpy(buf, (const char *) flash->virt_addr, size);
|
|
|
|
else
|
2004-03-20 17:46:10 +01:00
|
|
|
flash->read(flash, buf);
|
2004-03-17 23:22:08 +01:00
|
|
|
fwrite(buf, sizeof(char), size, image);
|
|
|
|
fclose(image);
|
|
|
|
printf("done\n");
|
|
|
|
} else {
|
2004-03-20 17:46:10 +01:00
|
|
|
if ((image = fopen(filename, "r")) == NULL) {
|
2004-03-17 23:22:08 +01:00
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-03-20 17:46:10 +01:00
|
|
|
fread(buf, sizeof(char), size, image);
|
2004-03-17 23:22:08 +01:00
|
|
|
fclose(image);
|
|
|
|
}
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
if (write_it || (!read_it && !verify_it))
|
2004-03-20 17:46:10 +01:00
|
|
|
flash->write(flash, buf);
|
2004-03-17 23:22:08 +01:00
|
|
|
if (verify_it)
|
2004-03-20 17:46:10 +01:00
|
|
|
verify_flash(flash, buf, verbose);
|
2004-03-17 23:22:08 +01:00
|
|
|
return 0;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|