imx-dma.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. * drivers/dma/imx-dma.c
  3. *
  4. * This file contains a driver for the Freescale i.MX DMA engine
  5. * found on i.MX1/21/27
  6. *
  7. * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  8. * Copyright 2012 Javier Martin, Vista Silicon <javier.martin@vista-silicon.com>
  9. *
  10. * The code contained herein is licensed under the GNU General Public
  11. * License. You may obtain a copy of the GNU General Public License
  12. * Version 2 or later at the following locations:
  13. *
  14. * http://www.opensource.org/licenses/gpl-license.html
  15. * http://www.gnu.org/copyleft/gpl.html
  16. */
  17. #include <linux/init.h>
  18. #include <linux/types.h>
  19. #include <linux/mm.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/slab.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/clk.h>
  27. #include <linux/dmaengine.h>
  28. #include <linux/module.h>
  29. #include <asm/irq.h>
  30. #include <linux/platform_data/dma-imx.h>
  31. #include <mach/hardware.h>
  32. #include "dmaengine.h"
  33. #define IMXDMA_MAX_CHAN_DESCRIPTORS 16
  34. #define IMX_DMA_CHANNELS 16
  35. #define IMX_DMA_2D_SLOTS 2
  36. #define IMX_DMA_2D_SLOT_A 0
  37. #define IMX_DMA_2D_SLOT_B 1
  38. #define IMX_DMA_LENGTH_LOOP ((unsigned int)-1)
  39. #define IMX_DMA_MEMSIZE_32 (0 << 4)
  40. #define IMX_DMA_MEMSIZE_8 (1 << 4)
  41. #define IMX_DMA_MEMSIZE_16 (2 << 4)
  42. #define IMX_DMA_TYPE_LINEAR (0 << 10)
  43. #define IMX_DMA_TYPE_2D (1 << 10)
  44. #define IMX_DMA_TYPE_FIFO (2 << 10)
  45. #define IMX_DMA_ERR_BURST (1 << 0)
  46. #define IMX_DMA_ERR_REQUEST (1 << 1)
  47. #define IMX_DMA_ERR_TRANSFER (1 << 2)
  48. #define IMX_DMA_ERR_BUFFER (1 << 3)
  49. #define IMX_DMA_ERR_TIMEOUT (1 << 4)
  50. #define DMA_DCR 0x00 /* Control Register */
  51. #define DMA_DISR 0x04 /* Interrupt status Register */
  52. #define DMA_DIMR 0x08 /* Interrupt mask Register */
  53. #define DMA_DBTOSR 0x0c /* Burst timeout status Register */
  54. #define DMA_DRTOSR 0x10 /* Request timeout Register */
  55. #define DMA_DSESR 0x14 /* Transfer Error Status Register */
  56. #define DMA_DBOSR 0x18 /* Buffer overflow status Register */
  57. #define DMA_DBTOCR 0x1c /* Burst timeout control Register */
  58. #define DMA_WSRA 0x40 /* W-Size Register A */
  59. #define DMA_XSRA 0x44 /* X-Size Register A */
  60. #define DMA_YSRA 0x48 /* Y-Size Register A */
  61. #define DMA_WSRB 0x4c /* W-Size Register B */
  62. #define DMA_XSRB 0x50 /* X-Size Register B */
  63. #define DMA_YSRB 0x54 /* Y-Size Register B */
  64. #define DMA_SAR(x) (0x80 + ((x) << 6)) /* Source Address Registers */
  65. #define DMA_DAR(x) (0x84 + ((x) << 6)) /* Destination Address Registers */
  66. #define DMA_CNTR(x) (0x88 + ((x) << 6)) /* Count Registers */
  67. #define DMA_CCR(x) (0x8c + ((x) << 6)) /* Control Registers */
  68. #define DMA_RSSR(x) (0x90 + ((x) << 6)) /* Request source select Registers */
  69. #define DMA_BLR(x) (0x94 + ((x) << 6)) /* Burst length Registers */
  70. #define DMA_RTOR(x) (0x98 + ((x) << 6)) /* Request timeout Registers */
  71. #define DMA_BUCR(x) (0x98 + ((x) << 6)) /* Bus Utilization Registers */
  72. #define DMA_CCNR(x) (0x9C + ((x) << 6)) /* Channel counter Registers */
  73. #define DCR_DRST (1<<1)
  74. #define DCR_DEN (1<<0)
  75. #define DBTOCR_EN (1<<15)
  76. #define DBTOCR_CNT(x) ((x) & 0x7fff)
  77. #define CNTR_CNT(x) ((x) & 0xffffff)
  78. #define CCR_ACRPT (1<<14)
  79. #define CCR_DMOD_LINEAR (0x0 << 12)
  80. #define CCR_DMOD_2D (0x1 << 12)
  81. #define CCR_DMOD_FIFO (0x2 << 12)
  82. #define CCR_DMOD_EOBFIFO (0x3 << 12)
  83. #define CCR_SMOD_LINEAR (0x0 << 10)
  84. #define CCR_SMOD_2D (0x1 << 10)
  85. #define CCR_SMOD_FIFO (0x2 << 10)
  86. #define CCR_SMOD_EOBFIFO (0x3 << 10)
  87. #define CCR_MDIR_DEC (1<<9)
  88. #define CCR_MSEL_B (1<<8)
  89. #define CCR_DSIZ_32 (0x0 << 6)
  90. #define CCR_DSIZ_8 (0x1 << 6)
  91. #define CCR_DSIZ_16 (0x2 << 6)
  92. #define CCR_SSIZ_32 (0x0 << 4)
  93. #define CCR_SSIZ_8 (0x1 << 4)
  94. #define CCR_SSIZ_16 (0x2 << 4)
  95. #define CCR_REN (1<<3)
  96. #define CCR_RPT (1<<2)
  97. #define CCR_FRC (1<<1)
  98. #define CCR_CEN (1<<0)
  99. #define RTOR_EN (1<<15)
  100. #define RTOR_CLK (1<<14)
  101. #define RTOR_PSC (1<<13)
  102. enum imxdma_prep_type {
  103. IMXDMA_DESC_MEMCPY,
  104. IMXDMA_DESC_INTERLEAVED,
  105. IMXDMA_DESC_SLAVE_SG,
  106. IMXDMA_DESC_CYCLIC,
  107. };
  108. struct imx_dma_2d_config {
  109. u16 xsr;
  110. u16 ysr;
  111. u16 wsr;
  112. int count;
  113. };
  114. struct imxdma_desc {
  115. struct list_head node;
  116. struct dma_async_tx_descriptor desc;
  117. enum dma_status status;
  118. dma_addr_t src;
  119. dma_addr_t dest;
  120. size_t len;
  121. enum dma_transfer_direction direction;
  122. enum imxdma_prep_type type;
  123. /* For memcpy and interleaved */
  124. unsigned int config_port;
  125. unsigned int config_mem;
  126. /* For interleaved transfers */
  127. unsigned int x;
  128. unsigned int y;
  129. unsigned int w;
  130. /* For slave sg and cyclic */
  131. struct scatterlist *sg;
  132. unsigned int sgcount;
  133. };
  134. struct imxdma_channel {
  135. int hw_chaining;
  136. struct timer_list watchdog;
  137. struct imxdma_engine *imxdma;
  138. unsigned int channel;
  139. struct tasklet_struct dma_tasklet;
  140. struct list_head ld_free;
  141. struct list_head ld_queue;
  142. struct list_head ld_active;
  143. int descs_allocated;
  144. enum dma_slave_buswidth word_size;
  145. dma_addr_t per_address;
  146. u32 watermark_level;
  147. struct dma_chan chan;
  148. struct dma_async_tx_descriptor desc;
  149. enum dma_status status;
  150. int dma_request;
  151. struct scatterlist *sg_list;
  152. u32 ccr_from_device;
  153. u32 ccr_to_device;
  154. bool enabled_2d;
  155. int slot_2d;
  156. };
  157. struct imxdma_engine {
  158. struct device *dev;
  159. struct device_dma_parameters dma_parms;
  160. struct dma_device dma_device;
  161. void __iomem *base;
  162. struct clk *dma_ahb;
  163. struct clk *dma_ipg;
  164. spinlock_t lock;
  165. struct imx_dma_2d_config slots_2d[IMX_DMA_2D_SLOTS];
  166. struct imxdma_channel channel[IMX_DMA_CHANNELS];
  167. };
  168. static struct imxdma_channel *to_imxdma_chan(struct dma_chan *chan)
  169. {
  170. return container_of(chan, struct imxdma_channel, chan);
  171. }
  172. static inline bool imxdma_chan_is_doing_cyclic(struct imxdma_channel *imxdmac)
  173. {
  174. struct imxdma_desc *desc;
  175. if (!list_empty(&imxdmac->ld_active)) {
  176. desc = list_first_entry(&imxdmac->ld_active, struct imxdma_desc,
  177. node);
  178. if (desc->type == IMXDMA_DESC_CYCLIC)
  179. return true;
  180. }
  181. return false;
  182. }
  183. static void imx_dmav1_writel(struct imxdma_engine *imxdma, unsigned val,
  184. unsigned offset)
  185. {
  186. __raw_writel(val, imxdma->base + offset);
  187. }
  188. static unsigned imx_dmav1_readl(struct imxdma_engine *imxdma, unsigned offset)
  189. {
  190. return __raw_readl(imxdma->base + offset);
  191. }
  192. static int imxdma_hw_chain(struct imxdma_channel *imxdmac)
  193. {
  194. if (cpu_is_mx27())
  195. return imxdmac->hw_chaining;
  196. else
  197. return 0;
  198. }
  199. /*
  200. * imxdma_sg_next - prepare next chunk for scatter-gather DMA emulation
  201. */
  202. static inline int imxdma_sg_next(struct imxdma_desc *d)
  203. {
  204. struct imxdma_channel *imxdmac = to_imxdma_chan(d->desc.chan);
  205. struct imxdma_engine *imxdma = imxdmac->imxdma;
  206. struct scatterlist *sg = d->sg;
  207. unsigned long now;
  208. now = min(d->len, sg_dma_len(sg));
  209. if (d->len != IMX_DMA_LENGTH_LOOP)
  210. d->len -= now;
  211. if (d->direction == DMA_DEV_TO_MEM)
  212. imx_dmav1_writel(imxdma, sg->dma_address,
  213. DMA_DAR(imxdmac->channel));
  214. else
  215. imx_dmav1_writel(imxdma, sg->dma_address,
  216. DMA_SAR(imxdmac->channel));
  217. imx_dmav1_writel(imxdma, now, DMA_CNTR(imxdmac->channel));
  218. dev_dbg(imxdma->dev, " %s channel: %d dst 0x%08x, src 0x%08x, "
  219. "size 0x%08x\n", __func__, imxdmac->channel,
  220. imx_dmav1_readl(imxdma, DMA_DAR(imxdmac->channel)),
  221. imx_dmav1_readl(imxdma, DMA_SAR(imxdmac->channel)),
  222. imx_dmav1_readl(imxdma, DMA_CNTR(imxdmac->channel)));
  223. return now;
  224. }
  225. static void imxdma_enable_hw(struct imxdma_desc *d)
  226. {
  227. struct imxdma_channel *imxdmac = to_imxdma_chan(d->desc.chan);
  228. struct imxdma_engine *imxdma = imxdmac->imxdma;
  229. int channel = imxdmac->channel;
  230. unsigned long flags;
  231. dev_dbg(imxdma->dev, "%s channel %d\n", __func__, channel);
  232. local_irq_save(flags);
  233. imx_dmav1_writel(imxdma, 1 << channel, DMA_DISR);
  234. imx_dmav1_writel(imxdma, imx_dmav1_readl(imxdma, DMA_DIMR) &
  235. ~(1 << channel), DMA_DIMR);
  236. imx_dmav1_writel(imxdma, imx_dmav1_readl(imxdma, DMA_CCR(channel)) |
  237. CCR_CEN | CCR_ACRPT, DMA_CCR(channel));
  238. if ((cpu_is_mx21() || cpu_is_mx27()) &&
  239. d->sg && imxdma_hw_chain(imxdmac)) {
  240. d->sg = sg_next(d->sg);
  241. if (d->sg) {
  242. u32 tmp;
  243. imxdma_sg_next(d);
  244. tmp = imx_dmav1_readl(imxdma, DMA_CCR(channel));
  245. imx_dmav1_writel(imxdma, tmp | CCR_RPT | CCR_ACRPT,
  246. DMA_CCR(channel));
  247. }
  248. }
  249. local_irq_restore(flags);
  250. }
  251. static void imxdma_disable_hw(struct imxdma_channel *imxdmac)
  252. {
  253. struct imxdma_engine *imxdma = imxdmac->imxdma;
  254. int channel = imxdmac->channel;
  255. unsigned long flags;
  256. dev_dbg(imxdma->dev, "%s channel %d\n", __func__, channel);
  257. if (imxdma_hw_chain(imxdmac))
  258. del_timer(&imxdmac->watchdog);
  259. local_irq_save(flags);
  260. imx_dmav1_writel(imxdma, imx_dmav1_readl(imxdma, DMA_DIMR) |
  261. (1 << channel), DMA_DIMR);
  262. imx_dmav1_writel(imxdma, imx_dmav1_readl(imxdma, DMA_CCR(channel)) &
  263. ~CCR_CEN, DMA_CCR(channel));
  264. imx_dmav1_writel(imxdma, 1 << channel, DMA_DISR);
  265. local_irq_restore(flags);
  266. }
  267. static void imxdma_watchdog(unsigned long data)
  268. {
  269. struct imxdma_channel *imxdmac = (struct imxdma_channel *)data;
  270. struct imxdma_engine *imxdma = imxdmac->imxdma;
  271. int channel = imxdmac->channel;
  272. imx_dmav1_writel(imxdma, 0, DMA_CCR(channel));
  273. /* Tasklet watchdog error handler */
  274. tasklet_schedule(&imxdmac->dma_tasklet);
  275. dev_dbg(imxdma->dev, "channel %d: watchdog timeout!\n",
  276. imxdmac->channel);
  277. }
  278. static irqreturn_t imxdma_err_handler(int irq, void *dev_id)
  279. {
  280. struct imxdma_engine *imxdma = dev_id;
  281. unsigned int err_mask;
  282. int i, disr;
  283. int errcode;
  284. disr = imx_dmav1_readl(imxdma, DMA_DISR);
  285. err_mask = imx_dmav1_readl(imxdma, DMA_DBTOSR) |
  286. imx_dmav1_readl(imxdma, DMA_DRTOSR) |
  287. imx_dmav1_readl(imxdma, DMA_DSESR) |
  288. imx_dmav1_readl(imxdma, DMA_DBOSR);
  289. if (!err_mask)
  290. return IRQ_HANDLED;
  291. imx_dmav1_writel(imxdma, disr & err_mask, DMA_DISR);
  292. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  293. if (!(err_mask & (1 << i)))
  294. continue;
  295. errcode = 0;
  296. if (imx_dmav1_readl(imxdma, DMA_DBTOSR) & (1 << i)) {
  297. imx_dmav1_writel(imxdma, 1 << i, DMA_DBTOSR);
  298. errcode |= IMX_DMA_ERR_BURST;
  299. }
  300. if (imx_dmav1_readl(imxdma, DMA_DRTOSR) & (1 << i)) {
  301. imx_dmav1_writel(imxdma, 1 << i, DMA_DRTOSR);
  302. errcode |= IMX_DMA_ERR_REQUEST;
  303. }
  304. if (imx_dmav1_readl(imxdma, DMA_DSESR) & (1 << i)) {
  305. imx_dmav1_writel(imxdma, 1 << i, DMA_DSESR);
  306. errcode |= IMX_DMA_ERR_TRANSFER;
  307. }
  308. if (imx_dmav1_readl(imxdma, DMA_DBOSR) & (1 << i)) {
  309. imx_dmav1_writel(imxdma, 1 << i, DMA_DBOSR);
  310. errcode |= IMX_DMA_ERR_BUFFER;
  311. }
  312. /* Tasklet error handler */
  313. tasklet_schedule(&imxdma->channel[i].dma_tasklet);
  314. printk(KERN_WARNING
  315. "DMA timeout on channel %d -%s%s%s%s\n", i,
  316. errcode & IMX_DMA_ERR_BURST ? " burst" : "",
  317. errcode & IMX_DMA_ERR_REQUEST ? " request" : "",
  318. errcode & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
  319. errcode & IMX_DMA_ERR_BUFFER ? " buffer" : "");
  320. }
  321. return IRQ_HANDLED;
  322. }
  323. static void dma_irq_handle_channel(struct imxdma_channel *imxdmac)
  324. {
  325. struct imxdma_engine *imxdma = imxdmac->imxdma;
  326. int chno = imxdmac->channel;
  327. struct imxdma_desc *desc;
  328. spin_lock(&imxdma->lock);
  329. if (list_empty(&imxdmac->ld_active)) {
  330. spin_unlock(&imxdma->lock);
  331. goto out;
  332. }
  333. desc = list_first_entry(&imxdmac->ld_active,
  334. struct imxdma_desc,
  335. node);
  336. spin_unlock(&imxdma->lock);
  337. if (desc->sg) {
  338. u32 tmp;
  339. desc->sg = sg_next(desc->sg);
  340. if (desc->sg) {
  341. imxdma_sg_next(desc);
  342. tmp = imx_dmav1_readl(imxdma, DMA_CCR(chno));
  343. if (imxdma_hw_chain(imxdmac)) {
  344. /* FIXME: The timeout should probably be
  345. * configurable
  346. */
  347. mod_timer(&imxdmac->watchdog,
  348. jiffies + msecs_to_jiffies(500));
  349. tmp |= CCR_CEN | CCR_RPT | CCR_ACRPT;
  350. imx_dmav1_writel(imxdma, tmp, DMA_CCR(chno));
  351. } else {
  352. imx_dmav1_writel(imxdma, tmp & ~CCR_CEN,
  353. DMA_CCR(chno));
  354. tmp |= CCR_CEN;
  355. }
  356. imx_dmav1_writel(imxdma, tmp, DMA_CCR(chno));
  357. if (imxdma_chan_is_doing_cyclic(imxdmac))
  358. /* Tasklet progression */
  359. tasklet_schedule(&imxdmac->dma_tasklet);
  360. return;
  361. }
  362. if (imxdma_hw_chain(imxdmac)) {
  363. del_timer(&imxdmac->watchdog);
  364. return;
  365. }
  366. }
  367. out:
  368. imx_dmav1_writel(imxdma, 0, DMA_CCR(chno));
  369. /* Tasklet irq */
  370. tasklet_schedule(&imxdmac->dma_tasklet);
  371. }
  372. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  373. {
  374. struct imxdma_engine *imxdma = dev_id;
  375. int i, disr;
  376. if (cpu_is_mx21() || cpu_is_mx27())
  377. imxdma_err_handler(irq, dev_id);
  378. disr = imx_dmav1_readl(imxdma, DMA_DISR);
  379. dev_dbg(imxdma->dev, "%s called, disr=0x%08x\n", __func__, disr);
  380. imx_dmav1_writel(imxdma, disr, DMA_DISR);
  381. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  382. if (disr & (1 << i))
  383. dma_irq_handle_channel(&imxdma->channel[i]);
  384. }
  385. return IRQ_HANDLED;
  386. }
  387. static int imxdma_xfer_desc(struct imxdma_desc *d)
  388. {
  389. struct imxdma_channel *imxdmac = to_imxdma_chan(d->desc.chan);
  390. struct imxdma_engine *imxdma = imxdmac->imxdma;
  391. unsigned long flags;
  392. int slot = -1;
  393. int i;
  394. /* Configure and enable */
  395. switch (d->type) {
  396. case IMXDMA_DESC_INTERLEAVED:
  397. /* Try to get a free 2D slot */
  398. spin_lock_irqsave(&imxdma->lock, flags);
  399. for (i = 0; i < IMX_DMA_2D_SLOTS; i++) {
  400. if ((imxdma->slots_2d[i].count > 0) &&
  401. ((imxdma->slots_2d[i].xsr != d->x) ||
  402. (imxdma->slots_2d[i].ysr != d->y) ||
  403. (imxdma->slots_2d[i].wsr != d->w)))
  404. continue;
  405. slot = i;
  406. break;
  407. }
  408. if (slot < 0) {
  409. spin_unlock_irqrestore(&imxdma->lock, flags);
  410. return -EBUSY;
  411. }
  412. imxdma->slots_2d[slot].xsr = d->x;
  413. imxdma->slots_2d[slot].ysr = d->y;
  414. imxdma->slots_2d[slot].wsr = d->w;
  415. imxdma->slots_2d[slot].count++;
  416. imxdmac->slot_2d = slot;
  417. imxdmac->enabled_2d = true;
  418. spin_unlock_irqrestore(&imxdma->lock, flags);
  419. if (slot == IMX_DMA_2D_SLOT_A) {
  420. d->config_mem &= ~CCR_MSEL_B;
  421. d->config_port &= ~CCR_MSEL_B;
  422. imx_dmav1_writel(imxdma, d->x, DMA_XSRA);
  423. imx_dmav1_writel(imxdma, d->y, DMA_YSRA);
  424. imx_dmav1_writel(imxdma, d->w, DMA_WSRA);
  425. } else {
  426. d->config_mem |= CCR_MSEL_B;
  427. d->config_port |= CCR_MSEL_B;
  428. imx_dmav1_writel(imxdma, d->x, DMA_XSRB);
  429. imx_dmav1_writel(imxdma, d->y, DMA_YSRB);
  430. imx_dmav1_writel(imxdma, d->w, DMA_WSRB);
  431. }
  432. /*
  433. * We fall-through here intentionally, since a 2D transfer is
  434. * similar to MEMCPY just adding the 2D slot configuration.
  435. */
  436. case IMXDMA_DESC_MEMCPY:
  437. imx_dmav1_writel(imxdma, d->src, DMA_SAR(imxdmac->channel));
  438. imx_dmav1_writel(imxdma, d->dest, DMA_DAR(imxdmac->channel));
  439. imx_dmav1_writel(imxdma, d->config_mem | (d->config_port << 2),
  440. DMA_CCR(imxdmac->channel));
  441. imx_dmav1_writel(imxdma, d->len, DMA_CNTR(imxdmac->channel));
  442. dev_dbg(imxdma->dev, "%s channel: %d dest=0x%08x src=0x%08x "
  443. "dma_length=%d\n", __func__, imxdmac->channel,
  444. d->dest, d->src, d->len);
  445. break;
  446. /* Cyclic transfer is the same as slave_sg with special sg configuration. */
  447. case IMXDMA_DESC_CYCLIC:
  448. case IMXDMA_DESC_SLAVE_SG:
  449. if (d->direction == DMA_DEV_TO_MEM) {
  450. imx_dmav1_writel(imxdma, imxdmac->per_address,
  451. DMA_SAR(imxdmac->channel));
  452. imx_dmav1_writel(imxdma, imxdmac->ccr_from_device,
  453. DMA_CCR(imxdmac->channel));
  454. dev_dbg(imxdma->dev, "%s channel: %d sg=%p sgcount=%d "
  455. "total length=%d dev_addr=0x%08x (dev2mem)\n",
  456. __func__, imxdmac->channel, d->sg, d->sgcount,
  457. d->len, imxdmac->per_address);
  458. } else if (d->direction == DMA_MEM_TO_DEV) {
  459. imx_dmav1_writel(imxdma, imxdmac->per_address,
  460. DMA_DAR(imxdmac->channel));
  461. imx_dmav1_writel(imxdma, imxdmac->ccr_to_device,
  462. DMA_CCR(imxdmac->channel));
  463. dev_dbg(imxdma->dev, "%s channel: %d sg=%p sgcount=%d "
  464. "total length=%d dev_addr=0x%08x (mem2dev)\n",
  465. __func__, imxdmac->channel, d->sg, d->sgcount,
  466. d->len, imxdmac->per_address);
  467. } else {
  468. dev_err(imxdma->dev, "%s channel: %d bad dma mode\n",
  469. __func__, imxdmac->channel);
  470. return -EINVAL;
  471. }
  472. imxdma_sg_next(d);
  473. break;
  474. default:
  475. return -EINVAL;
  476. }
  477. imxdma_enable_hw(d);
  478. return 0;
  479. }
  480. static void imxdma_tasklet(unsigned long data)
  481. {
  482. struct imxdma_channel *imxdmac = (void *)data;
  483. struct imxdma_engine *imxdma = imxdmac->imxdma;
  484. struct imxdma_desc *desc;
  485. spin_lock(&imxdma->lock);
  486. if (list_empty(&imxdmac->ld_active)) {
  487. /* Someone might have called terminate all */
  488. goto out;
  489. }
  490. desc = list_first_entry(&imxdmac->ld_active, struct imxdma_desc, node);
  491. if (desc->desc.callback)
  492. desc->desc.callback(desc->desc.callback_param);
  493. /* If we are dealing with a cyclic descriptor, keep it on ld_active
  494. * and dont mark the descriptor as complete.
  495. * Only in non-cyclic cases it would be marked as complete
  496. */
  497. if (imxdma_chan_is_doing_cyclic(imxdmac))
  498. goto out;
  499. else
  500. dma_cookie_complete(&desc->desc);
  501. /* Free 2D slot if it was an interleaved transfer */
  502. if (imxdmac->enabled_2d) {
  503. imxdma->slots_2d[imxdmac->slot_2d].count--;
  504. imxdmac->enabled_2d = false;
  505. }
  506. list_move_tail(imxdmac->ld_active.next, &imxdmac->ld_free);
  507. if (!list_empty(&imxdmac->ld_queue)) {
  508. desc = list_first_entry(&imxdmac->ld_queue, struct imxdma_desc,
  509. node);
  510. list_move_tail(imxdmac->ld_queue.next, &imxdmac->ld_active);
  511. if (imxdma_xfer_desc(desc) < 0)
  512. dev_warn(imxdma->dev, "%s: channel: %d couldn't xfer desc\n",
  513. __func__, imxdmac->channel);
  514. }
  515. out:
  516. spin_unlock(&imxdma->lock);
  517. }
  518. static int imxdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  519. unsigned long arg)
  520. {
  521. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  522. struct dma_slave_config *dmaengine_cfg = (void *)arg;
  523. struct imxdma_engine *imxdma = imxdmac->imxdma;
  524. unsigned long flags;
  525. unsigned int mode = 0;
  526. switch (cmd) {
  527. case DMA_TERMINATE_ALL:
  528. imxdma_disable_hw(imxdmac);
  529. spin_lock_irqsave(&imxdma->lock, flags);
  530. list_splice_tail_init(&imxdmac->ld_active, &imxdmac->ld_free);
  531. list_splice_tail_init(&imxdmac->ld_queue, &imxdmac->ld_free);
  532. spin_unlock_irqrestore(&imxdma->lock, flags);
  533. return 0;
  534. case DMA_SLAVE_CONFIG:
  535. if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
  536. imxdmac->per_address = dmaengine_cfg->src_addr;
  537. imxdmac->watermark_level = dmaengine_cfg->src_maxburst;
  538. imxdmac->word_size = dmaengine_cfg->src_addr_width;
  539. } else {
  540. imxdmac->per_address = dmaengine_cfg->dst_addr;
  541. imxdmac->watermark_level = dmaengine_cfg->dst_maxburst;
  542. imxdmac->word_size = dmaengine_cfg->dst_addr_width;
  543. }
  544. switch (imxdmac->word_size) {
  545. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  546. mode = IMX_DMA_MEMSIZE_8;
  547. break;
  548. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  549. mode = IMX_DMA_MEMSIZE_16;
  550. break;
  551. default:
  552. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  553. mode = IMX_DMA_MEMSIZE_32;
  554. break;
  555. }
  556. imxdmac->hw_chaining = 1;
  557. if (!imxdma_hw_chain(imxdmac))
  558. return -EINVAL;
  559. imxdmac->ccr_from_device = (mode | IMX_DMA_TYPE_FIFO) |
  560. ((IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR) << 2) |
  561. CCR_REN;
  562. imxdmac->ccr_to_device =
  563. (IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR) |
  564. ((mode | IMX_DMA_TYPE_FIFO) << 2) | CCR_REN;
  565. imx_dmav1_writel(imxdma, imxdmac->dma_request,
  566. DMA_RSSR(imxdmac->channel));
  567. /* Set burst length */
  568. imx_dmav1_writel(imxdma, imxdmac->watermark_level *
  569. imxdmac->word_size, DMA_BLR(imxdmac->channel));
  570. return 0;
  571. default:
  572. return -ENOSYS;
  573. }
  574. return -EINVAL;
  575. }
  576. static enum dma_status imxdma_tx_status(struct dma_chan *chan,
  577. dma_cookie_t cookie,
  578. struct dma_tx_state *txstate)
  579. {
  580. return dma_cookie_status(chan, cookie, txstate);
  581. }
  582. static dma_cookie_t imxdma_tx_submit(struct dma_async_tx_descriptor *tx)
  583. {
  584. struct imxdma_channel *imxdmac = to_imxdma_chan(tx->chan);
  585. struct imxdma_engine *imxdma = imxdmac->imxdma;
  586. dma_cookie_t cookie;
  587. unsigned long flags;
  588. spin_lock_irqsave(&imxdma->lock, flags);
  589. list_move_tail(imxdmac->ld_free.next, &imxdmac->ld_queue);
  590. cookie = dma_cookie_assign(tx);
  591. spin_unlock_irqrestore(&imxdma->lock, flags);
  592. return cookie;
  593. }
  594. static int imxdma_alloc_chan_resources(struct dma_chan *chan)
  595. {
  596. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  597. struct imx_dma_data *data = chan->private;
  598. if (data != NULL)
  599. imxdmac->dma_request = data->dma_request;
  600. while (imxdmac->descs_allocated < IMXDMA_MAX_CHAN_DESCRIPTORS) {
  601. struct imxdma_desc *desc;
  602. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  603. if (!desc)
  604. break;
  605. __memzero(&desc->desc, sizeof(struct dma_async_tx_descriptor));
  606. dma_async_tx_descriptor_init(&desc->desc, chan);
  607. desc->desc.tx_submit = imxdma_tx_submit;
  608. /* txd.flags will be overwritten in prep funcs */
  609. desc->desc.flags = DMA_CTRL_ACK;
  610. desc->status = DMA_SUCCESS;
  611. list_add_tail(&desc->node, &imxdmac->ld_free);
  612. imxdmac->descs_allocated++;
  613. }
  614. if (!imxdmac->descs_allocated)
  615. return -ENOMEM;
  616. return imxdmac->descs_allocated;
  617. }
  618. static void imxdma_free_chan_resources(struct dma_chan *chan)
  619. {
  620. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  621. struct imxdma_engine *imxdma = imxdmac->imxdma;
  622. struct imxdma_desc *desc, *_desc;
  623. unsigned long flags;
  624. spin_lock_irqsave(&imxdma->lock, flags);
  625. imxdma_disable_hw(imxdmac);
  626. list_splice_tail_init(&imxdmac->ld_active, &imxdmac->ld_free);
  627. list_splice_tail_init(&imxdmac->ld_queue, &imxdmac->ld_free);
  628. spin_unlock_irqrestore(&imxdma->lock, flags);
  629. list_for_each_entry_safe(desc, _desc, &imxdmac->ld_free, node) {
  630. kfree(desc);
  631. imxdmac->descs_allocated--;
  632. }
  633. INIT_LIST_HEAD(&imxdmac->ld_free);
  634. if (imxdmac->sg_list) {
  635. kfree(imxdmac->sg_list);
  636. imxdmac->sg_list = NULL;
  637. }
  638. }
  639. static struct dma_async_tx_descriptor *imxdma_prep_slave_sg(
  640. struct dma_chan *chan, struct scatterlist *sgl,
  641. unsigned int sg_len, enum dma_transfer_direction direction,
  642. unsigned long flags, void *context)
  643. {
  644. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  645. struct scatterlist *sg;
  646. int i, dma_length = 0;
  647. struct imxdma_desc *desc;
  648. if (list_empty(&imxdmac->ld_free) ||
  649. imxdma_chan_is_doing_cyclic(imxdmac))
  650. return NULL;
  651. desc = list_first_entry(&imxdmac->ld_free, struct imxdma_desc, node);
  652. for_each_sg(sgl, sg, sg_len, i) {
  653. dma_length += sg_dma_len(sg);
  654. }
  655. switch (imxdmac->word_size) {
  656. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  657. if (sg_dma_len(sgl) & 3 || sgl->dma_address & 3)
  658. return NULL;
  659. break;
  660. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  661. if (sg_dma_len(sgl) & 1 || sgl->dma_address & 1)
  662. return NULL;
  663. break;
  664. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  665. break;
  666. default:
  667. return NULL;
  668. }
  669. desc->type = IMXDMA_DESC_SLAVE_SG;
  670. desc->sg = sgl;
  671. desc->sgcount = sg_len;
  672. desc->len = dma_length;
  673. desc->direction = direction;
  674. if (direction == DMA_DEV_TO_MEM) {
  675. desc->src = imxdmac->per_address;
  676. } else {
  677. desc->dest = imxdmac->per_address;
  678. }
  679. desc->desc.callback = NULL;
  680. desc->desc.callback_param = NULL;
  681. return &desc->desc;
  682. }
  683. static struct dma_async_tx_descriptor *imxdma_prep_dma_cyclic(
  684. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  685. size_t period_len, enum dma_transfer_direction direction,
  686. unsigned long flags, void *context)
  687. {
  688. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  689. struct imxdma_engine *imxdma = imxdmac->imxdma;
  690. struct imxdma_desc *desc;
  691. int i;
  692. unsigned int periods = buf_len / period_len;
  693. dev_dbg(imxdma->dev, "%s channel: %d buf_len=%d period_len=%d\n",
  694. __func__, imxdmac->channel, buf_len, period_len);
  695. if (list_empty(&imxdmac->ld_free) ||
  696. imxdma_chan_is_doing_cyclic(imxdmac))
  697. return NULL;
  698. desc = list_first_entry(&imxdmac->ld_free, struct imxdma_desc, node);
  699. if (imxdmac->sg_list)
  700. kfree(imxdmac->sg_list);
  701. imxdmac->sg_list = kcalloc(periods + 1,
  702. sizeof(struct scatterlist), GFP_KERNEL);
  703. if (!imxdmac->sg_list)
  704. return NULL;
  705. sg_init_table(imxdmac->sg_list, periods);
  706. for (i = 0; i < periods; i++) {
  707. imxdmac->sg_list[i].page_link = 0;
  708. imxdmac->sg_list[i].offset = 0;
  709. imxdmac->sg_list[i].dma_address = dma_addr;
  710. sg_dma_len(&imxdmac->sg_list[i]) = period_len;
  711. dma_addr += period_len;
  712. }
  713. /* close the loop */
  714. imxdmac->sg_list[periods].offset = 0;
  715. sg_dma_len(&imxdmac->sg_list[periods]) = 0;
  716. imxdmac->sg_list[periods].page_link =
  717. ((unsigned long)imxdmac->sg_list | 0x01) & ~0x02;
  718. desc->type = IMXDMA_DESC_CYCLIC;
  719. desc->sg = imxdmac->sg_list;
  720. desc->sgcount = periods;
  721. desc->len = IMX_DMA_LENGTH_LOOP;
  722. desc->direction = direction;
  723. if (direction == DMA_DEV_TO_MEM) {
  724. desc->src = imxdmac->per_address;
  725. } else {
  726. desc->dest = imxdmac->per_address;
  727. }
  728. desc->desc.callback = NULL;
  729. desc->desc.callback_param = NULL;
  730. return &desc->desc;
  731. }
  732. static struct dma_async_tx_descriptor *imxdma_prep_dma_memcpy(
  733. struct dma_chan *chan, dma_addr_t dest,
  734. dma_addr_t src, size_t len, unsigned long flags)
  735. {
  736. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  737. struct imxdma_engine *imxdma = imxdmac->imxdma;
  738. struct imxdma_desc *desc;
  739. dev_dbg(imxdma->dev, "%s channel: %d src=0x%x dst=0x%x len=%d\n",
  740. __func__, imxdmac->channel, src, dest, len);
  741. if (list_empty(&imxdmac->ld_free) ||
  742. imxdma_chan_is_doing_cyclic(imxdmac))
  743. return NULL;
  744. desc = list_first_entry(&imxdmac->ld_free, struct imxdma_desc, node);
  745. desc->type = IMXDMA_DESC_MEMCPY;
  746. desc->src = src;
  747. desc->dest = dest;
  748. desc->len = len;
  749. desc->direction = DMA_MEM_TO_MEM;
  750. desc->config_port = IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR;
  751. desc->config_mem = IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR;
  752. desc->desc.callback = NULL;
  753. desc->desc.callback_param = NULL;
  754. return &desc->desc;
  755. }
  756. static struct dma_async_tx_descriptor *imxdma_prep_dma_interleaved(
  757. struct dma_chan *chan, struct dma_interleaved_template *xt,
  758. unsigned long flags)
  759. {
  760. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  761. struct imxdma_engine *imxdma = imxdmac->imxdma;
  762. struct imxdma_desc *desc;
  763. dev_dbg(imxdma->dev, "%s channel: %d src_start=0x%x dst_start=0x%x\n"
  764. " src_sgl=%s dst_sgl=%s numf=%d frame_size=%d\n", __func__,
  765. imxdmac->channel, xt->src_start, xt->dst_start,
  766. xt->src_sgl ? "true" : "false", xt->dst_sgl ? "true" : "false",
  767. xt->numf, xt->frame_size);
  768. if (list_empty(&imxdmac->ld_free) ||
  769. imxdma_chan_is_doing_cyclic(imxdmac))
  770. return NULL;
  771. if (xt->frame_size != 1 || xt->numf <= 0 || xt->dir != DMA_MEM_TO_MEM)
  772. return NULL;
  773. desc = list_first_entry(&imxdmac->ld_free, struct imxdma_desc, node);
  774. desc->type = IMXDMA_DESC_INTERLEAVED;
  775. desc->src = xt->src_start;
  776. desc->dest = xt->dst_start;
  777. desc->x = xt->sgl[0].size;
  778. desc->y = xt->numf;
  779. desc->w = xt->sgl[0].icg + desc->x;
  780. desc->len = desc->x * desc->y;
  781. desc->direction = DMA_MEM_TO_MEM;
  782. desc->config_port = IMX_DMA_MEMSIZE_32;
  783. desc->config_mem = IMX_DMA_MEMSIZE_32;
  784. if (xt->src_sgl)
  785. desc->config_mem |= IMX_DMA_TYPE_2D;
  786. if (xt->dst_sgl)
  787. desc->config_port |= IMX_DMA_TYPE_2D;
  788. desc->desc.callback = NULL;
  789. desc->desc.callback_param = NULL;
  790. return &desc->desc;
  791. }
  792. static void imxdma_issue_pending(struct dma_chan *chan)
  793. {
  794. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  795. struct imxdma_engine *imxdma = imxdmac->imxdma;
  796. struct imxdma_desc *desc;
  797. unsigned long flags;
  798. spin_lock_irqsave(&imxdma->lock, flags);
  799. if (list_empty(&imxdmac->ld_active) &&
  800. !list_empty(&imxdmac->ld_queue)) {
  801. desc = list_first_entry(&imxdmac->ld_queue,
  802. struct imxdma_desc, node);
  803. if (imxdma_xfer_desc(desc) < 0) {
  804. dev_warn(imxdma->dev,
  805. "%s: channel: %d couldn't issue DMA xfer\n",
  806. __func__, imxdmac->channel);
  807. } else {
  808. list_move_tail(imxdmac->ld_queue.next,
  809. &imxdmac->ld_active);
  810. }
  811. }
  812. spin_unlock_irqrestore(&imxdma->lock, flags);
  813. }
  814. static int __init imxdma_probe(struct platform_device *pdev)
  815. {
  816. struct imxdma_engine *imxdma;
  817. int ret, i;
  818. imxdma = kzalloc(sizeof(*imxdma), GFP_KERNEL);
  819. if (!imxdma)
  820. return -ENOMEM;
  821. if (cpu_is_mx1()) {
  822. imxdma->base = MX1_IO_ADDRESS(MX1_DMA_BASE_ADDR);
  823. } else if (cpu_is_mx21()) {
  824. imxdma->base = MX21_IO_ADDRESS(MX21_DMA_BASE_ADDR);
  825. } else if (cpu_is_mx27()) {
  826. imxdma->base = MX27_IO_ADDRESS(MX27_DMA_BASE_ADDR);
  827. } else {
  828. kfree(imxdma);
  829. return 0;
  830. }
  831. imxdma->dma_ipg = devm_clk_get(&pdev->dev, "ipg");
  832. if (IS_ERR(imxdma->dma_ipg)) {
  833. ret = PTR_ERR(imxdma->dma_ipg);
  834. goto err_clk;
  835. }
  836. imxdma->dma_ahb = devm_clk_get(&pdev->dev, "ahb");
  837. if (IS_ERR(imxdma->dma_ahb)) {
  838. ret = PTR_ERR(imxdma->dma_ahb);
  839. goto err_clk;
  840. }
  841. clk_prepare_enable(imxdma->dma_ipg);
  842. clk_prepare_enable(imxdma->dma_ahb);
  843. /* reset DMA module */
  844. imx_dmav1_writel(imxdma, DCR_DRST, DMA_DCR);
  845. if (cpu_is_mx1()) {
  846. ret = request_irq(MX1_DMA_INT, dma_irq_handler, 0, "DMA", imxdma);
  847. if (ret) {
  848. dev_warn(imxdma->dev, "Can't register IRQ for DMA\n");
  849. goto err_enable;
  850. }
  851. ret = request_irq(MX1_DMA_ERR, imxdma_err_handler, 0, "DMA", imxdma);
  852. if (ret) {
  853. dev_warn(imxdma->dev, "Can't register ERRIRQ for DMA\n");
  854. free_irq(MX1_DMA_INT, NULL);
  855. goto err_enable;
  856. }
  857. }
  858. /* enable DMA module */
  859. imx_dmav1_writel(imxdma, DCR_DEN, DMA_DCR);
  860. /* clear all interrupts */
  861. imx_dmav1_writel(imxdma, (1 << IMX_DMA_CHANNELS) - 1, DMA_DISR);
  862. /* disable interrupts */
  863. imx_dmav1_writel(imxdma, (1 << IMX_DMA_CHANNELS) - 1, DMA_DIMR);
  864. INIT_LIST_HEAD(&imxdma->dma_device.channels);
  865. dma_cap_set(DMA_SLAVE, imxdma->dma_device.cap_mask);
  866. dma_cap_set(DMA_CYCLIC, imxdma->dma_device.cap_mask);
  867. dma_cap_set(DMA_MEMCPY, imxdma->dma_device.cap_mask);
  868. dma_cap_set(DMA_INTERLEAVE, imxdma->dma_device.cap_mask);
  869. /* Initialize 2D global parameters */
  870. for (i = 0; i < IMX_DMA_2D_SLOTS; i++)
  871. imxdma->slots_2d[i].count = 0;
  872. spin_lock_init(&imxdma->lock);
  873. /* Initialize channel parameters */
  874. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  875. struct imxdma_channel *imxdmac = &imxdma->channel[i];
  876. if (cpu_is_mx21() || cpu_is_mx27()) {
  877. ret = request_irq(MX2x_INT_DMACH0 + i,
  878. dma_irq_handler, 0, "DMA", imxdma);
  879. if (ret) {
  880. dev_warn(imxdma->dev, "Can't register IRQ %d "
  881. "for DMA channel %d\n",
  882. MX2x_INT_DMACH0 + i, i);
  883. goto err_init;
  884. }
  885. init_timer(&imxdmac->watchdog);
  886. imxdmac->watchdog.function = &imxdma_watchdog;
  887. imxdmac->watchdog.data = (unsigned long)imxdmac;
  888. }
  889. imxdmac->imxdma = imxdma;
  890. INIT_LIST_HEAD(&imxdmac->ld_queue);
  891. INIT_LIST_HEAD(&imxdmac->ld_free);
  892. INIT_LIST_HEAD(&imxdmac->ld_active);
  893. tasklet_init(&imxdmac->dma_tasklet, imxdma_tasklet,
  894. (unsigned long)imxdmac);
  895. imxdmac->chan.device = &imxdma->dma_device;
  896. dma_cookie_init(&imxdmac->chan);
  897. imxdmac->channel = i;
  898. /* Add the channel to the DMAC list */
  899. list_add_tail(&imxdmac->chan.device_node,
  900. &imxdma->dma_device.channels);
  901. }
  902. imxdma->dev = &pdev->dev;
  903. imxdma->dma_device.dev = &pdev->dev;
  904. imxdma->dma_device.device_alloc_chan_resources = imxdma_alloc_chan_resources;
  905. imxdma->dma_device.device_free_chan_resources = imxdma_free_chan_resources;
  906. imxdma->dma_device.device_tx_status = imxdma_tx_status;
  907. imxdma->dma_device.device_prep_slave_sg = imxdma_prep_slave_sg;
  908. imxdma->dma_device.device_prep_dma_cyclic = imxdma_prep_dma_cyclic;
  909. imxdma->dma_device.device_prep_dma_memcpy = imxdma_prep_dma_memcpy;
  910. imxdma->dma_device.device_prep_interleaved_dma = imxdma_prep_dma_interleaved;
  911. imxdma->dma_device.device_control = imxdma_control;
  912. imxdma->dma_device.device_issue_pending = imxdma_issue_pending;
  913. platform_set_drvdata(pdev, imxdma);
  914. imxdma->dma_device.copy_align = 2; /* 2^2 = 4 bytes alignment */
  915. imxdma->dma_device.dev->dma_parms = &imxdma->dma_parms;
  916. dma_set_max_seg_size(imxdma->dma_device.dev, 0xffffff);
  917. ret = dma_async_device_register(&imxdma->dma_device);
  918. if (ret) {
  919. dev_err(&pdev->dev, "unable to register\n");
  920. goto err_init;
  921. }
  922. return 0;
  923. err_init:
  924. if (cpu_is_mx21() || cpu_is_mx27()) {
  925. while (--i >= 0)
  926. free_irq(MX2x_INT_DMACH0 + i, NULL);
  927. } else if cpu_is_mx1() {
  928. free_irq(MX1_DMA_INT, NULL);
  929. free_irq(MX1_DMA_ERR, NULL);
  930. }
  931. err_enable:
  932. clk_disable_unprepare(imxdma->dma_ipg);
  933. clk_disable_unprepare(imxdma->dma_ahb);
  934. err_clk:
  935. kfree(imxdma);
  936. return ret;
  937. }
  938. static int __exit imxdma_remove(struct platform_device *pdev)
  939. {
  940. struct imxdma_engine *imxdma = platform_get_drvdata(pdev);
  941. int i;
  942. dma_async_device_unregister(&imxdma->dma_device);
  943. if (cpu_is_mx21() || cpu_is_mx27()) {
  944. for (i = 0; i < IMX_DMA_CHANNELS; i++)
  945. free_irq(MX2x_INT_DMACH0 + i, NULL);
  946. } else if cpu_is_mx1() {
  947. free_irq(MX1_DMA_INT, NULL);
  948. free_irq(MX1_DMA_ERR, NULL);
  949. }
  950. clk_disable_unprepare(imxdma->dma_ipg);
  951. clk_disable_unprepare(imxdma->dma_ahb);
  952. kfree(imxdma);
  953. return 0;
  954. }
  955. static struct platform_driver imxdma_driver = {
  956. .driver = {
  957. .name = "imx-dma",
  958. },
  959. .remove = __exit_p(imxdma_remove),
  960. };
  961. static int __init imxdma_module_init(void)
  962. {
  963. return platform_driver_probe(&imxdma_driver, imxdma_probe);
  964. }
  965. subsys_initcall(imxdma_module_init);
  966. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  967. MODULE_DESCRIPTION("i.MX dma driver");
  968. MODULE_LICENSE("GPL");