Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp280444imm; Fri, 6 Jul 2018 19:51:31 -0700 (PDT) X-Google-Smtp-Source: AAOMgpemNK7yNquRHcFLBoAi34pisEOWWM7614ebDt9VmoEo6vrnGBpWeyLFFMLjmAGkDmCZT2Eg X-Received: by 2002:a65:64d3:: with SMTP id t19-v6mr11348904pgv.148.1530931891053; Fri, 06 Jul 2018 19:51:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530931891; cv=none; d=google.com; s=arc-20160816; b=cmhcJZ2S97tU6JDnyq8o6wIsBq2O0PGOS6vcx+Vhf1U9JxpPWKynHMsEJN1uJ4DpUI l2s+eQsT+eHfDOZfKAeu5jorIMrfNVWOqFD7rNWyK3J5wLCgY0vJ5rBB8ESJ4uWm4F5g sZWsUgiULKnUUe+ZP2+iN9IewmSHUIa1ocG9kV3Ck0KtOXa34FpRrW7v1k/XFU92YS1G L8WrZqdob32XWR0edGOfKwF2OYmHe+qPk7npoPbnZqRNocA3oZ8tRqHyvnELOnOamB7z V/SDaepO7Hzszv+1PmyZVu7vQlrXayBr8I56ulURd9TyHm7y0Z8OFU4qLhtXf8OWCP06 fU/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=c8UJKGcjVm6uOfHXoVfJeA60TaR2DZt9WJ27GOnc+QE=; b=Jpih0tyauUfjQB15dxYrUpseAmkeFQDv4fsHUyH+LaUmUsicMacOua4AVDRybjYNqK 1tzYboeQWbJmiWvkuoBU6UhMtL29wJnlyVBrK6wETUWb8975iM/wqIvdEUFkklJ3w4as RFqzIQWewhIZdmzcrc2cm8LJhbxugdPnYOle4EiKfMt4EYF45ggjYz92LDoFcMerkbJ4 O2d9TWENXXKvu5OfS2FWtbT6g/Qmb4Akx1+F2famjAsN8DETXZxYE7H1ejSmiUjJ4Ag7 3+zvt/UsaII/a3P7WjoNVb9SWlQdF1Fc+s3xuMSFUPyPEQrXwLWktQMwh7ISSmE/Yp8k JJlw== 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 s14-v6si9014772pga.21.2018.07.06.19.51.17; Fri, 06 Jul 2018 19:51:31 -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 S933197AbeGGCuR (ORCPT + 99 others); Fri, 6 Jul 2018 22:50:17 -0400 Received: from mailgw01.mediatek.com ([210.61.82.183]:23874 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S932974AbeGGCtp (ORCPT ); Fri, 6 Jul 2018 22:49:45 -0400 X-UUID: e1f501fcac0e4084be99745e61c6b31b-20180707 Received: from mtkexhb02.mediatek.inc [(172.21.101.103)] by mailgw01.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 1095399838; Sat, 07 Jul 2018 10:49:41 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs03n1.mediatek.inc (172.21.101.181) with Microsoft SMTP Server (TLS) id 15.0.1210.3; Sat, 7 Jul 2018 10:49:39 +0800 Received: from localhost.localdomain (10.17.3.153) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1210.3 via Frontend Transport; Sat, 7 Jul 2018 10:49:39 +0800 From: Jun Gao To: Wolfram Sang CC: , , , , , , Jun Gao Subject: [PATCH 3/3] i2c: mediatek: Use DMA safe buffers for i2c transactions Date: Sat, 7 Jul 2018 10:49:13 +0800 Message-ID: <1530931753-8264-4-git-send-email-jun.gao@mediatek.com> X-Mailer: git-send-email 1.8.1.1.dirty In-Reply-To: <1530931753-8264-1-git-send-email-jun.gao@mediatek.com> References: <1530931753-8264-1-git-send-email-jun.gao@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jun Gao DMA mode will always be used in i2c transactions, try to allocate a DMA safe buffer if the buf of struct i2c_msg used is not DMA safe. Signed-off-by: Jun Gao --- drivers/i2c/busses/i2c-mt65xx.c | 62 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/drivers/i2c/busses/i2c-mt65xx.c b/drivers/i2c/busses/i2c-mt65xx.c index 806e8b90..dd014ee 100644 --- a/drivers/i2c/busses/i2c-mt65xx.c +++ b/drivers/i2c/busses/i2c-mt65xx.c @@ -441,6 +441,8 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, u16 control_reg; u16 restart_flag = 0; u32 reg_4g_mode; + u8 *dma_rd_buf; + u8 *dma_wr_buf; dma_addr_t rpaddr = 0; dma_addr_t wpaddr = 0; int ret; @@ -500,10 +502,18 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, if (i2c->op == I2C_MASTER_RD) { writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); writel(I2C_DMA_CON_RX, i2c->pdmabase + OFFSET_CON); - rpaddr = dma_map_single(i2c->dev, msgs->buf, + + dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 0); + if (!dma_rd_buf) + return -ENOMEM; + + rpaddr = dma_map_single(i2c->dev, dma_rd_buf, msgs->len, DMA_FROM_DEVICE); - if (dma_mapping_error(i2c->dev, rpaddr)) + if (dma_mapping_error(i2c->dev, rpaddr)) { + i2c_free_dma_safe_msg_buf(msgs, dma_rd_buf); + return -ENOMEM; + } if (i2c->dev_comp->support_33bits) { reg_4g_mode = mtk_i2c_set_4g_mode(rpaddr); @@ -515,10 +525,18 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, } else if (i2c->op == I2C_MASTER_WR) { writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG); writel(I2C_DMA_CON_TX, i2c->pdmabase + OFFSET_CON); - wpaddr = dma_map_single(i2c->dev, msgs->buf, + + dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 0); + if (!dma_wr_buf) + return -ENOMEM; + + wpaddr = dma_map_single(i2c->dev, dma_wr_buf, msgs->len, DMA_TO_DEVICE); - if (dma_mapping_error(i2c->dev, wpaddr)) + if (dma_mapping_error(i2c->dev, wpaddr)) { + i2c_free_dma_safe_msg_buf(msgs, dma_wr_buf); + return -ENOMEM; + } if (i2c->dev_comp->support_33bits) { reg_4g_mode = mtk_i2c_set_4g_mode(wpaddr); @@ -530,16 +548,39 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, } else { writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG); writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_CON); - wpaddr = dma_map_single(i2c->dev, msgs->buf, + + dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 0); + if (!dma_wr_buf) + return -ENOMEM; + + wpaddr = dma_map_single(i2c->dev, dma_wr_buf, msgs->len, DMA_TO_DEVICE); - if (dma_mapping_error(i2c->dev, wpaddr)) + if (dma_mapping_error(i2c->dev, wpaddr)) { + i2c_free_dma_safe_msg_buf(msgs, dma_wr_buf); + return -ENOMEM; - rpaddr = dma_map_single(i2c->dev, (msgs + 1)->buf, + } + + dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 0); + if (!dma_rd_buf) { + dma_unmap_single(i2c->dev, wpaddr, + msgs->len, DMA_TO_DEVICE); + + i2c_free_dma_safe_msg_buf(msgs, dma_wr_buf); + + return -ENOMEM; + } + + rpaddr = dma_map_single(i2c->dev, dma_rd_buf, (msgs + 1)->len, DMA_FROM_DEVICE); if (dma_mapping_error(i2c->dev, rpaddr)) { dma_unmap_single(i2c->dev, wpaddr, msgs->len, DMA_TO_DEVICE); + + i2c_free_dma_safe_msg_buf(msgs, dma_wr_buf); + i2c_free_dma_safe_msg_buf((msgs + 1), dma_rd_buf); + return -ENOMEM; } @@ -578,14 +619,21 @@ static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs, if (i2c->op == I2C_MASTER_WR) { dma_unmap_single(i2c->dev, wpaddr, msgs->len, DMA_TO_DEVICE); + + i2c_release_dma_safe_msg_buf(msgs, dma_wr_buf); } else if (i2c->op == I2C_MASTER_RD) { dma_unmap_single(i2c->dev, rpaddr, msgs->len, DMA_FROM_DEVICE); + + i2c_release_dma_safe_msg_buf(msgs, dma_rd_buf); } else { dma_unmap_single(i2c->dev, wpaddr, msgs->len, DMA_TO_DEVICE); dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len, DMA_FROM_DEVICE); + + i2c_release_dma_safe_msg_buf(msgs, dma_wr_buf); + i2c_release_dma_safe_msg_buf((msgs + 1), dma_rd_buf); } if (ret == 0) { -- 1.8.1.1