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 | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 78 | printf |
| 79 | ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 80 | 0x4e, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 81 | return -1; |
| 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int enable_flash_vt8235(struct pci_dev *dev, char *name) |
| 87 | { |
| 88 | unsigned char old, new, val; |
| 89 | unsigned int base; |
| 90 | int ok; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 91 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 92 | /* get io privilege access PCI configuration space */ |
| 93 | if (iopl(3) != 0) { |
| 94 | perror("Can not set io priviliage"); |
| 95 | exit(1); |
| 96 | } |
| 97 | |
| 98 | old = pci_read_byte(dev, 0x40); |
| 99 | |
| 100 | new = old | 0x10; |
| 101 | |
| 102 | if (new == old) |
| 103 | return 0; |
| 104 | |
| 105 | ok = pci_write_byte(dev, 0x40, new); |
| 106 | if (ok != 0) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 107 | printf |
| 108 | ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 109 | old, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* enable GPIO15 which is connected to write protect. */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 113 | base = |
| 114 | ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) |
| 115 | << 8); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 116 | val = inb(base + 0x4d); |
| 117 | val |= 0x80; |
| 118 | outb(val, base + 0x4d); |
| 119 | |
| 120 | if (ok != 0) { |
| 121 | return -1; |
| 122 | } else { |
| 123 | return 0; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static int enable_flash_vt8231(struct pci_dev *dev, char *name) |
| 128 | { |
| 129 | unsigned char val; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 130 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 131 | val = pci_read_byte(dev, 0x40); |
| 132 | val |= 0x10; |
| 133 | pci_write_byte(dev, 0x40, val); |
| 134 | |
| 135 | if (pci_read_byte(dev, 0x40) != val) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 136 | printf |
| 137 | ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 138 | 0x40, val, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 139 | return -1; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int enable_flash_cs5530(struct pci_dev *dev, char *name) |
| 145 | { |
| 146 | unsigned char new; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 147 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 148 | pci_write_byte(dev, 0x52, 0xee); |
| 149 | |
| 150 | new = pci_read_byte(dev, 0x52); |
| 151 | |
| 152 | if (new != 0xee) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 153 | printf |
| 154 | ("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 155 | 0x52, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 156 | return -1; |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int enable_flash_sc1100(struct pci_dev *dev, char *name) |
| 162 | { |
| 163 | unsigned char new; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 164 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 165 | pci_write_byte(dev, 0x52, 0xee); |
| 166 | |
| 167 | new = pci_read_byte(dev, 0x52); |
| 168 | |
| 169 | if (new != 0xee) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 170 | printf |
| 171 | ("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 172 | 0x52, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 173 | return -1; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int enable_flash_sis5595(struct pci_dev *dev, char *name) |
| 179 | { |
| 180 | unsigned char new, newer; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 181 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 182 | new = pci_read_byte(dev, 0x45); |
| 183 | |
| 184 | /* clear bit 5 */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 185 | new &= (~0x20); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 186 | /* set bit 2 */ |
| 187 | new |= 0x4; |
| 188 | |
| 189 | pci_write_byte(dev, 0x45, new); |
| 190 | |
| 191 | newer = pci_read_byte(dev, 0x45); |
| 192 | if (newer != new) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 193 | printf |
| 194 | ("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 195 | 0x45, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 196 | printf("Stuck at 0x%x\n", newer); |
| 197 | return -1; |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 202 | static int enable_flash_amd8111(struct pci_dev *dev, char *name) |
| 203 | { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 204 | /* register 4e.b gets or'ed with one */ |
| 205 | unsigned char old, new; |
| 206 | /* if it fails, it fails. There are so many variations of broken mobos |
| 207 | * that it is hard to argue that we should quit at this point. |
| 208 | */ |
| 209 | |
| 210 | //dump_pci_device(dev); |
| 211 | |
| 212 | old = pci_read_byte(dev, 0x43); |
| 213 | new = old | 0x80; |
| 214 | if (new != old) { |
| 215 | pci_write_byte(dev, 0x43, new); |
| 216 | if (pci_read_byte(dev, 0x43) != new) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 217 | printf |
| 218 | ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 219 | 0x43, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 223 | old = pci_read_byte(dev, 0x40); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 224 | new = old | 0x01; |
| 225 | if (new == old) |
| 226 | return 0; |
| 227 | pci_write_byte(dev, 0x40, new); |
| 228 | |
| 229 | if (pci_read_byte(dev, 0x40) != new) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 230 | printf |
| 231 | ("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 232 | 0x40, new, name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 233 | return -1; |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | typedef struct penable { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 239 | unsigned short vendor, device; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 240 | char *name; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 241 | int (*doit) (struct pci_dev * dev, char *name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 242 | } FLASH_ENABLE; |
| 243 | |
| 244 | static FLASH_ENABLE enables[] = { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 245 | {0x1039, 0x0630, "sis630", enable_flash_sis630}, |
| 246 | {0x8086, 0x2480, "E7500", enable_flash_e7500}, |
| 247 | {0x1106, 0x8231, "VT8231", enable_flash_vt8231}, |
| 248 | {0x1106, 0x3177, "VT8235", enable_flash_vt8235}, |
| 249 | {0x1078, 0x0100, "CS5530", enable_flash_cs5530}, |
| 250 | {0x100b, 0x0510, "SC1100", enable_flash_sc1100}, |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 251 | {0x1039, 0x0008, "SIS5595", enable_flash_sis5595}, |
| 252 | {0x1022, 0x7468, "AMD8111", enable_flash_amd8111}, |
| 253 | }; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 254 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 255 | int enable_flash_write() |
| 256 | { |
| 257 | int i; |
| 258 | struct pci_access *pacc; |
| 259 | struct pci_dev *dev = 0; |
| 260 | FLASH_ENABLE *enable = 0; |
| 261 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 262 | pacc = pci_alloc(); /* Get the pci_access structure */ |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 263 | /* Set all options you want -- here we stick with the defaults */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 264 | pci_init(pacc); /* Initialize the PCI library */ |
| 265 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 266 | |
| 267 | /* now let's try to find the chipset we have ... */ |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 268 | for (i = 0; i < sizeof(enables) / sizeof(enables[0]) && (!dev); |
| 269 | i++) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 270 | struct pci_filter f; |
| 271 | struct pci_dev *z; |
| 272 | /* the first param is unused. */ |
| 273 | pci_filter_init((struct pci_access *) 0, &f); |
| 274 | f.vendor = enables[i].vendor; |
| 275 | f.device = enables[i].device; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame^] | 276 | for (z = pacc->devices; z; z = z->next) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 277 | if (pci_filter_match(&f, z)) { |
| 278 | enable = &enables[i]; |
| 279 | dev = z; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* now do the deed. */ |
| 284 | if (enable) { |
| 285 | printf("Enabling flash write on %s...", enable->name); |
| 286 | if (enable->doit(dev, enable->name) == 0) |
| 287 | printf("OK\n"); |
| 288 | } |
| 289 | return 0; |
| 290 | } |