From patchwork Tue Oct 21 01:09:41 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Gong X-Patchwork-Id: 5108801 Return-Path: X-Original-To: patchwork-dmaengine@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id EAE87C11AC for ; Tue, 21 Oct 2014 02:31:03 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F161320176 for ; Tue, 21 Oct 2014 02:31:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1BD6A20122 for ; Tue, 21 Oct 2014 02:30:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754245AbaJUCap (ORCPT ); Mon, 20 Oct 2014 22:30:45 -0400 Received: from mail-by2on0114.outbound.protection.outlook.com ([207.46.100.114]:33103 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754240AbaJUCao (ORCPT ); Mon, 20 Oct 2014 22:30:44 -0400 Received: from DM2PR03CA0043.namprd03.prod.outlook.com (10.141.96.42) by BLUPR03MB033.namprd03.prod.outlook.com (10.255.209.145) with Microsoft SMTP Server (TLS) id 15.1.6.6; Tue, 21 Oct 2014 02:15:37 +0000 Received: from BY2FFO11FD053.protection.gbl (2a01:111:f400:7c0c::149) by DM2PR03CA0043.outlook.office365.com (2a01:111:e400:2428::42) with Microsoft SMTP Server (TLS) id 15.0.1054.13 via Frontend Transport; Tue, 21 Oct 2014 02:15:37 +0000 Received: from az84smr01.freescale.net (192.88.158.2) by BY2FFO11FD053.mail.protection.outlook.com (10.1.15.190) with Microsoft SMTP Server (TLS) id 15.0.1049.20 via Frontend Transport; Tue, 21 Oct 2014 02:15:37 +0000 Received: from shlinux2.ap.freescale.net (shlinux2.ap.freescale.net [10.192.224.44]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id s9L2F6Rv020280; Mon, 20 Oct 2014 19:15:35 -0700 From: Robin Gong To: , , CC: , , Subject: [PATCH v4 3/3] dma: imx-sdma: reorg code to make code clean Date: Tue, 21 Oct 2014 09:09:41 +0800 Message-ID: <1413853781-18384-4-git-send-email-b38343@freescale.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1413853781-18384-1-git-send-email-b38343@freescale.com> References: <1413853781-18384-1-git-send-email-b38343@freescale.com> X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CIP:192.88.158.2; CTRY:US; IPV:CAL; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10019020)(6009001)(189002)(199003)(229853001)(69596002)(44976005)(19580395003)(6806004)(107046002)(104016003)(19580405001)(68736004)(50226001)(4396001)(106466001)(81156004)(97736003)(48376002)(105606002)(50466002)(85306004)(33646002)(95666004)(62966002)(84676001)(76482002)(80022003)(99396003)(46102003)(120916001)(26826002)(102836001)(31966008)(76176999)(50986999)(85852003)(89996001)(93916002)(87936001)(92726001)(92566001)(2201001)(87286001)(88136002)(104166001)(36756003)(77156001)(21056001)(20776003)(47776003)(64706001)(42262002); DIR:OUT; SFP:1102; SCL:1; SRVR:BLUPR03MB033; H:az84smr01.freescale.net; FPR:; MLV:ovrnspm; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; MIME-Version: 1.0 X-Microsoft-Antispam: UriScan:; X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:;SRVR:BLUPR03MB033; X-Forefront-PRVS: 0371762FE7 Received-SPF: Fail (protection.outlook.com: domain of freescale.com does not designate 192.88.158.2 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.158.2; helo=az84smr01.freescale.net; Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=yibin.gong@freescale.com; X-OriginatorOrg: freescale.com Sender: dmaengine-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dmaengine@vger.kernel.org X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP code reorg for transfer prepare and bus width check. Signed-off-by: Robin Gong --- drivers/dma/imx-sdma.c | 127 +++++++++++++++++++++++-------------------------- 1 file changed, 60 insertions(+), 67 deletions(-) diff --git a/drivers/dma/imx-sdma.c b/drivers/dma/imx-sdma.c index 7e8aa2d..b0365c2 100644 --- a/drivers/dma/imx-sdma.c +++ b/drivers/dma/imx-sdma.c @@ -1026,6 +1026,52 @@ static void sdma_free_chan_resources(struct dma_chan *chan) clk_disable(sdma->clk_ahb); } +static int sdma_transfer_init(struct sdma_channel *sdmac, + enum dma_transfer_direction direction) +{ + int ret = 0; + + sdmac->status = DMA_IN_PROGRESS; + sdmac->buf_tail = 0; + sdmac->flags = 0; + sdmac->direction = direction; + + ret = sdma_load_context(sdmac); + if (ret) + return ret; + + sdmac->chn_count = 0; + + return ret; +} + +static int check_bd_buswidth(struct sdma_buffer_descriptor *bd, + struct sdma_channel *sdmac, int count, + dma_addr_t dma_dst, dma_addr_t dma_src) +{ + int ret = 0; + + switch (sdmac->word_size) { + case DMA_SLAVE_BUSWIDTH_4_BYTES: + bd->mode.command = 0; + if ((count | dma_dst | dma_src) & 3) + ret = -EINVAL; + break; + case DMA_SLAVE_BUSWIDTH_2_BYTES: + bd->mode.command = 2; + if ((count | dma_dst | dma_src) & 1) + ret = -EINVAL; + break; + case DMA_SLAVE_BUSWIDTH_1_BYTE: + bd->mode.command = 1; + break; + default: + return -EINVAL; + } + + return ret; +} + static struct dma_async_tx_descriptor *sdma_prep_memcpy( struct dma_chan *chan, dma_addr_t dma_dst, dma_addr_t dma_src, size_t len, unsigned long flags) @@ -1034,7 +1080,7 @@ static struct dma_async_tx_descriptor *sdma_prep_memcpy( struct sdma_engine *sdma = sdmac->sdma; int channel = sdmac->channel; size_t count; - int i = 0, param, ret; + int i = 0, param; struct sdma_buffer_descriptor *bd; if (!chan || !len || sdmac->status == DMA_IN_PROGRESS) @@ -1046,21 +1092,12 @@ static struct dma_async_tx_descriptor *sdma_prep_memcpy( goto err_out; } - sdmac->status = DMA_IN_PROGRESS; - - sdmac->buf_tail = 0; - dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n", &dma_src, &dma_dst, len, channel); - sdmac->direction = DMA_MEM_TO_MEM; - - ret = sdma_load_context(sdmac); - if (ret) + if (sdma_transfer_init(sdmac, DMA_MEM_TO_MEM)) goto err_out; - sdmac->chn_count = 0; - do { count = min_t(size_t, len, SDMA_BD_MAX_CNT); bd = &sdmac->bd[i]; @@ -1068,28 +1105,8 @@ static struct dma_async_tx_descriptor *sdma_prep_memcpy( bd->ext_buffer_addr = dma_dst; bd->mode.count = count; - if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) { - ret = -EINVAL; + if (check_bd_buswidth(bd, sdmac, count, dma_dst, dma_src)) goto err_out; - } - - switch (sdmac->word_size) { - case DMA_SLAVE_BUSWIDTH_4_BYTES: - bd->mode.command = 0; - if ((count | dma_dst | dma_src) & 3) - return NULL; - break; - case DMA_SLAVE_BUSWIDTH_2_BYTES: - bd->mode.command = 2; - if ((count | dma_dst | dma_src) & 1) - return NULL; - break; - case DMA_SLAVE_BUSWIDTH_1_BYTE: - bd->mode.command = 1; - break; - default: - return NULL; - } dma_src += count; dma_dst += count; @@ -1141,21 +1158,10 @@ static struct dma_async_tx_descriptor *sdma_prep_sg( if (sdmac->status == DMA_IN_PROGRESS) return NULL; - sdmac->status = DMA_IN_PROGRESS; - - sdmac->flags = 0; - - sdmac->buf_tail = 0; dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n", src_nents, channel); - sdmac->direction = direction; - - ret = sdma_load_context(sdmac); - if (ret) - goto err_out; - if (src_nents > NUM_BD) { dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n", channel, src_nents, NUM_BD); @@ -1163,7 +1169,9 @@ static struct dma_async_tx_descriptor *sdma_prep_sg( goto err_out; } - sdmac->chn_count = 0; + if (sdma_transfer_init(sdmac, direction)) + goto err_out; + for_each_sg(src_sg, sg_src, src_nents, i) { struct sdma_buffer_descriptor *bd = &sdmac->bd[i]; int param; @@ -1187,30 +1195,15 @@ static struct dma_async_tx_descriptor *sdma_prep_sg( bd->mode.count = count; sdmac->chn_count += count; - if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) { - ret = -EINVAL; + if (direction == DMA_MEM_TO_MEM) + ret = check_bd_buswidth(bd, sdmac, count, + sg_dst->dma_address, + sg_src->dma_address); + else + ret = check_bd_buswidth(bd, sdmac, count, 0, + sg_src->dma_address); + if (ret) goto err_out; - } - - switch (sdmac->word_size) { - case DMA_SLAVE_BUSWIDTH_4_BYTES: - bd->mode.command = 0; - if ((count | sg_src->dma_address | (sg_dst && - (sg_dst->dma_address))) & 3) - return NULL; - break; - case DMA_SLAVE_BUSWIDTH_2_BYTES: - bd->mode.command = 2; - if ((count | sg_src->dma_address | - (sg_dst && (sg_dst->dma_address))) & 1) - return NULL; - break; - case DMA_SLAVE_BUSWIDTH_1_BYTE: - bd->mode.command = 1; - break; - default: - return NULL; - } param = BD_DONE | BD_EXTD | BD_CONT;