dma.c 5.1 KB

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