dma-axi-dmac.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the Analog Devices AXI-DMAC core
  4. *
  5. * Copyright 2013-2019 Analog Devices Inc.
  6. * Author: Lars-Peter Clausen <lars@metafoo.de>
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/device.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/of_address.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. #include <linux/fpga/adi-axi-common.h>
  25. #include <dt-bindings/dma/axi-dmac.h>
  26. #include "dmaengine.h"
  27. #include "virt-dma.h"
  28. /*
  29. * The AXI-DMAC is a soft IP core that is used in FPGA designs. The core has
  30. * various instantiation parameters which decided the exact feature set support
  31. * by the core.
  32. *
  33. * Each channel of the core has a source interface and a destination interface.
  34. * The number of channels and the type of the channel interfaces is selected at
  35. * configuration time. A interface can either be a connected to a central memory
  36. * interconnect, which allows access to system memory, or it can be connected to
  37. * a dedicated bus which is directly connected to a data port on a peripheral.
  38. * Given that those are configuration options of the core that are selected when
  39. * it is instantiated this means that they can not be changed by software at
  40. * runtime. By extension this means that each channel is uni-directional. It can
  41. * either be device to memory or memory to device, but not both. Also since the
  42. * device side is a dedicated data bus only connected to a single peripheral
  43. * there is no address than can or needs to be configured for the device side.
  44. */
  45. #define AXI_DMAC_REG_INTERFACE_DESC 0x10
  46. #define AXI_DMAC_DMA_SRC_TYPE_MSK GENMASK(13, 12)
  47. #define AXI_DMAC_DMA_SRC_TYPE_GET(x) FIELD_GET(AXI_DMAC_DMA_SRC_TYPE_MSK, x)
  48. #define AXI_DMAC_DMA_SRC_WIDTH_MSK GENMASK(11, 8)
  49. #define AXI_DMAC_DMA_SRC_WIDTH_GET(x) FIELD_GET(AXI_DMAC_DMA_SRC_WIDTH_MSK, x)
  50. #define AXI_DMAC_DMA_DST_TYPE_MSK GENMASK(5, 4)
  51. #define AXI_DMAC_DMA_DST_TYPE_GET(x) FIELD_GET(AXI_DMAC_DMA_DST_TYPE_MSK, x)
  52. #define AXI_DMAC_DMA_DST_WIDTH_MSK GENMASK(3, 0)
  53. #define AXI_DMAC_DMA_DST_WIDTH_GET(x) FIELD_GET(AXI_DMAC_DMA_DST_WIDTH_MSK, x)
  54. #define AXI_DMAC_REG_COHERENCY_DESC 0x14
  55. #define AXI_DMAC_DST_COHERENT_MSK BIT(0)
  56. #define AXI_DMAC_DST_COHERENT_GET(x) FIELD_GET(AXI_DMAC_DST_COHERENT_MSK, x)
  57. #define AXI_DMAC_REG_IRQ_MASK 0x80
  58. #define AXI_DMAC_REG_IRQ_PENDING 0x84
  59. #define AXI_DMAC_REG_IRQ_SOURCE 0x88
  60. #define AXI_DMAC_REG_CTRL 0x400
  61. #define AXI_DMAC_REG_TRANSFER_ID 0x404
  62. #define AXI_DMAC_REG_START_TRANSFER 0x408
  63. #define AXI_DMAC_REG_FLAGS 0x40c
  64. #define AXI_DMAC_REG_DEST_ADDRESS 0x410
  65. #define AXI_DMAC_REG_SRC_ADDRESS 0x414
  66. #define AXI_DMAC_REG_X_LENGTH 0x418
  67. #define AXI_DMAC_REG_Y_LENGTH 0x41c
  68. #define AXI_DMAC_REG_DEST_STRIDE 0x420
  69. #define AXI_DMAC_REG_SRC_STRIDE 0x424
  70. #define AXI_DMAC_REG_TRANSFER_DONE 0x428
  71. #define AXI_DMAC_REG_ACTIVE_TRANSFER_ID 0x42c
  72. #define AXI_DMAC_REG_STATUS 0x430
  73. #define AXI_DMAC_REG_CURRENT_SRC_ADDR 0x434
  74. #define AXI_DMAC_REG_CURRENT_DEST_ADDR 0x438
  75. #define AXI_DMAC_REG_PARTIAL_XFER_LEN 0x44c
  76. #define AXI_DMAC_REG_PARTIAL_XFER_ID 0x450
  77. #define AXI_DMAC_REG_CURRENT_SG_ID 0x454
  78. #define AXI_DMAC_REG_SG_ADDRESS 0x47c
  79. #define AXI_DMAC_REG_SG_ADDRESS_HIGH 0x4bc
  80. #define AXI_DMAC_CTRL_ENABLE BIT(0)
  81. #define AXI_DMAC_CTRL_PAUSE BIT(1)
  82. #define AXI_DMAC_CTRL_ENABLE_SG BIT(2)
  83. #define AXI_DMAC_IRQ_SOT BIT(0)
  84. #define AXI_DMAC_IRQ_EOT BIT(1)
  85. #define AXI_DMAC_FLAG_CYCLIC BIT(0)
  86. #define AXI_DMAC_FLAG_LAST BIT(1)
  87. #define AXI_DMAC_FLAG_PARTIAL_REPORT BIT(2)
  88. #define AXI_DMAC_FLAG_PARTIAL_XFER_DONE BIT(31)
  89. /* The maximum ID allocated by the hardware is 31 */
  90. #define AXI_DMAC_SG_UNUSED 32U
  91. /* Flags for axi_dmac_hw_desc.flags */
  92. #define AXI_DMAC_HW_FLAG_LAST BIT(0)
  93. #define AXI_DMAC_HW_FLAG_IRQ BIT(1)
  94. struct axi_dmac_hw_desc {
  95. u32 flags;
  96. u32 id;
  97. u64 dest_addr;
  98. u64 src_addr;
  99. u64 next_sg_addr;
  100. u32 y_len;
  101. u32 x_len;
  102. u32 src_stride;
  103. u32 dst_stride;
  104. u64 __pad[2];
  105. };
  106. struct axi_dmac_sg {
  107. unsigned int partial_len;
  108. bool schedule_when_free;
  109. struct axi_dmac_hw_desc *hw;
  110. dma_addr_t hw_phys;
  111. };
  112. struct axi_dmac_desc {
  113. struct virt_dma_desc vdesc;
  114. struct axi_dmac_chan *chan;
  115. bool cyclic;
  116. bool have_partial_xfer;
  117. unsigned int num_submitted;
  118. unsigned int num_completed;
  119. unsigned int num_sgs;
  120. struct axi_dmac_sg sg[] __counted_by(num_sgs);
  121. };
  122. struct axi_dmac_chan {
  123. struct virt_dma_chan vchan;
  124. struct axi_dmac_desc *next_desc;
  125. struct list_head active_descs;
  126. enum dma_transfer_direction direction;
  127. unsigned int src_width;
  128. unsigned int dest_width;
  129. unsigned int src_type;
  130. unsigned int dest_type;
  131. unsigned int max_length;
  132. unsigned int address_align_mask;
  133. unsigned int length_align_mask;
  134. bool hw_partial_xfer;
  135. bool hw_cyclic;
  136. bool hw_2d;
  137. bool hw_sg;
  138. };
  139. struct axi_dmac {
  140. void __iomem *base;
  141. int irq;
  142. struct clk *clk;
  143. struct dma_device dma_dev;
  144. struct axi_dmac_chan chan;
  145. };
  146. static struct axi_dmac *chan_to_axi_dmac(struct axi_dmac_chan *chan)
  147. {
  148. return container_of(chan->vchan.chan.device, struct axi_dmac,
  149. dma_dev);
  150. }
  151. static struct axi_dmac_chan *to_axi_dmac_chan(struct dma_chan *c)
  152. {
  153. return container_of(c, struct axi_dmac_chan, vchan.chan);
  154. }
  155. static struct axi_dmac_desc *to_axi_dmac_desc(struct virt_dma_desc *vdesc)
  156. {
  157. return container_of(vdesc, struct axi_dmac_desc, vdesc);
  158. }
  159. static void axi_dmac_write(struct axi_dmac *axi_dmac, unsigned int reg,
  160. unsigned int val)
  161. {
  162. writel(val, axi_dmac->base + reg);
  163. }
  164. static int axi_dmac_read(struct axi_dmac *axi_dmac, unsigned int reg)
  165. {
  166. return readl(axi_dmac->base + reg);
  167. }
  168. static int axi_dmac_src_is_mem(struct axi_dmac_chan *chan)
  169. {
  170. return chan->src_type == AXI_DMAC_BUS_TYPE_AXI_MM;
  171. }
  172. static int axi_dmac_dest_is_mem(struct axi_dmac_chan *chan)
  173. {
  174. return chan->dest_type == AXI_DMAC_BUS_TYPE_AXI_MM;
  175. }
  176. static bool axi_dmac_check_len(struct axi_dmac_chan *chan, unsigned int len)
  177. {
  178. if (len == 0)
  179. return false;
  180. if ((len & chan->length_align_mask) != 0) /* Not aligned */
  181. return false;
  182. return true;
  183. }
  184. static bool axi_dmac_check_addr(struct axi_dmac_chan *chan, dma_addr_t addr)
  185. {
  186. if ((addr & chan->address_align_mask) != 0) /* Not aligned */
  187. return false;
  188. return true;
  189. }
  190. static void axi_dmac_start_transfer(struct axi_dmac_chan *chan)
  191. {
  192. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  193. struct virt_dma_desc *vdesc;
  194. struct axi_dmac_desc *desc;
  195. struct axi_dmac_sg *sg;
  196. unsigned int flags = 0;
  197. unsigned int val;
  198. if (!chan->hw_sg) {
  199. val = axi_dmac_read(dmac, AXI_DMAC_REG_START_TRANSFER);
  200. if (val) /* Queue is full, wait for the next SOT IRQ */
  201. return;
  202. }
  203. desc = chan->next_desc;
  204. if (!desc) {
  205. vdesc = vchan_next_desc(&chan->vchan);
  206. if (!vdesc)
  207. return;
  208. list_move_tail(&vdesc->node, &chan->active_descs);
  209. desc = to_axi_dmac_desc(vdesc);
  210. }
  211. sg = &desc->sg[desc->num_submitted];
  212. /* Already queued in cyclic mode. Wait for it to finish */
  213. if (sg->hw->id != AXI_DMAC_SG_UNUSED) {
  214. sg->schedule_when_free = true;
  215. return;
  216. }
  217. if (chan->hw_sg) {
  218. chan->next_desc = NULL;
  219. } else if (++desc->num_submitted == desc->num_sgs ||
  220. desc->have_partial_xfer) {
  221. if (desc->cyclic)
  222. desc->num_submitted = 0; /* Start again */
  223. else
  224. chan->next_desc = NULL;
  225. flags |= AXI_DMAC_FLAG_LAST;
  226. } else {
  227. chan->next_desc = desc;
  228. }
  229. sg->hw->id = axi_dmac_read(dmac, AXI_DMAC_REG_TRANSFER_ID);
  230. if (!chan->hw_sg) {
  231. if (axi_dmac_dest_is_mem(chan)) {
  232. axi_dmac_write(dmac, AXI_DMAC_REG_DEST_ADDRESS, sg->hw->dest_addr);
  233. axi_dmac_write(dmac, AXI_DMAC_REG_DEST_STRIDE, sg->hw->dst_stride);
  234. }
  235. if (axi_dmac_src_is_mem(chan)) {
  236. axi_dmac_write(dmac, AXI_DMAC_REG_SRC_ADDRESS, sg->hw->src_addr);
  237. axi_dmac_write(dmac, AXI_DMAC_REG_SRC_STRIDE, sg->hw->src_stride);
  238. }
  239. }
  240. /*
  241. * If the hardware supports cyclic transfers and there is no callback to
  242. * call, enable hw cyclic mode to avoid unnecessary interrupts.
  243. */
  244. if (chan->hw_cyclic && desc->cyclic && !desc->vdesc.tx.callback) {
  245. if (chan->hw_sg)
  246. desc->sg[desc->num_sgs - 1].hw->flags &= ~AXI_DMAC_HW_FLAG_IRQ;
  247. else if (desc->num_sgs == 1)
  248. flags |= AXI_DMAC_FLAG_CYCLIC;
  249. }
  250. if (chan->hw_partial_xfer)
  251. flags |= AXI_DMAC_FLAG_PARTIAL_REPORT;
  252. if (chan->hw_sg) {
  253. axi_dmac_write(dmac, AXI_DMAC_REG_SG_ADDRESS, (u32)sg->hw_phys);
  254. axi_dmac_write(dmac, AXI_DMAC_REG_SG_ADDRESS_HIGH,
  255. (u64)sg->hw_phys >> 32);
  256. } else {
  257. axi_dmac_write(dmac, AXI_DMAC_REG_X_LENGTH, sg->hw->x_len);
  258. axi_dmac_write(dmac, AXI_DMAC_REG_Y_LENGTH, sg->hw->y_len);
  259. }
  260. axi_dmac_write(dmac, AXI_DMAC_REG_FLAGS, flags);
  261. axi_dmac_write(dmac, AXI_DMAC_REG_START_TRANSFER, 1);
  262. }
  263. static struct axi_dmac_desc *axi_dmac_active_desc(struct axi_dmac_chan *chan)
  264. {
  265. return list_first_entry_or_null(&chan->active_descs,
  266. struct axi_dmac_desc, vdesc.node);
  267. }
  268. static inline unsigned int axi_dmac_total_sg_bytes(struct axi_dmac_chan *chan,
  269. struct axi_dmac_sg *sg)
  270. {
  271. if (chan->hw_2d)
  272. return (sg->hw->x_len + 1) * (sg->hw->y_len + 1);
  273. else
  274. return (sg->hw->x_len + 1);
  275. }
  276. static void axi_dmac_dequeue_partial_xfers(struct axi_dmac_chan *chan)
  277. {
  278. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  279. struct axi_dmac_desc *desc;
  280. struct axi_dmac_sg *sg;
  281. u32 xfer_done, len, id, i;
  282. bool found_sg;
  283. do {
  284. len = axi_dmac_read(dmac, AXI_DMAC_REG_PARTIAL_XFER_LEN);
  285. id = axi_dmac_read(dmac, AXI_DMAC_REG_PARTIAL_XFER_ID);
  286. found_sg = false;
  287. list_for_each_entry(desc, &chan->active_descs, vdesc.node) {
  288. for (i = 0; i < desc->num_sgs; i++) {
  289. sg = &desc->sg[i];
  290. if (sg->hw->id == AXI_DMAC_SG_UNUSED)
  291. continue;
  292. if (sg->hw->id == id) {
  293. desc->have_partial_xfer = true;
  294. sg->partial_len = len;
  295. found_sg = true;
  296. break;
  297. }
  298. }
  299. if (found_sg)
  300. break;
  301. }
  302. if (found_sg) {
  303. dev_dbg(dmac->dma_dev.dev,
  304. "Found partial segment id=%u, len=%u\n",
  305. id, len);
  306. } else {
  307. dev_warn(dmac->dma_dev.dev,
  308. "Not found partial segment id=%u, len=%u\n",
  309. id, len);
  310. }
  311. /* Check if we have any more partial transfers */
  312. xfer_done = axi_dmac_read(dmac, AXI_DMAC_REG_TRANSFER_DONE);
  313. xfer_done = !(xfer_done & AXI_DMAC_FLAG_PARTIAL_XFER_DONE);
  314. } while (!xfer_done);
  315. }
  316. static void axi_dmac_compute_residue(struct axi_dmac_chan *chan,
  317. struct axi_dmac_desc *active)
  318. {
  319. struct dmaengine_result *rslt = &active->vdesc.tx_result;
  320. unsigned int start = active->num_completed - 1;
  321. struct axi_dmac_sg *sg;
  322. unsigned int i, total;
  323. rslt->result = DMA_TRANS_NOERROR;
  324. rslt->residue = 0;
  325. if (chan->hw_sg)
  326. return;
  327. /*
  328. * We get here if the last completed segment is partial, which
  329. * means we can compute the residue from that segment onwards
  330. */
  331. for (i = start; i < active->num_sgs; i++) {
  332. sg = &active->sg[i];
  333. total = axi_dmac_total_sg_bytes(chan, sg);
  334. rslt->residue += (total - sg->partial_len);
  335. }
  336. }
  337. static bool axi_dmac_transfer_done(struct axi_dmac_chan *chan,
  338. unsigned int completed_transfers)
  339. {
  340. struct axi_dmac_desc *active;
  341. struct axi_dmac_sg *sg;
  342. bool start_next = false;
  343. active = axi_dmac_active_desc(chan);
  344. if (!active)
  345. return false;
  346. if (chan->hw_partial_xfer &&
  347. (completed_transfers & AXI_DMAC_FLAG_PARTIAL_XFER_DONE))
  348. axi_dmac_dequeue_partial_xfers(chan);
  349. if (chan->hw_sg) {
  350. if (active->cyclic) {
  351. vchan_cyclic_callback(&active->vdesc);
  352. } else {
  353. list_del(&active->vdesc.node);
  354. vchan_cookie_complete(&active->vdesc);
  355. active = axi_dmac_active_desc(chan);
  356. start_next = !!active;
  357. }
  358. } else {
  359. do {
  360. sg = &active->sg[active->num_completed];
  361. if (sg->hw->id == AXI_DMAC_SG_UNUSED) /* Not yet submitted */
  362. break;
  363. if (!(BIT(sg->hw->id) & completed_transfers))
  364. break;
  365. active->num_completed++;
  366. sg->hw->id = AXI_DMAC_SG_UNUSED;
  367. if (sg->schedule_when_free) {
  368. sg->schedule_when_free = false;
  369. start_next = true;
  370. }
  371. if (sg->partial_len)
  372. axi_dmac_compute_residue(chan, active);
  373. if (active->cyclic)
  374. vchan_cyclic_callback(&active->vdesc);
  375. if (active->num_completed == active->num_sgs ||
  376. sg->partial_len) {
  377. if (active->cyclic) {
  378. active->num_completed = 0; /* wrap around */
  379. } else {
  380. list_del(&active->vdesc.node);
  381. vchan_cookie_complete(&active->vdesc);
  382. active = axi_dmac_active_desc(chan);
  383. }
  384. }
  385. } while (active);
  386. }
  387. return start_next;
  388. }
  389. static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid)
  390. {
  391. struct axi_dmac *dmac = devid;
  392. unsigned int pending;
  393. bool start_next = false;
  394. pending = axi_dmac_read(dmac, AXI_DMAC_REG_IRQ_PENDING);
  395. if (!pending)
  396. return IRQ_NONE;
  397. axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_PENDING, pending);
  398. spin_lock(&dmac->chan.vchan.lock);
  399. /* One or more transfers have finished */
  400. if (pending & AXI_DMAC_IRQ_EOT) {
  401. unsigned int completed;
  402. completed = axi_dmac_read(dmac, AXI_DMAC_REG_TRANSFER_DONE);
  403. start_next = axi_dmac_transfer_done(&dmac->chan, completed);
  404. }
  405. /* Space has become available in the descriptor queue */
  406. if ((pending & AXI_DMAC_IRQ_SOT) || start_next)
  407. axi_dmac_start_transfer(&dmac->chan);
  408. spin_unlock(&dmac->chan.vchan.lock);
  409. return IRQ_HANDLED;
  410. }
  411. static int axi_dmac_terminate_all(struct dma_chan *c)
  412. {
  413. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  414. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  415. unsigned long flags;
  416. LIST_HEAD(head);
  417. spin_lock_irqsave(&chan->vchan.lock, flags);
  418. axi_dmac_write(dmac, AXI_DMAC_REG_CTRL, 0);
  419. chan->next_desc = NULL;
  420. vchan_get_all_descriptors(&chan->vchan, &head);
  421. list_splice_tail_init(&chan->active_descs, &head);
  422. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  423. vchan_dma_desc_free_list(&chan->vchan, &head);
  424. return 0;
  425. }
  426. static void axi_dmac_synchronize(struct dma_chan *c)
  427. {
  428. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  429. vchan_synchronize(&chan->vchan);
  430. }
  431. static void axi_dmac_issue_pending(struct dma_chan *c)
  432. {
  433. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  434. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  435. unsigned long flags;
  436. u32 ctrl = AXI_DMAC_CTRL_ENABLE;
  437. if (chan->hw_sg)
  438. ctrl |= AXI_DMAC_CTRL_ENABLE_SG;
  439. axi_dmac_write(dmac, AXI_DMAC_REG_CTRL, ctrl);
  440. spin_lock_irqsave(&chan->vchan.lock, flags);
  441. if (vchan_issue_pending(&chan->vchan))
  442. axi_dmac_start_transfer(chan);
  443. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  444. }
  445. static struct axi_dmac_desc *
  446. axi_dmac_alloc_desc(struct axi_dmac_chan *chan, unsigned int num_sgs)
  447. {
  448. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  449. struct device *dev = dmac->dma_dev.dev;
  450. struct axi_dmac_hw_desc *hws;
  451. struct axi_dmac_desc *desc;
  452. dma_addr_t hw_phys;
  453. unsigned int i;
  454. desc = kzalloc(struct_size(desc, sg, num_sgs), GFP_NOWAIT);
  455. if (!desc)
  456. return NULL;
  457. desc->num_sgs = num_sgs;
  458. desc->chan = chan;
  459. hws = dma_alloc_coherent(dev, PAGE_ALIGN(num_sgs * sizeof(*hws)),
  460. &hw_phys, GFP_ATOMIC);
  461. if (!hws) {
  462. kfree(desc);
  463. return NULL;
  464. }
  465. for (i = 0; i < num_sgs; i++) {
  466. desc->sg[i].hw = &hws[i];
  467. desc->sg[i].hw_phys = hw_phys + i * sizeof(*hws);
  468. hws[i].id = AXI_DMAC_SG_UNUSED;
  469. hws[i].flags = 0;
  470. /* Link hardware descriptors */
  471. hws[i].next_sg_addr = hw_phys + (i + 1) * sizeof(*hws);
  472. }
  473. /* The last hardware descriptor will trigger an interrupt */
  474. desc->sg[num_sgs - 1].hw->flags = AXI_DMAC_HW_FLAG_LAST | AXI_DMAC_HW_FLAG_IRQ;
  475. return desc;
  476. }
  477. static void axi_dmac_free_desc(struct axi_dmac_desc *desc)
  478. {
  479. struct axi_dmac *dmac = chan_to_axi_dmac(desc->chan);
  480. struct device *dev = dmac->dma_dev.dev;
  481. struct axi_dmac_hw_desc *hw = desc->sg[0].hw;
  482. dma_addr_t hw_phys = desc->sg[0].hw_phys;
  483. dma_free_coherent(dev, PAGE_ALIGN(desc->num_sgs * sizeof(*hw)),
  484. hw, hw_phys);
  485. kfree(desc);
  486. }
  487. static struct axi_dmac_sg *axi_dmac_fill_linear_sg(struct axi_dmac_chan *chan,
  488. enum dma_transfer_direction direction, dma_addr_t addr,
  489. unsigned int num_periods, unsigned int period_len,
  490. struct axi_dmac_sg *sg)
  491. {
  492. unsigned int num_segments, i;
  493. unsigned int segment_size;
  494. unsigned int len;
  495. /* Split into multiple equally sized segments if necessary */
  496. num_segments = DIV_ROUND_UP(period_len, chan->max_length);
  497. segment_size = DIV_ROUND_UP(period_len, num_segments);
  498. /* Take care of alignment */
  499. segment_size = ((segment_size - 1) | chan->length_align_mask) + 1;
  500. for (i = 0; i < num_periods; i++) {
  501. for (len = period_len; len > segment_size; sg++) {
  502. if (direction == DMA_DEV_TO_MEM)
  503. sg->hw->dest_addr = addr;
  504. else
  505. sg->hw->src_addr = addr;
  506. sg->hw->x_len = segment_size - 1;
  507. sg->hw->y_len = 0;
  508. sg->hw->flags = 0;
  509. addr += segment_size;
  510. len -= segment_size;
  511. }
  512. if (direction == DMA_DEV_TO_MEM)
  513. sg->hw->dest_addr = addr;
  514. else
  515. sg->hw->src_addr = addr;
  516. sg->hw->x_len = len - 1;
  517. sg->hw->y_len = 0;
  518. sg++;
  519. addr += len;
  520. }
  521. return sg;
  522. }
  523. static struct dma_async_tx_descriptor *
  524. axi_dmac_prep_peripheral_dma_vec(struct dma_chan *c, const struct dma_vec *vecs,
  525. size_t nb, enum dma_transfer_direction direction,
  526. unsigned long flags)
  527. {
  528. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  529. struct axi_dmac_desc *desc;
  530. unsigned int num_sgs = 0;
  531. struct axi_dmac_sg *dsg;
  532. size_t i;
  533. if (direction != chan->direction)
  534. return NULL;
  535. for (i = 0; i < nb; i++)
  536. num_sgs += DIV_ROUND_UP(vecs[i].len, chan->max_length);
  537. desc = axi_dmac_alloc_desc(chan, num_sgs);
  538. if (!desc)
  539. return NULL;
  540. dsg = desc->sg;
  541. for (i = 0; i < nb; i++) {
  542. if (!axi_dmac_check_addr(chan, vecs[i].addr) ||
  543. !axi_dmac_check_len(chan, vecs[i].len)) {
  544. kfree(desc);
  545. return NULL;
  546. }
  547. dsg = axi_dmac_fill_linear_sg(chan, direction, vecs[i].addr, 1,
  548. vecs[i].len, dsg);
  549. }
  550. desc->cyclic = false;
  551. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  552. }
  553. static struct dma_async_tx_descriptor *axi_dmac_prep_slave_sg(
  554. struct dma_chan *c, struct scatterlist *sgl,
  555. unsigned int sg_len, enum dma_transfer_direction direction,
  556. unsigned long flags, void *context)
  557. {
  558. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  559. struct axi_dmac_desc *desc;
  560. struct axi_dmac_sg *dsg;
  561. struct scatterlist *sg;
  562. unsigned int num_sgs;
  563. unsigned int i;
  564. if (direction != chan->direction)
  565. return NULL;
  566. num_sgs = 0;
  567. for_each_sg(sgl, sg, sg_len, i)
  568. num_sgs += DIV_ROUND_UP(sg_dma_len(sg), chan->max_length);
  569. desc = axi_dmac_alloc_desc(chan, num_sgs);
  570. if (!desc)
  571. return NULL;
  572. dsg = desc->sg;
  573. for_each_sg(sgl, sg, sg_len, i) {
  574. if (!axi_dmac_check_addr(chan, sg_dma_address(sg)) ||
  575. !axi_dmac_check_len(chan, sg_dma_len(sg))) {
  576. axi_dmac_free_desc(desc);
  577. return NULL;
  578. }
  579. dsg = axi_dmac_fill_linear_sg(chan, direction, sg_dma_address(sg), 1,
  580. sg_dma_len(sg), dsg);
  581. }
  582. desc->cyclic = false;
  583. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  584. }
  585. static struct dma_async_tx_descriptor *axi_dmac_prep_dma_cyclic(
  586. struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
  587. size_t period_len, enum dma_transfer_direction direction,
  588. unsigned long flags)
  589. {
  590. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  591. struct axi_dmac_desc *desc;
  592. unsigned int num_periods, num_segments, num_sgs;
  593. if (direction != chan->direction)
  594. return NULL;
  595. if (!axi_dmac_check_len(chan, buf_len) ||
  596. !axi_dmac_check_addr(chan, buf_addr))
  597. return NULL;
  598. if (period_len == 0 || buf_len % period_len)
  599. return NULL;
  600. num_periods = buf_len / period_len;
  601. num_segments = DIV_ROUND_UP(period_len, chan->max_length);
  602. num_sgs = num_periods * num_segments;
  603. desc = axi_dmac_alloc_desc(chan, num_sgs);
  604. if (!desc)
  605. return NULL;
  606. /* Chain the last descriptor to the first, and remove its "last" flag */
  607. desc->sg[num_sgs - 1].hw->next_sg_addr = desc->sg[0].hw_phys;
  608. desc->sg[num_sgs - 1].hw->flags &= ~AXI_DMAC_HW_FLAG_LAST;
  609. axi_dmac_fill_linear_sg(chan, direction, buf_addr, num_periods,
  610. period_len, desc->sg);
  611. desc->cyclic = true;
  612. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  613. }
  614. static struct dma_async_tx_descriptor *axi_dmac_prep_interleaved(
  615. struct dma_chan *c, struct dma_interleaved_template *xt,
  616. unsigned long flags)
  617. {
  618. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  619. struct axi_dmac_desc *desc;
  620. size_t dst_icg, src_icg;
  621. if (xt->frame_size != 1)
  622. return NULL;
  623. if (xt->dir != chan->direction)
  624. return NULL;
  625. if (axi_dmac_src_is_mem(chan)) {
  626. if (!xt->src_inc || !axi_dmac_check_addr(chan, xt->src_start))
  627. return NULL;
  628. }
  629. if (axi_dmac_dest_is_mem(chan)) {
  630. if (!xt->dst_inc || !axi_dmac_check_addr(chan, xt->dst_start))
  631. return NULL;
  632. }
  633. dst_icg = dmaengine_get_dst_icg(xt, &xt->sgl[0]);
  634. src_icg = dmaengine_get_src_icg(xt, &xt->sgl[0]);
  635. if (chan->hw_2d) {
  636. if (!axi_dmac_check_len(chan, xt->sgl[0].size) ||
  637. xt->numf == 0)
  638. return NULL;
  639. if (xt->sgl[0].size + dst_icg > chan->max_length ||
  640. xt->sgl[0].size + src_icg > chan->max_length)
  641. return NULL;
  642. } else {
  643. if (dst_icg != 0 || src_icg != 0)
  644. return NULL;
  645. if (chan->max_length / xt->sgl[0].size < xt->numf)
  646. return NULL;
  647. if (!axi_dmac_check_len(chan, xt->sgl[0].size * xt->numf))
  648. return NULL;
  649. }
  650. desc = axi_dmac_alloc_desc(chan, 1);
  651. if (!desc)
  652. return NULL;
  653. if (axi_dmac_src_is_mem(chan)) {
  654. desc->sg[0].hw->src_addr = xt->src_start;
  655. desc->sg[0].hw->src_stride = xt->sgl[0].size + src_icg;
  656. }
  657. if (axi_dmac_dest_is_mem(chan)) {
  658. desc->sg[0].hw->dest_addr = xt->dst_start;
  659. desc->sg[0].hw->dst_stride = xt->sgl[0].size + dst_icg;
  660. }
  661. if (chan->hw_2d) {
  662. desc->sg[0].hw->x_len = xt->sgl[0].size - 1;
  663. desc->sg[0].hw->y_len = xt->numf - 1;
  664. } else {
  665. desc->sg[0].hw->x_len = xt->sgl[0].size * xt->numf - 1;
  666. desc->sg[0].hw->y_len = 0;
  667. }
  668. if (flags & DMA_CYCLIC)
  669. desc->cyclic = true;
  670. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  671. }
  672. static void axi_dmac_free_chan_resources(struct dma_chan *c)
  673. {
  674. vchan_free_chan_resources(to_virt_chan(c));
  675. }
  676. static void axi_dmac_desc_free(struct virt_dma_desc *vdesc)
  677. {
  678. axi_dmac_free_desc(to_axi_dmac_desc(vdesc));
  679. }
  680. static bool axi_dmac_regmap_rdwr(struct device *dev, unsigned int reg)
  681. {
  682. switch (reg) {
  683. case AXI_DMAC_REG_IRQ_MASK:
  684. case AXI_DMAC_REG_IRQ_SOURCE:
  685. case AXI_DMAC_REG_IRQ_PENDING:
  686. case AXI_DMAC_REG_CTRL:
  687. case AXI_DMAC_REG_TRANSFER_ID:
  688. case AXI_DMAC_REG_START_TRANSFER:
  689. case AXI_DMAC_REG_FLAGS:
  690. case AXI_DMAC_REG_DEST_ADDRESS:
  691. case AXI_DMAC_REG_SRC_ADDRESS:
  692. case AXI_DMAC_REG_X_LENGTH:
  693. case AXI_DMAC_REG_Y_LENGTH:
  694. case AXI_DMAC_REG_DEST_STRIDE:
  695. case AXI_DMAC_REG_SRC_STRIDE:
  696. case AXI_DMAC_REG_TRANSFER_DONE:
  697. case AXI_DMAC_REG_ACTIVE_TRANSFER_ID:
  698. case AXI_DMAC_REG_STATUS:
  699. case AXI_DMAC_REG_CURRENT_SRC_ADDR:
  700. case AXI_DMAC_REG_CURRENT_DEST_ADDR:
  701. case AXI_DMAC_REG_PARTIAL_XFER_LEN:
  702. case AXI_DMAC_REG_PARTIAL_XFER_ID:
  703. case AXI_DMAC_REG_CURRENT_SG_ID:
  704. case AXI_DMAC_REG_SG_ADDRESS:
  705. case AXI_DMAC_REG_SG_ADDRESS_HIGH:
  706. return true;
  707. default:
  708. return false;
  709. }
  710. }
  711. static const struct regmap_config axi_dmac_regmap_config = {
  712. .reg_bits = 32,
  713. .val_bits = 32,
  714. .reg_stride = 4,
  715. .max_register = AXI_DMAC_REG_PARTIAL_XFER_ID,
  716. .readable_reg = axi_dmac_regmap_rdwr,
  717. .writeable_reg = axi_dmac_regmap_rdwr,
  718. };
  719. static void axi_dmac_adjust_chan_params(struct axi_dmac_chan *chan)
  720. {
  721. chan->address_align_mask = max(chan->dest_width, chan->src_width) - 1;
  722. if (axi_dmac_dest_is_mem(chan) && axi_dmac_src_is_mem(chan))
  723. chan->direction = DMA_MEM_TO_MEM;
  724. else if (!axi_dmac_dest_is_mem(chan) && axi_dmac_src_is_mem(chan))
  725. chan->direction = DMA_MEM_TO_DEV;
  726. else if (axi_dmac_dest_is_mem(chan) && !axi_dmac_src_is_mem(chan))
  727. chan->direction = DMA_DEV_TO_MEM;
  728. else
  729. chan->direction = DMA_DEV_TO_DEV;
  730. }
  731. /*
  732. * The configuration stored in the devicetree matches the configuration
  733. * parameters of the peripheral instance and allows the driver to know which
  734. * features are implemented and how it should behave.
  735. */
  736. static int axi_dmac_parse_chan_dt(struct device_node *of_chan,
  737. struct axi_dmac_chan *chan)
  738. {
  739. u32 val;
  740. int ret;
  741. ret = of_property_read_u32(of_chan, "reg", &val);
  742. if (ret)
  743. return ret;
  744. /* We only support 1 channel for now */
  745. if (val != 0)
  746. return -EINVAL;
  747. ret = of_property_read_u32(of_chan, "adi,source-bus-type", &val);
  748. if (ret)
  749. return ret;
  750. if (val > AXI_DMAC_BUS_TYPE_FIFO)
  751. return -EINVAL;
  752. chan->src_type = val;
  753. ret = of_property_read_u32(of_chan, "adi,destination-bus-type", &val);
  754. if (ret)
  755. return ret;
  756. if (val > AXI_DMAC_BUS_TYPE_FIFO)
  757. return -EINVAL;
  758. chan->dest_type = val;
  759. ret = of_property_read_u32(of_chan, "adi,source-bus-width", &val);
  760. if (ret)
  761. return ret;
  762. chan->src_width = val / 8;
  763. ret = of_property_read_u32(of_chan, "adi,destination-bus-width", &val);
  764. if (ret)
  765. return ret;
  766. chan->dest_width = val / 8;
  767. axi_dmac_adjust_chan_params(chan);
  768. return 0;
  769. }
  770. static int axi_dmac_parse_dt(struct device *dev, struct axi_dmac *dmac)
  771. {
  772. struct device_node *of_channels, *of_chan;
  773. int ret;
  774. of_channels = of_get_child_by_name(dev->of_node, "adi,channels");
  775. if (of_channels == NULL)
  776. return -ENODEV;
  777. for_each_child_of_node(of_channels, of_chan) {
  778. ret = axi_dmac_parse_chan_dt(of_chan, &dmac->chan);
  779. if (ret) {
  780. of_node_put(of_chan);
  781. of_node_put(of_channels);
  782. return -EINVAL;
  783. }
  784. }
  785. of_node_put(of_channels);
  786. return 0;
  787. }
  788. static int axi_dmac_read_chan_config(struct device *dev, struct axi_dmac *dmac)
  789. {
  790. struct axi_dmac_chan *chan = &dmac->chan;
  791. unsigned int val, desc;
  792. desc = axi_dmac_read(dmac, AXI_DMAC_REG_INTERFACE_DESC);
  793. if (desc == 0) {
  794. dev_err(dev, "DMA interface register reads zero\n");
  795. return -EFAULT;
  796. }
  797. val = AXI_DMAC_DMA_SRC_TYPE_GET(desc);
  798. if (val > AXI_DMAC_BUS_TYPE_FIFO) {
  799. dev_err(dev, "Invalid source bus type read: %d\n", val);
  800. return -EINVAL;
  801. }
  802. chan->src_type = val;
  803. val = AXI_DMAC_DMA_DST_TYPE_GET(desc);
  804. if (val > AXI_DMAC_BUS_TYPE_FIFO) {
  805. dev_err(dev, "Invalid destination bus type read: %d\n", val);
  806. return -EINVAL;
  807. }
  808. chan->dest_type = val;
  809. val = AXI_DMAC_DMA_SRC_WIDTH_GET(desc);
  810. if (val == 0) {
  811. dev_err(dev, "Source bus width is zero\n");
  812. return -EINVAL;
  813. }
  814. /* widths are stored in log2 */
  815. chan->src_width = 1 << val;
  816. val = AXI_DMAC_DMA_DST_WIDTH_GET(desc);
  817. if (val == 0) {
  818. dev_err(dev, "Destination bus width is zero\n");
  819. return -EINVAL;
  820. }
  821. chan->dest_width = 1 << val;
  822. axi_dmac_adjust_chan_params(chan);
  823. return 0;
  824. }
  825. static int axi_dmac_detect_caps(struct axi_dmac *dmac, unsigned int version)
  826. {
  827. struct axi_dmac_chan *chan = &dmac->chan;
  828. axi_dmac_write(dmac, AXI_DMAC_REG_FLAGS, AXI_DMAC_FLAG_CYCLIC);
  829. if (axi_dmac_read(dmac, AXI_DMAC_REG_FLAGS) == AXI_DMAC_FLAG_CYCLIC)
  830. chan->hw_cyclic = true;
  831. axi_dmac_write(dmac, AXI_DMAC_REG_SG_ADDRESS, 0xffffffff);
  832. if (axi_dmac_read(dmac, AXI_DMAC_REG_SG_ADDRESS))
  833. chan->hw_sg = true;
  834. axi_dmac_write(dmac, AXI_DMAC_REG_Y_LENGTH, 1);
  835. if (axi_dmac_read(dmac, AXI_DMAC_REG_Y_LENGTH) == 1)
  836. chan->hw_2d = true;
  837. axi_dmac_write(dmac, AXI_DMAC_REG_X_LENGTH, 0xffffffff);
  838. chan->max_length = axi_dmac_read(dmac, AXI_DMAC_REG_X_LENGTH);
  839. if (chan->max_length != UINT_MAX)
  840. chan->max_length++;
  841. axi_dmac_write(dmac, AXI_DMAC_REG_DEST_ADDRESS, 0xffffffff);
  842. if (axi_dmac_read(dmac, AXI_DMAC_REG_DEST_ADDRESS) == 0 &&
  843. chan->dest_type == AXI_DMAC_BUS_TYPE_AXI_MM) {
  844. dev_err(dmac->dma_dev.dev,
  845. "Destination memory-mapped interface not supported.");
  846. return -ENODEV;
  847. }
  848. axi_dmac_write(dmac, AXI_DMAC_REG_SRC_ADDRESS, 0xffffffff);
  849. if (axi_dmac_read(dmac, AXI_DMAC_REG_SRC_ADDRESS) == 0 &&
  850. chan->src_type == AXI_DMAC_BUS_TYPE_AXI_MM) {
  851. dev_err(dmac->dma_dev.dev,
  852. "Source memory-mapped interface not supported.");
  853. return -ENODEV;
  854. }
  855. if (version >= ADI_AXI_PCORE_VER(4, 2, 'a'))
  856. chan->hw_partial_xfer = true;
  857. if (version >= ADI_AXI_PCORE_VER(4, 1, 'a')) {
  858. axi_dmac_write(dmac, AXI_DMAC_REG_X_LENGTH, 0x00);
  859. chan->length_align_mask =
  860. axi_dmac_read(dmac, AXI_DMAC_REG_X_LENGTH);
  861. } else {
  862. chan->length_align_mask = chan->address_align_mask;
  863. }
  864. return 0;
  865. }
  866. static void axi_dmac_tasklet_kill(void *task)
  867. {
  868. tasklet_kill(task);
  869. }
  870. static void axi_dmac_free_dma_controller(void *of_node)
  871. {
  872. of_dma_controller_free(of_node);
  873. }
  874. static int axi_dmac_probe(struct platform_device *pdev)
  875. {
  876. struct dma_device *dma_dev;
  877. struct axi_dmac *dmac;
  878. struct regmap *regmap;
  879. unsigned int version;
  880. u32 irq_mask = 0;
  881. int ret;
  882. dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
  883. if (!dmac)
  884. return -ENOMEM;
  885. dmac->irq = platform_get_irq(pdev, 0);
  886. if (dmac->irq < 0)
  887. return dmac->irq;
  888. if (dmac->irq == 0)
  889. return -EINVAL;
  890. dmac->base = devm_platform_ioremap_resource(pdev, 0);
  891. if (IS_ERR(dmac->base))
  892. return PTR_ERR(dmac->base);
  893. dmac->clk = devm_clk_get_enabled(&pdev->dev, NULL);
  894. if (IS_ERR(dmac->clk))
  895. return PTR_ERR(dmac->clk);
  896. version = axi_dmac_read(dmac, ADI_AXI_REG_VERSION);
  897. if (version >= ADI_AXI_PCORE_VER(4, 3, 'a'))
  898. ret = axi_dmac_read_chan_config(&pdev->dev, dmac);
  899. else
  900. ret = axi_dmac_parse_dt(&pdev->dev, dmac);
  901. if (ret < 0)
  902. return ret;
  903. INIT_LIST_HEAD(&dmac->chan.active_descs);
  904. dma_set_max_seg_size(&pdev->dev, UINT_MAX);
  905. dma_dev = &dmac->dma_dev;
  906. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  907. dma_cap_set(DMA_CYCLIC, dma_dev->cap_mask);
  908. dma_cap_set(DMA_INTERLEAVE, dma_dev->cap_mask);
  909. dma_dev->device_free_chan_resources = axi_dmac_free_chan_resources;
  910. dma_dev->device_tx_status = dma_cookie_status;
  911. dma_dev->device_issue_pending = axi_dmac_issue_pending;
  912. dma_dev->device_prep_slave_sg = axi_dmac_prep_slave_sg;
  913. dma_dev->device_prep_peripheral_dma_vec = axi_dmac_prep_peripheral_dma_vec;
  914. dma_dev->device_prep_dma_cyclic = axi_dmac_prep_dma_cyclic;
  915. dma_dev->device_prep_interleaved_dma = axi_dmac_prep_interleaved;
  916. dma_dev->device_terminate_all = axi_dmac_terminate_all;
  917. dma_dev->device_synchronize = axi_dmac_synchronize;
  918. dma_dev->dev = &pdev->dev;
  919. dma_dev->src_addr_widths = BIT(dmac->chan.src_width);
  920. dma_dev->dst_addr_widths = BIT(dmac->chan.dest_width);
  921. dma_dev->directions = BIT(dmac->chan.direction);
  922. dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  923. dma_dev->max_sg_burst = 31; /* 31 SGs maximum in one burst */
  924. INIT_LIST_HEAD(&dma_dev->channels);
  925. dmac->chan.vchan.desc_free = axi_dmac_desc_free;
  926. vchan_init(&dmac->chan.vchan, dma_dev);
  927. ret = axi_dmac_detect_caps(dmac, version);
  928. if (ret)
  929. return ret;
  930. dma_dev->copy_align = (dmac->chan.address_align_mask + 1);
  931. if (dmac->chan.hw_sg)
  932. irq_mask |= AXI_DMAC_IRQ_SOT;
  933. axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_MASK, irq_mask);
  934. if (of_dma_is_coherent(pdev->dev.of_node)) {
  935. ret = axi_dmac_read(dmac, AXI_DMAC_REG_COHERENCY_DESC);
  936. if (version < ADI_AXI_PCORE_VER(4, 4, 'a') ||
  937. !AXI_DMAC_DST_COHERENT_GET(ret)) {
  938. dev_err(dmac->dma_dev.dev,
  939. "Coherent DMA not supported in hardware");
  940. return -EINVAL;
  941. }
  942. }
  943. ret = dmaenginem_async_device_register(dma_dev);
  944. if (ret)
  945. return ret;
  946. /*
  947. * Put the action in here so it get's done before unregistering the DMA
  948. * device.
  949. */
  950. ret = devm_add_action_or_reset(&pdev->dev, axi_dmac_tasklet_kill,
  951. &dmac->chan.vchan.task);
  952. if (ret)
  953. return ret;
  954. ret = of_dma_controller_register(pdev->dev.of_node,
  955. of_dma_xlate_by_chan_id, dma_dev);
  956. if (ret)
  957. return ret;
  958. ret = devm_add_action_or_reset(&pdev->dev, axi_dmac_free_dma_controller,
  959. pdev->dev.of_node);
  960. if (ret)
  961. return ret;
  962. ret = devm_request_irq(&pdev->dev, dmac->irq, axi_dmac_interrupt_handler,
  963. IRQF_SHARED, dev_name(&pdev->dev), dmac);
  964. if (ret)
  965. return ret;
  966. regmap = devm_regmap_init_mmio(&pdev->dev, dmac->base,
  967. &axi_dmac_regmap_config);
  968. return PTR_ERR_OR_ZERO(regmap);
  969. }
  970. static const struct of_device_id axi_dmac_of_match_table[] = {
  971. { .compatible = "adi,axi-dmac-1.00.a" },
  972. { },
  973. };
  974. MODULE_DEVICE_TABLE(of, axi_dmac_of_match_table);
  975. static struct platform_driver axi_dmac_driver = {
  976. .driver = {
  977. .name = "dma-axi-dmac",
  978. .of_match_table = axi_dmac_of_match_table,
  979. },
  980. .probe = axi_dmac_probe,
  981. };
  982. module_platform_driver(axi_dmac_driver);
  983. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  984. MODULE_DESCRIPTION("DMA controller driver for the AXI-DMAC controller");
  985. MODULE_LICENSE("GPL v2");