dma.c 5.9 KB

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