dma-axi-dmac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. if (!pending)
  228. return IRQ_NONE;
  229. axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_PENDING, pending);
  230. spin_lock(&dmac->chan.vchan.lock);
  231. /* One or more transfers have finished */
  232. if (pending & AXI_DMAC_IRQ_EOT) {
  233. unsigned int completed;
  234. completed = axi_dmac_read(dmac, AXI_DMAC_REG_TRANSFER_DONE);
  235. axi_dmac_transfer_done(&dmac->chan, completed);
  236. }
  237. /* Space has become available in the descriptor queue */
  238. if (pending & AXI_DMAC_IRQ_SOT)
  239. axi_dmac_start_transfer(&dmac->chan);
  240. spin_unlock(&dmac->chan.vchan.lock);
  241. return IRQ_HANDLED;
  242. }
  243. static int axi_dmac_terminate_all(struct dma_chan *c)
  244. {
  245. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  246. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  247. unsigned long flags;
  248. LIST_HEAD(head);
  249. spin_lock_irqsave(&chan->vchan.lock, flags);
  250. axi_dmac_write(dmac, AXI_DMAC_REG_CTRL, 0);
  251. chan->next_desc = NULL;
  252. vchan_get_all_descriptors(&chan->vchan, &head);
  253. list_splice_tail_init(&chan->active_descs, &head);
  254. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  255. vchan_dma_desc_free_list(&chan->vchan, &head);
  256. return 0;
  257. }
  258. static void axi_dmac_synchronize(struct dma_chan *c)
  259. {
  260. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  261. vchan_synchronize(&chan->vchan);
  262. }
  263. static void axi_dmac_issue_pending(struct dma_chan *c)
  264. {
  265. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  266. struct axi_dmac *dmac = chan_to_axi_dmac(chan);
  267. unsigned long flags;
  268. axi_dmac_write(dmac, AXI_DMAC_REG_CTRL, AXI_DMAC_CTRL_ENABLE);
  269. spin_lock_irqsave(&chan->vchan.lock, flags);
  270. if (vchan_issue_pending(&chan->vchan))
  271. axi_dmac_start_transfer(chan);
  272. spin_unlock_irqrestore(&chan->vchan.lock, flags);
  273. }
  274. static struct axi_dmac_desc *axi_dmac_alloc_desc(unsigned int num_sgs)
  275. {
  276. struct axi_dmac_desc *desc;
  277. desc = kzalloc(sizeof(struct axi_dmac_desc) +
  278. sizeof(struct axi_dmac_sg) * num_sgs, GFP_NOWAIT);
  279. if (!desc)
  280. return NULL;
  281. desc->num_sgs = num_sgs;
  282. return desc;
  283. }
  284. static struct dma_async_tx_descriptor *axi_dmac_prep_slave_sg(
  285. struct dma_chan *c, struct scatterlist *sgl,
  286. unsigned int sg_len, enum dma_transfer_direction direction,
  287. unsigned long flags, void *context)
  288. {
  289. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  290. struct axi_dmac_desc *desc;
  291. struct scatterlist *sg;
  292. unsigned int i;
  293. if (direction != chan->direction)
  294. return NULL;
  295. desc = axi_dmac_alloc_desc(sg_len);
  296. if (!desc)
  297. return NULL;
  298. for_each_sg(sgl, sg, sg_len, i) {
  299. if (!axi_dmac_check_addr(chan, sg_dma_address(sg)) ||
  300. !axi_dmac_check_len(chan, sg_dma_len(sg))) {
  301. kfree(desc);
  302. return NULL;
  303. }
  304. if (direction == DMA_DEV_TO_MEM)
  305. desc->sg[i].dest_addr = sg_dma_address(sg);
  306. else
  307. desc->sg[i].src_addr = sg_dma_address(sg);
  308. desc->sg[i].x_len = sg_dma_len(sg);
  309. desc->sg[i].y_len = 1;
  310. }
  311. desc->cyclic = false;
  312. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  313. }
  314. static struct dma_async_tx_descriptor *axi_dmac_prep_dma_cyclic(
  315. struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
  316. size_t period_len, enum dma_transfer_direction direction,
  317. unsigned long flags)
  318. {
  319. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  320. struct axi_dmac_desc *desc;
  321. unsigned int num_periods, i;
  322. if (direction != chan->direction)
  323. return NULL;
  324. if (!axi_dmac_check_len(chan, buf_len) ||
  325. !axi_dmac_check_addr(chan, buf_addr))
  326. return NULL;
  327. if (period_len == 0 || buf_len % period_len)
  328. return NULL;
  329. num_periods = buf_len / period_len;
  330. desc = axi_dmac_alloc_desc(num_periods);
  331. if (!desc)
  332. return NULL;
  333. for (i = 0; i < num_periods; i++) {
  334. if (direction == DMA_DEV_TO_MEM)
  335. desc->sg[i].dest_addr = buf_addr;
  336. else
  337. desc->sg[i].src_addr = buf_addr;
  338. desc->sg[i].x_len = period_len;
  339. desc->sg[i].y_len = 1;
  340. buf_addr += period_len;
  341. }
  342. desc->cyclic = true;
  343. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  344. }
  345. static struct dma_async_tx_descriptor *axi_dmac_prep_interleaved(
  346. struct dma_chan *c, struct dma_interleaved_template *xt,
  347. unsigned long flags)
  348. {
  349. struct axi_dmac_chan *chan = to_axi_dmac_chan(c);
  350. struct axi_dmac_desc *desc;
  351. size_t dst_icg, src_icg;
  352. if (xt->frame_size != 1)
  353. return NULL;
  354. if (xt->dir != chan->direction)
  355. return NULL;
  356. if (axi_dmac_src_is_mem(chan)) {
  357. if (!xt->src_inc || !axi_dmac_check_addr(chan, xt->src_start))
  358. return NULL;
  359. }
  360. if (axi_dmac_dest_is_mem(chan)) {
  361. if (!xt->dst_inc || !axi_dmac_check_addr(chan, xt->dst_start))
  362. return NULL;
  363. }
  364. dst_icg = dmaengine_get_dst_icg(xt, &xt->sgl[0]);
  365. src_icg = dmaengine_get_src_icg(xt, &xt->sgl[0]);
  366. if (chan->hw_2d) {
  367. if (!axi_dmac_check_len(chan, xt->sgl[0].size) ||
  368. xt->numf == 0)
  369. return NULL;
  370. if (xt->sgl[0].size + dst_icg > chan->max_length ||
  371. xt->sgl[0].size + src_icg > chan->max_length)
  372. return NULL;
  373. } else {
  374. if (dst_icg != 0 || src_icg != 0)
  375. return NULL;
  376. if (chan->max_length / xt->sgl[0].size < xt->numf)
  377. return NULL;
  378. if (!axi_dmac_check_len(chan, xt->sgl[0].size * xt->numf))
  379. return NULL;
  380. }
  381. desc = axi_dmac_alloc_desc(1);
  382. if (!desc)
  383. return NULL;
  384. if (axi_dmac_src_is_mem(chan)) {
  385. desc->sg[0].src_addr = xt->src_start;
  386. desc->sg[0].src_stride = xt->sgl[0].size + src_icg;
  387. }
  388. if (axi_dmac_dest_is_mem(chan)) {
  389. desc->sg[0].dest_addr = xt->dst_start;
  390. desc->sg[0].dest_stride = xt->sgl[0].size + dst_icg;
  391. }
  392. if (chan->hw_2d) {
  393. desc->sg[0].x_len = xt->sgl[0].size;
  394. desc->sg[0].y_len = xt->numf;
  395. } else {
  396. desc->sg[0].x_len = xt->sgl[0].size * xt->numf;
  397. desc->sg[0].y_len = 1;
  398. }
  399. return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  400. }
  401. static void axi_dmac_free_chan_resources(struct dma_chan *c)
  402. {
  403. vchan_free_chan_resources(to_virt_chan(c));
  404. }
  405. static void axi_dmac_desc_free(struct virt_dma_desc *vdesc)
  406. {
  407. kfree(container_of(vdesc, struct axi_dmac_desc, vdesc));
  408. }
  409. /*
  410. * The configuration stored in the devicetree matches the configuration
  411. * parameters of the peripheral instance and allows the driver to know which
  412. * features are implemented and how it should behave.
  413. */
  414. static int axi_dmac_parse_chan_dt(struct device_node *of_chan,
  415. struct axi_dmac_chan *chan)
  416. {
  417. u32 val;
  418. int ret;
  419. ret = of_property_read_u32(of_chan, "reg", &val);
  420. if (ret)
  421. return ret;
  422. /* We only support 1 channel for now */
  423. if (val != 0)
  424. return -EINVAL;
  425. ret = of_property_read_u32(of_chan, "adi,source-bus-type", &val);
  426. if (ret)
  427. return ret;
  428. if (val > AXI_DMAC_BUS_TYPE_FIFO)
  429. return -EINVAL;
  430. chan->src_type = val;
  431. ret = of_property_read_u32(of_chan, "adi,destination-bus-type", &val);
  432. if (ret)
  433. return ret;
  434. if (val > AXI_DMAC_BUS_TYPE_FIFO)
  435. return -EINVAL;
  436. chan->dest_type = val;
  437. ret = of_property_read_u32(of_chan, "adi,source-bus-width", &val);
  438. if (ret)
  439. return ret;
  440. chan->src_width = val / 8;
  441. ret = of_property_read_u32(of_chan, "adi,destination-bus-width", &val);
  442. if (ret)
  443. return ret;
  444. chan->dest_width = val / 8;
  445. ret = of_property_read_u32(of_chan, "adi,length-width", &val);
  446. if (ret)
  447. return ret;
  448. if (val >= 32)
  449. chan->max_length = UINT_MAX;
  450. else
  451. chan->max_length = (1ULL << val) - 1;
  452. chan->align_mask = max(chan->dest_width, chan->src_width) - 1;
  453. if (axi_dmac_dest_is_mem(chan) && axi_dmac_src_is_mem(chan))
  454. chan->direction = DMA_MEM_TO_MEM;
  455. else if (!axi_dmac_dest_is_mem(chan) && axi_dmac_src_is_mem(chan))
  456. chan->direction = DMA_MEM_TO_DEV;
  457. else if (axi_dmac_dest_is_mem(chan) && !axi_dmac_src_is_mem(chan))
  458. chan->direction = DMA_DEV_TO_MEM;
  459. else
  460. chan->direction = DMA_DEV_TO_DEV;
  461. chan->hw_cyclic = of_property_read_bool(of_chan, "adi,cyclic");
  462. chan->hw_2d = of_property_read_bool(of_chan, "adi,2d");
  463. return 0;
  464. }
  465. static int axi_dmac_probe(struct platform_device *pdev)
  466. {
  467. struct device_node *of_channels, *of_chan;
  468. struct dma_device *dma_dev;
  469. struct axi_dmac *dmac;
  470. struct resource *res;
  471. int ret;
  472. dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
  473. if (!dmac)
  474. return -ENOMEM;
  475. dmac->irq = platform_get_irq(pdev, 0);
  476. if (dmac->irq < 0)
  477. return dmac->irq;
  478. if (dmac->irq == 0)
  479. return -EINVAL;
  480. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  481. dmac->base = devm_ioremap_resource(&pdev->dev, res);
  482. if (IS_ERR(dmac->base))
  483. return PTR_ERR(dmac->base);
  484. dmac->clk = devm_clk_get(&pdev->dev, NULL);
  485. if (IS_ERR(dmac->clk))
  486. return PTR_ERR(dmac->clk);
  487. INIT_LIST_HEAD(&dmac->chan.active_descs);
  488. of_channels = of_get_child_by_name(pdev->dev.of_node, "adi,channels");
  489. if (of_channels == NULL)
  490. return -ENODEV;
  491. for_each_child_of_node(of_channels, of_chan) {
  492. ret = axi_dmac_parse_chan_dt(of_chan, &dmac->chan);
  493. if (ret) {
  494. of_node_put(of_chan);
  495. of_node_put(of_channels);
  496. return -EINVAL;
  497. }
  498. }
  499. of_node_put(of_channels);
  500. pdev->dev.dma_parms = &dmac->dma_parms;
  501. dma_set_max_seg_size(&pdev->dev, dmac->chan.max_length);
  502. dma_dev = &dmac->dma_dev;
  503. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  504. dma_cap_set(DMA_CYCLIC, dma_dev->cap_mask);
  505. dma_dev->device_free_chan_resources = axi_dmac_free_chan_resources;
  506. dma_dev->device_tx_status = dma_cookie_status;
  507. dma_dev->device_issue_pending = axi_dmac_issue_pending;
  508. dma_dev->device_prep_slave_sg = axi_dmac_prep_slave_sg;
  509. dma_dev->device_prep_dma_cyclic = axi_dmac_prep_dma_cyclic;
  510. dma_dev->device_prep_interleaved_dma = axi_dmac_prep_interleaved;
  511. dma_dev->device_terminate_all = axi_dmac_terminate_all;
  512. dma_dev->device_synchronize = axi_dmac_synchronize;
  513. dma_dev->dev = &pdev->dev;
  514. dma_dev->chancnt = 1;
  515. dma_dev->src_addr_widths = BIT(dmac->chan.src_width);
  516. dma_dev->dst_addr_widths = BIT(dmac->chan.dest_width);
  517. dma_dev->directions = BIT(dmac->chan.direction);
  518. dma_dev->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  519. INIT_LIST_HEAD(&dma_dev->channels);
  520. dmac->chan.vchan.desc_free = axi_dmac_desc_free;
  521. vchan_init(&dmac->chan.vchan, dma_dev);
  522. ret = clk_prepare_enable(dmac->clk);
  523. if (ret < 0)
  524. return ret;
  525. axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_MASK, 0x00);
  526. ret = dma_async_device_register(dma_dev);
  527. if (ret)
  528. goto err_clk_disable;
  529. ret = of_dma_controller_register(pdev->dev.of_node,
  530. of_dma_xlate_by_chan_id, dma_dev);
  531. if (ret)
  532. goto err_unregister_device;
  533. ret = request_irq(dmac->irq, axi_dmac_interrupt_handler, 0,
  534. dev_name(&pdev->dev), dmac);
  535. if (ret)
  536. goto err_unregister_of;
  537. platform_set_drvdata(pdev, dmac);
  538. return 0;
  539. err_unregister_of:
  540. of_dma_controller_free(pdev->dev.of_node);
  541. err_unregister_device:
  542. dma_async_device_unregister(&dmac->dma_dev);
  543. err_clk_disable:
  544. clk_disable_unprepare(dmac->clk);
  545. return ret;
  546. }
  547. static int axi_dmac_remove(struct platform_device *pdev)
  548. {
  549. struct axi_dmac *dmac = platform_get_drvdata(pdev);
  550. of_dma_controller_free(pdev->dev.of_node);
  551. free_irq(dmac->irq, dmac);
  552. tasklet_kill(&dmac->chan.vchan.task);
  553. dma_async_device_unregister(&dmac->dma_dev);
  554. clk_disable_unprepare(dmac->clk);
  555. return 0;
  556. }
  557. static const struct of_device_id axi_dmac_of_match_table[] = {
  558. { .compatible = "adi,axi-dmac-1.00.a" },
  559. { },
  560. };
  561. MODULE_DEVICE_TABLE(of, axi_dmac_of_match_table);
  562. static struct platform_driver axi_dmac_driver = {
  563. .driver = {
  564. .name = "dma-axi-dmac",
  565. .of_match_table = axi_dmac_of_match_table,
  566. },
  567. .probe = axi_dmac_probe,
  568. .remove = axi_dmac_remove,
  569. };
  570. module_platform_driver(axi_dmac_driver);
  571. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  572. MODULE_DESCRIPTION("DMA controller driver for the AXI-DMAC controller");
  573. MODULE_LICENSE("GPL v2");