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 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 27 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 28 | |
| 29 | /*----------------------------------------------------------------------- |
| 30 | * Functions |
| 31 | */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 32 | static ulong flash_get_size(vu_long *addr, flash_info_t *info); |
| 33 | static int write_word(flash_info_t *info, ulong dest, ulong data); |
| 34 | static void flash_get_offsets(ulong base, flash_info_t *info); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 35 | |
| 36 | /*----------------------------------------------------------------------- |
| 37 | */ |
| 38 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 39 | unsigned long flash_init(void) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 40 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 41 | volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 42 | volatile memctl8xx_t *memctl = &immap->im_memctl; |
Wolfgang Denk | 8a210ff | 2011-11-04 15:55:39 +0000 | [diff] [blame] | 43 | unsigned long size_b0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 44 | int i; |
| 45 | |
| 46 | /* Init: no FLASHes known */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 47 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 48 | flash_info[i].flash_id = FLASH_UNKNOWN; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 49 | |
| 50 | /* Static FLASH Bank configuration here - FIXME XXX */ |
| 51 | |
| 52 | size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]); |
| 53 | |
| 54 | if (flash_info[0].flash_id == FLASH_UNKNOWN) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 55 | printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 56 | size_b0, size_b0<<20); |
| 57 | } |
| 58 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 59 | /* Remap FLASH according to real size */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 60 | memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 61 | #ifdef CONFIG_FLASH_16BIT |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 62 | memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | |
| 63 | BR_MS_GPCM | BR_V | BR_PS_16; /* 16 Bit data port */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 64 | #else |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 65 | memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | |
| 66 | BR_MS_GPCM | BR_V; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | /* Re-do sizing to get full correct info */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 70 | size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, |
| 71 | &flash_info[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 72 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 73 | flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 74 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 75 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 76 | /* monitor protection ON by default */ |
| 77 | flash_protect(FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 78 | CONFIG_SYS_MONITOR_BASE, |
| 79 | CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1, |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 80 | &flash_info[0]); |
| 81 | #endif |
| 82 | |
Wolfgang Denk | 8a210ff | 2011-11-04 15:55:39 +0000 | [diff] [blame] | 83 | memctl->memc_br1 = 0; /* invalidate bank 1 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 84 | |
| 85 | flash_info[0].size = size_b0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 86 | |
Wolfgang Denk | 8a210ff | 2011-11-04 15:55:39 +0000 | [diff] [blame] | 87 | return size_b0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /*----------------------------------------------------------------------- |
| 91 | */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 92 | static void flash_get_offsets(ulong base, flash_info_t *info) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 93 | { |
| 94 | int i; |
| 95 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 96 | if (info->flash_id == FLASH_UNKNOWN) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 97 | return; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 98 | |
| 99 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 100 | for (i = 0; i < info->sector_count; i++) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 101 | info->start[i] = base + (i * 0x00002000); |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 102 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
| 106 | /* set up sector start address table */ |
| 107 | if (info->flash_id & FLASH_BTYPE) { |
| 108 | /* set sector offsets for bottom boot block type */ |
| 109 | #ifdef CONFIG_FLASH_16BIT |
| 110 | info->start[0] = base + 0x00000000; |
| 111 | info->start[1] = base + 0x00004000; |
| 112 | info->start[2] = base + 0x00006000; |
| 113 | info->start[3] = base + 0x00008000; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 114 | for (i = 4; i < info->sector_count; i++) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 115 | info->start[i] = base + (i * 0x00010000) - 0x00030000; |
| 116 | #else |
| 117 | info->start[0] = base + 0x00000000; |
| 118 | info->start[1] = base + 0x00008000; |
| 119 | info->start[2] = base + 0x0000C000; |
| 120 | info->start[3] = base + 0x00010000; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 121 | for (i = 4; i < info->sector_count; i++) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 122 | info->start[i] = base + (i * 0x00020000) - 0x00060000; |
| 123 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 124 | } else { |
| 125 | /* set sector offsets for top boot block type */ |
| 126 | i = info->sector_count - 1; |
| 127 | info->start[i--] = base + info->size - 0x00008000; |
| 128 | info->start[i--] = base + info->size - 0x0000C000; |
| 129 | info->start[i--] = base + info->size - 0x00010000; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 130 | for (; i >= 0; i--) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 131 | info->start[i] = base + i * 0x00020000; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 132 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /*----------------------------------------------------------------------- |
| 136 | */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 137 | void flash_print_info(flash_info_t *info) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 138 | { |
| 139 | int i; |
| 140 | |
| 141 | if (info->flash_id == FLASH_UNKNOWN) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 142 | printf("missing or unknown FLASH type\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
| 146 | switch (info->flash_id & FLASH_VENDMASK) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 147 | case FLASH_MAN_AMD: |
| 148 | printf("AMD "); |
| 149 | break; |
| 150 | case FLASH_MAN_FUJ: |
| 151 | printf("FUJITSU "); |
| 152 | break; |
| 153 | case FLASH_MAN_SST: |
| 154 | printf("SST "); |
| 155 | break; |
| 156 | case FLASH_MAN_STM: |
| 157 | printf("STM "); |
| 158 | break; |
| 159 | default: |
| 160 | printf("Unknown Vendor "); |
| 161 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | switch (info->flash_id & FLASH_TYPEMASK) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 165 | case FLASH_AM400B: |
| 166 | printf("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
| 167 | break; |
| 168 | case FLASH_AM400T: |
| 169 | printf("AM29LV400T (4 Mbit, top boot sector)\n"); |
| 170 | break; |
| 171 | case FLASH_AM800B: |
| 172 | printf("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
| 173 | break; |
| 174 | case FLASH_AM800T: |
| 175 | printf("AM29LV800T (8 Mbit, top boot sector)\n"); |
| 176 | break; |
| 177 | case FLASH_AM160B: |
| 178 | printf("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
| 179 | break; |
| 180 | case FLASH_AM160T: |
| 181 | printf("AM29LV160T (16 Mbit, top boot sector)\n"); |
| 182 | break; |
| 183 | case FLASH_AM320B: |
| 184 | printf("AM29LV320B (32 Mbit, bottom boot sect)\n"); |
| 185 | break; |
| 186 | case FLASH_AM320T: |
| 187 | printf("AM29LV320T (32 Mbit, top boot sector)\n"); |
| 188 | break; |
| 189 | case FLASH_SST200A: |
| 190 | printf("39xF200A (2M = 128K x 16)\n"); |
| 191 | break; |
| 192 | case FLASH_SST400A: |
| 193 | printf("39xF400A (4M = 256K x 16)\n"); |
| 194 | break; |
| 195 | case FLASH_SST800A: |
| 196 | printf("39xF800A (8M = 512K x 16)\n"); |
| 197 | break; |
| 198 | case FLASH_STM800AB: |
| 199 | printf("M29W800AB (8M = 512K x 16)\n"); |
| 200 | break; |
| 201 | default: |
| 202 | printf("Unknown Chip Type\n"); |
| 203 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 206 | printf(" Size: %ld MB in %d Sectors\n", |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 207 | info->size >> 20, info->sector_count); |
| 208 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 209 | printf(" Sector Start Addresses:"); |
| 210 | for (i = 0; i < info->sector_count; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 211 | if ((i % 5) == 0) |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 212 | printf("\n "); |
| 213 | printf(" %08lX%s", |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 214 | info->start[i], |
| 215 | info->protect[i] ? " (RO)" : " " |
| 216 | ); |
| 217 | } |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 218 | printf("\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 219 | return; |
| 220 | } |
| 221 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 222 | /* |
| 223 | * The following code cannot be run from FLASH! |
| 224 | */ |
| 225 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 226 | static ulong flash_get_size(vu_long *addr, flash_info_t *info) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 227 | { |
| 228 | short i; |
| 229 | ulong value; |
| 230 | ulong base = (ulong)addr; |
| 231 | |
| 232 | /* Write auto select command: read Manufacturer ID */ |
| 233 | #ifdef CONFIG_FLASH_16BIT |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 234 | vu_short *s_addr = (vu_short *)addr; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 235 | s_addr[0x5555] = 0x00AA; |
| 236 | s_addr[0x2AAA] = 0x0055; |
| 237 | s_addr[0x5555] = 0x0090; |
| 238 | value = s_addr[0]; |
| 239 | value = value|(value<<16); |
| 240 | #else |
| 241 | addr[0x5555] = 0x00AA00AA; |
| 242 | addr[0x2AAA] = 0x00550055; |
| 243 | addr[0x5555] = 0x00900090; |
| 244 | value = addr[0]; |
| 245 | #endif |
| 246 | |
| 247 | switch (value) { |
| 248 | case AMD_MANUFACT: |
| 249 | info->flash_id = FLASH_MAN_AMD; |
| 250 | break; |
| 251 | case FUJ_MANUFACT: |
| 252 | info->flash_id = FLASH_MAN_FUJ; |
| 253 | break; |
| 254 | case SST_MANUFACT: |
| 255 | info->flash_id = FLASH_MAN_SST; |
| 256 | break; |
| 257 | case STM_MANUFACT: |
| 258 | info->flash_id = FLASH_MAN_STM; |
| 259 | break; |
| 260 | default: |
| 261 | info->flash_id = FLASH_UNKNOWN; |
| 262 | info->sector_count = 0; |
| 263 | info->size = 0; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 264 | return 0; /* no or unknown flash */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 265 | } |
| 266 | #ifdef CONFIG_FLASH_16BIT |
| 267 | value = s_addr[1]; |
| 268 | value = value|(value<<16); |
| 269 | #else |
| 270 | value = addr[1]; /* device ID */ |
| 271 | #endif |
| 272 | |
| 273 | switch (value) { |
| 274 | case AMD_ID_LV400T: |
| 275 | info->flash_id += FLASH_AM400T; |
| 276 | info->sector_count = 11; |
| 277 | info->size = 0x00100000; |
| 278 | break; /* => 1 MB */ |
| 279 | |
| 280 | case AMD_ID_LV400B: |
| 281 | info->flash_id += FLASH_AM400B; |
| 282 | info->sector_count = 11; |
| 283 | info->size = 0x00100000; |
| 284 | break; /* => 1 MB */ |
| 285 | |
| 286 | case AMD_ID_LV800T: |
| 287 | info->flash_id += FLASH_AM800T; |
| 288 | info->sector_count = 19; |
| 289 | info->size = 0x00200000; |
| 290 | break; /* => 2 MB */ |
| 291 | |
| 292 | case AMD_ID_LV800B: |
| 293 | info->flash_id += FLASH_AM800B; |
| 294 | #ifdef CONFIG_FLASH_16BIT |
| 295 | info->sector_count = 19; |
| 296 | info->size = 0x00100000; /* => 1 MB */ |
| 297 | #else |
| 298 | info->sector_count = 19; |
| 299 | info->size = 0x00200000; /* => 2 MB */ |
| 300 | #endif |
| 301 | break; |
| 302 | |
| 303 | case AMD_ID_LV160T: |
| 304 | info->flash_id += FLASH_AM160T; |
| 305 | info->sector_count = 35; |
| 306 | info->size = 0x00400000; |
| 307 | break; /* => 4 MB */ |
| 308 | |
| 309 | case AMD_ID_LV160B: |
| 310 | info->flash_id += FLASH_AM160B; |
| 311 | #ifdef CONFIG_FLASH_16BIT |
| 312 | info->sector_count = 35; |
| 313 | info->size = 0x00200000; /* => 2 MB */ |
| 314 | #else |
| 315 | info->sector_count = 35; |
| 316 | info->size = 0x00400000; /* => 4 MB */ |
| 317 | #endif |
| 318 | |
| 319 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 320 | case SST_ID_xF200A: |
| 321 | info->flash_id += FLASH_SST200A; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 322 | info->sector_count = 64; /* 39xF200A (2M = 128K x 16) */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 323 | info->size = 0x00080000; |
| 324 | break; |
| 325 | case SST_ID_xF400A: |
| 326 | info->flash_id += FLASH_SST400A; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 327 | info->sector_count = 128; /* 39xF400A (4M = 256K x 16) */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 328 | info->size = 0x00100000; |
| 329 | break; |
| 330 | case SST_ID_xF800A: |
| 331 | info->flash_id += FLASH_SST800A; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 332 | info->sector_count = 256; /* 39xF800A (8M = 512K x 16) */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 333 | info->size = 0x00200000; |
| 334 | break; /* => 2 MB */ |
| 335 | case STM_ID_x800AB: |
| 336 | info->flash_id += FLASH_STM800AB; |
| 337 | info->sector_count = 19; |
| 338 | info->size = 0x00200000; |
| 339 | break; /* => 2 MB */ |
| 340 | default: |
| 341 | info->flash_id = FLASH_UNKNOWN; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 342 | return 0; /* => no or unknown flash */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 343 | |
| 344 | } |
| 345 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 346 | if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 347 | printf("** ERROR: sector count %d > max (%d) **\n", |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 348 | info->sector_count, CONFIG_SYS_MAX_FLASH_SECT); |
| 349 | info->sector_count = CONFIG_SYS_MAX_FLASH_SECT; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 353 | for (i = 0; i < info->sector_count; i++) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 354 | info->start[i] = base + (i * 0x00002000); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 355 | } else { /* AMD and Fujitsu types */ |
| 356 | /* set up sector start address table */ |
| 357 | if (info->flash_id & FLASH_BTYPE) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 358 | /* set sector offsets for bottom boot block type */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 359 | #ifdef CONFIG_FLASH_16BIT |
| 360 | |
| 361 | info->start[0] = base + 0x00000000; |
| 362 | info->start[1] = base + 0x00004000; |
| 363 | info->start[2] = base + 0x00006000; |
| 364 | info->start[3] = base + 0x00008000; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 365 | for (i = 4; i < info->sector_count; i++) |
| 366 | info->start[i] = base + |
| 367 | (i * 0x00010000) - 0x00030000; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 368 | #else |
| 369 | info->start[0] = base + 0x00000000; |
| 370 | info->start[1] = base + 0x00008000; |
| 371 | info->start[2] = base + 0x0000C000; |
| 372 | info->start[3] = base + 0x00010000; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 373 | for (i = 4; i < info->sector_count; i++) |
| 374 | info->start[i] = base + |
| 375 | (i * 0x00020000) - 0x00060000; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 376 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 377 | } else { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 378 | /* set sector offsets for top boot block type */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 379 | i = info->sector_count - 1; |
| 380 | info->start[i--] = base + info->size - 0x00008000; |
| 381 | info->start[i--] = base + info->size - 0x0000C000; |
| 382 | info->start[i--] = base + info->size - 0x00010000; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 383 | for (; i >= 0; i--) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 384 | info->start[i] = base + i * 0x00020000; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* check for protected sectors */ |
| 388 | for (i = 0; i < info->sector_count; i++) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 389 | /* |
| 390 | * read sector protection at sector address: |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 391 | * (A7 .. A0) = 0x02 |
| 392 | * D0 = 1 if protected |
| 393 | */ |
| 394 | #ifdef CONFIG_FLASH_16BIT |
| 395 | s_addr = (volatile unsigned short *)(info->start[i]); |
| 396 | info->protect[i] = s_addr[2] & 1; |
| 397 | #else |
| 398 | addr = (volatile unsigned long *)(info->start[i]); |
| 399 | info->protect[i] = addr[2] & 1; |
| 400 | #endif |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Prevent writes to uninitialized FLASH. |
| 406 | */ |
| 407 | if (info->flash_id != FLASH_UNKNOWN) { |
| 408 | #ifdef CONFIG_FLASH_16BIT |
| 409 | s_addr = (volatile unsigned short *)(info->start[0]); |
| 410 | *s_addr = 0x00F0; /* reset bank */ |
| 411 | #else |
| 412 | addr = (volatile unsigned long *)info->start[0]; |
| 413 | *addr = 0x00F000F0; /* reset bank */ |
| 414 | #endif |
| 415 | |
| 416 | } |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 417 | return info->size; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 420 | int flash_erase(flash_info_t *info, int s_first, int s_last) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 421 | { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 422 | vu_long *addr = (vu_long *)(info->start[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 423 | int flag, prot, sect; |
| 424 | ulong start, now, last; |
| 425 | #ifdef CONFIG_FLASH_16BIT |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 426 | vu_short *s_addr = (vu_short *)addr; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 427 | #endif |
| 428 | |
| 429 | if ((s_first < 0) || (s_first > s_last)) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 430 | if (info->flash_id == FLASH_UNKNOWN) |
| 431 | printf("- missing\n"); |
| 432 | else |
| 433 | printf("- no sectors to erase\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 434 | return 1; |
| 435 | } |
| 436 | /*#ifndef CONFIG_FLASH_16BIT |
| 437 | ulong type; |
| 438 | type = (info->flash_id & FLASH_VENDMASK); |
| 439 | if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) { |
| 440 | printf ("Can't erase unknown flash type %08lx - aborted\n", |
| 441 | info->flash_id); |
| 442 | return; |
| 443 | } |
| 444 | #endif*/ |
| 445 | prot = 0; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 446 | for (sect = s_first; sect <= s_last; ++sect) { |
| 447 | if (info->protect[sect]) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 448 | prot++; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | if (prot) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 452 | printf("- Warning: %d protected sectors will not be erased!\n", |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 453 | prot); |
| 454 | } else { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 455 | printf("\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 458 | start = get_timer(0); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 459 | last = start; |
| 460 | /* Start erase on unprotected sectors */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 461 | for (sect = s_first; sect <= s_last; sect++) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 462 | if (info->protect[sect] == 0) { /* not protected */ |
| 463 | #ifdef CONFIG_FLASH_16BIT |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 464 | vu_short *s_sect_addr = (vu_short *)(info->start[sect]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 465 | #else |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 466 | vu_long *sect_addr = (vu_long *)(info->start[sect]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 467 | #endif |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 468 | /* Disable interrupts which might cause a timeout */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 469 | flag = disable_interrupts(); |
| 470 | |
| 471 | #ifdef CONFIG_FLASH_16BIT |
| 472 | |
| 473 | /*printf("\ns_sect_addr=%x",s_sect_addr);*/ |
| 474 | s_addr[0x5555] = 0x00AA; |
| 475 | s_addr[0x2AAA] = 0x0055; |
| 476 | s_addr[0x5555] = 0x0080; |
| 477 | s_addr[0x5555] = 0x00AA; |
| 478 | s_addr[0x2AAA] = 0x0055; |
| 479 | s_sect_addr[0] = 0x0030; |
| 480 | #else |
| 481 | addr[0x5555] = 0x00AA00AA; |
| 482 | addr[0x2AAA] = 0x00550055; |
| 483 | addr[0x5555] = 0x00800080; |
| 484 | addr[0x5555] = 0x00AA00AA; |
| 485 | addr[0x2AAA] = 0x00550055; |
| 486 | sect_addr[0] = 0x00300030; |
| 487 | #endif |
| 488 | /* re-enable interrupts if necessary */ |
| 489 | if (flag) |
| 490 | enable_interrupts(); |
| 491 | |
| 492 | /* wait at least 80us - let's wait 1 ms */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 493 | udelay(1000); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 494 | |
| 495 | #ifdef CONFIG_FLASH_16BIT |
| 496 | while ((s_sect_addr[0] & 0x0080) != 0x0080) { |
| 497 | #else |
| 498 | while ((sect_addr[0] & 0x00800080) != 0x00800080) { |
| 499 | #endif |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 500 | now = get_timer(start); |
| 501 | if (now > CONFIG_SYS_FLASH_ERASE_TOUT) { |
| 502 | printf("Timeout\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 503 | return 1; |
| 504 | } |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 505 | /* show every second that we're waiting */ |
| 506 | if ((now - last) > 1000) { |
| 507 | putc('.'); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 508 | last = now; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /* reset to read mode */ |
| 515 | addr = (volatile unsigned long *)info->start[0]; |
| 516 | #ifdef CONFIG_FLASH_16BIT |
| 517 | s_addr[0] = 0x00F0; /* reset bank */ |
| 518 | #else |
| 519 | addr[0] = 0x00F000F0; /* reset bank */ |
| 520 | #endif |
| 521 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 522 | printf(" done\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | /*----------------------------------------------------------------------- |
| 527 | * Copy memory to flash, returns: |
| 528 | * 0 - OK |
| 529 | * 1 - write timeout |
| 530 | * 2 - Flash not erased |
| 531 | * 4 - Flash not identified |
| 532 | */ |
| 533 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 534 | int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 535 | { |
| 536 | ulong cp, wp, data; |
| 537 | int i, l, rc; |
| 538 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 539 | if (info->flash_id == FLASH_UNKNOWN) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 540 | return 4; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 541 | |
| 542 | wp = (addr & ~3); /* get lower word aligned address */ |
| 543 | |
| 544 | /* |
| 545 | * handle unaligned start bytes |
| 546 | */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 547 | l = addr - wp; |
| 548 | |
| 549 | if (l != 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 550 | data = 0; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 551 | for (i = 0, cp = wp; i < l; ++i, ++cp) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 552 | data = (data << 8) | (*(uchar *)cp); |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 553 | |
| 554 | for (; i < 4 && cnt > 0; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 555 | data = (data << 8) | *src++; |
| 556 | --cnt; |
| 557 | ++cp; |
| 558 | } |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 559 | for (; cnt == 0 && i < 4; ++i, ++cp) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 560 | data = (data << 8) | (*(uchar *)cp); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 561 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 562 | rc = write_word(info, wp, data); |
| 563 | |
| 564 | if (rc != 0) |
| 565 | return rc; |
| 566 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 567 | wp += 4; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * handle word aligned part |
| 572 | */ |
| 573 | while (cnt >= 4) { |
| 574 | data = 0; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 575 | for (i = 0; i < 4; ++i) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 576 | data = (data << 8) | *src++; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 577 | |
| 578 | rc = write_word(info, wp, data); |
| 579 | if (rc != 0) |
| 580 | return rc; |
| 581 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 582 | wp += 4; |
| 583 | cnt -= 4; |
| 584 | } |
| 585 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 586 | if (cnt == 0) |
| 587 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 588 | |
| 589 | /* |
| 590 | * handle unaligned tail bytes |
| 591 | */ |
| 592 | data = 0; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 593 | for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 594 | data = (data << 8) | *src++; |
| 595 | --cnt; |
| 596 | } |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 597 | for (; i < 4; ++i, ++cp) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 598 | data = (data << 8) | (*(uchar *)cp); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 599 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 600 | return write_word(info, wp, data); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /*----------------------------------------------------------------------- |
| 604 | * Write a word to Flash, returns: |
| 605 | * 0 - OK |
| 606 | * 1 - write timeout |
| 607 | * 2 - Flash not erased |
| 608 | */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 609 | static int write_word(flash_info_t *info, ulong dest, ulong data) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 610 | { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 611 | vu_long *addr = (vu_long *)(info->start[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 612 | |
| 613 | #ifdef CONFIG_FLASH_16BIT |
| 614 | vu_short high_data; |
| 615 | vu_short low_data; |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 616 | vu_short *s_addr = (vu_short *)addr; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 617 | #endif |
| 618 | ulong start; |
| 619 | int flag; |
| 620 | |
| 621 | /* Check if Flash is (sufficiently) erased */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 622 | if ((*((vu_long *)dest) & data) != data) |
| 623 | return 2; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 624 | |
| 625 | #ifdef CONFIG_FLASH_16BIT |
| 626 | /* Write the 16 higher-bits */ |
| 627 | /* Disable interrupts which might cause a timeout here */ |
| 628 | flag = disable_interrupts(); |
| 629 | |
| 630 | high_data = ((data>>16) & 0x0000ffff); |
| 631 | |
| 632 | s_addr[0x5555] = 0x00AA; |
| 633 | s_addr[0x2AAA] = 0x0055; |
| 634 | s_addr[0x5555] = 0x00A0; |
| 635 | |
| 636 | *((vu_short *)dest) = high_data; |
| 637 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 638 | /* re-enable interrupts if necessary */ |
| 639 | if (flag) |
| 640 | enable_interrupts(); |
| 641 | |
| 642 | /* data polling for D7 */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 643 | start = get_timer(0); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 644 | while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) { |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 645 | if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) |
| 646 | return 1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 647 | } |
| 648 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 649 | /* Write the 16 lower-bits */ |
| 650 | #endif |
| 651 | |
| 652 | /* Disable interrupts which might cause a timeout here */ |
| 653 | flag = disable_interrupts(); |
| 654 | #ifdef CONFIG_FLASH_16BIT |
| 655 | dest += 0x2; |
| 656 | low_data = (data & 0x0000ffff); |
| 657 | |
| 658 | s_addr[0x5555] = 0x00AA; |
| 659 | s_addr[0x2AAA] = 0x0055; |
| 660 | s_addr[0x5555] = 0x00A0; |
| 661 | *((vu_short *)dest) = low_data; |
| 662 | |
| 663 | #else |
| 664 | addr[0x5555] = 0x00AA00AA; |
| 665 | addr[0x2AAA] = 0x00550055; |
| 666 | addr[0x5555] = 0x00A000A0; |
| 667 | *((vu_long *)dest) = data; |
| 668 | #endif |
| 669 | |
| 670 | /* re-enable interrupts if necessary */ |
| 671 | if (flag) |
| 672 | enable_interrupts(); |
| 673 | |
| 674 | /* data polling for D7 */ |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 675 | start = get_timer(0); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 676 | |
| 677 | #ifdef CONFIG_FLASH_16BIT |
| 678 | while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) { |
| 679 | #else |
| 680 | while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) { |
| 681 | #endif |
| 682 | |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 683 | if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) |
| 684 | return 1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 685 | } |
Wolfgang Denk | 8220f7e | 2011-11-04 15:55:38 +0000 | [diff] [blame] | 686 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 687 | } |