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