dma.c 9.8 KB

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