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 | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 36 | |
| 37 | #include "flash.h" |
| 38 | #include "jedec.h" |
Ronald G. Minnich | 3c910ed | 2002-05-28 23:29:17 +0000 | [diff] [blame] | 39 | #include "m29f400bt.h" |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 40 | #include "82802ab.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 41 | |
| 42 | struct flashchip flashchips[] = { |
| 43 | {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024, |
| 44 | probe_29f040b, erase_29f040b, write_29f040b}, |
| 45 | {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256, |
| 46 | probe_jedec, erase_jedec, write_jedec}, |
| 47 | {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024, |
| 48 | probe_29f002, erase_29f002, write_29f002}, |
| 49 | {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128, |
| 50 | probe_jedec, erase_jedec, write_jedec}, |
| 51 | {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256, |
| 52 | probe_28sf040, erase_28sf040, write_28sf040}, |
Ronald G. Minnich | c831647 | 2002-03-21 22:40:40 +0000 | [diff] [blame] | 53 | {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096, |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 54 | probe_39sf020, erase_39sf020, write_39sf020}, |
Ollie Lho | 6041bcd | 2002-07-18 03:32:00 +0000 | [diff] [blame] | 55 | {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096, |
| 56 | probe_39sf020, erase_39sf020, write_39sf020}, |
Andrew Ip | 973b26d | 2002-12-30 13:10:06 +0000 | [diff] [blame] | 57 | {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128, |
| 58 | probe_jedec, erase_jedec, write_jedec}, |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 59 | {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128, |
| 60 | probe_jedec, erase_jedec, write_jedec}, |
Andrew Ip | f0126ce | 2002-10-16 06:58:05 +0000 | [diff] [blame] | 61 | {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128, |
| 62 | probe_49f002, erase_49f002, write_49f002}, |
Ronald G. Minnich | 3c910ed | 2002-05-28 23:29:17 +0000 | [diff] [blame] | 63 | {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024, |
| 64 | probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt}, |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 65 | {"82802ab", 137, 173 , NULL, 512, 64*1024, |
| 66 | probe_82802ab, erase_82802ab, write_82802ab}, |
Ronald G. Minnich | bb0322f | 2003-01-13 23:43:36 +0000 | [diff] [blame] | 67 | {"82802ac", 137, 172 , NULL, 1024, 64*1024, |
| 68 | probe_82802ab, erase_82802ab, write_82802ab}, |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 69 | {NULL,} |
| 70 | }; |
| 71 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 72 | int enable_flash_sis630 (struct pci_dev *dev, char *name) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 73 | { |
| 74 | char b; |
| 75 | |
| 76 | /* get io privilege access PCI configuration space */ |
| 77 | if (iopl(3) != 0) { |
| 78 | perror("Can not set io priviliage"); |
| 79 | exit(1); |
| 80 | } |
| 81 | |
| 82 | /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */ |
| 83 | outl(0x80000840, 0x0cf8); |
| 84 | b = inb(0x0cfc) | 0x0b; |
| 85 | outb(b, 0xcfc); |
| 86 | /* Flash write enable on SiS 540/630 */ |
| 87 | outl(0x80000845, 0x0cf8); |
| 88 | b = inb(0x0cfd) | 0x40; |
| 89 | outb(b, 0xcfd); |
| 90 | |
| 91 | /* The same thing on SiS 950 SuperIO side */ |
| 92 | outb(0x87, 0x2e); |
| 93 | outb(0x01, 0x2e); |
| 94 | outb(0x55, 0x2e); |
| 95 | outb(0x55, 0x2e); |
| 96 | |
| 97 | if (inb(0x2f) != 0x87) { |
| 98 | outb(0x87, 0x4e); |
| 99 | outb(0x01, 0x4e); |
| 100 | outb(0x55, 0x4e); |
| 101 | outb(0xaa, 0x4e); |
| 102 | if (inb(0x4f) != 0x87) { |
| 103 | printf("Can not access SiS 950\n"); |
| 104 | return -1; |
| 105 | } |
| 106 | outb(0x24, 0x4e); |
| 107 | b = inb(0x4f) | 0xfc; |
| 108 | outb(0x24, 0x4e); |
| 109 | outb(b, 0x4f); |
| 110 | outb(0x02, 0x4e); |
| 111 | outb(0x02, 0x4f); |
| 112 | } |
| 113 | |
| 114 | outb(0x24, 0x2e); |
| 115 | printf("2f is %#x\n", inb(0x2f)); |
| 116 | b = inb(0x2f) | 0xfc; |
| 117 | outb(0x24, 0x2e); |
| 118 | outb(b, 0x2f); |
| 119 | |
| 120 | outb(0x02, 0x2e); |
| 121 | outb(0x02, 0x2f); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 126 | int |
| 127 | enable_flash_e7500(struct pci_dev *dev, char *name) { |
| 128 | /* register 4e.b gets or'ed with one */ |
| 129 | unsigned char old, new; |
| 130 | int ok; |
| 131 | /* if it fails, it fails. There are so many variations of broken mobos |
| 132 | * that it is hard to argue that we should quit at this point. |
| 133 | */ |
| 134 | |
| 135 | old = pci_read_byte(dev, 0x4e); |
| 136 | |
| 137 | new = old | 1; |
| 138 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 139 | if (new == old) |
| 140 | return 0; |
| 141 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 142 | ok = pci_write_byte(dev, 0x4e, new); |
| 143 | |
| 144 | if (ok != new) { |
| 145 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 146 | old, new, name); |
| 147 | return -1; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 152 | int |
| 153 | enable_flash_vt8231(struct pci_dev *dev, char *name) { |
| 154 | unsigned char old, new; |
| 155 | int ok; |
| 156 | |
| 157 | old = pci_read_byte(dev, 0x40); |
| 158 | |
| 159 | new = old | 0x10; |
| 160 | |
| 161 | if (new == old) |
| 162 | return 0; |
| 163 | |
| 164 | ok = pci_write_byte(dev, 0x40, new); |
| 165 | |
| 166 | if (ok != 0) { |
| 167 | printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 168 | old, new, name); |
| 169 | return -1; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
Ronald G. Minnich | b9d2770 | 2003-04-21 17:56:12 +0000 | [diff] [blame^] | 174 | int |
| 175 | enable_flash_cs5530(struct pci_dev *dev, char *name) { |
| 176 | unsigned char new; |
| 177 | int ok; |
| 178 | |
| 179 | pci_write_byte(dev, 0x52, 0xee); |
| 180 | |
| 181 | new = pci_read_byte(dev, 0x52); |
| 182 | |
| 183 | if (new != 0xee) { |
| 184 | printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", |
| 185 | 0x52, new, name); |
| 186 | return -1; |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 191 | struct flashchip * probe_flash(struct flashchip * flash) |
| 192 | { |
| 193 | int fd_mem; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 194 | volatile char * bios; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 195 | unsigned long size; |
| 196 | |
| 197 | if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) { |
| 198 | perror("Can not open /dev/mem"); |
| 199 | exit(1); |
| 200 | } |
| 201 | |
| 202 | while (flash->name != NULL) { |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 203 | printf("Trying %s, %d KB\n", flash->name, flash->total_size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 204 | size = flash->total_size * 1024; |
| 205 | bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 206 | fd_mem, (off_t) (0 - size)); |
| 207 | if (bios == MAP_FAILED) { |
| 208 | perror("Error MMAP /dev/mem"); |
| 209 | exit(1); |
| 210 | } |
| 211 | flash->virt_addr = bios; |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 212 | flash->fd_mem = fd_mem; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 213 | |
| 214 | if (flash->probe(flash) == 1) { |
| 215 | printf ("%s found at physical address: 0x%lx\n", |
| 216 | flash->name, (0 - size), bios); |
| 217 | return flash; |
| 218 | } |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 219 | munmap ((void *) bios, size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 220 | flash++; |
| 221 | } |
| 222 | return NULL; |
| 223 | } |
| 224 | |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 225 | int verify_flash (struct flashchip * flash, char * buf, int verbose) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 226 | { |
| 227 | int i = 0; |
| 228 | int total_size = flash->total_size *1024; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 229 | volatile char * bios = flash->virt_addr; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 230 | |
| 231 | printf("Verifying address: "); |
| 232 | while (i++ < total_size) { |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 233 | if (verbose) |
| 234 | printf("0x%08x", i); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 235 | if (*(bios+i) != *(buf+i)) { |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 236 | printf("FAILED\n"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 237 | return 0; |
| 238 | } |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 239 | if (verbose) |
| 240 | printf("\b\b\b\b\b\b\b\b\b\b"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 241 | } |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 242 | if (verbose) |
| 243 | printf("\n"); |
| 244 | else |
| 245 | printf("VERIFIED\n"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 246 | return 1; |
| 247 | } |
| 248 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 249 | // count to a billion. Time it. If it's < 1 sec, count to 10B, etc. |
| 250 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 251 | unsigned long micro = 1; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 252 | |
| 253 | void |
| 254 | myusec_calibrate_delay() |
| 255 | { |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 256 | int count = 1000; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 257 | volatile unsigned long i; |
| 258 | unsigned long timeusec; |
| 259 | struct timeval start, end; |
| 260 | int ok = 0; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 261 | void myusec_delay(int time); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 262 | |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 263 | printf("Setting up microsecond timing loop\n"); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 264 | while (! ok) { |
Ronald G. Minnich | 4572a82 | 2003-02-11 16:09:12 +0000 | [diff] [blame] | 265 | //fprintf(stderr, "Try %d\n", count); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 266 | gettimeofday(&start, 0); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 267 | myusec_delay(count); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 268 | gettimeofday(&end, 0); |
| 269 | timeusec = 1000000 * (end.tv_sec - start.tv_sec ) + |
| 270 | (end.tv_usec - start.tv_usec); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 271 | //fprintf(stderr, "timeusec is %d\n", timeusec); |
| 272 | count *= 2; |
| 273 | if (timeusec < 1000000/4) |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 274 | continue; |
| 275 | ok = 1; |
| 276 | } |
| 277 | |
| 278 | // compute one microsecond. That will be count / time |
| 279 | micro = count / timeusec; |
| 280 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 281 | fprintf(stderr, "%dM loops per second\n", micro); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 282 | |
| 283 | |
| 284 | } |
| 285 | |
| 286 | void |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 287 | myusec_delay(int time) |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 288 | { |
| 289 | volatile unsigned long i; |
| 290 | for(i = 0; i < time * micro; i++) |
| 291 | ; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 294 | typedef struct penable { |
| 295 | unsigned short vendor, device; |
| 296 | char *name; |
| 297 | int (*doit)(struct pci_dev *dev, char *name); |
| 298 | } FLASH_ENABLE; |
| 299 | |
| 300 | FLASH_ENABLE enables[] = { |
| 301 | |
| 302 | {0x1, 0x1, "sis630 -- what's the ID?", enable_flash_sis630}, |
| 303 | {0x8086, 0x2480, "E7500", enable_flash_e7500}, |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 304 | {0x1106, 0x8231, "VT8231", enable_flash_vt8231}, |
Ronald G. Minnich | b9d2770 | 2003-04-21 17:56:12 +0000 | [diff] [blame^] | 305 | {0x1078, 0x0100, "CS5530", enable_flash_cs5530}, |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | int |
| 309 | enable_flash_write() { |
| 310 | int i; |
| 311 | struct pci_access *pacc; |
| 312 | struct pci_dev *dev = 0; |
| 313 | unsigned int c; |
| 314 | FLASH_ENABLE *enable = 0; |
| 315 | |
| 316 | pacc = pci_alloc(); /* Get the pci_access structure */ |
| 317 | /* Set all options you want -- here we stick with the defaults */ |
| 318 | pci_init(pacc); /* Initialize the PCI library */ |
| 319 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
| 320 | |
| 321 | /* now let's try to find the chipset we have ... */ |
| 322 | for(i = 0; i < sizeof(enables)/sizeof(enables[0]) && (! dev); i++) { |
| 323 | struct pci_filter f; |
| 324 | struct pci_dev *z; |
| 325 | /* the first param is unused. */ |
| 326 | pci_filter_init((struct pci_access *) 0, &f); |
| 327 | f.vendor = enables[i].vendor; |
| 328 | f.device = enables[i].device; |
| 329 | for(z=pacc->devices; z; z=z->next) |
| 330 | if (pci_filter_match(&f, z)) { |
| 331 | enable = &enables[i]; |
| 332 | dev = z; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* now do the deed. */ |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 337 | if (enable) { |
| 338 | printf("Enabling flash write on %s...", enable->name); |
| 339 | if (enable->doit(dev, enable->name) == 0) |
| 340 | printf("OK\n"); |
| 341 | } |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 345 | void usage(const char *name) |
| 346 | { |
| 347 | printf("usage: %s [-rwv] [file]\n", name); |
| 348 | printf("-r: read flash and save into file\n" |
| 349 | "-w: write file into flash (default when file is specified)\n" |
| 350 | "-v: verify flash against file\n" |
| 351 | " If no file is specified, then all that happens\n" |
| 352 | " is that flash info is dumped\n"); |
| 353 | exit(1); |
| 354 | } |
| 355 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 356 | int |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 357 | main (int argc, char * argv[]) |
| 358 | { |
| 359 | char * buf; |
| 360 | unsigned long size; |
| 361 | FILE * image; |
| 362 | struct flashchip * flash; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 363 | int opt; |
| 364 | int read_it = 0, write_it = 0, verify_it = 0; |
| 365 | char *filename = NULL; |
| 366 | while ((opt = getopt(argc, argv, "rwv")) != EOF) { |
| 367 | switch (opt) { |
| 368 | case 'r': |
| 369 | read_it = 1; |
| 370 | break; |
| 371 | case 'w': |
| 372 | write_it = 1; |
| 373 | break; |
| 374 | case 'v': |
| 375 | verify_it = 1; |
| 376 | break; |
| 377 | default: |
| 378 | usage(argv[0]); |
| 379 | break; |
| 380 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 381 | } |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 382 | if (read_it && write_it) { |
| 383 | printf("-r and -w are mutually exclusive\n"); |
| 384 | usage(argv[0]); |
| 385 | } |
| 386 | |
| 387 | if (optind < argc) |
| 388 | filename = argv[optind++]; |
| 389 | |
| 390 | printf("Calibrating timer since microsleep sucks ... takes a second\n"); |
| 391 | myusec_calibrate_delay(); |
| 392 | printf("OK, calibrated, now do the deed\n"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 393 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 394 | /* try to enable it. Failure IS an option, since not all motherboards |
| 395 | * really need this to be done, etc., etc. It sucks. |
| 396 | */ |
| 397 | (void) enable_flash_write(); |
| 398 | |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 399 | if ((flash = probe_flash (flashchips)) == NULL) { |
| 400 | printf("EEPROM not found\n"); |
| 401 | exit(1); |
| 402 | } |
| 403 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 404 | printf("Part is %s\n", flash->name); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 405 | if (!filename){ |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 406 | printf("OK, only ENABLING flash write, but NOT FLASHING\n"); |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 407 | return 0; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 408 | } |
| 409 | size = flash->total_size * 1024; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 410 | buf = (char *) calloc (size, sizeof(char)); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 411 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 412 | if (read_it) { |
| 413 | if ((image = fopen (filename, "w")) == NULL) { |
Ronald G. Minnich | b9d2770 | 2003-04-21 17:56:12 +0000 | [diff] [blame^] | 414 | perror(filename); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 415 | exit(1); |
| 416 | } |
| 417 | printf("Reading Flash..."); |
| 418 | memcpy(buf, (const char *) flash->virt_addr, size); |
| 419 | fwrite(buf, sizeof(char), size, image); |
| 420 | fclose(image); |
| 421 | printf("done\n"); |
| 422 | } else { |
| 423 | if ((image = fopen (filename, "r")) == NULL) { |
Ronald G. Minnich | b9d2770 | 2003-04-21 17:56:12 +0000 | [diff] [blame^] | 424 | perror(filename); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 425 | exit(1); |
| 426 | } |
| 427 | fread (buf, sizeof(char), size, image); |
| 428 | fclose(image); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 431 | if (write_it) |
| 432 | flash->write (flash, buf); |
| 433 | if (verify_it) |
| 434 | verify_flash (flash, buf, /* verbose = */ 0); |
Ronald G. Minnich | 4572a82 | 2003-02-11 16:09:12 +0000 | [diff] [blame] | 435 | return 0; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 436 | } |