1 // SPDX-License-Identifier: GPL-2.0
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.
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/mutex.h>
15 #include <linux/math64.h>
16 #include <linux/sizes.h>
17 #include <linux/slab.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/of_platform.h>
21 #include <linux/sched/task_stack.h>
22 #include <linux/spi/flash.h>
23 #include <linux/mtd/spi-nor.h>
27 /* Define max times to check status register before we give up. */
30 * For everything but full-chip erase; probably could be much smaller, but kept
31 * around for safety for now
33 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
36 * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
39 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
41 #define SPI_NOR_MAX_ADDR_WIDTH 4
44 * spi_nor_spimem_bounce() - check if a bounce buffer is needed for the data
46 * @nor: pointer to 'struct spi_nor'
47 * @op: pointer to 'struct spi_mem_op' template for transfer
49 * If we have to use the bounce buffer, the data field in @op will be updated.
51 * Return: true if the bounce buffer is needed, false if not
53 static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op)
55 /* op->data.buf.in occupies the same memory as op->data.buf.out */
56 if (object_is_on_stack(op->data.buf.in) ||
57 !virt_addr_valid(op->data.buf.in)) {
58 if (op->data.nbytes > nor->bouncebuf_size)
59 op->data.nbytes = nor->bouncebuf_size;
60 op->data.buf.in = nor->bouncebuf;
68 * spi_nor_spimem_exec_op() - execute a memory operation
69 * @nor: pointer to 'struct spi_nor'
70 * @op: pointer to 'struct spi_mem_op' template for transfer
72 * Return: 0 on success, -error otherwise.
74 static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op)
78 error = spi_mem_adjust_op_size(nor->spimem, op);
82 return spi_mem_exec_op(nor->spimem, op);
86 * spi_nor_spimem_read_data() - read data from flash's memory region via
88 * @nor: pointer to 'struct spi_nor'
89 * @from: offset to read from
90 * @len: number of bytes to read
91 * @buf: pointer to dst buffer
93 * Return: number of bytes read successfully, -errno otherwise
95 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from,
98 struct spi_mem_op op =
99 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
100 SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
101 SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
102 SPI_MEM_OP_DATA_IN(len, buf, 1));
107 /* get transfer protocols. */
108 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
109 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
110 op.dummy.buswidth = op.addr.buswidth;
111 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
113 /* convert the dummy cycles to the number of bytes */
114 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
116 usebouncebuf = spi_nor_spimem_bounce(nor, &op);
118 if (nor->dirmap.rdesc) {
119 nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val,
120 op.data.nbytes, op.data.buf.in);
122 error = spi_nor_spimem_exec_op(nor, &op);
125 nbytes = op.data.nbytes;
128 if (usebouncebuf && nbytes > 0)
129 memcpy(buf, op.data.buf.in, nbytes);
135 * spi_nor_read_data() - read data from flash memory
136 * @nor: pointer to 'struct spi_nor'
137 * @from: offset to read from
138 * @len: number of bytes to read
139 * @buf: pointer to dst buffer
141 * Return: number of bytes read successfully, -errno otherwise
143 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf)
146 return spi_nor_spimem_read_data(nor, from, len, buf);
148 return nor->controller_ops->read(nor, from, len, buf);
152 * spi_nor_spimem_write_data() - write data to flash memory via
154 * @nor: pointer to 'struct spi_nor'
155 * @to: offset to write to
156 * @len: number of bytes to write
157 * @buf: pointer to src buffer
159 * Return: number of bytes written successfully, -errno otherwise
161 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to,
162 size_t len, const u8 *buf)
164 struct spi_mem_op op =
165 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
166 SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
168 SPI_MEM_OP_DATA_OUT(len, buf, 1));
172 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
173 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
174 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
176 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
179 if (spi_nor_spimem_bounce(nor, &op))
180 memcpy(nor->bouncebuf, buf, op.data.nbytes);
182 if (nor->dirmap.wdesc) {
183 nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val,
184 op.data.nbytes, op.data.buf.out);
186 error = spi_nor_spimem_exec_op(nor, &op);
189 nbytes = op.data.nbytes;
196 * spi_nor_write_data() - write data to flash memory
197 * @nor: pointer to 'struct spi_nor'
198 * @to: offset to write to
199 * @len: number of bytes to write
200 * @buf: pointer to src buffer
202 * Return: number of bytes written successfully, -errno otherwise
204 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
208 return spi_nor_spimem_write_data(nor, to, len, buf);
210 return nor->controller_ops->write(nor, to, len, buf);
214 * spi_nor_write_enable() - Set write enable latch with Write Enable command.
215 * @nor: pointer to 'struct spi_nor'.
217 * Return: 0 on success, -errno otherwise.
219 int spi_nor_write_enable(struct spi_nor *nor)
224 struct spi_mem_op op =
225 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREN, 1),
230 ret = spi_mem_exec_op(nor->spimem, &op);
232 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WREN,
237 dev_dbg(nor->dev, "error %d on Write Enable\n", ret);
243 * spi_nor_write_disable() - Send Write Disable instruction to the chip.
244 * @nor: pointer to 'struct spi_nor'.
246 * Return: 0 on success, -errno otherwise.
248 int spi_nor_write_disable(struct spi_nor *nor)
253 struct spi_mem_op op =
254 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRDI, 1),
259 ret = spi_mem_exec_op(nor->spimem, &op);
261 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRDI,
266 dev_dbg(nor->dev, "error %d on Write Disable\n", ret);
272 * spi_nor_read_sr() - Read the Status Register.
273 * @nor: pointer to 'struct spi_nor'.
274 * @sr: pointer to a DMA-able buffer where the value of the
275 * Status Register will be written.
277 * Return: 0 on success, -errno otherwise.
279 static int spi_nor_read_sr(struct spi_nor *nor, u8 *sr)
284 struct spi_mem_op op =
285 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 1),
288 SPI_MEM_OP_DATA_IN(1, sr, 1));
290 ret = spi_mem_exec_op(nor->spimem, &op);
292 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDSR,
297 dev_dbg(nor->dev, "error %d reading SR\n", ret);
303 * spi_nor_read_fsr() - Read the Flag Status Register.
304 * @nor: pointer to 'struct spi_nor'
305 * @fsr: pointer to a DMA-able buffer where the value of the
306 * Flag Status Register will be written.
308 * Return: 0 on success, -errno otherwise.
310 static int spi_nor_read_fsr(struct spi_nor *nor, u8 *fsr)
315 struct spi_mem_op op =
316 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 1),
319 SPI_MEM_OP_DATA_IN(1, fsr, 1));
321 ret = spi_mem_exec_op(nor->spimem, &op);
323 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDFSR,
328 dev_dbg(nor->dev, "error %d reading FSR\n", ret);
334 * spi_nor_read_cr() - Read the Configuration Register using the
335 * SPINOR_OP_RDCR (35h) command.
336 * @nor: pointer to 'struct spi_nor'
337 * @cr: pointer to a DMA-able buffer where the value of the
338 * Configuration Register will be written.
340 * Return: 0 on success, -errno otherwise.
342 static int spi_nor_read_cr(struct spi_nor *nor, u8 *cr)
347 struct spi_mem_op op =
348 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDCR, 1),
351 SPI_MEM_OP_DATA_IN(1, cr, 1));
353 ret = spi_mem_exec_op(nor->spimem, &op);
355 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDCR, cr, 1);
359 dev_dbg(nor->dev, "error %d reading CR\n", ret);
365 * spi_nor_set_4byte_addr_mode() - Enter/Exit 4-byte address mode.
366 * @nor: pointer to 'struct spi_nor'.
367 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
370 * Return: 0 on success, -errno otherwise.
372 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
377 struct spi_mem_op op =
378 SPI_MEM_OP(SPI_MEM_OP_CMD(enable ?
386 ret = spi_mem_exec_op(nor->spimem, &op);
388 ret = nor->controller_ops->write_reg(nor,
389 enable ? SPINOR_OP_EN4B :
395 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
401 * spansion_set_4byte_addr_mode() - Set 4-byte address mode for Spansion
403 * @nor: pointer to 'struct spi_nor'.
404 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
407 * Return: 0 on success, -errno otherwise.
409 static int spansion_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
413 nor->bouncebuf[0] = enable << 7;
416 struct spi_mem_op op =
417 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_BRWR, 1),
420 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1));
422 ret = spi_mem_exec_op(nor->spimem, &op);
424 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_BRWR,
429 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
435 * spi_nor_write_ear() - Write Extended Address Register.
436 * @nor: pointer to 'struct spi_nor'.
437 * @ear: value to write to the Extended Address Register.
439 * Return: 0 on success, -errno otherwise.
441 int spi_nor_write_ear(struct spi_nor *nor, u8 ear)
445 nor->bouncebuf[0] = ear;
448 struct spi_mem_op op =
449 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREAR, 1),
452 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1));
454 ret = spi_mem_exec_op(nor->spimem, &op);
456 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WREAR,
461 dev_dbg(nor->dev, "error %d writing EAR\n", ret);
467 * spi_nor_xread_sr() - Read the Status Register on S3AN flashes.
468 * @nor: pointer to 'struct spi_nor'.
469 * @sr: pointer to a DMA-able buffer where the value of the
470 * Status Register will be written.
472 * Return: 0 on success, -errno otherwise.
474 int spi_nor_xread_sr(struct spi_nor *nor, u8 *sr)
479 struct spi_mem_op op =
480 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_XRDSR, 1),
483 SPI_MEM_OP_DATA_IN(1, sr, 1));
485 ret = spi_mem_exec_op(nor->spimem, &op);
487 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_XRDSR,
492 dev_dbg(nor->dev, "error %d reading XRDSR\n", ret);
498 * spi_nor_xsr_ready() - Query the Status Register of the S3AN flash to see if
499 * the flash is ready for new commands.
500 * @nor: pointer to 'struct spi_nor'.
502 * Return: 1 if ready, 0 if not ready, -errno on errors.
504 static int spi_nor_xsr_ready(struct spi_nor *nor)
508 ret = spi_nor_xread_sr(nor, nor->bouncebuf);
512 return !!(nor->bouncebuf[0] & XSR_RDY);
516 * spi_nor_clear_sr() - Clear the Status Register.
517 * @nor: pointer to 'struct spi_nor'.
519 static void spi_nor_clear_sr(struct spi_nor *nor)
524 struct spi_mem_op op =
525 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLSR, 1),
530 ret = spi_mem_exec_op(nor->spimem, &op);
532 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CLSR,
537 dev_dbg(nor->dev, "error %d clearing SR\n", ret);
541 * spi_nor_sr_ready() - Query the Status Register to see if the flash is ready
543 * @nor: pointer to 'struct spi_nor'.
545 * Return: 1 if ready, 0 if not ready, -errno on errors.
547 static int spi_nor_sr_ready(struct spi_nor *nor)
549 int ret = spi_nor_read_sr(nor, nor->bouncebuf);
554 if (nor->flags & SNOR_F_USE_CLSR &&
555 nor->bouncebuf[0] & (SR_E_ERR | SR_P_ERR)) {
556 if (nor->bouncebuf[0] & SR_E_ERR)
557 dev_err(nor->dev, "Erase Error occurred\n");
559 dev_err(nor->dev, "Programming Error occurred\n");
561 spi_nor_clear_sr(nor);
564 * WEL bit remains set to one when an erase or page program
565 * error occurs. Issue a Write Disable command to protect
566 * against inadvertent writes that can possibly corrupt the
567 * contents of the memory.
569 ret = spi_nor_write_disable(nor);
576 return !(nor->bouncebuf[0] & SR_WIP);
580 * spi_nor_clear_fsr() - Clear the Flag Status Register.
581 * @nor: pointer to 'struct spi_nor'.
583 static void spi_nor_clear_fsr(struct spi_nor *nor)
588 struct spi_mem_op op =
589 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLFSR, 1),
594 ret = spi_mem_exec_op(nor->spimem, &op);
596 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CLFSR,
601 dev_dbg(nor->dev, "error %d clearing FSR\n", ret);
605 * spi_nor_fsr_ready() - Query the Flag Status Register to see if the flash is
606 * ready for new commands.
607 * @nor: pointer to 'struct spi_nor'.
609 * Return: 1 if ready, 0 if not ready, -errno on errors.
611 static int spi_nor_fsr_ready(struct spi_nor *nor)
613 int ret = spi_nor_read_fsr(nor, nor->bouncebuf);
618 if (nor->bouncebuf[0] & (FSR_E_ERR | FSR_P_ERR)) {
619 if (nor->bouncebuf[0] & FSR_E_ERR)
620 dev_err(nor->dev, "Erase operation failed.\n");
622 dev_err(nor->dev, "Program operation failed.\n");
624 if (nor->bouncebuf[0] & FSR_PT_ERR)
626 "Attempted to modify a protected sector.\n");
628 spi_nor_clear_fsr(nor);
631 * WEL bit remains set to one when an erase or page program
632 * error occurs. Issue a Write Disable command to protect
633 * against inadvertent writes that can possibly corrupt the
634 * contents of the memory.
636 ret = spi_nor_write_disable(nor);
643 return !!(nor->bouncebuf[0] & FSR_READY);
647 * spi_nor_ready() - Query the flash to see if it is ready for new commands.
648 * @nor: pointer to 'struct spi_nor'.
650 * Return: 1 if ready, 0 if not ready, -errno on errors.
652 static int spi_nor_ready(struct spi_nor *nor)
656 if (nor->flags & SNOR_F_READY_XSR_RDY)
657 sr = spi_nor_xsr_ready(nor);
659 sr = spi_nor_sr_ready(nor);
662 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
669 * spi_nor_wait_till_ready_with_timeout() - Service routine to read the
670 * Status Register until ready, or timeout occurs.
671 * @nor: pointer to "struct spi_nor".
672 * @timeout_jiffies: jiffies to wait until timeout.
674 * Return: 0 on success, -errno otherwise.
676 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
677 unsigned long timeout_jiffies)
679 unsigned long deadline;
680 int timeout = 0, ret;
682 deadline = jiffies + timeout_jiffies;
685 if (time_after_eq(jiffies, deadline))
688 ret = spi_nor_ready(nor);
697 dev_dbg(nor->dev, "flash operation timed out\n");
703 * spi_nor_wait_till_ready() - Wait for a predefined amount of time for the
704 * flash to be ready, or timeout occurs.
705 * @nor: pointer to "struct spi_nor".
707 * Return: 0 on success, -errno otherwise.
709 int spi_nor_wait_till_ready(struct spi_nor *nor)
711 return spi_nor_wait_till_ready_with_timeout(nor,
712 DEFAULT_READY_WAIT_JIFFIES);
716 * spi_nor_write_sr() - Write the Status Register.
717 * @nor: pointer to 'struct spi_nor'.
718 * @sr: pointer to DMA-able buffer to write to the Status Register.
719 * @len: number of bytes to write to the Status Register.
721 * Return: 0 on success, -errno otherwise.
723 static int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len)
727 ret = spi_nor_write_enable(nor);
732 struct spi_mem_op op =
733 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR, 1),
736 SPI_MEM_OP_DATA_OUT(len, sr, 1));
738 ret = spi_mem_exec_op(nor->spimem, &op);
740 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRSR,
745 dev_dbg(nor->dev, "error %d writing SR\n", ret);
749 return spi_nor_wait_till_ready(nor);
753 * spi_nor_write_sr1_and_check() - Write one byte to the Status Register 1 and
754 * ensure that the byte written match the received value.
755 * @nor: pointer to a 'struct spi_nor'.
756 * @sr1: byte value to be written to the Status Register.
758 * Return: 0 on success, -errno otherwise.
760 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1)
764 nor->bouncebuf[0] = sr1;
766 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1);
770 ret = spi_nor_read_sr(nor, nor->bouncebuf);
774 if (nor->bouncebuf[0] != sr1) {
775 dev_dbg(nor->dev, "SR1: read back test failed\n");
783 * spi_nor_write_16bit_sr_and_check() - Write the Status Register 1 and the
784 * Status Register 2 in one shot. Ensure that the byte written in the Status
785 * Register 1 match the received value, and that the 16-bit Write did not
786 * affect what was already in the Status Register 2.
787 * @nor: pointer to a 'struct spi_nor'.
788 * @sr1: byte value to be written to the Status Register 1.
790 * Return: 0 on success, -errno otherwise.
792 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1)
795 u8 *sr_cr = nor->bouncebuf;
798 /* Make sure we don't overwrite the contents of Status Register 2. */
799 if (!(nor->flags & SNOR_F_NO_READ_CR)) {
800 ret = spi_nor_read_cr(nor, &sr_cr[1]);
803 } else if (nor->params->quad_enable) {
805 * If the Status Register 2 Read command (35h) is not
806 * supported, we should at least be sure we don't
807 * change the value of the SR2 Quad Enable bit.
809 * We can safely assume that when the Quad Enable method is
810 * set, the value of the QE bit is one, as a consequence of the
811 * nor->params->quad_enable() call.
813 * We can safely assume that the Quad Enable bit is present in
814 * the Status Register 2 at BIT(1). According to the JESD216
815 * revB standard, BFPT DWORDS[15], bits 22:20, the 16-bit
816 * Write Status (01h) command is available just for the cases
817 * in which the QE bit is described in SR2 at BIT(1).
819 sr_cr[1] = SR2_QUAD_EN_BIT1;
826 ret = spi_nor_write_sr(nor, sr_cr, 2);
830 if (nor->flags & SNOR_F_NO_READ_CR)
833 cr_written = sr_cr[1];
835 ret = spi_nor_read_cr(nor, &sr_cr[1]);
839 if (cr_written != sr_cr[1]) {
840 dev_dbg(nor->dev, "CR: read back test failed\n");
848 * spi_nor_write_16bit_cr_and_check() - Write the Status Register 1 and the
849 * Configuration Register in one shot. Ensure that the byte written in the
850 * Configuration Register match the received value, and that the 16-bit Write
851 * did not affect what was already in the Status Register 1.
852 * @nor: pointer to a 'struct spi_nor'.
853 * @cr: byte value to be written to the Configuration Register.
855 * Return: 0 on success, -errno otherwise.
857 static int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr)
860 u8 *sr_cr = nor->bouncebuf;
863 /* Keep the current value of the Status Register 1. */
864 ret = spi_nor_read_sr(nor, sr_cr);
870 ret = spi_nor_write_sr(nor, sr_cr, 2);
874 sr_written = sr_cr[0];
876 ret = spi_nor_read_sr(nor, sr_cr);
880 if (sr_written != sr_cr[0]) {
881 dev_dbg(nor->dev, "SR: Read back test failed\n");
885 if (nor->flags & SNOR_F_NO_READ_CR)
888 ret = spi_nor_read_cr(nor, &sr_cr[1]);
892 if (cr != sr_cr[1]) {
893 dev_dbg(nor->dev, "CR: read back test failed\n");
901 * spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that
902 * the byte written match the received value without affecting other bits in the
903 * Status Register 1 and 2.
904 * @nor: pointer to a 'struct spi_nor'.
905 * @sr1: byte value to be written to the Status Register.
907 * Return: 0 on success, -errno otherwise.
909 static int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1)
911 if (nor->flags & SNOR_F_HAS_16BIT_SR)
912 return spi_nor_write_16bit_sr_and_check(nor, sr1);
914 return spi_nor_write_sr1_and_check(nor, sr1);
918 * spi_nor_write_sr2() - Write the Status Register 2 using the
919 * SPINOR_OP_WRSR2 (3eh) command.
920 * @nor: pointer to 'struct spi_nor'.
921 * @sr2: pointer to DMA-able buffer to write to the Status Register 2.
923 * Return: 0 on success, -errno otherwise.
925 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2)
929 ret = spi_nor_write_enable(nor);
934 struct spi_mem_op op =
935 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR2, 1),
938 SPI_MEM_OP_DATA_OUT(1, sr2, 1));
940 ret = spi_mem_exec_op(nor->spimem, &op);
942 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRSR2,
947 dev_dbg(nor->dev, "error %d writing SR2\n", ret);
951 return spi_nor_wait_till_ready(nor);
955 * spi_nor_read_sr2() - Read the Status Register 2 using the
956 * SPINOR_OP_RDSR2 (3fh) command.
957 * @nor: pointer to 'struct spi_nor'.
958 * @sr2: pointer to DMA-able buffer where the value of the
959 * Status Register 2 will be written.
961 * Return: 0 on success, -errno otherwise.
963 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2)
968 struct spi_mem_op op =
969 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR2, 1),
972 SPI_MEM_OP_DATA_IN(1, sr2, 1));
974 ret = spi_mem_exec_op(nor->spimem, &op);
976 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDSR2,
981 dev_dbg(nor->dev, "error %d reading SR2\n", ret);
987 * spi_nor_erase_chip() - Erase the entire flash memory.
988 * @nor: pointer to 'struct spi_nor'.
990 * Return: 0 on success, -errno otherwise.
992 static int spi_nor_erase_chip(struct spi_nor *nor)
996 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
999 struct spi_mem_op op =
1000 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CHIP_ERASE, 1),
1002 SPI_MEM_OP_NO_DUMMY,
1003 SPI_MEM_OP_NO_DATA);
1005 ret = spi_mem_exec_op(nor->spimem, &op);
1007 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CHIP_ERASE,
1012 dev_dbg(nor->dev, "error %d erasing chip\n", ret);
1017 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
1021 for (i = 0; i < size; i++)
1022 if (table[i][0] == opcode)
1025 /* No conversion found, keep input op code. */
1029 u8 spi_nor_convert_3to4_read(u8 opcode)
1031 static const u8 spi_nor_3to4_read[][2] = {
1032 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
1033 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
1034 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
1035 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
1036 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
1037 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
1038 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
1039 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
1041 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
1042 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
1043 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
1046 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
1047 ARRAY_SIZE(spi_nor_3to4_read));
1050 static u8 spi_nor_convert_3to4_program(u8 opcode)
1052 static const u8 spi_nor_3to4_program[][2] = {
1053 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
1054 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
1055 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
1056 { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B },
1057 { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B },
1060 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
1061 ARRAY_SIZE(spi_nor_3to4_program));
1064 static u8 spi_nor_convert_3to4_erase(u8 opcode)
1066 static const u8 spi_nor_3to4_erase[][2] = {
1067 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
1068 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
1069 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
1072 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
1073 ARRAY_SIZE(spi_nor_3to4_erase));
1076 static bool spi_nor_has_uniform_erase(const struct spi_nor *nor)
1078 return !!nor->params->erase_map.uniform_erase_type;
1081 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor)
1083 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
1084 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
1085 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
1087 if (!spi_nor_has_uniform_erase(nor)) {
1088 struct spi_nor_erase_map *map = &nor->params->erase_map;
1089 struct spi_nor_erase_type *erase;
1092 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1093 erase = &map->erase_type[i];
1095 spi_nor_convert_3to4_erase(erase->opcode);
1100 int spi_nor_lock_and_prep(struct spi_nor *nor)
1104 mutex_lock(&nor->lock);
1106 if (nor->controller_ops && nor->controller_ops->prepare) {
1107 ret = nor->controller_ops->prepare(nor);
1109 mutex_unlock(&nor->lock);
1116 void spi_nor_unlock_and_unprep(struct spi_nor *nor)
1118 if (nor->controller_ops && nor->controller_ops->unprepare)
1119 nor->controller_ops->unprepare(nor);
1120 mutex_unlock(&nor->lock);
1123 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)
1125 if (!nor->params->convert_addr)
1128 return nor->params->convert_addr(nor, addr);
1132 * Initiate the erasure of a single sector
1134 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
1138 addr = spi_nor_convert_addr(nor, addr);
1141 struct spi_mem_op op =
1142 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1),
1143 SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
1144 SPI_MEM_OP_NO_DUMMY,
1145 SPI_MEM_OP_NO_DATA);
1147 return spi_mem_exec_op(nor->spimem, &op);
1148 } else if (nor->controller_ops->erase) {
1149 return nor->controller_ops->erase(nor, addr);
1153 * Default implementation, if driver doesn't have a specialized HW
1156 for (i = nor->addr_width - 1; i >= 0; i--) {
1157 nor->bouncebuf[i] = addr & 0xff;
1161 return nor->controller_ops->write_reg(nor, nor->erase_opcode,
1162 nor->bouncebuf, nor->addr_width);
1166 * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
1167 * @erase: pointer to a structure that describes a SPI NOR erase type
1168 * @dividend: dividend value
1169 * @remainder: pointer to u32 remainder (will be updated)
1171 * Return: the result of the division
1173 static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase,
1174 u64 dividend, u32 *remainder)
1176 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
1177 *remainder = (u32)dividend & erase->size_mask;
1178 return dividend >> erase->size_shift;
1182 * spi_nor_find_best_erase_type() - find the best erase type for the given
1183 * offset in the serial flash memory and the
1184 * number of bytes to erase. The region in
1185 * which the address fits is expected to be
1187 * @map: the erase map of the SPI NOR
1188 * @region: pointer to a structure that describes a SPI NOR erase region
1189 * @addr: offset in the serial flash memory
1190 * @len: number of bytes to erase
1192 * Return: a pointer to the best fitted erase type, NULL otherwise.
1194 static const struct spi_nor_erase_type *
1195 spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
1196 const struct spi_nor_erase_region *region,
1199 const struct spi_nor_erase_type *erase;
1202 u8 erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
1205 * Erase types are ordered by size, with the smallest erase type at
1208 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
1209 /* Does the erase region support the tested erase type? */
1210 if (!(erase_mask & BIT(i)))
1213 erase = &map->erase_type[i];
1215 /* Don't erase more than what the user has asked for. */
1216 if (erase->size > len)
1219 /* Alignment is not mandatory for overlaid regions */
1220 if (region->offset & SNOR_OVERLAID_REGION)
1223 spi_nor_div_by_erase_size(erase, addr, &rem);
1233 static u64 spi_nor_region_is_last(const struct spi_nor_erase_region *region)
1235 return region->offset & SNOR_LAST_REGION;
1238 static u64 spi_nor_region_end(const struct spi_nor_erase_region *region)
1240 return (region->offset & ~SNOR_ERASE_FLAGS_MASK) + region->size;
1244 * spi_nor_region_next() - get the next spi nor region
1245 * @region: pointer to a structure that describes a SPI NOR erase region
1247 * Return: the next spi nor region or NULL if last region.
1249 struct spi_nor_erase_region *
1250 spi_nor_region_next(struct spi_nor_erase_region *region)
1252 if (spi_nor_region_is_last(region))
1259 * spi_nor_find_erase_region() - find the region of the serial flash memory in
1260 * which the offset fits
1261 * @map: the erase map of the SPI NOR
1262 * @addr: offset in the serial flash memory
1264 * Return: a pointer to the spi_nor_erase_region struct, ERR_PTR(-errno)
1267 static struct spi_nor_erase_region *
1268 spi_nor_find_erase_region(const struct spi_nor_erase_map *map, u64 addr)
1270 struct spi_nor_erase_region *region = map->regions;
1271 u64 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1272 u64 region_end = region_start + region->size;
1274 while (addr < region_start || addr >= region_end) {
1275 region = spi_nor_region_next(region);
1277 return ERR_PTR(-EINVAL);
1279 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1280 region_end = region_start + region->size;
1287 * spi_nor_init_erase_cmd() - initialize an erase command
1288 * @region: pointer to a structure that describes a SPI NOR erase region
1289 * @erase: pointer to a structure that describes a SPI NOR erase type
1291 * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
1294 static struct spi_nor_erase_command *
1295 spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region,
1296 const struct spi_nor_erase_type *erase)
1298 struct spi_nor_erase_command *cmd;
1300 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1302 return ERR_PTR(-ENOMEM);
1304 INIT_LIST_HEAD(&cmd->list);
1305 cmd->opcode = erase->opcode;
1308 if (region->offset & SNOR_OVERLAID_REGION)
1309 cmd->size = region->size;
1311 cmd->size = erase->size;
1317 * spi_nor_destroy_erase_cmd_list() - destroy erase command list
1318 * @erase_list: list of erase commands
1320 static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list)
1322 struct spi_nor_erase_command *cmd, *next;
1324 list_for_each_entry_safe(cmd, next, erase_list, list) {
1325 list_del(&cmd->list);
1331 * spi_nor_init_erase_cmd_list() - initialize erase command list
1332 * @nor: pointer to a 'struct spi_nor'
1333 * @erase_list: list of erase commands to be executed once we validate that the
1334 * erase can be performed
1335 * @addr: offset in the serial flash memory
1336 * @len: number of bytes to erase
1338 * Builds the list of best fitted erase commands and verifies if the erase can
1341 * Return: 0 on success, -errno otherwise.
1343 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
1344 struct list_head *erase_list,
1347 const struct spi_nor_erase_map *map = &nor->params->erase_map;
1348 const struct spi_nor_erase_type *erase, *prev_erase = NULL;
1349 struct spi_nor_erase_region *region;
1350 struct spi_nor_erase_command *cmd = NULL;
1354 region = spi_nor_find_erase_region(map, addr);
1356 return PTR_ERR(region);
1358 region_end = spi_nor_region_end(region);
1361 erase = spi_nor_find_best_erase_type(map, region, addr, len);
1363 goto destroy_erase_cmd_list;
1365 if (prev_erase != erase ||
1366 region->offset & SNOR_OVERLAID_REGION) {
1367 cmd = spi_nor_init_erase_cmd(region, erase);
1370 goto destroy_erase_cmd_list;
1373 list_add_tail(&cmd->list, erase_list);
1381 if (len && addr >= region_end) {
1382 region = spi_nor_region_next(region);
1384 goto destroy_erase_cmd_list;
1385 region_end = spi_nor_region_end(region);
1393 destroy_erase_cmd_list:
1394 spi_nor_destroy_erase_cmd_list(erase_list);
1399 * spi_nor_erase_multi_sectors() - perform a non-uniform erase
1400 * @nor: pointer to a 'struct spi_nor'
1401 * @addr: offset in the serial flash memory
1402 * @len: number of bytes to erase
1404 * Build a list of best fitted erase commands and execute it once we validate
1405 * that the erase can be performed.
1407 * Return: 0 on success, -errno otherwise.
1409 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
1411 LIST_HEAD(erase_list);
1412 struct spi_nor_erase_command *cmd, *next;
1415 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len);
1419 list_for_each_entry_safe(cmd, next, &erase_list, list) {
1420 nor->erase_opcode = cmd->opcode;
1421 while (cmd->count) {
1422 ret = spi_nor_write_enable(nor);
1424 goto destroy_erase_cmd_list;
1426 ret = spi_nor_erase_sector(nor, addr);
1428 goto destroy_erase_cmd_list;
1433 ret = spi_nor_wait_till_ready(nor);
1435 goto destroy_erase_cmd_list;
1437 list_del(&cmd->list);
1443 destroy_erase_cmd_list:
1444 spi_nor_destroy_erase_cmd_list(&erase_list);
1449 * Erase an address range on the nor chip. The address range may extend
1450 * one or more erase sectors. Return an error is there is a problem erasing.
1452 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
1454 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1459 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
1460 (long long)instr->len);
1462 if (spi_nor_has_uniform_erase(nor)) {
1463 div_u64_rem(instr->len, mtd->erasesize, &rem);
1471 ret = spi_nor_lock_and_prep(nor);
1475 /* whole-chip erase? */
1476 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
1477 unsigned long timeout;
1479 ret = spi_nor_write_enable(nor);
1483 ret = spi_nor_erase_chip(nor);
1488 * Scale the timeout linearly with the size of the flash, with
1489 * a minimum calibrated to an old 2MB flash. We could try to
1490 * pull these from CFI/SFDP, but these values should be good
1493 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
1494 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
1495 (unsigned long)(mtd->size / SZ_2M));
1496 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
1500 /* REVISIT in some cases we could speed up erasing large regions
1501 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
1502 * to use "small sector erase", but that's not always optimal.
1505 /* "sector"-at-a-time erase */
1506 } else if (spi_nor_has_uniform_erase(nor)) {
1508 ret = spi_nor_write_enable(nor);
1512 ret = spi_nor_erase_sector(nor, addr);
1516 addr += mtd->erasesize;
1517 len -= mtd->erasesize;
1519 ret = spi_nor_wait_till_ready(nor);
1524 /* erase multiple sectors */
1526 ret = spi_nor_erase_multi_sectors(nor, addr, len);
1531 ret = spi_nor_write_disable(nor);
1534 spi_nor_unlock_and_unprep(nor);
1539 static u8 spi_nor_get_sr_bp_mask(struct spi_nor *nor)
1541 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1543 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6)
1544 return mask | SR_BP3_BIT6;
1546 if (nor->flags & SNOR_F_HAS_4BIT_BP)
1547 return mask | SR_BP3;
1552 static u8 spi_nor_get_sr_tb_mask(struct spi_nor *nor)
1554 if (nor->flags & SNOR_F_HAS_SR_TB_BIT6)
1560 static u64 spi_nor_get_min_prot_length_sr(struct spi_nor *nor)
1562 unsigned int bp_slots, bp_slots_needed;
1563 u8 mask = spi_nor_get_sr_bp_mask(nor);
1565 /* Reserved one for "protect none" and one for "protect all". */
1566 bp_slots = (1 << hweight8(mask)) - 2;
1567 bp_slots_needed = ilog2(nor->info->n_sectors);
1569 if (bp_slots_needed > bp_slots)
1570 return nor->info->sector_size <<
1571 (bp_slots_needed - bp_slots);
1573 return nor->info->sector_size;
1576 static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,
1579 struct mtd_info *mtd = &nor->mtd;
1581 u8 mask = spi_nor_get_sr_bp_mask(nor);
1582 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
1583 u8 bp, val = sr & mask;
1585 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6)
1586 val = (val & ~SR_BP3_BIT6) | SR_BP3;
1588 bp = val >> SR_BP_SHIFT;
1597 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
1598 *len = min_prot_len << (bp - 1);
1600 if (*len > mtd->size)
1603 if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask)
1606 *ofs = mtd->size - *len;
1610 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
1611 * @locked is false); 0 otherwise
1613 static int spi_nor_check_lock_status_sr(struct spi_nor *nor, loff_t ofs,
1614 uint64_t len, u8 sr, bool locked)
1622 spi_nor_get_locked_range_sr(nor, sr, &lock_offs, &lock_len);
1625 /* Requested range is a sub-range of locked range */
1626 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
1628 /* Requested range does not overlap with locked range */
1629 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
1632 static int spi_nor_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1635 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, true);
1638 static int spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1641 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, false);
1645 * Lock a region of the flash. Compatible with ST Micro and similar flash.
1646 * Supports the block protection bits BP{0,1,2}/BP{0,1,2,3} in the status
1648 * (SR). Does not support these features found in newer SR bitfields:
1649 * - SEC: sector/block protect - only handle SEC=0 (block protect)
1650 * - CMP: complement protect - only support CMP=0 (range is not complemented)
1652 * Support for the following is provided conditionally for some flash:
1653 * - TB: top/bottom protect
1655 * Sample table portion for 8MB flash (Winbond w25q64fw):
1657 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
1658 * --------------------------------------------------------------------------
1659 * X | X | 0 | 0 | 0 | NONE | NONE
1660 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
1661 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
1662 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
1663 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
1664 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
1665 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
1666 * X | X | 1 | 1 | 1 | 8 MB | ALL
1667 * ------|-------|-------|-------|-------|---------------|-------------------
1668 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
1669 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
1670 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
1671 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
1672 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
1673 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
1675 * Returns negative on errors, 0 on success.
1677 static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1679 struct mtd_info *mtd = &nor->mtd;
1681 int ret, status_old, status_new;
1682 u8 mask = spi_nor_get_sr_bp_mask(nor);
1683 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
1686 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1689 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1693 status_old = nor->bouncebuf[0];
1695 /* If nothing in our range is unlocked, we don't need to do anything */
1696 if (spi_nor_is_locked_sr(nor, ofs, len, status_old))
1699 /* If anything below us is unlocked, we can't use 'bottom' protection */
1700 if (!spi_nor_is_locked_sr(nor, 0, ofs, status_old))
1701 can_be_bottom = false;
1703 /* If anything above us is unlocked, we can't use 'top' protection */
1704 if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
1708 if (!can_be_bottom && !can_be_top)
1711 /* Prefer top, if both are valid */
1712 use_top = can_be_top;
1714 /* lock_len: length of region that should end up locked */
1716 lock_len = mtd->size - ofs;
1718 lock_len = ofs + len;
1720 if (lock_len == mtd->size) {
1723 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
1724 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
1725 val = pow << SR_BP_SHIFT;
1727 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
1728 val = (val & ~SR_BP3) | SR_BP3_BIT6;
1733 /* Don't "lock" with no region! */
1738 status_new = (status_old & ~mask & ~tb_mask) | val;
1740 /* Disallow further writes if WP pin is asserted */
1741 status_new |= SR_SRWD;
1744 status_new |= tb_mask;
1746 /* Don't bother if they're the same */
1747 if (status_new == status_old)
1750 /* Only modify protection if it will not unlock other areas */
1751 if ((status_new & mask) < (status_old & mask))
1754 return spi_nor_write_sr_and_check(nor, status_new);
1758 * Unlock a region of the flash. See spi_nor_sr_lock() for more info
1760 * Returns negative on errors, 0 on success.
1762 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1764 struct mtd_info *mtd = &nor->mtd;
1766 int ret, status_old, status_new;
1767 u8 mask = spi_nor_get_sr_bp_mask(nor);
1768 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
1771 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1774 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1778 status_old = nor->bouncebuf[0];
1780 /* If nothing in our range is locked, we don't need to do anything */
1781 if (spi_nor_is_unlocked_sr(nor, ofs, len, status_old))
1784 /* If anything below us is locked, we can't use 'top' protection */
1785 if (!spi_nor_is_unlocked_sr(nor, 0, ofs, status_old))
1788 /* If anything above us is locked, we can't use 'bottom' protection */
1789 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
1791 can_be_bottom = false;
1793 if (!can_be_bottom && !can_be_top)
1796 /* Prefer top, if both are valid */
1797 use_top = can_be_top;
1799 /* lock_len: length of region that should remain locked */
1801 lock_len = mtd->size - (ofs + len);
1805 if (lock_len == 0) {
1806 val = 0; /* fully unlocked */
1808 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
1809 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
1810 val = pow << SR_BP_SHIFT;
1812 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
1813 val = (val & ~SR_BP3) | SR_BP3_BIT6;
1815 /* Some power-of-two sizes are not supported */
1820 status_new = (status_old & ~mask & ~tb_mask) | val;
1822 /* Don't protect status register if we're fully unlocked */
1824 status_new &= ~SR_SRWD;
1827 status_new |= tb_mask;
1829 /* Don't bother if they're the same */
1830 if (status_new == status_old)
1833 /* Only modify protection if it will not lock other areas */
1834 if ((status_new & mask) > (status_old & mask))
1837 return spi_nor_write_sr_and_check(nor, status_new);
1841 * Check if a region of the flash is (completely) locked. See spi_nor_sr_lock()
1844 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
1845 * negative on errors.
1847 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1851 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1855 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
1858 static const struct spi_nor_locking_ops spi_nor_sr_locking_ops = {
1859 .lock = spi_nor_sr_lock,
1860 .unlock = spi_nor_sr_unlock,
1861 .is_locked = spi_nor_sr_is_locked,
1864 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1866 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1869 ret = spi_nor_lock_and_prep(nor);
1873 ret = nor->params->locking_ops->lock(nor, ofs, len);
1875 spi_nor_unlock_and_unprep(nor);
1879 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1881 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1884 ret = spi_nor_lock_and_prep(nor);
1888 ret = nor->params->locking_ops->unlock(nor, ofs, len);
1890 spi_nor_unlock_and_unprep(nor);
1894 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1896 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1899 ret = spi_nor_lock_and_prep(nor);
1903 ret = nor->params->locking_ops->is_locked(nor, ofs, len);
1905 spi_nor_unlock_and_unprep(nor);
1910 * spi_nor_sr1_bit6_quad_enable() - Set/Unset the Quad Enable BIT(6) in the
1911 * Status Register 1.
1912 * @nor: pointer to a 'struct spi_nor'
1913 * @enable: true to enable Quad mode, false to disable Quad mode.
1915 * Bit 6 of the Status Register 1 is the QE bit for Macronix like QSPI memories.
1917 * Return: 0 on success, -errno otherwise.
1919 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor, bool enable)
1923 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1927 if ((enable && (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6)) ||
1928 (!enable && !(nor->bouncebuf[0] & SR1_QUAD_EN_BIT6)))
1932 nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6;
1934 nor->bouncebuf[0] &= ~SR1_QUAD_EN_BIT6;
1936 return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]);
1940 * spi_nor_sr2_bit1_quad_enable() - set/unset the Quad Enable BIT(1) in the
1941 * Status Register 2.
1942 * @nor: pointer to a 'struct spi_nor'.
1943 * @enable: true to enable Quad mode, false to disable Quad mode.
1945 * Bit 1 of the Status Register 2 is the QE bit for Spansion like QSPI memories.
1947 * Return: 0 on success, -errno otherwise.
1949 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor, bool enable)
1953 if (nor->flags & SNOR_F_NO_READ_CR)
1954 return spi_nor_write_16bit_cr_and_check(nor,
1955 enable ? SR2_QUAD_EN_BIT1 : 0);
1957 ret = spi_nor_read_cr(nor, nor->bouncebuf);
1961 if ((enable && (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1)) ||
1962 (!enable && !(nor->bouncebuf[0] & SR2_QUAD_EN_BIT1)))
1966 nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1;
1968 nor->bouncebuf[0] &= ~SR2_QUAD_EN_BIT1;
1970 return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]);
1974 * spi_nor_sr2_bit7_quad_enable() - set/unset QE bit in Status Register 2.
1975 * @nor: pointer to a 'struct spi_nor'
1976 * @enable: true to enable Quad mode, false to disable Quad mode.
1978 * Set the Quad Enable (QE) bit in the Status Register 2.
1980 * This is one of the procedures to set the QE bit described in the SFDP
1981 * (JESD216 rev B) specification but no manufacturer using this procedure has
1982 * been identified yet, hence the name of the function.
1984 * Return: 0 on success, -errno otherwise.
1986 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor, bool enable)
1988 u8 *sr2 = nor->bouncebuf;
1992 /* Check current Quad Enable bit value. */
1993 ret = spi_nor_read_sr2(nor, sr2);
1996 if ((enable && (*sr2 & SR2_QUAD_EN_BIT7)) ||
1997 (!enable && !(*sr2 & SR2_QUAD_EN_BIT7)))
2000 /* Update the Quad Enable bit. */
2002 *sr2 |= SR2_QUAD_EN_BIT7;
2004 *sr2 &= ~SR2_QUAD_EN_BIT7;
2006 ret = spi_nor_write_sr2(nor, sr2);
2012 /* Read back and check it. */
2013 ret = spi_nor_read_sr2(nor, sr2);
2017 if (*sr2 != sr2_written) {
2018 dev_dbg(nor->dev, "SR2: Read back test failed\n");
2025 static const struct spi_nor_manufacturer *manufacturers[] = {
2032 &spi_nor_gigadevice,
2045 static const struct flash_info *
2046 spi_nor_search_part_by_id(const struct flash_info *parts, unsigned int nparts,
2051 for (i = 0; i < nparts; i++) {
2052 if (parts[i].id_len &&
2053 !memcmp(parts[i].id, id, parts[i].id_len))
2060 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
2062 const struct flash_info *info;
2063 u8 *id = nor->bouncebuf;
2068 struct spi_mem_op op =
2069 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDID, 1),
2071 SPI_MEM_OP_NO_DUMMY,
2072 SPI_MEM_OP_DATA_IN(SPI_NOR_MAX_ID_LEN, id, 1));
2074 ret = spi_mem_exec_op(nor->spimem, &op);
2076 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id,
2077 SPI_NOR_MAX_ID_LEN);
2080 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret);
2081 return ERR_PTR(ret);
2084 for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
2085 info = spi_nor_search_part_by_id(manufacturers[i]->parts,
2086 manufacturers[i]->nparts,
2089 nor->manufacturer = manufacturers[i];
2094 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
2095 SPI_NOR_MAX_ID_LEN, id);
2096 return ERR_PTR(-ENODEV);
2099 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
2100 size_t *retlen, u_char *buf)
2102 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2105 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
2107 ret = spi_nor_lock_and_prep(nor);
2114 addr = spi_nor_convert_addr(nor, addr);
2116 ret = spi_nor_read_data(nor, addr, len, buf);
2118 /* We shouldn't see 0-length reads */
2134 spi_nor_unlock_and_unprep(nor);
2139 * Write an address range to the nor chip. Data must be written in
2140 * FLASH_PAGESIZE chunks. The address range may be any size provided
2141 * it is within the physical boundaries.
2143 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
2144 size_t *retlen, const u_char *buf)
2146 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2147 size_t page_offset, page_remain, i;
2150 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
2152 ret = spi_nor_lock_and_prep(nor);
2156 for (i = 0; i < len; ) {
2158 loff_t addr = to + i;
2161 * If page_size is a power of two, the offset can be quickly
2162 * calculated with an AND operation. On the other cases we
2163 * need to do a modulus operation (more expensive).
2164 * Power of two numbers have only one bit set and we can use
2165 * the instruction hweight32 to detect if we need to do a
2166 * modulus (do_div()) or not.
2168 if (hweight32(nor->page_size) == 1) {
2169 page_offset = addr & (nor->page_size - 1);
2171 uint64_t aux = addr;
2173 page_offset = do_div(aux, nor->page_size);
2175 /* the size of data remaining on the first page */
2176 page_remain = min_t(size_t,
2177 nor->page_size - page_offset, len - i);
2179 addr = spi_nor_convert_addr(nor, addr);
2181 ret = spi_nor_write_enable(nor);
2185 ret = spi_nor_write_data(nor, addr, page_remain, buf + i);
2190 ret = spi_nor_wait_till_ready(nor);
2198 spi_nor_unlock_and_unprep(nor);
2202 static int spi_nor_check(struct spi_nor *nor)
2205 (!nor->spimem && !nor->controller_ops) ||
2206 (!nor->spimem && nor->controller_ops &&
2207 (!nor->controller_ops->read ||
2208 !nor->controller_ops->write ||
2209 !nor->controller_ops->read_reg ||
2210 !nor->controller_ops->write_reg))) {
2211 pr_err("spi-nor: please fill all the necessary fields!\n");
2215 if (nor->spimem && nor->controller_ops) {
2216 dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one of them.\n");
2224 spi_nor_set_read_settings(struct spi_nor_read_command *read,
2228 enum spi_nor_protocol proto)
2230 read->num_mode_clocks = num_mode_clocks;
2231 read->num_wait_states = num_wait_states;
2232 read->opcode = opcode;
2233 read->proto = proto;
2236 void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
2237 enum spi_nor_protocol proto)
2239 pp->opcode = opcode;
2243 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2247 for (i = 0; i < size; i++)
2248 if (table[i][0] == (int)hwcaps)
2254 int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2256 static const int hwcaps_read2cmd[][2] = {
2257 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
2258 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
2259 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
2260 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
2261 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
2262 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
2263 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
2264 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
2265 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
2266 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
2267 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
2268 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
2269 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
2270 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
2271 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
2274 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2275 ARRAY_SIZE(hwcaps_read2cmd));
2278 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2280 static const int hwcaps_pp2cmd[][2] = {
2281 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
2282 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
2283 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
2284 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
2285 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
2286 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
2287 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
2290 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2291 ARRAY_SIZE(hwcaps_pp2cmd));
2295 * spi_nor_spimem_check_op - check if the operation is supported
2297 *@nor: pointer to a 'struct spi_nor'
2298 *@op: pointer to op template to be checked
2300 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2302 static int spi_nor_spimem_check_op(struct spi_nor *nor,
2303 struct spi_mem_op *op)
2306 * First test with 4 address bytes. The opcode itself might
2307 * be a 3B addressing opcode but we don't care, because
2308 * SPI controller implementation should not check the opcode,
2309 * but just the sequence.
2311 op->addr.nbytes = 4;
2312 if (!spi_mem_supports_op(nor->spimem, op)) {
2313 if (nor->mtd.size > SZ_16M)
2316 /* If flash size <= 16MB, 3 address bytes are sufficient */
2317 op->addr.nbytes = 3;
2318 if (!spi_mem_supports_op(nor->spimem, op))
2326 * spi_nor_spimem_check_readop - check if the read op is supported
2328 *@nor: pointer to a 'struct spi_nor'
2329 *@read: pointer to op template to be checked
2331 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2333 static int spi_nor_spimem_check_readop(struct spi_nor *nor,
2334 const struct spi_nor_read_command *read)
2336 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(read->opcode, 1),
2337 SPI_MEM_OP_ADDR(3, 0, 1),
2338 SPI_MEM_OP_DUMMY(0, 1),
2339 SPI_MEM_OP_DATA_IN(0, NULL, 1));
2341 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(read->proto);
2342 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(read->proto);
2343 op.data.buswidth = spi_nor_get_protocol_data_nbits(read->proto);
2344 op.dummy.buswidth = op.addr.buswidth;
2345 op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
2346 op.dummy.buswidth / 8;
2348 return spi_nor_spimem_check_op(nor, &op);
2352 * spi_nor_spimem_check_pp - check if the page program op is supported
2354 *@nor: pointer to a 'struct spi_nor'
2355 *@pp: pointer to op template to be checked
2357 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2359 static int spi_nor_spimem_check_pp(struct spi_nor *nor,
2360 const struct spi_nor_pp_command *pp)
2362 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(pp->opcode, 1),
2363 SPI_MEM_OP_ADDR(3, 0, 1),
2364 SPI_MEM_OP_NO_DUMMY,
2365 SPI_MEM_OP_DATA_OUT(0, NULL, 1));
2367 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(pp->proto);
2368 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(pp->proto);
2369 op.data.buswidth = spi_nor_get_protocol_data_nbits(pp->proto);
2371 return spi_nor_spimem_check_op(nor, &op);
2375 * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
2376 * based on SPI controller capabilities
2377 * @nor: pointer to a 'struct spi_nor'
2378 * @hwcaps: pointer to resulting capabilities after adjusting
2379 * according to controller and flash's capability
2382 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps)
2384 struct spi_nor_flash_parameter *params = nor->params;
2387 /* DTR modes are not supported yet, mask them all. */
2388 *hwcaps &= ~SNOR_HWCAPS_DTR;
2390 /* X-X-X modes are not supported yet, mask them all. */
2391 *hwcaps &= ~SNOR_HWCAPS_X_X_X;
2393 for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
2396 if (!(*hwcaps & BIT(cap)))
2399 rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
2401 spi_nor_spimem_check_readop(nor, ¶ms->reads[rdidx]))
2402 *hwcaps &= ~BIT(cap);
2404 ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
2408 if (spi_nor_spimem_check_pp(nor,
2409 ¶ms->page_programs[ppidx]))
2410 *hwcaps &= ~BIT(cap);
2415 * spi_nor_set_erase_type() - set a SPI NOR erase type
2416 * @erase: pointer to a structure that describes a SPI NOR erase type
2417 * @size: the size of the sector/block erased by the erase type
2418 * @opcode: the SPI command op code to erase the sector/block
2420 void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
2424 erase->opcode = opcode;
2425 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
2426 erase->size_shift = ffs(erase->size) - 1;
2427 erase->size_mask = (1 << erase->size_shift) - 1;
2431 * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
2432 * @map: the erase map of the SPI NOR
2433 * @erase_mask: bitmask encoding erase types that can erase the entire
2435 * @flash_size: the spi nor flash memory size
2437 void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
2438 u8 erase_mask, u64 flash_size)
2440 /* Offset 0 with erase_mask and SNOR_LAST_REGION bit set */
2441 map->uniform_region.offset = (erase_mask & SNOR_ERASE_TYPE_MASK) |
2443 map->uniform_region.size = flash_size;
2444 map->regions = &map->uniform_region;
2445 map->uniform_erase_type = erase_mask;
2448 int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
2449 const struct sfdp_parameter_header *bfpt_header,
2450 const struct sfdp_bfpt *bfpt,
2451 struct spi_nor_flash_parameter *params)
2455 if (nor->manufacturer && nor->manufacturer->fixups &&
2456 nor->manufacturer->fixups->post_bfpt) {
2457 ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header,
2463 if (nor->info->fixups && nor->info->fixups->post_bfpt)
2464 return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt,
2470 static int spi_nor_select_read(struct spi_nor *nor,
2473 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2474 const struct spi_nor_read_command *read;
2479 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2483 read = &nor->params->reads[cmd];
2484 nor->read_opcode = read->opcode;
2485 nor->read_proto = read->proto;
2488 * In the SPI NOR framework, we don't need to make the difference
2489 * between mode clock cycles and wait state clock cycles.
2490 * Indeed, the value of the mode clock cycles is used by a QSPI
2491 * flash memory to know whether it should enter or leave its 0-4-4
2492 * (Continuous Read / XIP) mode.
2493 * eXecution In Place is out of the scope of the mtd sub-system.
2494 * Hence we choose to merge both mode and wait state clock cycles
2495 * into the so called dummy clock cycles.
2497 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2501 static int spi_nor_select_pp(struct spi_nor *nor,
2504 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2505 const struct spi_nor_pp_command *pp;
2510 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2514 pp = &nor->params->page_programs[cmd];
2515 nor->program_opcode = pp->opcode;
2516 nor->write_proto = pp->proto;
2521 * spi_nor_select_uniform_erase() - select optimum uniform erase type
2522 * @map: the erase map of the SPI NOR
2523 * @wanted_size: the erase type size to search for. Contains the value of
2524 * info->sector_size or of the "small sector" size in case
2525 * CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined.
2527 * Once the optimum uniform sector erase command is found, disable all the
2530 * Return: pointer to erase type on success, NULL otherwise.
2532 static const struct spi_nor_erase_type *
2533 spi_nor_select_uniform_erase(struct spi_nor_erase_map *map,
2534 const u32 wanted_size)
2536 const struct spi_nor_erase_type *tested_erase, *erase = NULL;
2538 u8 uniform_erase_type = map->uniform_erase_type;
2540 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
2541 if (!(uniform_erase_type & BIT(i)))
2544 tested_erase = &map->erase_type[i];
2547 * If the current erase size is the one, stop here:
2548 * we have found the right uniform Sector Erase command.
2550 if (tested_erase->size == wanted_size) {
2551 erase = tested_erase;
2556 * Otherwise, the current erase size is still a valid canditate.
2557 * Select the biggest valid candidate.
2559 if (!erase && tested_erase->size)
2560 erase = tested_erase;
2561 /* keep iterating to find the wanted_size */
2567 /* Disable all other Sector Erase commands. */
2568 map->uniform_erase_type &= ~SNOR_ERASE_TYPE_MASK;
2569 map->uniform_erase_type |= BIT(erase - map->erase_type);
2573 static int spi_nor_select_erase(struct spi_nor *nor)
2575 struct spi_nor_erase_map *map = &nor->params->erase_map;
2576 const struct spi_nor_erase_type *erase = NULL;
2577 struct mtd_info *mtd = &nor->mtd;
2578 u32 wanted_size = nor->info->sector_size;
2582 * The previous implementation handling Sector Erase commands assumed
2583 * that the SPI flash memory has an uniform layout then used only one
2584 * of the supported erase sizes for all Sector Erase commands.
2585 * So to be backward compatible, the new implementation also tries to
2586 * manage the SPI flash memory as uniform with a single erase sector
2587 * size, when possible.
2589 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2590 /* prefer "small sector" erase if possible */
2591 wanted_size = 4096u;
2594 if (spi_nor_has_uniform_erase(nor)) {
2595 erase = spi_nor_select_uniform_erase(map, wanted_size);
2598 nor->erase_opcode = erase->opcode;
2599 mtd->erasesize = erase->size;
2604 * For non-uniform SPI flash memory, set mtd->erasesize to the
2605 * maximum erase sector size. No need to set nor->erase_opcode.
2607 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
2608 if (map->erase_type[i].size) {
2609 erase = &map->erase_type[i];
2617 mtd->erasesize = erase->size;
2621 static int spi_nor_default_setup(struct spi_nor *nor,
2622 const struct spi_nor_hwcaps *hwcaps)
2624 struct spi_nor_flash_parameter *params = nor->params;
2625 u32 ignored_mask, shared_mask;
2629 * Keep only the hardware capabilities supported by both the SPI
2630 * controller and the SPI flash memory.
2632 shared_mask = hwcaps->mask & params->hwcaps.mask;
2636 * When called from spi_nor_probe(), all caps are set and we
2637 * need to discard some of them based on what the SPI
2638 * controller actually supports (using spi_mem_supports_op()).
2640 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask);
2643 * SPI n-n-n protocols are not supported when the SPI
2644 * controller directly implements the spi_nor interface.
2645 * Yet another reason to switch to spi-mem.
2647 ignored_mask = SNOR_HWCAPS_X_X_X;
2648 if (shared_mask & ignored_mask) {
2650 "SPI n-n-n protocols are not supported.\n");
2651 shared_mask &= ~ignored_mask;
2655 /* Select the (Fast) Read command. */
2656 err = spi_nor_select_read(nor, shared_mask);
2659 "can't select read settings supported by both the SPI controller and memory.\n");
2663 /* Select the Page Program command. */
2664 err = spi_nor_select_pp(nor, shared_mask);
2667 "can't select write settings supported by both the SPI controller and memory.\n");
2671 /* Select the Sector Erase command. */
2672 err = spi_nor_select_erase(nor);
2675 "can't select erase settings supported by both the SPI controller and memory.\n");
2682 static int spi_nor_setup(struct spi_nor *nor,
2683 const struct spi_nor_hwcaps *hwcaps)
2685 if (!nor->params->setup)
2688 return nor->params->setup(nor, hwcaps);
2692 * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and
2693 * settings based on MFR register and ->default_init() hook.
2694 * @nor: pointer to a 'struct spi_nor'.
2696 static void spi_nor_manufacturer_init_params(struct spi_nor *nor)
2698 if (nor->manufacturer && nor->manufacturer->fixups &&
2699 nor->manufacturer->fixups->default_init)
2700 nor->manufacturer->fixups->default_init(nor);
2702 if (nor->info->fixups && nor->info->fixups->default_init)
2703 nor->info->fixups->default_init(nor);
2707 * spi_nor_sfdp_init_params() - Initialize the flash's parameters and settings
2708 * based on JESD216 SFDP standard.
2709 * @nor: pointer to a 'struct spi_nor'.
2711 * The method has a roll-back mechanism: in case the SFDP parsing fails, the
2712 * legacy flash parameters and settings will be restored.
2714 static void spi_nor_sfdp_init_params(struct spi_nor *nor)
2716 struct spi_nor_flash_parameter sfdp_params;
2718 memcpy(&sfdp_params, nor->params, sizeof(sfdp_params));
2720 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
2721 nor->addr_width = 0;
2722 nor->flags &= ~SNOR_F_4B_OPCODES;
2724 memcpy(nor->params, &sfdp_params, sizeof(*nor->params));
2729 * spi_nor_info_init_params() - Initialize the flash's parameters and settings
2730 * based on nor->info data.
2731 * @nor: pointer to a 'struct spi_nor'.
2733 static void spi_nor_info_init_params(struct spi_nor *nor)
2735 struct spi_nor_flash_parameter *params = nor->params;
2736 struct spi_nor_erase_map *map = ¶ms->erase_map;
2737 const struct flash_info *info = nor->info;
2738 struct device_node *np = spi_nor_get_flash_node(nor);
2741 /* Initialize legacy flash parameters and settings. */
2742 params->quad_enable = spi_nor_sr2_bit1_quad_enable;
2743 params->set_4byte_addr_mode = spansion_set_4byte_addr_mode;
2744 params->setup = spi_nor_default_setup;
2745 /* Default to 16-bit Write Status (01h) Command */
2746 nor->flags |= SNOR_F_HAS_16BIT_SR;
2748 /* Set SPI NOR sizes. */
2749 params->size = (u64)info->sector_size * info->n_sectors;
2750 params->page_size = info->page_size;
2752 if (!(info->flags & SPI_NOR_NO_FR)) {
2753 /* Default to Fast Read for DT and non-DT platform devices. */
2754 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2756 /* Mask out Fast Read if not requested at DT instantiation. */
2757 if (np && !of_property_read_bool(np, "m25p,fast-read"))
2758 params->hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2761 /* (Fast) Read settings. */
2762 params->hwcaps.mask |= SNOR_HWCAPS_READ;
2763 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
2764 0, 0, SPINOR_OP_READ,
2767 if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
2768 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
2769 0, 8, SPINOR_OP_READ_FAST,
2772 if (info->flags & SPI_NOR_DUAL_READ) {
2773 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2774 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2],
2775 0, 8, SPINOR_OP_READ_1_1_2,
2779 if (info->flags & SPI_NOR_QUAD_READ) {
2780 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2781 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
2782 0, 8, SPINOR_OP_READ_1_1_4,
2786 if (info->flags & SPI_NOR_OCTAL_READ) {
2787 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
2788 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_8],
2789 0, 8, SPINOR_OP_READ_1_1_8,
2793 /* Page Program settings. */
2794 params->hwcaps.mask |= SNOR_HWCAPS_PP;
2795 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
2796 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2799 * Sector Erase settings. Sort Erase Types in ascending order, with the
2800 * smallest erase size starting at BIT(0).
2804 if (info->flags & SECT_4K_PMC) {
2805 erase_mask |= BIT(i);
2806 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
2807 SPINOR_OP_BE_4K_PMC);
2809 } else if (info->flags & SECT_4K) {
2810 erase_mask |= BIT(i);
2811 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
2815 erase_mask |= BIT(i);
2816 spi_nor_set_erase_type(&map->erase_type[i], info->sector_size,
2818 spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
2822 * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
2823 * after SFDP has been parsed (is also called for SPI NORs that do not
2825 * @nor: pointer to a 'struct spi_nor'
2827 * Typically used to tweak various parameters that could not be extracted by
2828 * other means (i.e. when information provided by the SFDP/flash_info tables
2829 * are incomplete or wrong).
2831 static void spi_nor_post_sfdp_fixups(struct spi_nor *nor)
2833 if (nor->manufacturer && nor->manufacturer->fixups &&
2834 nor->manufacturer->fixups->post_sfdp)
2835 nor->manufacturer->fixups->post_sfdp(nor);
2837 if (nor->info->fixups && nor->info->fixups->post_sfdp)
2838 nor->info->fixups->post_sfdp(nor);
2842 * spi_nor_late_init_params() - Late initialization of default flash parameters.
2843 * @nor: pointer to a 'struct spi_nor'
2845 * Used to set default flash parameters and settings when the ->default_init()
2846 * hook or the SFDP parser let voids.
2848 static void spi_nor_late_init_params(struct spi_nor *nor)
2851 * NOR protection support. When locking_ops are not provided, we pick
2854 if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops)
2855 nor->params->locking_ops = &spi_nor_sr_locking_ops;
2859 * spi_nor_init_params() - Initialize the flash's parameters and settings.
2860 * @nor: pointer to a 'struct spi_nor'.
2862 * The flash parameters and settings are initialized based on a sequence of
2863 * calls that are ordered by priority:
2865 * 1/ Default flash parameters initialization. The initializations are done
2866 * based on nor->info data:
2867 * spi_nor_info_init_params()
2869 * which can be overwritten by:
2870 * 2/ Manufacturer flash parameters initialization. The initializations are
2871 * done based on MFR register, or when the decisions can not be done solely
2872 * based on MFR, by using specific flash_info tweeks, ->default_init():
2873 * spi_nor_manufacturer_init_params()
2875 * which can be overwritten by:
2876 * 3/ SFDP flash parameters initialization. JESD216 SFDP is a standard and
2877 * should be more accurate that the above.
2878 * spi_nor_sfdp_init_params()
2880 * Please note that there is a ->post_bfpt() fixup hook that can overwrite
2881 * the flash parameters and settings immediately after parsing the Basic
2882 * Flash Parameter Table.
2884 * which can be overwritten by:
2885 * 4/ Post SFDP flash parameters initialization. Used to tweak various
2886 * parameters that could not be extracted by other means (i.e. when
2887 * information provided by the SFDP/flash_info tables are incomplete or
2889 * spi_nor_post_sfdp_fixups()
2891 * 5/ Late default flash parameters initialization, used when the
2892 * ->default_init() hook or the SFDP parser do not set specific params.
2893 * spi_nor_late_init_params()
2895 static int spi_nor_init_params(struct spi_nor *nor)
2897 nor->params = devm_kzalloc(nor->dev, sizeof(*nor->params), GFP_KERNEL);
2901 spi_nor_info_init_params(nor);
2903 spi_nor_manufacturer_init_params(nor);
2905 if ((nor->info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
2906 !(nor->info->flags & SPI_NOR_SKIP_SFDP))
2907 spi_nor_sfdp_init_params(nor);
2909 spi_nor_post_sfdp_fixups(nor);
2911 spi_nor_late_init_params(nor);
2917 * spi_nor_quad_enable() - enable/disable Quad I/O if needed.
2918 * @nor: pointer to a 'struct spi_nor'
2919 * @enable: true to enable Quad mode. false to disable Quad mode.
2921 * Return: 0 on success, -errno otherwise.
2923 static int spi_nor_quad_enable(struct spi_nor *nor, bool enable)
2925 if (!nor->params->quad_enable)
2928 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2929 spi_nor_get_protocol_width(nor->write_proto) == 4))
2932 return nor->params->quad_enable(nor, enable);
2936 * spi_nor_unlock_all() - Unlocks the entire flash memory array.
2937 * @nor: pointer to a 'struct spi_nor'.
2939 * Some SPI NOR flashes are write protected by default after a power-on reset
2940 * cycle, in order to avoid inadvertent writes during power-up. Backward
2941 * compatibility imposes to unlock the entire flash memory array at power-up
2944 static int spi_nor_unlock_all(struct spi_nor *nor)
2946 if (nor->flags & SNOR_F_HAS_LOCK)
2947 return spi_nor_unlock(&nor->mtd, 0, nor->params->size);
2952 static int spi_nor_init(struct spi_nor *nor)
2956 err = spi_nor_quad_enable(nor, true);
2958 dev_dbg(nor->dev, "quad mode not supported\n");
2962 err = spi_nor_unlock_all(nor);
2964 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
2968 if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES)) {
2970 * If the RESET# pin isn't hooked up properly, or the system
2971 * otherwise doesn't perform a reset command in the boot
2972 * sequence, it's impossible to 100% protect against unexpected
2973 * reboots (e.g., crashes). Warn the user (or hopefully, system
2974 * designer) that this is bad.
2976 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
2977 "enabling reset hack; may not recover from unexpected reboots\n");
2978 nor->params->set_4byte_addr_mode(nor, true);
2984 /* mtd resume handler */
2985 static void spi_nor_resume(struct mtd_info *mtd)
2987 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2988 struct device *dev = nor->dev;
2991 /* re-initialize the nor chip */
2992 ret = spi_nor_init(nor);
2994 dev_err(dev, "resume() failed\n");
2997 void spi_nor_restore(struct spi_nor *nor)
2999 /* restore the addressing mode */
3000 if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES) &&
3001 nor->flags & SNOR_F_BROKEN_RESET)
3002 nor->params->set_4byte_addr_mode(nor, false);
3004 spi_nor_quad_enable(nor, false);
3006 EXPORT_SYMBOL_GPL(spi_nor_restore);
3008 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
3013 for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
3014 for (j = 0; j < manufacturers[i]->nparts; j++) {
3015 if (!strcmp(name, manufacturers[i]->parts[j].name)) {
3016 nor->manufacturer = manufacturers[i];
3017 return &manufacturers[i]->parts[j];
3025 static int spi_nor_set_addr_width(struct spi_nor *nor)
3027 if (nor->addr_width) {
3028 /* already configured from SFDP */
3029 } else if (nor->info->addr_width) {
3030 nor->addr_width = nor->info->addr_width;
3031 } else if (nor->mtd.size > 0x1000000) {
3032 /* enable 4-byte addressing if the device exceeds 16MiB */
3033 nor->addr_width = 4;
3035 nor->addr_width = 3;
3038 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
3039 dev_dbg(nor->dev, "address width is too large: %u\n",
3044 /* Set 4byte opcodes when possible. */
3045 if (nor->addr_width == 4 && nor->flags & SNOR_F_4B_OPCODES &&
3046 !(nor->flags & SNOR_F_HAS_4BAIT))
3047 spi_nor_set_4byte_opcodes(nor);
3052 static void spi_nor_debugfs_init(struct spi_nor *nor,
3053 const struct flash_info *info)
3055 struct mtd_info *mtd = &nor->mtd;
3057 mtd->dbg.partname = info->name;
3058 mtd->dbg.partid = devm_kasprintf(nor->dev, GFP_KERNEL, "spi-nor:%*phN",
3059 info->id_len, info->id);
3062 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
3065 const struct flash_info *info = NULL;
3068 info = spi_nor_match_id(nor, name);
3069 /* Try to auto-detect if chip name wasn't specified or not found */
3071 info = spi_nor_read_id(nor);
3072 if (IS_ERR_OR_NULL(info))
3073 return ERR_PTR(-ENOENT);
3076 * If caller has specified name of flash model that can normally be
3077 * detected using JEDEC, let's verify it.
3079 if (name && info->id_len) {
3080 const struct flash_info *jinfo;
3082 jinfo = spi_nor_read_id(nor);
3083 if (IS_ERR(jinfo)) {
3085 } else if (jinfo != info) {
3087 * JEDEC knows better, so overwrite platform ID. We
3088 * can't trust partitions any longer, but we'll let
3089 * mtd apply them anyway, since some partitions may be
3090 * marked read-only, and we don't want to lose that
3091 * information, even if it's not 100% accurate.
3093 dev_warn(nor->dev, "found %s, expected %s\n",
3094 jinfo->name, info->name);
3102 int spi_nor_scan(struct spi_nor *nor, const char *name,
3103 const struct spi_nor_hwcaps *hwcaps)
3105 const struct flash_info *info;
3106 struct device *dev = nor->dev;
3107 struct mtd_info *mtd = &nor->mtd;
3108 struct device_node *np = spi_nor_get_flash_node(nor);
3112 ret = spi_nor_check(nor);
3116 /* Reset SPI protocol for all commands. */
3117 nor->reg_proto = SNOR_PROTO_1_1_1;
3118 nor->read_proto = SNOR_PROTO_1_1_1;
3119 nor->write_proto = SNOR_PROTO_1_1_1;
3122 * We need the bounce buffer early to read/write registers when going
3123 * through the spi-mem layer (buffers have to be DMA-able).
3124 * For spi-mem drivers, we'll reallocate a new buffer if
3125 * nor->page_size turns out to be greater than PAGE_SIZE (which
3126 * shouldn't happen before long since NOR pages are usually less
3127 * than 1KB) after spi_nor_scan() returns.
3129 nor->bouncebuf_size = PAGE_SIZE;
3130 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
3132 if (!nor->bouncebuf)
3135 info = spi_nor_get_flash_info(nor, name);
3137 return PTR_ERR(info);
3141 spi_nor_debugfs_init(nor, info);
3143 mutex_init(&nor->lock);
3146 * Make sure the XSR_RDY flag is set before calling
3147 * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
3148 * with Atmel SPI NOR.
3150 if (info->flags & SPI_NOR_XSR_RDY)
3151 nor->flags |= SNOR_F_READY_XSR_RDY;
3153 if (info->flags & SPI_NOR_HAS_LOCK)
3154 nor->flags |= SNOR_F_HAS_LOCK;
3156 mtd->_write = spi_nor_write;
3158 /* Init flash parameters based on flash_info struct and SFDP */
3159 ret = spi_nor_init_params(nor);
3164 mtd->name = dev_name(dev);
3166 mtd->type = MTD_NORFLASH;
3168 mtd->flags = MTD_CAP_NORFLASH;
3169 mtd->size = nor->params->size;
3170 mtd->_erase = spi_nor_erase;
3171 mtd->_read = spi_nor_read;
3172 mtd->_resume = spi_nor_resume;
3174 if (nor->params->locking_ops) {
3175 mtd->_lock = spi_nor_lock;
3176 mtd->_unlock = spi_nor_unlock;
3177 mtd->_is_locked = spi_nor_is_locked;
3180 if (info->flags & USE_FSR)
3181 nor->flags |= SNOR_F_USE_FSR;
3182 if (info->flags & SPI_NOR_HAS_TB) {
3183 nor->flags |= SNOR_F_HAS_SR_TB;
3184 if (info->flags & SPI_NOR_TB_SR_BIT6)
3185 nor->flags |= SNOR_F_HAS_SR_TB_BIT6;
3188 if (info->flags & NO_CHIP_ERASE)
3189 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
3190 if (info->flags & USE_CLSR)
3191 nor->flags |= SNOR_F_USE_CLSR;
3193 if (info->flags & SPI_NOR_4BIT_BP) {
3194 nor->flags |= SNOR_F_HAS_4BIT_BP;
3195 if (info->flags & SPI_NOR_BP3_SR_BIT6)
3196 nor->flags |= SNOR_F_HAS_SR_BP3_BIT6;
3199 if (info->flags & SPI_NOR_NO_ERASE)
3200 mtd->flags |= MTD_NO_ERASE;
3202 mtd->dev.parent = dev;
3203 nor->page_size = nor->params->page_size;
3204 mtd->writebufsize = nor->page_size;
3206 if (of_property_read_bool(np, "broken-flash-reset"))
3207 nor->flags |= SNOR_F_BROKEN_RESET;
3210 * Configure the SPI memory:
3211 * - select op codes for (Fast) Read, Page Program and Sector Erase.
3212 * - set the number of dummy cycles (mode cycles + wait states).
3213 * - set the SPI protocols for register and memory accesses.
3215 ret = spi_nor_setup(nor, hwcaps);
3219 if (info->flags & SPI_NOR_4B_OPCODES)
3220 nor->flags |= SNOR_F_4B_OPCODES;
3222 ret = spi_nor_set_addr_width(nor);
3226 /* Send all the required SPI flash commands to initialize device */
3227 ret = spi_nor_init(nor);
3231 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
3232 (long long)mtd->size >> 10);
3235 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
3236 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
3237 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
3238 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
3240 if (mtd->numeraseregions)
3241 for (i = 0; i < mtd->numeraseregions; i++)
3243 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
3244 ".erasesize = 0x%.8x (%uKiB), "
3245 ".numblocks = %d }\n",
3246 i, (long long)mtd->eraseregions[i].offset,
3247 mtd->eraseregions[i].erasesize,
3248 mtd->eraseregions[i].erasesize / 1024,
3249 mtd->eraseregions[i].numblocks);
3252 EXPORT_SYMBOL_GPL(spi_nor_scan);
3254 static int spi_nor_create_read_dirmap(struct spi_nor *nor)
3256 struct spi_mem_dirmap_info info = {
3257 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
3258 SPI_MEM_OP_ADDR(nor->addr_width, 0, 1),
3259 SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
3260 SPI_MEM_OP_DATA_IN(0, NULL, 1)),
3262 .length = nor->mtd.size,
3264 struct spi_mem_op *op = &info.op_tmpl;
3266 /* get transfer protocols. */
3267 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
3268 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
3269 op->dummy.buswidth = op->addr.buswidth;
3270 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
3272 /* convert the dummy cycles to the number of bytes */
3273 op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8;
3275 nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3277 return PTR_ERR_OR_ZERO(nor->dirmap.rdesc);
3280 static int spi_nor_create_write_dirmap(struct spi_nor *nor)
3282 struct spi_mem_dirmap_info info = {
3283 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
3284 SPI_MEM_OP_ADDR(nor->addr_width, 0, 1),
3285 SPI_MEM_OP_NO_DUMMY,
3286 SPI_MEM_OP_DATA_OUT(0, NULL, 1)),
3288 .length = nor->mtd.size,
3290 struct spi_mem_op *op = &info.op_tmpl;
3292 /* get transfer protocols. */
3293 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
3294 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
3295 op->dummy.buswidth = op->addr.buswidth;
3296 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
3298 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
3299 op->addr.nbytes = 0;
3301 nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3303 return PTR_ERR_OR_ZERO(nor->dirmap.wdesc);
3306 static int spi_nor_probe(struct spi_mem *spimem)
3308 struct spi_device *spi = spimem->spi;
3309 struct flash_platform_data *data = dev_get_platdata(&spi->dev);
3310 struct spi_nor *nor;
3312 * Enable all caps by default. The core will mask them after
3313 * checking what's really supported using spi_mem_supports_op().
3315 const struct spi_nor_hwcaps hwcaps = { .mask = SNOR_HWCAPS_ALL };
3319 nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL);
3323 nor->spimem = spimem;
3324 nor->dev = &spi->dev;
3325 spi_nor_set_flash_node(nor, spi->dev.of_node);
3327 spi_mem_set_drvdata(spimem, nor);
3329 if (data && data->name)
3330 nor->mtd.name = data->name;
3333 nor->mtd.name = spi_mem_get_name(spimem);
3336 * For some (historical?) reason many platforms provide two different
3337 * names in flash_platform_data: "name" and "type". Quite often name is
3338 * set to "m25p80" and then "type" provides a real chip name.
3339 * If that's the case, respect "type" and ignore a "name".
3341 if (data && data->type)
3342 flash_name = data->type;
3343 else if (!strcmp(spi->modalias, "spi-nor"))
3344 flash_name = NULL; /* auto-detect */
3346 flash_name = spi->modalias;
3348 ret = spi_nor_scan(nor, flash_name, &hwcaps);
3353 * None of the existing parts have > 512B pages, but let's play safe
3354 * and add this logic so that if anyone ever adds support for such
3355 * a NOR we don't end up with buffer overflows.
3357 if (nor->page_size > PAGE_SIZE) {
3358 nor->bouncebuf_size = nor->page_size;
3359 devm_kfree(nor->dev, nor->bouncebuf);
3360 nor->bouncebuf = devm_kmalloc(nor->dev,
3361 nor->bouncebuf_size,
3363 if (!nor->bouncebuf)
3367 ret = spi_nor_create_read_dirmap(nor);
3371 ret = spi_nor_create_write_dirmap(nor);
3375 return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
3376 data ? data->nr_parts : 0);
3379 static int spi_nor_remove(struct spi_mem *spimem)
3381 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3383 spi_nor_restore(nor);
3385 /* Clean up MTD stuff. */
3386 return mtd_device_unregister(&nor->mtd);
3389 static void spi_nor_shutdown(struct spi_mem *spimem)
3391 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3393 spi_nor_restore(nor);
3397 * Do NOT add to this array without reading the following:
3399 * Historically, many flash devices are bound to this driver by their name. But
3400 * since most of these flash are compatible to some extent, and their
3401 * differences can often be differentiated by the JEDEC read-ID command, we
3402 * encourage new users to add support to the spi-nor library, and simply bind
3403 * against a generic string here (e.g., "jedec,spi-nor").
3405 * Many flash names are kept here in this list (as well as in spi-nor.c) to
3406 * keep them available as module aliases for existing platforms.
3408 static const struct spi_device_id spi_nor_dev_ids[] = {
3410 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
3411 * hack around the fact that the SPI core does not provide uevent
3412 * matching for .of_match_table
3417 * Entries not used in DTs that should be safe to drop after replacing
3418 * them with "spi-nor" in platform data.
3420 {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
3423 * Entries that were used in DTs without "jedec,spi-nor" fallback and
3424 * should be kept for backward compatibility.
3426 {"at25df321a"}, {"at25df641"}, {"at26df081a"},
3427 {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"},
3428 {"mx25l25635e"},{"mx66l51235l"},
3429 {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"},
3430 {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"},
3432 {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
3433 {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"},
3434 {"m25p64"}, {"m25p128"},
3435 {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
3436 {"w25q80bl"}, {"w25q128"}, {"w25q256"},
3438 /* Flashes that can't be detected using JEDEC */
3439 {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
3440 {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
3441 {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
3443 /* Everspin MRAMs (non-JEDEC) */
3444 { "mr25h128" }, /* 128 Kib, 40 MHz */
3445 { "mr25h256" }, /* 256 Kib, 40 MHz */
3446 { "mr25h10" }, /* 1 Mib, 40 MHz */
3447 { "mr25h40" }, /* 4 Mib, 40 MHz */
3451 MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids);
3453 static const struct of_device_id spi_nor_of_table[] = {
3455 * Generic compatibility for SPI NOR that can be identified by the
3456 * JEDEC READ ID opcode (0x9F). Use this, if possible.
3458 { .compatible = "jedec,spi-nor" },
3461 MODULE_DEVICE_TABLE(of, spi_nor_of_table);
3464 * REVISIT: many of these chips have deep power-down modes, which
3465 * should clearly be entered on suspend() to minimize power use.
3466 * And also when they're otherwise idle...
3468 static struct spi_mem_driver spi_nor_driver = {
3472 .of_match_table = spi_nor_of_table,
3474 .id_table = spi_nor_dev_ids,
3476 .probe = spi_nor_probe,
3477 .remove = spi_nor_remove,
3478 .shutdown = spi_nor_shutdown,
3480 module_spi_mem_driver(spi_nor_driver);
3482 MODULE_LICENSE("GPL v2");
3484 MODULE_AUTHOR("Mike Lavender");
3485 MODULE_DESCRIPTION("framework for SPI NOR");