zx296702_dma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * Copyright 2015 Linaro.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/device.h>
  10. #include <linux/dmaengine.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of.h>
  22. #include <linux/clk.h>
  23. #include <linux/of_dma.h>
  24. #include "virt-dma.h"
  25. #define DRIVER_NAME "zx-dma"
  26. #define DMA_ALIGN 4
  27. #define DMA_MAX_SIZE (0x10000 - 512)
  28. #define LLI_BLOCK_SIZE (4 * PAGE_SIZE)
  29. #define REG_ZX_SRC_ADDR 0x00
  30. #define REG_ZX_DST_ADDR 0x04
  31. #define REG_ZX_TX_X_COUNT 0x08
  32. #define REG_ZX_TX_ZY_COUNT 0x0c
  33. #define REG_ZX_SRC_ZY_STEP 0x10
  34. #define REG_ZX_DST_ZY_STEP 0x14
  35. #define REG_ZX_LLI_ADDR 0x1c
  36. #define REG_ZX_CTRL 0x20
  37. #define REG_ZX_TC_IRQ 0x800
  38. #define REG_ZX_SRC_ERR_IRQ 0x804
  39. #define REG_ZX_DST_ERR_IRQ 0x808
  40. #define REG_ZX_CFG_ERR_IRQ 0x80c
  41. #define REG_ZX_TC_IRQ_RAW 0x810
  42. #define REG_ZX_SRC_ERR_IRQ_RAW 0x814
  43. #define REG_ZX_DST_ERR_IRQ_RAW 0x818
  44. #define REG_ZX_CFG_ERR_IRQ_RAW 0x81c
  45. #define REG_ZX_STATUS 0x820
  46. #define REG_ZX_DMA_GRP_PRIO 0x824
  47. #define REG_ZX_DMA_ARB 0x828
  48. #define ZX_FORCE_CLOSE BIT(31)
  49. #define ZX_DST_BURST_WIDTH(x) (((x) & 0x7) << 13)
  50. #define ZX_MAX_BURST_LEN 16
  51. #define ZX_SRC_BURST_LEN(x) (((x) & 0xf) << 9)
  52. #define ZX_SRC_BURST_WIDTH(x) (((x) & 0x7) << 6)
  53. #define ZX_IRQ_ENABLE_ALL (3 << 4)
  54. #define ZX_DST_FIFO_MODE BIT(3)
  55. #define ZX_SRC_FIFO_MODE BIT(2)
  56. #define ZX_SOFT_REQ BIT(1)
  57. #define ZX_CH_ENABLE BIT(0)
  58. #define ZX_DMA_BUSWIDTHS \
  59. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  60. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  61. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  62. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
  63. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  64. enum zx_dma_burst_width {
  65. ZX_DMA_WIDTH_8BIT = 0,
  66. ZX_DMA_WIDTH_16BIT = 1,
  67. ZX_DMA_WIDTH_32BIT = 2,
  68. ZX_DMA_WIDTH_64BIT = 3,
  69. };
  70. struct zx_desc_hw {
  71. u32 saddr;
  72. u32 daddr;
  73. u32 src_x;
  74. u32 src_zy;
  75. u32 src_zy_step;
  76. u32 dst_zy_step;
  77. u32 reserved1;
  78. u32 lli;
  79. u32 ctr;
  80. u32 reserved[7]; /* pack as hardware registers region size */
  81. } __aligned(32);
  82. struct zx_dma_desc_sw {
  83. struct virt_dma_desc vd;
  84. dma_addr_t desc_hw_lli;
  85. size_t desc_num;
  86. size_t size;
  87. struct zx_desc_hw *desc_hw;
  88. };
  89. struct zx_dma_phy;
  90. struct zx_dma_chan {
  91. struct dma_slave_config slave_cfg;
  92. int id; /* Request phy chan id */
  93. u32 ccfg;
  94. u32 cyclic;
  95. struct virt_dma_chan vc;
  96. struct zx_dma_phy *phy;
  97. struct list_head node;
  98. dma_addr_t dev_addr;
  99. enum dma_status status;
  100. };
  101. struct zx_dma_phy {
  102. u32 idx;
  103. void __iomem *base;
  104. struct zx_dma_chan *vchan;
  105. struct zx_dma_desc_sw *ds_run;
  106. struct zx_dma_desc_sw *ds_done;
  107. };
  108. struct zx_dma_dev {
  109. struct dma_device slave;
  110. void __iomem *base;
  111. spinlock_t lock; /* lock for ch and phy */
  112. struct list_head chan_pending;
  113. struct zx_dma_phy *phy;
  114. struct zx_dma_chan *chans;
  115. struct clk *clk;
  116. struct dma_pool *pool;
  117. u32 dma_channels;
  118. u32 dma_requests;
  119. int irq;
  120. };
  121. #define to_zx_dma(dmadev) container_of(dmadev, struct zx_dma_dev, slave)
  122. static struct zx_dma_chan *to_zx_chan(struct dma_chan *chan)
  123. {
  124. return container_of(chan, struct zx_dma_chan, vc.chan);
  125. }
  126. static void zx_dma_terminate_chan(struct zx_dma_phy *phy, struct zx_dma_dev *d)
  127. {
  128. u32 val = 0;
  129. val = readl_relaxed(phy->base + REG_ZX_CTRL);
  130. val &= ~ZX_CH_ENABLE;
  131. val |= ZX_FORCE_CLOSE;
  132. writel_relaxed(val, phy->base + REG_ZX_CTRL);
  133. val = 0x1 << phy->idx;
  134. writel_relaxed(val, d->base + REG_ZX_TC_IRQ_RAW);
  135. writel_relaxed(val, d->base + REG_ZX_SRC_ERR_IRQ_RAW);
  136. writel_relaxed(val, d->base + REG_ZX_DST_ERR_IRQ_RAW);
  137. writel_relaxed(val, d->base + REG_ZX_CFG_ERR_IRQ_RAW);
  138. }
  139. static void zx_dma_set_desc(struct zx_dma_phy *phy, struct zx_desc_hw *hw)
  140. {
  141. writel_relaxed(hw->saddr, phy->base + REG_ZX_SRC_ADDR);
  142. writel_relaxed(hw->daddr, phy->base + REG_ZX_DST_ADDR);
  143. writel_relaxed(hw->src_x, phy->base + REG_ZX_TX_X_COUNT);
  144. writel_relaxed(0, phy->base + REG_ZX_TX_ZY_COUNT);
  145. writel_relaxed(0, phy->base + REG_ZX_SRC_ZY_STEP);
  146. writel_relaxed(0, phy->base + REG_ZX_DST_ZY_STEP);
  147. writel_relaxed(hw->lli, phy->base + REG_ZX_LLI_ADDR);
  148. writel_relaxed(hw->ctr, phy->base + REG_ZX_CTRL);
  149. }
  150. static u32 zx_dma_get_curr_lli(struct zx_dma_phy *phy)
  151. {
  152. return readl_relaxed(phy->base + REG_ZX_LLI_ADDR);
  153. }
  154. static u32 zx_dma_get_chan_stat(struct zx_dma_dev *d)
  155. {
  156. return readl_relaxed(d->base + REG_ZX_STATUS);
  157. }
  158. static void zx_dma_init_state(struct zx_dma_dev *d)
  159. {
  160. /* set same priority */
  161. writel_relaxed(0x0, d->base + REG_ZX_DMA_ARB);
  162. /* clear all irq */
  163. writel_relaxed(0xffffffff, d->base + REG_ZX_TC_IRQ_RAW);
  164. writel_relaxed(0xffffffff, d->base + REG_ZX_SRC_ERR_IRQ_RAW);
  165. writel_relaxed(0xffffffff, d->base + REG_ZX_DST_ERR_IRQ_RAW);
  166. writel_relaxed(0xffffffff, d->base + REG_ZX_CFG_ERR_IRQ_RAW);
  167. }
  168. static int zx_dma_start_txd(struct zx_dma_chan *c)
  169. {
  170. struct zx_dma_dev *d = to_zx_dma(c->vc.chan.device);
  171. struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  172. if (!c->phy)
  173. return -EAGAIN;
  174. if (BIT(c->phy->idx) & zx_dma_get_chan_stat(d))
  175. return -EAGAIN;
  176. if (vd) {
  177. struct zx_dma_desc_sw *ds =
  178. container_of(vd, struct zx_dma_desc_sw, vd);
  179. /*
  180. * fetch and remove request from vc->desc_issued
  181. * so vc->desc_issued only contains desc pending
  182. */
  183. list_del(&ds->vd.node);
  184. c->phy->ds_run = ds;
  185. c->phy->ds_done = NULL;
  186. /* start dma */
  187. zx_dma_set_desc(c->phy, ds->desc_hw);
  188. return 0;
  189. }
  190. c->phy->ds_done = NULL;
  191. c->phy->ds_run = NULL;
  192. return -EAGAIN;
  193. }
  194. static void zx_dma_task(struct zx_dma_dev *d)
  195. {
  196. struct zx_dma_phy *p;
  197. struct zx_dma_chan *c, *cn;
  198. unsigned pch, pch_alloc = 0;
  199. unsigned long flags;
  200. /* check new dma request of running channel in vc->desc_issued */
  201. list_for_each_entry_safe(c, cn, &d->slave.channels,
  202. vc.chan.device_node) {
  203. spin_lock_irqsave(&c->vc.lock, flags);
  204. p = c->phy;
  205. if (p && p->ds_done && zx_dma_start_txd(c)) {
  206. /* No current txd associated with this channel */
  207. dev_dbg(d->slave.dev, "pchan %u: free\n", p->idx);
  208. /* Mark this channel free */
  209. c->phy = NULL;
  210. p->vchan = NULL;
  211. }
  212. spin_unlock_irqrestore(&c->vc.lock, flags);
  213. }
  214. /* check new channel request in d->chan_pending */
  215. spin_lock_irqsave(&d->lock, flags);
  216. while (!list_empty(&d->chan_pending)) {
  217. c = list_first_entry(&d->chan_pending,
  218. struct zx_dma_chan, node);
  219. p = &d->phy[c->id];
  220. if (!p->vchan) {
  221. /* remove from d->chan_pending */
  222. list_del_init(&c->node);
  223. pch_alloc |= 1 << c->id;
  224. /* Mark this channel allocated */
  225. p->vchan = c;
  226. c->phy = p;
  227. } else {
  228. dev_dbg(d->slave.dev, "pchan %u: busy!\n", c->id);
  229. }
  230. }
  231. spin_unlock_irqrestore(&d->lock, flags);
  232. for (pch = 0; pch < d->dma_channels; pch++) {
  233. if (pch_alloc & (1 << pch)) {
  234. p = &d->phy[pch];
  235. c = p->vchan;
  236. if (c) {
  237. spin_lock_irqsave(&c->vc.lock, flags);
  238. zx_dma_start_txd(c);
  239. spin_unlock_irqrestore(&c->vc.lock, flags);
  240. }
  241. }
  242. }
  243. }
  244. static irqreturn_t zx_dma_int_handler(int irq, void *dev_id)
  245. {
  246. struct zx_dma_dev *d = (struct zx_dma_dev *)dev_id;
  247. struct zx_dma_phy *p;
  248. struct zx_dma_chan *c;
  249. u32 tc = readl_relaxed(d->base + REG_ZX_TC_IRQ);
  250. u32 serr = readl_relaxed(d->base + REG_ZX_SRC_ERR_IRQ);
  251. u32 derr = readl_relaxed(d->base + REG_ZX_DST_ERR_IRQ);
  252. u32 cfg = readl_relaxed(d->base + REG_ZX_CFG_ERR_IRQ);
  253. u32 i, irq_chan = 0, task = 0;
  254. while (tc) {
  255. i = __ffs(tc);
  256. tc &= ~BIT(i);
  257. p = &d->phy[i];
  258. c = p->vchan;
  259. if (c) {
  260. unsigned long flags;
  261. spin_lock_irqsave(&c->vc.lock, flags);
  262. if (c->cyclic) {
  263. vchan_cyclic_callback(&p->ds_run->vd);
  264. } else {
  265. vchan_cookie_complete(&p->ds_run->vd);
  266. p->ds_done = p->ds_run;
  267. task = 1;
  268. }
  269. spin_unlock_irqrestore(&c->vc.lock, flags);
  270. irq_chan |= BIT(i);
  271. }
  272. }
  273. if (serr || derr || cfg)
  274. dev_warn(d->slave.dev, "DMA ERR src 0x%x, dst 0x%x, cfg 0x%x\n",
  275. serr, derr, cfg);
  276. writel_relaxed(irq_chan, d->base + REG_ZX_TC_IRQ_RAW);
  277. writel_relaxed(serr, d->base + REG_ZX_SRC_ERR_IRQ_RAW);
  278. writel_relaxed(derr, d->base + REG_ZX_DST_ERR_IRQ_RAW);
  279. writel_relaxed(cfg, d->base + REG_ZX_CFG_ERR_IRQ_RAW);
  280. if (task)
  281. zx_dma_task(d);
  282. return IRQ_HANDLED;
  283. }
  284. static void zx_dma_free_chan_resources(struct dma_chan *chan)
  285. {
  286. struct zx_dma_chan *c = to_zx_chan(chan);
  287. struct zx_dma_dev *d = to_zx_dma(chan->device);
  288. unsigned long flags;
  289. spin_lock_irqsave(&d->lock, flags);
  290. list_del_init(&c->node);
  291. spin_unlock_irqrestore(&d->lock, flags);
  292. vchan_free_chan_resources(&c->vc);
  293. c->ccfg = 0;
  294. }
  295. static enum dma_status zx_dma_tx_status(struct dma_chan *chan,
  296. dma_cookie_t cookie,
  297. struct dma_tx_state *state)
  298. {
  299. struct zx_dma_chan *c = to_zx_chan(chan);
  300. struct zx_dma_phy *p;
  301. struct virt_dma_desc *vd;
  302. unsigned long flags;
  303. enum dma_status ret;
  304. size_t bytes = 0;
  305. ret = dma_cookie_status(&c->vc.chan, cookie, state);
  306. if (ret == DMA_COMPLETE || !state)
  307. return ret;
  308. spin_lock_irqsave(&c->vc.lock, flags);
  309. p = c->phy;
  310. ret = c->status;
  311. /*
  312. * If the cookie is on our issue queue, then the residue is
  313. * its total size.
  314. */
  315. vd = vchan_find_desc(&c->vc, cookie);
  316. if (vd) {
  317. bytes = container_of(vd, struct zx_dma_desc_sw, vd)->size;
  318. } else if ((!p) || (!p->ds_run)) {
  319. bytes = 0;
  320. } else {
  321. struct zx_dma_desc_sw *ds = p->ds_run;
  322. u32 clli = 0, index = 0;
  323. bytes = 0;
  324. clli = zx_dma_get_curr_lli(p);
  325. index = (clli - ds->desc_hw_lli) / sizeof(struct zx_desc_hw);
  326. for (; index < ds->desc_num; index++) {
  327. bytes += ds->desc_hw[index].src_x;
  328. /* end of lli */
  329. if (!ds->desc_hw[index].lli)
  330. break;
  331. }
  332. }
  333. spin_unlock_irqrestore(&c->vc.lock, flags);
  334. dma_set_residue(state, bytes);
  335. return ret;
  336. }
  337. static void zx_dma_issue_pending(struct dma_chan *chan)
  338. {
  339. struct zx_dma_chan *c = to_zx_chan(chan);
  340. struct zx_dma_dev *d = to_zx_dma(chan->device);
  341. unsigned long flags;
  342. int issue = 0;
  343. spin_lock_irqsave(&c->vc.lock, flags);
  344. /* add request to vc->desc_issued */
  345. if (vchan_issue_pending(&c->vc)) {
  346. spin_lock(&d->lock);
  347. if (!c->phy && list_empty(&c->node)) {
  348. /* if new channel, add chan_pending */
  349. list_add_tail(&c->node, &d->chan_pending);
  350. issue = 1;
  351. dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
  352. }
  353. spin_unlock(&d->lock);
  354. } else {
  355. dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
  356. }
  357. spin_unlock_irqrestore(&c->vc.lock, flags);
  358. if (issue)
  359. zx_dma_task(d);
  360. }
  361. static void zx_dma_fill_desc(struct zx_dma_desc_sw *ds, dma_addr_t dst,
  362. dma_addr_t src, size_t len, u32 num, u32 ccfg)
  363. {
  364. if ((num + 1) < ds->desc_num)
  365. ds->desc_hw[num].lli = ds->desc_hw_lli + (num + 1) *
  366. sizeof(struct zx_desc_hw);
  367. ds->desc_hw[num].saddr = src;
  368. ds->desc_hw[num].daddr = dst;
  369. ds->desc_hw[num].src_x = len;
  370. ds->desc_hw[num].ctr = ccfg;
  371. }
  372. static struct zx_dma_desc_sw *zx_alloc_desc_resource(int num,
  373. struct dma_chan *chan)
  374. {
  375. struct zx_dma_chan *c = to_zx_chan(chan);
  376. struct zx_dma_desc_sw *ds;
  377. struct zx_dma_dev *d = to_zx_dma(chan->device);
  378. int lli_limit = LLI_BLOCK_SIZE / sizeof(struct zx_desc_hw);
  379. if (num > lli_limit) {
  380. dev_dbg(chan->device->dev, "vch %p: sg num %d exceed max %d\n",
  381. &c->vc, num, lli_limit);
  382. return NULL;
  383. }
  384. ds = kzalloc(sizeof(*ds), GFP_ATOMIC);
  385. if (!ds)
  386. return NULL;
  387. ds->desc_hw = dma_pool_alloc(d->pool, GFP_NOWAIT, &ds->desc_hw_lli);
  388. if (!ds->desc_hw) {
  389. dev_dbg(chan->device->dev, "vch %p: dma alloc fail\n", &c->vc);
  390. kfree(ds);
  391. return NULL;
  392. }
  393. memset(ds->desc_hw, 0, sizeof(struct zx_desc_hw) * num);
  394. ds->desc_num = num;
  395. return ds;
  396. }
  397. static enum zx_dma_burst_width zx_dma_burst_width(enum dma_slave_buswidth width)
  398. {
  399. switch (width) {
  400. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  401. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  402. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  403. case DMA_SLAVE_BUSWIDTH_8_BYTES:
  404. return ffs(width) - 1;
  405. default:
  406. return ZX_DMA_WIDTH_32BIT;
  407. }
  408. }
  409. static int zx_pre_config(struct zx_dma_chan *c, enum dma_transfer_direction dir)
  410. {
  411. struct dma_slave_config *cfg = &c->slave_cfg;
  412. enum zx_dma_burst_width src_width;
  413. enum zx_dma_burst_width dst_width;
  414. u32 maxburst = 0;
  415. switch (dir) {
  416. case DMA_MEM_TO_MEM:
  417. c->ccfg = ZX_CH_ENABLE | ZX_SOFT_REQ
  418. | ZX_SRC_BURST_LEN(ZX_MAX_BURST_LEN - 1)
  419. | ZX_SRC_BURST_WIDTH(ZX_DMA_WIDTH_32BIT)
  420. | ZX_DST_BURST_WIDTH(ZX_DMA_WIDTH_32BIT);
  421. break;
  422. case DMA_MEM_TO_DEV:
  423. c->dev_addr = cfg->dst_addr;
  424. /* dst len is calculated from src width, len and dst width.
  425. * We need make sure dst len not exceed MAX LEN.
  426. * Trailing single transaction that does not fill a full
  427. * burst also require identical src/dst data width.
  428. */
  429. dst_width = zx_dma_burst_width(cfg->dst_addr_width);
  430. maxburst = cfg->dst_maxburst;
  431. maxburst = maxburst < ZX_MAX_BURST_LEN ?
  432. maxburst : ZX_MAX_BURST_LEN;
  433. c->ccfg = ZX_DST_FIFO_MODE | ZX_CH_ENABLE
  434. | ZX_SRC_BURST_LEN(maxburst - 1)
  435. | ZX_SRC_BURST_WIDTH(dst_width)
  436. | ZX_DST_BURST_WIDTH(dst_width);
  437. break;
  438. case DMA_DEV_TO_MEM:
  439. c->dev_addr = cfg->src_addr;
  440. src_width = zx_dma_burst_width(cfg->src_addr_width);
  441. maxburst = cfg->src_maxburst;
  442. maxburst = maxburst < ZX_MAX_BURST_LEN ?
  443. maxburst : ZX_MAX_BURST_LEN;
  444. c->ccfg = ZX_SRC_FIFO_MODE | ZX_CH_ENABLE
  445. | ZX_SRC_BURST_LEN(maxburst - 1)
  446. | ZX_SRC_BURST_WIDTH(src_width)
  447. | ZX_DST_BURST_WIDTH(src_width);
  448. break;
  449. default:
  450. return -EINVAL;
  451. }
  452. return 0;
  453. }
  454. static struct dma_async_tx_descriptor *zx_dma_prep_memcpy(
  455. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  456. size_t len, unsigned long flags)
  457. {
  458. struct zx_dma_chan *c = to_zx_chan(chan);
  459. struct zx_dma_desc_sw *ds;
  460. size_t copy = 0;
  461. int num = 0;
  462. if (!len)
  463. return NULL;
  464. if (zx_pre_config(c, DMA_MEM_TO_MEM))
  465. return NULL;
  466. num = DIV_ROUND_UP(len, DMA_MAX_SIZE);
  467. ds = zx_alloc_desc_resource(num, chan);
  468. if (!ds)
  469. return NULL;
  470. ds->size = len;
  471. num = 0;
  472. do {
  473. copy = min_t(size_t, len, DMA_MAX_SIZE);
  474. zx_dma_fill_desc(ds, dst, src, copy, num++, c->ccfg);
  475. src += copy;
  476. dst += copy;
  477. len -= copy;
  478. } while (len);
  479. c->cyclic = 0;
  480. ds->desc_hw[num - 1].lli = 0; /* end of link */
  481. ds->desc_hw[num - 1].ctr |= ZX_IRQ_ENABLE_ALL;
  482. return vchan_tx_prep(&c->vc, &ds->vd, flags);
  483. }
  484. static struct dma_async_tx_descriptor *zx_dma_prep_slave_sg(
  485. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
  486. enum dma_transfer_direction dir, unsigned long flags, void *context)
  487. {
  488. struct zx_dma_chan *c = to_zx_chan(chan);
  489. struct zx_dma_desc_sw *ds;
  490. size_t len, avail, total = 0;
  491. struct scatterlist *sg;
  492. dma_addr_t addr, src = 0, dst = 0;
  493. int num = sglen, i;
  494. if (!sgl)
  495. return NULL;
  496. if (zx_pre_config(c, dir))
  497. return NULL;
  498. for_each_sg(sgl, sg, sglen, i) {
  499. avail = sg_dma_len(sg);
  500. if (avail > DMA_MAX_SIZE)
  501. num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
  502. }
  503. ds = zx_alloc_desc_resource(num, chan);
  504. if (!ds)
  505. return NULL;
  506. c->cyclic = 0;
  507. num = 0;
  508. for_each_sg(sgl, sg, sglen, i) {
  509. addr = sg_dma_address(sg);
  510. avail = sg_dma_len(sg);
  511. total += avail;
  512. do {
  513. len = min_t(size_t, avail, DMA_MAX_SIZE);
  514. if (dir == DMA_MEM_TO_DEV) {
  515. src = addr;
  516. dst = c->dev_addr;
  517. } else if (dir == DMA_DEV_TO_MEM) {
  518. src = c->dev_addr;
  519. dst = addr;
  520. }
  521. zx_dma_fill_desc(ds, dst, src, len, num++, c->ccfg);
  522. addr += len;
  523. avail -= len;
  524. } while (avail);
  525. }
  526. ds->desc_hw[num - 1].lli = 0; /* end of link */
  527. ds->desc_hw[num - 1].ctr |= ZX_IRQ_ENABLE_ALL;
  528. ds->size = total;
  529. return vchan_tx_prep(&c->vc, &ds->vd, flags);
  530. }
  531. static struct dma_async_tx_descriptor *zx_dma_prep_dma_cyclic(
  532. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  533. size_t period_len, enum dma_transfer_direction dir,
  534. unsigned long flags)
  535. {
  536. struct zx_dma_chan *c = to_zx_chan(chan);
  537. struct zx_dma_desc_sw *ds;
  538. dma_addr_t src = 0, dst = 0;
  539. int num_periods = buf_len / period_len;
  540. int buf = 0, num = 0;
  541. if (period_len > DMA_MAX_SIZE) {
  542. dev_err(chan->device->dev, "maximum period size exceeded\n");
  543. return NULL;
  544. }
  545. if (zx_pre_config(c, dir))
  546. return NULL;
  547. ds = zx_alloc_desc_resource(num_periods, chan);
  548. if (!ds)
  549. return NULL;
  550. c->cyclic = 1;
  551. while (buf < buf_len) {
  552. if (dir == DMA_MEM_TO_DEV) {
  553. src = dma_addr;
  554. dst = c->dev_addr;
  555. } else if (dir == DMA_DEV_TO_MEM) {
  556. src = c->dev_addr;
  557. dst = dma_addr;
  558. }
  559. zx_dma_fill_desc(ds, dst, src, period_len, num++,
  560. c->ccfg | ZX_IRQ_ENABLE_ALL);
  561. dma_addr += period_len;
  562. buf += period_len;
  563. }
  564. ds->desc_hw[num - 1].lli = ds->desc_hw_lli;
  565. ds->size = buf_len;
  566. return vchan_tx_prep(&c->vc, &ds->vd, flags);
  567. }
  568. static int zx_dma_config(struct dma_chan *chan,
  569. struct dma_slave_config *cfg)
  570. {
  571. struct zx_dma_chan *c = to_zx_chan(chan);
  572. if (!cfg)
  573. return -EINVAL;
  574. memcpy(&c->slave_cfg, cfg, sizeof(*cfg));
  575. return 0;
  576. }
  577. static int zx_dma_terminate_all(struct dma_chan *chan)
  578. {
  579. struct zx_dma_chan *c = to_zx_chan(chan);
  580. struct zx_dma_dev *d = to_zx_dma(chan->device);
  581. struct zx_dma_phy *p = c->phy;
  582. unsigned long flags;
  583. LIST_HEAD(head);
  584. dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
  585. /* Prevent this channel being scheduled */
  586. spin_lock(&d->lock);
  587. list_del_init(&c->node);
  588. spin_unlock(&d->lock);
  589. /* Clear the tx descriptor lists */
  590. spin_lock_irqsave(&c->vc.lock, flags);
  591. vchan_get_all_descriptors(&c->vc, &head);
  592. if (p) {
  593. /* vchan is assigned to a pchan - stop the channel */
  594. zx_dma_terminate_chan(p, d);
  595. c->phy = NULL;
  596. p->vchan = NULL;
  597. p->ds_run = NULL;
  598. p->ds_done = NULL;
  599. }
  600. spin_unlock_irqrestore(&c->vc.lock, flags);
  601. vchan_dma_desc_free_list(&c->vc, &head);
  602. return 0;
  603. }
  604. static int zx_dma_transfer_pause(struct dma_chan *chan)
  605. {
  606. struct zx_dma_chan *c = to_zx_chan(chan);
  607. u32 val = 0;
  608. val = readl_relaxed(c->phy->base + REG_ZX_CTRL);
  609. val &= ~ZX_CH_ENABLE;
  610. writel_relaxed(val, c->phy->base + REG_ZX_CTRL);
  611. return 0;
  612. }
  613. static int zx_dma_transfer_resume(struct dma_chan *chan)
  614. {
  615. struct zx_dma_chan *c = to_zx_chan(chan);
  616. u32 val = 0;
  617. val = readl_relaxed(c->phy->base + REG_ZX_CTRL);
  618. val |= ZX_CH_ENABLE;
  619. writel_relaxed(val, c->phy->base + REG_ZX_CTRL);
  620. return 0;
  621. }
  622. static void zx_dma_free_desc(struct virt_dma_desc *vd)
  623. {
  624. struct zx_dma_desc_sw *ds =
  625. container_of(vd, struct zx_dma_desc_sw, vd);
  626. struct zx_dma_dev *d = to_zx_dma(vd->tx.chan->device);
  627. dma_pool_free(d->pool, ds->desc_hw, ds->desc_hw_lli);
  628. kfree(ds);
  629. }
  630. static const struct of_device_id zx6702_dma_dt_ids[] = {
  631. { .compatible = "zte,zx296702-dma", },
  632. {}
  633. };
  634. MODULE_DEVICE_TABLE(of, zx6702_dma_dt_ids);
  635. static struct dma_chan *zx_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
  636. struct of_dma *ofdma)
  637. {
  638. struct zx_dma_dev *d = ofdma->of_dma_data;
  639. unsigned int request = dma_spec->args[0];
  640. struct dma_chan *chan;
  641. struct zx_dma_chan *c;
  642. if (request >= d->dma_requests)
  643. return NULL;
  644. chan = dma_get_any_slave_channel(&d->slave);
  645. if (!chan) {
  646. dev_err(d->slave.dev, "get channel fail in %s.\n", __func__);
  647. return NULL;
  648. }
  649. c = to_zx_chan(chan);
  650. c->id = request;
  651. dev_info(d->slave.dev, "zx_dma: pchan %u: alloc vchan %p\n",
  652. c->id, &c->vc);
  653. return chan;
  654. }
  655. static int zx_dma_probe(struct platform_device *op)
  656. {
  657. struct zx_dma_dev *d;
  658. struct resource *iores;
  659. int i, ret = 0;
  660. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  661. if (!iores)
  662. return -EINVAL;
  663. d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
  664. if (!d)
  665. return -ENOMEM;
  666. d->base = devm_ioremap_resource(&op->dev, iores);
  667. if (IS_ERR(d->base))
  668. return PTR_ERR(d->base);
  669. of_property_read_u32((&op->dev)->of_node,
  670. "dma-channels", &d->dma_channels);
  671. of_property_read_u32((&op->dev)->of_node,
  672. "dma-requests", &d->dma_requests);
  673. if (!d->dma_requests || !d->dma_channels)
  674. return -EINVAL;
  675. d->clk = devm_clk_get(&op->dev, NULL);
  676. if (IS_ERR(d->clk)) {
  677. dev_err(&op->dev, "no dma clk\n");
  678. return PTR_ERR(d->clk);
  679. }
  680. d->irq = platform_get_irq(op, 0);
  681. ret = devm_request_irq(&op->dev, d->irq, zx_dma_int_handler,
  682. 0, DRIVER_NAME, d);
  683. if (ret)
  684. return ret;
  685. /* A DMA memory pool for LLIs, align on 32-byte boundary */
  686. d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
  687. LLI_BLOCK_SIZE, 32, 0);
  688. if (!d->pool)
  689. return -ENOMEM;
  690. /* init phy channel */
  691. d->phy = devm_kzalloc(&op->dev,
  692. d->dma_channels * sizeof(struct zx_dma_phy), GFP_KERNEL);
  693. if (!d->phy)
  694. return -ENOMEM;
  695. for (i = 0; i < d->dma_channels; i++) {
  696. struct zx_dma_phy *p = &d->phy[i];
  697. p->idx = i;
  698. p->base = d->base + i * 0x40;
  699. }
  700. INIT_LIST_HEAD(&d->slave.channels);
  701. dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
  702. dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
  703. dma_cap_set(DMA_CYCLIC, d->slave.cap_mask);
  704. dma_cap_set(DMA_PRIVATE, d->slave.cap_mask);
  705. d->slave.dev = &op->dev;
  706. d->slave.device_free_chan_resources = zx_dma_free_chan_resources;
  707. d->slave.device_tx_status = zx_dma_tx_status;
  708. d->slave.device_prep_dma_memcpy = zx_dma_prep_memcpy;
  709. d->slave.device_prep_slave_sg = zx_dma_prep_slave_sg;
  710. d->slave.device_prep_dma_cyclic = zx_dma_prep_dma_cyclic;
  711. d->slave.device_issue_pending = zx_dma_issue_pending;
  712. d->slave.device_config = zx_dma_config;
  713. d->slave.device_terminate_all = zx_dma_terminate_all;
  714. d->slave.device_pause = zx_dma_transfer_pause;
  715. d->slave.device_resume = zx_dma_transfer_resume;
  716. d->slave.copy_align = DMA_ALIGN;
  717. d->slave.src_addr_widths = ZX_DMA_BUSWIDTHS;
  718. d->slave.dst_addr_widths = ZX_DMA_BUSWIDTHS;
  719. d->slave.directions = BIT(DMA_MEM_TO_MEM) | BIT(DMA_MEM_TO_DEV)
  720. | BIT(DMA_DEV_TO_MEM);
  721. d->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  722. /* init virtual channel */
  723. d->chans = devm_kzalloc(&op->dev,
  724. d->dma_requests * sizeof(struct zx_dma_chan), GFP_KERNEL);
  725. if (!d->chans)
  726. return -ENOMEM;
  727. for (i = 0; i < d->dma_requests; i++) {
  728. struct zx_dma_chan *c = &d->chans[i];
  729. c->status = DMA_IN_PROGRESS;
  730. INIT_LIST_HEAD(&c->node);
  731. c->vc.desc_free = zx_dma_free_desc;
  732. vchan_init(&c->vc, &d->slave);
  733. }
  734. /* Enable clock before accessing registers */
  735. ret = clk_prepare_enable(d->clk);
  736. if (ret < 0) {
  737. dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
  738. goto zx_dma_out;
  739. }
  740. zx_dma_init_state(d);
  741. spin_lock_init(&d->lock);
  742. INIT_LIST_HEAD(&d->chan_pending);
  743. platform_set_drvdata(op, d);
  744. ret = dma_async_device_register(&d->slave);
  745. if (ret)
  746. goto clk_dis;
  747. ret = of_dma_controller_register((&op->dev)->of_node,
  748. zx_of_dma_simple_xlate, d);
  749. if (ret)
  750. goto of_dma_register_fail;
  751. dev_info(&op->dev, "initialized\n");
  752. return 0;
  753. of_dma_register_fail:
  754. dma_async_device_unregister(&d->slave);
  755. clk_dis:
  756. clk_disable_unprepare(d->clk);
  757. zx_dma_out:
  758. return ret;
  759. }
  760. static int zx_dma_remove(struct platform_device *op)
  761. {
  762. struct zx_dma_chan *c, *cn;
  763. struct zx_dma_dev *d = platform_get_drvdata(op);
  764. /* explictly free the irq */
  765. devm_free_irq(&op->dev, d->irq, d);
  766. dma_async_device_unregister(&d->slave);
  767. of_dma_controller_free((&op->dev)->of_node);
  768. list_for_each_entry_safe(c, cn, &d->slave.channels,
  769. vc.chan.device_node) {
  770. list_del(&c->vc.chan.device_node);
  771. }
  772. clk_disable_unprepare(d->clk);
  773. dmam_pool_destroy(d->pool);
  774. return 0;
  775. }
  776. #ifdef CONFIG_PM_SLEEP
  777. static int zx_dma_suspend_dev(struct device *dev)
  778. {
  779. struct zx_dma_dev *d = dev_get_drvdata(dev);
  780. u32 stat = 0;
  781. stat = zx_dma_get_chan_stat(d);
  782. if (stat) {
  783. dev_warn(d->slave.dev,
  784. "chan %d is running fail to suspend\n", stat);
  785. return -1;
  786. }
  787. clk_disable_unprepare(d->clk);
  788. return 0;
  789. }
  790. static int zx_dma_resume_dev(struct device *dev)
  791. {
  792. struct zx_dma_dev *d = dev_get_drvdata(dev);
  793. int ret = 0;
  794. ret = clk_prepare_enable(d->clk);
  795. if (ret < 0) {
  796. dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
  797. return ret;
  798. }
  799. zx_dma_init_state(d);
  800. return 0;
  801. }
  802. #endif
  803. static SIMPLE_DEV_PM_OPS(zx_dma_pmops, zx_dma_suspend_dev, zx_dma_resume_dev);
  804. static struct platform_driver zx_pdma_driver = {
  805. .driver = {
  806. .name = DRIVER_NAME,
  807. .pm = &zx_dma_pmops,
  808. .of_match_table = zx6702_dma_dt_ids,
  809. },
  810. .probe = zx_dma_probe,
  811. .remove = zx_dma_remove,
  812. };
  813. module_platform_driver(zx_pdma_driver);
  814. MODULE_DESCRIPTION("ZTE ZX296702 DMA Driver");
  815. MODULE_AUTHOR("Jun Nie jun.nie@linaro.org");
  816. MODULE_LICENSE("GPL v2");