From: Vignesh R <vigneshr@ti.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v3 09/20] mtd: spi: spi-nor-core: Add SPI MEM support
Date: Tue, 29 Jan 2019 11:19:56 +0530 [thread overview]
Message-ID: <20190129055007.17376-10-vigneshr@ti.com> (raw)
In-Reply-To: <20190129055007.17376-1-vigneshr@ti.com>
Many SPI controllers have special MMIO interfaces which provide
accelerated read/write access but require knowledge of flash parameters
to make use of it. Recent spi-mem layer provides a way to support such
controllers.
Therefore, add spi-mem support to spi-nor-core as a way to support SPI
controllers with MMIO interface. SPI MEM layer takes care of translating
spi_mem_ops to spi_xfer()s in case of legacy SPI controllers.
Signed-off-by: Vignesh R <vigneshr@ti.com>
Tested-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
Tested-by: Stefan Roese <sr@denx.de>
Tested-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
drivers/mtd/spi/spi-nor-core.c | 97 ++++++++++++++++++++++++++++++++--
1 file changed, 93 insertions(+), 4 deletions(-)
diff --git a/drivers/mtd/spi/spi-nor-core.c b/drivers/mtd/spi/spi-nor-core.c
index 19171766ce82..633b7226f37f 100644
--- a/drivers/mtd/spi/spi-nor-core.c
+++ b/drivers/mtd/spi/spi-nor-core.c
@@ -88,26 +88,115 @@ struct flash_info {
#define JEDEC_MFR(info) ((info)->id[0])
+static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
+ *op, void *buf)
+{
+ if (op->data.dir == SPI_MEM_DATA_IN)
+ op->data.buf.in = buf;
+ else
+ op->data.buf.out = buf;
+ return spi_mem_exec_op(nor->spi, op);
+}
+
static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
{
- return -EINVAL;
+ struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
+ SPI_MEM_OP_NO_ADDR,
+ SPI_MEM_OP_NO_DUMMY,
+ SPI_MEM_OP_DATA_IN(len, NULL, 1));
+ int ret;
+
+ ret = spi_nor_read_write_reg(nor, &op, val);
+ if (ret < 0)
+ dev_dbg(&flash->spimem->spi->dev, "error %d reading %x\n", ret,
+ code);
+
+ return ret;
}
static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
{
- return -EINVAL;
+ struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
+ SPI_MEM_OP_NO_ADDR,
+ SPI_MEM_OP_NO_DUMMY,
+ SPI_MEM_OP_DATA_OUT(len, NULL, 1));
+
+ return spi_nor_read_write_reg(nor, &op, buf);
}
static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
u_char *buf)
{
- return -EINVAL;
+ struct spi_mem_op op =
+ SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
+ SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
+ SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
+ SPI_MEM_OP_DATA_IN(len, buf, 1));
+ size_t remaining = len;
+ int ret;
+
+ /* get transfer protocols. */
+ op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
+ op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
+ op.dummy.buswidth = op.addr.buswidth;
+ op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
+
+ /* convert the dummy cycles to the number of bytes */
+ op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
+
+ while (remaining) {
+ op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
+ ret = spi_mem_adjust_op_size(nor->spi, &op);
+ if (ret)
+ return ret;
+
+ ret = spi_mem_exec_op(nor->spi, &op);
+ if (ret)
+ return ret;
+
+ op.addr.val += op.data.nbytes;
+ remaining -= op.data.nbytes;
+ op.data.buf.in += op.data.nbytes;
+ }
+
+ return len;
}
static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
const u_char *buf)
{
- return -EINVAL;
+ struct spi_mem_op op =
+ SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
+ SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
+ SPI_MEM_OP_NO_DUMMY,
+ SPI_MEM_OP_DATA_OUT(len, buf, 1));
+ size_t remaining = len;
+ int ret;
+
+ /* get transfer protocols. */
+ op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
+ op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
+ op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
+
+ if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
+ op.addr.nbytes = 0;
+
+ while (remaining) {
+ op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
+ ret = spi_mem_adjust_op_size(nor->spi, &op);
+ if (ret)
+ return ret;
+
+ ret = spi_mem_exec_op(nor->spi, &op);
+ if (ret)
+ return ret;
+
+ op.addr.val += op.data.nbytes;
+ remaining -= op.data.nbytes;
+ op.data.buf.out += op.data.nbytes;
+ }
+
+ return len;
}
/*
--
2.20.1
next prev parent reply other threads:[~2019-01-29 5:49 UTC|newest]
Thread overview: 58+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-29 5:49 [U-Boot] [PATCH v3 00/20] SF: Migrate to Linux SPI NOR framework Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 01/20] configs: Move CONFIG_SPI_FLASH into defconfigs Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 02/20] bitops: Fix GENMASK definition for Sandbox Vignesh R
2019-01-31 0:41 ` Simon Glass
2019-01-31 13:34 ` Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 03/20] spi: spi-mem: Allow use of spi_mem_exec_op for all SPI modes Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 04/20] spi: spi-mem: Extend spi_mem_adjust_op_size() to honor max xfer size Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 05/20] spi: spi-mem: Claim SPI bus before spi mem access Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 06/20] spi: Add non DM version of SPI_MEM Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 07/20] sh: bitops: add hweight*() macros Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 08/20] mtd: spi: Port SPI NOR framework from Linux Vignesh R
2019-01-29 5:49 ` Vignesh R [this message]
2019-01-29 5:49 ` [U-Boot] [PATCH v3 10/20] mtd: spi: spi-nor-core: Add 4 Byte addressing support Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 11/20] mtd: spi: spi-nor-core: Add SFDP support Vignesh R
2019-01-29 5:49 ` [U-Boot] [PATCH v3 12/20] mtd: spi: spi-nor-core: Add back U-Boot specific features Vignesh R
2019-01-29 5:50 ` [U-Boot] [PATCH v3 13/20] mtd: spi: sf_probe: Add "jedec, spi-nor" compatible string Vignesh R
2019-01-29 5:50 ` [U-Boot] [PATCH v3 14/20] mtd: spi: Switch to new SPI NOR framework Vignesh R
2019-01-31 12:23 ` Jagan Teki
2019-01-31 12:39 ` Vignesh R
2019-01-31 12:39 ` Jagan Teki
2019-01-31 12:55 ` Vignesh R
2019-01-31 12:57 ` Jagan Teki
2019-01-29 5:50 ` [U-Boot] [PATCH v3 15/20] mtd: spi: Remove unused files Vignesh R
2019-01-29 5:50 ` [U-Boot] [PATCH v3 16/20] mtd: spi: Add lightweight SPI flash stack for SPL Vignesh R
2019-01-31 12:06 ` Jagan Teki
2019-01-31 17:51 ` Vignesh R
2019-02-01 15:48 ` Jagan Teki
2019-02-01 17:04 ` Vignesh R
2019-02-02 13:12 ` Jagan Teki
2019-01-29 5:50 ` [U-Boot] [PATCH v3 17/20] spl: Kconfig: Enable SPI_FLASH_TINY by default " Vignesh R
2019-01-29 5:50 ` [U-Boot] [PATCH v3 18/20] configs: Remove SF_DUAL_FLASH Vignesh R
2019-01-29 5:50 ` [U-Boot] [PATCH v3 19/20] configs: Don't use SPI_FLASH_BAR as default Vignesh R
2019-01-31 13:18 ` Jagan Teki
2019-01-31 13:33 ` Vignesh R
2019-01-31 13:36 ` Jagan Teki
2019-01-31 13:47 ` Vignesh R
2019-01-31 13:50 ` Jagan Teki
2019-01-31 17:35 ` Vignesh R
2019-02-01 15:54 ` Jagan Teki
2019-02-01 17:08 ` Vignesh R
2019-02-02 13:10 ` Jagan Teki
2019-01-29 5:50 ` [U-Boot] [PATCH v3 20/20] MAINTAINERS: Add an entry for SPI NOR Vignesh R
2019-01-29 8:15 ` [U-Boot] [PATCH v3 00/20] SF: Migrate to Linux SPI NOR framework Simon Goldschmidt
2019-01-29 11:56 ` Vignesh R
2019-01-29 11:58 ` Simon Goldschmidt
2019-01-29 12:25 ` Tom Rini
2019-01-31 14:40 ` Jagan Teki
2019-01-31 14:42 ` Tom Rini
2019-01-31 14:45 ` Jagan Teki
2019-02-01 8:27 ` Vignesh R
2019-02-04 19:03 ` Jagan Teki
2019-01-31 14:48 ` Simon Goldschmidt
2019-01-31 14:51 ` Jagan Teki
2019-01-31 14:54 ` Tom Rini
2019-01-31 14:58 ` Simon Goldschmidt
2019-01-31 15:02 ` Tom Rini
2019-01-31 15:08 ` Simon Goldschmidt
2019-01-31 15:00 ` Jagan Teki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190129055007.17376-10-vigneshr@ti.com \
--to=vigneshr@ti.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox