xref: /openbmc/linux/drivers/dma/xgene-dma.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29f2fd0dfSRameshwar Prasad Sahu /*
39f2fd0dfSRameshwar Prasad Sahu  * Applied Micro X-Gene SoC DMA engine Driver
49f2fd0dfSRameshwar Prasad Sahu  *
59f2fd0dfSRameshwar Prasad Sahu  * Copyright (c) 2015, Applied Micro Circuits Corporation
69f2fd0dfSRameshwar Prasad Sahu  * Authors: Rameshwar Prasad Sahu <rsahu@apm.com>
79f2fd0dfSRameshwar Prasad Sahu  *	    Loc Ho <lho@apm.com>
89f2fd0dfSRameshwar Prasad Sahu  *
99f2fd0dfSRameshwar Prasad Sahu  * NOTE: PM support is currently not available.
109f2fd0dfSRameshwar Prasad Sahu  */
119f2fd0dfSRameshwar Prasad Sahu 
1289079493SRameshwar Prasad Sahu #include <linux/acpi.h>
139f2fd0dfSRameshwar Prasad Sahu #include <linux/clk.h>
149f2fd0dfSRameshwar Prasad Sahu #include <linux/delay.h>
159f2fd0dfSRameshwar Prasad Sahu #include <linux/dma-mapping.h>
169f2fd0dfSRameshwar Prasad Sahu #include <linux/dmaengine.h>
179f2fd0dfSRameshwar Prasad Sahu #include <linux/dmapool.h>
189f2fd0dfSRameshwar Prasad Sahu #include <linux/interrupt.h>
199f2fd0dfSRameshwar Prasad Sahu #include <linux/io.h>
20b0b79024SRameshwar Prasad Sahu #include <linux/irq.h>
21*897500c7SRob Herring #include <linux/mod_devicetable.h>
229f2fd0dfSRameshwar Prasad Sahu #include <linux/module.h>
23*897500c7SRob Herring #include <linux/platform_device.h>
249f2fd0dfSRameshwar Prasad Sahu 
259f2fd0dfSRameshwar Prasad Sahu #include "dmaengine.h"
269f2fd0dfSRameshwar Prasad Sahu 
279f2fd0dfSRameshwar Prasad Sahu /* X-Gene DMA ring csr registers and bit definations */
289f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_CONFIG			0x04
299f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_ENABLE			BIT(31)
309f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_ID			0x08
319f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_ID_SETUP(v)		((v) | BIT(31))
329f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_ID_BUF			0x0C
339f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_ID_BUF_SETUP(v)		(((v) << 9) | BIT(21))
349f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_THRESLD0_SET1		0x30
359f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_THRESLD0_SET1_VAL	0X64
369f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_THRESLD1_SET1		0x34
379f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_THRESLD1_SET1_VAL	0xC8
389f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_HYSTERESIS		0x68
399f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_HYSTERESIS_VAL		0xFFFFFFFF
409f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_STATE			0x6C
419f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_STATE_WR_BASE		0x70
429f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_NE_INT_MODE		0x017C
439f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_NE_INT_MODE_SET(m, v)	\
449f2fd0dfSRameshwar Prasad Sahu 	((m) = ((m) & ~BIT(31 - (v))) | BIT(31 - (v)))
459f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_NE_INT_MODE_RESET(m, v)	\
469f2fd0dfSRameshwar Prasad Sahu 	((m) &= (~BIT(31 - (v))))
479f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_CLKEN			0xC208
489f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_SRST			0xC200
499f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_MEM_RAM_SHUTDOWN		0xD070
509f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_BLK_MEM_RDY		0xD074
519f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_BLK_MEM_RDY_VAL		0xFFFFFFFF
529f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_ID_GET(owner, num)	(((owner) << 6) | (num))
539f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_DST_ID(v)		((1 << 10) | (v))
549f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_CMD_OFFSET		0x2C
559f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_CMD_BASE_OFFSET(v)	((v) << 6)
569f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_COHERENT_SET(m)		\
579f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[2] |= BIT(4))
589f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_ADDRL_SET(m, v)		\
599f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[2] |= (((v) >> 8) << 5))
609f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_ADDRH_SET(m, v)		\
619f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[3] |= ((v) >> 35))
629f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_ACCEPTLERR_SET(m)	\
639f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[3] |= BIT(19))
649f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_SIZE_SET(m, v)		\
659f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[3] |= ((v) << 23))
669f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_RECOMBBUF_SET(m)		\
679f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[3] |= BIT(27))
689f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_RECOMTIMEOUTL_SET(m)	\
699f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[3] |= (0x7 << 28))
709f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_RECOMTIMEOUTH_SET(m)	\
719f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[4] |= 0x3)
729f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_SELTHRSH_SET(m)		\
739f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[4] |= BIT(3))
749f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_TYPE_SET(m, v)		\
759f2fd0dfSRameshwar Prasad Sahu 	(((u32 *)(m))[4] |= ((v) << 19))
769f2fd0dfSRameshwar Prasad Sahu 
779f2fd0dfSRameshwar Prasad Sahu /* X-Gene DMA device csr registers and bit definitions */
789f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_IPBRR				0x0
799f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DEV_ID_RD(v)			((v) & 0x00000FFF)
809f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_BUS_ID_RD(v)			(((v) >> 12) & 3)
819f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_REV_NO_RD(v)			(((v) >> 14) & 3)
829f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_GCR				0x10
839f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_CH_SETUP(v)			\
849f2fd0dfSRameshwar Prasad Sahu 	((v) = ((v) & ~0x000FFFFF) | 0x000AAFFF)
859f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_ENABLE(v)			((v) |= BIT(31))
869f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DISABLE(v)			((v) &= ~BIT(31))
879f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RAID6_CONT			0x14
889f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RAID6_MULTI_CTRL(v)		((v) << 24)
899f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_INT				0x70
909f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_INT_MASK			0x74
919f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_INT_ALL_MASK			0xFFFFFFFF
929f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_INT_ALL_UNMASK		0x0
939f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_INT_MASK_SHIFT		0x14
949f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_INT0_MASK		0x90A0
959f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_INT1_MASK		0x90A8
969f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_INT2_MASK		0x90B0
979f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_INT3_MASK		0x90B8
989f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_INT4_MASK		0x90C0
999f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_CFG_RING_WQ_ASSOC		0x90E0
1009f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_ASSOC_RING_MNGR1		0xFFFFFFFF
1019f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_MEM_RAM_SHUTDOWN		0xD070
1029f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_BLK_MEM_RDY			0xD074
1039f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_BLK_MEM_RDY_VAL		0xFFFFFFFF
104cda8e937SRameshwar Prasad Sahu #define XGENE_DMA_RING_CMD_SM_OFFSET		0x8000
1059f2fd0dfSRameshwar Prasad Sahu 
1069f2fd0dfSRameshwar Prasad Sahu /* X-Gene SoC EFUSE csr register and bit defination */
1079f2fd0dfSRameshwar Prasad Sahu #define XGENE_SOC_JTAG1_SHADOW			0x18
1089f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_PQ_DISABLE_MASK		BIT(13)
1099f2fd0dfSRameshwar Prasad Sahu 
1109f2fd0dfSRameshwar Prasad Sahu /* X-Gene DMA Descriptor format */
1119f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_NV_BIT			BIT_ULL(50)
1129f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_IN_BIT			BIT_ULL(55)
1139f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_C_BIT			BIT_ULL(63)
1149f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_DR_BIT			BIT_ULL(61)
1159f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_ELERR_POS		46
1169f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_RTYPE_POS		56
1179f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_LERR_POS			60
1189f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_BUFLEN_POS		48
1199f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_HOENQ_NUM_POS		48
1209f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_ELERR_RD(m)		\
1219f2fd0dfSRameshwar Prasad Sahu 	(((m) >> XGENE_DMA_DESC_ELERR_POS) & 0x3)
1229f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_LERR_RD(m)		\
1239f2fd0dfSRameshwar Prasad Sahu 	(((m) >> XGENE_DMA_DESC_LERR_POS) & 0x7)
1249f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_STATUS(elerr, lerr)	\
1259f2fd0dfSRameshwar Prasad Sahu 	(((elerr) << 4) | (lerr))
1269f2fd0dfSRameshwar Prasad Sahu 
1279f2fd0dfSRameshwar Prasad Sahu /* X-Gene DMA descriptor empty s/w signature */
1289f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_EMPTY_SIGNATURE		~0ULL
1299f2fd0dfSRameshwar Prasad Sahu 
1309f2fd0dfSRameshwar Prasad Sahu /* X-Gene DMA configurable parameters defines */
1319f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_NUM		512
1329f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_BUFNUM		0x0
1339f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_CPU_BUFNUM		0x18
1349f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_OWNER_DMA	0x03
1359f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_OWNER_CPU	0x0F
1369f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_TYPE_REGULAR	0x01
1379f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_WQ_DESC_SIZE	32	/* 32 Bytes */
1389f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_RING_NUM_CONFIG	5
1399f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_MAX_CHANNEL		4
1409f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_XOR_CHANNEL		0
1419f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_PQ_CHANNEL		1
1429f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_MAX_BYTE_CNT		0x4000	/* 16 KB */
1439f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_MAX_64B_DESC_BYTE_CNT	0x14000	/* 80 KB */
1449f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_MAX_XOR_SRC		5
1459f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_16K_BUFFER_LEN_CODE	0x0
1466d0767c1SRameshwar Prasad Sahu #define XGENE_DMA_INVALID_LEN_CODE	0x7800000000000000ULL
1479f2fd0dfSRameshwar Prasad Sahu 
1489f2fd0dfSRameshwar Prasad Sahu /* X-Gene DMA descriptor error codes */
1499f2fd0dfSRameshwar Prasad Sahu #define ERR_DESC_AXI			0x01
1509f2fd0dfSRameshwar Prasad Sahu #define ERR_BAD_DESC			0x02
1519f2fd0dfSRameshwar Prasad Sahu #define ERR_READ_DATA_AXI		0x03
1529f2fd0dfSRameshwar Prasad Sahu #define ERR_WRITE_DATA_AXI		0x04
1539f2fd0dfSRameshwar Prasad Sahu #define ERR_FBP_TIMEOUT			0x05
1549f2fd0dfSRameshwar Prasad Sahu #define ERR_ECC				0x06
1559f2fd0dfSRameshwar Prasad Sahu #define ERR_DIFF_SIZE			0x08
1569f2fd0dfSRameshwar Prasad Sahu #define ERR_SCT_GAT_LEN			0x09
1579f2fd0dfSRameshwar Prasad Sahu #define ERR_CRC_ERR			0x11
1589f2fd0dfSRameshwar Prasad Sahu #define ERR_CHKSUM			0x12
1599f2fd0dfSRameshwar Prasad Sahu #define ERR_DIF				0x13
1609f2fd0dfSRameshwar Prasad Sahu 
1619f2fd0dfSRameshwar Prasad Sahu /* X-Gene DMA error interrupt codes */
1629f2fd0dfSRameshwar Prasad Sahu #define ERR_DIF_SIZE_INT		0x0
1639f2fd0dfSRameshwar Prasad Sahu #define ERR_GS_ERR_INT			0x1
1649f2fd0dfSRameshwar Prasad Sahu #define ERR_FPB_TIMEO_INT		0x2
1659f2fd0dfSRameshwar Prasad Sahu #define ERR_WFIFO_OVF_INT		0x3
1669f2fd0dfSRameshwar Prasad Sahu #define ERR_RFIFO_OVF_INT		0x4
1679f2fd0dfSRameshwar Prasad Sahu #define ERR_WR_TIMEO_INT		0x5
1689f2fd0dfSRameshwar Prasad Sahu #define ERR_RD_TIMEO_INT		0x6
1699f2fd0dfSRameshwar Prasad Sahu #define ERR_WR_ERR_INT			0x7
1709f2fd0dfSRameshwar Prasad Sahu #define ERR_RD_ERR_INT			0x8
1719f2fd0dfSRameshwar Prasad Sahu #define ERR_BAD_DESC_INT		0x9
1729f2fd0dfSRameshwar Prasad Sahu #define ERR_DESC_DST_INT		0xA
1739f2fd0dfSRameshwar Prasad Sahu #define ERR_DESC_SRC_INT		0xB
1749f2fd0dfSRameshwar Prasad Sahu 
1759f2fd0dfSRameshwar Prasad Sahu /* X-Gene DMA flyby operation code */
1766d0767c1SRameshwar Prasad Sahu #define FLYBY_2SRC_XOR			0x80
1776d0767c1SRameshwar Prasad Sahu #define FLYBY_3SRC_XOR			0x90
1786d0767c1SRameshwar Prasad Sahu #define FLYBY_4SRC_XOR			0xA0
1796d0767c1SRameshwar Prasad Sahu #define FLYBY_5SRC_XOR			0xB0
1809f2fd0dfSRameshwar Prasad Sahu 
1819f2fd0dfSRameshwar Prasad Sahu /* X-Gene DMA SW descriptor flags */
1829f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_FLAG_64B_DESC		BIT(0)
1839f2fd0dfSRameshwar Prasad Sahu 
1849f2fd0dfSRameshwar Prasad Sahu /* Define to dump X-Gene DMA descriptor */
1859f2fd0dfSRameshwar Prasad Sahu #define XGENE_DMA_DESC_DUMP(desc, m)	\
1869f2fd0dfSRameshwar Prasad Sahu 	print_hex_dump(KERN_ERR, (m),	\
1879f2fd0dfSRameshwar Prasad Sahu 			DUMP_PREFIX_ADDRESS, 16, 8, (desc), 32, 0)
1889f2fd0dfSRameshwar Prasad Sahu 
1899f2fd0dfSRameshwar Prasad Sahu #define to_dma_desc_sw(tx)		\
1909f2fd0dfSRameshwar Prasad Sahu 	container_of(tx, struct xgene_dma_desc_sw, tx)
1919f2fd0dfSRameshwar Prasad Sahu #define to_dma_chan(dchan)		\
1929f2fd0dfSRameshwar Prasad Sahu 	container_of(dchan, struct xgene_dma_chan, dma_chan)
1939f2fd0dfSRameshwar Prasad Sahu 
1949f2fd0dfSRameshwar Prasad Sahu #define chan_dbg(chan, fmt, arg...)	\
1959f2fd0dfSRameshwar Prasad Sahu 	dev_dbg(chan->dev, "%s: " fmt, chan->name, ##arg)
1969f2fd0dfSRameshwar Prasad Sahu #define chan_err(chan, fmt, arg...)	\
1979f2fd0dfSRameshwar Prasad Sahu 	dev_err(chan->dev, "%s: " fmt, chan->name, ##arg)
1989f2fd0dfSRameshwar Prasad Sahu 
1999f2fd0dfSRameshwar Prasad Sahu struct xgene_dma_desc_hw {
2006d0767c1SRameshwar Prasad Sahu 	__le64 m0;
2016d0767c1SRameshwar Prasad Sahu 	__le64 m1;
2026d0767c1SRameshwar Prasad Sahu 	__le64 m2;
2036d0767c1SRameshwar Prasad Sahu 	__le64 m3;
2049f2fd0dfSRameshwar Prasad Sahu };
2059f2fd0dfSRameshwar Prasad Sahu 
2069f2fd0dfSRameshwar Prasad Sahu enum xgene_dma_ring_cfgsize {
2079f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_CFG_SIZE_512B,
2089f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_CFG_SIZE_2KB,
2099f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_CFG_SIZE_16KB,
2109f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_CFG_SIZE_64KB,
2119f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_CFG_SIZE_512KB,
2129f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_CFG_SIZE_INVALID
2139f2fd0dfSRameshwar Prasad Sahu };
2149f2fd0dfSRameshwar Prasad Sahu 
2159f2fd0dfSRameshwar Prasad Sahu struct xgene_dma_ring {
2169f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma *pdma;
2179f2fd0dfSRameshwar Prasad Sahu 	u8 buf_num;
2189f2fd0dfSRameshwar Prasad Sahu 	u16 id;
2199f2fd0dfSRameshwar Prasad Sahu 	u16 num;
2209f2fd0dfSRameshwar Prasad Sahu 	u16 head;
2219f2fd0dfSRameshwar Prasad Sahu 	u16 owner;
2229f2fd0dfSRameshwar Prasad Sahu 	u16 slots;
2239f2fd0dfSRameshwar Prasad Sahu 	u16 dst_ring_num;
2249f2fd0dfSRameshwar Prasad Sahu 	u32 size;
2259f2fd0dfSRameshwar Prasad Sahu 	void __iomem *cmd;
2269f2fd0dfSRameshwar Prasad Sahu 	void __iomem *cmd_base;
2279f2fd0dfSRameshwar Prasad Sahu 	dma_addr_t desc_paddr;
2289f2fd0dfSRameshwar Prasad Sahu 	u32 state[XGENE_DMA_RING_NUM_CONFIG];
2299f2fd0dfSRameshwar Prasad Sahu 	enum xgene_dma_ring_cfgsize cfgsize;
2309f2fd0dfSRameshwar Prasad Sahu 	union {
2319f2fd0dfSRameshwar Prasad Sahu 		void *desc_vaddr;
2329f2fd0dfSRameshwar Prasad Sahu 		struct xgene_dma_desc_hw *desc_hw;
2339f2fd0dfSRameshwar Prasad Sahu 	};
2349f2fd0dfSRameshwar Prasad Sahu };
2359f2fd0dfSRameshwar Prasad Sahu 
2369f2fd0dfSRameshwar Prasad Sahu struct xgene_dma_desc_sw {
2379f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_hw desc1;
2389f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_hw desc2;
2399f2fd0dfSRameshwar Prasad Sahu 	u32 flags;
2409f2fd0dfSRameshwar Prasad Sahu 	struct list_head node;
2419f2fd0dfSRameshwar Prasad Sahu 	struct list_head tx_list;
2429f2fd0dfSRameshwar Prasad Sahu 	struct dma_async_tx_descriptor tx;
2439f2fd0dfSRameshwar Prasad Sahu };
2449f2fd0dfSRameshwar Prasad Sahu 
2459f2fd0dfSRameshwar Prasad Sahu /**
2469f2fd0dfSRameshwar Prasad Sahu  * struct xgene_dma_chan - internal representation of an X-Gene DMA channel
2479f2fd0dfSRameshwar Prasad Sahu  * @dma_chan: dmaengine channel object member
2489f2fd0dfSRameshwar Prasad Sahu  * @pdma: X-Gene DMA device structure reference
2499f2fd0dfSRameshwar Prasad Sahu  * @dev: struct device reference for dma mapping api
2509f2fd0dfSRameshwar Prasad Sahu  * @id: raw id of this channel
2519f2fd0dfSRameshwar Prasad Sahu  * @rx_irq: channel IRQ
2529f2fd0dfSRameshwar Prasad Sahu  * @name: name of X-Gene DMA channel
2539f2fd0dfSRameshwar Prasad Sahu  * @lock: serializes enqueue/dequeue operations to the descriptor pool
2549f2fd0dfSRameshwar Prasad Sahu  * @pending: number of transaction request pushed to DMA controller for
2559f2fd0dfSRameshwar Prasad Sahu  *	execution, but still waiting for completion,
2569f2fd0dfSRameshwar Prasad Sahu  * @max_outstanding: max number of outstanding request we can push to channel
2579f2fd0dfSRameshwar Prasad Sahu  * @ld_pending: descriptors which are queued to run, but have not yet been
2589f2fd0dfSRameshwar Prasad Sahu  *	submitted to the hardware for execution
2599f2fd0dfSRameshwar Prasad Sahu  * @ld_running: descriptors which are currently being executing by the hardware
2609f2fd0dfSRameshwar Prasad Sahu  * @ld_completed: descriptors which have finished execution by the hardware.
2619f2fd0dfSRameshwar Prasad Sahu  *	These descriptors have already had their cleanup actions run. They
2629f2fd0dfSRameshwar Prasad Sahu  *	are waiting for the ACK bit to be set by the async tx API.
2639f2fd0dfSRameshwar Prasad Sahu  * @desc_pool: descriptor pool for DMA operations
2649f2fd0dfSRameshwar Prasad Sahu  * @tasklet: bottom half where all completed descriptors cleans
2659f2fd0dfSRameshwar Prasad Sahu  * @tx_ring: transmit ring descriptor that we use to prepare actual
2669f2fd0dfSRameshwar Prasad Sahu  *	descriptors for further executions
2679f2fd0dfSRameshwar Prasad Sahu  * @rx_ring: receive ring descriptor that we use to get completed DMA
2689f2fd0dfSRameshwar Prasad Sahu  *	descriptors during cleanup time
2699f2fd0dfSRameshwar Prasad Sahu  */
2709f2fd0dfSRameshwar Prasad Sahu struct xgene_dma_chan {
2719f2fd0dfSRameshwar Prasad Sahu 	struct dma_chan dma_chan;
2729f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma *pdma;
2739f2fd0dfSRameshwar Prasad Sahu 	struct device *dev;
2749f2fd0dfSRameshwar Prasad Sahu 	int id;
2759f2fd0dfSRameshwar Prasad Sahu 	int rx_irq;
276ed1f0418SDan Carpenter 	char name[10];
2779f2fd0dfSRameshwar Prasad Sahu 	spinlock_t lock;
2789f2fd0dfSRameshwar Prasad Sahu 	int pending;
2799f2fd0dfSRameshwar Prasad Sahu 	int max_outstanding;
2809f2fd0dfSRameshwar Prasad Sahu 	struct list_head ld_pending;
2819f2fd0dfSRameshwar Prasad Sahu 	struct list_head ld_running;
2829f2fd0dfSRameshwar Prasad Sahu 	struct list_head ld_completed;
2839f2fd0dfSRameshwar Prasad Sahu 	struct dma_pool *desc_pool;
2849f2fd0dfSRameshwar Prasad Sahu 	struct tasklet_struct tasklet;
2859f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_ring tx_ring;
2869f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_ring rx_ring;
2879f2fd0dfSRameshwar Prasad Sahu };
2889f2fd0dfSRameshwar Prasad Sahu 
2899f2fd0dfSRameshwar Prasad Sahu /**
2909f2fd0dfSRameshwar Prasad Sahu  * struct xgene_dma - internal representation of an X-Gene DMA device
2915726164fSLee Jones  * @dev: reference to this device's struct device
2925726164fSLee Jones  * @clk: reference to this device's clock
2939f2fd0dfSRameshwar Prasad Sahu  * @err_irq: DMA error irq number
2949f2fd0dfSRameshwar Prasad Sahu  * @ring_num: start id number for DMA ring
2959f2fd0dfSRameshwar Prasad Sahu  * @csr_dma: base for DMA register access
2969f2fd0dfSRameshwar Prasad Sahu  * @csr_ring: base for DMA ring register access
2979f2fd0dfSRameshwar Prasad Sahu  * @csr_ring_cmd: base for DMA ring command register access
2989f2fd0dfSRameshwar Prasad Sahu  * @csr_efuse: base for efuse register access
2999f2fd0dfSRameshwar Prasad Sahu  * @dma_dev: embedded struct dma_device
3009f2fd0dfSRameshwar Prasad Sahu  * @chan: reference to X-Gene DMA channels
3019f2fd0dfSRameshwar Prasad Sahu  */
3029f2fd0dfSRameshwar Prasad Sahu struct xgene_dma {
3039f2fd0dfSRameshwar Prasad Sahu 	struct device *dev;
3049f2fd0dfSRameshwar Prasad Sahu 	struct clk *clk;
3059f2fd0dfSRameshwar Prasad Sahu 	int err_irq;
3069f2fd0dfSRameshwar Prasad Sahu 	int ring_num;
3079f2fd0dfSRameshwar Prasad Sahu 	void __iomem *csr_dma;
3089f2fd0dfSRameshwar Prasad Sahu 	void __iomem *csr_ring;
3099f2fd0dfSRameshwar Prasad Sahu 	void __iomem *csr_ring_cmd;
3109f2fd0dfSRameshwar Prasad Sahu 	void __iomem *csr_efuse;
3119f2fd0dfSRameshwar Prasad Sahu 	struct dma_device dma_dev[XGENE_DMA_MAX_CHANNEL];
3129f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan chan[XGENE_DMA_MAX_CHANNEL];
3139f2fd0dfSRameshwar Prasad Sahu };
3149f2fd0dfSRameshwar Prasad Sahu 
3159f2fd0dfSRameshwar Prasad Sahu static const char * const xgene_dma_desc_err[] = {
3169f2fd0dfSRameshwar Prasad Sahu 	[ERR_DESC_AXI] = "AXI error when reading src/dst link list",
3179f2fd0dfSRameshwar Prasad Sahu 	[ERR_BAD_DESC] = "ERR or El_ERR fields not set to zero in desc",
3189f2fd0dfSRameshwar Prasad Sahu 	[ERR_READ_DATA_AXI] = "AXI error when reading data",
3199f2fd0dfSRameshwar Prasad Sahu 	[ERR_WRITE_DATA_AXI] = "AXI error when writing data",
3209f2fd0dfSRameshwar Prasad Sahu 	[ERR_FBP_TIMEOUT] = "Timeout on bufpool fetch",
3219f2fd0dfSRameshwar Prasad Sahu 	[ERR_ECC] = "ECC double bit error",
3229f2fd0dfSRameshwar Prasad Sahu 	[ERR_DIFF_SIZE] = "Bufpool too small to hold all the DIF result",
3239f2fd0dfSRameshwar Prasad Sahu 	[ERR_SCT_GAT_LEN] = "Gather and scatter data length not same",
3249f2fd0dfSRameshwar Prasad Sahu 	[ERR_CRC_ERR] = "CRC error",
3259f2fd0dfSRameshwar Prasad Sahu 	[ERR_CHKSUM] = "Checksum error",
3269f2fd0dfSRameshwar Prasad Sahu 	[ERR_DIF] = "DIF error",
3279f2fd0dfSRameshwar Prasad Sahu };
3289f2fd0dfSRameshwar Prasad Sahu 
3299f2fd0dfSRameshwar Prasad Sahu static const char * const xgene_dma_err[] = {
3309f2fd0dfSRameshwar Prasad Sahu 	[ERR_DIF_SIZE_INT] = "DIF size error",
3319f2fd0dfSRameshwar Prasad Sahu 	[ERR_GS_ERR_INT] = "Gather scatter not same size error",
3329f2fd0dfSRameshwar Prasad Sahu 	[ERR_FPB_TIMEO_INT] = "Free pool time out error",
3339f2fd0dfSRameshwar Prasad Sahu 	[ERR_WFIFO_OVF_INT] = "Write FIFO over flow error",
3349f2fd0dfSRameshwar Prasad Sahu 	[ERR_RFIFO_OVF_INT] = "Read FIFO over flow error",
3359f2fd0dfSRameshwar Prasad Sahu 	[ERR_WR_TIMEO_INT] = "Write time out error",
3369f2fd0dfSRameshwar Prasad Sahu 	[ERR_RD_TIMEO_INT] = "Read time out error",
3379f2fd0dfSRameshwar Prasad Sahu 	[ERR_WR_ERR_INT] = "HBF bus write error",
3389f2fd0dfSRameshwar Prasad Sahu 	[ERR_RD_ERR_INT] = "HBF bus read error",
3399f2fd0dfSRameshwar Prasad Sahu 	[ERR_BAD_DESC_INT] = "Ring descriptor HE0 not set error",
3409f2fd0dfSRameshwar Prasad Sahu 	[ERR_DESC_DST_INT] = "HFB reading dst link address error",
3419f2fd0dfSRameshwar Prasad Sahu 	[ERR_DESC_SRC_INT] = "HFB reading src link address error",
3429f2fd0dfSRameshwar Prasad Sahu };
3439f2fd0dfSRameshwar Prasad Sahu 
is_pq_enabled(struct xgene_dma * pdma)3449f2fd0dfSRameshwar Prasad Sahu static bool is_pq_enabled(struct xgene_dma *pdma)
3459f2fd0dfSRameshwar Prasad Sahu {
3469f2fd0dfSRameshwar Prasad Sahu 	u32 val;
3479f2fd0dfSRameshwar Prasad Sahu 
3489f2fd0dfSRameshwar Prasad Sahu 	val = ioread32(pdma->csr_efuse + XGENE_SOC_JTAG1_SHADOW);
3499f2fd0dfSRameshwar Prasad Sahu 	return !(val & XGENE_DMA_PQ_DISABLE_MASK);
3509f2fd0dfSRameshwar Prasad Sahu }
3519f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_encode_len(size_t len)3526d0767c1SRameshwar Prasad Sahu static u64 xgene_dma_encode_len(size_t len)
3539f2fd0dfSRameshwar Prasad Sahu {
3549f2fd0dfSRameshwar Prasad Sahu 	return (len < XGENE_DMA_MAX_BYTE_CNT) ?
3556d0767c1SRameshwar Prasad Sahu 		((u64)len << XGENE_DMA_DESC_BUFLEN_POS) :
3566d0767c1SRameshwar Prasad Sahu 		XGENE_DMA_16K_BUFFER_LEN_CODE;
3579f2fd0dfSRameshwar Prasad Sahu }
3589f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_encode_xor_flyby(u32 src_cnt)3599f2fd0dfSRameshwar Prasad Sahu static u8 xgene_dma_encode_xor_flyby(u32 src_cnt)
3609f2fd0dfSRameshwar Prasad Sahu {
3619f2fd0dfSRameshwar Prasad Sahu 	static u8 flyby_type[] = {
3629f2fd0dfSRameshwar Prasad Sahu 		FLYBY_2SRC_XOR, /* Dummy */
3639f2fd0dfSRameshwar Prasad Sahu 		FLYBY_2SRC_XOR, /* Dummy */
3649f2fd0dfSRameshwar Prasad Sahu 		FLYBY_2SRC_XOR,
3659f2fd0dfSRameshwar Prasad Sahu 		FLYBY_3SRC_XOR,
3669f2fd0dfSRameshwar Prasad Sahu 		FLYBY_4SRC_XOR,
3679f2fd0dfSRameshwar Prasad Sahu 		FLYBY_5SRC_XOR
3689f2fd0dfSRameshwar Prasad Sahu 	};
3699f2fd0dfSRameshwar Prasad Sahu 
3709f2fd0dfSRameshwar Prasad Sahu 	return flyby_type[src_cnt];
3719f2fd0dfSRameshwar Prasad Sahu }
3729f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_set_src_buffer(__le64 * ext8,size_t * len,dma_addr_t * paddr)3736d0767c1SRameshwar Prasad Sahu static void xgene_dma_set_src_buffer(__le64 *ext8, size_t *len,
3749f2fd0dfSRameshwar Prasad Sahu 				     dma_addr_t *paddr)
3759f2fd0dfSRameshwar Prasad Sahu {
3769f2fd0dfSRameshwar Prasad Sahu 	size_t nbytes = (*len < XGENE_DMA_MAX_BYTE_CNT) ?
3779f2fd0dfSRameshwar Prasad Sahu 			*len : XGENE_DMA_MAX_BYTE_CNT;
3789f2fd0dfSRameshwar Prasad Sahu 
3796d0767c1SRameshwar Prasad Sahu 	*ext8 |= cpu_to_le64(*paddr);
3806d0767c1SRameshwar Prasad Sahu 	*ext8 |= cpu_to_le64(xgene_dma_encode_len(nbytes));
3819f2fd0dfSRameshwar Prasad Sahu 	*len -= nbytes;
3829f2fd0dfSRameshwar Prasad Sahu 	*paddr += nbytes;
3839f2fd0dfSRameshwar Prasad Sahu }
3849f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_lookup_ext8(struct xgene_dma_desc_hw * desc,int idx)3856d0767c1SRameshwar Prasad Sahu static __le64 *xgene_dma_lookup_ext8(struct xgene_dma_desc_hw *desc, int idx)
3869f2fd0dfSRameshwar Prasad Sahu {
3876d0767c1SRameshwar Prasad Sahu 	switch (idx) {
3886d0767c1SRameshwar Prasad Sahu 	case 0:
3896d0767c1SRameshwar Prasad Sahu 		return &desc->m1;
3906d0767c1SRameshwar Prasad Sahu 	case 1:
3916d0767c1SRameshwar Prasad Sahu 		return &desc->m0;
3926d0767c1SRameshwar Prasad Sahu 	case 2:
3936d0767c1SRameshwar Prasad Sahu 		return &desc->m3;
3946d0767c1SRameshwar Prasad Sahu 	case 3:
3956d0767c1SRameshwar Prasad Sahu 		return &desc->m2;
3966d0767c1SRameshwar Prasad Sahu 	default:
3976d0767c1SRameshwar Prasad Sahu 		pr_err("Invalid dma descriptor index\n");
3989f2fd0dfSRameshwar Prasad Sahu 	}
3999f2fd0dfSRameshwar Prasad Sahu 
4006d0767c1SRameshwar Prasad Sahu 	return NULL;
4016d0767c1SRameshwar Prasad Sahu }
4026d0767c1SRameshwar Prasad Sahu 
xgene_dma_init_desc(struct xgene_dma_desc_hw * desc,u16 dst_ring_num)4036d0767c1SRameshwar Prasad Sahu static void xgene_dma_init_desc(struct xgene_dma_desc_hw *desc,
4046d0767c1SRameshwar Prasad Sahu 				u16 dst_ring_num)
4059f2fd0dfSRameshwar Prasad Sahu {
4066d0767c1SRameshwar Prasad Sahu 	desc->m0 |= cpu_to_le64(XGENE_DMA_DESC_IN_BIT);
4076d0767c1SRameshwar Prasad Sahu 	desc->m0 |= cpu_to_le64((u64)XGENE_DMA_RING_OWNER_DMA <<
4086d0767c1SRameshwar Prasad Sahu 				XGENE_DMA_DESC_RTYPE_POS);
4096d0767c1SRameshwar Prasad Sahu 	desc->m1 |= cpu_to_le64(XGENE_DMA_DESC_C_BIT);
4106d0767c1SRameshwar Prasad Sahu 	desc->m3 |= cpu_to_le64((u64)dst_ring_num <<
4116d0767c1SRameshwar Prasad Sahu 				XGENE_DMA_DESC_HOENQ_NUM_POS);
4129f2fd0dfSRameshwar Prasad Sahu }
4139f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_prep_xor_desc(struct xgene_dma_chan * chan,struct xgene_dma_desc_sw * desc_sw,dma_addr_t * dst,dma_addr_t * src,u32 src_cnt,size_t * nbytes,const u8 * scf)4149f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_prep_xor_desc(struct xgene_dma_chan *chan,
4159f2fd0dfSRameshwar Prasad Sahu 				    struct xgene_dma_desc_sw *desc_sw,
4169f2fd0dfSRameshwar Prasad Sahu 				    dma_addr_t *dst, dma_addr_t *src,
4179f2fd0dfSRameshwar Prasad Sahu 				    u32 src_cnt, size_t *nbytes,
4189f2fd0dfSRameshwar Prasad Sahu 				    const u8 *scf)
4199f2fd0dfSRameshwar Prasad Sahu {
4206d0767c1SRameshwar Prasad Sahu 	struct xgene_dma_desc_hw *desc1, *desc2;
4219f2fd0dfSRameshwar Prasad Sahu 	size_t len = *nbytes;
4229f2fd0dfSRameshwar Prasad Sahu 	int i;
4239f2fd0dfSRameshwar Prasad Sahu 
4249f2fd0dfSRameshwar Prasad Sahu 	desc1 = &desc_sw->desc1;
4259f2fd0dfSRameshwar Prasad Sahu 	desc2 = &desc_sw->desc2;
4269f2fd0dfSRameshwar Prasad Sahu 
4279f2fd0dfSRameshwar Prasad Sahu 	/* Initialize DMA descriptor */
4289f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_init_desc(desc1, chan->tx_ring.dst_ring_num);
4299f2fd0dfSRameshwar Prasad Sahu 
4309f2fd0dfSRameshwar Prasad Sahu 	/* Set destination address */
4316d0767c1SRameshwar Prasad Sahu 	desc1->m2 |= cpu_to_le64(XGENE_DMA_DESC_DR_BIT);
4326d0767c1SRameshwar Prasad Sahu 	desc1->m3 |= cpu_to_le64(*dst);
4339f2fd0dfSRameshwar Prasad Sahu 
4349f2fd0dfSRameshwar Prasad Sahu 	/* We have multiple source addresses, so need to set NV bit*/
4356d0767c1SRameshwar Prasad Sahu 	desc1->m0 |= cpu_to_le64(XGENE_DMA_DESC_NV_BIT);
4369f2fd0dfSRameshwar Prasad Sahu 
4379f2fd0dfSRameshwar Prasad Sahu 	/* Set flyby opcode */
4386d0767c1SRameshwar Prasad Sahu 	desc1->m2 |= cpu_to_le64(xgene_dma_encode_xor_flyby(src_cnt));
4399f2fd0dfSRameshwar Prasad Sahu 
4409f2fd0dfSRameshwar Prasad Sahu 	/* Set 1st to 5th source addresses */
4419f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < src_cnt; i++) {
4429f2fd0dfSRameshwar Prasad Sahu 		len = *nbytes;
4436d0767c1SRameshwar Prasad Sahu 		xgene_dma_set_src_buffer((i == 0) ? &desc1->m1 :
4449f2fd0dfSRameshwar Prasad Sahu 					 xgene_dma_lookup_ext8(desc2, i - 1),
4459f2fd0dfSRameshwar Prasad Sahu 					 &len, &src[i]);
4466d0767c1SRameshwar Prasad Sahu 		desc1->m2 |= cpu_to_le64((scf[i] << ((i + 1) * 8)));
4479f2fd0dfSRameshwar Prasad Sahu 	}
4489f2fd0dfSRameshwar Prasad Sahu 
4499f2fd0dfSRameshwar Prasad Sahu 	/* Update meta data */
4509f2fd0dfSRameshwar Prasad Sahu 	*nbytes = len;
4519f2fd0dfSRameshwar Prasad Sahu 	*dst += XGENE_DMA_MAX_BYTE_CNT;
4529f2fd0dfSRameshwar Prasad Sahu 
4539f2fd0dfSRameshwar Prasad Sahu 	/* We need always 64B descriptor to perform xor or pq operations */
4549f2fd0dfSRameshwar Prasad Sahu 	desc_sw->flags |= XGENE_DMA_FLAG_64B_DESC;
4559f2fd0dfSRameshwar Prasad Sahu }
4569f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_tx_submit(struct dma_async_tx_descriptor * tx)4579f2fd0dfSRameshwar Prasad Sahu static dma_cookie_t xgene_dma_tx_submit(struct dma_async_tx_descriptor *tx)
4589f2fd0dfSRameshwar Prasad Sahu {
4599f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_sw *desc;
4609f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan;
4619f2fd0dfSRameshwar Prasad Sahu 	dma_cookie_t cookie;
4629f2fd0dfSRameshwar Prasad Sahu 
4639f2fd0dfSRameshwar Prasad Sahu 	if (unlikely(!tx))
4649f2fd0dfSRameshwar Prasad Sahu 		return -EINVAL;
4659f2fd0dfSRameshwar Prasad Sahu 
4669f2fd0dfSRameshwar Prasad Sahu 	chan = to_dma_chan(tx->chan);
4679f2fd0dfSRameshwar Prasad Sahu 	desc = to_dma_desc_sw(tx);
4689f2fd0dfSRameshwar Prasad Sahu 
4699f2fd0dfSRameshwar Prasad Sahu 	spin_lock_bh(&chan->lock);
4709f2fd0dfSRameshwar Prasad Sahu 
4719f2fd0dfSRameshwar Prasad Sahu 	cookie = dma_cookie_assign(tx);
4729f2fd0dfSRameshwar Prasad Sahu 
4739f2fd0dfSRameshwar Prasad Sahu 	/* Add this transaction list onto the tail of the pending queue */
4749f2fd0dfSRameshwar Prasad Sahu 	list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
4759f2fd0dfSRameshwar Prasad Sahu 
4769f2fd0dfSRameshwar Prasad Sahu 	spin_unlock_bh(&chan->lock);
4779f2fd0dfSRameshwar Prasad Sahu 
4789f2fd0dfSRameshwar Prasad Sahu 	return cookie;
4799f2fd0dfSRameshwar Prasad Sahu }
4809f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_clean_descriptor(struct xgene_dma_chan * chan,struct xgene_dma_desc_sw * desc)4819f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_clean_descriptor(struct xgene_dma_chan *chan,
4829f2fd0dfSRameshwar Prasad Sahu 				       struct xgene_dma_desc_sw *desc)
4839f2fd0dfSRameshwar Prasad Sahu {
4849f2fd0dfSRameshwar Prasad Sahu 	list_del(&desc->node);
4859f2fd0dfSRameshwar Prasad Sahu 	chan_dbg(chan, "LD %p free\n", desc);
4869f2fd0dfSRameshwar Prasad Sahu 	dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
4879f2fd0dfSRameshwar Prasad Sahu }
4889f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_alloc_descriptor(struct xgene_dma_chan * chan)4899f2fd0dfSRameshwar Prasad Sahu static struct xgene_dma_desc_sw *xgene_dma_alloc_descriptor(
4909f2fd0dfSRameshwar Prasad Sahu 				 struct xgene_dma_chan *chan)
4919f2fd0dfSRameshwar Prasad Sahu {
4929f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_sw *desc;
4939f2fd0dfSRameshwar Prasad Sahu 	dma_addr_t phys;
4949f2fd0dfSRameshwar Prasad Sahu 
4959c811209SVinod Koul 	desc = dma_pool_zalloc(chan->desc_pool, GFP_NOWAIT, &phys);
4969f2fd0dfSRameshwar Prasad Sahu 	if (!desc) {
4979f2fd0dfSRameshwar Prasad Sahu 		chan_err(chan, "Failed to allocate LDs\n");
4989f2fd0dfSRameshwar Prasad Sahu 		return NULL;
4999f2fd0dfSRameshwar Prasad Sahu 	}
5009f2fd0dfSRameshwar Prasad Sahu 
5019f2fd0dfSRameshwar Prasad Sahu 	INIT_LIST_HEAD(&desc->tx_list);
5029f2fd0dfSRameshwar Prasad Sahu 	desc->tx.phys = phys;
5039f2fd0dfSRameshwar Prasad Sahu 	desc->tx.tx_submit = xgene_dma_tx_submit;
5049f2fd0dfSRameshwar Prasad Sahu 	dma_async_tx_descriptor_init(&desc->tx, &chan->dma_chan);
5059f2fd0dfSRameshwar Prasad Sahu 
5069f2fd0dfSRameshwar Prasad Sahu 	chan_dbg(chan, "LD %p allocated\n", desc);
5079f2fd0dfSRameshwar Prasad Sahu 
5089f2fd0dfSRameshwar Prasad Sahu 	return desc;
5099f2fd0dfSRameshwar Prasad Sahu }
5109f2fd0dfSRameshwar Prasad Sahu 
5119f2fd0dfSRameshwar Prasad Sahu /**
5129f2fd0dfSRameshwar Prasad Sahu  * xgene_dma_clean_completed_descriptor - free all descriptors which
5139f2fd0dfSRameshwar Prasad Sahu  * has been completed and acked
5149f2fd0dfSRameshwar Prasad Sahu  * @chan: X-Gene DMA channel
5159f2fd0dfSRameshwar Prasad Sahu  *
5169f2fd0dfSRameshwar Prasad Sahu  * This function is used on all completed and acked descriptors.
5179f2fd0dfSRameshwar Prasad Sahu  */
xgene_dma_clean_completed_descriptor(struct xgene_dma_chan * chan)5189f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_clean_completed_descriptor(struct xgene_dma_chan *chan)
5199f2fd0dfSRameshwar Prasad Sahu {
5209f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_sw *desc, *_desc;
5219f2fd0dfSRameshwar Prasad Sahu 
5229f2fd0dfSRameshwar Prasad Sahu 	/* Run the callback for each descriptor, in order */
5239f2fd0dfSRameshwar Prasad Sahu 	list_for_each_entry_safe(desc, _desc, &chan->ld_completed, node) {
5249f2fd0dfSRameshwar Prasad Sahu 		if (async_tx_test_ack(&desc->tx))
5259f2fd0dfSRameshwar Prasad Sahu 			xgene_dma_clean_descriptor(chan, desc);
5269f2fd0dfSRameshwar Prasad Sahu 	}
5279f2fd0dfSRameshwar Prasad Sahu }
5289f2fd0dfSRameshwar Prasad Sahu 
5299f2fd0dfSRameshwar Prasad Sahu /**
5309f2fd0dfSRameshwar Prasad Sahu  * xgene_dma_run_tx_complete_actions - cleanup a single link descriptor
5319f2fd0dfSRameshwar Prasad Sahu  * @chan: X-Gene DMA channel
5329f2fd0dfSRameshwar Prasad Sahu  * @desc: descriptor to cleanup and free
5339f2fd0dfSRameshwar Prasad Sahu  *
5349f2fd0dfSRameshwar Prasad Sahu  * This function is used on a descriptor which has been executed by the DMA
5359f2fd0dfSRameshwar Prasad Sahu  * controller. It will run any callbacks, submit any dependencies.
5369f2fd0dfSRameshwar Prasad Sahu  */
xgene_dma_run_tx_complete_actions(struct xgene_dma_chan * chan,struct xgene_dma_desc_sw * desc)5379f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_run_tx_complete_actions(struct xgene_dma_chan *chan,
5389f2fd0dfSRameshwar Prasad Sahu 					      struct xgene_dma_desc_sw *desc)
5399f2fd0dfSRameshwar Prasad Sahu {
5409f2fd0dfSRameshwar Prasad Sahu 	struct dma_async_tx_descriptor *tx = &desc->tx;
5419f2fd0dfSRameshwar Prasad Sahu 
5429f2fd0dfSRameshwar Prasad Sahu 	/*
5439f2fd0dfSRameshwar Prasad Sahu 	 * If this is not the last transaction in the group,
5449f2fd0dfSRameshwar Prasad Sahu 	 * then no need to complete cookie and run any callback as
5459f2fd0dfSRameshwar Prasad Sahu 	 * this is not the tx_descriptor which had been sent to caller
5469f2fd0dfSRameshwar Prasad Sahu 	 * of this DMA request
5479f2fd0dfSRameshwar Prasad Sahu 	 */
5489f2fd0dfSRameshwar Prasad Sahu 
5499f2fd0dfSRameshwar Prasad Sahu 	if (tx->cookie == 0)
5509f2fd0dfSRameshwar Prasad Sahu 		return;
5519f2fd0dfSRameshwar Prasad Sahu 
5529f2fd0dfSRameshwar Prasad Sahu 	dma_cookie_complete(tx);
553fd3c69bdSDave Jiang 	dma_descriptor_unmap(tx);
5549f2fd0dfSRameshwar Prasad Sahu 
5559f2fd0dfSRameshwar Prasad Sahu 	/* Run the link descriptor callback function */
556b1f884a5SDave Jiang 	dmaengine_desc_get_callback_invoke(tx, NULL);
5579f2fd0dfSRameshwar Prasad Sahu 
5589f2fd0dfSRameshwar Prasad Sahu 	/* Run any dependencies */
5599f2fd0dfSRameshwar Prasad Sahu 	dma_run_dependencies(tx);
5609f2fd0dfSRameshwar Prasad Sahu }
5619f2fd0dfSRameshwar Prasad Sahu 
5629f2fd0dfSRameshwar Prasad Sahu /**
5639f2fd0dfSRameshwar Prasad Sahu  * xgene_dma_clean_running_descriptor - move the completed descriptor from
5649f2fd0dfSRameshwar Prasad Sahu  * ld_running to ld_completed
5659f2fd0dfSRameshwar Prasad Sahu  * @chan: X-Gene DMA channel
5669f2fd0dfSRameshwar Prasad Sahu  * @desc: the descriptor which is completed
5679f2fd0dfSRameshwar Prasad Sahu  *
5689f2fd0dfSRameshwar Prasad Sahu  * Free the descriptor directly if acked by async_tx api,
5699f2fd0dfSRameshwar Prasad Sahu  * else move it to queue ld_completed.
5709f2fd0dfSRameshwar Prasad Sahu  */
xgene_dma_clean_running_descriptor(struct xgene_dma_chan * chan,struct xgene_dma_desc_sw * desc)5719f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_clean_running_descriptor(struct xgene_dma_chan *chan,
5729f2fd0dfSRameshwar Prasad Sahu 					       struct xgene_dma_desc_sw *desc)
5739f2fd0dfSRameshwar Prasad Sahu {
5749f2fd0dfSRameshwar Prasad Sahu 	/* Remove from the list of running transactions */
5759f2fd0dfSRameshwar Prasad Sahu 	list_del(&desc->node);
5769f2fd0dfSRameshwar Prasad Sahu 
5779f2fd0dfSRameshwar Prasad Sahu 	/*
5789f2fd0dfSRameshwar Prasad Sahu 	 * the client is allowed to attach dependent operations
5799f2fd0dfSRameshwar Prasad Sahu 	 * until 'ack' is set
5809f2fd0dfSRameshwar Prasad Sahu 	 */
5819f2fd0dfSRameshwar Prasad Sahu 	if (!async_tx_test_ack(&desc->tx)) {
5829f2fd0dfSRameshwar Prasad Sahu 		/*
5839f2fd0dfSRameshwar Prasad Sahu 		 * Move this descriptor to the list of descriptors which is
5849f2fd0dfSRameshwar Prasad Sahu 		 * completed, but still awaiting the 'ack' bit to be set.
5859f2fd0dfSRameshwar Prasad Sahu 		 */
5869f2fd0dfSRameshwar Prasad Sahu 		list_add_tail(&desc->node, &chan->ld_completed);
5879f2fd0dfSRameshwar Prasad Sahu 		return;
5889f2fd0dfSRameshwar Prasad Sahu 	}
5899f2fd0dfSRameshwar Prasad Sahu 
5909f2fd0dfSRameshwar Prasad Sahu 	chan_dbg(chan, "LD %p free\n", desc);
5919f2fd0dfSRameshwar Prasad Sahu 	dma_pool_free(chan->desc_pool, desc, desc->tx.phys);
5929f2fd0dfSRameshwar Prasad Sahu }
5939f2fd0dfSRameshwar Prasad Sahu 
xgene_chan_xfer_request(struct xgene_dma_chan * chan,struct xgene_dma_desc_sw * desc_sw)594ee08b59dSRameshwar Prasad Sahu static void xgene_chan_xfer_request(struct xgene_dma_chan *chan,
5959f2fd0dfSRameshwar Prasad Sahu 				    struct xgene_dma_desc_sw *desc_sw)
5969f2fd0dfSRameshwar Prasad Sahu {
597ee08b59dSRameshwar Prasad Sahu 	struct xgene_dma_ring *ring = &chan->tx_ring;
5989f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_hw *desc_hw;
5999f2fd0dfSRameshwar Prasad Sahu 
6009f2fd0dfSRameshwar Prasad Sahu 	/* Get hw descriptor from DMA tx ring */
6019f2fd0dfSRameshwar Prasad Sahu 	desc_hw = &ring->desc_hw[ring->head];
6029f2fd0dfSRameshwar Prasad Sahu 
6039f2fd0dfSRameshwar Prasad Sahu 	/*
6049f2fd0dfSRameshwar Prasad Sahu 	 * Increment the head count to point next
6059f2fd0dfSRameshwar Prasad Sahu 	 * descriptor for next time
6069f2fd0dfSRameshwar Prasad Sahu 	 */
6079f2fd0dfSRameshwar Prasad Sahu 	if (++ring->head == ring->slots)
6089f2fd0dfSRameshwar Prasad Sahu 		ring->head = 0;
6099f2fd0dfSRameshwar Prasad Sahu 
6109f2fd0dfSRameshwar Prasad Sahu 	/* Copy prepared sw descriptor data to hw descriptor */
6119f2fd0dfSRameshwar Prasad Sahu 	memcpy(desc_hw, &desc_sw->desc1, sizeof(*desc_hw));
6129f2fd0dfSRameshwar Prasad Sahu 
6139f2fd0dfSRameshwar Prasad Sahu 	/*
6149f2fd0dfSRameshwar Prasad Sahu 	 * Check if we have prepared 64B descriptor,
6159f2fd0dfSRameshwar Prasad Sahu 	 * in this case we need one more hw descriptor
6169f2fd0dfSRameshwar Prasad Sahu 	 */
6179f2fd0dfSRameshwar Prasad Sahu 	if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) {
6189f2fd0dfSRameshwar Prasad Sahu 		desc_hw = &ring->desc_hw[ring->head];
6199f2fd0dfSRameshwar Prasad Sahu 
6209f2fd0dfSRameshwar Prasad Sahu 		if (++ring->head == ring->slots)
6219f2fd0dfSRameshwar Prasad Sahu 			ring->head = 0;
6229f2fd0dfSRameshwar Prasad Sahu 
6239f2fd0dfSRameshwar Prasad Sahu 		memcpy(desc_hw, &desc_sw->desc2, sizeof(*desc_hw));
6249f2fd0dfSRameshwar Prasad Sahu 	}
6259f2fd0dfSRameshwar Prasad Sahu 
626ee08b59dSRameshwar Prasad Sahu 	/* Increment the pending transaction count */
627ee08b59dSRameshwar Prasad Sahu 	chan->pending += ((desc_sw->flags &
628ee08b59dSRameshwar Prasad Sahu 			  XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
629ee08b59dSRameshwar Prasad Sahu 
6309f2fd0dfSRameshwar Prasad Sahu 	/* Notify the hw that we have descriptor ready for execution */
6319f2fd0dfSRameshwar Prasad Sahu 	iowrite32((desc_sw->flags & XGENE_DMA_FLAG_64B_DESC) ?
6329f2fd0dfSRameshwar Prasad Sahu 		  2 : 1, ring->cmd);
6339f2fd0dfSRameshwar Prasad Sahu }
6349f2fd0dfSRameshwar Prasad Sahu 
6359f2fd0dfSRameshwar Prasad Sahu /**
6369f2fd0dfSRameshwar Prasad Sahu  * xgene_chan_xfer_ld_pending - push any pending transactions to hw
6379f2fd0dfSRameshwar Prasad Sahu  * @chan : X-Gene DMA channel
6389f2fd0dfSRameshwar Prasad Sahu  *
6396d0767c1SRameshwar Prasad Sahu  * LOCKING: must hold chan->lock
6409f2fd0dfSRameshwar Prasad Sahu  */
xgene_chan_xfer_ld_pending(struct xgene_dma_chan * chan)6419f2fd0dfSRameshwar Prasad Sahu static void xgene_chan_xfer_ld_pending(struct xgene_dma_chan *chan)
6429f2fd0dfSRameshwar Prasad Sahu {
6439f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
6449f2fd0dfSRameshwar Prasad Sahu 
6459f2fd0dfSRameshwar Prasad Sahu 	/*
6469f2fd0dfSRameshwar Prasad Sahu 	 * If the list of pending descriptors is empty, then we
6479f2fd0dfSRameshwar Prasad Sahu 	 * don't need to do any work at all
6489f2fd0dfSRameshwar Prasad Sahu 	 */
6499f2fd0dfSRameshwar Prasad Sahu 	if (list_empty(&chan->ld_pending)) {
6509f2fd0dfSRameshwar Prasad Sahu 		chan_dbg(chan, "No pending LDs\n");
6519f2fd0dfSRameshwar Prasad Sahu 		return;
6529f2fd0dfSRameshwar Prasad Sahu 	}
6539f2fd0dfSRameshwar Prasad Sahu 
6549f2fd0dfSRameshwar Prasad Sahu 	/*
6559f2fd0dfSRameshwar Prasad Sahu 	 * Move elements from the queue of pending transactions onto the list
6569f2fd0dfSRameshwar Prasad Sahu 	 * of running transactions and push it to hw for further executions
6579f2fd0dfSRameshwar Prasad Sahu 	 */
6589f2fd0dfSRameshwar Prasad Sahu 	list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_pending, node) {
6599f2fd0dfSRameshwar Prasad Sahu 		/*
6609f2fd0dfSRameshwar Prasad Sahu 		 * Check if have pushed max number of transactions to hw
6619f2fd0dfSRameshwar Prasad Sahu 		 * as capable, so let's stop here and will push remaining
6629f2fd0dfSRameshwar Prasad Sahu 		 * elements from pening ld queue after completing some
6639f2fd0dfSRameshwar Prasad Sahu 		 * descriptors that we have already pushed
6649f2fd0dfSRameshwar Prasad Sahu 		 */
6659f2fd0dfSRameshwar Prasad Sahu 		if (chan->pending >= chan->max_outstanding)
6669f2fd0dfSRameshwar Prasad Sahu 			return;
6679f2fd0dfSRameshwar Prasad Sahu 
668ee08b59dSRameshwar Prasad Sahu 		xgene_chan_xfer_request(chan, desc_sw);
6699f2fd0dfSRameshwar Prasad Sahu 
6709f2fd0dfSRameshwar Prasad Sahu 		/*
6719f2fd0dfSRameshwar Prasad Sahu 		 * Delete this element from ld pending queue and append it to
6729f2fd0dfSRameshwar Prasad Sahu 		 * ld running queue
6739f2fd0dfSRameshwar Prasad Sahu 		 */
6749f2fd0dfSRameshwar Prasad Sahu 		list_move_tail(&desc_sw->node, &chan->ld_running);
6759f2fd0dfSRameshwar Prasad Sahu 	}
6769f2fd0dfSRameshwar Prasad Sahu }
6779f2fd0dfSRameshwar Prasad Sahu 
6789f2fd0dfSRameshwar Prasad Sahu /**
6799f2fd0dfSRameshwar Prasad Sahu  * xgene_dma_cleanup_descriptors - cleanup link descriptors which are completed
6809f2fd0dfSRameshwar Prasad Sahu  * and move them to ld_completed to free until flag 'ack' is set
6819f2fd0dfSRameshwar Prasad Sahu  * @chan: X-Gene DMA channel
6829f2fd0dfSRameshwar Prasad Sahu  *
6839f2fd0dfSRameshwar Prasad Sahu  * This function is used on descriptors which have been executed by the DMA
6849f2fd0dfSRameshwar Prasad Sahu  * controller. It will run any callbacks, submit any dependencies, then
6859f2fd0dfSRameshwar Prasad Sahu  * free these descriptors if flag 'ack' is set.
6869f2fd0dfSRameshwar Prasad Sahu  */
xgene_dma_cleanup_descriptors(struct xgene_dma_chan * chan)6879f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_cleanup_descriptors(struct xgene_dma_chan *chan)
6889f2fd0dfSRameshwar Prasad Sahu {
6899f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_ring *ring = &chan->rx_ring;
6909f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_sw *desc_sw, *_desc_sw;
6919f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_hw *desc_hw;
692005ce70bSRameshwar Prasad Sahu 	struct list_head ld_completed;
6939f2fd0dfSRameshwar Prasad Sahu 	u8 status;
6949f2fd0dfSRameshwar Prasad Sahu 
695005ce70bSRameshwar Prasad Sahu 	INIT_LIST_HEAD(&ld_completed);
696005ce70bSRameshwar Prasad Sahu 
697f177a431SJeff Xie 	spin_lock(&chan->lock);
698005ce70bSRameshwar Prasad Sahu 
6999f2fd0dfSRameshwar Prasad Sahu 	/* Clean already completed and acked descriptors */
7009f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_clean_completed_descriptor(chan);
7019f2fd0dfSRameshwar Prasad Sahu 
702005ce70bSRameshwar Prasad Sahu 	/* Move all completed descriptors to ld completed queue, in order */
7039f2fd0dfSRameshwar Prasad Sahu 	list_for_each_entry_safe(desc_sw, _desc_sw, &chan->ld_running, node) {
7049f2fd0dfSRameshwar Prasad Sahu 		/* Get subsequent hw descriptor from DMA rx ring */
7059f2fd0dfSRameshwar Prasad Sahu 		desc_hw = &ring->desc_hw[ring->head];
7069f2fd0dfSRameshwar Prasad Sahu 
7079f2fd0dfSRameshwar Prasad Sahu 		/* Check if this descriptor has been completed */
7086d0767c1SRameshwar Prasad Sahu 		if (unlikely(le64_to_cpu(desc_hw->m0) ==
7096d0767c1SRameshwar Prasad Sahu 			     XGENE_DMA_DESC_EMPTY_SIGNATURE))
7109f2fd0dfSRameshwar Prasad Sahu 			break;
7119f2fd0dfSRameshwar Prasad Sahu 
7129f2fd0dfSRameshwar Prasad Sahu 		if (++ring->head == ring->slots)
7139f2fd0dfSRameshwar Prasad Sahu 			ring->head = 0;
7149f2fd0dfSRameshwar Prasad Sahu 
7159f2fd0dfSRameshwar Prasad Sahu 		/* Check if we have any error with DMA transactions */
7169f2fd0dfSRameshwar Prasad Sahu 		status = XGENE_DMA_DESC_STATUS(
7179f2fd0dfSRameshwar Prasad Sahu 				XGENE_DMA_DESC_ELERR_RD(le64_to_cpu(
7189f2fd0dfSRameshwar Prasad Sahu 							desc_hw->m0)),
7199f2fd0dfSRameshwar Prasad Sahu 				XGENE_DMA_DESC_LERR_RD(le64_to_cpu(
7209f2fd0dfSRameshwar Prasad Sahu 						       desc_hw->m0)));
7219f2fd0dfSRameshwar Prasad Sahu 		if (status) {
7229f2fd0dfSRameshwar Prasad Sahu 			/* Print the DMA error type */
7239f2fd0dfSRameshwar Prasad Sahu 			chan_err(chan, "%s\n", xgene_dma_desc_err[status]);
7249f2fd0dfSRameshwar Prasad Sahu 
7259f2fd0dfSRameshwar Prasad Sahu 			/*
7269f2fd0dfSRameshwar Prasad Sahu 			 * We have DMA transactions error here. Dump DMA Tx
7279f2fd0dfSRameshwar Prasad Sahu 			 * and Rx descriptors for this request */
7289f2fd0dfSRameshwar Prasad Sahu 			XGENE_DMA_DESC_DUMP(&desc_sw->desc1,
7299f2fd0dfSRameshwar Prasad Sahu 					    "X-Gene DMA TX DESC1: ");
7309f2fd0dfSRameshwar Prasad Sahu 
7319f2fd0dfSRameshwar Prasad Sahu 			if (desc_sw->flags & XGENE_DMA_FLAG_64B_DESC)
7329f2fd0dfSRameshwar Prasad Sahu 				XGENE_DMA_DESC_DUMP(&desc_sw->desc2,
7339f2fd0dfSRameshwar Prasad Sahu 						    "X-Gene DMA TX DESC2: ");
7349f2fd0dfSRameshwar Prasad Sahu 
7359f2fd0dfSRameshwar Prasad Sahu 			XGENE_DMA_DESC_DUMP(desc_hw,
7369f2fd0dfSRameshwar Prasad Sahu 					    "X-Gene DMA RX ERR DESC: ");
7379f2fd0dfSRameshwar Prasad Sahu 		}
7389f2fd0dfSRameshwar Prasad Sahu 
7399f2fd0dfSRameshwar Prasad Sahu 		/* Notify the hw about this completed descriptor */
7409f2fd0dfSRameshwar Prasad Sahu 		iowrite32(-1, ring->cmd);
7419f2fd0dfSRameshwar Prasad Sahu 
7429f2fd0dfSRameshwar Prasad Sahu 		/* Mark this hw descriptor as processed */
7436d0767c1SRameshwar Prasad Sahu 		desc_hw->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
7449f2fd0dfSRameshwar Prasad Sahu 
7459f2fd0dfSRameshwar Prasad Sahu 		/*
7469f2fd0dfSRameshwar Prasad Sahu 		 * Decrement the pending transaction count
7479f2fd0dfSRameshwar Prasad Sahu 		 * as we have processed one
7489f2fd0dfSRameshwar Prasad Sahu 		 */
749ee08b59dSRameshwar Prasad Sahu 		chan->pending -= ((desc_sw->flags &
750ee08b59dSRameshwar Prasad Sahu 				  XGENE_DMA_FLAG_64B_DESC) ? 2 : 1);
751005ce70bSRameshwar Prasad Sahu 
752005ce70bSRameshwar Prasad Sahu 		/*
753005ce70bSRameshwar Prasad Sahu 		 * Delete this node from ld running queue and append it to
754005ce70bSRameshwar Prasad Sahu 		 * ld completed queue for further processing
755005ce70bSRameshwar Prasad Sahu 		 */
756005ce70bSRameshwar Prasad Sahu 		list_move_tail(&desc_sw->node, &ld_completed);
7579f2fd0dfSRameshwar Prasad Sahu 	}
7589f2fd0dfSRameshwar Prasad Sahu 
7599f2fd0dfSRameshwar Prasad Sahu 	/*
7609f2fd0dfSRameshwar Prasad Sahu 	 * Start any pending transactions automatically
7619f2fd0dfSRameshwar Prasad Sahu 	 * In the ideal case, we keep the DMA controller busy while we go
7629f2fd0dfSRameshwar Prasad Sahu 	 * ahead and free the descriptors below.
7639f2fd0dfSRameshwar Prasad Sahu 	 */
7649f2fd0dfSRameshwar Prasad Sahu 	xgene_chan_xfer_ld_pending(chan);
765005ce70bSRameshwar Prasad Sahu 
766f177a431SJeff Xie 	spin_unlock(&chan->lock);
767005ce70bSRameshwar Prasad Sahu 
768005ce70bSRameshwar Prasad Sahu 	/* Run the callback for each descriptor, in order */
769005ce70bSRameshwar Prasad Sahu 	list_for_each_entry_safe(desc_sw, _desc_sw, &ld_completed, node) {
770005ce70bSRameshwar Prasad Sahu 		xgene_dma_run_tx_complete_actions(chan, desc_sw);
771005ce70bSRameshwar Prasad Sahu 		xgene_dma_clean_running_descriptor(chan, desc_sw);
772005ce70bSRameshwar Prasad Sahu 	}
7739f2fd0dfSRameshwar Prasad Sahu }
7749f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_alloc_chan_resources(struct dma_chan * dchan)7759f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_alloc_chan_resources(struct dma_chan *dchan)
7769f2fd0dfSRameshwar Prasad Sahu {
7779f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan = to_dma_chan(dchan);
7789f2fd0dfSRameshwar Prasad Sahu 
7799f2fd0dfSRameshwar Prasad Sahu 	/* Has this channel already been allocated? */
7809f2fd0dfSRameshwar Prasad Sahu 	if (chan->desc_pool)
7819f2fd0dfSRameshwar Prasad Sahu 		return 1;
7829f2fd0dfSRameshwar Prasad Sahu 
7839f2fd0dfSRameshwar Prasad Sahu 	chan->desc_pool = dma_pool_create(chan->name, chan->dev,
7849f2fd0dfSRameshwar Prasad Sahu 					  sizeof(struct xgene_dma_desc_sw),
7859f2fd0dfSRameshwar Prasad Sahu 					  0, 0);
7869f2fd0dfSRameshwar Prasad Sahu 	if (!chan->desc_pool) {
7879f2fd0dfSRameshwar Prasad Sahu 		chan_err(chan, "Failed to allocate descriptor pool\n");
7889f2fd0dfSRameshwar Prasad Sahu 		return -ENOMEM;
7899f2fd0dfSRameshwar Prasad Sahu 	}
7909f2fd0dfSRameshwar Prasad Sahu 
7919e1630b8SColin Ian King 	chan_dbg(chan, "Allocate descriptor pool\n");
7929f2fd0dfSRameshwar Prasad Sahu 
7939f2fd0dfSRameshwar Prasad Sahu 	return 1;
7949f2fd0dfSRameshwar Prasad Sahu }
7959f2fd0dfSRameshwar Prasad Sahu 
7969f2fd0dfSRameshwar Prasad Sahu /**
7979f2fd0dfSRameshwar Prasad Sahu  * xgene_dma_free_desc_list - Free all descriptors in a queue
7989f2fd0dfSRameshwar Prasad Sahu  * @chan: X-Gene DMA channel
7999f2fd0dfSRameshwar Prasad Sahu  * @list: the list to free
8009f2fd0dfSRameshwar Prasad Sahu  *
8016d0767c1SRameshwar Prasad Sahu  * LOCKING: must hold chan->lock
8029f2fd0dfSRameshwar Prasad Sahu  */
xgene_dma_free_desc_list(struct xgene_dma_chan * chan,struct list_head * list)8039f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_free_desc_list(struct xgene_dma_chan *chan,
8049f2fd0dfSRameshwar Prasad Sahu 				     struct list_head *list)
8059f2fd0dfSRameshwar Prasad Sahu {
8069f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_sw *desc, *_desc;
8079f2fd0dfSRameshwar Prasad Sahu 
8089f2fd0dfSRameshwar Prasad Sahu 	list_for_each_entry_safe(desc, _desc, list, node)
8099f2fd0dfSRameshwar Prasad Sahu 		xgene_dma_clean_descriptor(chan, desc);
8109f2fd0dfSRameshwar Prasad Sahu }
8119f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_free_chan_resources(struct dma_chan * dchan)8129f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_free_chan_resources(struct dma_chan *dchan)
8139f2fd0dfSRameshwar Prasad Sahu {
8149f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan = to_dma_chan(dchan);
8159f2fd0dfSRameshwar Prasad Sahu 
8169f2fd0dfSRameshwar Prasad Sahu 	chan_dbg(chan, "Free all resources\n");
8179f2fd0dfSRameshwar Prasad Sahu 
8189f2fd0dfSRameshwar Prasad Sahu 	if (!chan->desc_pool)
8199f2fd0dfSRameshwar Prasad Sahu 		return;
8209f2fd0dfSRameshwar Prasad Sahu 
8219f2fd0dfSRameshwar Prasad Sahu 	/* Process all running descriptor */
8229f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_cleanup_descriptors(chan);
8239f2fd0dfSRameshwar Prasad Sahu 
824005ce70bSRameshwar Prasad Sahu 	spin_lock_bh(&chan->lock);
825005ce70bSRameshwar Prasad Sahu 
8269f2fd0dfSRameshwar Prasad Sahu 	/* Clean all link descriptor queues */
8279f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_free_desc_list(chan, &chan->ld_pending);
8289f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_free_desc_list(chan, &chan->ld_running);
8299f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_free_desc_list(chan, &chan->ld_completed);
8309f2fd0dfSRameshwar Prasad Sahu 
8319f2fd0dfSRameshwar Prasad Sahu 	spin_unlock_bh(&chan->lock);
8329f2fd0dfSRameshwar Prasad Sahu 
8339f2fd0dfSRameshwar Prasad Sahu 	/* Delete this channel DMA pool */
8349f2fd0dfSRameshwar Prasad Sahu 	dma_pool_destroy(chan->desc_pool);
8359f2fd0dfSRameshwar Prasad Sahu 	chan->desc_pool = NULL;
8369f2fd0dfSRameshwar Prasad Sahu }
8379f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_prep_xor(struct dma_chan * dchan,dma_addr_t dst,dma_addr_t * src,u32 src_cnt,size_t len,unsigned long flags)8389f2fd0dfSRameshwar Prasad Sahu static struct dma_async_tx_descriptor *xgene_dma_prep_xor(
8399f2fd0dfSRameshwar Prasad Sahu 	struct dma_chan *dchan, dma_addr_t dst,	dma_addr_t *src,
8409f2fd0dfSRameshwar Prasad Sahu 	u32 src_cnt, size_t len, unsigned long flags)
8419f2fd0dfSRameshwar Prasad Sahu {
8429f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_sw *first = NULL, *new;
8439f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan;
8449f2fd0dfSRameshwar Prasad Sahu 	static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {
8459f2fd0dfSRameshwar Prasad Sahu 				0x01, 0x01, 0x01, 0x01, 0x01};
8469f2fd0dfSRameshwar Prasad Sahu 
8479f2fd0dfSRameshwar Prasad Sahu 	if (unlikely(!dchan || !len))
8489f2fd0dfSRameshwar Prasad Sahu 		return NULL;
8499f2fd0dfSRameshwar Prasad Sahu 
8509f2fd0dfSRameshwar Prasad Sahu 	chan = to_dma_chan(dchan);
8519f2fd0dfSRameshwar Prasad Sahu 
8529f2fd0dfSRameshwar Prasad Sahu 	do {
8539f2fd0dfSRameshwar Prasad Sahu 		/* Allocate the link descriptor from DMA pool */
8549f2fd0dfSRameshwar Prasad Sahu 		new = xgene_dma_alloc_descriptor(chan);
8559f2fd0dfSRameshwar Prasad Sahu 		if (!new)
8569f2fd0dfSRameshwar Prasad Sahu 			goto fail;
8579f2fd0dfSRameshwar Prasad Sahu 
8589f2fd0dfSRameshwar Prasad Sahu 		/* Prepare xor DMA descriptor */
8599f2fd0dfSRameshwar Prasad Sahu 		xgene_dma_prep_xor_desc(chan, new, &dst, src,
8609f2fd0dfSRameshwar Prasad Sahu 					src_cnt, &len, multi);
8619f2fd0dfSRameshwar Prasad Sahu 
8629f2fd0dfSRameshwar Prasad Sahu 		if (!first)
8639f2fd0dfSRameshwar Prasad Sahu 			first = new;
8649f2fd0dfSRameshwar Prasad Sahu 
8659f2fd0dfSRameshwar Prasad Sahu 		new->tx.cookie = 0;
8669f2fd0dfSRameshwar Prasad Sahu 		async_tx_ack(&new->tx);
8679f2fd0dfSRameshwar Prasad Sahu 
8689f2fd0dfSRameshwar Prasad Sahu 		/* Insert the link descriptor to the LD ring */
8699f2fd0dfSRameshwar Prasad Sahu 		list_add_tail(&new->node, &first->tx_list);
8709f2fd0dfSRameshwar Prasad Sahu 	} while (len);
8719f2fd0dfSRameshwar Prasad Sahu 
8729f2fd0dfSRameshwar Prasad Sahu 	new->tx.flags = flags; /* client is in control of this ack */
8739f2fd0dfSRameshwar Prasad Sahu 	new->tx.cookie = -EBUSY;
8749f2fd0dfSRameshwar Prasad Sahu 	list_splice(&first->tx_list, &new->tx_list);
8759f2fd0dfSRameshwar Prasad Sahu 
8769f2fd0dfSRameshwar Prasad Sahu 	return &new->tx;
8779f2fd0dfSRameshwar Prasad Sahu 
8789f2fd0dfSRameshwar Prasad Sahu fail:
8799f2fd0dfSRameshwar Prasad Sahu 	if (!first)
8809f2fd0dfSRameshwar Prasad Sahu 		return NULL;
8819f2fd0dfSRameshwar Prasad Sahu 
8826d0767c1SRameshwar Prasad Sahu 	xgene_dma_free_desc_list(chan, &first->tx_list);
8839f2fd0dfSRameshwar Prasad Sahu 	return NULL;
8849f2fd0dfSRameshwar Prasad Sahu }
8859f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_prep_pq(struct dma_chan * dchan,dma_addr_t * dst,dma_addr_t * src,u32 src_cnt,const u8 * scf,size_t len,unsigned long flags)8869f2fd0dfSRameshwar Prasad Sahu static struct dma_async_tx_descriptor *xgene_dma_prep_pq(
8879f2fd0dfSRameshwar Prasad Sahu 	struct dma_chan *dchan, dma_addr_t *dst, dma_addr_t *src,
8889f2fd0dfSRameshwar Prasad Sahu 	u32 src_cnt, const u8 *scf, size_t len, unsigned long flags)
8899f2fd0dfSRameshwar Prasad Sahu {
8909f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_desc_sw *first = NULL, *new;
8919f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan;
8929f2fd0dfSRameshwar Prasad Sahu 	size_t _len = len;
8939f2fd0dfSRameshwar Prasad Sahu 	dma_addr_t _src[XGENE_DMA_MAX_XOR_SRC];
8949f2fd0dfSRameshwar Prasad Sahu 	static u8 multi[XGENE_DMA_MAX_XOR_SRC] = {0x01, 0x01, 0x01, 0x01, 0x01};
8959f2fd0dfSRameshwar Prasad Sahu 
8969f2fd0dfSRameshwar Prasad Sahu 	if (unlikely(!dchan || !len))
8979f2fd0dfSRameshwar Prasad Sahu 		return NULL;
8989f2fd0dfSRameshwar Prasad Sahu 
8999f2fd0dfSRameshwar Prasad Sahu 	chan = to_dma_chan(dchan);
9009f2fd0dfSRameshwar Prasad Sahu 
9019f2fd0dfSRameshwar Prasad Sahu 	/*
9029f2fd0dfSRameshwar Prasad Sahu 	 * Save source addresses on local variable, may be we have to
9039f2fd0dfSRameshwar Prasad Sahu 	 * prepare two descriptor to generate P and Q if both enabled
9049f2fd0dfSRameshwar Prasad Sahu 	 * in the flags by client
9059f2fd0dfSRameshwar Prasad Sahu 	 */
9069f2fd0dfSRameshwar Prasad Sahu 	memcpy(_src, src, sizeof(*src) * src_cnt);
9079f2fd0dfSRameshwar Prasad Sahu 
9089f2fd0dfSRameshwar Prasad Sahu 	if (flags & DMA_PREP_PQ_DISABLE_P)
9099f2fd0dfSRameshwar Prasad Sahu 		len = 0;
9109f2fd0dfSRameshwar Prasad Sahu 
9119f2fd0dfSRameshwar Prasad Sahu 	if (flags & DMA_PREP_PQ_DISABLE_Q)
9129f2fd0dfSRameshwar Prasad Sahu 		_len = 0;
9139f2fd0dfSRameshwar Prasad Sahu 
9149f2fd0dfSRameshwar Prasad Sahu 	do {
9159f2fd0dfSRameshwar Prasad Sahu 		/* Allocate the link descriptor from DMA pool */
9169f2fd0dfSRameshwar Prasad Sahu 		new = xgene_dma_alloc_descriptor(chan);
9179f2fd0dfSRameshwar Prasad Sahu 		if (!new)
9189f2fd0dfSRameshwar Prasad Sahu 			goto fail;
9199f2fd0dfSRameshwar Prasad Sahu 
9209f2fd0dfSRameshwar Prasad Sahu 		if (!first)
9219f2fd0dfSRameshwar Prasad Sahu 			first = new;
9229f2fd0dfSRameshwar Prasad Sahu 
9239f2fd0dfSRameshwar Prasad Sahu 		new->tx.cookie = 0;
9249f2fd0dfSRameshwar Prasad Sahu 		async_tx_ack(&new->tx);
9259f2fd0dfSRameshwar Prasad Sahu 
9269f2fd0dfSRameshwar Prasad Sahu 		/* Insert the link descriptor to the LD ring */
9279f2fd0dfSRameshwar Prasad Sahu 		list_add_tail(&new->node, &first->tx_list);
9289f2fd0dfSRameshwar Prasad Sahu 
9299f2fd0dfSRameshwar Prasad Sahu 		/*
9309f2fd0dfSRameshwar Prasad Sahu 		 * Prepare DMA descriptor to generate P,
9319f2fd0dfSRameshwar Prasad Sahu 		 * if DMA_PREP_PQ_DISABLE_P flag is not set
9329f2fd0dfSRameshwar Prasad Sahu 		 */
9339f2fd0dfSRameshwar Prasad Sahu 		if (len) {
9349f2fd0dfSRameshwar Prasad Sahu 			xgene_dma_prep_xor_desc(chan, new, &dst[0], src,
9359f2fd0dfSRameshwar Prasad Sahu 						src_cnt, &len, multi);
9369f2fd0dfSRameshwar Prasad Sahu 			continue;
9379f2fd0dfSRameshwar Prasad Sahu 		}
9389f2fd0dfSRameshwar Prasad Sahu 
9399f2fd0dfSRameshwar Prasad Sahu 		/*
9409f2fd0dfSRameshwar Prasad Sahu 		 * Prepare DMA descriptor to generate Q,
9419f2fd0dfSRameshwar Prasad Sahu 		 * if DMA_PREP_PQ_DISABLE_Q flag is not set
9429f2fd0dfSRameshwar Prasad Sahu 		 */
9439f2fd0dfSRameshwar Prasad Sahu 		if (_len) {
9449f2fd0dfSRameshwar Prasad Sahu 			xgene_dma_prep_xor_desc(chan, new, &dst[1], _src,
9459f2fd0dfSRameshwar Prasad Sahu 						src_cnt, &_len, scf);
9469f2fd0dfSRameshwar Prasad Sahu 		}
9479f2fd0dfSRameshwar Prasad Sahu 	} while (len || _len);
9489f2fd0dfSRameshwar Prasad Sahu 
9499f2fd0dfSRameshwar Prasad Sahu 	new->tx.flags = flags; /* client is in control of this ack */
9509f2fd0dfSRameshwar Prasad Sahu 	new->tx.cookie = -EBUSY;
9519f2fd0dfSRameshwar Prasad Sahu 	list_splice(&first->tx_list, &new->tx_list);
9529f2fd0dfSRameshwar Prasad Sahu 
9539f2fd0dfSRameshwar Prasad Sahu 	return &new->tx;
9549f2fd0dfSRameshwar Prasad Sahu 
9559f2fd0dfSRameshwar Prasad Sahu fail:
9569f2fd0dfSRameshwar Prasad Sahu 	if (!first)
9579f2fd0dfSRameshwar Prasad Sahu 		return NULL;
9589f2fd0dfSRameshwar Prasad Sahu 
9596d0767c1SRameshwar Prasad Sahu 	xgene_dma_free_desc_list(chan, &first->tx_list);
9609f2fd0dfSRameshwar Prasad Sahu 	return NULL;
9619f2fd0dfSRameshwar Prasad Sahu }
9629f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_issue_pending(struct dma_chan * dchan)9639f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_issue_pending(struct dma_chan *dchan)
9649f2fd0dfSRameshwar Prasad Sahu {
9659f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan = to_dma_chan(dchan);
9669f2fd0dfSRameshwar Prasad Sahu 
9679f2fd0dfSRameshwar Prasad Sahu 	spin_lock_bh(&chan->lock);
9689f2fd0dfSRameshwar Prasad Sahu 	xgene_chan_xfer_ld_pending(chan);
9699f2fd0dfSRameshwar Prasad Sahu 	spin_unlock_bh(&chan->lock);
9709f2fd0dfSRameshwar Prasad Sahu }
9719f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_tx_status(struct dma_chan * dchan,dma_cookie_t cookie,struct dma_tx_state * txstate)9729f2fd0dfSRameshwar Prasad Sahu static enum dma_status xgene_dma_tx_status(struct dma_chan *dchan,
9739f2fd0dfSRameshwar Prasad Sahu 					   dma_cookie_t cookie,
9749f2fd0dfSRameshwar Prasad Sahu 					   struct dma_tx_state *txstate)
9759f2fd0dfSRameshwar Prasad Sahu {
9769f2fd0dfSRameshwar Prasad Sahu 	return dma_cookie_status(dchan, cookie, txstate);
9779f2fd0dfSRameshwar Prasad Sahu }
9789f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_tasklet_cb(struct tasklet_struct * t)9790e71d9b9SAllen Pais static void xgene_dma_tasklet_cb(struct tasklet_struct *t)
9809f2fd0dfSRameshwar Prasad Sahu {
9810e71d9b9SAllen Pais 	struct xgene_dma_chan *chan = from_tasklet(chan, t, tasklet);
9829f2fd0dfSRameshwar Prasad Sahu 
9839f2fd0dfSRameshwar Prasad Sahu 	/* Run all cleanup for descriptors which have been completed */
9849f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_cleanup_descriptors(chan);
9859f2fd0dfSRameshwar Prasad Sahu 
9869f2fd0dfSRameshwar Prasad Sahu 	/* Re-enable DMA channel IRQ */
9879f2fd0dfSRameshwar Prasad Sahu 	enable_irq(chan->rx_irq);
9889f2fd0dfSRameshwar Prasad Sahu }
9899f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_chan_ring_isr(int irq,void * id)9909f2fd0dfSRameshwar Prasad Sahu static irqreturn_t xgene_dma_chan_ring_isr(int irq, void *id)
9919f2fd0dfSRameshwar Prasad Sahu {
9929f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan = (struct xgene_dma_chan *)id;
9939f2fd0dfSRameshwar Prasad Sahu 
9949f2fd0dfSRameshwar Prasad Sahu 	BUG_ON(!chan);
9959f2fd0dfSRameshwar Prasad Sahu 
9969f2fd0dfSRameshwar Prasad Sahu 	/*
9979f2fd0dfSRameshwar Prasad Sahu 	 * Disable DMA channel IRQ until we process completed
9989f2fd0dfSRameshwar Prasad Sahu 	 * descriptors
9999f2fd0dfSRameshwar Prasad Sahu 	 */
10009f2fd0dfSRameshwar Prasad Sahu 	disable_irq_nosync(chan->rx_irq);
10019f2fd0dfSRameshwar Prasad Sahu 
10029f2fd0dfSRameshwar Prasad Sahu 	/*
10039f2fd0dfSRameshwar Prasad Sahu 	 * Schedule the tasklet to handle all cleanup of the current
10049f2fd0dfSRameshwar Prasad Sahu 	 * transaction. It will start a new transaction if there is
10059f2fd0dfSRameshwar Prasad Sahu 	 * one pending.
10069f2fd0dfSRameshwar Prasad Sahu 	 */
10079f2fd0dfSRameshwar Prasad Sahu 	tasklet_schedule(&chan->tasklet);
10089f2fd0dfSRameshwar Prasad Sahu 
10099f2fd0dfSRameshwar Prasad Sahu 	return IRQ_HANDLED;
10109f2fd0dfSRameshwar Prasad Sahu }
10119f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_err_isr(int irq,void * id)10129f2fd0dfSRameshwar Prasad Sahu static irqreturn_t xgene_dma_err_isr(int irq, void *id)
10139f2fd0dfSRameshwar Prasad Sahu {
10149f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma *pdma = (struct xgene_dma *)id;
10159f2fd0dfSRameshwar Prasad Sahu 	unsigned long int_mask;
10169f2fd0dfSRameshwar Prasad Sahu 	u32 val, i;
10179f2fd0dfSRameshwar Prasad Sahu 
10189f2fd0dfSRameshwar Prasad Sahu 	val = ioread32(pdma->csr_dma + XGENE_DMA_INT);
10199f2fd0dfSRameshwar Prasad Sahu 
10209f2fd0dfSRameshwar Prasad Sahu 	/* Clear DMA interrupts */
10219f2fd0dfSRameshwar Prasad Sahu 	iowrite32(val, pdma->csr_dma + XGENE_DMA_INT);
10229f2fd0dfSRameshwar Prasad Sahu 
10239f2fd0dfSRameshwar Prasad Sahu 	/* Print DMA error info */
10249f2fd0dfSRameshwar Prasad Sahu 	int_mask = val >> XGENE_DMA_INT_MASK_SHIFT;
10259f2fd0dfSRameshwar Prasad Sahu 	for_each_set_bit(i, &int_mask, ARRAY_SIZE(xgene_dma_err))
10269f2fd0dfSRameshwar Prasad Sahu 		dev_err(pdma->dev,
10279f2fd0dfSRameshwar Prasad Sahu 			"Interrupt status 0x%08X %s\n", val, xgene_dma_err[i]);
10289f2fd0dfSRameshwar Prasad Sahu 
10299f2fd0dfSRameshwar Prasad Sahu 	return IRQ_HANDLED;
10309f2fd0dfSRameshwar Prasad Sahu }
10319f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_wr_ring_state(struct xgene_dma_ring * ring)10329f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_wr_ring_state(struct xgene_dma_ring *ring)
10339f2fd0dfSRameshwar Prasad Sahu {
10349f2fd0dfSRameshwar Prasad Sahu 	int i;
10359f2fd0dfSRameshwar Prasad Sahu 
10369f2fd0dfSRameshwar Prasad Sahu 	iowrite32(ring->num, ring->pdma->csr_ring + XGENE_DMA_RING_STATE);
10379f2fd0dfSRameshwar Prasad Sahu 
10389f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < XGENE_DMA_RING_NUM_CONFIG; i++)
10399f2fd0dfSRameshwar Prasad Sahu 		iowrite32(ring->state[i], ring->pdma->csr_ring +
10409f2fd0dfSRameshwar Prasad Sahu 			  XGENE_DMA_RING_STATE_WR_BASE + (i * 4));
10419f2fd0dfSRameshwar Prasad Sahu }
10429f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_clr_ring_state(struct xgene_dma_ring * ring)10439f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_clr_ring_state(struct xgene_dma_ring *ring)
10449f2fd0dfSRameshwar Prasad Sahu {
10459f2fd0dfSRameshwar Prasad Sahu 	memset(ring->state, 0, sizeof(u32) * XGENE_DMA_RING_NUM_CONFIG);
10469f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_wr_ring_state(ring);
10479f2fd0dfSRameshwar Prasad Sahu }
10489f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_setup_ring(struct xgene_dma_ring * ring)10499f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_setup_ring(struct xgene_dma_ring *ring)
10509f2fd0dfSRameshwar Prasad Sahu {
10519f2fd0dfSRameshwar Prasad Sahu 	void *ring_cfg = ring->state;
10529f2fd0dfSRameshwar Prasad Sahu 	u64 addr = ring->desc_paddr;
10539f2fd0dfSRameshwar Prasad Sahu 	u32 i, val;
10549f2fd0dfSRameshwar Prasad Sahu 
10559f2fd0dfSRameshwar Prasad Sahu 	ring->slots = ring->size / XGENE_DMA_RING_WQ_DESC_SIZE;
10569f2fd0dfSRameshwar Prasad Sahu 
10579f2fd0dfSRameshwar Prasad Sahu 	/* Clear DMA ring state */
10589f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_clr_ring_state(ring);
10599f2fd0dfSRameshwar Prasad Sahu 
10609f2fd0dfSRameshwar Prasad Sahu 	/* Set DMA ring type */
10619f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_TYPE_SET(ring_cfg, XGENE_DMA_RING_TYPE_REGULAR);
10629f2fd0dfSRameshwar Prasad Sahu 
10639f2fd0dfSRameshwar Prasad Sahu 	if (ring->owner == XGENE_DMA_RING_OWNER_DMA) {
10649f2fd0dfSRameshwar Prasad Sahu 		/* Set recombination buffer and timeout */
10659f2fd0dfSRameshwar Prasad Sahu 		XGENE_DMA_RING_RECOMBBUF_SET(ring_cfg);
10669f2fd0dfSRameshwar Prasad Sahu 		XGENE_DMA_RING_RECOMTIMEOUTL_SET(ring_cfg);
10679f2fd0dfSRameshwar Prasad Sahu 		XGENE_DMA_RING_RECOMTIMEOUTH_SET(ring_cfg);
10689f2fd0dfSRameshwar Prasad Sahu 	}
10699f2fd0dfSRameshwar Prasad Sahu 
10709f2fd0dfSRameshwar Prasad Sahu 	/* Initialize DMA ring state */
10719f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_SELTHRSH_SET(ring_cfg);
10729f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_ACCEPTLERR_SET(ring_cfg);
10739f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_COHERENT_SET(ring_cfg);
10749f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_ADDRL_SET(ring_cfg, addr);
10759f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_ADDRH_SET(ring_cfg, addr);
10769f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_SIZE_SET(ring_cfg, ring->cfgsize);
10779f2fd0dfSRameshwar Prasad Sahu 
10789f2fd0dfSRameshwar Prasad Sahu 	/* Write DMA ring configurations */
10799f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_wr_ring_state(ring);
10809f2fd0dfSRameshwar Prasad Sahu 
10819f2fd0dfSRameshwar Prasad Sahu 	/* Set DMA ring id */
10829f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_RING_ID_SETUP(ring->id),
10839f2fd0dfSRameshwar Prasad Sahu 		  ring->pdma->csr_ring + XGENE_DMA_RING_ID);
10849f2fd0dfSRameshwar Prasad Sahu 
10859f2fd0dfSRameshwar Prasad Sahu 	/* Set DMA ring buffer */
10869f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_RING_ID_BUF_SETUP(ring->num),
10879f2fd0dfSRameshwar Prasad Sahu 		  ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
10889f2fd0dfSRameshwar Prasad Sahu 
10899f2fd0dfSRameshwar Prasad Sahu 	if (ring->owner != XGENE_DMA_RING_OWNER_CPU)
10909f2fd0dfSRameshwar Prasad Sahu 		return;
10919f2fd0dfSRameshwar Prasad Sahu 
10929f2fd0dfSRameshwar Prasad Sahu 	/* Set empty signature to DMA Rx ring descriptors */
10939f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < ring->slots; i++) {
10946d0767c1SRameshwar Prasad Sahu 		struct xgene_dma_desc_hw *desc;
10956d0767c1SRameshwar Prasad Sahu 
10969f2fd0dfSRameshwar Prasad Sahu 		desc = &ring->desc_hw[i];
10976d0767c1SRameshwar Prasad Sahu 		desc->m0 = cpu_to_le64(XGENE_DMA_DESC_EMPTY_SIGNATURE);
10989f2fd0dfSRameshwar Prasad Sahu 	}
10999f2fd0dfSRameshwar Prasad Sahu 
11009f2fd0dfSRameshwar Prasad Sahu 	/* Enable DMA Rx ring interrupt */
11019f2fd0dfSRameshwar Prasad Sahu 	val = ioread32(ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
11029f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_RING_NE_INT_MODE_SET(val, ring->buf_num);
11039f2fd0dfSRameshwar Prasad Sahu 	iowrite32(val, ring->pdma->csr_ring + XGENE_DMA_RING_NE_INT_MODE);
11049f2fd0dfSRameshwar Prasad Sahu }
11059f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_clear_ring(struct xgene_dma_ring * ring)11069f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_clear_ring(struct xgene_dma_ring *ring)
11079f2fd0dfSRameshwar Prasad Sahu {
11089f2fd0dfSRameshwar Prasad Sahu 	u32 ring_id, val;
11099f2fd0dfSRameshwar Prasad Sahu 
11109f2fd0dfSRameshwar Prasad Sahu 	if (ring->owner == XGENE_DMA_RING_OWNER_CPU) {
11119f2fd0dfSRameshwar Prasad Sahu 		/* Disable DMA Rx ring interrupt */
11129f2fd0dfSRameshwar Prasad Sahu 		val = ioread32(ring->pdma->csr_ring +
11139f2fd0dfSRameshwar Prasad Sahu 			       XGENE_DMA_RING_NE_INT_MODE);
11149f2fd0dfSRameshwar Prasad Sahu 		XGENE_DMA_RING_NE_INT_MODE_RESET(val, ring->buf_num);
11159f2fd0dfSRameshwar Prasad Sahu 		iowrite32(val, ring->pdma->csr_ring +
11169f2fd0dfSRameshwar Prasad Sahu 			  XGENE_DMA_RING_NE_INT_MODE);
11179f2fd0dfSRameshwar Prasad Sahu 	}
11189f2fd0dfSRameshwar Prasad Sahu 
11199f2fd0dfSRameshwar Prasad Sahu 	/* Clear DMA ring state */
11209f2fd0dfSRameshwar Prasad Sahu 	ring_id = XGENE_DMA_RING_ID_SETUP(ring->id);
11219f2fd0dfSRameshwar Prasad Sahu 	iowrite32(ring_id, ring->pdma->csr_ring + XGENE_DMA_RING_ID);
11229f2fd0dfSRameshwar Prasad Sahu 
11239f2fd0dfSRameshwar Prasad Sahu 	iowrite32(0, ring->pdma->csr_ring + XGENE_DMA_RING_ID_BUF);
11249f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_clr_ring_state(ring);
11259f2fd0dfSRameshwar Prasad Sahu }
11269f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_set_ring_cmd(struct xgene_dma_ring * ring)11279f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_set_ring_cmd(struct xgene_dma_ring *ring)
11289f2fd0dfSRameshwar Prasad Sahu {
11299f2fd0dfSRameshwar Prasad Sahu 	ring->cmd_base = ring->pdma->csr_ring_cmd +
11309f2fd0dfSRameshwar Prasad Sahu 				XGENE_DMA_RING_CMD_BASE_OFFSET((ring->num -
11319f2fd0dfSRameshwar Prasad Sahu 							  XGENE_DMA_RING_NUM));
11329f2fd0dfSRameshwar Prasad Sahu 
11339f2fd0dfSRameshwar Prasad Sahu 	ring->cmd = ring->cmd_base + XGENE_DMA_RING_CMD_OFFSET;
11349f2fd0dfSRameshwar Prasad Sahu }
11359f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_get_ring_size(struct xgene_dma_chan * chan,enum xgene_dma_ring_cfgsize cfgsize)11369f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_get_ring_size(struct xgene_dma_chan *chan,
11379f2fd0dfSRameshwar Prasad Sahu 				   enum xgene_dma_ring_cfgsize cfgsize)
11389f2fd0dfSRameshwar Prasad Sahu {
11399f2fd0dfSRameshwar Prasad Sahu 	int size;
11409f2fd0dfSRameshwar Prasad Sahu 
11419f2fd0dfSRameshwar Prasad Sahu 	switch (cfgsize) {
11429f2fd0dfSRameshwar Prasad Sahu 	case XGENE_DMA_RING_CFG_SIZE_512B:
11439f2fd0dfSRameshwar Prasad Sahu 		size = 0x200;
11449f2fd0dfSRameshwar Prasad Sahu 		break;
11459f2fd0dfSRameshwar Prasad Sahu 	case XGENE_DMA_RING_CFG_SIZE_2KB:
11469f2fd0dfSRameshwar Prasad Sahu 		size = 0x800;
11479f2fd0dfSRameshwar Prasad Sahu 		break;
11489f2fd0dfSRameshwar Prasad Sahu 	case XGENE_DMA_RING_CFG_SIZE_16KB:
11499f2fd0dfSRameshwar Prasad Sahu 		size = 0x4000;
11509f2fd0dfSRameshwar Prasad Sahu 		break;
11519f2fd0dfSRameshwar Prasad Sahu 	case XGENE_DMA_RING_CFG_SIZE_64KB:
11529f2fd0dfSRameshwar Prasad Sahu 		size = 0x10000;
11539f2fd0dfSRameshwar Prasad Sahu 		break;
11549f2fd0dfSRameshwar Prasad Sahu 	case XGENE_DMA_RING_CFG_SIZE_512KB:
11559f2fd0dfSRameshwar Prasad Sahu 		size = 0x80000;
11569f2fd0dfSRameshwar Prasad Sahu 		break;
11579f2fd0dfSRameshwar Prasad Sahu 	default:
11589f2fd0dfSRameshwar Prasad Sahu 		chan_err(chan, "Unsupported cfg ring size %d\n", cfgsize);
11599f2fd0dfSRameshwar Prasad Sahu 		return -EINVAL;
11609f2fd0dfSRameshwar Prasad Sahu 	}
11619f2fd0dfSRameshwar Prasad Sahu 
11629f2fd0dfSRameshwar Prasad Sahu 	return size;
11639f2fd0dfSRameshwar Prasad Sahu }
11649f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_delete_ring_one(struct xgene_dma_ring * ring)11659f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_delete_ring_one(struct xgene_dma_ring *ring)
11669f2fd0dfSRameshwar Prasad Sahu {
11679f2fd0dfSRameshwar Prasad Sahu 	/* Clear DMA ring configurations */
11689f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_clear_ring(ring);
11699f2fd0dfSRameshwar Prasad Sahu 
11709f2fd0dfSRameshwar Prasad Sahu 	/* De-allocate DMA ring descriptor */
11719f2fd0dfSRameshwar Prasad Sahu 	if (ring->desc_vaddr) {
11729f2fd0dfSRameshwar Prasad Sahu 		dma_free_coherent(ring->pdma->dev, ring->size,
11739f2fd0dfSRameshwar Prasad Sahu 				  ring->desc_vaddr, ring->desc_paddr);
11749f2fd0dfSRameshwar Prasad Sahu 		ring->desc_vaddr = NULL;
11759f2fd0dfSRameshwar Prasad Sahu 	}
11769f2fd0dfSRameshwar Prasad Sahu }
11779f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_delete_chan_rings(struct xgene_dma_chan * chan)11789f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_delete_chan_rings(struct xgene_dma_chan *chan)
11799f2fd0dfSRameshwar Prasad Sahu {
11809f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_delete_ring_one(&chan->rx_ring);
11819f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_delete_ring_one(&chan->tx_ring);
11829f2fd0dfSRameshwar Prasad Sahu }
11839f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_create_ring_one(struct xgene_dma_chan * chan,struct xgene_dma_ring * ring,enum xgene_dma_ring_cfgsize cfgsize)11849f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_create_ring_one(struct xgene_dma_chan *chan,
11859f2fd0dfSRameshwar Prasad Sahu 				     struct xgene_dma_ring *ring,
11869f2fd0dfSRameshwar Prasad Sahu 				     enum xgene_dma_ring_cfgsize cfgsize)
11879f2fd0dfSRameshwar Prasad Sahu {
1188c1492b4cSAndrzej Hajda 	int ret;
1189c1492b4cSAndrzej Hajda 
11909f2fd0dfSRameshwar Prasad Sahu 	/* Setup DMA ring descriptor variables */
11919f2fd0dfSRameshwar Prasad Sahu 	ring->pdma = chan->pdma;
11929f2fd0dfSRameshwar Prasad Sahu 	ring->cfgsize = cfgsize;
11939f2fd0dfSRameshwar Prasad Sahu 	ring->num = chan->pdma->ring_num++;
11949f2fd0dfSRameshwar Prasad Sahu 	ring->id = XGENE_DMA_RING_ID_GET(ring->owner, ring->buf_num);
11959f2fd0dfSRameshwar Prasad Sahu 
1196c1492b4cSAndrzej Hajda 	ret = xgene_dma_get_ring_size(chan, cfgsize);
1197c1492b4cSAndrzej Hajda 	if (ret <= 0)
1198c1492b4cSAndrzej Hajda 		return ret;
1199c1492b4cSAndrzej Hajda 	ring->size = ret;
12009f2fd0dfSRameshwar Prasad Sahu 
12019f2fd0dfSRameshwar Prasad Sahu 	/* Allocate memory for DMA ring descriptor */
1202750afb08SLuis Chamberlain 	ring->desc_vaddr = dma_alloc_coherent(chan->dev, ring->size,
12039f2fd0dfSRameshwar Prasad Sahu 					      &ring->desc_paddr, GFP_KERNEL);
12049f2fd0dfSRameshwar Prasad Sahu 	if (!ring->desc_vaddr) {
12059f2fd0dfSRameshwar Prasad Sahu 		chan_err(chan, "Failed to allocate ring desc\n");
12069f2fd0dfSRameshwar Prasad Sahu 		return -ENOMEM;
12079f2fd0dfSRameshwar Prasad Sahu 	}
12089f2fd0dfSRameshwar Prasad Sahu 
12099f2fd0dfSRameshwar Prasad Sahu 	/* Configure and enable DMA ring */
12109f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_set_ring_cmd(ring);
12119f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_setup_ring(ring);
12129f2fd0dfSRameshwar Prasad Sahu 
12139f2fd0dfSRameshwar Prasad Sahu 	return 0;
12149f2fd0dfSRameshwar Prasad Sahu }
12159f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_create_chan_rings(struct xgene_dma_chan * chan)12169f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_create_chan_rings(struct xgene_dma_chan *chan)
12179f2fd0dfSRameshwar Prasad Sahu {
12189f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_ring *rx_ring = &chan->rx_ring;
12199f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_ring *tx_ring = &chan->tx_ring;
12209f2fd0dfSRameshwar Prasad Sahu 	int ret;
12219f2fd0dfSRameshwar Prasad Sahu 
12229f2fd0dfSRameshwar Prasad Sahu 	/* Create DMA Rx ring descriptor */
12239f2fd0dfSRameshwar Prasad Sahu 	rx_ring->owner = XGENE_DMA_RING_OWNER_CPU;
12249f2fd0dfSRameshwar Prasad Sahu 	rx_ring->buf_num = XGENE_DMA_CPU_BUFNUM + chan->id;
12259f2fd0dfSRameshwar Prasad Sahu 
12269f2fd0dfSRameshwar Prasad Sahu 	ret = xgene_dma_create_ring_one(chan, rx_ring,
12279f2fd0dfSRameshwar Prasad Sahu 					XGENE_DMA_RING_CFG_SIZE_64KB);
12289f2fd0dfSRameshwar Prasad Sahu 	if (ret)
12299f2fd0dfSRameshwar Prasad Sahu 		return ret;
12309f2fd0dfSRameshwar Prasad Sahu 
12319f2fd0dfSRameshwar Prasad Sahu 	chan_dbg(chan, "Rx ring id 0x%X num %d desc 0x%p\n",
12329f2fd0dfSRameshwar Prasad Sahu 		 rx_ring->id, rx_ring->num, rx_ring->desc_vaddr);
12339f2fd0dfSRameshwar Prasad Sahu 
12349f2fd0dfSRameshwar Prasad Sahu 	/* Create DMA Tx ring descriptor */
12359f2fd0dfSRameshwar Prasad Sahu 	tx_ring->owner = XGENE_DMA_RING_OWNER_DMA;
12369f2fd0dfSRameshwar Prasad Sahu 	tx_ring->buf_num = XGENE_DMA_BUFNUM + chan->id;
12379f2fd0dfSRameshwar Prasad Sahu 
12389f2fd0dfSRameshwar Prasad Sahu 	ret = xgene_dma_create_ring_one(chan, tx_ring,
12399f2fd0dfSRameshwar Prasad Sahu 					XGENE_DMA_RING_CFG_SIZE_64KB);
12409f2fd0dfSRameshwar Prasad Sahu 	if (ret) {
12419f2fd0dfSRameshwar Prasad Sahu 		xgene_dma_delete_ring_one(rx_ring);
12429f2fd0dfSRameshwar Prasad Sahu 		return ret;
12439f2fd0dfSRameshwar Prasad Sahu 	}
12449f2fd0dfSRameshwar Prasad Sahu 
12459f2fd0dfSRameshwar Prasad Sahu 	tx_ring->dst_ring_num = XGENE_DMA_RING_DST_ID(rx_ring->num);
12469f2fd0dfSRameshwar Prasad Sahu 
12479f2fd0dfSRameshwar Prasad Sahu 	chan_dbg(chan,
12489f2fd0dfSRameshwar Prasad Sahu 		 "Tx ring id 0x%X num %d desc 0x%p\n",
12499f2fd0dfSRameshwar Prasad Sahu 		 tx_ring->id, tx_ring->num, tx_ring->desc_vaddr);
12509f2fd0dfSRameshwar Prasad Sahu 
12519f2fd0dfSRameshwar Prasad Sahu 	/* Set the max outstanding request possible to this channel */
1252ee08b59dSRameshwar Prasad Sahu 	chan->max_outstanding = tx_ring->slots;
12539f2fd0dfSRameshwar Prasad Sahu 
12549f2fd0dfSRameshwar Prasad Sahu 	return ret;
12559f2fd0dfSRameshwar Prasad Sahu }
12569f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_init_rings(struct xgene_dma * pdma)12579f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_init_rings(struct xgene_dma *pdma)
12589f2fd0dfSRameshwar Prasad Sahu {
12599f2fd0dfSRameshwar Prasad Sahu 	int ret, i, j;
12609f2fd0dfSRameshwar Prasad Sahu 
12619f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
12629f2fd0dfSRameshwar Prasad Sahu 		ret = xgene_dma_create_chan_rings(&pdma->chan[i]);
12639f2fd0dfSRameshwar Prasad Sahu 		if (ret) {
12649f2fd0dfSRameshwar Prasad Sahu 			for (j = 0; j < i; j++)
12659f2fd0dfSRameshwar Prasad Sahu 				xgene_dma_delete_chan_rings(&pdma->chan[j]);
12669f2fd0dfSRameshwar Prasad Sahu 			return ret;
12679f2fd0dfSRameshwar Prasad Sahu 		}
12689f2fd0dfSRameshwar Prasad Sahu 	}
12699f2fd0dfSRameshwar Prasad Sahu 
12709f2fd0dfSRameshwar Prasad Sahu 	return ret;
12719f2fd0dfSRameshwar Prasad Sahu }
12729f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_enable(struct xgene_dma * pdma)12739f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_enable(struct xgene_dma *pdma)
12749f2fd0dfSRameshwar Prasad Sahu {
12759f2fd0dfSRameshwar Prasad Sahu 	u32 val;
12769f2fd0dfSRameshwar Prasad Sahu 
12779f2fd0dfSRameshwar Prasad Sahu 	/* Configure and enable DMA engine */
12789f2fd0dfSRameshwar Prasad Sahu 	val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
12799f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_CH_SETUP(val);
12809f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_ENABLE(val);
12819f2fd0dfSRameshwar Prasad Sahu 	iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
12829f2fd0dfSRameshwar Prasad Sahu }
12839f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_disable(struct xgene_dma * pdma)12849f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_disable(struct xgene_dma *pdma)
12859f2fd0dfSRameshwar Prasad Sahu {
12869f2fd0dfSRameshwar Prasad Sahu 	u32 val;
12879f2fd0dfSRameshwar Prasad Sahu 
12889f2fd0dfSRameshwar Prasad Sahu 	val = ioread32(pdma->csr_dma + XGENE_DMA_GCR);
12899f2fd0dfSRameshwar Prasad Sahu 	XGENE_DMA_DISABLE(val);
12909f2fd0dfSRameshwar Prasad Sahu 	iowrite32(val, pdma->csr_dma + XGENE_DMA_GCR);
12919f2fd0dfSRameshwar Prasad Sahu }
12929f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_mask_interrupts(struct xgene_dma * pdma)12939f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_mask_interrupts(struct xgene_dma *pdma)
12949f2fd0dfSRameshwar Prasad Sahu {
12959f2fd0dfSRameshwar Prasad Sahu 	/*
12969f2fd0dfSRameshwar Prasad Sahu 	 * Mask DMA ring overflow, underflow and
12979f2fd0dfSRameshwar Prasad Sahu 	 * AXI write/read error interrupts
12989f2fd0dfSRameshwar Prasad Sahu 	 */
12999f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_MASK,
13009f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
13019f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_MASK,
13029f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
13039f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_MASK,
13049f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
13059f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_MASK,
13069f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
13079f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_MASK,
13089f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
13099f2fd0dfSRameshwar Prasad Sahu 
13109f2fd0dfSRameshwar Prasad Sahu 	/* Mask DMA error interrupts */
13119f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_MASK, pdma->csr_dma + XGENE_DMA_INT_MASK);
13129f2fd0dfSRameshwar Prasad Sahu }
13139f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_unmask_interrupts(struct xgene_dma * pdma)13149f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_unmask_interrupts(struct xgene_dma *pdma)
13159f2fd0dfSRameshwar Prasad Sahu {
13169f2fd0dfSRameshwar Prasad Sahu 	/*
13179f2fd0dfSRameshwar Prasad Sahu 	 * Unmask DMA ring overflow, underflow and
13189f2fd0dfSRameshwar Prasad Sahu 	 * AXI write/read error interrupts
13199f2fd0dfSRameshwar Prasad Sahu 	 */
13209f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_UNMASK,
13219f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT0_MASK);
13229f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_UNMASK,
13239f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT1_MASK);
13249f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_UNMASK,
13259f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT2_MASK);
13269f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_UNMASK,
13279f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT3_MASK);
13289f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_UNMASK,
13299f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_RING_INT4_MASK);
13309f2fd0dfSRameshwar Prasad Sahu 
13319f2fd0dfSRameshwar Prasad Sahu 	/* Unmask DMA error interrupts */
13329f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_INT_ALL_UNMASK,
13339f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_INT_MASK);
13349f2fd0dfSRameshwar Prasad Sahu }
13359f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_init_hw(struct xgene_dma * pdma)13369f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_init_hw(struct xgene_dma *pdma)
13379f2fd0dfSRameshwar Prasad Sahu {
13389f2fd0dfSRameshwar Prasad Sahu 	u32 val;
13399f2fd0dfSRameshwar Prasad Sahu 
13409f2fd0dfSRameshwar Prasad Sahu 	/* Associate DMA ring to corresponding ring HW */
13419f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_ASSOC_RING_MNGR1,
13429f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_dma + XGENE_DMA_CFG_RING_WQ_ASSOC);
13439f2fd0dfSRameshwar Prasad Sahu 
13449f2fd0dfSRameshwar Prasad Sahu 	/* Configure RAID6 polynomial control setting */
13459f2fd0dfSRameshwar Prasad Sahu 	if (is_pq_enabled(pdma))
13469f2fd0dfSRameshwar Prasad Sahu 		iowrite32(XGENE_DMA_RAID6_MULTI_CTRL(0x1D),
13479f2fd0dfSRameshwar Prasad Sahu 			  pdma->csr_dma + XGENE_DMA_RAID6_CONT);
13489f2fd0dfSRameshwar Prasad Sahu 	else
13499f2fd0dfSRameshwar Prasad Sahu 		dev_info(pdma->dev, "PQ is disabled in HW\n");
13509f2fd0dfSRameshwar Prasad Sahu 
13519f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_enable(pdma);
13529f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_unmask_interrupts(pdma);
13539f2fd0dfSRameshwar Prasad Sahu 
13549f2fd0dfSRameshwar Prasad Sahu 	/* Get DMA id and version info */
13559f2fd0dfSRameshwar Prasad Sahu 	val = ioread32(pdma->csr_dma + XGENE_DMA_IPBRR);
13569f2fd0dfSRameshwar Prasad Sahu 
13579f2fd0dfSRameshwar Prasad Sahu 	/* DMA device info */
13589f2fd0dfSRameshwar Prasad Sahu 	dev_info(pdma->dev,
13599f2fd0dfSRameshwar Prasad Sahu 		 "X-Gene DMA v%d.%02d.%02d driver registered %d channels",
13609f2fd0dfSRameshwar Prasad Sahu 		 XGENE_DMA_REV_NO_RD(val), XGENE_DMA_BUS_ID_RD(val),
13619f2fd0dfSRameshwar Prasad Sahu 		 XGENE_DMA_DEV_ID_RD(val), XGENE_DMA_MAX_CHANNEL);
13629f2fd0dfSRameshwar Prasad Sahu }
13639f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_init_ring_mngr(struct xgene_dma * pdma)1364a3f92e8eSkbuild test robot static int xgene_dma_init_ring_mngr(struct xgene_dma *pdma)
13659f2fd0dfSRameshwar Prasad Sahu {
13669f2fd0dfSRameshwar Prasad Sahu 	if (ioread32(pdma->csr_ring + XGENE_DMA_RING_CLKEN) &&
13679f2fd0dfSRameshwar Prasad Sahu 	    (!ioread32(pdma->csr_ring + XGENE_DMA_RING_SRST)))
13689f2fd0dfSRameshwar Prasad Sahu 		return 0;
13699f2fd0dfSRameshwar Prasad Sahu 
13709f2fd0dfSRameshwar Prasad Sahu 	iowrite32(0x3, pdma->csr_ring + XGENE_DMA_RING_CLKEN);
13719f2fd0dfSRameshwar Prasad Sahu 	iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_SRST);
13729f2fd0dfSRameshwar Prasad Sahu 
13739f2fd0dfSRameshwar Prasad Sahu 	/* Bring up memory */
13749f2fd0dfSRameshwar Prasad Sahu 	iowrite32(0x0, pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
13759f2fd0dfSRameshwar Prasad Sahu 
13769f2fd0dfSRameshwar Prasad Sahu 	/* Force a barrier */
13779f2fd0dfSRameshwar Prasad Sahu 	ioread32(pdma->csr_ring + XGENE_DMA_RING_MEM_RAM_SHUTDOWN);
13789f2fd0dfSRameshwar Prasad Sahu 
13799f2fd0dfSRameshwar Prasad Sahu 	/* reset may take up to 1ms */
13809f2fd0dfSRameshwar Prasad Sahu 	usleep_range(1000, 1100);
13819f2fd0dfSRameshwar Prasad Sahu 
13829f2fd0dfSRameshwar Prasad Sahu 	if (ioread32(pdma->csr_ring + XGENE_DMA_RING_BLK_MEM_RDY)
13839f2fd0dfSRameshwar Prasad Sahu 		!= XGENE_DMA_RING_BLK_MEM_RDY_VAL) {
13849f2fd0dfSRameshwar Prasad Sahu 		dev_err(pdma->dev,
13859f2fd0dfSRameshwar Prasad Sahu 			"Failed to release ring mngr memory from shutdown\n");
13869f2fd0dfSRameshwar Prasad Sahu 		return -ENODEV;
13879f2fd0dfSRameshwar Prasad Sahu 	}
13889f2fd0dfSRameshwar Prasad Sahu 
13899f2fd0dfSRameshwar Prasad Sahu 	/* program threshold set 1 and all hysteresis */
13909f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_RING_THRESLD0_SET1_VAL,
13919f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_ring + XGENE_DMA_RING_THRESLD0_SET1);
13929f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_RING_THRESLD1_SET1_VAL,
13939f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_ring + XGENE_DMA_RING_THRESLD1_SET1);
13949f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_RING_HYSTERESIS_VAL,
13959f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_ring + XGENE_DMA_RING_HYSTERESIS);
13969f2fd0dfSRameshwar Prasad Sahu 
13979f2fd0dfSRameshwar Prasad Sahu 	/* Enable QPcore and assign error queue */
13989f2fd0dfSRameshwar Prasad Sahu 	iowrite32(XGENE_DMA_RING_ENABLE,
13999f2fd0dfSRameshwar Prasad Sahu 		  pdma->csr_ring + XGENE_DMA_RING_CONFIG);
14009f2fd0dfSRameshwar Prasad Sahu 
14019f2fd0dfSRameshwar Prasad Sahu 	return 0;
14029f2fd0dfSRameshwar Prasad Sahu }
14039f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_init_mem(struct xgene_dma * pdma)14049f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_init_mem(struct xgene_dma *pdma)
14059f2fd0dfSRameshwar Prasad Sahu {
14069f2fd0dfSRameshwar Prasad Sahu 	int ret;
14079f2fd0dfSRameshwar Prasad Sahu 
14089f2fd0dfSRameshwar Prasad Sahu 	ret = xgene_dma_init_ring_mngr(pdma);
14099f2fd0dfSRameshwar Prasad Sahu 	if (ret)
14109f2fd0dfSRameshwar Prasad Sahu 		return ret;
14119f2fd0dfSRameshwar Prasad Sahu 
14129f2fd0dfSRameshwar Prasad Sahu 	/* Bring up memory */
14139f2fd0dfSRameshwar Prasad Sahu 	iowrite32(0x0, pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
14149f2fd0dfSRameshwar Prasad Sahu 
14159f2fd0dfSRameshwar Prasad Sahu 	/* Force a barrier */
14169f2fd0dfSRameshwar Prasad Sahu 	ioread32(pdma->csr_dma + XGENE_DMA_MEM_RAM_SHUTDOWN);
14179f2fd0dfSRameshwar Prasad Sahu 
14189f2fd0dfSRameshwar Prasad Sahu 	/* reset may take up to 1ms */
14199f2fd0dfSRameshwar Prasad Sahu 	usleep_range(1000, 1100);
14209f2fd0dfSRameshwar Prasad Sahu 
14219f2fd0dfSRameshwar Prasad Sahu 	if (ioread32(pdma->csr_dma + XGENE_DMA_BLK_MEM_RDY)
14229f2fd0dfSRameshwar Prasad Sahu 		!= XGENE_DMA_BLK_MEM_RDY_VAL) {
14239f2fd0dfSRameshwar Prasad Sahu 		dev_err(pdma->dev,
14249f2fd0dfSRameshwar Prasad Sahu 			"Failed to release DMA memory from shutdown\n");
14259f2fd0dfSRameshwar Prasad Sahu 		return -ENODEV;
14269f2fd0dfSRameshwar Prasad Sahu 	}
14279f2fd0dfSRameshwar Prasad Sahu 
14289f2fd0dfSRameshwar Prasad Sahu 	return 0;
14299f2fd0dfSRameshwar Prasad Sahu }
14309f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_request_irqs(struct xgene_dma * pdma)14319f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_request_irqs(struct xgene_dma *pdma)
14329f2fd0dfSRameshwar Prasad Sahu {
14339f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan;
14349f2fd0dfSRameshwar Prasad Sahu 	int ret, i, j;
14359f2fd0dfSRameshwar Prasad Sahu 
14369f2fd0dfSRameshwar Prasad Sahu 	/* Register DMA error irq */
14379f2fd0dfSRameshwar Prasad Sahu 	ret = devm_request_irq(pdma->dev, pdma->err_irq, xgene_dma_err_isr,
14389f2fd0dfSRameshwar Prasad Sahu 			       0, "dma_error", pdma);
14399f2fd0dfSRameshwar Prasad Sahu 	if (ret) {
14409f2fd0dfSRameshwar Prasad Sahu 		dev_err(pdma->dev,
14419f2fd0dfSRameshwar Prasad Sahu 			"Failed to register error IRQ %d\n", pdma->err_irq);
14429f2fd0dfSRameshwar Prasad Sahu 		return ret;
14439f2fd0dfSRameshwar Prasad Sahu 	}
14449f2fd0dfSRameshwar Prasad Sahu 
14459f2fd0dfSRameshwar Prasad Sahu 	/* Register DMA channel rx irq */
14469f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
14479f2fd0dfSRameshwar Prasad Sahu 		chan = &pdma->chan[i];
1448b0b79024SRameshwar Prasad Sahu 		irq_set_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
14499f2fd0dfSRameshwar Prasad Sahu 		ret = devm_request_irq(chan->dev, chan->rx_irq,
14509f2fd0dfSRameshwar Prasad Sahu 				       xgene_dma_chan_ring_isr,
14519f2fd0dfSRameshwar Prasad Sahu 				       0, chan->name, chan);
14529f2fd0dfSRameshwar Prasad Sahu 		if (ret) {
14539f2fd0dfSRameshwar Prasad Sahu 			chan_err(chan, "Failed to register Rx IRQ %d\n",
14549f2fd0dfSRameshwar Prasad Sahu 				 chan->rx_irq);
14559f2fd0dfSRameshwar Prasad Sahu 			devm_free_irq(pdma->dev, pdma->err_irq, pdma);
14569f2fd0dfSRameshwar Prasad Sahu 
14579f2fd0dfSRameshwar Prasad Sahu 			for (j = 0; j < i; j++) {
14589f2fd0dfSRameshwar Prasad Sahu 				chan = &pdma->chan[i];
1459b0b79024SRameshwar Prasad Sahu 				irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
14609f2fd0dfSRameshwar Prasad Sahu 				devm_free_irq(chan->dev, chan->rx_irq, chan);
14619f2fd0dfSRameshwar Prasad Sahu 			}
14629f2fd0dfSRameshwar Prasad Sahu 
14639f2fd0dfSRameshwar Prasad Sahu 			return ret;
14649f2fd0dfSRameshwar Prasad Sahu 		}
14659f2fd0dfSRameshwar Prasad Sahu 	}
14669f2fd0dfSRameshwar Prasad Sahu 
14679f2fd0dfSRameshwar Prasad Sahu 	return 0;
14689f2fd0dfSRameshwar Prasad Sahu }
14699f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_free_irqs(struct xgene_dma * pdma)14709f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_free_irqs(struct xgene_dma *pdma)
14719f2fd0dfSRameshwar Prasad Sahu {
14729f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan;
14739f2fd0dfSRameshwar Prasad Sahu 	int i;
14749f2fd0dfSRameshwar Prasad Sahu 
14759f2fd0dfSRameshwar Prasad Sahu 	/* Free DMA device error irq */
14769f2fd0dfSRameshwar Prasad Sahu 	devm_free_irq(pdma->dev, pdma->err_irq, pdma);
14779f2fd0dfSRameshwar Prasad Sahu 
14789f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
14799f2fd0dfSRameshwar Prasad Sahu 		chan = &pdma->chan[i];
1480b0b79024SRameshwar Prasad Sahu 		irq_clear_status_flags(chan->rx_irq, IRQ_DISABLE_UNLAZY);
14819f2fd0dfSRameshwar Prasad Sahu 		devm_free_irq(chan->dev, chan->rx_irq, chan);
14829f2fd0dfSRameshwar Prasad Sahu 	}
14839f2fd0dfSRameshwar Prasad Sahu }
14849f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_set_caps(struct xgene_dma_chan * chan,struct dma_device * dma_dev)14859f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_set_caps(struct xgene_dma_chan *chan,
14869f2fd0dfSRameshwar Prasad Sahu 			       struct dma_device *dma_dev)
14879f2fd0dfSRameshwar Prasad Sahu {
14889f2fd0dfSRameshwar Prasad Sahu 	/* Initialize DMA device capability mask */
14899f2fd0dfSRameshwar Prasad Sahu 	dma_cap_zero(dma_dev->cap_mask);
14909f2fd0dfSRameshwar Prasad Sahu 
14919f2fd0dfSRameshwar Prasad Sahu 	/* Set DMA device capability */
14929f2fd0dfSRameshwar Prasad Sahu 
14939f2fd0dfSRameshwar Prasad Sahu 	/* Basically here, the X-Gene SoC DMA engine channel 0 supports XOR
14949f2fd0dfSRameshwar Prasad Sahu 	 * and channel 1 supports XOR, PQ both. First thing here is we have
14959f2fd0dfSRameshwar Prasad Sahu 	 * mechanism in hw to enable/disable PQ/XOR supports on channel 1,
14969f2fd0dfSRameshwar Prasad Sahu 	 * we can make sure this by reading SoC Efuse register.
14979f2fd0dfSRameshwar Prasad Sahu 	 * Second thing, we have hw errata that if we run channel 0 and
14989f2fd0dfSRameshwar Prasad Sahu 	 * channel 1 simultaneously with executing XOR and PQ request,
14999f2fd0dfSRameshwar Prasad Sahu 	 * suddenly DMA engine hangs, So here we enable XOR on channel 0 only
15009f2fd0dfSRameshwar Prasad Sahu 	 * if XOR and PQ supports on channel 1 is disabled.
15019f2fd0dfSRameshwar Prasad Sahu 	 */
15029f2fd0dfSRameshwar Prasad Sahu 	if ((chan->id == XGENE_DMA_PQ_CHANNEL) &&
15039f2fd0dfSRameshwar Prasad Sahu 	    is_pq_enabled(chan->pdma)) {
15049f2fd0dfSRameshwar Prasad Sahu 		dma_cap_set(DMA_PQ, dma_dev->cap_mask);
15059f2fd0dfSRameshwar Prasad Sahu 		dma_cap_set(DMA_XOR, dma_dev->cap_mask);
15069f2fd0dfSRameshwar Prasad Sahu 	} else if ((chan->id == XGENE_DMA_XOR_CHANNEL) &&
15079f2fd0dfSRameshwar Prasad Sahu 		   !is_pq_enabled(chan->pdma)) {
15089f2fd0dfSRameshwar Prasad Sahu 		dma_cap_set(DMA_XOR, dma_dev->cap_mask);
15099f2fd0dfSRameshwar Prasad Sahu 	}
15109f2fd0dfSRameshwar Prasad Sahu 
15119f2fd0dfSRameshwar Prasad Sahu 	/* Set base and prep routines */
15129f2fd0dfSRameshwar Prasad Sahu 	dma_dev->dev = chan->dev;
15139f2fd0dfSRameshwar Prasad Sahu 	dma_dev->device_alloc_chan_resources = xgene_dma_alloc_chan_resources;
15149f2fd0dfSRameshwar Prasad Sahu 	dma_dev->device_free_chan_resources = xgene_dma_free_chan_resources;
15159f2fd0dfSRameshwar Prasad Sahu 	dma_dev->device_issue_pending = xgene_dma_issue_pending;
15169f2fd0dfSRameshwar Prasad Sahu 	dma_dev->device_tx_status = xgene_dma_tx_status;
15179f2fd0dfSRameshwar Prasad Sahu 
15189f2fd0dfSRameshwar Prasad Sahu 	if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
15199f2fd0dfSRameshwar Prasad Sahu 		dma_dev->device_prep_dma_xor = xgene_dma_prep_xor;
15209f2fd0dfSRameshwar Prasad Sahu 		dma_dev->max_xor = XGENE_DMA_MAX_XOR_SRC;
152177a68e56SMaxime Ripard 		dma_dev->xor_align = DMAENGINE_ALIGN_64_BYTES;
15229f2fd0dfSRameshwar Prasad Sahu 	}
15239f2fd0dfSRameshwar Prasad Sahu 
15249f2fd0dfSRameshwar Prasad Sahu 	if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
15259f2fd0dfSRameshwar Prasad Sahu 		dma_dev->device_prep_dma_pq = xgene_dma_prep_pq;
15269f2fd0dfSRameshwar Prasad Sahu 		dma_dev->max_pq = XGENE_DMA_MAX_XOR_SRC;
152777a68e56SMaxime Ripard 		dma_dev->pq_align = DMAENGINE_ALIGN_64_BYTES;
15289f2fd0dfSRameshwar Prasad Sahu 	}
15299f2fd0dfSRameshwar Prasad Sahu }
15309f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_async_register(struct xgene_dma * pdma,int id)15319f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_async_register(struct xgene_dma *pdma, int id)
15329f2fd0dfSRameshwar Prasad Sahu {
15339f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan = &pdma->chan[id];
15349f2fd0dfSRameshwar Prasad Sahu 	struct dma_device *dma_dev = &pdma->dma_dev[id];
15359f2fd0dfSRameshwar Prasad Sahu 	int ret;
15369f2fd0dfSRameshwar Prasad Sahu 
15379f2fd0dfSRameshwar Prasad Sahu 	chan->dma_chan.device = dma_dev;
15389f2fd0dfSRameshwar Prasad Sahu 
15399f2fd0dfSRameshwar Prasad Sahu 	spin_lock_init(&chan->lock);
15409f2fd0dfSRameshwar Prasad Sahu 	INIT_LIST_HEAD(&chan->ld_pending);
15419f2fd0dfSRameshwar Prasad Sahu 	INIT_LIST_HEAD(&chan->ld_running);
15429f2fd0dfSRameshwar Prasad Sahu 	INIT_LIST_HEAD(&chan->ld_completed);
15430e71d9b9SAllen Pais 	tasklet_setup(&chan->tasklet, xgene_dma_tasklet_cb);
15449f2fd0dfSRameshwar Prasad Sahu 
15459f2fd0dfSRameshwar Prasad Sahu 	chan->pending = 0;
15469f2fd0dfSRameshwar Prasad Sahu 	chan->desc_pool = NULL;
15479f2fd0dfSRameshwar Prasad Sahu 	dma_cookie_init(&chan->dma_chan);
15489f2fd0dfSRameshwar Prasad Sahu 
15499f2fd0dfSRameshwar Prasad Sahu 	/* Setup dma device capabilities and prep routines */
15509f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_set_caps(chan, dma_dev);
15519f2fd0dfSRameshwar Prasad Sahu 
15529f2fd0dfSRameshwar Prasad Sahu 	/* Initialize DMA device list head */
15539f2fd0dfSRameshwar Prasad Sahu 	INIT_LIST_HEAD(&dma_dev->channels);
15549f2fd0dfSRameshwar Prasad Sahu 	list_add_tail(&chan->dma_chan.device_node, &dma_dev->channels);
15559f2fd0dfSRameshwar Prasad Sahu 
15569f2fd0dfSRameshwar Prasad Sahu 	/* Register with Linux async DMA framework*/
15579f2fd0dfSRameshwar Prasad Sahu 	ret = dma_async_device_register(dma_dev);
15589f2fd0dfSRameshwar Prasad Sahu 	if (ret) {
15599f2fd0dfSRameshwar Prasad Sahu 		chan_err(chan, "Failed to register async device %d", ret);
15609f2fd0dfSRameshwar Prasad Sahu 		tasklet_kill(&chan->tasklet);
15619f2fd0dfSRameshwar Prasad Sahu 
15629f2fd0dfSRameshwar Prasad Sahu 		return ret;
15639f2fd0dfSRameshwar Prasad Sahu 	}
15649f2fd0dfSRameshwar Prasad Sahu 
15659f2fd0dfSRameshwar Prasad Sahu 	/* DMA capability info */
15669f2fd0dfSRameshwar Prasad Sahu 	dev_info(pdma->dev,
1567c678fa66SDave Jiang 		 "%s: CAPABILITY ( %s%s)\n", dma_chan_name(&chan->dma_chan),
15689f2fd0dfSRameshwar Prasad Sahu 		 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "XOR " : "",
15699f2fd0dfSRameshwar Prasad Sahu 		 dma_has_cap(DMA_PQ, dma_dev->cap_mask) ? "PQ " : "");
15709f2fd0dfSRameshwar Prasad Sahu 
15719f2fd0dfSRameshwar Prasad Sahu 	return 0;
15729f2fd0dfSRameshwar Prasad Sahu }
15739f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_init_async(struct xgene_dma * pdma)15749f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_init_async(struct xgene_dma *pdma)
15759f2fd0dfSRameshwar Prasad Sahu {
15769f2fd0dfSRameshwar Prasad Sahu 	int ret, i, j;
15779f2fd0dfSRameshwar Prasad Sahu 
15789f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < XGENE_DMA_MAX_CHANNEL ; i++) {
15799f2fd0dfSRameshwar Prasad Sahu 		ret = xgene_dma_async_register(pdma, i);
15809f2fd0dfSRameshwar Prasad Sahu 		if (ret) {
15819f2fd0dfSRameshwar Prasad Sahu 			for (j = 0; j < i; j++) {
15829f2fd0dfSRameshwar Prasad Sahu 				dma_async_device_unregister(&pdma->dma_dev[j]);
15839f2fd0dfSRameshwar Prasad Sahu 				tasklet_kill(&pdma->chan[j].tasklet);
15849f2fd0dfSRameshwar Prasad Sahu 			}
15859f2fd0dfSRameshwar Prasad Sahu 
15869f2fd0dfSRameshwar Prasad Sahu 			return ret;
15879f2fd0dfSRameshwar Prasad Sahu 		}
15889f2fd0dfSRameshwar Prasad Sahu 	}
15899f2fd0dfSRameshwar Prasad Sahu 
15909f2fd0dfSRameshwar Prasad Sahu 	return ret;
15919f2fd0dfSRameshwar Prasad Sahu }
15929f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_async_unregister(struct xgene_dma * pdma)15939f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_async_unregister(struct xgene_dma *pdma)
15949f2fd0dfSRameshwar Prasad Sahu {
15959f2fd0dfSRameshwar Prasad Sahu 	int i;
15969f2fd0dfSRameshwar Prasad Sahu 
15979f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
15989f2fd0dfSRameshwar Prasad Sahu 		dma_async_device_unregister(&pdma->dma_dev[i]);
15999f2fd0dfSRameshwar Prasad Sahu }
16009f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_init_channels(struct xgene_dma * pdma)16019f2fd0dfSRameshwar Prasad Sahu static void xgene_dma_init_channels(struct xgene_dma *pdma)
16029f2fd0dfSRameshwar Prasad Sahu {
16039f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan;
16049f2fd0dfSRameshwar Prasad Sahu 	int i;
16059f2fd0dfSRameshwar Prasad Sahu 
16069f2fd0dfSRameshwar Prasad Sahu 	pdma->ring_num = XGENE_DMA_RING_NUM;
16079f2fd0dfSRameshwar Prasad Sahu 
16089f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
16099f2fd0dfSRameshwar Prasad Sahu 		chan = &pdma->chan[i];
16109f2fd0dfSRameshwar Prasad Sahu 		chan->dev = pdma->dev;
16119f2fd0dfSRameshwar Prasad Sahu 		chan->pdma = pdma;
16129f2fd0dfSRameshwar Prasad Sahu 		chan->id = i;
1613ed1f0418SDan Carpenter 		snprintf(chan->name, sizeof(chan->name), "dmachan%d", chan->id);
16149f2fd0dfSRameshwar Prasad Sahu 	}
16159f2fd0dfSRameshwar Prasad Sahu }
16169f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_get_resources(struct platform_device * pdev,struct xgene_dma * pdma)16179f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_get_resources(struct platform_device *pdev,
16189f2fd0dfSRameshwar Prasad Sahu 				   struct xgene_dma *pdma)
16199f2fd0dfSRameshwar Prasad Sahu {
16209f2fd0dfSRameshwar Prasad Sahu 	struct resource *res;
16219f2fd0dfSRameshwar Prasad Sahu 	int irq, i;
16229f2fd0dfSRameshwar Prasad Sahu 
16239f2fd0dfSRameshwar Prasad Sahu 	/* Get DMA csr region */
16249f2fd0dfSRameshwar Prasad Sahu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16259f2fd0dfSRameshwar Prasad Sahu 	if (!res) {
16269f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "Failed to get csr region\n");
16279f2fd0dfSRameshwar Prasad Sahu 		return -ENXIO;
16289f2fd0dfSRameshwar Prasad Sahu 	}
16299f2fd0dfSRameshwar Prasad Sahu 
16309f2fd0dfSRameshwar Prasad Sahu 	pdma->csr_dma = devm_ioremap(&pdev->dev, res->start,
16319f2fd0dfSRameshwar Prasad Sahu 				     resource_size(res));
16329c361b1aSDan Carpenter 	if (!pdma->csr_dma) {
16339f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "Failed to ioremap csr region");
16349c361b1aSDan Carpenter 		return -ENOMEM;
16359f2fd0dfSRameshwar Prasad Sahu 	}
16369f2fd0dfSRameshwar Prasad Sahu 
16379f2fd0dfSRameshwar Prasad Sahu 	/* Get DMA ring csr region */
16389f2fd0dfSRameshwar Prasad Sahu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
16399f2fd0dfSRameshwar Prasad Sahu 	if (!res) {
16409f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "Failed to get ring csr region\n");
16419f2fd0dfSRameshwar Prasad Sahu 		return -ENXIO;
16429f2fd0dfSRameshwar Prasad Sahu 	}
16439f2fd0dfSRameshwar Prasad Sahu 
16449f2fd0dfSRameshwar Prasad Sahu 	pdma->csr_ring =  devm_ioremap(&pdev->dev, res->start,
16459f2fd0dfSRameshwar Prasad Sahu 				       resource_size(res));
16469c361b1aSDan Carpenter 	if (!pdma->csr_ring) {
16479f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "Failed to ioremap ring csr region");
16489c361b1aSDan Carpenter 		return -ENOMEM;
16499f2fd0dfSRameshwar Prasad Sahu 	}
16509f2fd0dfSRameshwar Prasad Sahu 
16519f2fd0dfSRameshwar Prasad Sahu 	/* Get DMA ring cmd csr region */
16529f2fd0dfSRameshwar Prasad Sahu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
16539f2fd0dfSRameshwar Prasad Sahu 	if (!res) {
16549f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "Failed to get ring cmd csr region\n");
16559f2fd0dfSRameshwar Prasad Sahu 		return -ENXIO;
16569f2fd0dfSRameshwar Prasad Sahu 	}
16579f2fd0dfSRameshwar Prasad Sahu 
16589f2fd0dfSRameshwar Prasad Sahu 	pdma->csr_ring_cmd = devm_ioremap(&pdev->dev, res->start,
16599f2fd0dfSRameshwar Prasad Sahu 					  resource_size(res));
16609c361b1aSDan Carpenter 	if (!pdma->csr_ring_cmd) {
16619f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "Failed to ioremap ring cmd csr region");
16629c361b1aSDan Carpenter 		return -ENOMEM;
16639f2fd0dfSRameshwar Prasad Sahu 	}
16649f2fd0dfSRameshwar Prasad Sahu 
1665cda8e937SRameshwar Prasad Sahu 	pdma->csr_ring_cmd += XGENE_DMA_RING_CMD_SM_OFFSET;
1666cda8e937SRameshwar Prasad Sahu 
16679f2fd0dfSRameshwar Prasad Sahu 	/* Get efuse csr region */
16689f2fd0dfSRameshwar Prasad Sahu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
16699f2fd0dfSRameshwar Prasad Sahu 	if (!res) {
16709f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "Failed to get efuse csr region\n");
16719f2fd0dfSRameshwar Prasad Sahu 		return -ENXIO;
16729f2fd0dfSRameshwar Prasad Sahu 	}
16739f2fd0dfSRameshwar Prasad Sahu 
16749f2fd0dfSRameshwar Prasad Sahu 	pdma->csr_efuse = devm_ioremap(&pdev->dev, res->start,
16759f2fd0dfSRameshwar Prasad Sahu 				       resource_size(res));
16769c361b1aSDan Carpenter 	if (!pdma->csr_efuse) {
16779f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "Failed to ioremap efuse csr region");
16789c361b1aSDan Carpenter 		return -ENOMEM;
16799f2fd0dfSRameshwar Prasad Sahu 	}
16809f2fd0dfSRameshwar Prasad Sahu 
16819f2fd0dfSRameshwar Prasad Sahu 	/* Get DMA error interrupt */
16829f2fd0dfSRameshwar Prasad Sahu 	irq = platform_get_irq(pdev, 0);
1683e17be6e1SStephen Boyd 	if (irq <= 0)
16849f2fd0dfSRameshwar Prasad Sahu 		return -ENXIO;
16859f2fd0dfSRameshwar Prasad Sahu 
16869f2fd0dfSRameshwar Prasad Sahu 	pdma->err_irq = irq;
16879f2fd0dfSRameshwar Prasad Sahu 
16889f2fd0dfSRameshwar Prasad Sahu 	/* Get DMA Rx ring descriptor interrupts for all DMA channels */
16899f2fd0dfSRameshwar Prasad Sahu 	for (i = 1; i <= XGENE_DMA_MAX_CHANNEL; i++) {
16909f2fd0dfSRameshwar Prasad Sahu 		irq = platform_get_irq(pdev, i);
1691e17be6e1SStephen Boyd 		if (irq <= 0)
16929f2fd0dfSRameshwar Prasad Sahu 			return -ENXIO;
16939f2fd0dfSRameshwar Prasad Sahu 
16949f2fd0dfSRameshwar Prasad Sahu 		pdma->chan[i - 1].rx_irq = irq;
16959f2fd0dfSRameshwar Prasad Sahu 	}
16969f2fd0dfSRameshwar Prasad Sahu 
16979f2fd0dfSRameshwar Prasad Sahu 	return 0;
16989f2fd0dfSRameshwar Prasad Sahu }
16999f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_probe(struct platform_device * pdev)17009f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_probe(struct platform_device *pdev)
17019f2fd0dfSRameshwar Prasad Sahu {
17029f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma *pdma;
17039f2fd0dfSRameshwar Prasad Sahu 	int ret, i;
17049f2fd0dfSRameshwar Prasad Sahu 
17059f2fd0dfSRameshwar Prasad Sahu 	pdma = devm_kzalloc(&pdev->dev, sizeof(*pdma), GFP_KERNEL);
17069f2fd0dfSRameshwar Prasad Sahu 	if (!pdma)
17079f2fd0dfSRameshwar Prasad Sahu 		return -ENOMEM;
17089f2fd0dfSRameshwar Prasad Sahu 
17099f2fd0dfSRameshwar Prasad Sahu 	pdma->dev = &pdev->dev;
17109f2fd0dfSRameshwar Prasad Sahu 	platform_set_drvdata(pdev, pdma);
17119f2fd0dfSRameshwar Prasad Sahu 
17129f2fd0dfSRameshwar Prasad Sahu 	ret = xgene_dma_get_resources(pdev, pdma);
17139f2fd0dfSRameshwar Prasad Sahu 	if (ret)
17149f2fd0dfSRameshwar Prasad Sahu 		return ret;
17159f2fd0dfSRameshwar Prasad Sahu 
17169f2fd0dfSRameshwar Prasad Sahu 	pdma->clk = devm_clk_get(&pdev->dev, NULL);
171789079493SRameshwar Prasad Sahu 	if (IS_ERR(pdma->clk) && !ACPI_COMPANION(&pdev->dev)) {
17189f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "Failed to get clk\n");
17199f2fd0dfSRameshwar Prasad Sahu 		return PTR_ERR(pdma->clk);
17209f2fd0dfSRameshwar Prasad Sahu 	}
17219f2fd0dfSRameshwar Prasad Sahu 
17229f2fd0dfSRameshwar Prasad Sahu 	/* Enable clk before accessing registers */
172389079493SRameshwar Prasad Sahu 	if (!IS_ERR(pdma->clk)) {
17249f2fd0dfSRameshwar Prasad Sahu 		ret = clk_prepare_enable(pdma->clk);
17259f2fd0dfSRameshwar Prasad Sahu 		if (ret) {
17269f2fd0dfSRameshwar Prasad Sahu 			dev_err(&pdev->dev, "Failed to enable clk %d\n", ret);
17279f2fd0dfSRameshwar Prasad Sahu 			return ret;
17289f2fd0dfSRameshwar Prasad Sahu 		}
172989079493SRameshwar Prasad Sahu 	}
17309f2fd0dfSRameshwar Prasad Sahu 
17319f2fd0dfSRameshwar Prasad Sahu 	/* Remove DMA RAM out of shutdown */
17329f2fd0dfSRameshwar Prasad Sahu 	ret = xgene_dma_init_mem(pdma);
17339f2fd0dfSRameshwar Prasad Sahu 	if (ret)
17349f2fd0dfSRameshwar Prasad Sahu 		goto err_clk_enable;
17359f2fd0dfSRameshwar Prasad Sahu 
17369f2fd0dfSRameshwar Prasad Sahu 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(42));
17379f2fd0dfSRameshwar Prasad Sahu 	if (ret) {
17389f2fd0dfSRameshwar Prasad Sahu 		dev_err(&pdev->dev, "No usable DMA configuration\n");
17399f2fd0dfSRameshwar Prasad Sahu 		goto err_dma_mask;
17409f2fd0dfSRameshwar Prasad Sahu 	}
17419f2fd0dfSRameshwar Prasad Sahu 
17429f2fd0dfSRameshwar Prasad Sahu 	/* Initialize DMA channels software state */
17439f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_init_channels(pdma);
17449f2fd0dfSRameshwar Prasad Sahu 
17459f2fd0dfSRameshwar Prasad Sahu 	/* Configue DMA rings */
17469f2fd0dfSRameshwar Prasad Sahu 	ret = xgene_dma_init_rings(pdma);
17479f2fd0dfSRameshwar Prasad Sahu 	if (ret)
17489f2fd0dfSRameshwar Prasad Sahu 		goto err_clk_enable;
17499f2fd0dfSRameshwar Prasad Sahu 
17509f2fd0dfSRameshwar Prasad Sahu 	ret = xgene_dma_request_irqs(pdma);
17519f2fd0dfSRameshwar Prasad Sahu 	if (ret)
17529f2fd0dfSRameshwar Prasad Sahu 		goto err_request_irq;
17539f2fd0dfSRameshwar Prasad Sahu 
17549f2fd0dfSRameshwar Prasad Sahu 	/* Configure and enable DMA engine */
17559f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_init_hw(pdma);
17569f2fd0dfSRameshwar Prasad Sahu 
17579f2fd0dfSRameshwar Prasad Sahu 	/* Register DMA device with linux async framework */
17589f2fd0dfSRameshwar Prasad Sahu 	ret = xgene_dma_init_async(pdma);
17599f2fd0dfSRameshwar Prasad Sahu 	if (ret)
17609f2fd0dfSRameshwar Prasad Sahu 		goto err_async_init;
17619f2fd0dfSRameshwar Prasad Sahu 
17629f2fd0dfSRameshwar Prasad Sahu 	return 0;
17639f2fd0dfSRameshwar Prasad Sahu 
17649f2fd0dfSRameshwar Prasad Sahu err_async_init:
17659f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_free_irqs(pdma);
17669f2fd0dfSRameshwar Prasad Sahu 
17679f2fd0dfSRameshwar Prasad Sahu err_request_irq:
17689f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++)
17699f2fd0dfSRameshwar Prasad Sahu 		xgene_dma_delete_chan_rings(&pdma->chan[i]);
17709f2fd0dfSRameshwar Prasad Sahu 
17719f2fd0dfSRameshwar Prasad Sahu err_dma_mask:
17729f2fd0dfSRameshwar Prasad Sahu err_clk_enable:
177389079493SRameshwar Prasad Sahu 	if (!IS_ERR(pdma->clk))
17749f2fd0dfSRameshwar Prasad Sahu 		clk_disable_unprepare(pdma->clk);
17759f2fd0dfSRameshwar Prasad Sahu 
17769f2fd0dfSRameshwar Prasad Sahu 	return ret;
17779f2fd0dfSRameshwar Prasad Sahu }
17789f2fd0dfSRameshwar Prasad Sahu 
xgene_dma_remove(struct platform_device * pdev)17799f2fd0dfSRameshwar Prasad Sahu static int xgene_dma_remove(struct platform_device *pdev)
17809f2fd0dfSRameshwar Prasad Sahu {
17819f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma *pdma = platform_get_drvdata(pdev);
17829f2fd0dfSRameshwar Prasad Sahu 	struct xgene_dma_chan *chan;
17839f2fd0dfSRameshwar Prasad Sahu 	int i;
17849f2fd0dfSRameshwar Prasad Sahu 
17859f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_async_unregister(pdma);
17869f2fd0dfSRameshwar Prasad Sahu 
17879f2fd0dfSRameshwar Prasad Sahu 	/* Mask interrupts and disable DMA engine */
17889f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_mask_interrupts(pdma);
17899f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_disable(pdma);
17909f2fd0dfSRameshwar Prasad Sahu 	xgene_dma_free_irqs(pdma);
17919f2fd0dfSRameshwar Prasad Sahu 
17929f2fd0dfSRameshwar Prasad Sahu 	for (i = 0; i < XGENE_DMA_MAX_CHANNEL; i++) {
17939f2fd0dfSRameshwar Prasad Sahu 		chan = &pdma->chan[i];
17949f2fd0dfSRameshwar Prasad Sahu 		tasklet_kill(&chan->tasklet);
17959f2fd0dfSRameshwar Prasad Sahu 		xgene_dma_delete_chan_rings(chan);
17969f2fd0dfSRameshwar Prasad Sahu 	}
17979f2fd0dfSRameshwar Prasad Sahu 
179889079493SRameshwar Prasad Sahu 	if (!IS_ERR(pdma->clk))
17999f2fd0dfSRameshwar Prasad Sahu 		clk_disable_unprepare(pdma->clk);
18009f2fd0dfSRameshwar Prasad Sahu 
18019f2fd0dfSRameshwar Prasad Sahu 	return 0;
18029f2fd0dfSRameshwar Prasad Sahu }
18039f2fd0dfSRameshwar Prasad Sahu 
180489079493SRameshwar Prasad Sahu #ifdef CONFIG_ACPI
180589079493SRameshwar Prasad Sahu static const struct acpi_device_id xgene_dma_acpi_match_ptr[] = {
180689079493SRameshwar Prasad Sahu 	{"APMC0D43", 0},
180789079493SRameshwar Prasad Sahu 	{},
180889079493SRameshwar Prasad Sahu };
180989079493SRameshwar Prasad Sahu MODULE_DEVICE_TABLE(acpi, xgene_dma_acpi_match_ptr);
181089079493SRameshwar Prasad Sahu #endif
181189079493SRameshwar Prasad Sahu 
18129f2fd0dfSRameshwar Prasad Sahu static const struct of_device_id xgene_dma_of_match_ptr[] = {
18139f2fd0dfSRameshwar Prasad Sahu 	{.compatible = "apm,xgene-storm-dma",},
18149f2fd0dfSRameshwar Prasad Sahu 	{},
18159f2fd0dfSRameshwar Prasad Sahu };
18169f2fd0dfSRameshwar Prasad Sahu MODULE_DEVICE_TABLE(of, xgene_dma_of_match_ptr);
18179f2fd0dfSRameshwar Prasad Sahu 
18189f2fd0dfSRameshwar Prasad Sahu static struct platform_driver xgene_dma_driver = {
18199f2fd0dfSRameshwar Prasad Sahu 	.probe = xgene_dma_probe,
18209f2fd0dfSRameshwar Prasad Sahu 	.remove = xgene_dma_remove,
18219f2fd0dfSRameshwar Prasad Sahu 	.driver = {
18229f2fd0dfSRameshwar Prasad Sahu 		.name = "X-Gene-DMA",
18239f2fd0dfSRameshwar Prasad Sahu 		.of_match_table = xgene_dma_of_match_ptr,
182489079493SRameshwar Prasad Sahu 		.acpi_match_table = ACPI_PTR(xgene_dma_acpi_match_ptr),
18259f2fd0dfSRameshwar Prasad Sahu 	},
18269f2fd0dfSRameshwar Prasad Sahu };
18279f2fd0dfSRameshwar Prasad Sahu 
18289f2fd0dfSRameshwar Prasad Sahu module_platform_driver(xgene_dma_driver);
18299f2fd0dfSRameshwar Prasad Sahu 
18309f2fd0dfSRameshwar Prasad Sahu MODULE_DESCRIPTION("APM X-Gene SoC DMA driver");
18319f2fd0dfSRameshwar Prasad Sahu MODULE_AUTHOR("Rameshwar Prasad Sahu <rsahu@apm.com>");
18329f2fd0dfSRameshwar Prasad Sahu MODULE_AUTHOR("Loc Ho <lho@apm.com>");
18339f2fd0dfSRameshwar Prasad Sahu MODULE_LICENSE("GPL");
18349f2fd0dfSRameshwar Prasad Sahu MODULE_VERSION("1.0");
1835