wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001, 2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2002 |
| 6 | * Frank Panno <fpanno@delphintech.com>, Delphin Technology AG |
| 7 | * |
| 8 | * Flash Routines for AMD device AM29DL323DB on the EP8260 board. |
| 9 | * |
| 10 | * This file is based on board/tqm8260/flash.c. |
| 11 | *-------------------------------------------------------------------- |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 12 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | #include <mpc8xx.h> |
| 17 | |
| 18 | #define V_ULONG(a) (*(volatile unsigned long *)( a )) |
| 19 | #define V_BYTE(a) (*(volatile unsigned char *)( a )) |
| 20 | |
| 21 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 22 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | /*----------------------------------------------------------------------- |
| 26 | */ |
| 27 | void flash_reset(void) |
| 28 | { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 29 | if( flash_info[0].flash_id != FLASH_UNKNOWN ) { |
| 30 | V_ULONG( flash_info[0].start[0] ) = 0x00F000F0; |
| 31 | V_ULONG( flash_info[0].start[0] + 4 ) = 0x00F000F0; |
| 32 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /*----------------------------------------------------------------------- |
| 36 | */ |
| 37 | ulong flash_get_size( ulong baseaddr, flash_info_t *info ) |
| 38 | { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 39 | short i; |
| 40 | unsigned long flashtest_h, flashtest_l; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 41 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 42 | /* Write auto select command sequence and test FLASH answer */ |
| 43 | V_ULONG(baseaddr + ((ulong)0x0555 << 3)) = 0x00AA00AA; |
| 44 | V_ULONG(baseaddr + ((ulong)0x02AA << 3)) = 0x00550055; |
| 45 | V_ULONG(baseaddr + ((ulong)0x0555 << 3)) = 0x00900090; |
| 46 | V_ULONG(baseaddr + 4 + ((ulong)0x0555 << 3)) = 0x00AA00AA; |
| 47 | V_ULONG(baseaddr + 4 + ((ulong)0x02AA << 3)) = 0x00550055; |
| 48 | V_ULONG(baseaddr + 4 + ((ulong)0x0555 << 3)) = 0x00900090; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 49 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 50 | flashtest_h = V_ULONG(baseaddr); /* manufacturer ID */ |
| 51 | flashtest_l = V_ULONG(baseaddr + 4); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 52 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 53 | if ((int)flashtest_h == AMD_MANUFACT) { |
| 54 | info->flash_id = FLASH_MAN_AMD; |
| 55 | } else { |
| 56 | info->flash_id = FLASH_UNKNOWN; |
| 57 | info->sector_count = 0; |
| 58 | info->size = 0; |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 59 | return (0); /* no or unknown flash */ |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 60 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 61 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 62 | flashtest_h = V_ULONG(baseaddr + 8); /* device ID */ |
| 63 | flashtest_l = V_ULONG(baseaddr + 12); |
| 64 | if (flashtest_h != flashtest_l) { |
| 65 | info->flash_id = FLASH_UNKNOWN; |
| 66 | return(0); |
| 67 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 68 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 69 | switch((int)flashtest_h) { |
| 70 | case AMD_ID_DL323B: |
| 71 | info->flash_id += FLASH_AMDL323B; |
| 72 | info->sector_count = 71; |
| 73 | info->size = 0x01000000; /* 4 * 4 MB = 16 MB */ |
| 74 | break; |
| 75 | case AMD_ID_LV640U: /* AMDLV640 and AMDLV641 have same ID */ |
| 76 | info->flash_id += FLASH_AMLV640U; |
| 77 | info->sector_count = 128; |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 78 | info->size = 0x02000000; /* 4 * 8 MB = 32 MB */ |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 79 | break; |
| 80 | default: |
| 81 | info->flash_id = FLASH_UNKNOWN; |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 82 | return(0); /* no or unknown flash */ |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 83 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 84 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 85 | if(flashtest_h == AMD_ID_LV640U) { |
| 86 | /* set up sector start adress table (uniform sector type) */ |
| 87 | for (i = 0; i < info->sector_count; i++) |
| 88 | info->start[i] = baseaddr + (i * 0x00040000); |
| 89 | } else { |
| 90 | /* set up sector start adress table (bottom sector type) */ |
| 91 | for (i = 0; i < 8; i++) { |
| 92 | info->start[i] = baseaddr + (i * 0x00008000); |
| 93 | } |
| 94 | for (i = 8; i < info->sector_count; i++) { |
| 95 | info->start[i] = baseaddr + (i * 0x00040000) - 0x001C0000; |
| 96 | } |
| 97 | } |
| 98 | /* check for protected sectors */ |
| 99 | for (i = 0; i < info->sector_count; i++) { |
| 100 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 101 | if ((V_ULONG( info->start[i] + 16 ) & 0x00010001) || |
| 102 | (V_ULONG( info->start[i] + 20 ) & 0x00010001)) { |
| 103 | info->protect[i] = 1; /* D0 = 1 if protected */ |
| 104 | } else { |
| 105 | info->protect[i] = 0; |
| 106 | } |
| 107 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 108 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 109 | flash_reset(); |
| 110 | return(info->size); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /*----------------------------------------------------------------------- |
| 114 | */ |
| 115 | unsigned long flash_init (void) |
| 116 | { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 117 | unsigned long size_b0 = 0; |
| 118 | int i; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 119 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 120 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 121 | for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 122 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 123 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 124 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 125 | /* Static FLASH Bank configuration here (only one bank) */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 126 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 127 | size_b0 = flash_get_size(CONFIG_SYS_FLASH0_BASE, &flash_info[0]); |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 128 | if (flash_info[0].flash_id == FLASH_UNKNOWN || size_b0 == 0) { |
| 129 | printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", |
| 130 | size_b0, size_b0>>20); |
| 131 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 132 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 133 | /* |
| 134 | * protect monitor and environment sectors |
| 135 | */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 136 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 137 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 138 | flash_protect(FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 139 | CONFIG_SYS_MONITOR_BASE, |
| 140 | CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1, |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 141 | &flash_info[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 142 | #endif |
| 143 | |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 144 | #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR) |
| 145 | # ifndef CONFIG_ENV_SIZE |
| 146 | # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 147 | # endif |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 148 | flash_protect(FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 149 | CONFIG_ENV_ADDR, |
| 150 | CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 151 | &flash_info[0]); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 152 | #endif |
| 153 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 154 | return (size_b0); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /*----------------------------------------------------------------------- |
| 158 | */ |
| 159 | void flash_print_info (flash_info_t *info) |
| 160 | { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 161 | int i; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 162 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 163 | if (info->flash_id == FLASH_UNKNOWN) { |
| 164 | printf ("missing or unknown FLASH type\n"); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | switch ((info->flash_id >> 16) & 0xff) { |
| 169 | case FLASH_MAN_AMD: printf ("AMD "); break; |
| 170 | default: printf ("Unknown Vendor "); break; |
| 171 | } |
| 172 | |
| 173 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 174 | case FLASH_AMDL323B: printf ("29DL323B (32 M, bottom sector)\n"); |
| 175 | break; |
| 176 | case FLASH_AMLV640U: printf ("29LV640U (64 M, uniform sector)\n"); |
| 177 | break; |
| 178 | default: printf ("Unknown Chip Type\n"); |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | printf (" Size: %ld MB in %d Sectors\n", |
| 183 | info->size >> 20, info->sector_count); |
| 184 | |
| 185 | printf (" Sector Start Addresses:"); |
| 186 | for (i=0; i<info->sector_count; ++i) { |
| 187 | if ((i % 5) == 0) |
| 188 | printf ("\n "); |
| 189 | printf (" %08lX%s", |
| 190 | info->start[i], |
| 191 | info->protect[i] ? " (RO)" : " " |
| 192 | ); |
| 193 | } |
| 194 | printf ("\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 195 | return; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /*----------------------------------------------------------------------- |
| 199 | */ |
| 200 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 201 | { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 202 | int flag, prot, sect, l_sect; |
| 203 | ulong start, now, last; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 204 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 205 | if ((s_first < 0) || (s_first > s_last)) { |
| 206 | if (info->flash_id == FLASH_UNKNOWN) { |
| 207 | printf ("- missing\n"); |
| 208 | } else { |
| 209 | printf ("- no sectors to erase\n"); |
| 210 | } |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | prot = 0; |
| 215 | for (sect = s_first; sect <= s_last; sect++) { |
| 216 | if (info->protect[sect]) |
| 217 | prot++; |
| 218 | } |
| 219 | |
| 220 | if (prot) { |
| 221 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 222 | prot); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 223 | } else { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 224 | printf ("\n"); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 225 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 226 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 227 | l_sect = -1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 228 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 229 | /* Disable interrupts which might cause a timeout here */ |
| 230 | flag = disable_interrupts(); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 231 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 232 | V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00AA00AA; |
| 233 | V_ULONG( info->start[0] + (0x02AA << 3) ) = 0x00550055; |
| 234 | V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00800080; |
| 235 | V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00AA00AA; |
| 236 | V_ULONG( info->start[0] + (0x02AA << 3) ) = 0x00550055; |
| 237 | V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00AA00AA; |
| 238 | V_ULONG( info->start[0] + 4 + (0x02AA << 3) ) = 0x00550055; |
| 239 | V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00800080; |
| 240 | V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00AA00AA; |
| 241 | V_ULONG( info->start[0] + 4 + (0x02AA << 3) ) = 0x00550055; |
| 242 | udelay (1000); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 243 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 244 | /* Start erase on unprotected sectors */ |
| 245 | for (sect = s_first; sect<=s_last; sect++) { |
| 246 | if (info->protect[sect] == 0) { /* not protected */ |
| 247 | V_ULONG( info->start[sect] ) = 0x00300030; |
| 248 | V_ULONG( info->start[sect] + 4 ) = 0x00300030; |
| 249 | l_sect = sect; |
| 250 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 251 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 252 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 253 | /* re-enable interrupts if necessary */ |
| 254 | if (flag) |
| 255 | enable_interrupts(); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 256 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 257 | /* wait at least 80us - let's wait 1 ms */ |
| 258 | udelay (1000); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 259 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 260 | /* |
| 261 | * We wait for the last triggered sector |
| 262 | */ |
| 263 | if (l_sect < 0) |
| 264 | goto DONE; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 265 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 266 | start = get_timer (0); |
| 267 | last = start; |
| 268 | while ((V_ULONG( info->start[l_sect] ) & 0x00800080) != 0x00800080 || |
| 269 | (V_ULONG( info->start[l_sect] + 4 ) & 0x00800080) != 0x00800080) |
| 270 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 271 | if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 272 | printf ("Timeout\n"); |
| 273 | return 1; |
| 274 | } |
| 275 | /* show that we're waiting */ |
| 276 | if ((now - last) > 1000) { /* every second */ |
| 277 | serial_putc ('.'); |
| 278 | last = now; |
| 279 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 280 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 281 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 282 | DONE: |
| 283 | /* reset to read mode */ |
| 284 | flash_reset (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 285 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 286 | printf (" done\n"); |
| 287 | return 0; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static int write_dword (flash_info_t *, ulong, unsigned char *); |
| 291 | |
| 292 | /*----------------------------------------------------------------------- |
| 293 | * Copy memory to flash, returns: |
| 294 | * 0 - OK |
| 295 | * 1 - write timeout |
| 296 | * 2 - Flash not erased |
| 297 | */ |
| 298 | |
| 299 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 300 | { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 301 | ulong dp; |
| 302 | static unsigned char bb[8]; |
| 303 | int i, l, rc, cc = cnt; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 304 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 305 | dp = (addr & ~7); /* get lower dword aligned address */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 306 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 307 | /* |
| 308 | * handle unaligned start bytes |
| 309 | */ |
| 310 | if ((l = addr - dp) != 0) { |
| 311 | for (i = 0; i < 8; i++) |
| 312 | bb[i] = (i < l || (i-l) >= cc) ? V_BYTE(dp+i) : *src++; |
| 313 | if ((rc = write_dword(info, dp, bb)) != 0) |
| 314 | { |
| 315 | return (rc); |
| 316 | } |
| 317 | dp += 8; |
| 318 | cc -= 8 - l; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 319 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 320 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 321 | /* |
| 322 | * handle word aligned part |
| 323 | */ |
| 324 | while (cc >= 8) { |
| 325 | if ((rc = write_dword(info, dp, src)) != 0) { |
| 326 | return (rc); |
| 327 | } |
| 328 | dp += 8; |
| 329 | src += 8; |
| 330 | cc -= 8; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 331 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 332 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 333 | if (cc <= 0) { |
| 334 | return (0); |
| 335 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 336 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 337 | /* |
| 338 | * handle unaligned tail bytes |
| 339 | */ |
| 340 | for (i = 0; i < 8; i++) { |
| 341 | bb[i] = (i < cc) ? *src++ : V_BYTE(dp+i); |
| 342 | } |
| 343 | return (write_dword(info, dp, bb)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /*----------------------------------------------------------------------- |
| 347 | * Write a dword to Flash, returns: |
| 348 | * 0 - OK |
| 349 | * 1 - write timeout |
| 350 | * 2 - Flash not erased |
| 351 | */ |
| 352 | static int write_dword (flash_info_t *info, ulong dest, unsigned char * pdata) |
| 353 | { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 354 | ulong start; |
| 355 | ulong cl = 0, ch =0; |
| 356 | int flag, i; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 357 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 358 | for (ch=0, i=0; i < 4; i++) |
| 359 | ch = (ch << 8) + *pdata++; /* high word */ |
| 360 | for (cl=0, i=0; i < 4; i++) |
| 361 | cl = (cl << 8) + *pdata++; /* low word */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 362 | |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 363 | /* Check if Flash is (sufficiently) erased */ |
| 364 | if ((*((vu_long *)dest) & ch) != ch |
| 365 | ||(*((vu_long *)(dest + 4)) & cl) != cl) |
| 366 | { |
| 367 | return (2); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 368 | } |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 369 | |
| 370 | /* Disable interrupts which might cause a timeout here */ |
| 371 | flag = disable_interrupts(); |
| 372 | |
| 373 | V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00AA00AA; |
| 374 | V_ULONG( info->start[0] + (0x02AA << 3) ) = 0x00550055; |
| 375 | V_ULONG( info->start[0] + (0x0555 << 3) ) = 0x00A000A0; |
| 376 | V_ULONG( dest ) = ch; |
| 377 | V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00AA00AA; |
| 378 | V_ULONG( info->start[0] + 4 + (0x02AA << 3) ) = 0x00550055; |
| 379 | V_ULONG( info->start[0] + 4 + (0x0555 << 3) ) = 0x00A000A0; |
| 380 | V_ULONG( dest + 4 ) = cl; |
| 381 | |
| 382 | /* re-enable interrupts if necessary */ |
| 383 | if (flag) |
| 384 | enable_interrupts(); |
| 385 | |
| 386 | /* data polling for D7 */ |
| 387 | start = get_timer (0); |
| 388 | while (((V_ULONG( dest ) & 0x00800080) != (ch & 0x00800080)) || |
| 389 | ((V_ULONG( dest + 4 ) & 0x00800080) != (cl & 0x00800080))) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 390 | if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
wdenk | 9dd611b | 2005-01-09 17:19:34 +0000 | [diff] [blame] | 391 | return (1); |
| 392 | } |
| 393 | } |
| 394 | return (0); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 395 | } |