8250_mtk_dma.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * Mediatek 8250 DMA driver.
  3. *
  4. * Copyright (c) 2017 MediaTek Inc.
  5. * Author: Long Cheng <long.cheng@mediatek.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #define pr_fmt(fmt) "mtk-8250-dma: " fmt
  17. #define DRV_NAME "mtk-8250-dma"
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <linux/dmaengine.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/list.h>
  26. #include <linux/module.h>
  27. #include <linux/of_dma.h>
  28. #include <linux/of_device.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/pm_runtime.h>
  33. #include "virt-dma.h"
  34. #define MTK_SDMA_REQUESTS 127
  35. #define MTK_SDMA_CHANNELS (CONFIG_SERIAL_8250_NR_UARTS * 2)
  36. #define VFF_RX_INT_FLAG_CLR_B (BIT(0) | BIT(1))
  37. #define VFF_TX_INT_FLAG_CLR_B 0
  38. #define VFF_RX_INT_EN0_B BIT(0) /*rx valid size >= vff thre */
  39. #define VFF_RX_INT_EN1_B BIT(1)
  40. #define VFF_TX_INT_EN_B BIT(0) /*tx left size >= vff thre */
  41. #define VFF_INT_EN_CLR_B 0
  42. #define VFF_WARM_RST_B BIT(0)
  43. #define VFF_EN_B BIT(0)
  44. #define VFF_STOP_B BIT(0)
  45. #define VFF_STOP_CLR_B 0
  46. #define VFF_FLUSH_B BIT(0)
  47. #define VFF_FLUSH_CLR_B 0
  48. #define VFF_4G_SUPPORT_CLR_B 0
  49. #define VFF_ORI_ADDR_BITS_NUM 32
  50. #define VFF_TX_THRE(n) ((n)*7/8) /* interrupt trigger level for tx */
  51. #define VFF_RX_THRE(n) ((n)*3/4) /* interrupt trigger level for rx */
  52. #define MTK_DMA_RING_SIZE 0xffffU
  53. /* invert this bit when wrap ring head again*/
  54. #define MTK_DMA_RING_WRAP 0x10000U
  55. struct mtk_dmadev {
  56. struct dma_device ddev;
  57. void __iomem *mem_base[MTK_SDMA_CHANNELS];
  58. spinlock_t lock;
  59. struct tasklet_struct task;
  60. struct list_head pending;
  61. struct clk *clk;
  62. unsigned int dma_requests;
  63. unsigned int support_bits;
  64. unsigned int dma_irq[MTK_SDMA_CHANNELS];
  65. struct mtk_chan *lch_map[MTK_SDMA_CHANNELS];
  66. };
  67. struct mtk_chan {
  68. struct virt_dma_chan vc;
  69. struct list_head node;
  70. struct dma_slave_config cfg;
  71. void __iomem *channel_base;
  72. struct mtk_dma_desc *desc;
  73. bool paused;
  74. bool requested;
  75. unsigned int dma_sig;
  76. unsigned int dma_ch;
  77. unsigned int sgidx;
  78. unsigned int remain_size;
  79. unsigned int rx_ptr;
  80. /*sync*/
  81. struct completion done; /* dma transfer done */
  82. spinlock_t lock;
  83. atomic_t loopcnt;
  84. atomic_t entry; /* entry count */
  85. };
  86. struct mtk_dma_sg {
  87. dma_addr_t addr;
  88. unsigned int en; /* number of elements (24-bit) */
  89. unsigned int fn; /* number of frames (16-bit) */
  90. };
  91. struct mtk_dma_desc {
  92. struct virt_dma_desc vd;
  93. enum dma_transfer_direction dir;
  94. dma_addr_t dev_addr;
  95. unsigned int sglen;
  96. struct mtk_dma_sg sg[0];
  97. };
  98. enum {
  99. VFF_INT_FLAG = 0x00,
  100. VFF_INT_EN = 0x04,
  101. VFF_EN = 0x08,
  102. VFF_RST = 0x0c,
  103. VFF_STOP = 0x10,
  104. VFF_FLUSH = 0x14,
  105. VFF_ADDR = 0x1c,
  106. VFF_LEN = 0x24,
  107. VFF_THRE = 0x28,
  108. VFF_WPT = 0x2c,
  109. VFF_RPT = 0x30,
  110. /*TX: the buffer size HW can read. RX: the buffer size SW can read.*/
  111. VFF_VALID_SIZE = 0x3c,
  112. /*TX: the buffer size SW can write. RX: the buffer size HW can write.*/
  113. VFF_LEFT_SIZE = 0x40,
  114. VFF_DEBUG_STATUS = 0x50,
  115. VFF_4G_SUPPORT = 0x54,
  116. };
  117. static bool mtk_dma_filter_fn(struct dma_chan *chan, void *param);
  118. static struct of_dma_filter_info mtk_dma_info = {
  119. .filter_fn = mtk_dma_filter_fn,
  120. };
  121. static inline struct mtk_dmadev *to_mtk_dma_dev(struct dma_device *d)
  122. {
  123. return container_of(d, struct mtk_dmadev, ddev);
  124. }
  125. static inline struct mtk_chan *to_mtk_dma_chan(struct dma_chan *c)
  126. {
  127. return container_of(c, struct mtk_chan, vc.chan);
  128. }
  129. static inline struct mtk_dma_desc
  130. *to_mtk_dma_desc(struct dma_async_tx_descriptor *t)
  131. {
  132. return container_of(t, struct mtk_dma_desc, vd.tx);
  133. }
  134. static void mtk_dma_chan_write(struct mtk_chan *c, unsigned int reg,
  135. unsigned int val)
  136. {
  137. writel(val, c->channel_base + reg);
  138. }
  139. static unsigned int mtk_dma_chan_read(struct mtk_chan *c, unsigned int reg)
  140. {
  141. return readl(c->channel_base + reg);
  142. }
  143. static void mtk_dma_desc_free(struct virt_dma_desc *vd)
  144. {
  145. struct dma_chan *chan = vd->tx.chan;
  146. struct mtk_chan *c = to_mtk_dma_chan(chan);
  147. unsigned long flags;
  148. spin_lock_irqsave(&c->vc.lock, flags);
  149. if (c->desc != NULL) {
  150. kfree(c->desc);
  151. c->desc = NULL;
  152. if (c->cfg.direction == DMA_DEV_TO_MEM)
  153. atomic_dec(&c->entry);
  154. }
  155. spin_unlock_irqrestore(&c->vc.lock, flags);
  156. }
  157. static int mtk_dma_clk_enable(struct mtk_dmadev *mtkd)
  158. {
  159. int rc;
  160. rc = clk_prepare_enable(mtkd->clk);
  161. if (rc) {
  162. pr_err("Couldn't enable the clock\n");
  163. return rc;
  164. }
  165. return 0;
  166. }
  167. static void mtk_dma_clk_disable(struct mtk_dmadev *mtkd)
  168. {
  169. clk_disable_unprepare(mtkd->clk);
  170. }
  171. static void mtk_dma_remove_virt_list(dma_cookie_t cookie,
  172. struct virt_dma_chan *vc)
  173. {
  174. struct virt_dma_desc *vd;
  175. if (list_empty(&vc->desc_issued) == 0) {
  176. list_for_each_entry(vd, &vc->desc_issued, node) {
  177. if (cookie == vd->tx.cookie) {
  178. INIT_LIST_HEAD(&vc->desc_issued);
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. static void mtk_dma_tx_flush(struct dma_chan *chan)
  185. {
  186. struct mtk_chan *c = to_mtk_dma_chan(chan);
  187. if (mtk_dma_chan_read(c, VFF_FLUSH) == 0) {
  188. mtk_dma_chan_write(c, VFF_FLUSH, VFF_FLUSH_B);
  189. if (atomic_dec_and_test(&c->loopcnt))
  190. complete(&c->done);
  191. }
  192. }
  193. /*
  194. * check whether the dma flush operation is finished or not.
  195. * return 0 for flush success.
  196. * return others for flush timeout.
  197. */
  198. static int mtk_dma_check_flush_result(struct dma_chan *chan)
  199. {
  200. struct timespec start, end;
  201. struct mtk_chan *c = to_mtk_dma_chan(chan);
  202. start = ktime_to_timespec(ktime_get());
  203. while (mtk_dma_chan_read(c, VFF_FLUSH)) {
  204. end = ktime_to_timespec(ktime_get());
  205. if ((end.tv_sec - start.tv_sec) > 1 ||
  206. ((end.tv_sec - start.tv_sec) == 1 &&
  207. end.tv_nsec > start.tv_nsec)) {
  208. pr_err("[DMA] Polling flush timeout\n");
  209. return -1;
  210. }
  211. }
  212. return 0;
  213. }
  214. static void mtk_dma_tx_write(struct dma_chan *chan)
  215. {
  216. struct mtk_chan *c = to_mtk_dma_chan(chan);
  217. struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
  218. unsigned int txcount = c->remain_size;
  219. unsigned int len, send, left, wpt, wrap;
  220. if (atomic_inc_return(&c->entry) > 1) {
  221. if (vchan_issue_pending(&c->vc) && (c->desc == NULL)) {
  222. spin_lock(&mtkd->lock);
  223. list_add_tail(&c->node, &mtkd->pending);
  224. spin_unlock(&mtkd->lock);
  225. tasklet_schedule(&mtkd->task);
  226. }
  227. } else {
  228. len = mtk_dma_chan_read(c, VFF_LEN);
  229. if (mtk_dma_check_flush_result(chan) != 0)
  230. return;
  231. while ((left = mtk_dma_chan_read(c, VFF_LEFT_SIZE)) > 0U) {
  232. send = min(left, c->remain_size);
  233. wpt = mtk_dma_chan_read(c, VFF_WPT);
  234. wrap = wpt & MTK_DMA_RING_WRAP ? 0 : MTK_DMA_RING_WRAP;
  235. if ((wpt & (len - 1U)) + send < len)
  236. mtk_dma_chan_write(c, VFF_WPT, wpt + send);
  237. else
  238. mtk_dma_chan_write(c, VFF_WPT,
  239. ((wpt + send) & (len - 1U)) | wrap);
  240. c->remain_size -= send;
  241. if (c->remain_size == 0U)
  242. break;
  243. }
  244. if (txcount != c->remain_size) {
  245. mtk_dma_chan_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
  246. mtk_dma_tx_flush(chan);
  247. }
  248. }
  249. atomic_dec(&c->entry);
  250. }
  251. static void mtk_dma_start_tx(struct mtk_chan *c)
  252. {
  253. if (mtk_dma_chan_read(c, VFF_LEFT_SIZE) == 0U) {
  254. pr_info("%s maybe need fix? @L %d\n", __func__, __LINE__);
  255. mtk_dma_chan_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
  256. } else {
  257. reinit_completion(&c->done);
  258. /* inc twice, once for tx_flush, another for tx_interrupt */
  259. atomic_inc(&c->loopcnt);
  260. atomic_inc(&c->loopcnt);
  261. mtk_dma_tx_write(&c->vc.chan);
  262. }
  263. c->paused = false;
  264. }
  265. static void mtk_dma_get_rx_size(struct mtk_chan *c)
  266. {
  267. unsigned int count;
  268. unsigned int rdptr, wrptr, wrreg, rdreg;
  269. unsigned int rx_size = mtk_dma_chan_read(c, VFF_LEN);
  270. rdreg = mtk_dma_chan_read(c, VFF_RPT);
  271. wrreg = mtk_dma_chan_read(c, VFF_WPT);
  272. rdptr = rdreg & MTK_DMA_RING_SIZE;
  273. wrptr = wrreg & MTK_DMA_RING_SIZE;
  274. count = ((rdreg ^ wrreg) & MTK_DMA_RING_WRAP) ?
  275. (wrptr + rx_size - rdptr) : (wrptr - rdptr);
  276. c->remain_size = count;
  277. c->rx_ptr = rdptr;
  278. mtk_dma_chan_write(c, VFF_RPT, wrreg);
  279. }
  280. static void mtk_dma_start_rx(struct mtk_chan *c)
  281. {
  282. struct dma_chan *chan = &(c->vc.chan);
  283. struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
  284. struct mtk_dma_desc *d = c->desc;
  285. if (mtk_dma_chan_read(c, VFF_VALID_SIZE) != 0U &&
  286. d != NULL && d->vd.tx.cookie != 0) {
  287. mtk_dma_get_rx_size(c);
  288. mtk_dma_remove_virt_list(d->vd.tx.cookie, &c->vc);
  289. vchan_cookie_complete(&d->vd);
  290. } else {
  291. if (mtk_dma_chan_read(c, VFF_VALID_SIZE) != 0U) {
  292. spin_lock(&mtkd->lock);
  293. if (list_empty(&mtkd->pending))
  294. list_add_tail(&c->node, &mtkd->pending);
  295. spin_unlock(&mtkd->lock);
  296. tasklet_schedule(&mtkd->task);
  297. } else {
  298. if (atomic_read(&c->entry) > 0UL)
  299. atomic_set(&c->entry, 0);
  300. }
  301. }
  302. }
  303. static void mtk_dma_reset(struct mtk_chan *c)
  304. {
  305. struct mtk_dmadev *mtkd = to_mtk_dma_dev(c->vc.chan.device);
  306. mtk_dma_chan_write(c, VFF_ADDR, 0);
  307. mtk_dma_chan_write(c, VFF_THRE, 0);
  308. mtk_dma_chan_write(c, VFF_LEN, 0);
  309. mtk_dma_chan_write(c, VFF_RST, VFF_WARM_RST_B);
  310. while
  311. (mtk_dma_chan_read(c, VFF_EN));
  312. if (c->cfg.direction == DMA_DEV_TO_MEM)
  313. mtk_dma_chan_write(c, VFF_RPT, 0);
  314. else if (c->cfg.direction == DMA_MEM_TO_DEV)
  315. mtk_dma_chan_write(c, VFF_WPT, 0);
  316. else
  317. pr_info("Unknown direction.\n");
  318. if (mtkd->support_bits)
  319. mtk_dma_chan_write(c, VFF_4G_SUPPORT, VFF_4G_SUPPORT_CLR_B);
  320. }
  321. static void mtk_dma_stop(struct mtk_chan *c)
  322. {
  323. int polling_cnt;
  324. mtk_dma_chan_write(c, VFF_FLUSH, VFF_FLUSH_CLR_B);
  325. polling_cnt = 0;
  326. while (mtk_dma_chan_read(c, VFF_FLUSH)) {
  327. if (polling_cnt++ > 10000) {
  328. pr_err("dma stop: polling FLUSH fail, DEBUG=0x%x\n",
  329. mtk_dma_chan_read(c, VFF_DEBUG_STATUS));
  330. break;
  331. }
  332. }
  333. polling_cnt = 0;
  334. /*set stop as 1 -> wait until en is 0 -> set stop as 0*/
  335. mtk_dma_chan_write(c, VFF_STOP, VFF_STOP_B);
  336. while (mtk_dma_chan_read(c, VFF_EN)) {
  337. if (polling_cnt++ > 10000) {
  338. pr_err("dma stop: polling VFF_EN fail, DEBUG=0x%x\n",
  339. mtk_dma_chan_read(c, VFF_DEBUG_STATUS));
  340. break;
  341. }
  342. }
  343. mtk_dma_chan_write(c, VFF_STOP, VFF_STOP_CLR_B);
  344. mtk_dma_chan_write(c, VFF_INT_EN, VFF_INT_EN_CLR_B);
  345. if (c->cfg.direction == DMA_DEV_TO_MEM)
  346. mtk_dma_chan_write(c, VFF_INT_FLAG, VFF_RX_INT_FLAG_CLR_B);
  347. else
  348. mtk_dma_chan_write(c, VFF_INT_FLAG, VFF_TX_INT_FLAG_CLR_B);
  349. c->paused = true;
  350. }
  351. /*
  352. * We need to deal with 'all channels in-use'
  353. */
  354. static void mtk_dma_rx_sched(struct mtk_chan *c)
  355. {
  356. struct dma_chan *chan = &(c->vc.chan);
  357. struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
  358. if (atomic_read(&c->entry) < 1) {
  359. mtk_dma_start_rx(c);
  360. } else {
  361. spin_lock(&mtkd->lock);
  362. if (list_empty(&mtkd->pending))
  363. list_add_tail(&c->node, &mtkd->pending);
  364. spin_unlock(&mtkd->lock);
  365. tasklet_schedule(&mtkd->task);
  366. }
  367. }
  368. /*
  369. * This callback schedules all pending channels. We could be more
  370. * clever here by postponing allocation of the real DMA channels to
  371. * this point, and freeing them when our virtual channel becomes idle.
  372. *
  373. * We would then need to deal with 'all channels in-use'
  374. */
  375. static void mtk_dma_sched(unsigned long data)
  376. {
  377. struct mtk_dmadev *mtkd = (struct mtk_dmadev *)data;
  378. struct mtk_chan *c;
  379. struct virt_dma_desc *vd;
  380. dma_cookie_t cookie;
  381. LIST_HEAD(head);
  382. unsigned long flags;
  383. spin_lock_irq(&mtkd->lock);
  384. list_splice_tail_init(&mtkd->pending, &head);
  385. spin_unlock_irq(&mtkd->lock);
  386. if (list_empty(&head) == 0) {
  387. c = list_first_entry(&head, struct mtk_chan, node);
  388. cookie = c->vc.chan.cookie;
  389. spin_lock_irqsave(&c->vc.lock, flags);
  390. if (c->cfg.direction == DMA_DEV_TO_MEM) {
  391. list_del_init(&c->node);
  392. mtk_dma_rx_sched(c);
  393. } else if (c->cfg.direction == DMA_MEM_TO_DEV) {
  394. vd = vchan_find_desc(&c->vc, cookie);
  395. c->desc = to_mtk_dma_desc(&vd->tx);
  396. list_del_init(&c->node);
  397. mtk_dma_start_tx(c);
  398. }
  399. spin_unlock_irqrestore(&c->vc.lock, flags);
  400. }
  401. }
  402. static int mtk_dma_alloc_chan_resources(struct dma_chan *chan)
  403. {
  404. struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
  405. struct mtk_chan *c = to_mtk_dma_chan(chan);
  406. int ret;
  407. pm_runtime_get_sync(mtkd->ddev.dev);
  408. ret = -EBUSY;
  409. if (mtkd->lch_map[c->dma_ch] == NULL) {
  410. c->channel_base = mtkd->mem_base[c->dma_ch];
  411. mtkd->lch_map[c->dma_ch] = c;
  412. ret = 1;
  413. }
  414. c->requested = false;
  415. mtk_dma_reset(c);
  416. return ret;
  417. }
  418. static void mtk_dma_free_chan_resources(struct dma_chan *chan)
  419. {
  420. struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
  421. struct mtk_chan *c = to_mtk_dma_chan(chan);
  422. if (c->requested == true) {
  423. c->requested = false;
  424. free_irq(mtkd->dma_irq[c->dma_ch], chan);
  425. }
  426. c->channel_base = NULL;
  427. mtkd->lch_map[c->dma_ch] = NULL;
  428. vchan_free_chan_resources(&c->vc);
  429. pr_debug("freeing channel for %u\n", c->dma_sig);
  430. c->dma_sig = 0;
  431. tasklet_kill(&mtkd->task);
  432. pm_runtime_put_sync(mtkd->ddev.dev);
  433. }
  434. static enum dma_status mtk_dma_tx_status(struct dma_chan *chan,
  435. dma_cookie_t cookie, struct dma_tx_state *txstate)
  436. {
  437. struct mtk_chan *c = to_mtk_dma_chan(chan);
  438. enum dma_status ret;
  439. unsigned long flags;
  440. ret = dma_cookie_status(chan, cookie, txstate);
  441. spin_lock_irqsave(&c->vc.lock, flags);
  442. if (ret == DMA_IN_PROGRESS) {
  443. c->rx_ptr = mtk_dma_chan_read(c, VFF_RPT) & MTK_DMA_RING_SIZE;
  444. txstate->residue = c->rx_ptr;
  445. } else if (ret == DMA_COMPLETE && c->cfg.direction == DMA_DEV_TO_MEM) {
  446. txstate->residue = c->remain_size;
  447. } else {
  448. txstate->residue = 0;
  449. }
  450. spin_unlock_irqrestore(&c->vc.lock, flags);
  451. return ret;
  452. }
  453. static unsigned int mtk_dma_desc_size(struct mtk_dma_desc *d)
  454. {
  455. struct mtk_dma_sg *sg;
  456. unsigned int i;
  457. unsigned int size;
  458. for (size = i = 0; i < d->sglen; i++) {
  459. sg = &d->sg[i];
  460. size += sg->en * sg->fn;
  461. }
  462. return size;
  463. }
  464. static struct dma_async_tx_descriptor *mtk_dma_prep_slave_sg(
  465. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
  466. enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
  467. {
  468. struct mtk_chan *c = to_mtk_dma_chan(chan);
  469. enum dma_slave_buswidth dev_width;
  470. struct scatterlist *sgent;
  471. struct mtk_dma_desc *d;
  472. dma_addr_t dev_addr;
  473. unsigned int i, j, en, frame_bytes;
  474. en = frame_bytes = 1;
  475. if (dir == DMA_DEV_TO_MEM) {
  476. dev_addr = c->cfg.src_addr;
  477. dev_width = c->cfg.src_addr_width;
  478. } else if (dir == DMA_MEM_TO_DEV) {
  479. dev_addr = c->cfg.dst_addr;
  480. dev_width = c->cfg.dst_addr_width;
  481. } else {
  482. pr_err("%s: bad direction?\n", __func__);
  483. return NULL;
  484. }
  485. /* Now allocate and setup the descriptor. */
  486. d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC);
  487. if (d == NULL)
  488. return NULL;
  489. d->dir = dir;
  490. d->dev_addr = dev_addr;
  491. j = 0;
  492. for_each_sg(sgl, sgent, sglen, i) {
  493. d->sg[j].addr = sg_dma_address(sgent);
  494. d->sg[j].en = en;
  495. d->sg[j].fn = sg_dma_len(sgent) / frame_bytes;
  496. j++;
  497. }
  498. d->sglen = j;
  499. if (dir == DMA_MEM_TO_DEV)
  500. c->remain_size = mtk_dma_desc_size(d);
  501. return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
  502. }
  503. static void mtk_dma_issue_pending(struct dma_chan *chan)
  504. {
  505. struct mtk_chan *c = to_mtk_dma_chan(chan);
  506. struct mtk_dmadev *mtkd;
  507. struct virt_dma_desc *vd;
  508. dma_cookie_t cookie;
  509. unsigned long flags;
  510. spin_lock_irqsave(&c->vc.lock, flags);
  511. if (c->cfg.direction == DMA_DEV_TO_MEM) {
  512. cookie = c->vc.chan.cookie;
  513. mtkd = to_mtk_dma_dev(chan->device);
  514. if (vchan_issue_pending(&c->vc) && (c->desc == NULL)) {
  515. vd = vchan_find_desc(&c->vc, cookie);
  516. c->desc = to_mtk_dma_desc(&vd->tx);
  517. if (atomic_read(&c->entry) > 0)
  518. atomic_set(&c->entry, 0);
  519. }
  520. } else if (c->cfg.direction == DMA_MEM_TO_DEV) {
  521. cookie = c->vc.chan.cookie;
  522. if (vchan_issue_pending(&c->vc) && !c->desc) {
  523. vd = vchan_find_desc(&c->vc, cookie);
  524. c->desc = to_mtk_dma_desc(&vd->tx);
  525. mtk_dma_start_tx(c);
  526. }
  527. }
  528. spin_unlock_irqrestore(&c->vc.lock, flags);
  529. }
  530. static irqreturn_t mtk_dma_rx_interrupt(int irq, void *dev_id)
  531. {
  532. struct dma_chan *chan = (struct dma_chan *)dev_id;
  533. struct mtk_chan *c = to_mtk_dma_chan(chan);
  534. struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
  535. unsigned long flags;
  536. spin_lock_irqsave(&c->vc.lock, flags);
  537. mtk_dma_chan_write(c, VFF_INT_FLAG, VFF_RX_INT_FLAG_CLR_B);
  538. if (atomic_inc_return(&c->entry) > 1) {
  539. if (list_empty(&mtkd->pending))
  540. list_add_tail(&c->node, &mtkd->pending);
  541. tasklet_schedule(&mtkd->task);
  542. } else {
  543. mtk_dma_start_rx(c);
  544. }
  545. spin_unlock_irqrestore(&c->vc.lock, flags);
  546. return IRQ_HANDLED;
  547. }
  548. static irqreturn_t mtk_dma_tx_interrupt(int irq, void *dev_id)
  549. {
  550. struct dma_chan *chan = (struct dma_chan *)dev_id;
  551. struct mtk_chan *c = to_mtk_dma_chan(chan);
  552. struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
  553. struct mtk_dma_desc *d = c->desc;
  554. unsigned long flags;
  555. spin_lock_irqsave(&c->vc.lock, flags);
  556. if (c->remain_size != 0) {
  557. list_add_tail(&c->node, &mtkd->pending);
  558. tasklet_schedule(&mtkd->task);
  559. } else {
  560. mtk_dma_remove_virt_list(d->vd.tx.cookie, &c->vc);
  561. vchan_cookie_complete(&d->vd);
  562. }
  563. spin_unlock_irqrestore(&c->vc.lock, flags);
  564. mtk_dma_chan_write(c, VFF_INT_FLAG, VFF_TX_INT_FLAG_CLR_B);
  565. if (atomic_dec_and_test(&c->loopcnt))
  566. complete(&c->done);
  567. return IRQ_HANDLED;
  568. }
  569. static int mtk_dma_slave_config(struct dma_chan *chan,
  570. struct dma_slave_config *cfg)
  571. {
  572. struct mtk_chan *c = to_mtk_dma_chan(chan);
  573. struct mtk_dmadev *mtkd = to_mtk_dma_dev(c->vc.chan.device);
  574. int ret;
  575. c->cfg = *cfg;
  576. if (cfg->direction == DMA_DEV_TO_MEM) {
  577. unsigned int rx_len = cfg->src_addr_width * 1024;
  578. mtk_dma_chan_write(c, VFF_ADDR, cfg->src_addr);
  579. mtk_dma_chan_write(c, VFF_LEN, rx_len);
  580. mtk_dma_chan_write(c, VFF_THRE, VFF_RX_THRE(rx_len));
  581. mtk_dma_chan_write(c, VFF_INT_EN, VFF_RX_INT_EN0_B |
  582. VFF_RX_INT_EN1_B);
  583. mtk_dma_chan_write(c, VFF_INT_FLAG, VFF_RX_INT_FLAG_CLR_B);
  584. mtk_dma_chan_write(c, VFF_EN, VFF_EN_B);
  585. if (mtkd->support_bits > VFF_ORI_ADDR_BITS_NUM)
  586. mtk_dma_chan_write(c, VFF_4G_SUPPORT,
  587. upper_32_bits(cfg->src_addr));
  588. if (c->requested == false) {
  589. atomic_set(&c->entry, 0);
  590. c->requested = true;
  591. ret = request_irq(mtkd->dma_irq[c->dma_ch],
  592. mtk_dma_rx_interrupt, IRQF_TRIGGER_NONE,
  593. DRV_NAME, chan);
  594. if (ret) {
  595. pr_err("Cannot request rx dma IRQ\n");
  596. return -EINVAL;
  597. }
  598. }
  599. } else if (cfg->direction == DMA_MEM_TO_DEV) {
  600. unsigned int tx_len = cfg->dst_addr_width * 1024;
  601. mtk_dma_chan_write(c, VFF_ADDR, cfg->dst_addr);
  602. mtk_dma_chan_write(c, VFF_LEN, tx_len);
  603. mtk_dma_chan_write(c, VFF_THRE, VFF_TX_THRE(tx_len));
  604. mtk_dma_chan_write(c, VFF_INT_FLAG, VFF_TX_INT_FLAG_CLR_B);
  605. mtk_dma_chan_write(c, VFF_EN, VFF_EN_B);
  606. if (mtkd->support_bits > VFF_ORI_ADDR_BITS_NUM)
  607. mtk_dma_chan_write(c, VFF_4G_SUPPORT,
  608. upper_32_bits(cfg->dst_addr));
  609. if (c->requested == false) {
  610. c->requested = true;
  611. ret = request_irq(mtkd->dma_irq[c->dma_ch],
  612. mtk_dma_tx_interrupt, IRQF_TRIGGER_NONE,
  613. DRV_NAME, chan);
  614. if (ret) {
  615. pr_err("Cannot request tx dma IRQ\n");
  616. return -EINVAL;
  617. }
  618. }
  619. } else
  620. pr_info("Unknown direction!\n");
  621. if (mtk_dma_chan_read(c, VFF_EN) != VFF_EN_B) {
  622. pr_err("config dir%d dma fail\n", cfg->direction);
  623. return -EINVAL;
  624. }
  625. return 0;
  626. }
  627. static int mtk_dma_terminate_all(struct dma_chan *chan)
  628. {
  629. struct mtk_chan *c = to_mtk_dma_chan(chan);
  630. unsigned long flags;
  631. LIST_HEAD(head);
  632. if (atomic_read(&c->loopcnt) != 0)
  633. wait_for_completion(&c->done);
  634. spin_lock_irqsave(&c->vc.lock, flags);
  635. if (c->desc) {
  636. mtk_dma_remove_virt_list(c->desc->vd.tx.cookie, &c->vc);
  637. spin_unlock_irqrestore(&c->vc.lock, flags);
  638. mtk_dma_desc_free(&c->desc->vd);
  639. spin_lock_irqsave(&c->vc.lock, flags);
  640. if (c->paused == false) {
  641. list_del_init(&c->node);
  642. mtk_dma_stop(c);
  643. }
  644. }
  645. vchan_get_all_descriptors(&c->vc, &head);
  646. spin_unlock_irqrestore(&c->vc.lock, flags);
  647. vchan_dma_desc_free_list(&c->vc, &head);
  648. return 0;
  649. }
  650. static int mtk_dma_device_pause(struct dma_chan *chan)
  651. {
  652. /* Pause/Resume only allowed with cyclic mode */
  653. return -EINVAL;
  654. }
  655. static int mtk_dma_device_resume(struct dma_chan *chan)
  656. {
  657. /* Pause/Resume only allowed with cyclic mode */
  658. return -EINVAL;
  659. }
  660. static int mtk_dma_chan_init(struct mtk_dmadev *mtkd)
  661. {
  662. struct mtk_chan *c;
  663. c = devm_kzalloc(mtkd->ddev.dev, sizeof(*c), GFP_KERNEL);
  664. if (c == NULL)
  665. return -ENOMEM;
  666. c->vc.desc_free = mtk_dma_desc_free;
  667. vchan_init(&c->vc, &mtkd->ddev);
  668. spin_lock_init(&c->lock);
  669. INIT_LIST_HEAD(&c->node);
  670. init_completion(&c->done);
  671. atomic_set(&c->loopcnt, 0);
  672. atomic_set(&c->entry, 0);
  673. return 0;
  674. }
  675. static void mtk_dma_free(struct mtk_dmadev *mtkd)
  676. {
  677. tasklet_kill(&mtkd->task);
  678. while (list_empty(&mtkd->ddev.channels) == 0) {
  679. struct mtk_chan *c = list_first_entry(&mtkd->ddev.channels,
  680. struct mtk_chan, vc.chan.device_node);
  681. list_del(&c->vc.chan.device_node);
  682. tasklet_kill(&c->vc.task);
  683. devm_kfree(mtkd->ddev.dev, c);
  684. }
  685. }
  686. static const struct of_device_id mtk_uart_dma_match[] = {
  687. { .compatible = "mediatek,mt6577-uart-dma", },
  688. { /* sentinel */ },
  689. };
  690. MODULE_DEVICE_TABLE(of, mtk_uart_dma_match);
  691. static int mtk_dma_probe(struct platform_device *pdev)
  692. {
  693. struct mtk_dmadev *mtkd;
  694. struct resource *res;
  695. int rc, i;
  696. unsigned int addr_bits = VFF_ORI_ADDR_BITS_NUM;
  697. mtkd = devm_kzalloc(&pdev->dev, sizeof(*mtkd), GFP_KERNEL);
  698. if (mtkd == NULL)
  699. return -ENOMEM;
  700. for (i = 0; i < MTK_SDMA_CHANNELS; i++) {
  701. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  702. if (res == NULL)
  703. return -ENODEV;
  704. mtkd->mem_base[i] = devm_ioremap_resource(&pdev->dev, res);
  705. if (IS_ERR(mtkd->mem_base[i]))
  706. return PTR_ERR(mtkd->mem_base[i]);
  707. }
  708. /* request irq */
  709. for (i = 0; i < MTK_SDMA_CHANNELS; i++) {
  710. mtkd->dma_irq[i] = platform_get_irq(pdev, i);
  711. if ((int)mtkd->dma_irq[i] < 0) {
  712. pr_err("Cannot claim IRQ%d\n", i);
  713. return mtkd->dma_irq[i];
  714. }
  715. }
  716. mtkd->clk = devm_clk_get(&pdev->dev, NULL);
  717. if (IS_ERR(mtkd->clk)) {
  718. pr_err("No clock specified\n");
  719. return PTR_ERR(mtkd->clk);
  720. }
  721. if (of_property_read_u32(pdev->dev.of_node, "dma-bits", &addr_bits))
  722. addr_bits = VFF_ORI_ADDR_BITS_NUM;
  723. pr_info("DMA address bits: %d\n", addr_bits);
  724. mtkd->support_bits = addr_bits;
  725. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(addr_bits));
  726. if (rc)
  727. return rc;
  728. dma_cap_set(DMA_SLAVE, mtkd->ddev.cap_mask);
  729. mtkd->ddev.device_alloc_chan_resources = mtk_dma_alloc_chan_resources;
  730. mtkd->ddev.device_free_chan_resources = mtk_dma_free_chan_resources;
  731. mtkd->ddev.device_tx_status = mtk_dma_tx_status;
  732. mtkd->ddev.device_issue_pending = mtk_dma_issue_pending;
  733. mtkd->ddev.device_prep_slave_sg = mtk_dma_prep_slave_sg;
  734. mtkd->ddev.device_config = mtk_dma_slave_config;
  735. mtkd->ddev.device_pause = mtk_dma_device_pause;
  736. mtkd->ddev.device_resume = mtk_dma_device_resume;
  737. mtkd->ddev.device_terminate_all = mtk_dma_terminate_all;
  738. mtkd->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE);
  739. mtkd->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE);
  740. mtkd->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  741. mtkd->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  742. mtkd->ddev.dev = &pdev->dev;
  743. INIT_LIST_HEAD(&mtkd->ddev.channels);
  744. INIT_LIST_HEAD(&mtkd->pending);
  745. spin_lock_init(&mtkd->lock);
  746. tasklet_init(&mtkd->task, mtk_dma_sched, (unsigned long)mtkd);
  747. mtkd->dma_requests = MTK_SDMA_REQUESTS;
  748. if ((pdev->dev.of_node != NULL) &&
  749. (of_property_read_u32(pdev->dev.of_node,
  750. "dma-requests", &mtkd->dma_requests) != 0)) {
  751. pr_info("Missing dma-requests property, using %u.\n",
  752. MTK_SDMA_REQUESTS);
  753. }
  754. for (i = 0; i < MTK_SDMA_CHANNELS; i++) {
  755. rc = mtk_dma_chan_init(mtkd);
  756. if (rc)
  757. goto err_no_dma;
  758. }
  759. pm_runtime_enable(&pdev->dev);
  760. pm_runtime_set_active(&pdev->dev);
  761. rc = dma_async_device_register(&mtkd->ddev);
  762. if (rc) {
  763. pr_warn("fail to register slave DMA device: %d\n", rc);
  764. mtk_dma_clk_disable(mtkd);
  765. goto err_no_dma;
  766. }
  767. platform_set_drvdata(pdev, mtkd);
  768. if (pdev->dev.of_node) {
  769. mtk_dma_info.dma_cap = mtkd->ddev.cap_mask;
  770. /* Device-tree DMA controller registration */
  771. rc = of_dma_controller_register(pdev->dev.of_node,
  772. of_dma_simple_xlate, &mtk_dma_info);
  773. if (rc) {
  774. pr_warn("fail to register DMA controller\n");
  775. dma_async_device_unregister(&mtkd->ddev);
  776. mtk_dma_clk_disable(mtkd);
  777. goto err_no_dma;
  778. }
  779. }
  780. return rc;
  781. err_no_dma:
  782. mtk_dma_free(mtkd);
  783. return rc;
  784. }
  785. static int mtk_dma_remove(struct platform_device *pdev)
  786. {
  787. struct mtk_dmadev *mtkd = platform_get_drvdata(pdev);
  788. if (pdev->dev.of_node)
  789. of_dma_controller_free(pdev->dev.of_node);
  790. pm_runtime_disable(&pdev->dev);
  791. pm_runtime_put_noidle(&pdev->dev);
  792. dma_async_device_unregister(&mtkd->ddev);
  793. mtk_dma_free(mtkd);
  794. return 0;
  795. }
  796. #ifdef CONFIG_PM_SLEEP
  797. static int mtk_dma_suspend(struct device *dev)
  798. {
  799. struct mtk_dmadev *mtkd = dev_get_drvdata(dev);
  800. if (!pm_runtime_suspended(dev))
  801. mtk_dma_clk_disable(mtkd);
  802. return 0;
  803. }
  804. static int mtk_dma_resume(struct device *dev)
  805. {
  806. int ret;
  807. struct mtk_dmadev *mtkd = dev_get_drvdata(dev);
  808. if (!pm_runtime_suspended(dev)) {
  809. ret = mtk_dma_clk_enable(mtkd);
  810. if (ret) {
  811. pr_info("fail to enable clk: %d\n", ret);
  812. return ret;
  813. }
  814. }
  815. return 0;
  816. }
  817. static int mtk_dma_runtime_suspend(struct device *dev)
  818. {
  819. struct mtk_dmadev *mtkd = dev_get_drvdata(dev);
  820. mtk_dma_clk_disable(mtkd);
  821. return 0;
  822. }
  823. static int mtk_dma_runtime_resume(struct device *dev)
  824. {
  825. int ret;
  826. struct mtk_dmadev *mtkd = dev_get_drvdata(dev);
  827. ret = mtk_dma_clk_enable(mtkd);
  828. if (ret) {
  829. pr_info("fail to enable clk: %d\n", ret);
  830. return ret;
  831. }
  832. return 0;
  833. }
  834. #endif /* CONFIG_PM_SLEEP */
  835. static const struct dev_pm_ops mtk_dma_pm = {
  836. SET_SYSTEM_SLEEP_PM_OPS(mtk_dma_suspend, mtk_dma_resume)
  837. SET_RUNTIME_PM_OPS(mtk_dma_runtime_suspend,
  838. mtk_dma_runtime_resume, NULL)
  839. };
  840. static struct platform_driver mtk_dma_driver = {
  841. .probe = mtk_dma_probe,
  842. .remove = mtk_dma_remove,
  843. .driver = {
  844. .name = "mtk-8250-dma",
  845. .pm = &mtk_dma_pm,
  846. .of_match_table = of_match_ptr(mtk_uart_dma_match),
  847. },
  848. };
  849. static bool mtk_dma_filter_fn(struct dma_chan *chan, void *param)
  850. {
  851. if (chan->device->dev->driver == &mtk_dma_driver.driver) {
  852. struct mtk_dmadev *mtkd = to_mtk_dma_dev(chan->device);
  853. struct mtk_chan *c = to_mtk_dma_chan(chan);
  854. unsigned int req = *(unsigned int *)param;
  855. if (req <= mtkd->dma_requests) {
  856. c->dma_sig = req;
  857. c->dma_ch = req;
  858. return true;
  859. }
  860. }
  861. return false;
  862. }
  863. static int mtk_dma_init(void)
  864. {
  865. return platform_driver_register(&mtk_dma_driver);
  866. }
  867. subsys_initcall(mtk_dma_init);
  868. static void __exit mtk_dma_exit(void)
  869. {
  870. platform_driver_unregister(&mtk_dma_driver);
  871. }
  872. module_exit(mtk_dma_exit);