0054-DMA-ralink-add-rt2880-dma-engine.patch 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. From cf93418a4bd5e69f069a65da92537bd4d6191223 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Sun, 27 Jul 2014 09:29:51 +0100
  4. Subject: [PATCH 54/57] DMA: ralink: add rt2880 dma engine
  5. Signed-off-by: John Crispin <blogic@openwrt.org>
  6. ---
  7. drivers/dma/Kconfig | 6 +
  8. drivers/dma/Makefile | 1 +
  9. drivers/dma/dmaengine.c | 26 ++
  10. drivers/dma/ralink-gdma.c | 577 +++++++++++++++++++++++++++++++++++++++++++++
  11. include/linux/dmaengine.h | 1 +
  12. 5 files changed, 611 insertions(+)
  13. create mode 100644 drivers/dma/ralink-gdma.c
  14. --- a/drivers/dma/Kconfig
  15. +++ b/drivers/dma/Kconfig
  16. @@ -409,6 +409,12 @@ config NBPFAXI_DMA
  17. help
  18. Support for "Type-AXI" NBPF DMA IPs from Renesas
  19. +config DMA_RALINK
  20. + tristate "RALINK DMA support"
  21. + depends on RALINK && SOC_MT7620
  22. + select DMA_ENGINE
  23. + select DMA_VIRTUAL_CHANNELS
  24. +
  25. config DMA_ENGINE
  26. bool
  27. --- a/drivers/dma/Makefile
  28. +++ b/drivers/dma/Makefile
  29. @@ -49,3 +49,4 @@ obj-y += xilinx/
  30. obj-$(CONFIG_INTEL_MIC_X100_DMA) += mic_x100_dma.o
  31. obj-$(CONFIG_NBPFAXI_DMA) += nbpfaxi.o
  32. obj-$(CONFIG_DMA_SUN6I) += sun6i-dma.o
  33. +obj-$(CONFIG_DMA_RALINK) += ralink-gdma.o
  34. --- /dev/null
  35. +++ b/drivers/dma/ralink-gdma.c
  36. @@ -0,0 +1,577 @@
  37. +/*
  38. + * Copyright (C) 2013, Lars-Peter Clausen <lars@metafoo.de>
  39. + * GDMA4740 DMAC support
  40. + *
  41. + * This program is free software; you can redistribute it and/or modify it
  42. + * under the terms of the GNU General Public License as published by the
  43. + * Free Software Foundation; either version 2 of the License, or (at your
  44. + * option) any later version.
  45. + *
  46. + * You should have received a copy of the GNU General Public License along
  47. + * with this program; if not, write to the Free Software Foundation, Inc.,
  48. + * 675 Mass Ave, Cambridge, MA 02139, USA.
  49. + *
  50. + */
  51. +
  52. +#include <linux/dmaengine.h>
  53. +#include <linux/dma-mapping.h>
  54. +#include <linux/err.h>
  55. +#include <linux/init.h>
  56. +#include <linux/list.h>
  57. +#include <linux/module.h>
  58. +#include <linux/platform_device.h>
  59. +#include <linux/slab.h>
  60. +#include <linux/spinlock.h>
  61. +#include <linux/irq.h>
  62. +#include <linux/of_dma.h>
  63. +
  64. +#include "virt-dma.h"
  65. +
  66. +#define GDMA_NR_CHANS 16
  67. +
  68. +#define GDMA_REG_SRC_ADDR(x) (0x00 + (x) * 0x10)
  69. +#define GDMA_REG_DST_ADDR(x) (0x04 + (x) * 0x10)
  70. +
  71. +#define GDMA_REG_CTRL0(x) (0x08 + (x) * 0x10)
  72. +#define GDMA_REG_CTRL0_TX_MASK 0xffff
  73. +#define GDMA_REG_CTRL0_TX_SHIFT 16
  74. +#define GDMA_REG_CTRL0_CURR_MASK 0xff
  75. +#define GDMA_REG_CTRL0_CURR_SHIFT 8
  76. +#define GDMA_REG_CTRL0_SRC_ADDR_FIXED BIT(7)
  77. +#define GDMA_REG_CTRL0_DST_ADDR_FIXED BIT(6)
  78. +#define GDMA_REG_CTRL0_BURST_MASK 0x7
  79. +#define GDMA_REG_CTRL0_BURST_SHIFT 3
  80. +#define GDMA_REG_CTRL0_DONE_INT BIT(2)
  81. +#define GDMA_REG_CTRL0_ENABLE BIT(1)
  82. +#define GDMA_REG_CTRL0_HW_MODE 0
  83. +
  84. +#define GDMA_REG_CTRL1(x) (0x0c + (x) * 0x10)
  85. +#define GDMA_REG_CTRL1_SEG_MASK 0xf
  86. +#define GDMA_REG_CTRL1_SEG_SHIFT 22
  87. +#define GDMA_REG_CTRL1_REQ_MASK 0x3f
  88. +#define GDMA_REG_CTRL1_SRC_REQ_SHIFT 16
  89. +#define GDMA_REG_CTRL1_DST_REQ_SHIFT 8
  90. +#define GDMA_REG_CTRL1_CONTINOUS BIT(14)
  91. +#define GDMA_REG_CTRL1_NEXT_MASK 0x1f
  92. +#define GDMA_REG_CTRL1_NEXT_SHIFT 3
  93. +#define GDMA_REG_CTRL1_COHERENT BIT(2)
  94. +#define GDMA_REG_CTRL1_FAIL BIT(1)
  95. +#define GDMA_REG_CTRL1_MASK BIT(0)
  96. +
  97. +#define GDMA_REG_UNMASK_INT 0x200
  98. +#define GDMA_REG_DONE_INT 0x204
  99. +
  100. +#define GDMA_REG_GCT 0x220
  101. +#define GDMA_REG_GCT_CHAN_MASK 0x3
  102. +#define GDMA_REG_GCT_CHAN_SHIFT 3
  103. +#define GDMA_REG_GCT_VER_MASK 0x3
  104. +#define GDMA_REG_GCT_VER_SHIFT 1
  105. +#define GDMA_REG_GCT_ARBIT_RR BIT(0)
  106. +
  107. +enum gdma_dma_transfer_size {
  108. + GDMA_TRANSFER_SIZE_4BYTE = 0,
  109. + GDMA_TRANSFER_SIZE_8BYTE = 1,
  110. + GDMA_TRANSFER_SIZE_16BYTE = 2,
  111. + GDMA_TRANSFER_SIZE_32BYTE = 3,
  112. +};
  113. +
  114. +struct gdma_dma_sg {
  115. + dma_addr_t addr;
  116. + unsigned int len;
  117. +};
  118. +
  119. +struct gdma_dma_desc {
  120. + struct virt_dma_desc vdesc;
  121. +
  122. + enum dma_transfer_direction direction;
  123. + bool cyclic;
  124. +
  125. + unsigned int num_sgs;
  126. + struct gdma_dma_sg sg[];
  127. +};
  128. +
  129. +struct gdma_dmaengine_chan {
  130. + struct virt_dma_chan vchan;
  131. + unsigned int id;
  132. +
  133. + dma_addr_t fifo_addr;
  134. + unsigned int transfer_shift;
  135. +
  136. + struct gdma_dma_desc *desc;
  137. + unsigned int next_sg;
  138. +};
  139. +
  140. +struct gdma_dma_dev {
  141. + struct dma_device ddev;
  142. + void __iomem *base;
  143. + struct clk *clk;
  144. +
  145. + struct gdma_dmaengine_chan chan[GDMA_NR_CHANS];
  146. +};
  147. +
  148. +static struct gdma_dma_dev *gdma_dma_chan_get_dev(
  149. + struct gdma_dmaengine_chan *chan)
  150. +{
  151. + return container_of(chan->vchan.chan.device, struct gdma_dma_dev,
  152. + ddev);
  153. +}
  154. +
  155. +static struct gdma_dmaengine_chan *to_gdma_dma_chan(struct dma_chan *c)
  156. +{
  157. + return container_of(c, struct gdma_dmaengine_chan, vchan.chan);
  158. +}
  159. +
  160. +static struct gdma_dma_desc *to_gdma_dma_desc(struct virt_dma_desc *vdesc)
  161. +{
  162. + return container_of(vdesc, struct gdma_dma_desc, vdesc);
  163. +}
  164. +
  165. +static inline uint32_t gdma_dma_read(struct gdma_dma_dev *dma_dev,
  166. + unsigned int reg)
  167. +{
  168. + return readl(dma_dev->base + reg);
  169. +}
  170. +
  171. +static inline void gdma_dma_write(struct gdma_dma_dev *dma_dev,
  172. + unsigned reg, uint32_t val)
  173. +{
  174. + //printk("gdma --> %p = 0x%08X\n", dma_dev->base + reg, val);
  175. + writel(val, dma_dev->base + reg);
  176. +}
  177. +
  178. +static inline void gdma_dma_write_mask(struct gdma_dma_dev *dma_dev,
  179. + unsigned int reg, uint32_t val, uint32_t mask)
  180. +{
  181. + uint32_t tmp;
  182. +
  183. + tmp = gdma_dma_read(dma_dev, reg);
  184. + tmp &= ~mask;
  185. + tmp |= val;
  186. + gdma_dma_write(dma_dev, reg, tmp);
  187. +}
  188. +
  189. +static struct gdma_dma_desc *gdma_dma_alloc_desc(unsigned int num_sgs)
  190. +{
  191. + return kzalloc(sizeof(struct gdma_dma_desc) +
  192. + sizeof(struct gdma_dma_sg) * num_sgs, GFP_ATOMIC);
  193. +}
  194. +
  195. +static enum gdma_dma_transfer_size gdma_dma_maxburst(u32 maxburst)
  196. +{
  197. + if (maxburst <= 7)
  198. + return GDMA_TRANSFER_SIZE_4BYTE;
  199. + else if (maxburst <= 15)
  200. + return GDMA_TRANSFER_SIZE_8BYTE;
  201. + else if (maxburst <= 31)
  202. + return GDMA_TRANSFER_SIZE_16BYTE;
  203. +
  204. + return GDMA_TRANSFER_SIZE_32BYTE;
  205. +}
  206. +
  207. +static int gdma_dma_slave_config(struct dma_chan *c,
  208. + const struct dma_slave_config *config)
  209. +{
  210. + struct gdma_dmaengine_chan *chan = to_gdma_dma_chan(c);
  211. + struct gdma_dma_dev *dma_dev = gdma_dma_chan_get_dev(chan);
  212. + enum gdma_dma_transfer_size transfer_size;
  213. + uint32_t flags;
  214. + uint32_t ctrl0, ctrl1;
  215. +
  216. + switch (config->direction) {
  217. + case DMA_MEM_TO_DEV:
  218. + ctrl1 = 32 << GDMA_REG_CTRL1_SRC_REQ_SHIFT;
  219. + ctrl1 |= config->slave_id << GDMA_REG_CTRL1_DST_REQ_SHIFT;
  220. + flags = GDMA_REG_CTRL0_DST_ADDR_FIXED;
  221. + transfer_size = gdma_dma_maxburst(config->dst_maxburst);
  222. + chan->fifo_addr = config->dst_addr;
  223. + break;
  224. +
  225. + case DMA_DEV_TO_MEM:
  226. + ctrl1 = config->slave_id << GDMA_REG_CTRL1_SRC_REQ_SHIFT;
  227. + ctrl1 |= 32 << GDMA_REG_CTRL1_DST_REQ_SHIFT;
  228. + flags = GDMA_REG_CTRL0_SRC_ADDR_FIXED;
  229. + transfer_size = gdma_dma_maxburst(config->src_maxburst);
  230. + chan->fifo_addr = config->src_addr;
  231. + break;
  232. +
  233. + default:
  234. + return -EINVAL;
  235. + }
  236. +
  237. + chan->transfer_shift = 1 + transfer_size;
  238. +
  239. + ctrl0 = flags | GDMA_REG_CTRL0_HW_MODE;
  240. + ctrl0 |= GDMA_REG_CTRL0_DONE_INT;
  241. +
  242. + ctrl1 &= ~(GDMA_REG_CTRL1_NEXT_MASK << GDMA_REG_CTRL1_NEXT_SHIFT);
  243. + ctrl1 |= chan->id << GDMA_REG_CTRL1_NEXT_SHIFT;
  244. + ctrl1 |= GDMA_REG_CTRL1_FAIL;
  245. + ctrl1 &= ~GDMA_REG_CTRL1_CONTINOUS;
  246. + gdma_dma_write(dma_dev, GDMA_REG_CTRL0(chan->id), ctrl0);
  247. + gdma_dma_write(dma_dev, GDMA_REG_CTRL1(chan->id), ctrl1);
  248. +
  249. + return 0;
  250. +}
  251. +
  252. +static int gdma_dma_terminate_all(struct dma_chan *c)
  253. +{
  254. + struct gdma_dmaengine_chan *chan = to_gdma_dma_chan(c);
  255. + struct gdma_dma_dev *dma_dev = gdma_dma_chan_get_dev(chan);
  256. + unsigned long flags;
  257. + LIST_HEAD(head);
  258. +
  259. + spin_lock_irqsave(&chan->vchan.lock, flags);
  260. + gdma_dma_write_mask(dma_dev, GDMA_REG_CTRL0(chan->id), 0,
  261. + GDMA_REG_CTRL0_ENABLE);
  262. + chan->desc = NULL;
  263. + vchan_get_all_descriptors(&chan->vchan, &head);
  264. + spin_unlock_irqrestore(&chan->vchan.lock, flags);
  265. +
  266. + vchan_dma_desc_free_list(&chan->vchan, &head);
  267. +
  268. + return 0;
  269. +}
  270. +
  271. +static int gdma_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  272. + unsigned long arg)
  273. +{
  274. + struct dma_slave_config *config = (struct dma_slave_config *)arg;
  275. +
  276. + switch (cmd) {
  277. + case DMA_SLAVE_CONFIG:
  278. + return gdma_dma_slave_config(chan, config);
  279. + case DMA_TERMINATE_ALL:
  280. + return gdma_dma_terminate_all(chan);
  281. + default:
  282. + return -ENOSYS;
  283. + }
  284. +}
  285. +
  286. +static int gdma_dma_start_transfer(struct gdma_dmaengine_chan *chan)
  287. +{
  288. + struct gdma_dma_dev *dma_dev = gdma_dma_chan_get_dev(chan);
  289. + dma_addr_t src_addr, dst_addr;
  290. + struct virt_dma_desc *vdesc;
  291. + struct gdma_dma_sg *sg;
  292. +
  293. + gdma_dma_write_mask(dma_dev, GDMA_REG_CTRL0(chan->id), 0,
  294. + GDMA_REG_CTRL0_ENABLE);
  295. +
  296. + if (!chan->desc) {
  297. + vdesc = vchan_next_desc(&chan->vchan);
  298. + if (!vdesc)
  299. + return 0;
  300. + chan->desc = to_gdma_dma_desc(vdesc);
  301. + chan->next_sg = 0;
  302. + }
  303. +
  304. + if (chan->next_sg == chan->desc->num_sgs)
  305. + chan->next_sg = 0;
  306. +
  307. + sg = &chan->desc->sg[chan->next_sg];
  308. +
  309. + if (chan->desc->direction == DMA_MEM_TO_DEV) {
  310. + src_addr = sg->addr;
  311. + dst_addr = chan->fifo_addr;
  312. + } else {
  313. + src_addr = chan->fifo_addr;
  314. + dst_addr = sg->addr;
  315. + }
  316. + gdma_dma_write(dma_dev, GDMA_REG_SRC_ADDR(chan->id), src_addr);
  317. + gdma_dma_write(dma_dev, GDMA_REG_DST_ADDR(chan->id), dst_addr);
  318. + gdma_dma_write_mask(dma_dev, GDMA_REG_CTRL0(chan->id),
  319. + (sg->len << GDMA_REG_CTRL0_TX_SHIFT) | GDMA_REG_CTRL0_ENABLE,
  320. + GDMA_REG_CTRL0_TX_MASK << GDMA_REG_CTRL0_TX_SHIFT);
  321. + chan->next_sg++;
  322. + gdma_dma_write_mask(dma_dev, GDMA_REG_CTRL1(chan->id), 0, GDMA_REG_CTRL1_MASK);
  323. +
  324. + return 0;
  325. +}
  326. +
  327. +static void gdma_dma_chan_irq(struct gdma_dmaengine_chan *chan)
  328. +{
  329. + spin_lock(&chan->vchan.lock);
  330. + if (chan->desc) {
  331. + if (chan->desc && chan->desc->cyclic) {
  332. + vchan_cyclic_callback(&chan->desc->vdesc);
  333. + } else {
  334. + if (chan->next_sg == chan->desc->num_sgs) {
  335. + chan->desc = NULL;
  336. + vchan_cookie_complete(&chan->desc->vdesc);
  337. + }
  338. + }
  339. + }
  340. + gdma_dma_start_transfer(chan);
  341. + spin_unlock(&chan->vchan.lock);
  342. +}
  343. +
  344. +static irqreturn_t gdma_dma_irq(int irq, void *devid)
  345. +{
  346. + struct gdma_dma_dev *dma_dev = devid;
  347. + uint32_t unmask, done;
  348. + unsigned int i;
  349. +
  350. + unmask = gdma_dma_read(dma_dev, GDMA_REG_UNMASK_INT);
  351. + gdma_dma_write(dma_dev, GDMA_REG_UNMASK_INT, unmask);
  352. + done = gdma_dma_read(dma_dev, GDMA_REG_DONE_INT);
  353. +
  354. + for (i = 0; i < GDMA_NR_CHANS; ++i)
  355. + if (done & BIT(i))
  356. + gdma_dma_chan_irq(&dma_dev->chan[i]);
  357. + gdma_dma_write(dma_dev, GDMA_REG_DONE_INT, done);
  358. +
  359. + return IRQ_HANDLED;
  360. +}
  361. +
  362. +static void gdma_dma_issue_pending(struct dma_chan *c)
  363. +{
  364. + struct gdma_dmaengine_chan *chan = to_gdma_dma_chan(c);
  365. + unsigned long flags;
  366. +
  367. + spin_lock_irqsave(&chan->vchan.lock, flags);
  368. + if (vchan_issue_pending(&chan->vchan) && !chan->desc)
  369. + gdma_dma_start_transfer(chan);
  370. + spin_unlock_irqrestore(&chan->vchan.lock, flags);
  371. +}
  372. +
  373. +static struct dma_async_tx_descriptor *gdma_dma_prep_slave_sg(
  374. + struct dma_chan *c, struct scatterlist *sgl,
  375. + unsigned int sg_len, enum dma_transfer_direction direction,
  376. + unsigned long flags, void *context)
  377. +{
  378. + struct gdma_dmaengine_chan *chan = to_gdma_dma_chan(c);
  379. + struct gdma_dma_desc *desc;
  380. + struct scatterlist *sg;
  381. + unsigned int i;
  382. +
  383. + desc = gdma_dma_alloc_desc(sg_len);
  384. + if (!desc)
  385. + return NULL;
  386. +
  387. + for_each_sg(sgl, sg, sg_len, i) {
  388. + desc->sg[i].addr = sg_dma_address(sg);
  389. + desc->sg[i].len = sg_dma_len(sg);
  390. + }
  391. +
  392. + desc->num_sgs = sg_len;
  393. + desc->direction = direction;
  394. + desc->cyclic = false;
  395. +
  396. + return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  397. +}
  398. +
  399. +static struct dma_async_tx_descriptor *gdma_dma_prep_dma_cyclic(
  400. + struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
  401. + size_t period_len, enum dma_transfer_direction direction,
  402. + unsigned long flags, void *context)
  403. +{
  404. + struct gdma_dmaengine_chan *chan = to_gdma_dma_chan(c);
  405. + struct gdma_dma_desc *desc;
  406. + unsigned int num_periods, i;
  407. +
  408. + if (buf_len % period_len)
  409. + return NULL;
  410. +
  411. + num_periods = buf_len / period_len;
  412. +
  413. + desc = gdma_dma_alloc_desc(num_periods);
  414. + if (!desc)
  415. + return NULL;
  416. +
  417. + for (i = 0; i < num_periods; i++) {
  418. + desc->sg[i].addr = buf_addr;
  419. + desc->sg[i].len = period_len;
  420. + buf_addr += period_len;
  421. + }
  422. +
  423. + desc->num_sgs = num_periods;
  424. + desc->direction = direction;
  425. + desc->cyclic = true;
  426. +
  427. + return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
  428. +}
  429. +
  430. +static size_t gdma_dma_desc_residue(struct gdma_dmaengine_chan *chan,
  431. + struct gdma_dma_desc *desc, unsigned int next_sg)
  432. +{
  433. + struct gdma_dma_dev *dma_dev = gdma_dma_chan_get_dev(chan);
  434. + unsigned int residue, count;
  435. + unsigned int i;
  436. +
  437. + residue = 0;
  438. +
  439. + for (i = next_sg; i < desc->num_sgs; i++)
  440. + residue += desc->sg[i].len;
  441. +
  442. + if (next_sg != 0) {
  443. + count = gdma_dma_read(dma_dev, GDMA_REG_CTRL0(chan->id));
  444. + count >>= GDMA_REG_CTRL0_CURR_SHIFT;
  445. + count &= GDMA_REG_CTRL0_CURR_MASK;
  446. + residue += count << chan->transfer_shift;
  447. + }
  448. +
  449. + return residue;
  450. +}
  451. +
  452. +static enum dma_status gdma_dma_tx_status(struct dma_chan *c,
  453. + dma_cookie_t cookie, struct dma_tx_state *state)
  454. +{
  455. + struct gdma_dmaengine_chan *chan = to_gdma_dma_chan(c);
  456. + struct virt_dma_desc *vdesc;
  457. + enum dma_status status;
  458. + unsigned long flags;
  459. +
  460. + status = dma_cookie_status(c, cookie, state);
  461. + if (status == DMA_SUCCESS || !state)
  462. + return status;
  463. +
  464. + spin_lock_irqsave(&chan->vchan.lock, flags);
  465. + vdesc = vchan_find_desc(&chan->vchan, cookie);
  466. + if (cookie == chan->desc->vdesc.tx.cookie) {
  467. + state->residue = gdma_dma_desc_residue(chan, chan->desc,
  468. + chan->next_sg);
  469. + } else if (vdesc) {
  470. + state->residue = gdma_dma_desc_residue(chan,
  471. + to_gdma_dma_desc(vdesc), 0);
  472. + } else {
  473. + state->residue = 0;
  474. + }
  475. + spin_unlock_irqrestore(&chan->vchan.lock, flags);
  476. +
  477. + return status;
  478. +}
  479. +
  480. +static int gdma_dma_alloc_chan_resources(struct dma_chan *c)
  481. +{
  482. + return 0;
  483. +}
  484. +
  485. +static void gdma_dma_free_chan_resources(struct dma_chan *c)
  486. +{
  487. + vchan_free_chan_resources(to_virt_chan(c));
  488. +}
  489. +
  490. +static void gdma_dma_desc_free(struct virt_dma_desc *vdesc)
  491. +{
  492. + kfree(container_of(vdesc, struct gdma_dma_desc, vdesc));
  493. +}
  494. +
  495. +static struct dma_chan *
  496. +of_dma_xlate_by_chan_id(struct of_phandle_args *dma_spec,
  497. + struct of_dma *ofdma)
  498. +{
  499. + struct gdma_dma_dev *dma_dev = ofdma->of_dma_data;
  500. + unsigned int request = dma_spec->args[0];
  501. +
  502. + if (request >= GDMA_NR_CHANS)
  503. + return NULL;
  504. +
  505. + return dma_get_slave_channel(&(dma_dev->chan[request].vchan.chan));
  506. +}
  507. +
  508. +static int gdma_dma_probe(struct platform_device *pdev)
  509. +{
  510. + struct gdma_dmaengine_chan *chan;
  511. + struct gdma_dma_dev *dma_dev;
  512. + struct dma_device *dd;
  513. + unsigned int i;
  514. + struct resource *res;
  515. + uint32_t gct;
  516. + int ret;
  517. + int irq;
  518. +
  519. +
  520. + dma_dev = devm_kzalloc(&pdev->dev, sizeof(*dma_dev), GFP_KERNEL);
  521. + if (!dma_dev)
  522. + return -EINVAL;
  523. +
  524. + dd = &dma_dev->ddev;
  525. +
  526. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  527. + dma_dev->base = devm_ioremap_resource(&pdev->dev, res);
  528. + if (IS_ERR(dma_dev->base))
  529. + return PTR_ERR(dma_dev->base);
  530. +
  531. + dma_cap_set(DMA_SLAVE, dd->cap_mask);
  532. + dma_cap_set(DMA_CYCLIC, dd->cap_mask);
  533. + dd->device_alloc_chan_resources = gdma_dma_alloc_chan_resources;
  534. + dd->device_free_chan_resources = gdma_dma_free_chan_resources;
  535. + dd->device_tx_status = gdma_dma_tx_status;
  536. + dd->device_issue_pending = gdma_dma_issue_pending;
  537. + dd->device_prep_slave_sg = gdma_dma_prep_slave_sg;
  538. + dd->device_prep_dma_cyclic = gdma_dma_prep_dma_cyclic;
  539. + dd->device_control = gdma_dma_control;
  540. + dd->dev = &pdev->dev;
  541. + dd->chancnt = GDMA_NR_CHANS;
  542. + INIT_LIST_HEAD(&dd->channels);
  543. +
  544. + for (i = 0; i < dd->chancnt; i++) {
  545. + chan = &dma_dev->chan[i];
  546. + chan->id = i;
  547. + chan->vchan.desc_free = gdma_dma_desc_free;
  548. + vchan_init(&chan->vchan, dd);
  549. + }
  550. +
  551. + ret = dma_async_device_register(dd);
  552. + if (ret)
  553. + return ret;
  554. +
  555. + ret = of_dma_controller_register(pdev->dev.of_node,
  556. + of_dma_xlate_by_chan_id, dma_dev);
  557. + if (ret)
  558. + goto err_unregister;
  559. +
  560. + irq = platform_get_irq(pdev, 0);
  561. + ret = request_irq(irq, gdma_dma_irq, 0, dev_name(&pdev->dev), dma_dev);
  562. + if (ret)
  563. + goto err_unregister;
  564. +
  565. + gdma_dma_write(dma_dev, GDMA_REG_UNMASK_INT, 0);
  566. + gdma_dma_write(dma_dev, GDMA_REG_DONE_INT, BIT(dd->chancnt) - 1);
  567. +
  568. + gct = gdma_dma_read(dma_dev, GDMA_REG_GCT);
  569. + dev_info(&pdev->dev, "revision: %d, channels: %d\n",
  570. + (gct >> GDMA_REG_GCT_VER_SHIFT) & GDMA_REG_GCT_VER_MASK,
  571. + 8 << ((gct >> GDMA_REG_GCT_CHAN_SHIFT) & GDMA_REG_GCT_CHAN_MASK));
  572. + platform_set_drvdata(pdev, dma_dev);
  573. +
  574. + gdma_dma_write(dma_dev, GDMA_REG_GCT, GDMA_REG_GCT_ARBIT_RR);
  575. +
  576. + return 0;
  577. +
  578. +err_unregister:
  579. + dma_async_device_unregister(dd);
  580. + return ret;
  581. +}
  582. +
  583. +static int gdma_dma_remove(struct platform_device *pdev)
  584. +{
  585. + struct gdma_dma_dev *dma_dev = platform_get_drvdata(pdev);
  586. + int irq = platform_get_irq(pdev, 0);
  587. +
  588. + free_irq(irq, dma_dev);
  589. + of_dma_controller_free(pdev->dev.of_node);
  590. + dma_async_device_unregister(&dma_dev->ddev);
  591. +
  592. + return 0;
  593. +}
  594. +
  595. +static const struct of_device_id gdma_of_match_table[] = {
  596. + { .compatible = "ralink,rt2880-gdma" },
  597. + { },
  598. +};
  599. +
  600. +static struct platform_driver gdma_dma_driver = {
  601. + .probe = gdma_dma_probe,
  602. + .remove = gdma_dma_remove,
  603. + .driver = {
  604. + .name = "gdma-rt2880",
  605. + .owner = THIS_MODULE,
  606. + .of_match_table = gdma_of_match_table,
  607. + },
  608. +};
  609. +module_platform_driver(gdma_dma_driver);
  610. +
  611. +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  612. +MODULE_DESCRIPTION("GDMA4740 DMA driver");
  613. +MODULE_LICENSE("GPLv2");
  614. --- a/include/linux/dmaengine.h
  615. +++ b/include/linux/dmaengine.h
  616. @@ -1058,6 +1058,7 @@ struct dma_chan *dma_request_slave_chann
  617. const char *name);
  618. struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name);
  619. void dma_release_channel(struct dma_chan *chan);
  620. +struct dma_chan *dma_get_slave_channel(struct dma_chan *chan);
  621. #else
  622. static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
  623. {