dma-mx1-mx2.c 23 KB

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