mmp_pdma.c 29 KB

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