wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001-2003 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <mpc824x.h> |
| 10 | #include <asm/processor.h> |
| 11 | |
Jean-Christophe PLAGNIOL-VILLARD | 53db4cd | 2008-09-10 22:48:04 +0200 | [diff] [blame] | 12 | #if defined(CONFIG_ENV_IS_IN_FLASH) |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 13 | # ifndef CONFIG_ENV_ADDR |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 14 | # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET) |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 15 | # endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 16 | # ifndef CONFIG_ENV_SIZE |
| 17 | # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 18 | # endif |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 19 | # ifndef CONFIG_ENV_SECT_SIZE |
| 20 | # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 21 | # endif |
| 22 | #endif |
| 23 | |
| 24 | #define FLASH_BANK_SIZE 0x800000 |
| 25 | #define MAIN_SECT_SIZE 0x40000 |
| 26 | #define PARAM_SECT_SIZE 0x8000 |
| 27 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 28 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 29 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 30 | static int write_data (flash_info_t * info, ulong dest, ulong * data); |
| 31 | static void write_via_fpu (vu_long * addr, ulong * data); |
| 32 | static __inline__ unsigned long get_msr (void); |
| 33 | static __inline__ void set_msr (unsigned long msr); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 34 | |
| 35 | /*---------------------------------------------------------------------*/ |
| 36 | #undef DEBUG_FLASH |
| 37 | |
| 38 | /*---------------------------------------------------------------------*/ |
| 39 | #ifdef DEBUG_FLASH |
| 40 | #define DEBUGF(fmt,args...) printf(fmt ,##args) |
| 41 | #else |
| 42 | #define DEBUGF(fmt,args...) |
| 43 | #endif |
| 44 | /*---------------------------------------------------------------------*/ |
| 45 | |
| 46 | /*----------------------------------------------------------------------- |
| 47 | */ |
| 48 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 49 | unsigned long flash_init (void) |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 50 | { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 51 | int i, j; |
| 52 | ulong size = 0; |
| 53 | uchar tempChar; |
| 54 | vu_long *tmpaddr; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 55 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 56 | /* Enable flash writes on CPC45 */ |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 57 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 58 | tempChar = BOARD_CTRL; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 59 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 60 | tempChar |= (B_CTRL_FWPT_1 | B_CTRL_FWRE_1); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 61 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 62 | tempChar &= ~(B_CTRL_FWPT_0 | B_CTRL_FWRE_0); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 63 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 64 | BOARD_CTRL = tempChar; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 65 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 66 | __asm__ volatile ("sync\n eieio"); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 67 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 68 | for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
| 69 | vu_long *addr = (vu_long *) (CONFIG_SYS_FLASH_BASE + i * FLASH_BANK_SIZE); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 70 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 71 | addr[0] = 0x00900090; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 72 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 73 | __asm__ volatile ("sync\n eieio"); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 74 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 75 | udelay (100); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 76 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 77 | DEBUGF ("Flash bank # %d:\n" |
| 78 | "\tManuf. ID @ 0x%08lX: 0x%08lX\n" |
| 79 | "\tDevice ID @ 0x%08lX: 0x%08lX\n", |
| 80 | i, |
| 81 | (ulong) (&addr[0]), addr[0], |
| 82 | (ulong) (&addr[2]), addr[2]); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 83 | |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 84 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 85 | if ((addr[0] == addr[1]) && (addr[0] == INTEL_MANUFACT) && |
| 86 | (addr[2] == addr[3]) && (addr[2] == INTEL_ID_28F160F3T)) { |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 87 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 88 | flash_info[i].flash_id = |
| 89 | (FLASH_MAN_INTEL & FLASH_VENDMASK) | |
| 90 | (INTEL_ID_28F160F3T & FLASH_TYPEMASK); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 91 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 92 | } else if ((addr[0] == addr[1]) && (addr[0] == INTEL_MANUFACT) |
| 93 | && (addr[2] == addr[3]) |
| 94 | && (addr[2] == INTEL_ID_28F160C3T)) { |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 95 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 96 | flash_info[i].flash_id = |
| 97 | (FLASH_MAN_INTEL & FLASH_VENDMASK) | |
| 98 | (INTEL_ID_28F160C3T & FLASH_TYPEMASK); |
| 99 | |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 100 | } else { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 101 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 102 | addr[0] = 0xFFFFFFFF; |
| 103 | goto Done; |
| 104 | } |
| 105 | |
| 106 | DEBUGF ("flash_id = 0x%08lX\n", flash_info[i].flash_id); |
| 107 | |
| 108 | addr[0] = 0xFFFFFFFF; |
| 109 | |
| 110 | flash_info[i].size = FLASH_BANK_SIZE; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 111 | flash_info[i].sector_count = CONFIG_SYS_MAX_FLASH_SECT; |
| 112 | memset (flash_info[i].protect, 0, CONFIG_SYS_MAX_FLASH_SECT); |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 113 | for (j = 0; j < flash_info[i].sector_count; j++) { |
| 114 | if (j > 30) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 115 | flash_info[i].start[j] = CONFIG_SYS_FLASH_BASE + |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 116 | i * FLASH_BANK_SIZE + |
| 117 | (MAIN_SECT_SIZE * 31) + (j - |
| 118 | 31) * |
| 119 | PARAM_SECT_SIZE; |
| 120 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 121 | flash_info[i].start[j] = CONFIG_SYS_FLASH_BASE + |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 122 | i * FLASH_BANK_SIZE + |
| 123 | j * MAIN_SECT_SIZE; |
| 124 | } |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 125 | } |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 126 | |
| 127 | /* unlock sectors, if 160C3T */ |
| 128 | |
| 129 | for (j = 0; j < flash_info[i].sector_count; j++) { |
| 130 | tmpaddr = (vu_long *) flash_info[i].start[j]; |
| 131 | |
| 132 | if ((flash_info[i].flash_id & FLASH_TYPEMASK) == |
| 133 | (INTEL_ID_28F160C3T & FLASH_TYPEMASK)) { |
| 134 | tmpaddr[0] = 0x00600060; |
| 135 | tmpaddr[0] = 0x00D000D0; |
| 136 | tmpaddr[1] = 0x00600060; |
| 137 | tmpaddr[1] = 0x00D000D0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | size += flash_info[i].size; |
| 142 | |
| 143 | addr[0] = 0x00FF00FF; |
| 144 | addr[1] = 0x00FF00FF; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 145 | } |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 146 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 147 | /* Protect monitor and environment sectors |
| 148 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 149 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE + FLASH_BANK_SIZE |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 150 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 151 | CONFIG_SYS_MONITOR_BASE, |
| 152 | CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 153 | &flash_info[1]); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 154 | #else |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 155 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 156 | CONFIG_SYS_MONITOR_BASE, |
| 157 | CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1, |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 158 | &flash_info[0]); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 159 | #endif |
| 160 | |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 161 | #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 162 | #if CONFIG_ENV_ADDR >= CONFIG_SYS_FLASH_BASE + FLASH_BANK_SIZE |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 163 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 164 | CONFIG_ENV_ADDR, |
| 165 | CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[1]); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 166 | #else |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 167 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 168 | CONFIG_ENV_ADDR, |
| 169 | CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 170 | #endif |
| 171 | #endif |
| 172 | |
| 173 | Done: |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 174 | return size; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /*----------------------------------------------------------------------- |
| 178 | */ |
| 179 | void flash_print_info (flash_info_t * info) |
| 180 | { |
| 181 | int i; |
| 182 | |
| 183 | switch ((i = info->flash_id & FLASH_VENDMASK)) { |
| 184 | case (FLASH_MAN_INTEL & FLASH_VENDMASK): |
| 185 | printf ("Intel: "); |
| 186 | break; |
| 187 | default: |
| 188 | printf ("Unknown Vendor 0x%04x ", i); |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | switch ((i = info->flash_id & FLASH_TYPEMASK)) { |
| 193 | case (INTEL_ID_28F160F3T & FLASH_TYPEMASK): |
| 194 | printf ("28F160F3T (16Mbit)\n"); |
| 195 | break; |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 196 | |
| 197 | case (INTEL_ID_28F160C3T & FLASH_TYPEMASK): |
| 198 | printf ("28F160C3T (16Mbit)\n"); |
| 199 | break; |
| 200 | |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 201 | default: |
| 202 | printf ("Unknown Chip Type 0x%04x\n", i); |
| 203 | goto Done; |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | printf (" Size: %ld MB in %d Sectors\n", |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 208 | info->size >> 20, info->sector_count); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 209 | |
| 210 | printf (" Sector Start Addresses:"); |
| 211 | for (i = 0; i < info->sector_count; i++) { |
| 212 | if ((i % 5) == 0) { |
| 213 | printf ("\n "); |
| 214 | } |
| 215 | printf (" %08lX%s", info->start[i], |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 216 | info->protect[i] ? " (RO)" : " "); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 217 | } |
| 218 | printf ("\n"); |
| 219 | |
| 220 | Done: |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | /*----------------------------------------------------------------------- |
| 225 | */ |
| 226 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 227 | int flash_erase (flash_info_t * info, int s_first, int s_last) |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 228 | { |
| 229 | int flag, prot, sect; |
| 230 | ulong start, now, last; |
| 231 | |
| 232 | DEBUGF ("Erase flash bank %d sect %d ... %d\n", |
| 233 | info - &flash_info[0], s_first, s_last); |
| 234 | |
| 235 | if ((s_first < 0) || (s_first > s_last)) { |
| 236 | if (info->flash_id == FLASH_UNKNOWN) { |
| 237 | printf ("- missing\n"); |
| 238 | } else { |
| 239 | printf ("- no sectors to erase\n"); |
| 240 | } |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | if ((info->flash_id & FLASH_VENDMASK) != |
| 245 | (FLASH_MAN_INTEL & FLASH_VENDMASK)) { |
| 246 | printf ("Can erase only Intel flash types - aborted\n"); |
| 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | prot = 0; |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 251 | for (sect = s_first; sect <= s_last; ++sect) { |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 252 | if (info->protect[sect]) { |
| 253 | prot++; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (prot) { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 258 | printf ("- Warning: %d protected sectors will not be erased!\n", prot); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 259 | } else { |
| 260 | printf ("\n"); |
| 261 | } |
| 262 | |
| 263 | start = get_timer (0); |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 264 | last = start; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 265 | /* Start erase on unprotected sectors */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 266 | for (sect = s_first; sect <= s_last; sect++) { |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 267 | if (info->protect[sect] == 0) { /* not protected */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 268 | vu_long *addr = (vu_long *) (info->start[sect]); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 269 | |
| 270 | DEBUGF ("Erase sect %d @ 0x%08lX\n", |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 271 | sect, (ulong) addr); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 272 | |
| 273 | /* Disable interrupts which might cause a timeout |
| 274 | * here. |
| 275 | */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 276 | flag = disable_interrupts (); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 277 | |
| 278 | addr[0] = 0x00500050; /* clear status register */ |
| 279 | addr[0] = 0x00200020; /* erase setup */ |
| 280 | addr[0] = 0x00D000D0; /* erase confirm */ |
| 281 | |
| 282 | addr[1] = 0x00500050; /* clear status register */ |
| 283 | addr[1] = 0x00200020; /* erase setup */ |
| 284 | addr[1] = 0x00D000D0; /* erase confirm */ |
| 285 | |
| 286 | /* re-enable interrupts if necessary */ |
| 287 | if (flag) |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 288 | enable_interrupts (); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 289 | |
| 290 | /* wait at least 80us - let's wait 1 ms */ |
| 291 | udelay (1000); |
| 292 | |
| 293 | while (((addr[0] & 0x00800080) != 0x00800080) || |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 294 | ((addr[1] & 0x00800080) != 0x00800080)) { |
| 295 | if ((now = get_timer (start)) > |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 296 | CONFIG_SYS_FLASH_ERASE_TOUT) { |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 297 | printf ("Timeout\n"); |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 298 | addr[0] = 0x00B000B0; /* suspend erase */ |
| 299 | addr[0] = 0x00FF00FF; /* to read mode */ |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 300 | return 1; |
| 301 | } |
| 302 | |
| 303 | /* show that we're waiting */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 304 | if ((now - last) > 1000) { /* every second */ |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 305 | putc ('.'); |
| 306 | last = now; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | addr[0] = 0x00FF00FF; |
| 311 | } |
| 312 | } |
| 313 | printf (" done\n"); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /*----------------------------------------------------------------------- |
| 318 | * Copy memory to flash, returns: |
| 319 | * 0 - OK |
| 320 | * 1 - write timeout |
| 321 | * 2 - Flash not erased |
| 322 | * 4 - Flash not identified |
| 323 | */ |
| 324 | |
| 325 | #define FLASH_WIDTH 8 /* flash bus width in bytes */ |
| 326 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 327 | int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt) |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 328 | { |
| 329 | ulong wp, cp, msr; |
| 330 | int l, rc, i; |
| 331 | ulong data[2]; |
| 332 | ulong *datah = &data[0]; |
| 333 | ulong *datal = &data[1]; |
| 334 | |
| 335 | DEBUGF ("Flash write_buff: @ 0x%08lx, src 0x%08lx len %ld\n", |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 336 | addr, (ulong) src, cnt); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 337 | |
| 338 | if (info->flash_id == FLASH_UNKNOWN) { |
| 339 | return 4; |
| 340 | } |
| 341 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 342 | msr = get_msr (); |
| 343 | set_msr (msr | MSR_FP); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 344 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 345 | wp = (addr & ~(FLASH_WIDTH - 1)); /* get lower aligned address */ |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * handle unaligned start bytes |
| 349 | */ |
| 350 | if ((l = addr - wp) != 0) { |
| 351 | *datah = *datal = 0; |
| 352 | |
| 353 | for (i = 0, cp = wp; i < l; i++, cp++) { |
| 354 | if (i >= 4) { |
| 355 | *datah = (*datah << 8) | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 356 | ((*datal & 0xFF000000) >> 24); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 357 | } |
| 358 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 359 | *datal = (*datal << 8) | (*(uchar *) cp); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 360 | } |
| 361 | for (; i < FLASH_WIDTH && cnt > 0; ++i) { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 362 | char tmp = *src++; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 363 | |
| 364 | if (i >= 4) { |
| 365 | *datah = (*datah << 8) | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 366 | ((*datal & 0xFF000000) >> 24); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | *datal = (*datal << 8) | tmp; |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 370 | --cnt; |
| 371 | ++cp; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | for (; cnt == 0 && i < FLASH_WIDTH; ++i, ++cp) { |
| 375 | if (i >= 4) { |
| 376 | *datah = (*datah << 8) | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 377 | ((*datal & 0xFF000000) >> 24); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 378 | } |
| 379 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 380 | *datal = (*datah << 8) | (*(uchar *) cp); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 381 | } |
| 382 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 383 | if ((rc = write_data (info, wp, data)) != 0) { |
| 384 | set_msr (msr); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 385 | return (rc); |
| 386 | } |
| 387 | |
| 388 | wp += FLASH_WIDTH; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * handle FLASH_WIDTH aligned part |
| 393 | */ |
| 394 | while (cnt >= FLASH_WIDTH) { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 395 | *datah = *(ulong *) src; |
| 396 | *datal = *(ulong *) (src + 4); |
| 397 | if ((rc = write_data (info, wp, data)) != 0) { |
| 398 | set_msr (msr); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 399 | return (rc); |
| 400 | } |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 401 | wp += FLASH_WIDTH; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 402 | cnt -= FLASH_WIDTH; |
| 403 | src += FLASH_WIDTH; |
| 404 | } |
| 405 | |
| 406 | if (cnt == 0) { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 407 | set_msr (msr); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 408 | return (0); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * handle unaligned tail bytes |
| 413 | */ |
| 414 | *datah = *datal = 0; |
| 415 | for (i = 0, cp = wp; i < FLASH_WIDTH && cnt > 0; ++i, ++cp) { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 416 | char tmp = *src++; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 417 | |
| 418 | if (i >= 4) { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 419 | *datah = (*datah << 8) | ((*datal & 0xFF000000) >> |
| 420 | 24); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | *datal = (*datal << 8) | tmp; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 424 | --cnt; |
| 425 | } |
| 426 | |
| 427 | for (; i < FLASH_WIDTH; ++i, ++cp) { |
| 428 | if (i >= 4) { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 429 | *datah = (*datah << 8) | ((*datal & 0xFF000000) >> |
| 430 | 24); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 431 | } |
| 432 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 433 | *datal = (*datal << 8) | (*(uchar *) cp); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 434 | } |
| 435 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 436 | rc = write_data (info, wp, data); |
| 437 | set_msr (msr); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 438 | |
| 439 | return (rc); |
| 440 | } |
| 441 | |
| 442 | /*----------------------------------------------------------------------- |
| 443 | * Write a word to Flash, returns: |
| 444 | * 0 - OK |
| 445 | * 1 - write timeout |
| 446 | * 2 - Flash not erased |
| 447 | */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 448 | static int write_data (flash_info_t * info, ulong dest, ulong * data) |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 449 | { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 450 | vu_long *addr = (vu_long *) dest; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 451 | ulong start; |
| 452 | int flag; |
| 453 | |
| 454 | /* Check if Flash is (sufficiently) erased */ |
| 455 | if (((addr[0] & data[0]) != data[0]) || |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 456 | ((addr[1] & data[1]) != data[1])) { |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 457 | return (2); |
| 458 | } |
| 459 | /* Disable interrupts which might cause a timeout here */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 460 | flag = disable_interrupts (); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 461 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 462 | addr[0] = 0x00400040; /* write setup */ |
| 463 | write_via_fpu (addr, data); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 464 | |
| 465 | /* re-enable interrupts if necessary */ |
| 466 | if (flag) |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 467 | enable_interrupts (); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 468 | |
| 469 | start = get_timer (0); |
| 470 | |
| 471 | while (((addr[0] & 0x00800080) != 0x00800080) || |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 472 | ((addr[1] & 0x00800080) != 0x00800080)) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 473 | if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 474 | addr[0] = 0x00FF00FF; /* restore read mode */ |
| 475 | return (1); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | addr[0] = 0x00FF00FF; /* restore read mode */ |
| 480 | |
| 481 | return (0); |
| 482 | } |
| 483 | |
| 484 | /*----------------------------------------------------------------------- |
| 485 | */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 486 | static void write_via_fpu (vu_long * addr, ulong * data) |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 487 | { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 488 | __asm__ __volatile__ ("lfd 1, 0(%0)"::"r" (data)); |
| 489 | __asm__ __volatile__ ("stfd 1, 0(%0)"::"r" (addr)); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 490 | } |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 491 | |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 492 | /*----------------------------------------------------------------------- |
| 493 | */ |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 494 | static __inline__ unsigned long get_msr (void) |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 495 | { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 496 | unsigned long msr; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 497 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 498 | __asm__ __volatile__ ("mfmsr %0":"=r" (msr):); |
| 499 | |
| 500 | return msr; |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 501 | } |
| 502 | |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 503 | static __inline__ void set_msr (unsigned long msr) |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 504 | { |
wdenk | 9e930b6 | 2004-06-19 21:19:10 +0000 | [diff] [blame] | 505 | __asm__ __volatile__ ("mtmsr %0"::"r" (msr)); |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 506 | } |