mmp_pdma.c 22 KB

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