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> |
| 35 | |
| 36 | #include "flash.h" |
| 37 | #include "jedec.h" |
Ronald G. Minnich | 3c910ed | 2002-05-28 23:29:17 +0000 | [diff] [blame] | 38 | #include "m29f400bt.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 39 | |
| 40 | struct flashchip flashchips[] = { |
| 41 | {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024, |
| 42 | probe_29f040b, erase_29f040b, write_29f040b}, |
| 43 | {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256, |
| 44 | probe_jedec, erase_jedec, write_jedec}, |
| 45 | {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024, |
| 46 | probe_29f002, erase_29f002, write_29f002}, |
| 47 | {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128, |
| 48 | probe_jedec, erase_jedec, write_jedec}, |
| 49 | {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256, |
| 50 | probe_28sf040, erase_28sf040, write_28sf040}, |
Ronald G. Minnich | c831647 | 2002-03-21 22:40:40 +0000 | [diff] [blame] | 51 | {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096, |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 52 | probe_39sf020, erase_39sf020, write_39sf020}, |
Ollie Lho | 6041bcd | 2002-07-18 03:32:00 +0000 | [diff] [blame^] | 53 | {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096, |
| 54 | probe_39sf020, erase_39sf020, write_39sf020}, |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 55 | {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128, |
| 56 | probe_jedec, erase_jedec, write_jedec}, |
Ronald G. Minnich | 3c910ed | 2002-05-28 23:29:17 +0000 | [diff] [blame] | 57 | {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024, |
| 58 | probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt}, |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 59 | {NULL,} |
| 60 | }; |
| 61 | |
| 62 | int enable_flash_sis630 (void) |
| 63 | { |
| 64 | char b; |
| 65 | |
| 66 | /* get io privilege access PCI configuration space */ |
| 67 | if (iopl(3) != 0) { |
| 68 | perror("Can not set io priviliage"); |
| 69 | exit(1); |
| 70 | } |
| 71 | |
| 72 | /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */ |
| 73 | outl(0x80000840, 0x0cf8); |
| 74 | b = inb(0x0cfc) | 0x0b; |
| 75 | outb(b, 0xcfc); |
| 76 | /* Flash write enable on SiS 540/630 */ |
| 77 | outl(0x80000845, 0x0cf8); |
| 78 | b = inb(0x0cfd) | 0x40; |
| 79 | outb(b, 0xcfd); |
| 80 | |
| 81 | /* The same thing on SiS 950 SuperIO side */ |
| 82 | outb(0x87, 0x2e); |
| 83 | outb(0x01, 0x2e); |
| 84 | outb(0x55, 0x2e); |
| 85 | outb(0x55, 0x2e); |
| 86 | |
| 87 | if (inb(0x2f) != 0x87) { |
| 88 | outb(0x87, 0x4e); |
| 89 | outb(0x01, 0x4e); |
| 90 | outb(0x55, 0x4e); |
| 91 | outb(0xaa, 0x4e); |
| 92 | if (inb(0x4f) != 0x87) { |
| 93 | printf("Can not access SiS 950\n"); |
| 94 | return -1; |
| 95 | } |
| 96 | outb(0x24, 0x4e); |
| 97 | b = inb(0x4f) | 0xfc; |
| 98 | outb(0x24, 0x4e); |
| 99 | outb(b, 0x4f); |
| 100 | outb(0x02, 0x4e); |
| 101 | outb(0x02, 0x4f); |
| 102 | } |
| 103 | |
| 104 | outb(0x24, 0x2e); |
| 105 | printf("2f is %#x\n", inb(0x2f)); |
| 106 | b = inb(0x2f) | 0xfc; |
| 107 | outb(0x24, 0x2e); |
| 108 | outb(b, 0x2f); |
| 109 | |
| 110 | outb(0x02, 0x2e); |
| 111 | outb(0x02, 0x2f); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | struct flashchip * probe_flash(struct flashchip * flash) |
| 117 | { |
| 118 | int fd_mem; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 119 | volatile char * bios; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 120 | unsigned long size; |
| 121 | |
| 122 | if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) { |
| 123 | perror("Can not open /dev/mem"); |
| 124 | exit(1); |
| 125 | } |
| 126 | |
| 127 | while (flash->name != NULL) { |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 128 | printf("Trying %s, %d KB\n", flash->name, flash->total_size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 129 | size = flash->total_size * 1024; |
| 130 | bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 131 | fd_mem, (off_t) (0 - size)); |
| 132 | if (bios == MAP_FAILED) { |
| 133 | perror("Error MMAP /dev/mem"); |
| 134 | exit(1); |
| 135 | } |
| 136 | flash->virt_addr = bios; |
| 137 | |
| 138 | if (flash->probe(flash) == 1) { |
| 139 | printf ("%s found at physical address: 0x%lx\n", |
| 140 | flash->name, (0 - size), bios); |
| 141 | return flash; |
| 142 | } |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 143 | munmap ((void *) bios, size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 144 | flash++; |
| 145 | } |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | int verify_flash (struct flashchip * flash, char * buf) |
| 150 | { |
| 151 | int i = 0; |
| 152 | int total_size = flash->total_size *1024; |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 153 | volatile char * bios = flash->virt_addr; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 154 | |
| 155 | printf("Verifying address: "); |
| 156 | while (i++ < total_size) { |
| 157 | printf("0x%08x", i); |
| 158 | if (*(bios+i) != *(buf+i)) { |
| 159 | return 0; |
| 160 | } |
| 161 | printf("\b\b\b\b\b\b\b\b\b\b"); |
| 162 | } |
| 163 | printf("\n"); |
| 164 | return 1; |
| 165 | } |
| 166 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 167 | // count to a billion. Time it. If it's < 1 sec, count to 10B, etc. |
| 168 | |
| 169 | unsigned long micro = 0; |
| 170 | |
| 171 | void |
| 172 | myusec_calibrate_delay() |
| 173 | { |
| 174 | unsigned long count = 2 * 1024 * 1024; |
| 175 | volatile unsigned long i; |
| 176 | unsigned long timeusec; |
| 177 | struct timeval start, end; |
| 178 | int ok = 0; |
| 179 | |
| 180 | fprintf(stderr, "Setting up microsecond timing loop\n"); |
| 181 | while (! ok) { |
| 182 | fprintf(stderr, "Try %d\n", count); |
| 183 | gettimeofday(&start, 0); |
| 184 | for( i = count; i; i--) |
| 185 | ; |
| 186 | gettimeofday(&end, 0); |
| 187 | timeusec = 1000000 * (end.tv_sec - start.tv_sec ) + |
| 188 | (end.tv_usec - start.tv_usec); |
| 189 | fprintf(stderr, "timeusec is %d\n", timeusec); |
| 190 | count *= 10; |
| 191 | if (timeusec < 1000000) |
| 192 | continue; |
| 193 | ok = 1; |
| 194 | } |
| 195 | |
| 196 | // compute one microsecond. That will be count / time |
| 197 | micro = count / timeusec; |
| 198 | |
| 199 | fprintf(stderr, "one us is %d count\n", micro); |
| 200 | |
| 201 | |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | myusec_delay(time) |
| 206 | { |
| 207 | volatile unsigned long i; |
| 208 | for(i = 0; i < time * micro; i++) |
| 209 | ; |
| 210 | |
| 211 | } |
| 212 | |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 213 | main (int argc, char * argv[]) |
| 214 | { |
| 215 | char * buf; |
| 216 | unsigned long size; |
| 217 | FILE * image; |
| 218 | struct flashchip * flash; |
| 219 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 220 | myusec_calibrate_delay(); |
| 221 | |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 222 | if (argc > 2){ |
| 223 | printf("usage: %s [romimage]\n", argv[0]); |
| 224 | printf(" If no romimage is specified, then all that happens\n"); |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 225 | printf(" is that flash info is dumped\n"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 228 | if ((flash = probe_flash (flashchips)) == NULL) { |
| 229 | printf("EEPROM not found\n"); |
| 230 | exit(1); |
| 231 | } |
| 232 | |
Ronald G. Minnich | ef5779d | 2002-01-29 20:18:02 +0000 | [diff] [blame] | 233 | printf("Part is %s\n", flash->name); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 234 | if (argc < 2){ |
| 235 | printf("OK, only ENABLING flash write, but NOT FLASHING\n"); |
| 236 | exit(0); |
| 237 | } |
| 238 | size = flash->total_size * 1024; |
| 239 | |
| 240 | if ((image = fopen (argv[1], "r")) == NULL) { |
| 241 | perror("Error opening image file"); |
| 242 | exit(1); |
| 243 | } |
| 244 | |
| 245 | buf = (char *) calloc (size, sizeof(char)); |
| 246 | fread (buf, sizeof(char), size, image); |
| 247 | |
| 248 | flash->write (flash, buf); |
| 249 | verify_flash (flash, buf); |
| 250 | } |