mmp_pdma.c 27 KB

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