blob: b47f66d32b40e14007724c5fdac18611a19b79be [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;
Roger Quadros660fa882012-08-07 16:26:32 +0300630 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
Roger Quadros660fa882012-08-07 16:26:32 +0300672 transfer_size = min(request->length - request->actual,
673 channel->max_len);
674 musb_ep->dma->desired_mode = 1;
675
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700676 } else {
677 if (!musb_ep->hb_mult &&
678 musb_ep->hw_ep->rx_double_buffered)
679 csr |= MUSB_RXCSR_AUTOCLEAR;
680 csr |= MUSB_RXCSR_DMAENAB;
681 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300682
Roger Quadros660fa882012-08-07 16:26:32 +0300683 transfer_size = min(request->length - request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400684 (unsigned)fifo_count);
Roger Quadros660fa882012-08-07 16:26:32 +0300685 musb_ep->dma->desired_mode = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300686 }
687
Roger Quadros660fa882012-08-07 16:26:32 +0300688 use_dma = c->channel_program(
689 channel,
690 musb_ep->packet_sz,
691 channel->desired_mode,
692 request->dma
693 + request->actual,
694 transfer_size);
695
Felipe Balbi550a7372008-07-24 12:27:36 +0300696 if (use_dma)
697 return;
698 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100699#elif defined(CONFIG_USB_UX500_DMA)
700 if ((is_buffer_mapped(req)) &&
701 (request->actual < request->length)) {
702
703 struct dma_controller *c;
704 struct dma_channel *channel;
705 int transfer_size = 0;
706
707 c = musb->dma_controller;
708 channel = musb_ep->dma;
709
710 /* In case first packet is short */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400711 if (fifo_count < musb_ep->packet_sz)
712 transfer_size = fifo_count;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100713 else if (request->short_not_ok)
714 transfer_size = min(request->length -
715 request->actual,
716 channel->max_len);
717 else
718 transfer_size = min(request->length -
719 request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400720 (unsigned)fifo_count);
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100721
722 csr &= ~MUSB_RXCSR_DMAMODE;
723 csr |= (MUSB_RXCSR_DMAENAB |
724 MUSB_RXCSR_AUTOCLEAR);
725
726 musb_writew(epio, MUSB_RXCSR, csr);
727
728 if (transfer_size <= musb_ep->packet_sz) {
729 musb_ep->dma->desired_mode = 0;
730 } else {
731 musb_ep->dma->desired_mode = 1;
732 /* Mode must be set after DMAENAB */
733 csr |= MUSB_RXCSR_DMAMODE;
734 musb_writew(epio, MUSB_RXCSR, csr);
735 }
736
737 if (c->channel_program(channel,
738 musb_ep->packet_sz,
739 channel->desired_mode,
740 request->dma
741 + request->actual,
742 transfer_size))
743
744 return;
745 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300746#endif /* Mentor's DMA */
747
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400748 len = request->length - request->actual;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300749 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300750 musb_ep->end_point.name,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400751 fifo_count, len,
Felipe Balbi550a7372008-07-24 12:27:36 +0300752 musb_ep->packet_sz);
753
Felipe Balbic2c96322009-02-21 15:29:42 -0800754 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300755
756#ifdef CONFIG_USB_TUSB_OMAP_DMA
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100757 if (tusb_dma_omap() && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300758 struct dma_controller *c = musb->dma_controller;
759 struct dma_channel *channel = musb_ep->dma;
760 u32 dma_addr = request->dma + request->actual;
761 int ret;
762
763 ret = c->channel_program(channel,
764 musb_ep->packet_sz,
765 channel->desired_mode,
766 dma_addr,
767 fifo_count);
768 if (ret)
769 return;
770 }
771#endif
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600772 /*
773 * Unmap the dma buffer back to cpu if dma channel
774 * programming fails. This buffer is mapped if the
775 * channel allocation is successful
776 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100777 if (is_buffer_mapped(req)) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600778 unmap_dma_buffer(req, musb);
779
Ming Leie75df372010-11-16 23:37:37 +0800780 /*
781 * Clear DMAENAB and AUTOCLEAR for the
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600782 * PIO mode transfer
783 */
Ming Leie75df372010-11-16 23:37:37 +0800784 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600785 musb_writew(epio, MUSB_RXCSR, csr);
786 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300787
788 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
789 (request->buf + request->actual));
790 request->actual += fifo_count;
791
792 /* REVISIT if we left anything in the fifo, flush
793 * it and report -EOVERFLOW
794 */
795
796 /* ack the read! */
797 csr |= MUSB_RXCSR_P_WZC_BITS;
798 csr &= ~MUSB_RXCSR_RXPKTRDY;
799 musb_writew(epio, MUSB_RXCSR, csr);
800 }
801 }
802
803 /* reach the end or short packet detected */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400804 if (request->actual == request->length ||
805 fifo_count < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300806 musb_g_giveback(musb_ep, request, 0);
807}
808
809/*
810 * Data ready for a request; called from IRQ
811 */
812void musb_g_rx(struct musb *musb, u8 epnum)
813{
814 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200815 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300816 struct usb_request *request;
817 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300818 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300819 void __iomem *epio = musb->endpoints[epnum].regs;
820 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300821 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
822
823 if (hw_ep->is_shared_fifo)
824 musb_ep = &hw_ep->ep_in;
825 else
826 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300827
828 musb_ep_select(mbase, epnum);
829
Felipe Balbiad1adb82011-02-16 12:40:05 +0200830 req = next_request(musb_ep);
831 if (!req)
Maulik Mankad0abdc362009-12-22 16:18:19 +0530832 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300833
Felipe Balbiad1adb82011-02-16 12:40:05 +0200834 request = &req->request;
835
Felipe Balbi550a7372008-07-24 12:27:36 +0300836 csr = musb_readw(epio, MUSB_RXCSR);
837 dma = is_dma_capable() ? musb_ep->dma : NULL;
838
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300839 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
Felipe Balbi550a7372008-07-24 12:27:36 +0300840 csr, dma ? " (dma)" : "", request);
841
842 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300843 csr |= MUSB_RXCSR_P_WZC_BITS;
844 csr &= ~MUSB_RXCSR_P_SENTSTALL;
845 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300846 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300847 }
848
849 if (csr & MUSB_RXCSR_P_OVERRUN) {
850 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
851 csr &= ~MUSB_RXCSR_P_OVERRUN;
852 musb_writew(epio, MUSB_RXCSR, csr);
853
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300854 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
Sergei Shtylyov43467862010-09-24 13:44:12 +0300855 if (request->status == -EINPROGRESS)
Felipe Balbi550a7372008-07-24 12:27:36 +0300856 request->status = -EOVERFLOW;
857 }
858 if (csr & MUSB_RXCSR_INCOMPRX) {
859 /* REVISIT not necessarily an error */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300860 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +0300861 }
862
863 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
864 /* "should not happen"; likely RXPKTRDY pending for DMA */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300865 dev_dbg(musb->controller, "%s busy, csr %04x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300866 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300867 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300868 }
869
870 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
871 csr &= ~(MUSB_RXCSR_AUTOCLEAR
872 | MUSB_RXCSR_DMAENAB
873 | MUSB_RXCSR_DMAMODE);
874 musb_writew(epio, MUSB_RXCSR,
875 MUSB_RXCSR_P_WZC_BITS | csr);
876
877 request->actual += musb_ep->dma->actual_len;
878
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300879 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300880 epnum, csr,
881 musb_readw(epio, MUSB_RXCSR),
882 musb_ep->dma->actual_len, request);
883
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100884#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
885 defined(CONFIG_USB_UX500_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +0300886 /* Autoclear doesn't clear RxPktRdy for short packets */
Ming Lei9001d802010-09-25 05:50:43 -0500887 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
Felipe Balbi550a7372008-07-24 12:27:36 +0300888 || (dma->actual_len
889 & (musb_ep->packet_sz - 1))) {
890 /* ack the read! */
891 csr &= ~MUSB_RXCSR_RXPKTRDY;
892 musb_writew(epio, MUSB_RXCSR, csr);
893 }
894
895 /* incomplete, and not short? wait for next IN packet */
896 if ((request->actual < request->length)
897 && (musb_ep->dma->actual_len
Ming Lei9001d802010-09-25 05:50:43 -0500898 == musb_ep->packet_sz)) {
899 /* In double buffer case, continue to unload fifo if
900 * there is Rx packet in FIFO.
901 **/
902 csr = musb_readw(epio, MUSB_RXCSR);
903 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
904 hw_ep->rx_double_buffered)
905 goto exit;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300906 return;
Ming Lei9001d802010-09-25 05:50:43 -0500907 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300908#endif
909 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530910 /*
911 * In the giveback function the MUSB lock is
912 * released and acquired after sometime. During
913 * this time period the INDEX register could get
914 * changed by the gadget_queue function especially
915 * on SMP systems. Reselect the INDEX to be sure
916 * we are reading/modifying the right registers
917 */
918 musb_ep_select(mbase, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300919
Felipe Balbiad1adb82011-02-16 12:40:05 +0200920 req = next_request(musb_ep);
921 if (!req)
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300922 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300923 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100924#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
925 defined(CONFIG_USB_UX500_DMA)
Ming Lei9001d802010-09-25 05:50:43 -0500926exit:
Ajay Kumar Guptabb324b02010-11-22 14:22:41 +0530927#endif
Sergei Shtylyov43467862010-09-24 13:44:12 +0300928 /* Analyze request */
Felipe Balbiad1adb82011-02-16 12:40:05 +0200929 rxstate(musb, req);
Felipe Balbi550a7372008-07-24 12:27:36 +0300930}
931
932/* ------------------------------------------------------------ */
933
934static int musb_gadget_enable(struct usb_ep *ep,
935 const struct usb_endpoint_descriptor *desc)
936{
937 unsigned long flags;
938 struct musb_ep *musb_ep;
939 struct musb_hw_ep *hw_ep;
940 void __iomem *regs;
941 struct musb *musb;
942 void __iomem *mbase;
943 u8 epnum;
944 u16 csr;
945 unsigned tmp;
946 int status = -EINVAL;
947
948 if (!ep || !desc)
949 return -EINVAL;
950
951 musb_ep = to_musb_ep(ep);
952 hw_ep = musb_ep->hw_ep;
953 regs = hw_ep->regs;
954 musb = musb_ep->musb;
955 mbase = musb->mregs;
956 epnum = musb_ep->current_epnum;
957
958 spin_lock_irqsave(&musb->lock, flags);
959
960 if (musb_ep->desc) {
961 status = -EBUSY;
962 goto fail;
963 }
Julia Lawall96bcd092009-01-24 17:57:24 -0800964 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +0300965
966 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -0800967 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300968 goto fail;
969
970 /* REVISIT this rules out high bandwidth periodic transfers */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700971 tmp = usb_endpoint_maxp(desc);
Ming Leif11d8932010-09-24 13:44:04 +0300972 if (tmp & ~0x07ff) {
973 int ok;
974
975 if (usb_endpoint_dir_in(desc))
976 ok = musb->hb_iso_tx;
977 else
978 ok = musb->hb_iso_rx;
979
980 if (!ok) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300981 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
Ming Leif11d8932010-09-24 13:44:04 +0300982 goto fail;
983 }
984 musb_ep->hb_mult = (tmp >> 11) & 3;
985 } else {
986 musb_ep->hb_mult = 0;
987 }
988
989 musb_ep->packet_sz = tmp & 0x7ff;
990 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300991
992 /* enable the interrupts for the endpoint, set the endpoint
993 * packet size (or fail), set the mode, clear the fifo
994 */
995 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -0800996 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300997
998 if (hw_ep->is_shared_fifo)
999 musb_ep->is_in = 1;
1000 if (!musb_ep->is_in)
1001 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001002
1003 if (tmp > hw_ep->max_packet_sz_tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001004 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001005 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001006 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001007
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001008 musb->intrtxe |= (1 << epnum);
1009 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001010
1011 /* REVISIT if can_bulk_split(), use by updating "tmp";
1012 * likewise high bandwidth periodic tx
1013 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001014 /* Set TXMAXP with the FIFO size of the endpoint
Ming Lei31c99092010-10-19 19:08:25 -05001015 * to disable double buffering mode.
Cliff Cai9f445cb2010-01-28 20:44:18 -05001016 */
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301017 if (musb->double_buffer_not_ok) {
Felipe Balbi06624812011-01-21 13:39:20 +08001018 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301019 } else {
1020 if (can_bulk_split(musb, musb_ep->type))
1021 musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
1022 musb_ep->packet_sz) - 1;
Felipe Balbi06624812011-01-21 13:39:20 +08001023 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1024 | (musb_ep->hb_mult << 11));
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301025 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001026
1027 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1028 if (musb_readw(regs, MUSB_TXCSR)
1029 & MUSB_TXCSR_FIFONOTEMPTY)
1030 csr |= MUSB_TXCSR_FLUSHFIFO;
1031 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1032 csr |= MUSB_TXCSR_P_ISO;
1033
1034 /* set twice in case of double buffering */
1035 musb_writew(regs, MUSB_TXCSR, csr);
1036 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1037 musb_writew(regs, MUSB_TXCSR, csr);
1038
1039 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +03001040
1041 if (hw_ep->is_shared_fifo)
1042 musb_ep->is_in = 0;
1043 if (musb_ep->is_in)
1044 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001045
1046 if (tmp > hw_ep->max_packet_sz_rx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001047 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001048 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001049 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001050
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001051 musb->intrrxe |= (1 << epnum);
1052 musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001053
1054 /* REVISIT if can_bulk_combine() use by updating "tmp"
1055 * likewise high bandwidth periodic rx
1056 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001057 /* Set RXMAXP with the FIFO size of the endpoint
1058 * to disable double buffering mode.
1059 */
Felipe Balbi06624812011-01-21 13:39:20 +08001060 if (musb->double_buffer_not_ok)
1061 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1062 else
1063 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1064 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001065
1066 /* force shared fifo to OUT-only mode */
1067 if (hw_ep->is_shared_fifo) {
1068 csr = musb_readw(regs, MUSB_TXCSR);
1069 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1070 musb_writew(regs, MUSB_TXCSR, csr);
1071 }
1072
1073 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1074 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1075 csr |= MUSB_RXCSR_P_ISO;
1076 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1077 csr |= MUSB_RXCSR_DISNYET;
1078
1079 /* set twice in case of double buffering */
1080 musb_writew(regs, MUSB_RXCSR, csr);
1081 musb_writew(regs, MUSB_RXCSR, csr);
1082 }
1083
1084 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1085 * for some reason you run out of channels here.
1086 */
1087 if (is_dma_capable() && musb->dma_controller) {
1088 struct dma_controller *c = musb->dma_controller;
1089
1090 musb_ep->dma = c->channel_alloc(c, hw_ep,
1091 (desc->bEndpointAddress & USB_DIR_IN));
1092 } else
1093 musb_ep->dma = NULL;
1094
1095 musb_ep->desc = desc;
1096 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001097 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001098 status = 0;
1099
1100 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1101 musb_driver_name, musb_ep->end_point.name,
1102 ({ char *s; switch (musb_ep->type) {
1103 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1104 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1105 default: s = "iso"; break;
1106 }; s; }),
1107 musb_ep->is_in ? "IN" : "OUT",
1108 musb_ep->dma ? "dma, " : "",
1109 musb_ep->packet_sz);
1110
1111 schedule_work(&musb->irq_work);
1112
1113fail:
1114 spin_unlock_irqrestore(&musb->lock, flags);
1115 return status;
1116}
1117
1118/*
1119 * Disable an endpoint flushing all requests queued.
1120 */
1121static int musb_gadget_disable(struct usb_ep *ep)
1122{
1123 unsigned long flags;
1124 struct musb *musb;
1125 u8 epnum;
1126 struct musb_ep *musb_ep;
1127 void __iomem *epio;
1128 int status = 0;
1129
1130 musb_ep = to_musb_ep(ep);
1131 musb = musb_ep->musb;
1132 epnum = musb_ep->current_epnum;
1133 epio = musb->endpoints[epnum].regs;
1134
1135 spin_lock_irqsave(&musb->lock, flags);
1136 musb_ep_select(musb->mregs, epnum);
1137
1138 /* zero the endpoint sizes */
1139 if (musb_ep->is_in) {
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001140 musb->intrtxe &= ~(1 << epnum);
1141 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001142 musb_writew(epio, MUSB_TXMAXP, 0);
1143 } else {
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001144 musb->intrrxe &= ~(1 << epnum);
1145 musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001146 musb_writew(epio, MUSB_RXMAXP, 0);
1147 }
1148
1149 musb_ep->desc = NULL;
Grazvydas Ignotas08f75bf2012-05-26 00:21:33 +03001150 musb_ep->end_point.desc = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001151
1152 /* abort all pending DMA and requests */
1153 nuke(musb_ep, -ESHUTDOWN);
1154
1155 schedule_work(&musb->irq_work);
1156
1157 spin_unlock_irqrestore(&(musb->lock), flags);
1158
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001159 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001160
1161 return status;
1162}
1163
1164/*
1165 * Allocate a request for an endpoint.
1166 * Reused by ep0 code.
1167 */
1168struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1169{
1170 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001171 struct musb *musb = musb_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03001172 struct musb_request *request = NULL;
1173
1174 request = kzalloc(sizeof *request, gfp_flags);
Felipe Balbi0607f862010-12-01 11:03:54 +02001175 if (!request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001176 dev_dbg(musb->controller, "not enough memory\n");
Felipe Balbi0607f862010-12-01 11:03:54 +02001177 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001178 }
1179
Felipe Balbi0607f862010-12-01 11:03:54 +02001180 request->request.dma = DMA_ADDR_INVALID;
1181 request->epnum = musb_ep->current_epnum;
1182 request->ep = musb_ep;
1183
Felipe Balbi550a7372008-07-24 12:27:36 +03001184 return &request->request;
1185}
1186
1187/*
1188 * Free a request
1189 * Reused by ep0 code.
1190 */
1191void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1192{
1193 kfree(to_musb_request(req));
1194}
1195
1196static LIST_HEAD(buffers);
1197
1198struct free_record {
1199 struct list_head list;
1200 struct device *dev;
1201 unsigned bytes;
1202 dma_addr_t dma;
1203};
1204
1205/*
1206 * Context: controller locked, IRQs blocked.
1207 */
Sergei Shtylyova666e3e2010-09-11 13:23:12 -05001208void musb_ep_restart(struct musb *musb, struct musb_request *req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001209{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001210 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001211 req->tx ? "TX/IN" : "RX/OUT",
1212 &req->request, req->request.length, req->epnum);
1213
1214 musb_ep_select(musb->mregs, req->epnum);
1215 if (req->tx)
1216 txstate(musb, req);
1217 else
1218 rxstate(musb, req);
1219}
1220
1221static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1222 gfp_t gfp_flags)
1223{
1224 struct musb_ep *musb_ep;
1225 struct musb_request *request;
1226 struct musb *musb;
1227 int status = 0;
1228 unsigned long lockflags;
1229
1230 if (!ep || !req)
1231 return -EINVAL;
1232 if (!req->buf)
1233 return -ENODATA;
1234
1235 musb_ep = to_musb_ep(ep);
1236 musb = musb_ep->musb;
1237
1238 request = to_musb_request(req);
1239 request->musb = musb;
1240
1241 if (request->ep != musb_ep)
1242 return -EINVAL;
1243
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001244 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
Felipe Balbi550a7372008-07-24 12:27:36 +03001245
1246 /* request is mine now... */
1247 request->request.actual = 0;
1248 request->request.status = -EINPROGRESS;
1249 request->epnum = musb_ep->current_epnum;
1250 request->tx = musb_ep->is_in;
1251
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +01001252 map_dma_buffer(request, musb, musb_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001253
1254 spin_lock_irqsave(&musb->lock, lockflags);
1255
1256 /* don't queue if the ep is down */
1257 if (!musb_ep->desc) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001258 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001259 req, ep->name, "disabled");
1260 status = -ESHUTDOWN;
1261 goto cleanup;
1262 }
1263
1264 /* add request to the list */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001265 list_add_tail(&request->list, &musb_ep->req_list);
Felipe Balbi550a7372008-07-24 12:27:36 +03001266
1267 /* it this is the head of the queue, start i/o ... */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001268 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
Felipe Balbi550a7372008-07-24 12:27:36 +03001269 musb_ep_restart(musb, request);
1270
1271cleanup:
1272 spin_unlock_irqrestore(&musb->lock, lockflags);
1273 return status;
1274}
1275
1276static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1277{
1278 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001279 struct musb_request *req = to_musb_request(request);
1280 struct musb_request *r;
Felipe Balbi550a7372008-07-24 12:27:36 +03001281 unsigned long flags;
1282 int status = 0;
1283 struct musb *musb = musb_ep->musb;
1284
1285 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1286 return -EINVAL;
1287
1288 spin_lock_irqsave(&musb->lock, flags);
1289
1290 list_for_each_entry(r, &musb_ep->req_list, list) {
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001291 if (r == req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001292 break;
1293 }
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001294 if (r != req) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001295 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001296 status = -EINVAL;
1297 goto done;
1298 }
1299
1300 /* if the hardware doesn't have the request, easy ... */
Felipe Balbi3d5ad132011-03-22 11:38:49 +02001301 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
Felipe Balbi550a7372008-07-24 12:27:36 +03001302 musb_g_giveback(musb_ep, request, -ECONNRESET);
1303
1304 /* ... else abort the dma transfer ... */
1305 else if (is_dma_capable() && musb_ep->dma) {
1306 struct dma_controller *c = musb->dma_controller;
1307
1308 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1309 if (c->channel_abort)
1310 status = c->channel_abort(musb_ep->dma);
1311 else
1312 status = -EBUSY;
1313 if (status == 0)
1314 musb_g_giveback(musb_ep, request, -ECONNRESET);
1315 } else {
1316 /* NOTE: by sticking to easily tested hardware/driver states,
1317 * we leave counting of in-flight packets imprecise.
1318 */
1319 musb_g_giveback(musb_ep, request, -ECONNRESET);
1320 }
1321
1322done:
1323 spin_unlock_irqrestore(&musb->lock, flags);
1324 return status;
1325}
1326
1327/*
1328 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1329 * data but will queue requests.
1330 *
1331 * exported to ep0 code
1332 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001333static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001334{
1335 struct musb_ep *musb_ep = to_musb_ep(ep);
1336 u8 epnum = musb_ep->current_epnum;
1337 struct musb *musb = musb_ep->musb;
1338 void __iomem *epio = musb->endpoints[epnum].regs;
1339 void __iomem *mbase;
1340 unsigned long flags;
1341 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001342 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001343 int status = 0;
1344
1345 if (!ep)
1346 return -EINVAL;
1347 mbase = musb->mregs;
1348
1349 spin_lock_irqsave(&musb->lock, flags);
1350
1351 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1352 status = -EINVAL;
1353 goto done;
1354 }
1355
1356 musb_ep_select(mbase, epnum);
1357
Felipe Balbiad1adb82011-02-16 12:40:05 +02001358 request = next_request(musb_ep);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001359 if (value) {
1360 if (request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001361 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001362 ep->name);
1363 status = -EAGAIN;
1364 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001365 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001366 /* Cannot portably stall with non-empty FIFO */
1367 if (musb_ep->is_in) {
1368 csr = musb_readw(epio, MUSB_TXCSR);
1369 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001370 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001371 status = -EAGAIN;
1372 goto done;
1373 }
1374 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001375 } else
1376 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001377
1378 /* set/clear the stall and toggle bits */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001379 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
Felipe Balbi550a7372008-07-24 12:27:36 +03001380 if (musb_ep->is_in) {
1381 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001382 csr |= MUSB_TXCSR_P_WZC_BITS
1383 | MUSB_TXCSR_CLRDATATOG;
1384 if (value)
1385 csr |= MUSB_TXCSR_P_SENDSTALL;
1386 else
1387 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1388 | MUSB_TXCSR_P_SENTSTALL);
1389 csr &= ~MUSB_TXCSR_TXPKTRDY;
1390 musb_writew(epio, MUSB_TXCSR, csr);
1391 } else {
1392 csr = musb_readw(epio, MUSB_RXCSR);
1393 csr |= MUSB_RXCSR_P_WZC_BITS
1394 | MUSB_RXCSR_FLUSHFIFO
1395 | MUSB_RXCSR_CLRDATATOG;
1396 if (value)
1397 csr |= MUSB_RXCSR_P_SENDSTALL;
1398 else
1399 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1400 | MUSB_RXCSR_P_SENTSTALL);
1401 musb_writew(epio, MUSB_RXCSR, csr);
1402 }
1403
Felipe Balbi550a7372008-07-24 12:27:36 +03001404 /* maybe start the first request in the queue */
1405 if (!musb_ep->busy && !value && request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001406 dev_dbg(musb->controller, "restarting the request\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001407 musb_ep_restart(musb, request);
1408 }
1409
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001410done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001411 spin_unlock_irqrestore(&musb->lock, flags);
1412 return status;
1413}
1414
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001415/*
1416 * Sets the halt feature with the clear requests ignored
1417 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001418static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001419{
1420 struct musb_ep *musb_ep = to_musb_ep(ep);
1421
1422 if (!ep)
1423 return -EINVAL;
1424
1425 musb_ep->wedged = 1;
1426
1427 return usb_ep_set_halt(ep);
1428}
1429
Felipe Balbi550a7372008-07-24 12:27:36 +03001430static int musb_gadget_fifo_status(struct usb_ep *ep)
1431{
1432 struct musb_ep *musb_ep = to_musb_ep(ep);
1433 void __iomem *epio = musb_ep->hw_ep->regs;
1434 int retval = -EINVAL;
1435
1436 if (musb_ep->desc && !musb_ep->is_in) {
1437 struct musb *musb = musb_ep->musb;
1438 int epnum = musb_ep->current_epnum;
1439 void __iomem *mbase = musb->mregs;
1440 unsigned long flags;
1441
1442 spin_lock_irqsave(&musb->lock, flags);
1443
1444 musb_ep_select(mbase, epnum);
1445 /* FIXME return zero unless RXPKTRDY is set */
1446 retval = musb_readw(epio, MUSB_RXCOUNT);
1447
1448 spin_unlock_irqrestore(&musb->lock, flags);
1449 }
1450 return retval;
1451}
1452
1453static void musb_gadget_fifo_flush(struct usb_ep *ep)
1454{
1455 struct musb_ep *musb_ep = to_musb_ep(ep);
1456 struct musb *musb = musb_ep->musb;
1457 u8 epnum = musb_ep->current_epnum;
1458 void __iomem *epio = musb->endpoints[epnum].regs;
1459 void __iomem *mbase;
1460 unsigned long flags;
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001461 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03001462
1463 mbase = musb->mregs;
1464
1465 spin_lock_irqsave(&musb->lock, flags);
1466 musb_ep_select(mbase, (u8) epnum);
1467
1468 /* disable interrupts */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001469 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
Felipe Balbi550a7372008-07-24 12:27:36 +03001470
1471 if (musb_ep->is_in) {
1472 csr = musb_readw(epio, MUSB_TXCSR);
1473 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1474 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
Yauheni Kaliuta4858f062011-06-08 17:12:02 +03001475 /*
1476 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1477 * to interrupt current FIFO loading, but not flushing
1478 * the already loaded ones.
1479 */
1480 csr &= ~MUSB_TXCSR_TXPKTRDY;
Felipe Balbi550a7372008-07-24 12:27:36 +03001481 musb_writew(epio, MUSB_TXCSR, csr);
1482 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1483 musb_writew(epio, MUSB_TXCSR, csr);
1484 }
1485 } else {
1486 csr = musb_readw(epio, MUSB_RXCSR);
1487 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1488 musb_writew(epio, MUSB_RXCSR, csr);
1489 musb_writew(epio, MUSB_RXCSR, csr);
1490 }
1491
1492 /* re-enable interrupt */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001493 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001494 spin_unlock_irqrestore(&musb->lock, flags);
1495}
1496
1497static const struct usb_ep_ops musb_ep_ops = {
1498 .enable = musb_gadget_enable,
1499 .disable = musb_gadget_disable,
1500 .alloc_request = musb_alloc_request,
1501 .free_request = musb_free_request,
1502 .queue = musb_gadget_queue,
1503 .dequeue = musb_gadget_dequeue,
1504 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001505 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001506 .fifo_status = musb_gadget_fifo_status,
1507 .fifo_flush = musb_gadget_fifo_flush
1508};
1509
1510/* ----------------------------------------------------------------------- */
1511
1512static int musb_gadget_get_frame(struct usb_gadget *gadget)
1513{
1514 struct musb *musb = gadget_to_musb(gadget);
1515
1516 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1517}
1518
1519static int musb_gadget_wakeup(struct usb_gadget *gadget)
1520{
1521 struct musb *musb = gadget_to_musb(gadget);
1522 void __iomem *mregs = musb->mregs;
1523 unsigned long flags;
1524 int status = -EINVAL;
1525 u8 power, devctl;
1526 int retries;
1527
1528 spin_lock_irqsave(&musb->lock, flags);
1529
David Brownell84e250f2009-03-31 12:30:04 -07001530 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001531 case OTG_STATE_B_PERIPHERAL:
1532 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1533 * that's part of the standard usb 1.1 state machine, and
1534 * doesn't affect OTG transitions.
1535 */
1536 if (musb->may_wakeup && musb->is_suspended)
1537 break;
1538 goto done;
1539 case OTG_STATE_B_IDLE:
1540 /* Start SRP ... OTG not required. */
1541 devctl = musb_readb(mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001542 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001543 devctl |= MUSB_DEVCTL_SESSION;
1544 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1545 devctl = musb_readb(mregs, MUSB_DEVCTL);
1546 retries = 100;
1547 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1548 devctl = musb_readb(mregs, MUSB_DEVCTL);
1549 if (retries-- < 1)
1550 break;
1551 }
1552 retries = 10000;
1553 while (devctl & MUSB_DEVCTL_SESSION) {
1554 devctl = musb_readb(mregs, MUSB_DEVCTL);
1555 if (retries-- < 1)
1556 break;
1557 }
1558
Hema HK86205432011-03-22 16:54:22 +05301559 spin_unlock_irqrestore(&musb->lock, flags);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001560 otg_start_srp(musb->xceiv->otg);
Hema HK86205432011-03-22 16:54:22 +05301561 spin_lock_irqsave(&musb->lock, flags);
1562
Felipe Balbi550a7372008-07-24 12:27:36 +03001563 /* Block idling for at least 1s */
1564 musb_platform_try_idle(musb,
1565 jiffies + msecs_to_jiffies(1 * HZ));
1566
1567 status = 0;
1568 goto done;
1569 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001570 dev_dbg(musb->controller, "Unhandled wake: %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02001571 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001572 goto done;
1573 }
1574
1575 status = 0;
1576
1577 power = musb_readb(mregs, MUSB_POWER);
1578 power |= MUSB_POWER_RESUME;
1579 musb_writeb(mregs, MUSB_POWER, power);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001580 dev_dbg(musb->controller, "issue wakeup\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001581
1582 /* FIXME do this next chunk in a timer callback, no udelay */
1583 mdelay(2);
1584
1585 power = musb_readb(mregs, MUSB_POWER);
1586 power &= ~MUSB_POWER_RESUME;
1587 musb_writeb(mregs, MUSB_POWER, power);
1588done:
1589 spin_unlock_irqrestore(&musb->lock, flags);
1590 return status;
1591}
1592
1593static int
1594musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1595{
1596 struct musb *musb = gadget_to_musb(gadget);
1597
1598 musb->is_self_powered = !!is_selfpowered;
1599 return 0;
1600}
1601
1602static void musb_pullup(struct musb *musb, int is_on)
1603{
1604 u8 power;
1605
1606 power = musb_readb(musb->mregs, MUSB_POWER);
1607 if (is_on)
1608 power |= MUSB_POWER_SOFTCONN;
1609 else
1610 power &= ~MUSB_POWER_SOFTCONN;
1611
1612 /* FIXME if on, HdrcStart; if off, HdrcStop */
1613
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001614 dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1615 is_on ? "on" : "off");
Felipe Balbi550a7372008-07-24 12:27:36 +03001616 musb_writeb(musb->mregs, MUSB_POWER, power);
1617}
1618
1619#if 0
1620static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1621{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001622 dev_dbg(musb->controller, "<= %s =>\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001623
1624 /*
1625 * FIXME iff driver's softconnect flag is set (as it is during probe,
1626 * though that can clear it), just musb_pullup().
1627 */
1628
1629 return -EINVAL;
1630}
1631#endif
1632
1633static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1634{
1635 struct musb *musb = gadget_to_musb(gadget);
1636
David Brownell84e250f2009-03-31 12:30:04 -07001637 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001638 return -EOPNOTSUPP;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001639 return usb_phy_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001640}
1641
1642static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1643{
1644 struct musb *musb = gadget_to_musb(gadget);
1645 unsigned long flags;
1646
1647 is_on = !!is_on;
1648
John Stultz93e098a2011-07-20 17:09:34 -07001649 pm_runtime_get_sync(musb->controller);
1650
Felipe Balbi550a7372008-07-24 12:27:36 +03001651 /* NOTE: this assumes we are sensing vbus; we'd rather
1652 * not pullup unless the B-session is active.
1653 */
1654 spin_lock_irqsave(&musb->lock, flags);
1655 if (is_on != musb->softconnect) {
1656 musb->softconnect = is_on;
1657 musb_pullup(musb, is_on);
1658 }
1659 spin_unlock_irqrestore(&musb->lock, flags);
John Stultz93e098a2011-07-20 17:09:34 -07001660
1661 pm_runtime_put(musb->controller);
1662
Felipe Balbi550a7372008-07-24 12:27:36 +03001663 return 0;
1664}
1665
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001666static int musb_gadget_start(struct usb_gadget *g,
1667 struct usb_gadget_driver *driver);
1668static int musb_gadget_stop(struct usb_gadget *g,
1669 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001670
Felipe Balbi550a7372008-07-24 12:27:36 +03001671static const struct usb_gadget_ops musb_gadget_operations = {
1672 .get_frame = musb_gadget_get_frame,
1673 .wakeup = musb_gadget_wakeup,
1674 .set_selfpowered = musb_gadget_set_self_powered,
1675 /* .vbus_session = musb_gadget_vbus_session, */
1676 .vbus_draw = musb_gadget_vbus_draw,
1677 .pullup = musb_gadget_pullup,
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001678 .udc_start = musb_gadget_start,
1679 .udc_stop = musb_gadget_stop,
Felipe Balbi550a7372008-07-24 12:27:36 +03001680};
1681
1682/* ----------------------------------------------------------------------- */
1683
1684/* Registration */
1685
1686/* Only this registration code "knows" the rule (from USB standards)
1687 * about there being only one external upstream port. It assumes
1688 * all peripheral ports are external...
1689 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001690
1691static void musb_gadget_release(struct device *dev)
1692{
1693 /* kref_put(WHAT) */
1694 dev_dbg(dev, "%s\n", __func__);
1695}
1696
1697
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001698static void
Felipe Balbi550a7372008-07-24 12:27:36 +03001699init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1700{
1701 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1702
1703 memset(ep, 0, sizeof *ep);
1704
1705 ep->current_epnum = epnum;
1706 ep->musb = musb;
1707 ep->hw_ep = hw_ep;
1708 ep->is_in = is_in;
1709
1710 INIT_LIST_HEAD(&ep->req_list);
1711
1712 sprintf(ep->name, "ep%d%s", epnum,
1713 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1714 is_in ? "in" : "out"));
1715 ep->end_point.name = ep->name;
1716 INIT_LIST_HEAD(&ep->end_point.ep_list);
1717 if (!epnum) {
1718 ep->end_point.maxpacket = 64;
1719 ep->end_point.ops = &musb_g_ep0_ops;
1720 musb->g.ep0 = &ep->end_point;
1721 } else {
1722 if (is_in)
1723 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1724 else
1725 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1726 ep->end_point.ops = &musb_ep_ops;
1727 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1728 }
1729}
1730
1731/*
1732 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1733 * to the rest of the driver state.
1734 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001735static inline void musb_g_init_endpoints(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001736{
1737 u8 epnum;
1738 struct musb_hw_ep *hw_ep;
1739 unsigned count = 0;
1740
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001741 /* initialize endpoint list just once */
Felipe Balbi550a7372008-07-24 12:27:36 +03001742 INIT_LIST_HEAD(&(musb->g.ep_list));
1743
1744 for (epnum = 0, hw_ep = musb->endpoints;
1745 epnum < musb->nr_endpoints;
1746 epnum++, hw_ep++) {
1747 if (hw_ep->is_shared_fifo /* || !epnum */) {
1748 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1749 count++;
1750 } else {
1751 if (hw_ep->max_packet_sz_tx) {
1752 init_peripheral_ep(musb, &hw_ep->ep_in,
1753 epnum, 1);
1754 count++;
1755 }
1756 if (hw_ep->max_packet_sz_rx) {
1757 init_peripheral_ep(musb, &hw_ep->ep_out,
1758 epnum, 0);
1759 count++;
1760 }
1761 }
1762 }
1763}
1764
1765/* called once during driver setup to initialize and link into
1766 * the driver model; memory is zeroed.
1767 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001768int musb_gadget_setup(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001769{
1770 int status;
1771
1772 /* REVISIT minor race: if (erroneously) setting up two
1773 * musb peripherals at the same time, only the bus lock
1774 * is probably held.
1775 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001776
1777 musb->g.ops = &musb_gadget_operations;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001778 musb->g.max_speed = USB_SPEED_HIGH;
Felipe Balbi550a7372008-07-24 12:27:36 +03001779 musb->g.speed = USB_SPEED_UNKNOWN;
1780
1781 /* this "gadget" abstracts/virtualizes the controller */
Felipe Balbi550a7372008-07-24 12:27:36 +03001782 musb->g.dev.parent = musb->controller;
1783 musb->g.dev.dma_mask = musb->controller->dma_mask;
1784 musb->g.dev.release = musb_gadget_release;
1785 musb->g.name = musb_driver_name;
Felipe Balbi032ec492011-11-24 15:46:26 +02001786 musb->g.is_otg = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001787
1788 musb_g_init_endpoints(musb);
1789
1790 musb->is_active = 0;
1791 musb_platform_try_idle(musb, 0);
1792
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001793 status = usb_add_gadget_udc(musb->controller, &musb->g);
1794 if (status)
1795 goto err;
1796
1797 return 0;
1798err:
Sebastian Andrzej Siewior6193d692011-08-10 11:01:57 +02001799 musb->g.dev.parent = NULL;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001800 device_unregister(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001801 return status;
1802}
1803
1804void musb_gadget_cleanup(struct musb *musb)
1805{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001806 usb_del_gadget_udc(&musb->g);
Felipe Balbi550a7372008-07-24 12:27:36 +03001807}
1808
1809/*
1810 * Register the gadget driver. Used by gadget drivers when
1811 * registering themselves with the controller.
1812 *
1813 * -EINVAL something went wrong (not driver)
1814 * -EBUSY another gadget is already using the controller
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001815 * -ENOMEM no memory to perform the operation
Felipe Balbi550a7372008-07-24 12:27:36 +03001816 *
1817 * @param driver the gadget driver
1818 * @return <0 if error, 0 if everything is fine
1819 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001820static int musb_gadget_start(struct usb_gadget *g,
1821 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03001822{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001823 struct musb *musb = gadget_to_musb(g);
Heikki Krogerusd445b6d2012-02-13 13:24:15 +02001824 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi032ec492011-11-24 15:46:26 +02001825 struct usb_hcd *hcd = musb_to_hcd(musb);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001826 unsigned long flags;
Felipe Balbi032ec492011-11-24 15:46:26 +02001827 int retval = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001828
Felipe Balbi032ec492011-11-24 15:46:26 +02001829 if (driver->max_speed < USB_SPEED_HIGH) {
1830 retval = -EINVAL;
1831 goto err;
1832 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001833
Hema HK7acc6192011-02-28 14:19:34 +05301834 pm_runtime_get_sync(musb->controller);
1835
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001836 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03001837
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001838 musb->softconnect = 0;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001839 musb->gadget_driver = driver;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001840
1841 spin_lock_irqsave(&musb->lock, flags);
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001842 musb->is_active = 1;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001843
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001844 otg_set_peripheral(otg, &musb->g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001845 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001846 spin_unlock_irqrestore(&musb->lock, flags);
1847
Felipe Balbi032ec492011-11-24 15:46:26 +02001848 /* REVISIT: funcall to other code, which also
1849 * handles power budgeting ... this way also
1850 * ensures HdrcStart is indirectly called.
1851 */
1852 retval = usb_add_hcd(hcd, 0, 0);
1853 if (retval < 0) {
1854 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
1855 goto err;
Felipe Balbi550a7372008-07-24 12:27:36 +03001856 }
Felipe Balbi032ec492011-11-24 15:46:26 +02001857
1858 if ((musb->xceiv->last_event == USB_EVENT_ID)
1859 && otg->set_vbus)
1860 otg_set_vbus(otg, 1);
1861
1862 hcd->self.uses_pio_for_control = 1;
1863
Jarkko Nikulacdefce12011-04-29 16:17:35 +03001864 if (musb->xceiv->last_event == USB_EVENT_NONE)
1865 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001866
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001867 return 0;
1868
Felipe Balbi032ec492011-11-24 15:46:26 +02001869err:
Felipe Balbi550a7372008-07-24 12:27:36 +03001870 return retval;
1871}
Felipe Balbi550a7372008-07-24 12:27:36 +03001872
1873static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1874{
1875 int i;
1876 struct musb_hw_ep *hw_ep;
1877
1878 /* don't disconnect if it's not connected */
1879 if (musb->g.speed == USB_SPEED_UNKNOWN)
1880 driver = NULL;
1881 else
1882 musb->g.speed = USB_SPEED_UNKNOWN;
1883
1884 /* deactivate the hardware */
1885 if (musb->softconnect) {
1886 musb->softconnect = 0;
1887 musb_pullup(musb, 0);
1888 }
1889 musb_stop(musb);
1890
1891 /* killing any outstanding requests will quiesce the driver;
1892 * then report disconnect
1893 */
1894 if (driver) {
1895 for (i = 0, hw_ep = musb->endpoints;
1896 i < musb->nr_endpoints;
1897 i++, hw_ep++) {
1898 musb_ep_select(musb->mregs, i);
1899 if (hw_ep->is_shared_fifo /* || !epnum */) {
1900 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1901 } else {
1902 if (hw_ep->max_packet_sz_tx)
1903 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1904 if (hw_ep->max_packet_sz_rx)
1905 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1906 }
1907 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001908 }
1909}
1910
1911/*
1912 * Unregister the gadget driver. Used by gadget drivers when
1913 * unregistering themselves from the controller.
1914 *
1915 * @param driver the gadget driver to unregister
1916 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001917static int musb_gadget_stop(struct usb_gadget *g,
1918 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03001919{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001920 struct musb *musb = gadget_to_musb(g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001921 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03001922
Hema HK7acc6192011-02-28 14:19:34 +05301923 if (musb->xceiv->last_event == USB_EVENT_NONE)
1924 pm_runtime_get_sync(musb->controller);
1925
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001926 /*
1927 * REVISIT always use otg_set_peripheral() here too;
Felipe Balbi550a7372008-07-24 12:27:36 +03001928 * this needs to shut down the OTG engine.
1929 */
1930
1931 spin_lock_irqsave(&musb->lock, flags);
1932
Felipe Balbi550a7372008-07-24 12:27:36 +03001933 musb_hnp_stop(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001934
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001935 (void) musb_gadget_vbus_draw(&musb->g, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001936
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001937 musb->xceiv->state = OTG_STATE_UNDEFINED;
1938 stop_activity(musb, driver);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001939 otg_set_peripheral(musb->xceiv->otg, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001940
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001941 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03001942
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001943 musb->is_active = 0;
1944 musb_platform_try_idle(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001945 spin_unlock_irqrestore(&musb->lock, flags);
1946
Felipe Balbi032ec492011-11-24 15:46:26 +02001947 usb_remove_hcd(musb_to_hcd(musb));
1948 /*
1949 * FIXME we need to be able to register another
1950 * gadget driver here and have everything work;
1951 * that currently misbehaves.
1952 */
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001953
Hema HK7acc6192011-02-28 14:19:34 +05301954 pm_runtime_put(musb->controller);
1955
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001956 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001957}
Felipe Balbi550a7372008-07-24 12:27:36 +03001958
1959/* ----------------------------------------------------------------------- */
1960
1961/* lifecycle operations called through plat_uds.c */
1962
1963void musb_g_resume(struct musb *musb)
1964{
1965 musb->is_suspended = 0;
David Brownell84e250f2009-03-31 12:30:04 -07001966 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001967 case OTG_STATE_B_IDLE:
1968 break;
1969 case OTG_STATE_B_WAIT_ACON:
1970 case OTG_STATE_B_PERIPHERAL:
1971 musb->is_active = 1;
1972 if (musb->gadget_driver && musb->gadget_driver->resume) {
1973 spin_unlock(&musb->lock);
1974 musb->gadget_driver->resume(&musb->g);
1975 spin_lock(&musb->lock);
1976 }
1977 break;
1978 default:
1979 WARNING("unhandled RESUME transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02001980 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001981 }
1982}
1983
1984/* called when SOF packets stop for 3+ msec */
1985void musb_g_suspend(struct musb *musb)
1986{
1987 u8 devctl;
1988
1989 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001990 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001991
David Brownell84e250f2009-03-31 12:30:04 -07001992 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001993 case OTG_STATE_B_IDLE:
1994 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
David Brownell84e250f2009-03-31 12:30:04 -07001995 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001996 break;
1997 case OTG_STATE_B_PERIPHERAL:
1998 musb->is_suspended = 1;
1999 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2000 spin_unlock(&musb->lock);
2001 musb->gadget_driver->suspend(&musb->g);
2002 spin_lock(&musb->lock);
2003 }
2004 break;
2005 default:
2006 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2007 * A_PERIPHERAL may need care too
2008 */
2009 WARNING("unhandled SUSPEND transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002010 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002011 }
2012}
2013
2014/* Called during SRP */
2015void musb_g_wakeup(struct musb *musb)
2016{
2017 musb_gadget_wakeup(&musb->g);
2018}
2019
2020/* called when VBUS drops below session threshold, and in other cases */
2021void musb_g_disconnect(struct musb *musb)
2022{
2023 void __iomem *mregs = musb->mregs;
2024 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2025
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002026 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002027
2028 /* clear HR */
2029 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2030
2031 /* don't draw vbus until new b-default session */
2032 (void) musb_gadget_vbus_draw(&musb->g, 0);
2033
2034 musb->g.speed = USB_SPEED_UNKNOWN;
2035 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2036 spin_unlock(&musb->lock);
2037 musb->gadget_driver->disconnect(&musb->g);
2038 spin_lock(&musb->lock);
2039 }
2040
David Brownell84e250f2009-03-31 12:30:04 -07002041 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002042 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002043 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002044 otg_state_string(musb->xceiv->state));
David Brownell84e250f2009-03-31 12:30:04 -07002045 musb->xceiv->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07002046 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002047 break;
2048 case OTG_STATE_A_PERIPHERAL:
David Brownell1de00da2009-04-02 10:16:11 -07002049 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
David Brownellab983f2a2009-03-31 12:35:09 -07002050 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002051 break;
2052 case OTG_STATE_B_WAIT_ACON:
2053 case OTG_STATE_B_HOST:
Felipe Balbi550a7372008-07-24 12:27:36 +03002054 case OTG_STATE_B_PERIPHERAL:
2055 case OTG_STATE_B_IDLE:
David Brownell84e250f2009-03-31 12:30:04 -07002056 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002057 break;
2058 case OTG_STATE_B_SRP_INIT:
2059 break;
2060 }
2061
2062 musb->is_active = 0;
2063}
2064
2065void musb_g_reset(struct musb *musb)
2066__releases(musb->lock)
2067__acquires(musb->lock)
2068{
2069 void __iomem *mbase = musb->mregs;
2070 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2071 u8 power;
2072
Sebastian Andrzej Siewior515ba292012-10-30 19:52:24 +01002073 dev_dbg(musb->controller, "<== %s driver '%s'\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002074 (devctl & MUSB_DEVCTL_BDEVICE)
2075 ? "B-Device" : "A-Device",
Felipe Balbi550a7372008-07-24 12:27:36 +03002076 musb->gadget_driver
2077 ? musb->gadget_driver->driver.name
2078 : NULL
2079 );
2080
2081 /* report disconnect, if we didn't already (flushing EP state) */
2082 if (musb->g.speed != USB_SPEED_UNKNOWN)
2083 musb_g_disconnect(musb);
2084
2085 /* clear HR */
2086 else if (devctl & MUSB_DEVCTL_HR)
2087 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2088
2089
2090 /* what speed did we negotiate? */
2091 power = musb_readb(mbase, MUSB_POWER);
2092 musb->g.speed = (power & MUSB_POWER_HSMODE)
2093 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2094
2095 /* start in USB_STATE_DEFAULT */
2096 musb->is_active = 1;
2097 musb->is_suspended = 0;
2098 MUSB_DEV_MODE(musb);
2099 musb->address = 0;
2100 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2101
2102 musb->may_wakeup = 0;
2103 musb->g.b_hnp_enable = 0;
2104 musb->g.a_alt_hnp_support = 0;
2105 musb->g.a_hnp_support = 0;
2106
2107 /* Normal reset, as B-Device;
2108 * or else after HNP, as A-Device
2109 */
2110 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -07002111 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002112 musb->g.is_a_peripheral = 0;
Felipe Balbi032ec492011-11-24 15:46:26 +02002113 } else {
David Brownell84e250f2009-03-31 12:30:04 -07002114 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002115 musb->g.is_a_peripheral = 1;
Felipe Balbi032ec492011-11-24 15:46:26 +02002116 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002117
2118 /* start with default limits on VBUS power draw */
Felipe Balbi032ec492011-11-24 15:46:26 +02002119 (void) musb_gadget_vbus_draw(&musb->g, 8);
Felipe Balbi550a7372008-07-24 12:27:36 +03002120}