blob: 8a9bf042c7edbc39ed4139a2371dcdf9030c8c4b [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
6static int enable_flash_sis630 (struct pci_dev *dev, char *name)
7{
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);
45 outb(0x02, 0x4f);
46 }
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 */
67
68 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) {
78 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
79 0x4e, new, name);
80 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;
90
91 /* 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) {
106 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
107 old, new, name);
108 }
109
110 /* enable GPIO15 which is connected to write protect. */
111 base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8);
112 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;
126
127 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) {
132 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
133 0x40, val, name);
134 return -1;
135 }
136 return 0;
137}
138
139static int enable_flash_cs5530(struct pci_dev *dev, char *name)
140{
141 unsigned char new;
142
143 pci_write_byte(dev, 0x52, 0xee);
144
145 new = pci_read_byte(dev, 0x52);
146
147 if (new != 0xee) {
148 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
149 0x52, new, name);
150 return -1;
151 }
152 return 0;
153}
154
155static int enable_flash_sc1100(struct pci_dev *dev, char *name)
156{
157 unsigned char new;
158
159 pci_write_byte(dev, 0x52, 0xee);
160
161 new = pci_read_byte(dev, 0x52);
162
163 if (new != 0xee) {
164 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
165 0x52, new, name);
166 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;
174
175 new = pci_read_byte(dev, 0x45);
176
177 /* clear bit 5 */
178 new &= (~ 0x20);
179 /* 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) {
186 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
187 0x45, new, name);
188 printf("Stuck at 0x%x\n", newer);
189 return -1;
190 }
191 return 0;
192}
193
194static int enable_flash_amd8111(struct pci_dev *dev, char *name) {
195 /* register 4e.b gets or'ed with one */
196 unsigned char old, new;
197 /* if it fails, it fails. There are so many variations of broken mobos
198 * that it is hard to argue that we should quit at this point.
199 */
200
201 //dump_pci_device(dev);
202
203 old = pci_read_byte(dev, 0x43);
204 new = old | 0x80;
205 if (new != old) {
206 pci_write_byte(dev, 0x43, new);
207 if (pci_read_byte(dev, 0x43) != new) {
208 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
209 0x43, new, name);
210 }
211 }
212
213 old = pci_read_byte(dev, 0x40);
214 new = old | 0x01;
215 if (new == old)
216 return 0;
217 pci_write_byte(dev, 0x40, new);
218
219 if (pci_read_byte(dev, 0x40) != new) {
220 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
221 0x40, new, name);
222 return -1;
223 }
224 return 0;
225}
226
227typedef struct penable {
228 unsigned short vendor, device;
229 char *name;
230 int (*doit)(struct pci_dev *dev, char *name);
231} FLASH_ENABLE;
232
233static FLASH_ENABLE enables[] = {
234 {0x1039, 0x0630, "sis630", enable_flash_sis630},
235 {0x8086, 0x2480, "E7500", enable_flash_e7500},
236 {0x1106, 0x8231, "VT8231", enable_flash_vt8231},
237 {0x1106, 0x3177, "VT8235", enable_flash_vt8235},
238 {0x1078, 0x0100, "CS5530", enable_flash_cs5530},
239 {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
240 {0x1039, 0x0008, "SIS5595", enable_flash_sis5595},
241 {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
242};
243
244int enable_flash_write()
245{
246 int i;
247 struct pci_access *pacc;
248 struct pci_dev *dev = 0;
249 FLASH_ENABLE *enable = 0;
250
251 pacc = pci_alloc(); /* Get the pci_access structure */
252 /* Set all options you want -- here we stick with the defaults */
253 pci_init(pacc); /* Initialize the PCI library */
254 pci_scan_bus(pacc); /* We want to get the list of devices */
255
256 /* now let's try to find the chipset we have ... */
257 for (i = 0; i < sizeof(enables)/sizeof(enables[0]) && (! dev); i++) {
258 struct pci_filter f;
259 struct pci_dev *z;
260 /* the first param is unused. */
261 pci_filter_init((struct pci_access *) 0, &f);
262 f.vendor = enables[i].vendor;
263 f.device = enables[i].device;
264 for (z=pacc->devices; z; z=z->next)
265 if (pci_filter_match(&f, z)) {
266 enable = &enables[i];
267 dev = z;
268 }
269 }
270
271 /* now do the deed. */
272 if (enable) {
273 printf("Enabling flash write on %s...", enable->name);
274 if (enable->doit(dev, enable->name) == 0)
275 printf("OK\n");
276 }
277 return 0;
278}