Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/kernel/dma.c |
| 3 | * |
| 4 | * Copyright (C) 1995-2000 Russell King |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * Front-end to the DMA handling. This handles the allocation/freeing |
| 11 | * of DMA channels, and provides a unified interface to the machines |
| 12 | * DMA facilities. |
| 13 | */ |
| 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/errno.h> |
| 18 | |
| 19 | #include <asm/dma.h> |
| 20 | |
| 21 | #include <asm/mach/dma.h> |
| 22 | |
| 23 | DEFINE_SPINLOCK(dma_spin_lock); |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 24 | EXPORT_SYMBOL(dma_spin_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static dma_t dma_chan[MAX_DMA_CHANNELS]; |
| 27 | |
| 28 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | * Request DMA channel |
| 30 | * |
| 31 | * On certain platforms, we have to allocate an interrupt as well... |
| 32 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 33 | int request_dma(unsigned int chan, const char *device_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 35 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | int ret; |
| 37 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 38 | if (chan >= MAX_DMA_CHANNELS || !dma->d_ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | goto bad_dma; |
| 40 | |
| 41 | if (xchg(&dma->lock, 1) != 0) |
| 42 | goto busy; |
| 43 | |
| 44 | dma->device_id = device_id; |
| 45 | dma->active = 0; |
| 46 | dma->invalid = 1; |
| 47 | |
| 48 | ret = 0; |
| 49 | if (dma->d_ops->request) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 50 | ret = dma->d_ops->request(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | if (ret) |
| 53 | xchg(&dma->lock, 0); |
| 54 | |
| 55 | return ret; |
| 56 | |
| 57 | bad_dma: |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 58 | printk(KERN_ERR "dma: trying to allocate DMA%d\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return -EINVAL; |
| 60 | |
| 61 | busy: |
| 62 | return -EBUSY; |
| 63 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 64 | EXPORT_SYMBOL(request_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Free DMA channel |
| 68 | * |
| 69 | * On certain platforms, we have to free interrupt as well... |
| 70 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 71 | void free_dma(unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 73 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 75 | if (chan >= MAX_DMA_CHANNELS || !dma->d_ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | goto bad_dma; |
| 77 | |
| 78 | if (dma->active) { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 79 | printk(KERN_ERR "dma%d: freeing active DMA\n", chan); |
| 80 | dma->d_ops->disable(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | dma->active = 0; |
| 82 | } |
| 83 | |
| 84 | if (xchg(&dma->lock, 0) != 0) { |
| 85 | if (dma->d_ops->free) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 86 | dma->d_ops->free(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 90 | printk(KERN_ERR "dma%d: trying to free free DMA\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return; |
| 92 | |
| 93 | bad_dma: |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 94 | printk(KERN_ERR "dma: trying to free DMA%d\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 96 | EXPORT_SYMBOL(free_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | /* Set DMA Scatter-Gather list |
| 99 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 100 | void set_dma_sg (unsigned int chan, struct scatterlist *sg, int nr_sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 102 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | if (dma->active) |
| 105 | printk(KERN_ERR "dma%d: altering DMA SG while " |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 106 | "DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | dma->sg = sg; |
| 109 | dma->sgcount = nr_sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | dma->invalid = 1; |
| 111 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 112 | EXPORT_SYMBOL(set_dma_sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /* Set DMA address |
| 115 | * |
| 116 | * Copy address to the structure, and set the invalid bit |
| 117 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 118 | void __set_dma_addr (unsigned int chan, void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 120 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | if (dma->active) |
| 123 | printk(KERN_ERR "dma%d: altering DMA address while " |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 124 | "DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Russell King | 7cdad48 | 2006-01-04 15:08:30 +0000 | [diff] [blame] | 126 | dma->sg = NULL; |
| 127 | dma->addr = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | dma->invalid = 1; |
| 129 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 130 | EXPORT_SYMBOL(__set_dma_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* Set DMA byte count |
| 133 | * |
| 134 | * Copy address to the structure, and set the invalid bit |
| 135 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 136 | void set_dma_count (unsigned int chan, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 138 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | if (dma->active) |
| 141 | printk(KERN_ERR "dma%d: altering DMA count while " |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 142 | "DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Russell King | 7cdad48 | 2006-01-04 15:08:30 +0000 | [diff] [blame] | 144 | dma->sg = NULL; |
| 145 | dma->count = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | dma->invalid = 1; |
| 147 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 148 | EXPORT_SYMBOL(set_dma_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | /* Set DMA direction mode |
| 151 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 152 | void set_dma_mode (unsigned int chan, dmamode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 154 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | if (dma->active) |
| 157 | printk(KERN_ERR "dma%d: altering DMA mode while " |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 158 | "DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | dma->dma_mode = mode; |
| 161 | dma->invalid = 1; |
| 162 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 163 | EXPORT_SYMBOL(set_dma_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* Enable DMA channel |
| 166 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 167 | void enable_dma (unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 169 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | if (!dma->lock) |
| 172 | goto free_dma; |
| 173 | |
| 174 | if (dma->active == 0) { |
| 175 | dma->active = 1; |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 176 | dma->d_ops->enable(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | return; |
| 179 | |
| 180 | free_dma: |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 181 | printk(KERN_ERR "dma%d: trying to enable free DMA\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | BUG(); |
| 183 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 184 | EXPORT_SYMBOL(enable_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | /* Disable DMA channel |
| 187 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 188 | void disable_dma (unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 190 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | if (!dma->lock) |
| 193 | goto free_dma; |
| 194 | |
| 195 | if (dma->active == 1) { |
| 196 | dma->active = 0; |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 197 | dma->d_ops->disable(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | return; |
| 200 | |
| 201 | free_dma: |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 202 | printk(KERN_ERR "dma%d: trying to disable free DMA\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | BUG(); |
| 204 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 205 | EXPORT_SYMBOL(disable_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | /* |
| 208 | * Is the specified DMA channel active? |
| 209 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 210 | int dma_channel_active(unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 212 | return dma_chan[chan].active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
Russell King | ec14d79 | 2007-03-31 21:36:53 +0100 | [diff] [blame] | 214 | EXPORT_SYMBOL(dma_channel_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 216 | void set_dma_page(unsigned int chan, char pagenr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 218 | printk(KERN_ERR "dma%d: trying to set_dma_page\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 220 | EXPORT_SYMBOL(set_dma_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 222 | void set_dma_speed(unsigned int chan, int cycle_ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 224 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | int ret = 0; |
| 226 | |
| 227 | if (dma->d_ops->setspeed) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 228 | ret = dma->d_ops->setspeed(chan, dma, cycle_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | dma->speed = ret; |
| 230 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 231 | EXPORT_SYMBOL(set_dma_speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 233 | int get_dma_residue(unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 235 | dma_t *dma = dma_chan + chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | int ret = 0; |
| 237 | |
| 238 | if (dma->d_ops->residue) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame^] | 239 | ret = dma->d_ops->residue(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | return ret; |
| 242 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 243 | EXPORT_SYMBOL(get_dma_residue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Russell King | 6842b92 | 2006-01-04 15:17:08 +0000 | [diff] [blame] | 245 | static int __init init_dma(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
| 247 | arch_dma_init(dma_chan); |
Russell King | 6842b92 | 2006-01-04 15:17:08 +0000 | [diff] [blame] | 248 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Russell King | 6842b92 | 2006-01-04 15:17:08 +0000 | [diff] [blame] | 251 | core_initcall(init_dma); |