dma.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * linux/arch/arm/kernel/dma.c
  3. *
  4. * Copyright (C) 1995-2000 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Front-end to the DMA handling. This handles the allocation/freeing
  11. * of DMA channels, and provides a unified interface to the machines
  12. * DMA facilities.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/errno.h>
  18. #include <asm/dma.h>
  19. #include <asm/mach/dma.h>
  20. DEFINE_SPINLOCK(dma_spin_lock);
  21. EXPORT_SYMBOL(dma_spin_lock);
  22. static dma_t dma_chan[MAX_DMA_CHANNELS];
  23. static inline dma_t *dma_channel(unsigned int chan)
  24. {
  25. dma_t *dma = dma_chan + chan;
  26. if (chan >= MAX_DMA_CHANNELS || !dma->d_ops)
  27. return NULL;
  28. return dma;
  29. }
  30. /*
  31. * Request DMA channel
  32. *
  33. * On certain platforms, we have to allocate an interrupt as well...
  34. */
  35. int request_dma(unsigned int chan, const char *device_id)
  36. {
  37. dma_t *dma = dma_channel(chan);
  38. int ret;
  39. if (!dma)
  40. goto bad_dma;
  41. if (xchg(&dma->lock, 1) != 0)
  42. goto busy;
  43. dma->device_id = device_id;
  44. dma->active = 0;
  45. dma->invalid = 1;
  46. ret = 0;
  47. if (dma->d_ops->request)
  48. ret = dma->d_ops->request(chan, dma);
  49. if (ret)
  50. xchg(&dma->lock, 0);
  51. return ret;
  52. bad_dma:
  53. printk(KERN_ERR "dma: trying to allocate DMA%d\n", chan);
  54. return -EINVAL;
  55. busy:
  56. return -EBUSY;
  57. }
  58. EXPORT_SYMBOL(request_dma);
  59. /*
  60. * Free DMA channel
  61. *
  62. * On certain platforms, we have to free interrupt as well...
  63. */
  64. void free_dma(unsigned int chan)
  65. {
  66. dma_t *dma = dma_channel(chan);
  67. if (!dma)
  68. goto bad_dma;
  69. if (dma->active) {
  70. printk(KERN_ERR "dma%d: freeing active DMA\n", chan);
  71. dma->d_ops->disable(chan, dma);
  72. dma->active = 0;
  73. }
  74. if (xchg(&dma->lock, 0) != 0) {
  75. if (dma->d_ops->free)
  76. dma->d_ops->free(chan, dma);
  77. return;
  78. }
  79. printk(KERN_ERR "dma%d: trying to free free DMA\n", chan);
  80. return;
  81. bad_dma:
  82. printk(KERN_ERR "dma: trying to free DMA%d\n", chan);
  83. }
  84. EXPORT_SYMBOL(free_dma);
  85. /* Set DMA Scatter-Gather list
  86. */
  87. void set_dma_sg (unsigned int chan, struct scatterlist *sg, int nr_sg)
  88. {
  89. dma_t *dma = dma_channel(chan);
  90. if (dma->active)
  91. printk(KERN_ERR "dma%d: altering DMA SG while "
  92. "DMA active\n", chan);
  93. dma->sg = sg;
  94. dma->sgcount = nr_sg;
  95. dma->invalid = 1;
  96. }
  97. EXPORT_SYMBOL(set_dma_sg);
  98. /* Set DMA address
  99. *
  100. * Copy address to the structure, and set the invalid bit
  101. */
  102. void __set_dma_addr (unsigned int chan, void *addr)
  103. {
  104. dma_t *dma = dma_channel(chan);
  105. if (dma->active)
  106. printk(KERN_ERR "dma%d: altering DMA address while "
  107. "DMA active\n", chan);
  108. dma->sg = NULL;
  109. dma->addr = addr;
  110. dma->invalid = 1;
  111. }
  112. EXPORT_SYMBOL(__set_dma_addr);
  113. /* Set DMA byte count
  114. *
  115. * Copy address to the structure, and set the invalid bit
  116. */
  117. void set_dma_count (unsigned int chan, unsigned long count)
  118. {
  119. dma_t *dma = dma_channel(chan);
  120. if (dma->active)
  121. printk(KERN_ERR "dma%d: altering DMA count while "
  122. "DMA active\n", chan);
  123. dma->sg = NULL;
  124. dma->count = count;
  125. dma->invalid = 1;
  126. }
  127. EXPORT_SYMBOL(set_dma_count);
  128. /* Set DMA direction mode
  129. */
  130. void set_dma_mode (unsigned int chan, dmamode_t mode)
  131. {
  132. dma_t *dma = dma_channel(chan);
  133. if (dma->active)
  134. printk(KERN_ERR "dma%d: altering DMA mode while "
  135. "DMA active\n", chan);
  136. dma->dma_mode = mode;
  137. dma->invalid = 1;
  138. }
  139. EXPORT_SYMBOL(set_dma_mode);
  140. /* Enable DMA channel
  141. */
  142. void enable_dma (unsigned int chan)
  143. {
  144. dma_t *dma = dma_channel(chan);
  145. if (!dma->lock)
  146. goto free_dma;
  147. if (dma->active == 0) {
  148. dma->active = 1;
  149. dma->d_ops->enable(chan, dma);
  150. }
  151. return;
  152. free_dma:
  153. printk(KERN_ERR "dma%d: trying to enable free DMA\n", chan);
  154. BUG();
  155. }
  156. EXPORT_SYMBOL(enable_dma);
  157. /* Disable DMA channel
  158. */
  159. void disable_dma (unsigned int chan)
  160. {
  161. dma_t *dma = dma_channel(chan);
  162. if (!dma->lock)
  163. goto free_dma;
  164. if (dma->active == 1) {
  165. dma->active = 0;
  166. dma->d_ops->disable(chan, dma);
  167. }
  168. return;
  169. free_dma:
  170. printk(KERN_ERR "dma%d: trying to disable free DMA\n", chan);
  171. BUG();
  172. }
  173. EXPORT_SYMBOL(disable_dma);
  174. /*
  175. * Is the specified DMA channel active?
  176. */
  177. int dma_channel_active(unsigned int chan)
  178. {
  179. dma_t *dma = dma_channel(chan);
  180. return dma->active;
  181. }
  182. EXPORT_SYMBOL(dma_channel_active);
  183. void set_dma_page(unsigned int chan, char pagenr)
  184. {
  185. printk(KERN_ERR "dma%d: trying to set_dma_page\n", chan);
  186. }
  187. EXPORT_SYMBOL(set_dma_page);
  188. void set_dma_speed(unsigned int chan, int cycle_ns)
  189. {
  190. dma_t *dma = dma_channel(chan);
  191. int ret = 0;
  192. if (dma->d_ops->setspeed)
  193. ret = dma->d_ops->setspeed(chan, dma, cycle_ns);
  194. dma->speed = ret;
  195. }
  196. EXPORT_SYMBOL(set_dma_speed);
  197. int get_dma_residue(unsigned int chan)
  198. {
  199. dma_t *dma = dma_channel(chan);
  200. int ret = 0;
  201. if (dma->d_ops->residue)
  202. ret = dma->d_ops->residue(chan, dma);
  203. return ret;
  204. }
  205. EXPORT_SYMBOL(get_dma_residue);
  206. static int __init init_dma(void)
  207. {
  208. arch_dma_init(dma_chan);
  209. return 0;
  210. }
  211. core_initcall(init_dma);