Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1 | #include <sys/io.h> |
| 2 | #include <stdio.h> |
| 3 | #include <pci/pci.h> |
| 4 | #include <stdlib.h> |
| 5 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 6 | static int enable_flash_sis630(struct pci_dev *dev, char *name) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 7 | { |
| 8 | char b; |
| 9 | |
| 10 | /* get io privilege access PCI configuration space */ |
| 11 | if (iopl(3) != 0) { |
| 12 | perror("Can not set io priviliage"); |
| 13 | exit(1); |
| 14 | } |
| 15 | |
| 16 | /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */ |
| 17 | outl(0x80000840, 0x0cf8); |
| 18 | b = inb(0x0cfc) | 0x0b; |
| 19 | outb(b, 0xcfc); |
| 20 | /* Flash write enable on SiS 540/630 */ |
| 21 | outl(0x80000845, 0x0cf8); |
| 22 | b = inb(0x0cfd) | 0x40; |
| 23 | outb(b, 0xcfd); |
| 24 | |
| 25 | /* The same thing on SiS 950 SuperIO side */ |
| 26 | outb(0x87, 0x2e); |
| 27 | outb(0x01, 0x2e); |
| 28 | outb(0x55, 0x2e); |
| 29 | outb(0x55, 0x2e); |
| 30 | |
| 31 | if (inb(0x2f) != 0x87) { |
| 32 | outb(0x87, 0x4e); |
| 33 | outb(0x01, 0x4e); |
| 34 | outb(0x55, 0x4e); |
| 35 | outb(0xaa, 0x4e); |
| 36 | if (inb(0x4f) != 0x87) { |
| 37 | printf("Can not access SiS 950\n"); |
| 38 | return -1; |
| 39 | } |
| 40 | outb(0x24, 0x4e); |
| 41 | b = inb(0x4f) | 0xfc; |
| 42 | outb(0x24, 0x4e); |
| 43 | outb(b, 0x4f); |
| 44 | outb(0x02, 0x4e); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 45 | outb(0x02, 0x4f); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | outb(0x24, 0x2e); |
| 49 | printf("2f is %#x\n", inb(0x2f)); |
| 50 | b = inb(0x2f) | 0xfc; |
| 51 | outb(0x24, 0x2e); |
| 52 | outb(b, 0x2f); |
| 53 | |
| 54 | outb(0x02, 0x2e); |
| 55 | outb(0x02, 0x2f); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int enable_flash_e7500(struct pci_dev *dev, char *name) |
| 61 | { |
| 62 | /* register 4e.b gets or'ed with one */ |
| 63 | unsigned char old, new; |
| 64 | /* if it fails, it fails. There are so many variations of broken mobos |
| 65 | * that it is hard to argue that we should quit at this point. |
| 66 | */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 67 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 68 | old = pci_read_byte(dev, 0x4e); |
| 69 | |
| 70 | new = old | 1; |
| 71 | |
| 72 | if (new == old) |
| 73 | return 0; |
| 74 | |
| 75 | pci_write_byte(dev, 0x4e, new); |
| 76 | |
| 77 | if (pci_read_byte(dev, 0x4e) != new) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 78 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 79 | 0x4e, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 80 | return -1; |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
Ronald G. Minnich | 6a96741 | 2004-09-28 20:09:06 +0000 | [diff] [blame] | 85 | static int enable_flash_ich4(struct pci_dev *dev, char *name) |
| 86 | { |
| 87 | /* register 4e.b gets or'ed with one */ |
| 88 | unsigned char old, new; |
| 89 | /* if it fails, it fails. There are so many variations of broken mobos |
| 90 | * that it is hard to argue that we should quit at this point. |
| 91 | */ |
| 92 | |
| 93 | old = pci_read_byte(dev, 0x4e); |
| 94 | |
| 95 | new = old | 1; |
| 96 | |
| 97 | if (new == old) |
| 98 | return 0; |
| 99 | |
| 100 | pci_write_byte(dev, 0x4e, new); |
| 101 | |
| 102 | if (pci_read_byte(dev, 0x4e) != new) { |
| 103 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 104 | 0x4e, new, name); |
| 105 | return -1; |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 110 | static int enable_flash_vt8235(struct pci_dev *dev, char *name) |
| 111 | { |
| 112 | unsigned char old, new, val; |
| 113 | unsigned int base; |
| 114 | int ok; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 115 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 116 | /* get io privilege access PCI configuration space */ |
| 117 | if (iopl(3) != 0) { |
| 118 | perror("Can not set io priviliage"); |
| 119 | exit(1); |
| 120 | } |
| 121 | |
| 122 | old = pci_read_byte(dev, 0x40); |
| 123 | |
| 124 | new = old | 0x10; |
| 125 | |
| 126 | if (new == old) |
| 127 | return 0; |
| 128 | |
| 129 | ok = pci_write_byte(dev, 0x40, new); |
| 130 | if (ok != 0) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 131 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 132 | old, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* enable GPIO15 which is connected to write protect. */ |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 136 | base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 137 | val = inb(base + 0x4d); |
| 138 | val |= 0x80; |
| 139 | outb(val, base + 0x4d); |
| 140 | |
| 141 | if (ok != 0) { |
| 142 | return -1; |
| 143 | } else { |
| 144 | return 0; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static int enable_flash_vt8231(struct pci_dev *dev, char *name) |
| 149 | { |
| 150 | unsigned char val; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 151 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 152 | val = pci_read_byte(dev, 0x40); |
| 153 | val |= 0x10; |
| 154 | pci_write_byte(dev, 0x40, val); |
| 155 | |
| 156 | if (pci_read_byte(dev, 0x40) != val) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 157 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 158 | 0x40, val, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 159 | return -1; |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int enable_flash_cs5530(struct pci_dev *dev, char *name) |
| 165 | { |
| 166 | unsigned char new; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 167 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 168 | pci_write_byte(dev, 0x52, 0xee); |
| 169 | |
| 170 | new = pci_read_byte(dev, 0x52); |
| 171 | |
| 172 | if (new != 0xee) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 173 | printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 174 | 0x52, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 175 | return -1; |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int enable_flash_sc1100(struct pci_dev *dev, char *name) |
| 181 | { |
| 182 | unsigned char new; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 183 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 184 | pci_write_byte(dev, 0x52, 0xee); |
| 185 | |
| 186 | new = pci_read_byte(dev, 0x52); |
| 187 | |
| 188 | if (new != 0xee) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 189 | printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 190 | 0x52, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 191 | return -1; |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int enable_flash_sis5595(struct pci_dev *dev, char *name) |
| 197 | { |
| 198 | unsigned char new, newer; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 199 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 200 | new = pci_read_byte(dev, 0x45); |
| 201 | |
| 202 | /* clear bit 5 */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 203 | new &= (~0x20); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 204 | /* set bit 2 */ |
| 205 | new |= 0x4; |
| 206 | |
| 207 | pci_write_byte(dev, 0x45, new); |
| 208 | |
| 209 | newer = pci_read_byte(dev, 0x45); |
| 210 | if (newer != new) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 211 | printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 212 | 0x45, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 213 | printf("Stuck at 0x%x\n", newer); |
| 214 | return -1; |
| 215 | } |
| 216 | return 0; |
| 217 | } |
| 218 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 219 | static int enable_flash_amd8111(struct pci_dev *dev, char *name) |
| 220 | { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 221 | /* register 4e.b gets or'ed with one */ |
| 222 | unsigned char old, new; |
| 223 | /* if it fails, it fails. There are so many variations of broken mobos |
| 224 | * that it is hard to argue that we should quit at this point. |
| 225 | */ |
| 226 | |
Ollie Lho | d11f361 | 2004-12-07 17:19:04 +0000 | [diff] [blame] | 227 | /* enable decoding at 0xffb00000 to 0xffffffff */ |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 228 | old = pci_read_byte(dev, 0x43); |
Ollie Lho | d11f361 | 2004-12-07 17:19:04 +0000 | [diff] [blame] | 229 | new = old | 0xC0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 230 | if (new != old) { |
| 231 | pci_write_byte(dev, 0x43, new); |
| 232 | if (pci_read_byte(dev, 0x43) != new) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 233 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 234 | 0x43, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 238 | old = pci_read_byte(dev, 0x40); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 239 | new = old | 0x01; |
| 240 | if (new == old) |
| 241 | return 0; |
| 242 | pci_write_byte(dev, 0x40, new); |
| 243 | |
| 244 | if (pci_read_byte(dev, 0x40) != new) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 245 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 246 | 0x40, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 247 | return -1; |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | typedef struct penable { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 253 | unsigned short vendor, device; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 254 | char *name; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 255 | int (*doit) (struct pci_dev * dev, char *name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 256 | } FLASH_ENABLE; |
| 257 | |
| 258 | static FLASH_ENABLE enables[] = { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 259 | {0x1039, 0x0630, "sis630", enable_flash_sis630}, |
| 260 | {0x8086, 0x2480, "E7500", enable_flash_e7500}, |
Ronald G. Minnich | 6a96741 | 2004-09-28 20:09:06 +0000 | [diff] [blame] | 261 | {0x8086, 0x24c0, "ICH4", enable_flash_ich4}, |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 262 | {0x1106, 0x8231, "VT8231", enable_flash_vt8231}, |
| 263 | {0x1106, 0x3177, "VT8235", enable_flash_vt8235}, |
| 264 | {0x1078, 0x0100, "CS5530", enable_flash_cs5530}, |
| 265 | {0x100b, 0x0510, "SC1100", enable_flash_sc1100}, |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 266 | {0x1039, 0x0008, "SIS5595", enable_flash_sis5595}, |
| 267 | {0x1022, 0x7468, "AMD8111", enable_flash_amd8111}, |
| 268 | }; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 269 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 270 | int enable_flash_write() |
| 271 | { |
| 272 | int i; |
| 273 | struct pci_access *pacc; |
| 274 | struct pci_dev *dev = 0; |
| 275 | FLASH_ENABLE *enable = 0; |
| 276 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 277 | pacc = pci_alloc(); /* Get the pci_access structure */ |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 278 | /* Set all options you want -- here we stick with the defaults */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 279 | pci_init(pacc); /* Initialize the PCI library */ |
| 280 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 281 | |
| 282 | /* now let's try to find the chipset we have ... */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 283 | for (i = 0; i < sizeof(enables) / sizeof(enables[0]) && (!dev); |
| 284 | i++) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 285 | struct pci_filter f; |
| 286 | struct pci_dev *z; |
| 287 | /* the first param is unused. */ |
| 288 | pci_filter_init((struct pci_access *) 0, &f); |
| 289 | f.vendor = enables[i].vendor; |
| 290 | f.device = enables[i].device; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 291 | for (z = pacc->devices; z; z = z->next) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 292 | if (pci_filter_match(&f, z)) { |
| 293 | enable = &enables[i]; |
| 294 | dev = z; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* now do the deed. */ |
| 299 | if (enable) { |
| 300 | printf("Enabling flash write on %s...", enable->name); |
| 301 | if (enable->doit(dev, enable->name) == 0) |
| 302 | printf("OK\n"); |
| 303 | } |
| 304 | return 0; |
| 305 | } |