dma.c 4.9 KB

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