blob: f62ffaede1abba48cc120445c036200922273669 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbi550a7372008-07-24 12:27:36 +03002/*
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
Sergei Shtylyovcea83242009-11-18 22:51:18 +03008 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
Felipe Balbi550a7372008-07-24 12:27:36 +03009 */
10
11#include <linux/kernel.h>
12#include <linux/list.h>
13#include <linux/timer.h>
14#include <linux/module.h>
15#include <linux/smp.h>
16#include <linux/spinlock.h>
17#include <linux/delay.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030018#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030020
21#include "musb_core.h"
Bin Liufc780032016-06-30 12:12:27 -050022#include "musb_trace.h"
Felipe Balbi550a7372008-07-24 12:27:36 +030023
24
Felipe Balbi550a7372008-07-24 12:27:36 +030025/* ----------------------------------------------------------------------- */
26
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010027#define is_buffer_mapped(req) (is_dma_capable() && \
28 (req->map_state != UN_MAPPED))
29
Hema Kalliguddi92d27112010-11-15 04:24:01 -060030/* Maps the buffer to dma */
31
32static inline void map_dma_buffer(struct musb_request *request,
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010033 struct musb *musb, struct musb_ep *musb_ep)
Hema Kalliguddi92d27112010-11-15 04:24:01 -060034{
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +010035 int compatible = true;
36 struct dma_controller *dma = musb->dma_controller;
37
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010038 request->map_state = UN_MAPPED;
39
40 if (!is_dma_capable() || !musb_ep->dma)
41 return;
42
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +010043 /* Check if DMA engine can handle this request.
44 * DMA code must reject the USB request explicitly.
45 * Default behaviour is to map the request.
46 */
47 if (dma->is_compatible)
48 compatible = dma->is_compatible(musb_ep->dma,
49 musb_ep->packet_sz, request->request.buf,
50 request->request.length);
51 if (!compatible)
52 return;
53
Hema Kalliguddi92d27112010-11-15 04:24:01 -060054 if (request->request.dma == DMA_ADDR_INVALID) {
Sebastian Andrzej Siewior7b360f42013-08-13 19:35:43 +020055 dma_addr_t dma_addr;
56 int ret;
57
58 dma_addr = dma_map_single(
Hema Kalliguddi92d27112010-11-15 04:24:01 -060059 musb->controller,
60 request->request.buf,
61 request->request.length,
62 request->tx
63 ? DMA_TO_DEVICE
64 : DMA_FROM_DEVICE);
Sebastian Andrzej Siewior7b360f42013-08-13 19:35:43 +020065 ret = dma_mapping_error(musb->controller, dma_addr);
66 if (ret)
67 return;
68
69 request->request.dma = dma_addr;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010070 request->map_state = MUSB_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -060071 } else {
72 dma_sync_single_for_device(musb->controller,
73 request->request.dma,
74 request->request.length,
75 request->tx
76 ? DMA_TO_DEVICE
77 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010078 request->map_state = PRE_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -060079 }
80}
81
82/* Unmap the buffer from dma and maps it back to cpu */
83static inline void unmap_dma_buffer(struct musb_request *request,
84 struct musb *musb)
85{
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +053086 struct musb_ep *musb_ep = request->ep;
87
88 if (!is_buffer_mapped(request) || !musb_ep->dma)
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010089 return;
90
Hema Kalliguddi92d27112010-11-15 04:24:01 -060091 if (request->request.dma == DMA_ADDR_INVALID) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +030092 dev_vdbg(musb->controller,
93 "not unmapping a never mapped buffer\n");
Hema Kalliguddi92d27112010-11-15 04:24:01 -060094 return;
95 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010096 if (request->map_state == MUSB_MAPPED) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -060097 dma_unmap_single(musb->controller,
98 request->request.dma,
99 request->request.length,
100 request->tx
101 ? DMA_TO_DEVICE
102 : DMA_FROM_DEVICE);
103 request->request.dma = DMA_ADDR_INVALID;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100104 } else { /* PRE_MAPPED */
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600105 dma_sync_single_for_cpu(musb->controller,
106 request->request.dma,
107 request->request.length,
108 request->tx
109 ? DMA_TO_DEVICE
110 : DMA_FROM_DEVICE);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600111 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100112 request->map_state = UN_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600113}
114
Felipe Balbi550a7372008-07-24 12:27:36 +0300115/*
116 * Immediately complete a request.
117 *
118 * @param request the request to complete
119 * @param status the status to complete the request with
120 * Context: controller locked, IRQs blocked.
121 */
122void musb_g_giveback(
123 struct musb_ep *ep,
124 struct usb_request *request,
125 int status)
126__releases(ep->musb->lock)
127__acquires(ep->musb->lock)
128{
129 struct musb_request *req;
130 struct musb *musb;
131 int busy = ep->busy;
132
133 req = to_musb_request(request);
134
Felipe Balbiad1adb82011-02-16 12:40:05 +0200135 list_del(&req->list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300136 if (req->request.status == -EINPROGRESS)
137 req->request.status = status;
138 musb = req->musb;
139
140 ep->busy = 1;
141 spin_unlock(&musb->lock);
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +0530142
143 if (!dma_mapping_error(&musb->g.dev, request->dma))
144 unmap_dma_buffer(req, musb);
145
Bin Liufc780032016-06-30 12:12:27 -0500146 trace_musb_req_gb(req);
Michal Sojka304f7e52014-09-24 22:43:19 +0200147 usb_gadget_giveback_request(&req->ep->end_point, &req->request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300148 spin_lock(&musb->lock);
149 ep->busy = busy;
150}
151
152/* ----------------------------------------------------------------------- */
153
154/*
155 * Abort requests queued to an endpoint using the status. Synchronous.
156 * caller locked controller and blocked irqs, and selected this ep.
157 */
158static void nuke(struct musb_ep *ep, const int status)
159{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300160 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300161 struct musb_request *req = NULL;
162 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
163
164 ep->busy = 1;
165
166 if (is_dma_capable() && ep->dma) {
167 struct dma_controller *c = ep->musb->dma_controller;
168 int value;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700169
Felipe Balbi550a7372008-07-24 12:27:36 +0300170 if (ep->is_in) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700171 /*
172 * The programming guide says that we must not clear
173 * the DMAMODE bit before DMAENAB, so we only
174 * clear it in the second write...
175 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300176 musb_writew(epio, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700177 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
Felipe Balbi550a7372008-07-24 12:27:36 +0300178 musb_writew(epio, MUSB_TXCSR,
179 0 | MUSB_TXCSR_FLUSHFIFO);
180 } else {
181 musb_writew(epio, MUSB_RXCSR,
182 0 | MUSB_RXCSR_FLUSHFIFO);
183 musb_writew(epio, MUSB_RXCSR,
184 0 | MUSB_RXCSR_FLUSHFIFO);
185 }
186
187 value = c->channel_abort(ep->dma);
Bin Liub99d3652016-06-30 12:12:22 -0500188 musb_dbg(musb, "%s: abort DMA --> %d", ep->name, value);
Felipe Balbi550a7372008-07-24 12:27:36 +0300189 c->channel_release(ep->dma);
190 ep->dma = NULL;
191 }
192
Felipe Balbiad1adb82011-02-16 12:40:05 +0200193 while (!list_empty(&ep->req_list)) {
194 req = list_first_entry(&ep->req_list, struct musb_request, list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300195 musb_g_giveback(ep, &req->request, status);
196 }
197}
198
199/* ----------------------------------------------------------------------- */
200
201/* Data transfers - pure PIO, pure DMA, or mixed mode */
202
203/*
204 * This assumes the separate CPPI engine is responding to DMA requests
205 * from the usb core ... sequenced a bit differently from mentor dma.
206 */
207
208static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
209{
210 if (can_bulk_split(musb, ep->type))
211 return ep->hw_ep->max_packet_sz_tx;
212 else
213 return ep->packet_sz;
214}
215
Felipe Balbi550a7372008-07-24 12:27:36 +0300216/*
217 * An endpoint is transmitting data. This can be called either from
218 * the IRQ routine or from ep.queue() to kickstart a request on an
219 * endpoint.
220 *
221 * Context: controller locked, IRQs blocked, endpoint selected
222 */
223static void txstate(struct musb *musb, struct musb_request *req)
224{
225 u8 epnum = req->epnum;
226 struct musb_ep *musb_ep;
227 void __iomem *epio = musb->endpoints[epnum].regs;
228 struct usb_request *request;
229 u16 fifo_count = 0, csr;
230 int use_dma = 0;
231
232 musb_ep = req->ep;
233
Vikram Panditaabf710e2012-05-18 13:48:04 -0700234 /* Check if EP is disabled */
235 if (!musb_ep->desc) {
Bin Liub99d3652016-06-30 12:12:22 -0500236 musb_dbg(musb, "ep:%s disabled - ignore request",
Vikram Panditaabf710e2012-05-18 13:48:04 -0700237 musb_ep->end_point.name);
238 return;
239 }
240
Felipe Balbi550a7372008-07-24 12:27:36 +0300241 /* we shouldn't get here while DMA is active ... but we do ... */
242 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Bin Liub99d3652016-06-30 12:12:22 -0500243 musb_dbg(musb, "dma pending...");
Felipe Balbi550a7372008-07-24 12:27:36 +0300244 return;
245 }
246
247 /* read TXCSR before */
248 csr = musb_readw(epio, MUSB_TXCSR);
249
250 request = &req->request;
251 fifo_count = min(max_ep_writesize(musb, musb_ep),
252 (int)(request->length - request->actual));
253
254 if (csr & MUSB_TXCSR_TXPKTRDY) {
Bin Liub99d3652016-06-30 12:12:22 -0500255 musb_dbg(musb, "%s old packet still ready , txcsr %03x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300256 musb_ep->end_point.name, csr);
257 return;
258 }
259
260 if (csr & MUSB_TXCSR_P_SENDSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -0500261 musb_dbg(musb, "%s stalling, txcsr %03x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300262 musb_ep->end_point.name, csr);
263 return;
264 }
265
Bin Liub99d3652016-06-30 12:12:22 -0500266 musb_dbg(musb, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300267 epnum, musb_ep->packet_sz, fifo_count,
268 csr);
269
270#ifndef CONFIG_MUSB_PIO_ONLY
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100271 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300272 struct dma_controller *c = musb->dma_controller;
Ming Lei66af83d2010-09-20 10:32:06 +0300273 size_t request_size;
274
275 /* setup DMA, then program endpoint CSR */
276 request_size = min_t(size_t, request->length - request->actual,
277 musb_ep->dma->max_len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300278
Ajay Kumar Guptad17d5352012-07-20 11:07:23 +0530279 use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300280
281 /* MUSB_TXCSR_P_ISO is still set correctly */
282
Felipe Balbi03840fa2015-08-06 10:47:16 -0500283 if (musb_dma_inventra(musb) || musb_dma_ux500(musb)) {
Anand Gadiyard1043a22009-04-02 12:07:08 -0700284 if (request_size < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300285 musb_ep->dma->desired_mode = 0;
286 else
287 musb_ep->dma->desired_mode = 1;
288
289 use_dma = use_dma && c->channel_program(
290 musb_ep->dma, musb_ep->packet_sz,
291 musb_ep->dma->desired_mode,
Cliff Cai796a83f2009-12-21 21:18:02 -0500292 request->dma + request->actual, request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300293 if (use_dma) {
294 if (musb_ep->dma->desired_mode == 0) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700295 /*
296 * We must not clear the DMAMODE bit
297 * before the DMAENAB bit -- and the
298 * latter doesn't always get cleared
299 * before we get here...
300 */
301 csr &= ~(MUSB_TXCSR_AUTOSET
302 | MUSB_TXCSR_DMAENAB);
303 musb_writew(epio, MUSB_TXCSR, csr
304 | MUSB_TXCSR_P_WZC_BITS);
305 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300306 csr |= (MUSB_TXCSR_DMAENAB |
307 MUSB_TXCSR_MODE);
308 /* against programming guide */
Ming Leif11d8932010-09-24 13:44:04 +0300309 } else {
310 csr |= (MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300311 | MUSB_TXCSR_DMAMODE
312 | MUSB_TXCSR_MODE);
supriya karanthbb3a2ef2012-12-06 11:12:48 +0530313 /*
314 * Enable Autoset according to table
315 * below
316 * bulk_split hb_mult Autoset_Enable
317 * 0 0 Yes(Normal)
318 * 0 >0 No(High BW ISO)
319 * 1 0 Yes(HS bulk)
320 * 1 >0 Yes(FS bulk)
321 */
322 if (!musb_ep->hb_mult ||
Geyslan G. Bem1a171622015-12-10 17:50:12 -0300323 can_bulk_split(musb,
324 musb_ep->type))
Ming Leif11d8932010-09-24 13:44:04 +0300325 csr |= MUSB_TXCSR_AUTOSET;
326 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300327 csr &= ~MUSB_TXCSR_P_UNDERRUN;
Ming Leif11d8932010-09-24 13:44:04 +0300328
Felipe Balbi550a7372008-07-24 12:27:36 +0300329 musb_writew(epio, MUSB_TXCSR, csr);
330 }
331 }
332
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700333 if (is_cppi_enabled(musb)) {
Sebastian Andrzej Siewiorfc525752013-08-13 19:38:23 +0200334 /* program endpoint CSR first, then setup DMA */
335 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
336 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
337 MUSB_TXCSR_MODE;
338 musb_writew(epio, MUSB_TXCSR, (MUSB_TXCSR_P_WZC_BITS &
339 ~MUSB_TXCSR_P_UNDERRUN) | csr);
340
341 /* ensure writebuffer is empty */
342 csr = musb_readw(epio, MUSB_TXCSR);
343
344 /*
345 * NOTE host side sets DMAENAB later than this; both are
346 * OK since the transfer dma glue (between CPPI and
347 * Mentor fifos) just tells CPPI it could start. Data
348 * only moves to the USB TX fifo when both fifos are
349 * ready.
350 */
351 /*
352 * "mode" is irrelevant here; handle terminating ZLPs
353 * like PIO does, since the hardware RNDIS mode seems
354 * unreliable except for the
355 * last-packet-is-already-short case.
356 */
357 use_dma = use_dma && c->channel_program(
358 musb_ep->dma, musb_ep->packet_sz,
359 0,
360 request->dma + request->actual,
361 request_size);
362 if (!use_dma) {
363 c->channel_release(musb_ep->dma);
364 musb_ep->dma = NULL;
365 csr &= ~MUSB_TXCSR_DMAENAB;
366 musb_writew(epio, MUSB_TXCSR, csr);
367 /* invariant: prequest->buf is non-null */
368 }
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700369 } else if (tusb_dma_omap(musb))
Sebastian Andrzej Siewiorfc525752013-08-13 19:38:23 +0200370 use_dma = use_dma && c->channel_program(
371 musb_ep->dma, musb_ep->packet_sz,
372 request->zero,
373 request->dma + request->actual,
374 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300375 }
376#endif
377
378 if (!use_dma) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600379 /*
380 * Unmap the dma buffer back to cpu if dma channel
381 * programming fails
382 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100383 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600384
Felipe Balbi550a7372008-07-24 12:27:36 +0300385 musb_write_fifo(musb_ep->hw_ep, fifo_count,
386 (u8 *) (request->buf + request->actual));
387 request->actual += fifo_count;
388 csr |= MUSB_TXCSR_TXPKTRDY;
389 csr &= ~MUSB_TXCSR_P_UNDERRUN;
390 musb_writew(epio, MUSB_TXCSR, csr);
391 }
392
393 /* host may already have the data when this message shows... */
Bin Liub99d3652016-06-30 12:12:22 -0500394 musb_dbg(musb, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300395 musb_ep->end_point.name, use_dma ? "dma" : "pio",
396 request->actual, request->length,
397 musb_readw(epio, MUSB_TXCSR),
398 fifo_count,
399 musb_readw(epio, MUSB_TXMAXP));
400}
401
402/*
403 * FIFO state update (e.g. data ready).
404 * Called from IRQ, with controller locked.
405 */
406void musb_g_tx(struct musb *musb, u8 epnum)
407{
408 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200409 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300410 struct usb_request *request;
411 u8 __iomem *mbase = musb->mregs;
412 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
413 void __iomem *epio = musb->endpoints[epnum].regs;
414 struct dma_channel *dma;
415
416 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200417 req = next_request(musb_ep);
418 request = &req->request;
Felipe Balbi550a7372008-07-24 12:27:36 +0300419
420 csr = musb_readw(epio, MUSB_TXCSR);
Bin Liub99d3652016-06-30 12:12:22 -0500421 musb_dbg(musb, "<== %s, txcsr %04x", musb_ep->end_point.name, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300422
423 dma = is_dma_capable() ? musb_ep->dma : NULL;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300424
425 /*
426 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
427 * probably rates reporting as a host error.
428 */
429 if (csr & MUSB_TXCSR_P_SENTSTALL) {
430 csr |= MUSB_TXCSR_P_WZC_BITS;
431 csr &= ~MUSB_TXCSR_P_SENTSTALL;
432 musb_writew(epio, MUSB_TXCSR, csr);
433 return;
434 }
435
436 if (csr & MUSB_TXCSR_P_UNDERRUN) {
437 /* We NAKed, no big deal... little reason to care. */
438 csr |= MUSB_TXCSR_P_WZC_BITS;
439 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
440 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300441 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
442 epnum, request);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300443 }
444
445 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
446 /*
447 * SHOULD NOT HAPPEN... has with CPPI though, after
448 * changing SENDSTALL (and other cases); harmless?
Felipe Balbi550a7372008-07-24 12:27:36 +0300449 */
Bin Liub99d3652016-06-30 12:12:22 -0500450 musb_dbg(musb, "%s dma still busy?", musb_ep->end_point.name);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300451 return;
452 }
453
454 if (request) {
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300455
Bin Liu9aea9b62018-04-30 11:20:54 -0500456 trace_musb_req_tx(req);
457
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300458 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300459 csr |= MUSB_TXCSR_P_WZC_BITS;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300460 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
Mian Yousaf Kaukab100d4a92011-03-15 16:24:24 +0100461 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
Felipe Balbi550a7372008-07-24 12:27:36 +0300462 musb_writew(epio, MUSB_TXCSR, csr);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300463 /* Ensure writebuffer is empty. */
464 csr = musb_readw(epio, MUSB_TXCSR);
465 request->actual += musb_ep->dma->actual_len;
Bin Liub99d3652016-06-30 12:12:22 -0500466 musb_dbg(musb, "TXCSR%d %04x, DMA off, len %zu, req %p",
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300467 epnum, csr, musb_ep->dma->actual_len, request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300468 }
469
Ming Leie7379aa2010-09-24 13:44:14 +0300470 /*
471 * First, maybe a terminating short packet. Some DMA
472 * engines might handle this by themselves.
473 */
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700474 if ((request->zero && request->length)
Ming Leie7379aa2010-09-24 13:44:14 +0300475 && (request->length % musb_ep->packet_sz == 0)
Paul Elderc418fd62019-01-30 08:13:21 -0600476 && (request->actual == request->length)) {
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700477
Ming Leie7379aa2010-09-24 13:44:14 +0300478 /*
479 * On DMA completion, FIFO may not be
480 * available yet...
481 */
482 if (csr & MUSB_TXCSR_TXPKTRDY)
483 return;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300484
Ming Leie7379aa2010-09-24 13:44:14 +0300485 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
486 | MUSB_TXCSR_TXPKTRDY);
487 request->zero = 0;
488 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300489
Ming Leie7379aa2010-09-24 13:44:14 +0300490 if (request->actual == request->length) {
491 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530492 /*
493 * In the giveback function the MUSB lock is
494 * released and acquired after sometime. During
495 * this time period the INDEX register could get
496 * changed by the gadget_queue function especially
497 * on SMP systems. Reselect the INDEX to be sure
498 * we are reading/modifying the right registers
499 */
500 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200501 req = musb_ep->desc ? next_request(musb_ep) : NULL;
502 if (!req) {
Bin Liub99d3652016-06-30 12:12:22 -0500503 musb_dbg(musb, "%s idle now",
Ming Leie7379aa2010-09-24 13:44:14 +0300504 musb_ep->end_point.name);
505 return;
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300506 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300507 }
508
Felipe Balbiad1adb82011-02-16 12:40:05 +0200509 txstate(musb, req);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300510 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300511}
512
513/* ------------------------------------------------------------ */
514
Felipe Balbi550a7372008-07-24 12:27:36 +0300515/*
516 * Context: controller locked, IRQs blocked, endpoint selected
517 */
518static void rxstate(struct musb *musb, struct musb_request *req)
519{
Felipe Balbi550a7372008-07-24 12:27:36 +0300520 const u8 epnum = req->epnum;
521 struct usb_request *request = &req->request;
Ming Leibd2e74d2010-09-20 10:32:01 +0300522 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300523 void __iomem *epio = musb->endpoints[epnum].regs;
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400524 unsigned len = 0;
525 u16 fifo_count;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300526 u16 csr = musb_readw(epio, MUSB_RXCSR);
Ming Leibd2e74d2010-09-20 10:32:01 +0300527 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700528 u8 use_mode_1;
Ming Leibd2e74d2010-09-20 10:32:01 +0300529
530 if (hw_ep->is_shared_fifo)
531 musb_ep = &hw_ep->ep_in;
532 else
533 musb_ep = &hw_ep->ep_out;
534
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400535 fifo_count = musb_ep->packet_sz;
Felipe Balbi550a7372008-07-24 12:27:36 +0300536
Vikram Panditaabf710e2012-05-18 13:48:04 -0700537 /* Check if EP is disabled */
538 if (!musb_ep->desc) {
Bin Liub99d3652016-06-30 12:12:22 -0500539 musb_dbg(musb, "ep:%s disabled - ignore request",
Vikram Panditaabf710e2012-05-18 13:48:04 -0700540 musb_ep->end_point.name);
541 return;
542 }
543
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300544 /* We shouldn't get here while DMA is active, but we do... */
545 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Bin Liub99d3652016-06-30 12:12:22 -0500546 musb_dbg(musb, "DMA pending...");
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300547 return;
548 }
549
550 if (csr & MUSB_RXCSR_P_SENDSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -0500551 musb_dbg(musb, "%s stalling, RXCSR %04x",
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300552 musb_ep->end_point.name, csr);
553 return;
554 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300555
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700556 if (is_cppi_enabled(musb) && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300557 struct dma_controller *c = musb->dma_controller;
558 struct dma_channel *channel = musb_ep->dma;
559
560 /* NOTE: CPPI won't actually stop advancing the DMA
561 * queue after short packet transfers, so this is almost
562 * always going to run as IRQ-per-packet DMA so that
563 * faults will be handled correctly.
564 */
565 if (c->channel_program(channel,
566 musb_ep->packet_sz,
567 !request->short_not_ok,
568 request->dma + request->actual,
569 request->length - request->actual)) {
570
571 /* make sure that if an rxpkt arrived after the irq,
572 * the cppi engine will be ready to take it as soon
573 * as DMA is enabled
574 */
575 csr &= ~(MUSB_RXCSR_AUTOCLEAR
576 | MUSB_RXCSR_DMAMODE);
577 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
578 musb_writew(epio, MUSB_RXCSR, csr);
579 return;
580 }
581 }
582
583 if (csr & MUSB_RXCSR_RXPKTRDY) {
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400584 fifo_count = musb_readw(epio, MUSB_RXCOUNT);
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700585
586 /*
Felipe Balbi00a89182012-10-26 09:55:31 +0300587 * Enable Mode 1 on RX transfers only when short_not_ok flag
588 * is set. Currently short_not_ok flag is set only from
589 * file_storage and f_mass_storage drivers
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700590 */
Felipe Balbi00a89182012-10-26 09:55:31 +0300591
592 if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700593 use_mode_1 = 1;
594 else
595 use_mode_1 = 0;
596
Felipe Balbi550a7372008-07-24 12:27:36 +0300597 if (request->actual < request->length) {
Felipe Balbi03840fa2015-08-06 10:47:16 -0500598 if (!is_buffer_mapped(req))
599 goto buffer_aint_mapped;
600
601 if (musb_dma_inventra(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300602 struct dma_controller *c;
603 struct dma_channel *channel;
604 int use_dma = 0;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200605 unsigned int transfer_size;
Felipe Balbi550a7372008-07-24 12:27:36 +0300606
607 c = musb->dma_controller;
608 channel = musb_ep->dma;
609
Felipe Balbi00a89182012-10-26 09:55:31 +0300610 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
611 * mode 0 only. So we do not get endpoint interrupts due to DMA
612 * completion. We only get interrupts from DMA controller.
613 *
614 * We could operate in DMA mode 1 if we knew the size of the tranfer
615 * in advance. For mass storage class, request->length = what the host
616 * sends, so that'd work. But for pretty much everything else,
617 * request->length is routinely more than what the host sends. For
618 * most these gadgets, end of is signified either by a short packet,
619 * or filling the last byte of the buffer. (Sending extra data in
620 * that last pckate should trigger an overflow fault.) But in mode 1,
621 * we don't get DMA completion interrupt for short packets.
622 *
623 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
624 * to get endpoint interrupt on every DMA req, but that didn't seem
625 * to work reliably.
626 *
627 * REVISIT an updated g_file_storage can set req->short_not_ok, which
628 * then becomes usable as a runtime "use mode 1" hint...
629 */
630
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700631 /* Experimental: Mode1 works with mass storage use cases */
632 if (use_mode_1) {
Ming Lei9001d802010-09-25 05:50:43 -0500633 csr |= MUSB_RXCSR_AUTOCLEAR;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700634 musb_writew(epio, MUSB_RXCSR, csr);
635 csr |= MUSB_RXCSR_DMAENAB;
636 musb_writew(epio, MUSB_RXCSR, csr);
637
638 /*
639 * this special sequence (enabling and then
640 * disabling MUSB_RXCSR_DMAMODE) is required
641 * to get DMAReq to activate
642 */
643 musb_writew(epio, MUSB_RXCSR,
644 csr | MUSB_RXCSR_DMAMODE);
645 musb_writew(epio, MUSB_RXCSR, csr);
646
Felipe Balbi37730ec2013-02-06 10:19:15 +0200647 transfer_size = min_t(unsigned int,
648 request->length -
649 request->actual,
Roger Quadros660fa882012-08-07 16:26:32 +0300650 channel->max_len);
651 musb_ep->dma->desired_mode = 1;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700652 } else {
653 if (!musb_ep->hb_mult &&
654 musb_ep->hw_ep->rx_double_buffered)
655 csr |= MUSB_RXCSR_AUTOCLEAR;
656 csr |= MUSB_RXCSR_DMAENAB;
657 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300658
Roger Quadros660fa882012-08-07 16:26:32 +0300659 transfer_size = min(request->length - request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400660 (unsigned)fifo_count);
Roger Quadros660fa882012-08-07 16:26:32 +0300661 musb_ep->dma->desired_mode = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300662 }
663
Roger Quadros660fa882012-08-07 16:26:32 +0300664 use_dma = c->channel_program(
665 channel,
666 musb_ep->packet_sz,
667 channel->desired_mode,
668 request->dma
669 + request->actual,
670 transfer_size);
671
Felipe Balbi550a7372008-07-24 12:27:36 +0300672 if (use_dma)
673 return;
674 }
Felipe Balbi03840fa2015-08-06 10:47:16 -0500675
676 if ((musb_dma_ux500(musb)) &&
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100677 (request->actual < request->length)) {
678
679 struct dma_controller *c;
680 struct dma_channel *channel;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200681 unsigned int transfer_size = 0;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100682
683 c = musb->dma_controller;
684 channel = musb_ep->dma;
685
686 /* In case first packet is short */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400687 if (fifo_count < musb_ep->packet_sz)
688 transfer_size = fifo_count;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100689 else if (request->short_not_ok)
Felipe Balbi37730ec2013-02-06 10:19:15 +0200690 transfer_size = min_t(unsigned int,
691 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100692 request->actual,
693 channel->max_len);
694 else
Felipe Balbi37730ec2013-02-06 10:19:15 +0200695 transfer_size = min_t(unsigned int,
696 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100697 request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400698 (unsigned)fifo_count);
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100699
700 csr &= ~MUSB_RXCSR_DMAMODE;
701 csr |= (MUSB_RXCSR_DMAENAB |
702 MUSB_RXCSR_AUTOCLEAR);
703
704 musb_writew(epio, MUSB_RXCSR, csr);
705
706 if (transfer_size <= musb_ep->packet_sz) {
707 musb_ep->dma->desired_mode = 0;
708 } else {
709 musb_ep->dma->desired_mode = 1;
710 /* Mode must be set after DMAENAB */
711 csr |= MUSB_RXCSR_DMAMODE;
712 musb_writew(epio, MUSB_RXCSR, csr);
713 }
714
715 if (c->channel_program(channel,
716 musb_ep->packet_sz,
717 channel->desired_mode,
718 request->dma
719 + request->actual,
720 transfer_size))
721
722 return;
723 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300724
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400725 len = request->length - request->actual;
Bin Liub99d3652016-06-30 12:12:22 -0500726 musb_dbg(musb, "%s OUT/RX pio fifo %d/%d, maxpacket %d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300727 musb_ep->end_point.name,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400728 fifo_count, len,
Felipe Balbi550a7372008-07-24 12:27:36 +0300729 musb_ep->packet_sz);
730
Felipe Balbic2c96322009-02-21 15:29:42 -0800731 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300732
Felipe Balbi03840fa2015-08-06 10:47:16 -0500733 if (tusb_dma_omap(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300734 struct dma_controller *c = musb->dma_controller;
735 struct dma_channel *channel = musb_ep->dma;
736 u32 dma_addr = request->dma + request->actual;
737 int ret;
738
739 ret = c->channel_program(channel,
740 musb_ep->packet_sz,
741 channel->desired_mode,
742 dma_addr,
743 fifo_count);
744 if (ret)
745 return;
746 }
Felipe Balbi03840fa2015-08-06 10:47:16 -0500747
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600748 /*
749 * Unmap the dma buffer back to cpu if dma channel
750 * programming fails. This buffer is mapped if the
751 * channel allocation is successful
752 */
Felipe Balbi03840fa2015-08-06 10:47:16 -0500753 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600754
Felipe Balbi03840fa2015-08-06 10:47:16 -0500755 /*
756 * Clear DMAENAB and AUTOCLEAR for the
757 * PIO mode transfer
758 */
759 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
760 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300761
Felipe Balbi03840fa2015-08-06 10:47:16 -0500762buffer_aint_mapped:
Felipe Balbi550a7372008-07-24 12:27:36 +0300763 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
764 (request->buf + request->actual));
765 request->actual += fifo_count;
766
767 /* REVISIT if we left anything in the fifo, flush
768 * it and report -EOVERFLOW
769 */
770
771 /* ack the read! */
772 csr |= MUSB_RXCSR_P_WZC_BITS;
773 csr &= ~MUSB_RXCSR_RXPKTRDY;
774 musb_writew(epio, MUSB_RXCSR, csr);
775 }
776 }
777
778 /* reach the end or short packet detected */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400779 if (request->actual == request->length ||
780 fifo_count < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300781 musb_g_giveback(musb_ep, request, 0);
782}
783
784/*
785 * Data ready for a request; called from IRQ
786 */
787void musb_g_rx(struct musb *musb, u8 epnum)
788{
789 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200790 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300791 struct usb_request *request;
792 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300793 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300794 void __iomem *epio = musb->endpoints[epnum].regs;
795 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300796 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
797
798 if (hw_ep->is_shared_fifo)
799 musb_ep = &hw_ep->ep_in;
800 else
801 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300802
803 musb_ep_select(mbase, epnum);
804
Felipe Balbiad1adb82011-02-16 12:40:05 +0200805 req = next_request(musb_ep);
806 if (!req)
Maulik Mankad0abdc362009-12-22 16:18:19 +0530807 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300808
Bin Liufc780032016-06-30 12:12:27 -0500809 trace_musb_req_rx(req);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200810 request = &req->request;
811
Felipe Balbi550a7372008-07-24 12:27:36 +0300812 csr = musb_readw(epio, MUSB_RXCSR);
813 dma = is_dma_capable() ? musb_ep->dma : NULL;
814
Bin Liub99d3652016-06-30 12:12:22 -0500815 musb_dbg(musb, "<== %s, rxcsr %04x%s %p", musb_ep->end_point.name,
Felipe Balbi550a7372008-07-24 12:27:36 +0300816 csr, dma ? " (dma)" : "", request);
817
818 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300819 csr |= MUSB_RXCSR_P_WZC_BITS;
820 csr &= ~MUSB_RXCSR_P_SENTSTALL;
821 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300822 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300823 }
824
825 if (csr & MUSB_RXCSR_P_OVERRUN) {
826 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
827 csr &= ~MUSB_RXCSR_P_OVERRUN;
828 musb_writew(epio, MUSB_RXCSR, csr);
829
Bin Liub99d3652016-06-30 12:12:22 -0500830 musb_dbg(musb, "%s iso overrun on %p", musb_ep->name, request);
Sergei Shtylyov43467862010-09-24 13:44:12 +0300831 if (request->status == -EINPROGRESS)
Felipe Balbi550a7372008-07-24 12:27:36 +0300832 request->status = -EOVERFLOW;
833 }
834 if (csr & MUSB_RXCSR_INCOMPRX) {
835 /* REVISIT not necessarily an error */
Bin Liub99d3652016-06-30 12:12:22 -0500836 musb_dbg(musb, "%s, incomprx", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +0300837 }
838
839 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
840 /* "should not happen"; likely RXPKTRDY pending for DMA */
Bin Liub99d3652016-06-30 12:12:22 -0500841 musb_dbg(musb, "%s busy, csr %04x",
Felipe Balbi550a7372008-07-24 12:27:36 +0300842 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300843 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300844 }
845
846 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
847 csr &= ~(MUSB_RXCSR_AUTOCLEAR
848 | MUSB_RXCSR_DMAENAB
849 | MUSB_RXCSR_DMAMODE);
850 musb_writew(epio, MUSB_RXCSR,
851 MUSB_RXCSR_P_WZC_BITS | csr);
852
853 request->actual += musb_ep->dma->actual_len;
854
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100855#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
856 defined(CONFIG_USB_UX500_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +0300857 /* Autoclear doesn't clear RxPktRdy for short packets */
Ming Lei9001d802010-09-25 05:50:43 -0500858 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
Felipe Balbi550a7372008-07-24 12:27:36 +0300859 || (dma->actual_len
860 & (musb_ep->packet_sz - 1))) {
861 /* ack the read! */
862 csr &= ~MUSB_RXCSR_RXPKTRDY;
863 musb_writew(epio, MUSB_RXCSR, csr);
864 }
865
866 /* incomplete, and not short? wait for next IN packet */
867 if ((request->actual < request->length)
868 && (musb_ep->dma->actual_len
Ming Lei9001d802010-09-25 05:50:43 -0500869 == musb_ep->packet_sz)) {
870 /* In double buffer case, continue to unload fifo if
871 * there is Rx packet in FIFO.
872 **/
873 csr = musb_readw(epio, MUSB_RXCSR);
874 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
875 hw_ep->rx_double_buffered)
876 goto exit;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300877 return;
Ming Lei9001d802010-09-25 05:50:43 -0500878 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300879#endif
880 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530881 /*
882 * In the giveback function the MUSB lock is
883 * released and acquired after sometime. During
884 * this time period the INDEX register could get
885 * changed by the gadget_queue function especially
886 * on SMP systems. Reselect the INDEX to be sure
887 * we are reading/modifying the right registers
888 */
889 musb_ep_select(mbase, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300890
Felipe Balbiad1adb82011-02-16 12:40:05 +0200891 req = next_request(musb_ep);
892 if (!req)
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300893 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300894 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100895#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
896 defined(CONFIG_USB_UX500_DMA)
Ming Lei9001d802010-09-25 05:50:43 -0500897exit:
Ajay Kumar Guptabb324b02010-11-22 14:22:41 +0530898#endif
Sergei Shtylyov43467862010-09-24 13:44:12 +0300899 /* Analyze request */
Felipe Balbiad1adb82011-02-16 12:40:05 +0200900 rxstate(musb, req);
Felipe Balbi550a7372008-07-24 12:27:36 +0300901}
902
903/* ------------------------------------------------------------ */
904
905static int musb_gadget_enable(struct usb_ep *ep,
906 const struct usb_endpoint_descriptor *desc)
907{
908 unsigned long flags;
909 struct musb_ep *musb_ep;
910 struct musb_hw_ep *hw_ep;
911 void __iomem *regs;
912 struct musb *musb;
913 void __iomem *mbase;
914 u8 epnum;
915 u16 csr;
916 unsigned tmp;
917 int status = -EINVAL;
918
919 if (!ep || !desc)
920 return -EINVAL;
921
922 musb_ep = to_musb_ep(ep);
923 hw_ep = musb_ep->hw_ep;
924 regs = hw_ep->regs;
925 musb = musb_ep->musb;
926 mbase = musb->mregs;
927 epnum = musb_ep->current_epnum;
928
929 spin_lock_irqsave(&musb->lock, flags);
930
931 if (musb_ep->desc) {
932 status = -EBUSY;
933 goto fail;
934 }
Julia Lawall96bcd092009-01-24 17:57:24 -0800935 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +0300936
937 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -0800938 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300939 goto fail;
940
941 /* REVISIT this rules out high bandwidth periodic transfers */
Felipe Balbi6ddcabc2016-09-28 13:40:40 +0300942 tmp = usb_endpoint_maxp_mult(desc) - 1;
943 if (tmp) {
Ming Leif11d8932010-09-24 13:44:04 +0300944 int ok;
945
946 if (usb_endpoint_dir_in(desc))
947 ok = musb->hb_iso_tx;
948 else
949 ok = musb->hb_iso_rx;
950
951 if (!ok) {
Bin Liub99d3652016-06-30 12:12:22 -0500952 musb_dbg(musb, "no support for high bandwidth ISO");
Ming Leif11d8932010-09-24 13:44:04 +0300953 goto fail;
954 }
Felipe Balbi6ddcabc2016-09-28 13:40:40 +0300955 musb_ep->hb_mult = tmp;
Ming Leif11d8932010-09-24 13:44:04 +0300956 } else {
957 musb_ep->hb_mult = 0;
958 }
959
Felipe Balbi6ddcabc2016-09-28 13:40:40 +0300960 musb_ep->packet_sz = usb_endpoint_maxp(desc);
Ming Leif11d8932010-09-24 13:44:04 +0300961 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300962
963 /* enable the interrupts for the endpoint, set the endpoint
964 * packet size (or fail), set the mode, clear the fifo
965 */
966 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -0800967 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300968
969 if (hw_ep->is_shared_fifo)
970 musb_ep->is_in = 1;
971 if (!musb_ep->is_in)
972 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +0300973
974 if (tmp > hw_ep->max_packet_sz_tx) {
Bin Liub99d3652016-06-30 12:12:22 -0500975 musb_dbg(musb, "packet size beyond hardware FIFO size");
Felipe Balbi550a7372008-07-24 12:27:36 +0300976 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +0300977 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300978
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100979 musb->intrtxe |= (1 << epnum);
980 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +0300981
982 /* REVISIT if can_bulk_split(), use by updating "tmp";
983 * likewise high bandwidth periodic tx
984 */
Cliff Cai9f445cb2010-01-28 20:44:18 -0500985 /* Set TXMAXP with the FIFO size of the endpoint
Ming Lei31c99092010-10-19 19:08:25 -0500986 * to disable double buffering mode.
Cliff Cai9f445cb2010-01-28 20:44:18 -0500987 */
Arnd Bergmanna9762b72018-03-09 17:37:54 +0100988 if (can_bulk_split(musb, musb_ep->type))
989 musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
990 musb_ep->packet_sz) - 1;
991 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
992 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +0300993
994 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
995 if (musb_readw(regs, MUSB_TXCSR)
996 & MUSB_TXCSR_FIFONOTEMPTY)
997 csr |= MUSB_TXCSR_FLUSHFIFO;
998 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
999 csr |= MUSB_TXCSR_P_ISO;
1000
1001 /* set twice in case of double buffering */
1002 musb_writew(regs, MUSB_TXCSR, csr);
1003 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1004 musb_writew(regs, MUSB_TXCSR, csr);
1005
1006 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +03001007
1008 if (hw_ep->is_shared_fifo)
1009 musb_ep->is_in = 0;
1010 if (musb_ep->is_in)
1011 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001012
1013 if (tmp > hw_ep->max_packet_sz_rx) {
Bin Liub99d3652016-06-30 12:12:22 -05001014 musb_dbg(musb, "packet size beyond hardware FIFO size");
Felipe Balbi550a7372008-07-24 12:27:36 +03001015 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001016 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001017
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001018 musb->intrrxe |= (1 << epnum);
1019 musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001020
1021 /* REVISIT if can_bulk_combine() use by updating "tmp"
1022 * likewise high bandwidth periodic rx
1023 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001024 /* Set RXMAXP with the FIFO size of the endpoint
1025 * to disable double buffering mode.
1026 */
Arnd Bergmanna9762b72018-03-09 17:37:54 +01001027 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1028 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001029
1030 /* force shared fifo to OUT-only mode */
1031 if (hw_ep->is_shared_fifo) {
1032 csr = musb_readw(regs, MUSB_TXCSR);
1033 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1034 musb_writew(regs, MUSB_TXCSR, csr);
1035 }
1036
1037 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1038 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1039 csr |= MUSB_RXCSR_P_ISO;
1040 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1041 csr |= MUSB_RXCSR_DISNYET;
1042
1043 /* set twice in case of double buffering */
1044 musb_writew(regs, MUSB_RXCSR, csr);
1045 musb_writew(regs, MUSB_RXCSR, csr);
1046 }
1047
1048 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1049 * for some reason you run out of channels here.
1050 */
1051 if (is_dma_capable() && musb->dma_controller) {
1052 struct dma_controller *c = musb->dma_controller;
1053
1054 musb_ep->dma = c->channel_alloc(c, hw_ep,
1055 (desc->bEndpointAddress & USB_DIR_IN));
1056 } else
1057 musb_ep->dma = NULL;
1058
1059 musb_ep->desc = desc;
1060 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001061 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001062 status = 0;
1063
1064 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1065 musb_driver_name, musb_ep->end_point.name,
Bin Liu0ccbada2017-08-24 11:38:32 -05001066 musb_ep_xfertype_string(musb_ep->type),
Felipe Balbi550a7372008-07-24 12:27:36 +03001067 musb_ep->is_in ? "IN" : "OUT",
1068 musb_ep->dma ? "dma, " : "",
1069 musb_ep->packet_sz);
1070
Tony Lindgren2bff3912016-11-16 13:21:24 -06001071 schedule_delayed_work(&musb->irq_work, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001072
1073fail:
1074 spin_unlock_irqrestore(&musb->lock, flags);
1075 return status;
1076}
1077
1078/*
1079 * Disable an endpoint flushing all requests queued.
1080 */
1081static int musb_gadget_disable(struct usb_ep *ep)
1082{
1083 unsigned long flags;
1084 struct musb *musb;
1085 u8 epnum;
1086 struct musb_ep *musb_ep;
1087 void __iomem *epio;
Felipe Balbi550a7372008-07-24 12:27:36 +03001088
1089 musb_ep = to_musb_ep(ep);
1090 musb = musb_ep->musb;
1091 epnum = musb_ep->current_epnum;
1092 epio = musb->endpoints[epnum].regs;
1093
1094 spin_lock_irqsave(&musb->lock, flags);
1095 musb_ep_select(musb->mregs, epnum);
1096
1097 /* zero the endpoint sizes */
1098 if (musb_ep->is_in) {
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001099 musb->intrtxe &= ~(1 << epnum);
1100 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001101 musb_writew(epio, MUSB_TXMAXP, 0);
1102 } else {
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001103 musb->intrrxe &= ~(1 << epnum);
1104 musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001105 musb_writew(epio, MUSB_RXMAXP, 0);
1106 }
1107
Felipe Balbi550a7372008-07-24 12:27:36 +03001108 /* abort all pending DMA and requests */
1109 nuke(musb_ep, -ESHUTDOWN);
1110
Tal Shorer607fb0f2016-04-25 15:53:29 -05001111 musb_ep->desc = NULL;
1112 musb_ep->end_point.desc = NULL;
1113
Tony Lindgren2bff3912016-11-16 13:21:24 -06001114 schedule_delayed_work(&musb->irq_work, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001115
1116 spin_unlock_irqrestore(&(musb->lock), flags);
1117
Bin Liub99d3652016-06-30 12:12:22 -05001118 musb_dbg(musb, "%s", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001119
Saurav Girepunje29e56c02019-10-02 20:02:45 +05301120 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001121}
1122
1123/*
1124 * Allocate a request for an endpoint.
1125 * Reused by ep0 code.
1126 */
1127struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1128{
1129 struct musb_ep *musb_ep = to_musb_ep(ep);
1130 struct musb_request *request = NULL;
1131
1132 request = kzalloc(sizeof *request, gfp_flags);
Bin Liub99d3652016-06-30 12:12:22 -05001133 if (!request)
Felipe Balbi0607f862010-12-01 11:03:54 +02001134 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001135
Felipe Balbi0607f862010-12-01 11:03:54 +02001136 request->request.dma = DMA_ADDR_INVALID;
1137 request->epnum = musb_ep->current_epnum;
1138 request->ep = musb_ep;
1139
Bin Liufc780032016-06-30 12:12:27 -05001140 trace_musb_req_alloc(request);
Felipe Balbi550a7372008-07-24 12:27:36 +03001141 return &request->request;
1142}
1143
1144/*
1145 * Free a request
1146 * Reused by ep0 code.
1147 */
1148void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1149{
Bin Liufc780032016-06-30 12:12:27 -05001150 struct musb_request *request = to_musb_request(req);
1151
1152 trace_musb_req_free(request);
1153 kfree(request);
Felipe Balbi550a7372008-07-24 12:27:36 +03001154}
1155
1156static LIST_HEAD(buffers);
1157
1158struct free_record {
1159 struct list_head list;
1160 struct device *dev;
1161 unsigned bytes;
1162 dma_addr_t dma;
1163};
1164
1165/*
1166 * Context: controller locked, IRQs blocked.
1167 */
Sergei Shtylyova666e3e2010-09-11 13:23:12 -05001168void musb_ep_restart(struct musb *musb, struct musb_request *req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001169{
Bin Liufc780032016-06-30 12:12:27 -05001170 trace_musb_req_start(req);
Felipe Balbi550a7372008-07-24 12:27:36 +03001171 musb_ep_select(musb->mregs, req->epnum);
1172 if (req->tx)
1173 txstate(musb, req);
1174 else
1175 rxstate(musb, req);
1176}
1177
Tony Lindgrenea2f35c2016-11-16 13:21:23 -06001178static int musb_ep_restart_resume_work(struct musb *musb, void *data)
1179{
1180 struct musb_request *req = data;
1181
1182 musb_ep_restart(musb, req);
1183
1184 return 0;
1185}
1186
Felipe Balbi550a7372008-07-24 12:27:36 +03001187static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1188 gfp_t gfp_flags)
1189{
1190 struct musb_ep *musb_ep;
1191 struct musb_request *request;
1192 struct musb *musb;
Tony Lindgrenea2f35c2016-11-16 13:21:23 -06001193 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001194 unsigned long lockflags;
1195
1196 if (!ep || !req)
1197 return -EINVAL;
1198 if (!req->buf)
1199 return -ENODATA;
1200
1201 musb_ep = to_musb_ep(ep);
1202 musb = musb_ep->musb;
1203
1204 request = to_musb_request(req);
1205 request->musb = musb;
1206
1207 if (request->ep != musb_ep)
1208 return -EINVAL;
1209
Tony Lindgrenea2f35c2016-11-16 13:21:23 -06001210 status = pm_runtime_get(musb->controller);
1211 if ((status != -EINPROGRESS) && status < 0) {
1212 dev_err(musb->controller,
1213 "pm runtime get failed in %s\n",
1214 __func__);
1215 pm_runtime_put_noidle(musb->controller);
1216
1217 return status;
1218 }
1219 status = 0;
1220
Bin Liufc780032016-06-30 12:12:27 -05001221 trace_musb_req_enq(request);
Felipe Balbi550a7372008-07-24 12:27:36 +03001222
1223 /* request is mine now... */
1224 request->request.actual = 0;
1225 request->request.status = -EINPROGRESS;
1226 request->epnum = musb_ep->current_epnum;
1227 request->tx = musb_ep->is_in;
1228
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +01001229 map_dma_buffer(request, musb, musb_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001230
1231 spin_lock_irqsave(&musb->lock, lockflags);
1232
1233 /* don't queue if the ep is down */
1234 if (!musb_ep->desc) {
Bin Liub99d3652016-06-30 12:12:22 -05001235 musb_dbg(musb, "req %p queued to %s while ep %s",
Felipe Balbi550a7372008-07-24 12:27:36 +03001236 req, ep->name, "disabled");
1237 status = -ESHUTDOWN;
Sebastian Andrzej Siewior23a53d92013-06-19 17:38:15 +02001238 unmap_dma_buffer(request, musb);
1239 goto unlock;
Felipe Balbi550a7372008-07-24 12:27:36 +03001240 }
1241
1242 /* add request to the list */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001243 list_add_tail(&request->list, &musb_ep->req_list);
Felipe Balbi550a7372008-07-24 12:27:36 +03001244
1245 /* it this is the head of the queue, start i/o ... */
Tony Lindgrenea2f35c2016-11-16 13:21:23 -06001246 if (!musb_ep->busy && &request->list == musb_ep->req_list.next) {
1247 status = musb_queue_resume_work(musb,
1248 musb_ep_restart_resume_work,
1249 request);
1250 if (status < 0)
1251 dev_err(musb->controller, "%s resume work: %i\n",
1252 __func__, status);
1253 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001254
Sebastian Andrzej Siewior23a53d92013-06-19 17:38:15 +02001255unlock:
Felipe Balbi550a7372008-07-24 12:27:36 +03001256 spin_unlock_irqrestore(&musb->lock, lockflags);
Tony Lindgrencacaaf82016-10-19 12:03:40 -05001257 pm_runtime_mark_last_busy(musb->controller);
1258 pm_runtime_put_autosuspend(musb->controller);
1259
Felipe Balbi550a7372008-07-24 12:27:36 +03001260 return status;
1261}
1262
1263static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1264{
1265 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001266 struct musb_request *req = to_musb_request(request);
1267 struct musb_request *r;
Felipe Balbi550a7372008-07-24 12:27:36 +03001268 unsigned long flags;
1269 int status = 0;
1270 struct musb *musb = musb_ep->musb;
1271
Bin Liufc780032016-06-30 12:12:27 -05001272 if (!ep || !request || req->ep != musb_ep)
Felipe Balbi550a7372008-07-24 12:27:36 +03001273 return -EINVAL;
1274
Bin Liufc780032016-06-30 12:12:27 -05001275 trace_musb_req_deq(req);
1276
Felipe Balbi550a7372008-07-24 12:27:36 +03001277 spin_lock_irqsave(&musb->lock, flags);
1278
1279 list_for_each_entry(r, &musb_ep->req_list, list) {
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001280 if (r == req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001281 break;
1282 }
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001283 if (r != req) {
Bin Liub99d3652016-06-30 12:12:22 -05001284 dev_err(musb->controller, "request %p not queued to %s\n",
1285 request, ep->name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001286 status = -EINVAL;
1287 goto done;
1288 }
1289
1290 /* if the hardware doesn't have the request, easy ... */
Felipe Balbi3d5ad132011-03-22 11:38:49 +02001291 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
Felipe Balbi550a7372008-07-24 12:27:36 +03001292 musb_g_giveback(musb_ep, request, -ECONNRESET);
1293
1294 /* ... else abort the dma transfer ... */
1295 else if (is_dma_capable() && musb_ep->dma) {
1296 struct dma_controller *c = musb->dma_controller;
1297
1298 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1299 if (c->channel_abort)
1300 status = c->channel_abort(musb_ep->dma);
1301 else
1302 status = -EBUSY;
1303 if (status == 0)
1304 musb_g_giveback(musb_ep, request, -ECONNRESET);
1305 } else {
1306 /* NOTE: by sticking to easily tested hardware/driver states,
1307 * we leave counting of in-flight packets imprecise.
1308 */
1309 musb_g_giveback(musb_ep, request, -ECONNRESET);
1310 }
1311
1312done:
1313 spin_unlock_irqrestore(&musb->lock, flags);
1314 return status;
1315}
1316
1317/*
Geert Uytterhoevenc1aa81d2019-10-24 17:28:33 +02001318 * Set or clear the halt bit of an endpoint. A halted endpoint won't tx/rx any
Felipe Balbi550a7372008-07-24 12:27:36 +03001319 * data but will queue requests.
1320 *
1321 * exported to ep0 code
1322 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001323static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001324{
1325 struct musb_ep *musb_ep = to_musb_ep(ep);
1326 u8 epnum = musb_ep->current_epnum;
1327 struct musb *musb = musb_ep->musb;
1328 void __iomem *epio = musb->endpoints[epnum].regs;
1329 void __iomem *mbase;
1330 unsigned long flags;
1331 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001332 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001333 int status = 0;
1334
1335 if (!ep)
1336 return -EINVAL;
1337 mbase = musb->mregs;
1338
1339 spin_lock_irqsave(&musb->lock, flags);
1340
1341 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1342 status = -EINVAL;
1343 goto done;
1344 }
1345
1346 musb_ep_select(mbase, epnum);
1347
Felipe Balbiad1adb82011-02-16 12:40:05 +02001348 request = next_request(musb_ep);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001349 if (value) {
1350 if (request) {
Bin Liub99d3652016-06-30 12:12:22 -05001351 musb_dbg(musb, "request in progress, cannot halt %s",
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001352 ep->name);
1353 status = -EAGAIN;
1354 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001355 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001356 /* Cannot portably stall with non-empty FIFO */
1357 if (musb_ep->is_in) {
1358 csr = musb_readw(epio, MUSB_TXCSR);
1359 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Bin Liub99d3652016-06-30 12:12:22 -05001360 musb_dbg(musb, "FIFO busy, cannot halt %s",
1361 ep->name);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001362 status = -EAGAIN;
1363 goto done;
1364 }
1365 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001366 } else
1367 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001368
1369 /* set/clear the stall and toggle bits */
Bin Liub99d3652016-06-30 12:12:22 -05001370 musb_dbg(musb, "%s: %s stall", ep->name, value ? "set" : "clear");
Felipe Balbi550a7372008-07-24 12:27:36 +03001371 if (musb_ep->is_in) {
1372 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001373 csr |= MUSB_TXCSR_P_WZC_BITS
1374 | MUSB_TXCSR_CLRDATATOG;
1375 if (value)
1376 csr |= MUSB_TXCSR_P_SENDSTALL;
1377 else
1378 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1379 | MUSB_TXCSR_P_SENTSTALL);
1380 csr &= ~MUSB_TXCSR_TXPKTRDY;
1381 musb_writew(epio, MUSB_TXCSR, csr);
1382 } else {
1383 csr = musb_readw(epio, MUSB_RXCSR);
1384 csr |= MUSB_RXCSR_P_WZC_BITS
1385 | MUSB_RXCSR_FLUSHFIFO
1386 | MUSB_RXCSR_CLRDATATOG;
1387 if (value)
1388 csr |= MUSB_RXCSR_P_SENDSTALL;
1389 else
1390 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1391 | MUSB_RXCSR_P_SENTSTALL);
1392 musb_writew(epio, MUSB_RXCSR, csr);
1393 }
1394
Felipe Balbi550a7372008-07-24 12:27:36 +03001395 /* maybe start the first request in the queue */
1396 if (!musb_ep->busy && !value && request) {
Bin Liub99d3652016-06-30 12:12:22 -05001397 musb_dbg(musb, "restarting the request");
Felipe Balbi550a7372008-07-24 12:27:36 +03001398 musb_ep_restart(musb, request);
1399 }
1400
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001401done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001402 spin_unlock_irqrestore(&musb->lock, flags);
1403 return status;
1404}
1405
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001406/*
1407 * Sets the halt feature with the clear requests ignored
1408 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001409static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001410{
1411 struct musb_ep *musb_ep = to_musb_ep(ep);
1412
1413 if (!ep)
1414 return -EINVAL;
1415
1416 musb_ep->wedged = 1;
1417
1418 return usb_ep_set_halt(ep);
1419}
1420
Felipe Balbi550a7372008-07-24 12:27:36 +03001421static int musb_gadget_fifo_status(struct usb_ep *ep)
1422{
1423 struct musb_ep *musb_ep = to_musb_ep(ep);
1424 void __iomem *epio = musb_ep->hw_ep->regs;
1425 int retval = -EINVAL;
1426
1427 if (musb_ep->desc && !musb_ep->is_in) {
1428 struct musb *musb = musb_ep->musb;
1429 int epnum = musb_ep->current_epnum;
1430 void __iomem *mbase = musb->mregs;
1431 unsigned long flags;
1432
1433 spin_lock_irqsave(&musb->lock, flags);
1434
1435 musb_ep_select(mbase, epnum);
1436 /* FIXME return zero unless RXPKTRDY is set */
1437 retval = musb_readw(epio, MUSB_RXCOUNT);
1438
1439 spin_unlock_irqrestore(&musb->lock, flags);
1440 }
1441 return retval;
1442}
1443
1444static void musb_gadget_fifo_flush(struct usb_ep *ep)
1445{
1446 struct musb_ep *musb_ep = to_musb_ep(ep);
1447 struct musb *musb = musb_ep->musb;
1448 u8 epnum = musb_ep->current_epnum;
1449 void __iomem *epio = musb->endpoints[epnum].regs;
1450 void __iomem *mbase;
1451 unsigned long flags;
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001452 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03001453
1454 mbase = musb->mregs;
1455
1456 spin_lock_irqsave(&musb->lock, flags);
1457 musb_ep_select(mbase, (u8) epnum);
1458
1459 /* disable interrupts */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001460 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
Felipe Balbi550a7372008-07-24 12:27:36 +03001461
1462 if (musb_ep->is_in) {
1463 csr = musb_readw(epio, MUSB_TXCSR);
1464 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1465 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
Yauheni Kaliuta4858f062011-06-08 17:12:02 +03001466 /*
1467 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1468 * to interrupt current FIFO loading, but not flushing
1469 * the already loaded ones.
1470 */
1471 csr &= ~MUSB_TXCSR_TXPKTRDY;
Felipe Balbi550a7372008-07-24 12:27:36 +03001472 musb_writew(epio, MUSB_TXCSR, csr);
1473 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1474 musb_writew(epio, MUSB_TXCSR, csr);
1475 }
1476 } else {
1477 csr = musb_readw(epio, MUSB_RXCSR);
1478 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1479 musb_writew(epio, MUSB_RXCSR, csr);
1480 musb_writew(epio, MUSB_RXCSR, csr);
1481 }
1482
1483 /* re-enable interrupt */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001484 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001485 spin_unlock_irqrestore(&musb->lock, flags);
1486}
1487
1488static const struct usb_ep_ops musb_ep_ops = {
1489 .enable = musb_gadget_enable,
1490 .disable = musb_gadget_disable,
1491 .alloc_request = musb_alloc_request,
1492 .free_request = musb_free_request,
1493 .queue = musb_gadget_queue,
1494 .dequeue = musb_gadget_dequeue,
1495 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001496 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001497 .fifo_status = musb_gadget_fifo_status,
1498 .fifo_flush = musb_gadget_fifo_flush
1499};
1500
1501/* ----------------------------------------------------------------------- */
1502
1503static int musb_gadget_get_frame(struct usb_gadget *gadget)
1504{
1505 struct musb *musb = gadget_to_musb(gadget);
1506
1507 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1508}
1509
1510static int musb_gadget_wakeup(struct usb_gadget *gadget)
1511{
1512 struct musb *musb = gadget_to_musb(gadget);
1513 void __iomem *mregs = musb->mregs;
1514 unsigned long flags;
1515 int status = -EINVAL;
1516 u8 power, devctl;
1517 int retries;
1518
1519 spin_lock_irqsave(&musb->lock, flags);
1520
Antoine Tenarte47d9252014-10-30 18:41:13 +01001521 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001522 case OTG_STATE_B_PERIPHERAL:
1523 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1524 * that's part of the standard usb 1.1 state machine, and
1525 * doesn't affect OTG transitions.
1526 */
1527 if (musb->may_wakeup && musb->is_suspended)
1528 break;
1529 goto done;
1530 case OTG_STATE_B_IDLE:
1531 /* Start SRP ... OTG not required. */
1532 devctl = musb_readb(mregs, MUSB_DEVCTL);
Bin Liub99d3652016-06-30 12:12:22 -05001533 musb_dbg(musb, "Sending SRP: devctl: %02x", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001534 devctl |= MUSB_DEVCTL_SESSION;
1535 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1536 devctl = musb_readb(mregs, MUSB_DEVCTL);
1537 retries = 100;
1538 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1539 devctl = musb_readb(mregs, MUSB_DEVCTL);
1540 if (retries-- < 1)
1541 break;
1542 }
1543 retries = 10000;
1544 while (devctl & MUSB_DEVCTL_SESSION) {
1545 devctl = musb_readb(mregs, MUSB_DEVCTL);
1546 if (retries-- < 1)
1547 break;
1548 }
1549
Hema HK86205432011-03-22 16:54:22 +05301550 spin_unlock_irqrestore(&musb->lock, flags);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001551 otg_start_srp(musb->xceiv->otg);
Hema HK86205432011-03-22 16:54:22 +05301552 spin_lock_irqsave(&musb->lock, flags);
1553
Felipe Balbi550a7372008-07-24 12:27:36 +03001554 /* Block idling for at least 1s */
1555 musb_platform_try_idle(musb,
1556 jiffies + msecs_to_jiffies(1 * HZ));
1557
1558 status = 0;
1559 goto done;
1560 default:
Bin Liub99d3652016-06-30 12:12:22 -05001561 musb_dbg(musb, "Unhandled wake: %s",
Antoine Tenarte47d9252014-10-30 18:41:13 +01001562 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001563 goto done;
1564 }
1565
1566 status = 0;
1567
1568 power = musb_readb(mregs, MUSB_POWER);
1569 power |= MUSB_POWER_RESUME;
1570 musb_writeb(mregs, MUSB_POWER, power);
Bin Liub99d3652016-06-30 12:12:22 -05001571 musb_dbg(musb, "issue wakeup");
Felipe Balbi550a7372008-07-24 12:27:36 +03001572
1573 /* FIXME do this next chunk in a timer callback, no udelay */
1574 mdelay(2);
1575
1576 power = musb_readb(mregs, MUSB_POWER);
1577 power &= ~MUSB_POWER_RESUME;
1578 musb_writeb(mregs, MUSB_POWER, power);
1579done:
1580 spin_unlock_irqrestore(&musb->lock, flags);
1581 return status;
1582}
1583
1584static int
1585musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1586{
Peter Chendadac982015-01-28 16:32:41 +08001587 gadget->is_selfpowered = !!is_selfpowered;
Felipe Balbi550a7372008-07-24 12:27:36 +03001588 return 0;
1589}
1590
1591static void musb_pullup(struct musb *musb, int is_on)
1592{
1593 u8 power;
1594
1595 power = musb_readb(musb->mregs, MUSB_POWER);
1596 if (is_on)
1597 power |= MUSB_POWER_SOFTCONN;
1598 else
1599 power &= ~MUSB_POWER_SOFTCONN;
1600
1601 /* FIXME if on, HdrcStart; if off, HdrcStop */
1602
Bin Liub99d3652016-06-30 12:12:22 -05001603 musb_dbg(musb, "gadget D+ pullup %s",
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001604 is_on ? "on" : "off");
Felipe Balbi550a7372008-07-24 12:27:36 +03001605 musb_writeb(musb->mregs, MUSB_POWER, power);
1606}
1607
1608#if 0
1609static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1610{
Bin Liub99d3652016-06-30 12:12:22 -05001611 musb_dbg(musb, "<= %s =>\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001612
1613 /*
1614 * FIXME iff driver's softconnect flag is set (as it is during probe,
1615 * though that can clear it), just musb_pullup().
1616 */
1617
1618 return -EINVAL;
1619}
1620#endif
1621
1622static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1623{
1624 struct musb *musb = gadget_to_musb(gadget);
1625
David Brownell84e250f2009-03-31 12:30:04 -07001626 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001627 return -EOPNOTSUPP;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001628 return usb_phy_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001629}
1630
Tony Lindgren517baff2016-05-31 10:05:14 -05001631static void musb_gadget_work(struct work_struct *work)
1632{
1633 struct musb *musb;
1634 unsigned long flags;
1635
1636 musb = container_of(work, struct musb, gadget_work.work);
1637 pm_runtime_get_sync(musb->controller);
1638 spin_lock_irqsave(&musb->lock, flags);
1639 musb_pullup(musb, musb->softconnect);
1640 spin_unlock_irqrestore(&musb->lock, flags);
1641 pm_runtime_mark_last_busy(musb->controller);
1642 pm_runtime_put_autosuspend(musb->controller);
1643}
1644
Felipe Balbi550a7372008-07-24 12:27:36 +03001645static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1646{
1647 struct musb *musb = gadget_to_musb(gadget);
1648 unsigned long flags;
1649
1650 is_on = !!is_on;
1651
1652 /* NOTE: this assumes we are sensing vbus; we'd rather
1653 * not pullup unless the B-session is active.
1654 */
1655 spin_lock_irqsave(&musb->lock, flags);
1656 if (is_on != musb->softconnect) {
1657 musb->softconnect = is_on;
Tony Lindgren517baff2016-05-31 10:05:14 -05001658 schedule_delayed_work(&musb->gadget_work, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001659 }
1660 spin_unlock_irqrestore(&musb->lock, flags);
John Stultz93e098a82011-07-20 17:09:34 -07001661
Felipe Balbi550a7372008-07-24 12:27:36 +03001662 return 0;
1663}
1664
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001665static int musb_gadget_start(struct usb_gadget *g,
1666 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001667static int musb_gadget_stop(struct usb_gadget *g);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001668
Felipe Balbi550a7372008-07-24 12:27:36 +03001669static const struct usb_gadget_ops musb_gadget_operations = {
1670 .get_frame = musb_gadget_get_frame,
1671 .wakeup = musb_gadget_wakeup,
1672 .set_selfpowered = musb_gadget_set_self_powered,
1673 /* .vbus_session = musb_gadget_vbus_session, */
1674 .vbus_draw = musb_gadget_vbus_draw,
1675 .pullup = musb_gadget_pullup,
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001676 .udc_start = musb_gadget_start,
1677 .udc_stop = musb_gadget_stop,
Felipe Balbi550a7372008-07-24 12:27:36 +03001678};
1679
1680/* ----------------------------------------------------------------------- */
1681
1682/* Registration */
1683
1684/* Only this registration code "knows" the rule (from USB standards)
1685 * about there being only one external upstream port. It assumes
1686 * all peripheral ports are external...
1687 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001688
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001689static void
Felipe Balbi550a7372008-07-24 12:27:36 +03001690init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1691{
1692 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1693
1694 memset(ep, 0, sizeof *ep);
1695
1696 ep->current_epnum = epnum;
1697 ep->musb = musb;
1698 ep->hw_ep = hw_ep;
1699 ep->is_in = is_in;
1700
1701 INIT_LIST_HEAD(&ep->req_list);
1702
1703 sprintf(ep->name, "ep%d%s", epnum,
1704 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1705 is_in ? "in" : "out"));
1706 ep->end_point.name = ep->name;
1707 INIT_LIST_HEAD(&ep->end_point.ep_list);
1708 if (!epnum) {
Robert Baldygae117e742013-12-13 12:23:38 +01001709 usb_ep_set_maxpacket_limit(&ep->end_point, 64);
Robert Baldyga85019552015-07-31 16:00:46 +02001710 ep->end_point.caps.type_control = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001711 ep->end_point.ops = &musb_g_ep0_ops;
1712 musb->g.ep0 = &ep->end_point;
1713 } else {
1714 if (is_in)
Robert Baldygae117e742013-12-13 12:23:38 +01001715 usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_tx);
Felipe Balbi550a7372008-07-24 12:27:36 +03001716 else
Robert Baldygae117e742013-12-13 12:23:38 +01001717 usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_rx);
Robert Baldyga85019552015-07-31 16:00:46 +02001718 ep->end_point.caps.type_iso = true;
1719 ep->end_point.caps.type_bulk = true;
1720 ep->end_point.caps.type_int = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001721 ep->end_point.ops = &musb_ep_ops;
1722 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1723 }
Robert Baldyga85019552015-07-31 16:00:46 +02001724
1725 if (!epnum || hw_ep->is_shared_fifo) {
1726 ep->end_point.caps.dir_in = true;
1727 ep->end_point.caps.dir_out = true;
1728 } else if (is_in)
1729 ep->end_point.caps.dir_in = true;
1730 else
1731 ep->end_point.caps.dir_out = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001732}
1733
1734/*
1735 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1736 * to the rest of the driver state.
1737 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001738static inline void musb_g_init_endpoints(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001739{
1740 u8 epnum;
1741 struct musb_hw_ep *hw_ep;
1742 unsigned count = 0;
1743
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001744 /* initialize endpoint list just once */
Felipe Balbi550a7372008-07-24 12:27:36 +03001745 INIT_LIST_HEAD(&(musb->g.ep_list));
1746
1747 for (epnum = 0, hw_ep = musb->endpoints;
1748 epnum < musb->nr_endpoints;
1749 epnum++, hw_ep++) {
1750 if (hw_ep->is_shared_fifo /* || !epnum */) {
1751 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1752 count++;
1753 } else {
1754 if (hw_ep->max_packet_sz_tx) {
1755 init_peripheral_ep(musb, &hw_ep->ep_in,
1756 epnum, 1);
1757 count++;
1758 }
1759 if (hw_ep->max_packet_sz_rx) {
1760 init_peripheral_ep(musb, &hw_ep->ep_out,
1761 epnum, 0);
1762 count++;
1763 }
1764 }
1765 }
1766}
1767
1768/* called once during driver setup to initialize and link into
1769 * the driver model; memory is zeroed.
1770 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001771int musb_gadget_setup(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001772{
1773 int status;
1774
1775 /* REVISIT minor race: if (erroneously) setting up two
1776 * musb peripherals at the same time, only the bus lock
1777 * is probably held.
1778 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001779
1780 musb->g.ops = &musb_gadget_operations;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001781 musb->g.max_speed = USB_SPEED_HIGH;
Felipe Balbi550a7372008-07-24 12:27:36 +03001782 musb->g.speed = USB_SPEED_UNKNOWN;
1783
Bin Liu1374a4302013-09-17 12:43:13 -05001784 MUSB_DEV_MODE(musb);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001785 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Bin Liu1374a4302013-09-17 12:43:13 -05001786
Felipe Balbi550a7372008-07-24 12:27:36 +03001787 /* this "gadget" abstracts/virtualizes the controller */
Felipe Balbi550a7372008-07-24 12:27:36 +03001788 musb->g.name = musb_driver_name;
Bin Liu0a9134b2018-05-21 08:42:19 -05001789 /* don't support otg protocols */
Apelete Seketelifd3923a2013-11-19 23:18:20 +01001790 musb->g.is_otg = 0;
Tony Lindgren517baff2016-05-31 10:05:14 -05001791 INIT_DELAYED_WORK(&musb->gadget_work, musb_gadget_work);
Felipe Balbi550a7372008-07-24 12:27:36 +03001792 musb_g_init_endpoints(musb);
1793
1794 musb->is_active = 0;
1795 musb_platform_try_idle(musb, 0);
1796
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001797 status = usb_add_gadget_udc(musb->controller, &musb->g);
1798 if (status)
1799 goto err;
1800
1801 return 0;
1802err:
Sebastian Andrzej Siewior6193d692011-08-10 11:01:57 +02001803 musb->g.dev.parent = NULL;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001804 device_unregister(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001805 return status;
1806}
1807
1808void musb_gadget_cleanup(struct musb *musb)
1809{
Bin Liu7ad76952018-05-21 08:42:15 -05001810 if (musb->port_mode == MUSB_HOST)
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02001811 return;
Tony Lindgren517baff2016-05-31 10:05:14 -05001812
1813 cancel_delayed_work_sync(&musb->gadget_work);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001814 usb_del_gadget_udc(&musb->g);
Felipe Balbi550a7372008-07-24 12:27:36 +03001815}
1816
1817/*
1818 * Register the gadget driver. Used by gadget drivers when
1819 * registering themselves with the controller.
1820 *
1821 * -EINVAL something went wrong (not driver)
1822 * -EBUSY another gadget is already using the controller
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001823 * -ENOMEM no memory to perform the operation
Felipe Balbi550a7372008-07-24 12:27:36 +03001824 *
1825 * @param driver the gadget driver
1826 * @return <0 if error, 0 if everything is fine
1827 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001828static int musb_gadget_start(struct usb_gadget *g,
1829 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03001830{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001831 struct musb *musb = gadget_to_musb(g);
Heikki Krogerusd445b6d2012-02-13 13:24:15 +02001832 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001833 unsigned long flags;
Felipe Balbi032ec492011-11-24 15:46:26 +02001834 int retval = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001835
Felipe Balbi032ec492011-11-24 15:46:26 +02001836 if (driver->max_speed < USB_SPEED_HIGH) {
1837 retval = -EINVAL;
1838 goto err;
1839 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001840
Hema HK7acc6192011-02-28 14:19:34 +05301841 pm_runtime_get_sync(musb->controller);
1842
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001843 musb->softconnect = 0;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001844 musb->gadget_driver = driver;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001845
1846 spin_lock_irqsave(&musb->lock, flags);
Greg Kroah-Hartman43e699c2013-10-14 13:06:15 -07001847 musb->is_active = 1;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001848
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001849 otg_set_peripheral(otg, &musb->g);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001850 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001851 spin_unlock_irqrestore(&musb->lock, flags);
1852
Sebastian Andrzej Siewior001dd842013-10-11 10:38:13 +02001853 musb_start(musb);
1854
Felipe Balbi032ec492011-11-24 15:46:26 +02001855 /* REVISIT: funcall to other code, which also
1856 * handles power budgeting ... this way also
1857 * ensures HdrcStart is indirectly called.
1858 */
Grazvydas Ignotasb65ae0f2013-03-24 17:36:55 +02001859 if (musb->xceiv->last_event == USB_EVENT_ID)
1860 musb_platform_set_vbus(musb, 1);
Felipe Balbi032ec492011-11-24 15:46:26 +02001861
Tony Lindgren30647212016-05-31 10:05:13 -05001862 pm_runtime_mark_last_busy(musb->controller);
1863 pm_runtime_put_autosuspend(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001864
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001865 return 0;
1866
Felipe Balbi032ec492011-11-24 15:46:26 +02001867err:
Felipe Balbi550a7372008-07-24 12:27:36 +03001868 return retval;
1869}
Felipe Balbi550a7372008-07-24 12:27:36 +03001870
Felipe Balbi550a7372008-07-24 12:27:36 +03001871/*
1872 * Unregister the gadget driver. Used by gadget drivers when
1873 * unregistering themselves from the controller.
1874 *
1875 * @param driver the gadget driver to unregister
1876 */
Felipe Balbi22835b82014-10-17 12:05:12 -05001877static int musb_gadget_stop(struct usb_gadget *g)
Felipe Balbi550a7372008-07-24 12:27:36 +03001878{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001879 struct musb *musb = gadget_to_musb(g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001880 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03001881
Tony Lindgren30647212016-05-31 10:05:13 -05001882 pm_runtime_get_sync(musb->controller);
Hema HK7acc6192011-02-28 14:19:34 +05301883
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001884 /*
1885 * REVISIT always use otg_set_peripheral() here too;
Felipe Balbi550a7372008-07-24 12:27:36 +03001886 * this needs to shut down the OTG engine.
1887 */
1888
1889 spin_lock_irqsave(&musb->lock, flags);
1890
Felipe Balbi550a7372008-07-24 12:27:36 +03001891 musb_hnp_stop(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001892
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001893 (void) musb_gadget_vbus_draw(&musb->g, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001894
Antoine Tenarte47d9252014-10-30 18:41:13 +01001895 musb->xceiv->otg->state = OTG_STATE_UNDEFINED;
Felipe Balbid5638fc2015-02-02 17:14:12 -06001896 musb_stop(musb);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001897 otg_set_peripheral(musb->xceiv->otg, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001898
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001899 musb->is_active = 0;
Grazvydas Ignotase21de102013-03-10 02:49:14 +02001900 musb->gadget_driver = NULL;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001901 musb_platform_try_idle(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001902 spin_unlock_irqrestore(&musb->lock, flags);
1903
Felipe Balbi032ec492011-11-24 15:46:26 +02001904 /*
1905 * FIXME we need to be able to register another
1906 * gadget driver here and have everything work;
1907 * that currently misbehaves.
1908 */
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001909
Tony Lindgren4e719182016-09-22 15:58:29 -05001910 /* Force check of devctl register for PM runtime */
Tony Lindgren2bff3912016-11-16 13:21:24 -06001911 schedule_delayed_work(&musb->irq_work, 0);
Tony Lindgren4e719182016-09-22 15:58:29 -05001912
Tony Lindgren7099dbc2016-05-31 10:05:11 -05001913 pm_runtime_mark_last_busy(musb->controller);
1914 pm_runtime_put_autosuspend(musb->controller);
Hema HK7acc6192011-02-28 14:19:34 +05301915
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001916 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001917}
Felipe Balbi550a7372008-07-24 12:27:36 +03001918
1919/* ----------------------------------------------------------------------- */
1920
1921/* lifecycle operations called through plat_uds.c */
1922
1923void musb_g_resume(struct musb *musb)
1924{
1925 musb->is_suspended = 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001926 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001927 case OTG_STATE_B_IDLE:
1928 break;
1929 case OTG_STATE_B_WAIT_ACON:
1930 case OTG_STATE_B_PERIPHERAL:
1931 musb->is_active = 1;
1932 if (musb->gadget_driver && musb->gadget_driver->resume) {
1933 spin_unlock(&musb->lock);
1934 musb->gadget_driver->resume(&musb->g);
1935 spin_lock(&musb->lock);
1936 }
1937 break;
1938 default:
1939 WARNING("unhandled RESUME transition (%s)\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01001940 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001941 }
1942}
1943
1944/* called when SOF packets stop for 3+ msec */
1945void musb_g_suspend(struct musb *musb)
1946{
1947 u8 devctl;
1948
1949 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Bin Liub99d3652016-06-30 12:12:22 -05001950 musb_dbg(musb, "musb_g_suspend: devctl %02x", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001951
Antoine Tenarte47d9252014-10-30 18:41:13 +01001952 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001953 case OTG_STATE_B_IDLE:
1954 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
Antoine Tenarte47d9252014-10-30 18:41:13 +01001955 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001956 break;
1957 case OTG_STATE_B_PERIPHERAL:
1958 musb->is_suspended = 1;
1959 if (musb->gadget_driver && musb->gadget_driver->suspend) {
1960 spin_unlock(&musb->lock);
1961 musb->gadget_driver->suspend(&musb->g);
1962 spin_lock(&musb->lock);
1963 }
1964 break;
1965 default:
1966 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
1967 * A_PERIPHERAL may need care too
1968 */
Bin Liub99d3652016-06-30 12:12:22 -05001969 WARNING("unhandled SUSPEND transition (%s)",
Antoine Tenarte47d9252014-10-30 18:41:13 +01001970 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001971 }
1972}
1973
1974/* Called during SRP */
1975void musb_g_wakeup(struct musb *musb)
1976{
1977 musb_gadget_wakeup(&musb->g);
1978}
1979
1980/* called when VBUS drops below session threshold, and in other cases */
1981void musb_g_disconnect(struct musb *musb)
1982{
1983 void __iomem *mregs = musb->mregs;
1984 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
1985
Bin Liub99d3652016-06-30 12:12:22 -05001986 musb_dbg(musb, "musb_g_disconnect: devctl %02x", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001987
1988 /* clear HR */
1989 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
1990
1991 /* don't draw vbus until new b-default session */
1992 (void) musb_gadget_vbus_draw(&musb->g, 0);
1993
1994 musb->g.speed = USB_SPEED_UNKNOWN;
1995 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
1996 spin_unlock(&musb->lock);
1997 musb->gadget_driver->disconnect(&musb->g);
1998 spin_lock(&musb->lock);
1999 }
2000
Antoine Tenarte47d9252014-10-30 18:41:13 +01002001 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002002 default:
Bin Liub99d3652016-06-30 12:12:22 -05002003 musb_dbg(musb, "Unhandled disconnect %s, setting a_idle",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002004 usb_otg_state_string(musb->xceiv->otg->state));
2005 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07002006 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002007 break;
2008 case OTG_STATE_A_PERIPHERAL:
Antoine Tenarte47d9252014-10-30 18:41:13 +01002009 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
David Brownellab983f2a2009-03-31 12:35:09 -07002010 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002011 break;
2012 case OTG_STATE_B_WAIT_ACON:
2013 case OTG_STATE_B_HOST:
Felipe Balbi550a7372008-07-24 12:27:36 +03002014 case OTG_STATE_B_PERIPHERAL:
2015 case OTG_STATE_B_IDLE:
Antoine Tenarte47d9252014-10-30 18:41:13 +01002016 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002017 break;
2018 case OTG_STATE_B_SRP_INIT:
2019 break;
2020 }
2021
2022 musb->is_active = 0;
2023}
2024
2025void musb_g_reset(struct musb *musb)
2026__releases(musb->lock)
2027__acquires(musb->lock)
2028{
2029 void __iomem *mbase = musb->mregs;
2030 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2031 u8 power;
2032
Bin Liub99d3652016-06-30 12:12:22 -05002033 musb_dbg(musb, "<== %s driver '%s'",
Felipe Balbi550a7372008-07-24 12:27:36 +03002034 (devctl & MUSB_DEVCTL_BDEVICE)
2035 ? "B-Device" : "A-Device",
Felipe Balbi550a7372008-07-24 12:27:36 +03002036 musb->gadget_driver
2037 ? musb->gadget_driver->driver.name
2038 : NULL
2039 );
2040
Felipe Balbi1189f7f2014-11-06 14:27:54 +08002041 /* report reset, if we didn't already (flushing EP state) */
2042 if (musb->gadget_driver && musb->g.speed != USB_SPEED_UNKNOWN) {
2043 spin_unlock(&musb->lock);
2044 usb_gadget_udc_reset(&musb->g, musb->gadget_driver);
2045 spin_lock(&musb->lock);
2046 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002047
2048 /* clear HR */
2049 else if (devctl & MUSB_DEVCTL_HR)
2050 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2051
2052
2053 /* what speed did we negotiate? */
2054 power = musb_readb(mbase, MUSB_POWER);
2055 musb->g.speed = (power & MUSB_POWER_HSMODE)
2056 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2057
2058 /* start in USB_STATE_DEFAULT */
2059 musb->is_active = 1;
2060 musb->is_suspended = 0;
2061 MUSB_DEV_MODE(musb);
2062 musb->address = 0;
2063 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2064
2065 musb->may_wakeup = 0;
2066 musb->g.b_hnp_enable = 0;
2067 musb->g.a_alt_hnp_support = 0;
2068 musb->g.a_hnp_support = 0;
Robert Baldygaca1023c2015-07-28 07:20:00 +02002069 musb->g.quirk_zlp_not_supp = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002070
2071 /* Normal reset, as B-Device;
2072 * or else after HNP, as A-Device
2073 */
Apelete Seketeli23db9fd2013-12-19 21:42:27 +01002074 if (!musb->g.is_otg) {
2075 /* USB device controllers that are not OTG compatible
2076 * may not have DEVCTL register in silicon.
2077 * In that case, do not rely on devctl for setting
2078 * peripheral mode.
2079 */
Antoine Tenarte47d9252014-10-30 18:41:13 +01002080 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Apelete Seketeli23db9fd2013-12-19 21:42:27 +01002081 musb->g.is_a_peripheral = 0;
2082 } else if (devctl & MUSB_DEVCTL_BDEVICE) {
Antoine Tenarte47d9252014-10-30 18:41:13 +01002083 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002084 musb->g.is_a_peripheral = 0;
Felipe Balbi032ec492011-11-24 15:46:26 +02002085 } else {
Antoine Tenarte47d9252014-10-30 18:41:13 +01002086 musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002087 musb->g.is_a_peripheral = 1;
Felipe Balbi032ec492011-11-24 15:46:26 +02002088 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002089
2090 /* start with default limits on VBUS power draw */
Felipe Balbi032ec492011-11-24 15:46:26 +02002091 (void) musb_gadget_vbus_draw(&musb->g, 8);
Felipe Balbi550a7372008-07-24 12:27:36 +03002092}