blob: ac8de9f40e3c2d31c0401a8339d1d6727167acbe [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
26/*
27 * Match on pci-ids, no report received, just data from the mainboard
28 * specific code:
29 * main: 0x1022:0x746B, which is the SMBUS controller.
30 * card: 0x1022:0x36C0...
31 */
32
33static int board_agami_aruma(char *name)
34{
35 /* Extended function index register, either 0x2e or 0x4e */
36#define EFIR 0x2e
37 /* Extended function data register, one plus the index reg. */
38#define EFDR EFIR + 1
39 char b;
40
41 /* Disable the flash write protect. The flash write protect is
42 * connected to the WinBond w83627hf GPIO 24.
43 */
44 outb(0x87, EFIR); /* sequence to unlock extended functions */
45 outb(0x87, EFIR);
46
47 outb(0x20, EFIR); /* SIO device ID register */
48 b = inb(EFDR);
49 printf_debug("\nW83627HF device ID = 0x%x\n",b);
50
51 if (b != 0x52) {
52 fprintf(stderr, "\nIncorrect device ID, aborting write protect disable\n");
53 return -1;
54 }
55
56 outb(0x2b, EFIR); /* GPIO multiplexed pin reg. */
57 b = inb(EFDR) | 0x10;
58 outb(0x2b, EFIR);
59 outb(b, EFDR); /* select GPIO 24 instead of WDTO */
60
61 outb(0x7, EFIR); /* logical device select */
62 outb(0x8, EFDR); /* point to device 8, GPIO port 2 */
63
64 outb(0x30, EFIR); /* logic device activation control */
65 outb(0x1, EFDR); /* activate */
66
67 outb(0xf0, EFIR); /* GPIO 20-27 I/O selection register */
68 b = inb(EFDR) & ~0x10;
69 outb(0xf0, EFIR);
70 outb(b, EFDR); /* set GPIO 24 as an output */
71
72 outb(0xf1, EFIR); /* GPIO 20-27 data register */
73 b = inb(EFDR) | 0x10;
74 outb(0xf1, EFIR);
75 outb(b, EFDR); /* set GPIO 24 */
76
77 outb(0xaa, EFIR); /* command to exit extended functions */
78
79 return 0;
80}
81
82/*
83 * Suited for VIAs EPIA M and MII, and maybe other CLE266 based EPIAs.
84 *
85 * We don't need to do this when using linuxbios, GPIO15 is never lowered there.
86 */
87
88static int board_via_epia_m(char *name)
89{
90 struct pci_dev *dev;
91 unsigned int base;
92 uint8_t val;
93
94 dev = pci_dev_find(0x1106, 0x3177); /* VT8235 ISA bridge */
95 if (!dev) {
96 fprintf(stderr, "\nERROR: VT8235 ISA Bridge not found.\n");
97 return -1;
98 }
99
100 /* GPIO12-15 -> output */
101 val = pci_read_byte(dev, 0xE4);
102 val |= 0x10;
103 pci_write_byte(dev, 0xE4, val);
104
105 /* Get Power Management IO address. */
106 base = pci_read_word(dev, 0x88) & 0xFF80;
107
108 /* enable GPIO15 which is connected to write protect. */
109 val = inb(base + 0x4D);
110 val |= 0x80;
111 outb(val, base + 0x4D);
112
113 return 0;
114}
115
116/*
117 * Winbond LPC super IO.
118 *
119 * Raises the ROM MEMW# line.
120 */
121
122static void w83697_rom_memw_enable(void)
123{
124 uint8_t val;
125
126 outb(0x87, 0x2E); /* enable extended functions */
127 outb(0x87, 0x2E);
128
129 outb(0x24, 0x2E); /* rom bits live here */
130
131 val = inb(0x2F);
132 if (!(val & 0x02)) /* flash rom enabled? */
133 outb(val | 0x08, 0x2F); /* enable MEMW# */
134
135 outb(0xAA, 0x2E); /* disable extended functions */
136}
137
138/*
139 * Suited for Asus A7V8X-MX SE and A7V400-MX.
140 *
141 */
142
143static int board_asus_a7v8x_mx(char *name)
144{
145 struct pci_dev *dev;
146 uint8_t val;
147
148 dev = pci_dev_find(0x1106, 0x3177); /* VT8235 ISA bridge */
149 if (!dev) {
150 fprintf(stderr, "\nERROR: VT8235 ISA Bridge not found.\n");
151 return -1;
152 }
153
154 /* This bit is marked reserved actually */
155 val = pci_read_byte(dev, 0x59);
156 val &= 0x7F;
157 pci_write_byte(dev, 0x59, val);
158
159 w83697_rom_memw_enable();
160
161 return 0;
162}
163
164/*
165 * We use 2 sets of ids here, you're free to choose which is which. This
166 * to provide a very high degree of certainty when matching a board on
167 * the basis of Subsystem/card ids. As not every vendor handles
168 * subsystem/card ids in a sane manner.
169 *
170 * Keep the second set nulled if it should be ignored.
171 *
172 */
173
174struct board_pciid_enable {
175 /* Any device, but make it sensible, like the isa bridge. */
176 uint16_t first_vendor;
177 uint16_t first_device;
178 uint16_t first_card_vendor;
179 uint16_t first_card_device;
180
181 /* Any device, but make it sensible, like
182 * the host bridge. May be NULL
183 */
184 uint16_t second_vendor;
185 uint16_t second_device;
186 uint16_t second_card_vendor;
187 uint16_t second_card_device;
188
189 /* From linuxbios table */
190 char *lb_vendor;
191 char *lb_part;
192
193 char *name;
194 int (*enable)(char *name);
195};
196
197struct board_pciid_enable board_pciid_enables[] = {
198 { 0x1022, 0x746B, 0x1022, 0x36C0, 0x0000, 0x0000, 0x0000, 0x0000,
199 "AGAMI", "ARUMA", "Agami Aruma", board_agami_aruma },
200 { 0x1106, 0x3177, 0x1106, 0xAA01, 0x1106, 0x3123, 0x1106, 0xAA01,
201 NULL, NULL, "VIA EPIA M/MII/...", board_via_epia_m },
202 { 0x1106, 0x3177, 0x1043, 0x80A1, 0x1106, 0x3205, 0x1043, 0x8118,
203 NULL, NULL, "Asus A7V8-MX SE", board_asus_a7v8x_mx },
204 { 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL } /* Keep this */
205
206};
207
208/*
209 * Match boards on linuxbios table gathered vendor and part name.
210 * Require main pci-ids to match too as extra safety.
211 *
212 */
213static struct board_pciid_enable *
214board_match_linuxbios_name(char *vendor, char *part)
215{
216 struct board_pciid_enable *board = board_pciid_enables;
217
218 for (; board->name; board++) {
219 if (!board->lb_vendor || strcmp(board->lb_vendor, vendor))
220 continue;
221
222 if (!board->lb_part || strcmp(board->lb_part, part))
223 continue;
224
225 if (!pci_dev_find(board->first_vendor, board->first_device))
226 continue;
227
228 if (board->second_vendor &&
229 !pci_dev_find(board->second_vendor, board->second_device))
230 continue;
231 return board;
232 }
233 return NULL;
234}
235
236/*
237 * Match boards on pci ids and subsystem ids.
238 * Second set of ids can be main only or missing completely.
239 */
240static struct board_pciid_enable *board_match_pci_card_ids(void)
241{
242 struct board_pciid_enable *board = board_pciid_enables;
243
244 for (; board->name; board++) {
245 if (!board->first_card_vendor || !board->first_card_device)
246 continue;
247
248 if (!pci_card_find(board->first_vendor, board->first_device,
249 board->first_card_vendor,
250 board->first_card_device))
251 continue;
252
253 if (board->second_vendor) {
254 if (board->second_card_vendor) {
255 if (!pci_card_find(board->second_vendor,
256 board->second_device,
257 board->second_card_vendor,
258 board->second_card_device))
259 continue;
260 } else {
261 if (!pci_dev_find(board->second_vendor,
262 board->second_device))
263 continue;
264 }
265 }
266
267 return board;
268 }
269
270 return NULL;
271}
272
273/*
274 *
275 */
276int board_flash_enable(char *vendor, char *part)
277{
278 struct board_pciid_enable *board = NULL;
279 int ret = 0;
280
281 if (vendor && part)
282 board = board_match_linuxbios_name(vendor, part);
283
284 if (!board)
285 board = board_match_pci_card_ids();
286
287 if (board) {
288 printf("Found board \"%s\": Enabling flash write... ",
289 board->name);
290
291 ret = board->enable(board->name);
292 if (ret)
293 printf("Failed!\n");
294 else
295 printf("OK.\n");
296 }
297
298 return ret;
299}