dma-mx1-mx2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * linux/arch/arm/plat-mxc/dma-mx1-mx2.c
  3. *
  4. * i.MX DMA registration and IRQ dispatching
  5. *
  6. * Copyright 2006 Pavel Pisa <pisa@cmp.felk.cvut.cz>
  7. * Copyright 2008 Juergen Beisert, <kernel@pengutronix.de>
  8. * Copyright 2008 Sascha Hauer, <s.hauer@pengutronix.de>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/errno.h>
  29. #include <linux/clk.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/io.h>
  32. #include <asm/system.h>
  33. #include <asm/irq.h>
  34. #include <mach/hardware.h>
  35. #include <mach/dma-mx1-mx2.h>
  36. #define DMA_DCR 0x00 /* Control Register */
  37. #define DMA_DISR 0x04 /* Interrupt status Register */
  38. #define DMA_DIMR 0x08 /* Interrupt mask Register */
  39. #define DMA_DBTOSR 0x0c /* Burst timeout status Register */
  40. #define DMA_DRTOSR 0x10 /* Request timeout Register */
  41. #define DMA_DSESR 0x14 /* Transfer Error Status Register */
  42. #define DMA_DBOSR 0x18 /* Buffer overflow status Register */
  43. #define DMA_DBTOCR 0x1c /* Burst timeout control Register */
  44. #define DMA_WSRA 0x40 /* W-Size Register A */
  45. #define DMA_XSRA 0x44 /* X-Size Register A */
  46. #define DMA_YSRA 0x48 /* Y-Size Register A */
  47. #define DMA_WSRB 0x4c /* W-Size Register B */
  48. #define DMA_XSRB 0x50 /* X-Size Register B */
  49. #define DMA_YSRB 0x54 /* Y-Size Register B */
  50. #define DMA_SAR(x) (0x80 + ((x) << 6)) /* Source Address Registers */
  51. #define DMA_DAR(x) (0x84 + ((x) << 6)) /* Destination Address Registers */
  52. #define DMA_CNTR(x) (0x88 + ((x) << 6)) /* Count Registers */
  53. #define DMA_CCR(x) (0x8c + ((x) << 6)) /* Control Registers */
  54. #define DMA_RSSR(x) (0x90 + ((x) << 6)) /* Request source select Registers */
  55. #define DMA_BLR(x) (0x94 + ((x) << 6)) /* Burst length Registers */
  56. #define DMA_RTOR(x) (0x98 + ((x) << 6)) /* Request timeout Registers */
  57. #define DMA_BUCR(x) (0x98 + ((x) << 6)) /* Bus Utilization Registers */
  58. #define DMA_CCNR(x) (0x9C + ((x) << 6)) /* Channel counter Registers */
  59. #define DCR_DRST (1<<1)
  60. #define DCR_DEN (1<<0)
  61. #define DBTOCR_EN (1<<15)
  62. #define DBTOCR_CNT(x) ((x) & 0x7fff)
  63. #define CNTR_CNT(x) ((x) & 0xffffff)
  64. #define CCR_ACRPT (1<<14)
  65. #define CCR_DMOD_LINEAR (0x0 << 12)
  66. #define CCR_DMOD_2D (0x1 << 12)
  67. #define CCR_DMOD_FIFO (0x2 << 12)
  68. #define CCR_DMOD_EOBFIFO (0x3 << 12)
  69. #define CCR_SMOD_LINEAR (0x0 << 10)
  70. #define CCR_SMOD_2D (0x1 << 10)
  71. #define CCR_SMOD_FIFO (0x2 << 10)
  72. #define CCR_SMOD_EOBFIFO (0x3 << 10)
  73. #define CCR_MDIR_DEC (1<<9)
  74. #define CCR_MSEL_B (1<<8)
  75. #define CCR_DSIZ_32 (0x0 << 6)
  76. #define CCR_DSIZ_8 (0x1 << 6)
  77. #define CCR_DSIZ_16 (0x2 << 6)
  78. #define CCR_SSIZ_32 (0x0 << 4)
  79. #define CCR_SSIZ_8 (0x1 << 4)
  80. #define CCR_SSIZ_16 (0x2 << 4)
  81. #define CCR_REN (1<<3)
  82. #define CCR_RPT (1<<2)
  83. #define CCR_FRC (1<<1)
  84. #define CCR_CEN (1<<0)
  85. #define RTOR_EN (1<<15)
  86. #define RTOR_CLK (1<<14)
  87. #define RTOR_PSC (1<<13)
  88. /*
  89. * struct imx_dma_channel - i.MX specific DMA extension
  90. * @name: name specified by DMA client
  91. * @irq_handler: client callback for end of transfer
  92. * @err_handler: client callback for error condition
  93. * @data: clients context data for callbacks
  94. * @dma_mode: direction of the transfer %DMA_MODE_READ or %DMA_MODE_WRITE
  95. * @sg: pointer to the actual read/written chunk for scatter-gather emulation
  96. * @resbytes: total residual number of bytes to transfer
  97. * (it can be lower or same as sum of SG mapped chunk sizes)
  98. * @sgcount: number of chunks to be read/written
  99. *
  100. * Structure is used for IMX DMA processing. It would be probably good
  101. * @struct dma_struct in the future for external interfacing and use
  102. * @struct imx_dma_channel only as extension to it.
  103. */
  104. struct imx_dma_channel {
  105. const char *name;
  106. void (*irq_handler) (int, void *);
  107. void (*err_handler) (int, void *, int errcode);
  108. void (*prog_handler) (int, void *, struct scatterlist *);
  109. void *data;
  110. unsigned int dma_mode;
  111. struct scatterlist *sg;
  112. unsigned int resbytes;
  113. int dma_num;
  114. int in_use;
  115. u32 ccr_from_device;
  116. u32 ccr_to_device;
  117. struct timer_list watchdog;
  118. int hw_chaining;
  119. };
  120. static struct imx_dma_channel imx_dma_channels[IMX_DMA_CHANNELS];
  121. static struct clk *dma_clk;
  122. static int imx_dma_hw_chain(struct imx_dma_channel *imxdma)
  123. {
  124. if (cpu_is_mx27())
  125. return imxdma->hw_chaining;
  126. else
  127. return 0;
  128. }
  129. /*
  130. * imx_dma_sg_next - prepare next chunk for scatter-gather DMA emulation
  131. */
  132. static inline int imx_dma_sg_next(int channel, struct scatterlist *sg)
  133. {
  134. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  135. unsigned long now;
  136. if (!imxdma->name) {
  137. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  138. __func__, channel);
  139. return 0;
  140. }
  141. now = min(imxdma->resbytes, sg->length);
  142. imxdma->resbytes -= now;
  143. if ((imxdma->dma_mode & DMA_MODE_MASK) == DMA_MODE_READ)
  144. __raw_writel(sg->dma_address, DMA_BASE + DMA_DAR(channel));
  145. else
  146. __raw_writel(sg->dma_address, DMA_BASE + DMA_SAR(channel));
  147. __raw_writel(now, DMA_BASE + DMA_CNTR(channel));
  148. pr_debug("imxdma%d: next sg chunk dst 0x%08x, src 0x%08x, "
  149. "size 0x%08x\n", channel,
  150. __raw_readl(DMA_BASE + DMA_DAR(channel)),
  151. __raw_readl(DMA_BASE + DMA_SAR(channel)),
  152. __raw_readl(DMA_BASE + DMA_CNTR(channel)));
  153. return now;
  154. }
  155. /**
  156. * imx_dma_setup_single - setup i.MX DMA channel for linear memory to/from
  157. * device transfer
  158. *
  159. * @channel: i.MX DMA channel number
  160. * @dma_address: the DMA/physical memory address of the linear data block
  161. * to transfer
  162. * @dma_length: length of the data block in bytes
  163. * @dev_addr: physical device port address
  164. * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
  165. * or %DMA_MODE_WRITE from memory to the device
  166. *
  167. * Return value: if incorrect parameters are provided -%EINVAL.
  168. * Zero indicates success.
  169. */
  170. int
  171. imx_dma_setup_single(int channel, dma_addr_t dma_address,
  172. unsigned int dma_length, unsigned int dev_addr,
  173. unsigned int dmamode)
  174. {
  175. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  176. imxdma->sg = NULL;
  177. imxdma->dma_mode = dmamode;
  178. if (!dma_address) {
  179. printk(KERN_ERR "imxdma%d: imx_dma_setup_single null address\n",
  180. channel);
  181. return -EINVAL;
  182. }
  183. if (!dma_length) {
  184. printk(KERN_ERR "imxdma%d: imx_dma_setup_single zero length\n",
  185. channel);
  186. return -EINVAL;
  187. }
  188. if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
  189. pr_debug("imxdma%d: %s dma_addressg=0x%08x dma_length=%d "
  190. "dev_addr=0x%08x for read\n",
  191. channel, __func__, (unsigned int)dma_address,
  192. dma_length, dev_addr);
  193. __raw_writel(dev_addr, DMA_BASE + DMA_SAR(channel));
  194. __raw_writel(dma_address, DMA_BASE + DMA_DAR(channel));
  195. __raw_writel(imxdma->ccr_from_device,
  196. DMA_BASE + DMA_CCR(channel));
  197. } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
  198. pr_debug("imxdma%d: %s dma_addressg=0x%08x dma_length=%d "
  199. "dev_addr=0x%08x for write\n",
  200. channel, __func__, (unsigned int)dma_address,
  201. dma_length, dev_addr);
  202. __raw_writel(dma_address, DMA_BASE + DMA_SAR(channel));
  203. __raw_writel(dev_addr, DMA_BASE + DMA_DAR(channel));
  204. __raw_writel(imxdma->ccr_to_device,
  205. DMA_BASE + DMA_CCR(channel));
  206. } else {
  207. printk(KERN_ERR "imxdma%d: imx_dma_setup_single bad dmamode\n",
  208. channel);
  209. return -EINVAL;
  210. }
  211. __raw_writel(dma_length, DMA_BASE + DMA_CNTR(channel));
  212. return 0;
  213. }
  214. EXPORT_SYMBOL(imx_dma_setup_single);
  215. /**
  216. * imx_dma_setup_sg - setup i.MX DMA channel SG list to/from device transfer
  217. * @channel: i.MX DMA channel number
  218. * @sg: pointer to the scatter-gather list/vector
  219. * @sgcount: scatter-gather list hungs count
  220. * @dma_length: total length of the transfer request in bytes
  221. * @dev_addr: physical device port address
  222. * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
  223. * or %DMA_MODE_WRITE from memory to the device
  224. *
  225. * The function sets up DMA channel state and registers to be ready for
  226. * transfer specified by provided parameters. The scatter-gather emulation
  227. * is set up according to the parameters.
  228. *
  229. * The full preparation of the transfer requires setup of more register
  230. * by the caller before imx_dma_enable() can be called.
  231. *
  232. * %BLR(channel) holds transfer burst length in bytes, 0 means 64 bytes
  233. *
  234. * %RSSR(channel) has to be set to the DMA request line source %DMA_REQ_xxx
  235. *
  236. * %CCR(channel) has to specify transfer parameters, the next settings is
  237. * typical for linear or simple scatter-gather transfers if %DMA_MODE_READ is
  238. * specified
  239. *
  240. * %CCR_DMOD_LINEAR | %CCR_DSIZ_32 | %CCR_SMOD_FIFO | %CCR_SSIZ_x
  241. *
  242. * The typical setup for %DMA_MODE_WRITE is specified by next options
  243. * combination
  244. *
  245. * %CCR_SMOD_LINEAR | %CCR_SSIZ_32 | %CCR_DMOD_FIFO | %CCR_DSIZ_x
  246. *
  247. * Be careful here and do not mistakenly mix source and target device
  248. * port sizes constants, they are really different:
  249. * %CCR_SSIZ_8, %CCR_SSIZ_16, %CCR_SSIZ_32,
  250. * %CCR_DSIZ_8, %CCR_DSIZ_16, %CCR_DSIZ_32
  251. *
  252. * Return value: if incorrect parameters are provided -%EINVAL.
  253. * Zero indicates success.
  254. */
  255. int
  256. imx_dma_setup_sg(int channel,
  257. struct scatterlist *sg, unsigned int sgcount,
  258. unsigned int dma_length, unsigned int dev_addr,
  259. unsigned int dmamode)
  260. {
  261. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  262. if (imxdma->in_use)
  263. return -EBUSY;
  264. imxdma->sg = sg;
  265. imxdma->dma_mode = dmamode;
  266. imxdma->resbytes = dma_length;
  267. if (!sg || !sgcount) {
  268. printk(KERN_ERR "imxdma%d: imx_dma_setup_sg epty sg list\n",
  269. channel);
  270. return -EINVAL;
  271. }
  272. if (!sg->length) {
  273. printk(KERN_ERR "imxdma%d: imx_dma_setup_sg zero length\n",
  274. channel);
  275. return -EINVAL;
  276. }
  277. if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
  278. pr_debug("imxdma%d: %s sg=%p sgcount=%d total length=%d "
  279. "dev_addr=0x%08x for read\n",
  280. channel, __func__, sg, sgcount, dma_length, dev_addr);
  281. __raw_writel(dev_addr, DMA_BASE + DMA_SAR(channel));
  282. __raw_writel(imxdma->ccr_from_device,
  283. DMA_BASE + DMA_CCR(channel));
  284. } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
  285. pr_debug("imxdma%d: %s sg=%p sgcount=%d total length=%d "
  286. "dev_addr=0x%08x for write\n",
  287. channel, __func__, sg, sgcount, dma_length, dev_addr);
  288. __raw_writel(dev_addr, DMA_BASE + DMA_DAR(channel));
  289. __raw_writel(imxdma->ccr_to_device,
  290. DMA_BASE + DMA_CCR(channel));
  291. } else {
  292. printk(KERN_ERR "imxdma%d: imx_dma_setup_sg bad dmamode\n",
  293. channel);
  294. return -EINVAL;
  295. }
  296. imx_dma_sg_next(channel, sg);
  297. return 0;
  298. }
  299. EXPORT_SYMBOL(imx_dma_setup_sg);
  300. int
  301. imx_dma_config_channel(int channel, unsigned int config_port,
  302. unsigned int config_mem, unsigned int dmareq, int hw_chaining)
  303. {
  304. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  305. u32 dreq = 0;
  306. imxdma->hw_chaining = 0;
  307. if (hw_chaining) {
  308. imxdma->hw_chaining = 1;
  309. if (!imx_dma_hw_chain(imxdma))
  310. return -EINVAL;
  311. }
  312. if (dmareq)
  313. dreq = CCR_REN;
  314. imxdma->ccr_from_device = config_port | (config_mem << 2) | dreq;
  315. imxdma->ccr_to_device = config_mem | (config_port << 2) | dreq;
  316. __raw_writel(dmareq, DMA_BASE + DMA_RSSR(channel));
  317. return 0;
  318. }
  319. EXPORT_SYMBOL(imx_dma_config_channel);
  320. void imx_dma_config_burstlen(int channel, unsigned int burstlen)
  321. {
  322. __raw_writel(burstlen, DMA_BASE + DMA_BLR(channel));
  323. }
  324. EXPORT_SYMBOL(imx_dma_config_burstlen);
  325. /**
  326. * imx_dma_setup_handlers - setup i.MX DMA channel end and error notification
  327. * handlers
  328. * @channel: i.MX DMA channel number
  329. * @irq_handler: the pointer to the function called if the transfer
  330. * ends successfully
  331. * @err_handler: the pointer to the function called if the premature
  332. * end caused by error occurs
  333. * @data: user specified value to be passed to the handlers
  334. */
  335. int
  336. imx_dma_setup_handlers(int channel,
  337. void (*irq_handler) (int, void *),
  338. void (*err_handler) (int, void *, int),
  339. void *data)
  340. {
  341. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  342. unsigned long flags;
  343. if (!imxdma->name) {
  344. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  345. __func__, channel);
  346. return -ENODEV;
  347. }
  348. local_irq_save(flags);
  349. __raw_writel(1 << channel, DMA_BASE + DMA_DISR);
  350. imxdma->irq_handler = irq_handler;
  351. imxdma->err_handler = err_handler;
  352. imxdma->data = data;
  353. local_irq_restore(flags);
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(imx_dma_setup_handlers);
  357. /**
  358. * imx_dma_setup_progression_handler - setup i.MX DMA channel progression
  359. * handlers
  360. * @channel: i.MX DMA channel number
  361. * @prog_handler: the pointer to the function called if the transfer progresses
  362. */
  363. int
  364. imx_dma_setup_progression_handler(int channel,
  365. void (*prog_handler) (int, void*, struct scatterlist*))
  366. {
  367. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  368. unsigned long flags;
  369. if (!imxdma->name) {
  370. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  371. __func__, channel);
  372. return -ENODEV;
  373. }
  374. local_irq_save(flags);
  375. imxdma->prog_handler = prog_handler;
  376. local_irq_restore(flags);
  377. return 0;
  378. }
  379. EXPORT_SYMBOL(imx_dma_setup_progression_handler);
  380. /**
  381. * imx_dma_enable - function to start i.MX DMA channel operation
  382. * @channel: i.MX DMA channel number
  383. *
  384. * The channel has to be allocated by driver through imx_dma_request()
  385. * or imx_dma_request_by_prio() function.
  386. * The transfer parameters has to be set to the channel registers through
  387. * call of the imx_dma_setup_single() or imx_dma_setup_sg() function
  388. * and registers %BLR(channel), %RSSR(channel) and %CCR(channel) has to
  389. * be set prior this function call by the channel user.
  390. */
  391. void imx_dma_enable(int channel)
  392. {
  393. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  394. unsigned long flags;
  395. pr_debug("imxdma%d: imx_dma_enable\n", channel);
  396. if (!imxdma->name) {
  397. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  398. __func__, channel);
  399. return;
  400. }
  401. if (imxdma->in_use)
  402. return;
  403. local_irq_save(flags);
  404. __raw_writel(1 << channel, DMA_BASE + DMA_DISR);
  405. __raw_writel(__raw_readl(DMA_BASE + DMA_DIMR) & ~(1 << channel),
  406. DMA_BASE + DMA_DIMR);
  407. __raw_writel(__raw_readl(DMA_BASE + DMA_CCR(channel)) | CCR_CEN |
  408. CCR_ACRPT,
  409. DMA_BASE + DMA_CCR(channel));
  410. #ifdef CONFIG_ARCH_MX2
  411. if (imxdma->sg && imx_dma_hw_chain(imxdma)) {
  412. imxdma->sg = sg_next(imxdma->sg);
  413. if (imxdma->sg) {
  414. u32 tmp;
  415. imx_dma_sg_next(channel, imxdma->sg);
  416. tmp = __raw_readl(DMA_BASE + DMA_CCR(channel));
  417. __raw_writel(tmp | CCR_RPT | CCR_ACRPT,
  418. DMA_BASE + DMA_CCR(channel));
  419. }
  420. }
  421. #endif
  422. imxdma->in_use = 1;
  423. local_irq_restore(flags);
  424. }
  425. EXPORT_SYMBOL(imx_dma_enable);
  426. /**
  427. * imx_dma_disable - stop, finish i.MX DMA channel operatin
  428. * @channel: i.MX DMA channel number
  429. */
  430. void imx_dma_disable(int channel)
  431. {
  432. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  433. unsigned long flags;
  434. pr_debug("imxdma%d: imx_dma_disable\n", channel);
  435. if (imx_dma_hw_chain(imxdma))
  436. del_timer(&imxdma->watchdog);
  437. local_irq_save(flags);
  438. __raw_writel(__raw_readl(DMA_BASE + DMA_DIMR) | (1 << channel),
  439. DMA_BASE + DMA_DIMR);
  440. __raw_writel(__raw_readl(DMA_BASE + DMA_CCR(channel)) & ~CCR_CEN,
  441. DMA_BASE + DMA_CCR(channel));
  442. __raw_writel(1 << channel, DMA_BASE + DMA_DISR);
  443. imxdma->in_use = 0;
  444. local_irq_restore(flags);
  445. }
  446. EXPORT_SYMBOL(imx_dma_disable);
  447. #ifdef CONFIG_ARCH_MX2
  448. static void imx_dma_watchdog(unsigned long chno)
  449. {
  450. struct imx_dma_channel *imxdma = &imx_dma_channels[chno];
  451. __raw_writel(0, DMA_BASE + DMA_CCR(chno));
  452. imxdma->in_use = 0;
  453. imxdma->sg = NULL;
  454. if (imxdma->err_handler)
  455. imxdma->err_handler(chno, imxdma->data, IMX_DMA_ERR_TIMEOUT);
  456. }
  457. #endif
  458. static irqreturn_t dma_err_handler(int irq, void *dev_id)
  459. {
  460. int i, disr;
  461. struct imx_dma_channel *imxdma;
  462. unsigned int err_mask;
  463. int errcode;
  464. disr = __raw_readl(DMA_BASE + DMA_DISR);
  465. err_mask = __raw_readl(DMA_BASE + DMA_DBTOSR) |
  466. __raw_readl(DMA_BASE + DMA_DRTOSR) |
  467. __raw_readl(DMA_BASE + DMA_DSESR) |
  468. __raw_readl(DMA_BASE + DMA_DBOSR);
  469. if (!err_mask)
  470. return IRQ_HANDLED;
  471. __raw_writel(disr & err_mask, DMA_BASE + DMA_DISR);
  472. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  473. if (!(err_mask & (1 << i)))
  474. continue;
  475. imxdma = &imx_dma_channels[i];
  476. errcode = 0;
  477. if (__raw_readl(DMA_BASE + DMA_DBTOSR) & (1 << i)) {
  478. __raw_writel(1 << i, DMA_BASE + DMA_DBTOSR);
  479. errcode |= IMX_DMA_ERR_BURST;
  480. }
  481. if (__raw_readl(DMA_BASE + DMA_DRTOSR) & (1 << i)) {
  482. __raw_writel(1 << i, DMA_BASE + DMA_DRTOSR);
  483. errcode |= IMX_DMA_ERR_REQUEST;
  484. }
  485. if (__raw_readl(DMA_BASE + DMA_DSESR) & (1 << i)) {
  486. __raw_writel(1 << i, DMA_BASE + DMA_DSESR);
  487. errcode |= IMX_DMA_ERR_TRANSFER;
  488. }
  489. if (__raw_readl(DMA_BASE + DMA_DBOSR) & (1 << i)) {
  490. __raw_writel(1 << i, DMA_BASE + DMA_DBOSR);
  491. errcode |= IMX_DMA_ERR_BUFFER;
  492. }
  493. if (imxdma->name && imxdma->err_handler) {
  494. imxdma->err_handler(i, imxdma->data, errcode);
  495. continue;
  496. }
  497. imx_dma_channels[i].sg = NULL;
  498. printk(KERN_WARNING
  499. "DMA timeout on channel %d (%s) -%s%s%s%s\n",
  500. i, imxdma->name,
  501. errcode & IMX_DMA_ERR_BURST ? " burst" : "",
  502. errcode & IMX_DMA_ERR_REQUEST ? " request" : "",
  503. errcode & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
  504. errcode & IMX_DMA_ERR_BUFFER ? " buffer" : "");
  505. }
  506. return IRQ_HANDLED;
  507. }
  508. static void dma_irq_handle_channel(int chno)
  509. {
  510. struct imx_dma_channel *imxdma = &imx_dma_channels[chno];
  511. if (!imxdma->name) {
  512. /*
  513. * IRQ for an unregistered DMA channel:
  514. * let's clear the interrupts and disable it.
  515. */
  516. printk(KERN_WARNING
  517. "spurious IRQ for DMA channel %d\n", chno);
  518. return;
  519. }
  520. if (imxdma->sg) {
  521. u32 tmp;
  522. struct scatterlist *current_sg = imxdma->sg;
  523. imxdma->sg = sg_next(imxdma->sg);
  524. if (imxdma->sg) {
  525. imx_dma_sg_next(chno, imxdma->sg);
  526. tmp = __raw_readl(DMA_BASE + DMA_CCR(chno));
  527. if (imx_dma_hw_chain(imxdma)) {
  528. /* FIXME: The timeout should probably be
  529. * configurable
  530. */
  531. mod_timer(&imxdma->watchdog,
  532. jiffies + msecs_to_jiffies(500));
  533. tmp |= CCR_CEN | CCR_RPT | CCR_ACRPT;
  534. __raw_writel(tmp, DMA_BASE +
  535. DMA_CCR(chno));
  536. } else {
  537. __raw_writel(tmp & ~CCR_CEN, DMA_BASE +
  538. DMA_CCR(chno));
  539. tmp |= CCR_CEN;
  540. }
  541. __raw_writel(tmp, DMA_BASE + DMA_CCR(chno));
  542. if (imxdma->prog_handler)
  543. imxdma->prog_handler(chno, imxdma->data,
  544. current_sg);
  545. return;
  546. }
  547. if (imx_dma_hw_chain(imxdma)) {
  548. del_timer(&imxdma->watchdog);
  549. return;
  550. }
  551. }
  552. __raw_writel(0, DMA_BASE + DMA_CCR(chno));
  553. imxdma->in_use = 0;
  554. if (imxdma->irq_handler)
  555. imxdma->irq_handler(chno, imxdma->data);
  556. }
  557. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  558. {
  559. int i, disr;
  560. #ifdef CONFIG_ARCH_MX2
  561. dma_err_handler(irq, dev_id);
  562. #endif
  563. disr = __raw_readl(DMA_BASE + DMA_DISR);
  564. pr_debug("imxdma: dma_irq_handler called, disr=0x%08x\n",
  565. disr);
  566. __raw_writel(disr, DMA_BASE + DMA_DISR);
  567. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  568. if (disr & (1 << i))
  569. dma_irq_handle_channel(i);
  570. }
  571. return IRQ_HANDLED;
  572. }
  573. /**
  574. * imx_dma_request - request/allocate specified channel number
  575. * @channel: i.MX DMA channel number
  576. * @name: the driver/caller own non-%NULL identification
  577. */
  578. int imx_dma_request(int channel, const char *name)
  579. {
  580. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  581. unsigned long flags;
  582. int ret = 0;
  583. /* basic sanity checks */
  584. if (!name)
  585. return -EINVAL;
  586. if (channel >= IMX_DMA_CHANNELS) {
  587. printk(KERN_CRIT "%s: called for non-existed channel %d\n",
  588. __func__, channel);
  589. return -EINVAL;
  590. }
  591. local_irq_save(flags);
  592. if (imxdma->name) {
  593. local_irq_restore(flags);
  594. return -EBUSY;
  595. }
  596. memset(imxdma, 0, sizeof(imxdma));
  597. imxdma->name = name;
  598. local_irq_restore(flags); /* request_irq() can block */
  599. #ifdef CONFIG_ARCH_MX2
  600. ret = request_irq(MXC_INT_DMACH0 + channel, dma_irq_handler, 0, "DMA",
  601. NULL);
  602. if (ret) {
  603. imxdma->name = NULL;
  604. printk(KERN_CRIT "Can't register IRQ %d for DMA channel %d\n",
  605. MXC_INT_DMACH0 + channel, channel);
  606. return ret;
  607. }
  608. init_timer(&imxdma->watchdog);
  609. imxdma->watchdog.function = &imx_dma_watchdog;
  610. imxdma->watchdog.data = channel;
  611. #endif
  612. return ret;
  613. }
  614. EXPORT_SYMBOL(imx_dma_request);
  615. /**
  616. * imx_dma_free - release previously acquired channel
  617. * @channel: i.MX DMA channel number
  618. */
  619. void imx_dma_free(int channel)
  620. {
  621. unsigned long flags;
  622. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  623. if (!imxdma->name) {
  624. printk(KERN_CRIT
  625. "%s: trying to free free channel %d\n",
  626. __func__, channel);
  627. return;
  628. }
  629. local_irq_save(flags);
  630. /* Disable interrupts */
  631. imx_dma_disable(channel);
  632. imxdma->name = NULL;
  633. #ifdef CONFIG_ARCH_MX2
  634. free_irq(MXC_INT_DMACH0 + channel, NULL);
  635. #endif
  636. local_irq_restore(flags);
  637. }
  638. EXPORT_SYMBOL(imx_dma_free);
  639. /**
  640. * imx_dma_request_by_prio - find and request some of free channels best
  641. * suiting requested priority
  642. * @channel: i.MX DMA channel number
  643. * @name: the driver/caller own non-%NULL identification
  644. *
  645. * This function tries to find a free channel in the specified priority group
  646. * This function tries to find a free channel in the specified priority group
  647. * if the priority cannot be achieved it tries to look for free channel
  648. * in the higher and then even lower priority groups.
  649. *
  650. * Return value: If there is no free channel to allocate, -%ENODEV is returned.
  651. * On successful allocation channel is returned.
  652. */
  653. int imx_dma_request_by_prio(const char *name, enum imx_dma_prio prio)
  654. {
  655. int i;
  656. int best;
  657. switch (prio) {
  658. case (DMA_PRIO_HIGH):
  659. best = 8;
  660. break;
  661. case (DMA_PRIO_MEDIUM):
  662. best = 4;
  663. break;
  664. case (DMA_PRIO_LOW):
  665. default:
  666. best = 0;
  667. break;
  668. }
  669. for (i = best; i < IMX_DMA_CHANNELS; i++)
  670. if (!imx_dma_request(i, name))
  671. return i;
  672. for (i = best - 1; i >= 0; i--)
  673. if (!imx_dma_request(i, name))
  674. return i;
  675. printk(KERN_ERR "%s: no free DMA channel found\n", __func__);
  676. return -ENODEV;
  677. }
  678. EXPORT_SYMBOL(imx_dma_request_by_prio);
  679. static int __init imx_dma_init(void)
  680. {
  681. int ret = 0;
  682. int i;
  683. dma_clk = clk_get(NULL, "dma");
  684. clk_enable(dma_clk);
  685. /* reset DMA module */
  686. __raw_writel(DCR_DRST, DMA_BASE + DMA_DCR);
  687. #ifdef CONFIG_ARCH_MX1
  688. ret = request_irq(DMA_INT, dma_irq_handler, 0, "DMA", NULL);
  689. if (ret) {
  690. printk(KERN_CRIT "Wow! Can't register IRQ for DMA\n");
  691. return ret;
  692. }
  693. ret = request_irq(DMA_ERR, dma_err_handler, 0, "DMA", NULL);
  694. if (ret) {
  695. printk(KERN_CRIT "Wow! Can't register ERRIRQ for DMA\n");
  696. free_irq(DMA_INT, NULL);
  697. return ret;
  698. }
  699. #endif
  700. /* enable DMA module */
  701. __raw_writel(DCR_DEN, DMA_BASE + DMA_DCR);
  702. /* clear all interrupts */
  703. __raw_writel((1 << IMX_DMA_CHANNELS) - 1, DMA_BASE + DMA_DISR);
  704. /* disable interrupts */
  705. __raw_writel((1 << IMX_DMA_CHANNELS) - 1, DMA_BASE + DMA_DIMR);
  706. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  707. imx_dma_channels[i].sg = NULL;
  708. imx_dma_channels[i].dma_num = i;
  709. }
  710. return ret;
  711. }
  712. arch_initcall(imx_dma_init);