mpc512x_dma.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * Copyright (C) Freescale Semicondutor, Inc. 2007, 2008.
  3. * Copyright (C) Semihalf 2009
  4. * Copyright (C) Ilya Yanok, Emcraft Systems 2010
  5. * Copyright (C) Alexander Popov, Promcontroller 2014
  6. *
  7. * Written by Piotr Ziecik <kosmo@semihalf.com>. Hardware description
  8. * (defines, structures and comments) was taken from MPC5121 DMA driver
  9. * written by Hongjun Chen <hong-jun.chen@freescale.com>.
  10. *
  11. * Approved as OSADL project by a majority of OSADL members and funded
  12. * by OSADL membership fees in 2009; for details see www.osadl.org.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but WITHOUT
  20. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  22. * more details.
  23. *
  24. * The full GNU General Public License is included in this distribution in the
  25. * file called COPYING.
  26. */
  27. /*
  28. * MPC512x and MPC8308 DMA driver. It supports
  29. * memory to memory data transfers (tested using dmatest module) and
  30. * data transfers between memory and peripheral I/O memory
  31. * by means of slave scatter/gather with these limitations:
  32. * - chunked transfers (described by s/g lists with more than one item)
  33. * are refused as long as proper support for scatter/gather is missing;
  34. * - transfers on MPC8308 always start from software as this SoC appears
  35. * not to have external request lines for peripheral flow control;
  36. * - only peripheral devices with 4-byte FIFO access register are supported;
  37. * - minimal memory <-> I/O memory transfer chunk is 4 bytes and consequently
  38. * source and destination addresses must be 4-byte aligned
  39. * and transfer size must be aligned on (4 * maxburst) boundary;
  40. */
  41. #include <linux/module.h>
  42. #include <linux/dmaengine.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/io.h>
  46. #include <linux/slab.h>
  47. #include <linux/of_address.h>
  48. #include <linux/of_device.h>
  49. #include <linux/of_irq.h>
  50. #include <linux/of_dma.h>
  51. #include <linux/of_platform.h>
  52. #include <linux/random.h>
  53. #include "dmaengine.h"
  54. /* Number of DMA Transfer descriptors allocated per channel */
  55. #define MPC_DMA_DESCRIPTORS 64
  56. /* Macro definitions */
  57. #define MPC_DMA_TCD_OFFSET 0x1000
  58. /*
  59. * Maximum channel counts for individual hardware variants
  60. * and the maximum channel count over all supported controllers,
  61. * used for data structure size
  62. */
  63. #define MPC8308_DMACHAN_MAX 16
  64. #define MPC512x_DMACHAN_MAX 64
  65. #define MPC_DMA_CHANNELS 64
  66. /* Arbitration mode of group and channel */
  67. #define MPC_DMA_DMACR_EDCG (1 << 31)
  68. #define MPC_DMA_DMACR_ERGA (1 << 3)
  69. #define MPC_DMA_DMACR_ERCA (1 << 2)
  70. /* Error codes */
  71. #define MPC_DMA_DMAES_VLD (1 << 31)
  72. #define MPC_DMA_DMAES_GPE (1 << 15)
  73. #define MPC_DMA_DMAES_CPE (1 << 14)
  74. #define MPC_DMA_DMAES_ERRCHN(err) \
  75. (((err) >> 8) & 0x3f)
  76. #define MPC_DMA_DMAES_SAE (1 << 7)
  77. #define MPC_DMA_DMAES_SOE (1 << 6)
  78. #define MPC_DMA_DMAES_DAE (1 << 5)
  79. #define MPC_DMA_DMAES_DOE (1 << 4)
  80. #define MPC_DMA_DMAES_NCE (1 << 3)
  81. #define MPC_DMA_DMAES_SGE (1 << 2)
  82. #define MPC_DMA_DMAES_SBE (1 << 1)
  83. #define MPC_DMA_DMAES_DBE (1 << 0)
  84. #define MPC_DMA_DMAGPOR_SNOOP_ENABLE (1 << 6)
  85. #define MPC_DMA_TSIZE_1 0x00
  86. #define MPC_DMA_TSIZE_2 0x01
  87. #define MPC_DMA_TSIZE_4 0x02
  88. #define MPC_DMA_TSIZE_16 0x04
  89. #define MPC_DMA_TSIZE_32 0x05
  90. /* MPC5121 DMA engine registers */
  91. struct __attribute__ ((__packed__)) mpc_dma_regs {
  92. /* 0x00 */
  93. u32 dmacr; /* DMA control register */
  94. u32 dmaes; /* DMA error status */
  95. /* 0x08 */
  96. u32 dmaerqh; /* DMA enable request high(channels 63~32) */
  97. u32 dmaerql; /* DMA enable request low(channels 31~0) */
  98. u32 dmaeeih; /* DMA enable error interrupt high(ch63~32) */
  99. u32 dmaeeil; /* DMA enable error interrupt low(ch31~0) */
  100. /* 0x18 */
  101. u8 dmaserq; /* DMA set enable request */
  102. u8 dmacerq; /* DMA clear enable request */
  103. u8 dmaseei; /* DMA set enable error interrupt */
  104. u8 dmaceei; /* DMA clear enable error interrupt */
  105. /* 0x1c */
  106. u8 dmacint; /* DMA clear interrupt request */
  107. u8 dmacerr; /* DMA clear error */
  108. u8 dmassrt; /* DMA set start bit */
  109. u8 dmacdne; /* DMA clear DONE status bit */
  110. /* 0x20 */
  111. u32 dmainth; /* DMA interrupt request high(ch63~32) */
  112. u32 dmaintl; /* DMA interrupt request low(ch31~0) */
  113. u32 dmaerrh; /* DMA error high(ch63~32) */
  114. u32 dmaerrl; /* DMA error low(ch31~0) */
  115. /* 0x30 */
  116. u32 dmahrsh; /* DMA hw request status high(ch63~32) */
  117. u32 dmahrsl; /* DMA hardware request status low(ch31~0) */
  118. union {
  119. u32 dmaihsa; /* DMA interrupt high select AXE(ch63~32) */
  120. u32 dmagpor; /* (General purpose register on MPC8308) */
  121. };
  122. u32 dmailsa; /* DMA interrupt low select AXE(ch31~0) */
  123. /* 0x40 ~ 0xff */
  124. u32 reserve0[48]; /* Reserved */
  125. /* 0x100 */
  126. u8 dchpri[MPC_DMA_CHANNELS];
  127. /* DMA channels(0~63) priority */
  128. };
  129. struct __attribute__ ((__packed__)) mpc_dma_tcd {
  130. /* 0x00 */
  131. u32 saddr; /* Source address */
  132. u32 smod:5; /* Source address modulo */
  133. u32 ssize:3; /* Source data transfer size */
  134. u32 dmod:5; /* Destination address modulo */
  135. u32 dsize:3; /* Destination data transfer size */
  136. u32 soff:16; /* Signed source address offset */
  137. /* 0x08 */
  138. u32 nbytes; /* Inner "minor" byte count */
  139. u32 slast; /* Last source address adjustment */
  140. u32 daddr; /* Destination address */
  141. /* 0x14 */
  142. u32 citer_elink:1; /* Enable channel-to-channel linking on
  143. * minor loop complete
  144. */
  145. u32 citer_linkch:6; /* Link channel for minor loop complete */
  146. u32 citer:9; /* Current "major" iteration count */
  147. u32 doff:16; /* Signed destination address offset */
  148. /* 0x18 */
  149. u32 dlast_sga; /* Last Destination address adjustment/scatter
  150. * gather address
  151. */
  152. /* 0x1c */
  153. u32 biter_elink:1; /* Enable channel-to-channel linking on major
  154. * loop complete
  155. */
  156. u32 biter_linkch:6;
  157. u32 biter:9; /* Beginning "major" iteration count */
  158. u32 bwc:2; /* Bandwidth control */
  159. u32 major_linkch:6; /* Link channel number */
  160. u32 done:1; /* Channel done */
  161. u32 active:1; /* Channel active */
  162. u32 major_elink:1; /* Enable channel-to-channel linking on major
  163. * loop complete
  164. */
  165. u32 e_sg:1; /* Enable scatter/gather processing */
  166. u32 d_req:1; /* Disable request */
  167. u32 int_half:1; /* Enable an interrupt when major counter is
  168. * half complete
  169. */
  170. u32 int_maj:1; /* Enable an interrupt when major iteration
  171. * count completes
  172. */
  173. u32 start:1; /* Channel start */
  174. };
  175. struct mpc_dma_desc {
  176. struct dma_async_tx_descriptor desc;
  177. struct mpc_dma_tcd *tcd;
  178. dma_addr_t tcd_paddr;
  179. int error;
  180. struct list_head node;
  181. int will_access_peripheral;
  182. };
  183. struct mpc_dma_chan {
  184. struct dma_chan chan;
  185. struct list_head free;
  186. struct list_head prepared;
  187. struct list_head queued;
  188. struct list_head active;
  189. struct list_head completed;
  190. struct mpc_dma_tcd *tcd;
  191. dma_addr_t tcd_paddr;
  192. /* Settings for access to peripheral FIFO */
  193. dma_addr_t src_per_paddr;
  194. u32 src_tcd_nunits;
  195. dma_addr_t dst_per_paddr;
  196. u32 dst_tcd_nunits;
  197. /* Lock for this structure */
  198. spinlock_t lock;
  199. };
  200. struct mpc_dma {
  201. struct dma_device dma;
  202. struct tasklet_struct tasklet;
  203. struct mpc_dma_chan channels[MPC_DMA_CHANNELS];
  204. struct mpc_dma_regs __iomem *regs;
  205. struct mpc_dma_tcd __iomem *tcd;
  206. int irq;
  207. int irq2;
  208. uint error_status;
  209. int is_mpc8308;
  210. /* Lock for error_status field in this structure */
  211. spinlock_t error_status_lock;
  212. };
  213. #define DRV_NAME "mpc512x_dma"
  214. /* Convert struct dma_chan to struct mpc_dma_chan */
  215. static inline struct mpc_dma_chan *dma_chan_to_mpc_dma_chan(struct dma_chan *c)
  216. {
  217. return container_of(c, struct mpc_dma_chan, chan);
  218. }
  219. /* Convert struct dma_chan to struct mpc_dma */
  220. static inline struct mpc_dma *dma_chan_to_mpc_dma(struct dma_chan *c)
  221. {
  222. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(c);
  223. return container_of(mchan, struct mpc_dma, channels[c->chan_id]);
  224. }
  225. /*
  226. * Execute all queued DMA descriptors.
  227. *
  228. * Following requirements must be met while calling mpc_dma_execute():
  229. * a) mchan->lock is acquired,
  230. * b) mchan->active list is empty,
  231. * c) mchan->queued list contains at least one entry.
  232. */
  233. static void mpc_dma_execute(struct mpc_dma_chan *mchan)
  234. {
  235. struct mpc_dma *mdma = dma_chan_to_mpc_dma(&mchan->chan);
  236. struct mpc_dma_desc *first = NULL;
  237. struct mpc_dma_desc *prev = NULL;
  238. struct mpc_dma_desc *mdesc;
  239. int cid = mchan->chan.chan_id;
  240. while (!list_empty(&mchan->queued)) {
  241. mdesc = list_first_entry(&mchan->queued,
  242. struct mpc_dma_desc, node);
  243. /*
  244. * Grab either several mem-to-mem transfer descriptors
  245. * or one peripheral transfer descriptor,
  246. * don't mix mem-to-mem and peripheral transfer descriptors
  247. * within the same 'active' list.
  248. */
  249. if (mdesc->will_access_peripheral) {
  250. if (list_empty(&mchan->active))
  251. list_move_tail(&mdesc->node, &mchan->active);
  252. break;
  253. } else {
  254. list_move_tail(&mdesc->node, &mchan->active);
  255. }
  256. }
  257. /* Chain descriptors into one transaction */
  258. list_for_each_entry(mdesc, &mchan->active, node) {
  259. if (!first)
  260. first = mdesc;
  261. if (!prev) {
  262. prev = mdesc;
  263. continue;
  264. }
  265. prev->tcd->dlast_sga = mdesc->tcd_paddr;
  266. prev->tcd->e_sg = 1;
  267. mdesc->tcd->start = 1;
  268. prev = mdesc;
  269. }
  270. prev->tcd->int_maj = 1;
  271. /* Send first descriptor in chain into hardware */
  272. memcpy_toio(&mdma->tcd[cid], first->tcd, sizeof(struct mpc_dma_tcd));
  273. if (first != prev)
  274. mdma->tcd[cid].e_sg = 1;
  275. if (mdma->is_mpc8308) {
  276. /* MPC8308, no request lines, software initiated start */
  277. out_8(&mdma->regs->dmassrt, cid);
  278. } else if (first->will_access_peripheral) {
  279. /* Peripherals involved, start by external request signal */
  280. out_8(&mdma->regs->dmaserq, cid);
  281. } else {
  282. /* Memory to memory transfer, software initiated start */
  283. out_8(&mdma->regs->dmassrt, cid);
  284. }
  285. }
  286. /* Handle interrupt on one half of DMA controller (32 channels) */
  287. static void mpc_dma_irq_process(struct mpc_dma *mdma, u32 is, u32 es, int off)
  288. {
  289. struct mpc_dma_chan *mchan;
  290. struct mpc_dma_desc *mdesc;
  291. u32 status = is | es;
  292. int ch;
  293. while ((ch = fls(status) - 1) >= 0) {
  294. status &= ~(1 << ch);
  295. mchan = &mdma->channels[ch + off];
  296. spin_lock(&mchan->lock);
  297. out_8(&mdma->regs->dmacint, ch + off);
  298. out_8(&mdma->regs->dmacerr, ch + off);
  299. /* Check error status */
  300. if (es & (1 << ch))
  301. list_for_each_entry(mdesc, &mchan->active, node)
  302. mdesc->error = -EIO;
  303. /* Execute queued descriptors */
  304. list_splice_tail_init(&mchan->active, &mchan->completed);
  305. if (!list_empty(&mchan->queued))
  306. mpc_dma_execute(mchan);
  307. spin_unlock(&mchan->lock);
  308. }
  309. }
  310. /* Interrupt handler */
  311. static irqreturn_t mpc_dma_irq(int irq, void *data)
  312. {
  313. struct mpc_dma *mdma = data;
  314. uint es;
  315. /* Save error status register */
  316. es = in_be32(&mdma->regs->dmaes);
  317. spin_lock(&mdma->error_status_lock);
  318. if ((es & MPC_DMA_DMAES_VLD) && mdma->error_status == 0)
  319. mdma->error_status = es;
  320. spin_unlock(&mdma->error_status_lock);
  321. /* Handle interrupt on each channel */
  322. if (mdma->dma.chancnt > 32) {
  323. mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmainth),
  324. in_be32(&mdma->regs->dmaerrh), 32);
  325. }
  326. mpc_dma_irq_process(mdma, in_be32(&mdma->regs->dmaintl),
  327. in_be32(&mdma->regs->dmaerrl), 0);
  328. /* Schedule tasklet */
  329. tasklet_schedule(&mdma->tasklet);
  330. return IRQ_HANDLED;
  331. }
  332. /* process completed descriptors */
  333. static void mpc_dma_process_completed(struct mpc_dma *mdma)
  334. {
  335. dma_cookie_t last_cookie = 0;
  336. struct mpc_dma_chan *mchan;
  337. struct mpc_dma_desc *mdesc;
  338. struct dma_async_tx_descriptor *desc;
  339. unsigned long flags;
  340. LIST_HEAD(list);
  341. int i;
  342. for (i = 0; i < mdma->dma.chancnt; i++) {
  343. mchan = &mdma->channels[i];
  344. /* Get all completed descriptors */
  345. spin_lock_irqsave(&mchan->lock, flags);
  346. if (!list_empty(&mchan->completed))
  347. list_splice_tail_init(&mchan->completed, &list);
  348. spin_unlock_irqrestore(&mchan->lock, flags);
  349. if (list_empty(&list))
  350. continue;
  351. /* Execute callbacks and run dependencies */
  352. list_for_each_entry(mdesc, &list, node) {
  353. desc = &mdesc->desc;
  354. if (desc->callback)
  355. desc->callback(desc->callback_param);
  356. last_cookie = desc->cookie;
  357. dma_run_dependencies(desc);
  358. }
  359. /* Free descriptors */
  360. spin_lock_irqsave(&mchan->lock, flags);
  361. list_splice_tail_init(&list, &mchan->free);
  362. mchan->chan.completed_cookie = last_cookie;
  363. spin_unlock_irqrestore(&mchan->lock, flags);
  364. }
  365. }
  366. /* DMA Tasklet */
  367. static void mpc_dma_tasklet(unsigned long data)
  368. {
  369. struct mpc_dma *mdma = (void *)data;
  370. unsigned long flags;
  371. uint es;
  372. spin_lock_irqsave(&mdma->error_status_lock, flags);
  373. es = mdma->error_status;
  374. mdma->error_status = 0;
  375. spin_unlock_irqrestore(&mdma->error_status_lock, flags);
  376. /* Print nice error report */
  377. if (es) {
  378. dev_err(mdma->dma.dev,
  379. "Hardware reported following error(s) on channel %u:\n",
  380. MPC_DMA_DMAES_ERRCHN(es));
  381. if (es & MPC_DMA_DMAES_GPE)
  382. dev_err(mdma->dma.dev, "- Group Priority Error\n");
  383. if (es & MPC_DMA_DMAES_CPE)
  384. dev_err(mdma->dma.dev, "- Channel Priority Error\n");
  385. if (es & MPC_DMA_DMAES_SAE)
  386. dev_err(mdma->dma.dev, "- Source Address Error\n");
  387. if (es & MPC_DMA_DMAES_SOE)
  388. dev_err(mdma->dma.dev, "- Source Offset"
  389. " Configuration Error\n");
  390. if (es & MPC_DMA_DMAES_DAE)
  391. dev_err(mdma->dma.dev, "- Destination Address"
  392. " Error\n");
  393. if (es & MPC_DMA_DMAES_DOE)
  394. dev_err(mdma->dma.dev, "- Destination Offset"
  395. " Configuration Error\n");
  396. if (es & MPC_DMA_DMAES_NCE)
  397. dev_err(mdma->dma.dev, "- NBytes/Citter"
  398. " Configuration Error\n");
  399. if (es & MPC_DMA_DMAES_SGE)
  400. dev_err(mdma->dma.dev, "- Scatter/Gather"
  401. " Configuration Error\n");
  402. if (es & MPC_DMA_DMAES_SBE)
  403. dev_err(mdma->dma.dev, "- Source Bus Error\n");
  404. if (es & MPC_DMA_DMAES_DBE)
  405. dev_err(mdma->dma.dev, "- Destination Bus Error\n");
  406. }
  407. mpc_dma_process_completed(mdma);
  408. }
  409. /* Submit descriptor to hardware */
  410. static dma_cookie_t mpc_dma_tx_submit(struct dma_async_tx_descriptor *txd)
  411. {
  412. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(txd->chan);
  413. struct mpc_dma_desc *mdesc;
  414. unsigned long flags;
  415. dma_cookie_t cookie;
  416. mdesc = container_of(txd, struct mpc_dma_desc, desc);
  417. spin_lock_irqsave(&mchan->lock, flags);
  418. /* Move descriptor to queue */
  419. list_move_tail(&mdesc->node, &mchan->queued);
  420. /* If channel is idle, execute all queued descriptors */
  421. if (list_empty(&mchan->active))
  422. mpc_dma_execute(mchan);
  423. /* Update cookie */
  424. cookie = dma_cookie_assign(txd);
  425. spin_unlock_irqrestore(&mchan->lock, flags);
  426. return cookie;
  427. }
  428. /* Alloc channel resources */
  429. static int mpc_dma_alloc_chan_resources(struct dma_chan *chan)
  430. {
  431. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  432. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  433. struct mpc_dma_desc *mdesc;
  434. struct mpc_dma_tcd *tcd;
  435. dma_addr_t tcd_paddr;
  436. unsigned long flags;
  437. LIST_HEAD(descs);
  438. int i;
  439. /* Alloc DMA memory for Transfer Control Descriptors */
  440. tcd = dma_alloc_coherent(mdma->dma.dev,
  441. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  442. &tcd_paddr, GFP_KERNEL);
  443. if (!tcd)
  444. return -ENOMEM;
  445. /* Alloc descriptors for this channel */
  446. for (i = 0; i < MPC_DMA_DESCRIPTORS; i++) {
  447. mdesc = kzalloc(sizeof(struct mpc_dma_desc), GFP_KERNEL);
  448. if (!mdesc) {
  449. dev_notice(mdma->dma.dev, "Memory allocation error. "
  450. "Allocated only %u descriptors\n", i);
  451. break;
  452. }
  453. dma_async_tx_descriptor_init(&mdesc->desc, chan);
  454. mdesc->desc.flags = DMA_CTRL_ACK;
  455. mdesc->desc.tx_submit = mpc_dma_tx_submit;
  456. mdesc->tcd = &tcd[i];
  457. mdesc->tcd_paddr = tcd_paddr + (i * sizeof(struct mpc_dma_tcd));
  458. list_add_tail(&mdesc->node, &descs);
  459. }
  460. /* Return error only if no descriptors were allocated */
  461. if (i == 0) {
  462. dma_free_coherent(mdma->dma.dev,
  463. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  464. tcd, tcd_paddr);
  465. return -ENOMEM;
  466. }
  467. spin_lock_irqsave(&mchan->lock, flags);
  468. mchan->tcd = tcd;
  469. mchan->tcd_paddr = tcd_paddr;
  470. list_splice_tail_init(&descs, &mchan->free);
  471. spin_unlock_irqrestore(&mchan->lock, flags);
  472. /* Enable Error Interrupt */
  473. out_8(&mdma->regs->dmaseei, chan->chan_id);
  474. return 0;
  475. }
  476. /* Free channel resources */
  477. static void mpc_dma_free_chan_resources(struct dma_chan *chan)
  478. {
  479. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  480. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  481. struct mpc_dma_desc *mdesc, *tmp;
  482. struct mpc_dma_tcd *tcd;
  483. dma_addr_t tcd_paddr;
  484. unsigned long flags;
  485. LIST_HEAD(descs);
  486. spin_lock_irqsave(&mchan->lock, flags);
  487. /* Channel must be idle */
  488. BUG_ON(!list_empty(&mchan->prepared));
  489. BUG_ON(!list_empty(&mchan->queued));
  490. BUG_ON(!list_empty(&mchan->active));
  491. BUG_ON(!list_empty(&mchan->completed));
  492. /* Move data */
  493. list_splice_tail_init(&mchan->free, &descs);
  494. tcd = mchan->tcd;
  495. tcd_paddr = mchan->tcd_paddr;
  496. spin_unlock_irqrestore(&mchan->lock, flags);
  497. /* Free DMA memory used by descriptors */
  498. dma_free_coherent(mdma->dma.dev,
  499. MPC_DMA_DESCRIPTORS * sizeof(struct mpc_dma_tcd),
  500. tcd, tcd_paddr);
  501. /* Free descriptors */
  502. list_for_each_entry_safe(mdesc, tmp, &descs, node)
  503. kfree(mdesc);
  504. /* Disable Error Interrupt */
  505. out_8(&mdma->regs->dmaceei, chan->chan_id);
  506. }
  507. /* Send all pending descriptor to hardware */
  508. static void mpc_dma_issue_pending(struct dma_chan *chan)
  509. {
  510. /*
  511. * We are posting descriptors to the hardware as soon as
  512. * they are ready, so this function does nothing.
  513. */
  514. }
  515. /* Check request completion status */
  516. static enum dma_status
  517. mpc_dma_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  518. struct dma_tx_state *txstate)
  519. {
  520. return dma_cookie_status(chan, cookie, txstate);
  521. }
  522. /* Prepare descriptor for memory to memory copy */
  523. static struct dma_async_tx_descriptor *
  524. mpc_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  525. size_t len, unsigned long flags)
  526. {
  527. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  528. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  529. struct mpc_dma_desc *mdesc = NULL;
  530. struct mpc_dma_tcd *tcd;
  531. unsigned long iflags;
  532. /* Get free descriptor */
  533. spin_lock_irqsave(&mchan->lock, iflags);
  534. if (!list_empty(&mchan->free)) {
  535. mdesc = list_first_entry(&mchan->free, struct mpc_dma_desc,
  536. node);
  537. list_del(&mdesc->node);
  538. }
  539. spin_unlock_irqrestore(&mchan->lock, iflags);
  540. if (!mdesc) {
  541. /* try to free completed descriptors */
  542. mpc_dma_process_completed(mdma);
  543. return NULL;
  544. }
  545. mdesc->error = 0;
  546. mdesc->will_access_peripheral = 0;
  547. tcd = mdesc->tcd;
  548. /* Prepare Transfer Control Descriptor for this transaction */
  549. memset(tcd, 0, sizeof(struct mpc_dma_tcd));
  550. if (IS_ALIGNED(src | dst | len, 32)) {
  551. tcd->ssize = MPC_DMA_TSIZE_32;
  552. tcd->dsize = MPC_DMA_TSIZE_32;
  553. tcd->soff = 32;
  554. tcd->doff = 32;
  555. } else if (!mdma->is_mpc8308 && IS_ALIGNED(src | dst | len, 16)) {
  556. /* MPC8308 doesn't support 16 byte transfers */
  557. tcd->ssize = MPC_DMA_TSIZE_16;
  558. tcd->dsize = MPC_DMA_TSIZE_16;
  559. tcd->soff = 16;
  560. tcd->doff = 16;
  561. } else if (IS_ALIGNED(src | dst | len, 4)) {
  562. tcd->ssize = MPC_DMA_TSIZE_4;
  563. tcd->dsize = MPC_DMA_TSIZE_4;
  564. tcd->soff = 4;
  565. tcd->doff = 4;
  566. } else if (IS_ALIGNED(src | dst | len, 2)) {
  567. tcd->ssize = MPC_DMA_TSIZE_2;
  568. tcd->dsize = MPC_DMA_TSIZE_2;
  569. tcd->soff = 2;
  570. tcd->doff = 2;
  571. } else {
  572. tcd->ssize = MPC_DMA_TSIZE_1;
  573. tcd->dsize = MPC_DMA_TSIZE_1;
  574. tcd->soff = 1;
  575. tcd->doff = 1;
  576. }
  577. tcd->saddr = src;
  578. tcd->daddr = dst;
  579. tcd->nbytes = len;
  580. tcd->biter = 1;
  581. tcd->citer = 1;
  582. /* Place descriptor in prepared list */
  583. spin_lock_irqsave(&mchan->lock, iflags);
  584. list_add_tail(&mdesc->node, &mchan->prepared);
  585. spin_unlock_irqrestore(&mchan->lock, iflags);
  586. return &mdesc->desc;
  587. }
  588. static struct dma_async_tx_descriptor *
  589. mpc_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  590. unsigned int sg_len, enum dma_transfer_direction direction,
  591. unsigned long flags, void *context)
  592. {
  593. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  594. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  595. struct mpc_dma_desc *mdesc = NULL;
  596. dma_addr_t per_paddr;
  597. u32 tcd_nunits;
  598. struct mpc_dma_tcd *tcd;
  599. unsigned long iflags;
  600. struct scatterlist *sg;
  601. size_t len;
  602. int iter, i;
  603. /* Currently there is no proper support for scatter/gather */
  604. if (sg_len != 1)
  605. return NULL;
  606. if (!is_slave_direction(direction))
  607. return NULL;
  608. for_each_sg(sgl, sg, sg_len, i) {
  609. spin_lock_irqsave(&mchan->lock, iflags);
  610. mdesc = list_first_entry(&mchan->free,
  611. struct mpc_dma_desc, node);
  612. if (!mdesc) {
  613. spin_unlock_irqrestore(&mchan->lock, iflags);
  614. /* Try to free completed descriptors */
  615. mpc_dma_process_completed(mdma);
  616. return NULL;
  617. }
  618. list_del(&mdesc->node);
  619. if (direction == DMA_DEV_TO_MEM) {
  620. per_paddr = mchan->src_per_paddr;
  621. tcd_nunits = mchan->src_tcd_nunits;
  622. } else {
  623. per_paddr = mchan->dst_per_paddr;
  624. tcd_nunits = mchan->dst_tcd_nunits;
  625. }
  626. spin_unlock_irqrestore(&mchan->lock, iflags);
  627. if (per_paddr == 0 || tcd_nunits == 0)
  628. goto err_prep;
  629. mdesc->error = 0;
  630. mdesc->will_access_peripheral = 1;
  631. /* Prepare Transfer Control Descriptor for this transaction */
  632. tcd = mdesc->tcd;
  633. memset(tcd, 0, sizeof(struct mpc_dma_tcd));
  634. if (!IS_ALIGNED(sg_dma_address(sg), 4))
  635. goto err_prep;
  636. if (direction == DMA_DEV_TO_MEM) {
  637. tcd->saddr = per_paddr;
  638. tcd->daddr = sg_dma_address(sg);
  639. tcd->soff = 0;
  640. tcd->doff = 4;
  641. } else {
  642. tcd->saddr = sg_dma_address(sg);
  643. tcd->daddr = per_paddr;
  644. tcd->soff = 4;
  645. tcd->doff = 0;
  646. }
  647. tcd->ssize = MPC_DMA_TSIZE_4;
  648. tcd->dsize = MPC_DMA_TSIZE_4;
  649. len = sg_dma_len(sg);
  650. tcd->nbytes = tcd_nunits * 4;
  651. if (!IS_ALIGNED(len, tcd->nbytes))
  652. goto err_prep;
  653. iter = len / tcd->nbytes;
  654. if (iter >= 1 << 15) {
  655. /* len is too big */
  656. goto err_prep;
  657. }
  658. /* citer_linkch contains the high bits of iter */
  659. tcd->biter = iter & 0x1ff;
  660. tcd->biter_linkch = iter >> 9;
  661. tcd->citer = tcd->biter;
  662. tcd->citer_linkch = tcd->biter_linkch;
  663. tcd->e_sg = 0;
  664. tcd->d_req = 1;
  665. /* Place descriptor in prepared list */
  666. spin_lock_irqsave(&mchan->lock, iflags);
  667. list_add_tail(&mdesc->node, &mchan->prepared);
  668. spin_unlock_irqrestore(&mchan->lock, iflags);
  669. }
  670. return &mdesc->desc;
  671. err_prep:
  672. /* Put the descriptor back */
  673. spin_lock_irqsave(&mchan->lock, iflags);
  674. list_add_tail(&mdesc->node, &mchan->free);
  675. spin_unlock_irqrestore(&mchan->lock, iflags);
  676. return NULL;
  677. }
  678. static int mpc_dma_device_config(struct dma_chan *chan,
  679. struct dma_slave_config *cfg)
  680. {
  681. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  682. unsigned long flags;
  683. /*
  684. * Software constraints:
  685. * - only transfers between a peripheral device and
  686. * memory are supported;
  687. * - only peripheral devices with 4-byte FIFO access register
  688. * are supported;
  689. * - minimal transfer chunk is 4 bytes and consequently
  690. * source and destination addresses must be 4-byte aligned
  691. * and transfer size must be aligned on (4 * maxburst)
  692. * boundary;
  693. * - during the transfer RAM address is being incremented by
  694. * the size of minimal transfer chunk;
  695. * - peripheral port's address is constant during the transfer.
  696. */
  697. if (cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES ||
  698. cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES ||
  699. !IS_ALIGNED(cfg->src_addr, 4) ||
  700. !IS_ALIGNED(cfg->dst_addr, 4)) {
  701. return -EINVAL;
  702. }
  703. spin_lock_irqsave(&mchan->lock, flags);
  704. mchan->src_per_paddr = cfg->src_addr;
  705. mchan->src_tcd_nunits = cfg->src_maxburst;
  706. mchan->dst_per_paddr = cfg->dst_addr;
  707. mchan->dst_tcd_nunits = cfg->dst_maxburst;
  708. /* Apply defaults */
  709. if (mchan->src_tcd_nunits == 0)
  710. mchan->src_tcd_nunits = 1;
  711. if (mchan->dst_tcd_nunits == 0)
  712. mchan->dst_tcd_nunits = 1;
  713. spin_unlock_irqrestore(&mchan->lock, flags);
  714. return 0;
  715. }
  716. static int mpc_dma_device_terminate_all(struct dma_chan *chan)
  717. {
  718. struct mpc_dma_chan *mchan = dma_chan_to_mpc_dma_chan(chan);
  719. struct mpc_dma *mdma = dma_chan_to_mpc_dma(chan);
  720. unsigned long flags;
  721. /* Disable channel requests */
  722. spin_lock_irqsave(&mchan->lock, flags);
  723. out_8(&mdma->regs->dmacerq, chan->chan_id);
  724. list_splice_tail_init(&mchan->prepared, &mchan->free);
  725. list_splice_tail_init(&mchan->queued, &mchan->free);
  726. list_splice_tail_init(&mchan->active, &mchan->free);
  727. spin_unlock_irqrestore(&mchan->lock, flags);
  728. return 0;
  729. }
  730. static int mpc_dma_probe(struct platform_device *op)
  731. {
  732. struct device_node *dn = op->dev.of_node;
  733. struct device *dev = &op->dev;
  734. struct dma_device *dma;
  735. struct mpc_dma *mdma;
  736. struct mpc_dma_chan *mchan;
  737. struct resource res;
  738. ulong regs_start, regs_size;
  739. int retval, i;
  740. u8 chancnt;
  741. mdma = devm_kzalloc(dev, sizeof(struct mpc_dma), GFP_KERNEL);
  742. if (!mdma) {
  743. dev_err(dev, "Memory exhausted!\n");
  744. retval = -ENOMEM;
  745. goto err;
  746. }
  747. mdma->irq = irq_of_parse_and_map(dn, 0);
  748. if (mdma->irq == NO_IRQ) {
  749. dev_err(dev, "Error mapping IRQ!\n");
  750. retval = -EINVAL;
  751. goto err;
  752. }
  753. if (of_device_is_compatible(dn, "fsl,mpc8308-dma")) {
  754. mdma->is_mpc8308 = 1;
  755. mdma->irq2 = irq_of_parse_and_map(dn, 1);
  756. if (mdma->irq2 == NO_IRQ) {
  757. dev_err(dev, "Error mapping IRQ!\n");
  758. retval = -EINVAL;
  759. goto err_dispose1;
  760. }
  761. }
  762. retval = of_address_to_resource(dn, 0, &res);
  763. if (retval) {
  764. dev_err(dev, "Error parsing memory region!\n");
  765. goto err_dispose2;
  766. }
  767. regs_start = res.start;
  768. regs_size = resource_size(&res);
  769. if (!devm_request_mem_region(dev, regs_start, regs_size, DRV_NAME)) {
  770. dev_err(dev, "Error requesting memory region!\n");
  771. retval = -EBUSY;
  772. goto err_dispose2;
  773. }
  774. mdma->regs = devm_ioremap(dev, regs_start, regs_size);
  775. if (!mdma->regs) {
  776. dev_err(dev, "Error mapping memory region!\n");
  777. retval = -ENOMEM;
  778. goto err_dispose2;
  779. }
  780. mdma->tcd = (struct mpc_dma_tcd *)((u8 *)(mdma->regs)
  781. + MPC_DMA_TCD_OFFSET);
  782. retval = request_irq(mdma->irq, &mpc_dma_irq, 0, DRV_NAME, mdma);
  783. if (retval) {
  784. dev_err(dev, "Error requesting IRQ!\n");
  785. retval = -EINVAL;
  786. goto err_dispose2;
  787. }
  788. if (mdma->is_mpc8308) {
  789. retval = request_irq(mdma->irq2, &mpc_dma_irq, 0,
  790. DRV_NAME, mdma);
  791. if (retval) {
  792. dev_err(dev, "Error requesting IRQ2!\n");
  793. retval = -EINVAL;
  794. goto err_free1;
  795. }
  796. }
  797. spin_lock_init(&mdma->error_status_lock);
  798. dma = &mdma->dma;
  799. dma->dev = dev;
  800. dma->device_alloc_chan_resources = mpc_dma_alloc_chan_resources;
  801. dma->device_free_chan_resources = mpc_dma_free_chan_resources;
  802. dma->device_issue_pending = mpc_dma_issue_pending;
  803. dma->device_tx_status = mpc_dma_tx_status;
  804. dma->device_prep_dma_memcpy = mpc_dma_prep_memcpy;
  805. dma->device_prep_slave_sg = mpc_dma_prep_slave_sg;
  806. dma->device_config = mpc_dma_device_config;
  807. dma->device_terminate_all = mpc_dma_device_terminate_all;
  808. INIT_LIST_HEAD(&dma->channels);
  809. dma_cap_set(DMA_MEMCPY, dma->cap_mask);
  810. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  811. if (mdma->is_mpc8308)
  812. chancnt = MPC8308_DMACHAN_MAX;
  813. else
  814. chancnt = MPC512x_DMACHAN_MAX;
  815. for (i = 0; i < chancnt; i++) {
  816. mchan = &mdma->channels[i];
  817. mchan->chan.device = dma;
  818. dma_cookie_init(&mchan->chan);
  819. INIT_LIST_HEAD(&mchan->free);
  820. INIT_LIST_HEAD(&mchan->prepared);
  821. INIT_LIST_HEAD(&mchan->queued);
  822. INIT_LIST_HEAD(&mchan->active);
  823. INIT_LIST_HEAD(&mchan->completed);
  824. spin_lock_init(&mchan->lock);
  825. list_add_tail(&mchan->chan.device_node, &dma->channels);
  826. }
  827. tasklet_init(&mdma->tasklet, mpc_dma_tasklet, (unsigned long)mdma);
  828. /*
  829. * Configure DMA Engine:
  830. * - Dynamic clock,
  831. * - Round-robin group arbitration,
  832. * - Round-robin channel arbitration.
  833. */
  834. if (mdma->is_mpc8308) {
  835. /* MPC8308 has 16 channels and lacks some registers */
  836. out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_ERCA);
  837. /* enable snooping */
  838. out_be32(&mdma->regs->dmagpor, MPC_DMA_DMAGPOR_SNOOP_ENABLE);
  839. /* Disable error interrupts */
  840. out_be32(&mdma->regs->dmaeeil, 0);
  841. /* Clear interrupts status */
  842. out_be32(&mdma->regs->dmaintl, 0xFFFF);
  843. out_be32(&mdma->regs->dmaerrl, 0xFFFF);
  844. } else {
  845. out_be32(&mdma->regs->dmacr, MPC_DMA_DMACR_EDCG |
  846. MPC_DMA_DMACR_ERGA | MPC_DMA_DMACR_ERCA);
  847. /* Disable hardware DMA requests */
  848. out_be32(&mdma->regs->dmaerqh, 0);
  849. out_be32(&mdma->regs->dmaerql, 0);
  850. /* Disable error interrupts */
  851. out_be32(&mdma->regs->dmaeeih, 0);
  852. out_be32(&mdma->regs->dmaeeil, 0);
  853. /* Clear interrupts status */
  854. out_be32(&mdma->regs->dmainth, 0xFFFFFFFF);
  855. out_be32(&mdma->regs->dmaintl, 0xFFFFFFFF);
  856. out_be32(&mdma->regs->dmaerrh, 0xFFFFFFFF);
  857. out_be32(&mdma->regs->dmaerrl, 0xFFFFFFFF);
  858. /* Route interrupts to IPIC */
  859. out_be32(&mdma->regs->dmaihsa, 0);
  860. out_be32(&mdma->regs->dmailsa, 0);
  861. }
  862. /* Register DMA engine */
  863. dev_set_drvdata(dev, mdma);
  864. retval = dma_async_device_register(dma);
  865. if (retval)
  866. goto err_free2;
  867. /* Register with OF helpers for DMA lookups (nonfatal) */
  868. if (dev->of_node) {
  869. retval = of_dma_controller_register(dev->of_node,
  870. of_dma_xlate_by_chan_id, mdma);
  871. if (retval)
  872. dev_warn(dev, "Could not register for OF lookup\n");
  873. }
  874. return 0;
  875. err_free2:
  876. if (mdma->is_mpc8308)
  877. free_irq(mdma->irq2, mdma);
  878. err_free1:
  879. free_irq(mdma->irq, mdma);
  880. err_dispose2:
  881. if (mdma->is_mpc8308)
  882. irq_dispose_mapping(mdma->irq2);
  883. err_dispose1:
  884. irq_dispose_mapping(mdma->irq);
  885. err:
  886. return retval;
  887. }
  888. static int mpc_dma_remove(struct platform_device *op)
  889. {
  890. struct device *dev = &op->dev;
  891. struct mpc_dma *mdma = dev_get_drvdata(dev);
  892. if (dev->of_node)
  893. of_dma_controller_free(dev->of_node);
  894. dma_async_device_unregister(&mdma->dma);
  895. if (mdma->is_mpc8308) {
  896. free_irq(mdma->irq2, mdma);
  897. irq_dispose_mapping(mdma->irq2);
  898. }
  899. free_irq(mdma->irq, mdma);
  900. irq_dispose_mapping(mdma->irq);
  901. return 0;
  902. }
  903. static const struct of_device_id mpc_dma_match[] = {
  904. { .compatible = "fsl,mpc5121-dma", },
  905. { .compatible = "fsl,mpc8308-dma", },
  906. {},
  907. };
  908. MODULE_DEVICE_TABLE(of, mpc_dma_match);
  909. static struct platform_driver mpc_dma_driver = {
  910. .probe = mpc_dma_probe,
  911. .remove = mpc_dma_remove,
  912. .driver = {
  913. .name = DRV_NAME,
  914. .of_match_table = mpc_dma_match,
  915. },
  916. };
  917. module_platform_driver(mpc_dma_driver);
  918. MODULE_LICENSE("GPL");
  919. MODULE_AUTHOR("Piotr Ziecik <kosmo@semihalf.com>");