k3dma.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * Copyright (c) 2013 - 2015 Linaro Ltd.
  3. * Copyright (c) 2013 Hisilicon Limited.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/device.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of.h>
  23. #include <linux/clk.h>
  24. #include <linux/of_dma.h>
  25. #include "virt-dma.h"
  26. #define DRIVER_NAME "k3-dma"
  27. #define DMA_MAX_SIZE 0x1ffc
  28. #define DMA_CYCLIC_MAX_PERIOD 0x1000
  29. #define LLI_BLOCK_SIZE (4 * PAGE_SIZE)
  30. #define INT_STAT 0x00
  31. #define INT_TC1 0x04
  32. #define INT_TC2 0x08
  33. #define INT_ERR1 0x0c
  34. #define INT_ERR2 0x10
  35. #define INT_TC1_MASK 0x18
  36. #define INT_TC2_MASK 0x1c
  37. #define INT_ERR1_MASK 0x20
  38. #define INT_ERR2_MASK 0x24
  39. #define INT_TC1_RAW 0x600
  40. #define INT_TC2_RAW 0x608
  41. #define INT_ERR1_RAW 0x610
  42. #define INT_ERR2_RAW 0x618
  43. #define CH_PRI 0x688
  44. #define CH_STAT 0x690
  45. #define CX_CUR_CNT 0x704
  46. #define CX_LLI 0x800
  47. #define CX_CNT1 0x80c
  48. #define CX_CNT0 0x810
  49. #define CX_SRC 0x814
  50. #define CX_DST 0x818
  51. #define CX_CFG 0x81c
  52. #define AXI_CFG 0x820
  53. #define AXI_CFG_DEFAULT 0x201201
  54. #define CX_LLI_CHAIN_EN 0x2
  55. #define CX_CFG_EN 0x1
  56. #define CX_CFG_NODEIRQ BIT(1)
  57. #define CX_CFG_MEM2PER (0x1 << 2)
  58. #define CX_CFG_PER2MEM (0x2 << 2)
  59. #define CX_CFG_SRCINCR (0x1 << 31)
  60. #define CX_CFG_DSTINCR (0x1 << 30)
  61. struct k3_desc_hw {
  62. u32 lli;
  63. u32 reserved[3];
  64. u32 count;
  65. u32 saddr;
  66. u32 daddr;
  67. u32 config;
  68. } __aligned(32);
  69. struct k3_dma_desc_sw {
  70. struct virt_dma_desc vd;
  71. dma_addr_t desc_hw_lli;
  72. size_t desc_num;
  73. size_t size;
  74. struct k3_desc_hw *desc_hw;
  75. };
  76. struct k3_dma_phy;
  77. struct k3_dma_chan {
  78. u32 ccfg;
  79. struct virt_dma_chan vc;
  80. struct k3_dma_phy *phy;
  81. struct list_head node;
  82. enum dma_transfer_direction dir;
  83. dma_addr_t dev_addr;
  84. enum dma_status status;
  85. bool cyclic;
  86. };
  87. struct k3_dma_phy {
  88. u32 idx;
  89. void __iomem *base;
  90. struct k3_dma_chan *vchan;
  91. struct k3_dma_desc_sw *ds_run;
  92. struct k3_dma_desc_sw *ds_done;
  93. };
  94. struct k3_dma_dev {
  95. struct dma_device slave;
  96. void __iomem *base;
  97. struct tasklet_struct task;
  98. spinlock_t lock;
  99. struct list_head chan_pending;
  100. struct k3_dma_phy *phy;
  101. struct k3_dma_chan *chans;
  102. struct clk *clk;
  103. struct dma_pool *pool;
  104. u32 dma_channels;
  105. u32 dma_requests;
  106. unsigned int irq;
  107. };
  108. #define to_k3_dma(dmadev) container_of(dmadev, struct k3_dma_dev, slave)
  109. static struct k3_dma_chan *to_k3_chan(struct dma_chan *chan)
  110. {
  111. return container_of(chan, struct k3_dma_chan, vc.chan);
  112. }
  113. static void k3_dma_pause_dma(struct k3_dma_phy *phy, bool on)
  114. {
  115. u32 val = 0;
  116. if (on) {
  117. val = readl_relaxed(phy->base + CX_CFG);
  118. val |= CX_CFG_EN;
  119. writel_relaxed(val, phy->base + CX_CFG);
  120. } else {
  121. val = readl_relaxed(phy->base + CX_CFG);
  122. val &= ~CX_CFG_EN;
  123. writel_relaxed(val, phy->base + CX_CFG);
  124. }
  125. }
  126. static void k3_dma_terminate_chan(struct k3_dma_phy *phy, struct k3_dma_dev *d)
  127. {
  128. u32 val = 0;
  129. k3_dma_pause_dma(phy, false);
  130. val = 0x1 << phy->idx;
  131. writel_relaxed(val, d->base + INT_TC1_RAW);
  132. writel_relaxed(val, d->base + INT_TC2_RAW);
  133. writel_relaxed(val, d->base + INT_ERR1_RAW);
  134. writel_relaxed(val, d->base + INT_ERR2_RAW);
  135. }
  136. static void k3_dma_set_desc(struct k3_dma_phy *phy, struct k3_desc_hw *hw)
  137. {
  138. writel_relaxed(hw->lli, phy->base + CX_LLI);
  139. writel_relaxed(hw->count, phy->base + CX_CNT0);
  140. writel_relaxed(hw->saddr, phy->base + CX_SRC);
  141. writel_relaxed(hw->daddr, phy->base + CX_DST);
  142. writel_relaxed(AXI_CFG_DEFAULT, phy->base + AXI_CFG);
  143. writel_relaxed(hw->config, phy->base + CX_CFG);
  144. }
  145. static u32 k3_dma_get_curr_cnt(struct k3_dma_dev *d, struct k3_dma_phy *phy)
  146. {
  147. u32 cnt = 0;
  148. cnt = readl_relaxed(d->base + CX_CUR_CNT + phy->idx * 0x10);
  149. cnt &= 0xffff;
  150. return cnt;
  151. }
  152. static u32 k3_dma_get_curr_lli(struct k3_dma_phy *phy)
  153. {
  154. return readl_relaxed(phy->base + CX_LLI);
  155. }
  156. static u32 k3_dma_get_chan_stat(struct k3_dma_dev *d)
  157. {
  158. return readl_relaxed(d->base + CH_STAT);
  159. }
  160. static void k3_dma_enable_dma(struct k3_dma_dev *d, bool on)
  161. {
  162. if (on) {
  163. /* set same priority */
  164. writel_relaxed(0x0, d->base + CH_PRI);
  165. /* unmask irq */
  166. writel_relaxed(0xffff, d->base + INT_TC1_MASK);
  167. writel_relaxed(0xffff, d->base + INT_TC2_MASK);
  168. writel_relaxed(0xffff, d->base + INT_ERR1_MASK);
  169. writel_relaxed(0xffff, d->base + INT_ERR2_MASK);
  170. } else {
  171. /* mask irq */
  172. writel_relaxed(0x0, d->base + INT_TC1_MASK);
  173. writel_relaxed(0x0, d->base + INT_TC2_MASK);
  174. writel_relaxed(0x0, d->base + INT_ERR1_MASK);
  175. writel_relaxed(0x0, d->base + INT_ERR2_MASK);
  176. }
  177. }
  178. static irqreturn_t k3_dma_int_handler(int irq, void *dev_id)
  179. {
  180. struct k3_dma_dev *d = (struct k3_dma_dev *)dev_id;
  181. struct k3_dma_phy *p;
  182. struct k3_dma_chan *c;
  183. u32 stat = readl_relaxed(d->base + INT_STAT);
  184. u32 tc1 = readl_relaxed(d->base + INT_TC1);
  185. u32 tc2 = readl_relaxed(d->base + INT_TC2);
  186. u32 err1 = readl_relaxed(d->base + INT_ERR1);
  187. u32 err2 = readl_relaxed(d->base + INT_ERR2);
  188. u32 i, irq_chan = 0;
  189. while (stat) {
  190. i = __ffs(stat);
  191. stat &= ~BIT(i);
  192. if (likely(tc1 & BIT(i)) || (tc2 & BIT(i))) {
  193. unsigned long flags;
  194. p = &d->phy[i];
  195. c = p->vchan;
  196. if (c && (tc1 & BIT(i))) {
  197. spin_lock_irqsave(&c->vc.lock, flags);
  198. if (p->ds_run != NULL) {
  199. vchan_cookie_complete(&p->ds_run->vd);
  200. p->ds_done = p->ds_run;
  201. p->ds_run = NULL;
  202. }
  203. spin_unlock_irqrestore(&c->vc.lock, flags);
  204. }
  205. if (c && (tc2 & BIT(i))) {
  206. spin_lock_irqsave(&c->vc.lock, flags);
  207. if (p->ds_run != NULL)
  208. vchan_cyclic_callback(&p->ds_run->vd);
  209. spin_unlock_irqrestore(&c->vc.lock, flags);
  210. }
  211. irq_chan |= BIT(i);
  212. }
  213. if (unlikely((err1 & BIT(i)) || (err2 & BIT(i))))
  214. dev_warn(d->slave.dev, "DMA ERR\n");
  215. }
  216. writel_relaxed(irq_chan, d->base + INT_TC1_RAW);
  217. writel_relaxed(irq_chan, d->base + INT_TC2_RAW);
  218. writel_relaxed(err1, d->base + INT_ERR1_RAW);
  219. writel_relaxed(err2, d->base + INT_ERR2_RAW);
  220. if (irq_chan)
  221. tasklet_schedule(&d->task);
  222. if (irq_chan || err1 || err2)
  223. return IRQ_HANDLED;
  224. return IRQ_NONE;
  225. }
  226. static int k3_dma_start_txd(struct k3_dma_chan *c)
  227. {
  228. struct k3_dma_dev *d = to_k3_dma(c->vc.chan.device);
  229. struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  230. if (!c->phy)
  231. return -EAGAIN;
  232. if (BIT(c->phy->idx) & k3_dma_get_chan_stat(d))
  233. return -EAGAIN;
  234. /* Avoid losing track of ds_run if a transaction is in flight */
  235. if (c->phy->ds_run)
  236. return -EAGAIN;
  237. if (vd) {
  238. struct k3_dma_desc_sw *ds =
  239. container_of(vd, struct k3_dma_desc_sw, vd);
  240. /*
  241. * fetch and remove request from vc->desc_issued
  242. * so vc->desc_issued only contains desc pending
  243. */
  244. list_del(&ds->vd.node);
  245. c->phy->ds_run = ds;
  246. c->phy->ds_done = NULL;
  247. /* start dma */
  248. k3_dma_set_desc(c->phy, &ds->desc_hw[0]);
  249. return 0;
  250. }
  251. c->phy->ds_run = NULL;
  252. c->phy->ds_done = NULL;
  253. return -EAGAIN;
  254. }
  255. static void k3_dma_tasklet(unsigned long arg)
  256. {
  257. struct k3_dma_dev *d = (struct k3_dma_dev *)arg;
  258. struct k3_dma_phy *p;
  259. struct k3_dma_chan *c, *cn;
  260. unsigned pch, pch_alloc = 0;
  261. /* check new dma request of running channel in vc->desc_issued */
  262. list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
  263. spin_lock_irq(&c->vc.lock);
  264. p = c->phy;
  265. if (p && p->ds_done) {
  266. if (k3_dma_start_txd(c)) {
  267. /* No current txd associated with this channel */
  268. dev_dbg(d->slave.dev, "pchan %u: free\n", p->idx);
  269. /* Mark this channel free */
  270. c->phy = NULL;
  271. p->vchan = NULL;
  272. }
  273. }
  274. spin_unlock_irq(&c->vc.lock);
  275. }
  276. /* check new channel request in d->chan_pending */
  277. spin_lock_irq(&d->lock);
  278. for (pch = 0; pch < d->dma_channels; pch++) {
  279. p = &d->phy[pch];
  280. if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
  281. c = list_first_entry(&d->chan_pending,
  282. struct k3_dma_chan, node);
  283. /* remove from d->chan_pending */
  284. list_del_init(&c->node);
  285. pch_alloc |= 1 << pch;
  286. /* Mark this channel allocated */
  287. p->vchan = c;
  288. c->phy = p;
  289. dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, &c->vc);
  290. }
  291. }
  292. spin_unlock_irq(&d->lock);
  293. for (pch = 0; pch < d->dma_channels; pch++) {
  294. if (pch_alloc & (1 << pch)) {
  295. p = &d->phy[pch];
  296. c = p->vchan;
  297. if (c) {
  298. spin_lock_irq(&c->vc.lock);
  299. k3_dma_start_txd(c);
  300. spin_unlock_irq(&c->vc.lock);
  301. }
  302. }
  303. }
  304. }
  305. static void k3_dma_free_chan_resources(struct dma_chan *chan)
  306. {
  307. struct k3_dma_chan *c = to_k3_chan(chan);
  308. struct k3_dma_dev *d = to_k3_dma(chan->device);
  309. unsigned long flags;
  310. spin_lock_irqsave(&d->lock, flags);
  311. list_del_init(&c->node);
  312. spin_unlock_irqrestore(&d->lock, flags);
  313. vchan_free_chan_resources(&c->vc);
  314. c->ccfg = 0;
  315. }
  316. static enum dma_status k3_dma_tx_status(struct dma_chan *chan,
  317. dma_cookie_t cookie, struct dma_tx_state *state)
  318. {
  319. struct k3_dma_chan *c = to_k3_chan(chan);
  320. struct k3_dma_dev *d = to_k3_dma(chan->device);
  321. struct k3_dma_phy *p;
  322. struct virt_dma_desc *vd;
  323. unsigned long flags;
  324. enum dma_status ret;
  325. size_t bytes = 0;
  326. ret = dma_cookie_status(&c->vc.chan, cookie, state);
  327. if (ret == DMA_COMPLETE)
  328. return ret;
  329. spin_lock_irqsave(&c->vc.lock, flags);
  330. p = c->phy;
  331. ret = c->status;
  332. /*
  333. * If the cookie is on our issue queue, then the residue is
  334. * its total size.
  335. */
  336. vd = vchan_find_desc(&c->vc, cookie);
  337. if (vd && !c->cyclic) {
  338. bytes = container_of(vd, struct k3_dma_desc_sw, vd)->size;
  339. } else if ((!p) || (!p->ds_run)) {
  340. bytes = 0;
  341. } else {
  342. struct k3_dma_desc_sw *ds = p->ds_run;
  343. u32 clli = 0, index = 0;
  344. bytes = k3_dma_get_curr_cnt(d, p);
  345. clli = k3_dma_get_curr_lli(p);
  346. index = ((clli - ds->desc_hw_lli) /
  347. sizeof(struct k3_desc_hw)) + 1;
  348. for (; index < ds->desc_num; index++) {
  349. bytes += ds->desc_hw[index].count;
  350. /* end of lli */
  351. if (!ds->desc_hw[index].lli)
  352. break;
  353. }
  354. }
  355. spin_unlock_irqrestore(&c->vc.lock, flags);
  356. dma_set_residue(state, bytes);
  357. return ret;
  358. }
  359. static void k3_dma_issue_pending(struct dma_chan *chan)
  360. {
  361. struct k3_dma_chan *c = to_k3_chan(chan);
  362. struct k3_dma_dev *d = to_k3_dma(chan->device);
  363. unsigned long flags;
  364. spin_lock_irqsave(&c->vc.lock, flags);
  365. /* add request to vc->desc_issued */
  366. if (vchan_issue_pending(&c->vc)) {
  367. spin_lock(&d->lock);
  368. if (!c->phy) {
  369. if (list_empty(&c->node)) {
  370. /* if new channel, add chan_pending */
  371. list_add_tail(&c->node, &d->chan_pending);
  372. /* check in tasklet */
  373. tasklet_schedule(&d->task);
  374. dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
  375. }
  376. }
  377. spin_unlock(&d->lock);
  378. } else
  379. dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
  380. spin_unlock_irqrestore(&c->vc.lock, flags);
  381. }
  382. static void k3_dma_fill_desc(struct k3_dma_desc_sw *ds, dma_addr_t dst,
  383. dma_addr_t src, size_t len, u32 num, u32 ccfg)
  384. {
  385. if (num != ds->desc_num - 1)
  386. ds->desc_hw[num].lli = ds->desc_hw_lli + (num + 1) *
  387. sizeof(struct k3_desc_hw);
  388. ds->desc_hw[num].lli |= CX_LLI_CHAIN_EN;
  389. ds->desc_hw[num].count = len;
  390. ds->desc_hw[num].saddr = src;
  391. ds->desc_hw[num].daddr = dst;
  392. ds->desc_hw[num].config = ccfg;
  393. }
  394. static struct k3_dma_desc_sw *k3_dma_alloc_desc_resource(int num,
  395. struct dma_chan *chan)
  396. {
  397. struct k3_dma_chan *c = to_k3_chan(chan);
  398. struct k3_dma_desc_sw *ds;
  399. struct k3_dma_dev *d = to_k3_dma(chan->device);
  400. int lli_limit = LLI_BLOCK_SIZE / sizeof(struct k3_desc_hw);
  401. if (num > lli_limit) {
  402. dev_dbg(chan->device->dev, "vch %p: sg num %d exceed max %d\n",
  403. &c->vc, num, lli_limit);
  404. return NULL;
  405. }
  406. ds = kzalloc(sizeof(*ds), GFP_NOWAIT);
  407. if (!ds)
  408. return NULL;
  409. ds->desc_hw = dma_pool_zalloc(d->pool, GFP_NOWAIT, &ds->desc_hw_lli);
  410. if (!ds->desc_hw) {
  411. dev_dbg(chan->device->dev, "vch %p: dma alloc fail\n", &c->vc);
  412. kfree(ds);
  413. return NULL;
  414. }
  415. ds->desc_num = num;
  416. return ds;
  417. }
  418. static struct dma_async_tx_descriptor *k3_dma_prep_memcpy(
  419. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  420. size_t len, unsigned long flags)
  421. {
  422. struct k3_dma_chan *c = to_k3_chan(chan);
  423. struct k3_dma_desc_sw *ds;
  424. size_t copy = 0;
  425. int num = 0;
  426. if (!len)
  427. return NULL;
  428. num = DIV_ROUND_UP(len, DMA_MAX_SIZE);
  429. ds = k3_dma_alloc_desc_resource(num, chan);
  430. if (!ds)
  431. return NULL;
  432. c->cyclic = 0;
  433. ds->size = len;
  434. num = 0;
  435. if (!c->ccfg) {
  436. /* default is memtomem, without calling device_config */
  437. c->ccfg = CX_CFG_SRCINCR | CX_CFG_DSTINCR | CX_CFG_EN;
  438. c->ccfg |= (0xf << 20) | (0xf << 24); /* burst = 16 */
  439. c->ccfg |= (0x3 << 12) | (0x3 << 16); /* width = 64 bit */
  440. }
  441. do {
  442. copy = min_t(size_t, len, DMA_MAX_SIZE);
  443. k3_dma_fill_desc(ds, dst, src, copy, num++, c->ccfg);
  444. if (c->dir == DMA_MEM_TO_DEV) {
  445. src += copy;
  446. } else if (c->dir == DMA_DEV_TO_MEM) {
  447. dst += copy;
  448. } else {
  449. src += copy;
  450. dst += copy;
  451. }
  452. len -= copy;
  453. } while (len);
  454. ds->desc_hw[num-1].lli = 0; /* end of link */
  455. return vchan_tx_prep(&c->vc, &ds->vd, flags);
  456. }
  457. static struct dma_async_tx_descriptor *k3_dma_prep_slave_sg(
  458. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
  459. enum dma_transfer_direction dir, unsigned long flags, void *context)
  460. {
  461. struct k3_dma_chan *c = to_k3_chan(chan);
  462. struct k3_dma_desc_sw *ds;
  463. size_t len, avail, total = 0;
  464. struct scatterlist *sg;
  465. dma_addr_t addr, src = 0, dst = 0;
  466. int num = sglen, i;
  467. if (sgl == NULL)
  468. return NULL;
  469. c->cyclic = 0;
  470. for_each_sg(sgl, sg, sglen, i) {
  471. avail = sg_dma_len(sg);
  472. if (avail > DMA_MAX_SIZE)
  473. num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
  474. }
  475. ds = k3_dma_alloc_desc_resource(num, chan);
  476. if (!ds)
  477. return NULL;
  478. num = 0;
  479. for_each_sg(sgl, sg, sglen, i) {
  480. addr = sg_dma_address(sg);
  481. avail = sg_dma_len(sg);
  482. total += avail;
  483. do {
  484. len = min_t(size_t, avail, DMA_MAX_SIZE);
  485. if (dir == DMA_MEM_TO_DEV) {
  486. src = addr;
  487. dst = c->dev_addr;
  488. } else if (dir == DMA_DEV_TO_MEM) {
  489. src = c->dev_addr;
  490. dst = addr;
  491. }
  492. k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg);
  493. addr += len;
  494. avail -= len;
  495. } while (avail);
  496. }
  497. ds->desc_hw[num-1].lli = 0; /* end of link */
  498. ds->size = total;
  499. return vchan_tx_prep(&c->vc, &ds->vd, flags);
  500. }
  501. static struct dma_async_tx_descriptor *
  502. k3_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
  503. size_t buf_len, size_t period_len,
  504. enum dma_transfer_direction dir,
  505. unsigned long flags)
  506. {
  507. struct k3_dma_chan *c = to_k3_chan(chan);
  508. struct k3_dma_desc_sw *ds;
  509. size_t len, avail, total = 0;
  510. dma_addr_t addr, src = 0, dst = 0;
  511. int num = 1, since = 0;
  512. size_t modulo = DMA_CYCLIC_MAX_PERIOD;
  513. u32 en_tc2 = 0;
  514. dev_dbg(chan->device->dev, "%s: buf %pad, dst %pad, buf len %zu, period_len = %zu, dir %d\n",
  515. __func__, &buf_addr, &to_k3_chan(chan)->dev_addr,
  516. buf_len, period_len, (int)dir);
  517. avail = buf_len;
  518. if (avail > modulo)
  519. num += DIV_ROUND_UP(avail, modulo) - 1;
  520. ds = k3_dma_alloc_desc_resource(num, chan);
  521. if (!ds)
  522. return NULL;
  523. c->cyclic = 1;
  524. addr = buf_addr;
  525. avail = buf_len;
  526. total = avail;
  527. num = 0;
  528. if (period_len < modulo)
  529. modulo = period_len;
  530. do {
  531. len = min_t(size_t, avail, modulo);
  532. if (dir == DMA_MEM_TO_DEV) {
  533. src = addr;
  534. dst = c->dev_addr;
  535. } else if (dir == DMA_DEV_TO_MEM) {
  536. src = c->dev_addr;
  537. dst = addr;
  538. }
  539. since += len;
  540. if (since >= period_len) {
  541. /* descriptor asks for TC2 interrupt on completion */
  542. en_tc2 = CX_CFG_NODEIRQ;
  543. since -= period_len;
  544. } else
  545. en_tc2 = 0;
  546. k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg | en_tc2);
  547. addr += len;
  548. avail -= len;
  549. } while (avail);
  550. /* "Cyclic" == end of link points back to start of link */
  551. ds->desc_hw[num - 1].lli |= ds->desc_hw_lli;
  552. ds->size = total;
  553. return vchan_tx_prep(&c->vc, &ds->vd, flags);
  554. }
  555. static int k3_dma_config(struct dma_chan *chan,
  556. struct dma_slave_config *cfg)
  557. {
  558. struct k3_dma_chan *c = to_k3_chan(chan);
  559. u32 maxburst = 0, val = 0;
  560. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  561. if (cfg == NULL)
  562. return -EINVAL;
  563. c->dir = cfg->direction;
  564. if (c->dir == DMA_DEV_TO_MEM) {
  565. c->ccfg = CX_CFG_DSTINCR;
  566. c->dev_addr = cfg->src_addr;
  567. maxburst = cfg->src_maxburst;
  568. width = cfg->src_addr_width;
  569. } else if (c->dir == DMA_MEM_TO_DEV) {
  570. c->ccfg = CX_CFG_SRCINCR;
  571. c->dev_addr = cfg->dst_addr;
  572. maxburst = cfg->dst_maxburst;
  573. width = cfg->dst_addr_width;
  574. }
  575. switch (width) {
  576. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  577. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  578. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  579. case DMA_SLAVE_BUSWIDTH_8_BYTES:
  580. val = __ffs(width);
  581. break;
  582. default:
  583. val = 3;
  584. break;
  585. }
  586. c->ccfg |= (val << 12) | (val << 16);
  587. if ((maxburst == 0) || (maxburst > 16))
  588. val = 15;
  589. else
  590. val = maxburst - 1;
  591. c->ccfg |= (val << 20) | (val << 24);
  592. c->ccfg |= CX_CFG_MEM2PER | CX_CFG_EN;
  593. /* specific request line */
  594. c->ccfg |= c->vc.chan.chan_id << 4;
  595. return 0;
  596. }
  597. static void k3_dma_free_desc(struct virt_dma_desc *vd)
  598. {
  599. struct k3_dma_desc_sw *ds =
  600. container_of(vd, struct k3_dma_desc_sw, vd);
  601. struct k3_dma_dev *d = to_k3_dma(vd->tx.chan->device);
  602. dma_pool_free(d->pool, ds->desc_hw, ds->desc_hw_lli);
  603. kfree(ds);
  604. }
  605. static int k3_dma_terminate_all(struct dma_chan *chan)
  606. {
  607. struct k3_dma_chan *c = to_k3_chan(chan);
  608. struct k3_dma_dev *d = to_k3_dma(chan->device);
  609. struct k3_dma_phy *p = c->phy;
  610. unsigned long flags;
  611. LIST_HEAD(head);
  612. dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
  613. /* Prevent this channel being scheduled */
  614. spin_lock(&d->lock);
  615. list_del_init(&c->node);
  616. spin_unlock(&d->lock);
  617. /* Clear the tx descriptor lists */
  618. spin_lock_irqsave(&c->vc.lock, flags);
  619. vchan_get_all_descriptors(&c->vc, &head);
  620. if (p) {
  621. /* vchan is assigned to a pchan - stop the channel */
  622. k3_dma_terminate_chan(p, d);
  623. c->phy = NULL;
  624. p->vchan = NULL;
  625. if (p->ds_run) {
  626. vchan_terminate_vdesc(&p->ds_run->vd);
  627. p->ds_run = NULL;
  628. }
  629. p->ds_done = NULL;
  630. }
  631. spin_unlock_irqrestore(&c->vc.lock, flags);
  632. vchan_dma_desc_free_list(&c->vc, &head);
  633. return 0;
  634. }
  635. static void k3_dma_synchronize(struct dma_chan *chan)
  636. {
  637. struct k3_dma_chan *c = to_k3_chan(chan);
  638. vchan_synchronize(&c->vc);
  639. }
  640. static int k3_dma_transfer_pause(struct dma_chan *chan)
  641. {
  642. struct k3_dma_chan *c = to_k3_chan(chan);
  643. struct k3_dma_dev *d = to_k3_dma(chan->device);
  644. struct k3_dma_phy *p = c->phy;
  645. dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
  646. if (c->status == DMA_IN_PROGRESS) {
  647. c->status = DMA_PAUSED;
  648. if (p) {
  649. k3_dma_pause_dma(p, false);
  650. } else {
  651. spin_lock(&d->lock);
  652. list_del_init(&c->node);
  653. spin_unlock(&d->lock);
  654. }
  655. }
  656. return 0;
  657. }
  658. static int k3_dma_transfer_resume(struct dma_chan *chan)
  659. {
  660. struct k3_dma_chan *c = to_k3_chan(chan);
  661. struct k3_dma_dev *d = to_k3_dma(chan->device);
  662. struct k3_dma_phy *p = c->phy;
  663. unsigned long flags;
  664. dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
  665. spin_lock_irqsave(&c->vc.lock, flags);
  666. if (c->status == DMA_PAUSED) {
  667. c->status = DMA_IN_PROGRESS;
  668. if (p) {
  669. k3_dma_pause_dma(p, true);
  670. } else if (!list_empty(&c->vc.desc_issued)) {
  671. spin_lock(&d->lock);
  672. list_add_tail(&c->node, &d->chan_pending);
  673. spin_unlock(&d->lock);
  674. }
  675. }
  676. spin_unlock_irqrestore(&c->vc.lock, flags);
  677. return 0;
  678. }
  679. static const struct of_device_id k3_pdma_dt_ids[] = {
  680. { .compatible = "hisilicon,k3-dma-1.0", },
  681. {}
  682. };
  683. MODULE_DEVICE_TABLE(of, k3_pdma_dt_ids);
  684. static struct dma_chan *k3_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  685. struct of_dma *ofdma)
  686. {
  687. struct k3_dma_dev *d = ofdma->of_dma_data;
  688. unsigned int request = dma_spec->args[0];
  689. if (request >= d->dma_requests)
  690. return NULL;
  691. return dma_get_slave_channel(&(d->chans[request].vc.chan));
  692. }
  693. static int k3_dma_probe(struct platform_device *op)
  694. {
  695. struct k3_dma_dev *d;
  696. const struct of_device_id *of_id;
  697. struct resource *iores;
  698. int i, ret, irq = 0;
  699. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  700. if (!iores)
  701. return -EINVAL;
  702. d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
  703. if (!d)
  704. return -ENOMEM;
  705. d->base = devm_ioremap_resource(&op->dev, iores);
  706. if (IS_ERR(d->base))
  707. return PTR_ERR(d->base);
  708. of_id = of_match_device(k3_pdma_dt_ids, &op->dev);
  709. if (of_id) {
  710. of_property_read_u32((&op->dev)->of_node,
  711. "dma-channels", &d->dma_channels);
  712. of_property_read_u32((&op->dev)->of_node,
  713. "dma-requests", &d->dma_requests);
  714. }
  715. d->clk = devm_clk_get(&op->dev, NULL);
  716. if (IS_ERR(d->clk)) {
  717. dev_err(&op->dev, "no dma clk\n");
  718. return PTR_ERR(d->clk);
  719. }
  720. irq = platform_get_irq(op, 0);
  721. ret = devm_request_irq(&op->dev, irq,
  722. k3_dma_int_handler, 0, DRIVER_NAME, d);
  723. if (ret)
  724. return ret;
  725. d->irq = irq;
  726. /* A DMA memory pool for LLIs, align on 32-byte boundary */
  727. d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
  728. LLI_BLOCK_SIZE, 32, 0);
  729. if (!d->pool)
  730. return -ENOMEM;
  731. /* init phy channel */
  732. d->phy = devm_kcalloc(&op->dev,
  733. d->dma_channels, sizeof(struct k3_dma_phy), GFP_KERNEL);
  734. if (d->phy == NULL)
  735. return -ENOMEM;
  736. for (i = 0; i < d->dma_channels; i++) {
  737. struct k3_dma_phy *p = &d->phy[i];
  738. p->idx = i;
  739. p->base = d->base + i * 0x40;
  740. }
  741. INIT_LIST_HEAD(&d->slave.channels);
  742. dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
  743. dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
  744. dma_cap_set(DMA_CYCLIC, d->slave.cap_mask);
  745. d->slave.dev = &op->dev;
  746. d->slave.device_free_chan_resources = k3_dma_free_chan_resources;
  747. d->slave.device_tx_status = k3_dma_tx_status;
  748. d->slave.device_prep_dma_memcpy = k3_dma_prep_memcpy;
  749. d->slave.device_prep_slave_sg = k3_dma_prep_slave_sg;
  750. d->slave.device_prep_dma_cyclic = k3_dma_prep_dma_cyclic;
  751. d->slave.device_issue_pending = k3_dma_issue_pending;
  752. d->slave.device_config = k3_dma_config;
  753. d->slave.device_pause = k3_dma_transfer_pause;
  754. d->slave.device_resume = k3_dma_transfer_resume;
  755. d->slave.device_terminate_all = k3_dma_terminate_all;
  756. d->slave.device_synchronize = k3_dma_synchronize;
  757. d->slave.copy_align = DMAENGINE_ALIGN_8_BYTES;
  758. /* init virtual channel */
  759. d->chans = devm_kcalloc(&op->dev,
  760. d->dma_requests, sizeof(struct k3_dma_chan), GFP_KERNEL);
  761. if (d->chans == NULL)
  762. return -ENOMEM;
  763. for (i = 0; i < d->dma_requests; i++) {
  764. struct k3_dma_chan *c = &d->chans[i];
  765. c->status = DMA_IN_PROGRESS;
  766. INIT_LIST_HEAD(&c->node);
  767. c->vc.desc_free = k3_dma_free_desc;
  768. vchan_init(&c->vc, &d->slave);
  769. }
  770. /* Enable clock before accessing registers */
  771. ret = clk_prepare_enable(d->clk);
  772. if (ret < 0) {
  773. dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
  774. return ret;
  775. }
  776. k3_dma_enable_dma(d, true);
  777. ret = dma_async_device_register(&d->slave);
  778. if (ret)
  779. goto dma_async_register_fail;
  780. ret = of_dma_controller_register((&op->dev)->of_node,
  781. k3_of_dma_simple_xlate, d);
  782. if (ret)
  783. goto of_dma_register_fail;
  784. spin_lock_init(&d->lock);
  785. INIT_LIST_HEAD(&d->chan_pending);
  786. tasklet_init(&d->task, k3_dma_tasklet, (unsigned long)d);
  787. platform_set_drvdata(op, d);
  788. dev_info(&op->dev, "initialized\n");
  789. return 0;
  790. of_dma_register_fail:
  791. dma_async_device_unregister(&d->slave);
  792. dma_async_register_fail:
  793. clk_disable_unprepare(d->clk);
  794. return ret;
  795. }
  796. static int k3_dma_remove(struct platform_device *op)
  797. {
  798. struct k3_dma_chan *c, *cn;
  799. struct k3_dma_dev *d = platform_get_drvdata(op);
  800. dma_async_device_unregister(&d->slave);
  801. of_dma_controller_free((&op->dev)->of_node);
  802. devm_free_irq(&op->dev, d->irq, d);
  803. list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
  804. list_del(&c->vc.chan.device_node);
  805. tasklet_kill(&c->vc.task);
  806. }
  807. tasklet_kill(&d->task);
  808. clk_disable_unprepare(d->clk);
  809. return 0;
  810. }
  811. #ifdef CONFIG_PM_SLEEP
  812. static int k3_dma_suspend_dev(struct device *dev)
  813. {
  814. struct k3_dma_dev *d = dev_get_drvdata(dev);
  815. u32 stat = 0;
  816. stat = k3_dma_get_chan_stat(d);
  817. if (stat) {
  818. dev_warn(d->slave.dev,
  819. "chan %d is running fail to suspend\n", stat);
  820. return -1;
  821. }
  822. k3_dma_enable_dma(d, false);
  823. clk_disable_unprepare(d->clk);
  824. return 0;
  825. }
  826. static int k3_dma_resume_dev(struct device *dev)
  827. {
  828. struct k3_dma_dev *d = dev_get_drvdata(dev);
  829. int ret = 0;
  830. ret = clk_prepare_enable(d->clk);
  831. if (ret < 0) {
  832. dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
  833. return ret;
  834. }
  835. k3_dma_enable_dma(d, true);
  836. return 0;
  837. }
  838. #endif
  839. static SIMPLE_DEV_PM_OPS(k3_dma_pmops, k3_dma_suspend_dev, k3_dma_resume_dev);
  840. static struct platform_driver k3_pdma_driver = {
  841. .driver = {
  842. .name = DRIVER_NAME,
  843. .pm = &k3_dma_pmops,
  844. .of_match_table = k3_pdma_dt_ids,
  845. },
  846. .probe = k3_dma_probe,
  847. .remove = k3_dma_remove,
  848. };
  849. module_platform_driver(k3_pdma_driver);
  850. MODULE_DESCRIPTION("Hisilicon k3 DMA Driver");
  851. MODULE_ALIAS("platform:k3dma");
  852. MODULE_LICENSE("GPL v2");