xilinx_dpdma.c 49 KB

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