blob: cc503591b634b8810ddd6c914354b2c3f7e41721 [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
2 * MUSB OTG driver peripheral support
3 *
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
Sergei Shtylyovcea83242009-11-18 22:51:18 +03007 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
Felipe Balbi550a7372008-07-24 12:27:36 +03008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
26 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36#include <linux/kernel.h>
37#include <linux/list.h>
38#include <linux/timer.h>
39#include <linux/module.h>
40#include <linux/smp.h>
41#include <linux/spinlock.h>
42#include <linux/delay.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030043#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030045
46#include "musb_core.h"
47
48
Felipe Balbi550a7372008-07-24 12:27:36 +030049/* ----------------------------------------------------------------------- */
50
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010051#define is_buffer_mapped(req) (is_dma_capable() && \
52 (req->map_state != UN_MAPPED))
53
Hema Kalliguddi92d27112010-11-15 04:24:01 -060054/* Maps the buffer to dma */
55
56static inline void map_dma_buffer(struct musb_request *request,
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010057 struct musb *musb, struct musb_ep *musb_ep)
Hema Kalliguddi92d27112010-11-15 04:24:01 -060058{
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +010059 int compatible = true;
60 struct dma_controller *dma = musb->dma_controller;
61
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010062 request->map_state = UN_MAPPED;
63
64 if (!is_dma_capable() || !musb_ep->dma)
65 return;
66
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +010067 /* Check if DMA engine can handle this request.
68 * DMA code must reject the USB request explicitly.
69 * Default behaviour is to map the request.
70 */
71 if (dma->is_compatible)
72 compatible = dma->is_compatible(musb_ep->dma,
73 musb_ep->packet_sz, request->request.buf,
74 request->request.length);
75 if (!compatible)
76 return;
77
Hema Kalliguddi92d27112010-11-15 04:24:01 -060078 if (request->request.dma == DMA_ADDR_INVALID) {
Sebastian Andrzej Siewior7b360f42013-08-13 19:35:43 +020079 dma_addr_t dma_addr;
80 int ret;
81
82 dma_addr = dma_map_single(
Hema Kalliguddi92d27112010-11-15 04:24:01 -060083 musb->controller,
84 request->request.buf,
85 request->request.length,
86 request->tx
87 ? DMA_TO_DEVICE
88 : DMA_FROM_DEVICE);
Sebastian Andrzej Siewior7b360f42013-08-13 19:35:43 +020089 ret = dma_mapping_error(musb->controller, dma_addr);
90 if (ret)
91 return;
92
93 request->request.dma = dma_addr;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010094 request->map_state = MUSB_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -060095 } else {
96 dma_sync_single_for_device(musb->controller,
97 request->request.dma,
98 request->request.length,
99 request->tx
100 ? DMA_TO_DEVICE
101 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100102 request->map_state = PRE_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600103 }
104}
105
106/* Unmap the buffer from dma and maps it back to cpu */
107static inline void unmap_dma_buffer(struct musb_request *request,
108 struct musb *musb)
109{
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +0530110 struct musb_ep *musb_ep = request->ep;
111
112 if (!is_buffer_mapped(request) || !musb_ep->dma)
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100113 return;
114
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600115 if (request->request.dma == DMA_ADDR_INVALID) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300116 dev_vdbg(musb->controller,
117 "not unmapping a never mapped buffer\n");
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600118 return;
119 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100120 if (request->map_state == MUSB_MAPPED) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600121 dma_unmap_single(musb->controller,
122 request->request.dma,
123 request->request.length,
124 request->tx
125 ? DMA_TO_DEVICE
126 : DMA_FROM_DEVICE);
127 request->request.dma = DMA_ADDR_INVALID;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100128 } else { /* PRE_MAPPED */
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600129 dma_sync_single_for_cpu(musb->controller,
130 request->request.dma,
131 request->request.length,
132 request->tx
133 ? DMA_TO_DEVICE
134 : DMA_FROM_DEVICE);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600135 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100136 request->map_state = UN_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600137}
138
Felipe Balbi550a7372008-07-24 12:27:36 +0300139/*
140 * Immediately complete a request.
141 *
142 * @param request the request to complete
143 * @param status the status to complete the request with
144 * Context: controller locked, IRQs blocked.
145 */
146void musb_g_giveback(
147 struct musb_ep *ep,
148 struct usb_request *request,
149 int status)
150__releases(ep->musb->lock)
151__acquires(ep->musb->lock)
152{
153 struct musb_request *req;
154 struct musb *musb;
155 int busy = ep->busy;
156
157 req = to_musb_request(request);
158
Felipe Balbiad1adb82011-02-16 12:40:05 +0200159 list_del(&req->list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300160 if (req->request.status == -EINPROGRESS)
161 req->request.status = status;
162 musb = req->musb;
163
164 ep->busy = 1;
165 spin_unlock(&musb->lock);
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +0530166
167 if (!dma_mapping_error(&musb->g.dev, request->dma))
168 unmap_dma_buffer(req, musb);
169
Felipe Balbi550a7372008-07-24 12:27:36 +0300170 if (request->status == 0)
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300171 dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300172 ep->end_point.name, request,
173 req->request.actual, req->request.length);
174 else
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300175 dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300176 ep->end_point.name, request,
177 req->request.actual, req->request.length,
178 request->status);
Michal Sojka304f7e52014-09-24 22:43:19 +0200179 usb_gadget_giveback_request(&req->ep->end_point, &req->request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300180 spin_lock(&musb->lock);
181 ep->busy = busy;
182}
183
184/* ----------------------------------------------------------------------- */
185
186/*
187 * Abort requests queued to an endpoint using the status. Synchronous.
188 * caller locked controller and blocked irqs, and selected this ep.
189 */
190static void nuke(struct musb_ep *ep, const int status)
191{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300192 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300193 struct musb_request *req = NULL;
194 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
195
196 ep->busy = 1;
197
198 if (is_dma_capable() && ep->dma) {
199 struct dma_controller *c = ep->musb->dma_controller;
200 int value;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700201
Felipe Balbi550a7372008-07-24 12:27:36 +0300202 if (ep->is_in) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700203 /*
204 * The programming guide says that we must not clear
205 * the DMAMODE bit before DMAENAB, so we only
206 * clear it in the second write...
207 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300208 musb_writew(epio, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700209 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
Felipe Balbi550a7372008-07-24 12:27:36 +0300210 musb_writew(epio, MUSB_TXCSR,
211 0 | MUSB_TXCSR_FLUSHFIFO);
212 } else {
213 musb_writew(epio, MUSB_RXCSR,
214 0 | MUSB_RXCSR_FLUSHFIFO);
215 musb_writew(epio, MUSB_RXCSR,
216 0 | MUSB_RXCSR_FLUSHFIFO);
217 }
218
219 value = c->channel_abort(ep->dma);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300220 dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
221 ep->name, value);
Felipe Balbi550a7372008-07-24 12:27:36 +0300222 c->channel_release(ep->dma);
223 ep->dma = NULL;
224 }
225
Felipe Balbiad1adb82011-02-16 12:40:05 +0200226 while (!list_empty(&ep->req_list)) {
227 req = list_first_entry(&ep->req_list, struct musb_request, list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300228 musb_g_giveback(ep, &req->request, status);
229 }
230}
231
232/* ----------------------------------------------------------------------- */
233
234/* Data transfers - pure PIO, pure DMA, or mixed mode */
235
236/*
237 * This assumes the separate CPPI engine is responding to DMA requests
238 * from the usb core ... sequenced a bit differently from mentor dma.
239 */
240
241static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
242{
243 if (can_bulk_split(musb, ep->type))
244 return ep->hw_ep->max_packet_sz_tx;
245 else
246 return ep->packet_sz;
247}
248
Felipe Balbi550a7372008-07-24 12:27:36 +0300249/*
250 * An endpoint is transmitting data. This can be called either from
251 * the IRQ routine or from ep.queue() to kickstart a request on an
252 * endpoint.
253 *
254 * Context: controller locked, IRQs blocked, endpoint selected
255 */
256static void txstate(struct musb *musb, struct musb_request *req)
257{
258 u8 epnum = req->epnum;
259 struct musb_ep *musb_ep;
260 void __iomem *epio = musb->endpoints[epnum].regs;
261 struct usb_request *request;
262 u16 fifo_count = 0, csr;
263 int use_dma = 0;
264
265 musb_ep = req->ep;
266
Vikram Panditaabf710e2012-05-18 13:48:04 -0700267 /* Check if EP is disabled */
268 if (!musb_ep->desc) {
269 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
270 musb_ep->end_point.name);
271 return;
272 }
273
Felipe Balbi550a7372008-07-24 12:27:36 +0300274 /* we shouldn't get here while DMA is active ... but we do ... */
275 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300276 dev_dbg(musb->controller, "dma pending...\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300277 return;
278 }
279
280 /* read TXCSR before */
281 csr = musb_readw(epio, MUSB_TXCSR);
282
283 request = &req->request;
284 fifo_count = min(max_ep_writesize(musb, musb_ep),
285 (int)(request->length - request->actual));
286
287 if (csr & MUSB_TXCSR_TXPKTRDY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300288 dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300289 musb_ep->end_point.name, csr);
290 return;
291 }
292
293 if (csr & MUSB_TXCSR_P_SENDSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300294 dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300295 musb_ep->end_point.name, csr);
296 return;
297 }
298
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300299 dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300300 epnum, musb_ep->packet_sz, fifo_count,
301 csr);
302
303#ifndef CONFIG_MUSB_PIO_ONLY
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100304 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300305 struct dma_controller *c = musb->dma_controller;
Ming Lei66af83d2010-09-20 10:32:06 +0300306 size_t request_size;
307
308 /* setup DMA, then program endpoint CSR */
309 request_size = min_t(size_t, request->length - request->actual,
310 musb_ep->dma->max_len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300311
Ajay Kumar Guptad17d5352012-07-20 11:07:23 +0530312 use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300313
314 /* MUSB_TXCSR_P_ISO is still set correctly */
315
Felipe Balbi03840fa2015-08-06 10:47:16 -0500316 if (musb_dma_inventra(musb) || musb_dma_ux500(musb)) {
Anand Gadiyard1043a22009-04-02 12:07:08 -0700317 if (request_size < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300318 musb_ep->dma->desired_mode = 0;
319 else
320 musb_ep->dma->desired_mode = 1;
321
322 use_dma = use_dma && c->channel_program(
323 musb_ep->dma, musb_ep->packet_sz,
324 musb_ep->dma->desired_mode,
Cliff Cai796a83f2009-12-21 21:18:02 -0500325 request->dma + request->actual, request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300326 if (use_dma) {
327 if (musb_ep->dma->desired_mode == 0) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700328 /*
329 * We must not clear the DMAMODE bit
330 * before the DMAENAB bit -- and the
331 * latter doesn't always get cleared
332 * before we get here...
333 */
334 csr &= ~(MUSB_TXCSR_AUTOSET
335 | MUSB_TXCSR_DMAENAB);
336 musb_writew(epio, MUSB_TXCSR, csr
337 | MUSB_TXCSR_P_WZC_BITS);
338 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300339 csr |= (MUSB_TXCSR_DMAENAB |
340 MUSB_TXCSR_MODE);
341 /* against programming guide */
Ming Leif11d8932010-09-24 13:44:04 +0300342 } else {
343 csr |= (MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300344 | MUSB_TXCSR_DMAMODE
345 | MUSB_TXCSR_MODE);
supriya karanthbb3a2ef2012-12-06 11:12:48 +0530346 /*
347 * Enable Autoset according to table
348 * below
349 * bulk_split hb_mult Autoset_Enable
350 * 0 0 Yes(Normal)
351 * 0 >0 No(High BW ISO)
352 * 1 0 Yes(HS bulk)
353 * 1 >0 Yes(FS bulk)
354 */
355 if (!musb_ep->hb_mult ||
356 (musb_ep->hb_mult &&
357 can_bulk_split(musb,
358 musb_ep->type)))
Ming Leif11d8932010-09-24 13:44:04 +0300359 csr |= MUSB_TXCSR_AUTOSET;
360 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300361 csr &= ~MUSB_TXCSR_P_UNDERRUN;
Ming Leif11d8932010-09-24 13:44:04 +0300362
Felipe Balbi550a7372008-07-24 12:27:36 +0300363 musb_writew(epio, MUSB_TXCSR, csr);
364 }
365 }
366
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700367 if (is_cppi_enabled(musb)) {
Sebastian Andrzej Siewiorfc525752013-08-13 19:38:23 +0200368 /* program endpoint CSR first, then setup DMA */
369 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
370 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
371 MUSB_TXCSR_MODE;
372 musb_writew(epio, MUSB_TXCSR, (MUSB_TXCSR_P_WZC_BITS &
373 ~MUSB_TXCSR_P_UNDERRUN) | csr);
374
375 /* ensure writebuffer is empty */
376 csr = musb_readw(epio, MUSB_TXCSR);
377
378 /*
379 * NOTE host side sets DMAENAB later than this; both are
380 * OK since the transfer dma glue (between CPPI and
381 * Mentor fifos) just tells CPPI it could start. Data
382 * only moves to the USB TX fifo when both fifos are
383 * ready.
384 */
385 /*
386 * "mode" is irrelevant here; handle terminating ZLPs
387 * like PIO does, since the hardware RNDIS mode seems
388 * unreliable except for the
389 * last-packet-is-already-short case.
390 */
391 use_dma = use_dma && c->channel_program(
392 musb_ep->dma, musb_ep->packet_sz,
393 0,
394 request->dma + request->actual,
395 request_size);
396 if (!use_dma) {
397 c->channel_release(musb_ep->dma);
398 musb_ep->dma = NULL;
399 csr &= ~MUSB_TXCSR_DMAENAB;
400 musb_writew(epio, MUSB_TXCSR, csr);
401 /* invariant: prequest->buf is non-null */
402 }
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700403 } else if (tusb_dma_omap(musb))
Sebastian Andrzej Siewiorfc525752013-08-13 19:38:23 +0200404 use_dma = use_dma && c->channel_program(
405 musb_ep->dma, musb_ep->packet_sz,
406 request->zero,
407 request->dma + request->actual,
408 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300409 }
410#endif
411
412 if (!use_dma) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600413 /*
414 * Unmap the dma buffer back to cpu if dma channel
415 * programming fails
416 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100417 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600418
Felipe Balbi550a7372008-07-24 12:27:36 +0300419 musb_write_fifo(musb_ep->hw_ep, fifo_count,
420 (u8 *) (request->buf + request->actual));
421 request->actual += fifo_count;
422 csr |= MUSB_TXCSR_TXPKTRDY;
423 csr &= ~MUSB_TXCSR_P_UNDERRUN;
424 musb_writew(epio, MUSB_TXCSR, csr);
425 }
426
427 /* host may already have the data when this message shows... */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300428 dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300429 musb_ep->end_point.name, use_dma ? "dma" : "pio",
430 request->actual, request->length,
431 musb_readw(epio, MUSB_TXCSR),
432 fifo_count,
433 musb_readw(epio, MUSB_TXMAXP));
434}
435
436/*
437 * FIFO state update (e.g. data ready).
438 * Called from IRQ, with controller locked.
439 */
440void musb_g_tx(struct musb *musb, u8 epnum)
441{
442 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200443 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300444 struct usb_request *request;
445 u8 __iomem *mbase = musb->mregs;
446 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
447 void __iomem *epio = musb->endpoints[epnum].regs;
448 struct dma_channel *dma;
449
450 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200451 req = next_request(musb_ep);
452 request = &req->request;
Felipe Balbi550a7372008-07-24 12:27:36 +0300453
454 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300455 dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300456
457 dma = is_dma_capable() ? musb_ep->dma : NULL;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300458
459 /*
460 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
461 * probably rates reporting as a host error.
462 */
463 if (csr & MUSB_TXCSR_P_SENTSTALL) {
464 csr |= MUSB_TXCSR_P_WZC_BITS;
465 csr &= ~MUSB_TXCSR_P_SENTSTALL;
466 musb_writew(epio, MUSB_TXCSR, csr);
467 return;
468 }
469
470 if (csr & MUSB_TXCSR_P_UNDERRUN) {
471 /* We NAKed, no big deal... little reason to care. */
472 csr |= MUSB_TXCSR_P_WZC_BITS;
473 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
474 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300475 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
476 epnum, request);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300477 }
478
479 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
480 /*
481 * SHOULD NOT HAPPEN... has with CPPI though, after
482 * changing SENDSTALL (and other cases); harmless?
Felipe Balbi550a7372008-07-24 12:27:36 +0300483 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300484 dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300485 return;
486 }
487
488 if (request) {
489 u8 is_dma = 0;
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700490 bool short_packet = false;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300491
492 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
493 is_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300494 csr |= MUSB_TXCSR_P_WZC_BITS;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300495 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
Mian Yousaf Kaukab100d4a92011-03-15 16:24:24 +0100496 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
Felipe Balbi550a7372008-07-24 12:27:36 +0300497 musb_writew(epio, MUSB_TXCSR, csr);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300498 /* Ensure writebuffer is empty. */
499 csr = musb_readw(epio, MUSB_TXCSR);
500 request->actual += musb_ep->dma->actual_len;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300501 dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300502 epnum, csr, musb_ep->dma->actual_len, request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300503 }
504
Ming Leie7379aa2010-09-24 13:44:14 +0300505 /*
506 * First, maybe a terminating short packet. Some DMA
507 * engines might handle this by themselves.
508 */
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700509 if ((request->zero && request->length)
Ming Leie7379aa2010-09-24 13:44:14 +0300510 && (request->length % musb_ep->packet_sz == 0)
511 && (request->actual == request->length))
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700512 short_packet = true;
513
514 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb)) &&
515 (is_dma && (!dma->desired_mode ||
Ming Leie7379aa2010-09-24 13:44:14 +0300516 (request->actual &
Tony Lindgrenfb91cdd2015-05-01 12:29:30 -0700517 (musb_ep->packet_sz - 1)))))
518 short_packet = true;
519
520 if (short_packet) {
Ming Leie7379aa2010-09-24 13:44:14 +0300521 /*
522 * On DMA completion, FIFO may not be
523 * available yet...
524 */
525 if (csr & MUSB_TXCSR_TXPKTRDY)
526 return;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300527
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300528 dev_dbg(musb->controller, "sending zero pkt\n");
Ming Leie7379aa2010-09-24 13:44:14 +0300529 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
530 | MUSB_TXCSR_TXPKTRDY);
531 request->zero = 0;
532 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300533
Ming Leie7379aa2010-09-24 13:44:14 +0300534 if (request->actual == request->length) {
535 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530536 /*
537 * In the giveback function the MUSB lock is
538 * released and acquired after sometime. During
539 * this time period the INDEX register could get
540 * changed by the gadget_queue function especially
541 * on SMP systems. Reselect the INDEX to be sure
542 * we are reading/modifying the right registers
543 */
544 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200545 req = musb_ep->desc ? next_request(musb_ep) : NULL;
546 if (!req) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300547 dev_dbg(musb->controller, "%s idle now\n",
Ming Leie7379aa2010-09-24 13:44:14 +0300548 musb_ep->end_point.name);
549 return;
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300550 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300551 }
552
Felipe Balbiad1adb82011-02-16 12:40:05 +0200553 txstate(musb, req);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300554 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300555}
556
557/* ------------------------------------------------------------ */
558
Felipe Balbi550a7372008-07-24 12:27:36 +0300559/*
560 * Context: controller locked, IRQs blocked, endpoint selected
561 */
562static void rxstate(struct musb *musb, struct musb_request *req)
563{
Felipe Balbi550a7372008-07-24 12:27:36 +0300564 const u8 epnum = req->epnum;
565 struct usb_request *request = &req->request;
Ming Leibd2e74d2010-09-20 10:32:01 +0300566 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300567 void __iomem *epio = musb->endpoints[epnum].regs;
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400568 unsigned len = 0;
569 u16 fifo_count;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300570 u16 csr = musb_readw(epio, MUSB_RXCSR);
Ming Leibd2e74d2010-09-20 10:32:01 +0300571 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700572 u8 use_mode_1;
Ming Leibd2e74d2010-09-20 10:32:01 +0300573
574 if (hw_ep->is_shared_fifo)
575 musb_ep = &hw_ep->ep_in;
576 else
577 musb_ep = &hw_ep->ep_out;
578
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400579 fifo_count = musb_ep->packet_sz;
Felipe Balbi550a7372008-07-24 12:27:36 +0300580
Vikram Panditaabf710e2012-05-18 13:48:04 -0700581 /* Check if EP is disabled */
582 if (!musb_ep->desc) {
583 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
584 musb_ep->end_point.name);
585 return;
586 }
587
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300588 /* We shouldn't get here while DMA is active, but we do... */
589 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300590 dev_dbg(musb->controller, "DMA pending...\n");
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300591 return;
592 }
593
594 if (csr & MUSB_RXCSR_P_SENDSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300595 dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300596 musb_ep->end_point.name, csr);
597 return;
598 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300599
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700600 if (is_cppi_enabled(musb) && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300601 struct dma_controller *c = musb->dma_controller;
602 struct dma_channel *channel = musb_ep->dma;
603
604 /* NOTE: CPPI won't actually stop advancing the DMA
605 * queue after short packet transfers, so this is almost
606 * always going to run as IRQ-per-packet DMA so that
607 * faults will be handled correctly.
608 */
609 if (c->channel_program(channel,
610 musb_ep->packet_sz,
611 !request->short_not_ok,
612 request->dma + request->actual,
613 request->length - request->actual)) {
614
615 /* make sure that if an rxpkt arrived after the irq,
616 * the cppi engine will be ready to take it as soon
617 * as DMA is enabled
618 */
619 csr &= ~(MUSB_RXCSR_AUTOCLEAR
620 | MUSB_RXCSR_DMAMODE);
621 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
622 musb_writew(epio, MUSB_RXCSR, csr);
623 return;
624 }
625 }
626
627 if (csr & MUSB_RXCSR_RXPKTRDY) {
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400628 fifo_count = musb_readw(epio, MUSB_RXCOUNT);
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700629
630 /*
Felipe Balbi00a89182012-10-26 09:55:31 +0300631 * Enable Mode 1 on RX transfers only when short_not_ok flag
632 * is set. Currently short_not_ok flag is set only from
633 * file_storage and f_mass_storage drivers
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700634 */
Felipe Balbi00a89182012-10-26 09:55:31 +0300635
636 if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700637 use_mode_1 = 1;
638 else
639 use_mode_1 = 0;
640
Felipe Balbi550a7372008-07-24 12:27:36 +0300641 if (request->actual < request->length) {
Felipe Balbi03840fa2015-08-06 10:47:16 -0500642 if (!is_buffer_mapped(req))
643 goto buffer_aint_mapped;
644
645 if (musb_dma_inventra(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300646 struct dma_controller *c;
647 struct dma_channel *channel;
648 int use_dma = 0;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200649 unsigned int transfer_size;
Felipe Balbi550a7372008-07-24 12:27:36 +0300650
651 c = musb->dma_controller;
652 channel = musb_ep->dma;
653
Felipe Balbi00a89182012-10-26 09:55:31 +0300654 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
655 * mode 0 only. So we do not get endpoint interrupts due to DMA
656 * completion. We only get interrupts from DMA controller.
657 *
658 * We could operate in DMA mode 1 if we knew the size of the tranfer
659 * in advance. For mass storage class, request->length = what the host
660 * sends, so that'd work. But for pretty much everything else,
661 * request->length is routinely more than what the host sends. For
662 * most these gadgets, end of is signified either by a short packet,
663 * or filling the last byte of the buffer. (Sending extra data in
664 * that last pckate should trigger an overflow fault.) But in mode 1,
665 * we don't get DMA completion interrupt for short packets.
666 *
667 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
668 * to get endpoint interrupt on every DMA req, but that didn't seem
669 * to work reliably.
670 *
671 * REVISIT an updated g_file_storage can set req->short_not_ok, which
672 * then becomes usable as a runtime "use mode 1" hint...
673 */
674
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700675 /* Experimental: Mode1 works with mass storage use cases */
676 if (use_mode_1) {
Ming Lei9001d802010-09-25 05:50:43 -0500677 csr |= MUSB_RXCSR_AUTOCLEAR;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700678 musb_writew(epio, MUSB_RXCSR, csr);
679 csr |= MUSB_RXCSR_DMAENAB;
680 musb_writew(epio, MUSB_RXCSR, csr);
681
682 /*
683 * this special sequence (enabling and then
684 * disabling MUSB_RXCSR_DMAMODE) is required
685 * to get DMAReq to activate
686 */
687 musb_writew(epio, MUSB_RXCSR,
688 csr | MUSB_RXCSR_DMAMODE);
689 musb_writew(epio, MUSB_RXCSR, csr);
690
Felipe Balbi37730ec2013-02-06 10:19:15 +0200691 transfer_size = min_t(unsigned int,
692 request->length -
693 request->actual,
Roger Quadros660fa882012-08-07 16:26:32 +0300694 channel->max_len);
695 musb_ep->dma->desired_mode = 1;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700696 } else {
697 if (!musb_ep->hb_mult &&
698 musb_ep->hw_ep->rx_double_buffered)
699 csr |= MUSB_RXCSR_AUTOCLEAR;
700 csr |= MUSB_RXCSR_DMAENAB;
701 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300702
Roger Quadros660fa882012-08-07 16:26:32 +0300703 transfer_size = min(request->length - request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400704 (unsigned)fifo_count);
Roger Quadros660fa882012-08-07 16:26:32 +0300705 musb_ep->dma->desired_mode = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300706 }
707
Roger Quadros660fa882012-08-07 16:26:32 +0300708 use_dma = c->channel_program(
709 channel,
710 musb_ep->packet_sz,
711 channel->desired_mode,
712 request->dma
713 + request->actual,
714 transfer_size);
715
Felipe Balbi550a7372008-07-24 12:27:36 +0300716 if (use_dma)
717 return;
718 }
Felipe Balbi03840fa2015-08-06 10:47:16 -0500719
720 if ((musb_dma_ux500(musb)) &&
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100721 (request->actual < request->length)) {
722
723 struct dma_controller *c;
724 struct dma_channel *channel;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200725 unsigned int transfer_size = 0;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100726
727 c = musb->dma_controller;
728 channel = musb_ep->dma;
729
730 /* In case first packet is short */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400731 if (fifo_count < musb_ep->packet_sz)
732 transfer_size = fifo_count;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100733 else if (request->short_not_ok)
Felipe Balbi37730ec2013-02-06 10:19:15 +0200734 transfer_size = min_t(unsigned int,
735 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100736 request->actual,
737 channel->max_len);
738 else
Felipe Balbi37730ec2013-02-06 10:19:15 +0200739 transfer_size = min_t(unsigned int,
740 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100741 request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400742 (unsigned)fifo_count);
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100743
744 csr &= ~MUSB_RXCSR_DMAMODE;
745 csr |= (MUSB_RXCSR_DMAENAB |
746 MUSB_RXCSR_AUTOCLEAR);
747
748 musb_writew(epio, MUSB_RXCSR, csr);
749
750 if (transfer_size <= musb_ep->packet_sz) {
751 musb_ep->dma->desired_mode = 0;
752 } else {
753 musb_ep->dma->desired_mode = 1;
754 /* Mode must be set after DMAENAB */
755 csr |= MUSB_RXCSR_DMAMODE;
756 musb_writew(epio, MUSB_RXCSR, csr);
757 }
758
759 if (c->channel_program(channel,
760 musb_ep->packet_sz,
761 channel->desired_mode,
762 request->dma
763 + request->actual,
764 transfer_size))
765
766 return;
767 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300768
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400769 len = request->length - request->actual;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300770 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300771 musb_ep->end_point.name,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400772 fifo_count, len,
Felipe Balbi550a7372008-07-24 12:27:36 +0300773 musb_ep->packet_sz);
774
Felipe Balbic2c96322009-02-21 15:29:42 -0800775 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300776
Felipe Balbi03840fa2015-08-06 10:47:16 -0500777 if (tusb_dma_omap(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300778 struct dma_controller *c = musb->dma_controller;
779 struct dma_channel *channel = musb_ep->dma;
780 u32 dma_addr = request->dma + request->actual;
781 int ret;
782
783 ret = c->channel_program(channel,
784 musb_ep->packet_sz,
785 channel->desired_mode,
786 dma_addr,
787 fifo_count);
788 if (ret)
789 return;
790 }
Felipe Balbi03840fa2015-08-06 10:47:16 -0500791
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600792 /*
793 * Unmap the dma buffer back to cpu if dma channel
794 * programming fails. This buffer is mapped if the
795 * channel allocation is successful
796 */
Felipe Balbi03840fa2015-08-06 10:47:16 -0500797 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600798
Felipe Balbi03840fa2015-08-06 10:47:16 -0500799 /*
800 * Clear DMAENAB and AUTOCLEAR for the
801 * PIO mode transfer
802 */
803 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
804 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300805
Felipe Balbi03840fa2015-08-06 10:47:16 -0500806buffer_aint_mapped:
Felipe Balbi550a7372008-07-24 12:27:36 +0300807 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
808 (request->buf + request->actual));
809 request->actual += fifo_count;
810
811 /* REVISIT if we left anything in the fifo, flush
812 * it and report -EOVERFLOW
813 */
814
815 /* ack the read! */
816 csr |= MUSB_RXCSR_P_WZC_BITS;
817 csr &= ~MUSB_RXCSR_RXPKTRDY;
818 musb_writew(epio, MUSB_RXCSR, csr);
819 }
820 }
821
822 /* reach the end or short packet detected */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400823 if (request->actual == request->length ||
824 fifo_count < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300825 musb_g_giveback(musb_ep, request, 0);
826}
827
828/*
829 * Data ready for a request; called from IRQ
830 */
831void musb_g_rx(struct musb *musb, u8 epnum)
832{
833 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200834 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300835 struct usb_request *request;
836 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300837 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300838 void __iomem *epio = musb->endpoints[epnum].regs;
839 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300840 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
841
842 if (hw_ep->is_shared_fifo)
843 musb_ep = &hw_ep->ep_in;
844 else
845 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300846
847 musb_ep_select(mbase, epnum);
848
Felipe Balbiad1adb82011-02-16 12:40:05 +0200849 req = next_request(musb_ep);
850 if (!req)
Maulik Mankad0abdc362009-12-22 16:18:19 +0530851 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300852
Felipe Balbiad1adb82011-02-16 12:40:05 +0200853 request = &req->request;
854
Felipe Balbi550a7372008-07-24 12:27:36 +0300855 csr = musb_readw(epio, MUSB_RXCSR);
856 dma = is_dma_capable() ? musb_ep->dma : NULL;
857
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300858 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
Felipe Balbi550a7372008-07-24 12:27:36 +0300859 csr, dma ? " (dma)" : "", request);
860
861 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300862 csr |= MUSB_RXCSR_P_WZC_BITS;
863 csr &= ~MUSB_RXCSR_P_SENTSTALL;
864 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300865 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300866 }
867
868 if (csr & MUSB_RXCSR_P_OVERRUN) {
869 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
870 csr &= ~MUSB_RXCSR_P_OVERRUN;
871 musb_writew(epio, MUSB_RXCSR, csr);
872
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300873 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
Sergei Shtylyov43467862010-09-24 13:44:12 +0300874 if (request->status == -EINPROGRESS)
Felipe Balbi550a7372008-07-24 12:27:36 +0300875 request->status = -EOVERFLOW;
876 }
877 if (csr & MUSB_RXCSR_INCOMPRX) {
878 /* REVISIT not necessarily an error */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300879 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +0300880 }
881
882 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
883 /* "should not happen"; likely RXPKTRDY pending for DMA */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300884 dev_dbg(musb->controller, "%s busy, csr %04x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300885 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300886 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300887 }
888
889 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
890 csr &= ~(MUSB_RXCSR_AUTOCLEAR
891 | MUSB_RXCSR_DMAENAB
892 | MUSB_RXCSR_DMAMODE);
893 musb_writew(epio, MUSB_RXCSR,
894 MUSB_RXCSR_P_WZC_BITS | csr);
895
896 request->actual += musb_ep->dma->actual_len;
897
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300898 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300899 epnum, csr,
900 musb_readw(epio, MUSB_RXCSR),
901 musb_ep->dma->actual_len, request);
902
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100903#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
904 defined(CONFIG_USB_UX500_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +0300905 /* Autoclear doesn't clear RxPktRdy for short packets */
Ming Lei9001d802010-09-25 05:50:43 -0500906 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
Felipe Balbi550a7372008-07-24 12:27:36 +0300907 || (dma->actual_len
908 & (musb_ep->packet_sz - 1))) {
909 /* ack the read! */
910 csr &= ~MUSB_RXCSR_RXPKTRDY;
911 musb_writew(epio, MUSB_RXCSR, csr);
912 }
913
914 /* incomplete, and not short? wait for next IN packet */
915 if ((request->actual < request->length)
916 && (musb_ep->dma->actual_len
Ming Lei9001d802010-09-25 05:50:43 -0500917 == musb_ep->packet_sz)) {
918 /* In double buffer case, continue to unload fifo if
919 * there is Rx packet in FIFO.
920 **/
921 csr = musb_readw(epio, MUSB_RXCSR);
922 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
923 hw_ep->rx_double_buffered)
924 goto exit;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300925 return;
Ming Lei9001d802010-09-25 05:50:43 -0500926 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300927#endif
928 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530929 /*
930 * In the giveback function the MUSB lock is
931 * released and acquired after sometime. During
932 * this time period the INDEX register could get
933 * changed by the gadget_queue function especially
934 * on SMP systems. Reselect the INDEX to be sure
935 * we are reading/modifying the right registers
936 */
937 musb_ep_select(mbase, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300938
Felipe Balbiad1adb82011-02-16 12:40:05 +0200939 req = next_request(musb_ep);
940 if (!req)
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300941 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300942 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100943#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
944 defined(CONFIG_USB_UX500_DMA)
Ming Lei9001d802010-09-25 05:50:43 -0500945exit:
Ajay Kumar Guptabb324b02010-11-22 14:22:41 +0530946#endif
Sergei Shtylyov43467862010-09-24 13:44:12 +0300947 /* Analyze request */
Felipe Balbiad1adb82011-02-16 12:40:05 +0200948 rxstate(musb, req);
Felipe Balbi550a7372008-07-24 12:27:36 +0300949}
950
951/* ------------------------------------------------------------ */
952
953static int musb_gadget_enable(struct usb_ep *ep,
954 const struct usb_endpoint_descriptor *desc)
955{
956 unsigned long flags;
957 struct musb_ep *musb_ep;
958 struct musb_hw_ep *hw_ep;
959 void __iomem *regs;
960 struct musb *musb;
961 void __iomem *mbase;
962 u8 epnum;
963 u16 csr;
964 unsigned tmp;
965 int status = -EINVAL;
966
967 if (!ep || !desc)
968 return -EINVAL;
969
970 musb_ep = to_musb_ep(ep);
971 hw_ep = musb_ep->hw_ep;
972 regs = hw_ep->regs;
973 musb = musb_ep->musb;
974 mbase = musb->mregs;
975 epnum = musb_ep->current_epnum;
976
977 spin_lock_irqsave(&musb->lock, flags);
978
979 if (musb_ep->desc) {
980 status = -EBUSY;
981 goto fail;
982 }
Julia Lawall96bcd092009-01-24 17:57:24 -0800983 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +0300984
985 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -0800986 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300987 goto fail;
988
989 /* REVISIT this rules out high bandwidth periodic transfers */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700990 tmp = usb_endpoint_maxp(desc);
Ming Leif11d8932010-09-24 13:44:04 +0300991 if (tmp & ~0x07ff) {
992 int ok;
993
994 if (usb_endpoint_dir_in(desc))
995 ok = musb->hb_iso_tx;
996 else
997 ok = musb->hb_iso_rx;
998
999 if (!ok) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001000 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
Ming Leif11d8932010-09-24 13:44:04 +03001001 goto fail;
1002 }
1003 musb_ep->hb_mult = (tmp >> 11) & 3;
1004 } else {
1005 musb_ep->hb_mult = 0;
1006 }
1007
1008 musb_ep->packet_sz = tmp & 0x7ff;
1009 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
Felipe Balbi550a7372008-07-24 12:27:36 +03001010
1011 /* enable the interrupts for the endpoint, set the endpoint
1012 * packet size (or fail), set the mode, clear the fifo
1013 */
1014 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -08001015 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001016
1017 if (hw_ep->is_shared_fifo)
1018 musb_ep->is_in = 1;
1019 if (!musb_ep->is_in)
1020 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001021
1022 if (tmp > hw_ep->max_packet_sz_tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001023 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001024 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001025 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001026
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001027 musb->intrtxe |= (1 << epnum);
1028 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001029
1030 /* REVISIT if can_bulk_split(), use by updating "tmp";
1031 * likewise high bandwidth periodic tx
1032 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001033 /* Set TXMAXP with the FIFO size of the endpoint
Ming Lei31c99092010-10-19 19:08:25 -05001034 * to disable double buffering mode.
Cliff Cai9f445cb2010-01-28 20:44:18 -05001035 */
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301036 if (musb->double_buffer_not_ok) {
Felipe Balbi06624812011-01-21 13:39:20 +08001037 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301038 } else {
1039 if (can_bulk_split(musb, musb_ep->type))
1040 musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
1041 musb_ep->packet_sz) - 1;
Felipe Balbi06624812011-01-21 13:39:20 +08001042 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1043 | (musb_ep->hb_mult << 11));
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301044 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001045
1046 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1047 if (musb_readw(regs, MUSB_TXCSR)
1048 & MUSB_TXCSR_FIFONOTEMPTY)
1049 csr |= MUSB_TXCSR_FLUSHFIFO;
1050 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1051 csr |= MUSB_TXCSR_P_ISO;
1052
1053 /* set twice in case of double buffering */
1054 musb_writew(regs, MUSB_TXCSR, csr);
1055 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1056 musb_writew(regs, MUSB_TXCSR, csr);
1057
1058 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +03001059
1060 if (hw_ep->is_shared_fifo)
1061 musb_ep->is_in = 0;
1062 if (musb_ep->is_in)
1063 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001064
1065 if (tmp > hw_ep->max_packet_sz_rx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001066 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001067 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001068 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001069
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001070 musb->intrrxe |= (1 << epnum);
1071 musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001072
1073 /* REVISIT if can_bulk_combine() use by updating "tmp"
1074 * likewise high bandwidth periodic rx
1075 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001076 /* Set RXMAXP with the FIFO size of the endpoint
1077 * to disable double buffering mode.
1078 */
Felipe Balbi06624812011-01-21 13:39:20 +08001079 if (musb->double_buffer_not_ok)
1080 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1081 else
1082 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1083 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001084
1085 /* force shared fifo to OUT-only mode */
1086 if (hw_ep->is_shared_fifo) {
1087 csr = musb_readw(regs, MUSB_TXCSR);
1088 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1089 musb_writew(regs, MUSB_TXCSR, csr);
1090 }
1091
1092 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1093 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1094 csr |= MUSB_RXCSR_P_ISO;
1095 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1096 csr |= MUSB_RXCSR_DISNYET;
1097
1098 /* set twice in case of double buffering */
1099 musb_writew(regs, MUSB_RXCSR, csr);
1100 musb_writew(regs, MUSB_RXCSR, csr);
1101 }
1102
1103 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1104 * for some reason you run out of channels here.
1105 */
1106 if (is_dma_capable() && musb->dma_controller) {
1107 struct dma_controller *c = musb->dma_controller;
1108
1109 musb_ep->dma = c->channel_alloc(c, hw_ep,
1110 (desc->bEndpointAddress & USB_DIR_IN));
1111 } else
1112 musb_ep->dma = NULL;
1113
1114 musb_ep->desc = desc;
1115 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001116 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001117 status = 0;
1118
1119 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1120 musb_driver_name, musb_ep->end_point.name,
1121 ({ char *s; switch (musb_ep->type) {
1122 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1123 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1124 default: s = "iso"; break;
Joe Perches2b84f922013-10-08 16:01:37 -07001125 } s; }),
Felipe Balbi550a7372008-07-24 12:27:36 +03001126 musb_ep->is_in ? "IN" : "OUT",
1127 musb_ep->dma ? "dma, " : "",
1128 musb_ep->packet_sz);
1129
1130 schedule_work(&musb->irq_work);
1131
1132fail:
1133 spin_unlock_irqrestore(&musb->lock, flags);
1134 return status;
1135}
1136
1137/*
1138 * Disable an endpoint flushing all requests queued.
1139 */
1140static int musb_gadget_disable(struct usb_ep *ep)
1141{
1142 unsigned long flags;
1143 struct musb *musb;
1144 u8 epnum;
1145 struct musb_ep *musb_ep;
1146 void __iomem *epio;
1147 int status = 0;
1148
1149 musb_ep = to_musb_ep(ep);
1150 musb = musb_ep->musb;
1151 epnum = musb_ep->current_epnum;
1152 epio = musb->endpoints[epnum].regs;
1153
1154 spin_lock_irqsave(&musb->lock, flags);
1155 musb_ep_select(musb->mregs, epnum);
1156
1157 /* zero the endpoint sizes */
1158 if (musb_ep->is_in) {
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001159 musb->intrtxe &= ~(1 << epnum);
1160 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001161 musb_writew(epio, MUSB_TXMAXP, 0);
1162 } else {
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001163 musb->intrrxe &= ~(1 << epnum);
1164 musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001165 musb_writew(epio, MUSB_RXMAXP, 0);
1166 }
1167
1168 musb_ep->desc = NULL;
Grazvydas Ignotas08f75bf2012-05-26 00:21:33 +03001169 musb_ep->end_point.desc = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001170
1171 /* abort all pending DMA and requests */
1172 nuke(musb_ep, -ESHUTDOWN);
1173
1174 schedule_work(&musb->irq_work);
1175
1176 spin_unlock_irqrestore(&(musb->lock), flags);
1177
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001178 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001179
1180 return status;
1181}
1182
1183/*
1184 * Allocate a request for an endpoint.
1185 * Reused by ep0 code.
1186 */
1187struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1188{
1189 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001190 struct musb *musb = musb_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03001191 struct musb_request *request = NULL;
1192
1193 request = kzalloc(sizeof *request, gfp_flags);
Felipe Balbi0607f862010-12-01 11:03:54 +02001194 if (!request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001195 dev_dbg(musb->controller, "not enough memory\n");
Felipe Balbi0607f862010-12-01 11:03:54 +02001196 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001197 }
1198
Felipe Balbi0607f862010-12-01 11:03:54 +02001199 request->request.dma = DMA_ADDR_INVALID;
1200 request->epnum = musb_ep->current_epnum;
1201 request->ep = musb_ep;
1202
Felipe Balbi550a7372008-07-24 12:27:36 +03001203 return &request->request;
1204}
1205
1206/*
1207 * Free a request
1208 * Reused by ep0 code.
1209 */
1210void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1211{
1212 kfree(to_musb_request(req));
1213}
1214
1215static LIST_HEAD(buffers);
1216
1217struct free_record {
1218 struct list_head list;
1219 struct device *dev;
1220 unsigned bytes;
1221 dma_addr_t dma;
1222};
1223
1224/*
1225 * Context: controller locked, IRQs blocked.
1226 */
Sergei Shtylyova666e3e2010-09-11 13:23:12 -05001227void musb_ep_restart(struct musb *musb, struct musb_request *req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001228{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001229 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001230 req->tx ? "TX/IN" : "RX/OUT",
1231 &req->request, req->request.length, req->epnum);
1232
1233 musb_ep_select(musb->mregs, req->epnum);
1234 if (req->tx)
1235 txstate(musb, req);
1236 else
1237 rxstate(musb, req);
1238}
1239
1240static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1241 gfp_t gfp_flags)
1242{
1243 struct musb_ep *musb_ep;
1244 struct musb_request *request;
1245 struct musb *musb;
1246 int status = 0;
1247 unsigned long lockflags;
1248
1249 if (!ep || !req)
1250 return -EINVAL;
1251 if (!req->buf)
1252 return -ENODATA;
1253
1254 musb_ep = to_musb_ep(ep);
1255 musb = musb_ep->musb;
1256
1257 request = to_musb_request(req);
1258 request->musb = musb;
1259
1260 if (request->ep != musb_ep)
1261 return -EINVAL;
1262
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001263 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
Felipe Balbi550a7372008-07-24 12:27:36 +03001264
1265 /* request is mine now... */
1266 request->request.actual = 0;
1267 request->request.status = -EINPROGRESS;
1268 request->epnum = musb_ep->current_epnum;
1269 request->tx = musb_ep->is_in;
1270
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +01001271 map_dma_buffer(request, musb, musb_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001272
1273 spin_lock_irqsave(&musb->lock, lockflags);
1274
1275 /* don't queue if the ep is down */
1276 if (!musb_ep->desc) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001277 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001278 req, ep->name, "disabled");
1279 status = -ESHUTDOWN;
Sebastian Andrzej Siewior23a53d92013-06-19 17:38:15 +02001280 unmap_dma_buffer(request, musb);
1281 goto unlock;
Felipe Balbi550a7372008-07-24 12:27:36 +03001282 }
1283
1284 /* add request to the list */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001285 list_add_tail(&request->list, &musb_ep->req_list);
Felipe Balbi550a7372008-07-24 12:27:36 +03001286
1287 /* it this is the head of the queue, start i/o ... */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001288 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
Felipe Balbi550a7372008-07-24 12:27:36 +03001289 musb_ep_restart(musb, request);
1290
Sebastian Andrzej Siewior23a53d92013-06-19 17:38:15 +02001291unlock:
Felipe Balbi550a7372008-07-24 12:27:36 +03001292 spin_unlock_irqrestore(&musb->lock, lockflags);
1293 return status;
1294}
1295
1296static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1297{
1298 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001299 struct musb_request *req = to_musb_request(request);
1300 struct musb_request *r;
Felipe Balbi550a7372008-07-24 12:27:36 +03001301 unsigned long flags;
1302 int status = 0;
1303 struct musb *musb = musb_ep->musb;
1304
1305 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1306 return -EINVAL;
1307
1308 spin_lock_irqsave(&musb->lock, flags);
1309
1310 list_for_each_entry(r, &musb_ep->req_list, list) {
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001311 if (r == req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001312 break;
1313 }
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001314 if (r != req) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001315 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001316 status = -EINVAL;
1317 goto done;
1318 }
1319
1320 /* if the hardware doesn't have the request, easy ... */
Felipe Balbi3d5ad132011-03-22 11:38:49 +02001321 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
Felipe Balbi550a7372008-07-24 12:27:36 +03001322 musb_g_giveback(musb_ep, request, -ECONNRESET);
1323
1324 /* ... else abort the dma transfer ... */
1325 else if (is_dma_capable() && musb_ep->dma) {
1326 struct dma_controller *c = musb->dma_controller;
1327
1328 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1329 if (c->channel_abort)
1330 status = c->channel_abort(musb_ep->dma);
1331 else
1332 status = -EBUSY;
1333 if (status == 0)
1334 musb_g_giveback(musb_ep, request, -ECONNRESET);
1335 } else {
1336 /* NOTE: by sticking to easily tested hardware/driver states,
1337 * we leave counting of in-flight packets imprecise.
1338 */
1339 musb_g_giveback(musb_ep, request, -ECONNRESET);
1340 }
1341
1342done:
1343 spin_unlock_irqrestore(&musb->lock, flags);
1344 return status;
1345}
1346
1347/*
1348 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1349 * data but will queue requests.
1350 *
1351 * exported to ep0 code
1352 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001353static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001354{
1355 struct musb_ep *musb_ep = to_musb_ep(ep);
1356 u8 epnum = musb_ep->current_epnum;
1357 struct musb *musb = musb_ep->musb;
1358 void __iomem *epio = musb->endpoints[epnum].regs;
1359 void __iomem *mbase;
1360 unsigned long flags;
1361 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001362 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001363 int status = 0;
1364
1365 if (!ep)
1366 return -EINVAL;
1367 mbase = musb->mregs;
1368
1369 spin_lock_irqsave(&musb->lock, flags);
1370
1371 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1372 status = -EINVAL;
1373 goto done;
1374 }
1375
1376 musb_ep_select(mbase, epnum);
1377
Felipe Balbiad1adb82011-02-16 12:40:05 +02001378 request = next_request(musb_ep);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001379 if (value) {
1380 if (request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001381 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001382 ep->name);
1383 status = -EAGAIN;
1384 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001385 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001386 /* Cannot portably stall with non-empty FIFO */
1387 if (musb_ep->is_in) {
1388 csr = musb_readw(epio, MUSB_TXCSR);
1389 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001390 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001391 status = -EAGAIN;
1392 goto done;
1393 }
1394 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001395 } else
1396 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001397
1398 /* set/clear the stall and toggle bits */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001399 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
Felipe Balbi550a7372008-07-24 12:27:36 +03001400 if (musb_ep->is_in) {
1401 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001402 csr |= MUSB_TXCSR_P_WZC_BITS
1403 | MUSB_TXCSR_CLRDATATOG;
1404 if (value)
1405 csr |= MUSB_TXCSR_P_SENDSTALL;
1406 else
1407 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1408 | MUSB_TXCSR_P_SENTSTALL);
1409 csr &= ~MUSB_TXCSR_TXPKTRDY;
1410 musb_writew(epio, MUSB_TXCSR, csr);
1411 } else {
1412 csr = musb_readw(epio, MUSB_RXCSR);
1413 csr |= MUSB_RXCSR_P_WZC_BITS
1414 | MUSB_RXCSR_FLUSHFIFO
1415 | MUSB_RXCSR_CLRDATATOG;
1416 if (value)
1417 csr |= MUSB_RXCSR_P_SENDSTALL;
1418 else
1419 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1420 | MUSB_RXCSR_P_SENTSTALL);
1421 musb_writew(epio, MUSB_RXCSR, csr);
1422 }
1423
Felipe Balbi550a7372008-07-24 12:27:36 +03001424 /* maybe start the first request in the queue */
1425 if (!musb_ep->busy && !value && request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001426 dev_dbg(musb->controller, "restarting the request\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001427 musb_ep_restart(musb, request);
1428 }
1429
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001430done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001431 spin_unlock_irqrestore(&musb->lock, flags);
1432 return status;
1433}
1434
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001435/*
1436 * Sets the halt feature with the clear requests ignored
1437 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001438static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001439{
1440 struct musb_ep *musb_ep = to_musb_ep(ep);
1441
1442 if (!ep)
1443 return -EINVAL;
1444
1445 musb_ep->wedged = 1;
1446
1447 return usb_ep_set_halt(ep);
1448}
1449
Felipe Balbi550a7372008-07-24 12:27:36 +03001450static int musb_gadget_fifo_status(struct usb_ep *ep)
1451{
1452 struct musb_ep *musb_ep = to_musb_ep(ep);
1453 void __iomem *epio = musb_ep->hw_ep->regs;
1454 int retval = -EINVAL;
1455
1456 if (musb_ep->desc && !musb_ep->is_in) {
1457 struct musb *musb = musb_ep->musb;
1458 int epnum = musb_ep->current_epnum;
1459 void __iomem *mbase = musb->mregs;
1460 unsigned long flags;
1461
1462 spin_lock_irqsave(&musb->lock, flags);
1463
1464 musb_ep_select(mbase, epnum);
1465 /* FIXME return zero unless RXPKTRDY is set */
1466 retval = musb_readw(epio, MUSB_RXCOUNT);
1467
1468 spin_unlock_irqrestore(&musb->lock, flags);
1469 }
1470 return retval;
1471}
1472
1473static void musb_gadget_fifo_flush(struct usb_ep *ep)
1474{
1475 struct musb_ep *musb_ep = to_musb_ep(ep);
1476 struct musb *musb = musb_ep->musb;
1477 u8 epnum = musb_ep->current_epnum;
1478 void __iomem *epio = musb->endpoints[epnum].regs;
1479 void __iomem *mbase;
1480 unsigned long flags;
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001481 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03001482
1483 mbase = musb->mregs;
1484
1485 spin_lock_irqsave(&musb->lock, flags);
1486 musb_ep_select(mbase, (u8) epnum);
1487
1488 /* disable interrupts */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001489 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
Felipe Balbi550a7372008-07-24 12:27:36 +03001490
1491 if (musb_ep->is_in) {
1492 csr = musb_readw(epio, MUSB_TXCSR);
1493 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1494 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
Yauheni Kaliuta4858f062011-06-08 17:12:02 +03001495 /*
1496 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1497 * to interrupt current FIFO loading, but not flushing
1498 * the already loaded ones.
1499 */
1500 csr &= ~MUSB_TXCSR_TXPKTRDY;
Felipe Balbi550a7372008-07-24 12:27:36 +03001501 musb_writew(epio, MUSB_TXCSR, csr);
1502 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1503 musb_writew(epio, MUSB_TXCSR, csr);
1504 }
1505 } else {
1506 csr = musb_readw(epio, MUSB_RXCSR);
1507 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1508 musb_writew(epio, MUSB_RXCSR, csr);
1509 musb_writew(epio, MUSB_RXCSR, csr);
1510 }
1511
1512 /* re-enable interrupt */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001513 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001514 spin_unlock_irqrestore(&musb->lock, flags);
1515}
1516
1517static const struct usb_ep_ops musb_ep_ops = {
1518 .enable = musb_gadget_enable,
1519 .disable = musb_gadget_disable,
1520 .alloc_request = musb_alloc_request,
1521 .free_request = musb_free_request,
1522 .queue = musb_gadget_queue,
1523 .dequeue = musb_gadget_dequeue,
1524 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001525 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001526 .fifo_status = musb_gadget_fifo_status,
1527 .fifo_flush = musb_gadget_fifo_flush
1528};
1529
1530/* ----------------------------------------------------------------------- */
1531
1532static int musb_gadget_get_frame(struct usb_gadget *gadget)
1533{
1534 struct musb *musb = gadget_to_musb(gadget);
1535
1536 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1537}
1538
1539static int musb_gadget_wakeup(struct usb_gadget *gadget)
1540{
1541 struct musb *musb = gadget_to_musb(gadget);
1542 void __iomem *mregs = musb->mregs;
1543 unsigned long flags;
1544 int status = -EINVAL;
1545 u8 power, devctl;
1546 int retries;
1547
1548 spin_lock_irqsave(&musb->lock, flags);
1549
Antoine Tenarte47d9252014-10-30 18:41:13 +01001550 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001551 case OTG_STATE_B_PERIPHERAL:
1552 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1553 * that's part of the standard usb 1.1 state machine, and
1554 * doesn't affect OTG transitions.
1555 */
1556 if (musb->may_wakeup && musb->is_suspended)
1557 break;
1558 goto done;
1559 case OTG_STATE_B_IDLE:
1560 /* Start SRP ... OTG not required. */
1561 devctl = musb_readb(mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001562 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001563 devctl |= MUSB_DEVCTL_SESSION;
1564 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1565 devctl = musb_readb(mregs, MUSB_DEVCTL);
1566 retries = 100;
1567 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1568 devctl = musb_readb(mregs, MUSB_DEVCTL);
1569 if (retries-- < 1)
1570 break;
1571 }
1572 retries = 10000;
1573 while (devctl & MUSB_DEVCTL_SESSION) {
1574 devctl = musb_readb(mregs, MUSB_DEVCTL);
1575 if (retries-- < 1)
1576 break;
1577 }
1578
Hema HK86205432011-03-22 16:54:22 +05301579 spin_unlock_irqrestore(&musb->lock, flags);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001580 otg_start_srp(musb->xceiv->otg);
Hema HK86205432011-03-22 16:54:22 +05301581 spin_lock_irqsave(&musb->lock, flags);
1582
Felipe Balbi550a7372008-07-24 12:27:36 +03001583 /* Block idling for at least 1s */
1584 musb_platform_try_idle(musb,
1585 jiffies + msecs_to_jiffies(1 * HZ));
1586
1587 status = 0;
1588 goto done;
1589 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001590 dev_dbg(musb->controller, "Unhandled wake: %s\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01001591 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001592 goto done;
1593 }
1594
1595 status = 0;
1596
1597 power = musb_readb(mregs, MUSB_POWER);
1598 power |= MUSB_POWER_RESUME;
1599 musb_writeb(mregs, MUSB_POWER, power);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001600 dev_dbg(musb->controller, "issue wakeup\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001601
1602 /* FIXME do this next chunk in a timer callback, no udelay */
1603 mdelay(2);
1604
1605 power = musb_readb(mregs, MUSB_POWER);
1606 power &= ~MUSB_POWER_RESUME;
1607 musb_writeb(mregs, MUSB_POWER, power);
1608done:
1609 spin_unlock_irqrestore(&musb->lock, flags);
1610 return status;
1611}
1612
1613static int
1614musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1615{
Peter Chendadac982015-01-28 16:32:41 +08001616 gadget->is_selfpowered = !!is_selfpowered;
Felipe Balbi550a7372008-07-24 12:27:36 +03001617 return 0;
1618}
1619
1620static void musb_pullup(struct musb *musb, int is_on)
1621{
1622 u8 power;
1623
1624 power = musb_readb(musb->mregs, MUSB_POWER);
1625 if (is_on)
1626 power |= MUSB_POWER_SOFTCONN;
1627 else
1628 power &= ~MUSB_POWER_SOFTCONN;
1629
1630 /* FIXME if on, HdrcStart; if off, HdrcStop */
1631
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001632 dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1633 is_on ? "on" : "off");
Felipe Balbi550a7372008-07-24 12:27:36 +03001634 musb_writeb(musb->mregs, MUSB_POWER, power);
1635}
1636
1637#if 0
1638static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1639{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001640 dev_dbg(musb->controller, "<= %s =>\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001641
1642 /*
1643 * FIXME iff driver's softconnect flag is set (as it is during probe,
1644 * though that can clear it), just musb_pullup().
1645 */
1646
1647 return -EINVAL;
1648}
1649#endif
1650
1651static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1652{
1653 struct musb *musb = gadget_to_musb(gadget);
1654
David Brownell84e250f2009-03-31 12:30:04 -07001655 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001656 return -EOPNOTSUPP;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001657 return usb_phy_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001658}
1659
1660static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1661{
1662 struct musb *musb = gadget_to_musb(gadget);
1663 unsigned long flags;
1664
1665 is_on = !!is_on;
1666
John Stultz93e098a2011-07-20 17:09:34 -07001667 pm_runtime_get_sync(musb->controller);
1668
Felipe Balbi550a7372008-07-24 12:27:36 +03001669 /* NOTE: this assumes we are sensing vbus; we'd rather
1670 * not pullup unless the B-session is active.
1671 */
1672 spin_lock_irqsave(&musb->lock, flags);
1673 if (is_on != musb->softconnect) {
1674 musb->softconnect = is_on;
1675 musb_pullup(musb, is_on);
1676 }
1677 spin_unlock_irqrestore(&musb->lock, flags);
John Stultz93e098a2011-07-20 17:09:34 -07001678
1679 pm_runtime_put(musb->controller);
1680
Felipe Balbi550a7372008-07-24 12:27:36 +03001681 return 0;
1682}
1683
Robert Baldyga26b8aa42015-08-06 14:11:15 +02001684#ifdef CONFIG_BLACKFIN
1685static struct usb_ep *musb_match_ep(struct usb_gadget *g,
1686 struct usb_endpoint_descriptor *desc,
1687 struct usb_ss_ep_comp_descriptor *ep_comp)
1688{
1689 struct usb_ep *ep = NULL;
1690
1691 switch (usb_endpoint_type(desc)) {
1692 case USB_ENDPOINT_XFER_ISOC:
1693 case USB_ENDPOINT_XFER_BULK:
1694 if (usb_endpoint_dir_in(desc))
1695 ep = gadget_find_ep_by_name(g, "ep5in");
1696 else
1697 ep = gadget_find_ep_by_name(g, "ep6out");
1698 break;
1699 case USB_ENDPOINT_XFER_INT:
1700 if (usb_endpoint_dir_in(desc))
1701 ep = gadget_find_ep_by_name(g, "ep1in");
1702 else
1703 ep = gadget_find_ep_by_name(g, "ep2out");
1704 break;
1705 default:
1706 }
1707
1708 if (ep && usb_gadget_ep_match_desc(g, ep, desc, ep_comp))
1709 return ep;
1710
1711 return NULL;
1712}
1713#else
1714#define musb_match_ep NULL
1715#endif
1716
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001717static int musb_gadget_start(struct usb_gadget *g,
1718 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001719static int musb_gadget_stop(struct usb_gadget *g);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001720
Felipe Balbi550a7372008-07-24 12:27:36 +03001721static const struct usb_gadget_ops musb_gadget_operations = {
1722 .get_frame = musb_gadget_get_frame,
1723 .wakeup = musb_gadget_wakeup,
1724 .set_selfpowered = musb_gadget_set_self_powered,
1725 /* .vbus_session = musb_gadget_vbus_session, */
1726 .vbus_draw = musb_gadget_vbus_draw,
1727 .pullup = musb_gadget_pullup,
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001728 .udc_start = musb_gadget_start,
1729 .udc_stop = musb_gadget_stop,
Robert Baldyga26b8aa42015-08-06 14:11:15 +02001730 .match_ep = musb_match_ep,
Felipe Balbi550a7372008-07-24 12:27:36 +03001731};
1732
1733/* ----------------------------------------------------------------------- */
1734
1735/* Registration */
1736
1737/* Only this registration code "knows" the rule (from USB standards)
1738 * about there being only one external upstream port. It assumes
1739 * all peripheral ports are external...
1740 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001741
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001742static void
Felipe Balbi550a7372008-07-24 12:27:36 +03001743init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1744{
1745 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1746
1747 memset(ep, 0, sizeof *ep);
1748
1749 ep->current_epnum = epnum;
1750 ep->musb = musb;
1751 ep->hw_ep = hw_ep;
1752 ep->is_in = is_in;
1753
1754 INIT_LIST_HEAD(&ep->req_list);
1755
1756 sprintf(ep->name, "ep%d%s", epnum,
1757 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1758 is_in ? "in" : "out"));
1759 ep->end_point.name = ep->name;
1760 INIT_LIST_HEAD(&ep->end_point.ep_list);
1761 if (!epnum) {
Robert Baldygae117e742013-12-13 12:23:38 +01001762 usb_ep_set_maxpacket_limit(&ep->end_point, 64);
Robert Baldyga85019552015-07-31 16:00:46 +02001763 ep->end_point.caps.type_control = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001764 ep->end_point.ops = &musb_g_ep0_ops;
1765 musb->g.ep0 = &ep->end_point;
1766 } else {
1767 if (is_in)
Robert Baldygae117e742013-12-13 12:23:38 +01001768 usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_tx);
Felipe Balbi550a7372008-07-24 12:27:36 +03001769 else
Robert Baldygae117e742013-12-13 12:23:38 +01001770 usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_rx);
Robert Baldyga85019552015-07-31 16:00:46 +02001771 ep->end_point.caps.type_iso = true;
1772 ep->end_point.caps.type_bulk = true;
1773 ep->end_point.caps.type_int = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001774 ep->end_point.ops = &musb_ep_ops;
1775 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1776 }
Robert Baldyga85019552015-07-31 16:00:46 +02001777
1778 if (!epnum || hw_ep->is_shared_fifo) {
1779 ep->end_point.caps.dir_in = true;
1780 ep->end_point.caps.dir_out = true;
1781 } else if (is_in)
1782 ep->end_point.caps.dir_in = true;
1783 else
1784 ep->end_point.caps.dir_out = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001785}
1786
1787/*
1788 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1789 * to the rest of the driver state.
1790 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001791static inline void musb_g_init_endpoints(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001792{
1793 u8 epnum;
1794 struct musb_hw_ep *hw_ep;
1795 unsigned count = 0;
1796
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001797 /* initialize endpoint list just once */
Felipe Balbi550a7372008-07-24 12:27:36 +03001798 INIT_LIST_HEAD(&(musb->g.ep_list));
1799
1800 for (epnum = 0, hw_ep = musb->endpoints;
1801 epnum < musb->nr_endpoints;
1802 epnum++, hw_ep++) {
1803 if (hw_ep->is_shared_fifo /* || !epnum */) {
1804 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1805 count++;
1806 } else {
1807 if (hw_ep->max_packet_sz_tx) {
1808 init_peripheral_ep(musb, &hw_ep->ep_in,
1809 epnum, 1);
1810 count++;
1811 }
1812 if (hw_ep->max_packet_sz_rx) {
1813 init_peripheral_ep(musb, &hw_ep->ep_out,
1814 epnum, 0);
1815 count++;
1816 }
1817 }
1818 }
1819}
1820
1821/* called once during driver setup to initialize and link into
1822 * the driver model; memory is zeroed.
1823 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001824int musb_gadget_setup(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001825{
1826 int status;
1827
1828 /* REVISIT minor race: if (erroneously) setting up two
1829 * musb peripherals at the same time, only the bus lock
1830 * is probably held.
1831 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001832
1833 musb->g.ops = &musb_gadget_operations;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001834 musb->g.max_speed = USB_SPEED_HIGH;
Felipe Balbi550a7372008-07-24 12:27:36 +03001835 musb->g.speed = USB_SPEED_UNKNOWN;
1836
Bin Liu1374a4302013-09-17 12:43:13 -05001837 MUSB_DEV_MODE(musb);
1838 musb->xceiv->otg->default_a = 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001839 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Bin Liu1374a4302013-09-17 12:43:13 -05001840
Felipe Balbi550a7372008-07-24 12:27:36 +03001841 /* this "gadget" abstracts/virtualizes the controller */
Felipe Balbi550a7372008-07-24 12:27:36 +03001842 musb->g.name = musb_driver_name;
Apelete Seketelifd3923a2013-11-19 23:18:20 +01001843#if IS_ENABLED(CONFIG_USB_MUSB_DUAL_ROLE)
Felipe Balbi032ec492011-11-24 15:46:26 +02001844 musb->g.is_otg = 1;
Apelete Seketelifd3923a2013-11-19 23:18:20 +01001845#elif IS_ENABLED(CONFIG_USB_MUSB_GADGET)
1846 musb->g.is_otg = 0;
1847#endif
Felipe Balbi550a7372008-07-24 12:27:36 +03001848
1849 musb_g_init_endpoints(musb);
1850
1851 musb->is_active = 0;
1852 musb_platform_try_idle(musb, 0);
1853
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001854 status = usb_add_gadget_udc(musb->controller, &musb->g);
1855 if (status)
1856 goto err;
1857
1858 return 0;
1859err:
Sebastian Andrzej Siewior6193d692011-08-10 11:01:57 +02001860 musb->g.dev.parent = NULL;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001861 device_unregister(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001862 return status;
1863}
1864
1865void musb_gadget_cleanup(struct musb *musb)
1866{
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02001867 if (musb->port_mode == MUSB_PORT_MODE_HOST)
1868 return;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001869 usb_del_gadget_udc(&musb->g);
Felipe Balbi550a7372008-07-24 12:27:36 +03001870}
1871
1872/*
1873 * Register the gadget driver. Used by gadget drivers when
1874 * registering themselves with the controller.
1875 *
1876 * -EINVAL something went wrong (not driver)
1877 * -EBUSY another gadget is already using the controller
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001878 * -ENOMEM no memory to perform the operation
Felipe Balbi550a7372008-07-24 12:27:36 +03001879 *
1880 * @param driver the gadget driver
1881 * @return <0 if error, 0 if everything is fine
1882 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001883static int musb_gadget_start(struct usb_gadget *g,
1884 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03001885{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001886 struct musb *musb = gadget_to_musb(g);
Heikki Krogerusd445b6d2012-02-13 13:24:15 +02001887 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001888 unsigned long flags;
Felipe Balbi032ec492011-11-24 15:46:26 +02001889 int retval = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001890
Felipe Balbi032ec492011-11-24 15:46:26 +02001891 if (driver->max_speed < USB_SPEED_HIGH) {
1892 retval = -EINVAL;
1893 goto err;
1894 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001895
Hema HK7acc6192011-02-28 14:19:34 +05301896 pm_runtime_get_sync(musb->controller);
1897
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001898 musb->softconnect = 0;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001899 musb->gadget_driver = driver;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001900
1901 spin_lock_irqsave(&musb->lock, flags);
Greg Kroah-Hartman43e699c2013-10-14 13:06:15 -07001902 musb->is_active = 1;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001903
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001904 otg_set_peripheral(otg, &musb->g);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001905 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001906 spin_unlock_irqrestore(&musb->lock, flags);
1907
Sebastian Andrzej Siewior001dd842013-10-11 10:38:13 +02001908 musb_start(musb);
1909
Felipe Balbi032ec492011-11-24 15:46:26 +02001910 /* REVISIT: funcall to other code, which also
1911 * handles power budgeting ... this way also
1912 * ensures HdrcStart is indirectly called.
1913 */
Grazvydas Ignotasb65ae0f2013-03-24 17:36:55 +02001914 if (musb->xceiv->last_event == USB_EVENT_ID)
1915 musb_platform_set_vbus(musb, 1);
Felipe Balbi032ec492011-11-24 15:46:26 +02001916
Jarkko Nikulacdefce12011-04-29 16:17:35 +03001917 if (musb->xceiv->last_event == USB_EVENT_NONE)
1918 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001919
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001920 return 0;
1921
Felipe Balbi032ec492011-11-24 15:46:26 +02001922err:
Felipe Balbi550a7372008-07-24 12:27:36 +03001923 return retval;
1924}
Felipe Balbi550a7372008-07-24 12:27:36 +03001925
Felipe Balbi550a7372008-07-24 12:27:36 +03001926/*
1927 * Unregister the gadget driver. Used by gadget drivers when
1928 * unregistering themselves from the controller.
1929 *
1930 * @param driver the gadget driver to unregister
1931 */
Felipe Balbi22835b82014-10-17 12:05:12 -05001932static int musb_gadget_stop(struct usb_gadget *g)
Felipe Balbi550a7372008-07-24 12:27:36 +03001933{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001934 struct musb *musb = gadget_to_musb(g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001935 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03001936
Hema HK7acc6192011-02-28 14:19:34 +05301937 if (musb->xceiv->last_event == USB_EVENT_NONE)
1938 pm_runtime_get_sync(musb->controller);
1939
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001940 /*
1941 * REVISIT always use otg_set_peripheral() here too;
Felipe Balbi550a7372008-07-24 12:27:36 +03001942 * this needs to shut down the OTG engine.
1943 */
1944
1945 spin_lock_irqsave(&musb->lock, flags);
1946
Felipe Balbi550a7372008-07-24 12:27:36 +03001947 musb_hnp_stop(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001948
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001949 (void) musb_gadget_vbus_draw(&musb->g, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001950
Antoine Tenarte47d9252014-10-30 18:41:13 +01001951 musb->xceiv->otg->state = OTG_STATE_UNDEFINED;
Felipe Balbid5638fc2015-02-02 17:14:12 -06001952 musb_stop(musb);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001953 otg_set_peripheral(musb->xceiv->otg, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001954
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001955 musb->is_active = 0;
Grazvydas Ignotase21de102013-03-10 02:49:14 +02001956 musb->gadget_driver = NULL;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001957 musb_platform_try_idle(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001958 spin_unlock_irqrestore(&musb->lock, flags);
1959
Felipe Balbi032ec492011-11-24 15:46:26 +02001960 /*
1961 * FIXME we need to be able to register another
1962 * gadget driver here and have everything work;
1963 * that currently misbehaves.
1964 */
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001965
Hema HK7acc6192011-02-28 14:19:34 +05301966 pm_runtime_put(musb->controller);
1967
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001968 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001969}
Felipe Balbi550a7372008-07-24 12:27:36 +03001970
1971/* ----------------------------------------------------------------------- */
1972
1973/* lifecycle operations called through plat_uds.c */
1974
1975void musb_g_resume(struct musb *musb)
1976{
1977 musb->is_suspended = 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001978 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001979 case OTG_STATE_B_IDLE:
1980 break;
1981 case OTG_STATE_B_WAIT_ACON:
1982 case OTG_STATE_B_PERIPHERAL:
1983 musb->is_active = 1;
1984 if (musb->gadget_driver && musb->gadget_driver->resume) {
1985 spin_unlock(&musb->lock);
1986 musb->gadget_driver->resume(&musb->g);
1987 spin_lock(&musb->lock);
1988 }
1989 break;
1990 default:
1991 WARNING("unhandled RESUME transition (%s)\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01001992 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001993 }
1994}
1995
1996/* called when SOF packets stop for 3+ msec */
1997void musb_g_suspend(struct musb *musb)
1998{
1999 u8 devctl;
2000
2001 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002002 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002003
Antoine Tenarte47d9252014-10-30 18:41:13 +01002004 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002005 case OTG_STATE_B_IDLE:
2006 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
Antoine Tenarte47d9252014-10-30 18:41:13 +01002007 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002008 break;
2009 case OTG_STATE_B_PERIPHERAL:
2010 musb->is_suspended = 1;
2011 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2012 spin_unlock(&musb->lock);
2013 musb->gadget_driver->suspend(&musb->g);
2014 spin_lock(&musb->lock);
2015 }
2016 break;
2017 default:
2018 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2019 * A_PERIPHERAL may need care too
2020 */
2021 WARNING("unhandled SUSPEND transition (%s)\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002022 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002023 }
2024}
2025
2026/* Called during SRP */
2027void musb_g_wakeup(struct musb *musb)
2028{
2029 musb_gadget_wakeup(&musb->g);
2030}
2031
2032/* called when VBUS drops below session threshold, and in other cases */
2033void musb_g_disconnect(struct musb *musb)
2034{
2035 void __iomem *mregs = musb->mregs;
2036 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2037
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002038 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002039
2040 /* clear HR */
2041 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2042
2043 /* don't draw vbus until new b-default session */
2044 (void) musb_gadget_vbus_draw(&musb->g, 0);
2045
2046 musb->g.speed = USB_SPEED_UNKNOWN;
2047 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2048 spin_unlock(&musb->lock);
2049 musb->gadget_driver->disconnect(&musb->g);
2050 spin_lock(&musb->lock);
2051 }
2052
Antoine Tenarte47d9252014-10-30 18:41:13 +01002053 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002054 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002055 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002056 usb_otg_state_string(musb->xceiv->otg->state));
2057 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07002058 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002059 break;
2060 case OTG_STATE_A_PERIPHERAL:
Antoine Tenarte47d9252014-10-30 18:41:13 +01002061 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
David Brownellab983f2a2009-03-31 12:35:09 -07002062 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002063 break;
2064 case OTG_STATE_B_WAIT_ACON:
2065 case OTG_STATE_B_HOST:
Felipe Balbi550a7372008-07-24 12:27:36 +03002066 case OTG_STATE_B_PERIPHERAL:
2067 case OTG_STATE_B_IDLE:
Antoine Tenarte47d9252014-10-30 18:41:13 +01002068 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002069 break;
2070 case OTG_STATE_B_SRP_INIT:
2071 break;
2072 }
2073
2074 musb->is_active = 0;
2075}
2076
2077void musb_g_reset(struct musb *musb)
2078__releases(musb->lock)
2079__acquires(musb->lock)
2080{
2081 void __iomem *mbase = musb->mregs;
2082 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2083 u8 power;
2084
Sebastian Andrzej Siewior515ba292012-10-30 19:52:24 +01002085 dev_dbg(musb->controller, "<== %s driver '%s'\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002086 (devctl & MUSB_DEVCTL_BDEVICE)
2087 ? "B-Device" : "A-Device",
Felipe Balbi550a7372008-07-24 12:27:36 +03002088 musb->gadget_driver
2089 ? musb->gadget_driver->driver.name
2090 : NULL
2091 );
2092
Felipe Balbi1189f7f2014-11-06 14:27:54 +08002093 /* report reset, if we didn't already (flushing EP state) */
2094 if (musb->gadget_driver && musb->g.speed != USB_SPEED_UNKNOWN) {
2095 spin_unlock(&musb->lock);
2096 usb_gadget_udc_reset(&musb->g, musb->gadget_driver);
2097 spin_lock(&musb->lock);
2098 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002099
2100 /* clear HR */
2101 else if (devctl & MUSB_DEVCTL_HR)
2102 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2103
2104
2105 /* what speed did we negotiate? */
2106 power = musb_readb(mbase, MUSB_POWER);
2107 musb->g.speed = (power & MUSB_POWER_HSMODE)
2108 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2109
2110 /* start in USB_STATE_DEFAULT */
2111 musb->is_active = 1;
2112 musb->is_suspended = 0;
2113 MUSB_DEV_MODE(musb);
2114 musb->address = 0;
2115 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2116
2117 musb->may_wakeup = 0;
2118 musb->g.b_hnp_enable = 0;
2119 musb->g.a_alt_hnp_support = 0;
2120 musb->g.a_hnp_support = 0;
Robert Baldygaca1023c2015-07-28 07:20:00 +02002121 musb->g.quirk_zlp_not_supp = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002122
2123 /* Normal reset, as B-Device;
2124 * or else after HNP, as A-Device
2125 */
Apelete Seketeli23db9fd2013-12-19 21:42:27 +01002126 if (!musb->g.is_otg) {
2127 /* USB device controllers that are not OTG compatible
2128 * may not have DEVCTL register in silicon.
2129 * In that case, do not rely on devctl for setting
2130 * peripheral mode.
2131 */
Antoine Tenarte47d9252014-10-30 18:41:13 +01002132 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Apelete Seketeli23db9fd2013-12-19 21:42:27 +01002133 musb->g.is_a_peripheral = 0;
2134 } else if (devctl & MUSB_DEVCTL_BDEVICE) {
Antoine Tenarte47d9252014-10-30 18:41:13 +01002135 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002136 musb->g.is_a_peripheral = 0;
Felipe Balbi032ec492011-11-24 15:46:26 +02002137 } else {
Antoine Tenarte47d9252014-10-30 18:41:13 +01002138 musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002139 musb->g.is_a_peripheral = 1;
Felipe Balbi032ec492011-11-24 15:46:26 +02002140 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002141
2142 /* start with default limits on VBUS power draw */
Felipe Balbi032ec492011-11-24 15:46:26 +02002143 (void) musb_gadget_vbus_draw(&musb->g, 8);
Felipe Balbi550a7372008-07-24 12:27:36 +03002144}