mpc512x_dma.c 22 KB

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