Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
Bob Liu | 5ddebe5 | 2012-03-19 13:50:27 +0800 | [diff] [blame^] | 2 | * bfin_dma.c - Blackfin DMA implementation |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 3 | * |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 4 | * Copyright 2004-2008 Analog Devices Inc. |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 5 | * |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 6 | * Licensed under the GPL-2 or later. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/errno.h> |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/kernel.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 13 | #include <linux/param.h> |
Graf Yang | d642a8a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 14 | #include <linux/proc_fs.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 15 | #include <linux/sched.h> |
Graf Yang | d642a8a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 16 | #include <linux/seq_file.h> |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 17 | #include <linux/spinlock.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 18 | |
Roy Huang | 24a07a1 | 2007-07-12 22:41:45 +0800 | [diff] [blame] | 19 | #include <asm/blackfin.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 20 | #include <asm/cacheflush.h> |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 21 | #include <asm/dma.h> |
| 22 | #include <asm/uaccess.h> |
Robin Getz | 837ec2d | 2009-07-07 20:17:09 +0000 | [diff] [blame] | 23 | #include <asm/early_printk.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 24 | |
Robin Getz | 76068c3 | 2009-04-23 20:56:42 +0000 | [diff] [blame] | 25 | /* |
| 26 | * To make sure we work around 05000119 - we always check DMA_DONE bit, |
| 27 | * never the DMA_RUN bit |
| 28 | */ |
| 29 | |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 30 | struct dma_channel dma_ch[MAX_DMA_CHANNELS]; |
| 31 | EXPORT_SYMBOL(dma_ch); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 32 | |
Mike Frysinger | a161bb0 | 2007-05-21 18:09:14 +0800 | [diff] [blame] | 33 | static int __init blackfin_dma_init(void) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 34 | { |
| 35 | int i; |
| 36 | |
| 37 | printk(KERN_INFO "Blackfin DMA Controller\n"); |
| 38 | |
Steven Miao | f9691bb | 2011-05-05 14:14:48 +0800 | [diff] [blame] | 39 | |
| 40 | #if ANOMALY_05000480 |
| 41 | bfin_write_DMAC_TC_PER(0x0111); |
| 42 | #endif |
| 43 | |
Mike Frysinger | 211daf9 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 44 | for (i = 0; i < MAX_DMA_CHANNELS; i++) { |
Mike Frysinger | d2e015d | 2009-10-09 22:18:12 +0000 | [diff] [blame] | 45 | atomic_set(&dma_ch[i].chan_status, 0); |
Bernd Schmidt | 7795566 | 2008-04-24 05:31:18 +0800 | [diff] [blame] | 46 | dma_ch[i].regs = dma_io_base_addr[i]; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 47 | } |
Michael Hennerich | 23ee968 | 2007-05-21 18:09:17 +0800 | [diff] [blame] | 48 | /* Mark MEMDMA Channel 0 as requested since we're using it internally */ |
Graf Yang | d642a8a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 49 | request_dma(CH_MEM_STREAM0_DEST, "Blackfin dma_memcpy"); |
| 50 | request_dma(CH_MEM_STREAM0_SRC, "Blackfin dma_memcpy"); |
Michael Hennerich | a924db7 | 2007-08-03 17:43:29 +0800 | [diff] [blame] | 51 | |
| 52 | #if defined(CONFIG_DEB_DMA_URGENT) |
| 53 | bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE() |
| 54 | | DEB1_URGENT | DEB2_URGENT | DEB3_URGENT); |
| 55 | #endif |
Graf Yang | d642a8a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | arch_initcall(blackfin_dma_init); |
| 60 | |
| 61 | #ifdef CONFIG_PROC_FS |
Graf Yang | d642a8a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 62 | static int proc_dma_show(struct seq_file *m, void *v) |
| 63 | { |
| 64 | int i; |
| 65 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 66 | for (i = 0; i < MAX_DMA_CHANNELS; ++i) |
Mike Frysinger | d2e015d | 2009-10-09 22:18:12 +0000 | [diff] [blame] | 67 | if (dma_channel_active(i)) |
Graf Yang | d642a8a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 68 | seq_printf(m, "%2d: %s\n", i, dma_ch[i].device_id); |
| 69 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Graf Yang | d642a8a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 73 | static int proc_dma_open(struct inode *inode, struct file *file) |
| 74 | { |
| 75 | return single_open(file, proc_dma_show, NULL); |
| 76 | } |
| 77 | |
| 78 | static const struct file_operations proc_dma_operations = { |
| 79 | .open = proc_dma_open, |
| 80 | .read = seq_read, |
| 81 | .llseek = seq_lseek, |
| 82 | .release = single_release, |
| 83 | }; |
| 84 | |
| 85 | static int __init proc_dma_init(void) |
| 86 | { |
| 87 | return proc_create("dma", 0, NULL, &proc_dma_operations) != NULL; |
| 88 | } |
| 89 | late_initcall(proc_dma_init); |
| 90 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 91 | |
steven miao | 5583517 | 2010-10-22 08:48:41 +0000 | [diff] [blame] | 92 | static void set_dma_peripheral_map(unsigned int channel, const char *device_id) |
| 93 | { |
| 94 | #ifdef CONFIG_BF54x |
| 95 | unsigned int per_map; |
| 96 | |
| 97 | switch (channel) { |
| 98 | case CH_UART2_RX: per_map = 0xC << 12; break; |
| 99 | case CH_UART2_TX: per_map = 0xD << 12; break; |
| 100 | case CH_UART3_RX: per_map = 0xE << 12; break; |
| 101 | case CH_UART3_TX: per_map = 0xF << 12; break; |
| 102 | default: return; |
| 103 | } |
| 104 | |
| 105 | if (strncmp(device_id, "BFIN_UART", 9) == 0) |
| 106 | dma_ch[channel].regs->peripheral_map = per_map; |
| 107 | #endif |
| 108 | } |
| 109 | |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 110 | /** |
| 111 | * request_dma - request a DMA channel |
| 112 | * |
| 113 | * Request the specific DMA channel from the system if it's available. |
| 114 | */ |
Michael McTernan | 99532fd | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 115 | int request_dma(unsigned int channel, const char *device_id) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 116 | { |
Frans Pop | 2bc4aff | 2010-02-06 18:47:18 +0100 | [diff] [blame] | 117 | pr_debug("request_dma() : BEGIN\n"); |
Michael Hennerich | 5ce998c | 2008-05-17 15:57:01 +0800 | [diff] [blame] | 118 | |
Graf Yang | d642a8a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 119 | if (device_id == NULL) |
| 120 | printk(KERN_WARNING "request_dma(%u): no device_id given\n", channel); |
| 121 | |
Michael Hennerich | 5ce998c | 2008-05-17 15:57:01 +0800 | [diff] [blame] | 122 | #if defined(CONFIG_BF561) && ANOMALY_05000182 |
| 123 | if (channel >= CH_IMEM_STREAM0_DEST && channel <= CH_IMEM_STREAM1_DEST) { |
| 124 | if (get_cclk() > 500000000) { |
| 125 | printk(KERN_WARNING |
| 126 | "Request IMDMA failed due to ANOMALY 05000182\n"); |
| 127 | return -EFAULT; |
| 128 | } |
| 129 | } |
| 130 | #endif |
| 131 | |
Mike Frysinger | d2e015d | 2009-10-09 22:18:12 +0000 | [diff] [blame] | 132 | if (atomic_cmpxchg(&dma_ch[channel].chan_status, 0, 1)) { |
Frans Pop | 2bc4aff | 2010-02-06 18:47:18 +0100 | [diff] [blame] | 133 | pr_debug("DMA CHANNEL IN USE\n"); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 134 | return -EBUSY; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 135 | } |
| 136 | |
steven miao | 5583517 | 2010-10-22 08:48:41 +0000 | [diff] [blame] | 137 | set_dma_peripheral_map(channel, device_id); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 138 | dma_ch[channel].device_id = device_id; |
Mike Frysinger | 9b01140 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 139 | dma_ch[channel].irq = 0; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 140 | |
| 141 | /* This is to be enabled by putting a restriction - |
| 142 | * you have to request DMA, before doing any operations on |
| 143 | * descriptor/channel |
| 144 | */ |
Frans Pop | 2bc4aff | 2010-02-06 18:47:18 +0100 | [diff] [blame] | 145 | pr_debug("request_dma() : END\n"); |
Mike Frysinger | 596b565 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 146 | return 0; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 147 | } |
| 148 | EXPORT_SYMBOL(request_dma); |
| 149 | |
Mike Frysinger | 68532bd | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 150 | int set_dma_callback(unsigned int channel, irq_handler_t callback, void *data) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 151 | { |
Mike Frysinger | e34132f | 2009-11-24 18:36:36 +0000 | [diff] [blame] | 152 | int ret; |
| 153 | unsigned int irq; |
| 154 | |
| 155 | BUG_ON(channel >= MAX_DMA_CHANNELS || !callback || |
Mike Frysinger | d2e015d | 2009-10-09 22:18:12 +0000 | [diff] [blame] | 156 | !atomic_read(&dma_ch[channel].chan_status)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 157 | |
Mike Frysinger | e34132f | 2009-11-24 18:36:36 +0000 | [diff] [blame] | 158 | irq = channel2irq(channel); |
| 159 | ret = request_irq(irq, callback, 0, dma_ch[channel].device_id, data); |
| 160 | if (ret) |
| 161 | return ret; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 162 | |
Mike Frysinger | e34132f | 2009-11-24 18:36:36 +0000 | [diff] [blame] | 163 | dma_ch[channel].irq = irq; |
| 164 | dma_ch[channel].data = data; |
Mike Frysinger | 8f1cc23 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 165 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | EXPORT_SYMBOL(set_dma_callback); |
| 169 | |
Mike Frysinger | 9c417a4 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 170 | /** |
| 171 | * clear_dma_buffer - clear DMA fifos for specified channel |
| 172 | * |
| 173 | * Set the Buffer Clear bit in the Configuration register of specific DMA |
| 174 | * channel. This will stop the descriptor based DMA operation. |
| 175 | */ |
| 176 | static void clear_dma_buffer(unsigned int channel) |
| 177 | { |
| 178 | dma_ch[channel].regs->cfg |= RESTART; |
| 179 | SSYNC(); |
| 180 | dma_ch[channel].regs->cfg &= ~RESTART; |
| 181 | } |
| 182 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 183 | void free_dma(unsigned int channel) |
| 184 | { |
Frans Pop | 2bc4aff | 2010-02-06 18:47:18 +0100 | [diff] [blame] | 185 | pr_debug("freedma() : BEGIN\n"); |
Roel Kluin | ac86075 | 2009-08-02 14:26:48 +0200 | [diff] [blame] | 186 | BUG_ON(channel >= MAX_DMA_CHANNELS || |
Mike Frysinger | d2e015d | 2009-10-09 22:18:12 +0000 | [diff] [blame] | 187 | !atomic_read(&dma_ch[channel].chan_status)); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 188 | |
| 189 | /* Halt the DMA */ |
| 190 | disable_dma(channel); |
| 191 | clear_dma_buffer(channel); |
| 192 | |
Mike Frysinger | 9b01140 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 193 | if (dma_ch[channel].irq) |
Michael Hennerich | a2ba8b1 | 2008-10-28 18:19:29 +0800 | [diff] [blame] | 194 | free_irq(dma_ch[channel].irq, dma_ch[channel].data); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 195 | |
| 196 | /* Clear the DMA Variable in the Channel */ |
Mike Frysinger | d2e015d | 2009-10-09 22:18:12 +0000 | [diff] [blame] | 197 | atomic_set(&dma_ch[channel].chan_status, 0); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 198 | |
Frans Pop | 2bc4aff | 2010-02-06 18:47:18 +0100 | [diff] [blame] | 199 | pr_debug("freedma() : END\n"); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 200 | } |
| 201 | EXPORT_SYMBOL(free_dma); |
| 202 | |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 203 | #ifdef CONFIG_PM |
Mike Frysinger | c9e0020 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 204 | # ifndef MAX_DMA_SUSPEND_CHANNELS |
| 205 | # define MAX_DMA_SUSPEND_CHANNELS MAX_DMA_CHANNELS |
| 206 | # endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 207 | int blackfin_dma_suspend(void) |
| 208 | { |
| 209 | int i; |
| 210 | |
Mike Frysinger | d2e015d | 2009-10-09 22:18:12 +0000 | [diff] [blame] | 211 | for (i = 0; i < MAX_DMA_CHANNELS; ++i) { |
| 212 | if (dma_ch[i].regs->cfg & DMAEN) { |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 213 | printk(KERN_ERR "DMA Channel %d failed to suspend\n", i); |
| 214 | return -EBUSY; |
| 215 | } |
| 216 | |
Mike Frysinger | d2e015d | 2009-10-09 22:18:12 +0000 | [diff] [blame] | 217 | if (i < MAX_DMA_SUSPEND_CHANNELS) |
| 218 | dma_ch[i].saved_peripheral_map = dma_ch[i].regs->peripheral_map; |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 219 | } |
| 220 | |
Bob Liu | 5ddebe5 | 2012-03-19 13:50:27 +0800 | [diff] [blame^] | 221 | #if ANOMALY_05000480 |
| 222 | bfin_write_DMAC_TC_PER(0x0); |
| 223 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | void blackfin_dma_resume(void) |
| 228 | { |
| 229 | int i; |
Michael Hennerich | 865bddf | 2009-10-20 13:38:04 +0000 | [diff] [blame] | 230 | |
| 231 | for (i = 0; i < MAX_DMA_CHANNELS; ++i) { |
| 232 | dma_ch[i].regs->cfg = 0; |
| 233 | |
| 234 | if (i < MAX_DMA_SUSPEND_CHANNELS) |
| 235 | dma_ch[i].regs->peripheral_map = dma_ch[i].saved_peripheral_map; |
| 236 | } |
Bob Liu | 5ddebe5 | 2012-03-19 13:50:27 +0800 | [diff] [blame^] | 237 | #if ANOMALY_05000480 |
| 238 | bfin_write_DMAC_TC_PER(0x0111); |
| 239 | #endif |
Michael Hennerich | 1efc80b | 2008-07-19 16:57:32 +0800 | [diff] [blame] | 240 | } |
| 241 | #endif |
| 242 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 243 | /** |
| 244 | * blackfin_dma_early_init - minimal DMA init |
| 245 | * |
| 246 | * Setup a few DMA registers so we can safely do DMA transfers early on in |
| 247 | * the kernel booting process. Really this just means using dma_memcpy(). |
| 248 | */ |
| 249 | void __init blackfin_dma_early_init(void) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 250 | { |
Robin Getz | 837ec2d | 2009-07-07 20:17:09 +0000 | [diff] [blame] | 251 | early_shadow_stamp(); |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 252 | bfin_write_MDMA_S0_CONFIG(0); |
Robin Getz | fecbd73 | 2009-04-23 20:49:43 +0000 | [diff] [blame] | 253 | bfin_write_MDMA_S1_CONFIG(0); |
| 254 | } |
| 255 | |
| 256 | void __init early_dma_memcpy(void *pdst, const void *psrc, size_t size) |
| 257 | { |
| 258 | unsigned long dst = (unsigned long)pdst; |
| 259 | unsigned long src = (unsigned long)psrc; |
| 260 | struct dma_register *dst_ch, *src_ch; |
| 261 | |
Robin Getz | 837ec2d | 2009-07-07 20:17:09 +0000 | [diff] [blame] | 262 | early_shadow_stamp(); |
| 263 | |
Robin Getz | fecbd73 | 2009-04-23 20:49:43 +0000 | [diff] [blame] | 264 | /* We assume that everything is 4 byte aligned, so include |
| 265 | * a basic sanity check |
| 266 | */ |
| 267 | BUG_ON(dst % 4); |
| 268 | BUG_ON(src % 4); |
| 269 | BUG_ON(size % 4); |
| 270 | |
Mike Frysinger | 532f07c | 2009-06-29 22:45:50 +0000 | [diff] [blame] | 271 | src_ch = 0; |
| 272 | /* Find an avalible memDMA channel */ |
| 273 | while (1) { |
| 274 | if (src_ch == (struct dma_register *)MDMA_S0_NEXT_DESC_PTR) { |
| 275 | dst_ch = (struct dma_register *)MDMA_D1_NEXT_DESC_PTR; |
| 276 | src_ch = (struct dma_register *)MDMA_S1_NEXT_DESC_PTR; |
| 277 | } else { |
| 278 | dst_ch = (struct dma_register *)MDMA_D0_NEXT_DESC_PTR; |
| 279 | src_ch = (struct dma_register *)MDMA_S0_NEXT_DESC_PTR; |
| 280 | } |
| 281 | |
| 282 | if (!bfin_read16(&src_ch->cfg)) |
| 283 | break; |
| 284 | else if (bfin_read16(&dst_ch->irq_status) & DMA_DONE) { |
| 285 | bfin_write16(&src_ch->cfg, 0); |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
Robin Getz | fecbd73 | 2009-04-23 20:49:43 +0000 | [diff] [blame] | 290 | /* Force a sync in case a previous config reset on this channel |
| 291 | * occurred. This is needed so subsequent writes to DMA registers |
| 292 | * are not spuriously lost/corrupted. |
| 293 | */ |
| 294 | __builtin_bfin_ssync(); |
| 295 | |
Robin Getz | fecbd73 | 2009-04-23 20:49:43 +0000 | [diff] [blame] | 296 | /* Destination */ |
| 297 | bfin_write32(&dst_ch->start_addr, dst); |
| 298 | bfin_write16(&dst_ch->x_count, size >> 2); |
| 299 | bfin_write16(&dst_ch->x_modify, 1 << 2); |
| 300 | bfin_write16(&dst_ch->irq_status, DMA_DONE | DMA_ERR); |
| 301 | |
| 302 | /* Source */ |
| 303 | bfin_write32(&src_ch->start_addr, src); |
| 304 | bfin_write16(&src_ch->x_count, size >> 2); |
| 305 | bfin_write16(&src_ch->x_modify, 1 << 2); |
| 306 | bfin_write16(&src_ch->irq_status, DMA_DONE | DMA_ERR); |
| 307 | |
| 308 | /* Enable */ |
| 309 | bfin_write16(&src_ch->cfg, DMAEN | WDSIZE_32); |
| 310 | bfin_write16(&dst_ch->cfg, WNR | DI_EN | DMAEN | WDSIZE_32); |
| 311 | |
| 312 | /* Since we are atomic now, don't use the workaround ssync */ |
| 313 | __builtin_bfin_ssync(); |
| 314 | } |
| 315 | |
| 316 | void __init early_dma_memcpy_done(void) |
| 317 | { |
Robin Getz | 837ec2d | 2009-07-07 20:17:09 +0000 | [diff] [blame] | 318 | early_shadow_stamp(); |
| 319 | |
Robin Getz | fecbd73 | 2009-04-23 20:49:43 +0000 | [diff] [blame] | 320 | while ((bfin_read_MDMA_S0_CONFIG() && !(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE)) || |
| 321 | (bfin_read_MDMA_S1_CONFIG() && !(bfin_read_MDMA_D1_IRQ_STATUS() & DMA_DONE))) |
| 322 | continue; |
| 323 | |
| 324 | bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR); |
| 325 | bfin_write_MDMA_D1_IRQ_STATUS(DMA_DONE | DMA_ERR); |
| 326 | /* |
| 327 | * Now that DMA is done, we would normally flush cache, but |
| 328 | * i/d cache isn't running this early, so we don't bother, |
| 329 | * and just clear out the DMA channel for next time |
| 330 | */ |
| 331 | bfin_write_MDMA_S0_CONFIG(0); |
| 332 | bfin_write_MDMA_S1_CONFIG(0); |
| 333 | bfin_write_MDMA_D0_CONFIG(0); |
| 334 | bfin_write_MDMA_D1_CONFIG(0); |
| 335 | |
| 336 | __builtin_bfin_ssync(); |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 337 | } |
| 338 | |
| 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); |
Michael Hennerich | 23ee968 | 2007-05-21 18:09:17 +0800 | [diff] [blame] | 350 | unsigned long flags; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 351 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 352 | spin_lock_irqsave(&mdma_lock, flags); |
Mike Frysinger | 1f83b8f | 2007-07-12 22:58:21 +0800 | [diff] [blame] | 353 | |
Mike Frysinger | 41245ac | 2009-02-04 16:49:45 +0800 | [diff] [blame] | 354 | /* Force a sync in case a previous config reset on this channel |
| 355 | * occurred. This is needed so subsequent writes to DMA registers |
| 356 | * are not spuriously lost/corrupted. Do it under irq lock and |
| 357 | * without the anomaly version (because we are atomic already). |
| 358 | */ |
| 359 | __builtin_bfin_ssync(); |
| 360 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 361 | if (bfin_read_MDMA_S0_CONFIG()) |
| 362 | while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE)) |
| 363 | continue; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 364 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 365 | if (conf & DMA2D) { |
| 366 | /* For larger bit sizes, we've already divided down cnt so it |
| 367 | * is no longer a multiple of 64k. So we have to break down |
| 368 | * the limit here so it is a multiple of the incoming size. |
| 369 | * There is no limitation here in terms of total size other |
| 370 | * than the hardware though as the bits lost in the shift are |
| 371 | * made up by MODIFY (== we can hit the whole address space). |
| 372 | * X: (2^(16 - 0)) * 1 == (2^(16 - 1)) * 2 == (2^(16 - 2)) * 4 |
| 373 | */ |
| 374 | u32 shift = abs(dmod) >> 1; |
| 375 | size_t ycnt = cnt >> (16 - shift); |
| 376 | cnt = 1 << (16 - shift); |
| 377 | bfin_write_MDMA_D0_Y_COUNT(ycnt); |
| 378 | bfin_write_MDMA_S0_Y_COUNT(ycnt); |
| 379 | bfin_write_MDMA_D0_Y_MODIFY(dmod); |
| 380 | bfin_write_MDMA_S0_Y_MODIFY(smod); |
| 381 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 382 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 383 | bfin_write_MDMA_D0_START_ADDR(daddr); |
| 384 | bfin_write_MDMA_D0_X_COUNT(cnt); |
| 385 | bfin_write_MDMA_D0_X_MODIFY(dmod); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 386 | bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR); |
| 387 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 388 | bfin_write_MDMA_S0_START_ADDR(saddr); |
| 389 | bfin_write_MDMA_S0_X_COUNT(cnt); |
| 390 | bfin_write_MDMA_S0_X_MODIFY(smod); |
| 391 | bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 392 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 393 | bfin_write_MDMA_S0_CONFIG(DMAEN | conf); |
| 394 | bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | conf); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 395 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 396 | spin_unlock_irqrestore(&mdma_lock, flags); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 397 | |
Michael Hennerich | 1a7d91d | 2007-10-10 17:42:55 +0800 | [diff] [blame] | 398 | SSYNC(); |
| 399 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 400 | while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE)) |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 401 | if (bfin_read_MDMA_S0_CONFIG()) |
| 402 | continue; |
| 403 | else |
| 404 | return; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 405 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 406 | bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 407 | |
| 408 | bfin_write_MDMA_S0_CONFIG(0); |
| 409 | bfin_write_MDMA_D0_CONFIG(0); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 410 | } |
Aubrey Li | 5f9a3e8 | 2007-05-21 18:09:28 +0800 | [diff] [blame] | 411 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 412 | /** |
| 413 | * _dma_memcpy - translate C memcpy settings into MDMA settings |
| 414 | * |
| 415 | * Handle all the high level steps before we touch the MDMA registers. So |
Mike Frysinger | 7ad883a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 416 | * handle direction, tweaking of sizes, and formatting of addresses. |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 417 | */ |
| 418 | static void *_dma_memcpy(void *pdst, const void *psrc, size_t size) |
Aubrey Li | 5f9a3e8 | 2007-05-21 18:09:28 +0800 | [diff] [blame] | 419 | { |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 420 | u32 conf, shift; |
| 421 | s16 mod; |
| 422 | unsigned long dst = (unsigned long)pdst; |
| 423 | unsigned long src = (unsigned long)psrc; |
Aubrey Li | 5f9a3e8 | 2007-05-21 18:09:28 +0800 | [diff] [blame] | 424 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 425 | if (size == 0) |
| 426 | return NULL; |
| 427 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 428 | if (dst % 4 == 0 && src % 4 == 0 && size % 4 == 0) { |
| 429 | conf = WDSIZE_32; |
| 430 | shift = 2; |
| 431 | } else if (dst % 2 == 0 && src % 2 == 0 && size % 2 == 0) { |
| 432 | conf = WDSIZE_16; |
| 433 | shift = 1; |
| 434 | } else { |
| 435 | conf = WDSIZE_8; |
| 436 | shift = 0; |
| 437 | } |
| 438 | |
| 439 | /* If the two memory regions have a chance of overlapping, make |
| 440 | * sure the memcpy still works as expected. Do this by having the |
| 441 | * copy run backwards instead. |
| 442 | */ |
| 443 | mod = 1 << shift; |
| 444 | if (src < dst) { |
| 445 | mod *= -1; |
| 446 | dst += size + mod; |
| 447 | src += size + mod; |
| 448 | } |
| 449 | size >>= shift; |
| 450 | |
| 451 | if (size > 0x10000) |
| 452 | conf |= DMA2D; |
| 453 | |
| 454 | __dma_memcpy(dst, mod, src, mod, size, conf); |
| 455 | |
| 456 | return pdst; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * dma_memcpy - DMA memcpy under mutex lock |
| 461 | * |
| 462 | * Do not check arguments before starting the DMA memcpy. Break the transfer |
| 463 | * up into two pieces. The first transfer is in multiples of 64k and the |
| 464 | * second transfer is the piece smaller than 64k. |
| 465 | */ |
Mike Frysinger | 7ad883a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 466 | void *dma_memcpy(void *pdst, const void *psrc, size_t size) |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 467 | { |
Mike Frysinger | 7ad883a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 468 | unsigned long dst = (unsigned long)pdst; |
| 469 | unsigned long src = (unsigned long)psrc; |
Mike Frysinger | 7ad883a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 470 | |
Jie Zhang | 67834fa | 2009-06-10 06:26:26 +0000 | [diff] [blame] | 471 | if (bfin_addr_dcacheable(src)) |
Mike Frysinger | 7ad883a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 472 | blackfin_dcache_flush_range(src, src + size); |
| 473 | |
Jie Zhang | 67834fa | 2009-06-10 06:26:26 +0000 | [diff] [blame] | 474 | if (bfin_addr_dcacheable(dst)) |
Mike Frysinger | 7ad883a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 475 | blackfin_dcache_invalidate_range(dst, dst + size); |
| 476 | |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 477 | return dma_memcpy_nocache(pdst, psrc, size); |
| 478 | } |
| 479 | EXPORT_SYMBOL(dma_memcpy); |
| 480 | |
| 481 | /** |
| 482 | * dma_memcpy_nocache - DMA memcpy under mutex lock |
| 483 | * - No cache flush/invalidate |
| 484 | * |
| 485 | * Do not check arguments before starting the DMA memcpy. Break the transfer |
| 486 | * up into two pieces. The first transfer is in multiples of 64k and the |
| 487 | * second transfer is the piece smaller than 64k. |
| 488 | */ |
| 489 | void *dma_memcpy_nocache(void *pdst, const void *psrc, size_t size) |
| 490 | { |
| 491 | size_t bulk, rest; |
| 492 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 493 | bulk = size & ~0xffff; |
Aubrey Li | 5f9a3e8 | 2007-05-21 18:09:28 +0800 | [diff] [blame] | 494 | rest = size - bulk; |
| 495 | if (bulk) |
Mike Frysinger | 7ad883a | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 496 | _dma_memcpy(pdst, psrc, bulk); |
| 497 | _dma_memcpy(pdst + bulk, psrc + bulk, rest); |
| 498 | return pdst; |
Aubrey Li | 5f9a3e8 | 2007-05-21 18:09:28 +0800 | [diff] [blame] | 499 | } |
Michael Hennerich | d1401e1 | 2010-06-16 09:12:10 +0000 | [diff] [blame] | 500 | EXPORT_SYMBOL(dma_memcpy_nocache); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 501 | |
Mike Frysinger | 49946e7 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 502 | /** |
| 503 | * safe_dma_memcpy - DMA memcpy w/argument checking |
| 504 | * |
| 505 | * Verify arguments are safe before heading to dma_memcpy(). |
| 506 | */ |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 507 | void *safe_dma_memcpy(void *dst, const void *src, size_t size) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 508 | { |
Mike Frysinger | 49946e7 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 509 | if (!access_ok(VERIFY_WRITE, dst, size)) |
| 510 | return NULL; |
| 511 | if (!access_ok(VERIFY_READ, src, size)) |
| 512 | return NULL; |
| 513 | return dma_memcpy(dst, src, size); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 514 | } |
| 515 | EXPORT_SYMBOL(safe_dma_memcpy); |
Michael Hennerich | 23ee968 | 2007-05-21 18:09:17 +0800 | [diff] [blame] | 516 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 517 | static void _dma_out(unsigned long addr, unsigned long buf, unsigned short len, |
| 518 | u16 size, u16 dma_size) |
Michael Hennerich | 23ee968 | 2007-05-21 18:09:17 +0800 | [diff] [blame] | 519 | { |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 520 | blackfin_dcache_flush_range(buf, buf + len * size); |
| 521 | __dma_memcpy(addr, 0, buf, size, len, dma_size); |
Michael Hennerich | 23ee968 | 2007-05-21 18:09:17 +0800 | [diff] [blame] | 522 | } |
Michael Hennerich | 23ee968 | 2007-05-21 18:09:17 +0800 | [diff] [blame] | 523 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 524 | static void _dma_in(unsigned long addr, unsigned long buf, unsigned short len, |
| 525 | u16 size, u16 dma_size) |
Michael Hennerich | 23ee968 | 2007-05-21 18:09:17 +0800 | [diff] [blame] | 526 | { |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 527 | blackfin_dcache_invalidate_range(buf, buf + len * size); |
| 528 | __dma_memcpy(buf, size, addr, 0, len, dma_size); |
Michael Hennerich | 23ee968 | 2007-05-21 18:09:17 +0800 | [diff] [blame] | 529 | } |
Michael Hennerich | 23ee968 | 2007-05-21 18:09:17 +0800 | [diff] [blame] | 530 | |
Mike Frysinger | dd3dd38 | 2009-01-07 23:14:39 +0800 | [diff] [blame] | 531 | #define MAKE_DMA_IO(io, bwl, isize, dmasize, cnst) \ |
| 532 | void dma_##io##s##bwl(unsigned long addr, cnst void *buf, unsigned short len) \ |
| 533 | { \ |
| 534 | _dma_##io(addr, (unsigned long)buf, len, isize, WDSIZE_##dmasize); \ |
| 535 | } \ |
| 536 | EXPORT_SYMBOL(dma_##io##s##bwl) |
| 537 | MAKE_DMA_IO(out, b, 1, 8, const); |
| 538 | MAKE_DMA_IO(in, b, 1, 8, ); |
| 539 | MAKE_DMA_IO(out, w, 2, 16, const); |
| 540 | MAKE_DMA_IO(in, w, 2, 16, ); |
| 541 | MAKE_DMA_IO(out, l, 4, 32, const); |
| 542 | MAKE_DMA_IO(in, l, 4, 32, ); |