standardDeviationMemoryDefinition.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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();