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 int spi_flash_cmd_read_status(struct spi_flash
*flash
, u8
*rs
)
38 cmd
= CMD_READ_STATUS
;
39 ret
= spi_flash_read_common(flash
, &cmd
, 1, rs
, 1);
41 debug("SF: fail to read status register\n");
48 static int read_fsr(struct spi_flash
*flash
, u8
*fsr
)
51 const u8 cmd
= CMD_FLAG_STATUS
;
53 ret
= spi_flash_read_common(flash
, &cmd
, 1, fsr
, 1);
55 debug("SF: fail to read flag status register\n");
62 int spi_flash_cmd_write_status(struct spi_flash
*flash
, u8 ws
)
67 cmd
= CMD_WRITE_STATUS
;
68 ret
= spi_flash_write_common(flash
, &cmd
, 1, &ws
, 1);
70 debug("SF: fail to write status register\n");
77 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
78 int spi_flash_cmd_read_config(struct spi_flash
*flash
, u8
*rc
)
83 cmd
= CMD_READ_CONFIG
;
84 ret
= spi_flash_read_common(flash
, &cmd
, 1, rc
, 1);
86 debug("SF: fail to read config register\n");
93 int spi_flash_cmd_write_config(struct spi_flash
*flash
, u8 wc
)
99 ret
= spi_flash_cmd_read_status(flash
, &data
[0]);
103 cmd
= CMD_WRITE_STATUS
;
105 ret
= spi_flash_write_common(flash
, &cmd
, 1, &data
, 2);
107 debug("SF: fail to write config register\n");
115 #ifdef CONFIG_SPI_FLASH_BAR
116 static int spi_flash_write_bank(struct spi_flash
*flash
, u32 offset
)
121 bank_sel
= offset
/ (SPI_FLASH_16MB_BOUN
<< flash
->shift
);
122 if (bank_sel
== flash
->bank_curr
)
125 cmd
= flash
->bank_write_cmd
;
126 ret
= spi_flash_write_common(flash
, &cmd
, 1, &bank_sel
, 1);
128 debug("SF: fail to write bank register\n");
133 flash
->bank_curr
= bank_sel
;
134 return flash
->bank_curr
;
138 #ifdef CONFIG_SF_DUAL_FLASH
139 static void spi_flash_dual_flash(struct spi_flash
*flash
, u32
*addr
)
141 switch (flash
->dual_flash
) {
142 case SF_DUAL_STACKED_FLASH
:
143 if (*addr
>= (flash
->size
>> 1)) {
144 *addr
-= flash
->size
>> 1;
145 flash
->spi
->flags
|= SPI_XFER_U_PAGE
;
147 flash
->spi
->flags
&= ~SPI_XFER_U_PAGE
;
150 case SF_DUAL_PARALLEL_FLASH
:
151 *addr
>>= flash
->shift
;
154 debug("SF: Unsupported dual_flash=%d\n", flash
->dual_flash
);
160 static int spi_flash_sr_ready(struct spi_flash
*flash
)
165 ret
= spi_flash_cmd_read_status(flash
, &sr
);
169 return !(sr
& STATUS_WIP
);
172 static int spi_flash_fsr_ready(struct spi_flash
*flash
)
177 ret
= read_fsr(flash
, &fsr
);
181 return fsr
& STATUS_PEC
;
184 static int spi_flash_ready(struct spi_flash
*flash
)
188 sr
= spi_flash_sr_ready(flash
);
193 if (flash
->flags
& SNOR_F_USE_FSR
) {
194 fsr
= spi_flash_fsr_ready(flash
);
202 int spi_flash_cmd_wait_ready(struct spi_flash
*flash
, unsigned long timeout
)
206 timebase
= get_timer(0);
208 while (get_timer(timebase
) < timeout
) {
209 ret
= spi_flash_ready(flash
);
216 printf("SF: Timeout!\n");
221 int spi_flash_write_common(struct spi_flash
*flash
, const u8
*cmd
,
222 size_t cmd_len
, const void *buf
, size_t buf_len
)
224 struct spi_slave
*spi
= flash
->spi
;
225 unsigned long timeout
= SPI_FLASH_PROG_TIMEOUT
;
229 timeout
= SPI_FLASH_PAGE_ERASE_TIMEOUT
;
231 ret
= spi_claim_bus(flash
->spi
);
233 debug("SF: unable to claim SPI bus\n");
237 ret
= spi_flash_cmd_write_enable(flash
);
239 debug("SF: enabling write failed\n");
243 ret
= spi_flash_cmd_write(spi
, cmd
, cmd_len
, buf
, buf_len
);
245 debug("SF: write cmd failed\n");
249 ret
= spi_flash_cmd_wait_ready(flash
, timeout
);
251 debug("SF: write %s timed out\n",
252 timeout
== SPI_FLASH_PROG_TIMEOUT
?
253 "program" : "page erase");
257 spi_release_bus(spi
);
262 int spi_flash_cmd_erase_ops(struct spi_flash
*flash
, u32 offset
, size_t len
)
264 u32 erase_size
, erase_addr
;
265 u8 cmd
[SPI_FLASH_CMD_LEN
];
268 erase_size
= flash
->erase_size
;
269 if (offset
% erase_size
|| len
% erase_size
) {
270 debug("SF: Erase offset/length not multiple of erase size\n");
274 if (flash
->flash_is_locked
) {
275 if (flash
->flash_is_locked(flash
, offset
, len
) > 0) {
276 printf("offset 0x%x is protected and cannot be erased\n",
282 cmd
[0] = flash
->erase_cmd
;
286 #ifdef CONFIG_SF_DUAL_FLASH
287 if (flash
->dual_flash
> SF_SINGLE_FLASH
)
288 spi_flash_dual_flash(flash
, &erase_addr
);
290 #ifdef CONFIG_SPI_FLASH_BAR
291 ret
= spi_flash_write_bank(flash
, erase_addr
);
295 spi_flash_addr(erase_addr
, cmd
);
297 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd
[0], cmd
[1],
298 cmd
[2], cmd
[3], erase_addr
);
300 ret
= spi_flash_write_common(flash
, cmd
, sizeof(cmd
), NULL
, 0);
302 debug("SF: erase failed\n");
306 offset
+= erase_size
;
313 int spi_flash_cmd_write_ops(struct spi_flash
*flash
, u32 offset
,
314 size_t len
, const void *buf
)
316 unsigned long byte_addr
, page_size
;
318 size_t chunk_len
, actual
;
319 u8 cmd
[SPI_FLASH_CMD_LEN
];
322 page_size
= flash
->page_size
;
324 if (flash
->flash_is_locked
) {
325 if (flash
->flash_is_locked(flash
, offset
, len
) > 0) {
326 printf("offset 0x%x is protected and cannot be written\n",
332 cmd
[0] = flash
->write_cmd
;
333 for (actual
= 0; actual
< len
; actual
+= chunk_len
) {
336 #ifdef CONFIG_SF_DUAL_FLASH
337 if (flash
->dual_flash
> SF_SINGLE_FLASH
)
338 spi_flash_dual_flash(flash
, &write_addr
);
340 #ifdef CONFIG_SPI_FLASH_BAR
341 ret
= spi_flash_write_bank(flash
, write_addr
);
345 byte_addr
= offset
% page_size
;
346 chunk_len
= min(len
- actual
, (size_t)(page_size
- byte_addr
));
348 if (flash
->spi
->max_write_size
)
349 chunk_len
= min(chunk_len
,
350 (size_t)flash
->spi
->max_write_size
);
352 spi_flash_addr(write_addr
, cmd
);
354 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
355 buf
+ actual
, cmd
[0], cmd
[1], cmd
[2], cmd
[3], chunk_len
);
357 ret
= spi_flash_write_common(flash
, cmd
, sizeof(cmd
),
358 buf
+ actual
, chunk_len
);
360 debug("SF: write failed\n");
370 int spi_flash_read_common(struct spi_flash
*flash
, const u8
*cmd
,
371 size_t cmd_len
, void *data
, size_t data_len
)
373 struct spi_slave
*spi
= flash
->spi
;
376 ret
= spi_claim_bus(flash
->spi
);
378 debug("SF: unable to claim SPI bus\n");
382 ret
= spi_flash_cmd_read(spi
, cmd
, cmd_len
, data
, data_len
);
384 debug("SF: read cmd failed\n");
388 spi_release_bus(spi
);
393 void __weak
spi_flash_copy_mmap(void *data
, void *offset
, size_t len
)
395 memcpy(data
, offset
, len
);
398 int spi_flash_cmd_read_ops(struct spi_flash
*flash
, u32 offset
,
399 size_t len
, void *data
)
402 u32 remain_len
, read_len
, read_addr
;
406 /* Handle memory-mapped SPI */
407 if (flash
->memory_map
) {
408 ret
= spi_claim_bus(flash
->spi
);
410 debug("SF: unable to claim SPI bus\n");
413 spi_xfer(flash
->spi
, 0, NULL
, NULL
, SPI_XFER_MMAP
);
414 spi_flash_copy_mmap(data
, flash
->memory_map
+ offset
, len
);
415 spi_xfer(flash
->spi
, 0, NULL
, NULL
, SPI_XFER_MMAP_END
);
416 spi_release_bus(flash
->spi
);
420 cmdsz
= SPI_FLASH_CMD_LEN
+ flash
->dummy_byte
;
421 cmd
= calloc(1, cmdsz
);
423 debug("SF: Failed to allocate cmd\n");
427 cmd
[0] = flash
->read_cmd
;
431 #ifdef CONFIG_SF_DUAL_FLASH
432 if (flash
->dual_flash
> SF_SINGLE_FLASH
)
433 spi_flash_dual_flash(flash
, &read_addr
);
435 #ifdef CONFIG_SPI_FLASH_BAR
436 ret
= spi_flash_write_bank(flash
, read_addr
);
439 bank_sel
= flash
->bank_curr
;
441 remain_len
= ((SPI_FLASH_16MB_BOUN
<< flash
->shift
) *
442 (bank_sel
+ 1)) - offset
;
443 if (len
< remain_len
)
446 read_len
= remain_len
;
448 spi_flash_addr(read_addr
, cmd
);
450 ret
= spi_flash_read_common(flash
, cmd
, cmdsz
, data
, read_len
);
452 debug("SF: read failed\n");
465 #ifdef CONFIG_SPI_FLASH_SST
466 static int sst_byte_write(struct spi_flash
*flash
, u32 offset
, const void *buf
)
476 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
477 spi_w8r8(flash
->spi
, CMD_READ_STATUS
), buf
, cmd
[0], offset
);
479 ret
= spi_flash_cmd_write_enable(flash
);
483 ret
= spi_flash_cmd_write(flash
->spi
, cmd
, sizeof(cmd
), buf
, 1);
487 return spi_flash_cmd_wait_ready(flash
, SPI_FLASH_PROG_TIMEOUT
);
490 int sst_write_wp(struct spi_flash
*flash
, u32 offset
, size_t len
,
493 size_t actual
, cmd_len
;
497 ret
= spi_claim_bus(flash
->spi
);
499 debug("SF: Unable to claim SPI bus\n");
503 /* If the data is not word aligned, write out leading single byte */
506 ret
= sst_byte_write(flash
, offset
, buf
);
512 ret
= spi_flash_cmd_write_enable(flash
);
517 cmd
[0] = CMD_SST_AAI_WP
;
518 cmd
[1] = offset
>> 16;
519 cmd
[2] = offset
>> 8;
522 for (; actual
< len
- 1; actual
+= 2) {
523 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
524 spi_w8r8(flash
->spi
, CMD_READ_STATUS
), buf
+ actual
,
527 ret
= spi_flash_cmd_write(flash
->spi
, cmd
, cmd_len
,
530 debug("SF: sst word program failed\n");
534 ret
= spi_flash_cmd_wait_ready(flash
, SPI_FLASH_PROG_TIMEOUT
);
543 ret
= spi_flash_cmd_write_disable(flash
);
545 /* If there is a single trailing byte, write it out */
546 if (!ret
&& actual
!= len
)
547 ret
= sst_byte_write(flash
, offset
, buf
+ actual
);
550 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
551 ret
? "failure" : "success", len
, offset
- actual
);
553 spi_release_bus(flash
->spi
);
557 int sst_write_bp(struct spi_flash
*flash
, u32 offset
, size_t len
,
563 ret
= spi_claim_bus(flash
->spi
);
565 debug("SF: Unable to claim SPI bus\n");
569 for (actual
= 0; actual
< len
; actual
++) {
570 ret
= sst_byte_write(flash
, offset
, buf
+ actual
);
572 debug("SF: sst byte program failed\n");
579 ret
= spi_flash_cmd_write_disable(flash
);
581 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
582 ret
? "failure" : "success", len
, offset
- actual
);
584 spi_release_bus(flash
->spi
);
589 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
590 static void stm_get_locked_range(struct spi_flash
*flash
, u8 sr
, loff_t
*ofs
,
593 u8 mask
= SR_BP2
| SR_BP1
| SR_BP0
;
594 int shift
= ffs(mask
) - 1;
602 pow
= ((sr
& mask
) ^ mask
) >> shift
;
603 *len
= flash
->size
>> pow
;
604 *ofs
= flash
->size
- *len
;
609 * Return 1 if the entire region is locked, 0 otherwise
611 static int stm_is_locked_sr(struct spi_flash
*flash
, u32 ofs
, u32 len
,
617 stm_get_locked_range(flash
, sr
, &lock_offs
, &lock_len
);
619 return (ofs
+ len
<= lock_offs
+ lock_len
) && (ofs
>= lock_offs
);
623 * Check if a region of the flash is (completely) locked. See stm_lock() for
626 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
627 * negative on errors.
629 int stm_is_locked(struct spi_flash
*flash
, u32 ofs
, size_t len
)
634 status
= spi_flash_cmd_read_status(flash
, &sr
);
638 return stm_is_locked_sr(flash
, ofs
, len
, sr
);
642 * Lock a region of the flash. Compatible with ST Micro and similar flash.
643 * Supports only the block protection bits BP{0,1,2} in the status register
644 * (SR). Does not support these features found in newer SR bitfields:
645 * - TB: top/bottom protect - only handle TB=0 (top protect)
646 * - SEC: sector/block protect - only handle SEC=0 (block protect)
647 * - CMP: complement protect - only support CMP=0 (range is not complemented)
649 * Sample table portion for 8MB flash (Winbond w25q64fw):
651 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
652 * --------------------------------------------------------------------------
653 * X | X | 0 | 0 | 0 | NONE | NONE
654 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
655 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
656 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
657 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
658 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
659 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
660 * X | X | 1 | 1 | 1 | 8 MB | ALL
662 * Returns negative on errors, 0 on success.
664 int stm_lock(struct spi_flash
*flash
, u32 ofs
, size_t len
)
666 u8 status_old
, status_new
;
667 u8 mask
= SR_BP2
| SR_BP1
| SR_BP0
;
668 u8 shift
= ffs(mask
) - 1, pow
, val
;
671 ret
= spi_flash_cmd_read_status(flash
, &status_old
);
675 /* SPI NOR always locks to the end */
676 if (ofs
+ len
!= flash
->size
) {
677 /* Does combined region extend to end? */
678 if (!stm_is_locked_sr(flash
, ofs
+ len
, flash
->size
- ofs
- len
,
681 len
= flash
->size
- ofs
;
685 * Need smallest pow such that:
687 * 1 / (2^pow) <= (len / size)
689 * so (assuming power-of-2 size) we do:
691 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
693 pow
= ilog2(flash
->size
) - ilog2(len
);
694 val
= mask
- (pow
<< shift
);
698 /* Don't "lock" with no region! */
702 status_new
= (status_old
& ~mask
) | val
;
704 /* Only modify protection if it will not unlock other areas */
705 if ((status_new
& mask
) <= (status_old
& mask
))
708 spi_flash_cmd_write_status(flash
, status_new
);
714 * Unlock a region of the flash. See stm_lock() for more info
716 * Returns negative on errors, 0 on success.
718 int stm_unlock(struct spi_flash
*flash
, u32 ofs
, size_t len
)
720 uint8_t status_old
, status_new
;
721 u8 mask
= SR_BP2
| SR_BP1
| SR_BP0
;
722 u8 shift
= ffs(mask
) - 1, pow
, val
;
725 ret
= spi_flash_cmd_read_status(flash
, &status_old
);
729 /* Cannot unlock; would unlock larger region than requested */
730 if (stm_is_locked_sr(flash
, status_old
, ofs
- flash
->erase_size
,
734 * Need largest pow such that:
736 * 1 / (2^pow) >= (len / size)
738 * so (assuming power-of-2 size) we do:
740 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
742 pow
= ilog2(flash
->size
) - order_base_2(flash
->size
- (ofs
+ len
));
743 if (ofs
+ len
== flash
->size
) {
744 val
= 0; /* fully unlocked */
746 val
= mask
- (pow
<< shift
);
747 /* Some power-of-two sizes are not supported */
752 status_new
= (status_old
& ~mask
) | val
;
754 /* Only modify protection if it will not lock other areas */
755 if ((status_new
& mask
) >= (status_old
& mask
))
758 spi_flash_cmd_write_status(flash
, status_new
);
765 /* Read commands array */
766 static u8 spi_read_cmds_array
[] = {
769 CMD_READ_DUAL_OUTPUT_FAST
,
770 CMD_READ_DUAL_IO_FAST
,
771 CMD_READ_QUAD_OUTPUT_FAST
,
772 CMD_READ_QUAD_IO_FAST
,
775 #ifdef CONFIG_SPI_FLASH_MACRONIX
776 static int spi_flash_set_qeb_mxic(struct spi_flash
*flash
)
781 ret
= spi_flash_cmd_read_status(flash
, &qeb_status
);
785 if (qeb_status
& STATUS_QEB_MXIC
) {
786 debug("SF: mxic: QEB is already set\n");
788 ret
= spi_flash_cmd_write_status(flash
, STATUS_QEB_MXIC
);
797 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
798 static int spi_flash_set_qeb_winspan(struct spi_flash
*flash
)
803 ret
= spi_flash_cmd_read_config(flash
, &qeb_status
);
807 if (qeb_status
& STATUS_QEB_WINSPAN
) {
808 debug("SF: winspan: QEB is already set\n");
810 ret
= spi_flash_cmd_write_config(flash
, STATUS_QEB_WINSPAN
);
819 static int spi_flash_set_qeb(struct spi_flash
*flash
, u8 idcode0
)
822 #ifdef CONFIG_SPI_FLASH_MACRONIX
823 case SPI_FLASH_CFI_MFR_MACRONIX
:
824 return spi_flash_set_qeb_mxic(flash
);
826 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
827 case SPI_FLASH_CFI_MFR_SPANSION
:
828 case SPI_FLASH_CFI_MFR_WINBOND
:
829 return spi_flash_set_qeb_winspan(flash
);
831 #ifdef CONFIG_SPI_FLASH_STMICRO
832 case SPI_FLASH_CFI_MFR_STMICRO
:
833 debug("SF: QEB is volatile for %02x flash\n", idcode0
);
837 printf("SF: Need set QEB func for %02x flash\n", idcode0
);
842 #ifdef CONFIG_SPI_FLASH_BAR
843 static int spi_flash_read_bank(struct spi_flash
*flash
, u8 idcode0
)
848 if (flash
->size
<= SPI_FLASH_16MB_BOUN
)
852 case SPI_FLASH_CFI_MFR_SPANSION
:
853 flash
->bank_read_cmd
= CMD_BANKADDR_BRRD
;
854 flash
->bank_write_cmd
= CMD_BANKADDR_BRWR
;
856 flash
->bank_read_cmd
= CMD_EXTNADDR_RDEAR
;
857 flash
->bank_write_cmd
= CMD_EXTNADDR_WREAR
;
860 ret
= spi_flash_read_common(flash
, &flash
->bank_read_cmd
, 1,
863 debug("SF: fail to read bank addr register\n");
868 flash
->bank_curr
= curr_bank
;
873 #if CONFIG_IS_ENABLED(OF_CONTROL)
874 int spi_flash_decode_fdt(const void *blob
, struct spi_flash
*flash
)
880 /* If there is no node, do nothing */
881 node
= fdtdec_next_compatible(blob
, 0, COMPAT_GENERIC_SPI_FLASH
);
885 addr
= fdtdec_get_addr_size(blob
, node
, "memory-map", &size
);
886 if (addr
== FDT_ADDR_T_NONE
) {
887 debug("%s: Cannot decode address\n", __func__
);
891 if (flash
->size
!= size
) {
892 debug("%s: Memory map must cover entire device\n", __func__
);
895 flash
->memory_map
= map_sysmem(addr
, size
);
899 #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
901 int spi_flash_scan(struct spi_slave
*spi
, struct spi_flash
*flash
)
903 const struct spi_flash_params
*params
;
904 u16 jedec
, ext_jedec
;
909 /* Read the ID codes */
910 ret
= spi_flash_cmd(spi
, CMD_READ_ID
, idcode
, sizeof(idcode
));
912 printf("SF: Failed to get idcodes\n");
917 printf("SF: Got idcodes\n");
918 print_buffer(0, idcode
, 1, sizeof(idcode
), 0);
921 jedec
= idcode
[1] << 8 | idcode
[2];
922 ext_jedec
= idcode
[3] << 8 | idcode
[4];
924 /* Validate params from spi_flash_params table */
925 params
= spi_flash_params_table
;
926 for (; params
->name
!= NULL
; params
++) {
927 if ((params
->jedec
>> 16) == idcode
[0]) {
928 if ((params
->jedec
& 0xFFFF) == jedec
) {
929 if (params
->ext_jedec
== 0)
931 else if (params
->ext_jedec
== ext_jedec
)
938 printf("SF: Unsupported flash IDs: ");
939 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
940 idcode
[0], jedec
, ext_jedec
);
941 return -EPROTONOSUPPORT
;
944 /* Flash powers up read-only, so clear BP# bits */
945 #if defined(CONFIG_SPI_FLASH_ATMEL) || \
946 defined(CONFIG_SPI_FLASH_MACRONIX) || \
947 defined(CONFIG_SPI_FLASH_SST)
948 spi_flash_cmd_write_status(flash
, 0);
951 /* Assign spi data */
953 flash
->name
= params
->name
;
954 flash
->memory_map
= spi
->memory_map
;
955 flash
->dual_flash
= flash
->spi
->option
;
957 /* Assign spi flash flags */
958 if (params
->flags
& SST_WR
)
959 flash
->flags
|= SNOR_F_SST_WR
;
961 /* Assign spi_flash ops */
962 #ifndef CONFIG_DM_SPI_FLASH
963 flash
->write
= spi_flash_cmd_write_ops
;
964 #if defined(CONFIG_SPI_FLASH_SST)
965 if (flash
->flags
& SNOR_F_SST_WR
) {
966 if (flash
->spi
->op_mode_tx
& SPI_OPM_TX_BP
)
967 flash
->write
= sst_write_bp
;
969 flash
->write
= sst_write_wp
;
972 flash
->erase
= spi_flash_cmd_erase_ops
;
973 flash
->read
= spi_flash_cmd_read_ops
;
976 /* lock hooks are flash specific - assign them based on idcode0 */
978 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
979 case SPI_FLASH_CFI_MFR_STMICRO
:
980 case SPI_FLASH_CFI_MFR_SST
:
981 flash
->flash_lock
= stm_lock
;
982 flash
->flash_unlock
= stm_unlock
;
983 flash
->flash_is_locked
= stm_is_locked
;
987 debug("SF: Lock ops not supported for %02x flash\n", idcode
[0]);
990 /* Compute the flash size */
991 flash
->shift
= (flash
->dual_flash
& SF_DUAL_PARALLEL_FLASH
) ? 1 : 0;
993 * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
994 * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
995 * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
998 if (ext_jedec
== 0x4d00) {
999 if ((jedec
== 0x0215) || (jedec
== 0x216))
1000 flash
->page_size
= 256;
1002 flash
->page_size
= 512;
1004 flash
->page_size
= 256;
1006 flash
->page_size
<<= flash
->shift
;
1007 flash
->sector_size
= params
->sector_size
<< flash
->shift
;
1008 flash
->size
= flash
->sector_size
* params
->nr_sectors
<< flash
->shift
;
1009 #ifdef CONFIG_SF_DUAL_FLASH
1010 if (flash
->dual_flash
& SF_DUAL_STACKED_FLASH
)
1014 /* Compute erase sector and command */
1015 if (params
->flags
& SECT_4K
) {
1016 flash
->erase_cmd
= CMD_ERASE_4K
;
1017 flash
->erase_size
= 4096 << flash
->shift
;
1018 } else if (params
->flags
& SECT_32K
) {
1019 flash
->erase_cmd
= CMD_ERASE_32K
;
1020 flash
->erase_size
= 32768 << flash
->shift
;
1022 flash
->erase_cmd
= CMD_ERASE_64K
;
1023 flash
->erase_size
= flash
->sector_size
;
1026 /* Now erase size becomes valid sector size */
1027 flash
->sector_size
= flash
->erase_size
;
1029 /* Look for the fastest read cmd */
1030 cmd
= fls(params
->e_rd_cmd
& flash
->spi
->op_mode_rx
);
1032 cmd
= spi_read_cmds_array
[cmd
- 1];
1033 flash
->read_cmd
= cmd
;
1035 /* Go for default supported read cmd */
1036 flash
->read_cmd
= CMD_READ_ARRAY_FAST
;
1039 /* Not require to look for fastest only two write cmds yet */
1040 if (params
->flags
& WR_QPP
&& flash
->spi
->op_mode_tx
& SPI_OPM_TX_QPP
)
1041 flash
->write_cmd
= CMD_QUAD_PAGE_PROGRAM
;
1043 /* Go for default supported write cmd */
1044 flash
->write_cmd
= CMD_PAGE_PROGRAM
;
1046 /* Set the quad enable bit - only for quad commands */
1047 if ((flash
->read_cmd
== CMD_READ_QUAD_OUTPUT_FAST
) ||
1048 (flash
->read_cmd
== CMD_READ_QUAD_IO_FAST
) ||
1049 (flash
->write_cmd
== CMD_QUAD_PAGE_PROGRAM
)) {
1050 ret
= spi_flash_set_qeb(flash
, idcode
[0]);
1052 debug("SF: Fail to set QEB for %02x\n", idcode
[0]);
1057 /* Read dummy_byte: dummy byte is determined based on the
1058 * dummy cycles of a particular command.
1059 * Fast commands - dummy_byte = dummy_cycles/8
1060 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
1061 * For I/O commands except cmd[0] everything goes on no.of lines
1062 * based on particular command but incase of fast commands except
1063 * data all go on single line irrespective of command.
1065 switch (flash
->read_cmd
) {
1066 case CMD_READ_QUAD_IO_FAST
:
1067 flash
->dummy_byte
= 2;
1069 case CMD_READ_ARRAY_SLOW
:
1070 flash
->dummy_byte
= 0;
1073 flash
->dummy_byte
= 1;
1076 #ifdef CONFIG_SPI_FLASH_STMICRO
1077 if (params
->flags
& E_FSR
)
1078 flash
->flags
|= SNOR_F_USE_FSR
;
1081 /* Configure the BAR - discover bank cmds and read current bank */
1082 #ifdef CONFIG_SPI_FLASH_BAR
1083 ret
= spi_flash_read_bank(flash
, idcode
[0]);
1088 #if CONFIG_IS_ENABLED(OF_CONTROL)
1089 ret
= spi_flash_decode_fdt(gd
->fdt_blob
, flash
);
1091 debug("SF: FDT decode error\n");
1096 #ifndef CONFIG_SPL_BUILD
1097 printf("SF: Detected %s with page size ", flash
->name
);
1098 print_size(flash
->page_size
, ", erase size ");
1099 print_size(flash
->erase_size
, ", total ");
1100 print_size(flash
->size
, "");
1101 if (flash
->memory_map
)
1102 printf(", mapped at %p", flash
->memory_map
);
1106 #ifndef CONFIG_SPI_FLASH_BAR
1107 if (((flash
->dual_flash
== SF_SINGLE_FLASH
) &&
1108 (flash
->size
> SPI_FLASH_16MB_BOUN
)) ||
1109 ((flash
->dual_flash
> SF_SINGLE_FLASH
) &&
1110 (flash
->size
> SPI_FLASH_16MB_BOUN
<< 1))) {
1111 puts("SF: Warning - Only lower 16MiB accessible,");
1112 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");