Lines Matching full:nor
23 #include <linux/mtd/spi-nor.h>
46 * @nor: pointer to 'struct spi_nor'
53 static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op) in spi_nor_spimem_bounce() argument
58 if (op->data.nbytes > nor->bouncebuf_size) in spi_nor_spimem_bounce()
59 op->data.nbytes = nor->bouncebuf_size; in spi_nor_spimem_bounce()
60 op->data.buf.in = nor->bouncebuf; in spi_nor_spimem_bounce()
69 * @nor: pointer to 'struct spi_nor'
74 static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op) in spi_nor_spimem_exec_op() argument
78 error = spi_mem_adjust_op_size(nor->spimem, op); in spi_nor_spimem_exec_op()
82 return spi_mem_exec_op(nor->spimem, op); in spi_nor_spimem_exec_op()
88 * @nor: pointer to 'struct spi_nor'
95 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from, in spi_nor_spimem_read_data() argument
99 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1), in spi_nor_spimem_read_data()
100 SPI_MEM_OP_ADDR(nor->addr_width, from, 1), in spi_nor_spimem_read_data()
101 SPI_MEM_OP_DUMMY(nor->read_dummy, 1), in spi_nor_spimem_read_data()
108 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto); in spi_nor_spimem_read_data()
109 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto); in spi_nor_spimem_read_data()
111 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); in spi_nor_spimem_read_data()
114 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; in spi_nor_spimem_read_data()
116 usebouncebuf = spi_nor_spimem_bounce(nor, &op); in spi_nor_spimem_read_data()
118 if (nor->dirmap.rdesc) { in spi_nor_spimem_read_data()
119 nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val, in spi_nor_spimem_read_data()
122 error = spi_nor_spimem_exec_op(nor, &op); in spi_nor_spimem_read_data()
136 * @nor: pointer to 'struct spi_nor'
143 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf) in spi_nor_read_data() argument
145 if (nor->spimem) in spi_nor_read_data()
146 return spi_nor_spimem_read_data(nor, from, len, buf); in spi_nor_read_data()
148 return nor->controller_ops->read(nor, from, len, buf); in spi_nor_read_data()
154 * @nor: pointer to 'struct spi_nor'
161 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to, in spi_nor_spimem_write_data() argument
165 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1), in spi_nor_spimem_write_data()
166 SPI_MEM_OP_ADDR(nor->addr_width, to, 1), in spi_nor_spimem_write_data()
172 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto); in spi_nor_spimem_write_data()
173 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto); in spi_nor_spimem_write_data()
174 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); in spi_nor_spimem_write_data()
176 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) in spi_nor_spimem_write_data()
179 if (spi_nor_spimem_bounce(nor, &op)) in spi_nor_spimem_write_data()
180 memcpy(nor->bouncebuf, buf, op.data.nbytes); in spi_nor_spimem_write_data()
182 if (nor->dirmap.wdesc) { in spi_nor_spimem_write_data()
183 nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val, in spi_nor_spimem_write_data()
186 error = spi_nor_spimem_exec_op(nor, &op); in spi_nor_spimem_write_data()
197 * @nor: pointer to 'struct spi_nor'
204 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len, in spi_nor_write_data() argument
207 if (nor->spimem) in spi_nor_write_data()
208 return spi_nor_spimem_write_data(nor, to, len, buf); in spi_nor_write_data()
210 return nor->controller_ops->write(nor, to, len, buf); in spi_nor_write_data()
215 * @nor: pointer to 'struct spi_nor'.
219 int spi_nor_write_enable(struct spi_nor *nor) in spi_nor_write_enable() argument
223 if (nor->spimem) { in spi_nor_write_enable()
230 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_enable()
232 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WREN, in spi_nor_write_enable()
237 dev_dbg(nor->dev, "error %d on Write Enable\n", ret); in spi_nor_write_enable()
244 * @nor: pointer to 'struct spi_nor'.
248 int spi_nor_write_disable(struct spi_nor *nor) in spi_nor_write_disable() argument
252 if (nor->spimem) { in spi_nor_write_disable()
259 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_disable()
261 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRDI, in spi_nor_write_disable()
266 dev_dbg(nor->dev, "error %d on Write Disable\n", ret); in spi_nor_write_disable()
273 * @nor: pointer to 'struct spi_nor'.
279 static int spi_nor_read_sr(struct spi_nor *nor, u8 *sr) in spi_nor_read_sr() argument
283 if (nor->spimem) { in spi_nor_read_sr()
290 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_sr()
292 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDSR, in spi_nor_read_sr()
297 dev_dbg(nor->dev, "error %d reading SR\n", ret); in spi_nor_read_sr()
304 * @nor: pointer to 'struct spi_nor'
310 static int spi_nor_read_fsr(struct spi_nor *nor, u8 *fsr) in spi_nor_read_fsr() argument
314 if (nor->spimem) { in spi_nor_read_fsr()
321 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_fsr()
323 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDFSR, in spi_nor_read_fsr()
328 dev_dbg(nor->dev, "error %d reading FSR\n", ret); in spi_nor_read_fsr()
336 * @nor: pointer to 'struct spi_nor'
342 static int spi_nor_read_cr(struct spi_nor *nor, u8 *cr) in spi_nor_read_cr() argument
346 if (nor->spimem) { in spi_nor_read_cr()
353 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_cr()
355 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDCR, cr, 1); in spi_nor_read_cr()
359 dev_dbg(nor->dev, "error %d reading CR\n", ret); in spi_nor_read_cr()
366 * @nor: pointer to 'struct spi_nor'.
372 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable) in spi_nor_set_4byte_addr_mode() argument
376 if (nor->spimem) { in spi_nor_set_4byte_addr_mode()
386 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_set_4byte_addr_mode()
388 ret = nor->controller_ops->write_reg(nor, in spi_nor_set_4byte_addr_mode()
395 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); in spi_nor_set_4byte_addr_mode()
403 * @nor: pointer to 'struct spi_nor'.
409 static int spansion_set_4byte_addr_mode(struct spi_nor *nor, bool enable) in spansion_set_4byte_addr_mode() argument
413 nor->bouncebuf[0] = enable << 7; in spansion_set_4byte_addr_mode()
415 if (nor->spimem) { in spansion_set_4byte_addr_mode()
420 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1)); in spansion_set_4byte_addr_mode()
422 ret = spi_mem_exec_op(nor->spimem, &op); in spansion_set_4byte_addr_mode()
424 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_BRWR, in spansion_set_4byte_addr_mode()
425 nor->bouncebuf, 1); in spansion_set_4byte_addr_mode()
429 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); in spansion_set_4byte_addr_mode()
436 * @nor: pointer to 'struct spi_nor'.
441 int spi_nor_write_ear(struct spi_nor *nor, u8 ear) in spi_nor_write_ear() argument
445 nor->bouncebuf[0] = ear; in spi_nor_write_ear()
447 if (nor->spimem) { in spi_nor_write_ear()
452 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1)); in spi_nor_write_ear()
454 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_ear()
456 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WREAR, in spi_nor_write_ear()
457 nor->bouncebuf, 1); in spi_nor_write_ear()
461 dev_dbg(nor->dev, "error %d writing EAR\n", ret); in spi_nor_write_ear()
468 * @nor: pointer to 'struct spi_nor'.
474 int spi_nor_xread_sr(struct spi_nor *nor, u8 *sr) in spi_nor_xread_sr() argument
478 if (nor->spimem) { in spi_nor_xread_sr()
485 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_xread_sr()
487 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_XRDSR, in spi_nor_xread_sr()
492 dev_dbg(nor->dev, "error %d reading XRDSR\n", ret); in spi_nor_xread_sr()
500 * @nor: pointer to 'struct spi_nor'.
504 static int spi_nor_xsr_ready(struct spi_nor *nor) in spi_nor_xsr_ready() argument
508 ret = spi_nor_xread_sr(nor, nor->bouncebuf); in spi_nor_xsr_ready()
512 return !!(nor->bouncebuf[0] & XSR_RDY); in spi_nor_xsr_ready()
517 * @nor: pointer to 'struct spi_nor'.
519 static void spi_nor_clear_sr(struct spi_nor *nor) in spi_nor_clear_sr() argument
523 if (nor->spimem) { in spi_nor_clear_sr()
530 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_clear_sr()
532 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CLSR, in spi_nor_clear_sr()
537 dev_dbg(nor->dev, "error %d clearing SR\n", ret); in spi_nor_clear_sr()
543 * @nor: pointer to 'struct spi_nor'.
547 static int spi_nor_sr_ready(struct spi_nor *nor) in spi_nor_sr_ready() argument
549 int ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_ready()
554 if (nor->flags & SNOR_F_USE_CLSR && in spi_nor_sr_ready()
555 nor->bouncebuf[0] & (SR_E_ERR | SR_P_ERR)) { in spi_nor_sr_ready()
556 if (nor->bouncebuf[0] & SR_E_ERR) in spi_nor_sr_ready()
557 dev_err(nor->dev, "Erase Error occurred\n"); in spi_nor_sr_ready()
559 dev_err(nor->dev, "Programming Error occurred\n"); in spi_nor_sr_ready()
561 spi_nor_clear_sr(nor); in spi_nor_sr_ready()
569 ret = spi_nor_write_disable(nor); in spi_nor_sr_ready()
576 return !(nor->bouncebuf[0] & SR_WIP); in spi_nor_sr_ready()
581 * @nor: pointer to 'struct spi_nor'.
583 static void spi_nor_clear_fsr(struct spi_nor *nor) in spi_nor_clear_fsr() argument
587 if (nor->spimem) { in spi_nor_clear_fsr()
594 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_clear_fsr()
596 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CLFSR, in spi_nor_clear_fsr()
601 dev_dbg(nor->dev, "error %d clearing FSR\n", ret); in spi_nor_clear_fsr()
607 * @nor: pointer to 'struct spi_nor'.
611 static int spi_nor_fsr_ready(struct spi_nor *nor) in spi_nor_fsr_ready() argument
613 int ret = spi_nor_read_fsr(nor, nor->bouncebuf); in spi_nor_fsr_ready()
618 if (nor->bouncebuf[0] & (FSR_E_ERR | FSR_P_ERR)) { in spi_nor_fsr_ready()
619 if (nor->bouncebuf[0] & FSR_E_ERR) in spi_nor_fsr_ready()
620 dev_err(nor->dev, "Erase operation failed.\n"); in spi_nor_fsr_ready()
622 dev_err(nor->dev, "Program operation failed.\n"); in spi_nor_fsr_ready()
624 if (nor->bouncebuf[0] & FSR_PT_ERR) in spi_nor_fsr_ready()
625 dev_err(nor->dev, in spi_nor_fsr_ready()
628 spi_nor_clear_fsr(nor); in spi_nor_fsr_ready()
636 ret = spi_nor_write_disable(nor); in spi_nor_fsr_ready()
643 return !!(nor->bouncebuf[0] & FSR_READY); in spi_nor_fsr_ready()
648 * @nor: pointer to 'struct spi_nor'.
652 static int spi_nor_ready(struct spi_nor *nor) in spi_nor_ready() argument
656 if (nor->flags & SNOR_F_READY_XSR_RDY) in spi_nor_ready()
657 sr = spi_nor_xsr_ready(nor); in spi_nor_ready()
659 sr = spi_nor_sr_ready(nor); in spi_nor_ready()
662 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1; in spi_nor_ready()
671 * @nor: pointer to "struct spi_nor".
676 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor, in spi_nor_wait_till_ready_with_timeout() argument
688 ret = spi_nor_ready(nor); in spi_nor_wait_till_ready_with_timeout()
697 dev_dbg(nor->dev, "flash operation timed out\n"); in spi_nor_wait_till_ready_with_timeout()
705 * @nor: pointer to "struct spi_nor".
709 int spi_nor_wait_till_ready(struct spi_nor *nor) in spi_nor_wait_till_ready() argument
711 return spi_nor_wait_till_ready_with_timeout(nor, in spi_nor_wait_till_ready()
717 * @nor: pointer to 'struct spi_nor'.
723 static int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len) in spi_nor_write_sr() argument
727 ret = spi_nor_write_enable(nor); in spi_nor_write_sr()
731 if (nor->spimem) { in spi_nor_write_sr()
738 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_sr()
740 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRSR, in spi_nor_write_sr()
745 dev_dbg(nor->dev, "error %d writing SR\n", ret); in spi_nor_write_sr()
749 return spi_nor_wait_till_ready(nor); in spi_nor_write_sr()
755 * @nor: pointer to a 'struct spi_nor'.
760 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_sr1_and_check() argument
764 nor->bouncebuf[0] = sr1; in spi_nor_write_sr1_and_check()
766 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1); in spi_nor_write_sr1_and_check()
770 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_write_sr1_and_check()
774 if (nor->bouncebuf[0] != sr1) { in spi_nor_write_sr1_and_check()
775 dev_dbg(nor->dev, "SR1: read back test failed\n"); in spi_nor_write_sr1_and_check()
787 * @nor: pointer to a 'struct spi_nor'.
792 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_16bit_sr_and_check() argument
795 u8 *sr_cr = nor->bouncebuf; in spi_nor_write_16bit_sr_and_check()
799 if (!(nor->flags & SNOR_F_NO_READ_CR)) { in spi_nor_write_16bit_sr_and_check()
800 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_sr_and_check()
803 } else if (nor->params->quad_enable) { in spi_nor_write_16bit_sr_and_check()
811 * nor->params->quad_enable() call. in spi_nor_write_16bit_sr_and_check()
826 ret = spi_nor_write_sr(nor, sr_cr, 2); in spi_nor_write_16bit_sr_and_check()
830 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_write_16bit_sr_and_check()
835 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_sr_and_check()
840 dev_dbg(nor->dev, "CR: read back test failed\n"); in spi_nor_write_16bit_sr_and_check()
852 * @nor: pointer to a 'struct spi_nor'.
857 static int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr) in spi_nor_write_16bit_cr_and_check() argument
860 u8 *sr_cr = nor->bouncebuf; in spi_nor_write_16bit_cr_and_check()
864 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_cr_and_check()
870 ret = spi_nor_write_sr(nor, sr_cr, 2); in spi_nor_write_16bit_cr_and_check()
876 ret = spi_nor_read_sr(nor, sr_cr); in spi_nor_write_16bit_cr_and_check()
881 dev_dbg(nor->dev, "SR: Read back test failed\n"); in spi_nor_write_16bit_cr_and_check()
885 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_write_16bit_cr_and_check()
888 ret = spi_nor_read_cr(nor, &sr_cr[1]); in spi_nor_write_16bit_cr_and_check()
893 dev_dbg(nor->dev, "CR: read back test failed\n"); in spi_nor_write_16bit_cr_and_check()
904 * @nor: pointer to a 'struct spi_nor'.
909 static int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1) in spi_nor_write_sr_and_check() argument
911 if (nor->flags & SNOR_F_HAS_16BIT_SR) in spi_nor_write_sr_and_check()
912 return spi_nor_write_16bit_sr_and_check(nor, sr1); in spi_nor_write_sr_and_check()
914 return spi_nor_write_sr1_and_check(nor, sr1); in spi_nor_write_sr_and_check()
920 * @nor: pointer to 'struct spi_nor'.
925 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2) in spi_nor_write_sr2() argument
929 ret = spi_nor_write_enable(nor); in spi_nor_write_sr2()
933 if (nor->spimem) { in spi_nor_write_sr2()
940 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_write_sr2()
942 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRSR2, in spi_nor_write_sr2()
947 dev_dbg(nor->dev, "error %d writing SR2\n", ret); in spi_nor_write_sr2()
951 return spi_nor_wait_till_ready(nor); in spi_nor_write_sr2()
957 * @nor: pointer to 'struct spi_nor'.
963 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2) in spi_nor_read_sr2() argument
967 if (nor->spimem) { in spi_nor_read_sr2()
974 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_sr2()
976 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDSR2, in spi_nor_read_sr2()
981 dev_dbg(nor->dev, "error %d reading SR2\n", ret); in spi_nor_read_sr2()
988 * @nor: pointer to 'struct spi_nor'.
992 static int spi_nor_erase_chip(struct spi_nor *nor) in spi_nor_erase_chip() argument
996 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10)); in spi_nor_erase_chip()
998 if (nor->spimem) { in spi_nor_erase_chip()
1005 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_erase_chip()
1007 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CHIP_ERASE, in spi_nor_erase_chip()
1012 dev_dbg(nor->dev, "error %d erasing chip\n", ret); in spi_nor_erase_chip()
1076 static bool spi_nor_has_uniform_erase(const struct spi_nor *nor) in spi_nor_has_uniform_erase() argument
1078 return !!nor->params->erase_map.uniform_erase_type; in spi_nor_has_uniform_erase()
1081 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor) in spi_nor_set_4byte_opcodes() argument
1083 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode); in spi_nor_set_4byte_opcodes()
1084 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode); in spi_nor_set_4byte_opcodes()
1085 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode); in spi_nor_set_4byte_opcodes()
1087 if (!spi_nor_has_uniform_erase(nor)) { in spi_nor_set_4byte_opcodes()
1088 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_set_4byte_opcodes()
1100 int spi_nor_lock_and_prep(struct spi_nor *nor) in spi_nor_lock_and_prep() argument
1104 mutex_lock(&nor->lock); in spi_nor_lock_and_prep()
1106 if (nor->controller_ops && nor->controller_ops->prepare) { in spi_nor_lock_and_prep()
1107 ret = nor->controller_ops->prepare(nor); in spi_nor_lock_and_prep()
1109 mutex_unlock(&nor->lock); in spi_nor_lock_and_prep()
1116 void spi_nor_unlock_and_unprep(struct spi_nor *nor) in spi_nor_unlock_and_unprep() argument
1118 if (nor->controller_ops && nor->controller_ops->unprepare) in spi_nor_unlock_and_unprep()
1119 nor->controller_ops->unprepare(nor); in spi_nor_unlock_and_unprep()
1120 mutex_unlock(&nor->lock); in spi_nor_unlock_and_unprep()
1123 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr) in spi_nor_convert_addr() argument
1125 if (!nor->params->convert_addr) in spi_nor_convert_addr()
1128 return nor->params->convert_addr(nor, addr); in spi_nor_convert_addr()
1134 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) in spi_nor_erase_sector() argument
1138 addr = spi_nor_convert_addr(nor, addr); in spi_nor_erase_sector()
1140 if (nor->spimem) { in spi_nor_erase_sector()
1142 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1), in spi_nor_erase_sector()
1143 SPI_MEM_OP_ADDR(nor->addr_width, addr, 1), in spi_nor_erase_sector()
1147 return spi_mem_exec_op(nor->spimem, &op); in spi_nor_erase_sector()
1148 } else if (nor->controller_ops->erase) { in spi_nor_erase_sector()
1149 return nor->controller_ops->erase(nor, addr); in spi_nor_erase_sector()
1156 for (i = nor->addr_width - 1; i >= 0; i--) { in spi_nor_erase_sector()
1157 nor->bouncebuf[i] = addr & 0xff; in spi_nor_erase_sector()
1161 return nor->controller_ops->write_reg(nor, nor->erase_opcode, in spi_nor_erase_sector()
1162 nor->bouncebuf, nor->addr_width); in spi_nor_erase_sector()
1167 * @erase: pointer to a structure that describes a SPI NOR erase type
1187 * @map: the erase map of the SPI NOR
1188 * @region: pointer to a structure that describes a SPI NOR erase region
1244 * spi_nor_region_next() - get the next spi nor region
1245 * @region: pointer to a structure that describes a SPI NOR erase region
1247 * Return: the next spi nor region or NULL if last region.
1261 * @map: the erase map of the SPI NOR
1288 * @region: pointer to a structure that describes a SPI NOR erase region
1289 * @erase: pointer to a structure that describes a SPI NOR erase type
1332 * @nor: pointer to a 'struct spi_nor'
1343 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor, in spi_nor_init_erase_cmd_list() argument
1347 const struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_init_erase_cmd_list()
1400 * @nor: pointer to a 'struct spi_nor'
1409 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len) in spi_nor_erase_multi_sectors() argument
1415 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len); in spi_nor_erase_multi_sectors()
1420 nor->erase_opcode = cmd->opcode; in spi_nor_erase_multi_sectors()
1422 ret = spi_nor_write_enable(nor); in spi_nor_erase_multi_sectors()
1426 ret = spi_nor_erase_sector(nor, addr); in spi_nor_erase_multi_sectors()
1433 ret = spi_nor_wait_till_ready(nor); in spi_nor_erase_multi_sectors()
1449 * Erase an address range on the nor chip. The address range may extend
1454 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_erase() local
1459 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr, in spi_nor_erase()
1462 if (spi_nor_has_uniform_erase(nor)) { in spi_nor_erase()
1471 ret = spi_nor_lock_and_prep(nor); in spi_nor_erase()
1476 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) { in spi_nor_erase()
1479 ret = spi_nor_write_enable(nor); in spi_nor_erase()
1483 ret = spi_nor_erase_chip(nor); in spi_nor_erase()
1496 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout); in spi_nor_erase()
1506 } else if (spi_nor_has_uniform_erase(nor)) { in spi_nor_erase()
1508 ret = spi_nor_write_enable(nor); in spi_nor_erase()
1512 ret = spi_nor_erase_sector(nor, addr); in spi_nor_erase()
1519 ret = spi_nor_wait_till_ready(nor); in spi_nor_erase()
1526 ret = spi_nor_erase_multi_sectors(nor, addr, len); in spi_nor_erase()
1531 ret = spi_nor_write_disable(nor); in spi_nor_erase()
1534 spi_nor_unlock_and_unprep(nor); in spi_nor_erase()
1539 static u8 spi_nor_get_sr_bp_mask(struct spi_nor *nor) in spi_nor_get_sr_bp_mask() argument
1543 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6) in spi_nor_get_sr_bp_mask()
1546 if (nor->flags & SNOR_F_HAS_4BIT_BP) in spi_nor_get_sr_bp_mask()
1552 static u8 spi_nor_get_sr_tb_mask(struct spi_nor *nor) in spi_nor_get_sr_tb_mask() argument
1554 if (nor->flags & SNOR_F_HAS_SR_TB_BIT6) in spi_nor_get_sr_tb_mask()
1560 static u64 spi_nor_get_min_prot_length_sr(struct spi_nor *nor) in spi_nor_get_min_prot_length_sr() argument
1563 u8 mask = spi_nor_get_sr_bp_mask(nor); in spi_nor_get_min_prot_length_sr()
1567 bp_slots_needed = ilog2(nor->info->n_sectors); in spi_nor_get_min_prot_length_sr()
1570 return nor->info->sector_size << in spi_nor_get_min_prot_length_sr()
1573 return nor->info->sector_size; in spi_nor_get_min_prot_length_sr()
1576 static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs, in spi_nor_get_locked_range_sr() argument
1579 struct mtd_info *mtd = &nor->mtd; in spi_nor_get_locked_range_sr()
1581 u8 mask = spi_nor_get_sr_bp_mask(nor); in spi_nor_get_locked_range_sr()
1582 u8 tb_mask = spi_nor_get_sr_tb_mask(nor); in spi_nor_get_locked_range_sr()
1585 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6) in spi_nor_get_locked_range_sr()
1597 min_prot_len = spi_nor_get_min_prot_length_sr(nor); in spi_nor_get_locked_range_sr()
1603 if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask) in spi_nor_get_locked_range_sr()
1613 static int spi_nor_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, in spi_nor_check_lock_status_sr() argument
1622 spi_nor_get_locked_range_sr(nor, sr, &lock_offs, &lock_len); in spi_nor_check_lock_status_sr()
1632 static int spi_nor_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len, in spi_nor_is_locked_sr() argument
1635 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, true); in spi_nor_is_locked_sr()
1638 static int spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len, in spi_nor_is_unlocked_sr() argument
1641 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, false); in spi_nor_is_unlocked_sr()
1677 static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len) in spi_nor_sr_lock() argument
1679 struct mtd_info *mtd = &nor->mtd; in spi_nor_sr_lock()
1682 u8 mask = spi_nor_get_sr_bp_mask(nor); in spi_nor_sr_lock()
1683 u8 tb_mask = spi_nor_get_sr_tb_mask(nor); in spi_nor_sr_lock()
1686 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; in spi_nor_sr_lock()
1689 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_lock()
1693 status_old = nor->bouncebuf[0]; in spi_nor_sr_lock()
1696 if (spi_nor_is_locked_sr(nor, ofs, len, status_old)) in spi_nor_sr_lock()
1700 if (!spi_nor_is_locked_sr(nor, 0, ofs, status_old)) in spi_nor_sr_lock()
1704 if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len), in spi_nor_sr_lock()
1723 min_prot_len = spi_nor_get_min_prot_length_sr(nor); in spi_nor_sr_lock()
1727 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3) in spi_nor_sr_lock()
1754 return spi_nor_write_sr_and_check(nor, status_new); in spi_nor_sr_lock()
1762 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len) in spi_nor_sr_unlock() argument
1764 struct mtd_info *mtd = &nor->mtd; in spi_nor_sr_unlock()
1767 u8 mask = spi_nor_get_sr_bp_mask(nor); in spi_nor_sr_unlock()
1768 u8 tb_mask = spi_nor_get_sr_tb_mask(nor); in spi_nor_sr_unlock()
1771 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; in spi_nor_sr_unlock()
1774 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_unlock()
1778 status_old = nor->bouncebuf[0]; in spi_nor_sr_unlock()
1781 if (spi_nor_is_unlocked_sr(nor, ofs, len, status_old)) in spi_nor_sr_unlock()
1785 if (!spi_nor_is_unlocked_sr(nor, 0, ofs, status_old)) in spi_nor_sr_unlock()
1789 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len), in spi_nor_sr_unlock()
1808 min_prot_len = spi_nor_get_min_prot_length_sr(nor); in spi_nor_sr_unlock()
1812 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3) in spi_nor_sr_unlock()
1837 return spi_nor_write_sr_and_check(nor, status_new); in spi_nor_sr_unlock()
1847 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len) in spi_nor_sr_is_locked() argument
1851 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_is_locked()
1855 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]); in spi_nor_sr_is_locked()
1866 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_lock() local
1869 ret = spi_nor_lock_and_prep(nor); in spi_nor_lock()
1873 ret = nor->params->locking_ops->lock(nor, ofs, len); in spi_nor_lock()
1875 spi_nor_unlock_and_unprep(nor); in spi_nor_lock()
1881 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_unlock() local
1884 ret = spi_nor_lock_and_prep(nor); in spi_nor_unlock()
1888 ret = nor->params->locking_ops->unlock(nor, ofs, len); in spi_nor_unlock()
1890 spi_nor_unlock_and_unprep(nor); in spi_nor_unlock()
1896 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_is_locked() local
1899 ret = spi_nor_lock_and_prep(nor); in spi_nor_is_locked()
1903 ret = nor->params->locking_ops->is_locked(nor, ofs, len); in spi_nor_is_locked()
1905 spi_nor_unlock_and_unprep(nor); in spi_nor_is_locked()
1912 * @nor: pointer to a 'struct spi_nor'
1918 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor) in spi_nor_sr1_bit6_quad_enable() argument
1922 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr1_bit6_quad_enable()
1926 if (nor->bouncebuf[0] & SR1_QUAD_EN_BIT6) in spi_nor_sr1_bit6_quad_enable()
1929 nor->bouncebuf[0] |= SR1_QUAD_EN_BIT6; in spi_nor_sr1_bit6_quad_enable()
1931 return spi_nor_write_sr1_and_check(nor, nor->bouncebuf[0]); in spi_nor_sr1_bit6_quad_enable()
1937 * @nor: pointer to a 'struct spi_nor'.
1943 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor) in spi_nor_sr2_bit1_quad_enable() argument
1947 if (nor->flags & SNOR_F_NO_READ_CR) in spi_nor_sr2_bit1_quad_enable()
1948 return spi_nor_write_16bit_cr_and_check(nor, SR2_QUAD_EN_BIT1); in spi_nor_sr2_bit1_quad_enable()
1950 ret = spi_nor_read_cr(nor, nor->bouncebuf); in spi_nor_sr2_bit1_quad_enable()
1954 if (nor->bouncebuf[0] & SR2_QUAD_EN_BIT1) in spi_nor_sr2_bit1_quad_enable()
1957 nor->bouncebuf[0] |= SR2_QUAD_EN_BIT1; in spi_nor_sr2_bit1_quad_enable()
1959 return spi_nor_write_16bit_cr_and_check(nor, nor->bouncebuf[0]); in spi_nor_sr2_bit1_quad_enable()
1964 * @nor: pointer to a 'struct spi_nor'
1974 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor) in spi_nor_sr2_bit7_quad_enable() argument
1976 u8 *sr2 = nor->bouncebuf; in spi_nor_sr2_bit7_quad_enable()
1981 ret = spi_nor_read_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1990 ret = spi_nor_write_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
1997 ret = spi_nor_read_sr2(nor, sr2); in spi_nor_sr2_bit7_quad_enable()
2002 dev_dbg(nor->dev, "SR2: Read back test failed\n"); in spi_nor_sr2_bit7_quad_enable()
2044 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor) in spi_nor_read_id() argument
2047 u8 *id = nor->bouncebuf; in spi_nor_read_id()
2051 if (nor->spimem) { in spi_nor_read_id()
2058 ret = spi_mem_exec_op(nor->spimem, &op); in spi_nor_read_id()
2060 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id, in spi_nor_read_id()
2064 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret); in spi_nor_read_id()
2073 nor->manufacturer = manufacturers[i]; in spi_nor_read_id()
2078 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n", in spi_nor_read_id()
2086 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_read() local
2089 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len); in spi_nor_read()
2091 ret = spi_nor_lock_and_prep(nor); in spi_nor_read()
2098 addr = spi_nor_convert_addr(nor, addr); in spi_nor_read()
2100 ret = spi_nor_read_data(nor, addr, len, buf); in spi_nor_read()
2118 spi_nor_unlock_and_unprep(nor); in spi_nor_read()
2123 * Write an address range to the nor chip. Data must be written in
2130 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_write() local
2134 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len); in spi_nor_write()
2136 ret = spi_nor_lock_and_prep(nor); in spi_nor_write()
2152 if (hweight32(nor->page_size) == 1) { in spi_nor_write()
2153 page_offset = addr & (nor->page_size - 1); in spi_nor_write()
2157 page_offset = do_div(aux, nor->page_size); in spi_nor_write()
2161 nor->page_size - page_offset, len - i); in spi_nor_write()
2163 addr = spi_nor_convert_addr(nor, addr); in spi_nor_write()
2165 ret = spi_nor_write_enable(nor); in spi_nor_write()
2169 ret = spi_nor_write_data(nor, addr, page_remain, buf + i); in spi_nor_write()
2174 ret = spi_nor_wait_till_ready(nor); in spi_nor_write()
2182 spi_nor_unlock_and_unprep(nor); in spi_nor_write()
2186 static int spi_nor_check(struct spi_nor *nor) in spi_nor_check() argument
2188 if (!nor->dev || in spi_nor_check()
2189 (!nor->spimem && !nor->controller_ops) || in spi_nor_check()
2190 (!nor->spimem && nor->controller_ops && in spi_nor_check()
2191 (!nor->controller_ops->read || in spi_nor_check()
2192 !nor->controller_ops->write || in spi_nor_check()
2193 !nor->controller_ops->read_reg || in spi_nor_check()
2194 !nor->controller_ops->write_reg))) { in spi_nor_check()
2195 pr_err("spi-nor: please fill all the necessary fields!\n"); in spi_nor_check()
2199 if (nor->spimem && nor->controller_ops) { in spi_nor_check()
2200 …dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one… in spi_nor_check()
2281 *@nor: pointer to a 'struct spi_nor'
2286 static int spi_nor_spimem_check_op(struct spi_nor *nor, in spi_nor_spimem_check_op() argument
2296 if (!spi_mem_supports_op(nor->spimem, op)) { in spi_nor_spimem_check_op()
2297 if (nor->mtd.size > SZ_16M) in spi_nor_spimem_check_op()
2302 if (!spi_mem_supports_op(nor->spimem, op)) in spi_nor_spimem_check_op()
2312 *@nor: pointer to a 'struct spi_nor'
2317 static int spi_nor_spimem_check_readop(struct spi_nor *nor, in spi_nor_spimem_check_readop() argument
2332 return spi_nor_spimem_check_op(nor, &op); in spi_nor_spimem_check_readop()
2338 *@nor: pointer to a 'struct spi_nor'
2343 static int spi_nor_spimem_check_pp(struct spi_nor *nor, in spi_nor_spimem_check_pp() argument
2355 return spi_nor_spimem_check_op(nor, &op); in spi_nor_spimem_check_pp()
2361 * @nor: pointer to a 'struct spi_nor'
2366 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps) in spi_nor_spimem_adjust_hwcaps() argument
2368 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_spimem_adjust_hwcaps()
2385 spi_nor_spimem_check_readop(nor, ¶ms->reads[rdidx])) in spi_nor_spimem_adjust_hwcaps()
2392 if (spi_nor_spimem_check_pp(nor, in spi_nor_spimem_adjust_hwcaps()
2399 * spi_nor_set_erase_type() - set a SPI NOR erase type
2400 * @erase: pointer to a structure that describes a SPI NOR erase type
2416 * @map: the erase map of the SPI NOR
2419 * @flash_size: the spi nor flash memory size
2432 int spi_nor_post_bfpt_fixups(struct spi_nor *nor, in spi_nor_post_bfpt_fixups() argument
2439 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_post_bfpt_fixups()
2440 nor->manufacturer->fixups->post_bfpt) { in spi_nor_post_bfpt_fixups()
2441 ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header, in spi_nor_post_bfpt_fixups()
2447 if (nor->info->fixups && nor->info->fixups->post_bfpt) in spi_nor_post_bfpt_fixups()
2448 return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt, in spi_nor_post_bfpt_fixups()
2454 static int spi_nor_select_read(struct spi_nor *nor, in spi_nor_select_read() argument
2467 read = &nor->params->reads[cmd]; in spi_nor_select_read()
2468 nor->read_opcode = read->opcode; in spi_nor_select_read()
2469 nor->read_proto = read->proto; in spi_nor_select_read()
2472 * In the SPI NOR framework, we don't need to make the difference in spi_nor_select_read()
2481 nor->read_dummy = read->num_mode_clocks + read->num_wait_states; in spi_nor_select_read()
2485 static int spi_nor_select_pp(struct spi_nor *nor, in spi_nor_select_pp() argument
2498 pp = &nor->params->page_programs[cmd]; in spi_nor_select_pp()
2499 nor->program_opcode = pp->opcode; in spi_nor_select_pp()
2500 nor->write_proto = pp->proto; in spi_nor_select_pp()
2506 * @map: the erase map of the SPI NOR
2557 static int spi_nor_select_erase(struct spi_nor *nor) in spi_nor_select_erase() argument
2559 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_select_erase()
2561 struct mtd_info *mtd = &nor->mtd; in spi_nor_select_erase()
2562 u32 wanted_size = nor->info->sector_size; in spi_nor_select_erase()
2578 if (spi_nor_has_uniform_erase(nor)) { in spi_nor_select_erase()
2582 nor->erase_opcode = erase->opcode; in spi_nor_select_erase()
2589 * maximum erase sector size. No need to set nor->erase_opcode. in spi_nor_select_erase()
2605 static int spi_nor_default_setup(struct spi_nor *nor, in spi_nor_default_setup() argument
2608 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_default_setup()
2618 if (nor->spimem) { in spi_nor_default_setup()
2624 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask); in spi_nor_default_setup()
2633 dev_dbg(nor->dev, in spi_nor_default_setup()
2640 err = spi_nor_select_read(nor, shared_mask); in spi_nor_default_setup()
2642 dev_dbg(nor->dev, in spi_nor_default_setup()
2648 err = spi_nor_select_pp(nor, shared_mask); in spi_nor_default_setup()
2650 dev_dbg(nor->dev, in spi_nor_default_setup()
2656 err = spi_nor_select_erase(nor); in spi_nor_default_setup()
2658 dev_dbg(nor->dev, in spi_nor_default_setup()
2666 static int spi_nor_setup(struct spi_nor *nor, in spi_nor_setup() argument
2669 if (!nor->params->setup) in spi_nor_setup()
2672 return nor->params->setup(nor, hwcaps); in spi_nor_setup()
2678 * @nor: pointer to a 'struct spi_nor'.
2680 static void spi_nor_manufacturer_init_params(struct spi_nor *nor) in spi_nor_manufacturer_init_params() argument
2682 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_manufacturer_init_params()
2683 nor->manufacturer->fixups->default_init) in spi_nor_manufacturer_init_params()
2684 nor->manufacturer->fixups->default_init(nor); in spi_nor_manufacturer_init_params()
2686 if (nor->info->fixups && nor->info->fixups->default_init) in spi_nor_manufacturer_init_params()
2687 nor->info->fixups->default_init(nor); in spi_nor_manufacturer_init_params()
2693 * @nor: pointer to a 'struct spi_nor'.
2698 static void spi_nor_sfdp_init_params(struct spi_nor *nor) in spi_nor_sfdp_init_params() argument
2702 memcpy(&sfdp_params, nor->params, sizeof(sfdp_params)); in spi_nor_sfdp_init_params()
2704 if (spi_nor_parse_sfdp(nor, nor->params)) { in spi_nor_sfdp_init_params()
2705 memcpy(nor->params, &sfdp_params, sizeof(*nor->params)); in spi_nor_sfdp_init_params()
2706 nor->addr_width = 0; in spi_nor_sfdp_init_params()
2707 nor->flags &= ~SNOR_F_4B_OPCODES; in spi_nor_sfdp_init_params()
2713 * based on nor->info data.
2714 * @nor: pointer to a 'struct spi_nor'.
2716 static void spi_nor_info_init_params(struct spi_nor *nor) in spi_nor_info_init_params() argument
2718 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_info_init_params()
2720 const struct flash_info *info = nor->info; in spi_nor_info_init_params()
2721 struct device_node *np = spi_nor_get_flash_node(nor); in spi_nor_info_init_params()
2729 nor->flags |= SNOR_F_HAS_16BIT_SR; in spi_nor_info_init_params()
2731 /* Set SPI NOR sizes. */ in spi_nor_info_init_params()
2808 * @nor: pointer to a 'struct spi_nor'
2814 static void spi_nor_post_sfdp_fixups(struct spi_nor *nor) in spi_nor_post_sfdp_fixups() argument
2816 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_post_sfdp_fixups()
2817 nor->manufacturer->fixups->post_sfdp) in spi_nor_post_sfdp_fixups()
2818 nor->manufacturer->fixups->post_sfdp(nor); in spi_nor_post_sfdp_fixups()
2820 if (nor->info->fixups && nor->info->fixups->post_sfdp) in spi_nor_post_sfdp_fixups()
2821 nor->info->fixups->post_sfdp(nor); in spi_nor_post_sfdp_fixups()
2826 * @nor: pointer to a 'struct spi_nor'
2831 static void spi_nor_late_init_params(struct spi_nor *nor) in spi_nor_late_init_params() argument
2834 * NOR protection support. When locking_ops are not provided, we pick in spi_nor_late_init_params()
2837 if (nor->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops) in spi_nor_late_init_params()
2838 nor->params->locking_ops = &spi_nor_sr_locking_ops; in spi_nor_late_init_params()
2843 * @nor: pointer to a 'struct spi_nor'.
2849 * based on nor->info data:
2878 static int spi_nor_init_params(struct spi_nor *nor) in spi_nor_init_params() argument
2880 nor->params = devm_kzalloc(nor->dev, sizeof(*nor->params), GFP_KERNEL); in spi_nor_init_params()
2881 if (!nor->params) in spi_nor_init_params()
2884 spi_nor_info_init_params(nor); in spi_nor_init_params()
2886 spi_nor_manufacturer_init_params(nor); in spi_nor_init_params()
2888 if ((nor->info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) && in spi_nor_init_params()
2889 !(nor->info->flags & SPI_NOR_SKIP_SFDP)) in spi_nor_init_params()
2890 spi_nor_sfdp_init_params(nor); in spi_nor_init_params()
2892 spi_nor_post_sfdp_fixups(nor); in spi_nor_init_params()
2894 spi_nor_late_init_params(nor); in spi_nor_init_params()
2901 * @nor: pointer to a 'struct spi_nor'
2905 static int spi_nor_quad_enable(struct spi_nor *nor) in spi_nor_quad_enable() argument
2907 if (!nor->params->quad_enable) in spi_nor_quad_enable()
2910 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 || in spi_nor_quad_enable()
2911 spi_nor_get_protocol_width(nor->write_proto) == 4)) in spi_nor_quad_enable()
2914 return nor->params->quad_enable(nor); in spi_nor_quad_enable()
2919 * @nor: pointer to a 'struct spi_nor'.
2921 * Some SPI NOR flashes are write protected by default after a power-on reset
2926 static int spi_nor_unlock_all(struct spi_nor *nor) in spi_nor_unlock_all() argument
2928 if (nor->flags & SNOR_F_HAS_LOCK) in spi_nor_unlock_all()
2929 return spi_nor_unlock(&nor->mtd, 0, nor->params->size); in spi_nor_unlock_all()
2934 static int spi_nor_init(struct spi_nor *nor) in spi_nor_init() argument
2938 err = spi_nor_quad_enable(nor); in spi_nor_init()
2940 dev_dbg(nor->dev, "quad mode not supported\n"); in spi_nor_init()
2944 err = spi_nor_unlock_all(nor); in spi_nor_init()
2946 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n"); in spi_nor_init()
2950 if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES)) { in spi_nor_init()
2958 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET, in spi_nor_init()
2960 nor->params->set_4byte_addr_mode(nor, true); in spi_nor_init()
2969 struct spi_nor *nor = mtd_to_spi_nor(mtd); in spi_nor_resume() local
2970 struct device *dev = nor->dev; in spi_nor_resume()
2973 /* re-initialize the nor chip */ in spi_nor_resume()
2974 ret = spi_nor_init(nor); in spi_nor_resume()
2979 void spi_nor_restore(struct spi_nor *nor) in spi_nor_restore() argument
2982 if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES) && in spi_nor_restore()
2983 nor->flags & SNOR_F_BROKEN_RESET) in spi_nor_restore()
2984 nor->params->set_4byte_addr_mode(nor, false); in spi_nor_restore()
2988 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor, in spi_nor_match_id() argument
2996 nor->manufacturer = manufacturers[i]; in spi_nor_match_id()
3005 static int spi_nor_set_addr_width(struct spi_nor *nor) in spi_nor_set_addr_width() argument
3007 if (nor->addr_width) { in spi_nor_set_addr_width()
3009 } else if (nor->info->addr_width) { in spi_nor_set_addr_width()
3010 nor->addr_width = nor->info->addr_width; in spi_nor_set_addr_width()
3012 nor->addr_width = 3; in spi_nor_set_addr_width()
3015 if (nor->addr_width == 3 && nor->mtd.size > 0x1000000) { in spi_nor_set_addr_width()
3017 nor->addr_width = 4; in spi_nor_set_addr_width()
3020 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) { in spi_nor_set_addr_width()
3021 dev_dbg(nor->dev, "address width is too large: %u\n", in spi_nor_set_addr_width()
3022 nor->addr_width); in spi_nor_set_addr_width()
3027 if (nor->addr_width == 4 && nor->flags & SNOR_F_4B_OPCODES && in spi_nor_set_addr_width()
3028 !(nor->flags & SNOR_F_HAS_4BAIT)) in spi_nor_set_addr_width()
3029 spi_nor_set_4byte_opcodes(nor); in spi_nor_set_addr_width()
3034 static void spi_nor_debugfs_init(struct spi_nor *nor, in spi_nor_debugfs_init() argument
3037 struct mtd_info *mtd = &nor->mtd; in spi_nor_debugfs_init()
3040 mtd->dbg.partid = devm_kasprintf(nor->dev, GFP_KERNEL, "spi-nor:%*phN", in spi_nor_debugfs_init()
3044 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor, in spi_nor_get_flash_info() argument
3050 info = spi_nor_match_id(nor, name); in spi_nor_get_flash_info()
3053 info = spi_nor_read_id(nor); in spi_nor_get_flash_info()
3064 jinfo = spi_nor_read_id(nor); in spi_nor_get_flash_info()
3075 dev_warn(nor->dev, "found %s, expected %s\n", in spi_nor_get_flash_info()
3084 int spi_nor_scan(struct spi_nor *nor, const char *name, in spi_nor_scan() argument
3088 struct device *dev = nor->dev; in spi_nor_scan()
3089 struct mtd_info *mtd = &nor->mtd; in spi_nor_scan()
3090 struct device_node *np = spi_nor_get_flash_node(nor); in spi_nor_scan()
3094 ret = spi_nor_check(nor); in spi_nor_scan()
3099 nor->reg_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
3100 nor->read_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
3101 nor->write_proto = SNOR_PROTO_1_1_1; in spi_nor_scan()
3107 * nor->page_size turns out to be greater than PAGE_SIZE (which in spi_nor_scan()
3108 * shouldn't happen before long since NOR pages are usually less in spi_nor_scan()
3111 nor->bouncebuf_size = PAGE_SIZE; in spi_nor_scan()
3112 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size, in spi_nor_scan()
3114 if (!nor->bouncebuf) in spi_nor_scan()
3117 info = spi_nor_get_flash_info(nor, name); in spi_nor_scan()
3121 nor->info = info; in spi_nor_scan()
3123 spi_nor_debugfs_init(nor, info); in spi_nor_scan()
3125 mutex_init(&nor->lock); in spi_nor_scan()
3130 * with Atmel SPI NOR. in spi_nor_scan()
3133 nor->flags |= SNOR_F_READY_XSR_RDY; in spi_nor_scan()
3136 nor->flags |= SNOR_F_HAS_LOCK; in spi_nor_scan()
3141 ret = spi_nor_init_params(nor); in spi_nor_scan()
3147 mtd->priv = nor; in spi_nor_scan()
3151 mtd->size = nor->params->size; in spi_nor_scan()
3156 if (nor->params->locking_ops) { in spi_nor_scan()
3163 nor->flags |= SNOR_F_USE_FSR; in spi_nor_scan()
3165 nor->flags |= SNOR_F_HAS_SR_TB; in spi_nor_scan()
3167 nor->flags |= SNOR_F_HAS_SR_TB_BIT6; in spi_nor_scan()
3171 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE; in spi_nor_scan()
3173 nor->flags |= SNOR_F_USE_CLSR; in spi_nor_scan()
3176 nor->flags |= SNOR_F_HAS_4BIT_BP; in spi_nor_scan()
3178 nor->flags |= SNOR_F_HAS_SR_BP3_BIT6; in spi_nor_scan()
3185 nor->page_size = nor->params->page_size; in spi_nor_scan()
3186 mtd->writebufsize = nor->page_size; in spi_nor_scan()
3189 nor->flags |= SNOR_F_BROKEN_RESET; in spi_nor_scan()
3197 ret = spi_nor_setup(nor, hwcaps); in spi_nor_scan()
3202 nor->flags |= SNOR_F_4B_OPCODES; in spi_nor_scan()
3204 ret = spi_nor_set_addr_width(nor); in spi_nor_scan()
3209 ret = spi_nor_init(nor); in spi_nor_scan()
3236 static int spi_nor_create_read_dirmap(struct spi_nor *nor) in spi_nor_create_read_dirmap() argument
3239 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1), in spi_nor_create_read_dirmap()
3240 SPI_MEM_OP_ADDR(nor->addr_width, 0, 1), in spi_nor_create_read_dirmap()
3241 SPI_MEM_OP_DUMMY(nor->read_dummy, 1), in spi_nor_create_read_dirmap()
3244 .length = nor->mtd.size, in spi_nor_create_read_dirmap()
3249 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto); in spi_nor_create_read_dirmap()
3250 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto); in spi_nor_create_read_dirmap()
3252 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); in spi_nor_create_read_dirmap()
3255 op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8; in spi_nor_create_read_dirmap()
3257 nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, in spi_nor_create_read_dirmap()
3259 return PTR_ERR_OR_ZERO(nor->dirmap.rdesc); in spi_nor_create_read_dirmap()
3262 static int spi_nor_create_write_dirmap(struct spi_nor *nor) in spi_nor_create_write_dirmap() argument
3265 .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1), in spi_nor_create_write_dirmap()
3266 SPI_MEM_OP_ADDR(nor->addr_width, 0, 1), in spi_nor_create_write_dirmap()
3270 .length = nor->mtd.size, in spi_nor_create_write_dirmap()
3275 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto); in spi_nor_create_write_dirmap()
3276 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto); in spi_nor_create_write_dirmap()
3278 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); in spi_nor_create_write_dirmap()
3280 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) in spi_nor_create_write_dirmap()
3283 nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, in spi_nor_create_write_dirmap()
3285 return PTR_ERR_OR_ZERO(nor->dirmap.wdesc); in spi_nor_create_write_dirmap()
3292 struct spi_nor *nor; in spi_nor_probe() local
3301 nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL); in spi_nor_probe()
3302 if (!nor) in spi_nor_probe()
3305 nor->spimem = spimem; in spi_nor_probe()
3306 nor->dev = &spi->dev; in spi_nor_probe()
3307 spi_nor_set_flash_node(nor, spi->dev.of_node); in spi_nor_probe()
3309 spi_mem_set_drvdata(spimem, nor); in spi_nor_probe()
3312 nor->mtd.name = data->name; in spi_nor_probe()
3314 if (!nor->mtd.name) in spi_nor_probe()
3315 nor->mtd.name = spi_mem_get_name(spimem); in spi_nor_probe()
3325 else if (!strcmp(spi->modalias, "spi-nor")) in spi_nor_probe()
3330 ret = spi_nor_scan(nor, flash_name, &hwcaps); in spi_nor_probe()
3337 * a NOR we don't end up with buffer overflows. in spi_nor_probe()
3339 if (nor->page_size > PAGE_SIZE) { in spi_nor_probe()
3340 nor->bouncebuf_size = nor->page_size; in spi_nor_probe()
3341 devm_kfree(nor->dev, nor->bouncebuf); in spi_nor_probe()
3342 nor->bouncebuf = devm_kmalloc(nor->dev, in spi_nor_probe()
3343 nor->bouncebuf_size, in spi_nor_probe()
3345 if (!nor->bouncebuf) in spi_nor_probe()
3349 ret = spi_nor_create_read_dirmap(nor); in spi_nor_probe()
3353 ret = spi_nor_create_write_dirmap(nor); in spi_nor_probe()
3357 return mtd_device_register(&nor->mtd, data ? data->parts : NULL, in spi_nor_probe()
3363 struct spi_nor *nor = spi_mem_get_drvdata(spimem); in spi_nor_remove() local
3365 spi_nor_restore(nor); in spi_nor_remove()
3368 return mtd_device_unregister(&nor->mtd); in spi_nor_remove()
3373 struct spi_nor *nor = spi_mem_get_drvdata(spimem); in spi_nor_shutdown() local
3375 spi_nor_restore(nor); in spi_nor_shutdown()
3384 * encourage new users to add support to the spi-nor library, and simply bind
3385 * against a generic string here (e.g., "jedec,spi-nor").
3387 * Many flash names are kept here in this list (as well as in spi-nor.c) to
3392 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
3396 {"spi-nor"},
3400 * them with "spi-nor" in platform data.
3405 * Entries that were used in DTs without "jedec,spi-nor" fallback and
3437 * Generic compatibility for SPI NOR that can be identified by the
3440 { .compatible = "jedec,spi-nor" },
3453 .name = "spi-nor",
3467 MODULE_DESCRIPTION("framework for SPI NOR");