stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com |
| 4 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <asm/processor.h> |
| 10 | |
| 11 | #undef DEBUG_FLASH |
| 12 | /* |
| 13 | * This file implements a Common Flash Interface (CFI) driver for ppcboot. |
| 14 | * The width of the port and the width of the chips are determined at initialization. |
| 15 | * These widths are used to calculate the address for access CFI data structures. |
| 16 | * It has been tested on an Intel Strataflash implementation. |
| 17 | * |
| 18 | * References |
| 19 | * JEDEC Standard JESD68 - Common Flash Interface (CFI) |
| 20 | * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes |
| 21 | * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets |
| 22 | * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet |
| 23 | * |
| 24 | * TODO |
| 25 | * Use Primary Extended Query table (PRI) and Alternate Algorithm Query Table (ALT) to determine if protection is available |
| 26 | * Add support for other command sets Use the PRI and ALT to determine command set |
| 27 | * Verify erase and program timeouts. |
| 28 | */ |
| 29 | |
| 30 | #define FLASH_CMD_CFI 0x98 |
| 31 | #define FLASH_CMD_READ_ID 0x90 |
| 32 | #define FLASH_CMD_RESET 0xff |
| 33 | #define FLASH_CMD_BLOCK_ERASE 0x20 |
| 34 | #define FLASH_CMD_ERASE_CONFIRM 0xD0 |
| 35 | #define FLASH_CMD_WRITE 0x40 |
| 36 | #define FLASH_CMD_PROTECT 0x60 |
| 37 | #define FLASH_CMD_PROTECT_SET 0x01 |
| 38 | #define FLASH_CMD_PROTECT_CLEAR 0xD0 |
| 39 | #define FLASH_CMD_CLEAR_STATUS 0x50 |
| 40 | #define FLASH_CMD_WRITE_TO_BUFFER 0xE8 |
| 41 | #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0 |
| 42 | |
| 43 | #define FLASH_STATUS_DONE 0x80 |
| 44 | #define FLASH_STATUS_ESS 0x40 |
| 45 | #define FLASH_STATUS_ECLBS 0x20 |
| 46 | #define FLASH_STATUS_PSLBS 0x10 |
| 47 | #define FLASH_STATUS_VPENS 0x08 |
| 48 | #define FLASH_STATUS_PSS 0x04 |
| 49 | #define FLASH_STATUS_DPS 0x02 |
| 50 | #define FLASH_STATUS_R 0x01 |
| 51 | #define FLASH_STATUS_PROTECT 0x01 |
| 52 | |
| 53 | #define FLASH_OFFSET_CFI 0x55 |
| 54 | #define FLASH_OFFSET_CFI_RESP 0x10 |
| 55 | #define FLASH_OFFSET_WTOUT 0x1F |
| 56 | #define FLASH_OFFSET_WBTOUT 0x20 |
| 57 | #define FLASH_OFFSET_ETOUT 0x21 |
| 58 | #define FLASH_OFFSET_CETOUT 0x22 |
| 59 | #define FLASH_OFFSET_WMAX_TOUT 0x23 |
| 60 | #define FLASH_OFFSET_WBMAX_TOUT 0x24 |
| 61 | #define FLASH_OFFSET_EMAX_TOUT 0x25 |
| 62 | #define FLASH_OFFSET_CEMAX_TOUT 0x26 |
| 63 | #define FLASH_OFFSET_SIZE 0x27 |
| 64 | #define FLASH_OFFSET_INTERFACE 0x28 |
| 65 | #define FLASH_OFFSET_BUFFER_SIZE 0x2A |
| 66 | #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C |
| 67 | #define FLASH_OFFSET_ERASE_REGIONS 0x2D |
| 68 | #define FLASH_OFFSET_PROTECT 0x02 |
| 69 | #define FLASH_OFFSET_USER_PROTECTION 0x85 |
| 70 | #define FLASH_OFFSET_INTEL_PROTECTION 0x81 |
| 71 | |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 72 | #define FLASH_MAN_CFI 0x01000000 |
| 73 | |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 74 | typedef union { |
| 75 | unsigned char c; |
| 76 | unsigned short w; |
| 77 | unsigned long l; |
| 78 | } cfiword_t; |
| 79 | |
| 80 | typedef union { |
| 81 | unsigned char * cp; |
| 82 | unsigned short *wp; |
| 83 | unsigned long *lp; |
| 84 | } cfiptr_t; |
| 85 | |
| 86 | #define NUM_ERASE_REGIONS 4 |
| 87 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 88 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */ |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 89 | |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 90 | /*----------------------------------------------------------------------- |
| 91 | * Functions |
| 92 | */ |
| 93 | |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 94 | static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c); |
| 95 | static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf); |
| 96 | static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd); |
| 97 | static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd); |
| 98 | static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd); |
| 99 | static int flash_detect_cfi(flash_info_t * info); |
| 100 | static ulong flash_get_size (ulong base, int banknum); |
| 101 | static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword); |
| 102 | static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 103 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 104 | static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len); |
| 105 | #endif |
| 106 | /*----------------------------------------------------------------------- |
| 107 | * create an address based on the offset and the port width |
| 108 | */ |
| 109 | inline uchar * flash_make_addr(flash_info_t * info, int sect, int offset) |
| 110 | { |
| 111 | return ((uchar *)(info->start[sect] + (offset * info->portwidth))); |
| 112 | } |
| 113 | /*----------------------------------------------------------------------- |
| 114 | * read a character at a port width address |
| 115 | */ |
| 116 | inline uchar flash_read_uchar(flash_info_t * info, uchar offset) |
| 117 | { |
| 118 | uchar *cp; |
| 119 | cp = flash_make_addr(info, 0, offset); |
| 120 | return (cp[info->portwidth - 1]); |
| 121 | } |
| 122 | |
| 123 | /*----------------------------------------------------------------------- |
| 124 | * read a short word by swapping for ppc format. |
| 125 | */ |
| 126 | ushort flash_read_ushort(flash_info_t * info, int sect, uchar offset) |
| 127 | { |
| 128 | uchar * addr; |
| 129 | |
| 130 | addr = flash_make_addr(info, sect, offset); |
| 131 | return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | /*----------------------------------------------------------------------- |
| 136 | * read a long word by picking the least significant byte of each maiximum |
| 137 | * port size word. Swap for ppc format. |
| 138 | */ |
| 139 | ulong flash_read_long(flash_info_t * info, int sect, uchar offset) |
| 140 | { |
| 141 | uchar * addr; |
| 142 | |
| 143 | addr = flash_make_addr(info, sect, offset); |
| 144 | return ( (addr[(2*info->portwidth) - 1] << 24 ) | (addr[(info->portwidth) -1] << 16) | |
| 145 | (addr[(4*info->portwidth) - 1] << 8) | addr[(3*info->portwidth) - 1]); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | /*----------------------------------------------------------------------- |
| 150 | */ |
| 151 | unsigned long flash_init (void) |
| 152 | { |
| 153 | unsigned long size; |
| 154 | int i; |
| 155 | unsigned long address; |
| 156 | |
| 157 | |
| 158 | /* The flash is positioned back to back, with the demultiplexing of the chip |
| 159 | * based on the A24 address line. |
| 160 | * |
| 161 | */ |
| 162 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 163 | address = CONFIG_SYS_FLASH_BASE; |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 164 | size = 0; |
| 165 | |
| 166 | /* Init: no FLASHes known */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 167 | for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) { |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 168 | flash_info[i].flash_id = FLASH_UNKNOWN; |
| 169 | size += flash_info[i].size = flash_get_size(address, i); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 170 | address += CONFIG_SYS_FLASH_INCREMENT; |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 171 | if (flash_info[0].flash_id == FLASH_UNKNOWN) { |
| 172 | printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",i, |
| 173 | flash_info[0].size, flash_info[i].size<<20); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | #if 0 /* test-only */ |
| 178 | /* Monitor protection ON by default */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 179 | #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE) |
| 180 | for(i=0; flash_info[0].start[i] < CONFIG_SYS_MONITOR_BASE+CONFIG_SYS_MONITOR_LEN-1; i++) |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 181 | (void)flash_real_protect(&flash_info[0], i, 1); |
| 182 | #endif |
| 183 | #else |
| 184 | /* monitor protection ON by default */ |
| 185 | flash_protect (FLAG_PROTECT_SET, |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 186 | - CONFIG_SYS_MONITOR_LEN, |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 187 | - 1, &flash_info[1]); |
| 188 | #endif |
| 189 | |
| 190 | return (size); |
| 191 | } |
| 192 | |
| 193 | /*----------------------------------------------------------------------- |
| 194 | */ |
| 195 | int flash_erase (flash_info_t *info, int s_first, int s_last) |
| 196 | { |
| 197 | int rcode = 0; |
| 198 | int prot; |
| 199 | int sect; |
| 200 | |
| 201 | if( info->flash_id != FLASH_MAN_CFI) { |
| 202 | printf ("Can't erase unknown flash type - aborted\n"); |
| 203 | return 1; |
| 204 | } |
| 205 | if ((s_first < 0) || (s_first > s_last)) { |
| 206 | printf ("- no sectors to erase\n"); |
| 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 | if (prot) { |
| 217 | printf ("- Warning: %d protected sectors will not be erased!\n", |
| 218 | prot); |
| 219 | } else { |
| 220 | printf ("\n"); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | for (sect = s_first; sect<=s_last; sect++) { |
| 225 | if (info->protect[sect] == 0) { /* not protected */ |
| 226 | flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS); |
| 227 | flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE); |
| 228 | flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM); |
| 229 | |
| 230 | if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) { |
| 231 | rcode = 1; |
| 232 | } else |
| 233 | printf("."); |
| 234 | } |
| 235 | } |
| 236 | printf (" done\n"); |
| 237 | return rcode; |
| 238 | } |
| 239 | |
| 240 | /*----------------------------------------------------------------------- |
| 241 | */ |
| 242 | void flash_print_info (flash_info_t *info) |
| 243 | { |
| 244 | int i; |
| 245 | |
| 246 | if (info->flash_id != FLASH_MAN_CFI) { |
| 247 | printf ("missing or unknown FLASH type\n"); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | printf("CFI conformant FLASH (%d x %d)", |
| 252 | (info->portwidth << 3 ), (info->chipwidth << 3 )); |
| 253 | printf (" Size: %ld MB in %d Sectors\n", |
| 254 | info->size >> 20, info->sector_count); |
| 255 | printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n", |
| 256 | info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size); |
| 257 | |
| 258 | printf (" Sector Start Addresses:"); |
| 259 | for (i=0; i<info->sector_count; ++i) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 260 | #ifdef CONFIG_SYS_FLASH_EMPTY_INFO |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 261 | int k; |
| 262 | int size; |
| 263 | int erased; |
| 264 | volatile unsigned long *flash; |
| 265 | |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 266 | /* |
| 267 | * Check if whole sector is erased |
| 268 | */ |
| 269 | if (i != (info->sector_count-1)) |
| 270 | size = info->start[i+1] - info->start[i]; |
| 271 | else |
| 272 | size = info->start[0] + info->size - info->start[i]; |
| 273 | erased = 1; |
| 274 | flash = (volatile unsigned long *)info->start[i]; |
| 275 | size = size >> 2; /* divide by 4 for longword access */ |
| 276 | for (k=0; k<size; k++) |
| 277 | { |
| 278 | if (*flash++ != 0xffffffff) |
| 279 | { |
| 280 | erased = 0; |
| 281 | break; |
| 282 | } |
| 283 | } |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 284 | |
| 285 | if ((i % 5) == 0) |
| 286 | printf ("\n "); |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 287 | /* print empty and read-only info */ |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 288 | printf (" %08lX%s%s", |
| 289 | info->start[i], |
| 290 | erased ? " E" : " ", |
| 291 | info->protect[i] ? "RO " : " "); |
| 292 | #else |
| 293 | if ((i % 5) == 0) |
| 294 | printf ("\n "); |
| 295 | printf (" %08lX%s", |
| 296 | info->start[i], |
| 297 | info->protect[i] ? " (RO)" : " "); |
| 298 | #endif |
| 299 | } |
| 300 | printf ("\n"); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | /*----------------------------------------------------------------------- |
| 305 | * Copy memory to flash, returns: |
| 306 | * 0 - OK |
| 307 | * 1 - write timeout |
| 308 | * 2 - Flash not erased |
| 309 | */ |
| 310 | int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 311 | { |
| 312 | ulong wp; |
| 313 | ulong cp; |
| 314 | int aln; |
| 315 | cfiword_t cword; |
| 316 | int i, rc; |
| 317 | |
| 318 | /* get lower aligned address */ |
| 319 | wp = (addr & ~(info->portwidth - 1)); |
| 320 | |
| 321 | /* handle unaligned start */ |
| 322 | if((aln = addr - wp) != 0) { |
| 323 | cword.l = 0; |
| 324 | cp = wp; |
| 325 | for(i=0;i<aln; ++i, ++cp) |
| 326 | flash_add_byte(info, &cword, (*(uchar *)cp)); |
| 327 | |
| 328 | for(; (i< info->portwidth) && (cnt > 0) ; i++) { |
| 329 | flash_add_byte(info, &cword, *src++); |
| 330 | cnt--; |
| 331 | cp++; |
| 332 | } |
| 333 | for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp) |
| 334 | flash_add_byte(info, &cword, (*(uchar *)cp)); |
| 335 | if((rc = flash_write_cfiword(info, wp, cword)) != 0) |
| 336 | return rc; |
| 337 | wp = cp; |
| 338 | } |
| 339 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 340 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 341 | while(cnt >= info->portwidth) { |
| 342 | i = info->buffer_size > cnt? cnt: info->buffer_size; |
| 343 | if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK) |
| 344 | return rc; |
| 345 | wp += i; |
| 346 | src += i; |
| 347 | cnt -=i; |
| 348 | } |
| 349 | #else |
| 350 | /* handle the aligned part */ |
| 351 | while(cnt >= info->portwidth) { |
| 352 | cword.l = 0; |
| 353 | for(i = 0; i < info->portwidth; i++) { |
| 354 | flash_add_byte(info, &cword, *src++); |
| 355 | } |
| 356 | if((rc = flash_write_cfiword(info, wp, cword)) != 0) |
| 357 | return rc; |
| 358 | wp += info->portwidth; |
| 359 | cnt -= info->portwidth; |
| 360 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 361 | #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */ |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 362 | if (cnt == 0) { |
| 363 | return (0); |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * handle unaligned tail bytes |
| 368 | */ |
| 369 | cword.l = 0; |
| 370 | for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) { |
| 371 | flash_add_byte(info, &cword, *src++); |
| 372 | --cnt; |
| 373 | } |
| 374 | for (; i<info->portwidth; ++i, ++cp) { |
| 375 | flash_add_byte(info, & cword, (*(uchar *)cp)); |
| 376 | } |
| 377 | |
| 378 | return flash_write_cfiword(info, wp, cword); |
| 379 | } |
| 380 | |
| 381 | /*----------------------------------------------------------------------- |
| 382 | */ |
| 383 | int flash_real_protect(flash_info_t *info, long sector, int prot) |
| 384 | { |
| 385 | int retcode = 0; |
| 386 | |
| 387 | flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 388 | flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT); |
| 389 | if(prot) |
| 390 | flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET); |
| 391 | else |
| 392 | flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR); |
| 393 | |
| 394 | if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout, |
| 395 | prot?"protect":"unprotect")) == 0) { |
| 396 | |
| 397 | info->protect[sector] = prot; |
| 398 | /* Intel's unprotect unprotects all locking */ |
| 399 | if(prot == 0) { |
| 400 | int i; |
| 401 | for(i = 0 ; i<info->sector_count; i++) { |
| 402 | if(info->protect[i]) |
| 403 | flash_real_protect(info, i, 1); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return retcode; |
| 409 | } |
| 410 | /*----------------------------------------------------------------------- |
| 411 | * wait for XSR.7 to be set. Time out with an error if it does not. |
| 412 | * This routine does not set the flash to read-array mode. |
| 413 | */ |
| 414 | static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt) |
| 415 | { |
| 416 | ulong start; |
| 417 | |
| 418 | /* Wait for command completion */ |
| 419 | start = get_timer (0); |
| 420 | while(!flash_isset(info, sector, 0, FLASH_STATUS_DONE)) { |
| 421 | if (get_timer(start) > info->erase_blk_tout) { |
| 422 | printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]); |
| 423 | flash_write_cmd(info, sector, 0, FLASH_CMD_RESET); |
| 424 | return ERR_TIMOUT; |
| 425 | } |
| 426 | } |
| 427 | return ERR_OK; |
| 428 | } |
| 429 | /*----------------------------------------------------------------------- |
| 430 | * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check. |
| 431 | * This routine sets the flash to read-array mode. |
| 432 | */ |
| 433 | static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt) |
| 434 | { |
| 435 | int retcode; |
| 436 | retcode = flash_status_check(info, sector, tout, prompt); |
| 437 | if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) { |
| 438 | retcode = ERR_INVAL; |
| 439 | printf("Flash %s error at address %lx\n", prompt,info->start[sector]); |
| 440 | if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){ |
| 441 | printf("Command Sequence Error.\n"); |
| 442 | } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){ |
| 443 | printf("Block Erase Error.\n"); |
wdenk | 07d7e6b | 2004-12-16 21:44:03 +0000 | [diff] [blame] | 444 | retcode = ERR_NOT_ERASED; |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 445 | } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) { |
| 446 | printf("Locking Error\n"); |
| 447 | } |
| 448 | if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){ |
| 449 | printf("Block locked.\n"); |
| 450 | retcode = ERR_PROTECTED; |
| 451 | } |
| 452 | if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS)) |
| 453 | printf("Vpp Low Error.\n"); |
| 454 | } |
| 455 | flash_write_cmd(info, sector, 0, FLASH_CMD_RESET); |
| 456 | return retcode; |
| 457 | } |
| 458 | /*----------------------------------------------------------------------- |
| 459 | */ |
| 460 | static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c) |
| 461 | { |
| 462 | switch(info->portwidth) { |
| 463 | case FLASH_CFI_8BIT: |
| 464 | cword->c = c; |
| 465 | break; |
| 466 | case FLASH_CFI_16BIT: |
| 467 | cword->w = (cword->w << 8) | c; |
| 468 | break; |
| 469 | case FLASH_CFI_32BIT: |
| 470 | cword->l = (cword->l << 8) | c; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | |
| 475 | /*----------------------------------------------------------------------- |
| 476 | * make a proper sized command based on the port and chip widths |
| 477 | */ |
| 478 | static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf) |
| 479 | { |
| 480 | int i; |
| 481 | uchar *cp = (uchar *)cmdbuf; |
| 482 | for(i=0; i< info->portwidth; i++) |
| 483 | *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Write a proper sized command to the correct address |
| 488 | */ |
| 489 | static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd) |
| 490 | { |
| 491 | |
| 492 | volatile cfiptr_t addr; |
| 493 | cfiword_t cword; |
| 494 | addr.cp = flash_make_addr(info, sect, offset); |
| 495 | flash_make_cmd(info, cmd, &cword); |
| 496 | switch(info->portwidth) { |
| 497 | case FLASH_CFI_8BIT: |
| 498 | *addr.cp = cword.c; |
| 499 | break; |
| 500 | case FLASH_CFI_16BIT: |
| 501 | *addr.wp = cword.w; |
| 502 | break; |
| 503 | case FLASH_CFI_32BIT: |
| 504 | *addr.lp = cword.l; |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /*----------------------------------------------------------------------- |
| 510 | */ |
| 511 | static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd) |
| 512 | { |
| 513 | cfiptr_t cptr; |
| 514 | cfiword_t cword; |
| 515 | int retval; |
| 516 | cptr.cp = flash_make_addr(info, sect, offset); |
| 517 | flash_make_cmd(info, cmd, &cword); |
| 518 | switch(info->portwidth) { |
| 519 | case FLASH_CFI_8BIT: |
| 520 | retval = (cptr.cp[0] == cword.c); |
| 521 | break; |
| 522 | case FLASH_CFI_16BIT: |
| 523 | retval = (cptr.wp[0] == cword.w); |
| 524 | break; |
| 525 | case FLASH_CFI_32BIT: |
| 526 | retval = (cptr.lp[0] == cword.l); |
| 527 | break; |
| 528 | default: |
| 529 | retval = 0; |
| 530 | break; |
| 531 | } |
| 532 | return retval; |
| 533 | } |
| 534 | /*----------------------------------------------------------------------- |
| 535 | */ |
| 536 | static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd) |
| 537 | { |
| 538 | cfiptr_t cptr; |
| 539 | cfiword_t cword; |
| 540 | int retval; |
| 541 | cptr.cp = flash_make_addr(info, sect, offset); |
| 542 | flash_make_cmd(info, cmd, &cword); |
| 543 | switch(info->portwidth) { |
| 544 | case FLASH_CFI_8BIT: |
| 545 | retval = ((cptr.cp[0] & cword.c) == cword.c); |
| 546 | break; |
| 547 | case FLASH_CFI_16BIT: |
| 548 | retval = ((cptr.wp[0] & cword.w) == cword.w); |
| 549 | break; |
| 550 | case FLASH_CFI_32BIT: |
| 551 | retval = ((cptr.lp[0] & cword.l) == cword.l); |
| 552 | break; |
| 553 | default: |
| 554 | retval = 0; |
| 555 | break; |
| 556 | } |
| 557 | return retval; |
| 558 | } |
| 559 | |
| 560 | /*----------------------------------------------------------------------- |
| 561 | * detect if flash is compatible with the Common Flash Interface (CFI) |
| 562 | * http://www.jedec.org/download/search/jesd68.pdf |
| 563 | * |
| 564 | */ |
| 565 | static int flash_detect_cfi(flash_info_t * info) |
| 566 | { |
| 567 | |
| 568 | for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_32BIT; |
| 569 | info->portwidth <<= 1) { |
| 570 | for(info->chipwidth =FLASH_CFI_BY8; |
| 571 | info->chipwidth <= info->portwidth; |
| 572 | info->chipwidth <<= 1) { |
| 573 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 574 | flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI); |
| 575 | if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') && |
| 576 | flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') && |
| 577 | flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) |
| 578 | return 1; |
| 579 | } |
| 580 | } |
| 581 | return 0; |
| 582 | } |
| 583 | /* |
| 584 | * The following code cannot be run from FLASH! |
| 585 | * |
| 586 | */ |
| 587 | static ulong flash_get_size (ulong base, int banknum) |
| 588 | { |
| 589 | flash_info_t * info = &flash_info[banknum]; |
| 590 | int i, j; |
| 591 | int sect_cnt; |
| 592 | unsigned long sector; |
| 593 | unsigned long tmp; |
| 594 | int size_ratio; |
| 595 | uchar num_erase_regions; |
| 596 | int erase_region_size; |
| 597 | int erase_region_count; |
| 598 | |
| 599 | info->start[0] = base; |
| 600 | |
| 601 | if(flash_detect_cfi(info)){ |
| 602 | #ifdef DEBUG_FLASH |
| 603 | printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */ |
| 604 | #endif |
| 605 | size_ratio = info->portwidth / info->chipwidth; |
| 606 | num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS); |
| 607 | #ifdef DEBUG_FLASH |
| 608 | printf("found %d erase regions\n", num_erase_regions); |
| 609 | #endif |
| 610 | sect_cnt = 0; |
| 611 | sector = base; |
| 612 | for(i = 0 ; i < num_erase_regions; i++) { |
| 613 | if(i > NUM_ERASE_REGIONS) { |
| 614 | printf("%d erase regions found, only %d used\n", |
| 615 | num_erase_regions, NUM_ERASE_REGIONS); |
| 616 | break; |
| 617 | } |
| 618 | tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS); |
| 619 | erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128; |
| 620 | tmp >>= 16; |
| 621 | erase_region_count = (tmp & 0xffff) +1; |
| 622 | for(j = 0; j< erase_region_count; j++) { |
| 623 | info->start[sect_cnt] = sector; |
| 624 | sector += (erase_region_size * size_ratio); |
| 625 | info->protect[sect_cnt] = flash_isset(info, sect_cnt, FLASH_OFFSET_PROTECT, FLASH_STATUS_PROTECT); |
| 626 | sect_cnt++; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | info->sector_count = sect_cnt; |
| 631 | /* multiply the size by the number of chips */ |
| 632 | info->size = (1 << flash_read_uchar(info, FLASH_OFFSET_SIZE)) * size_ratio; |
| 633 | info->buffer_size = (1 << flash_read_ushort(info, 0, FLASH_OFFSET_BUFFER_SIZE)); |
| 634 | tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_ETOUT); |
| 635 | info->erase_blk_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_EMAX_TOUT))); |
| 636 | tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WBTOUT); |
| 637 | info->buffer_write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WBMAX_TOUT))); |
| 638 | tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WTOUT); |
| 639 | info->write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WMAX_TOUT)))/ 1000; |
| 640 | info->flash_id = FLASH_MAN_CFI; |
| 641 | } |
| 642 | |
| 643 | flash_write_cmd(info, 0, 0, FLASH_CMD_RESET); |
| 644 | return(info->size); |
| 645 | } |
| 646 | |
| 647 | |
| 648 | /*----------------------------------------------------------------------- |
| 649 | */ |
| 650 | static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword) |
| 651 | { |
| 652 | |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 653 | cfiptr_t cptr; |
| 654 | int flag; |
| 655 | |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 656 | cptr.cp = (uchar *)dest; |
| 657 | |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 658 | /* Check if Flash is (sufficiently) erased */ |
| 659 | switch(info->portwidth) { |
| 660 | case FLASH_CFI_8BIT: |
| 661 | flag = ((cptr.cp[0] & cword.c) == cword.c); |
| 662 | break; |
| 663 | case FLASH_CFI_16BIT: |
| 664 | flag = ((cptr.wp[0] & cword.w) == cword.w); |
| 665 | break; |
| 666 | case FLASH_CFI_32BIT: |
| 667 | flag = ((cptr.lp[0] & cword.l) == cword.l); |
| 668 | break; |
| 669 | default: |
| 670 | return 2; |
| 671 | } |
| 672 | if(!flag) |
| 673 | return 2; |
| 674 | |
| 675 | /* Disable interrupts which might cause a timeout here */ |
| 676 | flag = disable_interrupts(); |
| 677 | |
| 678 | flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS); |
| 679 | flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE); |
| 680 | |
| 681 | switch(info->portwidth) { |
| 682 | case FLASH_CFI_8BIT: |
| 683 | cptr.cp[0] = cword.c; |
| 684 | break; |
| 685 | case FLASH_CFI_16BIT: |
| 686 | cptr.wp[0] = cword.w; |
| 687 | break; |
| 688 | case FLASH_CFI_32BIT: |
| 689 | cptr.lp[0] = cword.l; |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | /* re-enable interrupts if necessary */ |
| 694 | if(flag) |
| 695 | enable_interrupts(); |
| 696 | |
| 697 | return flash_full_status_check(info, 0, info->write_tout, "write"); |
| 698 | } |
| 699 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 700 | #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE |
stroese | 842053a | 2004-12-16 18:17:50 +0000 | [diff] [blame] | 701 | |
| 702 | /* loop through the sectors from the highest address |
| 703 | * when the passed address is greater or equal to the sector address |
| 704 | * we have a match |
| 705 | */ |
| 706 | static int find_sector(flash_info_t *info, ulong addr) |
| 707 | { |
| 708 | int sector; |
| 709 | for(sector = info->sector_count - 1; sector >= 0; sector--) { |
| 710 | if(addr >= info->start[sector]) |
| 711 | break; |
| 712 | } |
| 713 | return sector; |
| 714 | } |
| 715 | |
| 716 | static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len) |
| 717 | { |
| 718 | |
| 719 | int sector; |
| 720 | int cnt; |
| 721 | int retcode; |
| 722 | volatile cfiptr_t src; |
| 723 | volatile cfiptr_t dst; |
| 724 | |
| 725 | src.cp = cp; |
| 726 | dst.cp = (uchar *)dest; |
| 727 | sector = find_sector(info, dest); |
| 728 | flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 729 | flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER); |
| 730 | if((retcode = flash_status_check(info, sector, info->buffer_write_tout, |
| 731 | "write to buffer")) == ERR_OK) { |
| 732 | switch(info->portwidth) { |
| 733 | case FLASH_CFI_8BIT: |
| 734 | cnt = len; |
| 735 | break; |
| 736 | case FLASH_CFI_16BIT: |
| 737 | cnt = len >> 1; |
| 738 | if (len & 0x1) { /* test-only: unaligned size */ |
| 739 | puts("\nUnalgined size!!!\n"); /* test-only */ |
| 740 | cnt++; |
| 741 | } |
| 742 | break; |
| 743 | case FLASH_CFI_32BIT: |
| 744 | cnt = len >> 2; |
| 745 | break; |
| 746 | default: |
| 747 | return ERR_INVAL; |
| 748 | break; |
| 749 | } |
| 750 | flash_write_cmd(info, sector, 0, (uchar)cnt-1); |
| 751 | while(cnt-- > 0) { |
| 752 | switch(info->portwidth) { |
| 753 | case FLASH_CFI_8BIT: |
| 754 | *dst.cp++ = *src.cp++; |
| 755 | break; |
| 756 | case FLASH_CFI_16BIT: |
| 757 | *dst.wp++ = *src.wp++; |
| 758 | break; |
| 759 | case FLASH_CFI_32BIT: |
| 760 | *dst.lp++ = *src.lp++; |
| 761 | break; |
| 762 | default: |
| 763 | return ERR_INVAL; |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM); |
| 768 | retcode = flash_full_status_check(info, sector, info->buffer_write_tout, |
| 769 | "buffer write"); |
| 770 | } |
| 771 | flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS); |
| 772 | return retcode; |
| 773 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 774 | #endif /* CONFIG_SYS_USE_FLASH_BUFFER_WRITE */ |