edma.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /*
  2. * TI EDMA DMA engine driver
  3. *
  4. * Copyright 2012 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/dmaengine.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/edma.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_data/edma.h>
  28. #include "dmaengine.h"
  29. #include "virt-dma.h"
  30. /*
  31. * This will go away when the private EDMA API is folded
  32. * into this driver and the platform device(s) are
  33. * instantiated in the arch code. We can only get away
  34. * with this simplification because DA8XX may not be built
  35. * in the same kernel image with other DaVinci parts. This
  36. * avoids having to sprinkle dmaengine driver platform devices
  37. * and data throughout all the existing board files.
  38. */
  39. #ifdef CONFIG_ARCH_DAVINCI_DA8XX
  40. #define EDMA_CTLRS 2
  41. #define EDMA_CHANS 32
  42. #else
  43. #define EDMA_CTLRS 1
  44. #define EDMA_CHANS 64
  45. #endif /* CONFIG_ARCH_DAVINCI_DA8XX */
  46. /*
  47. * Max of 20 segments per channel to conserve PaRAM slots
  48. * Also note that MAX_NR_SG should be atleast the no.of periods
  49. * that are required for ASoC, otherwise DMA prep calls will
  50. * fail. Today davinci-pcm is the only user of this driver and
  51. * requires atleast 17 slots, so we setup the default to 20.
  52. */
  53. #define MAX_NR_SG 20
  54. #define EDMA_MAX_SLOTS MAX_NR_SG
  55. #define EDMA_DESCRIPTORS 16
  56. struct edma_pset {
  57. u32 len;
  58. dma_addr_t addr;
  59. struct edmacc_param param;
  60. };
  61. struct edma_desc {
  62. struct virt_dma_desc vdesc;
  63. struct list_head node;
  64. enum dma_transfer_direction direction;
  65. int cyclic;
  66. int absync;
  67. int pset_nr;
  68. struct edma_chan *echan;
  69. int processed;
  70. /*
  71. * The following 4 elements are used for residue accounting.
  72. *
  73. * - processed_stat: the number of SG elements we have traversed
  74. * so far to cover accounting. This is updated directly to processed
  75. * during edma_callback and is always <= processed, because processed
  76. * refers to the number of pending transfer (programmed to EDMA
  77. * controller), where as processed_stat tracks number of transfers
  78. * accounted for so far.
  79. *
  80. * - residue: The amount of bytes we have left to transfer for this desc
  81. *
  82. * - residue_stat: The residue in bytes of data we have covered
  83. * so far for accounting. This is updated directly to residue
  84. * during callbacks to keep it current.
  85. *
  86. * - sg_len: Tracks the length of the current intermediate transfer,
  87. * this is required to update the residue during intermediate transfer
  88. * completion callback.
  89. */
  90. int processed_stat;
  91. u32 sg_len;
  92. u32 residue;
  93. u32 residue_stat;
  94. struct edma_pset pset[0];
  95. };
  96. struct edma_cc;
  97. struct edma_chan {
  98. struct virt_dma_chan vchan;
  99. struct list_head node;
  100. struct edma_desc *edesc;
  101. struct edma_cc *ecc;
  102. int ch_num;
  103. bool alloced;
  104. int slot[EDMA_MAX_SLOTS];
  105. int missed;
  106. struct dma_slave_config cfg;
  107. };
  108. struct edma_cc {
  109. int ctlr;
  110. struct dma_device dma_slave;
  111. struct edma_chan slave_chans[EDMA_CHANS];
  112. int num_slave_chans;
  113. int dummy_slot;
  114. };
  115. static inline struct edma_cc *to_edma_cc(struct dma_device *d)
  116. {
  117. return container_of(d, struct edma_cc, dma_slave);
  118. }
  119. static inline struct edma_chan *to_edma_chan(struct dma_chan *c)
  120. {
  121. return container_of(c, struct edma_chan, vchan.chan);
  122. }
  123. static inline struct edma_desc
  124. *to_edma_desc(struct dma_async_tx_descriptor *tx)
  125. {
  126. return container_of(tx, struct edma_desc, vdesc.tx);
  127. }
  128. static void edma_desc_free(struct virt_dma_desc *vdesc)
  129. {
  130. kfree(container_of(vdesc, struct edma_desc, vdesc));
  131. }
  132. /* Dispatch a queued descriptor to the controller (caller holds lock) */
  133. static void edma_execute(struct edma_chan *echan)
  134. {
  135. struct virt_dma_desc *vdesc;
  136. struct edma_desc *edesc;
  137. struct device *dev = echan->vchan.chan.device->dev;
  138. int i, j, left, nslots;
  139. /* If either we processed all psets or we're still not started */
  140. if (!echan->edesc ||
  141. echan->edesc->pset_nr == echan->edesc->processed) {
  142. /* Get next vdesc */
  143. vdesc = vchan_next_desc(&echan->vchan);
  144. if (!vdesc) {
  145. echan->edesc = NULL;
  146. return;
  147. }
  148. list_del(&vdesc->node);
  149. echan->edesc = to_edma_desc(&vdesc->tx);
  150. }
  151. edesc = echan->edesc;
  152. /* Find out how many left */
  153. left = edesc->pset_nr - edesc->processed;
  154. nslots = min(MAX_NR_SG, left);
  155. edesc->sg_len = 0;
  156. /* Write descriptor PaRAM set(s) */
  157. for (i = 0; i < nslots; i++) {
  158. j = i + edesc->processed;
  159. edma_write_slot(echan->slot[i], &edesc->pset[j].param);
  160. edesc->sg_len += edesc->pset[j].len;
  161. dev_vdbg(echan->vchan.chan.device->dev,
  162. "\n pset[%d]:\n"
  163. " chnum\t%d\n"
  164. " slot\t%d\n"
  165. " opt\t%08x\n"
  166. " src\t%08x\n"
  167. " dst\t%08x\n"
  168. " abcnt\t%08x\n"
  169. " ccnt\t%08x\n"
  170. " bidx\t%08x\n"
  171. " cidx\t%08x\n"
  172. " lkrld\t%08x\n",
  173. j, echan->ch_num, echan->slot[i],
  174. edesc->pset[j].param.opt,
  175. edesc->pset[j].param.src,
  176. edesc->pset[j].param.dst,
  177. edesc->pset[j].param.a_b_cnt,
  178. edesc->pset[j].param.ccnt,
  179. edesc->pset[j].param.src_dst_bidx,
  180. edesc->pset[j].param.src_dst_cidx,
  181. edesc->pset[j].param.link_bcntrld);
  182. /* Link to the previous slot if not the last set */
  183. if (i != (nslots - 1))
  184. edma_link(echan->slot[i], echan->slot[i+1]);
  185. }
  186. edesc->processed += nslots;
  187. /*
  188. * If this is either the last set in a set of SG-list transactions
  189. * then setup a link to the dummy slot, this results in all future
  190. * events being absorbed and that's OK because we're done
  191. */
  192. if (edesc->processed == edesc->pset_nr) {
  193. if (edesc->cyclic)
  194. edma_link(echan->slot[nslots-1], echan->slot[1]);
  195. else
  196. edma_link(echan->slot[nslots-1],
  197. echan->ecc->dummy_slot);
  198. }
  199. if (edesc->processed <= MAX_NR_SG) {
  200. dev_dbg(dev, "first transfer starting on channel %d\n",
  201. echan->ch_num);
  202. edma_start(echan->ch_num);
  203. } else {
  204. dev_dbg(dev, "chan: %d: completed %d elements, resuming\n",
  205. echan->ch_num, edesc->processed);
  206. edma_resume(echan->ch_num);
  207. }
  208. /*
  209. * This happens due to setup times between intermediate transfers
  210. * in long SG lists which have to be broken up into transfers of
  211. * MAX_NR_SG
  212. */
  213. if (echan->missed) {
  214. dev_dbg(dev, "missed event on channel %d\n", echan->ch_num);
  215. edma_clean_channel(echan->ch_num);
  216. edma_stop(echan->ch_num);
  217. edma_start(echan->ch_num);
  218. edma_trigger_channel(echan->ch_num);
  219. echan->missed = 0;
  220. }
  221. }
  222. static int edma_terminate_all(struct dma_chan *chan)
  223. {
  224. struct edma_chan *echan = to_edma_chan(chan);
  225. unsigned long flags;
  226. LIST_HEAD(head);
  227. spin_lock_irqsave(&echan->vchan.lock, flags);
  228. /*
  229. * Stop DMA activity: we assume the callback will not be called
  230. * after edma_dma() returns (even if it does, it will see
  231. * echan->edesc is NULL and exit.)
  232. */
  233. if (echan->edesc) {
  234. int cyclic = echan->edesc->cyclic;
  235. echan->edesc = NULL;
  236. edma_stop(echan->ch_num);
  237. /* Move the cyclic channel back to default queue */
  238. if (cyclic)
  239. edma_assign_channel_eventq(echan->ch_num,
  240. EVENTQ_DEFAULT);
  241. }
  242. vchan_get_all_descriptors(&echan->vchan, &head);
  243. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  244. vchan_dma_desc_free_list(&echan->vchan, &head);
  245. return 0;
  246. }
  247. static int edma_slave_config(struct dma_chan *chan,
  248. struct dma_slave_config *cfg)
  249. {
  250. struct edma_chan *echan = to_edma_chan(chan);
  251. if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
  252. cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  253. return -EINVAL;
  254. memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
  255. return 0;
  256. }
  257. static int edma_dma_pause(struct dma_chan *chan)
  258. {
  259. struct edma_chan *echan = to_edma_chan(chan);
  260. /* Pause/Resume only allowed with cyclic mode */
  261. if (!echan->edesc || !echan->edesc->cyclic)
  262. return -EINVAL;
  263. edma_pause(echan->ch_num);
  264. return 0;
  265. }
  266. static int edma_dma_resume(struct dma_chan *chan)
  267. {
  268. struct edma_chan *echan = to_edma_chan(chan);
  269. /* Pause/Resume only allowed with cyclic mode */
  270. if (!echan->edesc->cyclic)
  271. return -EINVAL;
  272. edma_resume(echan->ch_num);
  273. return 0;
  274. }
  275. /*
  276. * A PaRAM set configuration abstraction used by other modes
  277. * @chan: Channel who's PaRAM set we're configuring
  278. * @pset: PaRAM set to initialize and setup.
  279. * @src_addr: Source address of the DMA
  280. * @dst_addr: Destination address of the DMA
  281. * @burst: In units of dev_width, how much to send
  282. * @dev_width: How much is the dev_width
  283. * @dma_length: Total length of the DMA transfer
  284. * @direction: Direction of the transfer
  285. */
  286. static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
  287. dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
  288. enum dma_slave_buswidth dev_width, unsigned int dma_length,
  289. enum dma_transfer_direction direction)
  290. {
  291. struct edma_chan *echan = to_edma_chan(chan);
  292. struct device *dev = chan->device->dev;
  293. struct edmacc_param *param = &epset->param;
  294. int acnt, bcnt, ccnt, cidx;
  295. int src_bidx, dst_bidx, src_cidx, dst_cidx;
  296. int absync;
  297. acnt = dev_width;
  298. /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
  299. if (!burst)
  300. burst = 1;
  301. /*
  302. * If the maxburst is equal to the fifo width, use
  303. * A-synced transfers. This allows for large contiguous
  304. * buffer transfers using only one PaRAM set.
  305. */
  306. if (burst == 1) {
  307. /*
  308. * For the A-sync case, bcnt and ccnt are the remainder
  309. * and quotient respectively of the division of:
  310. * (dma_length / acnt) by (SZ_64K -1). This is so
  311. * that in case bcnt over flows, we have ccnt to use.
  312. * Note: In A-sync tranfer only, bcntrld is used, but it
  313. * only applies for sg_dma_len(sg) >= SZ_64K.
  314. * In this case, the best way adopted is- bccnt for the
  315. * first frame will be the remainder below. Then for
  316. * every successive frame, bcnt will be SZ_64K-1. This
  317. * is assured as bcntrld = 0xffff in end of function.
  318. */
  319. absync = false;
  320. ccnt = dma_length / acnt / (SZ_64K - 1);
  321. bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
  322. /*
  323. * If bcnt is non-zero, we have a remainder and hence an
  324. * extra frame to transfer, so increment ccnt.
  325. */
  326. if (bcnt)
  327. ccnt++;
  328. else
  329. bcnt = SZ_64K - 1;
  330. cidx = acnt;
  331. } else {
  332. /*
  333. * If maxburst is greater than the fifo address_width,
  334. * use AB-synced transfers where A count is the fifo
  335. * address_width and B count is the maxburst. In this
  336. * case, we are limited to transfers of C count frames
  337. * of (address_width * maxburst) where C count is limited
  338. * to SZ_64K-1. This places an upper bound on the length
  339. * of an SG segment that can be handled.
  340. */
  341. absync = true;
  342. bcnt = burst;
  343. ccnt = dma_length / (acnt * bcnt);
  344. if (ccnt > (SZ_64K - 1)) {
  345. dev_err(dev, "Exceeded max SG segment size\n");
  346. return -EINVAL;
  347. }
  348. cidx = acnt * bcnt;
  349. }
  350. epset->len = dma_length;
  351. if (direction == DMA_MEM_TO_DEV) {
  352. src_bidx = acnt;
  353. src_cidx = cidx;
  354. dst_bidx = 0;
  355. dst_cidx = 0;
  356. epset->addr = src_addr;
  357. } else if (direction == DMA_DEV_TO_MEM) {
  358. src_bidx = 0;
  359. src_cidx = 0;
  360. dst_bidx = acnt;
  361. dst_cidx = cidx;
  362. epset->addr = dst_addr;
  363. } else if (direction == DMA_MEM_TO_MEM) {
  364. src_bidx = acnt;
  365. src_cidx = cidx;
  366. dst_bidx = acnt;
  367. dst_cidx = cidx;
  368. } else {
  369. dev_err(dev, "%s: direction not implemented yet\n", __func__);
  370. return -EINVAL;
  371. }
  372. param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
  373. /* Configure A or AB synchronized transfers */
  374. if (absync)
  375. param->opt |= SYNCDIM;
  376. param->src = src_addr;
  377. param->dst = dst_addr;
  378. param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
  379. param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
  380. param->a_b_cnt = bcnt << 16 | acnt;
  381. param->ccnt = ccnt;
  382. /*
  383. * Only time when (bcntrld) auto reload is required is for
  384. * A-sync case, and in this case, a requirement of reload value
  385. * of SZ_64K-1 only is assured. 'link' is initially set to NULL
  386. * and then later will be populated by edma_execute.
  387. */
  388. param->link_bcntrld = 0xffffffff;
  389. return absync;
  390. }
  391. static struct dma_async_tx_descriptor *edma_prep_slave_sg(
  392. struct dma_chan *chan, struct scatterlist *sgl,
  393. unsigned int sg_len, enum dma_transfer_direction direction,
  394. unsigned long tx_flags, void *context)
  395. {
  396. struct edma_chan *echan = to_edma_chan(chan);
  397. struct device *dev = chan->device->dev;
  398. struct edma_desc *edesc;
  399. dma_addr_t src_addr = 0, dst_addr = 0;
  400. enum dma_slave_buswidth dev_width;
  401. u32 burst;
  402. struct scatterlist *sg;
  403. int i, nslots, ret;
  404. if (unlikely(!echan || !sgl || !sg_len))
  405. return NULL;
  406. if (direction == DMA_DEV_TO_MEM) {
  407. src_addr = echan->cfg.src_addr;
  408. dev_width = echan->cfg.src_addr_width;
  409. burst = echan->cfg.src_maxburst;
  410. } else if (direction == DMA_MEM_TO_DEV) {
  411. dst_addr = echan->cfg.dst_addr;
  412. dev_width = echan->cfg.dst_addr_width;
  413. burst = echan->cfg.dst_maxburst;
  414. } else {
  415. dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
  416. return NULL;
  417. }
  418. if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  419. dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
  420. return NULL;
  421. }
  422. edesc = kzalloc(sizeof(*edesc) + sg_len *
  423. sizeof(edesc->pset[0]), GFP_ATOMIC);
  424. if (!edesc) {
  425. dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
  426. return NULL;
  427. }
  428. edesc->pset_nr = sg_len;
  429. edesc->residue = 0;
  430. edesc->direction = direction;
  431. edesc->echan = echan;
  432. /* Allocate a PaRAM slot, if needed */
  433. nslots = min_t(unsigned, MAX_NR_SG, sg_len);
  434. for (i = 0; i < nslots; i++) {
  435. if (echan->slot[i] < 0) {
  436. echan->slot[i] =
  437. edma_alloc_slot(EDMA_CTLR(echan->ch_num),
  438. EDMA_SLOT_ANY);
  439. if (echan->slot[i] < 0) {
  440. kfree(edesc);
  441. dev_err(dev, "%s: Failed to allocate slot\n",
  442. __func__);
  443. return NULL;
  444. }
  445. }
  446. }
  447. /* Configure PaRAM sets for each SG */
  448. for_each_sg(sgl, sg, sg_len, i) {
  449. /* Get address for each SG */
  450. if (direction == DMA_DEV_TO_MEM)
  451. dst_addr = sg_dma_address(sg);
  452. else
  453. src_addr = sg_dma_address(sg);
  454. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  455. dst_addr, burst, dev_width,
  456. sg_dma_len(sg), direction);
  457. if (ret < 0) {
  458. kfree(edesc);
  459. return NULL;
  460. }
  461. edesc->absync = ret;
  462. edesc->residue += sg_dma_len(sg);
  463. /* If this is the last in a current SG set of transactions,
  464. enable interrupts so that next set is processed */
  465. if (!((i+1) % MAX_NR_SG))
  466. edesc->pset[i].param.opt |= TCINTEN;
  467. /* If this is the last set, enable completion interrupt flag */
  468. if (i == sg_len - 1)
  469. edesc->pset[i].param.opt |= TCINTEN;
  470. }
  471. edesc->residue_stat = edesc->residue;
  472. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  473. }
  474. static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
  475. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  476. size_t len, unsigned long tx_flags)
  477. {
  478. int ret;
  479. struct edma_desc *edesc;
  480. struct device *dev = chan->device->dev;
  481. struct edma_chan *echan = to_edma_chan(chan);
  482. if (unlikely(!echan || !len))
  483. return NULL;
  484. edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
  485. if (!edesc) {
  486. dev_dbg(dev, "Failed to allocate a descriptor\n");
  487. return NULL;
  488. }
  489. edesc->pset_nr = 1;
  490. ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
  491. DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
  492. if (ret < 0)
  493. return NULL;
  494. edesc->absync = ret;
  495. /*
  496. * Enable intermediate transfer chaining to re-trigger channel
  497. * on completion of every TR, and enable transfer-completion
  498. * interrupt on completion of the whole transfer.
  499. */
  500. edesc->pset[0].param.opt |= ITCCHEN;
  501. edesc->pset[0].param.opt |= TCINTEN;
  502. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  503. }
  504. static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
  505. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  506. size_t period_len, enum dma_transfer_direction direction,
  507. unsigned long tx_flags)
  508. {
  509. struct edma_chan *echan = to_edma_chan(chan);
  510. struct device *dev = chan->device->dev;
  511. struct edma_desc *edesc;
  512. dma_addr_t src_addr, dst_addr;
  513. enum dma_slave_buswidth dev_width;
  514. u32 burst;
  515. int i, ret, nslots;
  516. if (unlikely(!echan || !buf_len || !period_len))
  517. return NULL;
  518. if (direction == DMA_DEV_TO_MEM) {
  519. src_addr = echan->cfg.src_addr;
  520. dst_addr = buf_addr;
  521. dev_width = echan->cfg.src_addr_width;
  522. burst = echan->cfg.src_maxburst;
  523. } else if (direction == DMA_MEM_TO_DEV) {
  524. src_addr = buf_addr;
  525. dst_addr = echan->cfg.dst_addr;
  526. dev_width = echan->cfg.dst_addr_width;
  527. burst = echan->cfg.dst_maxburst;
  528. } else {
  529. dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
  530. return NULL;
  531. }
  532. if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  533. dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
  534. return NULL;
  535. }
  536. if (unlikely(buf_len % period_len)) {
  537. dev_err(dev, "Period should be multiple of Buffer length\n");
  538. return NULL;
  539. }
  540. nslots = (buf_len / period_len) + 1;
  541. /*
  542. * Cyclic DMA users such as audio cannot tolerate delays introduced
  543. * by cases where the number of periods is more than the maximum
  544. * number of SGs the EDMA driver can handle at a time. For DMA types
  545. * such as Slave SGs, such delays are tolerable and synchronized,
  546. * but the synchronization is difficult to achieve with Cyclic and
  547. * cannot be guaranteed, so we error out early.
  548. */
  549. if (nslots > MAX_NR_SG)
  550. return NULL;
  551. edesc = kzalloc(sizeof(*edesc) + nslots *
  552. sizeof(edesc->pset[0]), GFP_ATOMIC);
  553. if (!edesc) {
  554. dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
  555. return NULL;
  556. }
  557. edesc->cyclic = 1;
  558. edesc->pset_nr = nslots;
  559. edesc->residue = edesc->residue_stat = buf_len;
  560. edesc->direction = direction;
  561. edesc->echan = echan;
  562. dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
  563. __func__, echan->ch_num, nslots, period_len, buf_len);
  564. for (i = 0; i < nslots; i++) {
  565. /* Allocate a PaRAM slot, if needed */
  566. if (echan->slot[i] < 0) {
  567. echan->slot[i] =
  568. edma_alloc_slot(EDMA_CTLR(echan->ch_num),
  569. EDMA_SLOT_ANY);
  570. if (echan->slot[i] < 0) {
  571. kfree(edesc);
  572. dev_err(dev, "%s: Failed to allocate slot\n",
  573. __func__);
  574. return NULL;
  575. }
  576. }
  577. if (i == nslots - 1) {
  578. memcpy(&edesc->pset[i], &edesc->pset[0],
  579. sizeof(edesc->pset[0]));
  580. break;
  581. }
  582. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  583. dst_addr, burst, dev_width, period_len,
  584. direction);
  585. if (ret < 0) {
  586. kfree(edesc);
  587. return NULL;
  588. }
  589. if (direction == DMA_DEV_TO_MEM)
  590. dst_addr += period_len;
  591. else
  592. src_addr += period_len;
  593. dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
  594. dev_vdbg(dev,
  595. "\n pset[%d]:\n"
  596. " chnum\t%d\n"
  597. " slot\t%d\n"
  598. " opt\t%08x\n"
  599. " src\t%08x\n"
  600. " dst\t%08x\n"
  601. " abcnt\t%08x\n"
  602. " ccnt\t%08x\n"
  603. " bidx\t%08x\n"
  604. " cidx\t%08x\n"
  605. " lkrld\t%08x\n",
  606. i, echan->ch_num, echan->slot[i],
  607. edesc->pset[i].param.opt,
  608. edesc->pset[i].param.src,
  609. edesc->pset[i].param.dst,
  610. edesc->pset[i].param.a_b_cnt,
  611. edesc->pset[i].param.ccnt,
  612. edesc->pset[i].param.src_dst_bidx,
  613. edesc->pset[i].param.src_dst_cidx,
  614. edesc->pset[i].param.link_bcntrld);
  615. edesc->absync = ret;
  616. /*
  617. * Enable period interrupt only if it is requested
  618. */
  619. if (tx_flags & DMA_PREP_INTERRUPT)
  620. edesc->pset[i].param.opt |= TCINTEN;
  621. }
  622. /* Place the cyclic channel to highest priority queue */
  623. edma_assign_channel_eventq(echan->ch_num, EVENTQ_0);
  624. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  625. }
  626. static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
  627. {
  628. struct edma_chan *echan = data;
  629. struct device *dev = echan->vchan.chan.device->dev;
  630. struct edma_desc *edesc;
  631. struct edmacc_param p;
  632. edesc = echan->edesc;
  633. /* Pause the channel for non-cyclic */
  634. if (!edesc || (edesc && !edesc->cyclic))
  635. edma_pause(echan->ch_num);
  636. switch (ch_status) {
  637. case EDMA_DMA_COMPLETE:
  638. spin_lock(&echan->vchan.lock);
  639. if (edesc) {
  640. if (edesc->cyclic) {
  641. vchan_cyclic_callback(&edesc->vdesc);
  642. } else if (edesc->processed == edesc->pset_nr) {
  643. dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
  644. edesc->residue = 0;
  645. edma_stop(echan->ch_num);
  646. vchan_cookie_complete(&edesc->vdesc);
  647. edma_execute(echan);
  648. } else {
  649. dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
  650. /* Update statistics for tx_status */
  651. edesc->residue -= edesc->sg_len;
  652. edesc->residue_stat = edesc->residue;
  653. edesc->processed_stat = edesc->processed;
  654. edma_execute(echan);
  655. }
  656. }
  657. spin_unlock(&echan->vchan.lock);
  658. break;
  659. case EDMA_DMA_CC_ERROR:
  660. spin_lock(&echan->vchan.lock);
  661. edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
  662. /*
  663. * Issue later based on missed flag which will be sure
  664. * to happen as:
  665. * (1) we finished transmitting an intermediate slot and
  666. * edma_execute is coming up.
  667. * (2) or we finished current transfer and issue will
  668. * call edma_execute.
  669. *
  670. * Important note: issuing can be dangerous here and
  671. * lead to some nasty recursion when we are in a NULL
  672. * slot. So we avoid doing so and set the missed flag.
  673. */
  674. if (p.a_b_cnt == 0 && p.ccnt == 0) {
  675. dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
  676. echan->missed = 1;
  677. } else {
  678. /*
  679. * The slot is already programmed but the event got
  680. * missed, so its safe to issue it here.
  681. */
  682. dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
  683. edma_clean_channel(echan->ch_num);
  684. edma_stop(echan->ch_num);
  685. edma_start(echan->ch_num);
  686. edma_trigger_channel(echan->ch_num);
  687. }
  688. spin_unlock(&echan->vchan.lock);
  689. break;
  690. default:
  691. break;
  692. }
  693. }
  694. /* Alloc channel resources */
  695. static int edma_alloc_chan_resources(struct dma_chan *chan)
  696. {
  697. struct edma_chan *echan = to_edma_chan(chan);
  698. struct device *dev = chan->device->dev;
  699. int ret;
  700. int a_ch_num;
  701. LIST_HEAD(descs);
  702. a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
  703. chan, EVENTQ_DEFAULT);
  704. if (a_ch_num < 0) {
  705. ret = -ENODEV;
  706. goto err_no_chan;
  707. }
  708. if (a_ch_num != echan->ch_num) {
  709. dev_err(dev, "failed to allocate requested channel %u:%u\n",
  710. EDMA_CTLR(echan->ch_num),
  711. EDMA_CHAN_SLOT(echan->ch_num));
  712. ret = -ENODEV;
  713. goto err_wrong_chan;
  714. }
  715. echan->alloced = true;
  716. echan->slot[0] = echan->ch_num;
  717. dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
  718. EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
  719. return 0;
  720. err_wrong_chan:
  721. edma_free_channel(a_ch_num);
  722. err_no_chan:
  723. return ret;
  724. }
  725. /* Free channel resources */
  726. static void edma_free_chan_resources(struct dma_chan *chan)
  727. {
  728. struct edma_chan *echan = to_edma_chan(chan);
  729. struct device *dev = chan->device->dev;
  730. int i;
  731. /* Terminate transfers */
  732. edma_stop(echan->ch_num);
  733. vchan_free_chan_resources(&echan->vchan);
  734. /* Free EDMA PaRAM slots */
  735. for (i = 1; i < EDMA_MAX_SLOTS; i++) {
  736. if (echan->slot[i] >= 0) {
  737. edma_free_slot(echan->slot[i]);
  738. echan->slot[i] = -1;
  739. }
  740. }
  741. /* Free EDMA channel */
  742. if (echan->alloced) {
  743. edma_free_channel(echan->ch_num);
  744. echan->alloced = false;
  745. }
  746. dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
  747. }
  748. /* Send pending descriptor to hardware */
  749. static void edma_issue_pending(struct dma_chan *chan)
  750. {
  751. struct edma_chan *echan = to_edma_chan(chan);
  752. unsigned long flags;
  753. spin_lock_irqsave(&echan->vchan.lock, flags);
  754. if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
  755. edma_execute(echan);
  756. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  757. }
  758. static u32 edma_residue(struct edma_desc *edesc)
  759. {
  760. bool dst = edesc->direction == DMA_DEV_TO_MEM;
  761. struct edma_pset *pset = edesc->pset;
  762. dma_addr_t done, pos;
  763. int i;
  764. /*
  765. * We always read the dst/src position from the first RamPar
  766. * pset. That's the one which is active now.
  767. */
  768. pos = edma_get_position(edesc->echan->slot[0], dst);
  769. /*
  770. * Cyclic is simple. Just subtract pset[0].addr from pos.
  771. *
  772. * We never update edesc->residue in the cyclic case, so we
  773. * can tell the remaining room to the end of the circular
  774. * buffer.
  775. */
  776. if (edesc->cyclic) {
  777. done = pos - pset->addr;
  778. edesc->residue_stat = edesc->residue - done;
  779. return edesc->residue_stat;
  780. }
  781. /*
  782. * For SG operation we catch up with the last processed
  783. * status.
  784. */
  785. pset += edesc->processed_stat;
  786. for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
  787. /*
  788. * If we are inside this pset address range, we know
  789. * this is the active one. Get the current delta and
  790. * stop walking the psets.
  791. */
  792. if (pos >= pset->addr && pos < pset->addr + pset->len)
  793. return edesc->residue_stat - (pos - pset->addr);
  794. /* Otherwise mark it done and update residue_stat. */
  795. edesc->processed_stat++;
  796. edesc->residue_stat -= pset->len;
  797. }
  798. return edesc->residue_stat;
  799. }
  800. /* Check request completion status */
  801. static enum dma_status edma_tx_status(struct dma_chan *chan,
  802. dma_cookie_t cookie,
  803. struct dma_tx_state *txstate)
  804. {
  805. struct edma_chan *echan = to_edma_chan(chan);
  806. struct virt_dma_desc *vdesc;
  807. enum dma_status ret;
  808. unsigned long flags;
  809. ret = dma_cookie_status(chan, cookie, txstate);
  810. if (ret == DMA_COMPLETE || !txstate)
  811. return ret;
  812. spin_lock_irqsave(&echan->vchan.lock, flags);
  813. if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
  814. txstate->residue = edma_residue(echan->edesc);
  815. else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
  816. txstate->residue = to_edma_desc(&vdesc->tx)->residue;
  817. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  818. return ret;
  819. }
  820. static void __init edma_chan_init(struct edma_cc *ecc,
  821. struct dma_device *dma,
  822. struct edma_chan *echans)
  823. {
  824. int i, j;
  825. for (i = 0; i < EDMA_CHANS; i++) {
  826. struct edma_chan *echan = &echans[i];
  827. echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
  828. echan->ecc = ecc;
  829. echan->vchan.desc_free = edma_desc_free;
  830. vchan_init(&echan->vchan, dma);
  831. INIT_LIST_HEAD(&echan->node);
  832. for (j = 0; j < EDMA_MAX_SLOTS; j++)
  833. echan->slot[j] = -1;
  834. }
  835. }
  836. #define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  837. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  838. BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
  839. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  840. static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
  841. struct device *dev)
  842. {
  843. dma->device_prep_slave_sg = edma_prep_slave_sg;
  844. dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
  845. dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
  846. dma->device_alloc_chan_resources = edma_alloc_chan_resources;
  847. dma->device_free_chan_resources = edma_free_chan_resources;
  848. dma->device_issue_pending = edma_issue_pending;
  849. dma->device_tx_status = edma_tx_status;
  850. dma->device_config = edma_slave_config;
  851. dma->device_pause = edma_dma_pause;
  852. dma->device_resume = edma_dma_resume;
  853. dma->device_terminate_all = edma_terminate_all;
  854. dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
  855. dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
  856. dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  857. dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  858. dma->dev = dev;
  859. /*
  860. * code using dma memcpy must make sure alignment of
  861. * length is at dma->copy_align boundary.
  862. */
  863. dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
  864. INIT_LIST_HEAD(&dma->channels);
  865. }
  866. static int edma_probe(struct platform_device *pdev)
  867. {
  868. struct edma_cc *ecc;
  869. int ret;
  870. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  871. if (ret)
  872. return ret;
  873. ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
  874. if (!ecc) {
  875. dev_err(&pdev->dev, "Can't allocate controller\n");
  876. return -ENOMEM;
  877. }
  878. ecc->ctlr = pdev->id;
  879. ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
  880. if (ecc->dummy_slot < 0) {
  881. dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
  882. return ecc->dummy_slot;
  883. }
  884. dma_cap_zero(ecc->dma_slave.cap_mask);
  885. dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
  886. dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
  887. dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
  888. edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
  889. edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
  890. ret = dma_async_device_register(&ecc->dma_slave);
  891. if (ret)
  892. goto err_reg1;
  893. platform_set_drvdata(pdev, ecc);
  894. dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
  895. return 0;
  896. err_reg1:
  897. edma_free_slot(ecc->dummy_slot);
  898. return ret;
  899. }
  900. static int edma_remove(struct platform_device *pdev)
  901. {
  902. struct device *dev = &pdev->dev;
  903. struct edma_cc *ecc = dev_get_drvdata(dev);
  904. dma_async_device_unregister(&ecc->dma_slave);
  905. edma_free_slot(ecc->dummy_slot);
  906. return 0;
  907. }
  908. static struct platform_driver edma_driver = {
  909. .probe = edma_probe,
  910. .remove = edma_remove,
  911. .driver = {
  912. .name = "edma-dma-engine",
  913. },
  914. };
  915. bool edma_filter_fn(struct dma_chan *chan, void *param)
  916. {
  917. if (chan->device->dev->driver == &edma_driver.driver) {
  918. struct edma_chan *echan = to_edma_chan(chan);
  919. unsigned ch_req = *(unsigned *)param;
  920. return ch_req == echan->ch_num;
  921. }
  922. return false;
  923. }
  924. EXPORT_SYMBOL(edma_filter_fn);
  925. static int edma_init(void)
  926. {
  927. return platform_driver_register(&edma_driver);
  928. }
  929. subsys_initcall(edma_init);
  930. static void __exit edma_exit(void)
  931. {
  932. platform_driver_unregister(&edma_driver);
  933. }
  934. module_exit(edma_exit);
  935. MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
  936. MODULE_DESCRIPTION("TI EDMA DMA engine driver");
  937. MODULE_LICENSE("GPL v2");