Add board enable for the gigabyte ga_2761gxdk board
[coreboot.git] / util / flashrom / board_enable.c
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2005-2007 coresystems GmbH <stepan@coresystems.de>
5  * Copyright (C) 2006 Uwe Hermann <uwe@hermann-uwe.de>
6  * Copyright (C) 2007 Luc Verhaegen <libv@skynet.be>
7  * Copyright (C) 2007 Carl-Daniel Hailfinger
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; version 2 of the License.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 /*
24  * Contains the board specific flash enables.
25  */
26
27 #include <stdio.h>
28 #include <pci/pci.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include "flash.h"
32
33 /*
34  * Helper functions for many Winbond Super I/Os of the W836xx range.
35  */
36 /* Enter extended functions */
37 static void w836xx_ext_enter(uint16_t port)
38 {
39         outb(0x87, port);
40         outb(0x87, port);
41 }
42
43 /* Leave extended functions */
44 static void w836xx_ext_leave(uint16_t port)
45 {
46         outb(0xAA, port);
47 }
48
49 /* General functions for reading/writing Winbond Super I/Os. */
50 static unsigned char wbsio_read(uint16_t index, uint8_t reg)
51 {
52         outb(reg, index);
53         return inb(index + 1);
54 }
55
56 static void wbsio_write(uint16_t index, uint8_t reg, uint8_t data)
57 {
58         outb(reg, index);
59         outb(data, index + 1);
60 }
61
62 static void wbsio_mask(uint16_t index, uint8_t reg, uint8_t data, uint8_t mask)
63 {
64         uint8_t tmp;
65
66         outb(reg, index);
67         tmp = inb(index + 1) & ~mask;
68         outb(tmp | (data & mask), index + 1);
69 }
70
71 /**
72  * Winbond W83627HF: Raise GPIO24.
73  *
74  * Suited for:
75  *  - Agami Aruma
76  *  - IWILL DK8-HTX
77  */
78 static int w83627hf_gpio24_raise(uint16_t index, const char *name)
79 {
80         w836xx_ext_enter(index);
81
82         /* Is this the W83627HF? */
83         if (wbsio_read(index, 0x20) != 0x52) {  /* Super I/O device ID reg. */
84                 fprintf(stderr, "\nERROR: %s: W83627HF: Wrong ID: 0x%02X.\n",
85                         name, wbsio_read(index, 0x20));
86                 w836xx_ext_leave(index);
87                 return -1;
88         }
89
90         /* PIN89S: WDTO/GP24 multiplex -> GPIO24 */
91         wbsio_mask(index, 0x2B, 0x10, 0x10);
92
93         /* Select logical device 8: GPIO port 2 */
94         wbsio_write(index, 0x07, 0x08);
95
96         wbsio_mask(index, 0x30, 0x01, 0x01);    /* Activate logical device. */
97         wbsio_mask(index, 0xF0, 0x00, 0x10);    /* GPIO24 -> output */
98         wbsio_mask(index, 0xF2, 0x00, 0x10);    /* Clear GPIO24 inversion */
99         wbsio_mask(index, 0xF1, 0x10, 0x10);    /* Raise GPIO24 */
100
101         w836xx_ext_leave(index);
102
103         return 0;
104 }
105
106 static int w83627hf_gpio24_raise_2e(const char *name)
107 {
108         /* TODO: Typo? Shouldn't this be 0x2e? */
109         return w83627hf_gpio24_raise(0x2d, name);
110 }
111
112 /**
113  * Winbond W83627THF: GPIO 4, bit 4
114  *
115  * Suited for:
116  *  - MSI K8N-NEO3
117  */
118 static int w83627thf_gpio4_4_raise(uint16_t index, const char *name)
119 {
120         w836xx_ext_enter(index);
121
122         /* Is this the W83627THF? */
123         if (wbsio_read(index, 0x20) != 0x82) {  /* Super I/O device ID reg. */
124                 fprintf(stderr, "\nERROR: %s: W83627THF: Wrong ID: 0x%02X.\n",
125                         name, wbsio_read(index, 0x20));
126                 w836xx_ext_leave(index);
127                 return -1;
128         }
129
130         /* PINxxxxS: GPIO4/bit 4 multiplex -> GPIOXXX */
131
132         wbsio_write(index, 0x07, 0x09);      /* Select LDN 9: GPIO port 4 */
133         wbsio_mask(index, 0x30, 0x02, 0x02); /* Activate logical device. */
134         wbsio_mask(index, 0xF4, 0x00, 0x10); /* GPIO4 bit 4 -> output */
135         wbsio_mask(index, 0xF6, 0x00, 0x10); /* Clear GPIO4 bit 4 inversion */
136         wbsio_mask(index, 0xF5, 0x10, 0x10); /* Raise GPIO4 bit 4 */
137
138         w836xx_ext_leave(index);
139
140         return 0;
141 }
142
143 static int w83627thf_gpio4_4_raise_4e(const char *name)
144 {
145         return w83627thf_gpio4_4_raise(0x4e, name);
146 }
147
148 /**
149  * Suited for VIAs EPIA M and MII, and maybe other CLE266 based EPIAs.
150  *
151  * We don't need to do this when using LinuxBIOS, GPIO15 is never lowered there.
152  */
153 static int board_via_epia_m(const char *name)
154 {
155         struct pci_dev *dev;
156         uint16_t base;
157         uint8_t val;
158
159         dev = pci_dev_find(0x1106, 0x3177);     /* VT8235 ISA bridge */
160         if (!dev) {
161                 fprintf(stderr, "\nERROR: VT8235 ISA bridge not found.\n");
162                 return -1;
163         }
164
165         /* GPIO12-15 -> output */
166         val = pci_read_byte(dev, 0xE4);
167         val |= 0x10;
168         pci_write_byte(dev, 0xE4, val);
169
170         /* Get Power Management IO address. */
171         base = pci_read_word(dev, 0x88) & 0xFF80;
172
173         /* Enable GPIO15 which is connected to write protect. */
174         val = inb(base + 0x4D);
175         val |= 0x80;
176         outb(val, base + 0x4D);
177
178         return 0;
179 }
180
181 /**
182  * Suited for:
183  *   - ASUS A7V8X-MX SE and A7V400-MX: AMD K7 + VIA KM400A + VT8235
184  *   - Tyan Tomcat K7M: AMD Geode NX + VIA KM400 + VT8237.
185  */
186 static int board_asus_a7v8x_mx(const char *name)
187 {
188         struct pci_dev *dev;
189         uint8_t val;
190
191         dev = pci_dev_find(0x1106, 0x3177);     /* VT8235 ISA bridge */
192         if (!dev)
193                 dev = pci_dev_find(0x1106, 0x3227);     /* VT8237 ISA bridge */
194         if (!dev) {
195                 fprintf(stderr, "\nERROR: VT823x ISA bridge not found.\n");
196                 return -1;
197         }
198
199         /* This bit is marked reserved actually. */
200         val = pci_read_byte(dev, 0x59);
201         val &= 0x7F;
202         pci_write_byte(dev, 0x59, val);
203
204         /* Raise ROM MEMW# line on Winbond W83697 Super I/O. */
205         w836xx_ext_enter(0x2E);
206
207         if (!(wbsio_read(0x2E, 0x24) & 0x02))   /* Flash ROM enabled? */
208                 wbsio_mask(0x2E, 0x24, 0x08, 0x08);     /* Enable MEMW#. */
209
210         w836xx_ext_leave(0x2E);
211
212         return 0;
213 }
214
215 /**
216  * Suited for ASUS P5A.
217  *
218  * This is rather nasty code, but there's no way to do this cleanly.
219  * We're basically talking to some unknown device on SMBus, my guess
220  * is that it is the Winbond W83781D that lives near the DIP BIOS.
221  */
222 static int board_asus_p5a(const char *name)
223 {
224         uint8_t tmp;
225         int i;
226
227 #define ASUSP5A_LOOP 5000
228
229         outb(0x00, 0xE807);
230         outb(0xEF, 0xE803);
231
232         outb(0xFF, 0xE800);
233
234         for (i = 0; i < ASUSP5A_LOOP; i++) {
235                 outb(0xE1, 0xFF);
236                 if (inb(0xE800) & 0x04)
237                         break;
238         }
239
240         if (i == ASUSP5A_LOOP) {
241                 printf("%s: Unable to contact device.\n", name);
242                 return -1;
243         }
244
245         outb(0x20, 0xE801);
246         outb(0x20, 0xE1);
247
248         outb(0xFF, 0xE802);
249
250         for (i = 0; i < ASUSP5A_LOOP; i++) {
251                 tmp = inb(0xE800);
252                 if (tmp & 0x70)
253                         break;
254         }
255
256         if ((i == ASUSP5A_LOOP) || !(tmp & 0x10)) {
257                 printf("%s: failed to read device.\n", name);
258                 return -1;
259         }
260
261         tmp = inb(0xE804);
262         tmp &= ~0x02;
263
264         outb(0x00, 0xE807);
265         outb(0xEE, 0xE803);
266
267         outb(tmp, 0xE804);
268
269         outb(0xFF, 0xE800);
270         outb(0xE1, 0xFF);
271
272         outb(0x20, 0xE801);
273         outb(0x20, 0xE1);
274
275         outb(0xFF, 0xE802);
276
277         for (i = 0; i < ASUSP5A_LOOP; i++) {
278                 tmp = inb(0xE800);
279                 if (tmp & 0x70)
280                         break;
281         }
282
283         if ((i == ASUSP5A_LOOP) || !(tmp & 0x10)) {
284                 printf("%s: failed to write to device.\n", name);
285                 return -1;
286         }
287
288         return 0;
289 }
290
291 static int board_ibm_x3455(const char *name)
292 {
293         uint8_t byte;
294
295         /* Set GPIO lines in the Broadcom HT-1000 southbridge. */
296         outb(0x45, 0xcd6);
297         byte = inb(0xcd7);
298         outb(byte | 0x20, 0xcd7);
299
300         return 0;
301 }
302
303 /**
304  * Suited for EPoX EP-BX3, and maybe some other Intel 440BX based boards.
305  */
306 static int board_epox_ep_bx3(const char *name)
307 {
308         uint8_t tmp;
309
310         /* Raise GPIO22. */
311         tmp = inb(0x4036);
312         outb(tmp, 0xEB);
313
314         tmp |= 0x40;
315
316         outb(tmp, 0x4036);
317         outb(tmp, 0xEB);
318
319         return 0;
320 }
321
322 /**
323  * Suited for Acorp 6A815EPD.
324  */
325 static int board_acorp_6a815epd(const char *name)
326 {
327         struct pci_dev *dev;
328         uint16_t port;
329         uint8_t val;
330
331         dev = pci_dev_find(0x8086, 0x2440);     /* Intel ICH2 LPC */
332         if (!dev) {
333                 fprintf(stderr, "\nERROR: ICH2 LPC bridge not found.\n");
334                 return -1;
335         }
336
337         /* Use GPIOBASE register to find where the GPIO is mapped. */
338         port = (pci_read_word(dev, 0x58) & 0xFFC0) + 0xE;
339
340         val = inb(port);
341         val |= 0x80; /* Top Block Lock -- pin 8 of PLCC32 */
342         val |= 0x40; /* Lower Blocks Lock -- pin 7 of PLCC32 */
343         outb(val, port);
344
345         return 0;
346 }
347
348 /**
349  * We use 2 sets of IDs here, you're free to choose which is which. This
350  * is to provide a very high degree of certainty when matching a board on
351  * the basis of subsystem/card IDs. As not every vendor handles
352  * subsystem/card IDs in a sane manner.
353  *
354  * Keep the second set NULLed if it should be ignored.
355  */
356 struct board_pciid_enable {
357         /* Any device, but make it sensible, like the ISA bridge. */
358         uint16_t first_vendor;
359         uint16_t first_device;
360         uint16_t first_card_vendor;
361         uint16_t first_card_device;
362
363         /* Any device, but make it sensible, like 
364          * the host bridge. May be NULL.
365          */
366         uint16_t second_vendor;
367         uint16_t second_device;
368         uint16_t second_card_vendor;
369         uint16_t second_card_device;
370
371         /* The vendor / part name from the LinuxBIOS table. */
372         const char *lb_vendor;
373         const char *lb_part;
374
375         const char *name;
376         int (*enable) (const char *name);
377 };
378
379 struct board_pciid_enable board_pciid_enables[] = {
380         {0x10de, 0x0360, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
381          "gigabyte", "m57sli", "GIGABYTE GA-M57SLI-S4", it87xx_probe_spi_flash},
382         {0x10de, 0x03e0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
383          "gigabyte", "m61p", "GIGABYTE GA-M61P-S3", it87xx_probe_spi_flash},
384         {0x1039, 0x0761, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
385          "gigabyte", "2761gxdk", "GIGABYTE GA-2761GXDK", it87xx_probe_spi_flash},
386         {0x1022, 0x7468, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
387          "iwill", "dk8_htx", "IWILL DK8-HTX", w83627hf_gpio24_raise_2e},
388         {0x10de, 0x005e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
389          "msi", "k8n-neo3", "MSI K8N Neo3", w83627thf_gpio4_4_raise_4e},
390         {0x1022, 0x746B, 0x1022, 0x36C0, 0x0000, 0x0000, 0x0000, 0x0000,
391          "AGAMI", "ARUMA", "agami Aruma", w83627hf_gpio24_raise_2e},
392         {0x1106, 0x3177, 0x1106, 0xAA01, 0x1106, 0x3123, 0x1106, 0xAA01,
393          NULL, NULL, "VIA EPIA M/MII/...", board_via_epia_m},
394         {0x1106, 0x3177, 0x1043, 0x80A1, 0x1106, 0x3205, 0x1043, 0x8118,
395          NULL, NULL, "ASUS A7V8-MX SE", board_asus_a7v8x_mx},
396         {0x8086, 0x1076, 0x8086, 0x1176, 0x1106, 0x3059, 0x10f1, 0x2498,
397          NULL, NULL, "Tyan Tomcat K7M", board_asus_a7v8x_mx},
398         {0x10B9, 0x1541, 0x0000, 0x0000, 0x10B9, 0x1533, 0x0000, 0x0000,
399          "asus", "p5a", "ASUS P5A", board_asus_p5a},
400         {0x1166, 0x0205, 0x1014, 0x0347, 0x0000, 0x0000, 0x0000, 0x0000,
401          "ibm", "x3455", "IBM x3455", board_ibm_x3455},
402         {0x8086, 0x7110, 0x0000, 0x0000, 0x8086, 0x7190, 0x0000, 0x0000,
403          "epox", "ep-bx3", "EPoX EP-BX3", board_epox_ep_bx3},
404         {0x8086, 0x1130, 0x0000, 0x0000, 0x105a, 0x0d30, 0x105a, 0x4d33,
405          "acorp", "6a815epd", "Acorp 6A815EPD", board_acorp_6a815epd},
406         {0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL}    /* Keep this */
407 };
408
409 /**
410  * Match boards on LinuxBIOS table gathered vendor and part name.
411  * Require main PCI IDs to match too as extra safety.
412  */
413 static struct board_pciid_enable *board_match_linuxbios_name(const char *vendor, const char *part)
414 {
415         struct board_pciid_enable *board = board_pciid_enables;
416
417         for (; board->name; board++) {
418                 if (!board->lb_vendor || strcmp(board->lb_vendor, vendor))
419                         continue;
420
421                 if (!board->lb_part || strcmp(board->lb_part, part))
422                         continue;
423
424                 if (!pci_dev_find(board->first_vendor, board->first_device))
425                         continue;
426
427                 if (board->second_vendor &&
428                         !pci_dev_find(board->second_vendor, board->second_device))
429                         continue;
430                 return board;
431         }
432
433         printf("NOT FOUND %s:%s\n", vendor, part);
434
435         return NULL;
436 }
437
438 /**
439  * Match boards on PCI IDs and subsystem IDs.
440  * Second set of IDs can be main only or missing completely.
441  */
442 static struct board_pciid_enable *board_match_pci_card_ids(void)
443 {
444         struct board_pciid_enable *board = board_pciid_enables;
445
446         for (; board->name; board++) {
447                 if (!board->first_card_vendor || !board->first_card_device)
448                         continue;
449
450                 if (!pci_card_find(board->first_vendor, board->first_device,
451                                         board->first_card_vendor,
452                                         board->first_card_device))
453                         continue;
454
455                 if (board->second_vendor) {
456                         if (board->second_card_vendor) {
457                                 if (!pci_card_find(board->second_vendor,
458                                                 board->second_device,
459                                                 board->second_card_vendor,
460                                                 board->second_card_device))
461                                         continue;
462                         } else {
463                                 if (!pci_dev_find(board->second_vendor,
464                                                         board->second_device))
465                                         continue;
466                         }
467                 }
468
469                 return board;
470         }
471
472         return NULL;
473 }
474
475 int board_flash_enable(const char *vendor, const char *part)
476 {
477         struct board_pciid_enable *board = NULL;
478         int ret = 0;
479
480         if (vendor && part)
481                 board = board_match_linuxbios_name(vendor, part);
482
483         if (!board)
484                 board = board_match_pci_card_ids();
485
486         if (board) {
487                 printf("Found board \"%s\": enabling flash write... ",
488                         board->name);
489
490                 ret = board->enable(board->name);
491                 if (ret)
492                         printf("FAILED!\n");
493                 else
494                         printf("OK.\n");
495         }
496
497         return ret;
498 }