3 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/processor.h>
14 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
15 * The width of the port and the width of the chips are determined at initialization.
16 * These widths are used to calculate the address for access CFI data structures.
17 * It has been tested on an Intel Strataflash implementation.
20 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
21 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
22 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
23 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
26 * Use Primary Extended Query table (PRI) and Alternate Algorithm Query Table (ALT) to determine if protection is available
27 * Add support for other command sets Use the PRI and ALT to determine command set
28 * Verify erase and program timeouts.
31 #define FLASH_CMD_CFI 0x98
32 #define FLASH_CMD_READ_ID 0x90
33 #define FLASH_CMD_RESET 0xff
34 #define FLASH_CMD_BLOCK_ERASE 0x20
35 #define FLASH_CMD_ERASE_CONFIRM 0xD0
36 #define FLASH_CMD_WRITE 0x40
37 #define FLASH_CMD_PROTECT 0x60
38 #define FLASH_CMD_PROTECT_SET 0x01
39 #define FLASH_CMD_PROTECT_CLEAR 0xD0
40 #define FLASH_CMD_CLEAR_STATUS 0x50
41 #define FLASH_CMD_WRITE_TO_BUFFER 0xE8
42 #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
44 #define FLASH_STATUS_DONE 0x80
45 #define FLASH_STATUS_ESS 0x40
46 #define FLASH_STATUS_ECLBS 0x20
47 #define FLASH_STATUS_PSLBS 0x10
48 #define FLASH_STATUS_VPENS 0x08
49 #define FLASH_STATUS_PSS 0x04
50 #define FLASH_STATUS_DPS 0x02
51 #define FLASH_STATUS_R 0x01
52 #define FLASH_STATUS_PROTECT 0x01
54 #define FLASH_OFFSET_CFI 0x55
55 #define FLASH_OFFSET_CFI_RESP 0x10
56 #define FLASH_OFFSET_WTOUT 0x1F
57 #define FLASH_OFFSET_WBTOUT 0x20
58 #define FLASH_OFFSET_ETOUT 0x21
59 #define FLASH_OFFSET_CETOUT 0x22
60 #define FLASH_OFFSET_WMAX_TOUT 0x23
61 #define FLASH_OFFSET_WBMAX_TOUT 0x24
62 #define FLASH_OFFSET_EMAX_TOUT 0x25
63 #define FLASH_OFFSET_CEMAX_TOUT 0x26
64 #define FLASH_OFFSET_SIZE 0x27
65 #define FLASH_OFFSET_INTERFACE 0x28
66 #define FLASH_OFFSET_BUFFER_SIZE 0x2A
67 #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
68 #define FLASH_OFFSET_ERASE_REGIONS 0x2D
69 #define FLASH_OFFSET_PROTECT 0x02
70 #define FLASH_OFFSET_USER_PROTECTION 0x85
71 #define FLASH_OFFSET_INTEL_PROTECTION 0x81
74 #define FLASH_MAN_CFI 0x01000000
89 #define NUM_ERASE_REGIONS 4
91 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
94 /*-----------------------------------------------------------------------
99 static void flash_add_byte(flash_info_t
*info
, cfiword_t
* cword
, uchar c
);
100 static void flash_make_cmd(flash_info_t
* info
, uchar cmd
, void * cmdbuf
);
101 static void flash_write_cmd(flash_info_t
* info
, int sect
, uchar offset
, uchar cmd
);
102 static int flash_isequal(flash_info_t
* info
, int sect
, uchar offset
, uchar cmd
);
103 static int flash_isset(flash_info_t
* info
, int sect
, uchar offset
, uchar cmd
);
104 static int flash_detect_cfi(flash_info_t
* info
);
105 static ulong
flash_get_size (ulong base
, int banknum
);
106 static int flash_write_cfiword (flash_info_t
*info
, ulong dest
, cfiword_t cword
);
107 static int flash_full_status_check(flash_info_t
* info
, ulong sector
, ulong tout
, char * prompt
);
108 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
109 static int flash_write_cfibuffer(flash_info_t
* info
, ulong dest
, uchar
* cp
, int len
);
111 /*-----------------------------------------------------------------------
112 * create an address based on the offset and the port width
114 inline uchar
* flash_make_addr(flash_info_t
* info
, int sect
, int offset
)
116 return ((uchar
*)(info
->start
[sect
] + (offset
* info
->portwidth
)));
118 /*-----------------------------------------------------------------------
119 * read a character at a port width address
121 inline uchar
flash_read_uchar(flash_info_t
* info
, uchar offset
)
124 cp
= flash_make_addr(info
, 0, offset
);
125 return (cp
[info
->portwidth
- 1]);
128 /*-----------------------------------------------------------------------
129 * read a short word by swapping for ppc format.
131 ushort
flash_read_ushort(flash_info_t
* info
, int sect
, uchar offset
)
135 addr
= flash_make_addr(info
, sect
, offset
);
136 return ((addr
[(2*info
->portwidth
) - 1] << 8) | addr
[info
->portwidth
- 1]);
140 /*-----------------------------------------------------------------------
141 * read a long word by picking the least significant byte of each maiximum
142 * port size word. Swap for ppc format.
144 ulong
flash_read_long(flash_info_t
* info
, int sect
, uchar offset
)
148 addr
= flash_make_addr(info
, sect
, offset
);
149 return ( (addr
[(2*info
->portwidth
) - 1] << 24 ) | (addr
[(info
->portwidth
) -1] << 16) |
150 (addr
[(4*info
->portwidth
) - 1] << 8) | addr
[(3*info
->portwidth
) - 1]);
154 /*-----------------------------------------------------------------------
156 unsigned long flash_init (void)
160 unsigned long address
;
163 /* The flash is positioned back to back, with the demultiplexing of the chip
164 * based on the A24 address line.
168 address
= CONFIG_SYS_FLASH_BASE
;
172 /* Init: no FLASHes known */
173 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
174 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
175 size
+= flash_info
[i
].size
= flash_get_size(address
, i
);
176 address
+= CONFIG_SYS_FLASH_INCREMENT
;
177 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
178 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",i
,
179 flash_info
[0].size
, flash_info
[i
].size
<<20);
183 /* Monitor protection ON by default */
184 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
185 for(i
=0; flash_info
[0].start
[i
] < CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1; i
++)
186 (void)flash_real_protect(&flash_info
[0], i
, 1);
192 /*-----------------------------------------------------------------------
194 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
200 if( info
->flash_id
!= FLASH_MAN_CFI
) {
201 printf ("Can't erase unknown flash type - aborted\n");
204 if ((s_first
< 0) || (s_first
> s_last
)) {
205 printf ("- no sectors to erase\n");
210 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
211 if (info
->protect
[sect
]) {
216 printf ("- Warning: %d protected sectors will not be erased!\n",
223 for (sect
= s_first
; sect
<=s_last
; sect
++) {
224 if (info
->protect
[sect
] == 0) { /* not protected */
225 flash_write_cmd(info
, sect
, 0, FLASH_CMD_CLEAR_STATUS
);
226 flash_write_cmd(info
, sect
, 0, FLASH_CMD_BLOCK_ERASE
);
227 flash_write_cmd(info
, sect
, 0, FLASH_CMD_ERASE_CONFIRM
);
229 if(flash_full_status_check(info
, sect
, info
->erase_blk_tout
, "erase")) {
239 /*-----------------------------------------------------------------------
241 void flash_print_info (flash_info_t
*info
)
245 if (info
->flash_id
!= FLASH_MAN_CFI
) {
246 printf ("missing or unknown FLASH type\n");
250 printf("CFI conformant FLASH (%d x %d)",
251 (info
->portwidth
<< 3 ), (info
->chipwidth
<< 3 ));
252 printf (" Size: %ld MB in %d Sectors\n",
253 info
->size
>> 20, info
->sector_count
);
254 printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
255 info
->erase_blk_tout
, info
->write_tout
, info
->buffer_write_tout
, info
->buffer_size
);
257 printf (" Sector Start Addresses:");
258 for (i
=0; i
<info
->sector_count
; ++i
) {
263 info
->protect
[i
] ? " (RO)" : " "
270 /*-----------------------------------------------------------------------
271 * Copy memory to flash, returns:
274 * 2 - Flash not erased
276 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
284 /* get lower aligned address */
285 wp
= (addr
& ~(info
->portwidth
- 1));
287 /* handle unaligned start */
288 if((aln
= addr
- wp
) != 0) {
291 for(i
=0;i
<aln
; ++i
, ++cp
)
292 flash_add_byte(info
, &cword
, (*(uchar
*)cp
));
294 for(; (i
< info
->portwidth
) && (cnt
> 0) ; i
++) {
295 flash_add_byte(info
, &cword
, *src
++);
299 for(; (cnt
== 0) && (i
< info
->portwidth
); ++i
, ++cp
)
300 flash_add_byte(info
, &cword
, (*(uchar
*)cp
));
301 if((rc
= flash_write_cfiword(info
, wp
, cword
)) != 0)
306 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
307 while(cnt
>= info
->portwidth
) {
308 i
= info
->buffer_size
> cnt
? cnt
: info
->buffer_size
;
309 if((rc
= flash_write_cfibuffer(info
, wp
, src
,i
)) != ERR_OK
)
316 /* handle the aligned part */
317 while(cnt
>= info
->portwidth
) {
319 for(i
= 0; i
< info
->portwidth
; i
++) {
320 flash_add_byte(info
, &cword
, *src
++);
322 if((rc
= flash_write_cfiword(info
, wp
, cword
)) != 0)
324 wp
+= info
->portwidth
;
325 cnt
-= info
->portwidth
;
327 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
333 * handle unaligned tail bytes
336 for (i
=0, cp
=wp
; (i
<info
->portwidth
) && (cnt
>0); ++i
, ++cp
) {
337 flash_add_byte(info
, &cword
, *src
++);
340 for (; i
<info
->portwidth
; ++i
, ++cp
) {
341 flash_add_byte(info
, & cword
, (*(uchar
*)cp
));
344 return flash_write_cfiword(info
, wp
, cword
);
347 /*-----------------------------------------------------------------------
349 int flash_real_protect(flash_info_t
*info
, long sector
, int prot
)
353 flash_write_cmd(info
, sector
, 0, FLASH_CMD_CLEAR_STATUS
);
354 flash_write_cmd(info
, sector
, 0, FLASH_CMD_PROTECT
);
356 flash_write_cmd(info
, sector
, 0, FLASH_CMD_PROTECT_SET
);
358 flash_write_cmd(info
, sector
, 0, FLASH_CMD_PROTECT_CLEAR
);
360 if((retcode
= flash_full_status_check(info
, sector
, info
->erase_blk_tout
,
361 prot
?"protect":"unprotect")) == 0) {
363 info
->protect
[sector
] = prot
;
364 /* Intel's unprotect unprotects all locking */
367 for(i
= 0 ; i
<info
->sector_count
; i
++) {
369 flash_real_protect(info
, i
, 1);
376 /*-----------------------------------------------------------------------
377 * wait for XSR.7 to be set. Time out with an error if it does not.
378 * This routine does not set the flash to read-array mode.
380 static int flash_status_check(flash_info_t
* info
, ulong sector
, ulong tout
, char * prompt
)
384 /* Wait for command completion */
385 start
= get_timer (0);
386 while(!flash_isset(info
, sector
, 0, FLASH_STATUS_DONE
)) {
387 if (get_timer(start
) > info
->erase_blk_tout
) {
388 printf("Flash %s timeout at address %lx\n", prompt
, info
->start
[sector
]);
389 flash_write_cmd(info
, sector
, 0, FLASH_CMD_RESET
);
395 /*-----------------------------------------------------------------------
396 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
397 * This routine sets the flash to read-array mode.
399 static int flash_full_status_check(flash_info_t
* info
, ulong sector
, ulong tout
, char * prompt
)
402 retcode
= flash_status_check(info
, sector
, tout
, prompt
);
403 if((retcode
== ERR_OK
) && !flash_isequal(info
,sector
, 0, FLASH_STATUS_DONE
)) {
405 printf("Flash %s error at address %lx\n", prompt
,info
->start
[sector
]);
406 if(flash_isset(info
, sector
, 0, FLASH_STATUS_ECLBS
| FLASH_STATUS_PSLBS
)){
407 printf("Command Sequence Error.\n");
408 } else if(flash_isset(info
, sector
, 0, FLASH_STATUS_ECLBS
)){
409 printf("Block Erase Error.\n");
410 retcode
= ERR_NOT_ERASED
;
411 } else if (flash_isset(info
, sector
, 0, FLASH_STATUS_PSLBS
)) {
412 printf("Locking Error\n");
414 if(flash_isset(info
, sector
, 0, FLASH_STATUS_DPS
)){
415 printf("Block locked.\n");
416 retcode
= ERR_PROTECTED
;
418 if(flash_isset(info
, sector
, 0, FLASH_STATUS_VPENS
))
419 printf("Vpp Low Error.\n");
421 flash_write_cmd(info
, sector
, 0, FLASH_CMD_RESET
);
424 /*-----------------------------------------------------------------------
426 static void flash_add_byte(flash_info_t
*info
, cfiword_t
* cword
, uchar c
)
428 switch(info
->portwidth
) {
432 case FLASH_CFI_16BIT
:
433 cword
->w
= (cword
->w
<< 8) | c
;
435 case FLASH_CFI_32BIT
:
436 cword
->l
= (cword
->l
<< 8) | c
;
441 /*-----------------------------------------------------------------------
442 * make a proper sized command based on the port and chip widths
444 static void flash_make_cmd(flash_info_t
* info
, uchar cmd
, void * cmdbuf
)
447 uchar
*cp
= (uchar
*)cmdbuf
;
448 for(i
=0; i
< info
->portwidth
; i
++)
449 *cp
++ = ((i
+1) % info
->chipwidth
) ? '\0':cmd
;
453 * Write a proper sized command to the correct address
455 static void flash_write_cmd(flash_info_t
* info
, int sect
, uchar offset
, uchar cmd
)
458 volatile cfiptr_t addr
;
460 addr
.cp
= flash_make_addr(info
, sect
, offset
);
461 flash_make_cmd(info
, cmd
, &cword
);
462 switch(info
->portwidth
) {
466 case FLASH_CFI_16BIT
:
469 case FLASH_CFI_32BIT
:
475 /*-----------------------------------------------------------------------
477 static int flash_isequal(flash_info_t
* info
, int sect
, uchar offset
, uchar cmd
)
482 cptr
.cp
= flash_make_addr(info
, sect
, offset
);
483 flash_make_cmd(info
, cmd
, &cword
);
484 switch(info
->portwidth
) {
486 retval
= (cptr
.cp
[0] == cword
.c
);
488 case FLASH_CFI_16BIT
:
489 retval
= (cptr
.wp
[0] == cword
.w
);
491 case FLASH_CFI_32BIT
:
492 retval
= (cptr
.lp
[0] == cword
.l
);
500 /*-----------------------------------------------------------------------
502 static int flash_isset(flash_info_t
* info
, int sect
, uchar offset
, uchar cmd
)
507 cptr
.cp
= flash_make_addr(info
, sect
, offset
);
508 flash_make_cmd(info
, cmd
, &cword
);
509 switch(info
->portwidth
) {
511 retval
= ((cptr
.cp
[0] & cword
.c
) == cword
.c
);
513 case FLASH_CFI_16BIT
:
514 retval
= ((cptr
.wp
[0] & cword
.w
) == cword
.w
);
516 case FLASH_CFI_32BIT
:
517 retval
= ((cptr
.lp
[0] & cword
.l
) == cword
.l
);
526 /*-----------------------------------------------------------------------
527 * detect if flash is compatible with the Common Flash Interface (CFI)
528 * http://www.jedec.org/download/search/jesd68.pdf
531 static int flash_detect_cfi(flash_info_t
* info
)
534 for(info
->portwidth
=FLASH_CFI_8BIT
; info
->portwidth
<= FLASH_CFI_32BIT
;
535 info
->portwidth
<<= 1) {
536 for(info
->chipwidth
=FLASH_CFI_BY8
;
537 info
->chipwidth
<= info
->portwidth
;
538 info
->chipwidth
<<= 1) {
539 flash_write_cmd(info
, 0, 0, FLASH_CMD_RESET
);
540 flash_write_cmd(info
, 0, FLASH_OFFSET_CFI
, FLASH_CMD_CFI
);
541 if(flash_isequal(info
, 0, FLASH_OFFSET_CFI_RESP
,'Q') &&
542 flash_isequal(info
, 0, FLASH_OFFSET_CFI_RESP
+ 1, 'R') &&
543 flash_isequal(info
, 0, FLASH_OFFSET_CFI_RESP
+ 2, 'Y'))
550 * The following code cannot be run from FLASH!
553 static ulong
flash_get_size (ulong base
, int banknum
)
555 flash_info_t
* info
= &flash_info
[banknum
];
558 unsigned long sector
;
561 uchar num_erase_regions
;
562 int erase_region_size
;
563 int erase_region_count
;
565 info
->start
[0] = base
;
567 if(flash_detect_cfi(info
)){
568 size_ratio
= info
->portwidth
/ info
->chipwidth
;
569 num_erase_regions
= flash_read_uchar(info
, FLASH_OFFSET_NUM_ERASE_REGIONS
);
571 printf("found %d erase regions\n", num_erase_regions
);
575 for(i
= 0 ; i
< num_erase_regions
; i
++) {
576 if(i
> NUM_ERASE_REGIONS
) {
577 printf("%d erase regions found, only %d used\n",
578 num_erase_regions
, NUM_ERASE_REGIONS
);
581 tmp
= flash_read_long(info
, 0, FLASH_OFFSET_ERASE_REGIONS
);
582 erase_region_size
= (tmp
& 0xffff)? ((tmp
& 0xffff) * 256): 128;
584 erase_region_count
= (tmp
& 0xffff) +1;
585 for(j
= 0; j
< erase_region_count
; j
++) {
586 info
->start
[sect_cnt
] = sector
;
587 sector
+= (erase_region_size
* size_ratio
);
588 info
->protect
[sect_cnt
] = flash_isset(info
, sect_cnt
, FLASH_OFFSET_PROTECT
, FLASH_STATUS_PROTECT
);
593 info
->sector_count
= sect_cnt
;
594 /* multiply the size by the number of chips */
595 info
->size
= (1 << flash_read_uchar(info
, FLASH_OFFSET_SIZE
)) * size_ratio
;
596 info
->buffer_size
= (1 << flash_read_ushort(info
, 0, FLASH_OFFSET_BUFFER_SIZE
));
597 tmp
= 1 << flash_read_uchar(info
, FLASH_OFFSET_ETOUT
);
598 info
->erase_blk_tout
= (tmp
* (1 << flash_read_uchar(info
, FLASH_OFFSET_EMAX_TOUT
)));
599 tmp
= 1 << flash_read_uchar(info
, FLASH_OFFSET_WBTOUT
);
600 info
->buffer_write_tout
= (tmp
* (1 << flash_read_uchar(info
, FLASH_OFFSET_WBMAX_TOUT
)));
601 tmp
= 1 << flash_read_uchar(info
, FLASH_OFFSET_WTOUT
);
602 info
->write_tout
= (tmp
* (1 << flash_read_uchar(info
, FLASH_OFFSET_WMAX_TOUT
)))/ 1000;
603 info
->flash_id
= FLASH_MAN_CFI
;
606 flash_write_cmd(info
, 0, 0, FLASH_CMD_RESET
);
611 /*-----------------------------------------------------------------------
613 static int flash_write_cfiword (flash_info_t
*info
, ulong dest
, cfiword_t cword
)
620 ctladdr
.cp
= flash_make_addr(info
, 0, 0);
621 cptr
.cp
= (uchar
*)dest
;
624 /* Check if Flash is (sufficiently) erased */
625 switch(info
->portwidth
) {
627 flag
= ((cptr
.cp
[0] & cword
.c
) == cword
.c
);
629 case FLASH_CFI_16BIT
:
630 flag
= ((cptr
.wp
[0] & cword
.w
) == cword
.w
);
632 case FLASH_CFI_32BIT
:
633 flag
= ((cptr
.lp
[0] & cword
.l
) == cword
.l
);
641 /* Disable interrupts which might cause a timeout here */
642 flag
= disable_interrupts();
644 flash_write_cmd(info
, 0, 0, FLASH_CMD_CLEAR_STATUS
);
645 flash_write_cmd(info
, 0, 0, FLASH_CMD_WRITE
);
647 switch(info
->portwidth
) {
649 cptr
.cp
[0] = cword
.c
;
651 case FLASH_CFI_16BIT
:
652 cptr
.wp
[0] = cword
.w
;
654 case FLASH_CFI_32BIT
:
655 cptr
.lp
[0] = cword
.l
;
659 /* re-enable interrupts if necessary */
663 return flash_full_status_check(info
, 0, info
->write_tout
, "write");
666 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
668 /* loop through the sectors from the highest address
669 * when the passed address is greater or equal to the sector address
672 static int find_sector(flash_info_t
*info
, ulong addr
)
675 for(sector
= info
->sector_count
- 1; sector
>= 0; sector
--) {
676 if(addr
>= info
->start
[sector
])
682 static int flash_write_cfibuffer(flash_info_t
* info
, ulong dest
, uchar
* cp
, int len
)
688 volatile cfiptr_t src
;
689 volatile cfiptr_t dst
;
692 dst
.cp
= (uchar
*)dest
;
693 sector
= find_sector(info
, dest
);
694 flash_write_cmd(info
, sector
, 0, FLASH_CMD_CLEAR_STATUS
);
695 flash_write_cmd(info
, sector
, 0, FLASH_CMD_WRITE_TO_BUFFER
);
696 if((retcode
= flash_status_check(info
, sector
, info
->buffer_write_tout
,
697 "write to buffer")) == ERR_OK
) {
698 switch(info
->portwidth
) {
702 case FLASH_CFI_16BIT
:
705 case FLASH_CFI_32BIT
:
712 flash_write_cmd(info
, sector
, 0, (uchar
)cnt
-1);
714 switch(info
->portwidth
) {
716 *dst
.cp
++ = *src
.cp
++;
718 case FLASH_CFI_16BIT
:
719 *dst
.wp
++ = *src
.wp
++;
721 case FLASH_CFI_32BIT
:
722 *dst
.lp
++ = *src
.lp
++;
729 flash_write_cmd(info
, sector
, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM
);
730 retcode
= flash_full_status_check(info
, sector
, info
->buffer_write_tout
,
733 flash_write_cmd(info
, sector
, 0, FLASH_CMD_CLEAR_STATUS
);
736 #endif /* CONFIG_SYS_USE_FLASH_BUFFER_WRITE */