dma-axi-dmac.c 19 KB

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