Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/arch/arm/kernel/dma.c |
| 4 | * |
| 5 | * Copyright (C) 1995-2000 Russell King |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Front-end to the DMA handling. This handles the allocation/freeing |
| 8 | * of DMA channels, and provides a unified interface to the machines |
| 9 | * DMA facilities. |
| 10 | */ |
| 11 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/errno.h> |
Russell King | d667522 | 2008-12-08 17:50:25 +0000 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
Russell King | e193ba2 | 2009-12-24 18:32:13 +0000 | [diff] [blame] | 16 | #include <linux/seq_file.h> |
| 17 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/dma.h> |
| 20 | |
| 21 | #include <asm/mach/dma.h> |
| 22 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 23 | DEFINE_RAW_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 | 3afb6e9c | 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 | 3afb6e9c | 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; |
Russell King | d667522 | 2008-12-08 17:50:25 +0000 | [diff] [blame] | 40 | |
| 41 | sg_init_table(&dma->buf, 1); |
| 42 | |
Russell King | 2f757f2 | 2008-12-08 16:33:30 +0000 | [diff] [blame] | 43 | if (dma_chan[chan]) |
| 44 | return -EBUSY; |
| 45 | dma_chan[chan] = dma; |
| 46 | return 0; |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * Request DMA channel |
| 51 | * |
| 52 | * On certain platforms, we have to allocate an interrupt as well... |
| 53 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 54 | int request_dma(unsigned int chan, const char *device_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 56 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | int ret; |
| 58 | |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 59 | if (!dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | goto bad_dma; |
| 61 | |
| 62 | if (xchg(&dma->lock, 1) != 0) |
| 63 | goto busy; |
| 64 | |
| 65 | dma->device_id = device_id; |
| 66 | dma->active = 0; |
| 67 | dma->invalid = 1; |
| 68 | |
| 69 | ret = 0; |
| 70 | if (dma->d_ops->request) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 71 | ret = dma->d_ops->request(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | if (ret) |
| 74 | xchg(&dma->lock, 0); |
| 75 | |
| 76 | return ret; |
| 77 | |
| 78 | bad_dma: |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 79 | pr_err("dma: trying to allocate DMA%d\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return -EINVAL; |
| 81 | |
| 82 | busy: |
| 83 | return -EBUSY; |
| 84 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 85 | EXPORT_SYMBOL(request_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Free DMA channel |
| 89 | * |
| 90 | * On certain platforms, we have to free interrupt as well... |
| 91 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 92 | void free_dma(unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 94 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 96 | if (!dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | goto bad_dma; |
| 98 | |
| 99 | if (dma->active) { |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 100 | pr_err("dma%d: freeing active DMA\n", chan); |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 101 | dma->d_ops->disable(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | dma->active = 0; |
| 103 | } |
| 104 | |
| 105 | if (xchg(&dma->lock, 0) != 0) { |
| 106 | if (dma->d_ops->free) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 107 | dma->d_ops->free(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 111 | pr_err("dma%d: trying to free free DMA\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | return; |
| 113 | |
| 114 | bad_dma: |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 115 | pr_err("dma: trying to free DMA%d\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 117 | EXPORT_SYMBOL(free_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | /* Set DMA Scatter-Gather list |
| 120 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 121 | 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] | 122 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 123 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | if (dma->active) |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 126 | pr_err("dma%d: altering DMA SG while DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | dma->sg = sg; |
| 129 | dma->sgcount = nr_sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | dma->invalid = 1; |
| 131 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 132 | EXPORT_SYMBOL(set_dma_sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | /* Set DMA address |
| 135 | * |
| 136 | * Copy address to the structure, and set the invalid bit |
| 137 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 138 | void __set_dma_addr (unsigned int chan, void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 140 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | if (dma->active) |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 143 | pr_err("dma%d: altering DMA address while DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Russell King | 7cdad48 | 2006-01-04 15:08:30 +0000 | [diff] [blame] | 145 | dma->sg = NULL; |
| 146 | dma->addr = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | dma->invalid = 1; |
| 148 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 149 | EXPORT_SYMBOL(__set_dma_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | /* Set DMA byte count |
| 152 | * |
| 153 | * Copy address to the structure, and set the invalid bit |
| 154 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 155 | void set_dma_count (unsigned int chan, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 157 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | if (dma->active) |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 160 | pr_err("dma%d: altering DMA count while 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 | f0ffc81 | 2009-01-02 12:34:55 +0000 | [diff] [blame] | 170 | void set_dma_mode (unsigned int chan, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Russell King | 3afb6e9c | 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) |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 175 | pr_err("dma%d: altering DMA mode while DMA active\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | dma->dma_mode = mode; |
| 178 | dma->invalid = 1; |
| 179 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 180 | EXPORT_SYMBOL(set_dma_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | /* Enable DMA channel |
| 183 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 184 | void enable_dma (unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 186 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | if (!dma->lock) |
| 189 | goto free_dma; |
| 190 | |
| 191 | if (dma->active == 0) { |
| 192 | dma->active = 1; |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 193 | dma->d_ops->enable(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | return; |
| 196 | |
| 197 | free_dma: |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 198 | pr_err("dma%d: trying to enable free DMA\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | BUG(); |
| 200 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 201 | EXPORT_SYMBOL(enable_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | /* Disable DMA channel |
| 204 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 205 | void disable_dma (unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 207 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | if (!dma->lock) |
| 210 | goto free_dma; |
| 211 | |
| 212 | if (dma->active == 1) { |
| 213 | dma->active = 0; |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 214 | dma->d_ops->disable(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | return; |
| 217 | |
| 218 | free_dma: |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 219 | pr_err("dma%d: trying to disable free DMA\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | BUG(); |
| 221 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 222 | EXPORT_SYMBOL(disable_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * Is the specified DMA channel active? |
| 226 | */ |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 227 | int dma_channel_active(unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 229 | dma_t *dma = dma_channel(chan); |
| 230 | return dma->active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
Russell King | ec14d79 | 2007-03-31 21:36:53 +0100 | [diff] [blame] | 232 | EXPORT_SYMBOL(dma_channel_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 234 | void set_dma_page(unsigned int chan, char pagenr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 236 | pr_err("dma%d: trying to set_dma_page\n", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 238 | EXPORT_SYMBOL(set_dma_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 240 | void set_dma_speed(unsigned int chan, int cycle_ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 242 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | int ret = 0; |
| 244 | |
| 245 | if (dma->d_ops->setspeed) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 246 | ret = dma->d_ops->setspeed(chan, dma, cycle_ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | dma->speed = ret; |
| 248 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 249 | EXPORT_SYMBOL(set_dma_speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 251 | int get_dma_residue(unsigned int chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Russell King | 3afb6e9c | 2008-12-08 16:08:48 +0000 | [diff] [blame] | 253 | dma_t *dma = dma_channel(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | int ret = 0; |
| 255 | |
| 256 | if (dma->d_ops->residue) |
Russell King | 1df8130 | 2008-12-08 15:58:50 +0000 | [diff] [blame] | 257 | ret = dma->d_ops->residue(chan, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | return ret; |
| 260 | } |
Russell King | d7b4a75 | 2006-01-04 15:52:45 +0000 | [diff] [blame] | 261 | EXPORT_SYMBOL(get_dma_residue); |
Russell King | e193ba2 | 2009-12-24 18:32:13 +0000 | [diff] [blame] | 262 | |
| 263 | #ifdef CONFIG_PROC_FS |
| 264 | static int proc_dma_show(struct seq_file *m, void *v) |
| 265 | { |
| 266 | int i; |
| 267 | |
| 268 | for (i = 0 ; i < MAX_DMA_CHANNELS ; i++) { |
| 269 | dma_t *dma = dma_channel(i); |
| 270 | if (dma && dma->lock) |
| 271 | seq_printf(m, "%2d: %s\n", i, dma->device_id); |
| 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 | |
Russell King | e193ba2 | 2009-12-24 18:32:13 +0000 | [diff] [blame] | 276 | static int __init proc_dma_init(void) |
| 277 | { |
Christoph Hellwig | 3f3942a | 2018-05-15 15:57:23 +0200 | [diff] [blame] | 278 | proc_create_single("dma", 0, NULL, proc_dma_show); |
Russell King | e193ba2 | 2009-12-24 18:32:13 +0000 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | __initcall(proc_dma_init); |
| 283 | #endif |