mtk-hsdma.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017-2018 MediaTek Inc.
  3. /*
  4. * Driver for MediaTek High-Speed DMA Controller
  5. *
  6. * Author: Sean Wang <[email protected]>
  7. *
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/clk.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/err.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/refcount.h>
  23. #include <linux/slab.h>
  24. #include "../virt-dma.h"
  25. #define MTK_HSDMA_USEC_POLL 20
  26. #define MTK_HSDMA_TIMEOUT_POLL 200000
  27. #define MTK_HSDMA_DMA_BUSWIDTHS BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
  28. /* The default number of virtual channel */
  29. #define MTK_HSDMA_NR_VCHANS 3
  30. /* Only one physical channel supported */
  31. #define MTK_HSDMA_NR_MAX_PCHANS 1
  32. /* Macro for physical descriptor (PD) manipulation */
  33. /* The number of PD which must be 2 of power */
  34. #define MTK_DMA_SIZE 64
  35. #define MTK_HSDMA_NEXT_DESP_IDX(x, y) (((x) + 1) & ((y) - 1))
  36. #define MTK_HSDMA_LAST_DESP_IDX(x, y) (((x) - 1) & ((y) - 1))
  37. #define MTK_HSDMA_MAX_LEN 0x3f80
  38. #define MTK_HSDMA_ALIGN_SIZE 4
  39. #define MTK_HSDMA_PLEN_MASK 0x3fff
  40. #define MTK_HSDMA_DESC_PLEN(x) (((x) & MTK_HSDMA_PLEN_MASK) << 16)
  41. #define MTK_HSDMA_DESC_PLEN_GET(x) (((x) >> 16) & MTK_HSDMA_PLEN_MASK)
  42. /* Registers for underlying ring manipulation */
  43. #define MTK_HSDMA_TX_BASE 0x0
  44. #define MTK_HSDMA_TX_CNT 0x4
  45. #define MTK_HSDMA_TX_CPU 0x8
  46. #define MTK_HSDMA_TX_DMA 0xc
  47. #define MTK_HSDMA_RX_BASE 0x100
  48. #define MTK_HSDMA_RX_CNT 0x104
  49. #define MTK_HSDMA_RX_CPU 0x108
  50. #define MTK_HSDMA_RX_DMA 0x10c
  51. /* Registers for global setup */
  52. #define MTK_HSDMA_GLO 0x204
  53. #define MTK_HSDMA_GLO_MULTI_DMA BIT(10)
  54. #define MTK_HSDMA_TX_WB_DDONE BIT(6)
  55. #define MTK_HSDMA_BURST_64BYTES (0x2 << 4)
  56. #define MTK_HSDMA_GLO_RX_BUSY BIT(3)
  57. #define MTK_HSDMA_GLO_RX_DMA BIT(2)
  58. #define MTK_HSDMA_GLO_TX_BUSY BIT(1)
  59. #define MTK_HSDMA_GLO_TX_DMA BIT(0)
  60. #define MTK_HSDMA_GLO_DMA (MTK_HSDMA_GLO_TX_DMA | \
  61. MTK_HSDMA_GLO_RX_DMA)
  62. #define MTK_HSDMA_GLO_BUSY (MTK_HSDMA_GLO_RX_BUSY | \
  63. MTK_HSDMA_GLO_TX_BUSY)
  64. #define MTK_HSDMA_GLO_DEFAULT (MTK_HSDMA_GLO_TX_DMA | \
  65. MTK_HSDMA_GLO_RX_DMA | \
  66. MTK_HSDMA_TX_WB_DDONE | \
  67. MTK_HSDMA_BURST_64BYTES | \
  68. MTK_HSDMA_GLO_MULTI_DMA)
  69. /* Registers for reset */
  70. #define MTK_HSDMA_RESET 0x208
  71. #define MTK_HSDMA_RST_TX BIT(0)
  72. #define MTK_HSDMA_RST_RX BIT(16)
  73. /* Registers for interrupt control */
  74. #define MTK_HSDMA_DLYINT 0x20c
  75. #define MTK_HSDMA_RXDLY_INT_EN BIT(15)
  76. /* Interrupt fires when the pending number's more than the specified */
  77. #define MTK_HSDMA_RXMAX_PINT(x) (((x) & 0x7f) << 8)
  78. /* Interrupt fires when the pending time's more than the specified in 20 us */
  79. #define MTK_HSDMA_RXMAX_PTIME(x) ((x) & 0x7f)
  80. #define MTK_HSDMA_DLYINT_DEFAULT (MTK_HSDMA_RXDLY_INT_EN | \
  81. MTK_HSDMA_RXMAX_PINT(20) | \
  82. MTK_HSDMA_RXMAX_PTIME(20))
  83. #define MTK_HSDMA_INT_STATUS 0x220
  84. #define MTK_HSDMA_INT_ENABLE 0x228
  85. #define MTK_HSDMA_INT_RXDONE BIT(16)
  86. enum mtk_hsdma_vdesc_flag {
  87. MTK_HSDMA_VDESC_FINISHED = 0x01,
  88. };
  89. #define IS_MTK_HSDMA_VDESC_FINISHED(x) ((x) == MTK_HSDMA_VDESC_FINISHED)
  90. /**
  91. * struct mtk_hsdma_pdesc - This is the struct holding info describing physical
  92. * descriptor (PD) and its placement must be kept at
  93. * 4-bytes alignment in little endian order.
  94. * @desc1: | The control pad used to indicate hardware how to
  95. * @desc2: | deal with the descriptor such as source and
  96. * @desc3: | destination address and data length. The maximum
  97. * @desc4: | data length each pdesc can handle is 0x3f80 bytes
  98. */
  99. struct mtk_hsdma_pdesc {
  100. __le32 desc1;
  101. __le32 desc2;
  102. __le32 desc3;
  103. __le32 desc4;
  104. } __packed __aligned(4);
  105. /**
  106. * struct mtk_hsdma_vdesc - This is the struct holding info describing virtual
  107. * descriptor (VD)
  108. * @vd: An instance for struct virt_dma_desc
  109. * @len: The total data size device wants to move
  110. * @residue: The remaining data size device will move
  111. * @dest: The destination address device wants to move to
  112. * @src: The source address device wants to move from
  113. */
  114. struct mtk_hsdma_vdesc {
  115. struct virt_dma_desc vd;
  116. size_t len;
  117. size_t residue;
  118. dma_addr_t dest;
  119. dma_addr_t src;
  120. };
  121. /**
  122. * struct mtk_hsdma_cb - This is the struct holding extra info required for RX
  123. * ring to know what relevant VD the PD is being
  124. * mapped to.
  125. * @vd: Pointer to the relevant VD.
  126. * @flag: Flag indicating what action should be taken when VD
  127. * is completed.
  128. */
  129. struct mtk_hsdma_cb {
  130. struct virt_dma_desc *vd;
  131. enum mtk_hsdma_vdesc_flag flag;
  132. };
  133. /**
  134. * struct mtk_hsdma_ring - This struct holds info describing underlying ring
  135. * space
  136. * @txd: The descriptor TX ring which describes DMA source
  137. * information
  138. * @rxd: The descriptor RX ring which describes DMA
  139. * destination information
  140. * @cb: The extra information pointed at by RX ring
  141. * @tphys: The physical addr of TX ring
  142. * @rphys: The physical addr of RX ring
  143. * @cur_tptr: Pointer to the next free descriptor used by the host
  144. * @cur_rptr: Pointer to the last done descriptor by the device
  145. */
  146. struct mtk_hsdma_ring {
  147. struct mtk_hsdma_pdesc *txd;
  148. struct mtk_hsdma_pdesc *rxd;
  149. struct mtk_hsdma_cb *cb;
  150. dma_addr_t tphys;
  151. dma_addr_t rphys;
  152. u16 cur_tptr;
  153. u16 cur_rptr;
  154. };
  155. /**
  156. * struct mtk_hsdma_pchan - This is the struct holding info describing physical
  157. * channel (PC)
  158. * @ring: An instance for the underlying ring
  159. * @sz_ring: Total size allocated for the ring
  160. * @nr_free: Total number of free rooms in the ring. It would
  161. * be accessed and updated frequently between IRQ
  162. * context and user context to reflect whether ring
  163. * can accept requests from VD.
  164. */
  165. struct mtk_hsdma_pchan {
  166. struct mtk_hsdma_ring ring;
  167. size_t sz_ring;
  168. atomic_t nr_free;
  169. };
  170. /**
  171. * struct mtk_hsdma_vchan - This is the struct holding info describing virtual
  172. * channel (VC)
  173. * @vc: An instance for struct virt_dma_chan
  174. * @issue_completion: The wait for all issued descriptors completited
  175. * @issue_synchronize: Bool indicating channel synchronization starts
  176. * @desc_hw_processing: List those descriptors the hardware is processing,
  177. * which is protected by vc.lock
  178. */
  179. struct mtk_hsdma_vchan {
  180. struct virt_dma_chan vc;
  181. struct completion issue_completion;
  182. bool issue_synchronize;
  183. struct list_head desc_hw_processing;
  184. };
  185. /**
  186. * struct mtk_hsdma_soc - This is the struct holding differences among SoCs
  187. * @ddone: Bit mask for DDONE
  188. * @ls0: Bit mask for LS0
  189. */
  190. struct mtk_hsdma_soc {
  191. __le32 ddone;
  192. __le32 ls0;
  193. };
  194. /**
  195. * struct mtk_hsdma_device - This is the struct holding info describing HSDMA
  196. * device
  197. * @ddev: An instance for struct dma_device
  198. * @base: The mapped register I/O base
  199. * @clk: The clock that device internal is using
  200. * @irq: The IRQ that device are using
  201. * @dma_requests: The number of VCs the device supports to
  202. * @vc: The pointer to all available VCs
  203. * @pc: The pointer to the underlying PC
  204. * @pc_refcnt: Track how many VCs are using the PC
  205. * @lock: Lock protect agaisting multiple VCs access PC
  206. * @soc: The pointer to area holding differences among
  207. * vaious platform
  208. */
  209. struct mtk_hsdma_device {
  210. struct dma_device ddev;
  211. void __iomem *base;
  212. struct clk *clk;
  213. u32 irq;
  214. u32 dma_requests;
  215. struct mtk_hsdma_vchan *vc;
  216. struct mtk_hsdma_pchan *pc;
  217. refcount_t pc_refcnt;
  218. /* Lock used to protect against multiple VCs access PC */
  219. spinlock_t lock;
  220. const struct mtk_hsdma_soc *soc;
  221. };
  222. static struct mtk_hsdma_device *to_hsdma_dev(struct dma_chan *chan)
  223. {
  224. return container_of(chan->device, struct mtk_hsdma_device, ddev);
  225. }
  226. static inline struct mtk_hsdma_vchan *to_hsdma_vchan(struct dma_chan *chan)
  227. {
  228. return container_of(chan, struct mtk_hsdma_vchan, vc.chan);
  229. }
  230. static struct mtk_hsdma_vdesc *to_hsdma_vdesc(struct virt_dma_desc *vd)
  231. {
  232. return container_of(vd, struct mtk_hsdma_vdesc, vd);
  233. }
  234. static struct device *hsdma2dev(struct mtk_hsdma_device *hsdma)
  235. {
  236. return hsdma->ddev.dev;
  237. }
  238. static u32 mtk_dma_read(struct mtk_hsdma_device *hsdma, u32 reg)
  239. {
  240. return readl(hsdma->base + reg);
  241. }
  242. static void mtk_dma_write(struct mtk_hsdma_device *hsdma, u32 reg, u32 val)
  243. {
  244. writel(val, hsdma->base + reg);
  245. }
  246. static void mtk_dma_rmw(struct mtk_hsdma_device *hsdma, u32 reg,
  247. u32 mask, u32 set)
  248. {
  249. u32 val;
  250. val = mtk_dma_read(hsdma, reg);
  251. val &= ~mask;
  252. val |= set;
  253. mtk_dma_write(hsdma, reg, val);
  254. }
  255. static void mtk_dma_set(struct mtk_hsdma_device *hsdma, u32 reg, u32 val)
  256. {
  257. mtk_dma_rmw(hsdma, reg, 0, val);
  258. }
  259. static void mtk_dma_clr(struct mtk_hsdma_device *hsdma, u32 reg, u32 val)
  260. {
  261. mtk_dma_rmw(hsdma, reg, val, 0);
  262. }
  263. static void mtk_hsdma_vdesc_free(struct virt_dma_desc *vd)
  264. {
  265. kfree(container_of(vd, struct mtk_hsdma_vdesc, vd));
  266. }
  267. static int mtk_hsdma_busy_wait(struct mtk_hsdma_device *hsdma)
  268. {
  269. u32 status = 0;
  270. return readl_poll_timeout(hsdma->base + MTK_HSDMA_GLO, status,
  271. !(status & MTK_HSDMA_GLO_BUSY),
  272. MTK_HSDMA_USEC_POLL,
  273. MTK_HSDMA_TIMEOUT_POLL);
  274. }
  275. static int mtk_hsdma_alloc_pchan(struct mtk_hsdma_device *hsdma,
  276. struct mtk_hsdma_pchan *pc)
  277. {
  278. struct mtk_hsdma_ring *ring = &pc->ring;
  279. int err;
  280. memset(pc, 0, sizeof(*pc));
  281. /*
  282. * Allocate ring space where [0 ... MTK_DMA_SIZE - 1] is for TX ring
  283. * and [MTK_DMA_SIZE ... 2 * MTK_DMA_SIZE - 1] is for RX ring.
  284. */
  285. pc->sz_ring = 2 * MTK_DMA_SIZE * sizeof(*ring->txd);
  286. ring->txd = dma_alloc_coherent(hsdma2dev(hsdma), pc->sz_ring,
  287. &ring->tphys, GFP_NOWAIT);
  288. if (!ring->txd)
  289. return -ENOMEM;
  290. ring->rxd = &ring->txd[MTK_DMA_SIZE];
  291. ring->rphys = ring->tphys + MTK_DMA_SIZE * sizeof(*ring->txd);
  292. ring->cur_tptr = 0;
  293. ring->cur_rptr = MTK_DMA_SIZE - 1;
  294. ring->cb = kcalloc(MTK_DMA_SIZE, sizeof(*ring->cb), GFP_NOWAIT);
  295. if (!ring->cb) {
  296. err = -ENOMEM;
  297. goto err_free_dma;
  298. }
  299. atomic_set(&pc->nr_free, MTK_DMA_SIZE - 1);
  300. /* Disable HSDMA and wait for the completion */
  301. mtk_dma_clr(hsdma, MTK_HSDMA_GLO, MTK_HSDMA_GLO_DMA);
  302. err = mtk_hsdma_busy_wait(hsdma);
  303. if (err)
  304. goto err_free_cb;
  305. /* Reset */
  306. mtk_dma_set(hsdma, MTK_HSDMA_RESET,
  307. MTK_HSDMA_RST_TX | MTK_HSDMA_RST_RX);
  308. mtk_dma_clr(hsdma, MTK_HSDMA_RESET,
  309. MTK_HSDMA_RST_TX | MTK_HSDMA_RST_RX);
  310. /* Setup HSDMA initial pointer in the ring */
  311. mtk_dma_write(hsdma, MTK_HSDMA_TX_BASE, ring->tphys);
  312. mtk_dma_write(hsdma, MTK_HSDMA_TX_CNT, MTK_DMA_SIZE);
  313. mtk_dma_write(hsdma, MTK_HSDMA_TX_CPU, ring->cur_tptr);
  314. mtk_dma_write(hsdma, MTK_HSDMA_TX_DMA, 0);
  315. mtk_dma_write(hsdma, MTK_HSDMA_RX_BASE, ring->rphys);
  316. mtk_dma_write(hsdma, MTK_HSDMA_RX_CNT, MTK_DMA_SIZE);
  317. mtk_dma_write(hsdma, MTK_HSDMA_RX_CPU, ring->cur_rptr);
  318. mtk_dma_write(hsdma, MTK_HSDMA_RX_DMA, 0);
  319. /* Enable HSDMA */
  320. mtk_dma_set(hsdma, MTK_HSDMA_GLO, MTK_HSDMA_GLO_DMA);
  321. /* Setup delayed interrupt */
  322. mtk_dma_write(hsdma, MTK_HSDMA_DLYINT, MTK_HSDMA_DLYINT_DEFAULT);
  323. /* Enable interrupt */
  324. mtk_dma_set(hsdma, MTK_HSDMA_INT_ENABLE, MTK_HSDMA_INT_RXDONE);
  325. return 0;
  326. err_free_cb:
  327. kfree(ring->cb);
  328. err_free_dma:
  329. dma_free_coherent(hsdma2dev(hsdma),
  330. pc->sz_ring, ring->txd, ring->tphys);
  331. return err;
  332. }
  333. static void mtk_hsdma_free_pchan(struct mtk_hsdma_device *hsdma,
  334. struct mtk_hsdma_pchan *pc)
  335. {
  336. struct mtk_hsdma_ring *ring = &pc->ring;
  337. /* Disable HSDMA and then wait for the completion */
  338. mtk_dma_clr(hsdma, MTK_HSDMA_GLO, MTK_HSDMA_GLO_DMA);
  339. mtk_hsdma_busy_wait(hsdma);
  340. /* Reset pointer in the ring */
  341. mtk_dma_clr(hsdma, MTK_HSDMA_INT_ENABLE, MTK_HSDMA_INT_RXDONE);
  342. mtk_dma_write(hsdma, MTK_HSDMA_TX_BASE, 0);
  343. mtk_dma_write(hsdma, MTK_HSDMA_TX_CNT, 0);
  344. mtk_dma_write(hsdma, MTK_HSDMA_TX_CPU, 0);
  345. mtk_dma_write(hsdma, MTK_HSDMA_RX_BASE, 0);
  346. mtk_dma_write(hsdma, MTK_HSDMA_RX_CNT, 0);
  347. mtk_dma_write(hsdma, MTK_HSDMA_RX_CPU, MTK_DMA_SIZE - 1);
  348. kfree(ring->cb);
  349. dma_free_coherent(hsdma2dev(hsdma),
  350. pc->sz_ring, ring->txd, ring->tphys);
  351. }
  352. static int mtk_hsdma_issue_pending_vdesc(struct mtk_hsdma_device *hsdma,
  353. struct mtk_hsdma_pchan *pc,
  354. struct mtk_hsdma_vdesc *hvd)
  355. {
  356. struct mtk_hsdma_ring *ring = &pc->ring;
  357. struct mtk_hsdma_pdesc *txd, *rxd;
  358. u16 reserved, prev, tlen, num_sgs;
  359. unsigned long flags;
  360. /* Protect against PC is accessed by multiple VCs simultaneously */
  361. spin_lock_irqsave(&hsdma->lock, flags);
  362. /*
  363. * Reserve rooms, where pc->nr_free is used to track how many free
  364. * rooms in the ring being updated in user and IRQ context.
  365. */
  366. num_sgs = DIV_ROUND_UP(hvd->len, MTK_HSDMA_MAX_LEN);
  367. reserved = min_t(u16, num_sgs, atomic_read(&pc->nr_free));
  368. if (!reserved) {
  369. spin_unlock_irqrestore(&hsdma->lock, flags);
  370. return -ENOSPC;
  371. }
  372. atomic_sub(reserved, &pc->nr_free);
  373. while (reserved--) {
  374. /* Limit size by PD capability for valid data moving */
  375. tlen = (hvd->len > MTK_HSDMA_MAX_LEN) ?
  376. MTK_HSDMA_MAX_LEN : hvd->len;
  377. /*
  378. * Setup PDs using the remaining VD info mapped on those
  379. * reserved rooms. And since RXD is shared memory between the
  380. * host and the device allocated by dma_alloc_coherent call,
  381. * the helper macro WRITE_ONCE can ensure the data written to
  382. * RAM would really happens.
  383. */
  384. txd = &ring->txd[ring->cur_tptr];
  385. WRITE_ONCE(txd->desc1, hvd->src);
  386. WRITE_ONCE(txd->desc2,
  387. hsdma->soc->ls0 | MTK_HSDMA_DESC_PLEN(tlen));
  388. rxd = &ring->rxd[ring->cur_tptr];
  389. WRITE_ONCE(rxd->desc1, hvd->dest);
  390. WRITE_ONCE(rxd->desc2, MTK_HSDMA_DESC_PLEN(tlen));
  391. /* Associate VD, the PD belonged to */
  392. ring->cb[ring->cur_tptr].vd = &hvd->vd;
  393. /* Move forward the pointer of TX ring */
  394. ring->cur_tptr = MTK_HSDMA_NEXT_DESP_IDX(ring->cur_tptr,
  395. MTK_DMA_SIZE);
  396. /* Update VD with remaining data */
  397. hvd->src += tlen;
  398. hvd->dest += tlen;
  399. hvd->len -= tlen;
  400. }
  401. /*
  402. * Tagging flag for the last PD for VD will be responsible for
  403. * completing VD.
  404. */
  405. if (!hvd->len) {
  406. prev = MTK_HSDMA_LAST_DESP_IDX(ring->cur_tptr, MTK_DMA_SIZE);
  407. ring->cb[prev].flag = MTK_HSDMA_VDESC_FINISHED;
  408. }
  409. /* Ensure all changes indeed done before we're going on */
  410. wmb();
  411. /*
  412. * Updating into hardware the pointer of TX ring lets HSDMA to take
  413. * action for those pending PDs.
  414. */
  415. mtk_dma_write(hsdma, MTK_HSDMA_TX_CPU, ring->cur_tptr);
  416. spin_unlock_irqrestore(&hsdma->lock, flags);
  417. return 0;
  418. }
  419. static void mtk_hsdma_issue_vchan_pending(struct mtk_hsdma_device *hsdma,
  420. struct mtk_hsdma_vchan *hvc)
  421. {
  422. struct virt_dma_desc *vd, *vd2;
  423. int err;
  424. lockdep_assert_held(&hvc->vc.lock);
  425. list_for_each_entry_safe(vd, vd2, &hvc->vc.desc_issued, node) {
  426. struct mtk_hsdma_vdesc *hvd;
  427. hvd = to_hsdma_vdesc(vd);
  428. /* Map VD into PC and all VCs shares a single PC */
  429. err = mtk_hsdma_issue_pending_vdesc(hsdma, hsdma->pc, hvd);
  430. /*
  431. * Move VD from desc_issued to desc_hw_processing when entire
  432. * VD is fit into available PDs. Otherwise, the uncompleted
  433. * VDs would stay in list desc_issued and then restart the
  434. * processing as soon as possible once underlying ring space
  435. * got freed.
  436. */
  437. if (err == -ENOSPC || hvd->len > 0)
  438. break;
  439. /*
  440. * The extra list desc_hw_processing is used because
  441. * hardware can't provide sufficient information allowing us
  442. * to know what VDs are still working on the underlying ring.
  443. * Through the additional list, it can help us to implement
  444. * terminate_all, residue calculation and such thing needed
  445. * to know detail descriptor status on the hardware.
  446. */
  447. list_move_tail(&vd->node, &hvc->desc_hw_processing);
  448. }
  449. }
  450. static void mtk_hsdma_free_rooms_in_ring(struct mtk_hsdma_device *hsdma)
  451. {
  452. struct mtk_hsdma_vchan *hvc;
  453. struct mtk_hsdma_pdesc *rxd;
  454. struct mtk_hsdma_vdesc *hvd;
  455. struct mtk_hsdma_pchan *pc;
  456. struct mtk_hsdma_cb *cb;
  457. int i = MTK_DMA_SIZE;
  458. __le32 desc2;
  459. u32 status;
  460. u16 next;
  461. /* Read IRQ status */
  462. status = mtk_dma_read(hsdma, MTK_HSDMA_INT_STATUS);
  463. if (unlikely(!(status & MTK_HSDMA_INT_RXDONE)))
  464. goto rx_done;
  465. pc = hsdma->pc;
  466. /*
  467. * Using a fail-safe loop with iterations of up to MTK_DMA_SIZE to
  468. * reclaim these finished descriptors: The most number of PDs the ISR
  469. * can handle at one time shouldn't be more than MTK_DMA_SIZE so we
  470. * take it as limited count instead of just using a dangerous infinite
  471. * poll.
  472. */
  473. while (i--) {
  474. next = MTK_HSDMA_NEXT_DESP_IDX(pc->ring.cur_rptr,
  475. MTK_DMA_SIZE);
  476. rxd = &pc->ring.rxd[next];
  477. /*
  478. * If MTK_HSDMA_DESC_DDONE is no specified, that means data
  479. * moving for the PD is still under going.
  480. */
  481. desc2 = READ_ONCE(rxd->desc2);
  482. if (!(desc2 & hsdma->soc->ddone))
  483. break;
  484. cb = &pc->ring.cb[next];
  485. if (unlikely(!cb->vd)) {
  486. dev_err(hsdma2dev(hsdma), "cb->vd cannot be null\n");
  487. break;
  488. }
  489. /* Update residue of VD the associated PD belonged to */
  490. hvd = to_hsdma_vdesc(cb->vd);
  491. hvd->residue -= MTK_HSDMA_DESC_PLEN_GET(rxd->desc2);
  492. /* Complete VD until the relevant last PD is finished */
  493. if (IS_MTK_HSDMA_VDESC_FINISHED(cb->flag)) {
  494. hvc = to_hsdma_vchan(cb->vd->tx.chan);
  495. spin_lock(&hvc->vc.lock);
  496. /* Remove VD from list desc_hw_processing */
  497. list_del(&cb->vd->node);
  498. /* Add VD into list desc_completed */
  499. vchan_cookie_complete(cb->vd);
  500. if (hvc->issue_synchronize &&
  501. list_empty(&hvc->desc_hw_processing)) {
  502. complete(&hvc->issue_completion);
  503. hvc->issue_synchronize = false;
  504. }
  505. spin_unlock(&hvc->vc.lock);
  506. cb->flag = 0;
  507. }
  508. cb->vd = NULL;
  509. /*
  510. * Recycle the RXD with the helper WRITE_ONCE that can ensure
  511. * data written into RAM would really happens.
  512. */
  513. WRITE_ONCE(rxd->desc1, 0);
  514. WRITE_ONCE(rxd->desc2, 0);
  515. pc->ring.cur_rptr = next;
  516. /* Release rooms */
  517. atomic_inc(&pc->nr_free);
  518. }
  519. /* Ensure all changes indeed done before we're going on */
  520. wmb();
  521. /* Update CPU pointer for those completed PDs */
  522. mtk_dma_write(hsdma, MTK_HSDMA_RX_CPU, pc->ring.cur_rptr);
  523. /*
  524. * Acking the pending IRQ allows hardware no longer to keep the used
  525. * IRQ line in certain trigger state when software has completed all
  526. * the finished physical descriptors.
  527. */
  528. if (atomic_read(&pc->nr_free) >= MTK_DMA_SIZE - 1)
  529. mtk_dma_write(hsdma, MTK_HSDMA_INT_STATUS, status);
  530. /* ASAP handles pending VDs in all VCs after freeing some rooms */
  531. for (i = 0; i < hsdma->dma_requests; i++) {
  532. hvc = &hsdma->vc[i];
  533. spin_lock(&hvc->vc.lock);
  534. mtk_hsdma_issue_vchan_pending(hsdma, hvc);
  535. spin_unlock(&hvc->vc.lock);
  536. }
  537. rx_done:
  538. /* All completed PDs are cleaned up, so enable interrupt again */
  539. mtk_dma_set(hsdma, MTK_HSDMA_INT_ENABLE, MTK_HSDMA_INT_RXDONE);
  540. }
  541. static irqreturn_t mtk_hsdma_irq(int irq, void *devid)
  542. {
  543. struct mtk_hsdma_device *hsdma = devid;
  544. /*
  545. * Disable interrupt until all completed PDs are cleaned up in
  546. * mtk_hsdma_free_rooms call.
  547. */
  548. mtk_dma_clr(hsdma, MTK_HSDMA_INT_ENABLE, MTK_HSDMA_INT_RXDONE);
  549. mtk_hsdma_free_rooms_in_ring(hsdma);
  550. return IRQ_HANDLED;
  551. }
  552. static struct virt_dma_desc *mtk_hsdma_find_active_desc(struct dma_chan *c,
  553. dma_cookie_t cookie)
  554. {
  555. struct mtk_hsdma_vchan *hvc = to_hsdma_vchan(c);
  556. struct virt_dma_desc *vd;
  557. list_for_each_entry(vd, &hvc->desc_hw_processing, node)
  558. if (vd->tx.cookie == cookie)
  559. return vd;
  560. list_for_each_entry(vd, &hvc->vc.desc_issued, node)
  561. if (vd->tx.cookie == cookie)
  562. return vd;
  563. return NULL;
  564. }
  565. static enum dma_status mtk_hsdma_tx_status(struct dma_chan *c,
  566. dma_cookie_t cookie,
  567. struct dma_tx_state *txstate)
  568. {
  569. struct mtk_hsdma_vchan *hvc = to_hsdma_vchan(c);
  570. struct mtk_hsdma_vdesc *hvd;
  571. struct virt_dma_desc *vd;
  572. enum dma_status ret;
  573. unsigned long flags;
  574. size_t bytes = 0;
  575. ret = dma_cookie_status(c, cookie, txstate);
  576. if (ret == DMA_COMPLETE || !txstate)
  577. return ret;
  578. spin_lock_irqsave(&hvc->vc.lock, flags);
  579. vd = mtk_hsdma_find_active_desc(c, cookie);
  580. spin_unlock_irqrestore(&hvc->vc.lock, flags);
  581. if (vd) {
  582. hvd = to_hsdma_vdesc(vd);
  583. bytes = hvd->residue;
  584. }
  585. dma_set_residue(txstate, bytes);
  586. return ret;
  587. }
  588. static void mtk_hsdma_issue_pending(struct dma_chan *c)
  589. {
  590. struct mtk_hsdma_device *hsdma = to_hsdma_dev(c);
  591. struct mtk_hsdma_vchan *hvc = to_hsdma_vchan(c);
  592. unsigned long flags;
  593. spin_lock_irqsave(&hvc->vc.lock, flags);
  594. if (vchan_issue_pending(&hvc->vc))
  595. mtk_hsdma_issue_vchan_pending(hsdma, hvc);
  596. spin_unlock_irqrestore(&hvc->vc.lock, flags);
  597. }
  598. static struct dma_async_tx_descriptor *
  599. mtk_hsdma_prep_dma_memcpy(struct dma_chan *c, dma_addr_t dest,
  600. dma_addr_t src, size_t len, unsigned long flags)
  601. {
  602. struct mtk_hsdma_vdesc *hvd;
  603. hvd = kzalloc(sizeof(*hvd), GFP_NOWAIT);
  604. if (!hvd)
  605. return NULL;
  606. hvd->len = len;
  607. hvd->residue = len;
  608. hvd->src = src;
  609. hvd->dest = dest;
  610. return vchan_tx_prep(to_virt_chan(c), &hvd->vd, flags);
  611. }
  612. static int mtk_hsdma_free_inactive_desc(struct dma_chan *c)
  613. {
  614. struct virt_dma_chan *vc = to_virt_chan(c);
  615. unsigned long flags;
  616. LIST_HEAD(head);
  617. spin_lock_irqsave(&vc->lock, flags);
  618. list_splice_tail_init(&vc->desc_allocated, &head);
  619. list_splice_tail_init(&vc->desc_submitted, &head);
  620. list_splice_tail_init(&vc->desc_issued, &head);
  621. spin_unlock_irqrestore(&vc->lock, flags);
  622. /* At the point, we don't expect users put descriptor into VC again */
  623. vchan_dma_desc_free_list(vc, &head);
  624. return 0;
  625. }
  626. static void mtk_hsdma_free_active_desc(struct dma_chan *c)
  627. {
  628. struct mtk_hsdma_vchan *hvc = to_hsdma_vchan(c);
  629. bool sync_needed = false;
  630. /*
  631. * Once issue_synchronize is being set, which means once the hardware
  632. * consumes all descriptors for the channel in the ring, the
  633. * synchronization must be notified immediately it is completed.
  634. */
  635. spin_lock(&hvc->vc.lock);
  636. if (!list_empty(&hvc->desc_hw_processing)) {
  637. hvc->issue_synchronize = true;
  638. sync_needed = true;
  639. }
  640. spin_unlock(&hvc->vc.lock);
  641. if (sync_needed)
  642. wait_for_completion(&hvc->issue_completion);
  643. /*
  644. * At the point, we expect that all remaining descriptors in the ring
  645. * for the channel should be all processing done.
  646. */
  647. WARN_ONCE(!list_empty(&hvc->desc_hw_processing),
  648. "Desc pending still in list desc_hw_processing\n");
  649. /* Free all descriptors in list desc_completed */
  650. vchan_synchronize(&hvc->vc);
  651. WARN_ONCE(!list_empty(&hvc->vc.desc_completed),
  652. "Desc pending still in list desc_completed\n");
  653. }
  654. static int mtk_hsdma_terminate_all(struct dma_chan *c)
  655. {
  656. /*
  657. * Free pending descriptors not processed yet by hardware that have
  658. * previously been submitted to the channel.
  659. */
  660. mtk_hsdma_free_inactive_desc(c);
  661. /*
  662. * However, the DMA engine doesn't provide any way to stop these
  663. * descriptors being processed currently by hardware. The only way is
  664. * to just waiting until these descriptors are all processed completely
  665. * through mtk_hsdma_free_active_desc call.
  666. */
  667. mtk_hsdma_free_active_desc(c);
  668. return 0;
  669. }
  670. static int mtk_hsdma_alloc_chan_resources(struct dma_chan *c)
  671. {
  672. struct mtk_hsdma_device *hsdma = to_hsdma_dev(c);
  673. int err;
  674. /*
  675. * Since HSDMA has only one PC, the resource for PC is being allocated
  676. * when the first VC is being created and the other VCs would run on
  677. * the same PC.
  678. */
  679. if (!refcount_read(&hsdma->pc_refcnt)) {
  680. err = mtk_hsdma_alloc_pchan(hsdma, hsdma->pc);
  681. if (err)
  682. return err;
  683. /*
  684. * refcount_inc would complain increment on 0; use-after-free.
  685. * Thus, we need to explicitly set it as 1 initially.
  686. */
  687. refcount_set(&hsdma->pc_refcnt, 1);
  688. } else {
  689. refcount_inc(&hsdma->pc_refcnt);
  690. }
  691. return 0;
  692. }
  693. static void mtk_hsdma_free_chan_resources(struct dma_chan *c)
  694. {
  695. struct mtk_hsdma_device *hsdma = to_hsdma_dev(c);
  696. /* Free all descriptors in all lists on the VC */
  697. mtk_hsdma_terminate_all(c);
  698. /* The resource for PC is not freed until all the VCs are destroyed */
  699. if (!refcount_dec_and_test(&hsdma->pc_refcnt))
  700. return;
  701. mtk_hsdma_free_pchan(hsdma, hsdma->pc);
  702. }
  703. static int mtk_hsdma_hw_init(struct mtk_hsdma_device *hsdma)
  704. {
  705. int err;
  706. pm_runtime_enable(hsdma2dev(hsdma));
  707. pm_runtime_get_sync(hsdma2dev(hsdma));
  708. err = clk_prepare_enable(hsdma->clk);
  709. if (err)
  710. return err;
  711. mtk_dma_write(hsdma, MTK_HSDMA_INT_ENABLE, 0);
  712. mtk_dma_write(hsdma, MTK_HSDMA_GLO, MTK_HSDMA_GLO_DEFAULT);
  713. return 0;
  714. }
  715. static int mtk_hsdma_hw_deinit(struct mtk_hsdma_device *hsdma)
  716. {
  717. mtk_dma_write(hsdma, MTK_HSDMA_GLO, 0);
  718. clk_disable_unprepare(hsdma->clk);
  719. pm_runtime_put_sync(hsdma2dev(hsdma));
  720. pm_runtime_disable(hsdma2dev(hsdma));
  721. return 0;
  722. }
  723. static const struct mtk_hsdma_soc mt7623_soc = {
  724. .ddone = BIT(31),
  725. .ls0 = BIT(30),
  726. };
  727. static const struct mtk_hsdma_soc mt7622_soc = {
  728. .ddone = BIT(15),
  729. .ls0 = BIT(14),
  730. };
  731. static const struct of_device_id mtk_hsdma_match[] = {
  732. { .compatible = "mediatek,mt7623-hsdma", .data = &mt7623_soc},
  733. { .compatible = "mediatek,mt7622-hsdma", .data = &mt7622_soc},
  734. { /* sentinel */ }
  735. };
  736. MODULE_DEVICE_TABLE(of, mtk_hsdma_match);
  737. static int mtk_hsdma_probe(struct platform_device *pdev)
  738. {
  739. struct mtk_hsdma_device *hsdma;
  740. struct mtk_hsdma_vchan *vc;
  741. struct dma_device *dd;
  742. struct resource *res;
  743. int i, err;
  744. hsdma = devm_kzalloc(&pdev->dev, sizeof(*hsdma), GFP_KERNEL);
  745. if (!hsdma)
  746. return -ENOMEM;
  747. dd = &hsdma->ddev;
  748. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  749. hsdma->base = devm_ioremap_resource(&pdev->dev, res);
  750. if (IS_ERR(hsdma->base))
  751. return PTR_ERR(hsdma->base);
  752. hsdma->soc = of_device_get_match_data(&pdev->dev);
  753. if (!hsdma->soc) {
  754. dev_err(&pdev->dev, "No device match found\n");
  755. return -ENODEV;
  756. }
  757. hsdma->clk = devm_clk_get(&pdev->dev, "hsdma");
  758. if (IS_ERR(hsdma->clk)) {
  759. dev_err(&pdev->dev, "No clock for %s\n",
  760. dev_name(&pdev->dev));
  761. return PTR_ERR(hsdma->clk);
  762. }
  763. err = platform_get_irq(pdev, 0);
  764. if (err < 0)
  765. return err;
  766. hsdma->irq = err;
  767. refcount_set(&hsdma->pc_refcnt, 0);
  768. spin_lock_init(&hsdma->lock);
  769. dma_cap_set(DMA_MEMCPY, dd->cap_mask);
  770. dd->copy_align = MTK_HSDMA_ALIGN_SIZE;
  771. dd->device_alloc_chan_resources = mtk_hsdma_alloc_chan_resources;
  772. dd->device_free_chan_resources = mtk_hsdma_free_chan_resources;
  773. dd->device_tx_status = mtk_hsdma_tx_status;
  774. dd->device_issue_pending = mtk_hsdma_issue_pending;
  775. dd->device_prep_dma_memcpy = mtk_hsdma_prep_dma_memcpy;
  776. dd->device_terminate_all = mtk_hsdma_terminate_all;
  777. dd->src_addr_widths = MTK_HSDMA_DMA_BUSWIDTHS;
  778. dd->dst_addr_widths = MTK_HSDMA_DMA_BUSWIDTHS;
  779. dd->directions = BIT(DMA_MEM_TO_MEM);
  780. dd->residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  781. dd->dev = &pdev->dev;
  782. INIT_LIST_HEAD(&dd->channels);
  783. hsdma->dma_requests = MTK_HSDMA_NR_VCHANS;
  784. if (pdev->dev.of_node && of_property_read_u32(pdev->dev.of_node,
  785. "dma-requests",
  786. &hsdma->dma_requests)) {
  787. dev_info(&pdev->dev,
  788. "Using %u as missing dma-requests property\n",
  789. MTK_HSDMA_NR_VCHANS);
  790. }
  791. hsdma->pc = devm_kcalloc(&pdev->dev, MTK_HSDMA_NR_MAX_PCHANS,
  792. sizeof(*hsdma->pc), GFP_KERNEL);
  793. if (!hsdma->pc)
  794. return -ENOMEM;
  795. hsdma->vc = devm_kcalloc(&pdev->dev, hsdma->dma_requests,
  796. sizeof(*hsdma->vc), GFP_KERNEL);
  797. if (!hsdma->vc)
  798. return -ENOMEM;
  799. for (i = 0; i < hsdma->dma_requests; i++) {
  800. vc = &hsdma->vc[i];
  801. vc->vc.desc_free = mtk_hsdma_vdesc_free;
  802. vchan_init(&vc->vc, dd);
  803. init_completion(&vc->issue_completion);
  804. INIT_LIST_HEAD(&vc->desc_hw_processing);
  805. }
  806. err = dma_async_device_register(dd);
  807. if (err)
  808. return err;
  809. err = of_dma_controller_register(pdev->dev.of_node,
  810. of_dma_xlate_by_chan_id, hsdma);
  811. if (err) {
  812. dev_err(&pdev->dev,
  813. "MediaTek HSDMA OF registration failed %d\n", err);
  814. goto err_unregister;
  815. }
  816. mtk_hsdma_hw_init(hsdma);
  817. err = devm_request_irq(&pdev->dev, hsdma->irq,
  818. mtk_hsdma_irq, 0,
  819. dev_name(&pdev->dev), hsdma);
  820. if (err) {
  821. dev_err(&pdev->dev,
  822. "request_irq failed with err %d\n", err);
  823. goto err_free;
  824. }
  825. platform_set_drvdata(pdev, hsdma);
  826. dev_info(&pdev->dev, "MediaTek HSDMA driver registered\n");
  827. return 0;
  828. err_free:
  829. mtk_hsdma_hw_deinit(hsdma);
  830. of_dma_controller_free(pdev->dev.of_node);
  831. err_unregister:
  832. dma_async_device_unregister(dd);
  833. return err;
  834. }
  835. static int mtk_hsdma_remove(struct platform_device *pdev)
  836. {
  837. struct mtk_hsdma_device *hsdma = platform_get_drvdata(pdev);
  838. struct mtk_hsdma_vchan *vc;
  839. int i;
  840. /* Kill VC task */
  841. for (i = 0; i < hsdma->dma_requests; i++) {
  842. vc = &hsdma->vc[i];
  843. list_del(&vc->vc.chan.device_node);
  844. tasklet_kill(&vc->vc.task);
  845. }
  846. /* Disable DMA interrupt */
  847. mtk_dma_write(hsdma, MTK_HSDMA_INT_ENABLE, 0);
  848. /* Waits for any pending IRQ handlers to complete */
  849. synchronize_irq(hsdma->irq);
  850. /* Disable hardware */
  851. mtk_hsdma_hw_deinit(hsdma);
  852. dma_async_device_unregister(&hsdma->ddev);
  853. of_dma_controller_free(pdev->dev.of_node);
  854. return 0;
  855. }
  856. static struct platform_driver mtk_hsdma_driver = {
  857. .probe = mtk_hsdma_probe,
  858. .remove = mtk_hsdma_remove,
  859. .driver = {
  860. .name = KBUILD_MODNAME,
  861. .of_match_table = mtk_hsdma_match,
  862. },
  863. };
  864. module_platform_driver(mtk_hsdma_driver);
  865. MODULE_DESCRIPTION("MediaTek High-Speed DMA Controller Driver");
  866. MODULE_AUTHOR("Sean Wang <[email protected]>");
  867. MODULE_LICENSE("GPL v2");