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