dma.c 5.5 KB

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