blob: 85a016a1b3915396a5844c63d938611261b1876b [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>
33#include <unistd.h>
34#include <stdio.h>
35
36#include "flash.h"
37#include "jedec.h"
Ronald G. Minnich3c910ed2002-05-28 23:29:17 +000038#include "m29f400bt.h"
Ronald G. Minnich56439422002-09-06 16:58:14 +000039#include "82802ab.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000040
41struct flashchip flashchips[] = {
42 {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024,
43 probe_29f040b, erase_29f040b, write_29f040b},
44 {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256,
45 probe_jedec, erase_jedec, write_jedec},
46 {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024,
47 probe_29f002, erase_29f002, write_29f002},
48 {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128,
49 probe_jedec, erase_jedec, write_jedec},
50 {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256,
51 probe_28sf040, erase_28sf040, write_28sf040},
Ronald G. Minnichc8316472002-03-21 22:40:40 +000052 {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096,
Ronald G. Minnich213ee712002-04-10 16:01:38 +000053 probe_39sf020, erase_39sf020, write_39sf020},
Ollie Lho6041bcd2002-07-18 03:32:00 +000054 {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096,
55 probe_39sf020, erase_39sf020, write_39sf020},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000056 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
57 probe_jedec, erase_jedec, write_jedec},
Ronald G. Minnich3c910ed2002-05-28 23:29:17 +000058 {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024,
59 probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt},
Ronald G. Minnich56439422002-09-06 16:58:14 +000060 {"82802ab", 137, 173 , NULL, 512, 64*1024,
61 probe_82802ab, erase_82802ab, write_82802ab},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000062 {NULL,}
63};
64
65int enable_flash_sis630 (void)
66{
67 char b;
68
69 /* get io privilege access PCI configuration space */
70 if (iopl(3) != 0) {
71 perror("Can not set io priviliage");
72 exit(1);
73 }
74
75 /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
76 outl(0x80000840, 0x0cf8);
77 b = inb(0x0cfc) | 0x0b;
78 outb(b, 0xcfc);
79 /* Flash write enable on SiS 540/630 */
80 outl(0x80000845, 0x0cf8);
81 b = inb(0x0cfd) | 0x40;
82 outb(b, 0xcfd);
83
84 /* The same thing on SiS 950 SuperIO side */
85 outb(0x87, 0x2e);
86 outb(0x01, 0x2e);
87 outb(0x55, 0x2e);
88 outb(0x55, 0x2e);
89
90 if (inb(0x2f) != 0x87) {
91 outb(0x87, 0x4e);
92 outb(0x01, 0x4e);
93 outb(0x55, 0x4e);
94 outb(0xaa, 0x4e);
95 if (inb(0x4f) != 0x87) {
96 printf("Can not access SiS 950\n");
97 return -1;
98 }
99 outb(0x24, 0x4e);
100 b = inb(0x4f) | 0xfc;
101 outb(0x24, 0x4e);
102 outb(b, 0x4f);
103 outb(0x02, 0x4e);
104 outb(0x02, 0x4f);
105 }
106
107 outb(0x24, 0x2e);
108 printf("2f is %#x\n", inb(0x2f));
109 b = inb(0x2f) | 0xfc;
110 outb(0x24, 0x2e);
111 outb(b, 0x2f);
112
113 outb(0x02, 0x2e);
114 outb(0x02, 0x2f);
115
116 return 0;
117}
118
119struct flashchip * probe_flash(struct flashchip * flash)
120{
121 int fd_mem;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000122 volatile char * bios;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000123 unsigned long size;
124
125 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
126 perror("Can not open /dev/mem");
127 exit(1);
128 }
129
130 while (flash->name != NULL) {
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000131 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000132 size = flash->total_size * 1024;
133 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
134 fd_mem, (off_t) (0 - size));
135 if (bios == MAP_FAILED) {
136 perror("Error MMAP /dev/mem");
137 exit(1);
138 }
139 flash->virt_addr = bios;
Ronald G. Minnich56439422002-09-06 16:58:14 +0000140 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000141
142 if (flash->probe(flash) == 1) {
143 printf ("%s found at physical address: 0x%lx\n",
144 flash->name, (0 - size), bios);
145 return flash;
146 }
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000147 munmap ((void *) bios, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000148 flash++;
149 }
150 return NULL;
151}
152
153int verify_flash (struct flashchip * flash, char * buf)
154{
155 int i = 0;
156 int total_size = flash->total_size *1024;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000157 volatile char * bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000158
159 printf("Verifying address: ");
160 while (i++ < total_size) {
161 printf("0x%08x", i);
162 if (*(bios+i) != *(buf+i)) {
163 return 0;
164 }
165 printf("\b\b\b\b\b\b\b\b\b\b");
166 }
167 printf("\n");
168 return 1;
169}
170
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000171// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
172
173unsigned long micro = 0;
174
175void
176myusec_calibrate_delay()
177{
178 unsigned long count = 2 * 1024 * 1024;
179 volatile unsigned long i;
180 unsigned long timeusec;
181 struct timeval start, end;
182 int ok = 0;
183
184 fprintf(stderr, "Setting up microsecond timing loop\n");
185 while (! ok) {
186 fprintf(stderr, "Try %d\n", count);
187 gettimeofday(&start, 0);
188 for( i = count; i; i--)
189 ;
190 gettimeofday(&end, 0);
191 timeusec = 1000000 * (end.tv_sec - start.tv_sec ) +
192 (end.tv_usec - start.tv_usec);
193 fprintf(stderr, "timeusec is %d\n", timeusec);
194 count *= 10;
195 if (timeusec < 1000000)
196 continue;
197 ok = 1;
198 }
199
200 // compute one microsecond. That will be count / time
201 micro = count / timeusec;
202
203 fprintf(stderr, "one us is %d count\n", micro);
204
205
206}
207
208void
209myusec_delay(time)
210{
211 volatile unsigned long i;
212 for(i = 0; i < time * micro; i++)
213 ;
214
215}
216
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000217main (int argc, char * argv[])
218{
219 char * buf;
220 unsigned long size;
221 FILE * image;
222 struct flashchip * flash;
223
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000224 myusec_calibrate_delay();
225
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000226 if (argc > 2){
227 printf("usage: %s [romimage]\n", argv[0]);
228 printf(" If no romimage is specified, then all that happens\n");
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000229 printf(" is that flash info is dumped\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000230 }
231
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000232 if ((flash = probe_flash (flashchips)) == NULL) {
233 printf("EEPROM not found\n");
234 exit(1);
235 }
236
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000237 printf("Part is %s\n", flash->name);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000238 if (argc < 2){
239 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
240 exit(0);
241 }
242 size = flash->total_size * 1024;
243
244 if ((image = fopen (argv[1], "r")) == NULL) {
245 perror("Error opening image file");
246 exit(1);
247 }
248
249 buf = (char *) calloc (size, sizeof(char));
250 fread (buf, sizeof(char), size, image);
251
252 flash->write (flash, buf);
253 verify_flash (flash, buf);
254}