blob: 5a755614bea1d71f5b2170b2639789dd7279495a [file] [log] [blame]
Luc Verhaegen8e3a6002007-04-04 22:45:58 +00001/*
2 * flash rom utility: enable flash writes (board specific)
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 *
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/*
15 * Contains the board specific flash enables.
16 */
17
18#include <stdio.h>
19#include <pci/pci.h>
20#include <stdint.h>
21#include <string.h>
22
23#include "flash.h"
24#include "debug.h"
25
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000026/*
27 * Helper functions for many Winbond superIOs of the w836xx range.
28 */
29#define W836_INDEX 0x2E
30#define W836_DATA 0x2F
31
32/* Enter extended functions */
Uwe Hermanna7e05482007-05-09 10:17:44 +000033static void w836xx_ext_enter(void)
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000034{
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000035 outb(0x87, W836_INDEX);
36 outb(0x87, W836_INDEX);
37}
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000038
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000039/* Leave extended functions */
Uwe Hermanna7e05482007-05-09 10:17:44 +000040static void w836xx_ext_leave(void)
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000041{
42 outb(0xAA, W836_INDEX);
43}
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000044
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000045/* General functions for read/writing WB SuperIOs */
Uwe Hermanna7e05482007-05-09 10:17:44 +000046static unsigned char wbsio_read(unsigned char index)
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000047{
48 outb(index, W836_INDEX);
49 return inb(W836_DATA);
50}
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000051
Uwe Hermanna7e05482007-05-09 10:17:44 +000052static void wbsio_write(unsigned char index, unsigned char data)
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000053{
54 outb(index, W836_INDEX);
55 outb(data, W836_DATA);
56}
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000057
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000058static void
Uwe Hermanna7e05482007-05-09 10:17:44 +000059wbsio_mask(unsigned char index, unsigned char data, unsigned char mask)
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000060{
61 unsigned char tmp;
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000062
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000063 outb(index, W836_INDEX);
64 tmp = inb(W836_DATA) & ~mask;
65 outb(tmp | (data & mask), W836_DATA);
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000066}
67
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000068/*
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000069 * WinBond w83627hf: raise GPIO24.
70 *
71 * Suited for:
72 * * Agami Aruma
73 * * IWILL DK8-HTX
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000074 */
75
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000076static int w83627hf_gpio24_raise(const char *name)
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000077{
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000078 w836xx_ext_enter();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000079
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000080 /* Is this the w83627hf? */
Uwe Hermanna7e05482007-05-09 10:17:44 +000081 if (wbsio_read(0x20) != 0x52) { /* SIO device ID register */
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000082 fprintf(stderr, "\nERROR: %s: W83627HF: Wrong ID: 0x%02X.\n",
83 name, wbsio_read(0x20));
84 w836xx_ext_leave();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000085 return -1;
86 }
87
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000088 /* PIN89S: WDTO/GP24 multiplex -> GPIO24 */
89 wbsio_mask(0x2B, 0x10, 0x10);
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000090
Uwe Hermanna7e05482007-05-09 10:17:44 +000091 wbsio_write(0x07, 0x08); /* Select logical device 8: GPIO port 2 */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000092
Uwe Hermanna7e05482007-05-09 10:17:44 +000093 wbsio_mask(0x30, 0x01, 0x01); /* Activate logical device. */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000094
Uwe Hermanna7e05482007-05-09 10:17:44 +000095 wbsio_mask(0xF0, 0x00, 0x10); /* GPIO24 -> output */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000096
Uwe Hermanna7e05482007-05-09 10:17:44 +000097 wbsio_mask(0xF2, 0x00, 0x10); /* Clear GPIO24 inversion */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000098
Uwe Hermanna7e05482007-05-09 10:17:44 +000099 wbsio_mask(0xF1, 0x10, 0x10); /* Raise GPIO24 */
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000100
101 w836xx_ext_leave();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000102
103 return 0;
104}
105
106/*
107 * Suited for VIAs EPIA M and MII, and maybe other CLE266 based EPIAs.
108 *
109 * We don't need to do this when using linuxbios, GPIO15 is never lowered there.
110 */
111
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000112static int board_via_epia_m(const char *name)
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000113{
Uwe Hermanna7e05482007-05-09 10:17:44 +0000114 struct pci_dev *dev;
115 unsigned int base;
116 uint8_t val;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000117
Uwe Hermanna7e05482007-05-09 10:17:44 +0000118 dev = pci_dev_find(0x1106, 0x3177); /* VT8235 ISA bridge */
119 if (!dev) {
120 fprintf(stderr, "\nERROR: VT8235 ISA Bridge not found.\n");
121 return -1;
122 }
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000123
Uwe Hermanna7e05482007-05-09 10:17:44 +0000124 /* GPIO12-15 -> output */
125 val = pci_read_byte(dev, 0xE4);
126 val |= 0x10;
127 pci_write_byte(dev, 0xE4, val);
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000128
Uwe Hermanna7e05482007-05-09 10:17:44 +0000129 /* Get Power Management IO address. */
130 base = pci_read_word(dev, 0x88) & 0xFF80;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000131
Uwe Hermanna7e05482007-05-09 10:17:44 +0000132 /* enable GPIO15 which is connected to write protect. */
133 val = inb(base + 0x4D);
134 val |= 0x80;
135 outb(val, base + 0x4D);
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000136
Uwe Hermanna7e05482007-05-09 10:17:44 +0000137 return 0;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000138}
139
140/*
Luc Verhaegen32707542007-07-04 17:51:49 +0000141 * Suited for:
142 * ASUS A7V8X-MX SE and A7V400-MX: AMD K7 + VIA KM400A + VT8235
143 * Tyan Tomcat K7M: AMD Geode NX + VIA KM400 + VT8237.
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000144 */
145
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000146static int board_asus_a7v8x_mx(const char *name)
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000147{
Uwe Hermanna7e05482007-05-09 10:17:44 +0000148 struct pci_dev *dev;
149 uint8_t val;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000150
Uwe Hermanna7e05482007-05-09 10:17:44 +0000151 dev = pci_dev_find(0x1106, 0x3177); /* VT8235 ISA bridge */
Luc Verhaegen32707542007-07-04 17:51:49 +0000152 if (!dev)
153 dev = pci_dev_find(0x1106, 0x3227); /* VT8237 ISA bridge */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000154 if (!dev) {
Luc Verhaegen32707542007-07-04 17:51:49 +0000155 fprintf(stderr, "\nERROR: VT823x ISA bridge not found.\n");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000156 return -1;
157 }
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000158
Uwe Hermanna7e05482007-05-09 10:17:44 +0000159 /* This bit is marked reserved actually */
160 val = pci_read_byte(dev, 0x59);
161 val &= 0x7F;
162 pci_write_byte(dev, 0x59, val);
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000163
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000164 /* Raise ROM MEMW# line on Winbond w83697 SuperIO */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000165 w836xx_ext_enter();
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000166
Uwe Hermanna7e05482007-05-09 10:17:44 +0000167 if (!(wbsio_read(0x24) & 0x02)) /* flash rom enabled? */
168 wbsio_mask(0x24, 0x08, 0x08); /* enable MEMW# */
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000169
170 w836xx_ext_leave();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000171
Uwe Hermanna7e05482007-05-09 10:17:44 +0000172 return 0;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000173}
174
175/*
Luc Verhaegen6b141752007-05-20 16:16:13 +0000176 * Suited for ASUS P5A.
177 *
178 * This is rather nasty code, but there's no way to do this cleanly.
179 * We're basically talking to some unknown device on SMBus, my guess
180 * is that it is the Winbond W83781D that lives near the DIP BIOS.
181 */
182
183static int board_asus_p5a(const char *name)
184{
185 uint8_t tmp;
186 int i;
187
188#define ASUSP5A_LOOP 5000
189
190 outb(0x00, 0xE807);
191 outb(0xEF, 0xE803);
192
193 outb(0xFF, 0xE800);
194
195 for (i = 0; i < ASUSP5A_LOOP; i++) {
196 outb(0xE1, 0xFF);
197 if (inb(0xE800) & 0x04)
198 break;
199 }
200
201 if (i == ASUSP5A_LOOP) {
202 printf("%s: Unable to contact device.\n", name);
203 return -1;
204 }
205
206 outb(0x20, 0xE801);
207 outb(0x20, 0xE1);
208
209 outb(0xFF, 0xE802);
210
211 for (i = 0; i < ASUSP5A_LOOP; i++) {
212 tmp = inb(0xE800);
213 if (tmp & 0x70)
214 break;
215 }
216
217 if ((i == ASUSP5A_LOOP) || !(tmp & 0x10)) {
218 printf("%s: failed to read device.\n", name);
219 return -1;
220 }
221
222 tmp = inb(0xE804);
223 tmp &= ~0x02;
224
225 outb(0x00, 0xE807);
226 outb(0xEE, 0xE803);
227
228 outb(tmp, 0xE804);
229
230 outb(0xFF, 0xE800);
231 outb(0xE1, 0xFF);
232
233 outb(0x20, 0xE801);
234 outb(0x20, 0xE1);
235
236 outb(0xFF, 0xE802);
237
238 for (i = 0; i < ASUSP5A_LOOP; i++) {
239 tmp = inb(0xE800);
240 if (tmp & 0x70)
241 break;
242 }
243
244 if ((i == ASUSP5A_LOOP) || !(tmp & 0x10)) {
245 printf("%s: failed to write to device.\n", name);
246 return -1;
247 }
248
249 return 0;
250}
251
Stefan Reinauer1c283f42007-06-05 12:51:52 +0000252static int board_ibm_x3455(const char *name)
253{
254 uint8_t byte;
255
Uwe Hermanne823ee02007-06-05 15:02:18 +0000256 /* Set GPIO lines in the Broadcom HT-1000 southbridge. */
Stefan Reinauer1c283f42007-06-05 12:51:52 +0000257 outb(0x45, 0xcd6);
258 byte = inb(0xcd7);
Uwe Hermanne823ee02007-06-05 15:02:18 +0000259 outb(byte | 0x20, 0xcd7);
Stefan Reinauer1c283f42007-06-05 12:51:52 +0000260
261 return 0;
262}
263
Luc Verhaegen6b141752007-05-20 16:16:13 +0000264/*
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000265 * We use 2 sets of ids here, you're free to choose which is which. This
266 * to provide a very high degree of certainty when matching a board on
267 * the basis of Subsystem/card ids. As not every vendor handles
268 * subsystem/card ids in a sane manner.
269 *
270 * Keep the second set nulled if it should be ignored.
271 *
272 */
273
274struct board_pciid_enable {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000275 /* Any device, but make it sensible, like the isa bridge. */
276 uint16_t first_vendor;
277 uint16_t first_device;
278 uint16_t first_card_vendor;
279 uint16_t first_card_device;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000280
Uwe Hermanna7e05482007-05-09 10:17:44 +0000281 /* Any device, but make it sensible, like
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000282 * the host bridge. May be NULL
283 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000284 uint16_t second_vendor;
285 uint16_t second_device;
286 uint16_t second_card_vendor;
287 uint16_t second_card_device;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000288
Uwe Hermanna7e05482007-05-09 10:17:44 +0000289 /* From linuxbios table */
290 char *lb_vendor;
291 char *lb_part;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000292
Uwe Hermanna7e05482007-05-09 10:17:44 +0000293 char *name;
294 int (*enable) (const char *name);
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000295};
296
297struct board_pciid_enable board_pciid_enables[] = {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000298 {0x1022, 0x7468, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
299 "iwill", "dk8_htx", "IWILL DK8-HTX", w83627hf_gpio24_raise},
300 {0x1022, 0x746B, 0x1022, 0x36C0, 0x0000, 0x0000, 0x0000, 0x0000,
301 "AGAMI", "ARUMA", "agami Aruma", w83627hf_gpio24_raise},
302 {0x1106, 0x3177, 0x1106, 0xAA01, 0x1106, 0x3123, 0x1106, 0xAA01,
303 NULL, NULL, "VIA EPIA M/MII/...", board_via_epia_m},
304 {0x1106, 0x3177, 0x1043, 0x80A1, 0x1106, 0x3205, 0x1043, 0x8118,
305 NULL, NULL, "ASUS A7V8-MX SE", board_asus_a7v8x_mx},
Luc Verhaegen32707542007-07-04 17:51:49 +0000306 {0x8086, 0x1076, 0x8086, 0x1176, 0x1106, 0x3059, 0x10f1, 0x2498,
307 NULL, NULL, "Tyan Tomcat K7M", board_asus_a7v8x_mx},
Luc Verhaegen6b141752007-05-20 16:16:13 +0000308 {0x10B9, 0x1541, 0x0000, 0x0000, 0x10B9, 0x1533, 0x0000, 0x0000,
309 "asus", "p5a", "ASUS P5A", board_asus_p5a},
Stefan Reinauer1c283f42007-06-05 12:51:52 +0000310 {0x1166, 0x0205, 0x1014, 0x0347, 0x0000, 0x0000, 0x0000, 0x0000,
311 "ibm", "x3455", "IBM x3455", board_ibm_x3455},
Uwe Hermanna7e05482007-05-09 10:17:44 +0000312 {0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL} /* Keep this */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000313};
314
315/*
316 * Match boards on linuxbios table gathered vendor and part name.
317 * Require main pci-ids to match too as extra safety.
318 *
319 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000320static struct board_pciid_enable *board_match_linuxbios_name(char *vendor,
321 char *part)
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000322{
Uwe Hermanna7e05482007-05-09 10:17:44 +0000323 struct board_pciid_enable *board = board_pciid_enables;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000324
Uwe Hermanna7e05482007-05-09 10:17:44 +0000325 for (; board->name; board++) {
326 if (!board->lb_vendor || strcmp(board->lb_vendor, vendor))
327 continue;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000328
Uwe Hermanna7e05482007-05-09 10:17:44 +0000329 if (!board->lb_part || strcmp(board->lb_part, part))
330 continue;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000331
Uwe Hermanna7e05482007-05-09 10:17:44 +0000332 if (!pci_dev_find(board->first_vendor, board->first_device))
333 continue;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000334
Uwe Hermanna7e05482007-05-09 10:17:44 +0000335 if (board->second_vendor &&
336 !pci_dev_find(board->second_vendor, board->second_device))
337 continue;
338 return board;
339 }
340 return NULL;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000341}
342
343/*
344 * Match boards on pci ids and subsystem ids.
345 * Second set of ids can be main only or missing completely.
346 */
347static struct board_pciid_enable *board_match_pci_card_ids(void)
348{
Uwe Hermanna7e05482007-05-09 10:17:44 +0000349 struct board_pciid_enable *board = board_pciid_enables;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000350
Uwe Hermanna7e05482007-05-09 10:17:44 +0000351 for (; board->name; board++) {
352 if (!board->first_card_vendor || !board->first_card_device)
353 continue;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000354
Uwe Hermanna7e05482007-05-09 10:17:44 +0000355 if (!pci_card_find(board->first_vendor, board->first_device,
356 board->first_card_vendor,
357 board->first_card_device))
358 continue;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000359
Uwe Hermanna7e05482007-05-09 10:17:44 +0000360 if (board->second_vendor) {
361 if (board->second_card_vendor) {
362 if (!pci_card_find(board->second_vendor,
363 board->second_device,
364 board->second_card_vendor,
365 board->second_card_device))
366 continue;
367 } else {
368 if (!pci_dev_find(board->second_vendor,
369 board->second_device))
370 continue;
371 }
372 }
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000373
Uwe Hermanna7e05482007-05-09 10:17:44 +0000374 return board;
375 }
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000376
Uwe Hermanna7e05482007-05-09 10:17:44 +0000377 return NULL;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000378}
379
380/*
381 *
382 */
383int board_flash_enable(char *vendor, char *part)
384{
Uwe Hermanna7e05482007-05-09 10:17:44 +0000385 struct board_pciid_enable *board = NULL;
386 int ret = 0;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000387
Uwe Hermanna7e05482007-05-09 10:17:44 +0000388 if (vendor && part)
389 board = board_match_linuxbios_name(vendor, part);
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000390
Uwe Hermanna7e05482007-05-09 10:17:44 +0000391 if (!board)
392 board = board_match_pci_card_ids();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000393
Uwe Hermanna7e05482007-05-09 10:17:44 +0000394 if (board) {
395 printf("Found board \"%s\": Enabling flash write... ",
396 board->name);
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000397
Uwe Hermanna7e05482007-05-09 10:17:44 +0000398 ret = board->enable(board->name);
399 if (ret)
400 printf("Failed!\n");
401 else
402 printf("OK.\n");
403 }
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000404
Uwe Hermanna7e05482007-05-09 10:17:44 +0000405 return ret;
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000406}