From patchwork Thu Jan 26 16:21:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Kurtz X-Patchwork-Id: 9539717 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4C3F4604A0 for ; Thu, 26 Jan 2017 16:22:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3C28C205F6 for ; Thu, 26 Jan 2017 16:22:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2FCDB2818E; Thu, 26 Jan 2017 16:22:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 79208205F6 for ; Thu, 26 Jan 2017 16:22:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753369AbdAZQWa (ORCPT ); Thu, 26 Jan 2017 11:22:30 -0500 Received: from mail-pf0-f178.google.com ([209.85.192.178]:35633 "EHLO mail-pf0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753087AbdAZQW1 (ORCPT ); Thu, 26 Jan 2017 11:22:27 -0500 Received: by mail-pf0-f178.google.com with SMTP id f144so66353977pfa.2 for ; Thu, 26 Jan 2017 08:22:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=NFe8oHpuU/3I37yN0U8ihUofwz6qNN90DseixYXrsPE=; b=RPgwQGT7ebLQucpCGOXUKL8kkXwvI9d3h3rZ+zxbzoE3E1wSlqHpAavlJ1ZKrZyYgc D+iVZCl0OMWuyaTHRSIiSTm3ivB39jkkUvGvxuf/UskW+pSzn+4iaJU2O2avDxd/fSAi 5WHM632hxGycwhbFT8m6azhHa2ECq96xkIGFA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=NFe8oHpuU/3I37yN0U8ihUofwz6qNN90DseixYXrsPE=; b=S+anfnet10XDbZo+kxro3jvCygjyW38MOoNOrNnX2aR/CoqzgVmaFRTCGroMCo2mES QJg2MHWl5XS0Mx8aHucXU5XGSFIvmSSG8Tqcl/HAIcHRK1r7ZB/TuB9aAPcHOIzYhk5k MeVYORh8QZTC1Rfzs/38r74p4oE5z9xyAGyvhnmCOtJTcNUtA4PkIeSdfHLiysyf/ocl O9Ho/rA7yS+uAnkcRpNAkkL+PBaNNmseEy0Pa8rl57teqyFXSc4xGZoZ9mCFDV4xktnq i0s63t+Sb07jdvW5UFhOvbfyaaxW6URqjBX/gmPZIhP3P3axQ8eQongjxTqiKmF++SCV lWkQ== X-Gm-Message-State: AIkVDXLAe9bZ6HTN0sN0xLsWFo/r2JEfdVW13gKhyXlz8+u0H1xtuAbheMLB1ZLZOjWU+9G5 X-Received: by 10.98.92.4 with SMTP id q4mr3905165pfb.151.1485447731528; Thu, 26 Jan 2017 08:22:11 -0800 (PST) Received: from djkurtz-z840.tpe.corp.google.com ([172.30.210.11]) by smtp.gmail.com with ESMTPSA id z77sm4607751pfk.47.2017.01.26.08.22.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 26 Jan 2017 08:22:11 -0800 (PST) From: Daniel Kurtz Cc: dtor@chromium.org, groeck@chromium.org, drinkcat@chromium.org, Robin Murphy , Daniel Kurtz , Leilk Liu , Mark Brown , Matthias Brugger , linux-spi@vger.kernel.org (open list:SPI SUBSYSTEM), linux-kernel@vger.kernel.org (open list), linux-arm-kernel@lists.infradead.org (moderated list:ARM/Mediatek SoC support), linux-mediatek@lists.infradead.org (moderated list:ARM/Mediatek SoC support) Subject: [PATCH 2/2] spi: mediatek: Only do dma for 4-byte aligned buffers Date: Fri, 27 Jan 2017 00:21:54 +0800 Message-Id: <20170126162154.124287-2-djkurtz@chromium.org> X-Mailer: git-send-email 2.11.0.483.g087da7b7c-goog In-Reply-To: <20170126162154.124287-1-djkurtz@chromium.org> References: <20170126162154.124287-1-djkurtz@chromium.org> To: unlisted-recipients:; (no To-header on input) Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Mediatek SPI DMA only works when tx and rx buffer addresses are 4-byte aligned. Unaligned DMA transactions appeared to work previously, since we the spi core was incorrectly using the spi_master device for dma, which had a 0 dma_mask, and therefore the swiotlb dma map operations were falling back to using bounce buffers. Since each DMA transaction would use its own buffer, the mapped starting address of each transaction was always aligned. When doing real DMA, the mapped address will share the alignment of the raw tx/rx buffer provided by the SPI user, which may or may not be aligned. If a buffer is not aligned, we cannot use DMA, and must use FIFO based transaction instead. So, this patch implements a scheme that allows using the FIFO for arbitrary length transactions (larger than the 32-byte FIFO size) by reloading the FIFO in the interrupt handler. Signed-off-by: Daniel Kurtz Cc: Leilk Liu --- drivers/spi/spi-mt65xx.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/drivers/spi/spi-mt65xx.c b/drivers/spi/spi-mt65xx.c index 899d7a8f0889..4b592fc25194 100644 --- a/drivers/spi/spi-mt65xx.c +++ b/drivers/spi/spi-mt65xx.c @@ -73,7 +73,7 @@ #define MTK_SPI_IDLE 0 #define MTK_SPI_PAUSED 1 -#define MTK_SPI_MAX_FIFO_SIZE 32 +#define MTK_SPI_MAX_FIFO_SIZE 32U #define MTK_SPI_PACKET_SIZE 1024 struct mtk_spi_compatible { @@ -333,7 +333,7 @@ static int mtk_spi_fifo_transfer(struct spi_master *master, struct mtk_spi *mdata = spi_master_get_devdata(master); mdata->cur_transfer = xfer; - mdata->xfer_len = xfer->len; + mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, xfer->len); mtk_spi_prepare_transfer(master, xfer); mtk_spi_setup_packet(master); @@ -410,7 +410,10 @@ static bool mtk_spi_can_dma(struct spi_master *master, struct spi_device *spi, struct spi_transfer *xfer) { - return xfer->len > MTK_SPI_MAX_FIFO_SIZE; + /* Buffers for DMA transactions must be 4-byte aligned */ + return (xfer->len > MTK_SPI_MAX_FIFO_SIZE && + (unsigned long)xfer->tx_buf % 4 == 0 && + (unsigned long)xfer->rx_buf % 4 == 0); } static int mtk_spi_setup(struct spi_device *spi) @@ -451,7 +454,33 @@ static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id) ®_val, remainder); } } - spi_finalize_current_transfer(master); + + trans->len -= mdata->xfer_len; + if (!trans->len) { + spi_finalize_current_transfer(master); + return IRQ_HANDLED; + } + + if (trans->tx_buf) + trans->tx_buf += mdata->xfer_len; + if (trans->rx_buf) + trans->rx_buf += mdata->xfer_len; + + mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, trans->len); + mtk_spi_setup_packet(master); + + cnt = trans->len / 4; + iowrite32_rep(mdata->base + SPI_TX_DATA_REG, trans->tx_buf, cnt); + + remainder = trans->len % 4; + if (remainder > 0) { + reg_val = 0; + memcpy(®_val, trans->tx_buf + (cnt * 4), remainder); + writel(reg_val, mdata->base + SPI_TX_DATA_REG); + } + + mtk_spi_enable_transfer(master); + return IRQ_HANDLED; }