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