mmp_pdma.c 24 KB

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