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