Skip to content

Commit 4629ada

Browse files
ambarusPratyush Yadav
authored andcommitted
mtd: spi-nor: micron-st: Rework spi_nor_micron_octal_dtr_enable()
Introduce template operation to remove code duplication. Split spi_nor_micron_octal_dtr_enable() in spi_nor_micron_octal_dtr_en() and spi_nor_micron_octal_dtr_dis() as it no longer made sense to try to keep everything alltogether: too many "if (enable)" throughout the code, which made the code difficult to follow. Add dev_dbg messages in case spi_nor_read_id() fails. Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com> Signed-off-by: Pratyush Yadav <p.yadav@ti.com> Reviewed-by: Pratyush Yadav <p.yadav@ti.com> Reviewed-by: Michael Walle <michael@walle.cc> Link: https://lore.kernel.org/r/20220420103427.47867-8-tudor.ambarus@microchip.com
1 parent a604ab3 commit 4629ada

1 file changed

Lines changed: 60 additions & 51 deletions

File tree

drivers/mtd/spi-nor/micron-st.c

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,82 +28,91 @@
2828
#define FSR_P_ERR BIT(4) /* Program operation status */
2929
#define FSR_PT_ERR BIT(1) /* Protection error bit */
3030

31-
static int micron_st_nor_octal_dtr_enable(struct spi_nor *nor, bool enable)
31+
/* Micron ST SPI NOR flash operations. */
32+
#define MICRON_ST_NOR_WR_ANY_REG_OP(naddr, addr, ndata, buf) \
33+
SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 0), \
34+
SPI_MEM_OP_ADDR(naddr, addr, 0), \
35+
SPI_MEM_OP_NO_DUMMY, \
36+
SPI_MEM_OP_DATA_OUT(ndata, buf, 0))
37+
38+
static int micron_st_nor_octal_dtr_en(struct spi_nor *nor)
3239
{
3340
struct spi_mem_op op;
3441
u8 *buf = nor->bouncebuf;
3542
int ret;
3643

37-
if (enable) {
38-
/* Use 20 dummy cycles for memory array reads. */
39-
ret = spi_nor_write_enable(nor);
40-
if (ret)
41-
return ret;
42-
43-
*buf = 20;
44-
op = (struct spi_mem_op)
45-
SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 1),
46-
SPI_MEM_OP_ADDR(3, SPINOR_REG_MT_CFR1V, 1),
47-
SPI_MEM_OP_NO_DUMMY,
48-
SPI_MEM_OP_DATA_OUT(1, buf, 1));
49-
50-
ret = spi_mem_exec_op(nor->spimem, &op);
51-
if (ret)
52-
return ret;
53-
54-
ret = spi_nor_wait_till_ready(nor);
55-
if (ret)
56-
return ret;
57-
}
44+
/* Use 20 dummy cycles for memory array reads. */
45+
*buf = 20;
46+
op = (struct spi_mem_op)
47+
MICRON_ST_NOR_WR_ANY_REG_OP(3, SPINOR_REG_MT_CFR1V, 1, buf);
48+
ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto);
49+
if (ret)
50+
return ret;
51+
ret = spi_nor_wait_till_ready(nor);
52+
if (ret)
53+
return ret;
5854

59-
ret = spi_nor_write_enable(nor);
55+
buf[0] = SPINOR_MT_OCT_DTR;
56+
op = (struct spi_mem_op)
57+
MICRON_ST_NOR_WR_ANY_REG_OP(3, SPINOR_REG_MT_CFR0V, 1, buf);
58+
ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto);
6059
if (ret)
6160
return ret;
6261

63-
if (enable) {
64-
buf[0] = SPINOR_MT_OCT_DTR;
65-
} else {
66-
/*
67-
* The register is 1-byte wide, but 1-byte transactions are not
68-
* allowed in 8D-8D-8D mode. The next register is the dummy
69-
* cycle configuration register. Since the transaction needs to
70-
* be at least 2 bytes wide, set the next register to its
71-
* default value. This also makes sense because the value was
72-
* changed when enabling 8D-8D-8D mode, it should be reset when
73-
* disabling.
74-
*/
75-
buf[0] = SPINOR_MT_EXSPI;
76-
buf[1] = SPINOR_REG_MT_CFR1V_DEF;
62+
/* Read flash ID to make sure the switch was successful. */
63+
ret = spi_nor_read_id(nor, 0, 8, buf, SNOR_PROTO_8_8_8_DTR);
64+
if (ret) {
65+
dev_dbg(nor->dev, "error %d reading JEDEC ID after enabling 8D-8D-8D mode\n", ret);
66+
return ret;
7767
}
7868

79-
op = (struct spi_mem_op)
80-
SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 1),
81-
SPI_MEM_OP_ADDR(enable ? 3 : 4,
82-
SPINOR_REG_MT_CFR0V, 1),
83-
SPI_MEM_OP_NO_DUMMY,
84-
SPI_MEM_OP_DATA_OUT(enable ? 1 : 2, buf, 1));
69+
if (memcmp(buf, nor->info->id, nor->info->id_len))
70+
return -EINVAL;
8571

86-
if (!enable)
87-
spi_nor_spimem_setup_op(nor, &op, SNOR_PROTO_8_8_8_DTR);
72+
return 0;
73+
}
74+
75+
static int micron_st_nor_octal_dtr_dis(struct spi_nor *nor)
76+
{
77+
struct spi_mem_op op;
78+
u8 *buf = nor->bouncebuf;
79+
int ret;
8880

89-
ret = spi_mem_exec_op(nor->spimem, &op);
81+
/*
82+
* The register is 1-byte wide, but 1-byte transactions are not allowed
83+
* in 8D-8D-8D mode. The next register is the dummy cycle configuration
84+
* register. Since the transaction needs to be at least 2 bytes wide,
85+
* set the next register to its default value. This also makes sense
86+
* because the value was changed when enabling 8D-8D-8D mode, it should
87+
* be reset when disabling.
88+
*/
89+
buf[0] = SPINOR_MT_EXSPI;
90+
buf[1] = SPINOR_REG_MT_CFR1V_DEF;
91+
op = (struct spi_mem_op)
92+
MICRON_ST_NOR_WR_ANY_REG_OP(4, SPINOR_REG_MT_CFR0V, 2, buf);
93+
ret = spi_nor_write_any_volatile_reg(nor, &op, SNOR_PROTO_8_8_8_DTR);
9094
if (ret)
9195
return ret;
9296

9397
/* Read flash ID to make sure the switch was successful. */
94-
if (enable)
95-
ret = spi_nor_read_id(nor, 0, 8, buf, SNOR_PROTO_8_8_8_DTR);
96-
else
97-
ret = spi_nor_read_id(nor, 0, 0, buf, SNOR_PROTO_1_1_1);
98-
if (ret)
98+
ret = spi_nor_read_id(nor, 0, 0, buf, SNOR_PROTO_1_1_1);
99+
if (ret) {
100+
dev_dbg(nor->dev, "error %d reading JEDEC ID after disabling 8D-8D-8D mode\n", ret);
99101
return ret;
102+
}
100103

101104
if (memcmp(buf, nor->info->id, nor->info->id_len))
102105
return -EINVAL;
103106

104107
return 0;
105108
}
106109

110+
static int micron_st_nor_octal_dtr_enable(struct spi_nor *nor, bool enable)
111+
{
112+
return enable ? micron_st_nor_octal_dtr_en(nor) :
113+
micron_st_nor_octal_dtr_dis(nor);
114+
}
115+
107116
static void mt35xu512aba_default_init(struct spi_nor *nor)
108117
{
109118
nor->params->octal_dtr_enable = micron_st_nor_octal_dtr_enable;

0 commit comments

Comments
 (0)