now we support 8111 and these parts.
[coreboot.git] / util / flash_and_burn / flash_rom.c
1 /*
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>
33 #include <sys/time.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <pci/pci.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 #include "flash.h"
41 #include "jedec.h"
42 #include "m29f400bt.h"
43 #include "82802ab.h"
44 #include "msys_doc.h"
45 #include "am29f040b.h"
46 #include "sst28sf040.h"
47 #include "w49f002u.h"
48 #include "sst39sf020.h"
49 #include "pm49fl004.h"
50 #include "mx29f002.h"
51
52 struct flashchip flashchips[] = {
53 #if  1
54    {"Am29F040B",   AMD_ID,     AM_29F040B,   NULL, 512, 64*1024,
55      probe_29f040b, erase_29f040b, write_29f040b, NULL},
56     {"At29C040A",   ATMEL_ID,   AT_29C040A,   NULL, 512, 256,
57      probe_jedec,   erase_jedec,   write_jedec, NULL},
58     {"Mx29f002",    MX_ID,      MX_29F002,    NULL, 256, 64*1024,
59      probe_29f002,  erase_29f002,  write_29f002, NULL},
60     {"SST29EE020A", SST_ID,     SST_29EE020A, NULL, 256, 128,
61      probe_jedec,   erase_jedec,   write_jedec, NULL},
62     {"SST28SF040A", SST_ID,     SST_28SF040,  NULL, 512, 256,
63      probe_28sf040, erase_28sf040, write_28sf040, NULL},
64     {"SST39SF020A", SST_ID,     SST_39SF020,  NULL, 256, 4096,
65      probe_39sf020, erase_39sf020, write_39sf020, NULL},
66     {"SST39VF020",  SST_ID,     SST_39VF020,  NULL, 256, 4096,
67      probe_39sf020, erase_39sf020, write_39sf020, NULL},
68 #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
74     {"W29C011",    WINBOND_ID, W_29C011,    NULL, 128, 128,
75      probe_jedec,   erase_jedec,   write_jedec, NULL},
76     {"W29C020C",    WINBOND_ID, W_29C020C,    NULL, 256, 128,
77      probe_jedec,   erase_jedec,   write_jedec, NULL},
78     {"W49F002U",    WINBOND_ID, W_49F002U,    NULL, 256, 128,
79      probe_49f002,   erase_49f002,   write_49f002, NULL},
80     {"M29F400BT",   ST_ID, ST_M29F400BT ,    NULL, 512, 64*1024,
81      probe_m29f400bt,   erase_m29f400bt,   write_linuxbios_m29f400bt, NULL},
82     {"82802ab",   137, 173 ,    NULL, 512, 64*1024,
83      probe_82802ab,   erase_82802ab,   write_82802ab, NULL},
84     {"82802ac",   137, 172 ,    NULL, 1024, 64*1024,
85      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},
90     {NULL,}
91 #endif
92 };
93
94 char *chip_to_probe = NULL;
95 #if 1
96 int enable_flash_sis630 (struct pci_dev *dev, char *name)
97 {
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
150 int
151 enable_flash_e7500(struct pci_dev *dev, char *name) {
152   /* register 4e.b gets or'ed with one */
153   unsigned char old, new;
154   /* 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
162   if (new == old)
163       return 0;
164
165   pci_write_byte(dev, 0x4e, new);
166
167   if (pci_read_byte(dev, 0x4e) != new) {
168     printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 
169            0x4e, new, name);
170     return -1;
171   }
172   return 0;
173 }
174
175 int
176 enable_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
213 int
214 enable_flash_vt8231(struct pci_dev *dev, char *name) {
215   unsigned char val;
216   
217   val = pci_read_byte(dev, 0x40);
218   val |= 0x10;
219   pci_write_byte(dev, 0x40, val);
220
221   if (pci_read_byte(dev, 0x40) != val) {
222     printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 
223            0x40, val, name);
224     return -1;
225   }
226   return 0;
227 }
228
229 int
230 enable_flash_cs5530(struct pci_dev *dev, char *name) {
231   unsigned char new;
232   
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
245 int
246 enable_flash_sc1100(struct pci_dev *dev, char *name) {
247   unsigned char new;
248   
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
261 int
262 enable_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 }
283 #endif
284 //BY LYH
285 #if 0
286 static 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
306 int
307 enable_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
348
349 struct flashchip * probe_flash(struct flashchip * flash)
350 {
351     int fd_mem;
352     volatile char * bios;
353     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) {
361         if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
362             flash++;
363             continue;
364         }
365         printf("Trying %s, %d KB\n", flash->name, flash->total_size);
366         size = flash->total_size * 1024;
367 /* 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         }
375         bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
376                      fd_mem, (off_t) (0-size));
377         if (bios == MAP_FAILED) {
378             perror("Error MMAP /dev/mem");
379             exit(1);
380         }
381         flash->virt_addr = bios;
382         flash->fd_mem = fd_mem;
383
384         if (flash->probe(flash) == 1) {
385             printf ("%s found at physical address: 0x%lx\n",
386                     flash->name, (0 - size));
387             return flash;
388         }
389         munmap ((void *) bios, size);
390         flash++;
391     }
392     return NULL;
393 }
394
395 int verify_flash (struct flashchip * flash, char * buf, int verbose)
396 {
397     int i = 0;
398     int total_size = flash->total_size *1024;
399     volatile char * bios = flash->virt_addr;
400
401     printf("Verifying address: ");
402     while (i++ < total_size) {
403         if (verbose) 
404                 printf("0x%08x", i);
405         if (*(bios+i) != *(buf+i)) {
406                 printf("FAILED\n");
407             return 0;
408         }
409         if (verbose) 
410                 printf("\b\b\b\b\b\b\b\b\b\b");
411     }
412     if (verbose)
413         printf("\n");
414     else
415         printf("VERIFIED\n");
416     return 1;
417 }
418
419 // count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
420
421 unsigned long micro = 1;
422
423 void 
424 myusec_calibrate_delay()
425 {
426         int count = 1000;
427         unsigned long timeusec;
428         struct timeval start, end;
429         int ok = 0;
430         void myusec_delay(int time);
431
432         printf("Setting up microsecond timing loop\n");
433         while (! ok) {
434                 //fprintf(stderr, "Try %d\n", count);
435                 gettimeofday(&start, 0);
436                 myusec_delay(count);
437                 gettimeofday(&end, 0);
438                 timeusec = 1000000 * (end.tv_sec - start.tv_sec ) + 
439                                 (end.tv_usec - start.tv_usec);
440                 //fprintf(stderr, "timeusec is %d\n", timeusec);
441                 count *= 2;
442                 if (timeusec < 1000000/4)
443                         continue;
444                 ok = 1;
445         }
446
447         // compute one microsecond. That will be count / time
448         micro = count / timeusec;
449
450         fprintf(stderr, "%ldM loops per second\n", (unsigned long)micro);
451
452
453 }
454
455 void
456 myusec_delay(int time)
457 {
458   volatile unsigned long i;
459   for(i = 0; i < time * micro; i++)
460         ;
461 }
462
463 typedef struct penable {
464   unsigned short vendor, device; 
465   char *name;
466   int (*doit)(struct pci_dev *dev, char *name);
467 } FLASH_ENABLE;
468
469 FLASH_ENABLE enables[] = {
470 #if 1
471   {0x1, 0x1, "sis630 -- what's the ID?", enable_flash_sis630},
472   {0x8086, 0x2480, "E7500", enable_flash_e7500},
473   {0x1106, 0x8231, "VT8231", enable_flash_vt8231},
474   {0x1106, 0x3177, "VT8235", enable_flash_vt8235},
475   {0x1078, 0x0100, "CS5530", enable_flash_cs5530},
476   {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
477   {0x1039, 0x8, "SIS5595", enable_flash_sis5595},
478 #endif
479   {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
480 };
481   
482 int
483 enable_flash_write() {
484   int i;
485   struct pci_access *pacc;
486   struct pci_dev *dev = 0;
487   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. */
510   if (enable) {
511       printf("Enabling flash write on %s...", enable->name);
512       if (enable->doit(dev, enable->name) == 0)
513           printf("OK\n");
514   }
515   return 0;
516 }
517
518 void usage(const char *name)
519 {
520     printf("usage: %s [-rwv] [-c chipname][file]\n", name);
521     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"
524         "-c: probe only for specified flash chip\n"
525         " If no file is specified, then all that happens\n"
526         " is that flash info is dumped\n");
527     exit(1);
528 }
529
530 int
531 main (int argc, char * argv[])
532 {
533     char * buf;
534     unsigned long size;
535     FILE * image;
536     struct flashchip * flash;
537     int opt;
538     int read_it = 0, write_it = 0, verify_it = 0;
539     char *filename = NULL;
540
541     setbuf(stdout, NULL);
542
543     while ((opt = getopt(argc, argv, "rwvc:")) != EOF) {
544         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;
554         case 'c':
555             chip_to_probe = strdup(optarg);
556             break;
557         default:
558             usage(argv[0]);
559             break;
560         }
561     }
562     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");
573
574     /* 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     
579     if ((flash = probe_flash (flashchips)) == NULL) {
580         printf("EEPROM not found\n");
581         exit(1);
582     }
583
584     printf("Part is %s\n", flash->name);
585     if (!filename){
586         printf("OK, only ENABLING flash write, but NOT FLASHING\n");
587         return 0;
588     }
589     size = flash->total_size * 1024;
590     buf = (char *) calloc (size, sizeof(char));
591
592     if (read_it) {
593         if ((image = fopen (filename, "w")) == NULL) {
594             perror(filename);
595             exit(1);
596         }
597         printf("Reading Flash...");
598         if(flash->read == NULL)
599                 memcpy(buf, (const char *) flash->virt_addr, size);
600         else
601                 flash->read (flash, buf);
602         fwrite(buf, sizeof(char), size, image);
603         fclose(image);
604         printf("done\n");
605     } else {
606         if ((image = fopen (filename, "r")) == NULL) {
607             perror(filename);
608             exit(1);
609         }
610         fread (buf, sizeof(char), size, image);
611         fclose(image);
612     }
613
614     if (write_it || (!read_it && !verify_it))
615         flash->write (flash, buf);
616     if (verify_it)
617         verify_flash (flash, buf, /* verbose = */ 0);
618     return 0;
619 }