edma.c 21 KB

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