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