imx-dma.c 34 KB

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