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.
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
43 #define JEDEC_MFR(info) ((info)->id[0])
46 * spi_nor_spimem_bounce() - check if a bounce buffer is needed for the data
48 * @nor: pointer to 'struct spi_nor'
49 * @op: pointer to 'struct spi_mem_op' template for transfer
51 * If we have to use the bounce buffer, the data field in @op will be updated.
53 * Return: true if the bounce buffer is needed, false if not
55 static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op)
57 /* op->data.buf.in occupies the same memory as op->data.buf.out */
58 if (object_is_on_stack(op->data.buf.in) ||
59 !virt_addr_valid(op->data.buf.in)) {
60 if (op->data.nbytes > nor->bouncebuf_size)
61 op->data.nbytes = nor->bouncebuf_size;
62 op->data.buf.in = nor->bouncebuf;
70 * spi_nor_spimem_exec_op() - execute a memory operation
71 * @nor: pointer to 'struct spi_nor'
72 * @op: pointer to 'struct spi_mem_op' template for transfer
74 * Return: 0 on success, -error otherwise.
76 static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op)
80 error = spi_mem_adjust_op_size(nor->spimem, op);
84 return spi_mem_exec_op(nor->spimem, op);
88 * spi_nor_spimem_read_data() - read data from flash's memory region via
90 * @nor: pointer to 'struct spi_nor'
91 * @from: offset to read from
92 * @len: number of bytes to read
93 * @buf: pointer to dst buffer
95 * Return: number of bytes read successfully, -errno otherwise
97 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from,
100 struct spi_mem_op op =
101 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
102 SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
103 SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
104 SPI_MEM_OP_DATA_IN(len, buf, 1));
109 /* get transfer protocols. */
110 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
111 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
112 op.dummy.buswidth = op.addr.buswidth;
113 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
115 /* convert the dummy cycles to the number of bytes */
116 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
118 usebouncebuf = spi_nor_spimem_bounce(nor, &op);
120 if (nor->dirmap.rdesc) {
121 nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val,
122 op.data.nbytes, op.data.buf.in);
124 error = spi_nor_spimem_exec_op(nor, &op);
127 nbytes = op.data.nbytes;
130 if (usebouncebuf && nbytes > 0)
131 memcpy(buf, op.data.buf.in, nbytes);
137 * spi_nor_read_data() - read data from flash memory
138 * @nor: pointer to 'struct spi_nor'
139 * @from: offset to read from
140 * @len: number of bytes to read
141 * @buf: pointer to dst buffer
143 * Return: number of bytes read successfully, -errno otherwise
145 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf)
148 return spi_nor_spimem_read_data(nor, from, len, buf);
150 return nor->controller_ops->read(nor, from, len, buf);
154 * spi_nor_spimem_write_data() - write data to flash memory via
156 * @nor: pointer to 'struct spi_nor'
157 * @to: offset to write to
158 * @len: number of bytes to write
159 * @buf: pointer to src buffer
161 * Return: number of bytes written successfully, -errno otherwise
163 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to,
164 size_t len, const u8 *buf)
166 struct spi_mem_op op =
167 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
168 SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
170 SPI_MEM_OP_DATA_OUT(len, buf, 1));
174 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
175 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
176 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
178 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
181 if (spi_nor_spimem_bounce(nor, &op))
182 memcpy(nor->bouncebuf, buf, op.data.nbytes);
184 if (nor->dirmap.wdesc) {
185 nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val,
186 op.data.nbytes, op.data.buf.out);
188 error = spi_nor_spimem_exec_op(nor, &op);
191 nbytes = op.data.nbytes;
198 * spi_nor_write_data() - write data to flash memory
199 * @nor: pointer to 'struct spi_nor'
200 * @to: offset to write to
201 * @len: number of bytes to write
202 * @buf: pointer to src buffer
204 * Return: number of bytes written successfully, -errno otherwise
206 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
210 return spi_nor_spimem_write_data(nor, to, len, buf);
212 return nor->controller_ops->write(nor, to, len, buf);
216 * spi_nor_write_enable() - Set write enable latch with Write Enable command.
217 * @nor: pointer to 'struct spi_nor'.
219 * Return: 0 on success, -errno otherwise.
221 int spi_nor_write_enable(struct spi_nor *nor)
226 struct spi_mem_op op =
227 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREN, 1),
232 ret = spi_mem_exec_op(nor->spimem, &op);
234 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WREN,
239 dev_dbg(nor->dev, "error %d on Write Enable\n", ret);
245 * spi_nor_write_disable() - Send Write Disable instruction to the chip.
246 * @nor: pointer to 'struct spi_nor'.
248 * Return: 0 on success, -errno otherwise.
250 int spi_nor_write_disable(struct spi_nor *nor)
255 struct spi_mem_op op =
256 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRDI, 1),
261 ret = spi_mem_exec_op(nor->spimem, &op);
263 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRDI,
268 dev_dbg(nor->dev, "error %d on Write Disable\n", ret);
274 * spi_nor_read_sr() - Read the Status Register.
275 * @nor: pointer to 'struct spi_nor'.
276 * @sr: pointer to a DMA-able buffer where the value of the
277 * Status Register will be written.
279 * Return: 0 on success, -errno otherwise.
281 static int spi_nor_read_sr(struct spi_nor *nor, u8 *sr)
286 struct spi_mem_op op =
287 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 1),
290 SPI_MEM_OP_DATA_IN(1, sr, 1));
292 ret = spi_mem_exec_op(nor->spimem, &op);
294 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDSR,
299 dev_dbg(nor->dev, "error %d reading SR\n", ret);
305 * spi_nor_read_fsr() - Read the Flag Status Register.
306 * @nor: pointer to 'struct spi_nor'
307 * @fsr: pointer to a DMA-able buffer where the value of the
308 * Flag Status Register will be written.
310 * Return: 0 on success, -errno otherwise.
312 static int spi_nor_read_fsr(struct spi_nor *nor, u8 *fsr)
317 struct spi_mem_op op =
318 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 1),
321 SPI_MEM_OP_DATA_IN(1, fsr, 1));
323 ret = spi_mem_exec_op(nor->spimem, &op);
325 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDFSR,
330 dev_dbg(nor->dev, "error %d reading FSR\n", ret);
336 * spi_nor_read_cr() - Read the Configuration Register using the
337 * SPINOR_OP_RDCR (35h) command.
338 * @nor: pointer to 'struct spi_nor'
339 * @cr: pointer to a DMA-able buffer where the value of the
340 * Configuration Register will be written.
342 * Return: 0 on success, -errno otherwise.
344 static int spi_nor_read_cr(struct spi_nor *nor, u8 *cr)
349 struct spi_mem_op op =
350 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDCR, 1),
353 SPI_MEM_OP_DATA_IN(1, cr, 1));
355 ret = spi_mem_exec_op(nor->spimem, &op);
357 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDCR, cr, 1);
361 dev_dbg(nor->dev, "error %d reading CR\n", ret);
367 * spi_nor_set_4byte_addr_mode() - Enter/Exit 4-byte address mode.
368 * @nor: pointer to 'struct spi_nor'.
369 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
372 * Return: 0 on success, -errno otherwise.
374 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
379 struct spi_mem_op op =
380 SPI_MEM_OP(SPI_MEM_OP_CMD(enable ?
388 ret = spi_mem_exec_op(nor->spimem, &op);
390 ret = nor->controller_ops->write_reg(nor,
391 enable ? SPINOR_OP_EN4B :
397 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
403 * st_micron_set_4byte_addr_mode() - Set 4-byte address mode for ST and Micron
405 * @nor: pointer to 'struct spi_nor'.
406 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
409 * Return: 0 on success, -errno otherwise.
411 static int st_micron_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
415 ret = spi_nor_write_enable(nor);
419 ret = spi_nor_set_4byte_addr_mode(nor, enable);
423 return spi_nor_write_disable(nor);
427 * spansion_set_4byte_addr_mode() - Set 4-byte address mode for Spansion
429 * @nor: pointer to 'struct spi_nor'.
430 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
433 * Return: 0 on success, -errno otherwise.
435 static int spansion_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
439 nor->bouncebuf[0] = enable << 7;
442 struct spi_mem_op op =
443 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_BRWR, 1),
446 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1));
448 ret = spi_mem_exec_op(nor->spimem, &op);
450 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_BRWR,
455 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
461 * spi_nor_write_ear() - Write Extended Address Register.
462 * @nor: pointer to 'struct spi_nor'.
463 * @ear: value to write to the Extended Address Register.
465 * Return: 0 on success, -errno otherwise.
467 int spi_nor_write_ear(struct spi_nor *nor, u8 ear)
471 nor->bouncebuf[0] = ear;
474 struct spi_mem_op op =
475 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREAR, 1),
478 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1));
480 ret = spi_mem_exec_op(nor->spimem, &op);
482 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WREAR,
487 dev_dbg(nor->dev, "error %d writing EAR\n", ret);
493 * winbond_set_4byte_addr_mode() - Set 4-byte address mode for Winbond flashes.
494 * @nor: pointer to 'struct spi_nor'.
495 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
498 * Return: 0 on success, -errno otherwise.
500 static int winbond_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
504 ret = spi_nor_set_4byte_addr_mode(nor, enable);
509 * On Winbond W25Q256FV, leaving 4byte mode causes the Extended Address
510 * Register to be set to 1, so all 3-byte-address reads come from the
511 * second 16M. We must clear the register to enable normal behavior.
513 ret = spi_nor_write_enable(nor);
517 ret = spi_nor_write_ear(nor, 0);
521 return spi_nor_write_disable(nor);
525 * spi_nor_xread_sr() - Read the Status Register on S3AN flashes.
526 * @nor: pointer to 'struct spi_nor'.
527 * @sr: pointer to a DMA-able buffer where the value of the
528 * Status Register will be written.
530 * Return: 0 on success, -errno otherwise.
532 int spi_nor_xread_sr(struct spi_nor *nor, u8 *sr)
537 struct spi_mem_op op =
538 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_XRDSR, 1),
541 SPI_MEM_OP_DATA_IN(1, sr, 1));
543 ret = spi_mem_exec_op(nor->spimem, &op);
545 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_XRDSR,
550 dev_dbg(nor->dev, "error %d reading XRDSR\n", ret);
556 * spi_nor_xsr_ready() - Query the Status Register of the S3AN flash to see if
557 * the flash is ready for new commands.
558 * @nor: pointer to 'struct spi_nor'.
560 * Return: 0 on success, -errno otherwise.
562 static int spi_nor_xsr_ready(struct spi_nor *nor)
566 ret = spi_nor_xread_sr(nor, nor->bouncebuf);
570 return !!(nor->bouncebuf[0] & XSR_RDY);
574 * spi_nor_clear_sr() - Clear the Status Register.
575 * @nor: pointer to 'struct spi_nor'.
577 static void spi_nor_clear_sr(struct spi_nor *nor)
582 struct spi_mem_op op =
583 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLSR, 1),
588 ret = spi_mem_exec_op(nor->spimem, &op);
590 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CLSR,
595 dev_dbg(nor->dev, "error %d clearing SR\n", ret);
599 * spi_nor_sr_ready() - Query the Status Register to see if the flash is ready
601 * @nor: pointer to 'struct spi_nor'.
603 * Return: 0 on success, -errno otherwise.
605 static int spi_nor_sr_ready(struct spi_nor *nor)
607 int ret = spi_nor_read_sr(nor, nor->bouncebuf);
612 if (nor->flags & SNOR_F_USE_CLSR &&
613 nor->bouncebuf[0] & (SR_E_ERR | SR_P_ERR)) {
614 if (nor->bouncebuf[0] & SR_E_ERR)
615 dev_err(nor->dev, "Erase Error occurred\n");
617 dev_err(nor->dev, "Programming Error occurred\n");
619 spi_nor_clear_sr(nor);
623 return !(nor->bouncebuf[0] & SR_WIP);
627 * spi_nor_clear_fsr() - Clear the Flag Status Register.
628 * @nor: pointer to 'struct spi_nor'.
630 static void spi_nor_clear_fsr(struct spi_nor *nor)
635 struct spi_mem_op op =
636 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLFSR, 1),
641 ret = spi_mem_exec_op(nor->spimem, &op);
643 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CLFSR,
648 dev_dbg(nor->dev, "error %d clearing FSR\n", ret);
652 * spi_nor_fsr_ready() - Query the Flag Status Register to see if the flash is
653 * ready for new commands.
654 * @nor: pointer to 'struct spi_nor'.
656 * Return: 0 on success, -errno otherwise.
658 static int spi_nor_fsr_ready(struct spi_nor *nor)
660 int ret = spi_nor_read_fsr(nor, nor->bouncebuf);
665 if (nor->bouncebuf[0] & (FSR_E_ERR | FSR_P_ERR)) {
666 if (nor->bouncebuf[0] & FSR_E_ERR)
667 dev_err(nor->dev, "Erase operation failed.\n");
669 dev_err(nor->dev, "Program operation failed.\n");
671 if (nor->bouncebuf[0] & FSR_PT_ERR)
673 "Attempted to modify a protected sector.\n");
675 spi_nor_clear_fsr(nor);
679 return nor->bouncebuf[0] & FSR_READY;
683 * spi_nor_ready() - Query the flash to see if it is ready for new commands.
684 * @nor: pointer to 'struct spi_nor'.
686 * Return: 0 on success, -errno otherwise.
688 static int spi_nor_ready(struct spi_nor *nor)
692 if (nor->flags & SNOR_F_READY_XSR_RDY)
693 sr = spi_nor_xsr_ready(nor);
695 sr = spi_nor_sr_ready(nor);
698 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
705 * spi_nor_wait_till_ready_with_timeout() - Service routine to read the
706 * Status Register until ready, or timeout occurs.
707 * @nor: pointer to "struct spi_nor".
708 * @timeout_jiffies: jiffies to wait until timeout.
710 * Return: 0 on success, -errno otherwise.
712 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
713 unsigned long timeout_jiffies)
715 unsigned long deadline;
716 int timeout = 0, ret;
718 deadline = jiffies + timeout_jiffies;
721 if (time_after_eq(jiffies, deadline))
724 ret = spi_nor_ready(nor);
733 dev_dbg(nor->dev, "flash operation timed out\n");
739 * spi_nor_wait_till_ready() - Wait for a predefined amount of time for the
740 * flash to be ready, or timeout occurs.
741 * @nor: pointer to "struct spi_nor".
743 * Return: 0 on success, -errno otherwise.
745 int spi_nor_wait_till_ready(struct spi_nor *nor)
747 return spi_nor_wait_till_ready_with_timeout(nor,
748 DEFAULT_READY_WAIT_JIFFIES);
752 * spi_nor_write_sr() - Write the Status Register.
753 * @nor: pointer to 'struct spi_nor'.
754 * @sr: pointer to DMA-able buffer to write to the Status Register.
755 * @len: number of bytes to write to the Status Register.
757 * Return: 0 on success, -errno otherwise.
759 static int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len)
763 ret = spi_nor_write_enable(nor);
768 struct spi_mem_op op =
769 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR, 1),
772 SPI_MEM_OP_DATA_OUT(len, sr, 1));
774 ret = spi_mem_exec_op(nor->spimem, &op);
776 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRSR,
781 dev_dbg(nor->dev, "error %d writing SR\n", ret);
785 return spi_nor_wait_till_ready(nor);
789 * spi_nor_write_sr1_and_check() - Write one byte to the Status Register 1 and
790 * ensure that the byte written match the received value.
791 * @nor: pointer to a 'struct spi_nor'.
792 * @sr1: byte value to be written to the Status Register.
794 * Return: 0 on success, -errno otherwise.
796 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1)
800 nor->bouncebuf[0] = sr1;
802 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1);
806 ret = spi_nor_read_sr(nor, nor->bouncebuf);
810 if (nor->bouncebuf[0] != sr1) {
811 dev_dbg(nor->dev, "SR1: read back test failed\n");
819 * spi_nor_write_16bit_sr_and_check() - Write the Status Register 1 and the
820 * Status Register 2 in one shot. Ensure that the byte written in the Status
821 * Register 1 match the received value, and that the 16-bit Write did not
822 * affect what was already in the Status Register 2.
823 * @nor: pointer to a 'struct spi_nor'.
824 * @sr1: byte value to be written to the Status Register 1.
826 * Return: 0 on success, -errno otherwise.
828 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1)
831 u8 *sr_cr = nor->bouncebuf;
834 /* Make sure we don't overwrite the contents of Status Register 2. */
835 if (!(nor->flags & SNOR_F_NO_READ_CR)) {
836 ret = spi_nor_read_cr(nor, &sr_cr[1]);
839 } else if (nor->params.quad_enable) {
841 * If the Status Register 2 Read command (35h) is not
842 * supported, we should at least be sure we don't
843 * change the value of the SR2 Quad Enable bit.
845 * We can safely assume that when the Quad Enable method is
846 * set, the value of the QE bit is one, as a consequence of the
847 * nor->params.quad_enable() call.
849 * We can safely assume that the Quad Enable bit is present in
850 * the Status Register 2 at BIT(1). According to the JESD216
851 * revB standard, BFPT DWORDS[15], bits 22:20, the 16-bit
852 * Write Status (01h) command is available just for the cases
853 * in which the QE bit is described in SR2 at BIT(1).
855 sr_cr[1] = SR2_QUAD_EN_BIT1;
862 ret = spi_nor_write_sr(nor, sr_cr, 2);
866 if (nor->flags & SNOR_F_NO_READ_CR)
869 cr_written = sr_cr[1];
871 ret = spi_nor_read_cr(nor, &sr_cr[1]);
875 if (cr_written != sr_cr[1]) {
876 dev_dbg(nor->dev, "CR: read back test failed\n");
884 * spi_nor_write_16bit_cr_and_check() - Write the Status Register 1 and the
885 * Configuration Register in one shot. Ensure that the byte written in the
886 * Configuration Register match the received value, and that the 16-bit Write
887 * did not affect what was already in the Status Register 1.
888 * @nor: pointer to a 'struct spi_nor'.
889 * @cr: byte value to be written to the Configuration Register.
891 * Return: 0 on success, -errno otherwise.
893 static int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr)
896 u8 *sr_cr = nor->bouncebuf;
899 /* Keep the current value of the Status Register 1. */
900 ret = spi_nor_read_sr(nor, sr_cr);
906 ret = spi_nor_write_sr(nor, sr_cr, 2);
910 sr_written = sr_cr[0];
912 ret = spi_nor_read_sr(nor, sr_cr);
916 if (sr_written != sr_cr[0]) {
917 dev_dbg(nor->dev, "SR: Read back test failed\n");
921 if (nor->flags & SNOR_F_NO_READ_CR)
924 ret = spi_nor_read_cr(nor, &sr_cr[1]);
928 if (cr != sr_cr[1]) {
929 dev_dbg(nor->dev, "CR: read back test failed\n");
937 * spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that
938 * the byte written match the received value without affecting other bits in the
939 * Status Register 1 and 2.
940 * @nor: pointer to a 'struct spi_nor'.
941 * @sr1: byte value to be written to the Status Register.
943 * Return: 0 on success, -errno otherwise.
945 static int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1)
947 if (nor->flags & SNOR_F_HAS_16BIT_SR)
948 return spi_nor_write_16bit_sr_and_check(nor, sr1);
950 return spi_nor_write_sr1_and_check(nor, sr1);
954 * spi_nor_write_sr2() - Write the Status Register 2 using the
955 * SPINOR_OP_WRSR2 (3eh) command.
956 * @nor: pointer to 'struct spi_nor'.
957 * @sr2: pointer to DMA-able buffer to write to the Status Register 2.
959 * Return: 0 on success, -errno otherwise.
961 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2)
965 ret = spi_nor_write_enable(nor);
970 struct spi_mem_op op =
971 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR2, 1),
974 SPI_MEM_OP_DATA_OUT(1, sr2, 1));
976 ret = spi_mem_exec_op(nor->spimem, &op);
978 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRSR2,
983 dev_dbg(nor->dev, "error %d writing SR2\n", ret);
987 return spi_nor_wait_till_ready(nor);
991 * spi_nor_read_sr2() - Read the Status Register 2 using the
992 * SPINOR_OP_RDSR2 (3fh) command.
993 * @nor: pointer to 'struct spi_nor'.
994 * @sr2: pointer to DMA-able buffer where the value of the
995 * Status Register 2 will be written.
997 * Return: 0 on success, -errno otherwise.
999 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2)
1004 struct spi_mem_op op =
1005 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR2, 1),
1007 SPI_MEM_OP_NO_DUMMY,
1008 SPI_MEM_OP_DATA_IN(1, sr2, 1));
1010 ret = spi_mem_exec_op(nor->spimem, &op);
1012 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDSR2,
1017 dev_dbg(nor->dev, "error %d reading SR2\n", ret);
1023 * spi_nor_erase_chip() - Erase the entire flash memory.
1024 * @nor: pointer to 'struct spi_nor'.
1026 * Return: 0 on success, -errno otherwise.
1028 static int spi_nor_erase_chip(struct spi_nor *nor)
1032 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
1035 struct spi_mem_op op =
1036 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CHIP_ERASE, 1),
1038 SPI_MEM_OP_NO_DUMMY,
1039 SPI_MEM_OP_NO_DATA);
1041 ret = spi_mem_exec_op(nor->spimem, &op);
1043 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CHIP_ERASE,
1048 dev_dbg(nor->dev, "error %d erasing chip\n", ret);
1053 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
1057 for (i = 0; i < size; i++)
1058 if (table[i][0] == opcode)
1061 /* No conversion found, keep input op code. */
1065 u8 spi_nor_convert_3to4_read(u8 opcode)
1067 static const u8 spi_nor_3to4_read[][2] = {
1068 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
1069 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
1070 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
1071 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
1072 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
1073 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
1074 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
1075 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
1077 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
1078 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
1079 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
1082 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
1083 ARRAY_SIZE(spi_nor_3to4_read));
1086 static u8 spi_nor_convert_3to4_program(u8 opcode)
1088 static const u8 spi_nor_3to4_program[][2] = {
1089 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
1090 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
1091 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
1092 { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B },
1093 { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B },
1096 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
1097 ARRAY_SIZE(spi_nor_3to4_program));
1100 static u8 spi_nor_convert_3to4_erase(u8 opcode)
1102 static const u8 spi_nor_3to4_erase[][2] = {
1103 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
1104 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
1105 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
1108 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
1109 ARRAY_SIZE(spi_nor_3to4_erase));
1112 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor)
1114 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
1115 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
1116 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
1118 if (!spi_nor_has_uniform_erase(nor)) {
1119 struct spi_nor_erase_map *map = &nor->params.erase_map;
1120 struct spi_nor_erase_type *erase;
1123 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1124 erase = &map->erase_type[i];
1126 spi_nor_convert_3to4_erase(erase->opcode);
1131 int spi_nor_lock_and_prep(struct spi_nor *nor)
1135 mutex_lock(&nor->lock);
1137 if (nor->controller_ops && nor->controller_ops->prepare) {
1138 ret = nor->controller_ops->prepare(nor);
1140 mutex_unlock(&nor->lock);
1147 void spi_nor_unlock_and_unprep(struct spi_nor *nor)
1149 if (nor->controller_ops && nor->controller_ops->unprepare)
1150 nor->controller_ops->unprepare(nor);
1151 mutex_unlock(&nor->lock);
1155 * This code converts an address to the Default Address Mode, that has non
1156 * power of two page sizes. We must support this mode because it is the default
1157 * mode supported by Xilinx tools, it can access the whole flash area and
1158 * changing over to the Power-of-two mode is irreversible and corrupts the
1160 * Addr can safely be unsigned int, the biggest S3AN device is smaller than
1163 static u32 s3an_convert_addr(struct spi_nor *nor, u32 addr)
1167 offset = addr % nor->page_size;
1168 page = addr / nor->page_size;
1169 page <<= (nor->page_size > 512) ? 10 : 9;
1171 return page | offset;
1174 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)
1176 if (!nor->params.convert_addr)
1179 return nor->params.convert_addr(nor, addr);
1183 * Initiate the erasure of a single sector
1185 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
1189 addr = spi_nor_convert_addr(nor, addr);
1192 struct spi_mem_op op =
1193 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1),
1194 SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
1195 SPI_MEM_OP_NO_DUMMY,
1196 SPI_MEM_OP_NO_DATA);
1198 return spi_mem_exec_op(nor->spimem, &op);
1199 } else if (nor->controller_ops->erase) {
1200 return nor->controller_ops->erase(nor, addr);
1204 * Default implementation, if driver doesn't have a specialized HW
1207 for (i = nor->addr_width - 1; i >= 0; i--) {
1208 nor->bouncebuf[i] = addr & 0xff;
1212 return nor->controller_ops->write_reg(nor, nor->erase_opcode,
1213 nor->bouncebuf, nor->addr_width);
1217 * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
1218 * @erase: pointer to a structure that describes a SPI NOR erase type
1219 * @dividend: dividend value
1220 * @remainder: pointer to u32 remainder (will be updated)
1222 * Return: the result of the division
1224 static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase,
1225 u64 dividend, u32 *remainder)
1227 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
1228 *remainder = (u32)dividend & erase->size_mask;
1229 return dividend >> erase->size_shift;
1233 * spi_nor_find_best_erase_type() - find the best erase type for the given
1234 * offset in the serial flash memory and the
1235 * number of bytes to erase. The region in
1236 * which the address fits is expected to be
1238 * @map: the erase map of the SPI NOR
1239 * @region: pointer to a structure that describes a SPI NOR erase region
1240 * @addr: offset in the serial flash memory
1241 * @len: number of bytes to erase
1243 * Return: a pointer to the best fitted erase type, NULL otherwise.
1245 static const struct spi_nor_erase_type *
1246 spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
1247 const struct spi_nor_erase_region *region,
1250 const struct spi_nor_erase_type *erase;
1253 u8 erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
1256 * Erase types are ordered by size, with the smallest erase type at
1259 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
1260 /* Does the erase region support the tested erase type? */
1261 if (!(erase_mask & BIT(i)))
1264 erase = &map->erase_type[i];
1266 /* Don't erase more than what the user has asked for. */
1267 if (erase->size > len)
1270 /* Alignment is not mandatory for overlaid regions */
1271 if (region->offset & SNOR_OVERLAID_REGION)
1274 spi_nor_div_by_erase_size(erase, addr, &rem);
1285 * spi_nor_region_next() - get the next spi nor region
1286 * @region: pointer to a structure that describes a SPI NOR erase region
1288 * Return: the next spi nor region or NULL if last region.
1290 struct spi_nor_erase_region *
1291 spi_nor_region_next(struct spi_nor_erase_region *region)
1293 if (spi_nor_region_is_last(region))
1300 * spi_nor_find_erase_region() - find the region of the serial flash memory in
1301 * which the offset fits
1302 * @map: the erase map of the SPI NOR
1303 * @addr: offset in the serial flash memory
1305 * Return: a pointer to the spi_nor_erase_region struct, ERR_PTR(-errno)
1308 static struct spi_nor_erase_region *
1309 spi_nor_find_erase_region(const struct spi_nor_erase_map *map, u64 addr)
1311 struct spi_nor_erase_region *region = map->regions;
1312 u64 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1313 u64 region_end = region_start + region->size;
1315 while (addr < region_start || addr >= region_end) {
1316 region = spi_nor_region_next(region);
1318 return ERR_PTR(-EINVAL);
1320 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1321 region_end = region_start + region->size;
1328 * spi_nor_init_erase_cmd() - initialize an erase command
1329 * @region: pointer to a structure that describes a SPI NOR erase region
1330 * @erase: pointer to a structure that describes a SPI NOR erase type
1332 * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
1335 static struct spi_nor_erase_command *
1336 spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region,
1337 const struct spi_nor_erase_type *erase)
1339 struct spi_nor_erase_command *cmd;
1341 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1343 return ERR_PTR(-ENOMEM);
1345 INIT_LIST_HEAD(&cmd->list);
1346 cmd->opcode = erase->opcode;
1349 if (region->offset & SNOR_OVERLAID_REGION)
1350 cmd->size = region->size;
1352 cmd->size = erase->size;
1358 * spi_nor_destroy_erase_cmd_list() - destroy erase command list
1359 * @erase_list: list of erase commands
1361 static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list)
1363 struct spi_nor_erase_command *cmd, *next;
1365 list_for_each_entry_safe(cmd, next, erase_list, list) {
1366 list_del(&cmd->list);
1372 * spi_nor_init_erase_cmd_list() - initialize erase command list
1373 * @nor: pointer to a 'struct spi_nor'
1374 * @erase_list: list of erase commands to be executed once we validate that the
1375 * erase can be performed
1376 * @addr: offset in the serial flash memory
1377 * @len: number of bytes to erase
1379 * Builds the list of best fitted erase commands and verifies if the erase can
1382 * Return: 0 on success, -errno otherwise.
1384 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
1385 struct list_head *erase_list,
1388 const struct spi_nor_erase_map *map = &nor->params.erase_map;
1389 const struct spi_nor_erase_type *erase, *prev_erase = NULL;
1390 struct spi_nor_erase_region *region;
1391 struct spi_nor_erase_command *cmd = NULL;
1395 region = spi_nor_find_erase_region(map, addr);
1397 return PTR_ERR(region);
1399 region_end = spi_nor_region_end(region);
1402 erase = spi_nor_find_best_erase_type(map, region, addr, len);
1404 goto destroy_erase_cmd_list;
1406 if (prev_erase != erase ||
1407 region->offset & SNOR_OVERLAID_REGION) {
1408 cmd = spi_nor_init_erase_cmd(region, erase);
1411 goto destroy_erase_cmd_list;
1414 list_add_tail(&cmd->list, erase_list);
1422 if (len && addr >= region_end) {
1423 region = spi_nor_region_next(region);
1425 goto destroy_erase_cmd_list;
1426 region_end = spi_nor_region_end(region);
1434 destroy_erase_cmd_list:
1435 spi_nor_destroy_erase_cmd_list(erase_list);
1440 * spi_nor_erase_multi_sectors() - perform a non-uniform erase
1441 * @nor: pointer to a 'struct spi_nor'
1442 * @addr: offset in the serial flash memory
1443 * @len: number of bytes to erase
1445 * Build a list of best fitted erase commands and execute it once we validate
1446 * that the erase can be performed.
1448 * Return: 0 on success, -errno otherwise.
1450 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
1452 LIST_HEAD(erase_list);
1453 struct spi_nor_erase_command *cmd, *next;
1456 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len);
1460 list_for_each_entry_safe(cmd, next, &erase_list, list) {
1461 nor->erase_opcode = cmd->opcode;
1462 while (cmd->count) {
1463 ret = spi_nor_write_enable(nor);
1465 goto destroy_erase_cmd_list;
1467 ret = spi_nor_erase_sector(nor, addr);
1469 goto destroy_erase_cmd_list;
1474 ret = spi_nor_wait_till_ready(nor);
1476 goto destroy_erase_cmd_list;
1478 list_del(&cmd->list);
1484 destroy_erase_cmd_list:
1485 spi_nor_destroy_erase_cmd_list(&erase_list);
1490 * Erase an address range on the nor chip. The address range may extend
1491 * one or more erase sectors. Return an error is there is a problem erasing.
1493 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
1495 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1500 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
1501 (long long)instr->len);
1503 if (spi_nor_has_uniform_erase(nor)) {
1504 div_u64_rem(instr->len, mtd->erasesize, &rem);
1512 ret = spi_nor_lock_and_prep(nor);
1516 /* whole-chip erase? */
1517 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
1518 unsigned long timeout;
1520 ret = spi_nor_write_enable(nor);
1524 ret = spi_nor_erase_chip(nor);
1529 * Scale the timeout linearly with the size of the flash, with
1530 * a minimum calibrated to an old 2MB flash. We could try to
1531 * pull these from CFI/SFDP, but these values should be good
1534 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
1535 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
1536 (unsigned long)(mtd->size / SZ_2M));
1537 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
1541 /* REVISIT in some cases we could speed up erasing large regions
1542 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
1543 * to use "small sector erase", but that's not always optimal.
1546 /* "sector"-at-a-time erase */
1547 } else if (spi_nor_has_uniform_erase(nor)) {
1549 ret = spi_nor_write_enable(nor);
1553 ret = spi_nor_erase_sector(nor, addr);
1557 addr += mtd->erasesize;
1558 len -= mtd->erasesize;
1560 ret = spi_nor_wait_till_ready(nor);
1565 /* erase multiple sectors */
1567 ret = spi_nor_erase_multi_sectors(nor, addr, len);
1572 ret = spi_nor_write_disable(nor);
1575 spi_nor_unlock_and_unprep(nor);
1580 static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,
1583 struct mtd_info *mtd = &nor->mtd;
1584 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1585 u8 tb_mask = SR_TB_BIT5;
1588 if (nor->flags & SNOR_F_HAS_SR_TB_BIT6)
1589 tb_mask = SR_TB_BIT6;
1596 pow = ((sr & mask) ^ mask) >> SR_BP_SHIFT;
1597 *len = mtd->size >> pow;
1598 if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask)
1601 *ofs = mtd->size - *len;
1606 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
1607 * @locked is false); 0 otherwise
1609 static int spi_nor_check_lock_status_sr(struct spi_nor *nor, loff_t ofs,
1610 uint64_t len, u8 sr, bool locked)
1618 spi_nor_get_locked_range_sr(nor, sr, &lock_offs, &lock_len);
1621 /* Requested range is a sub-range of locked range */
1622 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
1624 /* Requested range does not overlap with locked range */
1625 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
1628 static int spi_nor_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1631 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, true);
1634 static int spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1637 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, false);
1641 * Lock a region of the flash. Compatible with ST Micro and similar flash.
1642 * Supports the block protection bits BP{0,1,2} in the status register
1643 * (SR). Does not support these features found in newer SR bitfields:
1644 * - SEC: sector/block protect - only handle SEC=0 (block protect)
1645 * - CMP: complement protect - only support CMP=0 (range is not complemented)
1647 * Support for the following is provided conditionally for some flash:
1648 * - TB: top/bottom protect
1650 * Sample table portion for 8MB flash (Winbond w25q64fw):
1652 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
1653 * --------------------------------------------------------------------------
1654 * X | X | 0 | 0 | 0 | NONE | NONE
1655 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
1656 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
1657 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
1658 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
1659 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
1660 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
1661 * X | X | 1 | 1 | 1 | 8 MB | ALL
1662 * ------|-------|-------|-------|-------|---------------|-------------------
1663 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
1664 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
1665 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
1666 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
1667 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
1668 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
1670 * Returns negative on errors, 0 on success.
1672 static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1674 struct mtd_info *mtd = &nor->mtd;
1675 int ret, status_old, status_new;
1676 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1677 u8 tb_mask = SR_TB_BIT5;
1680 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1683 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1687 status_old = nor->bouncebuf[0];
1689 /* If nothing in our range is unlocked, we don't need to do anything */
1690 if (spi_nor_is_locked_sr(nor, ofs, len, status_old))
1693 /* If anything below us is unlocked, we can't use 'bottom' protection */
1694 if (!spi_nor_is_locked_sr(nor, 0, ofs, status_old))
1695 can_be_bottom = false;
1697 /* If anything above us is unlocked, we can't use 'top' protection */
1698 if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
1702 if (!can_be_bottom && !can_be_top)
1705 /* Prefer top, if both are valid */
1706 use_top = can_be_top;
1708 /* lock_len: length of region that should end up locked */
1710 lock_len = mtd->size - ofs;
1712 lock_len = ofs + len;
1714 if (nor->flags & SNOR_F_HAS_SR_TB_BIT6)
1715 tb_mask = SR_TB_BIT6;
1718 * Need smallest pow such that:
1720 * 1 / (2^pow) <= (len / size)
1722 * so (assuming power-of-2 size) we do:
1724 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
1726 pow = ilog2(mtd->size) - ilog2(lock_len);
1727 val = mask - (pow << SR_BP_SHIFT);
1730 /* Don't "lock" with no region! */
1734 status_new = (status_old & ~mask & ~tb_mask) | val;
1736 /* Disallow further writes if WP pin is asserted */
1737 status_new |= SR_SRWD;
1740 status_new |= tb_mask;
1742 /* Don't bother if they're the same */
1743 if (status_new == status_old)
1746 /* Only modify protection if it will not unlock other areas */
1747 if ((status_new & mask) < (status_old & mask))
1750 return spi_nor_write_sr_and_check(nor, status_new);
1754 * Unlock a region of the flash. See spi_nor_sr_lock() for more info
1756 * Returns negative on errors, 0 on success.
1758 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1760 struct mtd_info *mtd = &nor->mtd;
1761 int ret, status_old, status_new;
1762 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1763 u8 tb_mask = SR_TB_BIT5;
1766 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1769 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1773 status_old = nor->bouncebuf[0];
1775 /* If nothing in our range is locked, we don't need to do anything */
1776 if (spi_nor_is_unlocked_sr(nor, ofs, len, status_old))
1779 /* If anything below us is locked, we can't use 'top' protection */
1780 if (!spi_nor_is_unlocked_sr(nor, 0, ofs, status_old))
1783 /* If anything above us is locked, we can't use 'bottom' protection */
1784 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
1786 can_be_bottom = false;
1788 if (!can_be_bottom && !can_be_top)
1791 /* Prefer top, if both are valid */
1792 use_top = can_be_top;
1794 /* lock_len: length of region that should remain locked */
1796 lock_len = mtd->size - (ofs + len);
1800 if (nor->flags & SNOR_F_HAS_SR_TB_BIT6)
1801 tb_mask = SR_TB_BIT6;
1803 * Need largest pow such that:
1805 * 1 / (2^pow) >= (len / size)
1807 * so (assuming power-of-2 size) we do:
1809 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
1811 pow = ilog2(mtd->size) - order_base_2(lock_len);
1812 if (lock_len == 0) {
1813 val = 0; /* fully unlocked */
1815 val = mask - (pow << SR_BP_SHIFT);
1816 /* Some power-of-two sizes are not supported */
1821 status_new = (status_old & ~mask & ~tb_mask) | val;
1823 /* Don't protect status register if we're fully unlocked */
1825 status_new &= ~SR_SRWD;
1828 status_new |= tb_mask;
1830 /* Don't bother if they're the same */
1831 if (status_new == status_old)
1834 /* Only modify protection if it will not lock other areas */
1835 if ((status_new & mask) > (status_old & mask))
1838 return spi_nor_write_sr_and_check(nor, status_new);
1842 * Check if a region of the flash is (completely) locked. See spi_nor_sr_lock()
1845 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
1846 * negative on errors.
1848 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1852 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1856 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
1859 static const struct spi_nor_locking_ops spi_nor_sr_locking_ops = {
1860 .lock = spi_nor_sr_lock,
1861 .unlock = spi_nor_sr_unlock,
1862 .is_locked = spi_nor_sr_is_locked,
1865 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1867 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1870 ret = spi_nor_lock_and_prep(nor);
1874 ret = nor->params.locking_ops->lock(nor, ofs, len);
1876 spi_nor_unlock_and_unprep(nor);
1880 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1882 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1885 ret = spi_nor_lock_and_prep(nor);
1889 ret = nor->params.locking_ops->unlock(nor, ofs, len);
1891 spi_nor_unlock_and_unprep(nor);
1895 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1897 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1900 ret = spi_nor_lock_and_prep(nor);
1904 ret = nor->params.locking_ops->is_locked(nor, ofs, len);
1906 spi_nor_unlock_and_unprep(nor);
1911 * spi_nor_sr1_bit6_quad_enable() - Set the Quad Enable BIT(6) in the Status
1913 * @nor: pointer to a 'struct spi_nor'
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)
1923 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1927 if (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6)
1930 nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6;
1932 return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]);
1936 * spi_nor_sr2_bit1_quad_enable() - set the Quad Enable BIT(1) in the Status
1938 * @nor: pointer to a 'struct spi_nor'.
1940 * Bit 1 of the Status Register 2 is the QE bit for Spansion like QSPI memories.
1942 * Return: 0 on success, -errno otherwise.
1944 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor)
1948 if (nor->flags & SNOR_F_NO_READ_CR)
1949 return spi_nor_write_16bit_cr_and_check(nor, SR2_QUAD_EN_BIT1);
1951 ret = spi_nor_read_cr(nor, nor->bouncebuf);
1955 if (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1)
1958 nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1;
1960 return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]);
1964 * spi_nor_sr2_bit7_quad_enable() - set QE bit in Status Register 2.
1965 * @nor: pointer to a 'struct spi_nor'
1967 * Set the Quad Enable (QE) bit in the Status Register 2.
1969 * This is one of the procedures to set the QE bit described in the SFDP
1970 * (JESD216 rev B) specification but no manufacturer using this procedure has
1971 * been identified yet, hence the name of the function.
1973 * Return: 0 on success, -errno otherwise.
1975 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor)
1977 u8 *sr2 = nor->bouncebuf;
1981 /* Check current Quad Enable bit value. */
1982 ret = spi_nor_read_sr2(nor, sr2);
1985 if (*sr2 & SR2_QUAD_EN_BIT7)
1988 /* Update the Quad Enable bit. */
1989 *sr2 |= SR2_QUAD_EN_BIT7;
1991 ret = spi_nor_write_sr2(nor, sr2);
1997 /* Read back and check it. */
1998 ret = spi_nor_read_sr2(nor, sr2);
2002 if (*sr2 != sr2_written) {
2003 dev_dbg(nor->dev, "SR2: Read back test failed\n");
2011 is25lp256_post_bfpt_fixups(struct spi_nor *nor,
2012 const struct sfdp_parameter_header *bfpt_header,
2013 const struct sfdp_bfpt *bfpt,
2014 struct spi_nor_flash_parameter *params)
2017 * IS25LP256 supports 4B opcodes, but the BFPT advertises a
2018 * BFPT_DWORD1_ADDRESS_BYTES_3_ONLY address width.
2019 * Overwrite the address width advertised by the BFPT.
2021 if ((bfpt->dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) ==
2022 BFPT_DWORD1_ADDRESS_BYTES_3_ONLY)
2023 nor->addr_width = 4;
2028 static struct spi_nor_fixups is25lp256_fixups = {
2029 .post_bfpt = is25lp256_post_bfpt_fixups,
2033 mx25l25635_post_bfpt_fixups(struct spi_nor *nor,
2034 const struct sfdp_parameter_header *bfpt_header,
2035 const struct sfdp_bfpt *bfpt,
2036 struct spi_nor_flash_parameter *params)
2039 * MX25L25635F supports 4B opcodes but MX25L25635E does not.
2040 * Unfortunately, Macronix has re-used the same JEDEC ID for both
2041 * variants which prevents us from defining a new entry in the parts
2043 * We need a way to differentiate MX25L25635E and MX25L25635F, and it
2044 * seems that the F version advertises support for Fast Read 4-4-4 in
2047 if (bfpt->dwords[BFPT_DWORD(5)] & BFPT_DWORD5_FAST_READ_4_4_4)
2048 nor->flags |= SNOR_F_4B_OPCODES;
2053 static struct spi_nor_fixups mx25l25635_fixups = {
2054 .post_bfpt = mx25l25635_post_bfpt_fixups,
2057 static void gd25q256_default_init(struct spi_nor *nor)
2060 * Some manufacturer like GigaDevice may use different
2061 * bit to set QE on different memories, so the MFR can't
2062 * indicate the quad_enable method for this case, we need
2063 * to set it in the default_init fixup hook.
2065 nor->params.quad_enable = spi_nor_sr1_bit6_quad_enable;
2068 static struct spi_nor_fixups gd25q256_fixups = {
2069 .default_init = gd25q256_default_init,
2072 /* NOTE: double check command sets and memory organization when you add
2073 * more nor chips. This current list focusses on newer chips, which
2074 * have been converging on command sets which including JEDEC ID.
2076 * All newly added entries should describe *hardware* and should use SECT_4K
2077 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
2078 * scenarios excluding small sectors there is config option that can be
2079 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
2080 * For historical (and compatibility) reasons (before we got above config) some
2081 * old entries may be missing 4K flag.
2083 static const struct flash_info spi_nor_ids[] = {
2084 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
2085 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
2086 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
2088 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
2089 { "at25df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
2090 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
2091 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
2093 { "at25sl321", INFO(0x1f4216, 0, 64 * 1024, 64,
2094 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2096 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
2097 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
2098 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
2099 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
2101 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
2103 /* EON -- en25xxx */
2104 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
2105 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
2106 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
2107 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
2108 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
2109 { "en25q80a", INFO(0x1c3014, 0, 64 * 1024, 16,
2110 SECT_4K | SPI_NOR_DUAL_READ) },
2111 { "en25qh16", INFO(0x1c7015, 0, 64 * 1024, 32,
2112 SECT_4K | SPI_NOR_DUAL_READ) },
2113 { "en25qh32", INFO(0x1c7016, 0, 64 * 1024, 64, 0) },
2114 { "en25qh64", INFO(0x1c7017, 0, 64 * 1024, 128,
2115 SECT_4K | SPI_NOR_DUAL_READ) },
2116 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
2117 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
2118 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
2121 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
2122 { "f25l32qa", INFO(0x8c4116, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
2123 { "f25l64qa", INFO(0x8c4117, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_HAS_LOCK) },
2126 { "mr25h128", CAT25_INFO( 16 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2127 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2128 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2129 { "mr25h40", CAT25_INFO(512 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2132 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
2136 "gd25q16", INFO(0xc84015, 0, 64 * 1024, 32,
2137 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2138 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2141 "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
2142 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2143 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2146 "gd25lq32", INFO(0xc86016, 0, 64 * 1024, 64,
2147 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2148 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2151 "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
2152 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2153 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2156 "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
2157 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2158 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2161 "gd25lq128d", INFO(0xc86018, 0, 64 * 1024, 256,
2162 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2163 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2166 "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
2167 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2168 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2171 "gd25q256", INFO(0xc84019, 0, 64 * 1024, 512,
2172 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2173 SPI_NOR_4B_OPCODES | SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB |
2175 .fixups = &gd25q256_fixups,
2178 /* Intel/Numonyx -- xxxs33b */
2179 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
2180 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
2181 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
2184 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
2185 { "is25lq040b", INFO(0x9d4013, 0, 64 * 1024, 8,
2186 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2187 { "is25lp016d", INFO(0x9d6015, 0, 64 * 1024, 32,
2188 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2189 { "is25lp080d", INFO(0x9d6014, 0, 64 * 1024, 16,
2190 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2191 { "is25lp032", INFO(0x9d6016, 0, 64 * 1024, 64,
2192 SECT_4K | SPI_NOR_DUAL_READ) },
2193 { "is25lp064", INFO(0x9d6017, 0, 64 * 1024, 128,
2194 SECT_4K | SPI_NOR_DUAL_READ) },
2195 { "is25lp128", INFO(0x9d6018, 0, 64 * 1024, 256,
2196 SECT_4K | SPI_NOR_DUAL_READ) },
2197 { "is25lp256", INFO(0x9d6019, 0, 64 * 1024, 512,
2198 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2200 .fixups = &is25lp256_fixups },
2201 { "is25wp032", INFO(0x9d7016, 0, 64 * 1024, 64,
2202 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2203 { "is25wp064", INFO(0x9d7017, 0, 64 * 1024, 128,
2204 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2205 { "is25wp128", INFO(0x9d7018, 0, 64 * 1024, 256,
2206 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2207 { "is25wp256", INFO(0x9d7019, 0, 64 * 1024, 512,
2208 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2210 .fixups = &is25lp256_fixups },
2213 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
2214 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
2215 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
2216 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
2217 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
2218 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
2219 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
2220 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
2221 { "mx25u2033e", INFO(0xc22532, 0, 64 * 1024, 4, SECT_4K) },
2222 { "mx25u3235f", INFO(0xc22536, 0, 64 * 1024, 64,
2223 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2224 { "mx25u4035", INFO(0xc22533, 0, 64 * 1024, 8, SECT_4K) },
2225 { "mx25u8035", INFO(0xc22534, 0, 64 * 1024, 16, SECT_4K) },
2226 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
2227 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
2228 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
2229 { "mx25r3235f", INFO(0xc22816, 0, 64 * 1024, 64,
2230 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2231 { "mx25u12835f", INFO(0xc22538, 0, 64 * 1024, 256,
2232 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2233 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512,
2234 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)
2235 .fixups = &mx25l25635_fixups },
2236 { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_4B_OPCODES) },
2237 { "mx25v8035f", INFO(0xc22314, 0, 64 * 1024, 16,
2238 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2239 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
2240 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2241 { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2242 { "mx66l1g45g", INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2243 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
2245 /* Micron <--> ST Micro */
2246 { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_QUAD_READ) },
2247 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
2248 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
2249 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
2250 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
2251 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K |
2252 USE_FSR | SPI_NOR_QUAD_READ) },
2253 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K |
2254 USE_FSR | SPI_NOR_QUAD_READ) },
2255 { "mt25ql256a", INFO6(0x20ba19, 0x104400, 64 * 1024, 512,
2256 SECT_4K | USE_FSR | SPI_NOR_DUAL_READ |
2257 SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2258 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K |
2259 USE_FSR | SPI_NOR_DUAL_READ |
2260 SPI_NOR_QUAD_READ) },
2261 { "mt25qu256a", INFO6(0x20bb19, 0x104400, 64 * 1024, 512,
2262 SECT_4K | USE_FSR | SPI_NOR_DUAL_READ |
2263 SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2264 { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K |
2265 USE_FSR | SPI_NOR_QUAD_READ) },
2266 { "mt25ql512a", INFO6(0x20ba20, 0x104400, 64 * 1024, 1024,
2267 SECT_4K | USE_FSR | SPI_NOR_DUAL_READ |
2268 SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2269 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
2270 { "mt25qu512a", INFO6(0x20bb20, 0x104400, 64 * 1024, 1024,
2271 SECT_4K | USE_FSR | SPI_NOR_DUAL_READ |
2272 SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2273 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K |
2274 USE_FSR | SPI_NOR_QUAD_READ) },
2275 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
2276 { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
2277 { "mt25ql02g", INFO(0x20ba22, 0, 64 * 1024, 4096,
2278 SECT_4K | USE_FSR | SPI_NOR_QUAD_READ |
2280 { "mt25qu02g", INFO(0x20bb22, 0, 64 * 1024, 4096, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
2284 "mt35xu512aba", INFO(0x2c5b1a, 0, 128 * 1024, 512,
2285 SECT_4K | USE_FSR | SPI_NOR_OCTAL_READ |
2288 { "mt35xu02g", INFO(0x2c5b1c, 0, 128 * 1024, 2048,
2289 SECT_4K | USE_FSR | SPI_NOR_OCTAL_READ |
2290 SPI_NOR_4B_OPCODES) },
2293 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
2294 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
2295 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
2297 /* Spansion/Cypress -- single (large) sector size only, at least
2298 * for the chips listed here (without boot sectors).
2300 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2301 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2302 { "s25fl128s0", INFO6(0x012018, 0x4d0080, 256 * 1024, 64,
2303 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2304 { "s25fl128s1", INFO6(0x012018, 0x4d0180, 64 * 1024, 256,
2305 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2306 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR) },
2307 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2308 { "s25fl512s", INFO6(0x010220, 0x4d0080, 256 * 1024, 256,
2309 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2310 SPI_NOR_HAS_LOCK | USE_CLSR) },
2311 { "s25fs512s", INFO6(0x010220, 0x4d0081, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2312 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
2313 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
2314 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
2315 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2316 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2317 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
2318 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
2319 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
2320 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
2321 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
2322 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2323 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2324 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2325 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
2326 { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2327 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
2328 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
2329 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
2330 { "s25fl208k", INFO(0x014014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ) },
2331 { "s25fl064l", INFO(0x016017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2332 { "s25fl128l", INFO(0x016018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2333 { "s25fl256l", INFO(0x016019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2335 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
2336 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
2337 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
2338 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
2339 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
2340 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
2341 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
2342 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
2343 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
2344 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
2345 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
2346 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
2347 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
2348 { "sst26wf016b", INFO(0xbf2651, 0, 64 * 1024, 32, SECT_4K |
2349 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2350 { "sst26vf016b", INFO(0xbf2641, 0, 64 * 1024, 32, SECT_4K |
2351 SPI_NOR_DUAL_READ) },
2352 { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2354 /* ST Microelectronics -- newer production may have feature updates */
2355 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
2356 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
2357 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
2358 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
2359 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
2360 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
2361 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
2362 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
2363 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
2365 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
2366 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
2367 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
2368 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
2369 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
2370 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
2371 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
2372 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
2373 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
2375 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
2376 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
2377 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
2379 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
2380 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
2381 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
2383 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
2384 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
2385 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
2386 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
2387 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
2388 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
2390 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
2391 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
2392 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
2393 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
2394 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
2395 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
2396 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
2398 "w25q16dw", INFO(0xef6015, 0, 64 * 1024, 32,
2399 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2400 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2402 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
2404 "w25q16jv-im/jm", INFO(0xef7015, 0, 64 * 1024, 32,
2405 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2406 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2408 { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K) },
2409 { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K) },
2410 { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K) },
2411 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
2413 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
2414 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2415 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2418 "w25q32jv", INFO(0xef7016, 0, 64 * 1024, 64,
2419 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2420 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2423 "w25q32jwm", INFO(0xef8016, 0, 64 * 1024, 64,
2424 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2425 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2427 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
2428 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
2430 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
2431 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2432 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2435 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
2436 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2437 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2440 "w25q128jv", INFO(0xef7018, 0, 64 * 1024, 256,
2441 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2442 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2444 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
2445 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
2446 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
2447 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512,
2448 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2449 SPI_NOR_4B_OPCODES) },
2450 { "w25q256jvm", INFO(0xef7019, 0, 64 * 1024, 512,
2451 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2452 { "w25q256jw", INFO(0xef6019, 0, 64 * 1024, 512,
2453 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2454 { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
2455 SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
2457 /* Catalyst / On Semiconductor -- non-JEDEC */
2458 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2459 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2460 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2461 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2462 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2464 /* Xilinx S3AN Internal Flash */
2465 { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
2466 { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
2467 { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
2468 { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
2469 { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
2471 /* XMC (Wuhan Xinxin Semiconductor Manufacturing Corp.) */
2472 { "XM25QH64A", INFO(0x207017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2473 { "XM25QH128A", INFO(0x207018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2477 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
2479 u8 *id = nor->bouncebuf;
2484 struct spi_mem_op op =
2485 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDID, 1),
2487 SPI_MEM_OP_NO_DUMMY,
2488 SPI_MEM_OP_DATA_IN(SPI_NOR_MAX_ID_LEN, id, 1));
2490 ret = spi_mem_exec_op(nor->spimem, &op);
2492 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id,
2493 SPI_NOR_MAX_ID_LEN);
2496 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret);
2497 return ERR_PTR(ret);
2500 for (i = 0; i < ARRAY_SIZE(spi_nor_ids) - 1; i++) {
2501 if (spi_nor_ids[i].id_len &&
2502 !memcmp(spi_nor_ids[i].id, id, spi_nor_ids[i].id_len))
2503 return &spi_nor_ids[i];
2505 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
2506 SPI_NOR_MAX_ID_LEN, id);
2507 return ERR_PTR(-ENODEV);
2510 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
2511 size_t *retlen, u_char *buf)
2513 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2516 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
2518 ret = spi_nor_lock_and_prep(nor);
2525 addr = spi_nor_convert_addr(nor, addr);
2527 ret = spi_nor_read_data(nor, addr, len, buf);
2529 /* We shouldn't see 0-length reads */
2545 spi_nor_unlock_and_unprep(nor);
2549 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
2550 size_t *retlen, const u_char *buf)
2552 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2556 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
2558 ret = spi_nor_lock_and_prep(nor);
2562 ret = spi_nor_write_enable(nor);
2566 nor->sst_write_second = false;
2568 /* Start write from odd address. */
2570 nor->program_opcode = SPINOR_OP_BP;
2572 /* write one byte. */
2573 ret = spi_nor_write_data(nor, to, 1, buf);
2576 WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
2577 ret = spi_nor_wait_till_ready(nor);
2585 /* Write out most of the data here. */
2586 for (; actual < len - 1; actual += 2) {
2587 nor->program_opcode = SPINOR_OP_AAI_WP;
2589 /* write two bytes. */
2590 ret = spi_nor_write_data(nor, to, 2, buf + actual);
2593 WARN(ret != 2, "While writing 2 bytes written %i bytes\n", ret);
2594 ret = spi_nor_wait_till_ready(nor);
2598 nor->sst_write_second = true;
2600 nor->sst_write_second = false;
2602 ret = spi_nor_write_disable(nor);
2606 ret = spi_nor_wait_till_ready(nor);
2610 /* Write out trailing byte if it exists. */
2611 if (actual != len) {
2612 ret = spi_nor_write_enable(nor);
2616 nor->program_opcode = SPINOR_OP_BP;
2617 ret = spi_nor_write_data(nor, to, 1, buf + actual);
2620 WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
2621 ret = spi_nor_wait_till_ready(nor);
2627 ret = spi_nor_write_disable(nor);
2631 spi_nor_unlock_and_unprep(nor);
2636 * Write an address range to the nor chip. Data must be written in
2637 * FLASH_PAGESIZE chunks. The address range may be any size provided
2638 * it is within the physical boundaries.
2640 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
2641 size_t *retlen, const u_char *buf)
2643 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2644 size_t page_offset, page_remain, i;
2647 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
2649 ret = spi_nor_lock_and_prep(nor);
2653 for (i = 0; i < len; ) {
2655 loff_t addr = to + i;
2658 * If page_size is a power of two, the offset can be quickly
2659 * calculated with an AND operation. On the other cases we
2660 * need to do a modulus operation (more expensive).
2661 * Power of two numbers have only one bit set and we can use
2662 * the instruction hweight32 to detect if we need to do a
2663 * modulus (do_div()) or not.
2665 if (hweight32(nor->page_size) == 1) {
2666 page_offset = addr & (nor->page_size - 1);
2668 uint64_t aux = addr;
2670 page_offset = do_div(aux, nor->page_size);
2672 /* the size of data remaining on the first page */
2673 page_remain = min_t(size_t,
2674 nor->page_size - page_offset, len - i);
2676 addr = spi_nor_convert_addr(nor, addr);
2678 ret = spi_nor_write_enable(nor);
2682 ret = spi_nor_write_data(nor, addr, page_remain, buf + i);
2687 ret = spi_nor_wait_till_ready(nor);
2695 spi_nor_unlock_and_unprep(nor);
2699 static int spi_nor_check(struct spi_nor *nor)
2702 (!nor->spimem && !nor->controller_ops) ||
2703 (!nor->spimem && nor->controller_ops &&
2704 (!nor->controller_ops->read ||
2705 !nor->controller_ops->write ||
2706 !nor->controller_ops->read_reg ||
2707 !nor->controller_ops->write_reg))) {
2708 pr_err("spi-nor: please fill all the necessary fields!\n");
2712 if (nor->spimem && nor->controller_ops) {
2713 dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one of them.\n");
2720 static int s3an_nor_setup(struct spi_nor *nor,
2721 const struct spi_nor_hwcaps *hwcaps)
2725 ret = spi_nor_xread_sr(nor, nor->bouncebuf);
2729 nor->erase_opcode = SPINOR_OP_XSE;
2730 nor->program_opcode = SPINOR_OP_XPP;
2731 nor->read_opcode = SPINOR_OP_READ;
2732 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2735 * This flashes have a page size of 264 or 528 bytes (known as
2736 * Default addressing mode). It can be changed to a more standard
2737 * Power of two mode where the page size is 256/512. This comes
2738 * with a price: there is 3% less of space, the data is corrupted
2739 * and the page size cannot be changed back to default addressing
2742 * The current addressing mode can be read from the XRDSR register
2743 * and should not be changed, because is a destructive operation.
2745 if (nor->bouncebuf[0] & XSR_PAGESIZE) {
2746 /* Flash in Power of 2 mode */
2747 nor->page_size = (nor->page_size == 264) ? 256 : 512;
2748 nor->mtd.writebufsize = nor->page_size;
2749 nor->mtd.size = 8 * nor->page_size * nor->info->n_sectors;
2750 nor->mtd.erasesize = 8 * nor->page_size;
2752 /* Flash in Default addressing mode */
2753 nor->params.convert_addr = s3an_convert_addr;
2754 nor->mtd.erasesize = nor->info->sector_size;
2761 spi_nor_set_read_settings(struct spi_nor_read_command *read,
2765 enum spi_nor_protocol proto)
2767 read->num_mode_clocks = num_mode_clocks;
2768 read->num_wait_states = num_wait_states;
2769 read->opcode = opcode;
2770 read->proto = proto;
2773 void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
2774 enum spi_nor_protocol proto)
2776 pp->opcode = opcode;
2780 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2784 for (i = 0; i < size; i++)
2785 if (table[i][0] == (int)hwcaps)
2791 int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2793 static const int hwcaps_read2cmd[][2] = {
2794 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
2795 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
2796 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
2797 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
2798 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
2799 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
2800 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
2801 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
2802 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
2803 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
2804 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
2805 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
2806 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
2807 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
2808 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
2811 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2812 ARRAY_SIZE(hwcaps_read2cmd));
2815 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2817 static const int hwcaps_pp2cmd[][2] = {
2818 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
2819 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
2820 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
2821 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
2822 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
2823 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
2824 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
2827 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2828 ARRAY_SIZE(hwcaps_pp2cmd));
2832 * spi_nor_spimem_check_op - check if the operation is supported
2834 *@nor: pointer to a 'struct spi_nor'
2835 *@op: pointer to op template to be checked
2837 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2839 static int spi_nor_spimem_check_op(struct spi_nor *nor,
2840 struct spi_mem_op *op)
2843 * First test with 4 address bytes. The opcode itself might
2844 * be a 3B addressing opcode but we don't care, because
2845 * SPI controller implementation should not check the opcode,
2846 * but just the sequence.
2848 op->addr.nbytes = 4;
2849 if (!spi_mem_supports_op(nor->spimem, op)) {
2850 if (nor->mtd.size > SZ_16M)
2853 /* If flash size <= 16MB, 3 address bytes are sufficient */
2854 op->addr.nbytes = 3;
2855 if (!spi_mem_supports_op(nor->spimem, op))
2863 * spi_nor_spimem_check_readop - check if the read op is supported
2865 *@nor: pointer to a 'struct spi_nor'
2866 *@read: pointer to op template to be checked
2868 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2870 static int spi_nor_spimem_check_readop(struct spi_nor *nor,
2871 const struct spi_nor_read_command *read)
2873 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(read->opcode, 1),
2874 SPI_MEM_OP_ADDR(3, 0, 1),
2875 SPI_MEM_OP_DUMMY(0, 1),
2876 SPI_MEM_OP_DATA_IN(0, NULL, 1));
2878 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(read->proto);
2879 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(read->proto);
2880 op.data.buswidth = spi_nor_get_protocol_data_nbits(read->proto);
2881 op.dummy.buswidth = op.addr.buswidth;
2882 op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
2883 op.dummy.buswidth / 8;
2885 return spi_nor_spimem_check_op(nor, &op);
2889 * spi_nor_spimem_check_pp - check if the page program op is supported
2891 *@nor: pointer to a 'struct spi_nor'
2892 *@pp: pointer to op template to be checked
2894 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2896 static int spi_nor_spimem_check_pp(struct spi_nor *nor,
2897 const struct spi_nor_pp_command *pp)
2899 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(pp->opcode, 1),
2900 SPI_MEM_OP_ADDR(3, 0, 1),
2901 SPI_MEM_OP_NO_DUMMY,
2902 SPI_MEM_OP_DATA_OUT(0, NULL, 1));
2904 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(pp->proto);
2905 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(pp->proto);
2906 op.data.buswidth = spi_nor_get_protocol_data_nbits(pp->proto);
2908 return spi_nor_spimem_check_op(nor, &op);
2912 * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
2913 * based on SPI controller capabilities
2914 * @nor: pointer to a 'struct spi_nor'
2915 * @hwcaps: pointer to resulting capabilities after adjusting
2916 * according to controller and flash's capability
2919 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps)
2921 struct spi_nor_flash_parameter *params = &nor->params;
2924 /* DTR modes are not supported yet, mask them all. */
2925 *hwcaps &= ~SNOR_HWCAPS_DTR;
2927 /* X-X-X modes are not supported yet, mask them all. */
2928 *hwcaps &= ~SNOR_HWCAPS_X_X_X;
2930 for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
2933 if (!(*hwcaps & BIT(cap)))
2936 rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
2938 spi_nor_spimem_check_readop(nor, ¶ms->reads[rdidx]))
2939 *hwcaps &= ~BIT(cap);
2941 ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
2945 if (spi_nor_spimem_check_pp(nor,
2946 ¶ms->page_programs[ppidx]))
2947 *hwcaps &= ~BIT(cap);
2952 * spi_nor_set_erase_type() - set a SPI NOR erase type
2953 * @erase: pointer to a structure that describes a SPI NOR erase type
2954 * @size: the size of the sector/block erased by the erase type
2955 * @opcode: the SPI command op code to erase the sector/block
2957 void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
2961 erase->opcode = opcode;
2962 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
2963 erase->size_shift = ffs(erase->size) - 1;
2964 erase->size_mask = (1 << erase->size_shift) - 1;
2968 * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
2969 * @map: the erase map of the SPI NOR
2970 * @erase_mask: bitmask encoding erase types that can erase the entire
2972 * @flash_size: the spi nor flash memory size
2974 void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
2975 u8 erase_mask, u64 flash_size)
2977 /* Offset 0 with erase_mask and SNOR_LAST_REGION bit set */
2978 map->uniform_region.offset = (erase_mask & SNOR_ERASE_TYPE_MASK) |
2980 map->uniform_region.size = flash_size;
2981 map->regions = &map->uniform_region;
2982 map->uniform_erase_type = erase_mask;
2985 int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
2986 const struct sfdp_parameter_header *bfpt_header,
2987 const struct sfdp_bfpt *bfpt,
2988 struct spi_nor_flash_parameter *params)
2990 if (nor->info->fixups && nor->info->fixups->post_bfpt)
2991 return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt,
2997 static int spi_nor_select_read(struct spi_nor *nor,
3000 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
3001 const struct spi_nor_read_command *read;
3006 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
3010 read = &nor->params.reads[cmd];
3011 nor->read_opcode = read->opcode;
3012 nor->read_proto = read->proto;
3015 * In the spi-nor framework, we don't need to make the difference
3016 * between mode clock cycles and wait state clock cycles.
3017 * Indeed, the value of the mode clock cycles is used by a QSPI
3018 * flash memory to know whether it should enter or leave its 0-4-4
3019 * (Continuous Read / XIP) mode.
3020 * eXecution In Place is out of the scope of the mtd sub-system.
3021 * Hence we choose to merge both mode and wait state clock cycles
3022 * into the so called dummy clock cycles.
3024 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
3028 static int spi_nor_select_pp(struct spi_nor *nor,
3031 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
3032 const struct spi_nor_pp_command *pp;
3037 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
3041 pp = &nor->params.page_programs[cmd];
3042 nor->program_opcode = pp->opcode;
3043 nor->write_proto = pp->proto;
3048 * spi_nor_select_uniform_erase() - select optimum uniform erase type
3049 * @map: the erase map of the SPI NOR
3050 * @wanted_size: the erase type size to search for. Contains the value of
3051 * info->sector_size or of the "small sector" size in case
3052 * CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined.
3054 * Once the optimum uniform sector erase command is found, disable all the
3057 * Return: pointer to erase type on success, NULL otherwise.
3059 static const struct spi_nor_erase_type *
3060 spi_nor_select_uniform_erase(struct spi_nor_erase_map *map,
3061 const u32 wanted_size)
3063 const struct spi_nor_erase_type *tested_erase, *erase = NULL;
3065 u8 uniform_erase_type = map->uniform_erase_type;
3067 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
3068 if (!(uniform_erase_type & BIT(i)))
3071 tested_erase = &map->erase_type[i];
3074 * If the current erase size is the one, stop here:
3075 * we have found the right uniform Sector Erase command.
3077 if (tested_erase->size == wanted_size) {
3078 erase = tested_erase;
3083 * Otherwise, the current erase size is still a valid canditate.
3084 * Select the biggest valid candidate.
3086 if (!erase && tested_erase->size)
3087 erase = tested_erase;
3088 /* keep iterating to find the wanted_size */
3094 /* Disable all other Sector Erase commands. */
3095 map->uniform_erase_type &= ~SNOR_ERASE_TYPE_MASK;
3096 map->uniform_erase_type |= BIT(erase - map->erase_type);
3100 static int spi_nor_select_erase(struct spi_nor *nor)
3102 struct spi_nor_erase_map *map = &nor->params.erase_map;
3103 const struct spi_nor_erase_type *erase = NULL;
3104 struct mtd_info *mtd = &nor->mtd;
3105 u32 wanted_size = nor->info->sector_size;
3109 * The previous implementation handling Sector Erase commands assumed
3110 * that the SPI flash memory has an uniform layout then used only one
3111 * of the supported erase sizes for all Sector Erase commands.
3112 * So to be backward compatible, the new implementation also tries to
3113 * manage the SPI flash memory as uniform with a single erase sector
3114 * size, when possible.
3116 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
3117 /* prefer "small sector" erase if possible */
3118 wanted_size = 4096u;
3121 if (spi_nor_has_uniform_erase(nor)) {
3122 erase = spi_nor_select_uniform_erase(map, wanted_size);
3125 nor->erase_opcode = erase->opcode;
3126 mtd->erasesize = erase->size;
3131 * For non-uniform SPI flash memory, set mtd->erasesize to the
3132 * maximum erase sector size. No need to set nor->erase_opcode.
3134 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
3135 if (map->erase_type[i].size) {
3136 erase = &map->erase_type[i];
3144 mtd->erasesize = erase->size;
3148 static int spi_nor_default_setup(struct spi_nor *nor,
3149 const struct spi_nor_hwcaps *hwcaps)
3151 struct spi_nor_flash_parameter *params = &nor->params;
3152 u32 ignored_mask, shared_mask;
3156 * Keep only the hardware capabilities supported by both the SPI
3157 * controller and the SPI flash memory.
3159 shared_mask = hwcaps->mask & params->hwcaps.mask;
3163 * When called from spi_nor_probe(), all caps are set and we
3164 * need to discard some of them based on what the SPI
3165 * controller actually supports (using spi_mem_supports_op()).
3167 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask);
3170 * SPI n-n-n protocols are not supported when the SPI
3171 * controller directly implements the spi_nor interface.
3172 * Yet another reason to switch to spi-mem.
3174 ignored_mask = SNOR_HWCAPS_X_X_X;
3175 if (shared_mask & ignored_mask) {
3177 "SPI n-n-n protocols are not supported.\n");
3178 shared_mask &= ~ignored_mask;
3182 /* Select the (Fast) Read command. */
3183 err = spi_nor_select_read(nor, shared_mask);
3186 "can't select read settings supported by both the SPI controller and memory.\n");
3190 /* Select the Page Program command. */
3191 err = spi_nor_select_pp(nor, shared_mask);
3194 "can't select write settings supported by both the SPI controller and memory.\n");
3198 /* Select the Sector Erase command. */
3199 err = spi_nor_select_erase(nor);
3202 "can't select erase settings supported by both the SPI controller and memory.\n");
3209 static int spi_nor_setup(struct spi_nor *nor,
3210 const struct spi_nor_hwcaps *hwcaps)
3212 if (!nor->params.setup)
3215 return nor->params.setup(nor, hwcaps);
3218 static void atmel_set_default_init(struct spi_nor *nor)
3220 nor->flags |= SNOR_F_HAS_LOCK;
3223 static void intel_set_default_init(struct spi_nor *nor)
3225 nor->flags |= SNOR_F_HAS_LOCK;
3228 static void issi_set_default_init(struct spi_nor *nor)
3230 nor->params.quad_enable = spi_nor_sr1_bit6_quad_enable;
3233 static void macronix_set_default_init(struct spi_nor *nor)
3235 nor->params.quad_enable = spi_nor_sr1_bit6_quad_enable;
3236 nor->params.set_4byte_addr_mode = spi_nor_set_4byte_addr_mode;
3239 static void sst_set_default_init(struct spi_nor *nor)
3241 nor->flags |= SNOR_F_HAS_LOCK;
3244 static void st_micron_set_default_init(struct spi_nor *nor)
3246 nor->flags |= SNOR_F_HAS_LOCK;
3247 nor->flags &= ~SNOR_F_HAS_16BIT_SR;
3248 nor->params.quad_enable = NULL;
3249 nor->params.set_4byte_addr_mode = st_micron_set_4byte_addr_mode;
3252 static void winbond_set_default_init(struct spi_nor *nor)
3254 nor->params.set_4byte_addr_mode = winbond_set_4byte_addr_mode;
3258 * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and
3259 * settings based on MFR register and ->default_init() hook.
3260 * @nor: pointer to a 'struct spi-nor'.
3262 static void spi_nor_manufacturer_init_params(struct spi_nor *nor)
3264 /* Init flash parameters based on MFR */
3265 switch (JEDEC_MFR(nor->info)) {
3266 case SNOR_MFR_ATMEL:
3267 atmel_set_default_init(nor);
3270 case SNOR_MFR_INTEL:
3271 intel_set_default_init(nor);
3275 issi_set_default_init(nor);
3278 case SNOR_MFR_MACRONIX:
3279 macronix_set_default_init(nor);
3283 case SNOR_MFR_MICRON:
3284 st_micron_set_default_init(nor);
3288 sst_set_default_init(nor);
3291 case SNOR_MFR_WINBOND:
3292 winbond_set_default_init(nor);
3299 if (nor->info->fixups && nor->info->fixups->default_init)
3300 nor->info->fixups->default_init(nor);
3304 * spi_nor_sfdp_init_params() - Initialize the flash's parameters and settings
3305 * based on JESD216 SFDP standard.
3306 * @nor: pointer to a 'struct spi-nor'.
3308 * The method has a roll-back mechanism: in case the SFDP parsing fails, the
3309 * legacy flash parameters and settings will be restored.
3311 static void spi_nor_sfdp_init_params(struct spi_nor *nor)
3313 struct spi_nor_flash_parameter sfdp_params;
3315 memcpy(&sfdp_params, &nor->params, sizeof(sfdp_params));
3317 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
3318 nor->addr_width = 0;
3319 nor->flags &= ~SNOR_F_4B_OPCODES;
3321 memcpy(&nor->params, &sfdp_params, sizeof(nor->params));
3326 * spi_nor_info_init_params() - Initialize the flash's parameters and settings
3327 * based on nor->info data.
3328 * @nor: pointer to a 'struct spi-nor'.
3330 static void spi_nor_info_init_params(struct spi_nor *nor)
3332 struct spi_nor_flash_parameter *params = &nor->params;
3333 struct spi_nor_erase_map *map = ¶ms->erase_map;
3334 const struct flash_info *info = nor->info;
3335 struct device_node *np = spi_nor_get_flash_node(nor);
3338 /* Initialize legacy flash parameters and settings. */
3339 params->quad_enable = spi_nor_sr2_bit1_quad_enable;
3340 params->set_4byte_addr_mode = spansion_set_4byte_addr_mode;
3341 params->setup = spi_nor_default_setup;
3342 /* Default to 16-bit Write Status (01h) Command */
3343 nor->flags |= SNOR_F_HAS_16BIT_SR;
3345 /* Set SPI NOR sizes. */
3346 params->size = (u64)info->sector_size * info->n_sectors;
3347 params->page_size = info->page_size;
3349 if (!(info->flags & SPI_NOR_NO_FR)) {
3350 /* Default to Fast Read for DT and non-DT platform devices. */
3351 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
3353 /* Mask out Fast Read if not requested at DT instantiation. */
3354 if (np && !of_property_read_bool(np, "m25p,fast-read"))
3355 params->hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
3358 /* (Fast) Read settings. */
3359 params->hwcaps.mask |= SNOR_HWCAPS_READ;
3360 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
3361 0, 0, SPINOR_OP_READ,
3364 if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
3365 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
3366 0, 8, SPINOR_OP_READ_FAST,
3369 if (info->flags & SPI_NOR_DUAL_READ) {
3370 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
3371 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2],
3372 0, 8, SPINOR_OP_READ_1_1_2,
3376 if (info->flags & SPI_NOR_QUAD_READ) {
3377 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
3378 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
3379 0, 8, SPINOR_OP_READ_1_1_4,
3383 if (info->flags & SPI_NOR_OCTAL_READ) {
3384 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
3385 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_8],
3386 0, 8, SPINOR_OP_READ_1_1_8,
3390 /* Page Program settings. */
3391 params->hwcaps.mask |= SNOR_HWCAPS_PP;
3392 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
3393 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
3396 * Sector Erase settings. Sort Erase Types in ascending order, with the
3397 * smallest erase size starting at BIT(0).
3401 if (info->flags & SECT_4K_PMC) {
3402 erase_mask |= BIT(i);
3403 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
3404 SPINOR_OP_BE_4K_PMC);
3406 } else if (info->flags & SECT_4K) {
3407 erase_mask |= BIT(i);
3408 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
3412 erase_mask |= BIT(i);
3413 spi_nor_set_erase_type(&map->erase_type[i], info->sector_size,
3415 spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
3418 static void spansion_post_sfdp_fixups(struct spi_nor *nor)
3420 if (nor->params.size <= SZ_16M)
3423 nor->flags |= SNOR_F_4B_OPCODES;
3424 /* No small sector erase for 4-byte command set */
3425 nor->erase_opcode = SPINOR_OP_SE;
3426 nor->mtd.erasesize = nor->info->sector_size;
3429 static void s3an_post_sfdp_fixups(struct spi_nor *nor)
3431 nor->params.setup = s3an_nor_setup;
3435 * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
3436 * after SFDP has been parsed (is also called for SPI NORs that do not
3438 * @nor: pointer to a 'struct spi_nor'
3440 * Typically used to tweak various parameters that could not be extracted by
3441 * other means (i.e. when information provided by the SFDP/flash_info tables
3442 * are incomplete or wrong).
3444 static void spi_nor_post_sfdp_fixups(struct spi_nor *nor)
3446 switch (JEDEC_MFR(nor->info)) {
3447 case SNOR_MFR_SPANSION:
3448 spansion_post_sfdp_fixups(nor);
3455 if (nor->info->flags & SPI_S3AN)
3456 s3an_post_sfdp_fixups(nor);
3458 if (nor->info->fixups && nor->info->fixups->post_sfdp)
3459 nor->info->fixups->post_sfdp(nor);
3463 * spi_nor_late_init_params() - Late initialization of default flash parameters.
3464 * @nor: pointer to a 'struct spi_nor'
3466 * Used to set default flash parameters and settings when the ->default_init()
3467 * hook or the SFDP parser let voids.
3469 static void spi_nor_late_init_params(struct spi_nor *nor)
3472 * NOR protection support. When locking_ops are not provided, we pick
3475 if (nor->flags & SNOR_F_HAS_LOCK && !nor->params.locking_ops)
3476 nor->params.locking_ops = &spi_nor_sr_locking_ops;
3480 * spi_nor_init_params() - Initialize the flash's parameters and settings.
3481 * @nor: pointer to a 'struct spi-nor'.
3483 * The flash parameters and settings are initialized based on a sequence of
3484 * calls that are ordered by priority:
3486 * 1/ Default flash parameters initialization. The initializations are done
3487 * based on nor->info data:
3488 * spi_nor_info_init_params()
3490 * which can be overwritten by:
3491 * 2/ Manufacturer flash parameters initialization. The initializations are
3492 * done based on MFR register, or when the decisions can not be done solely
3493 * based on MFR, by using specific flash_info tweeks, ->default_init():
3494 * spi_nor_manufacturer_init_params()
3496 * which can be overwritten by:
3497 * 3/ SFDP flash parameters initialization. JESD216 SFDP is a standard and
3498 * should be more accurate that the above.
3499 * spi_nor_sfdp_init_params()
3501 * Please note that there is a ->post_bfpt() fixup hook that can overwrite
3502 * the flash parameters and settings immediately after parsing the Basic
3503 * Flash Parameter Table.
3505 * which can be overwritten by:
3506 * 4/ Post SFDP flash parameters initialization. Used to tweak various
3507 * parameters that could not be extracted by other means (i.e. when
3508 * information provided by the SFDP/flash_info tables are incomplete or
3510 * spi_nor_post_sfdp_fixups()
3512 * 5/ Late default flash parameters initialization, used when the
3513 * ->default_init() hook or the SFDP parser do not set specific params.
3514 * spi_nor_late_init_params()
3516 static void spi_nor_init_params(struct spi_nor *nor)
3518 spi_nor_info_init_params(nor);
3520 spi_nor_manufacturer_init_params(nor);
3522 if ((nor->info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
3523 !(nor->info->flags & SPI_NOR_SKIP_SFDP))
3524 spi_nor_sfdp_init_params(nor);
3526 spi_nor_post_sfdp_fixups(nor);
3528 spi_nor_late_init_params(nor);
3532 * spi_nor_quad_enable() - enable Quad I/O if needed.
3533 * @nor: pointer to a 'struct spi_nor'
3535 * Return: 0 on success, -errno otherwise.
3537 static int spi_nor_quad_enable(struct spi_nor *nor)
3539 if (!nor->params.quad_enable)
3542 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 ||
3543 spi_nor_get_protocol_width(nor->write_proto) == 4))
3546 return nor->params.quad_enable(nor);
3550 * spi_nor_unlock_all() - Unlocks the entire flash memory array.
3551 * @nor: pointer to a 'struct spi_nor'.
3553 * Some SPI NOR flashes are write protected by default after a power-on reset
3554 * cycle, in order to avoid inadvertent writes during power-up. Backward
3555 * compatibility imposes to unlock the entire flash memory array at power-up
3558 static int spi_nor_unlock_all(struct spi_nor *nor)
3560 if (nor->flags & SNOR_F_HAS_LOCK)
3561 return spi_nor_unlock(&nor->mtd, 0, nor->params.size);
3566 static int spi_nor_init(struct spi_nor *nor)
3570 err = spi_nor_quad_enable(nor);
3572 dev_dbg(nor->dev, "quad mode not supported\n");
3576 err = spi_nor_unlock_all(nor);
3578 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
3582 if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES)) {
3584 * If the RESET# pin isn't hooked up properly, or the system
3585 * otherwise doesn't perform a reset command in the boot
3586 * sequence, it's impossible to 100% protect against unexpected
3587 * reboots (e.g., crashes). Warn the user (or hopefully, system
3588 * designer) that this is bad.
3590 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
3591 "enabling reset hack; may not recover from unexpected reboots\n");
3592 nor->params.set_4byte_addr_mode(nor, true);
3598 /* mtd resume handler */
3599 static void spi_nor_resume(struct mtd_info *mtd)
3601 struct spi_nor *nor = mtd_to_spi_nor(mtd);
3602 struct device *dev = nor->dev;
3605 /* re-initialize the nor chip */
3606 ret = spi_nor_init(nor);
3608 dev_err(dev, "resume() failed\n");
3611 void spi_nor_restore(struct spi_nor *nor)
3613 /* restore the addressing mode */
3614 if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES) &&
3615 nor->flags & SNOR_F_BROKEN_RESET)
3616 nor->params.set_4byte_addr_mode(nor, false);
3618 EXPORT_SYMBOL_GPL(spi_nor_restore);
3620 static const struct flash_info *spi_nor_match_id(const char *name)
3622 const struct flash_info *id = spi_nor_ids;
3625 if (!strcmp(name, id->name))
3632 static int spi_nor_set_addr_width(struct spi_nor *nor)
3634 if (nor->addr_width) {
3635 /* already configured from SFDP */
3636 } else if (nor->info->addr_width) {
3637 nor->addr_width = nor->info->addr_width;
3638 } else if (nor->mtd.size > 0x1000000) {
3639 /* enable 4-byte addressing if the device exceeds 16MiB */
3640 nor->addr_width = 4;
3642 nor->addr_width = 3;
3645 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
3646 dev_dbg(nor->dev, "address width is too large: %u\n",
3651 /* Set 4byte opcodes when possible. */
3652 if (nor->addr_width == 4 && nor->flags & SNOR_F_4B_OPCODES &&
3653 !(nor->flags & SNOR_F_HAS_4BAIT))
3654 spi_nor_set_4byte_opcodes(nor);
3659 static void spi_nor_debugfs_init(struct spi_nor *nor,
3660 const struct flash_info *info)
3662 struct mtd_info *mtd = &nor->mtd;
3664 mtd->dbg.partname = info->name;
3665 mtd->dbg.partid = devm_kasprintf(nor->dev, GFP_KERNEL, "spi-nor:%*phN",
3666 info->id_len, info->id);
3669 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
3672 const struct flash_info *info = NULL;
3675 info = spi_nor_match_id(name);
3676 /* Try to auto-detect if chip name wasn't specified or not found */
3678 info = spi_nor_read_id(nor);
3679 if (IS_ERR_OR_NULL(info))
3680 return ERR_PTR(-ENOENT);
3683 * If caller has specified name of flash model that can normally be
3684 * detected using JEDEC, let's verify it.
3686 if (name && info->id_len) {
3687 const struct flash_info *jinfo;
3689 jinfo = spi_nor_read_id(nor);
3690 if (IS_ERR(jinfo)) {
3692 } else if (jinfo != info) {
3694 * JEDEC knows better, so overwrite platform ID. We
3695 * can't trust partitions any longer, but we'll let
3696 * mtd apply them anyway, since some partitions may be
3697 * marked read-only, and we don't want to lose that
3698 * information, even if it's not 100% accurate.
3700 dev_warn(nor->dev, "found %s, expected %s\n",
3701 jinfo->name, info->name);
3709 int spi_nor_scan(struct spi_nor *nor, const char *name,
3710 const struct spi_nor_hwcaps *hwcaps)
3712 const struct flash_info *info;
3713 struct device *dev = nor->dev;
3714 struct mtd_info *mtd = &nor->mtd;
3715 struct device_node *np = spi_nor_get_flash_node(nor);
3716 struct spi_nor_flash_parameter *params = &nor->params;
3720 ret = spi_nor_check(nor);
3724 /* Reset SPI protocol for all commands. */
3725 nor->reg_proto = SNOR_PROTO_1_1_1;
3726 nor->read_proto = SNOR_PROTO_1_1_1;
3727 nor->write_proto = SNOR_PROTO_1_1_1;
3730 * We need the bounce buffer early to read/write registers when going
3731 * through the spi-mem layer (buffers have to be DMA-able).
3732 * For spi-mem drivers, we'll reallocate a new buffer if
3733 * nor->page_size turns out to be greater than PAGE_SIZE (which
3734 * shouldn't happen before long since NOR pages are usually less
3735 * than 1KB) after spi_nor_scan() returns.
3737 nor->bouncebuf_size = PAGE_SIZE;
3738 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
3740 if (!nor->bouncebuf)
3743 info = spi_nor_get_flash_info(nor, name);
3745 return PTR_ERR(info);
3749 spi_nor_debugfs_init(nor, info);
3751 mutex_init(&nor->lock);
3754 * Make sure the XSR_RDY flag is set before calling
3755 * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
3756 * with Atmel spi-nor
3758 if (info->flags & SPI_NOR_XSR_RDY)
3759 nor->flags |= SNOR_F_READY_XSR_RDY;
3761 if (info->flags & SPI_NOR_HAS_LOCK)
3762 nor->flags |= SNOR_F_HAS_LOCK;
3764 /* Init flash parameters based on flash_info struct and SFDP */
3765 spi_nor_init_params(nor);
3768 mtd->name = dev_name(dev);
3770 mtd->type = MTD_NORFLASH;
3772 mtd->flags = MTD_CAP_NORFLASH;
3773 mtd->size = params->size;
3774 mtd->_erase = spi_nor_erase;
3775 mtd->_read = spi_nor_read;
3776 mtd->_resume = spi_nor_resume;
3778 if (nor->params.locking_ops) {
3779 mtd->_lock = spi_nor_lock;
3780 mtd->_unlock = spi_nor_unlock;
3781 mtd->_is_locked = spi_nor_is_locked;
3784 /* sst nor chips use AAI word program */
3785 if (info->flags & SST_WRITE)
3786 mtd->_write = sst_write;
3788 mtd->_write = spi_nor_write;
3790 if (info->flags & USE_FSR)
3791 nor->flags |= SNOR_F_USE_FSR;
3792 if (info->flags & SPI_NOR_HAS_TB) {
3793 nor->flags |= SNOR_F_HAS_SR_TB;
3794 if (info->flags & SPI_NOR_TB_SR_BIT6)
3795 nor->flags |= SNOR_F_HAS_SR_TB_BIT6;
3798 if (info->flags & NO_CHIP_ERASE)
3799 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
3800 if (info->flags & USE_CLSR)
3801 nor->flags |= SNOR_F_USE_CLSR;
3803 if (info->flags & SPI_NOR_NO_ERASE)
3804 mtd->flags |= MTD_NO_ERASE;
3806 mtd->dev.parent = dev;
3807 nor->page_size = params->page_size;
3808 mtd->writebufsize = nor->page_size;
3810 if (of_property_read_bool(np, "broken-flash-reset"))
3811 nor->flags |= SNOR_F_BROKEN_RESET;
3814 * Configure the SPI memory:
3815 * - select op codes for (Fast) Read, Page Program and Sector Erase.
3816 * - set the number of dummy cycles (mode cycles + wait states).
3817 * - set the SPI protocols for register and memory accesses.
3819 ret = spi_nor_setup(nor, hwcaps);
3823 if (info->flags & SPI_NOR_4B_OPCODES)
3824 nor->flags |= SNOR_F_4B_OPCODES;
3826 ret = spi_nor_set_addr_width(nor);
3830 /* Send all the required SPI flash commands to initialize device */
3831 ret = spi_nor_init(nor);
3835 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
3836 (long long)mtd->size >> 10);
3839 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
3840 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
3841 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
3842 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
3844 if (mtd->numeraseregions)
3845 for (i = 0; i < mtd->numeraseregions; i++)
3847 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
3848 ".erasesize = 0x%.8x (%uKiB), "
3849 ".numblocks = %d }\n",
3850 i, (long long)mtd->eraseregions[i].offset,
3851 mtd->eraseregions[i].erasesize,
3852 mtd->eraseregions[i].erasesize / 1024,
3853 mtd->eraseregions[i].numblocks);
3856 EXPORT_SYMBOL_GPL(spi_nor_scan);
3858 static int spi_nor_create_read_dirmap(struct spi_nor *nor)
3860 struct spi_mem_dirmap_info info = {
3861 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
3862 SPI_MEM_OP_ADDR(nor->addr_width, 0, 1),
3863 SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
3864 SPI_MEM_OP_DATA_IN(0, NULL, 1)),
3866 .length = nor->mtd.size,
3868 struct spi_mem_op *op = &info.op_tmpl;
3870 /* get transfer protocols. */
3871 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
3872 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
3873 op->dummy.buswidth = op->addr.buswidth;
3874 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
3876 /* convert the dummy cycles to the number of bytes */
3877 op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8;
3879 nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3881 return PTR_ERR_OR_ZERO(nor->dirmap.rdesc);
3884 static int spi_nor_create_write_dirmap(struct spi_nor *nor)
3886 struct spi_mem_dirmap_info info = {
3887 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
3888 SPI_MEM_OP_ADDR(nor->addr_width, 0, 1),
3889 SPI_MEM_OP_NO_DUMMY,
3890 SPI_MEM_OP_DATA_OUT(0, NULL, 1)),
3892 .length = nor->mtd.size,
3894 struct spi_mem_op *op = &info.op_tmpl;
3896 /* get transfer protocols. */
3897 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
3898 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
3899 op->dummy.buswidth = op->addr.buswidth;
3900 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
3902 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
3903 op->addr.nbytes = 0;
3905 nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3907 return PTR_ERR_OR_ZERO(nor->dirmap.wdesc);
3910 static int spi_nor_probe(struct spi_mem *spimem)
3912 struct spi_device *spi = spimem->spi;
3913 struct flash_platform_data *data = dev_get_platdata(&spi->dev);
3914 struct spi_nor *nor;
3916 * Enable all caps by default. The core will mask them after
3917 * checking what's really supported using spi_mem_supports_op().
3919 const struct spi_nor_hwcaps hwcaps = { .mask = SNOR_HWCAPS_ALL };
3923 nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL);
3927 nor->spimem = spimem;
3928 nor->dev = &spi->dev;
3929 spi_nor_set_flash_node(nor, spi->dev.of_node);
3931 spi_mem_set_drvdata(spimem, nor);
3933 if (data && data->name)
3934 nor->mtd.name = data->name;
3937 nor->mtd.name = spi_mem_get_name(spimem);
3940 * For some (historical?) reason many platforms provide two different
3941 * names in flash_platform_data: "name" and "type". Quite often name is
3942 * set to "m25p80" and then "type" provides a real chip name.
3943 * If that's the case, respect "type" and ignore a "name".
3945 if (data && data->type)
3946 flash_name = data->type;
3947 else if (!strcmp(spi->modalias, "spi-nor"))
3948 flash_name = NULL; /* auto-detect */
3950 flash_name = spi->modalias;
3952 ret = spi_nor_scan(nor, flash_name, &hwcaps);
3957 * None of the existing parts have > 512B pages, but let's play safe
3958 * and add this logic so that if anyone ever adds support for such
3959 * a NOR we don't end up with buffer overflows.
3961 if (nor->page_size > PAGE_SIZE) {
3962 nor->bouncebuf_size = nor->page_size;
3963 devm_kfree(nor->dev, nor->bouncebuf);
3964 nor->bouncebuf = devm_kmalloc(nor->dev,
3965 nor->bouncebuf_size,
3967 if (!nor->bouncebuf)
3971 ret = spi_nor_create_read_dirmap(nor);
3975 ret = spi_nor_create_write_dirmap(nor);
3979 return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
3980 data ? data->nr_parts : 0);
3983 static int spi_nor_remove(struct spi_mem *spimem)
3985 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3987 spi_nor_restore(nor);
3989 /* Clean up MTD stuff. */
3990 return mtd_device_unregister(&nor->mtd);
3993 static void spi_nor_shutdown(struct spi_mem *spimem)
3995 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3997 spi_nor_restore(nor);
4001 * Do NOT add to this array without reading the following:
4003 * Historically, many flash devices are bound to this driver by their name. But
4004 * since most of these flash are compatible to some extent, and their
4005 * differences can often be differentiated by the JEDEC read-ID command, we
4006 * encourage new users to add support to the spi-nor library, and simply bind
4007 * against a generic string here (e.g., "jedec,spi-nor").
4009 * Many flash names are kept here in this list (as well as in spi-nor.c) to
4010 * keep them available as module aliases for existing platforms.
4012 static const struct spi_device_id spi_nor_dev_ids[] = {
4014 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
4015 * hack around the fact that the SPI core does not provide uevent
4016 * matching for .of_match_table
4021 * Entries not used in DTs that should be safe to drop after replacing
4022 * them with "spi-nor" in platform data.
4024 {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
4027 * Entries that were used in DTs without "jedec,spi-nor" fallback and
4028 * should be kept for backward compatibility.
4030 {"at25df321a"}, {"at25df641"}, {"at26df081a"},
4031 {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"},
4032 {"mx25l25635e"},{"mx66l51235l"},
4033 {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"},
4034 {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"},
4036 {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
4037 {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"},
4038 {"m25p64"}, {"m25p128"},
4039 {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
4040 {"w25q80bl"}, {"w25q128"}, {"w25q256"},
4042 /* Flashes that can't be detected using JEDEC */
4043 {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
4044 {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
4045 {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
4047 /* Everspin MRAMs (non-JEDEC) */
4048 { "mr25h128" }, /* 128 Kib, 40 MHz */
4049 { "mr25h256" }, /* 256 Kib, 40 MHz */
4050 { "mr25h10" }, /* 1 Mib, 40 MHz */
4051 { "mr25h40" }, /* 4 Mib, 40 MHz */
4055 MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids);
4057 static const struct of_device_id spi_nor_of_table[] = {
4059 * Generic compatibility for SPI NOR that can be identified by the
4060 * JEDEC READ ID opcode (0x9F). Use this, if possible.
4062 { .compatible = "jedec,spi-nor" },
4065 MODULE_DEVICE_TABLE(of, spi_nor_of_table);
4068 * REVISIT: many of these chips have deep power-down modes, which
4069 * should clearly be entered on suspend() to minimize power use.
4070 * And also when they're otherwise idle...
4072 static struct spi_mem_driver spi_nor_driver = {
4076 .of_match_table = spi_nor_of_table,
4078 .id_table = spi_nor_dev_ids,
4080 .probe = spi_nor_probe,
4081 .remove = spi_nor_remove,
4082 .shutdown = spi_nor_shutdown,
4084 module_spi_mem_driver(spi_nor_driver);
4086 MODULE_LICENSE("GPL v2");
4087 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
4088 MODULE_AUTHOR("Mike Lavender");
4089 MODULE_DESCRIPTION("framework for SPI NOR");