Lines Matching full:nor
23 #include <linux/mtd/spi-nor.h>
49 * @nor: pointer to a 'struct spi_nor'
57 static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor, in spi_nor_get_cmd_ext() argument
60 switch (nor->cmd_ext_type) { in spi_nor_get_cmd_ext()
68 dev_err(nor->dev, "Unknown command extension type\n"); in spi_nor_get_cmd_ext()
75 * @nor: pointer to a 'struct spi_nor'
80 void spi_nor_spimem_setup_op(const struct spi_nor *nor, in spi_nor_spimem_setup_op() argument
101 * something like 4S-4D-4D, but SPI NOR can't. So, set all 4 in spi_nor_spimem_setup_op()
112 ext = spi_nor_get_cmd_ext(nor, op); in spi_nor_spimem_setup_op()
121 * @nor: pointer to 'struct spi_nor'
128 static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op) in spi_nor_spimem_bounce() argument
133 if (op->data.nbytes > nor->bouncebuf_size) in spi_nor_spimem_bounce()
134 op->data.nbytes = nor->bouncebuf_size; in spi_nor_spimem_bounce()
135 op->data.buf.in = nor->bouncebuf; in spi_nor_spimem_bounce()
144 * @nor: pointer to 'struct spi_nor'
149 static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op) in spi_nor_spimem_exec_op() argument
153 error = spi_mem_adjust_op_size(nor->spimem, op); in spi_nor_spimem_exec_op()
157 return spi_mem_exec_op(nor->spimem, op); in spi_nor_spimem_exec_op()
160 static int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode, in spi_nor_controller_ops_read_reg() argument
163 if (spi_nor_protocol_is_dtr(nor->reg_proto)) in spi_nor_controller_ops_read_reg()
166 return nor->controller_ops->read_reg(nor, opcode, buf, len); in spi_nor_controller_ops_read_reg()
169 static int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode, in spi_nor_controller_ops_write_reg() argument
172 if (spi_nor_protocol_is_dtr(nor->reg_proto)) in spi_nor_controller_ops_write_reg()
175 return nor->controller_ops->write_reg(nor, opcode, buf, len); in spi_nor_controller_ops_write_reg()
178 static int spi_nor_controller_ops_erase(struct spi_nor *nor, loff_t offs) in spi_nor_controller_ops_erase() argument
180 if (spi_nor_protocol_is_dtr(nor->write_proto)) in spi_nor_controller_ops_erase()
183 return nor->controller_ops->erase(nor, offs); in spi_nor_controller_ops_erase()
189 * @nor: pointer to 'struct spi_nor'
196 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from, in spi_nor_spimem_read_data() argument
200 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0), in spi_nor_spimem_read_data()
201 SPI_MEM_OP_ADDR(nor->addr_width, from, 0), in spi_nor_spimem_read_data()
202 SPI_MEM_OP_DUMMY(nor->read_dummy, 0), in spi_nor_spimem_read_data()
208 spi_nor_spimem_setup_op(nor, &op, nor->read_proto); in spi_nor_spimem_read_data()
211 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; in spi_nor_spimem_read_data()
212 if (spi_nor_protocol_is_dtr(nor->read_proto)) in spi_nor_spimem_read_data()
215 usebouncebuf = spi_nor_spimem_bounce(nor, &op); in spi_nor_spimem_read_data()
217 if (nor->dirmap.rdesc) { in spi_nor_spimem_read_data()
218 nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val, in spi_nor_spimem_read_data()
221 error = spi_nor_spimem_exec_op(nor, &op); in spi_nor_spimem_read_data()
235 * @nor: pointer to 'struct spi_nor'
242 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf) in spi_nor_read_data() argument
244 if (nor->spimem) in spi_nor_read_data()
245 return spi_nor_spimem_read_data(nor, from, len, buf); in spi_nor_read_data()
247 return nor->controller_ops->read(nor, from, len, buf); in spi_nor_read_data()
253 * @nor: pointer to 'struct spi_nor'
260 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to, in spi_nor_spimem_write_data() argument
264 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0), in spi_nor_spimem_write_data()
265 SPI_MEM_OP_ADDR(nor->addr_width, to, 0), in spi_nor_spimem_write_data()
271 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) in spi_nor_spimem_write_data()
274 spi_nor_spimem_setup_op(nor, &op, nor->write_proto); in spi_nor_spimem_write_data()
276 if (spi_nor_spimem_bounce(nor, &op)) in spi_nor_spimem_write_data()
277 memcpy(nor->bouncebuf, buf, op.data.nbytes); in spi_nor_spimem_write_data()
279 if (nor->dirmap.wdesc) { in spi_nor_spimem_write_data()
280 nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val, in spi_nor_spimem_write_data()
283 error = spi_nor_spimem_exec_op(nor, &op); in spi_nor_spimem_write_data()
294 * @nor: pointer to 'struct spi_nor'
301 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len, in spi_nor_write_data() argument
304 if (nor->spimem) in spi_nor_write_data()
305 return spi_nor_spimem_write_data(nor, to, len, buf); in spi_nor_write_data()
307 return nor->controller_ops->write(nor, to, len, buf); in spi_nor_write_data()
312 * @nor: pointer to 'struct spi_nor'.
316 int spi_nor_write_enable(struct spi_nor *nor) in spi_nor_write_enable() argument
320 if (nor->spimem) { in spi_nor_write_enable()
327 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_enable()
329 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_enable()
331 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WREN, in spi_nor_write_enable()
336 dev_dbg(nor->dev, "error %d on Write Enable\n", ret); in spi_nor_write_enable()
343 * @nor: pointer to 'struct spi_nor'.
347 int spi_nor_write_disable(struct spi_nor *nor) in spi_nor_write_disable() argument
351 if (nor->spimem) { in spi_nor_write_disable()
358 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_disable()
360 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_disable()
362 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRDI, in spi_nor_write_disable()
367 dev_dbg(nor->dev, "error %d on Write Disable\n", ret); in spi_nor_write_disable()
374 * @nor: pointer to 'struct spi_nor'.
380 int spi_nor_read_sr(struct spi_nor *nor, u8 *sr) in spi_nor_read_sr() argument
384 if (nor->spimem) { in spi_nor_read_sr()
391 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) { in spi_nor_read_sr()
392 op.addr.nbytes = nor->params->rdsr_addr_nbytes; in spi_nor_read_sr()
393 op.dummy.nbytes = nor->params->rdsr_dummy; in spi_nor_read_sr()
401 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_sr()
403 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_sr()
405 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR, sr, in spi_nor_read_sr()
410 dev_dbg(nor->dev, "error %d reading SR\n", ret); in spi_nor_read_sr()
417 * @nor: pointer to 'struct spi_nor'
424 static int spi_nor_read_fsr(struct spi_nor *nor, u8 *fsr) in spi_nor_read_fsr() argument
428 if (nor->spimem) { in spi_nor_read_fsr()
435 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) { in spi_nor_read_fsr()
436 op.addr.nbytes = nor->params->rdsr_addr_nbytes; in spi_nor_read_fsr()
437 op.dummy.nbytes = nor->params->rdsr_dummy; in spi_nor_read_fsr()
445 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_fsr()
447 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_fsr()
449 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDFSR, fsr, in spi_nor_read_fsr()
454 dev_dbg(nor->dev, "error %d reading FSR\n", ret); in spi_nor_read_fsr()
462 * @nor: pointer to 'struct spi_nor'
468 int spi_nor_read_cr(struct spi_nor *nor, u8 *cr) in spi_nor_read_cr() argument
472 if (nor->spimem) { in spi_nor_read_cr()
479 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_cr()
481 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_cr()
483 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDCR, cr, in spi_nor_read_cr()
488 dev_dbg(nor->dev, "error %d reading CR\n", ret); in spi_nor_read_cr()
495 * @nor: pointer to 'struct spi_nor'.
501 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable) in spi_nor_set_4byte_addr_mode() argument
505 if (nor->spimem) { in spi_nor_set_4byte_addr_mode()
515 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_set_4byte_addr_mode()
517 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_set_4byte_addr_mode()
519 ret = spi_nor_controller_ops_write_reg(nor, in spi_nor_set_4byte_addr_mode()
526 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); in spi_nor_set_4byte_addr_mode()
534 * @nor: pointer to 'struct spi_nor'.
540 static int spansion_set_4byte_addr_mode(struct spi_nor *nor, bool enable) in spansion_set_4byte_addr_mode() argument
544 nor->bouncebuf[0] = enable << 7; in spansion_set_4byte_addr_mode()
546 if (nor->spimem) { in spansion_set_4byte_addr_mode()
551 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 0)); in spansion_set_4byte_addr_mode()
553 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spansion_set_4byte_addr_mode()
555 ret = spi_mem_exec_op(nor->spimem, &op); in spansion_set_4byte_addr_mode()
557 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_BRWR, in spansion_set_4byte_addr_mode()
558 nor->bouncebuf, 1); in spansion_set_4byte_addr_mode()
562 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); in spansion_set_4byte_addr_mode()
569 * @nor: pointer to 'struct spi_nor'.
574 int spi_nor_write_ear(struct spi_nor *nor, u8 ear) in spi_nor_write_ear() argument
578 nor->bouncebuf[0] = ear; in spi_nor_write_ear()
580 if (nor->spimem) { in spi_nor_write_ear()
585 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 0)); in spi_nor_write_ear()
587 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_ear()
589 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_ear()
591 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WREAR, in spi_nor_write_ear()
592 nor->bouncebuf, 1); in spi_nor_write_ear()
596 dev_dbg(nor->dev, "error %d writing EAR\n", ret); in spi_nor_write_ear()
603 * @nor: pointer to 'struct spi_nor'.
609 int spi_nor_xread_sr(struct spi_nor *nor, u8 *sr) in spi_nor_xread_sr() argument
613 if (nor->spimem) { in spi_nor_xread_sr()
620 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_xread_sr()
622 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_xread_sr()
624 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_XRDSR, sr, in spi_nor_xread_sr()
629 dev_dbg(nor->dev, "error %d reading XRDSR\n", ret); in spi_nor_xread_sr()
637 * @nor: pointer to 'struct spi_nor'.
641 static int spi_nor_xsr_ready(struct spi_nor *nor) in spi_nor_xsr_ready() argument
645 ret = spi_nor_xread_sr(nor, nor->bouncebuf); in spi_nor_xsr_ready()
649 return !!(nor->bouncebuf[0] & XSR_RDY); in spi_nor_xsr_ready()
654 * @nor: pointer to 'struct spi_nor'.
656 static void spi_nor_clear_sr(struct spi_nor *nor) in spi_nor_clear_sr() argument
660 if (nor->spimem) { in spi_nor_clear_sr()
667 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_clear_sr()
669 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_clear_sr()
671 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_CLSR, in spi_nor_clear_sr()
676 dev_dbg(nor->dev, "error %d clearing SR\n", ret); in spi_nor_clear_sr()
682 * @nor: pointer to 'struct spi_nor'.
686 static int spi_nor_sr_ready(struct spi_nor *nor) in spi_nor_sr_ready() argument
688 int ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_ready()
693 if (nor->flags & SNOR_F_USE_CLSR && in spi_nor_sr_ready()
694 nor->bouncebuf[0] & (SR_E_ERR | SR_P_ERR)) { in spi_nor_sr_ready()
695 if (nor->bouncebuf[0] & SR_E_ERR) in spi_nor_sr_ready()
696 dev_err(nor->dev, "Erase Error occurred\n"); in spi_nor_sr_ready()
698 dev_err(nor->dev, "Programming Error occurred\n"); in spi_nor_sr_ready()
700 spi_nor_clear_sr(nor); in spi_nor_sr_ready()
708 ret = spi_nor_write_disable(nor); in spi_nor_sr_ready()
715 return !(nor->bouncebuf[0] & SR_WIP); in spi_nor_sr_ready()
720 * @nor: pointer to 'struct spi_nor'.
722 static void spi_nor_clear_fsr(struct spi_nor *nor) in spi_nor_clear_fsr() argument
726 if (nor->spimem) { in spi_nor_clear_fsr()
733 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_clear_fsr()
735 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_clear_fsr()
737 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_CLFSR, in spi_nor_clear_fsr()
742 dev_dbg(nor->dev, "error %d clearing FSR\n", ret); in spi_nor_clear_fsr()
748 * @nor: pointer to 'struct spi_nor'.
752 static int spi_nor_fsr_ready(struct spi_nor *nor) in spi_nor_fsr_ready() argument
754 int ret = spi_nor_read_fsr(nor, nor->bouncebuf); in spi_nor_fsr_ready()
759 if (nor->bouncebuf[0] & (FSR_E_ERR | FSR_P_ERR)) { in spi_nor_fsr_ready()
760 if (nor->bouncebuf[0] & FSR_E_ERR) in spi_nor_fsr_ready()
761 dev_err(nor->dev, "Erase operation failed.\n"); in spi_nor_fsr_ready()
763 dev_err(nor->dev, "Program operation failed.\n"); in spi_nor_fsr_ready()
765 if (nor->bouncebuf[0] & FSR_PT_ERR) in spi_nor_fsr_ready()
766 dev_err(nor->dev, in spi_nor_fsr_ready()
769 spi_nor_clear_fsr(nor); in spi_nor_fsr_ready()
777 ret = spi_nor_write_disable(nor); in spi_nor_fsr_ready()
784 return !!(nor->bouncebuf[0] & FSR_READY); in spi_nor_fsr_ready()
789 * @nor: pointer to 'struct spi_nor'.
793 static int spi_nor_ready(struct spi_nor *nor) in spi_nor_ready() argument
797 if (nor->flags & SNOR_F_READY_XSR_RDY) in spi_nor_ready()
798 sr = spi_nor_xsr_ready(nor); in spi_nor_ready()
800 sr = spi_nor_sr_ready(nor); in spi_nor_ready()
803 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1; in spi_nor_ready()
812 * @nor: pointer to "struct spi_nor".
817 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor, in spi_nor_wait_till_ready_with_timeout() argument
829 ret = spi_nor_ready(nor); in spi_nor_wait_till_ready_with_timeout()
838 dev_dbg(nor->dev, "flash operation timed out\n"); in spi_nor_wait_till_ready_with_timeout()
846 * @nor: pointer to "struct spi_nor".
850 int spi_nor_wait_till_ready(struct spi_nor *nor) in spi_nor_wait_till_ready() argument
852 return spi_nor_wait_till_ready_with_timeout(nor, in spi_nor_wait_till_ready()
858 * @nor: pointer to 'struct spi_nor'.
862 int spi_nor_global_block_unlock(struct spi_nor *nor) in spi_nor_global_block_unlock() argument
866 ret = spi_nor_write_enable(nor); in spi_nor_global_block_unlock()
870 if (nor->spimem) { in spi_nor_global_block_unlock()
877 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_global_block_unlock()
879 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_global_block_unlock()
881 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_GBULK, in spi_nor_global_block_unlock()
886 dev_dbg(nor->dev, "error %d on Global Block Unlock\n", ret); in spi_nor_global_block_unlock()
890 return spi_nor_wait_till_ready(nor); in spi_nor_global_block_unlock()
895 * @nor: pointer to 'struct spi_nor'.
901 int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len) in spi_nor_write_sr() argument
905 ret = spi_nor_write_enable(nor); in spi_nor_write_sr()
909 if (nor->spimem) { in spi_nor_write_sr()
916 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_sr()
918 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_sr()
920 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR, sr, in spi_nor_write_sr()
925 dev_dbg(nor->dev, "error %d writing SR\n", ret); in spi_nor_write_sr()
929 return spi_nor_wait_till_ready(nor); in spi_nor_write_sr()
935 * @nor: pointer to a 'struct spi_nor'.
940 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_sr1_and_check() argument
944 nor->bouncebuf[0] = sr1; in spi_nor_write_sr1_and_check()
946 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1); in spi_nor_write_sr1_and_check()
950 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_write_sr1_and_check()
954 if (nor->bouncebuf[0] != sr1) { in spi_nor_write_sr1_and_check()
955 dev_dbg(nor->dev, "SR1: read back test failed\n"); in spi_nor_write_sr1_and_check()
967 * @nor: pointer to a 'struct spi_nor'.
972 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_16bit_sr_and_check() argument
975 u8 *sr_cr = nor->bouncebuf; in spi_nor_write_16bit_sr_and_check()
979 if (!(nor->flags & SNOR_F_NO_READ_CR)) { in spi_nor_write_16bit_sr_and_check()
980 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_sr_and_check()
983 } else if (nor->params->quad_enable) { in spi_nor_write_16bit_sr_and_check()
991 * nor->params->quad_enable() call. in spi_nor_write_16bit_sr_and_check()
1006 ret = spi_nor_write_sr(nor, sr_cr, 2); in spi_nor_write_16bit_sr_and_check()
1010 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_write_16bit_sr_and_check()
1015 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_sr_and_check()
1020 dev_dbg(nor->dev, "CR: read back test failed\n"); in spi_nor_write_16bit_sr_and_check()
1032 * @nor: pointer to a 'struct spi_nor'.
1037 int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr) in spi_nor_write_16bit_cr_and_check() argument
1040 u8 *sr_cr = nor->bouncebuf; in spi_nor_write_16bit_cr_and_check()
1044 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_cr_and_check()
1050 ret = spi_nor_write_sr(nor, sr_cr, 2); in spi_nor_write_16bit_cr_and_check()
1056 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_cr_and_check()
1061 dev_dbg(nor->dev, "SR: Read back test failed\n"); in spi_nor_write_16bit_cr_and_check()
1065 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_write_16bit_cr_and_check()
1068 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_cr_and_check()
1073 dev_dbg(nor->dev, "CR: read back test failed\n"); in spi_nor_write_16bit_cr_and_check()
1084 * @nor: pointer to a 'struct spi_nor'.
1089 int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_sr_and_check() argument
1091 if (nor->flags & SNOR_F_HAS_16BIT_SR) in spi_nor_write_sr_and_check()
1092 return spi_nor_write_16bit_sr_and_check(nor, sr1); in spi_nor_write_sr_and_check()
1094 return spi_nor_write_sr1_and_check(nor, sr1); in spi_nor_write_sr_and_check()
1100 * @nor: pointer to 'struct spi_nor'.
1105 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2) in spi_nor_write_sr2() argument
1109 ret = spi_nor_write_enable(nor); in spi_nor_write_sr2()
1113 if (nor->spimem) { in spi_nor_write_sr2()
1120 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_sr2()
1122 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_sr2()
1124 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR2, in spi_nor_write_sr2()
1129 dev_dbg(nor->dev, "error %d writing SR2\n", ret); in spi_nor_write_sr2()
1133 return spi_nor_wait_till_ready(nor); in spi_nor_write_sr2()
1139 * @nor: pointer to 'struct spi_nor'.
1145 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2) in spi_nor_read_sr2() argument
1149 if (nor->spimem) { in spi_nor_read_sr2()
1156 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_sr2()
1158 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_sr2()
1160 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR2, sr2, in spi_nor_read_sr2()
1165 dev_dbg(nor->dev, "error %d reading SR2\n", ret); in spi_nor_read_sr2()
1172 * @nor: pointer to 'struct spi_nor'.
1176 static int spi_nor_erase_chip(struct spi_nor *nor) in spi_nor_erase_chip() argument
1180 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10)); in spi_nor_erase_chip()
1182 if (nor->spimem) { in spi_nor_erase_chip()
1189 spi_nor_spimem_setup_op(nor, &op, nor->write_proto); in spi_nor_erase_chip()
1191 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_erase_chip()
1193 ret = spi_nor_controller_ops_write_reg(nor, in spi_nor_erase_chip()
1199 dev_dbg(nor->dev, "error %d erasing chip\n", ret); in spi_nor_erase_chip()
1263 static bool spi_nor_has_uniform_erase(const struct spi_nor *nor) in spi_nor_has_uniform_erase() argument
1265 return !!nor->params->erase_map.uniform_erase_type; in spi_nor_has_uniform_erase()
1268 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor) in spi_nor_set_4byte_opcodes() argument
1270 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode); in spi_nor_set_4byte_opcodes()
1271 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode); in spi_nor_set_4byte_opcodes()
1272 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode); in spi_nor_set_4byte_opcodes()
1274 if (!spi_nor_has_uniform_erase(nor)) { in spi_nor_set_4byte_opcodes()
1275 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_set_4byte_opcodes()
1287 int spi_nor_lock_and_prep(struct spi_nor *nor) in spi_nor_lock_and_prep() argument
1291 mutex_lock(&nor->lock); in spi_nor_lock_and_prep()
1293 if (nor->controller_ops && nor->controller_ops->prepare) { in spi_nor_lock_and_prep()
1294 ret = nor->controller_ops->prepare(nor); in spi_nor_lock_and_prep()
1296 mutex_unlock(&nor->lock); in spi_nor_lock_and_prep()
1303 void spi_nor_unlock_and_unprep(struct spi_nor *nor) in spi_nor_unlock_and_unprep() argument
1305 if (nor->controller_ops && nor->controller_ops->unprepare) in spi_nor_unlock_and_unprep()
1306 nor->controller_ops->unprepare(nor); in spi_nor_unlock_and_unprep()
1307 mutex_unlock(&nor->lock); in spi_nor_unlock_and_unprep()
1310 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr) in spi_nor_convert_addr() argument
1312 if (!nor->params->convert_addr) in spi_nor_convert_addr()
1315 return nor->params->convert_addr(nor, addr); in spi_nor_convert_addr()
1321 int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) in spi_nor_erase_sector() argument
1325 addr = spi_nor_convert_addr(nor, addr); in spi_nor_erase_sector()
1327 if (nor->spimem) { in spi_nor_erase_sector()
1329 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0), in spi_nor_erase_sector()
1330 SPI_MEM_OP_ADDR(nor->addr_width, addr, 0), in spi_nor_erase_sector()
1334 spi_nor_spimem_setup_op(nor, &op, nor->write_proto); in spi_nor_erase_sector()
1336 return spi_mem_exec_op(nor->spimem, &op); in spi_nor_erase_sector()
1337 } else if (nor->controller_ops->erase) { in spi_nor_erase_sector()
1338 return spi_nor_controller_ops_erase(nor, addr); in spi_nor_erase_sector()
1345 for (i = nor->addr_width - 1; i >= 0; i--) { in spi_nor_erase_sector()
1346 nor->bouncebuf[i] = addr & 0xff; in spi_nor_erase_sector()
1350 return spi_nor_controller_ops_write_reg(nor, nor->erase_opcode, in spi_nor_erase_sector()
1351 nor->bouncebuf, nor->addr_width); in spi_nor_erase_sector()
1356 * @erase: pointer to a structure that describes a SPI NOR erase type
1376 * @map: the erase map of the SPI NOR
1377 * @region: pointer to a structure that describes a SPI NOR erase region
1432 * spi_nor_region_next() - get the next spi nor region
1433 * @region: pointer to a structure that describes a SPI NOR erase region
1435 * Return: the next spi nor region or NULL if last region.
1449 * @map: the erase map of the SPI NOR
1476 * @region: pointer to a structure that describes a SPI NOR erase region
1477 * @erase: pointer to a structure that describes a SPI NOR erase type
1520 * @nor: pointer to a 'struct spi_nor'
1531 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor, in spi_nor_init_erase_cmd_list() argument
1535 const struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_init_erase_cmd_list()
1589 * @nor: pointer to a 'struct spi_nor'
1598 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len) in spi_nor_erase_multi_sectors() argument
1604 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len); in spi_nor_erase_multi_sectors()
1609 nor->erase_opcode = cmd->opcode; in spi_nor_erase_multi_sectors()
1611 …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()
1614 ret = spi_nor_write_enable(nor); in spi_nor_erase_multi_sectors()
1618 ret = spi_nor_erase_sector(nor, addr); in spi_nor_erase_multi_sectors()
1622 ret = spi_nor_wait_till_ready(nor); in spi_nor_erase_multi_sectors()
1641 * Erase an address range on the nor chip. The address range may extend
1646 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_erase() local
1651 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr, in spi_nor_erase()
1654 if (spi_nor_has_uniform_erase(nor)) { in spi_nor_erase()
1663 ret = spi_nor_lock_and_prep(nor); in spi_nor_erase()
1668 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) { in spi_nor_erase()
1671 ret = spi_nor_write_enable(nor); in spi_nor_erase()
1675 ret = spi_nor_erase_chip(nor); in spi_nor_erase()
1688 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout); in spi_nor_erase()
1698 } else if (spi_nor_has_uniform_erase(nor)) { in spi_nor_erase()
1700 ret = spi_nor_write_enable(nor); in spi_nor_erase()
1704 ret = spi_nor_erase_sector(nor, addr); in spi_nor_erase()
1708 ret = spi_nor_wait_till_ready(nor); in spi_nor_erase()
1718 ret = spi_nor_erase_multi_sectors(nor, addr, len); in spi_nor_erase()
1723 ret = spi_nor_write_disable(nor); in spi_nor_erase()
1726 spi_nor_unlock_and_unprep(nor); in spi_nor_erase()
1734 * @nor: pointer to a 'struct spi_nor'
1740 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor) in spi_nor_sr1_bit6_quad_enable() argument
1744 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr1_bit6_quad_enable()
1748 if (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6) in spi_nor_sr1_bit6_quad_enable()
1751 nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6; in spi_nor_sr1_bit6_quad_enable()
1753 return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]); in spi_nor_sr1_bit6_quad_enable()
1759 * @nor: pointer to a 'struct spi_nor'.
1765 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor) in spi_nor_sr2_bit1_quad_enable() argument
1769 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_sr2_bit1_quad_enable()
1770 return spi_nor_write_16bit_cr_and_check(nor, SR2_QUAD_EN_BIT1); in spi_nor_sr2_bit1_quad_enable()
1772 ret = spi_nor_read_cr(nor, nor->bouncebuf); in spi_nor_sr2_bit1_quad_enable()
1776 if (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1) in spi_nor_sr2_bit1_quad_enable()
1779 nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1; in spi_nor_sr2_bit1_quad_enable()
1781 return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]); in spi_nor_sr2_bit1_quad_enable()
1786 * @nor: pointer to a 'struct spi_nor'
1796 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor) in spi_nor_sr2_bit7_quad_enable() argument
1798 u8 *sr2 = nor->bouncebuf; in spi_nor_sr2_bit7_quad_enable()
1803 ret = spi_nor_read_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1812 ret = spi_nor_write_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1819 ret = spi_nor_read_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1824 dev_dbg(nor->dev, "SR2: Read back test failed\n"); in spi_nor_sr2_bit7_quad_enable()
1866 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor) in spi_nor_read_id() argument
1869 u8 *id = nor->bouncebuf; in spi_nor_read_id()
1873 if (nor->spimem) { in spi_nor_read_id()
1880 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_id()
1882 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id, in spi_nor_read_id()
1886 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret); in spi_nor_read_id()
1895 nor->manufacturer = manufacturers[i]; in spi_nor_read_id()
1900 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n", in spi_nor_read_id()
1908 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_read() local
1911 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len); in spi_nor_read()
1913 ret = spi_nor_lock_and_prep(nor); in spi_nor_read()
1920 addr = spi_nor_convert_addr(nor, addr); in spi_nor_read()
1922 ret = spi_nor_read_data(nor, addr, len, buf); in spi_nor_read()
1940 spi_nor_unlock_and_unprep(nor); in spi_nor_read()
1945 * Write an address range to the nor chip. Data must be written in
1952 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_write() local
1956 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len); in spi_nor_write()
1958 ret = spi_nor_lock_and_prep(nor); in spi_nor_write()
1971 if (is_power_of_2(nor->page_size)) { in spi_nor_write()
1972 page_offset = addr & (nor->page_size - 1); in spi_nor_write()
1976 page_offset = do_div(aux, nor->page_size); in spi_nor_write()
1980 nor->page_size - page_offset, len - i); in spi_nor_write()
1982 addr = spi_nor_convert_addr(nor, addr); in spi_nor_write()
1984 ret = spi_nor_write_enable(nor); in spi_nor_write()
1988 ret = spi_nor_write_data(nor, addr, page_remain, buf + i); in spi_nor_write()
1993 ret = spi_nor_wait_till_ready(nor); in spi_nor_write()
2001 spi_nor_unlock_and_unprep(nor); in spi_nor_write()
2005 static int spi_nor_check(struct spi_nor *nor) in spi_nor_check() argument
2007 if (!nor->dev || in spi_nor_check()
2008 (!nor->spimem && !nor->controller_ops) || in spi_nor_check()
2009 (!nor->spimem && nor->controller_ops && in spi_nor_check()
2010 (!nor->controller_ops->read || in spi_nor_check()
2011 !nor->controller_ops->write || in spi_nor_check()
2012 !nor->controller_ops->read_reg || in spi_nor_check()
2013 !nor->controller_ops->write_reg))) { in spi_nor_check()
2014 pr_err("spi-nor: please fill all the necessary fields!\n"); in spi_nor_check()
2018 if (nor->spimem && nor->controller_ops) { in spi_nor_check()
2019 …dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one… in spi_nor_check()
2102 *@nor: pointer to a 'struct spi_nor'
2107 static int spi_nor_spimem_check_op(struct spi_nor *nor, in spi_nor_spimem_check_op() argument
2117 if (!spi_mem_supports_op(nor->spimem, op)) { in spi_nor_spimem_check_op()
2118 if (nor->mtd.size > SZ_16M) in spi_nor_spimem_check_op()
2123 if (!spi_mem_supports_op(nor->spimem, op)) in spi_nor_spimem_check_op()
2133 *@nor: pointer to a 'struct spi_nor'
2138 static int spi_nor_spimem_check_readop(struct spi_nor *nor, in spi_nor_spimem_check_readop() argument
2146 spi_nor_spimem_setup_op(nor, &op, read->proto); in spi_nor_spimem_check_readop()
2149 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; in spi_nor_spimem_check_readop()
2150 if (spi_nor_protocol_is_dtr(nor->read_proto)) in spi_nor_spimem_check_readop()
2153 return spi_nor_spimem_check_op(nor, &op); in spi_nor_spimem_check_readop()
2159 *@nor: pointer to a 'struct spi_nor'
2164 static int spi_nor_spimem_check_pp(struct spi_nor *nor, in spi_nor_spimem_check_pp() argument
2172 spi_nor_spimem_setup_op(nor, &op, pp->proto); in spi_nor_spimem_check_pp()
2174 return spi_nor_spimem_check_op(nor, &op); in spi_nor_spimem_check_pp()
2180 * @nor: pointer to a 'struct spi_nor'
2185 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps) in spi_nor_spimem_adjust_hwcaps() argument
2187 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_spimem_adjust_hwcaps()
2197 if (nor->flags & SNOR_F_BROKEN_RESET) in spi_nor_spimem_adjust_hwcaps()
2208 spi_nor_spimem_check_readop(nor, ¶ms->reads[rdidx])) in spi_nor_spimem_adjust_hwcaps()
2215 if (spi_nor_spimem_check_pp(nor, in spi_nor_spimem_adjust_hwcaps()
2222 * spi_nor_set_erase_type() - set a SPI NOR erase type
2223 * @erase: pointer to a structure that describes a SPI NOR erase type
2239 * @map: the erase map of the SPI NOR
2242 * @flash_size: the spi nor flash memory size
2255 int spi_nor_post_bfpt_fixups(struct spi_nor *nor, in spi_nor_post_bfpt_fixups() argument
2261 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_post_bfpt_fixups()
2262 nor->manufacturer->fixups->post_bfpt) { in spi_nor_post_bfpt_fixups()
2263 ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header, in spi_nor_post_bfpt_fixups()
2269 if (nor->info->fixups && nor->info->fixups->post_bfpt) in spi_nor_post_bfpt_fixups()
2270 return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt); in spi_nor_post_bfpt_fixups()
2275 static int spi_nor_select_read(struct spi_nor *nor, in spi_nor_select_read() argument
2288 read = &nor->params->reads[cmd]; in spi_nor_select_read()
2289 nor->read_opcode = read->opcode; in spi_nor_select_read()
2290 nor->read_proto = read->proto; in spi_nor_select_read()
2293 * In the SPI NOR framework, we don't need to make the difference in spi_nor_select_read()
2302 nor->read_dummy = read->num_mode_clocks + read->num_wait_states; in spi_nor_select_read()
2306 static int spi_nor_select_pp(struct spi_nor *nor, in spi_nor_select_pp() argument
2319 pp = &nor->params->page_programs[cmd]; in spi_nor_select_pp()
2320 nor->program_opcode = pp->opcode; in spi_nor_select_pp()
2321 nor->write_proto = pp->proto; in spi_nor_select_pp()
2327 * @map: the erase map of the SPI NOR
2378 static int spi_nor_select_erase(struct spi_nor *nor) in spi_nor_select_erase() argument
2380 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_select_erase()
2382 struct mtd_info *mtd = &nor->mtd; in spi_nor_select_erase()
2383 u32 wanted_size = nor->info->sector_size; in spi_nor_select_erase()
2399 if (spi_nor_has_uniform_erase(nor)) { in spi_nor_select_erase()
2403 nor->erase_opcode = erase->opcode; in spi_nor_select_erase()
2410 * maximum erase sector size. No need to set nor->erase_opcode. in spi_nor_select_erase()
2426 static int spi_nor_default_setup(struct spi_nor *nor, in spi_nor_default_setup() argument
2429 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_default_setup()
2439 if (nor->spimem) { in spi_nor_default_setup()
2445 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask); in spi_nor_default_setup()
2454 dev_dbg(nor->dev, in spi_nor_default_setup()
2461 err = spi_nor_select_read(nor, shared_mask); in spi_nor_default_setup()
2463 dev_dbg(nor->dev, in spi_nor_default_setup()
2469 err = spi_nor_select_pp(nor, shared_mask); in spi_nor_default_setup()
2471 dev_dbg(nor->dev, in spi_nor_default_setup()
2477 err = spi_nor_select_erase(nor); in spi_nor_default_setup()
2479 dev_dbg(nor->dev, in spi_nor_default_setup()
2487 static int spi_nor_setup(struct spi_nor *nor, in spi_nor_setup() argument
2490 if (!nor->params->setup) in spi_nor_setup()
2493 return nor->params->setup(nor, hwcaps); in spi_nor_setup()
2499 * @nor: pointer to a 'struct spi_nor'.
2501 static void spi_nor_manufacturer_init_params(struct spi_nor *nor) in spi_nor_manufacturer_init_params() argument
2503 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_manufacturer_init_params()
2504 nor->manufacturer->fixups->default_init) in spi_nor_manufacturer_init_params()
2505 nor->manufacturer->fixups->default_init(nor); in spi_nor_manufacturer_init_params()
2507 if (nor->info->fixups && nor->info->fixups->default_init) in spi_nor_manufacturer_init_params()
2508 nor->info->fixups->default_init(nor); in spi_nor_manufacturer_init_params()
2514 * @nor: pointer to a 'struct spi_nor'.
2519 static void spi_nor_sfdp_init_params(struct spi_nor *nor) in spi_nor_sfdp_init_params() argument
2523 memcpy(&sfdp_params, nor->params, sizeof(sfdp_params)); in spi_nor_sfdp_init_params()
2525 if (spi_nor_parse_sfdp(nor)) { in spi_nor_sfdp_init_params()
2526 memcpy(nor->params, &sfdp_params, sizeof(*nor->params)); in spi_nor_sfdp_init_params()
2527 nor->addr_width = 0; in spi_nor_sfdp_init_params()
2528 nor->flags &= ~SNOR_F_4B_OPCODES; in spi_nor_sfdp_init_params()
2534 * based on nor->info data.
2535 * @nor: pointer to a 'struct spi_nor'.
2537 static void spi_nor_info_init_params(struct spi_nor *nor) in spi_nor_info_init_params() argument
2539 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_info_init_params()
2541 const struct flash_info *info = nor->info; in spi_nor_info_init_params()
2542 struct device_node *np = spi_nor_get_flash_node(nor); in spi_nor_info_init_params()
2552 nor->flags |= SNOR_F_HAS_16BIT_SR; in spi_nor_info_init_params()
2554 /* Set SPI NOR sizes. */ in spi_nor_info_init_params()
2649 * @nor: pointer to a 'struct spi_nor'
2655 static void spi_nor_post_sfdp_fixups(struct spi_nor *nor) in spi_nor_post_sfdp_fixups() argument
2657 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_post_sfdp_fixups()
2658 nor->manufacturer->fixups->post_sfdp) in spi_nor_post_sfdp_fixups()
2659 nor->manufacturer->fixups->post_sfdp(nor); in spi_nor_post_sfdp_fixups()
2661 if (nor->info->fixups && nor->info->fixups->post_sfdp) in spi_nor_post_sfdp_fixups()
2662 nor->info->fixups->post_sfdp(nor); in spi_nor_post_sfdp_fixups()
2667 * @nor: pointer to a 'struct spi_nor'
2672 static void spi_nor_late_init_params(struct spi_nor *nor) in spi_nor_late_init_params() argument
2675 * NOR protection support. When locking_ops are not provided, we pick in spi_nor_late_init_params()
2678 if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops) in spi_nor_late_init_params()
2679 spi_nor_init_default_locking_ops(nor); in spi_nor_late_init_params()
2684 * @nor: pointer to a 'struct spi_nor'.
2690 * based on nor->info data:
2719 static int spi_nor_init_params(struct spi_nor *nor) in spi_nor_init_params() argument
2721 nor->params = devm_kzalloc(nor->dev, sizeof(*nor->params), GFP_KERNEL); in spi_nor_init_params()
2722 if (!nor->params) in spi_nor_init_params()
2725 spi_nor_info_init_params(nor); in spi_nor_init_params()
2727 spi_nor_manufacturer_init_params(nor); in spi_nor_init_params()
2729 if ((nor->info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | in spi_nor_init_params()
2731 !(nor->info->flags & SPI_NOR_SKIP_SFDP)) in spi_nor_init_params()
2732 spi_nor_sfdp_init_params(nor); in spi_nor_init_params()
2734 spi_nor_post_sfdp_fixups(nor); in spi_nor_init_params()
2736 spi_nor_late_init_params(nor); in spi_nor_init_params()
2742 * @nor: pointer to a 'struct spi_nor'
2747 static int spi_nor_octal_dtr_enable(struct spi_nor *nor, bool enable) in spi_nor_octal_dtr_enable() argument
2751 if (!nor->params->octal_dtr_enable) in spi_nor_octal_dtr_enable()
2754 if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR && in spi_nor_octal_dtr_enable()
2755 nor->write_proto == SNOR_PROTO_8_8_8_DTR)) in spi_nor_octal_dtr_enable()
2758 if (!(nor->flags & SNOR_F_IO_MODE_EN_VOLATILE)) in spi_nor_octal_dtr_enable()
2761 ret = nor->params->octal_dtr_enable(nor, enable); in spi_nor_octal_dtr_enable()
2766 nor->reg_proto = SNOR_PROTO_8_8_8_DTR; in spi_nor_octal_dtr_enable()
2768 nor->reg_proto = SNOR_PROTO_1_1_1; in spi_nor_octal_dtr_enable()
2775 * @nor: pointer to a 'struct spi_nor'
2779 static int spi_nor_quad_enable(struct spi_nor *nor) in spi_nor_quad_enable() argument
2781 if (!nor->params->quad_enable) in spi_nor_quad_enable()
2784 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 || in spi_nor_quad_enable()
2785 spi_nor_get_protocol_width(nor->write_proto) == 4)) in spi_nor_quad_enable()
2788 return nor->params->quad_enable(nor); in spi_nor_quad_enable()
2791 static int spi_nor_init(struct spi_nor *nor) in spi_nor_init() argument
2795 err = spi_nor_octal_dtr_enable(nor, true); in spi_nor_init()
2797 dev_dbg(nor->dev, "octal mode not supported\n"); in spi_nor_init()
2801 err = spi_nor_quad_enable(nor); in spi_nor_init()
2803 dev_dbg(nor->dev, "quad mode not supported\n"); in spi_nor_init()
2808 * Some SPI NOR flashes are write protected by default after a power-on in spi_nor_init()
2819 nor->flags & SNOR_F_SWP_IS_VOLATILE)) in spi_nor_init()
2820 spi_nor_try_unlock_all(nor); in spi_nor_init()
2822 if (nor->addr_width == 4 && in spi_nor_init()
2823 nor->read_proto != SNOR_PROTO_8_8_8_DTR && in spi_nor_init()
2824 !(nor->flags & SNOR_F_4B_OPCODES)) { in spi_nor_init()
2832 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET, in spi_nor_init()
2834 nor->params->set_4byte_addr_mode(nor, true); in spi_nor_init()
2842 * @nor: pointer to 'struct spi_nor'
2855 static void spi_nor_soft_reset(struct spi_nor *nor) in spi_nor_soft_reset() argument
2865 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_soft_reset()
2867 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_soft_reset()
2869 dev_warn(nor->dev, "Software reset failed: %d\n", ret); in spi_nor_soft_reset()
2878 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_soft_reset()
2880 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_soft_reset()
2882 dev_warn(nor->dev, "Software reset failed: %d\n", ret); in spi_nor_soft_reset()
2897 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_suspend() local
2901 ret = spi_nor_octal_dtr_enable(nor, false); in spi_nor_suspend()
2903 dev_err(nor->dev, "suspend() failed\n"); in spi_nor_suspend()
2911 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_resume() local
2912 struct device *dev = nor->dev; in spi_nor_resume()
2915 /* re-initialize the nor chip */ in spi_nor_resume()
2916 ret = spi_nor_init(nor); in spi_nor_resume()
2924 struct spi_nor *nor = mtd_to_spi_nor(master); in spi_nor_get_device() local
2927 if (nor->spimem) in spi_nor_get_device()
2928 dev = nor->spimem->spi->controller->dev.parent; in spi_nor_get_device()
2930 dev = nor->dev; in spi_nor_get_device()
2941 struct spi_nor *nor = mtd_to_spi_nor(master); in spi_nor_put_device() local
2944 if (nor->spimem) in spi_nor_put_device()
2945 dev = nor->spimem->spi->controller->dev.parent; in spi_nor_put_device()
2947 dev = nor->dev; in spi_nor_put_device()
2952 void spi_nor_restore(struct spi_nor *nor) in spi_nor_restore() argument
2955 if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES) && in spi_nor_restore()
2956 nor->flags & SNOR_F_BROKEN_RESET) in spi_nor_restore()
2957 nor->params->set_4byte_addr_mode(nor, false); in spi_nor_restore()
2959 if (nor->flags & SNOR_F_SOFT_RESET) in spi_nor_restore()
2960 spi_nor_soft_reset(nor); in spi_nor_restore()
2964 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor, in spi_nor_match_id() argument
2972 nor->manufacturer = manufacturers[i]; in spi_nor_match_id()
2981 static int spi_nor_set_addr_width(struct spi_nor *nor) in spi_nor_set_addr_width() argument
2983 if (nor->addr_width) { in spi_nor_set_addr_width()
2985 } else if (nor->read_proto == SNOR_PROTO_8_8_8_DTR) { in spi_nor_set_addr_width()
2998 nor->addr_width = 4; in spi_nor_set_addr_width()
2999 } else if (nor->info->addr_width) { in spi_nor_set_addr_width()
3000 nor->addr_width = nor->info->addr_width; in spi_nor_set_addr_width()
3002 nor->addr_width = 3; in spi_nor_set_addr_width()
3005 if (nor->addr_width == 3 && nor->mtd.size > 0x1000000) { in spi_nor_set_addr_width()
3007 nor->addr_width = 4; in spi_nor_set_addr_width()
3010 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) { in spi_nor_set_addr_width()
3011 dev_dbg(nor->dev, "address width is too large: %u\n", in spi_nor_set_addr_width()
3012 nor->addr_width); in spi_nor_set_addr_width()
3017 if (nor->addr_width == 4 && nor->flags & SNOR_F_4B_OPCODES && in spi_nor_set_addr_width()
3018 !(nor->flags & SNOR_F_HAS_4BAIT)) in spi_nor_set_addr_width()
3019 spi_nor_set_4byte_opcodes(nor); in spi_nor_set_addr_width()
3024 static void spi_nor_debugfs_init(struct spi_nor *nor, in spi_nor_debugfs_init() argument
3027 struct mtd_info *mtd = &nor->mtd; in spi_nor_debugfs_init()
3030 mtd->dbg.partid = devm_kasprintf(nor->dev, GFP_KERNEL, "spi-nor:%*phN", in spi_nor_debugfs_init()
3034 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor, in spi_nor_get_flash_info() argument
3040 info = spi_nor_match_id(nor, name); in spi_nor_get_flash_info()
3043 info = spi_nor_read_id(nor); in spi_nor_get_flash_info()
3054 jinfo = spi_nor_read_id(nor); in spi_nor_get_flash_info()
3065 dev_warn(nor->dev, "found %s, expected %s\n", in spi_nor_get_flash_info()
3074 int spi_nor_scan(struct spi_nor *nor, const char *name, in spi_nor_scan() argument
3078 struct device *dev = nor->dev; in spi_nor_scan()
3079 struct mtd_info *mtd = &nor->mtd; in spi_nor_scan()
3080 struct device_node *np = spi_nor_get_flash_node(nor); in spi_nor_scan()
3084 ret = spi_nor_check(nor); in spi_nor_scan()
3089 nor->reg_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
3090 nor->read_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
3091 nor->write_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
3097 * nor->page_size turns out to be greater than PAGE_SIZE (which in spi_nor_scan()
3098 * shouldn't happen before long since NOR pages are usually less in spi_nor_scan()
3101 nor->bouncebuf_size = PAGE_SIZE; in spi_nor_scan()
3102 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size, in spi_nor_scan()
3104 if (!nor->bouncebuf) in spi_nor_scan()
3107 info = spi_nor_get_flash_info(nor, name); in spi_nor_scan()
3111 nor->info = info; in spi_nor_scan()
3113 spi_nor_debugfs_init(nor, info); in spi_nor_scan()
3115 mutex_init(&nor->lock); in spi_nor_scan()
3120 * with Atmel SPI NOR. in spi_nor_scan()
3123 nor->flags |= SNOR_F_READY_XSR_RDY; in spi_nor_scan()
3126 nor->flags |= SNOR_F_HAS_LOCK; in spi_nor_scan()
3131 ret = spi_nor_init_params(nor); in spi_nor_scan()
3137 mtd->priv = nor; in spi_nor_scan()
3139 mtd->writesize = nor->params->writesize; in spi_nor_scan()
3141 mtd->size = nor->params->size; in spi_nor_scan()
3150 nor->flags |= SNOR_F_USE_FSR; in spi_nor_scan()
3152 nor->flags |= SNOR_F_HAS_SR_TB; in spi_nor_scan()
3154 nor->flags |= SNOR_F_HAS_SR_TB_BIT6; in spi_nor_scan()
3158 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE; in spi_nor_scan()
3160 nor->flags |= SNOR_F_USE_CLSR; in spi_nor_scan()
3162 nor->flags |= SNOR_F_SWP_IS_VOLATILE; in spi_nor_scan()
3165 nor->flags |= SNOR_F_HAS_4BIT_BP; in spi_nor_scan()
3167 nor->flags |= SNOR_F_HAS_SR_BP3_BIT6; in spi_nor_scan()
3174 nor->page_size = nor->params->page_size; in spi_nor_scan()
3175 mtd->writebufsize = nor->page_size; in spi_nor_scan()
3178 nor->flags |= SNOR_F_BROKEN_RESET; in spi_nor_scan()
3186 ret = spi_nor_setup(nor, hwcaps); in spi_nor_scan()
3191 nor->flags |= SNOR_F_4B_OPCODES; in spi_nor_scan()
3194 nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE; in spi_nor_scan()
3196 ret = spi_nor_set_addr_width(nor); in spi_nor_scan()
3200 spi_nor_register_locking_ops(nor); in spi_nor_scan()
3203 ret = spi_nor_init(nor); in spi_nor_scan()
3208 spi_nor_otp_init(nor); in spi_nor_scan()
3233 static int spi_nor_create_read_dirmap(struct spi_nor *nor) in spi_nor_create_read_dirmap() argument
3236 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0), in spi_nor_create_read_dirmap()
3237 SPI_MEM_OP_ADDR(nor->addr_width, 0, 0), in spi_nor_create_read_dirmap()
3238 SPI_MEM_OP_DUMMY(nor->read_dummy, 0), in spi_nor_create_read_dirmap()
3241 .length = nor->mtd.size, in spi_nor_create_read_dirmap()
3245 spi_nor_spimem_setup_op(nor, op, nor->read_proto); in spi_nor_create_read_dirmap()
3248 op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8; in spi_nor_create_read_dirmap()
3249 if (spi_nor_protocol_is_dtr(nor->read_proto)) in spi_nor_create_read_dirmap()
3257 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); in spi_nor_create_read_dirmap()
3259 nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, in spi_nor_create_read_dirmap()
3261 return PTR_ERR_OR_ZERO(nor->dirmap.rdesc); in spi_nor_create_read_dirmap()
3264 static int spi_nor_create_write_dirmap(struct spi_nor *nor) in spi_nor_create_write_dirmap() argument
3267 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0), in spi_nor_create_write_dirmap()
3268 SPI_MEM_OP_ADDR(nor->addr_width, 0, 0), in spi_nor_create_write_dirmap()
3272 .length = nor->mtd.size, in spi_nor_create_write_dirmap()
3276 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) in spi_nor_create_write_dirmap()
3279 spi_nor_spimem_setup_op(nor, op, nor->write_proto); in spi_nor_create_write_dirmap()
3286 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); in spi_nor_create_write_dirmap()
3288 nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, in spi_nor_create_write_dirmap()
3290 return PTR_ERR_OR_ZERO(nor->dirmap.wdesc); in spi_nor_create_write_dirmap()
3297 struct spi_nor *nor; in spi_nor_probe() local
3306 nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL); in spi_nor_probe()
3307 if (!nor) in spi_nor_probe()
3310 nor->spimem = spimem; in spi_nor_probe()
3311 nor->dev = &spi->dev; in spi_nor_probe()
3312 spi_nor_set_flash_node(nor, spi->dev.of_node); in spi_nor_probe()
3314 spi_mem_set_drvdata(spimem, nor); in spi_nor_probe()
3317 nor->mtd.name = data->name; in spi_nor_probe()
3319 if (!nor->mtd.name) in spi_nor_probe()
3320 nor->mtd.name = spi_mem_get_name(spimem); in spi_nor_probe()
3330 else if (!strcmp(spi->modalias, "spi-nor")) in spi_nor_probe()
3335 ret = spi_nor_scan(nor, flash_name, &hwcaps); in spi_nor_probe()
3342 * a NOR we don't end up with buffer overflows. in spi_nor_probe()
3344 if (nor->page_size > PAGE_SIZE) { in spi_nor_probe()
3345 nor->bouncebuf_size = nor->page_size; in spi_nor_probe()
3346 devm_kfree(nor->dev, nor->bouncebuf); in spi_nor_probe()
3347 nor->bouncebuf = devm_kmalloc(nor->dev, in spi_nor_probe()
3348 nor->bouncebuf_size, in spi_nor_probe()
3350 if (!nor->bouncebuf) in spi_nor_probe()
3354 ret = spi_nor_create_read_dirmap(nor); in spi_nor_probe()
3358 ret = spi_nor_create_write_dirmap(nor); in spi_nor_probe()
3362 return mtd_device_register(&nor->mtd, data ? data->parts : NULL, in spi_nor_probe()
3368 struct spi_nor *nor = spi_mem_get_drvdata(spimem); in spi_nor_remove() local
3370 spi_nor_restore(nor); in spi_nor_remove()
3373 return mtd_device_unregister(&nor->mtd); in spi_nor_remove()
3378 struct spi_nor *nor = spi_mem_get_drvdata(spimem); in spi_nor_shutdown() local
3380 spi_nor_restore(nor); in spi_nor_shutdown()
3389 * encourage new users to add support to the spi-nor library, and simply bind
3390 * against a generic string here (e.g., "jedec,spi-nor").
3392 * Many flash names are kept here in this list (as well as in spi-nor.c) to
3397 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
3401 {"spi-nor"},
3405 * them with "spi-nor" in platform data.
3410 * Entries that were used in DTs without "jedec,spi-nor" fallback and
3442 * Generic compatibility for SPI NOR that can be identified by the
3445 { .compatible = "jedec,spi-nor" },
3458 .name = "spi-nor",
3473 MODULE_DESCRIPTION("framework for SPI NOR");