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