blob: b063b3c1f4ce89737a6b60109fb291c14d8003ef [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"
Ronald G. Minniche3dad012004-02-10 21:34:18 +000049#include "pm49fl004.h"
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000050#include "mx29f002.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000051
52struct flashchip flashchips[] = {
Ronald G. Minniche3dad012004-02-10 21:34:18 +000053#if 1
54 {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000055 probe_29f040b, erase_29f040b, write_29f040b, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000056 {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000057 probe_jedec, erase_jedec, write_jedec, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000058 {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000059 probe_29f002, erase_29f002, write_29f002, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000060 {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000061 probe_jedec, erase_jedec, write_jedec, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000062 {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000063 probe_28sf040, erase_28sf040, write_28sf040, NULL},
Ronald G. Minnichc8316472002-03-21 22:40:40 +000064 {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000065 probe_39sf020, erase_39sf020, write_39sf020, NULL},
Ollie Lho6041bcd2002-07-18 03:32:00 +000066 {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000067 probe_39sf020, erase_39sf020, write_39sf020, NULL},
Ronald G. Minniche3dad012004-02-10 21:34:18 +000068#endif
69//By LYH begin
70 {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64*1024,
71 probe_49fl004, erase_49fl004, write_49fl004, NULL},
72//END
73#if 1
Andrew Ip973b26d2002-12-30 13:10:06 +000074 {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000075 probe_jedec, erase_jedec, write_jedec, NULL},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000076 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000077 probe_jedec, erase_jedec, write_jedec, NULL},
Andrew Ipf0126ce2002-10-16 06:58:05 +000078 {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000079 probe_49f002, erase_49f002, write_49f002, NULL},
Ronald G. Minnich3c910ed2002-05-28 23:29:17 +000080 {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000081 probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL},
Ronald G. Minnich56439422002-09-06 16:58:14 +000082 {"82802ab", 137, 173 , NULL, 512, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000083 probe_82802ab, erase_82802ab, write_82802ab, NULL},
Ronald G. Minnichbb0322f2003-01-13 23:43:36 +000084 {"82802ac", 137, 172 , NULL, 1024, 64*1024,
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000085 probe_82802ab, erase_82802ab, write_82802ab, NULL},
86 {"MD-2802 (M-Systems DiskOnChip Millennium Module)",
87 MSYSTEMS_ID, MSYSTEMS_MD2802,
88 NULL, 8, 8*1024,
89 probe_md2802, erase_md2802, write_md2802, read_md2802},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000090 {NULL,}
Ronald G. Minniche3dad012004-02-10 21:34:18 +000091#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000092};
93
Ronald G. Minnichceec4202003-07-25 04:37:41 +000094char *chip_to_probe = NULL;
Ronald G. Minniche3dad012004-02-10 21:34:18 +000095#if 1
Ronald G. Minnichc73ad982003-02-11 21:06:09 +000096int enable_flash_sis630 (struct pci_dev *dev, char *name)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000097{
98 char b;
99
100 /* get io privilege access PCI configuration space */
101 if (iopl(3) != 0) {
102 perror("Can not set io priviliage");
103 exit(1);
104 }
105
106 /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
107 outl(0x80000840, 0x0cf8);
108 b = inb(0x0cfc) | 0x0b;
109 outb(b, 0xcfc);
110 /* Flash write enable on SiS 540/630 */
111 outl(0x80000845, 0x0cf8);
112 b = inb(0x0cfd) | 0x40;
113 outb(b, 0xcfd);
114
115 /* The same thing on SiS 950 SuperIO side */
116 outb(0x87, 0x2e);
117 outb(0x01, 0x2e);
118 outb(0x55, 0x2e);
119 outb(0x55, 0x2e);
120
121 if (inb(0x2f) != 0x87) {
122 outb(0x87, 0x4e);
123 outb(0x01, 0x4e);
124 outb(0x55, 0x4e);
125 outb(0xaa, 0x4e);
126 if (inb(0x4f) != 0x87) {
127 printf("Can not access SiS 950\n");
128 return -1;
129 }
130 outb(0x24, 0x4e);
131 b = inb(0x4f) | 0xfc;
132 outb(0x24, 0x4e);
133 outb(b, 0x4f);
134 outb(0x02, 0x4e);
135 outb(0x02, 0x4f);
136 }
137
138 outb(0x24, 0x2e);
139 printf("2f is %#x\n", inb(0x2f));
140 b = inb(0x2f) | 0xfc;
141 outb(0x24, 0x2e);
142 outb(b, 0x2f);
143
144 outb(0x02, 0x2e);
145 outb(0x02, 0x2f);
146
147 return 0;
148}
149
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000150int
151enable_flash_e7500(struct pci_dev *dev, char *name) {
152 /* register 4e.b gets or'ed with one */
153 unsigned char old, new;
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000154 /* if it fails, it fails. There are so many variations of broken mobos
155 * that it is hard to argue that we should quit at this point.
156 */
157
158 old = pci_read_byte(dev, 0x4e);
159
160 new = old | 1;
161
Ronald G. Minniche5559572003-03-28 03:29:43 +0000162 if (new == old)
163 return 0;
164
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000165 pci_write_byte(dev, 0x4e, new);
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000166
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000167 if (pci_read_byte(dev, 0x4e) != new) {
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000168 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000169 0x4e, new, name);
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000170 return -1;
171 }
172 return 0;
173}
174
Ronald G. Minniche5559572003-03-28 03:29:43 +0000175int
Andrew Ip772f6452003-07-21 14:54:16 +0000176enable_flash_vt8235(struct pci_dev *dev, char *name) {
177 unsigned char old, new, val;
178 unsigned int base;
179 int ok;
180
181 /* get io privilege access PCI configuration space */
182 if (iopl(3) != 0) {
183 perror("Can not set io priviliage");
184 exit(1);
185 }
186
187 old = pci_read_byte(dev, 0x40);
188
189 new = old | 0x10;
190
191 if (new == old)
192 return 0;
193
194 ok = pci_write_byte(dev, 0x40, new);
195 if (ok != 0) {
196 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
197 old, new, name);
198 }
199
200 /* enable GPIO15 which is connected to write protect. */
201 base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8);
202 val = inb(base + 0x4d);
203 val |= 0x80;
204 outb(val, base + 0x4d);
205
206 if (ok != 0) {
207 return -1;
208 } else {
209 return 0;
210 }
211}
212
213int
Ronald G. Minniche5559572003-03-28 03:29:43 +0000214enable_flash_vt8231(struct pci_dev *dev, char *name) {
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000215 unsigned char val;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000216
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000217 val = pci_read_byte(dev, 0x40);
218 val |= 0x10;
219 pci_write_byte(dev, 0x40, val);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000220
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000221 if (pci_read_byte(dev, 0x40) != val) {
Ronald G. Minniche5559572003-03-28 03:29:43 +0000222 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000223 0x40, val, name);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000224 return -1;
225 }
226 return 0;
227}
228
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000229int
230enable_flash_cs5530(struct pci_dev *dev, char *name) {
231 unsigned char new;
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000232
233 pci_write_byte(dev, 0x52, 0xee);
234
235 new = pci_read_byte(dev, 0x52);
236
237 if (new != 0xee) {
238 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
239 0x52, new, name);
240 return -1;
241 }
242 return 0;
243}
244
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000245int
Andrew Ipafe44a92003-07-22 10:12:08 +0000246enable_flash_sc1100(struct pci_dev *dev, char *name) {
247 unsigned char new;
Andrew Ipafe44a92003-07-22 10:12:08 +0000248
249 pci_write_byte(dev, 0x52, 0xee);
250
251 new = pci_read_byte(dev, 0x52);
252
253 if (new != 0xee) {
254 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
255 0x52, new, name);
256 return -1;
257 }
258 return 0;
259}
260
261int
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000262enable_flash_sis5595(struct pci_dev *dev, char *name) {
263 unsigned char new, newer;
264
265 new = pci_read_byte(dev, 0x45);
266
267 /* clear bit 5 */
268 new &= (~ 0x20);
269 /* set bit 2 */
270 new |= 0x4;
271
272 pci_write_byte(dev, 0x45, new);
273
274 newer = pci_read_byte(dev, 0x45);
275 if (newer != new) {
276 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
277 0x45, new, name);
278 printf("Stuck at 0x%x\n", newer);
279 return -1;
280 }
281 return 0;
282}
Ronald G. Minniche3dad012004-02-10 21:34:18 +0000283#endif
284//BY LYH
285#if 0
286static void dump_pci_device(struct pci_dev *dev)
287{
288 int i;
289
290 printf("\n");
291 for(i = 0; i <= 255; i++) {
292 unsigned char val;
293 if ((i & 0x0f) == 0) {
294 printf("0x%02x:",i);
295 }
296 val = pci_read_byte(dev, i);
297 printf(" 0x%02x",val);
298 if ((i & 0x0f) == 0x0f) {
299 printf("\r\n");
300 }
301 }
302}
303
304#endif
305
306int
307enable_flash_amd8111(struct pci_dev *dev, char *name) {
308 /* register 4e.b gets or'ed with one */
309 unsigned char old, new;
310 /* if it fails, it fails. There are so many variations of broken mobos
311 * that it is hard to argue that we should quit at this point.
312 */
313
314// dump_pci_device(dev);
315
316 old = pci_read_byte(dev, 0x43);
317
318 new = old | 0x80;
319
320 if (new != old) {
321
322 pci_write_byte(dev, 0x43, new);
323
324 if (pci_read_byte(dev, 0x43) != new) {
325 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
326 0x43, new, name);
327 }
328 }
329
330
331 old = pci_read_byte(dev, 0x40);
332
333 new = old | 0x01;
334
335 if (new == old)
336 return 0;
337
338 pci_write_byte(dev, 0x40, new);
339
340 if (pci_read_byte(dev, 0x40) != new) {
341 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
342 0x40, new, name);
343 return -1;
344 }
345 return 0;
346}
347//By LYH END
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000348
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000349struct flashchip * probe_flash(struct flashchip * flash)
350{
351 int fd_mem;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000352 volatile char * bios;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000353 unsigned long size;
354
355 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
356 perror("Can not open /dev/mem");
357 exit(1);
358 }
359
360 while (flash->name != NULL) {
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000361 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
362 flash++;
363 continue;
364 }
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000365 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000366 size = flash->total_size * 1024;
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000367/* BUG? what happens if getpagesize() > size!?
368 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
369 if(getpagesize() > size)
370 {
371 size = getpagesize();
372 printf("%s: warning: size: %d -> %ld\n", __FUNCTION__,
373 flash->total_size * 1024, (unsigned long)size);
374 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000375 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
Ronald G. Minniche3dad012004-02-10 21:34:18 +0000376 fd_mem, (off_t) (0-size));
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000377 if (bios == MAP_FAILED) {
378 perror("Error MMAP /dev/mem");
379 exit(1);
380 }
381 flash->virt_addr = bios;
Ronald G. Minnich56439422002-09-06 16:58:14 +0000382 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000383
384 if (flash->probe(flash) == 1) {
385 printf ("%s found at physical address: 0x%lx\n",
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000386 flash->name, (0 - size));
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000387 return flash;
388 }
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000389 munmap ((void *) bios, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000390 flash++;
391 }
392 return NULL;
393}
394
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000395int verify_flash (struct flashchip * flash, char * buf, int verbose)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000396{
397 int i = 0;
398 int total_size = flash->total_size *1024;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000399 volatile char * bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000400
401 printf("Verifying address: ");
402 while (i++ < total_size) {
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000403 if (verbose)
404 printf("0x%08x", i);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000405 if (*(bios+i) != *(buf+i)) {
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000406 printf("FAILED\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000407 return 0;
408 }
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000409 if (verbose)
410 printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000411 }
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000412 if (verbose)
413 printf("\n");
414 else
415 printf("VERIFIED\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000416 return 1;
417}
418
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000419// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
420
Ronald G. Minniche5559572003-03-28 03:29:43 +0000421unsigned long micro = 1;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000422
423void
424myusec_calibrate_delay()
425{
Ronald G. Minniche5559572003-03-28 03:29:43 +0000426 int count = 1000;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000427 unsigned long timeusec;
428 struct timeval start, end;
429 int ok = 0;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000430 void myusec_delay(int time);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000431
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000432 printf("Setting up microsecond timing loop\n");
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000433 while (! ok) {
Ronald G. Minnich4572a822003-02-11 16:09:12 +0000434 //fprintf(stderr, "Try %d\n", count);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000435 gettimeofday(&start, 0);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000436 myusec_delay(count);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000437 gettimeofday(&end, 0);
438 timeusec = 1000000 * (end.tv_sec - start.tv_sec ) +
439 (end.tv_usec - start.tv_usec);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000440 //fprintf(stderr, "timeusec is %d\n", timeusec);
441 count *= 2;
442 if (timeusec < 1000000/4)
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000443 continue;
444 ok = 1;
445 }
446
447 // compute one microsecond. That will be count / time
448 micro = count / timeusec;
449
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000450 fprintf(stderr, "%ldM loops per second\n", (unsigned long)micro);
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000451
452
453}
454
455void
Ronald G. Minniche5559572003-03-28 03:29:43 +0000456myusec_delay(int time)
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000457{
458 volatile unsigned long i;
459 for(i = 0; i < time * micro; i++)
460 ;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000461}
462
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000463typedef struct penable {
464 unsigned short vendor, device;
465 char *name;
466 int (*doit)(struct pci_dev *dev, char *name);
467} FLASH_ENABLE;
468
469FLASH_ENABLE enables[] = {
Ronald G. Minniche3dad012004-02-10 21:34:18 +0000470#if 1
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000471 {0x1, 0x1, "sis630 -- what's the ID?", enable_flash_sis630},
472 {0x8086, 0x2480, "E7500", enable_flash_e7500},
Ronald G. Minniche5559572003-03-28 03:29:43 +0000473 {0x1106, 0x8231, "VT8231", enable_flash_vt8231},
Andrew Ip772f6452003-07-21 14:54:16 +0000474 {0x1106, 0x3177, "VT8235", enable_flash_vt8235},
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000475 {0x1078, 0x0100, "CS5530", enable_flash_cs5530},
Andrew Ipafe44a92003-07-22 10:12:08 +0000476 {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +0000477 {0x1039, 0x8, "SIS5595", enable_flash_sis5595},
Ronald G. Minniche3dad012004-02-10 21:34:18 +0000478#endif
479 {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000480};
481
482int
483enable_flash_write() {
484 int i;
485 struct pci_access *pacc;
486 struct pci_dev *dev = 0;
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000487 FLASH_ENABLE *enable = 0;
488
489 pacc = pci_alloc(); /* Get the pci_access structure */
490 /* Set all options you want -- here we stick with the defaults */
491 pci_init(pacc); /* Initialize the PCI library */
492 pci_scan_bus(pacc); /* We want to get the list of devices */
493
494 /* now let's try to find the chipset we have ... */
495 for(i = 0; i < sizeof(enables)/sizeof(enables[0]) && (! dev); i++) {
496 struct pci_filter f;
497 struct pci_dev *z;
498 /* the first param is unused. */
499 pci_filter_init((struct pci_access *) 0, &f);
500 f.vendor = enables[i].vendor;
501 f.device = enables[i].device;
502 for(z=pacc->devices; z; z=z->next)
503 if (pci_filter_match(&f, z)) {
504 enable = &enables[i];
505 dev = z;
506 }
507 }
508
509 /* now do the deed. */
Ronald G. Minniche5559572003-03-28 03:29:43 +0000510 if (enable) {
511 printf("Enabling flash write on %s...", enable->name);
512 if (enable->doit(dev, enable->name) == 0)
513 printf("OK\n");
514 }
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000515 return 0;
516}
517
Ronald G. Minniche5559572003-03-28 03:29:43 +0000518void usage(const char *name)
519{
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000520 printf("usage: %s [-rwv] [-c chipname][file]\n", name);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000521 printf("-r: read flash and save into file\n"
522 "-w: write file into flash (default when file is specified)\n"
523 "-v: verify flash against file\n"
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000524 "-c: probe only for specified flash chip\n"
Ronald G. Minniche5559572003-03-28 03:29:43 +0000525 " If no file is specified, then all that happens\n"
526 " is that flash info is dumped\n");
527 exit(1);
528}
529
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000530int
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000531main (int argc, char * argv[])
532{
533 char * buf;
534 unsigned long size;
535 FILE * image;
536 struct flashchip * flash;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000537 int opt;
538 int read_it = 0, write_it = 0, verify_it = 0;
539 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000540
541 setbuf(stdout, NULL);
542
543 while ((opt = getopt(argc, argv, "rwvc:")) != EOF) {
Ronald G. Minniche5559572003-03-28 03:29:43 +0000544 switch (opt) {
545 case 'r':
546 read_it = 1;
547 break;
548 case 'w':
549 write_it = 1;
550 break;
551 case 'v':
552 verify_it = 1;
553 break;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000554 case 'c':
555 chip_to_probe = strdup(optarg);
556 break;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000557 default:
558 usage(argv[0]);
559 break;
560 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000561 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000562 if (read_it && write_it) {
563 printf("-r and -w are mutually exclusive\n");
564 usage(argv[0]);
565 }
566
567 if (optind < argc)
568 filename = argv[optind++];
569
570 printf("Calibrating timer since microsleep sucks ... takes a second\n");
571 myusec_calibrate_delay();
572 printf("OK, calibrated, now do the deed\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000573
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000574 /* try to enable it. Failure IS an option, since not all motherboards
575 * really need this to be done, etc., etc. It sucks.
576 */
577 (void) enable_flash_write();
578
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000579 if ((flash = probe_flash (flashchips)) == NULL) {
580 printf("EEPROM not found\n");
581 exit(1);
582 }
583
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000584 printf("Part is %s\n", flash->name);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000585 if (!filename){
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000586 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000587 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000588 }
589 size = flash->total_size * 1024;
Ronald G. Minniche5559572003-03-28 03:29:43 +0000590 buf = (char *) calloc (size, sizeof(char));
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000591
Ronald G. Minniche5559572003-03-28 03:29:43 +0000592 if (read_it) {
593 if ((image = fopen (filename, "w")) == NULL) {
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000594 perror(filename);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000595 exit(1);
596 }
597 printf("Reading Flash...");
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000598 if(flash->read == NULL)
599 memcpy(buf, (const char *) flash->virt_addr, size);
600 else
601 flash->read (flash, buf);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000602 fwrite(buf, sizeof(char), size, image);
603 fclose(image);
604 printf("done\n");
605 } else {
606 if ((image = fopen (filename, "r")) == NULL) {
Ronald G. Minnichb9d27702003-04-21 17:56:12 +0000607 perror(filename);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000608 exit(1);
609 }
610 fread (buf, sizeof(char), size, image);
611 fclose(image);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000612 }
613
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000614 if (write_it || (!read_it && !verify_it))
Ronald G. Minniche5559572003-03-28 03:29:43 +0000615 flash->write (flash, buf);
616 if (verify_it)
617 verify_flash (flash, buf, /* verbose = */ 0);
Ronald G. Minnich4572a822003-02-11 16:09:12 +0000618 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000619}