blob: 8ba98d8c0e8804cbd29c80db0bd157b0294e8ad3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Ilya Yanok06bb9202012-11-06 13:48:21 +00002/*
3 * MUSB OTG driver peripheral support
4 *
5 * Copyright 2005 Mentor Graphics Corporation
6 * Copyright (C) 2005-2006 by Texas Instruments
7 * Copyright (C) 2006-2007 Nokia Corporation
8 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
Ilya Yanok06bb9202012-11-06 13:48:21 +00009 */
10
Ilya Yanok06bb9202012-11-06 13:48:21 +000011#ifndef __UBOOT__
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070014#include <dm/devres.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +000015#include <linux/kernel.h>
16#include <linux/list.h>
17#include <linux/timer.h>
18#include <linux/module.h>
19#include <linux/smp.h>
20#include <linux/spinlock.h>
21#include <linux/delay.h>
22#include <linux/dma-mapping.h>
23#include <linux/slab.h>
24#else
25#include <common.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060026#include <linux/bug.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +000027#include <linux/usb/ch9.h>
28#include "linux-compat.h"
29#endif
30
31#include "musb_core.h"
32
33
34/* MUSB PERIPHERAL status 3-mar-2006:
35 *
36 * - EP0 seems solid. It passes both USBCV and usbtest control cases.
37 * Minor glitches:
38 *
39 * + remote wakeup to Linux hosts work, but saw USBCV failures;
40 * in one test run (operator error?)
41 * + endpoint halt tests -- in both usbtest and usbcv -- seem
42 * to break when dma is enabled ... is something wrongly
43 * clearing SENDSTALL?
44 *
45 * - Mass storage behaved ok when last tested. Network traffic patterns
46 * (with lots of short transfers etc) need retesting; they turn up the
47 * worst cases of the DMA, since short packets are typical but are not
48 * required.
49 *
50 * - TX/IN
51 * + both pio and dma behave in with network and g_zero tests
52 * + no cppi throughput issues other than no-hw-queueing
53 * + failed with FLAT_REG (DaVinci)
54 * + seems to behave with double buffering, PIO -and- CPPI
55 * + with gadgetfs + AIO, requests got lost?
56 *
57 * - RX/OUT
58 * + both pio and dma behave in with network and g_zero tests
59 * + dma is slow in typical case (short_not_ok is clear)
60 * + double buffering ok with PIO
61 * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
62 * + request lossage observed with gadgetfs
63 *
64 * - ISO not tested ... might work, but only weakly isochronous
65 *
66 * - Gadget driver disabling of softconnect during bind() is ignored; so
67 * drivers can't hold off host requests until userspace is ready.
68 * (Workaround: they can turn it off later.)
69 *
70 * - PORTABILITY (assumes PIO works):
71 * + DaVinci, basically works with cppi dma
72 * + OMAP 2430, ditto with mentor dma
73 * + TUSB 6010, platform-specific dma in the works
74 */
75
76/* ----------------------------------------------------------------------- */
77
78#define is_buffer_mapped(req) (is_dma_capable() && \
79 (req->map_state != UN_MAPPED))
80
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +020081#ifndef CONFIG_USB_MUSB_PIO_ONLY
Ilya Yanok06bb9202012-11-06 13:48:21 +000082/* Maps the buffer to dma */
83
84static inline void map_dma_buffer(struct musb_request *request,
85 struct musb *musb, struct musb_ep *musb_ep)
86{
87 int compatible = true;
88 struct dma_controller *dma = musb->dma_controller;
89
90 request->map_state = UN_MAPPED;
91
92 if (!is_dma_capable() || !musb_ep->dma)
93 return;
94
95 /* Check if DMA engine can handle this request.
96 * DMA code must reject the USB request explicitly.
97 * Default behaviour is to map the request.
98 */
99 if (dma->is_compatible)
100 compatible = dma->is_compatible(musb_ep->dma,
101 musb_ep->packet_sz, request->request.buf,
102 request->request.length);
103 if (!compatible)
104 return;
105
106 if (request->request.dma == DMA_ADDR_INVALID) {
107 request->request.dma = dma_map_single(
108 musb->controller,
109 request->request.buf,
110 request->request.length,
111 request->tx
112 ? DMA_TO_DEVICE
113 : DMA_FROM_DEVICE);
114 request->map_state = MUSB_MAPPED;
115 } else {
116 dma_sync_single_for_device(musb->controller,
117 request->request.dma,
118 request->request.length,
119 request->tx
120 ? DMA_TO_DEVICE
121 : DMA_FROM_DEVICE);
122 request->map_state = PRE_MAPPED;
123 }
124}
125
126/* Unmap the buffer from dma and maps it back to cpu */
127static inline void unmap_dma_buffer(struct musb_request *request,
128 struct musb *musb)
129{
130 if (!is_buffer_mapped(request))
131 return;
132
133 if (request->request.dma == DMA_ADDR_INVALID) {
134 dev_vdbg(musb->controller,
135 "not unmapping a never mapped buffer\n");
136 return;
137 }
138 if (request->map_state == MUSB_MAPPED) {
139 dma_unmap_single(musb->controller,
140 request->request.dma,
141 request->request.length,
142 request->tx
143 ? DMA_TO_DEVICE
144 : DMA_FROM_DEVICE);
145 request->request.dma = DMA_ADDR_INVALID;
146 } else { /* PRE_MAPPED */
147 dma_sync_single_for_cpu(musb->controller,
148 request->request.dma,
149 request->request.length,
150 request->tx
151 ? DMA_TO_DEVICE
152 : DMA_FROM_DEVICE);
153 }
154 request->map_state = UN_MAPPED;
155}
156#else
157static inline void map_dma_buffer(struct musb_request *request,
158 struct musb *musb, struct musb_ep *musb_ep)
159{
160}
161
162static inline void unmap_dma_buffer(struct musb_request *request,
163 struct musb *musb)
164{
165}
166#endif
167
168/*
169 * Immediately complete a request.
170 *
171 * @param request the request to complete
172 * @param status the status to complete the request with
173 * Context: controller locked, IRQs blocked.
174 */
175void musb_g_giveback(
176 struct musb_ep *ep,
177 struct usb_request *request,
178 int status)
179__releases(ep->musb->lock)
180__acquires(ep->musb->lock)
181{
182 struct musb_request *req;
183 struct musb *musb;
184 int busy = ep->busy;
185
186 req = to_musb_request(request);
187
188 list_del(&req->list);
189 if (req->request.status == -EINPROGRESS)
190 req->request.status = status;
191 musb = req->musb;
192
193 ep->busy = 1;
194 spin_unlock(&musb->lock);
195 unmap_dma_buffer(req, musb);
196 if (request->status == 0)
197 dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
198 ep->end_point.name, request,
199 req->request.actual, req->request.length);
200 else
201 dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
202 ep->end_point.name, request,
203 req->request.actual, req->request.length,
204 request->status);
205 req->request.complete(&req->ep->end_point, &req->request);
206 spin_lock(&musb->lock);
207 ep->busy = busy;
208}
209
210/* ----------------------------------------------------------------------- */
211
212/*
213 * Abort requests queued to an endpoint using the status. Synchronous.
214 * caller locked controller and blocked irqs, and selected this ep.
215 */
216static void nuke(struct musb_ep *ep, const int status)
217{
218 struct musb *musb = ep->musb;
219 struct musb_request *req = NULL;
220 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
221
222 ep->busy = 1;
223
224 if (is_dma_capable() && ep->dma) {
225 struct dma_controller *c = ep->musb->dma_controller;
226 int value;
227
228 if (ep->is_in) {
229 /*
230 * The programming guide says that we must not clear
231 * the DMAMODE bit before DMAENAB, so we only
232 * clear it in the second write...
233 */
234 musb_writew(epio, MUSB_TXCSR,
235 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
236 musb_writew(epio, MUSB_TXCSR,
237 0 | MUSB_TXCSR_FLUSHFIFO);
238 } else {
239 musb_writew(epio, MUSB_RXCSR,
240 0 | MUSB_RXCSR_FLUSHFIFO);
241 musb_writew(epio, MUSB_RXCSR,
242 0 | MUSB_RXCSR_FLUSHFIFO);
243 }
244
245 value = c->channel_abort(ep->dma);
246 dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
247 ep->name, value);
248 c->channel_release(ep->dma);
249 ep->dma = NULL;
250 }
251
252 while (!list_empty(&ep->req_list)) {
253 req = list_first_entry(&ep->req_list, struct musb_request, list);
254 musb_g_giveback(ep, &req->request, status);
255 }
256}
257
258/* ----------------------------------------------------------------------- */
259
260/* Data transfers - pure PIO, pure DMA, or mixed mode */
261
262/*
263 * This assumes the separate CPPI engine is responding to DMA requests
264 * from the usb core ... sequenced a bit differently from mentor dma.
265 */
266
267static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
268{
269 if (can_bulk_split(musb, ep->type))
270 return ep->hw_ep->max_packet_sz_tx;
271 else
272 return ep->packet_sz;
273}
274
275
276#ifdef CONFIG_USB_INVENTRA_DMA
277
278/* Peripheral tx (IN) using Mentor DMA works as follows:
279 Only mode 0 is used for transfers <= wPktSize,
280 mode 1 is used for larger transfers,
281
282 One of the following happens:
283 - Host sends IN token which causes an endpoint interrupt
284 -> TxAvail
285 -> if DMA is currently busy, exit.
286 -> if queue is non-empty, txstate().
287
288 - Request is queued by the gadget driver.
289 -> if queue was previously empty, txstate()
290
291 txstate()
292 -> start
293 /\ -> setup DMA
294 | (data is transferred to the FIFO, then sent out when
295 | IN token(s) are recd from Host.
296 | -> DMA interrupt on completion
297 | calls TxAvail.
298 | -> stop DMA, ~DMAENAB,
299 | -> set TxPktRdy for last short pkt or zlp
300 | -> Complete Request
301 | -> Continue next request (call txstate)
302 |___________________________________|
303
304 * Non-Mentor DMA engines can of course work differently, such as by
305 * upleveling from irq-per-packet to irq-per-buffer.
306 */
307
308#endif
309
310/*
311 * An endpoint is transmitting data. This can be called either from
312 * the IRQ routine or from ep.queue() to kickstart a request on an
313 * endpoint.
314 *
315 * Context: controller locked, IRQs blocked, endpoint selected
316 */
317static void txstate(struct musb *musb, struct musb_request *req)
318{
319 u8 epnum = req->epnum;
320 struct musb_ep *musb_ep;
321 void __iomem *epio = musb->endpoints[epnum].regs;
322 struct usb_request *request;
323 u16 fifo_count = 0, csr;
324 int use_dma = 0;
325
326 musb_ep = req->ep;
327
328 /* Check if EP is disabled */
329 if (!musb_ep->desc) {
330 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
331 musb_ep->end_point.name);
332 return;
333 }
334
335 /* we shouldn't get here while DMA is active ... but we do ... */
336 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
337 dev_dbg(musb->controller, "dma pending...\n");
338 return;
339 }
340
341 /* read TXCSR before */
342 csr = musb_readw(epio, MUSB_TXCSR);
343
344 request = &req->request;
345 fifo_count = min(max_ep_writesize(musb, musb_ep),
346 (int)(request->length - request->actual));
347
348 if (csr & MUSB_TXCSR_TXPKTRDY) {
349 dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
350 musb_ep->end_point.name, csr);
351 return;
352 }
353
354 if (csr & MUSB_TXCSR_P_SENDSTALL) {
355 dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
356 musb_ep->end_point.name, csr);
357 return;
358 }
359
360 dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
361 epnum, musb_ep->packet_sz, fifo_count,
362 csr);
363
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200364#ifndef CONFIG_USB_MUSB_PIO_ONLY
Ilya Yanok06bb9202012-11-06 13:48:21 +0000365 if (is_buffer_mapped(req)) {
366 struct dma_controller *c = musb->dma_controller;
367 size_t request_size;
368
369 /* setup DMA, then program endpoint CSR */
370 request_size = min_t(size_t, request->length - request->actual,
371 musb_ep->dma->max_len);
372
373 use_dma = (request->dma != DMA_ADDR_INVALID);
374
375 /* MUSB_TXCSR_P_ISO is still set correctly */
376
377#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
378 {
379 if (request_size < musb_ep->packet_sz)
380 musb_ep->dma->desired_mode = 0;
381 else
382 musb_ep->dma->desired_mode = 1;
383
384 use_dma = use_dma && c->channel_program(
385 musb_ep->dma, musb_ep->packet_sz,
386 musb_ep->dma->desired_mode,
387 request->dma + request->actual, request_size);
388 if (use_dma) {
389 if (musb_ep->dma->desired_mode == 0) {
390 /*
391 * We must not clear the DMAMODE bit
392 * before the DMAENAB bit -- and the
393 * latter doesn't always get cleared
394 * before we get here...
395 */
396 csr &= ~(MUSB_TXCSR_AUTOSET
397 | MUSB_TXCSR_DMAENAB);
398 musb_writew(epio, MUSB_TXCSR, csr
399 | MUSB_TXCSR_P_WZC_BITS);
400 csr &= ~MUSB_TXCSR_DMAMODE;
401 csr |= (MUSB_TXCSR_DMAENAB |
402 MUSB_TXCSR_MODE);
403 /* against programming guide */
404 } else {
405 csr |= (MUSB_TXCSR_DMAENAB
406 | MUSB_TXCSR_DMAMODE
407 | MUSB_TXCSR_MODE);
408 if (!musb_ep->hb_mult)
409 csr |= MUSB_TXCSR_AUTOSET;
410 }
411 csr &= ~MUSB_TXCSR_P_UNDERRUN;
412
413 musb_writew(epio, MUSB_TXCSR, csr);
414 }
415 }
416
417#elif defined(CONFIG_USB_TI_CPPI_DMA)
418 /* program endpoint CSR first, then setup DMA */
419 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
420 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
421 MUSB_TXCSR_MODE;
422 musb_writew(epio, MUSB_TXCSR,
423 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
424 | csr);
425
426 /* ensure writebuffer is empty */
427 csr = musb_readw(epio, MUSB_TXCSR);
428
429 /* NOTE host side sets DMAENAB later than this; both are
430 * OK since the transfer dma glue (between CPPI and Mentor
431 * fifos) just tells CPPI it could start. Data only moves
432 * to the USB TX fifo when both fifos are ready.
433 */
434
435 /* "mode" is irrelevant here; handle terminating ZLPs like
436 * PIO does, since the hardware RNDIS mode seems unreliable
437 * except for the last-packet-is-already-short case.
438 */
439 use_dma = use_dma && c->channel_program(
440 musb_ep->dma, musb_ep->packet_sz,
441 0,
442 request->dma + request->actual,
443 request_size);
444 if (!use_dma) {
445 c->channel_release(musb_ep->dma);
446 musb_ep->dma = NULL;
447 csr &= ~MUSB_TXCSR_DMAENAB;
448 musb_writew(epio, MUSB_TXCSR, csr);
449 /* invariant: prequest->buf is non-null */
450 }
451#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
452 use_dma = use_dma && c->channel_program(
453 musb_ep->dma, musb_ep->packet_sz,
454 request->zero,
455 request->dma + request->actual,
456 request_size);
457#endif
458 }
459#endif
460
461 if (!use_dma) {
462 /*
463 * Unmap the dma buffer back to cpu if dma channel
464 * programming fails
465 */
466 unmap_dma_buffer(req, musb);
467
468 musb_write_fifo(musb_ep->hw_ep, fifo_count,
469 (u8 *) (request->buf + request->actual));
470 request->actual += fifo_count;
471 csr |= MUSB_TXCSR_TXPKTRDY;
472 csr &= ~MUSB_TXCSR_P_UNDERRUN;
473 musb_writew(epio, MUSB_TXCSR, csr);
474 }
475
476 /* host may already have the data when this message shows... */
477 dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
478 musb_ep->end_point.name, use_dma ? "dma" : "pio",
479 request->actual, request->length,
480 musb_readw(epio, MUSB_TXCSR),
481 fifo_count,
482 musb_readw(epio, MUSB_TXMAXP));
483}
484
485/*
486 * FIFO state update (e.g. data ready).
487 * Called from IRQ, with controller locked.
488 */
489void musb_g_tx(struct musb *musb, u8 epnum)
490{
491 u16 csr;
492 struct musb_request *req;
493 struct usb_request *request;
494 u8 __iomem *mbase = musb->mregs;
495 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
496 void __iomem *epio = musb->endpoints[epnum].regs;
497 struct dma_channel *dma;
498
499 musb_ep_select(mbase, epnum);
500 req = next_request(musb_ep);
501 request = &req->request;
502
503 csr = musb_readw(epio, MUSB_TXCSR);
504 dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
505
506 dma = is_dma_capable() ? musb_ep->dma : NULL;
507
508 /*
509 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
510 * probably rates reporting as a host error.
511 */
512 if (csr & MUSB_TXCSR_P_SENTSTALL) {
513 csr |= MUSB_TXCSR_P_WZC_BITS;
514 csr &= ~MUSB_TXCSR_P_SENTSTALL;
515 musb_writew(epio, MUSB_TXCSR, csr);
516 return;
517 }
518
519 if (csr & MUSB_TXCSR_P_UNDERRUN) {
520 /* We NAKed, no big deal... little reason to care. */
521 csr |= MUSB_TXCSR_P_WZC_BITS;
522 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
523 musb_writew(epio, MUSB_TXCSR, csr);
524 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
525 epnum, request);
526 }
527
528 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
529 /*
530 * SHOULD NOT HAPPEN... has with CPPI though, after
531 * changing SENDSTALL (and other cases); harmless?
532 */
533 dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
534 return;
535 }
536
537 if (request) {
538 u8 is_dma = 0;
539
540 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
541 is_dma = 1;
542 csr |= MUSB_TXCSR_P_WZC_BITS;
543 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
544 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
545 musb_writew(epio, MUSB_TXCSR, csr);
546 /* Ensure writebuffer is empty. */
547 csr = musb_readw(epio, MUSB_TXCSR);
548 request->actual += musb_ep->dma->actual_len;
549 dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
550 epnum, csr, musb_ep->dma->actual_len, request);
551 }
552
553 /*
554 * First, maybe a terminating short packet. Some DMA
555 * engines might handle this by themselves.
556 */
557 if ((request->zero && request->length
558 && (request->length % musb_ep->packet_sz == 0)
559 && (request->actual == request->length))
560#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
561 || (is_dma && (!dma->desired_mode ||
562 (request->actual &
563 (musb_ep->packet_sz - 1))))
564#endif
565 ) {
566 /*
567 * On DMA completion, FIFO may not be
568 * available yet...
569 */
570 if (csr & MUSB_TXCSR_TXPKTRDY)
571 return;
572
573 dev_dbg(musb->controller, "sending zero pkt\n");
574 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
575 | MUSB_TXCSR_TXPKTRDY);
576 request->zero = 0;
577 }
578
579 if (request->actual == request->length) {
580 musb_g_giveback(musb_ep, request, 0);
581 /*
582 * In the giveback function the MUSB lock is
583 * released and acquired after sometime. During
584 * this time period the INDEX register could get
585 * changed by the gadget_queue function especially
586 * on SMP systems. Reselect the INDEX to be sure
587 * we are reading/modifying the right registers
588 */
589 musb_ep_select(mbase, epnum);
590 req = musb_ep->desc ? next_request(musb_ep) : NULL;
591 if (!req) {
592 dev_dbg(musb->controller, "%s idle now\n",
593 musb_ep->end_point.name);
594 return;
595 }
596 }
597
598 txstate(musb, req);
599 }
600}
601
602/* ------------------------------------------------------------ */
603
604#ifdef CONFIG_USB_INVENTRA_DMA
605
606/* Peripheral rx (OUT) using Mentor DMA works as follows:
607 - Only mode 0 is used.
608
609 - Request is queued by the gadget class driver.
610 -> if queue was previously empty, rxstate()
611
612 - Host sends OUT token which causes an endpoint interrupt
613 /\ -> RxReady
614 | -> if request queued, call rxstate
615 | /\ -> setup DMA
616 | | -> DMA interrupt on completion
617 | | -> RxReady
618 | | -> stop DMA
619 | | -> ack the read
620 | | -> if data recd = max expected
621 | | by the request, or host
622 | | sent a short packet,
623 | | complete the request,
624 | | and start the next one.
625 | |_____________________________________|
626 | else just wait for the host
627 | to send the next OUT token.
628 |__________________________________________________|
629
630 * Non-Mentor DMA engines can of course work differently.
631 */
632
633#endif
634
635/*
636 * Context: controller locked, IRQs blocked, endpoint selected
637 */
638static void rxstate(struct musb *musb, struct musb_request *req)
639{
640 const u8 epnum = req->epnum;
641 struct usb_request *request = &req->request;
642 struct musb_ep *musb_ep;
643 void __iomem *epio = musb->endpoints[epnum].regs;
644 unsigned fifo_count = 0;
645 u16 len;
646 u16 csr = musb_readw(epio, MUSB_RXCSR);
647 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
648 u8 use_mode_1;
649
650 if (hw_ep->is_shared_fifo)
651 musb_ep = &hw_ep->ep_in;
652 else
653 musb_ep = &hw_ep->ep_out;
654
655 len = musb_ep->packet_sz;
656
657 /* Check if EP is disabled */
658 if (!musb_ep->desc) {
659 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
660 musb_ep->end_point.name);
661 return;
662 }
663
664 /* We shouldn't get here while DMA is active, but we do... */
665 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
666 dev_dbg(musb->controller, "DMA pending...\n");
667 return;
668 }
669
670 if (csr & MUSB_RXCSR_P_SENDSTALL) {
671 dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
672 musb_ep->end_point.name, csr);
673 return;
674 }
675
676 if (is_cppi_enabled() && is_buffer_mapped(req)) {
677 struct dma_controller *c = musb->dma_controller;
678 struct dma_channel *channel = musb_ep->dma;
679
680 /* NOTE: CPPI won't actually stop advancing the DMA
681 * queue after short packet transfers, so this is almost
682 * always going to run as IRQ-per-packet DMA so that
683 * faults will be handled correctly.
684 */
685 if (c->channel_program(channel,
686 musb_ep->packet_sz,
687 !request->short_not_ok,
688 request->dma + request->actual,
689 request->length - request->actual)) {
690
691 /* make sure that if an rxpkt arrived after the irq,
692 * the cppi engine will be ready to take it as soon
693 * as DMA is enabled
694 */
695 csr &= ~(MUSB_RXCSR_AUTOCLEAR
696 | MUSB_RXCSR_DMAMODE);
697 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
698 musb_writew(epio, MUSB_RXCSR, csr);
699 return;
700 }
701 }
702
703 if (csr & MUSB_RXCSR_RXPKTRDY) {
704 len = musb_readw(epio, MUSB_RXCOUNT);
705
706 /*
707 * Enable Mode 1 on RX transfers only when short_not_ok flag
708 * is set. Currently short_not_ok flag is set only from
709 * file_storage and f_mass_storage drivers
710 */
711
712 if (request->short_not_ok && len == musb_ep->packet_sz)
713 use_mode_1 = 1;
714 else
715 use_mode_1 = 0;
716
717 if (request->actual < request->length) {
718#ifdef CONFIG_USB_INVENTRA_DMA
719 if (is_buffer_mapped(req)) {
720 struct dma_controller *c;
721 struct dma_channel *channel;
722 int use_dma = 0;
723
724 c = musb->dma_controller;
725 channel = musb_ep->dma;
726
727 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
728 * mode 0 only. So we do not get endpoint interrupts due to DMA
729 * completion. We only get interrupts from DMA controller.
730 *
731 * We could operate in DMA mode 1 if we knew the size of the tranfer
732 * in advance. For mass storage class, request->length = what the host
733 * sends, so that'd work. But for pretty much everything else,
734 * request->length is routinely more than what the host sends. For
735 * most these gadgets, end of is signified either by a short packet,
736 * or filling the last byte of the buffer. (Sending extra data in
737 * that last pckate should trigger an overflow fault.) But in mode 1,
738 * we don't get DMA completion interrupt for short packets.
739 *
740 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
741 * to get endpoint interrupt on every DMA req, but that didn't seem
742 * to work reliably.
743 *
744 * REVISIT an updated g_file_storage can set req->short_not_ok, which
745 * then becomes usable as a runtime "use mode 1" hint...
746 */
747
748 /* Experimental: Mode1 works with mass storage use cases */
749 if (use_mode_1) {
750 csr |= MUSB_RXCSR_AUTOCLEAR;
751 musb_writew(epio, MUSB_RXCSR, csr);
752 csr |= MUSB_RXCSR_DMAENAB;
753 musb_writew(epio, MUSB_RXCSR, csr);
754
755 /*
756 * this special sequence (enabling and then
757 * disabling MUSB_RXCSR_DMAMODE) is required
758 * to get DMAReq to activate
759 */
760 musb_writew(epio, MUSB_RXCSR,
761 csr | MUSB_RXCSR_DMAMODE);
762 musb_writew(epio, MUSB_RXCSR, csr);
763
764 } else {
765 if (!musb_ep->hb_mult &&
766 musb_ep->hw_ep->rx_double_buffered)
767 csr |= MUSB_RXCSR_AUTOCLEAR;
768 csr |= MUSB_RXCSR_DMAENAB;
769 musb_writew(epio, MUSB_RXCSR, csr);
770 }
771
772 if (request->actual < request->length) {
773 int transfer_size = 0;
774 if (use_mode_1) {
775 transfer_size = min(request->length - request->actual,
776 channel->max_len);
777 musb_ep->dma->desired_mode = 1;
778 } else {
779 transfer_size = min(request->length - request->actual,
780 (unsigned)len);
781 musb_ep->dma->desired_mode = 0;
782 }
783
784 use_dma = c->channel_program(
785 channel,
786 musb_ep->packet_sz,
787 channel->desired_mode,
788 request->dma
789 + request->actual,
790 transfer_size);
791 }
792
793 if (use_dma)
794 return;
795 }
796#elif defined(CONFIG_USB_UX500_DMA)
797 if ((is_buffer_mapped(req)) &&
798 (request->actual < request->length)) {
799
800 struct dma_controller *c;
801 struct dma_channel *channel;
802 int transfer_size = 0;
803
804 c = musb->dma_controller;
805 channel = musb_ep->dma;
806
807 /* In case first packet is short */
808 if (len < musb_ep->packet_sz)
809 transfer_size = len;
810 else if (request->short_not_ok)
811 transfer_size = min(request->length -
812 request->actual,
813 channel->max_len);
814 else
815 transfer_size = min(request->length -
816 request->actual,
817 (unsigned)len);
818
819 csr &= ~MUSB_RXCSR_DMAMODE;
820 csr |= (MUSB_RXCSR_DMAENAB |
821 MUSB_RXCSR_AUTOCLEAR);
822
823 musb_writew(epio, MUSB_RXCSR, csr);
824
825 if (transfer_size <= musb_ep->packet_sz) {
826 musb_ep->dma->desired_mode = 0;
827 } else {
828 musb_ep->dma->desired_mode = 1;
829 /* Mode must be set after DMAENAB */
830 csr |= MUSB_RXCSR_DMAMODE;
831 musb_writew(epio, MUSB_RXCSR, csr);
832 }
833
834 if (c->channel_program(channel,
835 musb_ep->packet_sz,
836 channel->desired_mode,
837 request->dma
838 + request->actual,
839 transfer_size))
840
841 return;
842 }
843#endif /* Mentor's DMA */
844
845 fifo_count = request->length - request->actual;
846 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
847 musb_ep->end_point.name,
848 len, fifo_count,
849 musb_ep->packet_sz);
850
851 fifo_count = min_t(unsigned, len, fifo_count);
852
853#ifdef CONFIG_USB_TUSB_OMAP_DMA
854 if (tusb_dma_omap() && is_buffer_mapped(req)) {
855 struct dma_controller *c = musb->dma_controller;
856 struct dma_channel *channel = musb_ep->dma;
857 u32 dma_addr = request->dma + request->actual;
858 int ret;
859
860 ret = c->channel_program(channel,
861 musb_ep->packet_sz,
862 channel->desired_mode,
863 dma_addr,
864 fifo_count);
865 if (ret)
866 return;
867 }
868#endif
869 /*
870 * Unmap the dma buffer back to cpu if dma channel
871 * programming fails. This buffer is mapped if the
872 * channel allocation is successful
873 */
874 if (is_buffer_mapped(req)) {
875 unmap_dma_buffer(req, musb);
876
877 /*
878 * Clear DMAENAB and AUTOCLEAR for the
879 * PIO mode transfer
880 */
881 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
882 musb_writew(epio, MUSB_RXCSR, csr);
883 }
884
885 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
886 (request->buf + request->actual));
887 request->actual += fifo_count;
888
889 /* REVISIT if we left anything in the fifo, flush
890 * it and report -EOVERFLOW
891 */
892
893 /* ack the read! */
894 csr |= MUSB_RXCSR_P_WZC_BITS;
895 csr &= ~MUSB_RXCSR_RXPKTRDY;
896 musb_writew(epio, MUSB_RXCSR, csr);
897 }
898 }
899
900 /* reach the end or short packet detected */
901 if (request->actual == request->length || len < musb_ep->packet_sz)
902 musb_g_giveback(musb_ep, request, 0);
903}
904
905/*
906 * Data ready for a request; called from IRQ
907 */
908void musb_g_rx(struct musb *musb, u8 epnum)
909{
910 u16 csr;
911 struct musb_request *req;
912 struct usb_request *request;
913 void __iomem *mbase = musb->mregs;
914 struct musb_ep *musb_ep;
915 void __iomem *epio = musb->endpoints[epnum].regs;
916 struct dma_channel *dma;
917 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
918
919 if (hw_ep->is_shared_fifo)
920 musb_ep = &hw_ep->ep_in;
921 else
922 musb_ep = &hw_ep->ep_out;
923
924 musb_ep_select(mbase, epnum);
925
926 req = next_request(musb_ep);
927 if (!req)
928 return;
929
930 request = &req->request;
931
932 csr = musb_readw(epio, MUSB_RXCSR);
933 dma = is_dma_capable() ? musb_ep->dma : NULL;
934
935 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
936 csr, dma ? " (dma)" : "", request);
937
938 if (csr & MUSB_RXCSR_P_SENTSTALL) {
939 csr |= MUSB_RXCSR_P_WZC_BITS;
940 csr &= ~MUSB_RXCSR_P_SENTSTALL;
941 musb_writew(epio, MUSB_RXCSR, csr);
942 return;
943 }
944
945 if (csr & MUSB_RXCSR_P_OVERRUN) {
946 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
947 csr &= ~MUSB_RXCSR_P_OVERRUN;
948 musb_writew(epio, MUSB_RXCSR, csr);
949
950 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
951 if (request->status == -EINPROGRESS)
952 request->status = -EOVERFLOW;
953 }
954 if (csr & MUSB_RXCSR_INCOMPRX) {
955 /* REVISIT not necessarily an error */
956 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
957 }
958
959 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
960 /* "should not happen"; likely RXPKTRDY pending for DMA */
961 dev_dbg(musb->controller, "%s busy, csr %04x\n",
962 musb_ep->end_point.name, csr);
963 return;
964 }
965
966 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
967 csr &= ~(MUSB_RXCSR_AUTOCLEAR
968 | MUSB_RXCSR_DMAENAB
969 | MUSB_RXCSR_DMAMODE);
970 musb_writew(epio, MUSB_RXCSR,
971 MUSB_RXCSR_P_WZC_BITS | csr);
972
973 request->actual += musb_ep->dma->actual_len;
974
975 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
976 epnum, csr,
977 musb_readw(epio, MUSB_RXCSR),
978 musb_ep->dma->actual_len, request);
979
980#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
981 defined(CONFIG_USB_UX500_DMA)
982 /* Autoclear doesn't clear RxPktRdy for short packets */
983 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
984 || (dma->actual_len
985 & (musb_ep->packet_sz - 1))) {
986 /* ack the read! */
987 csr &= ~MUSB_RXCSR_RXPKTRDY;
988 musb_writew(epio, MUSB_RXCSR, csr);
989 }
990
991 /* incomplete, and not short? wait for next IN packet */
992 if ((request->actual < request->length)
993 && (musb_ep->dma->actual_len
994 == musb_ep->packet_sz)) {
995 /* In double buffer case, continue to unload fifo if
996 * there is Rx packet in FIFO.
997 **/
998 csr = musb_readw(epio, MUSB_RXCSR);
999 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
1000 hw_ep->rx_double_buffered)
1001 goto exit;
1002 return;
1003 }
1004#endif
1005 musb_g_giveback(musb_ep, request, 0);
1006 /*
1007 * In the giveback function the MUSB lock is
1008 * released and acquired after sometime. During
1009 * this time period the INDEX register could get
1010 * changed by the gadget_queue function especially
1011 * on SMP systems. Reselect the INDEX to be sure
1012 * we are reading/modifying the right registers
1013 */
1014 musb_ep_select(mbase, epnum);
1015
1016 req = next_request(musb_ep);
1017 if (!req)
1018 return;
1019 }
1020#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
1021 defined(CONFIG_USB_UX500_DMA)
1022exit:
1023#endif
1024 /* Analyze request */
1025 rxstate(musb, req);
1026}
1027
1028/* ------------------------------------------------------------ */
1029
1030static int musb_gadget_enable(struct usb_ep *ep,
1031 const struct usb_endpoint_descriptor *desc)
1032{
1033 unsigned long flags;
1034 struct musb_ep *musb_ep;
1035 struct musb_hw_ep *hw_ep;
1036 void __iomem *regs;
1037 struct musb *musb;
1038 void __iomem *mbase;
1039 u8 epnum;
1040 u16 csr;
1041 unsigned tmp;
1042 int status = -EINVAL;
1043
1044 if (!ep || !desc)
1045 return -EINVAL;
1046
1047 musb_ep = to_musb_ep(ep);
1048 hw_ep = musb_ep->hw_ep;
1049 regs = hw_ep->regs;
1050 musb = musb_ep->musb;
1051 mbase = musb->mregs;
1052 epnum = musb_ep->current_epnum;
1053
1054 spin_lock_irqsave(&musb->lock, flags);
1055
1056 if (musb_ep->desc) {
1057 status = -EBUSY;
1058 goto fail;
1059 }
1060 musb_ep->type = usb_endpoint_type(desc);
1061
1062 /* check direction and (later) maxpacket size against endpoint */
1063 if (usb_endpoint_num(desc) != epnum)
1064 goto fail;
1065
1066 /* REVISIT this rules out high bandwidth periodic transfers */
1067 tmp = usb_endpoint_maxp(desc);
1068 if (tmp & ~0x07ff) {
1069 int ok;
1070
1071 if (usb_endpoint_dir_in(desc))
1072 ok = musb->hb_iso_tx;
1073 else
1074 ok = musb->hb_iso_rx;
1075
1076 if (!ok) {
1077 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
1078 goto fail;
1079 }
1080 musb_ep->hb_mult = (tmp >> 11) & 3;
1081 } else {
1082 musb_ep->hb_mult = 0;
1083 }
1084
1085 musb_ep->packet_sz = tmp & 0x7ff;
1086 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
1087
1088 /* enable the interrupts for the endpoint, set the endpoint
1089 * packet size (or fail), set the mode, clear the fifo
1090 */
1091 musb_ep_select(mbase, epnum);
1092 if (usb_endpoint_dir_in(desc)) {
1093 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1094
1095 if (hw_ep->is_shared_fifo)
1096 musb_ep->is_in = 1;
1097 if (!musb_ep->is_in)
1098 goto fail;
1099
1100 if (tmp > hw_ep->max_packet_sz_tx) {
1101 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1102 goto fail;
1103 }
1104
1105 int_txe |= (1 << epnum);
1106 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1107
1108 /* REVISIT if can_bulk_split(), use by updating "tmp";
1109 * likewise high bandwidth periodic tx
1110 */
1111 /* Set TXMAXP with the FIFO size of the endpoint
1112 * to disable double buffering mode.
1113 */
1114 if (musb->double_buffer_not_ok)
1115 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
1116 else
1117 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1118 | (musb_ep->hb_mult << 11));
1119
1120 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1121 if (musb_readw(regs, MUSB_TXCSR)
1122 & MUSB_TXCSR_FIFONOTEMPTY)
1123 csr |= MUSB_TXCSR_FLUSHFIFO;
1124 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1125 csr |= MUSB_TXCSR_P_ISO;
1126
1127 /* set twice in case of double buffering */
1128 musb_writew(regs, MUSB_TXCSR, csr);
1129 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1130 musb_writew(regs, MUSB_TXCSR, csr);
1131
1132 } else {
1133 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
1134
1135 if (hw_ep->is_shared_fifo)
1136 musb_ep->is_in = 0;
1137 if (musb_ep->is_in)
1138 goto fail;
1139
1140 if (tmp > hw_ep->max_packet_sz_rx) {
1141 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1142 goto fail;
1143 }
1144
1145 int_rxe |= (1 << epnum);
1146 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
1147
1148 /* REVISIT if can_bulk_combine() use by updating "tmp"
1149 * likewise high bandwidth periodic rx
1150 */
1151 /* Set RXMAXP with the FIFO size of the endpoint
1152 * to disable double buffering mode.
1153 */
1154 if (musb->double_buffer_not_ok)
1155 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1156 else
1157 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1158 | (musb_ep->hb_mult << 11));
1159
1160 /* force shared fifo to OUT-only mode */
1161 if (hw_ep->is_shared_fifo) {
1162 csr = musb_readw(regs, MUSB_TXCSR);
1163 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1164 musb_writew(regs, MUSB_TXCSR, csr);
1165 }
1166
1167 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1168 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1169 csr |= MUSB_RXCSR_P_ISO;
1170 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1171 csr |= MUSB_RXCSR_DISNYET;
1172
1173 /* set twice in case of double buffering */
1174 musb_writew(regs, MUSB_RXCSR, csr);
1175 musb_writew(regs, MUSB_RXCSR, csr);
1176 }
1177
1178 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1179 * for some reason you run out of channels here.
1180 */
1181 if (is_dma_capable() && musb->dma_controller) {
1182 struct dma_controller *c = musb->dma_controller;
1183
1184 musb_ep->dma = c->channel_alloc(c, hw_ep,
1185 (desc->bEndpointAddress & USB_DIR_IN));
1186 } else
1187 musb_ep->dma = NULL;
1188
1189 musb_ep->desc = desc;
1190 musb_ep->busy = 0;
1191 musb_ep->wedged = 0;
1192 status = 0;
1193
1194 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1195 musb_driver_name, musb_ep->end_point.name,
1196 ({ char *s; switch (musb_ep->type) {
1197 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1198 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1199 default: s = "iso"; break;
1200 }; s; }),
1201 musb_ep->is_in ? "IN" : "OUT",
1202 musb_ep->dma ? "dma, " : "",
1203 musb_ep->packet_sz);
1204
1205 schedule_work(&musb->irq_work);
1206
1207fail:
1208 spin_unlock_irqrestore(&musb->lock, flags);
1209 return status;
1210}
1211
1212/*
1213 * Disable an endpoint flushing all requests queued.
1214 */
1215static int musb_gadget_disable(struct usb_ep *ep)
1216{
1217 unsigned long flags;
1218 struct musb *musb;
1219 u8 epnum;
1220 struct musb_ep *musb_ep;
1221 void __iomem *epio;
1222 int status = 0;
1223
1224 musb_ep = to_musb_ep(ep);
1225 musb = musb_ep->musb;
1226 epnum = musb_ep->current_epnum;
1227 epio = musb->endpoints[epnum].regs;
1228
1229 spin_lock_irqsave(&musb->lock, flags);
1230 musb_ep_select(musb->mregs, epnum);
1231
1232 /* zero the endpoint sizes */
1233 if (musb_ep->is_in) {
1234 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1235 int_txe &= ~(1 << epnum);
1236 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1237 musb_writew(epio, MUSB_TXMAXP, 0);
1238 } else {
1239 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1240 int_rxe &= ~(1 << epnum);
1241 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1242 musb_writew(epio, MUSB_RXMAXP, 0);
1243 }
1244
1245 musb_ep->desc = NULL;
1246#ifndef __UBOOT__
1247 musb_ep->end_point.desc = NULL;
1248#endif
1249
1250 /* abort all pending DMA and requests */
1251 nuke(musb_ep, -ESHUTDOWN);
1252
1253 schedule_work(&musb->irq_work);
1254
1255 spin_unlock_irqrestore(&(musb->lock), flags);
1256
1257 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
1258
1259 return status;
1260}
1261
1262/*
1263 * Allocate a request for an endpoint.
1264 * Reused by ep0 code.
1265 */
1266struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1267{
1268 struct musb_ep *musb_ep = to_musb_ep(ep);
1269 struct musb *musb = musb_ep->musb;
1270 struct musb_request *request = NULL;
1271
1272 request = kzalloc(sizeof *request, gfp_flags);
1273 if (!request) {
1274 dev_dbg(musb->controller, "not enough memory\n");
1275 return NULL;
1276 }
1277
1278 request->request.dma = DMA_ADDR_INVALID;
1279 request->epnum = musb_ep->current_epnum;
1280 request->ep = musb_ep;
1281
1282 return &request->request;
1283}
1284
1285/*
1286 * Free a request
1287 * Reused by ep0 code.
1288 */
1289void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1290{
1291 kfree(to_musb_request(req));
1292}
1293
1294static LIST_HEAD(buffers);
1295
1296struct free_record {
1297 struct list_head list;
1298 struct device *dev;
1299 unsigned bytes;
1300 dma_addr_t dma;
1301};
1302
1303/*
1304 * Context: controller locked, IRQs blocked.
1305 */
1306void musb_ep_restart(struct musb *musb, struct musb_request *req)
1307{
1308 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
1309 req->tx ? "TX/IN" : "RX/OUT",
1310 &req->request, req->request.length, req->epnum);
1311
1312 musb_ep_select(musb->mregs, req->epnum);
1313 if (req->tx)
1314 txstate(musb, req);
1315 else
1316 rxstate(musb, req);
1317}
1318
1319static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1320 gfp_t gfp_flags)
1321{
1322 struct musb_ep *musb_ep;
1323 struct musb_request *request;
1324 struct musb *musb;
1325 int status = 0;
1326 unsigned long lockflags;
1327
1328 if (!ep || !req)
1329 return -EINVAL;
1330 if (!req->buf)
1331 return -ENODATA;
1332
1333 musb_ep = to_musb_ep(ep);
1334 musb = musb_ep->musb;
1335
1336 request = to_musb_request(req);
1337 request->musb = musb;
1338
1339 if (request->ep != musb_ep)
1340 return -EINVAL;
1341
1342 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
1343
1344 /* request is mine now... */
1345 request->request.actual = 0;
1346 request->request.status = -EINPROGRESS;
1347 request->epnum = musb_ep->current_epnum;
1348 request->tx = musb_ep->is_in;
1349
1350 map_dma_buffer(request, musb, musb_ep);
1351
1352 spin_lock_irqsave(&musb->lock, lockflags);
1353
1354 /* don't queue if the ep is down */
1355 if (!musb_ep->desc) {
1356 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
1357 req, ep->name, "disabled");
1358 status = -ESHUTDOWN;
1359 goto cleanup;
1360 }
1361
1362 /* add request to the list */
1363 list_add_tail(&request->list, &musb_ep->req_list);
1364
1365 /* it this is the head of the queue, start i/o ... */
1366 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
1367 musb_ep_restart(musb, request);
1368
1369cleanup:
1370 spin_unlock_irqrestore(&musb->lock, lockflags);
1371 return status;
1372}
1373
1374static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1375{
1376 struct musb_ep *musb_ep = to_musb_ep(ep);
1377 struct musb_request *req = to_musb_request(request);
1378 struct musb_request *r;
1379 unsigned long flags;
1380 int status = 0;
1381 struct musb *musb = musb_ep->musb;
1382
1383 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1384 return -EINVAL;
1385
1386 spin_lock_irqsave(&musb->lock, flags);
1387
1388 list_for_each_entry(r, &musb_ep->req_list, list) {
1389 if (r == req)
1390 break;
1391 }
1392 if (r != req) {
1393 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
1394 status = -EINVAL;
1395 goto done;
1396 }
1397
1398 /* if the hardware doesn't have the request, easy ... */
1399 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
1400 musb_g_giveback(musb_ep, request, -ECONNRESET);
1401
1402 /* ... else abort the dma transfer ... */
1403 else if (is_dma_capable() && musb_ep->dma) {
1404 struct dma_controller *c = musb->dma_controller;
1405
1406 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1407 if (c->channel_abort)
1408 status = c->channel_abort(musb_ep->dma);
1409 else
1410 status = -EBUSY;
1411 if (status == 0)
1412 musb_g_giveback(musb_ep, request, -ECONNRESET);
1413 } else {
1414 /* NOTE: by sticking to easily tested hardware/driver states,
1415 * we leave counting of in-flight packets imprecise.
1416 */
1417 musb_g_giveback(musb_ep, request, -ECONNRESET);
1418 }
1419
1420done:
1421 spin_unlock_irqrestore(&musb->lock, flags);
1422 return status;
1423}
1424
1425/*
1426 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1427 * data but will queue requests.
1428 *
1429 * exported to ep0 code
1430 */
1431static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1432{
1433 struct musb_ep *musb_ep = to_musb_ep(ep);
1434 u8 epnum = musb_ep->current_epnum;
1435 struct musb *musb = musb_ep->musb;
1436 void __iomem *epio = musb->endpoints[epnum].regs;
1437 void __iomem *mbase;
1438 unsigned long flags;
1439 u16 csr;
1440 struct musb_request *request;
1441 int status = 0;
1442
1443 if (!ep)
1444 return -EINVAL;
1445 mbase = musb->mregs;
1446
1447 spin_lock_irqsave(&musb->lock, flags);
1448
1449 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1450 status = -EINVAL;
1451 goto done;
1452 }
1453
1454 musb_ep_select(mbase, epnum);
1455
1456 request = next_request(musb_ep);
1457 if (value) {
1458 if (request) {
1459 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
1460 ep->name);
1461 status = -EAGAIN;
1462 goto done;
1463 }
1464 /* Cannot portably stall with non-empty FIFO */
1465 if (musb_ep->is_in) {
1466 csr = musb_readw(epio, MUSB_TXCSR);
1467 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1468 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
1469 status = -EAGAIN;
1470 goto done;
1471 }
1472 }
1473 } else
1474 musb_ep->wedged = 0;
1475
1476 /* set/clear the stall and toggle bits */
1477 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
1478 if (musb_ep->is_in) {
1479 csr = musb_readw(epio, MUSB_TXCSR);
1480 csr |= MUSB_TXCSR_P_WZC_BITS
1481 | MUSB_TXCSR_CLRDATATOG;
1482 if (value)
1483 csr |= MUSB_TXCSR_P_SENDSTALL;
1484 else
1485 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1486 | MUSB_TXCSR_P_SENTSTALL);
1487 csr &= ~MUSB_TXCSR_TXPKTRDY;
1488 musb_writew(epio, MUSB_TXCSR, csr);
1489 } else {
1490 csr = musb_readw(epio, MUSB_RXCSR);
1491 csr |= MUSB_RXCSR_P_WZC_BITS
1492 | MUSB_RXCSR_FLUSHFIFO
1493 | MUSB_RXCSR_CLRDATATOG;
1494 if (value)
1495 csr |= MUSB_RXCSR_P_SENDSTALL;
1496 else
1497 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1498 | MUSB_RXCSR_P_SENTSTALL);
1499 musb_writew(epio, MUSB_RXCSR, csr);
1500 }
1501
1502 /* maybe start the first request in the queue */
1503 if (!musb_ep->busy && !value && request) {
1504 dev_dbg(musb->controller, "restarting the request\n");
1505 musb_ep_restart(musb, request);
1506 }
1507
1508done:
1509 spin_unlock_irqrestore(&musb->lock, flags);
1510 return status;
1511}
1512
1513#ifndef __UBOOT__
1514/*
1515 * Sets the halt feature with the clear requests ignored
1516 */
1517static int musb_gadget_set_wedge(struct usb_ep *ep)
1518{
1519 struct musb_ep *musb_ep = to_musb_ep(ep);
1520
1521 if (!ep)
1522 return -EINVAL;
1523
1524 musb_ep->wedged = 1;
1525
1526 return usb_ep_set_halt(ep);
1527}
1528#endif
1529
1530static int musb_gadget_fifo_status(struct usb_ep *ep)
1531{
1532 struct musb_ep *musb_ep = to_musb_ep(ep);
1533 void __iomem *epio = musb_ep->hw_ep->regs;
1534 int retval = -EINVAL;
1535
1536 if (musb_ep->desc && !musb_ep->is_in) {
1537 struct musb *musb = musb_ep->musb;
1538 int epnum = musb_ep->current_epnum;
1539 void __iomem *mbase = musb->mregs;
1540 unsigned long flags;
1541
1542 spin_lock_irqsave(&musb->lock, flags);
1543
1544 musb_ep_select(mbase, epnum);
1545 /* FIXME return zero unless RXPKTRDY is set */
1546 retval = musb_readw(epio, MUSB_RXCOUNT);
1547
1548 spin_unlock_irqrestore(&musb->lock, flags);
1549 }
1550 return retval;
1551}
1552
1553static void musb_gadget_fifo_flush(struct usb_ep *ep)
1554{
1555 struct musb_ep *musb_ep = to_musb_ep(ep);
1556 struct musb *musb = musb_ep->musb;
1557 u8 epnum = musb_ep->current_epnum;
1558 void __iomem *epio = musb->endpoints[epnum].regs;
1559 void __iomem *mbase;
1560 unsigned long flags;
1561 u16 csr, int_txe;
1562
1563 mbase = musb->mregs;
1564
1565 spin_lock_irqsave(&musb->lock, flags);
1566 musb_ep_select(mbase, (u8) epnum);
1567
1568 /* disable interrupts */
1569 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1570 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1571
1572 if (musb_ep->is_in) {
1573 csr = musb_readw(epio, MUSB_TXCSR);
1574 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1575 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1576 /*
1577 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1578 * to interrupt current FIFO loading, but not flushing
1579 * the already loaded ones.
1580 */
1581 csr &= ~MUSB_TXCSR_TXPKTRDY;
1582 musb_writew(epio, MUSB_TXCSR, csr);
1583 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1584 musb_writew(epio, MUSB_TXCSR, csr);
1585 }
1586 } else {
1587 csr = musb_readw(epio, MUSB_RXCSR);
1588 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1589 musb_writew(epio, MUSB_RXCSR, csr);
1590 musb_writew(epio, MUSB_RXCSR, csr);
1591 }
1592
1593 /* re-enable interrupt */
1594 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1595 spin_unlock_irqrestore(&musb->lock, flags);
1596}
1597
1598static const struct usb_ep_ops musb_ep_ops = {
1599 .enable = musb_gadget_enable,
1600 .disable = musb_gadget_disable,
1601 .alloc_request = musb_alloc_request,
1602 .free_request = musb_free_request,
1603 .queue = musb_gadget_queue,
1604 .dequeue = musb_gadget_dequeue,
1605 .set_halt = musb_gadget_set_halt,
1606#ifndef __UBOOT__
1607 .set_wedge = musb_gadget_set_wedge,
1608#endif
1609 .fifo_status = musb_gadget_fifo_status,
1610 .fifo_flush = musb_gadget_fifo_flush
1611};
1612
1613/* ----------------------------------------------------------------------- */
1614
1615static int musb_gadget_get_frame(struct usb_gadget *gadget)
1616{
1617 struct musb *musb = gadget_to_musb(gadget);
1618
1619 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1620}
1621
1622static int musb_gadget_wakeup(struct usb_gadget *gadget)
1623{
1624#ifndef __UBOOT__
1625 struct musb *musb = gadget_to_musb(gadget);
1626 void __iomem *mregs = musb->mregs;
1627 unsigned long flags;
1628 int status = -EINVAL;
1629 u8 power, devctl;
1630 int retries;
1631
1632 spin_lock_irqsave(&musb->lock, flags);
1633
1634 switch (musb->xceiv->state) {
1635 case OTG_STATE_B_PERIPHERAL:
1636 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1637 * that's part of the standard usb 1.1 state machine, and
1638 * doesn't affect OTG transitions.
1639 */
1640 if (musb->may_wakeup && musb->is_suspended)
1641 break;
1642 goto done;
1643 case OTG_STATE_B_IDLE:
1644 /* Start SRP ... OTG not required. */
1645 devctl = musb_readb(mregs, MUSB_DEVCTL);
1646 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
1647 devctl |= MUSB_DEVCTL_SESSION;
1648 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1649 devctl = musb_readb(mregs, MUSB_DEVCTL);
1650 retries = 100;
1651 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1652 devctl = musb_readb(mregs, MUSB_DEVCTL);
1653 if (retries-- < 1)
1654 break;
1655 }
1656 retries = 10000;
1657 while (devctl & MUSB_DEVCTL_SESSION) {
1658 devctl = musb_readb(mregs, MUSB_DEVCTL);
1659 if (retries-- < 1)
1660 break;
1661 }
1662
1663 spin_unlock_irqrestore(&musb->lock, flags);
1664 otg_start_srp(musb->xceiv->otg);
1665 spin_lock_irqsave(&musb->lock, flags);
1666
1667 /* Block idling for at least 1s */
1668 musb_platform_try_idle(musb,
1669 jiffies + msecs_to_jiffies(1 * HZ));
1670
1671 status = 0;
1672 goto done;
1673 default:
1674 dev_dbg(musb->controller, "Unhandled wake: %s\n",
1675 otg_state_string(musb->xceiv->state));
1676 goto done;
1677 }
1678
1679 status = 0;
1680
1681 power = musb_readb(mregs, MUSB_POWER);
1682 power |= MUSB_POWER_RESUME;
1683 musb_writeb(mregs, MUSB_POWER, power);
1684 dev_dbg(musb->controller, "issue wakeup\n");
1685
1686 /* FIXME do this next chunk in a timer callback, no udelay */
1687 mdelay(2);
1688
1689 power = musb_readb(mregs, MUSB_POWER);
1690 power &= ~MUSB_POWER_RESUME;
1691 musb_writeb(mregs, MUSB_POWER, power);
1692done:
1693 spin_unlock_irqrestore(&musb->lock, flags);
1694 return status;
1695#else
1696 return 0;
1697#endif
1698}
1699
1700static int
1701musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1702{
1703 struct musb *musb = gadget_to_musb(gadget);
1704
1705 musb->is_self_powered = !!is_selfpowered;
1706 return 0;
1707}
1708
1709static void musb_pullup(struct musb *musb, int is_on)
1710{
1711 u8 power;
1712
1713 power = musb_readb(musb->mregs, MUSB_POWER);
1714 if (is_on)
1715 power |= MUSB_POWER_SOFTCONN;
1716 else
1717 power &= ~MUSB_POWER_SOFTCONN;
1718
1719 /* FIXME if on, HdrcStart; if off, HdrcStop */
1720
1721 dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1722 is_on ? "on" : "off");
1723 musb_writeb(musb->mregs, MUSB_POWER, power);
1724}
1725
1726#if 0
1727static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1728{
1729 dev_dbg(musb->controller, "<= %s =>\n", __func__);
1730
1731 /*
1732 * FIXME iff driver's softconnect flag is set (as it is during probe,
1733 * though that can clear it), just musb_pullup().
1734 */
1735
1736 return -EINVAL;
1737}
1738#endif
1739
1740static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1741{
1742#ifndef __UBOOT__
1743 struct musb *musb = gadget_to_musb(gadget);
1744
1745 if (!musb->xceiv->set_power)
1746 return -EOPNOTSUPP;
1747 return usb_phy_set_power(musb->xceiv, mA);
1748#else
1749 return 0;
1750#endif
1751}
1752
1753static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1754{
1755 struct musb *musb = gadget_to_musb(gadget);
1756 unsigned long flags;
1757
1758 is_on = !!is_on;
1759
1760 pm_runtime_get_sync(musb->controller);
1761
1762 /* NOTE: this assumes we are sensing vbus; we'd rather
1763 * not pullup unless the B-session is active.
1764 */
1765 spin_lock_irqsave(&musb->lock, flags);
1766 if (is_on != musb->softconnect) {
1767 musb->softconnect = is_on;
1768 musb_pullup(musb, is_on);
1769 }
1770 spin_unlock_irqrestore(&musb->lock, flags);
1771
1772 pm_runtime_put(musb->controller);
1773
1774 return 0;
1775}
1776
1777#ifndef __UBOOT__
1778static int musb_gadget_start(struct usb_gadget *g,
1779 struct usb_gadget_driver *driver);
1780static int musb_gadget_stop(struct usb_gadget *g,
1781 struct usb_gadget_driver *driver);
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +01001782#else
1783static int musb_gadget_stop(struct usb_gadget *g)
1784{
1785 struct musb *musb = gadget_to_musb(g);
1786
1787 musb_stop(musb);
1788 return 0;
1789}
Ilya Yanok06bb9202012-11-06 13:48:21 +00001790#endif
1791
1792static const struct usb_gadget_ops musb_gadget_operations = {
1793 .get_frame = musb_gadget_get_frame,
1794 .wakeup = musb_gadget_wakeup,
1795 .set_selfpowered = musb_gadget_set_self_powered,
1796 /* .vbus_session = musb_gadget_vbus_session, */
1797 .vbus_draw = musb_gadget_vbus_draw,
1798 .pullup = musb_gadget_pullup,
1799#ifndef __UBOOT__
1800 .udc_start = musb_gadget_start,
1801 .udc_stop = musb_gadget_stop,
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +01001802#else
1803 .udc_start = musb_gadget_start,
1804 .udc_stop = musb_gadget_stop,
Ilya Yanok06bb9202012-11-06 13:48:21 +00001805#endif
1806};
1807
1808/* ----------------------------------------------------------------------- */
1809
1810/* Registration */
1811
1812/* Only this registration code "knows" the rule (from USB standards)
1813 * about there being only one external upstream port. It assumes
1814 * all peripheral ports are external...
1815 */
1816
1817#ifndef __UBOOT__
1818static void musb_gadget_release(struct device *dev)
1819{
1820 /* kref_put(WHAT) */
1821 dev_dbg(dev, "%s\n", __func__);
1822}
1823#endif
1824
1825
1826static void __devinit
1827init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1828{
1829 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1830
1831 memset(ep, 0, sizeof *ep);
1832
1833 ep->current_epnum = epnum;
1834 ep->musb = musb;
1835 ep->hw_ep = hw_ep;
1836 ep->is_in = is_in;
1837
1838 INIT_LIST_HEAD(&ep->req_list);
1839
1840 sprintf(ep->name, "ep%d%s", epnum,
1841 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1842 is_in ? "in" : "out"));
1843 ep->end_point.name = ep->name;
1844 INIT_LIST_HEAD(&ep->end_point.ep_list);
1845 if (!epnum) {
1846 ep->end_point.maxpacket = 64;
1847 ep->end_point.ops = &musb_g_ep0_ops;
1848 musb->g.ep0 = &ep->end_point;
1849 } else {
1850 if (is_in)
1851 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1852 else
1853 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1854 ep->end_point.ops = &musb_ep_ops;
1855 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1856 }
1857}
1858
1859/*
1860 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1861 * to the rest of the driver state.
1862 */
1863static inline void __devinit musb_g_init_endpoints(struct musb *musb)
1864{
1865 u8 epnum;
1866 struct musb_hw_ep *hw_ep;
1867 unsigned count = 0;
1868
1869 /* initialize endpoint list just once */
1870 INIT_LIST_HEAD(&(musb->g.ep_list));
1871
1872 for (epnum = 0, hw_ep = musb->endpoints;
1873 epnum < musb->nr_endpoints;
1874 epnum++, hw_ep++) {
1875 if (hw_ep->is_shared_fifo /* || !epnum */) {
1876 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1877 count++;
1878 } else {
1879 if (hw_ep->max_packet_sz_tx) {
1880 init_peripheral_ep(musb, &hw_ep->ep_in,
1881 epnum, 1);
1882 count++;
1883 }
1884 if (hw_ep->max_packet_sz_rx) {
1885 init_peripheral_ep(musb, &hw_ep->ep_out,
1886 epnum, 0);
1887 count++;
1888 }
1889 }
1890 }
1891}
1892
1893/* called once during driver setup to initialize and link into
1894 * the driver model; memory is zeroed.
1895 */
1896int __devinit musb_gadget_setup(struct musb *musb)
1897{
1898 int status;
1899
1900 /* REVISIT minor race: if (erroneously) setting up two
1901 * musb peripherals at the same time, only the bus lock
1902 * is probably held.
1903 */
1904
1905 musb->g.ops = &musb_gadget_operations;
1906#ifndef __UBOOT__
1907 musb->g.max_speed = USB_SPEED_HIGH;
1908#endif
1909 musb->g.speed = USB_SPEED_UNKNOWN;
1910
1911#ifndef __UBOOT__
1912 /* this "gadget" abstracts/virtualizes the controller */
1913 dev_set_name(&musb->g.dev, "gadget");
1914 musb->g.dev.parent = musb->controller;
1915 musb->g.dev.dma_mask = musb->controller->dma_mask;
1916 musb->g.dev.release = musb_gadget_release;
1917#endif
1918 musb->g.name = musb_driver_name;
1919
1920#ifndef __UBOOT__
1921 if (is_otg_enabled(musb))
1922 musb->g.is_otg = 1;
1923#endif
1924
1925 musb_g_init_endpoints(musb);
1926
1927 musb->is_active = 0;
1928 musb_platform_try_idle(musb, 0);
1929
1930#ifndef __UBOOT__
1931 status = device_register(&musb->g.dev);
1932 if (status != 0) {
1933 put_device(&musb->g.dev);
1934 return status;
1935 }
1936 status = usb_add_gadget_udc(musb->controller, &musb->g);
1937 if (status)
1938 goto err;
1939#endif
1940
1941 return 0;
1942#ifndef __UBOOT__
1943err:
1944 musb->g.dev.parent = NULL;
1945 device_unregister(&musb->g.dev);
1946 return status;
1947#endif
1948}
1949
1950void musb_gadget_cleanup(struct musb *musb)
1951{
1952#ifndef __UBOOT__
1953 usb_del_gadget_udc(&musb->g);
1954 if (musb->g.dev.parent)
1955 device_unregister(&musb->g.dev);
1956#endif
1957}
1958
1959/*
1960 * Register the gadget driver. Used by gadget drivers when
1961 * registering themselves with the controller.
1962 *
1963 * -EINVAL something went wrong (not driver)
1964 * -EBUSY another gadget is already using the controller
1965 * -ENOMEM no memory to perform the operation
1966 *
1967 * @param driver the gadget driver
1968 * @return <0 if error, 0 if everything is fine
1969 */
1970#ifndef __UBOOT__
1971static int musb_gadget_start(struct usb_gadget *g,
1972 struct usb_gadget_driver *driver)
1973#else
1974int musb_gadget_start(struct usb_gadget *g,
1975 struct usb_gadget_driver *driver)
1976#endif
1977{
1978 struct musb *musb = gadget_to_musb(g);
1979#ifndef __UBOOT__
1980 struct usb_otg *otg = musb->xceiv->otg;
1981#endif
1982 unsigned long flags;
1983 int retval = -EINVAL;
1984
1985#ifndef __UBOOT__
1986 if (driver->max_speed < USB_SPEED_HIGH)
1987 goto err0;
1988#endif
1989
1990 pm_runtime_get_sync(musb->controller);
1991
1992#ifndef __UBOOT__
1993 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
1994#endif
1995
1996 musb->softconnect = 0;
1997 musb->gadget_driver = driver;
1998
1999 spin_lock_irqsave(&musb->lock, flags);
2000 musb->is_active = 1;
2001
2002#ifndef __UBOOT__
2003 otg_set_peripheral(otg, &musb->g);
2004 musb->xceiv->state = OTG_STATE_B_IDLE;
2005
2006 /*
2007 * FIXME this ignores the softconnect flag. Drivers are
2008 * allowed hold the peripheral inactive until for example
2009 * userspace hooks up printer hardware or DSP codecs, so
2010 * hosts only see fully functional devices.
2011 */
2012
2013 if (!is_otg_enabled(musb))
2014#endif
2015 musb_start(musb);
2016
2017 spin_unlock_irqrestore(&musb->lock, flags);
2018
2019#ifndef __UBOOT__
2020 if (is_otg_enabled(musb)) {
2021 struct usb_hcd *hcd = musb_to_hcd(musb);
2022
2023 dev_dbg(musb->controller, "OTG startup...\n");
2024
2025 /* REVISIT: funcall to other code, which also
2026 * handles power budgeting ... this way also
2027 * ensures HdrcStart is indirectly called.
2028 */
2029 retval = usb_add_hcd(musb_to_hcd(musb), 0, 0);
2030 if (retval < 0) {
2031 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
2032 goto err2;
2033 }
2034
2035 if ((musb->xceiv->last_event == USB_EVENT_ID)
2036 && otg->set_vbus)
2037 otg_set_vbus(otg, 1);
2038
2039 hcd->self.uses_pio_for_control = 1;
2040 }
2041 if (musb->xceiv->last_event == USB_EVENT_NONE)
2042 pm_runtime_put(musb->controller);
2043#endif
2044
2045 return 0;
2046
2047#ifndef __UBOOT__
2048err2:
2049 if (!is_otg_enabled(musb))
2050 musb_stop(musb);
2051err0:
2052 return retval;
2053#endif
2054}
2055
2056#ifndef __UBOOT__
2057static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
2058{
2059 int i;
2060 struct musb_hw_ep *hw_ep;
2061
2062 /* don't disconnect if it's not connected */
2063 if (musb->g.speed == USB_SPEED_UNKNOWN)
2064 driver = NULL;
2065 else
2066 musb->g.speed = USB_SPEED_UNKNOWN;
2067
2068 /* deactivate the hardware */
2069 if (musb->softconnect) {
2070 musb->softconnect = 0;
2071 musb_pullup(musb, 0);
2072 }
2073 musb_stop(musb);
2074
2075 /* killing any outstanding requests will quiesce the driver;
2076 * then report disconnect
2077 */
2078 if (driver) {
2079 for (i = 0, hw_ep = musb->endpoints;
2080 i < musb->nr_endpoints;
2081 i++, hw_ep++) {
2082 musb_ep_select(musb->mregs, i);
2083 if (hw_ep->is_shared_fifo /* || !epnum */) {
2084 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2085 } else {
2086 if (hw_ep->max_packet_sz_tx)
2087 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2088 if (hw_ep->max_packet_sz_rx)
2089 nuke(&hw_ep->ep_out, -ESHUTDOWN);
2090 }
2091 }
2092 }
2093}
2094
2095/*
2096 * Unregister the gadget driver. Used by gadget drivers when
2097 * unregistering themselves from the controller.
2098 *
2099 * @param driver the gadget driver to unregister
2100 */
2101static int musb_gadget_stop(struct usb_gadget *g,
2102 struct usb_gadget_driver *driver)
2103{
2104 struct musb *musb = gadget_to_musb(g);
2105 unsigned long flags;
2106
2107 if (musb->xceiv->last_event == USB_EVENT_NONE)
2108 pm_runtime_get_sync(musb->controller);
2109
2110 /*
2111 * REVISIT always use otg_set_peripheral() here too;
2112 * this needs to shut down the OTG engine.
2113 */
2114
2115 spin_lock_irqsave(&musb->lock, flags);
2116
2117 musb_hnp_stop(musb);
2118
2119 (void) musb_gadget_vbus_draw(&musb->g, 0);
2120
2121 musb->xceiv->state = OTG_STATE_UNDEFINED;
2122 stop_activity(musb, driver);
2123 otg_set_peripheral(musb->xceiv->otg, NULL);
2124
2125 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
2126
2127 musb->is_active = 0;
2128 musb_platform_try_idle(musb, 0);
2129 spin_unlock_irqrestore(&musb->lock, flags);
2130
2131 if (is_otg_enabled(musb)) {
2132 usb_remove_hcd(musb_to_hcd(musb));
2133 /* FIXME we need to be able to register another
2134 * gadget driver here and have everything work;
2135 * that currently misbehaves.
2136 */
2137 }
2138
2139 if (!is_otg_enabled(musb))
2140 musb_stop(musb);
2141
2142 pm_runtime_put(musb->controller);
2143
2144 return 0;
2145}
2146#endif
2147
2148/* ----------------------------------------------------------------------- */
2149
2150/* lifecycle operations called through plat_uds.c */
2151
2152void musb_g_resume(struct musb *musb)
2153{
2154#ifndef __UBOOT__
2155 musb->is_suspended = 0;
2156 switch (musb->xceiv->state) {
2157 case OTG_STATE_B_IDLE:
2158 break;
2159 case OTG_STATE_B_WAIT_ACON:
2160 case OTG_STATE_B_PERIPHERAL:
2161 musb->is_active = 1;
2162 if (musb->gadget_driver && musb->gadget_driver->resume) {
2163 spin_unlock(&musb->lock);
2164 musb->gadget_driver->resume(&musb->g);
2165 spin_lock(&musb->lock);
2166 }
2167 break;
2168 default:
2169 WARNING("unhandled RESUME transition (%s)\n",
2170 otg_state_string(musb->xceiv->state));
2171 }
2172#endif
2173}
2174
2175/* called when SOF packets stop for 3+ msec */
2176void musb_g_suspend(struct musb *musb)
2177{
2178#ifndef __UBOOT__
2179 u8 devctl;
2180
2181 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2182 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2183
2184 switch (musb->xceiv->state) {
2185 case OTG_STATE_B_IDLE:
2186 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2187 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2188 break;
2189 case OTG_STATE_B_PERIPHERAL:
2190 musb->is_suspended = 1;
2191 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2192 spin_unlock(&musb->lock);
2193 musb->gadget_driver->suspend(&musb->g);
2194 spin_lock(&musb->lock);
2195 }
2196 break;
2197 default:
2198 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2199 * A_PERIPHERAL may need care too
2200 */
2201 WARNING("unhandled SUSPEND transition (%s)\n",
2202 otg_state_string(musb->xceiv->state));
2203 }
2204#endif
2205}
2206
2207/* Called during SRP */
2208void musb_g_wakeup(struct musb *musb)
2209{
2210 musb_gadget_wakeup(&musb->g);
2211}
2212
2213/* called when VBUS drops below session threshold, and in other cases */
2214void musb_g_disconnect(struct musb *musb)
2215{
2216 void __iomem *mregs = musb->mregs;
2217 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2218
2219 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2220
2221 /* clear HR */
2222 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2223
2224 /* don't draw vbus until new b-default session */
2225 (void) musb_gadget_vbus_draw(&musb->g, 0);
2226
2227 musb->g.speed = USB_SPEED_UNKNOWN;
2228 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2229 spin_unlock(&musb->lock);
2230 musb->gadget_driver->disconnect(&musb->g);
2231 spin_lock(&musb->lock);
2232 }
2233
2234#ifndef __UBOOT__
2235 switch (musb->xceiv->state) {
2236 default:
2237 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
2238 otg_state_string(musb->xceiv->state));
2239 musb->xceiv->state = OTG_STATE_A_IDLE;
2240 MUSB_HST_MODE(musb);
2241 break;
2242 case OTG_STATE_A_PERIPHERAL:
2243 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2244 MUSB_HST_MODE(musb);
2245 break;
2246 case OTG_STATE_B_WAIT_ACON:
2247 case OTG_STATE_B_HOST:
2248 case OTG_STATE_B_PERIPHERAL:
2249 case OTG_STATE_B_IDLE:
2250 musb->xceiv->state = OTG_STATE_B_IDLE;
2251 break;
2252 case OTG_STATE_B_SRP_INIT:
2253 break;
2254 }
2255#endif
2256
2257 musb->is_active = 0;
2258}
2259
2260void musb_g_reset(struct musb *musb)
2261__releases(musb->lock)
2262__acquires(musb->lock)
2263{
2264 void __iomem *mbase = musb->mregs;
2265 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2266 u8 power;
2267
2268#ifndef __UBOOT__
2269 dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n",
2270 (devctl & MUSB_DEVCTL_BDEVICE)
2271 ? "B-Device" : "A-Device",
2272 musb_readb(mbase, MUSB_FADDR),
2273 musb->gadget_driver
2274 ? musb->gadget_driver->driver.name
2275 : NULL
2276 );
2277#endif
2278
2279 /* report disconnect, if we didn't already (flushing EP state) */
2280 if (musb->g.speed != USB_SPEED_UNKNOWN)
2281 musb_g_disconnect(musb);
2282
2283 /* clear HR */
2284 else if (devctl & MUSB_DEVCTL_HR)
2285 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2286
2287
2288 /* what speed did we negotiate? */
2289 power = musb_readb(mbase, MUSB_POWER);
2290 musb->g.speed = (power & MUSB_POWER_HSMODE)
2291 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2292
2293 /* start in USB_STATE_DEFAULT */
2294 musb->is_active = 1;
2295 musb->is_suspended = 0;
2296 MUSB_DEV_MODE(musb);
2297 musb->address = 0;
2298 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2299
2300 musb->may_wakeup = 0;
2301 musb->g.b_hnp_enable = 0;
2302 musb->g.a_alt_hnp_support = 0;
2303 musb->g.a_hnp_support = 0;
2304
2305#ifndef __UBOOT__
2306 /* Normal reset, as B-Device;
2307 * or else after HNP, as A-Device
2308 */
2309 if (devctl & MUSB_DEVCTL_BDEVICE) {
2310 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2311 musb->g.is_a_peripheral = 0;
2312 } else if (is_otg_enabled(musb)) {
2313 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
2314 musb->g.is_a_peripheral = 1;
2315 } else
2316 WARN_ON(1);
2317
2318 /* start with default limits on VBUS power draw */
2319 (void) musb_gadget_vbus_draw(&musb->g,
2320 is_otg_enabled(musb) ? 8 : 100);
2321#endif
2322}