xilinx_dpdma.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx ZynqMP DPDMA Engine driver
  4. *
  5. * Copyright (C) 2015 - 2020 Xilinx, Inc.
  6. *
  7. * Author: Hyun Woo Kwon <[email protected]>
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bits.h>
  11. #include <linux/clk.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/delay.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dmapool.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/wait.h>
  25. #include <dt-bindings/dma/xlnx-zynqmp-dpdma.h>
  26. #include "../dmaengine.h"
  27. #include "../virt-dma.h"
  28. /* DPDMA registers */
  29. #define XILINX_DPDMA_ERR_CTRL 0x000
  30. #define XILINX_DPDMA_ISR 0x004
  31. #define XILINX_DPDMA_IMR 0x008
  32. #define XILINX_DPDMA_IEN 0x00c
  33. #define XILINX_DPDMA_IDS 0x010
  34. #define XILINX_DPDMA_INTR_DESC_DONE(n) BIT((n) + 0)
  35. #define XILINX_DPDMA_INTR_DESC_DONE_MASK GENMASK(5, 0)
  36. #define XILINX_DPDMA_INTR_NO_OSTAND(n) BIT((n) + 6)
  37. #define XILINX_DPDMA_INTR_NO_OSTAND_MASK GENMASK(11, 6)
  38. #define XILINX_DPDMA_INTR_AXI_ERR(n) BIT((n) + 12)
  39. #define XILINX_DPDMA_INTR_AXI_ERR_MASK GENMASK(17, 12)
  40. #define XILINX_DPDMA_INTR_DESC_ERR(n) BIT((n) + 16)
  41. #define XILINX_DPDMA_INTR_DESC_ERR_MASK GENMASK(23, 18)
  42. #define XILINX_DPDMA_INTR_WR_CMD_FIFO_FULL BIT(24)
  43. #define XILINX_DPDMA_INTR_WR_DATA_FIFO_FULL BIT(25)
  44. #define XILINX_DPDMA_INTR_AXI_4K_CROSS BIT(26)
  45. #define XILINX_DPDMA_INTR_VSYNC BIT(27)
  46. #define XILINX_DPDMA_INTR_CHAN_ERR_MASK 0x00041000
  47. #define XILINX_DPDMA_INTR_CHAN_ERR 0x00fff000
  48. #define XILINX_DPDMA_INTR_GLOBAL_ERR 0x07000000
  49. #define XILINX_DPDMA_INTR_ERR_ALL 0x07fff000
  50. #define XILINX_DPDMA_INTR_CHAN_MASK 0x00041041
  51. #define XILINX_DPDMA_INTR_GLOBAL_MASK 0x0f000000
  52. #define XILINX_DPDMA_INTR_ALL 0x0fffffff
  53. #define XILINX_DPDMA_EISR 0x014
  54. #define XILINX_DPDMA_EIMR 0x018
  55. #define XILINX_DPDMA_EIEN 0x01c
  56. #define XILINX_DPDMA_EIDS 0x020
  57. #define XILINX_DPDMA_EINTR_INV_APB BIT(0)
  58. #define XILINX_DPDMA_EINTR_RD_AXI_ERR(n) BIT((n) + 1)
  59. #define XILINX_DPDMA_EINTR_RD_AXI_ERR_MASK GENMASK(6, 1)
  60. #define XILINX_DPDMA_EINTR_PRE_ERR(n) BIT((n) + 7)
  61. #define XILINX_DPDMA_EINTR_PRE_ERR_MASK GENMASK(12, 7)
  62. #define XILINX_DPDMA_EINTR_CRC_ERR(n) BIT((n) + 13)
  63. #define XILINX_DPDMA_EINTR_CRC_ERR_MASK GENMASK(18, 13)
  64. #define XILINX_DPDMA_EINTR_WR_AXI_ERR(n) BIT((n) + 19)
  65. #define XILINX_DPDMA_EINTR_WR_AXI_ERR_MASK GENMASK(24, 19)
  66. #define XILINX_DPDMA_EINTR_DESC_DONE_ERR(n) BIT((n) + 25)
  67. #define XILINX_DPDMA_EINTR_DESC_DONE_ERR_MASK GENMASK(30, 25)
  68. #define XILINX_DPDMA_EINTR_RD_CMD_FIFO_FULL BIT(32)
  69. #define XILINX_DPDMA_EINTR_CHAN_ERR_MASK 0x02082082
  70. #define XILINX_DPDMA_EINTR_CHAN_ERR 0x7ffffffe
  71. #define XILINX_DPDMA_EINTR_GLOBAL_ERR 0x80000001
  72. #define XILINX_DPDMA_EINTR_ALL 0xffffffff
  73. #define XILINX_DPDMA_CNTL 0x100
  74. #define XILINX_DPDMA_GBL 0x104
  75. #define XILINX_DPDMA_GBL_TRIG_MASK(n) ((n) << 0)
  76. #define XILINX_DPDMA_GBL_RETRIG_MASK(n) ((n) << 6)
  77. #define XILINX_DPDMA_ALC0_CNTL 0x108
  78. #define XILINX_DPDMA_ALC0_STATUS 0x10c
  79. #define XILINX_DPDMA_ALC0_MAX 0x110
  80. #define XILINX_DPDMA_ALC0_MIN 0x114
  81. #define XILINX_DPDMA_ALC0_ACC 0x118
  82. #define XILINX_DPDMA_ALC0_ACC_TRAN 0x11c
  83. #define XILINX_DPDMA_ALC1_CNTL 0x120
  84. #define XILINX_DPDMA_ALC1_STATUS 0x124
  85. #define XILINX_DPDMA_ALC1_MAX 0x128
  86. #define XILINX_DPDMA_ALC1_MIN 0x12c
  87. #define XILINX_DPDMA_ALC1_ACC 0x130
  88. #define XILINX_DPDMA_ALC1_ACC_TRAN 0x134
  89. /* Channel register */
  90. #define XILINX_DPDMA_CH_BASE 0x200
  91. #define XILINX_DPDMA_CH_OFFSET 0x100
  92. #define XILINX_DPDMA_CH_DESC_START_ADDRE 0x000
  93. #define XILINX_DPDMA_CH_DESC_START_ADDRE_MASK GENMASK(15, 0)
  94. #define XILINX_DPDMA_CH_DESC_START_ADDR 0x004
  95. #define XILINX_DPDMA_CH_DESC_NEXT_ADDRE 0x008
  96. #define XILINX_DPDMA_CH_DESC_NEXT_ADDR 0x00c
  97. #define XILINX_DPDMA_CH_PYLD_CUR_ADDRE 0x010
  98. #define XILINX_DPDMA_CH_PYLD_CUR_ADDR 0x014
  99. #define XILINX_DPDMA_CH_CNTL 0x018
  100. #define XILINX_DPDMA_CH_CNTL_ENABLE BIT(0)
  101. #define XILINX_DPDMA_CH_CNTL_PAUSE BIT(1)
  102. #define XILINX_DPDMA_CH_CNTL_QOS_DSCR_WR_MASK GENMASK(5, 2)
  103. #define XILINX_DPDMA_CH_CNTL_QOS_DSCR_RD_MASK GENMASK(9, 6)
  104. #define XILINX_DPDMA_CH_CNTL_QOS_DATA_RD_MASK GENMASK(13, 10)
  105. #define XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS 11
  106. #define XILINX_DPDMA_CH_STATUS 0x01c
  107. #define XILINX_DPDMA_CH_STATUS_OTRAN_CNT_MASK GENMASK(24, 21)
  108. #define XILINX_DPDMA_CH_VDO 0x020
  109. #define XILINX_DPDMA_CH_PYLD_SZ 0x024
  110. #define XILINX_DPDMA_CH_DESC_ID 0x028
  111. /* DPDMA descriptor fields */
  112. #define XILINX_DPDMA_DESC_CONTROL_PREEMBLE 0xa5
  113. #define XILINX_DPDMA_DESC_CONTROL_COMPLETE_INTR BIT(8)
  114. #define XILINX_DPDMA_DESC_CONTROL_DESC_UPDATE BIT(9)
  115. #define XILINX_DPDMA_DESC_CONTROL_IGNORE_DONE BIT(10)
  116. #define XILINX_DPDMA_DESC_CONTROL_FRAG_MODE BIT(18)
  117. #define XILINX_DPDMA_DESC_CONTROL_LAST BIT(19)
  118. #define XILINX_DPDMA_DESC_CONTROL_ENABLE_CRC BIT(20)
  119. #define XILINX_DPDMA_DESC_CONTROL_LAST_OF_FRAME BIT(21)
  120. #define XILINX_DPDMA_DESC_ID_MASK GENMASK(15, 0)
  121. #define XILINX_DPDMA_DESC_HSIZE_STRIDE_HSIZE_MASK GENMASK(17, 0)
  122. #define XILINX_DPDMA_DESC_HSIZE_STRIDE_STRIDE_MASK GENMASK(31, 18)
  123. #define XILINX_DPDMA_DESC_ADDR_EXT_NEXT_ADDR_MASK GENMASK(15, 0)
  124. #define XILINX_DPDMA_DESC_ADDR_EXT_SRC_ADDR_MASK GENMASK(31, 16)
  125. #define XILINX_DPDMA_ALIGN_BYTES 256
  126. #define XILINX_DPDMA_LINESIZE_ALIGN_BITS 128
  127. #define XILINX_DPDMA_NUM_CHAN 6
  128. struct xilinx_dpdma_chan;
  129. /**
  130. * struct xilinx_dpdma_hw_desc - DPDMA hardware descriptor
  131. * @control: control configuration field
  132. * @desc_id: descriptor ID
  133. * @xfer_size: transfer size
  134. * @hsize_stride: horizontal size and stride
  135. * @timestamp_lsb: LSB of time stamp
  136. * @timestamp_msb: MSB of time stamp
  137. * @addr_ext: upper 16 bit of 48 bit address (next_desc and src_addr)
  138. * @next_desc: next descriptor 32 bit address
  139. * @src_addr: payload source address (1st page, 32 LSB)
  140. * @addr_ext_23: payload source address (3nd and 3rd pages, 16 LSBs)
  141. * @addr_ext_45: payload source address (4th and 5th pages, 16 LSBs)
  142. * @src_addr2: payload source address (2nd page, 32 LSB)
  143. * @src_addr3: payload source address (3rd page, 32 LSB)
  144. * @src_addr4: payload source address (4th page, 32 LSB)
  145. * @src_addr5: payload source address (5th page, 32 LSB)
  146. * @crc: descriptor CRC
  147. */
  148. struct xilinx_dpdma_hw_desc {
  149. u32 control;
  150. u32 desc_id;
  151. u32 xfer_size;
  152. u32 hsize_stride;
  153. u32 timestamp_lsb;
  154. u32 timestamp_msb;
  155. u32 addr_ext;
  156. u32 next_desc;
  157. u32 src_addr;
  158. u32 addr_ext_23;
  159. u32 addr_ext_45;
  160. u32 src_addr2;
  161. u32 src_addr3;
  162. u32 src_addr4;
  163. u32 src_addr5;
  164. u32 crc;
  165. } __aligned(XILINX_DPDMA_ALIGN_BYTES);
  166. /**
  167. * struct xilinx_dpdma_sw_desc - DPDMA software descriptor
  168. * @hw: DPDMA hardware descriptor
  169. * @node: list node for software descriptors
  170. * @dma_addr: DMA address of the software descriptor
  171. */
  172. struct xilinx_dpdma_sw_desc {
  173. struct xilinx_dpdma_hw_desc hw;
  174. struct list_head node;
  175. dma_addr_t dma_addr;
  176. };
  177. /**
  178. * struct xilinx_dpdma_tx_desc - DPDMA transaction descriptor
  179. * @vdesc: virtual DMA descriptor
  180. * @chan: DMA channel
  181. * @descriptors: list of software descriptors
  182. * @error: an error has been detected with this descriptor
  183. */
  184. struct xilinx_dpdma_tx_desc {
  185. struct virt_dma_desc vdesc;
  186. struct xilinx_dpdma_chan *chan;
  187. struct list_head descriptors;
  188. bool error;
  189. };
  190. #define to_dpdma_tx_desc(_desc) \
  191. container_of(_desc, struct xilinx_dpdma_tx_desc, vdesc)
  192. /**
  193. * struct xilinx_dpdma_chan - DPDMA channel
  194. * @vchan: virtual DMA channel
  195. * @reg: register base address
  196. * @id: channel ID
  197. * @wait_to_stop: queue to wait for outstanding transacitons before stopping
  198. * @running: true if the channel is running
  199. * @first_frame: flag for the first frame of stream
  200. * @video_group: flag if multi-channel operation is needed for video channels
  201. * @lock: lock to access struct xilinx_dpdma_chan
  202. * @desc_pool: descriptor allocation pool
  203. * @err_task: error IRQ bottom half handler
  204. * @desc: References to descriptors being processed
  205. * @desc.pending: Descriptor schedule to the hardware, pending execution
  206. * @desc.active: Descriptor being executed by the hardware
  207. * @xdev: DPDMA device
  208. */
  209. struct xilinx_dpdma_chan {
  210. struct virt_dma_chan vchan;
  211. void __iomem *reg;
  212. unsigned int id;
  213. wait_queue_head_t wait_to_stop;
  214. bool running;
  215. bool first_frame;
  216. bool video_group;
  217. spinlock_t lock; /* lock to access struct xilinx_dpdma_chan */
  218. struct dma_pool *desc_pool;
  219. struct tasklet_struct err_task;
  220. struct {
  221. struct xilinx_dpdma_tx_desc *pending;
  222. struct xilinx_dpdma_tx_desc *active;
  223. } desc;
  224. struct xilinx_dpdma_device *xdev;
  225. };
  226. #define to_xilinx_chan(_chan) \
  227. container_of(_chan, struct xilinx_dpdma_chan, vchan.chan)
  228. /**
  229. * struct xilinx_dpdma_device - DPDMA device
  230. * @common: generic dma device structure
  231. * @reg: register base address
  232. * @dev: generic device structure
  233. * @irq: the interrupt number
  234. * @axi_clk: axi clock
  235. * @chan: DPDMA channels
  236. * @ext_addr: flag for 64 bit system (48 bit addressing)
  237. */
  238. struct xilinx_dpdma_device {
  239. struct dma_device common;
  240. void __iomem *reg;
  241. struct device *dev;
  242. int irq;
  243. struct clk *axi_clk;
  244. struct xilinx_dpdma_chan *chan[XILINX_DPDMA_NUM_CHAN];
  245. bool ext_addr;
  246. };
  247. /* -----------------------------------------------------------------------------
  248. * DebugFS
  249. */
  250. #ifdef CONFIG_DEBUG_FS
  251. #define XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE 32
  252. #define XILINX_DPDMA_DEBUGFS_UINT16_MAX_STR "65535"
  253. /* Match xilinx_dpdma_testcases vs dpdma_debugfs_reqs[] entry */
  254. enum xilinx_dpdma_testcases {
  255. DPDMA_TC_INTR_DONE,
  256. DPDMA_TC_NONE
  257. };
  258. struct xilinx_dpdma_debugfs {
  259. enum xilinx_dpdma_testcases testcase;
  260. u16 xilinx_dpdma_irq_done_count;
  261. unsigned int chan_id;
  262. };
  263. static struct xilinx_dpdma_debugfs dpdma_debugfs;
  264. struct xilinx_dpdma_debugfs_request {
  265. const char *name;
  266. enum xilinx_dpdma_testcases tc;
  267. ssize_t (*read)(char *buf);
  268. int (*write)(char *args);
  269. };
  270. static void xilinx_dpdma_debugfs_desc_done_irq(struct xilinx_dpdma_chan *chan)
  271. {
  272. if (chan->id == dpdma_debugfs.chan_id)
  273. dpdma_debugfs.xilinx_dpdma_irq_done_count++;
  274. }
  275. static ssize_t xilinx_dpdma_debugfs_desc_done_irq_read(char *buf)
  276. {
  277. size_t out_str_len;
  278. dpdma_debugfs.testcase = DPDMA_TC_NONE;
  279. out_str_len = strlen(XILINX_DPDMA_DEBUGFS_UINT16_MAX_STR);
  280. out_str_len = min_t(size_t, XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE,
  281. out_str_len);
  282. snprintf(buf, out_str_len, "%d",
  283. dpdma_debugfs.xilinx_dpdma_irq_done_count);
  284. return 0;
  285. }
  286. static int xilinx_dpdma_debugfs_desc_done_irq_write(char *args)
  287. {
  288. char *arg;
  289. int ret;
  290. u32 id;
  291. arg = strsep(&args, " ");
  292. if (!arg || strncasecmp(arg, "start", 5))
  293. return -EINVAL;
  294. arg = strsep(&args, " ");
  295. if (!arg)
  296. return -EINVAL;
  297. ret = kstrtou32(arg, 0, &id);
  298. if (ret < 0)
  299. return ret;
  300. if (id < ZYNQMP_DPDMA_VIDEO0 || id > ZYNQMP_DPDMA_AUDIO1)
  301. return -EINVAL;
  302. dpdma_debugfs.testcase = DPDMA_TC_INTR_DONE;
  303. dpdma_debugfs.xilinx_dpdma_irq_done_count = 0;
  304. dpdma_debugfs.chan_id = id;
  305. return 0;
  306. }
  307. /* Match xilinx_dpdma_testcases vs dpdma_debugfs_reqs[] entry */
  308. static struct xilinx_dpdma_debugfs_request dpdma_debugfs_reqs[] = {
  309. {
  310. .name = "DESCRIPTOR_DONE_INTR",
  311. .tc = DPDMA_TC_INTR_DONE,
  312. .read = xilinx_dpdma_debugfs_desc_done_irq_read,
  313. .write = xilinx_dpdma_debugfs_desc_done_irq_write,
  314. },
  315. };
  316. static ssize_t xilinx_dpdma_debugfs_read(struct file *f, char __user *buf,
  317. size_t size, loff_t *pos)
  318. {
  319. enum xilinx_dpdma_testcases testcase;
  320. char *kern_buff;
  321. int ret = 0;
  322. if (*pos != 0 || size <= 0)
  323. return -EINVAL;
  324. kern_buff = kzalloc(XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE, GFP_KERNEL);
  325. if (!kern_buff) {
  326. dpdma_debugfs.testcase = DPDMA_TC_NONE;
  327. return -ENOMEM;
  328. }
  329. testcase = READ_ONCE(dpdma_debugfs.testcase);
  330. if (testcase != DPDMA_TC_NONE) {
  331. ret = dpdma_debugfs_reqs[testcase].read(kern_buff);
  332. if (ret < 0)
  333. goto done;
  334. } else {
  335. strlcpy(kern_buff, "No testcase executed",
  336. XILINX_DPDMA_DEBUGFS_READ_MAX_SIZE);
  337. }
  338. size = min(size, strlen(kern_buff));
  339. if (copy_to_user(buf, kern_buff, size))
  340. ret = -EFAULT;
  341. done:
  342. kfree(kern_buff);
  343. if (ret)
  344. return ret;
  345. *pos = size + 1;
  346. return size;
  347. }
  348. static ssize_t xilinx_dpdma_debugfs_write(struct file *f,
  349. const char __user *buf, size_t size,
  350. loff_t *pos)
  351. {
  352. char *kern_buff, *kern_buff_start;
  353. char *testcase;
  354. unsigned int i;
  355. int ret;
  356. if (*pos != 0 || size <= 0)
  357. return -EINVAL;
  358. /* Supporting single instance of test as of now. */
  359. if (dpdma_debugfs.testcase != DPDMA_TC_NONE)
  360. return -EBUSY;
  361. kern_buff = kzalloc(size, GFP_KERNEL);
  362. if (!kern_buff)
  363. return -ENOMEM;
  364. kern_buff_start = kern_buff;
  365. ret = strncpy_from_user(kern_buff, buf, size);
  366. if (ret < 0)
  367. goto done;
  368. /* Read the testcase name from a user request. */
  369. testcase = strsep(&kern_buff, " ");
  370. for (i = 0; i < ARRAY_SIZE(dpdma_debugfs_reqs); i++) {
  371. if (!strcasecmp(testcase, dpdma_debugfs_reqs[i].name))
  372. break;
  373. }
  374. if (i == ARRAY_SIZE(dpdma_debugfs_reqs)) {
  375. ret = -EINVAL;
  376. goto done;
  377. }
  378. ret = dpdma_debugfs_reqs[i].write(kern_buff);
  379. if (ret < 0)
  380. goto done;
  381. ret = size;
  382. done:
  383. kfree(kern_buff_start);
  384. return ret;
  385. }
  386. static const struct file_operations fops_xilinx_dpdma_dbgfs = {
  387. .owner = THIS_MODULE,
  388. .read = xilinx_dpdma_debugfs_read,
  389. .write = xilinx_dpdma_debugfs_write,
  390. };
  391. static void xilinx_dpdma_debugfs_init(struct xilinx_dpdma_device *xdev)
  392. {
  393. struct dentry *dent;
  394. dpdma_debugfs.testcase = DPDMA_TC_NONE;
  395. dent = debugfs_create_file("testcase", 0444, xdev->common.dbg_dev_root,
  396. NULL, &fops_xilinx_dpdma_dbgfs);
  397. if (IS_ERR(dent))
  398. dev_err(xdev->dev, "Failed to create debugfs testcase file\n");
  399. }
  400. #else
  401. static void xilinx_dpdma_debugfs_init(struct xilinx_dpdma_device *xdev)
  402. {
  403. }
  404. static void xilinx_dpdma_debugfs_desc_done_irq(struct xilinx_dpdma_chan *chan)
  405. {
  406. }
  407. #endif /* CONFIG_DEBUG_FS */
  408. /* -----------------------------------------------------------------------------
  409. * I/O Accessors
  410. */
  411. static inline u32 dpdma_read(void __iomem *base, u32 offset)
  412. {
  413. return ioread32(base + offset);
  414. }
  415. static inline void dpdma_write(void __iomem *base, u32 offset, u32 val)
  416. {
  417. iowrite32(val, base + offset);
  418. }
  419. static inline void dpdma_clr(void __iomem *base, u32 offset, u32 clr)
  420. {
  421. dpdma_write(base, offset, dpdma_read(base, offset) & ~clr);
  422. }
  423. static inline void dpdma_set(void __iomem *base, u32 offset, u32 set)
  424. {
  425. dpdma_write(base, offset, dpdma_read(base, offset) | set);
  426. }
  427. /* -----------------------------------------------------------------------------
  428. * Descriptor Operations
  429. */
  430. /**
  431. * xilinx_dpdma_sw_desc_set_dma_addrs - Set DMA addresses in the descriptor
  432. * @xdev: DPDMA device
  433. * @sw_desc: The software descriptor in which to set DMA addresses
  434. * @prev: The previous descriptor
  435. * @dma_addr: array of dma addresses
  436. * @num_src_addr: number of addresses in @dma_addr
  437. *
  438. * Set all the DMA addresses in the hardware descriptor corresponding to @dev
  439. * from @dma_addr. If a previous descriptor is specified in @prev, its next
  440. * descriptor DMA address is set to the DMA address of @sw_desc. @prev may be
  441. * identical to @sw_desc for cyclic transfers.
  442. */
  443. static void xilinx_dpdma_sw_desc_set_dma_addrs(struct xilinx_dpdma_device *xdev,
  444. struct xilinx_dpdma_sw_desc *sw_desc,
  445. struct xilinx_dpdma_sw_desc *prev,
  446. dma_addr_t dma_addr[],
  447. unsigned int num_src_addr)
  448. {
  449. struct xilinx_dpdma_hw_desc *hw_desc = &sw_desc->hw;
  450. unsigned int i;
  451. hw_desc->src_addr = lower_32_bits(dma_addr[0]);
  452. if (xdev->ext_addr)
  453. hw_desc->addr_ext |=
  454. FIELD_PREP(XILINX_DPDMA_DESC_ADDR_EXT_SRC_ADDR_MASK,
  455. upper_32_bits(dma_addr[0]));
  456. for (i = 1; i < num_src_addr; i++) {
  457. u32 *addr = &hw_desc->src_addr2;
  458. addr[i-1] = lower_32_bits(dma_addr[i]);
  459. if (xdev->ext_addr) {
  460. u32 *addr_ext = &hw_desc->addr_ext_23;
  461. u32 addr_msb;
  462. addr_msb = upper_32_bits(dma_addr[i]) & GENMASK(15, 0);
  463. addr_msb <<= 16 * ((i - 1) % 2);
  464. addr_ext[(i - 1) / 2] |= addr_msb;
  465. }
  466. }
  467. if (!prev)
  468. return;
  469. prev->hw.next_desc = lower_32_bits(sw_desc->dma_addr);
  470. if (xdev->ext_addr)
  471. prev->hw.addr_ext |=
  472. FIELD_PREP(XILINX_DPDMA_DESC_ADDR_EXT_NEXT_ADDR_MASK,
  473. upper_32_bits(sw_desc->dma_addr));
  474. }
  475. /**
  476. * xilinx_dpdma_chan_alloc_sw_desc - Allocate a software descriptor
  477. * @chan: DPDMA channel
  478. *
  479. * Allocate a software descriptor from the channel's descriptor pool.
  480. *
  481. * Return: a software descriptor or NULL.
  482. */
  483. static struct xilinx_dpdma_sw_desc *
  484. xilinx_dpdma_chan_alloc_sw_desc(struct xilinx_dpdma_chan *chan)
  485. {
  486. struct xilinx_dpdma_sw_desc *sw_desc;
  487. dma_addr_t dma_addr;
  488. sw_desc = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &dma_addr);
  489. if (!sw_desc)
  490. return NULL;
  491. sw_desc->dma_addr = dma_addr;
  492. return sw_desc;
  493. }
  494. /**
  495. * xilinx_dpdma_chan_free_sw_desc - Free a software descriptor
  496. * @chan: DPDMA channel
  497. * @sw_desc: software descriptor to free
  498. *
  499. * Free a software descriptor from the channel's descriptor pool.
  500. */
  501. static void
  502. xilinx_dpdma_chan_free_sw_desc(struct xilinx_dpdma_chan *chan,
  503. struct xilinx_dpdma_sw_desc *sw_desc)
  504. {
  505. dma_pool_free(chan->desc_pool, sw_desc, sw_desc->dma_addr);
  506. }
  507. /**
  508. * xilinx_dpdma_chan_dump_tx_desc - Dump a tx descriptor
  509. * @chan: DPDMA channel
  510. * @tx_desc: tx descriptor to dump
  511. *
  512. * Dump contents of a tx descriptor
  513. */
  514. static void xilinx_dpdma_chan_dump_tx_desc(struct xilinx_dpdma_chan *chan,
  515. struct xilinx_dpdma_tx_desc *tx_desc)
  516. {
  517. struct xilinx_dpdma_sw_desc *sw_desc;
  518. struct device *dev = chan->xdev->dev;
  519. unsigned int i = 0;
  520. dev_dbg(dev, "------- TX descriptor dump start -------\n");
  521. dev_dbg(dev, "------- channel ID = %d -------\n", chan->id);
  522. list_for_each_entry(sw_desc, &tx_desc->descriptors, node) {
  523. struct xilinx_dpdma_hw_desc *hw_desc = &sw_desc->hw;
  524. dev_dbg(dev, "------- HW descriptor %d -------\n", i++);
  525. dev_dbg(dev, "descriptor DMA addr: %pad\n", &sw_desc->dma_addr);
  526. dev_dbg(dev, "control: 0x%08x\n", hw_desc->control);
  527. dev_dbg(dev, "desc_id: 0x%08x\n", hw_desc->desc_id);
  528. dev_dbg(dev, "xfer_size: 0x%08x\n", hw_desc->xfer_size);
  529. dev_dbg(dev, "hsize_stride: 0x%08x\n", hw_desc->hsize_stride);
  530. dev_dbg(dev, "timestamp_lsb: 0x%08x\n", hw_desc->timestamp_lsb);
  531. dev_dbg(dev, "timestamp_msb: 0x%08x\n", hw_desc->timestamp_msb);
  532. dev_dbg(dev, "addr_ext: 0x%08x\n", hw_desc->addr_ext);
  533. dev_dbg(dev, "next_desc: 0x%08x\n", hw_desc->next_desc);
  534. dev_dbg(dev, "src_addr: 0x%08x\n", hw_desc->src_addr);
  535. dev_dbg(dev, "addr_ext_23: 0x%08x\n", hw_desc->addr_ext_23);
  536. dev_dbg(dev, "addr_ext_45: 0x%08x\n", hw_desc->addr_ext_45);
  537. dev_dbg(dev, "src_addr2: 0x%08x\n", hw_desc->src_addr2);
  538. dev_dbg(dev, "src_addr3: 0x%08x\n", hw_desc->src_addr3);
  539. dev_dbg(dev, "src_addr4: 0x%08x\n", hw_desc->src_addr4);
  540. dev_dbg(dev, "src_addr5: 0x%08x\n", hw_desc->src_addr5);
  541. dev_dbg(dev, "crc: 0x%08x\n", hw_desc->crc);
  542. }
  543. dev_dbg(dev, "------- TX descriptor dump end -------\n");
  544. }
  545. /**
  546. * xilinx_dpdma_chan_alloc_tx_desc - Allocate a transaction descriptor
  547. * @chan: DPDMA channel
  548. *
  549. * Allocate a tx descriptor.
  550. *
  551. * Return: a tx descriptor or NULL.
  552. */
  553. static struct xilinx_dpdma_tx_desc *
  554. xilinx_dpdma_chan_alloc_tx_desc(struct xilinx_dpdma_chan *chan)
  555. {
  556. struct xilinx_dpdma_tx_desc *tx_desc;
  557. tx_desc = kzalloc(sizeof(*tx_desc), GFP_NOWAIT);
  558. if (!tx_desc)
  559. return NULL;
  560. INIT_LIST_HEAD(&tx_desc->descriptors);
  561. tx_desc->chan = chan;
  562. tx_desc->error = false;
  563. return tx_desc;
  564. }
  565. /**
  566. * xilinx_dpdma_chan_free_tx_desc - Free a virtual DMA descriptor
  567. * @vdesc: virtual DMA descriptor
  568. *
  569. * Free the virtual DMA descriptor @vdesc including its software descriptors.
  570. */
  571. static void xilinx_dpdma_chan_free_tx_desc(struct virt_dma_desc *vdesc)
  572. {
  573. struct xilinx_dpdma_sw_desc *sw_desc, *next;
  574. struct xilinx_dpdma_tx_desc *desc;
  575. if (!vdesc)
  576. return;
  577. desc = to_dpdma_tx_desc(vdesc);
  578. list_for_each_entry_safe(sw_desc, next, &desc->descriptors, node) {
  579. list_del(&sw_desc->node);
  580. xilinx_dpdma_chan_free_sw_desc(desc->chan, sw_desc);
  581. }
  582. kfree(desc);
  583. }
  584. /**
  585. * xilinx_dpdma_chan_prep_interleaved_dma - Prepare an interleaved dma
  586. * descriptor
  587. * @chan: DPDMA channel
  588. * @xt: dma interleaved template
  589. *
  590. * Prepare a tx descriptor including internal software/hardware descriptors
  591. * based on @xt.
  592. *
  593. * Return: A DPDMA TX descriptor on success, or NULL.
  594. */
  595. static struct xilinx_dpdma_tx_desc *
  596. xilinx_dpdma_chan_prep_interleaved_dma(struct xilinx_dpdma_chan *chan,
  597. struct dma_interleaved_template *xt)
  598. {
  599. struct xilinx_dpdma_tx_desc *tx_desc;
  600. struct xilinx_dpdma_sw_desc *sw_desc;
  601. struct xilinx_dpdma_hw_desc *hw_desc;
  602. size_t hsize = xt->sgl[0].size;
  603. size_t stride = hsize + xt->sgl[0].icg;
  604. if (!IS_ALIGNED(xt->src_start, XILINX_DPDMA_ALIGN_BYTES)) {
  605. dev_err(chan->xdev->dev, "buffer should be aligned at %d B\n",
  606. XILINX_DPDMA_ALIGN_BYTES);
  607. return NULL;
  608. }
  609. tx_desc = xilinx_dpdma_chan_alloc_tx_desc(chan);
  610. if (!tx_desc)
  611. return NULL;
  612. sw_desc = xilinx_dpdma_chan_alloc_sw_desc(chan);
  613. if (!sw_desc) {
  614. xilinx_dpdma_chan_free_tx_desc(&tx_desc->vdesc);
  615. return NULL;
  616. }
  617. xilinx_dpdma_sw_desc_set_dma_addrs(chan->xdev, sw_desc, sw_desc,
  618. &xt->src_start, 1);
  619. hw_desc = &sw_desc->hw;
  620. hsize = ALIGN(hsize, XILINX_DPDMA_LINESIZE_ALIGN_BITS / 8);
  621. hw_desc->xfer_size = hsize * xt->numf;
  622. hw_desc->hsize_stride =
  623. FIELD_PREP(XILINX_DPDMA_DESC_HSIZE_STRIDE_HSIZE_MASK, hsize) |
  624. FIELD_PREP(XILINX_DPDMA_DESC_HSIZE_STRIDE_STRIDE_MASK,
  625. stride / 16);
  626. hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_PREEMBLE;
  627. hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_COMPLETE_INTR;
  628. hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_IGNORE_DONE;
  629. hw_desc->control |= XILINX_DPDMA_DESC_CONTROL_LAST_OF_FRAME;
  630. list_add_tail(&sw_desc->node, &tx_desc->descriptors);
  631. return tx_desc;
  632. }
  633. /* -----------------------------------------------------------------------------
  634. * DPDMA Channel Operations
  635. */
  636. /**
  637. * xilinx_dpdma_chan_enable - Enable the channel
  638. * @chan: DPDMA channel
  639. *
  640. * Enable the channel and its interrupts. Set the QoS values for video class.
  641. */
  642. static void xilinx_dpdma_chan_enable(struct xilinx_dpdma_chan *chan)
  643. {
  644. u32 reg;
  645. reg = (XILINX_DPDMA_INTR_CHAN_MASK << chan->id)
  646. | XILINX_DPDMA_INTR_GLOBAL_MASK;
  647. dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN, reg);
  648. reg = (XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id)
  649. | XILINX_DPDMA_INTR_GLOBAL_ERR;
  650. dpdma_write(chan->xdev->reg, XILINX_DPDMA_EIEN, reg);
  651. reg = XILINX_DPDMA_CH_CNTL_ENABLE
  652. | FIELD_PREP(XILINX_DPDMA_CH_CNTL_QOS_DSCR_WR_MASK,
  653. XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS)
  654. | FIELD_PREP(XILINX_DPDMA_CH_CNTL_QOS_DSCR_RD_MASK,
  655. XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS)
  656. | FIELD_PREP(XILINX_DPDMA_CH_CNTL_QOS_DATA_RD_MASK,
  657. XILINX_DPDMA_CH_CNTL_QOS_VID_CLASS);
  658. dpdma_set(chan->reg, XILINX_DPDMA_CH_CNTL, reg);
  659. }
  660. /**
  661. * xilinx_dpdma_chan_disable - Disable the channel
  662. * @chan: DPDMA channel
  663. *
  664. * Disable the channel and its interrupts.
  665. */
  666. static void xilinx_dpdma_chan_disable(struct xilinx_dpdma_chan *chan)
  667. {
  668. u32 reg;
  669. reg = XILINX_DPDMA_INTR_CHAN_MASK << chan->id;
  670. dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN, reg);
  671. reg = XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id;
  672. dpdma_write(chan->xdev->reg, XILINX_DPDMA_EIEN, reg);
  673. dpdma_clr(chan->reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_ENABLE);
  674. }
  675. /**
  676. * xilinx_dpdma_chan_pause - Pause the channel
  677. * @chan: DPDMA channel
  678. *
  679. * Pause the channel.
  680. */
  681. static void xilinx_dpdma_chan_pause(struct xilinx_dpdma_chan *chan)
  682. {
  683. dpdma_set(chan->reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_PAUSE);
  684. }
  685. /**
  686. * xilinx_dpdma_chan_unpause - Unpause the channel
  687. * @chan: DPDMA channel
  688. *
  689. * Unpause the channel.
  690. */
  691. static void xilinx_dpdma_chan_unpause(struct xilinx_dpdma_chan *chan)
  692. {
  693. dpdma_clr(chan->reg, XILINX_DPDMA_CH_CNTL, XILINX_DPDMA_CH_CNTL_PAUSE);
  694. }
  695. static u32 xilinx_dpdma_chan_video_group_ready(struct xilinx_dpdma_chan *chan)
  696. {
  697. struct xilinx_dpdma_device *xdev = chan->xdev;
  698. u32 channels = 0;
  699. unsigned int i;
  700. for (i = ZYNQMP_DPDMA_VIDEO0; i <= ZYNQMP_DPDMA_VIDEO2; i++) {
  701. if (xdev->chan[i]->video_group && !xdev->chan[i]->running)
  702. return 0;
  703. if (xdev->chan[i]->video_group)
  704. channels |= BIT(i);
  705. }
  706. return channels;
  707. }
  708. /**
  709. * xilinx_dpdma_chan_queue_transfer - Queue the next transfer
  710. * @chan: DPDMA channel
  711. *
  712. * Queue the next descriptor, if any, to the hardware. If the channel is
  713. * stopped, start it first. Otherwise retrigger it with the next descriptor.
  714. */
  715. static void xilinx_dpdma_chan_queue_transfer(struct xilinx_dpdma_chan *chan)
  716. {
  717. struct xilinx_dpdma_device *xdev = chan->xdev;
  718. struct xilinx_dpdma_sw_desc *sw_desc;
  719. struct xilinx_dpdma_tx_desc *desc;
  720. struct virt_dma_desc *vdesc;
  721. u32 reg, channels;
  722. bool first_frame;
  723. lockdep_assert_held(&chan->lock);
  724. if (chan->desc.pending)
  725. return;
  726. if (!chan->running) {
  727. xilinx_dpdma_chan_unpause(chan);
  728. xilinx_dpdma_chan_enable(chan);
  729. chan->first_frame = true;
  730. chan->running = true;
  731. }
  732. vdesc = vchan_next_desc(&chan->vchan);
  733. if (!vdesc)
  734. return;
  735. desc = to_dpdma_tx_desc(vdesc);
  736. chan->desc.pending = desc;
  737. list_del(&desc->vdesc.node);
  738. /*
  739. * Assign the cookie to descriptors in this transaction. Only 16 bit
  740. * will be used, but it should be enough.
  741. */
  742. list_for_each_entry(sw_desc, &desc->descriptors, node)
  743. sw_desc->hw.desc_id = desc->vdesc.tx.cookie;
  744. sw_desc = list_first_entry(&desc->descriptors,
  745. struct xilinx_dpdma_sw_desc, node);
  746. dpdma_write(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDR,
  747. lower_32_bits(sw_desc->dma_addr));
  748. if (xdev->ext_addr)
  749. dpdma_write(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDRE,
  750. FIELD_PREP(XILINX_DPDMA_CH_DESC_START_ADDRE_MASK,
  751. upper_32_bits(sw_desc->dma_addr)));
  752. first_frame = chan->first_frame;
  753. chan->first_frame = false;
  754. if (chan->video_group) {
  755. channels = xilinx_dpdma_chan_video_group_ready(chan);
  756. /*
  757. * Trigger the transfer only when all channels in the group are
  758. * ready.
  759. */
  760. if (!channels)
  761. return;
  762. } else {
  763. channels = BIT(chan->id);
  764. }
  765. if (first_frame)
  766. reg = XILINX_DPDMA_GBL_TRIG_MASK(channels);
  767. else
  768. reg = XILINX_DPDMA_GBL_RETRIG_MASK(channels);
  769. dpdma_write(xdev->reg, XILINX_DPDMA_GBL, reg);
  770. }
  771. /**
  772. * xilinx_dpdma_chan_ostand - Number of outstanding transactions
  773. * @chan: DPDMA channel
  774. *
  775. * Read and return the number of outstanding transactions from register.
  776. *
  777. * Return: Number of outstanding transactions from the status register.
  778. */
  779. static u32 xilinx_dpdma_chan_ostand(struct xilinx_dpdma_chan *chan)
  780. {
  781. return FIELD_GET(XILINX_DPDMA_CH_STATUS_OTRAN_CNT_MASK,
  782. dpdma_read(chan->reg, XILINX_DPDMA_CH_STATUS));
  783. }
  784. /**
  785. * xilinx_dpdma_chan_no_ostand - Notify no outstanding transaction event
  786. * @chan: DPDMA channel
  787. *
  788. * Notify waiters for no outstanding event, so waiters can stop the channel
  789. * safely. This function is supposed to be called when 'no outstanding'
  790. * interrupt is generated. The 'no outstanding' interrupt is disabled and
  791. * should be re-enabled when this event is handled. If the channel status
  792. * register still shows some number of outstanding transactions, the interrupt
  793. * remains enabled.
  794. *
  795. * Return: 0 on success. On failure, -EWOULDBLOCK if there's still outstanding
  796. * transaction(s).
  797. */
  798. static int xilinx_dpdma_chan_notify_no_ostand(struct xilinx_dpdma_chan *chan)
  799. {
  800. u32 cnt;
  801. cnt = xilinx_dpdma_chan_ostand(chan);
  802. if (cnt) {
  803. dev_dbg(chan->xdev->dev, "%d outstanding transactions\n", cnt);
  804. return -EWOULDBLOCK;
  805. }
  806. /* Disable 'no outstanding' interrupt */
  807. dpdma_write(chan->xdev->reg, XILINX_DPDMA_IDS,
  808. XILINX_DPDMA_INTR_NO_OSTAND(chan->id));
  809. wake_up(&chan->wait_to_stop);
  810. return 0;
  811. }
  812. /**
  813. * xilinx_dpdma_chan_wait_no_ostand - Wait for the no outstanding irq
  814. * @chan: DPDMA channel
  815. *
  816. * Wait for the no outstanding transaction interrupt. This functions can sleep
  817. * for 50ms.
  818. *
  819. * Return: 0 on success. On failure, -ETIMEOUT for time out, or the error code
  820. * from wait_event_interruptible_timeout().
  821. */
  822. static int xilinx_dpdma_chan_wait_no_ostand(struct xilinx_dpdma_chan *chan)
  823. {
  824. int ret;
  825. /* Wait for a no outstanding transaction interrupt upto 50msec */
  826. ret = wait_event_interruptible_timeout(chan->wait_to_stop,
  827. !xilinx_dpdma_chan_ostand(chan),
  828. msecs_to_jiffies(50));
  829. if (ret > 0) {
  830. dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN,
  831. XILINX_DPDMA_INTR_NO_OSTAND(chan->id));
  832. return 0;
  833. }
  834. dev_err(chan->xdev->dev, "not ready to stop: %d trans\n",
  835. xilinx_dpdma_chan_ostand(chan));
  836. if (ret == 0)
  837. return -ETIMEDOUT;
  838. return ret;
  839. }
  840. /**
  841. * xilinx_dpdma_chan_poll_no_ostand - Poll the outstanding transaction status
  842. * @chan: DPDMA channel
  843. *
  844. * Poll the outstanding transaction status, and return when there's no
  845. * outstanding transaction. This functions can be used in the interrupt context
  846. * or where the atomicity is required. Calling thread may wait more than 50ms.
  847. *
  848. * Return: 0 on success, or -ETIMEDOUT.
  849. */
  850. static int xilinx_dpdma_chan_poll_no_ostand(struct xilinx_dpdma_chan *chan)
  851. {
  852. u32 cnt, loop = 50000;
  853. /* Poll at least for 50ms (20 fps). */
  854. do {
  855. cnt = xilinx_dpdma_chan_ostand(chan);
  856. udelay(1);
  857. } while (loop-- > 0 && cnt);
  858. if (loop) {
  859. dpdma_write(chan->xdev->reg, XILINX_DPDMA_IEN,
  860. XILINX_DPDMA_INTR_NO_OSTAND(chan->id));
  861. return 0;
  862. }
  863. dev_err(chan->xdev->dev, "not ready to stop: %d trans\n",
  864. xilinx_dpdma_chan_ostand(chan));
  865. return -ETIMEDOUT;
  866. }
  867. /**
  868. * xilinx_dpdma_chan_stop - Stop the channel
  869. * @chan: DPDMA channel
  870. *
  871. * Stop a previously paused channel by first waiting for completion of all
  872. * outstanding transaction and then disabling the channel.
  873. *
  874. * Return: 0 on success, or -ETIMEDOUT if the channel failed to stop.
  875. */
  876. static int xilinx_dpdma_chan_stop(struct xilinx_dpdma_chan *chan)
  877. {
  878. unsigned long flags;
  879. int ret;
  880. ret = xilinx_dpdma_chan_wait_no_ostand(chan);
  881. if (ret)
  882. return ret;
  883. spin_lock_irqsave(&chan->lock, flags);
  884. xilinx_dpdma_chan_disable(chan);
  885. chan->running = false;
  886. spin_unlock_irqrestore(&chan->lock, flags);
  887. return 0;
  888. }
  889. /**
  890. * xilinx_dpdma_chan_done_irq - Handle hardware descriptor completion
  891. * @chan: DPDMA channel
  892. *
  893. * Handle completion of the currently active descriptor (@chan->desc.active). As
  894. * we currently support cyclic transfers only, this just invokes the cyclic
  895. * callback. The descriptor will be completed at the VSYNC interrupt when a new
  896. * descriptor replaces it.
  897. */
  898. static void xilinx_dpdma_chan_done_irq(struct xilinx_dpdma_chan *chan)
  899. {
  900. struct xilinx_dpdma_tx_desc *active;
  901. unsigned long flags;
  902. spin_lock_irqsave(&chan->lock, flags);
  903. xilinx_dpdma_debugfs_desc_done_irq(chan);
  904. active = chan->desc.active;
  905. if (active)
  906. vchan_cyclic_callback(&active->vdesc);
  907. else
  908. dev_warn(chan->xdev->dev,
  909. "DONE IRQ with no active descriptor!\n");
  910. spin_unlock_irqrestore(&chan->lock, flags);
  911. }
  912. /**
  913. * xilinx_dpdma_chan_vsync_irq - Handle hardware descriptor scheduling
  914. * @chan: DPDMA channel
  915. *
  916. * At VSYNC the active descriptor may have been replaced by the pending
  917. * descriptor. Detect this through the DESC_ID and perform appropriate
  918. * bookkeeping.
  919. */
  920. static void xilinx_dpdma_chan_vsync_irq(struct xilinx_dpdma_chan *chan)
  921. {
  922. struct xilinx_dpdma_tx_desc *pending;
  923. struct xilinx_dpdma_sw_desc *sw_desc;
  924. unsigned long flags;
  925. u32 desc_id;
  926. spin_lock_irqsave(&chan->lock, flags);
  927. pending = chan->desc.pending;
  928. if (!chan->running || !pending)
  929. goto out;
  930. desc_id = dpdma_read(chan->reg, XILINX_DPDMA_CH_DESC_ID);
  931. /* If the retrigger raced with vsync, retry at the next frame. */
  932. sw_desc = list_first_entry(&pending->descriptors,
  933. struct xilinx_dpdma_sw_desc, node);
  934. if (sw_desc->hw.desc_id != desc_id)
  935. goto out;
  936. /*
  937. * Complete the active descriptor, if any, promote the pending
  938. * descriptor to active, and queue the next transfer, if any.
  939. */
  940. if (chan->desc.active)
  941. vchan_cookie_complete(&chan->desc.active->vdesc);
  942. chan->desc.active = pending;
  943. chan->desc.pending = NULL;
  944. xilinx_dpdma_chan_queue_transfer(chan);
  945. out:
  946. spin_unlock_irqrestore(&chan->lock, flags);
  947. }
  948. /**
  949. * xilinx_dpdma_chan_err - Detect any channel error
  950. * @chan: DPDMA channel
  951. * @isr: masked Interrupt Status Register
  952. * @eisr: Error Interrupt Status Register
  953. *
  954. * Return: true if any channel error occurs, or false otherwise.
  955. */
  956. static bool
  957. xilinx_dpdma_chan_err(struct xilinx_dpdma_chan *chan, u32 isr, u32 eisr)
  958. {
  959. if (!chan)
  960. return false;
  961. if (chan->running &&
  962. ((isr & (XILINX_DPDMA_INTR_CHAN_ERR_MASK << chan->id)) ||
  963. (eisr & (XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id))))
  964. return true;
  965. return false;
  966. }
  967. /**
  968. * xilinx_dpdma_chan_handle_err - DPDMA channel error handling
  969. * @chan: DPDMA channel
  970. *
  971. * This function is called when any channel error or any global error occurs.
  972. * The function disables the paused channel by errors and determines
  973. * if the current active descriptor can be rescheduled depending on
  974. * the descriptor status.
  975. */
  976. static void xilinx_dpdma_chan_handle_err(struct xilinx_dpdma_chan *chan)
  977. {
  978. struct xilinx_dpdma_device *xdev = chan->xdev;
  979. struct xilinx_dpdma_tx_desc *active;
  980. unsigned long flags;
  981. spin_lock_irqsave(&chan->lock, flags);
  982. dev_dbg(xdev->dev, "cur desc addr = 0x%04x%08x\n",
  983. dpdma_read(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDRE),
  984. dpdma_read(chan->reg, XILINX_DPDMA_CH_DESC_START_ADDR));
  985. dev_dbg(xdev->dev, "cur payload addr = 0x%04x%08x\n",
  986. dpdma_read(chan->reg, XILINX_DPDMA_CH_PYLD_CUR_ADDRE),
  987. dpdma_read(chan->reg, XILINX_DPDMA_CH_PYLD_CUR_ADDR));
  988. xilinx_dpdma_chan_disable(chan);
  989. chan->running = false;
  990. if (!chan->desc.active)
  991. goto out_unlock;
  992. active = chan->desc.active;
  993. chan->desc.active = NULL;
  994. xilinx_dpdma_chan_dump_tx_desc(chan, active);
  995. if (active->error)
  996. dev_dbg(xdev->dev, "repeated error on desc\n");
  997. /* Reschedule if there's no new descriptor */
  998. if (!chan->desc.pending &&
  999. list_empty(&chan->vchan.desc_issued)) {
  1000. active->error = true;
  1001. list_add_tail(&active->vdesc.node,
  1002. &chan->vchan.desc_issued);
  1003. } else {
  1004. xilinx_dpdma_chan_free_tx_desc(&active->vdesc);
  1005. }
  1006. out_unlock:
  1007. spin_unlock_irqrestore(&chan->lock, flags);
  1008. }
  1009. /* -----------------------------------------------------------------------------
  1010. * DMA Engine Operations
  1011. */
  1012. static struct dma_async_tx_descriptor *
  1013. xilinx_dpdma_prep_interleaved_dma(struct dma_chan *dchan,
  1014. struct dma_interleaved_template *xt,
  1015. unsigned long flags)
  1016. {
  1017. struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
  1018. struct xilinx_dpdma_tx_desc *desc;
  1019. if (xt->dir != DMA_MEM_TO_DEV)
  1020. return NULL;
  1021. if (!xt->numf || !xt->sgl[0].size)
  1022. return NULL;
  1023. if (!(flags & DMA_PREP_REPEAT) || !(flags & DMA_PREP_LOAD_EOT))
  1024. return NULL;
  1025. desc = xilinx_dpdma_chan_prep_interleaved_dma(chan, xt);
  1026. if (!desc)
  1027. return NULL;
  1028. vchan_tx_prep(&chan->vchan, &desc->vdesc, flags | DMA_CTRL_ACK);
  1029. return &desc->vdesc.tx;
  1030. }
  1031. /**
  1032. * xilinx_dpdma_alloc_chan_resources - Allocate resources for the channel
  1033. * @dchan: DMA channel
  1034. *
  1035. * Allocate a descriptor pool for the channel.
  1036. *
  1037. * Return: 0 on success, or -ENOMEM if failed to allocate a pool.
  1038. */
  1039. static int xilinx_dpdma_alloc_chan_resources(struct dma_chan *dchan)
  1040. {
  1041. struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
  1042. size_t align = __alignof__(struct xilinx_dpdma_sw_desc);
  1043. chan->desc_pool = dma_pool_create(dev_name(chan->xdev->dev),
  1044. chan->xdev->dev,
  1045. sizeof(struct xilinx_dpdma_sw_desc),
  1046. align, 0);
  1047. if (!chan->desc_pool) {
  1048. dev_err(chan->xdev->dev,
  1049. "failed to allocate a descriptor pool\n");
  1050. return -ENOMEM;
  1051. }
  1052. return 0;
  1053. }
  1054. /**
  1055. * xilinx_dpdma_free_chan_resources - Free all resources for the channel
  1056. * @dchan: DMA channel
  1057. *
  1058. * Free resources associated with the virtual DMA channel, and destroy the
  1059. * descriptor pool.
  1060. */
  1061. static void xilinx_dpdma_free_chan_resources(struct dma_chan *dchan)
  1062. {
  1063. struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
  1064. vchan_free_chan_resources(&chan->vchan);
  1065. dma_pool_destroy(chan->desc_pool);
  1066. chan->desc_pool = NULL;
  1067. }
  1068. static void xilinx_dpdma_issue_pending(struct dma_chan *dchan)
  1069. {
  1070. struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
  1071. unsigned long flags;
  1072. spin_lock_irqsave(&chan->vchan.lock, flags);
  1073. if (vchan_issue_pending(&chan->vchan))
  1074. xilinx_dpdma_chan_queue_transfer(chan);
  1075. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  1076. }
  1077. static int xilinx_dpdma_config(struct dma_chan *dchan,
  1078. struct dma_slave_config *config)
  1079. {
  1080. struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
  1081. unsigned long flags;
  1082. /*
  1083. * The destination address doesn't need to be specified as the DPDMA is
  1084. * hardwired to the destination (the DP controller). The transfer
  1085. * width, burst size and port window size are thus meaningless, they're
  1086. * fixed both on the DPDMA side and on the DP controller side.
  1087. */
  1088. spin_lock_irqsave(&chan->lock, flags);
  1089. /*
  1090. * Abuse the slave_id to indicate that the channel is part of a video
  1091. * group.
  1092. */
  1093. if (chan->id <= ZYNQMP_DPDMA_VIDEO2)
  1094. chan->video_group = config->slave_id != 0;
  1095. spin_unlock_irqrestore(&chan->lock, flags);
  1096. return 0;
  1097. }
  1098. static int xilinx_dpdma_pause(struct dma_chan *dchan)
  1099. {
  1100. xilinx_dpdma_chan_pause(to_xilinx_chan(dchan));
  1101. return 0;
  1102. }
  1103. static int xilinx_dpdma_resume(struct dma_chan *dchan)
  1104. {
  1105. xilinx_dpdma_chan_unpause(to_xilinx_chan(dchan));
  1106. return 0;
  1107. }
  1108. /**
  1109. * xilinx_dpdma_terminate_all - Terminate the channel and descriptors
  1110. * @dchan: DMA channel
  1111. *
  1112. * Pause the channel without waiting for ongoing transfers to complete. Waiting
  1113. * for completion is performed by xilinx_dpdma_synchronize() that will disable
  1114. * the channel to complete the stop.
  1115. *
  1116. * All the descriptors associated with the channel that are guaranteed not to
  1117. * be touched by the hardware. The pending and active descriptor are not
  1118. * touched, and will be freed either upon completion, or by
  1119. * xilinx_dpdma_synchronize().
  1120. *
  1121. * Return: 0 on success, or -ETIMEDOUT if the channel failed to stop.
  1122. */
  1123. static int xilinx_dpdma_terminate_all(struct dma_chan *dchan)
  1124. {
  1125. struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
  1126. struct xilinx_dpdma_device *xdev = chan->xdev;
  1127. LIST_HEAD(descriptors);
  1128. unsigned long flags;
  1129. unsigned int i;
  1130. /* Pause the channel (including the whole video group if applicable). */
  1131. if (chan->video_group) {
  1132. for (i = ZYNQMP_DPDMA_VIDEO0; i <= ZYNQMP_DPDMA_VIDEO2; i++) {
  1133. if (xdev->chan[i]->video_group &&
  1134. xdev->chan[i]->running) {
  1135. xilinx_dpdma_chan_pause(xdev->chan[i]);
  1136. xdev->chan[i]->video_group = false;
  1137. }
  1138. }
  1139. } else {
  1140. xilinx_dpdma_chan_pause(chan);
  1141. }
  1142. /* Gather all the descriptors we can free and free them. */
  1143. spin_lock_irqsave(&chan->vchan.lock, flags);
  1144. vchan_get_all_descriptors(&chan->vchan, &descriptors);
  1145. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  1146. vchan_dma_desc_free_list(&chan->vchan, &descriptors);
  1147. return 0;
  1148. }
  1149. /**
  1150. * xilinx_dpdma_synchronize - Synchronize callback execution
  1151. * @dchan: DMA channel
  1152. *
  1153. * Synchronizing callback execution ensures that all previously issued
  1154. * transfers have completed and all associated callbacks have been called and
  1155. * have returned.
  1156. *
  1157. * This function waits for the DMA channel to stop. It assumes it has been
  1158. * paused by a previous call to dmaengine_terminate_async(), and that no new
  1159. * pending descriptors have been issued with dma_async_issue_pending(). The
  1160. * behaviour is undefined otherwise.
  1161. */
  1162. static void xilinx_dpdma_synchronize(struct dma_chan *dchan)
  1163. {
  1164. struct xilinx_dpdma_chan *chan = to_xilinx_chan(dchan);
  1165. unsigned long flags;
  1166. xilinx_dpdma_chan_stop(chan);
  1167. spin_lock_irqsave(&chan->vchan.lock, flags);
  1168. if (chan->desc.pending) {
  1169. vchan_terminate_vdesc(&chan->desc.pending->vdesc);
  1170. chan->desc.pending = NULL;
  1171. }
  1172. if (chan->desc.active) {
  1173. vchan_terminate_vdesc(&chan->desc.active->vdesc);
  1174. chan->desc.active = NULL;
  1175. }
  1176. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  1177. vchan_synchronize(&chan->vchan);
  1178. }
  1179. /* -----------------------------------------------------------------------------
  1180. * Interrupt and Tasklet Handling
  1181. */
  1182. /**
  1183. * xilinx_dpdma_err - Detect any global error
  1184. * @isr: Interrupt Status Register
  1185. * @eisr: Error Interrupt Status Register
  1186. *
  1187. * Return: True if any global error occurs, or false otherwise.
  1188. */
  1189. static bool xilinx_dpdma_err(u32 isr, u32 eisr)
  1190. {
  1191. if (isr & XILINX_DPDMA_INTR_GLOBAL_ERR ||
  1192. eisr & XILINX_DPDMA_EINTR_GLOBAL_ERR)
  1193. return true;
  1194. return false;
  1195. }
  1196. /**
  1197. * xilinx_dpdma_handle_err_irq - Handle DPDMA error interrupt
  1198. * @xdev: DPDMA device
  1199. * @isr: masked Interrupt Status Register
  1200. * @eisr: Error Interrupt Status Register
  1201. *
  1202. * Handle if any error occurs based on @isr and @eisr. This function disables
  1203. * corresponding error interrupts, and those should be re-enabled once handling
  1204. * is done.
  1205. */
  1206. static void xilinx_dpdma_handle_err_irq(struct xilinx_dpdma_device *xdev,
  1207. u32 isr, u32 eisr)
  1208. {
  1209. bool err = xilinx_dpdma_err(isr, eisr);
  1210. unsigned int i;
  1211. dev_dbg_ratelimited(xdev->dev,
  1212. "error irq: isr = 0x%08x, eisr = 0x%08x\n",
  1213. isr, eisr);
  1214. /* Disable channel error interrupts until errors are handled. */
  1215. dpdma_write(xdev->reg, XILINX_DPDMA_IDS,
  1216. isr & ~XILINX_DPDMA_INTR_GLOBAL_ERR);
  1217. dpdma_write(xdev->reg, XILINX_DPDMA_EIDS,
  1218. eisr & ~XILINX_DPDMA_EINTR_GLOBAL_ERR);
  1219. for (i = 0; i < ARRAY_SIZE(xdev->chan); i++)
  1220. if (err || xilinx_dpdma_chan_err(xdev->chan[i], isr, eisr))
  1221. tasklet_schedule(&xdev->chan[i]->err_task);
  1222. }
  1223. /**
  1224. * xilinx_dpdma_enable_irq - Enable interrupts
  1225. * @xdev: DPDMA device
  1226. *
  1227. * Enable interrupts.
  1228. */
  1229. static void xilinx_dpdma_enable_irq(struct xilinx_dpdma_device *xdev)
  1230. {
  1231. dpdma_write(xdev->reg, XILINX_DPDMA_IEN, XILINX_DPDMA_INTR_ALL);
  1232. dpdma_write(xdev->reg, XILINX_DPDMA_EIEN, XILINX_DPDMA_EINTR_ALL);
  1233. }
  1234. /**
  1235. * xilinx_dpdma_disable_irq - Disable interrupts
  1236. * @xdev: DPDMA device
  1237. *
  1238. * Disable interrupts.
  1239. */
  1240. static void xilinx_dpdma_disable_irq(struct xilinx_dpdma_device *xdev)
  1241. {
  1242. dpdma_write(xdev->reg, XILINX_DPDMA_IDS, XILINX_DPDMA_INTR_ERR_ALL);
  1243. dpdma_write(xdev->reg, XILINX_DPDMA_EIDS, XILINX_DPDMA_EINTR_ALL);
  1244. }
  1245. /**
  1246. * xilinx_dpdma_chan_err_task - Per channel tasklet for error handling
  1247. * @t: pointer to the tasklet associated with this handler
  1248. *
  1249. * Per channel error handling tasklet. This function waits for the outstanding
  1250. * transaction to complete and triggers error handling. After error handling,
  1251. * re-enable channel error interrupts, and restart the channel if needed.
  1252. */
  1253. static void xilinx_dpdma_chan_err_task(struct tasklet_struct *t)
  1254. {
  1255. struct xilinx_dpdma_chan *chan = from_tasklet(chan, t, err_task);
  1256. struct xilinx_dpdma_device *xdev = chan->xdev;
  1257. unsigned long flags;
  1258. /* Proceed error handling even when polling fails. */
  1259. xilinx_dpdma_chan_poll_no_ostand(chan);
  1260. xilinx_dpdma_chan_handle_err(chan);
  1261. dpdma_write(xdev->reg, XILINX_DPDMA_IEN,
  1262. XILINX_DPDMA_INTR_CHAN_ERR_MASK << chan->id);
  1263. dpdma_write(xdev->reg, XILINX_DPDMA_EIEN,
  1264. XILINX_DPDMA_EINTR_CHAN_ERR_MASK << chan->id);
  1265. spin_lock_irqsave(&chan->lock, flags);
  1266. xilinx_dpdma_chan_queue_transfer(chan);
  1267. spin_unlock_irqrestore(&chan->lock, flags);
  1268. }
  1269. static irqreturn_t xilinx_dpdma_irq_handler(int irq, void *data)
  1270. {
  1271. struct xilinx_dpdma_device *xdev = data;
  1272. unsigned long mask;
  1273. unsigned int i;
  1274. u32 status;
  1275. u32 error;
  1276. status = dpdma_read(xdev->reg, XILINX_DPDMA_ISR);
  1277. error = dpdma_read(xdev->reg, XILINX_DPDMA_EISR);
  1278. if (!status && !error)
  1279. return IRQ_NONE;
  1280. dpdma_write(xdev->reg, XILINX_DPDMA_ISR, status);
  1281. dpdma_write(xdev->reg, XILINX_DPDMA_EISR, error);
  1282. if (status & XILINX_DPDMA_INTR_VSYNC) {
  1283. /*
  1284. * There's a single VSYNC interrupt that needs to be processed
  1285. * by each running channel to update the active descriptor.
  1286. */
  1287. for (i = 0; i < ARRAY_SIZE(xdev->chan); i++) {
  1288. struct xilinx_dpdma_chan *chan = xdev->chan[i];
  1289. if (chan)
  1290. xilinx_dpdma_chan_vsync_irq(chan);
  1291. }
  1292. }
  1293. mask = FIELD_GET(XILINX_DPDMA_INTR_DESC_DONE_MASK, status);
  1294. if (mask) {
  1295. for_each_set_bit(i, &mask, ARRAY_SIZE(xdev->chan))
  1296. xilinx_dpdma_chan_done_irq(xdev->chan[i]);
  1297. }
  1298. mask = FIELD_GET(XILINX_DPDMA_INTR_NO_OSTAND_MASK, status);
  1299. if (mask) {
  1300. for_each_set_bit(i, &mask, ARRAY_SIZE(xdev->chan))
  1301. xilinx_dpdma_chan_notify_no_ostand(xdev->chan[i]);
  1302. }
  1303. mask = status & XILINX_DPDMA_INTR_ERR_ALL;
  1304. if (mask || error)
  1305. xilinx_dpdma_handle_err_irq(xdev, mask, error);
  1306. return IRQ_HANDLED;
  1307. }
  1308. /* -----------------------------------------------------------------------------
  1309. * Initialization & Cleanup
  1310. */
  1311. static int xilinx_dpdma_chan_init(struct xilinx_dpdma_device *xdev,
  1312. unsigned int chan_id)
  1313. {
  1314. struct xilinx_dpdma_chan *chan;
  1315. chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
  1316. if (!chan)
  1317. return -ENOMEM;
  1318. chan->id = chan_id;
  1319. chan->reg = xdev->reg + XILINX_DPDMA_CH_BASE
  1320. + XILINX_DPDMA_CH_OFFSET * chan->id;
  1321. chan->running = false;
  1322. chan->xdev = xdev;
  1323. spin_lock_init(&chan->lock);
  1324. init_waitqueue_head(&chan->wait_to_stop);
  1325. tasklet_setup(&chan->err_task, xilinx_dpdma_chan_err_task);
  1326. chan->vchan.desc_free = xilinx_dpdma_chan_free_tx_desc;
  1327. vchan_init(&chan->vchan, &xdev->common);
  1328. xdev->chan[chan->id] = chan;
  1329. return 0;
  1330. }
  1331. static void xilinx_dpdma_chan_remove(struct xilinx_dpdma_chan *chan)
  1332. {
  1333. if (!chan)
  1334. return;
  1335. tasklet_kill(&chan->err_task);
  1336. list_del(&chan->vchan.chan.device_node);
  1337. }
  1338. static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
  1339. struct of_dma *ofdma)
  1340. {
  1341. struct xilinx_dpdma_device *xdev = ofdma->of_dma_data;
  1342. uint32_t chan_id = dma_spec->args[0];
  1343. if (chan_id >= ARRAY_SIZE(xdev->chan))
  1344. return NULL;
  1345. if (!xdev->chan[chan_id])
  1346. return NULL;
  1347. return dma_get_slave_channel(&xdev->chan[chan_id]->vchan.chan);
  1348. }
  1349. static int xilinx_dpdma_probe(struct platform_device *pdev)
  1350. {
  1351. struct xilinx_dpdma_device *xdev;
  1352. struct dma_device *ddev;
  1353. unsigned int i;
  1354. int ret;
  1355. xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
  1356. if (!xdev)
  1357. return -ENOMEM;
  1358. xdev->dev = &pdev->dev;
  1359. xdev->ext_addr = sizeof(dma_addr_t) > 4;
  1360. INIT_LIST_HEAD(&xdev->common.channels);
  1361. platform_set_drvdata(pdev, xdev);
  1362. xdev->axi_clk = devm_clk_get(xdev->dev, "axi_clk");
  1363. if (IS_ERR(xdev->axi_clk))
  1364. return PTR_ERR(xdev->axi_clk);
  1365. xdev->reg = devm_platform_ioremap_resource(pdev, 0);
  1366. if (IS_ERR(xdev->reg))
  1367. return PTR_ERR(xdev->reg);
  1368. xdev->irq = platform_get_irq(pdev, 0);
  1369. if (xdev->irq < 0) {
  1370. dev_err(xdev->dev, "failed to get platform irq\n");
  1371. return xdev->irq;
  1372. }
  1373. ret = request_irq(xdev->irq, xilinx_dpdma_irq_handler, IRQF_SHARED,
  1374. dev_name(xdev->dev), xdev);
  1375. if (ret) {
  1376. dev_err(xdev->dev, "failed to request IRQ\n");
  1377. return ret;
  1378. }
  1379. ddev = &xdev->common;
  1380. ddev->dev = &pdev->dev;
  1381. dma_cap_set(DMA_SLAVE, ddev->cap_mask);
  1382. dma_cap_set(DMA_PRIVATE, ddev->cap_mask);
  1383. dma_cap_set(DMA_INTERLEAVE, ddev->cap_mask);
  1384. dma_cap_set(DMA_REPEAT, ddev->cap_mask);
  1385. dma_cap_set(DMA_LOAD_EOT, ddev->cap_mask);
  1386. ddev->copy_align = fls(XILINX_DPDMA_ALIGN_BYTES - 1);
  1387. ddev->device_alloc_chan_resources = xilinx_dpdma_alloc_chan_resources;
  1388. ddev->device_free_chan_resources = xilinx_dpdma_free_chan_resources;
  1389. ddev->device_prep_interleaved_dma = xilinx_dpdma_prep_interleaved_dma;
  1390. /* TODO: Can we achieve better granularity ? */
  1391. ddev->device_tx_status = dma_cookie_status;
  1392. ddev->device_issue_pending = xilinx_dpdma_issue_pending;
  1393. ddev->device_config = xilinx_dpdma_config;
  1394. ddev->device_pause = xilinx_dpdma_pause;
  1395. ddev->device_resume = xilinx_dpdma_resume;
  1396. ddev->device_terminate_all = xilinx_dpdma_terminate_all;
  1397. ddev->device_synchronize = xilinx_dpdma_synchronize;
  1398. ddev->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED);
  1399. ddev->directions = BIT(DMA_MEM_TO_DEV);
  1400. ddev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  1401. for (i = 0; i < ARRAY_SIZE(xdev->chan); ++i) {
  1402. ret = xilinx_dpdma_chan_init(xdev, i);
  1403. if (ret < 0) {
  1404. dev_err(xdev->dev, "failed to initialize channel %u\n",
  1405. i);
  1406. goto error;
  1407. }
  1408. }
  1409. ret = clk_prepare_enable(xdev->axi_clk);
  1410. if (ret) {
  1411. dev_err(xdev->dev, "failed to enable the axi clock\n");
  1412. goto error;
  1413. }
  1414. ret = dma_async_device_register(ddev);
  1415. if (ret) {
  1416. dev_err(xdev->dev, "failed to register the dma device\n");
  1417. goto error_dma_async;
  1418. }
  1419. ret = of_dma_controller_register(xdev->dev->of_node,
  1420. of_dma_xilinx_xlate, ddev);
  1421. if (ret) {
  1422. dev_err(xdev->dev, "failed to register DMA to DT DMA helper\n");
  1423. goto error_of_dma;
  1424. }
  1425. xilinx_dpdma_enable_irq(xdev);
  1426. xilinx_dpdma_debugfs_init(xdev);
  1427. dev_info(&pdev->dev, "Xilinx DPDMA engine is probed\n");
  1428. return 0;
  1429. error_of_dma:
  1430. dma_async_device_unregister(ddev);
  1431. error_dma_async:
  1432. clk_disable_unprepare(xdev->axi_clk);
  1433. error:
  1434. for (i = 0; i < ARRAY_SIZE(xdev->chan); i++)
  1435. xilinx_dpdma_chan_remove(xdev->chan[i]);
  1436. free_irq(xdev->irq, xdev);
  1437. return ret;
  1438. }
  1439. static int xilinx_dpdma_remove(struct platform_device *pdev)
  1440. {
  1441. struct xilinx_dpdma_device *xdev = platform_get_drvdata(pdev);
  1442. unsigned int i;
  1443. /* Start by disabling the IRQ to avoid races during cleanup. */
  1444. free_irq(xdev->irq, xdev);
  1445. xilinx_dpdma_disable_irq(xdev);
  1446. of_dma_controller_free(pdev->dev.of_node);
  1447. dma_async_device_unregister(&xdev->common);
  1448. clk_disable_unprepare(xdev->axi_clk);
  1449. for (i = 0; i < ARRAY_SIZE(xdev->chan); i++)
  1450. xilinx_dpdma_chan_remove(xdev->chan[i]);
  1451. return 0;
  1452. }
  1453. static const struct of_device_id xilinx_dpdma_of_match[] = {
  1454. { .compatible = "xlnx,zynqmp-dpdma",},
  1455. { /* end of table */ },
  1456. };
  1457. MODULE_DEVICE_TABLE(of, xilinx_dpdma_of_match);
  1458. static struct platform_driver xilinx_dpdma_driver = {
  1459. .probe = xilinx_dpdma_probe,
  1460. .remove = xilinx_dpdma_remove,
  1461. .driver = {
  1462. .name = "xilinx-zynqmp-dpdma",
  1463. .of_match_table = xilinx_dpdma_of_match,
  1464. },
  1465. };
  1466. module_platform_driver(xilinx_dpdma_driver);
  1467. MODULE_AUTHOR("Xilinx, Inc.");
  1468. MODULE_DESCRIPTION("Xilinx ZynqMP DPDMA driver");
  1469. MODULE_LICENSE("GPL v2");