wdenk | 682011f | 2003-06-03 23:54:09 +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 | #include <common.h> |
| 25 | #include <mpc8xx.h> |
| 26 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 27 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 28 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 29 | /* |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 30 | * Functions |
| 31 | */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 32 | static ulong flash_get_size(vu_long *addr, flash_info_t *info); |
| 33 | static int write_word(flash_info_t *info, ulong dest, ulong data); |
| 34 | static void flash_get_offsets(ulong base, flash_info_t *info); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 35 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 36 | unsigned long flash_init(void) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 37 | { |
Wolfgang Denk | e0ead84 | 2011-11-04 15:55:47 +0000 | [diff] [blame] | 38 | unsigned long size_b0; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 39 | int i; |
| 40 | |
| 41 | /* Init: no FLASHes known */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 42 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) |
| 43 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 44 | |
| 45 | /* Detect size */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 46 | size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, |
| 47 | &flash_info[0]); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 48 | |
| 49 | /* Setup offsets */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 50 | flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 51 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 52 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 53 | /* Monitor protection ON by default */ |
| 54 | flash_protect(FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 55 | CONFIG_SYS_MONITOR_BASE, |
| 56 | CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1, |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 57 | &flash_info[0]); |
| 58 | #endif |
| 59 | |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 60 | flash_info[0].size = size_b0; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 61 | |
Wolfgang Denk | e0ead84 | 2011-11-04 15:55:47 +0000 | [diff] [blame] | 62 | return size_b0; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /*----------------------------------------------------------------------- |
| 66 | * Fix this to support variable sector sizes |
| 67 | */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 68 | static void flash_get_offsets(ulong base, flash_info_t *info) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 69 | { |
| 70 | int i; |
| 71 | |
| 72 | /* set up sector start address table */ |
| 73 | if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) { |
| 74 | /* set sector offsets for bottom boot block type */ |
| 75 | for (i = 0; i < info->sector_count; i++) |
| 76 | info->start[i] = base + (i * 0x00010000); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /*----------------------------------------------------------------------- |
| 81 | */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 82 | void flash_print_info(flash_info_t *info) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 83 | { |
| 84 | int i; |
| 85 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 86 | if (info->flash_id == FLASH_UNKNOWN) { |
| 87 | puts("missing or unknown FLASH type\n"); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 91 | switch (info->flash_id & FLASH_VENDMASK) { |
| 92 | case FLASH_MAN_AMD: |
| 93 | printf("AMD "); |
| 94 | break; |
| 95 | case FLASH_MAN_FUJ: |
| 96 | printf("FUJITSU "); |
| 97 | break; |
| 98 | case FLASH_MAN_BM: |
| 99 | printf("BRIGHT MICRO "); |
| 100 | break; |
| 101 | default: |
| 102 | printf("Unknown Vendor "); |
| 103 | break; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 106 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 107 | case FLASH_AM040: |
| 108 | printf("29F040 or 29LV040 (4 Mbit, uniform sectors)\n"); |
| 109 | break; |
| 110 | case FLASH_AM400B: |
| 111 | printf("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
| 112 | break; |
| 113 | case FLASH_AM400T: |
| 114 | printf("AM29LV400T (4 Mbit, top boot sector)\n"); |
| 115 | break; |
| 116 | case FLASH_AM800B: |
| 117 | printf("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
| 118 | break; |
| 119 | case FLASH_AM800T: |
| 120 | printf("AM29LV800T (8 Mbit, top boot sector)\n"); |
| 121 | break; |
| 122 | case FLASH_AM160B: |
| 123 | printf("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
| 124 | break; |
| 125 | case FLASH_AM160T: |
| 126 | printf("AM29LV160T (16 Mbit, top boot sector)\n"); |
| 127 | break; |
| 128 | case FLASH_AM320B: |
| 129 | printf("AM29LV320B (32 Mbit, bottom boot sect)\n"); |
| 130 | break; |
| 131 | case FLASH_AM320T: |
| 132 | printf("AM29LV320T (32 Mbit, top boot sector)\n"); |
| 133 | break; |
| 134 | default: |
| 135 | printf("Unknown Chip Type\n"); |
| 136 | break; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if (info->size >> 20) { |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 140 | printf(" Size: %ld MB in %d Sectors\n", |
| 141 | info->size >> 20, |
| 142 | info->sector_count); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 143 | } else { |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 144 | printf(" Size: %ld KB in %d Sectors\n", |
| 145 | info->size >> 10, |
| 146 | info->sector_count); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 149 | puts(" Sector Start Addresses:"); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 150 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 151 | for (i = 0; i < info->sector_count; ++i) { |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 152 | if ((i % 5) == 0) |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 153 | puts("\n "); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 154 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 155 | printf(" %08lX%s", |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 156 | info->start[i], |
| 157 | info->protect[i] ? " (RO)" : " "); |
| 158 | } |
| 159 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 160 | putc('\n'); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 161 | return; |
| 162 | } |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * The following code cannot be run from FLASH! |
| 166 | */ |
| 167 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 168 | static ulong flash_get_size(vu_long *addr, flash_info_t *info) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 169 | { |
| 170 | short i; |
| 171 | volatile unsigned char *caddr; |
| 172 | char value; |
| 173 | |
| 174 | caddr = (volatile unsigned char *)addr ; |
| 175 | |
| 176 | /* Write auto select command: read Manufacturer ID */ |
| 177 | |
Wolfgang Denk | e0ead84 | 2011-11-04 15:55:47 +0000 | [diff] [blame] | 178 | debug("Base address is: %8p\n", caddr); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 179 | |
| 180 | caddr[0x0555] = 0xAA; |
| 181 | caddr[0x02AA] = 0x55; |
| 182 | caddr[0x0555] = 0x90; |
| 183 | |
| 184 | value = caddr[0]; |
| 185 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 186 | debug("Manufact ID: %02x\n", value); |
| 187 | |
| 188 | switch (value) { |
| 189 | case 0x01: /*AMD_MANUFACT*/ |
| 190 | info->flash_id = FLASH_MAN_AMD; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 191 | break; |
| 192 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 193 | case 0x04: /*FUJ_MANUFACT*/ |
| 194 | info->flash_id = FLASH_MAN_FUJ; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 195 | break; |
| 196 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 197 | default: |
| 198 | info->flash_id = FLASH_UNKNOWN; |
| 199 | info->sector_count = 0; |
| 200 | info->size = 0; |
| 201 | break; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | value = caddr[1]; /* device ID */ |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 205 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 206 | debug("Device ID: %02x\n", value); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 207 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 208 | switch (value) { |
| 209 | case AMD_ID_LV040B: |
| 210 | info->flash_id += FLASH_AM040; |
| 211 | info->sector_count = 8; |
| 212 | info->size = 0x00080000; |
| 213 | break; /* => 512Kb */ |
| 214 | |
| 215 | default: |
| 216 | info->flash_id = FLASH_UNKNOWN; |
| 217 | return 0; /* => no or unknown flash */ |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 220 | flash_get_offsets((ulong)addr, &flash_info[0]); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 221 | |
| 222 | /* check for protected sectors */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 223 | for (i = 0; i < info->sector_count; i++) { |
| 224 | /* |
| 225 | * read sector protection at sector address, |
| 226 | * (A7 .. A0) = 0x02 |
| 227 | * D0 = 1 if protected |
| 228 | */ |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 229 | caddr = (volatile unsigned char *)(info->start[i]); |
| 230 | info->protect[i] = caddr[2] & 1; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Prevent writes to uninitialized FLASH. |
| 235 | */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 236 | if (info->flash_id != FLASH_UNKNOWN) { |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 237 | caddr = (volatile unsigned char *)info->start[0]; |
| 238 | *caddr = 0xF0; /* reset bank */ |
| 239 | } |
| 240 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 241 | return info->size; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 245 | int flash_erase(flash_info_t *info, int s_first, int s_last) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 246 | { |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 247 | volatile unsigned char *addr = |
| 248 | (volatile unsigned char *)(info->start[0]); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 249 | int flag, prot, sect, l_sect; |
| 250 | ulong start, now, last; |
| 251 | |
| 252 | if ((s_first < 0) || (s_first > s_last)) { |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 253 | if (info->flash_id == FLASH_UNKNOWN) |
| 254 | printf("- missing\n"); |
| 255 | else |
| 256 | printf("- no sectors to erase\n"); |
| 257 | |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 258 | return 1; |
| 259 | } |
| 260 | |
| 261 | if ((info->flash_id == FLASH_UNKNOWN) || |
| 262 | (info->flash_id > FLASH_AMD_COMP)) { |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 263 | printf("Can't erase unknown flash type - aborted\n"); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | prot = 0; |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 268 | for (sect = s_first; sect <= s_last; ++sect) { |
| 269 | if (info->protect[sect]) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 270 | prot++; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if (prot) { |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 274 | printf("- Warning: %d protected sectors will not be erased!\n", |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 275 | prot); |
| 276 | } else { |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 277 | printf("\n"); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | l_sect = -1; |
| 281 | |
| 282 | /* Disable interrupts which might cause a timeout here */ |
| 283 | flag = disable_interrupts(); |
| 284 | |
| 285 | addr[0x0555] = 0xAA; |
| 286 | addr[0x02AA] = 0x55; |
| 287 | addr[0x0555] = 0x80; |
| 288 | addr[0x0555] = 0xAA; |
| 289 | addr[0x02AA] = 0x55; |
| 290 | |
| 291 | /* Start erase on unprotected sectors */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 292 | for (sect = s_first; sect <= s_last; sect++) { |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 293 | if (info->protect[sect] == 0) { /* not protected */ |
| 294 | addr = (volatile unsigned char *)(info->start[sect]); |
| 295 | addr[0] = 0x30; |
| 296 | l_sect = sect; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /* re-enable interrupts if necessary */ |
| 301 | if (flag) |
| 302 | enable_interrupts(); |
| 303 | |
| 304 | /* wait at least 80us - let's wait 1 ms */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 305 | udelay(1000); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | * We wait for the last triggered sector |
| 309 | */ |
| 310 | if (l_sect < 0) |
| 311 | goto DONE; |
| 312 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 313 | start = get_timer(0); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 314 | last = start; |
| 315 | addr = (volatile unsigned char *)(info->start[l_sect]); |
| 316 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 317 | while ((addr[0] & 0xFF) != 0xFF) { |
| 318 | now = get_timer(start); |
| 319 | if (now > CONFIG_SYS_FLASH_ERASE_TOUT) { |
| 320 | printf("Timeout\n"); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 321 | return 1; |
| 322 | } |
| 323 | /* show that we're waiting */ |
| 324 | if ((now - last) > 1000) { /* every second */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 325 | putc('.'); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 326 | last = now; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | DONE: |
| 331 | /* reset to read mode */ |
| 332 | addr = (volatile unsigned char *)info->start[0]; |
| 333 | |
| 334 | addr[0] = 0xF0; /* reset bank */ |
| 335 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 336 | printf(" done\n"); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 340 | /* |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 341 | * Copy memory to flash, returns: |
| 342 | * 0 - OK |
| 343 | * 1 - write timeout |
| 344 | * 2 - Flash not erased |
| 345 | */ |
| 346 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 347 | int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 348 | { |
| 349 | ulong cp, wp, data; |
| 350 | int i, l, rc; |
| 351 | |
| 352 | wp = (addr & ~3); /* get lower word aligned address */ |
| 353 | |
| 354 | /* |
| 355 | * handle unaligned start bytes |
| 356 | */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 357 | l = addr - wp; |
| 358 | |
| 359 | if (l != 0) { |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 360 | data = 0; |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 361 | for (i = 0, cp = wp; i < l; ++i, ++cp) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 362 | data = (data << 8) | (*(uchar *)cp); |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 363 | |
| 364 | for (; i < 4 && cnt > 0; ++i) { |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 365 | data = (data << 8) | *src++; |
| 366 | --cnt; |
| 367 | ++cp; |
| 368 | } |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 369 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 370 | for (; cnt == 0 && i < 4; ++i, ++cp) |
| 371 | data = (data << 8) | (*(uchar *)cp); |
| 372 | |
| 373 | rc = write_word(info, wp, data); |
| 374 | |
| 375 | if (rc != 0) |
| 376 | return rc; |
| 377 | |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 378 | wp += 4; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * handle word aligned part |
| 383 | */ |
| 384 | while (cnt >= 4) { |
| 385 | data = 0; |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 386 | for (i = 0; i < 4; ++i) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 387 | data = (data << 8) | *src++; |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 388 | |
| 389 | rc = write_word(info, wp, data); |
| 390 | |
| 391 | if (rc != 0) |
| 392 | return rc; |
| 393 | |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 394 | wp += 4; |
| 395 | cnt -= 4; |
| 396 | } |
| 397 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 398 | if (cnt == 0) |
| 399 | return 0; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * handle unaligned tail bytes |
| 403 | */ |
| 404 | data = 0; |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 405 | for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) { |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 406 | data = (data << 8) | *src++; |
| 407 | --cnt; |
| 408 | } |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 409 | for (; i < 4; ++i, ++cp) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 410 | data = (data << 8) | (*(uchar *)cp); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 411 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 412 | return write_word(info, wp, data); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 415 | /* |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 416 | * Write a word to Flash, returns: |
| 417 | * 0 - OK |
| 418 | * 1 - write timeout |
| 419 | * 2 - Flash not erased |
| 420 | */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 421 | static int write_word(flash_info_t *info, ulong dest, ulong data) |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 422 | { |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 423 | volatile unsigned char *cdest, *cdata; |
| 424 | volatile unsigned char *addr = |
| 425 | (volatile unsigned char *)(info->start[0]); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 426 | ulong start; |
| 427 | int flag, count = 4 ; |
| 428 | |
| 429 | cdest = (volatile unsigned char *)dest ; |
| 430 | cdata = (volatile unsigned char *)&data ; |
| 431 | |
| 432 | /* Check if Flash is (sufficiently) erased */ |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 433 | if ((*((vu_long *)dest)&data) != data) |
| 434 | return 2; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 435 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 436 | while (count--) { |
| 437 | /* Disable interrupts which might cause a timeout here */ |
| 438 | flag = disable_interrupts(); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 439 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 440 | addr[0x0555] = 0xAA; |
| 441 | addr[0x02AA] = 0x55; |
| 442 | addr[0x0555] = 0xA0; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 443 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 444 | *cdest = *cdata; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 445 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 446 | /* re-enable interrupts if necessary */ |
| 447 | if (flag) |
| 448 | enable_interrupts(); |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 449 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 450 | /* data polling for D7 */ |
| 451 | start = get_timer(0); |
| 452 | while ((*cdest ^ *cdata) & 0x80) { |
| 453 | if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) |
| 454 | return 1; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 455 | } |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 456 | |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 457 | cdata++ ; |
| 458 | cdest++ ; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 459 | } |
Wolfgang Denk | 6915840 | 2011-11-04 15:55:46 +0000 | [diff] [blame] | 460 | return 0; |
wdenk | 682011f | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 461 | } |