imx-dma.c 32 KB

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