blob: a4f8ca5fd8b57f8d3129a5f795d52e2242018e68 [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. Minnichbb0322f2003-01-13 23:43:36 +000066 {"82802ac", 137, 172 , NULL, 1024, 64*1024,
67 probe_82802ab, erase_82802ab, write_82802ab},
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000068 {NULL,}
69};
70
71int enable_flash_sis630 (void)
72{
73 char b;
74
75 /* get io privilege access PCI configuration space */
76 if (iopl(3) != 0) {
77 perror("Can not set io priviliage");
78 exit(1);
79 }
80
81 /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
82 outl(0x80000840, 0x0cf8);
83 b = inb(0x0cfc) | 0x0b;
84 outb(b, 0xcfc);
85 /* Flash write enable on SiS 540/630 */
86 outl(0x80000845, 0x0cf8);
87 b = inb(0x0cfd) | 0x40;
88 outb(b, 0xcfd);
89
90 /* The same thing on SiS 950 SuperIO side */
91 outb(0x87, 0x2e);
92 outb(0x01, 0x2e);
93 outb(0x55, 0x2e);
94 outb(0x55, 0x2e);
95
96 if (inb(0x2f) != 0x87) {
97 outb(0x87, 0x4e);
98 outb(0x01, 0x4e);
99 outb(0x55, 0x4e);
100 outb(0xaa, 0x4e);
101 if (inb(0x4f) != 0x87) {
102 printf("Can not access SiS 950\n");
103 return -1;
104 }
105 outb(0x24, 0x4e);
106 b = inb(0x4f) | 0xfc;
107 outb(0x24, 0x4e);
108 outb(b, 0x4f);
109 outb(0x02, 0x4e);
110 outb(0x02, 0x4f);
111 }
112
113 outb(0x24, 0x2e);
114 printf("2f is %#x\n", inb(0x2f));
115 b = inb(0x2f) | 0xfc;
116 outb(0x24, 0x2e);
117 outb(b, 0x2f);
118
119 outb(0x02, 0x2e);
120 outb(0x02, 0x2f);
121
122 return 0;
123}
124
125struct flashchip * probe_flash(struct flashchip * flash)
126{
127 int fd_mem;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000128 volatile char * bios;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000129 unsigned long size;
130
131 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
132 perror("Can not open /dev/mem");
133 exit(1);
134 }
135
136 while (flash->name != NULL) {
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000137 printf("Trying %s, %d KB\n", flash->name, flash->total_size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000138 size = flash->total_size * 1024;
139 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
140 fd_mem, (off_t) (0 - size));
141 if (bios == MAP_FAILED) {
142 perror("Error MMAP /dev/mem");
143 exit(1);
144 }
145 flash->virt_addr = bios;
Ronald G. Minnich56439422002-09-06 16:58:14 +0000146 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000147
148 if (flash->probe(flash) == 1) {
149 printf ("%s found at physical address: 0x%lx\n",
150 flash->name, (0 - size), bios);
151 return flash;
152 }
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000153 munmap ((void *) bios, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000154 flash++;
155 }
156 return NULL;
157}
158
159int verify_flash (struct flashchip * flash, char * buf)
160{
161 int i = 0;
162 int total_size = flash->total_size *1024;
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000163 volatile char * bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000164
165 printf("Verifying address: ");
166 while (i++ < total_size) {
167 printf("0x%08x", i);
168 if (*(bios+i) != *(buf+i)) {
169 return 0;
170 }
171 printf("\b\b\b\b\b\b\b\b\b\b");
172 }
173 printf("\n");
174 return 1;
175}
176
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000177// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
178
179unsigned long micro = 0;
180
181void
182myusec_calibrate_delay()
183{
184 unsigned long count = 2 * 1024 * 1024;
185 volatile unsigned long i;
186 unsigned long timeusec;
187 struct timeval start, end;
188 int ok = 0;
189
190 fprintf(stderr, "Setting up microsecond timing loop\n");
191 while (! ok) {
192 fprintf(stderr, "Try %d\n", count);
193 gettimeofday(&start, 0);
194 for( i = count; i; i--)
195 ;
196 gettimeofday(&end, 0);
197 timeusec = 1000000 * (end.tv_sec - start.tv_sec ) +
198 (end.tv_usec - start.tv_usec);
199 fprintf(stderr, "timeusec is %d\n", timeusec);
200 count *= 10;
201 if (timeusec < 1000000)
202 continue;
203 ok = 1;
204 }
205
206 // compute one microsecond. That will be count / time
207 micro = count / timeusec;
208
209 fprintf(stderr, "one us is %d count\n", micro);
210
211
212}
213
214void
215myusec_delay(time)
216{
217 volatile unsigned long i;
218 for(i = 0; i < time * micro; i++)
219 ;
220
221}
222
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000223main (int argc, char * argv[])
224{
225 char * buf;
226 unsigned long size;
227 FILE * image;
228 struct flashchip * flash;
229
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000230 myusec_calibrate_delay();
231
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000232 if (argc > 2){
233 printf("usage: %s [romimage]\n", argv[0]);
234 printf(" If no romimage is specified, then all that happens\n");
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000235 printf(" is that flash info is dumped\n");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000236 }
237
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000238 if ((flash = probe_flash (flashchips)) == NULL) {
239 printf("EEPROM not found\n");
240 exit(1);
241 }
242
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000243 printf("Part is %s\n", flash->name);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000244 if (argc < 2){
245 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
246 exit(0);
247 }
248 size = flash->total_size * 1024;
249
250 if ((image = fopen (argv[1], "r")) == NULL) {
251 perror("Error opening image file");
252 exit(1);
253 }
254
255 buf = (char *) calloc (size, sizeof(char));
256 fread (buf, sizeof(char), size, image);
257
258 flash->write (flash, buf);
259 verify_flash (flash, buf);
260}