edma.c 29 KB

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