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