Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * flash_rom.c: Flash programming utility for SiS 630/950 M/Bs |
| 3 | * |
| 4 | * |
| 5 | * Copyright 2000 Silicon Integrated System Corporation |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | * |
| 22 | * Reference: |
| 23 | * 1. SiS 630 Specification |
| 24 | * 2. SiS 950 Specification |
| 25 | * |
| 26 | * $Id$ |
| 27 | */ |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <sys/mman.h> |
| 32 | #include <sys/io.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stdio.h> |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 35 | #include <pci/pci.h> |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 36 | #include <string.h> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 37 | |
| 38 | #include "flash.h" |
| 39 | #include "jedec.h" |
Ronald G. Minnich | 3c910ed | 2002-05-28 23:29:17 +0000 | [diff] [blame] | 40 | #include "m29f400bt.h" |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 41 | #include "82802ab.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 42 | |
| 43 | struct flashchip flashchips[] = { |
| 44 | {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024, |
| 45 | probe_29f040b, erase_29f040b, write_29f040b}, |
| 46 | {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256, |
| 47 | probe_jedec, erase_jedec, write_jedec}, |
| 48 | {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024, |
| 49 | probe_29f002, erase_29f002, write_29f002}, |
| 50 | {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128, |
| 51 | probe_jedec, erase_jedec, write_jedec}, |
| 52 | {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256, |
| 53 | probe_28sf040, erase_28sf040, write_28sf040}, |
Ronald G. Minnich | c831647 | 2002-03-21 22:40:40 +0000 | [diff] [blame] | 54 | {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096, |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 55 | probe_39sf020, erase_39sf020, write_39sf020}, |
Ollie Lho | 6041bcd | 2002-07-18 03:32:00 +0000 | [diff] [blame] | 56 | {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096, |
| 57 | probe_39sf020, erase_39sf020, write_39sf020}, |
Andrew Ip | 973b26d | 2002-12-30 13:10:06 +0000 | [diff] [blame] | 58 | {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128, |
| 59 | probe_jedec, erase_jedec, write_jedec}, |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 60 | {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128, |
| 61 | probe_jedec, erase_jedec, write_jedec}, |
Andrew Ip | f0126ce | 2002-10-16 06:58:05 +0000 | [diff] [blame] | 62 | {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128, |
| 63 | probe_49f002, erase_49f002, write_49f002}, |
Ronald G. Minnich | 3c910ed | 2002-05-28 23:29:17 +0000 | [diff] [blame] | 64 | {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024, |
| 65 | probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt}, |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 66 | {"82802ab", 137, 173 , NULL, 512, 64*1024, |
| 67 | probe_82802ab, erase_82802ab, write_82802ab}, |
Ronald G. Minnich | bb0322f | 2003-01-13 23:43:36 +0000 | [diff] [blame] | 68 | {"82802ac", 137, 172 , NULL, 1024, 64*1024, |
| 69 | probe_82802ab, erase_82802ab, write_82802ab}, |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 70 | {NULL,} |
| 71 | }; |
| 72 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 73 | char *chip_to_probe = NULL; |
| 74 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 75 | int enable_flash_sis630 (struct pci_dev *dev, char *name) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 76 | { |
| 77 | char b; |
| 78 | |
| 79 | /* get io privilege access PCI configuration space */ |
| 80 | if (iopl(3) != 0) { |
| 81 | perror("Can not set io priviliage"); |
| 82 | exit(1); |
| 83 | } |
| 84 | |
| 85 | /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */ |
| 86 | outl(0x80000840, 0x0cf8); |
| 87 | b = inb(0x0cfc) | 0x0b; |
| 88 | outb(b, 0xcfc); |
| 89 | /* Flash write enable on SiS 540/630 */ |
| 90 | outl(0x80000845, 0x0cf8); |
| 91 | b = inb(0x0cfd) | 0x40; |
| 92 | outb(b, 0xcfd); |
| 93 | |
| 94 | /* The same thing on SiS 950 SuperIO side */ |
| 95 | outb(0x87, 0x2e); |
| 96 | outb(0x01, 0x2e); |
| 97 | outb(0x55, 0x2e); |
| 98 | outb(0x55, 0x2e); |
| 99 | |
| 100 | if (inb(0x2f) != 0x87) { |
| 101 | outb(0x87, 0x4e); |
| 102 | outb(0x01, 0x4e); |
| 103 | outb(0x55, 0x4e); |
| 104 | outb(0xaa, 0x4e); |
| 105 | if (inb(0x4f) != 0x87) { |
| 106 | printf("Can not access SiS 950\n"); |
| 107 | return -1; |
| 108 | } |
| 109 | outb(0x24, 0x4e); |
| 110 | b = inb(0x4f) | 0xfc; |
| 111 | outb(0x24, 0x4e); |
| 112 | outb(b, 0x4f); |
| 113 | outb(0x02, 0x4e); |
| 114 | outb(0x02, 0x4f); |
| 115 | } |
| 116 | |
| 117 | outb(0x24, 0x2e); |
| 118 | printf("2f is %#x\n", inb(0x2f)); |
| 119 | b = inb(0x2f) | 0xfc; |
| 120 | outb(0x24, 0x2e); |
| 121 | outb(b, 0x2f); |
| 122 | |
| 123 | outb(0x02, 0x2e); |
| 124 | outb(0x02, 0x2f); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 129 | int |
| 130 | enable_flash_e7500(struct pci_dev *dev, char *name) { |
| 131 | /* register 4e.b gets or'ed with one */ |
| 132 | unsigned char old, new; |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 133 | /* if it fails, it fails. There are so many variations of broken mobos |
| 134 | * that it is hard to argue that we should quit at this point. |
| 135 | */ |
| 136 | |
| 137 | old = pci_read_byte(dev, 0x4e); |
| 138 | |
| 139 | new = old | 1; |
| 140 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 141 | if (new == old) |
| 142 | return 0; |
| 143 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 144 | pci_write_byte(dev, 0x4e, new); |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 145 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 146 | if (pci_read_byte(dev, 0x4e) != new) { |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 147 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 148 | 0x4e, new, name); |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 154 | int |
Andrew Ip | 772f645 | 2003-07-21 14:54:16 +0000 | [diff] [blame] | 155 | enable_flash_vt8235(struct pci_dev *dev, char *name) { |
| 156 | unsigned char old, new, val; |
| 157 | unsigned int base; |
| 158 | int ok; |
| 159 | |
| 160 | /* get io privilege access PCI configuration space */ |
| 161 | if (iopl(3) != 0) { |
| 162 | perror("Can not set io priviliage"); |
| 163 | exit(1); |
| 164 | } |
| 165 | |
| 166 | old = pci_read_byte(dev, 0x40); |
| 167 | |
| 168 | new = old | 0x10; |
| 169 | |
| 170 | if (new == old) |
| 171 | return 0; |
| 172 | |
| 173 | ok = pci_write_byte(dev, 0x40, new); |
| 174 | if (ok != 0) { |
| 175 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 176 | old, new, name); |
| 177 | } |
| 178 | |
| 179 | /* enable GPIO15 which is connected to write protect. */ |
| 180 | base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8); |
| 181 | val = inb(base + 0x4d); |
| 182 | val |= 0x80; |
| 183 | outb(val, base + 0x4d); |
| 184 | |
| 185 | if (ok != 0) { |
| 186 | return -1; |
| 187 | } else { |
| 188 | return 0; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | int |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 193 | enable_flash_vt8231(struct pci_dev *dev, char *name) { |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 194 | unsigned char val; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 195 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 196 | val = pci_read_byte(dev, 0x40); |
| 197 | val |= 0x10; |
| 198 | pci_write_byte(dev, 0x40, val); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 199 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 200 | if (pci_read_byte(dev, 0x40) != val) { |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 201 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 202 | 0x40, val, name); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 203 | return -1; |
| 204 | } |
| 205 | return 0; |
| 206 | } |
| 207 | |
Ronald G. Minnich | b9d2770 | 2003-04-21 17:56:12 +0000 | [diff] [blame] | 208 | int |
| 209 | enable_flash_cs5530(struct pci_dev *dev, char *name) { |
| 210 | unsigned char new; |
| 211 | int ok; |
| 212 | |
| 213 | pci_write_byte(dev, 0x52, 0xee); |
| 214 | |
| 215 | new = pci_read_byte(dev, 0x52); |
| 216 | |
| 217 | if (new != 0xee) { |
| 218 | printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 219 | 0x52, new, name); |
| 220 | return -1; |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
Ronald G. Minnich | 7bd1dee | 2003-05-08 19:33:19 +0000 | [diff] [blame] | 225 | int |
Andrew Ip | afe44a9 | 2003-07-22 10:12:08 +0000 | [diff] [blame] | 226 | enable_flash_sc1100(struct pci_dev *dev, char *name) { |
| 227 | unsigned char new; |
| 228 | int ok; |
| 229 | |
| 230 | pci_write_byte(dev, 0x52, 0xee); |
| 231 | |
| 232 | new = pci_read_byte(dev, 0x52); |
| 233 | |
| 234 | if (new != 0xee) { |
| 235 | printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 236 | 0x52, new, name); |
| 237 | return -1; |
| 238 | } |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | int |
Ronald G. Minnich | 7bd1dee | 2003-05-08 19:33:19 +0000 | [diff] [blame] | 243 | enable_flash_sis5595(struct pci_dev *dev, char *name) { |
| 244 | unsigned char new, newer; |
| 245 | |
| 246 | new = pci_read_byte(dev, 0x45); |
| 247 | |
| 248 | /* clear bit 5 */ |
| 249 | new &= (~ 0x20); |
| 250 | /* set bit 2 */ |
| 251 | new |= 0x4; |
| 252 | |
| 253 | pci_write_byte(dev, 0x45, new); |
| 254 | |
| 255 | newer = pci_read_byte(dev, 0x45); |
| 256 | if (newer != new) { |
| 257 | printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 258 | 0x45, new, name); |
| 259 | printf("Stuck at 0x%x\n", newer); |
| 260 | return -1; |
| 261 | } |
| 262 | return 0; |
| 263 | } |
| 264 | |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 265 | struct flashchip * probe_flash(struct flashchip * flash) |
| 266 | { |
| 267 | int fd_mem; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 268 | volatile char * bios; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 269 | unsigned long size; |
| 270 | |
| 271 | if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) { |
| 272 | perror("Can not open /dev/mem"); |
| 273 | exit(1); |
| 274 | } |
| 275 | |
| 276 | while (flash->name != NULL) { |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 277 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) { |
| 278 | flash++; |
| 279 | continue; |
| 280 | } |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 281 | printf("Trying %s, %d KB\n", flash->name, flash->total_size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 282 | size = flash->total_size * 1024; |
| 283 | bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 284 | fd_mem, (off_t) (0 - size)); |
| 285 | if (bios == MAP_FAILED) { |
| 286 | perror("Error MMAP /dev/mem"); |
| 287 | exit(1); |
| 288 | } |
| 289 | flash->virt_addr = bios; |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 290 | flash->fd_mem = fd_mem; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 291 | |
| 292 | if (flash->probe(flash) == 1) { |
| 293 | printf ("%s found at physical address: 0x%lx\n", |
| 294 | flash->name, (0 - size), bios); |
| 295 | return flash; |
| 296 | } |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 297 | munmap ((void *) bios, size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 298 | flash++; |
| 299 | } |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 303 | int verify_flash (struct flashchip * flash, char * buf, int verbose) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 304 | { |
| 305 | int i = 0; |
| 306 | int total_size = flash->total_size *1024; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 307 | volatile char * bios = flash->virt_addr; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 308 | |
| 309 | printf("Verifying address: "); |
| 310 | while (i++ < total_size) { |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 311 | if (verbose) |
| 312 | printf("0x%08x", i); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 313 | if (*(bios+i) != *(buf+i)) { |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 314 | printf("FAILED\n"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 315 | return 0; |
| 316 | } |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 317 | if (verbose) |
| 318 | printf("\b\b\b\b\b\b\b\b\b\b"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 319 | } |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 320 | if (verbose) |
| 321 | printf("\n"); |
| 322 | else |
| 323 | printf("VERIFIED\n"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 324 | return 1; |
| 325 | } |
| 326 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 327 | // count to a billion. Time it. If it's < 1 sec, count to 10B, etc. |
| 328 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 329 | unsigned long micro = 1; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 330 | |
| 331 | void |
| 332 | myusec_calibrate_delay() |
| 333 | { |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 334 | int count = 1000; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 335 | volatile unsigned long i; |
| 336 | unsigned long timeusec; |
| 337 | struct timeval start, end; |
| 338 | int ok = 0; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 339 | void myusec_delay(int time); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 340 | |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 341 | printf("Setting up microsecond timing loop\n"); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 342 | while (! ok) { |
Ronald G. Minnich | 4572a82 | 2003-02-11 16:09:12 +0000 | [diff] [blame] | 343 | //fprintf(stderr, "Try %d\n", count); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 344 | gettimeofday(&start, 0); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 345 | myusec_delay(count); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 346 | gettimeofday(&end, 0); |
| 347 | timeusec = 1000000 * (end.tv_sec - start.tv_sec ) + |
| 348 | (end.tv_usec - start.tv_usec); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 349 | //fprintf(stderr, "timeusec is %d\n", timeusec); |
| 350 | count *= 2; |
| 351 | if (timeusec < 1000000/4) |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 352 | continue; |
| 353 | ok = 1; |
| 354 | } |
| 355 | |
| 356 | // compute one microsecond. That will be count / time |
| 357 | micro = count / timeusec; |
| 358 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 359 | fprintf(stderr, "%dM loops per second\n", micro); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 360 | |
| 361 | |
| 362 | } |
| 363 | |
| 364 | void |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 365 | myusec_delay(int time) |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 366 | { |
| 367 | volatile unsigned long i; |
| 368 | for(i = 0; i < time * micro; i++) |
| 369 | ; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 372 | typedef struct penable { |
| 373 | unsigned short vendor, device; |
| 374 | char *name; |
| 375 | int (*doit)(struct pci_dev *dev, char *name); |
| 376 | } FLASH_ENABLE; |
| 377 | |
| 378 | FLASH_ENABLE enables[] = { |
| 379 | |
| 380 | {0x1, 0x1, "sis630 -- what's the ID?", enable_flash_sis630}, |
| 381 | {0x8086, 0x2480, "E7500", enable_flash_e7500}, |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 382 | {0x1106, 0x8231, "VT8231", enable_flash_vt8231}, |
Andrew Ip | 772f645 | 2003-07-21 14:54:16 +0000 | [diff] [blame] | 383 | {0x1106, 0x3177, "VT8235", enable_flash_vt8235}, |
Ronald G. Minnich | b9d2770 | 2003-04-21 17:56:12 +0000 | [diff] [blame] | 384 | {0x1078, 0x0100, "CS5530", enable_flash_cs5530}, |
Andrew Ip | afe44a9 | 2003-07-22 10:12:08 +0000 | [diff] [blame] | 385 | {0x100b, 0x0510, "SC1100", enable_flash_sc1100}, |
Ronald G. Minnich | 7bd1dee | 2003-05-08 19:33:19 +0000 | [diff] [blame] | 386 | {0x1039, 0x8, "SIS5595", enable_flash_sis5595}, |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | int |
| 390 | enable_flash_write() { |
| 391 | int i; |
| 392 | struct pci_access *pacc; |
| 393 | struct pci_dev *dev = 0; |
| 394 | unsigned int c; |
| 395 | FLASH_ENABLE *enable = 0; |
| 396 | |
| 397 | pacc = pci_alloc(); /* Get the pci_access structure */ |
| 398 | /* Set all options you want -- here we stick with the defaults */ |
| 399 | pci_init(pacc); /* Initialize the PCI library */ |
| 400 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
| 401 | |
| 402 | /* now let's try to find the chipset we have ... */ |
| 403 | for(i = 0; i < sizeof(enables)/sizeof(enables[0]) && (! dev); i++) { |
| 404 | struct pci_filter f; |
| 405 | struct pci_dev *z; |
| 406 | /* the first param is unused. */ |
| 407 | pci_filter_init((struct pci_access *) 0, &f); |
| 408 | f.vendor = enables[i].vendor; |
| 409 | f.device = enables[i].device; |
| 410 | for(z=pacc->devices; z; z=z->next) |
| 411 | if (pci_filter_match(&f, z)) { |
| 412 | enable = &enables[i]; |
| 413 | dev = z; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /* now do the deed. */ |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 418 | if (enable) { |
| 419 | printf("Enabling flash write on %s...", enable->name); |
| 420 | if (enable->doit(dev, enable->name) == 0) |
| 421 | printf("OK\n"); |
| 422 | } |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 426 | void usage(const char *name) |
| 427 | { |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 428 | printf("usage: %s [-rwv] [-c chipname][file]\n", name); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 429 | printf("-r: read flash and save into file\n" |
| 430 | "-w: write file into flash (default when file is specified)\n" |
| 431 | "-v: verify flash against file\n" |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 432 | "-c: probe only for specified flash chip\n" |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 433 | " If no file is specified, then all that happens\n" |
| 434 | " is that flash info is dumped\n"); |
| 435 | exit(1); |
| 436 | } |
| 437 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 438 | int |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 439 | main (int argc, char * argv[]) |
| 440 | { |
| 441 | char * buf; |
| 442 | unsigned long size; |
| 443 | FILE * image; |
| 444 | struct flashchip * flash; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 445 | int opt; |
| 446 | int read_it = 0, write_it = 0, verify_it = 0; |
| 447 | char *filename = NULL; |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 448 | |
| 449 | setbuf(stdout, NULL); |
| 450 | |
| 451 | while ((opt = getopt(argc, argv, "rwvc:")) != EOF) { |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 452 | switch (opt) { |
| 453 | case 'r': |
| 454 | read_it = 1; |
| 455 | break; |
| 456 | case 'w': |
| 457 | write_it = 1; |
| 458 | break; |
| 459 | case 'v': |
| 460 | verify_it = 1; |
| 461 | break; |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 462 | case 'c': |
| 463 | chip_to_probe = strdup(optarg); |
| 464 | break; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 465 | default: |
| 466 | usage(argv[0]); |
| 467 | break; |
| 468 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 469 | } |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 470 | if (read_it && write_it) { |
| 471 | printf("-r and -w are mutually exclusive\n"); |
| 472 | usage(argv[0]); |
| 473 | } |
| 474 | |
| 475 | if (optind < argc) |
| 476 | filename = argv[optind++]; |
| 477 | |
| 478 | printf("Calibrating timer since microsleep sucks ... takes a second\n"); |
| 479 | myusec_calibrate_delay(); |
| 480 | printf("OK, calibrated, now do the deed\n"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 481 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 482 | /* try to enable it. Failure IS an option, since not all motherboards |
| 483 | * really need this to be done, etc., etc. It sucks. |
| 484 | */ |
| 485 | (void) enable_flash_write(); |
| 486 | |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 487 | if ((flash = probe_flash (flashchips)) == NULL) { |
| 488 | printf("EEPROM not found\n"); |
| 489 | exit(1); |
| 490 | } |
| 491 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 492 | printf("Part is %s\n", flash->name); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 493 | if (!filename){ |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 494 | printf("OK, only ENABLING flash write, but NOT FLASHING\n"); |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 495 | return 0; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 496 | } |
| 497 | size = flash->total_size * 1024; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 498 | buf = (char *) calloc (size, sizeof(char)); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 499 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 500 | if (read_it) { |
| 501 | if ((image = fopen (filename, "w")) == NULL) { |
Ronald G. Minnich | b9d2770 | 2003-04-21 17:56:12 +0000 | [diff] [blame] | 502 | perror(filename); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 503 | exit(1); |
| 504 | } |
| 505 | printf("Reading Flash..."); |
| 506 | memcpy(buf, (const char *) flash->virt_addr, size); |
| 507 | fwrite(buf, sizeof(char), size, image); |
| 508 | fclose(image); |
| 509 | printf("done\n"); |
| 510 | } else { |
| 511 | if ((image = fopen (filename, "r")) == NULL) { |
Ronald G. Minnich | b9d2770 | 2003-04-21 17:56:12 +0000 | [diff] [blame] | 512 | perror(filename); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 513 | exit(1); |
| 514 | } |
| 515 | fread (buf, sizeof(char), size, image); |
| 516 | fclose(image); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame^] | 519 | if (write_it || (!read_it && !verify_it)) |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 520 | flash->write (flash, buf); |
| 521 | if (verify_it) |
| 522 | verify_flash (flash, buf, /* verbose = */ 0); |
Ronald G. Minnich | 4572a82 | 2003-02-11 16:09:12 +0000 | [diff] [blame] | 523 | return 0; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 524 | } |