wdenk | affae2b | 2002-08-17 09:36:01 +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 | |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 27 | flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 28 | |
| 29 | #if defined(CFG_ENV_IS_IN_FLASH) |
| 30 | # ifndef CFG_ENV_ADDR |
| 31 | # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET) |
| 32 | # endif |
| 33 | # ifndef CFG_ENV_SIZE |
| 34 | # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE |
| 35 | # endif |
| 36 | # ifndef CFG_ENV_SECT_SIZE |
| 37 | # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE |
| 38 | # endif |
| 39 | #endif |
| 40 | |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 41 | #define QUAD_ID(id) ((((ulong)(id) & 0xFF) << 24) | \ |
| 42 | (((ulong)(id) & 0xFF) << 16) | \ |
| 43 | (((ulong)(id) & 0xFF) << 8) | \ |
| 44 | (((ulong)(id) & 0xFF) << 0) \ |
| 45 | ) |
| 46 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 47 | /*----------------------------------------------------------------------- |
| 48 | * Functions |
| 49 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 50 | static ulong flash_get_size (vu_long * addr, flash_info_t * info); |
| 51 | static int write_word (flash_info_t * info, ulong dest, ulong data); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 52 | |
| 53 | /*----------------------------------------------------------------------- |
| 54 | */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 55 | unsigned long flash_init (void) |
| 56 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 57 | volatile immap_t *immap = (immap_t *) CFG_IMMR; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 58 | volatile memctl8xx_t *memctl = &immap->im_memctl; |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 59 | vu_long *bcsr = (vu_long *)BCSR_ADDR; |
| 60 | unsigned long pd_size, total_size, bsize, or_am; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 61 | int i; |
| 62 | |
| 63 | /* Init: no FLASHes known */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 64 | for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 65 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 66 | flash_info[i].size = 0; |
| 67 | flash_info[i].sector_count = 0; |
| 68 | flash_info[i].start[0] = 0xFFFFFFFF; /* For TFTP */ |
| 69 | } |
| 70 | |
| 71 | switch ((bcsr[2] & BCSR2_FLASH_PD_MASK) >> BCSR2_FLASH_PD_SHIFT) { |
| 72 | case 2: |
| 73 | case 4: |
| 74 | case 6: |
| 75 | pd_size = 0x800000; |
| 76 | or_am = 0xFF800000; |
| 77 | break; |
| 78 | |
| 79 | case 5: |
| 80 | case 7: |
| 81 | pd_size = 0x400000; |
| 82 | or_am = 0xFFC00000; |
| 83 | break; |
| 84 | |
| 85 | case 8: |
| 86 | pd_size = 0x200000; |
| 87 | or_am = 0xFFE00000; |
| 88 | break; |
| 89 | |
| 90 | default: |
| 91 | pd_size = 0; |
| 92 | or_am = 0xFFE00000; |
Wolfgang Denk | 9b55a25 | 2008-07-11 01:16:00 +0200 | [diff] [blame^] | 93 | printf("## Unsupported flash detected by BCSR: 0x%08lX\n", bcsr[2]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | total_size = 0; |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 97 | for (i = 0; i < CFG_MAX_FLASH_BANKS && total_size < pd_size; ++i) { |
| 98 | bsize = flash_get_size((vu_long *)(CFG_FLASH_BASE + total_size), |
| 99 | &flash_info[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 100 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 101 | if (flash_info[i].flash_id == FLASH_UNKNOWN) { |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 102 | printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n", |
| 103 | i, bsize, bsize >> 20); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 104 | } |
| 105 | |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 106 | total_size += bsize; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 107 | } |
| 108 | |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 109 | if (total_size != pd_size) { |
| 110 | printf("## Detected flash size %lu conflicts with PD data %lu\n", |
| 111 | total_size, pd_size); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* Remap FLASH according to real size */ |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 115 | memctl->memc_or0 = or_am | CFG_OR_TIMING_FLASH; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 116 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 117 | for (i = 0; i < CFG_MAX_FLASH_BANKS && flash_info[i].size != 0; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 118 | #if CFG_MONITOR_BASE >= CFG_FLASH_BASE |
| 119 | /* monitor protection ON by default */ |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 120 | if (CFG_MONITOR_BASE >= flash_info[i].start[0]) |
| 121 | flash_protect (FLAG_PROTECT_SET, |
| 122 | CFG_MONITOR_BASE, |
| 123 | CFG_MONITOR_BASE + monitor_flash_len - 1, |
| 124 | &flash_info[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 125 | #endif |
| 126 | |
| 127 | #ifdef CFG_ENV_IS_IN_FLASH |
| 128 | /* ENV protection ON by default */ |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 129 | if (CFG_ENV_ADDR >= flash_info[i].start[0]) |
| 130 | flash_protect (FLAG_PROTECT_SET, |
| 131 | CFG_ENV_ADDR, |
| 132 | CFG_ENV_ADDR + CFG_ENV_SIZE - 1, |
| 133 | &flash_info[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 134 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 135 | } |
| 136 | |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 137 | return total_size; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /*----------------------------------------------------------------------- |
| 141 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 142 | void flash_print_info (flash_info_t * info) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 143 | { |
| 144 | int i; |
| 145 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 146 | if (info->flash_id == FLASH_UNKNOWN) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 147 | printf ("missing or unknown FLASH type\n"); |
| 148 | return; |
| 149 | } |
| 150 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 151 | switch (info->flash_id & FLASH_VENDMASK) { |
| 152 | case FLASH_MAN_AMD: |
| 153 | printf ("AMD "); |
| 154 | break; |
| 155 | case FLASH_MAN_FUJ: |
| 156 | printf ("FUJITSU "); |
| 157 | break; |
| 158 | case FLASH_MAN_BM: |
| 159 | printf ("BRIGHT MICRO "); |
| 160 | break; |
| 161 | default: |
| 162 | printf ("Unknown Vendor "); |
| 163 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 164 | } |
| 165 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 166 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 167 | case FLASH_AM040: |
| 168 | printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n"); |
| 169 | break; |
| 170 | case FLASH_AM080: |
| 171 | printf ("29F080 or 29LV080 (8 Mbit, uniform sectors)\n"); |
| 172 | break; |
| 173 | case FLASH_AM400B: |
| 174 | printf ("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
| 175 | break; |
| 176 | case FLASH_AM400T: |
| 177 | printf ("AM29LV400T (4 Mbit, top boot sector)\n"); |
| 178 | break; |
| 179 | case FLASH_AM800B: |
| 180 | printf ("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
| 181 | break; |
| 182 | case FLASH_AM800T: |
| 183 | printf ("AM29LV800T (8 Mbit, top boot sector)\n"); |
| 184 | break; |
| 185 | case FLASH_AM160B: |
| 186 | printf ("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
| 187 | break; |
| 188 | case FLASH_AM160T: |
| 189 | printf ("AM29LV160T (16 Mbit, top boot sector)\n"); |
| 190 | break; |
| 191 | case FLASH_AM320B: |
| 192 | printf ("AM29LV320B (32 Mbit, bottom boot sect)\n"); |
| 193 | break; |
| 194 | case FLASH_AM320T: |
| 195 | printf ("AM29LV320T (32 Mbit, top boot sector)\n"); |
| 196 | break; |
| 197 | default: |
| 198 | printf ("Unknown Chip Type\n"); |
| 199 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 200 | } |
| 201 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 202 | printf (" Size: %ld MB in %d Sectors\n", info->size >> 20, |
| 203 | info->sector_count); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 204 | |
| 205 | printf (" Sector Start Addresses:"); |
| 206 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 207 | for (i = 0; i < info->sector_count; ++i) { |
| 208 | if ((i % 5) == 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 209 | printf ("\n "); |
| 210 | } |
| 211 | |
| 212 | printf (" %08lX%s", |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 213 | info->start[i], info->protect[i] ? " (RO)" : " "); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | printf ("\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /*----------------------------------------------------------------------- |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 220 | * The following code can not run from flash! |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 221 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 222 | static ulong flash_get_size (vu_long * addr, flash_info_t * info) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 223 | { |
| 224 | short i; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 225 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 226 | /* Write auto select command: read Manufacturer ID */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 227 | addr[0x0555] = 0xAAAAAAAA; |
| 228 | addr[0x02AA] = 0x55555555; |
| 229 | addr[0x0555] = 0x90909090; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 230 | |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 231 | switch (addr[0]) { |
| 232 | case QUAD_ID(AMD_MANUFACT): |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 233 | info->flash_id = FLASH_MAN_AMD; |
| 234 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 235 | |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 236 | case QUAD_ID(FUJ_MANUFACT): |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 237 | info->flash_id = FLASH_MAN_FUJ; |
| 238 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 239 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 240 | default: |
| 241 | info->flash_id = FLASH_UNKNOWN; |
| 242 | info->sector_count = 0; |
| 243 | info->size = 0; |
| 244 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 245 | } |
| 246 | |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 247 | switch (addr[1]) { /* device ID */ |
| 248 | case QUAD_ID(AMD_ID_F040B): |
| 249 | case QUAD_ID(AMD_ID_LV040B): |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 250 | info->flash_id += FLASH_AM040; |
| 251 | info->sector_count = 8; |
| 252 | info->size = 0x00200000; |
| 253 | break; /* => 2 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 254 | |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 255 | case QUAD_ID(AMD_ID_F080B): |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 256 | info->flash_id += FLASH_AM080; |
| 257 | info->sector_count = 16; |
| 258 | info->size = 0x00400000; |
| 259 | break; /* => 4 MB */ |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 260 | #if 0 |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 261 | case AMD_ID_LV400T: |
| 262 | info->flash_id += FLASH_AM400T; |
| 263 | info->sector_count = 11; |
| 264 | info->size = 0x00100000; |
| 265 | break; /* => 1 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 266 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 267 | case AMD_ID_LV400B: |
| 268 | info->flash_id += FLASH_AM400B; |
| 269 | info->sector_count = 11; |
| 270 | info->size = 0x00100000; |
| 271 | break; /* => 1 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 272 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 273 | case AMD_ID_LV800T: |
| 274 | info->flash_id += FLASH_AM800T; |
| 275 | info->sector_count = 19; |
| 276 | info->size = 0x00200000; |
| 277 | break; /* => 2 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 278 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 279 | case AMD_ID_LV800B: |
| 280 | info->flash_id += FLASH_AM800B; |
| 281 | info->sector_count = 19; |
| 282 | info->size = 0x00200000; |
| 283 | break; /* => 2 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 284 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 285 | case AMD_ID_LV160T: |
| 286 | info->flash_id += FLASH_AM160T; |
| 287 | info->sector_count = 35; |
| 288 | info->size = 0x00400000; |
| 289 | break; /* => 4 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 290 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 291 | case AMD_ID_LV160B: |
| 292 | info->flash_id += FLASH_AM160B; |
| 293 | info->sector_count = 35; |
| 294 | info->size = 0x00400000; |
| 295 | break; /* => 4 MB */ |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 296 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 297 | case AMD_ID_LV320T: |
| 298 | info->flash_id += FLASH_AM320T; |
| 299 | info->sector_count = 67; |
| 300 | info->size = 0x00800000; |
| 301 | break; /* => 8 MB */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 302 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 303 | case AMD_ID_LV320B: |
| 304 | info->flash_id += FLASH_AM320B; |
| 305 | info->sector_count = 67; |
| 306 | info->size = 0x00800000; |
| 307 | break; /* => 8 MB */ |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 308 | #endif /* 0 */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 309 | default: |
| 310 | info->flash_id = FLASH_UNKNOWN; |
| 311 | return (0); /* => no or unknown flash */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | #if 0 |
| 315 | /* set up sector start address table */ |
| 316 | if (info->flash_id & FLASH_BTYPE) { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 317 | /* set sector offsets for bottom boot block type */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 318 | info->start[0] = base + 0x00000000; |
| 319 | info->start[1] = base + 0x00008000; |
| 320 | info->start[2] = base + 0x0000C000; |
| 321 | info->start[3] = base + 0x00010000; |
| 322 | for (i = 4; i < info->sector_count; i++) { |
| 323 | info->start[i] = base + (i * 0x00020000) - 0x00060000; |
| 324 | } |
| 325 | } else { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 326 | /* set sector offsets for top boot block type */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 327 | i = info->sector_count - 1; |
| 328 | info->start[i--] = base + info->size - 0x00008000; |
| 329 | info->start[i--] = base + info->size - 0x0000C000; |
| 330 | info->start[i--] = base + info->size - 0x00010000; |
| 331 | for (; i >= 0; i--) { |
| 332 | info->start[i] = base + i * 0x00020000; |
| 333 | } |
| 334 | } |
| 335 | #else |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 336 | /* set sector offsets for uniform sector type */ |
| 337 | for (i = 0; i < info->sector_count; i++) |
| 338 | info->start[i] = (ulong)addr + (i * 0x00040000); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 339 | #endif |
| 340 | |
| 341 | /* check for protected sectors */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 342 | for (i = 0; i < info->sector_count; i++) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 343 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 344 | /* D0 = 1 if protected */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 345 | addr = (volatile unsigned long *) (info->start[i]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 346 | info->protect[i] = addr[2] & 1; |
| 347 | } |
| 348 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 349 | if (info->flash_id != FLASH_UNKNOWN) { |
| 350 | addr = (volatile unsigned long *) info->start[0]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 351 | *addr = 0xF0F0F0F0; /* reset bank */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | return (info->size); |
| 355 | } |
| 356 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 357 | /*----------------------------------------------------------------------- |
| 358 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 359 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 360 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 361 | vu_long *addr = (vu_long *) (info->start[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 362 | int flag, prot, sect, l_sect; |
| 363 | ulong start, now, last; |
| 364 | |
| 365 | if ((s_first < 0) || (s_first > s_last)) { |
| 366 | if (info->flash_id == FLASH_UNKNOWN) { |
| 367 | printf ("- missing\n"); |
| 368 | } else { |
| 369 | printf ("- no sectors to erase\n"); |
| 370 | } |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 371 | return ERR_INVAL; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | if ((info->flash_id == FLASH_UNKNOWN) || |
| 375 | (info->flash_id > FLASH_AMD_COMP)) { |
| 376 | printf ("Can't erase unknown flash type - aborted\n"); |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 377 | return ERR_UNKNOWN_FLASH_TYPE; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | prot = 0; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 381 | for (sect = s_first; sect <= s_last; ++sect) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 382 | if (info->protect[sect]) { |
| 383 | prot++; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (prot) { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 388 | printf ("- Warning: %d protected sectors will not be erased!\n", prot); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 389 | } else { |
| 390 | printf ("\n"); |
| 391 | } |
| 392 | |
| 393 | l_sect = -1; |
| 394 | |
| 395 | /* Disable interrupts which might cause a timeout here */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 396 | flag = disable_interrupts (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 397 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 398 | addr[0x0555] = 0xAAAAAAAA; |
| 399 | addr[0x02AA] = 0x55555555; |
| 400 | addr[0x0555] = 0x80808080; |
| 401 | addr[0x0555] = 0xAAAAAAAA; |
| 402 | addr[0x02AA] = 0x55555555; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 403 | |
| 404 | /* Start erase on unprotected sectors */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 405 | for (sect = s_first; sect <= s_last; sect++) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 406 | if (info->protect[sect] == 0) { /* not protected */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 407 | addr = (vu_long *) (info->start[sect]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 408 | addr[0] = 0x30303030; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 409 | l_sect = sect; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* re-enable interrupts if necessary */ |
| 414 | if (flag) |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 415 | enable_interrupts (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 416 | |
| 417 | /* wait at least 80us - let's wait 1 ms */ |
| 418 | udelay (1000); |
| 419 | |
| 420 | /* |
| 421 | * We wait for the last triggered sector |
| 422 | */ |
| 423 | if (l_sect < 0) |
| 424 | goto DONE; |
| 425 | |
| 426 | start = get_timer (0); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 427 | last = start; |
| 428 | addr = (vu_long *) (info->start[l_sect]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 429 | while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 430 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 431 | if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 432 | printf ("Timeout\n"); |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 433 | return ERR_TIMOUT; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 434 | } |
| 435 | /* show that we're waiting */ |
| 436 | if ((now - last) > 1000) { /* every second */ |
| 437 | putc ('.'); |
| 438 | last = now; |
| 439 | } |
| 440 | } |
| 441 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 442 | DONE: |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 443 | /* reset to read mode */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 444 | addr = (volatile unsigned long *) info->start[0]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 445 | addr[0] = 0xF0F0F0F0; /* reset bank */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 446 | |
| 447 | printf (" done\n"); |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 448 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /*----------------------------------------------------------------------- |
| 453 | * Copy memory to flash, returns: |
| 454 | * 0 - OK |
| 455 | * 1 - write timeout |
| 456 | * 2 - Flash not erased |
| 457 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 458 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 459 | { |
| 460 | ulong cp, wp, data; |
| 461 | int i, l, rc; |
| 462 | |
| 463 | wp = (addr & ~3); /* get lower word aligned address */ |
| 464 | |
| 465 | /* |
| 466 | * handle unaligned start bytes |
| 467 | */ |
| 468 | if ((l = addr - wp) != 0) { |
| 469 | data = 0; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 470 | for (i = 0, cp = wp; i < l; ++i, ++cp) { |
| 471 | data = (data << 8) | (*(uchar *) cp); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 472 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 473 | for (; i < 4 && cnt > 0; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 474 | data = (data << 8) | *src++; |
| 475 | --cnt; |
| 476 | ++cp; |
| 477 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 478 | for (; cnt == 0 && i < 4; ++i, ++cp) { |
| 479 | data = (data << 8) | (*(uchar *) cp); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 480 | } |
| 481 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 482 | if ((rc = write_word (info, wp, data)) != 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 483 | return (rc); |
| 484 | } |
| 485 | wp += 4; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * handle word aligned part |
| 490 | */ |
| 491 | while (cnt >= 4) { |
| 492 | data = 0; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 493 | for (i = 0; i < 4; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 494 | data = (data << 8) | *src++; |
| 495 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 496 | if ((rc = write_word (info, wp, data)) != 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 497 | return (rc); |
| 498 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 499 | wp += 4; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 500 | cnt -= 4; |
| 501 | } |
| 502 | |
| 503 | if (cnt == 0) { |
| 504 | return (0); |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * handle unaligned tail bytes |
| 509 | */ |
| 510 | data = 0; |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 511 | for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 512 | data = (data << 8) | *src++; |
| 513 | --cnt; |
| 514 | } |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 515 | for (; i < 4; ++i, ++cp) { |
| 516 | data = (data << 8) | (*(uchar *) cp); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 517 | } |
| 518 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 519 | return (write_word (info, wp, data)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /*----------------------------------------------------------------------- |
| 523 | * Write a word to Flash, returns: |
| 524 | * 0 - OK |
| 525 | * 1 - write timeout |
| 526 | * 2 - Flash not erased |
| 527 | */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 528 | static int write_word (flash_info_t * info, ulong dest, ulong data) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 529 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 530 | vu_long *addr = (vu_long *) (info->start[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 531 | ulong start; |
| 532 | int flag; |
| 533 | |
| 534 | /* Check if Flash is (sufficiently) erased */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 535 | if ((*((vu_long *) dest) & data) != data) { |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 536 | return ERR_NOT_ERASED; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 537 | } |
| 538 | /* Disable interrupts which might cause a timeout here */ |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 539 | flag = disable_interrupts (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 540 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 541 | addr[0x0555] = 0xAAAAAAAA; |
| 542 | addr[0x02AA] = 0x55555555; |
| 543 | addr[0x0555] = 0xA0A0A0A0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 544 | |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 545 | *((vu_long *) dest) = data; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 546 | |
| 547 | /* re-enable interrupts if necessary */ |
| 548 | if (flag) |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 549 | enable_interrupts (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 550 | |
| 551 | /* data polling for D7 */ |
| 552 | start = get_timer (0); |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 553 | while ((*((vu_long *) dest) & 0x80808080) != (data & 0x80808080)) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 554 | { |
wdenk | bf9e3b3 | 2004-02-12 00:47:09 +0000 | [diff] [blame] | 555 | if (get_timer (start) > CFG_FLASH_WRITE_TOUT) { |
wdenk | 99edcfb | 2004-06-09 21:54:22 +0000 | [diff] [blame] | 556 | return ERR_TIMOUT; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | return (0); |
| 560 | } |