Correctly disable the ROM area Write Protect bit in the Geode LX.
[coreboot.git] / util / flashrom / chipset_enable.c
1 /*
2  * This file is part of the flashrom project.
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 modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 /*
23  * Contains the chipset specific flash enables.
24  */
25
26 #define _LARGEFILE64_SOURCE
27
28 #include <stdio.h>
29 #include <pci/pci.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include "flash.h"
36
37 static int enable_flash_ali_m1533(struct pci_dev *dev, const char *name)
38 {
39         uint8_t tmp;
40
41         /*
42          * ROM Write enable, 0xFFFC0000-0xFFFDFFFF and
43          * 0xFFFE0000-0xFFFFFFFF ROM select enable.
44          */
45         tmp = pci_read_byte(dev, 0x47);
46         tmp |= 0x46;
47         pci_write_byte(dev, 0x47, tmp);
48
49         return 0;
50 }
51
52 static int enable_flash_sis630(struct pci_dev *dev, const char *name)
53 {
54         uint8_t b;
55
56         /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630. */
57         b = pci_read_byte(dev, 0x40);
58         pci_write_byte(dev, 0x40, b | 0xb);
59
60         /* Flash write enable on SiS 540/630. */
61         b = pci_read_byte(dev, 0x45);
62         pci_write_byte(dev, 0x45, b | 0x40);
63
64         /* The same thing on SiS 950 Super I/O side... */
65
66         /* First probe for Super I/O on config port 0x2e. */
67         outb(0x87, 0x2e);
68         outb(0x01, 0x2e);
69         outb(0x55, 0x2e);
70         outb(0x55, 0x2e);
71
72         if (inb(0x2f) != 0x87) {
73                 /* If that failed, try config port 0x4e. */
74                 outb(0x87, 0x4e);
75                 outb(0x01, 0x4e);
76                 outb(0x55, 0x4e);
77                 outb(0xaa, 0x4e);
78                 if (inb(0x4f) != 0x87) {
79                         printf("Can not access SiS 950\n");
80                         return -1;
81                 }
82                 outb(0x24, 0x4e);
83                 b = inb(0x4f) | 0xfc;
84                 outb(0x24, 0x4e);
85                 outb(b, 0x4f);
86                 outb(0x02, 0x4e);
87                 outb(0x02, 0x4f);
88         }
89
90         outb(0x24, 0x2e);
91         printf("2f is %#x\n", inb(0x2f));
92         b = inb(0x2f) | 0xfc;
93         outb(0x24, 0x2e);
94         outb(b, 0x2f);
95
96         outb(0x02, 0x2e);
97         outb(0x02, 0x2f);
98
99         return 0;
100 }
101
102 /* Datasheet:
103  *   - Name: 82371AB PCI-TO-ISA / IDE XCELERATOR (PIIX4)
104  *   - URL: http://www.intel.com/design/intarch/datashts/290562.htm
105  *   - PDF: http://www.intel.com/design/intarch/datashts/29056201.pdf
106  *   - Order Number: 290562-001
107  */
108 static int enable_flash_piix4(struct pci_dev *dev, const char *name)
109 {
110         uint16_t old, new;
111         uint16_t xbcs = 0x4e;   /* X-Bus Chip Select register. */
112
113         old = pci_read_word(dev, xbcs);
114
115         /* Set bit 9: 1-Meg Extended BIOS Enable (PCI master accesses to
116          *            FFF00000-FFF7FFFF are forwarded to ISA).
117          * Set bit 7: Extended BIOS Enable (PCI master accesses to
118          *            FFF80000-FFFDFFFF are forwarded to ISA).
119          * Set bit 6: Lower BIOS Enable (PCI master, or ISA master accesses to
120          *            the lower 64-Kbyte BIOS block (E0000-EFFFF) at the top
121          *            of 1 Mbyte, or the aliases at the top of 4 Gbyte
122          *            (FFFE0000-FFFEFFFF) result in the generation of BIOSCS#.
123          * Note: Accesses to FFFF0000-FFFFFFFF are always forwarded to ISA.
124          * Set bit 2: BIOSCS# Write Enable (1=enable, 0=disable).
125          */
126         new = old | 0x2c4;
127
128         if (new == old)
129                 return 0;
130
131         pci_write_word(dev, xbcs, new);
132
133         if (pci_read_word(dev, xbcs) != new) {
134                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", xbcs, new, name);
135                 return -1;
136         }
137
138         return 0;
139 }
140
141 /*
142  * See ie. page 375 of "Intel ICH7 External Design Specification"
143  * http://download.intel.com/design/chipsets/datashts/30701302.pdf
144  */
145 static int enable_flash_ich(struct pci_dev *dev, const char *name,
146                             int bios_cntl)
147 {
148         uint8_t old, new;
149
150         /*
151          * Note: the ICH0-ICH5 BIOS_CNTL register is actually 16 bit wide, but
152          * just treating it as 8 bit wide seems to work fine in practice.
153          */
154         old = pci_read_byte(dev, bios_cntl);
155
156         new = old | 1;
157
158         if (new == old)
159                 return 0;
160
161         pci_write_byte(dev, bios_cntl, new);
162
163         if (pci_read_byte(dev, bios_cntl) != new) {
164                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", bios_cntl, new, name);
165                 return -1;
166         }
167
168         return 0;
169 }
170
171 static int enable_flash_ich_4e(struct pci_dev *dev, const char *name)
172 {
173         return enable_flash_ich(dev, name, 0x4e);
174 }
175
176 static int enable_flash_ich_dc(struct pci_dev *dev, const char *name)
177 {
178         return enable_flash_ich(dev, name, 0xdc);
179 }
180
181 static int enable_flash_vt823x(struct pci_dev *dev, const char *name)
182 {
183         uint8_t val;
184
185         /* ROM write enable */
186         val = pci_read_byte(dev, 0x40);
187         val |= 0x10;
188         pci_write_byte(dev, 0x40, val);
189
190         if (pci_read_byte(dev, 0x40) != val) {
191                 printf("\nWARNING: Failed to enable ROM Write on \"%s\"\n",
192                        name);
193                 return -1;
194         }
195
196         return 0;
197 }
198
199 static int enable_flash_cs5530(struct pci_dev *dev, const char *name)
200 {
201         uint8_t reg8;
202
203         #define DECODE_CONTROL_REG2             0x5b    /* F0 index 0x5b */
204         #define ROM_AT_LOGIC_CONTROL_REG        0x52    /* F0 index 0x52 */
205
206         #define LOWER_ROM_ADDRESS_RANGE         (1 << 0)
207         #define ROM_WRITE_ENABLE                (1 << 1)
208         #define UPPER_ROM_ADDRESS_RANGE         (1 << 2)
209         #define BIOS_ROM_POSITIVE_DECODE        (1 << 5)
210
211         /* Decode 0x000E0000-0x000FFFFF (128 KB), not just 64 KB, and
212          * decode 0xFF000000-0xFFFFFFFF (16 MB), not just 256 KB.
213          * Make the configured ROM areas writable.
214          */
215         reg8 = pci_read_byte(dev, ROM_AT_LOGIC_CONTROL_REG);
216         reg8 |= LOWER_ROM_ADDRESS_RANGE;
217         reg8 |= UPPER_ROM_ADDRESS_RANGE;
218         reg8 |= ROM_WRITE_ENABLE;
219         pci_write_byte(dev, ROM_AT_LOGIC_CONTROL_REG, reg8);
220
221         /* Set positive decode on ROM. */
222         reg8 = pci_read_byte(dev, DECODE_CONTROL_REG2);
223         reg8 |= BIOS_ROM_POSITIVE_DECODE;
224         pci_write_byte(dev, DECODE_CONTROL_REG2, reg8);
225
226         return 0;
227 }
228
229 static int enable_flash_cs5536(struct pci_dev *dev, const char *name)
230 {
231         int fd_msr;
232         unsigned char buf[8];
233         unsigned int addr = 0x1808;
234
235         /* Geode systems write protect the BIOS via RCONFs (cache
236          * settings similar to MTRRs). To unlock, change MSR 0x1808
237          * top byte to 0x22. Reading and writing to msr, however
238          * requires instructions rdmsr/wrmsr, which are ring0 privileged
239          * instructions so only the kernel can do the read/write.  This
240          * function, therefore, requires that the msr kernel module be
241          * loaded to access these instructions from user space using
242          * device /dev/cpu/0/msr.  This hard-coded driver location
243          * could have potential problems on SMP machines since it
244          * assumes cpu0, but it is safe on the Geode which is not SMP.
245          *
246          * This is probably not portable beyond Linux.
247          */
248
249         fd_msr = open("/dev/cpu/0/msr", O_RDONLY);
250         if (!fd_msr) {
251                 perror("open msr");
252                 return -1;
253         }
254         lseek64(fd_msr, (off64_t) addr, SEEK_SET);
255         read(fd_msr, buf, 8);
256         close(fd_msr);
257         if (buf[7] != 0x22) {
258                 printf("Enabling Geode MSR to write to flash.\n");
259                 buf[7] &= 0xFB;
260                 fd_msr = open("/dev/cpu/0/msr", O_WRONLY);
261                 if (!fd_msr) {
262                         perror("open msr");
263                         return -1;
264                 }
265                 lseek64(fd_msr, (off64_t) addr, SEEK_SET);
266                 if (write(fd_msr, buf, 8) < 0) {
267                         perror("msr write");
268                         printf
269                             ("Cannot write to MSR.  Make sure msr kernel is loaded: 'modprobe msr'\n");
270                         return -1;
271                 }
272                 close(fd_msr);
273         }
274         return 0;
275 }
276
277 static int enable_flash_sc1100(struct pci_dev *dev, const char *name)
278 {
279         uint8_t new;
280
281         pci_write_byte(dev, 0x52, 0xee);
282
283         new = pci_read_byte(dev, 0x52);
284
285         if (new != 0xee) {
286                 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x52, new, name);
287                 return -1;
288         }
289
290         return 0;
291 }
292
293 static int enable_flash_sis5595(struct pci_dev *dev, const char *name)
294 {
295         uint8_t new, newer;
296
297         new = pci_read_byte(dev, 0x45);
298
299         new &= (~0x20);         /* Clear bit 5. */
300         new |= 0x4;             /* Set bit 2. */
301
302         pci_write_byte(dev, 0x45, new);
303
304         newer = pci_read_byte(dev, 0x45);
305         if (newer != new) {
306                 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x45, new, name);
307                 printf("Stuck at 0x%x\n", newer);
308                 return -1;
309         }
310
311         return 0;
312 }
313
314 static int enable_flash_amd8111(struct pci_dev *dev, const char *name)
315 {
316         uint8_t old, new;
317
318         /* Enable decoding at 0xffb00000 to 0xffffffff. */
319         old = pci_read_byte(dev, 0x43);
320         new = old | 0xC0;
321         if (new != old) {
322                 pci_write_byte(dev, 0x43, new);
323                 if (pci_read_byte(dev, 0x43) != new) {
324                         printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x43, new, name);
325                 }
326         }
327
328         old = pci_read_byte(dev, 0x40);
329         new = old | 0x01;
330         if (new == old)
331                 return 0;
332         pci_write_byte(dev, 0x40, new);
333
334         if (pci_read_byte(dev, 0x40) != new) {
335                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x40, new, name);
336                 return -1;
337         }
338
339         return 0;
340 }
341
342 static int enable_flash_ck804(struct pci_dev *dev, const char *name)
343 {
344         uint8_t old, new;
345
346         old = pci_read_byte(dev, 0x88);
347         new = old | 0xc0;
348         if (new != old) {
349                 pci_write_byte(dev, 0x88, new);
350                 if (pci_read_byte(dev, 0x88) != new) {
351                         printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x88, new, name);
352                 }
353         }
354
355         old = pci_read_byte(dev, 0x6d);
356         new = old | 0x01;
357         if (new == old)
358                 return 0;
359         pci_write_byte(dev, 0x6d, new);
360
361         if (pci_read_byte(dev, 0x6d) != new) {
362                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x6d, new, name);
363                 return -1;
364         }
365
366         return 0;
367 }
368
369 /* ATI Technologies Inc IXP SB400 PCI-ISA Bridge (rev 80) */
370 static int enable_flash_sb400(struct pci_dev *dev, const char *name)
371 {
372         uint8_t tmp;
373         struct pci_filter f;
374         struct pci_dev *smbusdev;
375
376         /* Look for the SMBus device. */
377         pci_filter_init((struct pci_access *)0, &f);
378         f.vendor = 0x1002;
379         f.device = 0x4372;
380
381         for (smbusdev = pacc->devices; smbusdev; smbusdev = smbusdev->next) {
382                 if (pci_filter_match(&f, smbusdev)) {
383                         break;
384                 }
385         }
386
387         if (!smbusdev) {
388                 fprintf(stderr, "ERROR: SMBus device not found. Aborting.\n");
389                 exit(1);
390         }
391
392         /* Enable some SMBus stuff. */
393         tmp = pci_read_byte(smbusdev, 0x79);
394         tmp |= 0x01;
395         pci_write_byte(smbusdev, 0x79, tmp);
396
397         /* Change southbridge. */
398         tmp = pci_read_byte(dev, 0x48);
399         tmp |= 0x21;
400         pci_write_byte(dev, 0x48, tmp);
401
402         /* Now become a bit silly. */
403         tmp = inb(0xc6f);
404         outb(tmp, 0xeb);
405         outb(tmp, 0xeb);
406         tmp |= 0x40;
407         outb(tmp, 0xc6f);
408         outb(tmp, 0xeb);
409         outb(tmp, 0xeb);
410
411         return 0;
412 }
413
414 static int enable_flash_mcp55(struct pci_dev *dev, const char *name)
415 {
416         uint8_t old, new, byte;
417         uint16_t word;
418
419         /* Set the 0-16 MB enable bits. */
420         byte = pci_read_byte(dev, 0x88);
421         byte |= 0xff;           /* 256K */
422         pci_write_byte(dev, 0x88, byte);
423         byte = pci_read_byte(dev, 0x8c);
424         byte |= 0xff;           /* 1M */
425         pci_write_byte(dev, 0x8c, byte);
426         word = pci_read_word(dev, 0x90);
427         word |= 0x7fff;         /* 16M */
428         pci_write_word(dev, 0x90, word);
429
430         old = pci_read_byte(dev, 0x6d);
431         new = old | 0x01;
432         if (new == old)
433                 return 0;
434         pci_write_byte(dev, 0x6d, new);
435
436         if (pci_read_byte(dev, 0x6d) != new) {
437                 printf
438                     ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n",
439                      0x6d, new, name);
440                 return -1;
441         }
442
443         return 0;
444 }
445
446 static int enable_flash_ht1000(struct pci_dev *dev, const char *name)
447 {
448         uint8_t byte;
449
450         /* Set the 4MB enable bit. */
451         byte = pci_read_byte(dev, 0x41);
452         byte |= 0x0e;
453         pci_write_byte(dev, 0x41, byte);
454
455         byte = pci_read_byte(dev, 0x43);
456         byte |= (1 << 4);
457         pci_write_byte(dev, 0x43, byte);
458
459         return 0;
460 }
461
462 typedef struct penable {
463         uint16_t vendor, device;
464         const char *name;
465         int (*doit) (struct pci_dev *dev, const char *name);
466 } FLASH_ENABLE;
467
468 static const FLASH_ENABLE enables[] = {
469         {0x1039, 0x0630, "SIS630", enable_flash_sis630},
470         {0x8086, 0x7110, "PIIX4/PIIX4E/PIIX4M", enable_flash_piix4},
471         {0x8086, 0x7198, "Intel 440MX", enable_flash_piix4},
472         {0x8086, 0x2410, "ICH", enable_flash_ich_4e},
473         {0x8086, 0x2420, "ICH0", enable_flash_ich_4e},
474         {0x8086, 0x2440, "ICH2", enable_flash_ich_4e},
475         {0x8086, 0x244c, "ICH2-M", enable_flash_ich_4e},
476         {0x8086, 0x2480, "ICH3-S", enable_flash_ich_4e},
477         {0x8086, 0x248c, "ICH3-M", enable_flash_ich_4e},
478         {0x8086, 0x24c0, "ICH4/ICH4-L", enable_flash_ich_4e},
479         {0x8086, 0x24cc, "ICH4-M", enable_flash_ich_4e},
480         {0x8086, 0x24d0, "ICH5/ICH5R", enable_flash_ich_4e},
481         {0x8086, 0x2640, "ICH6/ICH6R", enable_flash_ich_dc},
482         {0x8086, 0x2641, "ICH6-M", enable_flash_ich_dc},
483         {0x8086, 0x27b0, "ICH7DH", enable_flash_ich_dc},
484         {0x8086, 0x27b8, "ICH7/ICH7R", enable_flash_ich_dc},
485         {0x8086, 0x27b9, "ICH7M", enable_flash_ich_dc},
486         {0x8086, 0x27bd, "ICH7MDH", enable_flash_ich_dc},
487         {0x8086, 0x2810, "ICH8/ICH8R", enable_flash_ich_dc},
488         {0x8086, 0x2812, "ICH8DH", enable_flash_ich_dc},
489         {0x8086, 0x2814, "ICH8DO", enable_flash_ich_dc},
490         {0x1106, 0x8231, "VT8231", enable_flash_vt823x},
491         {0x1106, 0x3177, "VT8235", enable_flash_vt823x},
492         {0x1106, 0x3227, "VT8237", enable_flash_vt823x},
493         {0x1106, 0x8324, "CX700", enable_flash_vt823x},
494         {0x1106, 0x0686, "VT82C686", enable_flash_amd8111},
495         {0x1078, 0x0100, "CS5530/CS5530A", enable_flash_cs5530},
496         {0x100b, 0x0510, "SC1100", enable_flash_sc1100},
497         {0x1039, 0x0008, "SIS5595", enable_flash_sis5595},
498         {0x1022, 0x2080, "AMD GEODE CS5536", enable_flash_cs5536},
499         {0x1022, 0x7468, "AMD8111", enable_flash_amd8111},
500         {0x10B9, 0x1533, "ALi M1533", enable_flash_ali_m1533},
501         {0x10de, 0x0050, "NVIDIA CK804", enable_flash_ck804},   /* LPC */
502         {0x10de, 0x0051, "NVIDIA CK804", enable_flash_ck804},   /* Pro */
503         {0x10de, 0x00d3, "NVIDIA CK804", enable_flash_ck804},   /* Slave, should not be here, to fix known bug for A01. */
504         {0x10de, 0x0260, "NVidia MCP51", enable_flash_ck804},
505         {0x10de, 0x0261, "NVidia MCP51", enable_flash_ck804},
506         {0x10de, 0x0262, "NVidia MCP51", enable_flash_ck804},
507         {0x10de, 0x0263, "NVidia MCP51", enable_flash_ck804},
508         {0x10de, 0x0360, "NVIDIA MCP55", enable_flash_mcp55},   /* M57SLI-S4 */
509         {0x10de, 0x0361, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
510         {0x10de, 0x0362, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
511         {0x10de, 0x0363, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
512         {0x10de, 0x0364, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
513         {0x10de, 0x0365, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
514         {0x10de, 0x0366, "NVIDIA MCP55", enable_flash_mcp55},   /* LPC */
515         {0x10de, 0x0367, "NVIDIA MCP55", enable_flash_mcp55},   /* Pro */
516         {0x1002, 0x4377, "ATI SB400", enable_flash_sb400},
517         {0x1166, 0x0205, "Broadcom HT-1000", enable_flash_ht1000},
518 };
519
520 int chipset_flash_enable(void)
521 {
522         struct pci_dev *dev = 0;
523         int ret = -2;           /* Nothing! */
524         int i;
525
526         /* Now let's try to find the chipset we have... */
527         /* TODO: Use ARRAY_SIZE. */
528         for (i = 0; i < sizeof(enables) / sizeof(enables[0]); i++) {
529                 dev = pci_dev_find(enables[i].vendor, enables[i].device);
530                 if (dev)
531                         break;
532         }
533
534         if (dev) {
535                 printf("Found chipset \"%s\", enabling flash write... ",
536                        enables[i].name);
537
538                 ret = enables[i].doit(dev, enables[i].name);
539                 if (ret)
540                         printf("FAILED!\n");
541                 else
542                         printf("OK.\n");
543         }
544
545         return ret;
546 }