mmp_pdma.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. dint &= (dint - 1);
  184. phy = &pdev->phy[i];
  185. ret = mmp_pdma_chan_handler(irq, phy);
  186. if (ret == IRQ_HANDLED)
  187. irq_num++;
  188. }
  189. if (irq_num)
  190. return IRQ_HANDLED;
  191. return IRQ_NONE;
  192. }
  193. /* lookup free phy channel as descending priority */
  194. static struct mmp_pdma_phy *lookup_phy(struct mmp_pdma_chan *pchan)
  195. {
  196. int prio, i;
  197. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  198. struct mmp_pdma_phy *phy, *found = NULL;
  199. unsigned long flags;
  200. /*
  201. * dma channel priorities
  202. * ch 0 - 3, 16 - 19 <--> (0)
  203. * ch 4 - 7, 20 - 23 <--> (1)
  204. * ch 8 - 11, 24 - 27 <--> (2)
  205. * ch 12 - 15, 28 - 31 <--> (3)
  206. */
  207. spin_lock_irqsave(&pdev->phy_lock, flags);
  208. for (prio = 0; prio <= ((pdev->dma_channels - 1) & 0xf) >> 2; prio++) {
  209. for (i = 0; i < pdev->dma_channels; i++) {
  210. if (prio != (i & 0xf) >> 2)
  211. continue;
  212. phy = &pdev->phy[i];
  213. if (!phy->vchan) {
  214. phy->vchan = pchan;
  215. found = phy;
  216. goto out_unlock;
  217. }
  218. }
  219. }
  220. out_unlock:
  221. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  222. return found;
  223. }
  224. static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
  225. {
  226. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  227. unsigned long flags;
  228. u32 reg;
  229. if (!pchan->phy)
  230. return;
  231. /* clear the channel mapping in DRCMR */
  232. reg = DRCMR(pchan->drcmr);
  233. writel(0, pchan->phy->base + reg);
  234. spin_lock_irqsave(&pdev->phy_lock, flags);
  235. pchan->phy->vchan = NULL;
  236. pchan->phy = NULL;
  237. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  238. }
  239. /**
  240. * start_pending_queue - transfer any pending transactions
  241. * pending list ==> running list
  242. */
  243. static void start_pending_queue(struct mmp_pdma_chan *chan)
  244. {
  245. struct mmp_pdma_desc_sw *desc;
  246. /* still in running, irq will start the pending list */
  247. if (!chan->idle) {
  248. dev_dbg(chan->dev, "DMA controller still busy\n");
  249. return;
  250. }
  251. if (list_empty(&chan->chain_pending)) {
  252. /* chance to re-fetch phy channel with higher prio */
  253. mmp_pdma_free_phy(chan);
  254. dev_dbg(chan->dev, "no pending list\n");
  255. return;
  256. }
  257. if (!chan->phy) {
  258. chan->phy = lookup_phy(chan);
  259. if (!chan->phy) {
  260. dev_dbg(chan->dev, "no free dma channel\n");
  261. return;
  262. }
  263. }
  264. /*
  265. * pending -> running
  266. * reintilize pending list
  267. */
  268. desc = list_first_entry(&chan->chain_pending,
  269. struct mmp_pdma_desc_sw, node);
  270. list_splice_tail_init(&chan->chain_pending, &chan->chain_running);
  271. /*
  272. * Program the descriptor's address into the DMA controller,
  273. * then start the DMA transaction
  274. */
  275. set_desc(chan->phy, desc->async_tx.phys);
  276. enable_chan(chan->phy);
  277. chan->idle = false;
  278. }
  279. /* desc->tx_list ==> pending list */
  280. static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx)
  281. {
  282. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan);
  283. struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx);
  284. struct mmp_pdma_desc_sw *child;
  285. unsigned long flags;
  286. dma_cookie_t cookie = -EBUSY;
  287. spin_lock_irqsave(&chan->desc_lock, flags);
  288. list_for_each_entry(child, &desc->tx_list, node) {
  289. cookie = dma_cookie_assign(&child->async_tx);
  290. }
  291. /* softly link to pending list - desc->tx_list ==> pending list */
  292. list_splice_tail_init(&desc->tx_list, &chan->chain_pending);
  293. spin_unlock_irqrestore(&chan->desc_lock, flags);
  294. return cookie;
  295. }
  296. static struct mmp_pdma_desc_sw *
  297. mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan)
  298. {
  299. struct mmp_pdma_desc_sw *desc;
  300. dma_addr_t pdesc;
  301. desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
  302. if (!desc) {
  303. dev_err(chan->dev, "out of memory for link descriptor\n");
  304. return NULL;
  305. }
  306. memset(desc, 0, sizeof(*desc));
  307. INIT_LIST_HEAD(&desc->tx_list);
  308. dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
  309. /* each desc has submit */
  310. desc->async_tx.tx_submit = mmp_pdma_tx_submit;
  311. desc->async_tx.phys = pdesc;
  312. return desc;
  313. }
  314. /**
  315. * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel.
  316. *
  317. * This function will create a dma pool for descriptor allocation.
  318. * Request irq only when channel is requested
  319. * Return - The number of allocated descriptors.
  320. */
  321. static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan)
  322. {
  323. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  324. if (chan->desc_pool)
  325. return 1;
  326. chan->desc_pool = dma_pool_create(dev_name(&dchan->dev->device),
  327. chan->dev,
  328. sizeof(struct mmp_pdma_desc_sw),
  329. __alignof__(struct mmp_pdma_desc_sw),
  330. 0);
  331. if (!chan->desc_pool) {
  332. dev_err(chan->dev, "unable to allocate descriptor pool\n");
  333. return -ENOMEM;
  334. }
  335. mmp_pdma_free_phy(chan);
  336. chan->idle = true;
  337. chan->dev_addr = 0;
  338. return 1;
  339. }
  340. static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan,
  341. struct list_head *list)
  342. {
  343. struct mmp_pdma_desc_sw *desc, *_desc;
  344. list_for_each_entry_safe(desc, _desc, list, node) {
  345. list_del(&desc->node);
  346. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  347. }
  348. }
  349. static void mmp_pdma_free_chan_resources(struct dma_chan *dchan)
  350. {
  351. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  352. unsigned long flags;
  353. spin_lock_irqsave(&chan->desc_lock, flags);
  354. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  355. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  356. spin_unlock_irqrestore(&chan->desc_lock, flags);
  357. dma_pool_destroy(chan->desc_pool);
  358. chan->desc_pool = NULL;
  359. chan->idle = true;
  360. chan->dev_addr = 0;
  361. mmp_pdma_free_phy(chan);
  362. return;
  363. }
  364. static struct dma_async_tx_descriptor *
  365. mmp_pdma_prep_memcpy(struct dma_chan *dchan,
  366. dma_addr_t dma_dst, dma_addr_t dma_src,
  367. size_t len, unsigned long flags)
  368. {
  369. struct mmp_pdma_chan *chan;
  370. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  371. size_t copy = 0;
  372. if (!dchan)
  373. return NULL;
  374. if (!len)
  375. return NULL;
  376. chan = to_mmp_pdma_chan(dchan);
  377. chan->byte_align = false;
  378. if (!chan->dir) {
  379. chan->dir = DMA_MEM_TO_MEM;
  380. chan->dcmd = DCMD_INCTRGADDR | DCMD_INCSRCADDR;
  381. chan->dcmd |= DCMD_BURST32;
  382. }
  383. do {
  384. /* Allocate the link descriptor from DMA pool */
  385. new = mmp_pdma_alloc_descriptor(chan);
  386. if (!new) {
  387. dev_err(chan->dev, "no memory for desc\n");
  388. goto fail;
  389. }
  390. copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
  391. if (dma_src & 0x7 || dma_dst & 0x7)
  392. chan->byte_align = true;
  393. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy);
  394. new->desc.dsadr = dma_src;
  395. new->desc.dtadr = dma_dst;
  396. if (!first)
  397. first = new;
  398. else
  399. prev->desc.ddadr = new->async_tx.phys;
  400. new->async_tx.cookie = 0;
  401. async_tx_ack(&new->async_tx);
  402. prev = new;
  403. len -= copy;
  404. if (chan->dir == DMA_MEM_TO_DEV) {
  405. dma_src += copy;
  406. } else if (chan->dir == DMA_DEV_TO_MEM) {
  407. dma_dst += copy;
  408. } else if (chan->dir == DMA_MEM_TO_MEM) {
  409. dma_src += copy;
  410. dma_dst += copy;
  411. }
  412. /* Insert the link descriptor to the LD ring */
  413. list_add_tail(&new->node, &first->tx_list);
  414. } while (len);
  415. first->async_tx.flags = flags; /* client is in control of this ack */
  416. first->async_tx.cookie = -EBUSY;
  417. /* last desc and fire IRQ */
  418. new->desc.ddadr = DDADR_STOP;
  419. new->desc.dcmd |= DCMD_ENDIRQEN;
  420. chan->cyclic_first = NULL;
  421. return &first->async_tx;
  422. fail:
  423. if (first)
  424. mmp_pdma_free_desc_list(chan, &first->tx_list);
  425. return NULL;
  426. }
  427. static struct dma_async_tx_descriptor *
  428. mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  429. unsigned int sg_len, enum dma_transfer_direction dir,
  430. unsigned long flags, void *context)
  431. {
  432. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  433. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  434. size_t len, avail;
  435. struct scatterlist *sg;
  436. dma_addr_t addr;
  437. int i;
  438. if ((sgl == NULL) || (sg_len == 0))
  439. return NULL;
  440. chan->byte_align = false;
  441. for_each_sg(sgl, sg, sg_len, i) {
  442. addr = sg_dma_address(sg);
  443. avail = sg_dma_len(sgl);
  444. do {
  445. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  446. if (addr & 0x7)
  447. chan->byte_align = true;
  448. /* allocate and populate the descriptor */
  449. new = mmp_pdma_alloc_descriptor(chan);
  450. if (!new) {
  451. dev_err(chan->dev, "no memory for desc\n");
  452. goto fail;
  453. }
  454. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len);
  455. if (dir == DMA_MEM_TO_DEV) {
  456. new->desc.dsadr = addr;
  457. new->desc.dtadr = chan->dev_addr;
  458. } else {
  459. new->desc.dsadr = chan->dev_addr;
  460. new->desc.dtadr = addr;
  461. }
  462. if (!first)
  463. first = new;
  464. else
  465. prev->desc.ddadr = new->async_tx.phys;
  466. new->async_tx.cookie = 0;
  467. async_tx_ack(&new->async_tx);
  468. prev = new;
  469. /* Insert the link descriptor to the LD ring */
  470. list_add_tail(&new->node, &first->tx_list);
  471. /* update metadata */
  472. addr += len;
  473. avail -= len;
  474. } while (avail);
  475. }
  476. first->async_tx.cookie = -EBUSY;
  477. first->async_tx.flags = flags;
  478. /* last desc and fire IRQ */
  479. new->desc.ddadr = DDADR_STOP;
  480. new->desc.dcmd |= DCMD_ENDIRQEN;
  481. chan->dir = dir;
  482. chan->cyclic_first = NULL;
  483. return &first->async_tx;
  484. fail:
  485. if (first)
  486. mmp_pdma_free_desc_list(chan, &first->tx_list);
  487. return NULL;
  488. }
  489. static struct dma_async_tx_descriptor *
  490. mmp_pdma_prep_dma_cyclic(struct dma_chan *dchan,
  491. dma_addr_t buf_addr, size_t len, size_t period_len,
  492. 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. u32 maxburst = 0, addr = 0;
  564. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  565. if (!dchan)
  566. return -EINVAL;
  567. switch (cmd) {
  568. case DMA_TERMINATE_ALL:
  569. disable_chan(chan->phy);
  570. mmp_pdma_free_phy(chan);
  571. spin_lock_irqsave(&chan->desc_lock, flags);
  572. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  573. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  574. spin_unlock_irqrestore(&chan->desc_lock, flags);
  575. chan->idle = true;
  576. break;
  577. case DMA_SLAVE_CONFIG:
  578. if (cfg->direction == DMA_DEV_TO_MEM) {
  579. chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
  580. maxburst = cfg->src_maxburst;
  581. width = cfg->src_addr_width;
  582. addr = cfg->src_addr;
  583. } else if (cfg->direction == DMA_MEM_TO_DEV) {
  584. chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
  585. maxburst = cfg->dst_maxburst;
  586. width = cfg->dst_addr_width;
  587. addr = cfg->dst_addr;
  588. }
  589. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  590. chan->dcmd |= DCMD_WIDTH1;
  591. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  592. chan->dcmd |= DCMD_WIDTH2;
  593. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  594. chan->dcmd |= DCMD_WIDTH4;
  595. if (maxburst == 8)
  596. chan->dcmd |= DCMD_BURST8;
  597. else if (maxburst == 16)
  598. chan->dcmd |= DCMD_BURST16;
  599. else if (maxburst == 32)
  600. chan->dcmd |= DCMD_BURST32;
  601. chan->dir = cfg->direction;
  602. chan->dev_addr = addr;
  603. /* FIXME: drivers should be ported over to use the filter
  604. * function. Once that's done, the following two lines can
  605. * be removed.
  606. */
  607. if (cfg->slave_id)
  608. chan->drcmr = cfg->slave_id;
  609. break;
  610. default:
  611. return -ENOSYS;
  612. }
  613. return 0;
  614. }
  615. static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
  616. dma_cookie_t cookie)
  617. {
  618. struct mmp_pdma_desc_sw *sw;
  619. u32 curr, residue = 0;
  620. bool passed = false;
  621. bool cyclic = chan->cyclic_first != NULL;
  622. /*
  623. * If the channel does not have a phy pointer anymore, it has already
  624. * been completed. Therefore, its residue is 0.
  625. */
  626. if (!chan->phy)
  627. return 0;
  628. if (chan->dir == DMA_DEV_TO_MEM)
  629. curr = readl(chan->phy->base + DTADR(chan->phy->idx));
  630. else
  631. curr = readl(chan->phy->base + DSADR(chan->phy->idx));
  632. list_for_each_entry(sw, &chan->chain_running, node) {
  633. u32 start, end, len;
  634. if (chan->dir == DMA_DEV_TO_MEM)
  635. start = sw->desc.dtadr;
  636. else
  637. start = sw->desc.dsadr;
  638. len = sw->desc.dcmd & DCMD_LENGTH;
  639. end = start + len;
  640. /*
  641. * 'passed' will be latched once we found the descriptor which
  642. * lies inside the boundaries of the curr pointer. All
  643. * descriptors that occur in the list _after_ we found that
  644. * partially handled descriptor are still to be processed and
  645. * are hence added to the residual bytes counter.
  646. */
  647. if (passed) {
  648. residue += len;
  649. } else if (curr >= start && curr <= end) {
  650. residue += end - curr;
  651. passed = true;
  652. }
  653. /*
  654. * Descriptors that have the ENDIRQEN bit set mark the end of a
  655. * transaction chain, and the cookie assigned with it has been
  656. * returned previously from mmp_pdma_tx_submit().
  657. *
  658. * In case we have multiple transactions in the running chain,
  659. * and the cookie does not match the one the user asked us
  660. * about, reset the state variables and start over.
  661. *
  662. * This logic does not apply to cyclic transactions, where all
  663. * descriptors have the ENDIRQEN bit set, and for which we
  664. * can't have multiple transactions on one channel anyway.
  665. */
  666. if (cyclic || !(sw->desc.dcmd & DCMD_ENDIRQEN))
  667. continue;
  668. if (sw->async_tx.cookie == cookie) {
  669. return residue;
  670. } else {
  671. residue = 0;
  672. passed = false;
  673. }
  674. }
  675. /* We should only get here in case of cyclic transactions */
  676. return residue;
  677. }
  678. static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
  679. dma_cookie_t cookie,
  680. struct dma_tx_state *txstate)
  681. {
  682. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  683. enum dma_status ret;
  684. ret = dma_cookie_status(dchan, cookie, txstate);
  685. if (likely(ret != DMA_ERROR))
  686. dma_set_residue(txstate, mmp_pdma_residue(chan, cookie));
  687. return ret;
  688. }
  689. /**
  690. * mmp_pdma_issue_pending - Issue the DMA start command
  691. * pending list ==> running list
  692. */
  693. static void mmp_pdma_issue_pending(struct dma_chan *dchan)
  694. {
  695. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  696. unsigned long flags;
  697. spin_lock_irqsave(&chan->desc_lock, flags);
  698. start_pending_queue(chan);
  699. spin_unlock_irqrestore(&chan->desc_lock, flags);
  700. }
  701. /*
  702. * dma_do_tasklet
  703. * Do call back
  704. * Start pending list
  705. */
  706. static void dma_do_tasklet(unsigned long data)
  707. {
  708. struct mmp_pdma_chan *chan = (struct mmp_pdma_chan *)data;
  709. struct mmp_pdma_desc_sw *desc, *_desc;
  710. LIST_HEAD(chain_cleanup);
  711. unsigned long flags;
  712. if (chan->cyclic_first) {
  713. dma_async_tx_callback cb = NULL;
  714. void *cb_data = NULL;
  715. spin_lock_irqsave(&chan->desc_lock, flags);
  716. desc = chan->cyclic_first;
  717. cb = desc->async_tx.callback;
  718. cb_data = desc->async_tx.callback_param;
  719. spin_unlock_irqrestore(&chan->desc_lock, flags);
  720. if (cb)
  721. cb(cb_data);
  722. return;
  723. }
  724. /* submit pending list; callback for each desc; free desc */
  725. spin_lock_irqsave(&chan->desc_lock, flags);
  726. list_for_each_entry_safe(desc, _desc, &chan->chain_running, node) {
  727. /*
  728. * move the descriptors to a temporary list so we can drop
  729. * the lock during the entire cleanup operation
  730. */
  731. list_move(&desc->node, &chain_cleanup);
  732. /*
  733. * Look for the first list entry which has the ENDIRQEN flag
  734. * set. That is the descriptor we got an interrupt for, so
  735. * complete that transaction and its cookie.
  736. */
  737. if (desc->desc.dcmd & DCMD_ENDIRQEN) {
  738. dma_cookie_t cookie = desc->async_tx.cookie;
  739. dma_cookie_complete(&desc->async_tx);
  740. dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
  741. break;
  742. }
  743. }
  744. /*
  745. * The hardware is idle and ready for more when the
  746. * chain_running list is empty.
  747. */
  748. chan->idle = list_empty(&chan->chain_running);
  749. /* Start any pending transactions automatically */
  750. start_pending_queue(chan);
  751. spin_unlock_irqrestore(&chan->desc_lock, flags);
  752. /* Run the callback for each descriptor, in order */
  753. list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
  754. struct dma_async_tx_descriptor *txd = &desc->async_tx;
  755. /* Remove from the list of transactions */
  756. list_del(&desc->node);
  757. /* Run the link descriptor callback function */
  758. if (txd->callback)
  759. txd->callback(txd->callback_param);
  760. dma_pool_free(chan->desc_pool, desc, txd->phys);
  761. }
  762. }
  763. static int mmp_pdma_remove(struct platform_device *op)
  764. {
  765. struct mmp_pdma_device *pdev = platform_get_drvdata(op);
  766. dma_async_device_unregister(&pdev->device);
  767. return 0;
  768. }
  769. static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev, int idx, int irq)
  770. {
  771. struct mmp_pdma_phy *phy = &pdev->phy[idx];
  772. struct mmp_pdma_chan *chan;
  773. int ret;
  774. chan = devm_kzalloc(pdev->dev, sizeof(*chan), GFP_KERNEL);
  775. if (chan == NULL)
  776. return -ENOMEM;
  777. phy->idx = idx;
  778. phy->base = pdev->base;
  779. if (irq) {
  780. ret = devm_request_irq(pdev->dev, irq, mmp_pdma_chan_handler,
  781. IRQF_SHARED, "pdma", phy);
  782. if (ret) {
  783. dev_err(pdev->dev, "channel request irq fail!\n");
  784. return ret;
  785. }
  786. }
  787. spin_lock_init(&chan->desc_lock);
  788. chan->dev = pdev->dev;
  789. chan->chan.device = &pdev->device;
  790. tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
  791. INIT_LIST_HEAD(&chan->chain_pending);
  792. INIT_LIST_HEAD(&chan->chain_running);
  793. /* register virt channel to dma engine */
  794. list_add_tail(&chan->chan.device_node, &pdev->device.channels);
  795. return 0;
  796. }
  797. static struct of_device_id mmp_pdma_dt_ids[] = {
  798. { .compatible = "marvell,pdma-1.0", },
  799. {}
  800. };
  801. MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
  802. static struct dma_chan *mmp_pdma_dma_xlate(struct of_phandle_args *dma_spec,
  803. struct of_dma *ofdma)
  804. {
  805. struct mmp_pdma_device *d = ofdma->of_dma_data;
  806. struct dma_chan *chan;
  807. chan = dma_get_any_slave_channel(&d->device);
  808. if (!chan)
  809. return NULL;
  810. to_mmp_pdma_chan(chan)->drcmr = dma_spec->args[0];
  811. return chan;
  812. }
  813. static int mmp_pdma_probe(struct platform_device *op)
  814. {
  815. struct mmp_pdma_device *pdev;
  816. const struct of_device_id *of_id;
  817. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  818. struct resource *iores;
  819. int i, ret, irq = 0;
  820. int dma_channels = 0, irq_num = 0;
  821. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  822. if (!pdev)
  823. return -ENOMEM;
  824. pdev->dev = &op->dev;
  825. spin_lock_init(&pdev->phy_lock);
  826. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  827. pdev->base = devm_ioremap_resource(pdev->dev, iores);
  828. if (IS_ERR(pdev->base))
  829. return PTR_ERR(pdev->base);
  830. of_id = of_match_device(mmp_pdma_dt_ids, pdev->dev);
  831. if (of_id)
  832. of_property_read_u32(pdev->dev->of_node, "#dma-channels",
  833. &dma_channels);
  834. else if (pdata && pdata->dma_channels)
  835. dma_channels = pdata->dma_channels;
  836. else
  837. dma_channels = 32; /* default 32 channel */
  838. pdev->dma_channels = dma_channels;
  839. for (i = 0; i < dma_channels; i++) {
  840. if (platform_get_irq(op, i) > 0)
  841. irq_num++;
  842. }
  843. pdev->phy = devm_kcalloc(pdev->dev, dma_channels, sizeof(*pdev->phy),
  844. GFP_KERNEL);
  845. if (pdev->phy == NULL)
  846. return -ENOMEM;
  847. INIT_LIST_HEAD(&pdev->device.channels);
  848. if (irq_num != dma_channels) {
  849. /* all chan share one irq, demux inside */
  850. irq = platform_get_irq(op, 0);
  851. ret = devm_request_irq(pdev->dev, irq, mmp_pdma_int_handler,
  852. IRQF_SHARED, "pdma", pdev);
  853. if (ret)
  854. return ret;
  855. }
  856. for (i = 0; i < dma_channels; i++) {
  857. irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
  858. ret = mmp_pdma_chan_init(pdev, i, irq);
  859. if (ret)
  860. return ret;
  861. }
  862. dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
  863. dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
  864. dma_cap_set(DMA_CYCLIC, pdev->device.cap_mask);
  865. dma_cap_set(DMA_PRIVATE, pdev->device.cap_mask);
  866. pdev->device.dev = &op->dev;
  867. pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
  868. pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
  869. pdev->device.device_tx_status = mmp_pdma_tx_status;
  870. pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
  871. pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
  872. pdev->device.device_prep_dma_cyclic = mmp_pdma_prep_dma_cyclic;
  873. pdev->device.device_issue_pending = mmp_pdma_issue_pending;
  874. pdev->device.device_control = mmp_pdma_control;
  875. pdev->device.copy_align = PDMA_ALIGNMENT;
  876. if (pdev->dev->coherent_dma_mask)
  877. dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
  878. else
  879. dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
  880. ret = dma_async_device_register(&pdev->device);
  881. if (ret) {
  882. dev_err(pdev->device.dev, "unable to register\n");
  883. return ret;
  884. }
  885. if (op->dev.of_node) {
  886. /* Device-tree DMA controller registration */
  887. ret = of_dma_controller_register(op->dev.of_node,
  888. mmp_pdma_dma_xlate, pdev);
  889. if (ret < 0) {
  890. dev_err(&op->dev, "of_dma_controller_register failed\n");
  891. return ret;
  892. }
  893. }
  894. platform_set_drvdata(op, pdev);
  895. dev_info(pdev->device.dev, "initialized %d channels\n", dma_channels);
  896. return 0;
  897. }
  898. static const struct platform_device_id mmp_pdma_id_table[] = {
  899. { "mmp-pdma", },
  900. { },
  901. };
  902. static struct platform_driver mmp_pdma_driver = {
  903. .driver = {
  904. .name = "mmp-pdma",
  905. .owner = THIS_MODULE,
  906. .of_match_table = mmp_pdma_dt_ids,
  907. },
  908. .id_table = mmp_pdma_id_table,
  909. .probe = mmp_pdma_probe,
  910. .remove = mmp_pdma_remove,
  911. };
  912. bool mmp_pdma_filter_fn(struct dma_chan *chan, void *param)
  913. {
  914. struct mmp_pdma_chan *c = to_mmp_pdma_chan(chan);
  915. if (chan->device->dev->driver != &mmp_pdma_driver.driver)
  916. return false;
  917. c->drcmr = *(unsigned int *)param;
  918. return true;
  919. }
  920. EXPORT_SYMBOL_GPL(mmp_pdma_filter_fn);
  921. module_platform_driver(mmp_pdma_driver);
  922. MODULE_DESCRIPTION("MARVELL MMP Peripheral DMA Driver");
  923. MODULE_AUTHOR("Marvell International Ltd.");
  924. MODULE_LICENSE("GPL v2");