edma.c 19 KB

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