Lines Matching +full:spi +full:- +full:nor

1 // SPDX-License-Identifier: GPL-2.0
18 #include <linux/mtd/spi-nor.h>
25 #include <linux/spi/flash.h>
32 * For everything but full-chip erase; probably could be much smaller, but kept
38 * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
49 * spi_nor_get_cmd_ext() - Get the command opcode extension based on the
51 * @nor: pointer to a 'struct spi_nor'
59 static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor, in spi_nor_get_cmd_ext() argument
62 switch (nor->cmd_ext_type) { in spi_nor_get_cmd_ext()
64 return ~op->cmd.opcode; in spi_nor_get_cmd_ext()
67 return op->cmd.opcode; in spi_nor_get_cmd_ext()
70 dev_err(nor->dev, "Unknown command extension type\n"); in spi_nor_get_cmd_ext()
76 * spi_nor_spimem_setup_op() - Set up common properties of a spi-mem op.
77 * @nor: pointer to a 'struct spi_nor'
82 void spi_nor_spimem_setup_op(const struct spi_nor *nor, in spi_nor_spimem_setup_op() argument
88 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto); in spi_nor_spimem_setup_op()
90 if (op->addr.nbytes) in spi_nor_spimem_setup_op()
91 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto); in spi_nor_spimem_setup_op()
93 if (op->dummy.nbytes) in spi_nor_spimem_setup_op()
94 op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto); in spi_nor_spimem_setup_op()
96 if (op->data.nbytes) in spi_nor_spimem_setup_op()
97 op->data.buswidth = spi_nor_get_protocol_data_nbits(proto); in spi_nor_spimem_setup_op()
103 * something like 4S-4D-4D, but SPI NOR can't. So, set all 4 in spi_nor_spimem_setup_op()
106 op->cmd.dtr = true; in spi_nor_spimem_setup_op()
107 op->addr.dtr = true; in spi_nor_spimem_setup_op()
108 op->dummy.dtr = true; in spi_nor_spimem_setup_op()
109 op->data.dtr = true; in spi_nor_spimem_setup_op()
112 op->dummy.nbytes *= 2; in spi_nor_spimem_setup_op()
114 ext = spi_nor_get_cmd_ext(nor, op); in spi_nor_spimem_setup_op()
115 op->cmd.opcode = (op->cmd.opcode << 8) | ext; in spi_nor_spimem_setup_op()
116 op->cmd.nbytes = 2; in spi_nor_spimem_setup_op()
119 if (proto == SNOR_PROTO_8_8_8_DTR && nor->flags & SNOR_F_SWAP16) in spi_nor_spimem_setup_op()
120 op->data.swap16 = true; in spi_nor_spimem_setup_op()
124 * spi_nor_spimem_bounce() - check if a bounce buffer is needed for the data
126 * @nor: pointer to 'struct spi_nor'
133 static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op) in spi_nor_spimem_bounce() argument
135 /* op->data.buf.in occupies the same memory as op->data.buf.out */ in spi_nor_spimem_bounce()
136 if (object_is_on_stack(op->data.buf.in) || in spi_nor_spimem_bounce()
137 !virt_addr_valid(op->data.buf.in)) { in spi_nor_spimem_bounce()
138 if (op->data.nbytes > nor->bouncebuf_size) in spi_nor_spimem_bounce()
139 op->data.nbytes = nor->bouncebuf_size; in spi_nor_spimem_bounce()
140 op->data.buf.in = nor->bouncebuf; in spi_nor_spimem_bounce()
148 * spi_nor_spimem_exec_op() - execute a memory operation
149 * @nor: pointer to 'struct spi_nor'
152 * Return: 0 on success, -error otherwise.
154 static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op) in spi_nor_spimem_exec_op() argument
158 error = spi_mem_adjust_op_size(nor->spimem, op); in spi_nor_spimem_exec_op()
162 return spi_mem_exec_op(nor->spimem, op); in spi_nor_spimem_exec_op()
165 int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode, in spi_nor_controller_ops_read_reg() argument
168 if (spi_nor_protocol_is_dtr(nor->reg_proto)) in spi_nor_controller_ops_read_reg()
169 return -EOPNOTSUPP; in spi_nor_controller_ops_read_reg()
171 return nor->controller_ops->read_reg(nor, opcode, buf, len); in spi_nor_controller_ops_read_reg()
174 int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode, in spi_nor_controller_ops_write_reg() argument
177 if (spi_nor_protocol_is_dtr(nor->reg_proto)) in spi_nor_controller_ops_write_reg()
178 return -EOPNOTSUPP; in spi_nor_controller_ops_write_reg()
180 return nor->controller_ops->write_reg(nor, opcode, buf, len); in spi_nor_controller_ops_write_reg()
183 static int spi_nor_controller_ops_erase(struct spi_nor *nor, loff_t offs) in spi_nor_controller_ops_erase() argument
185 if (spi_nor_protocol_is_dtr(nor->reg_proto)) in spi_nor_controller_ops_erase()
186 return -EOPNOTSUPP; in spi_nor_controller_ops_erase()
188 return nor->controller_ops->erase(nor, offs); in spi_nor_controller_ops_erase()
192 * spi_nor_spimem_read_data() - read data from flash's memory region via
193 * spi-mem
194 * @nor: pointer to 'struct spi_nor'
199 * Return: number of bytes read successfully, -errno otherwise
201 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from, in spi_nor_spimem_read_data() argument
205 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0), in spi_nor_spimem_read_data()
206 SPI_MEM_OP_ADDR(nor->addr_nbytes, from, 0), in spi_nor_spimem_read_data()
207 SPI_MEM_OP_DUMMY(nor->read_dummy, 0), in spi_nor_spimem_read_data()
213 spi_nor_spimem_setup_op(nor, &op, nor->read_proto); in spi_nor_spimem_read_data()
216 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; in spi_nor_spimem_read_data()
217 if (spi_nor_protocol_is_dtr(nor->read_proto)) in spi_nor_spimem_read_data()
220 usebouncebuf = spi_nor_spimem_bounce(nor, &op); in spi_nor_spimem_read_data()
222 if (nor->dirmap.rdesc) { in spi_nor_spimem_read_data()
223 nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val, in spi_nor_spimem_read_data()
226 error = spi_nor_spimem_exec_op(nor, &op); in spi_nor_spimem_read_data()
239 * spi_nor_read_data() - read data from flash memory
240 * @nor: pointer to 'struct spi_nor'
245 * Return: number of bytes read successfully, -errno otherwise
247 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf) in spi_nor_read_data() argument
249 if (nor->spimem) in spi_nor_read_data()
250 return spi_nor_spimem_read_data(nor, from, len, buf); in spi_nor_read_data()
252 return nor->controller_ops->read(nor, from, len, buf); in spi_nor_read_data()
256 * spi_nor_spimem_write_data() - write data to flash memory via
257 * spi-mem
258 * @nor: pointer to 'struct spi_nor'
263 * Return: number of bytes written successfully, -errno otherwise
265 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to, in spi_nor_spimem_write_data() argument
269 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0), in spi_nor_spimem_write_data()
270 SPI_MEM_OP_ADDR(nor->addr_nbytes, to, 0), in spi_nor_spimem_write_data()
276 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) in spi_nor_spimem_write_data()
279 spi_nor_spimem_setup_op(nor, &op, nor->write_proto); in spi_nor_spimem_write_data()
281 if (spi_nor_spimem_bounce(nor, &op)) in spi_nor_spimem_write_data()
282 memcpy(nor->bouncebuf, buf, op.data.nbytes); in spi_nor_spimem_write_data()
284 if (nor->dirmap.wdesc) { in spi_nor_spimem_write_data()
285 nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val, in spi_nor_spimem_write_data()
288 error = spi_nor_spimem_exec_op(nor, &op); in spi_nor_spimem_write_data()
298 * spi_nor_write_data() - write data to flash memory
299 * @nor: pointer to 'struct spi_nor'
304 * Return: number of bytes written successfully, -errno otherwise
306 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len, in spi_nor_write_data() argument
309 if (nor->spimem) in spi_nor_write_data()
310 return spi_nor_spimem_write_data(nor, to, len, buf); in spi_nor_write_data()
312 return nor->controller_ops->write(nor, to, len, buf); in spi_nor_write_data()
316 * spi_nor_read_any_reg() - read any register from flash memory, nonvolatile or
318 * @nor: pointer to 'struct spi_nor'.
319 * @op: SPI memory operation. op->data.buf must be DMA-able.
320 * @proto: SPI protocol to use for the register operation.
322 * Return: zero on success, -errno otherwise
324 int spi_nor_read_any_reg(struct spi_nor *nor, struct spi_mem_op *op, in spi_nor_read_any_reg() argument
327 if (!nor->spimem) in spi_nor_read_any_reg()
328 return -EOPNOTSUPP; in spi_nor_read_any_reg()
330 spi_nor_spimem_setup_op(nor, op, proto); in spi_nor_read_any_reg()
331 return spi_nor_spimem_exec_op(nor, op); in spi_nor_read_any_reg()
335 * spi_nor_write_any_volatile_reg() - write any volatile register to flash
337 * @nor: pointer to 'struct spi_nor'
338 * @op: SPI memory operation. op->data.buf must be DMA-able.
339 * @proto: SPI protocol to use for the register operation.
344 * Return: zero on success, -errno otherwise
346 int spi_nor_write_any_volatile_reg(struct spi_nor *nor, struct spi_mem_op *op, in spi_nor_write_any_volatile_reg() argument
351 if (!nor->spimem) in spi_nor_write_any_volatile_reg()
352 return -EOPNOTSUPP; in spi_nor_write_any_volatile_reg()
354 ret = spi_nor_write_enable(nor); in spi_nor_write_any_volatile_reg()
357 spi_nor_spimem_setup_op(nor, op, proto); in spi_nor_write_any_volatile_reg()
358 return spi_nor_spimem_exec_op(nor, op); in spi_nor_write_any_volatile_reg()
362 * spi_nor_write_enable() - Set write enable latch with Write Enable command.
363 * @nor: pointer to 'struct spi_nor'.
365 * Return: 0 on success, -errno otherwise.
367 int spi_nor_write_enable(struct spi_nor *nor) in spi_nor_write_enable() argument
371 if (nor->spimem) { in spi_nor_write_enable()
374 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_enable()
376 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_enable()
378 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WREN, in spi_nor_write_enable()
383 dev_dbg(nor->dev, "error %d on Write Enable\n", ret); in spi_nor_write_enable()
389 * spi_nor_write_disable() - Send Write Disable instruction to the chip.
390 * @nor: pointer to 'struct spi_nor'.
392 * Return: 0 on success, -errno otherwise.
394 int spi_nor_write_disable(struct spi_nor *nor) in spi_nor_write_disable() argument
398 if (nor->spimem) { in spi_nor_write_disable()
401 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_disable()
403 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_disable()
405 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRDI, in spi_nor_write_disable()
410 dev_dbg(nor->dev, "error %d on Write Disable\n", ret); in spi_nor_write_disable()
416 * spi_nor_read_id() - Read the JEDEC ID.
417 * @nor: pointer to 'struct spi_nor'.
422 * @id: pointer to a DMA-able buffer where the value of the JEDEC ID
424 * @proto: the SPI protocol for register operation.
426 * Return: 0 on success, -errno otherwise.
428 int spi_nor_read_id(struct spi_nor *nor, u8 naddr, u8 ndummy, u8 *id, in spi_nor_read_id() argument
433 if (nor->spimem) { in spi_nor_read_id()
437 spi_nor_spimem_setup_op(nor, &op, proto); in spi_nor_read_id()
438 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_id()
440 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id, in spi_nor_read_id()
447 * spi_nor_read_sr() - Read the Status Register.
448 * @nor: pointer to 'struct spi_nor'.
449 * @sr: pointer to a DMA-able buffer where the value of the
452 * Return: 0 on success, -errno otherwise.
454 int spi_nor_read_sr(struct spi_nor *nor, u8 *sr) in spi_nor_read_sr() argument
458 if (nor->spimem) { in spi_nor_read_sr()
461 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) { in spi_nor_read_sr()
462 op.addr.nbytes = nor->params->rdsr_addr_nbytes; in spi_nor_read_sr()
463 op.dummy.nbytes = nor->params->rdsr_dummy; in spi_nor_read_sr()
471 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_sr()
473 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_sr()
475 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR, sr, in spi_nor_read_sr()
480 dev_dbg(nor->dev, "error %d reading SR\n", ret); in spi_nor_read_sr()
486 * spi_nor_read_cr() - Read the Configuration Register using the
488 * @nor: pointer to 'struct spi_nor'
489 * @cr: pointer to a DMA-able buffer where the value of the
492 * Return: 0 on success, -errno otherwise.
494 int spi_nor_read_cr(struct spi_nor *nor, u8 *cr) in spi_nor_read_cr() argument
498 if (nor->spimem) { in spi_nor_read_cr()
501 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_cr()
503 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_cr()
505 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDCR, cr, in spi_nor_read_cr()
510 dev_dbg(nor->dev, "error %d reading CR\n", ret); in spi_nor_read_cr()
516 * spi_nor_set_4byte_addr_mode_en4b_ex4b() - Enter/Exit 4-byte address mode
519 * @nor: pointer to 'struct spi_nor'.
520 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
523 * Return: 0 on success, -errno otherwise.
525 int spi_nor_set_4byte_addr_mode_en4b_ex4b(struct spi_nor *nor, bool enable) in spi_nor_set_4byte_addr_mode_en4b_ex4b() argument
529 if (nor->spimem) { in spi_nor_set_4byte_addr_mode_en4b_ex4b()
532 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_set_4byte_addr_mode_en4b_ex4b()
534 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_set_4byte_addr_mode_en4b_ex4b()
536 ret = spi_nor_controller_ops_write_reg(nor, in spi_nor_set_4byte_addr_mode_en4b_ex4b()
543 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); in spi_nor_set_4byte_addr_mode_en4b_ex4b()
549 * spi_nor_set_4byte_addr_mode_wren_en4b_ex4b() - Set 4-byte address mode using
552 * @nor: pointer to 'struct spi_nor'.
553 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
556 * Return: 0 on success, -errno otherwise.
558 int spi_nor_set_4byte_addr_mode_wren_en4b_ex4b(struct spi_nor *nor, bool enable) in spi_nor_set_4byte_addr_mode_wren_en4b_ex4b() argument
562 ret = spi_nor_write_enable(nor); in spi_nor_set_4byte_addr_mode_wren_en4b_ex4b()
566 ret = spi_nor_set_4byte_addr_mode_en4b_ex4b(nor, enable); in spi_nor_set_4byte_addr_mode_wren_en4b_ex4b()
570 return spi_nor_write_disable(nor); in spi_nor_set_4byte_addr_mode_wren_en4b_ex4b()
574 * spi_nor_set_4byte_addr_mode_brwr() - Set 4-byte address mode using
576 * @nor: pointer to 'struct spi_nor'.
577 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
580 * 8-bit volatile bank register used to define A[30:A24] bits. MSB (bit[7]) is
581 * used to enable/disable 4-byte address mode. When MSB is set to ‘1’, 4-byte
585 * Return: 0 on success, -errno otherwise.
587 int spi_nor_set_4byte_addr_mode_brwr(struct spi_nor *nor, bool enable) in spi_nor_set_4byte_addr_mode_brwr() argument
591 nor->bouncebuf[0] = enable << 7; in spi_nor_set_4byte_addr_mode_brwr()
593 if (nor->spimem) { in spi_nor_set_4byte_addr_mode_brwr()
594 struct spi_mem_op op = SPI_NOR_BRWR_OP(nor->bouncebuf); in spi_nor_set_4byte_addr_mode_brwr()
596 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_set_4byte_addr_mode_brwr()
598 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_set_4byte_addr_mode_brwr()
600 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_BRWR, in spi_nor_set_4byte_addr_mode_brwr()
601 nor->bouncebuf, 1); in spi_nor_set_4byte_addr_mode_brwr()
605 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); in spi_nor_set_4byte_addr_mode_brwr()
611 * spi_nor_sr_ready() - Query the Status Register to see if the flash is ready
613 * @nor: pointer to 'struct spi_nor'.
615 * Return: 1 if ready, 0 if not ready, -errno on errors.
617 int spi_nor_sr_ready(struct spi_nor *nor) in spi_nor_sr_ready() argument
621 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_ready()
625 return !(nor->bouncebuf[0] & SR_WIP); in spi_nor_sr_ready()
629 * spi_nor_use_parallel_locking() - Checks if RWW locking scheme shall be used
630 * @nor: pointer to 'struct spi_nor'.
634 static bool spi_nor_use_parallel_locking(struct spi_nor *nor) in spi_nor_use_parallel_locking() argument
636 return nor->flags & SNOR_F_RWW; in spi_nor_use_parallel_locking()
640 static int spi_nor_rww_start_rdst(struct spi_nor *nor) in spi_nor_rww_start_rdst() argument
642 struct spi_nor_rww *rww = &nor->rww; in spi_nor_rww_start_rdst()
644 guard(mutex)(&nor->lock); in spi_nor_rww_start_rdst()
646 if (rww->ongoing_io || rww->ongoing_rd) in spi_nor_rww_start_rdst()
647 return -EAGAIN; in spi_nor_rww_start_rdst()
649 rww->ongoing_io = true; in spi_nor_rww_start_rdst()
650 rww->ongoing_rd = true; in spi_nor_rww_start_rdst()
655 static void spi_nor_rww_end_rdst(struct spi_nor *nor) in spi_nor_rww_end_rdst() argument
657 struct spi_nor_rww *rww = &nor->rww; in spi_nor_rww_end_rdst()
659 guard(mutex)(&nor->lock); in spi_nor_rww_end_rdst()
661 rww->ongoing_io = false; in spi_nor_rww_end_rdst()
662 rww->ongoing_rd = false; in spi_nor_rww_end_rdst()
665 static int spi_nor_lock_rdst(struct spi_nor *nor) in spi_nor_lock_rdst() argument
667 if (spi_nor_use_parallel_locking(nor)) in spi_nor_lock_rdst()
668 return spi_nor_rww_start_rdst(nor); in spi_nor_lock_rdst()
673 static void spi_nor_unlock_rdst(struct spi_nor *nor) in spi_nor_unlock_rdst() argument
675 if (spi_nor_use_parallel_locking(nor)) { in spi_nor_unlock_rdst()
676 spi_nor_rww_end_rdst(nor); in spi_nor_unlock_rdst()
677 wake_up(&nor->rww.wait); in spi_nor_unlock_rdst()
682 * spi_nor_ready() - Query the flash to see if it is ready for new commands.
683 * @nor: pointer to 'struct spi_nor'.
685 * Return: 1 if ready, 0 if not ready, -errno on errors.
687 static int spi_nor_ready(struct spi_nor *nor) in spi_nor_ready() argument
691 ret = spi_nor_lock_rdst(nor); in spi_nor_ready()
696 if (nor->params->ready) in spi_nor_ready()
697 ret = nor->params->ready(nor); in spi_nor_ready()
699 ret = spi_nor_sr_ready(nor); in spi_nor_ready()
701 spi_nor_unlock_rdst(nor); in spi_nor_ready()
707 * spi_nor_wait_till_ready_with_timeout() - Service routine to read the
709 * @nor: pointer to "struct spi_nor".
712 * Return: 0 on success, -errno otherwise.
714 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor, in spi_nor_wait_till_ready_with_timeout() argument
726 ret = spi_nor_ready(nor); in spi_nor_wait_till_ready_with_timeout()
735 dev_dbg(nor->dev, "flash operation timed out\n"); in spi_nor_wait_till_ready_with_timeout()
737 return -ETIMEDOUT; in spi_nor_wait_till_ready_with_timeout()
741 * spi_nor_wait_till_ready() - Wait for a predefined amount of time for the
743 * @nor: pointer to "struct spi_nor".
745 * Return: 0 on success, -errno otherwise.
747 int spi_nor_wait_till_ready(struct spi_nor *nor) in spi_nor_wait_till_ready() argument
749 return spi_nor_wait_till_ready_with_timeout(nor, in spi_nor_wait_till_ready()
754 * spi_nor_global_block_unlock() - Unlock Global Block Protection.
755 * @nor: pointer to 'struct spi_nor'.
757 * Return: 0 on success, -errno otherwise.
759 int spi_nor_global_block_unlock(struct spi_nor *nor) in spi_nor_global_block_unlock() argument
763 ret = spi_nor_write_enable(nor); in spi_nor_global_block_unlock()
767 if (nor->spimem) { in spi_nor_global_block_unlock()
770 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_global_block_unlock()
772 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_global_block_unlock()
774 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_GBULK, in spi_nor_global_block_unlock()
779 dev_dbg(nor->dev, "error %d on Global Block Unlock\n", ret); in spi_nor_global_block_unlock()
783 return spi_nor_wait_till_ready(nor); in spi_nor_global_block_unlock()
787 * spi_nor_write_sr() - Write the Status Register.
788 * @nor: pointer to 'struct spi_nor'.
789 * @sr: pointer to DMA-able buffer to write to the Status Register.
792 * Return: 0 on success, -errno otherwise.
794 int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len) in spi_nor_write_sr() argument
798 ret = spi_nor_write_enable(nor); in spi_nor_write_sr()
802 if (nor->spimem) { in spi_nor_write_sr()
805 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_sr()
807 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_sr()
809 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR, sr, in spi_nor_write_sr()
814 dev_dbg(nor->dev, "error %d writing SR\n", ret); in spi_nor_write_sr()
818 return spi_nor_wait_till_ready(nor); in spi_nor_write_sr()
822 * spi_nor_write_sr1_and_check() - Write one byte to the Status Register 1 and
824 * @nor: pointer to a 'struct spi_nor'.
827 * Return: 0 on success, -errno otherwise.
829 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_sr1_and_check() argument
833 nor->bouncebuf[0] = sr1; in spi_nor_write_sr1_and_check()
835 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1); in spi_nor_write_sr1_and_check()
839 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_write_sr1_and_check()
843 if (nor->bouncebuf[0] != sr1) { in spi_nor_write_sr1_and_check()
844 dev_dbg(nor->dev, "SR1: read back test failed\n"); in spi_nor_write_sr1_and_check()
845 return -EIO; in spi_nor_write_sr1_and_check()
852 * spi_nor_write_16bit_sr_and_check() - Write the Status Register 1 and the
854 * Register 1 match the received value, and that the 16-bit Write did not
856 * @nor: pointer to a 'struct spi_nor'.
859 * Return: 0 on success, -errno otherwise.
861 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_16bit_sr_and_check() argument
864 u8 *sr_cr = nor->bouncebuf; in spi_nor_write_16bit_sr_and_check()
868 if (!(nor->flags & SNOR_F_NO_READ_CR)) { in spi_nor_write_16bit_sr_and_check()
869 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_sr_and_check()
872 } else if (spi_nor_get_protocol_width(nor->read_proto) == 4 && in spi_nor_write_16bit_sr_and_check()
873 spi_nor_get_protocol_width(nor->write_proto) == 4 && in spi_nor_write_16bit_sr_and_check()
874 nor->params->quad_enable) { in spi_nor_write_16bit_sr_and_check()
882 * consequence of the nor->params->quad_enable() call. in spi_nor_write_16bit_sr_and_check()
885 * bits 22:20, the 16-bit Write Status (01h) command is in spi_nor_write_16bit_sr_and_check()
896 ret = spi_nor_write_sr(nor, sr_cr, 2); in spi_nor_write_16bit_sr_and_check()
900 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_sr_and_check()
905 dev_dbg(nor->dev, "SR: Read back test failed\n"); in spi_nor_write_16bit_sr_and_check()
906 return -EIO; in spi_nor_write_16bit_sr_and_check()
909 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_write_16bit_sr_and_check()
914 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_sr_and_check()
919 dev_dbg(nor->dev, "CR: read back test failed\n"); in spi_nor_write_16bit_sr_and_check()
920 return -EIO; in spi_nor_write_16bit_sr_and_check()
927 * spi_nor_write_16bit_cr_and_check() - Write the Status Register 1 and the
929 * Configuration Register match the received value, and that the 16-bit Write
931 * @nor: pointer to a 'struct spi_nor'.
934 * Return: 0 on success, -errno otherwise.
936 int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr) in spi_nor_write_16bit_cr_and_check() argument
939 u8 *sr_cr = nor->bouncebuf; in spi_nor_write_16bit_cr_and_check()
943 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_cr_and_check()
949 ret = spi_nor_write_sr(nor, sr_cr, 2); in spi_nor_write_16bit_cr_and_check()
955 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_cr_and_check()
960 dev_dbg(nor->dev, "SR: Read back test failed\n"); in spi_nor_write_16bit_cr_and_check()
961 return -EIO; in spi_nor_write_16bit_cr_and_check()
964 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_write_16bit_cr_and_check()
967 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_cr_and_check()
972 dev_dbg(nor->dev, "CR: read back test failed\n"); in spi_nor_write_16bit_cr_and_check()
973 return -EIO; in spi_nor_write_16bit_cr_and_check()
980 * spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that
983 * @nor: pointer to a 'struct spi_nor'.
986 * Return: 0 on success, -errno otherwise.
988 int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_sr_and_check() argument
990 if (nor->flags & SNOR_F_HAS_16BIT_SR) in spi_nor_write_sr_and_check()
991 return spi_nor_write_16bit_sr_and_check(nor, sr1); in spi_nor_write_sr_and_check()
993 return spi_nor_write_sr1_and_check(nor, sr1); in spi_nor_write_sr_and_check()
997 * spi_nor_write_sr2() - Write the Status Register 2 using the
999 * @nor: pointer to 'struct spi_nor'.
1000 * @sr2: pointer to DMA-able buffer to write to the Status Register 2.
1002 * Return: 0 on success, -errno otherwise.
1004 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2) in spi_nor_write_sr2() argument
1008 ret = spi_nor_write_enable(nor); in spi_nor_write_sr2()
1012 if (nor->spimem) { in spi_nor_write_sr2()
1015 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_sr2()
1017 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_sr2()
1019 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR2, in spi_nor_write_sr2()
1024 dev_dbg(nor->dev, "error %d writing SR2\n", ret); in spi_nor_write_sr2()
1028 return spi_nor_wait_till_ready(nor); in spi_nor_write_sr2()
1032 * spi_nor_read_sr2() - Read the Status Register 2 using the
1034 * @nor: pointer to 'struct spi_nor'.
1035 * @sr2: pointer to DMA-able buffer where the value of the
1038 * Return: 0 on success, -errno otherwise.
1040 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2) in spi_nor_read_sr2() argument
1044 if (nor->spimem) { in spi_nor_read_sr2()
1047 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_sr2()
1049 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_sr2()
1051 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR2, sr2, in spi_nor_read_sr2()
1056 dev_dbg(nor->dev, "error %d reading SR2\n", ret); in spi_nor_read_sr2()
1062 * spi_nor_erase_die() - Erase the entire die.
1063 * @nor: pointer to 'struct spi_nor'.
1067 * Return: 0 on success, -errno otherwise.
1069 static int spi_nor_erase_die(struct spi_nor *nor, loff_t addr, size_t die_size) in spi_nor_erase_die() argument
1071 bool multi_die = nor->mtd.size != die_size; in spi_nor_erase_die()
1074 dev_dbg(nor->dev, " %lldKiB\n", (long long)(die_size >> 10)); in spi_nor_erase_die()
1076 if (nor->spimem) { in spi_nor_erase_die()
1078 SPI_NOR_DIE_ERASE_OP(nor->params->die_erase_opcode, in spi_nor_erase_die()
1079 nor->addr_nbytes, addr, multi_die); in spi_nor_erase_die()
1081 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_erase_die()
1083 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_erase_die()
1086 return -EOPNOTSUPP; in spi_nor_erase_die()
1088 ret = spi_nor_controller_ops_write_reg(nor, in spi_nor_erase_die()
1094 dev_dbg(nor->dev, "error %d erasing chip\n", ret); in spi_nor_erase_die()
1158 static bool spi_nor_has_uniform_erase(const struct spi_nor *nor) in spi_nor_has_uniform_erase() argument
1160 return !!nor->params->erase_map.uniform_region.erase_mask; in spi_nor_has_uniform_erase()
1163 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor) in spi_nor_set_4byte_opcodes() argument
1165 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode); in spi_nor_set_4byte_opcodes()
1166 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode); in spi_nor_set_4byte_opcodes()
1167 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode); in spi_nor_set_4byte_opcodes()
1169 if (!spi_nor_has_uniform_erase(nor)) { in spi_nor_set_4byte_opcodes()
1170 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_set_4byte_opcodes()
1175 erase = &map->erase_type[i]; in spi_nor_set_4byte_opcodes()
1176 erase->opcode = in spi_nor_set_4byte_opcodes()
1177 spi_nor_convert_3to4_erase(erase->opcode); in spi_nor_set_4byte_opcodes()
1182 static int spi_nor_prep(struct spi_nor *nor) in spi_nor_prep() argument
1186 if (nor->controller_ops && nor->controller_ops->prepare) in spi_nor_prep()
1187 ret = nor->controller_ops->prepare(nor); in spi_nor_prep()
1192 static void spi_nor_unprep(struct spi_nor *nor) in spi_nor_unprep() argument
1194 if (nor->controller_ops && nor->controller_ops->unprepare) in spi_nor_unprep()
1195 nor->controller_ops->unprepare(nor); in spi_nor_unprep()
1203 *last = DIV_ROUND_DOWN_ULL(start + len - 1, bank_size); in spi_nor_offset_to_banks()
1207 static bool spi_nor_rww_start_io(struct spi_nor *nor) in spi_nor_rww_start_io() argument
1209 struct spi_nor_rww *rww = &nor->rww; in spi_nor_rww_start_io()
1211 guard(mutex)(&nor->lock); in spi_nor_rww_start_io()
1213 if (rww->ongoing_io) in spi_nor_rww_start_io()
1216 rww->ongoing_io = true; in spi_nor_rww_start_io()
1221 static void spi_nor_rww_end_io(struct spi_nor *nor) in spi_nor_rww_end_io() argument
1223 guard(mutex)(&nor->lock); in spi_nor_rww_end_io()
1224 nor->rww.ongoing_io = false; in spi_nor_rww_end_io()
1227 static int spi_nor_lock_device(struct spi_nor *nor) in spi_nor_lock_device() argument
1229 if (!spi_nor_use_parallel_locking(nor)) in spi_nor_lock_device()
1232 return wait_event_killable(nor->rww.wait, spi_nor_rww_start_io(nor)); in spi_nor_lock_device()
1235 static void spi_nor_unlock_device(struct spi_nor *nor) in spi_nor_unlock_device() argument
1237 if (spi_nor_use_parallel_locking(nor)) { in spi_nor_unlock_device()
1238 spi_nor_rww_end_io(nor); in spi_nor_unlock_device()
1239 wake_up(&nor->rww.wait); in spi_nor_unlock_device()
1244 static bool spi_nor_rww_start_exclusive(struct spi_nor *nor) in spi_nor_rww_start_exclusive() argument
1246 struct spi_nor_rww *rww = &nor->rww; in spi_nor_rww_start_exclusive()
1248 mutex_lock(&nor->lock); in spi_nor_rww_start_exclusive()
1250 if (rww->ongoing_io || rww->ongoing_rd || rww->ongoing_pe) in spi_nor_rww_start_exclusive()
1253 rww->ongoing_io = true; in spi_nor_rww_start_exclusive()
1254 rww->ongoing_rd = true; in spi_nor_rww_start_exclusive()
1255 rww->ongoing_pe = true; in spi_nor_rww_start_exclusive()
1260 static void spi_nor_rww_end_exclusive(struct spi_nor *nor) in spi_nor_rww_end_exclusive() argument
1262 struct spi_nor_rww *rww = &nor->rww; in spi_nor_rww_end_exclusive()
1264 guard(mutex)(&nor->lock); in spi_nor_rww_end_exclusive()
1265 rww->ongoing_io = false; in spi_nor_rww_end_exclusive()
1266 rww->ongoing_rd = false; in spi_nor_rww_end_exclusive()
1267 rww->ongoing_pe = false; in spi_nor_rww_end_exclusive()
1270 int spi_nor_prep_and_lock(struct spi_nor *nor) in spi_nor_prep_and_lock() argument
1274 ret = spi_nor_prep(nor); in spi_nor_prep_and_lock()
1278 if (!spi_nor_use_parallel_locking(nor)) in spi_nor_prep_and_lock()
1279 mutex_lock(&nor->lock); in spi_nor_prep_and_lock()
1281 ret = wait_event_killable(nor->rww.wait, in spi_nor_prep_and_lock()
1282 spi_nor_rww_start_exclusive(nor)); in spi_nor_prep_and_lock()
1287 void spi_nor_unlock_and_unprep(struct spi_nor *nor) in spi_nor_unlock_and_unprep() argument
1289 if (!spi_nor_use_parallel_locking(nor)) { in spi_nor_unlock_and_unprep()
1290 mutex_unlock(&nor->lock); in spi_nor_unlock_and_unprep()
1292 spi_nor_rww_end_exclusive(nor); in spi_nor_unlock_and_unprep()
1293 wake_up(&nor->rww.wait); in spi_nor_unlock_and_unprep()
1296 spi_nor_unprep(nor); in spi_nor_unlock_and_unprep()
1300 static bool spi_nor_rww_start_pe(struct spi_nor *nor, loff_t start, size_t len) in spi_nor_rww_start_pe() argument
1302 struct spi_nor_rww *rww = &nor->rww; in spi_nor_rww_start_pe()
1307 guard(mutex)(&nor->lock); in spi_nor_rww_start_pe()
1309 if (rww->ongoing_io || rww->ongoing_rd || rww->ongoing_pe) in spi_nor_rww_start_pe()
1312 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last); in spi_nor_rww_start_pe()
1314 if (rww->used_banks & BIT(bank)) in spi_nor_rww_start_pe()
1320 rww->used_banks |= used_banks; in spi_nor_rww_start_pe()
1321 rww->ongoing_pe = true; in spi_nor_rww_start_pe()
1326 static void spi_nor_rww_end_pe(struct spi_nor *nor, loff_t start, size_t len) in spi_nor_rww_end_pe() argument
1328 struct spi_nor_rww *rww = &nor->rww; in spi_nor_rww_end_pe()
1332 guard(mutex)(&nor->lock); in spi_nor_rww_end_pe()
1334 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last); in spi_nor_rww_end_pe()
1336 rww->used_banks &= ~BIT(bank); in spi_nor_rww_end_pe()
1338 rww->ongoing_pe = false; in spi_nor_rww_end_pe()
1341 static int spi_nor_prep_and_lock_pe(struct spi_nor *nor, loff_t start, size_t len) in spi_nor_prep_and_lock_pe() argument
1345 ret = spi_nor_prep(nor); in spi_nor_prep_and_lock_pe()
1349 if (!spi_nor_use_parallel_locking(nor)) in spi_nor_prep_and_lock_pe()
1350 mutex_lock(&nor->lock); in spi_nor_prep_and_lock_pe()
1352 ret = wait_event_killable(nor->rww.wait, in spi_nor_prep_and_lock_pe()
1353 spi_nor_rww_start_pe(nor, start, len)); in spi_nor_prep_and_lock_pe()
1358 static void spi_nor_unlock_and_unprep_pe(struct spi_nor *nor, loff_t start, size_t len) in spi_nor_unlock_and_unprep_pe() argument
1360 if (!spi_nor_use_parallel_locking(nor)) { in spi_nor_unlock_and_unprep_pe()
1361 mutex_unlock(&nor->lock); in spi_nor_unlock_and_unprep_pe()
1363 spi_nor_rww_end_pe(nor, start, len); in spi_nor_unlock_and_unprep_pe()
1364 wake_up(&nor->rww.wait); in spi_nor_unlock_and_unprep_pe()
1367 spi_nor_unprep(nor); in spi_nor_unlock_and_unprep_pe()
1371 static bool spi_nor_rww_start_rd(struct spi_nor *nor, loff_t start, size_t len) in spi_nor_rww_start_rd() argument
1373 struct spi_nor_rww *rww = &nor->rww; in spi_nor_rww_start_rd()
1378 guard(mutex)(&nor->lock); in spi_nor_rww_start_rd()
1380 if (rww->ongoing_io || rww->ongoing_rd) in spi_nor_rww_start_rd()
1383 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last); in spi_nor_rww_start_rd()
1385 if (rww->used_banks & BIT(bank)) in spi_nor_rww_start_rd()
1391 rww->used_banks |= used_banks; in spi_nor_rww_start_rd()
1392 rww->ongoing_io = true; in spi_nor_rww_start_rd()
1393 rww->ongoing_rd = true; in spi_nor_rww_start_rd()
1398 static void spi_nor_rww_end_rd(struct spi_nor *nor, loff_t start, size_t len) in spi_nor_rww_end_rd() argument
1400 struct spi_nor_rww *rww = &nor->rww; in spi_nor_rww_end_rd()
1404 guard(mutex)(&nor->lock); in spi_nor_rww_end_rd()
1406 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last); in spi_nor_rww_end_rd()
1408 nor->rww.used_banks &= ~BIT(bank); in spi_nor_rww_end_rd()
1410 rww->ongoing_io = false; in spi_nor_rww_end_rd()
1411 rww->ongoing_rd = false; in spi_nor_rww_end_rd()
1414 static int spi_nor_prep_and_lock_rd(struct spi_nor *nor, loff_t start, size_t len) in spi_nor_prep_and_lock_rd() argument
1418 ret = spi_nor_prep(nor); in spi_nor_prep_and_lock_rd()
1422 if (!spi_nor_use_parallel_locking(nor)) in spi_nor_prep_and_lock_rd()
1423 mutex_lock(&nor->lock); in spi_nor_prep_and_lock_rd()
1425 ret = wait_event_killable(nor->rww.wait, in spi_nor_prep_and_lock_rd()
1426 spi_nor_rww_start_rd(nor, start, len)); in spi_nor_prep_and_lock_rd()
1431 static void spi_nor_unlock_and_unprep_rd(struct spi_nor *nor, loff_t start, size_t len) in spi_nor_unlock_and_unprep_rd() argument
1433 if (!spi_nor_use_parallel_locking(nor)) { in spi_nor_unlock_and_unprep_rd()
1434 mutex_unlock(&nor->lock); in spi_nor_unlock_and_unprep_rd()
1436 spi_nor_rww_end_rd(nor, start, len); in spi_nor_unlock_and_unprep_rd()
1437 wake_up(&nor->rww.wait); in spi_nor_unlock_and_unprep_rd()
1440 spi_nor_unprep(nor); in spi_nor_unlock_and_unprep_rd()
1446 int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) in spi_nor_erase_sector() argument
1450 if (nor->spimem) { in spi_nor_erase_sector()
1452 SPI_NOR_SECTOR_ERASE_OP(nor->erase_opcode, in spi_nor_erase_sector()
1453 nor->addr_nbytes, addr); in spi_nor_erase_sector()
1455 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_erase_sector()
1457 return spi_mem_exec_op(nor->spimem, &op); in spi_nor_erase_sector()
1458 } else if (nor->controller_ops->erase) { in spi_nor_erase_sector()
1459 return spi_nor_controller_ops_erase(nor, addr); in spi_nor_erase_sector()
1466 for (i = nor->addr_nbytes - 1; i >= 0; i--) { in spi_nor_erase_sector()
1467 nor->bouncebuf[i] = addr & 0xff; in spi_nor_erase_sector()
1471 return spi_nor_controller_ops_write_reg(nor, nor->erase_opcode, in spi_nor_erase_sector()
1472 nor->bouncebuf, nor->addr_nbytes); in spi_nor_erase_sector()
1476 * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
1477 * @erase: pointer to a structure that describes a SPI NOR erase type
1487 *remainder = (u32)dividend & erase->size_mask; in spi_nor_div_by_erase_size()
1488 return dividend >> erase->size_shift; in spi_nor_div_by_erase_size()
1492 * spi_nor_find_best_erase_type() - find the best erase type for the given
1497 * @map: the erase map of the SPI NOR
1498 * @region: pointer to a structure that describes a SPI NOR erase region
1517 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) { in spi_nor_find_best_erase_type()
1519 if (!(region->erase_mask & BIT(i))) in spi_nor_find_best_erase_type()
1522 erase = &map->erase_type[i]; in spi_nor_find_best_erase_type()
1523 if (!erase->size) in spi_nor_find_best_erase_type()
1527 if (region->overlaid && region->size <= len) in spi_nor_find_best_erase_type()
1531 if (erase->size > len) in spi_nor_find_best_erase_type()
1543 * spi_nor_init_erase_cmd() - initialize an erase command
1544 * @region: pointer to a structure that describes a SPI NOR erase region
1545 * @erase: pointer to a structure that describes a SPI NOR erase type
1547 * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
1558 return ERR_PTR(-ENOMEM); in spi_nor_init_erase_cmd()
1560 INIT_LIST_HEAD(&cmd->list); in spi_nor_init_erase_cmd()
1561 cmd->opcode = erase->opcode; in spi_nor_init_erase_cmd()
1562 cmd->count = 1; in spi_nor_init_erase_cmd()
1564 if (region->overlaid) in spi_nor_init_erase_cmd()
1565 cmd->size = region->size; in spi_nor_init_erase_cmd()
1567 cmd->size = erase->size; in spi_nor_init_erase_cmd()
1573 * spi_nor_destroy_erase_cmd_list() - destroy erase command list
1581 list_del(&cmd->list); in spi_nor_destroy_erase_cmd_list()
1587 * spi_nor_init_erase_cmd_list() - initialize erase command list
1588 * @nor: pointer to a 'struct spi_nor'
1597 * Return: 0 on success, -errno otherwise.
1599 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor, in spi_nor_init_erase_cmd_list() argument
1603 const struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_init_erase_cmd_list()
1609 int ret = -EINVAL; in spi_nor_init_erase_cmd_list()
1611 for (i = 0; i < map->n_regions && len; i++) { in spi_nor_init_erase_cmd_list()
1612 region = &map->regions[i]; in spi_nor_init_erase_cmd_list()
1613 region_end = region->offset + region->size; in spi_nor_init_erase_cmd_list()
1615 while (len && addr >= region->offset && addr < region_end) { in spi_nor_init_erase_cmd_list()
1621 if (prev_erase != erase || erase->size != cmd->size || in spi_nor_init_erase_cmd_list()
1622 region->overlaid) { in spi_nor_init_erase_cmd_list()
1629 list_add_tail(&cmd->list, erase_list); in spi_nor_init_erase_cmd_list()
1631 cmd->count++; in spi_nor_init_erase_cmd_list()
1634 len -= cmd->size; in spi_nor_init_erase_cmd_list()
1635 addr += cmd->size; in spi_nor_init_erase_cmd_list()
1648 * spi_nor_erase_multi_sectors() - perform a non-uniform erase
1649 * @nor: pointer to a 'struct spi_nor'
1656 * Return: 0 on success, -errno otherwise.
1658 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len) in spi_nor_erase_multi_sectors() argument
1664 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len); in spi_nor_erase_multi_sectors()
1669 nor->erase_opcode = cmd->opcode; in spi_nor_erase_multi_sectors()
1670 while (cmd->count) { in spi_nor_erase_multi_sectors()
1671 …dev_vdbg(nor->dev, "erase_cmd->size = 0x%08x, erase_cmd->opcode = 0x%02x, erase_cmd->count = %u\n", in spi_nor_erase_multi_sectors()
1672 cmd->size, cmd->opcode, cmd->count); in spi_nor_erase_multi_sectors()
1674 ret = spi_nor_lock_device(nor); in spi_nor_erase_multi_sectors()
1678 ret = spi_nor_write_enable(nor); in spi_nor_erase_multi_sectors()
1680 spi_nor_unlock_device(nor); in spi_nor_erase_multi_sectors()
1684 ret = spi_nor_erase_sector(nor, addr); in spi_nor_erase_multi_sectors()
1685 spi_nor_unlock_device(nor); in spi_nor_erase_multi_sectors()
1689 ret = spi_nor_wait_till_ready(nor); in spi_nor_erase_multi_sectors()
1693 addr += cmd->size; in spi_nor_erase_multi_sectors()
1694 cmd->count--; in spi_nor_erase_multi_sectors()
1696 list_del(&cmd->list); in spi_nor_erase_multi_sectors()
1707 static int spi_nor_erase_dice(struct spi_nor *nor, loff_t addr, in spi_nor_erase_dice() argument
1721 (unsigned long)(nor->mtd.size / SZ_2M)); in spi_nor_erase_dice()
1724 ret = spi_nor_lock_device(nor); in spi_nor_erase_dice()
1728 ret = spi_nor_write_enable(nor); in spi_nor_erase_dice()
1730 spi_nor_unlock_device(nor); in spi_nor_erase_dice()
1734 ret = spi_nor_erase_die(nor, addr, die_size); in spi_nor_erase_dice()
1736 spi_nor_unlock_device(nor); in spi_nor_erase_dice()
1740 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout); in spi_nor_erase_dice()
1745 len -= die_size; in spi_nor_erase_dice()
1753 * Erase an address range on the nor chip. The address range may extend
1758 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_erase() local
1759 u8 n_dice = nor->params->n_dice; in spi_nor_erase()
1765 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr, in spi_nor_erase()
1766 (long long)instr->len); in spi_nor_erase()
1768 if (spi_nor_has_uniform_erase(nor)) { in spi_nor_erase()
1769 div_u64_rem(instr->len, mtd->erasesize, &rem); in spi_nor_erase()
1771 return -EINVAL; in spi_nor_erase()
1774 addr = instr->addr; in spi_nor_erase()
1775 len = instr->len; in spi_nor_erase()
1778 die_size = div_u64(mtd->size, n_dice); in spi_nor_erase()
1779 if (!(len & (die_size - 1)) && !(addr & (die_size - 1))) in spi_nor_erase()
1782 die_size = mtd->size; in spi_nor_erase()
1785 ret = spi_nor_prep_and_lock_pe(nor, instr->addr, instr->len); in spi_nor_erase()
1790 if ((len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) || in spi_nor_erase()
1792 ret = spi_nor_erase_dice(nor, addr, len, die_size); in spi_nor_erase()
1801 /* "sector"-at-a-time erase */ in spi_nor_erase()
1802 } else if (spi_nor_has_uniform_erase(nor)) { in spi_nor_erase()
1804 ret = spi_nor_lock_device(nor); in spi_nor_erase()
1808 ret = spi_nor_write_enable(nor); in spi_nor_erase()
1810 spi_nor_unlock_device(nor); in spi_nor_erase()
1814 ret = spi_nor_erase_sector(nor, addr); in spi_nor_erase()
1815 spi_nor_unlock_device(nor); in spi_nor_erase()
1819 ret = spi_nor_wait_till_ready(nor); in spi_nor_erase()
1823 addr += mtd->erasesize; in spi_nor_erase()
1824 len -= mtd->erasesize; in spi_nor_erase()
1829 ret = spi_nor_erase_multi_sectors(nor, addr, len); in spi_nor_erase()
1834 ret = spi_nor_write_disable(nor); in spi_nor_erase()
1837 spi_nor_unlock_and_unprep_pe(nor, instr->addr, instr->len); in spi_nor_erase()
1843 * spi_nor_sr1_bit6_quad_enable() - Set the Quad Enable BIT(6) in the Status
1845 * @nor: pointer to a 'struct spi_nor'
1849 * Return: 0 on success, -errno otherwise.
1851 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor) in spi_nor_sr1_bit6_quad_enable() argument
1855 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr1_bit6_quad_enable()
1859 if (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6) in spi_nor_sr1_bit6_quad_enable()
1862 nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6; in spi_nor_sr1_bit6_quad_enable()
1864 return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]); in spi_nor_sr1_bit6_quad_enable()
1868 * spi_nor_sr2_bit1_quad_enable() - set the Quad Enable BIT(1) in the Status
1870 * @nor: pointer to a 'struct spi_nor'.
1874 * Return: 0 on success, -errno otherwise.
1876 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor) in spi_nor_sr2_bit1_quad_enable() argument
1880 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_sr2_bit1_quad_enable()
1881 return spi_nor_write_16bit_cr_and_check(nor, SR2_QUAD_EN_BIT1); in spi_nor_sr2_bit1_quad_enable()
1883 ret = spi_nor_read_cr(nor, nor->bouncebuf); in spi_nor_sr2_bit1_quad_enable()
1887 if (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1) in spi_nor_sr2_bit1_quad_enable()
1890 nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1; in spi_nor_sr2_bit1_quad_enable()
1892 return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]); in spi_nor_sr2_bit1_quad_enable()
1896 * spi_nor_sr2_bit7_quad_enable() - set QE bit in Status Register 2.
1897 * @nor: pointer to a 'struct spi_nor'
1905 * Return: 0 on success, -errno otherwise.
1907 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor) in spi_nor_sr2_bit7_quad_enable() argument
1909 u8 *sr2 = nor->bouncebuf; in spi_nor_sr2_bit7_quad_enable()
1914 ret = spi_nor_read_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1923 ret = spi_nor_write_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1930 ret = spi_nor_read_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1935 dev_dbg(nor->dev, "SR2: Read back test failed\n"); in spi_nor_sr2_bit7_quad_enable()
1936 return -EIO; in spi_nor_sr2_bit7_quad_enable()
1960 .name = "spi-nor-generic",
1963 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor, in spi_nor_match_id() argument
1970 for (j = 0; j < manufacturers[i]->nparts; j++) { in spi_nor_match_id()
1971 part = &manufacturers[i]->parts[j]; in spi_nor_match_id()
1972 if (part->id && in spi_nor_match_id()
1973 !memcmp(part->id->bytes, id, part->id->len)) { in spi_nor_match_id()
1974 nor->manufacturer = manufacturers[i]; in spi_nor_match_id()
1983 static const struct flash_info *spi_nor_detect(struct spi_nor *nor) in spi_nor_detect() argument
1986 u8 *id = nor->bouncebuf; in spi_nor_detect()
1989 ret = spi_nor_read_id(nor, 0, 0, id, nor->reg_proto); in spi_nor_detect()
1991 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret); in spi_nor_detect()
1996 nor->id = devm_kmemdup(nor->dev, id, SPI_NOR_MAX_ID_LEN, GFP_KERNEL); in spi_nor_detect()
1997 if (!nor->id) in spi_nor_detect()
1998 return ERR_PTR(-ENOMEM); in spi_nor_detect()
2000 info = spi_nor_match_id(nor, id); in spi_nor_detect()
2004 ret = spi_nor_check_sfdp_signature(nor); in spi_nor_detect()
2010 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n", in spi_nor_detect()
2012 return ERR_PTR(-ENODEV); in spi_nor_detect()
2020 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_read() local
2025 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len); in spi_nor_read()
2027 ret = spi_nor_prep_and_lock_rd(nor, from_lock, len_lock); in spi_nor_read()
2034 ret = spi_nor_read_data(nor, addr, len, buf); in spi_nor_read()
2036 /* We shouldn't see 0-length reads */ in spi_nor_read()
2037 ret = -EIO; in spi_nor_read()
2047 len -= ret; in spi_nor_read()
2052 spi_nor_unlock_and_unprep_rd(nor, from_lock, len_lock); in spi_nor_read()
2058 * Write an address range to the nor chip. Data must be written in
2065 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_write() local
2068 u32 page_size = nor->params->page_size; in spi_nor_write()
2070 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len); in spi_nor_write()
2072 ret = spi_nor_prep_and_lock_pe(nor, to, len); in spi_nor_write()
2079 size_t page_offset = addr & (page_size - 1); in spi_nor_write()
2081 size_t page_remain = min_t(size_t, page_size - page_offset, len - i); in spi_nor_write()
2083 ret = spi_nor_lock_device(nor); in spi_nor_write()
2087 ret = spi_nor_write_enable(nor); in spi_nor_write()
2089 spi_nor_unlock_device(nor); in spi_nor_write()
2093 ret = spi_nor_write_data(nor, addr, page_remain, buf + i); in spi_nor_write()
2094 spi_nor_unlock_device(nor); in spi_nor_write()
2099 ret = spi_nor_wait_till_ready(nor); in spi_nor_write()
2107 spi_nor_unlock_and_unprep_pe(nor, to, len); in spi_nor_write()
2112 static int spi_nor_check(struct spi_nor *nor) in spi_nor_check() argument
2114 if (!nor->dev || in spi_nor_check()
2115 (!nor->spimem && !nor->controller_ops) || in spi_nor_check()
2116 (!nor->spimem && nor->controller_ops && in spi_nor_check()
2117 (!nor->controller_ops->read || in spi_nor_check()
2118 !nor->controller_ops->write || in spi_nor_check()
2119 !nor->controller_ops->read_reg || in spi_nor_check()
2120 !nor->controller_ops->write_reg))) { in spi_nor_check()
2121 pr_err("spi-nor: please fill all the necessary fields!\n"); in spi_nor_check()
2122 return -EINVAL; in spi_nor_check()
2125 if (nor->spimem && nor->controller_ops) { in spi_nor_check()
2126 …dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one… in spi_nor_check()
2127 return -EINVAL; in spi_nor_check()
2140 read->num_mode_clocks = num_mode_clocks; in spi_nor_set_read_settings()
2141 read->num_wait_states = num_wait_states; in spi_nor_set_read_settings()
2142 read->opcode = opcode; in spi_nor_set_read_settings()
2143 read->proto = proto; in spi_nor_set_read_settings()
2149 pp->opcode = opcode; in spi_nor_set_pp_settings()
2150 pp->proto = proto; in spi_nor_set_pp_settings()
2161 return -EINVAL; in spi_nor_hwcaps2cmd()
2207 * spi_nor_spimem_check_op - check if the operation is supported
2209 *@nor: pointer to a 'struct spi_nor'
2212 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
2214 static int spi_nor_spimem_check_op(struct spi_nor *nor, in spi_nor_spimem_check_op() argument
2220 * SPI controller implementation should not check the opcode, in spi_nor_spimem_check_op()
2223 op->addr.nbytes = 4; in spi_nor_spimem_check_op()
2224 if (!spi_mem_supports_op(nor->spimem, op)) { in spi_nor_spimem_check_op()
2225 if (nor->params->size > SZ_16M) in spi_nor_spimem_check_op()
2226 return -EOPNOTSUPP; in spi_nor_spimem_check_op()
2229 op->addr.nbytes = 3; in spi_nor_spimem_check_op()
2230 if (!spi_mem_supports_op(nor->spimem, op)) in spi_nor_spimem_check_op()
2231 return -EOPNOTSUPP; in spi_nor_spimem_check_op()
2238 * spi_nor_spimem_check_readop - check if the read op is supported
2240 *@nor: pointer to a 'struct spi_nor'
2243 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
2245 static int spi_nor_spimem_check_readop(struct spi_nor *nor, in spi_nor_spimem_check_readop() argument
2248 struct spi_mem_op op = SPI_NOR_READ_OP(read->opcode); in spi_nor_spimem_check_readop()
2250 spi_nor_spimem_setup_op(nor, &op, read->proto); in spi_nor_spimem_check_readop()
2253 op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) * in spi_nor_spimem_check_readop()
2255 if (spi_nor_protocol_is_dtr(nor->read_proto)) in spi_nor_spimem_check_readop()
2258 return spi_nor_spimem_check_op(nor, &op); in spi_nor_spimem_check_readop()
2262 * spi_nor_spimem_check_pp - check if the page program op is supported
2264 *@nor: pointer to a 'struct spi_nor'
2267 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise.
2269 static int spi_nor_spimem_check_pp(struct spi_nor *nor, in spi_nor_spimem_check_pp() argument
2272 struct spi_mem_op op = SPI_NOR_PP_OP(pp->opcode); in spi_nor_spimem_check_pp()
2274 spi_nor_spimem_setup_op(nor, &op, pp->proto); in spi_nor_spimem_check_pp()
2276 return spi_nor_spimem_check_op(nor, &op); in spi_nor_spimem_check_pp()
2280 * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
2281 * based on SPI controller capabilities
2282 * @nor: pointer to a 'struct spi_nor'
2287 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps) in spi_nor_spimem_adjust_hwcaps() argument
2289 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_spimem_adjust_hwcaps()
2292 /* X-X-X modes are not supported yet, mask them all. */ in spi_nor_spimem_adjust_hwcaps()
2299 if (nor->flags & SNOR_F_BROKEN_RESET) in spi_nor_spimem_adjust_hwcaps()
2310 spi_nor_spimem_check_readop(nor, &params->reads[rdidx])) in spi_nor_spimem_adjust_hwcaps()
2317 if (spi_nor_spimem_check_pp(nor, in spi_nor_spimem_adjust_hwcaps()
2318 &params->page_programs[ppidx])) in spi_nor_spimem_adjust_hwcaps()
2324 * spi_nor_set_erase_type() - set a SPI NOR erase type
2325 * @erase: pointer to a structure that describes a SPI NOR erase type
2327 * @opcode: the SPI command op code to erase the sector/block
2332 erase->size = size; in spi_nor_set_erase_type()
2333 erase->opcode = opcode; in spi_nor_set_erase_type()
2335 erase->size_shift = ffs(erase->size) - 1; in spi_nor_set_erase_type()
2336 erase->size_mask = (1 << erase->size_shift) - 1; in spi_nor_set_erase_type()
2340 * spi_nor_mask_erase_type() - mask out a SPI NOR erase type
2341 * @erase: pointer to a structure that describes a SPI NOR erase type
2345 erase->size = 0; in spi_nor_mask_erase_type()
2349 * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
2350 * @map: the erase map of the SPI NOR
2353 * @flash_size: the spi nor flash memory size
2358 map->uniform_region.offset = 0; in spi_nor_init_uniform_erase_map()
2359 map->uniform_region.size = flash_size; in spi_nor_init_uniform_erase_map()
2360 map->uniform_region.erase_mask = erase_mask; in spi_nor_init_uniform_erase_map()
2361 map->regions = &map->uniform_region; in spi_nor_init_uniform_erase_map()
2362 map->n_regions = 1; in spi_nor_init_uniform_erase_map()
2365 int spi_nor_post_bfpt_fixups(struct spi_nor *nor, in spi_nor_post_bfpt_fixups() argument
2371 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_post_bfpt_fixups()
2372 nor->manufacturer->fixups->post_bfpt) { in spi_nor_post_bfpt_fixups()
2373 ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header, in spi_nor_post_bfpt_fixups()
2379 if (nor->info->fixups && nor->info->fixups->post_bfpt) in spi_nor_post_bfpt_fixups()
2380 return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt); in spi_nor_post_bfpt_fixups()
2385 static int spi_nor_select_read(struct spi_nor *nor, in spi_nor_select_read() argument
2388 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1; in spi_nor_select_read()
2392 return -EINVAL; in spi_nor_select_read()
2396 return -EINVAL; in spi_nor_select_read()
2398 read = &nor->params->reads[cmd]; in spi_nor_select_read()
2399 nor->read_opcode = read->opcode; in spi_nor_select_read()
2400 nor->read_proto = read->proto; in spi_nor_select_read()
2403 * In the SPI NOR framework, we don't need to make the difference in spi_nor_select_read()
2406 * flash memory to know whether it should enter or leave its 0-4-4 in spi_nor_select_read()
2408 * eXecution In Place is out of the scope of the mtd sub-system. in spi_nor_select_read()
2412 nor->read_dummy = read->num_mode_clocks + read->num_wait_states; in spi_nor_select_read()
2416 static int spi_nor_select_pp(struct spi_nor *nor, in spi_nor_select_pp() argument
2419 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1; in spi_nor_select_pp()
2423 return -EINVAL; in spi_nor_select_pp()
2427 return -EINVAL; in spi_nor_select_pp()
2429 pp = &nor->params->page_programs[cmd]; in spi_nor_select_pp()
2430 nor->program_opcode = pp->opcode; in spi_nor_select_pp()
2431 nor->write_proto = pp->proto; in spi_nor_select_pp()
2436 * spi_nor_select_uniform_erase() - select optimum uniform erase type
2437 * @map: the erase map of the SPI NOR
2449 u8 uniform_erase_type = map->uniform_region.erase_mask; in spi_nor_select_uniform_erase()
2455 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) { in spi_nor_select_uniform_erase()
2459 tested_erase = &map->erase_type[i]; in spi_nor_select_uniform_erase()
2462 if (!tested_erase->size) in spi_nor_select_uniform_erase()
2470 tested_erase->size == SZ_4K) { in spi_nor_select_uniform_erase()
2479 if (!erase && tested_erase->size) in spi_nor_select_uniform_erase()
2488 map->uniform_region.erase_mask = BIT(erase - map->erase_type); in spi_nor_select_uniform_erase()
2492 static int spi_nor_select_erase(struct spi_nor *nor) in spi_nor_select_erase() argument
2494 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_select_erase()
2496 struct mtd_info *mtd = &nor->mtd; in spi_nor_select_erase()
2501 * that the SPI flash memory has an uniform layout then used only one in spi_nor_select_erase()
2504 * manage the SPI flash memory as uniform with a single erase sector in spi_nor_select_erase()
2507 if (spi_nor_has_uniform_erase(nor)) { in spi_nor_select_erase()
2510 return -EINVAL; in spi_nor_select_erase()
2511 nor->erase_opcode = erase->opcode; in spi_nor_select_erase()
2512 mtd->erasesize = erase->size; in spi_nor_select_erase()
2517 * For non-uniform SPI flash memory, set mtd->erasesize to the in spi_nor_select_erase()
2518 * maximum erase sector size. No need to set nor->erase_opcode. in spi_nor_select_erase()
2520 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) { in spi_nor_select_erase()
2521 if (map->erase_type[i].size) { in spi_nor_select_erase()
2522 erase = &map->erase_type[i]; in spi_nor_select_erase()
2528 return -EINVAL; in spi_nor_select_erase()
2530 mtd->erasesize = erase->size; in spi_nor_select_erase()
2534 static int spi_nor_set_addr_nbytes(struct spi_nor *nor) in spi_nor_set_addr_nbytes() argument
2536 if (nor->params->addr_nbytes) { in spi_nor_set_addr_nbytes()
2537 nor->addr_nbytes = nor->params->addr_nbytes; in spi_nor_set_addr_nbytes()
2538 } else if (nor->read_proto == SNOR_PROTO_8_8_8_DTR) { in spi_nor_set_addr_nbytes()
2540 * In 8D-8D-8D mode, one byte takes half a cycle to transfer. So in spi_nor_set_addr_nbytes()
2548 * Force all 8D-8D-8D flashes to use an addr_nbytes of 4 to in spi_nor_set_addr_nbytes()
2551 nor->addr_nbytes = 4; in spi_nor_set_addr_nbytes()
2552 } else if (nor->info->addr_nbytes) { in spi_nor_set_addr_nbytes()
2553 nor->addr_nbytes = nor->info->addr_nbytes; in spi_nor_set_addr_nbytes()
2555 nor->addr_nbytes = 3; in spi_nor_set_addr_nbytes()
2558 if (nor->addr_nbytes == 3 && nor->params->size > 0x1000000) { in spi_nor_set_addr_nbytes()
2559 /* enable 4-byte addressing if the device exceeds 16MiB */ in spi_nor_set_addr_nbytes()
2560 nor->addr_nbytes = 4; in spi_nor_set_addr_nbytes()
2563 if (nor->addr_nbytes > SPI_NOR_MAX_ADDR_NBYTES) { in spi_nor_set_addr_nbytes()
2564 dev_dbg(nor->dev, "The number of address bytes is too large: %u\n", in spi_nor_set_addr_nbytes()
2565 nor->addr_nbytes); in spi_nor_set_addr_nbytes()
2566 return -EINVAL; in spi_nor_set_addr_nbytes()
2570 if (nor->addr_nbytes == 4 && nor->flags & SNOR_F_4B_OPCODES && in spi_nor_set_addr_nbytes()
2571 !(nor->flags & SNOR_F_HAS_4BAIT)) in spi_nor_set_addr_nbytes()
2572 spi_nor_set_4byte_opcodes(nor); in spi_nor_set_addr_nbytes()
2577 static int spi_nor_setup(struct spi_nor *nor, in spi_nor_setup() argument
2580 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_setup()
2585 * Keep only the hardware capabilities supported by both the SPI in spi_nor_setup()
2586 * controller and the SPI flash memory. in spi_nor_setup()
2588 shared_mask = hwcaps->mask & params->hwcaps.mask; in spi_nor_setup()
2590 if (nor->spimem) { in spi_nor_setup()
2593 * need to discard some of them based on what the SPI in spi_nor_setup()
2596 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask); in spi_nor_setup()
2599 * SPI n-n-n protocols are not supported when the SPI in spi_nor_setup()
2601 * Yet another reason to switch to spi-mem. in spi_nor_setup()
2605 dev_dbg(nor->dev, in spi_nor_setup()
2606 "SPI n-n-n protocols are not supported.\n"); in spi_nor_setup()
2612 err = spi_nor_select_read(nor, shared_mask); in spi_nor_setup()
2614 dev_dbg(nor->dev, in spi_nor_setup()
2615 "can't select read settings supported by both the SPI controller and memory.\n"); in spi_nor_setup()
2620 err = spi_nor_select_pp(nor, shared_mask); in spi_nor_setup()
2622 dev_dbg(nor->dev, in spi_nor_setup()
2623 "can't select write settings supported by both the SPI controller and memory.\n"); in spi_nor_setup()
2628 err = spi_nor_select_erase(nor); in spi_nor_setup()
2630 dev_dbg(nor->dev, in spi_nor_setup()
2631 "can't select erase settings supported by both the SPI controller and memory.\n"); in spi_nor_setup()
2635 return spi_nor_set_addr_nbytes(nor); in spi_nor_setup()
2639 * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and
2640 * settings based on MFR register and ->default_init() hook.
2641 * @nor: pointer to a 'struct spi_nor'.
2643 static void spi_nor_manufacturer_init_params(struct spi_nor *nor) in spi_nor_manufacturer_init_params() argument
2645 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_manufacturer_init_params()
2646 nor->manufacturer->fixups->default_init) in spi_nor_manufacturer_init_params()
2647 nor->manufacturer->fixups->default_init(nor); in spi_nor_manufacturer_init_params()
2649 if (nor->info->fixups && nor->info->fixups->default_init) in spi_nor_manufacturer_init_params()
2650 nor->info->fixups->default_init(nor); in spi_nor_manufacturer_init_params()
2654 * spi_nor_no_sfdp_init_params() - Initialize the flash's parameters and
2655 * settings based on nor->info->sfdp_flags. This method should be called only by
2660 * @nor: pointer to a 'struct spi_nor'.
2662 static void spi_nor_no_sfdp_init_params(struct spi_nor *nor) in spi_nor_no_sfdp_init_params() argument
2664 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_no_sfdp_init_params()
2665 struct spi_nor_erase_map *map = &params->erase_map; in spi_nor_no_sfdp_init_params()
2666 const struct flash_info *info = nor->info; in spi_nor_no_sfdp_init_params()
2667 const u8 no_sfdp_flags = info->no_sfdp_flags; in spi_nor_no_sfdp_init_params()
2671 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2; in spi_nor_no_sfdp_init_params()
2672 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2], in spi_nor_no_sfdp_init_params()
2678 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4; in spi_nor_no_sfdp_init_params()
2679 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4], in spi_nor_no_sfdp_init_params()
2685 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8; in spi_nor_no_sfdp_init_params()
2686 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_8], in spi_nor_no_sfdp_init_params()
2692 params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR; in spi_nor_no_sfdp_init_params()
2693 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_8_8_8_DTR], in spi_nor_no_sfdp_init_params()
2699 params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR; in spi_nor_no_sfdp_init_params()
2702 * Legacy SPI, use Legacy SPI opcode there as well. in spi_nor_no_sfdp_init_params()
2704 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_8_8_8_DTR], in spi_nor_no_sfdp_init_params()
2716 spi_nor_set_erase_type(&map->erase_type[i], 4096u, in spi_nor_no_sfdp_init_params()
2721 spi_nor_set_erase_type(&map->erase_type[i], in spi_nor_no_sfdp_init_params()
2722 info->sector_size ?: SPI_NOR_DEFAULT_SECTOR_SIZE, in spi_nor_no_sfdp_init_params()
2724 spi_nor_init_uniform_erase_map(map, erase_mask, params->size); in spi_nor_no_sfdp_init_params()
2728 * spi_nor_init_flags() - Initialize NOR flags for settings that are not defined
2730 * @nor: pointer to a 'struct spi_nor'
2732 static void spi_nor_init_flags(struct spi_nor *nor) in spi_nor_init_flags() argument
2734 struct device_node *np = spi_nor_get_flash_node(nor); in spi_nor_init_flags()
2735 const u16 flags = nor->info->flags; in spi_nor_init_flags()
2737 if (of_property_read_bool(np, "broken-flash-reset")) in spi_nor_init_flags()
2738 nor->flags |= SNOR_F_BROKEN_RESET; in spi_nor_init_flags()
2740 if (of_property_read_bool(np, "no-wp")) in spi_nor_init_flags()
2741 nor->flags |= SNOR_F_NO_WP; in spi_nor_init_flags()
2744 nor->flags |= SNOR_F_SWP_IS_VOLATILE; in spi_nor_init_flags()
2747 nor->flags |= SNOR_F_HAS_LOCK; in spi_nor_init_flags()
2750 nor->flags |= SNOR_F_HAS_SR_TB; in spi_nor_init_flags()
2752 nor->flags |= SNOR_F_HAS_SR_TB_BIT6; in spi_nor_init_flags()
2756 nor->flags |= SNOR_F_HAS_4BIT_BP; in spi_nor_init_flags()
2758 nor->flags |= SNOR_F_HAS_SR_BP3_BIT6; in spi_nor_init_flags()
2761 if (flags & SPI_NOR_RWW && nor->params->n_banks > 1 && in spi_nor_init_flags()
2762 !nor->controller_ops) in spi_nor_init_flags()
2763 nor->flags |= SNOR_F_RWW; in spi_nor_init_flags()
2767 * spi_nor_init_fixup_flags() - Initialize NOR flags for settings that can not
2772 * @nor: pointer to a 'struct spi_nor'
2774 static void spi_nor_init_fixup_flags(struct spi_nor *nor) in spi_nor_init_fixup_flags() argument
2776 const u8 fixup_flags = nor->info->fixup_flags; in spi_nor_init_fixup_flags()
2779 nor->flags |= SNOR_F_4B_OPCODES; in spi_nor_init_fixup_flags()
2782 nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE; in spi_nor_init_fixup_flags()
2786 * spi_nor_late_init_params() - Late initialization of default flash parameters.
2787 * @nor: pointer to a 'struct spi_nor'
2793 static int spi_nor_late_init_params(struct spi_nor *nor) in spi_nor_late_init_params() argument
2795 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_late_init_params()
2798 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_late_init_params()
2799 nor->manufacturer->fixups->late_init) { in spi_nor_late_init_params()
2800 ret = nor->manufacturer->fixups->late_init(nor); in spi_nor_late_init_params()
2806 spi_nor_init_flags(nor); in spi_nor_late_init_params()
2808 if (nor->info->fixups && nor->info->fixups->late_init) { in spi_nor_late_init_params()
2809 ret = nor->info->fixups->late_init(nor); in spi_nor_late_init_params()
2814 if (!nor->params->die_erase_opcode) in spi_nor_late_init_params()
2815 nor->params->die_erase_opcode = SPINOR_OP_CHIP_ERASE; in spi_nor_late_init_params()
2818 if (!params->set_4byte_addr_mode) in spi_nor_late_init_params()
2819 params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_brwr; in spi_nor_late_init_params()
2821 spi_nor_init_fixup_flags(nor); in spi_nor_late_init_params()
2824 * NOR protection support. When locking_ops are not provided, we pick in spi_nor_late_init_params()
2827 if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops) in spi_nor_late_init_params()
2828 spi_nor_init_default_locking_ops(nor); in spi_nor_late_init_params()
2830 if (params->n_banks > 1) in spi_nor_late_init_params()
2831 params->bank_size = div_u64(params->size, params->n_banks); in spi_nor_late_init_params()
2837 * spi_nor_sfdp_init_params_deprecated() - Deprecated way of initializing flash
2839 * @nor: pointer to a 'struct spi_nor'.
2841 * The method has a roll-back mechanism: in case the SFDP parsing fails, the
2844 static void spi_nor_sfdp_init_params_deprecated(struct spi_nor *nor) in spi_nor_sfdp_init_params_deprecated() argument
2848 memcpy(&sfdp_params, nor->params, sizeof(sfdp_params)); in spi_nor_sfdp_init_params_deprecated()
2850 if (spi_nor_parse_sfdp(nor)) { in spi_nor_sfdp_init_params_deprecated()
2851 memcpy(nor->params, &sfdp_params, sizeof(*nor->params)); in spi_nor_sfdp_init_params_deprecated()
2852 nor->flags &= ~SNOR_F_4B_OPCODES; in spi_nor_sfdp_init_params_deprecated()
2857 * spi_nor_init_params_deprecated() - Deprecated way of initializing flash
2859 * @nor: pointer to a 'struct spi_nor'.
2865 static void spi_nor_init_params_deprecated(struct spi_nor *nor) in spi_nor_init_params_deprecated() argument
2867 spi_nor_no_sfdp_init_params(nor); in spi_nor_init_params_deprecated()
2869 spi_nor_manufacturer_init_params(nor); in spi_nor_init_params_deprecated()
2871 if (nor->info->no_sfdp_flags & (SPI_NOR_DUAL_READ | in spi_nor_init_params_deprecated()
2875 spi_nor_sfdp_init_params_deprecated(nor); in spi_nor_init_params_deprecated()
2879 * spi_nor_init_default_params() - Default initialization of flash parameters
2882 * @nor: pointer to a 'struct spi_nor'.
2884 static void spi_nor_init_default_params(struct spi_nor *nor) in spi_nor_init_default_params() argument
2886 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_init_default_params()
2887 const struct flash_info *info = nor->info; in spi_nor_init_default_params()
2888 struct device_node *np = spi_nor_get_flash_node(nor); in spi_nor_init_default_params()
2890 params->quad_enable = spi_nor_sr2_bit1_quad_enable; in spi_nor_init_default_params()
2891 params->otp.org = info->otp; in spi_nor_init_default_params()
2893 /* Default to 16-bit Write Status (01h) Command */ in spi_nor_init_default_params()
2894 nor->flags |= SNOR_F_HAS_16BIT_SR; in spi_nor_init_default_params()
2896 /* Set SPI NOR sizes. */ in spi_nor_init_default_params()
2897 params->writesize = 1; in spi_nor_init_default_params()
2898 params->size = info->size; in spi_nor_init_default_params()
2899 params->bank_size = params->size; in spi_nor_init_default_params()
2900 params->page_size = info->page_size ?: SPI_NOR_DEFAULT_PAGE_SIZE; in spi_nor_init_default_params()
2901 params->n_banks = info->n_banks ?: SPI_NOR_DEFAULT_N_BANKS; in spi_nor_init_default_params()
2903 /* Default to Fast Read for non-DT and enable it if requested by DT. */ in spi_nor_init_default_params()
2904 if (!np || of_property_read_bool(np, "m25p,fast-read")) in spi_nor_init_default_params()
2905 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST; in spi_nor_init_default_params()
2908 params->hwcaps.mask |= SNOR_HWCAPS_READ; in spi_nor_init_default_params()
2909 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ], in spi_nor_init_default_params()
2913 if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST) in spi_nor_init_default_params()
2914 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST], in spi_nor_init_default_params()
2918 params->hwcaps.mask |= SNOR_HWCAPS_PP; in spi_nor_init_default_params()
2919 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP], in spi_nor_init_default_params()
2922 if (info->flags & SPI_NOR_QUAD_PP) { in spi_nor_init_default_params()
2923 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4; in spi_nor_init_default_params()
2924 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4], in spi_nor_init_default_params()
2930 * spi_nor_init_params() - Initialize the flash's parameters and settings.
2931 * @nor: pointer to a 'struct spi_nor'.
2937 * based on nor->info data:
2943 * based on MFR, by using specific flash_info tweeks, ->default_init():
2951 * Please note that there is a ->post_bfpt() fixup hook that can overwrite
2964 * Return: 0 on success, -errno otherwise.
2966 static int spi_nor_init_params(struct spi_nor *nor) in spi_nor_init_params() argument
2970 nor->params = devm_kzalloc(nor->dev, sizeof(*nor->params), GFP_KERNEL); in spi_nor_init_params()
2971 if (!nor->params) in spi_nor_init_params()
2972 return -ENOMEM; in spi_nor_init_params()
2974 spi_nor_init_default_params(nor); in spi_nor_init_params()
2976 if (spi_nor_needs_sfdp(nor)) { in spi_nor_init_params()
2977 ret = spi_nor_parse_sfdp(nor); in spi_nor_init_params()
2979 …dev_err(nor->dev, "BFPT parsing failed. Please consider using SPI_NOR_SKIP_SFDP when declaring the… in spi_nor_init_params()
2982 } else if (nor->info->no_sfdp_flags & SPI_NOR_SKIP_SFDP) { in spi_nor_init_params()
2983 spi_nor_no_sfdp_init_params(nor); in spi_nor_init_params()
2985 spi_nor_init_params_deprecated(nor); in spi_nor_init_params()
2988 ret = spi_nor_late_init_params(nor); in spi_nor_init_params()
2992 if (WARN_ON(!is_power_of_2(nor->params->page_size))) in spi_nor_init_params()
2993 return -EINVAL; in spi_nor_init_params()
2998 /** spi_nor_set_octal_dtr() - enable or disable Octal DTR I/O.
2999 * @nor: pointer to a 'struct spi_nor'
3002 * Return: 0 on success, -errno otherwise.
3004 static int spi_nor_set_octal_dtr(struct spi_nor *nor, bool enable) in spi_nor_set_octal_dtr() argument
3008 if (!nor->params->set_octal_dtr) in spi_nor_set_octal_dtr()
3011 if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR && in spi_nor_set_octal_dtr()
3012 nor->write_proto == SNOR_PROTO_8_8_8_DTR)) in spi_nor_set_octal_dtr()
3015 if (!(nor->flags & SNOR_F_IO_MODE_EN_VOLATILE)) in spi_nor_set_octal_dtr()
3018 ret = nor->params->set_octal_dtr(nor, enable); in spi_nor_set_octal_dtr()
3023 nor->reg_proto = SNOR_PROTO_8_8_8_DTR; in spi_nor_set_octal_dtr()
3025 nor->reg_proto = SNOR_PROTO_1_1_1; in spi_nor_set_octal_dtr()
3031 * spi_nor_quad_enable() - enable Quad I/O if needed.
3032 * @nor: pointer to a 'struct spi_nor'
3034 * Return: 0 on success, -errno otherwise.
3036 static int spi_nor_quad_enable(struct spi_nor *nor) in spi_nor_quad_enable() argument
3038 if (!nor->params->quad_enable) in spi_nor_quad_enable()
3041 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 || in spi_nor_quad_enable()
3042 spi_nor_get_protocol_width(nor->write_proto) == 4)) in spi_nor_quad_enable()
3045 return nor->params->quad_enable(nor); in spi_nor_quad_enable()
3049 * spi_nor_set_4byte_addr_mode() - Set address mode.
3050 * @nor: pointer to a 'struct spi_nor'.
3053 * Return: 0 on success, -errno otherwise.
3055 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable) in spi_nor_set_4byte_addr_mode() argument
3057 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_set_4byte_addr_mode()
3068 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET, in spi_nor_set_4byte_addr_mode()
3072 ret = params->set_4byte_addr_mode(nor, enable); in spi_nor_set_4byte_addr_mode()
3073 if (ret && ret != -EOPNOTSUPP) in spi_nor_set_4byte_addr_mode()
3077 params->addr_nbytes = 4; in spi_nor_set_4byte_addr_mode()
3078 params->addr_mode_nbytes = 4; in spi_nor_set_4byte_addr_mode()
3080 params->addr_nbytes = 3; in spi_nor_set_4byte_addr_mode()
3081 params->addr_mode_nbytes = 3; in spi_nor_set_4byte_addr_mode()
3087 static int spi_nor_init(struct spi_nor *nor) in spi_nor_init() argument
3091 err = spi_nor_set_octal_dtr(nor, true); in spi_nor_init()
3093 dev_dbg(nor->dev, "octal mode not supported\n"); in spi_nor_init()
3097 err = spi_nor_quad_enable(nor); in spi_nor_init()
3099 dev_dbg(nor->dev, "quad mode not supported\n"); in spi_nor_init()
3104 * Some SPI NOR flashes are write protected by default after a power-on in spi_nor_init()
3105 * reset cycle, in order to avoid inadvertent writes during power-up. in spi_nor_init()
3107 * array at power-up by default. Depending on the kernel configuration in spi_nor_init()
3115 nor->flags & SNOR_F_SWP_IS_VOLATILE)) in spi_nor_init()
3116 spi_nor_try_unlock_all(nor); in spi_nor_init()
3118 if (nor->addr_nbytes == 4 && in spi_nor_init()
3119 nor->read_proto != SNOR_PROTO_8_8_8_DTR && in spi_nor_init()
3120 !(nor->flags & SNOR_F_4B_OPCODES)) in spi_nor_init()
3121 return spi_nor_set_4byte_addr_mode(nor, true); in spi_nor_init()
3127 * spi_nor_soft_reset() - Perform a software reset
3128 * @nor: pointer to 'struct spi_nor'
3131 * the device to its power-on-reset state. This is useful when the software has
3139 * Return: 0 on success, -errno otherwise.
3141 static void spi_nor_soft_reset(struct spi_nor *nor) in spi_nor_soft_reset() argument
3148 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_soft_reset()
3150 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_soft_reset()
3152 if (ret != -EOPNOTSUPP) in spi_nor_soft_reset()
3153 dev_warn(nor->dev, "Software reset failed: %d\n", ret); in spi_nor_soft_reset()
3159 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_soft_reset()
3161 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_soft_reset()
3163 dev_warn(nor->dev, "Software reset failed: %d\n", ret); in spi_nor_soft_reset()
3170 * microseconds. So, sleep for a range of 200-400 us. in spi_nor_soft_reset()
3178 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_suspend() local
3182 ret = spi_nor_set_octal_dtr(nor, false); in spi_nor_suspend()
3184 dev_err(nor->dev, "suspend() failed\n"); in spi_nor_suspend()
3192 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_resume() local
3193 struct device *dev = nor->dev; in spi_nor_resume()
3196 /* re-initialize the nor chip */ in spi_nor_resume()
3197 ret = spi_nor_init(nor); in spi_nor_resume()
3205 struct spi_nor *nor = mtd_to_spi_nor(master); in spi_nor_get_device() local
3208 if (nor->spimem) in spi_nor_get_device()
3209 dev = nor->spimem->spi->controller->dev.parent; in spi_nor_get_device()
3211 dev = nor->dev; in spi_nor_get_device()
3213 if (!try_module_get(dev->driver->owner)) in spi_nor_get_device()
3214 return -ENODEV; in spi_nor_get_device()
3222 struct spi_nor *nor = mtd_to_spi_nor(master); in spi_nor_put_device() local
3225 if (nor->spimem) in spi_nor_put_device()
3226 dev = nor->spimem->spi->controller->dev.parent; in spi_nor_put_device()
3228 dev = nor->dev; in spi_nor_put_device()
3230 module_put(dev->driver->owner); in spi_nor_put_device()
3233 static void spi_nor_restore(struct spi_nor *nor) in spi_nor_restore() argument
3238 if (nor->addr_nbytes == 4 && !(nor->flags & SNOR_F_4B_OPCODES) && in spi_nor_restore()
3239 nor->flags & SNOR_F_BROKEN_RESET) { in spi_nor_restore()
3240 ret = spi_nor_set_4byte_addr_mode(nor, false); in spi_nor_restore()
3244 * will default to the 3-byte address mode after the in spi_nor_restore()
3247 dev_err(nor->dev, "Failed to exit 4-byte address mode, err = %d\n", ret); in spi_nor_restore()
3250 if (nor->flags & SNOR_F_SOFT_RESET) in spi_nor_restore()
3251 spi_nor_soft_reset(nor); in spi_nor_restore()
3254 static const struct flash_info *spi_nor_match_name(struct spi_nor *nor, in spi_nor_match_name() argument
3260 for (j = 0; j < manufacturers[i]->nparts; j++) { in spi_nor_match_name()
3261 if (manufacturers[i]->parts[j].name && in spi_nor_match_name()
3262 !strcmp(name, manufacturers[i]->parts[j].name)) { in spi_nor_match_name()
3263 nor->manufacturer = manufacturers[i]; in spi_nor_match_name()
3264 return &manufacturers[i]->parts[j]; in spi_nor_match_name()
3272 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor, in spi_nor_get_flash_info() argument
3278 info = spi_nor_match_name(nor, name); in spi_nor_get_flash_info()
3280 * Auto-detect if chip name wasn't specified or not found, or the chip in spi_nor_get_flash_info()
3282 * auto-detection to compare it later. in spi_nor_get_flash_info()
3284 if (!info || info->id) { in spi_nor_get_flash_info()
3287 jinfo = spi_nor_detect(nor); in spi_nor_get_flash_info()
3296 dev_warn(nor->dev, "found %s, expected %s\n", in spi_nor_get_flash_info()
3297 jinfo->name, info->name); in spi_nor_get_flash_info()
3312 if (region->overlaid) in spi_nor_get_region_erasesize()
3313 return region->size; in spi_nor_get_region_erasesize()
3315 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) { in spi_nor_get_region_erasesize()
3316 if (region->erase_mask & BIT(i)) in spi_nor_get_region_erasesize()
3323 static int spi_nor_set_mtd_eraseregions(struct spi_nor *nor) in spi_nor_set_mtd_eraseregions() argument
3325 const struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_set_mtd_eraseregions()
3326 const struct spi_nor_erase_region *region = map->regions; in spi_nor_set_mtd_eraseregions()
3328 struct mtd_info *mtd = &nor->mtd; in spi_nor_set_mtd_eraseregions()
3331 mtd_region = devm_kcalloc(nor->dev, map->n_regions, sizeof(*mtd_region), in spi_nor_set_mtd_eraseregions()
3334 return -ENOMEM; in spi_nor_set_mtd_eraseregions()
3336 for (i = 0; i < map->n_regions; i++) { in spi_nor_set_mtd_eraseregions()
3338 map->erase_type); in spi_nor_set_mtd_eraseregions()
3340 return -EINVAL; in spi_nor_set_mtd_eraseregions()
3347 mtd->numeraseregions = map->n_regions; in spi_nor_set_mtd_eraseregions()
3348 mtd->eraseregions = mtd_region; in spi_nor_set_mtd_eraseregions()
3353 static int spi_nor_set_mtd_info(struct spi_nor *nor) in spi_nor_set_mtd_info() argument
3355 struct mtd_info *mtd = &nor->mtd; in spi_nor_set_mtd_info()
3356 struct device *dev = nor->dev; in spi_nor_set_mtd_info()
3358 spi_nor_set_mtd_locking_ops(nor); in spi_nor_set_mtd_info()
3359 spi_nor_set_mtd_otp_ops(nor); in spi_nor_set_mtd_info()
3361 mtd->dev.parent = dev; in spi_nor_set_mtd_info()
3362 if (!mtd->name) in spi_nor_set_mtd_info()
3363 mtd->name = dev_name(dev); in spi_nor_set_mtd_info()
3364 mtd->type = MTD_NORFLASH; in spi_nor_set_mtd_info()
3365 mtd->flags = MTD_CAP_NORFLASH; in spi_nor_set_mtd_info()
3366 /* Unset BIT_WRITEABLE to enable JFFS2 write buffer for ECC'd NOR */ in spi_nor_set_mtd_info()
3367 if (nor->flags & SNOR_F_ECC) in spi_nor_set_mtd_info()
3368 mtd->flags &= ~MTD_BIT_WRITEABLE; in spi_nor_set_mtd_info()
3369 if (nor->info->flags & SPI_NOR_NO_ERASE) in spi_nor_set_mtd_info()
3370 mtd->flags |= MTD_NO_ERASE; in spi_nor_set_mtd_info()
3372 mtd->_erase = spi_nor_erase; in spi_nor_set_mtd_info()
3373 mtd->writesize = nor->params->writesize; in spi_nor_set_mtd_info()
3374 mtd->writebufsize = nor->params->page_size; in spi_nor_set_mtd_info()
3375 mtd->size = nor->params->size; in spi_nor_set_mtd_info()
3376 mtd->_read = spi_nor_read; in spi_nor_set_mtd_info()
3378 if (!mtd->_write) in spi_nor_set_mtd_info()
3379 mtd->_write = spi_nor_write; in spi_nor_set_mtd_info()
3380 mtd->_suspend = spi_nor_suspend; in spi_nor_set_mtd_info()
3381 mtd->_resume = spi_nor_resume; in spi_nor_set_mtd_info()
3382 mtd->_get_device = spi_nor_get_device; in spi_nor_set_mtd_info()
3383 mtd->_put_device = spi_nor_put_device; in spi_nor_set_mtd_info()
3385 if (!spi_nor_has_uniform_erase(nor)) in spi_nor_set_mtd_info()
3386 return spi_nor_set_mtd_eraseregions(nor); in spi_nor_set_mtd_info()
3391 static int spi_nor_hw_reset(struct spi_nor *nor) in spi_nor_hw_reset() argument
3395 reset = devm_gpiod_get_optional(nor->dev, "reset", GPIOD_OUT_LOW); in spi_nor_hw_reset()
3412 int spi_nor_scan(struct spi_nor *nor, const char *name, in spi_nor_scan() argument
3416 struct device *dev = nor->dev; in spi_nor_scan()
3419 ret = spi_nor_check(nor); in spi_nor_scan()
3423 /* Reset SPI protocol for all commands. */ in spi_nor_scan()
3424 nor->reg_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
3425 nor->read_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
3426 nor->write_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
3430 * through the spi-mem layer (buffers have to be DMA-able). in spi_nor_scan()
3431 * For spi-mem drivers, we'll reallocate a new buffer if in spi_nor_scan()
3432 * nor->params->page_size turns out to be greater than PAGE_SIZE (which in spi_nor_scan()
3433 * shouldn't happen before long since NOR pages are usually less in spi_nor_scan()
3436 nor->bouncebuf_size = PAGE_SIZE; in spi_nor_scan()
3437 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size, in spi_nor_scan()
3439 if (!nor->bouncebuf) in spi_nor_scan()
3440 return -ENOMEM; in spi_nor_scan()
3442 ret = spi_nor_hw_reset(nor); in spi_nor_scan()
3446 info = spi_nor_get_flash_info(nor, name); in spi_nor_scan()
3450 nor->info = info; in spi_nor_scan()
3452 mutex_init(&nor->lock); in spi_nor_scan()
3455 ret = spi_nor_init_params(nor); in spi_nor_scan()
3459 if (spi_nor_use_parallel_locking(nor)) in spi_nor_scan()
3460 init_waitqueue_head(&nor->rww.wait); in spi_nor_scan()
3463 * Configure the SPI memory: in spi_nor_scan()
3464 * - select op codes for (Fast) Read, Page Program and Sector Erase. in spi_nor_scan()
3465 * - set the number of dummy cycles (mode cycles + wait states). in spi_nor_scan()
3466 * - set the SPI protocols for register and memory accesses. in spi_nor_scan()
3467 * - set the number of address bytes. in spi_nor_scan()
3469 ret = spi_nor_setup(nor, hwcaps); in spi_nor_scan()
3473 /* Send all the required SPI flash commands to initialize device */ in spi_nor_scan()
3474 ret = spi_nor_init(nor); in spi_nor_scan()
3479 ret = spi_nor_set_mtd_info(nor); in spi_nor_scan()
3484 SPI_NOR_MAX_ID_LEN, nor->id); in spi_nor_scan()
3490 static int spi_nor_create_read_dirmap(struct spi_nor *nor) in spi_nor_create_read_dirmap() argument
3493 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0), in spi_nor_create_read_dirmap()
3494 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0), in spi_nor_create_read_dirmap()
3495 SPI_MEM_OP_DUMMY(nor->read_dummy, 0), in spi_nor_create_read_dirmap()
3498 .length = nor->params->size, in spi_nor_create_read_dirmap()
3502 spi_nor_spimem_setup_op(nor, op, nor->read_proto); in spi_nor_create_read_dirmap()
3505 op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8; in spi_nor_create_read_dirmap()
3506 if (spi_nor_protocol_is_dtr(nor->read_proto)) in spi_nor_create_read_dirmap()
3507 op->dummy.nbytes *= 2; in spi_nor_create_read_dirmap()
3511 * of data bytes is non-zero, the data buswidth won't be set here. So, in spi_nor_create_read_dirmap()
3514 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); in spi_nor_create_read_dirmap()
3516 nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, in spi_nor_create_read_dirmap()
3518 return PTR_ERR_OR_ZERO(nor->dirmap.rdesc); in spi_nor_create_read_dirmap()
3521 static int spi_nor_create_write_dirmap(struct spi_nor *nor) in spi_nor_create_write_dirmap() argument
3524 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0), in spi_nor_create_write_dirmap()
3525 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0), in spi_nor_create_write_dirmap()
3529 .length = nor->params->size, in spi_nor_create_write_dirmap()
3533 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) in spi_nor_create_write_dirmap()
3534 op->addr.nbytes = 0; in spi_nor_create_write_dirmap()
3536 spi_nor_spimem_setup_op(nor, op, nor->write_proto); in spi_nor_create_write_dirmap()
3540 * of data bytes is non-zero, the data buswidth won't be set here. So, in spi_nor_create_write_dirmap()
3543 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); in spi_nor_create_write_dirmap()
3545 nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, in spi_nor_create_write_dirmap()
3547 return PTR_ERR_OR_ZERO(nor->dirmap.wdesc); in spi_nor_create_write_dirmap()
3552 struct spi_device *spi = spimem->spi; in spi_nor_probe() local
3553 struct device *dev = &spi->dev; in spi_nor_probe()
3555 struct spi_nor *nor; in spi_nor_probe() local
3568 nor = devm_kzalloc(dev, sizeof(*nor), GFP_KERNEL); in spi_nor_probe()
3569 if (!nor) in spi_nor_probe()
3570 return -ENOMEM; in spi_nor_probe()
3572 nor->spimem = spimem; in spi_nor_probe()
3573 nor->dev = dev; in spi_nor_probe()
3574 spi_nor_set_flash_node(nor, dev->of_node); in spi_nor_probe()
3576 spi_mem_set_drvdata(spimem, nor); in spi_nor_probe()
3578 if (data && data->name) in spi_nor_probe()
3579 nor->mtd.name = data->name; in spi_nor_probe()
3581 if (!nor->mtd.name) in spi_nor_probe()
3582 nor->mtd.name = spi_mem_get_name(spimem); in spi_nor_probe()
3590 if (data && data->type) in spi_nor_probe()
3591 flash_name = data->type; in spi_nor_probe()
3592 else if (!strcmp(spi->modalias, "spi-nor")) in spi_nor_probe()
3593 flash_name = NULL; /* auto-detect */ in spi_nor_probe()
3595 flash_name = spi->modalias; in spi_nor_probe()
3597 ret = spi_nor_scan(nor, flash_name, &hwcaps); in spi_nor_probe()
3601 spi_nor_debugfs_register(nor); in spi_nor_probe()
3606 * a NOR we don't end up with buffer overflows. in spi_nor_probe()
3608 if (nor->params->page_size > PAGE_SIZE) { in spi_nor_probe()
3609 nor->bouncebuf_size = nor->params->page_size; in spi_nor_probe()
3610 devm_kfree(dev, nor->bouncebuf); in spi_nor_probe()
3611 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size, in spi_nor_probe()
3613 if (!nor->bouncebuf) in spi_nor_probe()
3614 return -ENOMEM; in spi_nor_probe()
3617 ret = spi_nor_create_read_dirmap(nor); in spi_nor_probe()
3621 ret = spi_nor_create_write_dirmap(nor); in spi_nor_probe()
3625 return mtd_device_register(&nor->mtd, data ? data->parts : NULL, in spi_nor_probe()
3626 data ? data->nr_parts : 0); in spi_nor_probe()
3631 struct spi_nor *nor = spi_mem_get_drvdata(spimem); in spi_nor_remove() local
3633 spi_nor_restore(nor); in spi_nor_remove()
3636 return mtd_device_unregister(&nor->mtd); in spi_nor_remove()
3641 struct spi_nor *nor = spi_mem_get_drvdata(spimem); in spi_nor_shutdown() local
3643 spi_nor_restore(nor); in spi_nor_shutdown()
3651 * differences can often be differentiated by the JEDEC read-ID command, we
3652 * encourage new users to add support to the spi-nor library, and simply bind
3653 * against a generic string here (e.g., "jedec,spi-nor").
3660 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
3661 * hack around the fact that the SPI core does not provide uevent
3664 {"spi-nor"},
3668 * them with "spi-nor" in platform data.
3673 * Entries that were used in DTs without "jedec,spi-nor" fallback and
3689 {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
3690 {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
3691 {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
3693 /* Everspin MRAMs (non-JEDEC) */
3701 MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids);
3705 * Generic compatibility for SPI NOR that can be identified by the
3708 { .compatible = "jedec,spi-nor" },
3714 * REVISIT: many of these chips have deep power-down modes, which
3721 .name = "spi-nor",
3748 MODULE_DESCRIPTION("framework for SPI NOR");