imx-dma.c 34 KB

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