edma.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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. /*
  236. * free the running request descriptor
  237. * since it is not in any of the vdesc lists
  238. */
  239. edma_desc_free(&echan->edesc->vdesc);
  240. echan->edesc = NULL;
  241. edma_stop(echan->ch_num);
  242. /* Move the cyclic channel back to default queue */
  243. if (cyclic)
  244. edma_assign_channel_eventq(echan->ch_num,
  245. EVENTQ_DEFAULT);
  246. }
  247. vchan_get_all_descriptors(&echan->vchan, &head);
  248. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  249. vchan_dma_desc_free_list(&echan->vchan, &head);
  250. return 0;
  251. }
  252. static int edma_slave_config(struct dma_chan *chan,
  253. struct dma_slave_config *cfg)
  254. {
  255. struct edma_chan *echan = to_edma_chan(chan);
  256. if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
  257. cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  258. return -EINVAL;
  259. memcpy(&echan->cfg, cfg, sizeof(echan->cfg));
  260. return 0;
  261. }
  262. static int edma_dma_pause(struct dma_chan *chan)
  263. {
  264. struct edma_chan *echan = to_edma_chan(chan);
  265. if (!echan->edesc)
  266. return -EINVAL;
  267. edma_pause(echan->ch_num);
  268. return 0;
  269. }
  270. static int edma_dma_resume(struct dma_chan *chan)
  271. {
  272. struct edma_chan *echan = to_edma_chan(chan);
  273. edma_resume(echan->ch_num);
  274. return 0;
  275. }
  276. /*
  277. * A PaRAM set configuration abstraction used by other modes
  278. * @chan: Channel who's PaRAM set we're configuring
  279. * @pset: PaRAM set to initialize and setup.
  280. * @src_addr: Source address of the DMA
  281. * @dst_addr: Destination address of the DMA
  282. * @burst: In units of dev_width, how much to send
  283. * @dev_width: How much is the dev_width
  284. * @dma_length: Total length of the DMA transfer
  285. * @direction: Direction of the transfer
  286. */
  287. static int edma_config_pset(struct dma_chan *chan, struct edma_pset *epset,
  288. dma_addr_t src_addr, dma_addr_t dst_addr, u32 burst,
  289. enum dma_slave_buswidth dev_width, unsigned int dma_length,
  290. enum dma_transfer_direction direction)
  291. {
  292. struct edma_chan *echan = to_edma_chan(chan);
  293. struct device *dev = chan->device->dev;
  294. struct edmacc_param *param = &epset->param;
  295. int acnt, bcnt, ccnt, cidx;
  296. int src_bidx, dst_bidx, src_cidx, dst_cidx;
  297. int absync;
  298. acnt = dev_width;
  299. /* src/dst_maxburst == 0 is the same case as src/dst_maxburst == 1 */
  300. if (!burst)
  301. burst = 1;
  302. /*
  303. * If the maxburst is equal to the fifo width, use
  304. * A-synced transfers. This allows for large contiguous
  305. * buffer transfers using only one PaRAM set.
  306. */
  307. if (burst == 1) {
  308. /*
  309. * For the A-sync case, bcnt and ccnt are the remainder
  310. * and quotient respectively of the division of:
  311. * (dma_length / acnt) by (SZ_64K -1). This is so
  312. * that in case bcnt over flows, we have ccnt to use.
  313. * Note: In A-sync tranfer only, bcntrld is used, but it
  314. * only applies for sg_dma_len(sg) >= SZ_64K.
  315. * In this case, the best way adopted is- bccnt for the
  316. * first frame will be the remainder below. Then for
  317. * every successive frame, bcnt will be SZ_64K-1. This
  318. * is assured as bcntrld = 0xffff in end of function.
  319. */
  320. absync = false;
  321. ccnt = dma_length / acnt / (SZ_64K - 1);
  322. bcnt = dma_length / acnt - ccnt * (SZ_64K - 1);
  323. /*
  324. * If bcnt is non-zero, we have a remainder and hence an
  325. * extra frame to transfer, so increment ccnt.
  326. */
  327. if (bcnt)
  328. ccnt++;
  329. else
  330. bcnt = SZ_64K - 1;
  331. cidx = acnt;
  332. } else {
  333. /*
  334. * If maxburst is greater than the fifo address_width,
  335. * use AB-synced transfers where A count is the fifo
  336. * address_width and B count is the maxburst. In this
  337. * case, we are limited to transfers of C count frames
  338. * of (address_width * maxburst) where C count is limited
  339. * to SZ_64K-1. This places an upper bound on the length
  340. * of an SG segment that can be handled.
  341. */
  342. absync = true;
  343. bcnt = burst;
  344. ccnt = dma_length / (acnt * bcnt);
  345. if (ccnt > (SZ_64K - 1)) {
  346. dev_err(dev, "Exceeded max SG segment size\n");
  347. return -EINVAL;
  348. }
  349. cidx = acnt * bcnt;
  350. }
  351. epset->len = dma_length;
  352. if (direction == DMA_MEM_TO_DEV) {
  353. src_bidx = acnt;
  354. src_cidx = cidx;
  355. dst_bidx = 0;
  356. dst_cidx = 0;
  357. epset->addr = src_addr;
  358. } else if (direction == DMA_DEV_TO_MEM) {
  359. src_bidx = 0;
  360. src_cidx = 0;
  361. dst_bidx = acnt;
  362. dst_cidx = cidx;
  363. epset->addr = dst_addr;
  364. } else if (direction == DMA_MEM_TO_MEM) {
  365. src_bidx = acnt;
  366. src_cidx = cidx;
  367. dst_bidx = acnt;
  368. dst_cidx = cidx;
  369. } else {
  370. dev_err(dev, "%s: direction not implemented yet\n", __func__);
  371. return -EINVAL;
  372. }
  373. param->opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
  374. /* Configure A or AB synchronized transfers */
  375. if (absync)
  376. param->opt |= SYNCDIM;
  377. param->src = src_addr;
  378. param->dst = dst_addr;
  379. param->src_dst_bidx = (dst_bidx << 16) | src_bidx;
  380. param->src_dst_cidx = (dst_cidx << 16) | src_cidx;
  381. param->a_b_cnt = bcnt << 16 | acnt;
  382. param->ccnt = ccnt;
  383. /*
  384. * Only time when (bcntrld) auto reload is required is for
  385. * A-sync case, and in this case, a requirement of reload value
  386. * of SZ_64K-1 only is assured. 'link' is initially set to NULL
  387. * and then later will be populated by edma_execute.
  388. */
  389. param->link_bcntrld = 0xffffffff;
  390. return absync;
  391. }
  392. static struct dma_async_tx_descriptor *edma_prep_slave_sg(
  393. struct dma_chan *chan, struct scatterlist *sgl,
  394. unsigned int sg_len, enum dma_transfer_direction direction,
  395. unsigned long tx_flags, void *context)
  396. {
  397. struct edma_chan *echan = to_edma_chan(chan);
  398. struct device *dev = chan->device->dev;
  399. struct edma_desc *edesc;
  400. dma_addr_t src_addr = 0, dst_addr = 0;
  401. enum dma_slave_buswidth dev_width;
  402. u32 burst;
  403. struct scatterlist *sg;
  404. int i, nslots, ret;
  405. if (unlikely(!echan || !sgl || !sg_len))
  406. return NULL;
  407. if (direction == DMA_DEV_TO_MEM) {
  408. src_addr = echan->cfg.src_addr;
  409. dev_width = echan->cfg.src_addr_width;
  410. burst = echan->cfg.src_maxburst;
  411. } else if (direction == DMA_MEM_TO_DEV) {
  412. dst_addr = echan->cfg.dst_addr;
  413. dev_width = echan->cfg.dst_addr_width;
  414. burst = echan->cfg.dst_maxburst;
  415. } else {
  416. dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
  417. return NULL;
  418. }
  419. if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  420. dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
  421. return NULL;
  422. }
  423. edesc = kzalloc(sizeof(*edesc) + sg_len *
  424. sizeof(edesc->pset[0]), GFP_ATOMIC);
  425. if (!edesc) {
  426. dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
  427. return NULL;
  428. }
  429. edesc->pset_nr = sg_len;
  430. edesc->residue = 0;
  431. edesc->direction = direction;
  432. edesc->echan = echan;
  433. /* Allocate a PaRAM slot, if needed */
  434. nslots = min_t(unsigned, MAX_NR_SG, sg_len);
  435. for (i = 0; i < nslots; i++) {
  436. if (echan->slot[i] < 0) {
  437. echan->slot[i] =
  438. edma_alloc_slot(EDMA_CTLR(echan->ch_num),
  439. EDMA_SLOT_ANY);
  440. if (echan->slot[i] < 0) {
  441. kfree(edesc);
  442. dev_err(dev, "%s: Failed to allocate slot\n",
  443. __func__);
  444. return NULL;
  445. }
  446. }
  447. }
  448. /* Configure PaRAM sets for each SG */
  449. for_each_sg(sgl, sg, sg_len, i) {
  450. /* Get address for each SG */
  451. if (direction == DMA_DEV_TO_MEM)
  452. dst_addr = sg_dma_address(sg);
  453. else
  454. src_addr = sg_dma_address(sg);
  455. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  456. dst_addr, burst, dev_width,
  457. sg_dma_len(sg), direction);
  458. if (ret < 0) {
  459. kfree(edesc);
  460. return NULL;
  461. }
  462. edesc->absync = ret;
  463. edesc->residue += sg_dma_len(sg);
  464. /* If this is the last in a current SG set of transactions,
  465. enable interrupts so that next set is processed */
  466. if (!((i+1) % MAX_NR_SG))
  467. edesc->pset[i].param.opt |= TCINTEN;
  468. /* If this is the last set, enable completion interrupt flag */
  469. if (i == sg_len - 1)
  470. edesc->pset[i].param.opt |= TCINTEN;
  471. }
  472. edesc->residue_stat = edesc->residue;
  473. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  474. }
  475. static struct dma_async_tx_descriptor *edma_prep_dma_memcpy(
  476. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  477. size_t len, unsigned long tx_flags)
  478. {
  479. int ret;
  480. struct edma_desc *edesc;
  481. struct device *dev = chan->device->dev;
  482. struct edma_chan *echan = to_edma_chan(chan);
  483. if (unlikely(!echan || !len))
  484. return NULL;
  485. edesc = kzalloc(sizeof(*edesc) + sizeof(edesc->pset[0]), GFP_ATOMIC);
  486. if (!edesc) {
  487. dev_dbg(dev, "Failed to allocate a descriptor\n");
  488. return NULL;
  489. }
  490. edesc->pset_nr = 1;
  491. ret = edma_config_pset(chan, &edesc->pset[0], src, dest, 1,
  492. DMA_SLAVE_BUSWIDTH_4_BYTES, len, DMA_MEM_TO_MEM);
  493. if (ret < 0)
  494. return NULL;
  495. edesc->absync = ret;
  496. /*
  497. * Enable intermediate transfer chaining to re-trigger channel
  498. * on completion of every TR, and enable transfer-completion
  499. * interrupt on completion of the whole transfer.
  500. */
  501. edesc->pset[0].param.opt |= ITCCHEN;
  502. edesc->pset[0].param.opt |= TCINTEN;
  503. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  504. }
  505. static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
  506. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  507. size_t period_len, enum dma_transfer_direction direction,
  508. unsigned long tx_flags)
  509. {
  510. struct edma_chan *echan = to_edma_chan(chan);
  511. struct device *dev = chan->device->dev;
  512. struct edma_desc *edesc;
  513. dma_addr_t src_addr, dst_addr;
  514. enum dma_slave_buswidth dev_width;
  515. u32 burst;
  516. int i, ret, nslots;
  517. if (unlikely(!echan || !buf_len || !period_len))
  518. return NULL;
  519. if (direction == DMA_DEV_TO_MEM) {
  520. src_addr = echan->cfg.src_addr;
  521. dst_addr = buf_addr;
  522. dev_width = echan->cfg.src_addr_width;
  523. burst = echan->cfg.src_maxburst;
  524. } else if (direction == DMA_MEM_TO_DEV) {
  525. src_addr = buf_addr;
  526. dst_addr = echan->cfg.dst_addr;
  527. dev_width = echan->cfg.dst_addr_width;
  528. burst = echan->cfg.dst_maxburst;
  529. } else {
  530. dev_err(dev, "%s: bad direction: %d\n", __func__, direction);
  531. return NULL;
  532. }
  533. if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  534. dev_err(dev, "%s: Undefined slave buswidth\n", __func__);
  535. return NULL;
  536. }
  537. if (unlikely(buf_len % period_len)) {
  538. dev_err(dev, "Period should be multiple of Buffer length\n");
  539. return NULL;
  540. }
  541. nslots = (buf_len / period_len) + 1;
  542. /*
  543. * Cyclic DMA users such as audio cannot tolerate delays introduced
  544. * by cases where the number of periods is more than the maximum
  545. * number of SGs the EDMA driver can handle at a time. For DMA types
  546. * such as Slave SGs, such delays are tolerable and synchronized,
  547. * but the synchronization is difficult to achieve with Cyclic and
  548. * cannot be guaranteed, so we error out early.
  549. */
  550. if (nslots > MAX_NR_SG)
  551. return NULL;
  552. edesc = kzalloc(sizeof(*edesc) + nslots *
  553. sizeof(edesc->pset[0]), GFP_ATOMIC);
  554. if (!edesc) {
  555. dev_err(dev, "%s: Failed to allocate a descriptor\n", __func__);
  556. return NULL;
  557. }
  558. edesc->cyclic = 1;
  559. edesc->pset_nr = nslots;
  560. edesc->residue = edesc->residue_stat = buf_len;
  561. edesc->direction = direction;
  562. edesc->echan = echan;
  563. dev_dbg(dev, "%s: channel=%d nslots=%d period_len=%zu buf_len=%zu\n",
  564. __func__, echan->ch_num, nslots, period_len, buf_len);
  565. for (i = 0; i < nslots; i++) {
  566. /* Allocate a PaRAM slot, if needed */
  567. if (echan->slot[i] < 0) {
  568. echan->slot[i] =
  569. edma_alloc_slot(EDMA_CTLR(echan->ch_num),
  570. EDMA_SLOT_ANY);
  571. if (echan->slot[i] < 0) {
  572. kfree(edesc);
  573. dev_err(dev, "%s: Failed to allocate slot\n",
  574. __func__);
  575. return NULL;
  576. }
  577. }
  578. if (i == nslots - 1) {
  579. memcpy(&edesc->pset[i], &edesc->pset[0],
  580. sizeof(edesc->pset[0]));
  581. break;
  582. }
  583. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  584. dst_addr, burst, dev_width, period_len,
  585. direction);
  586. if (ret < 0) {
  587. kfree(edesc);
  588. return NULL;
  589. }
  590. if (direction == DMA_DEV_TO_MEM)
  591. dst_addr += period_len;
  592. else
  593. src_addr += period_len;
  594. dev_vdbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
  595. dev_vdbg(dev,
  596. "\n pset[%d]:\n"
  597. " chnum\t%d\n"
  598. " slot\t%d\n"
  599. " opt\t%08x\n"
  600. " src\t%08x\n"
  601. " dst\t%08x\n"
  602. " abcnt\t%08x\n"
  603. " ccnt\t%08x\n"
  604. " bidx\t%08x\n"
  605. " cidx\t%08x\n"
  606. " lkrld\t%08x\n",
  607. i, echan->ch_num, echan->slot[i],
  608. edesc->pset[i].param.opt,
  609. edesc->pset[i].param.src,
  610. edesc->pset[i].param.dst,
  611. edesc->pset[i].param.a_b_cnt,
  612. edesc->pset[i].param.ccnt,
  613. edesc->pset[i].param.src_dst_bidx,
  614. edesc->pset[i].param.src_dst_cidx,
  615. edesc->pset[i].param.link_bcntrld);
  616. edesc->absync = ret;
  617. /*
  618. * Enable period interrupt only if it is requested
  619. */
  620. if (tx_flags & DMA_PREP_INTERRUPT)
  621. edesc->pset[i].param.opt |= TCINTEN;
  622. }
  623. /* Place the cyclic channel to highest priority queue */
  624. edma_assign_channel_eventq(echan->ch_num, EVENTQ_0);
  625. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  626. }
  627. static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
  628. {
  629. struct edma_chan *echan = data;
  630. struct device *dev = echan->vchan.chan.device->dev;
  631. struct edma_desc *edesc;
  632. struct edmacc_param p;
  633. edesc = echan->edesc;
  634. /* Pause the channel for non-cyclic */
  635. if (!edesc || (edesc && !edesc->cyclic))
  636. edma_pause(echan->ch_num);
  637. switch (ch_status) {
  638. case EDMA_DMA_COMPLETE:
  639. spin_lock(&echan->vchan.lock);
  640. if (edesc) {
  641. if (edesc->cyclic) {
  642. vchan_cyclic_callback(&edesc->vdesc);
  643. } else if (edesc->processed == edesc->pset_nr) {
  644. dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
  645. edesc->residue = 0;
  646. edma_stop(echan->ch_num);
  647. vchan_cookie_complete(&edesc->vdesc);
  648. edma_execute(echan);
  649. } else {
  650. dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
  651. /* Update statistics for tx_status */
  652. edesc->residue -= edesc->sg_len;
  653. edesc->residue_stat = edesc->residue;
  654. edesc->processed_stat = edesc->processed;
  655. edma_execute(echan);
  656. }
  657. }
  658. spin_unlock(&echan->vchan.lock);
  659. break;
  660. case EDMA_DMA_CC_ERROR:
  661. spin_lock(&echan->vchan.lock);
  662. edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
  663. /*
  664. * Issue later based on missed flag which will be sure
  665. * to happen as:
  666. * (1) we finished transmitting an intermediate slot and
  667. * edma_execute is coming up.
  668. * (2) or we finished current transfer and issue will
  669. * call edma_execute.
  670. *
  671. * Important note: issuing can be dangerous here and
  672. * lead to some nasty recursion when we are in a NULL
  673. * slot. So we avoid doing so and set the missed flag.
  674. */
  675. if (p.a_b_cnt == 0 && p.ccnt == 0) {
  676. dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
  677. echan->missed = 1;
  678. } else {
  679. /*
  680. * The slot is already programmed but the event got
  681. * missed, so its safe to issue it here.
  682. */
  683. dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
  684. edma_clean_channel(echan->ch_num);
  685. edma_stop(echan->ch_num);
  686. edma_start(echan->ch_num);
  687. edma_trigger_channel(echan->ch_num);
  688. }
  689. spin_unlock(&echan->vchan.lock);
  690. break;
  691. default:
  692. break;
  693. }
  694. }
  695. /* Alloc channel resources */
  696. static int edma_alloc_chan_resources(struct dma_chan *chan)
  697. {
  698. struct edma_chan *echan = to_edma_chan(chan);
  699. struct device *dev = chan->device->dev;
  700. int ret;
  701. int a_ch_num;
  702. LIST_HEAD(descs);
  703. a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
  704. echan, EVENTQ_DEFAULT);
  705. if (a_ch_num < 0) {
  706. ret = -ENODEV;
  707. goto err_no_chan;
  708. }
  709. if (a_ch_num != echan->ch_num) {
  710. dev_err(dev, "failed to allocate requested channel %u:%u\n",
  711. EDMA_CTLR(echan->ch_num),
  712. EDMA_CHAN_SLOT(echan->ch_num));
  713. ret = -ENODEV;
  714. goto err_wrong_chan;
  715. }
  716. echan->alloced = true;
  717. echan->slot[0] = echan->ch_num;
  718. dev_dbg(dev, "allocated channel %d for %u:%u\n", echan->ch_num,
  719. EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
  720. return 0;
  721. err_wrong_chan:
  722. edma_free_channel(a_ch_num);
  723. err_no_chan:
  724. return ret;
  725. }
  726. /* Free channel resources */
  727. static void edma_free_chan_resources(struct dma_chan *chan)
  728. {
  729. struct edma_chan *echan = to_edma_chan(chan);
  730. struct device *dev = chan->device->dev;
  731. int i;
  732. /* Terminate transfers */
  733. edma_stop(echan->ch_num);
  734. vchan_free_chan_resources(&echan->vchan);
  735. /* Free EDMA PaRAM slots */
  736. for (i = 1; i < EDMA_MAX_SLOTS; i++) {
  737. if (echan->slot[i] >= 0) {
  738. edma_free_slot(echan->slot[i]);
  739. echan->slot[i] = -1;
  740. }
  741. }
  742. /* Free EDMA channel */
  743. if (echan->alloced) {
  744. edma_free_channel(echan->ch_num);
  745. echan->alloced = false;
  746. }
  747. dev_dbg(dev, "freeing channel for %u\n", echan->ch_num);
  748. }
  749. /* Send pending descriptor to hardware */
  750. static void edma_issue_pending(struct dma_chan *chan)
  751. {
  752. struct edma_chan *echan = to_edma_chan(chan);
  753. unsigned long flags;
  754. spin_lock_irqsave(&echan->vchan.lock, flags);
  755. if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
  756. edma_execute(echan);
  757. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  758. }
  759. static u32 edma_residue(struct edma_desc *edesc)
  760. {
  761. bool dst = edesc->direction == DMA_DEV_TO_MEM;
  762. struct edma_pset *pset = edesc->pset;
  763. dma_addr_t done, pos;
  764. int i;
  765. /*
  766. * We always read the dst/src position from the first RamPar
  767. * pset. That's the one which is active now.
  768. */
  769. pos = edma_get_position(edesc->echan->slot[0], dst);
  770. /*
  771. * Cyclic is simple. Just subtract pset[0].addr from pos.
  772. *
  773. * We never update edesc->residue in the cyclic case, so we
  774. * can tell the remaining room to the end of the circular
  775. * buffer.
  776. */
  777. if (edesc->cyclic) {
  778. done = pos - pset->addr;
  779. edesc->residue_stat = edesc->residue - done;
  780. return edesc->residue_stat;
  781. }
  782. /*
  783. * For SG operation we catch up with the last processed
  784. * status.
  785. */
  786. pset += edesc->processed_stat;
  787. for (i = edesc->processed_stat; i < edesc->processed; i++, pset++) {
  788. /*
  789. * If we are inside this pset address range, we know
  790. * this is the active one. Get the current delta and
  791. * stop walking the psets.
  792. */
  793. if (pos >= pset->addr && pos < pset->addr + pset->len)
  794. return edesc->residue_stat - (pos - pset->addr);
  795. /* Otherwise mark it done and update residue_stat. */
  796. edesc->processed_stat++;
  797. edesc->residue_stat -= pset->len;
  798. }
  799. return edesc->residue_stat;
  800. }
  801. /* Check request completion status */
  802. static enum dma_status edma_tx_status(struct dma_chan *chan,
  803. dma_cookie_t cookie,
  804. struct dma_tx_state *txstate)
  805. {
  806. struct edma_chan *echan = to_edma_chan(chan);
  807. struct virt_dma_desc *vdesc;
  808. enum dma_status ret;
  809. unsigned long flags;
  810. ret = dma_cookie_status(chan, cookie, txstate);
  811. if (ret == DMA_COMPLETE || !txstate)
  812. return ret;
  813. spin_lock_irqsave(&echan->vchan.lock, flags);
  814. if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie)
  815. txstate->residue = edma_residue(echan->edesc);
  816. else if ((vdesc = vchan_find_desc(&echan->vchan, cookie)))
  817. txstate->residue = to_edma_desc(&vdesc->tx)->residue;
  818. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  819. return ret;
  820. }
  821. static void __init edma_chan_init(struct edma_cc *ecc,
  822. struct dma_device *dma,
  823. struct edma_chan *echans)
  824. {
  825. int i, j;
  826. for (i = 0; i < EDMA_CHANS; i++) {
  827. struct edma_chan *echan = &echans[i];
  828. echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
  829. echan->ecc = ecc;
  830. echan->vchan.desc_free = edma_desc_free;
  831. vchan_init(&echan->vchan, dma);
  832. INIT_LIST_HEAD(&echan->node);
  833. for (j = 0; j < EDMA_MAX_SLOTS; j++)
  834. echan->slot[j] = -1;
  835. }
  836. }
  837. #define EDMA_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  838. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  839. BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
  840. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  841. static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
  842. struct device *dev)
  843. {
  844. dma->device_prep_slave_sg = edma_prep_slave_sg;
  845. dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
  846. dma->device_prep_dma_memcpy = edma_prep_dma_memcpy;
  847. dma->device_alloc_chan_resources = edma_alloc_chan_resources;
  848. dma->device_free_chan_resources = edma_free_chan_resources;
  849. dma->device_issue_pending = edma_issue_pending;
  850. dma->device_tx_status = edma_tx_status;
  851. dma->device_config = edma_slave_config;
  852. dma->device_pause = edma_dma_pause;
  853. dma->device_resume = edma_dma_resume;
  854. dma->device_terminate_all = edma_terminate_all;
  855. dma->src_addr_widths = EDMA_DMA_BUSWIDTHS;
  856. dma->dst_addr_widths = EDMA_DMA_BUSWIDTHS;
  857. dma->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  858. dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  859. dma->dev = dev;
  860. /*
  861. * code using dma memcpy must make sure alignment of
  862. * length is at dma->copy_align boundary.
  863. */
  864. dma->copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
  865. INIT_LIST_HEAD(&dma->channels);
  866. }
  867. static int edma_probe(struct platform_device *pdev)
  868. {
  869. struct edma_cc *ecc;
  870. int ret;
  871. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  872. if (ret)
  873. return ret;
  874. ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
  875. if (!ecc) {
  876. dev_err(&pdev->dev, "Can't allocate controller\n");
  877. return -ENOMEM;
  878. }
  879. ecc->ctlr = pdev->id;
  880. ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
  881. if (ecc->dummy_slot < 0) {
  882. dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
  883. return ecc->dummy_slot;
  884. }
  885. dma_cap_zero(ecc->dma_slave.cap_mask);
  886. dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
  887. dma_cap_set(DMA_CYCLIC, ecc->dma_slave.cap_mask);
  888. dma_cap_set(DMA_MEMCPY, ecc->dma_slave.cap_mask);
  889. edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
  890. edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
  891. ret = dma_async_device_register(&ecc->dma_slave);
  892. if (ret)
  893. goto err_reg1;
  894. platform_set_drvdata(pdev, ecc);
  895. dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
  896. return 0;
  897. err_reg1:
  898. edma_free_slot(ecc->dummy_slot);
  899. return ret;
  900. }
  901. static int edma_remove(struct platform_device *pdev)
  902. {
  903. struct device *dev = &pdev->dev;
  904. struct edma_cc *ecc = dev_get_drvdata(dev);
  905. dma_async_device_unregister(&ecc->dma_slave);
  906. edma_free_slot(ecc->dummy_slot);
  907. return 0;
  908. }
  909. static struct platform_driver edma_driver = {
  910. .probe = edma_probe,
  911. .remove = edma_remove,
  912. .driver = {
  913. .name = "edma-dma-engine",
  914. },
  915. };
  916. bool edma_filter_fn(struct dma_chan *chan, void *param)
  917. {
  918. if (chan->device->dev->driver == &edma_driver.driver) {
  919. struct edma_chan *echan = to_edma_chan(chan);
  920. unsigned ch_req = *(unsigned *)param;
  921. return ch_req == echan->ch_num;
  922. }
  923. return false;
  924. }
  925. EXPORT_SYMBOL(edma_filter_fn);
  926. static int edma_init(void)
  927. {
  928. return platform_driver_register(&edma_driver);
  929. }
  930. subsys_initcall(edma_init);
  931. static void __exit edma_exit(void)
  932. {
  933. platform_driver_unregister(&edma_driver);
  934. }
  935. module_exit(edma_exit);
  936. MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
  937. MODULE_DESCRIPTION("TI EDMA DMA engine driver");
  938. MODULE_LICENSE("GPL v2");