imx-dma.c 32 KB

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