mmp_pdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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 0x1000
  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;
  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. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  207. return phy;
  208. }
  209. }
  210. }
  211. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  212. return NULL;
  213. }
  214. static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
  215. {
  216. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  217. unsigned long flags;
  218. u32 reg;
  219. if (!pchan->phy)
  220. return;
  221. /* clear the channel mapping in DRCMR */
  222. reg = DRCMR(pchan->phy->vchan->drcmr);
  223. writel(0, pchan->phy->base + reg);
  224. spin_lock_irqsave(&pdev->phy_lock, flags);
  225. pchan->phy->vchan = NULL;
  226. pchan->phy = NULL;
  227. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  228. }
  229. /* desc->tx_list ==> pending list */
  230. static void append_pending_queue(struct mmp_pdma_chan *chan,
  231. struct mmp_pdma_desc_sw *desc)
  232. {
  233. struct mmp_pdma_desc_sw *tail =
  234. to_mmp_pdma_desc(chan->chain_pending.prev);
  235. if (list_empty(&chan->chain_pending))
  236. goto out_splice;
  237. /* one irq per queue, even appended */
  238. tail->desc.ddadr = desc->async_tx.phys;
  239. tail->desc.dcmd &= ~DCMD_ENDIRQEN;
  240. /* softly link to pending list */
  241. out_splice:
  242. list_splice_tail_init(&desc->tx_list, &chan->chain_pending);
  243. }
  244. /**
  245. * start_pending_queue - transfer any pending transactions
  246. * pending list ==> running list
  247. */
  248. static void start_pending_queue(struct mmp_pdma_chan *chan)
  249. {
  250. struct mmp_pdma_desc_sw *desc;
  251. /* still in running, irq will start the pending list */
  252. if (!chan->idle) {
  253. dev_dbg(chan->dev, "DMA controller still busy\n");
  254. return;
  255. }
  256. if (list_empty(&chan->chain_pending)) {
  257. /* chance to re-fetch phy channel with higher prio */
  258. mmp_pdma_free_phy(chan);
  259. dev_dbg(chan->dev, "no pending list\n");
  260. return;
  261. }
  262. if (!chan->phy) {
  263. chan->phy = lookup_phy(chan);
  264. if (!chan->phy) {
  265. dev_dbg(chan->dev, "no free dma channel\n");
  266. return;
  267. }
  268. }
  269. /*
  270. * pending -> running
  271. * reintilize pending list
  272. */
  273. desc = list_first_entry(&chan->chain_pending,
  274. struct mmp_pdma_desc_sw, node);
  275. list_splice_tail_init(&chan->chain_pending, &chan->chain_running);
  276. /*
  277. * Program the descriptor's address into the DMA controller,
  278. * then start the DMA transaction
  279. */
  280. set_desc(chan->phy, desc->async_tx.phys);
  281. enable_chan(chan->phy);
  282. chan->idle = false;
  283. }
  284. /* desc->tx_list ==> pending list */
  285. static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx)
  286. {
  287. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan);
  288. struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx);
  289. struct mmp_pdma_desc_sw *child;
  290. unsigned long flags;
  291. dma_cookie_t cookie = -EBUSY;
  292. spin_lock_irqsave(&chan->desc_lock, flags);
  293. list_for_each_entry(child, &desc->tx_list, node) {
  294. cookie = dma_cookie_assign(&child->async_tx);
  295. }
  296. append_pending_queue(chan, desc);
  297. spin_unlock_irqrestore(&chan->desc_lock, flags);
  298. return cookie;
  299. }
  300. static struct mmp_pdma_desc_sw *
  301. mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan)
  302. {
  303. struct mmp_pdma_desc_sw *desc;
  304. dma_addr_t pdesc;
  305. desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
  306. if (!desc) {
  307. dev_err(chan->dev, "out of memory for link descriptor\n");
  308. return NULL;
  309. }
  310. memset(desc, 0, sizeof(*desc));
  311. INIT_LIST_HEAD(&desc->tx_list);
  312. dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
  313. /* each desc has submit */
  314. desc->async_tx.tx_submit = mmp_pdma_tx_submit;
  315. desc->async_tx.phys = pdesc;
  316. return desc;
  317. }
  318. /**
  319. * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel.
  320. *
  321. * This function will create a dma pool for descriptor allocation.
  322. * Request irq only when channel is requested
  323. * Return - The number of allocated descriptors.
  324. */
  325. static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan)
  326. {
  327. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  328. if (chan->desc_pool)
  329. return 1;
  330. chan->desc_pool =
  331. dma_pool_create(dev_name(&dchan->dev->device), chan->dev,
  332. sizeof(struct mmp_pdma_desc_sw),
  333. __alignof__(struct mmp_pdma_desc_sw), 0);
  334. if (!chan->desc_pool) {
  335. dev_err(chan->dev, "unable to allocate descriptor pool\n");
  336. return -ENOMEM;
  337. }
  338. mmp_pdma_free_phy(chan);
  339. chan->idle = true;
  340. chan->dev_addr = 0;
  341. return 1;
  342. }
  343. static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan,
  344. struct list_head *list)
  345. {
  346. struct mmp_pdma_desc_sw *desc, *_desc;
  347. list_for_each_entry_safe(desc, _desc, list, node) {
  348. list_del(&desc->node);
  349. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  350. }
  351. }
  352. static void mmp_pdma_free_chan_resources(struct dma_chan *dchan)
  353. {
  354. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  355. unsigned long flags;
  356. spin_lock_irqsave(&chan->desc_lock, flags);
  357. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  358. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  359. spin_unlock_irqrestore(&chan->desc_lock, flags);
  360. dma_pool_destroy(chan->desc_pool);
  361. chan->desc_pool = NULL;
  362. chan->idle = true;
  363. chan->dev_addr = 0;
  364. mmp_pdma_free_phy(chan);
  365. return;
  366. }
  367. static struct dma_async_tx_descriptor *
  368. mmp_pdma_prep_memcpy(struct dma_chan *dchan,
  369. dma_addr_t dma_dst, dma_addr_t dma_src,
  370. size_t len, unsigned long flags)
  371. {
  372. struct mmp_pdma_chan *chan;
  373. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  374. size_t copy = 0;
  375. if (!dchan)
  376. return NULL;
  377. if (!len)
  378. return NULL;
  379. chan = to_mmp_pdma_chan(dchan);
  380. if (!chan->dir) {
  381. chan->dir = DMA_MEM_TO_MEM;
  382. chan->dcmd = DCMD_INCTRGADDR | DCMD_INCSRCADDR;
  383. chan->dcmd |= DCMD_BURST32;
  384. }
  385. do {
  386. /* Allocate the link descriptor from DMA pool */
  387. new = mmp_pdma_alloc_descriptor(chan);
  388. if (!new) {
  389. dev_err(chan->dev, "no memory for desc\n");
  390. goto fail;
  391. }
  392. copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
  393. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy);
  394. new->desc.dsadr = dma_src;
  395. new->desc.dtadr = dma_dst;
  396. if (!first)
  397. first = new;
  398. else
  399. prev->desc.ddadr = new->async_tx.phys;
  400. new->async_tx.cookie = 0;
  401. async_tx_ack(&new->async_tx);
  402. prev = new;
  403. len -= copy;
  404. if (chan->dir == DMA_MEM_TO_DEV) {
  405. dma_src += copy;
  406. } else if (chan->dir == DMA_DEV_TO_MEM) {
  407. dma_dst += copy;
  408. } else if (chan->dir == DMA_MEM_TO_MEM) {
  409. dma_src += copy;
  410. dma_dst += copy;
  411. }
  412. /* Insert the link descriptor to the LD ring */
  413. list_add_tail(&new->node, &first->tx_list);
  414. } while (len);
  415. first->async_tx.flags = flags; /* client is in control of this ack */
  416. first->async_tx.cookie = -EBUSY;
  417. /* last desc and fire IRQ */
  418. new->desc.ddadr = DDADR_STOP;
  419. new->desc.dcmd |= DCMD_ENDIRQEN;
  420. return &first->async_tx;
  421. fail:
  422. if (first)
  423. mmp_pdma_free_desc_list(chan, &first->tx_list);
  424. return NULL;
  425. }
  426. static struct dma_async_tx_descriptor *
  427. mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  428. unsigned int sg_len, enum dma_transfer_direction dir,
  429. unsigned long flags, void *context)
  430. {
  431. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  432. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  433. size_t len, avail;
  434. struct scatterlist *sg;
  435. dma_addr_t addr;
  436. int i;
  437. if ((sgl == NULL) || (sg_len == 0))
  438. return NULL;
  439. for_each_sg(sgl, sg, sg_len, i) {
  440. addr = sg_dma_address(sg);
  441. avail = sg_dma_len(sgl);
  442. do {
  443. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  444. /* allocate and populate the descriptor */
  445. new = mmp_pdma_alloc_descriptor(chan);
  446. if (!new) {
  447. dev_err(chan->dev, "no memory for desc\n");
  448. goto fail;
  449. }
  450. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len);
  451. if (dir == DMA_MEM_TO_DEV) {
  452. new->desc.dsadr = addr;
  453. new->desc.dtadr = chan->dev_addr;
  454. } else {
  455. new->desc.dsadr = chan->dev_addr;
  456. new->desc.dtadr = addr;
  457. }
  458. if (!first)
  459. first = new;
  460. else
  461. prev->desc.ddadr = new->async_tx.phys;
  462. new->async_tx.cookie = 0;
  463. async_tx_ack(&new->async_tx);
  464. prev = new;
  465. /* Insert the link descriptor to the LD ring */
  466. list_add_tail(&new->node, &first->tx_list);
  467. /* update metadata */
  468. addr += len;
  469. avail -= len;
  470. } while (avail);
  471. }
  472. first->async_tx.cookie = -EBUSY;
  473. first->async_tx.flags = flags;
  474. /* last desc and fire IRQ */
  475. new->desc.ddadr = DDADR_STOP;
  476. new->desc.dcmd |= DCMD_ENDIRQEN;
  477. return &first->async_tx;
  478. fail:
  479. if (first)
  480. mmp_pdma_free_desc_list(chan, &first->tx_list);
  481. return NULL;
  482. }
  483. static int mmp_pdma_control(struct dma_chan *dchan, enum dma_ctrl_cmd cmd,
  484. unsigned long arg)
  485. {
  486. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  487. struct dma_slave_config *cfg = (void *)arg;
  488. unsigned long flags;
  489. int ret = 0;
  490. u32 maxburst = 0, addr = 0;
  491. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  492. if (!dchan)
  493. return -EINVAL;
  494. switch (cmd) {
  495. case DMA_TERMINATE_ALL:
  496. disable_chan(chan->phy);
  497. mmp_pdma_free_phy(chan);
  498. spin_lock_irqsave(&chan->desc_lock, flags);
  499. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  500. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  501. spin_unlock_irqrestore(&chan->desc_lock, flags);
  502. chan->idle = true;
  503. break;
  504. case DMA_SLAVE_CONFIG:
  505. if (cfg->direction == DMA_DEV_TO_MEM) {
  506. chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
  507. maxburst = cfg->src_maxburst;
  508. width = cfg->src_addr_width;
  509. addr = cfg->src_addr;
  510. } else if (cfg->direction == DMA_MEM_TO_DEV) {
  511. chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
  512. maxburst = cfg->dst_maxburst;
  513. width = cfg->dst_addr_width;
  514. addr = cfg->dst_addr;
  515. }
  516. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  517. chan->dcmd |= DCMD_WIDTH1;
  518. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  519. chan->dcmd |= DCMD_WIDTH2;
  520. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  521. chan->dcmd |= DCMD_WIDTH4;
  522. if (maxburst == 8)
  523. chan->dcmd |= DCMD_BURST8;
  524. else if (maxburst == 16)
  525. chan->dcmd |= DCMD_BURST16;
  526. else if (maxburst == 32)
  527. chan->dcmd |= DCMD_BURST32;
  528. chan->dir = cfg->direction;
  529. chan->drcmr = cfg->slave_id;
  530. chan->dev_addr = addr;
  531. break;
  532. default:
  533. return -ENOSYS;
  534. }
  535. return ret;
  536. }
  537. static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
  538. dma_cookie_t cookie, struct dma_tx_state *txstate)
  539. {
  540. return dma_cookie_status(dchan, cookie, txstate);
  541. }
  542. /**
  543. * mmp_pdma_issue_pending - Issue the DMA start command
  544. * pending list ==> running list
  545. */
  546. static void mmp_pdma_issue_pending(struct dma_chan *dchan)
  547. {
  548. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  549. unsigned long flags;
  550. spin_lock_irqsave(&chan->desc_lock, flags);
  551. start_pending_queue(chan);
  552. spin_unlock_irqrestore(&chan->desc_lock, flags);
  553. }
  554. /*
  555. * dma_do_tasklet
  556. * Do call back
  557. * Start pending list
  558. */
  559. static void dma_do_tasklet(unsigned long data)
  560. {
  561. struct mmp_pdma_chan *chan = (struct mmp_pdma_chan *)data;
  562. struct mmp_pdma_desc_sw *desc, *_desc;
  563. LIST_HEAD(chain_cleanup);
  564. unsigned long flags;
  565. /* submit pending list; callback for each desc; free desc */
  566. spin_lock_irqsave(&chan->desc_lock, flags);
  567. /* update the cookie if we have some descriptors to cleanup */
  568. if (!list_empty(&chan->chain_running)) {
  569. dma_cookie_t cookie;
  570. desc = to_mmp_pdma_desc(chan->chain_running.prev);
  571. cookie = desc->async_tx.cookie;
  572. dma_cookie_complete(&desc->async_tx);
  573. dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
  574. }
  575. /*
  576. * move the descriptors to a temporary list so we can drop the lock
  577. * during the entire cleanup operation
  578. */
  579. list_splice_tail_init(&chan->chain_running, &chain_cleanup);
  580. /* the hardware is now idle and ready for more */
  581. chan->idle = true;
  582. /* Start any pending transactions automatically */
  583. start_pending_queue(chan);
  584. spin_unlock_irqrestore(&chan->desc_lock, flags);
  585. /* Run the callback for each descriptor, in order */
  586. list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
  587. struct dma_async_tx_descriptor *txd = &desc->async_tx;
  588. /* Remove from the list of transactions */
  589. list_del(&desc->node);
  590. /* Run the link descriptor callback function */
  591. if (txd->callback)
  592. txd->callback(txd->callback_param);
  593. dma_pool_free(chan->desc_pool, desc, txd->phys);
  594. }
  595. }
  596. static int mmp_pdma_remove(struct platform_device *op)
  597. {
  598. struct mmp_pdma_device *pdev = platform_get_drvdata(op);
  599. dma_async_device_unregister(&pdev->device);
  600. return 0;
  601. }
  602. static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev,
  603. int idx, int irq)
  604. {
  605. struct mmp_pdma_phy *phy = &pdev->phy[idx];
  606. struct mmp_pdma_chan *chan;
  607. int ret;
  608. chan = devm_kzalloc(pdev->dev,
  609. sizeof(struct mmp_pdma_chan), GFP_KERNEL);
  610. if (chan == NULL)
  611. return -ENOMEM;
  612. phy->idx = idx;
  613. phy->base = pdev->base;
  614. if (irq) {
  615. ret = devm_request_irq(pdev->dev, irq,
  616. mmp_pdma_chan_handler, IRQF_DISABLED, "pdma", phy);
  617. if (ret) {
  618. dev_err(pdev->dev, "channel request irq fail!\n");
  619. return ret;
  620. }
  621. }
  622. spin_lock_init(&chan->desc_lock);
  623. chan->dev = pdev->dev;
  624. chan->chan.device = &pdev->device;
  625. tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
  626. INIT_LIST_HEAD(&chan->chain_pending);
  627. INIT_LIST_HEAD(&chan->chain_running);
  628. /* register virt channel to dma engine */
  629. list_add_tail(&chan->chan.device_node,
  630. &pdev->device.channels);
  631. return 0;
  632. }
  633. static struct of_device_id mmp_pdma_dt_ids[] = {
  634. { .compatible = "marvell,pdma-1.0", },
  635. {}
  636. };
  637. MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
  638. static int mmp_pdma_probe(struct platform_device *op)
  639. {
  640. struct mmp_pdma_device *pdev;
  641. const struct of_device_id *of_id;
  642. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  643. struct resource *iores;
  644. int i, ret, irq = 0;
  645. int dma_channels = 0, irq_num = 0;
  646. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  647. if (!pdev)
  648. return -ENOMEM;
  649. pdev->dev = &op->dev;
  650. spin_lock_init(&pdev->phy_lock);
  651. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  652. if (!iores)
  653. return -EINVAL;
  654. pdev->base = devm_ioremap_resource(pdev->dev, iores);
  655. if (IS_ERR(pdev->base))
  656. return PTR_ERR(pdev->base);
  657. of_id = of_match_device(mmp_pdma_dt_ids, pdev->dev);
  658. if (of_id)
  659. of_property_read_u32(pdev->dev->of_node,
  660. "#dma-channels", &dma_channels);
  661. else if (pdata && pdata->dma_channels)
  662. dma_channels = pdata->dma_channels;
  663. else
  664. dma_channels = 32; /* default 32 channel */
  665. pdev->dma_channels = dma_channels;
  666. for (i = 0; i < dma_channels; i++) {
  667. if (platform_get_irq(op, i) > 0)
  668. irq_num++;
  669. }
  670. pdev->phy = devm_kzalloc(pdev->dev,
  671. dma_channels * sizeof(struct mmp_pdma_chan), GFP_KERNEL);
  672. if (pdev->phy == NULL)
  673. return -ENOMEM;
  674. INIT_LIST_HEAD(&pdev->device.channels);
  675. if (irq_num != dma_channels) {
  676. /* all chan share one irq, demux inside */
  677. irq = platform_get_irq(op, 0);
  678. ret = devm_request_irq(pdev->dev, irq,
  679. mmp_pdma_int_handler, IRQF_DISABLED, "pdma", pdev);
  680. if (ret)
  681. return ret;
  682. }
  683. for (i = 0; i < dma_channels; i++) {
  684. irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
  685. ret = mmp_pdma_chan_init(pdev, i, irq);
  686. if (ret)
  687. return ret;
  688. }
  689. dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
  690. dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
  691. dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
  692. pdev->device.dev = &op->dev;
  693. pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
  694. pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
  695. pdev->device.device_tx_status = mmp_pdma_tx_status;
  696. pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
  697. pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
  698. pdev->device.device_issue_pending = mmp_pdma_issue_pending;
  699. pdev->device.device_control = mmp_pdma_control;
  700. pdev->device.copy_align = PDMA_ALIGNMENT;
  701. if (pdev->dev->coherent_dma_mask)
  702. dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
  703. else
  704. dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
  705. ret = dma_async_device_register(&pdev->device);
  706. if (ret) {
  707. dev_err(pdev->device.dev, "unable to register\n");
  708. return ret;
  709. }
  710. dev_info(pdev->device.dev, "initialized\n");
  711. return 0;
  712. }
  713. static const struct platform_device_id mmp_pdma_id_table[] = {
  714. { "mmp-pdma", },
  715. { },
  716. };
  717. static struct platform_driver mmp_pdma_driver = {
  718. .driver = {
  719. .name = "mmp-pdma",
  720. .owner = THIS_MODULE,
  721. .of_match_table = mmp_pdma_dt_ids,
  722. },
  723. .id_table = mmp_pdma_id_table,
  724. .probe = mmp_pdma_probe,
  725. .remove = mmp_pdma_remove,
  726. };
  727. module_platform_driver(mmp_pdma_driver);
  728. MODULE_DESCRIPTION("MARVELL MMP Periphera DMA Driver");
  729. MODULE_AUTHOR("Marvell International Ltd.");
  730. MODULE_LICENSE("GPL v2");