k3dma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. k3_dma_free_desc(&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 int k3_dma_transfer_pause(struct dma_chan *chan)
  636. {
  637. struct k3_dma_chan *c = to_k3_chan(chan);
  638. struct k3_dma_dev *d = to_k3_dma(chan->device);
  639. struct k3_dma_phy *p = c->phy;
  640. dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
  641. if (c->status == DMA_IN_PROGRESS) {
  642. c->status = DMA_PAUSED;
  643. if (p) {
  644. k3_dma_pause_dma(p, false);
  645. } else {
  646. spin_lock(&d->lock);
  647. list_del_init(&c->node);
  648. spin_unlock(&d->lock);
  649. }
  650. }
  651. return 0;
  652. }
  653. static int k3_dma_transfer_resume(struct dma_chan *chan)
  654. {
  655. struct k3_dma_chan *c = to_k3_chan(chan);
  656. struct k3_dma_dev *d = to_k3_dma(chan->device);
  657. struct k3_dma_phy *p = c->phy;
  658. unsigned long flags;
  659. dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
  660. spin_lock_irqsave(&c->vc.lock, flags);
  661. if (c->status == DMA_PAUSED) {
  662. c->status = DMA_IN_PROGRESS;
  663. if (p) {
  664. k3_dma_pause_dma(p, true);
  665. } else if (!list_empty(&c->vc.desc_issued)) {
  666. spin_lock(&d->lock);
  667. list_add_tail(&c->node, &d->chan_pending);
  668. spin_unlock(&d->lock);
  669. }
  670. }
  671. spin_unlock_irqrestore(&c->vc.lock, flags);
  672. return 0;
  673. }
  674. static const struct of_device_id k3_pdma_dt_ids[] = {
  675. { .compatible = "hisilicon,k3-dma-1.0", },
  676. {}
  677. };
  678. MODULE_DEVICE_TABLE(of, k3_pdma_dt_ids);
  679. static struct dma_chan *k3_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  680. struct of_dma *ofdma)
  681. {
  682. struct k3_dma_dev *d = ofdma->of_dma_data;
  683. unsigned int request = dma_spec->args[0];
  684. if (request >= d->dma_requests)
  685. return NULL;
  686. return dma_get_slave_channel(&(d->chans[request].vc.chan));
  687. }
  688. static int k3_dma_probe(struct platform_device *op)
  689. {
  690. struct k3_dma_dev *d;
  691. const struct of_device_id *of_id;
  692. struct resource *iores;
  693. int i, ret, irq = 0;
  694. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  695. if (!iores)
  696. return -EINVAL;
  697. d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
  698. if (!d)
  699. return -ENOMEM;
  700. d->base = devm_ioremap_resource(&op->dev, iores);
  701. if (IS_ERR(d->base))
  702. return PTR_ERR(d->base);
  703. of_id = of_match_device(k3_pdma_dt_ids, &op->dev);
  704. if (of_id) {
  705. of_property_read_u32((&op->dev)->of_node,
  706. "dma-channels", &d->dma_channels);
  707. of_property_read_u32((&op->dev)->of_node,
  708. "dma-requests", &d->dma_requests);
  709. }
  710. d->clk = devm_clk_get(&op->dev, NULL);
  711. if (IS_ERR(d->clk)) {
  712. dev_err(&op->dev, "no dma clk\n");
  713. return PTR_ERR(d->clk);
  714. }
  715. irq = platform_get_irq(op, 0);
  716. ret = devm_request_irq(&op->dev, irq,
  717. k3_dma_int_handler, 0, DRIVER_NAME, d);
  718. if (ret)
  719. return ret;
  720. d->irq = irq;
  721. /* A DMA memory pool for LLIs, align on 32-byte boundary */
  722. d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
  723. LLI_BLOCK_SIZE, 32, 0);
  724. if (!d->pool)
  725. return -ENOMEM;
  726. /* init phy channel */
  727. d->phy = devm_kzalloc(&op->dev,
  728. d->dma_channels * sizeof(struct k3_dma_phy), GFP_KERNEL);
  729. if (d->phy == NULL)
  730. return -ENOMEM;
  731. for (i = 0; i < d->dma_channels; i++) {
  732. struct k3_dma_phy *p = &d->phy[i];
  733. p->idx = i;
  734. p->base = d->base + i * 0x40;
  735. }
  736. INIT_LIST_HEAD(&d->slave.channels);
  737. dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
  738. dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
  739. dma_cap_set(DMA_CYCLIC, d->slave.cap_mask);
  740. d->slave.dev = &op->dev;
  741. d->slave.device_free_chan_resources = k3_dma_free_chan_resources;
  742. d->slave.device_tx_status = k3_dma_tx_status;
  743. d->slave.device_prep_dma_memcpy = k3_dma_prep_memcpy;
  744. d->slave.device_prep_slave_sg = k3_dma_prep_slave_sg;
  745. d->slave.device_prep_dma_cyclic = k3_dma_prep_dma_cyclic;
  746. d->slave.device_issue_pending = k3_dma_issue_pending;
  747. d->slave.device_config = k3_dma_config;
  748. d->slave.device_pause = k3_dma_transfer_pause;
  749. d->slave.device_resume = k3_dma_transfer_resume;
  750. d->slave.device_terminate_all = k3_dma_terminate_all;
  751. d->slave.copy_align = DMAENGINE_ALIGN_8_BYTES;
  752. /* init virtual channel */
  753. d->chans = devm_kzalloc(&op->dev,
  754. d->dma_requests * sizeof(struct k3_dma_chan), GFP_KERNEL);
  755. if (d->chans == NULL)
  756. return -ENOMEM;
  757. for (i = 0; i < d->dma_requests; i++) {
  758. struct k3_dma_chan *c = &d->chans[i];
  759. c->status = DMA_IN_PROGRESS;
  760. INIT_LIST_HEAD(&c->node);
  761. c->vc.desc_free = k3_dma_free_desc;
  762. vchan_init(&c->vc, &d->slave);
  763. }
  764. /* Enable clock before accessing registers */
  765. ret = clk_prepare_enable(d->clk);
  766. if (ret < 0) {
  767. dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
  768. return ret;
  769. }
  770. k3_dma_enable_dma(d, true);
  771. ret = dma_async_device_register(&d->slave);
  772. if (ret)
  773. goto dma_async_register_fail;
  774. ret = of_dma_controller_register((&op->dev)->of_node,
  775. k3_of_dma_simple_xlate, d);
  776. if (ret)
  777. goto of_dma_register_fail;
  778. spin_lock_init(&d->lock);
  779. INIT_LIST_HEAD(&d->chan_pending);
  780. tasklet_init(&d->task, k3_dma_tasklet, (unsigned long)d);
  781. platform_set_drvdata(op, d);
  782. dev_info(&op->dev, "initialized\n");
  783. return 0;
  784. of_dma_register_fail:
  785. dma_async_device_unregister(&d->slave);
  786. dma_async_register_fail:
  787. clk_disable_unprepare(d->clk);
  788. return ret;
  789. }
  790. static int k3_dma_remove(struct platform_device *op)
  791. {
  792. struct k3_dma_chan *c, *cn;
  793. struct k3_dma_dev *d = platform_get_drvdata(op);
  794. dma_async_device_unregister(&d->slave);
  795. of_dma_controller_free((&op->dev)->of_node);
  796. devm_free_irq(&op->dev, d->irq, d);
  797. list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
  798. list_del(&c->vc.chan.device_node);
  799. tasklet_kill(&c->vc.task);
  800. }
  801. tasklet_kill(&d->task);
  802. clk_disable_unprepare(d->clk);
  803. return 0;
  804. }
  805. #ifdef CONFIG_PM_SLEEP
  806. static int k3_dma_suspend_dev(struct device *dev)
  807. {
  808. struct k3_dma_dev *d = dev_get_drvdata(dev);
  809. u32 stat = 0;
  810. stat = k3_dma_get_chan_stat(d);
  811. if (stat) {
  812. dev_warn(d->slave.dev,
  813. "chan %d is running fail to suspend\n", stat);
  814. return -1;
  815. }
  816. k3_dma_enable_dma(d, false);
  817. clk_disable_unprepare(d->clk);
  818. return 0;
  819. }
  820. static int k3_dma_resume_dev(struct device *dev)
  821. {
  822. struct k3_dma_dev *d = dev_get_drvdata(dev);
  823. int ret = 0;
  824. ret = clk_prepare_enable(d->clk);
  825. if (ret < 0) {
  826. dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
  827. return ret;
  828. }
  829. k3_dma_enable_dma(d, true);
  830. return 0;
  831. }
  832. #endif
  833. static SIMPLE_DEV_PM_OPS(k3_dma_pmops, k3_dma_suspend_dev, k3_dma_resume_dev);
  834. static struct platform_driver k3_pdma_driver = {
  835. .driver = {
  836. .name = DRIVER_NAME,
  837. .pm = &k3_dma_pmops,
  838. .of_match_table = k3_pdma_dt_ids,
  839. },
  840. .probe = k3_dma_probe,
  841. .remove = k3_dma_remove,
  842. };
  843. module_platform_driver(k3_pdma_driver);
  844. MODULE_DESCRIPTION("Hisilicon k3 DMA Driver");
  845. MODULE_ALIAS("platform:k3dma");
  846. MODULE_LICENSE("GPL v2");