1 // SPDX-License-Identifier: GPL-2.0
3 * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
4 * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
6 * Copyright (C) 2005, Intec Automation Inc.
7 * Copyright (C) 2014, Freescale Semiconductor, Inc.
9 * Synced from Linux v4.19
13 #include <dm/device_compat.h>
14 #include <dm/devres.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/log2.h>
18 #include <linux/math64.h>
19 #include <linux/sizes.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/spi-nor.h>
26 #include "sf_internal.h"
28 /* Define max times to check status register before we give up. */
31 * For everything but full-chip erase; probably could be much smaller, but kept
32 * around for safety for now
35 #define HZ CONFIG_SYS_HZ
37 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
39 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
42 if (op->data.dir == SPI_MEM_DATA_IN)
43 op->data.buf.in = buf;
45 op->data.buf.out = buf;
46 return spi_mem_exec_op(nor->spi, op);
49 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
51 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
54 SPI_MEM_OP_DATA_IN(len, NULL, 1));
57 ret = spi_nor_read_write_reg(nor, &op, val);
59 dev_dbg(nor->dev, "error %d reading %x\n", ret, code);
64 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
66 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
69 SPI_MEM_OP_DATA_OUT(len, NULL, 1));
71 return spi_nor_read_write_reg(nor, &op, buf);
74 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
77 struct spi_mem_op op =
78 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
79 SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
80 SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
81 SPI_MEM_OP_DATA_IN(len, buf, 1));
82 size_t remaining = len;
85 /* get transfer protocols. */
86 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
87 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
88 op.dummy.buswidth = op.addr.buswidth;
89 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
91 /* convert the dummy cycles to the number of bytes */
92 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
95 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
96 ret = spi_mem_adjust_op_size(nor->spi, &op);
100 ret = spi_mem_exec_op(nor->spi, &op);
104 op.addr.val += op.data.nbytes;
105 remaining -= op.data.nbytes;
106 op.data.buf.in += op.data.nbytes;
112 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
115 struct spi_mem_op op =
116 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
117 SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
119 SPI_MEM_OP_DATA_OUT(len, buf, 1));
122 /* get transfer protocols. */
123 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
124 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
125 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
127 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
130 ret = spi_mem_adjust_op_size(nor->spi, &op);
133 op.data.nbytes = len < op.data.nbytes ? len : op.data.nbytes;
135 ret = spi_mem_exec_op(nor->spi, &op);
139 return op.data.nbytes;
143 * Read the status register, returning its value in the location
144 * Return the status register value.
145 * Returns negative if error occurred.
147 static int read_sr(struct spi_nor *nor)
152 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
154 pr_debug("error %d reading SR\n", (int)ret);
162 * Read the flag status register, returning its value in the location
163 * Return the status register value.
164 * Returns negative if error occurred.
166 static int read_fsr(struct spi_nor *nor)
171 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
173 pr_debug("error %d reading FSR\n", ret);
181 * Read configuration register, returning its value in the
182 * location. Return the configuration register value.
183 * Returns negative if error occurred.
185 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
186 static int read_cr(struct spi_nor *nor)
191 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
193 dev_dbg(nor->dev, "error %d reading CR\n", ret);
202 * Write status register 1 byte
203 * Returns negative if error occurred.
205 static int write_sr(struct spi_nor *nor, u8 val)
207 nor->cmd_buf[0] = val;
208 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
212 * Set write enable latch with Write Enable command.
213 * Returns negative if error occurred.
215 static int write_enable(struct spi_nor *nor)
217 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
221 * Send write disable instruction to the chip.
223 static int write_disable(struct spi_nor *nor)
225 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
228 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
233 #ifndef CONFIG_SPI_FLASH_BAR
234 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
238 for (i = 0; i < size; i++)
239 if (table[i][0] == opcode)
242 /* No conversion found, keep input op code. */
246 static u8 spi_nor_convert_3to4_read(u8 opcode)
248 static const u8 spi_nor_3to4_read[][2] = {
249 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
250 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
251 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
252 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
253 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
254 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
255 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
256 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
258 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
259 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
260 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
263 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
264 ARRAY_SIZE(spi_nor_3to4_read));
267 static u8 spi_nor_convert_3to4_program(u8 opcode)
269 static const u8 spi_nor_3to4_program[][2] = {
270 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
271 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
272 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
273 { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B },
274 { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B },
277 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
278 ARRAY_SIZE(spi_nor_3to4_program));
281 static u8 spi_nor_convert_3to4_erase(u8 opcode)
283 static const u8 spi_nor_3to4_erase[][2] = {
284 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
285 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
286 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
289 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
290 ARRAY_SIZE(spi_nor_3to4_erase));
293 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
294 const struct flash_info *info)
296 /* Do some manufacturer fixups first */
297 switch (JEDEC_MFR(info)) {
298 case SNOR_MFR_SPANSION:
299 /* No small sector erase for 4-byte command set */
300 nor->erase_opcode = SPINOR_OP_SE;
301 nor->mtd.erasesize = info->sector_size;
308 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
309 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
310 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
312 #endif /* !CONFIG_SPI_FLASH_BAR */
314 /* Enable/disable 4-byte addressing mode. */
315 static int set_4byte(struct spi_nor *nor, const struct flash_info *info,
319 bool need_wren = false;
322 switch (JEDEC_MFR(info)) {
324 case SNOR_MFR_MICRON:
325 /* Some Micron need WREN command; all will accept it */
328 case SNOR_MFR_MACRONIX:
329 case SNOR_MFR_WINBOND:
333 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
334 status = nor->write_reg(nor, cmd, NULL, 0);
338 if (!status && !enable &&
339 JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
341 * On Winbond W25Q256FV, leaving 4byte mode causes
342 * the Extended Address Register to be set to 1, so all
343 * 3-byte-address reads come from the second 16M.
344 * We must clear the register to enable normal behavior.
348 nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
355 nor->cmd_buf[0] = enable << 7;
356 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
360 static int spi_nor_sr_ready(struct spi_nor *nor)
362 int sr = read_sr(nor);
367 if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
369 dev_dbg(nor->dev, "Erase Error occurred\n");
371 dev_dbg(nor->dev, "Programming Error occurred\n");
373 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
377 return !(sr & SR_WIP);
380 static int spi_nor_fsr_ready(struct spi_nor *nor)
382 int fsr = read_fsr(nor);
387 if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
389 dev_err(nor->dev, "Erase operation failed.\n");
391 dev_err(nor->dev, "Program operation failed.\n");
393 if (fsr & FSR_PT_ERR)
395 "Attempted to modify a protected sector.\n");
397 nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
401 return fsr & FSR_READY;
404 static int spi_nor_ready(struct spi_nor *nor)
408 sr = spi_nor_sr_ready(nor);
411 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
418 * Service routine to read status register until ready, or timeout occurs.
419 * Returns non-zero if error.
421 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
422 unsigned long timeout)
424 unsigned long timebase;
427 timebase = get_timer(0);
429 while (get_timer(timebase) < timeout) {
430 ret = spi_nor_ready(nor);
437 dev_err(nor->dev, "flash operation timed out\n");
442 static int spi_nor_wait_till_ready(struct spi_nor *nor)
444 return spi_nor_wait_till_ready_with_timeout(nor,
445 DEFAULT_READY_WAIT_JIFFIES);
448 #ifdef CONFIG_SPI_FLASH_BAR
450 * This "clean_bar" is necessary in a situation when one was accessing
451 * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
453 * After it the BA24 bit shall be cleared to allow access to correct
454 * memory region after SW reset (by calling "reset" command).
456 * Otherwise, the BA24 bit may be left set and then after reset, the
457 * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
459 static int clean_bar(struct spi_nor *nor)
461 u8 cmd, bank_sel = 0;
463 if (nor->bank_curr == 0)
465 cmd = nor->bank_write_cmd;
469 return nor->write_reg(nor, cmd, &bank_sel, 1);
472 static int write_bar(struct spi_nor *nor, u32 offset)
477 bank_sel = offset / SZ_16M;
478 if (bank_sel == nor->bank_curr)
481 cmd = nor->bank_write_cmd;
483 ret = nor->write_reg(nor, cmd, &bank_sel, 1);
485 debug("SF: fail to write bank register\n");
490 nor->bank_curr = bank_sel;
491 return nor->bank_curr;
494 static int read_bar(struct spi_nor *nor, const struct flash_info *info)
499 switch (JEDEC_MFR(info)) {
500 case SNOR_MFR_SPANSION:
501 nor->bank_read_cmd = SPINOR_OP_BRRD;
502 nor->bank_write_cmd = SPINOR_OP_BRWR;
505 nor->bank_read_cmd = SPINOR_OP_RDEAR;
506 nor->bank_write_cmd = SPINOR_OP_WREAR;
509 ret = nor->read_reg(nor, nor->bank_read_cmd,
512 debug("SF: fail to read bank addr register\n");
515 nor->bank_curr = curr_bank;
522 * Initiate the erasure of a single sector
524 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
526 struct spi_mem_op op =
527 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1),
528 SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
533 return nor->erase(nor, addr);
536 * Default implementation, if driver doesn't have a specialized HW
539 return spi_mem_exec_op(nor->spi, &op);
543 * Erase an address range on the nor chip. The address range may extend
544 * one or more erase sectors. Return an error is there is a problem erasing.
546 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
548 struct spi_nor *nor = mtd_to_spi_nor(mtd);
552 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
553 (long long)instr->len);
558 div_u64_rem(instr->len, mtd->erasesize, &rem);
566 #ifdef CONFIG_SPI_FLASH_BAR
567 ret = write_bar(nor, addr);
573 ret = spi_nor_erase_sector(nor, addr);
577 addr += mtd->erasesize;
578 len -= mtd->erasesize;
580 ret = spi_nor_wait_till_ready(nor);
586 #ifdef CONFIG_SPI_FLASH_BAR
587 ret = clean_bar(nor);
594 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
595 /* Write status register and ensure bits in mask match written values */
596 static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
601 ret = write_sr(nor, status_new);
605 ret = spi_nor_wait_till_ready(nor);
613 return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
616 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
619 struct mtd_info *mtd = &nor->mtd;
620 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
621 int shift = ffs(mask) - 1;
629 pow = ((sr & mask) ^ mask) >> shift;
630 *len = mtd->size >> pow;
631 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
634 *ofs = mtd->size - *len;
639 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
640 * @locked is false); 0 otherwise
642 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len,
651 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
654 /* Requested range is a sub-range of locked range */
655 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
657 /* Requested range does not overlap with locked range */
658 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
661 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
664 return stm_check_lock_status_sr(nor, ofs, len, sr, true);
667 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
670 return stm_check_lock_status_sr(nor, ofs, len, sr, false);
674 * Lock a region of the flash. Compatible with ST Micro and similar flash.
675 * Supports the block protection bits BP{0,1,2} in the status register
676 * (SR). Does not support these features found in newer SR bitfields:
677 * - SEC: sector/block protect - only handle SEC=0 (block protect)
678 * - CMP: complement protect - only support CMP=0 (range is not complemented)
680 * Support for the following is provided conditionally for some flash:
681 * - TB: top/bottom protect
683 * Sample table portion for 8MB flash (Winbond w25q64fw):
685 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
686 * --------------------------------------------------------------------------
687 * X | X | 0 | 0 | 0 | NONE | NONE
688 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
689 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
690 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
691 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
692 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
693 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
694 * X | X | 1 | 1 | 1 | 8 MB | ALL
695 * ------|-------|-------|-------|-------|---------------|-------------------
696 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
697 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
698 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
699 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
700 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
701 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
703 * Returns negative on errors, 0 on success.
705 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
707 struct mtd_info *mtd = &nor->mtd;
708 int status_old, status_new;
709 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
710 u8 shift = ffs(mask) - 1, pow, val;
712 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
715 status_old = read_sr(nor);
719 /* If nothing in our range is unlocked, we don't need to do anything */
720 if (stm_is_locked_sr(nor, ofs, len, status_old))
723 /* If anything below us is unlocked, we can't use 'bottom' protection */
724 if (!stm_is_locked_sr(nor, 0, ofs, status_old))
725 can_be_bottom = false;
727 /* If anything above us is unlocked, we can't use 'top' protection */
728 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
732 if (!can_be_bottom && !can_be_top)
735 /* Prefer top, if both are valid */
736 use_top = can_be_top;
738 /* lock_len: length of region that should end up locked */
740 lock_len = mtd->size - ofs;
742 lock_len = ofs + len;
745 * Need smallest pow such that:
747 * 1 / (2^pow) <= (len / size)
749 * so (assuming power-of-2 size) we do:
751 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
753 pow = ilog2(mtd->size) - ilog2(lock_len);
754 val = mask - (pow << shift);
757 /* Don't "lock" with no region! */
761 status_new = (status_old & ~mask & ~SR_TB) | val;
763 /* Disallow further writes if WP pin is asserted */
764 status_new |= SR_SRWD;
769 /* Don't bother if they're the same */
770 if (status_new == status_old)
773 /* Only modify protection if it will not unlock other areas */
774 if ((status_new & mask) < (status_old & mask))
777 return write_sr_and_check(nor, status_new, mask);
781 * Unlock a region of the flash. See stm_lock() for more info
783 * Returns negative on errors, 0 on success.
785 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
787 struct mtd_info *mtd = &nor->mtd;
788 int status_old, status_new;
789 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
790 u8 shift = ffs(mask) - 1, pow, val;
792 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
795 status_old = read_sr(nor);
799 /* If nothing in our range is locked, we don't need to do anything */
800 if (stm_is_unlocked_sr(nor, ofs, len, status_old))
803 /* If anything below us is locked, we can't use 'top' protection */
804 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
807 /* If anything above us is locked, we can't use 'bottom' protection */
808 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
810 can_be_bottom = false;
812 if (!can_be_bottom && !can_be_top)
815 /* Prefer top, if both are valid */
816 use_top = can_be_top;
818 /* lock_len: length of region that should remain locked */
820 lock_len = mtd->size - (ofs + len);
825 * Need largest pow such that:
827 * 1 / (2^pow) >= (len / size)
829 * so (assuming power-of-2 size) we do:
831 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
833 pow = ilog2(mtd->size) - order_base_2(lock_len);
835 val = 0; /* fully unlocked */
837 val = mask - (pow << shift);
838 /* Some power-of-two sizes are not supported */
843 status_new = (status_old & ~mask & ~SR_TB) | val;
845 /* Don't protect status register if we're fully unlocked */
847 status_new &= ~SR_SRWD;
852 /* Don't bother if they're the same */
853 if (status_new == status_old)
856 /* Only modify protection if it will not lock other areas */
857 if ((status_new & mask) > (status_old & mask))
860 return write_sr_and_check(nor, status_new, mask);
864 * Check if a region of the flash is (completely) locked. See stm_lock() for
867 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
868 * negative on errors.
870 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
874 status = read_sr(nor);
878 return stm_is_locked_sr(nor, ofs, len, status);
880 #endif /* CONFIG_SPI_FLASH_STMICRO */
882 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
885 u8 id[SPI_NOR_MAX_ID_LEN];
886 const struct flash_info *info;
888 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
890 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
895 for (; info->name; info++) {
897 if (!memcmp(info->id, id, info->id_len))
902 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
903 id[0], id[1], id[2]);
904 return ERR_PTR(-ENODEV);
907 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
908 size_t *retlen, u_char *buf)
910 struct spi_nor *nor = mtd_to_spi_nor(mtd);
913 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
917 size_t read_len = len;
919 #ifdef CONFIG_SPI_FLASH_BAR
922 ret = write_bar(nor, addr);
925 remain_len = (SZ_16M * (nor->bank_curr + 1)) - addr;
927 if (len < remain_len)
930 read_len = remain_len;
933 ret = nor->read(nor, addr, read_len, buf);
935 /* We shouldn't see 0-length reads */
950 #ifdef CONFIG_SPI_FLASH_BAR
951 ret = clean_bar(nor);
956 #ifdef CONFIG_SPI_FLASH_SST
958 * sst26 flash series has its own block protection implementation:
959 * 4x - 8 KByte blocks - read & write protection bits - upper addresses
960 * 1x - 32 KByte blocks - write protection bits
961 * rest - 64 KByte blocks - write protection bits
962 * 1x - 32 KByte blocks - write protection bits
963 * 4x - 8 KByte blocks - read & write protection bits - lower addresses
965 * We'll support only per 64k lock/unlock so lower and upper 64 KByte region
966 * will be treated as single block.
968 #define SST26_BPR_8K_NUM 4
969 #define SST26_MAX_BPR_REG_LEN (18 + 1)
970 #define SST26_BOUND_REG_SIZE ((32 + SST26_BPR_8K_NUM * 8) * SZ_1K)
978 static bool sst26_process_bpr(u32 bpr_size, u8 *cmd, u32 bit, enum lock_ctl ctl)
982 cmd[bpr_size - (bit / 8) - 1] |= BIT(bit % 8);
984 case SST26_CTL_UNLOCK:
985 cmd[bpr_size - (bit / 8) - 1] &= ~BIT(bit % 8);
987 case SST26_CTL_CHECK:
988 return !!(cmd[bpr_size - (bit / 8) - 1] & BIT(bit % 8));
995 * Lock, unlock or check lock status of the flash region of the flash (depending
996 * on the lock_ctl value)
998 static int sst26_lock_ctl(struct spi_nor *nor, loff_t ofs, uint64_t len, enum lock_ctl ctl)
1000 struct mtd_info *mtd = &nor->mtd;
1001 u32 i, bpr_ptr, rptr_64k, lptr_64k, bpr_size;
1002 bool lower_64k = false, upper_64k = false;
1003 u8 bpr_buff[SST26_MAX_BPR_REG_LEN] = {};
1006 /* Check length and offset for 64k alignment */
1007 if ((ofs & (SZ_64K - 1)) || (len & (SZ_64K - 1))) {
1008 dev_err(nor->dev, "length or offset is not 64KiB allighned\n");
1012 if (ofs + len > mtd->size) {
1013 dev_err(nor->dev, "range is more than device size: %#llx + %#llx > %#llx\n",
1014 ofs, len, mtd->size);
1018 /* SST26 family has only 16 Mbit, 32 Mbit and 64 Mbit IC */
1019 if (mtd->size != SZ_2M &&
1020 mtd->size != SZ_4M &&
1024 bpr_size = 2 + (mtd->size / SZ_64K / 8);
1026 ret = nor->read_reg(nor, SPINOR_OP_READ_BPR, bpr_buff, bpr_size);
1028 dev_err(nor->dev, "fail to read block-protection register\n");
1032 rptr_64k = min_t(u32, ofs + len, mtd->size - SST26_BOUND_REG_SIZE);
1033 lptr_64k = max_t(u32, ofs, SST26_BOUND_REG_SIZE);
1035 upper_64k = ((ofs + len) > (mtd->size - SST26_BOUND_REG_SIZE));
1036 lower_64k = (ofs < SST26_BOUND_REG_SIZE);
1038 /* Lower bits in block-protection register are about 64k region */
1039 bpr_ptr = lptr_64k / SZ_64K - 1;
1041 /* Process 64K blocks region */
1042 while (lptr_64k < rptr_64k) {
1043 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1050 /* 32K and 8K region bits in BPR are after 64k region bits */
1051 bpr_ptr = (mtd->size - 2 * SST26_BOUND_REG_SIZE) / SZ_64K;
1053 /* Process lower 32K block region */
1055 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1060 /* Process upper 32K block region */
1062 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1067 /* Process lower 8K block regions */
1068 for (i = 0; i < SST26_BPR_8K_NUM; i++) {
1070 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1073 /* In 8K area BPR has both read and write protection bits */
1077 /* Process upper 8K block regions */
1078 for (i = 0; i < SST26_BPR_8K_NUM; i++) {
1080 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1083 /* In 8K area BPR has both read and write protection bits */
1087 /* If we check region status we don't need to write BPR back */
1088 if (ctl == SST26_CTL_CHECK)
1091 ret = nor->write_reg(nor, SPINOR_OP_WRITE_BPR, bpr_buff, bpr_size);
1093 dev_err(nor->dev, "fail to write block-protection register\n");
1100 static int sst26_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1102 return sst26_lock_ctl(nor, ofs, len, SST26_CTL_UNLOCK);
1105 static int sst26_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1107 return sst26_lock_ctl(nor, ofs, len, SST26_CTL_LOCK);
1111 * Returns EACCES (positive value) if region is locked, 0 if region is unlocked,
1112 * and negative on errors.
1114 static int sst26_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1117 * is_locked function is used for check before reading or erasing flash
1118 * region, so offset and length might be not 64k allighned, so adjust
1119 * them to be 64k allighned as sst26_lock_ctl works only with 64k
1120 * allighned regions.
1122 ofs -= ofs & (SZ_64K - 1);
1123 len = len & (SZ_64K - 1) ? (len & ~(SZ_64K - 1)) + SZ_64K : len;
1125 return sst26_lock_ctl(nor, ofs, len, SST26_CTL_CHECK);
1128 static int sst_write_byteprogram(struct spi_nor *nor, loff_t to, size_t len,
1129 size_t *retlen, const u_char *buf)
1134 for (actual = 0; actual < len; actual++) {
1135 nor->program_opcode = SPINOR_OP_BP;
1138 /* write one byte. */
1139 ret = nor->write(nor, to, 1, buf + actual);
1142 ret = spi_nor_wait_till_ready(nor);
1153 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1154 size_t *retlen, const u_char *buf)
1156 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1157 struct spi_slave *spi = nor->spi;
1161 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1162 if (spi->mode & SPI_TX_BYTE)
1163 return sst_write_byteprogram(nor, to, len, retlen, buf);
1167 nor->sst_write_second = false;
1170 /* Start write from odd address. */
1172 nor->program_opcode = SPINOR_OP_BP;
1174 /* write one byte. */
1175 ret = nor->write(nor, to, 1, buf);
1178 ret = spi_nor_wait_till_ready(nor);
1184 /* Write out most of the data here. */
1185 for (; actual < len - 1; actual += 2) {
1186 nor->program_opcode = SPINOR_OP_AAI_WP;
1188 /* write two bytes. */
1189 ret = nor->write(nor, to, 2, buf + actual);
1192 ret = spi_nor_wait_till_ready(nor);
1196 nor->sst_write_second = true;
1198 nor->sst_write_second = false;
1201 ret = spi_nor_wait_till_ready(nor);
1205 /* Write out trailing byte if it exists. */
1206 if (actual != len) {
1209 nor->program_opcode = SPINOR_OP_BP;
1210 ret = nor->write(nor, to, 1, buf + actual);
1213 ret = spi_nor_wait_till_ready(nor);
1225 * Write an address range to the nor chip. Data must be written in
1226 * FLASH_PAGESIZE chunks. The address range may be any size provided
1227 * it is within the physical boundaries.
1229 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1230 size_t *retlen, const u_char *buf)
1232 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1233 size_t page_offset, page_remain, i;
1236 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1241 for (i = 0; i < len; ) {
1243 loff_t addr = to + i;
1246 * If page_size is a power of two, the offset can be quickly
1247 * calculated with an AND operation. On the other cases we
1248 * need to do a modulus operation (more expensive).
1250 if (is_power_of_2(nor->page_size)) {
1251 page_offset = addr & (nor->page_size - 1);
1255 page_offset = do_div(aux, nor->page_size);
1257 /* the size of data remaining on the first page */
1258 page_remain = min_t(size_t,
1259 nor->page_size - page_offset, len - i);
1261 #ifdef CONFIG_SPI_FLASH_BAR
1262 ret = write_bar(nor, addr);
1267 ret = nor->write(nor, addr, page_remain, buf + i);
1272 ret = spi_nor_wait_till_ready(nor);
1280 #ifdef CONFIG_SPI_FLASH_BAR
1281 ret = clean_bar(nor);
1286 #ifdef CONFIG_SPI_FLASH_MACRONIX
1288 * macronix_quad_enable() - set QE bit in Status Register.
1289 * @nor: pointer to a 'struct spi_nor'
1291 * Set the Quad Enable (QE) bit in the Status Register.
1293 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1295 * Return: 0 on success, -errno otherwise.
1297 static int macronix_quad_enable(struct spi_nor *nor)
1304 if (val & SR_QUAD_EN_MX)
1309 write_sr(nor, val | SR_QUAD_EN_MX);
1311 ret = spi_nor_wait_till_ready(nor);
1316 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1317 dev_err(nor->dev, "Macronix Quad bit not set\n");
1325 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1327 * Write status Register and configuration register with 2 bytes
1328 * The first byte will be written to the status register, while the
1329 * second byte will be written to the configuration register.
1330 * Return negative if error occurred.
1332 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1338 ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1341 "error while writing configuration register\n");
1345 ret = spi_nor_wait_till_ready(nor);
1348 "timeout while writing configuration register\n");
1356 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1357 * @nor: pointer to a 'struct spi_nor'
1359 * Set the Quad Enable (QE) bit in the Configuration Register.
1360 * This function should be used with QSPI memories supporting the Read
1361 * Configuration Register (35h) instruction.
1363 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1366 * Return: 0 on success, -errno otherwise.
1368 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1373 /* Check current Quad Enable bit value. */
1377 "error while reading configuration register\n");
1381 if (ret & CR_QUAD_EN_SPAN)
1384 sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1386 /* Keep the current value of the Status Register. */
1389 dev_dbg(nor->dev, "error while reading status register\n");
1394 ret = write_sr_cr(nor, sr_cr);
1398 /* Read back and check it. */
1400 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1401 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1408 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1410 * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1411 * @nor: pointer to a 'struct spi_nor'
1413 * Set the Quad Enable (QE) bit in the Configuration Register.
1414 * This function should be used with QSPI memories not supporting the Read
1415 * Configuration Register (35h) instruction.
1417 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1420 * Return: 0 on success, -errno otherwise.
1422 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
1427 /* Keep the current value of the Status Register. */
1430 dev_dbg(nor->dev, "error while reading status register\n");
1434 sr_cr[1] = CR_QUAD_EN_SPAN;
1436 return write_sr_cr(nor, sr_cr);
1439 #endif /* CONFIG_SPI_FLASH_SFDP_SUPPORT */
1440 #endif /* CONFIG_SPI_FLASH_SPANSION */
1442 struct spi_nor_read_command {
1446 enum spi_nor_protocol proto;
1449 struct spi_nor_pp_command {
1451 enum spi_nor_protocol proto;
1454 enum spi_nor_read_command_index {
1457 SNOR_CMD_READ_1_1_1_DTR,
1460 SNOR_CMD_READ_1_1_2,
1461 SNOR_CMD_READ_1_2_2,
1462 SNOR_CMD_READ_2_2_2,
1463 SNOR_CMD_READ_1_2_2_DTR,
1466 SNOR_CMD_READ_1_1_4,
1467 SNOR_CMD_READ_1_4_4,
1468 SNOR_CMD_READ_4_4_4,
1469 SNOR_CMD_READ_1_4_4_DTR,
1472 SNOR_CMD_READ_1_1_8,
1473 SNOR_CMD_READ_1_8_8,
1474 SNOR_CMD_READ_8_8_8,
1475 SNOR_CMD_READ_1_8_8_DTR,
1480 enum spi_nor_pp_command_index {
1496 struct spi_nor_flash_parameter {
1500 struct spi_nor_hwcaps hwcaps;
1501 struct spi_nor_read_command reads[SNOR_CMD_READ_MAX];
1502 struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX];
1504 int (*quad_enable)(struct spi_nor *nor);
1508 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1512 enum spi_nor_protocol proto)
1514 read->num_mode_clocks = num_mode_clocks;
1515 read->num_wait_states = num_wait_states;
1516 read->opcode = opcode;
1517 read->proto = proto;
1521 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
1523 enum spi_nor_protocol proto)
1525 pp->opcode = opcode;
1529 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1531 * Serial Flash Discoverable Parameters (SFDP) parsing.
1535 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
1536 * @nor: pointer to a 'struct spi_nor'
1537 * @addr: offset in the SFDP area to start reading data from
1538 * @len: number of bytes to read
1539 * @buf: buffer where the SFDP data are copied into (dma-safe memory)
1541 * Whatever the actual numbers of bytes for address and dummy cycles are
1542 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
1543 * followed by a 3-byte address and 8 dummy clock cycles.
1545 * Return: 0 on success, -errno otherwise.
1547 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
1548 size_t len, void *buf)
1550 u8 addr_width, read_opcode, read_dummy;
1553 read_opcode = nor->read_opcode;
1554 addr_width = nor->addr_width;
1555 read_dummy = nor->read_dummy;
1557 nor->read_opcode = SPINOR_OP_RDSFDP;
1558 nor->addr_width = 3;
1559 nor->read_dummy = 8;
1562 ret = nor->read(nor, addr, len, (u8 *)buf);
1563 if (!ret || ret > len) {
1577 nor->read_opcode = read_opcode;
1578 nor->addr_width = addr_width;
1579 nor->read_dummy = read_dummy;
1584 struct sfdp_parameter_header {
1588 u8 length; /* in double words */
1589 u8 parameter_table_pointer[3]; /* byte address */
1593 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
1594 #define SFDP_PARAM_HEADER_PTP(p) \
1595 (((p)->parameter_table_pointer[2] << 16) | \
1596 ((p)->parameter_table_pointer[1] << 8) | \
1597 ((p)->parameter_table_pointer[0] << 0))
1599 #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
1600 #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
1601 #define SFDP_SST_ID 0x01bf /* Manufacturer specific Table */
1603 #define SFDP_SIGNATURE 0x50444653U
1604 #define SFDP_JESD216_MAJOR 1
1605 #define SFDP_JESD216_MINOR 0
1606 #define SFDP_JESD216A_MINOR 5
1607 #define SFDP_JESD216B_MINOR 6
1609 struct sfdp_header {
1610 u32 signature; /* Ox50444653U <=> "SFDP" */
1613 u8 nph; /* 0-base number of parameter headers */
1616 /* Basic Flash Parameter Table. */
1617 struct sfdp_parameter_header bfpt_header;
1620 /* Basic Flash Parameter Table */
1623 * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
1624 * They are indexed from 1 but C arrays are indexed from 0.
1626 #define BFPT_DWORD(i) ((i) - 1)
1627 #define BFPT_DWORD_MAX 16
1629 /* The first version of JESB216 defined only 9 DWORDs. */
1630 #define BFPT_DWORD_MAX_JESD216 9
1633 #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
1634 #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
1635 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
1636 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
1637 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
1638 #define BFPT_DWORD1_DTR BIT(19)
1639 #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
1640 #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
1641 #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
1644 #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
1645 #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
1648 #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
1649 #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
1654 * (from JESD216 rev B)
1655 * Quad Enable Requirements (QER):
1656 * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
1657 * reads based on instruction. DQ3/HOLD# functions are hold during
1658 * instruction phase.
1659 * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
1660 * two data bytes where bit 1 of the second byte is one.
1662 * Writing only one byte to the status register has the side-effect of
1663 * clearing status register 2, including the QE bit. The 100b code is
1664 * used if writing one byte to the status register does not modify
1665 * status register 2.
1666 * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
1667 * one data byte where bit 6 is one.
1669 * - 011b: QE is bit 7 of status register 2. It is set via Write status
1670 * register 2 instruction 3Eh with one data byte where bit 7 is one.
1672 * The status register 2 is read using instruction 3Fh.
1673 * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
1674 * two data bytes where bit 1 of the second byte is one.
1676 * In contrast to the 001b code, writing one byte to the status
1677 * register does not modify status register 2.
1678 * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
1679 * Read Status instruction 05h. Status register2 is read using
1680 * instruction 35h. QE is set via Writ Status instruction 01h with
1681 * two data bytes where bit 1 of the second byte is one.
1684 #define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
1685 #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
1686 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
1687 #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
1688 #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
1689 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
1690 #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
1693 u32 dwords[BFPT_DWORD_MAX];
1696 /* Fast Read settings. */
1699 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
1701 enum spi_nor_protocol proto)
1703 read->num_mode_clocks = (half >> 5) & 0x07;
1704 read->num_wait_states = (half >> 0) & 0x1f;
1705 read->opcode = (half >> 8) & 0xff;
1706 read->proto = proto;
1709 struct sfdp_bfpt_read {
1710 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
1714 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
1715 * whether the Fast Read x-y-z command is supported.
1717 u32 supported_dword;
1721 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
1722 * encodes the op code, the number of mode clocks and the number of wait
1723 * states to be used by Fast Read x-y-z command.
1728 /* The SPI protocol for this Fast Read x-y-z command. */
1729 enum spi_nor_protocol proto;
1732 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
1733 /* Fast Read 1-1-2 */
1735 SNOR_HWCAPS_READ_1_1_2,
1736 BFPT_DWORD(1), BIT(16), /* Supported bit */
1737 BFPT_DWORD(4), 0, /* Settings */
1741 /* Fast Read 1-2-2 */
1743 SNOR_HWCAPS_READ_1_2_2,
1744 BFPT_DWORD(1), BIT(20), /* Supported bit */
1745 BFPT_DWORD(4), 16, /* Settings */
1749 /* Fast Read 2-2-2 */
1751 SNOR_HWCAPS_READ_2_2_2,
1752 BFPT_DWORD(5), BIT(0), /* Supported bit */
1753 BFPT_DWORD(6), 16, /* Settings */
1757 /* Fast Read 1-1-4 */
1759 SNOR_HWCAPS_READ_1_1_4,
1760 BFPT_DWORD(1), BIT(22), /* Supported bit */
1761 BFPT_DWORD(3), 16, /* Settings */
1765 /* Fast Read 1-4-4 */
1767 SNOR_HWCAPS_READ_1_4_4,
1768 BFPT_DWORD(1), BIT(21), /* Supported bit */
1769 BFPT_DWORD(3), 0, /* Settings */
1773 /* Fast Read 4-4-4 */
1775 SNOR_HWCAPS_READ_4_4_4,
1776 BFPT_DWORD(5), BIT(4), /* Supported bit */
1777 BFPT_DWORD(7), 16, /* Settings */
1782 struct sfdp_bfpt_erase {
1784 * The half-word at offset <shift> in DWORD <dwoard> encodes the
1785 * op code and erase sector size to be used by Sector Erase commands.
1791 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
1792 /* Erase Type 1 in DWORD8 bits[15:0] */
1795 /* Erase Type 2 in DWORD8 bits[31:16] */
1796 {BFPT_DWORD(8), 16},
1798 /* Erase Type 3 in DWORD9 bits[15:0] */
1801 /* Erase Type 4 in DWORD9 bits[31:16] */
1802 {BFPT_DWORD(9), 16},
1805 static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
1808 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
1809 * @nor: pointer to a 'struct spi_nor'
1810 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
1811 * the Basic Flash Parameter Table length and version
1812 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
1815 * The Basic Flash Parameter Table is the main and only mandatory table as
1816 * defined by the SFDP (JESD216) specification.
1817 * It provides us with the total size (memory density) of the data array and
1818 * the number of address bytes for Fast Read, Page Program and Sector Erase
1820 * For Fast READ commands, it also gives the number of mode clock cycles and
1821 * wait states (regrouped in the number of dummy clock cycles) for each
1822 * supported instruction op code.
1823 * For Page Program, the page size is now available since JESD216 rev A, however
1824 * the supported instruction op codes are still not provided.
1825 * For Sector Erase commands, this table stores the supported instruction op
1826 * codes and the associated sector sizes.
1827 * Finally, the Quad Enable Requirements (QER) are also available since JESD216
1828 * rev A. The QER bits encode the manufacturer dependent procedure to be
1829 * executed to set the Quad Enable (QE) bit in some internal register of the
1830 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
1831 * sending any Quad SPI command to the memory. Actually, setting the QE bit
1832 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
1833 * and IO3 hence enabling 4 (Quad) I/O lines.
1835 * Return: 0 on success, -errno otherwise.
1837 static int spi_nor_parse_bfpt(struct spi_nor *nor,
1838 const struct sfdp_parameter_header *bfpt_header,
1839 struct spi_nor_flash_parameter *params)
1841 struct mtd_info *mtd = &nor->mtd;
1842 struct sfdp_bfpt bfpt;
1848 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
1849 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
1852 /* Read the Basic Flash Parameter Table. */
1853 len = min_t(size_t, sizeof(bfpt),
1854 bfpt_header->length * sizeof(u32));
1855 addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
1856 memset(&bfpt, 0, sizeof(bfpt));
1857 err = spi_nor_read_sfdp(nor, addr, len, &bfpt);
1861 /* Fix endianness of the BFPT DWORDs. */
1862 for (i = 0; i < BFPT_DWORD_MAX; i++)
1863 bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
1865 /* Number of address bytes. */
1866 switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
1867 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
1868 nor->addr_width = 3;
1871 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
1872 nor->addr_width = 4;
1879 /* Flash Memory Density (in bits). */
1880 params->size = bfpt.dwords[BFPT_DWORD(2)];
1881 if (params->size & BIT(31)) {
1882 params->size &= ~BIT(31);
1885 * Prevent overflows on params->size. Anyway, a NOR of 2^64
1886 * bits is unlikely to exist so this error probably means
1887 * the BFPT we are reading is corrupted/wrong.
1889 if (params->size > 63)
1892 params->size = 1ULL << params->size;
1896 params->size >>= 3; /* Convert to bytes. */
1898 /* Fast Read settings. */
1899 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
1900 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
1901 struct spi_nor_read_command *read;
1903 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
1904 params->hwcaps.mask &= ~rd->hwcaps;
1908 params->hwcaps.mask |= rd->hwcaps;
1909 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
1910 read = ¶ms->reads[cmd];
1911 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
1912 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
1915 /* Sector Erase settings. */
1916 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
1917 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
1921 half = bfpt.dwords[er->dword] >> er->shift;
1922 erasesize = half & 0xff;
1924 /* erasesize == 0 means this Erase Type is not supported. */
1928 erasesize = 1U << erasesize;
1929 opcode = (half >> 8) & 0xff;
1930 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
1931 if (erasesize == SZ_4K) {
1932 nor->erase_opcode = opcode;
1933 mtd->erasesize = erasesize;
1937 if (!mtd->erasesize || mtd->erasesize < erasesize) {
1938 nor->erase_opcode = opcode;
1939 mtd->erasesize = erasesize;
1943 /* Stop here if not JESD216 rev A or later. */
1944 if (bfpt_header->length < BFPT_DWORD_MAX)
1947 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
1948 params->page_size = bfpt.dwords[BFPT_DWORD(11)];
1949 params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
1950 params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
1951 params->page_size = 1U << params->page_size;
1953 /* Quad Enable Requirements. */
1954 switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
1955 case BFPT_DWORD15_QER_NONE:
1956 params->quad_enable = NULL;
1958 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1959 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
1960 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
1961 params->quad_enable = spansion_no_read_cr_quad_enable;
1964 #ifdef CONFIG_SPI_FLASH_MACRONIX
1965 case BFPT_DWORD15_QER_SR1_BIT6:
1966 params->quad_enable = macronix_quad_enable;
1969 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1970 case BFPT_DWORD15_QER_SR2_BIT1:
1971 params->quad_enable = spansion_read_cr_quad_enable;
1982 * spi_nor_parse_microchip_sfdp() - parse the Microchip manufacturer specific
1984 * @nor: pointer to a 'struct spi_nor'.
1985 * @param_header: pointer to the SFDP parameter header.
1987 * Return: 0 on success, -errno otherwise.
1990 spi_nor_parse_microchip_sfdp(struct spi_nor *nor,
1991 const struct sfdp_parameter_header *param_header)
1997 size = param_header->length * sizeof(u32);
1998 addr = SFDP_PARAM_HEADER_PTP(param_header);
2000 nor->manufacturer_sfdp = devm_kmalloc(nor->dev, size, GFP_KERNEL);
2001 if (!nor->manufacturer_sfdp)
2004 ret = spi_nor_read_sfdp(nor, addr, size, nor->manufacturer_sfdp);
2010 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
2011 * @nor: pointer to a 'struct spi_nor'
2012 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2015 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
2016 * specification. This is a standard which tends to supported by almost all
2017 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
2018 * runtime the main parameters needed to perform basic SPI flash operations such
2019 * as Fast Read, Page Program or Sector Erase commands.
2021 * Return: 0 on success, -errno otherwise.
2023 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2024 struct spi_nor_flash_parameter *params)
2026 const struct sfdp_parameter_header *param_header, *bfpt_header;
2027 struct sfdp_parameter_header *param_headers = NULL;
2028 struct sfdp_header header;
2032 /* Get the SFDP header. */
2033 err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header);
2037 /* Check the SFDP header version. */
2038 if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
2039 header.major != SFDP_JESD216_MAJOR)
2043 * Verify that the first and only mandatory parameter header is a
2044 * Basic Flash Parameter Table header as specified in JESD216.
2046 bfpt_header = &header.bfpt_header;
2047 if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
2048 bfpt_header->major != SFDP_JESD216_MAJOR)
2052 * Allocate memory then read all parameter headers with a single
2053 * Read SFDP command. These parameter headers will actually be parsed
2054 * twice: a first time to get the latest revision of the basic flash
2055 * parameter table, then a second time to handle the supported optional
2057 * Hence we read the parameter headers once for all to reduce the
2058 * processing time. Also we use kmalloc() instead of devm_kmalloc()
2059 * because we don't need to keep these parameter headers: the allocated
2060 * memory is always released with kfree() before exiting this function.
2063 psize = header.nph * sizeof(*param_headers);
2065 param_headers = kmalloc(psize, GFP_KERNEL);
2069 err = spi_nor_read_sfdp(nor, sizeof(header),
2070 psize, param_headers);
2073 "failed to read SFDP parameter headers\n");
2079 * Check other parameter headers to get the latest revision of
2080 * the basic flash parameter table.
2082 for (i = 0; i < header.nph; i++) {
2083 param_header = ¶m_headers[i];
2085 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
2086 param_header->major == SFDP_JESD216_MAJOR &&
2087 (param_header->minor > bfpt_header->minor ||
2088 (param_header->minor == bfpt_header->minor &&
2089 param_header->length > bfpt_header->length)))
2090 bfpt_header = param_header;
2093 err = spi_nor_parse_bfpt(nor, bfpt_header, params);
2097 /* Parse other parameter headers. */
2098 for (i = 0; i < header.nph; i++) {
2099 param_header = ¶m_headers[i];
2101 switch (SFDP_PARAM_HEADER_ID(param_header)) {
2102 case SFDP_SECTOR_MAP_ID:
2104 "non-uniform erase sector maps are not supported yet.\n");
2108 err = spi_nor_parse_microchip_sfdp(nor, param_header);
2117 "Failed to parse optional parameter table: %04x\n",
2118 SFDP_PARAM_HEADER_ID(param_header));
2120 * Let's not drop all information we extracted so far
2121 * if optional table parsers fail. In case of failing,
2122 * each optional parser is responsible to roll back to
2123 * the previously known spi_nor data.
2130 kfree(param_headers);
2134 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2135 struct spi_nor_flash_parameter *params)
2139 #endif /* SPI_FLASH_SFDP_SUPPORT */
2141 static int spi_nor_init_params(struct spi_nor *nor,
2142 const struct flash_info *info,
2143 struct spi_nor_flash_parameter *params)
2145 /* Set legacy flash parameters as default. */
2146 memset(params, 0, sizeof(*params));
2148 /* Set SPI NOR sizes. */
2149 params->size = info->sector_size * info->n_sectors;
2150 params->page_size = info->page_size;
2152 /* (Fast) Read settings. */
2153 params->hwcaps.mask |= SNOR_HWCAPS_READ;
2154 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
2155 0, 0, SPINOR_OP_READ,
2158 if (!(info->flags & SPI_NOR_NO_FR)) {
2159 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2160 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
2161 0, 8, SPINOR_OP_READ_FAST,
2165 if (info->flags & SPI_NOR_DUAL_READ) {
2166 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2167 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2],
2168 0, 8, SPINOR_OP_READ_1_1_2,
2172 if (info->flags & SPI_NOR_QUAD_READ) {
2173 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2174 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
2175 0, 8, SPINOR_OP_READ_1_1_4,
2179 if (info->flags & SPI_NOR_OCTAL_READ) {
2180 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
2181 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_8],
2182 0, 8, SPINOR_OP_READ_1_1_8,
2186 /* Page Program settings. */
2187 params->hwcaps.mask |= SNOR_HWCAPS_PP;
2188 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
2189 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2191 if (info->flags & SPI_NOR_QUAD_READ) {
2192 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
2193 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_1_1_4],
2194 SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
2197 /* Select the procedure to set the Quad Enable bit. */
2198 if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
2199 SNOR_HWCAPS_PP_QUAD)) {
2200 switch (JEDEC_MFR(info)) {
2201 #ifdef CONFIG_SPI_FLASH_MACRONIX
2202 case SNOR_MFR_MACRONIX:
2203 params->quad_enable = macronix_quad_enable;
2207 case SNOR_MFR_MICRON:
2211 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2212 /* Kept only for backward compatibility purpose. */
2213 params->quad_enable = spansion_read_cr_quad_enable;
2219 /* Override the parameters with data read from SFDP tables. */
2220 nor->addr_width = 0;
2221 nor->mtd.erasesize = 0;
2222 if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
2223 !(info->flags & SPI_NOR_SKIP_SFDP)) {
2224 struct spi_nor_flash_parameter sfdp_params;
2226 memcpy(&sfdp_params, params, sizeof(sfdp_params));
2227 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
2228 nor->addr_width = 0;
2229 nor->mtd.erasesize = 0;
2231 memcpy(params, &sfdp_params, sizeof(*params));
2238 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2242 for (i = 0; i < size; i++)
2243 if (table[i][0] == (int)hwcaps)
2249 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2251 static const int hwcaps_read2cmd[][2] = {
2252 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
2253 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
2254 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
2255 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
2256 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
2257 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
2258 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
2259 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
2260 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
2261 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
2262 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
2263 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
2264 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
2265 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
2266 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
2269 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2270 ARRAY_SIZE(hwcaps_read2cmd));
2273 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2275 static const int hwcaps_pp2cmd[][2] = {
2276 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
2277 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
2278 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
2279 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
2280 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
2281 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
2282 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
2285 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2286 ARRAY_SIZE(hwcaps_pp2cmd));
2289 static int spi_nor_select_read(struct spi_nor *nor,
2290 const struct spi_nor_flash_parameter *params,
2293 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2294 const struct spi_nor_read_command *read;
2299 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2303 read = ¶ms->reads[cmd];
2304 nor->read_opcode = read->opcode;
2305 nor->read_proto = read->proto;
2308 * In the spi-nor framework, we don't need to make the difference
2309 * between mode clock cycles and wait state clock cycles.
2310 * Indeed, the value of the mode clock cycles is used by a QSPI
2311 * flash memory to know whether it should enter or leave its 0-4-4
2312 * (Continuous Read / XIP) mode.
2313 * eXecution In Place is out of the scope of the mtd sub-system.
2314 * Hence we choose to merge both mode and wait state clock cycles
2315 * into the so called dummy clock cycles.
2317 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2321 static int spi_nor_select_pp(struct spi_nor *nor,
2322 const struct spi_nor_flash_parameter *params,
2325 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2326 const struct spi_nor_pp_command *pp;
2331 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2335 pp = ¶ms->page_programs[cmd];
2336 nor->program_opcode = pp->opcode;
2337 nor->write_proto = pp->proto;
2341 static int spi_nor_select_erase(struct spi_nor *nor,
2342 const struct flash_info *info)
2344 struct mtd_info *mtd = &nor->mtd;
2346 /* Do nothing if already configured from SFDP. */
2350 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
2351 /* prefer "small sector" erase if possible */
2352 if (info->flags & SECT_4K) {
2353 nor->erase_opcode = SPINOR_OP_BE_4K;
2354 mtd->erasesize = 4096;
2355 } else if (info->flags & SECT_4K_PMC) {
2356 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
2357 mtd->erasesize = 4096;
2361 nor->erase_opcode = SPINOR_OP_SE;
2362 mtd->erasesize = info->sector_size;
2367 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
2368 const struct spi_nor_flash_parameter *params,
2369 const struct spi_nor_hwcaps *hwcaps)
2371 u32 ignored_mask, shared_mask;
2372 bool enable_quad_io;
2376 * Keep only the hardware capabilities supported by both the SPI
2377 * controller and the SPI flash memory.
2379 shared_mask = hwcaps->mask & params->hwcaps.mask;
2381 /* SPI n-n-n protocols are not supported yet. */
2382 ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
2383 SNOR_HWCAPS_READ_4_4_4 |
2384 SNOR_HWCAPS_READ_8_8_8 |
2385 SNOR_HWCAPS_PP_4_4_4 |
2386 SNOR_HWCAPS_PP_8_8_8);
2387 if (shared_mask & ignored_mask) {
2389 "SPI n-n-n protocols are not supported yet.\n");
2390 shared_mask &= ~ignored_mask;
2393 /* Select the (Fast) Read command. */
2394 err = spi_nor_select_read(nor, params, shared_mask);
2397 "can't select read settings supported by both the SPI controller and memory.\n");
2401 /* Select the Page Program command. */
2402 err = spi_nor_select_pp(nor, params, shared_mask);
2405 "can't select write settings supported by both the SPI controller and memory.\n");
2409 /* Select the Sector Erase command. */
2410 err = spi_nor_select_erase(nor, info);
2413 "can't select erase settings supported by both the SPI controller and memory.\n");
2417 /* Enable Quad I/O if needed. */
2418 enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2419 spi_nor_get_protocol_width(nor->write_proto) == 4);
2420 if (enable_quad_io && params->quad_enable)
2421 nor->quad_enable = params->quad_enable;
2423 nor->quad_enable = NULL;
2428 static int spi_nor_init(struct spi_nor *nor)
2433 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
2434 * with the software protection bits set
2436 if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
2437 JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
2438 JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
2439 nor->info->flags & SPI_NOR_HAS_LOCK) {
2442 spi_nor_wait_till_ready(nor);
2445 if (nor->quad_enable) {
2446 err = nor->quad_enable(nor);
2448 dev_dbg(nor->dev, "quad mode not supported\n");
2453 if (nor->addr_width == 4 &&
2454 (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
2455 !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
2457 * If the RESET# pin isn't hooked up properly, or the system
2458 * otherwise doesn't perform a reset command in the boot
2459 * sequence, it's impossible to 100% protect against unexpected
2460 * reboots (e.g., crashes). Warn the user (or hopefully, system
2461 * designer) that this is bad.
2463 if (nor->flags & SNOR_F_BROKEN_RESET)
2464 printf("enabling reset hack; may not recover from unexpected reboots\n");
2465 set_4byte(nor, nor->info, 1);
2471 int spi_nor_scan(struct spi_nor *nor)
2473 struct spi_nor_flash_parameter params;
2474 const struct flash_info *info = NULL;
2475 struct mtd_info *mtd = &nor->mtd;
2476 struct spi_nor_hwcaps hwcaps = {
2477 .mask = SNOR_HWCAPS_READ |
2478 SNOR_HWCAPS_READ_FAST |
2481 struct spi_slave *spi = nor->spi;
2484 /* Reset SPI protocol for all commands. */
2485 nor->reg_proto = SNOR_PROTO_1_1_1;
2486 nor->read_proto = SNOR_PROTO_1_1_1;
2487 nor->write_proto = SNOR_PROTO_1_1_1;
2488 nor->read = spi_nor_read_data;
2489 nor->write = spi_nor_write_data;
2490 nor->read_reg = spi_nor_read_reg;
2491 nor->write_reg = spi_nor_write_reg;
2493 if (spi->mode & SPI_RX_OCTAL) {
2494 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
2496 if (spi->mode & SPI_TX_OCTAL)
2497 hwcaps.mask |= (SNOR_HWCAPS_READ_1_8_8 |
2498 SNOR_HWCAPS_PP_1_1_8 |
2499 SNOR_HWCAPS_PP_1_8_8);
2500 } else if (spi->mode & SPI_RX_QUAD) {
2501 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2503 if (spi->mode & SPI_TX_QUAD)
2504 hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 |
2505 SNOR_HWCAPS_PP_1_1_4 |
2506 SNOR_HWCAPS_PP_1_4_4);
2507 } else if (spi->mode & SPI_RX_DUAL) {
2508 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2510 if (spi->mode & SPI_TX_DUAL)
2511 hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2;
2514 info = spi_nor_read_id(nor);
2515 if (IS_ERR_OR_NULL(info))
2517 /* Parse the Serial Flash Discoverable Parameters table. */
2518 ret = spi_nor_init_params(nor, info, ¶ms);
2523 mtd->name = info->name;
2525 mtd->type = MTD_NORFLASH;
2527 mtd->flags = MTD_CAP_NORFLASH;
2528 mtd->size = params.size;
2529 mtd->_erase = spi_nor_erase;
2530 mtd->_read = spi_nor_read;
2532 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
2533 /* NOR protection support for STmicro/Micron chips and similar */
2534 if (JEDEC_MFR(info) == SNOR_MFR_ST ||
2535 JEDEC_MFR(info) == SNOR_MFR_MICRON ||
2536 JEDEC_MFR(info) == SNOR_MFR_SST ||
2537 info->flags & SPI_NOR_HAS_LOCK) {
2538 nor->flash_lock = stm_lock;
2539 nor->flash_unlock = stm_unlock;
2540 nor->flash_is_locked = stm_is_locked;
2544 #ifdef CONFIG_SPI_FLASH_SST
2546 * sst26 series block protection implementation differs from other
2549 if (info->flags & SPI_NOR_HAS_SST26LOCK) {
2550 nor->flash_lock = sst26_lock;
2551 nor->flash_unlock = sst26_unlock;
2552 nor->flash_is_locked = sst26_is_locked;
2555 /* sst nor chips use AAI word program */
2556 if (info->flags & SST_WRITE)
2557 mtd->_write = sst_write;
2560 mtd->_write = spi_nor_write;
2562 if (info->flags & USE_FSR)
2563 nor->flags |= SNOR_F_USE_FSR;
2564 if (info->flags & SPI_NOR_HAS_TB)
2565 nor->flags |= SNOR_F_HAS_SR_TB;
2566 if (info->flags & NO_CHIP_ERASE)
2567 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2568 if (info->flags & USE_CLSR)
2569 nor->flags |= SNOR_F_USE_CLSR;
2571 if (info->flags & SPI_NOR_NO_ERASE)
2572 mtd->flags |= MTD_NO_ERASE;
2574 nor->page_size = params.page_size;
2575 mtd->writebufsize = nor->page_size;
2577 /* Some devices cannot do fast-read, no matter what DT tells us */
2578 if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW))
2579 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2582 * Configure the SPI memory:
2583 * - select op codes for (Fast) Read, Page Program and Sector Erase.
2584 * - set the number of dummy cycles (mode cycles + wait states).
2585 * - set the SPI protocols for register and memory accesses.
2586 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
2588 ret = spi_nor_setup(nor, info, ¶ms, &hwcaps);
2592 if (nor->addr_width) {
2593 /* already configured from SFDP */
2594 } else if (info->addr_width) {
2595 nor->addr_width = info->addr_width;
2596 } else if (mtd->size > SZ_16M) {
2597 #ifndef CONFIG_SPI_FLASH_BAR
2598 /* enable 4-byte addressing if the device exceeds 16MiB */
2599 nor->addr_width = 4;
2600 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
2601 info->flags & SPI_NOR_4B_OPCODES)
2602 spi_nor_set_4byte_opcodes(nor, info);
2604 /* Configure the BAR - discover bank cmds and read current bank */
2605 nor->addr_width = 3;
2606 ret = read_bar(nor, info);
2611 nor->addr_width = 3;
2614 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
2615 dev_dbg(nor->dev, "address width is too large: %u\n",
2620 /* Send all the required SPI flash commands to initialize device */
2622 ret = spi_nor_init(nor);
2626 nor->name = mtd->name;
2627 nor->size = mtd->size;
2628 nor->erase_size = mtd->erasesize;
2629 nor->sector_size = mtd->erasesize;
2631 #ifndef CONFIG_SPL_BUILD
2632 printf("SF: Detected %s with page size ", nor->name);
2633 print_size(nor->page_size, ", erase size ");
2634 print_size(nor->erase_size, ", total ");
2635 print_size(nor->size, "");
2642 /* U-Boot specific functions, need to extend MTD to support these */
2643 int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
2645 int sr = read_sr(nor);
2650 return (sr >> 2) & 7;