sa11x0-dma.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*
  2. * SA11x0 DMAengine support
  3. *
  4. * Copyright (C) 2012 Russell King
  5. * Derived in part from arch/arm/mach-sa1100/dma.c,
  6. * Copyright (C) 2000, 2001 by Nicolas Pitre
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/device.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/sa11x0-dma.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #define NR_PHY_CHAN 6
  24. #define DMA_ALIGN 3
  25. #define DMA_MAX_SIZE 0x1fff
  26. #define DMA_CHUNK_SIZE 0x1000
  27. #define DMA_DDAR 0x00
  28. #define DMA_DCSR_S 0x04
  29. #define DMA_DCSR_C 0x08
  30. #define DMA_DCSR_R 0x0c
  31. #define DMA_DBSA 0x10
  32. #define DMA_DBTA 0x14
  33. #define DMA_DBSB 0x18
  34. #define DMA_DBTB 0x1c
  35. #define DMA_SIZE 0x20
  36. #define DCSR_RUN (1 << 0)
  37. #define DCSR_IE (1 << 1)
  38. #define DCSR_ERROR (1 << 2)
  39. #define DCSR_DONEA (1 << 3)
  40. #define DCSR_STRTA (1 << 4)
  41. #define DCSR_DONEB (1 << 5)
  42. #define DCSR_STRTB (1 << 6)
  43. #define DCSR_BIU (1 << 7)
  44. #define DDAR_RW (1 << 0) /* 0 = W, 1 = R */
  45. #define DDAR_E (1 << 1) /* 0 = LE, 1 = BE */
  46. #define DDAR_BS (1 << 2) /* 0 = BS4, 1 = BS8 */
  47. #define DDAR_DW (1 << 3) /* 0 = 8b, 1 = 16b */
  48. #define DDAR_Ser0UDCTr (0x0 << 4)
  49. #define DDAR_Ser0UDCRc (0x1 << 4)
  50. #define DDAR_Ser1SDLCTr (0x2 << 4)
  51. #define DDAR_Ser1SDLCRc (0x3 << 4)
  52. #define DDAR_Ser1UARTTr (0x4 << 4)
  53. #define DDAR_Ser1UARTRc (0x5 << 4)
  54. #define DDAR_Ser2ICPTr (0x6 << 4)
  55. #define DDAR_Ser2ICPRc (0x7 << 4)
  56. #define DDAR_Ser3UARTTr (0x8 << 4)
  57. #define DDAR_Ser3UARTRc (0x9 << 4)
  58. #define DDAR_Ser4MCP0Tr (0xa << 4)
  59. #define DDAR_Ser4MCP0Rc (0xb << 4)
  60. #define DDAR_Ser4MCP1Tr (0xc << 4)
  61. #define DDAR_Ser4MCP1Rc (0xd << 4)
  62. #define DDAR_Ser4SSPTr (0xe << 4)
  63. #define DDAR_Ser4SSPRc (0xf << 4)
  64. struct sa11x0_dma_sg {
  65. u32 addr;
  66. u32 len;
  67. };
  68. struct sa11x0_dma_desc {
  69. struct dma_async_tx_descriptor tx;
  70. u32 ddar;
  71. size_t size;
  72. /* maybe protected by c->lock */
  73. struct list_head node;
  74. unsigned sglen;
  75. struct sa11x0_dma_sg sg[0];
  76. };
  77. struct sa11x0_dma_phy;
  78. struct sa11x0_dma_chan {
  79. struct dma_chan chan;
  80. spinlock_t lock;
  81. dma_cookie_t lc;
  82. /* protected by c->lock */
  83. struct sa11x0_dma_phy *phy;
  84. enum dma_status status;
  85. struct list_head desc_submitted;
  86. struct list_head desc_issued;
  87. /* protected by d->lock */
  88. struct list_head node;
  89. u32 ddar;
  90. const char *name;
  91. };
  92. struct sa11x0_dma_phy {
  93. void __iomem *base;
  94. struct sa11x0_dma_dev *dev;
  95. unsigned num;
  96. struct sa11x0_dma_chan *vchan;
  97. /* Protected by c->lock */
  98. unsigned sg_load;
  99. struct sa11x0_dma_desc *txd_load;
  100. unsigned sg_done;
  101. struct sa11x0_dma_desc *txd_done;
  102. #ifdef CONFIG_PM_SLEEP
  103. u32 dbs[2];
  104. u32 dbt[2];
  105. u32 dcsr;
  106. #endif
  107. };
  108. struct sa11x0_dma_dev {
  109. struct dma_device slave;
  110. void __iomem *base;
  111. spinlock_t lock;
  112. struct tasklet_struct task;
  113. struct list_head chan_pending;
  114. struct list_head desc_complete;
  115. struct sa11x0_dma_phy phy[NR_PHY_CHAN];
  116. };
  117. static struct sa11x0_dma_chan *to_sa11x0_dma_chan(struct dma_chan *chan)
  118. {
  119. return container_of(chan, struct sa11x0_dma_chan, chan);
  120. }
  121. static struct sa11x0_dma_dev *to_sa11x0_dma(struct dma_device *dmadev)
  122. {
  123. return container_of(dmadev, struct sa11x0_dma_dev, slave);
  124. }
  125. static struct sa11x0_dma_desc *to_sa11x0_dma_tx(struct dma_async_tx_descriptor *tx)
  126. {
  127. return container_of(tx, struct sa11x0_dma_desc, tx);
  128. }
  129. static struct sa11x0_dma_desc *sa11x0_dma_next_desc(struct sa11x0_dma_chan *c)
  130. {
  131. if (list_empty(&c->desc_issued))
  132. return NULL;
  133. return list_first_entry(&c->desc_issued, struct sa11x0_dma_desc, node);
  134. }
  135. static void sa11x0_dma_start_desc(struct sa11x0_dma_phy *p, struct sa11x0_dma_desc *txd)
  136. {
  137. list_del(&txd->node);
  138. p->txd_load = txd;
  139. p->sg_load = 0;
  140. dev_vdbg(p->dev->slave.dev, "pchan %u: txd %p[%x]: starting: DDAR:%x\n",
  141. p->num, txd, txd->tx.cookie, txd->ddar);
  142. }
  143. static void noinline sa11x0_dma_start_sg(struct sa11x0_dma_phy *p,
  144. struct sa11x0_dma_chan *c)
  145. {
  146. struct sa11x0_dma_desc *txd = p->txd_load;
  147. struct sa11x0_dma_sg *sg;
  148. void __iomem *base = p->base;
  149. unsigned dbsx, dbtx;
  150. u32 dcsr;
  151. if (!txd)
  152. return;
  153. dcsr = readl_relaxed(base + DMA_DCSR_R);
  154. /* Don't try to load the next transfer if both buffers are started */
  155. if ((dcsr & (DCSR_STRTA | DCSR_STRTB)) == (DCSR_STRTA | DCSR_STRTB))
  156. return;
  157. if (p->sg_load == txd->sglen) {
  158. struct sa11x0_dma_desc *txn = sa11x0_dma_next_desc(c);
  159. /*
  160. * We have reached the end of the current descriptor.
  161. * Peek at the next descriptor, and if compatible with
  162. * the current, start processing it.
  163. */
  164. if (txn && txn->ddar == txd->ddar) {
  165. txd = txn;
  166. sa11x0_dma_start_desc(p, txn);
  167. } else {
  168. p->txd_load = NULL;
  169. return;
  170. }
  171. }
  172. sg = &txd->sg[p->sg_load++];
  173. /* Select buffer to load according to channel status */
  174. if (((dcsr & (DCSR_BIU | DCSR_STRTB)) == (DCSR_BIU | DCSR_STRTB)) ||
  175. ((dcsr & (DCSR_BIU | DCSR_STRTA)) == 0)) {
  176. dbsx = DMA_DBSA;
  177. dbtx = DMA_DBTA;
  178. dcsr = DCSR_STRTA | DCSR_IE | DCSR_RUN;
  179. } else {
  180. dbsx = DMA_DBSB;
  181. dbtx = DMA_DBTB;
  182. dcsr = DCSR_STRTB | DCSR_IE | DCSR_RUN;
  183. }
  184. writel_relaxed(sg->addr, base + dbsx);
  185. writel_relaxed(sg->len, base + dbtx);
  186. writel(dcsr, base + DMA_DCSR_S);
  187. dev_dbg(p->dev->slave.dev, "pchan %u: load: DCSR:%02x DBS%c:%08x DBT%c:%08x\n",
  188. p->num, dcsr,
  189. 'A' + (dbsx == DMA_DBSB), sg->addr,
  190. 'A' + (dbtx == DMA_DBTB), sg->len);
  191. }
  192. static void noinline sa11x0_dma_complete(struct sa11x0_dma_phy *p,
  193. struct sa11x0_dma_chan *c)
  194. {
  195. struct sa11x0_dma_desc *txd = p->txd_done;
  196. if (++p->sg_done == txd->sglen) {
  197. struct sa11x0_dma_dev *d = p->dev;
  198. dev_vdbg(d->slave.dev, "pchan %u: txd %p[%x]: completed\n",
  199. p->num, p->txd_done, p->txd_done->tx.cookie);
  200. c->lc = txd->tx.cookie;
  201. spin_lock(&d->lock);
  202. list_add_tail(&txd->node, &d->desc_complete);
  203. spin_unlock(&d->lock);
  204. p->sg_done = 0;
  205. p->txd_done = p->txd_load;
  206. tasklet_schedule(&d->task);
  207. }
  208. sa11x0_dma_start_sg(p, c);
  209. }
  210. static irqreturn_t sa11x0_dma_irq(int irq, void *dev_id)
  211. {
  212. struct sa11x0_dma_phy *p = dev_id;
  213. struct sa11x0_dma_dev *d = p->dev;
  214. struct sa11x0_dma_chan *c;
  215. u32 dcsr;
  216. dcsr = readl_relaxed(p->base + DMA_DCSR_R);
  217. if (!(dcsr & (DCSR_ERROR | DCSR_DONEA | DCSR_DONEB)))
  218. return IRQ_NONE;
  219. /* Clear reported status bits */
  220. writel_relaxed(dcsr & (DCSR_ERROR | DCSR_DONEA | DCSR_DONEB),
  221. p->base + DMA_DCSR_C);
  222. dev_dbg(d->slave.dev, "pchan %u: irq: DCSR:%02x\n", p->num, dcsr);
  223. if (dcsr & DCSR_ERROR) {
  224. dev_err(d->slave.dev, "pchan %u: error. DCSR:%02x DDAR:%08x DBSA:%08x DBTA:%08x DBSB:%08x DBTB:%08x\n",
  225. p->num, dcsr,
  226. readl_relaxed(p->base + DMA_DDAR),
  227. readl_relaxed(p->base + DMA_DBSA),
  228. readl_relaxed(p->base + DMA_DBTA),
  229. readl_relaxed(p->base + DMA_DBSB),
  230. readl_relaxed(p->base + DMA_DBTB));
  231. }
  232. c = p->vchan;
  233. if (c) {
  234. unsigned long flags;
  235. spin_lock_irqsave(&c->lock, flags);
  236. /*
  237. * Now that we're holding the lock, check that the vchan
  238. * really is associated with this pchan before touching the
  239. * hardware. This should always succeed, because we won't
  240. * change p->vchan or c->phy while the channel is actively
  241. * transferring.
  242. */
  243. if (c->phy == p) {
  244. if (dcsr & DCSR_DONEA)
  245. sa11x0_dma_complete(p, c);
  246. if (dcsr & DCSR_DONEB)
  247. sa11x0_dma_complete(p, c);
  248. }
  249. spin_unlock_irqrestore(&c->lock, flags);
  250. }
  251. return IRQ_HANDLED;
  252. }
  253. static void sa11x0_dma_start_txd(struct sa11x0_dma_chan *c)
  254. {
  255. struct sa11x0_dma_desc *txd = sa11x0_dma_next_desc(c);
  256. /* If the issued list is empty, we have no further txds to process */
  257. if (txd) {
  258. struct sa11x0_dma_phy *p = c->phy;
  259. sa11x0_dma_start_desc(p, txd);
  260. p->txd_done = txd;
  261. p->sg_done = 0;
  262. /* The channel should not have any transfers started */
  263. WARN_ON(readl_relaxed(p->base + DMA_DCSR_R) &
  264. (DCSR_STRTA | DCSR_STRTB));
  265. /* Clear the run and start bits before changing DDAR */
  266. writel_relaxed(DCSR_RUN | DCSR_STRTA | DCSR_STRTB,
  267. p->base + DMA_DCSR_C);
  268. writel_relaxed(txd->ddar, p->base + DMA_DDAR);
  269. /* Try to start both buffers */
  270. sa11x0_dma_start_sg(p, c);
  271. sa11x0_dma_start_sg(p, c);
  272. }
  273. }
  274. static void sa11x0_dma_tasklet(unsigned long arg)
  275. {
  276. struct sa11x0_dma_dev *d = (struct sa11x0_dma_dev *)arg;
  277. struct sa11x0_dma_phy *p;
  278. struct sa11x0_dma_chan *c;
  279. struct sa11x0_dma_desc *txd, *txn;
  280. LIST_HEAD(head);
  281. unsigned pch, pch_alloc = 0;
  282. dev_dbg(d->slave.dev, "tasklet enter\n");
  283. /* Get the completed tx descriptors */
  284. spin_lock_irq(&d->lock);
  285. list_splice_init(&d->desc_complete, &head);
  286. spin_unlock_irq(&d->lock);
  287. list_for_each_entry(txd, &head, node) {
  288. c = to_sa11x0_dma_chan(txd->tx.chan);
  289. dev_dbg(d->slave.dev, "vchan %p: txd %p[%x] completed\n",
  290. c, txd, txd->tx.cookie);
  291. spin_lock_irq(&c->lock);
  292. p = c->phy;
  293. if (p) {
  294. if (!p->txd_done)
  295. sa11x0_dma_start_txd(c);
  296. if (!p->txd_done) {
  297. /* No current txd associated with this channel */
  298. dev_dbg(d->slave.dev, "pchan %u: free\n", p->num);
  299. /* Mark this channel free */
  300. c->phy = NULL;
  301. p->vchan = NULL;
  302. }
  303. }
  304. spin_unlock_irq(&c->lock);
  305. }
  306. spin_lock_irq(&d->lock);
  307. for (pch = 0; pch < NR_PHY_CHAN; pch++) {
  308. p = &d->phy[pch];
  309. if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
  310. c = list_first_entry(&d->chan_pending,
  311. struct sa11x0_dma_chan, node);
  312. list_del_init(&c->node);
  313. pch_alloc |= 1 << pch;
  314. /* Mark this channel allocated */
  315. p->vchan = c;
  316. dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, c);
  317. }
  318. }
  319. spin_unlock_irq(&d->lock);
  320. for (pch = 0; pch < NR_PHY_CHAN; pch++) {
  321. if (pch_alloc & (1 << pch)) {
  322. p = &d->phy[pch];
  323. c = p->vchan;
  324. spin_lock_irq(&c->lock);
  325. c->phy = p;
  326. sa11x0_dma_start_txd(c);
  327. spin_unlock_irq(&c->lock);
  328. }
  329. }
  330. /* Now free the completed tx descriptor, and call their callbacks */
  331. list_for_each_entry_safe(txd, txn, &head, node) {
  332. dma_async_tx_callback callback = txd->tx.callback;
  333. void *callback_param = txd->tx.callback_param;
  334. dev_dbg(d->slave.dev, "txd %p[%x]: callback and free\n",
  335. txd, txd->tx.cookie);
  336. kfree(txd);
  337. if (callback)
  338. callback(callback_param);
  339. }
  340. dev_dbg(d->slave.dev, "tasklet exit\n");
  341. }
  342. static void sa11x0_dma_desc_free(struct sa11x0_dma_dev *d, struct list_head *head)
  343. {
  344. struct sa11x0_dma_desc *txd, *txn;
  345. list_for_each_entry_safe(txd, txn, head, node) {
  346. dev_dbg(d->slave.dev, "txd %p: freeing\n", txd);
  347. kfree(txd);
  348. }
  349. }
  350. static int sa11x0_dma_alloc_chan_resources(struct dma_chan *chan)
  351. {
  352. return 0;
  353. }
  354. static void sa11x0_dma_free_chan_resources(struct dma_chan *chan)
  355. {
  356. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  357. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  358. unsigned long flags;
  359. LIST_HEAD(head);
  360. spin_lock_irqsave(&c->lock, flags);
  361. spin_lock(&d->lock);
  362. list_del_init(&c->node);
  363. spin_unlock(&d->lock);
  364. list_splice_tail_init(&c->desc_submitted, &head);
  365. list_splice_tail_init(&c->desc_issued, &head);
  366. spin_unlock_irqrestore(&c->lock, flags);
  367. sa11x0_dma_desc_free(d, &head);
  368. }
  369. static dma_addr_t sa11x0_dma_pos(struct sa11x0_dma_phy *p)
  370. {
  371. unsigned reg;
  372. u32 dcsr;
  373. dcsr = readl_relaxed(p->base + DMA_DCSR_R);
  374. if ((dcsr & (DCSR_BIU | DCSR_STRTA)) == DCSR_STRTA ||
  375. (dcsr & (DCSR_BIU | DCSR_STRTB)) == DCSR_BIU)
  376. reg = DMA_DBSA;
  377. else
  378. reg = DMA_DBSB;
  379. return readl_relaxed(p->base + reg);
  380. }
  381. static enum dma_status sa11x0_dma_tx_status(struct dma_chan *chan,
  382. dma_cookie_t cookie, struct dma_tx_state *state)
  383. {
  384. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  385. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  386. struct sa11x0_dma_phy *p;
  387. struct sa11x0_dma_desc *txd;
  388. dma_cookie_t last_used, last_complete;
  389. unsigned long flags;
  390. enum dma_status ret;
  391. size_t bytes = 0;
  392. last_used = c->chan.cookie;
  393. last_complete = c->lc;
  394. ret = dma_async_is_complete(cookie, last_complete, last_used);
  395. if (ret == DMA_SUCCESS) {
  396. dma_set_tx_state(state, last_complete, last_used, 0);
  397. return ret;
  398. }
  399. spin_lock_irqsave(&c->lock, flags);
  400. p = c->phy;
  401. ret = c->status;
  402. if (p) {
  403. dma_addr_t addr = sa11x0_dma_pos(p);
  404. dev_vdbg(d->slave.dev, "tx_status: addr:%x\n", addr);
  405. txd = p->txd_done;
  406. if (txd) {
  407. unsigned i;
  408. for (i = 0; i < txd->sglen; i++) {
  409. dev_vdbg(d->slave.dev, "tx_status: [%u] %x+%x\n",
  410. i, txd->sg[i].addr, txd->sg[i].len);
  411. if (addr >= txd->sg[i].addr &&
  412. addr < txd->sg[i].addr + txd->sg[i].len) {
  413. unsigned len;
  414. len = txd->sg[i].len -
  415. (addr - txd->sg[i].addr);
  416. dev_vdbg(d->slave.dev, "tx_status: [%u] +%x\n",
  417. i, len);
  418. bytes += len;
  419. i++;
  420. break;
  421. }
  422. }
  423. for (; i < txd->sglen; i++) {
  424. dev_vdbg(d->slave.dev, "tx_status: [%u] %x+%x ++\n",
  425. i, txd->sg[i].addr, txd->sg[i].len);
  426. bytes += txd->sg[i].len;
  427. }
  428. }
  429. if (txd != p->txd_load && p->txd_load)
  430. bytes += p->txd_load->size;
  431. }
  432. list_for_each_entry(txd, &c->desc_issued, node) {
  433. bytes += txd->size;
  434. }
  435. spin_unlock_irqrestore(&c->lock, flags);
  436. dma_set_tx_state(state, last_complete, last_used, bytes);
  437. dev_vdbg(d->slave.dev, "tx_status: bytes 0x%zx\n", bytes);
  438. return ret;
  439. }
  440. /*
  441. * Move pending txds to the issued list, and re-init pending list.
  442. * If not already pending, add this channel to the list of pending
  443. * channels and trigger the tasklet to run.
  444. */
  445. static void sa11x0_dma_issue_pending(struct dma_chan *chan)
  446. {
  447. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  448. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  449. unsigned long flags;
  450. spin_lock_irqsave(&c->lock, flags);
  451. list_splice_tail_init(&c->desc_submitted, &c->desc_issued);
  452. if (!list_empty(&c->desc_issued)) {
  453. spin_lock(&d->lock);
  454. if (!c->phy && list_empty(&c->node)) {
  455. list_add_tail(&c->node, &d->chan_pending);
  456. tasklet_schedule(&d->task);
  457. dev_dbg(d->slave.dev, "vchan %p: issued\n", c);
  458. }
  459. spin_unlock(&d->lock);
  460. } else
  461. dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", c);
  462. spin_unlock_irqrestore(&c->lock, flags);
  463. }
  464. static dma_cookie_t sa11x0_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  465. {
  466. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(tx->chan);
  467. struct sa11x0_dma_desc *txd = to_sa11x0_dma_tx(tx);
  468. unsigned long flags;
  469. spin_lock_irqsave(&c->lock, flags);
  470. c->chan.cookie += 1;
  471. if (c->chan.cookie < 0)
  472. c->chan.cookie = 1;
  473. txd->tx.cookie = c->chan.cookie;
  474. list_add_tail(&txd->node, &c->desc_submitted);
  475. spin_unlock_irqrestore(&c->lock, flags);
  476. dev_dbg(tx->chan->device->dev, "vchan %p: txd %p[%x]: submitted\n",
  477. c, txd, txd->tx.cookie);
  478. return txd->tx.cookie;
  479. }
  480. static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg(
  481. struct dma_chan *chan, struct scatterlist *sg, unsigned int sglen,
  482. enum dma_transfer_direction dir, unsigned long flags, void *context)
  483. {
  484. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  485. struct sa11x0_dma_desc *txd;
  486. struct scatterlist *sgent;
  487. unsigned i, j = sglen;
  488. size_t size = 0;
  489. /* SA11x0 channels can only operate in their native direction */
  490. if (dir != (c->ddar & DDAR_RW ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV)) {
  491. dev_err(chan->device->dev, "vchan %p: bad DMA direction: DDAR:%08x dir:%u\n",
  492. c, c->ddar, dir);
  493. return NULL;
  494. }
  495. /* Do not allow zero-sized txds */
  496. if (sglen == 0)
  497. return NULL;
  498. for_each_sg(sg, sgent, sglen, i) {
  499. dma_addr_t addr = sg_dma_address(sgent);
  500. unsigned int len = sg_dma_len(sgent);
  501. if (len > DMA_MAX_SIZE)
  502. j += DIV_ROUND_UP(len, DMA_MAX_SIZE & ~DMA_ALIGN) - 1;
  503. if (addr & DMA_ALIGN) {
  504. dev_dbg(chan->device->dev, "vchan %p: bad buffer alignment: %08x\n",
  505. c, addr);
  506. return NULL;
  507. }
  508. }
  509. txd = kzalloc(sizeof(*txd) + j * sizeof(txd->sg[0]), GFP_ATOMIC);
  510. if (!txd) {
  511. dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", c);
  512. return NULL;
  513. }
  514. j = 0;
  515. for_each_sg(sg, sgent, sglen, i) {
  516. dma_addr_t addr = sg_dma_address(sgent);
  517. unsigned len = sg_dma_len(sgent);
  518. size += len;
  519. do {
  520. unsigned tlen = len;
  521. /*
  522. * Check whether the transfer will fit. If not, try
  523. * to split the transfer up such that we end up with
  524. * equal chunks - but make sure that we preserve the
  525. * alignment. This avoids small segments.
  526. */
  527. if (tlen > DMA_MAX_SIZE) {
  528. unsigned mult = DIV_ROUND_UP(tlen,
  529. DMA_MAX_SIZE & ~DMA_ALIGN);
  530. tlen = (tlen / mult) & ~DMA_ALIGN;
  531. }
  532. txd->sg[j].addr = addr;
  533. txd->sg[j].len = tlen;
  534. addr += tlen;
  535. len -= tlen;
  536. j++;
  537. } while (len);
  538. }
  539. dma_async_tx_descriptor_init(&txd->tx, &c->chan);
  540. txd->tx.flags = flags;
  541. txd->tx.tx_submit = sa11x0_dma_tx_submit;
  542. txd->ddar = c->ddar;
  543. txd->size = size;
  544. txd->sglen = j;
  545. dev_dbg(chan->device->dev, "vchan %p: txd %p: size %u nr %u\n",
  546. c, txd, txd->size, txd->sglen);
  547. return &txd->tx;
  548. }
  549. static int sa11x0_dma_slave_config(struct sa11x0_dma_chan *c, struct dma_slave_config *cfg)
  550. {
  551. u32 ddar = c->ddar & ((0xf << 4) | DDAR_RW);
  552. dma_addr_t addr;
  553. enum dma_slave_buswidth width;
  554. u32 maxburst;
  555. if (ddar & DDAR_RW) {
  556. addr = cfg->src_addr;
  557. width = cfg->src_addr_width;
  558. maxburst = cfg->src_maxburst;
  559. } else {
  560. addr = cfg->dst_addr;
  561. width = cfg->dst_addr_width;
  562. maxburst = cfg->dst_maxburst;
  563. }
  564. if ((width != DMA_SLAVE_BUSWIDTH_1_BYTE &&
  565. width != DMA_SLAVE_BUSWIDTH_2_BYTES) ||
  566. (maxburst != 4 && maxburst != 8))
  567. return -EINVAL;
  568. if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  569. ddar |= DDAR_DW;
  570. if (maxburst == 8)
  571. ddar |= DDAR_BS;
  572. dev_dbg(c->chan.device->dev, "vchan %p: dma_slave_config addr %x width %u burst %u\n",
  573. c, addr, width, maxburst);
  574. c->ddar = ddar | (addr & 0xf0000000) | (addr & 0x003ffffc) << 6;
  575. return 0;
  576. }
  577. static int sa11x0_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  578. unsigned long arg)
  579. {
  580. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  581. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  582. struct sa11x0_dma_phy *p;
  583. LIST_HEAD(head);
  584. unsigned long flags;
  585. int ret;
  586. switch (cmd) {
  587. case DMA_SLAVE_CONFIG:
  588. return sa11x0_dma_slave_config(c, (struct dma_slave_config *)arg);
  589. case DMA_TERMINATE_ALL:
  590. dev_dbg(d->slave.dev, "vchan %p: terminate all\n", c);
  591. /* Clear the tx descriptor lists */
  592. spin_lock_irqsave(&c->lock, flags);
  593. list_splice_tail_init(&c->desc_submitted, &head);
  594. list_splice_tail_init(&c->desc_issued, &head);
  595. p = c->phy;
  596. if (p) {
  597. struct sa11x0_dma_desc *txd, *txn;
  598. dev_dbg(d->slave.dev, "pchan %u: terminating\n", p->num);
  599. /* vchan is assigned to a pchan - stop the channel */
  600. writel(DCSR_RUN | DCSR_IE |
  601. DCSR_STRTA | DCSR_DONEA |
  602. DCSR_STRTB | DCSR_DONEB,
  603. p->base + DMA_DCSR_C);
  604. list_for_each_entry_safe(txd, txn, &d->desc_complete, node)
  605. if (txd->tx.chan == &c->chan)
  606. list_move(&txd->node, &head);
  607. if (p->txd_load) {
  608. if (p->txd_load != p->txd_done)
  609. list_add_tail(&p->txd_load->node, &head);
  610. p->txd_load = NULL;
  611. }
  612. if (p->txd_done) {
  613. list_add_tail(&p->txd_done->node, &head);
  614. p->txd_done = NULL;
  615. }
  616. c->phy = NULL;
  617. spin_lock(&d->lock);
  618. p->vchan = NULL;
  619. spin_unlock(&d->lock);
  620. tasklet_schedule(&d->task);
  621. }
  622. spin_unlock_irqrestore(&c->lock, flags);
  623. sa11x0_dma_desc_free(d, &head);
  624. ret = 0;
  625. break;
  626. case DMA_PAUSE:
  627. dev_dbg(d->slave.dev, "vchan %p: pause\n", c);
  628. spin_lock_irqsave(&c->lock, flags);
  629. if (c->status == DMA_IN_PROGRESS) {
  630. c->status = DMA_PAUSED;
  631. p = c->phy;
  632. if (p) {
  633. writel(DCSR_RUN | DCSR_IE, p->base + DMA_DCSR_C);
  634. } else {
  635. spin_lock(&d->lock);
  636. list_del_init(&c->node);
  637. spin_unlock(&d->lock);
  638. }
  639. }
  640. spin_unlock_irqrestore(&c->lock, flags);
  641. ret = 0;
  642. break;
  643. case DMA_RESUME:
  644. dev_dbg(d->slave.dev, "vchan %p: resume\n", c);
  645. spin_lock_irqsave(&c->lock, flags);
  646. if (c->status == DMA_PAUSED) {
  647. c->status = DMA_IN_PROGRESS;
  648. p = c->phy;
  649. if (p) {
  650. writel(DCSR_RUN | DCSR_IE, p->base + DMA_DCSR_S);
  651. } else if (!list_empty(&c->desc_issued)) {
  652. spin_lock(&d->lock);
  653. list_add_tail(&c->node, &d->chan_pending);
  654. spin_unlock(&d->lock);
  655. }
  656. }
  657. spin_unlock_irqrestore(&c->lock, flags);
  658. ret = 0;
  659. break;
  660. default:
  661. ret = -ENXIO;
  662. break;
  663. }
  664. return ret;
  665. }
  666. struct sa11x0_dma_channel_desc {
  667. u32 ddar;
  668. const char *name;
  669. };
  670. #define CD(d1, d2) { .ddar = DDAR_##d1 | d2, .name = #d1 }
  671. static const struct sa11x0_dma_channel_desc chan_desc[] = {
  672. CD(Ser0UDCTr, 0),
  673. CD(Ser0UDCRc, DDAR_RW),
  674. CD(Ser1SDLCTr, 0),
  675. CD(Ser1SDLCRc, DDAR_RW),
  676. CD(Ser1UARTTr, 0),
  677. CD(Ser1UARTRc, DDAR_RW),
  678. CD(Ser2ICPTr, 0),
  679. CD(Ser2ICPRc, DDAR_RW),
  680. CD(Ser3UARTTr, 0),
  681. CD(Ser3UARTRc, DDAR_RW),
  682. CD(Ser4MCP0Tr, 0),
  683. CD(Ser4MCP0Rc, DDAR_RW),
  684. CD(Ser4MCP1Tr, 0),
  685. CD(Ser4MCP1Rc, DDAR_RW),
  686. CD(Ser4SSPTr, 0),
  687. CD(Ser4SSPRc, DDAR_RW),
  688. };
  689. static int __devinit sa11x0_dma_init_dmadev(struct dma_device *dmadev,
  690. struct device *dev)
  691. {
  692. unsigned i;
  693. dmadev->chancnt = ARRAY_SIZE(chan_desc);
  694. INIT_LIST_HEAD(&dmadev->channels);
  695. dmadev->dev = dev;
  696. dmadev->device_alloc_chan_resources = sa11x0_dma_alloc_chan_resources;
  697. dmadev->device_free_chan_resources = sa11x0_dma_free_chan_resources;
  698. dmadev->device_control = sa11x0_dma_control;
  699. dmadev->device_tx_status = sa11x0_dma_tx_status;
  700. dmadev->device_issue_pending = sa11x0_dma_issue_pending;
  701. for (i = 0; i < dmadev->chancnt; i++) {
  702. struct sa11x0_dma_chan *c;
  703. c = kzalloc(sizeof(*c), GFP_KERNEL);
  704. if (!c) {
  705. dev_err(dev, "no memory for channel %u\n", i);
  706. return -ENOMEM;
  707. }
  708. c->chan.device = dmadev;
  709. c->status = DMA_IN_PROGRESS;
  710. c->ddar = chan_desc[i].ddar;
  711. c->name = chan_desc[i].name;
  712. spin_lock_init(&c->lock);
  713. INIT_LIST_HEAD(&c->desc_submitted);
  714. INIT_LIST_HEAD(&c->desc_issued);
  715. INIT_LIST_HEAD(&c->node);
  716. list_add_tail(&c->chan.device_node, &dmadev->channels);
  717. }
  718. return dma_async_device_register(dmadev);
  719. }
  720. static int sa11x0_dma_request_irq(struct platform_device *pdev, int nr,
  721. void *data)
  722. {
  723. int irq = platform_get_irq(pdev, nr);
  724. if (irq <= 0)
  725. return -ENXIO;
  726. return request_irq(irq, sa11x0_dma_irq, 0, dev_name(&pdev->dev), data);
  727. }
  728. static void sa11x0_dma_free_irq(struct platform_device *pdev, int nr,
  729. void *data)
  730. {
  731. int irq = platform_get_irq(pdev, nr);
  732. if (irq > 0)
  733. free_irq(irq, data);
  734. }
  735. static void sa11x0_dma_free_channels(struct dma_device *dmadev)
  736. {
  737. struct sa11x0_dma_chan *c, *cn;
  738. list_for_each_entry_safe(c, cn, &dmadev->channels, chan.device_node) {
  739. list_del(&c->chan.device_node);
  740. kfree(c);
  741. }
  742. }
  743. static int __devinit sa11x0_dma_probe(struct platform_device *pdev)
  744. {
  745. struct sa11x0_dma_dev *d;
  746. struct resource *res;
  747. unsigned i;
  748. int ret;
  749. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  750. if (!res)
  751. return -ENXIO;
  752. d = kzalloc(sizeof(*d), GFP_KERNEL);
  753. if (!d) {
  754. ret = -ENOMEM;
  755. goto err_alloc;
  756. }
  757. spin_lock_init(&d->lock);
  758. INIT_LIST_HEAD(&d->chan_pending);
  759. INIT_LIST_HEAD(&d->desc_complete);
  760. d->base = ioremap(res->start, resource_size(res));
  761. if (!d->base) {
  762. ret = -ENOMEM;
  763. goto err_ioremap;
  764. }
  765. tasklet_init(&d->task, sa11x0_dma_tasklet, (unsigned long)d);
  766. for (i = 0; i < NR_PHY_CHAN; i++) {
  767. struct sa11x0_dma_phy *p = &d->phy[i];
  768. p->dev = d;
  769. p->num = i;
  770. p->base = d->base + i * DMA_SIZE;
  771. writel_relaxed(DCSR_RUN | DCSR_IE | DCSR_ERROR |
  772. DCSR_DONEA | DCSR_STRTA | DCSR_DONEB | DCSR_STRTB,
  773. p->base + DMA_DCSR_C);
  774. writel_relaxed(0, p->base + DMA_DDAR);
  775. ret = sa11x0_dma_request_irq(pdev, i, p);
  776. if (ret) {
  777. while (i) {
  778. i--;
  779. sa11x0_dma_free_irq(pdev, i, &d->phy[i]);
  780. }
  781. goto err_irq;
  782. }
  783. }
  784. dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
  785. d->slave.device_prep_slave_sg = sa11x0_dma_prep_slave_sg;
  786. ret = sa11x0_dma_init_dmadev(&d->slave, &pdev->dev);
  787. if (ret) {
  788. dev_warn(d->slave.dev, "failed to register slave async device: %d\n",
  789. ret);
  790. goto err_slave_reg;
  791. }
  792. platform_set_drvdata(pdev, d);
  793. return 0;
  794. err_slave_reg:
  795. sa11x0_dma_free_channels(&d->slave);
  796. for (i = 0; i < NR_PHY_CHAN; i++)
  797. sa11x0_dma_free_irq(pdev, i, &d->phy[i]);
  798. err_irq:
  799. tasklet_kill(&d->task);
  800. iounmap(d->base);
  801. err_ioremap:
  802. kfree(d);
  803. err_alloc:
  804. return ret;
  805. }
  806. static int __devexit sa11x0_dma_remove(struct platform_device *pdev)
  807. {
  808. struct sa11x0_dma_dev *d = platform_get_drvdata(pdev);
  809. unsigned pch;
  810. dma_async_device_unregister(&d->slave);
  811. sa11x0_dma_free_channels(&d->slave);
  812. for (pch = 0; pch < NR_PHY_CHAN; pch++)
  813. sa11x0_dma_free_irq(pdev, pch, &d->phy[pch]);
  814. tasklet_kill(&d->task);
  815. iounmap(d->base);
  816. kfree(d);
  817. return 0;
  818. }
  819. #ifdef CONFIG_PM_SLEEP
  820. static int sa11x0_dma_suspend(struct device *dev)
  821. {
  822. struct sa11x0_dma_dev *d = dev_get_drvdata(dev);
  823. unsigned pch;
  824. for (pch = 0; pch < NR_PHY_CHAN; pch++) {
  825. struct sa11x0_dma_phy *p = &d->phy[pch];
  826. u32 dcsr, saved_dcsr;
  827. dcsr = saved_dcsr = readl_relaxed(p->base + DMA_DCSR_R);
  828. if (dcsr & DCSR_RUN) {
  829. writel(DCSR_RUN | DCSR_IE, p->base + DMA_DCSR_C);
  830. dcsr = readl_relaxed(p->base + DMA_DCSR_R);
  831. }
  832. saved_dcsr &= DCSR_RUN | DCSR_IE;
  833. if (dcsr & DCSR_BIU) {
  834. p->dbs[0] = readl_relaxed(p->base + DMA_DBSB);
  835. p->dbt[0] = readl_relaxed(p->base + DMA_DBTB);
  836. p->dbs[1] = readl_relaxed(p->base + DMA_DBSA);
  837. p->dbt[1] = readl_relaxed(p->base + DMA_DBTA);
  838. saved_dcsr |= (dcsr & DCSR_STRTA ? DCSR_STRTB : 0) |
  839. (dcsr & DCSR_STRTB ? DCSR_STRTA : 0);
  840. } else {
  841. p->dbs[0] = readl_relaxed(p->base + DMA_DBSA);
  842. p->dbt[0] = readl_relaxed(p->base + DMA_DBTA);
  843. p->dbs[1] = readl_relaxed(p->base + DMA_DBSB);
  844. p->dbt[1] = readl_relaxed(p->base + DMA_DBTB);
  845. saved_dcsr |= dcsr & (DCSR_STRTA | DCSR_STRTB);
  846. }
  847. p->dcsr = saved_dcsr;
  848. writel(DCSR_STRTA | DCSR_STRTB, p->base + DMA_DCSR_C);
  849. }
  850. return 0;
  851. }
  852. static int sa11x0_dma_resume(struct device *dev)
  853. {
  854. struct sa11x0_dma_dev *d = dev_get_drvdata(dev);
  855. unsigned pch;
  856. for (pch = 0; pch < NR_PHY_CHAN; pch++) {
  857. struct sa11x0_dma_phy *p = &d->phy[pch];
  858. struct sa11x0_dma_desc *txd = NULL;
  859. u32 dcsr = readl_relaxed(p->base + DMA_DCSR_R);
  860. WARN_ON(dcsr & (DCSR_BIU | DCSR_STRTA | DCSR_STRTB | DCSR_RUN));
  861. if (p->txd_done)
  862. txd = p->txd_done;
  863. else if (p->txd_load)
  864. txd = p->txd_load;
  865. if (!txd)
  866. continue;
  867. writel_relaxed(txd->ddar, p->base + DMA_DDAR);
  868. writel_relaxed(p->dbs[0], p->base + DMA_DBSA);
  869. writel_relaxed(p->dbt[0], p->base + DMA_DBTA);
  870. writel_relaxed(p->dbs[1], p->base + DMA_DBSB);
  871. writel_relaxed(p->dbt[1], p->base + DMA_DBTB);
  872. writel_relaxed(p->dcsr, p->base + DMA_DCSR_S);
  873. }
  874. return 0;
  875. }
  876. #endif
  877. static const struct dev_pm_ops sa11x0_dma_pm_ops = {
  878. .suspend_noirq = sa11x0_dma_suspend,
  879. .resume_noirq = sa11x0_dma_resume,
  880. .freeze_noirq = sa11x0_dma_suspend,
  881. .thaw_noirq = sa11x0_dma_resume,
  882. .poweroff_noirq = sa11x0_dma_suspend,
  883. .restore_noirq = sa11x0_dma_resume,
  884. };
  885. static struct platform_driver sa11x0_dma_driver = {
  886. .driver = {
  887. .name = "sa11x0-dma",
  888. .owner = THIS_MODULE,
  889. .pm = &sa11x0_dma_pm_ops,
  890. },
  891. .probe = sa11x0_dma_probe,
  892. .remove = __devexit_p(sa11x0_dma_remove),
  893. };
  894. bool sa11x0_dma_filter_fn(struct dma_chan *chan, void *param)
  895. {
  896. if (chan->device->dev->driver == &sa11x0_dma_driver.driver) {
  897. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  898. const char *p = param;
  899. return !strcmp(c->name, p);
  900. }
  901. return false;
  902. }
  903. EXPORT_SYMBOL(sa11x0_dma_filter_fn);
  904. static int __init sa11x0_dma_init(void)
  905. {
  906. return platform_driver_register(&sa11x0_dma_driver);
  907. }
  908. subsys_initcall(sa11x0_dma_init);
  909. static void __exit sa11x0_dma_exit(void)
  910. {
  911. platform_driver_unregister(&sa11x0_dma_driver);
  912. }
  913. module_exit(sa11x0_dma_exit);
  914. MODULE_AUTHOR("Russell King");
  915. MODULE_DESCRIPTION("SA-11x0 DMA driver");
  916. MODULE_LICENSE("GPL v2");
  917. MODULE_ALIAS("platform:sa11x0-dma");