From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752061AbbIGMtQ (ORCPT ); Mon, 7 Sep 2015 08:49:16 -0400 Received: from mail-bn1on0064.outbound.protection.outlook.com ([157.56.110.64]:19981 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750969AbbIGMtG (ORCPT ); Mon, 7 Sep 2015 08:49:06 -0400 Authentication-Results: spf=pass (sender IP is 149.199.60.83) smtp.mailfrom=xilinx.com; vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=bestguesspass action=none header.from=xilinx.com; From: Kedareswara rao Appana To: , , , , , CC: , , Subject: [PATCH v6 2/2] dmaengine: Add Xilinx AXI Central Direct Memory Access Engine driver support Date: Mon, 7 Sep 2015 18:03:18 +0530 Message-ID: <1441629198-17663-1-git-send-email-appanad@xilinx.com> X-Mailer: git-send-email 2.1.2 X-RCIS-Action: ALLOW X-TM-AS-Product-Ver: IMSS-7.1.0.1224-8.0.0.1202-21798.005 X-TM-AS-User-Approved-Sender: Yes;Yes X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BN1AFFO11FD038;1:GjxCuaJj0Pmc3nGtqJ7M8Jza8H9+LCssL2rgWrGnFZXF+UV4SBeftRoKamOgiwMQTH/P0CTDoL6w2+1BqxuxZiKaUunnP9dhRZg+sei2z31I1rEOotmkBZqNiGP6ae8bBl6AuGfRtTvSHhf6P+4/25PEg/R7Sr7zDi9cNHnH5eFleMEGP/MdwcDszrz7SBAPaINVhFe7R0Bzt3gFf1Bo36Iu+GC0UnwG8h0K9uhDvyMtXhCNqomabhnZmFncf/2ERhrQ1z+1XTrmFs6SvZYGyJM96gNErY1v6tSBPKfxtq/YZx9yszHo50ZXsqlZ7NSj61FpGJ3lZCwTJ5i2TDYVPRAWTgEsSCwyjG7x+ZCMF648tB0zG4BkNfIeTcD6JpY5+A/432XxjtFgA0AT4JNFMsfmNjqdGWKPPcne7IPxHiE= X-Forefront-Antispam-Report: CIP:149.199.60.83;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(2980300002)(438002)(189002)(199003)(50944005)(5007970100001)(7049001)(48376002)(4001450100002)(36756003)(36386004)(81156007)(50226001)(33646002)(229853001)(106466001)(4001540100001)(47776003)(64706001)(45336002)(5001770100001)(103686003)(46102003)(46386002)(63266004)(50986999)(42186005)(87936001)(2201001)(52956003)(92566002)(5003940100001)(77156002)(62966003)(50466002)(5001830100001)(5001860100001)(189998001)(5001960100002)(19580395003)(551934003)(19580405001)(86362001)(575784001)(6806004)(2004002)(107986001)(90966001);DIR:OUT;SFP:1101;SCL:1;SRVR:BN1AFFO11HUB039;H:xsj-pvapsmtpgw01;FPR:;SPF:Pass;PTR:unknown-60-83.xilinx.com;MX:1;A:1;LANG:en; MIME-Version: 1.0 Content-Type: text/plain X-Microsoft-Exchange-Diagnostics: 1;BN1AFFO11HUB039;2:aIbhheIl3YO6SycpmlV2UQqbDDiBtyysCynrnpi4CpyZV0+AJJ1UdFFIDeUVV3iooCgAbkXrQAzQX5/lPcQ9EpXBpZ/5AAyKwtVLkBEfm77O4+kRpoHMoQudXhu+uqh2fppPjs7kF58XJXD5bgGlXfnorj0odrlVW3oHtKRl1oo=;3:FemdEu1KkczdCR9kFkOe7E3mxooKaUEn8BbZ1qDpgdIC/BvuGrYVcj9u8NQZLon2E6XN0+fMFtPWMPWgrZokGlMvZGV0O+QG4Wh6seuki4ncRtwk6OTj6FH/vwg05I3dCcS5pNDGhQPkoKorebIfuSEO/EEkJ0Y6uVHfUhr3XjhtI0ivRYfKGodQQerZ0Cwe2qUL5cYJKq9d50pjJnm0BroBsiMpQ9tuNbZJAEcZbDv3pmWw11N4BGp2p0Dx5yvIdefkkz6OShDy+9fuzK3zNA==;25:u/a5HsWtjHMNcFKvd4bxRhyIFrZ1n3/6yFKqs++8AD6DgAPkWbozEObSHnA90IkZfvVzZVujTcRxIc7qPt/sScLbmuLNice/pm+PAH//BObdp7n6Ij8dDSPj5V5sRStVZSZh6284CvVKXop/as+YOXCpz1fKsqf8oMiT5mbYNgOGcRxfN3BuYL4Bily1ro5PBEiJWkoMNrVhg14aJPuTQWZXrdBN23ANmZL5vZsy3m/8qdoMOj5+6DnJ5f2sUmXHQNzKOVnzXdqFUFmX6GLjdA== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(8251501001);SRVR:BN1AFFO11HUB039; X-Microsoft-Exchange-Diagnostics: 1;BN1AFFO11HUB039;20:9ea0g1ATqFB8MSYMDvrqbK2KiIGuVuoIN4F08CEThikKGwbxAb+AWmUPJmZ0JDVLpI8AlhXE3xaeeOC22Muag5bz7ilzCuenGykoRN6hBFezh78uMqD2ZzqRzoo2SPzV8wW2obWEx/H+cCwLh9tNLTQUinkfrSs0cQAbWOBbfoyBLDCQ1gqYqZhpP9SjxZO3jhrtaUIrF/GgrXRcXuyB52gC7gg31fQJ/lklVVaSYEAjudBZoQJRjz9KkIycIteL9LsluvlihsTC/GDF5BR2P0z8ZdbvNOK8Zr7RTP/cC3TwT5NYdeZkOqLE2T3ZRaLzceT+tMvxC7JV7Z0eNnmEzwBVNnvyocinhxsXjszrcXVz+01z8sHkipeaMnqT0XZvm+piaJiIrRY8AClatvbfxGnbaprGkBACWTR+myI/rnJnVG9usV54ZGLUl6aOEV+MNKiwHog5W3UJD2/843ivzW5IVuR0qBKmURnVlT8hZASVv0R9JewSXEdNeSX6dEJa;4:WbPaIPMbx1w093DbAcuF5mYd64dIEnX3RTTnXSNKrMTw5LfGgaSmu159QlO1lx6hNmedoeh6CzEtd8uUw49R3fsG30cFUdxTTvIdBJDekA8JV7eQqiSeQB/Du1u2P5SaW9hvZO5JhXz/penckSEiBXYfuxengXCqc2jtmWs75HIRg5fxzEvaQxuLQ0qbhv/DbUc4tpkZrZE61YtGsD8bwKGTDlPZBE5SewA26fu+TbatAuOnIgZ0eiTuebQ1wSeLelW1mHXfEtPiY6DUUwJHv15mzXXOMsg4uI1DMH86oTZiq73z/OU5CmkaZV8kxu8d6ajE/oGIAta6JBULeCD0Bw== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(5005006)(8121501046)(3002001);SRVR:BN1AFFO11HUB039;BCL:0;PCL:0;RULEID:;SRVR:BN1AFFO11HUB039; X-Forefront-PRVS: 069255B8B8 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;BN1AFFO11HUB039;23:dMcJ/VQcoPMZSxBhMCKdPMiykubhSo+3yB3UM9V?= =?us-ascii?Q?Vij6hX2d9ror0D46HkfYPWJwVLDYH0e20WJnid8CPfm71izRP6ZPWCR7hzAZ?= =?us-ascii?Q?gzohiWb7vMaxcm4C4cmjTPExgH2dG6TICTkVTCjeg5RE6kubr8DU6RwPobat?= =?us-ascii?Q?yUmdOcvMSjXcz5SqZLliFypaRH8PNG7UozPnEb0szaoJpkT4Vw5TO6i5uSCW?= =?us-ascii?Q?pk/w5NFDYoA+VeQJdogLL4ifhCPzL3TYYg58Ch9767Kpenixtv25HTVh6qf0?= =?us-ascii?Q?SaM41HLhxOA4tec6iHGkKtSjZ0IQq7sf9gBqgUui3ORN3ALcpkK56jYoZWqH?= =?us-ascii?Q?UHoLVWa7hZjj5AkXhPjOYjeVHGrXF8qKAay4RXZ9ZVdunjDgI1XQiEhFL5mS?= =?us-ascii?Q?En8VJvfiRuSgw4RHgUFaf9QNKCnpXfVll7ir1MSv00d5xyiXBNR022hokSZ+?= =?us-ascii?Q?YpgFqv3z8+yRGEyEoGA/YPt0FzCPo4hEdNSC4ON8x/GS334T0EioV4jMiygT?= =?us-ascii?Q?CuIqE2muY4pAgU6sRfGLY4koWsRV/cHB9/h9Tt0WZyrYH0YnjPoRLsmcB1vq?= =?us-ascii?Q?Wal1gZKZSZA0iAF6oWcu4x6Zf3dWqcF0oHwIWeq1eMsanCVuBfxp7/f5GueI?= =?us-ascii?Q?dUHsTa8+mTiYLVO46zbUY5STt8knQwZ0iA/wM87tbdWtgoThz5b9/kvDJZxj?= =?us-ascii?Q?OR7c8BiLLrSpCXopMpKH2GsNh1/0AtJztXTw69vVM1ZNgqB9vmmEPei356Lt?= =?us-ascii?Q?LMrneUxwJmaB9Zjph/LIewn4l/g1p0Cc0bpeHyx7hzsgh83BZtFoVH6ewx2Z?= =?us-ascii?Q?Kz+zUocbN9Ksv6L0PMlf0NR5PJFkotGxOGzPEWLNTerXoMZFXZ3OkDR99kSE?= =?us-ascii?Q?aHZDG4brX+FUcFFIzyn0LcaqpPE459A+13k6hXBwHSeZtkqj02PR37g21uJT?= =?us-ascii?Q?cERHanbtl3eyLS9cDJERU5qM5hUYOrzTeUFdwl9Zw/0BDi/FZvn07OiKMMWk?= =?us-ascii?Q?BwE29JD3AwpgGqME5ilIYkH8f5CxhGpypNgohC/1di7RTr9QSDtSOwGuNUhI?= =?us-ascii?Q?xrlQFv9/oEg/yORAi8tZGh4Mstc5Gm8Mu1Yf36thLprSU9knM4Ezr3M4TnJi?= =?us-ascii?Q?z7vldaX94Dv9QcKzCbeNr7goyklYl5Op4UQEdAah9yXYQL5/8xPBgVsRtLRH?= =?us-ascii?Q?61y40hSpK7wT6wAO2Nviw8CeNz9vFXVJ3KxiAAti/3OIotCIQvvk1qaGLPBS?= =?us-ascii?Q?ia+BJH8UFcIMvd/BKxjgnDJs2Tcq1HmtT5jbhrVoOmDKbyR9y1sQjPoC3wKU?= =?us-ascii?Q?2/D7YFNLWq5SOlnS8zKH0550=3D?= X-Microsoft-Exchange-Diagnostics: 1;BN1AFFO11HUB039;5:Gq5Vssl/GeZVIyZSjkAPYYQQ7C3+vi8fsdcSAVcXpzzlioCpjfnnXm+Vej4sID4+AEl44TxiKigWyf8Kbw+QHNPLHszqiEQ0EJvJZ7rwjA8qWRdpWowp8vT/On7PIszjCbzkLswjnD+qmaxPFi3gUw==;24:KvyFi7KBDkpptwmIcGvu30Stl389HpeRmsqrJc06yzr8jkxmvDgOTGE+HRX06DTnnqiAmkPW/7gaK7Dafw3mZluhTw3NoFdIclqHv+1s1Cw= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Sep 2015 12:33:33.7724 (UTC) X-MS-Exchange-CrossTenant-Id: 657af505-d5df-48d0-8300-c31994686c5c X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=657af505-d5df-48d0-8300-c31994686c5c;Ip=[149.199.60.83];Helo=[xsj-pvapsmtpgw01] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN1AFFO11HUB039 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is the driver for the AXI Central Direct Memory Access (AXI CDMA) core, which is a soft Xilinx IP core that provides high-bandwidth Direct Memory Access (DMA) between a memory-mapped source address and a memory-mapped destination address. This module works on Zynq (ARM Based SoC) and Microblaze platforms. Signed-off-by: Kedareswara rao Appana --- Changes in v6: - Used the readl_poll_timeout instead of do while loops in the driver - Improved the SG list handling by chaining the bds. - Removed the unnecessary xilinx_cdma_channel_set_config API the properties in this API is not being used by the driver. Changes in v5: - updated license in the driver as suggested by Paul. - Corrected return value in is_idle funtion. Changes in v4: - used GFP_NOWAIT instead of GFP_KERNEL during the desc allocation - Modified spin_lock handling moved the spin_lock to the appropriate functions - device_control api is removed and updated driver as per newer APi's. Changes in v3: - Check for CDMA idle condition before changing the configuration. - Modified the xilinx_dma.h header file location to the include/linux/dma/xilinx_dma.h Changes in v2: - Rebased on 3.16-rc7. drivers/dma/Kconfig | 12 + drivers/dma/xilinx/Makefile | 1 + drivers/dma/xilinx/xilinx_cdma.c | 991 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 1004 insertions(+) create mode 100644 drivers/dma/xilinx/xilinx_cdma.c diff --git a/drivers/dma/Kconfig b/drivers/dma/Kconfig index b458475..68684df 100644 --- a/drivers/dma/Kconfig +++ b/drivers/dma/Kconfig @@ -515,6 +515,18 @@ config XILINX_VDMA channels, Memory Mapped to Stream (MM2S) and Stream to Memory Mapped (S2MM) for the data transfers. +config XILINX_CDMA + tristate "Xilinx AXI CDMA Engine" + depends on (ARCH_ZYNQ || MICROBLAZE) + select DMA_ENGINE + help + Enable support for Xilinx AXI CDMA Soft IP. + + The AXI CDMA is a soft IP which provides high-bandwidth + Direct Memory Access (DMA) between a memory-mapped source + address and a memory-mapped destination address using the + AXI4 protocol. + config ZX_DMA tristate "ZTE ZX296702 DMA support" depends on ARCH_ZX diff --git a/drivers/dma/xilinx/Makefile b/drivers/dma/xilinx/Makefile index 3c4e9f2..e1dee77 100644 --- a/drivers/dma/xilinx/Makefile +++ b/drivers/dma/xilinx/Makefile @@ -1 +1,2 @@ obj-$(CONFIG_XILINX_VDMA) += xilinx_vdma.o +obj-$(CONFIG_XILINX_CDMA) += xilinx_cdma.o diff --git a/drivers/dma/xilinx/xilinx_cdma.c b/drivers/dma/xilinx/xilinx_cdma.c new file mode 100644 index 0000000..4089f3a --- /dev/null +++ b/drivers/dma/xilinx/xilinx_cdma.c @@ -0,0 +1,991 @@ +/* + * DMA driver for Xilinx Central DMA Engine + * + * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved. + * + * Based on the Freescale DMA driver. + * + * Description: + * The AXI CDMA, is a soft IP, which provides high-bandwidth Direct Memory + * Access (DMA) between a memory-mapped source address and a memory-mapped + * destination address. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../dmaengine.h" + +/* Register Offsets */ +#define XILINX_CDMA_CONTROL_OFFSET 0x00 +#define XILINX_CDMA_STATUS_OFFSET 0x04 +#define XILINX_CDMA_CDESC_OFFSET 0x08 +#define XILINX_CDMA_TDESC_OFFSET 0x10 +#define XILINX_CDMA_SRCADDR_OFFSET 0x18 +#define XILINX_CDMA_DSTADDR_OFFSET 0x20 +#define XILINX_CDMA_BTT_OFFSET 0x28 + +/* General register bits definitions */ +#define XILINX_CDMA_CR_RESET BIT(2) +#define XILINX_CDMA_CR_SGMODE BIT(3) + +#define XILINX_CDMA_SR_IDLE BIT(1) + +#define XILINX_CDMA_XR_IRQ_IOC_MASK BIT(12) +#define XILINX_CDMA_XR_IRQ_DELAY_MASK BIT(13) +#define XILINX_CDMA_XR_IRQ_ERROR_MASK BIT(14) +#define XILINX_CDMA_XR_IRQ_ALL_MASK GENMASK(14, 12) + +#define XILINX_CDMA_XR_DELAY_MASK GENMASK(31, 24) +#define XILINX_CDMA_XR_COALESCE_MASK GENMASK(23, 16) + +#define XILINX_CDMA_DELAY_MAX GENMASK(7, 0) +#define XILINX_CDMA_DELAY_SHIFT 24 + +#define XILINX_CDMA_COALESCE_MAX GENMASK(7, 0) +#define XILINX_CDMA_COALESCE_SHIFT 16 + +/* Delay loop counter to prevent hardware failure */ +#define XILINX_CDMA_RESET_LOOP 1000000 + +/* Maximum transfer length */ +#define XILINX_CDMA_MAX_TRANS_LEN GENMASK(22, 0) + +/** + * struct xilinx_cdma_desc_hw - Hardware Descriptor + * @next_desc: Next Descriptor Pointer @0x00 + * @pad1: Reserved @0x04 + * @src_addr: Source address @0x08 + * @pad2: Reserved @0x0C + * @dest_addr: Destination address @0x10 + * @pad3: Reserved @0x14 + * @control: Control field @0x18 + * @status: Status field @0x1C + */ +struct xilinx_cdma_desc_hw { + u32 next_desc; + u32 pad1; + u32 src_addr; + u32 pad2; + u32 dest_addr; + u32 pad3; + u32 control; + u32 status; +} __aligned(64); + +/** + * struct xilinx_cdma_tx_segment - Descriptor segment + * @hw: Hardware descriptor + * @node: Node in the descriptor segments list + * @phys: Physical address of segment + */ +struct xilinx_cdma_tx_segment { + struct xilinx_cdma_desc_hw hw; + struct list_head node; + dma_addr_t phys; +} __aligned(64); + +/** + * struct xilinx_cdma_tx_descriptor - Per Transaction structure + * @async_tx: Async transaction descriptor + * @segments: TX segments list + * @node: Node in the channel descriptors list + */ +struct xilinx_cdma_tx_descriptor { + struct dma_async_tx_descriptor async_tx; + struct list_head segments; + struct list_head node; +}; + +/** + * struct xilinx_cdma_chan - Driver specific cdma channel structure + * @xdev: Driver specific device structure + * @lock: Descriptor operation lock + * @done_list: Complete descriptors + * @pending_list: Descriptors waiting + * @active_list: Descriptors ready to submit + * @common: DMA common channel + * @desc_pool: Descriptors pool + * @dev: The dma device + * @irq: Channel IRQ + * @has_sg: Support scatter transfers + * @err: Channel has errors + * @idle: Channel status + * @tasklet: Cleanup work after irq + * @desc_pendingcount: Descriptor pending count + */ +struct xilinx_cdma_chan { + struct xilinx_cdma_device *xdev; + spinlock_t lock; + struct list_head done_list; + struct list_head pending_list; + struct list_head active_list; + struct dma_chan common; + struct dma_pool *desc_pool; + struct device *dev; + int irq; + bool has_sg; + int err; + bool idle; + struct tasklet_struct tasklet; + u32 desc_pendingcount; +}; + +/** + * struct xilinx_cdma_device - CDMA device structure + * @regs: I/O mapped base address + * @dev: Device Structure + * @common: DMA device structure + * @chan: Driver specific cdma channel + * @has_sg: Specifies whether Scatter-Gather is present or not + */ +struct xilinx_cdma_device { + void __iomem *regs; + struct device *dev; + struct dma_device common; + struct xilinx_cdma_chan *chan; + bool has_sg; +}; + +/* Macros */ +#define to_xilinx_chan(chan) \ + container_of(chan, struct xilinx_cdma_chan, common) +#define to_cdma_tx_descriptor(tx) \ + container_of(tx, struct xilinx_cdma_tx_descriptor, async_tx) + +#define xilinx_cdma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \ + readl_poll_timeout(chan->xdev->regs + reg, val, cond, delay_us, \ + timeout_us) + +/* IO accessors */ +static inline void cdma_write(struct xilinx_cdma_chan *chan, u32 reg, u32 val) +{ + writel(val, chan->xdev->regs + reg); +} + +static inline u32 cdma_read(struct xilinx_cdma_chan *chan, u32 reg) +{ + return readl(chan->xdev->regs + reg); +} + +static inline void cdma_ctrl_clr(struct xilinx_cdma_chan *chan, u32 reg, + u32 clr) +{ + cdma_write(chan, reg, cdma_read(chan, reg) & ~clr); +} + +static inline void cdma_ctrl_set(struct xilinx_cdma_chan *chan, u32 reg, + u32 set) +{ + cdma_write(chan, reg, cdma_read(chan, reg) | set); +} + +/* ----------------------------------------------------------------------------- + * Descriptors and segments alloc and free + */ + +/** + * xilinx_cdma_alloc_tx_segment - Allocate transaction segment + * @chan: Driver specific cdma channel + * + * Return: The allocated segment on success and NULL on failure. + */ +static struct xilinx_cdma_tx_segment * +xilinx_cdma_alloc_tx_segment(struct xilinx_cdma_chan *chan) +{ + struct xilinx_cdma_tx_segment *segment; + dma_addr_t phys; + + segment = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &phys); + if (!segment) + return NULL; + + memset(segment, 0, sizeof(*segment)); + segment->phys = phys; + + return segment; +} + +/** + * xilinx_cdma_free_tx_segment - Free transaction segment + * @chan: Driver specific cdma channel + * @segment: cdma transaction segment + */ +static void xilinx_cdma_free_tx_segment(struct xilinx_cdma_chan *chan, + struct xilinx_cdma_tx_segment *segment) +{ + dma_pool_free(chan->desc_pool, segment, segment->phys); +} + +/** + * xilinx_cdma_tx_descriptor - Allocate transaction descriptor + * @chan: Driver specific cdma channel + * + * Return: The allocated descriptor on success and NULL on failure. + */ +static struct xilinx_cdma_tx_descriptor * +xilinx_cdma_alloc_tx_descriptor(struct xilinx_cdma_chan *chan) +{ + struct xilinx_cdma_tx_descriptor *desc; + + desc = kzalloc(sizeof(*desc), GFP_KERNEL); + if (!desc) + return NULL; + + INIT_LIST_HEAD(&desc->segments); + + return desc; +} + +/** + * xilinx_cdma_free_tx_descriptor - Free transaction descriptor + * @chan: Driver specific cdma channel + * @desc: cdma transaction descriptor + */ +static void +xilinx_cdma_free_tx_descriptor(struct xilinx_cdma_chan *chan, + struct xilinx_cdma_tx_descriptor *desc) +{ + struct xilinx_cdma_tx_segment *segment, *next; + + if (!desc) + return; + + list_for_each_entry_safe(segment, next, &desc->segments, node) { + list_del(&segment->node); + xilinx_cdma_free_tx_segment(chan, segment); + } + + kfree(desc); +} + +/** + * xilinx_cdma_alloc_chan_resources - Allocate channel resources + * @dchan: DMA channel + * + * Return: '0' on success and failure value on error + */ +static int xilinx_cdma_alloc_chan_resources(struct dma_chan *dchan) +{ + struct xilinx_cdma_chan *chan = to_xilinx_chan(dchan); + + /* Has this channel already been allocated? */ + if (chan->desc_pool) + return 0; + + /* + * We need the descriptor to be aligned to 64bytes + * for meeting Xilinx DMA specification requirement. + */ + chan->desc_pool = dma_pool_create("xilinx_cdma_desc_pool", + chan->dev, + sizeof(struct xilinx_cdma_tx_segment), + __alignof__(struct xilinx_cdma_tx_segment), 0); + if (!chan->desc_pool) { + dev_err(chan->dev, + "unable to allocate channel descriptor pool\n"); + return -ENOMEM; + } + + dma_cookie_init(dchan); + return 0; +} + +/** + * xilinx_cdma_free_desc_list - Free descriptors list + * @chan: Driver specific cdma channel + * @list: List to parse and delete the descriptor + */ +static void xilinx_cdma_free_desc_list(struct xilinx_cdma_chan *chan, + struct list_head *list) +{ + struct xilinx_cdma_tx_descriptor *desc, *next; + + list_for_each_entry_safe(desc, next, list, node) { + list_del(&desc->node); + xilinx_cdma_free_tx_descriptor(chan, desc); + } +} + +/** + * xilinx_cdma_free_descriptors - Free channel descriptors + * @chan: Driver specific dma channel + */ +static void xilinx_cdma_free_descriptors(struct xilinx_cdma_chan *chan) +{ + unsigned long flags; + + spin_lock_irqsave(&chan->lock, flags); + + xilinx_cdma_free_desc_list(chan, &chan->done_list); + xilinx_cdma_free_desc_list(chan, &chan->pending_list); + xilinx_cdma_free_desc_list(chan, &chan->active_list); + + spin_unlock_irqrestore(&chan->lock, flags); +} + +/** + * xilinx_cdma_free_chan_resources - Free channel resources + * @dchan: DMA channel + */ +static void xilinx_cdma_free_chan_resources(struct dma_chan *dchan) +{ + struct xilinx_cdma_chan *chan = to_xilinx_chan(dchan); + + xilinx_cdma_free_descriptors(chan); + + dma_pool_destroy(chan->desc_pool); + chan->desc_pool = NULL; +} + +/** + * xilinx_cdma_chan_desc_cleanup - Clean channel descriptors + * @chan: Driver specific cdma channel + */ +static void xilinx_cdma_chan_desc_cleanup(struct xilinx_cdma_chan *chan) +{ + struct xilinx_cdma_tx_descriptor *desc, *next; + unsigned long flags; + + spin_lock_irqsave(&chan->lock, flags); + + list_for_each_entry_safe(desc, next, &chan->done_list, node) { + dma_async_tx_callback callback; + void *callback_param; + + /* Remove from the list of running transactions */ + list_del(&desc->node); + + /* Run the link descriptor callback function */ + callback = desc->async_tx.callback; + callback_param = desc->async_tx.callback_param; + if (callback) { + spin_unlock_irqrestore(&chan->lock, flags); + callback(callback_param); + spin_lock_irqsave(&chan->lock, flags); + } + + /* Run any dependencies, then free the descriptor */ + dma_run_dependencies(&desc->async_tx); + xilinx_cdma_free_tx_descriptor(chan, desc); + } + + spin_unlock_irqrestore(&chan->lock, flags); +} + +/** + * xilinx_tx_status - Get CDMA transaction status + * @dchan: DMA channel + * @cookie: Transaction identifier + * @txstate: Transaction state + * + * Return: DMA transaction status + */ +static enum dma_status xilinx_tx_status(struct dma_chan *dchan, + dma_cookie_t cookie, + struct dma_tx_state *txstate) +{ + return dma_cookie_status(dchan, cookie, txstate); +} + +/** + * xilinx_cdma_start_transfer - Starts cdma transfer + * @chan: Driver specific channel struct pointer + */ +static void xilinx_cdma_start_transfer(struct xilinx_cdma_chan *chan) +{ + struct xilinx_cdma_tx_descriptor *head_desc, *tail_desc; + struct xilinx_cdma_tx_segment *tail_segment; + u32 ctrl_reg = cdma_read(chan, XILINX_CDMA_CONTROL_OFFSET); + + if (chan->err) + return; + + if (list_empty(&chan->pending_list)) + return; + + if (!chan->idle) + return; + + head_desc = list_first_entry(&chan->pending_list, + struct xilinx_cdma_tx_descriptor, node); + tail_desc = list_last_entry(&chan->pending_list, + struct xilinx_cdma_tx_descriptor, node); + tail_segment = list_last_entry(&tail_desc->segments, + struct xilinx_cdma_tx_segment, node); + + if (chan->desc_pendingcount <= XILINX_CDMA_COALESCE_MAX) { + ctrl_reg &= ~XILINX_CDMA_XR_COALESCE_MASK; + ctrl_reg |= chan->desc_pendingcount << + XILINX_CDMA_COALESCE_SHIFT; + cdma_write(chan, XILINX_CDMA_CONTROL_OFFSET, ctrl_reg); + } + + if (chan->has_sg) { + cdma_write(chan, XILINX_CDMA_CDESC_OFFSET, + head_desc->async_tx.phys); + + /* Update tail ptr register which will start the transfer */ + cdma_write(chan, XILINX_CDMA_TDESC_OFFSET, tail_segment->phys); + } else { + /* In simple mode */ + struct xilinx_cdma_tx_segment *segment; + struct xilinx_cdma_desc_hw *hw; + + segment = list_first_entry(&head_desc->segments, + struct xilinx_cdma_tx_segment, + node); + + hw = &segment->hw; + + cdma_write(chan, XILINX_CDMA_SRCADDR_OFFSET, hw->src_addr); + cdma_write(chan, XILINX_CDMA_DSTADDR_OFFSET, hw->dest_addr); + + /* Start the transfer */ + cdma_write(chan, XILINX_CDMA_BTT_OFFSET, + hw->control & XILINX_CDMA_MAX_TRANS_LEN); + } + + list_splice_tail_init(&chan->pending_list, &chan->active_list); + chan->desc_pendingcount = 0; + chan->idle = false; +} + +/** + * xilinx_cdma_issue_pending - Issue pending transactions + * @dchan: DMA channel + */ +static void xilinx_cdma_issue_pending(struct dma_chan *dchan) +{ + struct xilinx_cdma_chan *chan = to_xilinx_chan(dchan); + unsigned long flags; + + spin_lock_irqsave(&chan->lock, flags); + xilinx_cdma_start_transfer(chan); + spin_unlock_irqrestore(&chan->lock, flags); +} + +/** + * xilinx_cdma_complete_descriptor - Mark the active descriptor as complete + * @chan : xilinx DMA channel + */ +static void xilinx_cdma_complete_descriptor(struct xilinx_cdma_chan *chan) +{ + struct xilinx_cdma_tx_descriptor *desc, *next; + + if (list_empty(&chan->active_list)) + return; + + list_for_each_entry_safe(desc, next, &chan->active_list, node) { + list_del(&desc->node); + dma_cookie_complete(&desc->async_tx); + list_add_tail(&desc->node, &chan->done_list); + } +} + +/** + * xilinx_cdma_chan_reset - Reset CDMA channel + * @chan: Driver specific CDMA channel + * + * Return: '0' on success and failure value on error + */ +static int xilinx_cdma_chan_reset(struct xilinx_cdma_chan *chan) +{ + int err = 0; + u32 val; + + cdma_ctrl_set(chan, XILINX_CDMA_CONTROL_OFFSET, + XILINX_CDMA_CR_RESET); + + + /* Wait for the hardware to finish reset */ + err = xilinx_cdma_poll_timeout(chan, XILINX_CDMA_CONTROL_OFFSET, val, + !(val & XILINX_CDMA_CR_RESET), 10, + XILINX_CDMA_RESET_LOOP); + if (err) { + dev_err(chan->dev, "reset timeout, cr %x, sr %x\n", + cdma_read(chan, XILINX_CDMA_CONTROL_OFFSET), + cdma_read(chan, XILINX_CDMA_STATUS_OFFSET)); + return -EBUSY; + } + + /* Enable interrupts */ + cdma_ctrl_set(chan, XILINX_CDMA_CONTROL_OFFSET, + XILINX_CDMA_XR_IRQ_ALL_MASK); + + /* Enable SG Mode */ + if (chan->has_sg) + cdma_ctrl_set(chan, XILINX_CDMA_CONTROL_OFFSET, + XILINX_CDMA_CR_SGMODE); + + return 0; +} + +/** + * xilinx_cdma_irq_handler - CDMA Interrupt handler + * @irq: IRQ number + * @data: Pointer to the Xilinx CDMA channel structure + * + * Return: IRQ_HANDLED/IRQ_NONE + */ +static irqreturn_t xilinx_cdma_irq_handler(int irq, void *data) +{ + struct xilinx_cdma_chan *chan = data; + u32 stat; + + stat = cdma_read(chan, XILINX_CDMA_STATUS_OFFSET); + if (!(stat & XILINX_CDMA_XR_IRQ_ALL_MASK)) + return IRQ_NONE; + + /* Ack the interrupts */ + cdma_write(chan, XILINX_CDMA_STATUS_OFFSET, + XILINX_CDMA_XR_IRQ_ALL_MASK); + + if (stat & XILINX_CDMA_XR_IRQ_ERROR_MASK) { + dev_err(chan->dev, + "Channel %x has errors %x, cdr %x tdr %x\n", + (u32)chan, + (u32)cdma_read(chan, XILINX_CDMA_STATUS_OFFSET), + (u32)cdma_read(chan, XILINX_CDMA_CDESC_OFFSET), + (u32)cdma_read(chan, XILINX_CDMA_TDESC_OFFSET)); + chan->err = true; + } + + /* + * Device takes too long to do the transfer when user requires + * responsiveness + */ + if (stat & XILINX_CDMA_XR_IRQ_DELAY_MASK) + dev_dbg(chan->dev, "Inter-packet latency too long\n"); + + if (stat & XILINX_CDMA_XR_IRQ_IOC_MASK) { + spin_lock(&chan->lock); + xilinx_cdma_complete_descriptor(chan); + chan->idle = true; + xilinx_cdma_start_transfer(chan); + spin_unlock(&chan->lock); + } + + tasklet_schedule(&chan->tasklet); + return IRQ_HANDLED; +} + +/** + * xilinx_cdma_do_tasklet - Schedule completion tasklet + * @data: Pointer to the Xilinx cdma channel structure + */ +static void xilinx_cdma_do_tasklet(unsigned long data) +{ + struct xilinx_cdma_chan *chan = (struct xilinx_cdma_chan *)data; + + xilinx_cdma_chan_desc_cleanup(chan); +} + +/** + * append_desc_queue - Queuing descriptor + * @chan: Driver specific dma channel + * @desc: dma transaction descriptor + */ +static void append_desc_queue(struct xilinx_cdma_chan *chan, + struct xilinx_cdma_tx_descriptor *desc) +{ + struct xilinx_cdma_tx_segment *tail_segment; + struct xilinx_cdma_tx_descriptor *tail_desc; + + if (list_empty(&chan->pending_list)) + goto append; + + /* + * Add the hardware descriptor to the chain of hardware descriptors + * that already exists in memory. + */ + tail_desc = list_last_entry(&chan->pending_list, + struct xilinx_cdma_tx_descriptor, node); + tail_segment = list_last_entry(&tail_desc->segments, + struct xilinx_cdma_tx_segment, node); + tail_segment->hw.next_desc = (u32)desc->async_tx.phys; + + /* + * Add the software descriptor and all children to the list + * of pending transactions + */ +append: + list_add_tail(&desc->node, &chan->pending_list); + chan->desc_pendingcount++; + + if (unlikely(chan->desc_pendingcount > XILINX_CDMA_COALESCE_MAX)) { + dev_dbg(chan->dev, "desc pendingcount is too high\n"); + chan->desc_pendingcount = XILINX_CDMA_COALESCE_MAX; + BUG(); + } +} + +/** + * xilinx_cdma_tx_submit - Submit DMA transaction + * @tx: Async transaction descriptor + * + * Return: cookie value on success and failure value on error + */ +static dma_cookie_t xilinx_cdma_tx_submit(struct dma_async_tx_descriptor *tx) +{ + struct xilinx_cdma_chan *chan = to_xilinx_chan(tx->chan); + struct xilinx_cdma_tx_descriptor *desc = to_cdma_tx_descriptor(tx); + dma_cookie_t cookie; + unsigned long flags; + int err; + + if (chan->err) { + /* + * If reset fails, need to hard reset the system. + * Channel is no longer functional + */ + err = xilinx_cdma_chan_reset(chan); + if (err < 0) + return err; + } + + spin_lock_irqsave(&chan->lock, flags); + + cookie = dma_cookie_assign(tx); + + /* Put this transaction onto the tail of the pending queue */ + append_desc_queue(chan, desc); + + spin_unlock_irqrestore(&chan->lock, flags); + + return cookie; +} + +/** + * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction + * @dchan: DMA channel + * @dma_dst: destination address + * @dma_src: source address + * @len: transfer length + * @flags: transfer ack flags + * + * Return: Async transaction descriptor on success and NULL on failure + */ +static struct dma_async_tx_descriptor * +xilinx_cdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst, + dma_addr_t dma_src, size_t len, unsigned long flags) +{ + struct xilinx_cdma_chan *chan = to_xilinx_chan(dchan); + struct xilinx_cdma_desc_hw *hw; + struct xilinx_cdma_tx_descriptor *desc; + struct xilinx_cdma_tx_segment *segment, *prev; + + if (!len || len > XILINX_CDMA_MAX_TRANS_LEN) + return NULL; + + desc = xilinx_cdma_alloc_tx_descriptor(chan); + if (!desc) + return NULL; + + dma_async_tx_descriptor_init(&desc->async_tx, &chan->common); + desc->async_tx.tx_submit = xilinx_cdma_tx_submit; + async_tx_ack(&desc->async_tx); + + /* Allocate the link descriptor from DMA pool */ + segment = xilinx_cdma_alloc_tx_segment(chan); + if (!segment) + goto error; + + hw = &segment->hw; + hw->control = len; + hw->src_addr = lower_32_bits(dma_src); + hw->dest_addr = lower_32_bits(dma_dst); + + /* Fill the previous next descriptor with current */ + prev = list_last_entry(&desc->segments, + struct xilinx_cdma_tx_segment, node); + prev->hw.next_desc = segment->phys; + + /* Insert the segment into the descriptor segments list. */ + list_add_tail(&segment->node, &desc->segments); + + prev = segment; + + /* Link the last hardware descriptor with the first. */ + segment = list_first_entry(&desc->segments, + struct xilinx_cdma_tx_segment, node); + desc->async_tx.phys = segment->phys; + prev->hw.next_desc = segment->phys; + + return &desc->async_tx; + +error: + xilinx_cdma_free_tx_descriptor(chan, desc); + return NULL; +} + +/** + * xilinx_cdma_terminate_all - Free the descriptors + * @dchan: DMA Channel pointer + * + * Return: '0' always + */ +static int xilinx_cdma_terminate_all(struct dma_chan *dchan) +{ + struct xilinx_cdma_chan *chan = to_xilinx_chan(dchan); + + /* Reset the channel */ + xilinx_cdma_chan_reset(chan); + + /* Remove and free all of the descriptors in the lists */ + xilinx_cdma_free_descriptors(chan); + + return 0; +} + +/* ----------------------------------------------------------------------------- + * Probe and remove + */ + +/** + * xilinx_cdma_free_channel - Channel remove function + * @chan: Driver specific cdma channel + */ +static void xilinx_cdma_free_channel(struct xilinx_cdma_chan *chan) +{ + /* Disable Interrupts */ + cdma_ctrl_clr(chan, XILINX_CDMA_CONTROL_OFFSET, + XILINX_CDMA_XR_IRQ_ALL_MASK); + + if (chan->irq > 0) + free_irq(chan->irq, chan); + + tasklet_kill(&chan->tasklet); + + list_del(&chan->common.device_node); +} + +/** + * xilinx_cdma_chan_probe - Per Channel Probing + * It get channel features from the device tree entry and + * initialize special channel handling routines + * + * @xdev: Driver specific device structure + * @node: Device node + * + * Return: '0' on success and failure value on error + */ +static int xilinx_cdma_chan_probe(struct xilinx_cdma_device *xdev, + struct device_node *node) +{ + struct xilinx_cdma_chan *chan; + bool has_dre; + u32 value, width; + int err; + + /* Alloc channel */ + chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_NOWAIT); + if (!chan) + return -ENOMEM; + + chan->dev = xdev->dev; + chan->has_sg = xdev->has_sg; + chan->xdev = xdev; + chan->desc_pendingcount = 0x0; + + spin_lock_init(&chan->lock); + INIT_LIST_HEAD(&chan->pending_list); + INIT_LIST_HEAD(&chan->done_list); + INIT_LIST_HEAD(&chan->active_list); + + /* Retrieve the channel properties from the device tree */ + has_dre = of_property_read_bool(node, "xlnx,include-dre"); + + err = of_property_read_u32(node, "xlnx,datawidth", &value); + if (err) { + dev_err(xdev->dev, "unable to read datawidth property"); + return err; + } + width = value >> 3; /* convert bits to bytes */ + + /* If data width is greater than 8 bytes, DRE is not in hw */ + if (width > 8) + has_dre = false; + + if (!has_dre) + xdev->common.copy_align = fls(width - 1); + + /* Request the interrupt */ + chan->irq = irq_of_parse_and_map(node, 0); + err = request_irq(chan->irq, xilinx_cdma_irq_handler, IRQF_SHARED, + "xilinx-cdma-controller", chan); + if (err) { + dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq); + return err; + } + + /* Initialize the tasklet */ + tasklet_init(&chan->tasklet, xilinx_cdma_do_tasklet, + (unsigned long)chan); + + /* + * Initialize the DMA channel and add it to the DMA engine channels + * list. + */ + chan->common.device = &xdev->common; + + list_add_tail(&chan->common.device_node, &xdev->common.channels); + xdev->chan = chan; + + /* Initialize the channel */ + err = xilinx_cdma_chan_reset(chan); + if (err) { + dev_err(xdev->dev, "Reset channel failed\n"); + return err; + } + + chan->idle = true; + + return 0; +} + +/** + * of_dma_xilinx_xlate - Translation function + * @dma_spec: Pointer to DMA specifier as found in the device tree + * @ofdma: Pointer to DMA controller data + * + * Return: DMA channel pointer on success and NULL on error + */ +static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec, + struct of_dma *ofdma) +{ + struct xilinx_cdma_device *xdev = ofdma->of_dma_data; + + return dma_get_slave_channel(&xdev->chan->common); +} + +/** + * xilinx_cdma_probe - Driver probe function + * @pdev: Pointer to the platform_device structure + * + * Return: '0' on success and failure value on error + */ +static int xilinx_cdma_probe(struct platform_device *pdev) +{ + struct xilinx_cdma_device *xdev; + struct device_node *child, *node; + struct resource *res; + int ret; + + xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL); + if (!xdev) + return -ENOMEM; + + xdev->dev = &(pdev->dev); + INIT_LIST_HEAD(&xdev->common.channels); + + node = pdev->dev.of_node; + + /* Map the registers */ + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + xdev->regs = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(xdev->regs)) + return PTR_ERR(xdev->regs); + + /* Check if SG is enabled */ + xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg"); + + dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask); + xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy; + xdev->common.device_terminate_all = xilinx_cdma_terminate_all; + xdev->common.device_issue_pending = xilinx_cdma_issue_pending; + xdev->common.device_alloc_chan_resources = + xilinx_cdma_alloc_chan_resources; + xdev->common.device_free_chan_resources = + xilinx_cdma_free_chan_resources; + xdev->common.device_tx_status = xilinx_tx_status; + xdev->common.dev = &pdev->dev; + + platform_set_drvdata(pdev, xdev); + + child = of_get_next_child(node, NULL); + if (!child) { + dev_err(&pdev->dev, "No channel found\n"); + return PTR_ERR(child); + } + + ret = xilinx_cdma_chan_probe(xdev, child); + if (ret) { + dev_err(&pdev->dev, "Probing channel failed\n"); + goto free_chan_resources; + } + + dma_async_device_register(&xdev->common); + + ret = of_dma_controller_register(node, of_dma_xilinx_xlate, xdev); + if (ret) { + dev_err(&pdev->dev, "Unable to register DMA to DT\n"); + dma_async_device_unregister(&xdev->common); + goto free_chan_resources; + } + + dev_info(&pdev->dev, "Xilinx AXI CDMA Engine driver Probed!!\n"); + + return 0; + +free_chan_resources: + xilinx_cdma_free_channel(xdev->chan); + + return ret; +} + +/** + * xilinx_cdma_remove - Driver remove function + * @pdev: Pointer to the platform_device structure + * + * Return: Always '0' + */ +static int xilinx_cdma_remove(struct platform_device *pdev) +{ + struct xilinx_cdma_device *xdev = platform_get_drvdata(pdev); + + of_dma_controller_free(pdev->dev.of_node); + dma_async_device_unregister(&xdev->common); + + xilinx_cdma_free_channel(xdev->chan); + + return 0; +} + +static const struct of_device_id xilinx_cdma_of_match[] = { + { .compatible = "xlnx,axi-cdma-1.00.a", }, + {} +}; +MODULE_DEVICE_TABLE(of, xilinx_cdma_of_match); + +static struct platform_driver xilinx_cdma_driver = { + .driver = { + .name = "xilinx-cdma", + .of_match_table = xilinx_cdma_of_match, + }, + .probe = xilinx_cdma_probe, + .remove = xilinx_cdma_remove, +}; + +module_platform_driver(xilinx_cdma_driver); + +MODULE_AUTHOR("Xilinx, Inc."); +MODULE_DESCRIPTION("Xilinx CDMA driver"); +MODULE_LICENSE("GPL"); -- 2.1.2