mmp_pdma.c 22 KB

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