blob: 08fac829ce71ef695a1b0e01ceb078095a168e3c [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
Sean Anderson3e464862020-10-04 21:39:54 -040025#include <dm.h>
26#include <dm/device_compat.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060027#include <linux/bug.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060028#include <linux/printk.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
Ilya Yanok06bb9202012-11-06 13:48:21 +000035/* MUSB PERIPHERAL status 3-mar-2006:
36 *
37 * - EP0 seems solid. It passes both USBCV and usbtest control cases.
38 * Minor glitches:
39 *
40 * + remote wakeup to Linux hosts work, but saw USBCV failures;
41 * in one test run (operator error?)
42 * + endpoint halt tests -- in both usbtest and usbcv -- seem
43 * to break when dma is enabled ... is something wrongly
44 * clearing SENDSTALL?
45 *
46 * - Mass storage behaved ok when last tested. Network traffic patterns
47 * (with lots of short transfers etc) need retesting; they turn up the
48 * worst cases of the DMA, since short packets are typical but are not
49 * required.
50 *
51 * - TX/IN
52 * + both pio and dma behave in with network and g_zero tests
53 * + no cppi throughput issues other than no-hw-queueing
54 * + failed with FLAT_REG (DaVinci)
55 * + seems to behave with double buffering, PIO -and- CPPI
56 * + with gadgetfs + AIO, requests got lost?
57 *
58 * - RX/OUT
59 * + both pio and dma behave in with network and g_zero tests
60 * + dma is slow in typical case (short_not_ok is clear)
61 * + double buffering ok with PIO
62 * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
63 * + request lossage observed with gadgetfs
64 *
65 * - ISO not tested ... might work, but only weakly isochronous
66 *
67 * - Gadget driver disabling of softconnect during bind() is ignored; so
68 * drivers can't hold off host requests until userspace is ready.
69 * (Workaround: they can turn it off later.)
70 *
71 * - PORTABILITY (assumes PIO works):
72 * + DaVinci, basically works with cppi dma
73 * + OMAP 2430, ditto with mentor dma
74 * + TUSB 6010, platform-specific dma in the works
75 */
76
77/* ----------------------------------------------------------------------- */
78
79#define is_buffer_mapped(req) (is_dma_capable() && \
80 (req->map_state != UN_MAPPED))
81
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +020082#ifndef CONFIG_USB_MUSB_PIO_ONLY
Ilya Yanok06bb9202012-11-06 13:48:21 +000083/* Maps the buffer to dma */
84
85static inline void map_dma_buffer(struct musb_request *request,
86 struct musb *musb, struct musb_ep *musb_ep)
87{
88 int compatible = true;
89 struct dma_controller *dma = musb->dma_controller;
90
91 request->map_state = UN_MAPPED;
92
93 if (!is_dma_capable() || !musb_ep->dma)
94 return;
95
96 /* Check if DMA engine can handle this request.
97 * DMA code must reject the USB request explicitly.
98 * Default behaviour is to map the request.
99 */
100 if (dma->is_compatible)
101 compatible = dma->is_compatible(musb_ep->dma,
102 musb_ep->packet_sz, request->request.buf,
103 request->request.length);
104 if (!compatible)
105 return;
106
107 if (request->request.dma == DMA_ADDR_INVALID) {
108 request->request.dma = dma_map_single(
109 musb->controller,
110 request->request.buf,
111 request->request.length,
112 request->tx
113 ? DMA_TO_DEVICE
114 : DMA_FROM_DEVICE);
115 request->map_state = MUSB_MAPPED;
116 } else {
117 dma_sync_single_for_device(musb->controller,
118 request->request.dma,
119 request->request.length,
120 request->tx
121 ? DMA_TO_DEVICE
122 : DMA_FROM_DEVICE);
123 request->map_state = PRE_MAPPED;
124 }
125}
126
127/* Unmap the buffer from dma and maps it back to cpu */
128static inline void unmap_dma_buffer(struct musb_request *request,
129 struct musb *musb)
130{
131 if (!is_buffer_mapped(request))
132 return;
133
134 if (request->request.dma == DMA_ADDR_INVALID) {
135 dev_vdbg(musb->controller,
136 "not unmapping a never mapped buffer\n");
137 return;
138 }
139 if (request->map_state == MUSB_MAPPED) {
140 dma_unmap_single(musb->controller,
141 request->request.dma,
142 request->request.length,
143 request->tx
144 ? DMA_TO_DEVICE
145 : DMA_FROM_DEVICE);
146 request->request.dma = DMA_ADDR_INVALID;
147 } else { /* PRE_MAPPED */
148 dma_sync_single_for_cpu(musb->controller,
149 request->request.dma,
150 request->request.length,
151 request->tx
152 ? DMA_TO_DEVICE
153 : DMA_FROM_DEVICE);
154 }
155 request->map_state = UN_MAPPED;
156}
157#else
158static inline void map_dma_buffer(struct musb_request *request,
159 struct musb *musb, struct musb_ep *musb_ep)
160{
161}
162
163static inline void unmap_dma_buffer(struct musb_request *request,
164 struct musb *musb)
165{
166}
167#endif
168
169/*
170 * Immediately complete a request.
171 *
172 * @param request the request to complete
173 * @param status the status to complete the request with
174 * Context: controller locked, IRQs blocked.
175 */
176void musb_g_giveback(
177 struct musb_ep *ep,
178 struct usb_request *request,
179 int status)
180__releases(ep->musb->lock)
181__acquires(ep->musb->lock)
182{
183 struct musb_request *req;
184 struct musb *musb;
185 int busy = ep->busy;
186
187 req = to_musb_request(request);
188
189 list_del(&req->list);
190 if (req->request.status == -EINPROGRESS)
191 req->request.status = status;
192 musb = req->musb;
193
194 ep->busy = 1;
195 spin_unlock(&musb->lock);
196 unmap_dma_buffer(req, musb);
197 if (request->status == 0)
198 dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
199 ep->end_point.name, request,
200 req->request.actual, req->request.length);
201 else
202 dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
203 ep->end_point.name, request,
204 req->request.actual, req->request.length,
205 request->status);
206 req->request.complete(&req->ep->end_point, &req->request);
207 spin_lock(&musb->lock);
208 ep->busy = busy;
209}
210
211/* ----------------------------------------------------------------------- */
212
213/*
214 * Abort requests queued to an endpoint using the status. Synchronous.
215 * caller locked controller and blocked irqs, and selected this ep.
216 */
217static void nuke(struct musb_ep *ep, const int status)
218{
219 struct musb *musb = ep->musb;
220 struct musb_request *req = NULL;
221 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
222
223 ep->busy = 1;
224
225 if (is_dma_capable() && ep->dma) {
226 struct dma_controller *c = ep->musb->dma_controller;
227 int value;
228
229 if (ep->is_in) {
230 /*
231 * The programming guide says that we must not clear
232 * the DMAMODE bit before DMAENAB, so we only
233 * clear it in the second write...
234 */
235 musb_writew(epio, MUSB_TXCSR,
236 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
237 musb_writew(epio, MUSB_TXCSR,
238 0 | MUSB_TXCSR_FLUSHFIFO);
239 } else {
240 musb_writew(epio, MUSB_RXCSR,
241 0 | MUSB_RXCSR_FLUSHFIFO);
242 musb_writew(epio, MUSB_RXCSR,
243 0 | MUSB_RXCSR_FLUSHFIFO);
244 }
245
246 value = c->channel_abort(ep->dma);
247 dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
248 ep->name, value);
249 c->channel_release(ep->dma);
250 ep->dma = NULL;
251 }
252
253 while (!list_empty(&ep->req_list)) {
254 req = list_first_entry(&ep->req_list, struct musb_request, list);
255 musb_g_giveback(ep, &req->request, status);
256 }
257}
258
259/* ----------------------------------------------------------------------- */
260
261/* Data transfers - pure PIO, pure DMA, or mixed mode */
262
263/*
264 * This assumes the separate CPPI engine is responding to DMA requests
265 * from the usb core ... sequenced a bit differently from mentor dma.
266 */
267
268static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
269{
270 if (can_bulk_split(musb, ep->type))
271 return ep->hw_ep->max_packet_sz_tx;
272 else
273 return ep->packet_sz;
274}
275
Ilya Yanok06bb9202012-11-06 13:48:21 +0000276#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
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200996 * there is Rx packet in FIFO.
997 **/
Ilya Yanok06bb9202012-11-06 13:48:21 +0000998 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
qianfan Zhao307be4012021-11-16 08:30:12 +08001189 musb_ep->end_point.desc = desc;
Ilya Yanok06bb9202012-11-06 13:48:21 +00001190 musb_ep->desc = desc;
1191 musb_ep->busy = 0;
1192 musb_ep->wedged = 0;
1193 status = 0;
1194
1195 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1196 musb_driver_name, musb_ep->end_point.name,
1197 ({ char *s; switch (musb_ep->type) {
1198 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1199 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1200 default: s = "iso"; break;
1201 }; s; }),
1202 musb_ep->is_in ? "IN" : "OUT",
1203 musb_ep->dma ? "dma, " : "",
1204 musb_ep->packet_sz);
1205
1206 schedule_work(&musb->irq_work);
1207
1208fail:
1209 spin_unlock_irqrestore(&musb->lock, flags);
1210 return status;
1211}
1212
1213/*
1214 * Disable an endpoint flushing all requests queued.
1215 */
1216static int musb_gadget_disable(struct usb_ep *ep)
1217{
1218 unsigned long flags;
1219 struct musb *musb;
1220 u8 epnum;
1221 struct musb_ep *musb_ep;
1222 void __iomem *epio;
1223 int status = 0;
1224
1225 musb_ep = to_musb_ep(ep);
1226 musb = musb_ep->musb;
1227 epnum = musb_ep->current_epnum;
1228 epio = musb->endpoints[epnum].regs;
1229
1230 spin_lock_irqsave(&musb->lock, flags);
1231 musb_ep_select(musb->mregs, epnum);
1232
1233 /* zero the endpoint sizes */
1234 if (musb_ep->is_in) {
1235 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1236 int_txe &= ~(1 << epnum);
1237 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1238 musb_writew(epio, MUSB_TXMAXP, 0);
1239 } else {
1240 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1241 int_rxe &= ~(1 << epnum);
1242 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1243 musb_writew(epio, MUSB_RXMAXP, 0);
1244 }
1245
1246 musb_ep->desc = NULL;
Ilya Yanok06bb9202012-11-06 13:48:21 +00001247 musb_ep->end_point.desc = NULL;
Ilya Yanok06bb9202012-11-06 13:48:21 +00001248
1249 /* abort all pending DMA and requests */
1250 nuke(musb_ep, -ESHUTDOWN);
1251
1252 schedule_work(&musb->irq_work);
1253
1254 spin_unlock_irqrestore(&(musb->lock), flags);
1255
1256 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
1257
1258 return status;
1259}
1260
1261/*
1262 * Allocate a request for an endpoint.
1263 * Reused by ep0 code.
1264 */
1265struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1266{
1267 struct musb_ep *musb_ep = to_musb_ep(ep);
1268 struct musb *musb = musb_ep->musb;
1269 struct musb_request *request = NULL;
1270
1271 request = kzalloc(sizeof *request, gfp_flags);
1272 if (!request) {
1273 dev_dbg(musb->controller, "not enough memory\n");
1274 return NULL;
1275 }
1276
1277 request->request.dma = DMA_ADDR_INVALID;
1278 request->epnum = musb_ep->current_epnum;
1279 request->ep = musb_ep;
1280
1281 return &request->request;
1282}
1283
1284/*
1285 * Free a request
1286 * Reused by ep0 code.
1287 */
1288void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1289{
1290 kfree(to_musb_request(req));
1291}
1292
1293static LIST_HEAD(buffers);
1294
1295struct free_record {
1296 struct list_head list;
1297 struct device *dev;
1298 unsigned bytes;
1299 dma_addr_t dma;
1300};
1301
1302/*
1303 * Context: controller locked, IRQs blocked.
1304 */
1305void musb_ep_restart(struct musb *musb, struct musb_request *req)
1306{
1307 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
1308 req->tx ? "TX/IN" : "RX/OUT",
1309 &req->request, req->request.length, req->epnum);
1310
1311 musb_ep_select(musb->mregs, req->epnum);
1312 if (req->tx)
1313 txstate(musb, req);
1314 else
1315 rxstate(musb, req);
1316}
1317
1318static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1319 gfp_t gfp_flags)
1320{
1321 struct musb_ep *musb_ep;
1322 struct musb_request *request;
1323 struct musb *musb;
1324 int status = 0;
1325 unsigned long lockflags;
1326
1327 if (!ep || !req)
1328 return -EINVAL;
1329 if (!req->buf)
1330 return -ENODATA;
1331
1332 musb_ep = to_musb_ep(ep);
1333 musb = musb_ep->musb;
1334
1335 request = to_musb_request(req);
1336 request->musb = musb;
1337
1338 if (request->ep != musb_ep)
1339 return -EINVAL;
1340
1341 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
1342
1343 /* request is mine now... */
1344 request->request.actual = 0;
1345 request->request.status = -EINPROGRESS;
1346 request->epnum = musb_ep->current_epnum;
1347 request->tx = musb_ep->is_in;
1348
1349 map_dma_buffer(request, musb, musb_ep);
1350
1351 spin_lock_irqsave(&musb->lock, lockflags);
1352
1353 /* don't queue if the ep is down */
1354 if (!musb_ep->desc) {
1355 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
1356 req, ep->name, "disabled");
1357 status = -ESHUTDOWN;
1358 goto cleanup;
1359 }
1360
1361 /* add request to the list */
1362 list_add_tail(&request->list, &musb_ep->req_list);
1363
1364 /* it this is the head of the queue, start i/o ... */
1365 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
1366 musb_ep_restart(musb, request);
1367
1368cleanup:
1369 spin_unlock_irqrestore(&musb->lock, lockflags);
1370 return status;
1371}
1372
1373static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1374{
1375 struct musb_ep *musb_ep = to_musb_ep(ep);
1376 struct musb_request *req = to_musb_request(request);
1377 struct musb_request *r;
1378 unsigned long flags;
1379 int status = 0;
1380 struct musb *musb = musb_ep->musb;
1381
1382 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1383 return -EINVAL;
1384
1385 spin_lock_irqsave(&musb->lock, flags);
1386
1387 list_for_each_entry(r, &musb_ep->req_list, list) {
1388 if (r == req)
1389 break;
1390 }
1391 if (r != req) {
1392 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
1393 status = -EINVAL;
1394 goto done;
1395 }
1396
1397 /* if the hardware doesn't have the request, easy ... */
1398 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
1399 musb_g_giveback(musb_ep, request, -ECONNRESET);
1400
1401 /* ... else abort the dma transfer ... */
1402 else if (is_dma_capable() && musb_ep->dma) {
1403 struct dma_controller *c = musb->dma_controller;
1404
1405 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1406 if (c->channel_abort)
1407 status = c->channel_abort(musb_ep->dma);
1408 else
1409 status = -EBUSY;
1410 if (status == 0)
1411 musb_g_giveback(musb_ep, request, -ECONNRESET);
1412 } else {
1413 /* NOTE: by sticking to easily tested hardware/driver states,
1414 * we leave counting of in-flight packets imprecise.
1415 */
1416 musb_g_giveback(musb_ep, request, -ECONNRESET);
1417 }
1418
1419done:
1420 spin_unlock_irqrestore(&musb->lock, flags);
1421 return status;
1422}
1423
1424/*
1425 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1426 * data but will queue requests.
1427 *
1428 * exported to ep0 code
1429 */
1430static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1431{
1432 struct musb_ep *musb_ep = to_musb_ep(ep);
1433 u8 epnum = musb_ep->current_epnum;
1434 struct musb *musb = musb_ep->musb;
1435 void __iomem *epio = musb->endpoints[epnum].regs;
1436 void __iomem *mbase;
1437 unsigned long flags;
1438 u16 csr;
1439 struct musb_request *request;
1440 int status = 0;
1441
1442 if (!ep)
1443 return -EINVAL;
1444 mbase = musb->mregs;
1445
1446 spin_lock_irqsave(&musb->lock, flags);
1447
1448 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1449 status = -EINVAL;
1450 goto done;
1451 }
1452
1453 musb_ep_select(mbase, epnum);
1454
1455 request = next_request(musb_ep);
1456 if (value) {
1457 if (request) {
1458 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
1459 ep->name);
1460 status = -EAGAIN;
1461 goto done;
1462 }
1463 /* Cannot portably stall with non-empty FIFO */
1464 if (musb_ep->is_in) {
1465 csr = musb_readw(epio, MUSB_TXCSR);
1466 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1467 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
1468 status = -EAGAIN;
1469 goto done;
1470 }
1471 }
1472 } else
1473 musb_ep->wedged = 0;
1474
1475 /* set/clear the stall and toggle bits */
1476 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
1477 if (musb_ep->is_in) {
1478 csr = musb_readw(epio, MUSB_TXCSR);
1479 csr |= MUSB_TXCSR_P_WZC_BITS
1480 | MUSB_TXCSR_CLRDATATOG;
1481 if (value)
1482 csr |= MUSB_TXCSR_P_SENDSTALL;
1483 else
1484 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1485 | MUSB_TXCSR_P_SENTSTALL);
1486 csr &= ~MUSB_TXCSR_TXPKTRDY;
1487 musb_writew(epio, MUSB_TXCSR, csr);
1488 } else {
1489 csr = musb_readw(epio, MUSB_RXCSR);
1490 csr |= MUSB_RXCSR_P_WZC_BITS
1491 | MUSB_RXCSR_FLUSHFIFO
1492 | MUSB_RXCSR_CLRDATATOG;
1493 if (value)
1494 csr |= MUSB_RXCSR_P_SENDSTALL;
1495 else
1496 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1497 | MUSB_RXCSR_P_SENTSTALL);
1498 musb_writew(epio, MUSB_RXCSR, csr);
1499 }
1500
1501 /* maybe start the first request in the queue */
1502 if (!musb_ep->busy && !value && request) {
1503 dev_dbg(musb->controller, "restarting the request\n");
1504 musb_ep_restart(musb, request);
1505 }
1506
1507done:
1508 spin_unlock_irqrestore(&musb->lock, flags);
1509 return status;
1510}
1511
1512#ifndef __UBOOT__
1513/*
1514 * Sets the halt feature with the clear requests ignored
1515 */
1516static int musb_gadget_set_wedge(struct usb_ep *ep)
1517{
1518 struct musb_ep *musb_ep = to_musb_ep(ep);
1519
1520 if (!ep)
1521 return -EINVAL;
1522
1523 musb_ep->wedged = 1;
1524
1525 return usb_ep_set_halt(ep);
1526}
1527#endif
1528
1529static int musb_gadget_fifo_status(struct usb_ep *ep)
1530{
1531 struct musb_ep *musb_ep = to_musb_ep(ep);
1532 void __iomem *epio = musb_ep->hw_ep->regs;
1533 int retval = -EINVAL;
1534
1535 if (musb_ep->desc && !musb_ep->is_in) {
1536 struct musb *musb = musb_ep->musb;
1537 int epnum = musb_ep->current_epnum;
1538 void __iomem *mbase = musb->mregs;
1539 unsigned long flags;
1540
1541 spin_lock_irqsave(&musb->lock, flags);
1542
1543 musb_ep_select(mbase, epnum);
1544 /* FIXME return zero unless RXPKTRDY is set */
1545 retval = musb_readw(epio, MUSB_RXCOUNT);
1546
1547 spin_unlock_irqrestore(&musb->lock, flags);
1548 }
1549 return retval;
1550}
1551
1552static void musb_gadget_fifo_flush(struct usb_ep *ep)
1553{
1554 struct musb_ep *musb_ep = to_musb_ep(ep);
1555 struct musb *musb = musb_ep->musb;
1556 u8 epnum = musb_ep->current_epnum;
1557 void __iomem *epio = musb->endpoints[epnum].regs;
1558 void __iomem *mbase;
1559 unsigned long flags;
1560 u16 csr, int_txe;
1561
1562 mbase = musb->mregs;
1563
1564 spin_lock_irqsave(&musb->lock, flags);
1565 musb_ep_select(mbase, (u8) epnum);
1566
1567 /* disable interrupts */
1568 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1569 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1570
1571 if (musb_ep->is_in) {
1572 csr = musb_readw(epio, MUSB_TXCSR);
1573 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1574 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1575 /*
1576 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1577 * to interrupt current FIFO loading, but not flushing
1578 * the already loaded ones.
1579 */
1580 csr &= ~MUSB_TXCSR_TXPKTRDY;
1581 musb_writew(epio, MUSB_TXCSR, csr);
1582 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1583 musb_writew(epio, MUSB_TXCSR, csr);
1584 }
1585 } else {
1586 csr = musb_readw(epio, MUSB_RXCSR);
1587 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1588 musb_writew(epio, MUSB_RXCSR, csr);
1589 musb_writew(epio, MUSB_RXCSR, csr);
1590 }
1591
1592 /* re-enable interrupt */
1593 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1594 spin_unlock_irqrestore(&musb->lock, flags);
1595}
1596
1597static const struct usb_ep_ops musb_ep_ops = {
1598 .enable = musb_gadget_enable,
1599 .disable = musb_gadget_disable,
1600 .alloc_request = musb_alloc_request,
1601 .free_request = musb_free_request,
1602 .queue = musb_gadget_queue,
1603 .dequeue = musb_gadget_dequeue,
1604 .set_halt = musb_gadget_set_halt,
1605#ifndef __UBOOT__
1606 .set_wedge = musb_gadget_set_wedge,
1607#endif
1608 .fifo_status = musb_gadget_fifo_status,
1609 .fifo_flush = musb_gadget_fifo_flush
1610};
1611
1612/* ----------------------------------------------------------------------- */
1613
1614static int musb_gadget_get_frame(struct usb_gadget *gadget)
1615{
1616 struct musb *musb = gadget_to_musb(gadget);
1617
1618 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1619}
1620
1621static int musb_gadget_wakeup(struct usb_gadget *gadget)
1622{
1623#ifndef __UBOOT__
1624 struct musb *musb = gadget_to_musb(gadget);
1625 void __iomem *mregs = musb->mregs;
1626 unsigned long flags;
1627 int status = -EINVAL;
1628 u8 power, devctl;
1629 int retries;
1630
1631 spin_lock_irqsave(&musb->lock, flags);
1632
1633 switch (musb->xceiv->state) {
1634 case OTG_STATE_B_PERIPHERAL:
1635 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1636 * that's part of the standard usb 1.1 state machine, and
1637 * doesn't affect OTG transitions.
1638 */
1639 if (musb->may_wakeup && musb->is_suspended)
1640 break;
1641 goto done;
1642 case OTG_STATE_B_IDLE:
1643 /* Start SRP ... OTG not required. */
1644 devctl = musb_readb(mregs, MUSB_DEVCTL);
1645 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
1646 devctl |= MUSB_DEVCTL_SESSION;
1647 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1648 devctl = musb_readb(mregs, MUSB_DEVCTL);
1649 retries = 100;
1650 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1651 devctl = musb_readb(mregs, MUSB_DEVCTL);
1652 if (retries-- < 1)
1653 break;
1654 }
1655 retries = 10000;
1656 while (devctl & MUSB_DEVCTL_SESSION) {
1657 devctl = musb_readb(mregs, MUSB_DEVCTL);
1658 if (retries-- < 1)
1659 break;
1660 }
1661
1662 spin_unlock_irqrestore(&musb->lock, flags);
1663 otg_start_srp(musb->xceiv->otg);
1664 spin_lock_irqsave(&musb->lock, flags);
1665
1666 /* Block idling for at least 1s */
1667 musb_platform_try_idle(musb,
1668 jiffies + msecs_to_jiffies(1 * HZ));
1669
1670 status = 0;
1671 goto done;
1672 default:
1673 dev_dbg(musb->controller, "Unhandled wake: %s\n",
1674 otg_state_string(musb->xceiv->state));
1675 goto done;
1676 }
1677
1678 status = 0;
1679
1680 power = musb_readb(mregs, MUSB_POWER);
1681 power |= MUSB_POWER_RESUME;
1682 musb_writeb(mregs, MUSB_POWER, power);
1683 dev_dbg(musb->controller, "issue wakeup\n");
1684
1685 /* FIXME do this next chunk in a timer callback, no udelay */
1686 mdelay(2);
1687
1688 power = musb_readb(mregs, MUSB_POWER);
1689 power &= ~MUSB_POWER_RESUME;
1690 musb_writeb(mregs, MUSB_POWER, power);
1691done:
1692 spin_unlock_irqrestore(&musb->lock, flags);
1693 return status;
1694#else
1695 return 0;
1696#endif
1697}
1698
1699static int
1700musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1701{
1702 struct musb *musb = gadget_to_musb(gadget);
1703
1704 musb->is_self_powered = !!is_selfpowered;
1705 return 0;
1706}
1707
1708static void musb_pullup(struct musb *musb, int is_on)
1709{
1710 u8 power;
1711
1712 power = musb_readb(musb->mregs, MUSB_POWER);
1713 if (is_on)
1714 power |= MUSB_POWER_SOFTCONN;
1715 else
1716 power &= ~MUSB_POWER_SOFTCONN;
1717
1718 /* FIXME if on, HdrcStart; if off, HdrcStop */
1719
1720 dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1721 is_on ? "on" : "off");
1722 musb_writeb(musb->mregs, MUSB_POWER, power);
1723}
1724
1725#if 0
1726static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1727{
1728 dev_dbg(musb->controller, "<= %s =>\n", __func__);
1729
1730 /*
1731 * FIXME iff driver's softconnect flag is set (as it is during probe,
1732 * though that can clear it), just musb_pullup().
1733 */
1734
1735 return -EINVAL;
1736}
1737#endif
1738
1739static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1740{
1741#ifndef __UBOOT__
1742 struct musb *musb = gadget_to_musb(gadget);
1743
1744 if (!musb->xceiv->set_power)
1745 return -EOPNOTSUPP;
1746 return usb_phy_set_power(musb->xceiv, mA);
1747#else
1748 return 0;
1749#endif
1750}
1751
1752static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1753{
1754 struct musb *musb = gadget_to_musb(gadget);
1755 unsigned long flags;
1756
1757 is_on = !!is_on;
1758
1759 pm_runtime_get_sync(musb->controller);
1760
1761 /* NOTE: this assumes we are sensing vbus; we'd rather
1762 * not pullup unless the B-session is active.
1763 */
1764 spin_lock_irqsave(&musb->lock, flags);
1765 if (is_on != musb->softconnect) {
1766 musb->softconnect = is_on;
1767 musb_pullup(musb, is_on);
1768 }
1769 spin_unlock_irqrestore(&musb->lock, flags);
1770
1771 pm_runtime_put(musb->controller);
1772
1773 return 0;
1774}
1775
1776#ifndef __UBOOT__
1777static int musb_gadget_start(struct usb_gadget *g,
1778 struct usb_gadget_driver *driver);
1779static int musb_gadget_stop(struct usb_gadget *g,
1780 struct usb_gadget_driver *driver);
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +01001781#else
1782static int musb_gadget_stop(struct usb_gadget *g)
1783{
1784 struct musb *musb = gadget_to_musb(g);
1785
1786 musb_stop(musb);
1787 return 0;
1788}
Ilya Yanok06bb9202012-11-06 13:48:21 +00001789#endif
1790
1791static const struct usb_gadget_ops musb_gadget_operations = {
1792 .get_frame = musb_gadget_get_frame,
1793 .wakeup = musb_gadget_wakeup,
1794 .set_selfpowered = musb_gadget_set_self_powered,
1795 /* .vbus_session = musb_gadget_vbus_session, */
1796 .vbus_draw = musb_gadget_vbus_draw,
1797 .pullup = musb_gadget_pullup,
1798#ifndef __UBOOT__
1799 .udc_start = musb_gadget_start,
1800 .udc_stop = musb_gadget_stop,
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +01001801#else
1802 .udc_start = musb_gadget_start,
1803 .udc_stop = musb_gadget_stop,
Ilya Yanok06bb9202012-11-06 13:48:21 +00001804#endif
1805};
1806
1807/* ----------------------------------------------------------------------- */
1808
1809/* Registration */
1810
1811/* Only this registration code "knows" the rule (from USB standards)
1812 * about there being only one external upstream port. It assumes
1813 * all peripheral ports are external...
1814 */
1815
1816#ifndef __UBOOT__
1817static void musb_gadget_release(struct device *dev)
1818{
1819 /* kref_put(WHAT) */
1820 dev_dbg(dev, "%s\n", __func__);
1821}
1822#endif
1823
Ilya Yanok06bb9202012-11-06 13:48:21 +00001824static void __devinit
1825init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1826{
1827 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1828
1829 memset(ep, 0, sizeof *ep);
1830
1831 ep->current_epnum = epnum;
1832 ep->musb = musb;
1833 ep->hw_ep = hw_ep;
1834 ep->is_in = is_in;
1835
1836 INIT_LIST_HEAD(&ep->req_list);
1837
1838 sprintf(ep->name, "ep%d%s", epnum,
1839 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1840 is_in ? "in" : "out"));
1841 ep->end_point.name = ep->name;
1842 INIT_LIST_HEAD(&ep->end_point.ep_list);
1843 if (!epnum) {
1844 ep->end_point.maxpacket = 64;
1845 ep->end_point.ops = &musb_g_ep0_ops;
1846 musb->g.ep0 = &ep->end_point;
1847 } else {
1848 if (is_in)
1849 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1850 else
1851 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1852 ep->end_point.ops = &musb_ep_ops;
1853 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1854 }
1855}
1856
1857/*
1858 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1859 * to the rest of the driver state.
1860 */
1861static inline void __devinit musb_g_init_endpoints(struct musb *musb)
1862{
1863 u8 epnum;
1864 struct musb_hw_ep *hw_ep;
1865 unsigned count = 0;
1866
1867 /* initialize endpoint list just once */
1868 INIT_LIST_HEAD(&(musb->g.ep_list));
1869
1870 for (epnum = 0, hw_ep = musb->endpoints;
1871 epnum < musb->nr_endpoints;
1872 epnum++, hw_ep++) {
1873 if (hw_ep->is_shared_fifo /* || !epnum */) {
1874 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1875 count++;
1876 } else {
1877 if (hw_ep->max_packet_sz_tx) {
1878 init_peripheral_ep(musb, &hw_ep->ep_in,
1879 epnum, 1);
1880 count++;
1881 }
1882 if (hw_ep->max_packet_sz_rx) {
1883 init_peripheral_ep(musb, &hw_ep->ep_out,
1884 epnum, 0);
1885 count++;
1886 }
1887 }
1888 }
1889}
1890
1891/* called once during driver setup to initialize and link into
1892 * the driver model; memory is zeroed.
1893 */
1894int __devinit musb_gadget_setup(struct musb *musb)
1895{
1896 int status;
1897
1898 /* REVISIT minor race: if (erroneously) setting up two
1899 * musb peripherals at the same time, only the bus lock
1900 * is probably held.
1901 */
1902
1903 musb->g.ops = &musb_gadget_operations;
1904#ifndef __UBOOT__
1905 musb->g.max_speed = USB_SPEED_HIGH;
1906#endif
1907 musb->g.speed = USB_SPEED_UNKNOWN;
1908
1909#ifndef __UBOOT__
1910 /* this "gadget" abstracts/virtualizes the controller */
1911 dev_set_name(&musb->g.dev, "gadget");
1912 musb->g.dev.parent = musb->controller;
1913 musb->g.dev.dma_mask = musb->controller->dma_mask;
1914 musb->g.dev.release = musb_gadget_release;
1915#endif
1916 musb->g.name = musb_driver_name;
1917
1918#ifndef __UBOOT__
1919 if (is_otg_enabled(musb))
1920 musb->g.is_otg = 1;
1921#endif
1922
1923 musb_g_init_endpoints(musb);
1924
1925 musb->is_active = 0;
1926 musb_platform_try_idle(musb, 0);
1927
1928#ifndef __UBOOT__
1929 status = device_register(&musb->g.dev);
1930 if (status != 0) {
1931 put_device(&musb->g.dev);
1932 return status;
1933 }
1934 status = usb_add_gadget_udc(musb->controller, &musb->g);
1935 if (status)
1936 goto err;
1937#endif
1938
1939 return 0;
1940#ifndef __UBOOT__
1941err:
1942 musb->g.dev.parent = NULL;
1943 device_unregister(&musb->g.dev);
1944 return status;
1945#endif
1946}
1947
1948void musb_gadget_cleanup(struct musb *musb)
1949{
1950#ifndef __UBOOT__
1951 usb_del_gadget_udc(&musb->g);
1952 if (musb->g.dev.parent)
1953 device_unregister(&musb->g.dev);
1954#endif
1955}
1956
1957/*
1958 * Register the gadget driver. Used by gadget drivers when
1959 * registering themselves with the controller.
1960 *
1961 * -EINVAL something went wrong (not driver)
1962 * -EBUSY another gadget is already using the controller
1963 * -ENOMEM no memory to perform the operation
1964 *
1965 * @param driver the gadget driver
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001966 * Return: <0 if error, 0 if everything is fine
Ilya Yanok06bb9202012-11-06 13:48:21 +00001967 */
1968#ifndef __UBOOT__
1969static int musb_gadget_start(struct usb_gadget *g,
1970 struct usb_gadget_driver *driver)
1971#else
1972int musb_gadget_start(struct usb_gadget *g,
1973 struct usb_gadget_driver *driver)
1974#endif
1975{
1976 struct musb *musb = gadget_to_musb(g);
1977#ifndef __UBOOT__
1978 struct usb_otg *otg = musb->xceiv->otg;
1979#endif
1980 unsigned long flags;
1981 int retval = -EINVAL;
1982
1983#ifndef __UBOOT__
1984 if (driver->max_speed < USB_SPEED_HIGH)
1985 goto err0;
1986#endif
1987
1988 pm_runtime_get_sync(musb->controller);
1989
1990#ifndef __UBOOT__
1991 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
1992#endif
1993
1994 musb->softconnect = 0;
1995 musb->gadget_driver = driver;
1996
1997 spin_lock_irqsave(&musb->lock, flags);
1998 musb->is_active = 1;
1999
2000#ifndef __UBOOT__
2001 otg_set_peripheral(otg, &musb->g);
2002 musb->xceiv->state = OTG_STATE_B_IDLE;
2003
2004 /*
2005 * FIXME this ignores the softconnect flag. Drivers are
2006 * allowed hold the peripheral inactive until for example
2007 * userspace hooks up printer hardware or DSP codecs, so
2008 * hosts only see fully functional devices.
2009 */
2010
2011 if (!is_otg_enabled(musb))
2012#endif
2013 musb_start(musb);
2014
2015 spin_unlock_irqrestore(&musb->lock, flags);
2016
2017#ifndef __UBOOT__
2018 if (is_otg_enabled(musb)) {
2019 struct usb_hcd *hcd = musb_to_hcd(musb);
2020
2021 dev_dbg(musb->controller, "OTG startup...\n");
2022
2023 /* REVISIT: funcall to other code, which also
2024 * handles power budgeting ... this way also
2025 * ensures HdrcStart is indirectly called.
2026 */
2027 retval = usb_add_hcd(musb_to_hcd(musb), 0, 0);
2028 if (retval < 0) {
2029 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
2030 goto err2;
2031 }
2032
2033 if ((musb->xceiv->last_event == USB_EVENT_ID)
2034 && otg->set_vbus)
2035 otg_set_vbus(otg, 1);
2036
2037 hcd->self.uses_pio_for_control = 1;
2038 }
2039 if (musb->xceiv->last_event == USB_EVENT_NONE)
2040 pm_runtime_put(musb->controller);
2041#endif
2042
2043 return 0;
2044
2045#ifndef __UBOOT__
2046err2:
2047 if (!is_otg_enabled(musb))
2048 musb_stop(musb);
2049err0:
2050 return retval;
2051#endif
2052}
2053
2054#ifndef __UBOOT__
2055static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
2056{
2057 int i;
2058 struct musb_hw_ep *hw_ep;
2059
2060 /* don't disconnect if it's not connected */
2061 if (musb->g.speed == USB_SPEED_UNKNOWN)
2062 driver = NULL;
2063 else
2064 musb->g.speed = USB_SPEED_UNKNOWN;
2065
2066 /* deactivate the hardware */
2067 if (musb->softconnect) {
2068 musb->softconnect = 0;
2069 musb_pullup(musb, 0);
2070 }
2071 musb_stop(musb);
2072
2073 /* killing any outstanding requests will quiesce the driver;
2074 * then report disconnect
2075 */
2076 if (driver) {
2077 for (i = 0, hw_ep = musb->endpoints;
2078 i < musb->nr_endpoints;
2079 i++, hw_ep++) {
2080 musb_ep_select(musb->mregs, i);
2081 if (hw_ep->is_shared_fifo /* || !epnum */) {
2082 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2083 } else {
2084 if (hw_ep->max_packet_sz_tx)
2085 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2086 if (hw_ep->max_packet_sz_rx)
2087 nuke(&hw_ep->ep_out, -ESHUTDOWN);
2088 }
2089 }
2090 }
2091}
2092
2093/*
2094 * Unregister the gadget driver. Used by gadget drivers when
2095 * unregistering themselves from the controller.
2096 *
2097 * @param driver the gadget driver to unregister
2098 */
2099static int musb_gadget_stop(struct usb_gadget *g,
2100 struct usb_gadget_driver *driver)
2101{
2102 struct musb *musb = gadget_to_musb(g);
2103 unsigned long flags;
2104
2105 if (musb->xceiv->last_event == USB_EVENT_NONE)
2106 pm_runtime_get_sync(musb->controller);
2107
2108 /*
2109 * REVISIT always use otg_set_peripheral() here too;
2110 * this needs to shut down the OTG engine.
2111 */
2112
2113 spin_lock_irqsave(&musb->lock, flags);
2114
2115 musb_hnp_stop(musb);
2116
2117 (void) musb_gadget_vbus_draw(&musb->g, 0);
2118
2119 musb->xceiv->state = OTG_STATE_UNDEFINED;
2120 stop_activity(musb, driver);
2121 otg_set_peripheral(musb->xceiv->otg, NULL);
2122
2123 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
2124
2125 musb->is_active = 0;
2126 musb_platform_try_idle(musb, 0);
2127 spin_unlock_irqrestore(&musb->lock, flags);
2128
2129 if (is_otg_enabled(musb)) {
2130 usb_remove_hcd(musb_to_hcd(musb));
2131 /* FIXME we need to be able to register another
2132 * gadget driver here and have everything work;
2133 * that currently misbehaves.
2134 */
2135 }
2136
2137 if (!is_otg_enabled(musb))
2138 musb_stop(musb);
2139
2140 pm_runtime_put(musb->controller);
2141
2142 return 0;
2143}
2144#endif
2145
2146/* ----------------------------------------------------------------------- */
2147
2148/* lifecycle operations called through plat_uds.c */
2149
2150void musb_g_resume(struct musb *musb)
2151{
2152#ifndef __UBOOT__
2153 musb->is_suspended = 0;
2154 switch (musb->xceiv->state) {
2155 case OTG_STATE_B_IDLE:
2156 break;
2157 case OTG_STATE_B_WAIT_ACON:
2158 case OTG_STATE_B_PERIPHERAL:
2159 musb->is_active = 1;
2160 if (musb->gadget_driver && musb->gadget_driver->resume) {
2161 spin_unlock(&musb->lock);
2162 musb->gadget_driver->resume(&musb->g);
2163 spin_lock(&musb->lock);
2164 }
2165 break;
2166 default:
2167 WARNING("unhandled RESUME transition (%s)\n",
2168 otg_state_string(musb->xceiv->state));
2169 }
2170#endif
2171}
2172
2173/* called when SOF packets stop for 3+ msec */
2174void musb_g_suspend(struct musb *musb)
2175{
2176#ifndef __UBOOT__
2177 u8 devctl;
2178
2179 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2180 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2181
2182 switch (musb->xceiv->state) {
2183 case OTG_STATE_B_IDLE:
2184 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2185 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2186 break;
2187 case OTG_STATE_B_PERIPHERAL:
2188 musb->is_suspended = 1;
2189 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2190 spin_unlock(&musb->lock);
2191 musb->gadget_driver->suspend(&musb->g);
2192 spin_lock(&musb->lock);
2193 }
2194 break;
2195 default:
2196 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2197 * A_PERIPHERAL may need care too
2198 */
2199 WARNING("unhandled SUSPEND transition (%s)\n",
2200 otg_state_string(musb->xceiv->state));
2201 }
2202#endif
2203}
2204
2205/* Called during SRP */
2206void musb_g_wakeup(struct musb *musb)
2207{
2208 musb_gadget_wakeup(&musb->g);
2209}
2210
2211/* called when VBUS drops below session threshold, and in other cases */
2212void musb_g_disconnect(struct musb *musb)
2213{
2214 void __iomem *mregs = musb->mregs;
2215 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2216
2217 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2218
2219 /* clear HR */
2220 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2221
2222 /* don't draw vbus until new b-default session */
2223 (void) musb_gadget_vbus_draw(&musb->g, 0);
2224
2225 musb->g.speed = USB_SPEED_UNKNOWN;
2226 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2227 spin_unlock(&musb->lock);
2228 musb->gadget_driver->disconnect(&musb->g);
2229 spin_lock(&musb->lock);
2230 }
2231
2232#ifndef __UBOOT__
2233 switch (musb->xceiv->state) {
2234 default:
2235 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
2236 otg_state_string(musb->xceiv->state));
2237 musb->xceiv->state = OTG_STATE_A_IDLE;
2238 MUSB_HST_MODE(musb);
2239 break;
2240 case OTG_STATE_A_PERIPHERAL:
2241 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2242 MUSB_HST_MODE(musb);
2243 break;
2244 case OTG_STATE_B_WAIT_ACON:
2245 case OTG_STATE_B_HOST:
2246 case OTG_STATE_B_PERIPHERAL:
2247 case OTG_STATE_B_IDLE:
2248 musb->xceiv->state = OTG_STATE_B_IDLE;
2249 break;
2250 case OTG_STATE_B_SRP_INIT:
2251 break;
2252 }
2253#endif
2254
2255 musb->is_active = 0;
2256}
2257
2258void musb_g_reset(struct musb *musb)
2259__releases(musb->lock)
2260__acquires(musb->lock)
2261{
2262 void __iomem *mbase = musb->mregs;
2263 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2264 u8 power;
2265
2266#ifndef __UBOOT__
2267 dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n",
2268 (devctl & MUSB_DEVCTL_BDEVICE)
2269 ? "B-Device" : "A-Device",
2270 musb_readb(mbase, MUSB_FADDR),
2271 musb->gadget_driver
2272 ? musb->gadget_driver->driver.name
2273 : NULL
2274 );
2275#endif
2276
2277 /* report disconnect, if we didn't already (flushing EP state) */
2278 if (musb->g.speed != USB_SPEED_UNKNOWN)
2279 musb_g_disconnect(musb);
2280
2281 /* clear HR */
2282 else if (devctl & MUSB_DEVCTL_HR)
2283 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2284
Ilya Yanok06bb9202012-11-06 13:48:21 +00002285 /* what speed did we negotiate? */
2286 power = musb_readb(mbase, MUSB_POWER);
2287 musb->g.speed = (power & MUSB_POWER_HSMODE)
2288 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2289
2290 /* start in USB_STATE_DEFAULT */
2291 musb->is_active = 1;
2292 musb->is_suspended = 0;
2293 MUSB_DEV_MODE(musb);
2294 musb->address = 0;
2295 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2296
2297 musb->may_wakeup = 0;
2298 musb->g.b_hnp_enable = 0;
2299 musb->g.a_alt_hnp_support = 0;
2300 musb->g.a_hnp_support = 0;
2301
2302#ifndef __UBOOT__
2303 /* Normal reset, as B-Device;
2304 * or else after HNP, as A-Device
2305 */
2306 if (devctl & MUSB_DEVCTL_BDEVICE) {
2307 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2308 musb->g.is_a_peripheral = 0;
2309 } else if (is_otg_enabled(musb)) {
2310 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
2311 musb->g.is_a_peripheral = 1;
2312 } else
2313 WARN_ON(1);
2314
2315 /* start with default limits on VBUS power draw */
2316 (void) musb_gadget_vbus_draw(&musb->g,
2317 is_otg_enabled(musb) ? 8 : 100);
2318#endif
2319}