imx-dma.c 35 KB

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