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