wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 4 | * |
| 5 | * (C) Copyright 2001 |
| 6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 27 | /* #define DEBUG */ |
| 28 | |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 29 | #include <common.h> |
| 30 | #include <mpc8xx.h> |
| 31 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 32 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 33 | |
| 34 | #if defined(CFG_ENV_IS_IN_FLASH) |
| 35 | # ifndef CFG_ENV_ADDR |
| 36 | # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET) |
| 37 | # endif |
| 38 | # ifndef CFG_ENV_SIZE |
| 39 | # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE |
| 40 | # endif |
| 41 | # ifndef CFG_ENV_SECT_SIZE |
| 42 | # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE |
| 43 | # endif |
| 44 | #endif |
| 45 | |
| 46 | /*----------------------------------------------------------------------- |
| 47 | * Protection Flags: |
| 48 | */ |
| 49 | #define FLAG_PROTECT_SET 0x01 |
| 50 | #define FLAG_PROTECT_CLEAR 0x02 |
| 51 | |
| 52 | /* Board support for 1 or 2 flash devices */ |
| 53 | #undef FLASH_PORT_WIDTH32 |
| 54 | #define FLASH_PORT_WIDTH16 |
| 55 | |
| 56 | #ifdef FLASH_PORT_WIDTH16 |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 57 | #define FLASH_PORT_WIDTH ushort |
| 58 | #define FLASH_PORT_WIDTHV vu_short |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 59 | #else |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 60 | #define FLASH_PORT_WIDTH ulong |
| 61 | #define FLASH_PORT_WIDTHV vu_long |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 64 | #define FPW FLASH_PORT_WIDTH |
| 65 | #define FPWV FLASH_PORT_WIDTHV |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 66 | |
| 67 | /*----------------------------------------------------------------------- |
| 68 | * Functions |
| 69 | */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 70 | static ulong flash_get_size (FPW * addr, flash_info_t * info); |
| 71 | static int write_data (flash_info_t * info, ulong dest, FPW data); |
| 72 | static void flash_get_offsets (ulong base, flash_info_t * info); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 73 | |
| 74 | /*----------------------------------------------------------------------- |
| 75 | */ |
| 76 | |
| 77 | unsigned long flash_init (void) |
| 78 | { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 79 | volatile immap_t *immap = (immap_t *) CFG_IMMR; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 80 | volatile memctl8xx_t *memctl = &immap->im_memctl; |
| 81 | unsigned long size_b0; |
| 82 | int i; |
| 83 | |
| 84 | /* Init: no FLASHes known */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 85 | for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) { |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 86 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 87 | } |
| 88 | |
| 89 | /* Static FLASH Bank configuration here - FIXME XXX */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 90 | size_b0 = flash_get_size ((FPW *) FLASH_BASE0_PRELIM, &flash_info[0]); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 91 | |
| 92 | if (flash_info[0].flash_id == FLASH_UNKNOWN) { |
| 93 | printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 94 | size_b0, size_b0 << 20); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Remap FLASH according to real size */ |
| 98 | memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000); |
| 99 | memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V; |
| 100 | |
| 101 | /* Re-do sizing to get full correct info */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 102 | size_b0 = flash_get_size ((FPW *) CFG_FLASH_BASE, &flash_info[0]); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 103 | |
| 104 | flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]); |
| 105 | |
| 106 | #if CFG_MONITOR_BASE >= CFG_FLASH_BASE |
| 107 | /* monitor protection ON by default */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 108 | (void) flash_protect (FLAG_PROTECT_SET, |
| 109 | CFG_FLASH_BASE, |
| 110 | CFG_FLASH_BASE + CFG_MONITOR_LEN - 1, |
| 111 | &flash_info[0]); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 112 | #endif |
| 113 | |
| 114 | #ifdef CFG_ENV_IS_IN_FLASH |
| 115 | /* ENV protection ON by default */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 116 | flash_protect (FLAG_PROTECT_SET, |
| 117 | CFG_ENV_ADDR, |
| 118 | CFG_ENV_ADDR + CFG_ENV_SIZE - 1, |
| 119 | &flash_info[0]); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | flash_info[0].size = size_b0; |
| 123 | |
| 124 | return (size_b0); |
| 125 | } |
| 126 | |
| 127 | /*----------------------------------------------------------------------- |
| 128 | */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 129 | static void flash_get_offsets (ulong base, flash_info_t * info) |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 130 | { |
| 131 | int i; |
| 132 | |
| 133 | if (info->flash_id == FLASH_UNKNOWN) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) { |
| 138 | for (i = 0; i < info->sector_count; i++) { |
| 139 | info->start[i] = base + (i * 0x00020000); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /*----------------------------------------------------------------------- |
| 145 | */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 146 | void flash_print_info (flash_info_t * info) |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 147 | { |
| 148 | int i; |
| 149 | |
| 150 | if (info->flash_id == FLASH_UNKNOWN) { |
| 151 | printf ("missing or unknown FLASH type\n"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | switch (info->flash_id & FLASH_VENDMASK) { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 156 | case FLASH_MAN_INTEL: |
| 157 | printf ("INTEL "); |
| 158 | break; |
| 159 | default: |
| 160 | printf ("Unknown Vendor "); |
| 161 | break; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | switch (info->flash_id & FLASH_TYPEMASK) { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 165 | case FLASH_28F320J3A: |
| 166 | printf ("28F320J3A\n"); |
| 167 | break; |
| 168 | case FLASH_28F640J3A: |
| 169 | printf ("28F640J3A\n"); |
| 170 | break; |
| 171 | case FLASH_28F128J3A: |
| 172 | printf ("28F128J3A\n"); |
| 173 | break; |
| 174 | default: |
| 175 | printf ("Unknown Chip Type\n"); |
| 176 | break; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | printf (" Size: %ld MB in %d Sectors\n", |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 180 | info->size >> 20, info->sector_count); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 181 | |
| 182 | printf (" Sector Start Addresses:"); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 183 | for (i = 0; i < info->sector_count; ++i) { |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 184 | if ((i % 5) == 0) |
| 185 | printf ("\n "); |
| 186 | printf (" %08lX%s", |
| 187 | info->start[i], |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 188 | info->protect[i] ? " (RO)" : " "); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 189 | } |
| 190 | printf ("\n"); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | /*----------------------------------------------------------------------- |
| 195 | */ |
| 196 | |
| 197 | |
| 198 | /*----------------------------------------------------------------------- |
| 199 | */ |
| 200 | |
| 201 | /* |
| 202 | * The following code cannot be run from FLASH! |
| 203 | */ |
| 204 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 205 | static ulong flash_get_size (FPW * addr, flash_info_t * info) |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 206 | { |
| 207 | FPW value; |
| 208 | |
| 209 | /* Write auto select command: read Manufacturer ID */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 210 | addr[0x5555] = (FPW) 0x00AA00AA; |
| 211 | addr[0x2AAA] = (FPW) 0x00550055; |
| 212 | addr[0x5555] = (FPW) 0x00900090; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 213 | |
| 214 | value = addr[0]; |
| 215 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 216 | debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value); |
| 217 | |
| 218 | switch (value) { |
| 219 | case (FPW) INTEL_MANUFACT: |
| 220 | info->flash_id = FLASH_MAN_INTEL; |
| 221 | break; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 222 | default: |
| 223 | info->flash_id = FLASH_UNKNOWN; |
| 224 | info->sector_count = 0; |
| 225 | info->size = 0; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 226 | addr[0] = (FPW) 0x00FF00FF; /* restore read mode */ |
| 227 | return (0); /* no or unknown flash */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 228 | } |
| 229 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 230 | value = addr[1]; /* device ID */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 231 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 232 | debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 233 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 234 | switch (value) { |
| 235 | case (FPW) INTEL_ID_28F320J3A: |
| 236 | info->flash_id += FLASH_28F320J3A; |
| 237 | info->sector_count = 32; |
| 238 | info->size = 0x00400000; |
| 239 | break; /* => 4 MB */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 240 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 241 | case (FPW) INTEL_ID_28F640J3A: |
| 242 | info->flash_id += FLASH_28F640J3A; |
| 243 | info->sector_count = 64; |
| 244 | info->size = 0x00800000; |
| 245 | break; /* => 8 MB */ |
| 246 | |
| 247 | case (FPW) INTEL_ID_28F128J3A: |
| 248 | info->flash_id += FLASH_28F128J3A; |
| 249 | info->sector_count = 128; |
| 250 | info->size = 0x01000000; |
| 251 | break; /* => 16 MB */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 252 | |
| 253 | default: |
| 254 | info->flash_id = FLASH_UNKNOWN; |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | if (info->sector_count > CFG_MAX_FLASH_SECT) { |
| 259 | printf ("** ERROR: sector count %d > max (%d) **\n", |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 260 | info->sector_count, CFG_MAX_FLASH_SECT); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 261 | info->sector_count = CFG_MAX_FLASH_SECT; |
| 262 | } |
| 263 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 264 | addr[0] = (FPW) 0x00FF00FF; /* restore read mode */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 265 | |
| 266 | return (info->size); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | /*----------------------------------------------------------------------- |
| 271 | */ |
| 272 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 273 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 274 | { |
| 275 | int flag, prot, sect; |
| 276 | ulong type, start, now, last; |
| 277 | int rcode = 0; |
| 278 | |
| 279 | if ((s_first < 0) || (s_first > s_last)) { |
| 280 | if (info->flash_id == FLASH_UNKNOWN) { |
| 281 | printf ("- missing\n"); |
| 282 | } else { |
| 283 | printf ("- no sectors to erase\n"); |
| 284 | } |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | type = (info->flash_id & FLASH_VENDMASK); |
| 289 | if ((type != FLASH_MAN_INTEL)) { |
| 290 | printf ("Can't erase unknown flash type %08lx - aborted\n", |
| 291 | info->flash_id); |
| 292 | return 1; |
| 293 | } |
| 294 | |
| 295 | prot = 0; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 296 | for (sect = s_first; sect <= s_last; ++sect) { |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 297 | if (info->protect[sect]) { |
| 298 | prot++; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if (prot) { |
| 303 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 304 | prot); |
| 305 | } else { |
| 306 | printf ("\n"); |
| 307 | } |
| 308 | |
| 309 | start = get_timer (0); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 310 | last = start; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 311 | /* Start erase on unprotected sectors */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 312 | for (sect = s_first; sect <= s_last; sect++) { |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 313 | if (info->protect[sect] == 0) { /* not protected */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 314 | FPWV *addr = (FPWV *) (info->start[sect]); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 315 | FPW status; |
| 316 | |
| 317 | /* Disable interrupts which might cause a timeout here */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 318 | flag = disable_interrupts (); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 319 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 320 | *addr = (FPW) 0x00500050; /* clear status register */ |
| 321 | *addr = (FPW) 0x00200020; /* erase setup */ |
| 322 | *addr = (FPW) 0x00D000D0; /* erase confirm */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 323 | |
| 324 | /* re-enable interrupts if necessary */ |
| 325 | if (flag) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 326 | enable_interrupts (); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 327 | |
| 328 | /* wait at least 80us - let's wait 1 ms */ |
| 329 | udelay (1000); |
| 330 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 331 | while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) { |
| 332 | if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) { |
| 333 | printf ("Timeout\n"); |
| 334 | *addr = (FPW) 0x00B000B0; /* suspend erase */ |
| 335 | *addr = (FPW) 0x00FF00FF; /* reset to read mode */ |
| 336 | rcode = 1; |
| 337 | break; |
| 338 | } |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 339 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 340 | /* show that we're waiting */ |
| 341 | if ((now - last) > 1000) { /* every second */ |
| 342 | putc ('.'); |
| 343 | last = now; |
| 344 | } |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 345 | } |
| 346 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 347 | *addr = (FPW) 0x00FF00FF; /* reset to read mode */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | printf (" done\n"); |
| 351 | return rcode; |
| 352 | } |
| 353 | |
| 354 | /*----------------------------------------------------------------------- |
| 355 | * Copy memory to flash, returns: |
| 356 | * 0 - OK |
| 357 | * 1 - write timeout |
| 358 | * 2 - Flash not erased |
| 359 | * 4 - Flash not identified |
| 360 | */ |
| 361 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 362 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 363 | { |
| 364 | ulong cp, wp; |
| 365 | FPW data; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 366 | |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 367 | int i, l, rc, port_width; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 368 | |
| 369 | if (info->flash_id == FLASH_UNKNOWN) { |
| 370 | return 4; |
| 371 | } |
| 372 | /* get lower word aligned address */ |
| 373 | #ifdef FLASH_PORT_WIDTH16 |
| 374 | wp = (addr & ~1); |
| 375 | port_width = 2; |
| 376 | #else |
| 377 | wp = (addr & ~3); |
| 378 | port_width = 4; |
| 379 | #endif |
| 380 | |
| 381 | /* |
| 382 | * handle unaligned start bytes |
| 383 | */ |
| 384 | if ((l = addr - wp) != 0) { |
| 385 | data = 0; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 386 | for (i = 0, cp = wp; i < l; ++i, ++cp) { |
| 387 | data = (data << 8) | (*(uchar *) cp); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 388 | } |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 389 | for (; i < port_width && cnt > 0; ++i) { |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 390 | data = (data << 8) | *src++; |
| 391 | --cnt; |
| 392 | ++cp; |
| 393 | } |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 394 | for (; cnt == 0 && i < port_width; ++i, ++cp) { |
| 395 | data = (data << 8) | (*(uchar *) cp); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 396 | } |
| 397 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 398 | if ((rc = write_data (info, wp, data)) != 0) { |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 399 | return (rc); |
| 400 | } |
| 401 | wp += port_width; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * handle word aligned part |
| 406 | */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 407 | while (cnt >= port_width) { |
| 408 | data = 0; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 409 | for (i = 0; i < port_width; ++i) { |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 410 | data = (data << 8) | *src++; |
| 411 | } |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 412 | if ((rc = write_data (info, wp, data)) != 0) { |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 413 | return (rc); |
| 414 | } |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 415 | wp += port_width; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 416 | cnt -= port_width; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (cnt == 0) { |
| 420 | return (0); |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * handle unaligned tail bytes |
| 425 | */ |
| 426 | data = 0; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 427 | for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) { |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 428 | data = (data << 8) | *src++; |
| 429 | --cnt; |
| 430 | } |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 431 | for (; i < port_width; ++i, ++cp) { |
| 432 | data = (data << 8) | (*(uchar *) cp); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 433 | } |
| 434 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 435 | return (write_data (info, wp, data)); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /*----------------------------------------------------------------------- |
| 439 | * Write a word or halfword to Flash, returns: |
| 440 | * 0 - OK |
| 441 | * 1 - write timeout |
| 442 | * 2 - Flash not erased |
| 443 | */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 444 | static int write_data (flash_info_t * info, ulong dest, FPW data) |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 445 | { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 446 | FPWV *addr = (FPWV *) dest; |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 447 | ulong status; |
| 448 | ulong start; |
| 449 | int flag; |
| 450 | |
| 451 | /* Check if Flash is (sufficiently) erased */ |
| 452 | if ((*addr & data) != data) { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 453 | printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 454 | return (2); |
| 455 | } |
| 456 | /* Disable interrupts which might cause a timeout here */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 457 | flag = disable_interrupts (); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 458 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 459 | *addr = (FPW) 0x00400040; /* write setup */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 460 | *addr = data; |
| 461 | |
| 462 | /* re-enable interrupts if necessary */ |
| 463 | if (flag) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 464 | enable_interrupts (); |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 465 | |
| 466 | start = get_timer (0); |
| 467 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 468 | while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) { |
| 469 | if (get_timer (start) > CFG_FLASH_WRITE_TOUT) { |
| 470 | *addr = (FPW) 0x00FF00FF; /* restore read mode */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 471 | return (1); |
| 472 | } |
| 473 | } |
| 474 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 475 | *addr = (FPW) 0x00FF00FF; /* restore read mode */ |
wdenk | 8993072 | 2002-09-18 07:25:03 +0000 | [diff] [blame] | 476 | |
| 477 | return (0); |
| 478 | } |