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