blob: a4bb986723b17dc3fa0d3072011db8552c67ce98 [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. Minnichf4cf2ba2002-01-29 18:26:26 +000049
50struct flashchip flashchips[] = {
Ollie Lhocbbf1252004-03-17 22:22:08 +000051 {"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_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_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,
Ollie Lho73eca802004-03-19 22:10:07 +000062 probe_jedec, erase_jedec, write_39sf020, NULL},
Ollie Lhocbbf1252004-03-17 22:22:08 +000063 {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
Ollie Lho73eca802004-03-19 22:10:07 +000064 probe_jedec, erase_jedec, write_39sf020, NULL},
David Hendricks180850e2004-03-18 21:59:05 +000065 {"SST49LF040", SST_ID, SST_49LF040, NULL, 512, 4096,
Ollie Lho73eca802004-03-19 22:10:07 +000066 probe_jedec, erase_jedec, write_49lf040, NULL},
David Hendricks180850e2004-03-18 21:59:05 +000067 {"SST49LF080A", SST_ID, SST_49LF080A, NULL, 1024, 4096,
Ollie Lho73eca802004-03-19 22:10:07 +000068 probe_jedec, erase_jedec, write_49lf040, NULL},
David Hendricks180850e2004-03-18 21:59:05 +000069 {"SST49LF002A", SST_ID, SST_49LF002A, NULL, 256, 4096,
Ollie Lho73eca802004-03-19 22:10:07 +000070 probe_jedec, erase_jedec, write_49lf040, NULL},
David Hendricks180850e2004-03-18 21:59:05 +000071 {"SST49LF003A", SST_ID, SST_49LF003A, NULL, 384, 4096,
Ollie Lho73eca802004-03-19 22:10:07 +000072 probe_jedec, erase_jedec, write_49lf040, NULL},
David Hendricks180850e2004-03-18 21:59:05 +000073 {"SST49LF004A", SST_ID, SST_49LF004A, NULL, 512, 4096,
Ollie Lho73eca802004-03-19 22:10:07 +000074 probe_jedec, erase_jedec, write_49lf040, NULL},
David Hendricks180850e2004-03-18 21:59:05 +000075 {"SST49LF008A", SST_ID, SST_49LF008A, NULL, 1024, 4096,
Ollie Lho73eca802004-03-19 22:10:07 +000076 probe_jedec, erase_jedec, write_49lf040, NULL},
Ollie Lhocbbf1252004-03-17 22:22:08 +000077 {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64*1024,
Ollie Lho73eca802004-03-19 22:10:07 +000078 probe_jedec, erase_jedec, write_49fl004, NULL},
Ollie Lhocbbf1252004-03-17 22:22:08 +000079 {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
80 probe_jedec, erase_jedec, write_jedec, NULL},
81 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
82 probe_jedec, erase_jedec, write_jedec, NULL},
83 {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
Ollie Lho73eca802004-03-19 22:10:07 +000084 probe_jedec, erase_jedec, write_49f002, NULL},
85 {"M29F400BT", ST_ID, ST_M29F400BT ,NULL, 512, 64*1024,
Ollie Lhocbbf1252004-03-17 22:22:08 +000086 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,
93 NULL, 8, 8*1024,
94 probe_md2802, erase_md2802, write_md2802, read_md2802},
95 {NULL,}
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000096};
97
Ronald G. Minnichceec4202003-07-25 04:37:41 +000098char *chip_to_probe = NULL;
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +000099
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000100struct flashchip * probe_flash(struct flashchip * flash)
101{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000102 int fd_mem;
103 volatile char * bios;
104 unsigned long size;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000105
Ollie Lhocbbf1252004-03-17 22:22:08 +0000106 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
107 perror("Can not open /dev/mem");
108 exit(1);
109 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000110
Ollie Lhocbbf1252004-03-17 22:22:08 +0000111 while (flash->name != NULL) {
112 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
113 flash++;
114 continue;
115 }
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", __FUNCTION__,
123 flash->total_size * 1024, (unsigned long)size);
124 }
125 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
Ollie Lhocf29de82004-03-18 19:40:07 +0000126 fd_mem, (off_t) (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +0000127 if (bios == MAP_FAILED) {
128 perror("Error MMAP /dev/mem");
129 exit(1);
130 }
131 flash->virt_addr = bios;
132 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000133
Ollie Lhocbbf1252004-03-17 22:22:08 +0000134 if (flash->probe(flash) == 1) {
135 printf ("%s found at physical address: 0x%lx\n",
Ollie Lhocf29de82004-03-18 19:40:07 +0000136 flash->name, (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +0000137 return flash;
138 }
139 munmap ((void *) bios, size);
140 flash++;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000141 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000142 return NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000143}
144
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000145int verify_flash (struct flashchip * flash, char * buf, int verbose)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000146{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000147 int i = 0;
148 int total_size = flash->total_size *1024;
149 volatile char * bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000150
Ollie Lhocbbf1252004-03-17 22:22:08 +0000151 printf("Verifying address: ");
Ollie Lho789ad3d2004-03-18 20:27:33 +0000152 while (i < total_size) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000153 if (verbose)
154 printf("0x%08x", i);
155 if (*(bios+i) != *(buf+i)) {
156 printf("FAILED\n");
157 return 0;
158 }
159 if (verbose)
160 printf("\b\b\b\b\b\b\b\b\b\b");
Ollie Lho856943d2004-03-18 20:35:33 +0000161 i++;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000162 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000163 if (verbose)
164 printf("\n");
165 else
166 printf("VERIFIED\n");
167 return 1;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000168}
169
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000170
Ronald G. Minniche5559572003-03-28 03:29:43 +0000171void usage(const char *name)
172{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000173 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");
180 exit(1);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000181}
182
Ollie Lhocbbf1252004-03-17 22:22:08 +0000183int main (int argc, char * argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000184{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000185 char * buf;
186 unsigned long size;
187 FILE * image;
188 struct flashchip * flash;
189 int opt;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000190 int read_it = 0, write_it = 0, verify_it = 0, verbose = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000191 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000192
Ollie Lhocbbf1252004-03-17 22:22:08 +0000193 setbuf(stdout, NULL);
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000194
Ollie Lho789ad3d2004-03-18 20:27:33 +0000195 while ((opt = getopt(argc, argv, "rwvVc:")) != EOF) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000196 switch (opt) {
197 case 'r':
198 read_it = 1;
199 break;
200 case 'w':
201 write_it = 1;
202 break;
203 case 'v':
204 verify_it = 1;
205 break;
206 case 'c':
207 chip_to_probe = strdup(optarg);
208 break;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000209 case 'V':
210 verbose = 1;
211 break;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000212 default:
213 usage(argv[0]);
214 break;
215 }
216 }
217 if (read_it && write_it) {
218 printf("-r and -w are mutually exclusive\n");
219 usage(argv[0]);
220 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000221
Ollie Lhocbbf1252004-03-17 22:22:08 +0000222 if (optind < argc)
223 filename = argv[optind++];
Ronald G. Minniche5559572003-03-28 03:29:43 +0000224
Ollie Lhocbbf1252004-03-17 22:22:08 +0000225 printf("Calibrating timer since microsleep sucks ... takes a second\n");
226 myusec_calibrate_delay();
227 printf("OK, calibrated, now do the deed\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000228
Ollie Lhocbbf1252004-03-17 22:22:08 +0000229 /* try to enable it. Failure IS an option, since not all motherboards
230 * really need this to be done, etc., etc. It sucks.
231 */
232 (void) enable_flash_write();
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000233
Ollie Lhocbbf1252004-03-17 22:22:08 +0000234 if ((flash = probe_flash (flashchips)) == NULL) {
235 printf("EEPROM not found\n");
236 exit(1);
237 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000238
Ollie Lhocbbf1252004-03-17 22:22:08 +0000239 printf("Part is %s\n", flash->name);
240 if (!filename){
241 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
242 return 0;
243 }
244 size = flash->total_size * 1024;
245 buf = (char *) calloc (size, sizeof(char));
246
247 if (read_it) {
248 if ((image = fopen (filename, "w")) == NULL) {
249 perror(filename);
250 exit(1);
251 }
252 printf("Reading Flash...");
Ollie Lho73eca802004-03-19 22:10:07 +0000253 if (flash->read == NULL)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000254 memcpy(buf, (const char *) flash->virt_addr, size);
255 else
256 flash->read (flash, buf);
257 fwrite(buf, sizeof(char), size, image);
258 fclose(image);
259 printf("done\n");
260 } else {
261 if ((image = fopen (filename, "r")) == NULL) {
262 perror(filename);
263 exit(1);
264 }
265 fread (buf, sizeof(char), size, image);
266 fclose(image);
267 }
268
269 if (write_it || (!read_it && !verify_it))
270 flash->write (flash, buf);
271 if (verify_it)
Ollie Lho789ad3d2004-03-18 20:27:33 +0000272 verify_flash (flash, buf, verbose);
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000273 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000274}