blob: 02861f153d5ab66e67e8ab0c17b04fba1003af97 [file] [log] [blame]
Ollie Lhocbbf1252004-03-17 22:22:08 +00001#include <sys/io.h>
2#include <stdio.h>
3#include <pci/pci.h>
4#include <stdlib.h>
5
Ollie Lho761bf1b2004-03-20 16:46:10 +00006static int enable_flash_sis630(struct pci_dev *dev, char *name)
Ollie Lhocbbf1252004-03-17 22:22:08 +00007{
8 char b;
9
10 /* get io privilege access PCI configuration space */
11 if (iopl(3) != 0) {
12 perror("Can not set io priviliage");
13 exit(1);
14 }
15
16 /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
17 outl(0x80000840, 0x0cf8);
18 b = inb(0x0cfc) | 0x0b;
19 outb(b, 0xcfc);
20 /* Flash write enable on SiS 540/630 */
21 outl(0x80000845, 0x0cf8);
22 b = inb(0x0cfd) | 0x40;
23 outb(b, 0xcfd);
24
25 /* The same thing on SiS 950 SuperIO side */
26 outb(0x87, 0x2e);
27 outb(0x01, 0x2e);
28 outb(0x55, 0x2e);
29 outb(0x55, 0x2e);
30
31 if (inb(0x2f) != 0x87) {
32 outb(0x87, 0x4e);
33 outb(0x01, 0x4e);
34 outb(0x55, 0x4e);
35 outb(0xaa, 0x4e);
36 if (inb(0x4f) != 0x87) {
37 printf("Can not access SiS 950\n");
38 return -1;
39 }
40 outb(0x24, 0x4e);
41 b = inb(0x4f) | 0xfc;
42 outb(0x24, 0x4e);
43 outb(b, 0x4f);
44 outb(0x02, 0x4e);
Ollie Lho761bf1b2004-03-20 16:46:10 +000045 outb(0x02, 0x4f);
Ollie Lhocbbf1252004-03-17 22:22:08 +000046 }
47
48 outb(0x24, 0x2e);
49 printf("2f is %#x\n", inb(0x2f));
50 b = inb(0x2f) | 0xfc;
51 outb(0x24, 0x2e);
52 outb(b, 0x2f);
53
54 outb(0x02, 0x2e);
55 outb(0x02, 0x2f);
56
57 return 0;
58}
59
60static int enable_flash_e7500(struct pci_dev *dev, char *name)
61{
62 /* register 4e.b gets or'ed with one */
63 unsigned char old, new;
64 /* if it fails, it fails. There are so many variations of broken mobos
65 * that it is hard to argue that we should quit at this point.
66 */
Ollie Lho761bf1b2004-03-20 16:46:10 +000067
Ollie Lhocbbf1252004-03-17 22:22:08 +000068 old = pci_read_byte(dev, 0x4e);
69
70 new = old | 1;
71
72 if (new == old)
73 return 0;
74
75 pci_write_byte(dev, 0x4e, new);
76
77 if (pci_read_byte(dev, 0x4e) != new) {
Ollie Lho761bf1b2004-03-20 16:46:10 +000078 printf
79 ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
80 0x4e, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +000081 return -1;
82 }
83 return 0;
84}
85
86static int enable_flash_vt8235(struct pci_dev *dev, char *name)
87{
88 unsigned char old, new, val;
89 unsigned int base;
90 int ok;
Ollie Lho761bf1b2004-03-20 16:46:10 +000091
Ollie Lhocbbf1252004-03-17 22:22:08 +000092 /* get io privilege access PCI configuration space */
93 if (iopl(3) != 0) {
94 perror("Can not set io priviliage");
95 exit(1);
96 }
97
98 old = pci_read_byte(dev, 0x40);
99
100 new = old | 0x10;
101
102 if (new == old)
103 return 0;
104
105 ok = pci_write_byte(dev, 0x40, new);
106 if (ok != 0) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000107 printf
108 ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
109 old, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000110 }
111
112 /* enable GPIO15 which is connected to write protect. */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000113 base =
114 ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89)
115 << 8);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000116 val = inb(base + 0x4d);
117 val |= 0x80;
118 outb(val, base + 0x4d);
119
120 if (ok != 0) {
121 return -1;
122 } else {
123 return 0;
124 }
125}
126
127static int enable_flash_vt8231(struct pci_dev *dev, char *name)
128{
129 unsigned char val;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000130
Ollie Lhocbbf1252004-03-17 22:22:08 +0000131 val = pci_read_byte(dev, 0x40);
132 val |= 0x10;
133 pci_write_byte(dev, 0x40, val);
134
135 if (pci_read_byte(dev, 0x40) != val) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000136 printf
137 ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
138 0x40, val, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000139 return -1;
140 }
141 return 0;
142}
143
144static int enable_flash_cs5530(struct pci_dev *dev, char *name)
145{
146 unsigned char new;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000147
Ollie Lhocbbf1252004-03-17 22:22:08 +0000148 pci_write_byte(dev, 0x52, 0xee);
149
150 new = pci_read_byte(dev, 0x52);
151
152 if (new != 0xee) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000153 printf
154 ("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
155 0x52, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000156 return -1;
157 }
158 return 0;
159}
160
161static int enable_flash_sc1100(struct pci_dev *dev, char *name)
162{
163 unsigned char new;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000164
Ollie Lhocbbf1252004-03-17 22:22:08 +0000165 pci_write_byte(dev, 0x52, 0xee);
166
167 new = pci_read_byte(dev, 0x52);
168
169 if (new != 0xee) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000170 printf
171 ("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
172 0x52, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000173 return -1;
174 }
175 return 0;
176}
177
178static int enable_flash_sis5595(struct pci_dev *dev, char *name)
179{
180 unsigned char new, newer;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000181
Ollie Lhocbbf1252004-03-17 22:22:08 +0000182 new = pci_read_byte(dev, 0x45);
183
184 /* clear bit 5 */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000185 new &= (~0x20);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000186 /* set bit 2 */
187 new |= 0x4;
188
189 pci_write_byte(dev, 0x45, new);
190
191 newer = pci_read_byte(dev, 0x45);
192 if (newer != new) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000193 printf
194 ("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
195 0x45, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000196 printf("Stuck at 0x%x\n", newer);
197 return -1;
198 }
199 return 0;
200}
201
Ollie Lho761bf1b2004-03-20 16:46:10 +0000202static int enable_flash_amd8111(struct pci_dev *dev, char *name)
203{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000204 /* register 4e.b gets or'ed with one */
205 unsigned char old, new;
206 /* if it fails, it fails. There are so many variations of broken mobos
207 * that it is hard to argue that we should quit at this point.
208 */
209
210 //dump_pci_device(dev);
211
212 old = pci_read_byte(dev, 0x43);
213 new = old | 0x80;
214 if (new != old) {
215 pci_write_byte(dev, 0x43, new);
216 if (pci_read_byte(dev, 0x43) != new) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000217 printf
218 ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
219 0x43, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000220 }
221 }
222
Ollie Lho761bf1b2004-03-20 16:46:10 +0000223 old = pci_read_byte(dev, 0x40);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000224 new = old | 0x01;
225 if (new == old)
226 return 0;
227 pci_write_byte(dev, 0x40, new);
228
229 if (pci_read_byte(dev, 0x40) != new) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000230 printf
231 ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
232 0x40, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000233 return -1;
234 }
235 return 0;
236}
237
238typedef struct penable {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000239 unsigned short vendor, device;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000240 char *name;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000241 int (*doit) (struct pci_dev * dev, char *name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000242} FLASH_ENABLE;
243
244static FLASH_ENABLE enables[] = {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000245 {0x1039, 0x0630, "sis630", enable_flash_sis630},
246 {0x8086, 0x2480, "E7500", enable_flash_e7500},
247 {0x1106, 0x8231, "VT8231", enable_flash_vt8231},
248 {0x1106, 0x3177, "VT8235", enable_flash_vt8235},
249 {0x1078, 0x0100, "CS5530", enable_flash_cs5530},
250 {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
Ollie Lhocbbf1252004-03-17 22:22:08 +0000251 {0x1039, 0x0008, "SIS5595", enable_flash_sis5595},
252 {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
253};
Ollie Lho761bf1b2004-03-20 16:46:10 +0000254
Ollie Lhocbbf1252004-03-17 22:22:08 +0000255int enable_flash_write()
256{
257 int i;
258 struct pci_access *pacc;
259 struct pci_dev *dev = 0;
260 FLASH_ENABLE *enable = 0;
261
Ollie Lho761bf1b2004-03-20 16:46:10 +0000262 pacc = pci_alloc(); /* Get the pci_access structure */
Ollie Lhocbbf1252004-03-17 22:22:08 +0000263 /* Set all options you want -- here we stick with the defaults */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000264 pci_init(pacc); /* Initialize the PCI library */
265 pci_scan_bus(pacc); /* We want to get the list of devices */
Ollie Lhocbbf1252004-03-17 22:22:08 +0000266
267 /* now let's try to find the chipset we have ... */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000268 for (i = 0; i < sizeof(enables) / sizeof(enables[0]) && (!dev);
269 i++) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000270 struct pci_filter f;
271 struct pci_dev *z;
272 /* the first param is unused. */
273 pci_filter_init((struct pci_access *) 0, &f);
274 f.vendor = enables[i].vendor;
275 f.device = enables[i].device;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000276 for (z = pacc->devices; z; z = z->next)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000277 if (pci_filter_match(&f, z)) {
278 enable = &enables[i];
279 dev = z;
280 }
281 }
282
283 /* now do the deed. */
284 if (enable) {
285 printf("Enabling flash write on %s...", enable->name);
286 if (enable->doit(dev, enable->name) == 0)
287 printf("OK\n");
288 }
289 return 0;
290}