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 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 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->reg_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_nbytes, 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_nbytes, 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()
313 * @nor: pointer to 'struct spi_nor'.
319 int spi_nor_read_any_reg(struct spi_nor *nor, struct spi_mem_op *op, in spi_nor_read_any_reg() argument
322 if (!nor->spimem) in spi_nor_read_any_reg()
325 spi_nor_spimem_setup_op(nor, op, proto); in spi_nor_read_any_reg()
326 return spi_nor_spimem_exec_op(nor, op); in spi_nor_read_any_reg()
332 * @nor: pointer to 'struct spi_nor'
341 int spi_nor_write_any_volatile_reg(struct spi_nor *nor, struct spi_mem_op *op, in spi_nor_write_any_volatile_reg() argument
346 if (!nor->spimem) in spi_nor_write_any_volatile_reg()
349 ret = spi_nor_write_enable(nor); in spi_nor_write_any_volatile_reg()
352 spi_nor_spimem_setup_op(nor, op, proto); in spi_nor_write_any_volatile_reg()
353 return spi_nor_spimem_exec_op(nor, op); in spi_nor_write_any_volatile_reg()
358 * @nor: pointer to 'struct spi_nor'.
362 int spi_nor_write_enable(struct spi_nor *nor) in spi_nor_write_enable() argument
366 if (nor->spimem) { in spi_nor_write_enable()
369 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_enable()
371 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_enable()
373 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WREN, in spi_nor_write_enable()
378 dev_dbg(nor->dev, "error %d on Write Enable\n", ret); in spi_nor_write_enable()
385 * @nor: pointer to 'struct spi_nor'.
389 int spi_nor_write_disable(struct spi_nor *nor) in spi_nor_write_disable() argument
393 if (nor->spimem) { in spi_nor_write_disable()
396 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_disable()
398 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_disable()
400 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRDI, in spi_nor_write_disable()
405 dev_dbg(nor->dev, "error %d on Write Disable\n", ret); in spi_nor_write_disable()
412 * @nor: pointer to 'struct spi_nor'.
423 int spi_nor_read_id(struct spi_nor *nor, u8 naddr, u8 ndummy, u8 *id, in spi_nor_read_id() argument
428 if (nor->spimem) { in spi_nor_read_id()
432 spi_nor_spimem_setup_op(nor, &op, proto); in spi_nor_read_id()
433 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_id()
435 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id, in spi_nor_read_id()
443 * @nor: pointer to 'struct spi_nor'.
449 int spi_nor_read_sr(struct spi_nor *nor, u8 *sr) in spi_nor_read_sr() argument
453 if (nor->spimem) { in spi_nor_read_sr()
456 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) { in spi_nor_read_sr()
457 op.addr.nbytes = nor->params->rdsr_addr_nbytes; in spi_nor_read_sr()
458 op.dummy.nbytes = nor->params->rdsr_dummy; in spi_nor_read_sr()
466 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_sr()
468 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_sr()
470 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR, sr, in spi_nor_read_sr()
475 dev_dbg(nor->dev, "error %d reading SR\n", ret); in spi_nor_read_sr()
483 * @nor: pointer to 'struct spi_nor'
489 int spi_nor_read_cr(struct spi_nor *nor, u8 *cr) in spi_nor_read_cr() argument
493 if (nor->spimem) { in spi_nor_read_cr()
496 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_cr()
498 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_cr()
500 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDCR, cr, in spi_nor_read_cr()
505 dev_dbg(nor->dev, "error %d reading CR\n", ret); in spi_nor_read_cr()
512 * @nor: pointer to 'struct spi_nor'.
518 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable) in spi_nor_set_4byte_addr_mode() argument
522 if (nor->spimem) { in spi_nor_set_4byte_addr_mode()
525 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_set_4byte_addr_mode()
527 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_set_4byte_addr_mode()
529 ret = spi_nor_controller_ops_write_reg(nor, in spi_nor_set_4byte_addr_mode()
536 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); in spi_nor_set_4byte_addr_mode()
544 * @nor: pointer to 'struct spi_nor'.
550 static int spansion_set_4byte_addr_mode(struct spi_nor *nor, bool enable) in spansion_set_4byte_addr_mode() argument
554 nor->bouncebuf[0] = enable << 7; in spansion_set_4byte_addr_mode()
556 if (nor->spimem) { in spansion_set_4byte_addr_mode()
557 struct spi_mem_op op = SPI_NOR_BRWR_OP(nor->bouncebuf); in spansion_set_4byte_addr_mode()
559 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spansion_set_4byte_addr_mode()
561 ret = spi_mem_exec_op(nor->spimem, &op); in spansion_set_4byte_addr_mode()
563 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_BRWR, in spansion_set_4byte_addr_mode()
564 nor->bouncebuf, 1); in spansion_set_4byte_addr_mode()
568 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); in spansion_set_4byte_addr_mode()
576 * @nor: pointer to 'struct spi_nor'.
580 int spi_nor_sr_ready(struct spi_nor *nor) in spi_nor_sr_ready() argument
584 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_ready()
588 return !(nor->bouncebuf[0] & SR_WIP); in spi_nor_sr_ready()
593 * @nor: pointer to 'struct spi_nor'.
597 static int spi_nor_ready(struct spi_nor *nor) in spi_nor_ready() argument
600 if (nor->params->ready) in spi_nor_ready()
601 return nor->params->ready(nor); in spi_nor_ready()
603 return spi_nor_sr_ready(nor); in spi_nor_ready()
609 * @nor: pointer to "struct spi_nor".
614 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor, in spi_nor_wait_till_ready_with_timeout() argument
626 ret = spi_nor_ready(nor); in spi_nor_wait_till_ready_with_timeout()
635 dev_dbg(nor->dev, "flash operation timed out\n"); in spi_nor_wait_till_ready_with_timeout()
643 * @nor: pointer to "struct spi_nor".
647 int spi_nor_wait_till_ready(struct spi_nor *nor) in spi_nor_wait_till_ready() argument
649 return spi_nor_wait_till_ready_with_timeout(nor, in spi_nor_wait_till_ready()
655 * @nor: pointer to 'struct spi_nor'.
659 int spi_nor_global_block_unlock(struct spi_nor *nor) in spi_nor_global_block_unlock() argument
663 ret = spi_nor_write_enable(nor); in spi_nor_global_block_unlock()
667 if (nor->spimem) { in spi_nor_global_block_unlock()
670 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_global_block_unlock()
672 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_global_block_unlock()
674 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_GBULK, in spi_nor_global_block_unlock()
679 dev_dbg(nor->dev, "error %d on Global Block Unlock\n", ret); in spi_nor_global_block_unlock()
683 return spi_nor_wait_till_ready(nor); in spi_nor_global_block_unlock()
688 * @nor: pointer to 'struct spi_nor'.
694 int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len) in spi_nor_write_sr() argument
698 ret = spi_nor_write_enable(nor); in spi_nor_write_sr()
702 if (nor->spimem) { in spi_nor_write_sr()
705 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_sr()
707 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_sr()
709 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR, sr, in spi_nor_write_sr()
714 dev_dbg(nor->dev, "error %d writing SR\n", ret); in spi_nor_write_sr()
718 return spi_nor_wait_till_ready(nor); in spi_nor_write_sr()
724 * @nor: pointer to a 'struct spi_nor'.
729 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_sr1_and_check() argument
733 nor->bouncebuf[0] = sr1; in spi_nor_write_sr1_and_check()
735 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1); in spi_nor_write_sr1_and_check()
739 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_write_sr1_and_check()
743 if (nor->bouncebuf[0] != sr1) { in spi_nor_write_sr1_and_check()
744 dev_dbg(nor->dev, "SR1: read back test failed\n"); in spi_nor_write_sr1_and_check()
756 * @nor: pointer to a 'struct spi_nor'.
761 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_16bit_sr_and_check() argument
764 u8 *sr_cr = nor->bouncebuf; in spi_nor_write_16bit_sr_and_check()
768 if (!(nor->flags & SNOR_F_NO_READ_CR)) { in spi_nor_write_16bit_sr_and_check()
769 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_sr_and_check()
772 } else if (nor->params->quad_enable) { in spi_nor_write_16bit_sr_and_check()
780 * nor->params->quad_enable() call. in spi_nor_write_16bit_sr_and_check()
795 ret = spi_nor_write_sr(nor, sr_cr, 2); in spi_nor_write_16bit_sr_and_check()
799 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_sr_and_check()
804 dev_dbg(nor->dev, "SR: Read back test failed\n"); in spi_nor_write_16bit_sr_and_check()
808 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_write_16bit_sr_and_check()
813 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_sr_and_check()
818 dev_dbg(nor->dev, "CR: read back test failed\n"); in spi_nor_write_16bit_sr_and_check()
830 * @nor: pointer to a 'struct spi_nor'.
835 int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr) in spi_nor_write_16bit_cr_and_check() argument
838 u8 *sr_cr = nor->bouncebuf; in spi_nor_write_16bit_cr_and_check()
842 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_cr_and_check()
848 ret = spi_nor_write_sr(nor, sr_cr, 2); in spi_nor_write_16bit_cr_and_check()
854 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_cr_and_check()
859 dev_dbg(nor->dev, "SR: Read back test failed\n"); in spi_nor_write_16bit_cr_and_check()
863 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_write_16bit_cr_and_check()
866 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_cr_and_check()
871 dev_dbg(nor->dev, "CR: read back test failed\n"); in spi_nor_write_16bit_cr_and_check()
882 * @nor: pointer to a 'struct spi_nor'.
887 int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_sr_and_check() argument
889 if (nor->flags & SNOR_F_HAS_16BIT_SR) in spi_nor_write_sr_and_check()
890 return spi_nor_write_16bit_sr_and_check(nor, sr1); in spi_nor_write_sr_and_check()
892 return spi_nor_write_sr1_and_check(nor, sr1); in spi_nor_write_sr_and_check()
898 * @nor: pointer to 'struct spi_nor'.
903 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2) in spi_nor_write_sr2() argument
907 ret = spi_nor_write_enable(nor); in spi_nor_write_sr2()
911 if (nor->spimem) { in spi_nor_write_sr2()
914 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_write_sr2()
916 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_sr2()
918 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRSR2, in spi_nor_write_sr2()
923 dev_dbg(nor->dev, "error %d writing SR2\n", ret); in spi_nor_write_sr2()
927 return spi_nor_wait_till_ready(nor); in spi_nor_write_sr2()
933 * @nor: pointer to 'struct spi_nor'.
939 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2) in spi_nor_read_sr2() argument
943 if (nor->spimem) { in spi_nor_read_sr2()
946 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_read_sr2()
948 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_sr2()
950 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR2, sr2, in spi_nor_read_sr2()
955 dev_dbg(nor->dev, "error %d reading SR2\n", ret); in spi_nor_read_sr2()
962 * @nor: pointer to 'struct spi_nor'.
966 static int spi_nor_erase_chip(struct spi_nor *nor) in spi_nor_erase_chip() argument
970 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10)); in spi_nor_erase_chip()
972 if (nor->spimem) { in spi_nor_erase_chip()
975 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_erase_chip()
977 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_erase_chip()
979 ret = spi_nor_controller_ops_write_reg(nor, in spi_nor_erase_chip()
985 dev_dbg(nor->dev, "error %d erasing chip\n", ret); in spi_nor_erase_chip()
1049 static bool spi_nor_has_uniform_erase(const struct spi_nor *nor) in spi_nor_has_uniform_erase() argument
1051 return !!nor->params->erase_map.uniform_erase_type; in spi_nor_has_uniform_erase()
1054 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor) in spi_nor_set_4byte_opcodes() argument
1056 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode); in spi_nor_set_4byte_opcodes()
1057 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode); in spi_nor_set_4byte_opcodes()
1058 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode); in spi_nor_set_4byte_opcodes()
1060 if (!spi_nor_has_uniform_erase(nor)) { in spi_nor_set_4byte_opcodes()
1061 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_set_4byte_opcodes()
1073 int spi_nor_lock_and_prep(struct spi_nor *nor) in spi_nor_lock_and_prep() argument
1077 mutex_lock(&nor->lock); in spi_nor_lock_and_prep()
1079 if (nor->controller_ops && nor->controller_ops->prepare) { in spi_nor_lock_and_prep()
1080 ret = nor->controller_ops->prepare(nor); in spi_nor_lock_and_prep()
1082 mutex_unlock(&nor->lock); in spi_nor_lock_and_prep()
1089 void spi_nor_unlock_and_unprep(struct spi_nor *nor) in spi_nor_unlock_and_unprep() argument
1091 if (nor->controller_ops && nor->controller_ops->unprepare) in spi_nor_unlock_and_unprep()
1092 nor->controller_ops->unprepare(nor); in spi_nor_unlock_and_unprep()
1093 mutex_unlock(&nor->lock); in spi_nor_unlock_and_unprep()
1096 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr) in spi_nor_convert_addr() argument
1098 if (!nor->params->convert_addr) in spi_nor_convert_addr()
1101 return nor->params->convert_addr(nor, addr); in spi_nor_convert_addr()
1107 int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) in spi_nor_erase_sector() argument
1111 addr = spi_nor_convert_addr(nor, addr); in spi_nor_erase_sector()
1113 if (nor->spimem) { in spi_nor_erase_sector()
1115 SPI_NOR_SECTOR_ERASE_OP(nor->erase_opcode, in spi_nor_erase_sector()
1116 nor->addr_nbytes, addr); in spi_nor_erase_sector()
1118 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_erase_sector()
1120 return spi_mem_exec_op(nor->spimem, &op); in spi_nor_erase_sector()
1121 } else if (nor->controller_ops->erase) { in spi_nor_erase_sector()
1122 return spi_nor_controller_ops_erase(nor, addr); in spi_nor_erase_sector()
1129 for (i = nor->addr_nbytes - 1; i >= 0; i--) { in spi_nor_erase_sector()
1130 nor->bouncebuf[i] = addr & 0xff; in spi_nor_erase_sector()
1134 return spi_nor_controller_ops_write_reg(nor, nor->erase_opcode, in spi_nor_erase_sector()
1135 nor->bouncebuf, nor->addr_nbytes); in spi_nor_erase_sector()
1140 * @erase: pointer to a structure that describes a SPI NOR erase type
1160 * @map: the erase map of the SPI NOR
1161 * @region: pointer to a structure that describes a SPI NOR erase region
1216 * spi_nor_region_next() - get the next spi nor region
1217 * @region: pointer to a structure that describes a SPI NOR erase region
1219 * Return: the next spi nor region or NULL if last region.
1233 * @map: the erase map of the SPI NOR
1260 * @region: pointer to a structure that describes a SPI NOR erase region
1261 * @erase: pointer to a structure that describes a SPI NOR erase type
1304 * @nor: pointer to a 'struct spi_nor'
1315 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor, in spi_nor_init_erase_cmd_list() argument
1319 const struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_init_erase_cmd_list()
1373 * @nor: pointer to a 'struct spi_nor'
1382 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len) in spi_nor_erase_multi_sectors() argument
1388 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len); in spi_nor_erase_multi_sectors()
1393 nor->erase_opcode = cmd->opcode; in spi_nor_erase_multi_sectors()
1395 …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()
1398 ret = spi_nor_write_enable(nor); in spi_nor_erase_multi_sectors()
1402 ret = spi_nor_erase_sector(nor, addr); in spi_nor_erase_multi_sectors()
1406 ret = spi_nor_wait_till_ready(nor); in spi_nor_erase_multi_sectors()
1425 * Erase an address range on the nor chip. The address range may extend
1430 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_erase() local
1435 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr, in spi_nor_erase()
1438 if (spi_nor_has_uniform_erase(nor)) { in spi_nor_erase()
1447 ret = spi_nor_lock_and_prep(nor); in spi_nor_erase()
1452 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) { in spi_nor_erase()
1455 ret = spi_nor_write_enable(nor); in spi_nor_erase()
1459 ret = spi_nor_erase_chip(nor); in spi_nor_erase()
1472 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout); in spi_nor_erase()
1482 } else if (spi_nor_has_uniform_erase(nor)) { in spi_nor_erase()
1484 ret = spi_nor_write_enable(nor); in spi_nor_erase()
1488 ret = spi_nor_erase_sector(nor, addr); in spi_nor_erase()
1492 ret = spi_nor_wait_till_ready(nor); in spi_nor_erase()
1502 ret = spi_nor_erase_multi_sectors(nor, addr, len); in spi_nor_erase()
1507 ret = spi_nor_write_disable(nor); in spi_nor_erase()
1510 spi_nor_unlock_and_unprep(nor); in spi_nor_erase()
1518 * @nor: pointer to a 'struct spi_nor'
1524 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor) in spi_nor_sr1_bit6_quad_enable() argument
1528 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr1_bit6_quad_enable()
1532 if (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6) in spi_nor_sr1_bit6_quad_enable()
1535 nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6; in spi_nor_sr1_bit6_quad_enable()
1537 return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]); in spi_nor_sr1_bit6_quad_enable()
1543 * @nor: pointer to a 'struct spi_nor'.
1549 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor) in spi_nor_sr2_bit1_quad_enable() argument
1553 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_sr2_bit1_quad_enable()
1554 return spi_nor_write_16bit_cr_and_check(nor, SR2_QUAD_EN_BIT1); in spi_nor_sr2_bit1_quad_enable()
1556 ret = spi_nor_read_cr(nor, nor->bouncebuf); in spi_nor_sr2_bit1_quad_enable()
1560 if (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1) in spi_nor_sr2_bit1_quad_enable()
1563 nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1; in spi_nor_sr2_bit1_quad_enable()
1565 return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]); in spi_nor_sr2_bit1_quad_enable()
1570 * @nor: pointer to a 'struct spi_nor'
1580 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor) in spi_nor_sr2_bit7_quad_enable() argument
1582 u8 *sr2 = nor->bouncebuf; in spi_nor_sr2_bit7_quad_enable()
1587 ret = spi_nor_read_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1596 ret = spi_nor_write_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1603 ret = spi_nor_read_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1608 dev_dbg(nor->dev, "SR2: Read back test failed\n"); in spi_nor_sr2_bit7_quad_enable()
1635 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor, in spi_nor_match_id() argument
1646 nor->manufacturer = manufacturers[i]; in spi_nor_match_id()
1655 static const struct flash_info *spi_nor_detect(struct spi_nor *nor) in spi_nor_detect() argument
1658 u8 *id = nor->bouncebuf; in spi_nor_detect()
1661 ret = spi_nor_read_id(nor, 0, 0, id, nor->reg_proto); in spi_nor_detect()
1663 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret); in spi_nor_detect()
1667 info = spi_nor_match_id(nor, id); in spi_nor_detect()
1669 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n", in spi_nor_detect()
1679 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_read() local
1682 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len); in spi_nor_read()
1684 ret = spi_nor_lock_and_prep(nor); in spi_nor_read()
1691 addr = spi_nor_convert_addr(nor, addr); in spi_nor_read()
1693 ret = spi_nor_read_data(nor, addr, len, buf); in spi_nor_read()
1711 spi_nor_unlock_and_unprep(nor); in spi_nor_read()
1716 * Write an address range to the nor chip. Data must be written in
1723 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_write() local
1726 u32 page_size = nor->params->page_size; in spi_nor_write()
1728 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len); in spi_nor_write()
1730 ret = spi_nor_lock_and_prep(nor); in spi_nor_write()
1753 addr = spi_nor_convert_addr(nor, addr); in spi_nor_write()
1755 ret = spi_nor_write_enable(nor); in spi_nor_write()
1759 ret = spi_nor_write_data(nor, addr, page_remain, buf + i); in spi_nor_write()
1764 ret = spi_nor_wait_till_ready(nor); in spi_nor_write()
1772 spi_nor_unlock_and_unprep(nor); in spi_nor_write()
1776 static int spi_nor_check(struct spi_nor *nor) in spi_nor_check() argument
1778 if (!nor->dev || in spi_nor_check()
1779 (!nor->spimem && !nor->controller_ops) || in spi_nor_check()
1780 (!nor->spimem && nor->controller_ops && in spi_nor_check()
1781 (!nor->controller_ops->read || in spi_nor_check()
1782 !nor->controller_ops->write || in spi_nor_check()
1783 !nor->controller_ops->read_reg || in spi_nor_check()
1784 !nor->controller_ops->write_reg))) { in spi_nor_check()
1785 pr_err("spi-nor: please fill all the necessary fields!\n"); in spi_nor_check()
1789 if (nor->spimem && nor->controller_ops) { in spi_nor_check()
1790 …dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one… in spi_nor_check()
1873 *@nor: pointer to a 'struct spi_nor'
1878 static int spi_nor_spimem_check_op(struct spi_nor *nor, in spi_nor_spimem_check_op() argument
1888 if (!spi_mem_supports_op(nor->spimem, op)) { in spi_nor_spimem_check_op()
1889 if (nor->params->size > SZ_16M) in spi_nor_spimem_check_op()
1894 if (!spi_mem_supports_op(nor->spimem, op)) in spi_nor_spimem_check_op()
1904 *@nor: pointer to a 'struct spi_nor'
1909 static int spi_nor_spimem_check_readop(struct spi_nor *nor, in spi_nor_spimem_check_readop() argument
1914 spi_nor_spimem_setup_op(nor, &op, read->proto); in spi_nor_spimem_check_readop()
1917 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; in spi_nor_spimem_check_readop()
1918 if (spi_nor_protocol_is_dtr(nor->read_proto)) in spi_nor_spimem_check_readop()
1921 return spi_nor_spimem_check_op(nor, &op); in spi_nor_spimem_check_readop()
1927 *@nor: pointer to a 'struct spi_nor'
1932 static int spi_nor_spimem_check_pp(struct spi_nor *nor, in spi_nor_spimem_check_pp() argument
1937 spi_nor_spimem_setup_op(nor, &op, pp->proto); in spi_nor_spimem_check_pp()
1939 return spi_nor_spimem_check_op(nor, &op); in spi_nor_spimem_check_pp()
1945 * @nor: pointer to a 'struct spi_nor'
1950 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps) in spi_nor_spimem_adjust_hwcaps() argument
1952 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_spimem_adjust_hwcaps()
1962 if (nor->flags & SNOR_F_BROKEN_RESET) in spi_nor_spimem_adjust_hwcaps()
1973 spi_nor_spimem_check_readop(nor, &params->reads[rdidx])) in spi_nor_spimem_adjust_hwcaps()
1980 if (spi_nor_spimem_check_pp(nor, in spi_nor_spimem_adjust_hwcaps()
1987 * spi_nor_set_erase_type() - set a SPI NOR erase type
1988 * @erase: pointer to a structure that describes a SPI NOR erase type
2004 * @map: the erase map of the SPI NOR
2007 * @flash_size: the spi nor flash memory size
2020 int spi_nor_post_bfpt_fixups(struct spi_nor *nor, in spi_nor_post_bfpt_fixups() argument
2026 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_post_bfpt_fixups()
2027 nor->manufacturer->fixups->post_bfpt) { in spi_nor_post_bfpt_fixups()
2028 ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header, in spi_nor_post_bfpt_fixups()
2034 if (nor->info->fixups && nor->info->fixups->post_bfpt) in spi_nor_post_bfpt_fixups()
2035 return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt); in spi_nor_post_bfpt_fixups()
2040 static int spi_nor_select_read(struct spi_nor *nor, in spi_nor_select_read() argument
2053 read = &nor->params->reads[cmd]; in spi_nor_select_read()
2054 nor->read_opcode = read->opcode; in spi_nor_select_read()
2055 nor->read_proto = read->proto; in spi_nor_select_read()
2058 * In the SPI NOR framework, we don't need to make the difference in spi_nor_select_read()
2067 nor->read_dummy = read->num_mode_clocks + read->num_wait_states; in spi_nor_select_read()
2071 static int spi_nor_select_pp(struct spi_nor *nor, in spi_nor_select_pp() argument
2084 pp = &nor->params->page_programs[cmd]; in spi_nor_select_pp()
2085 nor->program_opcode = pp->opcode; in spi_nor_select_pp()
2086 nor->write_proto = pp->proto; in spi_nor_select_pp()
2092 * @map: the erase map of the SPI NOR
2143 static int spi_nor_select_erase(struct spi_nor *nor) in spi_nor_select_erase() argument
2145 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_select_erase()
2147 struct mtd_info *mtd = &nor->mtd; in spi_nor_select_erase()
2148 u32 wanted_size = nor->info->sector_size; in spi_nor_select_erase()
2164 if (spi_nor_has_uniform_erase(nor)) { in spi_nor_select_erase()
2168 nor->erase_opcode = erase->opcode; in spi_nor_select_erase()
2175 * maximum erase sector size. No need to set nor->erase_opcode. in spi_nor_select_erase()
2191 static int spi_nor_default_setup(struct spi_nor *nor, in spi_nor_default_setup() argument
2194 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_default_setup()
2204 if (nor->spimem) { in spi_nor_default_setup()
2210 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask); in spi_nor_default_setup()
2219 dev_dbg(nor->dev, in spi_nor_default_setup()
2226 err = spi_nor_select_read(nor, shared_mask); in spi_nor_default_setup()
2228 dev_dbg(nor->dev, in spi_nor_default_setup()
2234 err = spi_nor_select_pp(nor, shared_mask); in spi_nor_default_setup()
2236 dev_dbg(nor->dev, in spi_nor_default_setup()
2242 err = spi_nor_select_erase(nor); in spi_nor_default_setup()
2244 dev_dbg(nor->dev, in spi_nor_default_setup()
2252 static int spi_nor_set_addr_nbytes(struct spi_nor *nor) in spi_nor_set_addr_nbytes() argument
2254 if (nor->params->addr_nbytes) { in spi_nor_set_addr_nbytes()
2255 nor->addr_nbytes = nor->params->addr_nbytes; in spi_nor_set_addr_nbytes()
2256 } else if (nor->read_proto == SNOR_PROTO_8_8_8_DTR) { in spi_nor_set_addr_nbytes()
2269 nor->addr_nbytes = 4; in spi_nor_set_addr_nbytes()
2270 } else if (nor->info->addr_nbytes) { in spi_nor_set_addr_nbytes()
2271 nor->addr_nbytes = nor->info->addr_nbytes; in spi_nor_set_addr_nbytes()
2273 nor->addr_nbytes = 3; in spi_nor_set_addr_nbytes()
2276 if (nor->addr_nbytes == 3 && nor->params->size > 0x1000000) { in spi_nor_set_addr_nbytes()
2278 nor->addr_nbytes = 4; in spi_nor_set_addr_nbytes()
2281 if (nor->addr_nbytes > SPI_NOR_MAX_ADDR_NBYTES) { in spi_nor_set_addr_nbytes()
2282 dev_dbg(nor->dev, "The number of address bytes is too large: %u\n", in spi_nor_set_addr_nbytes()
2283 nor->addr_nbytes); in spi_nor_set_addr_nbytes()
2288 if (nor->addr_nbytes == 4 && nor->flags & SNOR_F_4B_OPCODES && in spi_nor_set_addr_nbytes()
2289 !(nor->flags & SNOR_F_HAS_4BAIT)) in spi_nor_set_addr_nbytes()
2290 spi_nor_set_4byte_opcodes(nor); in spi_nor_set_addr_nbytes()
2295 static int spi_nor_setup(struct spi_nor *nor, in spi_nor_setup() argument
2300 if (nor->params->setup) in spi_nor_setup()
2301 ret = nor->params->setup(nor, hwcaps); in spi_nor_setup()
2303 ret = spi_nor_default_setup(nor, hwcaps); in spi_nor_setup()
2307 return spi_nor_set_addr_nbytes(nor); in spi_nor_setup()
2313 * @nor: pointer to a 'struct spi_nor'.
2315 static void spi_nor_manufacturer_init_params(struct spi_nor *nor) in spi_nor_manufacturer_init_params() argument
2317 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_manufacturer_init_params()
2318 nor->manufacturer->fixups->default_init) in spi_nor_manufacturer_init_params()
2319 nor->manufacturer->fixups->default_init(nor); in spi_nor_manufacturer_init_params()
2321 if (nor->info->fixups && nor->info->fixups->default_init) in spi_nor_manufacturer_init_params()
2322 nor->info->fixups->default_init(nor); in spi_nor_manufacturer_init_params()
2327 * settings based on nor->info->sfdp_flags. This method should be called only by
2332 * @nor: pointer to a 'struct spi_nor'.
2334 static void spi_nor_no_sfdp_init_params(struct spi_nor *nor) in spi_nor_no_sfdp_init_params() argument
2336 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_no_sfdp_init_params()
2338 const u8 no_sfdp_flags = nor->info->no_sfdp_flags; in spi_nor_no_sfdp_init_params()
2392 spi_nor_set_erase_type(&map->erase_type[i], nor->info->sector_size, in spi_nor_no_sfdp_init_params()
2398 * spi_nor_init_flags() - Initialize NOR flags for settings that are not defined
2400 * @nor: pointer to a 'struct spi_nor'
2402 static void spi_nor_init_flags(struct spi_nor *nor) in spi_nor_init_flags() argument
2404 struct device_node *np = spi_nor_get_flash_node(nor); in spi_nor_init_flags()
2405 const u16 flags = nor->info->flags; in spi_nor_init_flags()
2408 nor->flags |= SNOR_F_BROKEN_RESET; in spi_nor_init_flags()
2411 nor->flags |= SNOR_F_SWP_IS_VOLATILE; in spi_nor_init_flags()
2414 nor->flags |= SNOR_F_HAS_LOCK; in spi_nor_init_flags()
2417 nor->flags |= SNOR_F_HAS_SR_TB; in spi_nor_init_flags()
2419 nor->flags |= SNOR_F_HAS_SR_TB_BIT6; in spi_nor_init_flags()
2423 nor->flags |= SNOR_F_HAS_4BIT_BP; in spi_nor_init_flags()
2425 nor->flags |= SNOR_F_HAS_SR_BP3_BIT6; in spi_nor_init_flags()
2429 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE; in spi_nor_init_flags()
2433 * spi_nor_init_fixup_flags() - Initialize NOR flags for settings that can not
2438 * @nor: pointer to a 'struct spi_nor'
2440 static void spi_nor_init_fixup_flags(struct spi_nor *nor) in spi_nor_init_fixup_flags() argument
2442 const u8 fixup_flags = nor->info->fixup_flags; in spi_nor_init_fixup_flags()
2445 nor->flags |= SNOR_F_4B_OPCODES; in spi_nor_init_fixup_flags()
2448 nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE; in spi_nor_init_fixup_flags()
2453 * @nor: pointer to a 'struct spi_nor'
2459 static void spi_nor_late_init_params(struct spi_nor *nor) in spi_nor_late_init_params() argument
2461 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_late_init_params()
2462 nor->manufacturer->fixups->late_init) in spi_nor_late_init_params()
2463 nor->manufacturer->fixups->late_init(nor); in spi_nor_late_init_params()
2465 if (nor->info->fixups && nor->info->fixups->late_init) in spi_nor_late_init_params()
2466 nor->info->fixups->late_init(nor); in spi_nor_late_init_params()
2468 spi_nor_init_flags(nor); in spi_nor_late_init_params()
2469 spi_nor_init_fixup_flags(nor); in spi_nor_late_init_params()
2472 * NOR protection support. When locking_ops are not provided, we pick in spi_nor_late_init_params()
2475 if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops) in spi_nor_late_init_params()
2476 spi_nor_init_default_locking_ops(nor); in spi_nor_late_init_params()
2482 * @nor: pointer to a 'struct spi_nor'.
2487 static void spi_nor_sfdp_init_params_deprecated(struct spi_nor *nor) in spi_nor_sfdp_init_params_deprecated() argument
2491 memcpy(&sfdp_params, nor->params, sizeof(sfdp_params)); in spi_nor_sfdp_init_params_deprecated()
2493 if (spi_nor_parse_sfdp(nor)) { in spi_nor_sfdp_init_params_deprecated()
2494 memcpy(nor->params, &sfdp_params, sizeof(*nor->params)); in spi_nor_sfdp_init_params_deprecated()
2495 nor->flags &= ~SNOR_F_4B_OPCODES; in spi_nor_sfdp_init_params_deprecated()
2502 * @nor: pointer to a 'struct spi_nor'.
2508 static void spi_nor_init_params_deprecated(struct spi_nor *nor) in spi_nor_init_params_deprecated() argument
2510 spi_nor_no_sfdp_init_params(nor); in spi_nor_init_params_deprecated()
2512 spi_nor_manufacturer_init_params(nor); in spi_nor_init_params_deprecated()
2514 if (nor->info->no_sfdp_flags & (SPI_NOR_DUAL_READ | in spi_nor_init_params_deprecated()
2518 spi_nor_sfdp_init_params_deprecated(nor); in spi_nor_init_params_deprecated()
2525 * @nor: pointer to a 'struct spi_nor'.
2527 static void spi_nor_init_default_params(struct spi_nor *nor) in spi_nor_init_default_params() argument
2529 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_init_default_params()
2530 const struct flash_info *info = nor->info; in spi_nor_init_default_params()
2531 struct device_node *np = spi_nor_get_flash_node(nor); in spi_nor_init_default_params()
2538 nor->flags |= SNOR_F_HAS_16BIT_SR; in spi_nor_init_default_params()
2540 /* Set SPI NOR sizes. */ in spi_nor_init_default_params()
2572 * @nor: pointer to a 'struct spi_nor'.
2578 * based on nor->info data:
2607 static int spi_nor_init_params(struct spi_nor *nor) in spi_nor_init_params() argument
2611 nor->params = devm_kzalloc(nor->dev, sizeof(*nor->params), GFP_KERNEL); in spi_nor_init_params()
2612 if (!nor->params) in spi_nor_init_params()
2615 spi_nor_init_default_params(nor); in spi_nor_init_params()
2617 if (nor->info->parse_sfdp) { in spi_nor_init_params()
2618 ret = spi_nor_parse_sfdp(nor); in spi_nor_init_params()
2620 …dev_err(nor->dev, "BFPT parsing failed. Please consider using SPI_NOR_SKIP_SFDP when declaring the… in spi_nor_init_params()
2623 } else if (nor->info->no_sfdp_flags & SPI_NOR_SKIP_SFDP) { in spi_nor_init_params()
2624 spi_nor_no_sfdp_init_params(nor); in spi_nor_init_params()
2626 spi_nor_init_params_deprecated(nor); in spi_nor_init_params()
2629 spi_nor_late_init_params(nor); in spi_nor_init_params()
2635 * @nor: pointer to a 'struct spi_nor'
2640 static int spi_nor_octal_dtr_enable(struct spi_nor *nor, bool enable) in spi_nor_octal_dtr_enable() argument
2644 if (!nor->params->octal_dtr_enable) in spi_nor_octal_dtr_enable()
2647 if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR && in spi_nor_octal_dtr_enable()
2648 nor->write_proto == SNOR_PROTO_8_8_8_DTR)) in spi_nor_octal_dtr_enable()
2651 if (!(nor->flags & SNOR_F_IO_MODE_EN_VOLATILE)) in spi_nor_octal_dtr_enable()
2654 ret = nor->params->octal_dtr_enable(nor, enable); in spi_nor_octal_dtr_enable()
2659 nor->reg_proto = SNOR_PROTO_8_8_8_DTR; in spi_nor_octal_dtr_enable()
2661 nor->reg_proto = SNOR_PROTO_1_1_1; in spi_nor_octal_dtr_enable()
2668 * @nor: pointer to a 'struct spi_nor'
2672 static int spi_nor_quad_enable(struct spi_nor *nor) in spi_nor_quad_enable() argument
2674 if (!nor->params->quad_enable) in spi_nor_quad_enable()
2677 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 || in spi_nor_quad_enable()
2678 spi_nor_get_protocol_width(nor->write_proto) == 4)) in spi_nor_quad_enable()
2681 return nor->params->quad_enable(nor); in spi_nor_quad_enable()
2684 static int spi_nor_init(struct spi_nor *nor) in spi_nor_init() argument
2688 err = spi_nor_octal_dtr_enable(nor, true); in spi_nor_init()
2690 dev_dbg(nor->dev, "octal mode not supported\n"); in spi_nor_init()
2694 err = spi_nor_quad_enable(nor); in spi_nor_init()
2696 dev_dbg(nor->dev, "quad mode not supported\n"); in spi_nor_init()
2701 * Some SPI NOR flashes are write protected by default after a power-on in spi_nor_init()
2712 nor->flags & SNOR_F_SWP_IS_VOLATILE)) in spi_nor_init()
2713 spi_nor_try_unlock_all(nor); in spi_nor_init()
2715 if (nor->addr_nbytes == 4 && in spi_nor_init()
2716 nor->read_proto != SNOR_PROTO_8_8_8_DTR && in spi_nor_init()
2717 !(nor->flags & SNOR_F_4B_OPCODES)) { in spi_nor_init()
2725 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET, in spi_nor_init()
2727 err = nor->params->set_4byte_addr_mode(nor, true); in spi_nor_init()
2737 * @nor: pointer to 'struct spi_nor'
2750 static void spi_nor_soft_reset(struct spi_nor *nor) in spi_nor_soft_reset() argument
2757 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_soft_reset()
2759 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_soft_reset()
2761 dev_warn(nor->dev, "Software reset failed: %d\n", ret); in spi_nor_soft_reset()
2767 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); in spi_nor_soft_reset()
2769 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_soft_reset()
2771 dev_warn(nor->dev, "Software reset failed: %d\n", ret); in spi_nor_soft_reset()
2786 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_suspend() local
2790 ret = spi_nor_octal_dtr_enable(nor, false); in spi_nor_suspend()
2792 dev_err(nor->dev, "suspend() failed\n"); in spi_nor_suspend()
2800 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_resume() local
2801 struct device *dev = nor->dev; in spi_nor_resume()
2804 /* re-initialize the nor chip */ in spi_nor_resume()
2805 ret = spi_nor_init(nor); in spi_nor_resume()
2813 struct spi_nor *nor = mtd_to_spi_nor(master); in spi_nor_get_device() local
2816 if (nor->spimem) in spi_nor_get_device()
2817 dev = nor->spimem->spi->controller->dev.parent; in spi_nor_get_device()
2819 dev = nor->dev; in spi_nor_get_device()
2830 struct spi_nor *nor = mtd_to_spi_nor(master); in spi_nor_put_device() local
2833 if (nor->spimem) in spi_nor_put_device()
2834 dev = nor->spimem->spi->controller->dev.parent; in spi_nor_put_device()
2836 dev = nor->dev; in spi_nor_put_device()
2841 void spi_nor_restore(struct spi_nor *nor) in spi_nor_restore() argument
2844 if (nor->addr_nbytes == 4 && !(nor->flags & SNOR_F_4B_OPCODES) && in spi_nor_restore()
2845 nor->flags & SNOR_F_BROKEN_RESET) in spi_nor_restore()
2846 nor->params->set_4byte_addr_mode(nor, false); in spi_nor_restore()
2848 if (nor->flags & SNOR_F_SOFT_RESET) in spi_nor_restore()
2849 spi_nor_soft_reset(nor); in spi_nor_restore()
2853 static const struct flash_info *spi_nor_match_name(struct spi_nor *nor, in spi_nor_match_name() argument
2861 nor->manufacturer = manufacturers[i]; in spi_nor_match_name()
2870 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor, in spi_nor_get_flash_info() argument
2876 info = spi_nor_match_name(nor, name); in spi_nor_get_flash_info()
2879 return spi_nor_detect(nor); in spi_nor_get_flash_info()
2888 jinfo = spi_nor_detect(nor); in spi_nor_get_flash_info()
2899 dev_warn(nor->dev, "found %s, expected %s\n", in spi_nor_get_flash_info()
2908 static void spi_nor_set_mtd_info(struct spi_nor *nor) in spi_nor_set_mtd_info() argument
2910 struct mtd_info *mtd = &nor->mtd; in spi_nor_set_mtd_info()
2911 struct device *dev = nor->dev; in spi_nor_set_mtd_info()
2913 spi_nor_set_mtd_locking_ops(nor); in spi_nor_set_mtd_info()
2914 spi_nor_set_mtd_otp_ops(nor); in spi_nor_set_mtd_info()
2921 if (nor->info->flags & SPI_NOR_NO_ERASE) in spi_nor_set_mtd_info()
2925 mtd->writesize = nor->params->writesize; in spi_nor_set_mtd_info()
2926 mtd->writebufsize = nor->params->page_size; in spi_nor_set_mtd_info()
2927 mtd->size = nor->params->size; in spi_nor_set_mtd_info()
2938 int spi_nor_scan(struct spi_nor *nor, const char *name, in spi_nor_scan() argument
2942 struct device *dev = nor->dev; in spi_nor_scan()
2943 struct mtd_info *mtd = &nor->mtd; in spi_nor_scan()
2947 ret = spi_nor_check(nor); in spi_nor_scan()
2952 nor->reg_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
2953 nor->read_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
2954 nor->write_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
2960 * nor->params->page_size turns out to be greater than PAGE_SIZE (which in spi_nor_scan()
2961 * shouldn't happen before long since NOR pages are usually less in spi_nor_scan()
2964 nor->bouncebuf_size = PAGE_SIZE; in spi_nor_scan()
2965 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size, in spi_nor_scan()
2967 if (!nor->bouncebuf) in spi_nor_scan()
2970 info = spi_nor_get_flash_info(nor, name); in spi_nor_scan()
2974 nor->info = info; in spi_nor_scan()
2976 mutex_init(&nor->lock); in spi_nor_scan()
2979 ret = spi_nor_init_params(nor); in spi_nor_scan()
2990 ret = spi_nor_setup(nor, hwcaps); in spi_nor_scan()
2995 ret = spi_nor_init(nor); in spi_nor_scan()
3000 spi_nor_set_mtd_info(nor); in spi_nor_scan()
3025 static int spi_nor_create_read_dirmap(struct spi_nor *nor) in spi_nor_create_read_dirmap() argument
3028 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0), in spi_nor_create_read_dirmap()
3029 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0), in spi_nor_create_read_dirmap()
3030 SPI_MEM_OP_DUMMY(nor->read_dummy, 0), in spi_nor_create_read_dirmap()
3033 .length = nor->params->size, in spi_nor_create_read_dirmap()
3037 spi_nor_spimem_setup_op(nor, op, nor->read_proto); in spi_nor_create_read_dirmap()
3040 op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8; in spi_nor_create_read_dirmap()
3041 if (spi_nor_protocol_is_dtr(nor->read_proto)) in spi_nor_create_read_dirmap()
3049 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); in spi_nor_create_read_dirmap()
3051 nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, in spi_nor_create_read_dirmap()
3053 return PTR_ERR_OR_ZERO(nor->dirmap.rdesc); in spi_nor_create_read_dirmap()
3056 static int spi_nor_create_write_dirmap(struct spi_nor *nor) in spi_nor_create_write_dirmap() argument
3059 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0), in spi_nor_create_write_dirmap()
3060 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0), in spi_nor_create_write_dirmap()
3064 .length = nor->params->size, in spi_nor_create_write_dirmap()
3068 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) in spi_nor_create_write_dirmap()
3071 spi_nor_spimem_setup_op(nor, op, nor->write_proto); in spi_nor_create_write_dirmap()
3078 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); in spi_nor_create_write_dirmap()
3080 nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, in spi_nor_create_write_dirmap()
3082 return PTR_ERR_OR_ZERO(nor->dirmap.wdesc); in spi_nor_create_write_dirmap()
3089 struct spi_nor *nor; in spi_nor_probe() local
3098 nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL); in spi_nor_probe()
3099 if (!nor) in spi_nor_probe()
3102 nor->spimem = spimem; in spi_nor_probe()
3103 nor->dev = &spi->dev; in spi_nor_probe()
3104 spi_nor_set_flash_node(nor, spi->dev.of_node); in spi_nor_probe()
3106 spi_mem_set_drvdata(spimem, nor); in spi_nor_probe()
3109 nor->mtd.name = data->name; in spi_nor_probe()
3111 if (!nor->mtd.name) in spi_nor_probe()
3112 nor->mtd.name = spi_mem_get_name(spimem); in spi_nor_probe()
3122 else if (!strcmp(spi->modalias, "spi-nor")) in spi_nor_probe()
3127 ret = spi_nor_scan(nor, flash_name, &hwcaps); in spi_nor_probe()
3131 spi_nor_debugfs_register(nor); in spi_nor_probe()
3136 * a NOR we don't end up with buffer overflows. in spi_nor_probe()
3138 if (nor->params->page_size > PAGE_SIZE) { in spi_nor_probe()
3139 nor->bouncebuf_size = nor->params->page_size; in spi_nor_probe()
3140 devm_kfree(nor->dev, nor->bouncebuf); in spi_nor_probe()
3141 nor->bouncebuf = devm_kmalloc(nor->dev, in spi_nor_probe()
3142 nor->bouncebuf_size, in spi_nor_probe()
3144 if (!nor->bouncebuf) in spi_nor_probe()
3148 ret = spi_nor_create_read_dirmap(nor); in spi_nor_probe()
3152 ret = spi_nor_create_write_dirmap(nor); in spi_nor_probe()
3156 return mtd_device_register(&nor->mtd, data ? data->parts : NULL, in spi_nor_probe()
3162 struct spi_nor *nor = spi_mem_get_drvdata(spimem); in spi_nor_remove() local
3164 spi_nor_restore(nor); in spi_nor_remove()
3167 return mtd_device_unregister(&nor->mtd); in spi_nor_remove()
3172 struct spi_nor *nor = spi_mem_get_drvdata(spimem); in spi_nor_shutdown() local
3174 spi_nor_restore(nor); in spi_nor_shutdown()
3183 * encourage new users to add support to the spi-nor library, and simply bind
3184 * against a generic string here (e.g., "jedec,spi-nor").
3191 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
3195 {"spi-nor"},
3199 * them with "spi-nor" in platform data.
3204 * Entries that were used in DTs without "jedec,spi-nor" fallback and
3236 * Generic compatibility for SPI NOR that can be identified by the
3239 { .compatible = "jedec,spi-nor" },
3252 .name = "spi-nor",
3267 MODULE_DESCRIPTION("framework for SPI NOR");