wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Marius Groeger <mgroeger@sysgo.de> |
| 4 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 5 | * |
| 6 | * (C) Copyright 2000 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 8 | * |
| 9 | * Flash Routines for AM290[48]0B devices |
| 10 | * |
| 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 | #include "vpd.h" |
| 18 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 19 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 20 | |
| 21 | /*----------------------------------------------------------------------- |
| 22 | * Functions |
| 23 | */ |
| 24 | |
| 25 | static ulong flash_get_size (vu_long *addr, flash_info_t *info); |
| 26 | static int write_word (flash_info_t *info, ulong dest, ulong data); |
| 27 | |
| 28 | /*----------------------------------------------------------------------- |
| 29 | */ |
| 30 | |
| 31 | unsigned long flash_init (void) |
| 32 | { |
| 33 | unsigned long size, totsize; |
| 34 | int i; |
| 35 | ulong addr; |
| 36 | |
| 37 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 38 | for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 39 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 40 | } |
| 41 | |
| 42 | totsize = 0; |
| 43 | addr = 0xfc000000; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 44 | for(i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 45 | size = flash_get_size((vu_long *)addr, &flash_info[i]); |
| 46 | if (flash_info[i].flash_id == FLASH_UNKNOWN) |
| 47 | break; |
| 48 | totsize += size; |
| 49 | addr += size; |
| 50 | } |
| 51 | |
| 52 | addr = 0xfe000000; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 53 | for(i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 54 | |
| 55 | size = flash_get_size((vu_long *)addr, &flash_info[i]); |
| 56 | if (flash_info[i].flash_id == FLASH_UNKNOWN) |
| 57 | break; |
| 58 | totsize += size; |
| 59 | addr += size; |
| 60 | } |
| 61 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 63 | /* monitor protection ON by default */ |
| 64 | flash_protect(FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 65 | CONFIG_SYS_MONITOR_BASE, |
| 66 | CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1, |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 67 | &flash_info[0]); |
| 68 | #endif |
| 69 | |
| 70 | return (totsize); |
| 71 | } |
| 72 | |
| 73 | /*----------------------------------------------------------------------- |
| 74 | */ |
| 75 | void flash_print_info (flash_info_t *info) |
| 76 | { |
| 77 | int i; |
| 78 | |
| 79 | if (info->flash_id == FLASH_UNKNOWN) { |
| 80 | printf ("missing or unknown FLASH type\n"); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | switch (info->flash_id >> 16) { |
| 85 | case 0x1: |
| 86 | printf ("AMD "); |
| 87 | break; |
| 88 | default: |
| 89 | printf ("Unknown Vendor "); |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 94 | case AMD_ID_F040B: |
| 95 | printf ("AM29F040B (4 Mbit)\n"); |
| 96 | break; |
| 97 | case AMD_ID_F080B: |
| 98 | printf ("AM29F080B (8 Mbit)\n"); |
| 99 | break; |
| 100 | case AMD_ID_F016D: |
| 101 | printf ("AM29F016D (16 Mbit)\n"); |
| 102 | break; |
| 103 | default: |
| 104 | printf ("Unknown Chip Type\n"); |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | printf (" Size: %ld MB in %d Sectors\n", |
| 109 | info->size >> 20, info->sector_count); |
| 110 | |
| 111 | printf (" Sector Start Addresses:"); |
| 112 | for (i=0; i<info->sector_count; ++i) { |
| 113 | if ((i % 5) == 0) |
| 114 | printf ("\n "); |
| 115 | printf (" %08lX%s", |
| 116 | info->start[i], |
| 117 | info->protect[i] ? " (RO)" : " " |
| 118 | ); |
| 119 | } |
| 120 | printf ("\n"); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * The following code cannot be run from FLASH! |
| 126 | */ |
| 127 | |
| 128 | static ulong flash_get_size (vu_long *addr, flash_info_t *info) |
| 129 | { |
| 130 | short i; |
| 131 | ulong vendor, devid; |
| 132 | ulong base = (ulong)addr; |
| 133 | |
| 134 | /* Write auto select command: read Manufacturer ID */ |
| 135 | addr[0x0555] = 0xAAAAAAAA; |
| 136 | addr[0x02AA] = 0x55555555; |
| 137 | addr[0x0555] = 0x90909090; |
| 138 | |
| 139 | vendor = addr[0]; |
| 140 | devid = addr[1] & 0xff; |
| 141 | |
| 142 | /* only support AMD */ |
| 143 | if (vendor != 0x01010101) { |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | vendor &= 0xf; |
| 148 | devid &= 0xff; |
| 149 | |
| 150 | if (devid == AMD_ID_F040B) { |
| 151 | info->flash_id = vendor << 16 | devid; |
| 152 | info->sector_count = 8; |
| 153 | info->size = info->sector_count * 0x10000; |
| 154 | } |
| 155 | else if (devid == AMD_ID_F080B) { |
| 156 | info->flash_id = vendor << 16 | devid; |
| 157 | info->sector_count = 16; |
| 158 | info->size = 4 * info->sector_count * 0x10000; |
| 159 | } |
| 160 | else if (devid == AMD_ID_F016D) { |
| 161 | info->flash_id = vendor << 16 | devid; |
| 162 | info->sector_count = 32; |
| 163 | info->size = 4 * info->sector_count * 0x10000; |
| 164 | } |
| 165 | else { |
| 166 | printf ("## Unknown Flash Type: %08lx\n", devid); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* check for protected sectors */ |
| 171 | for (i = 0; i < info->sector_count; i++) { |
| 172 | /* sector base address */ |
| 173 | info->start[i] = base + i * (info->size / info->sector_count); |
| 174 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 175 | /* D0 = 1 if protected */ |
| 176 | addr = (volatile unsigned long *)(info->start[i]); |
| 177 | info->protect[i] = addr[2] & 1; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Prevent writes to uninitialized FLASH. |
| 182 | */ |
| 183 | if (info->flash_id != FLASH_UNKNOWN) { |
| 184 | addr = (vu_long *)info->start[0]; |
| 185 | addr[0] = 0xF0; /* reset bank */ |
| 186 | } |
| 187 | |
| 188 | return (info->size); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /*----------------------------------------------------------------------- |
| 193 | */ |
| 194 | |
| 195 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 196 | { |
| 197 | vu_long *addr = (vu_long*)(info->start[0]); |
| 198 | int flag, prot, sect, l_sect; |
| 199 | ulong start, now, last; |
| 200 | |
| 201 | if ((s_first < 0) || (s_first > s_last)) { |
| 202 | if (info->flash_id == FLASH_UNKNOWN) { |
| 203 | printf ("- missing\n"); |
| 204 | } else { |
| 205 | printf ("- no sectors to erase\n"); |
| 206 | } |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | prot = 0; |
| 211 | for (sect = s_first; sect <= s_last; sect++) { |
| 212 | if (info->protect[sect]) { |
| 213 | prot++; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (prot) { |
| 218 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 219 | prot); |
| 220 | } else { |
| 221 | printf ("\n"); |
| 222 | } |
| 223 | |
| 224 | l_sect = -1; |
| 225 | |
| 226 | /* Disable interrupts which might cause a timeout here */ |
| 227 | flag = disable_interrupts(); |
| 228 | |
| 229 | addr[0x0555] = 0XAAAAAAAA; |
| 230 | addr[0x02AA] = 0x55555555; |
| 231 | addr[0x0555] = 0x80808080; |
| 232 | addr[0x0555] = 0XAAAAAAAA; |
| 233 | addr[0x02AA] = 0x55555555; |
| 234 | |
| 235 | /* Start erase on unprotected sectors */ |
| 236 | for (sect = s_first; sect<=s_last; sect++) { |
| 237 | if (info->protect[sect] == 0) { /* not protected */ |
| 238 | addr = (vu_long*)(info->start[sect]); |
| 239 | addr[0] = 0x30303030; |
| 240 | l_sect = sect; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* re-enable interrupts if necessary */ |
| 245 | if (flag) |
| 246 | enable_interrupts(); |
| 247 | |
| 248 | /* wait at least 80us - let's wait 1 ms */ |
| 249 | udelay (1000); |
| 250 | |
| 251 | /* |
| 252 | * We wait for the last triggered sector |
| 253 | */ |
| 254 | if (l_sect < 0) |
| 255 | goto DONE; |
| 256 | |
| 257 | start = get_timer (0); |
| 258 | last = start; |
| 259 | addr = (vu_long*)(info->start[l_sect]); |
| 260 | while ((addr[0] & 0x80808080) != 0x80808080) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 261 | if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 262 | printf ("Timeout\n"); |
| 263 | return 1; |
| 264 | } |
| 265 | /* show that we're waiting */ |
| 266 | if ((now - last) > 1000) { /* every second */ |
| 267 | serial_putc ('.'); |
| 268 | last = now; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | DONE: |
| 273 | /* reset to read mode */ |
| 274 | addr = (volatile unsigned long *)info->start[0]; |
| 275 | addr[0] = 0xF0F0F0F0; /* reset bank */ |
| 276 | |
| 277 | printf (" done\n"); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | /*----------------------------------------------------------------------- |
| 282 | * Copy memory to flash, returns: |
| 283 | * 0 - OK |
| 284 | * 1 - write timeout |
| 285 | * 2 - Flash not erased |
| 286 | */ |
| 287 | |
| 288 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 289 | { |
| 290 | ulong cp, wp, data; |
| 291 | int i, l, rc; |
| 292 | |
| 293 | wp = (addr & ~3); /* get lower word aligned address */ |
| 294 | |
| 295 | /* |
| 296 | * handle unaligned start bytes |
| 297 | */ |
| 298 | if ((l = addr - wp) != 0) { |
| 299 | data = 0; |
| 300 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
| 301 | data = (data << 8) | (*(uchar *)cp); |
| 302 | } |
| 303 | for (; i<4 && cnt>0; ++i) { |
| 304 | data = (data << 8) | *src++; |
| 305 | --cnt; |
| 306 | ++cp; |
| 307 | } |
| 308 | for (; cnt==0 && i<4; ++i, ++cp) { |
| 309 | data = (data << 8) | (*(uchar *)cp); |
| 310 | } |
| 311 | |
| 312 | if ((rc = write_word(info, wp, data)) != 0) { |
| 313 | return (rc); |
| 314 | } |
| 315 | wp += 4; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * handle word aligned part |
| 320 | */ |
| 321 | while (cnt >= 4) { |
| 322 | data = 0; |
| 323 | for (i=0; i<4; ++i) { |
| 324 | data = (data << 8) | *src++; |
| 325 | } |
| 326 | if ((rc = write_word(info, wp, data)) != 0) { |
| 327 | return (rc); |
| 328 | } |
| 329 | wp += 4; |
| 330 | cnt -= 4; |
| 331 | } |
| 332 | |
| 333 | if (cnt == 0) { |
| 334 | return (0); |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * handle unaligned tail bytes |
| 339 | */ |
| 340 | data = 0; |
| 341 | for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) { |
| 342 | data = (data << 8) | *src++; |
| 343 | --cnt; |
| 344 | } |
| 345 | for (; i<4; ++i, ++cp) { |
| 346 | data = (data << 8) | (*(uchar *)cp); |
| 347 | } |
| 348 | |
| 349 | return (write_word(info, wp, data)); |
| 350 | } |
| 351 | |
| 352 | /*----------------------------------------------------------------------- |
| 353 | * Write a word to Flash, returns: |
| 354 | * 0 - OK |
| 355 | * 1 - write timeout |
| 356 | * 2 - Flash not erased |
| 357 | */ |
| 358 | static int write_word (flash_info_t *info, ulong dest, ulong data) |
| 359 | { |
| 360 | vu_long *addr = (vu_long*)(info->start[0]); |
| 361 | ulong start; |
| 362 | int flag; |
| 363 | |
| 364 | /* Check if Flash is (sufficiently) erased */ |
| 365 | if ((*((vu_long *)dest) & data) != data) { |
| 366 | return (2); |
| 367 | } |
| 368 | /* Disable interrupts which might cause a timeout here */ |
| 369 | flag = disable_interrupts(); |
| 370 | |
| 371 | addr[0x0555] = 0xAAAAAAAA; |
| 372 | addr[0x02AA] = 0x55555555; |
| 373 | addr[0x0555] = 0xA0A0A0A0; |
| 374 | |
| 375 | *((vu_long *)dest) = data; |
| 376 | |
| 377 | /* re-enable interrupts if necessary */ |
| 378 | if (flag) |
| 379 | enable_interrupts(); |
| 380 | |
| 381 | /* data polling for D7 */ |
| 382 | start = get_timer (0); |
| 383 | while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 384 | if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 385 | return (1); |
| 386 | } |
| 387 | } |
| 388 | return (0); |
| 389 | } |
| 390 | |
| 391 | /*----------------------------------------------------------------------- |
| 392 | */ |