xilinx_vdma.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /*
  2. * DMA driver for Xilinx Video DMA Engine
  3. *
  4. * Copyright (C) 2010-2014 Xilinx, Inc. All rights reserved.
  5. *
  6. * Based on the Freescale DMA driver.
  7. *
  8. * Description:
  9. * The AXI Video Direct Memory Access (AXI VDMA) core is a soft Xilinx IP
  10. * core that provides high-bandwidth direct memory access between memory
  11. * and AXI4-Stream type video target peripherals. The core provides efficient
  12. * two dimensional DMA operations with independent asynchronous read (S2MM)
  13. * and write (MM2S) channel operation. It can be configured to have either
  14. * one channel or two channels. If configured as two channels, one is to
  15. * transmit to the video device (MM2S) and another is to receive from the
  16. * video device (S2MM). Initialization, status, interrupt and management
  17. * registers are accessed through an AXI4-Lite slave interface.
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation, either version 2 of the License, or
  22. * (at your option) any later version.
  23. */
  24. #include <linux/bitops.h>
  25. #include <linux/dmapool.h>
  26. #include <linux/dma/xilinx_dma.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/io.h>
  30. #include <linux/iopoll.h>
  31. #include <linux/module.h>
  32. #include <linux/of_address.h>
  33. #include <linux/of_dma.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/of_irq.h>
  36. #include <linux/slab.h>
  37. #include "../dmaengine.h"
  38. /* Register/Descriptor Offsets */
  39. #define XILINX_VDMA_MM2S_CTRL_OFFSET 0x0000
  40. #define XILINX_VDMA_S2MM_CTRL_OFFSET 0x0030
  41. #define XILINX_VDMA_MM2S_DESC_OFFSET 0x0050
  42. #define XILINX_VDMA_S2MM_DESC_OFFSET 0x00a0
  43. /* Control Registers */
  44. #define XILINX_VDMA_REG_DMACR 0x0000
  45. #define XILINX_VDMA_DMACR_DELAY_MAX 0xff
  46. #define XILINX_VDMA_DMACR_DELAY_SHIFT 24
  47. #define XILINX_VDMA_DMACR_FRAME_COUNT_MAX 0xff
  48. #define XILINX_VDMA_DMACR_FRAME_COUNT_SHIFT 16
  49. #define XILINX_VDMA_DMACR_ERR_IRQ BIT(14)
  50. #define XILINX_VDMA_DMACR_DLY_CNT_IRQ BIT(13)
  51. #define XILINX_VDMA_DMACR_FRM_CNT_IRQ BIT(12)
  52. #define XILINX_VDMA_DMACR_MASTER_SHIFT 8
  53. #define XILINX_VDMA_DMACR_FSYNCSRC_SHIFT 5
  54. #define XILINX_VDMA_DMACR_FRAMECNT_EN BIT(4)
  55. #define XILINX_VDMA_DMACR_GENLOCK_EN BIT(3)
  56. #define XILINX_VDMA_DMACR_RESET BIT(2)
  57. #define XILINX_VDMA_DMACR_CIRC_EN BIT(1)
  58. #define XILINX_VDMA_DMACR_RUNSTOP BIT(0)
  59. #define XILINX_VDMA_DMACR_FSYNCSRC_MASK GENMASK(6, 5)
  60. #define XILINX_VDMA_REG_DMASR 0x0004
  61. #define XILINX_VDMA_DMASR_EOL_LATE_ERR BIT(15)
  62. #define XILINX_VDMA_DMASR_ERR_IRQ BIT(14)
  63. #define XILINX_VDMA_DMASR_DLY_CNT_IRQ BIT(13)
  64. #define XILINX_VDMA_DMASR_FRM_CNT_IRQ BIT(12)
  65. #define XILINX_VDMA_DMASR_SOF_LATE_ERR BIT(11)
  66. #define XILINX_VDMA_DMASR_SG_DEC_ERR BIT(10)
  67. #define XILINX_VDMA_DMASR_SG_SLV_ERR BIT(9)
  68. #define XILINX_VDMA_DMASR_EOF_EARLY_ERR BIT(8)
  69. #define XILINX_VDMA_DMASR_SOF_EARLY_ERR BIT(7)
  70. #define XILINX_VDMA_DMASR_DMA_DEC_ERR BIT(6)
  71. #define XILINX_VDMA_DMASR_DMA_SLAVE_ERR BIT(5)
  72. #define XILINX_VDMA_DMASR_DMA_INT_ERR BIT(4)
  73. #define XILINX_VDMA_DMASR_IDLE BIT(1)
  74. #define XILINX_VDMA_DMASR_HALTED BIT(0)
  75. #define XILINX_VDMA_DMASR_DELAY_MASK GENMASK(31, 24)
  76. #define XILINX_VDMA_DMASR_FRAME_COUNT_MASK GENMASK(23, 16)
  77. #define XILINX_VDMA_REG_CURDESC 0x0008
  78. #define XILINX_VDMA_REG_TAILDESC 0x0010
  79. #define XILINX_VDMA_REG_REG_INDEX 0x0014
  80. #define XILINX_VDMA_REG_FRMSTORE 0x0018
  81. #define XILINX_VDMA_REG_THRESHOLD 0x001c
  82. #define XILINX_VDMA_REG_FRMPTR_STS 0x0024
  83. #define XILINX_VDMA_REG_PARK_PTR 0x0028
  84. #define XILINX_VDMA_PARK_PTR_WR_REF_SHIFT 8
  85. #define XILINX_VDMA_PARK_PTR_RD_REF_SHIFT 0
  86. #define XILINX_VDMA_REG_VDMA_VERSION 0x002c
  87. /* Register Direct Mode Registers */
  88. #define XILINX_VDMA_REG_VSIZE 0x0000
  89. #define XILINX_VDMA_REG_HSIZE 0x0004
  90. #define XILINX_VDMA_REG_FRMDLY_STRIDE 0x0008
  91. #define XILINX_VDMA_FRMDLY_STRIDE_FRMDLY_SHIFT 24
  92. #define XILINX_VDMA_FRMDLY_STRIDE_STRIDE_SHIFT 0
  93. #define XILINX_VDMA_REG_START_ADDRESS(n) (0x000c + 4 * (n))
  94. /* HW specific definitions */
  95. #define XILINX_VDMA_MAX_CHANS_PER_DEVICE 0x2
  96. #define XILINX_VDMA_DMAXR_ALL_IRQ_MASK \
  97. (XILINX_VDMA_DMASR_FRM_CNT_IRQ | \
  98. XILINX_VDMA_DMASR_DLY_CNT_IRQ | \
  99. XILINX_VDMA_DMASR_ERR_IRQ)
  100. #define XILINX_VDMA_DMASR_ALL_ERR_MASK \
  101. (XILINX_VDMA_DMASR_EOL_LATE_ERR | \
  102. XILINX_VDMA_DMASR_SOF_LATE_ERR | \
  103. XILINX_VDMA_DMASR_SG_DEC_ERR | \
  104. XILINX_VDMA_DMASR_SG_SLV_ERR | \
  105. XILINX_VDMA_DMASR_EOF_EARLY_ERR | \
  106. XILINX_VDMA_DMASR_SOF_EARLY_ERR | \
  107. XILINX_VDMA_DMASR_DMA_DEC_ERR | \
  108. XILINX_VDMA_DMASR_DMA_SLAVE_ERR | \
  109. XILINX_VDMA_DMASR_DMA_INT_ERR)
  110. /*
  111. * Recoverable errors are DMA Internal error, SOF Early, EOF Early
  112. * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
  113. * is enabled in the h/w system.
  114. */
  115. #define XILINX_VDMA_DMASR_ERR_RECOVER_MASK \
  116. (XILINX_VDMA_DMASR_SOF_LATE_ERR | \
  117. XILINX_VDMA_DMASR_EOF_EARLY_ERR | \
  118. XILINX_VDMA_DMASR_SOF_EARLY_ERR | \
  119. XILINX_VDMA_DMASR_DMA_INT_ERR)
  120. /* Axi VDMA Flush on Fsync bits */
  121. #define XILINX_VDMA_FLUSH_S2MM 3
  122. #define XILINX_VDMA_FLUSH_MM2S 2
  123. #define XILINX_VDMA_FLUSH_BOTH 1
  124. /* Delay loop counter to prevent hardware failure */
  125. #define XILINX_VDMA_LOOP_COUNT 1000000
  126. /**
  127. * struct xilinx_vdma_desc_hw - Hardware Descriptor
  128. * @next_desc: Next Descriptor Pointer @0x00
  129. * @pad1: Reserved @0x04
  130. * @buf_addr: Buffer address @0x08
  131. * @pad2: Reserved @0x0C
  132. * @vsize: Vertical Size @0x10
  133. * @hsize: Horizontal Size @0x14
  134. * @stride: Number of bytes between the first
  135. * pixels of each horizontal line @0x18
  136. */
  137. struct xilinx_vdma_desc_hw {
  138. u32 next_desc;
  139. u32 pad1;
  140. u32 buf_addr;
  141. u32 pad2;
  142. u32 vsize;
  143. u32 hsize;
  144. u32 stride;
  145. } __aligned(64);
  146. /**
  147. * struct xilinx_vdma_tx_segment - Descriptor segment
  148. * @hw: Hardware descriptor
  149. * @node: Node in the descriptor segments list
  150. * @phys: Physical address of segment
  151. */
  152. struct xilinx_vdma_tx_segment {
  153. struct xilinx_vdma_desc_hw hw;
  154. struct list_head node;
  155. dma_addr_t phys;
  156. } __aligned(64);
  157. /**
  158. * struct xilinx_vdma_tx_descriptor - Per Transaction structure
  159. * @async_tx: Async transaction descriptor
  160. * @segments: TX segments list
  161. * @node: Node in the channel descriptors list
  162. */
  163. struct xilinx_vdma_tx_descriptor {
  164. struct dma_async_tx_descriptor async_tx;
  165. struct list_head segments;
  166. struct list_head node;
  167. };
  168. /**
  169. * struct xilinx_vdma_chan - Driver specific VDMA channel structure
  170. * @xdev: Driver specific device structure
  171. * @ctrl_offset: Control registers offset
  172. * @desc_offset: TX descriptor registers offset
  173. * @lock: Descriptor operation lock
  174. * @pending_list: Descriptors waiting
  175. * @active_list: Descriptors ready to submit
  176. * @done_list: Complete descriptors
  177. * @common: DMA common channel
  178. * @desc_pool: Descriptors pool
  179. * @dev: The dma device
  180. * @irq: Channel IRQ
  181. * @id: Channel ID
  182. * @direction: Transfer direction
  183. * @num_frms: Number of frames
  184. * @has_sg: Support scatter transfers
  185. * @genlock: Support genlock mode
  186. * @err: Channel has errors
  187. * @tasklet: Cleanup work after irq
  188. * @config: Device configuration info
  189. * @flush_on_fsync: Flush on Frame sync
  190. * @desc_pendingcount: Descriptor pending count
  191. */
  192. struct xilinx_vdma_chan {
  193. struct xilinx_vdma_device *xdev;
  194. u32 ctrl_offset;
  195. u32 desc_offset;
  196. spinlock_t lock;
  197. struct list_head pending_list;
  198. struct list_head active_list;
  199. struct list_head done_list;
  200. struct dma_chan common;
  201. struct dma_pool *desc_pool;
  202. struct device *dev;
  203. int irq;
  204. int id;
  205. enum dma_transfer_direction direction;
  206. int num_frms;
  207. bool has_sg;
  208. bool genlock;
  209. bool err;
  210. struct tasklet_struct tasklet;
  211. struct xilinx_vdma_config config;
  212. bool flush_on_fsync;
  213. u32 desc_pendingcount;
  214. };
  215. /**
  216. * struct xilinx_vdma_device - VDMA device structure
  217. * @regs: I/O mapped base address
  218. * @dev: Device Structure
  219. * @common: DMA device structure
  220. * @chan: Driver specific VDMA channel
  221. * @has_sg: Specifies whether Scatter-Gather is present or not
  222. * @flush_on_fsync: Flush on frame sync
  223. */
  224. struct xilinx_vdma_device {
  225. void __iomem *regs;
  226. struct device *dev;
  227. struct dma_device common;
  228. struct xilinx_vdma_chan *chan[XILINX_VDMA_MAX_CHANS_PER_DEVICE];
  229. bool has_sg;
  230. u32 flush_on_fsync;
  231. };
  232. /* Macros */
  233. #define to_xilinx_chan(chan) \
  234. container_of(chan, struct xilinx_vdma_chan, common)
  235. #define to_vdma_tx_descriptor(tx) \
  236. container_of(tx, struct xilinx_vdma_tx_descriptor, async_tx)
  237. #define xilinx_vdma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
  238. readl_poll_timeout(chan->xdev->regs + chan->ctrl_offset + reg, val, \
  239. cond, delay_us, timeout_us)
  240. /* IO accessors */
  241. static inline u32 vdma_read(struct xilinx_vdma_chan *chan, u32 reg)
  242. {
  243. return ioread32(chan->xdev->regs + reg);
  244. }
  245. static inline void vdma_write(struct xilinx_vdma_chan *chan, u32 reg, u32 value)
  246. {
  247. iowrite32(value, chan->xdev->regs + reg);
  248. }
  249. static inline void vdma_desc_write(struct xilinx_vdma_chan *chan, u32 reg,
  250. u32 value)
  251. {
  252. vdma_write(chan, chan->desc_offset + reg, value);
  253. }
  254. static inline u32 vdma_ctrl_read(struct xilinx_vdma_chan *chan, u32 reg)
  255. {
  256. return vdma_read(chan, chan->ctrl_offset + reg);
  257. }
  258. static inline void vdma_ctrl_write(struct xilinx_vdma_chan *chan, u32 reg,
  259. u32 value)
  260. {
  261. vdma_write(chan, chan->ctrl_offset + reg, value);
  262. }
  263. static inline void vdma_ctrl_clr(struct xilinx_vdma_chan *chan, u32 reg,
  264. u32 clr)
  265. {
  266. vdma_ctrl_write(chan, reg, vdma_ctrl_read(chan, reg) & ~clr);
  267. }
  268. static inline void vdma_ctrl_set(struct xilinx_vdma_chan *chan, u32 reg,
  269. u32 set)
  270. {
  271. vdma_ctrl_write(chan, reg, vdma_ctrl_read(chan, reg) | set);
  272. }
  273. /* -----------------------------------------------------------------------------
  274. * Descriptors and segments alloc and free
  275. */
  276. /**
  277. * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
  278. * @chan: Driver specific VDMA channel
  279. *
  280. * Return: The allocated segment on success and NULL on failure.
  281. */
  282. static struct xilinx_vdma_tx_segment *
  283. xilinx_vdma_alloc_tx_segment(struct xilinx_vdma_chan *chan)
  284. {
  285. struct xilinx_vdma_tx_segment *segment;
  286. dma_addr_t phys;
  287. segment = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &phys);
  288. if (!segment)
  289. return NULL;
  290. memset(segment, 0, sizeof(*segment));
  291. segment->phys = phys;
  292. return segment;
  293. }
  294. /**
  295. * xilinx_vdma_free_tx_segment - Free transaction segment
  296. * @chan: Driver specific VDMA channel
  297. * @segment: VDMA transaction segment
  298. */
  299. static void xilinx_vdma_free_tx_segment(struct xilinx_vdma_chan *chan,
  300. struct xilinx_vdma_tx_segment *segment)
  301. {
  302. dma_pool_free(chan->desc_pool, segment, segment->phys);
  303. }
  304. /**
  305. * xilinx_vdma_tx_descriptor - Allocate transaction descriptor
  306. * @chan: Driver specific VDMA channel
  307. *
  308. * Return: The allocated descriptor on success and NULL on failure.
  309. */
  310. static struct xilinx_vdma_tx_descriptor *
  311. xilinx_vdma_alloc_tx_descriptor(struct xilinx_vdma_chan *chan)
  312. {
  313. struct xilinx_vdma_tx_descriptor *desc;
  314. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  315. if (!desc)
  316. return NULL;
  317. INIT_LIST_HEAD(&desc->segments);
  318. return desc;
  319. }
  320. /**
  321. * xilinx_vdma_free_tx_descriptor - Free transaction descriptor
  322. * @chan: Driver specific VDMA channel
  323. * @desc: VDMA transaction descriptor
  324. */
  325. static void
  326. xilinx_vdma_free_tx_descriptor(struct xilinx_vdma_chan *chan,
  327. struct xilinx_vdma_tx_descriptor *desc)
  328. {
  329. struct xilinx_vdma_tx_segment *segment, *next;
  330. if (!desc)
  331. return;
  332. list_for_each_entry_safe(segment, next, &desc->segments, node) {
  333. list_del(&segment->node);
  334. xilinx_vdma_free_tx_segment(chan, segment);
  335. }
  336. kfree(desc);
  337. }
  338. /* Required functions */
  339. /**
  340. * xilinx_vdma_free_desc_list - Free descriptors list
  341. * @chan: Driver specific VDMA channel
  342. * @list: List to parse and delete the descriptor
  343. */
  344. static void xilinx_vdma_free_desc_list(struct xilinx_vdma_chan *chan,
  345. struct list_head *list)
  346. {
  347. struct xilinx_vdma_tx_descriptor *desc, *next;
  348. list_for_each_entry_safe(desc, next, list, node) {
  349. list_del(&desc->node);
  350. xilinx_vdma_free_tx_descriptor(chan, desc);
  351. }
  352. }
  353. /**
  354. * xilinx_vdma_free_descriptors - Free channel descriptors
  355. * @chan: Driver specific VDMA channel
  356. */
  357. static void xilinx_vdma_free_descriptors(struct xilinx_vdma_chan *chan)
  358. {
  359. unsigned long flags;
  360. spin_lock_irqsave(&chan->lock, flags);
  361. xilinx_vdma_free_desc_list(chan, &chan->pending_list);
  362. xilinx_vdma_free_desc_list(chan, &chan->done_list);
  363. xilinx_vdma_free_desc_list(chan, &chan->active_list);
  364. spin_unlock_irqrestore(&chan->lock, flags);
  365. }
  366. /**
  367. * xilinx_vdma_free_chan_resources - Free channel resources
  368. * @dchan: DMA channel
  369. */
  370. static void xilinx_vdma_free_chan_resources(struct dma_chan *dchan)
  371. {
  372. struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
  373. dev_dbg(chan->dev, "Free all channel resources.\n");
  374. xilinx_vdma_free_descriptors(chan);
  375. dma_pool_destroy(chan->desc_pool);
  376. chan->desc_pool = NULL;
  377. }
  378. /**
  379. * xilinx_vdma_chan_desc_cleanup - Clean channel descriptors
  380. * @chan: Driver specific VDMA channel
  381. */
  382. static void xilinx_vdma_chan_desc_cleanup(struct xilinx_vdma_chan *chan)
  383. {
  384. struct xilinx_vdma_tx_descriptor *desc, *next;
  385. unsigned long flags;
  386. spin_lock_irqsave(&chan->lock, flags);
  387. list_for_each_entry_safe(desc, next, &chan->done_list, node) {
  388. dma_async_tx_callback callback;
  389. void *callback_param;
  390. /* Remove from the list of running transactions */
  391. list_del(&desc->node);
  392. /* Run the link descriptor callback function */
  393. callback = desc->async_tx.callback;
  394. callback_param = desc->async_tx.callback_param;
  395. if (callback) {
  396. spin_unlock_irqrestore(&chan->lock, flags);
  397. callback(callback_param);
  398. spin_lock_irqsave(&chan->lock, flags);
  399. }
  400. /* Run any dependencies, then free the descriptor */
  401. dma_run_dependencies(&desc->async_tx);
  402. xilinx_vdma_free_tx_descriptor(chan, desc);
  403. }
  404. spin_unlock_irqrestore(&chan->lock, flags);
  405. }
  406. /**
  407. * xilinx_vdma_do_tasklet - Schedule completion tasklet
  408. * @data: Pointer to the Xilinx VDMA channel structure
  409. */
  410. static void xilinx_vdma_do_tasklet(unsigned long data)
  411. {
  412. struct xilinx_vdma_chan *chan = (struct xilinx_vdma_chan *)data;
  413. xilinx_vdma_chan_desc_cleanup(chan);
  414. }
  415. /**
  416. * xilinx_vdma_alloc_chan_resources - Allocate channel resources
  417. * @dchan: DMA channel
  418. *
  419. * Return: '0' on success and failure value on error
  420. */
  421. static int xilinx_vdma_alloc_chan_resources(struct dma_chan *dchan)
  422. {
  423. struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
  424. /* Has this channel already been allocated? */
  425. if (chan->desc_pool)
  426. return 0;
  427. /*
  428. * We need the descriptor to be aligned to 64bytes
  429. * for meeting Xilinx VDMA specification requirement.
  430. */
  431. chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
  432. chan->dev,
  433. sizeof(struct xilinx_vdma_tx_segment),
  434. __alignof__(struct xilinx_vdma_tx_segment), 0);
  435. if (!chan->desc_pool) {
  436. dev_err(chan->dev,
  437. "unable to allocate channel %d descriptor pool\n",
  438. chan->id);
  439. return -ENOMEM;
  440. }
  441. dma_cookie_init(dchan);
  442. return 0;
  443. }
  444. /**
  445. * xilinx_vdma_tx_status - Get VDMA transaction status
  446. * @dchan: DMA channel
  447. * @cookie: Transaction identifier
  448. * @txstate: Transaction state
  449. *
  450. * Return: DMA transaction status
  451. */
  452. static enum dma_status xilinx_vdma_tx_status(struct dma_chan *dchan,
  453. dma_cookie_t cookie,
  454. struct dma_tx_state *txstate)
  455. {
  456. return dma_cookie_status(dchan, cookie, txstate);
  457. }
  458. /**
  459. * xilinx_vdma_is_running - Check if VDMA channel is running
  460. * @chan: Driver specific VDMA channel
  461. *
  462. * Return: '1' if running, '0' if not.
  463. */
  464. static bool xilinx_vdma_is_running(struct xilinx_vdma_chan *chan)
  465. {
  466. return !(vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR) &
  467. XILINX_VDMA_DMASR_HALTED) &&
  468. (vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR) &
  469. XILINX_VDMA_DMACR_RUNSTOP);
  470. }
  471. /**
  472. * xilinx_vdma_is_idle - Check if VDMA channel is idle
  473. * @chan: Driver specific VDMA channel
  474. *
  475. * Return: '1' if idle, '0' if not.
  476. */
  477. static bool xilinx_vdma_is_idle(struct xilinx_vdma_chan *chan)
  478. {
  479. return vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR) &
  480. XILINX_VDMA_DMASR_IDLE;
  481. }
  482. /**
  483. * xilinx_vdma_halt - Halt VDMA channel
  484. * @chan: Driver specific VDMA channel
  485. */
  486. static void xilinx_vdma_halt(struct xilinx_vdma_chan *chan)
  487. {
  488. int err;
  489. u32 val;
  490. vdma_ctrl_clr(chan, XILINX_VDMA_REG_DMACR, XILINX_VDMA_DMACR_RUNSTOP);
  491. /* Wait for the hardware to halt */
  492. err = xilinx_vdma_poll_timeout(chan, XILINX_VDMA_REG_DMASR, val,
  493. (val & XILINX_VDMA_DMASR_HALTED), 0,
  494. XILINX_VDMA_LOOP_COUNT);
  495. if (err) {
  496. dev_err(chan->dev, "Cannot stop channel %p: %x\n",
  497. chan, vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR));
  498. chan->err = true;
  499. }
  500. return;
  501. }
  502. /**
  503. * xilinx_vdma_start - Start VDMA channel
  504. * @chan: Driver specific VDMA channel
  505. */
  506. static void xilinx_vdma_start(struct xilinx_vdma_chan *chan)
  507. {
  508. int err;
  509. u32 val;
  510. vdma_ctrl_set(chan, XILINX_VDMA_REG_DMACR, XILINX_VDMA_DMACR_RUNSTOP);
  511. /* Wait for the hardware to start */
  512. err = xilinx_vdma_poll_timeout(chan, XILINX_VDMA_REG_DMASR, val,
  513. !(val & XILINX_VDMA_DMASR_HALTED), 0,
  514. XILINX_VDMA_LOOP_COUNT);
  515. if (err) {
  516. dev_err(chan->dev, "Cannot start channel %p: %x\n",
  517. chan, vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR));
  518. chan->err = true;
  519. }
  520. return;
  521. }
  522. /**
  523. * xilinx_vdma_start_transfer - Starts VDMA transfer
  524. * @chan: Driver specific channel struct pointer
  525. */
  526. static void xilinx_vdma_start_transfer(struct xilinx_vdma_chan *chan)
  527. {
  528. struct xilinx_vdma_config *config = &chan->config;
  529. struct xilinx_vdma_tx_descriptor *desc, *tail_desc;
  530. u32 reg;
  531. struct xilinx_vdma_tx_segment *tail_segment;
  532. /* This function was invoked with lock held */
  533. if (chan->err)
  534. return;
  535. if (list_empty(&chan->pending_list))
  536. return;
  537. desc = list_first_entry(&chan->pending_list,
  538. struct xilinx_vdma_tx_descriptor, node);
  539. tail_desc = list_last_entry(&chan->pending_list,
  540. struct xilinx_vdma_tx_descriptor, node);
  541. tail_segment = list_last_entry(&tail_desc->segments,
  542. struct xilinx_vdma_tx_segment, node);
  543. /* If it is SG mode and hardware is busy, cannot submit */
  544. if (chan->has_sg && xilinx_vdma_is_running(chan) &&
  545. !xilinx_vdma_is_idle(chan)) {
  546. dev_dbg(chan->dev, "DMA controller still busy\n");
  547. return;
  548. }
  549. /*
  550. * If hardware is idle, then all descriptors on the running lists are
  551. * done, start new transfers
  552. */
  553. if (chan->has_sg)
  554. vdma_ctrl_write(chan, XILINX_VDMA_REG_CURDESC,
  555. desc->async_tx.phys);
  556. /* Configure the hardware using info in the config structure */
  557. reg = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR);
  558. if (config->frm_cnt_en)
  559. reg |= XILINX_VDMA_DMACR_FRAMECNT_EN;
  560. else
  561. reg &= ~XILINX_VDMA_DMACR_FRAMECNT_EN;
  562. /* Configure channel to allow number frame buffers */
  563. vdma_ctrl_write(chan, XILINX_VDMA_REG_FRMSTORE,
  564. chan->desc_pendingcount);
  565. /*
  566. * With SG, start with circular mode, so that BDs can be fetched.
  567. * In direct register mode, if not parking, enable circular mode
  568. */
  569. if (chan->has_sg || !config->park)
  570. reg |= XILINX_VDMA_DMACR_CIRC_EN;
  571. if (config->park)
  572. reg &= ~XILINX_VDMA_DMACR_CIRC_EN;
  573. vdma_ctrl_write(chan, XILINX_VDMA_REG_DMACR, reg);
  574. if (config->park && (config->park_frm >= 0) &&
  575. (config->park_frm < chan->num_frms)) {
  576. if (chan->direction == DMA_MEM_TO_DEV)
  577. vdma_write(chan, XILINX_VDMA_REG_PARK_PTR,
  578. config->park_frm <<
  579. XILINX_VDMA_PARK_PTR_RD_REF_SHIFT);
  580. else
  581. vdma_write(chan, XILINX_VDMA_REG_PARK_PTR,
  582. config->park_frm <<
  583. XILINX_VDMA_PARK_PTR_WR_REF_SHIFT);
  584. }
  585. /* Start the hardware */
  586. xilinx_vdma_start(chan);
  587. if (chan->err)
  588. return;
  589. /* Start the transfer */
  590. if (chan->has_sg) {
  591. vdma_ctrl_write(chan, XILINX_VDMA_REG_TAILDESC,
  592. tail_segment->phys);
  593. } else {
  594. struct xilinx_vdma_tx_segment *segment, *last = NULL;
  595. int i = 0;
  596. list_for_each_entry(desc, &chan->pending_list, node) {
  597. segment = list_first_entry(&desc->segments,
  598. struct xilinx_vdma_tx_segment, node);
  599. vdma_desc_write(chan,
  600. XILINX_VDMA_REG_START_ADDRESS(i++),
  601. segment->hw.buf_addr);
  602. last = segment;
  603. }
  604. if (!last)
  605. return;
  606. /* HW expects these parameters to be same for one transaction */
  607. vdma_desc_write(chan, XILINX_VDMA_REG_HSIZE, last->hw.hsize);
  608. vdma_desc_write(chan, XILINX_VDMA_REG_FRMDLY_STRIDE,
  609. last->hw.stride);
  610. vdma_desc_write(chan, XILINX_VDMA_REG_VSIZE, last->hw.vsize);
  611. }
  612. list_splice_tail_init(&chan->pending_list, &chan->active_list);
  613. chan->desc_pendingcount = 0;
  614. }
  615. /**
  616. * xilinx_vdma_issue_pending - Issue pending transactions
  617. * @dchan: DMA channel
  618. */
  619. static void xilinx_vdma_issue_pending(struct dma_chan *dchan)
  620. {
  621. struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
  622. unsigned long flags;
  623. spin_lock_irqsave(&chan->lock, flags);
  624. xilinx_vdma_start_transfer(chan);
  625. spin_unlock_irqrestore(&chan->lock, flags);
  626. }
  627. /**
  628. * xilinx_vdma_complete_descriptor - Mark the active descriptor as complete
  629. * @chan : xilinx DMA channel
  630. *
  631. * CONTEXT: hardirq
  632. */
  633. static void xilinx_vdma_complete_descriptor(struct xilinx_vdma_chan *chan)
  634. {
  635. struct xilinx_vdma_tx_descriptor *desc, *next;
  636. /* This function was invoked with lock held */
  637. if (list_empty(&chan->active_list))
  638. return;
  639. list_for_each_entry_safe(desc, next, &chan->active_list, node) {
  640. list_del(&desc->node);
  641. dma_cookie_complete(&desc->async_tx);
  642. list_add_tail(&desc->node, &chan->done_list);
  643. }
  644. }
  645. /**
  646. * xilinx_vdma_reset - Reset VDMA channel
  647. * @chan: Driver specific VDMA channel
  648. *
  649. * Return: '0' on success and failure value on error
  650. */
  651. static int xilinx_vdma_reset(struct xilinx_vdma_chan *chan)
  652. {
  653. int err;
  654. u32 tmp;
  655. vdma_ctrl_set(chan, XILINX_VDMA_REG_DMACR, XILINX_VDMA_DMACR_RESET);
  656. /* Wait for the hardware to finish reset */
  657. err = xilinx_vdma_poll_timeout(chan, XILINX_VDMA_REG_DMACR, tmp,
  658. !(tmp & XILINX_VDMA_DMACR_RESET), 0,
  659. XILINX_VDMA_LOOP_COUNT);
  660. if (err) {
  661. dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
  662. vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR),
  663. vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR));
  664. return -ETIMEDOUT;
  665. }
  666. chan->err = false;
  667. return err;
  668. }
  669. /**
  670. * xilinx_vdma_chan_reset - Reset VDMA channel and enable interrupts
  671. * @chan: Driver specific VDMA channel
  672. *
  673. * Return: '0' on success and failure value on error
  674. */
  675. static int xilinx_vdma_chan_reset(struct xilinx_vdma_chan *chan)
  676. {
  677. int err;
  678. /* Reset VDMA */
  679. err = xilinx_vdma_reset(chan);
  680. if (err)
  681. return err;
  682. /* Enable interrupts */
  683. vdma_ctrl_set(chan, XILINX_VDMA_REG_DMACR,
  684. XILINX_VDMA_DMAXR_ALL_IRQ_MASK);
  685. return 0;
  686. }
  687. /**
  688. * xilinx_vdma_irq_handler - VDMA Interrupt handler
  689. * @irq: IRQ number
  690. * @data: Pointer to the Xilinx VDMA channel structure
  691. *
  692. * Return: IRQ_HANDLED/IRQ_NONE
  693. */
  694. static irqreturn_t xilinx_vdma_irq_handler(int irq, void *data)
  695. {
  696. struct xilinx_vdma_chan *chan = data;
  697. u32 status;
  698. /* Read the status and ack the interrupts. */
  699. status = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMASR);
  700. if (!(status & XILINX_VDMA_DMAXR_ALL_IRQ_MASK))
  701. return IRQ_NONE;
  702. vdma_ctrl_write(chan, XILINX_VDMA_REG_DMASR,
  703. status & XILINX_VDMA_DMAXR_ALL_IRQ_MASK);
  704. if (status & XILINX_VDMA_DMASR_ERR_IRQ) {
  705. /*
  706. * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
  707. * error is recoverable, ignore it. Otherwise flag the error.
  708. *
  709. * Only recoverable errors can be cleared in the DMASR register,
  710. * make sure not to write to other error bits to 1.
  711. */
  712. u32 errors = status & XILINX_VDMA_DMASR_ALL_ERR_MASK;
  713. vdma_ctrl_write(chan, XILINX_VDMA_REG_DMASR,
  714. errors & XILINX_VDMA_DMASR_ERR_RECOVER_MASK);
  715. if (!chan->flush_on_fsync ||
  716. (errors & ~XILINX_VDMA_DMASR_ERR_RECOVER_MASK)) {
  717. dev_err(chan->dev,
  718. "Channel %p has errors %x, cdr %x tdr %x\n",
  719. chan, errors,
  720. vdma_ctrl_read(chan, XILINX_VDMA_REG_CURDESC),
  721. vdma_ctrl_read(chan, XILINX_VDMA_REG_TAILDESC));
  722. chan->err = true;
  723. }
  724. }
  725. if (status & XILINX_VDMA_DMASR_DLY_CNT_IRQ) {
  726. /*
  727. * Device takes too long to do the transfer when user requires
  728. * responsiveness.
  729. */
  730. dev_dbg(chan->dev, "Inter-packet latency too long\n");
  731. }
  732. if (status & XILINX_VDMA_DMASR_FRM_CNT_IRQ) {
  733. spin_lock(&chan->lock);
  734. xilinx_vdma_complete_descriptor(chan);
  735. xilinx_vdma_start_transfer(chan);
  736. spin_unlock(&chan->lock);
  737. }
  738. tasklet_schedule(&chan->tasklet);
  739. return IRQ_HANDLED;
  740. }
  741. /**
  742. * append_desc_queue - Queuing descriptor
  743. * @chan: Driver specific dma channel
  744. * @desc: dma transaction descriptor
  745. */
  746. static void append_desc_queue(struct xilinx_vdma_chan *chan,
  747. struct xilinx_vdma_tx_descriptor *desc)
  748. {
  749. struct xilinx_vdma_tx_segment *tail_segment;
  750. struct xilinx_vdma_tx_descriptor *tail_desc;
  751. if (list_empty(&chan->pending_list))
  752. goto append;
  753. /*
  754. * Add the hardware descriptor to the chain of hardware descriptors
  755. * that already exists in memory.
  756. */
  757. tail_desc = list_last_entry(&chan->pending_list,
  758. struct xilinx_vdma_tx_descriptor, node);
  759. tail_segment = list_last_entry(&tail_desc->segments,
  760. struct xilinx_vdma_tx_segment, node);
  761. tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
  762. /*
  763. * Add the software descriptor and all children to the list
  764. * of pending transactions
  765. */
  766. append:
  767. list_add_tail(&desc->node, &chan->pending_list);
  768. chan->desc_pendingcount++;
  769. if (unlikely(chan->desc_pendingcount > chan->num_frms)) {
  770. dev_dbg(chan->dev, "desc pendingcount is too high\n");
  771. chan->desc_pendingcount = chan->num_frms;
  772. }
  773. }
  774. /**
  775. * xilinx_vdma_tx_submit - Submit DMA transaction
  776. * @tx: Async transaction descriptor
  777. *
  778. * Return: cookie value on success and failure value on error
  779. */
  780. static dma_cookie_t xilinx_vdma_tx_submit(struct dma_async_tx_descriptor *tx)
  781. {
  782. struct xilinx_vdma_tx_descriptor *desc = to_vdma_tx_descriptor(tx);
  783. struct xilinx_vdma_chan *chan = to_xilinx_chan(tx->chan);
  784. dma_cookie_t cookie;
  785. unsigned long flags;
  786. int err;
  787. if (chan->err) {
  788. /*
  789. * If reset fails, need to hard reset the system.
  790. * Channel is no longer functional
  791. */
  792. err = xilinx_vdma_chan_reset(chan);
  793. if (err < 0)
  794. return err;
  795. }
  796. spin_lock_irqsave(&chan->lock, flags);
  797. cookie = dma_cookie_assign(tx);
  798. /* Put this transaction onto the tail of the pending queue */
  799. append_desc_queue(chan, desc);
  800. spin_unlock_irqrestore(&chan->lock, flags);
  801. return cookie;
  802. }
  803. /**
  804. * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
  805. * DMA_SLAVE transaction
  806. * @dchan: DMA channel
  807. * @xt: Interleaved template pointer
  808. * @flags: transfer ack flags
  809. *
  810. * Return: Async transaction descriptor on success and NULL on failure
  811. */
  812. static struct dma_async_tx_descriptor *
  813. xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
  814. struct dma_interleaved_template *xt,
  815. unsigned long flags)
  816. {
  817. struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
  818. struct xilinx_vdma_tx_descriptor *desc;
  819. struct xilinx_vdma_tx_segment *segment, *prev = NULL;
  820. struct xilinx_vdma_desc_hw *hw;
  821. if (!is_slave_direction(xt->dir))
  822. return NULL;
  823. if (!xt->numf || !xt->sgl[0].size)
  824. return NULL;
  825. if (xt->frame_size != 1)
  826. return NULL;
  827. /* Allocate a transaction descriptor. */
  828. desc = xilinx_vdma_alloc_tx_descriptor(chan);
  829. if (!desc)
  830. return NULL;
  831. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  832. desc->async_tx.tx_submit = xilinx_vdma_tx_submit;
  833. async_tx_ack(&desc->async_tx);
  834. /* Allocate the link descriptor from DMA pool */
  835. segment = xilinx_vdma_alloc_tx_segment(chan);
  836. if (!segment)
  837. goto error;
  838. /* Fill in the hardware descriptor */
  839. hw = &segment->hw;
  840. hw->vsize = xt->numf;
  841. hw->hsize = xt->sgl[0].size;
  842. hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
  843. XILINX_VDMA_FRMDLY_STRIDE_STRIDE_SHIFT;
  844. hw->stride |= chan->config.frm_dly <<
  845. XILINX_VDMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
  846. if (xt->dir != DMA_MEM_TO_DEV)
  847. hw->buf_addr = xt->dst_start;
  848. else
  849. hw->buf_addr = xt->src_start;
  850. /* Insert the segment into the descriptor segments list. */
  851. list_add_tail(&segment->node, &desc->segments);
  852. prev = segment;
  853. /* Link the last hardware descriptor with the first. */
  854. segment = list_first_entry(&desc->segments,
  855. struct xilinx_vdma_tx_segment, node);
  856. desc->async_tx.phys = segment->phys;
  857. return &desc->async_tx;
  858. error:
  859. xilinx_vdma_free_tx_descriptor(chan, desc);
  860. return NULL;
  861. }
  862. /**
  863. * xilinx_vdma_terminate_all - Halt the channel and free descriptors
  864. * @chan: Driver specific VDMA Channel pointer
  865. */
  866. static int xilinx_vdma_terminate_all(struct dma_chan *dchan)
  867. {
  868. struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
  869. /* Halt the DMA engine */
  870. xilinx_vdma_halt(chan);
  871. /* Remove and free all of the descriptors in the lists */
  872. xilinx_vdma_free_descriptors(chan);
  873. return 0;
  874. }
  875. /**
  876. * xilinx_vdma_channel_set_config - Configure VDMA channel
  877. * Run-time configuration for Axi VDMA, supports:
  878. * . halt the channel
  879. * . configure interrupt coalescing and inter-packet delay threshold
  880. * . start/stop parking
  881. * . enable genlock
  882. *
  883. * @dchan: DMA channel
  884. * @cfg: VDMA device configuration pointer
  885. *
  886. * Return: '0' on success and failure value on error
  887. */
  888. int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
  889. struct xilinx_vdma_config *cfg)
  890. {
  891. struct xilinx_vdma_chan *chan = to_xilinx_chan(dchan);
  892. u32 dmacr;
  893. if (cfg->reset)
  894. return xilinx_vdma_chan_reset(chan);
  895. dmacr = vdma_ctrl_read(chan, XILINX_VDMA_REG_DMACR);
  896. chan->config.frm_dly = cfg->frm_dly;
  897. chan->config.park = cfg->park;
  898. /* genlock settings */
  899. chan->config.gen_lock = cfg->gen_lock;
  900. chan->config.master = cfg->master;
  901. if (cfg->gen_lock && chan->genlock) {
  902. dmacr |= XILINX_VDMA_DMACR_GENLOCK_EN;
  903. dmacr |= cfg->master << XILINX_VDMA_DMACR_MASTER_SHIFT;
  904. }
  905. chan->config.frm_cnt_en = cfg->frm_cnt_en;
  906. if (cfg->park)
  907. chan->config.park_frm = cfg->park_frm;
  908. else
  909. chan->config.park_frm = -1;
  910. chan->config.coalesc = cfg->coalesc;
  911. chan->config.delay = cfg->delay;
  912. if (cfg->coalesc <= XILINX_VDMA_DMACR_FRAME_COUNT_MAX) {
  913. dmacr |= cfg->coalesc << XILINX_VDMA_DMACR_FRAME_COUNT_SHIFT;
  914. chan->config.coalesc = cfg->coalesc;
  915. }
  916. if (cfg->delay <= XILINX_VDMA_DMACR_DELAY_MAX) {
  917. dmacr |= cfg->delay << XILINX_VDMA_DMACR_DELAY_SHIFT;
  918. chan->config.delay = cfg->delay;
  919. }
  920. /* FSync Source selection */
  921. dmacr &= ~XILINX_VDMA_DMACR_FSYNCSRC_MASK;
  922. dmacr |= cfg->ext_fsync << XILINX_VDMA_DMACR_FSYNCSRC_SHIFT;
  923. vdma_ctrl_write(chan, XILINX_VDMA_REG_DMACR, dmacr);
  924. return 0;
  925. }
  926. EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
  927. /* -----------------------------------------------------------------------------
  928. * Probe and remove
  929. */
  930. /**
  931. * xilinx_vdma_chan_remove - Per Channel remove function
  932. * @chan: Driver specific VDMA channel
  933. */
  934. static void xilinx_vdma_chan_remove(struct xilinx_vdma_chan *chan)
  935. {
  936. /* Disable all interrupts */
  937. vdma_ctrl_clr(chan, XILINX_VDMA_REG_DMACR,
  938. XILINX_VDMA_DMAXR_ALL_IRQ_MASK);
  939. if (chan->irq > 0)
  940. free_irq(chan->irq, chan);
  941. tasklet_kill(&chan->tasklet);
  942. list_del(&chan->common.device_node);
  943. }
  944. /**
  945. * xilinx_vdma_chan_probe - Per Channel Probing
  946. * It get channel features from the device tree entry and
  947. * initialize special channel handling routines
  948. *
  949. * @xdev: Driver specific device structure
  950. * @node: Device node
  951. *
  952. * Return: '0' on success and failure value on error
  953. */
  954. static int xilinx_vdma_chan_probe(struct xilinx_vdma_device *xdev,
  955. struct device_node *node)
  956. {
  957. struct xilinx_vdma_chan *chan;
  958. bool has_dre = false;
  959. u32 value, width;
  960. int err;
  961. /* Allocate and initialize the channel structure */
  962. chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
  963. if (!chan)
  964. return -ENOMEM;
  965. chan->dev = xdev->dev;
  966. chan->xdev = xdev;
  967. chan->has_sg = xdev->has_sg;
  968. chan->desc_pendingcount = 0x0;
  969. spin_lock_init(&chan->lock);
  970. INIT_LIST_HEAD(&chan->pending_list);
  971. INIT_LIST_HEAD(&chan->done_list);
  972. INIT_LIST_HEAD(&chan->active_list);
  973. /* Retrieve the channel properties from the device tree */
  974. has_dre = of_property_read_bool(node, "xlnx,include-dre");
  975. chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
  976. err = of_property_read_u32(node, "xlnx,datawidth", &value);
  977. if (err) {
  978. dev_err(xdev->dev, "missing xlnx,datawidth property\n");
  979. return err;
  980. }
  981. width = value >> 3; /* Convert bits to bytes */
  982. /* If data width is greater than 8 bytes, DRE is not in hw */
  983. if (width > 8)
  984. has_dre = false;
  985. if (!has_dre)
  986. xdev->common.copy_align = fls(width - 1);
  987. if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel")) {
  988. chan->direction = DMA_MEM_TO_DEV;
  989. chan->id = 0;
  990. chan->ctrl_offset = XILINX_VDMA_MM2S_CTRL_OFFSET;
  991. chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
  992. if (xdev->flush_on_fsync == XILINX_VDMA_FLUSH_BOTH ||
  993. xdev->flush_on_fsync == XILINX_VDMA_FLUSH_MM2S)
  994. chan->flush_on_fsync = true;
  995. } else if (of_device_is_compatible(node,
  996. "xlnx,axi-vdma-s2mm-channel")) {
  997. chan->direction = DMA_DEV_TO_MEM;
  998. chan->id = 1;
  999. chan->ctrl_offset = XILINX_VDMA_S2MM_CTRL_OFFSET;
  1000. chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
  1001. if (xdev->flush_on_fsync == XILINX_VDMA_FLUSH_BOTH ||
  1002. xdev->flush_on_fsync == XILINX_VDMA_FLUSH_S2MM)
  1003. chan->flush_on_fsync = true;
  1004. } else {
  1005. dev_err(xdev->dev, "Invalid channel compatible node\n");
  1006. return -EINVAL;
  1007. }
  1008. /* Request the interrupt */
  1009. chan->irq = irq_of_parse_and_map(node, 0);
  1010. err = request_irq(chan->irq, xilinx_vdma_irq_handler, IRQF_SHARED,
  1011. "xilinx-vdma-controller", chan);
  1012. if (err) {
  1013. dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
  1014. return err;
  1015. }
  1016. /* Initialize the tasklet */
  1017. tasklet_init(&chan->tasklet, xilinx_vdma_do_tasklet,
  1018. (unsigned long)chan);
  1019. /*
  1020. * Initialize the DMA channel and add it to the DMA engine channels
  1021. * list.
  1022. */
  1023. chan->common.device = &xdev->common;
  1024. list_add_tail(&chan->common.device_node, &xdev->common.channels);
  1025. xdev->chan[chan->id] = chan;
  1026. /* Reset the channel */
  1027. err = xilinx_vdma_chan_reset(chan);
  1028. if (err < 0) {
  1029. dev_err(xdev->dev, "Reset channel failed\n");
  1030. return err;
  1031. }
  1032. return 0;
  1033. }
  1034. /**
  1035. * of_dma_xilinx_xlate - Translation function
  1036. * @dma_spec: Pointer to DMA specifier as found in the device tree
  1037. * @ofdma: Pointer to DMA controller data
  1038. *
  1039. * Return: DMA channel pointer on success and NULL on error
  1040. */
  1041. static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
  1042. struct of_dma *ofdma)
  1043. {
  1044. struct xilinx_vdma_device *xdev = ofdma->of_dma_data;
  1045. int chan_id = dma_spec->args[0];
  1046. if (chan_id >= XILINX_VDMA_MAX_CHANS_PER_DEVICE)
  1047. return NULL;
  1048. return dma_get_slave_channel(&xdev->chan[chan_id]->common);
  1049. }
  1050. /**
  1051. * xilinx_vdma_probe - Driver probe function
  1052. * @pdev: Pointer to the platform_device structure
  1053. *
  1054. * Return: '0' on success and failure value on error
  1055. */
  1056. static int xilinx_vdma_probe(struct platform_device *pdev)
  1057. {
  1058. struct device_node *node = pdev->dev.of_node;
  1059. struct xilinx_vdma_device *xdev;
  1060. struct device_node *child;
  1061. struct resource *io;
  1062. u32 num_frames;
  1063. int i, err;
  1064. /* Allocate and initialize the DMA engine structure */
  1065. xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
  1066. if (!xdev)
  1067. return -ENOMEM;
  1068. xdev->dev = &pdev->dev;
  1069. /* Request and map I/O memory */
  1070. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1071. xdev->regs = devm_ioremap_resource(&pdev->dev, io);
  1072. if (IS_ERR(xdev->regs))
  1073. return PTR_ERR(xdev->regs);
  1074. /* Retrieve the DMA engine properties from the device tree */
  1075. xdev->has_sg = of_property_read_bool(node, "xlnx,include-sg");
  1076. err = of_property_read_u32(node, "xlnx,num-fstores", &num_frames);
  1077. if (err < 0) {
  1078. dev_err(xdev->dev, "missing xlnx,num-fstores property\n");
  1079. return err;
  1080. }
  1081. err = of_property_read_u32(node, "xlnx,flush-fsync",
  1082. &xdev->flush_on_fsync);
  1083. if (err < 0)
  1084. dev_warn(xdev->dev, "missing xlnx,flush-fsync property\n");
  1085. /* Initialize the DMA engine */
  1086. xdev->common.dev = &pdev->dev;
  1087. INIT_LIST_HEAD(&xdev->common.channels);
  1088. dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
  1089. dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
  1090. xdev->common.device_alloc_chan_resources =
  1091. xilinx_vdma_alloc_chan_resources;
  1092. xdev->common.device_free_chan_resources =
  1093. xilinx_vdma_free_chan_resources;
  1094. xdev->common.device_prep_interleaved_dma =
  1095. xilinx_vdma_dma_prep_interleaved;
  1096. xdev->common.device_terminate_all = xilinx_vdma_terminate_all;
  1097. xdev->common.device_tx_status = xilinx_vdma_tx_status;
  1098. xdev->common.device_issue_pending = xilinx_vdma_issue_pending;
  1099. platform_set_drvdata(pdev, xdev);
  1100. /* Initialize the channels */
  1101. for_each_child_of_node(node, child) {
  1102. err = xilinx_vdma_chan_probe(xdev, child);
  1103. if (err < 0)
  1104. goto error;
  1105. }
  1106. for (i = 0; i < XILINX_VDMA_MAX_CHANS_PER_DEVICE; i++)
  1107. if (xdev->chan[i])
  1108. xdev->chan[i]->num_frms = num_frames;
  1109. /* Register the DMA engine with the core */
  1110. dma_async_device_register(&xdev->common);
  1111. err = of_dma_controller_register(node, of_dma_xilinx_xlate,
  1112. xdev);
  1113. if (err < 0) {
  1114. dev_err(&pdev->dev, "Unable to register DMA to DT\n");
  1115. dma_async_device_unregister(&xdev->common);
  1116. goto error;
  1117. }
  1118. dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
  1119. return 0;
  1120. error:
  1121. for (i = 0; i < XILINX_VDMA_MAX_CHANS_PER_DEVICE; i++)
  1122. if (xdev->chan[i])
  1123. xilinx_vdma_chan_remove(xdev->chan[i]);
  1124. return err;
  1125. }
  1126. /**
  1127. * xilinx_vdma_remove - Driver remove function
  1128. * @pdev: Pointer to the platform_device structure
  1129. *
  1130. * Return: Always '0'
  1131. */
  1132. static int xilinx_vdma_remove(struct platform_device *pdev)
  1133. {
  1134. struct xilinx_vdma_device *xdev = platform_get_drvdata(pdev);
  1135. int i;
  1136. of_dma_controller_free(pdev->dev.of_node);
  1137. dma_async_device_unregister(&xdev->common);
  1138. for (i = 0; i < XILINX_VDMA_MAX_CHANS_PER_DEVICE; i++)
  1139. if (xdev->chan[i])
  1140. xilinx_vdma_chan_remove(xdev->chan[i]);
  1141. return 0;
  1142. }
  1143. static const struct of_device_id xilinx_vdma_of_ids[] = {
  1144. { .compatible = "xlnx,axi-vdma-1.00.a",},
  1145. {}
  1146. };
  1147. MODULE_DEVICE_TABLE(of, xilinx_vdma_of_ids);
  1148. static struct platform_driver xilinx_vdma_driver = {
  1149. .driver = {
  1150. .name = "xilinx-vdma",
  1151. .of_match_table = xilinx_vdma_of_ids,
  1152. },
  1153. .probe = xilinx_vdma_probe,
  1154. .remove = xilinx_vdma_remove,
  1155. };
  1156. module_platform_driver(xilinx_vdma_driver);
  1157. MODULE_AUTHOR("Xilinx, Inc.");
  1158. MODULE_DESCRIPTION("Xilinx VDMA driver");
  1159. MODULE_LICENSE("GPL v2");