blob: e8408883ab0d3e5fc002c9d1a4367b0978fe0b88 [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) {
79 request->request.dma = dma_map_single(
80 musb->controller,
81 request->request.buf,
82 request->request.length,
83 request->tx
84 ? DMA_TO_DEVICE
85 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010086 request->map_state = MUSB_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -060087 } else {
88 dma_sync_single_for_device(musb->controller,
89 request->request.dma,
90 request->request.length,
91 request->tx
92 ? DMA_TO_DEVICE
93 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010094 request->map_state = PRE_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -060095 }
96}
97
98/* Unmap the buffer from dma and maps it back to cpu */
99static inline void unmap_dma_buffer(struct musb_request *request,
100 struct musb *musb)
101{
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100102 if (!is_buffer_mapped(request))
103 return;
104
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600105 if (request->request.dma == DMA_ADDR_INVALID) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300106 dev_vdbg(musb->controller,
107 "not unmapping a never mapped buffer\n");
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600108 return;
109 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100110 if (request->map_state == MUSB_MAPPED) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600111 dma_unmap_single(musb->controller,
112 request->request.dma,
113 request->request.length,
114 request->tx
115 ? DMA_TO_DEVICE
116 : DMA_FROM_DEVICE);
117 request->request.dma = DMA_ADDR_INVALID;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100118 } else { /* PRE_MAPPED */
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600119 dma_sync_single_for_cpu(musb->controller,
120 request->request.dma,
121 request->request.length,
122 request->tx
123 ? DMA_TO_DEVICE
124 : DMA_FROM_DEVICE);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600125 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100126 request->map_state = UN_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600127}
128
Felipe Balbi550a7372008-07-24 12:27:36 +0300129/*
130 * Immediately complete a request.
131 *
132 * @param request the request to complete
133 * @param status the status to complete the request with
134 * Context: controller locked, IRQs blocked.
135 */
136void musb_g_giveback(
137 struct musb_ep *ep,
138 struct usb_request *request,
139 int status)
140__releases(ep->musb->lock)
141__acquires(ep->musb->lock)
142{
143 struct musb_request *req;
144 struct musb *musb;
145 int busy = ep->busy;
146
147 req = to_musb_request(request);
148
Felipe Balbiad1adb82011-02-16 12:40:05 +0200149 list_del(&req->list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300150 if (req->request.status == -EINPROGRESS)
151 req->request.status = status;
152 musb = req->musb;
153
154 ep->busy = 1;
155 spin_unlock(&musb->lock);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100156 unmap_dma_buffer(req, musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300157 if (request->status == 0)
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300158 dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300159 ep->end_point.name, request,
160 req->request.actual, req->request.length);
161 else
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300162 dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300163 ep->end_point.name, request,
164 req->request.actual, req->request.length,
165 request->status);
166 req->request.complete(&req->ep->end_point, &req->request);
167 spin_lock(&musb->lock);
168 ep->busy = busy;
169}
170
171/* ----------------------------------------------------------------------- */
172
173/*
174 * Abort requests queued to an endpoint using the status. Synchronous.
175 * caller locked controller and blocked irqs, and selected this ep.
176 */
177static void nuke(struct musb_ep *ep, const int status)
178{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300179 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300180 struct musb_request *req = NULL;
181 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
182
183 ep->busy = 1;
184
185 if (is_dma_capable() && ep->dma) {
186 struct dma_controller *c = ep->musb->dma_controller;
187 int value;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700188
Felipe Balbi550a7372008-07-24 12:27:36 +0300189 if (ep->is_in) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700190 /*
191 * The programming guide says that we must not clear
192 * the DMAMODE bit before DMAENAB, so we only
193 * clear it in the second write...
194 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300195 musb_writew(epio, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700196 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
Felipe Balbi550a7372008-07-24 12:27:36 +0300197 musb_writew(epio, MUSB_TXCSR,
198 0 | MUSB_TXCSR_FLUSHFIFO);
199 } else {
200 musb_writew(epio, MUSB_RXCSR,
201 0 | MUSB_RXCSR_FLUSHFIFO);
202 musb_writew(epio, MUSB_RXCSR,
203 0 | MUSB_RXCSR_FLUSHFIFO);
204 }
205
206 value = c->channel_abort(ep->dma);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300207 dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
208 ep->name, value);
Felipe Balbi550a7372008-07-24 12:27:36 +0300209 c->channel_release(ep->dma);
210 ep->dma = NULL;
211 }
212
Felipe Balbiad1adb82011-02-16 12:40:05 +0200213 while (!list_empty(&ep->req_list)) {
214 req = list_first_entry(&ep->req_list, struct musb_request, list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300215 musb_g_giveback(ep, &req->request, status);
216 }
217}
218
219/* ----------------------------------------------------------------------- */
220
221/* Data transfers - pure PIO, pure DMA, or mixed mode */
222
223/*
224 * This assumes the separate CPPI engine is responding to DMA requests
225 * from the usb core ... sequenced a bit differently from mentor dma.
226 */
227
228static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
229{
230 if (can_bulk_split(musb, ep->type))
231 return ep->hw_ep->max_packet_sz_tx;
232 else
233 return ep->packet_sz;
234}
235
Felipe Balbi550a7372008-07-24 12:27:36 +0300236/*
237 * An endpoint is transmitting data. This can be called either from
238 * the IRQ routine or from ep.queue() to kickstart a request on an
239 * endpoint.
240 *
241 * Context: controller locked, IRQs blocked, endpoint selected
242 */
243static void txstate(struct musb *musb, struct musb_request *req)
244{
245 u8 epnum = req->epnum;
246 struct musb_ep *musb_ep;
247 void __iomem *epio = musb->endpoints[epnum].regs;
248 struct usb_request *request;
249 u16 fifo_count = 0, csr;
250 int use_dma = 0;
251
252 musb_ep = req->ep;
253
Vikram Panditaabf710e2012-05-18 13:48:04 -0700254 /* Check if EP is disabled */
255 if (!musb_ep->desc) {
256 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
257 musb_ep->end_point.name);
258 return;
259 }
260
Felipe Balbi550a7372008-07-24 12:27:36 +0300261 /* we shouldn't get here while DMA is active ... but we do ... */
262 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300263 dev_dbg(musb->controller, "dma pending...\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300264 return;
265 }
266
267 /* read TXCSR before */
268 csr = musb_readw(epio, MUSB_TXCSR);
269
270 request = &req->request;
271 fifo_count = min(max_ep_writesize(musb, musb_ep),
272 (int)(request->length - request->actual));
273
274 if (csr & MUSB_TXCSR_TXPKTRDY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300275 dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300276 musb_ep->end_point.name, csr);
277 return;
278 }
279
280 if (csr & MUSB_TXCSR_P_SENDSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300281 dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300282 musb_ep->end_point.name, csr);
283 return;
284 }
285
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300286 dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300287 epnum, musb_ep->packet_sz, fifo_count,
288 csr);
289
290#ifndef CONFIG_MUSB_PIO_ONLY
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100291 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300292 struct dma_controller *c = musb->dma_controller;
Ming Lei66af83d2010-09-20 10:32:06 +0300293 size_t request_size;
294
295 /* setup DMA, then program endpoint CSR */
296 request_size = min_t(size_t, request->length - request->actual,
297 musb_ep->dma->max_len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300298
Ajay Kumar Guptad17d5352012-07-20 11:07:23 +0530299 use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300300
301 /* MUSB_TXCSR_P_ISO is still set correctly */
302
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100303#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +0300304 {
Anand Gadiyard1043a22009-04-02 12:07:08 -0700305 if (request_size < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300306 musb_ep->dma->desired_mode = 0;
307 else
308 musb_ep->dma->desired_mode = 1;
309
310 use_dma = use_dma && c->channel_program(
311 musb_ep->dma, musb_ep->packet_sz,
312 musb_ep->dma->desired_mode,
Cliff Cai796a83f2009-12-21 21:18:02 -0500313 request->dma + request->actual, request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300314 if (use_dma) {
315 if (musb_ep->dma->desired_mode == 0) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700316 /*
317 * We must not clear the DMAMODE bit
318 * before the DMAENAB bit -- and the
319 * latter doesn't always get cleared
320 * before we get here...
321 */
322 csr &= ~(MUSB_TXCSR_AUTOSET
323 | MUSB_TXCSR_DMAENAB);
324 musb_writew(epio, MUSB_TXCSR, csr
325 | MUSB_TXCSR_P_WZC_BITS);
326 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300327 csr |= (MUSB_TXCSR_DMAENAB |
328 MUSB_TXCSR_MODE);
329 /* against programming guide */
Ming Leif11d8932010-09-24 13:44:04 +0300330 } else {
331 csr |= (MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300332 | MUSB_TXCSR_DMAMODE
333 | MUSB_TXCSR_MODE);
supriya karanthbb3a2ef2012-12-06 11:12:48 +0530334 /*
335 * Enable Autoset according to table
336 * below
337 * bulk_split hb_mult Autoset_Enable
338 * 0 0 Yes(Normal)
339 * 0 >0 No(High BW ISO)
340 * 1 0 Yes(HS bulk)
341 * 1 >0 Yes(FS bulk)
342 */
343 if (!musb_ep->hb_mult ||
344 (musb_ep->hb_mult &&
345 can_bulk_split(musb,
346 musb_ep->type)))
Ming Leif11d8932010-09-24 13:44:04 +0300347 csr |= MUSB_TXCSR_AUTOSET;
348 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300349 csr &= ~MUSB_TXCSR_P_UNDERRUN;
Ming Leif11d8932010-09-24 13:44:04 +0300350
Felipe Balbi550a7372008-07-24 12:27:36 +0300351 musb_writew(epio, MUSB_TXCSR, csr);
352 }
353 }
354
355#elif defined(CONFIG_USB_TI_CPPI_DMA)
356 /* program endpoint CSR first, then setup DMA */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700357 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
Sergei Shtylyov37e3ee92009-03-27 12:53:32 -0700358 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
359 MUSB_TXCSR_MODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300360 musb_writew(epio, MUSB_TXCSR,
361 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
362 | csr);
363
364 /* ensure writebuffer is empty */
365 csr = musb_readw(epio, MUSB_TXCSR);
366
367 /* NOTE host side sets DMAENAB later than this; both are
368 * OK since the transfer dma glue (between CPPI and Mentor
369 * fifos) just tells CPPI it could start. Data only moves
370 * to the USB TX fifo when both fifos are ready.
371 */
372
373 /* "mode" is irrelevant here; handle terminating ZLPs like
374 * PIO does, since the hardware RNDIS mode seems unreliable
375 * except for the last-packet-is-already-short case.
376 */
377 use_dma = use_dma && c->channel_program(
378 musb_ep->dma, musb_ep->packet_sz,
379 0,
Ming Lei66af83d2010-09-20 10:32:06 +0300380 request->dma + request->actual,
381 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300382 if (!use_dma) {
383 c->channel_release(musb_ep->dma);
384 musb_ep->dma = NULL;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700385 csr &= ~MUSB_TXCSR_DMAENAB;
386 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300387 /* invariant: prequest->buf is non-null */
388 }
389#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
390 use_dma = use_dma && c->channel_program(
391 musb_ep->dma, musb_ep->packet_sz,
392 request->zero,
Ming Lei66af83d2010-09-20 10:32:06 +0300393 request->dma + request->actual,
394 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300395#endif
396 }
397#endif
398
399 if (!use_dma) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600400 /*
401 * Unmap the dma buffer back to cpu if dma channel
402 * programming fails
403 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100404 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600405
Felipe Balbi550a7372008-07-24 12:27:36 +0300406 musb_write_fifo(musb_ep->hw_ep, fifo_count,
407 (u8 *) (request->buf + request->actual));
408 request->actual += fifo_count;
409 csr |= MUSB_TXCSR_TXPKTRDY;
410 csr &= ~MUSB_TXCSR_P_UNDERRUN;
411 musb_writew(epio, MUSB_TXCSR, csr);
412 }
413
414 /* host may already have the data when this message shows... */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300415 dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300416 musb_ep->end_point.name, use_dma ? "dma" : "pio",
417 request->actual, request->length,
418 musb_readw(epio, MUSB_TXCSR),
419 fifo_count,
420 musb_readw(epio, MUSB_TXMAXP));
421}
422
423/*
424 * FIFO state update (e.g. data ready).
425 * Called from IRQ, with controller locked.
426 */
427void musb_g_tx(struct musb *musb, u8 epnum)
428{
429 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200430 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300431 struct usb_request *request;
432 u8 __iomem *mbase = musb->mregs;
433 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
434 void __iomem *epio = musb->endpoints[epnum].regs;
435 struct dma_channel *dma;
436
437 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200438 req = next_request(musb_ep);
439 request = &req->request;
Felipe Balbi550a7372008-07-24 12:27:36 +0300440
441 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300442 dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300443
444 dma = is_dma_capable() ? musb_ep->dma : NULL;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300445
446 /*
447 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
448 * probably rates reporting as a host error.
449 */
450 if (csr & MUSB_TXCSR_P_SENTSTALL) {
451 csr |= MUSB_TXCSR_P_WZC_BITS;
452 csr &= ~MUSB_TXCSR_P_SENTSTALL;
453 musb_writew(epio, MUSB_TXCSR, csr);
454 return;
455 }
456
457 if (csr & MUSB_TXCSR_P_UNDERRUN) {
458 /* We NAKed, no big deal... little reason to care. */
459 csr |= MUSB_TXCSR_P_WZC_BITS;
460 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
461 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300462 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
463 epnum, request);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300464 }
465
466 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
467 /*
468 * SHOULD NOT HAPPEN... has with CPPI though, after
469 * changing SENDSTALL (and other cases); harmless?
Felipe Balbi550a7372008-07-24 12:27:36 +0300470 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300471 dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300472 return;
473 }
474
475 if (request) {
476 u8 is_dma = 0;
477
478 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
479 is_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300480 csr |= MUSB_TXCSR_P_WZC_BITS;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300481 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
Mian Yousaf Kaukab100d4a92011-03-15 16:24:24 +0100482 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
Felipe Balbi550a7372008-07-24 12:27:36 +0300483 musb_writew(epio, MUSB_TXCSR, csr);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300484 /* Ensure writebuffer is empty. */
485 csr = musb_readw(epio, MUSB_TXCSR);
486 request->actual += musb_ep->dma->actual_len;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300487 dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300488 epnum, csr, musb_ep->dma->actual_len, request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300489 }
490
Ming Leie7379aa2010-09-24 13:44:14 +0300491 /*
492 * First, maybe a terminating short packet. Some DMA
493 * engines might handle this by themselves.
494 */
495 if ((request->zero && request->length
496 && (request->length % musb_ep->packet_sz == 0)
497 && (request->actual == request->length))
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100498#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
Ming Leie7379aa2010-09-24 13:44:14 +0300499 || (is_dma && (!dma->desired_mode ||
500 (request->actual &
501 (musb_ep->packet_sz - 1))))
Felipe Balbi550a7372008-07-24 12:27:36 +0300502#endif
Ming Leie7379aa2010-09-24 13:44:14 +0300503 ) {
504 /*
505 * On DMA completion, FIFO may not be
506 * available yet...
507 */
508 if (csr & MUSB_TXCSR_TXPKTRDY)
509 return;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300510
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300511 dev_dbg(musb->controller, "sending zero pkt\n");
Ming Leie7379aa2010-09-24 13:44:14 +0300512 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
513 | MUSB_TXCSR_TXPKTRDY);
514 request->zero = 0;
515 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300516
Ming Leie7379aa2010-09-24 13:44:14 +0300517 if (request->actual == request->length) {
518 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530519 /*
520 * In the giveback function the MUSB lock is
521 * released and acquired after sometime. During
522 * this time period the INDEX register could get
523 * changed by the gadget_queue function especially
524 * on SMP systems. Reselect the INDEX to be sure
525 * we are reading/modifying the right registers
526 */
527 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200528 req = musb_ep->desc ? next_request(musb_ep) : NULL;
529 if (!req) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300530 dev_dbg(musb->controller, "%s idle now\n",
Ming Leie7379aa2010-09-24 13:44:14 +0300531 musb_ep->end_point.name);
532 return;
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300533 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300534 }
535
Felipe Balbiad1adb82011-02-16 12:40:05 +0200536 txstate(musb, req);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300537 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300538}
539
540/* ------------------------------------------------------------ */
541
Felipe Balbi550a7372008-07-24 12:27:36 +0300542/*
543 * Context: controller locked, IRQs blocked, endpoint selected
544 */
545static void rxstate(struct musb *musb, struct musb_request *req)
546{
Felipe Balbi550a7372008-07-24 12:27:36 +0300547 const u8 epnum = req->epnum;
548 struct usb_request *request = &req->request;
Ming Leibd2e74d2010-09-20 10:32:01 +0300549 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300550 void __iomem *epio = musb->endpoints[epnum].regs;
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400551 unsigned len = 0;
552 u16 fifo_count;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300553 u16 csr = musb_readw(epio, MUSB_RXCSR);
Ming Leibd2e74d2010-09-20 10:32:01 +0300554 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700555 u8 use_mode_1;
Ming Leibd2e74d2010-09-20 10:32:01 +0300556
557 if (hw_ep->is_shared_fifo)
558 musb_ep = &hw_ep->ep_in;
559 else
560 musb_ep = &hw_ep->ep_out;
561
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400562 fifo_count = musb_ep->packet_sz;
Felipe Balbi550a7372008-07-24 12:27:36 +0300563
Vikram Panditaabf710e2012-05-18 13:48:04 -0700564 /* Check if EP is disabled */
565 if (!musb_ep->desc) {
566 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
567 musb_ep->end_point.name);
568 return;
569 }
570
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300571 /* We shouldn't get here while DMA is active, but we do... */
572 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300573 dev_dbg(musb->controller, "DMA pending...\n");
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300574 return;
575 }
576
577 if (csr & MUSB_RXCSR_P_SENDSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300578 dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300579 musb_ep->end_point.name, csr);
580 return;
581 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300582
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100583 if (is_cppi_enabled() && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300584 struct dma_controller *c = musb->dma_controller;
585 struct dma_channel *channel = musb_ep->dma;
586
587 /* NOTE: CPPI won't actually stop advancing the DMA
588 * queue after short packet transfers, so this is almost
589 * always going to run as IRQ-per-packet DMA so that
590 * faults will be handled correctly.
591 */
592 if (c->channel_program(channel,
593 musb_ep->packet_sz,
594 !request->short_not_ok,
595 request->dma + request->actual,
596 request->length - request->actual)) {
597
598 /* make sure that if an rxpkt arrived after the irq,
599 * the cppi engine will be ready to take it as soon
600 * as DMA is enabled
601 */
602 csr &= ~(MUSB_RXCSR_AUTOCLEAR
603 | MUSB_RXCSR_DMAMODE);
604 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
605 musb_writew(epio, MUSB_RXCSR, csr);
606 return;
607 }
608 }
609
610 if (csr & MUSB_RXCSR_RXPKTRDY) {
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400611 fifo_count = musb_readw(epio, MUSB_RXCOUNT);
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700612
613 /*
Felipe Balbi00a89182012-10-26 09:55:31 +0300614 * Enable Mode 1 on RX transfers only when short_not_ok flag
615 * is set. Currently short_not_ok flag is set only from
616 * file_storage and f_mass_storage drivers
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700617 */
Felipe Balbi00a89182012-10-26 09:55:31 +0300618
619 if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700620 use_mode_1 = 1;
621 else
622 use_mode_1 = 0;
623
Felipe Balbi550a7372008-07-24 12:27:36 +0300624 if (request->actual < request->length) {
625#ifdef CONFIG_USB_INVENTRA_DMA
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100626 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300627 struct dma_controller *c;
628 struct dma_channel *channel;
629 int use_dma = 0;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200630 unsigned int transfer_size;
Felipe Balbi550a7372008-07-24 12:27:36 +0300631
632 c = musb->dma_controller;
633 channel = musb_ep->dma;
634
Felipe Balbi00a89182012-10-26 09:55:31 +0300635 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
636 * mode 0 only. So we do not get endpoint interrupts due to DMA
637 * completion. We only get interrupts from DMA controller.
638 *
639 * We could operate in DMA mode 1 if we knew the size of the tranfer
640 * in advance. For mass storage class, request->length = what the host
641 * sends, so that'd work. But for pretty much everything else,
642 * request->length is routinely more than what the host sends. For
643 * most these gadgets, end of is signified either by a short packet,
644 * or filling the last byte of the buffer. (Sending extra data in
645 * that last pckate should trigger an overflow fault.) But in mode 1,
646 * we don't get DMA completion interrupt for short packets.
647 *
648 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
649 * to get endpoint interrupt on every DMA req, but that didn't seem
650 * to work reliably.
651 *
652 * REVISIT an updated g_file_storage can set req->short_not_ok, which
653 * then becomes usable as a runtime "use mode 1" hint...
654 */
655
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700656 /* Experimental: Mode1 works with mass storage use cases */
657 if (use_mode_1) {
Ming Lei9001d802010-09-25 05:50:43 -0500658 csr |= MUSB_RXCSR_AUTOCLEAR;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700659 musb_writew(epio, MUSB_RXCSR, csr);
660 csr |= MUSB_RXCSR_DMAENAB;
661 musb_writew(epio, MUSB_RXCSR, csr);
662
663 /*
664 * this special sequence (enabling and then
665 * disabling MUSB_RXCSR_DMAMODE) is required
666 * to get DMAReq to activate
667 */
668 musb_writew(epio, MUSB_RXCSR,
669 csr | MUSB_RXCSR_DMAMODE);
670 musb_writew(epio, MUSB_RXCSR, csr);
671
Felipe Balbi37730ec2013-02-06 10:19:15 +0200672 transfer_size = min_t(unsigned int,
673 request->length -
674 request->actual,
Roger Quadros660fa882012-08-07 16:26:32 +0300675 channel->max_len);
676 musb_ep->dma->desired_mode = 1;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700677 } else {
678 if (!musb_ep->hb_mult &&
679 musb_ep->hw_ep->rx_double_buffered)
680 csr |= MUSB_RXCSR_AUTOCLEAR;
681 csr |= MUSB_RXCSR_DMAENAB;
682 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300683
Roger Quadros660fa882012-08-07 16:26:32 +0300684 transfer_size = min(request->length - request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400685 (unsigned)fifo_count);
Roger Quadros660fa882012-08-07 16:26:32 +0300686 musb_ep->dma->desired_mode = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300687 }
688
Roger Quadros660fa882012-08-07 16:26:32 +0300689 use_dma = c->channel_program(
690 channel,
691 musb_ep->packet_sz,
692 channel->desired_mode,
693 request->dma
694 + request->actual,
695 transfer_size);
696
Felipe Balbi550a7372008-07-24 12:27:36 +0300697 if (use_dma)
698 return;
699 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100700#elif defined(CONFIG_USB_UX500_DMA)
701 if ((is_buffer_mapped(req)) &&
702 (request->actual < request->length)) {
703
704 struct dma_controller *c;
705 struct dma_channel *channel;
Felipe Balbi37730ec2013-02-06 10:19:15 +0200706 unsigned int transfer_size = 0;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100707
708 c = musb->dma_controller;
709 channel = musb_ep->dma;
710
711 /* In case first packet is short */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400712 if (fifo_count < musb_ep->packet_sz)
713 transfer_size = fifo_count;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100714 else if (request->short_not_ok)
Felipe Balbi37730ec2013-02-06 10:19:15 +0200715 transfer_size = min_t(unsigned int,
716 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100717 request->actual,
718 channel->max_len);
719 else
Felipe Balbi37730ec2013-02-06 10:19:15 +0200720 transfer_size = min_t(unsigned int,
721 request->length -
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100722 request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400723 (unsigned)fifo_count);
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100724
725 csr &= ~MUSB_RXCSR_DMAMODE;
726 csr |= (MUSB_RXCSR_DMAENAB |
727 MUSB_RXCSR_AUTOCLEAR);
728
729 musb_writew(epio, MUSB_RXCSR, csr);
730
731 if (transfer_size <= musb_ep->packet_sz) {
732 musb_ep->dma->desired_mode = 0;
733 } else {
734 musb_ep->dma->desired_mode = 1;
735 /* Mode must be set after DMAENAB */
736 csr |= MUSB_RXCSR_DMAMODE;
737 musb_writew(epio, MUSB_RXCSR, csr);
738 }
739
740 if (c->channel_program(channel,
741 musb_ep->packet_sz,
742 channel->desired_mode,
743 request->dma
744 + request->actual,
745 transfer_size))
746
747 return;
748 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300749#endif /* Mentor's DMA */
750
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400751 len = request->length - request->actual;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300752 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300753 musb_ep->end_point.name,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400754 fifo_count, len,
Felipe Balbi550a7372008-07-24 12:27:36 +0300755 musb_ep->packet_sz);
756
Felipe Balbic2c96322009-02-21 15:29:42 -0800757 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300758
759#ifdef CONFIG_USB_TUSB_OMAP_DMA
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100760 if (tusb_dma_omap() && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300761 struct dma_controller *c = musb->dma_controller;
762 struct dma_channel *channel = musb_ep->dma;
763 u32 dma_addr = request->dma + request->actual;
764 int ret;
765
766 ret = c->channel_program(channel,
767 musb_ep->packet_sz,
768 channel->desired_mode,
769 dma_addr,
770 fifo_count);
771 if (ret)
772 return;
773 }
774#endif
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600775 /*
776 * Unmap the dma buffer back to cpu if dma channel
777 * programming fails. This buffer is mapped if the
778 * channel allocation is successful
779 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100780 if (is_buffer_mapped(req)) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600781 unmap_dma_buffer(req, musb);
782
Ming Leie75df372010-11-16 23:37:37 +0800783 /*
784 * Clear DMAENAB and AUTOCLEAR for the
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600785 * PIO mode transfer
786 */
Ming Leie75df372010-11-16 23:37:37 +0800787 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600788 musb_writew(epio, MUSB_RXCSR, csr);
789 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300790
791 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
792 (request->buf + request->actual));
793 request->actual += fifo_count;
794
795 /* REVISIT if we left anything in the fifo, flush
796 * it and report -EOVERFLOW
797 */
798
799 /* ack the read! */
800 csr |= MUSB_RXCSR_P_WZC_BITS;
801 csr &= ~MUSB_RXCSR_RXPKTRDY;
802 musb_writew(epio, MUSB_RXCSR, csr);
803 }
804 }
805
806 /* reach the end or short packet detected */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400807 if (request->actual == request->length ||
808 fifo_count < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300809 musb_g_giveback(musb_ep, request, 0);
810}
811
812/*
813 * Data ready for a request; called from IRQ
814 */
815void musb_g_rx(struct musb *musb, u8 epnum)
816{
817 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200818 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300819 struct usb_request *request;
820 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300821 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300822 void __iomem *epio = musb->endpoints[epnum].regs;
823 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300824 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
825
826 if (hw_ep->is_shared_fifo)
827 musb_ep = &hw_ep->ep_in;
828 else
829 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300830
831 musb_ep_select(mbase, epnum);
832
Felipe Balbiad1adb82011-02-16 12:40:05 +0200833 req = next_request(musb_ep);
834 if (!req)
Maulik Mankad0abdc362009-12-22 16:18:19 +0530835 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300836
Felipe Balbiad1adb82011-02-16 12:40:05 +0200837 request = &req->request;
838
Felipe Balbi550a7372008-07-24 12:27:36 +0300839 csr = musb_readw(epio, MUSB_RXCSR);
840 dma = is_dma_capable() ? musb_ep->dma : NULL;
841
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300842 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
Felipe Balbi550a7372008-07-24 12:27:36 +0300843 csr, dma ? " (dma)" : "", request);
844
845 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300846 csr |= MUSB_RXCSR_P_WZC_BITS;
847 csr &= ~MUSB_RXCSR_P_SENTSTALL;
848 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300849 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300850 }
851
852 if (csr & MUSB_RXCSR_P_OVERRUN) {
853 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
854 csr &= ~MUSB_RXCSR_P_OVERRUN;
855 musb_writew(epio, MUSB_RXCSR, csr);
856
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300857 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
Sergei Shtylyov43467862010-09-24 13:44:12 +0300858 if (request->status == -EINPROGRESS)
Felipe Balbi550a7372008-07-24 12:27:36 +0300859 request->status = -EOVERFLOW;
860 }
861 if (csr & MUSB_RXCSR_INCOMPRX) {
862 /* REVISIT not necessarily an error */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300863 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +0300864 }
865
866 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
867 /* "should not happen"; likely RXPKTRDY pending for DMA */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300868 dev_dbg(musb->controller, "%s busy, csr %04x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300869 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300870 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300871 }
872
873 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
874 csr &= ~(MUSB_RXCSR_AUTOCLEAR
875 | MUSB_RXCSR_DMAENAB
876 | MUSB_RXCSR_DMAMODE);
877 musb_writew(epio, MUSB_RXCSR,
878 MUSB_RXCSR_P_WZC_BITS | csr);
879
880 request->actual += musb_ep->dma->actual_len;
881
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300882 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300883 epnum, csr,
884 musb_readw(epio, MUSB_RXCSR),
885 musb_ep->dma->actual_len, request);
886
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100887#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
888 defined(CONFIG_USB_UX500_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +0300889 /* Autoclear doesn't clear RxPktRdy for short packets */
Ming Lei9001d802010-09-25 05:50:43 -0500890 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
Felipe Balbi550a7372008-07-24 12:27:36 +0300891 || (dma->actual_len
892 & (musb_ep->packet_sz - 1))) {
893 /* ack the read! */
894 csr &= ~MUSB_RXCSR_RXPKTRDY;
895 musb_writew(epio, MUSB_RXCSR, csr);
896 }
897
898 /* incomplete, and not short? wait for next IN packet */
899 if ((request->actual < request->length)
900 && (musb_ep->dma->actual_len
Ming Lei9001d802010-09-25 05:50:43 -0500901 == musb_ep->packet_sz)) {
902 /* In double buffer case, continue to unload fifo if
903 * there is Rx packet in FIFO.
904 **/
905 csr = musb_readw(epio, MUSB_RXCSR);
906 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
907 hw_ep->rx_double_buffered)
908 goto exit;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300909 return;
Ming Lei9001d802010-09-25 05:50:43 -0500910 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300911#endif
912 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530913 /*
914 * In the giveback function the MUSB lock is
915 * released and acquired after sometime. During
916 * this time period the INDEX register could get
917 * changed by the gadget_queue function especially
918 * on SMP systems. Reselect the INDEX to be sure
919 * we are reading/modifying the right registers
920 */
921 musb_ep_select(mbase, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300922
Felipe Balbiad1adb82011-02-16 12:40:05 +0200923 req = next_request(musb_ep);
924 if (!req)
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300925 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300926 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100927#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
928 defined(CONFIG_USB_UX500_DMA)
Ming Lei9001d802010-09-25 05:50:43 -0500929exit:
Ajay Kumar Guptabb324b02010-11-22 14:22:41 +0530930#endif
Sergei Shtylyov43467862010-09-24 13:44:12 +0300931 /* Analyze request */
Felipe Balbiad1adb82011-02-16 12:40:05 +0200932 rxstate(musb, req);
Felipe Balbi550a7372008-07-24 12:27:36 +0300933}
934
935/* ------------------------------------------------------------ */
936
937static int musb_gadget_enable(struct usb_ep *ep,
938 const struct usb_endpoint_descriptor *desc)
939{
940 unsigned long flags;
941 struct musb_ep *musb_ep;
942 struct musb_hw_ep *hw_ep;
943 void __iomem *regs;
944 struct musb *musb;
945 void __iomem *mbase;
946 u8 epnum;
947 u16 csr;
948 unsigned tmp;
949 int status = -EINVAL;
950
951 if (!ep || !desc)
952 return -EINVAL;
953
954 musb_ep = to_musb_ep(ep);
955 hw_ep = musb_ep->hw_ep;
956 regs = hw_ep->regs;
957 musb = musb_ep->musb;
958 mbase = musb->mregs;
959 epnum = musb_ep->current_epnum;
960
961 spin_lock_irqsave(&musb->lock, flags);
962
963 if (musb_ep->desc) {
964 status = -EBUSY;
965 goto fail;
966 }
Julia Lawall96bcd092009-01-24 17:57:24 -0800967 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +0300968
969 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -0800970 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300971 goto fail;
972
973 /* REVISIT this rules out high bandwidth periodic transfers */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700974 tmp = usb_endpoint_maxp(desc);
Ming Leif11d8932010-09-24 13:44:04 +0300975 if (tmp & ~0x07ff) {
976 int ok;
977
978 if (usb_endpoint_dir_in(desc))
979 ok = musb->hb_iso_tx;
980 else
981 ok = musb->hb_iso_rx;
982
983 if (!ok) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300984 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
Ming Leif11d8932010-09-24 13:44:04 +0300985 goto fail;
986 }
987 musb_ep->hb_mult = (tmp >> 11) & 3;
988 } else {
989 musb_ep->hb_mult = 0;
990 }
991
992 musb_ep->packet_sz = tmp & 0x7ff;
993 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300994
995 /* enable the interrupts for the endpoint, set the endpoint
996 * packet size (or fail), set the mode, clear the fifo
997 */
998 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -0800999 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001000
1001 if (hw_ep->is_shared_fifo)
1002 musb_ep->is_in = 1;
1003 if (!musb_ep->is_in)
1004 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001005
1006 if (tmp > hw_ep->max_packet_sz_tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001007 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001008 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001009 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001010
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001011 musb->intrtxe |= (1 << epnum);
1012 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001013
1014 /* REVISIT if can_bulk_split(), use by updating "tmp";
1015 * likewise high bandwidth periodic tx
1016 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001017 /* Set TXMAXP with the FIFO size of the endpoint
Ming Lei31c99092010-10-19 19:08:25 -05001018 * to disable double buffering mode.
Cliff Cai9f445cb2010-01-28 20:44:18 -05001019 */
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301020 if (musb->double_buffer_not_ok) {
Felipe Balbi06624812011-01-21 13:39:20 +08001021 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301022 } else {
1023 if (can_bulk_split(musb, musb_ep->type))
1024 musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
1025 musb_ep->packet_sz) - 1;
Felipe Balbi06624812011-01-21 13:39:20 +08001026 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1027 | (musb_ep->hb_mult << 11));
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301028 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001029
1030 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1031 if (musb_readw(regs, MUSB_TXCSR)
1032 & MUSB_TXCSR_FIFONOTEMPTY)
1033 csr |= MUSB_TXCSR_FLUSHFIFO;
1034 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1035 csr |= MUSB_TXCSR_P_ISO;
1036
1037 /* set twice in case of double buffering */
1038 musb_writew(regs, MUSB_TXCSR, csr);
1039 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1040 musb_writew(regs, MUSB_TXCSR, csr);
1041
1042 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +03001043
1044 if (hw_ep->is_shared_fifo)
1045 musb_ep->is_in = 0;
1046 if (musb_ep->is_in)
1047 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001048
1049 if (tmp > hw_ep->max_packet_sz_rx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001050 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001051 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001052 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001053
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001054 musb->intrrxe |= (1 << epnum);
1055 musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001056
1057 /* REVISIT if can_bulk_combine() use by updating "tmp"
1058 * likewise high bandwidth periodic rx
1059 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001060 /* Set RXMAXP with the FIFO size of the endpoint
1061 * to disable double buffering mode.
1062 */
Felipe Balbi06624812011-01-21 13:39:20 +08001063 if (musb->double_buffer_not_ok)
1064 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1065 else
1066 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1067 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001068
1069 /* force shared fifo to OUT-only mode */
1070 if (hw_ep->is_shared_fifo) {
1071 csr = musb_readw(regs, MUSB_TXCSR);
1072 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1073 musb_writew(regs, MUSB_TXCSR, csr);
1074 }
1075
1076 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1077 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1078 csr |= MUSB_RXCSR_P_ISO;
1079 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1080 csr |= MUSB_RXCSR_DISNYET;
1081
1082 /* set twice in case of double buffering */
1083 musb_writew(regs, MUSB_RXCSR, csr);
1084 musb_writew(regs, MUSB_RXCSR, csr);
1085 }
1086
1087 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1088 * for some reason you run out of channels here.
1089 */
1090 if (is_dma_capable() && musb->dma_controller) {
1091 struct dma_controller *c = musb->dma_controller;
1092
1093 musb_ep->dma = c->channel_alloc(c, hw_ep,
1094 (desc->bEndpointAddress & USB_DIR_IN));
1095 } else
1096 musb_ep->dma = NULL;
1097
1098 musb_ep->desc = desc;
1099 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001100 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001101 status = 0;
1102
1103 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1104 musb_driver_name, musb_ep->end_point.name,
1105 ({ char *s; switch (musb_ep->type) {
1106 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1107 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1108 default: s = "iso"; break;
1109 }; s; }),
1110 musb_ep->is_in ? "IN" : "OUT",
1111 musb_ep->dma ? "dma, " : "",
1112 musb_ep->packet_sz);
1113
1114 schedule_work(&musb->irq_work);
1115
1116fail:
1117 spin_unlock_irqrestore(&musb->lock, flags);
1118 return status;
1119}
1120
1121/*
1122 * Disable an endpoint flushing all requests queued.
1123 */
1124static int musb_gadget_disable(struct usb_ep *ep)
1125{
1126 unsigned long flags;
1127 struct musb *musb;
1128 u8 epnum;
1129 struct musb_ep *musb_ep;
1130 void __iomem *epio;
1131 int status = 0;
1132
1133 musb_ep = to_musb_ep(ep);
1134 musb = musb_ep->musb;
1135 epnum = musb_ep->current_epnum;
1136 epio = musb->endpoints[epnum].regs;
1137
1138 spin_lock_irqsave(&musb->lock, flags);
1139 musb_ep_select(musb->mregs, epnum);
1140
1141 /* zero the endpoint sizes */
1142 if (musb_ep->is_in) {
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001143 musb->intrtxe &= ~(1 << epnum);
1144 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001145 musb_writew(epio, MUSB_TXMAXP, 0);
1146 } else {
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001147 musb->intrrxe &= ~(1 << epnum);
1148 musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001149 musb_writew(epio, MUSB_RXMAXP, 0);
1150 }
1151
1152 musb_ep->desc = NULL;
Grazvydas Ignotas08f75bf2012-05-26 00:21:33 +03001153 musb_ep->end_point.desc = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001154
1155 /* abort all pending DMA and requests */
1156 nuke(musb_ep, -ESHUTDOWN);
1157
1158 schedule_work(&musb->irq_work);
1159
1160 spin_unlock_irqrestore(&(musb->lock), flags);
1161
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001162 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001163
1164 return status;
1165}
1166
1167/*
1168 * Allocate a request for an endpoint.
1169 * Reused by ep0 code.
1170 */
1171struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1172{
1173 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001174 struct musb *musb = musb_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03001175 struct musb_request *request = NULL;
1176
1177 request = kzalloc(sizeof *request, gfp_flags);
Felipe Balbi0607f862010-12-01 11:03:54 +02001178 if (!request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001179 dev_dbg(musb->controller, "not enough memory\n");
Felipe Balbi0607f862010-12-01 11:03:54 +02001180 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001181 }
1182
Felipe Balbi0607f862010-12-01 11:03:54 +02001183 request->request.dma = DMA_ADDR_INVALID;
1184 request->epnum = musb_ep->current_epnum;
1185 request->ep = musb_ep;
1186
Felipe Balbi550a7372008-07-24 12:27:36 +03001187 return &request->request;
1188}
1189
1190/*
1191 * Free a request
1192 * Reused by ep0 code.
1193 */
1194void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1195{
1196 kfree(to_musb_request(req));
1197}
1198
1199static LIST_HEAD(buffers);
1200
1201struct free_record {
1202 struct list_head list;
1203 struct device *dev;
1204 unsigned bytes;
1205 dma_addr_t dma;
1206};
1207
1208/*
1209 * Context: controller locked, IRQs blocked.
1210 */
Sergei Shtylyova666e3e2010-09-11 13:23:12 -05001211void musb_ep_restart(struct musb *musb, struct musb_request *req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001212{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001213 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001214 req->tx ? "TX/IN" : "RX/OUT",
1215 &req->request, req->request.length, req->epnum);
1216
1217 musb_ep_select(musb->mregs, req->epnum);
1218 if (req->tx)
1219 txstate(musb, req);
1220 else
1221 rxstate(musb, req);
1222}
1223
1224static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1225 gfp_t gfp_flags)
1226{
1227 struct musb_ep *musb_ep;
1228 struct musb_request *request;
1229 struct musb *musb;
1230 int status = 0;
1231 unsigned long lockflags;
1232
1233 if (!ep || !req)
1234 return -EINVAL;
1235 if (!req->buf)
1236 return -ENODATA;
1237
1238 musb_ep = to_musb_ep(ep);
1239 musb = musb_ep->musb;
1240
1241 request = to_musb_request(req);
1242 request->musb = musb;
1243
1244 if (request->ep != musb_ep)
1245 return -EINVAL;
1246
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001247 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
Felipe Balbi550a7372008-07-24 12:27:36 +03001248
1249 /* request is mine now... */
1250 request->request.actual = 0;
1251 request->request.status = -EINPROGRESS;
1252 request->epnum = musb_ep->current_epnum;
1253 request->tx = musb_ep->is_in;
1254
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +01001255 map_dma_buffer(request, musb, musb_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001256
1257 spin_lock_irqsave(&musb->lock, lockflags);
1258
1259 /* don't queue if the ep is down */
1260 if (!musb_ep->desc) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001261 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001262 req, ep->name, "disabled");
1263 status = -ESHUTDOWN;
1264 goto cleanup;
1265 }
1266
1267 /* add request to the list */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001268 list_add_tail(&request->list, &musb_ep->req_list);
Felipe Balbi550a7372008-07-24 12:27:36 +03001269
1270 /* it this is the head of the queue, start i/o ... */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001271 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
Felipe Balbi550a7372008-07-24 12:27:36 +03001272 musb_ep_restart(musb, request);
1273
1274cleanup:
1275 spin_unlock_irqrestore(&musb->lock, lockflags);
1276 return status;
1277}
1278
1279static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1280{
1281 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001282 struct musb_request *req = to_musb_request(request);
1283 struct musb_request *r;
Felipe Balbi550a7372008-07-24 12:27:36 +03001284 unsigned long flags;
1285 int status = 0;
1286 struct musb *musb = musb_ep->musb;
1287
1288 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1289 return -EINVAL;
1290
1291 spin_lock_irqsave(&musb->lock, flags);
1292
1293 list_for_each_entry(r, &musb_ep->req_list, list) {
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001294 if (r == req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001295 break;
1296 }
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001297 if (r != req) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001298 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001299 status = -EINVAL;
1300 goto done;
1301 }
1302
1303 /* if the hardware doesn't have the request, easy ... */
Felipe Balbi3d5ad132011-03-22 11:38:49 +02001304 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
Felipe Balbi550a7372008-07-24 12:27:36 +03001305 musb_g_giveback(musb_ep, request, -ECONNRESET);
1306
1307 /* ... else abort the dma transfer ... */
1308 else if (is_dma_capable() && musb_ep->dma) {
1309 struct dma_controller *c = musb->dma_controller;
1310
1311 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1312 if (c->channel_abort)
1313 status = c->channel_abort(musb_ep->dma);
1314 else
1315 status = -EBUSY;
1316 if (status == 0)
1317 musb_g_giveback(musb_ep, request, -ECONNRESET);
1318 } else {
1319 /* NOTE: by sticking to easily tested hardware/driver states,
1320 * we leave counting of in-flight packets imprecise.
1321 */
1322 musb_g_giveback(musb_ep, request, -ECONNRESET);
1323 }
1324
1325done:
1326 spin_unlock_irqrestore(&musb->lock, flags);
1327 return status;
1328}
1329
1330/*
1331 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1332 * data but will queue requests.
1333 *
1334 * exported to ep0 code
1335 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001336static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001337{
1338 struct musb_ep *musb_ep = to_musb_ep(ep);
1339 u8 epnum = musb_ep->current_epnum;
1340 struct musb *musb = musb_ep->musb;
1341 void __iomem *epio = musb->endpoints[epnum].regs;
1342 void __iomem *mbase;
1343 unsigned long flags;
1344 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001345 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001346 int status = 0;
1347
1348 if (!ep)
1349 return -EINVAL;
1350 mbase = musb->mregs;
1351
1352 spin_lock_irqsave(&musb->lock, flags);
1353
1354 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1355 status = -EINVAL;
1356 goto done;
1357 }
1358
1359 musb_ep_select(mbase, epnum);
1360
Felipe Balbiad1adb82011-02-16 12:40:05 +02001361 request = next_request(musb_ep);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001362 if (value) {
1363 if (request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001364 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001365 ep->name);
1366 status = -EAGAIN;
1367 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001368 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001369 /* Cannot portably stall with non-empty FIFO */
1370 if (musb_ep->is_in) {
1371 csr = musb_readw(epio, MUSB_TXCSR);
1372 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001373 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001374 status = -EAGAIN;
1375 goto done;
1376 }
1377 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001378 } else
1379 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001380
1381 /* set/clear the stall and toggle bits */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001382 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
Felipe Balbi550a7372008-07-24 12:27:36 +03001383 if (musb_ep->is_in) {
1384 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001385 csr |= MUSB_TXCSR_P_WZC_BITS
1386 | MUSB_TXCSR_CLRDATATOG;
1387 if (value)
1388 csr |= MUSB_TXCSR_P_SENDSTALL;
1389 else
1390 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1391 | MUSB_TXCSR_P_SENTSTALL);
1392 csr &= ~MUSB_TXCSR_TXPKTRDY;
1393 musb_writew(epio, MUSB_TXCSR, csr);
1394 } else {
1395 csr = musb_readw(epio, MUSB_RXCSR);
1396 csr |= MUSB_RXCSR_P_WZC_BITS
1397 | MUSB_RXCSR_FLUSHFIFO
1398 | MUSB_RXCSR_CLRDATATOG;
1399 if (value)
1400 csr |= MUSB_RXCSR_P_SENDSTALL;
1401 else
1402 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1403 | MUSB_RXCSR_P_SENTSTALL);
1404 musb_writew(epio, MUSB_RXCSR, csr);
1405 }
1406
Felipe Balbi550a7372008-07-24 12:27:36 +03001407 /* maybe start the first request in the queue */
1408 if (!musb_ep->busy && !value && request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001409 dev_dbg(musb->controller, "restarting the request\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001410 musb_ep_restart(musb, request);
1411 }
1412
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001413done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001414 spin_unlock_irqrestore(&musb->lock, flags);
1415 return status;
1416}
1417
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001418/*
1419 * Sets the halt feature with the clear requests ignored
1420 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001421static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001422{
1423 struct musb_ep *musb_ep = to_musb_ep(ep);
1424
1425 if (!ep)
1426 return -EINVAL;
1427
1428 musb_ep->wedged = 1;
1429
1430 return usb_ep_set_halt(ep);
1431}
1432
Felipe Balbi550a7372008-07-24 12:27:36 +03001433static int musb_gadget_fifo_status(struct usb_ep *ep)
1434{
1435 struct musb_ep *musb_ep = to_musb_ep(ep);
1436 void __iomem *epio = musb_ep->hw_ep->regs;
1437 int retval = -EINVAL;
1438
1439 if (musb_ep->desc && !musb_ep->is_in) {
1440 struct musb *musb = musb_ep->musb;
1441 int epnum = musb_ep->current_epnum;
1442 void __iomem *mbase = musb->mregs;
1443 unsigned long flags;
1444
1445 spin_lock_irqsave(&musb->lock, flags);
1446
1447 musb_ep_select(mbase, epnum);
1448 /* FIXME return zero unless RXPKTRDY is set */
1449 retval = musb_readw(epio, MUSB_RXCOUNT);
1450
1451 spin_unlock_irqrestore(&musb->lock, flags);
1452 }
1453 return retval;
1454}
1455
1456static void musb_gadget_fifo_flush(struct usb_ep *ep)
1457{
1458 struct musb_ep *musb_ep = to_musb_ep(ep);
1459 struct musb *musb = musb_ep->musb;
1460 u8 epnum = musb_ep->current_epnum;
1461 void __iomem *epio = musb->endpoints[epnum].regs;
1462 void __iomem *mbase;
1463 unsigned long flags;
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001464 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03001465
1466 mbase = musb->mregs;
1467
1468 spin_lock_irqsave(&musb->lock, flags);
1469 musb_ep_select(mbase, (u8) epnum);
1470
1471 /* disable interrupts */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001472 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
Felipe Balbi550a7372008-07-24 12:27:36 +03001473
1474 if (musb_ep->is_in) {
1475 csr = musb_readw(epio, MUSB_TXCSR);
1476 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1477 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
Yauheni Kaliuta4858f062011-06-08 17:12:02 +03001478 /*
1479 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1480 * to interrupt current FIFO loading, but not flushing
1481 * the already loaded ones.
1482 */
1483 csr &= ~MUSB_TXCSR_TXPKTRDY;
Felipe Balbi550a7372008-07-24 12:27:36 +03001484 musb_writew(epio, MUSB_TXCSR, csr);
1485 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1486 musb_writew(epio, MUSB_TXCSR, csr);
1487 }
1488 } else {
1489 csr = musb_readw(epio, MUSB_RXCSR);
1490 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1491 musb_writew(epio, MUSB_RXCSR, csr);
1492 musb_writew(epio, MUSB_RXCSR, csr);
1493 }
1494
1495 /* re-enable interrupt */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001496 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001497 spin_unlock_irqrestore(&musb->lock, flags);
1498}
1499
1500static const struct usb_ep_ops musb_ep_ops = {
1501 .enable = musb_gadget_enable,
1502 .disable = musb_gadget_disable,
1503 .alloc_request = musb_alloc_request,
1504 .free_request = musb_free_request,
1505 .queue = musb_gadget_queue,
1506 .dequeue = musb_gadget_dequeue,
1507 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001508 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001509 .fifo_status = musb_gadget_fifo_status,
1510 .fifo_flush = musb_gadget_fifo_flush
1511};
1512
1513/* ----------------------------------------------------------------------- */
1514
1515static int musb_gadget_get_frame(struct usb_gadget *gadget)
1516{
1517 struct musb *musb = gadget_to_musb(gadget);
1518
1519 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1520}
1521
1522static int musb_gadget_wakeup(struct usb_gadget *gadget)
1523{
1524 struct musb *musb = gadget_to_musb(gadget);
1525 void __iomem *mregs = musb->mregs;
1526 unsigned long flags;
1527 int status = -EINVAL;
1528 u8 power, devctl;
1529 int retries;
1530
1531 spin_lock_irqsave(&musb->lock, flags);
1532
David Brownell84e250f2009-03-31 12:30:04 -07001533 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001534 case OTG_STATE_B_PERIPHERAL:
1535 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1536 * that's part of the standard usb 1.1 state machine, and
1537 * doesn't affect OTG transitions.
1538 */
1539 if (musb->may_wakeup && musb->is_suspended)
1540 break;
1541 goto done;
1542 case OTG_STATE_B_IDLE:
1543 /* Start SRP ... OTG not required. */
1544 devctl = musb_readb(mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001545 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001546 devctl |= MUSB_DEVCTL_SESSION;
1547 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1548 devctl = musb_readb(mregs, MUSB_DEVCTL);
1549 retries = 100;
1550 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1551 devctl = musb_readb(mregs, MUSB_DEVCTL);
1552 if (retries-- < 1)
1553 break;
1554 }
1555 retries = 10000;
1556 while (devctl & MUSB_DEVCTL_SESSION) {
1557 devctl = musb_readb(mregs, MUSB_DEVCTL);
1558 if (retries-- < 1)
1559 break;
1560 }
1561
Hema HK86205432011-03-22 16:54:22 +05301562 spin_unlock_irqrestore(&musb->lock, flags);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001563 otg_start_srp(musb->xceiv->otg);
Hema HK86205432011-03-22 16:54:22 +05301564 spin_lock_irqsave(&musb->lock, flags);
1565
Felipe Balbi550a7372008-07-24 12:27:36 +03001566 /* Block idling for at least 1s */
1567 musb_platform_try_idle(musb,
1568 jiffies + msecs_to_jiffies(1 * HZ));
1569
1570 status = 0;
1571 goto done;
1572 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001573 dev_dbg(musb->controller, "Unhandled wake: %s\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +02001574 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001575 goto done;
1576 }
1577
1578 status = 0;
1579
1580 power = musb_readb(mregs, MUSB_POWER);
1581 power |= MUSB_POWER_RESUME;
1582 musb_writeb(mregs, MUSB_POWER, power);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001583 dev_dbg(musb->controller, "issue wakeup\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001584
1585 /* FIXME do this next chunk in a timer callback, no udelay */
1586 mdelay(2);
1587
1588 power = musb_readb(mregs, MUSB_POWER);
1589 power &= ~MUSB_POWER_RESUME;
1590 musb_writeb(mregs, MUSB_POWER, power);
1591done:
1592 spin_unlock_irqrestore(&musb->lock, flags);
1593 return status;
1594}
1595
1596static int
1597musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1598{
1599 struct musb *musb = gadget_to_musb(gadget);
1600
1601 musb->is_self_powered = !!is_selfpowered;
1602 return 0;
1603}
1604
1605static void musb_pullup(struct musb *musb, int is_on)
1606{
1607 u8 power;
1608
1609 power = musb_readb(musb->mregs, MUSB_POWER);
1610 if (is_on)
1611 power |= MUSB_POWER_SOFTCONN;
1612 else
1613 power &= ~MUSB_POWER_SOFTCONN;
1614
1615 /* FIXME if on, HdrcStart; if off, HdrcStop */
1616
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001617 dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1618 is_on ? "on" : "off");
Felipe Balbi550a7372008-07-24 12:27:36 +03001619 musb_writeb(musb->mregs, MUSB_POWER, power);
1620}
1621
1622#if 0
1623static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1624{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001625 dev_dbg(musb->controller, "<= %s =>\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001626
1627 /*
1628 * FIXME iff driver's softconnect flag is set (as it is during probe,
1629 * though that can clear it), just musb_pullup().
1630 */
1631
1632 return -EINVAL;
1633}
1634#endif
1635
1636static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1637{
1638 struct musb *musb = gadget_to_musb(gadget);
1639
David Brownell84e250f2009-03-31 12:30:04 -07001640 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001641 return -EOPNOTSUPP;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001642 return usb_phy_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001643}
1644
1645static 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
John Stultz93e098a2011-07-20 17:09:34 -07001652 pm_runtime_get_sync(musb->controller);
1653
Felipe Balbi550a7372008-07-24 12:27:36 +03001654 /* NOTE: this assumes we are sensing vbus; we'd rather
1655 * not pullup unless the B-session is active.
1656 */
1657 spin_lock_irqsave(&musb->lock, flags);
1658 if (is_on != musb->softconnect) {
1659 musb->softconnect = is_on;
1660 musb_pullup(musb, is_on);
1661 }
1662 spin_unlock_irqrestore(&musb->lock, flags);
John Stultz93e098a2011-07-20 17:09:34 -07001663
1664 pm_runtime_put(musb->controller);
1665
Felipe Balbi550a7372008-07-24 12:27:36 +03001666 return 0;
1667}
1668
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001669static int musb_gadget_start(struct usb_gadget *g,
1670 struct usb_gadget_driver *driver);
1671static int musb_gadget_stop(struct usb_gadget *g,
1672 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001673
Felipe Balbi550a7372008-07-24 12:27:36 +03001674static const struct usb_gadget_ops musb_gadget_operations = {
1675 .get_frame = musb_gadget_get_frame,
1676 .wakeup = musb_gadget_wakeup,
1677 .set_selfpowered = musb_gadget_set_self_powered,
1678 /* .vbus_session = musb_gadget_vbus_session, */
1679 .vbus_draw = musb_gadget_vbus_draw,
1680 .pullup = musb_gadget_pullup,
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001681 .udc_start = musb_gadget_start,
1682 .udc_stop = musb_gadget_stop,
Felipe Balbi550a7372008-07-24 12:27:36 +03001683};
1684
1685/* ----------------------------------------------------------------------- */
1686
1687/* Registration */
1688
1689/* Only this registration code "knows" the rule (from USB standards)
1690 * about there being only one external upstream port. It assumes
1691 * all peripheral ports are external...
1692 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001693
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001694static void
Felipe Balbi550a7372008-07-24 12:27:36 +03001695init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1696{
1697 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1698
1699 memset(ep, 0, sizeof *ep);
1700
1701 ep->current_epnum = epnum;
1702 ep->musb = musb;
1703 ep->hw_ep = hw_ep;
1704 ep->is_in = is_in;
1705
1706 INIT_LIST_HEAD(&ep->req_list);
1707
1708 sprintf(ep->name, "ep%d%s", epnum,
1709 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1710 is_in ? "in" : "out"));
1711 ep->end_point.name = ep->name;
1712 INIT_LIST_HEAD(&ep->end_point.ep_list);
1713 if (!epnum) {
1714 ep->end_point.maxpacket = 64;
1715 ep->end_point.ops = &musb_g_ep0_ops;
1716 musb->g.ep0 = &ep->end_point;
1717 } else {
1718 if (is_in)
1719 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1720 else
1721 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1722 ep->end_point.ops = &musb_ep_ops;
1723 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1724 }
1725}
1726
1727/*
1728 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1729 * to the rest of the driver state.
1730 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001731static inline void musb_g_init_endpoints(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001732{
1733 u8 epnum;
1734 struct musb_hw_ep *hw_ep;
1735 unsigned count = 0;
1736
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001737 /* initialize endpoint list just once */
Felipe Balbi550a7372008-07-24 12:27:36 +03001738 INIT_LIST_HEAD(&(musb->g.ep_list));
1739
1740 for (epnum = 0, hw_ep = musb->endpoints;
1741 epnum < musb->nr_endpoints;
1742 epnum++, hw_ep++) {
1743 if (hw_ep->is_shared_fifo /* || !epnum */) {
1744 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1745 count++;
1746 } else {
1747 if (hw_ep->max_packet_sz_tx) {
1748 init_peripheral_ep(musb, &hw_ep->ep_in,
1749 epnum, 1);
1750 count++;
1751 }
1752 if (hw_ep->max_packet_sz_rx) {
1753 init_peripheral_ep(musb, &hw_ep->ep_out,
1754 epnum, 0);
1755 count++;
1756 }
1757 }
1758 }
1759}
1760
1761/* called once during driver setup to initialize and link into
1762 * the driver model; memory is zeroed.
1763 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001764int musb_gadget_setup(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001765{
1766 int status;
1767
1768 /* REVISIT minor race: if (erroneously) setting up two
1769 * musb peripherals at the same time, only the bus lock
1770 * is probably held.
1771 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001772
1773 musb->g.ops = &musb_gadget_operations;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001774 musb->g.max_speed = USB_SPEED_HIGH;
Felipe Balbi550a7372008-07-24 12:27:36 +03001775 musb->g.speed = USB_SPEED_UNKNOWN;
1776
1777 /* this "gadget" abstracts/virtualizes the controller */
Felipe Balbi550a7372008-07-24 12:27:36 +03001778 musb->g.name = musb_driver_name;
Felipe Balbi032ec492011-11-24 15:46:26 +02001779 musb->g.is_otg = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001780
1781 musb_g_init_endpoints(musb);
1782
1783 musb->is_active = 0;
1784 musb_platform_try_idle(musb, 0);
1785
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001786 status = usb_add_gadget_udc(musb->controller, &musb->g);
1787 if (status)
1788 goto err;
1789
1790 return 0;
1791err:
Sebastian Andrzej Siewior6193d692011-08-10 11:01:57 +02001792 musb->g.dev.parent = NULL;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001793 device_unregister(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001794 return status;
1795}
1796
1797void musb_gadget_cleanup(struct musb *musb)
1798{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001799 usb_del_gadget_udc(&musb->g);
Felipe Balbi550a7372008-07-24 12:27:36 +03001800}
1801
1802/*
1803 * Register the gadget driver. Used by gadget drivers when
1804 * registering themselves with the controller.
1805 *
1806 * -EINVAL something went wrong (not driver)
1807 * -EBUSY another gadget is already using the controller
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001808 * -ENOMEM no memory to perform the operation
Felipe Balbi550a7372008-07-24 12:27:36 +03001809 *
1810 * @param driver the gadget driver
1811 * @return <0 if error, 0 if everything is fine
1812 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001813static int musb_gadget_start(struct usb_gadget *g,
1814 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03001815{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001816 struct musb *musb = gadget_to_musb(g);
Heikki Krogerusd445b6d2012-02-13 13:24:15 +02001817 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi032ec492011-11-24 15:46:26 +02001818 struct usb_hcd *hcd = musb_to_hcd(musb);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001819 unsigned long flags;
Felipe Balbi032ec492011-11-24 15:46:26 +02001820 int retval = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001821
Felipe Balbi032ec492011-11-24 15:46:26 +02001822 if (driver->max_speed < USB_SPEED_HIGH) {
1823 retval = -EINVAL;
1824 goto err;
1825 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001826
Hema HK7acc6192011-02-28 14:19:34 +05301827 pm_runtime_get_sync(musb->controller);
1828
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001829 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03001830
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001831 musb->softconnect = 0;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001832 musb->gadget_driver = driver;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001833
1834 spin_lock_irqsave(&musb->lock, flags);
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001835 musb->is_active = 1;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001836
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001837 otg_set_peripheral(otg, &musb->g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001838 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001839 spin_unlock_irqrestore(&musb->lock, flags);
1840
Felipe Balbi032ec492011-11-24 15:46:26 +02001841 /* REVISIT: funcall to other code, which also
1842 * handles power budgeting ... this way also
1843 * ensures HdrcStart is indirectly called.
1844 */
1845 retval = usb_add_hcd(hcd, 0, 0);
1846 if (retval < 0) {
1847 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
1848 goto err;
Felipe Balbi550a7372008-07-24 12:27:36 +03001849 }
Felipe Balbi032ec492011-11-24 15:46:26 +02001850
1851 if ((musb->xceiv->last_event == USB_EVENT_ID)
1852 && otg->set_vbus)
1853 otg_set_vbus(otg, 1);
1854
1855 hcd->self.uses_pio_for_control = 1;
1856
Jarkko Nikulacdefce12011-04-29 16:17:35 +03001857 if (musb->xceiv->last_event == USB_EVENT_NONE)
1858 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001859
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001860 return 0;
1861
Felipe Balbi032ec492011-11-24 15:46:26 +02001862err:
Felipe Balbi550a7372008-07-24 12:27:36 +03001863 return retval;
1864}
Felipe Balbi550a7372008-07-24 12:27:36 +03001865
1866static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1867{
1868 int i;
1869 struct musb_hw_ep *hw_ep;
1870
1871 /* don't disconnect if it's not connected */
1872 if (musb->g.speed == USB_SPEED_UNKNOWN)
1873 driver = NULL;
1874 else
1875 musb->g.speed = USB_SPEED_UNKNOWN;
1876
1877 /* deactivate the hardware */
1878 if (musb->softconnect) {
1879 musb->softconnect = 0;
1880 musb_pullup(musb, 0);
1881 }
1882 musb_stop(musb);
1883
1884 /* killing any outstanding requests will quiesce the driver;
1885 * then report disconnect
1886 */
1887 if (driver) {
1888 for (i = 0, hw_ep = musb->endpoints;
1889 i < musb->nr_endpoints;
1890 i++, hw_ep++) {
1891 musb_ep_select(musb->mregs, i);
1892 if (hw_ep->is_shared_fifo /* || !epnum */) {
1893 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1894 } else {
1895 if (hw_ep->max_packet_sz_tx)
1896 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1897 if (hw_ep->max_packet_sz_rx)
1898 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1899 }
1900 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001901 }
1902}
1903
1904/*
1905 * Unregister the gadget driver. Used by gadget drivers when
1906 * unregistering themselves from the controller.
1907 *
1908 * @param driver the gadget driver to unregister
1909 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001910static int musb_gadget_stop(struct usb_gadget *g,
1911 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03001912{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001913 struct musb *musb = gadget_to_musb(g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001914 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03001915
Hema HK7acc6192011-02-28 14:19:34 +05301916 if (musb->xceiv->last_event == USB_EVENT_NONE)
1917 pm_runtime_get_sync(musb->controller);
1918
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001919 /*
1920 * REVISIT always use otg_set_peripheral() here too;
Felipe Balbi550a7372008-07-24 12:27:36 +03001921 * this needs to shut down the OTG engine.
1922 */
1923
1924 spin_lock_irqsave(&musb->lock, flags);
1925
Felipe Balbi550a7372008-07-24 12:27:36 +03001926 musb_hnp_stop(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001927
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001928 (void) musb_gadget_vbus_draw(&musb->g, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001929
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001930 musb->xceiv->state = OTG_STATE_UNDEFINED;
1931 stop_activity(musb, driver);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001932 otg_set_peripheral(musb->xceiv->otg, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001933
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001934 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03001935
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001936 musb->is_active = 0;
1937 musb_platform_try_idle(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001938 spin_unlock_irqrestore(&musb->lock, flags);
1939
Felipe Balbi032ec492011-11-24 15:46:26 +02001940 usb_remove_hcd(musb_to_hcd(musb));
1941 /*
1942 * FIXME we need to be able to register another
1943 * gadget driver here and have everything work;
1944 * that currently misbehaves.
1945 */
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001946
Hema HK7acc6192011-02-28 14:19:34 +05301947 pm_runtime_put(musb->controller);
1948
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001949 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001950}
Felipe Balbi550a7372008-07-24 12:27:36 +03001951
1952/* ----------------------------------------------------------------------- */
1953
1954/* lifecycle operations called through plat_uds.c */
1955
1956void musb_g_resume(struct musb *musb)
1957{
1958 musb->is_suspended = 0;
David Brownell84e250f2009-03-31 12:30:04 -07001959 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001960 case OTG_STATE_B_IDLE:
1961 break;
1962 case OTG_STATE_B_WAIT_ACON:
1963 case OTG_STATE_B_PERIPHERAL:
1964 musb->is_active = 1;
1965 if (musb->gadget_driver && musb->gadget_driver->resume) {
1966 spin_unlock(&musb->lock);
1967 musb->gadget_driver->resume(&musb->g);
1968 spin_lock(&musb->lock);
1969 }
1970 break;
1971 default:
1972 WARNING("unhandled RESUME transition (%s)\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +02001973 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001974 }
1975}
1976
1977/* called when SOF packets stop for 3+ msec */
1978void musb_g_suspend(struct musb *musb)
1979{
1980 u8 devctl;
1981
1982 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001983 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001984
David Brownell84e250f2009-03-31 12:30:04 -07001985 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001986 case OTG_STATE_B_IDLE:
1987 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
David Brownell84e250f2009-03-31 12:30:04 -07001988 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001989 break;
1990 case OTG_STATE_B_PERIPHERAL:
1991 musb->is_suspended = 1;
1992 if (musb->gadget_driver && musb->gadget_driver->suspend) {
1993 spin_unlock(&musb->lock);
1994 musb->gadget_driver->suspend(&musb->g);
1995 spin_lock(&musb->lock);
1996 }
1997 break;
1998 default:
1999 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2000 * A_PERIPHERAL may need care too
2001 */
2002 WARNING("unhandled SUSPEND transition (%s)\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +02002003 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002004 }
2005}
2006
2007/* Called during SRP */
2008void musb_g_wakeup(struct musb *musb)
2009{
2010 musb_gadget_wakeup(&musb->g);
2011}
2012
2013/* called when VBUS drops below session threshold, and in other cases */
2014void musb_g_disconnect(struct musb *musb)
2015{
2016 void __iomem *mregs = musb->mregs;
2017 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2018
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002019 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002020
2021 /* clear HR */
2022 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2023
2024 /* don't draw vbus until new b-default session */
2025 (void) musb_gadget_vbus_draw(&musb->g, 0);
2026
2027 musb->g.speed = USB_SPEED_UNKNOWN;
2028 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2029 spin_unlock(&musb->lock);
2030 musb->gadget_driver->disconnect(&musb->g);
2031 spin_lock(&musb->lock);
2032 }
2033
David Brownell84e250f2009-03-31 12:30:04 -07002034 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002035 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002036 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +02002037 usb_otg_state_string(musb->xceiv->state));
David Brownell84e250f2009-03-31 12:30:04 -07002038 musb->xceiv->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07002039 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002040 break;
2041 case OTG_STATE_A_PERIPHERAL:
David Brownell1de00da2009-04-02 10:16:11 -07002042 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
David Brownellab983f2a2009-03-31 12:35:09 -07002043 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002044 break;
2045 case OTG_STATE_B_WAIT_ACON:
2046 case OTG_STATE_B_HOST:
Felipe Balbi550a7372008-07-24 12:27:36 +03002047 case OTG_STATE_B_PERIPHERAL:
2048 case OTG_STATE_B_IDLE:
David Brownell84e250f2009-03-31 12:30:04 -07002049 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002050 break;
2051 case OTG_STATE_B_SRP_INIT:
2052 break;
2053 }
2054
2055 musb->is_active = 0;
2056}
2057
2058void musb_g_reset(struct musb *musb)
2059__releases(musb->lock)
2060__acquires(musb->lock)
2061{
2062 void __iomem *mbase = musb->mregs;
2063 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2064 u8 power;
2065
Sebastian Andrzej Siewior515ba292012-10-30 19:52:24 +01002066 dev_dbg(musb->controller, "<== %s driver '%s'\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002067 (devctl & MUSB_DEVCTL_BDEVICE)
2068 ? "B-Device" : "A-Device",
Felipe Balbi550a7372008-07-24 12:27:36 +03002069 musb->gadget_driver
2070 ? musb->gadget_driver->driver.name
2071 : NULL
2072 );
2073
2074 /* report disconnect, if we didn't already (flushing EP state) */
2075 if (musb->g.speed != USB_SPEED_UNKNOWN)
2076 musb_g_disconnect(musb);
2077
2078 /* clear HR */
2079 else if (devctl & MUSB_DEVCTL_HR)
2080 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2081
2082
2083 /* what speed did we negotiate? */
2084 power = musb_readb(mbase, MUSB_POWER);
2085 musb->g.speed = (power & MUSB_POWER_HSMODE)
2086 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2087
2088 /* start in USB_STATE_DEFAULT */
2089 musb->is_active = 1;
2090 musb->is_suspended = 0;
2091 MUSB_DEV_MODE(musb);
2092 musb->address = 0;
2093 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2094
2095 musb->may_wakeup = 0;
2096 musb->g.b_hnp_enable = 0;
2097 musb->g.a_alt_hnp_support = 0;
2098 musb->g.a_hnp_support = 0;
2099
2100 /* Normal reset, as B-Device;
2101 * or else after HNP, as A-Device
2102 */
2103 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -07002104 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002105 musb->g.is_a_peripheral = 0;
Felipe Balbi032ec492011-11-24 15:46:26 +02002106 } else {
David Brownell84e250f2009-03-31 12:30:04 -07002107 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002108 musb->g.is_a_peripheral = 1;
Felipe Balbi032ec492011-11-24 15:46:26 +02002109 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002110
2111 /* start with default limits on VBUS power draw */
Felipe Balbi032ec492011-11-24 15:46:26 +02002112 (void) musb_gadget_vbus_draw(&musb->g, 8);
Felipe Balbi550a7372008-07-24 12:27:36 +03002113}