wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@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 | /* |
| 25 | * 07-10-2002 Frank Gottschling: added 29F032 flash (ELPPC). |
| 26 | * fixed monitor protection part |
| 27 | * |
| 28 | * 09-18-2001 Andreas Heppel: Reduced the code in here to the usage |
| 29 | * of AMD's 29F040 and 29F016 flashes, since the BAB7xx does use |
| 30 | * any other. |
| 31 | */ |
| 32 | |
| 33 | #include <common.h> |
| 34 | #include <asm/processor.h> |
| 35 | #include <asm/pci_io.h> |
| 36 | |
| 37 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
| 38 | |
| 39 | ulong flash_get_size (vu_long *addr, flash_info_t *info); |
| 40 | static int write_word (flash_info_t *info, ulong dest, ulong data); |
| 41 | |
| 42 | /*flash command address offsets*/ |
| 43 | |
| 44 | #define ADDR0 (0x555) |
| 45 | #define ADDR1 (0x2AA) |
| 46 | #define ADDR3 (0x001) |
| 47 | |
| 48 | #define FLASH_WORD_SIZE unsigned char |
| 49 | |
| 50 | /*----------------------------------------------------------------------------*/ |
| 51 | |
| 52 | unsigned long flash_init (void) |
| 53 | { |
| 54 | unsigned long size1, size2; |
| 55 | int i; |
| 56 | |
| 57 | /* Init: no FLASHes known */ |
| 58 | for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) |
| 59 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 60 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* initialise 1st flash */ |
| 64 | size1 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]); |
| 65 | |
| 66 | if (flash_info[0].flash_id == FLASH_UNKNOWN) |
| 67 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 68 | printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", |
| 69 | size1, size1<<20); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* initialise 2nd flash */ |
| 73 | size2 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]); |
| 74 | |
| 75 | if (flash_info[1].flash_id == FLASH_UNKNOWN) |
| 76 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 77 | printf ("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n", |
| 78 | size2, size2<<20); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* monitor protection ON by default */ |
| 82 | if (size1 == 512*1024) |
| 83 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 84 | (void)flash_protect(FLAG_PROTECT_SET, |
| 85 | FLASH_BASE0_PRELIM, |
| 86 | FLASH_BASE0_PRELIM+monitor_flash_len-1, |
| 87 | &flash_info[0]); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 88 | } |
| 89 | if (size2 == 512*1024) |
| 90 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 91 | (void)flash_protect(FLAG_PROTECT_SET, |
| 92 | FLASH_BASE1_PRELIM, |
| 93 | FLASH_BASE1_PRELIM+monitor_flash_len-1, |
| 94 | &flash_info[1]); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 95 | } |
| 96 | if (size2 == 4*1024*1024) |
| 97 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 98 | (void)flash_protect(FLAG_PROTECT_SET, |
| 99 | CFG_FLASH_BASE, |
| 100 | CFG_FLASH_BASE+monitor_flash_len-1, |
| 101 | &flash_info[1]); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return (size1 + size2); |
| 105 | } |
| 106 | |
| 107 | /*----------------------------------------------------------------------------*/ |
| 108 | |
| 109 | void flash_print_info (flash_info_t *info) |
| 110 | { |
| 111 | int i; |
| 112 | int k; |
| 113 | int size; |
| 114 | int erased; |
| 115 | volatile unsigned long *flash; |
| 116 | |
| 117 | if (info->flash_id == FLASH_UNKNOWN) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 118 | printf ("missing or unknown FLASH type\n"); |
| 119 | flash_init(); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | if (info->flash_id == FLASH_UNKNOWN) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 123 | printf ("missing or unknown FLASH type\n"); |
| 124 | return; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | switch (info->flash_id & FLASH_VENDMASK) { |
| 128 | case FLASH_MAN_AMD: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 129 | printf ("AMD "); |
| 130 | break; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 131 | default: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | printf ("Unknown Vendor "); |
| 133 | break; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 137 | case AMD_ID_F040B: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 138 | printf ("AM29F040B (4 Mbit)\n"); |
| 139 | break; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 140 | case AMD_ID_F016D: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 141 | printf ("AM29F016D (16 Mbit)\n"); |
| 142 | break; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 143 | case AMD_ID_F032B: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 144 | printf ("AM29F032B (32 Mbit)\n"); |
| 145 | break; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 146 | default: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 147 | printf ("Unknown Chip Type\n"); |
| 148 | break; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (info->size >= (1 << 20)) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 152 | printf (" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 153 | } else { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 154 | printf (" Size: %ld kB in %d Sectors\n", info->size >> 10, info->sector_count); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | printf (" Sector Start Addresses:"); |
| 158 | for (i=0; i<info->sector_count; ++i) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 159 | /* |
| 160 | * Check if whole sector is erased |
| 161 | */ |
| 162 | if (i != (info->sector_count-1)) |
| 163 | size = info->start[i+1] - info->start[i]; |
| 164 | else |
| 165 | size = info->start[0] + info->size - info->start[i]; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 166 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 167 | erased = 1; |
| 168 | flash = (volatile unsigned long *)info->start[i]; |
| 169 | size = size >> 2; /* divide by 4 for longword access */ |
| 170 | for (k=0; k<size; k++) { |
| 171 | if (*flash++ != 0xffffffff) { |
| 172 | erased = 0; |
| 173 | break; |
| 174 | } |
| 175 | } |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 176 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 177 | if ((i % 5) == 0) |
| 178 | printf ("\n "); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 179 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 180 | printf (" %08lX%s%s", |
| 181 | info->start[i], |
| 182 | erased ? " E" : " ", |
| 183 | info->protect[i] ? "RO " : " "); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 184 | } |
| 185 | printf ("\n"); |
| 186 | } |
| 187 | |
| 188 | /*----------------------------------------------------------------------------*/ |
| 189 | /* |
| 190 | * The following code cannot be run from FLASH! |
| 191 | */ |
| 192 | ulong flash_get_size (vu_long *addr, flash_info_t *info) |
| 193 | { |
| 194 | short i; |
| 195 | ulong vendor, devid; |
| 196 | ulong base = (ulong)addr; |
| 197 | volatile unsigned char *caddr = (unsigned char *)addr; |
| 198 | |
| 199 | #ifdef DEBUG |
| 200 | printf("flash_get_size for address 0x%lx: \n", (unsigned long)caddr); |
| 201 | #endif |
| 202 | |
| 203 | /* Write auto select command: read Manufacturer ID */ |
| 204 | caddr[0] = 0xF0; /* reset bank */ |
| 205 | udelay(10); |
| 206 | |
| 207 | eieio(); |
| 208 | caddr[0x555] = 0xAA; |
| 209 | udelay(10); |
| 210 | caddr[0x2AA] = 0x55; |
| 211 | udelay(10); |
| 212 | caddr[0x555] = 0x90; |
| 213 | |
| 214 | udelay(10); |
| 215 | |
| 216 | vendor = caddr[0]; |
| 217 | devid = caddr[1]; |
| 218 | |
| 219 | #ifdef DEBUG |
| 220 | printf("Manufacturer: 0x%lx\n", vendor); |
| 221 | #endif |
| 222 | |
| 223 | vendor &= 0xff; |
| 224 | devid &= 0xff; |
| 225 | |
| 226 | /* We accept only two AMD types */ |
| 227 | switch (vendor) { |
| 228 | case (FLASH_WORD_SIZE)AMD_MANUFACT: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 229 | info->flash_id = FLASH_MAN_AMD; |
| 230 | break; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 231 | default: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 232 | info->flash_id = FLASH_UNKNOWN; |
| 233 | info->sector_count = 0; |
| 234 | info->size = 0; |
| 235 | return (0); /* no or unknown flash */ |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | switch (devid) { |
| 239 | case (FLASH_WORD_SIZE)AMD_ID_F040B: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 240 | info->flash_id |= AMD_ID_F040B; |
| 241 | info->sector_count = 8; |
| 242 | info->size = 0x00080000; |
| 243 | break; /* => 0.5 MB */ |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 244 | |
| 245 | case (FLASH_WORD_SIZE)AMD_ID_F016D: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 246 | info->flash_id |= AMD_ID_F016D; |
| 247 | info->sector_count = 32; |
| 248 | info->size = 0x00200000; |
| 249 | break; /* => 2 MB */ |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 250 | |
| 251 | case (FLASH_WORD_SIZE)AMD_ID_F032B: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 252 | info->flash_id |= AMD_ID_F032B; |
| 253 | info->sector_count = 64; |
| 254 | info->size = 0x00400000; |
| 255 | break; /* => 4 MB */ |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 256 | |
| 257 | default: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 258 | info->flash_id = FLASH_UNKNOWN; |
| 259 | return (0); /* => no or unknown flash */ |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 260 | |
| 261 | } |
| 262 | |
| 263 | #ifdef DEBUG |
| 264 | printf("flash id 0x%lx; sector count 0x%x, size 0x%lx\n", info->flash_id, info->sector_count, info->size); |
| 265 | #endif |
| 266 | |
| 267 | /* check for protected sectors */ |
| 268 | for (i = 0; i < info->sector_count; i++) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 269 | /* sector base address */ |
| 270 | info->start[i] = base + i * (info->size / info->sector_count); |
| 271 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 272 | /* D0 = 1 if protected */ |
| 273 | caddr = (volatile unsigned char *)(info->start[i]); |
| 274 | info->protect[i] = caddr[2] & 1; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Prevent writes to uninitialized FLASH. |
| 279 | */ |
| 280 | if (info->flash_id != FLASH_UNKNOWN) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 281 | caddr = (volatile unsigned char *)info->start[0]; |
| 282 | caddr[0] = 0xF0; /* reset bank */ |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | return (info->size); |
| 286 | } |
| 287 | |
| 288 | /*----------------------------------------------------------------------------*/ |
| 289 | |
| 290 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 291 | { |
| 292 | volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]); |
| 293 | int flag, prot, sect, l_sect; |
| 294 | ulong start, now, last; |
| 295 | int rc = 0; |
| 296 | |
| 297 | if ((s_first < 0) || (s_first > s_last)) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 298 | if (info->flash_id == FLASH_UNKNOWN) { |
| 299 | printf ("- missing\n"); |
| 300 | } else { |
| 301 | printf ("- no sectors to erase\n"); |
| 302 | } |
| 303 | return 1; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if ((info->flash_id == FLASH_UNKNOWN) || |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 307 | (info->flash_id > FLASH_AMD_COMP)) { |
| 308 | printf ("Can't erase unknown flash type - aborted\n"); |
| 309 | return 1; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | prot = 0; |
| 313 | for (sect=s_first; sect<=s_last; ++sect) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 314 | if (info->protect[sect]) { |
| 315 | prot++; |
| 316 | } |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if (prot) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 320 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 321 | prot); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 322 | } else { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 323 | printf ("\n"); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | l_sect = -1; |
| 327 | |
| 328 | /* Disable interrupts which might cause a timeout here */ |
| 329 | flag = disable_interrupts(); |
| 330 | |
| 331 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 332 | addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 333 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080; |
| 334 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 335 | addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 336 | |
| 337 | /* Start erase on unprotected sectors */ |
| 338 | for (sect = s_first; sect<=s_last; sect++) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 339 | if (info->protect[sect] == 0) { /* not protected */ |
| 340 | addr = (FLASH_WORD_SIZE *)(info->start[sect]); |
| 341 | if (info->flash_id & FLASH_MAN_SST) { |
| 342 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 343 | addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 344 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080; |
| 345 | addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 346 | addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 347 | addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */ |
| 348 | udelay(30000); /* wait 30 ms */ |
| 349 | } |
| 350 | else |
| 351 | addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */ |
| 352 | l_sect = sect; |
| 353 | } |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* re-enable interrupts if necessary */ |
| 357 | if (flag) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 358 | enable_interrupts(); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 359 | |
| 360 | /* wait at least 80us - let's wait 1 ms */ |
| 361 | udelay (1000); |
| 362 | |
| 363 | /* |
| 364 | * We wait for the last triggered sector |
| 365 | */ |
| 366 | if (l_sect < 0) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 367 | goto DONE; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 368 | |
| 369 | start = get_timer (0); |
| 370 | last = start; |
| 371 | addr = (FLASH_WORD_SIZE *)(info->start[l_sect]); |
| 372 | while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 373 | if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) { |
| 374 | printf ("Timeout\n"); |
| 375 | return 1; |
| 376 | } |
| 377 | /* show that we're waiting */ |
| 378 | if ((now - last) > 1000) { /* every second */ |
| 379 | serial_putc ('.'); |
| 380 | last = now; |
| 381 | } |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | DONE: |
| 385 | /* reset to read mode */ |
| 386 | addr = (FLASH_WORD_SIZE *)info->start[0]; |
| 387 | addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */ |
| 388 | |
| 389 | printf (" done\n"); |
| 390 | return rc; |
| 391 | } |
| 392 | |
| 393 | /*----------------------------------------------------------------------------*/ |
| 394 | /* |
| 395 | * Copy memory to flash, returns: |
| 396 | * 0 - OK |
| 397 | * 1 - write timeout |
| 398 | * 2 - Flash not erased |
| 399 | */ |
| 400 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 401 | { |
| 402 | ulong cp, wp, data; |
| 403 | int i, l, rc; |
| 404 | |
| 405 | wp = (addr & ~3); /* get lower word aligned address */ |
| 406 | |
| 407 | /* |
| 408 | * handle unaligned start bytes |
| 409 | */ |
| 410 | if ((l = addr - wp) != 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 411 | data = 0; |
| 412 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
| 413 | data = (data << 8) | (*(uchar *)cp); |
| 414 | } |
| 415 | for (; i<4 && cnt>0; ++i) { |
| 416 | data = (data << 8) | *src++; |
| 417 | --cnt; |
| 418 | ++cp; |
| 419 | } |
| 420 | for (; cnt==0 && i<4; ++i, ++cp) { |
| 421 | data = (data << 8) | (*(uchar *)cp); |
| 422 | } |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 423 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 424 | if ((rc = write_word(info, wp, data)) != 0) { |
| 425 | return (rc); |
| 426 | } |
| 427 | wp += 4; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* |
| 431 | * handle word aligned part |
| 432 | */ |
| 433 | while (cnt >= 4) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 434 | data = 0; |
| 435 | for (i=0; i<4; ++i) { |
| 436 | data = (data << 8) | *src++; |
| 437 | } |
| 438 | if ((rc = write_word(info, wp, data)) != 0) { |
| 439 | return (rc); |
| 440 | } |
| 441 | wp += 4; |
| 442 | cnt -= 4; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (cnt == 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 446 | return (0); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* |
| 450 | * handle unaligned tail bytes |
| 451 | */ |
| 452 | data = 0; |
| 453 | for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 454 | data = (data << 8) | *src++; |
| 455 | --cnt; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 456 | } |
| 457 | for (; i<4; ++i, ++cp) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 458 | data = (data << 8) | (*(uchar *)cp); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | return (write_word(info, wp, data)); |
| 462 | } |
| 463 | |
| 464 | /*----------------------------------------------------------------------------*/ |
| 465 | /* Write a word to Flash, returns: |
| 466 | * 0 - OK |
| 467 | * 1 - write timeout |
| 468 | * 2 - Flash not erased |
| 469 | */ |
| 470 | static int write_word (flash_info_t *info, ulong dest, ulong data) |
| 471 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 472 | volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]); |
| 473 | volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest; |
| 474 | volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 475 | ulong start; |
| 476 | int flag; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 477 | int i; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 478 | |
| 479 | /* Check if Flash is (sufficiently) erased */ |
| 480 | if ((*((volatile FLASH_WORD_SIZE *)dest) & |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 481 | (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) { |
| 482 | return (2); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 483 | } |
| 484 | /* Disable interrupts which might cause a timeout here */ |
| 485 | flag = disable_interrupts(); |
| 486 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 487 | for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++) |
| 488 | { |
| 489 | addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA; |
| 490 | addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055; |
| 491 | addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 492 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 493 | dest2[i] = data2[i]; |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 494 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 495 | /* re-enable interrupts if necessary */ |
| 496 | if (flag) |
| 497 | enable_interrupts(); |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 498 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 499 | /* data polling for D7 */ |
| 500 | start = get_timer (0); |
| 501 | while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) != |
| 502 | (data2[i] & (FLASH_WORD_SIZE)0x00800080)) { |
| 503 | if (get_timer(start) > CFG_FLASH_WRITE_TOUT) { |
| 504 | return (1); |
| 505 | } |
| 506 | } |
| 507 | } |
wdenk | e69b4b8 | 2002-09-17 21:26:59 +0000 | [diff] [blame] | 508 | |
| 509 | return (0); |
| 510 | } |
| 511 | |
| 512 | /*----------------------------------------------------------------------------*/ |