wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com |
| 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> |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 25 | #include <asm/processor.h> |
| 26 | |
| 27 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
| 28 | |
| 29 | /*----------------------------------------------------------------------- |
| 30 | * Functions |
| 31 | */ |
| 32 | static int write_word (flash_info_t *info, ulong dest, ulong data); |
| 33 | |
| 34 | /*----------------------------------------------------------------------- |
| 35 | */ |
| 36 | static void flash_get_offsets (ulong base, flash_info_t *info) |
| 37 | { |
| 38 | int i; |
| 39 | short n; |
| 40 | |
| 41 | /* set up sector start address table */ |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 42 | if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) || |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 43 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) { |
| 44 | for (i = 0; i < info->sector_count; i++) |
| 45 | info->start[i] = base + (i * 0x00010000); |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 46 | } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) || |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 47 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) || |
| 48 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) || |
| 49 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) { |
| 50 | /* set sector offsets for bottom boot block type */ |
| 51 | for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */ |
| 52 | info->start[i] = base; |
| 53 | base += 8 << 10; |
| 54 | } |
| 55 | while (i < info->sector_count) { /* 64k regular sectors */ |
| 56 | info->start[i] = base; |
| 57 | base += 64 << 10; |
| 58 | ++i; |
| 59 | } |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 60 | } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) || |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 61 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) || |
| 62 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) || |
| 63 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) { |
| 64 | /* set sector offsets for top boot block type */ |
| 65 | base += info->size; |
| 66 | i = info->sector_count; |
| 67 | for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */ |
| 68 | base -= 8 << 10; |
| 69 | --i; |
| 70 | info->start[i] = base; |
| 71 | } |
| 72 | while (i > 0) { /* 64k regular sectors */ |
| 73 | base -= 64 << 10; |
| 74 | --i; |
| 75 | info->start[i] = base; |
| 76 | } |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 77 | } else { |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 78 | if (info->flash_id & FLASH_BTYPE) { |
| 79 | /* set sector offsets for bottom boot block type */ |
| 80 | info->start[0] = base + 0x00000000; |
| 81 | info->start[1] = base + 0x00004000; |
| 82 | info->start[2] = base + 0x00006000; |
| 83 | info->start[3] = base + 0x00008000; |
| 84 | for (i = 4; i < info->sector_count; i++) { |
| 85 | info->start[i] = base + (i * 0x00010000) - 0x00030000; |
| 86 | } |
| 87 | } else { |
| 88 | /* set sector offsets for top boot block type */ |
| 89 | i = info->sector_count - 1; |
| 90 | info->start[i--] = base + info->size - 0x00004000; |
| 91 | info->start[i--] = base + info->size - 0x00006000; |
| 92 | info->start[i--] = base + info->size - 0x00008000; |
| 93 | for (; i >= 0; i--) { |
| 94 | info->start[i] = base + i * 0x00010000; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /*----------------------------------------------------------------------- |
| 101 | */ |
| 102 | void flash_print_info (flash_info_t *info) |
| 103 | { |
| 104 | int i; |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 105 | int k; |
| 106 | int size; |
| 107 | int erased; |
| 108 | volatile unsigned long *flash; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 109 | |
| 110 | if (info->flash_id == FLASH_UNKNOWN) { |
| 111 | printf ("missing or unknown FLASH type\n"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | switch (info->flash_id & FLASH_VENDMASK) { |
| 116 | case FLASH_MAN_AMD: printf ("AMD "); break; |
| 117 | case FLASH_MAN_FUJ: printf ("FUJITSU "); break; |
| 118 | case FLASH_MAN_SST: printf ("SST "); break; |
| 119 | case FLASH_MAN_STM: printf ("ST "); break; |
| 120 | default: printf ("Unknown Vendor "); break; |
| 121 | } |
| 122 | |
| 123 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 124 | case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
| 125 | break; |
| 126 | case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n"); |
| 127 | break; |
| 128 | case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
| 129 | break; |
| 130 | case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n"); |
| 131 | break; |
| 132 | case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
| 133 | break; |
| 134 | case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n"); |
| 135 | break; |
| 136 | case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n"); |
| 137 | break; |
| 138 | case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n"); |
| 139 | break; |
| 140 | case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n"); |
| 141 | break; |
| 142 | case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n"); |
| 143 | break; |
| 144 | case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n"); |
| 145 | break; |
| 146 | case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n"); |
| 147 | break; |
| 148 | case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n"); |
| 149 | break; |
| 150 | case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n"); |
| 151 | break; |
| 152 | case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n"); |
| 153 | break; |
| 154 | case FLASH_STMW320DT: printf ("M29W320DT (32 M, top sector)\n"); |
| 155 | break; |
| 156 | default: printf ("Unknown Chip Type\n"); |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | printf (" Size: %ld MB in %d Sectors\n", |
| 161 | info->size >> 20, info->sector_count); |
| 162 | |
| 163 | printf (" Sector Start Addresses:"); |
| 164 | for (i=0; i<info->sector_count; ++i) { |
| 165 | #ifdef CFG_FLASH_EMPTY_INFO |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 166 | /* |
| 167 | * Check if whole sector is erased |
| 168 | */ |
| 169 | if (i != (info->sector_count-1)) |
| 170 | size = info->start[i+1] - info->start[i]; |
| 171 | else |
| 172 | size = info->start[0] + info->size - info->start[i]; |
| 173 | erased = 1; |
| 174 | flash = (volatile unsigned long *)info->start[i]; |
| 175 | size = size >> 2; /* divide by 4 for longword access */ |
| 176 | for (k=0; k<size; k++) |
| 177 | { |
| 178 | if (*flash++ != 0xffffffff) |
| 179 | { |
| 180 | erased = 0; |
| 181 | break; |
| 182 | } |
| 183 | } |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 184 | |
| 185 | if ((i % 5) == 0) |
| 186 | printf ("\n "); |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 187 | /* print empty and read-only info */ |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 188 | printf (" %08lX%s%s", |
| 189 | info->start[i], |
| 190 | erased ? " E" : " ", |
| 191 | info->protect[i] ? "RO " : " "); |
| 192 | #else |
| 193 | if ((i % 5) == 0) |
| 194 | printf ("\n "); |
| 195 | printf (" %08lX%s", |
| 196 | info->start[i], |
| 197 | info->protect[i] ? " (RO)" : " "); |
| 198 | #endif |
| 199 | |
| 200 | } |
| 201 | printf ("\n"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | /*----------------------------------------------------------------------- |
| 206 | */ |
| 207 | |
| 208 | |
| 209 | /*----------------------------------------------------------------------- |
| 210 | */ |
| 211 | |
| 212 | /* |
| 213 | * The following code cannot be run from FLASH! |
| 214 | */ |
| 215 | static ulong flash_get_size (vu_long *addr, flash_info_t *info) |
| 216 | { |
| 217 | short i; |
| 218 | short n; |
| 219 | CFG_FLASH_WORD_SIZE value; |
| 220 | ulong base = (ulong)addr; |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 221 | volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)addr; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 222 | |
wdenk | 99874b4 | 2004-07-01 21:40:08 +0000 | [diff] [blame] | 223 | debug("[%s, %d] Entering ...\n", __FUNCTION__, __LINE__); |
| 224 | |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 225 | /* Write auto select command: read Manufacturer ID */ |
| 226 | addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 227 | addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 228 | addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090; |
| 229 | |
| 230 | value = addr2[CFG_FLASH_READ0]; |
| 231 | |
| 232 | switch (value) { |
| 233 | case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT: |
| 234 | info->flash_id = FLASH_MAN_AMD; |
| 235 | break; |
| 236 | case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT: |
| 237 | info->flash_id = FLASH_MAN_FUJ; |
| 238 | break; |
| 239 | case (CFG_FLASH_WORD_SIZE)SST_MANUFACT: |
| 240 | info->flash_id = FLASH_MAN_SST; |
| 241 | break; |
| 242 | case (CFG_FLASH_WORD_SIZE)STM_MANUFACT: |
| 243 | info->flash_id = FLASH_MAN_STM; |
| 244 | break; |
| 245 | default: |
| 246 | info->flash_id = FLASH_UNKNOWN; |
| 247 | info->sector_count = 0; |
| 248 | info->size = 0; |
| 249 | return (0); /* no or unknown flash */ |
| 250 | } |
| 251 | |
| 252 | value = addr2[CFG_FLASH_READ1]; /* device ID */ |
| 253 | |
| 254 | switch (value) { |
| 255 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T: |
| 256 | info->flash_id += FLASH_AM400T; |
| 257 | info->sector_count = 11; |
| 258 | info->size = 0x00080000; |
| 259 | break; /* => 0.5 MB */ |
| 260 | |
| 261 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B: |
| 262 | info->flash_id += FLASH_AM400B; |
| 263 | info->sector_count = 11; |
| 264 | info->size = 0x00080000; |
| 265 | break; /* => 0.5 MB */ |
| 266 | |
| 267 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T: |
| 268 | info->flash_id += FLASH_AM800T; |
| 269 | info->sector_count = 19; |
| 270 | info->size = 0x00100000; |
| 271 | break; /* => 1 MB */ |
| 272 | |
| 273 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B: |
| 274 | info->flash_id += FLASH_AM800B; |
| 275 | info->sector_count = 19; |
| 276 | info->size = 0x00100000; |
| 277 | break; /* => 1 MB */ |
| 278 | |
| 279 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T: |
| 280 | info->flash_id += FLASH_AM160T; |
| 281 | info->sector_count = 35; |
| 282 | info->size = 0x00200000; |
| 283 | break; /* => 2 MB */ |
| 284 | |
| 285 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B: |
| 286 | info->flash_id += FLASH_AM160B; |
| 287 | info->sector_count = 35; |
| 288 | info->size = 0x00200000; |
| 289 | break; /* => 2 MB */ |
| 290 | |
| 291 | case (CFG_FLASH_WORD_SIZE)STM_ID_29W320DT: |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 292 | info->flash_id += FLASH_STMW320DT; |
| 293 | info->sector_count = 67; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 294 | info->size = 0x00400000; break; /* => 4 MB */ |
| 295 | |
| 296 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T: |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 297 | info->flash_id += FLASH_AM320T; |
| 298 | info->sector_count = 71; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 299 | info->size = 0x00400000; break; /* => 4 MB */ |
| 300 | |
| 301 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B: |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 302 | info->flash_id += FLASH_AM320B; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 303 | info->sector_count = 71; |
| 304 | info->size = 0x00400000; break; /* => 4 MB */ |
| 305 | |
| 306 | case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322T: |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 307 | info->flash_id += FLASH_AMDL322T; |
| 308 | info->sector_count = 71; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 309 | info->size = 0x00400000; break; /* => 4 MB */ |
| 310 | |
| 311 | case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322B: |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 312 | info->flash_id += FLASH_AMDL322B; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 313 | info->sector_count = 71; |
| 314 | info->size = 0x00400000; break; /* => 4 MB */ |
| 315 | |
| 316 | case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323T: |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 317 | info->flash_id += FLASH_AMDL323T; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 318 | info->sector_count = 71; |
| 319 | info->size = 0x00400000; break; /* => 4 MB */ |
| 320 | |
| 321 | case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323B: |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 322 | info->flash_id += FLASH_AMDL323B; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 323 | info->sector_count = 71; |
| 324 | info->size = 0x00400000; break; /* => 4 MB */ |
| 325 | |
| 326 | case (CFG_FLASH_WORD_SIZE)AMD_ID_LV640U: |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 327 | info->flash_id += FLASH_AM640U; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 328 | info->sector_count = 128; |
| 329 | info->size = 0x00800000; break; /* => 8 MB */ |
| 330 | |
| 331 | case (CFG_FLASH_WORD_SIZE)SST_ID_xF800A: |
| 332 | info->flash_id += FLASH_SST800A; |
| 333 | info->sector_count = 16; |
| 334 | info->size = 0x00100000; |
| 335 | break; /* => 1 MB */ |
| 336 | |
| 337 | case (CFG_FLASH_WORD_SIZE)SST_ID_xF160A: |
| 338 | info->flash_id += FLASH_SST160A; |
| 339 | info->sector_count = 32; |
| 340 | info->size = 0x00200000; |
| 341 | break; /* => 2 MB */ |
| 342 | |
| 343 | default: |
| 344 | info->flash_id = FLASH_UNKNOWN; |
| 345 | return (0); /* => no or unknown flash */ |
| 346 | |
| 347 | } |
| 348 | |
| 349 | /* set up sector start address table */ |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 350 | if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) || |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 351 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) { |
| 352 | for (i = 0; i < info->sector_count; i++) |
| 353 | info->start[i] = base + (i * 0x00010000); |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 354 | } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) || |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 355 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) || |
| 356 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) || |
| 357 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) { |
| 358 | /* set sector offsets for bottom boot block type */ |
| 359 | for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */ |
| 360 | info->start[i] = base; |
| 361 | base += 8 << 10; |
| 362 | } |
| 363 | while (i < info->sector_count) { /* 64k regular sectors */ |
| 364 | info->start[i] = base; |
| 365 | base += 64 << 10; |
| 366 | ++i; |
| 367 | } |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 368 | } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) || |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 369 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) || |
| 370 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) || |
| 371 | ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) { |
| 372 | /* set sector offsets for top boot block type */ |
| 373 | base += info->size; |
| 374 | i = info->sector_count; |
| 375 | for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */ |
| 376 | base -= 8 << 10; |
| 377 | --i; |
| 378 | info->start[i] = base; |
| 379 | } |
| 380 | while (i > 0) { /* 64k regular sectors */ |
| 381 | base -= 64 << 10; |
| 382 | --i; |
| 383 | info->start[i] = base; |
| 384 | } |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 385 | } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) { |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 386 | /* set sector offsets for top boot block type */ |
| 387 | base += info->size; |
| 388 | i = info->sector_count; |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 389 | /* 1 x 16k boot sector */ |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 390 | base -= 16 << 10; |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 391 | --i; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 392 | info->start[i] = base; |
| 393 | /* 2 x 8k boot sectors */ |
| 394 | for (n=0; n<2; ++n) { |
| 395 | base -= 8 << 10; |
| 396 | --i; |
| 397 | info->start[i] = base; |
| 398 | } |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 399 | /* 1 x 32k boot sector */ |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 400 | base -= 32 << 10; |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 401 | --i; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 402 | info->start[i] = base; |
| 403 | |
| 404 | while (i > 0) { /* 64k regular sectors */ |
| 405 | base -= 64 << 10; |
| 406 | --i; |
| 407 | info->start[i] = base; |
| 408 | } |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 409 | } else { |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 410 | if (info->flash_id & FLASH_BTYPE) { |
| 411 | /* set sector offsets for bottom boot block type */ |
| 412 | info->start[0] = base + 0x00000000; |
| 413 | info->start[1] = base + 0x00004000; |
| 414 | info->start[2] = base + 0x00006000; |
| 415 | info->start[3] = base + 0x00008000; |
| 416 | for (i = 4; i < info->sector_count; i++) { |
| 417 | info->start[i] = base + (i * 0x00010000) - 0x00030000; |
| 418 | } |
| 419 | } else { |
| 420 | /* set sector offsets for top boot block type */ |
| 421 | i = info->sector_count - 1; |
| 422 | info->start[i--] = base + info->size - 0x00004000; |
| 423 | info->start[i--] = base + info->size - 0x00006000; |
| 424 | info->start[i--] = base + info->size - 0x00008000; |
| 425 | for (; i >= 0; i--) { |
| 426 | info->start[i] = base + i * 0x00010000; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* check for protected sectors */ |
| 432 | for (i = 0; i < info->sector_count; i++) { |
| 433 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 434 | /* D0 = 1 if protected */ |
| 435 | addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]); |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 436 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) |
| 437 | info->protect[i] = 0; |
| 438 | else |
| 439 | info->protect[i] = addr2[CFG_FLASH_READ2] & 1; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Prevent writes to uninitialized FLASH. |
| 444 | */ |
| 445 | if (info->flash_id != FLASH_UNKNOWN) { |
| 446 | addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0]; |
| 447 | *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */ |
| 448 | } |
| 449 | |
| 450 | return (info->size); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | /*----------------------------------------------------------------------- |
| 455 | */ |
| 456 | |
| 457 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 458 | { |
| 459 | volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]); |
| 460 | volatile CFG_FLASH_WORD_SIZE *addr2; |
| 461 | int flag, prot, sect, l_sect; |
| 462 | ulong start, now, last; |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 463 | int i; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 464 | |
| 465 | if ((s_first < 0) || (s_first > s_last)) { |
| 466 | if (info->flash_id == FLASH_UNKNOWN) { |
| 467 | printf ("- missing\n"); |
| 468 | } else { |
| 469 | printf ("- no sectors to erase\n"); |
| 470 | } |
| 471 | return 1; |
| 472 | } |
| 473 | |
| 474 | if (info->flash_id == FLASH_UNKNOWN) { |
| 475 | printf ("Can't erase unknown flash type - aborted\n"); |
| 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | prot = 0; |
| 480 | for (sect=s_first; sect<=s_last; ++sect) { |
| 481 | if (info->protect[sect]) { |
| 482 | prot++; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (prot) { |
| 487 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 488 | prot); |
| 489 | } else { |
| 490 | printf ("\n"); |
| 491 | } |
| 492 | |
| 493 | l_sect = -1; |
| 494 | |
| 495 | /* Disable interrupts which might cause a timeout here */ |
| 496 | flag = disable_interrupts(); |
| 497 | |
| 498 | /* Start erase on unprotected sectors */ |
| 499 | for (sect = s_first; sect<=s_last; sect++) { |
| 500 | if (info->protect[sect] == 0) { /* not protected */ |
| 501 | addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]); |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 502 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) { |
| 503 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 504 | addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 505 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080; |
| 506 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 507 | addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 508 | addr2[0] = (CFG_FLASH_WORD_SIZE)0x00500050; /* block erase */ |
| 509 | for (i=0; i<50; i++) |
| 510 | udelay(1000); /* wait 1 ms */ |
| 511 | } else { |
| 512 | if (sect == s_first) { |
| 513 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 514 | addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 515 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080; |
| 516 | addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 517 | addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 518 | } |
| 519 | addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */ |
| 520 | } |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 521 | l_sect = sect; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /* re-enable interrupts if necessary */ |
| 526 | if (flag) |
| 527 | enable_interrupts(); |
| 528 | |
| 529 | /* wait at least 80us - let's wait 1 ms */ |
| 530 | udelay (1000); |
| 531 | |
| 532 | /* |
| 533 | * We wait for the last triggered sector |
| 534 | */ |
| 535 | if (l_sect < 0) |
| 536 | goto DONE; |
| 537 | |
| 538 | start = get_timer (0); |
| 539 | last = start; |
| 540 | addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]); |
| 541 | while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) { |
| 542 | if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) { |
| 543 | printf ("Timeout\n"); |
| 544 | return 1; |
| 545 | } |
| 546 | /* show that we're waiting */ |
| 547 | if ((now - last) > 1000) { /* every second */ |
| 548 | putc ('.'); |
| 549 | last = now; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | DONE: |
| 554 | /* reset to read mode */ |
| 555 | addr = (CFG_FLASH_WORD_SIZE *)info->start[0]; |
| 556 | addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */ |
| 557 | |
| 558 | printf (" done\n"); |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /*----------------------------------------------------------------------- |
| 563 | * Copy memory to flash, returns: |
| 564 | * 0 - OK |
| 565 | * 1 - write timeout |
| 566 | * 2 - Flash not erased |
| 567 | */ |
| 568 | |
| 569 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 570 | { |
| 571 | ulong cp, wp, data; |
| 572 | int i, l, rc; |
| 573 | |
| 574 | wp = (addr & ~3); /* get lower word aligned address */ |
| 575 | |
| 576 | /* |
| 577 | * handle unaligned start bytes |
| 578 | */ |
| 579 | if ((l = addr - wp) != 0) { |
| 580 | data = 0; |
| 581 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 582 | #ifdef CONFIG_B2 |
| 583 | data = data | ((*(uchar *)cp)<<(8*i)); |
| 584 | #else |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 585 | data = (data << 8) | (*(uchar *)cp); |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 586 | #endif |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 587 | } |
| 588 | for (; i<4 && cnt>0; ++i) { |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 589 | #ifdef CONFIG_B2 |
| 590 | data = data | ((*src++)<<(8*i)); |
| 591 | #else |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 592 | data = (data << 8) | *src++; |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 593 | #endif |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 594 | --cnt; |
| 595 | ++cp; |
| 596 | } |
| 597 | for (; cnt==0 && i<4; ++i, ++cp) { |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 598 | #ifdef CONFIG_B2 |
| 599 | data = data | ((*(uchar *)cp)<<(8*i)); |
| 600 | #else |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 601 | data = (data << 8) | (*(uchar *)cp); |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 602 | #endif |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | if ((rc = write_word(info, wp, data)) != 0) { |
| 606 | return (rc); |
| 607 | } |
| 608 | wp += 4; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * handle word aligned part |
| 613 | */ |
| 614 | while (cnt >= 4) { |
| 615 | data = 0; |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 616 | #ifdef CONFIG_B2 |
| 617 | data = (*(ulong*)src); |
| 618 | src += 4; |
| 619 | #else |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 620 | for (i=0; i<4; ++i) { |
| 621 | data = (data << 8) | *src++; |
| 622 | } |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 623 | #endif |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 624 | if ((rc = write_word(info, wp, data)) != 0) { |
| 625 | return (rc); |
| 626 | } |
| 627 | wp += 4; |
| 628 | cnt -= 4; |
| 629 | } |
| 630 | |
| 631 | if (cnt == 0) { |
| 632 | return (0); |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * handle unaligned tail bytes |
| 637 | */ |
| 638 | data = 0; |
| 639 | for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) { |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 640 | #ifdef CONFIG_B2 |
| 641 | data = data | ((*src++)<<(8*i)); |
| 642 | #else |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 643 | data = (data << 8) | *src++; |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 644 | #endif |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 645 | --cnt; |
| 646 | } |
| 647 | for (; i<4; ++i, ++cp) { |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 648 | #ifdef CONFIG_B2 |
| 649 | data = data | ((*(uchar *)cp)<<(8*i)); |
| 650 | #else |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 651 | data = (data << 8) | (*(uchar *)cp); |
wdenk | b98ac28 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 652 | #endif |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | return (write_word(info, wp, data)); |
| 656 | } |
| 657 | |
| 658 | /*----------------------------------------------------------------------- |
| 659 | * Write a word to Flash, returns: |
| 660 | * 0 - OK |
| 661 | * 1 - write timeout |
| 662 | * 2 - Flash not erased |
| 663 | */ |
| 664 | static int write_word (flash_info_t *info, ulong dest, ulong data) |
| 665 | { |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 666 | volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]); |
| 667 | volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest; |
| 668 | volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 669 | ulong start; |
| 670 | int flag; |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 671 | int i; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 672 | |
| 673 | /* Check if Flash is (sufficiently) erased */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 674 | if ((*((volatile ulong *)dest) & data) != data) { |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 675 | return (2); |
| 676 | } |
| 677 | /* Disable interrupts which might cause a timeout here */ |
| 678 | flag = disable_interrupts(); |
| 679 | |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 680 | for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++) |
| 681 | { |
| 682 | addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA; |
| 683 | addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055; |
| 684 | addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 685 | |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 686 | dest2[i] = data2[i]; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 687 | |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 688 | /* re-enable interrupts if necessary */ |
| 689 | if (flag) |
| 690 | enable_interrupts(); |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 691 | |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 692 | /* data polling for D7 */ |
| 693 | start = get_timer (0); |
| 694 | while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) != |
| 695 | (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) { |
| 696 | if (get_timer(start) > CFG_FLASH_WRITE_TOUT) { |
| 697 | return (1); |
| 698 | } |
| 699 | } |
| 700 | } |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 701 | |
| 702 | return (0); |
| 703 | } |
| 704 | |
| 705 | /*----------------------------------------------------------------------- |
| 706 | */ |