2 * (C) Copyright 2002-2004
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
11 * Modified to work with little-endian systems.
13 * See file CREDITS for list of people who contributed to this
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * 01/20/2004 - combined variants of original driver.
33 * 01/22/2004 - Write performance enhancements for parallel chips (Tolunay)
34 * 01/23/2004 - Support for x8/x16 chips (Rune Raknerud)
35 * 01/27/2004 - Little endian support Ed Okerson
37 * Tested Architectures
38 * Port Width Chip Width # of banks Flash Chip Board
39 * 32 16 1 28F128J3 seranoa/eagle
40 * 64 16 1 28F128J3 seranoa/falcon
44 /* The DEBUG define must be before common to enable debugging */
48 #include <asm/processor.h>
49 #include <asm/byteorder.h>
50 #include <linux/byteorder/swab.h>
51 #ifdef CFG_FLASH_CFI_DRIVER
54 * This file implements a Common Flash Interface (CFI) driver for U-Boot.
55 * The width of the port and the width of the chips are determined at initialization.
56 * These widths are used to calculate the address for access CFI data structures.
57 * It has been tested on an Intel Strataflash implementation and AMD 29F016D.
60 * JEDEC Standard JESD68 - Common Flash Interface (CFI)
61 * JEDEC Standard JEP137-A Common Flash Interface (CFI) ID Codes
62 * Intel Application Note 646 Common Flash Interface (CFI) and Command Sets
63 * Intel 290667-008 3 Volt Intel StrataFlash Memory datasheet
67 * Use Primary Extended Query table (PRI) and Alternate Algorithm Query
68 * Table (ALT) to determine if protection is available
70 * Add support for other command sets Use the PRI and ALT to determine command set
71 * Verify erase and program timeouts.
74 #ifndef CFG_FLASH_BANKS_LIST
75 #define CFG_FLASH_BANKS_LIST { CFG_FLASH_BASE }
78 #define FLASH_CMD_CFI 0x98
79 #define FLASH_CMD_READ_ID 0x90
80 #define FLASH_CMD_RESET 0xff
81 #define FLASH_CMD_BLOCK_ERASE 0x20
82 #define FLASH_CMD_ERASE_CONFIRM 0xD0
83 #define FLASH_CMD_WRITE 0x40
84 #define FLASH_CMD_PROTECT 0x60
85 #define FLASH_CMD_PROTECT_SET 0x01
86 #define FLASH_CMD_PROTECT_CLEAR 0xD0
87 #define FLASH_CMD_CLEAR_STATUS 0x50
88 #define FLASH_CMD_WRITE_TO_BUFFER 0xE8
89 #define FLASH_CMD_WRITE_BUFFER_CONFIRM 0xD0
91 #define FLASH_STATUS_DONE 0x80
92 #define FLASH_STATUS_ESS 0x40
93 #define FLASH_STATUS_ECLBS 0x20
94 #define FLASH_STATUS_PSLBS 0x10
95 #define FLASH_STATUS_VPENS 0x08
96 #define FLASH_STATUS_PSS 0x04
97 #define FLASH_STATUS_DPS 0x02
98 #define FLASH_STATUS_R 0x01
99 #define FLASH_STATUS_PROTECT 0x01
101 #define AMD_CMD_RESET 0xF0
102 #define AMD_CMD_WRITE 0xA0
103 #define AMD_CMD_ERASE_START 0x80
104 #define AMD_CMD_ERASE_SECTOR 0x30
105 #define AMD_CMD_UNLOCK_START 0xAA
106 #define AMD_CMD_UNLOCK_ACK 0x55
108 #define AMD_STATUS_TOGGLE 0x40
109 #define AMD_STATUS_ERROR 0x20
110 #define AMD_ADDR_ERASE_START 0x555
111 #define AMD_ADDR_START 0x555
112 #define AMD_ADDR_ACK 0x2AA
114 #define FLASH_OFFSET_CFI 0x55
115 #define FLASH_OFFSET_CFI_RESP 0x10
116 #define FLASH_OFFSET_PRIMARY_VENDOR 0x13
117 #define FLASH_OFFSET_WTOUT 0x1F
118 #define FLASH_OFFSET_WBTOUT 0x20
119 #define FLASH_OFFSET_ETOUT 0x21
120 #define FLASH_OFFSET_CETOUT 0x22
121 #define FLASH_OFFSET_WMAX_TOUT 0x23
122 #define FLASH_OFFSET_WBMAX_TOUT 0x24
123 #define FLASH_OFFSET_EMAX_TOUT 0x25
124 #define FLASH_OFFSET_CEMAX_TOUT 0x26
125 #define FLASH_OFFSET_SIZE 0x27
126 #define FLASH_OFFSET_INTERFACE 0x28
127 #define FLASH_OFFSET_BUFFER_SIZE 0x2A
128 #define FLASH_OFFSET_NUM_ERASE_REGIONS 0x2C
129 #define FLASH_OFFSET_ERASE_REGIONS 0x2D
130 #define FLASH_OFFSET_PROTECT 0x02
131 #define FLASH_OFFSET_USER_PROTECTION 0x85
132 #define FLASH_OFFSET_INTEL_PROTECTION 0x81
135 #define FLASH_MAN_CFI 0x01000000
137 #define CFI_CMDSET_NONE 0
138 #define CFI_CMDSET_INTEL_EXTENDED 1
139 #define CFI_CMDSET_AMD_STANDARD 2
140 #define CFI_CMDSET_INTEL_STANDARD 3
141 #define CFI_CMDSET_AMD_EXTENDED 4
142 #define CFI_CMDSET_MITSU_STANDARD 256
143 #define CFI_CMDSET_MITSU_EXTENDED 257
144 #define CFI_CMDSET_SST 258
147 #ifdef CFG_FLASH_CFI_AMD_RESET /* needed for STM_ID_29W320DB on UC100 */
148 # undef FLASH_CMD_RESET
149 # define FLASH_CMD_RESET AMD_CMD_RESET /* use AMD-Reset instead */
157 unsigned long long ll;
161 volatile unsigned char *cp;
162 volatile unsigned short *wp;
163 volatile unsigned long *lp;
164 volatile unsigned long long *llp;
167 #define NUM_ERASE_REGIONS 4
169 static ulong bank_base[CFG_MAX_FLASH_BANKS] = CFG_FLASH_BANKS_LIST;
171 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
173 /*-----------------------------------------------------------------------
177 typedef unsigned long flash_sect_t;
179 static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c);
180 static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf);
181 static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
182 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect);
183 static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
184 static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
185 static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd);
186 static int flash_detect_cfi (flash_info_t * info);
187 static ulong flash_get_size (ulong base, int banknum);
188 static int flash_write_cfiword (flash_info_t * info, ulong dest, cfiword_t cword);
189 static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
190 ulong tout, char *prompt);
191 #ifdef CFG_FLASH_USE_BUFFER_WRITE
192 static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp, int len);
195 /*-----------------------------------------------------------------------
196 * create an address based on the offset and the port width
198 inline uchar *flash_make_addr (flash_info_t * info, flash_sect_t sect, uint offset)
200 return ((uchar *) (info->start[sect] + (offset * info->portwidth)));
204 /*-----------------------------------------------------------------------
207 void print_longlong (char *str, unsigned long long data)
212 cp = (unsigned char *) &data;
213 for (i = 0; i < 8; i++)
214 sprintf (&str[i * 2], "%2.2x", *cp++);
216 static void flash_printqry (flash_info_t * info, flash_sect_t sect)
221 for (x = 0; x < 0x40; x += 16 / info->portwidth) {
223 flash_make_addr (info, sect,
224 x + FLASH_OFFSET_CFI_RESP);
225 debug ("%p : ", cptr.cp);
226 for (y = 0; y < 16; y++) {
227 debug ("%2.2x ", cptr.cp[y]);
230 for (y = 0; y < 16; y++) {
231 if (cptr.cp[y] >= 0x20 && cptr.cp[y] <= 0x7e) {
232 debug ("%c", cptr.cp[y]);
243 /*-----------------------------------------------------------------------
244 * read a character at a port width address
246 inline uchar flash_read_uchar (flash_info_t * info, uint offset)
250 cp = flash_make_addr (info, 0, offset);
251 #if defined(__LITTLE_ENDIAN)
254 return (cp[info->portwidth - 1]);
258 /*-----------------------------------------------------------------------
259 * read a short word by swapping for ppc format.
261 ushort flash_read_ushort (flash_info_t * info, flash_sect_t sect, uint offset)
269 addr = flash_make_addr (info, sect, offset);
272 debug ("ushort addr is at %p info->portwidth = %d\n", addr,
274 for (x = 0; x < 2 * info->portwidth; x++) {
275 debug ("addr[%x] = 0x%x\n", x, addr[x]);
278 #if defined(__LITTLE_ENDIAN)
279 retval = ((addr[(info->portwidth)] << 8) | addr[0]);
281 retval = ((addr[(2 * info->portwidth) - 1] << 8) |
282 addr[info->portwidth - 1]);
285 debug ("retval = 0x%x\n", retval);
289 /*-----------------------------------------------------------------------
290 * read a long word by picking the least significant byte of each maiximum
291 * port size word. Swap for ppc format.
293 ulong flash_read_long (flash_info_t * info, flash_sect_t sect, uint offset)
301 addr = flash_make_addr (info, sect, offset);
304 debug ("long addr is at %p info->portwidth = %d\n", addr,
306 for (x = 0; x < 4 * info->portwidth; x++) {
307 debug ("addr[%x] = 0x%x\n", x, addr[x]);
310 #if defined(__LITTLE_ENDIAN)
311 retval = (addr[0] << 16) | (addr[(info->portwidth)] << 24) |
312 (addr[(2 * info->portwidth)]) | (addr[(3 * info->portwidth)] << 8);
314 retval = (addr[(2 * info->portwidth) - 1] << 24) |
315 (addr[(info->portwidth) - 1] << 16) |
316 (addr[(4 * info->portwidth) - 1] << 8) |
317 addr[(3 * info->portwidth) - 1];
322 /*-----------------------------------------------------------------------
324 unsigned long flash_init (void)
326 unsigned long size = 0;
329 /* Init: no FLASHes known */
330 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
331 flash_info[i].flash_id = FLASH_UNKNOWN;
332 size += flash_info[i].size = flash_get_size (bank_base[i], i);
333 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
334 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
335 i, flash_info[i].size, flash_info[i].size << 20);
339 /* Monitor protection ON by default */
340 #if (CFG_MONITOR_BASE >= CFG_FLASH_BASE)
341 flash_protect (FLAG_PROTECT_SET,
343 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
347 /* Environment protection ON by default */
348 #ifdef CFG_ENV_IS_IN_FLASH
349 flash_protect (FLAG_PROTECT_SET,
351 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
355 /* Redundant environment protection ON by default */
356 #ifdef CFG_ENV_ADDR_REDUND
357 flash_protect (FLAG_PROTECT_SET,
359 CFG_ENV_ADDR_REDUND + CFG_ENV_SIZE_REDUND - 1,
365 /*-----------------------------------------------------------------------
367 int flash_erase (flash_info_t * info, int s_first, int s_last)
373 if (info->flash_id != FLASH_MAN_CFI) {
374 puts ("Can't erase unknown flash type - aborted\n");
377 if ((s_first < 0) || (s_first > s_last)) {
378 puts ("- no sectors to erase\n");
383 for (sect = s_first; sect <= s_last; ++sect) {
384 if (info->protect[sect]) {
389 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
395 for (sect = s_first; sect <= s_last; sect++) {
396 if (info->protect[sect] == 0) { /* not protected */
397 switch (info->vendor) {
398 case CFI_CMDSET_INTEL_STANDARD:
399 case CFI_CMDSET_INTEL_EXTENDED:
400 flash_write_cmd (info, sect, 0, FLASH_CMD_CLEAR_STATUS);
401 flash_write_cmd (info, sect, 0, FLASH_CMD_BLOCK_ERASE);
402 flash_write_cmd (info, sect, 0, FLASH_CMD_ERASE_CONFIRM);
404 case CFI_CMDSET_AMD_STANDARD:
405 case CFI_CMDSET_AMD_EXTENDED:
406 flash_unlock_seq (info, sect);
407 flash_write_cmd (info, sect, AMD_ADDR_ERASE_START,
408 AMD_CMD_ERASE_START);
409 flash_unlock_seq (info, sect);
410 flash_write_cmd (info, sect, 0, AMD_CMD_ERASE_SECTOR);
413 debug ("Unkown flash vendor %d\n",
418 if (flash_full_status_check
419 (info, sect, info->erase_blk_tout, "erase")) {
429 /*-----------------------------------------------------------------------
431 void flash_print_info (flash_info_t * info)
435 if (info->flash_id != FLASH_MAN_CFI) {
436 puts ("missing or unknown FLASH type\n");
440 printf ("CFI conformant FLASH (%d x %d)",
441 (info->portwidth << 3), (info->chipwidth << 3));
442 printf (" Size: %ld MB in %d Sectors\n",
443 info->size >> 20, info->sector_count);
444 printf (" Erase timeout %ld ms, write timeout %ld ms, buffer write timeout %ld ms, buffer size %d\n",
445 info->erase_blk_tout,
447 info->buffer_write_tout,
450 puts (" Sector Start Addresses:");
451 for (i = 0; i < info->sector_count; ++i) {
452 #ifdef CFG_FLASH_EMPTY_INFO
456 volatile unsigned long *flash;
459 * Check if whole sector is erased
461 if (i != (info->sector_count - 1))
462 size = info->start[i + 1] - info->start[i];
464 size = info->start[0] + info->size - info->start[i];
466 flash = (volatile unsigned long *) info->start[i];
467 size = size >> 2; /* divide by 4 for longword access */
468 for (k = 0; k < size; k++) {
469 if (*flash++ != 0xffffffff) {
477 /* print empty and read-only info */
478 printf (" %08lX%s%s",
481 info->protect[i] ? "RO " : " ");
486 info->start[i], info->protect[i] ? " (RO) " : " ");
493 /*-----------------------------------------------------------------------
494 * Copy memory to flash, returns:
497 * 2 - Flash not erased
499 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
507 #ifdef CFG_FLASH_USE_BUFFER_WRITE
510 /* get lower aligned address */
511 /* get lower aligned address */
512 wp = (addr & ~(info->portwidth - 1));
514 /* handle unaligned start */
515 if ((aln = addr - wp) != 0) {
518 for (i = 0; i < aln; ++i, ++cp)
519 flash_add_byte (info, &cword, (*(uchar *) cp));
521 for (; (i < info->portwidth) && (cnt > 0); i++) {
522 flash_add_byte (info, &cword, *src++);
526 for (; (cnt == 0) && (i < info->portwidth); ++i, ++cp)
527 flash_add_byte (info, &cword, (*(uchar *) cp));
528 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
533 /* handle the aligned part */
534 #ifdef CFG_FLASH_USE_BUFFER_WRITE
535 buffered_size = (info->portwidth / info->chipwidth);
536 buffered_size *= info->buffer_size;
537 while (cnt >= info->portwidth) {
538 i = buffered_size > cnt ? cnt : buffered_size;
539 if ((rc = flash_write_cfibuffer (info, wp, src, i)) != ERR_OK)
541 i -= (i % info->portwidth);
547 while (cnt >= info->portwidth) {
549 for (i = 0; i < info->portwidth; i++) {
550 flash_add_byte (info, &cword, *src++);
552 if ((rc = flash_write_cfiword (info, wp, cword)) != 0)
554 wp += info->portwidth;
555 cnt -= info->portwidth;
557 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
563 * handle unaligned tail bytes
566 for (i = 0, cp = wp; (i < info->portwidth) && (cnt > 0); ++i, ++cp) {
567 flash_add_byte (info, &cword, *src++);
570 for (; i < info->portwidth; ++i, ++cp) {
571 flash_add_byte (info, &cword, (*(uchar *) cp));
574 return flash_write_cfiword (info, wp, cword);
577 /*-----------------------------------------------------------------------
579 #ifdef CFG_FLASH_PROTECTION
581 int flash_real_protect (flash_info_t * info, long sector, int prot)
585 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
586 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT);
588 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_SET);
590 flash_write_cmd (info, sector, 0, FLASH_CMD_PROTECT_CLEAR);
593 flash_full_status_check (info, sector, info->erase_blk_tout,
594 prot ? "protect" : "unprotect")) == 0) {
596 info->protect[sector] = prot;
597 /* Intel's unprotect unprotects all locking */
601 for (i = 0; i < info->sector_count; i++) {
602 if (info->protect[i])
603 flash_real_protect (info, i, 1);
610 /*-----------------------------------------------------------------------
611 * flash_read_user_serial - read the OneTimeProgramming cells
613 void flash_read_user_serial (flash_info_t * info, void *buffer, int offset,
620 src = flash_make_addr (info, 0, FLASH_OFFSET_USER_PROTECTION);
621 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
622 memcpy (dst, src + offset, len);
623 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
627 * flash_read_factory_serial - read the device Id from the protection area
629 void flash_read_factory_serial (flash_info_t * info, void *buffer, int offset,
634 src = flash_make_addr (info, 0, FLASH_OFFSET_INTEL_PROTECTION);
635 flash_write_cmd (info, 0, 0, FLASH_CMD_READ_ID);
636 memcpy (buffer, src + offset, len);
637 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
640 #endif /* CFG_FLASH_PROTECTION */
643 * flash_is_busy - check to see if the flash is busy
644 * This routine checks the status of the chip and returns true if the chip is busy
646 static int flash_is_busy (flash_info_t * info, flash_sect_t sect)
650 switch (info->vendor) {
651 case CFI_CMDSET_INTEL_STANDARD:
652 case CFI_CMDSET_INTEL_EXTENDED:
653 retval = !flash_isset (info, sect, 0, FLASH_STATUS_DONE);
655 case CFI_CMDSET_AMD_STANDARD:
656 case CFI_CMDSET_AMD_EXTENDED:
657 retval = flash_toggle (info, sect, 0, AMD_STATUS_TOGGLE);
662 debug ("flash_is_busy: %d\n", retval);
666 /*-----------------------------------------------------------------------
667 * wait for XSR.7 to be set. Time out with an error if it does not.
668 * This routine does not set the flash to read-array mode.
670 static int flash_status_check (flash_info_t * info, flash_sect_t sector,
671 ulong tout, char *prompt)
675 /* Wait for command completion */
676 start = get_timer (0);
677 while (flash_is_busy (info, sector)) {
678 if (get_timer (start) > info->erase_blk_tout * CFG_HZ) {
679 printf ("Flash %s timeout at address %lx data %lx\n",
680 prompt, info->start[sector],
681 flash_read_long (info, sector, 0));
682 flash_write_cmd (info, sector, 0, info->cmd_reset);
689 /*-----------------------------------------------------------------------
690 * Wait for XSR.7 to be set, if it times out print an error, otherwise do a full status check.
691 * This routine sets the flash to read-array mode.
693 static int flash_full_status_check (flash_info_t * info, flash_sect_t sector,
694 ulong tout, char *prompt)
698 retcode = flash_status_check (info, sector, tout, prompt);
699 switch (info->vendor) {
700 case CFI_CMDSET_INTEL_EXTENDED:
701 case CFI_CMDSET_INTEL_STANDARD:
702 if ((retcode != ERR_OK)
703 && !flash_isequal (info, sector, 0, FLASH_STATUS_DONE)) {
705 printf ("Flash %s error at address %lx\n", prompt,
706 info->start[sector]);
707 if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS | FLASH_STATUS_PSLBS)) {
708 puts ("Command Sequence Error.\n");
709 } else if (flash_isset (info, sector, 0, FLASH_STATUS_ECLBS)) {
710 puts ("Block Erase Error.\n");
711 retcode = ERR_NOT_ERASED;
712 } else if (flash_isset (info, sector, 0, FLASH_STATUS_PSLBS)) {
713 puts ("Locking Error\n");
715 if (flash_isset (info, sector, 0, FLASH_STATUS_DPS)) {
716 puts ("Block locked.\n");
717 retcode = ERR_PROTECTED;
719 if (flash_isset (info, sector, 0, FLASH_STATUS_VPENS))
720 puts ("Vpp Low Error.\n");
722 flash_write_cmd (info, sector, 0, FLASH_CMD_RESET);
730 /*-----------------------------------------------------------------------
732 static void flash_add_byte (flash_info_t * info, cfiword_t * cword, uchar c)
734 #if defined(__LITTLE_ENDIAN)
737 unsigned long long ll;
740 switch (info->portwidth) {
744 case FLASH_CFI_16BIT:
745 #if defined(__LITTLE_ENDIAN)
748 cword->w = (cword->w >> 8) | w;
750 cword->w = (cword->w << 8) | c;
753 case FLASH_CFI_32BIT:
754 #if defined(__LITTLE_ENDIAN)
757 cword->l = (cword->l >> 8) | l;
759 cword->l = (cword->l << 8) | c;
762 case FLASH_CFI_64BIT:
763 #if defined(__LITTLE_ENDIAN)
766 cword->ll = (cword->ll >> 8) | ll;
768 cword->ll = (cword->ll << 8) | c;
775 /*-----------------------------------------------------------------------
776 * make a proper sized command based on the port and chip widths
778 static void flash_make_cmd (flash_info_t * info, uchar cmd, void *cmdbuf)
782 #if defined(__LITTLE_ENDIAN)
786 uchar *cp = (uchar *) cmdbuf;
788 for (i = 0; i < info->portwidth; i++)
789 *cp++ = ((i + 1) % info->chipwidth) ? '\0' : cmd;
790 #if defined(__LITTLE_ENDIAN)
791 switch (info->portwidth) {
794 case FLASH_CFI_16BIT:
795 stmpw = *(ushort *) cmdbuf;
796 *(ushort *) cmdbuf = __swab16 (stmpw);
798 case FLASH_CFI_32BIT:
799 stmpi = *(uint *) cmdbuf;
800 *(uint *) cmdbuf = __swab32 (stmpi);
803 puts ("WARNING: flash_make_cmd: unsuppported LittleEndian mode\n");
810 * Write a proper sized command to the correct address
812 static void flash_write_cmd (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
815 volatile cfiptr_t addr;
818 addr.cp = flash_make_addr (info, sect, offset);
819 flash_make_cmd (info, cmd, &cword);
820 switch (info->portwidth) {
822 debug ("fwc addr %p cmd %x %x 8bit x %d bit\n", addr.cp, cmd,
823 cword.c, info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
826 case FLASH_CFI_16BIT:
827 debug ("fwc addr %p cmd %x %4.4x 16bit x %d bit\n", addr.wp,
829 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
832 case FLASH_CFI_32BIT:
833 debug ("fwc addr %p cmd %x %8.8lx 32bit x %d bit\n", addr.lp,
835 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
838 case FLASH_CFI_64BIT:
843 print_longlong (str, cword.ll);
845 debug ("fwrite addr %p cmd %x %s 64 bit x %d bit\n",
847 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
850 *addr.llp = cword.ll;
855 static void flash_unlock_seq (flash_info_t * info, flash_sect_t sect)
857 flash_write_cmd (info, sect, AMD_ADDR_START, AMD_CMD_UNLOCK_START);
858 flash_write_cmd (info, sect, AMD_ADDR_ACK, AMD_CMD_UNLOCK_ACK);
861 /*-----------------------------------------------------------------------
863 static int flash_isequal (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
869 cptr.cp = flash_make_addr (info, sect, offset);
870 flash_make_cmd (info, cmd, &cword);
872 debug ("is= cmd %x(%c) addr %p ", cmd, cmd, cptr.cp);
873 switch (info->portwidth) {
875 debug ("is= %x %x\n", cptr.cp[0], cword.c);
876 retval = (cptr.cp[0] == cword.c);
878 case FLASH_CFI_16BIT:
879 debug ("is= %4.4x %4.4x\n", cptr.wp[0], cword.w);
880 retval = (cptr.wp[0] == cword.w);
882 case FLASH_CFI_32BIT:
883 debug ("is= %8.8lx %8.8lx\n", cptr.lp[0], cword.l);
884 retval = (cptr.lp[0] == cword.l);
886 case FLASH_CFI_64BIT:
892 print_longlong (str1, cptr.llp[0]);
893 print_longlong (str2, cword.ll);
894 debug ("is= %s %s\n", str1, str2);
897 retval = (cptr.llp[0] == cword.ll);
906 /*-----------------------------------------------------------------------
908 static int flash_isset (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
914 cptr.cp = flash_make_addr (info, sect, offset);
915 flash_make_cmd (info, cmd, &cword);
916 switch (info->portwidth) {
918 retval = ((cptr.cp[0] & cword.c) == cword.c);
920 case FLASH_CFI_16BIT:
921 retval = ((cptr.wp[0] & cword.w) == cword.w);
923 case FLASH_CFI_32BIT:
924 retval = ((cptr.lp[0] & cword.l) == cword.l);
926 case FLASH_CFI_64BIT:
927 retval = ((cptr.llp[0] & cword.ll) == cword.ll);
936 /*-----------------------------------------------------------------------
938 static int flash_toggle (flash_info_t * info, flash_sect_t sect, uint offset, uchar cmd)
944 cptr.cp = flash_make_addr (info, sect, offset);
945 flash_make_cmd (info, cmd, &cword);
946 switch (info->portwidth) {
948 retval = ((cptr.cp[0] & cword.c) != (cptr.cp[0] & cword.c));
950 case FLASH_CFI_16BIT:
951 retval = ((cptr.wp[0] & cword.w) != (cptr.wp[0] & cword.w));
953 case FLASH_CFI_32BIT:
954 retval = ((cptr.lp[0] & cword.l) != (cptr.lp[0] & cword.l));
956 case FLASH_CFI_64BIT:
957 retval = ((cptr.llp[0] & cword.ll) !=
958 (cptr.llp[0] & cword.ll));
967 /*-----------------------------------------------------------------------
968 * detect if flash is compatible with the Common Flash Interface (CFI)
969 * http://www.jedec.org/download/search/jesd68.pdf
972 static int flash_detect_cfi (flash_info_t * info)
974 debug ("flash detect cfi\n");
976 for (info->portwidth = FLASH_CFI_8BIT;
977 info->portwidth <= FLASH_CFI_64BIT; info->portwidth <<= 1) {
978 for (info->chipwidth = FLASH_CFI_BY8;
979 info->chipwidth <= info->portwidth;
980 info->chipwidth <<= 1) {
981 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
982 flash_write_cmd (info, 0, FLASH_OFFSET_CFI, FLASH_CMD_CFI);
983 if (flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP, 'Q')
984 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 1, 'R')
985 && flash_isequal (info, 0, FLASH_OFFSET_CFI_RESP + 2, 'Y')) {
986 info->interface = flash_read_ushort (info, 0, FLASH_OFFSET_INTERFACE);
987 debug ("device interface is %d\n",
989 debug ("found port %d chip %d ",
990 info->portwidth, info->chipwidth);
991 debug ("port %d bits chip %d bits\n",
992 info->portwidth << CFI_FLASH_SHIFT_WIDTH,
993 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
998 debug ("not found\n");
1003 * The following code cannot be run from FLASH!
1006 static ulong flash_get_size (ulong base, int banknum)
1008 flash_info_t *info = &flash_info[banknum];
1010 flash_sect_t sect_cnt;
1011 unsigned long sector;
1014 uchar num_erase_regions;
1015 int erase_region_size;
1016 int erase_region_count;
1018 info->start[0] = base;
1020 if (flash_detect_cfi (info)) {
1021 info->vendor = flash_read_ushort (info, 0, FLASH_OFFSET_PRIMARY_VENDOR);
1023 flash_printqry (info, 0);
1025 switch (info->vendor) {
1026 case CFI_CMDSET_INTEL_STANDARD:
1027 case CFI_CMDSET_INTEL_EXTENDED:
1029 info->cmd_reset = FLASH_CMD_RESET;
1031 case CFI_CMDSET_AMD_STANDARD:
1032 case CFI_CMDSET_AMD_EXTENDED:
1033 info->cmd_reset = AMD_CMD_RESET;
1037 debug ("manufacturer is %d\n", info->vendor);
1038 size_ratio = info->portwidth / info->chipwidth;
1039 /* if the chip is x8/x16 reduce the ratio by half */
1040 if ((info->interface == FLASH_CFI_X8X16)
1041 && (info->chipwidth == FLASH_CFI_BY8)) {
1044 num_erase_regions = flash_read_uchar (info, FLASH_OFFSET_NUM_ERASE_REGIONS);
1045 debug ("size_ratio %d port %d bits chip %d bits\n",
1046 size_ratio, info->portwidth << CFI_FLASH_SHIFT_WIDTH,
1047 info->chipwidth << CFI_FLASH_SHIFT_WIDTH);
1048 debug ("found %d erase regions\n", num_erase_regions);
1051 for (i = 0; i < num_erase_regions; i++) {
1052 if (i > NUM_ERASE_REGIONS) {
1053 printf ("%d erase regions found, only %d used\n",
1054 num_erase_regions, NUM_ERASE_REGIONS);
1057 tmp = flash_read_long (info, 0,
1058 FLASH_OFFSET_ERASE_REGIONS +
1061 (tmp & 0xffff) ? ((tmp & 0xffff) * 256) : 128;
1063 erase_region_count = (tmp & 0xffff) + 1;
1064 debug ("erase_region_count = %d erase_region_size = %d\n",
1065 erase_region_count, erase_region_size);
1066 for (j = 0; j < erase_region_count; j++) {
1067 info->start[sect_cnt] = sector;
1068 sector += (erase_region_size * size_ratio);
1071 * Only read protection status from supported devices (intel...)
1073 switch (info->vendor) {
1074 case CFI_CMDSET_INTEL_EXTENDED:
1075 case CFI_CMDSET_INTEL_STANDARD:
1076 info->protect[sect_cnt] =
1077 flash_isset (info, sect_cnt,
1078 FLASH_OFFSET_PROTECT,
1079 FLASH_STATUS_PROTECT);
1082 info->protect[sect_cnt] = 0; /* default: not protected */
1089 info->sector_count = sect_cnt;
1090 /* multiply the size by the number of chips */
1091 info->size = (1 << flash_read_uchar (info, FLASH_OFFSET_SIZE)) * size_ratio;
1092 info->buffer_size = (1 << flash_read_ushort (info, 0, FLASH_OFFSET_BUFFER_SIZE));
1093 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_ETOUT);
1094 info->erase_blk_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_EMAX_TOUT)));
1095 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WBTOUT);
1096 info->buffer_write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WBMAX_TOUT)));
1097 tmp = 1 << flash_read_uchar (info, FLASH_OFFSET_WTOUT);
1098 info->write_tout = (tmp * (1 << flash_read_uchar (info, FLASH_OFFSET_WMAX_TOUT))) / 1000;
1099 info->flash_id = FLASH_MAN_CFI;
1100 if ((info->interface == FLASH_CFI_X8X16) && (info->chipwidth == FLASH_CFI_BY8)) {
1101 info->portwidth >>= 1; /* XXX - Need to test on x8/x16 in parallel. */
1105 flash_write_cmd (info, 0, 0, FLASH_CMD_RESET);
1106 return (info->size);
1110 /*-----------------------------------------------------------------------
1112 static int flash_write_cfiword (flash_info_t * info, ulong dest,
1120 ctladdr.cp = flash_make_addr (info, 0, 0);
1121 cptr.cp = (uchar *) dest;
1124 /* Check if Flash is (sufficiently) erased */
1125 switch (info->portwidth) {
1126 case FLASH_CFI_8BIT:
1127 flag = ((cptr.cp[0] & cword.c) == cword.c);
1129 case FLASH_CFI_16BIT:
1130 flag = ((cptr.wp[0] & cword.w) == cword.w);
1132 case FLASH_CFI_32BIT:
1133 flag = ((cptr.lp[0] & cword.l) == cword.l);
1135 case FLASH_CFI_64BIT:
1136 flag = ((cptr.llp[0] & cword.ll) == cword.ll);
1144 /* Disable interrupts which might cause a timeout here */
1145 flag = disable_interrupts ();
1147 switch (info->vendor) {
1148 case CFI_CMDSET_INTEL_EXTENDED:
1149 case CFI_CMDSET_INTEL_STANDARD:
1150 flash_write_cmd (info, 0, 0, FLASH_CMD_CLEAR_STATUS);
1151 flash_write_cmd (info, 0, 0, FLASH_CMD_WRITE);
1153 case CFI_CMDSET_AMD_EXTENDED:
1154 case CFI_CMDSET_AMD_STANDARD:
1155 flash_unlock_seq (info, 0);
1156 flash_write_cmd (info, 0, AMD_ADDR_START, AMD_CMD_WRITE);
1160 switch (info->portwidth) {
1161 case FLASH_CFI_8BIT:
1162 cptr.cp[0] = cword.c;
1164 case FLASH_CFI_16BIT:
1165 cptr.wp[0] = cword.w;
1167 case FLASH_CFI_32BIT:
1168 cptr.lp[0] = cword.l;
1170 case FLASH_CFI_64BIT:
1171 cptr.llp[0] = cword.ll;
1175 /* re-enable interrupts if necessary */
1177 enable_interrupts ();
1179 return flash_full_status_check (info, 0, info->write_tout, "write");
1182 #ifdef CFG_FLASH_USE_BUFFER_WRITE
1184 /* loop through the sectors from the highest address
1185 * when the passed address is greater or equal to the sector address
1188 static flash_sect_t find_sector (flash_info_t * info, ulong addr)
1190 flash_sect_t sector;
1192 for (sector = info->sector_count - 1; sector >= 0; sector--) {
1193 if (addr >= info->start[sector])
1199 static int flash_write_cfibuffer (flash_info_t * info, ulong dest, uchar * cp,
1202 flash_sect_t sector;
1205 volatile cfiptr_t src;
1206 volatile cfiptr_t dst;
1207 /* buffered writes in the AMD chip set is not supported yet */
1208 if((info->vendor == CFI_CMDSET_AMD_STANDARD) ||
1209 (info->vendor == CFI_CMDSET_AMD_EXTENDED))
1213 dst.cp = (uchar *) dest;
1214 sector = find_sector (info, dest);
1215 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1216 flash_write_cmd (info, sector, 0, FLASH_CMD_WRITE_TO_BUFFER);
1218 flash_status_check (info, sector, info->buffer_write_tout,
1219 "write to buffer")) == ERR_OK) {
1220 /* reduce the number of loops by the width of the port */
1221 switch (info->portwidth) {
1222 case FLASH_CFI_8BIT:
1225 case FLASH_CFI_16BIT:
1228 case FLASH_CFI_32BIT:
1231 case FLASH_CFI_64BIT:
1238 flash_write_cmd (info, sector, 0, (uchar) cnt - 1);
1240 switch (info->portwidth) {
1241 case FLASH_CFI_8BIT:
1242 *dst.cp++ = *src.cp++;
1244 case FLASH_CFI_16BIT:
1245 *dst.wp++ = *src.wp++;
1247 case FLASH_CFI_32BIT:
1248 *dst.lp++ = *src.lp++;
1250 case FLASH_CFI_64BIT:
1251 *dst.llp++ = *src.llp++;
1258 flash_write_cmd (info, sector, 0,
1259 FLASH_CMD_WRITE_BUFFER_CONFIRM);
1261 flash_full_status_check (info, sector,
1262 info->buffer_write_tout,
1265 flash_write_cmd (info, sector, 0, FLASH_CMD_CLEAR_STATUS);
1268 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
1269 #endif /* CFG_FLASH_CFI */