mmp_pdma.c 29 KB

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