zynqmp_dma.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * DMA driver for Xilinx ZynqMP DMA Engine
  3. *
  4. * Copyright (C) 2016 Xilinx, Inc. All rights reserved.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/dma/xilinx_dma.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/slab.h>
  23. #include <linux/clk.h>
  24. #include <linux/io-64-nonatomic-lo-hi.h>
  25. #include <linux/pm_runtime.h>
  26. #include "../dmaengine.h"
  27. /* Register Offsets */
  28. #define ZYNQMP_DMA_ISR 0x100
  29. #define ZYNQMP_DMA_IMR 0x104
  30. #define ZYNQMP_DMA_IER 0x108
  31. #define ZYNQMP_DMA_IDS 0x10C
  32. #define ZYNQMP_DMA_CTRL0 0x110
  33. #define ZYNQMP_DMA_CTRL1 0x114
  34. #define ZYNQMP_DMA_DATA_ATTR 0x120
  35. #define ZYNQMP_DMA_DSCR_ATTR 0x124
  36. #define ZYNQMP_DMA_SRC_DSCR_WRD0 0x128
  37. #define ZYNQMP_DMA_SRC_DSCR_WRD1 0x12C
  38. #define ZYNQMP_DMA_SRC_DSCR_WRD2 0x130
  39. #define ZYNQMP_DMA_SRC_DSCR_WRD3 0x134
  40. #define ZYNQMP_DMA_DST_DSCR_WRD0 0x138
  41. #define ZYNQMP_DMA_DST_DSCR_WRD1 0x13C
  42. #define ZYNQMP_DMA_DST_DSCR_WRD2 0x140
  43. #define ZYNQMP_DMA_DST_DSCR_WRD3 0x144
  44. #define ZYNQMP_DMA_SRC_START_LSB 0x158
  45. #define ZYNQMP_DMA_SRC_START_MSB 0x15C
  46. #define ZYNQMP_DMA_DST_START_LSB 0x160
  47. #define ZYNQMP_DMA_DST_START_MSB 0x164
  48. #define ZYNQMP_DMA_TOTAL_BYTE 0x188
  49. #define ZYNQMP_DMA_RATE_CTRL 0x18C
  50. #define ZYNQMP_DMA_IRQ_SRC_ACCT 0x190
  51. #define ZYNQMP_DMA_IRQ_DST_ACCT 0x194
  52. #define ZYNQMP_DMA_CTRL2 0x200
  53. /* Interrupt registers bit field definitions */
  54. #define ZYNQMP_DMA_DONE BIT(10)
  55. #define ZYNQMP_DMA_AXI_WR_DATA BIT(9)
  56. #define ZYNQMP_DMA_AXI_RD_DATA BIT(8)
  57. #define ZYNQMP_DMA_AXI_RD_DST_DSCR BIT(7)
  58. #define ZYNQMP_DMA_AXI_RD_SRC_DSCR BIT(6)
  59. #define ZYNQMP_DMA_IRQ_DST_ACCT_ERR BIT(5)
  60. #define ZYNQMP_DMA_IRQ_SRC_ACCT_ERR BIT(4)
  61. #define ZYNQMP_DMA_BYTE_CNT_OVRFL BIT(3)
  62. #define ZYNQMP_DMA_DST_DSCR_DONE BIT(2)
  63. #define ZYNQMP_DMA_INV_APB BIT(0)
  64. /* Control 0 register bit field definitions */
  65. #define ZYNQMP_DMA_OVR_FETCH BIT(7)
  66. #define ZYNQMP_DMA_POINT_TYPE_SG BIT(6)
  67. #define ZYNQMP_DMA_RATE_CTRL_EN BIT(3)
  68. /* Control 1 register bit field definitions */
  69. #define ZYNQMP_DMA_SRC_ISSUE GENMASK(4, 0)
  70. /* Data Attribute register bit field definitions */
  71. #define ZYNQMP_DMA_ARBURST GENMASK(27, 26)
  72. #define ZYNQMP_DMA_ARCACHE GENMASK(25, 22)
  73. #define ZYNQMP_DMA_ARCACHE_OFST 22
  74. #define ZYNQMP_DMA_ARQOS GENMASK(21, 18)
  75. #define ZYNQMP_DMA_ARQOS_OFST 18
  76. #define ZYNQMP_DMA_ARLEN GENMASK(17, 14)
  77. #define ZYNQMP_DMA_ARLEN_OFST 14
  78. #define ZYNQMP_DMA_AWBURST GENMASK(13, 12)
  79. #define ZYNQMP_DMA_AWCACHE GENMASK(11, 8)
  80. #define ZYNQMP_DMA_AWCACHE_OFST 8
  81. #define ZYNQMP_DMA_AWQOS GENMASK(7, 4)
  82. #define ZYNQMP_DMA_AWQOS_OFST 4
  83. #define ZYNQMP_DMA_AWLEN GENMASK(3, 0)
  84. #define ZYNQMP_DMA_AWLEN_OFST 0
  85. /* Descriptor Attribute register bit field definitions */
  86. #define ZYNQMP_DMA_AXCOHRNT BIT(8)
  87. #define ZYNQMP_DMA_AXCACHE GENMASK(7, 4)
  88. #define ZYNQMP_DMA_AXCACHE_OFST 4
  89. #define ZYNQMP_DMA_AXQOS GENMASK(3, 0)
  90. #define ZYNQMP_DMA_AXQOS_OFST 0
  91. /* Control register 2 bit field definitions */
  92. #define ZYNQMP_DMA_ENABLE BIT(0)
  93. /* Buffer Descriptor definitions */
  94. #define ZYNQMP_DMA_DESC_CTRL_STOP 0x10
  95. #define ZYNQMP_DMA_DESC_CTRL_COMP_INT 0x4
  96. #define ZYNQMP_DMA_DESC_CTRL_SIZE_256 0x2
  97. #define ZYNQMP_DMA_DESC_CTRL_COHRNT 0x1
  98. /* Interrupt Mask specific definitions */
  99. #define ZYNQMP_DMA_INT_ERR (ZYNQMP_DMA_AXI_RD_DATA | \
  100. ZYNQMP_DMA_AXI_WR_DATA | \
  101. ZYNQMP_DMA_AXI_RD_DST_DSCR | \
  102. ZYNQMP_DMA_AXI_RD_SRC_DSCR | \
  103. ZYNQMP_DMA_INV_APB)
  104. #define ZYNQMP_DMA_INT_OVRFL (ZYNQMP_DMA_BYTE_CNT_OVRFL | \
  105. ZYNQMP_DMA_IRQ_SRC_ACCT_ERR | \
  106. ZYNQMP_DMA_IRQ_DST_ACCT_ERR)
  107. #define ZYNQMP_DMA_INT_DONE (ZYNQMP_DMA_DONE | ZYNQMP_DMA_DST_DSCR_DONE)
  108. #define ZYNQMP_DMA_INT_EN_DEFAULT_MASK (ZYNQMP_DMA_INT_DONE | \
  109. ZYNQMP_DMA_INT_ERR | \
  110. ZYNQMP_DMA_INT_OVRFL | \
  111. ZYNQMP_DMA_DST_DSCR_DONE)
  112. /* Max number of descriptors per channel */
  113. #define ZYNQMP_DMA_NUM_DESCS 32
  114. /* Max transfer size per descriptor */
  115. #define ZYNQMP_DMA_MAX_TRANS_LEN 0x40000000
  116. /* Reset values for data attributes */
  117. #define ZYNQMP_DMA_AXCACHE_VAL 0xF
  118. #define ZYNQMP_DMA_ARLEN_RST_VAL 0xF
  119. #define ZYNQMP_DMA_AWLEN_RST_VAL 0xF
  120. #define ZYNQMP_DMA_SRC_ISSUE_RST_VAL 0x1F
  121. #define ZYNQMP_DMA_IDS_DEFAULT_MASK 0xFFF
  122. /* Bus width in bits */
  123. #define ZYNQMP_DMA_BUS_WIDTH_64 64
  124. #define ZYNQMP_DMA_BUS_WIDTH_128 128
  125. #define ZDMA_PM_TIMEOUT 100
  126. #define ZYNQMP_DMA_DESC_SIZE(chan) (chan->desc_size)
  127. #define to_chan(chan) container_of(chan, struct zynqmp_dma_chan, \
  128. common)
  129. #define tx_to_desc(tx) container_of(tx, struct zynqmp_dma_desc_sw, \
  130. async_tx)
  131. /**
  132. * struct zynqmp_dma_desc_ll - Hw linked list descriptor
  133. * @addr: Buffer address
  134. * @size: Size of the buffer
  135. * @ctrl: Control word
  136. * @nxtdscraddr: Next descriptor base address
  137. * @rsvd: Reserved field and for Hw internal use.
  138. */
  139. struct zynqmp_dma_desc_ll {
  140. u64 addr;
  141. u32 size;
  142. u32 ctrl;
  143. u64 nxtdscraddr;
  144. u64 rsvd;
  145. };
  146. /**
  147. * struct zynqmp_dma_desc_sw - Per Transaction structure
  148. * @src: Source address for simple mode dma
  149. * @dst: Destination address for simple mode dma
  150. * @len: Transfer length for simple mode dma
  151. * @node: Node in the channel descriptor list
  152. * @tx_list: List head for the current transfer
  153. * @async_tx: Async transaction descriptor
  154. * @src_v: Virtual address of the src descriptor
  155. * @src_p: Physical address of the src descriptor
  156. * @dst_v: Virtual address of the dst descriptor
  157. * @dst_p: Physical address of the dst descriptor
  158. */
  159. struct zynqmp_dma_desc_sw {
  160. u64 src;
  161. u64 dst;
  162. u32 len;
  163. struct list_head node;
  164. struct list_head tx_list;
  165. struct dma_async_tx_descriptor async_tx;
  166. struct zynqmp_dma_desc_ll *src_v;
  167. dma_addr_t src_p;
  168. struct zynqmp_dma_desc_ll *dst_v;
  169. dma_addr_t dst_p;
  170. };
  171. /**
  172. * struct zynqmp_dma_chan - Driver specific DMA channel structure
  173. * @zdev: Driver specific device structure
  174. * @regs: Control registers offset
  175. * @lock: Descriptor operation lock
  176. * @pending_list: Descriptors waiting
  177. * @free_list: Descriptors free
  178. * @active_list: Descriptors active
  179. * @sw_desc_pool: SW descriptor pool
  180. * @done_list: Complete descriptors
  181. * @common: DMA common channel
  182. * @desc_pool_v: Statically allocated descriptor base
  183. * @desc_pool_p: Physical allocated descriptor base
  184. * @desc_free_cnt: Descriptor available count
  185. * @dev: The dma device
  186. * @irq: Channel IRQ
  187. * @is_dmacoherent: Tells whether dma operations are coherent or not
  188. * @tasklet: Cleanup work after irq
  189. * @idle : Channel status;
  190. * @desc_size: Size of the low level descriptor
  191. * @err: Channel has errors
  192. * @bus_width: Bus width
  193. * @src_burst_len: Source burst length
  194. * @dst_burst_len: Dest burst length
  195. */
  196. struct zynqmp_dma_chan {
  197. struct zynqmp_dma_device *zdev;
  198. void __iomem *regs;
  199. spinlock_t lock;
  200. struct list_head pending_list;
  201. struct list_head free_list;
  202. struct list_head active_list;
  203. struct zynqmp_dma_desc_sw *sw_desc_pool;
  204. struct list_head done_list;
  205. struct dma_chan common;
  206. void *desc_pool_v;
  207. dma_addr_t desc_pool_p;
  208. u32 desc_free_cnt;
  209. struct device *dev;
  210. int irq;
  211. bool is_dmacoherent;
  212. struct tasklet_struct tasklet;
  213. bool idle;
  214. u32 desc_size;
  215. bool err;
  216. u32 bus_width;
  217. u32 src_burst_len;
  218. u32 dst_burst_len;
  219. };
  220. /**
  221. * struct zynqmp_dma_device - DMA device structure
  222. * @dev: Device Structure
  223. * @common: DMA device structure
  224. * @chan: Driver specific DMA channel
  225. * @clk_main: Pointer to main clock
  226. * @clk_apb: Pointer to apb clock
  227. */
  228. struct zynqmp_dma_device {
  229. struct device *dev;
  230. struct dma_device common;
  231. struct zynqmp_dma_chan *chan;
  232. struct clk *clk_main;
  233. struct clk *clk_apb;
  234. };
  235. static inline void zynqmp_dma_writeq(struct zynqmp_dma_chan *chan, u32 reg,
  236. u64 value)
  237. {
  238. lo_hi_writeq(value, chan->regs + reg);
  239. }
  240. /**
  241. * zynqmp_dma_update_desc_to_ctrlr - Updates descriptor to the controller
  242. * @chan: ZynqMP DMA DMA channel pointer
  243. * @desc: Transaction descriptor pointer
  244. */
  245. static void zynqmp_dma_update_desc_to_ctrlr(struct zynqmp_dma_chan *chan,
  246. struct zynqmp_dma_desc_sw *desc)
  247. {
  248. dma_addr_t addr;
  249. addr = desc->src_p;
  250. zynqmp_dma_writeq(chan, ZYNQMP_DMA_SRC_START_LSB, addr);
  251. addr = desc->dst_p;
  252. zynqmp_dma_writeq(chan, ZYNQMP_DMA_DST_START_LSB, addr);
  253. }
  254. /**
  255. * zynqmp_dma_desc_config_eod - Mark the descriptor as end descriptor
  256. * @chan: ZynqMP DMA channel pointer
  257. * @desc: Hw descriptor pointer
  258. */
  259. static void zynqmp_dma_desc_config_eod(struct zynqmp_dma_chan *chan,
  260. void *desc)
  261. {
  262. struct zynqmp_dma_desc_ll *hw = (struct zynqmp_dma_desc_ll *)desc;
  263. hw->ctrl |= ZYNQMP_DMA_DESC_CTRL_STOP;
  264. hw++;
  265. hw->ctrl |= ZYNQMP_DMA_DESC_CTRL_COMP_INT | ZYNQMP_DMA_DESC_CTRL_STOP;
  266. }
  267. /**
  268. * zynqmp_dma_config_sg_ll_desc - Configure the linked list descriptor
  269. * @chan: ZynqMP DMA channel pointer
  270. * @sdesc: Hw descriptor pointer
  271. * @src: Source buffer address
  272. * @dst: Destination buffer address
  273. * @len: Transfer length
  274. * @prev: Previous hw descriptor pointer
  275. */
  276. static void zynqmp_dma_config_sg_ll_desc(struct zynqmp_dma_chan *chan,
  277. struct zynqmp_dma_desc_ll *sdesc,
  278. dma_addr_t src, dma_addr_t dst, size_t len,
  279. struct zynqmp_dma_desc_ll *prev)
  280. {
  281. struct zynqmp_dma_desc_ll *ddesc = sdesc + 1;
  282. sdesc->size = ddesc->size = len;
  283. sdesc->addr = src;
  284. ddesc->addr = dst;
  285. sdesc->ctrl = ddesc->ctrl = ZYNQMP_DMA_DESC_CTRL_SIZE_256;
  286. if (chan->is_dmacoherent) {
  287. sdesc->ctrl |= ZYNQMP_DMA_DESC_CTRL_COHRNT;
  288. ddesc->ctrl |= ZYNQMP_DMA_DESC_CTRL_COHRNT;
  289. }
  290. if (prev) {
  291. dma_addr_t addr = chan->desc_pool_p +
  292. ((uintptr_t)sdesc - (uintptr_t)chan->desc_pool_v);
  293. ddesc = prev + 1;
  294. prev->nxtdscraddr = addr;
  295. ddesc->nxtdscraddr = addr + ZYNQMP_DMA_DESC_SIZE(chan);
  296. }
  297. }
  298. /**
  299. * zynqmp_dma_init - Initialize the channel
  300. * @chan: ZynqMP DMA channel pointer
  301. */
  302. static void zynqmp_dma_init(struct zynqmp_dma_chan *chan)
  303. {
  304. u32 val;
  305. writel(ZYNQMP_DMA_IDS_DEFAULT_MASK, chan->regs + ZYNQMP_DMA_IDS);
  306. val = readl(chan->regs + ZYNQMP_DMA_ISR);
  307. writel(val, chan->regs + ZYNQMP_DMA_ISR);
  308. if (chan->is_dmacoherent) {
  309. val = ZYNQMP_DMA_AXCOHRNT;
  310. val = (val & ~ZYNQMP_DMA_AXCACHE) |
  311. (ZYNQMP_DMA_AXCACHE_VAL << ZYNQMP_DMA_AXCACHE_OFST);
  312. writel(val, chan->regs + ZYNQMP_DMA_DSCR_ATTR);
  313. }
  314. val = readl(chan->regs + ZYNQMP_DMA_DATA_ATTR);
  315. if (chan->is_dmacoherent) {
  316. val = (val & ~ZYNQMP_DMA_ARCACHE) |
  317. (ZYNQMP_DMA_AXCACHE_VAL << ZYNQMP_DMA_ARCACHE_OFST);
  318. val = (val & ~ZYNQMP_DMA_AWCACHE) |
  319. (ZYNQMP_DMA_AXCACHE_VAL << ZYNQMP_DMA_AWCACHE_OFST);
  320. }
  321. writel(val, chan->regs + ZYNQMP_DMA_DATA_ATTR);
  322. /* Clearing the interrupt account rgisters */
  323. val = readl(chan->regs + ZYNQMP_DMA_IRQ_SRC_ACCT);
  324. val = readl(chan->regs + ZYNQMP_DMA_IRQ_DST_ACCT);
  325. chan->idle = true;
  326. }
  327. /**
  328. * zynqmp_dma_tx_submit - Submit DMA transaction
  329. * @tx: Async transaction descriptor pointer
  330. *
  331. * Return: cookie value
  332. */
  333. static dma_cookie_t zynqmp_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  334. {
  335. struct zynqmp_dma_chan *chan = to_chan(tx->chan);
  336. struct zynqmp_dma_desc_sw *desc, *new;
  337. dma_cookie_t cookie;
  338. new = tx_to_desc(tx);
  339. spin_lock_bh(&chan->lock);
  340. cookie = dma_cookie_assign(tx);
  341. if (!list_empty(&chan->pending_list)) {
  342. desc = list_last_entry(&chan->pending_list,
  343. struct zynqmp_dma_desc_sw, node);
  344. if (!list_empty(&desc->tx_list))
  345. desc = list_last_entry(&desc->tx_list,
  346. struct zynqmp_dma_desc_sw, node);
  347. desc->src_v->nxtdscraddr = new->src_p;
  348. desc->src_v->ctrl &= ~ZYNQMP_DMA_DESC_CTRL_STOP;
  349. desc->dst_v->nxtdscraddr = new->dst_p;
  350. desc->dst_v->ctrl &= ~ZYNQMP_DMA_DESC_CTRL_STOP;
  351. }
  352. list_add_tail(&new->node, &chan->pending_list);
  353. spin_unlock_bh(&chan->lock);
  354. return cookie;
  355. }
  356. /**
  357. * zynqmp_dma_get_descriptor - Get the sw descriptor from the pool
  358. * @chan: ZynqMP DMA channel pointer
  359. *
  360. * Return: The sw descriptor
  361. */
  362. static struct zynqmp_dma_desc_sw *
  363. zynqmp_dma_get_descriptor(struct zynqmp_dma_chan *chan)
  364. {
  365. struct zynqmp_dma_desc_sw *desc;
  366. spin_lock_bh(&chan->lock);
  367. desc = list_first_entry(&chan->free_list,
  368. struct zynqmp_dma_desc_sw, node);
  369. list_del(&desc->node);
  370. spin_unlock_bh(&chan->lock);
  371. INIT_LIST_HEAD(&desc->tx_list);
  372. /* Clear the src and dst descriptor memory */
  373. memset((void *)desc->src_v, 0, ZYNQMP_DMA_DESC_SIZE(chan));
  374. memset((void *)desc->dst_v, 0, ZYNQMP_DMA_DESC_SIZE(chan));
  375. return desc;
  376. }
  377. /**
  378. * zynqmp_dma_free_descriptor - Issue pending transactions
  379. * @chan: ZynqMP DMA channel pointer
  380. * @sdesc: Transaction descriptor pointer
  381. */
  382. static void zynqmp_dma_free_descriptor(struct zynqmp_dma_chan *chan,
  383. struct zynqmp_dma_desc_sw *sdesc)
  384. {
  385. struct zynqmp_dma_desc_sw *child, *next;
  386. chan->desc_free_cnt++;
  387. list_add_tail(&sdesc->node, &chan->free_list);
  388. list_for_each_entry_safe(child, next, &sdesc->tx_list, node) {
  389. chan->desc_free_cnt++;
  390. list_move_tail(&child->node, &chan->free_list);
  391. }
  392. }
  393. /**
  394. * zynqmp_dma_free_desc_list - Free descriptors list
  395. * @chan: ZynqMP DMA channel pointer
  396. * @list: List to parse and delete the descriptor
  397. */
  398. static void zynqmp_dma_free_desc_list(struct zynqmp_dma_chan *chan,
  399. struct list_head *list)
  400. {
  401. struct zynqmp_dma_desc_sw *desc, *next;
  402. list_for_each_entry_safe(desc, next, list, node)
  403. zynqmp_dma_free_descriptor(chan, desc);
  404. }
  405. /**
  406. * zynqmp_dma_alloc_chan_resources - Allocate channel resources
  407. * @dchan: DMA channel
  408. *
  409. * Return: Number of descriptors on success and failure value on error
  410. */
  411. static int zynqmp_dma_alloc_chan_resources(struct dma_chan *dchan)
  412. {
  413. struct zynqmp_dma_chan *chan = to_chan(dchan);
  414. struct zynqmp_dma_desc_sw *desc;
  415. int i, ret;
  416. ret = pm_runtime_get_sync(chan->dev);
  417. if (ret < 0)
  418. return ret;
  419. chan->sw_desc_pool = kcalloc(ZYNQMP_DMA_NUM_DESCS, sizeof(*desc),
  420. GFP_KERNEL);
  421. if (!chan->sw_desc_pool)
  422. return -ENOMEM;
  423. chan->idle = true;
  424. chan->desc_free_cnt = ZYNQMP_DMA_NUM_DESCS;
  425. INIT_LIST_HEAD(&chan->free_list);
  426. for (i = 0; i < ZYNQMP_DMA_NUM_DESCS; i++) {
  427. desc = chan->sw_desc_pool + i;
  428. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  429. desc->async_tx.tx_submit = zynqmp_dma_tx_submit;
  430. list_add_tail(&desc->node, &chan->free_list);
  431. }
  432. chan->desc_pool_v = dma_zalloc_coherent(chan->dev,
  433. (2 * chan->desc_size * ZYNQMP_DMA_NUM_DESCS),
  434. &chan->desc_pool_p, GFP_KERNEL);
  435. if (!chan->desc_pool_v)
  436. return -ENOMEM;
  437. for (i = 0; i < ZYNQMP_DMA_NUM_DESCS; i++) {
  438. desc = chan->sw_desc_pool + i;
  439. desc->src_v = (struct zynqmp_dma_desc_ll *) (chan->desc_pool_v +
  440. (i * ZYNQMP_DMA_DESC_SIZE(chan) * 2));
  441. desc->dst_v = (struct zynqmp_dma_desc_ll *) (desc->src_v + 1);
  442. desc->src_p = chan->desc_pool_p +
  443. (i * ZYNQMP_DMA_DESC_SIZE(chan) * 2);
  444. desc->dst_p = desc->src_p + ZYNQMP_DMA_DESC_SIZE(chan);
  445. }
  446. return ZYNQMP_DMA_NUM_DESCS;
  447. }
  448. /**
  449. * zynqmp_dma_start - Start DMA channel
  450. * @chan: ZynqMP DMA channel pointer
  451. */
  452. static void zynqmp_dma_start(struct zynqmp_dma_chan *chan)
  453. {
  454. writel(ZYNQMP_DMA_INT_EN_DEFAULT_MASK, chan->regs + ZYNQMP_DMA_IER);
  455. writel(0, chan->regs + ZYNQMP_DMA_TOTAL_BYTE);
  456. chan->idle = false;
  457. writel(ZYNQMP_DMA_ENABLE, chan->regs + ZYNQMP_DMA_CTRL2);
  458. }
  459. /**
  460. * zynqmp_dma_handle_ovfl_int - Process the overflow interrupt
  461. * @chan: ZynqMP DMA channel pointer
  462. * @status: Interrupt status value
  463. */
  464. static void zynqmp_dma_handle_ovfl_int(struct zynqmp_dma_chan *chan, u32 status)
  465. {
  466. if (status & ZYNQMP_DMA_BYTE_CNT_OVRFL)
  467. writel(0, chan->regs + ZYNQMP_DMA_TOTAL_BYTE);
  468. if (status & ZYNQMP_DMA_IRQ_DST_ACCT_ERR)
  469. readl(chan->regs + ZYNQMP_DMA_IRQ_DST_ACCT);
  470. if (status & ZYNQMP_DMA_IRQ_SRC_ACCT_ERR)
  471. readl(chan->regs + ZYNQMP_DMA_IRQ_SRC_ACCT);
  472. }
  473. static void zynqmp_dma_config(struct zynqmp_dma_chan *chan)
  474. {
  475. u32 val;
  476. val = readl(chan->regs + ZYNQMP_DMA_CTRL0);
  477. val |= ZYNQMP_DMA_POINT_TYPE_SG;
  478. writel(val, chan->regs + ZYNQMP_DMA_CTRL0);
  479. val = readl(chan->regs + ZYNQMP_DMA_DATA_ATTR);
  480. val = (val & ~ZYNQMP_DMA_ARLEN) |
  481. (chan->src_burst_len << ZYNQMP_DMA_ARLEN_OFST);
  482. val = (val & ~ZYNQMP_DMA_AWLEN) |
  483. (chan->dst_burst_len << ZYNQMP_DMA_AWLEN_OFST);
  484. writel(val, chan->regs + ZYNQMP_DMA_DATA_ATTR);
  485. }
  486. /**
  487. * zynqmp_dma_device_config - Zynqmp dma device configuration
  488. * @dchan: DMA channel
  489. * @config: DMA device config
  490. *
  491. * Return: 0 always
  492. */
  493. static int zynqmp_dma_device_config(struct dma_chan *dchan,
  494. struct dma_slave_config *config)
  495. {
  496. struct zynqmp_dma_chan *chan = to_chan(dchan);
  497. chan->src_burst_len = config->src_maxburst;
  498. chan->dst_burst_len = config->dst_maxburst;
  499. return 0;
  500. }
  501. /**
  502. * zynqmp_dma_start_transfer - Initiate the new transfer
  503. * @chan: ZynqMP DMA channel pointer
  504. */
  505. static void zynqmp_dma_start_transfer(struct zynqmp_dma_chan *chan)
  506. {
  507. struct zynqmp_dma_desc_sw *desc;
  508. if (!chan->idle)
  509. return;
  510. zynqmp_dma_config(chan);
  511. desc = list_first_entry_or_null(&chan->pending_list,
  512. struct zynqmp_dma_desc_sw, node);
  513. if (!desc)
  514. return;
  515. list_splice_tail_init(&chan->pending_list, &chan->active_list);
  516. zynqmp_dma_update_desc_to_ctrlr(chan, desc);
  517. zynqmp_dma_start(chan);
  518. }
  519. /**
  520. * zynqmp_dma_chan_desc_cleanup - Cleanup the completed descriptors
  521. * @chan: ZynqMP DMA channel
  522. */
  523. static void zynqmp_dma_chan_desc_cleanup(struct zynqmp_dma_chan *chan)
  524. {
  525. struct zynqmp_dma_desc_sw *desc, *next;
  526. list_for_each_entry_safe(desc, next, &chan->done_list, node) {
  527. dma_async_tx_callback callback;
  528. void *callback_param;
  529. list_del(&desc->node);
  530. callback = desc->async_tx.callback;
  531. callback_param = desc->async_tx.callback_param;
  532. if (callback) {
  533. spin_unlock(&chan->lock);
  534. callback(callback_param);
  535. spin_lock(&chan->lock);
  536. }
  537. /* Run any dependencies, then free the descriptor */
  538. zynqmp_dma_free_descriptor(chan, desc);
  539. }
  540. }
  541. /**
  542. * zynqmp_dma_complete_descriptor - Mark the active descriptor as complete
  543. * @chan: ZynqMP DMA channel pointer
  544. */
  545. static void zynqmp_dma_complete_descriptor(struct zynqmp_dma_chan *chan)
  546. {
  547. struct zynqmp_dma_desc_sw *desc;
  548. desc = list_first_entry_or_null(&chan->active_list,
  549. struct zynqmp_dma_desc_sw, node);
  550. if (!desc)
  551. return;
  552. list_del(&desc->node);
  553. dma_cookie_complete(&desc->async_tx);
  554. list_add_tail(&desc->node, &chan->done_list);
  555. }
  556. /**
  557. * zynqmp_dma_issue_pending - Issue pending transactions
  558. * @dchan: DMA channel pointer
  559. */
  560. static void zynqmp_dma_issue_pending(struct dma_chan *dchan)
  561. {
  562. struct zynqmp_dma_chan *chan = to_chan(dchan);
  563. spin_lock_bh(&chan->lock);
  564. zynqmp_dma_start_transfer(chan);
  565. spin_unlock_bh(&chan->lock);
  566. }
  567. /**
  568. * zynqmp_dma_free_descriptors - Free channel descriptors
  569. * @chan: ZynqMP DMA channel pointer
  570. */
  571. static void zynqmp_dma_free_descriptors(struct zynqmp_dma_chan *chan)
  572. {
  573. zynqmp_dma_free_desc_list(chan, &chan->active_list);
  574. zynqmp_dma_free_desc_list(chan, &chan->pending_list);
  575. zynqmp_dma_free_desc_list(chan, &chan->done_list);
  576. }
  577. /**
  578. * zynqmp_dma_free_chan_resources - Free channel resources
  579. * @dchan: DMA channel pointer
  580. */
  581. static void zynqmp_dma_free_chan_resources(struct dma_chan *dchan)
  582. {
  583. struct zynqmp_dma_chan *chan = to_chan(dchan);
  584. spin_lock_bh(&chan->lock);
  585. zynqmp_dma_free_descriptors(chan);
  586. spin_unlock_bh(&chan->lock);
  587. dma_free_coherent(chan->dev,
  588. (2 * ZYNQMP_DMA_DESC_SIZE(chan) * ZYNQMP_DMA_NUM_DESCS),
  589. chan->desc_pool_v, chan->desc_pool_p);
  590. kfree(chan->sw_desc_pool);
  591. pm_runtime_mark_last_busy(chan->dev);
  592. pm_runtime_put_autosuspend(chan->dev);
  593. }
  594. /**
  595. * zynqmp_dma_reset - Reset the channel
  596. * @chan: ZynqMP DMA channel pointer
  597. */
  598. static void zynqmp_dma_reset(struct zynqmp_dma_chan *chan)
  599. {
  600. writel(ZYNQMP_DMA_IDS_DEFAULT_MASK, chan->regs + ZYNQMP_DMA_IDS);
  601. zynqmp_dma_complete_descriptor(chan);
  602. zynqmp_dma_chan_desc_cleanup(chan);
  603. zynqmp_dma_free_descriptors(chan);
  604. zynqmp_dma_init(chan);
  605. }
  606. /**
  607. * zynqmp_dma_irq_handler - ZynqMP DMA Interrupt handler
  608. * @irq: IRQ number
  609. * @data: Pointer to the ZynqMP DMA channel structure
  610. *
  611. * Return: IRQ_HANDLED/IRQ_NONE
  612. */
  613. static irqreturn_t zynqmp_dma_irq_handler(int irq, void *data)
  614. {
  615. struct zynqmp_dma_chan *chan = (struct zynqmp_dma_chan *)data;
  616. u32 isr, imr, status;
  617. irqreturn_t ret = IRQ_NONE;
  618. isr = readl(chan->regs + ZYNQMP_DMA_ISR);
  619. imr = readl(chan->regs + ZYNQMP_DMA_IMR);
  620. status = isr & ~imr;
  621. writel(isr, chan->regs + ZYNQMP_DMA_ISR);
  622. if (status & ZYNQMP_DMA_INT_DONE) {
  623. tasklet_schedule(&chan->tasklet);
  624. ret = IRQ_HANDLED;
  625. }
  626. if (status & ZYNQMP_DMA_DONE)
  627. chan->idle = true;
  628. if (status & ZYNQMP_DMA_INT_ERR) {
  629. chan->err = true;
  630. tasklet_schedule(&chan->tasklet);
  631. dev_err(chan->dev, "Channel %p has errors\n", chan);
  632. ret = IRQ_HANDLED;
  633. }
  634. if (status & ZYNQMP_DMA_INT_OVRFL) {
  635. zynqmp_dma_handle_ovfl_int(chan, status);
  636. dev_dbg(chan->dev, "Channel %p overflow interrupt\n", chan);
  637. ret = IRQ_HANDLED;
  638. }
  639. return ret;
  640. }
  641. /**
  642. * zynqmp_dma_do_tasklet - Schedule completion tasklet
  643. * @data: Pointer to the ZynqMP DMA channel structure
  644. */
  645. static void zynqmp_dma_do_tasklet(unsigned long data)
  646. {
  647. struct zynqmp_dma_chan *chan = (struct zynqmp_dma_chan *)data;
  648. u32 count;
  649. spin_lock(&chan->lock);
  650. if (chan->err) {
  651. zynqmp_dma_reset(chan);
  652. chan->err = false;
  653. goto unlock;
  654. }
  655. count = readl(chan->regs + ZYNQMP_DMA_IRQ_DST_ACCT);
  656. while (count) {
  657. zynqmp_dma_complete_descriptor(chan);
  658. zynqmp_dma_chan_desc_cleanup(chan);
  659. count--;
  660. }
  661. if (chan->idle)
  662. zynqmp_dma_start_transfer(chan);
  663. unlock:
  664. spin_unlock(&chan->lock);
  665. }
  666. /**
  667. * zynqmp_dma_device_terminate_all - Aborts all transfers on a channel
  668. * @dchan: DMA channel pointer
  669. *
  670. * Return: Always '0'
  671. */
  672. static int zynqmp_dma_device_terminate_all(struct dma_chan *dchan)
  673. {
  674. struct zynqmp_dma_chan *chan = to_chan(dchan);
  675. spin_lock_bh(&chan->lock);
  676. writel(ZYNQMP_DMA_IDS_DEFAULT_MASK, chan->regs + ZYNQMP_DMA_IDS);
  677. zynqmp_dma_free_descriptors(chan);
  678. spin_unlock_bh(&chan->lock);
  679. return 0;
  680. }
  681. /**
  682. * zynqmp_dma_prep_memcpy - prepare descriptors for memcpy transaction
  683. * @dchan: DMA channel
  684. * @dma_dst: Destination buffer address
  685. * @dma_src: Source buffer address
  686. * @len: Transfer length
  687. * @flags: transfer ack flags
  688. *
  689. * Return: Async transaction descriptor on success and NULL on failure
  690. */
  691. static struct dma_async_tx_descriptor *zynqmp_dma_prep_memcpy(
  692. struct dma_chan *dchan, dma_addr_t dma_dst,
  693. dma_addr_t dma_src, size_t len, ulong flags)
  694. {
  695. struct zynqmp_dma_chan *chan;
  696. struct zynqmp_dma_desc_sw *new, *first = NULL;
  697. void *desc = NULL, *prev = NULL;
  698. size_t copy;
  699. u32 desc_cnt;
  700. chan = to_chan(dchan);
  701. desc_cnt = DIV_ROUND_UP(len, ZYNQMP_DMA_MAX_TRANS_LEN);
  702. spin_lock_bh(&chan->lock);
  703. if (desc_cnt > chan->desc_free_cnt) {
  704. spin_unlock_bh(&chan->lock);
  705. dev_dbg(chan->dev, "chan %p descs are not available\n", chan);
  706. return NULL;
  707. }
  708. chan->desc_free_cnt = chan->desc_free_cnt - desc_cnt;
  709. spin_unlock_bh(&chan->lock);
  710. do {
  711. /* Allocate and populate the descriptor */
  712. new = zynqmp_dma_get_descriptor(chan);
  713. copy = min_t(size_t, len, ZYNQMP_DMA_MAX_TRANS_LEN);
  714. desc = (struct zynqmp_dma_desc_ll *)new->src_v;
  715. zynqmp_dma_config_sg_ll_desc(chan, desc, dma_src,
  716. dma_dst, copy, prev);
  717. prev = desc;
  718. len -= copy;
  719. dma_src += copy;
  720. dma_dst += copy;
  721. if (!first)
  722. first = new;
  723. else
  724. list_add_tail(&new->node, &first->tx_list);
  725. } while (len);
  726. zynqmp_dma_desc_config_eod(chan, desc);
  727. async_tx_ack(&first->async_tx);
  728. first->async_tx.flags = flags;
  729. return &first->async_tx;
  730. }
  731. /**
  732. * zynqmp_dma_chan_remove - Channel remove function
  733. * @chan: ZynqMP DMA channel pointer
  734. */
  735. static void zynqmp_dma_chan_remove(struct zynqmp_dma_chan *chan)
  736. {
  737. if (!chan)
  738. return;
  739. if (chan->irq)
  740. devm_free_irq(chan->zdev->dev, chan->irq, chan);
  741. tasklet_kill(&chan->tasklet);
  742. list_del(&chan->common.device_node);
  743. }
  744. /**
  745. * zynqmp_dma_chan_probe - Per Channel Probing
  746. * @zdev: Driver specific device structure
  747. * @pdev: Pointer to the platform_device structure
  748. *
  749. * Return: '0' on success and failure value on error
  750. */
  751. static int zynqmp_dma_chan_probe(struct zynqmp_dma_device *zdev,
  752. struct platform_device *pdev)
  753. {
  754. struct zynqmp_dma_chan *chan;
  755. struct resource *res;
  756. struct device_node *node = pdev->dev.of_node;
  757. int err;
  758. chan = devm_kzalloc(zdev->dev, sizeof(*chan), GFP_KERNEL);
  759. if (!chan)
  760. return -ENOMEM;
  761. chan->dev = zdev->dev;
  762. chan->zdev = zdev;
  763. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  764. chan->regs = devm_ioremap_resource(&pdev->dev, res);
  765. if (IS_ERR(chan->regs))
  766. return PTR_ERR(chan->regs);
  767. chan->bus_width = ZYNQMP_DMA_BUS_WIDTH_64;
  768. chan->dst_burst_len = ZYNQMP_DMA_AWLEN_RST_VAL;
  769. chan->src_burst_len = ZYNQMP_DMA_ARLEN_RST_VAL;
  770. err = of_property_read_u32(node, "xlnx,bus-width", &chan->bus_width);
  771. if (err < 0) {
  772. dev_err(&pdev->dev, "missing xlnx,bus-width property\n");
  773. return err;
  774. }
  775. if (chan->bus_width != ZYNQMP_DMA_BUS_WIDTH_64 &&
  776. chan->bus_width != ZYNQMP_DMA_BUS_WIDTH_128) {
  777. dev_err(zdev->dev, "invalid bus-width value");
  778. return -EINVAL;
  779. }
  780. chan->is_dmacoherent = of_property_read_bool(node, "dma-coherent");
  781. zdev->chan = chan;
  782. tasklet_init(&chan->tasklet, zynqmp_dma_do_tasklet, (ulong)chan);
  783. spin_lock_init(&chan->lock);
  784. INIT_LIST_HEAD(&chan->active_list);
  785. INIT_LIST_HEAD(&chan->pending_list);
  786. INIT_LIST_HEAD(&chan->done_list);
  787. INIT_LIST_HEAD(&chan->free_list);
  788. dma_cookie_init(&chan->common);
  789. chan->common.device = &zdev->common;
  790. list_add_tail(&chan->common.device_node, &zdev->common.channels);
  791. zynqmp_dma_init(chan);
  792. chan->irq = platform_get_irq(pdev, 0);
  793. if (chan->irq < 0)
  794. return -ENXIO;
  795. err = devm_request_irq(&pdev->dev, chan->irq, zynqmp_dma_irq_handler, 0,
  796. "zynqmp-dma", chan);
  797. if (err)
  798. return err;
  799. chan->desc_size = sizeof(struct zynqmp_dma_desc_ll);
  800. chan->idle = true;
  801. return 0;
  802. }
  803. /**
  804. * of_zynqmp_dma_xlate - Translation function
  805. * @dma_spec: Pointer to DMA specifier as found in the device tree
  806. * @ofdma: Pointer to DMA controller data
  807. *
  808. * Return: DMA channel pointer on success and NULL on error
  809. */
  810. static struct dma_chan *of_zynqmp_dma_xlate(struct of_phandle_args *dma_spec,
  811. struct of_dma *ofdma)
  812. {
  813. struct zynqmp_dma_device *zdev = ofdma->of_dma_data;
  814. return dma_get_slave_channel(&zdev->chan->common);
  815. }
  816. /**
  817. * zynqmp_dma_suspend - Suspend method for the driver
  818. * @dev: Address of the device structure
  819. *
  820. * Put the driver into low power mode.
  821. * Return: 0 on success and failure value on error
  822. */
  823. static int __maybe_unused zynqmp_dma_suspend(struct device *dev)
  824. {
  825. if (!device_may_wakeup(dev))
  826. return pm_runtime_force_suspend(dev);
  827. return 0;
  828. }
  829. /**
  830. * zynqmp_dma_resume - Resume from suspend
  831. * @dev: Address of the device structure
  832. *
  833. * Resume operation after suspend.
  834. * Return: 0 on success and failure value on error
  835. */
  836. static int __maybe_unused zynqmp_dma_resume(struct device *dev)
  837. {
  838. if (!device_may_wakeup(dev))
  839. return pm_runtime_force_resume(dev);
  840. return 0;
  841. }
  842. /**
  843. * zynqmp_dma_runtime_suspend - Runtime suspend method for the driver
  844. * @dev: Address of the device structure
  845. *
  846. * Put the driver into low power mode.
  847. * Return: 0 always
  848. */
  849. static int __maybe_unused zynqmp_dma_runtime_suspend(struct device *dev)
  850. {
  851. struct zynqmp_dma_device *zdev = dev_get_drvdata(dev);
  852. clk_disable_unprepare(zdev->clk_main);
  853. clk_disable_unprepare(zdev->clk_apb);
  854. return 0;
  855. }
  856. /**
  857. * zynqmp_dma_runtime_resume - Runtime suspend method for the driver
  858. * @dev: Address of the device structure
  859. *
  860. * Put the driver into low power mode.
  861. * Return: 0 always
  862. */
  863. static int __maybe_unused zynqmp_dma_runtime_resume(struct device *dev)
  864. {
  865. struct zynqmp_dma_device *zdev = dev_get_drvdata(dev);
  866. int err;
  867. err = clk_prepare_enable(zdev->clk_main);
  868. if (err) {
  869. dev_err(dev, "Unable to enable main clock.\n");
  870. return err;
  871. }
  872. err = clk_prepare_enable(zdev->clk_apb);
  873. if (err) {
  874. dev_err(dev, "Unable to enable apb clock.\n");
  875. clk_disable_unprepare(zdev->clk_main);
  876. return err;
  877. }
  878. return 0;
  879. }
  880. static const struct dev_pm_ops zynqmp_dma_dev_pm_ops = {
  881. SET_SYSTEM_SLEEP_PM_OPS(zynqmp_dma_suspend, zynqmp_dma_resume)
  882. SET_RUNTIME_PM_OPS(zynqmp_dma_runtime_suspend,
  883. zynqmp_dma_runtime_resume, NULL)
  884. };
  885. /**
  886. * zynqmp_dma_probe - Driver probe function
  887. * @pdev: Pointer to the platform_device structure
  888. *
  889. * Return: '0' on success and failure value on error
  890. */
  891. static int zynqmp_dma_probe(struct platform_device *pdev)
  892. {
  893. struct zynqmp_dma_device *zdev;
  894. struct dma_device *p;
  895. int ret;
  896. zdev = devm_kzalloc(&pdev->dev, sizeof(*zdev), GFP_KERNEL);
  897. if (!zdev)
  898. return -ENOMEM;
  899. zdev->dev = &pdev->dev;
  900. INIT_LIST_HEAD(&zdev->common.channels);
  901. dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
  902. dma_cap_set(DMA_MEMCPY, zdev->common.cap_mask);
  903. p = &zdev->common;
  904. p->device_prep_dma_memcpy = zynqmp_dma_prep_memcpy;
  905. p->device_terminate_all = zynqmp_dma_device_terminate_all;
  906. p->device_issue_pending = zynqmp_dma_issue_pending;
  907. p->device_alloc_chan_resources = zynqmp_dma_alloc_chan_resources;
  908. p->device_free_chan_resources = zynqmp_dma_free_chan_resources;
  909. p->device_tx_status = dma_cookie_status;
  910. p->device_config = zynqmp_dma_device_config;
  911. p->dev = &pdev->dev;
  912. zdev->clk_main = devm_clk_get(&pdev->dev, "clk_main");
  913. if (IS_ERR(zdev->clk_main)) {
  914. dev_err(&pdev->dev, "main clock not found.\n");
  915. return PTR_ERR(zdev->clk_main);
  916. }
  917. zdev->clk_apb = devm_clk_get(&pdev->dev, "clk_apb");
  918. if (IS_ERR(zdev->clk_apb)) {
  919. dev_err(&pdev->dev, "apb clock not found.\n");
  920. return PTR_ERR(zdev->clk_apb);
  921. }
  922. platform_set_drvdata(pdev, zdev);
  923. pm_runtime_set_autosuspend_delay(zdev->dev, ZDMA_PM_TIMEOUT);
  924. pm_runtime_use_autosuspend(zdev->dev);
  925. pm_runtime_enable(zdev->dev);
  926. pm_runtime_get_sync(zdev->dev);
  927. if (!pm_runtime_enabled(zdev->dev)) {
  928. ret = zynqmp_dma_runtime_resume(zdev->dev);
  929. if (ret)
  930. return ret;
  931. }
  932. ret = zynqmp_dma_chan_probe(zdev, pdev);
  933. if (ret) {
  934. dev_err(&pdev->dev, "Probing channel failed\n");
  935. goto err_disable_pm;
  936. }
  937. p->dst_addr_widths = BIT(zdev->chan->bus_width / 8);
  938. p->src_addr_widths = BIT(zdev->chan->bus_width / 8);
  939. dma_async_device_register(&zdev->common);
  940. ret = of_dma_controller_register(pdev->dev.of_node,
  941. of_zynqmp_dma_xlate, zdev);
  942. if (ret) {
  943. dev_err(&pdev->dev, "Unable to register DMA to DT\n");
  944. dma_async_device_unregister(&zdev->common);
  945. goto free_chan_resources;
  946. }
  947. pm_runtime_mark_last_busy(zdev->dev);
  948. pm_runtime_put_sync_autosuspend(zdev->dev);
  949. dev_info(&pdev->dev, "ZynqMP DMA driver Probe success\n");
  950. return 0;
  951. free_chan_resources:
  952. zynqmp_dma_chan_remove(zdev->chan);
  953. err_disable_pm:
  954. if (!pm_runtime_enabled(zdev->dev))
  955. zynqmp_dma_runtime_suspend(zdev->dev);
  956. pm_runtime_disable(zdev->dev);
  957. return ret;
  958. }
  959. /**
  960. * zynqmp_dma_remove - Driver remove function
  961. * @pdev: Pointer to the platform_device structure
  962. *
  963. * Return: Always '0'
  964. */
  965. static int zynqmp_dma_remove(struct platform_device *pdev)
  966. {
  967. struct zynqmp_dma_device *zdev = platform_get_drvdata(pdev);
  968. of_dma_controller_free(pdev->dev.of_node);
  969. dma_async_device_unregister(&zdev->common);
  970. zynqmp_dma_chan_remove(zdev->chan);
  971. pm_runtime_disable(zdev->dev);
  972. if (!pm_runtime_enabled(zdev->dev))
  973. zynqmp_dma_runtime_suspend(zdev->dev);
  974. return 0;
  975. }
  976. static const struct of_device_id zynqmp_dma_of_match[] = {
  977. { .compatible = "xlnx,zynqmp-dma-1.0", },
  978. {}
  979. };
  980. MODULE_DEVICE_TABLE(of, zynqmp_dma_of_match);
  981. static struct platform_driver zynqmp_dma_driver = {
  982. .driver = {
  983. .name = "xilinx-zynqmp-dma",
  984. .of_match_table = zynqmp_dma_of_match,
  985. .pm = &zynqmp_dma_dev_pm_ops,
  986. },
  987. .probe = zynqmp_dma_probe,
  988. .remove = zynqmp_dma_remove,
  989. };
  990. module_platform_driver(zynqmp_dma_driver);
  991. MODULE_LICENSE("GPL");
  992. MODULE_AUTHOR("Xilinx, Inc.");
  993. MODULE_DESCRIPTION("Xilinx ZynqMP DMA driver");