bfin_dma_5xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * bfin_dma_5xx.c - Blackfin DMA implementation
  3. *
  4. * Copyright 2004-2008 Analog Devices Inc.
  5. * Licensed under the GPL-2 or later.
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/param.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/sched.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/spinlock.h>
  16. #include <asm/blackfin.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/dma.h>
  19. #include <asm/uaccess.h>
  20. struct dma_channel dma_ch[MAX_DMA_CHANNELS];
  21. EXPORT_SYMBOL(dma_ch);
  22. static int __init blackfin_dma_init(void)
  23. {
  24. int i;
  25. printk(KERN_INFO "Blackfin DMA Controller\n");
  26. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  27. dma_ch[i].chan_status = DMA_CHANNEL_FREE;
  28. dma_ch[i].regs = dma_io_base_addr[i];
  29. mutex_init(&(dma_ch[i].dmalock));
  30. }
  31. /* Mark MEMDMA Channel 0 as requested since we're using it internally */
  32. request_dma(CH_MEM_STREAM0_DEST, "Blackfin dma_memcpy");
  33. request_dma(CH_MEM_STREAM0_SRC, "Blackfin dma_memcpy");
  34. #if defined(CONFIG_DEB_DMA_URGENT)
  35. bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE()
  36. | DEB1_URGENT | DEB2_URGENT | DEB3_URGENT);
  37. #endif
  38. return 0;
  39. }
  40. arch_initcall(blackfin_dma_init);
  41. #ifdef CONFIG_PROC_FS
  42. static int proc_dma_show(struct seq_file *m, void *v)
  43. {
  44. int i;
  45. for (i = 0; i < MAX_DMA_CHANNELS; ++i)
  46. if (dma_ch[i].chan_status != DMA_CHANNEL_FREE)
  47. seq_printf(m, "%2d: %s\n", i, dma_ch[i].device_id);
  48. return 0;
  49. }
  50. static int proc_dma_open(struct inode *inode, struct file *file)
  51. {
  52. return single_open(file, proc_dma_show, NULL);
  53. }
  54. static const struct file_operations proc_dma_operations = {
  55. .open = proc_dma_open,
  56. .read = seq_read,
  57. .llseek = seq_lseek,
  58. .release = single_release,
  59. };
  60. static int __init proc_dma_init(void)
  61. {
  62. return proc_create("dma", 0, NULL, &proc_dma_operations) != NULL;
  63. }
  64. late_initcall(proc_dma_init);
  65. #endif
  66. /**
  67. * request_dma - request a DMA channel
  68. *
  69. * Request the specific DMA channel from the system if it's available.
  70. */
  71. int request_dma(unsigned int channel, const char *device_id)
  72. {
  73. pr_debug("request_dma() : BEGIN \n");
  74. if (device_id == NULL)
  75. printk(KERN_WARNING "request_dma(%u): no device_id given\n", channel);
  76. #if defined(CONFIG_BF561) && ANOMALY_05000182
  77. if (channel >= CH_IMEM_STREAM0_DEST && channel <= CH_IMEM_STREAM1_DEST) {
  78. if (get_cclk() > 500000000) {
  79. printk(KERN_WARNING
  80. "Request IMDMA failed due to ANOMALY 05000182\n");
  81. return -EFAULT;
  82. }
  83. }
  84. #endif
  85. mutex_lock(&(dma_ch[channel].dmalock));
  86. if ((dma_ch[channel].chan_status == DMA_CHANNEL_REQUESTED)
  87. || (dma_ch[channel].chan_status == DMA_CHANNEL_ENABLED)) {
  88. mutex_unlock(&(dma_ch[channel].dmalock));
  89. pr_debug("DMA CHANNEL IN USE \n");
  90. return -EBUSY;
  91. } else {
  92. dma_ch[channel].chan_status = DMA_CHANNEL_REQUESTED;
  93. pr_debug("DMA CHANNEL IS ALLOCATED \n");
  94. }
  95. mutex_unlock(&(dma_ch[channel].dmalock));
  96. #ifdef CONFIG_BF54x
  97. if (channel >= CH_UART2_RX && channel <= CH_UART3_TX) {
  98. unsigned int per_map;
  99. per_map = dma_ch[channel].regs->peripheral_map & 0xFFF;
  100. if (strncmp(device_id, "BFIN_UART", 9) == 0)
  101. dma_ch[channel].regs->peripheral_map = per_map |
  102. ((channel - CH_UART2_RX + 0xC)<<12);
  103. else
  104. dma_ch[channel].regs->peripheral_map = per_map |
  105. ((channel - CH_UART2_RX + 0x6)<<12);
  106. }
  107. #endif
  108. dma_ch[channel].device_id = device_id;
  109. dma_ch[channel].irq = 0;
  110. /* This is to be enabled by putting a restriction -
  111. * you have to request DMA, before doing any operations on
  112. * descriptor/channel
  113. */
  114. pr_debug("request_dma() : END \n");
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(request_dma);
  118. int set_dma_callback(unsigned int channel, irq_handler_t callback, void *data)
  119. {
  120. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  121. && channel < MAX_DMA_CHANNELS));
  122. if (callback != NULL) {
  123. int ret;
  124. unsigned int irq = channel2irq(channel);
  125. ret = request_irq(irq, callback, IRQF_DISABLED,
  126. dma_ch[channel].device_id, data);
  127. if (ret)
  128. return ret;
  129. dma_ch[channel].irq = irq;
  130. dma_ch[channel].data = data;
  131. }
  132. return 0;
  133. }
  134. EXPORT_SYMBOL(set_dma_callback);
  135. /**
  136. * clear_dma_buffer - clear DMA fifos for specified channel
  137. *
  138. * Set the Buffer Clear bit in the Configuration register of specific DMA
  139. * channel. This will stop the descriptor based DMA operation.
  140. */
  141. static void clear_dma_buffer(unsigned int channel)
  142. {
  143. dma_ch[channel].regs->cfg |= RESTART;
  144. SSYNC();
  145. dma_ch[channel].regs->cfg &= ~RESTART;
  146. }
  147. void free_dma(unsigned int channel)
  148. {
  149. pr_debug("freedma() : BEGIN \n");
  150. BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
  151. && channel < MAX_DMA_CHANNELS));
  152. /* Halt the DMA */
  153. disable_dma(channel);
  154. clear_dma_buffer(channel);
  155. if (dma_ch[channel].irq)
  156. free_irq(dma_ch[channel].irq, dma_ch[channel].data);
  157. /* Clear the DMA Variable in the Channel */
  158. mutex_lock(&(dma_ch[channel].dmalock));
  159. dma_ch[channel].chan_status = DMA_CHANNEL_FREE;
  160. mutex_unlock(&(dma_ch[channel].dmalock));
  161. pr_debug("freedma() : END \n");
  162. }
  163. EXPORT_SYMBOL(free_dma);
  164. #ifdef CONFIG_PM
  165. # ifndef MAX_DMA_SUSPEND_CHANNELS
  166. # define MAX_DMA_SUSPEND_CHANNELS MAX_DMA_CHANNELS
  167. # endif
  168. int blackfin_dma_suspend(void)
  169. {
  170. int i;
  171. for (i = 0; i < MAX_DMA_SUSPEND_CHANNELS; ++i) {
  172. if (dma_ch[i].chan_status == DMA_CHANNEL_ENABLED) {
  173. printk(KERN_ERR "DMA Channel %d failed to suspend\n", i);
  174. return -EBUSY;
  175. }
  176. dma_ch[i].saved_peripheral_map = dma_ch[i].regs->peripheral_map;
  177. }
  178. return 0;
  179. }
  180. void blackfin_dma_resume(void)
  181. {
  182. int i;
  183. for (i = 0; i < MAX_DMA_SUSPEND_CHANNELS; ++i)
  184. dma_ch[i].regs->peripheral_map = dma_ch[i].saved_peripheral_map;
  185. }
  186. #endif
  187. /**
  188. * blackfin_dma_early_init - minimal DMA init
  189. *
  190. * Setup a few DMA registers so we can safely do DMA transfers early on in
  191. * the kernel booting process. Really this just means using dma_memcpy().
  192. */
  193. void __init blackfin_dma_early_init(void)
  194. {
  195. bfin_write_MDMA_S0_CONFIG(0);
  196. }
  197. /**
  198. * __dma_memcpy - program the MDMA registers
  199. *
  200. * Actually program MDMA0 and wait for the transfer to finish. Disable IRQs
  201. * while programming registers so that everything is fully configured. Wait
  202. * for DMA to finish with IRQs enabled. If interrupted, the initial DMA_DONE
  203. * check will make sure we don't clobber any existing transfer.
  204. */
  205. static void __dma_memcpy(u32 daddr, s16 dmod, u32 saddr, s16 smod, size_t cnt, u32 conf)
  206. {
  207. static DEFINE_SPINLOCK(mdma_lock);
  208. unsigned long flags;
  209. spin_lock_irqsave(&mdma_lock, flags);
  210. if (bfin_read_MDMA_S0_CONFIG())
  211. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  212. continue;
  213. if (conf & DMA2D) {
  214. /* For larger bit sizes, we've already divided down cnt so it
  215. * is no longer a multiple of 64k. So we have to break down
  216. * the limit here so it is a multiple of the incoming size.
  217. * There is no limitation here in terms of total size other
  218. * than the hardware though as the bits lost in the shift are
  219. * made up by MODIFY (== we can hit the whole address space).
  220. * X: (2^(16 - 0)) * 1 == (2^(16 - 1)) * 2 == (2^(16 - 2)) * 4
  221. */
  222. u32 shift = abs(dmod) >> 1;
  223. size_t ycnt = cnt >> (16 - shift);
  224. cnt = 1 << (16 - shift);
  225. bfin_write_MDMA_D0_Y_COUNT(ycnt);
  226. bfin_write_MDMA_S0_Y_COUNT(ycnt);
  227. bfin_write_MDMA_D0_Y_MODIFY(dmod);
  228. bfin_write_MDMA_S0_Y_MODIFY(smod);
  229. }
  230. bfin_write_MDMA_D0_START_ADDR(daddr);
  231. bfin_write_MDMA_D0_X_COUNT(cnt);
  232. bfin_write_MDMA_D0_X_MODIFY(dmod);
  233. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  234. bfin_write_MDMA_S0_START_ADDR(saddr);
  235. bfin_write_MDMA_S0_X_COUNT(cnt);
  236. bfin_write_MDMA_S0_X_MODIFY(smod);
  237. bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  238. bfin_write_MDMA_S0_CONFIG(DMAEN | conf);
  239. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | conf);
  240. spin_unlock_irqrestore(&mdma_lock, flags);
  241. SSYNC();
  242. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  243. if (bfin_read_MDMA_S0_CONFIG())
  244. continue;
  245. else
  246. return;
  247. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  248. bfin_write_MDMA_S0_CONFIG(0);
  249. bfin_write_MDMA_D0_CONFIG(0);
  250. }
  251. /**
  252. * _dma_memcpy - translate C memcpy settings into MDMA settings
  253. *
  254. * Handle all the high level steps before we touch the MDMA registers. So
  255. * handle caching, tweaking of sizes, and formatting of addresses.
  256. */
  257. static void *_dma_memcpy(void *pdst, const void *psrc, size_t size)
  258. {
  259. u32 conf, shift;
  260. s16 mod;
  261. unsigned long dst = (unsigned long)pdst;
  262. unsigned long src = (unsigned long)psrc;
  263. if (size == 0)
  264. return NULL;
  265. if (bfin_addr_dcachable(src))
  266. blackfin_dcache_flush_range(src, src + size);
  267. if (bfin_addr_dcachable(dst))
  268. blackfin_dcache_invalidate_range(dst, dst + size);
  269. if (dst % 4 == 0 && src % 4 == 0 && size % 4 == 0) {
  270. conf = WDSIZE_32;
  271. shift = 2;
  272. } else if (dst % 2 == 0 && src % 2 == 0 && size % 2 == 0) {
  273. conf = WDSIZE_16;
  274. shift = 1;
  275. } else {
  276. conf = WDSIZE_8;
  277. shift = 0;
  278. }
  279. /* If the two memory regions have a chance of overlapping, make
  280. * sure the memcpy still works as expected. Do this by having the
  281. * copy run backwards instead.
  282. */
  283. mod = 1 << shift;
  284. if (src < dst) {
  285. mod *= -1;
  286. dst += size + mod;
  287. src += size + mod;
  288. }
  289. size >>= shift;
  290. if (size > 0x10000)
  291. conf |= DMA2D;
  292. __dma_memcpy(dst, mod, src, mod, size, conf);
  293. return pdst;
  294. }
  295. /**
  296. * dma_memcpy - DMA memcpy under mutex lock
  297. *
  298. * Do not check arguments before starting the DMA memcpy. Break the transfer
  299. * up into two pieces. The first transfer is in multiples of 64k and the
  300. * second transfer is the piece smaller than 64k.
  301. */
  302. void *dma_memcpy(void *dst, const void *src, size_t size)
  303. {
  304. size_t bulk, rest;
  305. bulk = size & ~0xffff;
  306. rest = size - bulk;
  307. if (bulk)
  308. _dma_memcpy(dst, src, bulk);
  309. _dma_memcpy(dst + bulk, src + bulk, rest);
  310. return dst;
  311. }
  312. EXPORT_SYMBOL(dma_memcpy);
  313. /**
  314. * safe_dma_memcpy - DMA memcpy w/argument checking
  315. *
  316. * Verify arguments are safe before heading to dma_memcpy().
  317. */
  318. void *safe_dma_memcpy(void *dst, const void *src, size_t size)
  319. {
  320. if (!access_ok(VERIFY_WRITE, dst, size))
  321. return NULL;
  322. if (!access_ok(VERIFY_READ, src, size))
  323. return NULL;
  324. return dma_memcpy(dst, src, size);
  325. }
  326. EXPORT_SYMBOL(safe_dma_memcpy);
  327. static void _dma_out(unsigned long addr, unsigned long buf, unsigned short len,
  328. u16 size, u16 dma_size)
  329. {
  330. blackfin_dcache_flush_range(buf, buf + len * size);
  331. __dma_memcpy(addr, 0, buf, size, len, dma_size);
  332. }
  333. static void _dma_in(unsigned long addr, unsigned long buf, unsigned short len,
  334. u16 size, u16 dma_size)
  335. {
  336. blackfin_dcache_invalidate_range(buf, buf + len * size);
  337. __dma_memcpy(buf, size, addr, 0, len, dma_size);
  338. }
  339. #define MAKE_DMA_IO(io, bwl, isize, dmasize, cnst) \
  340. void dma_##io##s##bwl(unsigned long addr, cnst void *buf, unsigned short len) \
  341. { \
  342. _dma_##io(addr, (unsigned long)buf, len, isize, WDSIZE_##dmasize); \
  343. } \
  344. EXPORT_SYMBOL(dma_##io##s##bwl)
  345. MAKE_DMA_IO(out, b, 1, 8, const);
  346. MAKE_DMA_IO(in, b, 1, 8, );
  347. MAKE_DMA_IO(out, w, 2, 16, const);
  348. MAKE_DMA_IO(in, w, 2, 16, );
  349. MAKE_DMA_IO(out, l, 4, 32, const);
  350. MAKE_DMA_IO(in, l, 4, 32, );