Drop a bunch of useless header files, merge them into flash.h.
[coreboot.git] / util / flashrom / chipset_enable.c
1 /*
2  *   flash rom utility: enable flash writes
3  *
4  *   Copyright (C) 2000 Silicon Integrated System Corporation
5  *   Copyright (C) 2005-2007 coresystems GmbH <stepan@coresystems.de>
6  *   Copyright (C) 2006 Uwe Hermann <uwe@hermann-uwe.de>
7  *
8  *   This program is free software; you can redistribute it and/or
9  *   modify it under the terms of the GNU General Public License
10  *   version 2
11  *
12  */
13
14 #include <stdio.h>
15 #include <pci/pci.h>
16 #include <stdlib.h>
17 #include "flash.h"
18
19 static int enable_flash_ali_m1533(struct pci_dev *dev, char *name)
20 {
21         uint8_t tmp;
22
23         /* ROM Write enable, 0xFFFC0000-0xFFFDFFFF and
24            0xFFFE0000-0xFFFFFFFF ROM select enable. */
25         tmp = pci_read_byte(dev, 0x47);
26         tmp |= 0x46;
27         pci_write_byte(dev, 0x47, tmp);
28
29         return 0;
30 }
31
32 static int enable_flash_sis630(struct pci_dev *dev, char *name)
33 {
34         char b;
35
36         /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */
37         outl(0x80000840, 0x0cf8);
38         b = inb(0x0cfc) | 0x0b;
39         outb(b, 0xcfc);
40         /* Flash write enable on SiS 540/630 */
41         outl(0x80000845, 0x0cf8);
42         b = inb(0x0cfd) | 0x40;
43         outb(b, 0xcfd);
44
45         /* The same thing on SiS 950 SuperIO side */
46         outb(0x87, 0x2e);
47         outb(0x01, 0x2e);
48         outb(0x55, 0x2e);
49         outb(0x55, 0x2e);
50
51         if (inb(0x2f) != 0x87) {
52                 outb(0x87, 0x4e);
53                 outb(0x01, 0x4e);
54                 outb(0x55, 0x4e);
55                 outb(0xaa, 0x4e);
56                 if (inb(0x4f) != 0x87) {
57                         printf("Can not access SiS 950\n");
58                         return -1;
59                 }
60                 outb(0x24, 0x4e);
61                 b = inb(0x4f) | 0xfc;
62                 outb(0x24, 0x4e);
63                 outb(b, 0x4f);
64                 outb(0x02, 0x4e);
65                 outb(0x02, 0x4f);
66         }
67
68         outb(0x24, 0x2e);
69         printf("2f is %#x\n", inb(0x2f));
70         b = inb(0x2f) | 0xfc;
71         outb(0x24, 0x2e);
72         outb(b, 0x2f);
73
74         outb(0x02, 0x2e);
75         outb(0x02, 0x2f);
76
77         return 0;
78 }
79
80 /* Datasheet:
81  *   - Name: 82371AB PCI-TO-ISA / IDE XCELERATOR (PIIX4)
82  *   - URL: http://www.intel.com/design/intarch/datashts/290562.htm
83  *   - PDF: http://www.intel.com/design/intarch/datashts/29056201.pdf
84  *   - Order Number: 290562-001
85  */
86 static int enable_flash_piix4(struct pci_dev *dev, char *name)
87 {
88         uint16_t old, new;
89         uint16_t xbcs = 0x4e;   /* X-Bus Chip Select register. */
90
91         old = pci_read_word(dev, xbcs);
92
93         /* Set bit 9: 1-Meg Extended BIOS Enable (PCI master accesses to
94          *            FFF00000-FFF7FFFF are forwarded to ISA).
95          * Set bit 7: Extended BIOS Enable (PCI master accesses to
96          *            FFF80000-FFFDFFFF are forwarded to ISA).
97          * Set bit 6: Lower BIOS Enable (PCI master, or ISA master accesses to
98          *            the lower 64-Kbyte BIOS block (E0000-EFFFF) at the top
99          *            of 1 Mbyte, or the aliases at the top of 4 Gbyte
100          *            (FFFE0000-FFFEFFFF) result in the generation of BIOSCS#.
101          * Note: Accesses to FFFF0000-FFFFFFFF are always forwarded to ISA.
102          * Set bit 2: BIOSCS# Write Enable (1=enable, 0=disable).
103          */
104         new = old | 0x2c4;
105
106         if (new == old)
107                 return 0;
108
109         pci_write_word(dev, xbcs, new);
110
111         if (pci_read_word(dev, xbcs) != new) {
112                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", xbcs, new, name);
113                 return -1;
114         }
115         return 0;
116 }
117
118 static int enable_flash_ich(struct pci_dev *dev, char *name, int bios_cntl)
119 {
120         /* register 4e.b gets or'ed with one */
121         uint8_t old, new;
122
123         /* if it fails, it fails. There are so many variations of broken mobos
124          * that it is hard to argue that we should quit at this point.
125          */
126
127         /* Note: the ICH0-ICH5 BIOS_CNTL register is actually 16 bit wide, but
128          * just treating it as 8 bit wide seems to work fine in practice.
129          */
130
131         /* see ie. page 375 of "Intel ICH7 External Design Specification"
132          * http://download.intel.com/design/chipsets/datashts/30701302.pdf
133          */
134
135         old = pci_read_byte(dev, bios_cntl);
136
137         new = old | 1;
138
139         if (new == old)
140                 return 0;
141
142         pci_write_byte(dev, bios_cntl, new);
143
144         if (pci_read_byte(dev, bios_cntl) != new) {
145                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", bios_cntl, new, name);
146                 return -1;
147         }
148         return 0;
149 }
150
151 static int enable_flash_ich_4e(struct pci_dev *dev, char *name)
152 {
153         return enable_flash_ich(dev, name, 0x4e);
154 }
155
156 static int enable_flash_ich_dc(struct pci_dev *dev, char *name)
157 {
158         return enable_flash_ich(dev, name, 0xdc);
159 }
160
161 /*
162  *
163  */
164 static int enable_flash_vt823x(struct pci_dev *dev, char *name)
165 {
166         uint8_t val;
167
168         /* ROM Write enable */
169         val = pci_read_byte(dev, 0x40);
170         val |= 0x10;
171         pci_write_byte(dev, 0x40, val);
172
173         if (pci_read_byte(dev, 0x40) != val) {
174                 printf("\nWARNING: Failed to enable ROM Write on \"%s\"\n",
175                        name);
176                 return -1;
177         }
178
179         return 0;
180 }
181
182 static int enable_flash_cs5530(struct pci_dev *dev, char *name)
183 {
184         uint8_t reg8;
185
186         #define DECODE_CONTROL_REG2             0x5b    /* F0 index 0x5b */
187         #define ROM_AT_LOGIC_CONTROL_REG        0x52    /* F0 index 0x52 */
188
189         #define LOWER_ROM_ADDRESS_RANGE         (1 << 0)
190         #define ROM_WRITE_ENABLE                (1 << 1)
191         #define UPPER_ROM_ADDRESS_RANGE         (1 << 2)
192         #define BIOS_ROM_POSITIVE_DECODE        (1 << 5)
193
194         /* Decode 0x000E0000-0x000FFFFF (128 KB), not just 64 KB, and
195          * decode 0xFF000000-0xFFFFFFFF (16 MB), not just 256 KB.
196          * Make the configured ROM areas writable.
197          */
198         reg8 = pci_read_byte(dev, ROM_AT_LOGIC_CONTROL_REG);
199         reg8 |= LOWER_ROM_ADDRESS_RANGE;
200         reg8 |= UPPER_ROM_ADDRESS_RANGE;
201         reg8 |= ROM_WRITE_ENABLE;
202         pci_write_byte(dev, ROM_AT_LOGIC_CONTROL_REG, reg8);
203
204         /* Set positive decode on ROM. */
205         reg8 = pci_read_byte(dev, DECODE_CONTROL_REG2);
206         reg8 |= BIOS_ROM_POSITIVE_DECODE;
207         pci_write_byte(dev, DECODE_CONTROL_REG2, reg8);
208
209         return 0;
210 }
211
212 static int enable_flash_sc1100(struct pci_dev *dev, char *name)
213 {
214         uint8_t new;
215
216         pci_write_byte(dev, 0x52, 0xee);
217
218         new = pci_read_byte(dev, 0x52);
219
220         if (new != 0xee) {
221                 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x52, new, name);
222                 return -1;
223         }
224         return 0;
225 }
226
227 static int enable_flash_sis5595(struct pci_dev *dev, char *name)
228 {
229         uint8_t new, newer;
230
231         new = pci_read_byte(dev, 0x45);
232
233         /* clear bit 5 */
234         new &= (~0x20);
235         /* set bit 2 */
236         new |= 0x4;
237
238         pci_write_byte(dev, 0x45, new);
239
240         newer = pci_read_byte(dev, 0x45);
241         if (newer != new) {
242                 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x45, new, name);
243                 printf("Stuck at 0x%x\n", newer);
244                 return -1;
245         }
246         return 0;
247 }
248
249 static int enable_flash_amd8111(struct pci_dev *dev, char *name)
250 {
251         /* register 4e.b gets or'ed with one */
252         uint8_t old, new;
253
254         /* if it fails, it fails. There are so many variations of broken mobos
255          * that it is hard to argue that we should quit at this point.
256          */
257
258         /* enable decoding at 0xffb00000 to 0xffffffff */
259         old = pci_read_byte(dev, 0x43);
260         new = old | 0xC0;
261         if (new != old) {
262                 pci_write_byte(dev, 0x43, new);
263                 if (pci_read_byte(dev, 0x43) != new) {
264                         printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x43, new, name);
265                 }
266         }
267
268         old = pci_read_byte(dev, 0x40);
269         new = old | 0x01;
270         if (new == old)
271                 return 0;
272         pci_write_byte(dev, 0x40, new);
273
274         if (pci_read_byte(dev, 0x40) != new) {
275                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x40, new, name);
276                 return -1;
277         }
278         return 0;
279 }
280
281 static int enable_flash_ck804(struct pci_dev *dev, char *name)
282 {
283         /* register 4e.b gets or'ed with one */
284         uint8_t old, new;
285
286         /* if it fails, it fails. There are so many variations of broken mobos
287          * that it is hard to argue that we should quit at this point.
288          */
289
290         /* dump_pci_device(dev); */
291
292         old = pci_read_byte(dev, 0x88);
293         new = old | 0xc0;
294         if (new != old) {
295                 pci_write_byte(dev, 0x88, new);
296                 if (pci_read_byte(dev, 0x88) != new) {
297                         printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x88, new, name);
298                 }
299         }
300
301         old = pci_read_byte(dev, 0x6d);
302         new = old | 0x01;
303         if (new == old)
304                 return 0;
305         pci_write_byte(dev, 0x6d, new);
306
307         if (pci_read_byte(dev, 0x6d) != new) {
308                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x6d, new, name);
309                 return -1;
310         }
311         return 0;
312 }
313
314 static int enable_flash_sb400(struct pci_dev *dev, char *name)
315 {
316         uint8_t tmp;
317
318         struct pci_filter f;
319         struct pci_dev *smbusdev;
320
321         /* then look for the smbus device */
322         pci_filter_init((struct pci_access *)0, &f);
323         f.vendor = 0x1002;
324         f.device = 0x4372;
325
326         for (smbusdev = pacc->devices; smbusdev; smbusdev = smbusdev->next) {
327                 if (pci_filter_match(&f, smbusdev)) {
328                         break;
329                 }
330         }
331
332         if (!smbusdev) {
333                 fprintf(stderr, "ERROR: SMBus device not found. aborting\n");
334                 exit(1);
335         }
336
337         /* enable some smbus stuff */
338         tmp = pci_read_byte(smbusdev, 0x79);
339         tmp |= 0x01;
340         pci_write_byte(smbusdev, 0x79, tmp);
341
342         /* change southbridge */
343         tmp = pci_read_byte(dev, 0x48);
344         tmp |= 0x21;
345         pci_write_byte(dev, 0x48, tmp);
346
347         /* now become a bit silly. */
348         tmp = inb(0xc6f);
349         outb(tmp, 0xeb);
350         outb(tmp, 0xeb);
351         tmp |= 0x40;
352         outb(tmp, 0xc6f);
353         outb(tmp, 0xeb);
354         outb(tmp, 0xeb);
355
356         return 0;
357 }
358
359 static int enable_flash_mcp55(struct pci_dev *dev, char *name)
360 {
361         /* register 4e.b gets or'ed with one */
362         unsigned char old, new, byte;
363         unsigned short word;
364
365         /* if it fails, it fails. There are so many variations of broken mobos
366          * that it is hard to argue that we should quit at this point.
367          */
368
369         /* dump_pci_device(dev); */
370
371         /* Set the 4MB enable bit bit */
372         byte = pci_read_byte(dev, 0x88);
373         byte |= 0xff;           /* 256K */
374         pci_write_byte(dev, 0x88, byte);
375         byte = pci_read_byte(dev, 0x8c);
376         byte |= 0xff;           /* 1M */
377         pci_write_byte(dev, 0x8c, byte);
378         word = pci_read_word(dev, 0x90);
379         word |= 0x7fff;         /* 15M */
380         pci_write_word(dev, 0x90, word);
381
382         old = pci_read_byte(dev, 0x6d);
383         new = old | 0x01;
384         if (new == old)
385                 return 0;
386         pci_write_byte(dev, 0x6d, new);
387
388         if (pci_read_byte(dev, 0x6d) != new) {
389                 printf
390                     ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
391                      0x6d, new, name);
392                 return -1;
393         }
394
395         return 0;
396
397 }
398
399 static int enable_flash_ht1000(struct pci_dev *dev, char *name)
400 {
401         uint8_t byte;
402
403         /* Set the 4MB enable bit. */
404         byte = pci_read_byte(dev, 0x41);
405         byte |= 0x0e;
406         pci_write_byte(dev, 0x41, byte);
407
408         byte = pci_read_byte(dev, 0x43);
409         byte |= (1<<4);
410         pci_write_byte(dev, 0x43, byte);
411
412         return 0;
413 }
414
415 typedef struct penable {
416         unsigned short vendor, device;
417         char *name;
418         int (*doit) (struct pci_dev * dev, char *name);
419 } FLASH_ENABLE;
420
421 static FLASH_ENABLE enables[] = {
422         {0x1039, 0x0630, "SIS630", enable_flash_sis630},
423         {0x8086, 0x7110, "PIIX4/PIIX4E/PIIX4M", enable_flash_piix4},
424         {0x8086, 0x2410, "ICH", enable_flash_ich_4e},
425         {0x8086, 0x2420, "ICH0", enable_flash_ich_4e},
426         {0x8086, 0x2440, "ICH2", enable_flash_ich_4e},
427         {0x8086, 0x244c, "ICH2-M", enable_flash_ich_4e},
428         {0x8086, 0x2480, "ICH3-S", enable_flash_ich_4e},
429         {0x8086, 0x248c, "ICH3-M", enable_flash_ich_4e},
430         {0x8086, 0x24c0, "ICH4/ICH4-L", enable_flash_ich_4e},
431         {0x8086, 0x24cc, "ICH4-M", enable_flash_ich_4e},
432         {0x8086, 0x24d0, "ICH5/ICH5R", enable_flash_ich_4e},
433         {0x8086, 0x2640, "ICH6/ICH6R", enable_flash_ich_dc},
434         {0x8086, 0x2641, "ICH6-M", enable_flash_ich_dc},
435         {0x8086, 0x27b0, "ICH7DH", enable_flash_ich_dc},
436         {0x8086, 0x27b8, "ICH7/ICH7R", enable_flash_ich_dc},
437         {0x8086, 0x27b9, "ICH7M", enable_flash_ich_dc},
438         {0x8086, 0x27bd, "ICH7MDH", enable_flash_ich_dc},
439         {0x8086, 0x2810, "ICH8/ICH8R", enable_flash_ich_dc},
440         {0x8086, 0x2812, "ICH8DH", enable_flash_ich_dc},
441         {0x8086, 0x2814, "ICH8DO", enable_flash_ich_dc},
442         {0x1106, 0x8231, "VT8231", enable_flash_vt823x},
443         {0x1106, 0x3177, "VT8235", enable_flash_vt823x},
444         {0x1106, 0x3227, "VT8237", enable_flash_vt823x},
445         {0x1106, 0x8324, "CX700", enable_flash_vt823x},
446         {0x1106, 0x0686, "VT82C686", enable_flash_amd8111},
447         {0x1078, 0x0100, "CS5530/CS5530A", enable_flash_cs5530},
448         {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
449         {0x1039, 0x0008, "SIS5595", enable_flash_sis5595},
450         {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
451         {0x10B9, 0x1533, "ALi M1533", enable_flash_ali_m1533},
452         /* this fallthrough looks broken. */
453         {0x10de, 0x0050, "NVIDIA CK804", enable_flash_ck804},   /* LPC */
454         {0x10de, 0x0051, "NVIDIA CK804", enable_flash_ck804},   /* Pro */
455         {0x10de, 0x00d3, "NVIDIA CK804", enable_flash_ck804},   /* Slave, should not be here, to fix known bug for A01. */
456
457         {0x10de, 0x0260, "NVidia MCP51", enable_flash_ck804},
458         {0x10de, 0x0261, "NVidia MCP51", enable_flash_ck804},
459         {0x10de, 0x0262, "NVidia MCP51", enable_flash_ck804},
460         {0x10de, 0x0263, "NVidia MCP51", enable_flash_ck804},
461
462         {0x10de, 0x0360, "NVIDIA MCP55", enable_flash_mcp55},   /* Gigabyte m57sli-s4 */
463         {0x10de, 0x0361, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
464         {0x10de, 0x0362, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
465         {0x10de, 0x0363, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
466         {0x10de, 0x0364, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
467         {0x10de, 0x0365, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
468         {0x10de, 0x0366, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
469         {0x10de, 0x0367, "NVIDIA MCP55", enable_flash_mcp55},   /* Pro */
470
471         {0x1002, 0x4377, "ATI SB400", enable_flash_sb400},      /* ATI Technologies Inc IXP SB400 PCI-ISA Bridge (rev 80) */
472
473         {0x1166, 0x0205, "Broadcom HT-1000", enable_flash_ht1000},
474 };
475
476 /*
477  *
478  */
479 int chipset_flash_enable(void)
480 {
481         struct pci_dev *dev = 0;
482         int ret = -2;           /* nothing! */
483         int i;
484
485         /* now let's try to find the chipset we have ... */
486         for (i = 0; i < sizeof(enables) / sizeof(enables[0]); i++) {
487                 dev = pci_dev_find(enables[i].vendor, enables[i].device);
488                 if (dev)
489                         break;
490         }
491
492         if (dev) {
493                 printf("Found chipset \"%s\": Enabling flash write... ",
494                        enables[i].name);
495
496                 ret = enables[i].doit(dev, enables[i].name);
497                 if (ret)
498                         printf("Failed!\n");
499                 else
500                         printf("OK.\n");
501         }
502
503         return ret;
504 }