imx-dma.c 34 KB

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