wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2002 |
| 6 | * Gregory E. Allen, gallen@arlut.utexas.edu |
| 7 | * Matthew E. Karger, karger@arlut.utexas.edu |
| 8 | * Applied Research Laboratories, The University of Texas at Austin |
| 9 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <mpc824x.h> |
| 15 | #include <asm/processor.h> |
| 16 | |
| 17 | #define ROM_CS0_START 0xFF800000 |
| 18 | #define ROM_CS1_START 0xFF000000 |
| 19 | |
Jean-Christophe PLAGNIOL-VILLARD | 5a1aceb | 2008-09-10 22:48:04 +0200 | [diff] [blame] | 20 | #if defined(CONFIG_ENV_IS_IN_FLASH) |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 21 | # ifndef CONFIG_ENV_ADDR |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 22 | # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 23 | # endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 24 | # ifndef CONFIG_ENV_SIZE |
| 25 | # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 26 | # endif |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 27 | # ifndef CONFIG_ENV_SECT_SIZE |
| 28 | # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 29 | # endif |
| 30 | #endif |
| 31 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 32 | #define FLASH_BANK_SIZE ((uint)(16 * 1024 * 1024)) /* max 16Mbyte */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 33 | #define MAIN_SECT_SIZE 0x10000 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 34 | #define SECT_SIZE_32KB 0x8000 |
| 35 | #define SECT_SIZE_8KB 0x2000 |
| 36 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 37 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 38 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 39 | static int write_word (flash_info_t * info, ulong dest, ulong data); |
| 40 | #if 0 |
| 41 | static void write_via_fpu (vu_long * addr, ulong * data); |
| 42 | #endif |
| 43 | static __inline__ unsigned long get_msr (void); |
| 44 | static __inline__ void set_msr (unsigned long msr); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 45 | |
| 46 | /*flash command address offsets*/ |
| 47 | #define ADDR0 (0x555) |
| 48 | #define ADDR1 (0xAAA) |
| 49 | #define ADDR3 (0x001) |
| 50 | |
| 51 | #define FLASH_WORD_SIZE unsigned char |
| 52 | |
| 53 | /*---------------------------------------------------------------------*/ |
| 54 | /*#define DEBUG_FLASH 1 */ |
| 55 | |
| 56 | /*---------------------------------------------------------------------*/ |
| 57 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 58 | unsigned long flash_init (void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 59 | { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 60 | int i; /* flash bank counter */ |
| 61 | int j; /* flash device sector counter */ |
| 62 | int k; /* flash size calculation loop counter */ |
| 63 | int N; /* pow(2,N) is flash size, but we don't have <math.h> */ |
| 64 | ulong total_size = 0, device_size = 1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 65 | unsigned char manuf_id, device_id; |
| 66 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 67 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
| 68 | vu_char *addr = (vu_char *) (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 69 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 70 | addr[0x555] = 0xAA; /* get manuf/device info command */ |
| 71 | addr[0x2AA] = 0x55; /* 3-cycle command */ |
| 72 | addr[0x555] = 0x90; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 73 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 74 | manuf_id = addr[0]; /* read back manuf/device info */ |
| 75 | device_id = addr[1]; |
| 76 | |
| 77 | addr[0x55] = 0x98; /* CFI command */ |
| 78 | N = addr[0x27]; /* read back device_size = pow(2,N) */ |
| 79 | |
| 80 | for (k = 0; k < N; k++) /* calculate device_size = pow(2,N) */ |
| 81 | device_size *= 2; |
| 82 | |
| 83 | flash_info[i].size = device_size; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 84 | flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 85 | |
| 86 | #if defined DEBUG_FLASH |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 87 | printf ("manuf_id = %x, device_id = %x\n", manuf_id, device_id); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 88 | #endif |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 89 | /* find out what kind of flash we are using */ |
| 90 | if ((manuf_id == (uchar) (AMD_MANUFACT)) |
| 91 | && (device_id == AMD_ID_LV033C)) { |
| 92 | flash_info[i].flash_id = |
| 93 | ((FLASH_MAN_AMD & FLASH_VENDMASK) << 16) | |
| 94 | (FLASH_AM033C & FLASH_TYPEMASK); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 95 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 96 | /* set individual sector start addresses */ |
| 97 | for (j = 0; j < flash_info[i].sector_count; j++) { |
| 98 | flash_info[i].start[j] = |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 99 | (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE + |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 100 | j * MAIN_SECT_SIZE); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | else if ((manuf_id == (uchar) (AMD_MANUFACT)) && |
| 105 | (device_id == AMD_ID_LV116DT)) { |
| 106 | flash_info[i].flash_id = |
| 107 | ((FLASH_MAN_AMD & FLASH_VENDMASK) << 16) | |
| 108 | (FLASH_AM160T & FLASH_TYPEMASK); |
| 109 | |
| 110 | /* set individual sector start addresses */ |
| 111 | for (j = 0; j < flash_info[i].sector_count; j++) { |
| 112 | flash_info[i].start[j] = |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 113 | (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE + |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 114 | j * MAIN_SECT_SIZE); |
| 115 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 116 | if (j < (CONFIG_SYS_MAX_FLASH_SECT - 3)) { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 117 | flash_info[i].start[j] = |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 118 | (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE + |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 119 | j * MAIN_SECT_SIZE); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 120 | } else if (j == (CONFIG_SYS_MAX_FLASH_SECT - 3)) { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 121 | flash_info[i].start[j] = |
| 122 | (flash_info[i].start[j - 1] + SECT_SIZE_32KB); |
| 123 | |
| 124 | } else { |
| 125 | flash_info[i].start[j] = |
| 126 | (flash_info[i].start[j - 1] + SECT_SIZE_8KB); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | else { |
| 132 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 133 | addr[0] = 0xFF; |
| 134 | goto Done; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | #if defined DEBUG_FLASH |
| 138 | printf ("flash_id = 0x%08lX\n", flash_info[i].flash_id); |
| 139 | #endif |
| 140 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 141 | addr[0] = 0xFF; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 142 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 143 | memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 144 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 145 | total_size += flash_info[i].size; |
| 146 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 147 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 148 | /* Protect monitor and environment sectors |
| 149 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 150 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE |
| 151 | flash_protect (FLAG_PROTECT_SET, CONFIG_SYS_MONITOR_BASE, |
| 152 | CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 153 | &flash_info[0]); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 156 | #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR) |
| 157 | flash_protect (FLAG_PROTECT_SET, CONFIG_ENV_ADDR, |
| 158 | CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 159 | #endif |
| 160 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 161 | Done: |
| 162 | return total_size; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /*----------------------------------------------------------------------- |
| 166 | */ |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 167 | void flash_print_info (flash_info_t * info) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 168 | { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 169 | static const char unk[] = "Unknown"; |
| 170 | const char *mfct = unk, *type = unk; |
| 171 | unsigned int i; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 172 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 173 | if (info->flash_id != FLASH_UNKNOWN) { |
| 174 | switch (info->flash_id & FLASH_VENDMASK) { |
| 175 | case FLASH_MAN_AMD: |
| 176 | mfct = "AMD"; |
| 177 | break; |
| 178 | case FLASH_MAN_FUJ: |
| 179 | mfct = "FUJITSU"; |
| 180 | break; |
| 181 | case FLASH_MAN_STM: |
| 182 | mfct = "STM"; |
| 183 | break; |
| 184 | case FLASH_MAN_SST: |
| 185 | mfct = "SST"; |
| 186 | break; |
| 187 | case FLASH_MAN_BM: |
| 188 | mfct = "Bright Microelectonics"; |
| 189 | break; |
| 190 | case FLASH_MAN_INTEL: |
| 191 | mfct = "Intel"; |
| 192 | break; |
| 193 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 194 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 195 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 196 | case FLASH_AM033C: |
| 197 | type = "AM29LV033C (32 Mbit, uniform sector size)"; |
| 198 | break; |
| 199 | case FLASH_AM160T: |
| 200 | type = "AM29LV160T (16 Mbit, top boot sector)"; |
| 201 | break; |
| 202 | case FLASH_AM040: |
| 203 | type = "AM29F040B (512K * 8, uniform sector size)"; |
| 204 | break; |
| 205 | case FLASH_AM400B: |
| 206 | type = "AM29LV400B (4 Mbit, bottom boot sect)"; |
| 207 | break; |
| 208 | case FLASH_AM400T: |
| 209 | type = "AM29LV400T (4 Mbit, top boot sector)"; |
| 210 | break; |
| 211 | case FLASH_AM800B: |
| 212 | type = "AM29LV800B (8 Mbit, bottom boot sect)"; |
| 213 | break; |
| 214 | case FLASH_AM800T: |
| 215 | type = "AM29LV800T (8 Mbit, top boot sector)"; |
| 216 | break; |
| 217 | case FLASH_AM320B: |
| 218 | type = "AM29LV320B (32 Mbit, bottom boot sect)"; |
| 219 | break; |
| 220 | case FLASH_AM320T: |
| 221 | type = "AM29LV320T (32 Mbit, top boot sector)"; |
| 222 | break; |
| 223 | case FLASH_STM800AB: |
| 224 | type = "M29W800AB (8 Mbit, bottom boot sect)"; |
| 225 | break; |
| 226 | case FLASH_SST800A: |
| 227 | type = "SST39LF/VF800 (8 Mbit, uniform sector size)"; |
| 228 | break; |
| 229 | case FLASH_SST160A: |
| 230 | type = "SST39LF/VF160 (16 Mbit, uniform sector size)"; |
| 231 | break; |
| 232 | } |
| 233 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 234 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 235 | printf ("\n Brand: %s Type: %s\n" |
| 236 | " Size: %lu KB in %d Sectors\n", |
| 237 | mfct, type, info->size >> 10, info->sector_count); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 238 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 239 | printf (" Sector Start Addresses:"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 240 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 241 | for (i = 0; i < info->sector_count; i++) { |
| 242 | unsigned long size; |
| 243 | unsigned int erased; |
| 244 | unsigned long *flash = (unsigned long *) info->start[i]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 245 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 246 | /* |
| 247 | * Check if whole sector is erased |
| 248 | */ |
| 249 | size = (i != (info->sector_count - 1)) ? |
| 250 | (info->start[i + 1] - info->start[i]) >> 2 : |
| 251 | (info->start[0] + info->size - info->start[i]) >> 2; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 252 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 253 | for (flash = (unsigned long *) info->start[i], erased = 1; |
| 254 | (flash != (unsigned long *) info->start[i] + size) && erased; |
| 255 | flash++) |
| 256 | erased = *flash == ~0x0UL; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 257 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 258 | printf ("%s %08lX %s %s", |
| 259 | (i % 5) ? "" : "\n ", |
| 260 | info->start[i], |
| 261 | erased ? "E" : " ", info->protect[i] ? "RO" : " "); |
| 262 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 263 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 264 | puts ("\n"); |
| 265 | return; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /*----------------------------------------------------------------------- |
| 269 | */ |
| 270 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 271 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 272 | { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 273 | volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]); |
| 274 | int flag, prot, sect, l_sect; |
| 275 | ulong start, now, last; |
| 276 | unsigned char sh8b; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 277 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 278 | if ((s_first < 0) || (s_first > s_last)) { |
| 279 | if (info->flash_id == FLASH_UNKNOWN) { |
| 280 | printf ("- missing\n"); |
| 281 | } else { |
| 282 | printf ("- no sectors to erase\n"); |
| 283 | } |
| 284 | return 1; |
| 285 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 286 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 287 | if ((info->flash_id == FLASH_UNKNOWN) || |
| 288 | (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) { |
| 289 | printf ("Can't erase unknown flash type - aborted\n"); |
| 290 | return 1; |
| 291 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 292 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 293 | prot = 0; |
| 294 | for (sect = s_first; sect <= s_last; ++sect) { |
| 295 | if (info->protect[sect]) { |
| 296 | prot++; |
| 297 | } |
| 298 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 299 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 300 | if (prot) { |
| 301 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 302 | prot); |
| 303 | } else { |
| 304 | printf ("\n"); |
| 305 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 306 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 307 | l_sect = -1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 308 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 309 | /* Check the ROM CS */ |
| 310 | if ((info->start[0] >= ROM_CS1_START) |
| 311 | && (info->start[0] < ROM_CS0_START)) |
| 312 | sh8b = 3; |
| 313 | else |
| 314 | sh8b = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 315 | |
| 316 | /* Disable interrupts which might cause a timeout here */ |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 317 | flag = disable_interrupts (); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 318 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 319 | addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA; |
| 320 | addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055; |
| 321 | addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080; |
| 322 | addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA; |
| 323 | addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 324 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 325 | /* Start erase on unprotected sectors */ |
| 326 | for (sect = s_first; sect <= s_last; sect++) { |
| 327 | if (info->protect[sect] == 0) { /* not protected */ |
| 328 | addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info-> |
| 329 | start[sect] - |
| 330 | info-> |
| 331 | start[0]) << |
| 332 | sh8b)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 333 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 334 | if (info->flash_id & FLASH_MAN_SST) { |
| 335 | addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA; |
| 336 | addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055; |
| 337 | addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00800080; |
| 338 | addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA; |
| 339 | addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055; |
| 340 | addr[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */ |
| 341 | udelay (30000); /* wait 30 ms */ |
| 342 | } else { |
| 343 | addr[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 344 | } |
| 345 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 346 | l_sect = sect; |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 349 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 350 | /* re-enable interrupts if necessary */ |
| 351 | if (flag) |
| 352 | enable_interrupts (); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 353 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 354 | /* wait at least 80us - let's wait 1 ms */ |
| 355 | udelay (1000); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 356 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 357 | /* |
| 358 | * We wait for the last triggered sector |
| 359 | */ |
| 360 | if (l_sect < 0) |
| 361 | goto DONE; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 362 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 363 | start = get_timer (0); |
| 364 | last = start; |
| 365 | addr = (FLASH_WORD_SIZE *) (info->start[0] + ((info->start[l_sect] - |
| 366 | info-> |
| 367 | start[0]) << sh8b)); |
| 368 | while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) != |
| 369 | (FLASH_WORD_SIZE) 0x00800080) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 370 | if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 371 | printf ("Timeout\n"); |
| 372 | return 1; |
| 373 | } |
| 374 | /* show that we're waiting */ |
| 375 | if ((now - last) > 1000) { /* every second */ |
| 376 | serial_putc ('.'); |
| 377 | last = now; |
| 378 | } |
| 379 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 380 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 381 | DONE: |
| 382 | /* reset to read mode */ |
| 383 | addr = (FLASH_WORD_SIZE *) info->start[0]; |
| 384 | addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 385 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 386 | printf (" done\n"); |
| 387 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | |
| 391 | /*----------------------------------------------------------------------- |
| 392 | * Copy memory to flash, returns: |
| 393 | * 0 - OK |
| 394 | * 1 - write timeout |
| 395 | * 2 - Flash not erased |
| 396 | */ |
| 397 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 398 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 399 | { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 400 | ulong cp, wp, data; |
| 401 | int i, l, rc; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 402 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 403 | wp = (addr & ~3); /* get lower word aligned address */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 404 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 405 | /* |
| 406 | * handle unaligned start bytes |
| 407 | */ |
| 408 | if ((l = addr - wp) != 0) { |
| 409 | data = 0; |
| 410 | for (i = 0, cp = wp; i < l; ++i, ++cp) { |
| 411 | data = (data << 8) | (*(uchar *) cp); |
| 412 | } |
| 413 | for (; i < 4 && cnt > 0; ++i) { |
| 414 | data = (data << 8) | *src++; |
| 415 | --cnt; |
| 416 | ++cp; |
| 417 | } |
| 418 | for (; cnt == 0 && i < 4; ++i, ++cp) { |
| 419 | data = (data << 8) | (*(uchar *) cp); |
| 420 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 421 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 422 | if ((rc = write_word (info, wp, data)) != 0) { |
| 423 | return (rc); |
| 424 | } |
| 425 | wp += 4; |
| 426 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 427 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 428 | /* |
| 429 | * handle word aligned part |
| 430 | */ |
| 431 | while (cnt >= 4) { |
| 432 | data = 0; |
| 433 | for (i = 0; i < 4; ++i) { |
| 434 | data = (data << 8) | *src++; |
| 435 | } |
| 436 | if ((rc = write_word (info, wp, data)) != 0) { |
| 437 | return (rc); |
| 438 | } |
| 439 | wp += 4; |
| 440 | cnt -= 4; |
| 441 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 442 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 443 | if (cnt == 0) { |
| 444 | return (0); |
| 445 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 446 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 447 | /* |
| 448 | * handle unaligned tail bytes |
| 449 | */ |
| 450 | data = 0; |
| 451 | for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) { |
| 452 | data = (data << 8) | *src++; |
| 453 | --cnt; |
| 454 | } |
| 455 | for (; i < 4; ++i, ++cp) { |
| 456 | data = (data << 8) | (*(uchar *) cp); |
| 457 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 458 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 459 | return (write_word (info, wp, data)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
| 463 | /*----------------------------------------------------------------------- |
| 464 | * Write a word to Flash, returns: |
| 465 | * 0 - OK |
| 466 | * 1 - write timeout |
| 467 | * 2 - Flash not erased |
| 468 | */ |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 469 | static int write_word (flash_info_t * info, ulong dest, ulong data) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 470 | { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 471 | volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) info->start[0]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 472 | volatile FLASH_WORD_SIZE *dest2; |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 473 | volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 474 | ulong start; |
| 475 | int flag; |
| 476 | int i; |
| 477 | unsigned char sh8b; |
| 478 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 479 | /* Check the ROM CS */ |
| 480 | if ((info->start[0] >= ROM_CS1_START) |
| 481 | && (info->start[0] < ROM_CS0_START)) |
| 482 | sh8b = 3; |
| 483 | else |
| 484 | sh8b = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 485 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 486 | dest2 = (FLASH_WORD_SIZE *) (((dest - info->start[0]) << sh8b) + |
| 487 | info->start[0]); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 488 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 489 | /* Check if Flash is (sufficiently) erased */ |
| 490 | if ((*dest2 & (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) { |
| 491 | return (2); |
| 492 | } |
| 493 | /* Disable interrupts which might cause a timeout here */ |
| 494 | flag = disable_interrupts (); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 495 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 496 | for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) { |
| 497 | addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00AA00AA; |
| 498 | addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE) 0x00550055; |
| 499 | addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE) 0x00A000A0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 500 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 501 | dest2[i << sh8b] = data2[i]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 502 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 503 | /* re-enable interrupts if necessary */ |
| 504 | if (flag) |
| 505 | enable_interrupts (); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 506 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 507 | /* data polling for D7 */ |
| 508 | start = get_timer (0); |
| 509 | while ((dest2[i << sh8b] & (FLASH_WORD_SIZE) 0x00800080) != |
| 510 | (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 511 | if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 512 | return (1); |
| 513 | } |
| 514 | } |
| 515 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 516 | |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 517 | return (0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 518 | } |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 519 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 520 | /*----------------------------------------------------------------------- |
| 521 | */ |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 522 | #if 0 |
| 523 | static void write_via_fpu (vu_long * addr, ulong * data) |
| 524 | { |
| 525 | __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data)); |
| 526 | __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr)); |
| 527 | } |
| 528 | #endif |
| 529 | |
| 530 | /*----------------------------------------------------------------------- |
| 531 | */ |
| 532 | static __inline__ unsigned long get_msr (void) |
| 533 | { |
| 534 | unsigned long msr; |
| 535 | |
| 536 | __asm__ __volatile__ ("mfmsr %0":"=r" (msr):); |
| 537 | |
| 538 | return msr; |
| 539 | } |
| 540 | |
| 541 | static __inline__ void set_msr (unsigned long msr) |
| 542 | { |
| 543 | __asm__ __volatile__ ("mtmsr %0"::"r" (msr)); |
| 544 | } |