k3dma.c 20 KB

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