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 /* EON -- en25xxx */
2085 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K
) },
2086 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
2087 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
2088 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
2089 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K
) },
2090 { "en25q80a", INFO(0x1c3014, 0, 64 * 1024, 16,
2091 SECT_4K
| SPI_NOR_DUAL_READ
) },
2092 { "en25qh16", INFO(0x1c7015, 0, 64 * 1024, 32,
2093 SECT_4K
| SPI_NOR_DUAL_READ
) },
2094 { "en25qh32", INFO(0x1c7016, 0, 64 * 1024, 64, 0) },
2095 { "en25qh64", INFO(0x1c7017, 0, 64 * 1024, 128,
2096 SECT_4K
| SPI_NOR_DUAL_READ
) },
2097 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
2098 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
2099 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K
) },
2102 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K
| SPI_NOR_HAS_LOCK
) },
2103 { "f25l32qa", INFO(0x8c4116, 0, 64 * 1024, 64, SECT_4K
| SPI_NOR_HAS_LOCK
) },
2104 { "f25l64qa", INFO(0x8c4117, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_HAS_LOCK
) },
2107 { "mr25h128", CAT25_INFO( 16 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
2108 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
2109 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
2110 { "mr25h40", CAT25_INFO(512 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
2113 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE
) },
2117 "gd25q16", INFO(0xc84015, 0, 64 * 1024, 32,
2118 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2119 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2122 "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
2123 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2124 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2127 "gd25lq32", INFO(0xc86016, 0, 64 * 1024, 64,
2128 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2129 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2132 "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
2133 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2134 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2137 "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
2138 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2139 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2142 "gd25lq128d", INFO(0xc86018, 0, 64 * 1024, 256,
2143 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2144 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2147 "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
2148 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2149 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2152 "gd25q256", INFO(0xc84019, 0, 64 * 1024, 512,
2153 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2154 SPI_NOR_4B_OPCODES
| SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
|
2156 .fixups
= &gd25q256_fixups
,
2159 /* Intel/Numonyx -- xxxs33b */
2160 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
2161 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
2162 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
2165 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K
) },
2166 { "is25lq040b", INFO(0x9d4013, 0, 64 * 1024, 8,
2167 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2168 { "is25lp016d", INFO(0x9d6015, 0, 64 * 1024, 32,
2169 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2170 { "is25lp080d", INFO(0x9d6014, 0, 64 * 1024, 16,
2171 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2172 { "is25lp032", INFO(0x9d6016, 0, 64 * 1024, 64,
2173 SECT_4K
| SPI_NOR_DUAL_READ
) },
2174 { "is25lp064", INFO(0x9d6017, 0, 64 * 1024, 128,
2175 SECT_4K
| SPI_NOR_DUAL_READ
) },
2176 { "is25lp128", INFO(0x9d6018, 0, 64 * 1024, 256,
2177 SECT_4K
| SPI_NOR_DUAL_READ
) },
2178 { "is25lp256", INFO(0x9d6019, 0, 64 * 1024, 512,
2179 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2181 .fixups
= &is25lp256_fixups
},
2182 { "is25wp032", INFO(0x9d7016, 0, 64 * 1024, 64,
2183 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2184 { "is25wp064", INFO(0x9d7017, 0, 64 * 1024, 128,
2185 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2186 { "is25wp128", INFO(0x9d7018, 0, 64 * 1024, 256,
2187 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2188 { "is25wp256", INFO(0x9d7019, 0, 64 * 1024, 512,
2189 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2191 .fixups
= &is25lp256_fixups
},
2194 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K
) },
2195 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K
) },
2196 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K
) },
2197 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
2198 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K
) },
2199 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K
) },
2200 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K
) },
2201 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K
) },
2202 { "mx25u2033e", INFO(0xc22532, 0, 64 * 1024, 4, SECT_4K
) },
2203 { "mx25u3235f", INFO(0xc22536, 0, 64 * 1024, 64,
2204 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2205 { "mx25u4035", INFO(0xc22533, 0, 64 * 1024, 8, SECT_4K
) },
2206 { "mx25u8035", INFO(0xc22534, 0, 64 * 1024, 16, SECT_4K
) },
2207 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K
) },
2208 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
2209 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
2210 { "mx25r3235f", INFO(0xc22816, 0, 64 * 1024, 64,
2211 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2212 { "mx25u12835f", INFO(0xc22538, 0, 64 * 1024, 256,
2213 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2214 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512,
2215 SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
)
2216 .fixups
= &mx25l25635_fixups
},
2217 { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K
| SPI_NOR_4B_OPCODES
) },
2218 { "mx25v8035f", INFO(0xc22314, 0, 64 * 1024, 16,
2219 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2220 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
2221 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
2222 { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
2223 { "mx66l1g45g", INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2224 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ
) },
2226 /* Micron <--> ST Micro */
2227 { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32, SECT_4K
| SPI_NOR_QUAD_READ
) },
2228 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ
) },
2229 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ
) },
2230 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_QUAD_READ
) },
2231 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_QUAD_READ
) },
2232 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K
|
2233 USE_FSR
| SPI_NOR_QUAD_READ
) },
2234 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K
|
2235 USE_FSR
| SPI_NOR_QUAD_READ
) },
2236 { "mt25ql256a", INFO6(0x20ba19, 0x104400, 64 * 1024, 512,
2237 SECT_4K
| USE_FSR
| SPI_NOR_DUAL_READ
|
2238 SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
2239 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K
|
2240 USE_FSR
| SPI_NOR_DUAL_READ
|
2241 SPI_NOR_QUAD_READ
) },
2242 { "mt25qu256a", INFO6(0x20bb19, 0x104400, 64 * 1024, 512,
2243 SECT_4K
| USE_FSR
| SPI_NOR_DUAL_READ
|
2244 SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
2245 { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K
|
2246 USE_FSR
| SPI_NOR_QUAD_READ
) },
2247 { "mt25ql512a", INFO6(0x20ba20, 0x104400, 64 * 1024, 1024,
2248 SECT_4K
| USE_FSR
| SPI_NOR_DUAL_READ
|
2249 SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
2250 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K
| USE_FSR
| SPI_NOR_QUAD_READ
) },
2251 { "mt25qu512a", INFO6(0x20bb20, 0x104400, 64 * 1024, 1024,
2252 SECT_4K
| USE_FSR
| SPI_NOR_DUAL_READ
|
2253 SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
2254 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K
|
2255 USE_FSR
| SPI_NOR_QUAD_READ
) },
2256 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K
| USE_FSR
| SPI_NOR_QUAD_READ
| NO_CHIP_ERASE
) },
2257 { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K
| USE_FSR
| SPI_NOR_QUAD_READ
| NO_CHIP_ERASE
) },
2258 { "mt25ql02g", INFO(0x20ba22, 0, 64 * 1024, 4096,
2259 SECT_4K
| USE_FSR
| SPI_NOR_QUAD_READ
|
2261 { "mt25qu02g", INFO(0x20bb22, 0, 64 * 1024, 4096, SECT_4K
| USE_FSR
| SPI_NOR_QUAD_READ
| NO_CHIP_ERASE
) },
2265 "mt35xu512aba", INFO(0x2c5b1a, 0, 128 * 1024, 512,
2266 SECT_4K
| USE_FSR
| SPI_NOR_OCTAL_READ
|
2269 { "mt35xu02g", INFO(0x2c5b1c, 0, 128 * 1024, 2048,
2270 SECT_4K
| USE_FSR
| SPI_NOR_OCTAL_READ
|
2271 SPI_NOR_4B_OPCODES
) },
2274 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC
) },
2275 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC
) },
2276 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K
) },
2278 /* Spansion/Cypress -- single (large) sector size only, at least
2279 * for the chips listed here (without boot sectors).
2281 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2282 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2283 { "s25fl128s0", INFO6(0x012018, 0x4d0080, 256 * 1024, 64,
2284 SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
2285 { "s25fl128s1", INFO6(0x012018, 0x4d0180, 64 * 1024, 256,
2286 SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
2287 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR
) },
2288 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
2289 { "s25fl512s", INFO6(0x010220, 0x4d0080, 256 * 1024, 256,
2290 SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2291 SPI_NOR_HAS_LOCK
| USE_CLSR
) },
2292 { "s25fs512s", INFO6(0x010220, 0x4d0081, 256 * 1024, 256, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
2293 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
2294 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
2295 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
2296 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
2297 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
2298 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
2299 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
2300 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
2301 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
2302 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
2303 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2304 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2305 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2306 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K
) },
2307 { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2308 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K
) },
2309 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K
) },
2310 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K
| SPI_NOR_DUAL_READ
) },
2311 { "s25fl208k", INFO(0x014014, 0, 64 * 1024, 16, SECT_4K
| SPI_NOR_DUAL_READ
) },
2312 { "s25fl064l", INFO(0x016017, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
2313 { "s25fl128l", INFO(0x016018, 0, 64 * 1024, 256, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
2314 { "s25fl256l", INFO(0x016019, 0, 64 * 1024, 512, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
2316 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
2317 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K
| SST_WRITE
) },
2318 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K
| SST_WRITE
) },
2319 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K
| SST_WRITE
) },
2320 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K
| SST_WRITE
) },
2321 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K
) },
2322 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K
| SST_WRITE
) },
2323 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K
| SST_WRITE
) },
2324 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K
| SST_WRITE
) },
2325 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K
) },
2326 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K
) },
2327 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K
| SST_WRITE
) },
2328 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K
| SST_WRITE
) },
2329 { "sst26wf016b", INFO(0xbf2651, 0, 64 * 1024, 32, SECT_4K
|
2330 SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2331 { "sst26vf016b", INFO(0xbf2641, 0, 64 * 1024, 32, SECT_4K
|
2332 SPI_NOR_DUAL_READ
) },
2333 { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2335 /* ST Microelectronics -- newer production may have feature updates */
2336 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
2337 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
2338 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
2339 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
2340 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
2341 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
2342 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
2343 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
2344 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
2346 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
2347 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
2348 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
2349 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
2350 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
2351 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
2352 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
2353 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
2354 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
2356 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
2357 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
2358 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
2360 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
2361 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
2362 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K
) },
2364 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K
) },
2365 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K
) },
2366 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K
) },
2367 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K
) },
2368 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
2369 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
2371 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
2372 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K
) },
2373 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K
) },
2374 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K
) },
2375 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K
) },
2376 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K
) },
2377 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K
) },
2379 "w25q16dw", INFO(0xef6015, 0, 64 * 1024, 32,
2380 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2381 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2383 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K
) },
2385 "w25q16jv-im/jm", INFO(0xef7015, 0, 64 * 1024, 32,
2386 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2387 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2389 { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K
) },
2390 { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K
) },
2391 { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K
) },
2392 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K
) },
2394 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
2395 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2396 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2399 "w25q32jv", INFO(0xef7016, 0, 64 * 1024, 64,
2400 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2401 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2404 "w25q32jwm", INFO(0xef8016, 0, 64 * 1024, 64,
2405 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2406 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2408 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K
) },
2409 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K
) },
2411 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
2412 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2413 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2416 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
2417 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2418 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2421 "w25q128jv", INFO(0xef7018, 0, 64 * 1024, 256,
2422 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2423 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
2425 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K
) },
2426 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K
) },
2427 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K
) },
2428 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512,
2429 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
2430 SPI_NOR_4B_OPCODES
) },
2431 { "w25q256jvm", INFO(0xef7019, 0, 64 * 1024, 512,
2432 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2433 { "w25q256jw", INFO(0xef6019, 0, 64 * 1024, 512,
2434 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2435 { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
2436 SECT_4K
| SPI_NOR_QUAD_READ
| SPI_NOR_DUAL_READ
) },
2438 /* Catalyst / On Semiconductor -- non-JEDEC */
2439 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
2440 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
2441 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
2442 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
2443 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
2445 /* Xilinx S3AN Internal Flash */
2446 { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
2447 { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
2448 { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
2449 { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
2450 { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
2452 /* XMC (Wuhan Xinxin Semiconductor Manufacturing Corp.) */
2453 { "XM25QH64A", INFO(0x207017, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2454 { "XM25QH128A", INFO(0x207018, 0, 64 * 1024, 256, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
2458 static const struct spi_nor_manufacturer
*manufacturers
[] = {
2462 static const struct flash_info
*
2463 spi_nor_search_part_by_id(const struct flash_info
*parts
, unsigned int nparts
,
2468 for (i
= 0; i
< nparts
; i
++) {
2469 if (parts
[i
].id_len
&&
2470 !memcmp(parts
[i
].id
, id
, parts
[i
].id_len
))
2477 static const struct flash_info
*spi_nor_read_id(struct spi_nor
*nor
)
2479 const struct flash_info
*info
;
2480 u8
*id
= nor
->bouncebuf
;
2485 struct spi_mem_op op
=
2486 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDID
, 1),
2488 SPI_MEM_OP_NO_DUMMY
,
2489 SPI_MEM_OP_DATA_IN(SPI_NOR_MAX_ID_LEN
, id
, 1));
2491 ret
= spi_mem_exec_op(nor
->spimem
, &op
);
2493 ret
= nor
->controller_ops
->read_reg(nor
, SPINOR_OP_RDID
, id
,
2494 SPI_NOR_MAX_ID_LEN
);
2497 dev_dbg(nor
->dev
, "error %d reading JEDEC ID\n", ret
);
2498 return ERR_PTR(ret
);
2501 for (i
= 0; i
< ARRAY_SIZE(manufacturers
); i
++) {
2502 info
= spi_nor_search_part_by_id(manufacturers
[i
]->parts
,
2503 manufacturers
[i
]->nparts
,
2506 nor
->manufacturer
= manufacturers
[i
];
2511 info
= spi_nor_search_part_by_id(spi_nor_ids
,
2512 ARRAY_SIZE(spi_nor_ids
) - 1, id
);
2516 dev_err(nor
->dev
, "unrecognized JEDEC id bytes: %*ph\n",
2517 SPI_NOR_MAX_ID_LEN
, id
);
2518 return ERR_PTR(-ENODEV
);
2521 static int spi_nor_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
2522 size_t *retlen
, u_char
*buf
)
2524 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
2527 dev_dbg(nor
->dev
, "from 0x%08x, len %zd\n", (u32
)from
, len
);
2529 ret
= spi_nor_lock_and_prep(nor
);
2536 addr
= spi_nor_convert_addr(nor
, addr
);
2538 ret
= spi_nor_read_data(nor
, addr
, len
, buf
);
2540 /* We shouldn't see 0-length reads */
2556 spi_nor_unlock_and_unprep(nor
);
2560 static int sst_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
2561 size_t *retlen
, const u_char
*buf
)
2563 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
2567 dev_dbg(nor
->dev
, "to 0x%08x, len %zd\n", (u32
)to
, len
);
2569 ret
= spi_nor_lock_and_prep(nor
);
2573 ret
= spi_nor_write_enable(nor
);
2577 nor
->sst_write_second
= false;
2579 /* Start write from odd address. */
2581 nor
->program_opcode
= SPINOR_OP_BP
;
2583 /* write one byte. */
2584 ret
= spi_nor_write_data(nor
, to
, 1, buf
);
2587 WARN(ret
!= 1, "While writing 1 byte written %i bytes\n", ret
);
2588 ret
= spi_nor_wait_till_ready(nor
);
2596 /* Write out most of the data here. */
2597 for (; actual
< len
- 1; actual
+= 2) {
2598 nor
->program_opcode
= SPINOR_OP_AAI_WP
;
2600 /* write two bytes. */
2601 ret
= spi_nor_write_data(nor
, to
, 2, buf
+ actual
);
2604 WARN(ret
!= 2, "While writing 2 bytes written %i bytes\n", ret
);
2605 ret
= spi_nor_wait_till_ready(nor
);
2609 nor
->sst_write_second
= true;
2611 nor
->sst_write_second
= false;
2613 ret
= spi_nor_write_disable(nor
);
2617 ret
= spi_nor_wait_till_ready(nor
);
2621 /* Write out trailing byte if it exists. */
2622 if (actual
!= len
) {
2623 ret
= spi_nor_write_enable(nor
);
2627 nor
->program_opcode
= SPINOR_OP_BP
;
2628 ret
= spi_nor_write_data(nor
, to
, 1, buf
+ actual
);
2631 WARN(ret
!= 1, "While writing 1 byte written %i bytes\n", ret
);
2632 ret
= spi_nor_wait_till_ready(nor
);
2638 ret
= spi_nor_write_disable(nor
);
2642 spi_nor_unlock_and_unprep(nor
);
2647 * Write an address range to the nor chip. Data must be written in
2648 * FLASH_PAGESIZE chunks. The address range may be any size provided
2649 * it is within the physical boundaries.
2651 static int spi_nor_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
2652 size_t *retlen
, const u_char
*buf
)
2654 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
2655 size_t page_offset
, page_remain
, i
;
2658 dev_dbg(nor
->dev
, "to 0x%08x, len %zd\n", (u32
)to
, len
);
2660 ret
= spi_nor_lock_and_prep(nor
);
2664 for (i
= 0; i
< len
; ) {
2666 loff_t addr
= to
+ i
;
2669 * If page_size is a power of two, the offset can be quickly
2670 * calculated with an AND operation. On the other cases we
2671 * need to do a modulus operation (more expensive).
2672 * Power of two numbers have only one bit set and we can use
2673 * the instruction hweight32 to detect if we need to do a
2674 * modulus (do_div()) or not.
2676 if (hweight32(nor
->page_size
) == 1) {
2677 page_offset
= addr
& (nor
->page_size
- 1);
2679 uint64_t aux
= addr
;
2681 page_offset
= do_div(aux
, nor
->page_size
);
2683 /* the size of data remaining on the first page */
2684 page_remain
= min_t(size_t,
2685 nor
->page_size
- page_offset
, len
- i
);
2687 addr
= spi_nor_convert_addr(nor
, addr
);
2689 ret
= spi_nor_write_enable(nor
);
2693 ret
= spi_nor_write_data(nor
, addr
, page_remain
, buf
+ i
);
2698 ret
= spi_nor_wait_till_ready(nor
);
2706 spi_nor_unlock_and_unprep(nor
);
2710 static int spi_nor_check(struct spi_nor
*nor
)
2713 (!nor
->spimem
&& !nor
->controller_ops
) ||
2714 (!nor
->spimem
&& nor
->controller_ops
&&
2715 (!nor
->controller_ops
->read
||
2716 !nor
->controller_ops
->write
||
2717 !nor
->controller_ops
->read_reg
||
2718 !nor
->controller_ops
->write_reg
))) {
2719 pr_err("spi-nor: please fill all the necessary fields!\n");
2723 if (nor
->spimem
&& nor
->controller_ops
) {
2724 dev_err(nor
->dev
, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one of them.\n");
2731 static int s3an_nor_setup(struct spi_nor
*nor
,
2732 const struct spi_nor_hwcaps
*hwcaps
)
2736 ret
= spi_nor_xread_sr(nor
, nor
->bouncebuf
);
2740 nor
->erase_opcode
= SPINOR_OP_XSE
;
2741 nor
->program_opcode
= SPINOR_OP_XPP
;
2742 nor
->read_opcode
= SPINOR_OP_READ
;
2743 nor
->flags
|= SNOR_F_NO_OP_CHIP_ERASE
;
2746 * This flashes have a page size of 264 or 528 bytes (known as
2747 * Default addressing mode). It can be changed to a more standard
2748 * Power of two mode where the page size is 256/512. This comes
2749 * with a price: there is 3% less of space, the data is corrupted
2750 * and the page size cannot be changed back to default addressing
2753 * The current addressing mode can be read from the XRDSR register
2754 * and should not be changed, because is a destructive operation.
2756 if (nor
->bouncebuf
[0] & XSR_PAGESIZE
) {
2757 /* Flash in Power of 2 mode */
2758 nor
->page_size
= (nor
->page_size
== 264) ? 256 : 512;
2759 nor
->mtd
.writebufsize
= nor
->page_size
;
2760 nor
->mtd
.size
= 8 * nor
->page_size
* nor
->info
->n_sectors
;
2761 nor
->mtd
.erasesize
= 8 * nor
->page_size
;
2763 /* Flash in Default addressing mode */
2764 nor
->params
.convert_addr
= s3an_convert_addr
;
2765 nor
->mtd
.erasesize
= nor
->info
->sector_size
;
2772 spi_nor_set_read_settings(struct spi_nor_read_command
*read
,
2776 enum spi_nor_protocol proto
)
2778 read
->num_mode_clocks
= num_mode_clocks
;
2779 read
->num_wait_states
= num_wait_states
;
2780 read
->opcode
= opcode
;
2781 read
->proto
= proto
;
2784 void spi_nor_set_pp_settings(struct spi_nor_pp_command
*pp
, u8 opcode
,
2785 enum spi_nor_protocol proto
)
2787 pp
->opcode
= opcode
;
2791 static int spi_nor_hwcaps2cmd(u32 hwcaps
, const int table
[][2], size_t size
)
2795 for (i
= 0; i
< size
; i
++)
2796 if (table
[i
][0] == (int)hwcaps
)
2802 int spi_nor_hwcaps_read2cmd(u32 hwcaps
)
2804 static const int hwcaps_read2cmd
[][2] = {
2805 { SNOR_HWCAPS_READ
, SNOR_CMD_READ
},
2806 { SNOR_HWCAPS_READ_FAST
, SNOR_CMD_READ_FAST
},
2807 { SNOR_HWCAPS_READ_1_1_1_DTR
, SNOR_CMD_READ_1_1_1_DTR
},
2808 { SNOR_HWCAPS_READ_1_1_2
, SNOR_CMD_READ_1_1_2
},
2809 { SNOR_HWCAPS_READ_1_2_2
, SNOR_CMD_READ_1_2_2
},
2810 { SNOR_HWCAPS_READ_2_2_2
, SNOR_CMD_READ_2_2_2
},
2811 { SNOR_HWCAPS_READ_1_2_2_DTR
, SNOR_CMD_READ_1_2_2_DTR
},
2812 { SNOR_HWCAPS_READ_1_1_4
, SNOR_CMD_READ_1_1_4
},
2813 { SNOR_HWCAPS_READ_1_4_4
, SNOR_CMD_READ_1_4_4
},
2814 { SNOR_HWCAPS_READ_4_4_4
, SNOR_CMD_READ_4_4_4
},
2815 { SNOR_HWCAPS_READ_1_4_4_DTR
, SNOR_CMD_READ_1_4_4_DTR
},
2816 { SNOR_HWCAPS_READ_1_1_8
, SNOR_CMD_READ_1_1_8
},
2817 { SNOR_HWCAPS_READ_1_8_8
, SNOR_CMD_READ_1_8_8
},
2818 { SNOR_HWCAPS_READ_8_8_8
, SNOR_CMD_READ_8_8_8
},
2819 { SNOR_HWCAPS_READ_1_8_8_DTR
, SNOR_CMD_READ_1_8_8_DTR
},
2822 return spi_nor_hwcaps2cmd(hwcaps
, hwcaps_read2cmd
,
2823 ARRAY_SIZE(hwcaps_read2cmd
));
2826 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps
)
2828 static const int hwcaps_pp2cmd
[][2] = {
2829 { SNOR_HWCAPS_PP
, SNOR_CMD_PP
},
2830 { SNOR_HWCAPS_PP_1_1_4
, SNOR_CMD_PP_1_1_4
},
2831 { SNOR_HWCAPS_PP_1_4_4
, SNOR_CMD_PP_1_4_4
},
2832 { SNOR_HWCAPS_PP_4_4_4
, SNOR_CMD_PP_4_4_4
},
2833 { SNOR_HWCAPS_PP_1_1_8
, SNOR_CMD_PP_1_1_8
},
2834 { SNOR_HWCAPS_PP_1_8_8
, SNOR_CMD_PP_1_8_8
},
2835 { SNOR_HWCAPS_PP_8_8_8
, SNOR_CMD_PP_8_8_8
},
2838 return spi_nor_hwcaps2cmd(hwcaps
, hwcaps_pp2cmd
,
2839 ARRAY_SIZE(hwcaps_pp2cmd
));
2843 * spi_nor_spimem_check_op - check if the operation is supported
2845 *@nor: pointer to a 'struct spi_nor'
2846 *@op: pointer to op template to be checked
2848 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2850 static int spi_nor_spimem_check_op(struct spi_nor
*nor
,
2851 struct spi_mem_op
*op
)
2854 * First test with 4 address bytes. The opcode itself might
2855 * be a 3B addressing opcode but we don't care, because
2856 * SPI controller implementation should not check the opcode,
2857 * but just the sequence.
2859 op
->addr
.nbytes
= 4;
2860 if (!spi_mem_supports_op(nor
->spimem
, op
)) {
2861 if (nor
->mtd
.size
> SZ_16M
)
2864 /* If flash size <= 16MB, 3 address bytes are sufficient */
2865 op
->addr
.nbytes
= 3;
2866 if (!spi_mem_supports_op(nor
->spimem
, op
))
2874 * spi_nor_spimem_check_readop - check if the read op is supported
2876 *@nor: pointer to a 'struct spi_nor'
2877 *@read: pointer to op template to be checked
2879 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2881 static int spi_nor_spimem_check_readop(struct spi_nor
*nor
,
2882 const struct spi_nor_read_command
*read
)
2884 struct spi_mem_op op
= SPI_MEM_OP(SPI_MEM_OP_CMD(read
->opcode
, 1),
2885 SPI_MEM_OP_ADDR(3, 0, 1),
2886 SPI_MEM_OP_DUMMY(0, 1),
2887 SPI_MEM_OP_DATA_IN(0, NULL
, 1));
2889 op
.cmd
.buswidth
= spi_nor_get_protocol_inst_nbits(read
->proto
);
2890 op
.addr
.buswidth
= spi_nor_get_protocol_addr_nbits(read
->proto
);
2891 op
.data
.buswidth
= spi_nor_get_protocol_data_nbits(read
->proto
);
2892 op
.dummy
.buswidth
= op
.addr
.buswidth
;
2893 op
.dummy
.nbytes
= (read
->num_mode_clocks
+ read
->num_wait_states
) *
2894 op
.dummy
.buswidth
/ 8;
2896 return spi_nor_spimem_check_op(nor
, &op
);
2900 * spi_nor_spimem_check_pp - check if the page program op is supported
2902 *@nor: pointer to a 'struct spi_nor'
2903 *@pp: pointer to op template to be checked
2905 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2907 static int spi_nor_spimem_check_pp(struct spi_nor
*nor
,
2908 const struct spi_nor_pp_command
*pp
)
2910 struct spi_mem_op op
= SPI_MEM_OP(SPI_MEM_OP_CMD(pp
->opcode
, 1),
2911 SPI_MEM_OP_ADDR(3, 0, 1),
2912 SPI_MEM_OP_NO_DUMMY
,
2913 SPI_MEM_OP_DATA_OUT(0, NULL
, 1));
2915 op
.cmd
.buswidth
= spi_nor_get_protocol_inst_nbits(pp
->proto
);
2916 op
.addr
.buswidth
= spi_nor_get_protocol_addr_nbits(pp
->proto
);
2917 op
.data
.buswidth
= spi_nor_get_protocol_data_nbits(pp
->proto
);
2919 return spi_nor_spimem_check_op(nor
, &op
);
2923 * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
2924 * based on SPI controller capabilities
2925 * @nor: pointer to a 'struct spi_nor'
2926 * @hwcaps: pointer to resulting capabilities after adjusting
2927 * according to controller and flash's capability
2930 spi_nor_spimem_adjust_hwcaps(struct spi_nor
*nor
, u32
*hwcaps
)
2932 struct spi_nor_flash_parameter
*params
= &nor
->params
;
2935 /* DTR modes are not supported yet, mask them all. */
2936 *hwcaps
&= ~SNOR_HWCAPS_DTR
;
2938 /* X-X-X modes are not supported yet, mask them all. */
2939 *hwcaps
&= ~SNOR_HWCAPS_X_X_X
;
2941 for (cap
= 0; cap
< sizeof(*hwcaps
) * BITS_PER_BYTE
; cap
++) {
2944 if (!(*hwcaps
& BIT(cap
)))
2947 rdidx
= spi_nor_hwcaps_read2cmd(BIT(cap
));
2949 spi_nor_spimem_check_readop(nor
, ¶ms
->reads
[rdidx
]))
2950 *hwcaps
&= ~BIT(cap
);
2952 ppidx
= spi_nor_hwcaps_pp2cmd(BIT(cap
));
2956 if (spi_nor_spimem_check_pp(nor
,
2957 ¶ms
->page_programs
[ppidx
]))
2958 *hwcaps
&= ~BIT(cap
);
2963 * spi_nor_set_erase_type() - set a SPI NOR erase type
2964 * @erase: pointer to a structure that describes a SPI NOR erase type
2965 * @size: the size of the sector/block erased by the erase type
2966 * @opcode: the SPI command op code to erase the sector/block
2968 void spi_nor_set_erase_type(struct spi_nor_erase_type
*erase
, u32 size
,
2972 erase
->opcode
= opcode
;
2973 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
2974 erase
->size_shift
= ffs(erase
->size
) - 1;
2975 erase
->size_mask
= (1 << erase
->size_shift
) - 1;
2979 * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
2980 * @map: the erase map of the SPI NOR
2981 * @erase_mask: bitmask encoding erase types that can erase the entire
2983 * @flash_size: the spi nor flash memory size
2985 void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map
*map
,
2986 u8 erase_mask
, u64 flash_size
)
2988 /* Offset 0 with erase_mask and SNOR_LAST_REGION bit set */
2989 map
->uniform_region
.offset
= (erase_mask
& SNOR_ERASE_TYPE_MASK
) |
2991 map
->uniform_region
.size
= flash_size
;
2992 map
->regions
= &map
->uniform_region
;
2993 map
->uniform_erase_type
= erase_mask
;
2996 int spi_nor_post_bfpt_fixups(struct spi_nor
*nor
,
2997 const struct sfdp_parameter_header
*bfpt_header
,
2998 const struct sfdp_bfpt
*bfpt
,
2999 struct spi_nor_flash_parameter
*params
)
3003 if (nor
->manufacturer
&& nor
->manufacturer
->fixups
&&
3004 nor
->manufacturer
->fixups
->post_bfpt
) {
3005 ret
= nor
->manufacturer
->fixups
->post_bfpt(nor
, bfpt_header
,
3011 if (nor
->info
->fixups
&& nor
->info
->fixups
->post_bfpt
)
3012 return nor
->info
->fixups
->post_bfpt(nor
, bfpt_header
, bfpt
,
3018 static int spi_nor_select_read(struct spi_nor
*nor
,
3021 int cmd
, best_match
= fls(shared_hwcaps
& SNOR_HWCAPS_READ_MASK
) - 1;
3022 const struct spi_nor_read_command
*read
;
3027 cmd
= spi_nor_hwcaps_read2cmd(BIT(best_match
));
3031 read
= &nor
->params
.reads
[cmd
];
3032 nor
->read_opcode
= read
->opcode
;
3033 nor
->read_proto
= read
->proto
;
3036 * In the spi-nor framework, we don't need to make the difference
3037 * between mode clock cycles and wait state clock cycles.
3038 * Indeed, the value of the mode clock cycles is used by a QSPI
3039 * flash memory to know whether it should enter or leave its 0-4-4
3040 * (Continuous Read / XIP) mode.
3041 * eXecution In Place is out of the scope of the mtd sub-system.
3042 * Hence we choose to merge both mode and wait state clock cycles
3043 * into the so called dummy clock cycles.
3045 nor
->read_dummy
= read
->num_mode_clocks
+ read
->num_wait_states
;
3049 static int spi_nor_select_pp(struct spi_nor
*nor
,
3052 int cmd
, best_match
= fls(shared_hwcaps
& SNOR_HWCAPS_PP_MASK
) - 1;
3053 const struct spi_nor_pp_command
*pp
;
3058 cmd
= spi_nor_hwcaps_pp2cmd(BIT(best_match
));
3062 pp
= &nor
->params
.page_programs
[cmd
];
3063 nor
->program_opcode
= pp
->opcode
;
3064 nor
->write_proto
= pp
->proto
;
3069 * spi_nor_select_uniform_erase() - select optimum uniform erase type
3070 * @map: the erase map of the SPI NOR
3071 * @wanted_size: the erase type size to search for. Contains the value of
3072 * info->sector_size or of the "small sector" size in case
3073 * CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined.
3075 * Once the optimum uniform sector erase command is found, disable all the
3078 * Return: pointer to erase type on success, NULL otherwise.
3080 static const struct spi_nor_erase_type
*
3081 spi_nor_select_uniform_erase(struct spi_nor_erase_map
*map
,
3082 const u32 wanted_size
)
3084 const struct spi_nor_erase_type
*tested_erase
, *erase
= NULL
;
3086 u8 uniform_erase_type
= map
->uniform_erase_type
;
3088 for (i
= SNOR_ERASE_TYPE_MAX
- 1; i
>= 0; i
--) {
3089 if (!(uniform_erase_type
& BIT(i
)))
3092 tested_erase
= &map
->erase_type
[i
];
3095 * If the current erase size is the one, stop here:
3096 * we have found the right uniform Sector Erase command.
3098 if (tested_erase
->size
== wanted_size
) {
3099 erase
= tested_erase
;
3104 * Otherwise, the current erase size is still a valid canditate.
3105 * Select the biggest valid candidate.
3107 if (!erase
&& tested_erase
->size
)
3108 erase
= tested_erase
;
3109 /* keep iterating to find the wanted_size */
3115 /* Disable all other Sector Erase commands. */
3116 map
->uniform_erase_type
&= ~SNOR_ERASE_TYPE_MASK
;
3117 map
->uniform_erase_type
|= BIT(erase
- map
->erase_type
);
3121 static int spi_nor_select_erase(struct spi_nor
*nor
)
3123 struct spi_nor_erase_map
*map
= &nor
->params
.erase_map
;
3124 const struct spi_nor_erase_type
*erase
= NULL
;
3125 struct mtd_info
*mtd
= &nor
->mtd
;
3126 u32 wanted_size
= nor
->info
->sector_size
;
3130 * The previous implementation handling Sector Erase commands assumed
3131 * that the SPI flash memory has an uniform layout then used only one
3132 * of the supported erase sizes for all Sector Erase commands.
3133 * So to be backward compatible, the new implementation also tries to
3134 * manage the SPI flash memory as uniform with a single erase sector
3135 * size, when possible.
3137 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
3138 /* prefer "small sector" erase if possible */
3139 wanted_size
= 4096u;
3142 if (spi_nor_has_uniform_erase(nor
)) {
3143 erase
= spi_nor_select_uniform_erase(map
, wanted_size
);
3146 nor
->erase_opcode
= erase
->opcode
;
3147 mtd
->erasesize
= erase
->size
;
3152 * For non-uniform SPI flash memory, set mtd->erasesize to the
3153 * maximum erase sector size. No need to set nor->erase_opcode.
3155 for (i
= SNOR_ERASE_TYPE_MAX
- 1; i
>= 0; i
--) {
3156 if (map
->erase_type
[i
].size
) {
3157 erase
= &map
->erase_type
[i
];
3165 mtd
->erasesize
= erase
->size
;
3169 static int spi_nor_default_setup(struct spi_nor
*nor
,
3170 const struct spi_nor_hwcaps
*hwcaps
)
3172 struct spi_nor_flash_parameter
*params
= &nor
->params
;
3173 u32 ignored_mask
, shared_mask
;
3177 * Keep only the hardware capabilities supported by both the SPI
3178 * controller and the SPI flash memory.
3180 shared_mask
= hwcaps
->mask
& params
->hwcaps
.mask
;
3184 * When called from spi_nor_probe(), all caps are set and we
3185 * need to discard some of them based on what the SPI
3186 * controller actually supports (using spi_mem_supports_op()).
3188 spi_nor_spimem_adjust_hwcaps(nor
, &shared_mask
);
3191 * SPI n-n-n protocols are not supported when the SPI
3192 * controller directly implements the spi_nor interface.
3193 * Yet another reason to switch to spi-mem.
3195 ignored_mask
= SNOR_HWCAPS_X_X_X
;
3196 if (shared_mask
& ignored_mask
) {
3198 "SPI n-n-n protocols are not supported.\n");
3199 shared_mask
&= ~ignored_mask
;
3203 /* Select the (Fast) Read command. */
3204 err
= spi_nor_select_read(nor
, shared_mask
);
3207 "can't select read settings supported by both the SPI controller and memory.\n");
3211 /* Select the Page Program command. */
3212 err
= spi_nor_select_pp(nor
, shared_mask
);
3215 "can't select write settings supported by both the SPI controller and memory.\n");
3219 /* Select the Sector Erase command. */
3220 err
= spi_nor_select_erase(nor
);
3223 "can't select erase settings supported by both the SPI controller and memory.\n");
3230 static int spi_nor_setup(struct spi_nor
*nor
,
3231 const struct spi_nor_hwcaps
*hwcaps
)
3233 if (!nor
->params
.setup
)
3236 return nor
->params
.setup(nor
, hwcaps
);
3239 static void intel_set_default_init(struct spi_nor
*nor
)
3241 nor
->flags
|= SNOR_F_HAS_LOCK
;
3244 static void issi_set_default_init(struct spi_nor
*nor
)
3246 nor
->params
.quad_enable
= spi_nor_sr1_bit6_quad_enable
;
3249 static void macronix_set_default_init(struct spi_nor
*nor
)
3251 nor
->params
.quad_enable
= spi_nor_sr1_bit6_quad_enable
;
3252 nor
->params
.set_4byte_addr_mode
= spi_nor_set_4byte_addr_mode
;
3255 static void sst_set_default_init(struct spi_nor
*nor
)
3257 nor
->flags
|= SNOR_F_HAS_LOCK
;
3260 static void st_micron_set_default_init(struct spi_nor
*nor
)
3262 nor
->flags
|= SNOR_F_HAS_LOCK
;
3263 nor
->flags
&= ~SNOR_F_HAS_16BIT_SR
;
3264 nor
->params
.quad_enable
= NULL
;
3265 nor
->params
.set_4byte_addr_mode
= st_micron_set_4byte_addr_mode
;
3268 static void winbond_set_default_init(struct spi_nor
*nor
)
3270 nor
->params
.set_4byte_addr_mode
= winbond_set_4byte_addr_mode
;
3274 * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and
3275 * settings based on MFR register and ->default_init() hook.
3276 * @nor: pointer to a 'struct spi-nor'.
3278 static void spi_nor_manufacturer_init_params(struct spi_nor
*nor
)
3280 /* Init flash parameters based on MFR */
3281 switch (JEDEC_MFR(nor
->info
)) {
3282 case SNOR_MFR_INTEL
:
3283 intel_set_default_init(nor
);
3287 issi_set_default_init(nor
);
3290 case SNOR_MFR_MACRONIX
:
3291 macronix_set_default_init(nor
);
3295 case SNOR_MFR_MICRON
:
3296 st_micron_set_default_init(nor
);
3300 sst_set_default_init(nor
);
3303 case SNOR_MFR_WINBOND
:
3304 winbond_set_default_init(nor
);
3311 if (nor
->manufacturer
&& nor
->manufacturer
->fixups
&&
3312 nor
->manufacturer
->fixups
->default_init
)
3313 nor
->manufacturer
->fixups
->default_init(nor
);
3315 if (nor
->info
->fixups
&& nor
->info
->fixups
->default_init
)
3316 nor
->info
->fixups
->default_init(nor
);
3320 * spi_nor_sfdp_init_params() - Initialize the flash's parameters and settings
3321 * based on JESD216 SFDP standard.
3322 * @nor: pointer to a 'struct spi-nor'.
3324 * The method has a roll-back mechanism: in case the SFDP parsing fails, the
3325 * legacy flash parameters and settings will be restored.
3327 static void spi_nor_sfdp_init_params(struct spi_nor
*nor
)
3329 struct spi_nor_flash_parameter sfdp_params
;
3331 memcpy(&sfdp_params
, &nor
->params
, sizeof(sfdp_params
));
3333 if (spi_nor_parse_sfdp(nor
, &sfdp_params
)) {
3334 nor
->addr_width
= 0;
3335 nor
->flags
&= ~SNOR_F_4B_OPCODES
;
3337 memcpy(&nor
->params
, &sfdp_params
, sizeof(nor
->params
));
3342 * spi_nor_info_init_params() - Initialize the flash's parameters and settings
3343 * based on nor->info data.
3344 * @nor: pointer to a 'struct spi-nor'.
3346 static void spi_nor_info_init_params(struct spi_nor
*nor
)
3348 struct spi_nor_flash_parameter
*params
= &nor
->params
;
3349 struct spi_nor_erase_map
*map
= ¶ms
->erase_map
;
3350 const struct flash_info
*info
= nor
->info
;
3351 struct device_node
*np
= spi_nor_get_flash_node(nor
);
3354 /* Initialize legacy flash parameters and settings. */
3355 params
->quad_enable
= spi_nor_sr2_bit1_quad_enable
;
3356 params
->set_4byte_addr_mode
= spansion_set_4byte_addr_mode
;
3357 params
->setup
= spi_nor_default_setup
;
3358 /* Default to 16-bit Write Status (01h) Command */
3359 nor
->flags
|= SNOR_F_HAS_16BIT_SR
;
3361 /* Set SPI NOR sizes. */
3362 params
->size
= (u64
)info
->sector_size
* info
->n_sectors
;
3363 params
->page_size
= info
->page_size
;
3365 if (!(info
->flags
& SPI_NOR_NO_FR
)) {
3366 /* Default to Fast Read for DT and non-DT platform devices. */
3367 params
->hwcaps
.mask
|= SNOR_HWCAPS_READ_FAST
;
3369 /* Mask out Fast Read if not requested at DT instantiation. */
3370 if (np
&& !of_property_read_bool(np
, "m25p,fast-read"))
3371 params
->hwcaps
.mask
&= ~SNOR_HWCAPS_READ_FAST
;
3374 /* (Fast) Read settings. */
3375 params
->hwcaps
.mask
|= SNOR_HWCAPS_READ
;
3376 spi_nor_set_read_settings(¶ms
->reads
[SNOR_CMD_READ
],
3377 0, 0, SPINOR_OP_READ
,
3380 if (params
->hwcaps
.mask
& SNOR_HWCAPS_READ_FAST
)
3381 spi_nor_set_read_settings(¶ms
->reads
[SNOR_CMD_READ_FAST
],
3382 0, 8, SPINOR_OP_READ_FAST
,
3385 if (info
->flags
& SPI_NOR_DUAL_READ
) {
3386 params
->hwcaps
.mask
|= SNOR_HWCAPS_READ_1_1_2
;
3387 spi_nor_set_read_settings(¶ms
->reads
[SNOR_CMD_READ_1_1_2
],
3388 0, 8, SPINOR_OP_READ_1_1_2
,
3392 if (info
->flags
& SPI_NOR_QUAD_READ
) {
3393 params
->hwcaps
.mask
|= SNOR_HWCAPS_READ_1_1_4
;
3394 spi_nor_set_read_settings(¶ms
->reads
[SNOR_CMD_READ_1_1_4
],
3395 0, 8, SPINOR_OP_READ_1_1_4
,
3399 if (info
->flags
& SPI_NOR_OCTAL_READ
) {
3400 params
->hwcaps
.mask
|= SNOR_HWCAPS_READ_1_1_8
;
3401 spi_nor_set_read_settings(¶ms
->reads
[SNOR_CMD_READ_1_1_8
],
3402 0, 8, SPINOR_OP_READ_1_1_8
,
3406 /* Page Program settings. */
3407 params
->hwcaps
.mask
|= SNOR_HWCAPS_PP
;
3408 spi_nor_set_pp_settings(¶ms
->page_programs
[SNOR_CMD_PP
],
3409 SPINOR_OP_PP
, SNOR_PROTO_1_1_1
);
3412 * Sector Erase settings. Sort Erase Types in ascending order, with the
3413 * smallest erase size starting at BIT(0).
3417 if (info
->flags
& SECT_4K_PMC
) {
3418 erase_mask
|= BIT(i
);
3419 spi_nor_set_erase_type(&map
->erase_type
[i
], 4096u,
3420 SPINOR_OP_BE_4K_PMC
);
3422 } else if (info
->flags
& SECT_4K
) {
3423 erase_mask
|= BIT(i
);
3424 spi_nor_set_erase_type(&map
->erase_type
[i
], 4096u,
3428 erase_mask
|= BIT(i
);
3429 spi_nor_set_erase_type(&map
->erase_type
[i
], info
->sector_size
,
3431 spi_nor_init_uniform_erase_map(map
, erase_mask
, params
->size
);
3434 static void spansion_post_sfdp_fixups(struct spi_nor
*nor
)
3436 if (nor
->params
.size
<= SZ_16M
)
3439 nor
->flags
|= SNOR_F_4B_OPCODES
;
3440 /* No small sector erase for 4-byte command set */
3441 nor
->erase_opcode
= SPINOR_OP_SE
;
3442 nor
->mtd
.erasesize
= nor
->info
->sector_size
;
3445 static void s3an_post_sfdp_fixups(struct spi_nor
*nor
)
3447 nor
->params
.setup
= s3an_nor_setup
;
3451 * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
3452 * after SFDP has been parsed (is also called for SPI NORs that do not
3454 * @nor: pointer to a 'struct spi_nor'
3456 * Typically used to tweak various parameters that could not be extracted by
3457 * other means (i.e. when information provided by the SFDP/flash_info tables
3458 * are incomplete or wrong).
3460 static void spi_nor_post_sfdp_fixups(struct spi_nor
*nor
)
3462 switch (JEDEC_MFR(nor
->info
)) {
3463 case SNOR_MFR_SPANSION
:
3464 spansion_post_sfdp_fixups(nor
);
3471 if (nor
->info
->flags
& SPI_S3AN
)
3472 s3an_post_sfdp_fixups(nor
);
3474 if (nor
->manufacturer
&& nor
->manufacturer
->fixups
&&
3475 nor
->manufacturer
->fixups
->post_sfdp
)
3476 nor
->manufacturer
->fixups
->post_sfdp(nor
);
3478 if (nor
->info
->fixups
&& nor
->info
->fixups
->post_sfdp
)
3479 nor
->info
->fixups
->post_sfdp(nor
);
3483 * spi_nor_late_init_params() - Late initialization of default flash parameters.
3484 * @nor: pointer to a 'struct spi_nor'
3486 * Used to set default flash parameters and settings when the ->default_init()
3487 * hook or the SFDP parser let voids.
3489 static void spi_nor_late_init_params(struct spi_nor
*nor
)
3492 * NOR protection support. When locking_ops are not provided, we pick
3495 if (nor
->flags
& SNOR_F_HAS_LOCK
&& !nor
->params
.locking_ops
)
3496 nor
->params
.locking_ops
= &spi_nor_sr_locking_ops
;
3500 * spi_nor_init_params() - Initialize the flash's parameters and settings.
3501 * @nor: pointer to a 'struct spi-nor'.
3503 * The flash parameters and settings are initialized based on a sequence of
3504 * calls that are ordered by priority:
3506 * 1/ Default flash parameters initialization. The initializations are done
3507 * based on nor->info data:
3508 * spi_nor_info_init_params()
3510 * which can be overwritten by:
3511 * 2/ Manufacturer flash parameters initialization. The initializations are
3512 * done based on MFR register, or when the decisions can not be done solely
3513 * based on MFR, by using specific flash_info tweeks, ->default_init():
3514 * spi_nor_manufacturer_init_params()
3516 * which can be overwritten by:
3517 * 3/ SFDP flash parameters initialization. JESD216 SFDP is a standard and
3518 * should be more accurate that the above.
3519 * spi_nor_sfdp_init_params()
3521 * Please note that there is a ->post_bfpt() fixup hook that can overwrite
3522 * the flash parameters and settings immediately after parsing the Basic
3523 * Flash Parameter Table.
3525 * which can be overwritten by:
3526 * 4/ Post SFDP flash parameters initialization. Used to tweak various
3527 * parameters that could not be extracted by other means (i.e. when
3528 * information provided by the SFDP/flash_info tables are incomplete or
3530 * spi_nor_post_sfdp_fixups()
3532 * 5/ Late default flash parameters initialization, used when the
3533 * ->default_init() hook or the SFDP parser do not set specific params.
3534 * spi_nor_late_init_params()
3536 static void spi_nor_init_params(struct spi_nor
*nor
)
3538 spi_nor_info_init_params(nor
);
3540 spi_nor_manufacturer_init_params(nor
);
3542 if ((nor
->info
->flags
& (SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
)) &&
3543 !(nor
->info
->flags
& SPI_NOR_SKIP_SFDP
))
3544 spi_nor_sfdp_init_params(nor
);
3546 spi_nor_post_sfdp_fixups(nor
);
3548 spi_nor_late_init_params(nor
);
3552 * spi_nor_quad_enable() - enable Quad I/O if needed.
3553 * @nor: pointer to a 'struct spi_nor'
3555 * Return: 0 on success, -errno otherwise.
3557 static int spi_nor_quad_enable(struct spi_nor
*nor
)
3559 if (!nor
->params
.quad_enable
)
3562 if (!(spi_nor_get_protocol_width(nor
->read_proto
) == 4 ||
3563 spi_nor_get_protocol_width(nor
->write_proto
) == 4))
3566 return nor
->params
.quad_enable(nor
);
3570 * spi_nor_unlock_all() - Unlocks the entire flash memory array.
3571 * @nor: pointer to a 'struct spi_nor'.
3573 * Some SPI NOR flashes are write protected by default after a power-on reset
3574 * cycle, in order to avoid inadvertent writes during power-up. Backward
3575 * compatibility imposes to unlock the entire flash memory array at power-up
3578 static int spi_nor_unlock_all(struct spi_nor
*nor
)
3580 if (nor
->flags
& SNOR_F_HAS_LOCK
)
3581 return spi_nor_unlock(&nor
->mtd
, 0, nor
->params
.size
);
3586 static int spi_nor_init(struct spi_nor
*nor
)
3590 err
= spi_nor_quad_enable(nor
);
3592 dev_dbg(nor
->dev
, "quad mode not supported\n");
3596 err
= spi_nor_unlock_all(nor
);
3598 dev_dbg(nor
->dev
, "Failed to unlock the entire flash memory array\n");
3602 if (nor
->addr_width
== 4 && !(nor
->flags
& SNOR_F_4B_OPCODES
)) {
3604 * If the RESET# pin isn't hooked up properly, or the system
3605 * otherwise doesn't perform a reset command in the boot
3606 * sequence, it's impossible to 100% protect against unexpected
3607 * reboots (e.g., crashes). Warn the user (or hopefully, system
3608 * designer) that this is bad.
3610 WARN_ONCE(nor
->flags
& SNOR_F_BROKEN_RESET
,
3611 "enabling reset hack; may not recover from unexpected reboots\n");
3612 nor
->params
.set_4byte_addr_mode(nor
, true);
3618 /* mtd resume handler */
3619 static void spi_nor_resume(struct mtd_info
*mtd
)
3621 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
3622 struct device
*dev
= nor
->dev
;
3625 /* re-initialize the nor chip */
3626 ret
= spi_nor_init(nor
);
3628 dev_err(dev
, "resume() failed\n");
3631 void spi_nor_restore(struct spi_nor
*nor
)
3633 /* restore the addressing mode */
3634 if (nor
->addr_width
== 4 && !(nor
->flags
& SNOR_F_4B_OPCODES
) &&
3635 nor
->flags
& SNOR_F_BROKEN_RESET
)
3636 nor
->params
.set_4byte_addr_mode(nor
, false);
3638 EXPORT_SYMBOL_GPL(spi_nor_restore
);
3640 static const struct flash_info
*spi_nor_match_id(struct spi_nor
*nor
,
3645 for (i
= 0; i
< ARRAY_SIZE(spi_nor_ids
) - 1; i
++) {
3646 if (!strcmp(name
, spi_nor_ids
[i
].name
))
3647 return &spi_nor_ids
[i
];
3650 for (i
= 0; i
< ARRAY_SIZE(manufacturers
); i
++) {
3651 for (j
= 0; j
< manufacturers
[i
]->nparts
; j
++) {
3652 if (!strcmp(name
, manufacturers
[i
]->parts
[j
].name
)) {
3653 nor
->manufacturer
= manufacturers
[i
];
3654 return &manufacturers
[i
]->parts
[j
];
3662 static int spi_nor_set_addr_width(struct spi_nor
*nor
)
3664 if (nor
->addr_width
) {
3665 /* already configured from SFDP */
3666 } else if (nor
->info
->addr_width
) {
3667 nor
->addr_width
= nor
->info
->addr_width
;
3668 } else if (nor
->mtd
.size
> 0x1000000) {
3669 /* enable 4-byte addressing if the device exceeds 16MiB */
3670 nor
->addr_width
= 4;
3672 nor
->addr_width
= 3;
3675 if (nor
->addr_width
> SPI_NOR_MAX_ADDR_WIDTH
) {
3676 dev_dbg(nor
->dev
, "address width is too large: %u\n",
3681 /* Set 4byte opcodes when possible. */
3682 if (nor
->addr_width
== 4 && nor
->flags
& SNOR_F_4B_OPCODES
&&
3683 !(nor
->flags
& SNOR_F_HAS_4BAIT
))
3684 spi_nor_set_4byte_opcodes(nor
);
3689 static void spi_nor_debugfs_init(struct spi_nor
*nor
,
3690 const struct flash_info
*info
)
3692 struct mtd_info
*mtd
= &nor
->mtd
;
3694 mtd
->dbg
.partname
= info
->name
;
3695 mtd
->dbg
.partid
= devm_kasprintf(nor
->dev
, GFP_KERNEL
, "spi-nor:%*phN",
3696 info
->id_len
, info
->id
);
3699 static const struct flash_info
*spi_nor_get_flash_info(struct spi_nor
*nor
,
3702 const struct flash_info
*info
= NULL
;
3705 info
= spi_nor_match_id(nor
, name
);
3706 /* Try to auto-detect if chip name wasn't specified or not found */
3708 info
= spi_nor_read_id(nor
);
3709 if (IS_ERR_OR_NULL(info
))
3710 return ERR_PTR(-ENOENT
);
3713 * If caller has specified name of flash model that can normally be
3714 * detected using JEDEC, let's verify it.
3716 if (name
&& info
->id_len
) {
3717 const struct flash_info
*jinfo
;
3719 jinfo
= spi_nor_read_id(nor
);
3720 if (IS_ERR(jinfo
)) {
3722 } else if (jinfo
!= info
) {
3724 * JEDEC knows better, so overwrite platform ID. We
3725 * can't trust partitions any longer, but we'll let
3726 * mtd apply them anyway, since some partitions may be
3727 * marked read-only, and we don't want to lose that
3728 * information, even if it's not 100% accurate.
3730 dev_warn(nor
->dev
, "found %s, expected %s\n",
3731 jinfo
->name
, info
->name
);
3739 int spi_nor_scan(struct spi_nor
*nor
, const char *name
,
3740 const struct spi_nor_hwcaps
*hwcaps
)
3742 const struct flash_info
*info
;
3743 struct device
*dev
= nor
->dev
;
3744 struct mtd_info
*mtd
= &nor
->mtd
;
3745 struct device_node
*np
= spi_nor_get_flash_node(nor
);
3746 struct spi_nor_flash_parameter
*params
= &nor
->params
;
3750 ret
= spi_nor_check(nor
);
3754 /* Reset SPI protocol for all commands. */
3755 nor
->reg_proto
= SNOR_PROTO_1_1_1
;
3756 nor
->read_proto
= SNOR_PROTO_1_1_1
;
3757 nor
->write_proto
= SNOR_PROTO_1_1_1
;
3760 * We need the bounce buffer early to read/write registers when going
3761 * through the spi-mem layer (buffers have to be DMA-able).
3762 * For spi-mem drivers, we'll reallocate a new buffer if
3763 * nor->page_size turns out to be greater than PAGE_SIZE (which
3764 * shouldn't happen before long since NOR pages are usually less
3765 * than 1KB) after spi_nor_scan() returns.
3767 nor
->bouncebuf_size
= PAGE_SIZE
;
3768 nor
->bouncebuf
= devm_kmalloc(dev
, nor
->bouncebuf_size
,
3770 if (!nor
->bouncebuf
)
3773 info
= spi_nor_get_flash_info(nor
, name
);
3775 return PTR_ERR(info
);
3779 spi_nor_debugfs_init(nor
, info
);
3781 mutex_init(&nor
->lock
);
3784 * Make sure the XSR_RDY flag is set before calling
3785 * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
3786 * with Atmel spi-nor
3788 if (info
->flags
& SPI_NOR_XSR_RDY
)
3789 nor
->flags
|= SNOR_F_READY_XSR_RDY
;
3791 if (info
->flags
& SPI_NOR_HAS_LOCK
)
3792 nor
->flags
|= SNOR_F_HAS_LOCK
;
3794 /* Init flash parameters based on flash_info struct and SFDP */
3795 spi_nor_init_params(nor
);
3798 mtd
->name
= dev_name(dev
);
3800 mtd
->type
= MTD_NORFLASH
;
3802 mtd
->flags
= MTD_CAP_NORFLASH
;
3803 mtd
->size
= params
->size
;
3804 mtd
->_erase
= spi_nor_erase
;
3805 mtd
->_read
= spi_nor_read
;
3806 mtd
->_resume
= spi_nor_resume
;
3808 if (nor
->params
.locking_ops
) {
3809 mtd
->_lock
= spi_nor_lock
;
3810 mtd
->_unlock
= spi_nor_unlock
;
3811 mtd
->_is_locked
= spi_nor_is_locked
;
3814 /* sst nor chips use AAI word program */
3815 if (info
->flags
& SST_WRITE
)
3816 mtd
->_write
= sst_write
;
3818 mtd
->_write
= spi_nor_write
;
3820 if (info
->flags
& USE_FSR
)
3821 nor
->flags
|= SNOR_F_USE_FSR
;
3822 if (info
->flags
& SPI_NOR_HAS_TB
) {
3823 nor
->flags
|= SNOR_F_HAS_SR_TB
;
3824 if (info
->flags
& SPI_NOR_TB_SR_BIT6
)
3825 nor
->flags
|= SNOR_F_HAS_SR_TB_BIT6
;
3828 if (info
->flags
& NO_CHIP_ERASE
)
3829 nor
->flags
|= SNOR_F_NO_OP_CHIP_ERASE
;
3830 if (info
->flags
& USE_CLSR
)
3831 nor
->flags
|= SNOR_F_USE_CLSR
;
3833 if (info
->flags
& SPI_NOR_NO_ERASE
)
3834 mtd
->flags
|= MTD_NO_ERASE
;
3836 mtd
->dev
.parent
= dev
;
3837 nor
->page_size
= params
->page_size
;
3838 mtd
->writebufsize
= nor
->page_size
;
3840 if (of_property_read_bool(np
, "broken-flash-reset"))
3841 nor
->flags
|= SNOR_F_BROKEN_RESET
;
3844 * Configure the SPI memory:
3845 * - select op codes for (Fast) Read, Page Program and Sector Erase.
3846 * - set the number of dummy cycles (mode cycles + wait states).
3847 * - set the SPI protocols for register and memory accesses.
3849 ret
= spi_nor_setup(nor
, hwcaps
);
3853 if (info
->flags
& SPI_NOR_4B_OPCODES
)
3854 nor
->flags
|= SNOR_F_4B_OPCODES
;
3856 ret
= spi_nor_set_addr_width(nor
);
3860 /* Send all the required SPI flash commands to initialize device */
3861 ret
= spi_nor_init(nor
);
3865 dev_info(dev
, "%s (%lld Kbytes)\n", info
->name
,
3866 (long long)mtd
->size
>> 10);
3869 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
3870 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
3871 mtd
->name
, (long long)mtd
->size
, (long long)(mtd
->size
>> 20),
3872 mtd
->erasesize
, mtd
->erasesize
/ 1024, mtd
->numeraseregions
);
3874 if (mtd
->numeraseregions
)
3875 for (i
= 0; i
< mtd
->numeraseregions
; i
++)
3877 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
3878 ".erasesize = 0x%.8x (%uKiB), "
3879 ".numblocks = %d }\n",
3880 i
, (long long)mtd
->eraseregions
[i
].offset
,
3881 mtd
->eraseregions
[i
].erasesize
,
3882 mtd
->eraseregions
[i
].erasesize
/ 1024,
3883 mtd
->eraseregions
[i
].numblocks
);
3886 EXPORT_SYMBOL_GPL(spi_nor_scan
);
3888 static int spi_nor_create_read_dirmap(struct spi_nor
*nor
)
3890 struct spi_mem_dirmap_info info
= {
3891 .op_tmpl
= SPI_MEM_OP(SPI_MEM_OP_CMD(nor
->read_opcode
, 1),
3892 SPI_MEM_OP_ADDR(nor
->addr_width
, 0, 1),
3893 SPI_MEM_OP_DUMMY(nor
->read_dummy
, 1),
3894 SPI_MEM_OP_DATA_IN(0, NULL
, 1)),
3896 .length
= nor
->mtd
.size
,
3898 struct spi_mem_op
*op
= &info
.op_tmpl
;
3900 /* get transfer protocols. */
3901 op
->cmd
.buswidth
= spi_nor_get_protocol_inst_nbits(nor
->read_proto
);
3902 op
->addr
.buswidth
= spi_nor_get_protocol_addr_nbits(nor
->read_proto
);
3903 op
->dummy
.buswidth
= op
->addr
.buswidth
;
3904 op
->data
.buswidth
= spi_nor_get_protocol_data_nbits(nor
->read_proto
);
3906 /* convert the dummy cycles to the number of bytes */
3907 op
->dummy
.nbytes
= (nor
->read_dummy
* op
->dummy
.buswidth
) / 8;
3909 nor
->dirmap
.rdesc
= devm_spi_mem_dirmap_create(nor
->dev
, nor
->spimem
,
3911 return PTR_ERR_OR_ZERO(nor
->dirmap
.rdesc
);
3914 static int spi_nor_create_write_dirmap(struct spi_nor
*nor
)
3916 struct spi_mem_dirmap_info info
= {
3917 .op_tmpl
= SPI_MEM_OP(SPI_MEM_OP_CMD(nor
->program_opcode
, 1),
3918 SPI_MEM_OP_ADDR(nor
->addr_width
, 0, 1),
3919 SPI_MEM_OP_NO_DUMMY
,
3920 SPI_MEM_OP_DATA_OUT(0, NULL
, 1)),
3922 .length
= nor
->mtd
.size
,
3924 struct spi_mem_op
*op
= &info
.op_tmpl
;
3926 /* get transfer protocols. */
3927 op
->cmd
.buswidth
= spi_nor_get_protocol_inst_nbits(nor
->write_proto
);
3928 op
->addr
.buswidth
= spi_nor_get_protocol_addr_nbits(nor
->write_proto
);
3929 op
->dummy
.buswidth
= op
->addr
.buswidth
;
3930 op
->data
.buswidth
= spi_nor_get_protocol_data_nbits(nor
->write_proto
);
3932 if (nor
->program_opcode
== SPINOR_OP_AAI_WP
&& nor
->sst_write_second
)
3933 op
->addr
.nbytes
= 0;
3935 nor
->dirmap
.wdesc
= devm_spi_mem_dirmap_create(nor
->dev
, nor
->spimem
,
3937 return PTR_ERR_OR_ZERO(nor
->dirmap
.wdesc
);
3940 static int spi_nor_probe(struct spi_mem
*spimem
)
3942 struct spi_device
*spi
= spimem
->spi
;
3943 struct flash_platform_data
*data
= dev_get_platdata(&spi
->dev
);
3944 struct spi_nor
*nor
;
3946 * Enable all caps by default. The core will mask them after
3947 * checking what's really supported using spi_mem_supports_op().
3949 const struct spi_nor_hwcaps hwcaps
= { .mask
= SNOR_HWCAPS_ALL
};
3953 nor
= devm_kzalloc(&spi
->dev
, sizeof(*nor
), GFP_KERNEL
);
3957 nor
->spimem
= spimem
;
3958 nor
->dev
= &spi
->dev
;
3959 spi_nor_set_flash_node(nor
, spi
->dev
.of_node
);
3961 spi_mem_set_drvdata(spimem
, nor
);
3963 if (data
&& data
->name
)
3964 nor
->mtd
.name
= data
->name
;
3967 nor
->mtd
.name
= spi_mem_get_name(spimem
);
3970 * For some (historical?) reason many platforms provide two different
3971 * names in flash_platform_data: "name" and "type". Quite often name is
3972 * set to "m25p80" and then "type" provides a real chip name.
3973 * If that's the case, respect "type" and ignore a "name".
3975 if (data
&& data
->type
)
3976 flash_name
= data
->type
;
3977 else if (!strcmp(spi
->modalias
, "spi-nor"))
3978 flash_name
= NULL
; /* auto-detect */
3980 flash_name
= spi
->modalias
;
3982 ret
= spi_nor_scan(nor
, flash_name
, &hwcaps
);
3987 * None of the existing parts have > 512B pages, but let's play safe
3988 * and add this logic so that if anyone ever adds support for such
3989 * a NOR we don't end up with buffer overflows.
3991 if (nor
->page_size
> PAGE_SIZE
) {
3992 nor
->bouncebuf_size
= nor
->page_size
;
3993 devm_kfree(nor
->dev
, nor
->bouncebuf
);
3994 nor
->bouncebuf
= devm_kmalloc(nor
->dev
,
3995 nor
->bouncebuf_size
,
3997 if (!nor
->bouncebuf
)
4001 ret
= spi_nor_create_read_dirmap(nor
);
4005 ret
= spi_nor_create_write_dirmap(nor
);
4009 return mtd_device_register(&nor
->mtd
, data
? data
->parts
: NULL
,
4010 data
? data
->nr_parts
: 0);
4013 static int spi_nor_remove(struct spi_mem
*spimem
)
4015 struct spi_nor
*nor
= spi_mem_get_drvdata(spimem
);
4017 spi_nor_restore(nor
);
4019 /* Clean up MTD stuff. */
4020 return mtd_device_unregister(&nor
->mtd
);
4023 static void spi_nor_shutdown(struct spi_mem
*spimem
)
4025 struct spi_nor
*nor
= spi_mem_get_drvdata(spimem
);
4027 spi_nor_restore(nor
);
4031 * Do NOT add to this array without reading the following:
4033 * Historically, many flash devices are bound to this driver by their name. But
4034 * since most of these flash are compatible to some extent, and their
4035 * differences can often be differentiated by the JEDEC read-ID command, we
4036 * encourage new users to add support to the spi-nor library, and simply bind
4037 * against a generic string here (e.g., "jedec,spi-nor").
4039 * Many flash names are kept here in this list (as well as in spi-nor.c) to
4040 * keep them available as module aliases for existing platforms.
4042 static const struct spi_device_id spi_nor_dev_ids
[] = {
4044 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
4045 * hack around the fact that the SPI core does not provide uevent
4046 * matching for .of_match_table
4051 * Entries not used in DTs that should be safe to drop after replacing
4052 * them with "spi-nor" in platform data.
4054 {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
4057 * Entries that were used in DTs without "jedec,spi-nor" fallback and
4058 * should be kept for backward compatibility.
4060 {"at25df321a"}, {"at25df641"}, {"at26df081a"},
4061 {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"},
4062 {"mx25l25635e"},{"mx66l51235l"},
4063 {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"},
4064 {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"},
4066 {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
4067 {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"},
4068 {"m25p64"}, {"m25p128"},
4069 {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
4070 {"w25q80bl"}, {"w25q128"}, {"w25q256"},
4072 /* Flashes that can't be detected using JEDEC */
4073 {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
4074 {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
4075 {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
4077 /* Everspin MRAMs (non-JEDEC) */
4078 { "mr25h128" }, /* 128 Kib, 40 MHz */
4079 { "mr25h256" }, /* 256 Kib, 40 MHz */
4080 { "mr25h10" }, /* 1 Mib, 40 MHz */
4081 { "mr25h40" }, /* 4 Mib, 40 MHz */
4085 MODULE_DEVICE_TABLE(spi
, spi_nor_dev_ids
);
4087 static const struct of_device_id spi_nor_of_table
[] = {
4089 * Generic compatibility for SPI NOR that can be identified by the
4090 * JEDEC READ ID opcode (0x9F). Use this, if possible.
4092 { .compatible
= "jedec,spi-nor" },
4095 MODULE_DEVICE_TABLE(of
, spi_nor_of_table
);
4098 * REVISIT: many of these chips have deep power-down modes, which
4099 * should clearly be entered on suspend() to minimize power use.
4100 * And also when they're otherwise idle...
4102 static struct spi_mem_driver spi_nor_driver
= {
4106 .of_match_table
= spi_nor_of_table
,
4108 .id_table
= spi_nor_dev_ids
,
4110 .probe
= spi_nor_probe
,
4111 .remove
= spi_nor_remove
,
4112 .shutdown
= spi_nor_shutdown
,
4114 module_spi_mem_driver(spi_nor_driver
);
4116 MODULE_LICENSE("GPL v2");
4117 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
4118 MODULE_AUTHOR("Mike Lavender");
4119 MODULE_DESCRIPTION("framework for SPI NOR");