mmp_pdma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. /* channel's basic info */
  89. struct tasklet_struct tasklet;
  90. u32 dcmd;
  91. u32 drcmr;
  92. u32 dev_addr;
  93. /* list for desc */
  94. spinlock_t desc_lock; /* Descriptor list lock */
  95. struct list_head chain_pending; /* Link descriptors queue for pending */
  96. struct list_head chain_running; /* Link descriptors queue for running */
  97. bool idle; /* channel statue machine */
  98. bool byte_align;
  99. struct dma_pool *desc_pool; /* Descriptors pool */
  100. };
  101. struct mmp_pdma_phy {
  102. int idx;
  103. void __iomem *base;
  104. struct mmp_pdma_chan *vchan;
  105. };
  106. struct mmp_pdma_device {
  107. int dma_channels;
  108. void __iomem *base;
  109. struct device *dev;
  110. struct dma_device device;
  111. struct mmp_pdma_phy *phy;
  112. spinlock_t phy_lock; /* protect alloc/free phy channels */
  113. };
  114. #define tx_to_mmp_pdma_desc(tx) container_of(tx, struct mmp_pdma_desc_sw, async_tx)
  115. #define to_mmp_pdma_desc(lh) container_of(lh, struct mmp_pdma_desc_sw, node)
  116. #define to_mmp_pdma_chan(dchan) container_of(dchan, struct mmp_pdma_chan, chan)
  117. #define to_mmp_pdma_dev(dmadev) container_of(dmadev, struct mmp_pdma_device, device)
  118. static void set_desc(struct mmp_pdma_phy *phy, dma_addr_t addr)
  119. {
  120. u32 reg = (phy->idx << 4) + DDADR;
  121. writel(addr, phy->base + reg);
  122. }
  123. static void enable_chan(struct mmp_pdma_phy *phy)
  124. {
  125. u32 reg, dalgn;
  126. if (!phy->vchan)
  127. return;
  128. reg = DRCMR(phy->vchan->drcmr);
  129. writel(DRCMR_MAPVLD | phy->idx, phy->base + reg);
  130. dalgn = readl(phy->base + DALGN);
  131. if (phy->vchan->byte_align)
  132. dalgn |= 1 << phy->idx;
  133. else
  134. dalgn &= ~(1 << phy->idx);
  135. writel(dalgn, phy->base + DALGN);
  136. reg = (phy->idx << 2) + DCSR;
  137. writel(readl(phy->base + reg) | DCSR_RUN,
  138. phy->base + reg);
  139. }
  140. static void disable_chan(struct mmp_pdma_phy *phy)
  141. {
  142. u32 reg;
  143. if (phy) {
  144. reg = (phy->idx << 2) + DCSR;
  145. writel(readl(phy->base + reg) & ~DCSR_RUN,
  146. phy->base + reg);
  147. }
  148. }
  149. static int clear_chan_irq(struct mmp_pdma_phy *phy)
  150. {
  151. u32 dcsr;
  152. u32 dint = readl(phy->base + DINT);
  153. u32 reg = (phy->idx << 2) + DCSR;
  154. if (dint & BIT(phy->idx)) {
  155. /* clear irq */
  156. dcsr = readl(phy->base + reg);
  157. writel(dcsr, phy->base + reg);
  158. if ((dcsr & DCSR_BUSERR) && (phy->vchan))
  159. dev_warn(phy->vchan->dev, "DCSR_BUSERR\n");
  160. return 0;
  161. }
  162. return -EAGAIN;
  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. tasklet_schedule(&phy->vchan->tasklet);
  169. return IRQ_HANDLED;
  170. } else
  171. return IRQ_NONE;
  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. dint &= (dint - 1);
  183. phy = &pdev->phy[i];
  184. ret = mmp_pdma_chan_handler(irq, phy);
  185. if (ret == IRQ_HANDLED)
  186. irq_num++;
  187. }
  188. if (irq_num)
  189. return IRQ_HANDLED;
  190. else
  191. return IRQ_NONE;
  192. }
  193. /* lookup free phy channel as descending priority */
  194. static struct mmp_pdma_phy *lookup_phy(struct mmp_pdma_chan *pchan)
  195. {
  196. int prio, i;
  197. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  198. struct mmp_pdma_phy *phy, *found = NULL;
  199. unsigned long flags;
  200. /*
  201. * dma channel priorities
  202. * ch 0 - 3, 16 - 19 <--> (0)
  203. * ch 4 - 7, 20 - 23 <--> (1)
  204. * ch 8 - 11, 24 - 27 <--> (2)
  205. * ch 12 - 15, 28 - 31 <--> (3)
  206. */
  207. spin_lock_irqsave(&pdev->phy_lock, flags);
  208. for (prio = 0; prio <= (((pdev->dma_channels - 1) & 0xf) >> 2); prio++) {
  209. for (i = 0; i < pdev->dma_channels; i++) {
  210. if (prio != ((i & 0xf) >> 2))
  211. continue;
  212. phy = &pdev->phy[i];
  213. if (!phy->vchan) {
  214. phy->vchan = pchan;
  215. found = phy;
  216. goto out_unlock;
  217. }
  218. }
  219. }
  220. out_unlock:
  221. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  222. return found;
  223. }
  224. static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
  225. {
  226. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  227. unsigned long flags;
  228. u32 reg;
  229. if (!pchan->phy)
  230. return;
  231. /* clear the channel mapping in DRCMR */
  232. reg = DRCMR(pchan->phy->vchan->drcmr);
  233. writel(0, pchan->phy->base + reg);
  234. spin_lock_irqsave(&pdev->phy_lock, flags);
  235. pchan->phy->vchan = NULL;
  236. pchan->phy = NULL;
  237. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  238. }
  239. /* desc->tx_list ==> pending list */
  240. static void append_pending_queue(struct mmp_pdma_chan *chan,
  241. struct mmp_pdma_desc_sw *desc)
  242. {
  243. struct mmp_pdma_desc_sw *tail =
  244. to_mmp_pdma_desc(chan->chain_pending.prev);
  245. if (list_empty(&chan->chain_pending))
  246. goto out_splice;
  247. /* one irq per queue, even appended */
  248. tail->desc.ddadr = desc->async_tx.phys;
  249. tail->desc.dcmd &= ~DCMD_ENDIRQEN;
  250. /* softly link to pending list */
  251. out_splice:
  252. list_splice_tail_init(&desc->tx_list, &chan->chain_pending);
  253. }
  254. /**
  255. * start_pending_queue - transfer any pending transactions
  256. * pending list ==> running list
  257. */
  258. static void start_pending_queue(struct mmp_pdma_chan *chan)
  259. {
  260. struct mmp_pdma_desc_sw *desc;
  261. /* still in running, irq will start the pending list */
  262. if (!chan->idle) {
  263. dev_dbg(chan->dev, "DMA controller still busy\n");
  264. return;
  265. }
  266. if (list_empty(&chan->chain_pending)) {
  267. /* chance to re-fetch phy channel with higher prio */
  268. mmp_pdma_free_phy(chan);
  269. dev_dbg(chan->dev, "no pending list\n");
  270. return;
  271. }
  272. if (!chan->phy) {
  273. chan->phy = lookup_phy(chan);
  274. if (!chan->phy) {
  275. dev_dbg(chan->dev, "no free dma channel\n");
  276. return;
  277. }
  278. }
  279. /*
  280. * pending -> running
  281. * reintilize pending list
  282. */
  283. desc = list_first_entry(&chan->chain_pending,
  284. struct mmp_pdma_desc_sw, node);
  285. list_splice_tail_init(&chan->chain_pending, &chan->chain_running);
  286. /*
  287. * Program the descriptor's address into the DMA controller,
  288. * then start the DMA transaction
  289. */
  290. set_desc(chan->phy, desc->async_tx.phys);
  291. enable_chan(chan->phy);
  292. chan->idle = false;
  293. }
  294. /* desc->tx_list ==> pending list */
  295. static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx)
  296. {
  297. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan);
  298. struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx);
  299. struct mmp_pdma_desc_sw *child;
  300. unsigned long flags;
  301. dma_cookie_t cookie = -EBUSY;
  302. spin_lock_irqsave(&chan->desc_lock, flags);
  303. list_for_each_entry(child, &desc->tx_list, node) {
  304. cookie = dma_cookie_assign(&child->async_tx);
  305. }
  306. append_pending_queue(chan, desc);
  307. spin_unlock_irqrestore(&chan->desc_lock, flags);
  308. return cookie;
  309. }
  310. static struct mmp_pdma_desc_sw *
  311. mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan)
  312. {
  313. struct mmp_pdma_desc_sw *desc;
  314. dma_addr_t pdesc;
  315. desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
  316. if (!desc) {
  317. dev_err(chan->dev, "out of memory for link descriptor\n");
  318. return NULL;
  319. }
  320. memset(desc, 0, sizeof(*desc));
  321. INIT_LIST_HEAD(&desc->tx_list);
  322. dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
  323. /* each desc has submit */
  324. desc->async_tx.tx_submit = mmp_pdma_tx_submit;
  325. desc->async_tx.phys = pdesc;
  326. return desc;
  327. }
  328. /**
  329. * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel.
  330. *
  331. * This function will create a dma pool for descriptor allocation.
  332. * Request irq only when channel is requested
  333. * Return - The number of allocated descriptors.
  334. */
  335. static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan)
  336. {
  337. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  338. if (chan->desc_pool)
  339. return 1;
  340. chan->desc_pool =
  341. dma_pool_create(dev_name(&dchan->dev->device), chan->dev,
  342. sizeof(struct mmp_pdma_desc_sw),
  343. __alignof__(struct mmp_pdma_desc_sw), 0);
  344. if (!chan->desc_pool) {
  345. dev_err(chan->dev, "unable to allocate descriptor pool\n");
  346. return -ENOMEM;
  347. }
  348. mmp_pdma_free_phy(chan);
  349. chan->idle = true;
  350. chan->dev_addr = 0;
  351. return 1;
  352. }
  353. static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan,
  354. struct list_head *list)
  355. {
  356. struct mmp_pdma_desc_sw *desc, *_desc;
  357. list_for_each_entry_safe(desc, _desc, list, node) {
  358. list_del(&desc->node);
  359. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  360. }
  361. }
  362. static void mmp_pdma_free_chan_resources(struct dma_chan *dchan)
  363. {
  364. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  365. unsigned long flags;
  366. spin_lock_irqsave(&chan->desc_lock, flags);
  367. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  368. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  369. spin_unlock_irqrestore(&chan->desc_lock, flags);
  370. dma_pool_destroy(chan->desc_pool);
  371. chan->desc_pool = NULL;
  372. chan->idle = true;
  373. chan->dev_addr = 0;
  374. mmp_pdma_free_phy(chan);
  375. return;
  376. }
  377. static struct dma_async_tx_descriptor *
  378. mmp_pdma_prep_memcpy(struct dma_chan *dchan,
  379. dma_addr_t dma_dst, dma_addr_t dma_src,
  380. size_t len, unsigned long flags)
  381. {
  382. struct mmp_pdma_chan *chan;
  383. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  384. size_t copy = 0;
  385. if (!dchan)
  386. return NULL;
  387. if (!len)
  388. return NULL;
  389. chan = to_mmp_pdma_chan(dchan);
  390. chan->byte_align = false;
  391. if (!chan->dir) {
  392. chan->dir = DMA_MEM_TO_MEM;
  393. chan->dcmd = DCMD_INCTRGADDR | DCMD_INCSRCADDR;
  394. chan->dcmd |= DCMD_BURST32;
  395. }
  396. do {
  397. /* Allocate the link descriptor from DMA pool */
  398. new = mmp_pdma_alloc_descriptor(chan);
  399. if (!new) {
  400. dev_err(chan->dev, "no memory for desc\n");
  401. goto fail;
  402. }
  403. copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
  404. if (dma_src & 0x7 || dma_dst & 0x7)
  405. chan->byte_align = true;
  406. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy);
  407. new->desc.dsadr = dma_src;
  408. new->desc.dtadr = dma_dst;
  409. if (!first)
  410. first = new;
  411. else
  412. prev->desc.ddadr = new->async_tx.phys;
  413. new->async_tx.cookie = 0;
  414. async_tx_ack(&new->async_tx);
  415. prev = new;
  416. len -= copy;
  417. if (chan->dir == DMA_MEM_TO_DEV) {
  418. dma_src += copy;
  419. } else if (chan->dir == DMA_DEV_TO_MEM) {
  420. dma_dst += copy;
  421. } else if (chan->dir == DMA_MEM_TO_MEM) {
  422. dma_src += copy;
  423. dma_dst += copy;
  424. }
  425. /* Insert the link descriptor to the LD ring */
  426. list_add_tail(&new->node, &first->tx_list);
  427. } while (len);
  428. first->async_tx.flags = flags; /* client is in control of this ack */
  429. first->async_tx.cookie = -EBUSY;
  430. /* last desc and fire IRQ */
  431. new->desc.ddadr = DDADR_STOP;
  432. new->desc.dcmd |= DCMD_ENDIRQEN;
  433. return &first->async_tx;
  434. fail:
  435. if (first)
  436. mmp_pdma_free_desc_list(chan, &first->tx_list);
  437. return NULL;
  438. }
  439. static struct dma_async_tx_descriptor *
  440. mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  441. unsigned int sg_len, enum dma_transfer_direction dir,
  442. unsigned long flags, void *context)
  443. {
  444. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  445. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  446. size_t len, avail;
  447. struct scatterlist *sg;
  448. dma_addr_t addr;
  449. int i;
  450. if ((sgl == NULL) || (sg_len == 0))
  451. return NULL;
  452. chan->byte_align = false;
  453. for_each_sg(sgl, sg, sg_len, i) {
  454. addr = sg_dma_address(sg);
  455. avail = sg_dma_len(sgl);
  456. do {
  457. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  458. if (addr & 0x7)
  459. chan->byte_align = true;
  460. /* allocate and populate the descriptor */
  461. new = mmp_pdma_alloc_descriptor(chan);
  462. if (!new) {
  463. dev_err(chan->dev, "no memory for desc\n");
  464. goto fail;
  465. }
  466. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len);
  467. if (dir == DMA_MEM_TO_DEV) {
  468. new->desc.dsadr = addr;
  469. new->desc.dtadr = chan->dev_addr;
  470. } else {
  471. new->desc.dsadr = chan->dev_addr;
  472. new->desc.dtadr = addr;
  473. }
  474. if (!first)
  475. first = new;
  476. else
  477. prev->desc.ddadr = new->async_tx.phys;
  478. new->async_tx.cookie = 0;
  479. async_tx_ack(&new->async_tx);
  480. prev = new;
  481. /* Insert the link descriptor to the LD ring */
  482. list_add_tail(&new->node, &first->tx_list);
  483. /* update metadata */
  484. addr += len;
  485. avail -= len;
  486. } while (avail);
  487. }
  488. first->async_tx.cookie = -EBUSY;
  489. first->async_tx.flags = flags;
  490. /* last desc and fire IRQ */
  491. new->desc.ddadr = DDADR_STOP;
  492. new->desc.dcmd |= DCMD_ENDIRQEN;
  493. return &first->async_tx;
  494. fail:
  495. if (first)
  496. mmp_pdma_free_desc_list(chan, &first->tx_list);
  497. return NULL;
  498. }
  499. static int mmp_pdma_control(struct dma_chan *dchan, enum dma_ctrl_cmd cmd,
  500. unsigned long arg)
  501. {
  502. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  503. struct dma_slave_config *cfg = (void *)arg;
  504. unsigned long flags;
  505. int ret = 0;
  506. u32 maxburst = 0, addr = 0;
  507. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  508. if (!dchan)
  509. return -EINVAL;
  510. switch (cmd) {
  511. case DMA_TERMINATE_ALL:
  512. disable_chan(chan->phy);
  513. mmp_pdma_free_phy(chan);
  514. spin_lock_irqsave(&chan->desc_lock, flags);
  515. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  516. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  517. spin_unlock_irqrestore(&chan->desc_lock, flags);
  518. chan->idle = true;
  519. break;
  520. case DMA_SLAVE_CONFIG:
  521. if (cfg->direction == DMA_DEV_TO_MEM) {
  522. chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
  523. maxburst = cfg->src_maxburst;
  524. width = cfg->src_addr_width;
  525. addr = cfg->src_addr;
  526. } else if (cfg->direction == DMA_MEM_TO_DEV) {
  527. chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
  528. maxburst = cfg->dst_maxburst;
  529. width = cfg->dst_addr_width;
  530. addr = cfg->dst_addr;
  531. }
  532. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  533. chan->dcmd |= DCMD_WIDTH1;
  534. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  535. chan->dcmd |= DCMD_WIDTH2;
  536. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  537. chan->dcmd |= DCMD_WIDTH4;
  538. if (maxburst == 8)
  539. chan->dcmd |= DCMD_BURST8;
  540. else if (maxburst == 16)
  541. chan->dcmd |= DCMD_BURST16;
  542. else if (maxburst == 32)
  543. chan->dcmd |= DCMD_BURST32;
  544. chan->dir = cfg->direction;
  545. chan->dev_addr = addr;
  546. /* FIXME: drivers should be ported over to use the filter
  547. * function. Once that's done, the following two lines can
  548. * be removed.
  549. */
  550. if (cfg->slave_id)
  551. chan->drcmr = cfg->slave_id;
  552. break;
  553. default:
  554. return -ENOSYS;
  555. }
  556. return ret;
  557. }
  558. static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
  559. dma_cookie_t cookie, struct dma_tx_state *txstate)
  560. {
  561. return dma_cookie_status(dchan, cookie, txstate);
  562. }
  563. /**
  564. * mmp_pdma_issue_pending - Issue the DMA start command
  565. * pending list ==> running list
  566. */
  567. static void mmp_pdma_issue_pending(struct dma_chan *dchan)
  568. {
  569. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  570. unsigned long flags;
  571. spin_lock_irqsave(&chan->desc_lock, flags);
  572. start_pending_queue(chan);
  573. spin_unlock_irqrestore(&chan->desc_lock, flags);
  574. }
  575. /*
  576. * dma_do_tasklet
  577. * Do call back
  578. * Start pending list
  579. */
  580. static void dma_do_tasklet(unsigned long data)
  581. {
  582. struct mmp_pdma_chan *chan = (struct mmp_pdma_chan *)data;
  583. struct mmp_pdma_desc_sw *desc, *_desc;
  584. LIST_HEAD(chain_cleanup);
  585. unsigned long flags;
  586. /* submit pending list; callback for each desc; free desc */
  587. spin_lock_irqsave(&chan->desc_lock, flags);
  588. list_for_each_entry_safe(desc, _desc, &chan->chain_running, node) {
  589. /*
  590. * move the descriptors to a temporary list so we can drop
  591. * the lock during the entire cleanup operation
  592. */
  593. list_del(&desc->node);
  594. list_add(&desc->node, &chain_cleanup);
  595. /*
  596. * Look for the first list entry which has the ENDIRQEN flag
  597. * set. That is the descriptor we got an interrupt for, so
  598. * complete that transaction and its cookie.
  599. */
  600. if (desc->desc.dcmd & DCMD_ENDIRQEN) {
  601. dma_cookie_t cookie = desc->async_tx.cookie;
  602. dma_cookie_complete(&desc->async_tx);
  603. dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
  604. break;
  605. }
  606. }
  607. /*
  608. * The hardware is idle and ready for more when the
  609. * chain_running list is empty.
  610. */
  611. chan->idle = list_empty(&chan->chain_running);
  612. /* Start any pending transactions automatically */
  613. start_pending_queue(chan);
  614. spin_unlock_irqrestore(&chan->desc_lock, flags);
  615. /* Run the callback for each descriptor, in order */
  616. list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
  617. struct dma_async_tx_descriptor *txd = &desc->async_tx;
  618. /* Remove from the list of transactions */
  619. list_del(&desc->node);
  620. /* Run the link descriptor callback function */
  621. if (txd->callback)
  622. txd->callback(txd->callback_param);
  623. dma_pool_free(chan->desc_pool, desc, txd->phys);
  624. }
  625. }
  626. static int mmp_pdma_remove(struct platform_device *op)
  627. {
  628. struct mmp_pdma_device *pdev = platform_get_drvdata(op);
  629. dma_async_device_unregister(&pdev->device);
  630. return 0;
  631. }
  632. static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev,
  633. int idx, int irq)
  634. {
  635. struct mmp_pdma_phy *phy = &pdev->phy[idx];
  636. struct mmp_pdma_chan *chan;
  637. int ret;
  638. chan = devm_kzalloc(pdev->dev,
  639. sizeof(struct mmp_pdma_chan), GFP_KERNEL);
  640. if (chan == NULL)
  641. return -ENOMEM;
  642. phy->idx = idx;
  643. phy->base = pdev->base;
  644. if (irq) {
  645. ret = devm_request_irq(pdev->dev, irq,
  646. mmp_pdma_chan_handler, IRQF_DISABLED, "pdma", phy);
  647. if (ret) {
  648. dev_err(pdev->dev, "channel request irq fail!\n");
  649. return ret;
  650. }
  651. }
  652. spin_lock_init(&chan->desc_lock);
  653. chan->dev = pdev->dev;
  654. chan->chan.device = &pdev->device;
  655. tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
  656. INIT_LIST_HEAD(&chan->chain_pending);
  657. INIT_LIST_HEAD(&chan->chain_running);
  658. /* register virt channel to dma engine */
  659. list_add_tail(&chan->chan.device_node,
  660. &pdev->device.channels);
  661. return 0;
  662. }
  663. static struct of_device_id mmp_pdma_dt_ids[] = {
  664. { .compatible = "marvell,pdma-1.0", },
  665. {}
  666. };
  667. MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
  668. static struct dma_chan *mmp_pdma_dma_xlate(struct of_phandle_args *dma_spec,
  669. struct of_dma *ofdma)
  670. {
  671. struct mmp_pdma_device *d = ofdma->of_dma_data;
  672. struct dma_chan *chan, *candidate;
  673. retry:
  674. candidate = NULL;
  675. /* walk the list of channels registered with the current instance and
  676. * find one that is currently unused */
  677. list_for_each_entry(chan, &d->device.channels, device_node)
  678. if (chan->client_count == 0) {
  679. candidate = chan;
  680. break;
  681. }
  682. if (!candidate)
  683. return NULL;
  684. /* dma_get_slave_channel will return NULL if we lost a race between
  685. * the lookup and the reservation */
  686. chan = dma_get_slave_channel(candidate);
  687. if (chan) {
  688. struct mmp_pdma_chan *c = to_mmp_pdma_chan(chan);
  689. c->drcmr = dma_spec->args[0];
  690. return chan;
  691. }
  692. goto retry;
  693. }
  694. static int mmp_pdma_probe(struct platform_device *op)
  695. {
  696. struct mmp_pdma_device *pdev;
  697. const struct of_device_id *of_id;
  698. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  699. struct resource *iores;
  700. int i, ret, irq = 0;
  701. int dma_channels = 0, irq_num = 0;
  702. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  703. if (!pdev)
  704. return -ENOMEM;
  705. pdev->dev = &op->dev;
  706. spin_lock_init(&pdev->phy_lock);
  707. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  708. pdev->base = devm_ioremap_resource(pdev->dev, iores);
  709. if (IS_ERR(pdev->base))
  710. return PTR_ERR(pdev->base);
  711. of_id = of_match_device(mmp_pdma_dt_ids, pdev->dev);
  712. if (of_id)
  713. of_property_read_u32(pdev->dev->of_node,
  714. "#dma-channels", &dma_channels);
  715. else if (pdata && pdata->dma_channels)
  716. dma_channels = pdata->dma_channels;
  717. else
  718. dma_channels = 32; /* default 32 channel */
  719. pdev->dma_channels = dma_channels;
  720. for (i = 0; i < dma_channels; i++) {
  721. if (platform_get_irq(op, i) > 0)
  722. irq_num++;
  723. }
  724. pdev->phy = devm_kzalloc(pdev->dev,
  725. dma_channels * sizeof(struct mmp_pdma_chan), GFP_KERNEL);
  726. if (pdev->phy == NULL)
  727. return -ENOMEM;
  728. INIT_LIST_HEAD(&pdev->device.channels);
  729. if (irq_num != dma_channels) {
  730. /* all chan share one irq, demux inside */
  731. irq = platform_get_irq(op, 0);
  732. ret = devm_request_irq(pdev->dev, irq,
  733. mmp_pdma_int_handler, IRQF_DISABLED, "pdma", pdev);
  734. if (ret)
  735. return ret;
  736. }
  737. for (i = 0; i < dma_channels; i++) {
  738. irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
  739. ret = mmp_pdma_chan_init(pdev, i, irq);
  740. if (ret)
  741. return ret;
  742. }
  743. dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
  744. dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
  745. pdev->device.dev = &op->dev;
  746. pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
  747. pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
  748. pdev->device.device_tx_status = mmp_pdma_tx_status;
  749. pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
  750. pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
  751. pdev->device.device_issue_pending = mmp_pdma_issue_pending;
  752. pdev->device.device_control = mmp_pdma_control;
  753. pdev->device.copy_align = PDMA_ALIGNMENT;
  754. if (pdev->dev->coherent_dma_mask)
  755. dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
  756. else
  757. dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
  758. ret = dma_async_device_register(&pdev->device);
  759. if (ret) {
  760. dev_err(pdev->device.dev, "unable to register\n");
  761. return ret;
  762. }
  763. if (op->dev.of_node) {
  764. /* Device-tree DMA controller registration */
  765. ret = of_dma_controller_register(op->dev.of_node,
  766. mmp_pdma_dma_xlate, pdev);
  767. if (ret < 0) {
  768. dev_err(&op->dev, "of_dma_controller_register failed\n");
  769. return ret;
  770. }
  771. }
  772. dev_info(pdev->device.dev, "initialized %d channels\n", dma_channels);
  773. return 0;
  774. }
  775. static const struct platform_device_id mmp_pdma_id_table[] = {
  776. { "mmp-pdma", },
  777. { },
  778. };
  779. static struct platform_driver mmp_pdma_driver = {
  780. .driver = {
  781. .name = "mmp-pdma",
  782. .owner = THIS_MODULE,
  783. .of_match_table = mmp_pdma_dt_ids,
  784. },
  785. .id_table = mmp_pdma_id_table,
  786. .probe = mmp_pdma_probe,
  787. .remove = mmp_pdma_remove,
  788. };
  789. bool mmp_pdma_filter_fn(struct dma_chan *chan, void *param)
  790. {
  791. struct mmp_pdma_chan *c = to_mmp_pdma_chan(chan);
  792. if (chan->device->dev->driver != &mmp_pdma_driver.driver)
  793. return false;
  794. c->drcmr = *(unsigned int *) param;
  795. return true;
  796. }
  797. EXPORT_SYMBOL_GPL(mmp_pdma_filter_fn);
  798. module_platform_driver(mmp_pdma_driver);
  799. MODULE_DESCRIPTION("MARVELL MMP Periphera DMA Driver");
  800. MODULE_AUTHOR("Marvell International Ltd.");
  801. MODULE_LICENSE("GPL v2");