dma-axi-dmac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. * Driver for the Analog Devices AXI-DMAC core
  3. *
  4. * Copyright 2013-2015 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2.
  8. */
  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/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <dt-bindings/dma/axi-dmac.h>
  23. #include "dmaengine.h"
  24. #include "virt-dma.h"
  25. /*
  26. * The AXI-DMAC is a soft IP core that is used in FPGA designs. The core has
  27. * various instantiation parameters which decided the exact feature set support
  28. * by the core.
  29. *
  30. * Each channel of the core has a source interface and a destination interface.
  31. * The number of channels and the type of the channel interfaces is selected at
  32. * configuration time. A interface can either be a connected to a central memory
  33. * interconnect, which allows access to system memory, or it can be connected to
  34. * a dedicated bus which is directly connected to a data port on a peripheral.
  35. * Given that those are configuration options of the core that are selected when
  36. * it is instantiated this means that they can not be changed by software at
  37. * runtime. By extension this means that each channel is uni-directional. It can
  38. * either be device to memory or memory to device, but not both. Also since the
  39. * device side is a dedicated data bus only connected to a single peripheral
  40. * there is no address than can or needs to be configured for the device side.
  41. */
  42. #define AXI_DMAC_REG_IRQ_MASK 0x80
  43. #define AXI_DMAC_REG_IRQ_PENDING 0x84
  44. #define AXI_DMAC_REG_IRQ_SOURCE 0x88
  45. #define AXI_DMAC_REG_CTRL 0x400
  46. #define AXI_DMAC_REG_TRANSFER_ID 0x404
  47. #define AXI_DMAC_REG_START_TRANSFER 0x408
  48. #define AXI_DMAC_REG_FLAGS 0x40c
  49. #define AXI_DMAC_REG_DEST_ADDRESS 0x410
  50. #define AXI_DMAC_REG_SRC_ADDRESS 0x414
  51. #define AXI_DMAC_REG_X_LENGTH 0x418
  52. #define AXI_DMAC_REG_Y_LENGTH 0x41c
  53. #define AXI_DMAC_REG_DEST_STRIDE 0x420
  54. #define AXI_DMAC_REG_SRC_STRIDE 0x424
  55. #define AXI_DMAC_REG_TRANSFER_DONE 0x428
  56. #define AXI_DMAC_REG_ACTIVE_TRANSFER_ID 0x42c
  57. #define AXI_DMAC_REG_STATUS 0x430
  58. #define AXI_DMAC_REG_CURRENT_SRC_ADDR 0x434
  59. #define AXI_DMAC_REG_CURRENT_DEST_ADDR 0x438
  60. #define AXI_DMAC_CTRL_ENABLE BIT(0)
  61. #define AXI_DMAC_CTRL_PAUSE BIT(1)
  62. #define AXI_DMAC_IRQ_SOT BIT(0)
  63. #define AXI_DMAC_IRQ_EOT BIT(1)
  64. #define AXI_DMAC_FLAG_CYCLIC BIT(0)
  65. struct axi_dmac_sg {
  66. dma_addr_t src_addr;
  67. dma_addr_t dest_addr;
  68. unsigned int x_len;
  69. unsigned int y_len;
  70. unsigned int dest_stride;
  71. unsigned int src_stride;
  72. unsigned int id;
  73. };
  74. struct axi_dmac_desc {
  75. struct virt_dma_desc vdesc;
  76. bool cyclic;
  77. unsigned int num_submitted;
  78. unsigned int num_completed;
  79. unsigned int num_sgs;
  80. struct axi_dmac_sg sg[];
  81. };
  82. struct axi_dmac_chan {
  83. struct virt_dma_chan vchan;
  84. struct axi_dmac_desc *next_desc;
  85. struct list_head active_descs;
  86. enum dma_transfer_direction direction;
  87. unsigned int src_width;
  88. unsigned int dest_width;
  89. unsigned int src_type;
  90. unsigned int dest_type;
  91. unsigned int max_length;
  92. unsigned int align_mask;
  93. bool hw_cyclic;
  94. bool hw_2d;
  95. };
  96. struct axi_dmac {
  97. void __iomem *base;
  98. int irq;
  99. struct clk *clk;
  100. struct dma_device dma_dev;
  101. struct axi_dmac_chan chan;
  102. struct device_dma_parameters dma_parms;
  103. };
  104. static struct axi_dmac *chan_to_axi_dmac(struct axi_dmac_chan *chan)
  105. {
  106. return container_of(chan->vchan.chan.device, struct axi_dmac,
  107. dma_dev);
  108. }
  109. static struct axi_dmac_chan *to_axi_dmac_chan(struct dma_chan *c)
  110. {
  111. return container_of(c, struct axi_dmac_chan, vchan.chan);
  112. }
  113. static struct axi_dmac_desc *to_axi_dmac_desc(struct virt_dma_desc *vdesc)
  114. {
  115. return container_of(vdesc, struct axi_dmac_desc, vdesc);
  116. }
  117. static void axi_dmac_write(struct axi_dmac *axi_dmac, unsigned int reg,
  118. unsigned int val)
  119. {
  120. writel(val, axi_dmac->base + reg);
  121. }
  122. static int axi_dmac_read(struct axi_dmac *axi_dmac, unsigned int reg)
  123. {
  124. return readl(axi_dmac->base + reg);
  125. }
  126. static int axi_dmac_src_is_mem(struct axi_dmac_chan *chan)
  127. {
  128. return chan->src_type == AXI_DMAC_BUS_TYPE_AXI_MM;
  129. }
  130. static int axi_dmac_dest_is_mem(struct axi_dmac_chan *chan)
  131. {
  132. return chan->dest_type == AXI_DMAC_BUS_TYPE_AXI_MM;
  133. }
  134. static bool axi_dmac_check_len(struct axi_dmac_chan *chan, unsigned int len)
  135. {
  136. if (len == 0 || len > chan->max_length)
  137. return false;
  138. if ((len & chan->align_mask) != 0) /* Not aligned */
  139. return false;
  140. return true;
  141. }
  142. static bool axi_dmac_check_addr(struct axi_dmac_chan *chan, dma_addr_t addr)
  143. {
  144. if ((addr & chan->align_mask) != 0) /* Not aligned */
  145. return false;
  146. return true;
  147. }
  148. static void axi_dmac_start_transfer(struct axi_dmac_chan *chan)
  149. {
  150. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  151. struct virt_dma_desc *vdesc;
  152. struct axi_dmac_desc *desc;
  153. struct axi_dmac_sg *sg;
  154. unsigned int flags = 0;
  155. unsigned int val;
  156. val = axi_dmac_read(dmac, AXI_DMAC_REG_START_TRANSFER);
  157. if (val) /* Queue is full, wait for the next SOT IRQ */
  158. return;
  159. desc = chan->next_desc;
  160. if (!desc) {
  161. vdesc = vchan_next_desc(&chan->vchan);
  162. if (!vdesc)
  163. return;
  164. list_move_tail(&vdesc->node, &chan->active_descs);
  165. desc = to_axi_dmac_desc(vdesc);
  166. }
  167. sg = &desc->sg[desc->num_submitted];
  168. desc->num_submitted++;
  169. if (desc->num_submitted == desc->num_sgs)
  170. chan->next_desc = NULL;
  171. else
  172. chan->next_desc = desc;
  173. sg->id = axi_dmac_read(dmac, AXI_DMAC_REG_TRANSFER_ID);
  174. if (axi_dmac_dest_is_mem(chan)) {
  175. axi_dmac_write(dmac, AXI_DMAC_REG_DEST_ADDRESS, sg->dest_addr);
  176. axi_dmac_write(dmac, AXI_DMAC_REG_DEST_STRIDE, sg->dest_stride);
  177. }
  178. if (axi_dmac_src_is_mem(chan)) {
  179. axi_dmac_write(dmac, AXI_DMAC_REG_SRC_ADDRESS, sg->src_addr);
  180. axi_dmac_write(dmac, AXI_DMAC_REG_SRC_STRIDE, sg->src_stride);
  181. }
  182. /*
  183. * If the hardware supports cyclic transfers and there is no callback to
  184. * call, enable hw cyclic mode to avoid unnecessary interrupts.
  185. */
  186. if (chan->hw_cyclic && desc->cyclic && !desc->vdesc.tx.callback)
  187. flags |= AXI_DMAC_FLAG_CYCLIC;
  188. axi_dmac_write(dmac, AXI_DMAC_REG_X_LENGTH, sg->x_len - 1);
  189. axi_dmac_write(dmac, AXI_DMAC_REG_Y_LENGTH, sg->y_len - 1);
  190. axi_dmac_write(dmac, AXI_DMAC_REG_FLAGS, flags);
  191. axi_dmac_write(dmac, AXI_DMAC_REG_START_TRANSFER, 1);
  192. }
  193. static struct axi_dmac_desc *axi_dmac_active_desc(struct axi_dmac_chan *chan)
  194. {
  195. return list_first_entry_or_null(&chan->active_descs,
  196. struct axi_dmac_desc, vdesc.node);
  197. }
  198. static void axi_dmac_transfer_done(struct axi_dmac_chan *chan,
  199. unsigned int completed_transfers)
  200. {
  201. struct axi_dmac_desc *active;
  202. struct axi_dmac_sg *sg;
  203. active = axi_dmac_active_desc(chan);
  204. if (!active)
  205. return;
  206. if (active->cyclic) {
  207. vchan_cyclic_callback(&active->vdesc);
  208. } else {
  209. do {
  210. sg = &active->sg[active->num_completed];
  211. if (!(BIT(sg->id) & completed_transfers))
  212. break;
  213. active->num_completed++;
  214. if (active->num_completed == active->num_sgs) {
  215. list_del(&active->vdesc.node);
  216. vchan_cookie_complete(&active->vdesc);
  217. active = axi_dmac_active_desc(chan);
  218. }
  219. } while (active);
  220. }
  221. }
  222. static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid)
  223. {
  224. struct axi_dmac *dmac = devid;
  225. unsigned int pending;
  226. pending = axi_dmac_read(dmac, AXI_DMAC_REG_IRQ_PENDING);
  227. axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_PENDING, pending);
  228. spin_lock(&dmac->chan.vchan.lock);
  229. /* One or more transfers have finished */
  230. if (pending & AXI_DMAC_IRQ_EOT) {
  231. unsigned int completed;
  232. completed = axi_dmac_read(dmac, AXI_DMAC_REG_TRANSFER_DONE);
  233. axi_dmac_transfer_done(&dmac->chan, completed);
  234. }
  235. /* Space has become available in the descriptor queue */
  236. if (pending & AXI_DMAC_IRQ_SOT)
  237. axi_dmac_start_transfer(&dmac->chan);
  238. spin_unlock(&dmac->chan.vchan.lock);
  239. return IRQ_HANDLED;
  240. }
  241. static int axi_dmac_terminate_all(struct dma_chan *c)
  242. {
  243. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  244. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  245. unsigned long flags;
  246. LIST_HEAD(head);
  247. spin_lock_irqsave(&chan->vchan.lock, flags);
  248. axi_dmac_write(dmac, AXI_DMAC_REG_CTRL, 0);
  249. chan->next_desc = NULL;
  250. vchan_get_all_descriptors(&chan->vchan, &head);
  251. list_splice_tail_init(&chan->active_descs, &head);
  252. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  253. vchan_dma_desc_free_list(&chan->vchan, &head);
  254. return 0;
  255. }
  256. static void axi_dmac_synchronize(struct dma_chan *c)
  257. {
  258. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  259. vchan_synchronize(&chan->vchan);
  260. }
  261. static void axi_dmac_issue_pending(struct dma_chan *c)
  262. {
  263. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  264. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  265. unsigned long flags;
  266. axi_dmac_write(dmac, AXI_DMAC_REG_CTRL, AXI_DMAC_CTRL_ENABLE);
  267. spin_lock_irqsave(&chan->vchan.lock, flags);
  268. if (vchan_issue_pending(&chan->vchan))
  269. axi_dmac_start_transfer(chan);
  270. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  271. }
  272. static struct axi_dmac_desc *axi_dmac_alloc_desc(unsigned int num_sgs)
  273. {
  274. struct axi_dmac_desc *desc;
  275. desc = kzalloc(sizeof(struct axi_dmac_desc) +
  276. sizeof(struct axi_dmac_sg) * num_sgs, GFP_NOWAIT);
  277. if (!desc)
  278. return NULL;
  279. desc->num_sgs = num_sgs;
  280. return desc;
  281. }
  282. static struct dma_async_tx_descriptor *axi_dmac_prep_slave_sg(
  283. struct dma_chan *c, struct scatterlist *sgl,
  284. unsigned int sg_len, enum dma_transfer_direction direction,
  285. unsigned long flags, void *context)
  286. {
  287. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  288. struct axi_dmac_desc *desc;
  289. struct scatterlist *sg;
  290. unsigned int i;
  291. if (direction != chan->direction)
  292. return NULL;
  293. desc = axi_dmac_alloc_desc(sg_len);
  294. if (!desc)
  295. return NULL;
  296. for_each_sg(sgl, sg, sg_len, i) {
  297. if (!axi_dmac_check_addr(chan, sg_dma_address(sg)) ||
  298. !axi_dmac_check_len(chan, sg_dma_len(sg))) {
  299. kfree(desc);
  300. return NULL;
  301. }
  302. if (direction == DMA_DEV_TO_MEM)
  303. desc->sg[i].dest_addr = sg_dma_address(sg);
  304. else
  305. desc->sg[i].src_addr = sg_dma_address(sg);
  306. desc->sg[i].x_len = sg_dma_len(sg);
  307. desc->sg[i].y_len = 1;
  308. }
  309. desc->cyclic = false;
  310. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  311. }
  312. static struct dma_async_tx_descriptor *axi_dmac_prep_dma_cyclic(
  313. struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
  314. size_t period_len, enum dma_transfer_direction direction,
  315. unsigned long flags)
  316. {
  317. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  318. struct axi_dmac_desc *desc;
  319. unsigned int num_periods, i;
  320. if (direction != chan->direction)
  321. return NULL;
  322. if (!axi_dmac_check_len(chan, buf_len) ||
  323. !axi_dmac_check_addr(chan, buf_addr))
  324. return NULL;
  325. if (period_len == 0 || buf_len % period_len)
  326. return NULL;
  327. num_periods = buf_len / period_len;
  328. desc = axi_dmac_alloc_desc(num_periods);
  329. if (!desc)
  330. return NULL;
  331. for (i = 0; i < num_periods; i++) {
  332. if (direction == DMA_DEV_TO_MEM)
  333. desc->sg[i].dest_addr = buf_addr;
  334. else
  335. desc->sg[i].src_addr = buf_addr;
  336. desc->sg[i].x_len = period_len;
  337. desc->sg[i].y_len = 1;
  338. buf_addr += period_len;
  339. }
  340. desc->cyclic = true;
  341. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  342. }
  343. static struct dma_async_tx_descriptor *axi_dmac_prep_interleaved(
  344. struct dma_chan *c, struct dma_interleaved_template *xt,
  345. unsigned long flags)
  346. {
  347. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  348. struct axi_dmac_desc *desc;
  349. size_t dst_icg, src_icg;
  350. if (xt->frame_size != 1)
  351. return NULL;
  352. if (xt->dir != chan->direction)
  353. return NULL;
  354. if (axi_dmac_src_is_mem(chan)) {
  355. if (!xt->src_inc || !axi_dmac_check_addr(chan, xt->src_start))
  356. return NULL;
  357. }
  358. if (axi_dmac_dest_is_mem(chan)) {
  359. if (!xt->dst_inc || !axi_dmac_check_addr(chan, xt->dst_start))
  360. return NULL;
  361. }
  362. dst_icg = dmaengine_get_dst_icg(xt, &xt->sgl[0]);
  363. src_icg = dmaengine_get_src_icg(xt, &xt->sgl[0]);
  364. if (chan->hw_2d) {
  365. if (!axi_dmac_check_len(chan, xt->sgl[0].size) ||
  366. !axi_dmac_check_len(chan, xt->numf))
  367. return NULL;
  368. if (xt->sgl[0].size + dst_icg > chan->max_length ||
  369. xt->sgl[0].size + src_icg > chan->max_length)
  370. return NULL;
  371. } else {
  372. if (dst_icg != 0 || src_icg != 0)
  373. return NULL;
  374. if (chan->max_length / xt->sgl[0].size < xt->numf)
  375. return NULL;
  376. if (!axi_dmac_check_len(chan, xt->sgl[0].size * xt->numf))
  377. return NULL;
  378. }
  379. desc = axi_dmac_alloc_desc(1);
  380. if (!desc)
  381. return NULL;
  382. if (axi_dmac_src_is_mem(chan)) {
  383. desc->sg[0].src_addr = xt->src_start;
  384. desc->sg[0].src_stride = xt->sgl[0].size + src_icg;
  385. }
  386. if (axi_dmac_dest_is_mem(chan)) {
  387. desc->sg[0].dest_addr = xt->dst_start;
  388. desc->sg[0].dest_stride = xt->sgl[0].size + dst_icg;
  389. }
  390. if (chan->hw_2d) {
  391. desc->sg[0].x_len = xt->sgl[0].size;
  392. desc->sg[0].y_len = xt->numf;
  393. } else {
  394. desc->sg[0].x_len = xt->sgl[0].size * xt->numf;
  395. desc->sg[0].y_len = 1;
  396. }
  397. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  398. }
  399. static void axi_dmac_free_chan_resources(struct dma_chan *c)
  400. {
  401. vchan_free_chan_resources(to_virt_chan(c));
  402. }
  403. static void axi_dmac_desc_free(struct virt_dma_desc *vdesc)
  404. {
  405. kfree(container_of(vdesc, struct axi_dmac_desc, vdesc));
  406. }
  407. /*
  408. * The configuration stored in the devicetree matches the configuration
  409. * parameters of the peripheral instance and allows the driver to know which
  410. * features are implemented and how it should behave.
  411. */
  412. static int axi_dmac_parse_chan_dt(struct device_node *of_chan,
  413. struct axi_dmac_chan *chan)
  414. {
  415. u32 val;
  416. int ret;
  417. ret = of_property_read_u32(of_chan, "reg", &val);
  418. if (ret)
  419. return ret;
  420. /* We only support 1 channel for now */
  421. if (val != 0)
  422. return -EINVAL;
  423. ret = of_property_read_u32(of_chan, "adi,source-bus-type", &val);
  424. if (ret)
  425. return ret;
  426. if (val > AXI_DMAC_BUS_TYPE_FIFO)
  427. return -EINVAL;
  428. chan->src_type = val;
  429. ret = of_property_read_u32(of_chan, "adi,destination-bus-type", &val);
  430. if (ret)
  431. return ret;
  432. if (val > AXI_DMAC_BUS_TYPE_FIFO)
  433. return -EINVAL;
  434. chan->dest_type = val;
  435. ret = of_property_read_u32(of_chan, "adi,source-bus-width", &val);
  436. if (ret)
  437. return ret;
  438. chan->src_width = val / 8;
  439. ret = of_property_read_u32(of_chan, "adi,destination-bus-width", &val);
  440. if (ret)
  441. return ret;
  442. chan->dest_width = val / 8;
  443. ret = of_property_read_u32(of_chan, "adi,length-width", &val);
  444. if (ret)
  445. return ret;
  446. if (val >= 32)
  447. chan->max_length = UINT_MAX;
  448. else
  449. chan->max_length = (1ULL << val) - 1;
  450. chan->align_mask = max(chan->dest_width, chan->src_width) - 1;
  451. if (axi_dmac_dest_is_mem(chan) && axi_dmac_src_is_mem(chan))
  452. chan->direction = DMA_MEM_TO_MEM;
  453. else if (!axi_dmac_dest_is_mem(chan) && axi_dmac_src_is_mem(chan))
  454. chan->direction = DMA_MEM_TO_DEV;
  455. else if (axi_dmac_dest_is_mem(chan) && !axi_dmac_src_is_mem(chan))
  456. chan->direction = DMA_DEV_TO_MEM;
  457. else
  458. chan->direction = DMA_DEV_TO_DEV;
  459. chan->hw_cyclic = of_property_read_bool(of_chan, "adi,cyclic");
  460. chan->hw_2d = of_property_read_bool(of_chan, "adi,2d");
  461. return 0;
  462. }
  463. static int axi_dmac_probe(struct platform_device *pdev)
  464. {
  465. struct device_node *of_channels, *of_chan;
  466. struct dma_device *dma_dev;
  467. struct axi_dmac *dmac;
  468. struct resource *res;
  469. int ret;
  470. dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
  471. if (!dmac)
  472. return -ENOMEM;
  473. dmac->irq = platform_get_irq(pdev, 0);
  474. if (dmac->irq <= 0)
  475. return -EINVAL;
  476. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  477. dmac->base = devm_ioremap_resource(&pdev->dev, res);
  478. if (IS_ERR(dmac->base))
  479. return PTR_ERR(dmac->base);
  480. dmac->clk = devm_clk_get(&pdev->dev, NULL);
  481. if (IS_ERR(dmac->clk))
  482. return PTR_ERR(dmac->clk);
  483. INIT_LIST_HEAD(&dmac->chan.active_descs);
  484. of_channels = of_get_child_by_name(pdev->dev.of_node, "adi,channels");
  485. if (of_channels == NULL)
  486. return -ENODEV;
  487. for_each_child_of_node(of_channels, of_chan) {
  488. ret = axi_dmac_parse_chan_dt(of_chan, &dmac->chan);
  489. if (ret) {
  490. of_node_put(of_chan);
  491. of_node_put(of_channels);
  492. return -EINVAL;
  493. }
  494. }
  495. of_node_put(of_channels);
  496. pdev->dev.dma_parms = &dmac->dma_parms;
  497. dma_set_max_seg_size(&pdev->dev, dmac->chan.max_length);
  498. dma_dev = &dmac->dma_dev;
  499. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  500. dma_cap_set(DMA_CYCLIC, dma_dev->cap_mask);
  501. dma_dev->device_free_chan_resources = axi_dmac_free_chan_resources;
  502. dma_dev->device_tx_status = dma_cookie_status;
  503. dma_dev->device_issue_pending = axi_dmac_issue_pending;
  504. dma_dev->device_prep_slave_sg = axi_dmac_prep_slave_sg;
  505. dma_dev->device_prep_dma_cyclic = axi_dmac_prep_dma_cyclic;
  506. dma_dev->device_prep_interleaved_dma = axi_dmac_prep_interleaved;
  507. dma_dev->device_terminate_all = axi_dmac_terminate_all;
  508. dma_dev->device_synchronize = axi_dmac_synchronize;
  509. dma_dev->dev = &pdev->dev;
  510. dma_dev->chancnt = 1;
  511. dma_dev->src_addr_widths = BIT(dmac->chan.src_width);
  512. dma_dev->dst_addr_widths = BIT(dmac->chan.dest_width);
  513. dma_dev->directions = BIT(dmac->chan.direction);
  514. dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  515. INIT_LIST_HEAD(&dma_dev->channels);
  516. dmac->chan.vchan.desc_free = axi_dmac_desc_free;
  517. vchan_init(&dmac->chan.vchan, dma_dev);
  518. ret = clk_prepare_enable(dmac->clk);
  519. if (ret < 0)
  520. return ret;
  521. axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_MASK, 0x00);
  522. ret = dma_async_device_register(dma_dev);
  523. if (ret)
  524. goto err_clk_disable;
  525. ret = of_dma_controller_register(pdev->dev.of_node,
  526. of_dma_xlate_by_chan_id, dma_dev);
  527. if (ret)
  528. goto err_unregister_device;
  529. ret = request_irq(dmac->irq, axi_dmac_interrupt_handler, 0,
  530. dev_name(&pdev->dev), dmac);
  531. if (ret)
  532. goto err_unregister_of;
  533. platform_set_drvdata(pdev, dmac);
  534. return 0;
  535. err_unregister_of:
  536. of_dma_controller_free(pdev->dev.of_node);
  537. err_unregister_device:
  538. dma_async_device_unregister(&dmac->dma_dev);
  539. err_clk_disable:
  540. clk_disable_unprepare(dmac->clk);
  541. return ret;
  542. }
  543. static int axi_dmac_remove(struct platform_device *pdev)
  544. {
  545. struct axi_dmac *dmac = platform_get_drvdata(pdev);
  546. of_dma_controller_free(pdev->dev.of_node);
  547. free_irq(dmac->irq, dmac);
  548. tasklet_kill(&dmac->chan.vchan.task);
  549. dma_async_device_unregister(&dmac->dma_dev);
  550. clk_disable_unprepare(dmac->clk);
  551. return 0;
  552. }
  553. static const struct of_device_id axi_dmac_of_match_table[] = {
  554. { .compatible = "adi,axi-dmac-1.00.a" },
  555. { },
  556. };
  557. static struct platform_driver axi_dmac_driver = {
  558. .driver = {
  559. .name = "dma-axi-dmac",
  560. .of_match_table = axi_dmac_of_match_table,
  561. },
  562. .probe = axi_dmac_probe,
  563. .remove = axi_dmac_remove,
  564. };
  565. module_platform_driver(axi_dmac_driver);
  566. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  567. MODULE_DESCRIPTION("DMA controller driver for the AXI-DMAC controller");
  568. MODULE_LICENSE("GPL v2");