mmp_pdma.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * Copyright 2012 Marvell International Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/types.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/slab.h>
  15. #include <linux/dmaengine.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/device.h>
  18. #include <linux/platform_data/mmp_dma.h>
  19. #include <linux/dmapool.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_dma.h>
  22. #include <linux/of.h>
  23. #include <linux/dma/mmp-pdma.h>
  24. #include "dmaengine.h"
  25. #define DCSR 0x0000
  26. #define DALGN 0x00a0
  27. #define DINT 0x00f0
  28. #define DDADR 0x0200
  29. #define DSADR 0x0204
  30. #define DTADR 0x0208
  31. #define DCMD 0x020c
  32. #define DCSR_RUN (1 << 31) /* Run Bit (read / write) */
  33. #define DCSR_NODESC (1 << 30) /* No-Descriptor Fetch (read / write) */
  34. #define DCSR_STOPIRQEN (1 << 29) /* Stop Interrupt Enable (read / write) */
  35. #define DCSR_REQPEND (1 << 8) /* Request Pending (read-only) */
  36. #define DCSR_STOPSTATE (1 << 3) /* Stop State (read-only) */
  37. #define DCSR_ENDINTR (1 << 2) /* End Interrupt (read / write) */
  38. #define DCSR_STARTINTR (1 << 1) /* Start Interrupt (read / write) */
  39. #define DCSR_BUSERR (1 << 0) /* Bus Error Interrupt (read / write) */
  40. #define DCSR_EORIRQEN (1 << 28) /* End of Receive Interrupt Enable (R/W) */
  41. #define DCSR_EORJMPEN (1 << 27) /* Jump to next descriptor on EOR */
  42. #define DCSR_EORSTOPEN (1 << 26) /* STOP on an EOR */
  43. #define DCSR_SETCMPST (1 << 25) /* Set Descriptor Compare Status */
  44. #define DCSR_CLRCMPST (1 << 24) /* Clear Descriptor Compare Status */
  45. #define DCSR_CMPST (1 << 10) /* The Descriptor Compare Status */
  46. #define DCSR_EORINTR (1 << 9) /* The end of Receive */
  47. #define DRCMR(n) ((((n) < 64) ? 0x0100 : 0x1100) + \
  48. (((n) & 0x3f) << 2))
  49. #define DRCMR_MAPVLD (1 << 7) /* Map Valid (read / write) */
  50. #define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */
  51. #define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */
  52. #define DDADR_STOP (1 << 0) /* Stop (read / write) */
  53. #define DCMD_INCSRCADDR (1 << 31) /* Source Address Increment Setting. */
  54. #define DCMD_INCTRGADDR (1 << 30) /* Target Address Increment Setting. */
  55. #define DCMD_FLOWSRC (1 << 29) /* Flow Control by the source. */
  56. #define DCMD_FLOWTRG (1 << 28) /* Flow Control by the target. */
  57. #define DCMD_STARTIRQEN (1 << 22) /* Start Interrupt Enable */
  58. #define DCMD_ENDIRQEN (1 << 21) /* End Interrupt Enable */
  59. #define DCMD_ENDIAN (1 << 18) /* Device Endian-ness. */
  60. #define DCMD_BURST8 (1 << 16) /* 8 byte burst */
  61. #define DCMD_BURST16 (2 << 16) /* 16 byte burst */
  62. #define DCMD_BURST32 (3 << 16) /* 32 byte burst */
  63. #define DCMD_WIDTH1 (1 << 14) /* 1 byte width */
  64. #define DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */
  65. #define DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */
  66. #define DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */
  67. #define PDMA_ALIGNMENT 3
  68. #define PDMA_MAX_DESC_BYTES DCMD_LENGTH
  69. struct mmp_pdma_desc_hw {
  70. u32 ddadr; /* Points to the next descriptor + flags */
  71. u32 dsadr; /* DSADR value for the current transfer */
  72. u32 dtadr; /* DTADR value for the current transfer */
  73. u32 dcmd; /* DCMD value for the current transfer */
  74. } __aligned(32);
  75. struct mmp_pdma_desc_sw {
  76. struct mmp_pdma_desc_hw desc;
  77. struct list_head node;
  78. struct list_head tx_list;
  79. struct dma_async_tx_descriptor async_tx;
  80. };
  81. struct mmp_pdma_phy;
  82. struct mmp_pdma_chan {
  83. struct device *dev;
  84. struct dma_chan chan;
  85. struct dma_async_tx_descriptor desc;
  86. struct mmp_pdma_phy *phy;
  87. enum dma_transfer_direction dir;
  88. struct mmp_pdma_desc_sw *cyclic_first; /* first desc_sw if channel
  89. * is in cyclic mode */
  90. /* channel's basic info */
  91. struct tasklet_struct tasklet;
  92. u32 dcmd;
  93. u32 drcmr;
  94. u32 dev_addr;
  95. /* list for desc */
  96. spinlock_t desc_lock; /* Descriptor list lock */
  97. struct list_head chain_pending; /* Link descriptors queue for pending */
  98. struct list_head chain_running; /* Link descriptors queue for running */
  99. bool idle; /* channel statue machine */
  100. bool byte_align;
  101. struct dma_pool *desc_pool; /* Descriptors pool */
  102. };
  103. struct mmp_pdma_phy {
  104. int idx;
  105. void __iomem *base;
  106. struct mmp_pdma_chan *vchan;
  107. };
  108. struct mmp_pdma_device {
  109. int dma_channels;
  110. void __iomem *base;
  111. struct device *dev;
  112. struct dma_device device;
  113. struct mmp_pdma_phy *phy;
  114. spinlock_t phy_lock; /* protect alloc/free phy channels */
  115. };
  116. #define tx_to_mmp_pdma_desc(tx) container_of(tx, struct mmp_pdma_desc_sw, async_tx)
  117. #define to_mmp_pdma_desc(lh) container_of(lh, struct mmp_pdma_desc_sw, node)
  118. #define to_mmp_pdma_chan(dchan) container_of(dchan, struct mmp_pdma_chan, chan)
  119. #define to_mmp_pdma_dev(dmadev) container_of(dmadev, struct mmp_pdma_device, device)
  120. static void set_desc(struct mmp_pdma_phy *phy, dma_addr_t addr)
  121. {
  122. u32 reg = (phy->idx << 4) + DDADR;
  123. writel(addr, phy->base + reg);
  124. }
  125. static void enable_chan(struct mmp_pdma_phy *phy)
  126. {
  127. u32 reg, dalgn;
  128. if (!phy->vchan)
  129. return;
  130. reg = DRCMR(phy->vchan->drcmr);
  131. writel(DRCMR_MAPVLD | phy->idx, phy->base + reg);
  132. dalgn = readl(phy->base + DALGN);
  133. if (phy->vchan->byte_align)
  134. dalgn |= 1 << phy->idx;
  135. else
  136. dalgn &= ~(1 << phy->idx);
  137. writel(dalgn, phy->base + DALGN);
  138. reg = (phy->idx << 2) + DCSR;
  139. writel(readl(phy->base + reg) | DCSR_RUN,
  140. phy->base + reg);
  141. }
  142. static void disable_chan(struct mmp_pdma_phy *phy)
  143. {
  144. u32 reg;
  145. if (phy) {
  146. reg = (phy->idx << 2) + DCSR;
  147. writel(readl(phy->base + reg) & ~DCSR_RUN,
  148. phy->base + reg);
  149. }
  150. }
  151. static int clear_chan_irq(struct mmp_pdma_phy *phy)
  152. {
  153. u32 dcsr;
  154. u32 dint = readl(phy->base + DINT);
  155. u32 reg = (phy->idx << 2) + DCSR;
  156. if (dint & BIT(phy->idx)) {
  157. /* clear irq */
  158. dcsr = readl(phy->base + reg);
  159. writel(dcsr, phy->base + reg);
  160. if ((dcsr & DCSR_BUSERR) && (phy->vchan))
  161. dev_warn(phy->vchan->dev, "DCSR_BUSERR\n");
  162. return 0;
  163. }
  164. return -EAGAIN;
  165. }
  166. static irqreturn_t mmp_pdma_chan_handler(int irq, void *dev_id)
  167. {
  168. struct mmp_pdma_phy *phy = dev_id;
  169. if (clear_chan_irq(phy) == 0) {
  170. tasklet_schedule(&phy->vchan->tasklet);
  171. return IRQ_HANDLED;
  172. } else
  173. return IRQ_NONE;
  174. }
  175. static irqreturn_t mmp_pdma_int_handler(int irq, void *dev_id)
  176. {
  177. struct mmp_pdma_device *pdev = dev_id;
  178. struct mmp_pdma_phy *phy;
  179. u32 dint = readl(pdev->base + DINT);
  180. int i, ret;
  181. int irq_num = 0;
  182. while (dint) {
  183. i = __ffs(dint);
  184. dint &= (dint - 1);
  185. phy = &pdev->phy[i];
  186. ret = mmp_pdma_chan_handler(irq, phy);
  187. if (ret == IRQ_HANDLED)
  188. irq_num++;
  189. }
  190. if (irq_num)
  191. return IRQ_HANDLED;
  192. else
  193. return IRQ_NONE;
  194. }
  195. /* lookup free phy channel as descending priority */
  196. static struct mmp_pdma_phy *lookup_phy(struct mmp_pdma_chan *pchan)
  197. {
  198. int prio, i;
  199. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  200. struct mmp_pdma_phy *phy, *found = NULL;
  201. unsigned long flags;
  202. /*
  203. * dma channel priorities
  204. * ch 0 - 3, 16 - 19 <--> (0)
  205. * ch 4 - 7, 20 - 23 <--> (1)
  206. * ch 8 - 11, 24 - 27 <--> (2)
  207. * ch 12 - 15, 28 - 31 <--> (3)
  208. */
  209. spin_lock_irqsave(&pdev->phy_lock, flags);
  210. for (prio = 0; prio <= (((pdev->dma_channels - 1) & 0xf) >> 2); prio++) {
  211. for (i = 0; i < pdev->dma_channels; i++) {
  212. if (prio != ((i & 0xf) >> 2))
  213. continue;
  214. phy = &pdev->phy[i];
  215. if (!phy->vchan) {
  216. phy->vchan = pchan;
  217. found = phy;
  218. goto out_unlock;
  219. }
  220. }
  221. }
  222. out_unlock:
  223. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  224. return found;
  225. }
  226. static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
  227. {
  228. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  229. unsigned long flags;
  230. u32 reg;
  231. if (!pchan->phy)
  232. return;
  233. /* clear the channel mapping in DRCMR */
  234. reg = DRCMR(pchan->phy->vchan->drcmr);
  235. writel(0, pchan->phy->base + reg);
  236. spin_lock_irqsave(&pdev->phy_lock, flags);
  237. pchan->phy->vchan = NULL;
  238. pchan->phy = NULL;
  239. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  240. }
  241. /**
  242. * start_pending_queue - transfer any pending transactions
  243. * pending list ==> running list
  244. */
  245. static void start_pending_queue(struct mmp_pdma_chan *chan)
  246. {
  247. struct mmp_pdma_desc_sw *desc;
  248. /* still in running, irq will start the pending list */
  249. if (!chan->idle) {
  250. dev_dbg(chan->dev, "DMA controller still busy\n");
  251. return;
  252. }
  253. if (list_empty(&chan->chain_pending)) {
  254. /* chance to re-fetch phy channel with higher prio */
  255. mmp_pdma_free_phy(chan);
  256. dev_dbg(chan->dev, "no pending list\n");
  257. return;
  258. }
  259. if (!chan->phy) {
  260. chan->phy = lookup_phy(chan);
  261. if (!chan->phy) {
  262. dev_dbg(chan->dev, "no free dma channel\n");
  263. return;
  264. }
  265. }
  266. /*
  267. * pending -> running
  268. * reintilize pending list
  269. */
  270. desc = list_first_entry(&chan->chain_pending,
  271. struct mmp_pdma_desc_sw, node);
  272. list_splice_tail_init(&chan->chain_pending, &chan->chain_running);
  273. /*
  274. * Program the descriptor's address into the DMA controller,
  275. * then start the DMA transaction
  276. */
  277. set_desc(chan->phy, desc->async_tx.phys);
  278. enable_chan(chan->phy);
  279. chan->idle = false;
  280. }
  281. /* desc->tx_list ==> pending list */
  282. static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx)
  283. {
  284. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan);
  285. struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx);
  286. struct mmp_pdma_desc_sw *child;
  287. unsigned long flags;
  288. dma_cookie_t cookie = -EBUSY;
  289. spin_lock_irqsave(&chan->desc_lock, flags);
  290. list_for_each_entry(child, &desc->tx_list, node) {
  291. cookie = dma_cookie_assign(&child->async_tx);
  292. }
  293. /* softly link to pending list - desc->tx_list ==> pending list */
  294. list_splice_tail_init(&desc->tx_list, &chan->chain_pending);
  295. spin_unlock_irqrestore(&chan->desc_lock, flags);
  296. return cookie;
  297. }
  298. static struct mmp_pdma_desc_sw *
  299. mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan)
  300. {
  301. struct mmp_pdma_desc_sw *desc;
  302. dma_addr_t pdesc;
  303. desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
  304. if (!desc) {
  305. dev_err(chan->dev, "out of memory for link descriptor\n");
  306. return NULL;
  307. }
  308. memset(desc, 0, sizeof(*desc));
  309. INIT_LIST_HEAD(&desc->tx_list);
  310. dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
  311. /* each desc has submit */
  312. desc->async_tx.tx_submit = mmp_pdma_tx_submit;
  313. desc->async_tx.phys = pdesc;
  314. return desc;
  315. }
  316. /**
  317. * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel.
  318. *
  319. * This function will create a dma pool for descriptor allocation.
  320. * Request irq only when channel is requested
  321. * Return - The number of allocated descriptors.
  322. */
  323. static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan)
  324. {
  325. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  326. if (chan->desc_pool)
  327. return 1;
  328. chan->desc_pool =
  329. dma_pool_create(dev_name(&dchan->dev->device), chan->dev,
  330. sizeof(struct mmp_pdma_desc_sw),
  331. __alignof__(struct mmp_pdma_desc_sw), 0);
  332. if (!chan->desc_pool) {
  333. dev_err(chan->dev, "unable to allocate descriptor pool\n");
  334. return -ENOMEM;
  335. }
  336. mmp_pdma_free_phy(chan);
  337. chan->idle = true;
  338. chan->dev_addr = 0;
  339. return 1;
  340. }
  341. static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan,
  342. struct list_head *list)
  343. {
  344. struct mmp_pdma_desc_sw *desc, *_desc;
  345. list_for_each_entry_safe(desc, _desc, list, node) {
  346. list_del(&desc->node);
  347. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  348. }
  349. }
  350. static void mmp_pdma_free_chan_resources(struct dma_chan *dchan)
  351. {
  352. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  353. unsigned long flags;
  354. spin_lock_irqsave(&chan->desc_lock, flags);
  355. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  356. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  357. spin_unlock_irqrestore(&chan->desc_lock, flags);
  358. dma_pool_destroy(chan->desc_pool);
  359. chan->desc_pool = NULL;
  360. chan->idle = true;
  361. chan->dev_addr = 0;
  362. mmp_pdma_free_phy(chan);
  363. return;
  364. }
  365. static struct dma_async_tx_descriptor *
  366. mmp_pdma_prep_memcpy(struct dma_chan *dchan,
  367. dma_addr_t dma_dst, dma_addr_t dma_src,
  368. size_t len, unsigned long flags)
  369. {
  370. struct mmp_pdma_chan *chan;
  371. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  372. size_t copy = 0;
  373. if (!dchan)
  374. return NULL;
  375. if (!len)
  376. return NULL;
  377. chan = to_mmp_pdma_chan(dchan);
  378. chan->byte_align = false;
  379. if (!chan->dir) {
  380. chan->dir = DMA_MEM_TO_MEM;
  381. chan->dcmd = DCMD_INCTRGADDR | DCMD_INCSRCADDR;
  382. chan->dcmd |= DCMD_BURST32;
  383. }
  384. do {
  385. /* Allocate the link descriptor from DMA pool */
  386. new = mmp_pdma_alloc_descriptor(chan);
  387. if (!new) {
  388. dev_err(chan->dev, "no memory for desc\n");
  389. goto fail;
  390. }
  391. copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
  392. if (dma_src & 0x7 || dma_dst & 0x7)
  393. chan->byte_align = true;
  394. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy);
  395. new->desc.dsadr = dma_src;
  396. new->desc.dtadr = dma_dst;
  397. if (!first)
  398. first = new;
  399. else
  400. prev->desc.ddadr = new->async_tx.phys;
  401. new->async_tx.cookie = 0;
  402. async_tx_ack(&new->async_tx);
  403. prev = new;
  404. len -= copy;
  405. if (chan->dir == DMA_MEM_TO_DEV) {
  406. dma_src += copy;
  407. } else if (chan->dir == DMA_DEV_TO_MEM) {
  408. dma_dst += copy;
  409. } else if (chan->dir == DMA_MEM_TO_MEM) {
  410. dma_src += copy;
  411. dma_dst += copy;
  412. }
  413. /* Insert the link descriptor to the LD ring */
  414. list_add_tail(&new->node, &first->tx_list);
  415. } while (len);
  416. first->async_tx.flags = flags; /* client is in control of this ack */
  417. first->async_tx.cookie = -EBUSY;
  418. /* last desc and fire IRQ */
  419. new->desc.ddadr = DDADR_STOP;
  420. new->desc.dcmd |= DCMD_ENDIRQEN;
  421. chan->cyclic_first = NULL;
  422. return &first->async_tx;
  423. fail:
  424. if (first)
  425. mmp_pdma_free_desc_list(chan, &first->tx_list);
  426. return NULL;
  427. }
  428. static struct dma_async_tx_descriptor *
  429. mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  430. unsigned int sg_len, enum dma_transfer_direction dir,
  431. unsigned long flags, void *context)
  432. {
  433. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  434. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  435. size_t len, avail;
  436. struct scatterlist *sg;
  437. dma_addr_t addr;
  438. int i;
  439. if ((sgl == NULL) || (sg_len == 0))
  440. return NULL;
  441. chan->byte_align = false;
  442. for_each_sg(sgl, sg, sg_len, i) {
  443. addr = sg_dma_address(sg);
  444. avail = sg_dma_len(sgl);
  445. do {
  446. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  447. if (addr & 0x7)
  448. chan->byte_align = true;
  449. /* allocate and populate the descriptor */
  450. new = mmp_pdma_alloc_descriptor(chan);
  451. if (!new) {
  452. dev_err(chan->dev, "no memory for desc\n");
  453. goto fail;
  454. }
  455. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len);
  456. if (dir == DMA_MEM_TO_DEV) {
  457. new->desc.dsadr = addr;
  458. new->desc.dtadr = chan->dev_addr;
  459. } else {
  460. new->desc.dsadr = chan->dev_addr;
  461. new->desc.dtadr = addr;
  462. }
  463. if (!first)
  464. first = new;
  465. else
  466. prev->desc.ddadr = new->async_tx.phys;
  467. new->async_tx.cookie = 0;
  468. async_tx_ack(&new->async_tx);
  469. prev = new;
  470. /* Insert the link descriptor to the LD ring */
  471. list_add_tail(&new->node, &first->tx_list);
  472. /* update metadata */
  473. addr += len;
  474. avail -= len;
  475. } while (avail);
  476. }
  477. first->async_tx.cookie = -EBUSY;
  478. first->async_tx.flags = flags;
  479. /* last desc and fire IRQ */
  480. new->desc.ddadr = DDADR_STOP;
  481. new->desc.dcmd |= DCMD_ENDIRQEN;
  482. chan->dir = dir;
  483. chan->cyclic_first = NULL;
  484. return &first->async_tx;
  485. fail:
  486. if (first)
  487. mmp_pdma_free_desc_list(chan, &first->tx_list);
  488. return NULL;
  489. }
  490. static struct dma_async_tx_descriptor *mmp_pdma_prep_dma_cyclic(
  491. struct dma_chan *dchan, dma_addr_t buf_addr, size_t len,
  492. size_t period_len, enum dma_transfer_direction direction,
  493. unsigned long flags, void *context)
  494. {
  495. struct mmp_pdma_chan *chan;
  496. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  497. dma_addr_t dma_src, dma_dst;
  498. if (!dchan || !len || !period_len)
  499. return NULL;
  500. /* the buffer length must be a multiple of period_len */
  501. if (len % period_len != 0)
  502. return NULL;
  503. if (period_len > PDMA_MAX_DESC_BYTES)
  504. return NULL;
  505. chan = to_mmp_pdma_chan(dchan);
  506. switch (direction) {
  507. case DMA_MEM_TO_DEV:
  508. dma_src = buf_addr;
  509. dma_dst = chan->dev_addr;
  510. break;
  511. case DMA_DEV_TO_MEM:
  512. dma_dst = buf_addr;
  513. dma_src = chan->dev_addr;
  514. break;
  515. default:
  516. dev_err(chan->dev, "Unsupported direction for cyclic DMA\n");
  517. return NULL;
  518. }
  519. chan->dir = direction;
  520. do {
  521. /* Allocate the link descriptor from DMA pool */
  522. new = mmp_pdma_alloc_descriptor(chan);
  523. if (!new) {
  524. dev_err(chan->dev, "no memory for desc\n");
  525. goto fail;
  526. }
  527. new->desc.dcmd = chan->dcmd | DCMD_ENDIRQEN |
  528. (DCMD_LENGTH & period_len);
  529. new->desc.dsadr = dma_src;
  530. new->desc.dtadr = dma_dst;
  531. if (!first)
  532. first = new;
  533. else
  534. prev->desc.ddadr = new->async_tx.phys;
  535. new->async_tx.cookie = 0;
  536. async_tx_ack(&new->async_tx);
  537. prev = new;
  538. len -= period_len;
  539. if (chan->dir == DMA_MEM_TO_DEV)
  540. dma_src += period_len;
  541. else
  542. dma_dst += period_len;
  543. /* Insert the link descriptor to the LD ring */
  544. list_add_tail(&new->node, &first->tx_list);
  545. } while (len);
  546. first->async_tx.flags = flags; /* client is in control of this ack */
  547. first->async_tx.cookie = -EBUSY;
  548. /* make the cyclic link */
  549. new->desc.ddadr = first->async_tx.phys;
  550. chan->cyclic_first = first;
  551. return &first->async_tx;
  552. fail:
  553. if (first)
  554. mmp_pdma_free_desc_list(chan, &first->tx_list);
  555. return NULL;
  556. }
  557. static int mmp_pdma_control(struct dma_chan *dchan, enum dma_ctrl_cmd cmd,
  558. unsigned long arg)
  559. {
  560. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  561. struct dma_slave_config *cfg = (void *)arg;
  562. unsigned long flags;
  563. int ret = 0;
  564. u32 maxburst = 0, addr = 0;
  565. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  566. if (!dchan)
  567. return -EINVAL;
  568. switch (cmd) {
  569. case DMA_TERMINATE_ALL:
  570. disable_chan(chan->phy);
  571. mmp_pdma_free_phy(chan);
  572. spin_lock_irqsave(&chan->desc_lock, flags);
  573. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  574. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  575. spin_unlock_irqrestore(&chan->desc_lock, flags);
  576. chan->idle = true;
  577. break;
  578. case DMA_SLAVE_CONFIG:
  579. if (cfg->direction == DMA_DEV_TO_MEM) {
  580. chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
  581. maxburst = cfg->src_maxburst;
  582. width = cfg->src_addr_width;
  583. addr = cfg->src_addr;
  584. } else if (cfg->direction == DMA_MEM_TO_DEV) {
  585. chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
  586. maxburst = cfg->dst_maxburst;
  587. width = cfg->dst_addr_width;
  588. addr = cfg->dst_addr;
  589. }
  590. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  591. chan->dcmd |= DCMD_WIDTH1;
  592. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  593. chan->dcmd |= DCMD_WIDTH2;
  594. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  595. chan->dcmd |= DCMD_WIDTH4;
  596. if (maxburst == 8)
  597. chan->dcmd |= DCMD_BURST8;
  598. else if (maxburst == 16)
  599. chan->dcmd |= DCMD_BURST16;
  600. else if (maxburst == 32)
  601. chan->dcmd |= DCMD_BURST32;
  602. chan->dir = cfg->direction;
  603. chan->dev_addr = addr;
  604. /* FIXME: drivers should be ported over to use the filter
  605. * function. Once that's done, the following two lines can
  606. * be removed.
  607. */
  608. if (cfg->slave_id)
  609. chan->drcmr = cfg->slave_id;
  610. break;
  611. default:
  612. return -ENOSYS;
  613. }
  614. return ret;
  615. }
  616. static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
  617. dma_cookie_t cookie, struct dma_tx_state *txstate)
  618. {
  619. return dma_cookie_status(dchan, cookie, txstate);
  620. }
  621. /**
  622. * mmp_pdma_issue_pending - Issue the DMA start command
  623. * pending list ==> running list
  624. */
  625. static void mmp_pdma_issue_pending(struct dma_chan *dchan)
  626. {
  627. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  628. unsigned long flags;
  629. spin_lock_irqsave(&chan->desc_lock, flags);
  630. start_pending_queue(chan);
  631. spin_unlock_irqrestore(&chan->desc_lock, flags);
  632. }
  633. /*
  634. * dma_do_tasklet
  635. * Do call back
  636. * Start pending list
  637. */
  638. static void dma_do_tasklet(unsigned long data)
  639. {
  640. struct mmp_pdma_chan *chan = (struct mmp_pdma_chan *)data;
  641. struct mmp_pdma_desc_sw *desc, *_desc;
  642. LIST_HEAD(chain_cleanup);
  643. unsigned long flags;
  644. if (chan->cyclic_first) {
  645. dma_async_tx_callback cb = NULL;
  646. void *cb_data = NULL;
  647. spin_lock_irqsave(&chan->desc_lock, flags);
  648. desc = chan->cyclic_first;
  649. cb = desc->async_tx.callback;
  650. cb_data = desc->async_tx.callback_param;
  651. spin_unlock_irqrestore(&chan->desc_lock, flags);
  652. if (cb)
  653. cb(cb_data);
  654. return;
  655. }
  656. /* submit pending list; callback for each desc; free desc */
  657. spin_lock_irqsave(&chan->desc_lock, flags);
  658. list_for_each_entry_safe(desc, _desc, &chan->chain_running, node) {
  659. /*
  660. * move the descriptors to a temporary list so we can drop
  661. * the lock during the entire cleanup operation
  662. */
  663. list_del(&desc->node);
  664. list_add(&desc->node, &chain_cleanup);
  665. /*
  666. * Look for the first list entry which has the ENDIRQEN flag
  667. * set. That is the descriptor we got an interrupt for, so
  668. * complete that transaction and its cookie.
  669. */
  670. if (desc->desc.dcmd & DCMD_ENDIRQEN) {
  671. dma_cookie_t cookie = desc->async_tx.cookie;
  672. dma_cookie_complete(&desc->async_tx);
  673. dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
  674. break;
  675. }
  676. }
  677. /*
  678. * The hardware is idle and ready for more when the
  679. * chain_running list is empty.
  680. */
  681. chan->idle = list_empty(&chan->chain_running);
  682. /* Start any pending transactions automatically */
  683. start_pending_queue(chan);
  684. spin_unlock_irqrestore(&chan->desc_lock, flags);
  685. /* Run the callback for each descriptor, in order */
  686. list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
  687. struct dma_async_tx_descriptor *txd = &desc->async_tx;
  688. /* Remove from the list of transactions */
  689. list_del(&desc->node);
  690. /* Run the link descriptor callback function */
  691. if (txd->callback)
  692. txd->callback(txd->callback_param);
  693. dma_pool_free(chan->desc_pool, desc, txd->phys);
  694. }
  695. }
  696. static int mmp_pdma_remove(struct platform_device *op)
  697. {
  698. struct mmp_pdma_device *pdev = platform_get_drvdata(op);
  699. dma_async_device_unregister(&pdev->device);
  700. return 0;
  701. }
  702. static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev,
  703. int idx, int irq)
  704. {
  705. struct mmp_pdma_phy *phy = &pdev->phy[idx];
  706. struct mmp_pdma_chan *chan;
  707. int ret;
  708. chan = devm_kzalloc(pdev->dev,
  709. sizeof(struct mmp_pdma_chan), GFP_KERNEL);
  710. if (chan == NULL)
  711. return -ENOMEM;
  712. phy->idx = idx;
  713. phy->base = pdev->base;
  714. if (irq) {
  715. ret = devm_request_irq(pdev->dev, irq,
  716. mmp_pdma_chan_handler, IRQF_DISABLED, "pdma", phy);
  717. if (ret) {
  718. dev_err(pdev->dev, "channel request irq fail!\n");
  719. return ret;
  720. }
  721. }
  722. spin_lock_init(&chan->desc_lock);
  723. chan->dev = pdev->dev;
  724. chan->chan.device = &pdev->device;
  725. tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
  726. INIT_LIST_HEAD(&chan->chain_pending);
  727. INIT_LIST_HEAD(&chan->chain_running);
  728. /* register virt channel to dma engine */
  729. list_add_tail(&chan->chan.device_node,
  730. &pdev->device.channels);
  731. return 0;
  732. }
  733. static struct of_device_id mmp_pdma_dt_ids[] = {
  734. { .compatible = "marvell,pdma-1.0", },
  735. {}
  736. };
  737. MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
  738. static struct dma_chan *mmp_pdma_dma_xlate(struct of_phandle_args *dma_spec,
  739. struct of_dma *ofdma)
  740. {
  741. struct mmp_pdma_device *d = ofdma->of_dma_data;
  742. struct dma_chan *chan, *candidate;
  743. retry:
  744. candidate = NULL;
  745. /* walk the list of channels registered with the current instance and
  746. * find one that is currently unused */
  747. list_for_each_entry(chan, &d->device.channels, device_node)
  748. if (chan->client_count == 0) {
  749. candidate = chan;
  750. break;
  751. }
  752. if (!candidate)
  753. return NULL;
  754. /* dma_get_slave_channel will return NULL if we lost a race between
  755. * the lookup and the reservation */
  756. chan = dma_get_slave_channel(candidate);
  757. if (chan) {
  758. struct mmp_pdma_chan *c = to_mmp_pdma_chan(chan);
  759. c->drcmr = dma_spec->args[0];
  760. return chan;
  761. }
  762. goto retry;
  763. }
  764. static int mmp_pdma_probe(struct platform_device *op)
  765. {
  766. struct mmp_pdma_device *pdev;
  767. const struct of_device_id *of_id;
  768. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  769. struct resource *iores;
  770. int i, ret, irq = 0;
  771. int dma_channels = 0, irq_num = 0;
  772. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  773. if (!pdev)
  774. return -ENOMEM;
  775. pdev->dev = &op->dev;
  776. spin_lock_init(&pdev->phy_lock);
  777. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  778. pdev->base = devm_ioremap_resource(pdev->dev, iores);
  779. if (IS_ERR(pdev->base))
  780. return PTR_ERR(pdev->base);
  781. of_id = of_match_device(mmp_pdma_dt_ids, pdev->dev);
  782. if (of_id)
  783. of_property_read_u32(pdev->dev->of_node,
  784. "#dma-channels", &dma_channels);
  785. else if (pdata && pdata->dma_channels)
  786. dma_channels = pdata->dma_channels;
  787. else
  788. dma_channels = 32; /* default 32 channel */
  789. pdev->dma_channels = dma_channels;
  790. for (i = 0; i < dma_channels; i++) {
  791. if (platform_get_irq(op, i) > 0)
  792. irq_num++;
  793. }
  794. pdev->phy = devm_kzalloc(pdev->dev,
  795. dma_channels * sizeof(struct mmp_pdma_chan), GFP_KERNEL);
  796. if (pdev->phy == NULL)
  797. return -ENOMEM;
  798. INIT_LIST_HEAD(&pdev->device.channels);
  799. if (irq_num != dma_channels) {
  800. /* all chan share one irq, demux inside */
  801. irq = platform_get_irq(op, 0);
  802. ret = devm_request_irq(pdev->dev, irq,
  803. mmp_pdma_int_handler, IRQF_DISABLED, "pdma", pdev);
  804. if (ret)
  805. return ret;
  806. }
  807. for (i = 0; i < dma_channels; i++) {
  808. irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
  809. ret = mmp_pdma_chan_init(pdev, i, irq);
  810. if (ret)
  811. return ret;
  812. }
  813. dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
  814. dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
  815. dma_cap_set(DMA_CYCLIC, pdev->device.cap_mask);
  816. pdev->device.dev = &op->dev;
  817. pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
  818. pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
  819. pdev->device.device_tx_status = mmp_pdma_tx_status;
  820. pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
  821. pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
  822. pdev->device.device_prep_dma_cyclic = mmp_pdma_prep_dma_cyclic;
  823. pdev->device.device_issue_pending = mmp_pdma_issue_pending;
  824. pdev->device.device_control = mmp_pdma_control;
  825. pdev->device.copy_align = PDMA_ALIGNMENT;
  826. if (pdev->dev->coherent_dma_mask)
  827. dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
  828. else
  829. dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
  830. ret = dma_async_device_register(&pdev->device);
  831. if (ret) {
  832. dev_err(pdev->device.dev, "unable to register\n");
  833. return ret;
  834. }
  835. if (op->dev.of_node) {
  836. /* Device-tree DMA controller registration */
  837. ret = of_dma_controller_register(op->dev.of_node,
  838. mmp_pdma_dma_xlate, pdev);
  839. if (ret < 0) {
  840. dev_err(&op->dev, "of_dma_controller_register failed\n");
  841. return ret;
  842. }
  843. }
  844. dev_info(pdev->device.dev, "initialized %d channels\n", dma_channels);
  845. return 0;
  846. }
  847. static const struct platform_device_id mmp_pdma_id_table[] = {
  848. { "mmp-pdma", },
  849. { },
  850. };
  851. static struct platform_driver mmp_pdma_driver = {
  852. .driver = {
  853. .name = "mmp-pdma",
  854. .owner = THIS_MODULE,
  855. .of_match_table = mmp_pdma_dt_ids,
  856. },
  857. .id_table = mmp_pdma_id_table,
  858. .probe = mmp_pdma_probe,
  859. .remove = mmp_pdma_remove,
  860. };
  861. bool mmp_pdma_filter_fn(struct dma_chan *chan, void *param)
  862. {
  863. struct mmp_pdma_chan *c = to_mmp_pdma_chan(chan);
  864. if (chan->device->dev->driver != &mmp_pdma_driver.driver)
  865. return false;
  866. c->drcmr = *(unsigned int *) param;
  867. return true;
  868. }
  869. EXPORT_SYMBOL_GPL(mmp_pdma_filter_fn);
  870. module_platform_driver(mmp_pdma_driver);
  871. MODULE_DESCRIPTION("MARVELL MMP Periphera DMA Driver");
  872. MODULE_AUTHOR("Marvell International Ltd.");
  873. MODULE_LICENSE("GPL v2");