sa11x0-dma.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. #include "virt-dma.h"
  24. #define NR_PHY_CHAN 6
  25. #define DMA_ALIGN 3
  26. #define DMA_MAX_SIZE 0x1fff
  27. #define DMA_CHUNK_SIZE 0x1000
  28. #define DMA_DDAR 0x00
  29. #define DMA_DCSR_S 0x04
  30. #define DMA_DCSR_C 0x08
  31. #define DMA_DCSR_R 0x0c
  32. #define DMA_DBSA 0x10
  33. #define DMA_DBTA 0x14
  34. #define DMA_DBSB 0x18
  35. #define DMA_DBTB 0x1c
  36. #define DMA_SIZE 0x20
  37. #define DCSR_RUN (1 << 0)
  38. #define DCSR_IE (1 << 1)
  39. #define DCSR_ERROR (1 << 2)
  40. #define DCSR_DONEA (1 << 3)
  41. #define DCSR_STRTA (1 << 4)
  42. #define DCSR_DONEB (1 << 5)
  43. #define DCSR_STRTB (1 << 6)
  44. #define DCSR_BIU (1 << 7)
  45. #define DDAR_RW (1 << 0) /* 0 = W, 1 = R */
  46. #define DDAR_E (1 << 1) /* 0 = LE, 1 = BE */
  47. #define DDAR_BS (1 << 2) /* 0 = BS4, 1 = BS8 */
  48. #define DDAR_DW (1 << 3) /* 0 = 8b, 1 = 16b */
  49. #define DDAR_Ser0UDCTr (0x0 << 4)
  50. #define DDAR_Ser0UDCRc (0x1 << 4)
  51. #define DDAR_Ser1SDLCTr (0x2 << 4)
  52. #define DDAR_Ser1SDLCRc (0x3 << 4)
  53. #define DDAR_Ser1UARTTr (0x4 << 4)
  54. #define DDAR_Ser1UARTRc (0x5 << 4)
  55. #define DDAR_Ser2ICPTr (0x6 << 4)
  56. #define DDAR_Ser2ICPRc (0x7 << 4)
  57. #define DDAR_Ser3UARTTr (0x8 << 4)
  58. #define DDAR_Ser3UARTRc (0x9 << 4)
  59. #define DDAR_Ser4MCP0Tr (0xa << 4)
  60. #define DDAR_Ser4MCP0Rc (0xb << 4)
  61. #define DDAR_Ser4MCP1Tr (0xc << 4)
  62. #define DDAR_Ser4MCP1Rc (0xd << 4)
  63. #define DDAR_Ser4SSPTr (0xe << 4)
  64. #define DDAR_Ser4SSPRc (0xf << 4)
  65. struct sa11x0_dma_sg {
  66. u32 addr;
  67. u32 len;
  68. };
  69. struct sa11x0_dma_desc {
  70. struct virt_dma_desc vd;
  71. u32 ddar;
  72. size_t size;
  73. unsigned period;
  74. bool cyclic;
  75. unsigned sglen;
  76. struct sa11x0_dma_sg sg[0];
  77. };
  78. struct sa11x0_dma_phy;
  79. struct sa11x0_dma_chan {
  80. struct virt_dma_chan vc;
  81. /* protected by c->vc.lock */
  82. struct sa11x0_dma_phy *phy;
  83. enum dma_status status;
  84. /* protected by d->lock */
  85. struct list_head node;
  86. u32 ddar;
  87. const char *name;
  88. };
  89. struct sa11x0_dma_phy {
  90. void __iomem *base;
  91. struct sa11x0_dma_dev *dev;
  92. unsigned num;
  93. struct sa11x0_dma_chan *vchan;
  94. /* Protected by c->vc.lock */
  95. unsigned sg_load;
  96. struct sa11x0_dma_desc *txd_load;
  97. unsigned sg_done;
  98. struct sa11x0_dma_desc *txd_done;
  99. u32 dbs[2];
  100. u32 dbt[2];
  101. u32 dcsr;
  102. };
  103. struct sa11x0_dma_dev {
  104. struct dma_device slave;
  105. void __iomem *base;
  106. spinlock_t lock;
  107. struct tasklet_struct task;
  108. struct list_head chan_pending;
  109. struct sa11x0_dma_phy phy[NR_PHY_CHAN];
  110. };
  111. static struct sa11x0_dma_chan *to_sa11x0_dma_chan(struct dma_chan *chan)
  112. {
  113. return container_of(chan, struct sa11x0_dma_chan, vc.chan);
  114. }
  115. static struct sa11x0_dma_dev *to_sa11x0_dma(struct dma_device *dmadev)
  116. {
  117. return container_of(dmadev, struct sa11x0_dma_dev, slave);
  118. }
  119. static struct sa11x0_dma_desc *sa11x0_dma_next_desc(struct sa11x0_dma_chan *c)
  120. {
  121. struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  122. return vd ? container_of(vd, struct sa11x0_dma_desc, vd) : NULL;
  123. }
  124. static void sa11x0_dma_free_desc(struct virt_dma_desc *vd)
  125. {
  126. kfree(container_of(vd, struct sa11x0_dma_desc, vd));
  127. }
  128. static void sa11x0_dma_start_desc(struct sa11x0_dma_phy *p, struct sa11x0_dma_desc *txd)
  129. {
  130. list_del(&txd->vd.node);
  131. p->txd_load = txd;
  132. p->sg_load = 0;
  133. dev_vdbg(p->dev->slave.dev, "pchan %u: txd %p[%x]: starting: DDAR:%x\n",
  134. p->num, &txd->vd, txd->vd.tx.cookie, txd->ddar);
  135. }
  136. static void noinline sa11x0_dma_start_sg(struct sa11x0_dma_phy *p,
  137. struct sa11x0_dma_chan *c)
  138. {
  139. struct sa11x0_dma_desc *txd = p->txd_load;
  140. struct sa11x0_dma_sg *sg;
  141. void __iomem *base = p->base;
  142. unsigned dbsx, dbtx;
  143. u32 dcsr;
  144. if (!txd)
  145. return;
  146. dcsr = readl_relaxed(base + DMA_DCSR_R);
  147. /* Don't try to load the next transfer if both buffers are started */
  148. if ((dcsr & (DCSR_STRTA | DCSR_STRTB)) == (DCSR_STRTA | DCSR_STRTB))
  149. return;
  150. if (p->sg_load == txd->sglen) {
  151. if (!txd->cyclic) {
  152. struct sa11x0_dma_desc *txn = sa11x0_dma_next_desc(c);
  153. /*
  154. * We have reached the end of the current descriptor.
  155. * Peek at the next descriptor, and if compatible with
  156. * the current, start processing it.
  157. */
  158. if (txn && txn->ddar == txd->ddar) {
  159. txd = txn;
  160. sa11x0_dma_start_desc(p, txn);
  161. } else {
  162. p->txd_load = NULL;
  163. return;
  164. }
  165. } else {
  166. /* Cyclic: reset back to beginning */
  167. p->sg_load = 0;
  168. }
  169. }
  170. sg = &txd->sg[p->sg_load++];
  171. /* Select buffer to load according to channel status */
  172. if (((dcsr & (DCSR_BIU | DCSR_STRTB)) == (DCSR_BIU | DCSR_STRTB)) ||
  173. ((dcsr & (DCSR_BIU | DCSR_STRTA)) == 0)) {
  174. dbsx = DMA_DBSA;
  175. dbtx = DMA_DBTA;
  176. dcsr = DCSR_STRTA | DCSR_IE | DCSR_RUN;
  177. } else {
  178. dbsx = DMA_DBSB;
  179. dbtx = DMA_DBTB;
  180. dcsr = DCSR_STRTB | DCSR_IE | DCSR_RUN;
  181. }
  182. writel_relaxed(sg->addr, base + dbsx);
  183. writel_relaxed(sg->len, base + dbtx);
  184. writel(dcsr, base + DMA_DCSR_S);
  185. dev_dbg(p->dev->slave.dev, "pchan %u: load: DCSR:%02x DBS%c:%08x DBT%c:%08x\n",
  186. p->num, dcsr,
  187. 'A' + (dbsx == DMA_DBSB), sg->addr,
  188. 'A' + (dbtx == DMA_DBTB), sg->len);
  189. }
  190. static void noinline sa11x0_dma_complete(struct sa11x0_dma_phy *p,
  191. struct sa11x0_dma_chan *c)
  192. {
  193. struct sa11x0_dma_desc *txd = p->txd_done;
  194. if (++p->sg_done == txd->sglen) {
  195. if (!txd->cyclic) {
  196. vchan_cookie_complete(&txd->vd);
  197. p->sg_done = 0;
  198. p->txd_done = p->txd_load;
  199. if (!p->txd_done)
  200. tasklet_schedule(&p->dev->task);
  201. } else {
  202. if ((p->sg_done % txd->period) == 0)
  203. vchan_cyclic_callback(&txd->vd);
  204. /* Cyclic: reset back to beginning */
  205. p->sg_done = 0;
  206. }
  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->vc.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->vc.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. unsigned pch, pch_alloc = 0;
  280. dev_dbg(d->slave.dev, "tasklet enter\n");
  281. list_for_each_entry(c, &d->slave.channels, vc.chan.device_node) {
  282. spin_lock_irq(&c->vc.lock);
  283. p = c->phy;
  284. if (p && !p->txd_done) {
  285. sa11x0_dma_start_txd(c);
  286. if (!p->txd_done) {
  287. /* No current txd associated with this channel */
  288. dev_dbg(d->slave.dev, "pchan %u: free\n", p->num);
  289. /* Mark this channel free */
  290. c->phy = NULL;
  291. p->vchan = NULL;
  292. }
  293. }
  294. spin_unlock_irq(&c->vc.lock);
  295. }
  296. spin_lock_irq(&d->lock);
  297. for (pch = 0; pch < NR_PHY_CHAN; pch++) {
  298. p = &d->phy[pch];
  299. if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
  300. c = list_first_entry(&d->chan_pending,
  301. struct sa11x0_dma_chan, node);
  302. list_del_init(&c->node);
  303. pch_alloc |= 1 << pch;
  304. /* Mark this channel allocated */
  305. p->vchan = c;
  306. dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, &c->vc);
  307. }
  308. }
  309. spin_unlock_irq(&d->lock);
  310. for (pch = 0; pch < NR_PHY_CHAN; pch++) {
  311. if (pch_alloc & (1 << pch)) {
  312. p = &d->phy[pch];
  313. c = p->vchan;
  314. spin_lock_irq(&c->vc.lock);
  315. c->phy = p;
  316. sa11x0_dma_start_txd(c);
  317. spin_unlock_irq(&c->vc.lock);
  318. }
  319. }
  320. dev_dbg(d->slave.dev, "tasklet exit\n");
  321. }
  322. static void sa11x0_dma_free_chan_resources(struct dma_chan *chan)
  323. {
  324. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  325. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  326. unsigned long flags;
  327. spin_lock_irqsave(&d->lock, flags);
  328. list_del_init(&c->node);
  329. spin_unlock_irqrestore(&d->lock, flags);
  330. vchan_free_chan_resources(&c->vc);
  331. }
  332. static dma_addr_t sa11x0_dma_pos(struct sa11x0_dma_phy *p)
  333. {
  334. unsigned reg;
  335. u32 dcsr;
  336. dcsr = readl_relaxed(p->base + DMA_DCSR_R);
  337. if ((dcsr & (DCSR_BIU | DCSR_STRTA)) == DCSR_STRTA ||
  338. (dcsr & (DCSR_BIU | DCSR_STRTB)) == DCSR_BIU)
  339. reg = DMA_DBSA;
  340. else
  341. reg = DMA_DBSB;
  342. return readl_relaxed(p->base + reg);
  343. }
  344. static enum dma_status sa11x0_dma_tx_status(struct dma_chan *chan,
  345. dma_cookie_t cookie, struct dma_tx_state *state)
  346. {
  347. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  348. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  349. struct sa11x0_dma_phy *p;
  350. struct virt_dma_desc *vd;
  351. unsigned long flags;
  352. enum dma_status ret;
  353. ret = dma_cookie_status(&c->vc.chan, cookie, state);
  354. if (ret == DMA_COMPLETE)
  355. return ret;
  356. if (!state)
  357. return c->status;
  358. spin_lock_irqsave(&c->vc.lock, flags);
  359. p = c->phy;
  360. /*
  361. * If the cookie is on our issue queue, then the residue is
  362. * its total size.
  363. */
  364. vd = vchan_find_desc(&c->vc, cookie);
  365. if (vd) {
  366. state->residue = container_of(vd, struct sa11x0_dma_desc, vd)->size;
  367. } else if (!p) {
  368. state->residue = 0;
  369. } else {
  370. struct sa11x0_dma_desc *txd;
  371. size_t bytes = 0;
  372. if (p->txd_done && p->txd_done->vd.tx.cookie == cookie)
  373. txd = p->txd_done;
  374. else if (p->txd_load && p->txd_load->vd.tx.cookie == cookie)
  375. txd = p->txd_load;
  376. else
  377. txd = NULL;
  378. ret = c->status;
  379. if (txd) {
  380. dma_addr_t addr = sa11x0_dma_pos(p);
  381. unsigned i;
  382. dev_vdbg(d->slave.dev, "tx_status: addr:%x\n", addr);
  383. for (i = 0; i < txd->sglen; i++) {
  384. dev_vdbg(d->slave.dev, "tx_status: [%u] %x+%x\n",
  385. i, txd->sg[i].addr, txd->sg[i].len);
  386. if (addr >= txd->sg[i].addr &&
  387. addr < txd->sg[i].addr + txd->sg[i].len) {
  388. unsigned len;
  389. len = txd->sg[i].len -
  390. (addr - txd->sg[i].addr);
  391. dev_vdbg(d->slave.dev, "tx_status: [%u] +%x\n",
  392. i, len);
  393. bytes += len;
  394. i++;
  395. break;
  396. }
  397. }
  398. for (; i < txd->sglen; i++) {
  399. dev_vdbg(d->slave.dev, "tx_status: [%u] %x+%x ++\n",
  400. i, txd->sg[i].addr, txd->sg[i].len);
  401. bytes += txd->sg[i].len;
  402. }
  403. }
  404. state->residue = bytes;
  405. }
  406. spin_unlock_irqrestore(&c->vc.lock, flags);
  407. dev_vdbg(d->slave.dev, "tx_status: bytes 0x%zx\n", state->residue);
  408. return ret;
  409. }
  410. /*
  411. * Move pending txds to the issued list, and re-init pending list.
  412. * If not already pending, add this channel to the list of pending
  413. * channels and trigger the tasklet to run.
  414. */
  415. static void sa11x0_dma_issue_pending(struct dma_chan *chan)
  416. {
  417. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  418. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  419. unsigned long flags;
  420. spin_lock_irqsave(&c->vc.lock, flags);
  421. if (vchan_issue_pending(&c->vc)) {
  422. if (!c->phy) {
  423. spin_lock(&d->lock);
  424. if (list_empty(&c->node)) {
  425. list_add_tail(&c->node, &d->chan_pending);
  426. tasklet_schedule(&d->task);
  427. dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
  428. }
  429. spin_unlock(&d->lock);
  430. }
  431. } else
  432. dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
  433. spin_unlock_irqrestore(&c->vc.lock, flags);
  434. }
  435. static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg(
  436. struct dma_chan *chan, struct scatterlist *sg, unsigned int sglen,
  437. enum dma_transfer_direction dir, unsigned long flags, void *context)
  438. {
  439. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  440. struct sa11x0_dma_desc *txd;
  441. struct scatterlist *sgent;
  442. unsigned i, j = sglen;
  443. size_t size = 0;
  444. /* SA11x0 channels can only operate in their native direction */
  445. if (dir != (c->ddar & DDAR_RW ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV)) {
  446. dev_err(chan->device->dev, "vchan %p: bad DMA direction: DDAR:%08x dir:%u\n",
  447. &c->vc, c->ddar, dir);
  448. return NULL;
  449. }
  450. /* Do not allow zero-sized txds */
  451. if (sglen == 0)
  452. return NULL;
  453. for_each_sg(sg, sgent, sglen, i) {
  454. dma_addr_t addr = sg_dma_address(sgent);
  455. unsigned int len = sg_dma_len(sgent);
  456. if (len > DMA_MAX_SIZE)
  457. j += DIV_ROUND_UP(len, DMA_MAX_SIZE & ~DMA_ALIGN) - 1;
  458. if (addr & DMA_ALIGN) {
  459. dev_dbg(chan->device->dev, "vchan %p: bad buffer alignment: %08x\n",
  460. &c->vc, addr);
  461. return NULL;
  462. }
  463. }
  464. txd = kzalloc(sizeof(*txd) + j * sizeof(txd->sg[0]), GFP_ATOMIC);
  465. if (!txd) {
  466. dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", &c->vc);
  467. return NULL;
  468. }
  469. j = 0;
  470. for_each_sg(sg, sgent, sglen, i) {
  471. dma_addr_t addr = sg_dma_address(sgent);
  472. unsigned len = sg_dma_len(sgent);
  473. size += len;
  474. do {
  475. unsigned tlen = len;
  476. /*
  477. * Check whether the transfer will fit. If not, try
  478. * to split the transfer up such that we end up with
  479. * equal chunks - but make sure that we preserve the
  480. * alignment. This avoids small segments.
  481. */
  482. if (tlen > DMA_MAX_SIZE) {
  483. unsigned mult = DIV_ROUND_UP(tlen,
  484. DMA_MAX_SIZE & ~DMA_ALIGN);
  485. tlen = (tlen / mult) & ~DMA_ALIGN;
  486. }
  487. txd->sg[j].addr = addr;
  488. txd->sg[j].len = tlen;
  489. addr += tlen;
  490. len -= tlen;
  491. j++;
  492. } while (len);
  493. }
  494. txd->ddar = c->ddar;
  495. txd->size = size;
  496. txd->sglen = j;
  497. dev_dbg(chan->device->dev, "vchan %p: txd %p: size %u nr %u\n",
  498. &c->vc, &txd->vd, txd->size, txd->sglen);
  499. return vchan_tx_prep(&c->vc, &txd->vd, flags);
  500. }
  501. static struct dma_async_tx_descriptor *sa11x0_dma_prep_dma_cyclic(
  502. struct dma_chan *chan, dma_addr_t addr, size_t size, size_t period,
  503. enum dma_transfer_direction dir, unsigned long flags)
  504. {
  505. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  506. struct sa11x0_dma_desc *txd;
  507. unsigned i, j, k, sglen, sgperiod;
  508. /* SA11x0 channels can only operate in their native direction */
  509. if (dir != (c->ddar & DDAR_RW ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV)) {
  510. dev_err(chan->device->dev, "vchan %p: bad DMA direction: DDAR:%08x dir:%u\n",
  511. &c->vc, c->ddar, dir);
  512. return NULL;
  513. }
  514. sgperiod = DIV_ROUND_UP(period, DMA_MAX_SIZE & ~DMA_ALIGN);
  515. sglen = size * sgperiod / period;
  516. /* Do not allow zero-sized txds */
  517. if (sglen == 0)
  518. return NULL;
  519. txd = kzalloc(sizeof(*txd) + sglen * sizeof(txd->sg[0]), GFP_ATOMIC);
  520. if (!txd) {
  521. dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", &c->vc);
  522. return NULL;
  523. }
  524. for (i = k = 0; i < size / period; i++) {
  525. size_t tlen, len = period;
  526. for (j = 0; j < sgperiod; j++, k++) {
  527. tlen = len;
  528. if (tlen > DMA_MAX_SIZE) {
  529. unsigned mult = DIV_ROUND_UP(tlen, DMA_MAX_SIZE & ~DMA_ALIGN);
  530. tlen = (tlen / mult) & ~DMA_ALIGN;
  531. }
  532. txd->sg[k].addr = addr;
  533. txd->sg[k].len = tlen;
  534. addr += tlen;
  535. len -= tlen;
  536. }
  537. WARN_ON(len != 0);
  538. }
  539. WARN_ON(k != sglen);
  540. txd->ddar = c->ddar;
  541. txd->size = size;
  542. txd->sglen = sglen;
  543. txd->cyclic = 1;
  544. txd->period = sgperiod;
  545. return vchan_tx_prep(&c->vc, &txd->vd, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  546. }
  547. static int sa11x0_dma_device_config(struct dma_chan *chan,
  548. struct dma_slave_config *cfg)
  549. {
  550. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  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->vc.chan.device->dev, "vchan %p: dma_slave_config addr %x width %u burst %u\n",
  573. &c->vc, addr, width, maxburst);
  574. c->ddar = ddar | (addr & 0xf0000000) | (addr & 0x003ffffc) << 6;
  575. return 0;
  576. }
  577. static int sa11x0_dma_device_pause(struct dma_chan *chan)
  578. {
  579. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  580. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  581. struct sa11x0_dma_phy *p;
  582. LIST_HEAD(head);
  583. unsigned long flags;
  584. dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
  585. spin_lock_irqsave(&c->vc.lock, flags);
  586. if (c->status == DMA_IN_PROGRESS) {
  587. c->status = DMA_PAUSED;
  588. p = c->phy;
  589. if (p) {
  590. writel(DCSR_RUN | DCSR_IE, p->base + DMA_DCSR_C);
  591. } else {
  592. spin_lock(&d->lock);
  593. list_del_init(&c->node);
  594. spin_unlock(&d->lock);
  595. }
  596. }
  597. spin_unlock_irqrestore(&c->vc.lock, flags);
  598. return 0;
  599. }
  600. static int sa11x0_dma_device_resume(struct dma_chan *chan)
  601. {
  602. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  603. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  604. struct sa11x0_dma_phy *p;
  605. LIST_HEAD(head);
  606. unsigned long flags;
  607. dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
  608. spin_lock_irqsave(&c->vc.lock, flags);
  609. if (c->status == DMA_PAUSED) {
  610. c->status = DMA_IN_PROGRESS;
  611. p = c->phy;
  612. if (p) {
  613. writel(DCSR_RUN | DCSR_IE, p->base + DMA_DCSR_S);
  614. } else if (!list_empty(&c->vc.desc_issued)) {
  615. spin_lock(&d->lock);
  616. list_add_tail(&c->node, &d->chan_pending);
  617. spin_unlock(&d->lock);
  618. }
  619. }
  620. spin_unlock_irqrestore(&c->vc.lock, flags);
  621. return 0;
  622. }
  623. static int sa11x0_dma_device_terminate_all(struct dma_chan *chan)
  624. {
  625. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  626. struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
  627. struct sa11x0_dma_phy *p;
  628. LIST_HEAD(head);
  629. unsigned long flags;
  630. dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
  631. /* Clear the tx descriptor lists */
  632. spin_lock_irqsave(&c->vc.lock, flags);
  633. vchan_get_all_descriptors(&c->vc, &head);
  634. p = c->phy;
  635. if (p) {
  636. dev_dbg(d->slave.dev, "pchan %u: terminating\n", p->num);
  637. /* vchan is assigned to a pchan - stop the channel */
  638. writel(DCSR_RUN | DCSR_IE |
  639. DCSR_STRTA | DCSR_DONEA |
  640. DCSR_STRTB | DCSR_DONEB,
  641. p->base + DMA_DCSR_C);
  642. if (p->txd_load) {
  643. if (p->txd_load != p->txd_done)
  644. list_add_tail(&p->txd_load->vd.node, &head);
  645. p->txd_load = NULL;
  646. }
  647. if (p->txd_done) {
  648. list_add_tail(&p->txd_done->vd.node, &head);
  649. p->txd_done = NULL;
  650. }
  651. c->phy = NULL;
  652. spin_lock(&d->lock);
  653. p->vchan = NULL;
  654. spin_unlock(&d->lock);
  655. tasklet_schedule(&d->task);
  656. }
  657. spin_unlock_irqrestore(&c->vc.lock, flags);
  658. vchan_dma_desc_free_list(&c->vc, &head);
  659. return 0;
  660. }
  661. struct sa11x0_dma_channel_desc {
  662. u32 ddar;
  663. const char *name;
  664. };
  665. #define CD(d1, d2) { .ddar = DDAR_##d1 | d2, .name = #d1 }
  666. static const struct sa11x0_dma_channel_desc chan_desc[] = {
  667. CD(Ser0UDCTr, 0),
  668. CD(Ser0UDCRc, DDAR_RW),
  669. CD(Ser1SDLCTr, 0),
  670. CD(Ser1SDLCRc, DDAR_RW),
  671. CD(Ser1UARTTr, 0),
  672. CD(Ser1UARTRc, DDAR_RW),
  673. CD(Ser2ICPTr, 0),
  674. CD(Ser2ICPRc, DDAR_RW),
  675. CD(Ser3UARTTr, 0),
  676. CD(Ser3UARTRc, DDAR_RW),
  677. CD(Ser4MCP0Tr, 0),
  678. CD(Ser4MCP0Rc, DDAR_RW),
  679. CD(Ser4MCP1Tr, 0),
  680. CD(Ser4MCP1Rc, DDAR_RW),
  681. CD(Ser4SSPTr, 0),
  682. CD(Ser4SSPRc, DDAR_RW),
  683. };
  684. static int sa11x0_dma_init_dmadev(struct dma_device *dmadev,
  685. struct device *dev)
  686. {
  687. unsigned i;
  688. INIT_LIST_HEAD(&dmadev->channels);
  689. dmadev->dev = dev;
  690. dmadev->device_free_chan_resources = sa11x0_dma_free_chan_resources;
  691. dmadev->device_config = sa11x0_dma_device_config;
  692. dmadev->device_pause = sa11x0_dma_device_pause;
  693. dmadev->device_resume = sa11x0_dma_device_resume;
  694. dmadev->device_terminate_all = sa11x0_dma_device_terminate_all;
  695. dmadev->device_tx_status = sa11x0_dma_tx_status;
  696. dmadev->device_issue_pending = sa11x0_dma_issue_pending;
  697. for (i = 0; i < ARRAY_SIZE(chan_desc); i++) {
  698. struct sa11x0_dma_chan *c;
  699. c = kzalloc(sizeof(*c), GFP_KERNEL);
  700. if (!c) {
  701. dev_err(dev, "no memory for channel %u\n", i);
  702. return -ENOMEM;
  703. }
  704. c->status = DMA_IN_PROGRESS;
  705. c->ddar = chan_desc[i].ddar;
  706. c->name = chan_desc[i].name;
  707. INIT_LIST_HEAD(&c->node);
  708. c->vc.desc_free = sa11x0_dma_free_desc;
  709. vchan_init(&c->vc, dmadev);
  710. }
  711. return dma_async_device_register(dmadev);
  712. }
  713. static int sa11x0_dma_request_irq(struct platform_device *pdev, int nr,
  714. void *data)
  715. {
  716. int irq = platform_get_irq(pdev, nr);
  717. if (irq <= 0)
  718. return -ENXIO;
  719. return request_irq(irq, sa11x0_dma_irq, 0, dev_name(&pdev->dev), data);
  720. }
  721. static void sa11x0_dma_free_irq(struct platform_device *pdev, int nr,
  722. void *data)
  723. {
  724. int irq = platform_get_irq(pdev, nr);
  725. if (irq > 0)
  726. free_irq(irq, data);
  727. }
  728. static void sa11x0_dma_free_channels(struct dma_device *dmadev)
  729. {
  730. struct sa11x0_dma_chan *c, *cn;
  731. list_for_each_entry_safe(c, cn, &dmadev->channels, vc.chan.device_node) {
  732. list_del(&c->vc.chan.device_node);
  733. tasklet_kill(&c->vc.task);
  734. kfree(c);
  735. }
  736. }
  737. static int sa11x0_dma_probe(struct platform_device *pdev)
  738. {
  739. struct sa11x0_dma_dev *d;
  740. struct resource *res;
  741. unsigned i;
  742. int ret;
  743. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  744. if (!res)
  745. return -ENXIO;
  746. d = kzalloc(sizeof(*d), GFP_KERNEL);
  747. if (!d) {
  748. ret = -ENOMEM;
  749. goto err_alloc;
  750. }
  751. spin_lock_init(&d->lock);
  752. INIT_LIST_HEAD(&d->chan_pending);
  753. d->base = ioremap(res->start, resource_size(res));
  754. if (!d->base) {
  755. ret = -ENOMEM;
  756. goto err_ioremap;
  757. }
  758. tasklet_init(&d->task, sa11x0_dma_tasklet, (unsigned long)d);
  759. for (i = 0; i < NR_PHY_CHAN; i++) {
  760. struct sa11x0_dma_phy *p = &d->phy[i];
  761. p->dev = d;
  762. p->num = i;
  763. p->base = d->base + i * DMA_SIZE;
  764. writel_relaxed(DCSR_RUN | DCSR_IE | DCSR_ERROR |
  765. DCSR_DONEA | DCSR_STRTA | DCSR_DONEB | DCSR_STRTB,
  766. p->base + DMA_DCSR_C);
  767. writel_relaxed(0, p->base + DMA_DDAR);
  768. ret = sa11x0_dma_request_irq(pdev, i, p);
  769. if (ret) {
  770. while (i) {
  771. i--;
  772. sa11x0_dma_free_irq(pdev, i, &d->phy[i]);
  773. }
  774. goto err_irq;
  775. }
  776. }
  777. dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
  778. dma_cap_set(DMA_CYCLIC, d->slave.cap_mask);
  779. d->slave.device_prep_slave_sg = sa11x0_dma_prep_slave_sg;
  780. d->slave.device_prep_dma_cyclic = sa11x0_dma_prep_dma_cyclic;
  781. d->slave.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  782. d->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  783. d->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  784. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES);
  785. d->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  786. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES);
  787. ret = sa11x0_dma_init_dmadev(&d->slave, &pdev->dev);
  788. if (ret) {
  789. dev_warn(d->slave.dev, "failed to register slave async device: %d\n",
  790. ret);
  791. goto err_slave_reg;
  792. }
  793. platform_set_drvdata(pdev, d);
  794. return 0;
  795. err_slave_reg:
  796. sa11x0_dma_free_channels(&d->slave);
  797. for (i = 0; i < NR_PHY_CHAN; i++)
  798. sa11x0_dma_free_irq(pdev, i, &d->phy[i]);
  799. err_irq:
  800. tasklet_kill(&d->task);
  801. iounmap(d->base);
  802. err_ioremap:
  803. kfree(d);
  804. err_alloc:
  805. return ret;
  806. }
  807. static int sa11x0_dma_remove(struct platform_device *pdev)
  808. {
  809. struct sa11x0_dma_dev *d = platform_get_drvdata(pdev);
  810. unsigned pch;
  811. dma_async_device_unregister(&d->slave);
  812. sa11x0_dma_free_channels(&d->slave);
  813. for (pch = 0; pch < NR_PHY_CHAN; pch++)
  814. sa11x0_dma_free_irq(pdev, pch, &d->phy[pch]);
  815. tasklet_kill(&d->task);
  816. iounmap(d->base);
  817. kfree(d);
  818. return 0;
  819. }
  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. static const struct dev_pm_ops sa11x0_dma_pm_ops = {
  877. .suspend_noirq = sa11x0_dma_suspend,
  878. .resume_noirq = sa11x0_dma_resume,
  879. .freeze_noirq = sa11x0_dma_suspend,
  880. .thaw_noirq = sa11x0_dma_resume,
  881. .poweroff_noirq = sa11x0_dma_suspend,
  882. .restore_noirq = sa11x0_dma_resume,
  883. };
  884. static struct platform_driver sa11x0_dma_driver = {
  885. .driver = {
  886. .name = "sa11x0-dma",
  887. .pm = &sa11x0_dma_pm_ops,
  888. },
  889. .probe = sa11x0_dma_probe,
  890. .remove = sa11x0_dma_remove,
  891. };
  892. bool sa11x0_dma_filter_fn(struct dma_chan *chan, void *param)
  893. {
  894. if (chan->device->dev->driver == &sa11x0_dma_driver.driver) {
  895. struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
  896. const char *p = param;
  897. return !strcmp(c->name, p);
  898. }
  899. return false;
  900. }
  901. EXPORT_SYMBOL(sa11x0_dma_filter_fn);
  902. static int __init sa11x0_dma_init(void)
  903. {
  904. return platform_driver_register(&sa11x0_dma_driver);
  905. }
  906. subsys_initcall(sa11x0_dma_init);
  907. static void __exit sa11x0_dma_exit(void)
  908. {
  909. platform_driver_unregister(&sa11x0_dma_driver);
  910. }
  911. module_exit(sa11x0_dma_exit);
  912. MODULE_AUTHOR("Russell King");
  913. MODULE_DESCRIPTION("SA-11x0 DMA driver");
  914. MODULE_LICENSE("GPL v2");
  915. MODULE_ALIAS("platform:sa11x0-dma");