mmp_pdma.c 22 KB

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