xilinx_vdma.c 36 KB

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