dma.c 5.0 KB

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