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