k3dma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. vchan_cookie_complete(&p->ds_run->vd);
  199. WARN_ON_ONCE(p->ds_done);
  200. p->ds_done = p->ds_run;
  201. p->ds_run = NULL;
  202. spin_unlock_irqrestore(&c->vc.lock, flags);
  203. }
  204. if (c && (tc2 & BIT(i))) {
  205. spin_lock_irqsave(&c->vc.lock, flags);
  206. if (p->ds_run != NULL)
  207. vchan_cyclic_callback(&p->ds_run->vd);
  208. spin_unlock_irqrestore(&c->vc.lock, flags);
  209. }
  210. irq_chan |= BIT(i);
  211. }
  212. if (unlikely((err1 & BIT(i)) || (err2 & BIT(i))))
  213. dev_warn(d->slave.dev, "DMA ERR\n");
  214. }
  215. writel_relaxed(irq_chan, d->base + INT_TC1_RAW);
  216. writel_relaxed(irq_chan, d->base + INT_TC2_RAW);
  217. writel_relaxed(err1, d->base + INT_ERR1_RAW);
  218. writel_relaxed(err2, d->base + INT_ERR2_RAW);
  219. if (irq_chan)
  220. tasklet_schedule(&d->task);
  221. if (irq_chan || err1 || err2)
  222. return IRQ_HANDLED;
  223. return IRQ_NONE;
  224. }
  225. static int k3_dma_start_txd(struct k3_dma_chan *c)
  226. {
  227. struct k3_dma_dev *d = to_k3_dma(c->vc.chan.device);
  228. struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  229. if (!c->phy)
  230. return -EAGAIN;
  231. if (BIT(c->phy->idx) & k3_dma_get_chan_stat(d))
  232. return -EAGAIN;
  233. if (vd) {
  234. struct k3_dma_desc_sw *ds =
  235. container_of(vd, struct k3_dma_desc_sw, vd);
  236. /*
  237. * fetch and remove request from vc->desc_issued
  238. * so vc->desc_issued only contains desc pending
  239. */
  240. list_del(&ds->vd.node);
  241. WARN_ON_ONCE(c->phy->ds_run);
  242. WARN_ON_ONCE(c->phy->ds_done);
  243. c->phy->ds_run = ds;
  244. /* start dma */
  245. k3_dma_set_desc(c->phy, &ds->desc_hw[0]);
  246. return 0;
  247. }
  248. return -EAGAIN;
  249. }
  250. static void k3_dma_tasklet(unsigned long arg)
  251. {
  252. struct k3_dma_dev *d = (struct k3_dma_dev *)arg;
  253. struct k3_dma_phy *p;
  254. struct k3_dma_chan *c, *cn;
  255. unsigned pch, pch_alloc = 0;
  256. /* check new dma request of running channel in vc->desc_issued */
  257. list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
  258. spin_lock_irq(&c->vc.lock);
  259. p = c->phy;
  260. if (p && p->ds_done) {
  261. if (k3_dma_start_txd(c)) {
  262. /* No current txd associated with this channel */
  263. dev_dbg(d->slave.dev, "pchan %u: free\n", p->idx);
  264. /* Mark this channel free */
  265. c->phy = NULL;
  266. p->vchan = NULL;
  267. }
  268. }
  269. spin_unlock_irq(&c->vc.lock);
  270. }
  271. /* check new channel request in d->chan_pending */
  272. spin_lock_irq(&d->lock);
  273. for (pch = 0; pch < d->dma_channels; pch++) {
  274. p = &d->phy[pch];
  275. if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
  276. c = list_first_entry(&d->chan_pending,
  277. struct k3_dma_chan, node);
  278. /* remove from d->chan_pending */
  279. list_del_init(&c->node);
  280. pch_alloc |= 1 << pch;
  281. /* Mark this channel allocated */
  282. p->vchan = c;
  283. c->phy = p;
  284. dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, &c->vc);
  285. }
  286. }
  287. spin_unlock_irq(&d->lock);
  288. for (pch = 0; pch < d->dma_channels; pch++) {
  289. if (pch_alloc & (1 << pch)) {
  290. p = &d->phy[pch];
  291. c = p->vchan;
  292. if (c) {
  293. spin_lock_irq(&c->vc.lock);
  294. k3_dma_start_txd(c);
  295. spin_unlock_irq(&c->vc.lock);
  296. }
  297. }
  298. }
  299. }
  300. static void k3_dma_free_chan_resources(struct dma_chan *chan)
  301. {
  302. struct k3_dma_chan *c = to_k3_chan(chan);
  303. struct k3_dma_dev *d = to_k3_dma(chan->device);
  304. unsigned long flags;
  305. spin_lock_irqsave(&d->lock, flags);
  306. list_del_init(&c->node);
  307. spin_unlock_irqrestore(&d->lock, flags);
  308. vchan_free_chan_resources(&c->vc);
  309. c->ccfg = 0;
  310. }
  311. static enum dma_status k3_dma_tx_status(struct dma_chan *chan,
  312. dma_cookie_t cookie, struct dma_tx_state *state)
  313. {
  314. struct k3_dma_chan *c = to_k3_chan(chan);
  315. struct k3_dma_dev *d = to_k3_dma(chan->device);
  316. struct k3_dma_phy *p;
  317. struct virt_dma_desc *vd;
  318. unsigned long flags;
  319. enum dma_status ret;
  320. size_t bytes = 0;
  321. ret = dma_cookie_status(&c->vc.chan, cookie, state);
  322. if (ret == DMA_COMPLETE)
  323. return ret;
  324. spin_lock_irqsave(&c->vc.lock, flags);
  325. p = c->phy;
  326. ret = c->status;
  327. /*
  328. * If the cookie is on our issue queue, then the residue is
  329. * its total size.
  330. */
  331. vd = vchan_find_desc(&c->vc, cookie);
  332. if (vd && !c->cyclic) {
  333. bytes = container_of(vd, struct k3_dma_desc_sw, vd)->size;
  334. } else if ((!p) || (!p->ds_run)) {
  335. bytes = 0;
  336. } else {
  337. struct k3_dma_desc_sw *ds = p->ds_run;
  338. u32 clli = 0, index = 0;
  339. bytes = k3_dma_get_curr_cnt(d, p);
  340. clli = k3_dma_get_curr_lli(p);
  341. index = ((clli - ds->desc_hw_lli) /
  342. sizeof(struct k3_desc_hw)) + 1;
  343. for (; index < ds->desc_num; index++) {
  344. bytes += ds->desc_hw[index].count;
  345. /* end of lli */
  346. if (!ds->desc_hw[index].lli)
  347. break;
  348. }
  349. }
  350. spin_unlock_irqrestore(&c->vc.lock, flags);
  351. dma_set_residue(state, bytes);
  352. return ret;
  353. }
  354. static void k3_dma_issue_pending(struct dma_chan *chan)
  355. {
  356. struct k3_dma_chan *c = to_k3_chan(chan);
  357. struct k3_dma_dev *d = to_k3_dma(chan->device);
  358. unsigned long flags;
  359. spin_lock_irqsave(&c->vc.lock, flags);
  360. /* add request to vc->desc_issued */
  361. if (vchan_issue_pending(&c->vc)) {
  362. spin_lock(&d->lock);
  363. if (!c->phy) {
  364. if (list_empty(&c->node)) {
  365. /* if new channel, add chan_pending */
  366. list_add_tail(&c->node, &d->chan_pending);
  367. /* check in tasklet */
  368. tasklet_schedule(&d->task);
  369. dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
  370. }
  371. }
  372. spin_unlock(&d->lock);
  373. } else
  374. dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
  375. spin_unlock_irqrestore(&c->vc.lock, flags);
  376. }
  377. static void k3_dma_fill_desc(struct k3_dma_desc_sw *ds, dma_addr_t dst,
  378. dma_addr_t src, size_t len, u32 num, u32 ccfg)
  379. {
  380. if (num != ds->desc_num - 1)
  381. ds->desc_hw[num].lli = ds->desc_hw_lli + (num + 1) *
  382. sizeof(struct k3_desc_hw);
  383. ds->desc_hw[num].lli |= CX_LLI_CHAIN_EN;
  384. ds->desc_hw[num].count = len;
  385. ds->desc_hw[num].saddr = src;
  386. ds->desc_hw[num].daddr = dst;
  387. ds->desc_hw[num].config = ccfg;
  388. }
  389. static struct k3_dma_desc_sw *k3_dma_alloc_desc_resource(int num,
  390. struct dma_chan *chan)
  391. {
  392. struct k3_dma_chan *c = to_k3_chan(chan);
  393. struct k3_dma_desc_sw *ds;
  394. struct k3_dma_dev *d = to_k3_dma(chan->device);
  395. int lli_limit = LLI_BLOCK_SIZE / sizeof(struct k3_desc_hw);
  396. if (num > lli_limit) {
  397. dev_dbg(chan->device->dev, "vch %p: sg num %d exceed max %d\n",
  398. &c->vc, num, lli_limit);
  399. return NULL;
  400. }
  401. ds = kzalloc(sizeof(*ds), GFP_NOWAIT);
  402. if (!ds)
  403. return NULL;
  404. ds->desc_hw = dma_pool_alloc(d->pool, GFP_NOWAIT, &ds->desc_hw_lli);
  405. if (!ds->desc_hw) {
  406. dev_dbg(chan->device->dev, "vch %p: dma alloc fail\n", &c->vc);
  407. kfree(ds);
  408. return NULL;
  409. }
  410. memset(ds->desc_hw, 0, sizeof(struct k3_desc_hw) * num);
  411. ds->desc_num = num;
  412. return ds;
  413. }
  414. static struct dma_async_tx_descriptor *k3_dma_prep_memcpy(
  415. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  416. size_t len, unsigned long flags)
  417. {
  418. struct k3_dma_chan *c = to_k3_chan(chan);
  419. struct k3_dma_desc_sw *ds;
  420. size_t copy = 0;
  421. int num = 0;
  422. if (!len)
  423. return NULL;
  424. num = DIV_ROUND_UP(len, DMA_MAX_SIZE);
  425. ds = k3_dma_alloc_desc_resource(num, chan);
  426. if (!ds)
  427. return NULL;
  428. c->cyclic = 0;
  429. ds->size = len;
  430. num = 0;
  431. if (!c->ccfg) {
  432. /* default is memtomem, without calling device_config */
  433. c->ccfg = CX_CFG_SRCINCR | CX_CFG_DSTINCR | CX_CFG_EN;
  434. c->ccfg |= (0xf << 20) | (0xf << 24); /* burst = 16 */
  435. c->ccfg |= (0x3 << 12) | (0x3 << 16); /* width = 64 bit */
  436. }
  437. do {
  438. copy = min_t(size_t, len, DMA_MAX_SIZE);
  439. k3_dma_fill_desc(ds, dst, src, copy, num++, c->ccfg);
  440. if (c->dir == DMA_MEM_TO_DEV) {
  441. src += copy;
  442. } else if (c->dir == DMA_DEV_TO_MEM) {
  443. dst += copy;
  444. } else {
  445. src += copy;
  446. dst += copy;
  447. }
  448. len -= copy;
  449. } while (len);
  450. ds->desc_hw[num-1].lli = 0; /* end of link */
  451. return vchan_tx_prep(&c->vc, &ds->vd, flags);
  452. }
  453. static struct dma_async_tx_descriptor *k3_dma_prep_slave_sg(
  454. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
  455. enum dma_transfer_direction dir, unsigned long flags, void *context)
  456. {
  457. struct k3_dma_chan *c = to_k3_chan(chan);
  458. struct k3_dma_desc_sw *ds;
  459. size_t len, avail, total = 0;
  460. struct scatterlist *sg;
  461. dma_addr_t addr, src = 0, dst = 0;
  462. int num = sglen, i;
  463. if (sgl == NULL)
  464. return NULL;
  465. c->cyclic = 0;
  466. for_each_sg(sgl, sg, sglen, i) {
  467. avail = sg_dma_len(sg);
  468. if (avail > DMA_MAX_SIZE)
  469. num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
  470. }
  471. ds = k3_dma_alloc_desc_resource(num, chan);
  472. if (!ds)
  473. return NULL;
  474. num = 0;
  475. for_each_sg(sgl, sg, sglen, i) {
  476. addr = sg_dma_address(sg);
  477. avail = sg_dma_len(sg);
  478. total += avail;
  479. do {
  480. len = min_t(size_t, avail, DMA_MAX_SIZE);
  481. if (dir == DMA_MEM_TO_DEV) {
  482. src = addr;
  483. dst = c->dev_addr;
  484. } else if (dir == DMA_DEV_TO_MEM) {
  485. src = c->dev_addr;
  486. dst = addr;
  487. }
  488. k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg);
  489. addr += len;
  490. avail -= len;
  491. } while (avail);
  492. }
  493. ds->desc_hw[num-1].lli = 0; /* end of link */
  494. ds->size = total;
  495. return vchan_tx_prep(&c->vc, &ds->vd, flags);
  496. }
  497. static struct dma_async_tx_descriptor *
  498. k3_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
  499. size_t buf_len, size_t period_len,
  500. enum dma_transfer_direction dir,
  501. unsigned long flags)
  502. {
  503. struct k3_dma_chan *c = to_k3_chan(chan);
  504. struct k3_dma_desc_sw *ds;
  505. size_t len, avail, total = 0;
  506. dma_addr_t addr, src = 0, dst = 0;
  507. int num = 1, since = 0;
  508. size_t modulo = DMA_CYCLIC_MAX_PERIOD;
  509. u32 en_tc2 = 0;
  510. dev_dbg(chan->device->dev, "%s: buf %pad, dst %pad, buf len %zu, period_len = %zu, dir %d\n",
  511. __func__, &buf_addr, &to_k3_chan(chan)->dev_addr,
  512. buf_len, period_len, (int)dir);
  513. avail = buf_len;
  514. if (avail > modulo)
  515. num += DIV_ROUND_UP(avail, modulo) - 1;
  516. ds = k3_dma_alloc_desc_resource(num, chan);
  517. if (!ds)
  518. return NULL;
  519. c->cyclic = 1;
  520. addr = buf_addr;
  521. avail = buf_len;
  522. total = avail;
  523. num = 0;
  524. if (period_len < modulo)
  525. modulo = period_len;
  526. do {
  527. len = min_t(size_t, avail, modulo);
  528. if (dir == DMA_MEM_TO_DEV) {
  529. src = addr;
  530. dst = c->dev_addr;
  531. } else if (dir == DMA_DEV_TO_MEM) {
  532. src = c->dev_addr;
  533. dst = addr;
  534. }
  535. since += len;
  536. if (since >= period_len) {
  537. /* descriptor asks for TC2 interrupt on completion */
  538. en_tc2 = CX_CFG_NODEIRQ;
  539. since -= period_len;
  540. } else
  541. en_tc2 = 0;
  542. k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg | en_tc2);
  543. addr += len;
  544. avail -= len;
  545. } while (avail);
  546. /* "Cyclic" == end of link points back to start of link */
  547. ds->desc_hw[num - 1].lli |= ds->desc_hw_lli;
  548. ds->size = total;
  549. return vchan_tx_prep(&c->vc, &ds->vd, flags);
  550. }
  551. static int k3_dma_config(struct dma_chan *chan,
  552. struct dma_slave_config *cfg)
  553. {
  554. struct k3_dma_chan *c = to_k3_chan(chan);
  555. u32 maxburst = 0, val = 0;
  556. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  557. if (cfg == NULL)
  558. return -EINVAL;
  559. c->dir = cfg->direction;
  560. if (c->dir == DMA_DEV_TO_MEM) {
  561. c->ccfg = CX_CFG_DSTINCR;
  562. c->dev_addr = cfg->src_addr;
  563. maxburst = cfg->src_maxburst;
  564. width = cfg->src_addr_width;
  565. } else if (c->dir == DMA_MEM_TO_DEV) {
  566. c->ccfg = CX_CFG_SRCINCR;
  567. c->dev_addr = cfg->dst_addr;
  568. maxburst = cfg->dst_maxburst;
  569. width = cfg->dst_addr_width;
  570. }
  571. switch (width) {
  572. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  573. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  574. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  575. case DMA_SLAVE_BUSWIDTH_8_BYTES:
  576. val = __ffs(width);
  577. break;
  578. default:
  579. val = 3;
  580. break;
  581. }
  582. c->ccfg |= (val << 12) | (val << 16);
  583. if ((maxburst == 0) || (maxburst > 16))
  584. val = 15;
  585. else
  586. val = maxburst - 1;
  587. c->ccfg |= (val << 20) | (val << 24);
  588. c->ccfg |= CX_CFG_MEM2PER | CX_CFG_EN;
  589. /* specific request line */
  590. c->ccfg |= c->vc.chan.chan_id << 4;
  591. return 0;
  592. }
  593. static void k3_dma_free_desc(struct virt_dma_desc *vd)
  594. {
  595. struct k3_dma_desc_sw *ds =
  596. container_of(vd, struct k3_dma_desc_sw, vd);
  597. struct k3_dma_dev *d = to_k3_dma(vd->tx.chan->device);
  598. dma_pool_free(d->pool, ds->desc_hw, ds->desc_hw_lli);
  599. kfree(ds);
  600. }
  601. static int k3_dma_terminate_all(struct dma_chan *chan)
  602. {
  603. struct k3_dma_chan *c = to_k3_chan(chan);
  604. struct k3_dma_dev *d = to_k3_dma(chan->device);
  605. struct k3_dma_phy *p = c->phy;
  606. unsigned long flags;
  607. LIST_HEAD(head);
  608. dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
  609. /* Prevent this channel being scheduled */
  610. spin_lock(&d->lock);
  611. list_del_init(&c->node);
  612. spin_unlock(&d->lock);
  613. /* Clear the tx descriptor lists */
  614. spin_lock_irqsave(&c->vc.lock, flags);
  615. vchan_get_all_descriptors(&c->vc, &head);
  616. if (p) {
  617. /* vchan is assigned to a pchan - stop the channel */
  618. k3_dma_terminate_chan(p, d);
  619. c->phy = NULL;
  620. p->vchan = NULL;
  621. if (p->ds_run) {
  622. k3_dma_free_desc(&p->ds_run->vd);
  623. p->ds_run = NULL;
  624. }
  625. if (p->ds_done) {
  626. k3_dma_free_desc(&p->ds_done->vd);
  627. p->ds_done = NULL;
  628. }
  629. }
  630. spin_unlock_irqrestore(&c->vc.lock, flags);
  631. vchan_dma_desc_free_list(&c->vc, &head);
  632. return 0;
  633. }
  634. static int k3_dma_transfer_pause(struct dma_chan *chan)
  635. {
  636. struct k3_dma_chan *c = to_k3_chan(chan);
  637. struct k3_dma_dev *d = to_k3_dma(chan->device);
  638. struct k3_dma_phy *p = c->phy;
  639. dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
  640. if (c->status == DMA_IN_PROGRESS) {
  641. c->status = DMA_PAUSED;
  642. if (p) {
  643. k3_dma_pause_dma(p, false);
  644. } else {
  645. spin_lock(&d->lock);
  646. list_del_init(&c->node);
  647. spin_unlock(&d->lock);
  648. }
  649. }
  650. return 0;
  651. }
  652. static int k3_dma_transfer_resume(struct dma_chan *chan)
  653. {
  654. struct k3_dma_chan *c = to_k3_chan(chan);
  655. struct k3_dma_dev *d = to_k3_dma(chan->device);
  656. struct k3_dma_phy *p = c->phy;
  657. unsigned long flags;
  658. dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
  659. spin_lock_irqsave(&c->vc.lock, flags);
  660. if (c->status == DMA_PAUSED) {
  661. c->status = DMA_IN_PROGRESS;
  662. if (p) {
  663. k3_dma_pause_dma(p, true);
  664. } else if (!list_empty(&c->vc.desc_issued)) {
  665. spin_lock(&d->lock);
  666. list_add_tail(&c->node, &d->chan_pending);
  667. spin_unlock(&d->lock);
  668. }
  669. }
  670. spin_unlock_irqrestore(&c->vc.lock, flags);
  671. return 0;
  672. }
  673. static const struct of_device_id k3_pdma_dt_ids[] = {
  674. { .compatible = "hisilicon,k3-dma-1.0", },
  675. {}
  676. };
  677. MODULE_DEVICE_TABLE(of, k3_pdma_dt_ids);
  678. static struct dma_chan *k3_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  679. struct of_dma *ofdma)
  680. {
  681. struct k3_dma_dev *d = ofdma->of_dma_data;
  682. unsigned int request = dma_spec->args[0];
  683. if (request > d->dma_requests)
  684. return NULL;
  685. return dma_get_slave_channel(&(d->chans[request].vc.chan));
  686. }
  687. static int k3_dma_probe(struct platform_device *op)
  688. {
  689. struct k3_dma_dev *d;
  690. const struct of_device_id *of_id;
  691. struct resource *iores;
  692. int i, ret, irq = 0;
  693. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  694. if (!iores)
  695. return -EINVAL;
  696. d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
  697. if (!d)
  698. return -ENOMEM;
  699. d->base = devm_ioremap_resource(&op->dev, iores);
  700. if (IS_ERR(d->base))
  701. return PTR_ERR(d->base);
  702. of_id = of_match_device(k3_pdma_dt_ids, &op->dev);
  703. if (of_id) {
  704. of_property_read_u32((&op->dev)->of_node,
  705. "dma-channels", &d->dma_channels);
  706. of_property_read_u32((&op->dev)->of_node,
  707. "dma-requests", &d->dma_requests);
  708. }
  709. d->clk = devm_clk_get(&op->dev, NULL);
  710. if (IS_ERR(d->clk)) {
  711. dev_err(&op->dev, "no dma clk\n");
  712. return PTR_ERR(d->clk);
  713. }
  714. irq = platform_get_irq(op, 0);
  715. ret = devm_request_irq(&op->dev, irq,
  716. k3_dma_int_handler, 0, DRIVER_NAME, d);
  717. if (ret)
  718. return ret;
  719. d->irq = irq;
  720. /* A DMA memory pool for LLIs, align on 32-byte boundary */
  721. d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
  722. LLI_BLOCK_SIZE, 32, 0);
  723. if (!d->pool)
  724. return -ENOMEM;
  725. /* init phy channel */
  726. d->phy = devm_kzalloc(&op->dev,
  727. d->dma_channels * sizeof(struct k3_dma_phy), GFP_KERNEL);
  728. if (d->phy == NULL)
  729. return -ENOMEM;
  730. for (i = 0; i < d->dma_channels; i++) {
  731. struct k3_dma_phy *p = &d->phy[i];
  732. p->idx = i;
  733. p->base = d->base + i * 0x40;
  734. }
  735. INIT_LIST_HEAD(&d->slave.channels);
  736. dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
  737. dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
  738. dma_cap_set(DMA_CYCLIC, d->slave.cap_mask);
  739. d->slave.dev = &op->dev;
  740. d->slave.device_free_chan_resources = k3_dma_free_chan_resources;
  741. d->slave.device_tx_status = k3_dma_tx_status;
  742. d->slave.device_prep_dma_memcpy = k3_dma_prep_memcpy;
  743. d->slave.device_prep_slave_sg = k3_dma_prep_slave_sg;
  744. d->slave.device_prep_dma_cyclic = k3_dma_prep_dma_cyclic;
  745. d->slave.device_issue_pending = k3_dma_issue_pending;
  746. d->slave.device_config = k3_dma_config;
  747. d->slave.device_pause = k3_dma_transfer_pause;
  748. d->slave.device_resume = k3_dma_transfer_resume;
  749. d->slave.device_terminate_all = k3_dma_terminate_all;
  750. d->slave.copy_align = DMAENGINE_ALIGN_8_BYTES;
  751. /* init virtual channel */
  752. d->chans = devm_kzalloc(&op->dev,
  753. d->dma_requests * sizeof(struct k3_dma_chan), GFP_KERNEL);
  754. if (d->chans == NULL)
  755. return -ENOMEM;
  756. for (i = 0; i < d->dma_requests; i++) {
  757. struct k3_dma_chan *c = &d->chans[i];
  758. c->status = DMA_IN_PROGRESS;
  759. INIT_LIST_HEAD(&c->node);
  760. c->vc.desc_free = k3_dma_free_desc;
  761. vchan_init(&c->vc, &d->slave);
  762. }
  763. /* Enable clock before accessing registers */
  764. ret = clk_prepare_enable(d->clk);
  765. if (ret < 0) {
  766. dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
  767. return ret;
  768. }
  769. k3_dma_enable_dma(d, true);
  770. ret = dma_async_device_register(&d->slave);
  771. if (ret)
  772. goto dma_async_register_fail;
  773. ret = of_dma_controller_register((&op->dev)->of_node,
  774. k3_of_dma_simple_xlate, d);
  775. if (ret)
  776. goto of_dma_register_fail;
  777. spin_lock_init(&d->lock);
  778. INIT_LIST_HEAD(&d->chan_pending);
  779. tasklet_init(&d->task, k3_dma_tasklet, (unsigned long)d);
  780. platform_set_drvdata(op, d);
  781. dev_info(&op->dev, "initialized\n");
  782. return 0;
  783. of_dma_register_fail:
  784. dma_async_device_unregister(&d->slave);
  785. dma_async_register_fail:
  786. clk_disable_unprepare(d->clk);
  787. return ret;
  788. }
  789. static int k3_dma_remove(struct platform_device *op)
  790. {
  791. struct k3_dma_chan *c, *cn;
  792. struct k3_dma_dev *d = platform_get_drvdata(op);
  793. dma_async_device_unregister(&d->slave);
  794. of_dma_controller_free((&op->dev)->of_node);
  795. devm_free_irq(&op->dev, d->irq, d);
  796. list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
  797. list_del(&c->vc.chan.device_node);
  798. tasklet_kill(&c->vc.task);
  799. }
  800. tasklet_kill(&d->task);
  801. clk_disable_unprepare(d->clk);
  802. return 0;
  803. }
  804. #ifdef CONFIG_PM_SLEEP
  805. static int k3_dma_suspend_dev(struct device *dev)
  806. {
  807. struct k3_dma_dev *d = dev_get_drvdata(dev);
  808. u32 stat = 0;
  809. stat = k3_dma_get_chan_stat(d);
  810. if (stat) {
  811. dev_warn(d->slave.dev,
  812. "chan %d is running fail to suspend\n", stat);
  813. return -1;
  814. }
  815. k3_dma_enable_dma(d, false);
  816. clk_disable_unprepare(d->clk);
  817. return 0;
  818. }
  819. static int k3_dma_resume_dev(struct device *dev)
  820. {
  821. struct k3_dma_dev *d = dev_get_drvdata(dev);
  822. int ret = 0;
  823. ret = clk_prepare_enable(d->clk);
  824. if (ret < 0) {
  825. dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
  826. return ret;
  827. }
  828. k3_dma_enable_dma(d, true);
  829. return 0;
  830. }
  831. #endif
  832. static SIMPLE_DEV_PM_OPS(k3_dma_pmops, k3_dma_suspend_dev, k3_dma_resume_dev);
  833. static struct platform_driver k3_pdma_driver = {
  834. .driver = {
  835. .name = DRIVER_NAME,
  836. .pm = &k3_dma_pmops,
  837. .of_match_table = k3_pdma_dt_ids,
  838. },
  839. .probe = k3_dma_probe,
  840. .remove = k3_dma_remove,
  841. };
  842. module_platform_driver(k3_pdma_driver);
  843. MODULE_DESCRIPTION("Hisilicon k3 DMA Driver");
  844. MODULE_ALIAS("platform:k3dma");
  845. MODULE_LICENSE("GPL v2");