mpc512x_dma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * Copyright (C) Freescale Semicondutor, Inc. 2007, 2008.
  3. * Copyright (C) Semihalf 2009
  4. *
  5. * Written by Piotr Ziecik <kosmo@semihalf.com>. Hardware description
  6. * (defines, structures and comments) was taken from MPC5121 DMA driver
  7. * written by Hongjun Chen <hong-jun.chen@freescale.com>.
  8. *
  9. * Approved as OSADL project by a majority of OSADL members and funded
  10. * by OSADL membership fees in 2009; for details see www.osadl.org.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. * more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program; if not, write to the Free Software Foundation, Inc., 59
  24. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. *
  26. * The full GNU General Public License is included in this distribution in the
  27. * file called COPYING.
  28. */
  29. /*
  30. * This is initial version of MPC5121 DMA driver. Only memory to memory
  31. * transfers are supported (tested using dmatest module).
  32. */
  33. #include <linux/module.h>
  34. #include <linux/dmaengine.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/io.h>
  38. #include <linux/of_device.h>
  39. #include <linux/of_platform.h>
  40. #include <linux/random.h>
  41. /* Number of DMA Transfer descriptors allocated per channel */
  42. #define MPC_DMA_DESCRIPTORS 64
  43. /* Macro definitions */
  44. #define MPC_DMA_CHANNELS 64
  45. #define MPC_DMA_TCD_OFFSET 0x1000
  46. /* Arbitration mode of group and channel */
  47. #define MPC_DMA_DMACR_EDCG (1 << 31)
  48. #define MPC_DMA_DMACR_ERGA (1 << 3)
  49. #define MPC_DMA_DMACR_ERCA (1 << 2)
  50. /* Error codes */
  51. #define MPC_DMA_DMAES_VLD (1 << 31)
  52. #define MPC_DMA_DMAES_GPE (1 << 15)
  53. #define MPC_DMA_DMAES_CPE (1 << 14)
  54. #define MPC_DMA_DMAES_ERRCHN(err) \
  55. (((err) >> 8) & 0x3f)
  56. #define MPC_DMA_DMAES_SAE (1 << 7)
  57. #define MPC_DMA_DMAES_SOE (1 << 6)
  58. #define MPC_DMA_DMAES_DAE (1 << 5)
  59. #define MPC_DMA_DMAES_DOE (1 << 4)
  60. #define MPC_DMA_DMAES_NCE (1 << 3)
  61. #define MPC_DMA_DMAES_SGE (1 << 2)
  62. #define MPC_DMA_DMAES_SBE (1 << 1)
  63. #define MPC_DMA_DMAES_DBE (1 << 0)
  64. #define MPC_DMA_TSIZE_1 0x00
  65. #define MPC_DMA_TSIZE_2 0x01
  66. #define MPC_DMA_TSIZE_4 0x02
  67. #define MPC_DMA_TSIZE_16 0x04
  68. #define MPC_DMA_TSIZE_32 0x05
  69. /* MPC5121 DMA engine registers */
  70. struct __attribute__ ((__packed__)) mpc_dma_regs {
  71. /* 0x00 */
  72. u32 dmacr; /* DMA control register */
  73. u32 dmaes; /* DMA error status */
  74. /* 0x08 */
  75. u32 dmaerqh; /* DMA enable request high(channels 63~32) */
  76. u32 dmaerql; /* DMA enable request low(channels 31~0) */
  77. u32 dmaeeih; /* DMA enable error interrupt high(ch63~32) */
  78. u32 dmaeeil; /* DMA enable error interrupt low(ch31~0) */
  79. /* 0x18 */
  80. u8 dmaserq; /* DMA set enable request */
  81. u8 dmacerq; /* DMA clear enable request */
  82. u8 dmaseei; /* DMA set enable error interrupt */
  83. u8 dmaceei; /* DMA clear enable error interrupt */
  84. /* 0x1c */
  85. u8 dmacint; /* DMA clear interrupt request */
  86. u8 dmacerr; /* DMA clear error */
  87. u8 dmassrt; /* DMA set start bit */
  88. u8 dmacdne; /* DMA clear DONE status bit */
  89. /* 0x20 */
  90. u32 dmainth; /* DMA interrupt request high(ch63~32) */
  91. u32 dmaintl; /* DMA interrupt request low(ch31~0) */
  92. u32 dmaerrh; /* DMA error high(ch63~32) */
  93. u32 dmaerrl; /* DMA error low(ch31~0) */
  94. /* 0x30 */
  95. u32 dmahrsh; /* DMA hw request status high(ch63~32) */
  96. u32 dmahrsl; /* DMA hardware request status low(ch31~0) */
  97. u32 dmaihsa; /* DMA interrupt high select AXE(ch63~32) */
  98. u32 dmailsa; /* DMA interrupt low select AXE(ch31~0) */
  99. /* 0x40 ~ 0xff */
  100. u32 reserve0[48]; /* Reserved */
  101. /* 0x100 */
  102. u8 dchpri[MPC_DMA_CHANNELS];
  103. /* DMA channels(0~63) priority */
  104. };
  105. struct __attribute__ ((__packed__)) mpc_dma_tcd {
  106. /* 0x00 */
  107. u32 saddr; /* Source address */
  108. u32 smod:5; /* Source address modulo */
  109. u32 ssize:3; /* Source data transfer size */
  110. u32 dmod:5; /* Destination address modulo */
  111. u32 dsize:3; /* Destination data transfer size */
  112. u32 soff:16; /* Signed source address offset */
  113. /* 0x08 */
  114. u32 nbytes; /* Inner "minor" byte count */
  115. u32 slast; /* Last source address adjustment */
  116. u32 daddr; /* Destination address */
  117. /* 0x14 */
  118. u32 citer_elink:1; /* Enable channel-to-channel linking on
  119. * minor loop complete
  120. */
  121. u32 citer_linkch:6; /* Link channel for minor loop complete */
  122. u32 citer:9; /* Current "major" iteration count */
  123. u32 doff:16; /* Signed destination address offset */
  124. /* 0x18 */
  125. u32 dlast_sga; /* Last Destination address adjustment/scatter
  126. * gather address
  127. */
  128. /* 0x1c */
  129. u32 biter_elink:1; /* Enable channel-to-channel linking on major
  130. * loop complete
  131. */
  132. u32 biter_linkch:6;
  133. u32 biter:9; /* Beginning "major" iteration count */
  134. u32 bwc:2; /* Bandwidth control */
  135. u32 major_linkch:6; /* Link channel number */
  136. u32 done:1; /* Channel done */
  137. u32 active:1; /* Channel active */
  138. u32 major_elink:1; /* Enable channel-to-channel linking on major
  139. * loop complete
  140. */
  141. u32 e_sg:1; /* Enable scatter/gather processing */
  142. u32 d_req:1; /* Disable request */
  143. u32 int_half:1; /* Enable an interrupt when major counter is
  144. * half complete
  145. */
  146. u32 int_maj:1; /* Enable an interrupt when major iteration
  147. * count completes
  148. */
  149. u32 start:1; /* Channel start */
  150. };
  151. struct mpc_dma_desc {
  152. struct dma_async_tx_descriptor desc;
  153. struct mpc_dma_tcd *tcd;
  154. dma_addr_t tcd_paddr;
  155. int error;
  156. struct list_head node;
  157. };
  158. struct mpc_dma_chan {
  159. struct dma_chan chan;
  160. struct list_head free;
  161. struct list_head prepared;
  162. struct list_head queued;
  163. struct list_head active;
  164. struct list_head completed;
  165. struct mpc_dma_tcd *tcd;
  166. dma_addr_t tcd_paddr;
  167. dma_cookie_t completed_cookie;
  168. /* Lock for this structure */
  169. spinlock_t lock;
  170. };
  171. struct mpc_dma {
  172. struct dma_device dma;
  173. struct tasklet_struct tasklet;
  174. struct mpc_dma_chan channels[MPC_DMA_CHANNELS];
  175. struct mpc_dma_regs __iomem *regs;
  176. struct mpc_dma_tcd __iomem *tcd;
  177. int irq;
  178. uint error_status;
  179. /* Lock for error_status field in this structure */
  180. spinlock_t error_status_lock;
  181. };
  182. #define DRV_NAME "mpc512x_dma"
  183. /* Convert struct dma_chan to struct mpc_dma_chan */
  184. static inline struct mpc_dma_chan *dma_chan_to_mpc_dma_chan(struct dma_chan *c)
  185. {
  186. return container_of(c, struct mpc_dma_chan, chan);
  187. }
  188. /* Convert struct dma_chan to struct mpc_dma */
  189. static inline struct mpc_dma *dma_chan_to_mpc_dma(struct dma_chan *c)
  190. {
  191. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(c);
  192. return container_of(mchan, struct mpc_dma, channels[c->chan_id]);
  193. }
  194. /*
  195. * Execute all queued DMA descriptors.
  196. *
  197. * Following requirements must be met while calling mpc_dma_execute():
  198. * a) mchan->lock is acquired,
  199. * b) mchan->active list is empty,
  200. * c) mchan->queued list contains at least one entry.
  201. */
  202. static void mpc_dma_execute(struct mpc_dma_chan *mchan)
  203. {
  204. struct mpc_dma *mdma = dma_chan_to_mpc_dma(&mchan->chan);
  205. struct mpc_dma_desc *first = NULL;
  206. struct mpc_dma_desc *prev = NULL;
  207. struct mpc_dma_desc *mdesc;
  208. int cid = mchan->chan.chan_id;
  209. /* Move all queued descriptors to active list */
  210. list_splice_tail_init(&mchan->queued, &mchan->active);
  211. /* Chain descriptors into one transaction */
  212. list_for_each_entry(mdesc, &mchan->active, node) {
  213. if (!first)
  214. first = mdesc;
  215. if (!prev) {
  216. prev = mdesc;
  217. continue;
  218. }
  219. prev->tcd->dlast_sga = mdesc->tcd_paddr;
  220. prev->tcd->e_sg = 1;
  221. mdesc->tcd->start = 1;
  222. prev = mdesc;
  223. }
  224. prev->tcd->start = 0;
  225. prev->tcd->int_maj = 1;
  226. /* Send first descriptor in chain into hardware */
  227. memcpy_toio(&mdma->tcd[cid], first->tcd, sizeof(struct mpc_dma_tcd));
  228. out_8(&mdma->regs->dmassrt, cid);
  229. }
  230. /* Handle interrupt on one half of DMA controller (32 channels) */
  231. static void mpc_dma_irq_process(struct mpc_dma *mdma, u32 is, u32 es, int off)
  232. {
  233. struct mpc_dma_chan *mchan;
  234. struct mpc_dma_desc *mdesc;
  235. u32 status = is | es;
  236. int ch;
  237. while ((ch = fls(status) - 1) >= 0) {
  238. status &= ~(1 << ch);
  239. mchan = &mdma->channels[ch + off];
  240. spin_lock(&mchan->lock);
  241. /* Check error status */
  242. if (es & (1 << ch))
  243. list_for_each_entry(mdesc, &mchan->active, node)
  244. mdesc->error = -EIO;
  245. /* Execute queued descriptors */
  246. list_splice_tail_init(&mchan->active, &mchan->completed);
  247. if (!list_empty(&mchan->queued))
  248. mpc_dma_execute(mchan);
  249. spin_unlock(&mchan->lock);
  250. }
  251. }
  252. /* Interrupt handler */
  253. static irqreturn_t mpc_dma_irq(int irq, void *data)
  254. {
  255. struct mpc_dma *mdma = data;
  256. uint es;
  257. /* Save error status register */
  258. es = in_be32(&mdma->regs->dmaes);
  259. spin_lock(&mdma->error_status_lock);
  260. if ((es & MPC_DMA_DMAES_VLD) && mdma->error_status == 0)
  261. mdma->error_status = es;
  262. spin_unlock(&mdma->error_status_lock);
  263. /* Handle interrupt on each channel */
  264. mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmainth),
  265. in_be32(&mdma->regs->dmaerrh), 32);
  266. mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmaintl),
  267. in_be32(&mdma->regs->dmaerrl), 0);
  268. /* Ack interrupt on all channels */
  269. out_be32(&mdma->regs->dmainth, 0xFFFFFFFF);
  270. out_be32(&mdma->regs->dmaintl, 0xFFFFFFFF);
  271. out_be32(&mdma->regs->dmaerrh, 0xFFFFFFFF);
  272. out_be32(&mdma->regs->dmaerrl, 0xFFFFFFFF);
  273. /* Schedule tasklet */
  274. tasklet_schedule(&mdma->tasklet);
  275. return IRQ_HANDLED;
  276. }
  277. /* DMA Tasklet */
  278. static void mpc_dma_tasklet(unsigned long data)
  279. {
  280. struct mpc_dma *mdma = (void *)data;
  281. dma_cookie_t last_cookie = 0;
  282. struct mpc_dma_chan *mchan;
  283. struct mpc_dma_desc *mdesc;
  284. struct dma_async_tx_descriptor *desc;
  285. unsigned long flags;
  286. LIST_HEAD(list);
  287. uint es;
  288. int i;
  289. spin_lock_irqsave(&mdma->error_status_lock, flags);
  290. es = mdma->error_status;
  291. mdma->error_status = 0;
  292. spin_unlock_irqrestore(&mdma->error_status_lock, flags);
  293. /* Print nice error report */
  294. if (es) {
  295. dev_err(mdma->dma.dev,
  296. "Hardware reported following error(s) on channel %u:\n",
  297. MPC_DMA_DMAES_ERRCHN(es));
  298. if (es & MPC_DMA_DMAES_GPE)
  299. dev_err(mdma->dma.dev, "- Group Priority Error\n");
  300. if (es & MPC_DMA_DMAES_CPE)
  301. dev_err(mdma->dma.dev, "- Channel Priority Error\n");
  302. if (es & MPC_DMA_DMAES_SAE)
  303. dev_err(mdma->dma.dev, "- Source Address Error\n");
  304. if (es & MPC_DMA_DMAES_SOE)
  305. dev_err(mdma->dma.dev, "- Source Offset"
  306. " Configuration Error\n");
  307. if (es & MPC_DMA_DMAES_DAE)
  308. dev_err(mdma->dma.dev, "- Destination Address"
  309. " Error\n");
  310. if (es & MPC_DMA_DMAES_DOE)
  311. dev_err(mdma->dma.dev, "- Destination Offset"
  312. " Configuration Error\n");
  313. if (es & MPC_DMA_DMAES_NCE)
  314. dev_err(mdma->dma.dev, "- NBytes/Citter"
  315. " Configuration Error\n");
  316. if (es & MPC_DMA_DMAES_SGE)
  317. dev_err(mdma->dma.dev, "- Scatter/Gather"
  318. " Configuration Error\n");
  319. if (es & MPC_DMA_DMAES_SBE)
  320. dev_err(mdma->dma.dev, "- Source Bus Error\n");
  321. if (es & MPC_DMA_DMAES_DBE)
  322. dev_err(mdma->dma.dev, "- Destination Bus Error\n");
  323. }
  324. for (i = 0; i < mdma->dma.chancnt; i++) {
  325. mchan = &mdma->channels[i];
  326. /* Get all completed descriptors */
  327. spin_lock_irqsave(&mchan->lock, flags);
  328. if (!list_empty(&mchan->completed))
  329. list_splice_tail_init(&mchan->completed, &list);
  330. spin_unlock_irqrestore(&mchan->lock, flags);
  331. if (list_empty(&list))
  332. continue;
  333. /* Execute callbacks and run dependencies */
  334. list_for_each_entry(mdesc, &list, node) {
  335. desc = &mdesc->desc;
  336. if (desc->callback)
  337. desc->callback(desc->callback_param);
  338. last_cookie = desc->cookie;
  339. dma_run_dependencies(desc);
  340. }
  341. /* Free descriptors */
  342. spin_lock_irqsave(&mchan->lock, flags);
  343. list_splice_tail_init(&list, &mchan->free);
  344. mchan->completed_cookie = last_cookie;
  345. spin_unlock_irqrestore(&mchan->lock, flags);
  346. }
  347. }
  348. /* Submit descriptor to hardware */
  349. static dma_cookie_t mpc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
  350. {
  351. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(txd->chan);
  352. struct mpc_dma_desc *mdesc;
  353. unsigned long flags;
  354. dma_cookie_t cookie;
  355. mdesc = container_of(txd, struct mpc_dma_desc, desc);
  356. spin_lock_irqsave(&mchan->lock, flags);
  357. /* Move descriptor to queue */
  358. list_move_tail(&mdesc->node, &mchan->queued);
  359. /* If channel is idle, execute all queued descriptors */
  360. if (list_empty(&mchan->active))
  361. mpc_dma_execute(mchan);
  362. /* Update cookie */
  363. cookie = mchan->chan.cookie + 1;
  364. if (cookie <= 0)
  365. cookie = 1;
  366. mchan->chan.cookie = cookie;
  367. mdesc->desc.cookie = cookie;
  368. spin_unlock_irqrestore(&mchan->lock, flags);
  369. return cookie;
  370. }
  371. /* Alloc channel resources */
  372. static int mpc_dma_alloc_chan_resources(struct dma_chan *chan)
  373. {
  374. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  375. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  376. struct mpc_dma_desc *mdesc;
  377. struct mpc_dma_tcd *tcd;
  378. dma_addr_t tcd_paddr;
  379. unsigned long flags;
  380. LIST_HEAD(descs);
  381. int i;
  382. /* Alloc DMA memory for Transfer Control Descriptors */
  383. tcd = dma_alloc_coherent(mdma->dma.dev,
  384. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  385. &tcd_paddr, GFP_KERNEL);
  386. if (!tcd)
  387. return -ENOMEM;
  388. /* Alloc descriptors for this channel */
  389. for (i = 0; i < MPC_DMA_DESCRIPTORS; i++) {
  390. mdesc = kzalloc(sizeof(struct mpc_dma_desc), GFP_KERNEL);
  391. if (!mdesc) {
  392. dev_notice(mdma->dma.dev, "Memory allocation error. "
  393. "Allocated only %u descriptors\n", i);
  394. break;
  395. }
  396. dma_async_tx_descriptor_init(&mdesc->desc, chan);
  397. mdesc->desc.flags = DMA_CTRL_ACK;
  398. mdesc->desc.tx_submit = mpc_dma_tx_submit;
  399. mdesc->tcd = &tcd[i];
  400. mdesc->tcd_paddr = tcd_paddr + (i * sizeof(struct mpc_dma_tcd));
  401. list_add_tail(&mdesc->node, &descs);
  402. }
  403. /* Return error only if no descriptors were allocated */
  404. if (i == 0) {
  405. dma_free_coherent(mdma->dma.dev,
  406. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  407. tcd, tcd_paddr);
  408. return -ENOMEM;
  409. }
  410. spin_lock_irqsave(&mchan->lock, flags);
  411. mchan->tcd = tcd;
  412. mchan->tcd_paddr = tcd_paddr;
  413. list_splice_tail_init(&descs, &mchan->free);
  414. spin_unlock_irqrestore(&mchan->lock, flags);
  415. /* Enable Error Interrupt */
  416. out_8(&mdma->regs->dmaseei, chan->chan_id);
  417. return 0;
  418. }
  419. /* Free channel resources */
  420. static void mpc_dma_free_chan_resources(struct dma_chan *chan)
  421. {
  422. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  423. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  424. struct mpc_dma_desc *mdesc, *tmp;
  425. struct mpc_dma_tcd *tcd;
  426. dma_addr_t tcd_paddr;
  427. unsigned long flags;
  428. LIST_HEAD(descs);
  429. spin_lock_irqsave(&mchan->lock, flags);
  430. /* Channel must be idle */
  431. BUG_ON(!list_empty(&mchan->prepared));
  432. BUG_ON(!list_empty(&mchan->queued));
  433. BUG_ON(!list_empty(&mchan->active));
  434. BUG_ON(!list_empty(&mchan->completed));
  435. /* Move data */
  436. list_splice_tail_init(&mchan->free, &descs);
  437. tcd = mchan->tcd;
  438. tcd_paddr = mchan->tcd_paddr;
  439. spin_unlock_irqrestore(&mchan->lock, flags);
  440. /* Free DMA memory used by descriptors */
  441. dma_free_coherent(mdma->dma.dev,
  442. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  443. tcd, tcd_paddr);
  444. /* Free descriptors */
  445. list_for_each_entry_safe(mdesc, tmp, &descs, node)
  446. kfree(mdesc);
  447. /* Disable Error Interrupt */
  448. out_8(&mdma->regs->dmaceei, chan->chan_id);
  449. }
  450. /* Send all pending descriptor to hardware */
  451. static void mpc_dma_issue_pending(struct dma_chan *chan)
  452. {
  453. /*
  454. * We are posting descriptors to the hardware as soon as
  455. * they are ready, so this function does nothing.
  456. */
  457. }
  458. /* Check request completion status */
  459. static enum dma_status
  460. mpc_dma_is_tx_complete(struct dma_chan *chan, dma_cookie_t cookie,
  461. dma_cookie_t *done, dma_cookie_t *used)
  462. {
  463. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  464. unsigned long flags;
  465. dma_cookie_t last_used;
  466. dma_cookie_t last_complete;
  467. spin_lock_irqsave(&mchan->lock, flags);
  468. last_used = mchan->chan.cookie;
  469. last_complete = mchan->completed_cookie;
  470. spin_unlock_irqrestore(&mchan->lock, flags);
  471. if (done)
  472. *done = last_complete;
  473. if (used)
  474. *used = last_used;
  475. return dma_async_is_complete(cookie, last_complete, last_used);
  476. }
  477. /* Prepare descriptor for memory to memory copy */
  478. static struct dma_async_tx_descriptor *
  479. mpc_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  480. size_t len, unsigned long flags)
  481. {
  482. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  483. struct mpc_dma_desc *mdesc = NULL;
  484. struct mpc_dma_tcd *tcd;
  485. unsigned long iflags;
  486. /* Get free descriptor */
  487. spin_lock_irqsave(&mchan->lock, iflags);
  488. if (!list_empty(&mchan->free)) {
  489. mdesc = list_first_entry(&mchan->free, struct mpc_dma_desc,
  490. node);
  491. list_del(&mdesc->node);
  492. }
  493. spin_unlock_irqrestore(&mchan->lock, iflags);
  494. if (!mdesc)
  495. return NULL;
  496. mdesc->error = 0;
  497. tcd = mdesc->tcd;
  498. /* Prepare Transfer Control Descriptor for this transaction */
  499. memset(tcd, 0, sizeof(struct mpc_dma_tcd));
  500. if (IS_ALIGNED(src | dst | len, 32)) {
  501. tcd->ssize = MPC_DMA_TSIZE_32;
  502. tcd->dsize = MPC_DMA_TSIZE_32;
  503. tcd->soff = 32;
  504. tcd->doff = 32;
  505. } else if (IS_ALIGNED(src | dst | len, 16)) {
  506. tcd->ssize = MPC_DMA_TSIZE_16;
  507. tcd->dsize = MPC_DMA_TSIZE_16;
  508. tcd->soff = 16;
  509. tcd->doff = 16;
  510. } else if (IS_ALIGNED(src | dst | len, 4)) {
  511. tcd->ssize = MPC_DMA_TSIZE_4;
  512. tcd->dsize = MPC_DMA_TSIZE_4;
  513. tcd->soff = 4;
  514. tcd->doff = 4;
  515. } else if (IS_ALIGNED(src | dst | len, 2)) {
  516. tcd->ssize = MPC_DMA_TSIZE_2;
  517. tcd->dsize = MPC_DMA_TSIZE_2;
  518. tcd->soff = 2;
  519. tcd->doff = 2;
  520. } else {
  521. tcd->ssize = MPC_DMA_TSIZE_1;
  522. tcd->dsize = MPC_DMA_TSIZE_1;
  523. tcd->soff = 1;
  524. tcd->doff = 1;
  525. }
  526. tcd->saddr = src;
  527. tcd->daddr = dst;
  528. tcd->nbytes = len;
  529. tcd->biter = 1;
  530. tcd->citer = 1;
  531. /* Place descriptor in prepared list */
  532. spin_lock_irqsave(&mchan->lock, iflags);
  533. list_add_tail(&mdesc->node, &mchan->prepared);
  534. spin_unlock_irqrestore(&mchan->lock, iflags);
  535. return &mdesc->desc;
  536. }
  537. static int __devinit mpc_dma_probe(struct of_device *op,
  538. const struct of_device_id *match)
  539. {
  540. struct device_node *dn = op->node;
  541. struct device *dev = &op->dev;
  542. struct dma_device *dma;
  543. struct mpc_dma *mdma;
  544. struct mpc_dma_chan *mchan;
  545. struct resource res;
  546. ulong regs_start, regs_size;
  547. int retval, i;
  548. mdma = devm_kzalloc(dev, sizeof(struct mpc_dma), GFP_KERNEL);
  549. if (!mdma) {
  550. dev_err(dev, "Memory exhausted!\n");
  551. return -ENOMEM;
  552. }
  553. mdma->irq = irq_of_parse_and_map(dn, 0);
  554. if (mdma->irq == NO_IRQ) {
  555. dev_err(dev, "Error mapping IRQ!\n");
  556. return -EINVAL;
  557. }
  558. retval = of_address_to_resource(dn, 0, &res);
  559. if (retval) {
  560. dev_err(dev, "Error parsing memory region!\n");
  561. return retval;
  562. }
  563. regs_start = res.start;
  564. regs_size = res.end - res.start + 1;
  565. if (!devm_request_mem_region(dev, regs_start, regs_size, DRV_NAME)) {
  566. dev_err(dev, "Error requesting memory region!\n");
  567. return -EBUSY;
  568. }
  569. mdma->regs = devm_ioremap(dev, regs_start, regs_size);
  570. if (!mdma->regs) {
  571. dev_err(dev, "Error mapping memory region!\n");
  572. return -ENOMEM;
  573. }
  574. mdma->tcd = (struct mpc_dma_tcd *)((u8 *)(mdma->regs)
  575. + MPC_DMA_TCD_OFFSET);
  576. retval = devm_request_irq(dev, mdma->irq, &mpc_dma_irq, 0, DRV_NAME,
  577. mdma);
  578. if (retval) {
  579. dev_err(dev, "Error requesting IRQ!\n");
  580. return -EINVAL;
  581. }
  582. spin_lock_init(&mdma->error_status_lock);
  583. dma = &mdma->dma;
  584. dma->dev = dev;
  585. dma->chancnt = MPC_DMA_CHANNELS;
  586. dma->device_alloc_chan_resources = mpc_dma_alloc_chan_resources;
  587. dma->device_free_chan_resources = mpc_dma_free_chan_resources;
  588. dma->device_issue_pending = mpc_dma_issue_pending;
  589. dma->device_is_tx_complete = mpc_dma_is_tx_complete;
  590. dma->device_prep_dma_memcpy = mpc_dma_prep_memcpy;
  591. INIT_LIST_HEAD(&dma->channels);
  592. dma_cap_set(DMA_MEMCPY, dma->cap_mask);
  593. for (i = 0; i < dma->chancnt; i++) {
  594. mchan = &mdma->channels[i];
  595. mchan->chan.device = dma;
  596. mchan->chan.chan_id = i;
  597. mchan->chan.cookie = 1;
  598. mchan->completed_cookie = mchan->chan.cookie;
  599. INIT_LIST_HEAD(&mchan->free);
  600. INIT_LIST_HEAD(&mchan->prepared);
  601. INIT_LIST_HEAD(&mchan->queued);
  602. INIT_LIST_HEAD(&mchan->active);
  603. INIT_LIST_HEAD(&mchan->completed);
  604. spin_lock_init(&mchan->lock);
  605. list_add_tail(&mchan->chan.device_node, &dma->channels);
  606. }
  607. tasklet_init(&mdma->tasklet, mpc_dma_tasklet, (unsigned long)mdma);
  608. /*
  609. * Configure DMA Engine:
  610. * - Dynamic clock,
  611. * - Round-robin group arbitration,
  612. * - Round-robin channel arbitration.
  613. */
  614. out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_EDCG |
  615. MPC_DMA_DMACR_ERGA | MPC_DMA_DMACR_ERCA);
  616. /* Disable hardware DMA requests */
  617. out_be32(&mdma->regs->dmaerqh, 0);
  618. out_be32(&mdma->regs->dmaerql, 0);
  619. /* Disable error interrupts */
  620. out_be32(&mdma->regs->dmaeeih, 0);
  621. out_be32(&mdma->regs->dmaeeil, 0);
  622. /* Clear interrupts status */
  623. out_be32(&mdma->regs->dmainth, 0xFFFFFFFF);
  624. out_be32(&mdma->regs->dmaintl, 0xFFFFFFFF);
  625. out_be32(&mdma->regs->dmaerrh, 0xFFFFFFFF);
  626. out_be32(&mdma->regs->dmaerrl, 0xFFFFFFFF);
  627. /* Route interrupts to IPIC */
  628. out_be32(&mdma->regs->dmaihsa, 0);
  629. out_be32(&mdma->regs->dmailsa, 0);
  630. /* Register DMA engine */
  631. dev_set_drvdata(dev, mdma);
  632. retval = dma_async_device_register(dma);
  633. if (retval) {
  634. devm_free_irq(dev, mdma->irq, mdma);
  635. irq_dispose_mapping(mdma->irq);
  636. }
  637. return retval;
  638. }
  639. static int __devexit mpc_dma_remove(struct of_device *op)
  640. {
  641. struct device *dev = &op->dev;
  642. struct mpc_dma *mdma = dev_get_drvdata(dev);
  643. dma_async_device_unregister(&mdma->dma);
  644. devm_free_irq(dev, mdma->irq, mdma);
  645. irq_dispose_mapping(mdma->irq);
  646. return 0;
  647. }
  648. static struct of_device_id mpc_dma_match[] = {
  649. { .compatible = "fsl,mpc5121-dma", },
  650. {},
  651. };
  652. static struct of_platform_driver mpc_dma_driver = {
  653. .match_table = mpc_dma_match,
  654. .probe = mpc_dma_probe,
  655. .remove = __devexit_p(mpc_dma_remove),
  656. .driver = {
  657. .name = DRV_NAME,
  658. .owner = THIS_MODULE,
  659. },
  660. };
  661. static int __init mpc_dma_init(void)
  662. {
  663. return of_register_platform_driver(&mpc_dma_driver);
  664. }
  665. module_init(mpc_dma_init);
  666. static void __exit mpc_dma_exit(void)
  667. {
  668. of_unregister_platform_driver(&mpc_dma_driver);
  669. }
  670. module_exit(mpc_dma_exit);
  671. MODULE_LICENSE("GPL");
  672. MODULE_AUTHOR("Piotr Ziecik <kosmo@semihalf.com>");