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> |
Russell King | e193ba2 | 2009-12-24 18:32:13 +0000 | [diff] [blame] | 19 | #include <linux/seq_file.h> |
| 20 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/dma.h> |
| 23 | |
| 24 | #include <asm/mach/dma.h> |
| 25 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 26 | DEFINE_RAW_SPINLOCK(dma_spin_lock); |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 27 | EXPORT_SYMBOL(dma_spin_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Russell King | 2f757f2 | 2008-12-08 16:33:30 +0000 | [diff] [blame] | 29 | static dma_t *dma_chan[MAX_DMA_CHANNELS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 31 | static inline dma_t *dma_channel(unsigned int chan) |
| 32 | { |
Russell King | 2f757f2 | 2008-12-08 16:33:30 +0000 | [diff] [blame] | 33 | if (chan >= MAX_DMA_CHANNELS) |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 34 | return NULL; |
| 35 | |
Russell King | 2f757f2 | 2008-12-08 16:33:30 +0000 | [diff] [blame] | 36 | return dma_chan[chan]; |
| 37 | } |
| 38 | |
| 39 | int __init isa_dma_add(unsigned int chan, dma_t *dma) |
| 40 | { |
| 41 | if (!dma->d_ops) |
| 42 | return -EINVAL; |
Russell King | d667522 | 2008-12-08 17:50:25 +0000 | [diff] [blame] | 43 | |
| 44 | sg_init_table(&dma->buf, 1); |
| 45 | |
Russell King | 2f757f2 | 2008-12-08 16:33:30 +0000 | [diff] [blame] | 46 | if (dma_chan[chan]) |
| 47 | return -EBUSY; |
| 48 | dma_chan[chan] = dma; |
| 49 | return 0; |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | * Request DMA channel |
| 54 | * |
| 55 | * On certain platforms, we have to allocate an interrupt as well... |
| 56 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 57 | int request_dma(unsigned int chan, const char *device_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 59 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | int ret; |
| 61 | |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 62 | if (!dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | goto bad_dma; |
| 64 | |
| 65 | if (xchg(&dma->lock, 1) != 0) |
| 66 | goto busy; |
| 67 | |
| 68 | dma->device_id = device_id; |
| 69 | dma->active = 0; |
| 70 | dma->invalid = 1; |
| 71 | |
| 72 | ret = 0; |
| 73 | if (dma->d_ops->request) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 74 | ret = dma->d_ops->request(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | if (ret) |
| 77 | xchg(&dma->lock, 0); |
| 78 | |
| 79 | return ret; |
| 80 | |
| 81 | bad_dma: |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 82 | pr_err("dma: trying to allocate DMA%d\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return -EINVAL; |
| 84 | |
| 85 | busy: |
| 86 | return -EBUSY; |
| 87 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 88 | EXPORT_SYMBOL(request_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Free DMA channel |
| 92 | * |
| 93 | * On certain platforms, we have to free interrupt as well... |
| 94 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 95 | void free_dma(unsigned int 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 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 99 | if (!dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | goto bad_dma; |
| 101 | |
| 102 | if (dma->active) { |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 103 | pr_err("dma%d: freeing active DMA\n", chan); |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 104 | dma->d_ops->disable(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | dma->active = 0; |
| 106 | } |
| 107 | |
| 108 | if (xchg(&dma->lock, 0) != 0) { |
| 109 | if (dma->d_ops->free) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 110 | dma->d_ops->free(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 114 | pr_err("dma%d: trying to free free DMA\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | return; |
| 116 | |
| 117 | bad_dma: |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 118 | pr_err("dma: trying to free DMA%d\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 120 | EXPORT_SYMBOL(free_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* Set DMA Scatter-Gather list |
| 123 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 124 | 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] | 125 | { |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 126 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | if (dma->active) |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 129 | pr_err("dma%d: altering DMA SG while DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | dma->sg = sg; |
| 132 | dma->sgcount = nr_sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | dma->invalid = 1; |
| 134 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 135 | EXPORT_SYMBOL(set_dma_sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | /* Set DMA address |
| 138 | * |
| 139 | * Copy address to the structure, and set the invalid bit |
| 140 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 141 | void __set_dma_addr (unsigned int chan, void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 143 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | if (dma->active) |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 146 | pr_err("dma%d: altering DMA address while 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) |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 163 | pr_err("dma%d: altering DMA count while DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Russell King | 7cdad48 | 2006-01-04 15:08:30 +0000 | [diff] [blame] | 165 | dma->sg = NULL; |
| 166 | dma->count = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | dma->invalid = 1; |
| 168 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 169 | EXPORT_SYMBOL(set_dma_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | /* Set DMA direction mode |
| 172 | */ |
Russell King | f0ffc81 | 2009-01-02 12:34:55 +0000 | [diff] [blame] | 173 | void set_dma_mode (unsigned int chan, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 175 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | if (dma->active) |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 178 | pr_err("dma%d: altering DMA mode while DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | dma->dma_mode = mode; |
| 181 | dma->invalid = 1; |
| 182 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 183 | EXPORT_SYMBOL(set_dma_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | /* Enable DMA channel |
| 186 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 187 | void enable_dma (unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 189 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | if (!dma->lock) |
| 192 | goto free_dma; |
| 193 | |
| 194 | if (dma->active == 0) { |
| 195 | dma->active = 1; |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 196 | dma->d_ops->enable(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | return; |
| 199 | |
| 200 | free_dma: |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 201 | pr_err("dma%d: trying to enable free DMA\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | BUG(); |
| 203 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 204 | EXPORT_SYMBOL(enable_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | /* Disable DMA channel |
| 207 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 208 | void disable_dma (unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 210 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | if (!dma->lock) |
| 213 | goto free_dma; |
| 214 | |
| 215 | if (dma->active == 1) { |
| 216 | dma->active = 0; |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 217 | dma->d_ops->disable(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | return; |
| 220 | |
| 221 | free_dma: |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 222 | pr_err("dma%d: trying to disable free DMA\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | BUG(); |
| 224 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 225 | EXPORT_SYMBOL(disable_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | /* |
| 228 | * Is the specified DMA channel active? |
| 229 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 230 | int dma_channel_active(unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 232 | dma_t *dma = dma_channel(chan); |
| 233 | return dma->active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
Russell King | ec14d79 | 2007-03-31 21:36:53 +0100 | [diff] [blame] | 235 | EXPORT_SYMBOL(dma_channel_active); |
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 | void set_dma_page(unsigned int chan, char pagenr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 239 | pr_err("dma%d: trying to set_dma_page\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 241 | EXPORT_SYMBOL(set_dma_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 243 | void set_dma_speed(unsigned int chan, int cycle_ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 245 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | int ret = 0; |
| 247 | |
| 248 | if (dma->d_ops->setspeed) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 249 | ret = dma->d_ops->setspeed(chan, dma, cycle_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | dma->speed = ret; |
| 251 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 252 | EXPORT_SYMBOL(set_dma_speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 254 | int get_dma_residue(unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Russell King | 3afb6e9 | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 256 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | int ret = 0; |
| 258 | |
| 259 | if (dma->d_ops->residue) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 260 | ret = dma->d_ops->residue(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | return ret; |
| 263 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 264 | EXPORT_SYMBOL(get_dma_residue); |
Russell King | e193ba2 | 2009-12-24 18:32:13 +0000 | [diff] [blame] | 265 | |
| 266 | #ifdef CONFIG_PROC_FS |
| 267 | static int proc_dma_show(struct seq_file *m, void *v) |
| 268 | { |
| 269 | int i; |
| 270 | |
| 271 | for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) { |
| 272 | dma_t *dma = dma_channel(i); |
| 273 | if (dma && dma->lock) |
| 274 | seq_printf(m, "%2d: %s\n", i, dma->device_id); |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int proc_dma_open(struct inode *inode, struct file *file) |
| 280 | { |
| 281 | return single_open(file, proc_dma_show, NULL); |
| 282 | } |
| 283 | |
| 284 | static const struct file_operations proc_dma_operations = { |
| 285 | .open = proc_dma_open, |
| 286 | .read = seq_read, |
| 287 | .llseek = seq_lseek, |
| 288 | .release = single_release, |
| 289 | }; |
| 290 | |
| 291 | static int __init proc_dma_init(void) |
| 292 | { |
| 293 | proc_create("dma", 0, NULL, &proc_dma_operations); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | __initcall(proc_dma_init); |
| 298 | #endif |