blob: 9616059b6a3dcd204479045157cdd17285708250 [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
2 * MUSB OTG driver peripheral support
3 *
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
Sergei Shtylyovcea83242009-11-18 22:51:18 +03007 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
Felipe Balbi550a7372008-07-24 12:27:36 +03008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
26 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36#include <linux/kernel.h>
37#include <linux/list.h>
38#include <linux/timer.h>
39#include <linux/module.h>
40#include <linux/smp.h>
41#include <linux/spinlock.h>
42#include <linux/delay.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030043#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030045
46#include "musb_core.h"
47
48
Felipe Balbi550a7372008-07-24 12:27:36 +030049/* ----------------------------------------------------------------------- */
50
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010051#define is_buffer_mapped(req) (is_dma_capable() && \
52 (req->map_state != UN_MAPPED))
53
Hema Kalliguddi92d27112010-11-15 04:24:01 -060054/* Maps the buffer to dma */
55
56static inline void map_dma_buffer(struct musb_request *request,
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010057 struct musb *musb, struct musb_ep *musb_ep)
Hema Kalliguddi92d27112010-11-15 04:24:01 -060058{
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +010059 int compatible = true;
60 struct dma_controller *dma = musb->dma_controller;
61
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010062 request->map_state = UN_MAPPED;
63
64 if (!is_dma_capable() || !musb_ep->dma)
65 return;
66
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +010067 /* Check if DMA engine can handle this request.
68 * DMA code must reject the USB request explicitly.
69 * Default behaviour is to map the request.
70 */
71 if (dma->is_compatible)
72 compatible = dma->is_compatible(musb_ep->dma,
73 musb_ep->packet_sz, request->request.buf,
74 request->request.length);
75 if (!compatible)
76 return;
77
Hema Kalliguddi92d27112010-11-15 04:24:01 -060078 if (request->request.dma == DMA_ADDR_INVALID) {
Sebastian Andrzej Siewior7b360f42013-08-13 19:35:43 +020079 dma_addr_t dma_addr;
80 int ret;
81
82 dma_addr = dma_map_single(
Hema Kalliguddi92d27112010-11-15 04:24:01 -060083 musb->controller,
84 request->request.buf,
85 request->request.length,
86 request->tx
87 ? DMA_TO_DEVICE
88 : DMA_FROM_DEVICE);
Sebastian Andrzej Siewior7b360f42013-08-13 19:35:43 +020089 ret = dma_mapping_error(musb->controller, dma_addr);
90 if (ret)
91 return;
92
93 request->request.dma = dma_addr;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010094 request->map_state = MUSB_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -060095 } else {
96 dma_sync_single_for_device(musb->controller,
97 request->request.dma,
98 request->request.length,
99 request->tx
100 ? DMA_TO_DEVICE
101 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100102 request->map_state = PRE_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600103 }
104}
105
106/* Unmap the buffer from dma and maps it back to cpu */
107static inline void unmap_dma_buffer(struct musb_request *request,
108 struct musb *musb)
109{
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +0530110 struct musb_ep *musb_ep = request->ep;
111
112 if (!is_buffer_mapped(request) || !musb_ep->dma)
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100113 return;
114
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600115 if (request->request.dma == DMA_ADDR_INVALID) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300116 dev_vdbg(musb->controller,
117 "not unmapping a never mapped buffer\n");
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600118 return;
119 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100120 if (request->map_state == MUSB_MAPPED) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600121 dma_unmap_single(musb->controller,
122 request->request.dma,
123 request->request.length,
124 request->tx
125 ? DMA_TO_DEVICE
126 : DMA_FROM_DEVICE);
127 request->request.dma = DMA_ADDR_INVALID;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100128 } else { /* PRE_MAPPED */
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600129 dma_sync_single_for_cpu(musb->controller,
130 request->request.dma,
131 request->request.length,
132 request->tx
133 ? DMA_TO_DEVICE
134 : DMA_FROM_DEVICE);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600135 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100136 request->map_state = UN_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600137}
138
Felipe Balbi550a7372008-07-24 12:27:36 +0300139/*
140 * Immediately complete a request.
141 *
142 * @param request the request to complete
143 * @param status the status to complete the request with
144 * Context: controller locked, IRQs blocked.
145 */
146void musb_g_giveback(
147 struct musb_ep *ep,
148 struct usb_request *request,
149 int status)
150__releases(ep->musb->lock)
151__acquires(ep->musb->lock)
152{
153 struct musb_request *req;
154 struct musb *musb;
155 int busy = ep->busy;
156
157 req = to_musb_request(request);
158
Felipe Balbiad1adb82011-02-16 12:40:05 +0200159 list_del(&req->list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300160 if (req->request.status == -EINPROGRESS)
161 req->request.status = status;
162 musb = req->musb;
163
164 ep->busy = 1;
165 spin_unlock(&musb->lock);
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +0530166
167 if (!dma_mapping_error(&musb->g.dev, request->dma))
168 unmap_dma_buffer(req, musb);
169
Felipe Balbi550a7372008-07-24 12:27:36 +0300170 if (request->status == 0)
Bin Liub99d3652016-06-30 12:12:22 -0500171 musb_dbg(musb, "%s done request %p, %d/",
Felipe Balbi550a7372008-07-24 12:27:36 +0300172 ep->end_point.name, request,
173 req->request.actual, req->request.length);
174 else
Bin Liub99d3652016-06-30 12:12:22 -0500175 musb_dbg(musb, "%s request %p, %d/%d fault %d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300176 ep->end_point.name, request,
177 req->request.actual, req->request.length,
178 request->status);
Michal Sojka304f7e52014-09-24 22:43:19 +0200179 usb_gadget_giveback_request(&req->ep->end_point, &req->request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300180 spin_lock(&musb->lock);
181 ep->busy = busy;
182}
183
184/* ----------------------------------------------------------------------- */
185
186/*
187 * Abort requests queued to an endpoint using the status. Synchronous.
188 * caller locked controller and blocked irqs, and selected this ep.
189 */
190static void nuke(struct musb_ep *ep, const int status)
191{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300192 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300193 struct musb_request *req = NULL;
194 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
195
196 ep->busy = 1;
197
198 if (is_dma_capable() && ep->dma) {
199 struct dma_controller *c = ep->musb->dma_controller;
200 int value;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700201
Felipe Balbi550a7372008-07-24 12:27:36 +0300202 if (ep->is_in) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700203 /*
204 * The programming guide says that we must not clear
205 * the DMAMODE bit before DMAENAB, so we only
206 * clear it in the second write...
207 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300208 musb_writew(epio, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700209 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
Felipe Balbi550a7372008-07-24 12:27:36 +0300210 musb_writew(epio, MUSB_TXCSR,
211 0 | MUSB_TXCSR_FLUSHFIFO);
212 } else {
213 musb_writew(epio, MUSB_RXCSR,
214 0 | MUSB_RXCSR_FLUSHFIFO);
215 musb_writew(epio, MUSB_RXCSR,
216 0 | MUSB_RXCSR_FLUSHFIFO);
217 }
218
219 value = c->channel_abort(ep->dma);
Bin Liub99d3652016-06-30 12:12:22 -0500220 musb_dbg(musb, "%s: abort DMA --> %d", ep->name, value);
Felipe Balbi550a7372008-07-24 12:27:36 +0300221 c->channel_release(ep->dma);
222 ep->dma = NULL;
223 }
224
Felipe Balbiad1adb82011-02-16 12:40:05 +0200225 while (!list_empty(&ep->req_list)) {
226 req = list_first_entry(&ep->req_list, struct musb_request, list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300227 musb_g_giveback(ep, &req->request, status);
228 }
229}
230
231/* ----------------------------------------------------------------------- */
232
233/* Data transfers - pure PIO, pure DMA, or mixed mode */
234
235/*
236 * This assumes the separate CPPI engine is responding to DMA requests
237 * from the usb core ... sequenced a bit differently from mentor dma.
238 */
239
240static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
241{
242 if (can_bulk_split(musb, ep->type))
243 return ep->hw_ep->max_packet_sz_tx;
244 else
245 return ep->packet_sz;
246}
247
Felipe Balbi550a7372008-07-24 12:27:36 +0300248/*
249 * An endpoint is transmitting data. This can be called either from
250 * the IRQ routine or from ep.queue() to kickstart a request on an
251 * endpoint.
252 *
253 * Context: controller locked, IRQs blocked, endpoint selected
254 */
255static void txstate(struct musb *musb, struct musb_request *req)
256{
257 u8 epnum = req->epnum;
258 struct musb_ep *musb_ep;
259 void __iomem *epio = musb->endpoints[epnum].regs;
260 struct usb_request *request;
261 u16 fifo_count = 0, csr;
262 int use_dma = 0;
263
264 musb_ep = req->ep;
265
Vikram Panditaabf710e2012-05-18 13:48:04 -0700266 /* Check if EP is disabled */
267 if (!musb_ep->desc) {
Bin Liub99d3652016-06-30 12:12:22 -0500268 musb_dbg(musb, "ep:%s disabled - ignore request",
Vikram Panditaabf710e2012-05-18 13:48:04 -0700269 musb_ep->end_point.name);
270 return;
271 }
272
Felipe Balbi550a7372008-07-24 12:27:36 +0300273 /* we shouldn't get here while DMA is active ... but we do ... */
274 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Bin Liub99d3652016-06-30 12:12:22 -0500275 musb_dbg(musb, "dma pending...");
Felipe Balbi550a7372008-07-24 12:27:36 +0300276 return;
277 }
278
279 /* read TXCSR before */
280 csr = musb_readw(epio, MUSB_TXCSR);
281
282 request = &req->request;
283 fifo_count = min(max_ep_writesize(musb, musb_ep),
284 (int)(request->length - request->actual));
285
286 if (csr & MUSB_TXCSR_TXPKTRDY) {
Bin Liub99d3652016-06-30 12:12:22 -0500287 musb_dbg(musb, "%s old packet still ready , txcsr %03x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300288 musb_ep->end_point.name, csr);
289 return;
290 }
291
292 if (csr & MUSB_TXCSR_P_SENDSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -0500293 musb_dbg(musb, "%s stalling, txcsr %03x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300294 musb_ep->end_point.name, csr);
295 return;
296 }
297
Bin Liub99d3652016-06-30 12:12:22 -0500298 musb_dbg(musb, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300299 epnum, musb_ep->packet_sz, fifo_count,
300 csr);
301
302#ifndef CONFIG_MUSB_PIO_ONLY
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100303 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300304 struct dma_controller *c = musb->dma_controller;
Ming Lei66af83d2010-09-20 10:32:06 +0300305 size_t request_size;
306
307 /* setup DMA, then program endpoint CSR */
308 request_size = min_t(size_t, request->length - request->actual,
309 musb_ep->dma->max_len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300310
Ajay Kumar Guptad17d5352012-07-20 11:07:23 +0530311 use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300312
313 /* MUSB_TXCSR_P_ISO is still set correctly */
314
Felipe Balbi03840fa2015-08-06 10:47:16 -0500315 if (musb_dma_inventra(musb) || musb_dma_ux500(musb)) {
Anand Gadiyard1043a22009-04-02 12:07:08 -0700316 if (request_size < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300317 musb_ep->dma->desired_mode = 0;
318 else
319 musb_ep->dma->desired_mode = 1;
320
321 use_dma = use_dma && c->channel_program(
322 musb_ep->dma, musb_ep->packet_sz,
323 musb_ep->dma->desired_mode,
Cliff Cai796a83f2009-12-21 21:18:02 -0500324 request->dma + request->actual, request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300325 if (use_dma) {
326 if (musb_ep->dma->desired_mode == 0) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700327 /*
328 * We must not clear the DMAMODE bit
329 * before the DMAENAB bit -- and the
330 * latter doesn't always get cleared
331 * before we get here...
332 */
333 csr &= ~(MUSB_TXCSR_AUTOSET
334 | MUSB_TXCSR_DMAENAB);
335 musb_writew(epio, MUSB_TXCSR, csr
336 | MUSB_TXCSR_P_WZC_BITS);
337 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300338 csr |= (MUSB_TXCSR_DMAENAB |
339 MUSB_TXCSR_MODE);
340 /* against programming guide */
Ming Leif11d8932010-09-24 13:44:04 +0300341 } else {
342 csr |= (MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300343 | MUSB_TXCSR_DMAMODE
344 | MUSB_TXCSR_MODE);
supriya karanthbb3a2ef2012-12-06 11:12:48 +0530345 /*
346 * Enable Autoset according to table
347 * below
348 * bulk_split hb_mult Autoset_Enable
349 * 0 0 Yes(Normal)
350 * 0 >0 No(High BW ISO)
351 * 1 0 Yes(HS bulk)
352 * 1 >0 Yes(FS bulk)
353 */
354 if (!musb_ep->hb_mult ||
Geyslan G. Bem1a171622015-12-10 17:50:12 -0300355 can_bulk_split(musb,
356 musb_ep->type))
Ming Leif11d8932010-09-24 13:44:04 +0300357 csr |= MUSB_TXCSR_AUTOSET;
358 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300359 csr &= ~MUSB_TXCSR_P_UNDERRUN;
Ming Leif11d8932010-09-24 13:44:04 +0300360
Felipe Balbi550a7372008-07-24 12:27:36 +0300361 musb_writew(epio, MUSB_TXCSR, csr);
362 }
363 }
364
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700365 if (is_cppi_enabled(musb)) {
Sebastian Andrzej Siewiorfc525752013-08-13 19:38:23 +0200366 /* program endpoint CSR first, then setup DMA */
367 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
368 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
369 MUSB_TXCSR_MODE;
370 musb_writew(epio, MUSB_TXCSR, (MUSB_TXCSR_P_WZC_BITS &
371 ~MUSB_TXCSR_P_UNDERRUN) | csr);
372
373 /* ensure writebuffer is empty */
374 csr = musb_readw(epio, MUSB_TXCSR);
375
376 /*
377 * NOTE host side sets DMAENAB later than this; both are
378 * OK since the transfer dma glue (between CPPI and
379 * Mentor fifos) just tells CPPI it could start. Data
380 * only moves to the USB TX fifo when both fifos are
381 * ready.
382 */
383 /*
384 * "mode" is irrelevant here; handle terminating ZLPs
385 * like PIO does, since the hardware RNDIS mode seems
386 * unreliable except for the
387 * last-packet-is-already-short case.
388 */
389 use_dma = use_dma && c->channel_program(
390 musb_ep->dma, musb_ep->packet_sz,
391 0,
392 request->dma + request->actual,
393 request_size);
394 if (!use_dma) {
395 c->channel_release(musb_ep->dma);
396 musb_ep->dma = NULL;
397 csr &= ~MUSB_TXCSR_DMAENAB;
398 musb_writew(epio, MUSB_TXCSR, csr);
399 /* invariant: prequest->buf is non-null */
400 }
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700401 } else if (tusb_dma_omap(musb))
Sebastian Andrzej Siewiorfc525752013-08-13 19:38:23 +0200402 use_dma = use_dma && c->channel_program(
403 musb_ep->dma, musb_ep->packet_sz,
404 request->zero,
405 request->dma + request->actual,
406 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300407 }
408#endif
409
410 if (!use_dma) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600411 /*
412 * Unmap the dma buffer back to cpu if dma channel
413 * programming fails
414 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100415 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600416
Felipe Balbi550a7372008-07-24 12:27:36 +0300417 musb_write_fifo(musb_ep->hw_ep, fifo_count,
418 (u8 *) (request->buf + request->actual));
419 request->actual += fifo_count;
420 csr |= MUSB_TXCSR_TXPKTRDY;
421 csr &= ~MUSB_TXCSR_P_UNDERRUN;
422 musb_writew(epio, MUSB_TXCSR, csr);
423 }
424
425 /* host may already have the data when this message shows... */
Bin Liub99d3652016-06-30 12:12:22 -0500426 musb_dbg(musb, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300427 musb_ep->end_point.name, use_dma ? "dma" : "pio",
428 request->actual, request->length,
429 musb_readw(epio, MUSB_TXCSR),
430 fifo_count,
431 musb_readw(epio, MUSB_TXMAXP));
432}
433
434/*
435 * FIFO state update (e.g. data ready).
436 * Called from IRQ, with controller locked.
437 */
438void musb_g_tx(struct musb *musb, u8 epnum)
439{
440 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200441 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300442 struct usb_request *request;
443 u8 __iomem *mbase = musb->mregs;
444 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
445 void __iomem *epio = musb->endpoints[epnum].regs;
446 struct dma_channel *dma;
447
448 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200449 req = next_request(musb_ep);
450 request = &req->request;
Felipe Balbi550a7372008-07-24 12:27:36 +0300451
452 csr = musb_readw(epio, MUSB_TXCSR);
Bin Liub99d3652016-06-30 12:12:22 -0500453 musb_dbg(musb, "<== %s, txcsr %04x", musb_ep->end_point.name, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300454
455 dma = is_dma_capable() ? musb_ep->dma : NULL;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300456
457 /*
458 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
459 * probably rates reporting as a host error.
460 */
461 if (csr & MUSB_TXCSR_P_SENTSTALL) {
462 csr |= MUSB_TXCSR_P_WZC_BITS;
463 csr &= ~MUSB_TXCSR_P_SENTSTALL;
464 musb_writew(epio, MUSB_TXCSR, csr);
465 return;
466 }
467
468 if (csr & MUSB_TXCSR_P_UNDERRUN) {
469 /* We NAKed, no big deal... little reason to care. */
470 csr |= MUSB_TXCSR_P_WZC_BITS;
471 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
472 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300473 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
474 epnum, request);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300475 }
476
477 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
478 /*
479 * SHOULD NOT HAPPEN... has with CPPI though, after
480 * changing SENDSTALL (and other cases); harmless?
Felipe Balbi550a7372008-07-24 12:27:36 +0300481 */
Bin Liub99d3652016-06-30 12:12:22 -0500482 musb_dbg(musb, "%s dma still busy?", musb_ep->end_point.name);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300483 return;
484 }
485
486 if (request) {
487 u8 is_dma = 0;
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700488 bool short_packet = false;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300489
490 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
491 is_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300492 csr |= MUSB_TXCSR_P_WZC_BITS;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300493 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
Mian Yousaf Kaukab100d4a92011-03-15 16:24:24 +0100494 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
Felipe Balbi550a7372008-07-24 12:27:36 +0300495 musb_writew(epio, MUSB_TXCSR, csr);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300496 /* Ensure writebuffer is empty. */
497 csr = musb_readw(epio, MUSB_TXCSR);
498 request->actual += musb_ep->dma->actual_len;
Bin Liub99d3652016-06-30 12:12:22 -0500499 musb_dbg(musb, "TXCSR%d %04x, DMA off, len %zu, req %p",
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300500 epnum, csr, musb_ep->dma->actual_len, request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300501 }
502
Ming Leie7379aa2010-09-24 13:44:14 +0300503 /*
504 * First, maybe a terminating short packet. Some DMA
505 * engines might handle this by themselves.
506 */
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700507 if ((request->zero && request->length)
Ming Leie7379aa2010-09-24 13:44:14 +0300508 && (request->length % musb_ep->packet_sz == 0)
509 && (request->actual == request->length))
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700510 short_packet = true;
511
512 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb)) &&
513 (is_dma && (!dma->desired_mode ||
Ming Leie7379aa2010-09-24 13:44:14 +0300514 (request->actual &
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700515 (musb_ep->packet_sz - 1)))))
516 short_packet = true;
517
518 if (short_packet) {
Ming Leie7379aa2010-09-24 13:44:14 +0300519 /*
520 * On DMA completion, FIFO may not be
521 * available yet...
522 */
523 if (csr & MUSB_TXCSR_TXPKTRDY)
524 return;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300525
Ming Leie7379aa2010-09-24 13:44:14 +0300526 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
527 | MUSB_TXCSR_TXPKTRDY);
528 request->zero = 0;
529 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300530
Ming Leie7379aa2010-09-24 13:44:14 +0300531 if (request->actual == request->length) {
532 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530533 /*
534 * In the giveback function the MUSB lock is
535 * released and acquired after sometime. During
536 * this time period the INDEX register could get
537 * changed by the gadget_queue function especially
538 * on SMP systems. Reselect the INDEX to be sure
539 * we are reading/modifying the right registers
540 */
541 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200542 req = musb_ep->desc ? next_request(musb_ep) : NULL;
543 if (!req) {
Bin Liub99d3652016-06-30 12:12:22 -0500544 musb_dbg(musb, "%s idle now",
Ming Leie7379aa2010-09-24 13:44:14 +0300545 musb_ep->end_point.name);
546 return;
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300547 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300548 }
549
Felipe Balbiad1adb82011-02-16 12:40:05 +0200550 txstate(musb, req);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300551 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300552}
553
554/* ------------------------------------------------------------ */
555
Felipe Balbi550a7372008-07-24 12:27:36 +0300556/*
557 * Context: controller locked, IRQs blocked, endpoint selected
558 */
559static void rxstate(struct musb *musb, struct musb_request *req)
560{
Felipe Balbi550a7372008-07-24 12:27:36 +0300561 const u8 epnum = req->epnum;
562 struct usb_request *request = &req->request;
Ming Leibd2e74d2010-09-20 10:32:01 +0300563 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300564 void __iomem *epio = musb->endpoints[epnum].regs;
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400565 unsigned len = 0;
566 u16 fifo_count;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300567 u16 csr = musb_readw(epio, MUSB_RXCSR);
Ming Leibd2e74d2010-09-20 10:32:01 +0300568 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700569 u8 use_mode_1;
Ming Leibd2e74d2010-09-20 10:32:01 +0300570
571 if (hw_ep->is_shared_fifo)
572 musb_ep = &hw_ep->ep_in;
573 else
574 musb_ep = &hw_ep->ep_out;
575
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400576 fifo_count = musb_ep->packet_sz;
Felipe Balbi550a7372008-07-24 12:27:36 +0300577
Vikram Panditaabf710e2012-05-18 13:48:04 -0700578 /* Check if EP is disabled */
579 if (!musb_ep->desc) {
Bin Liub99d3652016-06-30 12:12:22 -0500580 musb_dbg(musb, "ep:%s disabled - ignore request",
Vikram Panditaabf710e2012-05-18 13:48:04 -0700581 musb_ep->end_point.name);
582 return;
583 }
584
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300585 /* We shouldn't get here while DMA is active, but we do... */
586 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Bin Liub99d3652016-06-30 12:12:22 -0500587 musb_dbg(musb, "DMA pending...");
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300588 return;
589 }
590
591 if (csr & MUSB_RXCSR_P_SENDSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -0500592 musb_dbg(musb, "%s stalling, RXCSR %04x",
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300593 musb_ep->end_point.name, csr);
594 return;
595 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300596
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700597 if (is_cppi_enabled(musb) && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300598 struct dma_controller *c = musb->dma_controller;
599 struct dma_channel *channel = musb_ep->dma;
600
601 /* NOTE: CPPI won't actually stop advancing the DMA
602 * queue after short packet transfers, so this is almost
603 * always going to run as IRQ-per-packet DMA so that
604 * faults will be handled correctly.
605 */
606 if (c->channel_program(channel,
607 musb_ep->packet_sz,
608 !request->short_not_ok,
609 request->dma + request->actual,
610 request->length - request->actual)) {
611
612 /* make sure that if an rxpkt arrived after the irq,
613 * the cppi engine will be ready to take it as soon
614 * as DMA is enabled
615 */
616 csr &= ~(MUSB_RXCSR_AUTOCLEAR
617 | MUSB_RXCSR_DMAMODE);
618 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
619 musb_writew(epio, MUSB_RXCSR, csr);
620 return;
621 }
622 }
623
624 if (csr & MUSB_RXCSR_RXPKTRDY) {
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400625 fifo_count = musb_readw(epio, MUSB_RXCOUNT);
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700626
627 /*
Felipe Balbi00a89182012-10-26 09:55:31 +0300628 * Enable Mode 1 on RX transfers only when short_not_ok flag
629 * is set. Currently short_not_ok flag is set only from
630 * file_storage and f_mass_storage drivers
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700631 */
Felipe Balbi00a89182012-10-26 09:55:31 +0300632
633 if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700634 use_mode_1 = 1;
635 else
636 use_mode_1 = 0;
637
Felipe Balbi550a7372008-07-24 12:27:36 +0300638 if (request->actual < request->length) {
Felipe Balbi03840fa2015-08-06 10:47:16 -0500639 if (!is_buffer_mapped(req))
640 goto buffer_aint_mapped;
641
642 if (musb_dma_inventra(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300643 struct dma_controller *c;
644 struct dma_channel *channel;
645 int use_dma = 0;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200646 unsigned int transfer_size;
Felipe Balbi550a7372008-07-24 12:27:36 +0300647
648 c = musb->dma_controller;
649 channel = musb_ep->dma;
650
Felipe Balbi00a89182012-10-26 09:55:31 +0300651 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
652 * mode 0 only. So we do not get endpoint interrupts due to DMA
653 * completion. We only get interrupts from DMA controller.
654 *
655 * We could operate in DMA mode 1 if we knew the size of the tranfer
656 * in advance. For mass storage class, request->length = what the host
657 * sends, so that'd work. But for pretty much everything else,
658 * request->length is routinely more than what the host sends. For
659 * most these gadgets, end of is signified either by a short packet,
660 * or filling the last byte of the buffer. (Sending extra data in
661 * that last pckate should trigger an overflow fault.) But in mode 1,
662 * we don't get DMA completion interrupt for short packets.
663 *
664 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
665 * to get endpoint interrupt on every DMA req, but that didn't seem
666 * to work reliably.
667 *
668 * REVISIT an updated g_file_storage can set req->short_not_ok, which
669 * then becomes usable as a runtime "use mode 1" hint...
670 */
671
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700672 /* Experimental: Mode1 works with mass storage use cases */
673 if (use_mode_1) {
Ming Lei9001d802010-09-25 05:50:43 -0500674 csr |= MUSB_RXCSR_AUTOCLEAR;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700675 musb_writew(epio, MUSB_RXCSR, csr);
676 csr |= MUSB_RXCSR_DMAENAB;
677 musb_writew(epio, MUSB_RXCSR, csr);
678
679 /*
680 * this special sequence (enabling and then
681 * disabling MUSB_RXCSR_DMAMODE) is required
682 * to get DMAReq to activate
683 */
684 musb_writew(epio, MUSB_RXCSR,
685 csr | MUSB_RXCSR_DMAMODE);
686 musb_writew(epio, MUSB_RXCSR, csr);
687
Felipe Balbi37730ec2013-02-06 10:19:15 +0200688 transfer_size = min_t(unsigned int,
689 request->length -
690 request->actual,
Roger Quadros660fa882012-08-07 16:26:32 +0300691 channel->max_len);
692 musb_ep->dma->desired_mode = 1;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700693 } else {
694 if (!musb_ep->hb_mult &&
695 musb_ep->hw_ep->rx_double_buffered)
696 csr |= MUSB_RXCSR_AUTOCLEAR;
697 csr |= MUSB_RXCSR_DMAENAB;
698 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300699
Roger Quadros660fa882012-08-07 16:26:32 +0300700 transfer_size = min(request->length - request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400701 (unsigned)fifo_count);
Roger Quadros660fa882012-08-07 16:26:32 +0300702 musb_ep->dma->desired_mode = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300703 }
704
Roger Quadros660fa882012-08-07 16:26:32 +0300705 use_dma = c->channel_program(
706 channel,
707 musb_ep->packet_sz,
708 channel->desired_mode,
709 request->dma
710 + request->actual,
711 transfer_size);
712
Felipe Balbi550a7372008-07-24 12:27:36 +0300713 if (use_dma)
714 return;
715 }
Felipe Balbi03840fa2015-08-06 10:47:16 -0500716
717 if ((musb_dma_ux500(musb)) &&
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100718 (request->actual < request->length)) {
719
720 struct dma_controller *c;
721 struct dma_channel *channel;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200722 unsigned int transfer_size = 0;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100723
724 c = musb->dma_controller;
725 channel = musb_ep->dma;
726
727 /* In case first packet is short */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400728 if (fifo_count < musb_ep->packet_sz)
729 transfer_size = fifo_count;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100730 else if (request->short_not_ok)
Felipe Balbi37730ec2013-02-06 10:19:15 +0200731 transfer_size = min_t(unsigned int,
732 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100733 request->actual,
734 channel->max_len);
735 else
Felipe Balbi37730ec2013-02-06 10:19:15 +0200736 transfer_size = min_t(unsigned int,
737 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100738 request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400739 (unsigned)fifo_count);
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100740
741 csr &= ~MUSB_RXCSR_DMAMODE;
742 csr |= (MUSB_RXCSR_DMAENAB |
743 MUSB_RXCSR_AUTOCLEAR);
744
745 musb_writew(epio, MUSB_RXCSR, csr);
746
747 if (transfer_size <= musb_ep->packet_sz) {
748 musb_ep->dma->desired_mode = 0;
749 } else {
750 musb_ep->dma->desired_mode = 1;
751 /* Mode must be set after DMAENAB */
752 csr |= MUSB_RXCSR_DMAMODE;
753 musb_writew(epio, MUSB_RXCSR, csr);
754 }
755
756 if (c->channel_program(channel,
757 musb_ep->packet_sz,
758 channel->desired_mode,
759 request->dma
760 + request->actual,
761 transfer_size))
762
763 return;
764 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300765
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400766 len = request->length - request->actual;
Bin Liub99d3652016-06-30 12:12:22 -0500767 musb_dbg(musb, "%s OUT/RX pio fifo %d/%d, maxpacket %d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300768 musb_ep->end_point.name,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400769 fifo_count, len,
Felipe Balbi550a7372008-07-24 12:27:36 +0300770 musb_ep->packet_sz);
771
Felipe Balbic2c96322009-02-21 15:29:42 -0800772 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300773
Felipe Balbi03840fa2015-08-06 10:47:16 -0500774 if (tusb_dma_omap(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300775 struct dma_controller *c = musb->dma_controller;
776 struct dma_channel *channel = musb_ep->dma;
777 u32 dma_addr = request->dma + request->actual;
778 int ret;
779
780 ret = c->channel_program(channel,
781 musb_ep->packet_sz,
782 channel->desired_mode,
783 dma_addr,
784 fifo_count);
785 if (ret)
786 return;
787 }
Felipe Balbi03840fa2015-08-06 10:47:16 -0500788
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600789 /*
790 * Unmap the dma buffer back to cpu if dma channel
791 * programming fails. This buffer is mapped if the
792 * channel allocation is successful
793 */
Felipe Balbi03840fa2015-08-06 10:47:16 -0500794 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600795
Felipe Balbi03840fa2015-08-06 10:47:16 -0500796 /*
797 * Clear DMAENAB and AUTOCLEAR for the
798 * PIO mode transfer
799 */
800 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
801 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300802
Felipe Balbi03840fa2015-08-06 10:47:16 -0500803buffer_aint_mapped:
Felipe Balbi550a7372008-07-24 12:27:36 +0300804 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
805 (request->buf + request->actual));
806 request->actual += fifo_count;
807
808 /* REVISIT if we left anything in the fifo, flush
809 * it and report -EOVERFLOW
810 */
811
812 /* ack the read! */
813 csr |= MUSB_RXCSR_P_WZC_BITS;
814 csr &= ~MUSB_RXCSR_RXPKTRDY;
815 musb_writew(epio, MUSB_RXCSR, csr);
816 }
817 }
818
819 /* reach the end or short packet detected */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400820 if (request->actual == request->length ||
821 fifo_count < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300822 musb_g_giveback(musb_ep, request, 0);
823}
824
825/*
826 * Data ready for a request; called from IRQ
827 */
828void musb_g_rx(struct musb *musb, u8 epnum)
829{
830 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200831 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300832 struct usb_request *request;
833 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300834 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300835 void __iomem *epio = musb->endpoints[epnum].regs;
836 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300837 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
838
839 if (hw_ep->is_shared_fifo)
840 musb_ep = &hw_ep->ep_in;
841 else
842 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300843
844 musb_ep_select(mbase, epnum);
845
Felipe Balbiad1adb82011-02-16 12:40:05 +0200846 req = next_request(musb_ep);
847 if (!req)
Maulik Mankad0abdc362009-12-22 16:18:19 +0530848 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300849
Felipe Balbiad1adb82011-02-16 12:40:05 +0200850 request = &req->request;
851
Felipe Balbi550a7372008-07-24 12:27:36 +0300852 csr = musb_readw(epio, MUSB_RXCSR);
853 dma = is_dma_capable() ? musb_ep->dma : NULL;
854
Bin Liub99d3652016-06-30 12:12:22 -0500855 musb_dbg(musb, "<== %s, rxcsr %04x%s %p", musb_ep->end_point.name,
Felipe Balbi550a7372008-07-24 12:27:36 +0300856 csr, dma ? " (dma)" : "", request);
857
858 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300859 csr |= MUSB_RXCSR_P_WZC_BITS;
860 csr &= ~MUSB_RXCSR_P_SENTSTALL;
861 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300862 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300863 }
864
865 if (csr & MUSB_RXCSR_P_OVERRUN) {
866 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
867 csr &= ~MUSB_RXCSR_P_OVERRUN;
868 musb_writew(epio, MUSB_RXCSR, csr);
869
Bin Liub99d3652016-06-30 12:12:22 -0500870 musb_dbg(musb, "%s iso overrun on %p", musb_ep->name, request);
Sergei Shtylyov43467862010-09-24 13:44:12 +0300871 if (request->status == -EINPROGRESS)
Felipe Balbi550a7372008-07-24 12:27:36 +0300872 request->status = -EOVERFLOW;
873 }
874 if (csr & MUSB_RXCSR_INCOMPRX) {
875 /* REVISIT not necessarily an error */
Bin Liub99d3652016-06-30 12:12:22 -0500876 musb_dbg(musb, "%s, incomprx", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +0300877 }
878
879 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
880 /* "should not happen"; likely RXPKTRDY pending for DMA */
Bin Liub99d3652016-06-30 12:12:22 -0500881 musb_dbg(musb, "%s busy, csr %04x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300882 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300883 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300884 }
885
886 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
887 csr &= ~(MUSB_RXCSR_AUTOCLEAR
888 | MUSB_RXCSR_DMAENAB
889 | MUSB_RXCSR_DMAMODE);
890 musb_writew(epio, MUSB_RXCSR,
891 MUSB_RXCSR_P_WZC_BITS | csr);
892
893 request->actual += musb_ep->dma->actual_len;
894
Bin Liub99d3652016-06-30 12:12:22 -0500895 musb_dbg(musb, "RXCSR%d %04x, dma off, %04x, len %zu, req %p",
Felipe Balbi550a7372008-07-24 12:27:36 +0300896 epnum, csr,
897 musb_readw(epio, MUSB_RXCSR),
898 musb_ep->dma->actual_len, request);
899
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100900#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
901 defined(CONFIG_USB_UX500_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +0300902 /* Autoclear doesn't clear RxPktRdy for short packets */
Ming Lei9001d802010-09-25 05:50:43 -0500903 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
Felipe Balbi550a7372008-07-24 12:27:36 +0300904 || (dma->actual_len
905 & (musb_ep->packet_sz - 1))) {
906 /* ack the read! */
907 csr &= ~MUSB_RXCSR_RXPKTRDY;
908 musb_writew(epio, MUSB_RXCSR, csr);
909 }
910
911 /* incomplete, and not short? wait for next IN packet */
912 if ((request->actual < request->length)
913 && (musb_ep->dma->actual_len
Ming Lei9001d802010-09-25 05:50:43 -0500914 == musb_ep->packet_sz)) {
915 /* In double buffer case, continue to unload fifo if
916 * there is Rx packet in FIFO.
917 **/
918 csr = musb_readw(epio, MUSB_RXCSR);
919 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
920 hw_ep->rx_double_buffered)
921 goto exit;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300922 return;
Ming Lei9001d802010-09-25 05:50:43 -0500923 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300924#endif
925 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530926 /*
927 * In the giveback function the MUSB lock is
928 * released and acquired after sometime. During
929 * this time period the INDEX register could get
930 * changed by the gadget_queue function especially
931 * on SMP systems. Reselect the INDEX to be sure
932 * we are reading/modifying the right registers
933 */
934 musb_ep_select(mbase, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300935
Felipe Balbiad1adb82011-02-16 12:40:05 +0200936 req = next_request(musb_ep);
937 if (!req)
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300938 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300939 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100940#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
941 defined(CONFIG_USB_UX500_DMA)
Ming Lei9001d802010-09-25 05:50:43 -0500942exit:
Ajay Kumar Guptabb324b02010-11-22 14:22:41 +0530943#endif
Sergei Shtylyov43467862010-09-24 13:44:12 +0300944 /* Analyze request */
Felipe Balbiad1adb82011-02-16 12:40:05 +0200945 rxstate(musb, req);
Felipe Balbi550a7372008-07-24 12:27:36 +0300946}
947
948/* ------------------------------------------------------------ */
949
950static int musb_gadget_enable(struct usb_ep *ep,
951 const struct usb_endpoint_descriptor *desc)
952{
953 unsigned long flags;
954 struct musb_ep *musb_ep;
955 struct musb_hw_ep *hw_ep;
956 void __iomem *regs;
957 struct musb *musb;
958 void __iomem *mbase;
959 u8 epnum;
960 u16 csr;
961 unsigned tmp;
962 int status = -EINVAL;
963
964 if (!ep || !desc)
965 return -EINVAL;
966
967 musb_ep = to_musb_ep(ep);
968 hw_ep = musb_ep->hw_ep;
969 regs = hw_ep->regs;
970 musb = musb_ep->musb;
971 mbase = musb->mregs;
972 epnum = musb_ep->current_epnum;
973
974 spin_lock_irqsave(&musb->lock, flags);
975
976 if (musb_ep->desc) {
977 status = -EBUSY;
978 goto fail;
979 }
Julia Lawall96bcd092009-01-24 17:57:24 -0800980 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +0300981
982 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -0800983 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300984 goto fail;
985
986 /* REVISIT this rules out high bandwidth periodic transfers */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700987 tmp = usb_endpoint_maxp(desc);
Ming Leif11d8932010-09-24 13:44:04 +0300988 if (tmp & ~0x07ff) {
989 int ok;
990
991 if (usb_endpoint_dir_in(desc))
992 ok = musb->hb_iso_tx;
993 else
994 ok = musb->hb_iso_rx;
995
996 if (!ok) {
Bin Liub99d3652016-06-30 12:12:22 -0500997 musb_dbg(musb, "no support for high bandwidth ISO");
Ming Leif11d8932010-09-24 13:44:04 +0300998 goto fail;
999 }
1000 musb_ep->hb_mult = (tmp >> 11) & 3;
1001 } else {
1002 musb_ep->hb_mult = 0;
1003 }
1004
1005 musb_ep->packet_sz = tmp & 0x7ff;
1006 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
Felipe Balbi550a7372008-07-24 12:27:36 +03001007
1008 /* enable the interrupts for the endpoint, set the endpoint
1009 * packet size (or fail), set the mode, clear the fifo
1010 */
1011 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -08001012 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001013
1014 if (hw_ep->is_shared_fifo)
1015 musb_ep->is_in = 1;
1016 if (!musb_ep->is_in)
1017 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001018
1019 if (tmp > hw_ep->max_packet_sz_tx) {
Bin Liub99d3652016-06-30 12:12:22 -05001020 musb_dbg(musb, "packet size beyond hardware FIFO size");
Felipe Balbi550a7372008-07-24 12:27:36 +03001021 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001022 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001023
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001024 musb->intrtxe |= (1 << epnum);
1025 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001026
1027 /* REVISIT if can_bulk_split(), use by updating "tmp";
1028 * likewise high bandwidth periodic tx
1029 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001030 /* Set TXMAXP with the FIFO size of the endpoint
Ming Lei31c99092010-10-19 19:08:25 -05001031 * to disable double buffering mode.
Cliff Cai9f445cb2010-01-28 20:44:18 -05001032 */
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301033 if (musb->double_buffer_not_ok) {
Felipe Balbi06624812011-01-21 13:39:20 +08001034 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301035 } else {
1036 if (can_bulk_split(musb, musb_ep->type))
1037 musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
1038 musb_ep->packet_sz) - 1;
Felipe Balbi06624812011-01-21 13:39:20 +08001039 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1040 | (musb_ep->hb_mult << 11));
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301041 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001042
1043 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1044 if (musb_readw(regs, MUSB_TXCSR)
1045 & MUSB_TXCSR_FIFONOTEMPTY)
1046 csr |= MUSB_TXCSR_FLUSHFIFO;
1047 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1048 csr |= MUSB_TXCSR_P_ISO;
1049
1050 /* set twice in case of double buffering */
1051 musb_writew(regs, MUSB_TXCSR, csr);
1052 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1053 musb_writew(regs, MUSB_TXCSR, csr);
1054
1055 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +03001056
1057 if (hw_ep->is_shared_fifo)
1058 musb_ep->is_in = 0;
1059 if (musb_ep->is_in)
1060 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001061
1062 if (tmp > hw_ep->max_packet_sz_rx) {
Bin Liub99d3652016-06-30 12:12:22 -05001063 musb_dbg(musb, "packet size beyond hardware FIFO size");
Felipe Balbi550a7372008-07-24 12:27:36 +03001064 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001065 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001066
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001067 musb->intrrxe |= (1 << epnum);
1068 musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001069
1070 /* REVISIT if can_bulk_combine() use by updating "tmp"
1071 * likewise high bandwidth periodic rx
1072 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001073 /* Set RXMAXP with the FIFO size of the endpoint
1074 * to disable double buffering mode.
1075 */
Felipe Balbi06624812011-01-21 13:39:20 +08001076 if (musb->double_buffer_not_ok)
1077 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1078 else
1079 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1080 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001081
1082 /* force shared fifo to OUT-only mode */
1083 if (hw_ep->is_shared_fifo) {
1084 csr = musb_readw(regs, MUSB_TXCSR);
1085 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1086 musb_writew(regs, MUSB_TXCSR, csr);
1087 }
1088
1089 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1090 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1091 csr |= MUSB_RXCSR_P_ISO;
1092 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1093 csr |= MUSB_RXCSR_DISNYET;
1094
1095 /* set twice in case of double buffering */
1096 musb_writew(regs, MUSB_RXCSR, csr);
1097 musb_writew(regs, MUSB_RXCSR, csr);
1098 }
1099
1100 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1101 * for some reason you run out of channels here.
1102 */
1103 if (is_dma_capable() && musb->dma_controller) {
1104 struct dma_controller *c = musb->dma_controller;
1105
1106 musb_ep->dma = c->channel_alloc(c, hw_ep,
1107 (desc->bEndpointAddress & USB_DIR_IN));
1108 } else
1109 musb_ep->dma = NULL;
1110
1111 musb_ep->desc = desc;
1112 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001113 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001114 status = 0;
1115
1116 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1117 musb_driver_name, musb_ep->end_point.name,
1118 ({ char *s; switch (musb_ep->type) {
1119 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1120 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1121 default: s = "iso"; break;
Joe Perches2b84f922013-10-08 16:01:37 -07001122 } s; }),
Felipe Balbi550a7372008-07-24 12:27:36 +03001123 musb_ep->is_in ? "IN" : "OUT",
1124 musb_ep->dma ? "dma, " : "",
1125 musb_ep->packet_sz);
1126
1127 schedule_work(&musb->irq_work);
1128
1129fail:
1130 spin_unlock_irqrestore(&musb->lock, flags);
1131 return status;
1132}
1133
1134/*
1135 * Disable an endpoint flushing all requests queued.
1136 */
1137static int musb_gadget_disable(struct usb_ep *ep)
1138{
1139 unsigned long flags;
1140 struct musb *musb;
1141 u8 epnum;
1142 struct musb_ep *musb_ep;
1143 void __iomem *epio;
1144 int status = 0;
1145
1146 musb_ep = to_musb_ep(ep);
1147 musb = musb_ep->musb;
1148 epnum = musb_ep->current_epnum;
1149 epio = musb->endpoints[epnum].regs;
1150
1151 spin_lock_irqsave(&musb->lock, flags);
1152 musb_ep_select(musb->mregs, epnum);
1153
1154 /* zero the endpoint sizes */
1155 if (musb_ep->is_in) {
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001156 musb->intrtxe &= ~(1 << epnum);
1157 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001158 musb_writew(epio, MUSB_TXMAXP, 0);
1159 } else {
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001160 musb->intrrxe &= ~(1 << epnum);
1161 musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001162 musb_writew(epio, MUSB_RXMAXP, 0);
1163 }
1164
Felipe Balbi550a7372008-07-24 12:27:36 +03001165 /* abort all pending DMA and requests */
1166 nuke(musb_ep, -ESHUTDOWN);
1167
Tal Shorer607fb0f2016-04-25 15:53:29 -05001168 musb_ep->desc = NULL;
1169 musb_ep->end_point.desc = NULL;
1170
Felipe Balbi550a7372008-07-24 12:27:36 +03001171 schedule_work(&musb->irq_work);
1172
1173 spin_unlock_irqrestore(&(musb->lock), flags);
1174
Bin Liub99d3652016-06-30 12:12:22 -05001175 musb_dbg(musb, "%s", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001176
1177 return status;
1178}
1179
1180/*
1181 * Allocate a request for an endpoint.
1182 * Reused by ep0 code.
1183 */
1184struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1185{
1186 struct musb_ep *musb_ep = to_musb_ep(ep);
1187 struct musb_request *request = NULL;
1188
1189 request = kzalloc(sizeof *request, gfp_flags);
Bin Liub99d3652016-06-30 12:12:22 -05001190 if (!request)
Felipe Balbi0607f862010-12-01 11:03:54 +02001191 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001192
Felipe Balbi0607f862010-12-01 11:03:54 +02001193 request->request.dma = DMA_ADDR_INVALID;
1194 request->epnum = musb_ep->current_epnum;
1195 request->ep = musb_ep;
1196
Felipe Balbi550a7372008-07-24 12:27:36 +03001197 return &request->request;
1198}
1199
1200/*
1201 * Free a request
1202 * Reused by ep0 code.
1203 */
1204void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1205{
1206 kfree(to_musb_request(req));
1207}
1208
1209static LIST_HEAD(buffers);
1210
1211struct free_record {
1212 struct list_head list;
1213 struct device *dev;
1214 unsigned bytes;
1215 dma_addr_t dma;
1216};
1217
1218/*
1219 * Context: controller locked, IRQs blocked.
1220 */
Sergei Shtylyova666e3e2010-09-11 13:23:12 -05001221void musb_ep_restart(struct musb *musb, struct musb_request *req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001222{
Bin Liub99d3652016-06-30 12:12:22 -05001223 musb_dbg(musb, "<== %s request %p len %u on hw_ep%d",
Felipe Balbi550a7372008-07-24 12:27:36 +03001224 req->tx ? "TX/IN" : "RX/OUT",
1225 &req->request, req->request.length, req->epnum);
1226
1227 musb_ep_select(musb->mregs, req->epnum);
1228 if (req->tx)
1229 txstate(musb, req);
1230 else
1231 rxstate(musb, req);
1232}
1233
1234static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1235 gfp_t gfp_flags)
1236{
1237 struct musb_ep *musb_ep;
1238 struct musb_request *request;
1239 struct musb *musb;
1240 int status = 0;
1241 unsigned long lockflags;
1242
1243 if (!ep || !req)
1244 return -EINVAL;
1245 if (!req->buf)
1246 return -ENODATA;
1247
1248 musb_ep = to_musb_ep(ep);
1249 musb = musb_ep->musb;
1250
1251 request = to_musb_request(req);
1252 request->musb = musb;
1253
1254 if (request->ep != musb_ep)
1255 return -EINVAL;
1256
Bin Liub99d3652016-06-30 12:12:22 -05001257 musb_dbg(musb, "<== to %s request=%p", ep->name, req);
Felipe Balbi550a7372008-07-24 12:27:36 +03001258
1259 /* request is mine now... */
1260 request->request.actual = 0;
1261 request->request.status = -EINPROGRESS;
1262 request->epnum = musb_ep->current_epnum;
1263 request->tx = musb_ep->is_in;
1264
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +01001265 map_dma_buffer(request, musb, musb_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001266
1267 spin_lock_irqsave(&musb->lock, lockflags);
1268
1269 /* don't queue if the ep is down */
1270 if (!musb_ep->desc) {
Bin Liub99d3652016-06-30 12:12:22 -05001271 musb_dbg(musb, "req %p queued to %s while ep %s",
Felipe Balbi550a7372008-07-24 12:27:36 +03001272 req, ep->name, "disabled");
1273 status = -ESHUTDOWN;
Sebastian Andrzej Siewior23a53d92013-06-19 17:38:15 +02001274 unmap_dma_buffer(request, musb);
1275 goto unlock;
Felipe Balbi550a7372008-07-24 12:27:36 +03001276 }
1277
1278 /* add request to the list */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001279 list_add_tail(&request->list, &musb_ep->req_list);
Felipe Balbi550a7372008-07-24 12:27:36 +03001280
1281 /* it this is the head of the queue, start i/o ... */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001282 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
Felipe Balbi550a7372008-07-24 12:27:36 +03001283 musb_ep_restart(musb, request);
1284
Sebastian Andrzej Siewior23a53d92013-06-19 17:38:15 +02001285unlock:
Felipe Balbi550a7372008-07-24 12:27:36 +03001286 spin_unlock_irqrestore(&musb->lock, lockflags);
1287 return status;
1288}
1289
1290static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1291{
1292 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001293 struct musb_request *req = to_musb_request(request);
1294 struct musb_request *r;
Felipe Balbi550a7372008-07-24 12:27:36 +03001295 unsigned long flags;
1296 int status = 0;
1297 struct musb *musb = musb_ep->musb;
1298
1299 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1300 return -EINVAL;
1301
1302 spin_lock_irqsave(&musb->lock, flags);
1303
1304 list_for_each_entry(r, &musb_ep->req_list, list) {
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001305 if (r == req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001306 break;
1307 }
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001308 if (r != req) {
Bin Liub99d3652016-06-30 12:12:22 -05001309 dev_err(musb->controller, "request %p not queued to %s\n",
1310 request, ep->name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001311 status = -EINVAL;
1312 goto done;
1313 }
1314
1315 /* if the hardware doesn't have the request, easy ... */
Felipe Balbi3d5ad132011-03-22 11:38:49 +02001316 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
Felipe Balbi550a7372008-07-24 12:27:36 +03001317 musb_g_giveback(musb_ep, request, -ECONNRESET);
1318
1319 /* ... else abort the dma transfer ... */
1320 else if (is_dma_capable() && musb_ep->dma) {
1321 struct dma_controller *c = musb->dma_controller;
1322
1323 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1324 if (c->channel_abort)
1325 status = c->channel_abort(musb_ep->dma);
1326 else
1327 status = -EBUSY;
1328 if (status == 0)
1329 musb_g_giveback(musb_ep, request, -ECONNRESET);
1330 } else {
1331 /* NOTE: by sticking to easily tested hardware/driver states,
1332 * we leave counting of in-flight packets imprecise.
1333 */
1334 musb_g_giveback(musb_ep, request, -ECONNRESET);
1335 }
1336
1337done:
1338 spin_unlock_irqrestore(&musb->lock, flags);
1339 return status;
1340}
1341
1342/*
1343 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1344 * data but will queue requests.
1345 *
1346 * exported to ep0 code
1347 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001348static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001349{
1350 struct musb_ep *musb_ep = to_musb_ep(ep);
1351 u8 epnum = musb_ep->current_epnum;
1352 struct musb *musb = musb_ep->musb;
1353 void __iomem *epio = musb->endpoints[epnum].regs;
1354 void __iomem *mbase;
1355 unsigned long flags;
1356 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001357 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001358 int status = 0;
1359
1360 if (!ep)
1361 return -EINVAL;
1362 mbase = musb->mregs;
1363
1364 spin_lock_irqsave(&musb->lock, flags);
1365
1366 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1367 status = -EINVAL;
1368 goto done;
1369 }
1370
1371 musb_ep_select(mbase, epnum);
1372
Felipe Balbiad1adb82011-02-16 12:40:05 +02001373 request = next_request(musb_ep);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001374 if (value) {
1375 if (request) {
Bin Liub99d3652016-06-30 12:12:22 -05001376 musb_dbg(musb, "request in progress, cannot halt %s",
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001377 ep->name);
1378 status = -EAGAIN;
1379 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001380 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001381 /* Cannot portably stall with non-empty FIFO */
1382 if (musb_ep->is_in) {
1383 csr = musb_readw(epio, MUSB_TXCSR);
1384 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Bin Liub99d3652016-06-30 12:12:22 -05001385 musb_dbg(musb, "FIFO busy, cannot halt %s",
1386 ep->name);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001387 status = -EAGAIN;
1388 goto done;
1389 }
1390 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001391 } else
1392 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001393
1394 /* set/clear the stall and toggle bits */
Bin Liub99d3652016-06-30 12:12:22 -05001395 musb_dbg(musb, "%s: %s stall", ep->name, value ? "set" : "clear");
Felipe Balbi550a7372008-07-24 12:27:36 +03001396 if (musb_ep->is_in) {
1397 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001398 csr |= MUSB_TXCSR_P_WZC_BITS
1399 | MUSB_TXCSR_CLRDATATOG;
1400 if (value)
1401 csr |= MUSB_TXCSR_P_SENDSTALL;
1402 else
1403 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1404 | MUSB_TXCSR_P_SENTSTALL);
1405 csr &= ~MUSB_TXCSR_TXPKTRDY;
1406 musb_writew(epio, MUSB_TXCSR, csr);
1407 } else {
1408 csr = musb_readw(epio, MUSB_RXCSR);
1409 csr |= MUSB_RXCSR_P_WZC_BITS
1410 | MUSB_RXCSR_FLUSHFIFO
1411 | MUSB_RXCSR_CLRDATATOG;
1412 if (value)
1413 csr |= MUSB_RXCSR_P_SENDSTALL;
1414 else
1415 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1416 | MUSB_RXCSR_P_SENTSTALL);
1417 musb_writew(epio, MUSB_RXCSR, csr);
1418 }
1419
Felipe Balbi550a7372008-07-24 12:27:36 +03001420 /* maybe start the first request in the queue */
1421 if (!musb_ep->busy && !value && request) {
Bin Liub99d3652016-06-30 12:12:22 -05001422 musb_dbg(musb, "restarting the request");
Felipe Balbi550a7372008-07-24 12:27:36 +03001423 musb_ep_restart(musb, request);
1424 }
1425
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001426done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001427 spin_unlock_irqrestore(&musb->lock, flags);
1428 return status;
1429}
1430
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001431/*
1432 * Sets the halt feature with the clear requests ignored
1433 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001434static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001435{
1436 struct musb_ep *musb_ep = to_musb_ep(ep);
1437
1438 if (!ep)
1439 return -EINVAL;
1440
1441 musb_ep->wedged = 1;
1442
1443 return usb_ep_set_halt(ep);
1444}
1445
Felipe Balbi550a7372008-07-24 12:27:36 +03001446static int musb_gadget_fifo_status(struct usb_ep *ep)
1447{
1448 struct musb_ep *musb_ep = to_musb_ep(ep);
1449 void __iomem *epio = musb_ep->hw_ep->regs;
1450 int retval = -EINVAL;
1451
1452 if (musb_ep->desc && !musb_ep->is_in) {
1453 struct musb *musb = musb_ep->musb;
1454 int epnum = musb_ep->current_epnum;
1455 void __iomem *mbase = musb->mregs;
1456 unsigned long flags;
1457
1458 spin_lock_irqsave(&musb->lock, flags);
1459
1460 musb_ep_select(mbase, epnum);
1461 /* FIXME return zero unless RXPKTRDY is set */
1462 retval = musb_readw(epio, MUSB_RXCOUNT);
1463
1464 spin_unlock_irqrestore(&musb->lock, flags);
1465 }
1466 return retval;
1467}
1468
1469static void musb_gadget_fifo_flush(struct usb_ep *ep)
1470{
1471 struct musb_ep *musb_ep = to_musb_ep(ep);
1472 struct musb *musb = musb_ep->musb;
1473 u8 epnum = musb_ep->current_epnum;
1474 void __iomem *epio = musb->endpoints[epnum].regs;
1475 void __iomem *mbase;
1476 unsigned long flags;
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001477 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03001478
1479 mbase = musb->mregs;
1480
1481 spin_lock_irqsave(&musb->lock, flags);
1482 musb_ep_select(mbase, (u8) epnum);
1483
1484 /* disable interrupts */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001485 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
Felipe Balbi550a7372008-07-24 12:27:36 +03001486
1487 if (musb_ep->is_in) {
1488 csr = musb_readw(epio, MUSB_TXCSR);
1489 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1490 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
Yauheni Kaliuta4858f062011-06-08 17:12:02 +03001491 /*
1492 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1493 * to interrupt current FIFO loading, but not flushing
1494 * the already loaded ones.
1495 */
1496 csr &= ~MUSB_TXCSR_TXPKTRDY;
Felipe Balbi550a7372008-07-24 12:27:36 +03001497 musb_writew(epio, MUSB_TXCSR, csr);
1498 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1499 musb_writew(epio, MUSB_TXCSR, csr);
1500 }
1501 } else {
1502 csr = musb_readw(epio, MUSB_RXCSR);
1503 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1504 musb_writew(epio, MUSB_RXCSR, csr);
1505 musb_writew(epio, MUSB_RXCSR, csr);
1506 }
1507
1508 /* re-enable interrupt */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001509 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001510 spin_unlock_irqrestore(&musb->lock, flags);
1511}
1512
1513static const struct usb_ep_ops musb_ep_ops = {
1514 .enable = musb_gadget_enable,
1515 .disable = musb_gadget_disable,
1516 .alloc_request = musb_alloc_request,
1517 .free_request = musb_free_request,
1518 .queue = musb_gadget_queue,
1519 .dequeue = musb_gadget_dequeue,
1520 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001521 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001522 .fifo_status = musb_gadget_fifo_status,
1523 .fifo_flush = musb_gadget_fifo_flush
1524};
1525
1526/* ----------------------------------------------------------------------- */
1527
1528static int musb_gadget_get_frame(struct usb_gadget *gadget)
1529{
1530 struct musb *musb = gadget_to_musb(gadget);
1531
1532 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1533}
1534
1535static int musb_gadget_wakeup(struct usb_gadget *gadget)
1536{
1537 struct musb *musb = gadget_to_musb(gadget);
1538 void __iomem *mregs = musb->mregs;
1539 unsigned long flags;
1540 int status = -EINVAL;
1541 u8 power, devctl;
1542 int retries;
1543
1544 spin_lock_irqsave(&musb->lock, flags);
1545
Antoine Tenarte47d9252014-10-30 18:41:13 +01001546 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001547 case OTG_STATE_B_PERIPHERAL:
1548 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1549 * that's part of the standard usb 1.1 state machine, and
1550 * doesn't affect OTG transitions.
1551 */
1552 if (musb->may_wakeup && musb->is_suspended)
1553 break;
1554 goto done;
1555 case OTG_STATE_B_IDLE:
1556 /* Start SRP ... OTG not required. */
1557 devctl = musb_readb(mregs, MUSB_DEVCTL);
Bin Liub99d3652016-06-30 12:12:22 -05001558 musb_dbg(musb, "Sending SRP: devctl: %02x", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001559 devctl |= MUSB_DEVCTL_SESSION;
1560 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1561 devctl = musb_readb(mregs, MUSB_DEVCTL);
1562 retries = 100;
1563 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1564 devctl = musb_readb(mregs, MUSB_DEVCTL);
1565 if (retries-- < 1)
1566 break;
1567 }
1568 retries = 10000;
1569 while (devctl & MUSB_DEVCTL_SESSION) {
1570 devctl = musb_readb(mregs, MUSB_DEVCTL);
1571 if (retries-- < 1)
1572 break;
1573 }
1574
Hema HK86205432011-03-22 16:54:22 +05301575 spin_unlock_irqrestore(&musb->lock, flags);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001576 otg_start_srp(musb->xceiv->otg);
Hema HK86205432011-03-22 16:54:22 +05301577 spin_lock_irqsave(&musb->lock, flags);
1578
Felipe Balbi550a7372008-07-24 12:27:36 +03001579 /* Block idling for at least 1s */
1580 musb_platform_try_idle(musb,
1581 jiffies + msecs_to_jiffies(1 * HZ));
1582
1583 status = 0;
1584 goto done;
1585 default:
Bin Liub99d3652016-06-30 12:12:22 -05001586 musb_dbg(musb, "Unhandled wake: %s",
Antoine Tenarte47d9252014-10-30 18:41:13 +01001587 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001588 goto done;
1589 }
1590
1591 status = 0;
1592
1593 power = musb_readb(mregs, MUSB_POWER);
1594 power |= MUSB_POWER_RESUME;
1595 musb_writeb(mregs, MUSB_POWER, power);
Bin Liub99d3652016-06-30 12:12:22 -05001596 musb_dbg(musb, "issue wakeup");
Felipe Balbi550a7372008-07-24 12:27:36 +03001597
1598 /* FIXME do this next chunk in a timer callback, no udelay */
1599 mdelay(2);
1600
1601 power = musb_readb(mregs, MUSB_POWER);
1602 power &= ~MUSB_POWER_RESUME;
1603 musb_writeb(mregs, MUSB_POWER, power);
1604done:
1605 spin_unlock_irqrestore(&musb->lock, flags);
1606 return status;
1607}
1608
1609static int
1610musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1611{
Peter Chendadac982015-01-28 16:32:41 +08001612 gadget->is_selfpowered = !!is_selfpowered;
Felipe Balbi550a7372008-07-24 12:27:36 +03001613 return 0;
1614}
1615
1616static void musb_pullup(struct musb *musb, int is_on)
1617{
1618 u8 power;
1619
1620 power = musb_readb(musb->mregs, MUSB_POWER);
1621 if (is_on)
1622 power |= MUSB_POWER_SOFTCONN;
1623 else
1624 power &= ~MUSB_POWER_SOFTCONN;
1625
1626 /* FIXME if on, HdrcStart; if off, HdrcStop */
1627
Bin Liub99d3652016-06-30 12:12:22 -05001628 musb_dbg(musb, "gadget D+ pullup %s",
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001629 is_on ? "on" : "off");
Felipe Balbi550a7372008-07-24 12:27:36 +03001630 musb_writeb(musb->mregs, MUSB_POWER, power);
1631}
1632
1633#if 0
1634static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1635{
Bin Liub99d3652016-06-30 12:12:22 -05001636 musb_dbg(musb, "<= %s =>\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001637
1638 /*
1639 * FIXME iff driver's softconnect flag is set (as it is during probe,
1640 * though that can clear it), just musb_pullup().
1641 */
1642
1643 return -EINVAL;
1644}
1645#endif
1646
1647static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1648{
1649 struct musb *musb = gadget_to_musb(gadget);
1650
David Brownell84e250f2009-03-31 12:30:04 -07001651 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001652 return -EOPNOTSUPP;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001653 return usb_phy_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001654}
1655
Tony Lindgren517baff2016-05-31 10:05:14 -05001656static void musb_gadget_work(struct work_struct *work)
1657{
1658 struct musb *musb;
1659 unsigned long flags;
1660
1661 musb = container_of(work, struct musb, gadget_work.work);
1662 pm_runtime_get_sync(musb->controller);
1663 spin_lock_irqsave(&musb->lock, flags);
1664 musb_pullup(musb, musb->softconnect);
1665 spin_unlock_irqrestore(&musb->lock, flags);
1666 pm_runtime_mark_last_busy(musb->controller);
1667 pm_runtime_put_autosuspend(musb->controller);
1668}
1669
Felipe Balbi550a7372008-07-24 12:27:36 +03001670static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1671{
1672 struct musb *musb = gadget_to_musb(gadget);
1673 unsigned long flags;
1674
1675 is_on = !!is_on;
1676
1677 /* NOTE: this assumes we are sensing vbus; we'd rather
1678 * not pullup unless the B-session is active.
1679 */
1680 spin_lock_irqsave(&musb->lock, flags);
1681 if (is_on != musb->softconnect) {
1682 musb->softconnect = is_on;
Tony Lindgren517baff2016-05-31 10:05:14 -05001683 schedule_delayed_work(&musb->gadget_work, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001684 }
1685 spin_unlock_irqrestore(&musb->lock, flags);
John Stultz93e098a2011-07-20 17:09:34 -07001686
Felipe Balbi550a7372008-07-24 12:27:36 +03001687 return 0;
1688}
1689
Robert Baldyga26b8aa42015-08-06 14:11:15 +02001690#ifdef CONFIG_BLACKFIN
1691static struct usb_ep *musb_match_ep(struct usb_gadget *g,
1692 struct usb_endpoint_descriptor *desc,
1693 struct usb_ss_ep_comp_descriptor *ep_comp)
1694{
1695 struct usb_ep *ep = NULL;
1696
1697 switch (usb_endpoint_type(desc)) {
1698 case USB_ENDPOINT_XFER_ISOC:
1699 case USB_ENDPOINT_XFER_BULK:
1700 if (usb_endpoint_dir_in(desc))
1701 ep = gadget_find_ep_by_name(g, "ep5in");
1702 else
1703 ep = gadget_find_ep_by_name(g, "ep6out");
1704 break;
1705 case USB_ENDPOINT_XFER_INT:
1706 if (usb_endpoint_dir_in(desc))
1707 ep = gadget_find_ep_by_name(g, "ep1in");
1708 else
1709 ep = gadget_find_ep_by_name(g, "ep2out");
1710 break;
1711 default:
Robert Baldyga2f3cc242015-08-07 14:13:34 +02001712 break;
Robert Baldyga26b8aa42015-08-06 14:11:15 +02001713 }
1714
1715 if (ep && usb_gadget_ep_match_desc(g, ep, desc, ep_comp))
1716 return ep;
1717
1718 return NULL;
1719}
1720#else
1721#define musb_match_ep NULL
1722#endif
1723
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001724static int musb_gadget_start(struct usb_gadget *g,
1725 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001726static int musb_gadget_stop(struct usb_gadget *g);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001727
Felipe Balbi550a7372008-07-24 12:27:36 +03001728static const struct usb_gadget_ops musb_gadget_operations = {
1729 .get_frame = musb_gadget_get_frame,
1730 .wakeup = musb_gadget_wakeup,
1731 .set_selfpowered = musb_gadget_set_self_powered,
1732 /* .vbus_session = musb_gadget_vbus_session, */
1733 .vbus_draw = musb_gadget_vbus_draw,
1734 .pullup = musb_gadget_pullup,
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001735 .udc_start = musb_gadget_start,
1736 .udc_stop = musb_gadget_stop,
Robert Baldyga26b8aa42015-08-06 14:11:15 +02001737 .match_ep = musb_match_ep,
Felipe Balbi550a7372008-07-24 12:27:36 +03001738};
1739
1740/* ----------------------------------------------------------------------- */
1741
1742/* Registration */
1743
1744/* Only this registration code "knows" the rule (from USB standards)
1745 * about there being only one external upstream port. It assumes
1746 * all peripheral ports are external...
1747 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001748
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001749static void
Felipe Balbi550a7372008-07-24 12:27:36 +03001750init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1751{
1752 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1753
1754 memset(ep, 0, sizeof *ep);
1755
1756 ep->current_epnum = epnum;
1757 ep->musb = musb;
1758 ep->hw_ep = hw_ep;
1759 ep->is_in = is_in;
1760
1761 INIT_LIST_HEAD(&ep->req_list);
1762
1763 sprintf(ep->name, "ep%d%s", epnum,
1764 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1765 is_in ? "in" : "out"));
1766 ep->end_point.name = ep->name;
1767 INIT_LIST_HEAD(&ep->end_point.ep_list);
1768 if (!epnum) {
Robert Baldygae117e742013-12-13 12:23:38 +01001769 usb_ep_set_maxpacket_limit(&ep->end_point, 64);
Robert Baldyga85019552015-07-31 16:00:46 +02001770 ep->end_point.caps.type_control = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001771 ep->end_point.ops = &musb_g_ep0_ops;
1772 musb->g.ep0 = &ep->end_point;
1773 } else {
1774 if (is_in)
Robert Baldygae117e742013-12-13 12:23:38 +01001775 usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_tx);
Felipe Balbi550a7372008-07-24 12:27:36 +03001776 else
Robert Baldygae117e742013-12-13 12:23:38 +01001777 usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_rx);
Robert Baldyga85019552015-07-31 16:00:46 +02001778 ep->end_point.caps.type_iso = true;
1779 ep->end_point.caps.type_bulk = true;
1780 ep->end_point.caps.type_int = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001781 ep->end_point.ops = &musb_ep_ops;
1782 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1783 }
Robert Baldyga85019552015-07-31 16:00:46 +02001784
1785 if (!epnum || hw_ep->is_shared_fifo) {
1786 ep->end_point.caps.dir_in = true;
1787 ep->end_point.caps.dir_out = true;
1788 } else if (is_in)
1789 ep->end_point.caps.dir_in = true;
1790 else
1791 ep->end_point.caps.dir_out = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001792}
1793
1794/*
1795 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1796 * to the rest of the driver state.
1797 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001798static inline void musb_g_init_endpoints(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001799{
1800 u8 epnum;
1801 struct musb_hw_ep *hw_ep;
1802 unsigned count = 0;
1803
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001804 /* initialize endpoint list just once */
Felipe Balbi550a7372008-07-24 12:27:36 +03001805 INIT_LIST_HEAD(&(musb->g.ep_list));
1806
1807 for (epnum = 0, hw_ep = musb->endpoints;
1808 epnum < musb->nr_endpoints;
1809 epnum++, hw_ep++) {
1810 if (hw_ep->is_shared_fifo /* || !epnum */) {
1811 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1812 count++;
1813 } else {
1814 if (hw_ep->max_packet_sz_tx) {
1815 init_peripheral_ep(musb, &hw_ep->ep_in,
1816 epnum, 1);
1817 count++;
1818 }
1819 if (hw_ep->max_packet_sz_rx) {
1820 init_peripheral_ep(musb, &hw_ep->ep_out,
1821 epnum, 0);
1822 count++;
1823 }
1824 }
1825 }
1826}
1827
1828/* called once during driver setup to initialize and link into
1829 * the driver model; memory is zeroed.
1830 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001831int musb_gadget_setup(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001832{
1833 int status;
1834
1835 /* REVISIT minor race: if (erroneously) setting up two
1836 * musb peripherals at the same time, only the bus lock
1837 * is probably held.
1838 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001839
1840 musb->g.ops = &musb_gadget_operations;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001841 musb->g.max_speed = USB_SPEED_HIGH;
Felipe Balbi550a7372008-07-24 12:27:36 +03001842 musb->g.speed = USB_SPEED_UNKNOWN;
1843
Bin Liu1374a4302013-09-17 12:43:13 -05001844 MUSB_DEV_MODE(musb);
1845 musb->xceiv->otg->default_a = 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001846 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Bin Liu1374a4302013-09-17 12:43:13 -05001847
Felipe Balbi550a7372008-07-24 12:27:36 +03001848 /* this "gadget" abstracts/virtualizes the controller */
Felipe Balbi550a7372008-07-24 12:27:36 +03001849 musb->g.name = musb_driver_name;
Apelete Seketelifd3923a2013-11-19 23:18:20 +01001850#if IS_ENABLED(CONFIG_USB_MUSB_DUAL_ROLE)
Felipe Balbi032ec492011-11-24 15:46:26 +02001851 musb->g.is_otg = 1;
Apelete Seketelifd3923a2013-11-19 23:18:20 +01001852#elif IS_ENABLED(CONFIG_USB_MUSB_GADGET)
1853 musb->g.is_otg = 0;
1854#endif
Tony Lindgren517baff2016-05-31 10:05:14 -05001855 INIT_DELAYED_WORK(&musb->gadget_work, musb_gadget_work);
Felipe Balbi550a7372008-07-24 12:27:36 +03001856 musb_g_init_endpoints(musb);
1857
1858 musb->is_active = 0;
1859 musb_platform_try_idle(musb, 0);
1860
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001861 status = usb_add_gadget_udc(musb->controller, &musb->g);
1862 if (status)
1863 goto err;
1864
1865 return 0;
1866err:
Sebastian Andrzej Siewior6193d692011-08-10 11:01:57 +02001867 musb->g.dev.parent = NULL;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001868 device_unregister(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001869 return status;
1870}
1871
1872void musb_gadget_cleanup(struct musb *musb)
1873{
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02001874 if (musb->port_mode == MUSB_PORT_MODE_HOST)
1875 return;
Tony Lindgren517baff2016-05-31 10:05:14 -05001876
1877 cancel_delayed_work_sync(&musb->gadget_work);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001878 usb_del_gadget_udc(&musb->g);
Felipe Balbi550a7372008-07-24 12:27:36 +03001879}
1880
1881/*
1882 * Register the gadget driver. Used by gadget drivers when
1883 * registering themselves with the controller.
1884 *
1885 * -EINVAL something went wrong (not driver)
1886 * -EBUSY another gadget is already using the controller
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001887 * -ENOMEM no memory to perform the operation
Felipe Balbi550a7372008-07-24 12:27:36 +03001888 *
1889 * @param driver the gadget driver
1890 * @return <0 if error, 0 if everything is fine
1891 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001892static int musb_gadget_start(struct usb_gadget *g,
1893 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03001894{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001895 struct musb *musb = gadget_to_musb(g);
Heikki Krogerusd445b6d2012-02-13 13:24:15 +02001896 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001897 unsigned long flags;
Felipe Balbi032ec492011-11-24 15:46:26 +02001898 int retval = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001899
Felipe Balbi032ec492011-11-24 15:46:26 +02001900 if (driver->max_speed < USB_SPEED_HIGH) {
1901 retval = -EINVAL;
1902 goto err;
1903 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001904
Hema HK7acc6192011-02-28 14:19:34 +05301905 pm_runtime_get_sync(musb->controller);
1906
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001907 musb->softconnect = 0;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001908 musb->gadget_driver = driver;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001909
1910 spin_lock_irqsave(&musb->lock, flags);
Greg Kroah-Hartman43e699c2013-10-14 13:06:15 -07001911 musb->is_active = 1;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001912
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001913 otg_set_peripheral(otg, &musb->g);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001914 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001915 spin_unlock_irqrestore(&musb->lock, flags);
1916
Sebastian Andrzej Siewior001dd842013-10-11 10:38:13 +02001917 musb_start(musb);
1918
Felipe Balbi032ec492011-11-24 15:46:26 +02001919 /* REVISIT: funcall to other code, which also
1920 * handles power budgeting ... this way also
1921 * ensures HdrcStart is indirectly called.
1922 */
Grazvydas Ignotasb65ae0f2013-03-24 17:36:55 +02001923 if (musb->xceiv->last_event == USB_EVENT_ID)
1924 musb_platform_set_vbus(musb, 1);
Felipe Balbi032ec492011-11-24 15:46:26 +02001925
Tony Lindgren30647212016-05-31 10:05:13 -05001926 pm_runtime_mark_last_busy(musb->controller);
1927 pm_runtime_put_autosuspend(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001928
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001929 return 0;
1930
Felipe Balbi032ec492011-11-24 15:46:26 +02001931err:
Felipe Balbi550a7372008-07-24 12:27:36 +03001932 return retval;
1933}
Felipe Balbi550a7372008-07-24 12:27:36 +03001934
Felipe Balbi550a7372008-07-24 12:27:36 +03001935/*
1936 * Unregister the gadget driver. Used by gadget drivers when
1937 * unregistering themselves from the controller.
1938 *
1939 * @param driver the gadget driver to unregister
1940 */
Felipe Balbi22835b82014-10-17 12:05:12 -05001941static int musb_gadget_stop(struct usb_gadget *g)
Felipe Balbi550a7372008-07-24 12:27:36 +03001942{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001943 struct musb *musb = gadget_to_musb(g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001944 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03001945
Tony Lindgren30647212016-05-31 10:05:13 -05001946 pm_runtime_get_sync(musb->controller);
Hema HK7acc6192011-02-28 14:19:34 +05301947
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001948 /*
1949 * REVISIT always use otg_set_peripheral() here too;
Felipe Balbi550a7372008-07-24 12:27:36 +03001950 * this needs to shut down the OTG engine.
1951 */
1952
1953 spin_lock_irqsave(&musb->lock, flags);
1954
Felipe Balbi550a7372008-07-24 12:27:36 +03001955 musb_hnp_stop(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001956
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001957 (void) musb_gadget_vbus_draw(&musb->g, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001958
Antoine Tenarte47d9252014-10-30 18:41:13 +01001959 musb->xceiv->otg->state = OTG_STATE_UNDEFINED;
Felipe Balbid5638fc2015-02-02 17:14:12 -06001960 musb_stop(musb);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001961 otg_set_peripheral(musb->xceiv->otg, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001962
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001963 musb->is_active = 0;
Grazvydas Ignotase21de102013-03-10 02:49:14 +02001964 musb->gadget_driver = NULL;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001965 musb_platform_try_idle(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001966 spin_unlock_irqrestore(&musb->lock, flags);
1967
Felipe Balbi032ec492011-11-24 15:46:26 +02001968 /*
1969 * FIXME we need to be able to register another
1970 * gadget driver here and have everything work;
1971 * that currently misbehaves.
1972 */
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001973
Tony Lindgren7099dbc2016-05-31 10:05:11 -05001974 pm_runtime_mark_last_busy(musb->controller);
1975 pm_runtime_put_autosuspend(musb->controller);
Hema HK7acc6192011-02-28 14:19:34 +05301976
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001977 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001978}
Felipe Balbi550a7372008-07-24 12:27:36 +03001979
1980/* ----------------------------------------------------------------------- */
1981
1982/* lifecycle operations called through plat_uds.c */
1983
1984void musb_g_resume(struct musb *musb)
1985{
1986 musb->is_suspended = 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001987 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001988 case OTG_STATE_B_IDLE:
1989 break;
1990 case OTG_STATE_B_WAIT_ACON:
1991 case OTG_STATE_B_PERIPHERAL:
1992 musb->is_active = 1;
1993 if (musb->gadget_driver && musb->gadget_driver->resume) {
1994 spin_unlock(&musb->lock);
1995 musb->gadget_driver->resume(&musb->g);
1996 spin_lock(&musb->lock);
1997 }
1998 break;
1999 default:
2000 WARNING("unhandled RESUME transition (%s)\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002001 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002002 }
2003}
2004
2005/* called when SOF packets stop for 3+ msec */
2006void musb_g_suspend(struct musb *musb)
2007{
2008 u8 devctl;
2009
2010 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Bin Liub99d3652016-06-30 12:12:22 -05002011 musb_dbg(musb, "musb_g_suspend: devctl %02x", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002012
Antoine Tenarte47d9252014-10-30 18:41:13 +01002013 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002014 case OTG_STATE_B_IDLE:
2015 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
Antoine Tenarte47d9252014-10-30 18:41:13 +01002016 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002017 break;
2018 case OTG_STATE_B_PERIPHERAL:
2019 musb->is_suspended = 1;
2020 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2021 spin_unlock(&musb->lock);
2022 musb->gadget_driver->suspend(&musb->g);
2023 spin_lock(&musb->lock);
2024 }
2025 break;
2026 default:
2027 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2028 * A_PERIPHERAL may need care too
2029 */
Bin Liub99d3652016-06-30 12:12:22 -05002030 WARNING("unhandled SUSPEND transition (%s)",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002031 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002032 }
2033}
2034
2035/* Called during SRP */
2036void musb_g_wakeup(struct musb *musb)
2037{
2038 musb_gadget_wakeup(&musb->g);
2039}
2040
2041/* called when VBUS drops below session threshold, and in other cases */
2042void musb_g_disconnect(struct musb *musb)
2043{
2044 void __iomem *mregs = musb->mregs;
2045 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2046
Bin Liub99d3652016-06-30 12:12:22 -05002047 musb_dbg(musb, "musb_g_disconnect: devctl %02x", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002048
2049 /* clear HR */
2050 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2051
2052 /* don't draw vbus until new b-default session */
2053 (void) musb_gadget_vbus_draw(&musb->g, 0);
2054
2055 musb->g.speed = USB_SPEED_UNKNOWN;
2056 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2057 spin_unlock(&musb->lock);
2058 musb->gadget_driver->disconnect(&musb->g);
2059 spin_lock(&musb->lock);
2060 }
2061
Antoine Tenarte47d9252014-10-30 18:41:13 +01002062 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002063 default:
Bin Liub99d3652016-06-30 12:12:22 -05002064 musb_dbg(musb, "Unhandled disconnect %s, setting a_idle",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002065 usb_otg_state_string(musb->xceiv->otg->state));
2066 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07002067 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002068 break;
2069 case OTG_STATE_A_PERIPHERAL:
Antoine Tenarte47d9252014-10-30 18:41:13 +01002070 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
David Brownellab983f2a2009-03-31 12:35:09 -07002071 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002072 break;
2073 case OTG_STATE_B_WAIT_ACON:
2074 case OTG_STATE_B_HOST:
Felipe Balbi550a7372008-07-24 12:27:36 +03002075 case OTG_STATE_B_PERIPHERAL:
2076 case OTG_STATE_B_IDLE:
Antoine Tenarte47d9252014-10-30 18:41:13 +01002077 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002078 break;
2079 case OTG_STATE_B_SRP_INIT:
2080 break;
2081 }
2082
2083 musb->is_active = 0;
2084}
2085
2086void musb_g_reset(struct musb *musb)
2087__releases(musb->lock)
2088__acquires(musb->lock)
2089{
2090 void __iomem *mbase = musb->mregs;
2091 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2092 u8 power;
2093
Bin Liub99d3652016-06-30 12:12:22 -05002094 musb_dbg(musb, "<== %s driver '%s'",
Felipe Balbi550a7372008-07-24 12:27:36 +03002095 (devctl & MUSB_DEVCTL_BDEVICE)
2096 ? "B-Device" : "A-Device",
Felipe Balbi550a7372008-07-24 12:27:36 +03002097 musb->gadget_driver
2098 ? musb->gadget_driver->driver.name
2099 : NULL
2100 );
2101
Felipe Balbi1189f7f2014-11-06 14:27:54 +08002102 /* report reset, if we didn't already (flushing EP state) */
2103 if (musb->gadget_driver && musb->g.speed != USB_SPEED_UNKNOWN) {
2104 spin_unlock(&musb->lock);
2105 usb_gadget_udc_reset(&musb->g, musb->gadget_driver);
2106 spin_lock(&musb->lock);
2107 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002108
2109 /* clear HR */
2110 else if (devctl & MUSB_DEVCTL_HR)
2111 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2112
2113
2114 /* what speed did we negotiate? */
2115 power = musb_readb(mbase, MUSB_POWER);
2116 musb->g.speed = (power & MUSB_POWER_HSMODE)
2117 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2118
2119 /* start in USB_STATE_DEFAULT */
2120 musb->is_active = 1;
2121 musb->is_suspended = 0;
2122 MUSB_DEV_MODE(musb);
2123 musb->address = 0;
2124 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2125
2126 musb->may_wakeup = 0;
2127 musb->g.b_hnp_enable = 0;
2128 musb->g.a_alt_hnp_support = 0;
2129 musb->g.a_hnp_support = 0;
Robert Baldygaca1023c2015-07-28 07:20:00 +02002130 musb->g.quirk_zlp_not_supp = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002131
2132 /* Normal reset, as B-Device;
2133 * or else after HNP, as A-Device
2134 */
Apelete Seketeli23db9fd2013-12-19 21:42:27 +01002135 if (!musb->g.is_otg) {
2136 /* USB device controllers that are not OTG compatible
2137 * may not have DEVCTL register in silicon.
2138 * In that case, do not rely on devctl for setting
2139 * peripheral mode.
2140 */
Antoine Tenarte47d9252014-10-30 18:41:13 +01002141 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Apelete Seketeli23db9fd2013-12-19 21:42:27 +01002142 musb->g.is_a_peripheral = 0;
2143 } else if (devctl & MUSB_DEVCTL_BDEVICE) {
Antoine Tenarte47d9252014-10-30 18:41:13 +01002144 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002145 musb->g.is_a_peripheral = 0;
Felipe Balbi032ec492011-11-24 15:46:26 +02002146 } else {
Antoine Tenarte47d9252014-10-30 18:41:13 +01002147 musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002148 musb->g.is_a_peripheral = 1;
Felipe Balbi032ec492011-11-24 15:46:26 +02002149 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002150
2151 /* start with default limits on VBUS power draw */
Felipe Balbi032ec492011-11-24 15:46:26 +02002152 (void) musb_gadget_vbus_draw(&musb->g, 8);
Felipe Balbi550a7372008-07-24 12:27:36 +03002153}