Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1 | /* |
| 2 | * MUSB OTG driver - support for Mentor's DMA controller |
| 3 | * |
| 4 | * Copyright 2005 Mentor Graphics Corporation |
| 5 | * Copyright (C) 2005-2007 by Texas Instruments |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | * 02110-1301 USA |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
| 24 | * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 26 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 27 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 28 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 30 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | #include <linux/device.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 37 | #include "musb_core.h" |
Bryan Wu | 6995eb6 | 2008-12-02 21:33:47 +0200 | [diff] [blame] | 38 | #include "musbhsdma.h" |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 39 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 40 | static void dma_channel_release(struct dma_channel *channel); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 41 | |
Sebastian Andrzej Siewior | 66c0188 | 2013-06-19 17:38:11 +0200 | [diff] [blame] | 42 | static void dma_controller_stop(struct musb_dma_controller *controller) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 43 | { |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 44 | struct musb *musb = controller->private_data; |
| 45 | struct dma_channel *channel; |
| 46 | u8 bit; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 47 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 48 | if (controller->used_channels != 0) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 49 | dev_err(musb->controller, |
| 50 | "Stopping DMA controller while channel active\n"); |
| 51 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 52 | for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) { |
| 53 | if (controller->used_channels & (1 << bit)) { |
| 54 | channel = &controller->channel[bit].channel; |
| 55 | dma_channel_release(channel); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 56 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 57 | if (!controller->used_channels) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 58 | break; |
| 59 | } |
| 60 | } |
| 61 | } |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static struct dma_channel *dma_channel_allocate(struct dma_controller *c, |
| 65 | struct musb_hw_ep *hw_ep, u8 transmit) |
| 66 | { |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 67 | struct musb_dma_controller *controller = container_of(c, |
| 68 | struct musb_dma_controller, controller); |
| 69 | struct musb_dma_channel *musb_channel = NULL; |
| 70 | struct dma_channel *channel = NULL; |
| 71 | u8 bit; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 72 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 73 | for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) { |
| 74 | if (!(controller->used_channels & (1 << bit))) { |
| 75 | controller->used_channels |= (1 << bit); |
| 76 | musb_channel = &(controller->channel[bit]); |
| 77 | musb_channel->controller = controller; |
| 78 | musb_channel->idx = bit; |
| 79 | musb_channel->epnum = hw_ep->epnum; |
| 80 | musb_channel->transmit = transmit; |
| 81 | channel = &(musb_channel->channel); |
| 82 | channel->private_data = musb_channel; |
| 83 | channel->status = MUSB_DMA_STATUS_FREE; |
Anil Shetty | 6587cc0 | 2010-09-24 13:44:05 +0300 | [diff] [blame] | 84 | channel->max_len = 0x100000; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 85 | /* Tx => mode 1; Rx => mode 0 */ |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 86 | channel->desired_mode = transmit; |
| 87 | channel->actual_len = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | } |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 91 | |
| 92 | return channel; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 93 | } |
| 94 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 95 | static void dma_channel_release(struct dma_channel *channel) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 96 | { |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 97 | struct musb_dma_channel *musb_channel = channel->private_data; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 98 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 99 | channel->actual_len = 0; |
| 100 | musb_channel->start_addr = 0; |
| 101 | musb_channel->len = 0; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 102 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 103 | musb_channel->controller->used_channels &= |
| 104 | ~(1 << musb_channel->idx); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 105 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 106 | channel->status = MUSB_DMA_STATUS_UNKNOWN; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 107 | } |
| 108 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 109 | static void configure_channel(struct dma_channel *channel, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 110 | u16 packet_sz, u8 mode, |
| 111 | dma_addr_t dma_addr, u32 len) |
| 112 | { |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 113 | struct musb_dma_channel *musb_channel = channel->private_data; |
| 114 | struct musb_dma_controller *controller = musb_channel->controller; |
Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 115 | struct musb *musb = controller->private_data; |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 116 | void __iomem *mbase = controller->base; |
| 117 | u8 bchannel = musb_channel->idx; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 118 | u16 csr = 0; |
| 119 | |
Bin Liu | b99d365 | 2016-06-30 12:12:22 -0500 | [diff] [blame] | 120 | musb_dbg(musb, "%p, pkt_sz %d, addr %pad, len %d, mode %d", |
Arnd Bergmann | 3ec08dd | 2016-01-28 17:23:14 +0100 | [diff] [blame] | 121 | channel, packet_sz, &dma_addr, len, mode); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 122 | |
| 123 | if (mode) { |
| 124 | csr |= 1 << MUSB_HSDMA_MODE1_SHIFT; |
| 125 | BUG_ON(len < packet_sz); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 126 | } |
Hema HK | c0f1f8e | 2010-06-24 23:07:09 +0530 | [diff] [blame] | 127 | csr |= MUSB_HSDMA_BURSTMODE_INCR16 |
| 128 | << MUSB_HSDMA_BURSTMODE_SHIFT; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 129 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 130 | csr |= (musb_channel->epnum << MUSB_HSDMA_ENDPOINT_SHIFT) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 131 | | (1 << MUSB_HSDMA_ENABLE_SHIFT) |
| 132 | | (1 << MUSB_HSDMA_IRQENABLE_SHIFT) |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 133 | | (musb_channel->transmit |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 134 | ? (1 << MUSB_HSDMA_TRANSMIT_SHIFT) |
| 135 | : 0); |
| 136 | |
| 137 | /* address/count */ |
Bryan Wu | 6995eb6 | 2008-12-02 21:33:47 +0200 | [diff] [blame] | 138 | musb_write_hsdma_addr(mbase, bchannel, dma_addr); |
| 139 | musb_write_hsdma_count(mbase, bchannel, len); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 140 | |
| 141 | /* control (this should start things) */ |
| 142 | musb_writew(mbase, |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 143 | MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL), |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 144 | csr); |
| 145 | } |
| 146 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 147 | static int dma_channel_program(struct dma_channel *channel, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 148 | u16 packet_sz, u8 mode, |
| 149 | dma_addr_t dma_addr, u32 len) |
| 150 | { |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 151 | struct musb_dma_channel *musb_channel = channel->private_data; |
Anand Gadiyar | 6e16edf | 2010-11-08 00:20:30 -0600 | [diff] [blame] | 152 | struct musb_dma_controller *controller = musb_channel->controller; |
| 153 | struct musb *musb = controller->private_data; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 154 | |
Bin Liu | b99d365 | 2016-06-30 12:12:22 -0500 | [diff] [blame] | 155 | musb_dbg(musb, "ep%d-%s pkt_sz %d, dma_addr %pad length %d, mode %d", |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 156 | musb_channel->epnum, |
| 157 | musb_channel->transmit ? "Tx" : "Rx", |
Arnd Bergmann | 3ec08dd | 2016-01-28 17:23:14 +0100 | [diff] [blame] | 158 | packet_sz, &dma_addr, len, mode); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 159 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 160 | BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN || |
| 161 | channel->status == MUSB_DMA_STATUS_BUSY); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 162 | |
Mike Frysinger | 1325430 | 2011-03-30 22:48:54 -0400 | [diff] [blame] | 163 | /* Let targets check/tweak the arguments */ |
| 164 | if (musb->ops->adjust_channel_params) { |
| 165 | int ret = musb->ops->adjust_channel_params(channel, |
| 166 | packet_sz, &mode, &dma_addr, &len); |
| 167 | if (ret) |
| 168 | return ret; |
| 169 | } |
| 170 | |
Anand Gadiyar | 6e16edf | 2010-11-08 00:20:30 -0600 | [diff] [blame] | 171 | /* |
| 172 | * The DMA engine in RTL1.8 and above cannot handle |
| 173 | * DMA addresses that are not aligned to a 4 byte boundary. |
| 174 | * It ends up masking the last two bits of the address |
| 175 | * programmed in DMA_ADDR. |
| 176 | * |
| 177 | * Fail such DMA transfers, so that the backup PIO mode |
| 178 | * can carry out the transfer |
| 179 | */ |
| 180 | if ((musb->hwvers >= MUSB_HWVERS_1800) && (dma_addr % 4)) |
| 181 | return false; |
| 182 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 183 | channel->actual_len = 0; |
| 184 | musb_channel->start_addr = dma_addr; |
| 185 | musb_channel->len = len; |
| 186 | musb_channel->max_packet_sz = packet_sz; |
| 187 | channel->status = MUSB_DMA_STATUS_BUSY; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 188 | |
Anand Gadiyar | 8ca47c8 | 2010-07-08 16:34:55 +0530 | [diff] [blame] | 189 | configure_channel(channel, packet_sz, mode, dma_addr, len); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 194 | static int dma_channel_abort(struct dma_channel *channel) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 195 | { |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 196 | struct musb_dma_channel *musb_channel = channel->private_data; |
| 197 | void __iomem *mbase = musb_channel->controller->base; |
Tony Lindgren | d026e9c | 2014-11-24 11:05:03 -0800 | [diff] [blame] | 198 | struct musb *musb = musb_channel->controller->private_data; |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 199 | |
| 200 | u8 bchannel = musb_channel->idx; |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 201 | int offset; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 202 | u16 csr; |
| 203 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 204 | if (channel->status == MUSB_DMA_STATUS_BUSY) { |
| 205 | if (musb_channel->transmit) { |
Tony Lindgren | d026e9c | 2014-11-24 11:05:03 -0800 | [diff] [blame] | 206 | offset = musb->io.ep_offset(musb_channel->epnum, |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 207 | MUSB_TXCSR); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 208 | |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 209 | /* |
| 210 | * The programming guide says that we must clear |
| 211 | * the DMAENAB bit before the DMAMODE bit... |
| 212 | */ |
| 213 | csr = musb_readw(mbase, offset); |
| 214 | csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB); |
| 215 | musb_writew(mbase, offset, csr); |
| 216 | csr &= ~MUSB_TXCSR_DMAMODE; |
| 217 | musb_writew(mbase, offset, csr); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 218 | } else { |
Tony Lindgren | d026e9c | 2014-11-24 11:05:03 -0800 | [diff] [blame] | 219 | offset = musb->io.ep_offset(musb_channel->epnum, |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 220 | MUSB_RXCSR); |
| 221 | |
| 222 | csr = musb_readw(mbase, offset); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 223 | csr &= ~(MUSB_RXCSR_AUTOCLEAR | |
| 224 | MUSB_RXCSR_DMAENAB | |
| 225 | MUSB_RXCSR_DMAMODE); |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 226 | musb_writew(mbase, offset, csr); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | musb_writew(mbase, |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 230 | MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL), |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 231 | 0); |
Bryan Wu | 6995eb6 | 2008-12-02 21:33:47 +0200 | [diff] [blame] | 232 | musb_write_hsdma_addr(mbase, bchannel, 0); |
| 233 | musb_write_hsdma_count(mbase, bchannel, 0); |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 234 | channel->status = MUSB_DMA_STATUS_FREE; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 235 | } |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 236 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static irqreturn_t dma_controller_irq(int irq, void *private_data) |
| 241 | { |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 242 | struct musb_dma_controller *controller = private_data; |
| 243 | struct musb *musb = controller->private_data; |
| 244 | struct musb_dma_channel *musb_channel; |
| 245 | struct dma_channel *channel; |
| 246 | |
| 247 | void __iomem *mbase = controller->base; |
| 248 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 249 | irqreturn_t retval = IRQ_NONE; |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 250 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 251 | unsigned long flags; |
| 252 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 253 | u8 bchannel; |
| 254 | u8 int_hsdma; |
| 255 | |
Anand Gadiyar | f933a0c | 2009-12-28 13:40:36 +0200 | [diff] [blame] | 256 | u32 addr, count; |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 257 | u16 csr; |
| 258 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 259 | spin_lock_irqsave(&musb->lock, flags); |
| 260 | |
| 261 | int_hsdma = musb_readb(mbase, MUSB_HSDMA_INTR); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 262 | |
Cliff Cai | 6bd03e7 | 2009-11-16 16:19:26 +0530 | [diff] [blame] | 263 | #ifdef CONFIG_BLACKFIN |
| 264 | /* Clear DMA interrupt flags */ |
| 265 | musb_writeb(mbase, MUSB_HSDMA_INTR, int_hsdma); |
| 266 | #endif |
| 267 | |
Anand Gadiyar | f933a0c | 2009-12-28 13:40:36 +0200 | [diff] [blame] | 268 | if (!int_hsdma) { |
Bin Liu | b99d365 | 2016-06-30 12:12:22 -0500 | [diff] [blame] | 269 | musb_dbg(musb, "spurious DMA irq"); |
Anand Gadiyar | f933a0c | 2009-12-28 13:40:36 +0200 | [diff] [blame] | 270 | |
| 271 | for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) { |
| 272 | musb_channel = (struct musb_dma_channel *) |
| 273 | &(controller->channel[bchannel]); |
| 274 | channel = &musb_channel->channel; |
| 275 | if (channel->status == MUSB_DMA_STATUS_BUSY) { |
| 276 | count = musb_read_hsdma_count(mbase, bchannel); |
| 277 | |
| 278 | if (count == 0) |
| 279 | int_hsdma |= (1 << bchannel); |
| 280 | } |
| 281 | } |
| 282 | |
Bin Liu | b99d365 | 2016-06-30 12:12:22 -0500 | [diff] [blame] | 283 | musb_dbg(musb, "int_hsdma = 0x%x", int_hsdma); |
Anand Gadiyar | f933a0c | 2009-12-28 13:40:36 +0200 | [diff] [blame] | 284 | |
| 285 | if (!int_hsdma) |
| 286 | goto done; |
| 287 | } |
| 288 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 289 | for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) { |
| 290 | if (int_hsdma & (1 << bchannel)) { |
| 291 | musb_channel = (struct musb_dma_channel *) |
| 292 | &(controller->channel[bchannel]); |
| 293 | channel = &musb_channel->channel; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 294 | |
| 295 | csr = musb_readw(mbase, |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 296 | MUSB_HSDMA_CHANNEL_OFFSET(bchannel, |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 297 | MUSB_HSDMA_CONTROL)); |
| 298 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 299 | if (csr & (1 << MUSB_HSDMA_BUSERROR_SHIFT)) { |
| 300 | musb_channel->channel.status = |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 301 | MUSB_DMA_STATUS_BUS_ABORT; |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 302 | } else { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 303 | u8 devctl; |
| 304 | |
Bryan Wu | 6995eb6 | 2008-12-02 21:33:47 +0200 | [diff] [blame] | 305 | addr = musb_read_hsdma_addr(mbase, |
| 306 | bchannel); |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 307 | channel->actual_len = addr |
| 308 | - musb_channel->start_addr; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 309 | |
Bin Liu | b99d365 | 2016-06-30 12:12:22 -0500 | [diff] [blame] | 310 | musb_dbg(musb, "ch %p, 0x%x -> 0x%x (%zu / %d) %s", |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 311 | channel, musb_channel->start_addr, |
| 312 | addr, channel->actual_len, |
| 313 | musb_channel->len, |
| 314 | (channel->actual_len |
| 315 | < musb_channel->len) ? |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 316 | "=> reconfig 0" : "=> complete"); |
| 317 | |
| 318 | devctl = musb_readb(mbase, MUSB_DEVCTL); |
| 319 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 320 | channel->status = MUSB_DMA_STATUS_FREE; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 321 | |
| 322 | /* completed */ |
Paul Elder | 27d8c60 | 2019-01-30 08:13:21 -0600 | [diff] [blame] | 323 | if (musb_channel->transmit && |
| 324 | (!channel->desired_mode || |
| 325 | (channel->actual_len % |
| 326 | musb_channel->max_packet_sz))) { |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 327 | u8 epnum = musb_channel->epnum; |
Tony Lindgren | d026e9c | 2014-11-24 11:05:03 -0800 | [diff] [blame] | 328 | int offset = musb->io.ep_offset(epnum, |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 329 | MUSB_TXCSR); |
| 330 | u16 txcsr; |
| 331 | |
| 332 | /* |
| 333 | * The programming guide says that we |
| 334 | * must clear DMAENAB before DMAMODE. |
| 335 | */ |
| 336 | musb_ep_select(mbase, epnum); |
| 337 | txcsr = musb_readw(mbase, offset); |
Paul Elder | 27d8c60 | 2019-01-30 08:13:21 -0600 | [diff] [blame] | 338 | if (channel->desired_mode == 1) { |
| 339 | txcsr &= ~(MUSB_TXCSR_DMAENAB |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 340 | | MUSB_TXCSR_AUTOSET); |
Paul Elder | 27d8c60 | 2019-01-30 08:13:21 -0600 | [diff] [blame] | 341 | musb_writew(mbase, offset, txcsr); |
| 342 | /* Send out the packet */ |
| 343 | txcsr &= ~MUSB_TXCSR_DMAMODE; |
| 344 | txcsr |= MUSB_TXCSR_DMAENAB; |
| 345 | } |
Sergei Shtylyov | b6e434a | 2009-03-26 18:27:47 -0700 | [diff] [blame] | 346 | txcsr |= MUSB_TXCSR_TXPKTRDY; |
| 347 | musb_writew(mbase, offset, txcsr); |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 348 | } |
Sergei Shtylyov | c7bbc05 | 2009-03-26 18:26:40 -0700 | [diff] [blame] | 349 | musb_dma_completion(musb, musb_channel->epnum, |
| 350 | musb_channel->transmit); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | } |
Bryan Wu | 6995eb6 | 2008-12-02 21:33:47 +0200 | [diff] [blame] | 354 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 355 | retval = IRQ_HANDLED; |
| 356 | done: |
| 357 | spin_unlock_irqrestore(&musb->lock, flags); |
| 358 | return retval; |
| 359 | } |
| 360 | |
Tony Lindgren | 7f6283e | 2015-05-01 12:29:28 -0700 | [diff] [blame] | 361 | void musbhs_dma_controller_destroy(struct dma_controller *c) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 362 | { |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 363 | struct musb_dma_controller *controller = container_of(c, |
| 364 | struct musb_dma_controller, controller); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 365 | |
Sebastian Andrzej Siewior | 66c0188 | 2013-06-19 17:38:11 +0200 | [diff] [blame] | 366 | dma_controller_stop(controller); |
| 367 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 368 | if (controller->irq) |
| 369 | free_irq(controller->irq, c); |
| 370 | |
| 371 | kfree(controller); |
| 372 | } |
Tony Lindgren | 7f6283e | 2015-05-01 12:29:28 -0700 | [diff] [blame] | 373 | EXPORT_SYMBOL_GPL(musbhs_dma_controller_destroy); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 374 | |
Tony Lindgren | 7f6283e | 2015-05-01 12:29:28 -0700 | [diff] [blame] | 375 | struct dma_controller *musbhs_dma_controller_create(struct musb *musb, |
| 376 | void __iomem *base) |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 377 | { |
| 378 | struct musb_dma_controller *controller; |
| 379 | struct device *dev = musb->controller; |
| 380 | struct platform_device *pdev = to_platform_device(dev); |
Hema Kalliguddi | fcf173e | 2010-09-29 11:26:39 -0500 | [diff] [blame] | 381 | int irq = platform_get_irq_byname(pdev, "dma"); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 382 | |
Sergei Shtylyov | 7effdbd | 2012-08-20 22:34:46 +0400 | [diff] [blame] | 383 | if (irq <= 0) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 384 | dev_err(dev, "No DMA interrupt line!\n"); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 388 | controller = kzalloc(sizeof(*controller), GFP_KERNEL); |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 389 | if (!controller) |
| 390 | return NULL; |
| 391 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 392 | controller->channel_count = MUSB_HSDMA_CHANNELS; |
| 393 | controller->private_data = musb; |
| 394 | controller->base = base; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 395 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 396 | controller->controller.channel_alloc = dma_channel_allocate; |
| 397 | controller->controller.channel_release = dma_channel_release; |
| 398 | controller->controller.channel_program = dma_channel_program; |
| 399 | controller->controller.channel_abort = dma_channel_abort; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 400 | |
Yong Zhang | b5dd18d | 2011-09-07 16:10:52 +0800 | [diff] [blame] | 401 | if (request_irq(irq, dma_controller_irq, 0, |
Kay Sievers | 427c4f3 | 2008-11-07 01:52:53 +0100 | [diff] [blame] | 402 | dev_name(musb->controller), &controller->controller)) { |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 403 | dev_err(dev, "request_irq %d failed!\n", irq); |
Tony Lindgren | 7f6283e | 2015-05-01 12:29:28 -0700 | [diff] [blame] | 404 | musb_dma_controller_destroy(&controller->controller); |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 405 | |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | controller->irq = irq; |
| 410 | |
Felipe Balbi | 458e6a5 | 2008-09-11 11:53:24 +0300 | [diff] [blame] | 411 | return &controller->controller; |
Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 412 | } |
Tony Lindgren | 7f6283e | 2015-05-01 12:29:28 -0700 | [diff] [blame] | 413 | EXPORT_SYMBOL_GPL(musbhs_dma_controller_create); |