edma.c 29 KB

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