blob: 89d66ad1b5d99505c62b6e4bde22f6779a781f3d [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 */
33static void
34w836xx_ext_enter(void)
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000035{
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000036 outb(0x87, W836_INDEX);
37 outb(0x87, W836_INDEX);
38}
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000039
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000040/* Leave extended functions */
41static void
42w836xx_ext_leave(void)
43{
44 outb(0xAA, W836_INDEX);
45}
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000046
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000047/* General functions for read/writing WB SuperIOs */
48static unsigned char
49wbsio_read(unsigned char index)
50{
51 outb(index, W836_INDEX);
52 return inb(W836_DATA);
53}
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000054
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000055static void
56wbsio_write(unsigned char index, unsigned char data)
57{
58 outb(index, W836_INDEX);
59 outb(data, W836_DATA);
60}
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000061
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000062static void
63wbsio_mask(unsigned char index, unsigned char data,
64 unsigned char mask)
65{
66 unsigned char tmp;
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000067
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000068 outb(index, W836_INDEX);
69 tmp = inb(W836_DATA) & ~mask;
70 outb(tmp | (data & mask), W836_DATA);
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +000071}
72
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000073/*
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000074 * WinBond w83627hf: raise GPIO24.
75 *
76 * Suited for:
77 * * Agami Aruma
78 * * IWILL DK8-HTX
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000079 */
80
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000081static int w83627hf_gpio24_raise(const char *name)
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000082{
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000083 w836xx_ext_enter();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000084
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000085 /* Is this the w83627hf? */
86 if (wbsio_read(0x20) != 0x52) { /* SIO device ID register */
87 fprintf(stderr, "\nERROR: %s: W83627HF: Wrong ID: 0x%02X.\n",
88 name, wbsio_read(0x20));
89 w836xx_ext_leave();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000090 return -1;
91 }
92
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000093 /* PIN89S: WDTO/GP24 multiplex -> GPIO24 */
94 wbsio_mask(0x2B, 0x10, 0x10);
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000095
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000096 wbsio_write(0x07, 0x08); /* Select logical device 8: GPIO port 2 */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000097
Luc Verhaegen7977f4e2007-05-04 04:47:04 +000098 wbsio_mask(0x30, 0x01, 0x01); /* Activate logical device. */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000099
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000100 wbsio_mask(0xF0, 0x00, 0x10); /* GPIO24 -> output */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000101
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000102 wbsio_mask(0xF2, 0x00, 0x10); /* Clear GPIO24 inversion */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000103
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000104 wbsio_mask(0xF1, 0x10, 0x10); /* Raise GPIO24 */
105
106 w836xx_ext_leave();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000107
108 return 0;
109}
110
111/*
112 * Suited for VIAs EPIA M and MII, and maybe other CLE266 based EPIAs.
113 *
114 * We don't need to do this when using linuxbios, GPIO15 is never lowered there.
115 */
116
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000117static int board_via_epia_m(const char *name)
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000118{
119 struct pci_dev *dev;
120 unsigned int base;
121 uint8_t val;
122
123 dev = pci_dev_find(0x1106, 0x3177); /* VT8235 ISA bridge */
124 if (!dev) {
125 fprintf(stderr, "\nERROR: VT8235 ISA Bridge not found.\n");
126 return -1;
127 }
128
129 /* GPIO12-15 -> output */
130 val = pci_read_byte(dev, 0xE4);
131 val |= 0x10;
132 pci_write_byte(dev, 0xE4, val);
133
134 /* Get Power Management IO address. */
135 base = pci_read_word(dev, 0x88) & 0xFF80;
136
137 /* enable GPIO15 which is connected to write protect. */
138 val = inb(base + 0x4D);
139 val |= 0x80;
140 outb(val, base + 0x4D);
141
142 return 0;
143}
144
145/*
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000146 * Suited for Asus A7V8X-MX SE and A7V400-MX.
147 *
148 */
149
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000150static int board_asus_a7v8x_mx(const char *name)
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000151{
152 struct pci_dev *dev;
153 uint8_t val;
154
155 dev = pci_dev_find(0x1106, 0x3177); /* VT8235 ISA bridge */
156 if (!dev) {
157 fprintf(stderr, "\nERROR: VT8235 ISA Bridge not found.\n");
158 return -1;
159 }
160
161 /* This bit is marked reserved actually */
162 val = pci_read_byte(dev, 0x59);
163 val &= 0x7F;
164 pci_write_byte(dev, 0x59, val);
165
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000166 /* Raise ROM MEMW# line on Winbond w83697 SuperIO */
167 w836xx_ext_enter();
168
169 if (!(wbsio_read(0x24) & 0x02)) /* flash rom enabled? */
170 wbsio_mask(0x24, 0x08, 0x08); /* enable MEMW# */
171
172 w836xx_ext_leave();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000173
174 return 0;
175}
176
177/*
178 * We use 2 sets of ids here, you're free to choose which is which. This
179 * to provide a very high degree of certainty when matching a board on
180 * the basis of Subsystem/card ids. As not every vendor handles
181 * subsystem/card ids in a sane manner.
182 *
183 * Keep the second set nulled if it should be ignored.
184 *
185 */
186
187struct board_pciid_enable {
188 /* Any device, but make it sensible, like the isa bridge. */
189 uint16_t first_vendor;
190 uint16_t first_device;
191 uint16_t first_card_vendor;
192 uint16_t first_card_device;
193
194 /* Any device, but make it sensible, like
195 * the host bridge. May be NULL
196 */
197 uint16_t second_vendor;
198 uint16_t second_device;
199 uint16_t second_card_vendor;
200 uint16_t second_card_device;
201
202 /* From linuxbios table */
203 char *lb_vendor;
204 char *lb_part;
205
206 char *name;
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000207 int (*enable)(const char *name);
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000208};
209
210struct board_pciid_enable board_pciid_enables[] = {
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000211 { 0x1022, 0x7468, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
212 "iwill", "dk8_htx", "IWILL DK8-HTX", w83627hf_gpio24_raise },
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +0000213 { 0x1022, 0x746B, 0x1022, 0x36C0, 0x0000, 0x0000, 0x0000, 0x0000,
Luc Verhaegen7977f4e2007-05-04 04:47:04 +0000214 "AGAMI", "ARUMA", "agami Aruma", w83627hf_gpio24_raise },
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +0000215 { 0x1106, 0x3177, 0x1106, 0xAA01, 0x1106, 0x3123, 0x1106, 0xAA01,
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000216 NULL, NULL, "VIA EPIA M/MII/...", board_via_epia_m },
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +0000217 { 0x1106, 0x3177, 0x1043, 0x80A1, 0x1106, 0x3205, 0x1043, 0x8118,
218 NULL, NULL, "ASUS A7V8-MX SE", board_asus_a7v8x_mx },
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000219
Mondrian Nuessleaef1c7c2007-05-03 10:09:23 +0000220 { 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL } /* Keep this */
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000221};
222
223/*
224 * Match boards on linuxbios table gathered vendor and part name.
225 * Require main pci-ids to match too as extra safety.
226 *
227 */
228static struct board_pciid_enable *
229board_match_linuxbios_name(char *vendor, char *part)
230{
231 struct board_pciid_enable *board = board_pciid_enables;
232
233 for (; board->name; board++) {
234 if (!board->lb_vendor || strcmp(board->lb_vendor, vendor))
235 continue;
236
237 if (!board->lb_part || strcmp(board->lb_part, part))
238 continue;
239
240 if (!pci_dev_find(board->first_vendor, board->first_device))
241 continue;
242
243 if (board->second_vendor &&
244 !pci_dev_find(board->second_vendor, board->second_device))
245 continue;
246 return board;
247 }
248 return NULL;
249}
250
251/*
252 * Match boards on pci ids and subsystem ids.
253 * Second set of ids can be main only or missing completely.
254 */
255static struct board_pciid_enable *board_match_pci_card_ids(void)
256{
257 struct board_pciid_enable *board = board_pciid_enables;
258
259 for (; board->name; board++) {
260 if (!board->first_card_vendor || !board->first_card_device)
261 continue;
262
263 if (!pci_card_find(board->first_vendor, board->first_device,
264 board->first_card_vendor,
265 board->first_card_device))
266 continue;
267
268 if (board->second_vendor) {
269 if (board->second_card_vendor) {
270 if (!pci_card_find(board->second_vendor,
271 board->second_device,
272 board->second_card_vendor,
273 board->second_card_device))
274 continue;
275 } else {
276 if (!pci_dev_find(board->second_vendor,
277 board->second_device))
278 continue;
279 }
280 }
281
282 return board;
283 }
284
285 return NULL;
286}
287
288/*
289 *
290 */
291int board_flash_enable(char *vendor, char *part)
292{
293 struct board_pciid_enable *board = NULL;
294 int ret = 0;
295
296 if (vendor && part)
297 board = board_match_linuxbios_name(vendor, part);
298
299 if (!board)
300 board = board_match_pci_card_ids();
301
302 if (board) {
303 printf("Found board \"%s\": Enabling flash write... ",
304 board->name);
305
306 ret = board->enable(board->name);
307 if (ret)
308 printf("Failed!\n");
309 else
310 printf("OK.\n");
311 }
312
313 return ret;
314}