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
73 #define FLASH_MAN_CFI 0x01000000
88 #define NUM_ERASE_REGIONS 4
90 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
93 /*-----------------------------------------------------------------------
98 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c);
99 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf);
100 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd);
101 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd);
102 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd);
103 static int flash_detect_cfi(flash_info_t * info);
104 static ulong flash_get_size (ulong base, int banknum);
105 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword);
106 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt);
107 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
108 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len);
110 /*-----------------------------------------------------------------------
111 * create an address based on the offset and the port width
113 inline uchar * flash_make_addr(flash_info_t * info, int sect, int offset)
115 return ((uchar *)(info->start[sect] + (offset * info->portwidth)));
117 /*-----------------------------------------------------------------------
118 * read a character at a port width address
120 inline uchar flash_read_uchar(flash_info_t * info, uchar offset)
123 cp = flash_make_addr(info, 0, offset);
124 return (cp[info->portwidth - 1]);
127 /*-----------------------------------------------------------------------
128 * read a short word by swapping for ppc format.
130 ushort flash_read_ushort(flash_info_t * info, int sect, uchar offset)
134 addr = flash_make_addr(info, sect, offset);
135 return ((addr[(2*info->portwidth) - 1] << 8) | addr[info->portwidth - 1]);
139 /*-----------------------------------------------------------------------
140 * read a long word by picking the least significant byte of each maiximum
141 * port size word. Swap for ppc format.
143 ulong flash_read_long(flash_info_t * info, int sect, uchar offset)
147 addr = flash_make_addr(info, sect, offset);
148 return ( (addr[(2*info->portwidth) - 1] << 24 ) | (addr[(info->portwidth) -1] << 16) |
149 (addr[(4*info->portwidth) - 1] << 8) | addr[(3*info->portwidth) - 1]);
153 /*-----------------------------------------------------------------------
155 unsigned long flash_init (void)
159 unsigned long address;
162 /* The flash is positioned back to back, with the demultiplexing of the chip
163 * based on the A24 address line.
167 address = CONFIG_SYS_FLASH_BASE;
170 /* Init: no FLASHes known */
171 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
172 flash_info[i].flash_id = FLASH_UNKNOWN;
173 size += flash_info[i].size = flash_get_size(address, i);
174 address += CONFIG_SYS_FLASH_INCREMENT;
175 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
176 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",i,
177 flash_info[0].size, flash_info[i].size<<20);
181 #if 0 /* test-only */
182 /* Monitor protection ON by default */
183 #if (CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE)
184 for(i=0; flash_info[0].start[i] < CONFIG_SYS_MONITOR_BASE+CONFIG_SYS_MONITOR_LEN-1; i++)
185 (void)flash_real_protect(&flash_info[0], i, 1);
188 /* monitor protection ON by default */
189 flash_protect (FLAG_PROTECT_SET,
190 - CONFIG_SYS_MONITOR_LEN,
191 - 1, &flash_info[1]);
197 /*-----------------------------------------------------------------------
199 int flash_erase (flash_info_t *info, int s_first, int s_last)
205 if( info->flash_id != FLASH_MAN_CFI) {
206 printf ("Can't erase unknown flash type - aborted\n");
209 if ((s_first < 0) || (s_first > s_last)) {
210 printf ("- no sectors to erase\n");
215 for (sect=s_first; sect<=s_last; ++sect) {
216 if (info->protect[sect]) {
221 printf ("- Warning: %d protected sectors will not be erased!\n",
228 for (sect = s_first; sect<=s_last; sect++) {
229 if (info->protect[sect] == 0) { /* not protected */
230 flash_write_cmd(info, sect, 0, FLASH_CMD_CLEAR_STATUS);
231 flash_write_cmd(info, sect, 0, FLASH_CMD_BLOCK_ERASE);
232 flash_write_cmd(info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
234 if(flash_full_status_check(info, sect, info->erase_blk_tout, "erase")) {
244 /*-----------------------------------------------------------------------
246 void flash_print_info (flash_info_t *info)
250 if (info->flash_id != FLASH_MAN_CFI) {
251 printf ("missing or unknown FLASH type\n");
255 printf("CFI conformant FLASH (%d x %d)",
256 (info->portwidth << 3 ), (info->chipwidth << 3 ));
257 printf (" Size: %ld MB in %d Sectors\n",
258 info->size >> 20, info->sector_count);
259 printf(" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
260 info->erase_blk_tout, info->write_tout, info->buffer_write_tout, info->buffer_size);
262 printf (" Sector Start Addresses:");
263 for (i=0; i<info->sector_count; ++i) {
264 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
268 volatile unsigned long *flash;
271 * Check if whole sector is erased
273 if (i != (info->sector_count-1))
274 size = info->start[i+1] - info->start[i];
276 size = info->start[0] + info->size - info->start[i];
278 flash = (volatile unsigned long *)info->start[i];
279 size = size >> 2; /* divide by 4 for longword access */
280 for (k=0; k<size; k++)
282 if (*flash++ != 0xffffffff)
291 /* print empty and read-only info */
292 printf (" %08lX%s%s",
295 info->protect[i] ? "RO " : " ");
301 info->protect[i] ? " (RO)" : " ");
308 /*-----------------------------------------------------------------------
309 * Copy memory to flash, returns:
312 * 2 - Flash not erased
314 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
322 /* get lower aligned address */
323 wp = (addr & ~(info->portwidth - 1));
325 /* handle unaligned start */
326 if((aln = addr - wp) != 0) {
329 for(i=0;i<aln; ++i, ++cp)
330 flash_add_byte(info, &cword, (*(uchar *)cp));
332 for(; (i< info->portwidth) && (cnt > 0) ; i++) {
333 flash_add_byte(info, &cword, *src++);
337 for(; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
338 flash_add_byte(info, &cword, (*(uchar *)cp));
339 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
344 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
345 while(cnt >= info->portwidth) {
346 i = info->buffer_size > cnt? cnt: info->buffer_size;
347 if((rc = flash_write_cfibuffer(info, wp, src,i)) != ERR_OK)
354 /* handle the aligned part */
355 while(cnt >= info->portwidth) {
357 for(i = 0; i < info->portwidth; i++) {
358 flash_add_byte(info, &cword, *src++);
360 if((rc = flash_write_cfiword(info, wp, cword)) != 0)
362 wp += info->portwidth;
363 cnt -= info->portwidth;
365 #endif /* CONFIG_SYS_FLASH_USE_BUFFER_WRITE */
371 * handle unaligned tail bytes
374 for (i=0, cp=wp; (i<info->portwidth) && (cnt>0); ++i, ++cp) {
375 flash_add_byte(info, &cword, *src++);
378 for (; i<info->portwidth; ++i, ++cp) {
379 flash_add_byte(info, & cword, (*(uchar *)cp));
382 return flash_write_cfiword(info, wp, cword);
385 /*-----------------------------------------------------------------------
387 int flash_real_protect(flash_info_t *info, long sector, int prot)
391 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
392 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT);
394 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_SET);
396 flash_write_cmd(info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
398 if((retcode = flash_full_status_check(info, sector, info->erase_blk_tout,
399 prot?"protect":"unprotect")) == 0) {
401 info->protect[sector] = prot;
402 /* Intel's unprotect unprotects all locking */
405 for(i = 0 ; i<info->sector_count; i++) {
407 flash_real_protect(info, i, 1);
414 /*-----------------------------------------------------------------------
415 * wait for XSR.7 to be set. Time out with an error if it does not.
416 * This routine does not set the flash to read-array mode.
418 static int flash_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
422 /* Wait for command completion */
423 start = get_timer (0);
424 while(!flash_isset(info, sector, 0, FLASH_STATUS_DONE)) {
425 if (get_timer(start) > info->erase_blk_tout) {
426 printf("Flash %s timeout at address %lx\n", prompt, info->start[sector]);
427 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
433 /*-----------------------------------------------------------------------
434 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
435 * This routine sets the flash to read-array mode.
437 static int flash_full_status_check(flash_info_t * info, ulong sector, ulong tout, char * prompt)
440 retcode = flash_status_check(info, sector, tout, prompt);
441 if((retcode == ERR_OK) && !flash_isequal(info,sector, 0, FLASH_STATUS_DONE)) {
443 printf("Flash %s error at address %lx\n", prompt,info->start[sector]);
444 if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)){
445 printf("Command Sequence Error.\n");
446 } else if(flash_isset(info, sector, 0, FLASH_STATUS_ECLBS)){
447 printf("Block Erase Error.\n");
448 retcode = ERR_NOT_ERASED;
449 } else if (flash_isset(info, sector, 0, FLASH_STATUS_PSLBS)) {
450 printf("Locking Error\n");
452 if(flash_isset(info, sector, 0, FLASH_STATUS_DPS)){
453 printf("Block locked.\n");
454 retcode = ERR_PROTECTED;
456 if(flash_isset(info, sector, 0, FLASH_STATUS_VPENS))
457 printf("Vpp Low Error.\n");
459 flash_write_cmd(info, sector, 0, FLASH_CMD_RESET);
462 /*-----------------------------------------------------------------------
464 static void flash_add_byte(flash_info_t *info, cfiword_t * cword, uchar c)
466 switch(info->portwidth) {
470 case FLASH_CFI_16BIT:
471 cword->w = (cword->w << 8) | c;
473 case FLASH_CFI_32BIT:
474 cword->l = (cword->l << 8) | c;
479 /*-----------------------------------------------------------------------
480 * make a proper sized command based on the port and chip widths
482 static void flash_make_cmd(flash_info_t * info, uchar cmd, void * cmdbuf)
485 uchar *cp = (uchar *)cmdbuf;
486 for(i=0; i< info->portwidth; i++)
487 *cp++ = ((i+1) % info->chipwidth) ? '\0':cmd;
491 * Write a proper sized command to the correct address
493 static void flash_write_cmd(flash_info_t * info, int sect, uchar offset, uchar cmd)
496 volatile cfiptr_t addr;
498 addr.cp = flash_make_addr(info, sect, offset);
499 flash_make_cmd(info, cmd, &cword);
500 switch(info->portwidth) {
504 case FLASH_CFI_16BIT:
507 case FLASH_CFI_32BIT:
513 /*-----------------------------------------------------------------------
515 static int flash_isequal(flash_info_t * info, int sect, uchar offset, uchar cmd)
520 cptr.cp = flash_make_addr(info, sect, offset);
521 flash_make_cmd(info, cmd, &cword);
522 switch(info->portwidth) {
524 retval = (cptr.cp[0] == cword.c);
526 case FLASH_CFI_16BIT:
527 retval = (cptr.wp[0] == cword.w);
529 case FLASH_CFI_32BIT:
530 retval = (cptr.lp[0] == cword.l);
538 /*-----------------------------------------------------------------------
540 static int flash_isset(flash_info_t * info, int sect, uchar offset, uchar cmd)
545 cptr.cp = flash_make_addr(info, sect, offset);
546 flash_make_cmd(info, cmd, &cword);
547 switch(info->portwidth) {
549 retval = ((cptr.cp[0] & cword.c) == cword.c);
551 case FLASH_CFI_16BIT:
552 retval = ((cptr.wp[0] & cword.w) == cword.w);
554 case FLASH_CFI_32BIT:
555 retval = ((cptr.lp[0] & cword.l) == cword.l);
564 /*-----------------------------------------------------------------------
565 * detect if flash is compatible with the Common Flash Interface (CFI)
566 * http://www.jedec.org/download/search/jesd68.pdf
569 static int flash_detect_cfi(flash_info_t * info)
572 for(info->portwidth=FLASH_CFI_8BIT; info->portwidth <= FLASH_CFI_32BIT;
573 info->portwidth <<= 1) {
574 for(info->chipwidth =FLASH_CFI_BY8;
575 info->chipwidth <= info->portwidth;
576 info->chipwidth <<= 1) {
577 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
578 flash_write_cmd(info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
579 if(flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP,'Q') &&
580 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R') &&
581 flash_isequal(info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y'))
588 * The following code cannot be run from FLASH!
591 static ulong flash_get_size (ulong base, int banknum)
593 flash_info_t * info = &flash_info[banknum];
596 unsigned long sector;
599 uchar num_erase_regions;
600 int erase_region_size;
601 int erase_region_count;
603 info->start[0] = base;
605 if(flash_detect_cfi(info)){
607 printf("portwidth=%d chipwidth=%d\n", info->portwidth, info->chipwidth); /* test-only */
609 size_ratio = info->portwidth / info->chipwidth;
610 num_erase_regions = flash_read_uchar(info, FLASH_OFFSET_NUM_ERASE_REGIONS);
612 printf("found %d erase regions\n", num_erase_regions);
616 for(i = 0 ; i < num_erase_regions; i++) {
617 if(i > NUM_ERASE_REGIONS) {
618 printf("%d erase regions found, only %d used\n",
619 num_erase_regions, NUM_ERASE_REGIONS);
622 tmp = flash_read_long(info, 0, FLASH_OFFSET_ERASE_REGIONS);
623 erase_region_size = (tmp & 0xffff)? ((tmp & 0xffff) * 256): 128;
625 erase_region_count = (tmp & 0xffff) +1;
626 for(j = 0; j< erase_region_count; j++) {
627 info->start[sect_cnt] = sector;
628 sector += (erase_region_size * size_ratio);
629 info->protect[sect_cnt] = flash_isset(info, sect_cnt, FLASH_OFFSET_PROTECT, FLASH_STATUS_PROTECT);
634 info->sector_count = sect_cnt;
635 /* multiply the size by the number of chips */
636 info->size = (1 << flash_read_uchar(info, FLASH_OFFSET_SIZE)) * size_ratio;
637 info->buffer_size = (1 << flash_read_ushort(info, 0, FLASH_OFFSET_BUFFER_SIZE));
638 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_ETOUT);
639 info->erase_blk_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_EMAX_TOUT)));
640 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WBTOUT);
641 info->buffer_write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WBMAX_TOUT)));
642 tmp = 1 << flash_read_uchar(info, FLASH_OFFSET_WTOUT);
643 info->write_tout = (tmp * (1 << flash_read_uchar(info, FLASH_OFFSET_WMAX_TOUT)))/ 1000;
644 info->flash_id = FLASH_MAN_CFI;
647 flash_write_cmd(info, 0, 0, FLASH_CMD_RESET);
652 /*-----------------------------------------------------------------------
654 static int flash_write_cfiword (flash_info_t *info, ulong dest, cfiword_t cword)
660 cptr.cp = (uchar *)dest;
662 /* Check if Flash is (sufficiently) erased */
663 switch(info->portwidth) {
665 flag = ((cptr.cp[0] & cword.c) == cword.c);
667 case FLASH_CFI_16BIT:
668 flag = ((cptr.wp[0] & cword.w) == cword.w);
670 case FLASH_CFI_32BIT:
671 flag = ((cptr.lp[0] & cword.l) == cword.l);
679 /* Disable interrupts which might cause a timeout here */
680 flag = disable_interrupts();
682 flash_write_cmd(info, 0, 0, FLASH_CMD_CLEAR_STATUS);
683 flash_write_cmd(info, 0, 0, FLASH_CMD_WRITE);
685 switch(info->portwidth) {
687 cptr.cp[0] = cword.c;
689 case FLASH_CFI_16BIT:
690 cptr.wp[0] = cword.w;
692 case FLASH_CFI_32BIT:
693 cptr.lp[0] = cword.l;
697 /* re-enable interrupts if necessary */
701 return flash_full_status_check(info, 0, info->write_tout, "write");
704 #ifdef CONFIG_SYS_FLASH_USE_BUFFER_WRITE
706 /* loop through the sectors from the highest address
707 * when the passed address is greater or equal to the sector address
710 static int find_sector(flash_info_t *info, ulong addr)
713 for(sector = info->sector_count - 1; sector >= 0; sector--) {
714 if(addr >= info->start[sector])
720 static int flash_write_cfibuffer(flash_info_t * info, ulong dest, uchar * cp, int len)
726 volatile cfiptr_t src;
727 volatile cfiptr_t dst;
730 dst.cp = (uchar *)dest;
731 sector = find_sector(info, dest);
732 flash_write_cmd(info, sector, 0, FLASH_CMD_CLEAR_STATUS);
733 flash_write_cmd(info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
734 if((retcode = flash_status_check(info, sector, info->buffer_write_tout,
735 "write to buffer")) == ERR_OK) {
736 switch(info->portwidth) {
740 case FLASH_CFI_16BIT:
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 */