bfin_dma_5xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. /* Force a sync in case a previous config reset on this channel
  211. * occurred. This is needed so subsequent writes to DMA registers
  212. * are not spuriously lost/corrupted. Do it under irq lock and
  213. * without the anomaly version (because we are atomic already).
  214. */
  215. __builtin_bfin_ssync();
  216. if (bfin_read_MDMA_S0_CONFIG())
  217. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  218. continue;
  219. if (conf & DMA2D) {
  220. /* For larger bit sizes, we've already divided down cnt so it
  221. * is no longer a multiple of 64k. So we have to break down
  222. * the limit here so it is a multiple of the incoming size.
  223. * There is no limitation here in terms of total size other
  224. * than the hardware though as the bits lost in the shift are
  225. * made up by MODIFY (== we can hit the whole address space).
  226. * X: (2^(16 - 0)) * 1 == (2^(16 - 1)) * 2 == (2^(16 - 2)) * 4
  227. */
  228. u32 shift = abs(dmod) >> 1;
  229. size_t ycnt = cnt >> (16 - shift);
  230. cnt = 1 << (16 - shift);
  231. bfin_write_MDMA_D0_Y_COUNT(ycnt);
  232. bfin_write_MDMA_S0_Y_COUNT(ycnt);
  233. bfin_write_MDMA_D0_Y_MODIFY(dmod);
  234. bfin_write_MDMA_S0_Y_MODIFY(smod);
  235. }
  236. bfin_write_MDMA_D0_START_ADDR(daddr);
  237. bfin_write_MDMA_D0_X_COUNT(cnt);
  238. bfin_write_MDMA_D0_X_MODIFY(dmod);
  239. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  240. bfin_write_MDMA_S0_START_ADDR(saddr);
  241. bfin_write_MDMA_S0_X_COUNT(cnt);
  242. bfin_write_MDMA_S0_X_MODIFY(smod);
  243. bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  244. bfin_write_MDMA_S0_CONFIG(DMAEN | conf);
  245. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | conf);
  246. spin_unlock_irqrestore(&mdma_lock, flags);
  247. SSYNC();
  248. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  249. if (bfin_read_MDMA_S0_CONFIG())
  250. continue;
  251. else
  252. return;
  253. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  254. bfin_write_MDMA_S0_CONFIG(0);
  255. bfin_write_MDMA_D0_CONFIG(0);
  256. }
  257. /**
  258. * _dma_memcpy - translate C memcpy settings into MDMA settings
  259. *
  260. * Handle all the high level steps before we touch the MDMA registers. So
  261. * handle direction, tweaking of sizes, and formatting of addresses.
  262. */
  263. static void *_dma_memcpy(void *pdst, const void *psrc, size_t size)
  264. {
  265. u32 conf, shift;
  266. s16 mod;
  267. unsigned long dst = (unsigned long)pdst;
  268. unsigned long src = (unsigned long)psrc;
  269. if (size == 0)
  270. return NULL;
  271. if (dst % 4 == 0 && src % 4 == 0 && size % 4 == 0) {
  272. conf = WDSIZE_32;
  273. shift = 2;
  274. } else if (dst % 2 == 0 && src % 2 == 0 && size % 2 == 0) {
  275. conf = WDSIZE_16;
  276. shift = 1;
  277. } else {
  278. conf = WDSIZE_8;
  279. shift = 0;
  280. }
  281. /* If the two memory regions have a chance of overlapping, make
  282. * sure the memcpy still works as expected. Do this by having the
  283. * copy run backwards instead.
  284. */
  285. mod = 1 << shift;
  286. if (src < dst) {
  287. mod *= -1;
  288. dst += size + mod;
  289. src += size + mod;
  290. }
  291. size >>= shift;
  292. if (size > 0x10000)
  293. conf |= DMA2D;
  294. __dma_memcpy(dst, mod, src, mod, size, conf);
  295. return pdst;
  296. }
  297. /**
  298. * dma_memcpy - DMA memcpy under mutex lock
  299. *
  300. * Do not check arguments before starting the DMA memcpy. Break the transfer
  301. * up into two pieces. The first transfer is in multiples of 64k and the
  302. * second transfer is the piece smaller than 64k.
  303. */
  304. void *dma_memcpy(void *pdst, const void *psrc, size_t size)
  305. {
  306. unsigned long dst = (unsigned long)pdst;
  307. unsigned long src = (unsigned long)psrc;
  308. size_t bulk, rest;
  309. if (bfin_addr_dcachable(src))
  310. blackfin_dcache_flush_range(src, src + size);
  311. if (bfin_addr_dcachable(dst))
  312. blackfin_dcache_invalidate_range(dst, dst + size);
  313. bulk = size & ~0xffff;
  314. rest = size - bulk;
  315. if (bulk)
  316. _dma_memcpy(pdst, psrc, bulk);
  317. _dma_memcpy(pdst + bulk, psrc + bulk, rest);
  318. return pdst;
  319. }
  320. EXPORT_SYMBOL(dma_memcpy);
  321. /**
  322. * safe_dma_memcpy - DMA memcpy w/argument checking
  323. *
  324. * Verify arguments are safe before heading to dma_memcpy().
  325. */
  326. void *safe_dma_memcpy(void *dst, const void *src, size_t size)
  327. {
  328. if (!access_ok(VERIFY_WRITE, dst, size))
  329. return NULL;
  330. if (!access_ok(VERIFY_READ, src, size))
  331. return NULL;
  332. return dma_memcpy(dst, src, size);
  333. }
  334. EXPORT_SYMBOL(safe_dma_memcpy);
  335. static void _dma_out(unsigned long addr, unsigned long buf, unsigned short len,
  336. u16 size, u16 dma_size)
  337. {
  338. blackfin_dcache_flush_range(buf, buf + len * size);
  339. __dma_memcpy(addr, 0, buf, size, len, dma_size);
  340. }
  341. static void _dma_in(unsigned long addr, unsigned long buf, unsigned short len,
  342. u16 size, u16 dma_size)
  343. {
  344. blackfin_dcache_invalidate_range(buf, buf + len * size);
  345. __dma_memcpy(buf, size, addr, 0, len, dma_size);
  346. }
  347. #define MAKE_DMA_IO(io, bwl, isize, dmasize, cnst) \
  348. void dma_##io##s##bwl(unsigned long addr, cnst void *buf, unsigned short len) \
  349. { \
  350. _dma_##io(addr, (unsigned long)buf, len, isize, WDSIZE_##dmasize); \
  351. } \
  352. EXPORT_SYMBOL(dma_##io##s##bwl)
  353. MAKE_DMA_IO(out, b, 1, 8, const);
  354. MAKE_DMA_IO(in, b, 1, 8, );
  355. MAKE_DMA_IO(out, w, 2, 16, const);
  356. MAKE_DMA_IO(in, w, 2, 16, );
  357. MAKE_DMA_IO(out, l, 4, 32, const);
  358. MAKE_DMA_IO(in, l, 4, 32, );