Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006 |
| 3 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <asm/processor.h> |
| 26 | |
| 27 | #ifndef CFG_FLASH_READ0 |
| 28 | #define CFG_FLASH_READ0 0x0000 /* 0 is standard */ |
| 29 | #define CFG_FLASH_READ1 0x0001 /* 1 is standard */ |
| 30 | #define CFG_FLASH_READ2 0x0002 /* 2 is standard */ |
| 31 | #endif |
| 32 | |
| 33 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
| 34 | |
| 35 | /* |
| 36 | * Functions |
| 37 | */ |
| 38 | static int write_word(flash_info_t *info, ulong dest, ulong data); |
| 39 | static ulong flash_get_size(vu_long *addr, flash_info_t *info); |
| 40 | |
| 41 | unsigned long flash_init(void) |
| 42 | { |
| 43 | unsigned long size_b0, size_b1; |
| 44 | int i; |
| 45 | unsigned long base_b0, base_b1; |
| 46 | |
| 47 | /* Init: no FLASHes known */ |
| 48 | for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) { |
| 49 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 50 | } |
| 51 | |
| 52 | /* Static FLASH Bank configuration here - FIXME XXX */ |
| 53 | |
| 54 | base_b0 = FLASH_BASE0_PRELIM; |
| 55 | size_b0 = flash_get_size ((vu_long *) base_b0, &flash_info[0]); |
| 56 | |
| 57 | if (flash_info[0].flash_id == FLASH_UNKNOWN) { |
| 58 | printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", |
| 59 | size_b0, size_b0 << 20); |
| 60 | } |
| 61 | |
| 62 | base_b1 = FLASH_BASE1_PRELIM; |
| 63 | size_b1 = flash_get_size ((vu_long *) base_b1, &flash_info[1]); |
| 64 | |
| 65 | return (size_b0 + size_b1); |
| 66 | } |
| 67 | |
| 68 | void flash_print_info(flash_info_t *info) |
| 69 | { |
| 70 | int i; |
| 71 | int k; |
| 72 | int size; |
| 73 | int erased; |
| 74 | volatile unsigned long *flash; |
| 75 | |
| 76 | if (info->flash_id == FLASH_UNKNOWN) { |
| 77 | printf ("missing or unknown FLASH type\n"); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | switch (info->flash_id & FLASH_VENDMASK) { |
| 82 | case FLASH_MAN_AMD: printf ("AMD "); break; |
| 83 | case FLASH_MAN_FUJ: printf ("FUJITSU "); break; |
| 84 | case FLASH_MAN_SST: printf ("SST "); break; |
| 85 | case FLASH_MAN_EXCEL: printf ("Excel Semiconductor "); break; |
| 86 | default: printf ("Unknown Vendor "); break; |
| 87 | } |
| 88 | |
| 89 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 90 | case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
| 91 | break; |
| 92 | case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n"); |
| 93 | break; |
| 94 | case FLASH_AM040: printf ("AM29LV040B (4 Mbit, uniform sector size)\n"); |
| 95 | break; |
| 96 | case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
| 97 | break; |
| 98 | case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n"); |
| 99 | break; |
| 100 | case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
| 101 | break; |
| 102 | case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n"); |
| 103 | break; |
| 104 | case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n"); |
| 105 | break; |
| 106 | case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n"); |
| 107 | break; |
| 108 | case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n"); |
| 109 | break; |
| 110 | case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n"); |
| 111 | break; |
| 112 | case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n"); |
| 113 | break; |
| 114 | case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n"); |
| 115 | break; |
| 116 | case FLASH_SST020: printf ("SST39LF/VF020 (2 Mbit, uniform sector size)\n"); |
| 117 | break; |
| 118 | case FLASH_SST040: printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n"); |
| 119 | break; |
| 120 | default: printf ("Unknown Chip Type\n"); |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | printf (" Size: %ld MB in %d Sectors\n", |
| 125 | info->size >> 20, info->sector_count); |
| 126 | |
| 127 | printf (" Sector Start Addresses:"); |
| 128 | for (i=0; i<info->sector_count; ++i) { |
| 129 | #ifdef CFG_FLASH_EMPTY_INFO |
| 130 | /* |
| 131 | * Check if whole sector is erased |
| 132 | */ |
| 133 | if (i != (info->sector_count-1)) |
| 134 | size = info->start[i+1] - info->start[i]; |
| 135 | else |
| 136 | size = info->start[0] + info->size - info->start[i]; |
| 137 | erased = 1; |
| 138 | flash = (volatile unsigned long *)info->start[i]; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 139 | size = size >> 2; /* divide by 4 for longword access */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 140 | for (k=0; k<size; k++) { |
| 141 | if (*flash++ != 0xffffffff) { |
| 142 | erased = 0; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if ((i % 5) == 0) |
| 148 | printf ("\n "); |
| 149 | /* print empty and read-only info */ |
| 150 | printf (" %08lX%s%s", |
| 151 | info->start[i], |
| 152 | erased ? " E" : " ", |
| 153 | info->protect[i] ? "RO " : " "); |
| 154 | #else |
| 155 | if ((i % 5) == 0) |
| 156 | printf ("\n "); |
| 157 | printf (" %08lX%s", |
| 158 | info->start[i], |
| 159 | info->protect[i] ? " (RO)" : " "); |
| 160 | #endif |
| 161 | |
| 162 | } |
| 163 | printf ("\n"); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * The following code cannot be run from FLASH! |
| 169 | */ |
| 170 | static ulong flash_get_size(vu_long *addr, flash_info_t *info) |
| 171 | { |
| 172 | short i; |
| 173 | short n; |
| 174 | volatile CFG_FLASH_WORD_SIZE value; |
| 175 | ulong base = (ulong)addr; |
| 176 | volatile CFG_FLASH_WORD_SIZE *addr2 = (volatile CFG_FLASH_WORD_SIZE *)addr; |
| 177 | |
| 178 | /* Write auto select command: read Manufacturer ID */ |
| 179 | addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 180 | addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 181 | addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090; |
| 182 | |
| 183 | value = addr2[CFG_FLASH_READ0]; |
| 184 | |
| 185 | switch (value) { |
| 186 | case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT: |
| 187 | info->flash_id = FLASH_MAN_AMD; |
| 188 | break; |
| 189 | case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT: |
| 190 | info->flash_id = FLASH_MAN_FUJ; |
| 191 | break; |
| 192 | case (CFG_FLASH_WORD_SIZE)SST_MANUFACT: |
| 193 | info->flash_id = FLASH_MAN_SST; |
| 194 | break; |
| 195 | case (CFG_FLASH_WORD_SIZE)EXCEL_MANUFACT: |
| 196 | info->flash_id = FLASH_MAN_EXCEL; |
| 197 | break; |
| 198 | default: |
| 199 | info->flash_id = FLASH_UNKNOWN; |
| 200 | info->sector_count = 0; |
| 201 | info->size = 0; |
| 202 | return (0); /* no or unknown flash */ |
| 203 | } |
| 204 | |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 205 | value = addr2[CFG_FLASH_READ1]; /* device ID */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 206 | |
| 207 | switch (value) { |
| 208 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T: |
| 209 | info->flash_id += FLASH_AM400T; |
| 210 | info->sector_count = 11; |
| 211 | info->size = 0x00080000; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 212 | break; /* => 0.5 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 213 | |
| 214 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B: |
| 215 | info->flash_id += FLASH_AM400B; |
| 216 | info->sector_count = 11; |
| 217 | info->size = 0x00080000; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 218 | break; /* => 0.5 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 219 | |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 220 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV040B: |
| 221 | info->flash_id += FLASH_AM040; |
| 222 | info->sector_count = 8; |
| 223 | info->size = 0x0080000; /* => 0.5 MB */ |
| 224 | break; |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 225 | |
| 226 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T: |
| 227 | info->flash_id += FLASH_AM800T; |
| 228 | info->sector_count = 19; |
| 229 | info->size = 0x00100000; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 230 | break; /* => 1 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 231 | |
| 232 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B: |
| 233 | info->flash_id += FLASH_AM800B; |
| 234 | info->sector_count = 19; |
| 235 | info->size = 0x00100000; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 236 | break; /* => 1 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 237 | |
| 238 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T: |
| 239 | info->flash_id += FLASH_AM160T; |
| 240 | info->sector_count = 35; |
| 241 | info->size = 0x00200000; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 242 | break; /* => 2 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 243 | |
| 244 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B: |
| 245 | info->flash_id += FLASH_AM160B; |
| 246 | info->sector_count = 35; |
| 247 | info->size = 0x00200000; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 248 | break; /* => 2 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 249 | |
| 250 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T: |
| 251 | info->flash_id += FLASH_AM320T; |
| 252 | info->sector_count = 71; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 253 | info->size = 0x00400000; |
| 254 | break; /* => 4 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 255 | |
| 256 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B: |
| 257 | info->flash_id += FLASH_AM320B; |
| 258 | info->sector_count = 71; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 259 | info->size = 0x00400000; |
| 260 | break; /* => 4 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 261 | |
| 262 | case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322T: |
| 263 | info->flash_id += FLASH_AMDL322T; |
| 264 | info->sector_count = 71; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 265 | info->size = 0x00400000; |
| 266 | break; /* => 4 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 267 | |
| 268 | case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322B: |
| 269 | info->flash_id += FLASH_AMDL322B; |
| 270 | info->sector_count = 71; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 271 | info->size = 0x00400000; |
| 272 | break; /* => 4 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 273 | |
| 274 | case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323T: |
| 275 | info->flash_id += FLASH_AMDL323T; |
| 276 | info->sector_count = 71; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 277 | info->size = 0x00400000; |
| 278 | break; /* => 4 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 279 | |
| 280 | case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323B: |
| 281 | info->flash_id += FLASH_AMDL323B; |
| 282 | info->sector_count = 71; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 283 | info->size = 0x00400000; |
| 284 | break; /* => 4 MB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 285 | |
| 286 | case (CFG_FLASH_WORD_SIZE)SST_ID_xF020: |
| 287 | info->flash_id += FLASH_SST020; |
| 288 | info->sector_count = 64; |
| 289 | info->size = 0x00040000; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 290 | break; /* => 256 kB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 291 | |
| 292 | case (CFG_FLASH_WORD_SIZE)SST_ID_xF040: |
| 293 | info->flash_id += FLASH_SST040; |
| 294 | info->sector_count = 128; |
| 295 | info->size = 0x00080000; |
Wolfgang Denk | aaa3214 | 2006-06-02 17:04:21 +0200 | [diff] [blame] | 296 | break; /* => 512 kB */ |
Stefan Roese | 9eba0c8 | 2006-06-02 16:18:04 +0200 | [diff] [blame] | 297 | |
| 298 | default: |
| 299 | info->flash_id = FLASH_UNKNOWN; |
| 300 | return (0); /* => no or unknown flash */ |
| 301 | |
| 302 | } |
| 303 | |
| 304 | /* set up sector start address table */ |
| 305 | if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) || |
| 306 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) { |
| 307 | for (i = 0; i < info->sector_count; i++) |
| 308 | info->start[i] = base + (i * 0x00001000); |
| 309 | } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) { |
| 310 | for (i = 0; i < info->sector_count; i++) |
| 311 | info->start[i] = base + (i * 0x00010000); |
| 312 | } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) || |
| 313 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) || |
| 314 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) || |
| 315 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) { |
| 316 | /* set sector offsets for bottom boot block type */ |
| 317 | for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */ |
| 318 | info->start[i] = base; |
| 319 | base += 8 << 10; |
| 320 | } |
| 321 | while (i < info->sector_count) { /* 64k regular sectors */ |
| 322 | info->start[i] = base; |
| 323 | base += 64 << 10; |
| 324 | ++i; |
| 325 | } |
| 326 | } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) || |
| 327 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) || |
| 328 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) || |
| 329 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) { |
| 330 | /* set sector offsets for top boot block type */ |
| 331 | base += info->size; |
| 332 | i = info->sector_count; |
| 333 | for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */ |
| 334 | base -= 8 << 10; |
| 335 | --i; |
| 336 | info->start[i] = base; |
| 337 | } |
| 338 | while (i > 0) { /* 64k regular sectors */ |
| 339 | base -= 64 << 10; |
| 340 | --i; |
| 341 | info->start[i] = base; |
| 342 | } |
| 343 | } else { |
| 344 | if (info->flash_id & FLASH_BTYPE) { |
| 345 | /* set sector offsets for bottom boot block type */ |
| 346 | info->start[0] = base + 0x00000000; |
| 347 | info->start[1] = base + 0x00004000; |
| 348 | info->start[2] = base + 0x00006000; |
| 349 | info->start[3] = base + 0x00008000; |
| 350 | for (i = 4; i < info->sector_count; i++) { |
| 351 | info->start[i] = base + (i * 0x00010000) - 0x00030000; |
| 352 | } |
| 353 | } else { |
| 354 | /* set sector offsets for top boot block type */ |
| 355 | i = info->sector_count - 1; |
| 356 | info->start[i--] = base + info->size - 0x00004000; |
| 357 | info->start[i--] = base + info->size - 0x00006000; |
| 358 | info->start[i--] = base + info->size - 0x00008000; |
| 359 | for (; i >= 0; i--) { |
| 360 | info->start[i] = base + i * 0x00010000; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /* check for protected sectors */ |
| 366 | for (i = 0; i < info->sector_count; i++) { |
| 367 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 368 | /* D0 = 1 if protected */ |
| 369 | addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]); |
| 370 | if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD) |
| 371 | info->protect[i] = 0; |
| 372 | else |
| 373 | info->protect[i] = addr2[CFG_FLASH_READ2] & 1; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Prevent writes to uninitialized FLASH. |
| 378 | */ |
| 379 | if (info->flash_id != FLASH_UNKNOWN) { |
| 380 | addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0]; |
| 381 | *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */ |
| 382 | } |
| 383 | |
| 384 | return (info->size); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | int flash_erase(flash_info_t *info, int s_first, int s_last) |
| 389 | { |
| 390 | volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]); |
| 391 | volatile CFG_FLASH_WORD_SIZE *addr2; |
| 392 | int flag, prot, sect, l_sect; |
| 393 | ulong start, now, last; |
| 394 | |
| 395 | if ((s_first < 0) || (s_first > s_last)) { |
| 396 | if (info->flash_id == FLASH_UNKNOWN) |
| 397 | printf ("- missing\n"); |
| 398 | else |
| 399 | printf ("- no sectors to erase\n"); |
| 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | if (info->flash_id == FLASH_UNKNOWN) { |
| 404 | printf ("Can't erase unknown flash type - aborted\n"); |
| 405 | return 1; |
| 406 | } |
| 407 | |
| 408 | prot = 0; |
| 409 | for (sect=s_first; sect<=s_last; ++sect) |
| 410 | if (info->protect[sect]) |
| 411 | prot++; |
| 412 | |
| 413 | if (prot) |
| 414 | printf ("- Warning: %d protected sectors will not be erased!\n", prot); |
| 415 | else |
| 416 | printf ("\n"); |
| 417 | |
| 418 | l_sect = -1; |
| 419 | |
| 420 | /* Disable interrupts which might cause a timeout here */ |
| 421 | flag = disable_interrupts(); |
| 422 | |
| 423 | /* Start erase on unprotected sectors */ |
| 424 | for (sect = s_first; sect<=s_last; sect++) { |
| 425 | if (info->protect[sect] == 0) { /* not protected */ |
| 426 | addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]); |
| 427 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) { |
| 428 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 429 | addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 430 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080; |
| 431 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 432 | addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 433 | addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */ |
| 434 | |
| 435 | /* re-enable interrupts if necessary */ |
| 436 | if (flag) { |
| 437 | enable_interrupts(); |
| 438 | flag = 0; |
| 439 | } |
| 440 | |
| 441 | /* data polling for D7 */ |
| 442 | start = get_timer (0); |
| 443 | while ((addr2[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != |
| 444 | (CFG_FLASH_WORD_SIZE)0x00800080) { |
| 445 | if (get_timer(start) > CFG_FLASH_WRITE_TOUT) |
| 446 | return (1); |
| 447 | } |
| 448 | } else { |
| 449 | if (sect == s_first) { |
| 450 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 451 | addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 452 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080; |
| 453 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 454 | addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 455 | } |
| 456 | addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */ |
| 457 | } |
| 458 | l_sect = sect; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /* re-enable interrupts if necessary */ |
| 463 | if (flag) |
| 464 | enable_interrupts(); |
| 465 | |
| 466 | /* wait at least 80us - let's wait 1 ms */ |
| 467 | udelay (1000); |
| 468 | |
| 469 | /* |
| 470 | * We wait for the last triggered sector |
| 471 | */ |
| 472 | if (l_sect < 0) |
| 473 | goto DONE; |
| 474 | |
| 475 | start = get_timer (0); |
| 476 | last = start; |
| 477 | addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]); |
| 478 | while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) { |
| 479 | if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) { |
| 480 | printf ("Timeout\n"); |
| 481 | return 1; |
| 482 | } |
| 483 | /* show that we're waiting */ |
| 484 | if ((now - last) > 1000) { /* every second */ |
| 485 | putc ('.'); |
| 486 | last = now; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | DONE: |
| 491 | /* reset to read mode */ |
| 492 | addr = (CFG_FLASH_WORD_SIZE *)info->start[0]; |
| 493 | addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */ |
| 494 | |
| 495 | printf (" done\n"); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Copy memory to flash, returns: |
| 501 | * 0 - OK |
| 502 | * 1 - write timeout |
| 503 | * 2 - Flash not erased |
| 504 | */ |
| 505 | int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 506 | { |
| 507 | ulong cp, wp, data; |
| 508 | int i, l, rc; |
| 509 | |
| 510 | wp = (addr & ~3); /* get lower word aligned address */ |
| 511 | |
| 512 | /* |
| 513 | * handle unaligned start bytes |
| 514 | */ |
| 515 | if ((l = addr - wp) != 0) { |
| 516 | data = 0; |
| 517 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
| 518 | data = (data << 8) | (*(uchar *)cp); |
| 519 | } |
| 520 | for (; i<4 && cnt>0; ++i) { |
| 521 | data = (data << 8) | *src++; |
| 522 | --cnt; |
| 523 | ++cp; |
| 524 | } |
| 525 | for (; cnt==0 && i<4; ++i, ++cp) { |
| 526 | data = (data << 8) | (*(uchar *)cp); |
| 527 | } |
| 528 | |
| 529 | if ((rc = write_word(info, wp, data)) != 0) { |
| 530 | return (rc); |
| 531 | } |
| 532 | wp += 4; |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * handle word aligned part |
| 537 | */ |
| 538 | while (cnt >= 4) { |
| 539 | data = 0; |
| 540 | for (i=0; i<4; ++i) |
| 541 | data = (data << 8) | *src++; |
| 542 | if ((rc = write_word(info, wp, data)) != 0) |
| 543 | return (rc); |
| 544 | wp += 4; |
| 545 | cnt -= 4; |
| 546 | } |
| 547 | |
| 548 | if (cnt == 0) |
| 549 | return (0); |
| 550 | |
| 551 | /* |
| 552 | * handle unaligned tail bytes |
| 553 | */ |
| 554 | data = 0; |
| 555 | for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) { |
| 556 | data = (data << 8) | *src++; |
| 557 | --cnt; |
| 558 | } |
| 559 | for (; i<4; ++i, ++cp) |
| 560 | data = (data << 8) | (*(uchar *)cp); |
| 561 | |
| 562 | return (write_word(info, wp, data)); |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Write a word to Flash, returns: |
| 567 | * 0 - OK |
| 568 | * 1 - write timeout |
| 569 | * 2 - Flash not erased |
| 570 | */ |
| 571 | static int write_word(flash_info_t *info, ulong dest, ulong data) |
| 572 | { |
| 573 | volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]); |
| 574 | volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest; |
| 575 | volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data; |
| 576 | ulong start; |
| 577 | int flag; |
| 578 | int i; |
| 579 | |
| 580 | /* Check if Flash is (sufficiently) erased */ |
| 581 | if ((*((vu_long *)dest) & data) != data) |
| 582 | return (2); |
| 583 | |
| 584 | /* Disable interrupts which might cause a timeout here */ |
| 585 | flag = disable_interrupts(); |
| 586 | |
| 587 | for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++) { |
| 588 | addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 589 | addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 590 | addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0; |
| 591 | |
| 592 | dest2[i] = data2[i]; |
| 593 | |
| 594 | /* re-enable interrupts if necessary */ |
| 595 | if (flag) |
| 596 | enable_interrupts(); |
| 597 | |
| 598 | /* data polling for D7 */ |
| 599 | start = get_timer (0); |
| 600 | while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) != |
| 601 | (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) { |
| 602 | if (get_timer(start) > CFG_FLASH_WRITE_TOUT) |
| 603 | return (1); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | return (0); |
| 608 | } |