wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Yoo. Jonghoon, IPone, yooth@ipone.co.kr |
| 10 | * U-Boot port on RPXlite board |
| 11 | * |
| 12 | * Some of flash control words are modified. (from 2x16bit device |
| 13 | * to 4x8bit device) |
| 14 | * RPXLite board I tested has only 4 AM29LV800BB devices. Other devices |
| 15 | * are not tested. |
| 16 | * |
| 17 | * (?) Does an RPXLite board which |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 18 | * does not use AM29LV800 flash memory exist ? |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 19 | * I don't know... |
| 20 | */ |
| 21 | |
| 22 | /* Yes,Yoo.They do use other FLASH for the board. |
| 23 | * |
| 24 | * Sam Song, IEMC. SHU, samsongshu@yahoo.com.cn |
| 25 | * U-Boot port on RPXlite DW version board |
| 26 | * |
| 27 | * By now,it uses 4 AM29DL323DB90VI devices(4x8bit). |
| 28 | * The total FLASH has 16MB(4x4MB). |
| 29 | * I just made some necessary changes on the basis of Wolfgang and Yoo's job. |
| 30 | * |
| 31 | * June 8, 2004 */ |
| 32 | |
| 33 | #include <common.h> |
| 34 | #include <mpc8xx.h> |
| 35 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 36 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 37 | |
| 38 | /*----------------------------------------------------------------------- |
| 39 | * Functions vu_long : volatile unsigned long IN include/common.h |
| 40 | */ |
| 41 | static ulong flash_get_size (vu_long *addr, flash_info_t *info); |
| 42 | static int write_word (flash_info_t *info, ulong dest, ulong data); |
| 43 | static void flash_get_offsets (ulong base, flash_info_t *info); |
| 44 | |
| 45 | unsigned long flash_init (void) |
| 46 | { |
| 47 | unsigned long size_b0 ; |
| 48 | int i; |
| 49 | |
| 50 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 51 | for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 52 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 53 | } |
| 54 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 55 | size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]); |
| 56 | flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]); |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 57 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 58 | #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 59 | /* If Monitor is in the cope of FLASH,then |
| 60 | * protect this area by default in case for |
| 61 | * other occupation. [SAM] */ |
| 62 | |
| 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+CONFIG_SYS_MONITOR_LEN-1, |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 67 | &flash_info[0]); |
| 68 | #endif |
| 69 | flash_info[0].size = size_b0; |
| 70 | return (size_b0); |
| 71 | } |
| 72 | |
| 73 | static void flash_get_offsets (ulong base, flash_info_t *info) |
| 74 | { |
| 75 | int i; |
| 76 | |
| 77 | /* set up sector start address table */ |
| 78 | if (info->flash_id & FLASH_BTYPE) { |
| 79 | info->start[0] = base + 0x00000000; |
| 80 | info->start[1] = base + 0x00008000; |
| 81 | info->start[2] = base + 0x00010000; |
| 82 | info->start[3] = base + 0x00018000; |
| 83 | info->start[4] = base + 0x00020000; |
| 84 | info->start[5] = base + 0x00028000; |
| 85 | info->start[6] = base + 0x00030000; |
| 86 | info->start[7] = base + 0x00038000; |
| 87 | |
| 88 | for (i = 8; i < info->sector_count; i++) { |
| 89 | info->start[i] = base + ((i-7) * 0x00040000); |
| 90 | } |
| 91 | } else { |
| 92 | i = info->sector_count - 1; |
| 93 | info->start[i--] = base + info->size - 0x00010000; |
| 94 | info->start[i--] = base + info->size - 0x00018000; |
| 95 | info->start[i--] = base + info->size - 0x00020000; |
| 96 | for (; i >= 0; i--) { |
| 97 | info->start[i] = base + i * 0x00040000; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | |
| 103 | void flash_print_info (flash_info_t *info) |
| 104 | { |
| 105 | int i; |
| 106 | |
| 107 | if (info->flash_id == FLASH_UNKNOWN) { |
| 108 | printf ("missing or unknown FLASH type\n"); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | switch (info->flash_id & FLASH_VENDMASK) { |
| 113 | case FLASH_MAN_AMD: printf ("AMD "); break; |
| 114 | case FLASH_MAN_FUJ: printf ("FUJITSU "); break; |
| 115 | default: printf ("Unknown Vendor "); break; |
| 116 | } |
| 117 | |
| 118 | switch (info->flash_id & FLASH_TYPEMASK) { |
| 119 | case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n"); |
| 120 | break; |
| 121 | case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n"); |
| 122 | break; |
| 123 | case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n"); |
| 124 | break; |
| 125 | case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n"); |
| 126 | break; |
| 127 | case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n"); |
| 128 | break; |
| 129 | case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n"); |
| 130 | break; |
| 131 | case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n"); |
| 132 | break; |
| 133 | case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n"); |
| 134 | break; |
| 135 | case FLASH_AMDL323B: printf ("AM29DL323B (32 Mbit, bottom boot sector)\n"); |
| 136 | break; |
| 137 | /* I just add the FLASH_AMDL323B for RPXlite_DW BOARD. [SAM] */ |
| 138 | default: printf ("Unknown Chip Type\n"); |
| 139 | break; |
| 140 | } |
| 141 | printf (" Size: %ld MB in %d Sectors\n",info->size >> 20, info->sector_count); |
| 142 | printf (" Sector Start Addresses:"); |
| 143 | for (i=0; i<info->sector_count; ++i) { |
| 144 | if ((i % 5) == 0) |
| 145 | printf ("\n "); |
| 146 | printf (" %08lX%s",info->start[i],info->protect[i] ? " (RO)" : " "); |
| 147 | } |
| 148 | printf ("\n"); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | static ulong flash_get_size (vu_long *addr, flash_info_t *info) |
| 153 | { |
| 154 | short i; |
| 155 | ulong value; |
| 156 | ulong base = (ulong)addr; |
| 157 | |
| 158 | /* Write auto select command: read Manufacturer ID */ |
| 159 | addr[0xAAA] = 0x00AA00AA ; |
| 160 | addr[0x555] = 0x00550055 ; |
| 161 | addr[0xAAA] = 0x00900090 ; |
| 162 | |
| 163 | value = addr[0] ; |
| 164 | switch (value & 0x00FF00FF) { |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 165 | case AMD_MANUFACT: /* AMD_MANUFACT =0x00010001 in flash.h */ |
| 166 | info->flash_id = FLASH_MAN_AMD; /* FLASH_MAN_AMD=0x00000000 in flash.h */ |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 167 | break; |
| 168 | case FUJ_MANUFACT: |
| 169 | info->flash_id = FLASH_MAN_FUJ; |
| 170 | break; |
| 171 | default: |
| 172 | info->flash_id = FLASH_UNKNOWN; |
| 173 | info->sector_count = 0; |
| 174 | info->size = 0; |
| 175 | return (0); /* no or unknown flash */ |
| 176 | } |
| 177 | |
| 178 | value = addr[2] ; /* device ID */ |
| 179 | switch (value & 0x00FF00FF) { |
| 180 | case (AMD_ID_LV400T & 0x00FF00FF): |
| 181 | info->flash_id += FLASH_AM400T; |
| 182 | info->sector_count = 11; |
| 183 | info->size = 0x00100000; |
| 184 | break; /* => 1 MB */ |
| 185 | case (AMD_ID_LV400B & 0x00FF00FF): |
| 186 | info->flash_id += FLASH_AM400B; |
| 187 | info->sector_count = 11; |
| 188 | info->size = 0x00100000; |
| 189 | break; /* => 1 MB */ |
| 190 | case (AMD_ID_LV800T & 0x00FF00FF): |
| 191 | info->flash_id += FLASH_AM800T; |
| 192 | info->sector_count = 19; |
| 193 | info->size = 0x00200000; |
| 194 | break; /* => 2 MB */ |
| 195 | case (AMD_ID_LV800B & 0x00FF00FF): |
| 196 | info->flash_id += FLASH_AM800B; |
| 197 | info->sector_count = 19; |
| 198 | info->size = 0x00400000; /* Size doubled by yooth */ |
| 199 | break; /* => 4 MB */ |
| 200 | case (AMD_ID_LV160T & 0x00FF00FF): |
| 201 | info->flash_id += FLASH_AM160T; |
| 202 | info->sector_count = 35; |
| 203 | info->size = 0x00400000; |
| 204 | break; /* => 4 MB */ |
| 205 | case (AMD_ID_LV160B & 0x00FF00FF): |
| 206 | info->flash_id += FLASH_AM160B; |
| 207 | info->sector_count = 35; |
| 208 | info->size = 0x00400000; |
| 209 | break; /* => 4 MB */ |
| 210 | case (AMD_ID_DL323B & 0x00FF00FF): |
| 211 | info->flash_id += FLASH_AMDL323B; |
| 212 | info->sector_count = 71; |
| 213 | info->size = 0x01000000; |
| 214 | break; /* => 16 MB(4x4MB) */ |
| 215 | /* AMD_ID_DL323B= 0x22532253 FLASH_AMDL323B= 0x0013 |
| 216 | * AMD_ID_DL323B could be found in <flash.h>.[SAM] |
| 217 | * So we could get : flash_id = 0x00000013. |
| 218 | * The first four-bit represents VEDOR ID,leaving others for FLASH ID. */ |
| 219 | default: |
| 220 | info->flash_id = FLASH_UNKNOWN; |
| 221 | return (0); /* => no or unknown flash */ |
| 222 | |
| 223 | } |
| 224 | /* set up sector start address table */ |
| 225 | if (info->flash_id & FLASH_BTYPE) { |
| 226 | /* FLASH_BTYPE=0x0001 mask for bottom boot sector type.If the last bit equals 1, |
| 227 | * it means bottom boot flash. GOOD IDEA! [SAM] |
| 228 | */ |
| 229 | |
| 230 | /* set sector offsets for bottom boot block type */ |
| 231 | info->start[0] = base + 0x00000000; |
| 232 | info->start[1] = base + 0x00008000; |
| 233 | info->start[2] = base + 0x00010000; |
| 234 | info->start[3] = base + 0x00018000; |
| 235 | info->start[4] = base + 0x00020000; |
| 236 | info->start[5] = base + 0x00028000; |
| 237 | info->start[6] = base + 0x00030000; |
| 238 | info->start[7] = base + 0x00038000; |
| 239 | |
| 240 | for (i = 8; i < info->sector_count; i++) { |
| 241 | info->start[i] = base + ((i-7) * 0x00040000) ; |
| 242 | } |
| 243 | } else { |
| 244 | /* set sector offsets for top boot block type */ |
| 245 | i = info->sector_count - 1; |
| 246 | info->start[i--] = base + info->size - 0x00010000; |
| 247 | info->start[i--] = base + info->size - 0x00018000; |
| 248 | info->start[i--] = base + info->size - 0x00020000; |
| 249 | for (; i >= 0; i--) { |
| 250 | info->start[i] = base + i * 0x00040000; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* check for protected sectors */ |
| 255 | for (i = 0; i < info->sector_count; i++) { |
| 256 | /* read sector protection at sector address, (A7 .. A0) = 0x02 */ |
| 257 | /* D0 = 1 if protected */ |
| 258 | addr = (volatile unsigned long *)(info->start[i]); |
| 259 | /* info->protect[i] = addr[4] & 1 ; */ |
| 260 | /* Mask it for disorder FLASH protection **[Sam]** */ |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Prevent writes to uninitialized FLASH. |
| 265 | */ |
| 266 | if (info->flash_id != FLASH_UNKNOWN) { |
| 267 | addr = (volatile unsigned long *)info->start[0]; |
| 268 | |
| 269 | *addr = 0xF0F0F0F0; /* reset bank */ |
| 270 | } |
| 271 | return (info->size); |
| 272 | } |
| 273 | |
| 274 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 275 | { |
| 276 | vu_long *addr = (vu_long*)(info->start[0]); |
| 277 | int flag, prot, sect, l_sect; |
| 278 | ulong start, now, last; |
| 279 | |
| 280 | if ((s_first < 0) || (s_first > s_last)) { |
| 281 | if (info->flash_id == FLASH_UNKNOWN) { |
| 282 | printf ("- missing\n"); |
| 283 | } else { |
| 284 | printf ("- no sectors to erase\n"); |
| 285 | } |
| 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | if ((info->flash_id == FLASH_UNKNOWN) || |
| 290 | (info->flash_id > FLASH_AMD_COMP)) { |
| 291 | printf ("Can't erase unknown flash type %08lx - aborted\n", |
| 292 | info->flash_id); |
| 293 | return 1; |
| 294 | } |
| 295 | |
| 296 | prot = 0; |
| 297 | for (sect=s_first; sect<=s_last; ++sect) { |
| 298 | if (info->protect[sect]) { |
| 299 | prot++; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (prot) { |
| 304 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 305 | prot); |
| 306 | } else { |
| 307 | printf ("\n"); |
| 308 | } |
| 309 | |
| 310 | l_sect = -1; |
| 311 | |
| 312 | /* Disable interrupts which might cause a timeout here */ |
| 313 | flag = disable_interrupts(); |
| 314 | |
| 315 | addr[0xAAA] = 0xAAAAAAAA; |
| 316 | addr[0x555] = 0x55555555; |
| 317 | addr[0xAAA] = 0x80808080; |
| 318 | addr[0xAAA] = 0xAAAAAAAA; |
| 319 | addr[0x555] = 0x55555555; |
| 320 | |
| 321 | /* Start erase on unprotected sectors */ |
| 322 | for (sect = s_first; sect<=s_last; sect++) { |
| 323 | if (info->protect[sect] == 0) { /* not protected */ |
| 324 | addr = (vu_long *)(info->start[sect]) ; |
| 325 | addr[0] = 0x30303030 ; |
| 326 | l_sect = sect; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /* re-enable interrupts if necessary */ |
| 331 | if (flag) |
| 332 | enable_interrupts(); |
| 333 | |
| 334 | /* wait at least 80us - let's wait 1 ms */ |
| 335 | udelay (1000); |
| 336 | |
| 337 | /* |
| 338 | * We wait for the last triggered sector |
| 339 | */ |
| 340 | if (l_sect < 0) |
| 341 | goto DONE; |
| 342 | |
| 343 | start = get_timer (0); |
| 344 | last = start; |
| 345 | addr = (vu_long *)(info->start[l_sect]); |
| 346 | while ((addr[0] & 0x80808080) != 0x80808080) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 347 | if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) { |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 348 | printf ("Timeout\n"); |
| 349 | return 1; |
| 350 | } |
| 351 | /* show that we're waiting */ |
| 352 | if ((now - last) > 1000) { /* every second */ |
| 353 | putc ('.'); |
| 354 | last = now; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | DONE: |
| 359 | /* reset to read mode */ |
| 360 | addr = (vu_long *)info->start[0]; |
| 361 | addr[0] = 0xF0F0F0F0; /* reset bank */ |
| 362 | |
| 363 | printf (" done\n"); |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /*----------------------------------------------------------------------- |
| 368 | * Copy memory to flash, returns: |
| 369 | * 0 - OK |
| 370 | * 1 - write timeout |
| 371 | * 2 - Flash not erased |
| 372 | */ |
| 373 | |
| 374 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 375 | { |
| 376 | ulong cp, wp, data; |
| 377 | int i, l, rc; |
| 378 | |
| 379 | wp = (addr & ~3); /* get lower word aligned address */ |
| 380 | |
| 381 | /* |
| 382 | * handle unaligned start bytes |
| 383 | */ |
| 384 | if ((l = addr - wp) != 0) { |
| 385 | data = 0; |
| 386 | for (i=0, cp=wp; i<l; ++i, ++cp) { |
| 387 | data = (data << 8) | (*(uchar *)cp); |
| 388 | } |
| 389 | for (; i<4 && cnt>0; ++i) { |
| 390 | data = (data << 8) | *src++; |
| 391 | --cnt; |
| 392 | ++cp; |
| 393 | } |
| 394 | for (; cnt==0 && i<4; ++i, ++cp) { |
| 395 | data = (data << 8) | (*(uchar *)cp); |
| 396 | } |
| 397 | |
| 398 | if ((rc = write_word(info, wp, data)) != 0) { |
| 399 | return (rc); |
| 400 | } |
| 401 | wp += 4; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * handle word aligned part |
| 406 | */ |
| 407 | while (cnt >= 4) { |
| 408 | data = 0; |
| 409 | for (i=0; i<4; ++i) { |
| 410 | data = (data << 8) | *src++; |
| 411 | } |
| 412 | if ((rc = write_word(info, wp, data)) != 0) { |
| 413 | return (rc); |
| 414 | } |
| 415 | wp += 4; |
| 416 | cnt -= 4; |
| 417 | } |
| 418 | |
| 419 | if (cnt == 0) { |
| 420 | return (0); |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * handle unaligned tail bytes |
| 425 | */ |
| 426 | data = 0; |
| 427 | for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) { |
| 428 | data = (data << 8) | *src++; |
| 429 | --cnt; |
| 430 | } |
| 431 | for (; i<4; ++i, ++cp) { |
| 432 | data = (data << 8) | (*(uchar *)cp); |
| 433 | } |
| 434 | return (write_word(info, wp, data)); |
| 435 | } |
| 436 | |
| 437 | /*----------------------------------------------------------------------- |
| 438 | * Write a word to Flash, returns: |
| 439 | * 0 - OK |
| 440 | * 1 - write timeout |
| 441 | * 2 - Flash not erased |
| 442 | */ |
| 443 | static int write_word (flash_info_t *info, ulong dest, ulong data) |
| 444 | { |
| 445 | vu_long *addr = (vu_long *)(info->start[0]); |
| 446 | ulong start; |
| 447 | int flag; |
| 448 | |
| 449 | /* Check if Flash is (sufficiently) erased */ |
| 450 | if ((*((vu_long *)dest) & data) != data) { |
| 451 | return (2); |
| 452 | } |
| 453 | /* Disable interrupts which might cause a timeout here */ |
| 454 | flag = disable_interrupts(); |
| 455 | |
| 456 | addr[0xAAA] = 0xAAAAAAAA; |
| 457 | addr[0x555] = 0x55555555; |
| 458 | addr[0xAAA] = 0xA0A0A0A0; |
| 459 | |
| 460 | *((vu_long *)dest) = data; |
| 461 | |
| 462 | /* re-enable interrupts if necessary */ |
| 463 | if (flag) |
| 464 | enable_interrupts(); |
| 465 | |
| 466 | /* data polling for D7 */ |
| 467 | start = get_timer (0); |
| 468 | while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 469 | if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) { |
wdenk | e63c8ee | 2004-06-09 21:04:48 +0000 | [diff] [blame] | 470 | return (1); |
| 471 | } |
| 472 | } |
| 473 | return (0); |
| 474 | } |