mpc512x_dma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. /* Lock for this structure */
  174. spinlock_t lock;
  175. };
  176. struct mpc_dma {
  177. struct dma_device dma;
  178. struct tasklet_struct tasklet;
  179. struct mpc_dma_chan channels[MPC_DMA_CHANNELS];
  180. struct mpc_dma_regs __iomem *regs;
  181. struct mpc_dma_tcd __iomem *tcd;
  182. int irq;
  183. int irq2;
  184. uint error_status;
  185. int is_mpc8308;
  186. /* Lock for error_status field in this structure */
  187. spinlock_t error_status_lock;
  188. };
  189. #define DRV_NAME "mpc512x_dma"
  190. /* Convert struct dma_chan to struct mpc_dma_chan */
  191. static inline struct mpc_dma_chan *dma_chan_to_mpc_dma_chan(struct dma_chan *c)
  192. {
  193. return container_of(c, struct mpc_dma_chan, chan);
  194. }
  195. /* Convert struct dma_chan to struct mpc_dma */
  196. static inline struct mpc_dma *dma_chan_to_mpc_dma(struct dma_chan *c)
  197. {
  198. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(c);
  199. return container_of(mchan, struct mpc_dma, channels[c->chan_id]);
  200. }
  201. /*
  202. * Execute all queued DMA descriptors.
  203. *
  204. * Following requirements must be met while calling mpc_dma_execute():
  205. * a) mchan->lock is acquired,
  206. * b) mchan->active list is empty,
  207. * c) mchan->queued list contains at least one entry.
  208. */
  209. static void mpc_dma_execute(struct mpc_dma_chan *mchan)
  210. {
  211. struct mpc_dma *mdma = dma_chan_to_mpc_dma(&mchan->chan);
  212. struct mpc_dma_desc *first = NULL;
  213. struct mpc_dma_desc *prev = NULL;
  214. struct mpc_dma_desc *mdesc;
  215. int cid = mchan->chan.chan_id;
  216. /* Move all queued descriptors to active list */
  217. list_splice_tail_init(&mchan->queued, &mchan->active);
  218. /* Chain descriptors into one transaction */
  219. list_for_each_entry(mdesc, &mchan->active, node) {
  220. if (!first)
  221. first = mdesc;
  222. if (!prev) {
  223. prev = mdesc;
  224. continue;
  225. }
  226. prev->tcd->dlast_sga = mdesc->tcd_paddr;
  227. prev->tcd->e_sg = 1;
  228. mdesc->tcd->start = 1;
  229. prev = mdesc;
  230. }
  231. prev->tcd->int_maj = 1;
  232. /* Send first descriptor in chain into hardware */
  233. memcpy_toio(&mdma->tcd[cid], first->tcd, sizeof(struct mpc_dma_tcd));
  234. if (first != prev)
  235. mdma->tcd[cid].e_sg = 1;
  236. out_8(&mdma->regs->dmassrt, cid);
  237. }
  238. /* Handle interrupt on one half of DMA controller (32 channels) */
  239. static void mpc_dma_irq_process(struct mpc_dma *mdma, u32 is, u32 es, int off)
  240. {
  241. struct mpc_dma_chan *mchan;
  242. struct mpc_dma_desc *mdesc;
  243. u32 status = is | es;
  244. int ch;
  245. while ((ch = fls(status) - 1) >= 0) {
  246. status &= ~(1 << ch);
  247. mchan = &mdma->channels[ch + off];
  248. spin_lock(&mchan->lock);
  249. out_8(&mdma->regs->dmacint, ch + off);
  250. out_8(&mdma->regs->dmacerr, ch + off);
  251. /* Check error status */
  252. if (es & (1 << ch))
  253. list_for_each_entry(mdesc, &mchan->active, node)
  254. mdesc->error = -EIO;
  255. /* Execute queued descriptors */
  256. list_splice_tail_init(&mchan->active, &mchan->completed);
  257. if (!list_empty(&mchan->queued))
  258. mpc_dma_execute(mchan);
  259. spin_unlock(&mchan->lock);
  260. }
  261. }
  262. /* Interrupt handler */
  263. static irqreturn_t mpc_dma_irq(int irq, void *data)
  264. {
  265. struct mpc_dma *mdma = data;
  266. uint es;
  267. /* Save error status register */
  268. es = in_be32(&mdma->regs->dmaes);
  269. spin_lock(&mdma->error_status_lock);
  270. if ((es & MPC_DMA_DMAES_VLD) && mdma->error_status == 0)
  271. mdma->error_status = es;
  272. spin_unlock(&mdma->error_status_lock);
  273. /* Handle interrupt on each channel */
  274. if (mdma->dma.chancnt > 32) {
  275. mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmainth),
  276. in_be32(&mdma->regs->dmaerrh), 32);
  277. }
  278. mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmaintl),
  279. in_be32(&mdma->regs->dmaerrl), 0);
  280. /* Schedule tasklet */
  281. tasklet_schedule(&mdma->tasklet);
  282. return IRQ_HANDLED;
  283. }
  284. /* process completed descriptors */
  285. static void mpc_dma_process_completed(struct mpc_dma *mdma)
  286. {
  287. dma_cookie_t last_cookie = 0;
  288. struct mpc_dma_chan *mchan;
  289. struct mpc_dma_desc *mdesc;
  290. struct dma_async_tx_descriptor *desc;
  291. unsigned long flags;
  292. LIST_HEAD(list);
  293. int i;
  294. for (i = 0; i < mdma->dma.chancnt; i++) {
  295. mchan = &mdma->channels[i];
  296. /* Get all completed descriptors */
  297. spin_lock_irqsave(&mchan->lock, flags);
  298. if (!list_empty(&mchan->completed))
  299. list_splice_tail_init(&mchan->completed, &list);
  300. spin_unlock_irqrestore(&mchan->lock, flags);
  301. if (list_empty(&list))
  302. continue;
  303. /* Execute callbacks and run dependencies */
  304. list_for_each_entry(mdesc, &list, node) {
  305. desc = &mdesc->desc;
  306. if (desc->callback)
  307. desc->callback(desc->callback_param);
  308. last_cookie = desc->cookie;
  309. dma_run_dependencies(desc);
  310. }
  311. /* Free descriptors */
  312. spin_lock_irqsave(&mchan->lock, flags);
  313. list_splice_tail_init(&list, &mchan->free);
  314. mchan->chan.completed_cookie = last_cookie;
  315. spin_unlock_irqrestore(&mchan->lock, flags);
  316. }
  317. }
  318. /* DMA Tasklet */
  319. static void mpc_dma_tasklet(unsigned long data)
  320. {
  321. struct mpc_dma *mdma = (void *)data;
  322. unsigned long flags;
  323. uint es;
  324. spin_lock_irqsave(&mdma->error_status_lock, flags);
  325. es = mdma->error_status;
  326. mdma->error_status = 0;
  327. spin_unlock_irqrestore(&mdma->error_status_lock, flags);
  328. /* Print nice error report */
  329. if (es) {
  330. dev_err(mdma->dma.dev,
  331. "Hardware reported following error(s) on channel %u:\n",
  332. MPC_DMA_DMAES_ERRCHN(es));
  333. if (es & MPC_DMA_DMAES_GPE)
  334. dev_err(mdma->dma.dev, "- Group Priority Error\n");
  335. if (es & MPC_DMA_DMAES_CPE)
  336. dev_err(mdma->dma.dev, "- Channel Priority Error\n");
  337. if (es & MPC_DMA_DMAES_SAE)
  338. dev_err(mdma->dma.dev, "- Source Address Error\n");
  339. if (es & MPC_DMA_DMAES_SOE)
  340. dev_err(mdma->dma.dev, "- Source Offset"
  341. " Configuration Error\n");
  342. if (es & MPC_DMA_DMAES_DAE)
  343. dev_err(mdma->dma.dev, "- Destination Address"
  344. " Error\n");
  345. if (es & MPC_DMA_DMAES_DOE)
  346. dev_err(mdma->dma.dev, "- Destination Offset"
  347. " Configuration Error\n");
  348. if (es & MPC_DMA_DMAES_NCE)
  349. dev_err(mdma->dma.dev, "- NBytes/Citter"
  350. " Configuration Error\n");
  351. if (es & MPC_DMA_DMAES_SGE)
  352. dev_err(mdma->dma.dev, "- Scatter/Gather"
  353. " Configuration Error\n");
  354. if (es & MPC_DMA_DMAES_SBE)
  355. dev_err(mdma->dma.dev, "- Source Bus Error\n");
  356. if (es & MPC_DMA_DMAES_DBE)
  357. dev_err(mdma->dma.dev, "- Destination Bus Error\n");
  358. }
  359. mpc_dma_process_completed(mdma);
  360. }
  361. /* Submit descriptor to hardware */
  362. static dma_cookie_t mpc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
  363. {
  364. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(txd->chan);
  365. struct mpc_dma_desc *mdesc;
  366. unsigned long flags;
  367. dma_cookie_t cookie;
  368. mdesc = container_of(txd, struct mpc_dma_desc, desc);
  369. spin_lock_irqsave(&mchan->lock, flags);
  370. /* Move descriptor to queue */
  371. list_move_tail(&mdesc->node, &mchan->queued);
  372. /* If channel is idle, execute all queued descriptors */
  373. if (list_empty(&mchan->active))
  374. mpc_dma_execute(mchan);
  375. /* Update cookie */
  376. cookie = mchan->chan.cookie + 1;
  377. if (cookie <= 0)
  378. cookie = 1;
  379. mchan->chan.cookie = cookie;
  380. mdesc->desc.cookie = cookie;
  381. spin_unlock_irqrestore(&mchan->lock, flags);
  382. return cookie;
  383. }
  384. /* Alloc channel resources */
  385. static int mpc_dma_alloc_chan_resources(struct dma_chan *chan)
  386. {
  387. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  388. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  389. struct mpc_dma_desc *mdesc;
  390. struct mpc_dma_tcd *tcd;
  391. dma_addr_t tcd_paddr;
  392. unsigned long flags;
  393. LIST_HEAD(descs);
  394. int i;
  395. /* Alloc DMA memory for Transfer Control Descriptors */
  396. tcd = dma_alloc_coherent(mdma->dma.dev,
  397. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  398. &tcd_paddr, GFP_KERNEL);
  399. if (!tcd)
  400. return -ENOMEM;
  401. /* Alloc descriptors for this channel */
  402. for (i = 0; i < MPC_DMA_DESCRIPTORS; i++) {
  403. mdesc = kzalloc(sizeof(struct mpc_dma_desc), GFP_KERNEL);
  404. if (!mdesc) {
  405. dev_notice(mdma->dma.dev, "Memory allocation error. "
  406. "Allocated only %u descriptors\n", i);
  407. break;
  408. }
  409. dma_async_tx_descriptor_init(&mdesc->desc, chan);
  410. mdesc->desc.flags = DMA_CTRL_ACK;
  411. mdesc->desc.tx_submit = mpc_dma_tx_submit;
  412. mdesc->tcd = &tcd[i];
  413. mdesc->tcd_paddr = tcd_paddr + (i * sizeof(struct mpc_dma_tcd));
  414. list_add_tail(&mdesc->node, &descs);
  415. }
  416. /* Return error only if no descriptors were allocated */
  417. if (i == 0) {
  418. dma_free_coherent(mdma->dma.dev,
  419. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  420. tcd, tcd_paddr);
  421. return -ENOMEM;
  422. }
  423. spin_lock_irqsave(&mchan->lock, flags);
  424. mchan->tcd = tcd;
  425. mchan->tcd_paddr = tcd_paddr;
  426. list_splice_tail_init(&descs, &mchan->free);
  427. spin_unlock_irqrestore(&mchan->lock, flags);
  428. /* Enable Error Interrupt */
  429. out_8(&mdma->regs->dmaseei, chan->chan_id);
  430. return 0;
  431. }
  432. /* Free channel resources */
  433. static void mpc_dma_free_chan_resources(struct dma_chan *chan)
  434. {
  435. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  436. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  437. struct mpc_dma_desc *mdesc, *tmp;
  438. struct mpc_dma_tcd *tcd;
  439. dma_addr_t tcd_paddr;
  440. unsigned long flags;
  441. LIST_HEAD(descs);
  442. spin_lock_irqsave(&mchan->lock, flags);
  443. /* Channel must be idle */
  444. BUG_ON(!list_empty(&mchan->prepared));
  445. BUG_ON(!list_empty(&mchan->queued));
  446. BUG_ON(!list_empty(&mchan->active));
  447. BUG_ON(!list_empty(&mchan->completed));
  448. /* Move data */
  449. list_splice_tail_init(&mchan->free, &descs);
  450. tcd = mchan->tcd;
  451. tcd_paddr = mchan->tcd_paddr;
  452. spin_unlock_irqrestore(&mchan->lock, flags);
  453. /* Free DMA memory used by descriptors */
  454. dma_free_coherent(mdma->dma.dev,
  455. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  456. tcd, tcd_paddr);
  457. /* Free descriptors */
  458. list_for_each_entry_safe(mdesc, tmp, &descs, node)
  459. kfree(mdesc);
  460. /* Disable Error Interrupt */
  461. out_8(&mdma->regs->dmaceei, chan->chan_id);
  462. }
  463. /* Send all pending descriptor to hardware */
  464. static void mpc_dma_issue_pending(struct dma_chan *chan)
  465. {
  466. /*
  467. * We are posting descriptors to the hardware as soon as
  468. * they are ready, so this function does nothing.
  469. */
  470. }
  471. /* Check request completion status */
  472. static enum dma_status
  473. mpc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  474. struct dma_tx_state *txstate)
  475. {
  476. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  477. unsigned long flags;
  478. dma_cookie_t last_used;
  479. dma_cookie_t last_complete;
  480. spin_lock_irqsave(&mchan->lock, flags);
  481. last_used = mchan->chan.cookie;
  482. last_complete = mchan->chan.completed_cookie;
  483. spin_unlock_irqrestore(&mchan->lock, flags);
  484. dma_set_tx_state(txstate, last_complete, last_used, 0);
  485. return dma_async_is_complete(cookie, last_complete, last_used);
  486. }
  487. /* Prepare descriptor for memory to memory copy */
  488. static struct dma_async_tx_descriptor *
  489. mpc_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  490. size_t len, unsigned long flags)
  491. {
  492. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  493. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  494. struct mpc_dma_desc *mdesc = NULL;
  495. struct mpc_dma_tcd *tcd;
  496. unsigned long iflags;
  497. /* Get free descriptor */
  498. spin_lock_irqsave(&mchan->lock, iflags);
  499. if (!list_empty(&mchan->free)) {
  500. mdesc = list_first_entry(&mchan->free, struct mpc_dma_desc,
  501. node);
  502. list_del(&mdesc->node);
  503. }
  504. spin_unlock_irqrestore(&mchan->lock, iflags);
  505. if (!mdesc) {
  506. /* try to free completed descriptors */
  507. mpc_dma_process_completed(mdma);
  508. return NULL;
  509. }
  510. mdesc->error = 0;
  511. tcd = mdesc->tcd;
  512. /* Prepare Transfer Control Descriptor for this transaction */
  513. memset(tcd, 0, sizeof(struct mpc_dma_tcd));
  514. if (IS_ALIGNED(src | dst | len, 32)) {
  515. tcd->ssize = MPC_DMA_TSIZE_32;
  516. tcd->dsize = MPC_DMA_TSIZE_32;
  517. tcd->soff = 32;
  518. tcd->doff = 32;
  519. } else if (!mdma->is_mpc8308 && IS_ALIGNED(src | dst | len, 16)) {
  520. /* MPC8308 doesn't support 16 byte transfers */
  521. tcd->ssize = MPC_DMA_TSIZE_16;
  522. tcd->dsize = MPC_DMA_TSIZE_16;
  523. tcd->soff = 16;
  524. tcd->doff = 16;
  525. } else if (IS_ALIGNED(src | dst | len, 4)) {
  526. tcd->ssize = MPC_DMA_TSIZE_4;
  527. tcd->dsize = MPC_DMA_TSIZE_4;
  528. tcd->soff = 4;
  529. tcd->doff = 4;
  530. } else if (IS_ALIGNED(src | dst | len, 2)) {
  531. tcd->ssize = MPC_DMA_TSIZE_2;
  532. tcd->dsize = MPC_DMA_TSIZE_2;
  533. tcd->soff = 2;
  534. tcd->doff = 2;
  535. } else {
  536. tcd->ssize = MPC_DMA_TSIZE_1;
  537. tcd->dsize = MPC_DMA_TSIZE_1;
  538. tcd->soff = 1;
  539. tcd->doff = 1;
  540. }
  541. tcd->saddr = src;
  542. tcd->daddr = dst;
  543. tcd->nbytes = len;
  544. tcd->biter = 1;
  545. tcd->citer = 1;
  546. /* Place descriptor in prepared list */
  547. spin_lock_irqsave(&mchan->lock, iflags);
  548. list_add_tail(&mdesc->node, &mchan->prepared);
  549. spin_unlock_irqrestore(&mchan->lock, iflags);
  550. return &mdesc->desc;
  551. }
  552. static int __devinit mpc_dma_probe(struct platform_device *op)
  553. {
  554. struct device_node *dn = op->dev.of_node;
  555. struct device *dev = &op->dev;
  556. struct dma_device *dma;
  557. struct mpc_dma *mdma;
  558. struct mpc_dma_chan *mchan;
  559. struct resource res;
  560. ulong regs_start, regs_size;
  561. int retval, i;
  562. mdma = devm_kzalloc(dev, sizeof(struct mpc_dma), GFP_KERNEL);
  563. if (!mdma) {
  564. dev_err(dev, "Memory exhausted!\n");
  565. return -ENOMEM;
  566. }
  567. mdma->irq = irq_of_parse_and_map(dn, 0);
  568. if (mdma->irq == NO_IRQ) {
  569. dev_err(dev, "Error mapping IRQ!\n");
  570. return -EINVAL;
  571. }
  572. if (of_device_is_compatible(dn, "fsl,mpc8308-dma")) {
  573. mdma->is_mpc8308 = 1;
  574. mdma->irq2 = irq_of_parse_and_map(dn, 1);
  575. if (mdma->irq2 == NO_IRQ) {
  576. dev_err(dev, "Error mapping IRQ!\n");
  577. return -EINVAL;
  578. }
  579. }
  580. retval = of_address_to_resource(dn, 0, &res);
  581. if (retval) {
  582. dev_err(dev, "Error parsing memory region!\n");
  583. return retval;
  584. }
  585. regs_start = res.start;
  586. regs_size = resource_size(&res);
  587. if (!devm_request_mem_region(dev, regs_start, regs_size, DRV_NAME)) {
  588. dev_err(dev, "Error requesting memory region!\n");
  589. return -EBUSY;
  590. }
  591. mdma->regs = devm_ioremap(dev, regs_start, regs_size);
  592. if (!mdma->regs) {
  593. dev_err(dev, "Error mapping memory region!\n");
  594. return -ENOMEM;
  595. }
  596. mdma->tcd = (struct mpc_dma_tcd *)((u8 *)(mdma->regs)
  597. + MPC_DMA_TCD_OFFSET);
  598. retval = devm_request_irq(dev, mdma->irq, &mpc_dma_irq, 0, DRV_NAME,
  599. mdma);
  600. if (retval) {
  601. dev_err(dev, "Error requesting IRQ!\n");
  602. return -EINVAL;
  603. }
  604. if (mdma->is_mpc8308) {
  605. retval = devm_request_irq(dev, mdma->irq2, &mpc_dma_irq, 0,
  606. DRV_NAME, mdma);
  607. if (retval) {
  608. dev_err(dev, "Error requesting IRQ2!\n");
  609. return -EINVAL;
  610. }
  611. }
  612. spin_lock_init(&mdma->error_status_lock);
  613. dma = &mdma->dma;
  614. dma->dev = dev;
  615. if (!mdma->is_mpc8308)
  616. dma->chancnt = MPC_DMA_CHANNELS;
  617. else
  618. dma->chancnt = 16; /* MPC8308 DMA has only 16 channels */
  619. dma->device_alloc_chan_resources = mpc_dma_alloc_chan_resources;
  620. dma->device_free_chan_resources = mpc_dma_free_chan_resources;
  621. dma->device_issue_pending = mpc_dma_issue_pending;
  622. dma->device_tx_status = mpc_dma_tx_status;
  623. dma->device_prep_dma_memcpy = mpc_dma_prep_memcpy;
  624. INIT_LIST_HEAD(&dma->channels);
  625. dma_cap_set(DMA_MEMCPY, dma->cap_mask);
  626. for (i = 0; i < dma->chancnt; i++) {
  627. mchan = &mdma->channels[i];
  628. mchan->chan.device = dma;
  629. mchan->chan.cookie = 1;
  630. mchan->chan.completed_cookie = mchan->chan.cookie;
  631. INIT_LIST_HEAD(&mchan->free);
  632. INIT_LIST_HEAD(&mchan->prepared);
  633. INIT_LIST_HEAD(&mchan->queued);
  634. INIT_LIST_HEAD(&mchan->active);
  635. INIT_LIST_HEAD(&mchan->completed);
  636. spin_lock_init(&mchan->lock);
  637. list_add_tail(&mchan->chan.device_node, &dma->channels);
  638. }
  639. tasklet_init(&mdma->tasklet, mpc_dma_tasklet, (unsigned long)mdma);
  640. /*
  641. * Configure DMA Engine:
  642. * - Dynamic clock,
  643. * - Round-robin group arbitration,
  644. * - Round-robin channel arbitration.
  645. */
  646. if (!mdma->is_mpc8308) {
  647. out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_EDCG |
  648. MPC_DMA_DMACR_ERGA | MPC_DMA_DMACR_ERCA);
  649. /* Disable hardware DMA requests */
  650. out_be32(&mdma->regs->dmaerqh, 0);
  651. out_be32(&mdma->regs->dmaerql, 0);
  652. /* Disable error interrupts */
  653. out_be32(&mdma->regs->dmaeeih, 0);
  654. out_be32(&mdma->regs->dmaeeil, 0);
  655. /* Clear interrupts status */
  656. out_be32(&mdma->regs->dmainth, 0xFFFFFFFF);
  657. out_be32(&mdma->regs->dmaintl, 0xFFFFFFFF);
  658. out_be32(&mdma->regs->dmaerrh, 0xFFFFFFFF);
  659. out_be32(&mdma->regs->dmaerrl, 0xFFFFFFFF);
  660. /* Route interrupts to IPIC */
  661. out_be32(&mdma->regs->dmaihsa, 0);
  662. out_be32(&mdma->regs->dmailsa, 0);
  663. } else {
  664. /* MPC8308 has 16 channels and lacks some registers */
  665. out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_ERCA);
  666. /* enable snooping */
  667. out_be32(&mdma->regs->dmagpor, MPC_DMA_DMAGPOR_SNOOP_ENABLE);
  668. /* Disable error interrupts */
  669. out_be32(&mdma->regs->dmaeeil, 0);
  670. /* Clear interrupts status */
  671. out_be32(&mdma->regs->dmaintl, 0xFFFF);
  672. out_be32(&mdma->regs->dmaerrl, 0xFFFF);
  673. }
  674. /* Register DMA engine */
  675. dev_set_drvdata(dev, mdma);
  676. retval = dma_async_device_register(dma);
  677. if (retval) {
  678. devm_free_irq(dev, mdma->irq, mdma);
  679. irq_dispose_mapping(mdma->irq);
  680. }
  681. return retval;
  682. }
  683. static int __devexit mpc_dma_remove(struct platform_device *op)
  684. {
  685. struct device *dev = &op->dev;
  686. struct mpc_dma *mdma = dev_get_drvdata(dev);
  687. dma_async_device_unregister(&mdma->dma);
  688. devm_free_irq(dev, mdma->irq, mdma);
  689. irq_dispose_mapping(mdma->irq);
  690. return 0;
  691. }
  692. static struct of_device_id mpc_dma_match[] = {
  693. { .compatible = "fsl,mpc5121-dma", },
  694. {},
  695. };
  696. static struct platform_driver mpc_dma_driver = {
  697. .probe = mpc_dma_probe,
  698. .remove = __devexit_p(mpc_dma_remove),
  699. .driver = {
  700. .name = DRV_NAME,
  701. .owner = THIS_MODULE,
  702. .of_match_table = mpc_dma_match,
  703. },
  704. };
  705. module_platform_driver(mpc_dma_driver);
  706. MODULE_LICENSE("GPL");
  707. MODULE_AUTHOR("Piotr Ziecik <kosmo@semihalf.com>");