3 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
5 * Copyright (C) 2003 Arabella Software Ltd.
6 * Yuli Barcohen <yuli@arabellasw.com>
7 * Modified to work with AMD flashes
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * 01/20/2004 - combined variants of original driver.
30 * Tested Architectures
31 * Port Width Chip Width # of banks Flash Chip Board
32 * 32 16 1 23F128J3 seranoa/eagle
36 /* The DEBUG define must be before common to enable debugging */
39 #include <asm/processor.h>
40 #ifdef CFG_FLASH_CFI_DRIVER
42 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
43 * The width of the port and the width of the chips are determined at initialization.
44 * These widths are used to calculate the address for access CFI data structures.
45 * It has been tested on an Intel Strataflash implementation and AMD 29F016D.
48 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
49 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
50 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
51 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
55 * Use Primary Extended Query table (PRI) and Alternate Algorithm Query
56 * Table (ALT) to determine if protection is available
58 * Add support for other command sets Use the PRI and ALT to determine command set
59 * Verify erase and program timeouts.
62 #define FLASH_CMD_CFI 0x98
63 #define FLASH_CMD_READ_ID 0x90
64 #define FLASH_CMD_RESET 0xff
65 #define FLASH_CMD_BLOCK_ERASE 0x20
66 #define FLASH_CMD_ERASE_CONFIRM 0xD0
67 #define FLASH_CMD_WRITE 0x40
68 #define FLASH_CMD_PROTECT 0x60
69 #define FLASH_CMD_PROTECT_SET 0x01
70 #define FLASH_CMD_PROTECT_CLEAR 0xD0
71 #define FLASH_CMD_CLEAR_STATUS 0x50
72 #define FLASH_CMD_WRITE_TO_BUFFER 0xE8
73 #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
75 #define FLASH_STATUS_DONE 0x80
76 #define FLASH_STATUS_ESS 0x40
77 #define FLASH_STATUS_ECLBS 0x20
78 #define FLASH_STATUS_PSLBS 0x10
79 #define FLASH_STATUS_VPENS 0x08
80 #define FLASH_STATUS_PSS 0x04
81 #define FLASH_STATUS_DPS 0x02
82 #define FLASH_STATUS_R 0x01
83 #define FLASH_STATUS_PROTECT 0x01
85 #define AMD_CMD_RESET 0xF0
86 #define AMD_CMD_WRITE 0xA0
87 #define AMD_CMD_ERASE_START 0x80
88 #define AMD_CMD_ERASE_SECTOR 0x30
90 #define AMD_STATUS_TOGGLE 0x40
91 #define AMD_STATUS_ERROR 0x20
93 #define FLASH_OFFSET_CFI 0x55
94 #define FLASH_OFFSET_CFI_RESP 0x10
95 #define FLASH_OFFSET_PRIMARY_VENDOR 0x13
96 #define FLASH_OFFSET_WTOUT 0x1F
97 #define FLASH_OFFSET_WBTOUT 0x20
98 #define FLASH_OFFSET_ETOUT 0x21
99 #define FLASH_OFFSET_CETOUT 0x22
100 #define FLASH_OFFSET_WMAX_TOUT 0x23
101 #define FLASH_OFFSET_WBMAX_TOUT 0x24
102 #define FLASH_OFFSET_EMAX_TOUT 0x25
103 #define FLASH_OFFSET_CEMAX_TOUT 0x26
104 #define FLASH_OFFSET_SIZE 0x27
105 #define FLASH_OFFSET_INTERFACE 0x28
106 #define FLASH_OFFSET_BUFFER_SIZE 0x2A
107 #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
108 #define FLASH_OFFSET_ERASE_REGIONS 0x2D
109 #define FLASH_OFFSET_PROTECT 0x02
110 #define FLASH_OFFSET_USER_PROTECTION 0x85
111 #define FLASH_OFFSET_INTEL_PROTECTION 0x81
114 #define FLASH_MAN_CFI 0x01000000
116 #define CFI_CMDSET_NONE 0
117 #define CFI_CMDSET_INTEL_EXTENDED 1
118 #define CFI_CMDSET_AMD_STANDARD 2
119 #define CFI_CMDSET_INTEL_STANDARD 3
120 #define CFI_CMDSET_AMD_EXTENDED 4
121 #define CFI_CMDSET_MITSU_STANDARD 256
122 #define CFI_CMDSET_MITSU_EXTENDED 257
123 #define CFI_CMDSET_SST 258
130 unsigned long long ll;
134 volatile unsigned char *cp;
135 volatile unsigned short *wp;
136 volatile unsigned long *lp;
137 volatile unsigned long long *llp;
140 #define NUM_ERASE_REGIONS 4
142 static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
144 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
146 /*-----------------------------------------------------------------------
150 typedef unsigned long flash_sect_t;
152 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c);
153 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf);
154 static void flash_write_cmd(flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
155 static void flash_unlock_seq(flash_info_t *info);
156 static int flash_isequal(flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
157 static int flash_isset(flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
158 static int flash_toggle(flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
159 static int flash_detect_cfi(flash_info_t * info);
160 static ulong flash_get_size (ulong base, int banknum);
161 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword);
162 static int flash_full_status_check(flash_info_t * info, flash_sect_t sector, ulong tout, char * prompt);
163 #ifdef CFG_FLASH_USE_BUFFER_WRITE
164 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len);
168 void print_longlong(char * str, unsigned long long data)
172 cp = (unsigned char *)&data;
174 sprintf(&str[i*2], "%2.2x", *cp++);
179 /*-----------------------------------------------------------------------
180 * create an address based on the offset and the port width
182 inline uchar * flash_make_addr(flash_info_t * info, flash_sect_t sect, uint offset)
184 return ((uchar *)(info->start[sect] + (offset * info->portwidth)));
186 /*-----------------------------------------------------------------------
187 * read a character at a port width address
189 inline uchar flash_read_uchar(flash_info_t * info, uint offset)
192 cp = flash_make_addr(info, 0, offset);
193 return (cp[info->portwidth - 1]);
196 /*-----------------------------------------------------------------------
197 * read a short word by swapping for ppc format.
199 ushort flash_read_ushort(flash_info_t * info, flash_sect_t sect, uint offset)
203 addr = flash_make_addr(info, sect, offset);
204 return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]);
208 /*-----------------------------------------------------------------------
209 * read a long word by picking the least significant byte of each maiximum
210 * port size word. Swap for ppc format.
212 ulong flash_read_long(flash_info_t * info, flash_sect_t sect, uint offset)
216 addr = flash_make_addr(info, sect, offset);
217 return ( (addr[(2*info->portwidth) - 1] << 24 ) | (addr[(info->portwidth) -1] << 16) |
218 (addr[(4*info->portwidth) - 1] << 8) | addr[(3*info->portwidth) - 1]);
222 /*-----------------------------------------------------------------------
224 unsigned long flash_init (void)
226 unsigned long size = 0;
229 /* Init: no FLASHes known */
230 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
231 flash_info[i].flash_id = FLASH_UNKNOWN;
232 size += flash_info[i].size = flash_get_size(bank_base[i], i);
233 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
234 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
235 i, flash_info[i].size, flash_info[i].size << 20);
239 /* Monitor protection ON by default */
240 #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
241 flash_protect(FLAG_PROTECT_SET,
243 CFG_MONITOR_BASE + monitor_flash_len - 1,
250 /*-----------------------------------------------------------------------
252 int flash_erase (flash_info_t *info, int s_first, int s_last)
258 if( info->flash_id != FLASH_MAN_CFI) {
259 printf ("Can't erase unknown flash type - aborted\n");
262 if ((s_first < 0) || (s_first > s_last)) {
263 printf ("- no sectors to erase\n");
268 for (sect=s_first; sect<=s_last; ++sect) {
269 if (info->protect[sect]) {
274 printf ("- Warning: %d protected sectors will not be erased!\n",
281 for (sect = s_first; sect<=s_last; sect++) {
282 if (info->protect[sect] == 0) { /* not protected */
283 switch(info->vendor) {
284 case CFI_CMDSET_INTEL_STANDARD:
285 case CFI_CMDSET_INTEL_EXTENDED:
286 flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
287 flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
288 flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
290 case CFI_CMDSET_AMD_STANDARD:
291 case CFI_CMDSET_AMD_EXTENDED:
292 flash_unlock_seq(info);
293 flash_write_cmd(info, sect, 0x555, AMD_CMD_ERASE_START);
294 flash_unlock_seq(info);
295 flash_write_cmd(info, sect, 0, AMD_CMD_ERASE_SECTOR);
298 debug("Unkown flash vendor %d\n", info->vendor);
302 if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
312 /*-----------------------------------------------------------------------
314 void flash_print_info (flash_info_t *info)
318 if (info->flash_id != FLASH_MAN_CFI) {
319 printf ("missing or unknown FLASH type\n");
323 printf("CFI conformant FLASH (%d x %d)",
324 (info->portwidth << 3 ), (info->chipwidth << 3 ));
325 printf (" Size: %ld MB in %d Sectors\n",
326 info->size >> 20, info->sector_count);
327 printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
328 info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
330 printf (" Sector Start Addresses:");
331 for (i=0; i<info->sector_count; ++i) {
332 #ifdef CFG_FLASH_EMPTY_INFO
336 volatile unsigned long *flash;
339 * Check if whole sector is erased
341 if (i != (info->sector_count-1))
342 size = info->start[i+1] - info->start[i];
344 size = info->start[0] + info->size - info->start[i];
346 flash = (volatile unsigned long *)info->start[i];
347 size = size >> 2; /* divide by 4 for longword access */
348 for (k=0; k<size; k++)
350 if (*flash++ != 0xffffffff)
359 /* print empty and read-only info */
360 printf (" %08lX%s%s",
363 info->protect[i] ? "RO " : " ");
369 info->protect[i] ? " (RO)" : " ");
376 /*-----------------------------------------------------------------------
377 * Copy memory to flash, returns:
380 * 2 - Flash not erased
382 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
390 /* get lower aligned address */
391 wp = (addr & ~(info->portwidth - 1));
393 /* handle unaligned start */
394 if((aln = addr - wp) != 0) {
397 for(i=0;i<aln; ++i, ++cp)
398 flash_add_byte(info, &cword, (*(uchar *)cp));
400 for(; (i< info->portwidth) && (cnt > 0) ; i++) {
401 flash_add_byte(info, &cword, *src++);
405 for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
406 flash_add_byte(info, &cword, (*(uchar *)cp));
407 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
412 #ifdef CFG_FLASH_USE_BUFFER_WRITE
413 while(cnt >= info->portwidth) {
414 i = info->buffer_size > cnt? cnt: info->buffer_size;
415 if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK)
422 /* handle the aligned part */
423 while(cnt >= info->portwidth) {
425 for(i = 0; i < info->portwidth; i++) {
426 flash_add_byte(info, &cword, *src++);
428 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
430 wp += info->portwidth;
431 cnt -= info->portwidth;
433 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
439 * handle unaligned tail bytes
442 for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
443 flash_add_byte(info, &cword, *src++);
446 for (; i<info->portwidth; ++i, ++cp) {
447 flash_add_byte(info, & cword, (*(uchar *)cp));
450 return flash_write_cfiword(info, wp, cword);
453 /*-----------------------------------------------------------------------
455 #ifdef CFG_FLASH_PROTECTION
457 int flash_real_protect(flash_info_t *info, long sector, int prot)
461 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
462 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
464 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
466 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
468 if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
469 prot?"protect":"unprotect")) == 0) {
471 info->protect[sector] = prot;
472 /* Intel's unprotect unprotects all locking */
475 for(i = 0 ; i<info->sector_count; i++) {
477 flash_real_protect(info, i, 1);
484 /*-----------------------------------------------------------------------
485 * flash_read_user_serial - read the OneTimeProgramming cells
487 void flash_read_user_serial(flash_info_t * info, void * buffer, int offset, int len)
493 src = flash_make_addr(info, 0, FLASH_OFFSET_USER_PROTECTION);
494 flash_write_cmd(info,0, 0, FLASH_CMD_READ_ID);
495 memcpy(dst,src + offset,len);
496 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
499 * flash_read_factory_serial - read the device Id from the protection area
501 void flash_read_factory_serial(flash_info_t * info, void * buffer, int offset, int len)
505 src = flash_make_addr(info, 0, FLASH_OFFSET_INTEL_PROTECTION);
506 flash_write_cmd(info,0, 0, FLASH_CMD_READ_ID);
507 memcpy(buffer,src + offset,len);
508 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
511 #endif /* CFG_FLASH_PROTECTION */
513 static int flash_poll_status(flash_info_t * info, flash_sect_t sect)
516 switch(info->vendor) {
517 case CFI_CMDSET_INTEL_STANDARD:
518 case CFI_CMDSET_INTEL_EXTENDED:
519 retval = !flash_isset(info, sect, 0, FLASH_STATUS_DONE);
521 case CFI_CMDSET_AMD_STANDARD:
522 case CFI_CMDSET_AMD_EXTENDED:
523 retval = flash_toggle(info, sect, 0, AMD_STATUS_TOGGLE);
530 /*-----------------------------------------------------------------------
531 * wait for XSR.7 to be set. Time out with an error if it does not.
532 * This routine does not set the flash to read-array mode.
534 static int flash_status_check(flash_info_t * info, flash_sect_t sector, ulong tout, char * prompt)
538 /* Wait for command completion */
539 start = get_timer (0);
540 while (flash_poll_status(info, sector)) {
541 if (get_timer(start) > info->erase_blk_tout) {
542 printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]);
543 flash_write_cmd(info, sector, 0, info->cmd_reset);
549 /*-----------------------------------------------------------------------
550 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
551 * This routine sets the flash to read-array mode.
553 static int flash_full_status_check(flash_info_t * info, flash_sect_t sector, ulong tout, char * prompt)
556 retcode = flash_status_check(info, sector, tout, prompt);
557 switch(info->vendor) {
558 case CFI_CMDSET_INTEL_EXTENDED:
559 case CFI_CMDSET_INTEL_STANDARD:
560 if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
562 printf("Flash %s error at address %lx\n", prompt,info->start[sector]);
563 if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){
564 printf("Command Sequence Error.\n");
565 } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){
566 printf("Block Erase Error.\n");
567 retcode = ERR_NOT_ERASED;
568 } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
569 printf("Locking Error\n");
571 if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
572 printf("Block locked.\n");
573 retcode = ERR_PROTECTED;
575 if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
576 printf("Vpp Low Error.\n");
578 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
585 /*-----------------------------------------------------------------------
587 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
589 switch(info->portwidth) {
593 case FLASH_CFI_16BIT:
594 cword->w = (cword->w << 8) | c;
596 case FLASH_CFI_32BIT:
597 cword->l = (cword->l << 8) | c;
599 case FLASH_CFI_64BIT:
600 cword->ll = (cword->ll << 8) | c;
606 /*-----------------------------------------------------------------------
607 * make a proper sized command based on the port and chip widths
609 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
612 uchar *cp = (uchar *)cmdbuf;
613 for(i=0; i< info->portwidth; i++)
614 *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
618 * Write a proper sized command to the correct address
620 static void flash_write_cmd(flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
623 volatile cfiptr_t addr;
625 addr.cp = flash_make_addr(info, sect, offset);
626 flash_make_cmd(info, cmd, &cword);
627 switch(info->portwidth) {
629 debug("fwc addr %p cmd %x %x 8bit x %d bit\n",addr.cp, cmd, cword.c,
630 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
633 case FLASH_CFI_16BIT:
634 debug("fwc addr %p cmd %x %4.4x 16bit x %d bit\n",addr.wp, cmd, cword.w,
635 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
638 case FLASH_CFI_32BIT:
639 debug("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n",addr.lp, cmd, cword.l,
640 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
643 case FLASH_CFI_64BIT:
647 print_longlong(str, cword.ll);
649 printf("fwc addr %p cmd %x %s 64 bit x %d bit\n",addr.llp, cmd, str,
650 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
653 *addr.llp = cword.ll;
658 static void flash_unlock_seq(flash_info_t *info)
660 flash_write_cmd(info, 0, 0x555, 0xAA);
661 flash_write_cmd(info, 0, 0x2AA, 0x55);
663 /*-----------------------------------------------------------------------
665 static int flash_isequal(flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
670 cptr.cp = flash_make_addr(info, sect, offset);
671 flash_make_cmd(info, cmd, &cword);
673 debug("is= cmd %x(%c) addr %p ", cmd,cmd, cptr.cp);
674 switch(info->portwidth) {
676 debug("is= %x %x\n", cptr.cp[0], cword.c);
677 retval = (cptr.cp[0] == cword.c);
679 case FLASH_CFI_16BIT:
680 debug("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
681 retval = (cptr.wp[0] == cword.w);
683 case FLASH_CFI_32BIT:
684 debug("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
685 retval = (cptr.lp[0] == cword.l);
687 case FLASH_CFI_64BIT:
692 print_longlong(str1, cptr.llp[0]);
693 print_longlong(str2, cword.ll);
694 printf("is= %s %s\n", str1, str2);
697 retval = (cptr.llp[0] == cword.ll);
705 /*-----------------------------------------------------------------------
707 static int flash_isset(flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
712 cptr.cp = flash_make_addr(info, sect, offset);
713 flash_make_cmd(info, cmd, &cword);
714 switch(info->portwidth) {
716 retval = ((cptr.cp[0] & cword.c) == cword.c);
718 case FLASH_CFI_16BIT:
719 retval = ((cptr.wp[0] & cword.w) == cword.w);
721 case FLASH_CFI_32BIT:
722 retval = ((cptr.lp[0] & cword.l) == cword.l);
724 case FLASH_CFI_64BIT:
725 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
733 /*-----------------------------------------------------------------------
735 static int flash_toggle(flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
740 cptr.cp = flash_make_addr(info, sect, offset);
741 flash_make_cmd(info, cmd, &cword);
742 switch(info->portwidth) {
744 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
746 case FLASH_CFI_16BIT:
747 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
749 case FLASH_CFI_32BIT:
750 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
752 case FLASH_CFI_64BIT:
753 retval = ((cptr.llp[0] & cword.ll) != (cptr.llp[0] & cword.ll));
762 /*-----------------------------------------------------------------------
763 * detect if flash is compatible with the Common Flash Interface (CFI)
764 * http://www.jedec.org/download/search/jesd68.pdf
767 static int flash_detect_cfi(flash_info_t * info)
770 puts("flash detect cfi\n");
772 for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_64BIT;
773 info->portwidth <<= 1) {
774 /* for(info->chipwidth = info->portwidth;
776 info->chipwidth >>= 1) { */
777 for(info->chipwidth =FLASH_CFI_BY8;
778 info->chipwidth <= info->portwidth;
779 info->chipwidth <<= 1) {
780 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
781 flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
782 if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') &&
783 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
784 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
785 debug("found port %d chip %d ", info->portwidth, info->chipwidth);
786 debug("port %d bits chip %d bits\n", info->portwidth << CFI_FLASH_SHIFT_WIDTH,
787 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
796 * The following code cannot be run from FLASH!
799 static ulong flash_get_size (ulong base, int banknum)
801 flash_info_t * info = &flash_info[banknum];
803 flash_sect_t sect_cnt;
804 unsigned long sector;
807 uchar num_erase_regions;
808 int erase_region_size;
809 int erase_region_count;
811 info->start[0] = base;
813 if(flash_detect_cfi(info)){
814 info->vendor = flash_read_ushort(info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
815 switch(info->vendor) {
816 case CFI_CMDSET_INTEL_STANDARD:
817 case CFI_CMDSET_INTEL_EXTENDED:
819 info->cmd_reset = FLASH_CMD_RESET;
821 case CFI_CMDSET_AMD_STANDARD:
822 case CFI_CMDSET_AMD_EXTENDED:
823 info->cmd_reset = AMD_CMD_RESET;
827 debug("manufacturer is %d\n", info->vendor);
828 size_ratio = info->portwidth / info->chipwidth;
829 num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
830 debug("size_ration %d port %d bits chip %d bits\n", size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
831 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
832 debug("found %d erase regions\n", num_erase_regions);
835 for(i = 0 ; i < num_erase_regions; i++) {
836 if(i > NUM_ERASE_REGIONS) {
837 printf("%d erase regions found, only %d used\n",
838 num_erase_regions, NUM_ERASE_REGIONS);
841 tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS + i*4);
842 erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128;
844 erase_region_count = (tmp & 0xffff) +1;
845 for(j = 0; j< erase_region_count; j++) {
846 info->start[sect_cnt] = sector;
847 sector += (erase_region_size * size_ratio);
848 info->protect[sect_cnt] = flash_isset(info, sect_cnt, FLASH_OFFSET_PROTECT, FLASH_STATUS_PROTECT);
853 info->sector_count = sect_cnt;
854 /* multiply the size by the number of chips */
855 info->size = (1 << flash_read_uchar(info, FLASH_OFFSET_SIZE)) * size_ratio;
856 info->buffer_size = (1 << flash_read_ushort(info, 0, FLASH_OFFSET_BUFFER_SIZE));
857 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_ETOUT);
858 info->erase_blk_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_EMAX_TOUT)));
859 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WBTOUT);
860 info->buffer_write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WBMAX_TOUT)));
861 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WTOUT);
862 info->write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WMAX_TOUT)))/ 1000;
863 info->flash_id = FLASH_MAN_CFI;
866 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
871 /*-----------------------------------------------------------------------
873 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
880 ctladdr.cp = flash_make_addr(info, 0, 0);
881 cptr.cp = (uchar *)dest;
884 /* Check if Flash is (sufficiently) erased */
885 switch(info->portwidth) {
887 flag = ((cptr.cp[0] & cword.c) == cword.c);
889 case FLASH_CFI_16BIT:
890 flag = ((cptr.wp[0] & cword.w) == cword.w);
892 case FLASH_CFI_32BIT:
893 flag = ((cptr.lp[0] & cword.l) == cword.l);
895 case FLASH_CFI_64BIT:
896 flag = ((cptr.lp[0] & cword.ll) == cword.ll);
904 /* Disable interrupts which might cause a timeout here */
905 flag = disable_interrupts();
907 switch(info->vendor) {
908 case CFI_CMDSET_INTEL_EXTENDED:
909 case CFI_CMDSET_INTEL_STANDARD:
910 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
911 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
913 case CFI_CMDSET_AMD_EXTENDED:
914 case CFI_CMDSET_AMD_STANDARD:
915 flash_unlock_seq(info);
916 flash_write_cmd(info, 0, 0x555, AMD_CMD_WRITE);
920 switch(info->portwidth) {
922 cptr.cp[0] = cword.c;
924 case FLASH_CFI_16BIT:
925 cptr.wp[0] = cword.w;
927 case FLASH_CFI_32BIT:
928 cptr.lp[0] = cword.l;
930 case FLASH_CFI_64BIT:
931 cptr.llp[0] = cword.ll;
935 /* re-enable interrupts if necessary */
939 return flash_full_status_check(info, 0, info->write_tout, "write");
942 #ifdef CFG_FLASH_USE_BUFFER_WRITE
944 /* loop through the sectors from the highest address
945 * when the passed address is greater or equal to the sector address
948 static flash_sect_t find_sector(flash_info_t *info, ulong addr)
951 for(sector = info->sector_count - 1; sector >= 0; sector--) {
952 if(addr >= info->start[sector])
958 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
963 volatile cfiptr_t src;
964 volatile cfiptr_t dst;
967 dst.cp = (uchar *)dest;
968 sector = find_sector(info, dest);
969 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
970 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
971 if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
972 "write to buffer")) == ERR_OK) {
973 switch(info->portwidth) {
977 case FLASH_CFI_16BIT:
980 case FLASH_CFI_32BIT:
983 case FLASH_CFI_64BIT:
990 flash_write_cmd(info, sector, 0, (uchar)cnt-1);
992 switch(info->portwidth) {
994 *dst.cp++ = *src.cp++;
996 case FLASH_CFI_16BIT:
997 *dst.wp++ = *src.wp++;
999 case FLASH_CFI_32BIT:
1000 *dst.lp++ = *src.lp++;
1002 case FLASH_CFI_64BIT:
1003 *dst.llp++ = *src.llp++;
1010 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
1011 retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
1014 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1017 #endif /* CFG_USE_FLASH_BUFFER_WRITE */
1018 #endif /* CFG_FLASH_CFI */