blob: 12aa2733b7fa8c24443a7077a55f5d30157c34a1 [file] [log] [blame]
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +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},
50 {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128,
51 probe_jedec, erase_jedec, write_jedec},
52 {NULL,}
53};
54
55int enable_flash_sis630 (void)
56{
57 char b;
58
59 /* get io privilege access PCI configuration space */
60 if (iopl(3) != 0) {
61 perror("Can not set io priviliage");
62 exit(1);
63 }
64
65 /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
66 outl(0x80000840, 0x0cf8);
67 b = inb(0x0cfc) | 0x0b;
68 outb(b, 0xcfc);
69 /* Flash write enable on SiS 540/630 */
70 outl(0x80000845, 0x0cf8);
71 b = inb(0x0cfd) | 0x40;
72 outb(b, 0xcfd);
73
74 /* The same thing on SiS 950 SuperIO side */
75 outb(0x87, 0x2e);
76 outb(0x01, 0x2e);
77 outb(0x55, 0x2e);
78 outb(0x55, 0x2e);
79
80 if (inb(0x2f) != 0x87) {
81 outb(0x87, 0x4e);
82 outb(0x01, 0x4e);
83 outb(0x55, 0x4e);
84 outb(0xaa, 0x4e);
85 if (inb(0x4f) != 0x87) {
86 printf("Can not access SiS 950\n");
87 return -1;
88 }
89 outb(0x24, 0x4e);
90 b = inb(0x4f) | 0xfc;
91 outb(0x24, 0x4e);
92 outb(b, 0x4f);
93 outb(0x02, 0x4e);
94 outb(0x02, 0x4f);
95 }
96
97 outb(0x24, 0x2e);
98 printf("2f is %#x\n", inb(0x2f));
99 b = inb(0x2f) | 0xfc;
100 outb(0x24, 0x2e);
101 outb(b, 0x2f);
102
103 outb(0x02, 0x2e);
104 outb(0x02, 0x2f);
105
106 return 0;
107}
108
109struct flashchip * probe_flash(struct flashchip * flash)
110{
111 int fd_mem;
112 char * bios;
113 unsigned long size;
114
115 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
116 perror("Can not open /dev/mem");
117 exit(1);
118 }
119
120 while (flash->name != NULL) {
121 size = flash->total_size * 1024;
122 bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
123 fd_mem, (off_t) (0 - size));
124 if (bios == MAP_FAILED) {
125 perror("Error MMAP /dev/mem");
126 exit(1);
127 }
128 flash->virt_addr = bios;
129
130 if (flash->probe(flash) == 1) {
131 printf ("%s found at physical address: 0x%lx\n",
132 flash->name, (0 - size), bios);
133 return flash;
134 }
135 munmap (bios, size);
136 flash++;
137 }
138 return NULL;
139}
140
141int verify_flash (struct flashchip * flash, char * buf)
142{
143 int i = 0;
144 int total_size = flash->total_size *1024;
145 char * bios = flash->virt_addr;
146
147 printf("Verifying address: ");
148 while (i++ < total_size) {
149 printf("0x%08x", i);
150 if (*(bios+i) != *(buf+i)) {
151 return 0;
152 }
153 printf("\b\b\b\b\b\b\b\b\b\b");
154 }
155 printf("\n");
156 return 1;
157}
158
159main (int argc, char * argv[])
160{
161 char * buf;
162 unsigned long size;
163 FILE * image;
164 struct flashchip * flash;
165
166 if (argc > 2){
167 printf("usage: %s [romimage]\n", argv[0]);
168 printf(" If no romimage is specified, then all that happens\n");
169 printf(" is that flash writes are enabled (useful for DoC)\n");
170 exit(1);
171 }
172
173 enable_flash_sis630 ();
174
175 if ((flash = probe_flash (flashchips)) == NULL) {
176 printf("EEPROM not found\n");
177 exit(1);
178 }
179
180 if (argc < 2){
181 printf("OK, only ENABLING flash write, but NOT FLASHING\n");
182 exit(0);
183 }
184 size = flash->total_size * 1024;
185
186 if ((image = fopen (argv[1], "r")) == NULL) {
187 perror("Error opening image file");
188 exit(1);
189 }
190
191 buf = (char *) calloc (size, sizeof(char));
192 fread (buf, sizeof(char), size, image);
193
194 flash->write (flash, buf);
195 verify_flash (flash, buf);
196}