imx-dma.c 32 KB

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