edma.c 29 KB

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