blob: 19fd9b07eb7536c0b898e7cdc42f6069187aee6b [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 Lho8b8897a2004-03-27 00:18:15 +000078 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
79 0x4e, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +000080 return -1;
81 }
82 return 0;
83}
84
Ronald G. Minnich6a967412004-09-28 20:09:06 +000085static int enable_flash_ich4(struct pci_dev *dev, char *name)
86{
87 /* register 4e.b gets or'ed with one */
88 unsigned char old, new;
89 /* if it fails, it fails. There are so many variations of broken mobos
90 * that it is hard to argue that we should quit at this point.
91 */
92
93 old = pci_read_byte(dev, 0x4e);
94
95 new = old | 1;
96
97 if (new == old)
98 return 0;
99
100 pci_write_byte(dev, 0x4e, new);
101
102 if (pci_read_byte(dev, 0x4e) != new) {
103 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
104 0x4e, new, name);
105 return -1;
106 }
107 return 0;
108}
109
Ollie Lhocbbf1252004-03-17 22:22:08 +0000110static int enable_flash_vt8235(struct pci_dev *dev, char *name)
111{
112 unsigned char old, new, val;
113 unsigned int base;
114 int ok;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000115
Ollie Lhocbbf1252004-03-17 22:22:08 +0000116 /* get io privilege access PCI configuration space */
117 if (iopl(3) != 0) {
118 perror("Can not set io priviliage");
119 exit(1);
120 }
121
122 old = pci_read_byte(dev, 0x40);
123
124 new = old | 0x10;
125
126 if (new == old)
127 return 0;
128
129 ok = pci_write_byte(dev, 0x40, new);
130 if (ok != 0) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000131 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
132 old, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000133 }
134
135 /* enable GPIO15 which is connected to write protect. */
Ollie Lho8b8897a2004-03-27 00:18:15 +0000136 base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000137 val = inb(base + 0x4d);
138 val |= 0x80;
139 outb(val, base + 0x4d);
140
141 if (ok != 0) {
142 return -1;
143 } else {
144 return 0;
145 }
146}
147
148static int enable_flash_vt8231(struct pci_dev *dev, char *name)
149{
150 unsigned char val;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000151
Ollie Lhocbbf1252004-03-17 22:22:08 +0000152 val = pci_read_byte(dev, 0x40);
153 val |= 0x10;
154 pci_write_byte(dev, 0x40, val);
155
156 if (pci_read_byte(dev, 0x40) != val) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000157 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
158 0x40, val, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000159 return -1;
160 }
161 return 0;
162}
163
164static int enable_flash_cs5530(struct pci_dev *dev, char *name)
165{
166 unsigned char new;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000167
Ollie Lhocbbf1252004-03-17 22:22:08 +0000168 pci_write_byte(dev, 0x52, 0xee);
169
170 new = pci_read_byte(dev, 0x52);
171
172 if (new != 0xee) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000173 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
174 0x52, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000175 return -1;
176 }
177 return 0;
178}
179
180static int enable_flash_sc1100(struct pci_dev *dev, char *name)
181{
182 unsigned char new;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000183
Ollie Lhocbbf1252004-03-17 22:22:08 +0000184 pci_write_byte(dev, 0x52, 0xee);
185
186 new = pci_read_byte(dev, 0x52);
187
188 if (new != 0xee) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000189 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
190 0x52, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000191 return -1;
192 }
193 return 0;
194}
195
196static int enable_flash_sis5595(struct pci_dev *dev, char *name)
197{
198 unsigned char new, newer;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000199
Ollie Lhocbbf1252004-03-17 22:22:08 +0000200 new = pci_read_byte(dev, 0x45);
201
202 /* clear bit 5 */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000203 new &= (~0x20);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000204 /* set bit 2 */
205 new |= 0x4;
206
207 pci_write_byte(dev, 0x45, new);
208
209 newer = pci_read_byte(dev, 0x45);
210 if (newer != new) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000211 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
212 0x45, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000213 printf("Stuck at 0x%x\n", newer);
214 return -1;
215 }
216 return 0;
217}
218
Ollie Lho761bf1b2004-03-20 16:46:10 +0000219static int enable_flash_amd8111(struct pci_dev *dev, char *name)
220{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000221 /* register 4e.b gets or'ed with one */
222 unsigned char old, new;
223 /* if it fails, it fails. There are so many variations of broken mobos
224 * that it is hard to argue that we should quit at this point.
225 */
226
Ollie Lhod11f3612004-12-07 17:19:04 +0000227 /* enable decoding at 0xffb00000 to 0xffffffff */
Ollie Lhocbbf1252004-03-17 22:22:08 +0000228 old = pci_read_byte(dev, 0x43);
Ollie Lhod11f3612004-12-07 17:19:04 +0000229 new = old | 0xC0;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000230 if (new != old) {
231 pci_write_byte(dev, 0x43, new);
232 if (pci_read_byte(dev, 0x43) != new) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000233 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
234 0x43, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000235 }
236 }
237
Ollie Lho761bf1b2004-03-20 16:46:10 +0000238 old = pci_read_byte(dev, 0x40);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000239 new = old | 0x01;
240 if (new == old)
241 return 0;
242 pci_write_byte(dev, 0x40, new);
243
244 if (pci_read_byte(dev, 0x40) != new) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000245 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
246 0x40, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000247 return -1;
248 }
249 return 0;
250}
251
Yinghai Lu952dfce2005-07-06 17:13:46 +0000252//By yhlu
253static int enable_flash_ck804(struct pci_dev *dev, char *name)
254{
255 /* register 4e.b gets or'ed with one */
256 unsigned char old, new;
257 /* if it fails, it fails. There are so many variations of broken mobos
258 * that it is hard to argue that we should quit at this point.
259 */
260
261 //dump_pci_device(dev);
262
263 old = pci_read_byte(dev, 0x88);
264 new = old | 0xc0;
265 if (new != old) {
266 pci_write_byte(dev, 0x88, new);
267 if (pci_read_byte(dev, 0x88) != new) {
268 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
269 0x88, new, name);
270 }
271 }
272
273 old = pci_read_byte(dev, 0x6d);
274 new = old | 0x01;
275 if (new == old)
276 return 0;
277 pci_write_byte(dev, 0x6d, new);
278
279 if (pci_read_byte(dev, 0x6d) != new) {
280 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
281 0x6d, new, name);
282 return -1;
283 }
284 return 0;
285}
286
Ollie Lhocbbf1252004-03-17 22:22:08 +0000287typedef struct penable {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000288 unsigned short vendor, device;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000289 char *name;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000290 int (*doit) (struct pci_dev * dev, char *name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000291} FLASH_ENABLE;
292
293static FLASH_ENABLE enables[] = {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000294 {0x1039, 0x0630, "sis630", enable_flash_sis630},
295 {0x8086, 0x2480, "E7500", enable_flash_e7500},
Ronald G. Minnich6a967412004-09-28 20:09:06 +0000296 {0x8086, 0x24c0, "ICH4", enable_flash_ich4},
Ollie Lho761bf1b2004-03-20 16:46:10 +0000297 {0x1106, 0x8231, "VT8231", enable_flash_vt8231},
298 {0x1106, 0x3177, "VT8235", enable_flash_vt8235},
299 {0x1078, 0x0100, "CS5530", enable_flash_cs5530},
300 {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
Ollie Lhocbbf1252004-03-17 22:22:08 +0000301 {0x1039, 0x0008, "SIS5595", enable_flash_sis5595},
302 {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
Yinghai Lu952dfce2005-07-06 17:13:46 +0000303 {0x10de, 0x0050, "NVIDIA CK804", enable_flash_ck804}, // LPC
304 {0x10de, 0x0051, "NVIDIA CK804", enable_flash_ck804}, // Pro
305 {0x10de, 0x00d3, "NVIDIA CK804", enable_flash_ck804}, // Slave, should not be here, to fix known bug for A01.
Ollie Lhocbbf1252004-03-17 22:22:08 +0000306};
Ollie Lho761bf1b2004-03-20 16:46:10 +0000307
Ollie Lhocbbf1252004-03-17 22:22:08 +0000308int enable_flash_write()
309{
310 int i;
311 struct pci_access *pacc;
312 struct pci_dev *dev = 0;
313 FLASH_ENABLE *enable = 0;
314
Ollie Lho761bf1b2004-03-20 16:46:10 +0000315 pacc = pci_alloc(); /* Get the pci_access structure */
Ollie Lhocbbf1252004-03-17 22:22:08 +0000316 /* Set all options you want -- here we stick with the defaults */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000317 pci_init(pacc); /* Initialize the PCI library */
318 pci_scan_bus(pacc); /* We want to get the list of devices */
Ollie Lhocbbf1252004-03-17 22:22:08 +0000319
320 /* now let's try to find the chipset we have ... */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000321 for (i = 0; i < sizeof(enables) / sizeof(enables[0]) && (!dev);
322 i++) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000323 struct pci_filter f;
324 struct pci_dev *z;
325 /* the first param is unused. */
326 pci_filter_init((struct pci_access *) 0, &f);
327 f.vendor = enables[i].vendor;
328 f.device = enables[i].device;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000329 for (z = pacc->devices; z; z = z->next)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000330 if (pci_filter_match(&f, z)) {
331 enable = &enables[i];
332 dev = z;
333 }
334 }
335
336 /* now do the deed. */
337 if (enable) {
338 printf("Enabling flash write on %s...", enable->name);
339 if (enable->doit(dev, enable->name) == 0)
340 printf("OK\n");
341 }
342 return 0;
343}