Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 26 | /* |
| 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 */ |
| 33 | static void |
| 34 | w836xx_ext_enter(void) |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 35 | { |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 36 | outb(0x87, W836_INDEX); |
| 37 | outb(0x87, W836_INDEX); |
| 38 | } |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 39 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 40 | /* Leave extended functions */ |
| 41 | static void |
| 42 | w836xx_ext_leave(void) |
| 43 | { |
| 44 | outb(0xAA, W836_INDEX); |
| 45 | } |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 46 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 47 | /* General functions for read/writing WB SuperIOs */ |
| 48 | static unsigned char |
| 49 | wbsio_read(unsigned char index) |
| 50 | { |
| 51 | outb(index, W836_INDEX); |
| 52 | return inb(W836_DATA); |
| 53 | } |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 54 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 55 | static void |
| 56 | wbsio_write(unsigned char index, unsigned char data) |
| 57 | { |
| 58 | outb(index, W836_INDEX); |
| 59 | outb(data, W836_DATA); |
| 60 | } |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 61 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 62 | static void |
| 63 | wbsio_mask(unsigned char index, unsigned char data, |
| 64 | unsigned char mask) |
| 65 | { |
| 66 | unsigned char tmp; |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 67 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 68 | outb(index, W836_INDEX); |
| 69 | tmp = inb(W836_DATA) & ~mask; |
| 70 | outb(tmp | (data & mask), W836_DATA); |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 73 | /* |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 74 | * WinBond w83627hf: raise GPIO24. |
| 75 | * |
| 76 | * Suited for: |
| 77 | * * Agami Aruma |
| 78 | * * IWILL DK8-HTX |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 79 | */ |
| 80 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 81 | static int w83627hf_gpio24_raise(const char *name) |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 82 | { |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 83 | w836xx_ext_enter(); |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 84 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 85 | /* 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 Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 93 | /* PIN89S: WDTO/GP24 multiplex -> GPIO24 */ |
| 94 | wbsio_mask(0x2B, 0x10, 0x10); |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 95 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 96 | wbsio_write(0x07, 0x08); /* Select logical device 8: GPIO port 2 */ |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 97 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 98 | wbsio_mask(0x30, 0x01, 0x01); /* Activate logical device. */ |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 99 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 100 | wbsio_mask(0xF0, 0x00, 0x10); /* GPIO24 -> output */ |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 101 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 102 | wbsio_mask(0xF2, 0x00, 0x10); /* Clear GPIO24 inversion */ |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 103 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 104 | wbsio_mask(0xF1, 0x10, 0x10); /* Raise GPIO24 */ |
| 105 | |
| 106 | w836xx_ext_leave(); |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 107 | |
| 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 Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 117 | static int board_via_epia_m(const char *name) |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 118 | { |
| 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 Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 146 | * Suited for Asus A7V8X-MX SE and A7V400-MX. |
| 147 | * |
| 148 | */ |
| 149 | |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 150 | static int board_asus_a7v8x_mx(const char *name) |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 151 | { |
| 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 Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 166 | /* 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 Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 173 | |
| 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 | |
| 187 | struct 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 Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 207 | int (*enable)(const char *name); |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | struct board_pciid_enable board_pciid_enables[] = { |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 211 | { 0x1022, 0x7468, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, |
| 212 | "iwill", "dk8_htx", "IWILL DK8-HTX", w83627hf_gpio24_raise }, |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 213 | { 0x1022, 0x746B, 0x1022, 0x36C0, 0x0000, 0x0000, 0x0000, 0x0000, |
Luc Verhaegen | 7977f4e | 2007-05-04 04:47:04 +0000 | [diff] [blame^] | 214 | "AGAMI", "ARUMA", "agami Aruma", w83627hf_gpio24_raise }, |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 215 | { 0x1106, 0x3177, 0x1106, 0xAA01, 0x1106, 0x3123, 0x1106, 0xAA01, |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 216 | NULL, NULL, "VIA EPIA M/MII/...", board_via_epia_m }, |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 217 | { 0x1106, 0x3177, 0x1043, 0x80A1, 0x1106, 0x3205, 0x1043, 0x8118, |
| 218 | NULL, NULL, "ASUS A7V8-MX SE", board_asus_a7v8x_mx }, |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 219 | |
Mondrian Nuessle | aef1c7c | 2007-05-03 10:09:23 +0000 | [diff] [blame] | 220 | { 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL } /* Keep this */ |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 221 | }; |
| 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 | */ |
| 228 | static struct board_pciid_enable * |
| 229 | board_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 | */ |
| 255 | static 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 | */ |
| 291 | int 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 | } |