dma.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * linux/arch/arm/plat-pxa/dma.c
  3. *
  4. * PXA DMA registration and IRQ dispatching
  5. *
  6. * Author: Nicolas Pitre
  7. * Created: Nov 15, 2001
  8. * Copyright: MontaVista Software Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/errno.h>
  20. #include <linux/dma-mapping.h>
  21. #include <asm/irq.h>
  22. #include <asm/memory.h>
  23. #include <mach/hardware.h>
  24. #include <mach/dma.h>
  25. #define DMA_DEBUG_NAME "pxa_dma"
  26. #define DMA_MAX_REQUESTERS 64
  27. struct dma_channel {
  28. char *name;
  29. pxa_dma_prio prio;
  30. void (*irq_handler)(int, void *);
  31. void *data;
  32. spinlock_t lock;
  33. };
  34. static struct dma_channel *dma_channels;
  35. static int num_dma_channels;
  36. /*
  37. * Debug fs
  38. */
  39. #ifdef CONFIG_DEBUG_FS
  40. #include <linux/debugfs.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/seq_file.h>
  43. static struct dentry *dbgfs_root, *dbgfs_state, **dbgfs_chan;
  44. static int dbg_show_requester_chan(struct seq_file *s, void *p)
  45. {
  46. int chan = (int)s->private;
  47. int i;
  48. u32 drcmr;
  49. seq_printf(s, "DMA channel %d requesters list :\n", chan);
  50. for (i = 0; i < DMA_MAX_REQUESTERS; i++) {
  51. drcmr = DRCMR(i);
  52. if ((drcmr & DRCMR_CHLNUM) == chan)
  53. seq_printf(s, "\tRequester %d (MAPVLD=%d)\n",
  54. i, !!(drcmr & DRCMR_MAPVLD));
  55. }
  56. return 0;
  57. }
  58. static inline int dbg_burst_from_dcmd(u32 dcmd)
  59. {
  60. int burst = (dcmd >> 16) & 0x3;
  61. return burst ? 4 << burst : 0;
  62. }
  63. static int is_phys_valid(unsigned long addr)
  64. {
  65. return pfn_valid(__phys_to_pfn(addr));
  66. }
  67. #define DCSR_STR(flag) (dcsr & DCSR_##flag ? #flag" " : "")
  68. #define DCMD_STR(flag) (dcmd & DCMD_##flag ? #flag" " : "")
  69. static int dbg_show_descriptors(struct seq_file *s, void *p)
  70. {
  71. int chan = (int)s->private;
  72. int i, max_show = 20, burst, width;
  73. u32 dcmd;
  74. unsigned long phys_desc;
  75. struct pxa_dma_desc *desc;
  76. unsigned long flags;
  77. spin_lock_irqsave(&dma_channels[chan].lock, flags);
  78. phys_desc = DDADR(chan);
  79. seq_printf(s, "DMA channel %d descriptors :\n", chan);
  80. seq_printf(s, "[%03d] First descriptor unknown\n", 0);
  81. for (i = 1; i < max_show && is_phys_valid(phys_desc); i++) {
  82. desc = phys_to_virt(phys_desc);
  83. dcmd = desc->dcmd;
  84. burst = dbg_burst_from_dcmd(dcmd);
  85. width = (1 << ((dcmd >> 14) & 0x3)) >> 1;
  86. seq_printf(s, "[%03d] Desc at %08lx(virt %p)\n",
  87. i, phys_desc, desc);
  88. seq_printf(s, "\tDDADR = %08x\n", desc->ddadr);
  89. seq_printf(s, "\tDSADR = %08x\n", desc->dsadr);
  90. seq_printf(s, "\tDTADR = %08x\n", desc->dtadr);
  91. seq_printf(s, "\tDCMD = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
  92. dcmd,
  93. DCMD_STR(INCSRCADDR), DCMD_STR(INCTRGADDR),
  94. DCMD_STR(FLOWSRC), DCMD_STR(FLOWTRG),
  95. DCMD_STR(STARTIRQEN), DCMD_STR(ENDIRQEN),
  96. DCMD_STR(ENDIAN), burst, width,
  97. dcmd & DCMD_LENGTH);
  98. phys_desc = desc->ddadr;
  99. }
  100. if (i == max_show)
  101. seq_printf(s, "[%03d] Desc at %08lx ... max display reached\n",
  102. i, phys_desc);
  103. else
  104. seq_printf(s, "[%03d] Desc at %08lx is %s\n",
  105. i, phys_desc, phys_desc == DDADR_STOP ?
  106. "DDADR_STOP" : "invalid");
  107. spin_unlock_irqrestore(&dma_channels[chan].lock, flags);
  108. return 0;
  109. }
  110. static int dbg_show_chan_state(struct seq_file *s, void *p)
  111. {
  112. int chan = (int)s->private;
  113. u32 dcsr, dcmd;
  114. int burst, width;
  115. static char *str_prio[] = { "high", "normal", "low" };
  116. dcsr = DCSR(chan);
  117. dcmd = DCMD(chan);
  118. burst = dbg_burst_from_dcmd(dcmd);
  119. width = (1 << ((dcmd >> 14) & 0x3)) >> 1;
  120. seq_printf(s, "DMA channel %d\n", chan);
  121. seq_printf(s, "\tPriority : %s\n", str_prio[dma_channels[chan].prio]);
  122. seq_printf(s, "\tUnaligned transfer bit: %s\n",
  123. DALGN & (1 << chan) ? "yes" : "no");
  124. seq_printf(s, "\tDCSR = %08x (%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
  125. dcsr, DCSR_STR(RUN), DCSR_STR(NODESC),
  126. DCSR_STR(STOPIRQEN), DCSR_STR(EORIRQEN),
  127. DCSR_STR(EORJMPEN), DCSR_STR(EORSTOPEN),
  128. DCSR_STR(SETCMPST), DCSR_STR(CLRCMPST),
  129. DCSR_STR(CMPST), DCSR_STR(EORINTR), DCSR_STR(REQPEND),
  130. DCSR_STR(STOPSTATE), DCSR_STR(ENDINTR),
  131. DCSR_STR(STARTINTR), DCSR_STR(BUSERR));
  132. seq_printf(s, "\tDCMD = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
  133. dcmd,
  134. DCMD_STR(INCSRCADDR), DCMD_STR(INCTRGADDR),
  135. DCMD_STR(FLOWSRC), DCMD_STR(FLOWTRG),
  136. DCMD_STR(STARTIRQEN), DCMD_STR(ENDIRQEN),
  137. DCMD_STR(ENDIAN), burst, width, dcmd & DCMD_LENGTH);
  138. seq_printf(s, "\tDSADR = %08x\n", DSADR(chan));
  139. seq_printf(s, "\tDTADR = %08x\n", DTADR(chan));
  140. seq_printf(s, "\tDDADR = %08x\n", DDADR(chan));
  141. return 0;
  142. }
  143. static int dbg_show_state(struct seq_file *s, void *p)
  144. {
  145. /* basic device status */
  146. seq_puts(s, "DMA engine status\n");
  147. seq_printf(s, "\tChannel number: %d\n", num_dma_channels);
  148. return 0;
  149. }
  150. #define DBGFS_FUNC_DECL(name) \
  151. static int dbg_open_##name(struct inode *inode, struct file *file) \
  152. { \
  153. return single_open(file, dbg_show_##name, inode->i_private); \
  154. } \
  155. static const struct file_operations dbg_fops_##name = { \
  156. .owner = THIS_MODULE, \
  157. .open = dbg_open_##name, \
  158. .llseek = seq_lseek, \
  159. .read = seq_read, \
  160. .release = single_release, \
  161. }
  162. DBGFS_FUNC_DECL(state);
  163. DBGFS_FUNC_DECL(chan_state);
  164. DBGFS_FUNC_DECL(descriptors);
  165. DBGFS_FUNC_DECL(requester_chan);
  166. static struct dentry *pxa_dma_dbg_alloc_chan(int ch, struct dentry *chandir)
  167. {
  168. char chan_name[11];
  169. struct dentry *chan, *chan_state = NULL, *chan_descr = NULL;
  170. struct dentry *chan_reqs = NULL;
  171. void *dt;
  172. scnprintf(chan_name, sizeof(chan_name), "%d", ch);
  173. chan = debugfs_create_dir(chan_name, chandir);
  174. dt = (void *)ch;
  175. if (chan)
  176. chan_state = debugfs_create_file("state", 0400, chan, dt,
  177. &dbg_fops_chan_state);
  178. if (chan_state)
  179. chan_descr = debugfs_create_file("descriptors", 0400, chan, dt,
  180. &dbg_fops_descriptors);
  181. if (chan_descr)
  182. chan_reqs = debugfs_create_file("requesters", 0400, chan, dt,
  183. &dbg_fops_requester_chan);
  184. if (!chan_reqs)
  185. goto err_state;
  186. return chan;
  187. err_state:
  188. debugfs_remove_recursive(chan);
  189. return NULL;
  190. }
  191. static void pxa_dma_init_debugfs(void)
  192. {
  193. int i;
  194. struct dentry *chandir;
  195. dbgfs_root = debugfs_create_dir(DMA_DEBUG_NAME, NULL);
  196. if (IS_ERR(dbgfs_root) || !dbgfs_root)
  197. goto err_root;
  198. dbgfs_state = debugfs_create_file("state", 0400, dbgfs_root, NULL,
  199. &dbg_fops_state);
  200. if (!dbgfs_state)
  201. goto err_state;
  202. dbgfs_chan = kmalloc(sizeof(*dbgfs_state) * num_dma_channels,
  203. GFP_KERNEL);
  204. if (!dbgfs_chan)
  205. goto err_alloc;
  206. chandir = debugfs_create_dir("channels", dbgfs_root);
  207. if (!chandir)
  208. goto err_chandir;
  209. for (i = 0; i < num_dma_channels; i++) {
  210. dbgfs_chan[i] = pxa_dma_dbg_alloc_chan(i, chandir);
  211. if (!dbgfs_chan[i])
  212. goto err_chans;
  213. }
  214. return;
  215. err_chans:
  216. err_chandir:
  217. kfree(dbgfs_chan);
  218. err_alloc:
  219. err_state:
  220. debugfs_remove_recursive(dbgfs_root);
  221. err_root:
  222. pr_err("pxa_dma: debugfs is not available\n");
  223. }
  224. static void __exit pxa_dma_cleanup_debugfs(void)
  225. {
  226. debugfs_remove_recursive(dbgfs_root);
  227. }
  228. #else
  229. static inline void pxa_dma_init_debugfs(void) {}
  230. static inline void pxa_dma_cleanup_debugfs(void) {}
  231. #endif
  232. int pxa_request_dma (char *name, pxa_dma_prio prio,
  233. void (*irq_handler)(int, void *),
  234. void *data)
  235. {
  236. unsigned long flags;
  237. int i, found = 0;
  238. /* basic sanity checks */
  239. if (!name || !irq_handler)
  240. return -EINVAL;
  241. local_irq_save(flags);
  242. do {
  243. /* try grabbing a DMA channel with the requested priority */
  244. for (i = 0; i < num_dma_channels; i++) {
  245. if ((dma_channels[i].prio == prio) &&
  246. !dma_channels[i].name) {
  247. found = 1;
  248. break;
  249. }
  250. }
  251. /* if requested prio group is full, try a hier priority */
  252. } while (!found && prio--);
  253. if (found) {
  254. DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  255. dma_channels[i].name = name;
  256. dma_channels[i].irq_handler = irq_handler;
  257. dma_channels[i].data = data;
  258. } else {
  259. printk (KERN_WARNING "No more available DMA channels for %s\n", name);
  260. i = -ENODEV;
  261. }
  262. local_irq_restore(flags);
  263. return i;
  264. }
  265. EXPORT_SYMBOL(pxa_request_dma);
  266. void pxa_free_dma (int dma_ch)
  267. {
  268. unsigned long flags;
  269. if (!dma_channels[dma_ch].name) {
  270. printk (KERN_CRIT
  271. "%s: trying to free channel %d which is already freed\n",
  272. __func__, dma_ch);
  273. return;
  274. }
  275. local_irq_save(flags);
  276. DCSR(dma_ch) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  277. dma_channels[dma_ch].name = NULL;
  278. local_irq_restore(flags);
  279. }
  280. EXPORT_SYMBOL(pxa_free_dma);
  281. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  282. {
  283. int i, dint = DINT;
  284. struct dma_channel *channel;
  285. while (dint) {
  286. i = __ffs(dint);
  287. dint &= (dint - 1);
  288. channel = &dma_channels[i];
  289. if (channel->name && channel->irq_handler) {
  290. channel->irq_handler(i, channel->data);
  291. } else {
  292. /*
  293. * IRQ for an unregistered DMA channel:
  294. * let's clear the interrupts and disable it.
  295. */
  296. printk (KERN_WARNING "spurious IRQ for DMA channel %d\n", i);
  297. DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  298. }
  299. }
  300. return IRQ_HANDLED;
  301. }
  302. int __init pxa_init_dma(int irq, int num_ch)
  303. {
  304. int i, ret;
  305. dma_channels = kzalloc(sizeof(struct dma_channel) * num_ch, GFP_KERNEL);
  306. if (dma_channels == NULL)
  307. return -ENOMEM;
  308. /* dma channel priorities on pxa2xx processors:
  309. * ch 0 - 3, 16 - 19 <--> (0) DMA_PRIO_HIGH
  310. * ch 4 - 7, 20 - 23 <--> (1) DMA_PRIO_MEDIUM
  311. * ch 8 - 15, 24 - 31 <--> (2) DMA_PRIO_LOW
  312. */
  313. for (i = 0; i < num_ch; i++) {
  314. DCSR(i) = 0;
  315. dma_channels[i].prio = min((i & 0xf) >> 2, DMA_PRIO_LOW);
  316. spin_lock_init(&dma_channels[i].lock);
  317. }
  318. ret = request_irq(irq, dma_irq_handler, 0, "DMA", NULL);
  319. if (ret) {
  320. printk (KERN_CRIT "Wow! Can't register IRQ for DMA\n");
  321. kfree(dma_channels);
  322. return ret;
  323. }
  324. num_dma_channels = num_ch;
  325. pxa_dma_init_debugfs();
  326. return 0;
  327. }