edma.c 24 KB

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