blob: a13d48bb5101cee88895f833c7931e690be78f1e [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>
32#include <sys/io.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000033#include <sys/time.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000034#include <unistd.h>
35#include <stdio.h>
Ronald G. Minnichc73ad982003-02-11 21:06:09 +000036#include <pci/pci.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000037#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000038#include <stdlib.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000039
40#include "flash.h"
41#include "jedec.h"
Ronald G. Minnich3c910ed2002-05-28 23:29:17 +000042#include "m29f400bt.h"
Ronald G. Minnich56439422002-09-06 16:58:14 +000043#include "82802ab.h"
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000044#include "msys_doc.h"
45#include "am29f040b.h"
46#include "sst28sf040.h"
47#include "w49f002u.h"
48#include "sst39sf020.h"
49#include "mx29f002.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000050
51struct flashchip flashchips[] = {
52 {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000053 probe_29f040b, erase_29f040b, write_29f040b, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000054 {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000055 probe_jedec, erase_jedec, write_jedec, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000056 {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000057 probe_29f002, erase_29f002, write_29f002, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000058 {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000059 probe_jedec, erase_jedec, write_jedec, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000060 {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000061 probe_28sf040, erase_28sf040, write_28sf040, NULL},
Ronald G. Minnichc8316472002-03-21 22:40:40 +000062 {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000063 probe_39sf020, erase_39sf020, write_39sf020, NULL},
Ollie Lho6041bcd2002-07-18 03:32:00 +000064 {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000065 probe_39sf020, erase_39sf020, write_39sf020, NULL},
Andrew Ip973b26d2002-12-30 13:10:06 +000066 {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000067 probe_jedec, erase_jedec, write_jedec, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000068 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000069 probe_jedec, erase_jedec, write_jedec, NULL},
Andrew Ipf0126ce2002-10-16 06:58:05 +000070 {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000071 probe_49f002, erase_49f002, write_49f002, NULL},
Ronald G. Minnich3c910ed2002-05-28 23:29:17 +000072 {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000073 probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL},
Ronald G. Minnich56439422002-09-06 16:58:14 +000074 {"82802ab", 137, 173 , NULL, 512, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000075 probe_82802ab, erase_82802ab, write_82802ab, NULL},
Ronald G. Minnichbb0322f2003-01-13 23:43:36 +000076 {"82802ac", 137, 172 , NULL, 1024, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000077 probe_82802ab, erase_82802ab, write_82802ab, NULL},
78 {"MD-2802 (M-Systems DiskOnChip Millennium Module)",
79 MSYSTEMS_ID, MSYSTEMS_MD2802,
80 NULL, 8, 8*1024,
81 probe_md2802, erase_md2802, write_md2802, read_md2802},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000082 {NULL,}
83};
84
Ronald G. Minnichceec4202003-07-25 04:37:41 +000085char *chip_to_probe = NULL;
86
Ronald G. Minnichc73ad982003-02-11 21:06:09 +000087int enable_flash_sis630 (struct pci_dev *dev, char *name)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000088{
89 char b;
90
91 /* get io privilege access PCI configuration space */
92 if (iopl(3) != 0) {
93 perror("Can not set io priviliage");
94 exit(1);
95 }
96
97 /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
98 outl(0x80000840, 0x0cf8);
99 b = inb(0x0cfc) | 0x0b;
100 outb(b, 0xcfc);
101 /* Flash write enable on SiS 540/630 */
102 outl(0x80000845, 0x0cf8);
103 b = inb(0x0cfd) | 0x40;
104 outb(b, 0xcfd);
105
106 /* The same thing on SiS 950 SuperIO side */
107 outb(0x87, 0x2e);
108 outb(0x01, 0x2e);
109 outb(0x55, 0x2e);
110 outb(0x55, 0x2e);
111
112 if (inb(0x2f) != 0x87) {
113 outb(0x87, 0x4e);
114 outb(0x01, 0x4e);
115 outb(0x55, 0x4e);
116 outb(0xaa, 0x4e);
117 if (inb(0x4f) != 0x87) {
118 printf("Can not access SiS 950\n");
119 return -1;
120 }
121 outb(0x24, 0x4e);
122 b = inb(0x4f) | 0xfc;
123 outb(0x24, 0x4e);
124 outb(b, 0x4f);
125 outb(0x02, 0x4e);
126 outb(0x02, 0x4f);
127 }
128
129 outb(0x24, 0x2e);
130 printf("2f is %#x\n", inb(0x2f));
131 b = inb(0x2f) | 0xfc;
132 outb(0x24, 0x2e);
133 outb(b, 0x2f);
134
135 outb(0x02, 0x2e);
136 outb(0x02, 0x2f);
137
138 return 0;
139}
140
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000141int
142enable_flash_e7500(struct pci_dev *dev, char *name) {
143 /* register 4e.b gets or'ed with one */
144 unsigned char old, new;
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000145 /* if it fails, it fails. There are so many variations of broken mobos
146 * that it is hard to argue that we should quit at this point.
147 */
148
149 old = pci_read_byte(dev, 0x4e);
150
151 new = old | 1;
152
Ronald G. Minniche5559572003-03-28 03:29:43 +0000153 if (new == old)
154 return 0;
155
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000156 pci_write_byte(dev, 0x4e, new);
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000157
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000158 if (pci_read_byte(dev, 0x4e) != new) {
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000159 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000160 0x4e, new, name);
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000161 return -1;
162 }
163 return 0;
164}
165
Ronald G. Minniche5559572003-03-28 03:29:43 +0000166int
Andrew Ip772f6452003-07-21 14:54:16 +0000167enable_flash_vt8235(struct pci_dev *dev, char *name) {
168 unsigned char old, new, val;
169 unsigned int base;
170 int ok;
171
172 /* get io privilege access PCI configuration space */
173 if (iopl(3) != 0) {
174 perror("Can not set io priviliage");
175 exit(1);
176 }
177
178 old = pci_read_byte(dev, 0x40);
179
180 new = old | 0x10;
181
182 if (new == old)
183 return 0;
184
185 ok = pci_write_byte(dev, 0x40, new);
186 if (ok != 0) {
187 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
188 old, new, name);
189 }
190
191 /* enable GPIO15 which is connected to write protect. */
192 base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8);
193 val = inb(base + 0x4d);
194 val |= 0x80;
195 outb(val, base + 0x4d);
196
197 if (ok != 0) {
198 return -1;
199 } else {
200 return 0;
201 }
202}
203
204int
Ronald G. Minniche5559572003-03-28 03:29:43 +0000205enable_flash_vt8231(struct pci_dev *dev, char *name) {
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000206 unsigned char val;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000207
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000208 val = pci_read_byte(dev, 0x40);
209 val |= 0x10;
210 pci_write_byte(dev, 0x40, val);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000211
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000212 if (pci_read_byte(dev, 0x40) != val) {
Ronald G. Minniche5559572003-03-28 03:29:43 +0000213 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000214 0x40, val, name);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000215 return -1;
216 }
217 return 0;
218}
219
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000220int
221enable_flash_cs5530(struct pci_dev *dev, char *name) {
222 unsigned char new;
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000223
224 pci_write_byte(dev, 0x52, 0xee);
225
226 new = pci_read_byte(dev, 0x52);
227
228 if (new != 0xee) {
229 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
230 0x52, new, name);
231 return -1;
232 }
233 return 0;
234}
235
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000236int
Andrew Ipafe44a92003-07-22 10:12:08 +0000237enable_flash_sc1100(struct pci_dev *dev, char *name) {
238 unsigned char new;
Andrew Ipafe44a92003-07-22 10:12:08 +0000239
240 pci_write_byte(dev, 0x52, 0xee);
241
242 new = pci_read_byte(dev, 0x52);
243
244 if (new != 0xee) {
245 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
246 0x52, new, name);
247 return -1;
248 }
249 return 0;
250}
251
252int
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000253enable_flash_sis5595(struct pci_dev *dev, char *name) {
254 unsigned char new, newer;
255
256 new = pci_read_byte(dev, 0x45);
257
258 /* clear bit 5 */
259 new &= (~ 0x20);
260 /* set bit 2 */
261 new |= 0x4;
262
263 pci_write_byte(dev, 0x45, new);
264
265 newer = pci_read_byte(dev, 0x45);
266 if (newer != new) {
267 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
268 0x45, new, name);
269 printf("Stuck at 0x%x\n", newer);
270 return -1;
271 }
272 return 0;
273}
274
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000275struct flashchip * probe_flash(struct flashchip * flash)
276{
277 int fd_mem;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000278 volatile char * bios;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000279 unsigned long size;
280
281 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
282 perror("Can not open /dev/mem");
283 exit(1);
284 }
285
286 while (flash->name != NULL) {
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000287 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
288 flash++;
289 continue;
290 }
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000291 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000292 size = flash->total_size * 1024;
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000293/* BUG? what happens if getpagesize() > size!?
294 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
295 if(getpagesize() > size)
296 {
297 size = getpagesize();
298 printf("%s: warning: size: %d -> %ld\n", __FUNCTION__,
299 flash->total_size * 1024, (unsigned long)size);
300 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000301 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
302 fd_mem, (off_t) (0 - size));
303 if (bios == MAP_FAILED) {
304 perror("Error MMAP /dev/mem");
305 exit(1);
306 }
307 flash->virt_addr = bios;
Ronald G. Minnich56439422002-09-06 16:58:14 +0000308 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000309
310 if (flash->probe(flash) == 1) {
311 printf ("%s found at physical address: 0x%lx\n",
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000312 flash->name, (0 - size));
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000313 return flash;
314 }
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000315 munmap ((void *) bios, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000316 flash++;
317 }
318 return NULL;
319}
320
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000321int verify_flash (struct flashchip * flash, char * buf, int verbose)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000322{
323 int i = 0;
324 int total_size = flash->total_size *1024;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000325 volatile char * bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000326
327 printf("Verifying address: ");
328 while (i++ < total_size) {
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000329 if (verbose)
330 printf("0x%08x", i);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000331 if (*(bios+i) != *(buf+i)) {
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000332 printf("FAILED\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000333 return 0;
334 }
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000335 if (verbose)
336 printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000337 }
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000338 if (verbose)
339 printf("\n");
340 else
341 printf("VERIFIED\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000342 return 1;
343}
344
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000345// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
346
Ronald G. Minniche5559572003-03-28 03:29:43 +0000347unsigned long micro = 1;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000348
349void
350myusec_calibrate_delay()
351{
Ronald G. Minniche5559572003-03-28 03:29:43 +0000352 int count = 1000;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000353 unsigned long timeusec;
354 struct timeval start, end;
355 int ok = 0;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000356 void myusec_delay(int time);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000357
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000358 printf("Setting up microsecond timing loop\n");
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000359 while (! ok) {
Ronald G. Minnich4572a822003-02-11 16:09:12 +0000360 //fprintf(stderr, "Try %d\n", count);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000361 gettimeofday(&start, 0);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000362 myusec_delay(count);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000363 gettimeofday(&end, 0);
364 timeusec = 1000000 * (end.tv_sec - start.tv_sec ) +
365 (end.tv_usec - start.tv_usec);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000366 //fprintf(stderr, "timeusec is %d\n", timeusec);
367 count *= 2;
368 if (timeusec < 1000000/4)
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000369 continue;
370 ok = 1;
371 }
372
373 // compute one microsecond. That will be count / time
374 micro = count / timeusec;
375
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000376 fprintf(stderr, "%ldM loops per second\n", (unsigned long)micro);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000377
378
379}
380
381void
Ronald G. Minniche5559572003-03-28 03:29:43 +0000382myusec_delay(int time)
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000383{
384 volatile unsigned long i;
385 for(i = 0; i < time * micro; i++)
386 ;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000387}
388
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000389typedef struct penable {
390 unsigned short vendor, device;
391 char *name;
392 int (*doit)(struct pci_dev *dev, char *name);
393} FLASH_ENABLE;
394
395FLASH_ENABLE enables[] = {
396
397 {0x1, 0x1, "sis630 -- what's the ID?", enable_flash_sis630},
398 {0x8086, 0x2480, "E7500", enable_flash_e7500},
Ronald G. Minniche5559572003-03-28 03:29:43 +0000399 {0x1106, 0x8231, "VT8231", enable_flash_vt8231},
Andrew Ip772f6452003-07-21 14:54:16 +0000400 {0x1106, 0x3177, "VT8235", enable_flash_vt8235},
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000401 {0x1078, 0x0100, "CS5530", enable_flash_cs5530},
Andrew Ipafe44a92003-07-22 10:12:08 +0000402 {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000403 {0x1039, 0x8, "SIS5595", enable_flash_sis5595},
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000404};
405
406int
407enable_flash_write() {
408 int i;
409 struct pci_access *pacc;
410 struct pci_dev *dev = 0;
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000411 FLASH_ENABLE *enable = 0;
412
413 pacc = pci_alloc(); /* Get the pci_access structure */
414 /* Set all options you want -- here we stick with the defaults */
415 pci_init(pacc); /* Initialize the PCI library */
416 pci_scan_bus(pacc); /* We want to get the list of devices */
417
418 /* now let's try to find the chipset we have ... */
419 for(i = 0; i < sizeof(enables)/sizeof(enables[0]) && (! dev); i++) {
420 struct pci_filter f;
421 struct pci_dev *z;
422 /* the first param is unused. */
423 pci_filter_init((struct pci_access *) 0, &f);
424 f.vendor = enables[i].vendor;
425 f.device = enables[i].device;
426 for(z=pacc->devices; z; z=z->next)
427 if (pci_filter_match(&f, z)) {
428 enable = &enables[i];
429 dev = z;
430 }
431 }
432
433 /* now do the deed. */
Ronald G. Minniche5559572003-03-28 03:29:43 +0000434 if (enable) {
435 printf("Enabling flash write on %s...", enable->name);
436 if (enable->doit(dev, enable->name) == 0)
437 printf("OK\n");
438 }
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000439 return 0;
440}
441
Ronald G. Minniche5559572003-03-28 03:29:43 +0000442void usage(const char *name)
443{
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000444 printf("usage: %s [-rwv] [-c chipname][file]\n", name);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000445 printf("-r: read flash and save into file\n"
446 "-w: write file into flash (default when file is specified)\n"
447 "-v: verify flash against file\n"
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000448 "-c: probe only for specified flash chip\n"
Ronald G. Minniche5559572003-03-28 03:29:43 +0000449 " If no file is specified, then all that happens\n"
450 " is that flash info is dumped\n");
451 exit(1);
452}
453
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000454int
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000455main (int argc, char * argv[])
456{
457 char * buf;
458 unsigned long size;
459 FILE * image;
460 struct flashchip * flash;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000461 int opt;
462 int read_it = 0, write_it = 0, verify_it = 0;
463 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000464
465 setbuf(stdout, NULL);
466
467 while ((opt = getopt(argc, argv, "rwvc:")) != EOF) {
Ronald G. Minniche5559572003-03-28 03:29:43 +0000468 switch (opt) {
469 case 'r':
470 read_it = 1;
471 break;
472 case 'w':
473 write_it = 1;
474 break;
475 case 'v':
476 verify_it = 1;
477 break;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000478 case 'c':
479 chip_to_probe = strdup(optarg);
480 break;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000481 default:
482 usage(argv[0]);
483 break;
484 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000485 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000486 if (read_it && write_it) {
487 printf("-r and -w are mutually exclusive\n");
488 usage(argv[0]);
489 }
490
491 if (optind < argc)
492 filename = argv[optind++];
493
494 printf("Calibrating timer since microsleep sucks ... takes a second\n");
495 myusec_calibrate_delay();
496 printf("OK, calibrated, now do the deed\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000497
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000498 /* try to enable it. Failure IS an option, since not all motherboards
499 * really need this to be done, etc., etc. It sucks.
500 */
501 (void) enable_flash_write();
502
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000503 if ((flash = probe_flash (flashchips)) == NULL) {
504 printf("EEPROM not found\n");
505 exit(1);
506 }
507
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000508 printf("Part is %s\n", flash->name);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000509 if (!filename){
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000510 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000511 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000512 }
513 size = flash->total_size * 1024;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000514 buf = (char *) calloc (size, sizeof(char));
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000515
Ronald G. Minniche5559572003-03-28 03:29:43 +0000516 if (read_it) {
517 if ((image = fopen (filename, "w")) == NULL) {
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000518 perror(filename);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000519 exit(1);
520 }
521 printf("Reading Flash...");
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000522 if(flash->read == NULL)
523 memcpy(buf, (const char *) flash->virt_addr, size);
524 else
525 flash->read (flash, buf);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000526 fwrite(buf, sizeof(char), size, image);
527 fclose(image);
528 printf("done\n");
529 } else {
530 if ((image = fopen (filename, "r")) == NULL) {
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000531 perror(filename);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000532 exit(1);
533 }
534 fread (buf, sizeof(char), size, image);
535 fclose(image);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000536 }
537
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000538 if (write_it || (!read_it && !verify_it))
Ronald G. Minniche5559572003-03-28 03:29:43 +0000539 flash->write (flash, buf);
540 if (verify_it)
541 verify_flash (flash, buf, /* verbose = */ 0);
Ronald G. Minnich4572a822003-02-11 16:09:12 +0000542 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000543}