From patchwork Mon Apr 6 18:31:04 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sricharan Ramabadhran X-Patchwork-Id: 6163901 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 01D209F2EC for ; Mon, 6 Apr 2015 18:36:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8ED4620303 for ; Mon, 6 Apr 2015 18:36:52 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 03522202EB for ; Mon, 6 Apr 2015 18:36:51 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1YfBpu-0000i3-Cl; Mon, 06 Apr 2015 18:33:26 +0000 Received: from smtp.codeaurora.org ([198.145.29.96]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YfBoL-00005l-67 for linux-arm-kernel@lists.infradead.org; Mon, 06 Apr 2015 18:31:53 +0000 Received: from smtp.codeaurora.org (localhost [127.0.0.1]) by smtp.codeaurora.org (Postfix) with ESMTP id C59E4140CE3; Mon, 6 Apr 2015 18:31:29 +0000 (UTC) Received: by smtp.codeaurora.org (Postfix, from userid 486) id 973CB140CE8; Mon, 6 Apr 2015 18:31:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Received: from blr-ubuntu-32.ap.qualcomm.com (unknown [202.46.23.61]) (using TLSv1.1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: sricharan@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 7BF83140CE3; Mon, 6 Apr 2015 18:31:25 +0000 (UTC) From: Sricharan R To: devicetree@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, dmaengine@vger.kernel.org, galak@codeaurora.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, agross@codeaurora.org, iivanov@mm-sol.com Subject: [PATCH V2 3/6] i2c: qup: Add bam dma capabilities Date: Tue, 7 Apr 2015 00:01:04 +0530 Message-Id: <1428345067-21878-4-git-send-email-sricharan@codeaurora.org> X-Mailer: git-send-email 1.8.2.1 In-Reply-To: <1428345067-21878-1-git-send-email-sricharan@codeaurora.org> References: <1428345067-21878-1-git-send-email-sricharan@codeaurora.org> X-Virus-Scanned: ClamAV using ClamSMTP X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150406_113149_408614_A0D7D740 X-CRM114-Status: GOOD ( 23.48 ) X-Spam-Score: -0.0 (/) Cc: srichara@qti.qualcomm.com X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP QUP cores can be attached to a BAM module, which acts as a dma engine for the QUP core. When DMA with BAM is enabled, the BAM consumer pipe transmitted data is written to the output FIFO and the BAM producer pipe received data is read from the input FIFO. With BAM capabilities, qup-i2c core can transfer more than 256 bytes, without a 'stop' which is not possible otherwise. Signed-off-by: Sricharan R --- [v2] Addressed comments from Ivan T. Ivanov drivers/i2c/busses/i2c-qup.c | 371 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 366 insertions(+), 5 deletions(-) diff --git a/drivers/i2c/busses/i2c-qup.c b/drivers/i2c/busses/i2c-qup.c index 8605557..2fd141d 100644 --- a/drivers/i2c/busses/i2c-qup.c +++ b/drivers/i2c/busses/i2c-qup.c @@ -25,6 +25,11 @@ #include #include #include +#include +#include +#include +#include +#include /* QUP Registers */ #define QUP_CONFIG 0x000 @@ -34,6 +39,7 @@ #define QUP_OPERATIONAL 0x018 #define QUP_ERROR_FLAGS 0x01c #define QUP_ERROR_FLAGS_EN 0x020 +#define QUP_OPERATIONAL_MASK 0x028 #define QUP_HW_VERSION 0x030 #define QUP_MX_OUTPUT_CNT 0x100 #define QUP_OUT_FIFO_BASE 0x110 @@ -53,6 +59,7 @@ #define QUP_STATE_VALID BIT(2) #define QUP_I2C_MAST_GEN BIT(4) +#define QUP_I2C_FLUSH BIT(6) #define QUP_OPERATIONAL_RESET 0x000ff0 #define QUP_I2C_STATUS_RESET 0xfffffc @@ -78,7 +85,10 @@ /* Packing/Unpacking words in FIFOs, and IO modes */ #define QUP_OUTPUT_BLK_MODE (1 << 10) +#define QUP_OUTPUT_BAM_MODE (3 << 10) #define QUP_INPUT_BLK_MODE (1 << 12) +#define QUP_INPUT_BAM_MODE (3 << 12) +#define QUP_BAM_MODE (QUP_OUTPUT_BAM_MODE | QUP_INPUT_BAM_MODE) #define QUP_UNPACK_EN BIT(14) #define QUP_PACK_EN BIT(15) @@ -105,6 +115,10 @@ #define QUP_TAG_V2_DATARD 0x85 #define QUP_TAG_V2_DATARD_STOP 0x87 +/* QUP BAM v2 tags */ +#define QUP_BAM_INPUT_EOT 0x93 +#define QUP_BAM_FLUSH_STOP 0x96 + /* frequency definitions for high speed and max speed */ #define I2C_QUP_CLK_FAST_FREQ 1000000 @@ -115,6 +129,11 @@ #define QUP_STATUS_ERROR_FLAGS 0x7c #define QUP_READ_LIMIT 256 +#define MX_TX_RX_LEN SZ_64K +#define MX_BLOCKS (MX_TX_RX_LEN / QUP_READ_LIMIT) + +/* Max timeout in ms for 32k bytes */ +#define TOUT_MAX 300 struct qup_i2c_block { int blocks; @@ -123,6 +142,23 @@ struct qup_i2c_block { int block_pos; }; +struct qup_i2c_tag { + u8 *start; + dma_addr_t addr; +}; + +struct qup_i2c_bam_rx { + struct qup_i2c_tag scratch_tag; + struct dma_chan *dma_rx; + struct scatterlist *sg_rx; +}; + +struct qup_i2c_bam_tx { + struct qup_i2c_tag footer_tag; + struct dma_chan *dma_tx; + struct scatterlist *sg_tx; +}; + struct qup_i2c_dev { struct device *dev; void __iomem *base; @@ -155,6 +191,12 @@ struct qup_i2c_dev { /* QUP core errors */ u32 qup_err; + /* dma parameters */ + bool is_dma; + struct dma_pool *dpool; + struct qup_i2c_tag start_tag; + struct qup_i2c_bam_rx brx; + struct qup_i2c_bam_tx btx; struct completion xfer; }; @@ -231,6 +273,14 @@ static int qup_i2c_poll_state(struct qup_i2c_dev *qup, u32 req_state) return qup_i2c_poll_state_mask(qup, req_state, QUP_STATE_MASK); } +static void qup_i2c_flush(struct qup_i2c_dev *qup) +{ + u32 val = readl(qup->base + QUP_STATE); + + val |= QUP_I2C_FLUSH; + writel(val, qup->base + QUP_STATE); +} + static int qup_i2c_poll_state_valid(struct qup_i2c_dev *qup) { return qup_i2c_poll_state_mask(qup, 0, 0); @@ -715,12 +765,242 @@ err: return ret; } +static void qup_i2c_bam_cb(void *data) +{ + struct qup_i2c_dev *qup = data; + + complete(&qup->xfer); +} + +static int get_tag_code(struct i2c_msg *msg, int last) +{ + u8 op; + + /* always issue stop for each read block */ + if (last) { + if (msg->flags & I2C_M_RD) + op = QUP_TAG_V2_DATARD_STOP; + else + op = QUP_TAG_V2_DATAWR_STOP; + } else { + if (msg->flags & I2C_M_RD) + op = QUP_TAG_V2_DATARD; + else + op = QUP_TAG_V2_DATAWR; + } + + return op; +} + +static int get_start_tag(u8 *tag, struct i2c_msg *msg, int first, int last, + int blen) +{ + u16 addr = (msg->addr << 1) | ((msg->flags & I2C_M_RD) == I2C_M_RD); + u8 op; + int len = 0; + + op = get_tag_code(msg, last); + + if (first) { + tag[len++] = QUP_TAG_V2_START; + tag[len++] = addr & 0xff; + if (msg->flags & I2C_M_TEN) + tag[len++] = addr >> 8; + } + tag[len++] = op; + tag[len++] = blen; + + if (msg->flags & I2C_M_RD & last) { + tag[len++] = QUP_BAM_INPUT_EOT; + tag[len++] = QUP_BAM_FLUSH_STOP; + } + + return len; +} + +void qup_sg_set_buf(struct scatterlist *sg, void *buf, struct qup_i2c_tag *tg, + unsigned int buflen, struct qup_i2c_dev *qup, + int map, int dir) +{ + sg_set_buf(sg, buf, buflen); + + if (map) + sg->dma_address = dma_map_single(qup->dev, buf, buflen, dir); + else + sg_dma_address(sg) = tg->addr + ((u8 *)buf - tg->start); +} + +static int bam_do_xfer(struct qup_i2c_dev *qup, struct i2c_msg *msg) +{ + struct dma_async_tx_descriptor *txd, *rxd = NULL; + int ret = 0; + dma_cookie_t cookie_rx, cookie_tx; + u32 rx_nents = 0, tx_nents = 0, len = 0; + /* QUP I2C read/write limit for single command is 256bytes max*/ + int blocks = (msg->len + QUP_READ_LIMIT) / QUP_READ_LIMIT; + int rem = msg->len % QUP_READ_LIMIT; + int tlen, i = 0, tx_len = 0; + + if (msg->flags & I2C_M_RD) { + tx_nents = 1; + rx_nents = (blocks << 1) + 1; + sg_init_table(qup->brx.sg_rx, rx_nents); + + while (i < blocks) { + /* transfer length set to '0' implies 256 bytes */ + tlen = (i == (blocks - 1)) ? rem : 0; + len += get_start_tag(&qup->start_tag.start[len], + msg, !i, (i == (blocks - 1)), + tlen); + + qup_sg_set_buf(&qup->brx.sg_rx[i << 1], + &qup->brx.scratch_tag.start[0], + &qup->brx.scratch_tag, + 2, qup, 0, 0); + + qup_sg_set_buf(&qup->brx.sg_rx[(i << 1) + 1], + &msg->buf[QUP_READ_LIMIT * i], + NULL, tlen, qup, 1, + DMA_FROM_DEVICE); + + i++; + } + + sg_init_one(qup->btx.sg_tx, &qup->start_tag.start[0], len); + qup_sg_set_buf(qup->btx.sg_tx, &qup->start_tag.start[0], + &qup->start_tag, len, qup, 0, 0); + qup_sg_set_buf(&qup->brx.sg_rx[i << 1], + &qup->brx.scratch_tag.start[1], + &qup->brx.scratch_tag, 2, + qup, 0, 0); + } else { + qup->btx.footer_tag.start[0] = QUP_BAM_FLUSH_STOP; + qup->btx.footer_tag.start[1] = QUP_BAM_FLUSH_STOP; + + tx_nents = (blocks << 1) + 1; + sg_init_table(qup->btx.sg_tx, tx_nents); + + while (i < blocks) { + tlen = (i == (blocks - 1)) ? rem : 0; + len = get_start_tag(&qup->start_tag.start[tx_len], + msg, !i, (i == (blocks - 1)), tlen); + + qup_sg_set_buf(&qup->btx.sg_tx[i << 1], + &qup->start_tag.start[tx_len], + &qup->start_tag, + len, qup, 0, 0); + + tx_len += len; + qup_sg_set_buf(&qup->btx.sg_tx[(i << 1) + 1], + &msg->buf[QUP_READ_LIMIT * i], NULL, + tlen, qup, 1, DMA_TO_DEVICE); + i++; + } + qup_sg_set_buf(&qup->btx.sg_tx[i << 1], + &qup->btx.footer_tag.start[0], + &qup->btx.footer_tag, 2, + qup, 0, 0); + } + + txd = dmaengine_prep_slave_sg(qup->btx.dma_tx, qup->btx.sg_tx, tx_nents, + DMA_MEM_TO_DEV, + DMA_PREP_INTERRUPT | DMA_PREP_FENCE); + if (!txd) { + dev_err(qup->dev, "failed to get tx desc\n"); + ret = -EINVAL; + goto desc_err; + } + + if (!rx_nents) { + txd->callback = qup_i2c_bam_cb; + txd->callback_param = qup; + } + + cookie_tx = dmaengine_submit(txd); + dma_async_issue_pending(qup->btx.dma_tx); + + if (rx_nents) { + rxd = dmaengine_prep_slave_sg(qup->brx.dma_rx, qup->brx.sg_rx, + rx_nents, DMA_DEV_TO_MEM, + DMA_PREP_INTERRUPT); + + if (!rxd) { + dev_err(qup->dev, "failed to get rx desc\n"); + ret = -EINVAL; + + /* abort TX descriptors */ + dmaengine_terminate_all(qup->btx.dma_tx); + goto desc_err; + } + + rxd->callback = qup_i2c_bam_cb; + rxd->callback_param = qup; + cookie_rx = dmaengine_submit(rxd); + dma_async_issue_pending(qup->brx.dma_rx); + } + + if (!wait_for_completion_timeout(&qup->xfer, TOUT_MAX * HZ)) { + dev_err(qup->dev, "normal trans timed out\n"); + ret = -ETIMEDOUT; + } + + if (ret || qup->bus_err || qup->qup_err) { + if (qup->bus_err & QUP_I2C_NACK_FLAG) + dev_err(qup->dev, "NACK from %x\n", msg->addr); + ret = -EIO; + qup_i2c_flush(qup); + qup_i2c_change_state(qup, QUP_RUN_STATE); + + /* wait for remaining interrupts to occur */ + if (!wait_for_completion_timeout(&qup->xfer, HZ)) + dev_err(qup->dev, "flush timed out\n"); + } + +desc_err: + return ret; +} + +static int qup_bam_xfer(struct i2c_adapter *adap, struct i2c_msg *msg) +{ + struct qup_i2c_dev *qup = i2c_get_adapdata(adap); + int ret = 0; + + enable_irq(qup->irq); + + qup->bus_err = 0; + qup->qup_err = 0; + + writel(0, qup->base + QUP_MX_INPUT_CNT); + writel(0, qup->base + QUP_MX_OUTPUT_CNT); + + /* set BAM mode */ + writel(QUP_REPACK_EN | QUP_BAM_MODE, qup->base + QUP_IO_MODE); + + /* mask fifo irqs */ + writel((0x3 << 8), qup->base + QUP_OPERATIONAL_MASK); + + /* set RUN STATE */ + ret = qup_i2c_change_state(qup, QUP_RUN_STATE); + if (ret) + goto out; + + writel(qup->clk_ctl, qup->base + QUP_I2C_CLK_CTL); + + qup->msg = msg; + ret = bam_do_xfer(qup, qup->msg); +out: + disable_irq(qup->irq); + + qup->msg = NULL; + return ret; +} + static int qup_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) { struct qup_i2c_dev *qup = i2c_get_adapdata(adap); - int ret, idx; + int ret, idx, len; ret = pm_runtime_get_sync(qup->dev); if (ret < 0) @@ -752,10 +1032,17 @@ static int qup_i2c_xfer(struct i2c_adapter *adap, reinit_completion(&qup->xfer); - if (msgs[idx].flags & I2C_M_RD) - ret = qup_i2c_read_one(qup, &msgs[idx]); - else - ret = qup_i2c_write_one(qup, &msgs[idx]); + len = (&msgs[idx])->len; + + if ((qup->is_dma) && (!is_vmalloc_addr((&msgs[idx])->buf)) && + (len > qup->out_fifo_sz)) { + ret = qup_bam_xfer(adap, &msgs[idx]); + } else { + if (msgs[idx].flags & I2C_M_RD) + ret = qup_i2c_read_one(qup, &msgs[idx]); + else + ret = qup_i2c_write_one(qup, &msgs[idx]); + } if (ret) break; @@ -829,6 +1116,7 @@ static int qup_i2c_probe(struct platform_device *pdev) int src_clk_freq, clk_freq_max; u32 clk_freq = 100000; const struct of_device_id *of_id; + int blocks; qup = devm_kzalloc(&pdev->dev, sizeof(*qup), GFP_KERNEL); if (!qup) @@ -849,8 +1137,65 @@ static int qup_i2c_probe(struct platform_device *pdev) } else { qup->use_v2_tags = 1; clk_freq_max = 3400000; + + qup->brx.dma_rx = dma_request_slave_channel(&pdev->dev, "rx"); + if (!qup->brx.dma_rx) + goto nodma; + + qup->btx.dma_tx = dma_request_slave_channel(&pdev->dev, "tx"); + if (!qup->btx.dma_tx) { + dma_release_channel(qup->brx.dma_rx); + qup->brx.dma_rx = NULL; + goto nodma; + } + + blocks = (MX_BLOCKS << 1) + 1; + qup->btx.sg_tx = devm_kzalloc(&pdev->dev, + sizeof(*qup->btx.sg_tx) * blocks, + GFP_KERNEL); + if (!qup->btx.sg_tx) { + ret = -ENOMEM; + goto fail; + } + qup->brx.sg_rx = devm_kzalloc(&pdev->dev, + sizeof(*qup->btx.sg_tx) * blocks, + GFP_KERNEL); + if (!qup->brx.sg_rx) { + ret = -ENOMEM; + goto fail; + } + + size = sizeof(struct qup_i2c_tag) * (blocks + 3); + qup->dpool = dma_pool_create("qup_i2c-dma-pool", &pdev->dev, + size, 4, 0); + + qup->start_tag.start = dma_pool_alloc(qup->dpool, GFP_KERNEL, + &qup->start_tag.addr); + if (!qup->start_tag.start) { + ret = -ENOMEM; + goto fail; + } + + qup->brx.scratch_tag.start = dma_pool_alloc(qup->dpool, + GFP_KERNEL, + &qup->brx.scratch_tag.addr); + + if (!qup->brx.scratch_tag.start) { + ret = -ENOMEM; + goto fail; + } + + qup->btx.footer_tag.start = dma_pool_alloc(qup->dpool, + GFP_KERNEL, + &qup->btx.footer_tag.addr); + if (!qup->btx.footer_tag.start) { + ret = -ENOMEM; + goto fail; + } + qup->is_dma = 1; } +nodma: /* We support frequencies up to HIGH SPEED Mode (3400KHz) */ if (!clk_freq || clk_freq > clk_freq_max) { dev_err(qup->dev, "clock frequency not supported %d\n", @@ -975,6 +1320,11 @@ fail_runtime: pm_runtime_disable(qup->dev); pm_runtime_set_suspended(qup->dev); fail: + if (qup->btx.dma_tx) + dma_release_channel(qup->btx.dma_tx); + if (qup->brx.dma_rx) + dma_release_channel(qup->brx.dma_rx); + qup_i2c_disable_clocks(qup); return ret; } @@ -983,6 +1333,17 @@ static int qup_i2c_remove(struct platform_device *pdev) { struct qup_i2c_dev *qup = platform_get_drvdata(pdev); + if (qup->is_dma) { + dma_pool_free(qup->dpool, qup->start_tag.start, + qup->start_tag.addr); + dma_pool_free(qup->dpool, qup->brx.scratch_tag.start, + qup->brx.scratch_tag.addr); + dma_pool_free(qup->dpool, qup->btx.footer_tag.start, + qup->btx.footer_tag.addr); + dma_pool_destroy(qup->dpool); + dma_release_channel(qup->btx.dma_tx); + dma_release_channel(qup->brx.dma_rx); + } disable_irq(qup->irq); qup_i2c_disable_clocks(qup); i2c_del_adapter(&qup->adap);