edma.c 30 KB

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