drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / drivers / mtd / spi-nor / core.c
blob9d6e85bf227b92d5ae847314f4d1869950b0bdaa
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
8 */
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/spi-nor.h>
18 #include <linux/mutex.h>
19 #include <linux/of_platform.h>
20 #include <linux/sched/task_stack.h>
21 #include <linux/sizes.h>
22 #include <linux/slab.h>
23 #include <linux/spi/flash.h>
25 #include "core.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
37 * for larger flash
39 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
41 #define SPI_NOR_MAX_ADDR_NBYTES 4
43 #define SPI_NOR_SRST_SLEEP_MIN 200
44 #define SPI_NOR_SRST_SLEEP_MAX 400
46 /**
47 * spi_nor_get_cmd_ext() - Get the command opcode extension based on the
48 * extension type.
49 * @nor: pointer to a 'struct spi_nor'
50 * @op: pointer to the 'struct spi_mem_op' whose properties
51 * need to be initialized.
53 * Right now, only "repeat" and "invert" are supported.
55 * Return: The opcode extension.
57 static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor,
58 const struct spi_mem_op *op)
60 switch (nor->cmd_ext_type) {
61 case SPI_NOR_EXT_INVERT:
62 return ~op->cmd.opcode;
64 case SPI_NOR_EXT_REPEAT:
65 return op->cmd.opcode;
67 default:
68 dev_err(nor->dev, "Unknown command extension type\n");
69 return 0;
73 /**
74 * spi_nor_spimem_setup_op() - Set up common properties of a spi-mem op.
75 * @nor: pointer to a 'struct spi_nor'
76 * @op: pointer to the 'struct spi_mem_op' whose properties
77 * need to be initialized.
78 * @proto: the protocol from which the properties need to be set.
80 void spi_nor_spimem_setup_op(const struct spi_nor *nor,
81 struct spi_mem_op *op,
82 const enum spi_nor_protocol proto)
84 u8 ext;
86 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
88 if (op->addr.nbytes)
89 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
91 if (op->dummy.nbytes)
92 op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
94 if (op->data.nbytes)
95 op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
97 if (spi_nor_protocol_is_dtr(proto)) {
99 * SPIMEM supports mixed DTR modes, but right now we can only
100 * have all phases either DTR or STR. IOW, SPIMEM can have
101 * something like 4S-4D-4D, but SPI NOR can't. So, set all 4
102 * phases to either DTR or STR.
104 op->cmd.dtr = true;
105 op->addr.dtr = true;
106 op->dummy.dtr = true;
107 op->data.dtr = true;
109 /* 2 bytes per clock cycle in DTR mode. */
110 op->dummy.nbytes *= 2;
112 ext = spi_nor_get_cmd_ext(nor, op);
113 op->cmd.opcode = (op->cmd.opcode << 8) | ext;
114 op->cmd.nbytes = 2;
119 * spi_nor_spimem_bounce() - check if a bounce buffer is needed for the data
120 * transfer
121 * @nor: pointer to 'struct spi_nor'
122 * @op: pointer to 'struct spi_mem_op' template for transfer
124 * If we have to use the bounce buffer, the data field in @op will be updated.
126 * Return: true if the bounce buffer is needed, false if not
128 static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op)
130 /* op->data.buf.in occupies the same memory as op->data.buf.out */
131 if (object_is_on_stack(op->data.buf.in) ||
132 !virt_addr_valid(op->data.buf.in)) {
133 if (op->data.nbytes > nor->bouncebuf_size)
134 op->data.nbytes = nor->bouncebuf_size;
135 op->data.buf.in = nor->bouncebuf;
136 return true;
139 return false;
143 * spi_nor_spimem_exec_op() - execute a memory operation
144 * @nor: pointer to 'struct spi_nor'
145 * @op: pointer to 'struct spi_mem_op' template for transfer
147 * Return: 0 on success, -error otherwise.
149 static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op)
151 int error;
153 error = spi_mem_adjust_op_size(nor->spimem, op);
154 if (error)
155 return error;
157 return spi_mem_exec_op(nor->spimem, op);
160 int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode,
161 u8 *buf, size_t len)
163 if (spi_nor_protocol_is_dtr(nor->reg_proto))
164 return -EOPNOTSUPP;
166 return nor->controller_ops->read_reg(nor, opcode, buf, len);
169 int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode,
170 const u8 *buf, size_t len)
172 if (spi_nor_protocol_is_dtr(nor->reg_proto))
173 return -EOPNOTSUPP;
175 return nor->controller_ops->write_reg(nor, opcode, buf, len);
178 static int spi_nor_controller_ops_erase(struct spi_nor *nor, loff_t offs)
180 if (spi_nor_protocol_is_dtr(nor->reg_proto))
181 return -EOPNOTSUPP;
183 return nor->controller_ops->erase(nor, offs);
187 * spi_nor_spimem_read_data() - read data from flash's memory region via
188 * spi-mem
189 * @nor: pointer to 'struct spi_nor'
190 * @from: offset to read from
191 * @len: number of bytes to read
192 * @buf: pointer to dst buffer
194 * Return: number of bytes read successfully, -errno otherwise
196 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from,
197 size_t len, u8 *buf)
199 struct spi_mem_op op =
200 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
201 SPI_MEM_OP_ADDR(nor->addr_nbytes, from, 0),
202 SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
203 SPI_MEM_OP_DATA_IN(len, buf, 0));
204 bool usebouncebuf;
205 ssize_t nbytes;
206 int error;
208 spi_nor_spimem_setup_op(nor, &op, nor->read_proto);
210 /* convert the dummy cycles to the number of bytes */
211 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
212 if (spi_nor_protocol_is_dtr(nor->read_proto))
213 op.dummy.nbytes *= 2;
215 usebouncebuf = spi_nor_spimem_bounce(nor, &op);
217 if (nor->dirmap.rdesc) {
218 nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val,
219 op.data.nbytes, op.data.buf.in);
220 } else {
221 error = spi_nor_spimem_exec_op(nor, &op);
222 if (error)
223 return error;
224 nbytes = op.data.nbytes;
227 if (usebouncebuf && nbytes > 0)
228 memcpy(buf, op.data.buf.in, nbytes);
230 return nbytes;
234 * spi_nor_read_data() - read data from flash memory
235 * @nor: pointer to 'struct spi_nor'
236 * @from: offset to read from
237 * @len: number of bytes to read
238 * @buf: pointer to dst buffer
240 * Return: number of bytes read successfully, -errno otherwise
242 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf)
244 if (nor->spimem)
245 return spi_nor_spimem_read_data(nor, from, len, buf);
247 return nor->controller_ops->read(nor, from, len, buf);
251 * spi_nor_spimem_write_data() - write data to flash memory via
252 * spi-mem
253 * @nor: pointer to 'struct spi_nor'
254 * @to: offset to write to
255 * @len: number of bytes to write
256 * @buf: pointer to src buffer
258 * Return: number of bytes written successfully, -errno otherwise
260 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to,
261 size_t len, const u8 *buf)
263 struct spi_mem_op op =
264 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
265 SPI_MEM_OP_ADDR(nor->addr_nbytes, to, 0),
266 SPI_MEM_OP_NO_DUMMY,
267 SPI_MEM_OP_DATA_OUT(len, buf, 0));
268 ssize_t nbytes;
269 int error;
271 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
272 op.addr.nbytes = 0;
274 spi_nor_spimem_setup_op(nor, &op, nor->write_proto);
276 if (spi_nor_spimem_bounce(nor, &op))
277 memcpy(nor->bouncebuf, buf, op.data.nbytes);
279 if (nor->dirmap.wdesc) {
280 nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val,
281 op.data.nbytes, op.data.buf.out);
282 } else {
283 error = spi_nor_spimem_exec_op(nor, &op);
284 if (error)
285 return error;
286 nbytes = op.data.nbytes;
289 return nbytes;
293 * spi_nor_write_data() - write data to flash memory
294 * @nor: pointer to 'struct spi_nor'
295 * @to: offset to write to
296 * @len: number of bytes to write
297 * @buf: pointer to src buffer
299 * Return: number of bytes written successfully, -errno otherwise
301 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
302 const u8 *buf)
304 if (nor->spimem)
305 return spi_nor_spimem_write_data(nor, to, len, buf);
307 return nor->controller_ops->write(nor, to, len, buf);
311 * spi_nor_read_any_reg() - read any register from flash memory, nonvolatile or
312 * volatile.
313 * @nor: pointer to 'struct spi_nor'.
314 * @op: SPI memory operation. op->data.buf must be DMA-able.
315 * @proto: SPI protocol to use for the register operation.
317 * Return: zero on success, -errno otherwise
319 int spi_nor_read_any_reg(struct spi_nor *nor, struct spi_mem_op *op,
320 enum spi_nor_protocol proto)
322 if (!nor->spimem)
323 return -EOPNOTSUPP;
325 spi_nor_spimem_setup_op(nor, op, proto);
326 return spi_nor_spimem_exec_op(nor, op);
330 * spi_nor_write_any_volatile_reg() - write any volatile register to flash
331 * memory.
332 * @nor: pointer to 'struct spi_nor'
333 * @op: SPI memory operation. op->data.buf must be DMA-able.
334 * @proto: SPI protocol to use for the register operation.
336 * Writing volatile registers are instant according to some manufacturers
337 * (Cypress, Micron) and do not need any status polling.
339 * Return: zero on success, -errno otherwise
341 int spi_nor_write_any_volatile_reg(struct spi_nor *nor, struct spi_mem_op *op,
342 enum spi_nor_protocol proto)
344 int ret;
346 if (!nor->spimem)
347 return -EOPNOTSUPP;
349 ret = spi_nor_write_enable(nor);
350 if (ret)
351 return ret;
352 spi_nor_spimem_setup_op(nor, op, proto);
353 return spi_nor_spimem_exec_op(nor, op);
357 * spi_nor_write_enable() - Set write enable latch with Write Enable command.
358 * @nor: pointer to 'struct spi_nor'.
360 * Return: 0 on success, -errno otherwise.
362 int spi_nor_write_enable(struct spi_nor *nor)
364 int ret;
366 if (nor->spimem) {
367 struct spi_mem_op op = SPI_NOR_WREN_OP;
369 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
371 ret = spi_mem_exec_op(nor->spimem, &op);
372 } else {
373 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WREN,
374 NULL, 0);
377 if (ret)
378 dev_dbg(nor->dev, "error %d on Write Enable\n", ret);
380 return ret;
384 * spi_nor_write_disable() - Send Write Disable instruction to the chip.
385 * @nor: pointer to 'struct spi_nor'.
387 * Return: 0 on success, -errno otherwise.
389 int spi_nor_write_disable(struct spi_nor *nor)
391 int ret;
393 if (nor->spimem) {
394 struct spi_mem_op op = SPI_NOR_WRDI_OP;
396 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
398 ret = spi_mem_exec_op(nor->spimem, &op);
399 } else {
400 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRDI,
401 NULL, 0);
404 if (ret)
405 dev_dbg(nor->dev, "error %d on Write Disable\n", ret);
407 return ret;
411 * spi_nor_read_id() - Read the JEDEC ID.
412 * @nor: pointer to 'struct spi_nor'.
413 * @naddr: number of address bytes to send. Can be zero if the operation
414 * does not need to send an address.
415 * @ndummy: number of dummy bytes to send after an opcode or address. Can
416 * be zero if the operation does not require dummy bytes.
417 * @id: pointer to a DMA-able buffer where the value of the JEDEC ID
418 * will be written.
419 * @proto: the SPI protocol for register operation.
421 * Return: 0 on success, -errno otherwise.
423 int spi_nor_read_id(struct spi_nor *nor, u8 naddr, u8 ndummy, u8 *id,
424 enum spi_nor_protocol proto)
426 int ret;
428 if (nor->spimem) {
429 struct spi_mem_op op =
430 SPI_NOR_READID_OP(naddr, ndummy, id, SPI_NOR_MAX_ID_LEN);
432 spi_nor_spimem_setup_op(nor, &op, proto);
433 ret = spi_mem_exec_op(nor->spimem, &op);
434 } else {
435 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id,
436 SPI_NOR_MAX_ID_LEN);
438 return ret;
442 * spi_nor_read_sr() - Read the Status Register.
443 * @nor: pointer to 'struct spi_nor'.
444 * @sr: pointer to a DMA-able buffer where the value of the
445 * Status Register will be written. Should be at least 2 bytes.
447 * Return: 0 on success, -errno otherwise.
449 int spi_nor_read_sr(struct spi_nor *nor, u8 *sr)
451 int ret;
453 if (nor->spimem) {
454 struct spi_mem_op op = SPI_NOR_RDSR_OP(sr);
456 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
457 op.addr.nbytes = nor->params->rdsr_addr_nbytes;
458 op.dummy.nbytes = nor->params->rdsr_dummy;
460 * We don't want to read only one byte in DTR mode. So,
461 * read 2 and then discard the second byte.
463 op.data.nbytes = 2;
466 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
468 ret = spi_mem_exec_op(nor->spimem, &op);
469 } else {
470 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR, sr,
474 if (ret)
475 dev_dbg(nor->dev, "error %d reading SR\n", ret);
477 return ret;
481 * spi_nor_read_cr() - Read the Configuration Register using the
482 * SPINOR_OP_RDCR (35h) command.
483 * @nor: pointer to 'struct spi_nor'
484 * @cr: pointer to a DMA-able buffer where the value of the
485 * Configuration Register will be written.
487 * Return: 0 on success, -errno otherwise.
489 int spi_nor_read_cr(struct spi_nor *nor, u8 *cr)
491 int ret;
493 if (nor->spimem) {
494 struct spi_mem_op op = SPI_NOR_RDCR_OP(cr);
496 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
498 ret = spi_mem_exec_op(nor->spimem, &op);
499 } else {
500 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDCR, cr,
504 if (ret)
505 dev_dbg(nor->dev, "error %d reading CR\n", ret);
507 return ret;
511 * spi_nor_set_4byte_addr_mode_en4b_ex4b() - Enter/Exit 4-byte address mode
512 * using SPINOR_OP_EN4B/SPINOR_OP_EX4B. Typically used by
513 * Winbond and Macronix.
514 * @nor: pointer to 'struct spi_nor'.
515 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
516 * address mode.
518 * Return: 0 on success, -errno otherwise.
520 int spi_nor_set_4byte_addr_mode_en4b_ex4b(struct spi_nor *nor, bool enable)
522 int ret;
524 if (nor->spimem) {
525 struct spi_mem_op op = SPI_NOR_EN4B_EX4B_OP(enable);
527 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
529 ret = spi_mem_exec_op(nor->spimem, &op);
530 } else {
531 ret = spi_nor_controller_ops_write_reg(nor,
532 enable ? SPINOR_OP_EN4B :
533 SPINOR_OP_EX4B,
534 NULL, 0);
537 if (ret)
538 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
540 return ret;
544 * spi_nor_set_4byte_addr_mode_wren_en4b_ex4b() - Set 4-byte address mode using
545 * SPINOR_OP_WREN followed by SPINOR_OP_EN4B or SPINOR_OP_EX4B. Typically used
546 * by ST and Micron flashes.
547 * @nor: pointer to 'struct spi_nor'.
548 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
549 * address mode.
551 * Return: 0 on success, -errno otherwise.
553 int spi_nor_set_4byte_addr_mode_wren_en4b_ex4b(struct spi_nor *nor, bool enable)
555 int ret;
557 ret = spi_nor_write_enable(nor);
558 if (ret)
559 return ret;
561 ret = spi_nor_set_4byte_addr_mode_en4b_ex4b(nor, enable);
562 if (ret)
563 return ret;
565 return spi_nor_write_disable(nor);
569 * spi_nor_set_4byte_addr_mode_brwr() - Set 4-byte address mode using
570 * SPINOR_OP_BRWR. Typically used by Spansion flashes.
571 * @nor: pointer to 'struct spi_nor'.
572 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
573 * address mode.
575 * 8-bit volatile bank register used to define A[30:A24] bits. MSB (bit[7]) is
576 * used to enable/disable 4-byte address mode. When MSB is set to ‘1’, 4-byte
577 * address mode is active and A[30:24] bits are don’t care. Write instruction is
578 * SPINOR_OP_BRWR(17h) with 1 byte of data.
580 * Return: 0 on success, -errno otherwise.
582 int spi_nor_set_4byte_addr_mode_brwr(struct spi_nor *nor, bool enable)
584 int ret;
586 nor->bouncebuf[0] = enable << 7;
588 if (nor->spimem) {
589 struct spi_mem_op op = SPI_NOR_BRWR_OP(nor->bouncebuf);
591 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
593 ret = spi_mem_exec_op(nor->spimem, &op);
594 } else {
595 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_BRWR,
596 nor->bouncebuf, 1);
599 if (ret)
600 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
602 return ret;
606 * spi_nor_sr_ready() - Query the Status Register to see if the flash is ready
607 * for new commands.
608 * @nor: pointer to 'struct spi_nor'.
610 * Return: 1 if ready, 0 if not ready, -errno on errors.
612 int spi_nor_sr_ready(struct spi_nor *nor)
614 int ret;
616 ret = spi_nor_read_sr(nor, nor->bouncebuf);
617 if (ret)
618 return ret;
620 return !(nor->bouncebuf[0] & SR_WIP);
624 * spi_nor_use_parallel_locking() - Checks if RWW locking scheme shall be used
625 * @nor: pointer to 'struct spi_nor'.
627 * Return: true if parallel locking is enabled, false otherwise.
629 static bool spi_nor_use_parallel_locking(struct spi_nor *nor)
631 return nor->flags & SNOR_F_RWW;
634 /* Locking helpers for status read operations */
635 static int spi_nor_rww_start_rdst(struct spi_nor *nor)
637 struct spi_nor_rww *rww = &nor->rww;
638 int ret = -EAGAIN;
640 mutex_lock(&nor->lock);
642 if (rww->ongoing_io || rww->ongoing_rd)
643 goto busy;
645 rww->ongoing_io = true;
646 rww->ongoing_rd = true;
647 ret = 0;
649 busy:
650 mutex_unlock(&nor->lock);
651 return ret;
654 static void spi_nor_rww_end_rdst(struct spi_nor *nor)
656 struct spi_nor_rww *rww = &nor->rww;
658 mutex_lock(&nor->lock);
660 rww->ongoing_io = false;
661 rww->ongoing_rd = false;
663 mutex_unlock(&nor->lock);
666 static int spi_nor_lock_rdst(struct spi_nor *nor)
668 if (spi_nor_use_parallel_locking(nor))
669 return spi_nor_rww_start_rdst(nor);
671 return 0;
674 static void spi_nor_unlock_rdst(struct spi_nor *nor)
676 if (spi_nor_use_parallel_locking(nor)) {
677 spi_nor_rww_end_rdst(nor);
678 wake_up(&nor->rww.wait);
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: 1 if ready, 0 if not ready, -errno on errors.
688 static int spi_nor_ready(struct spi_nor *nor)
690 int ret;
692 ret = spi_nor_lock_rdst(nor);
693 if (ret)
694 return 0;
696 /* Flashes might override the standard routine. */
697 if (nor->params->ready)
698 ret = nor->params->ready(nor);
699 else
700 ret = spi_nor_sr_ready(nor);
702 spi_nor_unlock_rdst(nor);
704 return ret;
708 * spi_nor_wait_till_ready_with_timeout() - Service routine to read the
709 * Status Register until ready, or timeout occurs.
710 * @nor: pointer to "struct spi_nor".
711 * @timeout_jiffies: jiffies to wait until timeout.
713 * Return: 0 on success, -errno otherwise.
715 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
716 unsigned long timeout_jiffies)
718 unsigned long deadline;
719 int timeout = 0, ret;
721 deadline = jiffies + timeout_jiffies;
723 while (!timeout) {
724 if (time_after_eq(jiffies, deadline))
725 timeout = 1;
727 ret = spi_nor_ready(nor);
728 if (ret < 0)
729 return ret;
730 if (ret)
731 return 0;
733 cond_resched();
736 dev_dbg(nor->dev, "flash operation timed out\n");
738 return -ETIMEDOUT;
742 * spi_nor_wait_till_ready() - Wait for a predefined amount of time for the
743 * flash to be ready, or timeout occurs.
744 * @nor: pointer to "struct spi_nor".
746 * Return: 0 on success, -errno otherwise.
748 int spi_nor_wait_till_ready(struct spi_nor *nor)
750 return spi_nor_wait_till_ready_with_timeout(nor,
751 DEFAULT_READY_WAIT_JIFFIES);
755 * spi_nor_global_block_unlock() - Unlock Global Block Protection.
756 * @nor: pointer to 'struct spi_nor'.
758 * Return: 0 on success, -errno otherwise.
760 int spi_nor_global_block_unlock(struct spi_nor *nor)
762 int ret;
764 ret = spi_nor_write_enable(nor);
765 if (ret)
766 return ret;
768 if (nor->spimem) {
769 struct spi_mem_op op = SPI_NOR_GBULK_OP;
771 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
773 ret = spi_mem_exec_op(nor->spimem, &op);
774 } else {
775 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_GBULK,
776 NULL, 0);
779 if (ret) {
780 dev_dbg(nor->dev, "error %d on Global Block Unlock\n", ret);
781 return ret;
784 return spi_nor_wait_till_ready(nor);
788 * spi_nor_write_sr() - Write the Status Register.
789 * @nor: pointer to 'struct spi_nor'.
790 * @sr: pointer to DMA-able buffer to write to the Status Register.
791 * @len: number of bytes to write to the Status Register.
793 * Return: 0 on success, -errno otherwise.
795 int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len)
797 int ret;
799 ret = spi_nor_write_enable(nor);
800 if (ret)
801 return ret;
803 if (nor->spimem) {
804 struct spi_mem_op op = SPI_NOR_WRSR_OP(sr, len);
806 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
808 ret = spi_mem_exec_op(nor->spimem, &op);
809 } else {
810 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR, sr,
811 len);
814 if (ret) {
815 dev_dbg(nor->dev, "error %d writing SR\n", ret);
816 return ret;
819 return spi_nor_wait_till_ready(nor);
823 * spi_nor_write_sr1_and_check() - Write one byte to the Status Register 1 and
824 * ensure that the byte written match the received value.
825 * @nor: pointer to a 'struct spi_nor'.
826 * @sr1: byte value to be written to the Status Register.
828 * Return: 0 on success, -errno otherwise.
830 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1)
832 int ret;
834 nor->bouncebuf[0] = sr1;
836 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1);
837 if (ret)
838 return ret;
840 ret = spi_nor_read_sr(nor, nor->bouncebuf);
841 if (ret)
842 return ret;
844 if (nor->bouncebuf[0] != sr1) {
845 dev_dbg(nor->dev, "SR1: read back test failed\n");
846 return -EIO;
849 return 0;
853 * spi_nor_write_16bit_sr_and_check() - Write the Status Register 1 and the
854 * Status Register 2 in one shot. Ensure that the byte written in the Status
855 * Register 1 match the received value, and that the 16-bit Write did not
856 * affect what was already in the Status Register 2.
857 * @nor: pointer to a 'struct spi_nor'.
858 * @sr1: byte value to be written to the Status Register 1.
860 * Return: 0 on success, -errno otherwise.
862 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1)
864 int ret;
865 u8 *sr_cr = nor->bouncebuf;
866 u8 cr_written;
868 /* Make sure we don't overwrite the contents of Status Register 2. */
869 if (!(nor->flags & SNOR_F_NO_READ_CR)) {
870 ret = spi_nor_read_cr(nor, &sr_cr[1]);
871 if (ret)
872 return ret;
873 } else if (spi_nor_get_protocol_width(nor->read_proto) == 4 &&
874 spi_nor_get_protocol_width(nor->write_proto) == 4 &&
875 nor->params->quad_enable) {
877 * If the Status Register 2 Read command (35h) is not
878 * supported, we should at least be sure we don't
879 * change the value of the SR2 Quad Enable bit.
881 * When the Quad Enable method is set and the buswidth is 4, we
882 * can safely assume that the value of the QE bit is one, as a
883 * consequence of the nor->params->quad_enable() call.
885 * According to the JESD216 revB standard, BFPT DWORDS[15],
886 * bits 22:20, the 16-bit Write Status (01h) command is
887 * available just for the cases in which the QE bit is
888 * described in SR2 at BIT(1).
890 sr_cr[1] = SR2_QUAD_EN_BIT1;
891 } else {
892 sr_cr[1] = 0;
895 sr_cr[0] = sr1;
897 ret = spi_nor_write_sr(nor, sr_cr, 2);
898 if (ret)
899 return ret;
901 ret = spi_nor_read_sr(nor, sr_cr);
902 if (ret)
903 return ret;
905 if (sr1 != sr_cr[0]) {
906 dev_dbg(nor->dev, "SR: Read back test failed\n");
907 return -EIO;
910 if (nor->flags & SNOR_F_NO_READ_CR)
911 return 0;
913 cr_written = sr_cr[1];
915 ret = spi_nor_read_cr(nor, &sr_cr[1]);
916 if (ret)
917 return ret;
919 if (cr_written != sr_cr[1]) {
920 dev_dbg(nor->dev, "CR: read back test failed\n");
921 return -EIO;
924 return 0;
928 * spi_nor_write_16bit_cr_and_check() - Write the Status Register 1 and the
929 * Configuration Register in one shot. Ensure that the byte written in the
930 * Configuration Register match the received value, and that the 16-bit Write
931 * did not affect what was already in the Status Register 1.
932 * @nor: pointer to a 'struct spi_nor'.
933 * @cr: byte value to be written to the Configuration Register.
935 * Return: 0 on success, -errno otherwise.
937 int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr)
939 int ret;
940 u8 *sr_cr = nor->bouncebuf;
941 u8 sr_written;
943 /* Keep the current value of the Status Register 1. */
944 ret = spi_nor_read_sr(nor, sr_cr);
945 if (ret)
946 return ret;
948 sr_cr[1] = cr;
950 ret = spi_nor_write_sr(nor, sr_cr, 2);
951 if (ret)
952 return ret;
954 sr_written = sr_cr[0];
956 ret = spi_nor_read_sr(nor, sr_cr);
957 if (ret)
958 return ret;
960 if (sr_written != sr_cr[0]) {
961 dev_dbg(nor->dev, "SR: Read back test failed\n");
962 return -EIO;
965 if (nor->flags & SNOR_F_NO_READ_CR)
966 return 0;
968 ret = spi_nor_read_cr(nor, &sr_cr[1]);
969 if (ret)
970 return ret;
972 if (cr != sr_cr[1]) {
973 dev_dbg(nor->dev, "CR: read back test failed\n");
974 return -EIO;
977 return 0;
981 * spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that
982 * the byte written match the received value without affecting other bits in the
983 * Status Register 1 and 2.
984 * @nor: pointer to a 'struct spi_nor'.
985 * @sr1: byte value to be written to the Status Register.
987 * Return: 0 on success, -errno otherwise.
989 int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1)
991 if (nor->flags & SNOR_F_HAS_16BIT_SR)
992 return spi_nor_write_16bit_sr_and_check(nor, sr1);
994 return spi_nor_write_sr1_and_check(nor, sr1);
998 * spi_nor_write_sr2() - Write the Status Register 2 using the
999 * SPINOR_OP_WRSR2 (3eh) command.
1000 * @nor: pointer to 'struct spi_nor'.
1001 * @sr2: pointer to DMA-able buffer to write to the Status Register 2.
1003 * Return: 0 on success, -errno otherwise.
1005 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2)
1007 int ret;
1009 ret = spi_nor_write_enable(nor);
1010 if (ret)
1011 return ret;
1013 if (nor->spimem) {
1014 struct spi_mem_op op = SPI_NOR_WRSR2_OP(sr2);
1016 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
1018 ret = spi_mem_exec_op(nor->spimem, &op);
1019 } else {
1020 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR2,
1021 sr2, 1);
1024 if (ret) {
1025 dev_dbg(nor->dev, "error %d writing SR2\n", ret);
1026 return ret;
1029 return spi_nor_wait_till_ready(nor);
1033 * spi_nor_read_sr2() - Read the Status Register 2 using the
1034 * SPINOR_OP_RDSR2 (3fh) command.
1035 * @nor: pointer to 'struct spi_nor'.
1036 * @sr2: pointer to DMA-able buffer where the value of the
1037 * Status Register 2 will be written.
1039 * Return: 0 on success, -errno otherwise.
1041 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2)
1043 int ret;
1045 if (nor->spimem) {
1046 struct spi_mem_op op = SPI_NOR_RDSR2_OP(sr2);
1048 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
1050 ret = spi_mem_exec_op(nor->spimem, &op);
1051 } else {
1052 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR2, sr2,
1056 if (ret)
1057 dev_dbg(nor->dev, "error %d reading SR2\n", ret);
1059 return ret;
1063 * spi_nor_erase_die() - Erase the entire die.
1064 * @nor: pointer to 'struct spi_nor'.
1065 * @addr: address of the die.
1066 * @die_size: size of the die.
1068 * Return: 0 on success, -errno otherwise.
1070 static int spi_nor_erase_die(struct spi_nor *nor, loff_t addr, size_t die_size)
1072 bool multi_die = nor->mtd.size != die_size;
1073 int ret;
1075 dev_dbg(nor->dev, " %lldKiB\n", (long long)(die_size >> 10));
1077 if (nor->spimem) {
1078 struct spi_mem_op op =
1079 SPI_NOR_DIE_ERASE_OP(nor->params->die_erase_opcode,
1080 nor->addr_nbytes, addr, multi_die);
1082 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
1084 ret = spi_mem_exec_op(nor->spimem, &op);
1085 } else {
1086 if (multi_die)
1087 return -EOPNOTSUPP;
1089 ret = spi_nor_controller_ops_write_reg(nor,
1090 SPINOR_OP_CHIP_ERASE,
1091 NULL, 0);
1094 if (ret)
1095 dev_dbg(nor->dev, "error %d erasing chip\n", ret);
1097 return ret;
1100 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
1102 size_t i;
1104 for (i = 0; i < size; i++)
1105 if (table[i][0] == opcode)
1106 return table[i][1];
1108 /* No conversion found, keep input op code. */
1109 return opcode;
1112 u8 spi_nor_convert_3to4_read(u8 opcode)
1114 static const u8 spi_nor_3to4_read[][2] = {
1115 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
1116 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
1117 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
1118 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
1119 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
1120 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
1121 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
1122 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
1124 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
1125 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
1126 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
1129 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
1130 ARRAY_SIZE(spi_nor_3to4_read));
1133 static u8 spi_nor_convert_3to4_program(u8 opcode)
1135 static const u8 spi_nor_3to4_program[][2] = {
1136 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
1137 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
1138 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
1139 { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B },
1140 { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B },
1143 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
1144 ARRAY_SIZE(spi_nor_3to4_program));
1147 static u8 spi_nor_convert_3to4_erase(u8 opcode)
1149 static const u8 spi_nor_3to4_erase[][2] = {
1150 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
1151 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
1152 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
1155 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
1156 ARRAY_SIZE(spi_nor_3to4_erase));
1159 static bool spi_nor_has_uniform_erase(const struct spi_nor *nor)
1161 return !!nor->params->erase_map.uniform_region.erase_mask;
1164 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor)
1166 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
1167 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
1168 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
1170 if (!spi_nor_has_uniform_erase(nor)) {
1171 struct spi_nor_erase_map *map = &nor->params->erase_map;
1172 struct spi_nor_erase_type *erase;
1173 int i;
1175 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1176 erase = &map->erase_type[i];
1177 erase->opcode =
1178 spi_nor_convert_3to4_erase(erase->opcode);
1183 static int spi_nor_prep(struct spi_nor *nor)
1185 int ret = 0;
1187 if (nor->controller_ops && nor->controller_ops->prepare)
1188 ret = nor->controller_ops->prepare(nor);
1190 return ret;
1193 static void spi_nor_unprep(struct spi_nor *nor)
1195 if (nor->controller_ops && nor->controller_ops->unprepare)
1196 nor->controller_ops->unprepare(nor);
1199 static void spi_nor_offset_to_banks(u64 bank_size, loff_t start, size_t len,
1200 u8 *first, u8 *last)
1202 /* This is currently safe, the number of banks being very small */
1203 *first = DIV_ROUND_DOWN_ULL(start, bank_size);
1204 *last = DIV_ROUND_DOWN_ULL(start + len - 1, bank_size);
1207 /* Generic helpers for internal locking and serialization */
1208 static bool spi_nor_rww_start_io(struct spi_nor *nor)
1210 struct spi_nor_rww *rww = &nor->rww;
1211 bool start = false;
1213 mutex_lock(&nor->lock);
1215 if (rww->ongoing_io)
1216 goto busy;
1218 rww->ongoing_io = true;
1219 start = true;
1221 busy:
1222 mutex_unlock(&nor->lock);
1223 return start;
1226 static void spi_nor_rww_end_io(struct spi_nor *nor)
1228 mutex_lock(&nor->lock);
1229 nor->rww.ongoing_io = false;
1230 mutex_unlock(&nor->lock);
1233 static int spi_nor_lock_device(struct spi_nor *nor)
1235 if (!spi_nor_use_parallel_locking(nor))
1236 return 0;
1238 return wait_event_killable(nor->rww.wait, spi_nor_rww_start_io(nor));
1241 static void spi_nor_unlock_device(struct spi_nor *nor)
1243 if (spi_nor_use_parallel_locking(nor)) {
1244 spi_nor_rww_end_io(nor);
1245 wake_up(&nor->rww.wait);
1249 /* Generic helpers for internal locking and serialization */
1250 static bool spi_nor_rww_start_exclusive(struct spi_nor *nor)
1252 struct spi_nor_rww *rww = &nor->rww;
1253 bool start = false;
1255 mutex_lock(&nor->lock);
1257 if (rww->ongoing_io || rww->ongoing_rd || rww->ongoing_pe)
1258 goto busy;
1260 rww->ongoing_io = true;
1261 rww->ongoing_rd = true;
1262 rww->ongoing_pe = true;
1263 start = true;
1265 busy:
1266 mutex_unlock(&nor->lock);
1267 return start;
1270 static void spi_nor_rww_end_exclusive(struct spi_nor *nor)
1272 struct spi_nor_rww *rww = &nor->rww;
1274 mutex_lock(&nor->lock);
1275 rww->ongoing_io = false;
1276 rww->ongoing_rd = false;
1277 rww->ongoing_pe = false;
1278 mutex_unlock(&nor->lock);
1281 int spi_nor_prep_and_lock(struct spi_nor *nor)
1283 int ret;
1285 ret = spi_nor_prep(nor);
1286 if (ret)
1287 return ret;
1289 if (!spi_nor_use_parallel_locking(nor))
1290 mutex_lock(&nor->lock);
1291 else
1292 ret = wait_event_killable(nor->rww.wait,
1293 spi_nor_rww_start_exclusive(nor));
1295 return ret;
1298 void spi_nor_unlock_and_unprep(struct spi_nor *nor)
1300 if (!spi_nor_use_parallel_locking(nor)) {
1301 mutex_unlock(&nor->lock);
1302 } else {
1303 spi_nor_rww_end_exclusive(nor);
1304 wake_up(&nor->rww.wait);
1307 spi_nor_unprep(nor);
1310 /* Internal locking helpers for program and erase operations */
1311 static bool spi_nor_rww_start_pe(struct spi_nor *nor, loff_t start, size_t len)
1313 struct spi_nor_rww *rww = &nor->rww;
1314 unsigned int used_banks = 0;
1315 bool started = false;
1316 u8 first, last;
1317 int bank;
1319 mutex_lock(&nor->lock);
1321 if (rww->ongoing_io || rww->ongoing_rd || rww->ongoing_pe)
1322 goto busy;
1324 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last);
1325 for (bank = first; bank <= last; bank++) {
1326 if (rww->used_banks & BIT(bank))
1327 goto busy;
1329 used_banks |= BIT(bank);
1332 rww->used_banks |= used_banks;
1333 rww->ongoing_pe = true;
1334 started = true;
1336 busy:
1337 mutex_unlock(&nor->lock);
1338 return started;
1341 static void spi_nor_rww_end_pe(struct spi_nor *nor, loff_t start, size_t len)
1343 struct spi_nor_rww *rww = &nor->rww;
1344 u8 first, last;
1345 int bank;
1347 mutex_lock(&nor->lock);
1349 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last);
1350 for (bank = first; bank <= last; bank++)
1351 rww->used_banks &= ~BIT(bank);
1353 rww->ongoing_pe = false;
1355 mutex_unlock(&nor->lock);
1358 static int spi_nor_prep_and_lock_pe(struct spi_nor *nor, loff_t start, size_t len)
1360 int ret;
1362 ret = spi_nor_prep(nor);
1363 if (ret)
1364 return ret;
1366 if (!spi_nor_use_parallel_locking(nor))
1367 mutex_lock(&nor->lock);
1368 else
1369 ret = wait_event_killable(nor->rww.wait,
1370 spi_nor_rww_start_pe(nor, start, len));
1372 return ret;
1375 static void spi_nor_unlock_and_unprep_pe(struct spi_nor *nor, loff_t start, size_t len)
1377 if (!spi_nor_use_parallel_locking(nor)) {
1378 mutex_unlock(&nor->lock);
1379 } else {
1380 spi_nor_rww_end_pe(nor, start, len);
1381 wake_up(&nor->rww.wait);
1384 spi_nor_unprep(nor);
1387 /* Internal locking helpers for read operations */
1388 static bool spi_nor_rww_start_rd(struct spi_nor *nor, loff_t start, size_t len)
1390 struct spi_nor_rww *rww = &nor->rww;
1391 unsigned int used_banks = 0;
1392 bool started = false;
1393 u8 first, last;
1394 int bank;
1396 mutex_lock(&nor->lock);
1398 if (rww->ongoing_io || rww->ongoing_rd)
1399 goto busy;
1401 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last);
1402 for (bank = first; bank <= last; bank++) {
1403 if (rww->used_banks & BIT(bank))
1404 goto busy;
1406 used_banks |= BIT(bank);
1409 rww->used_banks |= used_banks;
1410 rww->ongoing_io = true;
1411 rww->ongoing_rd = true;
1412 started = true;
1414 busy:
1415 mutex_unlock(&nor->lock);
1416 return started;
1419 static void spi_nor_rww_end_rd(struct spi_nor *nor, loff_t start, size_t len)
1421 struct spi_nor_rww *rww = &nor->rww;
1422 u8 first, last;
1423 int bank;
1425 mutex_lock(&nor->lock);
1427 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last);
1428 for (bank = first; bank <= last; bank++)
1429 nor->rww.used_banks &= ~BIT(bank);
1431 rww->ongoing_io = false;
1432 rww->ongoing_rd = false;
1434 mutex_unlock(&nor->lock);
1437 static int spi_nor_prep_and_lock_rd(struct spi_nor *nor, loff_t start, size_t len)
1439 int ret;
1441 ret = spi_nor_prep(nor);
1442 if (ret)
1443 return ret;
1445 if (!spi_nor_use_parallel_locking(nor))
1446 mutex_lock(&nor->lock);
1447 else
1448 ret = wait_event_killable(nor->rww.wait,
1449 spi_nor_rww_start_rd(nor, start, len));
1451 return ret;
1454 static void spi_nor_unlock_and_unprep_rd(struct spi_nor *nor, loff_t start, size_t len)
1456 if (!spi_nor_use_parallel_locking(nor)) {
1457 mutex_unlock(&nor->lock);
1458 } else {
1459 spi_nor_rww_end_rd(nor, start, len);
1460 wake_up(&nor->rww.wait);
1463 spi_nor_unprep(nor);
1467 * Initiate the erasure of a single sector
1469 int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
1471 int i;
1473 if (nor->spimem) {
1474 struct spi_mem_op op =
1475 SPI_NOR_SECTOR_ERASE_OP(nor->erase_opcode,
1476 nor->addr_nbytes, addr);
1478 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
1480 return spi_mem_exec_op(nor->spimem, &op);
1481 } else if (nor->controller_ops->erase) {
1482 return spi_nor_controller_ops_erase(nor, addr);
1486 * Default implementation, if driver doesn't have a specialized HW
1487 * control
1489 for (i = nor->addr_nbytes - 1; i >= 0; i--) {
1490 nor->bouncebuf[i] = addr & 0xff;
1491 addr >>= 8;
1494 return spi_nor_controller_ops_write_reg(nor, nor->erase_opcode,
1495 nor->bouncebuf, nor->addr_nbytes);
1499 * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
1500 * @erase: pointer to a structure that describes a SPI NOR erase type
1501 * @dividend: dividend value
1502 * @remainder: pointer to u32 remainder (will be updated)
1504 * Return: the result of the division
1506 static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase,
1507 u64 dividend, u32 *remainder)
1509 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
1510 *remainder = (u32)dividend & erase->size_mask;
1511 return dividend >> erase->size_shift;
1515 * spi_nor_find_best_erase_type() - find the best erase type for the given
1516 * offset in the serial flash memory and the
1517 * number of bytes to erase. The region in
1518 * which the address fits is expected to be
1519 * provided.
1520 * @map: the erase map of the SPI NOR
1521 * @region: pointer to a structure that describes a SPI NOR erase region
1522 * @addr: offset in the serial flash memory
1523 * @len: number of bytes to erase
1525 * Return: a pointer to the best fitted erase type, NULL otherwise.
1527 static const struct spi_nor_erase_type *
1528 spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
1529 const struct spi_nor_erase_region *region,
1530 u64 addr, u32 len)
1532 const struct spi_nor_erase_type *erase;
1533 u32 rem;
1534 int i;
1537 * Erase types are ordered by size, with the smallest erase type at
1538 * index 0.
1540 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
1541 /* Does the erase region support the tested erase type? */
1542 if (!(region->erase_mask & BIT(i)))
1543 continue;
1545 erase = &map->erase_type[i];
1546 if (!erase->size)
1547 continue;
1549 /* Alignment is not mandatory for overlaid regions */
1550 if (region->overlaid && region->size <= len)
1551 return erase;
1553 /* Don't erase more than what the user has asked for. */
1554 if (erase->size > len)
1555 continue;
1557 spi_nor_div_by_erase_size(erase, addr, &rem);
1558 if (!rem)
1559 return erase;
1562 return NULL;
1566 * spi_nor_init_erase_cmd() - initialize an erase command
1567 * @region: pointer to a structure that describes a SPI NOR erase region
1568 * @erase: pointer to a structure that describes a SPI NOR erase type
1570 * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
1571 * otherwise.
1573 static struct spi_nor_erase_command *
1574 spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region,
1575 const struct spi_nor_erase_type *erase)
1577 struct spi_nor_erase_command *cmd;
1579 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1580 if (!cmd)
1581 return ERR_PTR(-ENOMEM);
1583 INIT_LIST_HEAD(&cmd->list);
1584 cmd->opcode = erase->opcode;
1585 cmd->count = 1;
1587 if (region->overlaid)
1588 cmd->size = region->size;
1589 else
1590 cmd->size = erase->size;
1592 return cmd;
1596 * spi_nor_destroy_erase_cmd_list() - destroy erase command list
1597 * @erase_list: list of erase commands
1599 static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list)
1601 struct spi_nor_erase_command *cmd, *next;
1603 list_for_each_entry_safe(cmd, next, erase_list, list) {
1604 list_del(&cmd->list);
1605 kfree(cmd);
1610 * spi_nor_init_erase_cmd_list() - initialize erase command list
1611 * @nor: pointer to a 'struct spi_nor'
1612 * @erase_list: list of erase commands to be executed once we validate that the
1613 * erase can be performed
1614 * @addr: offset in the serial flash memory
1615 * @len: number of bytes to erase
1617 * Builds the list of best fitted erase commands and verifies if the erase can
1618 * be performed.
1620 * Return: 0 on success, -errno otherwise.
1622 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
1623 struct list_head *erase_list,
1624 u64 addr, u32 len)
1626 const struct spi_nor_erase_map *map = &nor->params->erase_map;
1627 const struct spi_nor_erase_type *erase, *prev_erase = NULL;
1628 struct spi_nor_erase_region *region;
1629 struct spi_nor_erase_command *cmd = NULL;
1630 u64 region_end;
1631 unsigned int i;
1632 int ret = -EINVAL;
1634 for (i = 0; i < map->n_regions && len; i++) {
1635 region = &map->regions[i];
1636 region_end = region->offset + region->size;
1638 while (len && addr >= region->offset && addr < region_end) {
1639 erase = spi_nor_find_best_erase_type(map, region, addr,
1640 len);
1641 if (!erase)
1642 goto destroy_erase_cmd_list;
1644 if (prev_erase != erase || erase->size != cmd->size ||
1645 region->overlaid) {
1646 cmd = spi_nor_init_erase_cmd(region, erase);
1647 if (IS_ERR(cmd)) {
1648 ret = PTR_ERR(cmd);
1649 goto destroy_erase_cmd_list;
1652 list_add_tail(&cmd->list, erase_list);
1653 } else {
1654 cmd->count++;
1657 len -= cmd->size;
1658 addr += cmd->size;
1659 prev_erase = erase;
1663 return 0;
1665 destroy_erase_cmd_list:
1666 spi_nor_destroy_erase_cmd_list(erase_list);
1667 return ret;
1671 * spi_nor_erase_multi_sectors() - perform a non-uniform erase
1672 * @nor: pointer to a 'struct spi_nor'
1673 * @addr: offset in the serial flash memory
1674 * @len: number of bytes to erase
1676 * Build a list of best fitted erase commands and execute it once we validate
1677 * that the erase can be performed.
1679 * Return: 0 on success, -errno otherwise.
1681 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
1683 LIST_HEAD(erase_list);
1684 struct spi_nor_erase_command *cmd, *next;
1685 int ret;
1687 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len);
1688 if (ret)
1689 return ret;
1691 list_for_each_entry_safe(cmd, next, &erase_list, list) {
1692 nor->erase_opcode = cmd->opcode;
1693 while (cmd->count) {
1694 dev_vdbg(nor->dev, "erase_cmd->size = 0x%08x, erase_cmd->opcode = 0x%02x, erase_cmd->count = %u\n",
1695 cmd->size, cmd->opcode, cmd->count);
1697 ret = spi_nor_lock_device(nor);
1698 if (ret)
1699 goto destroy_erase_cmd_list;
1701 ret = spi_nor_write_enable(nor);
1702 if (ret) {
1703 spi_nor_unlock_device(nor);
1704 goto destroy_erase_cmd_list;
1707 ret = spi_nor_erase_sector(nor, addr);
1708 spi_nor_unlock_device(nor);
1709 if (ret)
1710 goto destroy_erase_cmd_list;
1712 ret = spi_nor_wait_till_ready(nor);
1713 if (ret)
1714 goto destroy_erase_cmd_list;
1716 addr += cmd->size;
1717 cmd->count--;
1719 list_del(&cmd->list);
1720 kfree(cmd);
1723 return 0;
1725 destroy_erase_cmd_list:
1726 spi_nor_destroy_erase_cmd_list(&erase_list);
1727 return ret;
1730 static int spi_nor_erase_dice(struct spi_nor *nor, loff_t addr,
1731 size_t len, size_t die_size)
1733 unsigned long timeout;
1734 int ret;
1737 * Scale the timeout linearly with the size of the flash, with
1738 * a minimum calibrated to an old 2MB flash. We could try to
1739 * pull these from CFI/SFDP, but these values should be good
1740 * enough for now.
1742 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
1743 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
1744 (unsigned long)(nor->mtd.size / SZ_2M));
1746 do {
1747 ret = spi_nor_lock_device(nor);
1748 if (ret)
1749 return ret;
1751 ret = spi_nor_write_enable(nor);
1752 if (ret) {
1753 spi_nor_unlock_device(nor);
1754 return ret;
1757 ret = spi_nor_erase_die(nor, addr, die_size);
1759 spi_nor_unlock_device(nor);
1760 if (ret)
1761 return ret;
1763 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
1764 if (ret)
1765 return ret;
1767 addr += die_size;
1768 len -= die_size;
1770 } while (len);
1772 return 0;
1776 * Erase an address range on the nor chip. The address range may extend
1777 * one or more erase sectors. Return an error if there is a problem erasing.
1779 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
1781 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1782 u8 n_dice = nor->params->n_dice;
1783 bool multi_die_erase = false;
1784 u32 addr, len, rem;
1785 size_t die_size;
1786 int ret;
1788 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
1789 (long long)instr->len);
1791 if (spi_nor_has_uniform_erase(nor)) {
1792 div_u64_rem(instr->len, mtd->erasesize, &rem);
1793 if (rem)
1794 return -EINVAL;
1797 addr = instr->addr;
1798 len = instr->len;
1800 if (n_dice) {
1801 die_size = div_u64(mtd->size, n_dice);
1802 if (!(len & (die_size - 1)) && !(addr & (die_size - 1)))
1803 multi_die_erase = true;
1804 } else {
1805 die_size = mtd->size;
1808 ret = spi_nor_prep_and_lock_pe(nor, instr->addr, instr->len);
1809 if (ret)
1810 return ret;
1812 /* chip (die) erase? */
1813 if ((len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) ||
1814 multi_die_erase) {
1815 ret = spi_nor_erase_dice(nor, addr, len, die_size);
1816 if (ret)
1817 goto erase_err;
1819 /* REVISIT in some cases we could speed up erasing large regions
1820 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
1821 * to use "small sector erase", but that's not always optimal.
1824 /* "sector"-at-a-time erase */
1825 } else if (spi_nor_has_uniform_erase(nor)) {
1826 while (len) {
1827 ret = spi_nor_lock_device(nor);
1828 if (ret)
1829 goto erase_err;
1831 ret = spi_nor_write_enable(nor);
1832 if (ret) {
1833 spi_nor_unlock_device(nor);
1834 goto erase_err;
1837 ret = spi_nor_erase_sector(nor, addr);
1838 spi_nor_unlock_device(nor);
1839 if (ret)
1840 goto erase_err;
1842 ret = spi_nor_wait_till_ready(nor);
1843 if (ret)
1844 goto erase_err;
1846 addr += mtd->erasesize;
1847 len -= mtd->erasesize;
1850 /* erase multiple sectors */
1851 } else {
1852 ret = spi_nor_erase_multi_sectors(nor, addr, len);
1853 if (ret)
1854 goto erase_err;
1857 ret = spi_nor_write_disable(nor);
1859 erase_err:
1860 spi_nor_unlock_and_unprep_pe(nor, instr->addr, instr->len);
1862 return ret;
1866 * spi_nor_sr1_bit6_quad_enable() - Set the Quad Enable BIT(6) in the Status
1867 * Register 1.
1868 * @nor: pointer to a 'struct spi_nor'
1870 * Bit 6 of the Status Register 1 is the QE bit for Macronix like QSPI memories.
1872 * Return: 0 on success, -errno otherwise.
1874 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor)
1876 int ret;
1878 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1879 if (ret)
1880 return ret;
1882 if (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6)
1883 return 0;
1885 nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6;
1887 return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]);
1891 * spi_nor_sr2_bit1_quad_enable() - set the Quad Enable BIT(1) in the Status
1892 * Register 2.
1893 * @nor: pointer to a 'struct spi_nor'.
1895 * Bit 1 of the Status Register 2 is the QE bit for Spansion like QSPI memories.
1897 * Return: 0 on success, -errno otherwise.
1899 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor)
1901 int ret;
1903 if (nor->flags & SNOR_F_NO_READ_CR)
1904 return spi_nor_write_16bit_cr_and_check(nor, SR2_QUAD_EN_BIT1);
1906 ret = spi_nor_read_cr(nor, nor->bouncebuf);
1907 if (ret)
1908 return ret;
1910 if (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1)
1911 return 0;
1913 nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1;
1915 return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]);
1919 * spi_nor_sr2_bit7_quad_enable() - set QE bit in Status Register 2.
1920 * @nor: pointer to a 'struct spi_nor'
1922 * Set the Quad Enable (QE) bit in the Status Register 2.
1924 * This is one of the procedures to set the QE bit described in the SFDP
1925 * (JESD216 rev B) specification but no manufacturer using this procedure has
1926 * been identified yet, hence the name of the function.
1928 * Return: 0 on success, -errno otherwise.
1930 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor)
1932 u8 *sr2 = nor->bouncebuf;
1933 int ret;
1934 u8 sr2_written;
1936 /* Check current Quad Enable bit value. */
1937 ret = spi_nor_read_sr2(nor, sr2);
1938 if (ret)
1939 return ret;
1940 if (*sr2 & SR2_QUAD_EN_BIT7)
1941 return 0;
1943 /* Update the Quad Enable bit. */
1944 *sr2 |= SR2_QUAD_EN_BIT7;
1946 ret = spi_nor_write_sr2(nor, sr2);
1947 if (ret)
1948 return ret;
1950 sr2_written = *sr2;
1952 /* Read back and check it. */
1953 ret = spi_nor_read_sr2(nor, sr2);
1954 if (ret)
1955 return ret;
1957 if (*sr2 != sr2_written) {
1958 dev_dbg(nor->dev, "SR2: Read back test failed\n");
1959 return -EIO;
1962 return 0;
1965 static const struct spi_nor_manufacturer *manufacturers[] = {
1966 &spi_nor_atmel,
1967 &spi_nor_eon,
1968 &spi_nor_esmt,
1969 &spi_nor_everspin,
1970 &spi_nor_gigadevice,
1971 &spi_nor_intel,
1972 &spi_nor_issi,
1973 &spi_nor_macronix,
1974 &spi_nor_micron,
1975 &spi_nor_st,
1976 &spi_nor_spansion,
1977 &spi_nor_sst,
1978 &spi_nor_winbond,
1979 &spi_nor_xmc,
1982 static const struct flash_info spi_nor_generic_flash = {
1983 .name = "spi-nor-generic",
1986 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
1987 const u8 *id)
1989 const struct flash_info *part;
1990 unsigned int i, j;
1992 for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
1993 for (j = 0; j < manufacturers[i]->nparts; j++) {
1994 part = &manufacturers[i]->parts[j];
1995 if (part->id &&
1996 !memcmp(part->id->bytes, id, part->id->len)) {
1997 nor->manufacturer = manufacturers[i];
1998 return part;
2003 return NULL;
2006 static const struct flash_info *spi_nor_detect(struct spi_nor *nor)
2008 const struct flash_info *info;
2009 u8 *id = nor->bouncebuf;
2010 int ret;
2012 ret = spi_nor_read_id(nor, 0, 0, id, nor->reg_proto);
2013 if (ret) {
2014 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret);
2015 return ERR_PTR(ret);
2018 /* Cache the complete flash ID. */
2019 nor->id = devm_kmemdup(nor->dev, id, SPI_NOR_MAX_ID_LEN, GFP_KERNEL);
2020 if (!nor->id)
2021 return ERR_PTR(-ENOMEM);
2023 info = spi_nor_match_id(nor, id);
2025 /* Fallback to a generic flash described only by its SFDP data. */
2026 if (!info) {
2027 ret = spi_nor_check_sfdp_signature(nor);
2028 if (!ret)
2029 info = &spi_nor_generic_flash;
2032 if (!info) {
2033 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
2034 SPI_NOR_MAX_ID_LEN, id);
2035 return ERR_PTR(-ENODEV);
2037 return info;
2040 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
2041 size_t *retlen, u_char *buf)
2043 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2044 loff_t from_lock = from;
2045 size_t len_lock = len;
2046 ssize_t ret;
2048 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
2050 ret = spi_nor_prep_and_lock_rd(nor, from_lock, len_lock);
2051 if (ret)
2052 return ret;
2054 while (len) {
2055 loff_t addr = from;
2057 ret = spi_nor_read_data(nor, addr, len, buf);
2058 if (ret == 0) {
2059 /* We shouldn't see 0-length reads */
2060 ret = -EIO;
2061 goto read_err;
2063 if (ret < 0)
2064 goto read_err;
2066 WARN_ON(ret > len);
2067 *retlen += ret;
2068 buf += ret;
2069 from += ret;
2070 len -= ret;
2072 ret = 0;
2074 read_err:
2075 spi_nor_unlock_and_unprep_rd(nor, from_lock, len_lock);
2077 return ret;
2081 * Write an address range to the nor chip. Data must be written in
2082 * FLASH_PAGESIZE chunks. The address range may be any size provided
2083 * it is within the physical boundaries.
2085 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
2086 size_t *retlen, const u_char *buf)
2088 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2089 size_t i;
2090 ssize_t ret;
2091 u32 page_size = nor->params->page_size;
2093 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
2095 ret = spi_nor_prep_and_lock_pe(nor, to, len);
2096 if (ret)
2097 return ret;
2099 for (i = 0; i < len; ) {
2100 ssize_t written;
2101 loff_t addr = to + i;
2102 size_t page_offset = addr & (page_size - 1);
2103 /* the size of data remaining on the first page */
2104 size_t page_remain = min_t(size_t, page_size - page_offset, len - i);
2106 ret = spi_nor_lock_device(nor);
2107 if (ret)
2108 goto write_err;
2110 ret = spi_nor_write_enable(nor);
2111 if (ret) {
2112 spi_nor_unlock_device(nor);
2113 goto write_err;
2116 ret = spi_nor_write_data(nor, addr, page_remain, buf + i);
2117 spi_nor_unlock_device(nor);
2118 if (ret < 0)
2119 goto write_err;
2120 written = ret;
2122 ret = spi_nor_wait_till_ready(nor);
2123 if (ret)
2124 goto write_err;
2125 *retlen += written;
2126 i += written;
2129 write_err:
2130 spi_nor_unlock_and_unprep_pe(nor, to, len);
2132 return ret;
2135 static int spi_nor_check(struct spi_nor *nor)
2137 if (!nor->dev ||
2138 (!nor->spimem && !nor->controller_ops) ||
2139 (!nor->spimem && nor->controller_ops &&
2140 (!nor->controller_ops->read ||
2141 !nor->controller_ops->write ||
2142 !nor->controller_ops->read_reg ||
2143 !nor->controller_ops->write_reg))) {
2144 pr_err("spi-nor: please fill all the necessary fields!\n");
2145 return -EINVAL;
2148 if (nor->spimem && nor->controller_ops) {
2149 dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one of them.\n");
2150 return -EINVAL;
2153 return 0;
2156 void
2157 spi_nor_set_read_settings(struct spi_nor_read_command *read,
2158 u8 num_mode_clocks,
2159 u8 num_wait_states,
2160 u8 opcode,
2161 enum spi_nor_protocol proto)
2163 read->num_mode_clocks = num_mode_clocks;
2164 read->num_wait_states = num_wait_states;
2165 read->opcode = opcode;
2166 read->proto = proto;
2169 void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
2170 enum spi_nor_protocol proto)
2172 pp->opcode = opcode;
2173 pp->proto = proto;
2176 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2178 size_t i;
2180 for (i = 0; i < size; i++)
2181 if (table[i][0] == (int)hwcaps)
2182 return table[i][1];
2184 return -EINVAL;
2187 int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2189 static const int hwcaps_read2cmd[][2] = {
2190 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
2191 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
2192 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
2193 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
2194 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
2195 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
2196 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
2197 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
2198 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
2199 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
2200 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
2201 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
2202 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
2203 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
2204 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
2205 { SNOR_HWCAPS_READ_8_8_8_DTR, SNOR_CMD_READ_8_8_8_DTR },
2208 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2209 ARRAY_SIZE(hwcaps_read2cmd));
2212 int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2214 static const int hwcaps_pp2cmd[][2] = {
2215 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
2216 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
2217 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
2218 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
2219 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
2220 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
2221 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
2222 { SNOR_HWCAPS_PP_8_8_8_DTR, SNOR_CMD_PP_8_8_8_DTR },
2225 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2226 ARRAY_SIZE(hwcaps_pp2cmd));
2230 * spi_nor_spimem_check_op - check if the operation is supported
2231 * by controller
2232 *@nor: pointer to a 'struct spi_nor'
2233 *@op: pointer to op template to be checked
2235 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
2237 static int spi_nor_spimem_check_op(struct spi_nor *nor,
2238 struct spi_mem_op *op)
2241 * First test with 4 address bytes. The opcode itself might
2242 * be a 3B addressing opcode but we don't care, because
2243 * SPI controller implementation should not check the opcode,
2244 * but just the sequence.
2246 op->addr.nbytes = 4;
2247 if (!spi_mem_supports_op(nor->spimem, op)) {
2248 if (nor->params->size > SZ_16M)
2249 return -EOPNOTSUPP;
2251 /* If flash size <= 16MB, 3 address bytes are sufficient */
2252 op->addr.nbytes = 3;
2253 if (!spi_mem_supports_op(nor->spimem, op))
2254 return -EOPNOTSUPP;
2257 return 0;
2261 * spi_nor_spimem_check_readop - check if the read op is supported
2262 * by controller
2263 *@nor: pointer to a 'struct spi_nor'
2264 *@read: pointer to op template to be checked
2266 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
2268 static int spi_nor_spimem_check_readop(struct spi_nor *nor,
2269 const struct spi_nor_read_command *read)
2271 struct spi_mem_op op = SPI_NOR_READ_OP(read->opcode);
2273 spi_nor_spimem_setup_op(nor, &op, read->proto);
2275 /* convert the dummy cycles to the number of bytes */
2276 op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
2277 op.dummy.buswidth / 8;
2278 if (spi_nor_protocol_is_dtr(nor->read_proto))
2279 op.dummy.nbytes *= 2;
2281 return spi_nor_spimem_check_op(nor, &op);
2285 * spi_nor_spimem_check_pp - check if the page program op is supported
2286 * by controller
2287 *@nor: pointer to a 'struct spi_nor'
2288 *@pp: pointer to op template to be checked
2290 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
2292 static int spi_nor_spimem_check_pp(struct spi_nor *nor,
2293 const struct spi_nor_pp_command *pp)
2295 struct spi_mem_op op = SPI_NOR_PP_OP(pp->opcode);
2297 spi_nor_spimem_setup_op(nor, &op, pp->proto);
2299 return spi_nor_spimem_check_op(nor, &op);
2303 * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
2304 * based on SPI controller capabilities
2305 * @nor: pointer to a 'struct spi_nor'
2306 * @hwcaps: pointer to resulting capabilities after adjusting
2307 * according to controller and flash's capability
2309 static void
2310 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps)
2312 struct spi_nor_flash_parameter *params = nor->params;
2313 unsigned int cap;
2315 /* X-X-X modes are not supported yet, mask them all. */
2316 *hwcaps &= ~SNOR_HWCAPS_X_X_X;
2319 * If the reset line is broken, we do not want to enter a stateful
2320 * mode.
2322 if (nor->flags & SNOR_F_BROKEN_RESET)
2323 *hwcaps &= ~(SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR);
2325 for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
2326 int rdidx, ppidx;
2328 if (!(*hwcaps & BIT(cap)))
2329 continue;
2331 rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
2332 if (rdidx >= 0 &&
2333 spi_nor_spimem_check_readop(nor, &params->reads[rdidx]))
2334 *hwcaps &= ~BIT(cap);
2336 ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
2337 if (ppidx < 0)
2338 continue;
2340 if (spi_nor_spimem_check_pp(nor,
2341 &params->page_programs[ppidx]))
2342 *hwcaps &= ~BIT(cap);
2347 * spi_nor_set_erase_type() - set a SPI NOR erase type
2348 * @erase: pointer to a structure that describes a SPI NOR erase type
2349 * @size: the size of the sector/block erased by the erase type
2350 * @opcode: the SPI command op code to erase the sector/block
2352 void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
2353 u8 opcode)
2355 erase->size = size;
2356 erase->opcode = opcode;
2357 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
2358 erase->size_shift = ffs(erase->size) - 1;
2359 erase->size_mask = (1 << erase->size_shift) - 1;
2363 * spi_nor_mask_erase_type() - mask out a SPI NOR erase type
2364 * @erase: pointer to a structure that describes a SPI NOR erase type
2366 void spi_nor_mask_erase_type(struct spi_nor_erase_type *erase)
2368 erase->size = 0;
2372 * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
2373 * @map: the erase map of the SPI NOR
2374 * @erase_mask: bitmask encoding erase types that can erase the entire
2375 * flash memory
2376 * @flash_size: the spi nor flash memory size
2378 void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
2379 u8 erase_mask, u64 flash_size)
2381 map->uniform_region.offset = 0;
2382 map->uniform_region.size = flash_size;
2383 map->uniform_region.erase_mask = erase_mask;
2384 map->regions = &map->uniform_region;
2385 map->n_regions = 1;
2388 int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
2389 const struct sfdp_parameter_header *bfpt_header,
2390 const struct sfdp_bfpt *bfpt)
2392 int ret;
2394 if (nor->manufacturer && nor->manufacturer->fixups &&
2395 nor->manufacturer->fixups->post_bfpt) {
2396 ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header,
2397 bfpt);
2398 if (ret)
2399 return ret;
2402 if (nor->info->fixups && nor->info->fixups->post_bfpt)
2403 return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt);
2405 return 0;
2408 static int spi_nor_select_read(struct spi_nor *nor,
2409 u32 shared_hwcaps)
2411 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2412 const struct spi_nor_read_command *read;
2414 if (best_match < 0)
2415 return -EINVAL;
2417 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2418 if (cmd < 0)
2419 return -EINVAL;
2421 read = &nor->params->reads[cmd];
2422 nor->read_opcode = read->opcode;
2423 nor->read_proto = read->proto;
2426 * In the SPI NOR framework, we don't need to make the difference
2427 * between mode clock cycles and wait state clock cycles.
2428 * Indeed, the value of the mode clock cycles is used by a QSPI
2429 * flash memory to know whether it should enter or leave its 0-4-4
2430 * (Continuous Read / XIP) mode.
2431 * eXecution In Place is out of the scope of the mtd sub-system.
2432 * Hence we choose to merge both mode and wait state clock cycles
2433 * into the so called dummy clock cycles.
2435 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2436 return 0;
2439 static int spi_nor_select_pp(struct spi_nor *nor,
2440 u32 shared_hwcaps)
2442 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2443 const struct spi_nor_pp_command *pp;
2445 if (best_match < 0)
2446 return -EINVAL;
2448 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2449 if (cmd < 0)
2450 return -EINVAL;
2452 pp = &nor->params->page_programs[cmd];
2453 nor->program_opcode = pp->opcode;
2454 nor->write_proto = pp->proto;
2455 return 0;
2459 * spi_nor_select_uniform_erase() - select optimum uniform erase type
2460 * @map: the erase map of the SPI NOR
2462 * Once the optimum uniform sector erase command is found, disable all the
2463 * other.
2465 * Return: pointer to erase type on success, NULL otherwise.
2467 static const struct spi_nor_erase_type *
2468 spi_nor_select_uniform_erase(struct spi_nor_erase_map *map)
2470 const struct spi_nor_erase_type *tested_erase, *erase = NULL;
2471 int i;
2472 u8 uniform_erase_type = map->uniform_region.erase_mask;
2475 * Search for the biggest erase size, except for when compiled
2476 * to use 4k erases.
2478 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
2479 if (!(uniform_erase_type & BIT(i)))
2480 continue;
2482 tested_erase = &map->erase_type[i];
2484 /* Skip masked erase types. */
2485 if (!tested_erase->size)
2486 continue;
2489 * If the current erase size is the 4k one, stop here,
2490 * we have found the right uniform Sector Erase command.
2492 if (IS_ENABLED(CONFIG_MTD_SPI_NOR_USE_4K_SECTORS) &&
2493 tested_erase->size == SZ_4K) {
2494 erase = tested_erase;
2495 break;
2499 * Otherwise, the current erase size is still a valid candidate.
2500 * Select the biggest valid candidate.
2502 if (!erase && tested_erase->size)
2503 erase = tested_erase;
2504 /* keep iterating to find the wanted_size */
2507 if (!erase)
2508 return NULL;
2510 /* Disable all other Sector Erase commands. */
2511 map->uniform_region.erase_mask = BIT(erase - map->erase_type);
2512 return erase;
2515 static int spi_nor_select_erase(struct spi_nor *nor)
2517 struct spi_nor_erase_map *map = &nor->params->erase_map;
2518 const struct spi_nor_erase_type *erase = NULL;
2519 struct mtd_info *mtd = &nor->mtd;
2520 int i;
2523 * The previous implementation handling Sector Erase commands assumed
2524 * that the SPI flash memory has an uniform layout then used only one
2525 * of the supported erase sizes for all Sector Erase commands.
2526 * So to be backward compatible, the new implementation also tries to
2527 * manage the SPI flash memory as uniform with a single erase sector
2528 * size, when possible.
2530 if (spi_nor_has_uniform_erase(nor)) {
2531 erase = spi_nor_select_uniform_erase(map);
2532 if (!erase)
2533 return -EINVAL;
2534 nor->erase_opcode = erase->opcode;
2535 mtd->erasesize = erase->size;
2536 return 0;
2540 * For non-uniform SPI flash memory, set mtd->erasesize to the
2541 * maximum erase sector size. No need to set nor->erase_opcode.
2543 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
2544 if (map->erase_type[i].size) {
2545 erase = &map->erase_type[i];
2546 break;
2550 if (!erase)
2551 return -EINVAL;
2553 mtd->erasesize = erase->size;
2554 return 0;
2557 static int spi_nor_set_addr_nbytes(struct spi_nor *nor)
2559 if (nor->params->addr_nbytes) {
2560 nor->addr_nbytes = nor->params->addr_nbytes;
2561 } else if (nor->read_proto == SNOR_PROTO_8_8_8_DTR) {
2563 * In 8D-8D-8D mode, one byte takes half a cycle to transfer. So
2564 * in this protocol an odd addr_nbytes cannot be used because
2565 * then the address phase would only span a cycle and a half.
2566 * Half a cycle would be left over. We would then have to start
2567 * the dummy phase in the middle of a cycle and so too the data
2568 * phase, and we will end the transaction with half a cycle left
2569 * over.
2571 * Force all 8D-8D-8D flashes to use an addr_nbytes of 4 to
2572 * avoid this situation.
2574 nor->addr_nbytes = 4;
2575 } else if (nor->info->addr_nbytes) {
2576 nor->addr_nbytes = nor->info->addr_nbytes;
2577 } else {
2578 nor->addr_nbytes = 3;
2581 if (nor->addr_nbytes == 3 && nor->params->size > 0x1000000) {
2582 /* enable 4-byte addressing if the device exceeds 16MiB */
2583 nor->addr_nbytes = 4;
2586 if (nor->addr_nbytes > SPI_NOR_MAX_ADDR_NBYTES) {
2587 dev_dbg(nor->dev, "The number of address bytes is too large: %u\n",
2588 nor->addr_nbytes);
2589 return -EINVAL;
2592 /* Set 4byte opcodes when possible. */
2593 if (nor->addr_nbytes == 4 && nor->flags & SNOR_F_4B_OPCODES &&
2594 !(nor->flags & SNOR_F_HAS_4BAIT))
2595 spi_nor_set_4byte_opcodes(nor);
2597 return 0;
2600 static int spi_nor_setup(struct spi_nor *nor,
2601 const struct spi_nor_hwcaps *hwcaps)
2603 struct spi_nor_flash_parameter *params = nor->params;
2604 u32 ignored_mask, shared_mask;
2605 int err;
2608 * Keep only the hardware capabilities supported by both the SPI
2609 * controller and the SPI flash memory.
2611 shared_mask = hwcaps->mask & params->hwcaps.mask;
2613 if (nor->spimem) {
2615 * When called from spi_nor_probe(), all caps are set and we
2616 * need to discard some of them based on what the SPI
2617 * controller actually supports (using spi_mem_supports_op()).
2619 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask);
2620 } else {
2622 * SPI n-n-n protocols are not supported when the SPI
2623 * controller directly implements the spi_nor interface.
2624 * Yet another reason to switch to spi-mem.
2626 ignored_mask = SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR;
2627 if (shared_mask & ignored_mask) {
2628 dev_dbg(nor->dev,
2629 "SPI n-n-n protocols are not supported.\n");
2630 shared_mask &= ~ignored_mask;
2634 /* Select the (Fast) Read command. */
2635 err = spi_nor_select_read(nor, shared_mask);
2636 if (err) {
2637 dev_dbg(nor->dev,
2638 "can't select read settings supported by both the SPI controller and memory.\n");
2639 return err;
2642 /* Select the Page Program command. */
2643 err = spi_nor_select_pp(nor, shared_mask);
2644 if (err) {
2645 dev_dbg(nor->dev,
2646 "can't select write settings supported by both the SPI controller and memory.\n");
2647 return err;
2650 /* Select the Sector Erase command. */
2651 err = spi_nor_select_erase(nor);
2652 if (err) {
2653 dev_dbg(nor->dev,
2654 "can't select erase settings supported by both the SPI controller and memory.\n");
2655 return err;
2658 return spi_nor_set_addr_nbytes(nor);
2662 * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and
2663 * settings based on MFR register and ->default_init() hook.
2664 * @nor: pointer to a 'struct spi_nor'.
2666 static void spi_nor_manufacturer_init_params(struct spi_nor *nor)
2668 if (nor->manufacturer && nor->manufacturer->fixups &&
2669 nor->manufacturer->fixups->default_init)
2670 nor->manufacturer->fixups->default_init(nor);
2672 if (nor->info->fixups && nor->info->fixups->default_init)
2673 nor->info->fixups->default_init(nor);
2677 * spi_nor_no_sfdp_init_params() - Initialize the flash's parameters and
2678 * settings based on nor->info->sfdp_flags. This method should be called only by
2679 * flashes that do not define SFDP tables. If the flash supports SFDP but the
2680 * information is wrong and the settings from this function can not be retrieved
2681 * by parsing SFDP, one should instead use the fixup hooks and update the wrong
2682 * bits.
2683 * @nor: pointer to a 'struct spi_nor'.
2685 static void spi_nor_no_sfdp_init_params(struct spi_nor *nor)
2687 struct spi_nor_flash_parameter *params = nor->params;
2688 struct spi_nor_erase_map *map = &params->erase_map;
2689 const struct flash_info *info = nor->info;
2690 const u8 no_sfdp_flags = info->no_sfdp_flags;
2691 u8 i, erase_mask;
2693 if (no_sfdp_flags & SPI_NOR_DUAL_READ) {
2694 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2695 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
2696 0, 8, SPINOR_OP_READ_1_1_2,
2697 SNOR_PROTO_1_1_2);
2700 if (no_sfdp_flags & SPI_NOR_QUAD_READ) {
2701 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2702 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
2703 0, 8, SPINOR_OP_READ_1_1_4,
2704 SNOR_PROTO_1_1_4);
2707 if (no_sfdp_flags & SPI_NOR_OCTAL_READ) {
2708 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
2709 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_8],
2710 0, 8, SPINOR_OP_READ_1_1_8,
2711 SNOR_PROTO_1_1_8);
2714 if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_READ) {
2715 params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
2716 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_8_8_8_DTR],
2717 0, 20, SPINOR_OP_READ_FAST,
2718 SNOR_PROTO_8_8_8_DTR);
2721 if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_PP) {
2722 params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
2724 * Since xSPI Page Program opcode is backward compatible with
2725 * Legacy SPI, use Legacy SPI opcode there as well.
2727 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_8_8_8_DTR],
2728 SPINOR_OP_PP, SNOR_PROTO_8_8_8_DTR);
2732 * Sector Erase settings. Sort Erase Types in ascending order, with the
2733 * smallest erase size starting at BIT(0).
2735 erase_mask = 0;
2736 i = 0;
2737 if (no_sfdp_flags & SECT_4K) {
2738 erase_mask |= BIT(i);
2739 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
2740 SPINOR_OP_BE_4K);
2741 i++;
2743 erase_mask |= BIT(i);
2744 spi_nor_set_erase_type(&map->erase_type[i],
2745 info->sector_size ?: SPI_NOR_DEFAULT_SECTOR_SIZE,
2746 SPINOR_OP_SE);
2747 spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
2751 * spi_nor_init_flags() - Initialize NOR flags for settings that are not defined
2752 * in the JESD216 SFDP standard, thus can not be retrieved when parsing SFDP.
2753 * @nor: pointer to a 'struct spi_nor'
2755 static void spi_nor_init_flags(struct spi_nor *nor)
2757 struct device_node *np = spi_nor_get_flash_node(nor);
2758 const u16 flags = nor->info->flags;
2760 if (of_property_read_bool(np, "broken-flash-reset"))
2761 nor->flags |= SNOR_F_BROKEN_RESET;
2763 if (of_property_read_bool(np, "no-wp"))
2764 nor->flags |= SNOR_F_NO_WP;
2766 if (flags & SPI_NOR_SWP_IS_VOLATILE)
2767 nor->flags |= SNOR_F_SWP_IS_VOLATILE;
2769 if (flags & SPI_NOR_HAS_LOCK)
2770 nor->flags |= SNOR_F_HAS_LOCK;
2772 if (flags & SPI_NOR_HAS_TB) {
2773 nor->flags |= SNOR_F_HAS_SR_TB;
2774 if (flags & SPI_NOR_TB_SR_BIT6)
2775 nor->flags |= SNOR_F_HAS_SR_TB_BIT6;
2778 if (flags & SPI_NOR_4BIT_BP) {
2779 nor->flags |= SNOR_F_HAS_4BIT_BP;
2780 if (flags & SPI_NOR_BP3_SR_BIT6)
2781 nor->flags |= SNOR_F_HAS_SR_BP3_BIT6;
2784 if (flags & SPI_NOR_RWW && nor->params->n_banks > 1 &&
2785 !nor->controller_ops)
2786 nor->flags |= SNOR_F_RWW;
2790 * spi_nor_init_fixup_flags() - Initialize NOR flags for settings that can not
2791 * be discovered by SFDP for this particular flash because the SFDP table that
2792 * indicates this support is not defined in the flash. In case the table for
2793 * this support is defined but has wrong values, one should instead use a
2794 * post_sfdp() hook to set the SNOR_F equivalent flag.
2795 * @nor: pointer to a 'struct spi_nor'
2797 static void spi_nor_init_fixup_flags(struct spi_nor *nor)
2799 const u8 fixup_flags = nor->info->fixup_flags;
2801 if (fixup_flags & SPI_NOR_4B_OPCODES)
2802 nor->flags |= SNOR_F_4B_OPCODES;
2804 if (fixup_flags & SPI_NOR_IO_MODE_EN_VOLATILE)
2805 nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE;
2809 * spi_nor_late_init_params() - Late initialization of default flash parameters.
2810 * @nor: pointer to a 'struct spi_nor'
2812 * Used to initialize flash parameters that are not declared in the JESD216
2813 * SFDP standard, or where SFDP tables are not defined at all.
2814 * Will replace the spi_nor_manufacturer_init_params() method.
2816 static int spi_nor_late_init_params(struct spi_nor *nor)
2818 struct spi_nor_flash_parameter *params = nor->params;
2819 int ret;
2821 if (nor->manufacturer && nor->manufacturer->fixups &&
2822 nor->manufacturer->fixups->late_init) {
2823 ret = nor->manufacturer->fixups->late_init(nor);
2824 if (ret)
2825 return ret;
2828 /* Needed by some flashes late_init hooks. */
2829 spi_nor_init_flags(nor);
2831 if (nor->info->fixups && nor->info->fixups->late_init) {
2832 ret = nor->info->fixups->late_init(nor);
2833 if (ret)
2834 return ret;
2837 if (!nor->params->die_erase_opcode)
2838 nor->params->die_erase_opcode = SPINOR_OP_CHIP_ERASE;
2840 /* Default method kept for backward compatibility. */
2841 if (!params->set_4byte_addr_mode)
2842 params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_brwr;
2844 spi_nor_init_fixup_flags(nor);
2847 * NOR protection support. When locking_ops are not provided, we pick
2848 * the default ones.
2850 if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops)
2851 spi_nor_init_default_locking_ops(nor);
2853 if (params->n_banks > 1)
2854 params->bank_size = div_u64(params->size, params->n_banks);
2856 return 0;
2860 * spi_nor_sfdp_init_params_deprecated() - Deprecated way of initializing flash
2861 * parameters and settings based on JESD216 SFDP standard.
2862 * @nor: pointer to a 'struct spi_nor'.
2864 * The method has a roll-back mechanism: in case the SFDP parsing fails, the
2865 * legacy flash parameters and settings will be restored.
2867 static void spi_nor_sfdp_init_params_deprecated(struct spi_nor *nor)
2869 struct spi_nor_flash_parameter sfdp_params;
2871 memcpy(&sfdp_params, nor->params, sizeof(sfdp_params));
2873 if (spi_nor_parse_sfdp(nor)) {
2874 memcpy(nor->params, &sfdp_params, sizeof(*nor->params));
2875 nor->flags &= ~SNOR_F_4B_OPCODES;
2880 * spi_nor_init_params_deprecated() - Deprecated way of initializing flash
2881 * parameters and settings.
2882 * @nor: pointer to a 'struct spi_nor'.
2884 * The method assumes that flash doesn't support SFDP so it initializes flash
2885 * parameters in spi_nor_no_sfdp_init_params() which later on can be overwritten
2886 * when parsing SFDP, if supported.
2888 static void spi_nor_init_params_deprecated(struct spi_nor *nor)
2890 spi_nor_no_sfdp_init_params(nor);
2892 spi_nor_manufacturer_init_params(nor);
2894 if (nor->info->no_sfdp_flags & (SPI_NOR_DUAL_READ |
2895 SPI_NOR_QUAD_READ |
2896 SPI_NOR_OCTAL_READ |
2897 SPI_NOR_OCTAL_DTR_READ))
2898 spi_nor_sfdp_init_params_deprecated(nor);
2902 * spi_nor_init_default_params() - Default initialization of flash parameters
2903 * and settings. Done for all flashes, regardless is they define SFDP tables
2904 * or not.
2905 * @nor: pointer to a 'struct spi_nor'.
2907 static void spi_nor_init_default_params(struct spi_nor *nor)
2909 struct spi_nor_flash_parameter *params = nor->params;
2910 const struct flash_info *info = nor->info;
2911 struct device_node *np = spi_nor_get_flash_node(nor);
2913 params->quad_enable = spi_nor_sr2_bit1_quad_enable;
2914 params->otp.org = info->otp;
2916 /* Default to 16-bit Write Status (01h) Command */
2917 nor->flags |= SNOR_F_HAS_16BIT_SR;
2919 /* Set SPI NOR sizes. */
2920 params->writesize = 1;
2921 params->size = info->size;
2922 params->bank_size = params->size;
2923 params->page_size = info->page_size ?: SPI_NOR_DEFAULT_PAGE_SIZE;
2924 params->n_banks = info->n_banks ?: SPI_NOR_DEFAULT_N_BANKS;
2926 /* Default to Fast Read for non-DT and enable it if requested by DT. */
2927 if (!np || of_property_read_bool(np, "m25p,fast-read"))
2928 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2930 /* (Fast) Read settings. */
2931 params->hwcaps.mask |= SNOR_HWCAPS_READ;
2932 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
2933 0, 0, SPINOR_OP_READ,
2934 SNOR_PROTO_1_1_1);
2936 if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
2937 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
2938 0, 8, SPINOR_OP_READ_FAST,
2939 SNOR_PROTO_1_1_1);
2940 /* Page Program settings. */
2941 params->hwcaps.mask |= SNOR_HWCAPS_PP;
2942 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
2943 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2945 if (info->flags & SPI_NOR_QUAD_PP) {
2946 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
2947 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4],
2948 SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
2953 * spi_nor_init_params() - Initialize the flash's parameters and settings.
2954 * @nor: pointer to a 'struct spi_nor'.
2956 * The flash parameters and settings are initialized based on a sequence of
2957 * calls that are ordered by priority:
2959 * 1/ Default flash parameters initialization. The initializations are done
2960 * based on nor->info data:
2961 * spi_nor_info_init_params()
2963 * which can be overwritten by:
2964 * 2/ Manufacturer flash parameters initialization. The initializations are
2965 * done based on MFR register, or when the decisions can not be done solely
2966 * based on MFR, by using specific flash_info tweeks, ->default_init():
2967 * spi_nor_manufacturer_init_params()
2969 * which can be overwritten by:
2970 * 3/ SFDP flash parameters initialization. JESD216 SFDP is a standard and
2971 * should be more accurate that the above.
2972 * spi_nor_parse_sfdp() or spi_nor_no_sfdp_init_params()
2974 * Please note that there is a ->post_bfpt() fixup hook that can overwrite
2975 * the flash parameters and settings immediately after parsing the Basic
2976 * Flash Parameter Table.
2977 * spi_nor_post_sfdp_fixups() is called after the SFDP tables are parsed.
2978 * It is used to tweak various flash parameters when information provided
2979 * by the SFDP tables are wrong.
2981 * which can be overwritten by:
2982 * 4/ Late flash parameters initialization, used to initialize flash
2983 * parameters that are not declared in the JESD216 SFDP standard, or where SFDP
2984 * tables are not defined at all.
2985 * spi_nor_late_init_params()
2987 * Return: 0 on success, -errno otherwise.
2989 static int spi_nor_init_params(struct spi_nor *nor)
2991 int ret;
2993 nor->params = devm_kzalloc(nor->dev, sizeof(*nor->params), GFP_KERNEL);
2994 if (!nor->params)
2995 return -ENOMEM;
2997 spi_nor_init_default_params(nor);
2999 if (spi_nor_needs_sfdp(nor)) {
3000 ret = spi_nor_parse_sfdp(nor);
3001 if (ret) {
3002 dev_err(nor->dev, "BFPT parsing failed. Please consider using SPI_NOR_SKIP_SFDP when declaring the flash\n");
3003 return ret;
3005 } else if (nor->info->no_sfdp_flags & SPI_NOR_SKIP_SFDP) {
3006 spi_nor_no_sfdp_init_params(nor);
3007 } else {
3008 spi_nor_init_params_deprecated(nor);
3011 ret = spi_nor_late_init_params(nor);
3012 if (ret)
3013 return ret;
3015 if (WARN_ON(!is_power_of_2(nor->params->page_size)))
3016 return -EINVAL;
3018 return 0;
3021 /** spi_nor_set_octal_dtr() - enable or disable Octal DTR I/O.
3022 * @nor: pointer to a 'struct spi_nor'
3023 * @enable: whether to enable or disable Octal DTR
3025 * Return: 0 on success, -errno otherwise.
3027 static int spi_nor_set_octal_dtr(struct spi_nor *nor, bool enable)
3029 int ret;
3031 if (!nor->params->set_octal_dtr)
3032 return 0;
3034 if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR &&
3035 nor->write_proto == SNOR_PROTO_8_8_8_DTR))
3036 return 0;
3038 if (!(nor->flags & SNOR_F_IO_MODE_EN_VOLATILE))
3039 return 0;
3041 ret = nor->params->set_octal_dtr(nor, enable);
3042 if (ret)
3043 return ret;
3045 if (enable)
3046 nor->reg_proto = SNOR_PROTO_8_8_8_DTR;
3047 else
3048 nor->reg_proto = SNOR_PROTO_1_1_1;
3050 return 0;
3054 * spi_nor_quad_enable() - enable Quad I/O if needed.
3055 * @nor: pointer to a 'struct spi_nor'
3057 * Return: 0 on success, -errno otherwise.
3059 static int spi_nor_quad_enable(struct spi_nor *nor)
3061 if (!nor->params->quad_enable)
3062 return 0;
3064 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 ||
3065 spi_nor_get_protocol_width(nor->write_proto) == 4))
3066 return 0;
3068 return nor->params->quad_enable(nor);
3072 * spi_nor_set_4byte_addr_mode() - Set address mode.
3073 * @nor: pointer to a 'struct spi_nor'.
3074 * @enable: enable/disable 4 byte address mode.
3076 * Return: 0 on success, -errno otherwise.
3078 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable)
3080 struct spi_nor_flash_parameter *params = nor->params;
3081 int ret;
3083 if (enable) {
3085 * If the RESET# pin isn't hooked up properly, or the system
3086 * otherwise doesn't perform a reset command in the boot
3087 * sequence, it's impossible to 100% protect against unexpected
3088 * reboots (e.g., crashes). Warn the user (or hopefully, system
3089 * designer) that this is bad.
3091 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
3092 "enabling reset hack; may not recover from unexpected reboots\n");
3095 ret = params->set_4byte_addr_mode(nor, enable);
3096 if (ret && ret != -EOPNOTSUPP)
3097 return ret;
3099 if (enable) {
3100 params->addr_nbytes = 4;
3101 params->addr_mode_nbytes = 4;
3102 } else {
3103 params->addr_nbytes = 3;
3104 params->addr_mode_nbytes = 3;
3107 return 0;
3110 static int spi_nor_init(struct spi_nor *nor)
3112 int err;
3114 err = spi_nor_set_octal_dtr(nor, true);
3115 if (err) {
3116 dev_dbg(nor->dev, "octal mode not supported\n");
3117 return err;
3120 err = spi_nor_quad_enable(nor);
3121 if (err) {
3122 dev_dbg(nor->dev, "quad mode not supported\n");
3123 return err;
3127 * Some SPI NOR flashes are write protected by default after a power-on
3128 * reset cycle, in order to avoid inadvertent writes during power-up.
3129 * Backward compatibility imposes to unlock the entire flash memory
3130 * array at power-up by default. Depending on the kernel configuration
3131 * (1) do nothing, (2) always unlock the entire flash array or (3)
3132 * unlock the entire flash array only when the software write
3133 * protection bits are volatile. The latter is indicated by
3134 * SNOR_F_SWP_IS_VOLATILE.
3136 if (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE) ||
3137 (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE_ON_VOLATILE) &&
3138 nor->flags & SNOR_F_SWP_IS_VOLATILE))
3139 spi_nor_try_unlock_all(nor);
3141 if (nor->addr_nbytes == 4 &&
3142 nor->read_proto != SNOR_PROTO_8_8_8_DTR &&
3143 !(nor->flags & SNOR_F_4B_OPCODES))
3144 return spi_nor_set_4byte_addr_mode(nor, true);
3146 return 0;
3150 * spi_nor_soft_reset() - Perform a software reset
3151 * @nor: pointer to 'struct spi_nor'
3153 * Performs a "Soft Reset and Enter Default Protocol Mode" sequence which resets
3154 * the device to its power-on-reset state. This is useful when the software has
3155 * made some changes to device (volatile) registers and needs to reset it before
3156 * shutting down, for example.
3158 * Not every flash supports this sequence. The same set of opcodes might be used
3159 * for some other operation on a flash that does not support this. Support for
3160 * this sequence can be discovered via SFDP in the BFPT table.
3162 * Return: 0 on success, -errno otherwise.
3164 static void spi_nor_soft_reset(struct spi_nor *nor)
3166 struct spi_mem_op op;
3167 int ret;
3169 op = (struct spi_mem_op)SPINOR_SRSTEN_OP;
3171 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
3173 ret = spi_mem_exec_op(nor->spimem, &op);
3174 if (ret) {
3175 if (ret != -EOPNOTSUPP)
3176 dev_warn(nor->dev, "Software reset failed: %d\n", ret);
3177 return;
3180 op = (struct spi_mem_op)SPINOR_SRST_OP;
3182 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
3184 ret = spi_mem_exec_op(nor->spimem, &op);
3185 if (ret) {
3186 dev_warn(nor->dev, "Software reset failed: %d\n", ret);
3187 return;
3191 * Software Reset is not instant, and the delay varies from flash to
3192 * flash. Looking at a few flashes, most range somewhere below 100
3193 * microseconds. So, sleep for a range of 200-400 us.
3195 usleep_range(SPI_NOR_SRST_SLEEP_MIN, SPI_NOR_SRST_SLEEP_MAX);
3198 /* mtd suspend handler */
3199 static int spi_nor_suspend(struct mtd_info *mtd)
3201 struct spi_nor *nor = mtd_to_spi_nor(mtd);
3202 int ret;
3204 /* Disable octal DTR mode if we enabled it. */
3205 ret = spi_nor_set_octal_dtr(nor, false);
3206 if (ret)
3207 dev_err(nor->dev, "suspend() failed\n");
3209 return ret;
3212 /* mtd resume handler */
3213 static void spi_nor_resume(struct mtd_info *mtd)
3215 struct spi_nor *nor = mtd_to_spi_nor(mtd);
3216 struct device *dev = nor->dev;
3217 int ret;
3219 /* re-initialize the nor chip */
3220 ret = spi_nor_init(nor);
3221 if (ret)
3222 dev_err(dev, "resume() failed\n");
3225 static int spi_nor_get_device(struct mtd_info *mtd)
3227 struct mtd_info *master = mtd_get_master(mtd);
3228 struct spi_nor *nor = mtd_to_spi_nor(master);
3229 struct device *dev;
3231 if (nor->spimem)
3232 dev = nor->spimem->spi->controller->dev.parent;
3233 else
3234 dev = nor->dev;
3236 if (!try_module_get(dev->driver->owner))
3237 return -ENODEV;
3239 return 0;
3242 static void spi_nor_put_device(struct mtd_info *mtd)
3244 struct mtd_info *master = mtd_get_master(mtd);
3245 struct spi_nor *nor = mtd_to_spi_nor(master);
3246 struct device *dev;
3248 if (nor->spimem)
3249 dev = nor->spimem->spi->controller->dev.parent;
3250 else
3251 dev = nor->dev;
3253 module_put(dev->driver->owner);
3256 static void spi_nor_restore(struct spi_nor *nor)
3258 int ret;
3260 /* restore the addressing mode */
3261 if (nor->addr_nbytes == 4 && !(nor->flags & SNOR_F_4B_OPCODES) &&
3262 nor->flags & SNOR_F_BROKEN_RESET) {
3263 ret = spi_nor_set_4byte_addr_mode(nor, false);
3264 if (ret)
3266 * Do not stop the execution in the hope that the flash
3267 * will default to the 3-byte address mode after the
3268 * software reset.
3270 dev_err(nor->dev, "Failed to exit 4-byte address mode, err = %d\n", ret);
3273 if (nor->flags & SNOR_F_SOFT_RESET)
3274 spi_nor_soft_reset(nor);
3277 static const struct flash_info *spi_nor_match_name(struct spi_nor *nor,
3278 const char *name)
3280 unsigned int i, j;
3282 for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
3283 for (j = 0; j < manufacturers[i]->nparts; j++) {
3284 if (manufacturers[i]->parts[j].name &&
3285 !strcmp(name, manufacturers[i]->parts[j].name)) {
3286 nor->manufacturer = manufacturers[i];
3287 return &manufacturers[i]->parts[j];
3292 return NULL;
3295 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
3296 const char *name)
3298 const struct flash_info *info = NULL;
3300 if (name)
3301 info = spi_nor_match_name(nor, name);
3303 * Auto-detect if chip name wasn't specified or not found, or the chip
3304 * has an ID. If the chip supposedly has an ID, we also do an
3305 * auto-detection to compare it later.
3307 if (!info || info->id) {
3308 const struct flash_info *jinfo;
3310 jinfo = spi_nor_detect(nor);
3311 if (IS_ERR(jinfo))
3312 return jinfo;
3315 * If caller has specified name of flash model that can normally
3316 * be detected using JEDEC, let's verify it.
3318 if (info && jinfo != info)
3319 dev_warn(nor->dev, "found %s, expected %s\n",
3320 jinfo->name, info->name);
3322 /* If info was set before, JEDEC knows better. */
3323 info = jinfo;
3326 return info;
3329 static u32
3330 spi_nor_get_region_erasesize(const struct spi_nor_erase_region *region,
3331 const struct spi_nor_erase_type *erase_type)
3333 int i;
3335 if (region->overlaid)
3336 return region->size;
3338 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
3339 if (region->erase_mask & BIT(i))
3340 return erase_type[i].size;
3343 return 0;
3346 static int spi_nor_set_mtd_eraseregions(struct spi_nor *nor)
3348 const struct spi_nor_erase_map *map = &nor->params->erase_map;
3349 const struct spi_nor_erase_region *region = map->regions;
3350 struct mtd_erase_region_info *mtd_region;
3351 struct mtd_info *mtd = &nor->mtd;
3352 u32 erasesize, i;
3354 mtd_region = devm_kcalloc(nor->dev, map->n_regions, sizeof(*mtd_region),
3355 GFP_KERNEL);
3356 if (!mtd_region)
3357 return -ENOMEM;
3359 for (i = 0; i < map->n_regions; i++) {
3360 erasesize = spi_nor_get_region_erasesize(&region[i],
3361 map->erase_type);
3362 if (!erasesize)
3363 return -EINVAL;
3365 mtd_region[i].erasesize = erasesize;
3366 mtd_region[i].numblocks = div_u64(region[i].size, erasesize);
3367 mtd_region[i].offset = region[i].offset;
3370 mtd->numeraseregions = map->n_regions;
3371 mtd->eraseregions = mtd_region;
3373 return 0;
3376 static int spi_nor_set_mtd_info(struct spi_nor *nor)
3378 struct mtd_info *mtd = &nor->mtd;
3379 struct device *dev = nor->dev;
3381 spi_nor_set_mtd_locking_ops(nor);
3382 spi_nor_set_mtd_otp_ops(nor);
3384 mtd->dev.parent = dev;
3385 if (!mtd->name)
3386 mtd->name = dev_name(dev);
3387 mtd->type = MTD_NORFLASH;
3388 mtd->flags = MTD_CAP_NORFLASH;
3389 /* Unset BIT_WRITEABLE to enable JFFS2 write buffer for ECC'd NOR */
3390 if (nor->flags & SNOR_F_ECC)
3391 mtd->flags &= ~MTD_BIT_WRITEABLE;
3392 if (nor->info->flags & SPI_NOR_NO_ERASE)
3393 mtd->flags |= MTD_NO_ERASE;
3394 else
3395 mtd->_erase = spi_nor_erase;
3396 mtd->writesize = nor->params->writesize;
3397 mtd->writebufsize = nor->params->page_size;
3398 mtd->size = nor->params->size;
3399 mtd->_read = spi_nor_read;
3400 /* Might be already set by some SST flashes. */
3401 if (!mtd->_write)
3402 mtd->_write = spi_nor_write;
3403 mtd->_suspend = spi_nor_suspend;
3404 mtd->_resume = spi_nor_resume;
3405 mtd->_get_device = spi_nor_get_device;
3406 mtd->_put_device = spi_nor_put_device;
3408 if (!spi_nor_has_uniform_erase(nor))
3409 return spi_nor_set_mtd_eraseregions(nor);
3411 return 0;
3414 static int spi_nor_hw_reset(struct spi_nor *nor)
3416 struct gpio_desc *reset;
3418 reset = devm_gpiod_get_optional(nor->dev, "reset", GPIOD_OUT_LOW);
3419 if (IS_ERR_OR_NULL(reset))
3420 return PTR_ERR_OR_ZERO(reset);
3423 * Experimental delay values by looking at different flash device
3424 * vendors datasheets.
3426 usleep_range(1, 5);
3427 gpiod_set_value_cansleep(reset, 1);
3428 usleep_range(100, 150);
3429 gpiod_set_value_cansleep(reset, 0);
3430 usleep_range(1000, 1200);
3432 return 0;
3435 int spi_nor_scan(struct spi_nor *nor, const char *name,
3436 const struct spi_nor_hwcaps *hwcaps)
3438 const struct flash_info *info;
3439 struct device *dev = nor->dev;
3440 int ret;
3442 ret = spi_nor_check(nor);
3443 if (ret)
3444 return ret;
3446 /* Reset SPI protocol for all commands. */
3447 nor->reg_proto = SNOR_PROTO_1_1_1;
3448 nor->read_proto = SNOR_PROTO_1_1_1;
3449 nor->write_proto = SNOR_PROTO_1_1_1;
3452 * We need the bounce buffer early to read/write registers when going
3453 * through the spi-mem layer (buffers have to be DMA-able).
3454 * For spi-mem drivers, we'll reallocate a new buffer if
3455 * nor->params->page_size turns out to be greater than PAGE_SIZE (which
3456 * shouldn't happen before long since NOR pages are usually less
3457 * than 1KB) after spi_nor_scan() returns.
3459 nor->bouncebuf_size = PAGE_SIZE;
3460 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
3461 GFP_KERNEL);
3462 if (!nor->bouncebuf)
3463 return -ENOMEM;
3465 ret = spi_nor_hw_reset(nor);
3466 if (ret)
3467 return ret;
3469 info = spi_nor_get_flash_info(nor, name);
3470 if (IS_ERR(info))
3471 return PTR_ERR(info);
3473 nor->info = info;
3475 mutex_init(&nor->lock);
3477 /* Init flash parameters based on flash_info struct and SFDP */
3478 ret = spi_nor_init_params(nor);
3479 if (ret)
3480 return ret;
3482 if (spi_nor_use_parallel_locking(nor))
3483 init_waitqueue_head(&nor->rww.wait);
3486 * Configure the SPI memory:
3487 * - select op codes for (Fast) Read, Page Program and Sector Erase.
3488 * - set the number of dummy cycles (mode cycles + wait states).
3489 * - set the SPI protocols for register and memory accesses.
3490 * - set the number of address bytes.
3492 ret = spi_nor_setup(nor, hwcaps);
3493 if (ret)
3494 return ret;
3496 /* Send all the required SPI flash commands to initialize device */
3497 ret = spi_nor_init(nor);
3498 if (ret)
3499 return ret;
3501 /* No mtd_info fields should be used up to this point. */
3502 ret = spi_nor_set_mtd_info(nor);
3503 if (ret)
3504 return ret;
3506 dev_dbg(dev, "Manufacturer and device ID: %*phN\n",
3507 SPI_NOR_MAX_ID_LEN, nor->id);
3509 return 0;
3511 EXPORT_SYMBOL_GPL(spi_nor_scan);
3513 static int spi_nor_create_read_dirmap(struct spi_nor *nor)
3515 struct spi_mem_dirmap_info info = {
3516 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
3517 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0),
3518 SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
3519 SPI_MEM_OP_DATA_IN(0, NULL, 0)),
3520 .offset = 0,
3521 .length = nor->params->size,
3523 struct spi_mem_op *op = &info.op_tmpl;
3525 spi_nor_spimem_setup_op(nor, op, nor->read_proto);
3527 /* convert the dummy cycles to the number of bytes */
3528 op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8;
3529 if (spi_nor_protocol_is_dtr(nor->read_proto))
3530 op->dummy.nbytes *= 2;
3533 * Since spi_nor_spimem_setup_op() only sets buswidth when the number
3534 * of data bytes is non-zero, the data buswidth won't be set here. So,
3535 * do it explicitly.
3537 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
3539 nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3540 &info);
3541 return PTR_ERR_OR_ZERO(nor->dirmap.rdesc);
3544 static int spi_nor_create_write_dirmap(struct spi_nor *nor)
3546 struct spi_mem_dirmap_info info = {
3547 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
3548 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0),
3549 SPI_MEM_OP_NO_DUMMY,
3550 SPI_MEM_OP_DATA_OUT(0, NULL, 0)),
3551 .offset = 0,
3552 .length = nor->params->size,
3554 struct spi_mem_op *op = &info.op_tmpl;
3556 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
3557 op->addr.nbytes = 0;
3559 spi_nor_spimem_setup_op(nor, op, nor->write_proto);
3562 * Since spi_nor_spimem_setup_op() only sets buswidth when the number
3563 * of data bytes is non-zero, the data buswidth won't be set here. So,
3564 * do it explicitly.
3566 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
3568 nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
3569 &info);
3570 return PTR_ERR_OR_ZERO(nor->dirmap.wdesc);
3573 static int spi_nor_probe(struct spi_mem *spimem)
3575 struct spi_device *spi = spimem->spi;
3576 struct flash_platform_data *data = dev_get_platdata(&spi->dev);
3577 struct spi_nor *nor;
3579 * Enable all caps by default. The core will mask them after
3580 * checking what's really supported using spi_mem_supports_op().
3582 const struct spi_nor_hwcaps hwcaps = { .mask = SNOR_HWCAPS_ALL };
3583 char *flash_name;
3584 int ret;
3586 nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL);
3587 if (!nor)
3588 return -ENOMEM;
3590 nor->spimem = spimem;
3591 nor->dev = &spi->dev;
3592 spi_nor_set_flash_node(nor, spi->dev.of_node);
3594 spi_mem_set_drvdata(spimem, nor);
3596 if (data && data->name)
3597 nor->mtd.name = data->name;
3599 if (!nor->mtd.name)
3600 nor->mtd.name = spi_mem_get_name(spimem);
3603 * For some (historical?) reason many platforms provide two different
3604 * names in flash_platform_data: "name" and "type". Quite often name is
3605 * set to "m25p80" and then "type" provides a real chip name.
3606 * If that's the case, respect "type" and ignore a "name".
3608 if (data && data->type)
3609 flash_name = data->type;
3610 else if (!strcmp(spi->modalias, "spi-nor"))
3611 flash_name = NULL; /* auto-detect */
3612 else
3613 flash_name = spi->modalias;
3615 ret = spi_nor_scan(nor, flash_name, &hwcaps);
3616 if (ret)
3617 return ret;
3619 spi_nor_debugfs_register(nor);
3622 * None of the existing parts have > 512B pages, but let's play safe
3623 * and add this logic so that if anyone ever adds support for such
3624 * a NOR we don't end up with buffer overflows.
3626 if (nor->params->page_size > PAGE_SIZE) {
3627 nor->bouncebuf_size = nor->params->page_size;
3628 devm_kfree(nor->dev, nor->bouncebuf);
3629 nor->bouncebuf = devm_kmalloc(nor->dev,
3630 nor->bouncebuf_size,
3631 GFP_KERNEL);
3632 if (!nor->bouncebuf)
3633 return -ENOMEM;
3636 ret = spi_nor_create_read_dirmap(nor);
3637 if (ret)
3638 return ret;
3640 ret = spi_nor_create_write_dirmap(nor);
3641 if (ret)
3642 return ret;
3644 return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
3645 data ? data->nr_parts : 0);
3648 static int spi_nor_remove(struct spi_mem *spimem)
3650 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3652 spi_nor_restore(nor);
3654 /* Clean up MTD stuff. */
3655 return mtd_device_unregister(&nor->mtd);
3658 static void spi_nor_shutdown(struct spi_mem *spimem)
3660 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
3662 spi_nor_restore(nor);
3666 * Do NOT add to this array without reading the following:
3668 * Historically, many flash devices are bound to this driver by their name. But
3669 * since most of these flash are compatible to some extent, and their
3670 * differences can often be differentiated by the JEDEC read-ID command, we
3671 * encourage new users to add support to the spi-nor library, and simply bind
3672 * against a generic string here (e.g., "jedec,spi-nor").
3674 * Many flash names are kept here in this list to keep them available
3675 * as module aliases for existing platforms.
3677 static const struct spi_device_id spi_nor_dev_ids[] = {
3679 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
3680 * hack around the fact that the SPI core does not provide uevent
3681 * matching for .of_match_table
3683 {"spi-nor"},
3686 * Entries not used in DTs that should be safe to drop after replacing
3687 * them with "spi-nor" in platform data.
3689 {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
3692 * Entries that were used in DTs without "jedec,spi-nor" fallback and
3693 * should be kept for backward compatibility.
3695 {"at25df321a"}, {"at25df641"}, {"at26df081a"},
3696 {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"},
3697 {"mx25l25635e"},{"mx66l51235l"},
3698 {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"},
3699 {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"},
3700 {"s25fl064k"},
3701 {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
3702 {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"},
3703 {"m25p64"}, {"m25p128"},
3704 {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
3705 {"w25q80bl"}, {"w25q128"}, {"w25q256"},
3707 /* Flashes that can't be detected using JEDEC */
3708 {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
3709 {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
3710 {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
3712 /* Everspin MRAMs (non-JEDEC) */
3713 { "mr25h128" }, /* 128 Kib, 40 MHz */
3714 { "mr25h256" }, /* 256 Kib, 40 MHz */
3715 { "mr25h10" }, /* 1 Mib, 40 MHz */
3716 { "mr25h40" }, /* 4 Mib, 40 MHz */
3718 { },
3720 MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids);
3722 static const struct of_device_id spi_nor_of_table[] = {
3724 * Generic compatibility for SPI NOR that can be identified by the
3725 * JEDEC READ ID opcode (0x9F). Use this, if possible.
3727 { .compatible = "jedec,spi-nor" },
3728 { /* sentinel */ },
3730 MODULE_DEVICE_TABLE(of, spi_nor_of_table);
3733 * REVISIT: many of these chips have deep power-down modes, which
3734 * should clearly be entered on suspend() to minimize power use.
3735 * And also when they're otherwise idle...
3737 static struct spi_mem_driver spi_nor_driver = {
3738 .spidrv = {
3739 .driver = {
3740 .name = "spi-nor",
3741 .of_match_table = spi_nor_of_table,
3742 .dev_groups = spi_nor_sysfs_groups,
3744 .id_table = spi_nor_dev_ids,
3746 .probe = spi_nor_probe,
3747 .remove = spi_nor_remove,
3748 .shutdown = spi_nor_shutdown,
3751 static int __init spi_nor_module_init(void)
3753 return spi_mem_driver_register(&spi_nor_driver);
3755 module_init(spi_nor_module_init);
3757 static void __exit spi_nor_module_exit(void)
3759 spi_mem_driver_unregister(&spi_nor_driver);
3760 spi_nor_debugfs_shutdown();
3762 module_exit(spi_nor_module_exit);
3764 MODULE_LICENSE("GPL v2");
3765 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
3766 MODULE_AUTHOR("Mike Lavender");
3767 MODULE_DESCRIPTION("framework for SPI NOR");