1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005, Intec Automation Inc.
4 * Copyright (C) 2014, Freescale Semiconductor, Inc.
7 #include <linux/mtd/spi-nor.h>
11 /* flash_info mfr_flag. Used to read proprietary FSR register. */
12 #define USE_FSR BIT(0)
14 #define SPINOR_OP_MT_DIE_ERASE 0xc4 /* Chip (die) erase opcode */
15 #define SPINOR_OP_RDFSR 0x70 /* Read flag status register */
16 #define SPINOR_OP_CLFSR 0x50 /* Clear flag status register */
17 #define SPINOR_OP_MT_DTR_RD 0xfd /* Fast Read opcode in DTR mode */
18 #define SPINOR_OP_MT_RD_ANY_REG 0x85 /* Read volatile register */
19 #define SPINOR_OP_MT_WR_ANY_REG 0x81 /* Write volatile register */
20 #define SPINOR_REG_MT_CFR0V 0x00 /* For setting octal DTR mode */
21 #define SPINOR_REG_MT_CFR1V 0x01 /* For setting dummy cycles */
22 #define SPINOR_REG_MT_CFR1V_DEF 0x1f /* Default dummy cycles */
23 #define SPINOR_MT_OCT_DTR 0xe7 /* Enable Octal DTR. */
24 #define SPINOR_MT_EXSPI 0xff /* Enable Extended SPI (default) */
26 /* Flag Status Register bits */
27 #define FSR_READY BIT(7) /* Device status, 0 = Busy, 1 = Ready */
28 #define FSR_E_ERR BIT(5) /* Erase operation status */
29 #define FSR_P_ERR BIT(4) /* Program operation status */
30 #define FSR_PT_ERR BIT(1) /* Protection error bit */
32 /* Micron ST SPI NOR flash operations. */
33 #define MICRON_ST_NOR_WR_ANY_REG_OP(naddr, addr, ndata, buf) \
34 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 0), \
35 SPI_MEM_OP_ADDR(naddr, addr, 0), \
36 SPI_MEM_OP_NO_DUMMY, \
37 SPI_MEM_OP_DATA_OUT(ndata, buf, 0))
39 #define MICRON_ST_RDFSR_OP(buf) \
40 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 0), \
42 SPI_MEM_OP_NO_DUMMY, \
43 SPI_MEM_OP_DATA_IN(1, buf, 0))
45 #define MICRON_ST_CLFSR_OP \
46 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLFSR, 0), \
48 SPI_MEM_OP_NO_DUMMY, \
51 static int micron_st_nor_octal_dtr_en(struct spi_nor *nor)
54 u8 *buf = nor->bouncebuf;
56 u8 addr_mode_nbytes = nor->params->addr_mode_nbytes;
58 /* Use 20 dummy cycles for memory array reads. */
60 op = (struct spi_mem_op)
61 MICRON_ST_NOR_WR_ANY_REG_OP(addr_mode_nbytes,
62 SPINOR_REG_MT_CFR1V, 1, buf);
63 ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto);
67 buf[0] = SPINOR_MT_OCT_DTR;
68 op = (struct spi_mem_op)
69 MICRON_ST_NOR_WR_ANY_REG_OP(addr_mode_nbytes,
70 SPINOR_REG_MT_CFR0V, 1, buf);
71 ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto);
75 /* Read flash ID to make sure the switch was successful. */
76 ret = spi_nor_read_id(nor, 0, 8, buf, SNOR_PROTO_8_8_8_DTR);
78 dev_dbg(nor->dev, "error %d reading JEDEC ID after enabling 8D-8D-8D mode\n", ret);
82 if (memcmp(buf, nor->info->id->bytes, nor->info->id->len))
88 static int micron_st_nor_octal_dtr_dis(struct spi_nor *nor)
91 u8 *buf = nor->bouncebuf;
95 * The register is 1-byte wide, but 1-byte transactions are not allowed
96 * in 8D-8D-8D mode. The next register is the dummy cycle configuration
97 * register. Since the transaction needs to be at least 2 bytes wide,
98 * set the next register to its default value. This also makes sense
99 * because the value was changed when enabling 8D-8D-8D mode, it should
100 * be reset when disabling.
102 buf[0] = SPINOR_MT_EXSPI;
103 buf[1] = SPINOR_REG_MT_CFR1V_DEF;
104 op = (struct spi_mem_op)
105 MICRON_ST_NOR_WR_ANY_REG_OP(nor->addr_nbytes,
106 SPINOR_REG_MT_CFR0V, 2, buf);
107 ret = spi_nor_write_any_volatile_reg(nor, &op, SNOR_PROTO_8_8_8_DTR);
111 /* Read flash ID to make sure the switch was successful. */
112 ret = spi_nor_read_id(nor, 0, 0, buf, SNOR_PROTO_1_1_1);
114 dev_dbg(nor->dev, "error %d reading JEDEC ID after disabling 8D-8D-8D mode\n", ret);
118 if (memcmp(buf, nor->info->id->bytes, nor->info->id->len))
124 static int micron_st_nor_set_octal_dtr(struct spi_nor *nor, bool enable)
126 return enable ? micron_st_nor_octal_dtr_en(nor) :
127 micron_st_nor_octal_dtr_dis(nor);
130 static void mt35xu512aba_default_init(struct spi_nor *nor)
132 nor->params->set_octal_dtr = micron_st_nor_set_octal_dtr;
135 static int mt35xu512aba_post_sfdp_fixup(struct spi_nor *nor)
137 /* Set the Fast Read settings. */
138 nor->params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
139 spi_nor_set_read_settings(&nor->params->reads[SNOR_CMD_READ_8_8_8_DTR],
140 0, 20, SPINOR_OP_MT_DTR_RD,
141 SNOR_PROTO_8_8_8_DTR);
143 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
144 nor->params->rdsr_dummy = 8;
145 nor->params->rdsr_addr_nbytes = 0;
148 * The BFPT quad enable field is set to a reserved value so the quad
149 * enable function is ignored by spi_nor_parse_bfpt(). Make sure we
152 nor->params->quad_enable = NULL;
157 static const struct spi_nor_fixups mt35xu512aba_fixups = {
158 .default_init = mt35xu512aba_default_init,
159 .post_sfdp = mt35xu512aba_post_sfdp_fixup,
162 static const struct flash_info micron_nor_parts[] = {
164 .id = SNOR_ID(0x2c, 0x5b, 0x1a),
165 .name = "mt35xu512aba",
166 .sector_size = SZ_128K,
168 .no_sfdp_flags = SECT_4K | SPI_NOR_OCTAL_READ |
169 SPI_NOR_OCTAL_DTR_READ | SPI_NOR_OCTAL_DTR_PP,
170 .mfr_flags = USE_FSR,
171 .fixup_flags = SPI_NOR_4B_OPCODES | SPI_NOR_IO_MODE_EN_VOLATILE,
172 .fixups = &mt35xu512aba_fixups,
174 .id = SNOR_ID(0x2c, 0x5b, 0x1c),
176 .sector_size = SZ_128K,
178 .no_sfdp_flags = SECT_4K | SPI_NOR_OCTAL_READ,
179 .mfr_flags = USE_FSR,
180 .fixup_flags = SPI_NOR_4B_OPCODES,
184 static int mt25qu512a_post_bfpt_fixup(struct spi_nor *nor,
185 const struct sfdp_parameter_header *bfpt_header,
186 const struct sfdp_bfpt *bfpt)
188 nor->flags &= ~SNOR_F_HAS_16BIT_SR;
192 static struct spi_nor_fixups mt25qu512a_fixups = {
193 .post_bfpt = mt25qu512a_post_bfpt_fixup,
196 static int st_nor_four_die_late_init(struct spi_nor *nor)
198 struct spi_nor_flash_parameter *params = nor->params;
200 params->die_erase_opcode = SPINOR_OP_MT_DIE_ERASE;
204 * Unfortunately the die erase opcode does not have a 4-byte opcode
205 * correspondent for these flashes. The SFDP 4BAIT table fails to
206 * consider the die erase too. We're forced to enter in the 4 byte
207 * address mode in order to benefit of the die erase.
209 return spi_nor_set_4byte_addr_mode(nor, true);
212 static int st_nor_two_die_late_init(struct spi_nor *nor)
214 struct spi_nor_flash_parameter *params = nor->params;
216 params->die_erase_opcode = SPINOR_OP_MT_DIE_ERASE;
220 * Unfortunately the die erase opcode does not have a 4-byte opcode
221 * correspondent for these flashes. The SFDP 4BAIT table fails to
222 * consider the die erase too. We're forced to enter in the 4 byte
223 * address mode in order to benefit of the die erase.
225 return spi_nor_set_4byte_addr_mode(nor, true);
228 static struct spi_nor_fixups n25q00_fixups = {
229 .late_init = st_nor_four_die_late_init,
232 static struct spi_nor_fixups mt25q01_fixups = {
233 .late_init = st_nor_two_die_late_init,
236 static struct spi_nor_fixups mt25q02_fixups = {
237 .late_init = st_nor_four_die_late_init,
240 static const struct flash_info st_nor_parts[] = {
242 .name = "m25p05-nonjedec",
243 .sector_size = SZ_32K,
246 .name = "m25p10-nonjedec",
247 .sector_size = SZ_32K,
250 .name = "m25p20-nonjedec",
253 .name = "m25p40-nonjedec",
256 .name = "m25p80-nonjedec",
259 .name = "m25p16-nonjedec",
262 .name = "m25p32-nonjedec",
265 .name = "m25p64-nonjedec",
268 .name = "m25p128-nonjedec",
269 .sector_size = SZ_256K,
272 .id = SNOR_ID(0x20, 0x20, 0x10),
274 .sector_size = SZ_32K,
277 .id = SNOR_ID(0x20, 0x20, 0x11),
279 .sector_size = SZ_32K,
282 .id = SNOR_ID(0x20, 0x20, 0x12),
286 .id = SNOR_ID(0x20, 0x20, 0x13),
290 .id = SNOR_ID(0x20, 0x20, 0x14),
294 .id = SNOR_ID(0x20, 0x20, 0x15),
298 .id = SNOR_ID(0x20, 0x20, 0x16),
302 .id = SNOR_ID(0x20, 0x20, 0x17),
306 .id = SNOR_ID(0x20, 0x20, 0x18),
308 .sector_size = SZ_256K,
311 .id = SNOR_ID(0x20, 0x40, 0x11),
315 .id = SNOR_ID(0x20, 0x40, 0x14),
319 .id = SNOR_ID(0x20, 0x40, 0x15),
323 .id = SNOR_ID(0x20, 0x63, 0x16),
324 .name = "m25px32-s1",
326 .no_sfdp_flags = SECT_4K,
328 .id = SNOR_ID(0x20, 0x71, 0x14),
332 .id = SNOR_ID(0x20, 0x71, 0x15),
335 .no_sfdp_flags = SECT_4K,
337 .id = SNOR_ID(0x20, 0x71, 0x16),
340 .no_sfdp_flags = SECT_4K,
342 .id = SNOR_ID(0x20, 0x71, 0x17),
346 .id = SNOR_ID(0x20, 0x73, 0x16),
347 .name = "m25px32-s0",
349 .no_sfdp_flags = SECT_4K,
351 .id = SNOR_ID(0x20, 0x80, 0x12),
355 .id = SNOR_ID(0x20, 0x80, 0x14),
359 .id = SNOR_ID(0x20, 0x80, 0x15),
362 .no_sfdp_flags = SECT_4K,
364 .id = SNOR_ID(0x20, 0xba, 0x16),
367 .no_sfdp_flags = SPI_NOR_QUAD_READ,
369 .id = SNOR_ID(0x20, 0xba, 0x17),
372 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
374 .id = SNOR_ID(0x20, 0xba, 0x18),
375 .name = "n25q128a13",
377 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP |
379 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
380 .mfr_flags = USE_FSR,
382 .id = SNOR_ID(0x20, 0xba, 0x19, 0x10, 0x44, 0x00),
383 .name = "mt25ql256a",
385 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
386 .fixup_flags = SPI_NOR_4B_OPCODES,
387 .mfr_flags = USE_FSR,
389 .id = SNOR_ID(0x20, 0xba, 0x19),
392 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
393 .mfr_flags = USE_FSR,
395 .id = SNOR_ID(0x20, 0xba, 0x20, 0x10, 0x44, 0x00),
396 .name = "mt25ql512a",
398 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
399 .fixup_flags = SPI_NOR_4B_OPCODES,
400 .mfr_flags = USE_FSR,
402 .id = SNOR_ID(0x20, 0xba, 0x20),
403 .name = "n25q512ax3",
405 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP |
407 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
408 .mfr_flags = USE_FSR,
410 .id = SNOR_ID(0x20, 0xba, 0x21),
413 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP |
415 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
416 .mfr_flags = USE_FSR,
417 .fixups = &n25q00_fixups,
419 .id = SNOR_ID(0x20, 0xba, 0x22),
422 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
423 .mfr_flags = USE_FSR,
424 .fixups = &mt25q02_fixups,
426 .id = SNOR_ID(0x20, 0xbb, 0x15),
429 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
431 .id = SNOR_ID(0x20, 0xbb, 0x16),
434 .no_sfdp_flags = SPI_NOR_QUAD_READ,
436 .id = SNOR_ID(0x20, 0xbb, 0x17),
439 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
441 .id = SNOR_ID(0x20, 0xbb, 0x18),
442 .name = "n25q128a11",
444 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP |
446 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
447 .mfr_flags = USE_FSR,
449 .id = SNOR_ID(0x20, 0xbb, 0x19, 0x10, 0x44, 0x00),
450 .name = "mt25qu256a",
452 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP |
454 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
455 .fixup_flags = SPI_NOR_4B_OPCODES,
456 .mfr_flags = USE_FSR,
458 .id = SNOR_ID(0x20, 0xbb, 0x19),
459 .name = "n25q256ax1",
461 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
462 .mfr_flags = USE_FSR,
464 .id = SNOR_ID(0x20, 0xbb, 0x20, 0x10, 0x44, 0x00),
465 .name = "mt25qu512a",
466 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP |
468 .mfr_flags = USE_FSR,
469 .fixups = &mt25qu512a_fixups,
471 .id = SNOR_ID(0x20, 0xbb, 0x20),
474 .flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP |
476 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
477 .mfr_flags = USE_FSR,
479 .id = SNOR_ID(0x20, 0xbb, 0x21, 0x10, 0x44, 0x00),
481 .mfr_flags = USE_FSR,
482 .fixups = &mt25q01_fixups,
484 .id = SNOR_ID(0x20, 0xbb, 0x21),
487 .no_sfdp_flags = SECT_4K | SPI_NOR_QUAD_READ,
488 .mfr_flags = USE_FSR,
489 .fixups = &n25q00_fixups,
491 .id = SNOR_ID(0x20, 0xbb, 0x22),
494 .no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
495 .mfr_flags = USE_FSR,
496 .fixups = &mt25q02_fixups,
501 * micron_st_nor_read_fsr() - Read the Flag Status Register.
502 * @nor: pointer to 'struct spi_nor'
503 * @fsr: pointer to a DMA-able buffer where the value of the
504 * Flag Status Register will be written. Should be at least 2
507 * Return: 0 on success, -errno otherwise.
509 static int micron_st_nor_read_fsr(struct spi_nor *nor, u8 *fsr)
514 struct spi_mem_op op = MICRON_ST_RDFSR_OP(fsr);
516 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
517 op.addr.nbytes = nor->params->rdsr_addr_nbytes;
518 op.dummy.nbytes = nor->params->rdsr_dummy;
520 * We don't want to read only one byte in DTR mode. So,
521 * read 2 and then discard the second byte.
526 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
528 ret = spi_mem_exec_op(nor->spimem, &op);
530 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDFSR, fsr,
535 dev_dbg(nor->dev, "error %d reading FSR\n", ret);
541 * micron_st_nor_clear_fsr() - Clear the Flag Status Register.
542 * @nor: pointer to 'struct spi_nor'.
544 static void micron_st_nor_clear_fsr(struct spi_nor *nor)
549 struct spi_mem_op op = MICRON_ST_CLFSR_OP;
551 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
553 ret = spi_mem_exec_op(nor->spimem, &op);
555 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_CLFSR,
560 dev_dbg(nor->dev, "error %d clearing FSR\n", ret);
564 * micron_st_nor_ready() - Query the Status Register as well as the Flag Status
565 * Register to see if the flash is ready for new commands. If there are any
566 * errors in the FSR clear them.
567 * @nor: pointer to 'struct spi_nor'.
569 * Return: 1 if ready, 0 if not ready, -errno on errors.
571 static int micron_st_nor_ready(struct spi_nor *nor)
575 sr_ready = spi_nor_sr_ready(nor);
579 ret = micron_st_nor_read_fsr(nor, nor->bouncebuf);
582 * Some controllers, such as Intel SPI, do not support low
583 * level operations such as reading the flag status
584 * register. They only expose small amount of high level
585 * operations to the software. If this is the case we use
586 * only the status register value.
588 return ret == -EOPNOTSUPP ? sr_ready : ret;
591 if (nor->bouncebuf[0] & (FSR_E_ERR | FSR_P_ERR)) {
592 if (nor->bouncebuf[0] & FSR_E_ERR)
593 dev_err(nor->dev, "Erase operation failed.\n");
595 dev_err(nor->dev, "Program operation failed.\n");
597 if (nor->bouncebuf[0] & FSR_PT_ERR)
599 "Attempted to modify a protected sector.\n");
601 micron_st_nor_clear_fsr(nor);
604 * WEL bit remains set to one when an erase or page program
605 * error occurs. Issue a Write Disable command to protect
606 * against inadvertent writes that can possibly corrupt the
607 * contents of the memory.
609 ret = spi_nor_write_disable(nor);
616 return sr_ready && !!(nor->bouncebuf[0] & FSR_READY);
619 static void micron_st_nor_default_init(struct spi_nor *nor)
621 nor->flags |= SNOR_F_HAS_LOCK;
622 nor->flags &= ~SNOR_F_HAS_16BIT_SR;
623 nor->params->quad_enable = NULL;
626 static int micron_st_nor_late_init(struct spi_nor *nor)
628 struct spi_nor_flash_parameter *params = nor->params;
630 if (nor->info->mfr_flags & USE_FSR)
631 params->ready = micron_st_nor_ready;
633 if (!params->set_4byte_addr_mode)
634 params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_wren_en4b_ex4b;
639 static const struct spi_nor_fixups micron_st_nor_fixups = {
640 .default_init = micron_st_nor_default_init,
641 .late_init = micron_st_nor_late_init,
644 const struct spi_nor_manufacturer spi_nor_micron = {
646 .parts = micron_nor_parts,
647 .nparts = ARRAY_SIZE(micron_nor_parts),
648 .fixups = µn_st_nor_fixups,
651 const struct spi_nor_manufacturer spi_nor_st = {
653 .parts = st_nor_parts,
654 .nparts = ARRAY_SIZE(st_nor_parts),
655 .fixups = µn_st_nor_fixups,