wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | **===================================================================== |
| 3 | ** |
| 4 | ** Copyright (C) 2000, 2001, 2002, 2003 |
| 5 | ** The LEOX team <team@leox.org>, http://www.leox.org |
| 6 | ** |
| 7 | ** LEOX.org is about the development of free hardware and software resources |
| 8 | ** for system on chip. |
| 9 | ** |
| 10 | ** Description: U-Boot port on the LEOX's ELPT860 CPU board |
| 11 | ** ~~~~~~~~~~~ |
| 12 | ** |
| 13 | **===================================================================== |
| 14 | ** |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 15 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 16 | ** |
| 17 | **===================================================================== |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | ** Note 1: In this file, you have to provide the following variable: |
| 22 | ** ------ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 23 | ** flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS] |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 24 | ** 'flash_info_t' structure is defined into 'include/flash.h' |
| 25 | ** and defined as extern into 'common/cmd_flash.c' |
| 26 | ** |
| 27 | ** Note 2: In this file, you have to provide the following functions: |
| 28 | ** ------ |
| 29 | ** unsigned long flash_init(void) |
| 30 | ** called from 'board_init_r()' into 'common/board.c' |
| 31 | ** |
| 32 | ** void flash_print_info(flash_info_t *info) |
| 33 | ** called from 'do_flinfo()' into 'common/cmd_flash.c' |
| 34 | ** |
| 35 | ** int flash_erase(flash_info_t *info, |
| 36 | ** int s_first, |
| 37 | ** int s_last) |
| 38 | ** called from 'do_flerase()' & 'flash_sect_erase()' into 'common/cmd_flash.c' |
| 39 | ** |
| 40 | ** int write_buff (flash_info_t *info, |
| 41 | ** uchar *src, |
| 42 | ** ulong addr, |
| 43 | ** ulong cnt) |
| 44 | ** called from 'flash_write()' into 'common/cmd_flash.c' |
| 45 | */ |
| 46 | |
| 47 | #include <common.h> |
| 48 | #include <mpc8xx.h> |
| 49 | |
| 50 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 51 | #ifndef CONFIG_ENV_ADDR |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 52 | # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 55 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 56 | |
| 57 | /*----------------------------------------------------------------------- |
| 58 | * Internal Functions |
| 59 | */ |
| 60 | static void flash_get_offsets (ulong base, flash_info_t *info); |
| 61 | static ulong flash_get_size (volatile unsigned char *addr, flash_info_t *info); |
| 62 | |
| 63 | static int write_word (flash_info_t *info, ulong dest, ulong data); |
| 64 | static int write_byte (flash_info_t *info, ulong dest, uchar data); |
| 65 | |
| 66 | /*----------------------------------------------------------------------- |
| 67 | */ |
| 68 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 69 | unsigned long |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 70 | flash_init (void) |
| 71 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 72 | volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 73 | volatile memctl8xx_t *memctl = &immap->im_memctl; |
| 74 | unsigned long size_b0; |
| 75 | int i; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 76 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 77 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 78 | for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 79 | { |
| 80 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 81 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 82 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 83 | /* Static FLASH Bank configuration here - FIXME XXX */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 84 | |
| 85 | size_b0 = flash_get_size ((volatile unsigned char *)FLASH_BASE0_PRELIM, |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 86 | &flash_info[0]); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 87 | |
| 88 | if ( flash_info[0].flash_id == FLASH_UNKNOWN ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 89 | { |
| 90 | printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", |
| 91 | size_b0, size_b0<<20); |
| 92 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 93 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 94 | /* Remap FLASH according to real size */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 95 | memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK); |
| 96 | memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_PS_8 | BR_V; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 97 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 98 | /* Re-do sizing to get full correct info */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 99 | size_b0 = flash_get_size ((volatile unsigned char *)CONFIG_SYS_FLASH_BASE, |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 100 | &flash_info[0]); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 101 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 102 | flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 103 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 104 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 105 | /* monitor protection ON by default */ |
| 106 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 107 | CONFIG_SYS_MONITOR_BASE, |
| 108 | CONFIG_SYS_MONITOR_BASE + monitor_flash_len-1, |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 109 | &flash_info[0]); |
| 110 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 111 | |
Jean-Christophe PLAGNIOL-VILLARD | 5a1aceb | 2008-09-10 22:48:04 +0200 | [diff] [blame] | 112 | #ifdef CONFIG_ENV_IS_IN_FLASH |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 113 | /* ENV protection ON by default */ |
| 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 | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 117 | &flash_info[0]); |
| 118 | #endif |
| 119 | |
| 120 | flash_info[0].size = size_b0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 121 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 122 | return (size_b0); |
| 123 | } |
| 124 | |
| 125 | /*----------------------------------------------------------------------- |
| 126 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 127 | static void |
| 128 | flash_get_offsets (ulong base, |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 129 | flash_info_t *info) |
| 130 | { |
| 131 | int i; |
| 132 | |
| 133 | #define SECTOR_64KB 0x00010000 |
| 134 | |
| 135 | /* set up sector start adress table */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 136 | for (i = 0; i < info->sector_count; i++) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 137 | { |
| 138 | info->start[i] = base + (i * SECTOR_64KB); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /*----------------------------------------------------------------------- |
| 143 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 144 | void |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 145 | flash_print_info (flash_info_t *info) |
| 146 | { |
| 147 | int i; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 148 | |
| 149 | if ( info->flash_id == FLASH_UNKNOWN ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 150 | { |
| 151 | printf ("missing or unknown FLASH type\n"); |
| 152 | return; |
| 153 | } |
| 154 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 155 | switch ( info->flash_id & FLASH_VENDMASK ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 156 | { |
| 157 | case FLASH_MAN_AMD: printf ("AMD "); break; |
| 158 | case FLASH_MAN_FUJ: printf ("FUJITSU "); break; |
| 159 | case FLASH_MAN_STM: printf ("STM (Thomson) "); break; |
| 160 | default: printf ("Unknown Vendor "); break; |
| 161 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 162 | |
| 163 | switch ( info->flash_id & FLASH_TYPEMASK ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 164 | { |
| 165 | case FLASH_AM040: printf ("AM29F040 (4 Mbits)\n"); |
| 166 | break; |
| 167 | default: printf ("Unknown Chip Type\n"); |
| 168 | break; |
| 169 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 170 | |
| 171 | printf (" Size: %ld KB in %d Sectors\n", |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 172 | info->size >> 10, info->sector_count); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 173 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 174 | printf (" Sector Start Addresses:"); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 175 | for (i=0; i<info->sector_count; ++i) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 176 | { |
| 177 | if ((i % 5) == 0) |
| 178 | printf ("\n "); |
| 179 | printf (" %08lX%s", |
| 180 | info->start[i], |
| 181 | info->protect[i] ? " (RO)" : " " |
| 182 | ); |
| 183 | } |
| 184 | printf ("\n"); |
| 185 | |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | /*----------------------------------------------------------------------- |
| 190 | */ |
| 191 | |
| 192 | |
| 193 | /*----------------------------------------------------------------------- |
| 194 | */ |
| 195 | |
| 196 | /* |
| 197 | * The following code cannot be run from FLASH! |
| 198 | */ |
| 199 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 200 | static ulong |
| 201 | flash_get_size (volatile unsigned char *addr, |
| 202 | flash_info_t *info) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 203 | { |
| 204 | short i; |
| 205 | uchar value; |
| 206 | ulong base = (ulong)addr; |
| 207 | |
| 208 | /* Write auto select command: read Manufacturer ID */ |
| 209 | addr[0x0555] = 0xAA; |
| 210 | addr[0x02AA] = 0x55; |
| 211 | addr[0x0555] = 0x90; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 212 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 213 | value = addr[0]; |
| 214 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 215 | switch ( value ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 216 | { |
| 217 | /* case AMD_MANUFACT: */ |
| 218 | case 0x01: |
| 219 | info->flash_id = FLASH_MAN_AMD; |
| 220 | break; |
| 221 | /* case FUJ_MANUFACT: */ |
| 222 | case 0x04: |
| 223 | info->flash_id = FLASH_MAN_FUJ; |
| 224 | break; |
| 225 | /* case STM_MANUFACT: */ |
| 226 | case 0x20: |
| 227 | info->flash_id = FLASH_MAN_STM; |
| 228 | break; |
| 229 | |
| 230 | default: |
| 231 | info->flash_id = FLASH_UNKNOWN; |
| 232 | info->sector_count = 0; |
| 233 | info->size = 0; |
| 234 | return (0); /* no or unknown flash */ |
| 235 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 236 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 237 | value = addr[1]; /* device ID */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 238 | |
| 239 | switch ( value ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 240 | { |
| 241 | case STM_ID_F040B: |
| 242 | case AMD_ID_F040B: |
| 243 | info->flash_id += FLASH_AM040; /* 4 Mbits = 512k * 8 */ |
| 244 | info->sector_count = 8; |
| 245 | info->size = 0x00080000; |
| 246 | break; |
| 247 | |
| 248 | default: |
| 249 | info->flash_id = FLASH_UNKNOWN; |
| 250 | return (0); /* => no or unknown flash */ |
| 251 | } |
| 252 | |
| 253 | /* set up sector start adress table */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 254 | for (i = 0; i < info->sector_count; i++) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 255 | { |
| 256 | info->start[i] = base + (i * 0x00010000); |
| 257 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 258 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 259 | /* check for protected sectors */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 260 | for (i = 0; i < info->sector_count; i++) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 261 | { |
| 262 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 263 | /* D0 = 1 if protected */ |
| 264 | addr = (volatile unsigned char *)(info->start[i]); |
| 265 | info->protect[i] = addr[2] & 1; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Prevent writes to uninitialized FLASH. |
| 270 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 271 | if ( info->flash_id != FLASH_UNKNOWN ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 272 | { |
| 273 | addr = (volatile unsigned char *)info->start[0]; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 274 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 275 | *addr = 0xF0; /* reset bank */ |
| 276 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 277 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 278 | return (info->size); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /*----------------------------------------------------------------------- |
| 283 | */ |
| 284 | |
| 285 | int |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 286 | flash_erase (flash_info_t *info, |
| 287 | int s_first, |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 288 | int s_last) |
| 289 | { |
| 290 | volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]); |
| 291 | int flag, prot, sect, l_sect; |
| 292 | ulong start, now, last; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 293 | |
| 294 | if ( (s_first < 0) || (s_first > s_last) ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 295 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 296 | if ( info->flash_id == FLASH_UNKNOWN ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 297 | { |
| 298 | printf ("- missing\n"); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 299 | } |
| 300 | else |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 301 | { |
| 302 | printf ("- no sectors to erase\n"); |
| 303 | } |
| 304 | return ( 1 ); |
| 305 | } |
| 306 | |
| 307 | if ( (info->flash_id == FLASH_UNKNOWN) || |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 308 | (info->flash_id > FLASH_AMD_COMP) ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 309 | { |
| 310 | printf ("Can't erase unknown flash type %08lx - aborted\n", |
| 311 | info->flash_id); |
| 312 | return ( 1 ); |
| 313 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 314 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 315 | prot = 0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 316 | for (sect=s_first; sect<=s_last; ++sect) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 317 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 318 | if ( info->protect[sect] ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 319 | { |
| 320 | prot++; |
| 321 | } |
| 322 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 323 | |
| 324 | if ( prot ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 325 | { |
| 326 | printf ("- Warning: %d protected sectors will not be erased!\n", prot); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 327 | } |
| 328 | else |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 329 | { |
| 330 | printf ("\n"); |
| 331 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 332 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 333 | l_sect = -1; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 334 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 335 | /* Disable interrupts which might cause a timeout here */ |
| 336 | flag = disable_interrupts(); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 337 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 338 | addr[0x0555] = 0xAA; |
| 339 | addr[0x02AA] = 0x55; |
| 340 | addr[0x0555] = 0x80; |
| 341 | addr[0x0555] = 0xAA; |
| 342 | addr[0x02AA] = 0x55; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 343 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 344 | /* Start erase on unprotected sectors */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 345 | for (sect = s_first; sect<=s_last; sect++) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 346 | { |
| 347 | if (info->protect[sect] == 0) /* not protected */ |
| 348 | { |
| 349 | addr = (volatile unsigned char *)(info->start[sect]); |
| 350 | addr[0] = 0x30; |
| 351 | l_sect = sect; |
| 352 | } |
| 353 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 354 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 355 | /* re-enable interrupts if necessary */ |
| 356 | if ( flag ) |
| 357 | enable_interrupts(); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 358 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 359 | /* wait at least 80us - let's wait 1 ms */ |
| 360 | udelay (1000); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 361 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 362 | /* |
| 363 | * We wait for the last triggered sector |
| 364 | */ |
| 365 | if ( l_sect < 0 ) |
| 366 | goto DONE; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 367 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 368 | start = get_timer (0); |
| 369 | last = start; |
| 370 | addr = (volatile unsigned char *)(info->start[l_sect]); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 371 | while ( (addr[0] & 0x80) != 0x80 ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 372 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 373 | if ( (now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 374 | { |
| 375 | printf ("Timeout\n"); |
| 376 | return ( 1 ); |
| 377 | } |
| 378 | /* show that we're waiting */ |
| 379 | if ( (now - last) > 1000 ) /* every second */ |
| 380 | { |
| 381 | putc ('.'); |
| 382 | last = now; |
| 383 | } |
| 384 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 385 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 386 | DONE: |
| 387 | /* reset to read mode */ |
| 388 | addr = (volatile unsigned char *)info->start[0]; |
| 389 | addr[0] = 0xF0; /* reset bank */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 390 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 391 | printf (" done\n"); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 392 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 393 | return ( 0 ); |
| 394 | } |
| 395 | |
| 396 | /*----------------------------------------------------------------------- |
| 397 | * Copy memory to flash, returns: |
| 398 | * 0 - OK |
| 399 | * 1 - write timeout |
| 400 | * 2 - Flash not erased |
| 401 | */ |
| 402 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 403 | int |
| 404 | write_buff (flash_info_t *info, |
| 405 | uchar *src, |
| 406 | ulong addr, |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 407 | ulong cnt) |
| 408 | { |
| 409 | ulong cp, wp, data; |
| 410 | uchar bdata; |
| 411 | int i, l, rc; |
| 412 | |
| 413 | if ( (info->flash_id & FLASH_TYPEMASK) == FLASH_AM040 ) |
| 414 | { |
| 415 | /* Width of the data bus: 8 bits */ |
| 416 | |
| 417 | wp = addr; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 418 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 419 | while ( cnt ) |
| 420 | { |
| 421 | bdata = *src++; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 422 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 423 | if ( (rc = write_byte(info, wp, bdata)) != 0 ) |
| 424 | { |
| 425 | return (rc); |
| 426 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 427 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 428 | ++wp; |
| 429 | --cnt; |
| 430 | } |
| 431 | |
| 432 | return ( 0 ); |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | /* Width of the data bus: 32 bits */ |
| 437 | |
| 438 | wp = (addr & ~3); /* get lower word aligned address */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 439 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 440 | /* |
| 441 | * handle unaligned start bytes |
| 442 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 443 | if ( (l = addr - wp) != 0 ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 444 | { |
| 445 | data = 0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 446 | for (i=0, cp=wp; i<l; ++i, ++cp) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 447 | { |
| 448 | data = (data << 8) | (*(uchar *)cp); |
| 449 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 450 | for (; i<4 && cnt>0; ++i) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 451 | { |
| 452 | data = (data << 8) | *src++; |
| 453 | --cnt; |
| 454 | ++cp; |
| 455 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 456 | for (; cnt==0 && i<4; ++i, ++cp) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 457 | { |
| 458 | data = (data << 8) | (*(uchar *)cp); |
| 459 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 460 | |
| 461 | if ( (rc = write_word(info, wp, data)) != 0 ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 462 | { |
| 463 | return (rc); |
| 464 | } |
| 465 | wp += 4; |
| 466 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 467 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 468 | /* |
| 469 | * handle word aligned part |
| 470 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 471 | while ( cnt >= 4 ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 472 | { |
| 473 | data = 0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 474 | for (i=0; i<4; ++i) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 475 | { |
| 476 | data = (data << 8) | *src++; |
| 477 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 478 | if ( (rc = write_word(info, wp, data)) != 0 ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 479 | { |
| 480 | return (rc); |
| 481 | } |
| 482 | wp += 4; |
| 483 | cnt -= 4; |
| 484 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 485 | |
| 486 | if ( cnt == 0 ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 487 | { |
| 488 | return (0); |
| 489 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 490 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 491 | /* |
| 492 | * handle unaligned tail bytes |
| 493 | */ |
| 494 | data = 0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 495 | for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 496 | { |
| 497 | data = (data << 8) | *src++; |
| 498 | --cnt; |
| 499 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 500 | for (; i<4; ++i, ++cp) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 501 | { |
| 502 | data = (data << 8) | (*(uchar *)cp); |
| 503 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 504 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 505 | return (write_word(info, wp, data)); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /*----------------------------------------------------------------------- |
| 510 | * Write a word to Flash, returns: |
| 511 | * 0 - OK |
| 512 | * 1 - write timeout |
| 513 | * 2 - Flash not erased |
| 514 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 515 | static int |
| 516 | write_word (flash_info_t *info, |
| 517 | ulong dest, |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 518 | ulong data) |
| 519 | { |
| 520 | vu_long *addr = (vu_long*)(info->start[0]); |
| 521 | ulong start; |
| 522 | int flag; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 523 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 524 | /* Check if Flash is (sufficiently) erased */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 525 | if ( (*((vu_long *)dest) & data) != data ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 526 | { |
| 527 | return (2); |
| 528 | } |
| 529 | /* Disable interrupts which might cause a timeout here */ |
| 530 | flag = disable_interrupts(); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 531 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 532 | addr[0x0555] = 0x00AA00AA; |
| 533 | addr[0x02AA] = 0x00550055; |
| 534 | addr[0x0555] = 0x00A000A0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 535 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 536 | *((vu_long *)dest) = data; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 537 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 538 | /* re-enable interrupts if necessary */ |
| 539 | if ( flag ) |
| 540 | enable_interrupts(); |
| 541 | |
| 542 | /* data polling for D7 */ |
| 543 | start = get_timer (0); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 544 | while ( (*((vu_long *)dest) & 0x00800080) != (data & 0x00800080) ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 545 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 546 | if ( get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 547 | { |
| 548 | return (1); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | return (0); |
| 553 | } |
| 554 | |
| 555 | /*----------------------------------------------------------------------- |
| 556 | * Write a byte to Flash, returns: |
| 557 | * 0 - OK |
| 558 | * 1 - write timeout |
| 559 | * 2 - Flash not erased |
| 560 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 561 | static int |
| 562 | write_byte (flash_info_t *info, |
| 563 | ulong dest, |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 564 | uchar data) |
| 565 | { |
| 566 | volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]); |
| 567 | ulong start; |
| 568 | int flag; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 569 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 570 | /* Check if Flash is (sufficiently) erased */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 571 | if ( (*((volatile unsigned char *)dest) & data) != data ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 572 | { |
| 573 | return (2); |
| 574 | } |
| 575 | /* Disable interrupts which might cause a timeout here */ |
| 576 | flag = disable_interrupts(); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 577 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 578 | addr[0x0555] = 0xAA; |
| 579 | addr[0x02AA] = 0x55; |
| 580 | addr[0x0555] = 0xA0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 581 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 582 | *((volatile unsigned char *)dest) = data; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 583 | |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 584 | /* re-enable interrupts if necessary */ |
| 585 | if ( flag ) |
| 586 | enable_interrupts(); |
| 587 | |
| 588 | /* data polling for D7 */ |
| 589 | start = get_timer (0); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 590 | while ( (*((volatile unsigned char *)dest) & 0x80) != (data & 0x80) ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 591 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 592 | if ( get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT ) |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 593 | { |
| 594 | return (1); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | return (0); |
| 599 | } |
| 600 | |
| 601 | /*----------------------------------------------------------------------- |
| 602 | */ |