bfin_dma_5xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. bfin_write_MDMA_S1_CONFIG(0);
  197. }
  198. void __init early_dma_memcpy(void *pdst, const void *psrc, size_t size)
  199. {
  200. unsigned long dst = (unsigned long)pdst;
  201. unsigned long src = (unsigned long)psrc;
  202. struct dma_register *dst_ch, *src_ch;
  203. /* We assume that everything is 4 byte aligned, so include
  204. * a basic sanity check
  205. */
  206. BUG_ON(dst % 4);
  207. BUG_ON(src % 4);
  208. BUG_ON(size % 4);
  209. /* Force a sync in case a previous config reset on this channel
  210. * occurred. This is needed so subsequent writes to DMA registers
  211. * are not spuriously lost/corrupted.
  212. */
  213. __builtin_bfin_ssync();
  214. src_ch = 0;
  215. /* Find an avalible memDMA channel */
  216. while (1) {
  217. if (!src_ch || src_ch == (struct dma_register *)MDMA_S1_NEXT_DESC_PTR) {
  218. dst_ch = (struct dma_register *)MDMA_D0_NEXT_DESC_PTR;
  219. src_ch = (struct dma_register *)MDMA_S0_NEXT_DESC_PTR;
  220. } else {
  221. dst_ch = (struct dma_register *)MDMA_D1_NEXT_DESC_PTR;
  222. src_ch = (struct dma_register *)MDMA_S1_NEXT_DESC_PTR;
  223. }
  224. if (!bfin_read16(&src_ch->cfg)) {
  225. break;
  226. } else {
  227. if (bfin_read16(&src_ch->irq_status) & DMA_DONE)
  228. bfin_write16(&src_ch->cfg, 0);
  229. }
  230. }
  231. /* Destination */
  232. bfin_write32(&dst_ch->start_addr, dst);
  233. bfin_write16(&dst_ch->x_count, size >> 2);
  234. bfin_write16(&dst_ch->x_modify, 1 << 2);
  235. bfin_write16(&dst_ch->irq_status, DMA_DONE | DMA_ERR);
  236. /* Source */
  237. bfin_write32(&src_ch->start_addr, src);
  238. bfin_write16(&src_ch->x_count, size >> 2);
  239. bfin_write16(&src_ch->x_modify, 1 << 2);
  240. bfin_write16(&src_ch->irq_status, DMA_DONE | DMA_ERR);
  241. /* Enable */
  242. bfin_write16(&src_ch->cfg, DMAEN | WDSIZE_32);
  243. bfin_write16(&dst_ch->cfg, WNR | DI_EN | DMAEN | WDSIZE_32);
  244. /* Since we are atomic now, don't use the workaround ssync */
  245. __builtin_bfin_ssync();
  246. }
  247. void __init early_dma_memcpy_done(void)
  248. {
  249. while ((bfin_read_MDMA_S0_CONFIG() && !(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE)) ||
  250. (bfin_read_MDMA_S1_CONFIG() && !(bfin_read_MDMA_D1_IRQ_STATUS() & DMA_DONE)))
  251. continue;
  252. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  253. bfin_write_MDMA_D1_IRQ_STATUS(DMA_DONE | DMA_ERR);
  254. /*
  255. * Now that DMA is done, we would normally flush cache, but
  256. * i/d cache isn't running this early, so we don't bother,
  257. * and just clear out the DMA channel for next time
  258. */
  259. bfin_write_MDMA_S0_CONFIG(0);
  260. bfin_write_MDMA_S1_CONFIG(0);
  261. bfin_write_MDMA_D0_CONFIG(0);
  262. bfin_write_MDMA_D1_CONFIG(0);
  263. __builtin_bfin_ssync();
  264. }
  265. /**
  266. * __dma_memcpy - program the MDMA registers
  267. *
  268. * Actually program MDMA0 and wait for the transfer to finish. Disable IRQs
  269. * while programming registers so that everything is fully configured. Wait
  270. * for DMA to finish with IRQs enabled. If interrupted, the initial DMA_DONE
  271. * check will make sure we don't clobber any existing transfer.
  272. */
  273. static void __dma_memcpy(u32 daddr, s16 dmod, u32 saddr, s16 smod, size_t cnt, u32 conf)
  274. {
  275. static DEFINE_SPINLOCK(mdma_lock);
  276. unsigned long flags;
  277. spin_lock_irqsave(&mdma_lock, flags);
  278. /* Force a sync in case a previous config reset on this channel
  279. * occurred. This is needed so subsequent writes to DMA registers
  280. * are not spuriously lost/corrupted. Do it under irq lock and
  281. * without the anomaly version (because we are atomic already).
  282. */
  283. __builtin_bfin_ssync();
  284. if (bfin_read_MDMA_S0_CONFIG())
  285. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  286. continue;
  287. if (conf & DMA2D) {
  288. /* For larger bit sizes, we've already divided down cnt so it
  289. * is no longer a multiple of 64k. So we have to break down
  290. * the limit here so it is a multiple of the incoming size.
  291. * There is no limitation here in terms of total size other
  292. * than the hardware though as the bits lost in the shift are
  293. * made up by MODIFY (== we can hit the whole address space).
  294. * X: (2^(16 - 0)) * 1 == (2^(16 - 1)) * 2 == (2^(16 - 2)) * 4
  295. */
  296. u32 shift = abs(dmod) >> 1;
  297. size_t ycnt = cnt >> (16 - shift);
  298. cnt = 1 << (16 - shift);
  299. bfin_write_MDMA_D0_Y_COUNT(ycnt);
  300. bfin_write_MDMA_S0_Y_COUNT(ycnt);
  301. bfin_write_MDMA_D0_Y_MODIFY(dmod);
  302. bfin_write_MDMA_S0_Y_MODIFY(smod);
  303. }
  304. bfin_write_MDMA_D0_START_ADDR(daddr);
  305. bfin_write_MDMA_D0_X_COUNT(cnt);
  306. bfin_write_MDMA_D0_X_MODIFY(dmod);
  307. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  308. bfin_write_MDMA_S0_START_ADDR(saddr);
  309. bfin_write_MDMA_S0_X_COUNT(cnt);
  310. bfin_write_MDMA_S0_X_MODIFY(smod);
  311. bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  312. bfin_write_MDMA_S0_CONFIG(DMAEN | conf);
  313. bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | conf);
  314. spin_unlock_irqrestore(&mdma_lock, flags);
  315. SSYNC();
  316. while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
  317. if (bfin_read_MDMA_S0_CONFIG())
  318. continue;
  319. else
  320. return;
  321. bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
  322. bfin_write_MDMA_S0_CONFIG(0);
  323. bfin_write_MDMA_D0_CONFIG(0);
  324. }
  325. /**
  326. * _dma_memcpy - translate C memcpy settings into MDMA settings
  327. *
  328. * Handle all the high level steps before we touch the MDMA registers. So
  329. * handle direction, tweaking of sizes, and formatting of addresses.
  330. */
  331. static void *_dma_memcpy(void *pdst, const void *psrc, size_t size)
  332. {
  333. u32 conf, shift;
  334. s16 mod;
  335. unsigned long dst = (unsigned long)pdst;
  336. unsigned long src = (unsigned long)psrc;
  337. if (size == 0)
  338. return NULL;
  339. if (dst % 4 == 0 && src % 4 == 0 && size % 4 == 0) {
  340. conf = WDSIZE_32;
  341. shift = 2;
  342. } else if (dst % 2 == 0 && src % 2 == 0 && size % 2 == 0) {
  343. conf = WDSIZE_16;
  344. shift = 1;
  345. } else {
  346. conf = WDSIZE_8;
  347. shift = 0;
  348. }
  349. /* If the two memory regions have a chance of overlapping, make
  350. * sure the memcpy still works as expected. Do this by having the
  351. * copy run backwards instead.
  352. */
  353. mod = 1 << shift;
  354. if (src < dst) {
  355. mod *= -1;
  356. dst += size + mod;
  357. src += size + mod;
  358. }
  359. size >>= shift;
  360. if (size > 0x10000)
  361. conf |= DMA2D;
  362. __dma_memcpy(dst, mod, src, mod, size, conf);
  363. return pdst;
  364. }
  365. /**
  366. * dma_memcpy - DMA memcpy under mutex lock
  367. *
  368. * Do not check arguments before starting the DMA memcpy. Break the transfer
  369. * up into two pieces. The first transfer is in multiples of 64k and the
  370. * second transfer is the piece smaller than 64k.
  371. */
  372. void *dma_memcpy(void *pdst, const void *psrc, size_t size)
  373. {
  374. unsigned long dst = (unsigned long)pdst;
  375. unsigned long src = (unsigned long)psrc;
  376. size_t bulk, rest;
  377. if (bfin_addr_dcachable(src))
  378. blackfin_dcache_flush_range(src, src + size);
  379. if (bfin_addr_dcachable(dst))
  380. blackfin_dcache_invalidate_range(dst, dst + size);
  381. bulk = size & ~0xffff;
  382. rest = size - bulk;
  383. if (bulk)
  384. _dma_memcpy(pdst, psrc, bulk);
  385. _dma_memcpy(pdst + bulk, psrc + bulk, rest);
  386. return pdst;
  387. }
  388. EXPORT_SYMBOL(dma_memcpy);
  389. /**
  390. * safe_dma_memcpy - DMA memcpy w/argument checking
  391. *
  392. * Verify arguments are safe before heading to dma_memcpy().
  393. */
  394. void *safe_dma_memcpy(void *dst, const void *src, size_t size)
  395. {
  396. if (!access_ok(VERIFY_WRITE, dst, size))
  397. return NULL;
  398. if (!access_ok(VERIFY_READ, src, size))
  399. return NULL;
  400. return dma_memcpy(dst, src, size);
  401. }
  402. EXPORT_SYMBOL(safe_dma_memcpy);
  403. static void _dma_out(unsigned long addr, unsigned long buf, unsigned short len,
  404. u16 size, u16 dma_size)
  405. {
  406. blackfin_dcache_flush_range(buf, buf + len * size);
  407. __dma_memcpy(addr, 0, buf, size, len, dma_size);
  408. }
  409. static void _dma_in(unsigned long addr, unsigned long buf, unsigned short len,
  410. u16 size, u16 dma_size)
  411. {
  412. blackfin_dcache_invalidate_range(buf, buf + len * size);
  413. __dma_memcpy(buf, size, addr, 0, len, dma_size);
  414. }
  415. #define MAKE_DMA_IO(io, bwl, isize, dmasize, cnst) \
  416. void dma_##io##s##bwl(unsigned long addr, cnst void *buf, unsigned short len) \
  417. { \
  418. _dma_##io(addr, (unsigned long)buf, len, isize, WDSIZE_##dmasize); \
  419. } \
  420. EXPORT_SYMBOL(dma_##io##s##bwl)
  421. MAKE_DMA_IO(out, b, 1, 8, const);
  422. MAKE_DMA_IO(in, b, 1, 8, );
  423. MAKE_DMA_IO(out, w, 2, 16, const);
  424. MAKE_DMA_IO(in, w, 2, 16, );
  425. MAKE_DMA_IO(out, l, 4, 32, const);
  426. MAKE_DMA_IO(in, l, 4, 32, );