mpc512x_dma.c 23 KB

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