3 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include <asm/processor.h>
29 * This file implements a Common Flash Interface (CFI) driver for ppcboot.
30 * The width of the port and the width of the chips are determined at initialization.
31 * These widths are used to calculate the address for access CFI data structures.
32 * It has been tested on an Intel Strataflash implementation.
35 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
36 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
37 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
38 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
41 * Use Primary Extended Query table (PRI) and Alternate Algorithm Query Table (ALT) to determine if protection is available
42 * Add support for other command sets Use the PRI and ALT to determine command set
43 * Verify erase and program timeouts.
46 #define FLASH_CMD_CFI 0x98
47 #define FLASH_CMD_READ_ID 0x90
48 #define FLASH_CMD_RESET 0xff
49 #define FLASH_CMD_BLOCK_ERASE 0x20
50 #define FLASH_CMD_ERASE_CONFIRM 0xD0
51 #define FLASH_CMD_WRITE 0x40
52 #define FLASH_CMD_PROTECT 0x60
53 #define FLASH_CMD_PROTECT_SET 0x01
54 #define FLASH_CMD_PROTECT_CLEAR 0xD0
55 #define FLASH_CMD_CLEAR_STATUS 0x50
56 #define FLASH_CMD_WRITE_TO_BUFFER 0xE8
57 #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
59 #define FLASH_STATUS_DONE 0x80
60 #define FLASH_STATUS_ESS 0x40
61 #define FLASH_STATUS_ECLBS 0x20
62 #define FLASH_STATUS_PSLBS 0x10
63 #define FLASH_STATUS_VPENS 0x08
64 #define FLASH_STATUS_PSS 0x04
65 #define FLASH_STATUS_DPS 0x02
66 #define FLASH_STATUS_R 0x01
67 #define FLASH_STATUS_PROTECT 0x01
69 #define FLASH_OFFSET_CFI 0x55
70 #define FLASH_OFFSET_CFI_RESP 0x10
71 #define FLASH_OFFSET_WTOUT 0x1F
72 #define FLASH_OFFSET_WBTOUT 0x20
73 #define FLASH_OFFSET_ETOUT 0x21
74 #define FLASH_OFFSET_CETOUT 0x22
75 #define FLASH_OFFSET_WMAX_TOUT 0x23
76 #define FLASH_OFFSET_WBMAX_TOUT 0x24
77 #define FLASH_OFFSET_EMAX_TOUT 0x25
78 #define FLASH_OFFSET_CEMAX_TOUT 0x26
79 #define FLASH_OFFSET_SIZE 0x27
80 #define FLASH_OFFSET_INTERFACE 0x28
81 #define FLASH_OFFSET_BUFFER_SIZE 0x2A
82 #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
83 #define FLASH_OFFSET_ERASE_REGIONS 0x2D
84 #define FLASH_OFFSET_PROTECT 0x02
85 #define FLASH_OFFSET_USER_PROTECTION 0x85
86 #define FLASH_OFFSET_INTEL_PROTECTION 0x81
89 #define FLASH_MAN_CFI 0x01000000
104 #define NUM_ERASE_REGIONS 4
106 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
109 /*-----------------------------------------------------------------------
114 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c);
115 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf);
116 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd);
117 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd);
118 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd);
119 static int flash_detect_cfi(flash_info_t * info);
120 static ulong flash_get_size (ulong base, int banknum);
121 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword);
122 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt);
123 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
124 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len);
126 /*-----------------------------------------------------------------------
127 * create an address based on the offset and the port width
129 inline uchar * flash_make_addr(flash_info_t * info, int sect, int offset)
131 return ((uchar *)(info->start[sect] + (offset * info->portwidth)));
133 /*-----------------------------------------------------------------------
134 * read a character at a port width address
136 inline uchar flash_read_uchar(flash_info_t * info, uchar offset)
139 cp = flash_make_addr(info, 0, offset);
140 return (cp[info->portwidth - 1]);
143 /*-----------------------------------------------------------------------
144 * read a short word by swapping for ppc format.
146 ushort flash_read_ushort(flash_info_t * info, int sect, uchar offset)
150 addr = flash_make_addr(info, sect, offset);
151 return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]);
155 /*-----------------------------------------------------------------------
156 * read a long word by picking the least significant byte of each maiximum
157 * port size word. Swap for ppc format.
159 ulong flash_read_long(flash_info_t * info, int sect, uchar offset)
163 addr = flash_make_addr(info, sect, offset);
164 return ( (addr[(2*info->portwidth) - 1] << 24 ) | (addr[(info->portwidth) -1] << 16) |
165 (addr[(4*info->portwidth) - 1] << 8) | addr[(3*info->portwidth) - 1]);
169 /*-----------------------------------------------------------------------
171 unsigned long flash_init (void)
175 unsigned long address;
178 /* The flash is positioned back to back, with the demultiplexing of the chip
179 * based on the A24 address line.
183 address = CONFIG_SYS_FLASH_BASE;
186 /* Init: no FLASHes known */
187 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
188 flash_info[i].flash_id = FLASH_UNKNOWN;
189 size += flash_info[i].size = flash_get_size(address, i);
190 address += CONFIG_SYS_FLASH_INCREMENT;
191 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
192 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",i,
193 flash_info[0].size, flash_info[i].size<<20);
197 #if 0 /* test-only */
198 /* Monitor protection ON by default */
199 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
200 for(i=0; flash_info[0].start[i] < CONFIG_SYS_MONITOR_BASE+CONFIG_SYS_MONITOR_LEN-1; i++)
201 (void)flash_real_protect(&flash_info[0], i, 1);
204 /* monitor protection ON by default */
205 flash_protect (FLAG_PROTECT_SET,
206 - CONFIG_SYS_MONITOR_LEN,
207 - 1, &flash_info[1]);
213 /*-----------------------------------------------------------------------
215 int flash_erase (flash_info_t *info, int s_first, int s_last)
221 if( info->flash_id != FLASH_MAN_CFI) {
222 printf ("Can't erase unknown flash type - aborted\n");
225 if ((s_first < 0) || (s_first > s_last)) {
226 printf ("- no sectors to erase\n");
231 for (sect=s_first; sect<=s_last; ++sect) {
232 if (info->protect[sect]) {
237 printf ("- Warning: %d protected sectors will not be erased!\n",
244 for (sect = s_first; sect<=s_last; sect++) {
245 if (info->protect[sect] == 0) { /* not protected */
246 flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
247 flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
248 flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
250 if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
260 /*-----------------------------------------------------------------------
262 void flash_print_info (flash_info_t *info)
266 if (info->flash_id != FLASH_MAN_CFI) {
267 printf ("missing or unknown FLASH type\n");
271 printf("CFI conformant FLASH (%d x %d)",
272 (info->portwidth << 3 ), (info->chipwidth << 3 ));
273 printf (" Size: %ld MB in %d Sectors\n",
274 info->size >> 20, info->sector_count);
275 printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
276 info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
278 printf (" Sector Start Addresses:");
279 for (i=0; i<info->sector_count; ++i) {
280 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
284 volatile unsigned long *flash;
287 * Check if whole sector is erased
289 if (i != (info->sector_count-1))
290 size = info->start[i+1] - info->start[i];
292 size = info->start[0] + info->size - info->start[i];
294 flash = (volatile unsigned long *)info->start[i];
295 size = size >> 2; /* divide by 4 for longword access */
296 for (k=0; k<size; k++)
298 if (*flash++ != 0xffffffff)
307 /* print empty and read-only info */
308 printf (" %08lX%s%s",
311 info->protect[i] ? "RO " : " ");
317 info->protect[i] ? " (RO)" : " ");
324 /*-----------------------------------------------------------------------
325 * Copy memory to flash, returns:
328 * 2 - Flash not erased
330 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
338 /* get lower aligned address */
339 wp = (addr & ~(info->portwidth - 1));
341 /* handle unaligned start */
342 if((aln = addr - wp) != 0) {
345 for(i=0;i<aln; ++i, ++cp)
346 flash_add_byte(info, &cword, (*(uchar *)cp));
348 for(; (i< info->portwidth) && (cnt > 0) ; i++) {
349 flash_add_byte(info, &cword, *src++);
353 for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
354 flash_add_byte(info, &cword, (*(uchar *)cp));
355 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
360 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
361 while(cnt >= info->portwidth) {
362 i = info->buffer_size > cnt? cnt: info->buffer_size;
363 if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK)
370 /* handle the aligned part */
371 while(cnt >= info->portwidth) {
373 for(i = 0; i < info->portwidth; i++) {
374 flash_add_byte(info, &cword, *src++);
376 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
378 wp += info->portwidth;
379 cnt -= info->portwidth;
381 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
387 * handle unaligned tail bytes
390 for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
391 flash_add_byte(info, &cword, *src++);
394 for (; i<info->portwidth; ++i, ++cp) {
395 flash_add_byte(info, & cword, (*(uchar *)cp));
398 return flash_write_cfiword(info, wp, cword);
401 /*-----------------------------------------------------------------------
403 int flash_real_protect(flash_info_t *info, long sector, int prot)
407 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
408 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
410 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
412 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
414 if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
415 prot?"protect":"unprotect")) == 0) {
417 info->protect[sector] = prot;
418 /* Intel's unprotect unprotects all locking */
421 for(i = 0 ; i<info->sector_count; i++) {
423 flash_real_protect(info, i, 1);
430 /*-----------------------------------------------------------------------
431 * wait for XSR.7 to be set. Time out with an error if it does not.
432 * This routine does not set the flash to read-array mode.
434 static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
438 /* Wait for command completion */
439 start = get_timer (0);
440 while(!flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
441 if (get_timer(start) > info->erase_blk_tout) {
442 printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]);
443 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
449 /*-----------------------------------------------------------------------
450 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
451 * This routine sets the flash to read-array mode.
453 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
456 retcode = flash_status_check(info, sector, tout, prompt);
457 if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
459 printf("Flash %s error at address %lx\n", prompt,info->start[sector]);
460 if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){
461 printf("Command Sequence Error.\n");
462 } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){
463 printf("Block Erase Error.\n");
464 retcode = ERR_NOT_ERASED;
465 } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
466 printf("Locking Error\n");
468 if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
469 printf("Block locked.\n");
470 retcode = ERR_PROTECTED;
472 if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
473 printf("Vpp Low Error.\n");
475 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
478 /*-----------------------------------------------------------------------
480 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
482 switch(info->portwidth) {
486 case FLASH_CFI_16BIT:
487 cword->w = (cword->w << 8) | c;
489 case FLASH_CFI_32BIT:
490 cword->l = (cword->l << 8) | c;
495 /*-----------------------------------------------------------------------
496 * make a proper sized command based on the port and chip widths
498 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
501 uchar *cp = (uchar *)cmdbuf;
502 for(i=0; i< info->portwidth; i++)
503 *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
507 * Write a proper sized command to the correct address
509 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
512 volatile cfiptr_t addr;
514 addr.cp = flash_make_addr(info, sect, offset);
515 flash_make_cmd(info, cmd, &cword);
516 switch(info->portwidth) {
520 case FLASH_CFI_16BIT:
523 case FLASH_CFI_32BIT:
529 /*-----------------------------------------------------------------------
531 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
536 cptr.cp = flash_make_addr(info, sect, offset);
537 flash_make_cmd(info, cmd, &cword);
538 switch(info->portwidth) {
540 retval = (cptr.cp[0] == cword.c);
542 case FLASH_CFI_16BIT:
543 retval = (cptr.wp[0] == cword.w);
545 case FLASH_CFI_32BIT:
546 retval = (cptr.lp[0] == cword.l);
554 /*-----------------------------------------------------------------------
556 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
561 cptr.cp = flash_make_addr(info, sect, offset);
562 flash_make_cmd(info, cmd, &cword);
563 switch(info->portwidth) {
565 retval = ((cptr.cp[0] & cword.c) == cword.c);
567 case FLASH_CFI_16BIT:
568 retval = ((cptr.wp[0] & cword.w) == cword.w);
570 case FLASH_CFI_32BIT:
571 retval = ((cptr.lp[0] & cword.l) == cword.l);
580 /*-----------------------------------------------------------------------
581 * detect if flash is compatible with the Common Flash Interface (CFI)
582 * http://www.jedec.org/download/search/jesd68.pdf
585 static int flash_detect_cfi(flash_info_t * info)
588 for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_32BIT;
589 info->portwidth <<= 1) {
590 for(info->chipwidth =FLASH_CFI_BY8;
591 info->chipwidth <= info->portwidth;
592 info->chipwidth <<= 1) {
593 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
594 flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
595 if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') &&
596 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
597 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y'))
604 * The following code cannot be run from FLASH!
607 static ulong flash_get_size (ulong base, int banknum)
609 flash_info_t * info = &flash_info[banknum];
612 unsigned long sector;
615 uchar num_erase_regions;
616 int erase_region_size;
617 int erase_region_count;
619 info->start[0] = base;
621 if(flash_detect_cfi(info)){
623 printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */
625 size_ratio = info->portwidth / info->chipwidth;
626 num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
628 printf("found %d erase regions\n", num_erase_regions);
632 for(i = 0 ; i < num_erase_regions; i++) {
633 if(i > NUM_ERASE_REGIONS) {
634 printf("%d erase regions found, only %d used\n",
635 num_erase_regions, NUM_ERASE_REGIONS);
638 tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS);
639 erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128;
641 erase_region_count = (tmp & 0xffff) +1;
642 for(j = 0; j< erase_region_count; j++) {
643 info->start[sect_cnt] = sector;
644 sector += (erase_region_size * size_ratio);
645 info->protect[sect_cnt] = flash_isset(info, sect_cnt, FLASH_OFFSET_PROTECT, FLASH_STATUS_PROTECT);
650 info->sector_count = sect_cnt;
651 /* multiply the size by the number of chips */
652 info->size = (1 << flash_read_uchar(info, FLASH_OFFSET_SIZE)) * size_ratio;
653 info->buffer_size = (1 << flash_read_ushort(info, 0, FLASH_OFFSET_BUFFER_SIZE));
654 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_ETOUT);
655 info->erase_blk_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_EMAX_TOUT)));
656 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WBTOUT);
657 info->buffer_write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WBMAX_TOUT)));
658 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WTOUT);
659 info->write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WMAX_TOUT)))/ 1000;
660 info->flash_id = FLASH_MAN_CFI;
663 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
668 /*-----------------------------------------------------------------------
670 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
676 cptr.cp = (uchar *)dest;
678 /* Check if Flash is (sufficiently) erased */
679 switch(info->portwidth) {
681 flag = ((cptr.cp[0] & cword.c) == cword.c);
683 case FLASH_CFI_16BIT:
684 flag = ((cptr.wp[0] & cword.w) == cword.w);
686 case FLASH_CFI_32BIT:
687 flag = ((cptr.lp[0] & cword.l) == cword.l);
695 /* Disable interrupts which might cause a timeout here */
696 flag = disable_interrupts();
698 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
699 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
701 switch(info->portwidth) {
703 cptr.cp[0] = cword.c;
705 case FLASH_CFI_16BIT:
706 cptr.wp[0] = cword.w;
708 case FLASH_CFI_32BIT:
709 cptr.lp[0] = cword.l;
713 /* re-enable interrupts if necessary */
717 return flash_full_status_check(info, 0, info->write_tout, "write");
720 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
722 /* loop through the sectors from the highest address
723 * when the passed address is greater or equal to the sector address
726 static int find_sector(flash_info_t *info, ulong addr)
729 for(sector = info->sector_count - 1; sector >= 0; sector--) {
730 if(addr >= info->start[sector])
736 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
742 volatile cfiptr_t src;
743 volatile cfiptr_t dst;
746 dst.cp = (uchar *)dest;
747 sector = find_sector(info, dest);
748 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
749 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
750 if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
751 "write to buffer")) == ERR_OK) {
752 switch(info->portwidth) {
756 case FLASH_CFI_16BIT:
759 case FLASH_CFI_32BIT:
766 flash_write_cmd(info, sector, 0, (uchar)cnt-1);
768 switch(info->portwidth) {
770 *dst.cp++ = *src.cp++;
772 case FLASH_CFI_16BIT:
773 *dst.wp++ = *src.wp++;
775 case FLASH_CFI_32BIT:
776 *dst.lp++ = *src.lp++;
783 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
784 retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
787 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
790 #endif /* CONFIG_SYS_USE_FLASH_BUFFER_WRITE */