mpc512x_dma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. /* 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->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 = mchan->chan.cookie + 1;
  378. if (cookie <= 0)
  379. cookie = 1;
  380. mchan->chan.cookie = cookie;
  381. mdesc->desc.cookie = cookie;
  382. spin_unlock_irqrestore(&mchan->lock, flags);
  383. return cookie;
  384. }
  385. /* Alloc channel resources */
  386. static int mpc_dma_alloc_chan_resources(struct dma_chan *chan)
  387. {
  388. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  389. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  390. struct mpc_dma_desc *mdesc;
  391. struct mpc_dma_tcd *tcd;
  392. dma_addr_t tcd_paddr;
  393. unsigned long flags;
  394. LIST_HEAD(descs);
  395. int i;
  396. /* Alloc DMA memory for Transfer Control Descriptors */
  397. tcd = dma_alloc_coherent(mdma->dma.dev,
  398. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  399. &tcd_paddr, GFP_KERNEL);
  400. if (!tcd)
  401. return -ENOMEM;
  402. /* Alloc descriptors for this channel */
  403. for (i = 0; i < MPC_DMA_DESCRIPTORS; i++) {
  404. mdesc = kzalloc(sizeof(struct mpc_dma_desc), GFP_KERNEL);
  405. if (!mdesc) {
  406. dev_notice(mdma->dma.dev, "Memory allocation error. "
  407. "Allocated only %u descriptors\n", i);
  408. break;
  409. }
  410. dma_async_tx_descriptor_init(&mdesc->desc, chan);
  411. mdesc->desc.flags = DMA_CTRL_ACK;
  412. mdesc->desc.tx_submit = mpc_dma_tx_submit;
  413. mdesc->tcd = &tcd[i];
  414. mdesc->tcd_paddr = tcd_paddr + (i * sizeof(struct mpc_dma_tcd));
  415. list_add_tail(&mdesc->node, &descs);
  416. }
  417. /* Return error only if no descriptors were allocated */
  418. if (i == 0) {
  419. dma_free_coherent(mdma->dma.dev,
  420. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  421. tcd, tcd_paddr);
  422. return -ENOMEM;
  423. }
  424. spin_lock_irqsave(&mchan->lock, flags);
  425. mchan->tcd = tcd;
  426. mchan->tcd_paddr = tcd_paddr;
  427. list_splice_tail_init(&descs, &mchan->free);
  428. spin_unlock_irqrestore(&mchan->lock, flags);
  429. /* Enable Error Interrupt */
  430. out_8(&mdma->regs->dmaseei, chan->chan_id);
  431. return 0;
  432. }
  433. /* Free channel resources */
  434. static void mpc_dma_free_chan_resources(struct dma_chan *chan)
  435. {
  436. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  437. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  438. struct mpc_dma_desc *mdesc, *tmp;
  439. struct mpc_dma_tcd *tcd;
  440. dma_addr_t tcd_paddr;
  441. unsigned long flags;
  442. LIST_HEAD(descs);
  443. spin_lock_irqsave(&mchan->lock, flags);
  444. /* Channel must be idle */
  445. BUG_ON(!list_empty(&mchan->prepared));
  446. BUG_ON(!list_empty(&mchan->queued));
  447. BUG_ON(!list_empty(&mchan->active));
  448. BUG_ON(!list_empty(&mchan->completed));
  449. /* Move data */
  450. list_splice_tail_init(&mchan->free, &descs);
  451. tcd = mchan->tcd;
  452. tcd_paddr = mchan->tcd_paddr;
  453. spin_unlock_irqrestore(&mchan->lock, flags);
  454. /* Free DMA memory used by descriptors */
  455. dma_free_coherent(mdma->dma.dev,
  456. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  457. tcd, tcd_paddr);
  458. /* Free descriptors */
  459. list_for_each_entry_safe(mdesc, tmp, &descs, node)
  460. kfree(mdesc);
  461. /* Disable Error Interrupt */
  462. out_8(&mdma->regs->dmaceei, chan->chan_id);
  463. }
  464. /* Send all pending descriptor to hardware */
  465. static void mpc_dma_issue_pending(struct dma_chan *chan)
  466. {
  467. /*
  468. * We are posting descriptors to the hardware as soon as
  469. * they are ready, so this function does nothing.
  470. */
  471. }
  472. /* Check request completion status */
  473. static enum dma_status
  474. mpc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  475. struct dma_tx_state *txstate)
  476. {
  477. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  478. unsigned long flags;
  479. dma_cookie_t last_used;
  480. dma_cookie_t last_complete;
  481. spin_lock_irqsave(&mchan->lock, flags);
  482. last_used = mchan->chan.cookie;
  483. last_complete = mchan->completed_cookie;
  484. spin_unlock_irqrestore(&mchan->lock, flags);
  485. dma_set_tx_state(txstate, last_complete, last_used, 0);
  486. return dma_async_is_complete(cookie, last_complete, last_used);
  487. }
  488. /* Prepare descriptor for memory to memory copy */
  489. static struct dma_async_tx_descriptor *
  490. mpc_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  491. size_t len, unsigned long flags)
  492. {
  493. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  494. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  495. struct mpc_dma_desc *mdesc = NULL;
  496. struct mpc_dma_tcd *tcd;
  497. unsigned long iflags;
  498. /* Get free descriptor */
  499. spin_lock_irqsave(&mchan->lock, iflags);
  500. if (!list_empty(&mchan->free)) {
  501. mdesc = list_first_entry(&mchan->free, struct mpc_dma_desc,
  502. node);
  503. list_del(&mdesc->node);
  504. }
  505. spin_unlock_irqrestore(&mchan->lock, iflags);
  506. if (!mdesc) {
  507. /* try to free completed descriptors */
  508. mpc_dma_process_completed(mdma);
  509. return NULL;
  510. }
  511. mdesc->error = 0;
  512. tcd = mdesc->tcd;
  513. /* Prepare Transfer Control Descriptor for this transaction */
  514. memset(tcd, 0, sizeof(struct mpc_dma_tcd));
  515. if (IS_ALIGNED(src | dst | len, 32)) {
  516. tcd->ssize = MPC_DMA_TSIZE_32;
  517. tcd->dsize = MPC_DMA_TSIZE_32;
  518. tcd->soff = 32;
  519. tcd->doff = 32;
  520. } else if (!mdma->is_mpc8308 && IS_ALIGNED(src | dst | len, 16)) {
  521. /* MPC8308 doesn't support 16 byte transfers */
  522. tcd->ssize = MPC_DMA_TSIZE_16;
  523. tcd->dsize = MPC_DMA_TSIZE_16;
  524. tcd->soff = 16;
  525. tcd->doff = 16;
  526. } else if (IS_ALIGNED(src | dst | len, 4)) {
  527. tcd->ssize = MPC_DMA_TSIZE_4;
  528. tcd->dsize = MPC_DMA_TSIZE_4;
  529. tcd->soff = 4;
  530. tcd->doff = 4;
  531. } else if (IS_ALIGNED(src | dst | len, 2)) {
  532. tcd->ssize = MPC_DMA_TSIZE_2;
  533. tcd->dsize = MPC_DMA_TSIZE_2;
  534. tcd->soff = 2;
  535. tcd->doff = 2;
  536. } else {
  537. tcd->ssize = MPC_DMA_TSIZE_1;
  538. tcd->dsize = MPC_DMA_TSIZE_1;
  539. tcd->soff = 1;
  540. tcd->doff = 1;
  541. }
  542. tcd->saddr = src;
  543. tcd->daddr = dst;
  544. tcd->nbytes = len;
  545. tcd->biter = 1;
  546. tcd->citer = 1;
  547. /* Place descriptor in prepared list */
  548. spin_lock_irqsave(&mchan->lock, iflags);
  549. list_add_tail(&mdesc->node, &mchan->prepared);
  550. spin_unlock_irqrestore(&mchan->lock, iflags);
  551. return &mdesc->desc;
  552. }
  553. static int __devinit mpc_dma_probe(struct platform_device *op)
  554. {
  555. struct device_node *dn = op->dev.of_node;
  556. struct device *dev = &op->dev;
  557. struct dma_device *dma;
  558. struct mpc_dma *mdma;
  559. struct mpc_dma_chan *mchan;
  560. struct resource res;
  561. ulong regs_start, regs_size;
  562. int retval, i;
  563. mdma = devm_kzalloc(dev, sizeof(struct mpc_dma), GFP_KERNEL);
  564. if (!mdma) {
  565. dev_err(dev, "Memory exhausted!\n");
  566. return -ENOMEM;
  567. }
  568. mdma->irq = irq_of_parse_and_map(dn, 0);
  569. if (mdma->irq == NO_IRQ) {
  570. dev_err(dev, "Error mapping IRQ!\n");
  571. return -EINVAL;
  572. }
  573. if (of_device_is_compatible(dn, "fsl,mpc8308-dma")) {
  574. mdma->is_mpc8308 = 1;
  575. mdma->irq2 = irq_of_parse_and_map(dn, 1);
  576. if (mdma->irq2 == NO_IRQ) {
  577. dev_err(dev, "Error mapping IRQ!\n");
  578. return -EINVAL;
  579. }
  580. }
  581. retval = of_address_to_resource(dn, 0, &res);
  582. if (retval) {
  583. dev_err(dev, "Error parsing memory region!\n");
  584. return retval;
  585. }
  586. regs_start = res.start;
  587. regs_size = resource_size(&res);
  588. if (!devm_request_mem_region(dev, regs_start, regs_size, DRV_NAME)) {
  589. dev_err(dev, "Error requesting memory region!\n");
  590. return -EBUSY;
  591. }
  592. mdma->regs = devm_ioremap(dev, regs_start, regs_size);
  593. if (!mdma->regs) {
  594. dev_err(dev, "Error mapping memory region!\n");
  595. return -ENOMEM;
  596. }
  597. mdma->tcd = (struct mpc_dma_tcd *)((u8 *)(mdma->regs)
  598. + MPC_DMA_TCD_OFFSET);
  599. retval = devm_request_irq(dev, mdma->irq, &mpc_dma_irq, 0, DRV_NAME,
  600. mdma);
  601. if (retval) {
  602. dev_err(dev, "Error requesting IRQ!\n");
  603. return -EINVAL;
  604. }
  605. if (mdma->is_mpc8308) {
  606. retval = devm_request_irq(dev, mdma->irq2, &mpc_dma_irq, 0,
  607. DRV_NAME, mdma);
  608. if (retval) {
  609. dev_err(dev, "Error requesting IRQ2!\n");
  610. return -EINVAL;
  611. }
  612. }
  613. spin_lock_init(&mdma->error_status_lock);
  614. dma = &mdma->dma;
  615. dma->dev = dev;
  616. if (!mdma->is_mpc8308)
  617. dma->chancnt = MPC_DMA_CHANNELS;
  618. else
  619. dma->chancnt = 16; /* MPC8308 DMA has only 16 channels */
  620. dma->device_alloc_chan_resources = mpc_dma_alloc_chan_resources;
  621. dma->device_free_chan_resources = mpc_dma_free_chan_resources;
  622. dma->device_issue_pending = mpc_dma_issue_pending;
  623. dma->device_tx_status = mpc_dma_tx_status;
  624. dma->device_prep_dma_memcpy = mpc_dma_prep_memcpy;
  625. INIT_LIST_HEAD(&dma->channels);
  626. dma_cap_set(DMA_MEMCPY, dma->cap_mask);
  627. for (i = 0; i < dma->chancnt; i++) {
  628. mchan = &mdma->channels[i];
  629. mchan->chan.device = dma;
  630. mchan->chan.chan_id = i;
  631. mchan->chan.cookie = 1;
  632. mchan->completed_cookie = mchan->chan.cookie;
  633. INIT_LIST_HEAD(&mchan->free);
  634. INIT_LIST_HEAD(&mchan->prepared);
  635. INIT_LIST_HEAD(&mchan->queued);
  636. INIT_LIST_HEAD(&mchan->active);
  637. INIT_LIST_HEAD(&mchan->completed);
  638. spin_lock_init(&mchan->lock);
  639. list_add_tail(&mchan->chan.device_node, &dma->channels);
  640. }
  641. tasklet_init(&mdma->tasklet, mpc_dma_tasklet, (unsigned long)mdma);
  642. /*
  643. * Configure DMA Engine:
  644. * - Dynamic clock,
  645. * - Round-robin group arbitration,
  646. * - Round-robin channel arbitration.
  647. */
  648. if (!mdma->is_mpc8308) {
  649. out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_EDCG |
  650. MPC_DMA_DMACR_ERGA | MPC_DMA_DMACR_ERCA);
  651. /* Disable hardware DMA requests */
  652. out_be32(&mdma->regs->dmaerqh, 0);
  653. out_be32(&mdma->regs->dmaerql, 0);
  654. /* Disable error interrupts */
  655. out_be32(&mdma->regs->dmaeeih, 0);
  656. out_be32(&mdma->regs->dmaeeil, 0);
  657. /* Clear interrupts status */
  658. out_be32(&mdma->regs->dmainth, 0xFFFFFFFF);
  659. out_be32(&mdma->regs->dmaintl, 0xFFFFFFFF);
  660. out_be32(&mdma->regs->dmaerrh, 0xFFFFFFFF);
  661. out_be32(&mdma->regs->dmaerrl, 0xFFFFFFFF);
  662. /* Route interrupts to IPIC */
  663. out_be32(&mdma->regs->dmaihsa, 0);
  664. out_be32(&mdma->regs->dmailsa, 0);
  665. } else {
  666. /* MPC8308 has 16 channels and lacks some registers */
  667. out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_ERCA);
  668. /* enable snooping */
  669. out_be32(&mdma->regs->dmagpor, MPC_DMA_DMAGPOR_SNOOP_ENABLE);
  670. /* Disable error interrupts */
  671. out_be32(&mdma->regs->dmaeeil, 0);
  672. /* Clear interrupts status */
  673. out_be32(&mdma->regs->dmaintl, 0xFFFF);
  674. out_be32(&mdma->regs->dmaerrl, 0xFFFF);
  675. }
  676. /* Register DMA engine */
  677. dev_set_drvdata(dev, mdma);
  678. retval = dma_async_device_register(dma);
  679. if (retval) {
  680. devm_free_irq(dev, mdma->irq, mdma);
  681. irq_dispose_mapping(mdma->irq);
  682. }
  683. return retval;
  684. }
  685. static int __devexit mpc_dma_remove(struct platform_device *op)
  686. {
  687. struct device *dev = &op->dev;
  688. struct mpc_dma *mdma = dev_get_drvdata(dev);
  689. dma_async_device_unregister(&mdma->dma);
  690. devm_free_irq(dev, mdma->irq, mdma);
  691. irq_dispose_mapping(mdma->irq);
  692. return 0;
  693. }
  694. static struct of_device_id mpc_dma_match[] = {
  695. { .compatible = "fsl,mpc5121-dma", },
  696. {},
  697. };
  698. static struct platform_driver mpc_dma_driver = {
  699. .probe = mpc_dma_probe,
  700. .remove = __devexit_p(mpc_dma_remove),
  701. .driver = {
  702. .name = DRV_NAME,
  703. .owner = THIS_MODULE,
  704. .of_match_table = mpc_dma_match,
  705. },
  706. };
  707. static int __init mpc_dma_init(void)
  708. {
  709. return platform_driver_register(&mpc_dma_driver);
  710. }
  711. module_init(mpc_dma_init);
  712. static void __exit mpc_dma_exit(void)
  713. {
  714. platform_driver_unregister(&mpc_dma_driver);
  715. }
  716. module_exit(mpc_dma_exit);
  717. MODULE_LICENSE("GPL");
  718. MODULE_AUTHOR("Piotr Ziecik <kosmo@semihalf.com>");