dma-api.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * arch/sh/drivers/dma/dma-api.c
  3. *
  4. * SuperH-specific DMA management API
  5. *
  6. * Copyright (C) 2003, 2004 Paul Mundt
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/list.h>
  18. #include <asm/dma.h>
  19. DEFINE_SPINLOCK(dma_spin_lock);
  20. static LIST_HEAD(registered_dmac_list);
  21. /*
  22. * A brief note about the reasons for this API as it stands.
  23. *
  24. * For starters, the old ISA DMA API didn't work for us for a number of
  25. * reasons, for one, the vast majority of channels on the SH DMAC are
  26. * dual-address mode only, and both the new and the old DMA APIs are after the
  27. * concept of managing a DMA buffer, which doesn't overly fit this model very
  28. * well. In addition to which, the new API is largely geared at IOMMUs and
  29. * GARTs, and doesn't even support the channel notion very well.
  30. *
  31. * The other thing that's a marginal issue, is the sheer number of random DMA
  32. * engines that are present (ie, in boards like the Dreamcast), some of which
  33. * cascade off of the SH DMAC, and others do not. As such, there was a real
  34. * need for a scalable subsystem that could deal with both single and
  35. * dual-address mode usage, in addition to interoperating with cascaded DMACs.
  36. *
  37. * There really isn't any reason why this needs to be SH specific, though I'm
  38. * not aware of too many other processors (with the exception of some MIPS)
  39. * that have the same concept of a dual address mode, or any real desire to
  40. * actually make use of the DMAC even if such a subsystem were exposed
  41. * elsewhere.
  42. *
  43. * The idea for this was derived from the ARM port, which acted as an excellent
  44. * reference when trying to address these issues.
  45. *
  46. * It should also be noted that the decision to add Yet Another DMA API(tm) to
  47. * the kernel wasn't made easily, and was only decided upon after conferring
  48. * with jejb with regards to the state of the old and new APIs as they applied
  49. * to these circumstances. Philip Blundell was also a great help in figuring
  50. * out some single-address mode DMA semantics that were otherwise rather
  51. * confusing.
  52. */
  53. struct dma_info *get_dma_info(unsigned int chan)
  54. {
  55. struct list_head *pos, *tmp;
  56. unsigned int total = 0;
  57. /*
  58. * Look for each DMAC's range to determine who the owner of
  59. * the channel is.
  60. */
  61. list_for_each_safe(pos, tmp, &registered_dmac_list) {
  62. struct dma_info *info = list_entry(pos, struct dma_info, list);
  63. total += info->nr_channels;
  64. if (chan > total)
  65. continue;
  66. return info;
  67. }
  68. return NULL;
  69. }
  70. struct dma_channel *get_dma_channel(unsigned int chan)
  71. {
  72. struct dma_info *info = get_dma_info(chan);
  73. if (!info)
  74. return ERR_PTR(-EINVAL);
  75. return info->channels + chan;
  76. }
  77. int get_dma_residue(unsigned int chan)
  78. {
  79. struct dma_info *info = get_dma_info(chan);
  80. struct dma_channel *channel = &info->channels[chan];
  81. if (info->ops->get_residue)
  82. return info->ops->get_residue(channel);
  83. return 0;
  84. }
  85. int request_dma(unsigned int chan, const char *dev_id)
  86. {
  87. struct dma_info *info = get_dma_info(chan);
  88. struct dma_channel *channel = &info->channels[chan];
  89. down(&channel->sem);
  90. if (!info->ops || chan >= MAX_DMA_CHANNELS) {
  91. up(&channel->sem);
  92. return -EINVAL;
  93. }
  94. atomic_set(&channel->busy, 1);
  95. strlcpy(channel->dev_id, dev_id, sizeof(channel->dev_id));
  96. up(&channel->sem);
  97. if (info->ops->request)
  98. return info->ops->request(channel);
  99. return 0;
  100. }
  101. void free_dma(unsigned int chan)
  102. {
  103. struct dma_info *info = get_dma_info(chan);
  104. struct dma_channel *channel = &info->channels[chan];
  105. if (info->ops->free)
  106. info->ops->free(channel);
  107. atomic_set(&channel->busy, 0);
  108. }
  109. void dma_wait_for_completion(unsigned int chan)
  110. {
  111. struct dma_info *info = get_dma_info(chan);
  112. struct dma_channel *channel = &info->channels[chan];
  113. if (channel->flags & DMA_TEI_CAPABLE) {
  114. wait_event(channel->wait_queue,
  115. (info->ops->get_residue(channel) == 0));
  116. return;
  117. }
  118. while (info->ops->get_residue(channel))
  119. cpu_relax();
  120. }
  121. void dma_configure_channel(unsigned int chan, unsigned long flags)
  122. {
  123. struct dma_info *info = get_dma_info(chan);
  124. struct dma_channel *channel = &info->channels[chan];
  125. if (info->ops->configure)
  126. info->ops->configure(channel, flags);
  127. }
  128. int dma_xfer(unsigned int chan, unsigned long from,
  129. unsigned long to, size_t size, unsigned int mode)
  130. {
  131. struct dma_info *info = get_dma_info(chan);
  132. struct dma_channel *channel = &info->channels[chan];
  133. channel->sar = from;
  134. channel->dar = to;
  135. channel->count = size;
  136. channel->mode = mode;
  137. return info->ops->xfer(channel);
  138. }
  139. #ifdef CONFIG_PROC_FS
  140. static int dma_read_proc(char *buf, char **start, off_t off,
  141. int len, int *eof, void *data)
  142. {
  143. struct list_head *pos, *tmp;
  144. char *p = buf;
  145. if (list_empty(&registered_dmac_list))
  146. return 0;
  147. /*
  148. * Iterate over each registered DMAC
  149. */
  150. list_for_each_safe(pos, tmp, &registered_dmac_list) {
  151. struct dma_info *info = list_entry(pos, struct dma_info, list);
  152. int i;
  153. /*
  154. * Iterate over each channel
  155. */
  156. for (i = 0; i < info->nr_channels; i++) {
  157. struct dma_channel *channel = info->channels + i;
  158. if (!(channel->flags & DMA_CONFIGURED))
  159. continue;
  160. p += sprintf(p, "%2d: %14s %s\n", i,
  161. info->name, channel->dev_id);
  162. }
  163. }
  164. return p - buf;
  165. }
  166. #endif
  167. int __init register_dmac(struct dma_info *info)
  168. {
  169. int i;
  170. INIT_LIST_HEAD(&info->list);
  171. printk(KERN_INFO "DMA: Registering %s handler (%d channel%s).\n",
  172. info->name, info->nr_channels,
  173. info->nr_channels > 1 ? "s" : "");
  174. BUG_ON((info->flags & DMAC_CHANNELS_CONFIGURED) && !info->channels);
  175. /*
  176. * Don't touch pre-configured channels
  177. */
  178. if (!(info->flags & DMAC_CHANNELS_CONFIGURED)) {
  179. unsigned int size;
  180. size = sizeof(struct dma_channel) * info->nr_channels;
  181. info->channels = kmalloc(size, GFP_KERNEL);
  182. if (!info->channels)
  183. return -ENOMEM;
  184. memset(info->channels, 0, size);
  185. }
  186. for (i = 0; i < info->nr_channels; i++) {
  187. struct dma_channel *chan = info->channels + i;
  188. chan->chan = i;
  189. memcpy(chan->dev_id, "Unused", 7);
  190. if (info->flags & DMAC_CHANNELS_TEI_CAPABLE)
  191. chan->flags |= DMA_TEI_CAPABLE;
  192. init_MUTEX(&chan->sem);
  193. init_waitqueue_head(&chan->wait_queue);
  194. #ifdef CONFIG_SYSFS
  195. dma_create_sysfs_files(chan);
  196. #endif
  197. }
  198. list_add(&info->list, &registered_dmac_list);
  199. return 0;
  200. }
  201. void __exit unregister_dmac(struct dma_info *info)
  202. {
  203. if (!(info->flags & DMAC_CHANNELS_CONFIGURED))
  204. kfree(info->channels);
  205. list_del(&info->list);
  206. }
  207. static int __init dma_api_init(void)
  208. {
  209. printk("DMA: Registering DMA API.\n");
  210. #ifdef CONFIG_PROC_FS
  211. create_proc_read_entry("dma", 0, 0, dma_read_proc, 0);
  212. #endif
  213. return 0;
  214. }
  215. subsys_initcall(dma_api_init);
  216. MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
  217. MODULE_DESCRIPTION("DMA API for SuperH");
  218. MODULE_LICENSE("GPL");
  219. EXPORT_SYMBOL(request_dma);
  220. EXPORT_SYMBOL(free_dma);
  221. EXPORT_SYMBOL(register_dmac);
  222. EXPORT_SYMBOL(get_dma_residue);
  223. EXPORT_SYMBOL(get_dma_info);
  224. EXPORT_SYMBOL(get_dma_channel);
  225. EXPORT_SYMBOL(dma_xfer);
  226. EXPORT_SYMBOL(dma_wait_for_completion);
  227. EXPORT_SYMBOL(dma_configure_channel);