dma-mx1-mx2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. if (imxdma->resbytes != IMX_DMA_LENGTH_LOOP)
  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. unsigned int 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. unsigned int 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. #ifdef CONFIG_ARCH_MX2
  449. static void imx_dma_watchdog(unsigned long chno)
  450. {
  451. struct imx_dma_channel *imxdma = &imx_dma_channels[chno];
  452. __raw_writel(0, DMA_BASE + DMA_CCR(chno));
  453. imxdma->in_use = 0;
  454. imxdma->sg = NULL;
  455. if (imxdma->err_handler)
  456. imxdma->err_handler(chno, imxdma->data, IMX_DMA_ERR_TIMEOUT);
  457. }
  458. #endif
  459. static irqreturn_t dma_err_handler(int irq, void *dev_id)
  460. {
  461. int i, disr;
  462. struct imx_dma_channel *imxdma;
  463. unsigned int err_mask;
  464. int errcode;
  465. disr = __raw_readl(DMA_BASE + DMA_DISR);
  466. err_mask = __raw_readl(DMA_BASE + DMA_DBTOSR) |
  467. __raw_readl(DMA_BASE + DMA_DRTOSR) |
  468. __raw_readl(DMA_BASE + DMA_DSESR) |
  469. __raw_readl(DMA_BASE + DMA_DBOSR);
  470. if (!err_mask)
  471. return IRQ_HANDLED;
  472. __raw_writel(disr & err_mask, DMA_BASE + DMA_DISR);
  473. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  474. if (!(err_mask & (1 << i)))
  475. continue;
  476. imxdma = &imx_dma_channels[i];
  477. errcode = 0;
  478. if (__raw_readl(DMA_BASE + DMA_DBTOSR) & (1 << i)) {
  479. __raw_writel(1 << i, DMA_BASE + DMA_DBTOSR);
  480. errcode |= IMX_DMA_ERR_BURST;
  481. }
  482. if (__raw_readl(DMA_BASE + DMA_DRTOSR) & (1 << i)) {
  483. __raw_writel(1 << i, DMA_BASE + DMA_DRTOSR);
  484. errcode |= IMX_DMA_ERR_REQUEST;
  485. }
  486. if (__raw_readl(DMA_BASE + DMA_DSESR) & (1 << i)) {
  487. __raw_writel(1 << i, DMA_BASE + DMA_DSESR);
  488. errcode |= IMX_DMA_ERR_TRANSFER;
  489. }
  490. if (__raw_readl(DMA_BASE + DMA_DBOSR) & (1 << i)) {
  491. __raw_writel(1 << i, DMA_BASE + DMA_DBOSR);
  492. errcode |= IMX_DMA_ERR_BUFFER;
  493. }
  494. if (imxdma->name && imxdma->err_handler) {
  495. imxdma->err_handler(i, imxdma->data, errcode);
  496. continue;
  497. }
  498. imx_dma_channels[i].sg = NULL;
  499. printk(KERN_WARNING
  500. "DMA timeout on channel %d (%s) -%s%s%s%s\n",
  501. i, imxdma->name,
  502. errcode & IMX_DMA_ERR_BURST ? " burst" : "",
  503. errcode & IMX_DMA_ERR_REQUEST ? " request" : "",
  504. errcode & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
  505. errcode & IMX_DMA_ERR_BUFFER ? " buffer" : "");
  506. }
  507. return IRQ_HANDLED;
  508. }
  509. static void dma_irq_handle_channel(int chno)
  510. {
  511. struct imx_dma_channel *imxdma = &imx_dma_channels[chno];
  512. if (!imxdma->name) {
  513. /*
  514. * IRQ for an unregistered DMA channel:
  515. * let's clear the interrupts and disable it.
  516. */
  517. printk(KERN_WARNING
  518. "spurious IRQ for DMA channel %d\n", chno);
  519. return;
  520. }
  521. if (imxdma->sg) {
  522. u32 tmp;
  523. struct scatterlist *current_sg = imxdma->sg;
  524. imxdma->sg = sg_next(imxdma->sg);
  525. if (imxdma->sg) {
  526. imx_dma_sg_next(chno, imxdma->sg);
  527. tmp = __raw_readl(DMA_BASE + DMA_CCR(chno));
  528. if (imx_dma_hw_chain(imxdma)) {
  529. /* FIXME: The timeout should probably be
  530. * configurable
  531. */
  532. mod_timer(&imxdma->watchdog,
  533. jiffies + msecs_to_jiffies(500));
  534. tmp |= CCR_CEN | CCR_RPT | CCR_ACRPT;
  535. __raw_writel(tmp, DMA_BASE +
  536. DMA_CCR(chno));
  537. } else {
  538. __raw_writel(tmp & ~CCR_CEN, DMA_BASE +
  539. DMA_CCR(chno));
  540. tmp |= CCR_CEN;
  541. }
  542. __raw_writel(tmp, DMA_BASE + DMA_CCR(chno));
  543. if (imxdma->prog_handler)
  544. imxdma->prog_handler(chno, imxdma->data,
  545. current_sg);
  546. return;
  547. }
  548. if (imx_dma_hw_chain(imxdma)) {
  549. del_timer(&imxdma->watchdog);
  550. return;
  551. }
  552. }
  553. __raw_writel(0, DMA_BASE + DMA_CCR(chno));
  554. imxdma->in_use = 0;
  555. if (imxdma->irq_handler)
  556. imxdma->irq_handler(chno, imxdma->data);
  557. }
  558. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  559. {
  560. int i, disr;
  561. #ifdef CONFIG_ARCH_MX2
  562. dma_err_handler(irq, dev_id);
  563. #endif
  564. disr = __raw_readl(DMA_BASE + DMA_DISR);
  565. pr_debug("imxdma: dma_irq_handler called, disr=0x%08x\n",
  566. disr);
  567. __raw_writel(disr, DMA_BASE + DMA_DISR);
  568. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  569. if (disr & (1 << i))
  570. dma_irq_handle_channel(i);
  571. }
  572. return IRQ_HANDLED;
  573. }
  574. /**
  575. * imx_dma_request - request/allocate specified channel number
  576. * @channel: i.MX DMA channel number
  577. * @name: the driver/caller own non-%NULL identification
  578. */
  579. int imx_dma_request(int channel, const char *name)
  580. {
  581. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  582. unsigned long flags;
  583. int ret = 0;
  584. /* basic sanity checks */
  585. if (!name)
  586. return -EINVAL;
  587. if (channel >= IMX_DMA_CHANNELS) {
  588. printk(KERN_CRIT "%s: called for non-existed channel %d\n",
  589. __func__, channel);
  590. return -EINVAL;
  591. }
  592. local_irq_save(flags);
  593. if (imxdma->name) {
  594. local_irq_restore(flags);
  595. return -EBUSY;
  596. }
  597. memset(imxdma, 0, sizeof(imxdma));
  598. imxdma->name = name;
  599. local_irq_restore(flags); /* request_irq() can block */
  600. #ifdef CONFIG_ARCH_MX2
  601. ret = request_irq(MXC_INT_DMACH0 + channel, dma_irq_handler, 0, "DMA",
  602. NULL);
  603. if (ret) {
  604. imxdma->name = NULL;
  605. printk(KERN_CRIT "Can't register IRQ %d for DMA channel %d\n",
  606. MXC_INT_DMACH0 + channel, channel);
  607. return ret;
  608. }
  609. init_timer(&imxdma->watchdog);
  610. imxdma->watchdog.function = &imx_dma_watchdog;
  611. imxdma->watchdog.data = channel;
  612. #endif
  613. return ret;
  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. imx_dma_disable(channel);
  633. imxdma->name = NULL;
  634. #ifdef CONFIG_ARCH_MX2
  635. free_irq(MXC_INT_DMACH0 + channel, NULL);
  636. #endif
  637. local_irq_restore(flags);
  638. }
  639. EXPORT_SYMBOL(imx_dma_free);
  640. /**
  641. * imx_dma_request_by_prio - find and request some of free channels best
  642. * suiting requested priority
  643. * @channel: i.MX DMA channel number
  644. * @name: the driver/caller own non-%NULL identification
  645. *
  646. * This function tries to find a free channel in the specified priority group
  647. * This function tries to find a free channel in the specified priority group
  648. * if the priority cannot be achieved it tries to look for free channel
  649. * in the higher and then even lower priority groups.
  650. *
  651. * Return value: If there is no free channel to allocate, -%ENODEV is returned.
  652. * On successful allocation channel is returned.
  653. */
  654. int imx_dma_request_by_prio(const char *name, enum imx_dma_prio prio)
  655. {
  656. int i;
  657. int best;
  658. switch (prio) {
  659. case (DMA_PRIO_HIGH):
  660. best = 8;
  661. break;
  662. case (DMA_PRIO_MEDIUM):
  663. best = 4;
  664. break;
  665. case (DMA_PRIO_LOW):
  666. default:
  667. best = 0;
  668. break;
  669. }
  670. for (i = best; i < IMX_DMA_CHANNELS; i++)
  671. if (!imx_dma_request(i, name))
  672. return i;
  673. for (i = best - 1; i >= 0; i--)
  674. if (!imx_dma_request(i, name))
  675. return i;
  676. printk(KERN_ERR "%s: no free DMA channel found\n", __func__);
  677. return -ENODEV;
  678. }
  679. EXPORT_SYMBOL(imx_dma_request_by_prio);
  680. static int __init imx_dma_init(void)
  681. {
  682. int ret = 0;
  683. int i;
  684. dma_clk = clk_get(NULL, "dma");
  685. clk_enable(dma_clk);
  686. /* reset DMA module */
  687. __raw_writel(DCR_DRST, DMA_BASE + DMA_DCR);
  688. #ifdef CONFIG_ARCH_MX1
  689. ret = request_irq(DMA_INT, dma_irq_handler, 0, "DMA", NULL);
  690. if (ret) {
  691. printk(KERN_CRIT "Wow! Can't register IRQ for DMA\n");
  692. return ret;
  693. }
  694. ret = request_irq(DMA_ERR, dma_err_handler, 0, "DMA", NULL);
  695. if (ret) {
  696. printk(KERN_CRIT "Wow! Can't register ERRIRQ for DMA\n");
  697. free_irq(DMA_INT, NULL);
  698. return ret;
  699. }
  700. #endif
  701. /* enable DMA module */
  702. __raw_writel(DCR_DEN, DMA_BASE + DMA_DCR);
  703. /* clear all interrupts */
  704. __raw_writel((1 << IMX_DMA_CHANNELS) - 1, DMA_BASE + DMA_DISR);
  705. /* disable interrupts */
  706. __raw_writel((1 << IMX_DMA_CHANNELS) - 1, DMA_BASE + DMA_DIMR);
  707. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  708. imx_dma_channels[i].sg = NULL;
  709. imx_dma_channels[i].dma_num = i;
  710. }
  711. return ret;
  712. }
  713. arch_initcall(imx_dma_init);