edma.c 25 KB

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