dma-axi-dmac.c 18 KB

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