bfin_dma.c 17 KB

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