imx-dma.c 34 KB

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