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> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | #include <stdio.h> |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 34 | #include <string.h> |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 35 | #include <stdlib.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 | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 41 | #include "msys_doc.h" |
| 42 | #include "am29f040b.h" |
| 43 | #include "sst28sf040.h" |
| 44 | #include "w49f002u.h" |
| 45 | #include "sst39sf020.h" |
David Hendricks | 3770a11 | 2004-03-17 21:47:30 +0000 | [diff] [blame] | 46 | #include "sst49lf040.h" |
Ronald G. Minnich | e3dad01 | 2004-02-10 21:34:18 +0000 | [diff] [blame] | 47 | #include "pm49fl004.h" |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 48 | #include "mx29f002.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 49 | |
| 50 | struct flashchip flashchips[] = { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 51 | {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024, |
| 52 | probe_29f040b, erase_29f040b, write_29f040b, NULL}, |
| 53 | {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256, |
| 54 | probe_jedec, erase_jedec, write_jedec, NULL}, |
| 55 | {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024, |
| 56 | probe_29f002, erase_29f002, write_29f002, NULL}, |
| 57 | {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128, |
| 58 | probe_jedec, erase_jedec, write_jedec, NULL}, |
| 59 | {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256, |
| 60 | probe_28sf040, erase_28sf040, write_28sf040, NULL}, |
| 61 | {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096, |
| 62 | probe_39sf020, erase_39sf020, write_39sf020, NULL}, |
| 63 | {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096, |
| 64 | probe_39sf020, erase_39sf020, write_39sf020, NULL}, |
David Hendricks | 180850e | 2004-03-18 21:59:05 +0000 | [diff] [blame^] | 65 | {"SST49LF040", SST_ID, SST_49LF040, NULL, 512, 4096, |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 66 | probe_49lf040, erase_49lf040, write_49lf040, NULL}, |
David Hendricks | 180850e | 2004-03-18 21:59:05 +0000 | [diff] [blame^] | 67 | {"SST49LF080A", SST_ID, SST_49LF080A, NULL, 1024, 4096, |
| 68 | probe_49lf040, erase_49lf040, write_49lf040, NULL}, |
| 69 | {"SST49LF002A", SST_ID, SST_49LF002A, NULL, 256, 4096, |
| 70 | probe_49lf040, erase_49lf040, write_49lf040, NULL}, |
| 71 | {"SST49LF003A", SST_ID, SST_49LF003A, NULL, 384, 4096, |
| 72 | probe_49lf040, erase_49lf040, write_49lf040, NULL}, |
| 73 | {"SST49LF004A", SST_ID, SST_49LF004A, NULL, 512, 4096, |
| 74 | probe_49lf040, erase_49lf040, write_49lf040, NULL}, |
| 75 | {"SST49LF008A", SST_ID, SST_49LF008A, NULL, 1024, 4096, |
| 76 | probe_49lf040, erase_49lf040, write_49lf040, NULL}, |
| 77 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 78 | //By LYH begin |
| 79 | {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64*1024, |
| 80 | probe_49fl004, erase_49fl004, write_49fl004, NULL}, |
| 81 | //END |
| 82 | {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128, |
| 83 | probe_jedec, erase_jedec, write_jedec, NULL}, |
| 84 | {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128, |
| 85 | probe_jedec, erase_jedec, write_jedec, NULL}, |
| 86 | {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128, |
| 87 | probe_49f002, erase_49f002, write_49f002, NULL}, |
| 88 | {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024, |
| 89 | probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL}, |
| 90 | {"82802ab", 137, 173 , NULL, 512, 64*1024, |
| 91 | probe_82802ab, erase_82802ab, write_82802ab, NULL}, |
| 92 | {"82802ac", 137, 172 , NULL, 1024, 64*1024, |
| 93 | probe_82802ab, erase_82802ab, write_82802ab, NULL}, |
| 94 | {"MD-2802 (M-Systems DiskOnChip Millennium Module)", |
| 95 | MSYSTEMS_ID, MSYSTEMS_MD2802, |
| 96 | NULL, 8, 8*1024, |
| 97 | probe_md2802, erase_md2802, write_md2802, read_md2802}, |
| 98 | {NULL,} |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 101 | char *chip_to_probe = NULL; |
Ronald G. Minnich | 7bd1dee | 2003-05-08 19:33:19 +0000 | [diff] [blame] | 102 | |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 103 | struct flashchip * probe_flash(struct flashchip * flash) |
| 104 | { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 105 | int fd_mem; |
| 106 | volatile char * bios; |
| 107 | unsigned long size; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 108 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 109 | if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) { |
| 110 | perror("Can not open /dev/mem"); |
| 111 | exit(1); |
| 112 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 113 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 114 | while (flash->name != NULL) { |
| 115 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) { |
| 116 | flash++; |
| 117 | continue; |
| 118 | } |
| 119 | printf("Trying %s, %d KB\n", flash->name, flash->total_size); |
| 120 | size = flash->total_size * 1024; |
| 121 | /* BUG? what happens if getpagesize() > size!? |
| 122 | -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */ |
| 123 | if (getpagesize() > size) { |
| 124 | size = getpagesize(); |
| 125 | printf("%s: warning: size: %d -> %ld\n", __FUNCTION__, |
| 126 | flash->total_size * 1024, (unsigned long)size); |
| 127 | } |
| 128 | bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
Ollie Lho | cf29de8 | 2004-03-18 19:40:07 +0000 | [diff] [blame] | 129 | fd_mem, (off_t) (0xffffffff - size + 1)); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 130 | if (bios == MAP_FAILED) { |
| 131 | perror("Error MMAP /dev/mem"); |
| 132 | exit(1); |
| 133 | } |
| 134 | flash->virt_addr = bios; |
| 135 | flash->fd_mem = fd_mem; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 136 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 137 | if (flash->probe(flash) == 1) { |
| 138 | printf ("%s found at physical address: 0x%lx\n", |
Ollie Lho | cf29de8 | 2004-03-18 19:40:07 +0000 | [diff] [blame] | 139 | flash->name, (0xffffffff - size + 1)); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 140 | return flash; |
| 141 | } |
| 142 | munmap ((void *) bios, size); |
| 143 | flash++; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 144 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 145 | return NULL; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 148 | int verify_flash (struct flashchip * flash, char * buf, int verbose) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 149 | { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 150 | int i = 0; |
| 151 | int total_size = flash->total_size *1024; |
| 152 | volatile char * bios = flash->virt_addr; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 153 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 154 | printf("Verifying address: "); |
Ollie Lho | 789ad3d | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 155 | while (i < total_size) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 156 | if (verbose) |
| 157 | printf("0x%08x", i); |
| 158 | if (*(bios+i) != *(buf+i)) { |
| 159 | printf("FAILED\n"); |
| 160 | return 0; |
| 161 | } |
| 162 | if (verbose) |
| 163 | printf("\b\b\b\b\b\b\b\b\b\b"); |
Ollie Lho | 856943d | 2004-03-18 20:35:33 +0000 | [diff] [blame] | 164 | i++; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 165 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 166 | if (verbose) |
| 167 | printf("\n"); |
| 168 | else |
| 169 | printf("VERIFIED\n"); |
| 170 | return 1; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 173 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 174 | void usage(const char *name) |
| 175 | { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 176 | printf("usage: %s [-rwv] [-c chipname][file]\n", name); |
| 177 | printf("-r: read flash and save into file\n" |
| 178 | "-w: write file into flash (default when file is specified)\n" |
| 179 | "-v: verify flash against file\n" |
| 180 | "-c: probe only for specified flash chip\n" |
| 181 | " If no file is specified, then all that happens\n" |
| 182 | " is that flash info is dumped\n"); |
| 183 | exit(1); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 186 | int main (int argc, char * argv[]) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 187 | { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 188 | char * buf; |
| 189 | unsigned long size; |
| 190 | FILE * image; |
| 191 | struct flashchip * flash; |
| 192 | int opt; |
Ollie Lho | 789ad3d | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 193 | int read_it = 0, write_it = 0, verify_it = 0, verbose = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 194 | char *filename = NULL; |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 195 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 196 | setbuf(stdout, NULL); |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 197 | |
Ollie Lho | 789ad3d | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 198 | while ((opt = getopt(argc, argv, "rwvVc:")) != EOF) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 199 | switch (opt) { |
| 200 | case 'r': |
| 201 | read_it = 1; |
| 202 | break; |
| 203 | case 'w': |
| 204 | write_it = 1; |
| 205 | break; |
| 206 | case 'v': |
| 207 | verify_it = 1; |
| 208 | break; |
| 209 | case 'c': |
| 210 | chip_to_probe = strdup(optarg); |
| 211 | break; |
Ollie Lho | 789ad3d | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 212 | case 'V': |
| 213 | verbose = 1; |
| 214 | break; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 215 | default: |
| 216 | usage(argv[0]); |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | if (read_it && write_it) { |
| 221 | printf("-r and -w are mutually exclusive\n"); |
| 222 | usage(argv[0]); |
| 223 | } |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 224 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 225 | if (optind < argc) |
| 226 | filename = argv[optind++]; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 227 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 228 | printf("Calibrating timer since microsleep sucks ... takes a second\n"); |
| 229 | myusec_calibrate_delay(); |
| 230 | printf("OK, calibrated, now do the deed\n"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 231 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 232 | /* try to enable it. Failure IS an option, since not all motherboards |
| 233 | * really need this to be done, etc., etc. It sucks. |
| 234 | */ |
| 235 | (void) enable_flash_write(); |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 236 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 237 | if ((flash = probe_flash (flashchips)) == NULL) { |
| 238 | printf("EEPROM not found\n"); |
| 239 | exit(1); |
| 240 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 241 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 242 | printf("Part is %s\n", flash->name); |
| 243 | if (!filename){ |
| 244 | printf("OK, only ENABLING flash write, but NOT FLASHING\n"); |
| 245 | return 0; |
| 246 | } |
| 247 | size = flash->total_size * 1024; |
| 248 | buf = (char *) calloc (size, sizeof(char)); |
| 249 | |
| 250 | if (read_it) { |
| 251 | if ((image = fopen (filename, "w")) == NULL) { |
| 252 | perror(filename); |
| 253 | exit(1); |
| 254 | } |
| 255 | printf("Reading Flash..."); |
| 256 | if(flash->read == NULL) |
| 257 | memcpy(buf, (const char *) flash->virt_addr, size); |
| 258 | else |
| 259 | flash->read (flash, buf); |
| 260 | fwrite(buf, sizeof(char), size, image); |
| 261 | fclose(image); |
| 262 | printf("done\n"); |
| 263 | } else { |
| 264 | if ((image = fopen (filename, "r")) == NULL) { |
| 265 | perror(filename); |
| 266 | exit(1); |
| 267 | } |
| 268 | fread (buf, sizeof(char), size, image); |
| 269 | fclose(image); |
| 270 | } |
| 271 | |
| 272 | if (write_it || (!read_it && !verify_it)) |
| 273 | flash->write (flash, buf); |
| 274 | if (verify_it) |
Ollie Lho | 789ad3d | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 275 | verify_flash (flash, buf, verbose); |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 276 | return 0; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 277 | } |