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