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/delay.h>
13 #include <linux/device.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/spi-nor.h>
18 #include <linux/mutex.h>
19 #include <linux/of_platform.h>
20 #include <linux/sched/task_stack.h>
21 #include <linux/sizes.h>
22 #include <linux/slab.h>
23 #include <linux/spi/flash.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_NBYTES 4
43 #define SPI_NOR_SRST_SLEEP_MIN 200
44 #define SPI_NOR_SRST_SLEEP_MAX 400
47 * spi_nor_get_cmd_ext() - Get the command opcode extension based on the
49 * @nor: pointer to a 'struct spi_nor'
50 * @op: pointer to the 'struct spi_mem_op' whose properties
51 * need to be initialized.
53 * Right now, only "repeat" and "invert" are supported.
55 * Return: The opcode extension.
57 static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor,
58 const struct spi_mem_op *op)
60 switch (nor->cmd_ext_type) {
61 case SPI_NOR_EXT_INVERT:
62 return ~op->cmd.opcode;
64 case SPI_NOR_EXT_REPEAT:
65 return op->cmd.opcode;
68 dev_err(nor->dev, "Unknown command extension type\n");
74 * spi_nor_spimem_setup_op() - Set up common properties of a spi-mem op.
75 * @nor: pointer to a 'struct spi_nor'
76 * @op: pointer to the 'struct spi_mem_op' whose properties
77 * need to be initialized.
78 * @proto: the protocol from which the properties need to be set.
80 void spi_nor_spimem_setup_op(const struct spi_nor *nor,
81 struct spi_mem_op *op,
82 const enum spi_nor_protocol proto)
86 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
89 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
92 op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
95 op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
97 if (spi_nor_protocol_is_dtr(proto)) {
99 * SPIMEM supports mixed DTR modes, but right now we can only
100 * have all phases either DTR or STR. IOW, SPIMEM can have
101 * something like 4S-4D-4D, but SPI NOR can't. So, set all 4
102 * phases to either DTR or STR.
106 op->dummy.dtr = true;
109 /* 2 bytes per clock cycle in DTR mode. */
110 op->dummy.nbytes *= 2;
112 ext = spi_nor_get_cmd_ext(nor, op);
113 op->cmd.opcode = (op->cmd.opcode << 8) | ext;
119 * spi_nor_spimem_bounce() - check if a bounce buffer is needed for the data
121 * @nor: pointer to 'struct spi_nor'
122 * @op: pointer to 'struct spi_mem_op' template for transfer
124 * If we have to use the bounce buffer, the data field in @op will be updated.
126 * Return: true if the bounce buffer is needed, false if not
128 static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op)
130 /* op->data.buf.in occupies the same memory as op->data.buf.out */
131 if (object_is_on_stack(op->data.buf.in) ||
132 !virt_addr_valid(op->data.buf.in)) {
133 if (op->data.nbytes > nor->bouncebuf_size)
134 op->data.nbytes = nor->bouncebuf_size;
135 op->data.buf.in = nor->bouncebuf;
143 * spi_nor_spimem_exec_op() - execute a memory operation
144 * @nor: pointer to 'struct spi_nor'
145 * @op: pointer to 'struct spi_mem_op' template for transfer
147 * Return: 0 on success, -error otherwise.
149 static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op)
153 error = spi_mem_adjust_op_size(nor->spimem, op);
157 return spi_mem_exec_op(nor->spimem, op);
160 int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode,
163 if (spi_nor_protocol_is_dtr(nor->reg_proto))
166 return nor->controller_ops->read_reg(nor, opcode, buf, len);
169 int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode,
170 const u8 *buf, size_t len)
172 if (spi_nor_protocol_is_dtr(nor->reg_proto))
175 return nor->controller_ops->write_reg(nor, opcode, buf, len);
178 static int spi_nor_controller_ops_erase(struct spi_nor *nor, loff_t offs)
180 if (spi_nor_protocol_is_dtr(nor->reg_proto))
183 return nor->controller_ops->erase(nor, offs);
187 * spi_nor_spimem_read_data() - read data from flash's memory region via
189 * @nor: pointer to 'struct spi_nor'
190 * @from: offset to read from
191 * @len: number of bytes to read
192 * @buf: pointer to dst buffer
194 * Return: number of bytes read successfully, -errno otherwise
196 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from,
199 struct spi_mem_op op =
200 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
201 SPI_MEM_OP_ADDR(nor->addr_nbytes, from, 0),
202 SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
203 SPI_MEM_OP_DATA_IN(len, buf, 0));
208 spi_nor_spimem_setup_op(nor, &op, nor->read_proto);
210 /* convert the dummy cycles to the number of bytes */
211 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
212 if (spi_nor_protocol_is_dtr(nor->read_proto))
213 op.dummy.nbytes *= 2;
215 usebouncebuf = spi_nor_spimem_bounce(nor, &op);
217 if (nor->dirmap.rdesc) {
218 nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val,
219 op.data.nbytes, op.data.buf.in);
221 error = spi_nor_spimem_exec_op(nor, &op);
224 nbytes = op.data.nbytes;
227 if (usebouncebuf && nbytes > 0)
228 memcpy(buf, op.data.buf.in, nbytes);
234 * spi_nor_read_data() - read data from flash memory
235 * @nor: pointer to 'struct spi_nor'
236 * @from: offset to read from
237 * @len: number of bytes to read
238 * @buf: pointer to dst buffer
240 * Return: number of bytes read successfully, -errno otherwise
242 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf)
245 return spi_nor_spimem_read_data(nor, from, len, buf);
247 return nor->controller_ops->read(nor, from, len, buf);
251 * spi_nor_spimem_write_data() - write data to flash memory via
253 * @nor: pointer to 'struct spi_nor'
254 * @to: offset to write to
255 * @len: number of bytes to write
256 * @buf: pointer to src buffer
258 * Return: number of bytes written successfully, -errno otherwise
260 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to,
261 size_t len, const u8 *buf)
263 struct spi_mem_op op =
264 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
265 SPI_MEM_OP_ADDR(nor->addr_nbytes, to, 0),
267 SPI_MEM_OP_DATA_OUT(len, buf, 0));
271 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
274 spi_nor_spimem_setup_op(nor, &op, nor->write_proto);
276 if (spi_nor_spimem_bounce(nor, &op))
277 memcpy(nor->bouncebuf, buf, op.data.nbytes);
279 if (nor->dirmap.wdesc) {
280 nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val,
281 op.data.nbytes, op.data.buf.out);
283 error = spi_nor_spimem_exec_op(nor, &op);
286 nbytes = op.data.nbytes;
293 * spi_nor_write_data() - write data to flash memory
294 * @nor: pointer to 'struct spi_nor'
295 * @to: offset to write to
296 * @len: number of bytes to write
297 * @buf: pointer to src buffer
299 * Return: number of bytes written successfully, -errno otherwise
301 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
305 return spi_nor_spimem_write_data(nor, to, len, buf);
307 return nor->controller_ops->write(nor, to, len, buf);
311 * spi_nor_read_any_reg() - read any register from flash memory, nonvolatile or
313 * @nor: pointer to 'struct spi_nor'.
314 * @op: SPI memory operation. op->data.buf must be DMA-able.
315 * @proto: SPI protocol to use for the register operation.
317 * Return: zero on success, -errno otherwise
319 int spi_nor_read_any_reg(struct spi_nor *nor, struct spi_mem_op *op,
320 enum spi_nor_protocol proto)
325 spi_nor_spimem_setup_op(nor, op, proto);
326 return spi_nor_spimem_exec_op(nor, op);
330 * spi_nor_write_any_volatile_reg() - write any volatile register to flash
332 * @nor: pointer to 'struct spi_nor'
333 * @op: SPI memory operation. op->data.buf must be DMA-able.
334 * @proto: SPI protocol to use for the register operation.
336 * Writing volatile registers are instant according to some manufacturers
337 * (Cypress, Micron) and do not need any status polling.
339 * Return: zero on success, -errno otherwise
341 int spi_nor_write_any_volatile_reg(struct spi_nor *nor, struct spi_mem_op *op,
342 enum spi_nor_protocol proto)
349 ret = spi_nor_write_enable(nor);
352 spi_nor_spimem_setup_op(nor, op, proto);
353 return spi_nor_spimem_exec_op(nor, op);
357 * spi_nor_write_enable() - Set write enable latch with Write Enable command.
358 * @nor: pointer to 'struct spi_nor'.
360 * Return: 0 on success, -errno otherwise.
362 int spi_nor_write_enable(struct spi_nor *nor)
367 struct spi_mem_op op = SPI_NOR_WREN_OP;
369 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
371 ret = spi_mem_exec_op(nor->spimem, &op);
373 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WREN,
378 dev_dbg(nor->dev, "error %d on Write Enable\n", ret);
384 * spi_nor_write_disable() - Send Write Disable instruction to the chip.
385 * @nor: pointer to 'struct spi_nor'.
387 * Return: 0 on success, -errno otherwise.
389 int spi_nor_write_disable(struct spi_nor *nor)
394 struct spi_mem_op op = SPI_NOR_WRDI_OP;
396 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
398 ret = spi_mem_exec_op(nor->spimem, &op);
400 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRDI,
405 dev_dbg(nor->dev, "error %d on Write Disable\n", ret);
411 * spi_nor_read_id() - Read the JEDEC ID.
412 * @nor: pointer to 'struct spi_nor'.
413 * @naddr: number of address bytes to send. Can be zero if the operation
414 * does not need to send an address.
415 * @ndummy: number of dummy bytes to send after an opcode or address. Can
416 * be zero if the operation does not require dummy bytes.
417 * @id: pointer to a DMA-able buffer where the value of the JEDEC ID
419 * @proto: the SPI protocol for register operation.
421 * Return: 0 on success, -errno otherwise.
423 int spi_nor_read_id(struct spi_nor *nor, u8 naddr, u8 ndummy, u8 *id,
424 enum spi_nor_protocol proto)
429 struct spi_mem_op op =
430 SPI_NOR_READID_OP(naddr, ndummy, id, SPI_NOR_MAX_ID_LEN);
432 spi_nor_spimem_setup_op(nor, &op, proto);
433 ret = spi_mem_exec_op(nor->spimem, &op);
435 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id,
442 * spi_nor_read_sr() - Read the Status Register.
443 * @nor: pointer to 'struct spi_nor'.
444 * @sr: pointer to a DMA-able buffer where the value of the
445 * Status Register will be written. Should be at least 2 bytes.
447 * Return: 0 on success, -errno otherwise.
449 int spi_nor_read_sr(struct spi_nor *nor, u8 *sr)
454 struct spi_mem_op op = SPI_NOR_RDSR_OP(sr);
456 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
457 op.addr.nbytes = nor->params->rdsr_addr_nbytes;
458 op.dummy.nbytes = nor->params->rdsr_dummy;
460 * We don't want to read only one byte in DTR mode. So,
461 * read 2 and then discard the second byte.
466 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
468 ret = spi_mem_exec_op(nor->spimem, &op);
470 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR, sr,
475 dev_dbg(nor->dev, "error %d reading SR\n", ret);
481 * spi_nor_read_cr() - Read the Configuration Register using the
482 * SPINOR_OP_RDCR (35h) command.
483 * @nor: pointer to 'struct spi_nor'
484 * @cr: pointer to a DMA-able buffer where the value of the
485 * Configuration Register will be written.
487 * Return: 0 on success, -errno otherwise.
489 int spi_nor_read_cr(struct spi_nor *nor, u8 *cr)
494 struct spi_mem_op op = SPI_NOR_RDCR_OP(cr);
496 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
498 ret = spi_mem_exec_op(nor->spimem, &op);
500 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDCR, cr,
505 dev_dbg(nor->dev, "error %d reading CR\n", ret);
511 * spi_nor_set_4byte_addr_mode() - Enter/Exit 4-byte address mode.
512 * @nor: pointer to 'struct spi_nor'.
513 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
516 * Return: 0 on success, -errno otherwise.
518 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
523 struct spi_mem_op op = SPI_NOR_EN4B_EX4B_OP(enable);
525 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
527 ret = spi_mem_exec_op(nor->spimem, &op);
529 ret = spi_nor_controller_ops_write_reg(nor,
530 enable ? SPINOR_OP_EN4B :
536 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
542 * spansion_set_4byte_addr_mode() - Set 4-byte address mode for Spansion
544 * @nor: pointer to 'struct spi_nor'.
545 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
548 * Return: 0 on success, -errno otherwise.
550 static int spansion_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
554 nor->bouncebuf[0] = enable << 7;
557 struct spi_mem_op op = SPI_NOR_BRWR_OP(nor->bouncebuf);
559 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
561 ret = spi_mem_exec_op(nor->spimem, &op);
563 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_BRWR,
568 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
574 * spi_nor_sr_ready() - Query the Status Register to see if the flash is ready
576 * @nor: pointer to 'struct spi_nor'.
578 * Return: 1 if ready, 0 if not ready, -errno on errors.
580 int spi_nor_sr_ready(struct spi_nor *nor)
584 ret = spi_nor_read_sr(nor, nor->bouncebuf);
588 return !(nor->bouncebuf[0] & SR_WIP);
592 * spi_nor_ready() - Query the flash to see if it is ready for new commands.
593 * @nor: pointer to 'struct spi_nor'.
595 * Return: 1 if ready, 0 if not ready, -errno on errors.
597 static int spi_nor_ready(struct spi_nor *nor)
599 /* Flashes might override the standard routine. */
600 if (nor->params->ready)
601 return nor->params->ready(nor);
603 return spi_nor_sr_ready(nor);
607 * spi_nor_wait_till_ready_with_timeout() - Service routine to read the
608 * Status Register until ready, or timeout occurs.
609 * @nor: pointer to "struct spi_nor".
610 * @timeout_jiffies: jiffies to wait until timeout.
612 * Return: 0 on success, -errno otherwise.
614 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
615 unsigned long timeout_jiffies)
617 unsigned long deadline;
618 int timeout = 0, ret;
620 deadline = jiffies + timeout_jiffies;
623 if (time_after_eq(jiffies, deadline))
626 ret = spi_nor_ready(nor);
635 dev_dbg(nor->dev, "flash operation timed out\n");
641 * spi_nor_wait_till_ready() - Wait for a predefined amount of time for the
642 * flash to be ready, or timeout occurs.
643 * @nor: pointer to "struct spi_nor".
645 * Return: 0 on success, -errno otherwise.
647 int spi_nor_wait_till_ready(struct spi_nor *nor)
649 return spi_nor_wait_till_ready_with_timeout(nor,
650 DEFAULT_READY_WAIT_JIFFIES);
654 * spi_nor_global_block_unlock() - Unlock Global Block Protection.
655 * @nor: pointer to 'struct spi_nor'.
657 * Return: 0 on success, -errno otherwise.
659 int spi_nor_global_block_unlock(struct spi_nor *nor)
663 ret = spi_nor_write_enable(nor);
668 struct spi_mem_op op = SPI_NOR_GBULK_OP;
670 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
672 ret = spi_mem_exec_op(nor->spimem, &op);
674 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_GBULK,
679 dev_dbg(nor->dev, "error %d on Global Block Unlock\n", ret);
683 return spi_nor_wait_till_ready(nor);
687 * spi_nor_write_sr() - Write the Status Register.
688 * @nor: pointer to 'struct spi_nor'.
689 * @sr: pointer to DMA-able buffer to write to the Status Register.
690 * @len: number of bytes to write to the Status Register.
692 * Return: 0 on success, -errno otherwise.
694 int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len)
698 ret = spi_nor_write_enable(nor);
703 struct spi_mem_op op = SPI_NOR_WRSR_OP(sr, len);
705 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
707 ret = spi_mem_exec_op(nor->spimem, &op);
709 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR, sr,
714 dev_dbg(nor->dev, "error %d writing SR\n", ret);
718 return spi_nor_wait_till_ready(nor);
722 * spi_nor_write_sr1_and_check() - Write one byte to the Status Register 1 and
723 * ensure that the byte written match the received value.
724 * @nor: pointer to a 'struct spi_nor'.
725 * @sr1: byte value to be written to the Status Register.
727 * Return: 0 on success, -errno otherwise.
729 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1)
733 nor->bouncebuf[0] = sr1;
735 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1);
739 ret = spi_nor_read_sr(nor, nor->bouncebuf);
743 if (nor->bouncebuf[0] != sr1) {
744 dev_dbg(nor->dev, "SR1: read back test failed\n");
752 * spi_nor_write_16bit_sr_and_check() - Write the Status Register 1 and the
753 * Status Register 2 in one shot. Ensure that the byte written in the Status
754 * Register 1 match the received value, and that the 16-bit Write did not
755 * affect what was already in the Status Register 2.
756 * @nor: pointer to a 'struct spi_nor'.
757 * @sr1: byte value to be written to the Status Register 1.
759 * Return: 0 on success, -errno otherwise.
761 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1)
764 u8 *sr_cr = nor->bouncebuf;
767 /* Make sure we don't overwrite the contents of Status Register 2. */
768 if (!(nor->flags & SNOR_F_NO_READ_CR)) {
769 ret = spi_nor_read_cr(nor, &sr_cr[1]);
772 } else if (nor->params->quad_enable) {
774 * If the Status Register 2 Read command (35h) is not
775 * supported, we should at least be sure we don't
776 * change the value of the SR2 Quad Enable bit.
778 * We can safely assume that when the Quad Enable method is
779 * set, the value of the QE bit is one, as a consequence of the
780 * nor->params->quad_enable() call.
782 * We can safely assume that the Quad Enable bit is present in
783 * the Status Register 2 at BIT(1). According to the JESD216
784 * revB standard, BFPT DWORDS[15], bits 22:20, the 16-bit
785 * Write Status (01h) command is available just for the cases
786 * in which the QE bit is described in SR2 at BIT(1).
788 sr_cr[1] = SR2_QUAD_EN_BIT1;
795 ret = spi_nor_write_sr(nor, sr_cr, 2);
799 ret = spi_nor_read_sr(nor, sr_cr);
803 if (sr1 != sr_cr[0]) {
804 dev_dbg(nor->dev, "SR: Read back test failed\n");
808 if (nor->flags & SNOR_F_NO_READ_CR)
811 cr_written = sr_cr[1];
813 ret = spi_nor_read_cr(nor, &sr_cr[1]);
817 if (cr_written != sr_cr[1]) {
818 dev_dbg(nor->dev, "CR: read back test failed\n");
826 * spi_nor_write_16bit_cr_and_check() - Write the Status Register 1 and the
827 * Configuration Register in one shot. Ensure that the byte written in the
828 * Configuration Register match the received value, and that the 16-bit Write
829 * did not affect what was already in the Status Register 1.
830 * @nor: pointer to a 'struct spi_nor'.
831 * @cr: byte value to be written to the Configuration Register.
833 * Return: 0 on success, -errno otherwise.
835 int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr)
838 u8 *sr_cr = nor->bouncebuf;
841 /* Keep the current value of the Status Register 1. */
842 ret = spi_nor_read_sr(nor, sr_cr);
848 ret = spi_nor_write_sr(nor, sr_cr, 2);
852 sr_written = sr_cr[0];
854 ret = spi_nor_read_sr(nor, sr_cr);
858 if (sr_written != sr_cr[0]) {
859 dev_dbg(nor->dev, "SR: Read back test failed\n");
863 if (nor->flags & SNOR_F_NO_READ_CR)
866 ret = spi_nor_read_cr(nor, &sr_cr[1]);
870 if (cr != sr_cr[1]) {
871 dev_dbg(nor->dev, "CR: read back test failed\n");
879 * spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that
880 * the byte written match the received value without affecting other bits in the
881 * Status Register 1 and 2.
882 * @nor: pointer to a 'struct spi_nor'.
883 * @sr1: byte value to be written to the Status Register.
885 * Return: 0 on success, -errno otherwise.
887 int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1)
889 if (nor->flags & SNOR_F_HAS_16BIT_SR)
890 return spi_nor_write_16bit_sr_and_check(nor, sr1);
892 return spi_nor_write_sr1_and_check(nor, sr1);
896 * spi_nor_write_sr2() - Write the Status Register 2 using the
897 * SPINOR_OP_WRSR2 (3eh) command.
898 * @nor: pointer to 'struct spi_nor'.
899 * @sr2: pointer to DMA-able buffer to write to the Status Register 2.
901 * Return: 0 on success, -errno otherwise.
903 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2)
907 ret = spi_nor_write_enable(nor);
912 struct spi_mem_op op = SPI_NOR_WRSR2_OP(sr2);
914 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
916 ret = spi_mem_exec_op(nor->spimem, &op);
918 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR2,
923 dev_dbg(nor->dev, "error %d writing SR2\n", ret);
927 return spi_nor_wait_till_ready(nor);
931 * spi_nor_read_sr2() - Read the Status Register 2 using the
932 * SPINOR_OP_RDSR2 (3fh) command.
933 * @nor: pointer to 'struct spi_nor'.
934 * @sr2: pointer to DMA-able buffer where the value of the
935 * Status Register 2 will be written.
937 * Return: 0 on success, -errno otherwise.
939 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2)
944 struct spi_mem_op op = SPI_NOR_RDSR2_OP(sr2);
946 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
948 ret = spi_mem_exec_op(nor->spimem, &op);
950 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR2, sr2,
955 dev_dbg(nor->dev, "error %d reading SR2\n", ret);
961 * spi_nor_erase_chip() - Erase the entire flash memory.
962 * @nor: pointer to 'struct spi_nor'.
964 * Return: 0 on success, -errno otherwise.
966 static int spi_nor_erase_chip(struct spi_nor *nor)
970 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
973 struct spi_mem_op op = SPI_NOR_CHIP_ERASE_OP;
975 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
977 ret = spi_mem_exec_op(nor->spimem, &op);
979 ret = spi_nor_controller_ops_write_reg(nor,
980 SPINOR_OP_CHIP_ERASE,
985 dev_dbg(nor->dev, "error %d erasing chip\n", ret);
990 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
994 for (i = 0; i < size; i++)
995 if (table[i][0] == opcode)
998 /* No conversion found, keep input op code. */
1002 u8 spi_nor_convert_3to4_read(u8 opcode)
1004 static const u8 spi_nor_3to4_read[][2] = {
1005 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
1006 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
1007 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
1008 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
1009 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
1010 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
1011 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
1012 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
1014 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
1015 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
1016 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
1019 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
1020 ARRAY_SIZE(spi_nor_3to4_read));
1023 static u8 spi_nor_convert_3to4_program(u8 opcode)
1025 static const u8 spi_nor_3to4_program[][2] = {
1026 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
1027 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
1028 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
1029 { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B },
1030 { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B },
1033 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
1034 ARRAY_SIZE(spi_nor_3to4_program));
1037 static u8 spi_nor_convert_3to4_erase(u8 opcode)
1039 static const u8 spi_nor_3to4_erase[][2] = {
1040 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
1041 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
1042 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
1045 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
1046 ARRAY_SIZE(spi_nor_3to4_erase));
1049 static bool spi_nor_has_uniform_erase(const struct spi_nor *nor)
1051 return !!nor->params->erase_map.uniform_erase_type;
1054 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor)
1056 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
1057 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
1058 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
1060 if (!spi_nor_has_uniform_erase(nor)) {
1061 struct spi_nor_erase_map *map = &nor->params->erase_map;
1062 struct spi_nor_erase_type *erase;
1065 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1066 erase = &map->erase_type[i];
1068 spi_nor_convert_3to4_erase(erase->opcode);
1073 int spi_nor_lock_and_prep(struct spi_nor *nor)
1077 mutex_lock(&nor->lock);
1079 if (nor->controller_ops && nor->controller_ops->prepare) {
1080 ret = nor->controller_ops->prepare(nor);
1082 mutex_unlock(&nor->lock);
1089 void spi_nor_unlock_and_unprep(struct spi_nor *nor)
1091 if (nor->controller_ops && nor->controller_ops->unprepare)
1092 nor->controller_ops->unprepare(nor);
1093 mutex_unlock(&nor->lock);
1096 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)
1098 if (!nor->params->convert_addr)
1101 return nor->params->convert_addr(nor, addr);
1105 * Initiate the erasure of a single sector
1107 int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
1111 addr = spi_nor_convert_addr(nor, addr);
1114 struct spi_mem_op op =
1115 SPI_NOR_SECTOR_ERASE_OP(nor->erase_opcode,
1116 nor->addr_nbytes, addr);
1118 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
1120 return spi_mem_exec_op(nor->spimem, &op);
1121 } else if (nor->controller_ops->erase) {
1122 return spi_nor_controller_ops_erase(nor, addr);
1126 * Default implementation, if driver doesn't have a specialized HW
1129 for (i = nor->addr_nbytes - 1; i >= 0; i--) {
1130 nor->bouncebuf[i] = addr & 0xff;
1134 return spi_nor_controller_ops_write_reg(nor, nor->erase_opcode,
1135 nor->bouncebuf, nor->addr_nbytes);
1139 * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
1140 * @erase: pointer to a structure that describes a SPI NOR erase type
1141 * @dividend: dividend value
1142 * @remainder: pointer to u32 remainder (will be updated)
1144 * Return: the result of the division
1146 static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase,
1147 u64 dividend, u32 *remainder)
1149 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
1150 *remainder = (u32)dividend & erase->size_mask;
1151 return dividend >> erase->size_shift;
1155 * spi_nor_find_best_erase_type() - find the best erase type for the given
1156 * offset in the serial flash memory and the
1157 * number of bytes to erase. The region in
1158 * which the address fits is expected to be
1160 * @map: the erase map of the SPI NOR
1161 * @region: pointer to a structure that describes a SPI NOR erase region
1162 * @addr: offset in the serial flash memory
1163 * @len: number of bytes to erase
1165 * Return: a pointer to the best fitted erase type, NULL otherwise.
1167 static const struct spi_nor_erase_type *
1168 spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
1169 const struct spi_nor_erase_region *region,
1172 const struct spi_nor_erase_type *erase;
1175 u8 erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
1178 * Erase types are ordered by size, with the smallest erase type at
1181 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
1182 /* Does the erase region support the tested erase type? */
1183 if (!(erase_mask & BIT(i)))
1186 erase = &map->erase_type[i];
1190 /* Alignment is not mandatory for overlaid regions */
1191 if (region->offset & SNOR_OVERLAID_REGION &&
1192 region->size <= len)
1195 /* Don't erase more than what the user has asked for. */
1196 if (erase->size > len)
1199 spi_nor_div_by_erase_size(erase, addr, &rem);
1207 static u64 spi_nor_region_is_last(const struct spi_nor_erase_region *region)
1209 return region->offset & SNOR_LAST_REGION;
1212 static u64 spi_nor_region_end(const struct spi_nor_erase_region *region)
1214 return (region->offset & ~SNOR_ERASE_FLAGS_MASK) + region->size;
1218 * spi_nor_region_next() - get the next spi nor region
1219 * @region: pointer to a structure that describes a SPI NOR erase region
1221 * Return: the next spi nor region or NULL if last region.
1223 struct spi_nor_erase_region *
1224 spi_nor_region_next(struct spi_nor_erase_region *region)
1226 if (spi_nor_region_is_last(region))
1233 * spi_nor_find_erase_region() - find the region of the serial flash memory in
1234 * which the offset fits
1235 * @map: the erase map of the SPI NOR
1236 * @addr: offset in the serial flash memory
1238 * Return: a pointer to the spi_nor_erase_region struct, ERR_PTR(-errno)
1241 static struct spi_nor_erase_region *
1242 spi_nor_find_erase_region(const struct spi_nor_erase_map *map, u64 addr)
1244 struct spi_nor_erase_region *region = map->regions;
1245 u64 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1246 u64 region_end = region_start + region->size;
1248 while (addr < region_start || addr >= region_end) {
1249 region = spi_nor_region_next(region);
1251 return ERR_PTR(-EINVAL);
1253 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1254 region_end = region_start + region->size;
1261 * spi_nor_init_erase_cmd() - initialize an erase command
1262 * @region: pointer to a structure that describes a SPI NOR erase region
1263 * @erase: pointer to a structure that describes a SPI NOR erase type
1265 * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
1268 static struct spi_nor_erase_command *
1269 spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region,
1270 const struct spi_nor_erase_type *erase)
1272 struct spi_nor_erase_command *cmd;
1274 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1276 return ERR_PTR(-ENOMEM);
1278 INIT_LIST_HEAD(&cmd->list);
1279 cmd->opcode = erase->opcode;
1282 if (region->offset & SNOR_OVERLAID_REGION)
1283 cmd->size = region->size;
1285 cmd->size = erase->size;
1291 * spi_nor_destroy_erase_cmd_list() - destroy erase command list
1292 * @erase_list: list of erase commands
1294 static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list)
1296 struct spi_nor_erase_command *cmd, *next;
1298 list_for_each_entry_safe(cmd, next, erase_list, list) {
1299 list_del(&cmd->list);
1305 * spi_nor_init_erase_cmd_list() - initialize erase command list
1306 * @nor: pointer to a 'struct spi_nor'
1307 * @erase_list: list of erase commands to be executed once we validate that the
1308 * erase can be performed
1309 * @addr: offset in the serial flash memory
1310 * @len: number of bytes to erase
1312 * Builds the list of best fitted erase commands and verifies if the erase can
1315 * Return: 0 on success, -errno otherwise.
1317 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
1318 struct list_head *erase_list,
1321 const struct spi_nor_erase_map *map = &nor->params->erase_map;
1322 const struct spi_nor_erase_type *erase, *prev_erase = NULL;
1323 struct spi_nor_erase_region *region;
1324 struct spi_nor_erase_command *cmd = NULL;
1328 region = spi_nor_find_erase_region(map, addr);
1330 return PTR_ERR(region);
1332 region_end = spi_nor_region_end(region);
1335 erase = spi_nor_find_best_erase_type(map, region, addr, len);
1337 goto destroy_erase_cmd_list;
1339 if (prev_erase != erase ||
1340 erase->size != cmd->size ||
1341 region->offset & SNOR_OVERLAID_REGION) {
1342 cmd = spi_nor_init_erase_cmd(region, erase);
1345 goto destroy_erase_cmd_list;
1348 list_add_tail(&cmd->list, erase_list);
1356 if (len && addr >= region_end) {
1357 region = spi_nor_region_next(region);
1359 goto destroy_erase_cmd_list;
1360 region_end = spi_nor_region_end(region);
1368 destroy_erase_cmd_list:
1369 spi_nor_destroy_erase_cmd_list(erase_list);
1374 * spi_nor_erase_multi_sectors() - perform a non-uniform erase
1375 * @nor: pointer to a 'struct spi_nor'
1376 * @addr: offset in the serial flash memory
1377 * @len: number of bytes to erase
1379 * Build a list of best fitted erase commands and execute it once we validate
1380 * that the erase can be performed.
1382 * Return: 0 on success, -errno otherwise.
1384 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
1386 LIST_HEAD(erase_list);
1387 struct spi_nor_erase_command *cmd, *next;
1390 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len);
1394 list_for_each_entry_safe(cmd, next, &erase_list, list) {
1395 nor->erase_opcode = cmd->opcode;
1396 while (cmd->count) {
1397 dev_vdbg(nor->dev, "erase_cmd->size = 0x%08x, erase_cmd->opcode = 0x%02x, erase_cmd->count = %u\n",
1398 cmd->size, cmd->opcode, cmd->count);
1400 ret = spi_nor_write_enable(nor);
1402 goto destroy_erase_cmd_list;
1404 ret = spi_nor_erase_sector(nor, addr);
1406 goto destroy_erase_cmd_list;
1408 ret = spi_nor_wait_till_ready(nor);
1410 goto destroy_erase_cmd_list;
1415 list_del(&cmd->list);
1421 destroy_erase_cmd_list:
1422 spi_nor_destroy_erase_cmd_list(&erase_list);
1427 * Erase an address range on the nor chip. The address range may extend
1428 * one or more erase sectors. Return an error if there is a problem erasing.
1430 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
1432 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1437 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
1438 (long long)instr->len);
1440 if (spi_nor_has_uniform_erase(nor)) {
1441 div_u64_rem(instr->len, mtd->erasesize, &rem);
1449 ret = spi_nor_lock_and_prep(nor);
1453 /* whole-chip erase? */
1454 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
1455 unsigned long timeout;
1457 ret = spi_nor_write_enable(nor);
1461 ret = spi_nor_erase_chip(nor);
1466 * Scale the timeout linearly with the size of the flash, with
1467 * a minimum calibrated to an old 2MB flash. We could try to
1468 * pull these from CFI/SFDP, but these values should be good
1471 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
1472 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
1473 (unsigned long)(mtd->size / SZ_2M));
1474 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
1478 /* REVISIT in some cases we could speed up erasing large regions
1479 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
1480 * to use "small sector erase", but that's not always optimal.
1483 /* "sector"-at-a-time erase */
1484 } else if (spi_nor_has_uniform_erase(nor)) {
1486 ret = spi_nor_write_enable(nor);
1490 ret = spi_nor_erase_sector(nor, addr);
1494 ret = spi_nor_wait_till_ready(nor);
1498 addr += mtd->erasesize;
1499 len -= mtd->erasesize;
1502 /* erase multiple sectors */
1504 ret = spi_nor_erase_multi_sectors(nor, addr, len);
1509 ret = spi_nor_write_disable(nor);
1512 spi_nor_unlock_and_unprep(nor);
1518 * spi_nor_sr1_bit6_quad_enable() - Set the Quad Enable BIT(6) in the Status
1520 * @nor: pointer to a 'struct spi_nor'
1522 * Bit 6 of the Status Register 1 is the QE bit for Macronix like QSPI memories.
1524 * Return: 0 on success, -errno otherwise.
1526 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor)
1530 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1534 if (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6)
1537 nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6;
1539 return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]);
1543 * spi_nor_sr2_bit1_quad_enable() - set the Quad Enable BIT(1) in the Status
1545 * @nor: pointer to a 'struct spi_nor'.
1547 * Bit 1 of the Status Register 2 is the QE bit for Spansion like QSPI memories.
1549 * Return: 0 on success, -errno otherwise.
1551 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor)
1555 if (nor->flags & SNOR_F_NO_READ_CR)
1556 return spi_nor_write_16bit_cr_and_check(nor, SR2_QUAD_EN_BIT1);
1558 ret = spi_nor_read_cr(nor, nor->bouncebuf);
1562 if (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1)
1565 nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1;
1567 return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]);
1571 * spi_nor_sr2_bit7_quad_enable() - set QE bit in Status Register 2.
1572 * @nor: pointer to a 'struct spi_nor'
1574 * Set the Quad Enable (QE) bit in the Status Register 2.
1576 * This is one of the procedures to set the QE bit described in the SFDP
1577 * (JESD216 rev B) specification but no manufacturer using this procedure has
1578 * been identified yet, hence the name of the function.
1580 * Return: 0 on success, -errno otherwise.
1582 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor)
1584 u8 *sr2 = nor->bouncebuf;
1588 /* Check current Quad Enable bit value. */
1589 ret = spi_nor_read_sr2(nor, sr2);
1592 if (*sr2 & SR2_QUAD_EN_BIT7)
1595 /* Update the Quad Enable bit. */
1596 *sr2 |= SR2_QUAD_EN_BIT7;
1598 ret = spi_nor_write_sr2(nor, sr2);
1604 /* Read back and check it. */
1605 ret = spi_nor_read_sr2(nor, sr2);
1609 if (*sr2 != sr2_written) {
1610 dev_dbg(nor->dev, "SR2: Read back test failed\n");
1617 static const struct spi_nor_manufacturer *manufacturers[] = {
1624 &spi_nor_gigadevice,
1637 static const struct flash_info spi_nor_generic_flash = {
1638 .name = "spi-nor-generic",
1640 * JESD216 rev A doesn't specify the page size, therefore we need a
1647 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
1650 const struct flash_info *part;
1653 for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
1654 for (j = 0; j < manufacturers[i]->nparts; j++) {
1655 part = &manufacturers[i]->parts[j];
1657 !memcmp(part->id, id, part->id_len)) {
1658 nor->manufacturer = manufacturers[i];
1667 static const struct flash_info *spi_nor_detect(struct spi_nor *nor)
1669 const struct flash_info *info;
1670 u8 *id = nor->bouncebuf;
1673 ret = spi_nor_read_id(nor, 0, 0, id, nor->reg_proto);
1675 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret);
1676 return ERR_PTR(ret);
1679 /* Cache the complete flash ID. */
1680 nor->id = devm_kmemdup(nor->dev, id, SPI_NOR_MAX_ID_LEN, GFP_KERNEL);
1682 return ERR_PTR(-ENOMEM);
1684 info = spi_nor_match_id(nor, id);
1686 /* Fallback to a generic flash described only by its SFDP data. */
1688 ret = spi_nor_check_sfdp_signature(nor);
1690 info = &spi_nor_generic_flash;
1694 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
1695 SPI_NOR_MAX_ID_LEN, id);
1696 return ERR_PTR(-ENODEV);
1701 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1702 size_t *retlen, u_char *buf)
1704 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1707 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1709 ret = spi_nor_lock_and_prep(nor);
1716 addr = spi_nor_convert_addr(nor, addr);
1718 ret = spi_nor_read_data(nor, addr, len, buf);
1720 /* We shouldn't see 0-length reads */
1736 spi_nor_unlock_and_unprep(nor);
1741 * Write an address range to the nor chip. Data must be written in
1742 * FLASH_PAGESIZE chunks. The address range may be any size provided
1743 * it is within the physical boundaries.
1745 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1746 size_t *retlen, const u_char *buf)
1748 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1749 size_t page_offset, page_remain, i;
1751 u32 page_size = nor->params->page_size;
1753 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1755 ret = spi_nor_lock_and_prep(nor);
1759 for (i = 0; i < len; ) {
1761 loff_t addr = to + i;
1764 * If page_size is a power of two, the offset can be quickly
1765 * calculated with an AND operation. On the other cases we
1766 * need to do a modulus operation (more expensive).
1768 if (is_power_of_2(page_size)) {
1769 page_offset = addr & (page_size - 1);
1771 uint64_t aux = addr;
1773 page_offset = do_div(aux, page_size);
1775 /* the size of data remaining on the first page */
1776 page_remain = min_t(size_t, page_size - page_offset, len - i);
1778 addr = spi_nor_convert_addr(nor, addr);
1780 ret = spi_nor_write_enable(nor);
1784 ret = spi_nor_write_data(nor, addr, page_remain, buf + i);
1789 ret = spi_nor_wait_till_ready(nor);
1797 spi_nor_unlock_and_unprep(nor);
1801 static int spi_nor_check(struct spi_nor *nor)
1804 (!nor->spimem && !nor->controller_ops) ||
1805 (!nor->spimem && nor->controller_ops &&
1806 (!nor->controller_ops->read ||
1807 !nor->controller_ops->write ||
1808 !nor->controller_ops->read_reg ||
1809 !nor->controller_ops->write_reg))) {
1810 pr_err("spi-nor: please fill all the necessary fields!\n");
1814 if (nor->spimem && nor->controller_ops) {
1815 dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one of them.\n");
1823 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1827 enum spi_nor_protocol proto)
1829 read->num_mode_clocks = num_mode_clocks;
1830 read->num_wait_states = num_wait_states;
1831 read->opcode = opcode;
1832 read->proto = proto;
1835 void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
1836 enum spi_nor_protocol proto)
1838 pp->opcode = opcode;
1842 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
1846 for (i = 0; i < size; i++)
1847 if (table[i][0] == (int)hwcaps)
1853 int spi_nor_hwcaps_read2cmd(u32 hwcaps)
1855 static const int hwcaps_read2cmd[][2] = {
1856 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
1857 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
1858 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
1859 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
1860 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
1861 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
1862 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
1863 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
1864 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
1865 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
1866 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
1867 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
1868 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
1869 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
1870 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
1871 { SNOR_HWCAPS_READ_8_8_8_DTR, SNOR_CMD_READ_8_8_8_DTR },
1874 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
1875 ARRAY_SIZE(hwcaps_read2cmd));
1878 int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
1880 static const int hwcaps_pp2cmd[][2] = {
1881 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
1882 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
1883 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
1884 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
1885 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
1886 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
1887 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
1888 { SNOR_HWCAPS_PP_8_8_8_DTR, SNOR_CMD_PP_8_8_8_DTR },
1891 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
1892 ARRAY_SIZE(hwcaps_pp2cmd));
1896 * spi_nor_spimem_check_op - check if the operation is supported
1898 *@nor: pointer to a 'struct spi_nor'
1899 *@op: pointer to op template to be checked
1901 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
1903 static int spi_nor_spimem_check_op(struct spi_nor *nor,
1904 struct spi_mem_op *op)
1907 * First test with 4 address bytes. The opcode itself might
1908 * be a 3B addressing opcode but we don't care, because
1909 * SPI controller implementation should not check the opcode,
1910 * but just the sequence.
1912 op->addr.nbytes = 4;
1913 if (!spi_mem_supports_op(nor->spimem, op)) {
1914 if (nor->params->size > SZ_16M)
1917 /* If flash size <= 16MB, 3 address bytes are sufficient */
1918 op->addr.nbytes = 3;
1919 if (!spi_mem_supports_op(nor->spimem, op))
1927 * spi_nor_spimem_check_readop - check if the read op is supported
1929 *@nor: pointer to a 'struct spi_nor'
1930 *@read: pointer to op template to be checked
1932 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
1934 static int spi_nor_spimem_check_readop(struct spi_nor *nor,
1935 const struct spi_nor_read_command *read)
1937 struct spi_mem_op op = SPI_NOR_READ_OP(read->opcode);
1939 spi_nor_spimem_setup_op(nor, &op, read->proto);
1941 /* convert the dummy cycles to the number of bytes */
1942 op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
1943 op.dummy.buswidth / 8;
1944 if (spi_nor_protocol_is_dtr(nor->read_proto))
1945 op.dummy.nbytes *= 2;
1947 return spi_nor_spimem_check_op(nor, &op);
1951 * spi_nor_spimem_check_pp - check if the page program op is supported
1953 *@nor: pointer to a 'struct spi_nor'
1954 *@pp: pointer to op template to be checked
1956 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
1958 static int spi_nor_spimem_check_pp(struct spi_nor *nor,
1959 const struct spi_nor_pp_command *pp)
1961 struct spi_mem_op op = SPI_NOR_PP_OP(pp->opcode);
1963 spi_nor_spimem_setup_op(nor, &op, pp->proto);
1965 return spi_nor_spimem_check_op(nor, &op);
1969 * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
1970 * based on SPI controller capabilities
1971 * @nor: pointer to a 'struct spi_nor'
1972 * @hwcaps: pointer to resulting capabilities after adjusting
1973 * according to controller and flash's capability
1976 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps)
1978 struct spi_nor_flash_parameter *params = nor->params;
1981 /* X-X-X modes are not supported yet, mask them all. */
1982 *hwcaps &= ~SNOR_HWCAPS_X_X_X;
1985 * If the reset line is broken, we do not want to enter a stateful
1988 if (nor->flags & SNOR_F_BROKEN_RESET)
1989 *hwcaps &= ~(SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR);
1991 for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
1994 if (!(*hwcaps & BIT(cap)))
1997 rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
1999 spi_nor_spimem_check_readop(nor, ¶ms->reads[rdidx]))
2000 *hwcaps &= ~BIT(cap);
2002 ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
2006 if (spi_nor_spimem_check_pp(nor,
2007 ¶ms->page_programs[ppidx]))
2008 *hwcaps &= ~BIT(cap);
2013 * spi_nor_set_erase_type() - set a SPI NOR erase type
2014 * @erase: pointer to a structure that describes a SPI NOR erase type
2015 * @size: the size of the sector/block erased by the erase type
2016 * @opcode: the SPI command op code to erase the sector/block
2018 void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
2022 erase->opcode = opcode;
2023 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
2024 erase->size_shift = ffs(erase->size) - 1;
2025 erase->size_mask = (1 << erase->size_shift) - 1;
2029 * spi_nor_mask_erase_type() - mask out a SPI NOR erase type
2030 * @erase: pointer to a structure that describes a SPI NOR erase type
2032 void spi_nor_mask_erase_type(struct spi_nor_erase_type *erase)
2038 * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
2039 * @map: the erase map of the SPI NOR
2040 * @erase_mask: bitmask encoding erase types that can erase the entire
2042 * @flash_size: the spi nor flash memory size
2044 void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
2045 u8 erase_mask, u64 flash_size)
2047 /* Offset 0 with erase_mask and SNOR_LAST_REGION bit set */
2048 map->uniform_region.offset = (erase_mask & SNOR_ERASE_TYPE_MASK) |
2050 map->uniform_region.size = flash_size;
2051 map->regions = &map->uniform_region;
2052 map->uniform_erase_type = erase_mask;
2055 int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
2056 const struct sfdp_parameter_header *bfpt_header,
2057 const struct sfdp_bfpt *bfpt)
2061 if (nor->manufacturer && nor->manufacturer->fixups &&
2062 nor->manufacturer->fixups->post_bfpt) {
2063 ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header,
2069 if (nor->info->fixups && nor->info->fixups->post_bfpt)
2070 return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt);
2075 static int spi_nor_select_read(struct spi_nor *nor,
2078 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2079 const struct spi_nor_read_command *read;
2084 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2088 read = &nor->params->reads[cmd];
2089 nor->read_opcode = read->opcode;
2090 nor->read_proto = read->proto;
2093 * In the SPI NOR framework, we don't need to make the difference
2094 * between mode clock cycles and wait state clock cycles.
2095 * Indeed, the value of the mode clock cycles is used by a QSPI
2096 * flash memory to know whether it should enter or leave its 0-4-4
2097 * (Continuous Read / XIP) mode.
2098 * eXecution In Place is out of the scope of the mtd sub-system.
2099 * Hence we choose to merge both mode and wait state clock cycles
2100 * into the so called dummy clock cycles.
2102 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2106 static int spi_nor_select_pp(struct spi_nor *nor,
2109 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2110 const struct spi_nor_pp_command *pp;
2115 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2119 pp = &nor->params->page_programs[cmd];
2120 nor->program_opcode = pp->opcode;
2121 nor->write_proto = pp->proto;
2126 * spi_nor_select_uniform_erase() - select optimum uniform erase type
2127 * @map: the erase map of the SPI NOR
2128 * @wanted_size: the erase type size to search for. Contains the value of
2129 * info->sector_size, the "small sector" size in case
2130 * CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined or 0 if
2131 * there is no information about the sector size. The
2132 * latter is the case if the flash parameters are parsed
2133 * solely by SFDP, then the largest supported erase type
2136 * Once the optimum uniform sector erase command is found, disable all the
2139 * Return: pointer to erase type on success, NULL otherwise.
2141 static const struct spi_nor_erase_type *
2142 spi_nor_select_uniform_erase(struct spi_nor_erase_map *map,
2143 const u32 wanted_size)
2145 const struct spi_nor_erase_type *tested_erase, *erase = NULL;
2147 u8 uniform_erase_type = map->uniform_erase_type;
2149 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
2150 if (!(uniform_erase_type & BIT(i)))
2153 tested_erase = &map->erase_type[i];
2155 /* Skip masked erase types. */
2156 if (!tested_erase->size)
2160 * If the current erase size is the one, stop here:
2161 * we have found the right uniform Sector Erase command.
2163 if (tested_erase->size == wanted_size) {
2164 erase = tested_erase;
2169 * Otherwise, the current erase size is still a valid candidate.
2170 * Select the biggest valid candidate.
2172 if (!erase && tested_erase->size)
2173 erase = tested_erase;
2174 /* keep iterating to find the wanted_size */
2180 /* Disable all other Sector Erase commands. */
2181 map->uniform_erase_type &= ~SNOR_ERASE_TYPE_MASK;
2182 map->uniform_erase_type |= BIT(erase - map->erase_type);
2186 static int spi_nor_select_erase(struct spi_nor *nor)
2188 struct spi_nor_erase_map *map = &nor->params->erase_map;
2189 const struct spi_nor_erase_type *erase = NULL;
2190 struct mtd_info *mtd = &nor->mtd;
2191 u32 wanted_size = nor->info->sector_size;
2195 * The previous implementation handling Sector Erase commands assumed
2196 * that the SPI flash memory has an uniform layout then used only one
2197 * of the supported erase sizes for all Sector Erase commands.
2198 * So to be backward compatible, the new implementation also tries to
2199 * manage the SPI flash memory as uniform with a single erase sector
2200 * size, when possible.
2202 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2203 /* prefer "small sector" erase if possible */
2204 wanted_size = 4096u;
2207 if (spi_nor_has_uniform_erase(nor)) {
2208 erase = spi_nor_select_uniform_erase(map, wanted_size);
2211 nor->erase_opcode = erase->opcode;
2212 mtd->erasesize = erase->size;
2217 * For non-uniform SPI flash memory, set mtd->erasesize to the
2218 * maximum erase sector size. No need to set nor->erase_opcode.
2220 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
2221 if (map->erase_type[i].size) {
2222 erase = &map->erase_type[i];
2230 mtd->erasesize = erase->size;
2234 static int spi_nor_default_setup(struct spi_nor *nor,
2235 const struct spi_nor_hwcaps *hwcaps)
2237 struct spi_nor_flash_parameter *params = nor->params;
2238 u32 ignored_mask, shared_mask;
2242 * Keep only the hardware capabilities supported by both the SPI
2243 * controller and the SPI flash memory.
2245 shared_mask = hwcaps->mask & params->hwcaps.mask;
2249 * When called from spi_nor_probe(), all caps are set and we
2250 * need to discard some of them based on what the SPI
2251 * controller actually supports (using spi_mem_supports_op()).
2253 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask);
2256 * SPI n-n-n protocols are not supported when the SPI
2257 * controller directly implements the spi_nor interface.
2258 * Yet another reason to switch to spi-mem.
2260 ignored_mask = SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR;
2261 if (shared_mask & ignored_mask) {
2263 "SPI n-n-n protocols are not supported.\n");
2264 shared_mask &= ~ignored_mask;
2268 /* Select the (Fast) Read command. */
2269 err = spi_nor_select_read(nor, shared_mask);
2272 "can't select read settings supported by both the SPI controller and memory.\n");
2276 /* Select the Page Program command. */
2277 err = spi_nor_select_pp(nor, shared_mask);
2280 "can't select write settings supported by both the SPI controller and memory.\n");
2284 /* Select the Sector Erase command. */
2285 err = spi_nor_select_erase(nor);
2288 "can't select erase settings supported by both the SPI controller and memory.\n");
2295 static int spi_nor_set_addr_nbytes(struct spi_nor *nor)
2297 if (nor->params->addr_nbytes) {
2298 nor->addr_nbytes = nor->params->addr_nbytes;
2299 } else if (nor->read_proto == SNOR_PROTO_8_8_8_DTR) {
2301 * In 8D-8D-8D mode, one byte takes half a cycle to transfer. So
2302 * in this protocol an odd addr_nbytes cannot be used because
2303 * then the address phase would only span a cycle and a half.
2304 * Half a cycle would be left over. We would then have to start
2305 * the dummy phase in the middle of a cycle and so too the data
2306 * phase, and we will end the transaction with half a cycle left
2309 * Force all 8D-8D-8D flashes to use an addr_nbytes of 4 to
2310 * avoid this situation.
2312 nor->addr_nbytes = 4;
2313 } else if (nor->info->addr_nbytes) {
2314 nor->addr_nbytes = nor->info->addr_nbytes;
2316 nor->addr_nbytes = 3;
2319 if (nor->addr_nbytes == 3 && nor->params->size > 0x1000000) {
2320 /* enable 4-byte addressing if the device exceeds 16MiB */
2321 nor->addr_nbytes = 4;
2324 if (nor->addr_nbytes > SPI_NOR_MAX_ADDR_NBYTES) {
2325 dev_dbg(nor->dev, "The number of address bytes is too large: %u\n",
2330 /* Set 4byte opcodes when possible. */
2331 if (nor->addr_nbytes == 4 && nor->flags & SNOR_F_4B_OPCODES &&
2332 !(nor->flags & SNOR_F_HAS_4BAIT))
2333 spi_nor_set_4byte_opcodes(nor);
2338 static int spi_nor_setup(struct spi_nor *nor,
2339 const struct spi_nor_hwcaps *hwcaps)
2343 if (nor->params->setup)
2344 ret = nor->params->setup(nor, hwcaps);
2346 ret = spi_nor_default_setup(nor, hwcaps);
2350 return spi_nor_set_addr_nbytes(nor);
2354 * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and
2355 * settings based on MFR register and ->default_init() hook.
2356 * @nor: pointer to a 'struct spi_nor'.
2358 static void spi_nor_manufacturer_init_params(struct spi_nor *nor)
2360 if (nor->manufacturer && nor->manufacturer->fixups &&
2361 nor->manufacturer->fixups->default_init)
2362 nor->manufacturer->fixups->default_init(nor);
2364 if (nor->info->fixups && nor->info->fixups->default_init)
2365 nor->info->fixups->default_init(nor);
2369 * spi_nor_no_sfdp_init_params() - Initialize the flash's parameters and
2370 * settings based on nor->info->sfdp_flags. This method should be called only by
2371 * flashes that do not define SFDP tables. If the flash supports SFDP but the
2372 * information is wrong and the settings from this function can not be retrieved
2373 * by parsing SFDP, one should instead use the fixup hooks and update the wrong
2375 * @nor: pointer to a 'struct spi_nor'.
2377 static void spi_nor_no_sfdp_init_params(struct spi_nor *nor)
2379 struct spi_nor_flash_parameter *params = nor->params;
2380 struct spi_nor_erase_map *map = ¶ms->erase_map;
2381 const u8 no_sfdp_flags = nor->info->no_sfdp_flags;
2384 if (no_sfdp_flags & SPI_NOR_DUAL_READ) {
2385 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2386 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2],
2387 0, 8, SPINOR_OP_READ_1_1_2,
2391 if (no_sfdp_flags & SPI_NOR_QUAD_READ) {
2392 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2393 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
2394 0, 8, SPINOR_OP_READ_1_1_4,
2398 if (no_sfdp_flags & SPI_NOR_OCTAL_READ) {
2399 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
2400 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_8],
2401 0, 8, SPINOR_OP_READ_1_1_8,
2405 if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_READ) {
2406 params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
2407 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_8_8_8_DTR],
2408 0, 20, SPINOR_OP_READ_FAST,
2409 SNOR_PROTO_8_8_8_DTR);
2412 if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_PP) {
2413 params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
2415 * Since xSPI Page Program opcode is backward compatible with
2416 * Legacy SPI, use Legacy SPI opcode there as well.
2418 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_8_8_8_DTR],
2419 SPINOR_OP_PP, SNOR_PROTO_8_8_8_DTR);
2423 * Sector Erase settings. Sort Erase Types in ascending order, with the
2424 * smallest erase size starting at BIT(0).
2428 if (no_sfdp_flags & SECT_4K) {
2429 erase_mask |= BIT(i);
2430 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
2434 erase_mask |= BIT(i);
2435 spi_nor_set_erase_type(&map->erase_type[i], nor->info->sector_size,
2437 spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
2441 * spi_nor_init_flags() - Initialize NOR flags for settings that are not defined
2442 * in the JESD216 SFDP standard, thus can not be retrieved when parsing SFDP.
2443 * @nor: pointer to a 'struct spi_nor'
2445 static void spi_nor_init_flags(struct spi_nor *nor)
2447 struct device_node *np = spi_nor_get_flash_node(nor);
2448 const u16 flags = nor->info->flags;
2450 if (of_property_read_bool(np, "broken-flash-reset"))
2451 nor->flags |= SNOR_F_BROKEN_RESET;
2453 if (flags & SPI_NOR_SWP_IS_VOLATILE)
2454 nor->flags |= SNOR_F_SWP_IS_VOLATILE;
2456 if (flags & SPI_NOR_HAS_LOCK)
2457 nor->flags |= SNOR_F_HAS_LOCK;
2459 if (flags & SPI_NOR_HAS_TB) {
2460 nor->flags |= SNOR_F_HAS_SR_TB;
2461 if (flags & SPI_NOR_TB_SR_BIT6)
2462 nor->flags |= SNOR_F_HAS_SR_TB_BIT6;
2465 if (flags & SPI_NOR_4BIT_BP) {
2466 nor->flags |= SNOR_F_HAS_4BIT_BP;
2467 if (flags & SPI_NOR_BP3_SR_BIT6)
2468 nor->flags |= SNOR_F_HAS_SR_BP3_BIT6;
2471 if (flags & NO_CHIP_ERASE)
2472 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2476 * spi_nor_init_fixup_flags() - Initialize NOR flags for settings that can not
2477 * be discovered by SFDP for this particular flash because the SFDP table that
2478 * indicates this support is not defined in the flash. In case the table for
2479 * this support is defined but has wrong values, one should instead use a
2480 * post_sfdp() hook to set the SNOR_F equivalent flag.
2481 * @nor: pointer to a 'struct spi_nor'
2483 static void spi_nor_init_fixup_flags(struct spi_nor *nor)
2485 const u8 fixup_flags = nor->info->fixup_flags;
2487 if (fixup_flags & SPI_NOR_4B_OPCODES)
2488 nor->flags |= SNOR_F_4B_OPCODES;
2490 if (fixup_flags & SPI_NOR_IO_MODE_EN_VOLATILE)
2491 nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE;
2495 * spi_nor_late_init_params() - Late initialization of default flash parameters.
2496 * @nor: pointer to a 'struct spi_nor'
2498 * Used to initialize flash parameters that are not declared in the JESD216
2499 * SFDP standard, or where SFDP tables are not defined at all.
2500 * Will replace the spi_nor_manufacturer_init_params() method.
2502 static void spi_nor_late_init_params(struct spi_nor *nor)
2504 if (nor->manufacturer && nor->manufacturer->fixups &&
2505 nor->manufacturer->fixups->late_init)
2506 nor->manufacturer->fixups->late_init(nor);
2508 if (nor->info->fixups && nor->info->fixups->late_init)
2509 nor->info->fixups->late_init(nor);
2511 spi_nor_init_flags(nor);
2512 spi_nor_init_fixup_flags(nor);
2515 * NOR protection support. When locking_ops are not provided, we pick
2518 if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops)
2519 spi_nor_init_default_locking_ops(nor);
2523 * spi_nor_sfdp_init_params_deprecated() - Deprecated way of initializing flash
2524 * parameters and settings based on JESD216 SFDP standard.
2525 * @nor: pointer to a 'struct spi_nor'.
2527 * The method has a roll-back mechanism: in case the SFDP parsing fails, the
2528 * legacy flash parameters and settings will be restored.
2530 static void spi_nor_sfdp_init_params_deprecated(struct spi_nor *nor)
2532 struct spi_nor_flash_parameter sfdp_params;
2534 memcpy(&sfdp_params, nor->params, sizeof(sfdp_params));
2536 if (spi_nor_parse_sfdp(nor)) {
2537 memcpy(nor->params, &sfdp_params, sizeof(*nor->params));
2538 nor->flags &= ~SNOR_F_4B_OPCODES;
2543 * spi_nor_init_params_deprecated() - Deprecated way of initializing flash
2544 * parameters and settings.
2545 * @nor: pointer to a 'struct spi_nor'.
2547 * The method assumes that flash doesn't support SFDP so it initializes flash
2548 * parameters in spi_nor_no_sfdp_init_params() which later on can be overwritten
2549 * when parsing SFDP, if supported.
2551 static void spi_nor_init_params_deprecated(struct spi_nor *nor)
2553 spi_nor_no_sfdp_init_params(nor);
2555 spi_nor_manufacturer_init_params(nor);
2557 if (nor->info->no_sfdp_flags & (SPI_NOR_DUAL_READ |
2559 SPI_NOR_OCTAL_READ |
2560 SPI_NOR_OCTAL_DTR_READ))
2561 spi_nor_sfdp_init_params_deprecated(nor);
2565 * spi_nor_init_default_params() - Default initialization of flash parameters
2566 * and settings. Done for all flashes, regardless is they define SFDP tables
2568 * @nor: pointer to a 'struct spi_nor'.
2570 static void spi_nor_init_default_params(struct spi_nor *nor)
2572 struct spi_nor_flash_parameter *params = nor->params;
2573 const struct flash_info *info = nor->info;
2574 struct device_node *np = spi_nor_get_flash_node(nor);
2576 params->quad_enable = spi_nor_sr2_bit1_quad_enable;
2577 params->set_4byte_addr_mode = spansion_set_4byte_addr_mode;
2578 params->otp.org = &info->otp_org;
2580 /* Default to 16-bit Write Status (01h) Command */
2581 nor->flags |= SNOR_F_HAS_16BIT_SR;
2583 /* Set SPI NOR sizes. */
2584 params->writesize = 1;
2585 params->size = (u64)info->sector_size * info->n_sectors;
2586 params->page_size = info->page_size;
2588 if (!(info->flags & SPI_NOR_NO_FR)) {
2589 /* Default to Fast Read for DT and non-DT platform devices. */
2590 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2592 /* Mask out Fast Read if not requested at DT instantiation. */
2593 if (np && !of_property_read_bool(np, "m25p,fast-read"))
2594 params->hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2597 /* (Fast) Read settings. */
2598 params->hwcaps.mask |= SNOR_HWCAPS_READ;
2599 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
2600 0, 0, SPINOR_OP_READ,
2603 if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
2604 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
2605 0, 8, SPINOR_OP_READ_FAST,
2607 /* Page Program settings. */
2608 params->hwcaps.mask |= SNOR_HWCAPS_PP;
2609 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
2610 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2612 if (info->flags & SPI_NOR_QUAD_PP) {
2613 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
2614 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_1_1_4],
2615 SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
2620 * spi_nor_init_params() - Initialize the flash's parameters and settings.
2621 * @nor: pointer to a 'struct spi_nor'.
2623 * The flash parameters and settings are initialized based on a sequence of
2624 * calls that are ordered by priority:
2626 * 1/ Default flash parameters initialization. The initializations are done
2627 * based on nor->info data:
2628 * spi_nor_info_init_params()
2630 * which can be overwritten by:
2631 * 2/ Manufacturer flash parameters initialization. The initializations are
2632 * done based on MFR register, or when the decisions can not be done solely
2633 * based on MFR, by using specific flash_info tweeks, ->default_init():
2634 * spi_nor_manufacturer_init_params()
2636 * which can be overwritten by:
2637 * 3/ SFDP flash parameters initialization. JESD216 SFDP is a standard and
2638 * should be more accurate that the above.
2639 * spi_nor_parse_sfdp() or spi_nor_no_sfdp_init_params()
2641 * Please note that there is a ->post_bfpt() fixup hook that can overwrite
2642 * the flash parameters and settings immediately after parsing the Basic
2643 * Flash Parameter Table.
2644 * spi_nor_post_sfdp_fixups() is called after the SFDP tables are parsed.
2645 * It is used to tweak various flash parameters when information provided
2646 * by the SFDP tables are wrong.
2648 * which can be overwritten by:
2649 * 4/ Late flash parameters initialization, used to initialize flash
2650 * parameters that are not declared in the JESD216 SFDP standard, or where SFDP
2651 * tables are not defined at all.
2652 * spi_nor_late_init_params()
2654 * Return: 0 on success, -errno otherwise.
2656 static int spi_nor_init_params(struct spi_nor *nor)
2660 nor->params = devm_kzalloc(nor->dev, sizeof(*nor->params), GFP_KERNEL);
2664 spi_nor_init_default_params(nor);
2666 if (nor->info->parse_sfdp) {
2667 ret = spi_nor_parse_sfdp(nor);
2669 dev_err(nor->dev, "BFPT parsing failed. Please consider using SPI_NOR_SKIP_SFDP when declaring the flash\n");
2672 } else if (nor->info->no_sfdp_flags & SPI_NOR_SKIP_SFDP) {
2673 spi_nor_no_sfdp_init_params(nor);
2675 spi_nor_init_params_deprecated(nor);
2678 spi_nor_late_init_params(nor);
2683 /** spi_nor_octal_dtr_enable() - enable Octal DTR I/O if needed
2684 * @nor: pointer to a 'struct spi_nor'
2685 * @enable: whether to enable or disable Octal DTR
2687 * Return: 0 on success, -errno otherwise.
2689 static int spi_nor_octal_dtr_enable(struct spi_nor *nor, bool enable)
2693 if (!nor->params->octal_dtr_enable)
2696 if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR &&
2697 nor->write_proto == SNOR_PROTO_8_8_8_DTR))
2700 if (!(nor->flags & SNOR_F_IO_MODE_EN_VOLATILE))
2703 ret = nor->params->octal_dtr_enable(nor, enable);
2708 nor->reg_proto = SNOR_PROTO_8_8_8_DTR;
2710 nor->reg_proto = SNOR_PROTO_1_1_1;
2716 * spi_nor_quad_enable() - enable Quad I/O if needed.
2717 * @nor: pointer to a 'struct spi_nor'
2719 * Return: 0 on success, -errno otherwise.
2721 static int spi_nor_quad_enable(struct spi_nor *nor)
2723 if (!nor->params->quad_enable)
2726 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2727 spi_nor_get_protocol_width(nor->write_proto) == 4))
2730 return nor->params->quad_enable(nor);
2733 static int spi_nor_init(struct spi_nor *nor)
2737 err = spi_nor_octal_dtr_enable(nor, true);
2739 dev_dbg(nor->dev, "octal mode not supported\n");
2743 err = spi_nor_quad_enable(nor);
2745 dev_dbg(nor->dev, "quad mode not supported\n");
2750 * Some SPI NOR flashes are write protected by default after a power-on
2751 * reset cycle, in order to avoid inadvertent writes during power-up.
2752 * Backward compatibility imposes to unlock the entire flash memory
2753 * array at power-up by default. Depending on the kernel configuration
2754 * (1) do nothing, (2) always unlock the entire flash array or (3)
2755 * unlock the entire flash array only when the software write
2756 * protection bits are volatile. The latter is indicated by
2757 * SNOR_F_SWP_IS_VOLATILE.
2759 if (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE) ||
2760 (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE_ON_VOLATILE) &&
2761 nor->flags & SNOR_F_SWP_IS_VOLATILE))
2762 spi_nor_try_unlock_all(nor);
2764 if (nor->addr_nbytes == 4 &&
2765 nor->read_proto != SNOR_PROTO_8_8_8_DTR &&
2766 !(nor->flags & SNOR_F_4B_OPCODES)) {
2768 * If the RESET# pin isn't hooked up properly, or the system
2769 * otherwise doesn't perform a reset command in the boot
2770 * sequence, it's impossible to 100% protect against unexpected
2771 * reboots (e.g., crashes). Warn the user (or hopefully, system
2772 * designer) that this is bad.
2774 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
2775 "enabling reset hack; may not recover from unexpected reboots\n");
2776 err = nor->params->set_4byte_addr_mode(nor, true);
2777 if (err && err != -ENOTSUPP)
2785 * spi_nor_soft_reset() - Perform a software reset
2786 * @nor: pointer to 'struct spi_nor'
2788 * Performs a "Soft Reset and Enter Default Protocol Mode" sequence which resets
2789 * the device to its power-on-reset state. This is useful when the software has
2790 * made some changes to device (volatile) registers and needs to reset it before
2791 * shutting down, for example.
2793 * Not every flash supports this sequence. The same set of opcodes might be used
2794 * for some other operation on a flash that does not support this. Support for
2795 * this sequence can be discovered via SFDP in the BFPT table.
2797 * Return: 0 on success, -errno otherwise.
2799 static void spi_nor_soft_reset(struct spi_nor *nor)
2801 struct spi_mem_op op;
2804 op = (struct spi_mem_op)SPINOR_SRSTEN_OP;
2806 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
2808 ret = spi_mem_exec_op(nor->spimem, &op);
2810 dev_warn(nor->dev, "Software reset failed: %d\n", ret);
2814 op = (struct spi_mem_op)SPINOR_SRST_OP;
2816 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
2818 ret = spi_mem_exec_op(nor->spimem, &op);
2820 dev_warn(nor->dev, "Software reset failed: %d\n", ret);
2825 * Software Reset is not instant, and the delay varies from flash to
2826 * flash. Looking at a few flashes, most range somewhere below 100
2827 * microseconds. So, sleep for a range of 200-400 us.
2829 usleep_range(SPI_NOR_SRST_SLEEP_MIN, SPI_NOR_SRST_SLEEP_MAX);
2832 /* mtd suspend handler */
2833 static int spi_nor_suspend(struct mtd_info *mtd)
2835 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2838 /* Disable octal DTR mode if we enabled it. */
2839 ret = spi_nor_octal_dtr_enable(nor, false);
2841 dev_err(nor->dev, "suspend() failed\n");
2846 /* mtd resume handler */
2847 static void spi_nor_resume(struct mtd_info *mtd)
2849 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2850 struct device *dev = nor->dev;
2853 /* re-initialize the nor chip */
2854 ret = spi_nor_init(nor);
2856 dev_err(dev, "resume() failed\n");
2859 static int spi_nor_get_device(struct mtd_info *mtd)
2861 struct mtd_info *master = mtd_get_master(mtd);
2862 struct spi_nor *nor = mtd_to_spi_nor(master);
2866 dev = nor->spimem->spi->controller->dev.parent;
2870 if (!try_module_get(dev->driver->owner))
2876 static void spi_nor_put_device(struct mtd_info *mtd)
2878 struct mtd_info *master = mtd_get_master(mtd);
2879 struct spi_nor *nor = mtd_to_spi_nor(master);
2883 dev = nor->spimem->spi->controller->dev.parent;
2887 module_put(dev->driver->owner);
2890 void spi_nor_restore(struct spi_nor *nor)
2894 /* restore the addressing mode */
2895 if (nor->addr_nbytes == 4 && !(nor->flags & SNOR_F_4B_OPCODES) &&
2896 nor->flags & SNOR_F_BROKEN_RESET) {
2897 ret = nor->params->set_4byte_addr_mode(nor, false);
2900 * Do not stop the execution in the hope that the flash
2901 * will default to the 3-byte address mode after the
2904 dev_err(nor->dev, "Failed to exit 4-byte address mode, err = %d\n", ret);
2907 if (nor->flags & SNOR_F_SOFT_RESET)
2908 spi_nor_soft_reset(nor);
2910 EXPORT_SYMBOL_GPL(spi_nor_restore);
2912 static const struct flash_info *spi_nor_match_name(struct spi_nor *nor,
2917 for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
2918 for (j = 0; j < manufacturers[i]->nparts; j++) {
2919 if (!strcmp(name, manufacturers[i]->parts[j].name)) {
2920 nor->manufacturer = manufacturers[i];
2921 return &manufacturers[i]->parts[j];
2929 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
2932 const struct flash_info *info = NULL;
2935 info = spi_nor_match_name(nor, name);
2936 /* Try to auto-detect if chip name wasn't specified or not found */
2938 return spi_nor_detect(nor);
2941 * If caller has specified name of flash model that can normally be
2942 * detected using JEDEC, let's verify it.
2944 if (name && info->id_len) {
2945 const struct flash_info *jinfo;
2947 jinfo = spi_nor_detect(nor);
2948 if (IS_ERR(jinfo)) {
2950 } else if (jinfo != info) {
2952 * JEDEC knows better, so overwrite platform ID. We
2953 * can't trust partitions any longer, but we'll let
2954 * mtd apply them anyway, since some partitions may be
2955 * marked read-only, and we don't want to lose that
2956 * information, even if it's not 100% accurate.
2958 dev_warn(nor->dev, "found %s, expected %s\n",
2959 jinfo->name, info->name);
2967 static void spi_nor_set_mtd_info(struct spi_nor *nor)
2969 struct mtd_info *mtd = &nor->mtd;
2970 struct device *dev = nor->dev;
2972 spi_nor_set_mtd_locking_ops(nor);
2973 spi_nor_set_mtd_otp_ops(nor);
2975 mtd->dev.parent = dev;
2977 mtd->name = dev_name(dev);
2978 mtd->type = MTD_NORFLASH;
2979 mtd->flags = MTD_CAP_NORFLASH;
2980 if (nor->info->flags & SPI_NOR_NO_ERASE)
2981 mtd->flags |= MTD_NO_ERASE;
2983 mtd->_erase = spi_nor_erase;
2984 mtd->writesize = nor->params->writesize;
2985 mtd->writebufsize = nor->params->page_size;
2986 mtd->size = nor->params->size;
2987 mtd->_read = spi_nor_read;
2988 /* Might be already set by some SST flashes. */
2990 mtd->_write = spi_nor_write;
2991 mtd->_suspend = spi_nor_suspend;
2992 mtd->_resume = spi_nor_resume;
2993 mtd->_get_device = spi_nor_get_device;
2994 mtd->_put_device = spi_nor_put_device;
2997 static int spi_nor_hw_reset(struct spi_nor *nor)
2999 struct gpio_desc *reset;
3001 reset = devm_gpiod_get_optional(nor->dev, "reset", GPIOD_OUT_LOW);
3002 if (IS_ERR_OR_NULL(reset))
3003 return PTR_ERR_OR_ZERO(reset);
3006 * Experimental delay values by looking at different flash device
3007 * vendors datasheets.
3010 gpiod_set_value_cansleep(reset, 1);
3011 usleep_range(100, 150);
3012 gpiod_set_value_cansleep(reset, 0);
3013 usleep_range(1000, 1200);
3018 int spi_nor_scan(struct spi_nor *nor, const char *name,
3019 const struct spi_nor_hwcaps *hwcaps)
3021 const struct flash_info *info;
3022 struct device *dev = nor->dev;
3023 struct mtd_info *mtd = &nor->mtd;
3027 ret = spi_nor_check(nor);
3031 /* Reset SPI protocol for all commands. */
3032 nor->reg_proto = SNOR_PROTO_1_1_1;
3033 nor->read_proto = SNOR_PROTO_1_1_1;
3034 nor->write_proto = SNOR_PROTO_1_1_1;
3037 * We need the bounce buffer early to read/write registers when going
3038 * through the spi-mem layer (buffers have to be DMA-able).
3039 * For spi-mem drivers, we'll reallocate a new buffer if
3040 * nor->params->page_size turns out to be greater than PAGE_SIZE (which
3041 * shouldn't happen before long since NOR pages are usually less
3042 * than 1KB) after spi_nor_scan() returns.
3044 nor->bouncebuf_size = PAGE_SIZE;
3045 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
3047 if (!nor->bouncebuf)
3050 ret = spi_nor_hw_reset(nor);
3054 info = spi_nor_get_flash_info(nor, name);
3056 return PTR_ERR(info);
3060 mutex_init(&nor->lock);
3062 /* Init flash parameters based on flash_info struct and SFDP */
3063 ret = spi_nor_init_params(nor);
3068 * Configure the SPI memory:
3069 * - select op codes for (Fast) Read, Page Program and Sector Erase.
3070 * - set the number of dummy cycles (mode cycles + wait states).
3071 * - set the SPI protocols for register and memory accesses.
3072 * - set the number of address bytes.
3074 ret = spi_nor_setup(nor, hwcaps);
3078 /* Send all the required SPI flash commands to initialize device */
3079 ret = spi_nor_init(nor);
3083 /* No mtd_info fields should be used up to this point. */
3084 spi_nor_set_mtd_info(nor);
3086 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
3087 (long long)mtd->size >> 10);
3090 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
3091 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
3092 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
3093 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
3095 if (mtd->numeraseregions)
3096 for (i = 0; i < mtd->numeraseregions; i++)
3098 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
3099 ".erasesize = 0x%.8x (%uKiB), "
3100 ".numblocks = %d }\n",
3101 i, (long long)mtd->eraseregions[i].offset,
3102 mtd->eraseregions[i].erasesize,
3103 mtd->eraseregions[i].erasesize / 1024,
3104 mtd->eraseregions[i].numblocks);
3107 EXPORT_SYMBOL_GPL(spi_nor_scan);
3109 static int spi_nor_create_read_dirmap(struct spi_nor *nor)
3111 struct spi_mem_dirmap_info info = {
3112 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
3113 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0),
3114 SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
3115 SPI_MEM_OP_DATA_IN(0, NULL, 0)),
3117 .length = nor->params->size,
3119 struct spi_mem_op *op = &info.op_tmpl;
3121 spi_nor_spimem_setup_op(nor, op, nor->read_proto);
3123 /* convert the dummy cycles to the number of bytes */
3124 op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8;
3125 if (spi_nor_protocol_is_dtr(nor->read_proto))
3126 op->dummy.nbytes *= 2;
3129 * Since spi_nor_spimem_setup_op() only sets buswidth when the number
3130 * of data bytes is non-zero, the data buswidth won't be set here. So,
3133 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
3135 nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3137 return PTR_ERR_OR_ZERO(nor->dirmap.rdesc);
3140 static int spi_nor_create_write_dirmap(struct spi_nor *nor)
3142 struct spi_mem_dirmap_info info = {
3143 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
3144 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0),
3145 SPI_MEM_OP_NO_DUMMY,
3146 SPI_MEM_OP_DATA_OUT(0, NULL, 0)),
3148 .length = nor->params->size,
3150 struct spi_mem_op *op = &info.op_tmpl;
3152 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
3153 op->addr.nbytes = 0;
3155 spi_nor_spimem_setup_op(nor, op, nor->write_proto);
3158 * Since spi_nor_spimem_setup_op() only sets buswidth when the number
3159 * of data bytes is non-zero, the data buswidth won't be set here. So,
3162 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
3164 nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3166 return PTR_ERR_OR_ZERO(nor->dirmap.wdesc);
3169 static int spi_nor_probe(struct spi_mem *spimem)
3171 struct spi_device *spi = spimem->spi;
3172 struct flash_platform_data *data = dev_get_platdata(&spi->dev);
3173 struct spi_nor *nor;
3175 * Enable all caps by default. The core will mask them after
3176 * checking what's really supported using spi_mem_supports_op().
3178 const struct spi_nor_hwcaps hwcaps = { .mask = SNOR_HWCAPS_ALL };
3182 nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL);
3186 nor->spimem = spimem;
3187 nor->dev = &spi->dev;
3188 spi_nor_set_flash_node(nor, spi->dev.of_node);
3190 spi_mem_set_drvdata(spimem, nor);
3192 if (data && data->name)
3193 nor->mtd.name = data->name;
3196 nor->mtd.name = spi_mem_get_name(spimem);
3199 * For some (historical?) reason many platforms provide two different
3200 * names in flash_platform_data: "name" and "type". Quite often name is
3201 * set to "m25p80" and then "type" provides a real chip name.
3202 * If that's the case, respect "type" and ignore a "name".
3204 if (data && data->type)
3205 flash_name = data->type;
3206 else if (!strcmp(spi->modalias, "spi-nor"))
3207 flash_name = NULL; /* auto-detect */
3209 flash_name = spi->modalias;
3211 ret = spi_nor_scan(nor, flash_name, &hwcaps);
3215 spi_nor_debugfs_register(nor);
3218 * None of the existing parts have > 512B pages, but let's play safe
3219 * and add this logic so that if anyone ever adds support for such
3220 * a NOR we don't end up with buffer overflows.
3222 if (nor->params->page_size > PAGE_SIZE) {
3223 nor->bouncebuf_size = nor->params->page_size;
3224 devm_kfree(nor->dev, nor->bouncebuf);
3225 nor->bouncebuf = devm_kmalloc(nor->dev,
3226 nor->bouncebuf_size,
3228 if (!nor->bouncebuf)
3232 ret = spi_nor_create_read_dirmap(nor);
3236 ret = spi_nor_create_write_dirmap(nor);
3240 return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
3241 data ? data->nr_parts : 0);
3244 static int spi_nor_remove(struct spi_mem *spimem)
3246 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3248 spi_nor_restore(nor);
3250 /* Clean up MTD stuff. */
3251 return mtd_device_unregister(&nor->mtd);
3254 static void spi_nor_shutdown(struct spi_mem *spimem)
3256 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3258 spi_nor_restore(nor);
3262 * Do NOT add to this array without reading the following:
3264 * Historically, many flash devices are bound to this driver by their name. But
3265 * since most of these flash are compatible to some extent, and their
3266 * differences can often be differentiated by the JEDEC read-ID command, we
3267 * encourage new users to add support to the spi-nor library, and simply bind
3268 * against a generic string here (e.g., "jedec,spi-nor").
3270 * Many flash names are kept here in this list to keep them available
3271 * as module aliases for existing platforms.
3273 static const struct spi_device_id spi_nor_dev_ids[] = {
3275 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
3276 * hack around the fact that the SPI core does not provide uevent
3277 * matching for .of_match_table
3282 * Entries not used in DTs that should be safe to drop after replacing
3283 * them with "spi-nor" in platform data.
3285 {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
3288 * Entries that were used in DTs without "jedec,spi-nor" fallback and
3289 * should be kept for backward compatibility.
3291 {"at25df321a"}, {"at25df641"}, {"at26df081a"},
3292 {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"},
3293 {"mx25l25635e"},{"mx66l51235l"},
3294 {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"},
3295 {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"},
3297 {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
3298 {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"},
3299 {"m25p64"}, {"m25p128"},
3300 {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
3301 {"w25q80bl"}, {"w25q128"}, {"w25q256"},
3303 /* Flashes that can't be detected using JEDEC */
3304 {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
3305 {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
3306 {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
3308 /* Everspin MRAMs (non-JEDEC) */
3309 { "mr25h128" }, /* 128 Kib, 40 MHz */
3310 { "mr25h256" }, /* 256 Kib, 40 MHz */
3311 { "mr25h10" }, /* 1 Mib, 40 MHz */
3312 { "mr25h40" }, /* 4 Mib, 40 MHz */
3316 MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids);
3318 static const struct of_device_id spi_nor_of_table[] = {
3320 * Generic compatibility for SPI NOR that can be identified by the
3321 * JEDEC READ ID opcode (0x9F). Use this, if possible.
3323 { .compatible = "jedec,spi-nor" },
3326 MODULE_DEVICE_TABLE(of, spi_nor_of_table);
3329 * REVISIT: many of these chips have deep power-down modes, which
3330 * should clearly be entered on suspend() to minimize power use.
3331 * And also when they're otherwise idle...
3333 static struct spi_mem_driver spi_nor_driver = {
3337 .of_match_table = spi_nor_of_table,
3338 .dev_groups = spi_nor_sysfs_groups,
3340 .id_table = spi_nor_dev_ids,
3342 .probe = spi_nor_probe,
3343 .remove = spi_nor_remove,
3344 .shutdown = spi_nor_shutdown,
3346 module_spi_mem_driver(spi_nor_driver);
3348 MODULE_LICENSE("GPL v2");
3349 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
3350 MODULE_AUTHOR("Mike Lavender");
3351 MODULE_DESCRIPTION("framework for SPI NOR");