Skip to content

Commit f173f26

Browse files
r-vigneshambarus
authored andcommitted
mtd: spi-nor: always use bounce buffer for register read/writes
spi-mem layer expects all buffers passed to it to be DMA'able. But spi-nor layer mostly allocates buffers on stack for reading/writing to registers and therefore are not DMA'able. Introduce bounce buffer to be used to read/write to registers. This ensures that buffer passed to spi-mem layer during register read/writes is DMA'able. With this change nor->cmd-buf is no longer used, so drop it. Signed-off-by: Vignesh Raghavendra <vigneshr@ti.com> Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com> Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
1 parent 5f9e832 commit f173f26

2 files changed

Lines changed: 49 additions & 38 deletions

File tree

‎drivers/mtd/spi-nor/spi-nor.c‎

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,14 @@ struct flash_info {
296296
static int read_sr(struct spi_nor *nor)
297297
{
298298
int ret;
299-
u8 val;
300299

301-
ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
300+
ret = nor->read_reg(nor, SPINOR_OP_RDSR, nor->bouncebuf, 1);
302301
if (ret < 0) {
303302
pr_err("error %d reading SR\n", (int) ret);
304303
return ret;
305304
}
306305

307-
return val;
306+
return nor->bouncebuf[0];
308307
}
309308

310309
/*
@@ -315,15 +314,14 @@ static int read_sr(struct spi_nor *nor)
315314
static int read_fsr(struct spi_nor *nor)
316315
{
317316
int ret;
318-
u8 val;
319317

320-
ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
318+
ret = nor->read_reg(nor, SPINOR_OP_RDFSR, nor->bouncebuf, 1);
321319
if (ret < 0) {
322320
pr_err("error %d reading FSR\n", ret);
323321
return ret;
324322
}
325323

326-
return val;
324+
return nor->bouncebuf[0];
327325
}
328326

329327
/*
@@ -334,15 +332,14 @@ static int read_fsr(struct spi_nor *nor)
334332
static int read_cr(struct spi_nor *nor)
335333
{
336334
int ret;
337-
u8 val;
338335

339-
ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
336+
ret = nor->read_reg(nor, SPINOR_OP_RDCR, nor->bouncebuf, 1);
340337
if (ret < 0) {
341338
dev_err(nor->dev, "error %d reading CR\n", ret);
342339
return ret;
343340
}
344341

345-
return val;
342+
return nor->bouncebuf[0];
346343
}
347344

348345
/*
@@ -351,8 +348,8 @@ static int read_cr(struct spi_nor *nor)
351348
*/
352349
static int write_sr(struct spi_nor *nor, u8 val)
353350
{
354-
nor->cmd_buf[0] = val;
355-
return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
351+
nor->bouncebuf[0] = val;
352+
return nor->write_reg(nor, SPINOR_OP_WRSR, nor->bouncebuf, 1);
356353
}
357354

358355
/*
@@ -500,31 +497,31 @@ static int set_4byte(struct spi_nor *nor, bool enable)
500497
* We must clear the register to enable normal behavior.
501498
*/
502499
write_enable(nor);
503-
nor->cmd_buf[0] = 0;
504-
nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
500+
nor->bouncebuf[0] = 0;
501+
nor->write_reg(nor, SPINOR_OP_WREAR,
502+
nor->bouncebuf, 1);
505503
write_disable(nor);
506504
}
507505

508506
return status;
509507
default:
510508
/* Spansion style */
511-
nor->cmd_buf[0] = enable << 7;
512-
return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
509+
nor->bouncebuf[0] = enable << 7;
510+
return nor->write_reg(nor, SPINOR_OP_BRWR, nor->bouncebuf, 1);
513511
}
514512
}
515513

516514
static int s3an_sr_ready(struct spi_nor *nor)
517515
{
518516
int ret;
519-
u8 val;
520517

521-
ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
518+
ret = nor->read_reg(nor, SPINOR_OP_XRDSR, nor->bouncebuf, 1);
522519
if (ret < 0) {
523520
dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
524521
return ret;
525522
}
526523

527-
return !!(val & XSR_RDY);
524+
return !!(nor->bouncebuf[0] & XSR_RDY);
528525
}
529526

530527
static int spi_nor_sr_ready(struct spi_nor *nor)
@@ -683,7 +680,6 @@ static loff_t spi_nor_s3an_addr_convert(struct spi_nor *nor, unsigned int addr)
683680
*/
684681
static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
685682
{
686-
u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
687683
int i;
688684

689685
if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
@@ -697,11 +693,12 @@ static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
697693
* control
698694
*/
699695
for (i = nor->addr_width - 1; i >= 0; i--) {
700-
buf[i] = addr & 0xff;
696+
nor->bouncebuf[i] = addr & 0xff;
701697
addr >>= 8;
702698
}
703699

704-
return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
700+
return nor->write_reg(nor, nor->erase_opcode, nor->bouncebuf,
701+
nor->addr_width);
705702
}
706703

707704
/**
@@ -1485,9 +1482,11 @@ static int macronix_quad_enable(struct spi_nor *nor)
14851482
*/
14861483
static int spansion_quad_enable(struct spi_nor *nor)
14871484
{
1488-
u8 sr_cr[2] = {0, CR_QUAD_EN_SPAN};
1485+
u8 *sr_cr = nor->bouncebuf;
14891486
int ret;
14901487

1488+
sr_cr[0] = 0;
1489+
sr_cr[1] = CR_QUAD_EN_SPAN;
14911490
ret = write_sr_cr(nor, sr_cr);
14921491
if (ret)
14931492
return ret;
@@ -1517,7 +1516,7 @@ static int spansion_quad_enable(struct spi_nor *nor)
15171516
*/
15181517
static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
15191518
{
1520-
u8 sr_cr[2];
1519+
u8 *sr_cr = nor->bouncebuf;
15211520
int ret;
15221521

15231522
/* Keep the current value of the Status Register. */
@@ -1548,7 +1547,7 @@ static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
15481547
static int spansion_read_cr_quad_enable(struct spi_nor *nor)
15491548
{
15501549
struct device *dev = nor->dev;
1551-
u8 sr_cr[2];
1550+
u8 *sr_cr = nor->bouncebuf;
15521551
int ret;
15531552

15541553
/* Check current Quad Enable bit value. */
@@ -1599,22 +1598,22 @@ static int spansion_read_cr_quad_enable(struct spi_nor *nor)
15991598
*/
16001599
static int sr2_bit7_quad_enable(struct spi_nor *nor)
16011600
{
1602-
u8 sr2;
1601+
u8 *sr2 = nor->bouncebuf;
16031602
int ret;
16041603

16051604
/* Check current Quad Enable bit value. */
1606-
ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
1605+
ret = nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1);
16071606
if (ret)
16081607
return ret;
1609-
if (sr2 & SR2_QUAD_EN_BIT7)
1608+
if (*sr2 & SR2_QUAD_EN_BIT7)
16101609
return 0;
16111610

16121611
/* Update the Quad Enable bit. */
1613-
sr2 |= SR2_QUAD_EN_BIT7;
1612+
*sr2 |= SR2_QUAD_EN_BIT7;
16141613

16151614
write_enable(nor);
16161615

1617-
ret = nor->write_reg(nor, SPINOR_OP_WRSR2, &sr2, 1);
1616+
ret = nor->write_reg(nor, SPINOR_OP_WRSR2, sr2, 1);
16181617
if (ret < 0) {
16191618
dev_err(nor->dev, "error while writing status register 2\n");
16201619
return -EINVAL;
@@ -1627,8 +1626,8 @@ static int sr2_bit7_quad_enable(struct spi_nor *nor)
16271626
}
16281627

16291628
/* Read back and check it. */
1630-
ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
1631-
if (!(ret > 0 && (sr2 & SR2_QUAD_EN_BIT7))) {
1629+
ret = nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1);
1630+
if (!(ret > 0 && (*sr2 & SR2_QUAD_EN_BIT7))) {
16321631
dev_err(nor->dev, "SR2 Quad bit not set\n");
16331632
return -EINVAL;
16341633
}
@@ -1687,7 +1686,7 @@ static int spi_nor_spansion_clear_sr_bp(struct spi_nor *nor)
16871686
{
16881687
int ret;
16891688
u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1690-
u8 sr_cr[2] = {0};
1689+
u8 *sr_cr = nor->bouncebuf;
16911690

16921691
/* Check current Quad Enable bit value. */
16931692
ret = read_cr(nor);
@@ -2177,7 +2176,7 @@ static const struct flash_info spi_nor_ids[] = {
21772176
static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
21782177
{
21792178
int tmp;
2180-
u8 id[SPI_NOR_MAX_ID_LEN];
2179+
u8 *id = nor->bouncebuf;
21812180
const struct flash_info *info;
21822181

21832182
tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
@@ -2393,9 +2392,8 @@ static int spi_nor_check(struct spi_nor *nor)
23932392
static int s3an_nor_scan(struct spi_nor *nor)
23942393
{
23952394
int ret;
2396-
u8 val;
23972395

2398-
ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
2396+
ret = nor->read_reg(nor, SPINOR_OP_XRDSR, nor->bouncebuf, 1);
23992397
if (ret < 0) {
24002398
dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
24012399
return ret;
@@ -2417,7 +2415,7 @@ static int s3an_nor_scan(struct spi_nor *nor)
24172415
* The current addressing mode can be read from the XRDSR register
24182416
* and should not be changed, because is a destructive operation.
24192417
*/
2420-
if (val & XSR_PAGESIZE) {
2418+
if (nor->bouncebuf[0] & XSR_PAGESIZE) {
24212419
/* Flash in Power of 2 mode */
24222420
nor->page_size = (nor->page_size == 264) ? 256 : 512;
24232421
nor->mtd.writebufsize = nor->page_size;
@@ -4121,6 +4119,16 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
41214119
nor->read_proto = SNOR_PROTO_1_1_1;
41224120
nor->write_proto = SNOR_PROTO_1_1_1;
41234121

4122+
/*
4123+
* We need the bounce buffer early to read/write registers when going
4124+
* through the spi-mem layer (buffers have to be DMA-able).
4125+
*/
4126+
nor->bouncebuf_size = PAGE_SIZE;
4127+
nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
4128+
GFP_KERNEL);
4129+
if (!nor->bouncebuf)
4130+
return -ENOMEM;
4131+
41244132
if (name)
41254133
info = spi_nor_match_id(name);
41264134
/* Try to auto-detect if chip name wasn't specified or not found */

‎include/linux/mtd/spi-nor.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ struct flash_info;
344344
* @mtd: point to a mtd_info structure
345345
* @lock: the lock for the read/write/erase/lock/unlock operations
346346
* @dev: point to a spi device, or a spi nor controller device.
347+
* @bouncebuf: bounce buffer used when the buffer passed by the MTD
348+
* layer is not DMA-able
349+
* @bouncebuf_size: size of the bounce buffer
347350
* @info: spi-nor part JDEC MFR id and other info
348351
* @page_size: the page size of the SPI NOR
349352
* @addr_width: number of address bytes
@@ -356,7 +359,6 @@ struct flash_info;
356359
* @read_proto: the SPI protocol for read operations
357360
* @write_proto: the SPI protocol for write operations
358361
* @reg_proto the SPI protocol for read_reg/write_reg/erase operations
359-
* @cmd_buf: used by the write_reg
360362
* @erase_map: the erase map of the SPI NOR
361363
* @prepare: [OPTIONAL] do some preparations for the
362364
* read/write/erase/lock/unlock operations
@@ -382,6 +384,8 @@ struct spi_nor {
382384
struct mtd_info mtd;
383385
struct mutex lock;
384386
struct device *dev;
387+
u8 *bouncebuf;
388+
size_t bouncebuf_size;
385389
const struct flash_info *info;
386390
u32 page_size;
387391
u8 addr_width;
@@ -394,7 +398,6 @@ struct spi_nor {
394398
enum spi_nor_protocol reg_proto;
395399
bool sst_write_second;
396400
u32 flags;
397-
u8 cmd_buf[SPI_NOR_MAX_CMD_SIZE];
398401
struct spi_nor_erase_map erase_map;
399402

400403
int (*prepare)(struct spi_nor *nor, enum spi_nor_ops ops);

0 commit comments

Comments
 (0)