dma-v1.c 23 KB

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