blob: b476d355726a5963c28f82ae4236695d6724f778 [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"
David Hendricks3770a112004-03-17 21:47:30 +000049#include "sst49lf040.h"
Ronald G. Minniche3dad012004-02-10 21:34:18 +000050#include "pm49fl004.h"
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000051#include "mx29f002.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000052
53struct flashchip flashchips[] = {
Ronald G. Minniche3dad012004-02-10 21:34:18 +000054#if 1
55 {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000056 probe_29f040b, erase_29f040b, write_29f040b, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000057 {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000058 probe_jedec, erase_jedec, write_jedec, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000059 {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000060 probe_29f002, erase_29f002, write_29f002, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000061 {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000062 probe_jedec, erase_jedec, write_jedec, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000063 {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000064 probe_28sf040, erase_28sf040, write_28sf040, NULL},
Ronald G. Minnichc8316472002-03-21 22:40:40 +000065 {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000066 probe_39sf020, erase_39sf020, write_39sf020, NULL},
Ollie Lho6041bcd2002-07-18 03:32:00 +000067 {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000068 probe_39sf020, erase_39sf020, write_39sf020, NULL},
David Hendricks3770a112004-03-17 21:47:30 +000069 {"SST49LF040", SST_ID, SST_49LF040, NULL, 512, 4096,
70 probe_49lf040, erase_49lf040, write_49lf040, NULL},
71
Ronald G. Minniche3dad012004-02-10 21:34:18 +000072#endif
73//By LYH begin
74 {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64*1024,
75 probe_49fl004, erase_49fl004, write_49fl004, NULL},
76//END
77#if 1
Andrew Ip973b26d2002-12-30 13:10:06 +000078 {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000079 probe_jedec, erase_jedec, write_jedec, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000080 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000081 probe_jedec, erase_jedec, write_jedec, NULL},
Andrew Ipf0126ce2002-10-16 06:58:05 +000082 {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000083 probe_49f002, erase_49f002, write_49f002, NULL},
Ronald G. Minnich3c910ed2002-05-28 23:29:17 +000084 {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000085 probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL},
Ronald G. Minnich56439422002-09-06 16:58:14 +000086 {"82802ab", 137, 173 , NULL, 512, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000087 probe_82802ab, erase_82802ab, write_82802ab, NULL},
Ronald G. Minnichbb0322f2003-01-13 23:43:36 +000088 {"82802ac", 137, 172 , NULL, 1024, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000089 probe_82802ab, erase_82802ab, write_82802ab, NULL},
90 {"MD-2802 (M-Systems DiskOnChip Millennium Module)",
91 MSYSTEMS_ID, MSYSTEMS_MD2802,
92 NULL, 8, 8*1024,
93 probe_md2802, erase_md2802, write_md2802, read_md2802},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000094 {NULL,}
Ronald G. Minniche3dad012004-02-10 21:34:18 +000095#endif
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. Minniche3dad012004-02-10 21:34:18 +000099#if 1
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000100int enable_flash_sis630 (struct pci_dev *dev, char *name)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000101{
102 char b;
103
104 /* get io privilege access PCI configuration space */
105 if (iopl(3) != 0) {
106 perror("Can not set io priviliage");
107 exit(1);
108 }
109
110 /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
111 outl(0x80000840, 0x0cf8);
112 b = inb(0x0cfc) | 0x0b;
113 outb(b, 0xcfc);
114 /* Flash write enable on SiS 540/630 */
115 outl(0x80000845, 0x0cf8);
116 b = inb(0x0cfd) | 0x40;
117 outb(b, 0xcfd);
118
119 /* The same thing on SiS 950 SuperIO side */
120 outb(0x87, 0x2e);
121 outb(0x01, 0x2e);
122 outb(0x55, 0x2e);
123 outb(0x55, 0x2e);
124
125 if (inb(0x2f) != 0x87) {
126 outb(0x87, 0x4e);
127 outb(0x01, 0x4e);
128 outb(0x55, 0x4e);
129 outb(0xaa, 0x4e);
130 if (inb(0x4f) != 0x87) {
131 printf("Can not access SiS 950\n");
132 return -1;
133 }
134 outb(0x24, 0x4e);
135 b = inb(0x4f) | 0xfc;
136 outb(0x24, 0x4e);
137 outb(b, 0x4f);
138 outb(0x02, 0x4e);
139 outb(0x02, 0x4f);
140 }
141
142 outb(0x24, 0x2e);
143 printf("2f is %#x\n", inb(0x2f));
144 b = inb(0x2f) | 0xfc;
145 outb(0x24, 0x2e);
146 outb(b, 0x2f);
147
148 outb(0x02, 0x2e);
149 outb(0x02, 0x2f);
150
151 return 0;
152}
153
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000154int
155enable_flash_e7500(struct pci_dev *dev, char *name) {
156 /* register 4e.b gets or'ed with one */
157 unsigned char old, new;
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000158 /* if it fails, it fails. There are so many variations of broken mobos
159 * that it is hard to argue that we should quit at this point.
160 */
161
162 old = pci_read_byte(dev, 0x4e);
163
164 new = old | 1;
165
Ronald G. Minniche5559572003-03-28 03:29:43 +0000166 if (new == old)
167 return 0;
168
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000169 pci_write_byte(dev, 0x4e, new);
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000170
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000171 if (pci_read_byte(dev, 0x4e) != new) {
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000172 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000173 0x4e, new, name);
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000174 return -1;
175 }
176 return 0;
177}
178
Ronald G. Minniche5559572003-03-28 03:29:43 +0000179int
Andrew Ip772f6452003-07-21 14:54:16 +0000180enable_flash_vt8235(struct pci_dev *dev, char *name) {
181 unsigned char old, new, val;
182 unsigned int base;
183 int ok;
184
185 /* get io privilege access PCI configuration space */
186 if (iopl(3) != 0) {
187 perror("Can not set io priviliage");
188 exit(1);
189 }
190
191 old = pci_read_byte(dev, 0x40);
192
193 new = old | 0x10;
194
195 if (new == old)
196 return 0;
197
198 ok = pci_write_byte(dev, 0x40, new);
199 if (ok != 0) {
200 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
201 old, new, name);
202 }
203
204 /* enable GPIO15 which is connected to write protect. */
205 base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8);
206 val = inb(base + 0x4d);
207 val |= 0x80;
208 outb(val, base + 0x4d);
209
210 if (ok != 0) {
211 return -1;
212 } else {
213 return 0;
214 }
215}
216
217int
Ronald G. Minniche5559572003-03-28 03:29:43 +0000218enable_flash_vt8231(struct pci_dev *dev, char *name) {
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000219 unsigned char val;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000220
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000221 val = pci_read_byte(dev, 0x40);
222 val |= 0x10;
223 pci_write_byte(dev, 0x40, val);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000224
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000225 if (pci_read_byte(dev, 0x40) != val) {
Ronald G. Minniche5559572003-03-28 03:29:43 +0000226 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000227 0x40, val, name);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000228 return -1;
229 }
230 return 0;
231}
232
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000233int
234enable_flash_cs5530(struct pci_dev *dev, char *name) {
235 unsigned char new;
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000236
237 pci_write_byte(dev, 0x52, 0xee);
238
239 new = pci_read_byte(dev, 0x52);
240
241 if (new != 0xee) {
242 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
243 0x52, new, name);
244 return -1;
245 }
246 return 0;
247}
248
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000249int
Andrew Ipafe44a92003-07-22 10:12:08 +0000250enable_flash_sc1100(struct pci_dev *dev, char *name) {
251 unsigned char new;
Andrew Ipafe44a92003-07-22 10:12:08 +0000252
253 pci_write_byte(dev, 0x52, 0xee);
254
255 new = pci_read_byte(dev, 0x52);
256
257 if (new != 0xee) {
258 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
259 0x52, new, name);
260 return -1;
261 }
262 return 0;
263}
264
265int
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000266enable_flash_sis5595(struct pci_dev *dev, char *name) {
267 unsigned char new, newer;
268
269 new = pci_read_byte(dev, 0x45);
270
271 /* clear bit 5 */
272 new &= (~ 0x20);
273 /* set bit 2 */
274 new |= 0x4;
275
276 pci_write_byte(dev, 0x45, new);
277
278 newer = pci_read_byte(dev, 0x45);
279 if (newer != new) {
280 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
281 0x45, new, name);
282 printf("Stuck at 0x%x\n", newer);
283 return -1;
284 }
285 return 0;
286}
Ronald G. Minniche3dad012004-02-10 21:34:18 +0000287#endif
288//BY LYH
289#if 0
290static void dump_pci_device(struct pci_dev *dev)
291{
292 int i;
293
294 printf("\n");
295 for(i = 0; i <= 255; i++) {
296 unsigned char val;
297 if ((i & 0x0f) == 0) {
298 printf("0x%02x:",i);
299 }
300 val = pci_read_byte(dev, i);
301 printf(" 0x%02x",val);
302 if ((i & 0x0f) == 0x0f) {
303 printf("\r\n");
304 }
305 }
306}
307
308#endif
309
310int
311enable_flash_amd8111(struct pci_dev *dev, char *name) {
312 /* register 4e.b gets or'ed with one */
313 unsigned char old, new;
314 /* if it fails, it fails. There are so many variations of broken mobos
315 * that it is hard to argue that we should quit at this point.
316 */
317
318// dump_pci_device(dev);
319
320 old = pci_read_byte(dev, 0x43);
321
322 new = old | 0x80;
323
324 if (new != old) {
325
326 pci_write_byte(dev, 0x43, new);
327
328 if (pci_read_byte(dev, 0x43) != new) {
329 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
330 0x43, new, name);
331 }
332 }
333
334
335 old = pci_read_byte(dev, 0x40);
336
337 new = old | 0x01;
338
339 if (new == old)
340 return 0;
341
342 pci_write_byte(dev, 0x40, new);
343
344 if (pci_read_byte(dev, 0x40) != new) {
345 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
346 0x40, new, name);
347 return -1;
348 }
349 return 0;
350}
351//By LYH END
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000352
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000353struct flashchip * probe_flash(struct flashchip * flash)
354{
355 int fd_mem;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000356 volatile char * bios;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000357 unsigned long size;
358
359 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
360 perror("Can not open /dev/mem");
361 exit(1);
362 }
363
364 while (flash->name != NULL) {
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000365 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
366 flash++;
367 continue;
368 }
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000369 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000370 size = flash->total_size * 1024;
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000371/* BUG? what happens if getpagesize() > size!?
372 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
373 if(getpagesize() > size)
374 {
375 size = getpagesize();
376 printf("%s: warning: size: %d -> %ld\n", __FUNCTION__,
377 flash->total_size * 1024, (unsigned long)size);
378 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000379 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
David Hendricks3770a112004-03-17 21:47:30 +0000380 //fd_mem, (off_t) (0x100000000-size));
381 fd_mem, (off_t) (0x0-size));
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000382 if (bios == MAP_FAILED) {
383 perror("Error MMAP /dev/mem");
384 exit(1);
385 }
386 flash->virt_addr = bios;
Ronald G. Minnich56439422002-09-06 16:58:14 +0000387 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000388
389 if (flash->probe(flash) == 1) {
390 printf ("%s found at physical address: 0x%lx\n",
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000391 flash->name, (0 - size));
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000392 return flash;
393 }
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000394 munmap ((void *) bios, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000395 flash++;
396 }
397 return NULL;
398}
399
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000400int verify_flash (struct flashchip * flash, char * buf, int verbose)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000401{
402 int i = 0;
403 int total_size = flash->total_size *1024;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000404 volatile char * bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000405
406 printf("Verifying address: ");
407 while (i++ < total_size) {
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000408 if (verbose)
409 printf("0x%08x", i);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000410 if (*(bios+i) != *(buf+i)) {
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000411 printf("FAILED\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000412 return 0;
413 }
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000414 if (verbose)
415 printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000416 }
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000417 if (verbose)
418 printf("\n");
419 else
420 printf("VERIFIED\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000421 return 1;
422}
423
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000424// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
425
Ronald G. Minniche5559572003-03-28 03:29:43 +0000426unsigned long micro = 1;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000427
428void
429myusec_calibrate_delay()
430{
Ronald G. Minniche5559572003-03-28 03:29:43 +0000431 int count = 1000;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000432 unsigned long timeusec;
433 struct timeval start, end;
434 int ok = 0;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000435 void myusec_delay(int time);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000436
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000437 printf("Setting up microsecond timing loop\n");
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000438 while (! ok) {
Ronald G. Minnich4572a822003-02-11 16:09:12 +0000439 //fprintf(stderr, "Try %d\n", count);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000440 gettimeofday(&start, 0);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000441 myusec_delay(count);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000442 gettimeofday(&end, 0);
443 timeusec = 1000000 * (end.tv_sec - start.tv_sec ) +
444 (end.tv_usec - start.tv_usec);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000445 //fprintf(stderr, "timeusec is %d\n", timeusec);
446 count *= 2;
447 if (timeusec < 1000000/4)
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000448 continue;
449 ok = 1;
450 }
451
452 // compute one microsecond. That will be count / time
453 micro = count / timeusec;
454
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000455 fprintf(stderr, "%ldM loops per second\n", (unsigned long)micro);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000456
457
458}
459
460void
Ronald G. Minniche5559572003-03-28 03:29:43 +0000461myusec_delay(int time)
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000462{
463 volatile unsigned long i;
464 for(i = 0; i < time * micro; i++)
465 ;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000466}
467
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000468typedef struct penable {
469 unsigned short vendor, device;
470 char *name;
471 int (*doit)(struct pci_dev *dev, char *name);
472} FLASH_ENABLE;
473
474FLASH_ENABLE enables[] = {
Ronald G. Minniche3dad012004-02-10 21:34:18 +0000475#if 1
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000476 {0x1, 0x1, "sis630 -- what's the ID?", enable_flash_sis630},
477 {0x8086, 0x2480, "E7500", enable_flash_e7500},
Ronald G. Minniche5559572003-03-28 03:29:43 +0000478 {0x1106, 0x8231, "VT8231", enable_flash_vt8231},
Andrew Ip772f6452003-07-21 14:54:16 +0000479 {0x1106, 0x3177, "VT8235", enable_flash_vt8235},
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000480 {0x1078, 0x0100, "CS5530", enable_flash_cs5530},
Andrew Ipafe44a92003-07-22 10:12:08 +0000481 {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000482 {0x1039, 0x8, "SIS5595", enable_flash_sis5595},
Ronald G. Minniche3dad012004-02-10 21:34:18 +0000483#endif
484 {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000485};
486
487int
488enable_flash_write() {
489 int i;
490 struct pci_access *pacc;
491 struct pci_dev *dev = 0;
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000492 FLASH_ENABLE *enable = 0;
493
494 pacc = pci_alloc(); /* Get the pci_access structure */
495 /* Set all options you want -- here we stick with the defaults */
496 pci_init(pacc); /* Initialize the PCI library */
497 pci_scan_bus(pacc); /* We want to get the list of devices */
498
499 /* now let's try to find the chipset we have ... */
500 for(i = 0; i < sizeof(enables)/sizeof(enables[0]) && (! dev); i++) {
501 struct pci_filter f;
502 struct pci_dev *z;
503 /* the first param is unused. */
504 pci_filter_init((struct pci_access *) 0, &f);
505 f.vendor = enables[i].vendor;
506 f.device = enables[i].device;
507 for(z=pacc->devices; z; z=z->next)
508 if (pci_filter_match(&f, z)) {
509 enable = &enables[i];
510 dev = z;
511 }
512 }
513
514 /* now do the deed. */
Ronald G. Minniche5559572003-03-28 03:29:43 +0000515 if (enable) {
516 printf("Enabling flash write on %s...", enable->name);
517 if (enable->doit(dev, enable->name) == 0)
518 printf("OK\n");
519 }
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000520 return 0;
521}
522
Ronald G. Minniche5559572003-03-28 03:29:43 +0000523void usage(const char *name)
524{
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000525 printf("usage: %s [-rwv] [-c chipname][file]\n", name);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000526 printf("-r: read flash and save into file\n"
527 "-w: write file into flash (default when file is specified)\n"
528 "-v: verify flash against file\n"
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000529 "-c: probe only for specified flash chip\n"
Ronald G. Minniche5559572003-03-28 03:29:43 +0000530 " If no file is specified, then all that happens\n"
531 " is that flash info is dumped\n");
532 exit(1);
533}
534
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000535int
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000536main (int argc, char * argv[])
537{
538 char * buf;
539 unsigned long size;
540 FILE * image;
541 struct flashchip * flash;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000542 int opt;
543 int read_it = 0, write_it = 0, verify_it = 0;
544 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000545
546 setbuf(stdout, NULL);
547
548 while ((opt = getopt(argc, argv, "rwvc:")) != EOF) {
Ronald G. Minniche5559572003-03-28 03:29:43 +0000549 switch (opt) {
550 case 'r':
551 read_it = 1;
552 break;
553 case 'w':
554 write_it = 1;
555 break;
556 case 'v':
557 verify_it = 1;
558 break;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000559 case 'c':
560 chip_to_probe = strdup(optarg);
561 break;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000562 default:
563 usage(argv[0]);
564 break;
565 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000566 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000567 if (read_it && write_it) {
568 printf("-r and -w are mutually exclusive\n");
569 usage(argv[0]);
570 }
571
572 if (optind < argc)
573 filename = argv[optind++];
574
575 printf("Calibrating timer since microsleep sucks ... takes a second\n");
576 myusec_calibrate_delay();
577 printf("OK, calibrated, now do the deed\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000578
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000579 /* try to enable it. Failure IS an option, since not all motherboards
580 * really need this to be done, etc., etc. It sucks.
581 */
582 (void) enable_flash_write();
583
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000584 if ((flash = probe_flash (flashchips)) == NULL) {
585 printf("EEPROM not found\n");
586 exit(1);
587 }
588
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000589 printf("Part is %s\n", flash->name);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000590 if (!filename){
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000591 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000592 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000593 }
594 size = flash->total_size * 1024;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000595 buf = (char *) calloc (size, sizeof(char));
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000596
Ronald G. Minniche5559572003-03-28 03:29:43 +0000597 if (read_it) {
598 if ((image = fopen (filename, "w")) == NULL) {
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000599 perror(filename);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000600 exit(1);
601 }
602 printf("Reading Flash...");
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000603 if(flash->read == NULL)
604 memcpy(buf, (const char *) flash->virt_addr, size);
605 else
606 flash->read (flash, buf);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000607 fwrite(buf, sizeof(char), size, image);
608 fclose(image);
609 printf("done\n");
610 } else {
611 if ((image = fopen (filename, "r")) == NULL) {
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000612 perror(filename);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000613 exit(1);
614 }
615 fread (buf, sizeof(char), size, image);
616 fclose(image);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000617 }
618
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000619 if (write_it || (!read_it && !verify_it))
Ronald G. Minniche5559572003-03-28 03:29:43 +0000620 flash->write (flash, buf);
621 if (verify_it)
622 verify_flash (flash, buf, /* verbose = */ 0);
Ronald G. Minnich4572a822003-02-11 16:09:12 +0000623 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000624}