3 * Brad Kemp, Seranoa Networks, Brad.Kemp@seranoa.com
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/processor.h>
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.
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
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.
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
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
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
72 #define FLASH_MAN_CFI 0x01000000
86 #define NUM_ERASE_REGIONS 4
88 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
90 /*-----------------------------------------------------------------------
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);
103 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
104 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len);
106 /*-----------------------------------------------------------------------
107 * create an address based on the offset and the port width
109 inline uchar * flash_make_addr(flash_info_t * info, int sect, int offset)
111 return ((uchar *)(info->start[sect] + (offset * info->portwidth)));
113 /*-----------------------------------------------------------------------
114 * read a character at a port width address
116 inline uchar flash_read_uchar(flash_info_t * info, uchar offset)
119 cp = flash_make_addr(info, 0, offset);
120 return (cp[info->portwidth - 1]);
123 /*-----------------------------------------------------------------------
124 * read a short word by swapping for ppc format.
126 ushort flash_read_ushort(flash_info_t * info, int sect, uchar offset)
130 addr = flash_make_addr(info, sect, offset);
131 return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]);
135 /*-----------------------------------------------------------------------
136 * read a long word by picking the least significant byte of each maiximum
137 * port size word. Swap for ppc format.
139 ulong flash_read_long(flash_info_t * info, int sect, uchar offset)
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]);
149 /*-----------------------------------------------------------------------
151 unsigned long flash_init (void)
155 unsigned long address;
158 /* The flash is positioned back to back, with the demultiplexing of the chip
159 * based on the A24 address line.
163 address = CONFIG_SYS_FLASH_BASE;
166 /* Init: no FLASHes known */
167 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
168 flash_info[i].flash_id = FLASH_UNKNOWN;
169 size += flash_info[i].size = flash_get_size(address, i);
170 address += CONFIG_SYS_FLASH_INCREMENT;
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);
177 #if 0 /* test-only */
178 /* Monitor protection ON by default */
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++)
181 (void)flash_real_protect(&flash_info[0], i, 1);
184 /* monitor protection ON by default */
185 flash_protect (FLAG_PROTECT_SET,
186 - CONFIG_SYS_MONITOR_LEN,
187 - 1, &flash_info[1]);
193 /*-----------------------------------------------------------------------
195 int flash_erase (flash_info_t *info, int s_first, int s_last)
201 if( info->flash_id != FLASH_MAN_CFI) {
202 printf ("Can't erase unknown flash type - aborted\n");
205 if ((s_first < 0) || (s_first > s_last)) {
206 printf ("- no sectors to erase\n");
211 for (sect=s_first; sect<=s_last; ++sect) {
212 if (info->protect[sect]) {
217 printf ("- Warning: %d protected sectors will not be erased!\n",
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);
230 if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
240 /*-----------------------------------------------------------------------
242 void flash_print_info (flash_info_t *info)
246 if (info->flash_id != FLASH_MAN_CFI) {
247 printf ("missing or unknown FLASH type\n");
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);
258 printf (" Sector Start Addresses:");
259 for (i=0; i<info->sector_count; ++i) {
260 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
264 volatile unsigned long *flash;
267 * Check if whole sector is erased
269 if (i != (info->sector_count-1))
270 size = info->start[i+1] - info->start[i];
272 size = info->start[0] + info->size - info->start[i];
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++)
278 if (*flash++ != 0xffffffff)
287 /* print empty and read-only info */
288 printf (" %08lX%s%s",
291 info->protect[i] ? "RO " : " ");
297 info->protect[i] ? " (RO)" : " ");
304 /*-----------------------------------------------------------------------
305 * Copy memory to flash, returns:
308 * 2 - Flash not erased
310 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
318 /* get lower aligned address */
319 wp = (addr & ~(info->portwidth - 1));
321 /* handle unaligned start */
322 if((aln = addr - wp) != 0) {
325 for(i=0;i<aln; ++i, ++cp)
326 flash_add_byte(info, &cword, (*(uchar *)cp));
328 for(; (i< info->portwidth) && (cnt > 0) ; i++) {
329 flash_add_byte(info, &cword, *src++);
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)
340 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
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)
350 /* handle the aligned part */
351 while(cnt >= info->portwidth) {
353 for(i = 0; i < info->portwidth; i++) {
354 flash_add_byte(info, &cword, *src++);
356 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
358 wp += info->portwidth;
359 cnt -= info->portwidth;
361 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
367 * handle unaligned tail bytes
370 for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
371 flash_add_byte(info, &cword, *src++);
374 for (; i<info->portwidth; ++i, ++cp) {
375 flash_add_byte(info, & cword, (*(uchar *)cp));
378 return flash_write_cfiword(info, wp, cword);
381 /*-----------------------------------------------------------------------
383 int flash_real_protect(flash_info_t *info, long sector, int prot)
387 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
388 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
390 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
392 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
394 if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
395 prot?"protect":"unprotect")) == 0) {
397 info->protect[sector] = prot;
398 /* Intel's unprotect unprotects all locking */
401 for(i = 0 ; i<info->sector_count; i++) {
403 flash_real_protect(info, i, 1);
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.
414 static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
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);
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.
433 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
436 retcode = flash_status_check(info, sector, tout, prompt);
437 if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
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");
444 retcode = ERR_NOT_ERASED;
445 } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
446 printf("Locking Error\n");
448 if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
449 printf("Block locked.\n");
450 retcode = ERR_PROTECTED;
452 if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
453 printf("Vpp Low Error.\n");
455 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
458 /*-----------------------------------------------------------------------
460 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
462 switch(info->portwidth) {
466 case FLASH_CFI_16BIT:
467 cword->w = (cword->w << 8) | c;
469 case FLASH_CFI_32BIT:
470 cword->l = (cword->l << 8) | c;
475 /*-----------------------------------------------------------------------
476 * make a proper sized command based on the port and chip widths
478 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
481 uchar *cp = (uchar *)cmdbuf;
482 for(i=0; i< info->portwidth; i++)
483 *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
487 * Write a proper sized command to the correct address
489 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
492 volatile cfiptr_t addr;
494 addr.cp = flash_make_addr(info, sect, offset);
495 flash_make_cmd(info, cmd, &cword);
496 switch(info->portwidth) {
500 case FLASH_CFI_16BIT:
503 case FLASH_CFI_32BIT:
509 /*-----------------------------------------------------------------------
511 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
516 cptr.cp = flash_make_addr(info, sect, offset);
517 flash_make_cmd(info, cmd, &cword);
518 switch(info->portwidth) {
520 retval = (cptr.cp[0] == cword.c);
522 case FLASH_CFI_16BIT:
523 retval = (cptr.wp[0] == cword.w);
525 case FLASH_CFI_32BIT:
526 retval = (cptr.lp[0] == cword.l);
534 /*-----------------------------------------------------------------------
536 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
541 cptr.cp = flash_make_addr(info, sect, offset);
542 flash_make_cmd(info, cmd, &cword);
543 switch(info->portwidth) {
545 retval = ((cptr.cp[0] & cword.c) == cword.c);
547 case FLASH_CFI_16BIT:
548 retval = ((cptr.wp[0] & cword.w) == cword.w);
550 case FLASH_CFI_32BIT:
551 retval = ((cptr.lp[0] & cword.l) == cword.l);
560 /*-----------------------------------------------------------------------
561 * detect if flash is compatible with the Common Flash Interface (CFI)
562 * http://www.jedec.org/download/search/jesd68.pdf
565 static int flash_detect_cfi(flash_info_t * info)
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'))
584 * The following code cannot be run from FLASH!
587 static ulong flash_get_size (ulong base, int banknum)
589 flash_info_t * info = &flash_info[banknum];
592 unsigned long sector;
595 uchar num_erase_regions;
596 int erase_region_size;
597 int erase_region_count;
599 info->start[0] = base;
601 if(flash_detect_cfi(info)){
603 printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */
605 size_ratio = info->portwidth / info->chipwidth;
606 num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
608 printf("found %d erase regions\n", num_erase_regions);
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);
618 tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS);
619 erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128;
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);
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;
643 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
648 /*-----------------------------------------------------------------------
650 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
656 cptr.cp = (uchar *)dest;
658 /* Check if Flash is (sufficiently) erased */
659 switch(info->portwidth) {
661 flag = ((cptr.cp[0] & cword.c) == cword.c);
663 case FLASH_CFI_16BIT:
664 flag = ((cptr.wp[0] & cword.w) == cword.w);
666 case FLASH_CFI_32BIT:
667 flag = ((cptr.lp[0] & cword.l) == cword.l);
675 /* Disable interrupts which might cause a timeout here */
676 flag = disable_interrupts();
678 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
679 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
681 switch(info->portwidth) {
683 cptr.cp[0] = cword.c;
685 case FLASH_CFI_16BIT:
686 cptr.wp[0] = cword.w;
688 case FLASH_CFI_32BIT:
689 cptr.lp[0] = cword.l;
693 /* re-enable interrupts if necessary */
697 return flash_full_status_check(info, 0, info->write_tout, "write");
700 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
702 /* loop through the sectors from the highest address
703 * when the passed address is greater or equal to the sector address
706 static int find_sector(flash_info_t *info, ulong addr)
709 for(sector = info->sector_count - 1; sector >= 0; sector--) {
710 if(addr >= info->start[sector])
716 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
722 volatile cfiptr_t src;
723 volatile cfiptr_t dst;
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) {
736 case FLASH_CFI_16BIT:
738 if (len & 0x1) { /* test-only: unaligned size */
739 puts("\nUnalgined size!!!\n"); /* test-only */
743 case FLASH_CFI_32BIT:
750 flash_write_cmd(info, sector, 0, (uchar)cnt-1);
752 switch(info->portwidth) {
754 *dst.cp++ = *src.cp++;
756 case FLASH_CFI_16BIT:
757 *dst.wp++ = *src.wp++;
759 case FLASH_CFI_32BIT:
760 *dst.lp++ = *src.lp++;
767 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_BUFFER_CONFIRM);
768 retcode = flash_full_status_check(info, sector, info->buffer_write_tout,
771 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
774 #endif /* CONFIG_SYS_USE_FLASH_BUFFER_WRITE */