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
88 #define FLASH_MAN_CFI 0x01000000
102 #define NUM_ERASE_REGIONS 4
104 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
106 /*-----------------------------------------------------------------------
110 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c);
111 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf);
112 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd);
113 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd);
114 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd);
115 static int flash_detect_cfi(flash_info_t * info);
116 static ulong flash_get_size (ulong base, int banknum);
117 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword);
118 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt);
119 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
120 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len);
122 /*-----------------------------------------------------------------------
123 * create an address based on the offset and the port width
125 inline uchar * flash_make_addr(flash_info_t * info, int sect, int offset)
127 return ((uchar *)(info->start[sect] + (offset * info->portwidth)));
129 /*-----------------------------------------------------------------------
130 * read a character at a port width address
132 inline uchar flash_read_uchar(flash_info_t * info, uchar offset)
135 cp = flash_make_addr(info, 0, offset);
136 return (cp[info->portwidth - 1]);
139 /*-----------------------------------------------------------------------
140 * read a short word by swapping for ppc format.
142 ushort flash_read_ushort(flash_info_t * info, int sect, uchar offset)
146 addr = flash_make_addr(info, sect, offset);
147 return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]);
151 /*-----------------------------------------------------------------------
152 * read a long word by picking the least significant byte of each maiximum
153 * port size word. Swap for ppc format.
155 ulong flash_read_long(flash_info_t * info, int sect, uchar offset)
159 addr = flash_make_addr(info, sect, offset);
160 return ( (addr[(2*info->portwidth) - 1] << 24 ) | (addr[(info->portwidth) -1] << 16) |
161 (addr[(4*info->portwidth) - 1] << 8) | addr[(3*info->portwidth) - 1]);
165 /*-----------------------------------------------------------------------
167 unsigned long flash_init (void)
171 unsigned long address;
174 /* The flash is positioned back to back, with the demultiplexing of the chip
175 * based on the A24 address line.
179 address = CONFIG_SYS_FLASH_BASE;
182 /* Init: no FLASHes known */
183 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
184 flash_info[i].flash_id = FLASH_UNKNOWN;
185 size += flash_info[i].size = flash_get_size(address, i);
186 address += CONFIG_SYS_FLASH_INCREMENT;
187 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
188 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",i,
189 flash_info[0].size, flash_info[i].size<<20);
193 #if 0 /* test-only */
194 /* Monitor protection ON by default */
195 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
196 for(i=0; flash_info[0].start[i] < CONFIG_SYS_MONITOR_BASE+CONFIG_SYS_MONITOR_LEN-1; i++)
197 (void)flash_real_protect(&flash_info[0], i, 1);
200 /* monitor protection ON by default */
201 flash_protect (FLAG_PROTECT_SET,
202 - CONFIG_SYS_MONITOR_LEN,
203 - 1, &flash_info[1]);
209 /*-----------------------------------------------------------------------
211 int flash_erase (flash_info_t *info, int s_first, int s_last)
217 if( info->flash_id != FLASH_MAN_CFI) {
218 printf ("Can't erase unknown flash type - aborted\n");
221 if ((s_first < 0) || (s_first > s_last)) {
222 printf ("- no sectors to erase\n");
227 for (sect=s_first; sect<=s_last; ++sect) {
228 if (info->protect[sect]) {
233 printf ("- Warning: %d protected sectors will not be erased!\n",
240 for (sect = s_first; sect<=s_last; sect++) {
241 if (info->protect[sect] == 0) { /* not protected */
242 flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
243 flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
244 flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
246 if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
256 /*-----------------------------------------------------------------------
258 void flash_print_info (flash_info_t *info)
262 if (info->flash_id != FLASH_MAN_CFI) {
263 printf ("missing or unknown FLASH type\n");
267 printf("CFI conformant FLASH (%d x %d)",
268 (info->portwidth << 3 ), (info->chipwidth << 3 ));
269 printf (" Size: %ld MB in %d Sectors\n",
270 info->size >> 20, info->sector_count);
271 printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
272 info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
274 printf (" Sector Start Addresses:");
275 for (i=0; i<info->sector_count; ++i) {
276 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
280 volatile unsigned long *flash;
283 * Check if whole sector is erased
285 if (i != (info->sector_count-1))
286 size = info->start[i+1] - info->start[i];
288 size = info->start[0] + info->size - info->start[i];
290 flash = (volatile unsigned long *)info->start[i];
291 size = size >> 2; /* divide by 4 for longword access */
292 for (k=0; k<size; k++)
294 if (*flash++ != 0xffffffff)
303 /* print empty and read-only info */
304 printf (" %08lX%s%s",
307 info->protect[i] ? "RO " : " ");
313 info->protect[i] ? " (RO)" : " ");
320 /*-----------------------------------------------------------------------
321 * Copy memory to flash, returns:
324 * 2 - Flash not erased
326 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
334 /* get lower aligned address */
335 wp = (addr & ~(info->portwidth - 1));
337 /* handle unaligned start */
338 if((aln = addr - wp) != 0) {
341 for(i=0;i<aln; ++i, ++cp)
342 flash_add_byte(info, &cword, (*(uchar *)cp));
344 for(; (i< info->portwidth) && (cnt > 0) ; i++) {
345 flash_add_byte(info, &cword, *src++);
349 for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
350 flash_add_byte(info, &cword, (*(uchar *)cp));
351 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
356 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
357 while(cnt >= info->portwidth) {
358 i = info->buffer_size > cnt? cnt: info->buffer_size;
359 if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK)
366 /* handle the aligned part */
367 while(cnt >= info->portwidth) {
369 for(i = 0; i < info->portwidth; i++) {
370 flash_add_byte(info, &cword, *src++);
372 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
374 wp += info->portwidth;
375 cnt -= info->portwidth;
377 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
383 * handle unaligned tail bytes
386 for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
387 flash_add_byte(info, &cword, *src++);
390 for (; i<info->portwidth; ++i, ++cp) {
391 flash_add_byte(info, & cword, (*(uchar *)cp));
394 return flash_write_cfiword(info, wp, cword);
397 /*-----------------------------------------------------------------------
399 int flash_real_protect(flash_info_t *info, long sector, int prot)
403 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
404 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
406 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
408 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
410 if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
411 prot?"protect":"unprotect")) == 0) {
413 info->protect[sector] = prot;
414 /* Intel's unprotect unprotects all locking */
417 for(i = 0 ; i<info->sector_count; i++) {
419 flash_real_protect(info, i, 1);
426 /*-----------------------------------------------------------------------
427 * wait for XSR.7 to be set. Time out with an error if it does not.
428 * This routine does not set the flash to read-array mode.
430 static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
434 /* Wait for command completion */
435 start = get_timer (0);
436 while(!flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
437 if (get_timer(start) > info->erase_blk_tout) {
438 printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]);
439 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
445 /*-----------------------------------------------------------------------
446 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
447 * This routine sets the flash to read-array mode.
449 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
452 retcode = flash_status_check(info, sector, tout, prompt);
453 if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
455 printf("Flash %s error at address %lx\n", prompt,info->start[sector]);
456 if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){
457 printf("Command Sequence Error.\n");
458 } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){
459 printf("Block Erase Error.\n");
460 retcode = ERR_NOT_ERASED;
461 } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
462 printf("Locking Error\n");
464 if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
465 printf("Block locked.\n");
466 retcode = ERR_PROTECTED;
468 if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
469 printf("Vpp Low Error.\n");
471 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
474 /*-----------------------------------------------------------------------
476 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
478 switch(info->portwidth) {
482 case FLASH_CFI_16BIT:
483 cword->w = (cword->w << 8) | c;
485 case FLASH_CFI_32BIT:
486 cword->l = (cword->l << 8) | c;
491 /*-----------------------------------------------------------------------
492 * make a proper sized command based on the port and chip widths
494 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
497 uchar *cp = (uchar *)cmdbuf;
498 for(i=0; i< info->portwidth; i++)
499 *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
503 * Write a proper sized command to the correct address
505 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
508 volatile cfiptr_t addr;
510 addr.cp = flash_make_addr(info, sect, offset);
511 flash_make_cmd(info, cmd, &cword);
512 switch(info->portwidth) {
516 case FLASH_CFI_16BIT:
519 case FLASH_CFI_32BIT:
525 /*-----------------------------------------------------------------------
527 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
532 cptr.cp = flash_make_addr(info, sect, offset);
533 flash_make_cmd(info, cmd, &cword);
534 switch(info->portwidth) {
536 retval = (cptr.cp[0] == cword.c);
538 case FLASH_CFI_16BIT:
539 retval = (cptr.wp[0] == cword.w);
541 case FLASH_CFI_32BIT:
542 retval = (cptr.lp[0] == cword.l);
550 /*-----------------------------------------------------------------------
552 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
557 cptr.cp = flash_make_addr(info, sect, offset);
558 flash_make_cmd(info, cmd, &cword);
559 switch(info->portwidth) {
561 retval = ((cptr.cp[0] & cword.c) == cword.c);
563 case FLASH_CFI_16BIT:
564 retval = ((cptr.wp[0] & cword.w) == cword.w);
566 case FLASH_CFI_32BIT:
567 retval = ((cptr.lp[0] & cword.l) == cword.l);
576 /*-----------------------------------------------------------------------
577 * detect if flash is compatible with the Common Flash Interface (CFI)
578 * http://www.jedec.org/download/search/jesd68.pdf
581 static int flash_detect_cfi(flash_info_t * info)
584 for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_32BIT;
585 info->portwidth <<= 1) {
586 for(info->chipwidth =FLASH_CFI_BY8;
587 info->chipwidth <= info->portwidth;
588 info->chipwidth <<= 1) {
589 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
590 flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
591 if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') &&
592 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
593 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y'))
600 * The following code cannot be run from FLASH!
603 static ulong flash_get_size (ulong base, int banknum)
605 flash_info_t * info = &flash_info[banknum];
608 unsigned long sector;
611 uchar num_erase_regions;
612 int erase_region_size;
613 int erase_region_count;
615 info->start[0] = base;
617 if(flash_detect_cfi(info)){
619 printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */
621 size_ratio = info->portwidth / info->chipwidth;
622 num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
624 printf("found %d erase regions\n", num_erase_regions);
628 for(i = 0 ; i < num_erase_regions; i++) {
629 if(i > NUM_ERASE_REGIONS) {
630 printf("%d erase regions found, only %d used\n",
631 num_erase_regions, NUM_ERASE_REGIONS);
634 tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS);
635 erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128;
637 erase_region_count = (tmp & 0xffff) +1;
638 for(j = 0; j< erase_region_count; j++) {
639 info->start[sect_cnt] = sector;
640 sector += (erase_region_size * size_ratio);
641 info->protect[sect_cnt] = flash_isset(info, sect_cnt, FLASH_OFFSET_PROTECT, FLASH_STATUS_PROTECT);
646 info->sector_count = sect_cnt;
647 /* multiply the size by the number of chips */
648 info->size = (1 << flash_read_uchar(info, FLASH_OFFSET_SIZE)) * size_ratio;
649 info->buffer_size = (1 << flash_read_ushort(info, 0, FLASH_OFFSET_BUFFER_SIZE));
650 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_ETOUT);
651 info->erase_blk_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_EMAX_TOUT)));
652 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WBTOUT);
653 info->buffer_write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WBMAX_TOUT)));
654 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WTOUT);
655 info->write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WMAX_TOUT)))/ 1000;
656 info->flash_id = FLASH_MAN_CFI;
659 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
664 /*-----------------------------------------------------------------------
666 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
673 ctladdr.cp = flash_make_addr(info, 0, 0);
674 cptr.cp = (uchar *)dest;
677 /* Check if Flash is (sufficiently) erased */
678 switch(info->portwidth) {
680 flag = ((cptr.cp[0] & cword.c) == cword.c);
682 case FLASH_CFI_16BIT:
683 flag = ((cptr.wp[0] & cword.w) == cword.w);
685 case FLASH_CFI_32BIT:
686 flag = ((cptr.lp[0] & cword.l) == cword.l);
694 /* Disable interrupts which might cause a timeout here */
695 flag = disable_interrupts();
697 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
698 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
700 switch(info->portwidth) {
702 cptr.cp[0] = cword.c;
704 case FLASH_CFI_16BIT:
705 cptr.wp[0] = cword.w;
707 case FLASH_CFI_32BIT:
708 cptr.lp[0] = cword.l;
712 /* re-enable interrupts if necessary */
716 return flash_full_status_check(info, 0, info->write_tout, "write");
719 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
721 /* loop through the sectors from the highest address
722 * when the passed address is greater or equal to the sector address
725 static int find_sector(flash_info_t *info, ulong addr)
728 for(sector = info->sector_count - 1; sector >= 0; sector--) {
729 if(addr >= info->start[sector])
735 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
741 volatile cfiptr_t src;
742 volatile cfiptr_t dst;
745 dst.cp = (uchar *)dest;
746 sector = find_sector(info, dest);
747 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
748 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
749 if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
750 "write to buffer")) == ERR_OK) {
751 switch(info->portwidth) {
755 case FLASH_CFI_16BIT:
757 if (len & 0x1) { /* test-only: unaligned size */
758 puts("\nUnalgined size!!!\n"); /* test-only */
762 case FLASH_CFI_32BIT:
769 flash_write_cmd(info, sector, 0, (uchar)cnt-1);
771 switch(info->portwidth) {
773 *dst.cp++ = *src.cp++;
775 case FLASH_CFI_16BIT:
776 *dst.wp++ = *src.wp++;
778 case FLASH_CFI_32BIT:
779 *dst.lp++ = *src.lp++;
786 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
787 retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
790 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
793 #endif /* CONFIG_SYS_USE_FLASH_BUFFER_WRITE */