mpc512x_dma.c 22 KB

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