imx-dma.c 34 KB

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