blob: c26958f4baa3664146c42c8affcdfbf5e1055d92 [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,
62 probe_39sf020, erase_39sf020, write_39sf020, NULL},
63 {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
64 probe_39sf020, erase_39sf020, write_39sf020, NULL},
65 {"SST49LF040", SST_ID, SST_49LF040, NULL, 512, 4096,
66 probe_49lf040, erase_49lf040, write_49lf040, NULL},
67 //By LYH begin
68 {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64*1024,
69 probe_49fl004, erase_49fl004, write_49fl004, NULL},
70 //END
71 {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
72 probe_jedec, erase_jedec, write_jedec, NULL},
73 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
74 probe_jedec, erase_jedec, write_jedec, NULL},
75 {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
76 probe_49f002, erase_49f002, write_49f002, NULL},
77 {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024,
78 probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL},
79 {"82802ab", 137, 173 , NULL, 512, 64*1024,
80 probe_82802ab, erase_82802ab, write_82802ab, NULL},
81 {"82802ac", 137, 172 , NULL, 1024, 64*1024,
82 probe_82802ab, erase_82802ab, write_82802ab, NULL},
83 {"MD-2802 (M-Systems DiskOnChip Millennium Module)",
84 MSYSTEMS_ID, MSYSTEMS_MD2802,
85 NULL, 8, 8*1024,
86 probe_md2802, erase_md2802, write_md2802, read_md2802},
87 {NULL,}
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000088};
89
Ronald G. Minnichceec4202003-07-25 04:37:41 +000090char *chip_to_probe = NULL;
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +000091
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000092struct flashchip * probe_flash(struct flashchip * flash)
93{
Ollie Lhocbbf1252004-03-17 22:22:08 +000094 int fd_mem;
95 volatile char * bios;
96 unsigned long size;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000097
Ollie Lhocbbf1252004-03-17 22:22:08 +000098 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
99 perror("Can not open /dev/mem");
100 exit(1);
101 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000102
Ollie Lhocbbf1252004-03-17 22:22:08 +0000103 while (flash->name != NULL) {
104 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
105 flash++;
106 continue;
107 }
108 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
109 size = flash->total_size * 1024;
110 /* BUG? what happens if getpagesize() > size!?
111 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
112 if (getpagesize() > size) {
113 size = getpagesize();
114 printf("%s: warning: size: %d -> %ld\n", __FUNCTION__,
115 flash->total_size * 1024, (unsigned long)size);
116 }
117 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
Ollie Lhocf29de82004-03-18 19:40:07 +0000118 fd_mem, (off_t) (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +0000119 if (bios == MAP_FAILED) {
120 perror("Error MMAP /dev/mem");
121 exit(1);
122 }
123 flash->virt_addr = bios;
124 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000125
Ollie Lhocbbf1252004-03-17 22:22:08 +0000126 if (flash->probe(flash) == 1) {
127 printf ("%s found at physical address: 0x%lx\n",
Ollie Lhocf29de82004-03-18 19:40:07 +0000128 flash->name, (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +0000129 return flash;
130 }
131 munmap ((void *) bios, size);
132 flash++;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000133 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000134 return NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000135}
136
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000137int verify_flash (struct flashchip * flash, char * buf, int verbose)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000138{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000139 int i = 0;
140 int total_size = flash->total_size *1024;
141 volatile char * bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000142
Ollie Lhocbbf1252004-03-17 22:22:08 +0000143 printf("Verifying address: ");
Ollie Lho789ad3d2004-03-18 20:27:33 +0000144 while (i < total_size) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000145 if (verbose)
146 printf("0x%08x", i);
147 if (*(bios+i) != *(buf+i)) {
148 printf("FAILED\n");
149 return 0;
150 }
151 if (verbose)
152 printf("\b\b\b\b\b\b\b\b\b\b");
Ollie Lho856943d2004-03-18 20:35:33 +0000153 i++;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000154 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000155 if (verbose)
156 printf("\n");
157 else
158 printf("VERIFIED\n");
159 return 1;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000160}
161
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000162
Ronald G. Minniche5559572003-03-28 03:29:43 +0000163void usage(const char *name)
164{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000165 printf("usage: %s [-rwv] [-c chipname][file]\n", name);
166 printf("-r: read flash and save into file\n"
167 "-w: write file into flash (default when file is specified)\n"
168 "-v: verify flash against file\n"
169 "-c: probe only for specified flash chip\n"
170 " If no file is specified, then all that happens\n"
171 " is that flash info is dumped\n");
172 exit(1);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000173}
174
Ollie Lhocbbf1252004-03-17 22:22:08 +0000175int main (int argc, char * argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000176{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000177 char * buf;
178 unsigned long size;
179 FILE * image;
180 struct flashchip * flash;
181 int opt;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000182 int read_it = 0, write_it = 0, verify_it = 0, verbose = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000183 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000184
Ollie Lhocbbf1252004-03-17 22:22:08 +0000185 setbuf(stdout, NULL);
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000186
Ollie Lho789ad3d2004-03-18 20:27:33 +0000187 while ((opt = getopt(argc, argv, "rwvVc:")) != EOF) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000188 switch (opt) {
189 case 'r':
190 read_it = 1;
191 break;
192 case 'w':
193 write_it = 1;
194 break;
195 case 'v':
196 verify_it = 1;
197 break;
198 case 'c':
199 chip_to_probe = strdup(optarg);
200 break;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000201 case 'V':
202 verbose = 1;
203 break;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000204 default:
205 usage(argv[0]);
206 break;
207 }
208 }
209 if (read_it && write_it) {
210 printf("-r and -w are mutually exclusive\n");
211 usage(argv[0]);
212 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000213
Ollie Lhocbbf1252004-03-17 22:22:08 +0000214 if (optind < argc)
215 filename = argv[optind++];
Ronald G. Minniche5559572003-03-28 03:29:43 +0000216
Ollie Lhocbbf1252004-03-17 22:22:08 +0000217 printf("Calibrating timer since microsleep sucks ... takes a second\n");
218 myusec_calibrate_delay();
219 printf("OK, calibrated, now do the deed\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000220
Ollie Lhocbbf1252004-03-17 22:22:08 +0000221 /* try to enable it. Failure IS an option, since not all motherboards
222 * really need this to be done, etc., etc. It sucks.
223 */
224 (void) enable_flash_write();
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000225
Ollie Lhocbbf1252004-03-17 22:22:08 +0000226 if ((flash = probe_flash (flashchips)) == NULL) {
227 printf("EEPROM not found\n");
228 exit(1);
229 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000230
Ollie Lhocbbf1252004-03-17 22:22:08 +0000231 printf("Part is %s\n", flash->name);
232 if (!filename){
233 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
234 return 0;
235 }
236 size = flash->total_size * 1024;
237 buf = (char *) calloc (size, sizeof(char));
238
239 if (read_it) {
240 if ((image = fopen (filename, "w")) == NULL) {
241 perror(filename);
242 exit(1);
243 }
244 printf("Reading Flash...");
245 if(flash->read == NULL)
246 memcpy(buf, (const char *) flash->virt_addr, size);
247 else
248 flash->read (flash, buf);
249 fwrite(buf, sizeof(char), size, image);
250 fclose(image);
251 printf("done\n");
252 } else {
253 if ((image = fopen (filename, "r")) == NULL) {
254 perror(filename);
255 exit(1);
256 }
257 fread (buf, sizeof(char), size, image);
258 fclose(image);
259 }
260
261 if (write_it || (!read_it && !verify_it))
262 flash->write (flash, buf);
263 if (verify_it)
Ollie Lho789ad3d2004-03-18 20:27:33 +0000264 verify_flash (flash, buf, verbose);
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000265 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000266}