dma-v1.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * linux/arch/arm/plat-mxc/dma-v1.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-v1.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 void __iomem *imx_dmav1_baseaddr;
  121. static void imx_dmav1_writel(unsigned val, unsigned offset)
  122. {
  123. __raw_writel(val, imx_dmav1_baseaddr + offset);
  124. }
  125. static unsigned imx_dmav1_readl(unsigned offset)
  126. {
  127. return __raw_readl(imx_dmav1_baseaddr + offset);
  128. }
  129. static struct imx_dma_channel imx_dma_channels[IMX_DMA_CHANNELS];
  130. static struct clk *dma_clk;
  131. static int imx_dma_hw_chain(struct imx_dma_channel *imxdma)
  132. {
  133. if (cpu_is_mx27())
  134. return imxdma->hw_chaining;
  135. else
  136. return 0;
  137. }
  138. /*
  139. * imx_dma_sg_next - prepare next chunk for scatter-gather DMA emulation
  140. */
  141. static inline int imx_dma_sg_next(int channel, struct scatterlist *sg)
  142. {
  143. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  144. unsigned long now;
  145. if (!imxdma->name) {
  146. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  147. __func__, channel);
  148. return 0;
  149. }
  150. now = min(imxdma->resbytes, sg->length);
  151. if (imxdma->resbytes != IMX_DMA_LENGTH_LOOP)
  152. imxdma->resbytes -= now;
  153. if ((imxdma->dma_mode & DMA_MODE_MASK) == DMA_MODE_READ)
  154. imx_dmav1_writel(sg->dma_address, DMA_DAR(channel));
  155. else
  156. imx_dmav1_writel(sg->dma_address, DMA_SAR(channel));
  157. imx_dmav1_writel(now, DMA_CNTR(channel));
  158. pr_debug("imxdma%d: next sg chunk dst 0x%08x, src 0x%08x, "
  159. "size 0x%08x\n", channel,
  160. imx_dmav1_readl(DMA_DAR(channel)),
  161. imx_dmav1_readl(DMA_SAR(channel)),
  162. imx_dmav1_readl(DMA_CNTR(channel)));
  163. return now;
  164. }
  165. /**
  166. * imx_dma_setup_single - setup i.MX DMA channel for linear memory to/from
  167. * device transfer
  168. *
  169. * @channel: i.MX DMA channel number
  170. * @dma_address: the DMA/physical memory address of the linear data block
  171. * to transfer
  172. * @dma_length: length of the data block in bytes
  173. * @dev_addr: physical device port address
  174. * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
  175. * or %DMA_MODE_WRITE from memory to the device
  176. *
  177. * Return value: if incorrect parameters are provided -%EINVAL.
  178. * Zero indicates success.
  179. */
  180. int
  181. imx_dma_setup_single(int channel, dma_addr_t dma_address,
  182. unsigned int dma_length, unsigned int dev_addr,
  183. unsigned int dmamode)
  184. {
  185. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  186. imxdma->sg = NULL;
  187. imxdma->dma_mode = dmamode;
  188. if (!dma_address) {
  189. printk(KERN_ERR "imxdma%d: imx_dma_setup_single null address\n",
  190. channel);
  191. return -EINVAL;
  192. }
  193. if (!dma_length) {
  194. printk(KERN_ERR "imxdma%d: imx_dma_setup_single zero length\n",
  195. channel);
  196. return -EINVAL;
  197. }
  198. if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
  199. pr_debug("imxdma%d: %s dma_addressg=0x%08x dma_length=%d "
  200. "dev_addr=0x%08x for read\n",
  201. channel, __func__, (unsigned int)dma_address,
  202. dma_length, dev_addr);
  203. imx_dmav1_writel(dev_addr, DMA_SAR(channel));
  204. imx_dmav1_writel(dma_address, DMA_DAR(channel));
  205. imx_dmav1_writel(imxdma->ccr_from_device, DMA_CCR(channel));
  206. } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
  207. pr_debug("imxdma%d: %s dma_addressg=0x%08x dma_length=%d "
  208. "dev_addr=0x%08x for write\n",
  209. channel, __func__, (unsigned int)dma_address,
  210. dma_length, dev_addr);
  211. imx_dmav1_writel(dma_address, DMA_SAR(channel));
  212. imx_dmav1_writel(dev_addr, DMA_DAR(channel));
  213. imx_dmav1_writel(imxdma->ccr_to_device,
  214. DMA_CCR(channel));
  215. } else {
  216. printk(KERN_ERR "imxdma%d: imx_dma_setup_single bad dmamode\n",
  217. channel);
  218. return -EINVAL;
  219. }
  220. imx_dmav1_writel(dma_length, DMA_CNTR(channel));
  221. return 0;
  222. }
  223. EXPORT_SYMBOL(imx_dma_setup_single);
  224. /**
  225. * imx_dma_setup_sg - setup i.MX DMA channel SG list to/from device transfer
  226. * @channel: i.MX DMA channel number
  227. * @sg: pointer to the scatter-gather list/vector
  228. * @sgcount: scatter-gather list hungs count
  229. * @dma_length: total length of the transfer request in bytes
  230. * @dev_addr: physical device port address
  231. * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
  232. * or %DMA_MODE_WRITE from memory to the device
  233. *
  234. * The function sets up DMA channel state and registers to be ready for
  235. * transfer specified by provided parameters. The scatter-gather emulation
  236. * is set up according to the parameters.
  237. *
  238. * The full preparation of the transfer requires setup of more register
  239. * by the caller before imx_dma_enable() can be called.
  240. *
  241. * %BLR(channel) holds transfer burst length in bytes, 0 means 64 bytes
  242. *
  243. * %RSSR(channel) has to be set to the DMA request line source %DMA_REQ_xxx
  244. *
  245. * %CCR(channel) has to specify transfer parameters, the next settings is
  246. * typical for linear or simple scatter-gather transfers if %DMA_MODE_READ is
  247. * specified
  248. *
  249. * %CCR_DMOD_LINEAR | %CCR_DSIZ_32 | %CCR_SMOD_FIFO | %CCR_SSIZ_x
  250. *
  251. * The typical setup for %DMA_MODE_WRITE is specified by next options
  252. * combination
  253. *
  254. * %CCR_SMOD_LINEAR | %CCR_SSIZ_32 | %CCR_DMOD_FIFO | %CCR_DSIZ_x
  255. *
  256. * Be careful here and do not mistakenly mix source and target device
  257. * port sizes constants, they are really different:
  258. * %CCR_SSIZ_8, %CCR_SSIZ_16, %CCR_SSIZ_32,
  259. * %CCR_DSIZ_8, %CCR_DSIZ_16, %CCR_DSIZ_32
  260. *
  261. * Return value: if incorrect parameters are provided -%EINVAL.
  262. * Zero indicates success.
  263. */
  264. int
  265. imx_dma_setup_sg(int channel,
  266. struct scatterlist *sg, unsigned int sgcount,
  267. unsigned int dma_length, unsigned int dev_addr,
  268. unsigned int dmamode)
  269. {
  270. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  271. if (imxdma->in_use)
  272. return -EBUSY;
  273. imxdma->sg = sg;
  274. imxdma->dma_mode = dmamode;
  275. imxdma->resbytes = dma_length;
  276. if (!sg || !sgcount) {
  277. printk(KERN_ERR "imxdma%d: imx_dma_setup_sg empty sg list\n",
  278. channel);
  279. return -EINVAL;
  280. }
  281. if (!sg->length) {
  282. printk(KERN_ERR "imxdma%d: imx_dma_setup_sg zero length\n",
  283. channel);
  284. return -EINVAL;
  285. }
  286. if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
  287. pr_debug("imxdma%d: %s sg=%p sgcount=%d total length=%d "
  288. "dev_addr=0x%08x for read\n",
  289. channel, __func__, sg, sgcount, dma_length, dev_addr);
  290. imx_dmav1_writel(dev_addr, DMA_SAR(channel));
  291. imx_dmav1_writel(imxdma->ccr_from_device, DMA_CCR(channel));
  292. } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
  293. pr_debug("imxdma%d: %s sg=%p sgcount=%d total length=%d "
  294. "dev_addr=0x%08x for write\n",
  295. channel, __func__, sg, sgcount, dma_length, dev_addr);
  296. imx_dmav1_writel(dev_addr, DMA_DAR(channel));
  297. imx_dmav1_writel(imxdma->ccr_to_device, DMA_CCR(channel));
  298. } else {
  299. printk(KERN_ERR "imxdma%d: imx_dma_setup_sg bad dmamode\n",
  300. channel);
  301. return -EINVAL;
  302. }
  303. imx_dma_sg_next(channel, sg);
  304. return 0;
  305. }
  306. EXPORT_SYMBOL(imx_dma_setup_sg);
  307. int
  308. imx_dma_config_channel(int channel, unsigned int config_port,
  309. unsigned int config_mem, unsigned int dmareq, int hw_chaining)
  310. {
  311. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  312. u32 dreq = 0;
  313. imxdma->hw_chaining = 0;
  314. if (hw_chaining) {
  315. imxdma->hw_chaining = 1;
  316. if (!imx_dma_hw_chain(imxdma))
  317. return -EINVAL;
  318. }
  319. if (dmareq)
  320. dreq = CCR_REN;
  321. imxdma->ccr_from_device = config_port | (config_mem << 2) | dreq;
  322. imxdma->ccr_to_device = config_mem | (config_port << 2) | dreq;
  323. imx_dmav1_writel(dmareq, DMA_RSSR(channel));
  324. return 0;
  325. }
  326. EXPORT_SYMBOL(imx_dma_config_channel);
  327. void imx_dma_config_burstlen(int channel, unsigned int burstlen)
  328. {
  329. imx_dmav1_writel(burstlen, DMA_BLR(channel));
  330. }
  331. EXPORT_SYMBOL(imx_dma_config_burstlen);
  332. /**
  333. * imx_dma_setup_handlers - setup i.MX DMA channel end and error notification
  334. * handlers
  335. * @channel: i.MX DMA channel number
  336. * @irq_handler: the pointer to the function called if the transfer
  337. * ends successfully
  338. * @err_handler: the pointer to the function called if the premature
  339. * end caused by error occurs
  340. * @data: user specified value to be passed to the handlers
  341. */
  342. int
  343. imx_dma_setup_handlers(int channel,
  344. void (*irq_handler) (int, void *),
  345. void (*err_handler) (int, void *, int),
  346. void *data)
  347. {
  348. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  349. unsigned long flags;
  350. if (!imxdma->name) {
  351. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  352. __func__, channel);
  353. return -ENODEV;
  354. }
  355. local_irq_save(flags);
  356. imx_dmav1_writel(1 << channel, DMA_DISR);
  357. imxdma->irq_handler = irq_handler;
  358. imxdma->err_handler = err_handler;
  359. imxdma->data = data;
  360. local_irq_restore(flags);
  361. return 0;
  362. }
  363. EXPORT_SYMBOL(imx_dma_setup_handlers);
  364. /**
  365. * imx_dma_setup_progression_handler - setup i.MX DMA channel progression
  366. * handlers
  367. * @channel: i.MX DMA channel number
  368. * @prog_handler: the pointer to the function called if the transfer progresses
  369. */
  370. int
  371. imx_dma_setup_progression_handler(int channel,
  372. void (*prog_handler) (int, void*, struct scatterlist*))
  373. {
  374. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  375. unsigned long flags;
  376. if (!imxdma->name) {
  377. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  378. __func__, channel);
  379. return -ENODEV;
  380. }
  381. local_irq_save(flags);
  382. imxdma->prog_handler = prog_handler;
  383. local_irq_restore(flags);
  384. return 0;
  385. }
  386. EXPORT_SYMBOL(imx_dma_setup_progression_handler);
  387. /**
  388. * imx_dma_enable - function to start i.MX DMA channel operation
  389. * @channel: i.MX DMA channel number
  390. *
  391. * The channel has to be allocated by driver through imx_dma_request()
  392. * or imx_dma_request_by_prio() function.
  393. * The transfer parameters has to be set to the channel registers through
  394. * call of the imx_dma_setup_single() or imx_dma_setup_sg() function
  395. * and registers %BLR(channel), %RSSR(channel) and %CCR(channel) has to
  396. * be set prior this function call by the channel user.
  397. */
  398. void imx_dma_enable(int channel)
  399. {
  400. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  401. unsigned long flags;
  402. pr_debug("imxdma%d: imx_dma_enable\n", channel);
  403. if (!imxdma->name) {
  404. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  405. __func__, channel);
  406. return;
  407. }
  408. if (imxdma->in_use)
  409. return;
  410. local_irq_save(flags);
  411. imx_dmav1_writel(1 << channel, DMA_DISR);
  412. imx_dmav1_writel(imx_dmav1_readl(DMA_DIMR) & ~(1 << channel), DMA_DIMR);
  413. imx_dmav1_writel(imx_dmav1_readl(DMA_CCR(channel)) | CCR_CEN |
  414. CCR_ACRPT, DMA_CCR(channel));
  415. #ifdef CONFIG_ARCH_MX2
  416. if ((cpu_is_mx21() || cpu_is_mx27()) &&
  417. imxdma->sg && imx_dma_hw_chain(imxdma)) {
  418. imxdma->sg = sg_next(imxdma->sg);
  419. if (imxdma->sg) {
  420. u32 tmp;
  421. imx_dma_sg_next(channel, imxdma->sg);
  422. tmp = imx_dmav1_readl(DMA_CCR(channel));
  423. imx_dmav1_writel(tmp | CCR_RPT | CCR_ACRPT,
  424. DMA_CCR(channel));
  425. }
  426. }
  427. #endif
  428. imxdma->in_use = 1;
  429. local_irq_restore(flags);
  430. }
  431. EXPORT_SYMBOL(imx_dma_enable);
  432. /**
  433. * imx_dma_disable - stop, finish i.MX DMA channel operatin
  434. * @channel: i.MX DMA channel number
  435. */
  436. void imx_dma_disable(int channel)
  437. {
  438. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  439. unsigned long flags;
  440. pr_debug("imxdma%d: imx_dma_disable\n", channel);
  441. if (imx_dma_hw_chain(imxdma))
  442. del_timer(&imxdma->watchdog);
  443. local_irq_save(flags);
  444. imx_dmav1_writel(imx_dmav1_readl(DMA_DIMR) | (1 << channel), DMA_DIMR);
  445. imx_dmav1_writel(imx_dmav1_readl(DMA_CCR(channel)) & ~CCR_CEN,
  446. DMA_CCR(channel));
  447. imx_dmav1_writel(1 << channel, DMA_DISR);
  448. imxdma->in_use = 0;
  449. local_irq_restore(flags);
  450. }
  451. EXPORT_SYMBOL(imx_dma_disable);
  452. #ifdef CONFIG_ARCH_MX2
  453. static void imx_dma_watchdog(unsigned long chno)
  454. {
  455. struct imx_dma_channel *imxdma = &imx_dma_channels[chno];
  456. imx_dmav1_writel(0, DMA_CCR(chno));
  457. imxdma->in_use = 0;
  458. imxdma->sg = NULL;
  459. if (imxdma->err_handler)
  460. imxdma->err_handler(chno, imxdma->data, IMX_DMA_ERR_TIMEOUT);
  461. }
  462. #endif
  463. static irqreturn_t dma_err_handler(int irq, void *dev_id)
  464. {
  465. int i, disr;
  466. struct imx_dma_channel *imxdma;
  467. unsigned int err_mask;
  468. int errcode;
  469. disr = imx_dmav1_readl(DMA_DISR);
  470. err_mask = imx_dmav1_readl(DMA_DBTOSR) |
  471. imx_dmav1_readl(DMA_DRTOSR) |
  472. imx_dmav1_readl(DMA_DSESR) |
  473. imx_dmav1_readl(DMA_DBOSR);
  474. if (!err_mask)
  475. return IRQ_HANDLED;
  476. imx_dmav1_writel(disr & err_mask, DMA_DISR);
  477. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  478. if (!(err_mask & (1 << i)))
  479. continue;
  480. imxdma = &imx_dma_channels[i];
  481. errcode = 0;
  482. if (imx_dmav1_readl(DMA_DBTOSR) & (1 << i)) {
  483. imx_dmav1_writel(1 << i, DMA_DBTOSR);
  484. errcode |= IMX_DMA_ERR_BURST;
  485. }
  486. if (imx_dmav1_readl(DMA_DRTOSR) & (1 << i)) {
  487. imx_dmav1_writel(1 << i, DMA_DRTOSR);
  488. errcode |= IMX_DMA_ERR_REQUEST;
  489. }
  490. if (imx_dmav1_readl(DMA_DSESR) & (1 << i)) {
  491. imx_dmav1_writel(1 << i, DMA_DSESR);
  492. errcode |= IMX_DMA_ERR_TRANSFER;
  493. }
  494. if (imx_dmav1_readl(DMA_DBOSR) & (1 << i)) {
  495. imx_dmav1_writel(1 << i, DMA_DBOSR);
  496. errcode |= IMX_DMA_ERR_BUFFER;
  497. }
  498. if (imxdma->name && imxdma->err_handler) {
  499. imxdma->err_handler(i, imxdma->data, errcode);
  500. continue;
  501. }
  502. imx_dma_channels[i].sg = NULL;
  503. printk(KERN_WARNING
  504. "DMA timeout on channel %d (%s) -%s%s%s%s\n",
  505. i, imxdma->name,
  506. errcode & IMX_DMA_ERR_BURST ? " burst" : "",
  507. errcode & IMX_DMA_ERR_REQUEST ? " request" : "",
  508. errcode & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
  509. errcode & IMX_DMA_ERR_BUFFER ? " buffer" : "");
  510. }
  511. return IRQ_HANDLED;
  512. }
  513. static void dma_irq_handle_channel(int chno)
  514. {
  515. struct imx_dma_channel *imxdma = &imx_dma_channels[chno];
  516. if (!imxdma->name) {
  517. /*
  518. * IRQ for an unregistered DMA channel:
  519. * let's clear the interrupts and disable it.
  520. */
  521. printk(KERN_WARNING
  522. "spurious IRQ for DMA channel %d\n", chno);
  523. return;
  524. }
  525. if (imxdma->sg) {
  526. u32 tmp;
  527. struct scatterlist *current_sg = imxdma->sg;
  528. imxdma->sg = sg_next(imxdma->sg);
  529. if (imxdma->sg) {
  530. imx_dma_sg_next(chno, imxdma->sg);
  531. tmp = imx_dmav1_readl(DMA_CCR(chno));
  532. if (imx_dma_hw_chain(imxdma)) {
  533. /* FIXME: The timeout should probably be
  534. * configurable
  535. */
  536. mod_timer(&imxdma->watchdog,
  537. jiffies + msecs_to_jiffies(500));
  538. tmp |= CCR_CEN | CCR_RPT | CCR_ACRPT;
  539. imx_dmav1_writel(tmp, DMA_CCR(chno));
  540. } else {
  541. imx_dmav1_writel(tmp & ~CCR_CEN, DMA_CCR(chno));
  542. tmp |= CCR_CEN;
  543. }
  544. imx_dmav1_writel(tmp, DMA_CCR(chno));
  545. if (imxdma->prog_handler)
  546. imxdma->prog_handler(chno, imxdma->data,
  547. current_sg);
  548. return;
  549. }
  550. if (imx_dma_hw_chain(imxdma)) {
  551. del_timer(&imxdma->watchdog);
  552. return;
  553. }
  554. }
  555. imx_dmav1_writel(0, DMA_CCR(chno));
  556. imxdma->in_use = 0;
  557. if (imxdma->irq_handler)
  558. imxdma->irq_handler(chno, imxdma->data);
  559. }
  560. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  561. {
  562. int i, disr;
  563. #ifdef CONFIG_ARCH_MX2
  564. if (cpu_is_mx21() || cpu_is_mx27())
  565. dma_err_handler(irq, dev_id);
  566. #endif
  567. disr = imx_dmav1_readl(DMA_DISR);
  568. pr_debug("imxdma: dma_irq_handler called, disr=0x%08x\n",
  569. disr);
  570. imx_dmav1_writel(disr, DMA_DISR);
  571. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  572. if (disr & (1 << i))
  573. dma_irq_handle_channel(i);
  574. }
  575. return IRQ_HANDLED;
  576. }
  577. /**
  578. * imx_dma_request - request/allocate specified channel number
  579. * @channel: i.MX DMA channel number
  580. * @name: the driver/caller own non-%NULL identification
  581. */
  582. int imx_dma_request(int channel, const char *name)
  583. {
  584. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  585. unsigned long flags;
  586. int ret = 0;
  587. /* basic sanity checks */
  588. if (!name)
  589. return -EINVAL;
  590. if (channel >= IMX_DMA_CHANNELS) {
  591. printk(KERN_CRIT "%s: called for non-existed channel %d\n",
  592. __func__, channel);
  593. return -EINVAL;
  594. }
  595. local_irq_save(flags);
  596. if (imxdma->name) {
  597. local_irq_restore(flags);
  598. return -EBUSY;
  599. }
  600. memset(imxdma, 0, sizeof(*imxdma));
  601. imxdma->name = name;
  602. local_irq_restore(flags); /* request_irq() can block */
  603. #ifdef CONFIG_ARCH_MX2
  604. if (cpu_is_mx21() || cpu_is_mx27()) {
  605. ret = request_irq(MX2x_INT_DMACH0 + channel,
  606. dma_irq_handler, 0, "DMA", NULL);
  607. if (ret) {
  608. imxdma->name = NULL;
  609. pr_crit("Can't register IRQ %d for DMA channel %d\n",
  610. MX2x_INT_DMACH0 + channel, channel);
  611. return ret;
  612. }
  613. init_timer(&imxdma->watchdog);
  614. imxdma->watchdog.function = &imx_dma_watchdog;
  615. imxdma->watchdog.data = channel;
  616. }
  617. #endif
  618. return ret;
  619. }
  620. EXPORT_SYMBOL(imx_dma_request);
  621. /**
  622. * imx_dma_free - release previously acquired channel
  623. * @channel: i.MX DMA channel number
  624. */
  625. void imx_dma_free(int channel)
  626. {
  627. unsigned long flags;
  628. struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
  629. if (!imxdma->name) {
  630. printk(KERN_CRIT
  631. "%s: trying to free free channel %d\n",
  632. __func__, channel);
  633. return;
  634. }
  635. local_irq_save(flags);
  636. /* Disable interrupts */
  637. imx_dma_disable(channel);
  638. imxdma->name = NULL;
  639. #ifdef CONFIG_ARCH_MX2
  640. if (cpu_is_mx21() || cpu_is_mx27())
  641. free_irq(MX2x_INT_DMACH0 + channel, NULL);
  642. #endif
  643. local_irq_restore(flags);
  644. }
  645. EXPORT_SYMBOL(imx_dma_free);
  646. /**
  647. * imx_dma_request_by_prio - find and request some of free channels best
  648. * suiting requested priority
  649. * @channel: i.MX DMA channel number
  650. * @name: the driver/caller own non-%NULL identification
  651. *
  652. * This function tries to find a free channel in the specified priority group
  653. * if the priority cannot be achieved it tries to look for free channel
  654. * in the higher and then even lower priority groups.
  655. *
  656. * Return value: If there is no free channel to allocate, -%ENODEV is returned.
  657. * On successful allocation channel is returned.
  658. */
  659. int imx_dma_request_by_prio(const char *name, enum imx_dma_prio prio)
  660. {
  661. int i;
  662. int best;
  663. switch (prio) {
  664. case (DMA_PRIO_HIGH):
  665. best = 8;
  666. break;
  667. case (DMA_PRIO_MEDIUM):
  668. best = 4;
  669. break;
  670. case (DMA_PRIO_LOW):
  671. default:
  672. best = 0;
  673. break;
  674. }
  675. for (i = best; i < IMX_DMA_CHANNELS; i++)
  676. if (!imx_dma_request(i, name))
  677. return i;
  678. for (i = best - 1; i >= 0; i--)
  679. if (!imx_dma_request(i, name))
  680. return i;
  681. printk(KERN_ERR "%s: no free DMA channel found\n", __func__);
  682. return -ENODEV;
  683. }
  684. EXPORT_SYMBOL(imx_dma_request_by_prio);
  685. static int __init imx_dma_init(void)
  686. {
  687. int ret = 0;
  688. int i;
  689. #ifdef CONFIG_ARCH_MX1
  690. if (cpu_is_mx1())
  691. imx_dmav1_baseaddr = MX1_IO_ADDRESS(MX1_DMA_BASE_ADDR);
  692. else
  693. #endif
  694. #ifdef CONFIG_MACH_MX21
  695. if (cpu_is_mx21())
  696. imx_dmav1_baseaddr = MX21_IO_ADDRESS(MX21_DMA_BASE_ADDR);
  697. else
  698. #endif
  699. #ifdef CONFIG_MACH_MX27
  700. if (cpu_is_mx27())
  701. imx_dmav1_baseaddr = MX27_IO_ADDRESS(MX27_DMA_BASE_ADDR);
  702. else
  703. #endif
  704. return 0;
  705. dma_clk = clk_get(NULL, "dma");
  706. if (IS_ERR(dma_clk))
  707. return PTR_ERR(dma_clk);
  708. clk_enable(dma_clk);
  709. /* reset DMA module */
  710. imx_dmav1_writel(DCR_DRST, DMA_DCR);
  711. #ifdef CONFIG_ARCH_MX1
  712. if (cpu_is_mx1()) {
  713. ret = request_irq(MX1_DMA_INT, dma_irq_handler, 0, "DMA", NULL);
  714. if (ret) {
  715. pr_crit("Wow! Can't register IRQ for DMA\n");
  716. return ret;
  717. }
  718. ret = request_irq(MX1_DMA_ERR, dma_err_handler, 0, "DMA", NULL);
  719. if (ret) {
  720. pr_crit("Wow! Can't register ERRIRQ for DMA\n");
  721. free_irq(MX1_DMA_INT, NULL);
  722. return ret;
  723. }
  724. }
  725. #endif
  726. /* enable DMA module */
  727. imx_dmav1_writel(DCR_DEN, DMA_DCR);
  728. /* clear all interrupts */
  729. imx_dmav1_writel((1 << IMX_DMA_CHANNELS) - 1, DMA_DISR);
  730. /* disable interrupts */
  731. imx_dmav1_writel((1 << IMX_DMA_CHANNELS) - 1, DMA_DIMR);
  732. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  733. imx_dma_channels[i].sg = NULL;
  734. imx_dma_channels[i].dma_num = i;
  735. }
  736. return ret;
  737. }
  738. arch_initcall(imx_dma_init);