From patchwork Sun Aug 18 05:22:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 171552 Delivered-To: patch@linaro.org Received: by 2002:a92:d204:0:0:0:0:0 with SMTP id y4csp1402861ily; Sat, 17 Aug 2019 22:22:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqyRVbnA5You+USdxXQ+HYeTkUfr3xv8YQnlB5vUvtpG4xd4nEKfwFeZSpS+ptpqFobE5tsw X-Received: by 2002:a62:34c4:: with SMTP id b187mr9679450pfa.161.1566105748837; Sat, 17 Aug 2019 22:22:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566105748; cv=none; d=google.com; s=arc-20160816; b=K3QeEMlStISlDf4x18eGarq02Swm4D5+556fypunPsHUOb6I4f75N4dR3Q79/yGbyG NX1WDtCI7LGeSW02xGH/Va81lTxshsxpVhVt32Jptwex60xVBGn4p6hrSzBF0buj1oWL BAaWzNBGvJOc1vaSCqMxKBa2+99Ve8Fd73YR7TJdmQtwvHzPFOgZEndmeVhg9GXoZW8t zmMzzGUSMMvKPmvm9Nox3PldaHtrLH8ho+Fj+9tVOTXy309dkuLK8X8BZMX0Vva2rTFM 4hquHySJPU43HaNEJFmzig+wy27WDThX99qVDy0lhvrtRjCZqrZLwzG8rYGqFhE4hd8a +pJA== 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:dkim-signature; bh=LnUMl6nKmNKEDv9sXm8dLw8sMDEUaepAQtfU3G1sTUw=; b=CJWQGSQpvMMBOKVkYnzCBkCZ7sVtt6xjq9K4e54IzjKqW5eX1g8hpRWcUpI7Ix/Wfg YG2b8UmbWu8nWCGN9tz21pZaH/D6JuEyXQFfwpHiVRB+2dcVCM7NBt2lbAWfRIVKMIXK oZwQ1vCD34P6U/DEojNR7/fo+9B8mfaK9eDbgbzTC/ZofglLDCvpT5PNPmuSeWiINH0j jBpzo8OuVyvhRaCUAAgyfx4tuedt6JdYkveYMdz98stEi6/qydviBJfFnQkYgA6aXc/N I7FGUE+kg7WU6qZ7rvfeoiEKjJdtq4/Mli8hkQcY50eGCKakPuu1oCipwbLHp8iQVu9D karw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=KxCB3m+e; spf=pass (google.com: best guess record for domain of dmaengine-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=dmaengine-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b42si6279508pjc.18.2019.08.17.22.22.28; Sat, 17 Aug 2019 22:22:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of dmaengine-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=KxCB3m+e; spf=pass (google.com: best guess record for domain of dmaengine-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=dmaengine-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726247AbfHRFW2 (ORCPT + 3 others); Sun, 18 Aug 2019 01:22:28 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:46035 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725290AbfHRFW2 (ORCPT ); Sun, 18 Aug 2019 01:22:28 -0400 Received: by mail-pg1-f193.google.com with SMTP id o13so5018476pgp.12; Sat, 17 Aug 2019 22:22:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=LnUMl6nKmNKEDv9sXm8dLw8sMDEUaepAQtfU3G1sTUw=; b=KxCB3m+eDBW1K1mdBHnA9HcoiC6utxfBDmUFeuWKBOmqged672qbWEtZ1FBXJvZr8H qKfIVROkp4whMJoWPsvoCCailgm3o/zPj0MoyRF3+4qp4MwjNu2ic1WNshJdPYmxQBQv +g+feWQHFpUIEWFl6NxLxfg5EOan4jc0PdGajmZSD7H1SYJvTY51915eRwglBgJPBJVA 870kOud9Qy3rb/jY2c0kcnjcmvsTI/ERwYVNyIpyMUOG5cNCHVZniyro+HdnndjjTQwM iAl0CZLTAwtz2LVDE8UBljAogBQ+lOBPDq8ZuNAL48v/vcI32GBWFQetduFLtngYPaT9 KmWA== 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=LnUMl6nKmNKEDv9sXm8dLw8sMDEUaepAQtfU3G1sTUw=; b=m0UQ+KMrq8zn2/bXQc6WOSOvbjX2vG2FTkBWz2SvAiNMMlTIIVRiqTVaPjyWdROuA9 KVdSPgp1W6WBJOal45J3y423PwpkX1ibYLcwCgX30B1yAPNizMg8rzMkhZ2327hAmrqe xgavl9UfvJ6huP8DFkxR4EOn+NRPmvGvNA0Cz+TG3ovklSstKpM2IDIhopwhuhDPZ906 /USim+HIM8UZ+RJHIExIZfjDgLW/2f3SbRJem9OrgiF/Cabwp/PGo9+MCB7OabQB/YQj a0Pg8QBoNpzWSIlOWnPEcdkD3Rp3iAY5BWtD2xTf8BuWhNmpa6OdqaBGd1/VgNqPCbxC cEuA== X-Gm-Message-State: APjAAAW8NLl5RgLXvLYPEt2MFKxjmxV6a9EEwHMpR3aB1tMUMuw28FiZ YnkcnbJxLSDRF7na6prOB1AV1glW X-Received: by 2002:a62:b411:: with SMTP id h17mr17569440pfn.99.1566105747249; Sat, 17 Aug 2019 22:22:27 -0700 (PDT) Received: from localhost.localdomain (S0106d80d17472dbd.wp.shawcable.net. [24.79.253.190]) by smtp.gmail.com with ESMTPSA id x9sm9257303pgp.75.2019.08.17.22.22.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Aug 2019 22:22:26 -0700 (PDT) From: jassisinghbrar@gmail.com To: dmaengine@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: vkoul@kernel.org, robh+dt@kernel.org, Jassi Brar Subject: [PATCH 1/2] dt-bindings: milbeaut-m10v-xdmac: Add Socionext Milbeaut XDMAC bindings Date: Sun, 18 Aug 2019 00:22:24 -0500 Message-Id: <20190818052224.17857-1-jassisinghbrar@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190818052154.17789-1-jassisinghbrar@gmail.com> References: <20190818052154.17789-1-jassisinghbrar@gmail.com> Sender: dmaengine-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dmaengine@vger.kernel.org From: Jassi Brar Document the devicetree bindings for Socionext Milbeaut XDMAC controller. Controller only supports Mem->Mem transfers. Number of physical channels are determined by the number of irqs registered. Signed-off-by: Jassi Brar --- .../bindings/dma/milbeaut-m10v-xdmac.txt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Documentation/devicetree/bindings/dma/milbeaut-m10v-xdmac.txt -- 2.17.1 Reviewed-by: Rob Herring diff --git a/Documentation/devicetree/bindings/dma/milbeaut-m10v-xdmac.txt b/Documentation/devicetree/bindings/dma/milbeaut-m10v-xdmac.txt new file mode 100644 index 000000000000..1f15512e3f19 --- /dev/null +++ b/Documentation/devicetree/bindings/dma/milbeaut-m10v-xdmac.txt @@ -0,0 +1,24 @@ +* Milbeaut AXI DMA Controller + +Milbeaut AXI DMA controller has only memory to memory transfer capability. + +* DMA controller + +Required property: +- compatible: Should be "socionext,milbeaut-m10v-xdmac" +- reg: Should contain DMA registers location and length. +- interrupts: Should contain all of the per-channel DMA interrupts. + Number of channels is configurable - 2, 4 or 8, so + the number of interrupts specfied should be {2,4,8}. +- #dma-cells: Should be 1. + +Example: + xdmac0: dma-controller@1c250000 { + compatible = "socionext,milbeaut-m10v-xdmac"; + reg = <0x1c250000 0x1000>; + interrupts = <0 17 0x4>, + <0 18 0x4>, + <0 19 0x4>, + <0 20 0x4>; + #dma-cells = <1>; + }; From patchwork Sun Aug 18 05:22:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 171553 Delivered-To: patch@linaro.org Received: by 2002:a92:d204:0:0:0:0:0 with SMTP id y4csp1402971ily; Sat, 17 Aug 2019 22:22:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqwypQXvxHa5ShXFArugI7RiY6DtFOZAoSl+lcvgCL9tYV9pP5rUJyW6eKIsxzY5UYXQ1zL3 X-Received: by 2002:a63:6205:: with SMTP id w5mr13316013pgb.199.1566105758532; Sat, 17 Aug 2019 22:22:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566105758; cv=none; d=google.com; s=arc-20160816; b=ggKSzgTMD42+SUZO3z2lOuBOdnvBtNVxBXgVx9Eky//J3k9e0tkDRMWJud7HOVS7ZC fFRKoPy0QaZN8HHIkSgzRjvzfoFhSPaBo8zCUvJxn1PUAz5LxS3nYN3BzVggE1H9bvOJ FRgFtul/4zaJ37bksrNUAsNk3oDJzkk+Ls91ECBGttWU7x/X9x+q2W0gUH7VBU95qi45 RMTzL1yStIdwgDzVqpg91A9l0q+0TsRcsCiXopM4241usvwzqN9NWsoX9FrX3Wmg5/EO etRK6TDkUvcWxY7WgaWF92Wn576CZtyi9ALcRLIBXuKZeEokCS08/O7ZMA7+c2n42cSl LxHA== 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:dkim-signature; bh=R8XVJDdq+Ughl3wL/WaZSRfk1cHWmMC+MS8DhNxKQvc=; b=Io/ej8VjpuSiSg6FnxKsl7xIjKd0zPmFMLqYXUiXzMdB9eHzOsKD9r4Xp0uHb1W5KO 9AOACs/S7UUz7Zzy5Mlzub9CZsq/B7Z0oPJej8HPDHFdGHiB67viwOuZN1TBPLqzfJc6 /OZNkxCEg4+S6rCj0I1oElUHNmqRZrSodj4RdvKh76/1WWd3IEeAuHyLMyOEidOHiYJT TxsV4dgFv+DuYsQY0lwy523WeRRyWDjnv21PNL5Sgl843BxZQKZmyHKLI5YxIUD37bHP CPSsg4gJNDzjHb9BVU//NONGQyVi4BNJSrYAT7JzOAnjxMOK/6NK/PyxXfvY2zfgEB+4 4Npw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="WPL2D/mf"; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u5si2198373pgr.310.2019.08.17.22.22.38; Sat, 17 Aug 2019 22:22:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="WPL2D/mf"; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725290AbfHRFWi (ORCPT + 8 others); Sun, 18 Aug 2019 01:22:38 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:43153 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725942AbfHRFWh (ORCPT ); Sun, 18 Aug 2019 01:22:37 -0400 Received: by mail-pf1-f195.google.com with SMTP id v12so5239102pfn.10; Sat, 17 Aug 2019 22:22:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=R8XVJDdq+Ughl3wL/WaZSRfk1cHWmMC+MS8DhNxKQvc=; b=WPL2D/mfOvIuCQ6Lezmg5LGk1lv5leRnfu8gFBInUGW1bzaYT/nFVjpXoM7kNOFy+O a7elnpdZvkxqKxPPqAJWBKQ/VImG+HxkZgXoYzua083+znPmqsOz6q85UKKfJ1B3TdFj lxBY4EOP6XWymGYmL+6CePEWIaqkuRFo61O/gRCxJ742WsMSALvrDpvTeWiOczv1H9sM uSNfkbqksfKSZkwMYI7GaJ5SCmA0qO/4HKGpTmAdZ6gXtfHUR6MdKBbGBYzQQugL7KYD 2Cb+CZb6mEWkK1b57aoL7lextI9Bq3WVYgAQsx/6JhtNTs70AHl/7/7S3zixXqenmkA3 Nj2Q== 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=R8XVJDdq+Ughl3wL/WaZSRfk1cHWmMC+MS8DhNxKQvc=; b=bMbAlmb7A2nNC1AaYo5i79MJ3/OTstVLd2KQZtKL2ew99tBpAvBxDoW/7VMxaXNiNE /JqjoUj2oZHkcs48eBOkEp1UIGS4uZijNigEQzgUOeacotBGwN8w19MM3L2rORFBLTQ4 c/M6NmUzaZuINw8QevOmmPMICYax6Gh393/zVpbDE2TUOQCRDK9rO730RoqKkZ3MVIo6 VE7J70/rZopiGqr4beLVBjaSpBl9W3RVCJwu2tsoNUIJOmlIk+bj43wSvVcFjKfI/5Yd Xhorx1NW+FvWNoASNvfROEbNUnj4ySIPLjaXB6cTA50HMYT+cyC0cJePx3s6vlykP3OY 8DPQ== X-Gm-Message-State: APjAAAWKeWQyl6TWfE6m3yO7bfEDnXoCUIkcw15RIWWPgLxTDE+RIHFq 6RSXstZ7BUvGLUk7Ho01iewYsLXI X-Received: by 2002:a17:90a:a610:: with SMTP id c16mr14262436pjq.54.1566105756663; Sat, 17 Aug 2019 22:22:36 -0700 (PDT) Received: from localhost.localdomain (S0106d80d17472dbd.wp.shawcable.net. [24.79.253.190]) by smtp.gmail.com with ESMTPSA id l31sm11611134pgm.63.2019.08.17.22.22.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Aug 2019 22:22:36 -0700 (PDT) From: jassisinghbrar@gmail.com To: dmaengine@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: vkoul@kernel.org, robh+dt@kernel.org, Jassi Brar Subject: [PATCH 2/2] dmaengine: milbeaut-xdmac: Add XDMAC driver for Milbeaut platforms Date: Sun, 18 Aug 2019 00:22:33 -0500 Message-Id: <20190818052233.17912-1-jassisinghbrar@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190818052154.17789-1-jassisinghbrar@gmail.com> References: <20190818052154.17789-1-jassisinghbrar@gmail.com> Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org From: Jassi Brar Driver for Socionext Milbeaut XDMAC controller. The controller only supports Mem-To-Mem transfers over upto 8 configurable channels. Signed-off-by: Jassi Brar --- drivers/dma/Kconfig | 10 + drivers/dma/Makefile | 1 + drivers/dma/milbeaut-xdmac.c | 426 +++++++++++++++++++++++++++++++++++ 3 files changed, 437 insertions(+) create mode 100644 drivers/dma/milbeaut-xdmac.c -- 2.17.1 diff --git a/drivers/dma/Kconfig b/drivers/dma/Kconfig index 66979f27f0f3..e0e3b21a0c4c 100644 --- a/drivers/dma/Kconfig +++ b/drivers/dma/Kconfig @@ -358,6 +358,16 @@ config MILBEAUT_HDMAC Say yes here to support the Socionext Milbeaut HDMAC device. +config MILBEAUT_XDMAC + tristate "Milbeaut AXI DMA support" + depends on ARCH_MILBEAUT || COMPILE_TEST + depends on OF + select DMA_ENGINE + select DMA_VIRTUAL_CHANNELS + help + Say yes here to support the Socionext Milbeaut + XDMAC device. + config MMP_PDMA bool "MMP PDMA support" depends on ARCH_MMP || ARCH_PXA || COMPILE_TEST diff --git a/drivers/dma/Makefile b/drivers/dma/Makefile index e4aed0730dea..41e06a845b01 100644 --- a/drivers/dma/Makefile +++ b/drivers/dma/Makefile @@ -47,6 +47,7 @@ obj-$(CONFIG_INTEL_MIC_X100_DMA) += mic_x100_dma.o obj-$(CONFIG_K3_DMA) += k3dma.o obj-$(CONFIG_LPC18XX_DMAMUX) += lpc18xx-dmamux.o obj-$(CONFIG_MILBEAUT_HDMAC) += milbeaut-hdmac.o +obj-$(CONFIG_MILBEAUT_XDMAC) += milbeaut-xdmac.o obj-$(CONFIG_MMP_PDMA) += mmp_pdma.o obj-$(CONFIG_MMP_TDMA) += mmp_tdma.o obj-$(CONFIG_MOXART_DMA) += moxart-dma.o diff --git a/drivers/dma/milbeaut-xdmac.c b/drivers/dma/milbeaut-xdmac.c new file mode 100644 index 000000000000..f2e04541d031 --- /dev/null +++ b/drivers/dma/milbeaut-xdmac.c @@ -0,0 +1,426 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// Copyright (C) 2019 Linaro Ltd. +// Copyright (C) 2019 Socionext Inc. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "virt-dma.h" + +/* global register */ +#define M10V_XDACS 0x00 + +/* channel local register */ +#define M10V_XDTBC 0x10 +#define M10V_XDSSA 0x14 +#define M10V_XDDSA 0x18 +#define M10V_XDSAC 0x1C +#define M10V_XDDAC 0x20 +#define M10V_XDDCC 0x24 +#define M10V_XDDES 0x28 +#define M10V_XDDPC 0x2C +#define M10V_XDDSD 0x30 + +#define M10V_XDACS_XE BIT(28) + +#define M10V_DEFBS 0x3 +#define M10V_DEFBL 0xf + +#define M10V_XDSAC_SBS GENMASK(17, 16) +#define M10V_XDSAC_SBL GENMASK(11, 8) + +#define M10V_XDDAC_DBS GENMASK(17, 16) +#define M10V_XDDAC_DBL GENMASK(11, 8) + +#define M10V_XDDES_CE BIT(28) +#define M10V_XDDES_SE BIT(24) +#define M10V_XDDES_SA BIT(15) +#define M10V_XDDES_TF GENMASK(23, 20) +#define M10V_XDDES_EI BIT(1) +#define M10V_XDDES_TI BIT(0) + +#define M10V_XDDSD_IS_MASK GENMASK(3, 0) +#define M10V_XDDSD_IS_NORMAL 0x8 + +#define MLB_XDMAC_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \ + BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \ + BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \ + BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)) + +struct milbeaut_xdmac_desc { + struct virt_dma_desc vd; + size_t len; + dma_addr_t src; + dma_addr_t dst; +}; + +struct milbeaut_xdmac_chan { + struct virt_dma_chan vc; + struct milbeaut_xdmac_device *mdev; + struct milbeaut_xdmac_desc *md; + void __iomem *reg_ch_base; + int irq; + struct dma_slave_config cfg; +}; + +struct milbeaut_xdmac_device { + struct dma_device ddev; + void __iomem *reg_base; + struct milbeaut_xdmac_chan channels[0]; +}; + +static struct milbeaut_xdmac_chan * +to_milbeaut_xdmac_chan(struct virt_dma_chan *vc) +{ + return container_of(vc, struct milbeaut_xdmac_chan, vc); +} + +static struct milbeaut_xdmac_desc * +to_milbeaut_xdmac_desc(struct virt_dma_desc *vd) +{ + return container_of(vd, struct milbeaut_xdmac_desc, vd); +} + +/* mc->vc.lock must be held by caller */ +static struct milbeaut_xdmac_desc * +milbeaut_xdmac_next_desc(struct milbeaut_xdmac_chan *mc) +{ + struct virt_dma_desc *vd; + + vd = vchan_next_desc(&mc->vc); + if (!vd) { + mc->md = NULL; + return NULL; + } + + list_del(&vd->node); + + mc->md = to_milbeaut_xdmac_desc(vd); + + return mc->md; +} + +/* mc->vc.lock must be held by caller */ +static void milbeaut_chan_start(struct milbeaut_xdmac_chan *mc, + struct milbeaut_xdmac_desc *md) +{ + u32 val; + + /* Setup the channel */ + val = md->len - 1; + writel_relaxed(val, mc->reg_ch_base + M10V_XDTBC); + + val = md->src; + writel_relaxed(val, mc->reg_ch_base + M10V_XDSSA); + + val = md->dst; + writel_relaxed(val, mc->reg_ch_base + M10V_XDDSA); + + val = readl_relaxed(mc->reg_ch_base + M10V_XDSAC); + val &= ~(M10V_XDSAC_SBS | M10V_XDSAC_SBL); + val |= FIELD_PREP(M10V_XDSAC_SBS, M10V_DEFBS) | + FIELD_PREP(M10V_XDSAC_SBL, M10V_DEFBL); + writel_relaxed(val, mc->reg_ch_base + M10V_XDSAC); + + val = readl_relaxed(mc->reg_ch_base + M10V_XDDAC); + val &= ~(M10V_XDDAC_DBS | M10V_XDDAC_DBL); + val |= FIELD_PREP(M10V_XDDAC_DBS, M10V_DEFBS) | + FIELD_PREP(M10V_XDDAC_DBL, M10V_DEFBL); + writel_relaxed(val, mc->reg_ch_base + M10V_XDDAC); + + /* Start the channel */ + val = readl_relaxed(mc->reg_ch_base + M10V_XDDES); + val &= ~(M10V_XDDES_CE | M10V_XDDES_SE | M10V_XDDES_TF | + M10V_XDDES_EI | M10V_XDDES_TI); + val |= FIELD_PREP(M10V_XDDES_CE, 1) | FIELD_PREP(M10V_XDDES_SE, 1) | + FIELD_PREP(M10V_XDDES_TF, 1) | FIELD_PREP(M10V_XDDES_EI, 1) | + FIELD_PREP(M10V_XDDES_TI, 1); + writel_relaxed(val, mc->reg_ch_base + M10V_XDDES); +} + +/* mc->vc.lock must be held by caller */ +static void milbeaut_xdmac_start(struct milbeaut_xdmac_chan *mc) +{ + struct milbeaut_xdmac_desc *md; + + md = milbeaut_xdmac_next_desc(mc); + if (md) + milbeaut_chan_start(mc, md); +} + +static irqreturn_t milbeaut_xdmac_interrupt(int irq, void *dev_id) +{ + struct milbeaut_xdmac_chan *mc = dev_id; + struct milbeaut_xdmac_desc *md; + irqreturn_t ret = IRQ_HANDLED; + unsigned long flags; + u32 val; + + spin_lock_irqsave(&mc->vc.lock, flags); + + /* Ack and Stop */ + val = FIELD_PREP(M10V_XDDSD_IS_MASK, 0x0); + writel_relaxed(val, mc->reg_ch_base + M10V_XDDSD); + + md = mc->md; + if (!md) + goto out; + + vchan_cookie_complete(&md->vd); + + milbeaut_xdmac_start(mc); +out: + spin_unlock_irqrestore(&mc->vc.lock, flags); + return ret; +} + +static void milbeaut_xdmac_free_chan_resources(struct dma_chan *chan) +{ + vchan_free_chan_resources(to_virt_chan(chan)); +} + +static struct dma_async_tx_descriptor * +milbeaut_xdmac_prep_memcpy(struct dma_chan *chan, dma_addr_t dst, + dma_addr_t src, size_t len, unsigned long flags) +{ + struct virt_dma_chan *vc = to_virt_chan(chan); + struct milbeaut_xdmac_desc *md; + + md = kzalloc(sizeof(*md), GFP_NOWAIT); + if (!md) + return NULL; + + md->len = len; + md->src = src; + md->dst = dst; + + return vchan_tx_prep(vc, &md->vd, flags); +} + +static int milbeaut_xdmac_terminate_all(struct dma_chan *chan) +{ + struct virt_dma_chan *vc = to_virt_chan(chan); + struct milbeaut_xdmac_chan *mc = to_milbeaut_xdmac_chan(vc); + unsigned long flags; + int ret = 0; + u32 val; + + LIST_HEAD(head); + + spin_lock_irqsave(&vc->lock, flags); + + /* Halt the channel */ + val = readl(mc->reg_ch_base + M10V_XDDES); + val &= ~M10V_XDDES_CE; + val |= FIELD_PREP(M10V_XDDES_CE, 0); + writel(val, mc->reg_ch_base + M10V_XDDES); + + if (mc->md) { + vchan_terminate_vdesc(&mc->md->vd); + mc->md = NULL; + } + + vchan_get_all_descriptors(vc, &head); + + spin_unlock_irqrestore(&vc->lock, flags); + + vchan_dma_desc_free_list(vc, &head); + + return ret; +} + +static void milbeaut_xdmac_synchronize(struct dma_chan *chan) +{ + vchan_synchronize(to_virt_chan(chan)); +} + +static void milbeaut_xdmac_issue_pending(struct dma_chan *chan) +{ + struct virt_dma_chan *vc = to_virt_chan(chan); + struct milbeaut_xdmac_chan *mc = to_milbeaut_xdmac_chan(vc); + unsigned long flags; + + spin_lock_irqsave(&vc->lock, flags); + + if (vchan_issue_pending(vc) && !mc->md) + milbeaut_xdmac_start(mc); + + spin_unlock_irqrestore(&vc->lock, flags); +} + +static void milbeaut_xdmac_desc_free(struct virt_dma_desc *vd) +{ + kfree(to_milbeaut_xdmac_desc(vd)); +} + +static int milbeaut_xdmac_chan_init(struct platform_device *pdev, + struct milbeaut_xdmac_device *mdev, + int chan_id) +{ + struct device *dev = &pdev->dev; + struct milbeaut_xdmac_chan *mc = &mdev->channels[chan_id]; + char *irq_name; + int irq, ret; + + irq = platform_get_irq(pdev, chan_id); + if (irq < 0) { + dev_err(&pdev->dev, "failed to get IRQ number for ch%d\n", + chan_id); + return irq; + } + + irq_name = devm_kasprintf(dev, GFP_KERNEL, "milbeaut-xdmac-%d", + chan_id); + if (!irq_name) + return -ENOMEM; + + ret = devm_request_irq(dev, irq, milbeaut_xdmac_interrupt, + IRQF_SHARED, irq_name, mc); + if (ret) + return ret; + + mc->mdev = mdev; + mc->reg_ch_base = mdev->reg_base + chan_id * 0x30; + + mc->vc.desc_free = milbeaut_xdmac_desc_free; + vchan_init(&mc->vc, &mdev->ddev); + + return 0; +} + +static void enable_xdmac(struct milbeaut_xdmac_device *mdev) +{ + unsigned int val; + + val = readl(mdev->reg_base + M10V_XDACS); + val |= M10V_XDACS_XE; + writel(val, mdev->reg_base + M10V_XDACS); +} + +static void disable_xdmac(struct milbeaut_xdmac_device *mdev) +{ + unsigned int val; + + val = readl(mdev->reg_base + M10V_XDACS); + val &= ~M10V_XDACS_XE; + writel(val, mdev->reg_base + M10V_XDACS); +} + +static int milbeaut_xdmac_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct milbeaut_xdmac_device *mdev; + struct dma_device *ddev; + struct resource *res; + int nr_chans, ret, i; + + nr_chans = platform_irq_count(pdev); + if (nr_chans < 0) + return nr_chans; + + mdev = devm_kzalloc(dev, struct_size(mdev, channels, nr_chans), + GFP_KERNEL); + if (!mdev) + return -ENOMEM; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + mdev->reg_base = devm_ioremap_resource(dev, res); + if (IS_ERR(mdev->reg_base)) + return PTR_ERR(mdev->reg_base); + + ddev = &mdev->ddev; + ddev->dev = dev; + dma_cap_set(DMA_MEMCPY, ddev->cap_mask); + ddev->src_addr_widths = MLB_XDMAC_BUSWIDTHS; + ddev->dst_addr_widths = MLB_XDMAC_BUSWIDTHS; + ddev->device_free_chan_resources = milbeaut_xdmac_free_chan_resources; + ddev->device_prep_dma_memcpy = milbeaut_xdmac_prep_memcpy; + ddev->device_terminate_all = milbeaut_xdmac_terminate_all; + ddev->device_synchronize = milbeaut_xdmac_synchronize; + ddev->device_tx_status = dma_cookie_status; + ddev->device_issue_pending = milbeaut_xdmac_issue_pending; + INIT_LIST_HEAD(&ddev->channels); + + for (i = 0; i < nr_chans; i++) { + ret = milbeaut_xdmac_chan_init(pdev, mdev, i); + if (ret) + return ret; + } + + enable_xdmac(mdev); + + ret = dma_async_device_register(ddev); + if (ret) + return ret; + + ret = of_dma_controller_register(dev->of_node, + of_dma_simple_xlate, mdev); + if (ret) + goto unregister_dmac; + + platform_set_drvdata(pdev, mdev); + + return 0; + +unregister_dmac: + dma_async_device_unregister(ddev); + return ret; +} + +static int milbeaut_xdmac_remove(struct platform_device *pdev) +{ + struct milbeaut_xdmac_device *mdev = platform_get_drvdata(pdev); + struct dma_chan *chan; + int ret; + + /* + * Before reaching here, almost all descriptors have been freed by the + * ->device_free_chan_resources() hook. However, each channel might + * be still holding one descriptor that was on-flight at that moment. + * Terminate it to make sure this hardware is no longer running. Then, + * free the channel resources once again to avoid memory leak. + */ + list_for_each_entry(chan, &mdev->ddev.channels, device_node) { + ret = dmaengine_terminate_sync(chan); + if (ret) + return ret; + milbeaut_xdmac_free_chan_resources(chan); + } + + of_dma_controller_free(pdev->dev.of_node); + dma_async_device_unregister(&mdev->ddev); + + disable_xdmac(mdev); + + return 0; +} + +static const struct of_device_id milbeaut_xdmac_match[] = { + { .compatible = "socionext,milbeaut-m10v-xdmac" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, milbeaut_xdmac_match); + +static struct platform_driver milbeaut_xdmac_driver = { + .probe = milbeaut_xdmac_probe, + .remove = milbeaut_xdmac_remove, + .driver = { + .name = "milbeaut-m10v-xdmac", + .of_match_table = milbeaut_xdmac_match, + }, +}; +module_platform_driver(milbeaut_xdmac_driver); + +MODULE_DESCRIPTION("Milbeaut XDMAC DmaEngine driver"); +MODULE_LICENSE("GPL v2");