bfin_dma_5xx.c 13 KB

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