edma.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. kfree(edesc);
  267. dev_err(dev, "Failed to allocate slot\n");
  268. kfree(edesc);
  269. return NULL;
  270. }
  271. }
  272. }
  273. /* Configure PaRAM sets for each SG */
  274. for_each_sg(sgl, sg, sg_len, i) {
  275. acnt = dev_width;
  276. /*
  277. * If the maxburst is equal to the fifo width, use
  278. * A-synced transfers. This allows for large contiguous
  279. * buffer transfers using only one PaRAM set.
  280. */
  281. if (burst == 1) {
  282. edesc->absync = false;
  283. ccnt = sg_dma_len(sg) / acnt / (SZ_64K - 1);
  284. bcnt = sg_dma_len(sg) / acnt - ccnt * (SZ_64K - 1);
  285. if (bcnt)
  286. ccnt++;
  287. else
  288. bcnt = SZ_64K - 1;
  289. cidx = acnt;
  290. /*
  291. * If maxburst is greater than the fifo address_width,
  292. * use AB-synced transfers where A count is the fifo
  293. * address_width and B count is the maxburst. In this
  294. * case, we are limited to transfers of C count frames
  295. * of (address_width * maxburst) where C count is limited
  296. * to SZ_64K-1. This places an upper bound on the length
  297. * of an SG segment that can be handled.
  298. */
  299. } else {
  300. edesc->absync = true;
  301. bcnt = burst;
  302. ccnt = sg_dma_len(sg) / (acnt * bcnt);
  303. if (ccnt > (SZ_64K - 1)) {
  304. dev_err(dev, "Exceeded max SG segment size\n");
  305. kfree(edesc);
  306. return NULL;
  307. }
  308. cidx = acnt * bcnt;
  309. }
  310. if (direction == DMA_MEM_TO_DEV) {
  311. src = sg_dma_address(sg);
  312. dst = dev_addr;
  313. src_bidx = acnt;
  314. src_cidx = cidx;
  315. dst_bidx = 0;
  316. dst_cidx = 0;
  317. } else {
  318. src = dev_addr;
  319. dst = sg_dma_address(sg);
  320. src_bidx = 0;
  321. src_cidx = 0;
  322. dst_bidx = acnt;
  323. dst_cidx = cidx;
  324. }
  325. edesc->pset[i].opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num));
  326. /* Configure A or AB synchronized transfers */
  327. if (edesc->absync)
  328. edesc->pset[i].opt |= SYNCDIM;
  329. /* If this is the last in a current SG set of transactions,
  330. enable interrupts so that next set is processed */
  331. if (!((i+1) % MAX_NR_SG))
  332. edesc->pset[i].opt |= TCINTEN;
  333. /* If this is the last set, enable completion interrupt flag */
  334. if (i == sg_len - 1)
  335. edesc->pset[i].opt |= TCINTEN;
  336. edesc->pset[i].src = src;
  337. edesc->pset[i].dst = dst;
  338. edesc->pset[i].src_dst_bidx = (dst_bidx << 16) | src_bidx;
  339. edesc->pset[i].src_dst_cidx = (dst_cidx << 16) | src_cidx;
  340. edesc->pset[i].a_b_cnt = bcnt << 16 | acnt;
  341. edesc->pset[i].ccnt = ccnt;
  342. edesc->pset[i].link_bcntrld = 0xffffffff;
  343. }
  344. return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags);
  345. }
  346. static void edma_callback(unsigned ch_num, u16 ch_status, void *data)
  347. {
  348. struct edma_chan *echan = data;
  349. struct device *dev = echan->vchan.chan.device->dev;
  350. struct edma_desc *edesc;
  351. unsigned long flags;
  352. struct edmacc_param p;
  353. /* Pause the channel */
  354. edma_pause(echan->ch_num);
  355. switch (ch_status) {
  356. case DMA_COMPLETE:
  357. spin_lock_irqsave(&echan->vchan.lock, flags);
  358. edesc = echan->edesc;
  359. if (edesc) {
  360. if (edesc->processed == edesc->pset_nr) {
  361. dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num);
  362. edma_stop(echan->ch_num);
  363. vchan_cookie_complete(&edesc->vdesc);
  364. } else {
  365. dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num);
  366. }
  367. edma_execute(echan);
  368. }
  369. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  370. break;
  371. case DMA_CC_ERROR:
  372. spin_lock_irqsave(&echan->vchan.lock, flags);
  373. edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p);
  374. /*
  375. * Issue later based on missed flag which will be sure
  376. * to happen as:
  377. * (1) we finished transmitting an intermediate slot and
  378. * edma_execute is coming up.
  379. * (2) or we finished current transfer and issue will
  380. * call edma_execute.
  381. *
  382. * Important note: issuing can be dangerous here and
  383. * lead to some nasty recursion when we are in a NULL
  384. * slot. So we avoid doing so and set the missed flag.
  385. */
  386. if (p.a_b_cnt == 0 && p.ccnt == 0) {
  387. dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n");
  388. echan->missed = 1;
  389. } else {
  390. /*
  391. * The slot is already programmed but the event got
  392. * missed, so its safe to issue it here.
  393. */
  394. dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n");
  395. edma_clean_channel(echan->ch_num);
  396. edma_stop(echan->ch_num);
  397. edma_start(echan->ch_num);
  398. edma_trigger_channel(echan->ch_num);
  399. }
  400. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  401. break;
  402. default:
  403. break;
  404. }
  405. }
  406. /* Alloc channel resources */
  407. static int edma_alloc_chan_resources(struct dma_chan *chan)
  408. {
  409. struct edma_chan *echan = to_edma_chan(chan);
  410. struct device *dev = chan->device->dev;
  411. int ret;
  412. int a_ch_num;
  413. LIST_HEAD(descs);
  414. a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback,
  415. chan, EVENTQ_DEFAULT);
  416. if (a_ch_num < 0) {
  417. ret = -ENODEV;
  418. goto err_no_chan;
  419. }
  420. if (a_ch_num != echan->ch_num) {
  421. dev_err(dev, "failed to allocate requested channel %u:%u\n",
  422. EDMA_CTLR(echan->ch_num),
  423. EDMA_CHAN_SLOT(echan->ch_num));
  424. ret = -ENODEV;
  425. goto err_wrong_chan;
  426. }
  427. echan->alloced = true;
  428. echan->slot[0] = echan->ch_num;
  429. dev_info(dev, "allocated channel for %u:%u\n",
  430. EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num));
  431. return 0;
  432. err_wrong_chan:
  433. edma_free_channel(a_ch_num);
  434. err_no_chan:
  435. return ret;
  436. }
  437. /* Free channel resources */
  438. static void edma_free_chan_resources(struct dma_chan *chan)
  439. {
  440. struct edma_chan *echan = to_edma_chan(chan);
  441. struct device *dev = chan->device->dev;
  442. int i;
  443. /* Terminate transfers */
  444. edma_stop(echan->ch_num);
  445. vchan_free_chan_resources(&echan->vchan);
  446. /* Free EDMA PaRAM slots */
  447. for (i = 1; i < EDMA_MAX_SLOTS; i++) {
  448. if (echan->slot[i] >= 0) {
  449. edma_free_slot(echan->slot[i]);
  450. echan->slot[i] = -1;
  451. }
  452. }
  453. /* Free EDMA channel */
  454. if (echan->alloced) {
  455. edma_free_channel(echan->ch_num);
  456. echan->alloced = false;
  457. }
  458. dev_info(dev, "freeing channel for %u\n", echan->ch_num);
  459. }
  460. /* Send pending descriptor to hardware */
  461. static void edma_issue_pending(struct dma_chan *chan)
  462. {
  463. struct edma_chan *echan = to_edma_chan(chan);
  464. unsigned long flags;
  465. spin_lock_irqsave(&echan->vchan.lock, flags);
  466. if (vchan_issue_pending(&echan->vchan) && !echan->edesc)
  467. edma_execute(echan);
  468. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  469. }
  470. static size_t edma_desc_size(struct edma_desc *edesc)
  471. {
  472. int i;
  473. size_t size;
  474. if (edesc->absync)
  475. for (size = i = 0; i < edesc->pset_nr; i++)
  476. size += (edesc->pset[i].a_b_cnt & 0xffff) *
  477. (edesc->pset[i].a_b_cnt >> 16) *
  478. edesc->pset[i].ccnt;
  479. else
  480. size = (edesc->pset[0].a_b_cnt & 0xffff) *
  481. (edesc->pset[0].a_b_cnt >> 16) +
  482. (edesc->pset[0].a_b_cnt & 0xffff) *
  483. (SZ_64K - 1) * edesc->pset[0].ccnt;
  484. return size;
  485. }
  486. /* Check request completion status */
  487. static enum dma_status edma_tx_status(struct dma_chan *chan,
  488. dma_cookie_t cookie,
  489. struct dma_tx_state *txstate)
  490. {
  491. struct edma_chan *echan = to_edma_chan(chan);
  492. struct virt_dma_desc *vdesc;
  493. enum dma_status ret;
  494. unsigned long flags;
  495. ret = dma_cookie_status(chan, cookie, txstate);
  496. if (ret == DMA_SUCCESS || !txstate)
  497. return ret;
  498. spin_lock_irqsave(&echan->vchan.lock, flags);
  499. vdesc = vchan_find_desc(&echan->vchan, cookie);
  500. if (vdesc) {
  501. txstate->residue = edma_desc_size(to_edma_desc(&vdesc->tx));
  502. } else if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) {
  503. struct edma_desc *edesc = echan->edesc;
  504. txstate->residue = edma_desc_size(edesc);
  505. }
  506. spin_unlock_irqrestore(&echan->vchan.lock, flags);
  507. return ret;
  508. }
  509. static void __init edma_chan_init(struct edma_cc *ecc,
  510. struct dma_device *dma,
  511. struct edma_chan *echans)
  512. {
  513. int i, j;
  514. for (i = 0; i < EDMA_CHANS; i++) {
  515. struct edma_chan *echan = &echans[i];
  516. echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i);
  517. echan->ecc = ecc;
  518. echan->vchan.desc_free = edma_desc_free;
  519. vchan_init(&echan->vchan, dma);
  520. INIT_LIST_HEAD(&echan->node);
  521. for (j = 0; j < EDMA_MAX_SLOTS; j++)
  522. echan->slot[j] = -1;
  523. }
  524. }
  525. static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma,
  526. struct device *dev)
  527. {
  528. dma->device_prep_slave_sg = edma_prep_slave_sg;
  529. dma->device_alloc_chan_resources = edma_alloc_chan_resources;
  530. dma->device_free_chan_resources = edma_free_chan_resources;
  531. dma->device_issue_pending = edma_issue_pending;
  532. dma->device_tx_status = edma_tx_status;
  533. dma->device_control = edma_control;
  534. dma->dev = dev;
  535. INIT_LIST_HEAD(&dma->channels);
  536. }
  537. static int edma_probe(struct platform_device *pdev)
  538. {
  539. struct edma_cc *ecc;
  540. int ret;
  541. ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL);
  542. if (!ecc) {
  543. dev_err(&pdev->dev, "Can't allocate controller\n");
  544. return -ENOMEM;
  545. }
  546. ecc->ctlr = pdev->id;
  547. ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY);
  548. if (ecc->dummy_slot < 0) {
  549. dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n");
  550. return -EIO;
  551. }
  552. dma_cap_zero(ecc->dma_slave.cap_mask);
  553. dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask);
  554. edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev);
  555. edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans);
  556. ret = dma_async_device_register(&ecc->dma_slave);
  557. if (ret)
  558. goto err_reg1;
  559. platform_set_drvdata(pdev, ecc);
  560. dev_info(&pdev->dev, "TI EDMA DMA engine driver\n");
  561. return 0;
  562. err_reg1:
  563. edma_free_slot(ecc->dummy_slot);
  564. return ret;
  565. }
  566. static int edma_remove(struct platform_device *pdev)
  567. {
  568. struct device *dev = &pdev->dev;
  569. struct edma_cc *ecc = dev_get_drvdata(dev);
  570. dma_async_device_unregister(&ecc->dma_slave);
  571. edma_free_slot(ecc->dummy_slot);
  572. return 0;
  573. }
  574. static struct platform_driver edma_driver = {
  575. .probe = edma_probe,
  576. .remove = edma_remove,
  577. .driver = {
  578. .name = "edma-dma-engine",
  579. .owner = THIS_MODULE,
  580. },
  581. };
  582. bool edma_filter_fn(struct dma_chan *chan, void *param)
  583. {
  584. if (chan->device->dev->driver == &edma_driver.driver) {
  585. struct edma_chan *echan = to_edma_chan(chan);
  586. unsigned ch_req = *(unsigned *)param;
  587. return ch_req == echan->ch_num;
  588. }
  589. return false;
  590. }
  591. EXPORT_SYMBOL(edma_filter_fn);
  592. static struct platform_device *pdev0, *pdev1;
  593. static const struct platform_device_info edma_dev_info0 = {
  594. .name = "edma-dma-engine",
  595. .id = 0,
  596. };
  597. static const struct platform_device_info edma_dev_info1 = {
  598. .name = "edma-dma-engine",
  599. .id = 1,
  600. };
  601. static int edma_init(void)
  602. {
  603. int ret = platform_driver_register(&edma_driver);
  604. if (ret == 0) {
  605. pdev0 = platform_device_register_full(&edma_dev_info0);
  606. if (IS_ERR(pdev0)) {
  607. platform_driver_unregister(&edma_driver);
  608. ret = PTR_ERR(pdev0);
  609. goto out;
  610. }
  611. pdev0->dev.dma_mask = &pdev0->dev.coherent_dma_mask;
  612. pdev0->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  613. }
  614. if (EDMA_CTLRS == 2) {
  615. pdev1 = platform_device_register_full(&edma_dev_info1);
  616. if (IS_ERR(pdev1)) {
  617. platform_driver_unregister(&edma_driver);
  618. platform_device_unregister(pdev0);
  619. ret = PTR_ERR(pdev1);
  620. }
  621. pdev1->dev.dma_mask = &pdev1->dev.coherent_dma_mask;
  622. pdev1->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  623. }
  624. out:
  625. return ret;
  626. }
  627. subsys_initcall(edma_init);
  628. static void __exit edma_exit(void)
  629. {
  630. platform_device_unregister(pdev0);
  631. if (pdev1)
  632. platform_device_unregister(pdev1);
  633. platform_driver_unregister(&edma_driver);
  634. }
  635. module_exit(edma_exit);
  636. MODULE_AUTHOR("Matt Porter <matt.porter@linaro.org>");
  637. MODULE_DESCRIPTION("TI EDMA DMA engine driver");
  638. MODULE_LICENSE("GPL v2");