edma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. return NULL;
  383. }
  384. }
  385. }
  386. /* Configure PaRAM sets for each SG */
  387. for_each_sg(sgl, sg, sg_len, i) {
  388. /* Get address for each SG */
  389. if (direction == DMA_DEV_TO_MEM)
  390. dst_addr = sg_dma_address(sg);
  391. else
  392. src_addr = sg_dma_address(sg);
  393. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  394. dst_addr, burst, dev_width,
  395. sg_dma_len(sg), direction);
  396. if (ret < 0) {
  397. kfree(edesc);
  398. return NULL;
  399. }
  400. edesc->absync = ret;
  401. /* If this is the last in a current SG set of transactions,
  402. enable interrupts so that next set is processed */
  403. if (!((i+1) % MAX_NR_SG))
  404. edesc->pset[i].opt |= TCINTEN;
  405. /* If this is the last set, enable completion interrupt flag */
  406. if (i == sg_len - 1)
  407. edesc->pset[i].opt |= TCINTEN;
  408. }
  409. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  410. }
  411. static struct dma_async_tx_descriptor *edma_prep_dma_cyclic(
  412. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  413. size_t period_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, dst_addr;
  420. enum dma_slave_buswidth dev_width;
  421. u32 burst;
  422. int i, ret, nslots;
  423. if (unlikely(!echan || !buf_len || !period_len))
  424. return NULL;
  425. if (direction == DMA_DEV_TO_MEM) {
  426. src_addr = echan->cfg.src_addr;
  427. dst_addr = buf_addr;
  428. dev_width = echan->cfg.src_addr_width;
  429. burst = echan->cfg.src_maxburst;
  430. } else if (direction == DMA_MEM_TO_DEV) {
  431. src_addr = buf_addr;
  432. dst_addr = echan->cfg.dst_addr;
  433. dev_width = echan->cfg.dst_addr_width;
  434. burst = echan->cfg.dst_maxburst;
  435. } else {
  436. dev_err(dev, "%s: bad direction?\n", __func__);
  437. return NULL;
  438. }
  439. if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) {
  440. dev_err(dev, "Undefined slave buswidth\n");
  441. return NULL;
  442. }
  443. if (unlikely(buf_len % period_len)) {
  444. dev_err(dev, "Period should be multiple of Buffer length\n");
  445. return NULL;
  446. }
  447. nslots = (buf_len / period_len) + 1;
  448. /*
  449. * Cyclic DMA users such as audio cannot tolerate delays introduced
  450. * by cases where the number of periods is more than the maximum
  451. * number of SGs the EDMA driver can handle at a time. For DMA types
  452. * such as Slave SGs, such delays are tolerable and synchronized,
  453. * but the synchronization is difficult to achieve with Cyclic and
  454. * cannot be guaranteed, so we error out early.
  455. */
  456. if (nslots > MAX_NR_SG)
  457. return NULL;
  458. edesc = kzalloc(sizeof(*edesc) + nslots *
  459. sizeof(edesc->pset[0]), GFP_ATOMIC);
  460. if (!edesc) {
  461. dev_dbg(dev, "Failed to allocate a descriptor\n");
  462. return NULL;
  463. }
  464. edesc->cyclic = 1;
  465. edesc->pset_nr = nslots;
  466. dev_dbg(dev, "%s: nslots=%d\n", __func__, nslots);
  467. dev_dbg(dev, "%s: period_len=%d\n", __func__, period_len);
  468. dev_dbg(dev, "%s: buf_len=%d\n", __func__, buf_len);
  469. for (i = 0; i < nslots; i++) {
  470. /* Allocate a PaRAM slot, if needed */
  471. if (echan->slot[i] < 0) {
  472. echan->slot[i] =
  473. edma_alloc_slot(EDMA_CTLR(echan->ch_num),
  474. EDMA_SLOT_ANY);
  475. if (echan->slot[i] < 0) {
  476. dev_err(dev, "Failed to allocate slot\n");
  477. return NULL;
  478. }
  479. }
  480. if (i == nslots - 1) {
  481. memcpy(&edesc->pset[i], &edesc->pset[0],
  482. sizeof(edesc->pset[0]));
  483. break;
  484. }
  485. ret = edma_config_pset(chan, &edesc->pset[i], src_addr,
  486. dst_addr, burst, dev_width, period_len,
  487. direction);
  488. if (ret < 0)
  489. return NULL;
  490. if (direction == DMA_DEV_TO_MEM)
  491. dst_addr += period_len;
  492. else
  493. src_addr += period_len;
  494. dev_dbg(dev, "%s: Configure period %d of buf:\n", __func__, i);
  495. dev_dbg(dev,
  496. "\n pset[%d]:\n"
  497. " chnum\t%d\n"
  498. " slot\t%d\n"
  499. " opt\t%08x\n"
  500. " src\t%08x\n"
  501. " dst\t%08x\n"
  502. " abcnt\t%08x\n"
  503. " ccnt\t%08x\n"
  504. " bidx\t%08x\n"
  505. " cidx\t%08x\n"
  506. " lkrld\t%08x\n",
  507. i, echan->ch_num, echan->slot[i],
  508. edesc->pset[i].opt,
  509. edesc->pset[i].src,
  510. edesc->pset[i].dst,
  511. edesc->pset[i].a_b_cnt,
  512. edesc->pset[i].ccnt,
  513. edesc->pset[i].src_dst_bidx,
  514. edesc->pset[i].src_dst_cidx,
  515. edesc->pset[i].link_bcntrld);
  516. edesc->absync = ret;
  517. /*
  518. * Enable interrupts for every period because callback
  519. * has to be called for every period.
  520. */
  521. edesc->pset[i].opt |= TCINTEN;
  522. }
  523. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  524. }
  525. static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
  526. {
  527. struct edma_chan *echan = data;
  528. struct device *dev = echan->vchan.chan.device->dev;
  529. struct edma_desc *edesc;
  530. unsigned long flags;
  531. struct edmacc_param p;
  532. edesc = echan->edesc;
  533. /* Pause the channel for non-cyclic */
  534. if (!edesc || (edesc && !edesc->cyclic))
  535. edma_pause(echan->ch_num);
  536. switch (ch_status) {
  537. case EDMA_DMA_COMPLETE:
  538. spin_lock_irqsave(&echan->vchan.lock, flags);
  539. if (edesc) {
  540. if (edesc->cyclic) {
  541. vchan_cyclic_callback(&edesc->vdesc);
  542. } else if (edesc->processed == edesc->pset_nr) {
  543. dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
  544. edma_stop(echan->ch_num);
  545. vchan_cookie_complete(&edesc->vdesc);
  546. edma_execute(echan);
  547. } else {
  548. dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
  549. edma_execute(echan);
  550. }
  551. }
  552. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  553. break;
  554. case EDMA_DMA_CC_ERROR:
  555. spin_lock_irqsave(&echan->vchan.lock, flags);
  556. edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
  557. /*
  558. * Issue later based on missed flag which will be sure
  559. * to happen as:
  560. * (1) we finished transmitting an intermediate slot and
  561. * edma_execute is coming up.
  562. * (2) or we finished current transfer and issue will
  563. * call edma_execute.
  564. *
  565. * Important note: issuing can be dangerous here and
  566. * lead to some nasty recursion when we are in a NULL
  567. * slot. So we avoid doing so and set the missed flag.
  568. */
  569. if (p.a_b_cnt == 0 && p.ccnt == 0) {
  570. dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
  571. echan->missed = 1;
  572. } else {
  573. /*
  574. * The slot is already programmed but the event got
  575. * missed, so its safe to issue it here.
  576. */
  577. dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
  578. edma_clean_channel(echan->ch_num);
  579. edma_stop(echan->ch_num);
  580. edma_start(echan->ch_num);
  581. edma_trigger_channel(echan->ch_num);
  582. }
  583. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  584. break;
  585. default:
  586. break;
  587. }
  588. }
  589. /* Alloc channel resources */
  590. static int edma_alloc_chan_resources(struct dma_chan *chan)
  591. {
  592. struct edma_chan *echan = to_edma_chan(chan);
  593. struct device *dev = chan->device->dev;
  594. int ret;
  595. int a_ch_num;
  596. LIST_HEAD(descs);
  597. a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
  598. chan, EVENTQ_DEFAULT);
  599. if (a_ch_num < 0) {
  600. ret = -ENODEV;
  601. goto err_no_chan;
  602. }
  603. if (a_ch_num != echan->ch_num) {
  604. dev_err(dev, "failed to allocate requested channel %u:%u\n",
  605. EDMA_CTLR(echan->ch_num),
  606. EDMA_CHAN_SLOT(echan->ch_num));
  607. ret = -ENODEV;
  608. goto err_wrong_chan;
  609. }
  610. echan->alloced = true;
  611. echan->slot[0] = echan->ch_num;
  612. dev_info(dev, "allocated channel for %u:%u\n",
  613. EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
  614. return 0;
  615. err_wrong_chan:
  616. edma_free_channel(a_ch_num);
  617. err_no_chan:
  618. return ret;
  619. }
  620. /* Free channel resources */
  621. static void edma_free_chan_resources(struct dma_chan *chan)
  622. {
  623. struct edma_chan *echan = to_edma_chan(chan);
  624. struct device *dev = chan->device->dev;
  625. int i;
  626. /* Terminate transfers */
  627. edma_stop(echan->ch_num);
  628. vchan_free_chan_resources(&echan->vchan);
  629. /* Free EDMA PaRAM slots */
  630. for (i = 1; i < EDMA_MAX_SLOTS; i++) {
  631. if (echan->slot[i] >= 0) {
  632. edma_free_slot(echan->slot[i]);
  633. echan->slot[i] = -1;
  634. }
  635. }
  636. /* Free EDMA channel */
  637. if (echan->alloced) {
  638. edma_free_channel(echan->ch_num);
  639. echan->alloced = false;
  640. }
  641. dev_info(dev, "freeing channel for %u\n", echan->ch_num);
  642. }
  643. /* Send pending descriptor to hardware */
  644. static void edma_issue_pending(struct dma_chan *chan)
  645. {
  646. struct edma_chan *echan = to_edma_chan(chan);
  647. unsigned long flags;
  648. spin_lock_irqsave(&echan->vchan.lock, flags);
  649. if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
  650. edma_execute(echan);
  651. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  652. }
  653. static size_t edma_desc_size(struct edma_desc *edesc)
  654. {
  655. int i;
  656. size_t size;
  657. if (edesc->absync)
  658. for (size = i = 0; i < edesc->pset_nr; i++)
  659. size += (edesc->pset[i].a_b_cnt & 0xffff) *
  660. (edesc->pset[i].a_b_cnt >> 16) *
  661. edesc->pset[i].ccnt;
  662. else
  663. size = (edesc->pset[0].a_b_cnt & 0xffff) *
  664. (edesc->pset[0].a_b_cnt >> 16) +
  665. (edesc->pset[0].a_b_cnt & 0xffff) *
  666. (SZ_64K - 1) * edesc->pset[0].ccnt;
  667. return size;
  668. }
  669. /* Check request completion status */
  670. static enum dma_status edma_tx_status(struct dma_chan *chan,
  671. dma_cookie_t cookie,
  672. struct dma_tx_state *txstate)
  673. {
  674. struct edma_chan *echan = to_edma_chan(chan);
  675. struct virt_dma_desc *vdesc;
  676. enum dma_status ret;
  677. unsigned long flags;
  678. ret = dma_cookie_status(chan, cookie, txstate);
  679. if (ret == DMA_COMPLETE || !txstate)
  680. return ret;
  681. spin_lock_irqsave(&echan->vchan.lock, flags);
  682. vdesc = vchan_find_desc(&echan->vchan, cookie);
  683. if (vdesc) {
  684. txstate->residue = edma_desc_size(to_edma_desc(&vdesc->tx));
  685. } else if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) {
  686. struct edma_desc *edesc = echan->edesc;
  687. txstate->residue = edma_desc_size(edesc);
  688. }
  689. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  690. return ret;
  691. }
  692. static void __init edma_chan_init(struct edma_cc *ecc,
  693. struct dma_device *dma,
  694. struct edma_chan *echans)
  695. {
  696. int i, j;
  697. for (i = 0; i < EDMA_CHANS; i++) {
  698. struct edma_chan *echan = &echans[i];
  699. echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
  700. echan->ecc = ecc;
  701. echan->vchan.desc_free = edma_desc_free;
  702. vchan_init(&echan->vchan, dma);
  703. INIT_LIST_HEAD(&echan->node);
  704. for (j = 0; j < EDMA_MAX_SLOTS; j++)
  705. echan->slot[j] = -1;
  706. }
  707. }
  708. static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
  709. struct device *dev)
  710. {
  711. dma->device_prep_slave_sg = edma_prep_slave_sg;
  712. dma->device_prep_dma_cyclic = edma_prep_dma_cyclic;
  713. dma->device_alloc_chan_resources = edma_alloc_chan_resources;
  714. dma->device_free_chan_resources = edma_free_chan_resources;
  715. dma->device_issue_pending = edma_issue_pending;
  716. dma->device_tx_status = edma_tx_status;
  717. dma->device_control = edma_control;
  718. dma->dev = dev;
  719. INIT_LIST_HEAD(&dma->channels);
  720. }
  721. static int edma_probe(struct platform_device *pdev)
  722. {
  723. struct edma_cc *ecc;
  724. int ret;
  725. ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
  726. if (!ecc) {
  727. dev_err(&pdev->dev, "Can't allocate controller\n");
  728. return -ENOMEM;
  729. }
  730. ecc->ctlr = pdev->id;
  731. ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
  732. if (ecc->dummy_slot < 0) {
  733. dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
  734. return -EIO;
  735. }
  736. dma_cap_zero(ecc->dma_slave.cap_mask);
  737. dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
  738. edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
  739. edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
  740. ret = dma_async_device_register(&ecc->dma_slave);
  741. if (ret)
  742. goto err_reg1;
  743. platform_set_drvdata(pdev, ecc);
  744. dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
  745. return 0;
  746. err_reg1:
  747. edma_free_slot(ecc->dummy_slot);
  748. return ret;
  749. }
  750. static int edma_remove(struct platform_device *pdev)
  751. {
  752. struct device *dev = &pdev->dev;
  753. struct edma_cc *ecc = dev_get_drvdata(dev);
  754. dma_async_device_unregister(&ecc->dma_slave);
  755. edma_free_slot(ecc->dummy_slot);
  756. return 0;
  757. }
  758. static struct platform_driver edma_driver = {
  759. .probe = edma_probe,
  760. .remove = edma_remove,
  761. .driver = {
  762. .name = "edma-dma-engine",
  763. .owner = THIS_MODULE,
  764. },
  765. };
  766. bool edma_filter_fn(struct dma_chan *chan, void *param)
  767. {
  768. if (chan->device->dev->driver == &edma_driver.driver) {
  769. struct edma_chan *echan = to_edma_chan(chan);
  770. unsigned ch_req = *(unsigned *)param;
  771. return ch_req == echan->ch_num;
  772. }
  773. return false;
  774. }
  775. EXPORT_SYMBOL(edma_filter_fn);
  776. static struct platform_device *pdev0, *pdev1;
  777. static const struct platform_device_info edma_dev_info0 = {
  778. .name = "edma-dma-engine",
  779. .id = 0,
  780. };
  781. static const struct platform_device_info edma_dev_info1 = {
  782. .name = "edma-dma-engine",
  783. .id = 1,
  784. };
  785. static int edma_init(void)
  786. {
  787. int ret = platform_driver_register(&edma_driver);
  788. if (ret == 0) {
  789. pdev0 = platform_device_register_full(&edma_dev_info0);
  790. if (IS_ERR(pdev0)) {
  791. platform_driver_unregister(&edma_driver);
  792. ret = PTR_ERR(pdev0);
  793. goto out;
  794. }
  795. pdev0->dev.dma_mask = &pdev0->dev.coherent_dma_mask;
  796. pdev0->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  797. }
  798. if (EDMA_CTLRS == 2) {
  799. pdev1 = platform_device_register_full(&edma_dev_info1);
  800. if (IS_ERR(pdev1)) {
  801. platform_driver_unregister(&edma_driver);
  802. platform_device_unregister(pdev0);
  803. ret = PTR_ERR(pdev1);
  804. }
  805. pdev1->dev.dma_mask = &pdev1->dev.coherent_dma_mask;
  806. pdev1->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  807. }
  808. out:
  809. return ret;
  810. }
  811. subsys_initcall(edma_init);
  812. static void __exit edma_exit(void)
  813. {
  814. platform_device_unregister(pdev0);
  815. if (pdev1)
  816. platform_device_unregister(pdev1);
  817. platform_driver_unregister(&edma_driver);
  818. }
  819. module_exit(edma_exit);
  820. MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
  821. MODULE_DESCRIPTION("TI EDMA DMA engine driver");
  822. MODULE_LICENSE("GPL v2");