mmp_pdma.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2012 Marvell International Ltd.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/types.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/slab.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/device.h>
  15. #include <linux/platform_data/mmp_dma.h>
  16. #include <linux/dmapool.h>
  17. #include <linux/of_dma.h>
  18. #include <linux/of.h>
  19. #include "dmaengine.h"
  20. #define DCSR 0x0000
  21. #define DALGN 0x00a0
  22. #define DINT 0x00f0
  23. #define DDADR 0x0200
  24. #define DSADR(n) (0x0204 + ((n) << 4))
  25. #define DTADR(n) (0x0208 + ((n) << 4))
  26. #define DCMD 0x020c
  27. #define DCSR_RUN BIT(31) /* Run Bit (read / write) */
  28. #define DCSR_NODESC BIT(30) /* No-Descriptor Fetch (read / write) */
  29. #define DCSR_STOPIRQEN BIT(29) /* Stop Interrupt Enable (read / write) */
  30. #define DCSR_REQPEND BIT(8) /* Request Pending (read-only) */
  31. #define DCSR_STOPSTATE BIT(3) /* Stop State (read-only) */
  32. #define DCSR_ENDINTR BIT(2) /* End Interrupt (read / write) */
  33. #define DCSR_STARTINTR BIT(1) /* Start Interrupt (read / write) */
  34. #define DCSR_BUSERR BIT(0) /* Bus Error Interrupt (read / write) */
  35. #define DCSR_EORIRQEN BIT(28) /* End of Receive Interrupt Enable (R/W) */
  36. #define DCSR_EORJMPEN BIT(27) /* Jump to next descriptor on EOR */
  37. #define DCSR_EORSTOPEN BIT(26) /* STOP on an EOR */
  38. #define DCSR_SETCMPST BIT(25) /* Set Descriptor Compare Status */
  39. #define DCSR_CLRCMPST BIT(24) /* Clear Descriptor Compare Status */
  40. #define DCSR_CMPST BIT(10) /* The Descriptor Compare Status */
  41. #define DCSR_EORINTR BIT(9) /* The end of Receive */
  42. #define DRCMR(n) ((((n) < 64) ? 0x0100 : 0x1100) + (((n) & 0x3f) << 2))
  43. #define DRCMR_MAPVLD BIT(7) /* Map Valid (read / write) */
  44. #define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */
  45. #define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */
  46. #define DDADR_STOP BIT(0) /* Stop (read / write) */
  47. #define DCMD_INCSRCADDR BIT(31) /* Source Address Increment Setting. */
  48. #define DCMD_INCTRGADDR BIT(30) /* Target Address Increment Setting. */
  49. #define DCMD_FLOWSRC BIT(29) /* Flow Control by the source. */
  50. #define DCMD_FLOWTRG BIT(28) /* Flow Control by the target. */
  51. #define DCMD_STARTIRQEN BIT(22) /* Start Interrupt Enable */
  52. #define DCMD_ENDIRQEN BIT(21) /* End Interrupt Enable */
  53. #define DCMD_ENDIAN BIT(18) /* Device Endian-ness. */
  54. #define DCMD_BURST8 (1 << 16) /* 8 byte burst */
  55. #define DCMD_BURST16 (2 << 16) /* 16 byte burst */
  56. #define DCMD_BURST32 (3 << 16) /* 32 byte burst */
  57. #define DCMD_WIDTH1 (1 << 14) /* 1 byte width */
  58. #define DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */
  59. #define DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */
  60. #define DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */
  61. #define PDMA_MAX_DESC_BYTES DCMD_LENGTH
  62. struct mmp_pdma_desc_hw {
  63. u32 ddadr; /* Points to the next descriptor + flags */
  64. u32 dsadr; /* DSADR value for the current transfer */
  65. u32 dtadr; /* DTADR value for the current transfer */
  66. u32 dcmd; /* DCMD value for the current transfer */
  67. } __aligned(32);
  68. struct mmp_pdma_desc_sw {
  69. struct mmp_pdma_desc_hw desc;
  70. struct list_head node;
  71. struct list_head tx_list;
  72. struct dma_async_tx_descriptor async_tx;
  73. };
  74. struct mmp_pdma_phy;
  75. struct mmp_pdma_chan {
  76. struct device *dev;
  77. struct dma_chan chan;
  78. struct dma_async_tx_descriptor desc;
  79. struct mmp_pdma_phy *phy;
  80. enum dma_transfer_direction dir;
  81. struct dma_slave_config slave_config;
  82. struct mmp_pdma_desc_sw *cyclic_first; /* first desc_sw if channel
  83. * is in cyclic mode */
  84. /* channel's basic info */
  85. struct tasklet_struct tasklet;
  86. u32 dcmd;
  87. u32 drcmr;
  88. u32 dev_addr;
  89. /* list for desc */
  90. spinlock_t desc_lock; /* Descriptor list lock */
  91. struct list_head chain_pending; /* Link descriptors queue for pending */
  92. struct list_head chain_running; /* Link descriptors queue for running */
  93. bool idle; /* channel statue machine */
  94. bool byte_align;
  95. struct dma_pool *desc_pool; /* Descriptors pool */
  96. };
  97. struct mmp_pdma_phy {
  98. int idx;
  99. void __iomem *base;
  100. struct mmp_pdma_chan *vchan;
  101. };
  102. struct mmp_pdma_device {
  103. int dma_channels;
  104. void __iomem *base;
  105. struct device *dev;
  106. struct dma_device device;
  107. struct mmp_pdma_phy *phy;
  108. spinlock_t phy_lock; /* protect alloc/free phy channels */
  109. };
  110. #define tx_to_mmp_pdma_desc(tx) \
  111. container_of(tx, struct mmp_pdma_desc_sw, async_tx)
  112. #define to_mmp_pdma_desc(lh) \
  113. container_of(lh, struct mmp_pdma_desc_sw, node)
  114. #define to_mmp_pdma_chan(dchan) \
  115. container_of(dchan, struct mmp_pdma_chan, chan)
  116. #define to_mmp_pdma_dev(dmadev) \
  117. container_of(dmadev, struct mmp_pdma_device, device)
  118. static int mmp_pdma_config_write(struct dma_chan *dchan,
  119. struct dma_slave_config *cfg,
  120. enum dma_transfer_direction direction);
  121. static void set_desc(struct mmp_pdma_phy *phy, dma_addr_t addr)
  122. {
  123. u32 reg = (phy->idx << 4) + DDADR;
  124. writel(addr, phy->base + reg);
  125. }
  126. static void enable_chan(struct mmp_pdma_phy *phy)
  127. {
  128. u32 reg, dalgn;
  129. if (!phy->vchan)
  130. return;
  131. reg = DRCMR(phy->vchan->drcmr);
  132. writel(DRCMR_MAPVLD | phy->idx, phy->base + reg);
  133. dalgn = readl(phy->base + DALGN);
  134. if (phy->vchan->byte_align)
  135. dalgn |= 1 << phy->idx;
  136. else
  137. dalgn &= ~(1 << phy->idx);
  138. writel(dalgn, phy->base + DALGN);
  139. reg = (phy->idx << 2) + DCSR;
  140. writel(readl(phy->base + reg) | DCSR_RUN, phy->base + reg);
  141. }
  142. static void disable_chan(struct mmp_pdma_phy *phy)
  143. {
  144. u32 reg;
  145. if (!phy)
  146. return;
  147. reg = (phy->idx << 2) + DCSR;
  148. writel(readl(phy->base + reg) & ~DCSR_RUN, phy->base + reg);
  149. }
  150. static int clear_chan_irq(struct mmp_pdma_phy *phy)
  151. {
  152. u32 dcsr;
  153. u32 dint = readl(phy->base + DINT);
  154. u32 reg = (phy->idx << 2) + DCSR;
  155. if (!(dint & BIT(phy->idx)))
  156. return -EAGAIN;
  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. static irqreturn_t mmp_pdma_chan_handler(int irq, void *dev_id)
  165. {
  166. struct mmp_pdma_phy *phy = dev_id;
  167. if (clear_chan_irq(phy) != 0)
  168. return IRQ_NONE;
  169. tasklet_schedule(&phy->vchan->tasklet);
  170. return IRQ_HANDLED;
  171. }
  172. static irqreturn_t mmp_pdma_int_handler(int irq, void *dev_id)
  173. {
  174. struct mmp_pdma_device *pdev = dev_id;
  175. struct mmp_pdma_phy *phy;
  176. u32 dint = readl(pdev->base + DINT);
  177. int i, ret;
  178. int irq_num = 0;
  179. while (dint) {
  180. i = __ffs(dint);
  181. /* only handle interrupts belonging to pdma driver*/
  182. if (i >= pdev->dma_channels)
  183. break;
  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. return IRQ_NONE;
  193. }
  194. /* lookup free phy channel as descending priority */
  195. static struct mmp_pdma_phy *lookup_phy(struct mmp_pdma_chan *pchan)
  196. {
  197. int prio, i;
  198. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  199. struct mmp_pdma_phy *phy, *found = NULL;
  200. unsigned long flags;
  201. /*
  202. * dma channel priorities
  203. * ch 0 - 3, 16 - 19 <--> (0)
  204. * ch 4 - 7, 20 - 23 <--> (1)
  205. * ch 8 - 11, 24 - 27 <--> (2)
  206. * ch 12 - 15, 28 - 31 <--> (3)
  207. */
  208. spin_lock_irqsave(&pdev->phy_lock, flags);
  209. for (prio = 0; prio <= ((pdev->dma_channels - 1) & 0xf) >> 2; prio++) {
  210. for (i = 0; i < pdev->dma_channels; i++) {
  211. if (prio != (i & 0xf) >> 2)
  212. continue;
  213. phy = &pdev->phy[i];
  214. if (!phy->vchan) {
  215. phy->vchan = pchan;
  216. found = phy;
  217. goto out_unlock;
  218. }
  219. }
  220. }
  221. out_unlock:
  222. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  223. return found;
  224. }
  225. static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
  226. {
  227. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  228. unsigned long flags;
  229. u32 reg;
  230. if (!pchan->phy)
  231. return;
  232. /* clear the channel mapping in DRCMR */
  233. reg = DRCMR(pchan->drcmr);
  234. writel(0, pchan->phy->base + reg);
  235. spin_lock_irqsave(&pdev->phy_lock, flags);
  236. pchan->phy->vchan = NULL;
  237. pchan->phy = NULL;
  238. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  239. }
  240. /*
  241. * start_pending_queue - transfer any pending transactions
  242. * pending list ==> running list
  243. */
  244. static void start_pending_queue(struct mmp_pdma_chan *chan)
  245. {
  246. struct mmp_pdma_desc_sw *desc;
  247. /* still in running, irq will start the pending list */
  248. if (!chan->idle) {
  249. dev_dbg(chan->dev, "DMA controller still busy\n");
  250. return;
  251. }
  252. if (list_empty(&chan->chain_pending)) {
  253. /* chance to re-fetch phy channel with higher prio */
  254. mmp_pdma_free_phy(chan);
  255. dev_dbg(chan->dev, "no pending list\n");
  256. return;
  257. }
  258. if (!chan->phy) {
  259. chan->phy = lookup_phy(chan);
  260. if (!chan->phy) {
  261. dev_dbg(chan->dev, "no free dma channel\n");
  262. return;
  263. }
  264. }
  265. /*
  266. * pending -> running
  267. * reintilize pending list
  268. */
  269. desc = list_first_entry(&chan->chain_pending,
  270. struct mmp_pdma_desc_sw, node);
  271. list_splice_tail_init(&chan->chain_pending, &chan->chain_running);
  272. /*
  273. * Program the descriptor's address into the DMA controller,
  274. * then start the DMA transaction
  275. */
  276. set_desc(chan->phy, desc->async_tx.phys);
  277. enable_chan(chan->phy);
  278. chan->idle = false;
  279. }
  280. /* desc->tx_list ==> pending list */
  281. static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx)
  282. {
  283. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan);
  284. struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx);
  285. struct mmp_pdma_desc_sw *child;
  286. unsigned long flags;
  287. dma_cookie_t cookie = -EBUSY;
  288. spin_lock_irqsave(&chan->desc_lock, flags);
  289. list_for_each_entry(child, &desc->tx_list, node) {
  290. cookie = dma_cookie_assign(&child->async_tx);
  291. }
  292. /* softly link to pending list - desc->tx_list ==> pending list */
  293. list_splice_tail_init(&desc->tx_list, &chan->chain_pending);
  294. spin_unlock_irqrestore(&chan->desc_lock, flags);
  295. return cookie;
  296. }
  297. static struct mmp_pdma_desc_sw *
  298. mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan)
  299. {
  300. struct mmp_pdma_desc_sw *desc;
  301. dma_addr_t pdesc;
  302. desc = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
  303. if (!desc) {
  304. dev_err(chan->dev, "out of memory for link descriptor\n");
  305. return NULL;
  306. }
  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. mmp_pdma_config_write(dchan, &chan->slave_config, dir);
  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 *
  491. mmp_pdma_prep_dma_cyclic(struct dma_chan *dchan,
  492. dma_addr_t buf_addr, size_t len, size_t period_len,
  493. enum dma_transfer_direction direction,
  494. unsigned long flags)
  495. {
  496. struct mmp_pdma_chan *chan;
  497. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  498. dma_addr_t dma_src, dma_dst;
  499. if (!dchan || !len || !period_len)
  500. return NULL;
  501. /* the buffer length must be a multiple of period_len */
  502. if (len % period_len != 0)
  503. return NULL;
  504. if (period_len > PDMA_MAX_DESC_BYTES)
  505. return NULL;
  506. chan = to_mmp_pdma_chan(dchan);
  507. mmp_pdma_config_write(dchan, &chan->slave_config, direction);
  508. switch (direction) {
  509. case DMA_MEM_TO_DEV:
  510. dma_src = buf_addr;
  511. dma_dst = chan->dev_addr;
  512. break;
  513. case DMA_DEV_TO_MEM:
  514. dma_dst = buf_addr;
  515. dma_src = chan->dev_addr;
  516. break;
  517. default:
  518. dev_err(chan->dev, "Unsupported direction for cyclic DMA\n");
  519. return NULL;
  520. }
  521. chan->dir = direction;
  522. do {
  523. /* Allocate the link descriptor from DMA pool */
  524. new = mmp_pdma_alloc_descriptor(chan);
  525. if (!new) {
  526. dev_err(chan->dev, "no memory for desc\n");
  527. goto fail;
  528. }
  529. new->desc.dcmd = (chan->dcmd | DCMD_ENDIRQEN |
  530. (DCMD_LENGTH & period_len));
  531. new->desc.dsadr = dma_src;
  532. new->desc.dtadr = dma_dst;
  533. if (!first)
  534. first = new;
  535. else
  536. prev->desc.ddadr = new->async_tx.phys;
  537. new->async_tx.cookie = 0;
  538. async_tx_ack(&new->async_tx);
  539. prev = new;
  540. len -= period_len;
  541. if (chan->dir == DMA_MEM_TO_DEV)
  542. dma_src += period_len;
  543. else
  544. dma_dst += period_len;
  545. /* Insert the link descriptor to the LD ring */
  546. list_add_tail(&new->node, &first->tx_list);
  547. } while (len);
  548. first->async_tx.flags = flags; /* client is in control of this ack */
  549. first->async_tx.cookie = -EBUSY;
  550. /* make the cyclic link */
  551. new->desc.ddadr = first->async_tx.phys;
  552. chan->cyclic_first = first;
  553. return &first->async_tx;
  554. fail:
  555. if (first)
  556. mmp_pdma_free_desc_list(chan, &first->tx_list);
  557. return NULL;
  558. }
  559. static int mmp_pdma_config_write(struct dma_chan *dchan,
  560. struct dma_slave_config *cfg,
  561. enum dma_transfer_direction direction)
  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 (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 (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 = direction;
  592. chan->dev_addr = addr;
  593. return 0;
  594. }
  595. static int mmp_pdma_config(struct dma_chan *dchan,
  596. struct dma_slave_config *cfg)
  597. {
  598. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  599. memcpy(&chan->slave_config, cfg, sizeof(*cfg));
  600. return 0;
  601. }
  602. static int mmp_pdma_terminate_all(struct dma_chan *dchan)
  603. {
  604. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  605. unsigned long flags;
  606. if (!dchan)
  607. return -EINVAL;
  608. disable_chan(chan->phy);
  609. mmp_pdma_free_phy(chan);
  610. spin_lock_irqsave(&chan->desc_lock, flags);
  611. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  612. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  613. spin_unlock_irqrestore(&chan->desc_lock, flags);
  614. chan->idle = true;
  615. return 0;
  616. }
  617. static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
  618. dma_cookie_t cookie)
  619. {
  620. struct mmp_pdma_desc_sw *sw;
  621. u32 curr, residue = 0;
  622. bool passed = false;
  623. bool cyclic = chan->cyclic_first != NULL;
  624. /*
  625. * If the channel does not have a phy pointer anymore, it has already
  626. * been completed. Therefore, its residue is 0.
  627. */
  628. if (!chan->phy)
  629. return 0;
  630. if (chan->dir == DMA_DEV_TO_MEM)
  631. curr = readl(chan->phy->base + DTADR(chan->phy->idx));
  632. else
  633. curr = readl(chan->phy->base + DSADR(chan->phy->idx));
  634. list_for_each_entry(sw, &chan->chain_running, node) {
  635. u32 start, end, len;
  636. if (chan->dir == DMA_DEV_TO_MEM)
  637. start = sw->desc.dtadr;
  638. else
  639. start = sw->desc.dsadr;
  640. len = sw->desc.dcmd & DCMD_LENGTH;
  641. end = start + len;
  642. /*
  643. * 'passed' will be latched once we found the descriptor which
  644. * lies inside the boundaries of the curr pointer. All
  645. * descriptors that occur in the list _after_ we found that
  646. * partially handled descriptor are still to be processed and
  647. * are hence added to the residual bytes counter.
  648. */
  649. if (passed) {
  650. residue += len;
  651. } else if (curr >= start && curr <= end) {
  652. residue += end - curr;
  653. passed = true;
  654. }
  655. /*
  656. * Descriptors that have the ENDIRQEN bit set mark the end of a
  657. * transaction chain, and the cookie assigned with it has been
  658. * returned previously from mmp_pdma_tx_submit().
  659. *
  660. * In case we have multiple transactions in the running chain,
  661. * and the cookie does not match the one the user asked us
  662. * about, reset the state variables and start over.
  663. *
  664. * This logic does not apply to cyclic transactions, where all
  665. * descriptors have the ENDIRQEN bit set, and for which we
  666. * can't have multiple transactions on one channel anyway.
  667. */
  668. if (cyclic || !(sw->desc.dcmd & DCMD_ENDIRQEN))
  669. continue;
  670. if (sw->async_tx.cookie == cookie) {
  671. return residue;
  672. } else {
  673. residue = 0;
  674. passed = false;
  675. }
  676. }
  677. /* We should only get here in case of cyclic transactions */
  678. return residue;
  679. }
  680. static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
  681. dma_cookie_t cookie,
  682. struct dma_tx_state *txstate)
  683. {
  684. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  685. enum dma_status ret;
  686. ret = dma_cookie_status(dchan, cookie, txstate);
  687. if (likely(ret != DMA_ERROR))
  688. dma_set_residue(txstate, mmp_pdma_residue(chan, cookie));
  689. return ret;
  690. }
  691. /*
  692. * mmp_pdma_issue_pending - Issue the DMA start command
  693. * pending list ==> running list
  694. */
  695. static void mmp_pdma_issue_pending(struct dma_chan *dchan)
  696. {
  697. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  698. unsigned long flags;
  699. spin_lock_irqsave(&chan->desc_lock, flags);
  700. start_pending_queue(chan);
  701. spin_unlock_irqrestore(&chan->desc_lock, flags);
  702. }
  703. /*
  704. * dma_do_tasklet
  705. * Do call back
  706. * Start pending list
  707. */
  708. static void dma_do_tasklet(struct tasklet_struct *t)
  709. {
  710. struct mmp_pdma_chan *chan = from_tasklet(chan, t, tasklet);
  711. struct mmp_pdma_desc_sw *desc, *_desc;
  712. LIST_HEAD(chain_cleanup);
  713. unsigned long flags;
  714. struct dmaengine_desc_callback cb;
  715. if (chan->cyclic_first) {
  716. spin_lock_irqsave(&chan->desc_lock, flags);
  717. desc = chan->cyclic_first;
  718. dmaengine_desc_get_callback(&desc->async_tx, &cb);
  719. spin_unlock_irqrestore(&chan->desc_lock, flags);
  720. dmaengine_desc_callback_invoke(&cb, NULL);
  721. return;
  722. }
  723. /* submit pending list; callback for each desc; free desc */
  724. spin_lock_irqsave(&chan->desc_lock, flags);
  725. list_for_each_entry_safe(desc, _desc, &chan->chain_running, node) {
  726. /*
  727. * move the descriptors to a temporary list so we can drop
  728. * the lock during the entire cleanup operation
  729. */
  730. list_move(&desc->node, &chain_cleanup);
  731. /*
  732. * Look for the first list entry which has the ENDIRQEN flag
  733. * set. That is the descriptor we got an interrupt for, so
  734. * complete that transaction and its cookie.
  735. */
  736. if (desc->desc.dcmd & DCMD_ENDIRQEN) {
  737. dma_cookie_t cookie = desc->async_tx.cookie;
  738. dma_cookie_complete(&desc->async_tx);
  739. dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
  740. break;
  741. }
  742. }
  743. /*
  744. * The hardware is idle and ready for more when the
  745. * chain_running list is empty.
  746. */
  747. chan->idle = list_empty(&chan->chain_running);
  748. /* Start any pending transactions automatically */
  749. start_pending_queue(chan);
  750. spin_unlock_irqrestore(&chan->desc_lock, flags);
  751. /* Run the callback for each descriptor, in order */
  752. list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
  753. struct dma_async_tx_descriptor *txd = &desc->async_tx;
  754. /* Remove from the list of transactions */
  755. list_del(&desc->node);
  756. /* Run the link descriptor callback function */
  757. dmaengine_desc_get_callback(txd, &cb);
  758. dmaengine_desc_callback_invoke(&cb, NULL);
  759. dma_pool_free(chan->desc_pool, desc, txd->phys);
  760. }
  761. }
  762. static void mmp_pdma_remove(struct platform_device *op)
  763. {
  764. struct mmp_pdma_device *pdev = platform_get_drvdata(op);
  765. struct mmp_pdma_phy *phy;
  766. int i, irq = 0, irq_num = 0;
  767. if (op->dev.of_node)
  768. of_dma_controller_free(op->dev.of_node);
  769. for (i = 0; i < pdev->dma_channels; i++) {
  770. if (platform_get_irq(op, i) > 0)
  771. irq_num++;
  772. }
  773. if (irq_num != pdev->dma_channels) {
  774. irq = platform_get_irq(op, 0);
  775. devm_free_irq(&op->dev, irq, pdev);
  776. } else {
  777. for (i = 0; i < pdev->dma_channels; i++) {
  778. phy = &pdev->phy[i];
  779. irq = platform_get_irq(op, i);
  780. devm_free_irq(&op->dev, irq, phy);
  781. }
  782. }
  783. dma_async_device_unregister(&pdev->device);
  784. }
  785. static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev, int idx, int irq)
  786. {
  787. struct mmp_pdma_phy *phy = &pdev->phy[idx];
  788. struct mmp_pdma_chan *chan;
  789. int ret;
  790. chan = devm_kzalloc(pdev->dev, sizeof(*chan), GFP_KERNEL);
  791. if (chan == NULL)
  792. return -ENOMEM;
  793. phy->idx = idx;
  794. phy->base = pdev->base;
  795. if (irq) {
  796. ret = devm_request_irq(pdev->dev, irq, mmp_pdma_chan_handler,
  797. IRQF_SHARED, "pdma", phy);
  798. if (ret) {
  799. dev_err(pdev->dev, "channel request irq fail!\n");
  800. return ret;
  801. }
  802. }
  803. spin_lock_init(&chan->desc_lock);
  804. chan->dev = pdev->dev;
  805. chan->chan.device = &pdev->device;
  806. tasklet_setup(&chan->tasklet, dma_do_tasklet);
  807. INIT_LIST_HEAD(&chan->chain_pending);
  808. INIT_LIST_HEAD(&chan->chain_running);
  809. /* register virt channel to dma engine */
  810. list_add_tail(&chan->chan.device_node, &pdev->device.channels);
  811. return 0;
  812. }
  813. static const struct of_device_id mmp_pdma_dt_ids[] = {
  814. { .compatible = "marvell,pdma-1.0", },
  815. {}
  816. };
  817. MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
  818. static struct dma_chan *mmp_pdma_dma_xlate(struct of_phandle_args *dma_spec,
  819. struct of_dma *ofdma)
  820. {
  821. struct mmp_pdma_device *d = ofdma->of_dma_data;
  822. struct dma_chan *chan;
  823. chan = dma_get_any_slave_channel(&d->device);
  824. if (!chan)
  825. return NULL;
  826. to_mmp_pdma_chan(chan)->drcmr = dma_spec->args[0];
  827. return chan;
  828. }
  829. static int mmp_pdma_probe(struct platform_device *op)
  830. {
  831. struct mmp_pdma_device *pdev;
  832. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  833. int i, ret, irq = 0;
  834. int dma_channels = 0, irq_num = 0;
  835. const enum dma_slave_buswidth widths =
  836. DMA_SLAVE_BUSWIDTH_1_BYTE | DMA_SLAVE_BUSWIDTH_2_BYTES |
  837. DMA_SLAVE_BUSWIDTH_4_BYTES;
  838. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  839. if (!pdev)
  840. return -ENOMEM;
  841. pdev->dev = &op->dev;
  842. spin_lock_init(&pdev->phy_lock);
  843. pdev->base = devm_platform_ioremap_resource(op, 0);
  844. if (IS_ERR(pdev->base))
  845. return PTR_ERR(pdev->base);
  846. if (pdev->dev->of_node) {
  847. /* Parse new and deprecated dma-channels properties */
  848. if (of_property_read_u32(pdev->dev->of_node, "dma-channels",
  849. &dma_channels))
  850. of_property_read_u32(pdev->dev->of_node, "#dma-channels",
  851. &dma_channels);
  852. } else if (pdata && pdata->dma_channels) {
  853. dma_channels = pdata->dma_channels;
  854. } else {
  855. dma_channels = 32; /* default 32 channel */
  856. }
  857. pdev->dma_channels = dma_channels;
  858. for (i = 0; i < dma_channels; i++) {
  859. if (platform_get_irq_optional(op, i) > 0)
  860. irq_num++;
  861. }
  862. pdev->phy = devm_kcalloc(pdev->dev, dma_channels, sizeof(*pdev->phy),
  863. GFP_KERNEL);
  864. if (pdev->phy == NULL)
  865. return -ENOMEM;
  866. INIT_LIST_HEAD(&pdev->device.channels);
  867. if (irq_num != dma_channels) {
  868. /* all chan share one irq, demux inside */
  869. irq = platform_get_irq(op, 0);
  870. ret = devm_request_irq(pdev->dev, irq, mmp_pdma_int_handler,
  871. IRQF_SHARED, "pdma", pdev);
  872. if (ret)
  873. return ret;
  874. }
  875. for (i = 0; i < dma_channels; i++) {
  876. irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
  877. ret = mmp_pdma_chan_init(pdev, i, irq);
  878. if (ret)
  879. return ret;
  880. }
  881. dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
  882. dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
  883. dma_cap_set(DMA_CYCLIC, pdev->device.cap_mask);
  884. dma_cap_set(DMA_PRIVATE, pdev->device.cap_mask);
  885. pdev->device.dev = &op->dev;
  886. pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
  887. pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
  888. pdev->device.device_tx_status = mmp_pdma_tx_status;
  889. pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
  890. pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
  891. pdev->device.device_prep_dma_cyclic = mmp_pdma_prep_dma_cyclic;
  892. pdev->device.device_issue_pending = mmp_pdma_issue_pending;
  893. pdev->device.device_config = mmp_pdma_config;
  894. pdev->device.device_terminate_all = mmp_pdma_terminate_all;
  895. pdev->device.copy_align = DMAENGINE_ALIGN_8_BYTES;
  896. pdev->device.src_addr_widths = widths;
  897. pdev->device.dst_addr_widths = widths;
  898. pdev->device.directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  899. pdev->device.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  900. if (pdev->dev->coherent_dma_mask)
  901. dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
  902. else
  903. dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
  904. ret = dma_async_device_register(&pdev->device);
  905. if (ret) {
  906. dev_err(pdev->device.dev, "unable to register\n");
  907. return ret;
  908. }
  909. if (op->dev.of_node) {
  910. /* Device-tree DMA controller registration */
  911. ret = of_dma_controller_register(op->dev.of_node,
  912. mmp_pdma_dma_xlate, pdev);
  913. if (ret < 0) {
  914. dev_err(&op->dev, "of_dma_controller_register failed\n");
  915. dma_async_device_unregister(&pdev->device);
  916. return ret;
  917. }
  918. }
  919. platform_set_drvdata(op, pdev);
  920. dev_info(pdev->device.dev, "initialized %d channels\n", dma_channels);
  921. return 0;
  922. }
  923. static const struct platform_device_id mmp_pdma_id_table[] = {
  924. { "mmp-pdma", },
  925. { },
  926. };
  927. static struct platform_driver mmp_pdma_driver = {
  928. .driver = {
  929. .name = "mmp-pdma",
  930. .of_match_table = mmp_pdma_dt_ids,
  931. },
  932. .id_table = mmp_pdma_id_table,
  933. .probe = mmp_pdma_probe,
  934. .remove_new = mmp_pdma_remove,
  935. };
  936. module_platform_driver(mmp_pdma_driver);
  937. MODULE_DESCRIPTION("MARVELL MMP Peripheral DMA Driver");
  938. MODULE_AUTHOR("Marvell International Ltd.");
  939. MODULE_LICENSE("GPL v2");