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>
17 #include <linux/log2.h>
19 #include "sf_internal.h"
21 DECLARE_GLOBAL_DATA_PTR;
23 static void spi_flash_addr(u32 addr, u8 *cmd)
25 /* cmd[0] is actual command */
31 /* Read commands array */
32 static u8 spi_read_cmds_array[] = {
35 CMD_READ_DUAL_OUTPUT_FAST,
36 CMD_READ_DUAL_IO_FAST,
37 CMD_READ_QUAD_OUTPUT_FAST,
38 CMD_READ_QUAD_IO_FAST,
41 static int read_sr(struct spi_flash *flash, u8 *rs)
46 cmd = CMD_READ_STATUS;
47 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
49 debug("SF: fail to read status register\n");
56 static int read_fsr(struct spi_flash *flash, u8 *fsr)
59 const u8 cmd = CMD_FLAG_STATUS;
61 ret = spi_flash_read_common(flash, &cmd, 1, fsr, 1);
63 debug("SF: fail to read flag status register\n");
70 static int write_sr(struct spi_flash *flash, u8 ws)
75 cmd = CMD_WRITE_STATUS;
76 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
78 debug("SF: fail to write status register\n");
85 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
86 static int read_cr(struct spi_flash *flash, u8 *rc)
91 cmd = CMD_READ_CONFIG;
92 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
94 debug("SF: fail to read config register\n");
101 static int write_cr(struct spi_flash *flash, u8 wc)
107 ret = read_sr(flash, &data[0]);
111 cmd = CMD_WRITE_STATUS;
113 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
115 debug("SF: fail to write config register\n");
123 #ifdef CONFIG_SPI_FLASH_BAR
124 static int spi_flash_write_bar(struct spi_flash *flash, u32 offset)
129 bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
130 if (bank_sel == flash->bank_curr)
133 cmd = flash->bank_write_cmd;
134 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
136 debug("SF: fail to write bank register\n");
141 flash->bank_curr = bank_sel;
142 return flash->bank_curr;
145 static int spi_flash_read_bar(struct spi_flash *flash, u8 idcode0)
150 if (flash->size <= SPI_FLASH_16MB_BOUN)
154 case SPI_FLASH_CFI_MFR_SPANSION:
155 flash->bank_read_cmd = CMD_BANKADDR_BRRD;
156 flash->bank_write_cmd = CMD_BANKADDR_BRWR;
159 flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
160 flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
163 ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
166 debug("SF: fail to read bank addr register\n");
171 flash->bank_curr = curr_bank;
176 #ifdef CONFIG_SF_DUAL_FLASH
177 static void spi_flash_dual(struct spi_flash *flash, u32 *addr)
179 switch (flash->dual_flash) {
180 case SF_DUAL_STACKED_FLASH:
181 if (*addr >= (flash->size >> 1)) {
182 *addr -= flash->size >> 1;
183 flash->spi->flags |= SPI_XFER_U_PAGE;
185 flash->spi->flags &= ~SPI_XFER_U_PAGE;
188 case SF_DUAL_PARALLEL_FLASH:
189 *addr >>= flash->shift;
192 debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
198 static int spi_flash_sr_ready(struct spi_flash *flash)
203 ret = read_sr(flash, &sr);
207 return !(sr & STATUS_WIP);
210 static int spi_flash_fsr_ready(struct spi_flash *flash)
215 ret = read_fsr(flash, &fsr);
219 return fsr & STATUS_PEC;
222 static int spi_flash_ready(struct spi_flash *flash)
226 sr = spi_flash_sr_ready(flash);
231 if (flash->flags & SNOR_F_USE_FSR) {
232 fsr = spi_flash_fsr_ready(flash);
240 static int spi_flash_cmd_wait_ready(struct spi_flash *flash,
241 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, &erase_addr);
329 #ifdef CONFIG_SPI_FLASH_BAR
330 ret = spi_flash_write_bar(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, &write_addr);
379 #ifdef CONFIG_SPI_FLASH_BAR
380 ret = spi_flash_write_bar(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, &read_addr);
474 #ifdef CONFIG_SPI_FLASH_BAR
475 ret = spi_flash_write_bar(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 = read_sr(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 = read_sr(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 write_sr(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 = read_sr(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 write_sr(flash, status_new);
804 #ifdef CONFIG_SPI_FLASH_MACRONIX
805 static int spi_flash_set_qeb_mxic(struct spi_flash *flash)
810 ret = read_sr(flash, &qeb_status);
814 if (qeb_status & STATUS_QEB_MXIC) {
815 debug("SF: mxic: QEB is already set\n");
817 ret = write_sr(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 = read_cr(flash, &qeb_status);
836 if (qeb_status & STATUS_QEB_WINSPAN) {
837 debug("SF: winspan: QEB is already set\n");
839 ret = write_cr(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_flash *flash)
901 struct spi_slave *spi = flash->spi;
902 const struct spi_flash_params *params;
903 u16 jedec, ext_jedec;
908 /* Read the ID codes */
909 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
911 printf("SF: Failed to get idcodes\n");
916 printf("SF: Got idcodes\n");
917 print_buffer(0, idcode, 1, sizeof(idcode), 0);
920 jedec = idcode[1] << 8 | idcode[2];
921 ext_jedec = idcode[3] << 8 | idcode[4];
923 /* Validate params from spi_flash_params table */
924 params = spi_flash_params_table;
925 for (; params->name != NULL; params++) {
926 if ((params->jedec >> 16) == idcode[0]) {
927 if ((params->jedec & 0xFFFF) == jedec) {
928 if (params->ext_jedec == 0)
930 else if (params->ext_jedec == ext_jedec)
937 printf("SF: Unsupported flash IDs: ");
938 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
939 idcode[0], jedec, ext_jedec);
940 return -EPROTONOSUPPORT;
943 /* Flash powers up read-only, so clear BP# bits */
944 if (idcode[0] == SPI_FLASH_CFI_MFR_ATMEL ||
945 idcode[0] == SPI_FLASH_CFI_MFR_MACRONIX ||
946 idcode[0] == SPI_FLASH_CFI_MFR_SST)
949 /* Assign spi data */
950 flash->name = params->name;
951 flash->memory_map = spi->memory_map;
952 flash->dual_flash = flash->spi->option;
954 /* Assign spi flash flags */
955 if (params->flags & SST_WR)
956 flash->flags |= SNOR_F_SST_WR;
958 /* Assign spi_flash ops */
959 #ifndef CONFIG_DM_SPI_FLASH
960 flash->write = spi_flash_cmd_write_ops;
961 #if defined(CONFIG_SPI_FLASH_SST)
962 if (flash->flags & SNOR_F_SST_WR) {
963 if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
964 flash->write = sst_write_bp;
966 flash->write = sst_write_wp;
969 flash->erase = spi_flash_cmd_erase_ops;
970 flash->read = spi_flash_cmd_read_ops;
973 /* lock hooks are flash specific - assign them based on idcode0 */
975 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
976 case SPI_FLASH_CFI_MFR_STMICRO:
977 case SPI_FLASH_CFI_MFR_SST:
978 flash->flash_lock = stm_lock;
979 flash->flash_unlock = stm_unlock;
980 flash->flash_is_locked = stm_is_locked;
984 debug("SF: Lock ops not supported for %02x flash\n", idcode[0]);
987 /* Compute the flash size */
988 flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
990 * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
991 * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
992 * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
995 if (ext_jedec == 0x4d00) {
996 if ((jedec == 0x0215) || (jedec == 0x216))
997 flash->page_size = 256;
999 flash->page_size = 512;
1001 flash->page_size = 256;
1003 flash->page_size <<= flash->shift;
1004 flash->sector_size = params->sector_size << flash->shift;
1005 flash->size = flash->sector_size * params->nr_sectors << flash->shift;
1006 #ifdef CONFIG_SF_DUAL_FLASH
1007 if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
1011 /* Compute erase sector and command */
1012 if (params->flags & SECT_4K) {
1013 flash->erase_cmd = CMD_ERASE_4K;
1014 flash->erase_size = 4096 << flash->shift;
1015 } else if (params->flags & SECT_32K) {
1016 flash->erase_cmd = CMD_ERASE_32K;
1017 flash->erase_size = 32768 << flash->shift;
1019 flash->erase_cmd = CMD_ERASE_64K;
1020 flash->erase_size = flash->sector_size;
1023 /* Now erase size becomes valid sector size */
1024 flash->sector_size = flash->erase_size;
1026 /* Look for the fastest read cmd */
1027 cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
1029 cmd = spi_read_cmds_array[cmd - 1];
1030 flash->read_cmd = cmd;
1032 /* Go for default supported read cmd */
1033 flash->read_cmd = CMD_READ_ARRAY_FAST;
1036 /* Not require to look for fastest only two write cmds yet */
1037 if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
1038 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
1040 /* Go for default supported write cmd */
1041 flash->write_cmd = CMD_PAGE_PROGRAM;
1043 /* Set the quad enable bit - only for quad commands */
1044 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
1045 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
1046 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
1047 ret = spi_flash_set_qeb(flash, idcode[0]);
1049 debug("SF: Fail to set QEB for %02x\n", idcode[0]);
1054 /* Read dummy_byte: dummy byte is determined based on the
1055 * dummy cycles of a particular command.
1056 * Fast commands - dummy_byte = dummy_cycles/8
1057 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
1058 * For I/O commands except cmd[0] everything goes on no.of lines
1059 * based on particular command but incase of fast commands except
1060 * data all go on single line irrespective of command.
1062 switch (flash->read_cmd) {
1063 case CMD_READ_QUAD_IO_FAST:
1064 flash->dummy_byte = 2;
1066 case CMD_READ_ARRAY_SLOW:
1067 flash->dummy_byte = 0;
1070 flash->dummy_byte = 1;
1073 #ifdef CONFIG_SPI_FLASH_STMICRO
1074 if (params->flags & E_FSR)
1075 flash->flags |= SNOR_F_USE_FSR;
1078 /* Configure the BAR - discover bank cmds and read current bank */
1079 #ifdef CONFIG_SPI_FLASH_BAR
1080 ret = spi_flash_read_bar(flash, idcode[0]);
1085 #if CONFIG_IS_ENABLED(OF_CONTROL)
1086 ret = spi_flash_decode_fdt(gd->fdt_blob, flash);
1088 debug("SF: FDT decode error\n");
1093 #ifndef CONFIG_SPL_BUILD
1094 printf("SF: Detected %s with page size ", flash->name);
1095 print_size(flash->page_size, ", erase size ");
1096 print_size(flash->erase_size, ", total ");
1097 print_size(flash->size, "");
1098 if (flash->memory_map)
1099 printf(", mapped at %p", flash->memory_map);
1103 #ifndef CONFIG_SPI_FLASH_BAR
1104 if (((flash->dual_flash == SF_SINGLE_FLASH) &&
1105 (flash->size > SPI_FLASH_16MB_BOUN)) ||
1106 ((flash->dual_flash > SF_SINGLE_FLASH) &&
1107 (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
1108 puts("SF: Warning - Only lower 16MiB accessible,");
1109 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");