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