Received: by 2002:a25:5b86:0:0:0:0:0 with SMTP id p128csp308010ybb; Thu, 28 Mar 2019 03:01:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqxfGsqhDe0wwwyBVcodrMFM4282fkwi2pbT/UrkM8DZJz61sRERj7/I3tbTAopitFHqs7jR X-Received: by 2002:a17:902:e5:: with SMTP id a92mr41498073pla.326.1553767308581; Thu, 28 Mar 2019 03:01:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553767308; cv=none; d=google.com; s=arc-20160816; b=0VEj4qBEOeDZwHYOGhAbuvrp5qURcS7m8eEbsyWvR7kjBj6kXGOH1xPDeDyTcEOrB/ MPJuZ4Grh0Qht/IWvnNBxm7KlPoESEjxE76nX06q5P9pejMr8l1AjlxMUplck/5D1fc2 LwqcW2a0jR2azsXT2FSk16jpeK3pQpYXM2mEL94zkU4k5hsEeURYF/eqzE0q5eD7scQR e/TMAdpR1jETPogIfJoSU1TKXpjAtNp+8jfOZ0gOp6DWusYlegFmp63aoq8pSGStW3DM FiiK6vGgDUHpwnoMKiHvJF1UTXhE37VX5jYEBBQcyOkGMnzyZWKP+fS+2hxWRJg42qrV Ph9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=4KD1cZHKjH2Gnm6FM0B7Ci7J0sa0JGaQWDwPyjmJnxQ=; b=OgFi/auEWBwHR3c1FH3pCNrjDGqWE+fDEjX4UXRoaxZMOeaYGL8bK5r9qJLDB66pnT OssibN7G37xVhjlnjz/VHZLzTw6RHMe6n76pMaF/Hkf55zYDW3zQeDMJInwExoFO0/rT t9DDDZysAeoUWkJRULMGsiKDdMtLs8YlxeBhXWyQip31kLGrrdOtW8mP+W7If9ohCOX9 VShAHh669Z6YrCaV6SwaXriJoMqikdb7zj2t1u8kFFEx2tmDitZ5qg9trJF8tVbkgHVY sT20jYYoMUcjHkEPvNyw3Lh0bpKJF3UM5eR0A5sX0CwQRd+aZHiprR/NnJYSDKxSdiPF 6MQQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p4si20983430pgb.558.2019.03.28.03.01.32; Thu, 28 Mar 2019 03:01:48 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726238AbfC1KAl (ORCPT + 99 others); Thu, 28 Mar 2019 06:00:41 -0400 Received: from twhmllg3.macronix.com ([122.147.135.201]:50820 "EHLO TWHMLLG3.macronix.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725994AbfC1KAk (ORCPT ); Thu, 28 Mar 2019 06:00:40 -0400 Received: from localhost.localdomain ([172.17.195.96]) by TWHMLLG3.macronix.com with ESMTP id x2S9wlJO052359; Thu, 28 Mar 2019 17:58:52 +0800 (GMT-8) (envelope-from masonccyang@mxic.com.tw) From: Mason Yang To: broonie@kernel.org, marek.vasut@gmail.com, linux-kernel@vger.kernel.org, linux-spi@vger.kernel.org, bbrezillon@kernel.org, dwmw2@infradead.org, lee.jones@linaro.org, robh+dt@kernel.org, mark.rutland@arm.com, computersforpeace@gmal.com, paul.burton@mips.com, stefan@agner.ch, christophe.kerello@st.com, liang.yang@amlogic.com, geert@linux-m68k.org, devicetree@vger.kernel.org, marcel.ziswiler@toradex.com, linux-mtd@lists.infradead.org, richard@nod.at, miquel.raynal@bootlin.com Cc: juliensu@mxic.com.tw, zhengxunli@mxic.com.tw, Mason Yang Subject: [PATCH 5/7] spi: Add direct mapping mode for Macronix SPI controller Date: Thu, 28 Mar 2019 18:18:36 +0800 Message-Id: <1553768318-23149-6-git-send-email-masonccyang@mxic.com.tw> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1553768318-23149-1-git-send-email-masonccyang@mxic.com.tw> References: <1553768318-23149-1-git-send-email-masonccyang@mxic.com.tw> X-MAIL: TWHMLLG3.macronix.com x2S9wlJO052359 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add direct mapping read mode for Macronix SPI controller driver. Signed-off-by: Mason Yang --- drivers/spi/spi-mxic.c | 129 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 100 insertions(+), 29 deletions(-) diff --git a/drivers/spi/spi-mxic.c b/drivers/spi/spi-mxic.c index fbebf89..9f5ff2b 100644 --- a/drivers/spi/spi-mxic.c +++ b/drivers/spi/spi-mxic.c @@ -19,6 +19,7 @@ struct mxic_spi { struct clk *send_clk; struct clk *send_dly_clk; void __iomem *regs; + void __iomem *dirmap; u32 cur_speed_hz; }; @@ -128,6 +129,42 @@ static void mxic_spi_hw_init(struct mxic_spi *mxic) mxic->regs + HC_CFG); } +static u32 mxic_spi_mem_prep_op_cfg(const struct spi_mem_op *op) +{ + u32 cfg = OP_CMD_BYTES(1) | OP_CMD_BUSW(fls(op->cmd.buswidth) - 1); + + if (op->addr.nbytes) + cfg |= OP_ADDR_BYTES(op->addr.nbytes) | + OP_ADDR_BUSW(fls(op->addr.buswidth) - 1); + + if (op->dummy.nbytes) + cfg |= OP_DUMMY_CYC(op->dummy.nbytes); + + if (op->data.nbytes) { + cfg |= OP_DATA_BUSW(fls(op->data.buswidth) - 1); + if (op->data.dir == SPI_MEM_DATA_IN) + cfg |= OP_READ; + } + + return cfg; +} + +static void mxic_spi_set_hc_cfg(struct spi_device *spi, u32 flags) +{ + struct mxic_spi *mxic = spi_master_get_devdata(spi->master); + int nio = 1; + + if (spi->mode & (SPI_TX_QUAD | SPI_RX_QUAD)) + nio = 4; + else if (spi->mode & (SPI_TX_DUAL | SPI_RX_DUAL)) + nio = 2; + + writel(flags | HC_CFG_NIO(nio) | + HC_CFG_TYPE(spi->chip_select, HC_CFG_TYPE_SPI_NOR) | + HC_CFG_SLV_ACT(spi->chip_select) | HC_CFG_IDLE_SIO_LVL(1), + mxic->regs + HC_CFG); +} + static int mxic_spi_data_xfer(struct mxic_spi *mxic, const void *txbuf, void *rxbuf, unsigned int len) { @@ -201,43 +238,18 @@ static int mxic_spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) { struct mxic_spi *mxic = spi_master_get_devdata(mem->spi->master); - int nio = 1, i, ret; - u32 ss_ctrl; + int i, ret; u8 addr[8]; ret = mxic_spi_set_freq(mxic, mem->spi->max_speed_hz); if (ret) return ret; - if (mem->spi->mode & (SPI_TX_QUAD | SPI_RX_QUAD)) - nio = 4; - else if (mem->spi->mode & (SPI_TX_DUAL | SPI_RX_DUAL)) - nio = 2; + mxic_spi_set_hc_cfg(mem->spi, HC_CFG_MAN_CS_EN); - writel(HC_CFG_NIO(nio) | - HC_CFG_TYPE(mem->spi->chip_select, HC_CFG_TYPE_SPI_NOR) | - HC_CFG_SLV_ACT(mem->spi->chip_select) | HC_CFG_IDLE_SIO_LVL(1) | - HC_CFG_MAN_CS_EN, - mxic->regs + HC_CFG); writel(HC_EN_BIT, mxic->regs + HC_EN); - - ss_ctrl = OP_CMD_BYTES(1) | OP_CMD_BUSW(fls(op->cmd.buswidth) - 1); - - if (op->addr.nbytes) - ss_ctrl |= OP_ADDR_BYTES(op->addr.nbytes) | - OP_ADDR_BUSW(fls(op->addr.buswidth) - 1); - - if (op->dummy.nbytes) - ss_ctrl |= OP_DUMMY_CYC(op->dummy.nbytes); - - if (op->data.nbytes) { - ss_ctrl |= OP_DATA_BUSW(fls(op->data.buswidth) - 1); - if (op->data.dir == SPI_MEM_DATA_IN) - ss_ctrl |= OP_READ; - } - - writel(ss_ctrl, mxic->regs + SS_CTRL(mem->spi->chip_select)); - + writel(mxic_spi_mem_prep_op_cfg(op), + mxic->regs + SS_CTRL(mem->spi->chip_select)); writel(readl(mxic->regs + HC_CFG) | HC_CFG_MAN_CS_ASSERT, mxic->regs + HC_CFG); @@ -271,9 +283,64 @@ static int mxic_spi_mem_exec_op(struct spi_mem *mem, return ret; } +static int mxic_spi_mem_dirmap_create(struct spi_mem_dirmap_desc *desc) +{ + struct mxic_spi *mxic = spi_master_get_devdata(desc->mem->spi->master); + + if (!mxic->dirmap) + return -ENOTSUPP; + + /* + * TODO: overcome this limitation by moving LWR/LRD_ADDR during a + * read/write operation. + */ + if (desc->info.offset + desc->info.length > U32_MAX) + return -ENOTSUPP; + + if (!mxic_spi_mem_supports_op(desc->mem, &desc->info.op_tmpl)) + return -ENOTSUPP; + + if (desc->info.op_tmpl.data.dir == SPI_MEM_DATA_OUT) + return -ENOTSUPP; + + return 0; +} + +static ssize_t mxic_spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc, + u64 offs, size_t len, void *buf) +{ + struct mxic_spi *mxic = spi_master_get_devdata(desc->mem->spi->master); + int ret; + u32 sts; + + mxic_spi_set_hc_cfg(desc->mem->spi, 0); + + writel(mxic_spi_mem_prep_op_cfg(&desc->info.op_tmpl), + mxic->regs + LRD_CFG); + writel(LMODE_CMD0(desc->info.op_tmpl.cmd.opcode >> 8) | + (LMODE_CMD1(desc->info.op_tmpl.cmd.opcode) & 0xff00) | + LMODE_SLV_ACT(desc->mem->spi->chip_select) | + LMODE_EN, mxic->regs + LRD_CTRL); + + memcpy_fromio(buf, mxic->dirmap + offs, len); + + writel(INT_LRD_DIS, mxic->regs + INT_STS); + writel(0, mxic->regs + LRD_CTRL); + + ret = readl_poll_timeout(mxic->regs + INT_STS, sts, + sts & INT_LRD_DIS, 0, USEC_PER_SEC); + + if (ret) + return ret; + + return len; +} + static const struct spi_controller_mem_ops mxic_spi_mem_ops = { .supports_op = mxic_spi_mem_supports_op, .exec_op = mxic_spi_mem_exec_op, + .dirmap_create = mxic_spi_mem_dirmap_create, + .dirmap_read = mxic_spi_mem_dirmap_read, }; static void mxic_spi_set_cs(struct spi_device *spi, bool lvl) @@ -404,6 +471,10 @@ static int mxic_spi_probe(struct platform_device *pdev) if (IS_ERR(mxic->regs)) return PTR_ERR(mxic->regs); + mxic->dirmap = mxic_mfd->dirmap; + if (IS_ERR(mxic->dirmap)) + mxic->dirmap = NULL; + pm_runtime_enable(&pdev->dev); master->auto_runtime_pm = true; -- 1.9.1