mmp_pdma.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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_zalloc(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. INIT_LIST_HEAD(&desc->tx_list);
  309. dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
  310. /* each desc has submit */
  311. desc->async_tx.tx_submit = mmp_pdma_tx_submit;
  312. desc->async_tx.phys = pdesc;
  313. return desc;
  314. }
  315. /**
  316. * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel.
  317. *
  318. * This function will create a dma pool for descriptor allocation.
  319. * Request irq only when channel is requested
  320. * Return - The number of allocated descriptors.
  321. */
  322. static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan)
  323. {
  324. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  325. if (chan->desc_pool)
  326. return 1;
  327. chan->desc_pool = dma_pool_create(dev_name(&dchan->dev->device),
  328. chan->dev,
  329. sizeof(struct mmp_pdma_desc_sw),
  330. __alignof__(struct mmp_pdma_desc_sw),
  331. 0);
  332. if (!chan->desc_pool) {
  333. dev_err(chan->dev, "unable to allocate descriptor pool\n");
  334. return -ENOMEM;
  335. }
  336. mmp_pdma_free_phy(chan);
  337. chan->idle = true;
  338. chan->dev_addr = 0;
  339. return 1;
  340. }
  341. static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan,
  342. struct list_head *list)
  343. {
  344. struct mmp_pdma_desc_sw *desc, *_desc;
  345. list_for_each_entry_safe(desc, _desc, list, node) {
  346. list_del(&desc->node);
  347. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  348. }
  349. }
  350. static void mmp_pdma_free_chan_resources(struct dma_chan *dchan)
  351. {
  352. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  353. unsigned long flags;
  354. spin_lock_irqsave(&chan->desc_lock, flags);
  355. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  356. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  357. spin_unlock_irqrestore(&chan->desc_lock, flags);
  358. dma_pool_destroy(chan->desc_pool);
  359. chan->desc_pool = NULL;
  360. chan->idle = true;
  361. chan->dev_addr = 0;
  362. mmp_pdma_free_phy(chan);
  363. return;
  364. }
  365. static struct dma_async_tx_descriptor *
  366. mmp_pdma_prep_memcpy(struct dma_chan *dchan,
  367. dma_addr_t dma_dst, dma_addr_t dma_src,
  368. size_t len, unsigned long flags)
  369. {
  370. struct mmp_pdma_chan *chan;
  371. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  372. size_t copy = 0;
  373. if (!dchan)
  374. return NULL;
  375. if (!len)
  376. return NULL;
  377. chan = to_mmp_pdma_chan(dchan);
  378. chan->byte_align = false;
  379. if (!chan->dir) {
  380. chan->dir = DMA_MEM_TO_MEM;
  381. chan->dcmd = DCMD_INCTRGADDR | DCMD_INCSRCADDR;
  382. chan->dcmd |= DCMD_BURST32;
  383. }
  384. do {
  385. /* Allocate the link descriptor from DMA pool */
  386. new = mmp_pdma_alloc_descriptor(chan);
  387. if (!new) {
  388. dev_err(chan->dev, "no memory for desc\n");
  389. goto fail;
  390. }
  391. copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
  392. if (dma_src & 0x7 || dma_dst & 0x7)
  393. chan->byte_align = true;
  394. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy);
  395. new->desc.dsadr = dma_src;
  396. new->desc.dtadr = dma_dst;
  397. if (!first)
  398. first = new;
  399. else
  400. prev->desc.ddadr = new->async_tx.phys;
  401. new->async_tx.cookie = 0;
  402. async_tx_ack(&new->async_tx);
  403. prev = new;
  404. len -= copy;
  405. if (chan->dir == DMA_MEM_TO_DEV) {
  406. dma_src += copy;
  407. } else if (chan->dir == DMA_DEV_TO_MEM) {
  408. dma_dst += copy;
  409. } else if (chan->dir == DMA_MEM_TO_MEM) {
  410. dma_src += copy;
  411. dma_dst += copy;
  412. }
  413. /* Insert the link descriptor to the LD ring */
  414. list_add_tail(&new->node, &first->tx_list);
  415. } while (len);
  416. first->async_tx.flags = flags; /* client is in control of this ack */
  417. first->async_tx.cookie = -EBUSY;
  418. /* last desc and fire IRQ */
  419. new->desc.ddadr = DDADR_STOP;
  420. new->desc.dcmd |= DCMD_ENDIRQEN;
  421. chan->cyclic_first = NULL;
  422. return &first->async_tx;
  423. fail:
  424. if (first)
  425. mmp_pdma_free_desc_list(chan, &first->tx_list);
  426. return NULL;
  427. }
  428. static struct dma_async_tx_descriptor *
  429. mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  430. unsigned int sg_len, enum dma_transfer_direction dir,
  431. unsigned long flags, void *context)
  432. {
  433. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  434. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  435. size_t len, avail;
  436. struct scatterlist *sg;
  437. dma_addr_t addr;
  438. int i;
  439. if ((sgl == NULL) || (sg_len == 0))
  440. return NULL;
  441. chan->byte_align = false;
  442. for_each_sg(sgl, sg, sg_len, i) {
  443. addr = sg_dma_address(sg);
  444. avail = sg_dma_len(sgl);
  445. do {
  446. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  447. if (addr & 0x7)
  448. chan->byte_align = true;
  449. /* allocate and populate the descriptor */
  450. new = mmp_pdma_alloc_descriptor(chan);
  451. if (!new) {
  452. dev_err(chan->dev, "no memory for desc\n");
  453. goto fail;
  454. }
  455. new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len);
  456. if (dir == DMA_MEM_TO_DEV) {
  457. new->desc.dsadr = addr;
  458. new->desc.dtadr = chan->dev_addr;
  459. } else {
  460. new->desc.dsadr = chan->dev_addr;
  461. new->desc.dtadr = addr;
  462. }
  463. if (!first)
  464. first = new;
  465. else
  466. prev->desc.ddadr = new->async_tx.phys;
  467. new->async_tx.cookie = 0;
  468. async_tx_ack(&new->async_tx);
  469. prev = new;
  470. /* Insert the link descriptor to the LD ring */
  471. list_add_tail(&new->node, &first->tx_list);
  472. /* update metadata */
  473. addr += len;
  474. avail -= len;
  475. } while (avail);
  476. }
  477. first->async_tx.cookie = -EBUSY;
  478. first->async_tx.flags = flags;
  479. /* last desc and fire IRQ */
  480. new->desc.ddadr = DDADR_STOP;
  481. new->desc.dcmd |= DCMD_ENDIRQEN;
  482. chan->dir = dir;
  483. chan->cyclic_first = NULL;
  484. return &first->async_tx;
  485. fail:
  486. if (first)
  487. mmp_pdma_free_desc_list(chan, &first->tx_list);
  488. return NULL;
  489. }
  490. static struct dma_async_tx_descriptor *
  491. mmp_pdma_prep_dma_cyclic(struct dma_chan *dchan,
  492. dma_addr_t buf_addr, size_t len, size_t period_len,
  493. enum dma_transfer_direction direction,
  494. unsigned long flags)
  495. {
  496. struct mmp_pdma_chan *chan;
  497. struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
  498. dma_addr_t dma_src, dma_dst;
  499. if (!dchan || !len || !period_len)
  500. return NULL;
  501. /* the buffer length must be a multiple of period_len */
  502. if (len % period_len != 0)
  503. return NULL;
  504. if (period_len > PDMA_MAX_DESC_BYTES)
  505. return NULL;
  506. chan = to_mmp_pdma_chan(dchan);
  507. switch (direction) {
  508. case DMA_MEM_TO_DEV:
  509. dma_src = buf_addr;
  510. dma_dst = chan->dev_addr;
  511. break;
  512. case DMA_DEV_TO_MEM:
  513. dma_dst = buf_addr;
  514. dma_src = chan->dev_addr;
  515. break;
  516. default:
  517. dev_err(chan->dev, "Unsupported direction for cyclic DMA\n");
  518. return NULL;
  519. }
  520. chan->dir = direction;
  521. do {
  522. /* Allocate the link descriptor from DMA pool */
  523. new = mmp_pdma_alloc_descriptor(chan);
  524. if (!new) {
  525. dev_err(chan->dev, "no memory for desc\n");
  526. goto fail;
  527. }
  528. new->desc.dcmd = (chan->dcmd | DCMD_ENDIRQEN |
  529. (DCMD_LENGTH & period_len));
  530. new->desc.dsadr = dma_src;
  531. new->desc.dtadr = dma_dst;
  532. if (!first)
  533. first = new;
  534. else
  535. prev->desc.ddadr = new->async_tx.phys;
  536. new->async_tx.cookie = 0;
  537. async_tx_ack(&new->async_tx);
  538. prev = new;
  539. len -= period_len;
  540. if (chan->dir == DMA_MEM_TO_DEV)
  541. dma_src += period_len;
  542. else
  543. dma_dst += period_len;
  544. /* Insert the link descriptor to the LD ring */
  545. list_add_tail(&new->node, &first->tx_list);
  546. } while (len);
  547. first->async_tx.flags = flags; /* client is in control of this ack */
  548. first->async_tx.cookie = -EBUSY;
  549. /* make the cyclic link */
  550. new->desc.ddadr = first->async_tx.phys;
  551. chan->cyclic_first = first;
  552. return &first->async_tx;
  553. fail:
  554. if (first)
  555. mmp_pdma_free_desc_list(chan, &first->tx_list);
  556. return NULL;
  557. }
  558. static int mmp_pdma_config(struct dma_chan *dchan,
  559. struct dma_slave_config *cfg)
  560. {
  561. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  562. u32 maxburst = 0, addr = 0;
  563. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  564. if (!dchan)
  565. return -EINVAL;
  566. if (cfg->direction == DMA_DEV_TO_MEM) {
  567. chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
  568. maxburst = cfg->src_maxburst;
  569. width = cfg->src_addr_width;
  570. addr = cfg->src_addr;
  571. } else if (cfg->direction == DMA_MEM_TO_DEV) {
  572. chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
  573. maxburst = cfg->dst_maxburst;
  574. width = cfg->dst_addr_width;
  575. addr = cfg->dst_addr;
  576. }
  577. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  578. chan->dcmd |= DCMD_WIDTH1;
  579. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  580. chan->dcmd |= DCMD_WIDTH2;
  581. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  582. chan->dcmd |= DCMD_WIDTH4;
  583. if (maxburst == 8)
  584. chan->dcmd |= DCMD_BURST8;
  585. else if (maxburst == 16)
  586. chan->dcmd |= DCMD_BURST16;
  587. else if (maxburst == 32)
  588. chan->dcmd |= DCMD_BURST32;
  589. chan->dir = cfg->direction;
  590. chan->dev_addr = addr;
  591. /* FIXME: drivers should be ported over to use the filter
  592. * function. Once that's done, the following two lines can
  593. * be removed.
  594. */
  595. if (cfg->slave_id)
  596. chan->drcmr = cfg->slave_id;
  597. return 0;
  598. }
  599. static int mmp_pdma_terminate_all(struct dma_chan *dchan)
  600. {
  601. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  602. unsigned long flags;
  603. if (!dchan)
  604. return -EINVAL;
  605. disable_chan(chan->phy);
  606. mmp_pdma_free_phy(chan);
  607. spin_lock_irqsave(&chan->desc_lock, flags);
  608. mmp_pdma_free_desc_list(chan, &chan->chain_pending);
  609. mmp_pdma_free_desc_list(chan, &chan->chain_running);
  610. spin_unlock_irqrestore(&chan->desc_lock, flags);
  611. chan->idle = true;
  612. return 0;
  613. }
  614. static unsigned int mmp_pdma_residue(struct mmp_pdma_chan *chan,
  615. dma_cookie_t cookie)
  616. {
  617. struct mmp_pdma_desc_sw *sw;
  618. u32 curr, residue = 0;
  619. bool passed = false;
  620. bool cyclic = chan->cyclic_first != NULL;
  621. /*
  622. * If the channel does not have a phy pointer anymore, it has already
  623. * been completed. Therefore, its residue is 0.
  624. */
  625. if (!chan->phy)
  626. return 0;
  627. if (chan->dir == DMA_DEV_TO_MEM)
  628. curr = readl(chan->phy->base + DTADR(chan->phy->idx));
  629. else
  630. curr = readl(chan->phy->base + DSADR(chan->phy->idx));
  631. list_for_each_entry(sw, &chan->chain_running, node) {
  632. u32 start, end, len;
  633. if (chan->dir == DMA_DEV_TO_MEM)
  634. start = sw->desc.dtadr;
  635. else
  636. start = sw->desc.dsadr;
  637. len = sw->desc.dcmd & DCMD_LENGTH;
  638. end = start + len;
  639. /*
  640. * 'passed' will be latched once we found the descriptor which
  641. * lies inside the boundaries of the curr pointer. All
  642. * descriptors that occur in the list _after_ we found that
  643. * partially handled descriptor are still to be processed and
  644. * are hence added to the residual bytes counter.
  645. */
  646. if (passed) {
  647. residue += len;
  648. } else if (curr >= start && curr <= end) {
  649. residue += end - curr;
  650. passed = true;
  651. }
  652. /*
  653. * Descriptors that have the ENDIRQEN bit set mark the end of a
  654. * transaction chain, and the cookie assigned with it has been
  655. * returned previously from mmp_pdma_tx_submit().
  656. *
  657. * In case we have multiple transactions in the running chain,
  658. * and the cookie does not match the one the user asked us
  659. * about, reset the state variables and start over.
  660. *
  661. * This logic does not apply to cyclic transactions, where all
  662. * descriptors have the ENDIRQEN bit set, and for which we
  663. * can't have multiple transactions on one channel anyway.
  664. */
  665. if (cyclic || !(sw->desc.dcmd & DCMD_ENDIRQEN))
  666. continue;
  667. if (sw->async_tx.cookie == cookie) {
  668. return residue;
  669. } else {
  670. residue = 0;
  671. passed = false;
  672. }
  673. }
  674. /* We should only get here in case of cyclic transactions */
  675. return residue;
  676. }
  677. static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
  678. dma_cookie_t cookie,
  679. struct dma_tx_state *txstate)
  680. {
  681. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  682. enum dma_status ret;
  683. ret = dma_cookie_status(dchan, cookie, txstate);
  684. if (likely(ret != DMA_ERROR))
  685. dma_set_residue(txstate, mmp_pdma_residue(chan, cookie));
  686. return ret;
  687. }
  688. /**
  689. * mmp_pdma_issue_pending - Issue the DMA start command
  690. * pending list ==> running list
  691. */
  692. static void mmp_pdma_issue_pending(struct dma_chan *dchan)
  693. {
  694. struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
  695. unsigned long flags;
  696. spin_lock_irqsave(&chan->desc_lock, flags);
  697. start_pending_queue(chan);
  698. spin_unlock_irqrestore(&chan->desc_lock, flags);
  699. }
  700. /*
  701. * dma_do_tasklet
  702. * Do call back
  703. * Start pending list
  704. */
  705. static void dma_do_tasklet(unsigned long data)
  706. {
  707. struct mmp_pdma_chan *chan = (struct mmp_pdma_chan *)data;
  708. struct mmp_pdma_desc_sw *desc, *_desc;
  709. LIST_HEAD(chain_cleanup);
  710. unsigned long flags;
  711. struct dmaengine_desc_callback cb;
  712. if (chan->cyclic_first) {
  713. spin_lock_irqsave(&chan->desc_lock, flags);
  714. desc = chan->cyclic_first;
  715. dmaengine_desc_get_callback(&desc->async_tx, &cb);
  716. spin_unlock_irqrestore(&chan->desc_lock, flags);
  717. dmaengine_desc_callback_invoke(&cb, NULL);
  718. return;
  719. }
  720. /* submit pending list; callback for each desc; free desc */
  721. spin_lock_irqsave(&chan->desc_lock, flags);
  722. list_for_each_entry_safe(desc, _desc, &chan->chain_running, node) {
  723. /*
  724. * move the descriptors to a temporary list so we can drop
  725. * the lock during the entire cleanup operation
  726. */
  727. list_move(&desc->node, &chain_cleanup);
  728. /*
  729. * Look for the first list entry which has the ENDIRQEN flag
  730. * set. That is the descriptor we got an interrupt for, so
  731. * complete that transaction and its cookie.
  732. */
  733. if (desc->desc.dcmd & DCMD_ENDIRQEN) {
  734. dma_cookie_t cookie = desc->async_tx.cookie;
  735. dma_cookie_complete(&desc->async_tx);
  736. dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
  737. break;
  738. }
  739. }
  740. /*
  741. * The hardware is idle and ready for more when the
  742. * chain_running list is empty.
  743. */
  744. chan->idle = list_empty(&chan->chain_running);
  745. /* Start any pending transactions automatically */
  746. start_pending_queue(chan);
  747. spin_unlock_irqrestore(&chan->desc_lock, flags);
  748. /* Run the callback for each descriptor, in order */
  749. list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
  750. struct dma_async_tx_descriptor *txd = &desc->async_tx;
  751. /* Remove from the list of transactions */
  752. list_del(&desc->node);
  753. /* Run the link descriptor callback function */
  754. dmaengine_desc_get_callback(txd, &cb);
  755. dmaengine_desc_callback_invoke(&cb, NULL);
  756. dma_pool_free(chan->desc_pool, desc, txd->phys);
  757. }
  758. }
  759. static int mmp_pdma_remove(struct platform_device *op)
  760. {
  761. struct mmp_pdma_device *pdev = platform_get_drvdata(op);
  762. struct mmp_pdma_phy *phy;
  763. int i, irq = 0, irq_num = 0;
  764. for (i = 0; i < pdev->dma_channels; i++) {
  765. if (platform_get_irq(op, i) > 0)
  766. irq_num++;
  767. }
  768. if (irq_num != pdev->dma_channels) {
  769. irq = platform_get_irq(op, 0);
  770. devm_free_irq(&op->dev, irq, pdev);
  771. } else {
  772. for (i = 0; i < pdev->dma_channels; i++) {
  773. phy = &pdev->phy[i];
  774. irq = platform_get_irq(op, i);
  775. devm_free_irq(&op->dev, irq, phy);
  776. }
  777. }
  778. dma_async_device_unregister(&pdev->device);
  779. return 0;
  780. }
  781. static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev, int idx, int irq)
  782. {
  783. struct mmp_pdma_phy *phy = &pdev->phy[idx];
  784. struct mmp_pdma_chan *chan;
  785. int ret;
  786. chan = devm_kzalloc(pdev->dev, sizeof(*chan), GFP_KERNEL);
  787. if (chan == NULL)
  788. return -ENOMEM;
  789. phy->idx = idx;
  790. phy->base = pdev->base;
  791. if (irq) {
  792. ret = devm_request_irq(pdev->dev, irq, mmp_pdma_chan_handler,
  793. IRQF_SHARED, "pdma", phy);
  794. if (ret) {
  795. dev_err(pdev->dev, "channel request irq fail!\n");
  796. return ret;
  797. }
  798. }
  799. spin_lock_init(&chan->desc_lock);
  800. chan->dev = pdev->dev;
  801. chan->chan.device = &pdev->device;
  802. tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
  803. INIT_LIST_HEAD(&chan->chain_pending);
  804. INIT_LIST_HEAD(&chan->chain_running);
  805. /* register virt channel to dma engine */
  806. list_add_tail(&chan->chan.device_node, &pdev->device.channels);
  807. return 0;
  808. }
  809. static const struct of_device_id mmp_pdma_dt_ids[] = {
  810. { .compatible = "marvell,pdma-1.0", },
  811. {}
  812. };
  813. MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
  814. static struct dma_chan *mmp_pdma_dma_xlate(struct of_phandle_args *dma_spec,
  815. struct of_dma *ofdma)
  816. {
  817. struct mmp_pdma_device *d = ofdma->of_dma_data;
  818. struct dma_chan *chan;
  819. chan = dma_get_any_slave_channel(&d->device);
  820. if (!chan)
  821. return NULL;
  822. to_mmp_pdma_chan(chan)->drcmr = dma_spec->args[0];
  823. return chan;
  824. }
  825. static int mmp_pdma_probe(struct platform_device *op)
  826. {
  827. struct mmp_pdma_device *pdev;
  828. const struct of_device_id *of_id;
  829. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  830. struct resource *iores;
  831. int i, ret, irq = 0;
  832. int dma_channels = 0, irq_num = 0;
  833. const enum dma_slave_buswidth widths =
  834. DMA_SLAVE_BUSWIDTH_1_BYTE | DMA_SLAVE_BUSWIDTH_2_BYTES |
  835. DMA_SLAVE_BUSWIDTH_4_BYTES;
  836. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  837. if (!pdev)
  838. return -ENOMEM;
  839. pdev->dev = &op->dev;
  840. spin_lock_init(&pdev->phy_lock);
  841. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  842. pdev->base = devm_ioremap_resource(pdev->dev, iores);
  843. if (IS_ERR(pdev->base))
  844. return PTR_ERR(pdev->base);
  845. of_id = of_match_device(mmp_pdma_dt_ids, pdev->dev);
  846. if (of_id)
  847. of_property_read_u32(pdev->dev->of_node, "#dma-channels",
  848. &dma_channels);
  849. else if (pdata && pdata->dma_channels)
  850. dma_channels = pdata->dma_channels;
  851. else
  852. dma_channels = 32; /* default 32 channel */
  853. pdev->dma_channels = dma_channels;
  854. for (i = 0; i < dma_channels; i++) {
  855. if (platform_get_irq(op, i) > 0)
  856. irq_num++;
  857. }
  858. pdev->phy = devm_kcalloc(pdev->dev, dma_channels, sizeof(*pdev->phy),
  859. GFP_KERNEL);
  860. if (pdev->phy == NULL)
  861. return -ENOMEM;
  862. INIT_LIST_HEAD(&pdev->device.channels);
  863. if (irq_num != dma_channels) {
  864. /* all chan share one irq, demux inside */
  865. irq = platform_get_irq(op, 0);
  866. ret = devm_request_irq(pdev->dev, irq, mmp_pdma_int_handler,
  867. IRQF_SHARED, "pdma", pdev);
  868. if (ret)
  869. return ret;
  870. }
  871. for (i = 0; i < dma_channels; i++) {
  872. irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
  873. ret = mmp_pdma_chan_init(pdev, i, irq);
  874. if (ret)
  875. return ret;
  876. }
  877. dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
  878. dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
  879. dma_cap_set(DMA_CYCLIC, pdev->device.cap_mask);
  880. dma_cap_set(DMA_PRIVATE, pdev->device.cap_mask);
  881. pdev->device.dev = &op->dev;
  882. pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
  883. pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
  884. pdev->device.device_tx_status = mmp_pdma_tx_status;
  885. pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
  886. pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
  887. pdev->device.device_prep_dma_cyclic = mmp_pdma_prep_dma_cyclic;
  888. pdev->device.device_issue_pending = mmp_pdma_issue_pending;
  889. pdev->device.device_config = mmp_pdma_config;
  890. pdev->device.device_terminate_all = mmp_pdma_terminate_all;
  891. pdev->device.copy_align = DMAENGINE_ALIGN_8_BYTES;
  892. pdev->device.src_addr_widths = widths;
  893. pdev->device.dst_addr_widths = widths;
  894. pdev->device.directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  895. pdev->device.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  896. if (pdev->dev->coherent_dma_mask)
  897. dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
  898. else
  899. dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
  900. ret = dma_async_device_register(&pdev->device);
  901. if (ret) {
  902. dev_err(pdev->device.dev, "unable to register\n");
  903. return ret;
  904. }
  905. if (op->dev.of_node) {
  906. /* Device-tree DMA controller registration */
  907. ret = of_dma_controller_register(op->dev.of_node,
  908. mmp_pdma_dma_xlate, pdev);
  909. if (ret < 0) {
  910. dev_err(&op->dev, "of_dma_controller_register failed\n");
  911. return ret;
  912. }
  913. }
  914. platform_set_drvdata(op, pdev);
  915. dev_info(pdev->device.dev, "initialized %d channels\n", dma_channels);
  916. return 0;
  917. }
  918. static const struct platform_device_id mmp_pdma_id_table[] = {
  919. { "mmp-pdma", },
  920. { },
  921. };
  922. static struct platform_driver mmp_pdma_driver = {
  923. .driver = {
  924. .name = "mmp-pdma",
  925. .of_match_table = mmp_pdma_dt_ids,
  926. },
  927. .id_table = mmp_pdma_id_table,
  928. .probe = mmp_pdma_probe,
  929. .remove = mmp_pdma_remove,
  930. };
  931. bool mmp_pdma_filter_fn(struct dma_chan *chan, void *param)
  932. {
  933. struct mmp_pdma_chan *c = to_mmp_pdma_chan(chan);
  934. if (chan->device->dev->driver != &mmp_pdma_driver.driver)
  935. return false;
  936. c->drcmr = *(unsigned int *)param;
  937. return true;
  938. }
  939. EXPORT_SYMBOL_GPL(mmp_pdma_filter_fn);
  940. module_platform_driver(mmp_pdma_driver);
  941. MODULE_DESCRIPTION("MARVELL MMP Peripheral DMA Driver");
  942. MODULE_AUTHOR("Marvell International Ltd.");
  943. MODULE_LICENSE("GPL v2");