flashrom: Abstract mmap() in physmap.c and only open /dev/mem on the first physmap...
[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 <sys/mman.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include "flash.h"
37
38 unsigned long flashbase = 0;
39
40 /**
41  * flashrom defaults to LPC flash devices. If a known SPI controller is found
42  * and the SPI strappings are set, this will be overwritten by the probing code.
43  *
44  * Eventually, this will become an array when multiple flash support works.
45  */
46
47 flashbus_t flashbus = BUS_TYPE_LPC;
48 void *spibar = NULL;
49
50 extern int ichspi_lock;
51
52 static int enable_flash_ali_m1533(struct pci_dev *dev, const char *name)
53 {
54         uint8_t tmp;
55
56         /*
57          * ROM Write enable, 0xFFFC0000-0xFFFDFFFF and
58          * 0xFFFE0000-0xFFFFFFFF ROM select enable.
59          */
60         tmp = pci_read_byte(dev, 0x47);
61         tmp |= 0x46;
62         pci_write_byte(dev, 0x47, tmp);
63
64         return 0;
65 }
66
67 static int enable_flash_sis630(struct pci_dev *dev, const char *name)
68 {
69         uint8_t b;
70
71         /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630. */
72         b = pci_read_byte(dev, 0x40);
73         pci_write_byte(dev, 0x40, b | 0xb);
74
75         /* Flash write enable on SiS 540/630. */
76         b = pci_read_byte(dev, 0x45);
77         pci_write_byte(dev, 0x45, b | 0x40);
78
79         /* The same thing on SiS 950 Super I/O side... */
80
81         /* First probe for Super I/O on config port 0x2e. */
82         OUTB(0x87, 0x2e);
83         OUTB(0x01, 0x2e);
84         OUTB(0x55, 0x2e);
85         OUTB(0x55, 0x2e);
86
87         if (INB(0x2f) != 0x87) {
88                 /* If that failed, try config port 0x4e. */
89                 OUTB(0x87, 0x4e);
90                 OUTB(0x01, 0x4e);
91                 OUTB(0x55, 0x4e);
92                 OUTB(0xaa, 0x4e);
93                 if (INB(0x4f) != 0x87) {
94                         printf("Can not access SiS 950\n");
95                         return -1;
96                 }
97                 OUTB(0x24, 0x4e);
98                 b = INB(0x4f) | 0xfc;
99                 OUTB(0x24, 0x4e);
100                 OUTB(b, 0x4f);
101                 OUTB(0x02, 0x4e);
102                 OUTB(0x02, 0x4f);
103         }
104
105         OUTB(0x24, 0x2e);
106         printf("2f is %#x\n", INB(0x2f));
107         b = INB(0x2f) | 0xfc;
108         OUTB(0x24, 0x2e);
109         OUTB(b, 0x2f);
110
111         OUTB(0x02, 0x2e);
112         OUTB(0x02, 0x2f);
113
114         return 0;
115 }
116
117 /* Datasheet:
118  *   - Name: 82371AB PCI-TO-ISA / IDE XCELERATOR (PIIX4)
119  *   - URL: http://www.intel.com/design/intarch/datashts/290562.htm
120  *   - PDF: http://www.intel.com/design/intarch/datashts/29056201.pdf
121  *   - Order Number: 290562-001
122  */
123 static int enable_flash_piix4(struct pci_dev *dev, const char *name)
124 {
125         uint16_t old, new;
126         uint16_t xbcs = 0x4e;   /* X-Bus Chip Select register. */
127
128         old = pci_read_word(dev, xbcs);
129
130         /* Set bit 9: 1-Meg Extended BIOS Enable (PCI master accesses to
131          *            FFF00000-FFF7FFFF are forwarded to ISA).
132          *            Note: This bit is reserved on PIIX/PIIX3/MPIIX.
133          * Set bit 7: Extended BIOS Enable (PCI master accesses to
134          *            FFF80000-FFFDFFFF are forwarded to ISA).
135          * Set bit 6: Lower BIOS Enable (PCI master, or ISA master accesses to
136          *            the lower 64-Kbyte BIOS block (E0000-EFFFF) at the top
137          *            of 1 Mbyte, or the aliases at the top of 4 Gbyte
138          *            (FFFE0000-FFFEFFFF) result in the generation of BIOSCS#.
139          * Note: Accesses to FFFF0000-FFFFFFFF are always forwarded to ISA.
140          * Set bit 2: BIOSCS# Write Enable (1=enable, 0=disable).
141          */
142         if (dev->device_id == 0x122e || dev->device_id == 0x7000
143             || dev->device_id == 0x1234)
144                 new = old | 0x00c4; /* PIIX/PIIX3/MPIIX: Bit 9 is reserved. */
145         else
146                 new = old | 0x02c4;
147
148         if (new == old)
149                 return 0;
150
151         pci_write_word(dev, xbcs, new);
152
153         if (pci_read_word(dev, xbcs) != new) {
154                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", xbcs, new, name);
155                 return -1;
156         }
157
158         return 0;
159 }
160
161 /*
162  * See ie. page 375 of "Intel I/O Controller Hub 7 (ICH7) Family Datasheet"
163  * http://download.intel.com/design/chipsets/datashts/30701303.pdf
164  */
165 static int enable_flash_ich(struct pci_dev *dev, const char *name,
166                             int bios_cntl)
167 {
168         uint8_t old, new;
169
170         /*
171          * Note: the ICH0-ICH5 BIOS_CNTL register is actually 16 bit wide, but
172          * just treating it as 8 bit wide seems to work fine in practice.
173          */
174         old = pci_read_byte(dev, bios_cntl);
175
176         printf_debug("\nBIOS Lock Enable: %sabled, ",
177                      (old & (1 << 1)) ? "en" : "dis");
178         printf_debug("BIOS Write Enable: %sabled, ",
179                      (old & (1 << 0)) ? "en" : "dis");
180         printf_debug("BIOS_CNTL is 0x%x\n", old);
181
182         new = old | 1;
183
184         if (new == old)
185                 return 0;
186
187         pci_write_byte(dev, bios_cntl, new);
188
189         if (pci_read_byte(dev, bios_cntl) != new) {
190                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", bios_cntl, new, name);
191                 return -1;
192         }
193
194         return 0;
195 }
196
197 static int enable_flash_ich_4e(struct pci_dev *dev, const char *name)
198 {
199         return enable_flash_ich(dev, name, 0x4e);
200 }
201
202 static int enable_flash_ich_dc(struct pci_dev *dev, const char *name)
203 {
204         return enable_flash_ich(dev, name, 0xdc);
205 }
206
207 #define ICH_STRAP_RSVD 0x00
208 #define ICH_STRAP_SPI  0x01
209 #define ICH_STRAP_PCI  0x02
210 #define ICH_STRAP_LPC  0x03
211
212 static int enable_flash_vt8237s_spi(struct pci_dev *dev, const char *name)
213 {
214         uint32_t mmio_base;
215
216         mmio_base = (pci_read_long(dev, 0xbc)) << 8;
217         printf_debug("MMIO base at = 0x%x\n", mmio_base);
218         spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
219
220         printf_debug("0x6c: 0x%04x     (CLOCK/DEBUG)\n",
221                      *(uint16_t *) (spibar + 0x6c));
222
223         flashbus = BUS_TYPE_VIA_SPI;
224
225         return 0;
226 }
227
228 static int enable_flash_ich_dc_spi(struct pci_dev *dev, const char *name,
229                                    int ich_generation)
230 {
231         int ret, i;
232         uint8_t old, new, bbs, buc;
233         uint16_t spibar_offset, tmp2;
234         uint32_t tmp, gcs;
235         void *rcrb;
236         //TODO: These names are incorrect for EP80579. For that, the solution would look like the commented line
237         //static const char *straps_names[] = {"SPI", "reserved", "reserved", "LPC" };
238         static const char *straps_names[] = { "reserved", "SPI", "PCI", "LPC" };
239
240         /* Enable Flash Writes */
241         ret = enable_flash_ich_dc(dev, name);
242
243         /* Get physical address of Root Complex Register Block */
244         tmp = pci_read_long(dev, 0xf0) & 0xffffc000;
245         printf_debug("\nRoot Complex Register Block address = 0x%x\n", tmp);
246
247         /* Map RCBA to virtual memory */
248         rcrb = physmap("ICH RCRB", tmp, 0x4000);
249
250         gcs = *(volatile uint32_t *)(rcrb + 0x3410);
251         printf_debug("GCS = 0x%x: ", gcs);
252         printf_debug("BIOS Interface Lock-Down: %sabled, ",
253                      (gcs & 0x1) ? "en" : "dis");
254         bbs = (gcs >> 10) & 0x3;
255         printf_debug("BOOT BIOS Straps: 0x%x (%s)\n", bbs, straps_names[bbs]);
256
257         buc = *(volatile uint8_t *)(rcrb + 0x3414);
258         printf_debug("Top Swap : %s\n",
259                      (buc & 1) ? "enabled (A16 inverted)" : "not enabled");
260
261         /* It seems the ICH7 does not support SPI and LPC chips at the same
262          * time. At least not with our current code. So we prevent searching
263          * on ICH7 when the southbridge is strapped to LPC
264          */
265
266         if (ich_generation == 7 && bbs == ICH_STRAP_LPC) {
267                 /* No further SPI initialization required */
268                 return ret;
269         }
270
271         switch (ich_generation) {
272         case 7:
273                 flashbus = BUS_TYPE_ICH7_SPI;
274                 spibar_offset = 0x3020;
275                 break;
276         case 8:
277                 flashbus = BUS_TYPE_ICH9_SPI;
278                 spibar_offset = 0x3020;
279                 break;
280         case 9:
281         case 10:
282         default:                /* Future version might behave the same */
283                 flashbus = BUS_TYPE_ICH9_SPI;
284                 spibar_offset = 0x3800;
285                 break;
286         }
287
288         /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
289         printf_debug("SPIBAR = 0x%x + 0x%04x\n", tmp, spibar_offset);
290
291         /* Assign Virtual Address */
292         spibar = rcrb + spibar_offset;
293
294         switch (flashbus) {
295         case BUS_TYPE_ICH7_SPI:
296                 printf_debug("0x00: 0x%04x     (SPIS)\n",
297                              *(uint16_t *) (spibar + 0));
298                 printf_debug("0x02: 0x%04x     (SPIC)\n",
299                              *(uint16_t *) (spibar + 2));
300                 printf_debug("0x04: 0x%08x (SPIA)\n",
301                              *(uint32_t *) (spibar + 4));
302                 for (i = 0; i < 8; i++) {
303                         int offs;
304                         offs = 8 + (i * 8);
305                         printf_debug("0x%02x: 0x%08x (SPID%d)\n", offs,
306                                      *(uint32_t *) (spibar + offs), i);
307                         printf_debug("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
308                                      *(uint32_t *) (spibar + offs + 4), i);
309                 }
310                 printf_debug("0x50: 0x%08x (BBAR)\n",
311                              *(uint32_t *) (spibar + 0x50));
312                 printf_debug("0x54: 0x%04x     (PREOP)\n",
313                              *(uint16_t *) (spibar + 0x54));
314                 printf_debug("0x56: 0x%04x     (OPTYPE)\n",
315                              *(uint16_t *) (spibar + 0x56));
316                 printf_debug("0x58: 0x%08x (OPMENU)\n",
317                              *(uint32_t *) (spibar + 0x58));
318                 printf_debug("0x5c: 0x%08x (OPMENU+4)\n",
319                              *(uint32_t *) (spibar + 0x5c));
320                 for (i = 0; i < 4; i++) {
321                         int offs;
322                         offs = 0x60 + (i * 4);
323                         printf_debug("0x%02x: 0x%08x (PBR%d)\n", offs,
324                                      *(uint32_t *) (spibar + offs), i);
325                 }
326                 printf_debug("\n");
327                 if ((*(uint16_t *) spibar) & (1 << 15)) {
328                         printf("WARNING: SPI Configuration Lockdown activated.\n");
329                         ichspi_lock = 1;
330                 }
331                 ich_init_opcodes();
332                 break;
333         case BUS_TYPE_ICH9_SPI:
334                 tmp2 = *(uint16_t *) (spibar + 4);
335                 printf_debug("0x04: 0x%04x (HSFS)\n", tmp2);
336                 printf_debug("FLOCKDN %i, ", (tmp2 >> 15 & 1));
337                 printf_debug("FDV %i, ", (tmp2 >> 14) & 1);
338                 printf_debug("FDOPSS %i, ", (tmp2 >> 13) & 1);
339                 printf_debug("SCIP %i, ", (tmp2 >> 5) & 1);
340                 printf_debug("BERASE %i, ", (tmp2 >> 3) & 3);
341                 printf_debug("AEL %i, ", (tmp2 >> 2) & 1);
342                 printf_debug("FCERR %i, ", (tmp2 >> 1) & 1);
343                 printf_debug("FDONE %i\n", (tmp2 >> 0) & 1);
344
345                 tmp = *(uint32_t *) (spibar + 0x50);
346                 printf_debug("0x50: 0x%08x (FRAP)\n", tmp);
347                 printf_debug("BMWAG %i, ", (tmp >> 24) & 0xff);
348                 printf_debug("BMRAG %i, ", (tmp >> 16) & 0xff);
349                 printf_debug("BRWA %i, ", (tmp >> 8) & 0xff);
350                 printf_debug("BRRA %i\n", (tmp >> 0) & 0xff);
351
352                 printf_debug("0x54: 0x%08x (FREG0)\n",
353                              *(uint32_t *) (spibar + 0x54));
354                 printf_debug("0x58: 0x%08x (FREG1)\n",
355                              *(uint32_t *) (spibar + 0x58));
356                 printf_debug("0x5C: 0x%08x (FREG2)\n",
357                              *(uint32_t *) (spibar + 0x5C));
358                 printf_debug("0x60: 0x%08x (FREG3)\n",
359                              *(uint32_t *) (spibar + 0x60));
360                 printf_debug("0x64: 0x%08x (FREG4)\n",
361                              *(uint32_t *) (spibar + 0x64));
362                 printf_debug("0x74: 0x%08x (PR0)\n",
363                              *(uint32_t *) (spibar + 0x74));
364                 printf_debug("0x78: 0x%08x (PR1)\n",
365                              *(uint32_t *) (spibar + 0x78));
366                 printf_debug("0x7C: 0x%08x (PR2)\n",
367                              *(uint32_t *) (spibar + 0x7C));
368                 printf_debug("0x80: 0x%08x (PR3)\n",
369                              *(uint32_t *) (spibar + 0x80));
370                 printf_debug("0x84: 0x%08x (PR4)\n",
371                              *(uint32_t *) (spibar + 0x84));
372                 printf_debug("0x90: 0x%08x (SSFS, SSFC)\n",
373                              *(uint32_t *) (spibar + 0x90));
374                 printf_debug("0x94: 0x%04x     (PREOP)\n",
375                              *(uint16_t *) (spibar + 0x94));
376                 printf_debug("0x96: 0x%04x     (OPTYPE)\n",
377                              *(uint16_t *) (spibar + 0x96));
378                 printf_debug("0x98: 0x%08x (OPMENU)\n",
379                              *(uint32_t *) (spibar + 0x98));
380                 printf_debug("0x9C: 0x%08x (OPMENU+4)\n",
381                              *(uint32_t *) (spibar + 0x9C));
382                 printf_debug("0xA0: 0x%08x (BBAR)\n",
383                              *(uint32_t *) (spibar + 0xA0));
384                 printf_debug("0xB0: 0x%08x (FDOC)\n",
385                              *(uint32_t *) (spibar + 0xB0));
386                 if (tmp2 & (1 << 15)) {
387                         printf("WARNING: SPI Configuration Lockdown activated.\n");
388                         ichspi_lock = 1;
389                 }
390                 ich_init_opcodes();
391                 break;
392         default:
393                 /* Nothing */
394                 break;
395         }
396
397         old = pci_read_byte(dev, 0xdc);
398         printf_debug("SPI Read Configuration: ");
399         new = (old >> 2) & 0x3;
400         switch (new) {
401         case 0:
402         case 1:
403         case 2:
404                 printf_debug("prefetching %sabled, caching %sabled, ",
405                              (new & 0x2) ? "en" : "dis",
406                              (new & 0x1) ? "dis" : "en");
407                 break;
408         default:
409                 printf_debug("invalid prefetching/caching settings, ");
410                 break;
411         }
412
413         return ret;
414 }
415
416 static int enable_flash_ich7(struct pci_dev *dev, const char *name)
417 {
418         return enable_flash_ich_dc_spi(dev, name, 7);
419 }
420
421 static int enable_flash_ich8(struct pci_dev *dev, const char *name)
422 {
423         return enable_flash_ich_dc_spi(dev, name, 8);
424 }
425
426 static int enable_flash_ich9(struct pci_dev *dev, const char *name)
427 {
428         return enable_flash_ich_dc_spi(dev, name, 9);
429 }
430
431 static int enable_flash_ich10(struct pci_dev *dev, const char *name)
432 {
433         return enable_flash_ich_dc_spi(dev, name, 10);
434 }
435
436 static int enable_flash_vt823x(struct pci_dev *dev, const char *name)
437 {
438         uint8_t val;
439
440         /* enable ROM decode range (1MB) FFC00000 - FFFFFFFF */
441         pci_write_byte(dev, 0x41, 0x7f);
442
443         /* ROM write enable */
444         val = pci_read_byte(dev, 0x40);
445         val |= 0x10;
446         pci_write_byte(dev, 0x40, val);
447
448         if (pci_read_byte(dev, 0x40) != val) {
449                 printf("\nWARNING: Failed to enable ROM Write on \"%s\"\n",
450                        name);
451                 return -1;
452         }
453
454         return 0;
455 }
456
457 static int enable_flash_cs5530(struct pci_dev *dev, const char *name)
458 {
459         uint8_t reg8;
460
461 #define DECODE_CONTROL_REG2             0x5b    /* F0 index 0x5b */
462 #define ROM_AT_LOGIC_CONTROL_REG        0x52    /* F0 index 0x52 */
463
464 #define LOWER_ROM_ADDRESS_RANGE         (1 << 0)
465 #define ROM_WRITE_ENABLE                (1 << 1)
466 #define UPPER_ROM_ADDRESS_RANGE         (1 << 2)
467 #define BIOS_ROM_POSITIVE_DECODE        (1 << 5)
468
469         /* Decode 0x000E0000-0x000FFFFF (128 KB), not just 64 KB, and
470          * decode 0xFF000000-0xFFFFFFFF (16 MB), not just 256 KB.
471          * Make the configured ROM areas writable.
472          */
473         reg8 = pci_read_byte(dev, ROM_AT_LOGIC_CONTROL_REG);
474         reg8 |= LOWER_ROM_ADDRESS_RANGE;
475         reg8 |= UPPER_ROM_ADDRESS_RANGE;
476         reg8 |= ROM_WRITE_ENABLE;
477         pci_write_byte(dev, ROM_AT_LOGIC_CONTROL_REG, reg8);
478
479         /* Set positive decode on ROM. */
480         reg8 = pci_read_byte(dev, DECODE_CONTROL_REG2);
481         reg8 |= BIOS_ROM_POSITIVE_DECODE;
482         pci_write_byte(dev, DECODE_CONTROL_REG2, reg8);
483
484         return 0;
485 }
486
487 /**
488  * Geode systems write protect the BIOS via RCONFs (cache settings similar
489  * to MTRRs). To unlock, change MSR 0x1808 top byte to 0x22. Reading and
490  * writing to MSRs, however requires instructions rdmsr/wrmsr, which are
491  * ring0 privileged instructions so only the kernel can do the read/write.
492  * This function, therefore, requires that the msr kernel module be loaded
493  * to access these instructions from user space using device /dev/cpu/0/msr.
494  *
495  * This hard-coded location could have potential problems on SMP machines
496  * since it assumes cpu0, but it is safe on the Geode which is not SMP.
497  *
498  * Geode systems also write protect the NOR flash chip itself via MSR_NORF_CTL.
499  * To enable write to NOR Boot flash for the benefit of systems that have such
500  * a setup, raise MSR 0x51400018 WE_CS3 (write enable Boot Flash Chip Select).
501  *
502  * This is probably not portable beyond Linux.
503  */
504 static int enable_flash_cs5536(struct pci_dev *dev, const char *name)
505 {
506 #define MSR_RCONF_DEFAULT       0x1808
507 #define MSR_NORF_CTL            0x51400018
508
509         int fd_msr;
510         unsigned char buf[8];
511
512         fd_msr = open("/dev/cpu/0/msr", O_RDWR);
513         if (!fd_msr) {
514                 perror("open msr");
515                 return -1;
516         }
517
518         if (lseek64(fd_msr, (off64_t) MSR_RCONF_DEFAULT, SEEK_SET) == -1) {
519                 perror("lseek64");
520                 printf("Cannot operate on MSR. Did you run 'modprobe msr'?\n");
521                 close(fd_msr);
522                 return -1;
523         }
524
525         if (read(fd_msr, buf, 8) != 8) {
526                 perror("read msr");
527                 close(fd_msr);
528                 return -1;
529         }
530
531         if (buf[7] != 0x22) {
532                 buf[7] &= 0xfb;
533                 if (lseek64(fd_msr, (off64_t) MSR_RCONF_DEFAULT,
534                             SEEK_SET) == -1) {
535                         perror("lseek64");
536                         close(fd_msr);
537                         return -1;
538                 }
539
540                 if (write(fd_msr, buf, 8) < 0) {
541                         perror("msr write");
542                         close(fd_msr);
543                         return -1;
544                 }
545         }
546
547         if (lseek64(fd_msr, (off64_t) MSR_NORF_CTL, SEEK_SET) == -1) {
548                 perror("lseek64");
549                 close(fd_msr);
550                 return -1;
551         }
552
553         if (read(fd_msr, buf, 8) != 8) {
554                 perror("read msr");
555                 close(fd_msr);
556                 return -1;
557         }
558
559         /* Raise WE_CS3 bit. */
560         buf[0] |= 0x08;
561
562         if (lseek64(fd_msr, (off64_t) MSR_NORF_CTL, SEEK_SET) == -1) {
563                 perror("lseek64");
564                 close(fd_msr);
565                 return -1;
566         }
567         if (write(fd_msr, buf, 8) < 0) {
568                 perror("msr write");
569                 close(fd_msr);
570                 return -1;
571         }
572
573         close(fd_msr);
574
575 #undef MSR_RCONF_DEFAULT
576 #undef MSR_NORF_CTL
577         return 0;
578 }
579
580 static int enable_flash_sc1100(struct pci_dev *dev, const char *name)
581 {
582         uint8_t new;
583
584         pci_write_byte(dev, 0x52, 0xee);
585
586         new = pci_read_byte(dev, 0x52);
587
588         if (new != 0xee) {
589                 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x52, new, name);
590                 return -1;
591         }
592
593         return 0;
594 }
595
596 static int enable_flash_sis5595(struct pci_dev *dev, const char *name)
597 {
598         uint8_t new, newer;
599
600         new = pci_read_byte(dev, 0x45);
601
602         new &= (~0x20);         /* Clear bit 5. */
603         new |= 0x4;             /* Set bit 2. */
604
605         pci_write_byte(dev, 0x45, new);
606
607         newer = pci_read_byte(dev, 0x45);
608         if (newer != new) {
609                 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x45, new, name);
610                 printf("Stuck at 0x%x\n", newer);
611                 return -1;
612         }
613
614         /* Extended BIOS enable = 1, Lower BIOS Enable = 1 */
615         new = pci_read_byte(dev, 0x40);
616         new &= 0xFB;
617         new |= 0x3;
618         pci_write_byte(dev, 0x40, new);
619         newer = pci_read_byte(dev, 0x40);
620         if (newer != new) {
621                 printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x40, new, name);
622                 printf("Stuck at 0x%x\n", newer);
623                 return -1;
624         }
625         return 0;
626 }
627
628 /* Works for AMD-8111, VIA VT82C586A/B, VIA VT82C686A/B. */
629 static int enable_flash_amd8111(struct pci_dev *dev, const char *name)
630 {
631         uint8_t old, new;
632
633         /* Enable decoding at 0xffb00000 to 0xffffffff. */
634         old = pci_read_byte(dev, 0x43);
635         new = old | 0xC0;
636         if (new != old) {
637                 pci_write_byte(dev, 0x43, new);
638                 if (pci_read_byte(dev, 0x43) != new) {
639                         printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x43, new, name);
640                 }
641         }
642
643         /* Enable 'ROM write' bit. */
644         old = pci_read_byte(dev, 0x40);
645         new = old | 0x01;
646         if (new == old)
647                 return 0;
648         pci_write_byte(dev, 0x40, new);
649
650         if (pci_read_byte(dev, 0x40) != new) {
651                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x40, new, name);
652                 return -1;
653         }
654
655         return 0;
656 }
657
658 static int enable_flash_sb600(struct pci_dev *dev, const char *name)
659 {
660         uint32_t tmp, low_bits, num;
661         uint8_t reg;
662
663         low_bits = tmp = pci_read_long(dev, 0xa0);
664         low_bits &= ~0xffffc000; /* for mmap aligning requirements */
665         low_bits &= 0xfffffff0; /* remove low 4 bits */
666         tmp &= 0xffffc000;
667         printf_debug("SPI base address is at 0x%x\n", tmp + low_bits);
668
669         sb600_spibar = physmap("SB600 SPI registers", tmp, 0x4000);
670         sb600_spibar += low_bits;
671
672         /* Clear ROM protect 0-3. */
673         for (reg = 0x50; reg < 0x60; reg += 4) {
674                 num = pci_read_long(dev, reg);
675                 num &= 0xfffffffc;
676                 pci_write_byte(dev, reg, num);
677         }
678
679         flashbus = BUS_TYPE_SB600_SPI;
680
681         /* Enable SPI ROM in SB600 PM register. */
682         OUTB(0x8f, 0xcd6);
683         OUTB(0x0e, 0xcd7);
684
685         return 0;
686 }
687
688 static int enable_flash_ck804(struct pci_dev *dev, const char *name)
689 {
690         uint8_t old, new;
691
692         old = pci_read_byte(dev, 0x88);
693         new = old | 0xc0;
694         if (new != old) {
695                 pci_write_byte(dev, 0x88, new);
696                 if (pci_read_byte(dev, 0x88) != new) {
697                         printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x88, new, name);
698                 }
699         }
700
701         old = pci_read_byte(dev, 0x6d);
702         new = old | 0x01;
703         if (new == old)
704                 return 0;
705         pci_write_byte(dev, 0x6d, new);
706
707         if (pci_read_byte(dev, 0x6d) != new) {
708                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x6d, new, name);
709                 return -1;
710         }
711
712         return 0;
713 }
714
715 /* ATI Technologies Inc IXP SB400 PCI-ISA Bridge (rev 80) */
716 static int enable_flash_sb400(struct pci_dev *dev, const char *name)
717 {
718         uint8_t tmp;
719         struct pci_filter f;
720         struct pci_dev *smbusdev;
721
722         /* Look for the SMBus device. */
723         pci_filter_init((struct pci_access *)0, &f);
724         f.vendor = 0x1002;
725         f.device = 0x4372;
726
727         for (smbusdev = pacc->devices; smbusdev; smbusdev = smbusdev->next) {
728                 if (pci_filter_match(&f, smbusdev))
729                         break;
730         }
731
732         if (!smbusdev) {
733                 fprintf(stderr, "ERROR: SMBus device not found. Aborting.\n");
734                 exit(1);
735         }
736
737         /* Enable some SMBus stuff. */
738         tmp = pci_read_byte(smbusdev, 0x79);
739         tmp |= 0x01;
740         pci_write_byte(smbusdev, 0x79, tmp);
741
742         /* Change southbridge. */
743         tmp = pci_read_byte(dev, 0x48);
744         tmp |= 0x21;
745         pci_write_byte(dev, 0x48, tmp);
746
747         /* Now become a bit silly. */
748         tmp = INB(0xc6f);
749         OUTB(tmp, 0xeb);
750         OUTB(tmp, 0xeb);
751         tmp |= 0x40;
752         OUTB(tmp, 0xc6f);
753         OUTB(tmp, 0xeb);
754         OUTB(tmp, 0xeb);
755
756         return 0;
757 }
758
759 static int enable_flash_mcp55(struct pci_dev *dev, const char *name)
760 {
761         uint8_t old, new, byte;
762         uint16_t word;
763
764         /* Set the 0-16 MB enable bits. */
765         byte = pci_read_byte(dev, 0x88);
766         byte |= 0xff;           /* 256K */
767         pci_write_byte(dev, 0x88, byte);
768         byte = pci_read_byte(dev, 0x8c);
769         byte |= 0xff;           /* 1M */
770         pci_write_byte(dev, 0x8c, byte);
771         word = pci_read_word(dev, 0x90);
772         word |= 0x7fff;         /* 16M */
773         pci_write_word(dev, 0x90, word);
774
775         old = pci_read_byte(dev, 0x6d);
776         new = old | 0x01;
777         if (new == old)
778                 return 0;
779         pci_write_byte(dev, 0x6d, new);
780
781         if (pci_read_byte(dev, 0x6d) != new) {
782                 printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", 0x6d, new, name);
783                 return -1;
784         }
785
786         return 0;
787 }
788
789 static int enable_flash_ht1000(struct pci_dev *dev, const char *name)
790 {
791         uint8_t byte;
792
793         /* Set the 4MB enable bit. */
794         byte = pci_read_byte(dev, 0x41);
795         byte |= 0x0e;
796         pci_write_byte(dev, 0x41, byte);
797
798         byte = pci_read_byte(dev, 0x43);
799         byte |= (1 << 4);
800         pci_write_byte(dev, 0x43, byte);
801
802         return 0;
803 }
804
805 /**
806  * Usually on the x86 architectures (and on other PC-like platforms like some
807  * Alphas or Itanium) the system flash is mapped right below 4G. On the AMD
808  * Elan SC520 only a small piece of the system flash is mapped there, but the
809  * complete flash is mapped somewhere below 1G. The position can be determined
810  * by the BOOTCS PAR register.
811  */
812 static int get_flashbase_sc520(struct pci_dev *dev, const char *name)
813 {
814         int i, bootcs_found = 0;
815         uint32_t parx = 0;
816         void *mmcr;
817
818         /* 1. Map MMCR */
819         mmcr = physmap("Elan SC520 MMCR", 0xfffef000, getpagesize());
820
821         /* 2. Scan PAR0 (0x88) - PAR15 (0xc4) for
822          *    BOOTCS region (PARx[31:29] = 100b)e
823          */
824         for (i = 0x88; i <= 0xc4; i += 4) {
825                 parx = *(volatile uint32_t *)(mmcr + i);
826                 if ((parx >> 29) == 4) {
827                         bootcs_found = 1;
828                         break; /* BOOTCS found */
829                 }
830         }
831
832         /* 3. PARx[25] = 1b --> flashbase[29:16] = PARx[13:0]
833          *    PARx[25] = 0b --> flashbase[29:12] = PARx[17:0]
834          */
835         if (bootcs_found) {
836                 if (parx & (1 << 25)) {
837                         parx &= (1 << 14) - 1; /* Mask [13:0] */
838                         flashbase = parx << 16;
839                 } else {
840                         parx &= (1 << 18) - 1; /* Mask [17:0] */
841                         flashbase = parx << 12;
842                 }
843         } else {
844                 printf("AMD Elan SC520 detected, but no BOOTCS. Assuming flash at 4G\n");
845         }
846
847         /* 4. Clean up */
848         munmap (mmcr, getpagesize());
849         return 0;
850 }
851
852 typedef struct penable {
853         uint16_t vendor, device;
854         const char *name;
855         int (*doit) (struct pci_dev *dev, const char *name);
856 } FLASH_ENABLE;
857
858 static const FLASH_ENABLE enables[] = {
859         {0x1039, 0x0630, "SiS630",              enable_flash_sis630},
860         {0x8086, 0x122e, "Intel PIIX",          enable_flash_piix4},
861         {0x8086, 0x1234, "Intel MPIIX",         enable_flash_piix4},
862         {0x8086, 0x7000, "Intel PIIX3",         enable_flash_piix4},
863         {0x8086, 0x7110, "Intel PIIX4/4E/4M",   enable_flash_piix4},
864         {0x8086, 0x7198, "Intel 440MX",         enable_flash_piix4},
865         {0x8086, 0x2410, "Intel ICH",           enable_flash_ich_4e},
866         {0x8086, 0x2420, "Intel ICH0",          enable_flash_ich_4e},
867         {0x8086, 0x2440, "Intel ICH2",          enable_flash_ich_4e},
868         {0x8086, 0x244c, "Intel ICH2-M",        enable_flash_ich_4e},
869         {0x8086, 0x2480, "Intel ICH3-S",        enable_flash_ich_4e},
870         {0x8086, 0x248c, "Intel ICH3-M",        enable_flash_ich_4e},
871         {0x8086, 0x24c0, "Intel ICH4/ICH4-L",   enable_flash_ich_4e},
872         {0x8086, 0x24cc, "Intel ICH4-M",        enable_flash_ich_4e},
873         {0x8086, 0x24d0, "Intel ICH5/ICH5R",    enable_flash_ich_4e},
874         {0x8086, 0x25a1, "Intel 6300ESB",       enable_flash_ich_4e},
875         {0x8086, 0x2670, "Intel 631xESB/632xESB/3100",    enable_flash_ich_dc},
876         {0x8086, 0x2640, "Intel ICH6/ICH6R",    enable_flash_ich_dc},
877         {0x8086, 0x2641, "Intel ICH6-M",        enable_flash_ich_dc},
878         {0x8086, 0x5031, "Intel EP80579",       enable_flash_ich7},
879         {0x8086, 0x27b0, "Intel ICH7DH",        enable_flash_ich7},
880         {0x8086, 0x27b8, "Intel ICH7/ICH7R",    enable_flash_ich7},
881         {0x8086, 0x27b9, "Intel ICH7M",         enable_flash_ich7},
882         {0x8086, 0x27bd, "Intel ICH7MDH",       enable_flash_ich7},
883         {0x8086, 0x2810, "Intel ICH8/ICH8R",    enable_flash_ich8},
884         {0x8086, 0x2811, "Intel ICH8M-E",       enable_flash_ich8},
885         {0x8086, 0x2812, "Intel ICH8DH",        enable_flash_ich8},
886         {0x8086, 0x2814, "Intel ICH8DO",        enable_flash_ich8},
887         {0x8086, 0x2815, "Intel ICH8M",         enable_flash_ich8},
888         {0x8086, 0x2912, "Intel ICH9DH",        enable_flash_ich9},
889         {0x8086, 0x2914, "Intel ICH9DO",        enable_flash_ich9},
890         {0x8086, 0x2916, "Intel ICH9R",         enable_flash_ich9},
891         {0x8086, 0x2917, "Intel ICH9M-E",       enable_flash_ich9},
892         {0x8086, 0x2918, "Intel ICH9",          enable_flash_ich9},
893         {0x8086, 0x2919, "Intel ICH9M",         enable_flash_ich9},
894         {0x8086, 0x3a14, "Intel ICH10DO",       enable_flash_ich10},
895         {0x8086, 0x3a16, "Intel ICH10R",        enable_flash_ich10},
896         {0x8086, 0x3a18, "Intel ICH10",         enable_flash_ich10},
897         {0x8086, 0x3a1a, "Intel ICH10D",        enable_flash_ich10},
898         {0x1106, 0x8231, "VIA VT8231",          enable_flash_vt823x},
899         {0x1106, 0x3177, "VIA VT8235",          enable_flash_vt823x},
900         {0x1106, 0x3227, "VIA VT8237",          enable_flash_vt823x},
901         {0x1106, 0x3372, "VIA VT8237S",         enable_flash_vt8237s_spi},
902         {0x1106, 0x8324, "VIA CX700",           enable_flash_vt823x},
903         {0x1106, 0x0586, "VIA VT82C586A/B",     enable_flash_amd8111},
904         {0x1106, 0x0686, "VIA VT82C686A/B",     enable_flash_amd8111},
905         {0x1078, 0x0100, "AMD CS5530(A)",       enable_flash_cs5530},
906         {0x100b, 0x0510, "AMD SC1100",          enable_flash_sc1100},
907         {0x1039, 0x0008, "SiS5595",             enable_flash_sis5595},
908         {0x1022, 0x2080, "AMD CS5536",          enable_flash_cs5536},
909         {0x1022, 0x7468, "AMD8111",             enable_flash_amd8111},
910         {0x1002, 0x438D, "ATI(AMD) SB600",      enable_flash_sb600},
911         {0x1002, 0x439d, "ATI(AMD) SB700",      enable_flash_sb600},
912         {0x10B9, 0x1533, "ALi M1533",           enable_flash_ali_m1533},
913         {0x10de, 0x0050, "NVIDIA CK804",        enable_flash_ck804}, /* LPC */
914         {0x10de, 0x0051, "NVIDIA CK804",        enable_flash_ck804}, /* Pro */
915         /* Slave, should not be here, to fix known bug for A01. */
916         {0x10de, 0x00d3, "NVIDIA CK804",        enable_flash_ck804},
917         {0x10de, 0x0260, "NVIDIA MCP51",        enable_flash_ck804},
918         {0x10de, 0x0261, "NVIDIA MCP51",        enable_flash_ck804},
919         {0x10de, 0x0262, "NVIDIA MCP51",        enable_flash_ck804},
920         {0x10de, 0x0263, "NVIDIA MCP51",        enable_flash_ck804},
921         {0x10de, 0x0360, "NVIDIA MCP55",        enable_flash_mcp55}, /* M57SLI*/
922         {0x10de, 0x0361, "NVIDIA MCP55",        enable_flash_mcp55}, /* LPC */
923         {0x10de, 0x0362, "NVIDIA MCP55",        enable_flash_mcp55}, /* LPC */
924         {0x10de, 0x0363, "NVIDIA MCP55",        enable_flash_mcp55}, /* LPC */
925         {0x10de, 0x0364, "NVIDIA MCP55",        enable_flash_mcp55}, /* LPC */
926         {0x10de, 0x0365, "NVIDIA MCP55",        enable_flash_mcp55}, /* LPC */
927         {0x10de, 0x0366, "NVIDIA MCP55",        enable_flash_mcp55}, /* LPC */
928         {0x10de, 0x0367, "NVIDIA MCP55",        enable_flash_mcp55}, /* Pro */
929         {0x10de, 0x0548, "NVIDIA MCP67",        enable_flash_mcp55},
930         {0x1002, 0x4377, "ATI SB400",           enable_flash_sb400},
931         {0x1166, 0x0205, "Broadcom HT-1000",    enable_flash_ht1000},
932         {0x1022, 0x3000, "AMD Elan SC520",      get_flashbase_sc520},
933         {0x1022, 0x7440, "AMD AMD-768",         enable_flash_amd8111},
934 };
935
936 void print_supported_chipsets(void)
937 {
938         int i;
939
940         printf("\nSupported chipsets:\n\n");
941
942         for (i = 0; i < ARRAY_SIZE(enables); i++)
943                 printf("%s (%04x:%04x)\n", enables[i].name,
944                        enables[i].vendor, enables[i].device);
945 }
946
947 int chipset_flash_enable(void)
948 {
949         struct pci_dev *dev = 0;
950         int ret = -2;           /* Nothing! */
951         int i;
952
953         /* Now let's try to find the chipset we have... */
954         for (i = 0; i < ARRAY_SIZE(enables); i++) {
955                 dev = pci_dev_find(enables[i].vendor, enables[i].device);
956                 if (dev)
957                         break;
958         }
959
960         if (dev) {
961                 printf("Found chipset \"%s\", enabling flash write... ",
962                        enables[i].name);
963
964                 ret = enables[i].doit(dev, enables[i].name);
965                 if (ret)
966                         printf("FAILED!\n");
967                 else
968                         printf("OK.\n");
969         }
970
971         return ret;
972 }