blob: 67819da875bf763c2f102bec59a7b612f041e7b6 [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
85static int enable_flash_vt8235(struct pci_dev *dev, char *name)
86{
87 unsigned char old, new, val;
88 unsigned int base;
89 int ok;
Ollie Lho761bf1b2004-03-20 16:46:10 +000090
Ollie Lhocbbf1252004-03-17 22:22:08 +000091 /* get io privilege access PCI configuration space */
92 if (iopl(3) != 0) {
93 perror("Can not set io priviliage");
94 exit(1);
95 }
96
97 old = pci_read_byte(dev, 0x40);
98
99 new = old | 0x10;
100
101 if (new == old)
102 return 0;
103
104 ok = pci_write_byte(dev, 0x40, new);
105 if (ok != 0) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000106 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
107 old, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000108 }
109
110 /* enable GPIO15 which is connected to write protect. */
Ollie Lho8b8897a2004-03-27 00:18:15 +0000111 base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000112 val = inb(base + 0x4d);
113 val |= 0x80;
114 outb(val, base + 0x4d);
115
116 if (ok != 0) {
117 return -1;
118 } else {
119 return 0;
120 }
121}
122
123static int enable_flash_vt8231(struct pci_dev *dev, char *name)
124{
125 unsigned char val;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000126
Ollie Lhocbbf1252004-03-17 22:22:08 +0000127 val = pci_read_byte(dev, 0x40);
128 val |= 0x10;
129 pci_write_byte(dev, 0x40, val);
130
131 if (pci_read_byte(dev, 0x40) != val) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000132 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
133 0x40, val, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000134 return -1;
135 }
136 return 0;
137}
138
139static int enable_flash_cs5530(struct pci_dev *dev, char *name)
140{
141 unsigned char new;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000142
Ollie Lhocbbf1252004-03-17 22:22:08 +0000143 pci_write_byte(dev, 0x52, 0xee);
144
145 new = pci_read_byte(dev, 0x52);
146
147 if (new != 0xee) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000148 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
149 0x52, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000150 return -1;
151 }
152 return 0;
153}
154
155static int enable_flash_sc1100(struct pci_dev *dev, char *name)
156{
157 unsigned char new;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000158
Ollie Lhocbbf1252004-03-17 22:22:08 +0000159 pci_write_byte(dev, 0x52, 0xee);
160
161 new = pci_read_byte(dev, 0x52);
162
163 if (new != 0xee) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000164 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
165 0x52, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000166 return -1;
167 }
168 return 0;
169}
170
171static int enable_flash_sis5595(struct pci_dev *dev, char *name)
172{
173 unsigned char new, newer;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000174
Ollie Lhocbbf1252004-03-17 22:22:08 +0000175 new = pci_read_byte(dev, 0x45);
176
177 /* clear bit 5 */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000178 new &= (~0x20);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000179 /* set bit 2 */
180 new |= 0x4;
181
182 pci_write_byte(dev, 0x45, new);
183
184 newer = pci_read_byte(dev, 0x45);
185 if (newer != new) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000186 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
187 0x45, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000188 printf("Stuck at 0x%x\n", newer);
189 return -1;
190 }
191 return 0;
192}
193
Ollie Lho761bf1b2004-03-20 16:46:10 +0000194static int enable_flash_amd8111(struct pci_dev *dev, char *name)
195{
Ollie Lhocbbf1252004-03-17 22:22:08 +0000196 /* register 4e.b gets or'ed with one */
197 unsigned char old, new;
198 /* if it fails, it fails. There are so many variations of broken mobos
199 * that it is hard to argue that we should quit at this point.
200 */
201
202 //dump_pci_device(dev);
203
204 old = pci_read_byte(dev, 0x43);
205 new = old | 0x80;
206 if (new != old) {
207 pci_write_byte(dev, 0x43, new);
208 if (pci_read_byte(dev, 0x43) != new) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000209 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
210 0x43, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000211 }
212 }
213
Ollie Lho761bf1b2004-03-20 16:46:10 +0000214 old = pci_read_byte(dev, 0x40);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000215 new = old | 0x01;
216 if (new == old)
217 return 0;
218 pci_write_byte(dev, 0x40, new);
219
220 if (pci_read_byte(dev, 0x40) != new) {
Ollie Lho8b8897a2004-03-27 00:18:15 +0000221 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
222 0x40, new, name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000223 return -1;
224 }
225 return 0;
226}
227
228typedef struct penable {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000229 unsigned short vendor, device;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000230 char *name;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000231 int (*doit) (struct pci_dev * dev, char *name);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000232} FLASH_ENABLE;
233
234static FLASH_ENABLE enables[] = {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000235 {0x1039, 0x0630, "sis630", enable_flash_sis630},
236 {0x8086, 0x2480, "E7500", enable_flash_e7500},
237 {0x1106, 0x8231, "VT8231", enable_flash_vt8231},
238 {0x1106, 0x3177, "VT8235", enable_flash_vt8235},
239 {0x1078, 0x0100, "CS5530", enable_flash_cs5530},
240 {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
Ollie Lhocbbf1252004-03-17 22:22:08 +0000241 {0x1039, 0x0008, "SIS5595", enable_flash_sis5595},
242 {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
243};
Ollie Lho761bf1b2004-03-20 16:46:10 +0000244
Ollie Lhocbbf1252004-03-17 22:22:08 +0000245int enable_flash_write()
246{
247 int i;
248 struct pci_access *pacc;
249 struct pci_dev *dev = 0;
250 FLASH_ENABLE *enable = 0;
251
Ollie Lho761bf1b2004-03-20 16:46:10 +0000252 pacc = pci_alloc(); /* Get the pci_access structure */
Ollie Lhocbbf1252004-03-17 22:22:08 +0000253 /* Set all options you want -- here we stick with the defaults */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000254 pci_init(pacc); /* Initialize the PCI library */
255 pci_scan_bus(pacc); /* We want to get the list of devices */
Ollie Lhocbbf1252004-03-17 22:22:08 +0000256
257 /* now let's try to find the chipset we have ... */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000258 for (i = 0; i < sizeof(enables) / sizeof(enables[0]) && (!dev);
259 i++) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000260 struct pci_filter f;
261 struct pci_dev *z;
262 /* the first param is unused. */
263 pci_filter_init((struct pci_access *) 0, &f);
264 f.vendor = enables[i].vendor;
265 f.device = enables[i].device;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000266 for (z = pacc->devices; z; z = z->next)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000267 if (pci_filter_match(&f, z)) {
268 enable = &enables[i];
269 dev = z;
270 }
271 }
272
273 /* now do the deed. */
274 if (enable) {
275 printf("Enabling flash write on %s...", enable->name);
276 if (enable->doit(dev, enable->name) == 0)
277 printf("OK\n");
278 }
279 return 0;
280}