mmp_pdma.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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_dma.h>
  22. #include <linux/of.h>
  23. #include <linux/dma/mmp-pdma.h>
  24. #include "dmaengine.h"
  25. #define DCSR 0x0000
  26. #define DALGN 0x00a0
  27. #define DINT 0x00f0
  28. #define DDADR 0x0200
  29. #define DSADR(n) (0x0204 + ((n) << 4))
  30. #define DTADR(n) (0x0208 + ((n) << 4))
  31. #define DCMD 0x020c
  32. #define DCSR_RUN BIT(31) /* Run Bit (read / write) */
  33. #define DCSR_NODESC BIT(30) /* No-Descriptor Fetch (read / write) */
  34. #define DCSR_STOPIRQEN BIT(29) /* Stop Interrupt Enable (read / write) */
  35. #define DCSR_REQPEND BIT(8) /* Request Pending (read-only) */
  36. #define DCSR_STOPSTATE BIT(3) /* Stop State (read-only) */
  37. #define DCSR_ENDINTR BIT(2) /* End Interrupt (read / write) */
  38. #define DCSR_STARTINTR BIT(1) /* Start Interrupt (read / write) */
  39. #define DCSR_BUSERR BIT(0) /* Bus Error Interrupt (read / write) */
  40. #define DCSR_EORIRQEN BIT(28) /* End of Receive Interrupt Enable (R/W) */
  41. #define DCSR_EORJMPEN BIT(27) /* Jump to next descriptor on EOR */
  42. #define DCSR_EORSTOPEN BIT(26) /* STOP on an EOR */
  43. #define DCSR_SETCMPST BIT(25) /* Set Descriptor Compare Status */
  44. #define DCSR_CLRCMPST BIT(24) /* Clear Descriptor Compare Status */
  45. #define DCSR_CMPST BIT(10) /* The Descriptor Compare Status */
  46. #define DCSR_EORINTR BIT(9) /* The end of Receive */
  47. #define DRCMR(n) ((((n) < 64) ? 0x0100 : 0x1100) + (((n) & 0x3f) << 2))
  48. #define DRCMR_MAPVLD BIT(7) /* Map Valid (read / write) */
  49. #define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */
  50. #define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */
  51. #define DDADR_STOP BIT(0) /* Stop (read / write) */
  52. #define DCMD_INCSRCADDR BIT(31) /* Source Address Increment Setting. */
  53. #define DCMD_INCTRGADDR BIT(30) /* Target Address Increment Setting. */
  54. #define DCMD_FLOWSRC BIT(29) /* Flow Control by the source. */
  55. #define DCMD_FLOWTRG BIT(28) /* Flow Control by the target. */
  56. #define DCMD_STARTIRQEN BIT(22) /* Start Interrupt Enable */
  57. #define DCMD_ENDIRQEN BIT(21) /* End Interrupt Enable */
  58. #define DCMD_ENDIAN BIT(18) /* Device Endian-ness. */
  59. #define DCMD_BURST8 (1 << 16) /* 8 byte burst */
  60. #define DCMD_BURST16 (2 << 16) /* 16 byte burst */
  61. #define DCMD_BURST32 (3 << 16) /* 32 byte burst */
  62. #define DCMD_WIDTH1 (1 << 14) /* 1 byte width */
  63. #define DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */
  64. #define DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */
  65. #define DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */
  66. #define PDMA_MAX_DESC_BYTES DCMD_LENGTH
  67. struct mmp_pdma_desc_hw {
  68. u32 ddadr; /* Points to the next descriptor + flags */
  69. u32 dsadr; /* DSADR value for the current transfer */
  70. u32 dtadr; /* DTADR value for the current transfer */
  71. u32 dcmd; /* DCMD value for the current transfer */
  72. } __aligned(32);
  73. struct mmp_pdma_desc_sw {
  74. struct mmp_pdma_desc_hw desc;
  75. struct list_head node;
  76. struct list_head tx_list;
  77. struct dma_async_tx_descriptor async_tx;
  78. };
  79. struct mmp_pdma_phy;
  80. struct mmp_pdma_chan {
  81. struct device *dev;
  82. struct dma_chan chan;
  83. struct dma_async_tx_descriptor desc;
  84. struct mmp_pdma_phy *phy;
  85. enum dma_transfer_direction dir;
  86. struct mmp_pdma_desc_sw *cyclic_first; /* first desc_sw if channel
  87. * is in cyclic mode */
  88. /* channel's basic info */
  89. struct tasklet_struct tasklet;
  90. u32 dcmd;
  91. u32 drcmr;
  92. u32 dev_addr;
  93. /* list for desc */
  94. spinlock_t desc_lock; /* Descriptor list lock */
  95. struct list_head chain_pending; /* Link descriptors queue for pending */
  96. struct list_head chain_running; /* Link descriptors queue for running */
  97. bool idle; /* channel statue machine */
  98. bool byte_align;
  99. struct dma_pool *desc_pool; /* Descriptors pool */
  100. };
  101. struct mmp_pdma_phy {
  102. int idx;
  103. void __iomem *base;
  104. struct mmp_pdma_chan *vchan;
  105. };
  106. struct mmp_pdma_device {
  107. int dma_channels;
  108. void __iomem *base;
  109. struct device *dev;
  110. struct dma_device device;
  111. struct mmp_pdma_phy *phy;
  112. spinlock_t phy_lock; /* protect alloc/free phy channels */
  113. };
  114. #define tx_to_mmp_pdma_desc(tx) \
  115. container_of(tx, struct mmp_pdma_desc_sw, async_tx)
  116. #define to_mmp_pdma_desc(lh) \
  117. container_of(lh, struct mmp_pdma_desc_sw, node)
  118. #define to_mmp_pdma_chan(dchan) \
  119. container_of(dchan, struct mmp_pdma_chan, chan)
  120. #define to_mmp_pdma_dev(dmadev) \
  121. container_of(dmadev, struct mmp_pdma_device, device)
  122. static void set_desc(struct mmp_pdma_phy *phy, dma_addr_t addr)
  123. {
  124. u32 reg = (phy->idx << 4) + DDADR;
  125. writel(addr, phy->base + reg);
  126. }
  127. static void enable_chan(struct mmp_pdma_phy *phy)
  128. {
  129. u32 reg, dalgn;
  130. if (!phy->vchan)
  131. return;
  132. reg = DRCMR(phy->vchan->drcmr);
  133. writel(DRCMR_MAPVLD | phy->idx, phy->base + reg);
  134. dalgn = readl(phy->base + DALGN);
  135. if (phy->vchan->byte_align)
  136. dalgn |= 1 << phy->idx;
  137. else
  138. dalgn &= ~(1 << phy->idx);
  139. writel(dalgn, phy->base + DALGN);
  140. reg = (phy->idx << 2) + DCSR;
  141. writel(readl(phy->base + reg) | DCSR_RUN, phy->base + reg);
  142. }
  143. static void disable_chan(struct mmp_pdma_phy *phy)
  144. {
  145. u32 reg;
  146. if (!phy)
  147. return;
  148. reg = (phy->idx << 2) + DCSR;
  149. writel(readl(phy->base + reg) & ~DCSR_RUN, phy->base + reg);
  150. }
  151. static int clear_chan_irq(struct mmp_pdma_phy *phy)
  152. {
  153. u32 dcsr;
  154. u32 dint = readl(phy->base + DINT);
  155. u32 reg = (phy->idx << 2) + DCSR;
  156. if (!(dint & BIT(phy->idx)))
  157. return -EAGAIN;
  158. /* clear irq */
  159. dcsr = readl(phy->base + reg);
  160. writel(dcsr, phy->base + reg);
  161. if ((dcsr & DCSR_BUSERR) && (phy->vchan))
  162. dev_warn(phy->vchan->dev, "DCSR_BUSERR\n");
  163. return 0;
  164. }
  165. static irqreturn_t mmp_pdma_chan_handler(int irq, void *dev_id)
  166. {
  167. struct mmp_pdma_phy *phy = dev_id;
  168. if (clear_chan_irq(phy) != 0)
  169. return IRQ_NONE;
  170. tasklet_schedule(&phy->vchan->tasklet);
  171. return IRQ_HANDLED;
  172. }
  173. static irqreturn_t mmp_pdma_int_handler(int irq, void *dev_id)
  174. {
  175. struct mmp_pdma_device *pdev = dev_id;
  176. struct mmp_pdma_phy *phy;
  177. u32 dint = readl(pdev->base + DINT);
  178. int i, ret;
  179. int irq_num = 0;
  180. while (dint) {
  181. i = __ffs(dint);
  182. /* only handle interrupts belonging to pdma driver*/
  183. if (i >= pdev->dma_channels)
  184. break;
  185. dint &= (dint - 1);
  186. phy = &pdev->phy[i];
  187. ret = mmp_pdma_chan_handler(irq, phy);
  188. if (ret == IRQ_HANDLED)
  189. irq_num++;
  190. }
  191. if (irq_num)
  192. return IRQ_HANDLED;
  193. return IRQ_NONE;
  194. }
  195. /* lookup free phy channel as descending priority */
  196. static struct mmp_pdma_phy *lookup_phy(struct mmp_pdma_chan *pchan)
  197. {
  198. int prio, i;
  199. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  200. struct mmp_pdma_phy *phy, *found = NULL;
  201. unsigned long flags;
  202. /*
  203. * dma channel priorities
  204. * ch 0 - 3, 16 - 19 <--> (0)
  205. * ch 4 - 7, 20 - 23 <--> (1)
  206. * ch 8 - 11, 24 - 27 <--> (2)
  207. * ch 12 - 15, 28 - 31 <--> (3)
  208. */
  209. spin_lock_irqsave(&pdev->phy_lock, flags);
  210. for (prio = 0; prio <= ((pdev->dma_channels - 1) & 0xf) >> 2; prio++) {
  211. for (i = 0; i < pdev->dma_channels; i++) {
  212. if (prio != (i & 0xf) >> 2)
  213. continue;
  214. phy = &pdev->phy[i];
  215. if (!phy->vchan) {
  216. phy->vchan = pchan;
  217. found = phy;
  218. goto out_unlock;
  219. }
  220. }
  221. }
  222. out_unlock:
  223. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  224. return found;
  225. }
  226. static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
  227. {
  228. struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
  229. unsigned long flags;
  230. u32 reg;
  231. if (!pchan->phy)
  232. return;
  233. /* clear the channel mapping in DRCMR */
  234. reg = DRCMR(pchan->drcmr);
  235. writel(0, pchan->phy->base + reg);
  236. spin_lock_irqsave(&pdev->phy_lock, flags);
  237. pchan->phy->vchan = NULL;
  238. pchan->phy = NULL;
  239. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  240. }
  241. /**
  242. * start_pending_queue - transfer any pending transactions
  243. * pending list ==> running list
  244. */
  245. static void start_pending_queue(struct mmp_pdma_chan *chan)
  246. {
  247. struct mmp_pdma_desc_sw *desc;
  248. /* still in running, irq will start the pending list */
  249. if (!chan->idle) {
  250. dev_dbg(chan->dev, "DMA controller still busy\n");
  251. return;
  252. }
  253. if (list_empty(&chan->chain_pending)) {
  254. /* chance to re-fetch phy channel with higher prio */
  255. mmp_pdma_free_phy(chan);
  256. dev_dbg(chan->dev, "no pending list\n");
  257. return;
  258. }
  259. if (!chan->phy) {
  260. chan->phy = lookup_phy(chan);
  261. if (!chan->phy) {
  262. dev_dbg(chan->dev, "no free dma channel\n");
  263. return;
  264. }
  265. }
  266. /*
  267. * pending -> running
  268. * reintilize pending list
  269. */
  270. desc = list_first_entry(&chan->chain_pending,
  271. struct mmp_pdma_desc_sw, node);
  272. list_splice_tail_init(&chan->chain_pending, &chan->chain_running);
  273. /*
  274. * Program the descriptor's address into the DMA controller,
  275. * then start the DMA transaction
  276. */
  277. set_desc(chan->phy, desc->async_tx.phys);
  278. enable_chan(chan->phy);
  279. chan->idle = false;
  280. }
  281. /* desc->tx_list ==> pending list */
  282. static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx)
  283. {
  284. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan);
  285. struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx);
  286. struct mmp_pdma_desc_sw *child;
  287. unsigned long flags;
  288. dma_cookie_t cookie = -EBUSY;
  289. spin_lock_irqsave(&chan->desc_lock, flags);
  290. list_for_each_entry(child, &desc->tx_list, node) {
  291. cookie = dma_cookie_assign(&child->async_tx);
  292. }
  293. /* softly link to pending list - desc->tx_list ==> pending list */
  294. list_splice_tail_init(&desc->tx_list, &chan->chain_pending);
  295. spin_unlock_irqrestore(&chan->desc_lock, flags);
  296. return cookie;
  297. }
  298. static struct mmp_pdma_desc_sw *
  299. mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan)
  300. {
  301. struct mmp_pdma_desc_sw *desc;
  302. dma_addr_t pdesc;
  303. desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
  304. if (!desc) {
  305. dev_err(chan->dev, "out of memory for link descriptor\n");
  306. return NULL;
  307. }
  308. memset(desc, 0, sizeof(*desc));
  309. INIT_LIST_HEAD(&desc->tx_list);
  310. dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
  311. /* each desc has submit */
  312. desc->async_tx.tx_submit = mmp_pdma_tx_submit;
  313. desc->async_tx.phys = pdesc;
  314. return desc;
  315. }
  316. /**
  317. * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel.
  318. *
  319. * This function will create a dma pool for descriptor allocation.
  320. * Request irq only when channel is requested
  321. * Return - The number of allocated descriptors.
  322. */
  323. static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan)
  324. {
  325. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  326. if (chan->desc_pool)
  327. return 1;
  328. chan->desc_pool = dma_pool_create(dev_name(&dchan->dev->device),
  329. chan->dev,
  330. sizeof(struct mmp_pdma_desc_sw),
  331. __alignof__(struct mmp_pdma_desc_sw),
  332. 0);
  333. if (!chan->desc_pool) {
  334. dev_err(chan->dev, "unable to allocate descriptor pool\n");
  335. return -ENOMEM;
  336. }
  337. mmp_pdma_free_phy(chan);
  338. chan->idle = true;
  339. chan->dev_addr = 0;
  340. return 1;
  341. }
  342. static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan,
  343. struct list_head *list)
  344. {
  345. struct mmp_pdma_desc_sw *desc, *_desc;
  346. list_for_each_entry_safe(desc, _desc, list, node) {
  347. list_del(&desc->node);
  348. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  349. }
  350. }
  351. static void mmp_pdma_free_chan_resources(struct dma_chan *dchan)
  352. {
  353. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  354. unsigned long flags;
  355. spin_lock_irqsave(&chan->desc_lock, flags);
  356. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  357. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  358. spin_unlock_irqrestore(&chan->desc_lock, flags);
  359. dma_pool_destroy(chan->desc_pool);
  360. chan->desc_pool = NULL;
  361. chan->idle = true;
  362. chan->dev_addr = 0;
  363. mmp_pdma_free_phy(chan);
  364. return;
  365. }
  366. static struct dma_async_tx_descriptor *
  367. mmp_pdma_prep_memcpy(struct dma_chan *dchan,
  368. dma_addr_t dma_dst, dma_addr_t dma_src,
  369. size_t len, unsigned long flags)
  370. {
  371. struct mmp_pdma_chan *chan;
  372. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  373. size_t copy = 0;
  374. if (!dchan)
  375. return NULL;
  376. if (!len)
  377. return NULL;
  378. chan = to_mmp_pdma_chan(dchan);
  379. chan->byte_align = false;
  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. if (dma_src & 0x7 || dma_dst & 0x7)
  394. chan->byte_align = true;
  395. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy);
  396. new->desc.dsadr = dma_src;
  397. new->desc.dtadr = dma_dst;
  398. if (!first)
  399. first = new;
  400. else
  401. prev->desc.ddadr = new->async_tx.phys;
  402. new->async_tx.cookie = 0;
  403. async_tx_ack(&new->async_tx);
  404. prev = new;
  405. len -= copy;
  406. if (chan->dir == DMA_MEM_TO_DEV) {
  407. dma_src += copy;
  408. } else if (chan->dir == DMA_DEV_TO_MEM) {
  409. dma_dst += copy;
  410. } else if (chan->dir == DMA_MEM_TO_MEM) {
  411. dma_src += copy;
  412. dma_dst += copy;
  413. }
  414. /* Insert the link descriptor to the LD ring */
  415. list_add_tail(&new->node, &first->tx_list);
  416. } while (len);
  417. first->async_tx.flags = flags; /* client is in control of this ack */
  418. first->async_tx.cookie = -EBUSY;
  419. /* last desc and fire IRQ */
  420. new->desc.ddadr = DDADR_STOP;
  421. new->desc.dcmd |= DCMD_ENDIRQEN;
  422. chan->cyclic_first = NULL;
  423. return &first->async_tx;
  424. fail:
  425. if (first)
  426. mmp_pdma_free_desc_list(chan, &first->tx_list);
  427. return NULL;
  428. }
  429. static struct dma_async_tx_descriptor *
  430. mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  431. unsigned int sg_len, enum dma_transfer_direction dir,
  432. unsigned long flags, void *context)
  433. {
  434. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  435. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  436. size_t len, avail;
  437. struct scatterlist *sg;
  438. dma_addr_t addr;
  439. int i;
  440. if ((sgl == NULL) || (sg_len == 0))
  441. return NULL;
  442. chan->byte_align = false;
  443. for_each_sg(sgl, sg, sg_len, i) {
  444. addr = sg_dma_address(sg);
  445. avail = sg_dma_len(sgl);
  446. do {
  447. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  448. if (addr & 0x7)
  449. chan->byte_align = true;
  450. /* allocate and populate the descriptor */
  451. new = mmp_pdma_alloc_descriptor(chan);
  452. if (!new) {
  453. dev_err(chan->dev, "no memory for desc\n");
  454. goto fail;
  455. }
  456. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len);
  457. if (dir == DMA_MEM_TO_DEV) {
  458. new->desc.dsadr = addr;
  459. new->desc.dtadr = chan->dev_addr;
  460. } else {
  461. new->desc.dsadr = chan->dev_addr;
  462. new->desc.dtadr = addr;
  463. }
  464. if (!first)
  465. first = new;
  466. else
  467. prev->desc.ddadr = new->async_tx.phys;
  468. new->async_tx.cookie = 0;
  469. async_tx_ack(&new->async_tx);
  470. prev = new;
  471. /* Insert the link descriptor to the LD ring */
  472. list_add_tail(&new->node, &first->tx_list);
  473. /* update metadata */
  474. addr += len;
  475. avail -= len;
  476. } while (avail);
  477. }
  478. first->async_tx.cookie = -EBUSY;
  479. first->async_tx.flags = flags;
  480. /* last desc and fire IRQ */
  481. new->desc.ddadr = DDADR_STOP;
  482. new->desc.dcmd |= DCMD_ENDIRQEN;
  483. chan->dir = dir;
  484. chan->cyclic_first = NULL;
  485. return &first->async_tx;
  486. fail:
  487. if (first)
  488. mmp_pdma_free_desc_list(chan, &first->tx_list);
  489. return NULL;
  490. }
  491. static struct dma_async_tx_descriptor *
  492. mmp_pdma_prep_dma_cyclic(struct dma_chan *dchan,
  493. dma_addr_t buf_addr, size_t len, size_t period_len,
  494. enum dma_transfer_direction direction,
  495. unsigned long flags)
  496. {
  497. struct mmp_pdma_chan *chan;
  498. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  499. dma_addr_t dma_src, dma_dst;
  500. if (!dchan || !len || !period_len)
  501. return NULL;
  502. /* the buffer length must be a multiple of period_len */
  503. if (len % period_len != 0)
  504. return NULL;
  505. if (period_len > PDMA_MAX_DESC_BYTES)
  506. return NULL;
  507. chan = to_mmp_pdma_chan(dchan);
  508. switch (direction) {
  509. case DMA_MEM_TO_DEV:
  510. dma_src = buf_addr;
  511. dma_dst = chan->dev_addr;
  512. break;
  513. case DMA_DEV_TO_MEM:
  514. dma_dst = buf_addr;
  515. dma_src = chan->dev_addr;
  516. break;
  517. default:
  518. dev_err(chan->dev, "Unsupported direction for cyclic DMA\n");
  519. return NULL;
  520. }
  521. chan->dir = direction;
  522. do {
  523. /* Allocate the link descriptor from DMA pool */
  524. new = mmp_pdma_alloc_descriptor(chan);
  525. if (!new) {
  526. dev_err(chan->dev, "no memory for desc\n");
  527. goto fail;
  528. }
  529. new->desc.dcmd = (chan->dcmd | DCMD_ENDIRQEN |
  530. (DCMD_LENGTH & period_len));
  531. new->desc.dsadr = dma_src;
  532. new->desc.dtadr = dma_dst;
  533. if (!first)
  534. first = new;
  535. else
  536. prev->desc.ddadr = new->async_tx.phys;
  537. new->async_tx.cookie = 0;
  538. async_tx_ack(&new->async_tx);
  539. prev = new;
  540. len -= period_len;
  541. if (chan->dir == DMA_MEM_TO_DEV)
  542. dma_src += period_len;
  543. else
  544. dma_dst += period_len;
  545. /* Insert the link descriptor to the LD ring */
  546. list_add_tail(&new->node, &first->tx_list);
  547. } while (len);
  548. first->async_tx.flags = flags; /* client is in control of this ack */
  549. first->async_tx.cookie = -EBUSY;
  550. /* make the cyclic link */
  551. new->desc.ddadr = first->async_tx.phys;
  552. chan->cyclic_first = first;
  553. return &first->async_tx;
  554. fail:
  555. if (first)
  556. mmp_pdma_free_desc_list(chan, &first->tx_list);
  557. return NULL;
  558. }
  559. static int mmp_pdma_config(struct dma_chan *dchan,
  560. struct dma_slave_config *cfg)
  561. {
  562. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  563. u32 maxburst = 0, addr = 0;
  564. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  565. if (!dchan)
  566. return -EINVAL;
  567. if (cfg->direction == DMA_DEV_TO_MEM) {
  568. chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
  569. maxburst = cfg->src_maxburst;
  570. width = cfg->src_addr_width;
  571. addr = cfg->src_addr;
  572. } else if (cfg->direction == DMA_MEM_TO_DEV) {
  573. chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
  574. maxburst = cfg->dst_maxburst;
  575. width = cfg->dst_addr_width;
  576. addr = cfg->dst_addr;
  577. }
  578. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  579. chan->dcmd |= DCMD_WIDTH1;
  580. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  581. chan->dcmd |= DCMD_WIDTH2;
  582. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  583. chan->dcmd |= DCMD_WIDTH4;
  584. if (maxburst == 8)
  585. chan->dcmd |= DCMD_BURST8;
  586. else if (maxburst == 16)
  587. chan->dcmd |= DCMD_BURST16;
  588. else if (maxburst == 32)
  589. chan->dcmd |= DCMD_BURST32;
  590. chan->dir = cfg->direction;
  591. chan->dev_addr = addr;
  592. /* FIXME: drivers should be ported over to use the filter
  593. * function. Once that's done, the following two lines can
  594. * be removed.
  595. */
  596. if (cfg->slave_id)
  597. chan->drcmr = cfg->slave_id;
  598. return 0;
  599. }
  600. static int mmp_pdma_terminate_all(struct dma_chan *dchan)
  601. {
  602. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  603. unsigned long flags;
  604. if (!dchan)
  605. return -EINVAL;
  606. disable_chan(chan->phy);
  607. mmp_pdma_free_phy(chan);
  608. spin_lock_irqsave(&chan->desc_lock, flags);
  609. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  610. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  611. spin_unlock_irqrestore(&chan->desc_lock, flags);
  612. chan->idle = true;
  613. return 0;
  614. }
  615. static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
  616. dma_cookie_t cookie)
  617. {
  618. struct mmp_pdma_desc_sw *sw;
  619. u32 curr, residue = 0;
  620. bool passed = false;
  621. bool cyclic = chan->cyclic_first != NULL;
  622. /*
  623. * If the channel does not have a phy pointer anymore, it has already
  624. * been completed. Therefore, its residue is 0.
  625. */
  626. if (!chan->phy)
  627. return 0;
  628. if (chan->dir == DMA_DEV_TO_MEM)
  629. curr = readl(chan->phy->base + DTADR(chan->phy->idx));
  630. else
  631. curr = readl(chan->phy->base + DSADR(chan->phy->idx));
  632. list_for_each_entry(sw, &chan->chain_running, node) {
  633. u32 start, end, len;
  634. if (chan->dir == DMA_DEV_TO_MEM)
  635. start = sw->desc.dtadr;
  636. else
  637. start = sw->desc.dsadr;
  638. len = sw->desc.dcmd & DCMD_LENGTH;
  639. end = start + len;
  640. /*
  641. * 'passed' will be latched once we found the descriptor which
  642. * lies inside the boundaries of the curr pointer. All
  643. * descriptors that occur in the list _after_ we found that
  644. * partially handled descriptor are still to be processed and
  645. * are hence added to the residual bytes counter.
  646. */
  647. if (passed) {
  648. residue += len;
  649. } else if (curr >= start && curr <= end) {
  650. residue += end - curr;
  651. passed = true;
  652. }
  653. /*
  654. * Descriptors that have the ENDIRQEN bit set mark the end of a
  655. * transaction chain, and the cookie assigned with it has been
  656. * returned previously from mmp_pdma_tx_submit().
  657. *
  658. * In case we have multiple transactions in the running chain,
  659. * and the cookie does not match the one the user asked us
  660. * about, reset the state variables and start over.
  661. *
  662. * This logic does not apply to cyclic transactions, where all
  663. * descriptors have the ENDIRQEN bit set, and for which we
  664. * can't have multiple transactions on one channel anyway.
  665. */
  666. if (cyclic || !(sw->desc.dcmd & DCMD_ENDIRQEN))
  667. continue;
  668. if (sw->async_tx.cookie == cookie) {
  669. return residue;
  670. } else {
  671. residue = 0;
  672. passed = false;
  673. }
  674. }
  675. /* We should only get here in case of cyclic transactions */
  676. return residue;
  677. }
  678. static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
  679. dma_cookie_t cookie,
  680. struct dma_tx_state *txstate)
  681. {
  682. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  683. enum dma_status ret;
  684. ret = dma_cookie_status(dchan, cookie, txstate);
  685. if (likely(ret != DMA_ERROR))
  686. dma_set_residue(txstate, mmp_pdma_residue(chan, cookie));
  687. return ret;
  688. }
  689. /**
  690. * mmp_pdma_issue_pending - Issue the DMA start command
  691. * pending list ==> running list
  692. */
  693. static void mmp_pdma_issue_pending(struct dma_chan *dchan)
  694. {
  695. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  696. unsigned long flags;
  697. spin_lock_irqsave(&chan->desc_lock, flags);
  698. start_pending_queue(chan);
  699. spin_unlock_irqrestore(&chan->desc_lock, flags);
  700. }
  701. /*
  702. * dma_do_tasklet
  703. * Do call back
  704. * Start pending list
  705. */
  706. static void dma_do_tasklet(unsigned long data)
  707. {
  708. struct mmp_pdma_chan *chan = (struct mmp_pdma_chan *)data;
  709. struct mmp_pdma_desc_sw *desc, *_desc;
  710. LIST_HEAD(chain_cleanup);
  711. unsigned long flags;
  712. if (chan->cyclic_first) {
  713. dma_async_tx_callback cb = NULL;
  714. void *cb_data = NULL;
  715. spin_lock_irqsave(&chan->desc_lock, flags);
  716. desc = chan->cyclic_first;
  717. cb = desc->async_tx.callback;
  718. cb_data = desc->async_tx.callback_param;
  719. spin_unlock_irqrestore(&chan->desc_lock, flags);
  720. if (cb)
  721. cb(cb_data);
  722. return;
  723. }
  724. /* submit pending list; callback for each desc; free desc */
  725. spin_lock_irqsave(&chan->desc_lock, flags);
  726. list_for_each_entry_safe(desc, _desc, &chan->chain_running, node) {
  727. /*
  728. * move the descriptors to a temporary list so we can drop
  729. * the lock during the entire cleanup operation
  730. */
  731. list_move(&desc->node, &chain_cleanup);
  732. /*
  733. * Look for the first list entry which has the ENDIRQEN flag
  734. * set. That is the descriptor we got an interrupt for, so
  735. * complete that transaction and its cookie.
  736. */
  737. if (desc->desc.dcmd & DCMD_ENDIRQEN) {
  738. dma_cookie_t cookie = desc->async_tx.cookie;
  739. dma_cookie_complete(&desc->async_tx);
  740. dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
  741. break;
  742. }
  743. }
  744. /*
  745. * The hardware is idle and ready for more when the
  746. * chain_running list is empty.
  747. */
  748. chan->idle = list_empty(&chan->chain_running);
  749. /* Start any pending transactions automatically */
  750. start_pending_queue(chan);
  751. spin_unlock_irqrestore(&chan->desc_lock, flags);
  752. /* Run the callback for each descriptor, in order */
  753. list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
  754. struct dma_async_tx_descriptor *txd = &desc->async_tx;
  755. /* Remove from the list of transactions */
  756. list_del(&desc->node);
  757. /* Run the link descriptor callback function */
  758. if (txd->callback)
  759. txd->callback(txd->callback_param);
  760. dma_pool_free(chan->desc_pool, desc, txd->phys);
  761. }
  762. }
  763. static int mmp_pdma_remove(struct platform_device *op)
  764. {
  765. struct mmp_pdma_device *pdev = platform_get_drvdata(op);
  766. dma_async_device_unregister(&pdev->device);
  767. return 0;
  768. }
  769. static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev, int idx, int irq)
  770. {
  771. struct mmp_pdma_phy *phy = &pdev->phy[idx];
  772. struct mmp_pdma_chan *chan;
  773. int ret;
  774. chan = devm_kzalloc(pdev->dev, sizeof(*chan), GFP_KERNEL);
  775. if (chan == NULL)
  776. return -ENOMEM;
  777. phy->idx = idx;
  778. phy->base = pdev->base;
  779. if (irq) {
  780. ret = devm_request_irq(pdev->dev, irq, mmp_pdma_chan_handler,
  781. IRQF_SHARED, "pdma", phy);
  782. if (ret) {
  783. dev_err(pdev->dev, "channel request irq fail!\n");
  784. return ret;
  785. }
  786. }
  787. spin_lock_init(&chan->desc_lock);
  788. chan->dev = pdev->dev;
  789. chan->chan.device = &pdev->device;
  790. tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
  791. INIT_LIST_HEAD(&chan->chain_pending);
  792. INIT_LIST_HEAD(&chan->chain_running);
  793. /* register virt channel to dma engine */
  794. list_add_tail(&chan->chan.device_node, &pdev->device.channels);
  795. return 0;
  796. }
  797. static const struct of_device_id mmp_pdma_dt_ids[] = {
  798. { .compatible = "marvell,pdma-1.0", },
  799. {}
  800. };
  801. MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
  802. static struct dma_chan *mmp_pdma_dma_xlate(struct of_phandle_args *dma_spec,
  803. struct of_dma *ofdma)
  804. {
  805. struct mmp_pdma_device *d = ofdma->of_dma_data;
  806. struct dma_chan *chan;
  807. chan = dma_get_any_slave_channel(&d->device);
  808. if (!chan)
  809. return NULL;
  810. to_mmp_pdma_chan(chan)->drcmr = dma_spec->args[0];
  811. return chan;
  812. }
  813. static int mmp_pdma_probe(struct platform_device *op)
  814. {
  815. struct mmp_pdma_device *pdev;
  816. const struct of_device_id *of_id;
  817. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  818. struct resource *iores;
  819. int i, ret, irq = 0;
  820. int dma_channels = 0, irq_num = 0;
  821. const enum dma_slave_buswidth widths =
  822. DMA_SLAVE_BUSWIDTH_1_BYTE | DMA_SLAVE_BUSWIDTH_2_BYTES |
  823. DMA_SLAVE_BUSWIDTH_4_BYTES;
  824. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  825. if (!pdev)
  826. return -ENOMEM;
  827. pdev->dev = &op->dev;
  828. spin_lock_init(&pdev->phy_lock);
  829. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  830. pdev->base = devm_ioremap_resource(pdev->dev, iores);
  831. if (IS_ERR(pdev->base))
  832. return PTR_ERR(pdev->base);
  833. of_id = of_match_device(mmp_pdma_dt_ids, pdev->dev);
  834. if (of_id)
  835. of_property_read_u32(pdev->dev->of_node, "#dma-channels",
  836. &dma_channels);
  837. else if (pdata && pdata->dma_channels)
  838. dma_channels = pdata->dma_channels;
  839. else
  840. dma_channels = 32; /* default 32 channel */
  841. pdev->dma_channels = dma_channels;
  842. for (i = 0; i < dma_channels; i++) {
  843. if (platform_get_irq(op, i) > 0)
  844. irq_num++;
  845. }
  846. pdev->phy = devm_kcalloc(pdev->dev, dma_channels, sizeof(*pdev->phy),
  847. GFP_KERNEL);
  848. if (pdev->phy == NULL)
  849. return -ENOMEM;
  850. INIT_LIST_HEAD(&pdev->device.channels);
  851. if (irq_num != dma_channels) {
  852. /* all chan share one irq, demux inside */
  853. irq = platform_get_irq(op, 0);
  854. ret = devm_request_irq(pdev->dev, irq, mmp_pdma_int_handler,
  855. IRQF_SHARED, "pdma", pdev);
  856. if (ret)
  857. return ret;
  858. }
  859. for (i = 0; i < dma_channels; i++) {
  860. irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
  861. ret = mmp_pdma_chan_init(pdev, i, irq);
  862. if (ret)
  863. return ret;
  864. }
  865. dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
  866. dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
  867. dma_cap_set(DMA_CYCLIC, pdev->device.cap_mask);
  868. dma_cap_set(DMA_PRIVATE, pdev->device.cap_mask);
  869. pdev->device.dev = &op->dev;
  870. pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
  871. pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
  872. pdev->device.device_tx_status = mmp_pdma_tx_status;
  873. pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
  874. pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
  875. pdev->device.device_prep_dma_cyclic = mmp_pdma_prep_dma_cyclic;
  876. pdev->device.device_issue_pending = mmp_pdma_issue_pending;
  877. pdev->device.device_config = mmp_pdma_config;
  878. pdev->device.device_terminate_all = mmp_pdma_terminate_all;
  879. pdev->device.copy_align = DMAENGINE_ALIGN_8_BYTES;
  880. pdev->device.src_addr_widths = widths;
  881. pdev->device.dst_addr_widths = widths;
  882. pdev->device.directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  883. pdev->device.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  884. if (pdev->dev->coherent_dma_mask)
  885. dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
  886. else
  887. dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
  888. ret = dma_async_device_register(&pdev->device);
  889. if (ret) {
  890. dev_err(pdev->device.dev, "unable to register\n");
  891. return ret;
  892. }
  893. if (op->dev.of_node) {
  894. /* Device-tree DMA controller registration */
  895. ret = of_dma_controller_register(op->dev.of_node,
  896. mmp_pdma_dma_xlate, pdev);
  897. if (ret < 0) {
  898. dev_err(&op->dev, "of_dma_controller_register failed\n");
  899. return ret;
  900. }
  901. }
  902. platform_set_drvdata(op, pdev);
  903. dev_info(pdev->device.dev, "initialized %d channels\n", dma_channels);
  904. return 0;
  905. }
  906. static const struct platform_device_id mmp_pdma_id_table[] = {
  907. { "mmp-pdma", },
  908. { },
  909. };
  910. static struct platform_driver mmp_pdma_driver = {
  911. .driver = {
  912. .name = "mmp-pdma",
  913. .of_match_table = mmp_pdma_dt_ids,
  914. },
  915. .id_table = mmp_pdma_id_table,
  916. .probe = mmp_pdma_probe,
  917. .remove = mmp_pdma_remove,
  918. };
  919. bool mmp_pdma_filter_fn(struct dma_chan *chan, void *param)
  920. {
  921. struct mmp_pdma_chan *c = to_mmp_pdma_chan(chan);
  922. if (chan->device->dev->driver != &mmp_pdma_driver.driver)
  923. return false;
  924. c->drcmr = *(unsigned int *)param;
  925. return true;
  926. }
  927. EXPORT_SYMBOL_GPL(mmp_pdma_filter_fn);
  928. module_platform_driver(mmp_pdma_driver);
  929. MODULE_DESCRIPTION("MARVELL MMP Peripheral DMA Driver");
  930. MODULE_AUTHOR("Marvell International Ltd.");
  931. MODULE_LICENSE("GPL v2");