Skip to content

Commit 34267d3

Browse files
committed
Merge tag 'spi-nor/for-6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux into mtd/next
SPI NOR introduces byte swap support for 8D-8D-8D mode and a user for it: macronix. SPI NOR flashes may swap the bytes on a 16-bit boundary when configured in Octal DTR mode. For such cases the byte order is propagated through SPI MEM to the SPI controllers so that the controllers swap the bytes back at runtime. This avoids breaking the boot sequence because of the endianness problems that appear when the bootloaders use 1-1-1 and the kernel uses 8D-8D-8D with byte swap support. Along with the SPI MEM byte swap support we queue a patch for the SPI MXIC controller that swaps the bytes back at runtime. # -----BEGIN PGP SIGNATURE----- # # iQEzBAABCgAdFiEEHUIqys8OyG1eHf7fS1VPR6WNFOkFAmczG/oACgkQS1VPR6WN # FOnTCAf/YjH9AimQAFJLRKoGqsf6boh1JppcRh1YTRS+D6+Ap9+s1gJoZZYs5VWA # vNfBzGqDXknBvpmOgoXnuDu2zFs9FUvdN5Kf7w6LiS5qtz7uOxHdVoDQyDgnN6w5 # 9ts7qF7LViBHg/HgTEzQT2Zj6qmvIwUbccIkmJeehWjEP/urzOML5nPnM9g4HZVB # W8B5KQ4TiOY1GxkXvIP6EQS6mDKznP3yl2Hnsmk0HPpSm6D807O2zvT+z1SCxpjy # C8+mYRKsRxHoFGL6UzWgqREGBn2wzF7Ral1CR9SpSZZLLtr6S0shqRzKiiH8eiZK # 1hFpXzMS3OWi4a/5724AWaqcR0Qgqw== # =jf3Q # -----END PGP SIGNATURE----- # gpg: Signature made Tue 12 Nov 2024 10:12:26 AM CET # gpg: using RSA key 1D422ACACF0EC86D5E1DFEDF4B554F47A58D14E9 # gpg: Good signature from "Tudor Ambarus (4096-bit rsa key) <[email protected]>" [full] # gpg: aka "Tudor Ambarus <[email protected]>" [full] # gpg: [email protected]: Verified 15 signatures in the past 5 years. # Encrypted 0 messages. # gpg: [email protected]: Verified 15 signatures in the past 5 years. # Encrypted 0 messages.
2 parents f847000 + 98d1fb9 commit 34267d3

File tree

10 files changed

+133
-7
lines changed

10 files changed

+133
-7
lines changed

drivers/mtd/spi-nor/core.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void spi_nor_spimem_setup_op(const struct spi_nor *nor,
8989
op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
9090

9191
if (op->dummy.nbytes)
92-
op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
92+
op->dummy.buswidth = spi_nor_get_protocol_data_nbits(proto);
9393

9494
if (op->data.nbytes)
9595
op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
@@ -113,6 +113,9 @@ void spi_nor_spimem_setup_op(const struct spi_nor *nor,
113113
op->cmd.opcode = (op->cmd.opcode << 8) | ext;
114114
op->cmd.nbytes = 2;
115115
}
116+
117+
if (proto == SNOR_PROTO_8_8_8_DTR && nor->flags & SNOR_F_SWAP16)
118+
op->data.swap16 = true;
116119
}
117120

118121
/**

drivers/mtd/spi-nor/core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ enum spi_nor_option_flags {
140140
SNOR_F_RWW = BIT(14),
141141
SNOR_F_ECC = BIT(15),
142142
SNOR_F_NO_WP = BIT(16),
143+
SNOR_F_SWAP16 = BIT(17),
143144
};
144145

145146
struct spi_nor_read_command {

drivers/mtd/spi-nor/macronix.c

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@
88

99
#include "core.h"
1010

11+
#define MXIC_NOR_OP_RD_CR2 0x71 /* Read configuration register 2 opcode */
12+
#define MXIC_NOR_OP_WR_CR2 0x72 /* Write configuration register 2 opcode */
13+
#define MXIC_NOR_ADDR_CR2_MODE 0x00000000 /* CR2 address for setting spi/sopi/dopi mode */
14+
#define MXIC_NOR_ADDR_CR2_DC 0x00000300 /* CR2 address for setting dummy cycles */
15+
#define MXIC_NOR_REG_DOPI_EN 0x2 /* Enable Octal DTR */
16+
#define MXIC_NOR_REG_SPI_EN 0x0 /* Enable SPI */
17+
18+
/* Convert dummy cycles to bit pattern */
19+
#define MXIC_NOR_REG_DC(p) \
20+
((20 - (p)) >> 1)
21+
22+
#define MXIC_NOR_WR_CR2(addr, ndata, buf) \
23+
SPI_MEM_OP(SPI_MEM_OP_CMD(MXIC_NOR_OP_WR_CR2, 0), \
24+
SPI_MEM_OP_ADDR(4, addr, 0), \
25+
SPI_MEM_OP_NO_DUMMY, \
26+
SPI_MEM_OP_DATA_OUT(ndata, buf, 0))
27+
1128
static int
1229
mx25l25635_post_bfpt_fixups(struct spi_nor *nor,
1330
const struct sfdp_parameter_header *bfpt_header,
@@ -182,9 +199,88 @@ static const struct flash_info macronix_nor_parts[] = {
182199
.name = "mx25l3255e",
183200
.size = SZ_4M,
184201
.no_sfdp_flags = SECT_4K,
185-
}
202+
},
203+
/*
204+
* This spares us of adding new flash entries for flashes that can be
205+
* initialized solely based on the SFDP data, but still need the
206+
* manufacturer hooks to set parameters that can't be discovered at SFDP
207+
* parsing time.
208+
*/
209+
{ .id = SNOR_ID(0xc2) }
186210
};
187211

212+
static int macronix_nor_octal_dtr_en(struct spi_nor *nor)
213+
{
214+
struct spi_mem_op op;
215+
u8 *buf = nor->bouncebuf, i;
216+
int ret;
217+
218+
/* Use dummy cycles which is parse by SFDP and convert to bit pattern. */
219+
buf[0] = MXIC_NOR_REG_DC(nor->params->reads[SNOR_CMD_READ_8_8_8_DTR].num_wait_states);
220+
op = (struct spi_mem_op)MXIC_NOR_WR_CR2(MXIC_NOR_ADDR_CR2_DC, 1, buf);
221+
ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto);
222+
if (ret)
223+
return ret;
224+
225+
/* Set the octal and DTR enable bits. */
226+
buf[0] = MXIC_NOR_REG_DOPI_EN;
227+
op = (struct spi_mem_op)MXIC_NOR_WR_CR2(MXIC_NOR_ADDR_CR2_MODE, 1, buf);
228+
ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto);
229+
if (ret)
230+
return ret;
231+
232+
/* Read flash ID to make sure the switch was successful. */
233+
ret = spi_nor_read_id(nor, 4, 4, buf, SNOR_PROTO_8_8_8_DTR);
234+
if (ret) {
235+
dev_dbg(nor->dev, "error %d reading JEDEC ID after enabling 8D-8D-8D mode\n", ret);
236+
return ret;
237+
}
238+
239+
/* Macronix SPI-NOR flash 8D-8D-8D read ID would get 6 bytes data A-A-B-B-C-C */
240+
for (i = 0; i < nor->info->id->len; i++)
241+
if (buf[i * 2] != buf[(i * 2) + 1] || buf[i * 2] != nor->info->id->bytes[i])
242+
return -EINVAL;
243+
244+
return 0;
245+
}
246+
247+
static int macronix_nor_octal_dtr_dis(struct spi_nor *nor)
248+
{
249+
struct spi_mem_op op;
250+
u8 *buf = nor->bouncebuf;
251+
int ret;
252+
253+
/*
254+
* The register is 1-byte wide, but 1-byte transactions are not
255+
* allowed in 8D-8D-8D mode. Since there is no register at the
256+
* next location, just initialize the value to 0 and let the
257+
* transaction go on.
258+
*/
259+
buf[0] = MXIC_NOR_REG_SPI_EN;
260+
buf[1] = 0x0;
261+
op = (struct spi_mem_op)MXIC_NOR_WR_CR2(MXIC_NOR_ADDR_CR2_MODE, 2, buf);
262+
ret = spi_nor_write_any_volatile_reg(nor, &op, SNOR_PROTO_8_8_8_DTR);
263+
if (ret)
264+
return ret;
265+
266+
/* Read flash ID to make sure the switch was successful. */
267+
ret = spi_nor_read_id(nor, 0, 0, buf, SNOR_PROTO_1_1_1);
268+
if (ret) {
269+
dev_dbg(nor->dev, "error %d reading JEDEC ID after disabling 8D-8D-8D mode\n", ret);
270+
return ret;
271+
}
272+
273+
if (memcmp(buf, nor->info->id->bytes, nor->info->id->len))
274+
return -EINVAL;
275+
276+
return 0;
277+
}
278+
279+
static int macronix_nor_set_octal_dtr(struct spi_nor *nor, bool enable)
280+
{
281+
return enable ? macronix_nor_octal_dtr_en(nor) : macronix_nor_octal_dtr_dis(nor);
282+
}
283+
188284
static void macronix_nor_default_init(struct spi_nor *nor)
189285
{
190286
nor->params->quad_enable = spi_nor_sr1_bit6_quad_enable;
@@ -194,6 +290,7 @@ static int macronix_nor_late_init(struct spi_nor *nor)
194290
{
195291
if (!nor->params->set_4byte_addr_mode)
196292
nor->params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_en4b_ex4b;
293+
nor->params->set_octal_dtr = macronix_nor_set_octal_dtr;
197294

198295
return 0;
199296
}

drivers/mtd/spi-nor/sfdp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,10 @@ static int spi_nor_parse_bfpt(struct spi_nor *nor,
671671
return -EOPNOTSUPP;
672672
}
673673

674+
/* Byte order in 8D-8D-8D mode */
675+
if (bfpt.dwords[SFDP_DWORD(18)] & BFPT_DWORD18_BYTE_ORDER_SWAPPED)
676+
nor->flags |= SNOR_F_SWAP16;
677+
674678
return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt);
675679
}
676680

drivers/mtd/spi-nor/sfdp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ struct sfdp_bfpt {
130130
#define BFPT_DWORD18_CMD_EXT_INV (0x1UL << 29) /* Invert */
131131
#define BFPT_DWORD18_CMD_EXT_RES (0x2UL << 29) /* Reserved */
132132
#define BFPT_DWORD18_CMD_EXT_16B (0x3UL << 29) /* 16-bit opcode */
133+
#define BFPT_DWORD18_BYTE_ORDER_SWAPPED BIT(31) /* Byte order swapped in 8D-8D-8D mode */
133134

134135
struct sfdp_parameter_header {
135136
u8 id_lsb;

drivers/mtd/spi-nor/spansion.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ static int cypress_nor_sr_ready_and_clear_reg(struct spi_nor *nor, u64 addr)
106106
int ret;
107107

108108
if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
109+
op.addr.nbytes = nor->addr_nbytes;
109110
op.dummy.nbytes = params->rdsr_dummy;
110111
op.data.nbytes = 2;
111112
}

drivers/mtd/spi-nor/winbond.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ static const struct flash_info winbond_nor_parts[] = {
129129
.no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
130130
}, {
131131
.id = SNOR_ID(0xef, 0x40, 0x18),
132+
/* Flavors w/ and w/o SFDP. */
132133
.name = "w25q128",
133134
.size = SZ_16M,
134135
.flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,

drivers/spi/spi-mem.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ bool spi_mem_default_supports_op(struct spi_mem *mem,
172172
if (!spi_mem_controller_is_capable(ctlr, dtr))
173173
return false;
174174

175+
if (op->data.swap16 && !spi_mem_controller_is_capable(ctlr, swap16))
176+
return false;
177+
175178
if (op->cmd.nbytes != 2)
176179
return false;
177180
} else {

drivers/spi/spi-mxic.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ static void mxic_spi_hw_init(struct mxic_spi *mxic)
294294
mxic->regs + HC_CFG);
295295
}
296296

297-
static u32 mxic_spi_prep_hc_cfg(struct spi_device *spi, u32 flags)
297+
static u32 mxic_spi_prep_hc_cfg(struct spi_device *spi, u32 flags,
298+
bool swap16)
298299
{
299300
int nio = 1;
300301

@@ -305,6 +306,11 @@ static u32 mxic_spi_prep_hc_cfg(struct spi_device *spi, u32 flags)
305306
else if (spi->mode & (SPI_TX_DUAL | SPI_RX_DUAL))
306307
nio = 2;
307308

309+
if (swap16)
310+
flags &= ~HC_CFG_DATA_PASS;
311+
else
312+
flags |= HC_CFG_DATA_PASS;
313+
308314
return flags | HC_CFG_NIO(nio) |
309315
HC_CFG_TYPE(spi_get_chipselect(spi, 0), HC_CFG_TYPE_SPI_NOR) |
310316
HC_CFG_SLV_ACT(spi_get_chipselect(spi, 0)) | HC_CFG_IDLE_SIO_LVL(1);
@@ -397,7 +403,8 @@ static ssize_t mxic_spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
397403
if (WARN_ON(offs + desc->info.offset + len > U32_MAX))
398404
return -EINVAL;
399405

400-
writel(mxic_spi_prep_hc_cfg(desc->mem->spi, 0), mxic->regs + HC_CFG);
406+
writel(mxic_spi_prep_hc_cfg(desc->mem->spi, 0, desc->info.op_tmpl.data.swap16),
407+
mxic->regs + HC_CFG);
401408

402409
writel(mxic_spi_mem_prep_op_cfg(&desc->info.op_tmpl, len),
403410
mxic->regs + LRD_CFG);
@@ -441,7 +448,8 @@ static ssize_t mxic_spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
441448
if (WARN_ON(offs + desc->info.offset + len > U32_MAX))
442449
return -EINVAL;
443450

444-
writel(mxic_spi_prep_hc_cfg(desc->mem->spi, 0), mxic->regs + HC_CFG);
451+
writel(mxic_spi_prep_hc_cfg(desc->mem->spi, 0, desc->info.op_tmpl.data.swap16),
452+
mxic->regs + HC_CFG);
445453

446454
writel(mxic_spi_mem_prep_op_cfg(&desc->info.op_tmpl, len),
447455
mxic->regs + LWR_CFG);
@@ -518,7 +526,7 @@ static int mxic_spi_mem_exec_op(struct spi_mem *mem,
518526
if (ret)
519527
return ret;
520528

521-
writel(mxic_spi_prep_hc_cfg(mem->spi, HC_CFG_MAN_CS_EN),
529+
writel(mxic_spi_prep_hc_cfg(mem->spi, HC_CFG_MAN_CS_EN, op->data.swap16),
522530
mxic->regs + HC_CFG);
523531

524532
writel(HC_EN_BIT, mxic->regs + HC_EN);
@@ -573,6 +581,7 @@ static const struct spi_controller_mem_ops mxic_spi_mem_ops = {
573581
static const struct spi_controller_mem_caps mxic_spi_mem_caps = {
574582
.dtr = true,
575583
.ecc = true,
584+
.swap16 = true,
576585
};
577586

578587
static void mxic_spi_set_cs(struct spi_device *spi, bool lvl)

include/linux/spi/spi-mem.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ enum spi_mem_data_dir {
9090
* @data.buswidth: number of IO lanes used to send/receive the data
9191
* @data.dtr: whether the data should be sent in DTR mode or not
9292
* @data.ecc: whether error correction is required or not
93+
* @data.swap16: whether the byte order of 16-bit words is swapped when read
94+
* or written in Octal DTR mode compared to STR mode.
9395
* @data.dir: direction of the transfer
9496
* @data.nbytes: number of data bytes to send/receive. Can be zero if the
9597
* operation does not involve transferring data
@@ -124,7 +126,8 @@ struct spi_mem_op {
124126
u8 buswidth;
125127
u8 dtr : 1;
126128
u8 ecc : 1;
127-
u8 __pad : 6;
129+
u8 swap16 : 1;
130+
u8 __pad : 5;
128131
enum spi_mem_data_dir dir;
129132
unsigned int nbytes;
130133
union {
@@ -297,10 +300,13 @@ struct spi_controller_mem_ops {
297300
* struct spi_controller_mem_caps - SPI memory controller capabilities
298301
* @dtr: Supports DTR operations
299302
* @ecc: Supports operations with error correction
303+
* @swap16: Supports swapping bytes on a 16 bit boundary when configured in
304+
* Octal DTR
300305
*/
301306
struct spi_controller_mem_caps {
302307
bool dtr;
303308
bool ecc;
309+
bool swap16;
304310
};
305311

306312
#define spi_mem_controller_is_capable(ctlr, cap) \

0 commit comments

Comments
 (0)