dma.c 9.8 KB

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