blob: 3fd56e31d868dad8977c81f316117a8b7940562d [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
2 * flash_rom.c: Flash programming utility for SiS 630/950 M/Bs
3 *
4 *
5 * Copyright 2000 Silicon Integrated System Corporation
6 *
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.
11 *
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.
16 *
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.
20 *
21 *
22 * Reference:
23 * 1. SiS 630 Specification
24 * 2. SiS 950 Specification
25 *
26 * $Id$
27 */
28
29#include <errno.h>
30#include <fcntl.h>
31#include <sys/mman.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000032#include <unistd.h>
33#include <stdio.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000034#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000035#include <stdlib.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036
37#include "flash.h"
38#include "jedec.h"
Ronald G. Minnich3c910ed2002-05-28 23:29:17 +000039#include "m29f400bt.h"
Ronald G. Minnich56439422002-09-06 16:58:14 +000040#include "82802ab.h"
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000041#include "msys_doc.h"
42#include "am29f040b.h"
43#include "sst28sf040.h"
44#include "w49f002u.h"
45#include "sst39sf020.h"
David Hendricks3770a112004-03-17 21:47:30 +000046#include "sst49lf040.h"
Ronald G. Minniche3dad012004-02-10 21:34:18 +000047#include "pm49fl004.h"
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000048#include "mx29f002.h"
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000049#include "sst_fwhub.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000050
51struct flashchip flashchips[] = {
Ollie Lho8b8897a2004-03-27 00:18:15 +000052 {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64 * 1024,
53 probe_29f040b, erase_29f040b, write_29f040b, NULL},
54 {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256,
55 probe_jedec, erase_chip_jedec, write_jedec, NULL},
56 {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64 * 1024,
57 probe_29f002, erase_29f002, write_29f002, NULL},
58 {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128,
59 probe_jedec, erase_chip_jedec, write_jedec, NULL},
60 {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256,
61 probe_28sf040, erase_28sf040, write_28sf040, NULL},
62 {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096,
63 probe_jedec, erase_chip_jedec, write_39sf020,NULL},
64 {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
65 probe_jedec, erase_chip_jedec, write_39sf020,NULL},
66 {"SST49LF040", SST_ID, SST_49LF040, NULL, 512, 4096,
67 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
68 {"SST49LF080A", SST_ID, SST_49LF080A, NULL, 1024, 4096,
69 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
70 {"SST49LF002A", SST_ID, SST_49LF002A, NULL, 256, 4096,
71 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
72 {"SST49LF003A", SST_ID, SST_49LF003A, NULL, 384, 4096,
73 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
74 {"SST49LF004A", SST_ID, SST_49LF004A, NULL, 512, 4096,
75 probe_jedec, erase_chip_jedec, write_49lf040,NULL},
76 {"SST49LF008A", SST_ID, SST_49LF008A, NULL, 1024, 4096,
Ronald G. Minnich1f4d6532004-09-30 16:37:01 +000077 probe_sst_fwhub, erase_sst_fwhub, write_sst_fwhub, NULL},
Ollie Lho8b8897a2004-03-27 00:18:15 +000078 {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64 * 1024,
79 probe_jedec, erase_chip_jedec, write_49fl004,NULL},
80 {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
81 probe_jedec, erase_chip_jedec, write_jedec, NULL},
82 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
83 probe_jedec, erase_chip_jedec, write_jedec, NULL},
84 {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
85 probe_jedec, erase_chip_jedec, write_49f002, NULL},
86 {"M29F400BT", ST_ID, ST_M29F400BT, NULL, 512, 64 * 1024,
87 probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL},
88 {"82802ab", 137, 173, NULL, 512, 64 * 1024,
89 probe_82802ab, erase_82802ab, write_82802ab, NULL},
90 {"82802ac", 137, 172, NULL, 1024, 64 * 1024,
91 probe_82802ab, erase_82802ab, write_82802ab, NULL},
Ollie Lhocbbf1252004-03-17 22:22:08 +000092 {"MD-2802 (M-Systems DiskOnChip Millennium Module)",
93 MSYSTEMS_ID, MSYSTEMS_MD2802,
Ollie Lho761bf1b2004-03-20 16:46:10 +000094 NULL, 8, 8 * 1024,
Ollie Lhocbbf1252004-03-17 22:22:08 +000095 probe_md2802, erase_md2802, write_md2802, read_md2802},
96 {NULL,}
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000097};
98
Ronald G. Minnichceec4202003-07-25 04:37:41 +000099char *chip_to_probe = NULL;
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000100
Ollie Lho761bf1b2004-03-20 16:46:10 +0000101struct flashchip *probe_flash(struct flashchip *flash)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000102{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000103 int fd_mem;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000104 volatile char *bios;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000105 unsigned long size;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000106
Ollie Lhocbbf1252004-03-17 22:22:08 +0000107 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
108 perror("Can not open /dev/mem");
109 exit(1);
110 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000111
Ollie Lhocbbf1252004-03-17 22:22:08 +0000112 while (flash->name != NULL) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000113 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000114 flash++;
115 continue;
116 }
Ollie Lho8b8897a2004-03-27 00:18:15 +0000117 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000118 size = flash->total_size * 1024;
119 /* BUG? what happens if getpagesize() > size!?
120 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
121 if (getpagesize() > size) {
122 size = getpagesize();
Ollie Lho761bf1b2004-03-20 16:46:10 +0000123 printf("%s: warning: size: %d -> %ld\n",
124 __FUNCTION__, flash->total_size * 1024,
125 (unsigned long) size);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000126 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000127 bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
128 fd_mem, (off_t) (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +0000129 if (bios == MAP_FAILED) {
130 perror("Error MMAP /dev/mem");
131 exit(1);
132 }
133 flash->virt_addr = bios;
134 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000135
Ollie Lhocbbf1252004-03-17 22:22:08 +0000136 if (flash->probe(flash) == 1) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000137 printf("%s found at physical address: 0x%lx\n",
138 flash->name, (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +0000139 return flash;
140 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000141 munmap((void *) bios, size);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000142 flash++;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000143 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000144 return NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000145}
146
Ollie Lho761bf1b2004-03-20 16:46:10 +0000147int verify_flash(struct flashchip *flash, char *buf, int verbose)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000148{
Ollie Lhof51a7662004-03-20 17:05:01 +0000149 int i;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000150 int total_size = flash->total_size * 1024;
151 volatile char *bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000152
Ollie Lhocbbf1252004-03-17 22:22:08 +0000153 printf("Verifying address: ");
Ollie Lhof51a7662004-03-20 17:05:01 +0000154 for (i = 0; i < total_size; i++) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000155 if (verbose)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000156 printf("0x%08x", i);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000157 if (*(bios + i) != *(buf + i)) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000158 printf("FAILED\n");
159 return 0;
160 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000161 if (verbose)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000162 printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000163 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000164 if (verbose)
165 printf("\n");
166 else
167 printf("VERIFIED\n");
168 return 1;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000169}
170
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000171
Ronald G. Minniche5559572003-03-28 03:29:43 +0000172void usage(const char *name)
173{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000174 printf("usage: %s [-rwv] [-c chipname][file]\n", name);
175 printf("-r: read flash and save into file\n"
176 "-w: write file into flash (default when file is specified)\n"
177 "-v: verify flash against file\n"
178 "-c: probe only for specified flash chip\n"
179 " If no file is specified, then all that happens\n"
180 " is that flash info is dumped\n");
181 exit(1);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000182}
183
Ollie Lho761bf1b2004-03-20 16:46:10 +0000184int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000185{
Ollie Lho761bf1b2004-03-20 16:46:10 +0000186 char *buf;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000187 unsigned long size;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000188 FILE *image;
189 struct flashchip *flash;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000190 int opt;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000191 int read_it = 0, write_it = 0, verify_it = 0, verbose = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000192 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000193
Ollie Lhocbbf1252004-03-17 22:22:08 +0000194 setbuf(stdout, NULL);
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000195
Ollie Lho789ad3d2004-03-18 20:27:33 +0000196 while ((opt = getopt(argc, argv, "rwvVc:")) != EOF) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000197 switch (opt) {
198 case 'r':
199 read_it = 1;
200 break;
201 case 'w':
202 write_it = 1;
203 break;
204 case 'v':
205 verify_it = 1;
206 break;
207 case 'c':
208 chip_to_probe = strdup(optarg);
209 break;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000210 case 'V':
211 verbose = 1;
212 break;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000213 default:
214 usage(argv[0]);
215 break;
216 }
217 }
218 if (read_it && write_it) {
219 printf("-r and -w are mutually exclusive\n");
220 usage(argv[0]);
221 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000222
Ollie Lhocbbf1252004-03-17 22:22:08 +0000223 if (optind < argc)
224 filename = argv[optind++];
Ronald G. Minniche5559572003-03-28 03:29:43 +0000225
Ollie Lho8b8897a2004-03-27 00:18:15 +0000226 printf("Calibrating timer since microsleep sucks ... takes a second\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000227 myusec_calibrate_delay();
228 printf("OK, calibrated, now do the deed\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000229
Ollie Lhocbbf1252004-03-17 22:22:08 +0000230 /* try to enable it. Failure IS an option, since not all motherboards
231 * really need this to be done, etc., etc. It sucks.
232 */
233 (void) enable_flash_write();
Ollie Lho761bf1b2004-03-20 16:46:10 +0000234
235 if ((flash = probe_flash(flashchips)) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000236 printf("EEPROM not found\n");
237 exit(1);
238 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000239
Ollie Lhocbbf1252004-03-17 22:22:08 +0000240 printf("Part is %s\n", flash->name);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000241 if (!filename) {
Ollie Lhoaf474772004-09-07 21:31:47 +0000242 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000243 return 0;
244 }
245 size = flash->total_size * 1024;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000246 buf = (char *) calloc(size, sizeof(char));
Ollie Lhocbbf1252004-03-17 22:22:08 +0000247
248 if (read_it) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000249 if ((image = fopen(filename, "w")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000250 perror(filename);
251 exit(1);
252 }
253 printf("Reading Flash...");
Ollie Lho73eca802004-03-19 22:10:07 +0000254 if (flash->read == NULL)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000255 memcpy(buf, (const char *) flash->virt_addr, size);
256 else
Ollie Lho761bf1b2004-03-20 16:46:10 +0000257 flash->read(flash, buf);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000258 fwrite(buf, sizeof(char), size, image);
259 fclose(image);
260 printf("done\n");
261 } else {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000262 if ((image = fopen(filename, "r")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000263 perror(filename);
264 exit(1);
265 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000266 fread(buf, sizeof(char), size, image);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000267 fclose(image);
268 }
269
270 if (write_it || (!read_it && !verify_it))
Ollie Lho761bf1b2004-03-20 16:46:10 +0000271 flash->write(flash, buf);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000272 if (verify_it)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000273 verify_flash(flash, buf, verbose);
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000274 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000275}