4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
8 * SPDX-License-Identifier: GPL-2.0+
16 #include <spi_flash.h>
18 #include <linux/compiler.h>
19 #include <linux/log2.h>
21 #include "sf_internal.h"
23 DECLARE_GLOBAL_DATA_PTR
;
25 static void spi_flash_addr(u32 addr
, u8
*cmd
)
27 /* cmd[0] is actual command */
33 /* Read commands array */
34 static u8 spi_read_cmds_array
[] = {
37 CMD_READ_DUAL_OUTPUT_FAST
,
38 CMD_READ_DUAL_IO_FAST
,
39 CMD_READ_QUAD_OUTPUT_FAST
,
40 CMD_READ_QUAD_IO_FAST
,
43 int spi_flash_cmd_read_status(struct spi_flash
*flash
, u8
*rs
)
48 cmd
= CMD_READ_STATUS
;
49 ret
= spi_flash_read_common(flash
, &cmd
, 1, rs
, 1);
51 debug("SF: fail to read status register\n");
58 static int read_fsr(struct spi_flash
*flash
, u8
*fsr
)
61 const u8 cmd
= CMD_FLAG_STATUS
;
63 ret
= spi_flash_read_common(flash
, &cmd
, 1, fsr
, 1);
65 debug("SF: fail to read flag status register\n");
72 int spi_flash_cmd_write_status(struct spi_flash
*flash
, u8 ws
)
77 cmd
= CMD_WRITE_STATUS
;
78 ret
= spi_flash_write_common(flash
, &cmd
, 1, &ws
, 1);
80 debug("SF: fail to write status register\n");
87 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
88 int spi_flash_cmd_read_config(struct spi_flash
*flash
, u8
*rc
)
93 cmd
= CMD_READ_CONFIG
;
94 ret
= spi_flash_read_common(flash
, &cmd
, 1, rc
, 1);
96 debug("SF: fail to read config register\n");
103 int spi_flash_cmd_write_config(struct spi_flash
*flash
, u8 wc
)
109 ret
= spi_flash_cmd_read_status(flash
, &data
[0]);
113 cmd
= CMD_WRITE_STATUS
;
115 ret
= spi_flash_write_common(flash
, &cmd
, 1, &data
, 2);
117 debug("SF: fail to write config register\n");
125 #ifdef CONFIG_SPI_FLASH_BAR
126 static int spi_flash_write_bank(struct spi_flash
*flash
, u32 offset
)
131 bank_sel
= offset
/ (SPI_FLASH_16MB_BOUN
<< flash
->shift
);
132 if (bank_sel
== flash
->bank_curr
)
135 cmd
= flash
->bank_write_cmd
;
136 ret
= spi_flash_write_common(flash
, &cmd
, 1, &bank_sel
, 1);
138 debug("SF: fail to write bank register\n");
143 flash
->bank_curr
= bank_sel
;
144 return flash
->bank_curr
;
147 static int spi_flash_read_bank(struct spi_flash
*flash
, u8 idcode0
)
152 if (flash
->size
<= SPI_FLASH_16MB_BOUN
)
156 case SPI_FLASH_CFI_MFR_SPANSION
:
157 flash
->bank_read_cmd
= CMD_BANKADDR_BRRD
;
158 flash
->bank_write_cmd
= CMD_BANKADDR_BRWR
;
160 flash
->bank_read_cmd
= CMD_EXTNADDR_RDEAR
;
161 flash
->bank_write_cmd
= CMD_EXTNADDR_WREAR
;
164 ret
= spi_flash_read_common(flash
, &flash
->bank_read_cmd
, 1,
167 debug("SF: fail to read bank addr register\n");
172 flash
->bank_curr
= curr_bank
;
177 #ifdef CONFIG_SF_DUAL_FLASH
178 static void spi_flash_dual_flash(struct spi_flash
*flash
, u32
*addr
)
180 switch (flash
->dual_flash
) {
181 case SF_DUAL_STACKED_FLASH
:
182 if (*addr
>= (flash
->size
>> 1)) {
183 *addr
-= flash
->size
>> 1;
184 flash
->spi
->flags
|= SPI_XFER_U_PAGE
;
186 flash
->spi
->flags
&= ~SPI_XFER_U_PAGE
;
189 case SF_DUAL_PARALLEL_FLASH
:
190 *addr
>>= flash
->shift
;
193 debug("SF: Unsupported dual_flash=%d\n", flash
->dual_flash
);
199 static int spi_flash_sr_ready(struct spi_flash
*flash
)
204 ret
= spi_flash_cmd_read_status(flash
, &sr
);
208 return !(sr
& STATUS_WIP
);
211 static int spi_flash_fsr_ready(struct spi_flash
*flash
)
216 ret
= read_fsr(flash
, &fsr
);
220 return fsr
& STATUS_PEC
;
223 static int spi_flash_ready(struct spi_flash
*flash
)
227 sr
= spi_flash_sr_ready(flash
);
232 if (flash
->flags
& SNOR_F_USE_FSR
) {
233 fsr
= spi_flash_fsr_ready(flash
);
241 int spi_flash_cmd_wait_ready(struct spi_flash
*flash
, unsigned long timeout
)
245 timebase
= get_timer(0);
247 while (get_timer(timebase
) < timeout
) {
248 ret
= spi_flash_ready(flash
);
255 printf("SF: Timeout!\n");
260 int spi_flash_write_common(struct spi_flash
*flash
, const u8
*cmd
,
261 size_t cmd_len
, const void *buf
, size_t buf_len
)
263 struct spi_slave
*spi
= flash
->spi
;
264 unsigned long timeout
= SPI_FLASH_PROG_TIMEOUT
;
268 timeout
= SPI_FLASH_PAGE_ERASE_TIMEOUT
;
270 ret
= spi_claim_bus(flash
->spi
);
272 debug("SF: unable to claim SPI bus\n");
276 ret
= spi_flash_cmd_write_enable(flash
);
278 debug("SF: enabling write failed\n");
282 ret
= spi_flash_cmd_write(spi
, cmd
, cmd_len
, buf
, buf_len
);
284 debug("SF: write cmd failed\n");
288 ret
= spi_flash_cmd_wait_ready(flash
, timeout
);
290 debug("SF: write %s timed out\n",
291 timeout
== SPI_FLASH_PROG_TIMEOUT
?
292 "program" : "page erase");
296 spi_release_bus(spi
);
301 int spi_flash_cmd_erase_ops(struct spi_flash
*flash
, u32 offset
, size_t len
)
303 u32 erase_size
, erase_addr
;
304 u8 cmd
[SPI_FLASH_CMD_LEN
];
307 erase_size
= flash
->erase_size
;
308 if (offset
% erase_size
|| len
% erase_size
) {
309 debug("SF: Erase offset/length not multiple of erase size\n");
313 if (flash
->flash_is_locked
) {
314 if (flash
->flash_is_locked(flash
, offset
, len
) > 0) {
315 printf("offset 0x%x is protected and cannot be erased\n",
321 cmd
[0] = flash
->erase_cmd
;
325 #ifdef CONFIG_SF_DUAL_FLASH
326 if (flash
->dual_flash
> SF_SINGLE_FLASH
)
327 spi_flash_dual_flash(flash
, &erase_addr
);
329 #ifdef CONFIG_SPI_FLASH_BAR
330 ret
= spi_flash_write_bank(flash
, erase_addr
);
334 spi_flash_addr(erase_addr
, cmd
);
336 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd
[0], cmd
[1],
337 cmd
[2], cmd
[3], erase_addr
);
339 ret
= spi_flash_write_common(flash
, cmd
, sizeof(cmd
), NULL
, 0);
341 debug("SF: erase failed\n");
345 offset
+= erase_size
;
352 int spi_flash_cmd_write_ops(struct spi_flash
*flash
, u32 offset
,
353 size_t len
, const void *buf
)
355 unsigned long byte_addr
, page_size
;
357 size_t chunk_len
, actual
;
358 u8 cmd
[SPI_FLASH_CMD_LEN
];
361 page_size
= flash
->page_size
;
363 if (flash
->flash_is_locked
) {
364 if (flash
->flash_is_locked(flash
, offset
, len
) > 0) {
365 printf("offset 0x%x is protected and cannot be written\n",
371 cmd
[0] = flash
->write_cmd
;
372 for (actual
= 0; actual
< len
; actual
+= chunk_len
) {
375 #ifdef CONFIG_SF_DUAL_FLASH
376 if (flash
->dual_flash
> SF_SINGLE_FLASH
)
377 spi_flash_dual_flash(flash
, &write_addr
);
379 #ifdef CONFIG_SPI_FLASH_BAR
380 ret
= spi_flash_write_bank(flash
, write_addr
);
384 byte_addr
= offset
% page_size
;
385 chunk_len
= min(len
- actual
, (size_t)(page_size
- byte_addr
));
387 if (flash
->spi
->max_write_size
)
388 chunk_len
= min(chunk_len
,
389 (size_t)flash
->spi
->max_write_size
);
391 spi_flash_addr(write_addr
, cmd
);
393 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
394 buf
+ actual
, cmd
[0], cmd
[1], cmd
[2], cmd
[3], chunk_len
);
396 ret
= spi_flash_write_common(flash
, cmd
, sizeof(cmd
),
397 buf
+ actual
, chunk_len
);
399 debug("SF: write failed\n");
409 int spi_flash_read_common(struct spi_flash
*flash
, const u8
*cmd
,
410 size_t cmd_len
, void *data
, size_t data_len
)
412 struct spi_slave
*spi
= flash
->spi
;
415 ret
= spi_claim_bus(flash
->spi
);
417 debug("SF: unable to claim SPI bus\n");
421 ret
= spi_flash_cmd_read(spi
, cmd
, cmd_len
, data
, data_len
);
423 debug("SF: read cmd failed\n");
427 spi_release_bus(spi
);
432 void __weak
spi_flash_copy_mmap(void *data
, void *offset
, size_t len
)
434 memcpy(data
, offset
, len
);
437 int spi_flash_cmd_read_ops(struct spi_flash
*flash
, u32 offset
,
438 size_t len
, void *data
)
441 u32 remain_len
, read_len
, read_addr
;
445 /* Handle memory-mapped SPI */
446 if (flash
->memory_map
) {
447 ret
= spi_claim_bus(flash
->spi
);
449 debug("SF: unable to claim SPI bus\n");
452 spi_xfer(flash
->spi
, 0, NULL
, NULL
, SPI_XFER_MMAP
);
453 spi_flash_copy_mmap(data
, flash
->memory_map
+ offset
, len
);
454 spi_xfer(flash
->spi
, 0, NULL
, NULL
, SPI_XFER_MMAP_END
);
455 spi_release_bus(flash
->spi
);
459 cmdsz
= SPI_FLASH_CMD_LEN
+ flash
->dummy_byte
;
460 cmd
= calloc(1, cmdsz
);
462 debug("SF: Failed to allocate cmd\n");
466 cmd
[0] = flash
->read_cmd
;
470 #ifdef CONFIG_SF_DUAL_FLASH
471 if (flash
->dual_flash
> SF_SINGLE_FLASH
)
472 spi_flash_dual_flash(flash
, &read_addr
);
474 #ifdef CONFIG_SPI_FLASH_BAR
475 ret
= spi_flash_write_bank(flash
, read_addr
);
478 bank_sel
= flash
->bank_curr
;
480 remain_len
= ((SPI_FLASH_16MB_BOUN
<< flash
->shift
) *
481 (bank_sel
+ 1)) - offset
;
482 if (len
< remain_len
)
485 read_len
= remain_len
;
487 spi_flash_addr(read_addr
, cmd
);
489 ret
= spi_flash_read_common(flash
, cmd
, cmdsz
, data
, read_len
);
491 debug("SF: read failed\n");
504 #ifdef CONFIG_SPI_FLASH_SST
505 static int sst_byte_write(struct spi_flash
*flash
, u32 offset
, const void *buf
)
515 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
516 spi_w8r8(flash
->spi
, CMD_READ_STATUS
), buf
, cmd
[0], offset
);
518 ret
= spi_flash_cmd_write_enable(flash
);
522 ret
= spi_flash_cmd_write(flash
->spi
, cmd
, sizeof(cmd
), buf
, 1);
526 return spi_flash_cmd_wait_ready(flash
, SPI_FLASH_PROG_TIMEOUT
);
529 int sst_write_wp(struct spi_flash
*flash
, u32 offset
, size_t len
,
532 size_t actual
, cmd_len
;
536 ret
= spi_claim_bus(flash
->spi
);
538 debug("SF: Unable to claim SPI bus\n");
542 /* If the data is not word aligned, write out leading single byte */
545 ret
= sst_byte_write(flash
, offset
, buf
);
551 ret
= spi_flash_cmd_write_enable(flash
);
556 cmd
[0] = CMD_SST_AAI_WP
;
557 cmd
[1] = offset
>> 16;
558 cmd
[2] = offset
>> 8;
561 for (; actual
< len
- 1; actual
+= 2) {
562 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
563 spi_w8r8(flash
->spi
, CMD_READ_STATUS
), buf
+ actual
,
566 ret
= spi_flash_cmd_write(flash
->spi
, cmd
, cmd_len
,
569 debug("SF: sst word program failed\n");
573 ret
= spi_flash_cmd_wait_ready(flash
, SPI_FLASH_PROG_TIMEOUT
);
582 ret
= spi_flash_cmd_write_disable(flash
);
584 /* If there is a single trailing byte, write it out */
585 if (!ret
&& actual
!= len
)
586 ret
= sst_byte_write(flash
, offset
, buf
+ actual
);
589 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
590 ret
? "failure" : "success", len
, offset
- actual
);
592 spi_release_bus(flash
->spi
);
596 int sst_write_bp(struct spi_flash
*flash
, u32 offset
, size_t len
,
602 ret
= spi_claim_bus(flash
->spi
);
604 debug("SF: Unable to claim SPI bus\n");
608 for (actual
= 0; actual
< len
; actual
++) {
609 ret
= sst_byte_write(flash
, offset
, buf
+ actual
);
611 debug("SF: sst byte program failed\n");
618 ret
= spi_flash_cmd_write_disable(flash
);
620 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
621 ret
? "failure" : "success", len
, offset
- actual
);
623 spi_release_bus(flash
->spi
);
628 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
629 static void stm_get_locked_range(struct spi_flash
*flash
, u8 sr
, loff_t
*ofs
,
632 u8 mask
= SR_BP2
| SR_BP1
| SR_BP0
;
633 int shift
= ffs(mask
) - 1;
641 pow
= ((sr
& mask
) ^ mask
) >> shift
;
642 *len
= flash
->size
>> pow
;
643 *ofs
= flash
->size
- *len
;
648 * Return 1 if the entire region is locked, 0 otherwise
650 static int stm_is_locked_sr(struct spi_flash
*flash
, u32 ofs
, u32 len
,
656 stm_get_locked_range(flash
, sr
, &lock_offs
, &lock_len
);
658 return (ofs
+ len
<= lock_offs
+ lock_len
) && (ofs
>= lock_offs
);
662 * Check if a region of the flash is (completely) locked. See stm_lock() for
665 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
666 * negative on errors.
668 int stm_is_locked(struct spi_flash
*flash
, u32 ofs
, size_t len
)
673 status
= spi_flash_cmd_read_status(flash
, &sr
);
677 return stm_is_locked_sr(flash
, ofs
, len
, sr
);
681 * Lock a region of the flash. Compatible with ST Micro and similar flash.
682 * Supports only the block protection bits BP{0,1,2} in the status register
683 * (SR). Does not support these features found in newer SR bitfields:
684 * - TB: top/bottom protect - only handle TB=0 (top protect)
685 * - SEC: sector/block protect - only handle SEC=0 (block protect)
686 * - CMP: complement protect - only support CMP=0 (range is not complemented)
688 * Sample table portion for 8MB flash (Winbond w25q64fw):
690 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
691 * --------------------------------------------------------------------------
692 * X | X | 0 | 0 | 0 | NONE | NONE
693 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
694 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
695 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
696 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
697 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
698 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
699 * X | X | 1 | 1 | 1 | 8 MB | ALL
701 * Returns negative on errors, 0 on success.
703 int stm_lock(struct spi_flash
*flash
, u32 ofs
, size_t len
)
705 u8 status_old
, status_new
;
706 u8 mask
= SR_BP2
| SR_BP1
| SR_BP0
;
707 u8 shift
= ffs(mask
) - 1, pow
, val
;
710 ret
= spi_flash_cmd_read_status(flash
, &status_old
);
714 /* SPI NOR always locks to the end */
715 if (ofs
+ len
!= flash
->size
) {
716 /* Does combined region extend to end? */
717 if (!stm_is_locked_sr(flash
, ofs
+ len
, flash
->size
- ofs
- len
,
720 len
= flash
->size
- ofs
;
724 * Need smallest pow such that:
726 * 1 / (2^pow) <= (len / size)
728 * so (assuming power-of-2 size) we do:
730 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
732 pow
= ilog2(flash
->size
) - ilog2(len
);
733 val
= mask
- (pow
<< shift
);
737 /* Don't "lock" with no region! */
741 status_new
= (status_old
& ~mask
) | val
;
743 /* Only modify protection if it will not unlock other areas */
744 if ((status_new
& mask
) <= (status_old
& mask
))
747 spi_flash_cmd_write_status(flash
, status_new
);
753 * Unlock a region of the flash. See stm_lock() for more info
755 * Returns negative on errors, 0 on success.
757 int stm_unlock(struct spi_flash
*flash
, u32 ofs
, size_t len
)
759 uint8_t status_old
, status_new
;
760 u8 mask
= SR_BP2
| SR_BP1
| SR_BP0
;
761 u8 shift
= ffs(mask
) - 1, pow
, val
;
764 ret
= spi_flash_cmd_read_status(flash
, &status_old
);
768 /* Cannot unlock; would unlock larger region than requested */
769 if (stm_is_locked_sr(flash
, status_old
, ofs
- flash
->erase_size
,
773 * Need largest pow such that:
775 * 1 / (2^pow) >= (len / size)
777 * so (assuming power-of-2 size) we do:
779 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
781 pow
= ilog2(flash
->size
) - order_base_2(flash
->size
- (ofs
+ len
));
782 if (ofs
+ len
== flash
->size
) {
783 val
= 0; /* fully unlocked */
785 val
= mask
- (pow
<< shift
);
786 /* Some power-of-two sizes are not supported */
791 status_new
= (status_old
& ~mask
) | val
;
793 /* Only modify protection if it will not lock other areas */
794 if ((status_new
& mask
) >= (status_old
& mask
))
797 spi_flash_cmd_write_status(flash
, status_new
);
804 #ifdef CONFIG_SPI_FLASH_MACRONIX
805 static int spi_flash_set_qeb_mxic(struct spi_flash
*flash
)
810 ret
= spi_flash_cmd_read_status(flash
, &qeb_status
);
814 if (qeb_status
& STATUS_QEB_MXIC
) {
815 debug("SF: mxic: QEB is already set\n");
817 ret
= spi_flash_cmd_write_status(flash
, STATUS_QEB_MXIC
);
826 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
827 static int spi_flash_set_qeb_winspan(struct spi_flash
*flash
)
832 ret
= spi_flash_cmd_read_config(flash
, &qeb_status
);
836 if (qeb_status
& STATUS_QEB_WINSPAN
) {
837 debug("SF: winspan: QEB is already set\n");
839 ret
= spi_flash_cmd_write_config(flash
, STATUS_QEB_WINSPAN
);
848 static int spi_flash_set_qeb(struct spi_flash
*flash
, u8 idcode0
)
851 #ifdef CONFIG_SPI_FLASH_MACRONIX
852 case SPI_FLASH_CFI_MFR_MACRONIX
:
853 return spi_flash_set_qeb_mxic(flash
);
855 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
856 case SPI_FLASH_CFI_MFR_SPANSION
:
857 case SPI_FLASH_CFI_MFR_WINBOND
:
858 return spi_flash_set_qeb_winspan(flash
);
860 #ifdef CONFIG_SPI_FLASH_STMICRO
861 case SPI_FLASH_CFI_MFR_STMICRO
:
862 debug("SF: QEB is volatile for %02x flash\n", idcode0
);
866 printf("SF: Need set QEB func for %02x flash\n", idcode0
);
871 #if CONFIG_IS_ENABLED(OF_CONTROL)
872 int spi_flash_decode_fdt(const void *blob
, struct spi_flash
*flash
)
878 /* If there is no node, do nothing */
879 node
= fdtdec_next_compatible(blob
, 0, COMPAT_GENERIC_SPI_FLASH
);
883 addr
= fdtdec_get_addr_size(blob
, node
, "memory-map", &size
);
884 if (addr
== FDT_ADDR_T_NONE
) {
885 debug("%s: Cannot decode address\n", __func__
);
889 if (flash
->size
!= size
) {
890 debug("%s: Memory map must cover entire device\n", __func__
);
893 flash
->memory_map
= map_sysmem(addr
, size
);
897 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
899 int spi_flash_scan(struct spi_slave
*spi
, struct spi_flash
*flash
)
901 const struct spi_flash_params
*params
;
902 u16 jedec
, ext_jedec
;
907 /* Read the ID codes */
908 ret
= spi_flash_cmd(spi
, CMD_READ_ID
, idcode
, sizeof(idcode
));
910 printf("SF: Failed to get idcodes\n");
915 printf("SF: Got idcodes\n");
916 print_buffer(0, idcode
, 1, sizeof(idcode
), 0);
919 jedec
= idcode
[1] << 8 | idcode
[2];
920 ext_jedec
= idcode
[3] << 8 | idcode
[4];
922 /* Validate params from spi_flash_params table */
923 params
= spi_flash_params_table
;
924 for (; params
->name
!= NULL
; params
++) {
925 if ((params
->jedec
>> 16) == idcode
[0]) {
926 if ((params
->jedec
& 0xFFFF) == jedec
) {
927 if (params
->ext_jedec
== 0)
929 else if (params
->ext_jedec
== ext_jedec
)
936 printf("SF: Unsupported flash IDs: ");
937 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
938 idcode
[0], jedec
, ext_jedec
);
939 return -EPROTONOSUPPORT
;
942 /* Flash powers up read-only, so clear BP# bits */
943 #if defined(CONFIG_SPI_FLASH_ATMEL) || \
944 defined(CONFIG_SPI_FLASH_MACRONIX) || \
945 defined(CONFIG_SPI_FLASH_SST)
946 spi_flash_cmd_write_status(flash
, 0);
949 /* Assign spi data */
951 flash
->name
= params
->name
;
952 flash
->memory_map
= spi
->memory_map
;
953 flash
->dual_flash
= flash
->spi
->option
;
955 /* Assign spi flash flags */
956 if (params
->flags
& SST_WR
)
957 flash
->flags
|= SNOR_F_SST_WR
;
959 /* Assign spi_flash ops */
960 #ifndef CONFIG_DM_SPI_FLASH
961 flash
->write
= spi_flash_cmd_write_ops
;
962 #if defined(CONFIG_SPI_FLASH_SST)
963 if (flash
->flags
& SNOR_F_SST_WR
) {
964 if (flash
->spi
->op_mode_tx
& SPI_OPM_TX_BP
)
965 flash
->write
= sst_write_bp
;
967 flash
->write
= sst_write_wp
;
970 flash
->erase
= spi_flash_cmd_erase_ops
;
971 flash
->read
= spi_flash_cmd_read_ops
;
974 /* lock hooks are flash specific - assign them based on idcode0 */
976 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
977 case SPI_FLASH_CFI_MFR_STMICRO
:
978 case SPI_FLASH_CFI_MFR_SST
:
979 flash
->flash_lock
= stm_lock
;
980 flash
->flash_unlock
= stm_unlock
;
981 flash
->flash_is_locked
= stm_is_locked
;
985 debug("SF: Lock ops not supported for %02x flash\n", idcode
[0]);
988 /* Compute the flash size */
989 flash
->shift
= (flash
->dual_flash
& SF_DUAL_PARALLEL_FLASH
) ? 1 : 0;
991 * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
992 * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
993 * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
996 if (ext_jedec
== 0x4d00) {
997 if ((jedec
== 0x0215) || (jedec
== 0x216))
998 flash
->page_size
= 256;
1000 flash
->page_size
= 512;
1002 flash
->page_size
= 256;
1004 flash
->page_size
<<= flash
->shift
;
1005 flash
->sector_size
= params
->sector_size
<< flash
->shift
;
1006 flash
->size
= flash
->sector_size
* params
->nr_sectors
<< flash
->shift
;
1007 #ifdef CONFIG_SF_DUAL_FLASH
1008 if (flash
->dual_flash
& SF_DUAL_STACKED_FLASH
)
1012 /* Compute erase sector and command */
1013 if (params
->flags
& SECT_4K
) {
1014 flash
->erase_cmd
= CMD_ERASE_4K
;
1015 flash
->erase_size
= 4096 << flash
->shift
;
1016 } else if (params
->flags
& SECT_32K
) {
1017 flash
->erase_cmd
= CMD_ERASE_32K
;
1018 flash
->erase_size
= 32768 << flash
->shift
;
1020 flash
->erase_cmd
= CMD_ERASE_64K
;
1021 flash
->erase_size
= flash
->sector_size
;
1024 /* Now erase size becomes valid sector size */
1025 flash
->sector_size
= flash
->erase_size
;
1027 /* Look for the fastest read cmd */
1028 cmd
= fls(params
->e_rd_cmd
& flash
->spi
->op_mode_rx
);
1030 cmd
= spi_read_cmds_array
[cmd
- 1];
1031 flash
->read_cmd
= cmd
;
1033 /* Go for default supported read cmd */
1034 flash
->read_cmd
= CMD_READ_ARRAY_FAST
;
1037 /* Not require to look for fastest only two write cmds yet */
1038 if (params
->flags
& WR_QPP
&& flash
->spi
->op_mode_tx
& SPI_OPM_TX_QPP
)
1039 flash
->write_cmd
= CMD_QUAD_PAGE_PROGRAM
;
1041 /* Go for default supported write cmd */
1042 flash
->write_cmd
= CMD_PAGE_PROGRAM
;
1044 /* Set the quad enable bit - only for quad commands */
1045 if ((flash
->read_cmd
== CMD_READ_QUAD_OUTPUT_FAST
) ||
1046 (flash
->read_cmd
== CMD_READ_QUAD_IO_FAST
) ||
1047 (flash
->write_cmd
== CMD_QUAD_PAGE_PROGRAM
)) {
1048 ret
= spi_flash_set_qeb(flash
, idcode
[0]);
1050 debug("SF: Fail to set QEB for %02x\n", idcode
[0]);
1055 /* Read dummy_byte: dummy byte is determined based on the
1056 * dummy cycles of a particular command.
1057 * Fast commands - dummy_byte = dummy_cycles/8
1058 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
1059 * For I/O commands except cmd[0] everything goes on no.of lines
1060 * based on particular command but incase of fast commands except
1061 * data all go on single line irrespective of command.
1063 switch (flash
->read_cmd
) {
1064 case CMD_READ_QUAD_IO_FAST
:
1065 flash
->dummy_byte
= 2;
1067 case CMD_READ_ARRAY_SLOW
:
1068 flash
->dummy_byte
= 0;
1071 flash
->dummy_byte
= 1;
1074 #ifdef CONFIG_SPI_FLASH_STMICRO
1075 if (params
->flags
& E_FSR
)
1076 flash
->flags
|= SNOR_F_USE_FSR
;
1079 /* Configure the BAR - discover bank cmds and read current bank */
1080 #ifdef CONFIG_SPI_FLASH_BAR
1081 ret
= spi_flash_read_bank(flash
, idcode
[0]);
1086 #if CONFIG_IS_ENABLED(OF_CONTROL)
1087 ret
= spi_flash_decode_fdt(gd
->fdt_blob
, flash
);
1089 debug("SF: FDT decode error\n");
1094 #ifndef CONFIG_SPL_BUILD
1095 printf("SF: Detected %s with page size ", flash
->name
);
1096 print_size(flash
->page_size
, ", erase size ");
1097 print_size(flash
->erase_size
, ", total ");
1098 print_size(flash
->size
, "");
1099 if (flash
->memory_map
)
1100 printf(", mapped at %p", flash
->memory_map
);
1104 #ifndef CONFIG_SPI_FLASH_BAR
1105 if (((flash
->dual_flash
== SF_SINGLE_FLASH
) &&
1106 (flash
->size
> SPI_FLASH_16MB_BOUN
)) ||
1107 ((flash
->dual_flash
> SF_SINGLE_FLASH
) &&
1108 (flash
->size
> SPI_FLASH_16MB_BOUN
<< 1))) {
1109 puts("SF: Warning - Only lower 16MiB accessible,");
1110 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");