mpc512x_dma.c 22 KB

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