mmp_pdma.c 23 KB

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