blob: 4042ea017985de56346d0ac1dd070f33db079d77 [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"
Bin Liufc780032016-06-30 12:12:27 -050047#include "musb_trace.h"
Felipe Balbi550a7372008-07-24 12:27:36 +030048
49
Felipe Balbi550a7372008-07-24 12:27:36 +030050/* ----------------------------------------------------------------------- */
51
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010052#define is_buffer_mapped(req) (is_dma_capable() && \
53 (req->map_state != UN_MAPPED))
54
Hema Kalliguddi92d27112010-11-15 04:24:01 -060055/* Maps the buffer to dma */
56
57static inline void map_dma_buffer(struct musb_request *request,
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010058 struct musb *musb, struct musb_ep *musb_ep)
Hema Kalliguddi92d27112010-11-15 04:24:01 -060059{
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +010060 int compatible = true;
61 struct dma_controller *dma = musb->dma_controller;
62
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010063 request->map_state = UN_MAPPED;
64
65 if (!is_dma_capable() || !musb_ep->dma)
66 return;
67
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +010068 /* Check if DMA engine can handle this request.
69 * DMA code must reject the USB request explicitly.
70 * Default behaviour is to map the request.
71 */
72 if (dma->is_compatible)
73 compatible = dma->is_compatible(musb_ep->dma,
74 musb_ep->packet_sz, request->request.buf,
75 request->request.length);
76 if (!compatible)
77 return;
78
Hema Kalliguddi92d27112010-11-15 04:24:01 -060079 if (request->request.dma == DMA_ADDR_INVALID) {
Sebastian Andrzej Siewior7b360f42013-08-13 19:35:43 +020080 dma_addr_t dma_addr;
81 int ret;
82
83 dma_addr = dma_map_single(
Hema Kalliguddi92d27112010-11-15 04:24:01 -060084 musb->controller,
85 request->request.buf,
86 request->request.length,
87 request->tx
88 ? DMA_TO_DEVICE
89 : DMA_FROM_DEVICE);
Sebastian Andrzej Siewior7b360f42013-08-13 19:35:43 +020090 ret = dma_mapping_error(musb->controller, dma_addr);
91 if (ret)
92 return;
93
94 request->request.dma = dma_addr;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010095 request->map_state = MUSB_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -060096 } else {
97 dma_sync_single_for_device(musb->controller,
98 request->request.dma,
99 request->request.length,
100 request->tx
101 ? DMA_TO_DEVICE
102 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100103 request->map_state = PRE_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600104 }
105}
106
107/* Unmap the buffer from dma and maps it back to cpu */
108static inline void unmap_dma_buffer(struct musb_request *request,
109 struct musb *musb)
110{
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +0530111 struct musb_ep *musb_ep = request->ep;
112
113 if (!is_buffer_mapped(request) || !musb_ep->dma)
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100114 return;
115
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600116 if (request->request.dma == DMA_ADDR_INVALID) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300117 dev_vdbg(musb->controller,
118 "not unmapping a never mapped buffer\n");
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600119 return;
120 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100121 if (request->map_state == MUSB_MAPPED) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600122 dma_unmap_single(musb->controller,
123 request->request.dma,
124 request->request.length,
125 request->tx
126 ? DMA_TO_DEVICE
127 : DMA_FROM_DEVICE);
128 request->request.dma = DMA_ADDR_INVALID;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100129 } else { /* PRE_MAPPED */
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600130 dma_sync_single_for_cpu(musb->controller,
131 request->request.dma,
132 request->request.length,
133 request->tx
134 ? DMA_TO_DEVICE
135 : DMA_FROM_DEVICE);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600136 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100137 request->map_state = UN_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600138}
139
Felipe Balbi550a7372008-07-24 12:27:36 +0300140/*
141 * Immediately complete a request.
142 *
143 * @param request the request to complete
144 * @param status the status to complete the request with
145 * Context: controller locked, IRQs blocked.
146 */
147void musb_g_giveback(
148 struct musb_ep *ep,
149 struct usb_request *request,
150 int status)
151__releases(ep->musb->lock)
152__acquires(ep->musb->lock)
153{
154 struct musb_request *req;
155 struct musb *musb;
156 int busy = ep->busy;
157
158 req = to_musb_request(request);
159
Felipe Balbiad1adb82011-02-16 12:40:05 +0200160 list_del(&req->list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300161 if (req->request.status == -EINPROGRESS)
162 req->request.status = status;
163 musb = req->musb;
164
165 ep->busy = 1;
166 spin_unlock(&musb->lock);
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +0530167
168 if (!dma_mapping_error(&musb->g.dev, request->dma))
169 unmap_dma_buffer(req, musb);
170
Bin Liufc780032016-06-30 12:12:27 -0500171 trace_musb_req_gb(req);
Michal Sojka304f7e52014-09-24 22:43:19 +0200172 usb_gadget_giveback_request(&req->ep->end_point, &req->request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300173 spin_lock(&musb->lock);
174 ep->busy = busy;
175}
176
177/* ----------------------------------------------------------------------- */
178
179/*
180 * Abort requests queued to an endpoint using the status. Synchronous.
181 * caller locked controller and blocked irqs, and selected this ep.
182 */
183static void nuke(struct musb_ep *ep, const int status)
184{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300185 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300186 struct musb_request *req = NULL;
187 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
188
189 ep->busy = 1;
190
191 if (is_dma_capable() && ep->dma) {
192 struct dma_controller *c = ep->musb->dma_controller;
193 int value;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700194
Felipe Balbi550a7372008-07-24 12:27:36 +0300195 if (ep->is_in) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700196 /*
197 * The programming guide says that we must not clear
198 * the DMAMODE bit before DMAENAB, so we only
199 * clear it in the second write...
200 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300201 musb_writew(epio, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700202 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
Felipe Balbi550a7372008-07-24 12:27:36 +0300203 musb_writew(epio, MUSB_TXCSR,
204 0 | MUSB_TXCSR_FLUSHFIFO);
205 } else {
206 musb_writew(epio, MUSB_RXCSR,
207 0 | MUSB_RXCSR_FLUSHFIFO);
208 musb_writew(epio, MUSB_RXCSR,
209 0 | MUSB_RXCSR_FLUSHFIFO);
210 }
211
212 value = c->channel_abort(ep->dma);
Bin Liub99d3652016-06-30 12:12:22 -0500213 musb_dbg(musb, "%s: abort DMA --> %d", ep->name, value);
Felipe Balbi550a7372008-07-24 12:27:36 +0300214 c->channel_release(ep->dma);
215 ep->dma = NULL;
216 }
217
Felipe Balbiad1adb82011-02-16 12:40:05 +0200218 while (!list_empty(&ep->req_list)) {
219 req = list_first_entry(&ep->req_list, struct musb_request, list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300220 musb_g_giveback(ep, &req->request, status);
221 }
222}
223
224/* ----------------------------------------------------------------------- */
225
226/* Data transfers - pure PIO, pure DMA, or mixed mode */
227
228/*
229 * This assumes the separate CPPI engine is responding to DMA requests
230 * from the usb core ... sequenced a bit differently from mentor dma.
231 */
232
233static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
234{
235 if (can_bulk_split(musb, ep->type))
236 return ep->hw_ep->max_packet_sz_tx;
237 else
238 return ep->packet_sz;
239}
240
Felipe Balbi550a7372008-07-24 12:27:36 +0300241/*
242 * An endpoint is transmitting data. This can be called either from
243 * the IRQ routine or from ep.queue() to kickstart a request on an
244 * endpoint.
245 *
246 * Context: controller locked, IRQs blocked, endpoint selected
247 */
248static void txstate(struct musb *musb, struct musb_request *req)
249{
250 u8 epnum = req->epnum;
251 struct musb_ep *musb_ep;
252 void __iomem *epio = musb->endpoints[epnum].regs;
253 struct usb_request *request;
254 u16 fifo_count = 0, csr;
255 int use_dma = 0;
256
257 musb_ep = req->ep;
258
Vikram Panditaabf710e2012-05-18 13:48:04 -0700259 /* Check if EP is disabled */
260 if (!musb_ep->desc) {
Bin Liub99d3652016-06-30 12:12:22 -0500261 musb_dbg(musb, "ep:%s disabled - ignore request",
Vikram Panditaabf710e2012-05-18 13:48:04 -0700262 musb_ep->end_point.name);
263 return;
264 }
265
Felipe Balbi550a7372008-07-24 12:27:36 +0300266 /* we shouldn't get here while DMA is active ... but we do ... */
267 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Bin Liub99d3652016-06-30 12:12:22 -0500268 musb_dbg(musb, "dma pending...");
Felipe Balbi550a7372008-07-24 12:27:36 +0300269 return;
270 }
271
272 /* read TXCSR before */
273 csr = musb_readw(epio, MUSB_TXCSR);
274
275 request = &req->request;
276 fifo_count = min(max_ep_writesize(musb, musb_ep),
277 (int)(request->length - request->actual));
278
279 if (csr & MUSB_TXCSR_TXPKTRDY) {
Bin Liub99d3652016-06-30 12:12:22 -0500280 musb_dbg(musb, "%s old packet still ready , txcsr %03x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300281 musb_ep->end_point.name, csr);
282 return;
283 }
284
285 if (csr & MUSB_TXCSR_P_SENDSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -0500286 musb_dbg(musb, "%s stalling, txcsr %03x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300287 musb_ep->end_point.name, csr);
288 return;
289 }
290
Bin Liub99d3652016-06-30 12:12:22 -0500291 musb_dbg(musb, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300292 epnum, musb_ep->packet_sz, fifo_count,
293 csr);
294
295#ifndef CONFIG_MUSB_PIO_ONLY
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100296 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300297 struct dma_controller *c = musb->dma_controller;
Ming Lei66af83d2010-09-20 10:32:06 +0300298 size_t request_size;
299
300 /* setup DMA, then program endpoint CSR */
301 request_size = min_t(size_t, request->length - request->actual,
302 musb_ep->dma->max_len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300303
Ajay Kumar Guptad17d5352012-07-20 11:07:23 +0530304 use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300305
306 /* MUSB_TXCSR_P_ISO is still set correctly */
307
Felipe Balbi03840fa2015-08-06 10:47:16 -0500308 if (musb_dma_inventra(musb) || musb_dma_ux500(musb)) {
Anand Gadiyard1043a22009-04-02 12:07:08 -0700309 if (request_size < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300310 musb_ep->dma->desired_mode = 0;
311 else
312 musb_ep->dma->desired_mode = 1;
313
314 use_dma = use_dma && c->channel_program(
315 musb_ep->dma, musb_ep->packet_sz,
316 musb_ep->dma->desired_mode,
Cliff Cai796a83f2009-12-21 21:18:02 -0500317 request->dma + request->actual, request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300318 if (use_dma) {
319 if (musb_ep->dma->desired_mode == 0) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700320 /*
321 * We must not clear the DMAMODE bit
322 * before the DMAENAB bit -- and the
323 * latter doesn't always get cleared
324 * before we get here...
325 */
326 csr &= ~(MUSB_TXCSR_AUTOSET
327 | MUSB_TXCSR_DMAENAB);
328 musb_writew(epio, MUSB_TXCSR, csr
329 | MUSB_TXCSR_P_WZC_BITS);
330 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300331 csr |= (MUSB_TXCSR_DMAENAB |
332 MUSB_TXCSR_MODE);
333 /* against programming guide */
Ming Leif11d8932010-09-24 13:44:04 +0300334 } else {
335 csr |= (MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300336 | MUSB_TXCSR_DMAMODE
337 | MUSB_TXCSR_MODE);
supriya karanthbb3a2ef2012-12-06 11:12:48 +0530338 /*
339 * Enable Autoset according to table
340 * below
341 * bulk_split hb_mult Autoset_Enable
342 * 0 0 Yes(Normal)
343 * 0 >0 No(High BW ISO)
344 * 1 0 Yes(HS bulk)
345 * 1 >0 Yes(FS bulk)
346 */
347 if (!musb_ep->hb_mult ||
Geyslan G. Bem1a171622015-12-10 17:50:12 -0300348 can_bulk_split(musb,
349 musb_ep->type))
Ming Leif11d8932010-09-24 13:44:04 +0300350 csr |= MUSB_TXCSR_AUTOSET;
351 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300352 csr &= ~MUSB_TXCSR_P_UNDERRUN;
Ming Leif11d8932010-09-24 13:44:04 +0300353
Felipe Balbi550a7372008-07-24 12:27:36 +0300354 musb_writew(epio, MUSB_TXCSR, csr);
355 }
356 }
357
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700358 if (is_cppi_enabled(musb)) {
Sebastian Andrzej Siewiorfc525752013-08-13 19:38:23 +0200359 /* program endpoint CSR first, then setup DMA */
360 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
361 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
362 MUSB_TXCSR_MODE;
363 musb_writew(epio, MUSB_TXCSR, (MUSB_TXCSR_P_WZC_BITS &
364 ~MUSB_TXCSR_P_UNDERRUN) | csr);
365
366 /* ensure writebuffer is empty */
367 csr = musb_readw(epio, MUSB_TXCSR);
368
369 /*
370 * NOTE host side sets DMAENAB later than this; both are
371 * OK since the transfer dma glue (between CPPI and
372 * Mentor fifos) just tells CPPI it could start. Data
373 * only moves to the USB TX fifo when both fifos are
374 * ready.
375 */
376 /*
377 * "mode" is irrelevant here; handle terminating ZLPs
378 * like PIO does, since the hardware RNDIS mode seems
379 * unreliable except for the
380 * last-packet-is-already-short case.
381 */
382 use_dma = use_dma && c->channel_program(
383 musb_ep->dma, musb_ep->packet_sz,
384 0,
385 request->dma + request->actual,
386 request_size);
387 if (!use_dma) {
388 c->channel_release(musb_ep->dma);
389 musb_ep->dma = NULL;
390 csr &= ~MUSB_TXCSR_DMAENAB;
391 musb_writew(epio, MUSB_TXCSR, csr);
392 /* invariant: prequest->buf is non-null */
393 }
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700394 } else if (tusb_dma_omap(musb))
Sebastian Andrzej Siewiorfc525752013-08-13 19:38:23 +0200395 use_dma = use_dma && c->channel_program(
396 musb_ep->dma, musb_ep->packet_sz,
397 request->zero,
398 request->dma + request->actual,
399 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300400 }
401#endif
402
403 if (!use_dma) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600404 /*
405 * Unmap the dma buffer back to cpu if dma channel
406 * programming fails
407 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100408 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600409
Felipe Balbi550a7372008-07-24 12:27:36 +0300410 musb_write_fifo(musb_ep->hw_ep, fifo_count,
411 (u8 *) (request->buf + request->actual));
412 request->actual += fifo_count;
413 csr |= MUSB_TXCSR_TXPKTRDY;
414 csr &= ~MUSB_TXCSR_P_UNDERRUN;
415 musb_writew(epio, MUSB_TXCSR, csr);
416 }
417
418 /* host may already have the data when this message shows... */
Bin Liub99d3652016-06-30 12:12:22 -0500419 musb_dbg(musb, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300420 musb_ep->end_point.name, use_dma ? "dma" : "pio",
421 request->actual, request->length,
422 musb_readw(epio, MUSB_TXCSR),
423 fifo_count,
424 musb_readw(epio, MUSB_TXMAXP));
425}
426
427/*
428 * FIFO state update (e.g. data ready).
429 * Called from IRQ, with controller locked.
430 */
431void musb_g_tx(struct musb *musb, u8 epnum)
432{
433 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200434 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300435 struct usb_request *request;
436 u8 __iomem *mbase = musb->mregs;
437 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
438 void __iomem *epio = musb->endpoints[epnum].regs;
439 struct dma_channel *dma;
440
441 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200442 req = next_request(musb_ep);
443 request = &req->request;
Felipe Balbi550a7372008-07-24 12:27:36 +0300444
Bin Liufc780032016-06-30 12:12:27 -0500445 trace_musb_req_tx(req);
Felipe Balbi550a7372008-07-24 12:27:36 +0300446 csr = musb_readw(epio, MUSB_TXCSR);
Bin Liub99d3652016-06-30 12:12:22 -0500447 musb_dbg(musb, "<== %s, txcsr %04x", musb_ep->end_point.name, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300448
449 dma = is_dma_capable() ? musb_ep->dma : NULL;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300450
451 /*
452 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
453 * probably rates reporting as a host error.
454 */
455 if (csr & MUSB_TXCSR_P_SENTSTALL) {
456 csr |= MUSB_TXCSR_P_WZC_BITS;
457 csr &= ~MUSB_TXCSR_P_SENTSTALL;
458 musb_writew(epio, MUSB_TXCSR, csr);
459 return;
460 }
461
462 if (csr & MUSB_TXCSR_P_UNDERRUN) {
463 /* We NAKed, no big deal... little reason to care. */
464 csr |= MUSB_TXCSR_P_WZC_BITS;
465 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
466 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300467 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
468 epnum, request);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300469 }
470
471 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
472 /*
473 * SHOULD NOT HAPPEN... has with CPPI though, after
474 * changing SENDSTALL (and other cases); harmless?
Felipe Balbi550a7372008-07-24 12:27:36 +0300475 */
Bin Liub99d3652016-06-30 12:12:22 -0500476 musb_dbg(musb, "%s dma still busy?", musb_ep->end_point.name);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300477 return;
478 }
479
480 if (request) {
481 u8 is_dma = 0;
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700482 bool short_packet = false;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300483
484 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
485 is_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300486 csr |= MUSB_TXCSR_P_WZC_BITS;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300487 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
Mian Yousaf Kaukab100d4a92011-03-15 16:24:24 +0100488 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
Felipe Balbi550a7372008-07-24 12:27:36 +0300489 musb_writew(epio, MUSB_TXCSR, csr);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300490 /* Ensure writebuffer is empty. */
491 csr = musb_readw(epio, MUSB_TXCSR);
492 request->actual += musb_ep->dma->actual_len;
Bin Liub99d3652016-06-30 12:12:22 -0500493 musb_dbg(musb, "TXCSR%d %04x, DMA off, len %zu, req %p",
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300494 epnum, csr, musb_ep->dma->actual_len, request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300495 }
496
Ming Leie7379aa2010-09-24 13:44:14 +0300497 /*
498 * First, maybe a terminating short packet. Some DMA
499 * engines might handle this by themselves.
500 */
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700501 if ((request->zero && request->length)
Ming Leie7379aa2010-09-24 13:44:14 +0300502 && (request->length % musb_ep->packet_sz == 0)
503 && (request->actual == request->length))
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700504 short_packet = true;
505
506 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb)) &&
507 (is_dma && (!dma->desired_mode ||
Ming Leie7379aa2010-09-24 13:44:14 +0300508 (request->actual &
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700509 (musb_ep->packet_sz - 1)))))
510 short_packet = true;
511
512 if (short_packet) {
Ming Leie7379aa2010-09-24 13:44:14 +0300513 /*
514 * On DMA completion, FIFO may not be
515 * available yet...
516 */
517 if (csr & MUSB_TXCSR_TXPKTRDY)
518 return;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300519
Ming Leie7379aa2010-09-24 13:44:14 +0300520 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
521 | MUSB_TXCSR_TXPKTRDY);
522 request->zero = 0;
523 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300524
Ming Leie7379aa2010-09-24 13:44:14 +0300525 if (request->actual == request->length) {
526 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530527 /*
528 * In the giveback function the MUSB lock is
529 * released and acquired after sometime. During
530 * this time period the INDEX register could get
531 * changed by the gadget_queue function especially
532 * on SMP systems. Reselect the INDEX to be sure
533 * we are reading/modifying the right registers
534 */
535 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200536 req = musb_ep->desc ? next_request(musb_ep) : NULL;
537 if (!req) {
Bin Liub99d3652016-06-30 12:12:22 -0500538 musb_dbg(musb, "%s idle now",
Ming Leie7379aa2010-09-24 13:44:14 +0300539 musb_ep->end_point.name);
540 return;
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300541 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300542 }
543
Felipe Balbiad1adb82011-02-16 12:40:05 +0200544 txstate(musb, req);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300545 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300546}
547
548/* ------------------------------------------------------------ */
549
Felipe Balbi550a7372008-07-24 12:27:36 +0300550/*
551 * Context: controller locked, IRQs blocked, endpoint selected
552 */
553static void rxstate(struct musb *musb, struct musb_request *req)
554{
Felipe Balbi550a7372008-07-24 12:27:36 +0300555 const u8 epnum = req->epnum;
556 struct usb_request *request = &req->request;
Ming Leibd2e74d2010-09-20 10:32:01 +0300557 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300558 void __iomem *epio = musb->endpoints[epnum].regs;
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400559 unsigned len = 0;
560 u16 fifo_count;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300561 u16 csr = musb_readw(epio, MUSB_RXCSR);
Ming Leibd2e74d2010-09-20 10:32:01 +0300562 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700563 u8 use_mode_1;
Ming Leibd2e74d2010-09-20 10:32:01 +0300564
565 if (hw_ep->is_shared_fifo)
566 musb_ep = &hw_ep->ep_in;
567 else
568 musb_ep = &hw_ep->ep_out;
569
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400570 fifo_count = musb_ep->packet_sz;
Felipe Balbi550a7372008-07-24 12:27:36 +0300571
Vikram Panditaabf710e2012-05-18 13:48:04 -0700572 /* Check if EP is disabled */
573 if (!musb_ep->desc) {
Bin Liub99d3652016-06-30 12:12:22 -0500574 musb_dbg(musb, "ep:%s disabled - ignore request",
Vikram Panditaabf710e2012-05-18 13:48:04 -0700575 musb_ep->end_point.name);
576 return;
577 }
578
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300579 /* We shouldn't get here while DMA is active, but we do... */
580 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Bin Liub99d3652016-06-30 12:12:22 -0500581 musb_dbg(musb, "DMA pending...");
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300582 return;
583 }
584
585 if (csr & MUSB_RXCSR_P_SENDSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -0500586 musb_dbg(musb, "%s stalling, RXCSR %04x",
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300587 musb_ep->end_point.name, csr);
588 return;
589 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300590
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700591 if (is_cppi_enabled(musb) && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300592 struct dma_controller *c = musb->dma_controller;
593 struct dma_channel *channel = musb_ep->dma;
594
595 /* NOTE: CPPI won't actually stop advancing the DMA
596 * queue after short packet transfers, so this is almost
597 * always going to run as IRQ-per-packet DMA so that
598 * faults will be handled correctly.
599 */
600 if (c->channel_program(channel,
601 musb_ep->packet_sz,
602 !request->short_not_ok,
603 request->dma + request->actual,
604 request->length - request->actual)) {
605
606 /* make sure that if an rxpkt arrived after the irq,
607 * the cppi engine will be ready to take it as soon
608 * as DMA is enabled
609 */
610 csr &= ~(MUSB_RXCSR_AUTOCLEAR
611 | MUSB_RXCSR_DMAMODE);
612 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
613 musb_writew(epio, MUSB_RXCSR, csr);
614 return;
615 }
616 }
617
618 if (csr & MUSB_RXCSR_RXPKTRDY) {
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400619 fifo_count = musb_readw(epio, MUSB_RXCOUNT);
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700620
621 /*
Felipe Balbi00a89182012-10-26 09:55:31 +0300622 * Enable Mode 1 on RX transfers only when short_not_ok flag
623 * is set. Currently short_not_ok flag is set only from
624 * file_storage and f_mass_storage drivers
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700625 */
Felipe Balbi00a89182012-10-26 09:55:31 +0300626
627 if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700628 use_mode_1 = 1;
629 else
630 use_mode_1 = 0;
631
Felipe Balbi550a7372008-07-24 12:27:36 +0300632 if (request->actual < request->length) {
Felipe Balbi03840fa2015-08-06 10:47:16 -0500633 if (!is_buffer_mapped(req))
634 goto buffer_aint_mapped;
635
636 if (musb_dma_inventra(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300637 struct dma_controller *c;
638 struct dma_channel *channel;
639 int use_dma = 0;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200640 unsigned int transfer_size;
Felipe Balbi550a7372008-07-24 12:27:36 +0300641
642 c = musb->dma_controller;
643 channel = musb_ep->dma;
644
Felipe Balbi00a89182012-10-26 09:55:31 +0300645 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
646 * mode 0 only. So we do not get endpoint interrupts due to DMA
647 * completion. We only get interrupts from DMA controller.
648 *
649 * We could operate in DMA mode 1 if we knew the size of the tranfer
650 * in advance. For mass storage class, request->length = what the host
651 * sends, so that'd work. But for pretty much everything else,
652 * request->length is routinely more than what the host sends. For
653 * most these gadgets, end of is signified either by a short packet,
654 * or filling the last byte of the buffer. (Sending extra data in
655 * that last pckate should trigger an overflow fault.) But in mode 1,
656 * we don't get DMA completion interrupt for short packets.
657 *
658 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
659 * to get endpoint interrupt on every DMA req, but that didn't seem
660 * to work reliably.
661 *
662 * REVISIT an updated g_file_storage can set req->short_not_ok, which
663 * then becomes usable as a runtime "use mode 1" hint...
664 */
665
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700666 /* Experimental: Mode1 works with mass storage use cases */
667 if (use_mode_1) {
Ming Lei9001d802010-09-25 05:50:43 -0500668 csr |= MUSB_RXCSR_AUTOCLEAR;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700669 musb_writew(epio, MUSB_RXCSR, csr);
670 csr |= MUSB_RXCSR_DMAENAB;
671 musb_writew(epio, MUSB_RXCSR, csr);
672
673 /*
674 * this special sequence (enabling and then
675 * disabling MUSB_RXCSR_DMAMODE) is required
676 * to get DMAReq to activate
677 */
678 musb_writew(epio, MUSB_RXCSR,
679 csr | MUSB_RXCSR_DMAMODE);
680 musb_writew(epio, MUSB_RXCSR, csr);
681
Felipe Balbi37730ec2013-02-06 10:19:15 +0200682 transfer_size = min_t(unsigned int,
683 request->length -
684 request->actual,
Roger Quadros660fa882012-08-07 16:26:32 +0300685 channel->max_len);
686 musb_ep->dma->desired_mode = 1;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700687 } else {
688 if (!musb_ep->hb_mult &&
689 musb_ep->hw_ep->rx_double_buffered)
690 csr |= MUSB_RXCSR_AUTOCLEAR;
691 csr |= MUSB_RXCSR_DMAENAB;
692 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300693
Roger Quadros660fa882012-08-07 16:26:32 +0300694 transfer_size = min(request->length - request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400695 (unsigned)fifo_count);
Roger Quadros660fa882012-08-07 16:26:32 +0300696 musb_ep->dma->desired_mode = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300697 }
698
Roger Quadros660fa882012-08-07 16:26:32 +0300699 use_dma = c->channel_program(
700 channel,
701 musb_ep->packet_sz,
702 channel->desired_mode,
703 request->dma
704 + request->actual,
705 transfer_size);
706
Felipe Balbi550a7372008-07-24 12:27:36 +0300707 if (use_dma)
708 return;
709 }
Felipe Balbi03840fa2015-08-06 10:47:16 -0500710
711 if ((musb_dma_ux500(musb)) &&
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100712 (request->actual < request->length)) {
713
714 struct dma_controller *c;
715 struct dma_channel *channel;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200716 unsigned int transfer_size = 0;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100717
718 c = musb->dma_controller;
719 channel = musb_ep->dma;
720
721 /* In case first packet is short */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400722 if (fifo_count < musb_ep->packet_sz)
723 transfer_size = fifo_count;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100724 else if (request->short_not_ok)
Felipe Balbi37730ec2013-02-06 10:19:15 +0200725 transfer_size = min_t(unsigned int,
726 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100727 request->actual,
728 channel->max_len);
729 else
Felipe Balbi37730ec2013-02-06 10:19:15 +0200730 transfer_size = min_t(unsigned int,
731 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100732 request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400733 (unsigned)fifo_count);
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100734
735 csr &= ~MUSB_RXCSR_DMAMODE;
736 csr |= (MUSB_RXCSR_DMAENAB |
737 MUSB_RXCSR_AUTOCLEAR);
738
739 musb_writew(epio, MUSB_RXCSR, csr);
740
741 if (transfer_size <= musb_ep->packet_sz) {
742 musb_ep->dma->desired_mode = 0;
743 } else {
744 musb_ep->dma->desired_mode = 1;
745 /* Mode must be set after DMAENAB */
746 csr |= MUSB_RXCSR_DMAMODE;
747 musb_writew(epio, MUSB_RXCSR, csr);
748 }
749
750 if (c->channel_program(channel,
751 musb_ep->packet_sz,
752 channel->desired_mode,
753 request->dma
754 + request->actual,
755 transfer_size))
756
757 return;
758 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300759
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400760 len = request->length - request->actual;
Bin Liub99d3652016-06-30 12:12:22 -0500761 musb_dbg(musb, "%s OUT/RX pio fifo %d/%d, maxpacket %d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300762 musb_ep->end_point.name,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400763 fifo_count, len,
Felipe Balbi550a7372008-07-24 12:27:36 +0300764 musb_ep->packet_sz);
765
Felipe Balbic2c96322009-02-21 15:29:42 -0800766 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300767
Felipe Balbi03840fa2015-08-06 10:47:16 -0500768 if (tusb_dma_omap(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300769 struct dma_controller *c = musb->dma_controller;
770 struct dma_channel *channel = musb_ep->dma;
771 u32 dma_addr = request->dma + request->actual;
772 int ret;
773
774 ret = c->channel_program(channel,
775 musb_ep->packet_sz,
776 channel->desired_mode,
777 dma_addr,
778 fifo_count);
779 if (ret)
780 return;
781 }
Felipe Balbi03840fa2015-08-06 10:47:16 -0500782
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600783 /*
784 * Unmap the dma buffer back to cpu if dma channel
785 * programming fails. This buffer is mapped if the
786 * channel allocation is successful
787 */
Felipe Balbi03840fa2015-08-06 10:47:16 -0500788 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600789
Felipe Balbi03840fa2015-08-06 10:47:16 -0500790 /*
791 * Clear DMAENAB and AUTOCLEAR for the
792 * PIO mode transfer
793 */
794 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
795 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300796
Felipe Balbi03840fa2015-08-06 10:47:16 -0500797buffer_aint_mapped:
Felipe Balbi550a7372008-07-24 12:27:36 +0300798 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
799 (request->buf + request->actual));
800 request->actual += fifo_count;
801
802 /* REVISIT if we left anything in the fifo, flush
803 * it and report -EOVERFLOW
804 */
805
806 /* ack the read! */
807 csr |= MUSB_RXCSR_P_WZC_BITS;
808 csr &= ~MUSB_RXCSR_RXPKTRDY;
809 musb_writew(epio, MUSB_RXCSR, csr);
810 }
811 }
812
813 /* reach the end or short packet detected */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400814 if (request->actual == request->length ||
815 fifo_count < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300816 musb_g_giveback(musb_ep, request, 0);
817}
818
819/*
820 * Data ready for a request; called from IRQ
821 */
822void musb_g_rx(struct musb *musb, u8 epnum)
823{
824 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200825 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300826 struct usb_request *request;
827 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300828 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300829 void __iomem *epio = musb->endpoints[epnum].regs;
830 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300831 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
832
833 if (hw_ep->is_shared_fifo)
834 musb_ep = &hw_ep->ep_in;
835 else
836 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300837
838 musb_ep_select(mbase, epnum);
839
Felipe Balbiad1adb82011-02-16 12:40:05 +0200840 req = next_request(musb_ep);
841 if (!req)
Maulik Mankad0abdc362009-12-22 16:18:19 +0530842 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300843
Bin Liufc780032016-06-30 12:12:27 -0500844 trace_musb_req_rx(req);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200845 request = &req->request;
846
Felipe Balbi550a7372008-07-24 12:27:36 +0300847 csr = musb_readw(epio, MUSB_RXCSR);
848 dma = is_dma_capable() ? musb_ep->dma : NULL;
849
Bin Liub99d3652016-06-30 12:12:22 -0500850 musb_dbg(musb, "<== %s, rxcsr %04x%s %p", musb_ep->end_point.name,
Felipe Balbi550a7372008-07-24 12:27:36 +0300851 csr, dma ? " (dma)" : "", request);
852
853 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300854 csr |= MUSB_RXCSR_P_WZC_BITS;
855 csr &= ~MUSB_RXCSR_P_SENTSTALL;
856 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300857 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300858 }
859
860 if (csr & MUSB_RXCSR_P_OVERRUN) {
861 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
862 csr &= ~MUSB_RXCSR_P_OVERRUN;
863 musb_writew(epio, MUSB_RXCSR, csr);
864
Bin Liub99d3652016-06-30 12:12:22 -0500865 musb_dbg(musb, "%s iso overrun on %p", musb_ep->name, request);
Sergei Shtylyov43467862010-09-24 13:44:12 +0300866 if (request->status == -EINPROGRESS)
Felipe Balbi550a7372008-07-24 12:27:36 +0300867 request->status = -EOVERFLOW;
868 }
869 if (csr & MUSB_RXCSR_INCOMPRX) {
870 /* REVISIT not necessarily an error */
Bin Liub99d3652016-06-30 12:12:22 -0500871 musb_dbg(musb, "%s, incomprx", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +0300872 }
873
874 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
875 /* "should not happen"; likely RXPKTRDY pending for DMA */
Bin Liub99d3652016-06-30 12:12:22 -0500876 musb_dbg(musb, "%s busy, csr %04x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300877 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300878 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300879 }
880
881 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
882 csr &= ~(MUSB_RXCSR_AUTOCLEAR
883 | MUSB_RXCSR_DMAENAB
884 | MUSB_RXCSR_DMAMODE);
885 musb_writew(epio, MUSB_RXCSR,
886 MUSB_RXCSR_P_WZC_BITS | csr);
887
888 request->actual += musb_ep->dma->actual_len;
889
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100890#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
891 defined(CONFIG_USB_UX500_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +0300892 /* Autoclear doesn't clear RxPktRdy for short packets */
Ming Lei9001d802010-09-25 05:50:43 -0500893 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
Felipe Balbi550a7372008-07-24 12:27:36 +0300894 || (dma->actual_len
895 & (musb_ep->packet_sz - 1))) {
896 /* ack the read! */
897 csr &= ~MUSB_RXCSR_RXPKTRDY;
898 musb_writew(epio, MUSB_RXCSR, csr);
899 }
900
901 /* incomplete, and not short? wait for next IN packet */
902 if ((request->actual < request->length)
903 && (musb_ep->dma->actual_len
Ming Lei9001d802010-09-25 05:50:43 -0500904 == musb_ep->packet_sz)) {
905 /* In double buffer case, continue to unload fifo if
906 * there is Rx packet in FIFO.
907 **/
908 csr = musb_readw(epio, MUSB_RXCSR);
909 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
910 hw_ep->rx_double_buffered)
911 goto exit;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300912 return;
Ming Lei9001d802010-09-25 05:50:43 -0500913 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300914#endif
915 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530916 /*
917 * In the giveback function the MUSB lock is
918 * released and acquired after sometime. During
919 * this time period the INDEX register could get
920 * changed by the gadget_queue function especially
921 * on SMP systems. Reselect the INDEX to be sure
922 * we are reading/modifying the right registers
923 */
924 musb_ep_select(mbase, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300925
Felipe Balbiad1adb82011-02-16 12:40:05 +0200926 req = next_request(musb_ep);
927 if (!req)
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300928 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300929 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100930#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
931 defined(CONFIG_USB_UX500_DMA)
Ming Lei9001d802010-09-25 05:50:43 -0500932exit:
Ajay Kumar Guptabb324b02010-11-22 14:22:41 +0530933#endif
Sergei Shtylyov43467862010-09-24 13:44:12 +0300934 /* Analyze request */
Felipe Balbiad1adb82011-02-16 12:40:05 +0200935 rxstate(musb, req);
Felipe Balbi550a7372008-07-24 12:27:36 +0300936}
937
938/* ------------------------------------------------------------ */
939
940static int musb_gadget_enable(struct usb_ep *ep,
941 const struct usb_endpoint_descriptor *desc)
942{
943 unsigned long flags;
944 struct musb_ep *musb_ep;
945 struct musb_hw_ep *hw_ep;
946 void __iomem *regs;
947 struct musb *musb;
948 void __iomem *mbase;
949 u8 epnum;
950 u16 csr;
951 unsigned tmp;
952 int status = -EINVAL;
953
954 if (!ep || !desc)
955 return -EINVAL;
956
957 musb_ep = to_musb_ep(ep);
958 hw_ep = musb_ep->hw_ep;
959 regs = hw_ep->regs;
960 musb = musb_ep->musb;
961 mbase = musb->mregs;
962 epnum = musb_ep->current_epnum;
963
964 spin_lock_irqsave(&musb->lock, flags);
965
966 if (musb_ep->desc) {
967 status = -EBUSY;
968 goto fail;
969 }
Julia Lawall96bcd092009-01-24 17:57:24 -0800970 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +0300971
972 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -0800973 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300974 goto fail;
975
976 /* REVISIT this rules out high bandwidth periodic transfers */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700977 tmp = usb_endpoint_maxp(desc);
Ming Leif11d8932010-09-24 13:44:04 +0300978 if (tmp & ~0x07ff) {
979 int ok;
980
981 if (usb_endpoint_dir_in(desc))
982 ok = musb->hb_iso_tx;
983 else
984 ok = musb->hb_iso_rx;
985
986 if (!ok) {
Bin Liub99d3652016-06-30 12:12:22 -0500987 musb_dbg(musb, "no support for high bandwidth ISO");
Ming Leif11d8932010-09-24 13:44:04 +0300988 goto fail;
989 }
990 musb_ep->hb_mult = (tmp >> 11) & 3;
991 } else {
992 musb_ep->hb_mult = 0;
993 }
994
995 musb_ep->packet_sz = tmp & 0x7ff;
996 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300997
998 /* enable the interrupts for the endpoint, set the endpoint
999 * packet size (or fail), set the mode, clear the fifo
1000 */
1001 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -08001002 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001003
1004 if (hw_ep->is_shared_fifo)
1005 musb_ep->is_in = 1;
1006 if (!musb_ep->is_in)
1007 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001008
1009 if (tmp > hw_ep->max_packet_sz_tx) {
Bin Liub99d3652016-06-30 12:12:22 -05001010 musb_dbg(musb, "packet size beyond hardware FIFO size");
Felipe Balbi550a7372008-07-24 12:27:36 +03001011 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001012 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001013
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001014 musb->intrtxe |= (1 << epnum);
1015 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001016
1017 /* REVISIT if can_bulk_split(), use by updating "tmp";
1018 * likewise high bandwidth periodic tx
1019 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001020 /* Set TXMAXP with the FIFO size of the endpoint
Ming Lei31c99092010-10-19 19:08:25 -05001021 * to disable double buffering mode.
Cliff Cai9f445cb2010-01-28 20:44:18 -05001022 */
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301023 if (musb->double_buffer_not_ok) {
Felipe Balbi06624812011-01-21 13:39:20 +08001024 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301025 } else {
1026 if (can_bulk_split(musb, musb_ep->type))
1027 musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
1028 musb_ep->packet_sz) - 1;
Felipe Balbi06624812011-01-21 13:39:20 +08001029 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1030 | (musb_ep->hb_mult << 11));
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301031 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001032
1033 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1034 if (musb_readw(regs, MUSB_TXCSR)
1035 & MUSB_TXCSR_FIFONOTEMPTY)
1036 csr |= MUSB_TXCSR_FLUSHFIFO;
1037 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1038 csr |= MUSB_TXCSR_P_ISO;
1039
1040 /* set twice in case of double buffering */
1041 musb_writew(regs, MUSB_TXCSR, csr);
1042 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1043 musb_writew(regs, MUSB_TXCSR, csr);
1044
1045 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +03001046
1047 if (hw_ep->is_shared_fifo)
1048 musb_ep->is_in = 0;
1049 if (musb_ep->is_in)
1050 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001051
1052 if (tmp > hw_ep->max_packet_sz_rx) {
Bin Liub99d3652016-06-30 12:12:22 -05001053 musb_dbg(musb, "packet size beyond hardware FIFO size");
Felipe Balbi550a7372008-07-24 12:27:36 +03001054 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001055 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001056
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001057 musb->intrrxe |= (1 << epnum);
1058 musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001059
1060 /* REVISIT if can_bulk_combine() use by updating "tmp"
1061 * likewise high bandwidth periodic rx
1062 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001063 /* Set RXMAXP with the FIFO size of the endpoint
1064 * to disable double buffering mode.
1065 */
Felipe Balbi06624812011-01-21 13:39:20 +08001066 if (musb->double_buffer_not_ok)
1067 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1068 else
1069 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1070 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001071
1072 /* force shared fifo to OUT-only mode */
1073 if (hw_ep->is_shared_fifo) {
1074 csr = musb_readw(regs, MUSB_TXCSR);
1075 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1076 musb_writew(regs, MUSB_TXCSR, csr);
1077 }
1078
1079 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1080 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1081 csr |= MUSB_RXCSR_P_ISO;
1082 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1083 csr |= MUSB_RXCSR_DISNYET;
1084
1085 /* set twice in case of double buffering */
1086 musb_writew(regs, MUSB_RXCSR, csr);
1087 musb_writew(regs, MUSB_RXCSR, csr);
1088 }
1089
1090 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1091 * for some reason you run out of channels here.
1092 */
1093 if (is_dma_capable() && musb->dma_controller) {
1094 struct dma_controller *c = musb->dma_controller;
1095
1096 musb_ep->dma = c->channel_alloc(c, hw_ep,
1097 (desc->bEndpointAddress & USB_DIR_IN));
1098 } else
1099 musb_ep->dma = NULL;
1100
1101 musb_ep->desc = desc;
1102 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001103 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001104 status = 0;
1105
1106 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1107 musb_driver_name, musb_ep->end_point.name,
1108 ({ char *s; switch (musb_ep->type) {
1109 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1110 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1111 default: s = "iso"; break;
Joe Perches2b84f922013-10-08 16:01:37 -07001112 } s; }),
Felipe Balbi550a7372008-07-24 12:27:36 +03001113 musb_ep->is_in ? "IN" : "OUT",
1114 musb_ep->dma ? "dma, " : "",
1115 musb_ep->packet_sz);
1116
1117 schedule_work(&musb->irq_work);
1118
1119fail:
1120 spin_unlock_irqrestore(&musb->lock, flags);
1121 return status;
1122}
1123
1124/*
1125 * Disable an endpoint flushing all requests queued.
1126 */
1127static int musb_gadget_disable(struct usb_ep *ep)
1128{
1129 unsigned long flags;
1130 struct musb *musb;
1131 u8 epnum;
1132 struct musb_ep *musb_ep;
1133 void __iomem *epio;
1134 int status = 0;
1135
1136 musb_ep = to_musb_ep(ep);
1137 musb = musb_ep->musb;
1138 epnum = musb_ep->current_epnum;
1139 epio = musb->endpoints[epnum].regs;
1140
1141 spin_lock_irqsave(&musb->lock, flags);
1142 musb_ep_select(musb->mregs, epnum);
1143
1144 /* zero the endpoint sizes */
1145 if (musb_ep->is_in) {
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001146 musb->intrtxe &= ~(1 << epnum);
1147 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001148 musb_writew(epio, MUSB_TXMAXP, 0);
1149 } else {
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001150 musb->intrrxe &= ~(1 << epnum);
1151 musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001152 musb_writew(epio, MUSB_RXMAXP, 0);
1153 }
1154
Felipe Balbi550a7372008-07-24 12:27:36 +03001155 /* abort all pending DMA and requests */
1156 nuke(musb_ep, -ESHUTDOWN);
1157
Tal Shorer607fb0f2016-04-25 15:53:29 -05001158 musb_ep->desc = NULL;
1159 musb_ep->end_point.desc = NULL;
1160
Felipe Balbi550a7372008-07-24 12:27:36 +03001161 schedule_work(&musb->irq_work);
1162
1163 spin_unlock_irqrestore(&(musb->lock), flags);
1164
Bin Liub99d3652016-06-30 12:12:22 -05001165 musb_dbg(musb, "%s", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001166
1167 return status;
1168}
1169
1170/*
1171 * Allocate a request for an endpoint.
1172 * Reused by ep0 code.
1173 */
1174struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1175{
1176 struct musb_ep *musb_ep = to_musb_ep(ep);
1177 struct musb_request *request = NULL;
1178
1179 request = kzalloc(sizeof *request, gfp_flags);
Bin Liub99d3652016-06-30 12:12:22 -05001180 if (!request)
Felipe Balbi0607f862010-12-01 11:03:54 +02001181 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001182
Felipe Balbi0607f862010-12-01 11:03:54 +02001183 request->request.dma = DMA_ADDR_INVALID;
1184 request->epnum = musb_ep->current_epnum;
1185 request->ep = musb_ep;
1186
Bin Liufc780032016-06-30 12:12:27 -05001187 trace_musb_req_alloc(request);
Felipe Balbi550a7372008-07-24 12:27:36 +03001188 return &request->request;
1189}
1190
1191/*
1192 * Free a request
1193 * Reused by ep0 code.
1194 */
1195void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1196{
Bin Liufc780032016-06-30 12:12:27 -05001197 struct musb_request *request = to_musb_request(req);
1198
1199 trace_musb_req_free(request);
1200 kfree(request);
Felipe Balbi550a7372008-07-24 12:27:36 +03001201}
1202
1203static LIST_HEAD(buffers);
1204
1205struct free_record {
1206 struct list_head list;
1207 struct device *dev;
1208 unsigned bytes;
1209 dma_addr_t dma;
1210};
1211
1212/*
1213 * Context: controller locked, IRQs blocked.
1214 */
Sergei Shtylyova666e3e2010-09-11 13:23:12 -05001215void musb_ep_restart(struct musb *musb, struct musb_request *req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001216{
Bin Liufc780032016-06-30 12:12:27 -05001217 trace_musb_req_start(req);
Felipe Balbi550a7372008-07-24 12:27:36 +03001218 musb_ep_select(musb->mregs, req->epnum);
1219 if (req->tx)
1220 txstate(musb, req);
1221 else
1222 rxstate(musb, req);
1223}
1224
1225static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1226 gfp_t gfp_flags)
1227{
1228 struct musb_ep *musb_ep;
1229 struct musb_request *request;
1230 struct musb *musb;
1231 int status = 0;
1232 unsigned long lockflags;
1233
1234 if (!ep || !req)
1235 return -EINVAL;
1236 if (!req->buf)
1237 return -ENODATA;
1238
1239 musb_ep = to_musb_ep(ep);
1240 musb = musb_ep->musb;
1241
1242 request = to_musb_request(req);
1243 request->musb = musb;
1244
1245 if (request->ep != musb_ep)
1246 return -EINVAL;
1247
Bin Liufc780032016-06-30 12:12:27 -05001248 trace_musb_req_enq(request);
Felipe Balbi550a7372008-07-24 12:27:36 +03001249
1250 /* request is mine now... */
1251 request->request.actual = 0;
1252 request->request.status = -EINPROGRESS;
1253 request->epnum = musb_ep->current_epnum;
1254 request->tx = musb_ep->is_in;
1255
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +01001256 map_dma_buffer(request, musb, musb_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001257
Tony Lindgrencacaaf82016-10-19 12:03:40 -05001258 pm_runtime_get_sync(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001259 spin_lock_irqsave(&musb->lock, lockflags);
1260
1261 /* don't queue if the ep is down */
1262 if (!musb_ep->desc) {
Bin Liub99d3652016-06-30 12:12:22 -05001263 musb_dbg(musb, "req %p queued to %s while ep %s",
Felipe Balbi550a7372008-07-24 12:27:36 +03001264 req, ep->name, "disabled");
1265 status = -ESHUTDOWN;
Sebastian Andrzej Siewior23a53d92013-06-19 17:38:15 +02001266 unmap_dma_buffer(request, musb);
1267 goto unlock;
Felipe Balbi550a7372008-07-24 12:27:36 +03001268 }
1269
1270 /* add request to the list */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001271 list_add_tail(&request->list, &musb_ep->req_list);
Felipe Balbi550a7372008-07-24 12:27:36 +03001272
1273 /* it this is the head of the queue, start i/o ... */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001274 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
Felipe Balbi550a7372008-07-24 12:27:36 +03001275 musb_ep_restart(musb, request);
1276
Sebastian Andrzej Siewior23a53d92013-06-19 17:38:15 +02001277unlock:
Felipe Balbi550a7372008-07-24 12:27:36 +03001278 spin_unlock_irqrestore(&musb->lock, lockflags);
Tony Lindgrencacaaf82016-10-19 12:03:40 -05001279 pm_runtime_mark_last_busy(musb->controller);
1280 pm_runtime_put_autosuspend(musb->controller);
1281
Felipe Balbi550a7372008-07-24 12:27:36 +03001282 return status;
1283}
1284
1285static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1286{
1287 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001288 struct musb_request *req = to_musb_request(request);
1289 struct musb_request *r;
Felipe Balbi550a7372008-07-24 12:27:36 +03001290 unsigned long flags;
1291 int status = 0;
1292 struct musb *musb = musb_ep->musb;
1293
Bin Liufc780032016-06-30 12:12:27 -05001294 if (!ep || !request || req->ep != musb_ep)
Felipe Balbi550a7372008-07-24 12:27:36 +03001295 return -EINVAL;
1296
Bin Liufc780032016-06-30 12:12:27 -05001297 trace_musb_req_deq(req);
1298
Felipe Balbi550a7372008-07-24 12:27:36 +03001299 spin_lock_irqsave(&musb->lock, flags);
1300
1301 list_for_each_entry(r, &musb_ep->req_list, list) {
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001302 if (r == req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001303 break;
1304 }
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001305 if (r != req) {
Bin Liub99d3652016-06-30 12:12:22 -05001306 dev_err(musb->controller, "request %p not queued to %s\n",
1307 request, ep->name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001308 status = -EINVAL;
1309 goto done;
1310 }
1311
1312 /* if the hardware doesn't have the request, easy ... */
Felipe Balbi3d5ad132011-03-22 11:38:49 +02001313 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
Felipe Balbi550a7372008-07-24 12:27:36 +03001314 musb_g_giveback(musb_ep, request, -ECONNRESET);
1315
1316 /* ... else abort the dma transfer ... */
1317 else if (is_dma_capable() && musb_ep->dma) {
1318 struct dma_controller *c = musb->dma_controller;
1319
1320 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1321 if (c->channel_abort)
1322 status = c->channel_abort(musb_ep->dma);
1323 else
1324 status = -EBUSY;
1325 if (status == 0)
1326 musb_g_giveback(musb_ep, request, -ECONNRESET);
1327 } else {
1328 /* NOTE: by sticking to easily tested hardware/driver states,
1329 * we leave counting of in-flight packets imprecise.
1330 */
1331 musb_g_giveback(musb_ep, request, -ECONNRESET);
1332 }
1333
1334done:
1335 spin_unlock_irqrestore(&musb->lock, flags);
1336 return status;
1337}
1338
1339/*
1340 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1341 * data but will queue requests.
1342 *
1343 * exported to ep0 code
1344 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001345static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001346{
1347 struct musb_ep *musb_ep = to_musb_ep(ep);
1348 u8 epnum = musb_ep->current_epnum;
1349 struct musb *musb = musb_ep->musb;
1350 void __iomem *epio = musb->endpoints[epnum].regs;
1351 void __iomem *mbase;
1352 unsigned long flags;
1353 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001354 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001355 int status = 0;
1356
1357 if (!ep)
1358 return -EINVAL;
1359 mbase = musb->mregs;
1360
1361 spin_lock_irqsave(&musb->lock, flags);
1362
1363 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1364 status = -EINVAL;
1365 goto done;
1366 }
1367
1368 musb_ep_select(mbase, epnum);
1369
Felipe Balbiad1adb82011-02-16 12:40:05 +02001370 request = next_request(musb_ep);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001371 if (value) {
1372 if (request) {
Bin Liub99d3652016-06-30 12:12:22 -05001373 musb_dbg(musb, "request in progress, cannot halt %s",
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001374 ep->name);
1375 status = -EAGAIN;
1376 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001377 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001378 /* Cannot portably stall with non-empty FIFO */
1379 if (musb_ep->is_in) {
1380 csr = musb_readw(epio, MUSB_TXCSR);
1381 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Bin Liub99d3652016-06-30 12:12:22 -05001382 musb_dbg(musb, "FIFO busy, cannot halt %s",
1383 ep->name);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001384 status = -EAGAIN;
1385 goto done;
1386 }
1387 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001388 } else
1389 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001390
1391 /* set/clear the stall and toggle bits */
Bin Liub99d3652016-06-30 12:12:22 -05001392 musb_dbg(musb, "%s: %s stall", ep->name, value ? "set" : "clear");
Felipe Balbi550a7372008-07-24 12:27:36 +03001393 if (musb_ep->is_in) {
1394 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001395 csr |= MUSB_TXCSR_P_WZC_BITS
1396 | MUSB_TXCSR_CLRDATATOG;
1397 if (value)
1398 csr |= MUSB_TXCSR_P_SENDSTALL;
1399 else
1400 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1401 | MUSB_TXCSR_P_SENTSTALL);
1402 csr &= ~MUSB_TXCSR_TXPKTRDY;
1403 musb_writew(epio, MUSB_TXCSR, csr);
1404 } else {
1405 csr = musb_readw(epio, MUSB_RXCSR);
1406 csr |= MUSB_RXCSR_P_WZC_BITS
1407 | MUSB_RXCSR_FLUSHFIFO
1408 | MUSB_RXCSR_CLRDATATOG;
1409 if (value)
1410 csr |= MUSB_RXCSR_P_SENDSTALL;
1411 else
1412 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1413 | MUSB_RXCSR_P_SENTSTALL);
1414 musb_writew(epio, MUSB_RXCSR, csr);
1415 }
1416
Felipe Balbi550a7372008-07-24 12:27:36 +03001417 /* maybe start the first request in the queue */
1418 if (!musb_ep->busy && !value && request) {
Bin Liub99d3652016-06-30 12:12:22 -05001419 musb_dbg(musb, "restarting the request");
Felipe Balbi550a7372008-07-24 12:27:36 +03001420 musb_ep_restart(musb, request);
1421 }
1422
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001423done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001424 spin_unlock_irqrestore(&musb->lock, flags);
1425 return status;
1426}
1427
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001428/*
1429 * Sets the halt feature with the clear requests ignored
1430 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001431static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001432{
1433 struct musb_ep *musb_ep = to_musb_ep(ep);
1434
1435 if (!ep)
1436 return -EINVAL;
1437
1438 musb_ep->wedged = 1;
1439
1440 return usb_ep_set_halt(ep);
1441}
1442
Felipe Balbi550a7372008-07-24 12:27:36 +03001443static int musb_gadget_fifo_status(struct usb_ep *ep)
1444{
1445 struct musb_ep *musb_ep = to_musb_ep(ep);
1446 void __iomem *epio = musb_ep->hw_ep->regs;
1447 int retval = -EINVAL;
1448
1449 if (musb_ep->desc && !musb_ep->is_in) {
1450 struct musb *musb = musb_ep->musb;
1451 int epnum = musb_ep->current_epnum;
1452 void __iomem *mbase = musb->mregs;
1453 unsigned long flags;
1454
1455 spin_lock_irqsave(&musb->lock, flags);
1456
1457 musb_ep_select(mbase, epnum);
1458 /* FIXME return zero unless RXPKTRDY is set */
1459 retval = musb_readw(epio, MUSB_RXCOUNT);
1460
1461 spin_unlock_irqrestore(&musb->lock, flags);
1462 }
1463 return retval;
1464}
1465
1466static void musb_gadget_fifo_flush(struct usb_ep *ep)
1467{
1468 struct musb_ep *musb_ep = to_musb_ep(ep);
1469 struct musb *musb = musb_ep->musb;
1470 u8 epnum = musb_ep->current_epnum;
1471 void __iomem *epio = musb->endpoints[epnum].regs;
1472 void __iomem *mbase;
1473 unsigned long flags;
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001474 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03001475
1476 mbase = musb->mregs;
1477
1478 spin_lock_irqsave(&musb->lock, flags);
1479 musb_ep_select(mbase, (u8) epnum);
1480
1481 /* disable interrupts */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001482 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
Felipe Balbi550a7372008-07-24 12:27:36 +03001483
1484 if (musb_ep->is_in) {
1485 csr = musb_readw(epio, MUSB_TXCSR);
1486 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1487 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
Yauheni Kaliuta4858f062011-06-08 17:12:02 +03001488 /*
1489 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1490 * to interrupt current FIFO loading, but not flushing
1491 * the already loaded ones.
1492 */
1493 csr &= ~MUSB_TXCSR_TXPKTRDY;
Felipe Balbi550a7372008-07-24 12:27:36 +03001494 musb_writew(epio, MUSB_TXCSR, csr);
1495 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1496 musb_writew(epio, MUSB_TXCSR, csr);
1497 }
1498 } else {
1499 csr = musb_readw(epio, MUSB_RXCSR);
1500 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1501 musb_writew(epio, MUSB_RXCSR, csr);
1502 musb_writew(epio, MUSB_RXCSR, csr);
1503 }
1504
1505 /* re-enable interrupt */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001506 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001507 spin_unlock_irqrestore(&musb->lock, flags);
1508}
1509
1510static const struct usb_ep_ops musb_ep_ops = {
1511 .enable = musb_gadget_enable,
1512 .disable = musb_gadget_disable,
1513 .alloc_request = musb_alloc_request,
1514 .free_request = musb_free_request,
1515 .queue = musb_gadget_queue,
1516 .dequeue = musb_gadget_dequeue,
1517 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001518 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001519 .fifo_status = musb_gadget_fifo_status,
1520 .fifo_flush = musb_gadget_fifo_flush
1521};
1522
1523/* ----------------------------------------------------------------------- */
1524
1525static int musb_gadget_get_frame(struct usb_gadget *gadget)
1526{
1527 struct musb *musb = gadget_to_musb(gadget);
1528
1529 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1530}
1531
1532static int musb_gadget_wakeup(struct usb_gadget *gadget)
1533{
1534 struct musb *musb = gadget_to_musb(gadget);
1535 void __iomem *mregs = musb->mregs;
1536 unsigned long flags;
1537 int status = -EINVAL;
1538 u8 power, devctl;
1539 int retries;
1540
1541 spin_lock_irqsave(&musb->lock, flags);
1542
Antoine Tenarte47d9252014-10-30 18:41:13 +01001543 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001544 case OTG_STATE_B_PERIPHERAL:
1545 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1546 * that's part of the standard usb 1.1 state machine, and
1547 * doesn't affect OTG transitions.
1548 */
1549 if (musb->may_wakeup && musb->is_suspended)
1550 break;
1551 goto done;
1552 case OTG_STATE_B_IDLE:
1553 /* Start SRP ... OTG not required. */
1554 devctl = musb_readb(mregs, MUSB_DEVCTL);
Bin Liub99d3652016-06-30 12:12:22 -05001555 musb_dbg(musb, "Sending SRP: devctl: %02x", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001556 devctl |= MUSB_DEVCTL_SESSION;
1557 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1558 devctl = musb_readb(mregs, MUSB_DEVCTL);
1559 retries = 100;
1560 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1561 devctl = musb_readb(mregs, MUSB_DEVCTL);
1562 if (retries-- < 1)
1563 break;
1564 }
1565 retries = 10000;
1566 while (devctl & MUSB_DEVCTL_SESSION) {
1567 devctl = musb_readb(mregs, MUSB_DEVCTL);
1568 if (retries-- < 1)
1569 break;
1570 }
1571
Hema HK86205432011-03-22 16:54:22 +05301572 spin_unlock_irqrestore(&musb->lock, flags);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001573 otg_start_srp(musb->xceiv->otg);
Hema HK86205432011-03-22 16:54:22 +05301574 spin_lock_irqsave(&musb->lock, flags);
1575
Felipe Balbi550a7372008-07-24 12:27:36 +03001576 /* Block idling for at least 1s */
1577 musb_platform_try_idle(musb,
1578 jiffies + msecs_to_jiffies(1 * HZ));
1579
1580 status = 0;
1581 goto done;
1582 default:
Bin Liub99d3652016-06-30 12:12:22 -05001583 musb_dbg(musb, "Unhandled wake: %s",
Antoine Tenarte47d9252014-10-30 18:41:13 +01001584 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001585 goto done;
1586 }
1587
1588 status = 0;
1589
1590 power = musb_readb(mregs, MUSB_POWER);
1591 power |= MUSB_POWER_RESUME;
1592 musb_writeb(mregs, MUSB_POWER, power);
Bin Liub99d3652016-06-30 12:12:22 -05001593 musb_dbg(musb, "issue wakeup");
Felipe Balbi550a7372008-07-24 12:27:36 +03001594
1595 /* FIXME do this next chunk in a timer callback, no udelay */
1596 mdelay(2);
1597
1598 power = musb_readb(mregs, MUSB_POWER);
1599 power &= ~MUSB_POWER_RESUME;
1600 musb_writeb(mregs, MUSB_POWER, power);
1601done:
1602 spin_unlock_irqrestore(&musb->lock, flags);
1603 return status;
1604}
1605
1606static int
1607musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1608{
Peter Chendadac982015-01-28 16:32:41 +08001609 gadget->is_selfpowered = !!is_selfpowered;
Felipe Balbi550a7372008-07-24 12:27:36 +03001610 return 0;
1611}
1612
1613static void musb_pullup(struct musb *musb, int is_on)
1614{
1615 u8 power;
1616
1617 power = musb_readb(musb->mregs, MUSB_POWER);
1618 if (is_on)
1619 power |= MUSB_POWER_SOFTCONN;
1620 else
1621 power &= ~MUSB_POWER_SOFTCONN;
1622
1623 /* FIXME if on, HdrcStart; if off, HdrcStop */
1624
Bin Liub99d3652016-06-30 12:12:22 -05001625 musb_dbg(musb, "gadget D+ pullup %s",
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001626 is_on ? "on" : "off");
Felipe Balbi550a7372008-07-24 12:27:36 +03001627 musb_writeb(musb->mregs, MUSB_POWER, power);
1628}
1629
1630#if 0
1631static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1632{
Bin Liub99d3652016-06-30 12:12:22 -05001633 musb_dbg(musb, "<= %s =>\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001634
1635 /*
1636 * FIXME iff driver's softconnect flag is set (as it is during probe,
1637 * though that can clear it), just musb_pullup().
1638 */
1639
1640 return -EINVAL;
1641}
1642#endif
1643
1644static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1645{
1646 struct musb *musb = gadget_to_musb(gadget);
1647
David Brownell84e250f2009-03-31 12:30:04 -07001648 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001649 return -EOPNOTSUPP;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001650 return usb_phy_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001651}
1652
Tony Lindgren517baff2016-05-31 10:05:14 -05001653static void musb_gadget_work(struct work_struct *work)
1654{
1655 struct musb *musb;
1656 unsigned long flags;
1657
1658 musb = container_of(work, struct musb, gadget_work.work);
1659 pm_runtime_get_sync(musb->controller);
1660 spin_lock_irqsave(&musb->lock, flags);
1661 musb_pullup(musb, musb->softconnect);
1662 spin_unlock_irqrestore(&musb->lock, flags);
1663 pm_runtime_mark_last_busy(musb->controller);
1664 pm_runtime_put_autosuspend(musb->controller);
1665}
1666
Felipe Balbi550a7372008-07-24 12:27:36 +03001667static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1668{
1669 struct musb *musb = gadget_to_musb(gadget);
1670 unsigned long flags;
1671
1672 is_on = !!is_on;
1673
1674 /* NOTE: this assumes we are sensing vbus; we'd rather
1675 * not pullup unless the B-session is active.
1676 */
1677 spin_lock_irqsave(&musb->lock, flags);
1678 if (is_on != musb->softconnect) {
1679 musb->softconnect = is_on;
Tony Lindgren517baff2016-05-31 10:05:14 -05001680 schedule_delayed_work(&musb->gadget_work, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001681 }
1682 spin_unlock_irqrestore(&musb->lock, flags);
John Stultz93e098a2011-07-20 17:09:34 -07001683
Felipe Balbi550a7372008-07-24 12:27:36 +03001684 return 0;
1685}
1686
Robert Baldyga26b8aa42015-08-06 14:11:15 +02001687#ifdef CONFIG_BLACKFIN
1688static struct usb_ep *musb_match_ep(struct usb_gadget *g,
1689 struct usb_endpoint_descriptor *desc,
1690 struct usb_ss_ep_comp_descriptor *ep_comp)
1691{
1692 struct usb_ep *ep = NULL;
1693
1694 switch (usb_endpoint_type(desc)) {
1695 case USB_ENDPOINT_XFER_ISOC:
1696 case USB_ENDPOINT_XFER_BULK:
1697 if (usb_endpoint_dir_in(desc))
1698 ep = gadget_find_ep_by_name(g, "ep5in");
1699 else
1700 ep = gadget_find_ep_by_name(g, "ep6out");
1701 break;
1702 case USB_ENDPOINT_XFER_INT:
1703 if (usb_endpoint_dir_in(desc))
1704 ep = gadget_find_ep_by_name(g, "ep1in");
1705 else
1706 ep = gadget_find_ep_by_name(g, "ep2out");
1707 break;
1708 default:
Robert Baldyga2f3cc242015-08-07 14:13:34 +02001709 break;
Robert Baldyga26b8aa42015-08-06 14:11:15 +02001710 }
1711
1712 if (ep && usb_gadget_ep_match_desc(g, ep, desc, ep_comp))
1713 return ep;
1714
1715 return NULL;
1716}
1717#else
1718#define musb_match_ep NULL
1719#endif
1720
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001721static int musb_gadget_start(struct usb_gadget *g,
1722 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001723static int musb_gadget_stop(struct usb_gadget *g);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001724
Felipe Balbi550a7372008-07-24 12:27:36 +03001725static const struct usb_gadget_ops musb_gadget_operations = {
1726 .get_frame = musb_gadget_get_frame,
1727 .wakeup = musb_gadget_wakeup,
1728 .set_selfpowered = musb_gadget_set_self_powered,
1729 /* .vbus_session = musb_gadget_vbus_session, */
1730 .vbus_draw = musb_gadget_vbus_draw,
1731 .pullup = musb_gadget_pullup,
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001732 .udc_start = musb_gadget_start,
1733 .udc_stop = musb_gadget_stop,
Robert Baldyga26b8aa42015-08-06 14:11:15 +02001734 .match_ep = musb_match_ep,
Felipe Balbi550a7372008-07-24 12:27:36 +03001735};
1736
1737/* ----------------------------------------------------------------------- */
1738
1739/* Registration */
1740
1741/* Only this registration code "knows" the rule (from USB standards)
1742 * about there being only one external upstream port. It assumes
1743 * all peripheral ports are external...
1744 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001745
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001746static void
Felipe Balbi550a7372008-07-24 12:27:36 +03001747init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1748{
1749 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1750
1751 memset(ep, 0, sizeof *ep);
1752
1753 ep->current_epnum = epnum;
1754 ep->musb = musb;
1755 ep->hw_ep = hw_ep;
1756 ep->is_in = is_in;
1757
1758 INIT_LIST_HEAD(&ep->req_list);
1759
1760 sprintf(ep->name, "ep%d%s", epnum,
1761 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1762 is_in ? "in" : "out"));
1763 ep->end_point.name = ep->name;
1764 INIT_LIST_HEAD(&ep->end_point.ep_list);
1765 if (!epnum) {
Robert Baldygae117e742013-12-13 12:23:38 +01001766 usb_ep_set_maxpacket_limit(&ep->end_point, 64);
Robert Baldyga85019552015-07-31 16:00:46 +02001767 ep->end_point.caps.type_control = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001768 ep->end_point.ops = &musb_g_ep0_ops;
1769 musb->g.ep0 = &ep->end_point;
1770 } else {
1771 if (is_in)
Robert Baldygae117e742013-12-13 12:23:38 +01001772 usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_tx);
Felipe Balbi550a7372008-07-24 12:27:36 +03001773 else
Robert Baldygae117e742013-12-13 12:23:38 +01001774 usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_rx);
Robert Baldyga85019552015-07-31 16:00:46 +02001775 ep->end_point.caps.type_iso = true;
1776 ep->end_point.caps.type_bulk = true;
1777 ep->end_point.caps.type_int = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001778 ep->end_point.ops = &musb_ep_ops;
1779 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1780 }
Robert Baldyga85019552015-07-31 16:00:46 +02001781
1782 if (!epnum || hw_ep->is_shared_fifo) {
1783 ep->end_point.caps.dir_in = true;
1784 ep->end_point.caps.dir_out = true;
1785 } else if (is_in)
1786 ep->end_point.caps.dir_in = true;
1787 else
1788 ep->end_point.caps.dir_out = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001789}
1790
1791/*
1792 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1793 * to the rest of the driver state.
1794 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001795static inline void musb_g_init_endpoints(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001796{
1797 u8 epnum;
1798 struct musb_hw_ep *hw_ep;
1799 unsigned count = 0;
1800
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001801 /* initialize endpoint list just once */
Felipe Balbi550a7372008-07-24 12:27:36 +03001802 INIT_LIST_HEAD(&(musb->g.ep_list));
1803
1804 for (epnum = 0, hw_ep = musb->endpoints;
1805 epnum < musb->nr_endpoints;
1806 epnum++, hw_ep++) {
1807 if (hw_ep->is_shared_fifo /* || !epnum */) {
1808 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1809 count++;
1810 } else {
1811 if (hw_ep->max_packet_sz_tx) {
1812 init_peripheral_ep(musb, &hw_ep->ep_in,
1813 epnum, 1);
1814 count++;
1815 }
1816 if (hw_ep->max_packet_sz_rx) {
1817 init_peripheral_ep(musb, &hw_ep->ep_out,
1818 epnum, 0);
1819 count++;
1820 }
1821 }
1822 }
1823}
1824
1825/* called once during driver setup to initialize and link into
1826 * the driver model; memory is zeroed.
1827 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001828int musb_gadget_setup(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001829{
1830 int status;
1831
1832 /* REVISIT minor race: if (erroneously) setting up two
1833 * musb peripherals at the same time, only the bus lock
1834 * is probably held.
1835 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001836
1837 musb->g.ops = &musb_gadget_operations;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001838 musb->g.max_speed = USB_SPEED_HIGH;
Felipe Balbi550a7372008-07-24 12:27:36 +03001839 musb->g.speed = USB_SPEED_UNKNOWN;
1840
Bin Liu1374a4302013-09-17 12:43:13 -05001841 MUSB_DEV_MODE(musb);
1842 musb->xceiv->otg->default_a = 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001843 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Bin Liu1374a4302013-09-17 12:43:13 -05001844
Felipe Balbi550a7372008-07-24 12:27:36 +03001845 /* this "gadget" abstracts/virtualizes the controller */
Felipe Balbi550a7372008-07-24 12:27:36 +03001846 musb->g.name = musb_driver_name;
Apelete Seketelifd3923a2013-11-19 23:18:20 +01001847#if IS_ENABLED(CONFIG_USB_MUSB_DUAL_ROLE)
Felipe Balbi032ec492011-11-24 15:46:26 +02001848 musb->g.is_otg = 1;
Apelete Seketelifd3923a2013-11-19 23:18:20 +01001849#elif IS_ENABLED(CONFIG_USB_MUSB_GADGET)
1850 musb->g.is_otg = 0;
1851#endif
Tony Lindgren517baff2016-05-31 10:05:14 -05001852 INIT_DELAYED_WORK(&musb->gadget_work, musb_gadget_work);
Felipe Balbi550a7372008-07-24 12:27:36 +03001853 musb_g_init_endpoints(musb);
1854
1855 musb->is_active = 0;
1856 musb_platform_try_idle(musb, 0);
1857
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001858 status = usb_add_gadget_udc(musb->controller, &musb->g);
1859 if (status)
1860 goto err;
1861
1862 return 0;
1863err:
Sebastian Andrzej Siewior6193d692011-08-10 11:01:57 +02001864 musb->g.dev.parent = NULL;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001865 device_unregister(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001866 return status;
1867}
1868
1869void musb_gadget_cleanup(struct musb *musb)
1870{
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02001871 if (musb->port_mode == MUSB_PORT_MODE_HOST)
1872 return;
Tony Lindgren517baff2016-05-31 10:05:14 -05001873
1874 cancel_delayed_work_sync(&musb->gadget_work);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001875 usb_del_gadget_udc(&musb->g);
Felipe Balbi550a7372008-07-24 12:27:36 +03001876}
1877
1878/*
1879 * Register the gadget driver. Used by gadget drivers when
1880 * registering themselves with the controller.
1881 *
1882 * -EINVAL something went wrong (not driver)
1883 * -EBUSY another gadget is already using the controller
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001884 * -ENOMEM no memory to perform the operation
Felipe Balbi550a7372008-07-24 12:27:36 +03001885 *
1886 * @param driver the gadget driver
1887 * @return <0 if error, 0 if everything is fine
1888 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001889static int musb_gadget_start(struct usb_gadget *g,
1890 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03001891{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001892 struct musb *musb = gadget_to_musb(g);
Heikki Krogerusd445b6d2012-02-13 13:24:15 +02001893 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001894 unsigned long flags;
Felipe Balbi032ec492011-11-24 15:46:26 +02001895 int retval = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001896
Felipe Balbi032ec492011-11-24 15:46:26 +02001897 if (driver->max_speed < USB_SPEED_HIGH) {
1898 retval = -EINVAL;
1899 goto err;
1900 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001901
Hema HK7acc6192011-02-28 14:19:34 +05301902 pm_runtime_get_sync(musb->controller);
1903
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001904 musb->softconnect = 0;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001905 musb->gadget_driver = driver;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001906
1907 spin_lock_irqsave(&musb->lock, flags);
Greg Kroah-Hartman43e699c2013-10-14 13:06:15 -07001908 musb->is_active = 1;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001909
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001910 otg_set_peripheral(otg, &musb->g);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001911 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001912 spin_unlock_irqrestore(&musb->lock, flags);
1913
Sebastian Andrzej Siewior001dd842013-10-11 10:38:13 +02001914 musb_start(musb);
1915
Felipe Balbi032ec492011-11-24 15:46:26 +02001916 /* REVISIT: funcall to other code, which also
1917 * handles power budgeting ... this way also
1918 * ensures HdrcStart is indirectly called.
1919 */
Grazvydas Ignotasb65ae0f2013-03-24 17:36:55 +02001920 if (musb->xceiv->last_event == USB_EVENT_ID)
1921 musb_platform_set_vbus(musb, 1);
Felipe Balbi032ec492011-11-24 15:46:26 +02001922
Tony Lindgren30647212016-05-31 10:05:13 -05001923 pm_runtime_mark_last_busy(musb->controller);
1924 pm_runtime_put_autosuspend(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001925
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001926 return 0;
1927
Felipe Balbi032ec492011-11-24 15:46:26 +02001928err:
Felipe Balbi550a7372008-07-24 12:27:36 +03001929 return retval;
1930}
Felipe Balbi550a7372008-07-24 12:27:36 +03001931
Felipe Balbi550a7372008-07-24 12:27:36 +03001932/*
1933 * Unregister the gadget driver. Used by gadget drivers when
1934 * unregistering themselves from the controller.
1935 *
1936 * @param driver the gadget driver to unregister
1937 */
Felipe Balbi22835b82014-10-17 12:05:12 -05001938static int musb_gadget_stop(struct usb_gadget *g)
Felipe Balbi550a7372008-07-24 12:27:36 +03001939{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001940 struct musb *musb = gadget_to_musb(g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001941 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03001942
Tony Lindgren30647212016-05-31 10:05:13 -05001943 pm_runtime_get_sync(musb->controller);
Hema HK7acc6192011-02-28 14:19:34 +05301944
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001945 /*
1946 * REVISIT always use otg_set_peripheral() here too;
Felipe Balbi550a7372008-07-24 12:27:36 +03001947 * this needs to shut down the OTG engine.
1948 */
1949
1950 spin_lock_irqsave(&musb->lock, flags);
1951
Felipe Balbi550a7372008-07-24 12:27:36 +03001952 musb_hnp_stop(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001953
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001954 (void) musb_gadget_vbus_draw(&musb->g, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001955
Antoine Tenarte47d9252014-10-30 18:41:13 +01001956 musb->xceiv->otg->state = OTG_STATE_UNDEFINED;
Felipe Balbid5638fc2015-02-02 17:14:12 -06001957 musb_stop(musb);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001958 otg_set_peripheral(musb->xceiv->otg, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001959
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001960 musb->is_active = 0;
Grazvydas Ignotase21de102013-03-10 02:49:14 +02001961 musb->gadget_driver = NULL;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001962 musb_platform_try_idle(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001963 spin_unlock_irqrestore(&musb->lock, flags);
1964
Felipe Balbi032ec492011-11-24 15:46:26 +02001965 /*
1966 * FIXME we need to be able to register another
1967 * gadget driver here and have everything work;
1968 * that currently misbehaves.
1969 */
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001970
Tony Lindgren4e719182016-09-22 15:58:29 -05001971 /* Force check of devctl register for PM runtime */
1972 schedule_work(&musb->irq_work);
1973
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}