bfin_dma.c 14 KB

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