wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 1 | /* |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 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. |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 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 | |
| 27 | #include <common.h> |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 28 | #include <linux/byteorder/swab.h> |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 29 | |
| 30 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 31 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 32 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 33 | /* Board support for 1 or 2 flash devices */ |
| 34 | #undef FLASH_PORT_WIDTH32 |
| 35 | #define FLASH_PORT_WIDTH16 |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 36 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 37 | #ifdef FLASH_PORT_WIDTH16 |
| 38 | #define FLASH_PORT_WIDTH ushort |
| 39 | #define FLASH_PORT_WIDTHV vu_short |
| 40 | #define SWAP(x) __swab16(x) |
| 41 | #else |
| 42 | #define FLASH_PORT_WIDTH ulong |
| 43 | #define FLASH_PORT_WIDTHV vu_long |
| 44 | #define SWAP(x) __swab32(x) |
| 45 | #endif |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 46 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 47 | #define FPW FLASH_PORT_WIDTH |
| 48 | #define FPWV FLASH_PORT_WIDTHV |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 49 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 50 | #define mb() __asm__ __volatile__ ("" : : : "memory") |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 51 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 52 | /*----------------------------------------------------------------------- |
| 53 | * Functions |
| 54 | */ |
| 55 | static ulong flash_get_size (FPW *addr, flash_info_t *info); |
| 56 | static int write_data (flash_info_t *info, ulong dest, FPW data); |
| 57 | static void flash_get_offsets (ulong base, flash_info_t *info); |
| 58 | void inline spin_wheel(void); |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 59 | |
| 60 | /*----------------------------------------------------------------------- |
| 61 | */ |
| 62 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 63 | unsigned long flash_init (void) |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 64 | { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 65 | int i; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 66 | ulong size = 0; |
| 67 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 68 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 69 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 70 | switch (i) |
| 71 | { |
| 72 | case 0: |
| 73 | flash_get_size((FPW *)PHYS_FLASH_1, &flash_info[i]); |
| 74 | flash_get_offsets(PHYS_FLASH_1, &flash_info[i]); |
| 75 | break; |
| 76 | default: |
wdenk | 5958f4a | 2003-09-18 09:21:33 +0000 | [diff] [blame] | 77 | panic("configured too many flash banks!\n"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 78 | break; |
| 79 | } |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 80 | size += flash_info[i].size; |
| 81 | } |
| 82 | |
| 83 | /* Protect monitor and environment sectors |
| 84 | */ |
| 85 | flash_protect(FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | CONFIG_SYS_FLASH_BASE, |
| 87 | CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1, |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 88 | &flash_info[0]); |
| 89 | |
| 90 | flash_protect(FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 91 | CONFIG_ENV_ADDR, |
| 92 | CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 93 | &flash_info[0]); |
| 94 | |
| 95 | return size; |
| 96 | } |
| 97 | |
| 98 | /*----------------------------------------------------------------------- |
| 99 | */ |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 100 | static void flash_get_offsets (ulong base, flash_info_t *info) |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 101 | { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 102 | int i; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 103 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 104 | if (info->flash_id == FLASH_UNKNOWN) { |
| 105 | return; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 106 | } |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 107 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 108 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) { |
| 109 | for (i = 0; i < info->sector_count; i++) { |
| 110 | info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE); |
| 111 | info->protect[i] = 0; |
| 112 | } |
| 113 | } |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /*----------------------------------------------------------------------- |
| 117 | */ |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 118 | void flash_print_info (flash_info_t *info) |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 119 | { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 120 | int i; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 121 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 122 | if (info->flash_id == FLASH_UNKNOWN) { |
| 123 | printf ("missing or unknown FLASH type\n"); |
| 124 | return; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 125 | } |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 126 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 127 | switch (info->flash_id & FLASH_VENDMASK) { |
| 128 | case FLASH_MAN_INTEL: printf ("INTEL "); break; |
| 129 | default: printf ("Unknown Vendor "); break; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 130 | } |
| 131 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 132 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 133 | case FLASH_28F128J3A: |
| 134 | printf ("28F128J3A\n"); break; |
| 135 | default: printf ("Unknown Chip Type\n"); break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 136 | } |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 137 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 138 | printf (" Size: %ld MB in %d Sectors\n", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 139 | info->size >> 20, info->sector_count); |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 140 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 141 | printf (" Sector Start Addresses:"); |
| 142 | for (i=0; i<info->sector_count; ++i) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 143 | if ((i % 5) == 0) |
| 144 | printf ("\n "); |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 145 | printf (" %08lX%s", |
| 146 | info->start[i], |
| 147 | info->protect[i] ? " (RO)" : " " |
| 148 | ); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 149 | } |
| 150 | printf ("\n"); |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 151 | return; |
| 152 | } |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 153 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 154 | /* |
| 155 | * The following code cannot be run from FLASH! |
| 156 | */ |
| 157 | static ulong flash_get_size (FPW *addr, flash_info_t *info) |
| 158 | { |
| 159 | volatile FPW value; |
| 160 | /* Write auto select command: read Manufacturer ID */ |
| 161 | addr[0x5555] = (FPW)0x00AA00AA; |
| 162 | addr[0x2AAA] = (FPW)0x00550055; |
| 163 | addr[0x5555] = (FPW)0x00900090; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 164 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 165 | mb(); |
| 166 | value = addr[0]; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 167 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 168 | switch (value) { |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 169 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 170 | case (FPW)INTEL_MANUFACT: |
| 171 | info->flash_id = FLASH_MAN_INTEL; |
| 172 | break; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 173 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 174 | default: |
| 175 | info->flash_id = FLASH_UNKNOWN; |
| 176 | info->sector_count = 0; |
| 177 | info->size = 0; |
| 178 | addr[0] = (FPW)0x00FF00FF; /* restore read mode */ |
| 179 | return (0); /* no or unknown flash */ |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 180 | } |
| 181 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 182 | mb(); |
| 183 | value = addr[1]; /* device ID */ |
| 184 | switch (value) { |
| 185 | |
| 186 | case (FPW)INTEL_ID_28F128J3A: |
| 187 | info->flash_id += FLASH_28F128J3A; |
| 188 | info->sector_count = 128; |
| 189 | info->size = 0x02000000; |
| 190 | break; /* => 16 MB */ |
| 191 | |
| 192 | default: |
| 193 | info->flash_id = FLASH_UNKNOWN; |
| 194 | break; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 197 | if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 198 | printf ("** ERROR: sector count %d > max (%d) **\n", |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 199 | info->sector_count, CONFIG_SYS_MAX_FLASH_SECT); |
| 200 | info->sector_count = CONFIG_SYS_MAX_FLASH_SECT; |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | addr[0] = (FPW)0x00FF00FF; /* restore read mode */ |
| 204 | |
| 205 | return (info->size); |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 206 | } |
| 207 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 208 | |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 209 | /*----------------------------------------------------------------------- |
| 210 | */ |
| 211 | |
| 212 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 213 | { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 214 | int flag, prot, sect; |
Graeme Russ | f8b82ee | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 215 | ulong type, start; |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 216 | int rcode = 0; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 217 | |
| 218 | if ((s_first < 0) || (s_first > s_last)) { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 219 | if (info->flash_id == FLASH_UNKNOWN) { |
| 220 | printf ("- missing\n"); |
| 221 | } else { |
| 222 | printf ("- no sectors to erase\n"); |
| 223 | } |
| 224 | return 1; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 225 | } |
| 226 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 227 | type = (info->flash_id & FLASH_VENDMASK); |
| 228 | if ((type != FLASH_MAN_INTEL)) { |
| 229 | printf ("Can't erase unknown flash type %08lx - aborted\n", |
| 230 | info->flash_id); |
| 231 | return 1; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | prot = 0; |
| 235 | for (sect=s_first; sect<=s_last; ++sect) { |
| 236 | if (info->protect[sect]) { |
| 237 | prot++; |
| 238 | } |
| 239 | } |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 240 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 241 | if (prot) { |
| 242 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 243 | prot); |
| 244 | } else { |
| 245 | printf ("\n"); |
| 246 | } |
| 247 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 248 | /* Disable interrupts which might cause a timeout here */ |
| 249 | flag = disable_interrupts(); |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 250 | |
| 251 | /* Start erase on unprotected sectors */ |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 252 | for (sect = s_first; sect<=s_last; sect++) { |
| 253 | if (info->protect[sect] == 0) { /* not protected */ |
| 254 | FPWV *addr = (FPWV *)(info->start[sect]); |
| 255 | FPW status; |
| 256 | |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 257 | printf("Erasing sector %2d ... ", sect); |
| 258 | |
| 259 | /* arm simple, non interrupt dependent timer */ |
Graeme Russ | f8b82ee | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 260 | start = get_timer(0); |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 261 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 262 | *addr = (FPW)0x00500050; /* clear status register */ |
| 263 | *addr = (FPW)0x00200020; /* erase setup */ |
| 264 | *addr = (FPW)0x00D000D0; /* erase confirm */ |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 265 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 266 | while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) { |
Graeme Russ | f8b82ee | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 267 | if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 268 | printf ("Timeout\n"); |
| 269 | *addr = (FPW)0x00B000B0; /* suspend erase */ |
| 270 | *addr = (FPW)0x00FF00FF; /* reset to read mode */ |
| 271 | rcode = 1; |
| 272 | break; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 276 | *addr = (FPW)0x00500050; /* clear status register cmd. */ |
| 277 | *addr = (FPW)0x00FF00FF; /* resest to read mode */ |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 278 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 279 | printf (" done\n"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 282 | return rcode; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /*----------------------------------------------------------------------- |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 286 | * Copy memory to flash, returns: |
| 287 | * 0 - OK |
| 288 | * 1 - write timeout |
| 289 | * 2 - Flash not erased |
| 290 | * 4 - Flash not identified |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 291 | */ |
| 292 | |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 293 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 294 | { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 295 | ulong cp, wp; |
| 296 | FPW data; |
| 297 | int count, i, l, rc, port_width; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 298 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 299 | if (info->flash_id == FLASH_UNKNOWN) { |
| 300 | return 4; |
| 301 | } |
| 302 | /* get lower word aligned address */ |
| 303 | #ifdef FLASH_PORT_WIDTH16 |
| 304 | wp = (addr & ~1); |
| 305 | port_width = 2; |
| 306 | #else |
| 307 | wp = (addr & ~3); |
| 308 | port_width = 4; |
| 309 | #endif |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 310 | |
| 311 | /* |
| 312 | * handle unaligned start bytes |
| 313 | */ |
| 314 | if ((l = addr - wp) != 0) { |
| 315 | data = 0; |
| 316 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 317 | data = (data << 8) | (*(uchar *)cp); |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 318 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 319 | for (; i<port_width && cnt>0; ++i) { |
| 320 | data = (data << 8) | *src++; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 321 | --cnt; |
| 322 | ++cp; |
| 323 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 324 | for (; cnt==0 && i<port_width; ++i, ++cp) { |
| 325 | data = (data << 8) | (*(uchar *)cp); |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 326 | } |
| 327 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 328 | if ((rc = write_data(info, wp, SWAP(data))) != 0) { |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 329 | return (rc); |
| 330 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 331 | wp += port_width; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* |
| 335 | * handle word aligned part |
| 336 | */ |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 337 | count = 0; |
| 338 | while (cnt >= port_width) { |
| 339 | data = 0; |
| 340 | for (i=0; i<port_width; ++i) { |
| 341 | data = (data << 8) | *src++; |
| 342 | } |
| 343 | if ((rc = write_data(info, wp, SWAP(data))) != 0) { |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 344 | return (rc); |
| 345 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 346 | wp += port_width; |
| 347 | cnt -= port_width; |
| 348 | if (count++ > 0x800) |
| 349 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 350 | spin_wheel(); |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 351 | count = 0; |
| 352 | } |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (cnt == 0) { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 356 | return (0); |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
| 360 | * handle unaligned tail bytes |
| 361 | */ |
| 362 | data = 0; |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 363 | for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) { |
| 364 | data = (data << 8) | *src++; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 365 | --cnt; |
| 366 | } |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 367 | for (; i<port_width; ++i, ++cp) { |
| 368 | data = (data << 8) | (*(uchar *)cp); |
| 369 | } |
| 370 | |
| 371 | return (write_data(info, wp, SWAP(data))); |
| 372 | } |
| 373 | |
| 374 | /*----------------------------------------------------------------------- |
| 375 | * Write a word or halfword to Flash, returns: |
| 376 | * 0 - OK |
| 377 | * 1 - write timeout |
| 378 | * 2 - Flash not erased |
| 379 | */ |
| 380 | static int write_data (flash_info_t *info, ulong dest, FPW data) |
| 381 | { |
| 382 | FPWV *addr = (FPWV *)dest; |
| 383 | ulong status; |
| 384 | int flag; |
Graeme Russ | f8b82ee | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 385 | ulong start; |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 386 | |
| 387 | /* Check if Flash is (sufficiently) erased */ |
| 388 | if ((*addr & data) != data) { |
| 389 | printf("not erased at %08lx (%x)\n",(ulong)addr,*addr); |
| 390 | return (2); |
| 391 | } |
| 392 | /* Disable interrupts which might cause a timeout here */ |
| 393 | flag = disable_interrupts(); |
| 394 | |
| 395 | *addr = (FPW)0x00400040; /* write setup */ |
| 396 | *addr = data; |
| 397 | |
| 398 | /* arm simple, non interrupt dependent timer */ |
Graeme Russ | f8b82ee | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 399 | start = get_timer(0); |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 400 | |
| 401 | /* wait while polling the status register */ |
| 402 | while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) { |
Graeme Russ | f8b82ee | 2011-07-15 23:31:37 +0000 | [diff] [blame] | 403 | if (start = get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 404 | *addr = (FPW)0x00FF00FF; /* restore read mode */ |
| 405 | return (1); |
| 406 | } |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 407 | } |
| 408 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 409 | *addr = (FPW)0x00FF00FF; /* restore read mode */ |
| 410 | |
| 411 | return (0); |
| 412 | } |
| 413 | |
| 414 | void inline |
| 415 | spin_wheel(void) |
| 416 | { |
| 417 | static int p=0; |
| 418 | static char w[] = "\\/-"; |
| 419 | |
| 420 | printf("\010%c", w[p]); |
| 421 | (++p == 3) ? (p = 0) : 0; |
wdenk | ea8015b | 2002-10-26 16:43:06 +0000 | [diff] [blame] | 422 | } |