blob: 84c7661854559324c95da70ed06a9c932e24748c [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},
Andrew Ip973b26d2002-12-30 13:10:06 +000056 {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128,
57 probe_jedec, erase_jedec, write_jedec},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000058 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
59 probe_jedec, erase_jedec, write_jedec},
Andrew Ipf0126ce2002-10-16 06:58:05 +000060 {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128,
61 probe_49f002, erase_49f002, write_49f002},
Ronald G. Minnich3c910ed2002-05-28 23:29:17 +000062 {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024,
63 probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt},
Ronald G. Minnich56439422002-09-06 16:58:14 +000064 {"82802ab", 137, 173 , NULL, 512, 64*1024,
65 probe_82802ab, erase_82802ab, write_82802ab},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000066 {NULL,}
67};
68
69int enable_flash_sis630 (void)
70{
71 char b;
72
73 /* get io privilege access PCI configuration space */
74 if (iopl(3) != 0) {
75 perror("Can not set io priviliage");
76 exit(1);
77 }
78
79 /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
80 outl(0x80000840, 0x0cf8);
81 b = inb(0x0cfc) | 0x0b;
82 outb(b, 0xcfc);
83 /* Flash write enable on SiS 540/630 */
84 outl(0x80000845, 0x0cf8);
85 b = inb(0x0cfd) | 0x40;
86 outb(b, 0xcfd);
87
88 /* The same thing on SiS 950 SuperIO side */
89 outb(0x87, 0x2e);
90 outb(0x01, 0x2e);
91 outb(0x55, 0x2e);
92 outb(0x55, 0x2e);
93
94 if (inb(0x2f) != 0x87) {
95 outb(0x87, 0x4e);
96 outb(0x01, 0x4e);
97 outb(0x55, 0x4e);
98 outb(0xaa, 0x4e);
99 if (inb(0x4f) != 0x87) {
100 printf("Can not access SiS 950\n");
101 return -1;
102 }
103 outb(0x24, 0x4e);
104 b = inb(0x4f) | 0xfc;
105 outb(0x24, 0x4e);
106 outb(b, 0x4f);
107 outb(0x02, 0x4e);
108 outb(0x02, 0x4f);
109 }
110
111 outb(0x24, 0x2e);
112 printf("2f is %#x\n", inb(0x2f));
113 b = inb(0x2f) | 0xfc;
114 outb(0x24, 0x2e);
115 outb(b, 0x2f);
116
117 outb(0x02, 0x2e);
118 outb(0x02, 0x2f);
119
120 return 0;
121}
122
123struct flashchip * probe_flash(struct flashchip * flash)
124{
125 int fd_mem;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000126 volatile char * bios;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000127 unsigned long size;
128
129 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
130 perror("Can not open /dev/mem");
131 exit(1);
132 }
133
134 while (flash->name != NULL) {
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000135 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000136 size = flash->total_size * 1024;
137 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
138 fd_mem, (off_t) (0 - size));
139 if (bios == MAP_FAILED) {
140 perror("Error MMAP /dev/mem");
141 exit(1);
142 }
143 flash->virt_addr = bios;
Ronald G. Minnich56439422002-09-06 16:58:14 +0000144 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000145
146 if (flash->probe(flash) == 1) {
147 printf ("%s found at physical address: 0x%lx\n",
148 flash->name, (0 - size), bios);
149 return flash;
150 }
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000151 munmap ((void *) bios, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000152 flash++;
153 }
154 return NULL;
155}
156
157int verify_flash (struct flashchip * flash, char * buf)
158{
159 int i = 0;
160 int total_size = flash->total_size *1024;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000161 volatile char * bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000162
163 printf("Verifying address: ");
164 while (i++ < total_size) {
165 printf("0x%08x", i);
166 if (*(bios+i) != *(buf+i)) {
167 return 0;
168 }
169 printf("\b\b\b\b\b\b\b\b\b\b");
170 }
171 printf("\n");
172 return 1;
173}
174
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000175// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
176
177unsigned long micro = 0;
178
179void
180myusec_calibrate_delay()
181{
182 unsigned long count = 2 * 1024 * 1024;
183 volatile unsigned long i;
184 unsigned long timeusec;
185 struct timeval start, end;
186 int ok = 0;
187
188 fprintf(stderr, "Setting up microsecond timing loop\n");
189 while (! ok) {
190 fprintf(stderr, "Try %d\n", count);
191 gettimeofday(&start, 0);
192 for( i = count; i; i--)
193 ;
194 gettimeofday(&end, 0);
195 timeusec = 1000000 * (end.tv_sec - start.tv_sec ) +
196 (end.tv_usec - start.tv_usec);
197 fprintf(stderr, "timeusec is %d\n", timeusec);
198 count *= 10;
199 if (timeusec < 1000000)
200 continue;
201 ok = 1;
202 }
203
204 // compute one microsecond. That will be count / time
205 micro = count / timeusec;
206
207 fprintf(stderr, "one us is %d count\n", micro);
208
209
210}
211
212void
213myusec_delay(time)
214{
215 volatile unsigned long i;
216 for(i = 0; i < time * micro; i++)
217 ;
218
219}
220
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000221main (int argc, char * argv[])
222{
223 char * buf;
224 unsigned long size;
225 FILE * image;
226 struct flashchip * flash;
227
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000228 myusec_calibrate_delay();
229
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000230 if (argc > 2){
231 printf("usage: %s [romimage]\n", argv[0]);
232 printf(" If no romimage is specified, then all that happens\n");
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000233 printf(" is that flash info is dumped\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000234 }
235
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000236 if ((flash = probe_flash (flashchips)) == NULL) {
237 printf("EEPROM not found\n");
238 exit(1);
239 }
240
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000241 printf("Part is %s\n", flash->name);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000242 if (argc < 2){
243 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
244 exit(0);
245 }
246 size = flash->total_size * 1024;
247
248 if ((image = fopen (argv[1], "r")) == NULL) {
249 perror("Error opening image file");
250 exit(1);
251 }
252
253 buf = (char *) calloc (size, sizeof(char));
254 fread (buf, sizeof(char), size, image);
255
256 flash->write (flash, buf);
257 verify_flash (flash, buf);
258}