blob: d72557cfc05280f53e66acf7298fef9c8c578955 [file] [log] [blame]
Paul Zimmerman7359d482013-03-11 17:47:59 -07001/*
2 * hcd.c - DesignWare HS OTG Controller host-mode routines
3 *
4 * Copyright (C) 2004-2013 Synopsys, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
22 * later version.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * This file contains the core HCD code, and implements the Linux hc_driver
39 * API
40 */
41#include <linux/kernel.h>
42#include <linux/module.h>
43#include <linux/spinlock.h>
44#include <linux/interrupt.h>
45#include <linux/dma-mapping.h>
46#include <linux/delay.h>
47#include <linux/io.h>
48#include <linux/slab.h>
49#include <linux/usb.h>
50
51#include <linux/usb/hcd.h>
52#include <linux/usb/ch11.h>
53
54#include "core.h"
55#include "hcd.h"
56
57/**
58 * dwc2_dump_channel_info() - Prints the state of a host channel
59 *
60 * @hsotg: Programming view of DWC_otg controller
61 * @chan: Pointer to the channel to dump
62 *
63 * Must be called with interrupt disabled and spinlock held
64 *
65 * NOTE: This function will be removed once the peripheral controller code
66 * is integrated and the driver is stable
67 */
68static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
69 struct dwc2_host_chan *chan)
70{
71#ifdef VERBOSE_DEBUG
72 int num_channels = hsotg->core_params->host_channels;
73 struct dwc2_qh *qh;
74 u32 hcchar;
75 u32 hcsplt;
76 u32 hctsiz;
77 u32 hc_dma;
78 int i;
79
80 if (chan == NULL)
81 return;
82
83 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
84 hcsplt = readl(hsotg->regs + HCSPLT(chan->hc_num));
85 hctsiz = readl(hsotg->regs + HCTSIZ(chan->hc_num));
86 hc_dma = readl(hsotg->regs + HCDMA(chan->hc_num));
87
88 dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
89 dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
90 hcchar, hcsplt);
91 dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
92 hctsiz, hc_dma);
93 dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
94 chan->dev_addr, chan->ep_num, chan->ep_is_in);
95 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
96 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
97 dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
98 dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
99 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
100 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
101 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
102 (unsigned long)chan->xfer_dma);
103 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
104 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
105 dev_dbg(hsotg->dev, " NP inactive sched:\n");
106 list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
107 qh_list_entry)
108 dev_dbg(hsotg->dev, " %p\n", qh);
109 dev_dbg(hsotg->dev, " NP active sched:\n");
110 list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
111 qh_list_entry)
112 dev_dbg(hsotg->dev, " %p\n", qh);
113 dev_dbg(hsotg->dev, " Channels:\n");
114 for (i = 0; i < num_channels; i++) {
115 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
116
117 dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
118 }
119#endif /* VERBOSE_DEBUG */
120}
121
122/*
123 * Processes all the URBs in a single list of QHs. Completes them with
124 * -ETIMEDOUT and frees the QTD.
125 *
126 * Must be called with interrupt disabled and spinlock held
127 */
128static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
129 struct list_head *qh_list)
130{
131 struct dwc2_qh *qh, *qh_tmp;
132 struct dwc2_qtd *qtd, *qtd_tmp;
133
134 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
135 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
136 qtd_list_entry) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700137 dwc2_host_complete(hsotg, qtd, -ETIMEDOUT);
138 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700139 }
140 }
141}
142
143static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
144 struct list_head *qh_list)
145{
146 struct dwc2_qtd *qtd, *qtd_tmp;
147 struct dwc2_qh *qh, *qh_tmp;
148 unsigned long flags;
149
150 if (!qh_list->next)
151 /* The list hasn't been initialized yet */
152 return;
153
154 spin_lock_irqsave(&hsotg->lock, flags);
155
156 /* Ensure there are no QTDs or URBs left */
157 dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
158
159 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
160 dwc2_hcd_qh_unlink(hsotg, qh);
161
162 /* Free each QTD in the QH's QTD list */
163 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
164 qtd_list_entry)
165 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
166
167 spin_unlock_irqrestore(&hsotg->lock, flags);
168 dwc2_hcd_qh_free(hsotg, qh);
169 spin_lock_irqsave(&hsotg->lock, flags);
170 }
171
172 spin_unlock_irqrestore(&hsotg->lock, flags);
173}
174
175/*
176 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
177 * and periodic schedules. The QTD associated with each URB is removed from
178 * the schedule and freed. This function may be called when a disconnect is
179 * detected or when the HCD is being stopped.
180 *
181 * Must be called with interrupt disabled and spinlock held
182 */
183static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
184{
185 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
186 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
187 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
188 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
189 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
190 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
191}
192
193/**
194 * dwc2_hcd_start() - Starts the HCD when switching to Host mode
195 *
196 * @hsotg: Pointer to struct dwc2_hsotg
197 */
198void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
199{
200 u32 hprt0;
201
202 if (hsotg->op_state == OTG_STATE_B_HOST) {
203 /*
204 * Reset the port. During a HNP mode switch the reset
205 * needs to occur within 1ms and have a duration of at
206 * least 50ms.
207 */
208 hprt0 = dwc2_read_hprt0(hsotg);
209 hprt0 |= HPRT0_RST;
210 writel(hprt0, hsotg->regs + HPRT0);
211 }
212
213 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
214 msecs_to_jiffies(50));
215}
216
217/* Must be called with interrupt disabled and spinlock held */
218static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
219{
220 int num_channels = hsotg->core_params->host_channels;
221 struct dwc2_host_chan *channel;
222 u32 hcchar;
223 int i;
224
225 if (hsotg->core_params->dma_enable <= 0) {
226 /* Flush out any channel requests in slave mode */
227 for (i = 0; i < num_channels; i++) {
228 channel = hsotg->hc_ptr_array[i];
229 if (!list_empty(&channel->hc_list_entry))
230 continue;
231 hcchar = readl(hsotg->regs + HCCHAR(i));
232 if (hcchar & HCCHAR_CHENA) {
233 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
234 hcchar |= HCCHAR_CHDIS;
235 writel(hcchar, hsotg->regs + HCCHAR(i));
236 }
237 }
238 }
239
240 for (i = 0; i < num_channels; i++) {
241 channel = hsotg->hc_ptr_array[i];
242 if (!list_empty(&channel->hc_list_entry))
243 continue;
244 hcchar = readl(hsotg->regs + HCCHAR(i));
245 if (hcchar & HCCHAR_CHENA) {
246 /* Halt the channel */
247 hcchar |= HCCHAR_CHDIS;
248 writel(hcchar, hsotg->regs + HCCHAR(i));
249 }
250
251 dwc2_hc_cleanup(hsotg, channel);
252 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
253 /*
254 * Added for Descriptor DMA to prevent channel double cleanup in
255 * release_channel_ddma(), which is called from ep_disable when
256 * device disconnects
257 */
258 channel->qh = NULL;
259 }
Vincent Palatin7252f1b2015-03-15 13:24:32 -0700260 /* All channels have been freed, mark them available */
261 if (hsotg->core_params->uframe_sched > 0) {
262 hsotg->available_host_channels =
263 hsotg->core_params->host_channels;
264 } else {
265 hsotg->non_periodic_channels = 0;
266 hsotg->periodic_channels = 0;
267 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700268}
269
270/**
271 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
272 *
273 * @hsotg: Pointer to struct dwc2_hsotg
274 *
275 * Must be called with interrupt disabled and spinlock held
276 */
277void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg)
278{
279 u32 intr;
280
281 /* Set status flags for the hub driver */
282 hsotg->flags.b.port_connect_status_change = 1;
283 hsotg->flags.b.port_connect_status = 0;
284
285 /*
286 * Shutdown any transfers in process by clearing the Tx FIFO Empty
287 * interrupt mask and status bits and disabling subsequent host
288 * channel interrupts.
289 */
290 intr = readl(hsotg->regs + GINTMSK);
291 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
292 writel(intr, hsotg->regs + GINTMSK);
293 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
294 writel(intr, hsotg->regs + GINTSTS);
295
296 /*
297 * Turn off the vbus power only if the core has transitioned to device
298 * mode. If still in host mode, need to keep power on to detect a
299 * reconnection.
300 */
301 if (dwc2_is_device_mode(hsotg)) {
302 if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
303 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
304 writel(0, hsotg->regs + HPRT0);
305 }
306
307 dwc2_disable_host_interrupts(hsotg);
308 }
309
310 /* Respond with an error status to all URBs in the schedule */
311 dwc2_kill_all_urbs(hsotg);
312
313 if (dwc2_is_host_mode(hsotg))
314 /* Clean up any host channels that were in use */
315 dwc2_hcd_cleanup_channels(hsotg);
316
317 dwc2_host_disconnect(hsotg);
318}
319
320/**
321 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
322 *
323 * @hsotg: Pointer to struct dwc2_hsotg
324 */
325static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
326{
Gregory Herrerob46146d52015-01-30 09:09:26 +0100327 if (hsotg->lx_state == DWC2_L2) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700328 hsotg->flags.b.port_suspend_change = 1;
Gregory Herrerob46146d52015-01-30 09:09:26 +0100329 usb_hcd_resume_root_hub(hsotg->priv);
330 } else {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700331 hsotg->flags.b.port_l1_change = 1;
Gregory Herrerob46146d52015-01-30 09:09:26 +0100332 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700333}
334
335/**
336 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
337 *
338 * @hsotg: Pointer to struct dwc2_hsotg
339 *
340 * Must be called with interrupt disabled and spinlock held
341 */
342void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
343{
344 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
345
346 /*
347 * The root hub should be disconnected before this function is called.
348 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
349 * and the QH lists (via ..._hcd_endpoint_disable).
350 */
351
352 /* Turn off all host-specific interrupts */
353 dwc2_disable_host_interrupts(hsotg);
354
355 /* Turn off the vbus power */
356 dev_dbg(hsotg->dev, "PortPower off\n");
357 writel(0, hsotg->regs + HPRT0);
358}
359
Gregory Herrero33ad2612015-04-29 22:09:15 +0200360/* Caller must hold driver lock */
Paul Zimmerman7359d482013-03-11 17:47:59 -0700361static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
362 struct dwc2_hcd_urb *urb, void **ep_handle,
363 gfp_t mem_flags)
364{
365 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700366 u32 intr_mask;
367 int retval;
Nick Hudson9f8144c2013-12-06 14:01:44 -0800368 int dev_speed;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700369
370 if (!hsotg->flags.b.port_connect_status) {
371 /* No longer connected */
372 dev_err(hsotg->dev, "Not connected\n");
373 return -ENODEV;
374 }
375
Nick Hudson9f8144c2013-12-06 14:01:44 -0800376 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
377
378 /* Some configurations cannot support LS traffic on a FS root port */
379 if ((dev_speed == USB_SPEED_LOW) &&
380 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
381 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
382 u32 hprt0 = readl(hsotg->regs + HPRT0);
383 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
384
385 if (prtspd == HPRT0_SPD_FULL_SPEED)
386 return -ENODEV;
387 }
388
Paul Zimmerman7359d482013-03-11 17:47:59 -0700389 qtd = kzalloc(sizeof(*qtd), mem_flags);
390 if (!qtd)
391 return -ENOMEM;
392
393 dwc2_hcd_qtd_init(qtd, urb);
394 retval = dwc2_hcd_qtd_add(hsotg, qtd, (struct dwc2_qh **)ep_handle,
395 mem_flags);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800396 if (retval) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700397 dev_err(hsotg->dev,
398 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
399 retval);
400 kfree(qtd);
401 return retval;
402 }
403
404 intr_mask = readl(hsotg->regs + GINTMSK);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800405 if (!(intr_mask & GINTSTS_SOF)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700406 enum dwc2_transaction_type tr_type;
407
408 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
409 !(qtd->urb->flags & URB_GIVEBACK_ASAP))
410 /*
411 * Do not schedule SG transactions until qtd has
412 * URB_GIVEBACK_ASAP set
413 */
414 return 0;
415
Paul Zimmerman7359d482013-03-11 17:47:59 -0700416 tr_type = dwc2_hcd_select_transactions(hsotg);
417 if (tr_type != DWC2_TRANSACTION_NONE)
418 dwc2_hcd_queue_transactions(hsotg, tr_type);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700419 }
420
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800421 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700422}
423
424/* Must be called with interrupt disabled and spinlock held */
425static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
426 struct dwc2_hcd_urb *urb)
427{
428 struct dwc2_qh *qh;
429 struct dwc2_qtd *urb_qtd;
430
431 urb_qtd = urb->qtd;
432 if (!urb_qtd) {
433 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
434 return -EINVAL;
435 }
436
437 qh = urb_qtd->qh;
438 if (!qh) {
439 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
440 return -EINVAL;
441 }
442
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700443 urb->priv = NULL;
444
Paul Zimmerman7359d482013-03-11 17:47:59 -0700445 if (urb_qtd->in_process && qh->channel) {
446 dwc2_dump_channel_info(hsotg, qh->channel);
447
448 /* The QTD is in process (it has been assigned to a channel) */
449 if (hsotg->flags.b.port_connect_status)
450 /*
451 * If still connected (i.e. in host mode), halt the
452 * channel so it can be used for other transfers. If
453 * no longer connected, the host registers can't be
454 * written to halt the channel since the core is in
455 * device mode.
456 */
457 dwc2_hc_halt(hsotg, qh->channel,
458 DWC2_HC_XFER_URB_DEQUEUE);
459 }
460
461 /*
462 * Free the QTD and clean up the associated QH. Leave the QH in the
463 * schedule if it has any remaining QTDs.
464 */
465 if (hsotg->core_params->dma_desc_enable <= 0) {
466 u8 in_process = urb_qtd->in_process;
467
468 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
469 if (in_process) {
470 dwc2_hcd_qh_deactivate(hsotg, qh, 0);
471 qh->channel = NULL;
472 } else if (list_empty(&qh->qtd_list)) {
473 dwc2_hcd_qh_unlink(hsotg, qh);
474 }
475 } else {
476 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
477 }
478
479 return 0;
480}
481
482/* Must NOT be called with interrupt disabled or spinlock held */
483static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
484 struct usb_host_endpoint *ep, int retry)
485{
486 struct dwc2_qtd *qtd, *qtd_tmp;
487 struct dwc2_qh *qh;
488 unsigned long flags;
489 int rc;
490
491 spin_lock_irqsave(&hsotg->lock, flags);
492
493 qh = ep->hcpriv;
494 if (!qh) {
495 rc = -EINVAL;
496 goto err;
497 }
498
499 while (!list_empty(&qh->qtd_list) && retry--) {
500 if (retry == 0) {
501 dev_err(hsotg->dev,
502 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
503 rc = -EBUSY;
504 goto err;
505 }
506
507 spin_unlock_irqrestore(&hsotg->lock, flags);
508 usleep_range(20000, 40000);
509 spin_lock_irqsave(&hsotg->lock, flags);
510 qh = ep->hcpriv;
511 if (!qh) {
512 rc = -EINVAL;
513 goto err;
514 }
515 }
516
517 dwc2_hcd_qh_unlink(hsotg, qh);
518
519 /* Free each QTD in the QH's QTD list */
520 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
521 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
522
523 ep->hcpriv = NULL;
524 spin_unlock_irqrestore(&hsotg->lock, flags);
525 dwc2_hcd_qh_free(hsotg, qh);
526
527 return 0;
528
529err:
530 ep->hcpriv = NULL;
531 spin_unlock_irqrestore(&hsotg->lock, flags);
532
533 return rc;
534}
535
536/* Must be called with interrupt disabled and spinlock held */
537static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
538 struct usb_host_endpoint *ep)
539{
540 struct dwc2_qh *qh = ep->hcpriv;
541
542 if (!qh)
543 return -EINVAL;
544
545 qh->data_toggle = DWC2_HC_PID_DATA0;
546
547 return 0;
548}
549
550/*
551 * Initializes dynamic portions of the DWC_otg HCD state
552 *
553 * Must be called with interrupt disabled and spinlock held
554 */
555static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
556{
557 struct dwc2_host_chan *chan, *chan_tmp;
558 int num_channels;
559 int i;
560
561 hsotg->flags.d32 = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700562 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700563
564 if (hsotg->core_params->uframe_sched > 0) {
565 hsotg->available_host_channels =
566 hsotg->core_params->host_channels;
567 } else {
568 hsotg->non_periodic_channels = 0;
569 hsotg->periodic_channels = 0;
570 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700571
572 /*
573 * Put all channels in the free channel list and clean up channel
574 * states
575 */
576 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
577 hc_list_entry)
578 list_del_init(&chan->hc_list_entry);
579
580 num_channels = hsotg->core_params->host_channels;
581 for (i = 0; i < num_channels; i++) {
582 chan = hsotg->hc_ptr_array[i];
583 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
584 dwc2_hc_cleanup(hsotg, chan);
585 }
586
587 /* Initialize the DWC core for host mode operation */
588 dwc2_core_host_init(hsotg);
589}
590
591static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
592 struct dwc2_host_chan *chan,
593 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
594{
595 int hub_addr, hub_port;
596
597 chan->do_split = 1;
598 chan->xact_pos = qtd->isoc_split_pos;
599 chan->complete_split = qtd->complete_split;
600 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
601 chan->hub_addr = (u8)hub_addr;
602 chan->hub_port = (u8)hub_port;
603}
604
605static void *dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
606 struct dwc2_host_chan *chan,
607 struct dwc2_qtd *qtd, void *bufptr)
608{
609 struct dwc2_hcd_urb *urb = qtd->urb;
610 struct dwc2_hcd_iso_packet_desc *frame_desc;
611
612 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
613 case USB_ENDPOINT_XFER_CONTROL:
614 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
615
616 switch (qtd->control_phase) {
617 case DWC2_CONTROL_SETUP:
618 dev_vdbg(hsotg->dev, " Control setup transaction\n");
619 chan->do_ping = 0;
620 chan->ep_is_in = 0;
621 chan->data_pid_start = DWC2_HC_PID_SETUP;
622 if (hsotg->core_params->dma_enable > 0)
623 chan->xfer_dma = urb->setup_dma;
624 else
625 chan->xfer_buf = urb->setup_packet;
626 chan->xfer_len = 8;
627 bufptr = NULL;
628 break;
629
630 case DWC2_CONTROL_DATA:
631 dev_vdbg(hsotg->dev, " Control data transaction\n");
632 chan->data_pid_start = qtd->data_toggle;
633 break;
634
635 case DWC2_CONTROL_STATUS:
636 /*
637 * Direction is opposite of data direction or IN if no
638 * data
639 */
640 dev_vdbg(hsotg->dev, " Control status transaction\n");
641 if (urb->length == 0)
642 chan->ep_is_in = 1;
643 else
644 chan->ep_is_in =
645 dwc2_hcd_is_pipe_out(&urb->pipe_info);
646 if (chan->ep_is_in)
647 chan->do_ping = 0;
648 chan->data_pid_start = DWC2_HC_PID_DATA1;
649 chan->xfer_len = 0;
650 if (hsotg->core_params->dma_enable > 0)
651 chan->xfer_dma = hsotg->status_buf_dma;
652 else
653 chan->xfer_buf = hsotg->status_buf;
654 bufptr = NULL;
655 break;
656 }
657 break;
658
659 case USB_ENDPOINT_XFER_BULK:
660 chan->ep_type = USB_ENDPOINT_XFER_BULK;
661 break;
662
663 case USB_ENDPOINT_XFER_INT:
664 chan->ep_type = USB_ENDPOINT_XFER_INT;
665 break;
666
667 case USB_ENDPOINT_XFER_ISOC:
668 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
669 if (hsotg->core_params->dma_desc_enable > 0)
670 break;
671
672 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
673 frame_desc->status = 0;
674
675 if (hsotg->core_params->dma_enable > 0) {
676 chan->xfer_dma = urb->dma;
677 chan->xfer_dma += frame_desc->offset +
678 qtd->isoc_split_offset;
679 } else {
680 chan->xfer_buf = urb->buf;
681 chan->xfer_buf += frame_desc->offset +
682 qtd->isoc_split_offset;
683 }
684
685 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
686
687 /* For non-dword aligned buffers */
688 if (hsotg->core_params->dma_enable > 0 &&
689 (chan->xfer_dma & 0x3))
690 bufptr = (u8 *)urb->buf + frame_desc->offset +
691 qtd->isoc_split_offset;
692 else
693 bufptr = NULL;
694
695 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
696 if (chan->xfer_len <= 188)
697 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
698 else
699 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
700 }
701 break;
702 }
703
704 return bufptr;
705}
706
707static int dwc2_hc_setup_align_buf(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
Paul Zimmerman5dce9552014-09-16 13:47:27 -0700708 struct dwc2_host_chan *chan,
709 struct dwc2_hcd_urb *urb, void *bufptr)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700710{
711 u32 buf_size;
Paul Zimmerman5dce9552014-09-16 13:47:27 -0700712 struct urb *usb_urb;
713 struct usb_hcd *hcd;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700714
715 if (!qh->dw_align_buf) {
Paul Zimmerman5dce9552014-09-16 13:47:27 -0700716 if (chan->ep_type != USB_ENDPOINT_XFER_ISOC)
717 buf_size = hsotg->core_params->max_transfer_size;
718 else
719 /* 3072 = 3 max-size Isoc packets */
720 buf_size = 3072;
721
Paul Zimmerman7359d482013-03-11 17:47:59 -0700722 qh->dw_align_buf = dma_alloc_coherent(hsotg->dev, buf_size,
723 &qh->dw_align_buf_dma,
724 GFP_ATOMIC);
725 if (!qh->dw_align_buf)
726 return -ENOMEM;
Paul Zimmerman5dce9552014-09-16 13:47:27 -0700727 qh->dw_align_buf_size = buf_size;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700728 }
729
Paul Zimmerman5dce9552014-09-16 13:47:27 -0700730 if (chan->xfer_len) {
731 dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", __func__);
732 usb_urb = urb->priv;
733
734 if (usb_urb) {
735 if (usb_urb->transfer_flags &
736 (URB_SETUP_MAP_SINGLE | URB_DMA_MAP_SG |
737 URB_DMA_MAP_PAGE | URB_DMA_MAP_SINGLE)) {
738 hcd = dwc2_hsotg_to_hcd(hsotg);
739 usb_hcd_unmap_urb_for_dma(hcd, usb_urb);
740 }
741 if (!chan->ep_is_in)
742 memcpy(qh->dw_align_buf, bufptr,
743 chan->xfer_len);
744 } else {
745 dev_warn(hsotg->dev, "no URB in dwc2_urb\n");
746 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700747 }
748
749 chan->align_buf = qh->dw_align_buf_dma;
750 return 0;
751}
752
753/**
754 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
755 * channel and initializes the host channel to perform the transactions. The
756 * host channel is removed from the free list.
757 *
758 * @hsotg: The HCD state structure
759 * @qh: Transactions from the first QTD for this QH are selected and assigned
760 * to a free host channel
761 */
Dom Cobley20f2eb92013-09-23 14:23:34 -0700762static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700763{
764 struct dwc2_host_chan *chan;
765 struct dwc2_hcd_urb *urb;
766 struct dwc2_qtd *qtd;
767 void *bufptr = NULL;
768
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200769 if (dbg_qh(qh))
770 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700771
772 if (list_empty(&qh->qtd_list)) {
773 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -0700774 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700775 }
776
777 if (list_empty(&hsotg->free_hc_list)) {
778 dev_dbg(hsotg->dev, "No free channel to assign\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -0700779 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700780 }
781
782 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
783 hc_list_entry);
784
Dom Cobley20f2eb92013-09-23 14:23:34 -0700785 /* Remove host channel from free list */
Paul Zimmerman7359d482013-03-11 17:47:59 -0700786 list_del_init(&chan->hc_list_entry);
787
788 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
789 urb = qtd->urb;
790 qh->channel = chan;
791 qtd->in_process = 1;
792
793 /*
794 * Use usb_pipedevice to determine device address. This address is
795 * 0 before the SET_ADDRESS command and the correct address afterward.
796 */
797 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
798 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
799 chan->speed = qh->dev_speed;
800 chan->max_packet = dwc2_max_packet(qh->maxp);
801
802 chan->xfer_started = 0;
803 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
804 chan->error_state = (qtd->error_count > 0);
805 chan->halt_on_queue = 0;
806 chan->halt_pending = 0;
807 chan->requests = 0;
808
809 /*
810 * The following values may be modified in the transfer type section
811 * below. The xfer_len value may be reduced when the transfer is
812 * started to accommodate the max widths of the XferSize and PktCnt
813 * fields in the HCTSIZn register.
814 */
815
816 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
817 if (chan->ep_is_in)
818 chan->do_ping = 0;
819 else
820 chan->do_ping = qh->ping_state;
821
822 chan->data_pid_start = qh->data_toggle;
823 chan->multi_count = 1;
824
Rashika Kheriabb6c3422013-10-26 23:11:22 +0530825 if (urb->actual_length > urb->length &&
826 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
Paul Zimmerman84181082013-09-23 14:23:33 -0700827 urb->actual_length = urb->length;
828
Paul Zimmerman7359d482013-03-11 17:47:59 -0700829 if (hsotg->core_params->dma_enable > 0) {
830 chan->xfer_dma = urb->dma + urb->actual_length;
831
832 /* For non-dword aligned case */
833 if (hsotg->core_params->dma_desc_enable <= 0 &&
834 (chan->xfer_dma & 0x3))
835 bufptr = (u8 *)urb->buf + urb->actual_length;
836 } else {
837 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
838 }
839
840 chan->xfer_len = urb->length - urb->actual_length;
841 chan->xfer_count = 0;
842
843 /* Set the split attributes if required */
844 if (qh->do_split)
845 dwc2_hc_init_split(hsotg, chan, qtd, urb);
846 else
847 chan->do_split = 0;
848
849 /* Set the transfer attributes */
850 bufptr = dwc2_hc_init_xfer(hsotg, chan, qtd, bufptr);
851
852 /* Non DWORD-aligned buffer case */
853 if (bufptr) {
854 dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
Paul Zimmerman5dce9552014-09-16 13:47:27 -0700855 if (dwc2_hc_setup_align_buf(hsotg, qh, chan, urb, bufptr)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700856 dev_err(hsotg->dev,
857 "%s: Failed to allocate memory to handle non-dword aligned buffer\n",
858 __func__);
859 /* Add channel back to free list */
860 chan->align_buf = 0;
861 chan->multi_count = 0;
862 list_add_tail(&chan->hc_list_entry,
863 &hsotg->free_hc_list);
864 qtd->in_process = 0;
865 qh->channel = NULL;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700866 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700867 }
868 } else {
869 chan->align_buf = 0;
870 }
871
872 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
873 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
874 /*
875 * This value may be modified when the transfer is started
876 * to reflect the actual transfer length
877 */
878 chan->multi_count = dwc2_hb_mult(qh->maxp);
879
880 if (hsotg->core_params->dma_desc_enable > 0)
881 chan->desc_list_addr = qh->desc_list_dma;
882
883 dwc2_hc_init(hsotg, chan);
884 chan->qh = qh;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700885
886 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700887}
888
889/**
890 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
891 * schedule and assigns them to available host channels. Called from the HCD
892 * interrupt handler functions.
893 *
894 * @hsotg: The HCD state structure
895 *
896 * Return: The types of new transactions that were assigned to host channels
897 */
898enum dwc2_transaction_type dwc2_hcd_select_transactions(
899 struct dwc2_hsotg *hsotg)
900{
901 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
902 struct list_head *qh_ptr;
903 struct dwc2_qh *qh;
904 int num_channels;
905
906#ifdef DWC2_DEBUG_SOF
907 dev_vdbg(hsotg->dev, " Select Transactions\n");
908#endif
909
910 /* Process entries in the periodic ready list */
911 qh_ptr = hsotg->periodic_sched_ready.next;
912 while (qh_ptr != &hsotg->periodic_sched_ready) {
913 if (list_empty(&hsotg->free_hc_list))
914 break;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700915 if (hsotg->core_params->uframe_sched > 0) {
916 if (hsotg->available_host_channels <= 1)
917 break;
918 hsotg->available_host_channels--;
919 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700920 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
Dom Cobley20f2eb92013-09-23 14:23:34 -0700921 if (dwc2_assign_and_init_hc(hsotg, qh))
922 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700923
924 /*
925 * Move the QH from the periodic ready schedule to the
926 * periodic assigned schedule
927 */
928 qh_ptr = qh_ptr->next;
929 list_move(&qh->qh_list_entry, &hsotg->periodic_sched_assigned);
930 ret_val = DWC2_TRANSACTION_PERIODIC;
931 }
932
933 /*
934 * Process entries in the inactive portion of the non-periodic
935 * schedule. Some free host channels may not be used if they are
936 * reserved for periodic transfers.
937 */
938 num_channels = hsotg->core_params->host_channels;
939 qh_ptr = hsotg->non_periodic_sched_inactive.next;
940 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
Dom Cobley20f2eb92013-09-23 14:23:34 -0700941 if (hsotg->core_params->uframe_sched <= 0 &&
942 hsotg->non_periodic_channels >= num_channels -
Paul Zimmerman7359d482013-03-11 17:47:59 -0700943 hsotg->periodic_channels)
944 break;
945 if (list_empty(&hsotg->free_hc_list))
946 break;
947 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
Dom Cobley20f2eb92013-09-23 14:23:34 -0700948 if (hsotg->core_params->uframe_sched > 0) {
949 if (hsotg->available_host_channels < 1)
950 break;
951 hsotg->available_host_channels--;
952 }
953
954 if (dwc2_assign_and_init_hc(hsotg, qh))
955 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700956
957 /*
958 * Move the QH from the non-periodic inactive schedule to the
959 * non-periodic active schedule
960 */
961 qh_ptr = qh_ptr->next;
962 list_move(&qh->qh_list_entry,
963 &hsotg->non_periodic_sched_active);
964
965 if (ret_val == DWC2_TRANSACTION_NONE)
966 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
967 else
968 ret_val = DWC2_TRANSACTION_ALL;
969
Dom Cobley20f2eb92013-09-23 14:23:34 -0700970 if (hsotg->core_params->uframe_sched <= 0)
971 hsotg->non_periodic_channels++;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700972 }
973
974 return ret_val;
975}
976
977/**
978 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
979 * a host channel associated with either a periodic or non-periodic transfer
980 *
981 * @hsotg: The HCD state structure
982 * @chan: Host channel descriptor associated with either a periodic or
983 * non-periodic transfer
984 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
985 * for periodic transfers or the non-periodic Tx FIFO
986 * for non-periodic transfers
987 *
988 * Return: 1 if a request is queued and more requests may be needed to
989 * complete the transfer, 0 if no more requests are required for this
990 * transfer, -1 if there is insufficient space in the Tx FIFO
991 *
992 * This function assumes that there is space available in the appropriate
993 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
994 * it checks whether space is available in the appropriate Tx FIFO.
995 *
996 * Must be called with interrupt disabled and spinlock held
997 */
998static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
999 struct dwc2_host_chan *chan,
1000 u16 fifo_dwords_avail)
1001{
1002 int retval = 0;
1003
1004 if (hsotg->core_params->dma_enable > 0) {
1005 if (hsotg->core_params->dma_desc_enable > 0) {
1006 if (!chan->xfer_started ||
1007 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1008 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
1009 chan->qh->ping_state = 0;
1010 }
1011 } else if (!chan->xfer_started) {
1012 dwc2_hc_start_transfer(hsotg, chan);
1013 chan->qh->ping_state = 0;
1014 }
1015 } else if (chan->halt_pending) {
1016 /* Don't queue a request if the channel has been halted */
1017 } else if (chan->halt_on_queue) {
1018 dwc2_hc_halt(hsotg, chan, chan->halt_status);
1019 } else if (chan->do_ping) {
1020 if (!chan->xfer_started)
1021 dwc2_hc_start_transfer(hsotg, chan);
1022 } else if (!chan->ep_is_in ||
1023 chan->data_pid_start == DWC2_HC_PID_SETUP) {
1024 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
1025 if (!chan->xfer_started) {
1026 dwc2_hc_start_transfer(hsotg, chan);
1027 retval = 1;
1028 } else {
1029 retval = dwc2_hc_continue_transfer(hsotg, chan);
1030 }
1031 } else {
1032 retval = -1;
1033 }
1034 } else {
1035 if (!chan->xfer_started) {
1036 dwc2_hc_start_transfer(hsotg, chan);
1037 retval = 1;
1038 } else {
1039 retval = dwc2_hc_continue_transfer(hsotg, chan);
1040 }
1041 }
1042
1043 return retval;
1044}
1045
1046/*
1047 * Processes periodic channels for the next frame and queues transactions for
1048 * these channels to the DWC_otg controller. After queueing transactions, the
1049 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
1050 * to queue as Periodic Tx FIFO or request queue space becomes available.
1051 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
1052 *
1053 * Must be called with interrupt disabled and spinlock held
1054 */
1055static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
1056{
1057 struct list_head *qh_ptr;
1058 struct dwc2_qh *qh;
1059 u32 tx_status;
1060 u32 fspcavail;
1061 u32 gintmsk;
1062 int status;
1063 int no_queue_space = 0;
1064 int no_fifo_space = 0;
1065 u32 qspcavail;
1066
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001067 if (dbg_perio())
1068 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07001069
1070 tx_status = readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001071 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1072 TXSTS_QSPCAVAIL_SHIFT;
1073 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1074 TXSTS_FSPCAVAIL_SHIFT;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001075
1076 if (dbg_perio()) {
1077 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
1078 qspcavail);
1079 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
1080 fspcavail);
1081 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001082
1083 qh_ptr = hsotg->periodic_sched_assigned.next;
1084 while (qh_ptr != &hsotg->periodic_sched_assigned) {
1085 tx_status = readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmanacdb9042013-08-30 18:45:16 +02001086 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1087 TXSTS_QSPCAVAIL_SHIFT;
1088 if (qspcavail == 0) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001089 no_queue_space = 1;
1090 break;
1091 }
1092
1093 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
1094 if (!qh->channel) {
1095 qh_ptr = qh_ptr->next;
1096 continue;
1097 }
1098
1099 /* Make sure EP's TT buffer is clean before queueing qtds */
1100 if (qh->tt_buffer_dirty) {
1101 qh_ptr = qh_ptr->next;
1102 continue;
1103 }
1104
1105 /*
1106 * Set a flag if we're queuing high-bandwidth in slave mode.
1107 * The flag prevents any halts to get into the request queue in
1108 * the middle of multiple high-bandwidth packets getting queued.
1109 */
1110 if (hsotg->core_params->dma_enable <= 0 &&
1111 qh->channel->multi_count > 1)
1112 hsotg->queuing_high_bandwidth = 1;
1113
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001114 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1115 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001116 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
1117 if (status < 0) {
1118 no_fifo_space = 1;
1119 break;
1120 }
1121
1122 /*
1123 * In Slave mode, stay on the current transfer until there is
1124 * nothing more to do or the high-bandwidth request count is
1125 * reached. In DMA mode, only need to queue one request. The
1126 * controller automatically handles multiple packets for
1127 * high-bandwidth transfers.
1128 */
1129 if (hsotg->core_params->dma_enable > 0 || status == 0 ||
1130 qh->channel->requests == qh->channel->multi_count) {
1131 qh_ptr = qh_ptr->next;
1132 /*
1133 * Move the QH from the periodic assigned schedule to
1134 * the periodic queued schedule
1135 */
1136 list_move(&qh->qh_list_entry,
1137 &hsotg->periodic_sched_queued);
1138
1139 /* done queuing high bandwidth */
1140 hsotg->queuing_high_bandwidth = 0;
1141 }
1142 }
1143
1144 if (hsotg->core_params->dma_enable <= 0) {
1145 tx_status = readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001146 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1147 TXSTS_QSPCAVAIL_SHIFT;
1148 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1149 TXSTS_FSPCAVAIL_SHIFT;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001150 if (dbg_perio()) {
1151 dev_vdbg(hsotg->dev,
1152 " P Tx Req Queue Space Avail (after queue): %d\n",
1153 qspcavail);
1154 dev_vdbg(hsotg->dev,
1155 " P Tx FIFO Space Avail (after queue): %d\n",
1156 fspcavail);
1157 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001158
1159 if (!list_empty(&hsotg->periodic_sched_assigned) ||
1160 no_queue_space || no_fifo_space) {
1161 /*
1162 * May need to queue more transactions as the request
1163 * queue or Tx FIFO empties. Enable the periodic Tx
1164 * FIFO empty interrupt. (Always use the half-empty
1165 * level to ensure that new requests are loaded as
1166 * soon as possible.)
1167 */
1168 gintmsk = readl(hsotg->regs + GINTMSK);
1169 gintmsk |= GINTSTS_PTXFEMP;
1170 writel(gintmsk, hsotg->regs + GINTMSK);
1171 } else {
1172 /*
1173 * Disable the Tx FIFO empty interrupt since there are
1174 * no more transactions that need to be queued right
1175 * now. This function is called from interrupt
1176 * handlers to queue more transactions as transfer
1177 * states change.
1178 */
1179 gintmsk = readl(hsotg->regs + GINTMSK);
1180 gintmsk &= ~GINTSTS_PTXFEMP;
1181 writel(gintmsk, hsotg->regs + GINTMSK);
1182 }
1183 }
1184}
1185
1186/*
1187 * Processes active non-periodic channels and queues transactions for these
1188 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
1189 * FIFO Empty interrupt is enabled if there are more transactions to queue as
1190 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
1191 * FIFO Empty interrupt is disabled.
1192 *
1193 * Must be called with interrupt disabled and spinlock held
1194 */
1195static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
1196{
1197 struct list_head *orig_qh_ptr;
1198 struct dwc2_qh *qh;
1199 u32 tx_status;
1200 u32 qspcavail;
1201 u32 fspcavail;
1202 u32 gintmsk;
1203 int status;
1204 int no_queue_space = 0;
1205 int no_fifo_space = 0;
1206 int more_to_do = 0;
1207
1208 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
1209
1210 tx_status = readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001211 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1212 TXSTS_QSPCAVAIL_SHIFT;
1213 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1214 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001215 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
1216 qspcavail);
1217 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
1218 fspcavail);
1219
1220 /*
1221 * Keep track of the starting point. Skip over the start-of-list
1222 * entry.
1223 */
1224 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
1225 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
1226 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
1227
1228 /*
1229 * Process once through the active list or until no more space is
1230 * available in the request queue or the Tx FIFO
1231 */
1232 do {
1233 tx_status = readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001234 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1235 TXSTS_QSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001236 if (hsotg->core_params->dma_enable <= 0 && qspcavail == 0) {
1237 no_queue_space = 1;
1238 break;
1239 }
1240
1241 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
1242 qh_list_entry);
1243 if (!qh->channel)
1244 goto next;
1245
1246 /* Make sure EP's TT buffer is clean before queueing qtds */
1247 if (qh->tt_buffer_dirty)
1248 goto next;
1249
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001250 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1251 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001252 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
1253
1254 if (status > 0) {
1255 more_to_do = 1;
1256 } else if (status < 0) {
1257 no_fifo_space = 1;
1258 break;
1259 }
1260next:
1261 /* Advance to next QH, skipping start-of-list entry */
1262 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
1263 if (hsotg->non_periodic_qh_ptr ==
1264 &hsotg->non_periodic_sched_active)
1265 hsotg->non_periodic_qh_ptr =
1266 hsotg->non_periodic_qh_ptr->next;
1267 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
1268
1269 if (hsotg->core_params->dma_enable <= 0) {
1270 tx_status = readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001271 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1272 TXSTS_QSPCAVAIL_SHIFT;
1273 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1274 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001275 dev_vdbg(hsotg->dev,
1276 " NP Tx Req Queue Space Avail (after queue): %d\n",
1277 qspcavail);
1278 dev_vdbg(hsotg->dev,
1279 " NP Tx FIFO Space Avail (after queue): %d\n",
1280 fspcavail);
1281
1282 if (more_to_do || no_queue_space || no_fifo_space) {
1283 /*
1284 * May need to queue more transactions as the request
1285 * queue or Tx FIFO empties. Enable the non-periodic
1286 * Tx FIFO empty interrupt. (Always use the half-empty
1287 * level to ensure that new requests are loaded as
1288 * soon as possible.)
1289 */
1290 gintmsk = readl(hsotg->regs + GINTMSK);
1291 gintmsk |= GINTSTS_NPTXFEMP;
1292 writel(gintmsk, hsotg->regs + GINTMSK);
1293 } else {
1294 /*
1295 * Disable the Tx FIFO empty interrupt since there are
1296 * no more transactions that need to be queued right
1297 * now. This function is called from interrupt
1298 * handlers to queue more transactions as transfer
1299 * states change.
1300 */
1301 gintmsk = readl(hsotg->regs + GINTMSK);
1302 gintmsk &= ~GINTSTS_NPTXFEMP;
1303 writel(gintmsk, hsotg->regs + GINTMSK);
1304 }
1305 }
1306}
1307
1308/**
1309 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
1310 * and queues transactions for these channels to the DWC_otg controller. Called
1311 * from the HCD interrupt handler functions.
1312 *
1313 * @hsotg: The HCD state structure
1314 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
1315 * or both)
1316 *
1317 * Must be called with interrupt disabled and spinlock held
1318 */
1319void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
1320 enum dwc2_transaction_type tr_type)
1321{
1322#ifdef DWC2_DEBUG_SOF
1323 dev_vdbg(hsotg->dev, "Queue Transactions\n");
1324#endif
1325 /* Process host channels associated with periodic transfers */
1326 if ((tr_type == DWC2_TRANSACTION_PERIODIC ||
1327 tr_type == DWC2_TRANSACTION_ALL) &&
1328 !list_empty(&hsotg->periodic_sched_assigned))
1329 dwc2_process_periodic_channels(hsotg);
1330
1331 /* Process host channels associated with non-periodic transfers */
1332 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
1333 tr_type == DWC2_TRANSACTION_ALL) {
1334 if (!list_empty(&hsotg->non_periodic_sched_active)) {
1335 dwc2_process_non_periodic_channels(hsotg);
1336 } else {
1337 /*
1338 * Ensure NP Tx FIFO empty interrupt is disabled when
1339 * there are no non-periodic transfers to process
1340 */
1341 u32 gintmsk = readl(hsotg->regs + GINTMSK);
1342
1343 gintmsk &= ~GINTSTS_NPTXFEMP;
1344 writel(gintmsk, hsotg->regs + GINTMSK);
1345 }
1346 }
1347}
1348
1349static void dwc2_conn_id_status_change(struct work_struct *work)
1350{
1351 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
1352 wf_otg);
1353 u32 count = 0;
1354 u32 gotgctl;
1355
1356 dev_dbg(hsotg->dev, "%s()\n", __func__);
1357
1358 gotgctl = readl(hsotg->regs + GOTGCTL);
1359 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
1360 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
1361 !!(gotgctl & GOTGCTL_CONID_B));
1362
1363 /* B-Device connector (Device Mode) */
1364 if (gotgctl & GOTGCTL_CONID_B) {
1365 /* Wait for switch to device mode */
1366 dev_dbg(hsotg->dev, "connId B\n");
1367 while (!dwc2_is_device_mode(hsotg)) {
1368 dev_info(hsotg->dev,
1369 "Waiting for Peripheral Mode, Mode=%s\n",
1370 dwc2_is_host_mode(hsotg) ? "Host" :
1371 "Peripheral");
1372 usleep_range(20000, 40000);
1373 if (++count > 250)
1374 break;
1375 }
1376 if (count > 250)
1377 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07001378 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07001379 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Matthijs Kooijman6706c722013-04-11 17:52:41 +02001380 dwc2_core_init(hsotg, false, -1);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001381 dwc2_enable_global_interrupts(hsotg);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01001382 s3c_hsotg_core_init_disconnected(hsotg, false);
Dinh Nguyen510ffaa2014-11-11 11:13:36 -06001383 s3c_hsotg_core_connect(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001384 } else {
1385 /* A-Device connector (Host Mode) */
1386 dev_dbg(hsotg->dev, "connId A\n");
1387 while (!dwc2_is_host_mode(hsotg)) {
1388 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
1389 dwc2_is_host_mode(hsotg) ?
1390 "Host" : "Peripheral");
1391 usleep_range(20000, 40000);
1392 if (++count > 250)
1393 break;
1394 }
1395 if (count > 250)
1396 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07001397 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07001398 hsotg->op_state = OTG_STATE_A_HOST;
1399
1400 /* Initialize the Core for Host mode */
Matthijs Kooijman6706c722013-04-11 17:52:41 +02001401 dwc2_core_init(hsotg, false, -1);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001402 dwc2_enable_global_interrupts(hsotg);
1403 dwc2_hcd_start(hsotg);
1404 }
1405}
1406
1407static void dwc2_wakeup_detected(unsigned long data)
1408{
1409 struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)data;
1410 u32 hprt0;
1411
1412 dev_dbg(hsotg->dev, "%s()\n", __func__);
1413
1414 /*
1415 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
1416 * so that OPT tests pass with all PHYs.)
1417 */
1418 hprt0 = dwc2_read_hprt0(hsotg);
1419 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
1420 hprt0 &= ~HPRT0_RES;
1421 writel(hprt0, hsotg->regs + HPRT0);
1422 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
1423 readl(hsotg->regs + HPRT0));
1424
1425 dwc2_hcd_rem_wakeup(hsotg);
1426
1427 /* Change to L0 state */
1428 hsotg->lx_state = DWC2_L0;
1429}
1430
1431static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
1432{
1433 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
1434
1435 return hcd->self.b_hnp_enable;
1436}
1437
1438/* Must NOT be called with interrupt disabled or spinlock held */
1439static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
1440{
1441 unsigned long flags;
1442 u32 hprt0;
1443 u32 pcgctl;
1444 u32 gotgctl;
1445
1446 dev_dbg(hsotg->dev, "%s()\n", __func__);
1447
1448 spin_lock_irqsave(&hsotg->lock, flags);
1449
1450 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
1451 gotgctl = readl(hsotg->regs + GOTGCTL);
1452 gotgctl |= GOTGCTL_HSTSETHNPEN;
1453 writel(gotgctl, hsotg->regs + GOTGCTL);
1454 hsotg->op_state = OTG_STATE_A_SUSPEND;
1455 }
1456
1457 hprt0 = dwc2_read_hprt0(hsotg);
1458 hprt0 |= HPRT0_SUSP;
1459 writel(hprt0, hsotg->regs + HPRT0);
1460
1461 /* Update lx_state */
1462 hsotg->lx_state = DWC2_L2;
1463
1464 /* Suspend the Phy Clock */
1465 pcgctl = readl(hsotg->regs + PCGCTL);
1466 pcgctl |= PCGCTL_STOPPCLK;
1467 writel(pcgctl, hsotg->regs + PCGCTL);
1468 udelay(10);
1469
1470 /* For HNP the bus must be suspended for at least 200ms */
1471 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
1472 pcgctl = readl(hsotg->regs + PCGCTL);
1473 pcgctl &= ~PCGCTL_STOPPCLK;
1474 writel(pcgctl, hsotg->regs + PCGCTL);
1475
1476 spin_unlock_irqrestore(&hsotg->lock, flags);
1477
1478 usleep_range(200000, 250000);
1479 } else {
1480 spin_unlock_irqrestore(&hsotg->lock, flags);
1481 }
1482}
1483
1484/* Handles hub class-specific requests */
1485static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
1486 u16 wvalue, u16 windex, char *buf, u16 wlength)
1487{
1488 struct usb_hub_descriptor *hub_desc;
1489 int retval = 0;
1490 u32 hprt0;
1491 u32 port_status;
1492 u32 speed;
1493 u32 pcgctl;
1494
1495 switch (typereq) {
1496 case ClearHubFeature:
1497 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
1498
1499 switch (wvalue) {
1500 case C_HUB_LOCAL_POWER:
1501 case C_HUB_OVER_CURRENT:
1502 /* Nothing required here */
1503 break;
1504
1505 default:
1506 retval = -EINVAL;
1507 dev_err(hsotg->dev,
1508 "ClearHubFeature request %1xh unknown\n",
1509 wvalue);
1510 }
1511 break;
1512
1513 case ClearPortFeature:
1514 if (wvalue != USB_PORT_FEAT_L1)
1515 if (!windex || windex > 1)
1516 goto error;
1517 switch (wvalue) {
1518 case USB_PORT_FEAT_ENABLE:
1519 dev_dbg(hsotg->dev,
1520 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
1521 hprt0 = dwc2_read_hprt0(hsotg);
1522 hprt0 |= HPRT0_ENA;
1523 writel(hprt0, hsotg->regs + HPRT0);
1524 break;
1525
1526 case USB_PORT_FEAT_SUSPEND:
1527 dev_dbg(hsotg->dev,
1528 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
Paul Zimmermanb0bb9bb2015-01-15 19:21:46 +00001529 writel(0, hsotg->regs + PCGCTL);
Gregory Herrero42a66302015-04-03 10:53:25 +02001530 usleep_range(20000, 40000);
Paul Zimmermanb0bb9bb2015-01-15 19:21:46 +00001531
1532 hprt0 = dwc2_read_hprt0(hsotg);
1533 hprt0 |= HPRT0_RES;
1534 writel(hprt0, hsotg->regs + HPRT0);
1535 hprt0 &= ~HPRT0_SUSP;
Gregory Herrero42a66302015-04-03 10:53:25 +02001536 msleep(USB_RESUME_TIMEOUT);
Paul Zimmermanb0bb9bb2015-01-15 19:21:46 +00001537
1538 hprt0 &= ~HPRT0_RES;
1539 writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001540 break;
1541
1542 case USB_PORT_FEAT_POWER:
1543 dev_dbg(hsotg->dev,
1544 "ClearPortFeature USB_PORT_FEAT_POWER\n");
1545 hprt0 = dwc2_read_hprt0(hsotg);
1546 hprt0 &= ~HPRT0_PWR;
1547 writel(hprt0, hsotg->regs + HPRT0);
1548 break;
1549
1550 case USB_PORT_FEAT_INDICATOR:
1551 dev_dbg(hsotg->dev,
1552 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
1553 /* Port indicator not supported */
1554 break;
1555
1556 case USB_PORT_FEAT_C_CONNECTION:
1557 /*
1558 * Clears driver's internal Connect Status Change flag
1559 */
1560 dev_dbg(hsotg->dev,
1561 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
1562 hsotg->flags.b.port_connect_status_change = 0;
1563 break;
1564
1565 case USB_PORT_FEAT_C_RESET:
1566 /* Clears driver's internal Port Reset Change flag */
1567 dev_dbg(hsotg->dev,
1568 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
1569 hsotg->flags.b.port_reset_change = 0;
1570 break;
1571
1572 case USB_PORT_FEAT_C_ENABLE:
1573 /*
1574 * Clears the driver's internal Port Enable/Disable
1575 * Change flag
1576 */
1577 dev_dbg(hsotg->dev,
1578 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
1579 hsotg->flags.b.port_enable_change = 0;
1580 break;
1581
1582 case USB_PORT_FEAT_C_SUSPEND:
1583 /*
1584 * Clears the driver's internal Port Suspend Change
1585 * flag, which is set when resume signaling on the host
1586 * port is complete
1587 */
1588 dev_dbg(hsotg->dev,
1589 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
1590 hsotg->flags.b.port_suspend_change = 0;
1591 break;
1592
1593 case USB_PORT_FEAT_C_PORT_L1:
1594 dev_dbg(hsotg->dev,
1595 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
1596 hsotg->flags.b.port_l1_change = 0;
1597 break;
1598
1599 case USB_PORT_FEAT_C_OVER_CURRENT:
1600 dev_dbg(hsotg->dev,
1601 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
1602 hsotg->flags.b.port_over_current_change = 0;
1603 break;
1604
1605 default:
1606 retval = -EINVAL;
1607 dev_err(hsotg->dev,
1608 "ClearPortFeature request %1xh unknown or unsupported\n",
1609 wvalue);
1610 }
1611 break;
1612
1613 case GetHubDescriptor:
1614 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
1615 hub_desc = (struct usb_hub_descriptor *)buf;
1616 hub_desc->bDescLength = 9;
Sergei Shtylyova5dd0392015-03-29 01:36:28 +03001617 hub_desc->bDescriptorType = USB_DT_HUB;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001618 hub_desc->bNbrPorts = 1;
Sergei Shtylyov3d040de2015-01-19 01:54:15 +03001619 hub_desc->wHubCharacteristics =
1620 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
1621 HUB_CHAR_INDV_PORT_OCPM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001622 hub_desc->bPwrOn2PwrGood = 1;
1623 hub_desc->bHubContrCurrent = 0;
1624 hub_desc->u.hs.DeviceRemovable[0] = 0;
1625 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
1626 break;
1627
1628 case GetHubStatus:
1629 dev_dbg(hsotg->dev, "GetHubStatus\n");
1630 memset(buf, 0, 4);
1631 break;
1632
1633 case GetPortStatus:
Paul Zimmermanb8313412013-05-24 16:32:12 -07001634 dev_vdbg(hsotg->dev,
1635 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
1636 hsotg->flags.d32);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001637 if (!windex || windex > 1)
1638 goto error;
1639
1640 port_status = 0;
1641 if (hsotg->flags.b.port_connect_status_change)
1642 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
1643 if (hsotg->flags.b.port_enable_change)
1644 port_status |= USB_PORT_STAT_C_ENABLE << 16;
1645 if (hsotg->flags.b.port_suspend_change)
1646 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
1647 if (hsotg->flags.b.port_l1_change)
1648 port_status |= USB_PORT_STAT_C_L1 << 16;
1649 if (hsotg->flags.b.port_reset_change)
1650 port_status |= USB_PORT_STAT_C_RESET << 16;
1651 if (hsotg->flags.b.port_over_current_change) {
1652 dev_warn(hsotg->dev, "Overcurrent change detected\n");
1653 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1654 }
1655
1656 if (!hsotg->flags.b.port_connect_status) {
1657 /*
1658 * The port is disconnected, which means the core is
1659 * either in device mode or it soon will be. Just
1660 * return 0's for the remainder of the port status
1661 * since the port register can't be read if the core
1662 * is in device mode.
1663 */
1664 *(__le32 *)buf = cpu_to_le32(port_status);
1665 break;
1666 }
1667
1668 hprt0 = readl(hsotg->regs + HPRT0);
Paul Zimmermanb8313412013-05-24 16:32:12 -07001669 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001670
1671 if (hprt0 & HPRT0_CONNSTS)
1672 port_status |= USB_PORT_STAT_CONNECTION;
1673 if (hprt0 & HPRT0_ENA)
1674 port_status |= USB_PORT_STAT_ENABLE;
1675 if (hprt0 & HPRT0_SUSP)
1676 port_status |= USB_PORT_STAT_SUSPEND;
1677 if (hprt0 & HPRT0_OVRCURRACT)
1678 port_status |= USB_PORT_STAT_OVERCURRENT;
1679 if (hprt0 & HPRT0_RST)
1680 port_status |= USB_PORT_STAT_RESET;
1681 if (hprt0 & HPRT0_PWR)
1682 port_status |= USB_PORT_STAT_POWER;
1683
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02001684 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001685 if (speed == HPRT0_SPD_HIGH_SPEED)
1686 port_status |= USB_PORT_STAT_HIGH_SPEED;
1687 else if (speed == HPRT0_SPD_LOW_SPEED)
1688 port_status |= USB_PORT_STAT_LOW_SPEED;
1689
1690 if (hprt0 & HPRT0_TSTCTL_MASK)
1691 port_status |= USB_PORT_STAT_TEST;
1692 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
1693
Paul Zimmermanb8313412013-05-24 16:32:12 -07001694 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001695 *(__le32 *)buf = cpu_to_le32(port_status);
1696 break;
1697
1698 case SetHubFeature:
1699 dev_dbg(hsotg->dev, "SetHubFeature\n");
1700 /* No HUB features supported */
1701 break;
1702
1703 case SetPortFeature:
1704 dev_dbg(hsotg->dev, "SetPortFeature\n");
1705 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
1706 goto error;
1707
1708 if (!hsotg->flags.b.port_connect_status) {
1709 /*
1710 * The port is disconnected, which means the core is
1711 * either in device mode or it soon will be. Just
1712 * return without doing anything since the port
1713 * register can't be written if the core is in device
1714 * mode.
1715 */
1716 break;
1717 }
1718
1719 switch (wvalue) {
1720 case USB_PORT_FEAT_SUSPEND:
1721 dev_dbg(hsotg->dev,
1722 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
1723 if (windex != hsotg->otg_port)
1724 goto error;
1725 dwc2_port_suspend(hsotg, windex);
1726 break;
1727
1728 case USB_PORT_FEAT_POWER:
1729 dev_dbg(hsotg->dev,
1730 "SetPortFeature - USB_PORT_FEAT_POWER\n");
1731 hprt0 = dwc2_read_hprt0(hsotg);
1732 hprt0 |= HPRT0_PWR;
1733 writel(hprt0, hsotg->regs + HPRT0);
1734 break;
1735
1736 case USB_PORT_FEAT_RESET:
1737 hprt0 = dwc2_read_hprt0(hsotg);
1738 dev_dbg(hsotg->dev,
1739 "SetPortFeature - USB_PORT_FEAT_RESET\n");
1740 pcgctl = readl(hsotg->regs + PCGCTL);
1741 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
1742 writel(pcgctl, hsotg->regs + PCGCTL);
1743 /* ??? Original driver does this */
1744 writel(0, hsotg->regs + PCGCTL);
1745
1746 hprt0 = dwc2_read_hprt0(hsotg);
1747 /* Clear suspend bit if resetting from suspend state */
1748 hprt0 &= ~HPRT0_SUSP;
1749
1750 /*
1751 * When B-Host the Port reset bit is set in the Start
1752 * HCD Callback function, so that the reset is started
1753 * within 1ms of the HNP success interrupt
1754 */
1755 if (!dwc2_hcd_is_b_host(hsotg)) {
1756 hprt0 |= HPRT0_PWR | HPRT0_RST;
1757 dev_dbg(hsotg->dev,
1758 "In host mode, hprt0=%08x\n", hprt0);
1759 writel(hprt0, hsotg->regs + HPRT0);
1760 }
1761
1762 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
1763 usleep_range(50000, 70000);
1764 hprt0 &= ~HPRT0_RST;
1765 writel(hprt0, hsotg->regs + HPRT0);
1766 hsotg->lx_state = DWC2_L0; /* Now back to On state */
1767 break;
1768
1769 case USB_PORT_FEAT_INDICATOR:
1770 dev_dbg(hsotg->dev,
1771 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
1772 /* Not supported */
1773 break;
1774
1775 default:
1776 retval = -EINVAL;
1777 dev_err(hsotg->dev,
1778 "SetPortFeature %1xh unknown or unsupported\n",
1779 wvalue);
1780 break;
1781 }
1782 break;
1783
1784 default:
1785error:
1786 retval = -EINVAL;
1787 dev_dbg(hsotg->dev,
1788 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
1789 typereq, windex, wvalue);
1790 break;
1791 }
1792
1793 return retval;
1794}
1795
1796static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
1797{
1798 int retval;
1799
Paul Zimmerman7359d482013-03-11 17:47:59 -07001800 if (port != 1)
1801 return -EINVAL;
1802
1803 retval = (hsotg->flags.b.port_connect_status_change ||
1804 hsotg->flags.b.port_reset_change ||
1805 hsotg->flags.b.port_enable_change ||
1806 hsotg->flags.b.port_suspend_change ||
1807 hsotg->flags.b.port_over_current_change);
1808
1809 if (retval) {
1810 dev_dbg(hsotg->dev,
1811 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
1812 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
1813 hsotg->flags.b.port_connect_status_change);
1814 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
1815 hsotg->flags.b.port_reset_change);
1816 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
1817 hsotg->flags.b.port_enable_change);
1818 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
1819 hsotg->flags.b.port_suspend_change);
1820 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
1821 hsotg->flags.b.port_over_current_change);
1822 }
1823
1824 return retval;
1825}
1826
1827int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
1828{
1829 u32 hfnum = readl(hsotg->regs + HFNUM);
1830
1831#ifdef DWC2_DEBUG_SOF
1832 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001833 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001834#endif
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001835 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001836}
1837
1838int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
1839{
Aldo Iljazi6bf2e2a2013-11-30 19:33:57 +02001840 return hsotg->op_state == OTG_STATE_B_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001841}
1842
1843static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
1844 int iso_desc_count,
1845 gfp_t mem_flags)
1846{
1847 struct dwc2_hcd_urb *urb;
1848 u32 size = sizeof(*urb) + iso_desc_count *
1849 sizeof(struct dwc2_hcd_iso_packet_desc);
1850
1851 urb = kzalloc(size, mem_flags);
1852 if (urb)
1853 urb->packet_count = iso_desc_count;
1854 return urb;
1855}
1856
1857static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
1858 struct dwc2_hcd_urb *urb, u8 dev_addr,
1859 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
1860{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001861 if (dbg_perio() ||
1862 ep_type == USB_ENDPOINT_XFER_BULK ||
1863 ep_type == USB_ENDPOINT_XFER_CONTROL)
1864 dev_vdbg(hsotg->dev,
1865 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
1866 dev_addr, ep_num, ep_dir, ep_type, mps);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001867 urb->pipe_info.dev_addr = dev_addr;
1868 urb->pipe_info.ep_num = ep_num;
1869 urb->pipe_info.pipe_type = ep_type;
1870 urb->pipe_info.pipe_dir = ep_dir;
1871 urb->pipe_info.mps = mps;
1872}
1873
1874/*
1875 * NOTE: This function will be removed once the peripheral controller code
1876 * is integrated and the driver is stable
1877 */
1878void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
1879{
1880#ifdef DEBUG
1881 struct dwc2_host_chan *chan;
1882 struct dwc2_hcd_urb *urb;
1883 struct dwc2_qtd *qtd;
1884 int num_channels;
1885 u32 np_tx_status;
1886 u32 p_tx_status;
1887 int i;
1888
1889 num_channels = hsotg->core_params->host_channels;
1890 dev_dbg(hsotg->dev, "\n");
1891 dev_dbg(hsotg->dev,
1892 "************************************************************\n");
1893 dev_dbg(hsotg->dev, "HCD State:\n");
1894 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
1895
1896 for (i = 0; i < num_channels; i++) {
1897 chan = hsotg->hc_ptr_array[i];
1898 dev_dbg(hsotg->dev, " Channel %d:\n", i);
1899 dev_dbg(hsotg->dev,
1900 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
1901 chan->dev_addr, chan->ep_num, chan->ep_is_in);
1902 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
1903 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
1904 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
1905 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
1906 chan->data_pid_start);
1907 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
1908 dev_dbg(hsotg->dev, " xfer_started: %d\n",
1909 chan->xfer_started);
1910 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
1911 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
1912 (unsigned long)chan->xfer_dma);
1913 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
1914 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
1915 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
1916 chan->halt_on_queue);
1917 dev_dbg(hsotg->dev, " halt_pending: %d\n",
1918 chan->halt_pending);
1919 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
1920 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
1921 dev_dbg(hsotg->dev, " complete_split: %d\n",
1922 chan->complete_split);
1923 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
1924 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
1925 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
1926 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
1927 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
1928
1929 if (chan->xfer_started) {
1930 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
1931
1932 hfnum = readl(hsotg->regs + HFNUM);
1933 hcchar = readl(hsotg->regs + HCCHAR(i));
1934 hctsiz = readl(hsotg->regs + HCTSIZ(i));
1935 hcint = readl(hsotg->regs + HCINT(i));
1936 hcintmsk = readl(hsotg->regs + HCINTMSK(i));
1937 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
1938 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
1939 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
1940 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
1941 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
1942 }
1943
1944 if (!(chan->xfer_started && chan->qh))
1945 continue;
1946
1947 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
1948 if (!qtd->in_process)
1949 break;
1950 urb = qtd->urb;
1951 dev_dbg(hsotg->dev, " URB Info:\n");
1952 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
1953 qtd, urb);
1954 if (urb) {
1955 dev_dbg(hsotg->dev,
1956 " Dev: %d, EP: %d %s\n",
1957 dwc2_hcd_get_dev_addr(&urb->pipe_info),
1958 dwc2_hcd_get_ep_num(&urb->pipe_info),
1959 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
1960 "IN" : "OUT");
1961 dev_dbg(hsotg->dev,
1962 " Max packet size: %d\n",
1963 dwc2_hcd_get_mps(&urb->pipe_info));
1964 dev_dbg(hsotg->dev,
1965 " transfer_buffer: %p\n",
1966 urb->buf);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07001967 dev_dbg(hsotg->dev,
1968 " transfer_dma: %08lx\n",
1969 (unsigned long)urb->dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001970 dev_dbg(hsotg->dev,
1971 " transfer_buffer_length: %d\n",
1972 urb->length);
1973 dev_dbg(hsotg->dev, " actual_length: %d\n",
1974 urb->actual_length);
1975 }
1976 }
1977 }
1978
1979 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
1980 hsotg->non_periodic_channels);
1981 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
1982 hsotg->periodic_channels);
1983 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
1984 np_tx_status = readl(hsotg->regs + GNPTXSTS);
1985 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001986 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001987 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001988 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001989 p_tx_status = readl(hsotg->regs + HPTXSTS);
1990 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001991 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001992 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001993 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001994 dwc2_hcd_dump_frrem(hsotg);
1995 dwc2_dump_global_registers(hsotg);
1996 dwc2_dump_host_registers(hsotg);
1997 dev_dbg(hsotg->dev,
1998 "************************************************************\n");
1999 dev_dbg(hsotg->dev, "\n");
2000#endif
2001}
2002
2003/*
2004 * NOTE: This function will be removed once the peripheral controller code
2005 * is integrated and the driver is stable
2006 */
2007void dwc2_hcd_dump_frrem(struct dwc2_hsotg *hsotg)
2008{
2009#ifdef DWC2_DUMP_FRREM
2010 dev_dbg(hsotg->dev, "Frame remaining at SOF:\n");
2011 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2012 hsotg->frrem_samples, hsotg->frrem_accum,
2013 hsotg->frrem_samples > 0 ?
2014 hsotg->frrem_accum / hsotg->frrem_samples : 0);
2015 dev_dbg(hsotg->dev, "\n");
2016 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 7):\n");
2017 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2018 hsotg->hfnum_7_samples,
2019 hsotg->hfnum_7_frrem_accum,
2020 hsotg->hfnum_7_samples > 0 ?
2021 hsotg->hfnum_7_frrem_accum / hsotg->hfnum_7_samples : 0);
2022 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 0):\n");
2023 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2024 hsotg->hfnum_0_samples,
2025 hsotg->hfnum_0_frrem_accum,
2026 hsotg->hfnum_0_samples > 0 ?
2027 hsotg->hfnum_0_frrem_accum / hsotg->hfnum_0_samples : 0);
2028 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 1-6):\n");
2029 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2030 hsotg->hfnum_other_samples,
2031 hsotg->hfnum_other_frrem_accum,
2032 hsotg->hfnum_other_samples > 0 ?
2033 hsotg->hfnum_other_frrem_accum / hsotg->hfnum_other_samples :
2034 0);
2035 dev_dbg(hsotg->dev, "\n");
2036 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 7):\n");
2037 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2038 hsotg->hfnum_7_samples_a, hsotg->hfnum_7_frrem_accum_a,
2039 hsotg->hfnum_7_samples_a > 0 ?
2040 hsotg->hfnum_7_frrem_accum_a / hsotg->hfnum_7_samples_a : 0);
2041 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 0):\n");
2042 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2043 hsotg->hfnum_0_samples_a, hsotg->hfnum_0_frrem_accum_a,
2044 hsotg->hfnum_0_samples_a > 0 ?
2045 hsotg->hfnum_0_frrem_accum_a / hsotg->hfnum_0_samples_a : 0);
2046 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 1-6):\n");
2047 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2048 hsotg->hfnum_other_samples_a, hsotg->hfnum_other_frrem_accum_a,
2049 hsotg->hfnum_other_samples_a > 0 ?
2050 hsotg->hfnum_other_frrem_accum_a / hsotg->hfnum_other_samples_a
2051 : 0);
2052 dev_dbg(hsotg->dev, "\n");
2053 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 7):\n");
2054 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2055 hsotg->hfnum_7_samples_b, hsotg->hfnum_7_frrem_accum_b,
2056 hsotg->hfnum_7_samples_b > 0 ?
2057 hsotg->hfnum_7_frrem_accum_b / hsotg->hfnum_7_samples_b : 0);
2058 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 0):\n");
2059 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2060 hsotg->hfnum_0_samples_b, hsotg->hfnum_0_frrem_accum_b,
2061 (hsotg->hfnum_0_samples_b > 0) ?
2062 hsotg->hfnum_0_frrem_accum_b / hsotg->hfnum_0_samples_b : 0);
2063 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 1-6):\n");
2064 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2065 hsotg->hfnum_other_samples_b, hsotg->hfnum_other_frrem_accum_b,
2066 (hsotg->hfnum_other_samples_b > 0) ?
2067 hsotg->hfnum_other_frrem_accum_b / hsotg->hfnum_other_samples_b
2068 : 0);
2069#endif
2070}
2071
2072struct wrapper_priv_data {
2073 struct dwc2_hsotg *hsotg;
2074};
2075
2076/* Gets the dwc2_hsotg from a usb_hcd */
2077static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
2078{
2079 struct wrapper_priv_data *p;
2080
2081 p = (struct wrapper_priv_data *) &hcd->hcd_priv;
2082 return p->hsotg;
2083}
2084
2085static int _dwc2_hcd_start(struct usb_hcd *hcd);
2086
2087void dwc2_host_start(struct dwc2_hsotg *hsotg)
2088{
2089 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
2090
2091 hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
2092 _dwc2_hcd_start(hcd);
2093}
2094
2095void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
2096{
2097 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
2098
2099 hcd->self.is_b_host = 0;
2100}
2101
2102void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, int *hub_addr,
2103 int *hub_port)
2104{
2105 struct urb *urb = context;
2106
2107 if (urb->dev->tt)
2108 *hub_addr = urb->dev->tt->hub->devnum;
2109 else
2110 *hub_addr = 0;
2111 *hub_port = urb->dev->ttport;
2112}
2113
2114int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
2115{
2116 struct urb *urb = context;
2117
2118 return urb->dev->speed;
2119}
2120
2121static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
2122 struct urb *urb)
2123{
2124 struct usb_bus *bus = hcd_to_bus(hcd);
2125
2126 if (urb->interval)
2127 bus->bandwidth_allocated += bw / urb->interval;
2128 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2129 bus->bandwidth_isoc_reqs++;
2130 else
2131 bus->bandwidth_int_reqs++;
2132}
2133
2134static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
2135 struct urb *urb)
2136{
2137 struct usb_bus *bus = hcd_to_bus(hcd);
2138
2139 if (urb->interval)
2140 bus->bandwidth_allocated -= bw / urb->interval;
2141 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2142 bus->bandwidth_isoc_reqs--;
2143 else
2144 bus->bandwidth_int_reqs--;
2145}
2146
2147/*
2148 * Sets the final status of an URB and returns it to the upper layer. Any
2149 * required cleanup of the URB is performed.
2150 *
2151 * Must be called with interrupt disabled and spinlock held
2152 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002153void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
2154 int status)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002155{
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002156 struct urb *urb;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002157 int i;
2158
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002159 if (!qtd) {
2160 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
2161 return;
2162 }
2163
2164 if (!qtd->urb) {
2165 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
2166 return;
2167 }
2168
2169 urb = qtd->urb->priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002170 if (!urb) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002171 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002172 return;
2173 }
2174
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002175 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002176
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002177 if (dbg_urb(urb))
2178 dev_vdbg(hsotg->dev,
2179 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
2180 __func__, urb, usb_pipedevice(urb->pipe),
2181 usb_pipeendpoint(urb->pipe),
2182 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
2183 urb->actual_length);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002184
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002185 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002186 for (i = 0; i < urb->number_of_packets; i++)
2187 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
2188 i, urb->iso_frame_desc[i].status);
2189 }
2190
2191 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002192 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002193 for (i = 0; i < urb->number_of_packets; ++i) {
2194 urb->iso_frame_desc[i].actual_length =
2195 dwc2_hcd_urb_get_iso_desc_actual_length(
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002196 qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002197 urb->iso_frame_desc[i].status =
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002198 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002199 }
2200 }
2201
2202 urb->status = status;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002203 if (!status) {
2204 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
2205 urb->actual_length < urb->transfer_buffer_length)
2206 urb->status = -EREMOTEIO;
2207 }
2208
2209 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
2210 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2211 struct usb_host_endpoint *ep = urb->ep;
2212
2213 if (ep)
2214 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
2215 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
2216 urb);
2217 }
2218
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002219 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002220 urb->hcpriv = NULL;
2221 kfree(qtd->urb);
2222 qtd->urb = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002223
2224 spin_unlock(&hsotg->lock);
2225 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
2226 spin_lock(&hsotg->lock);
2227}
2228
2229/*
2230 * Work queue function for starting the HCD when A-Cable is connected
2231 */
2232static void dwc2_hcd_start_func(struct work_struct *work)
2233{
2234 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
2235 start_work.work);
2236
2237 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
2238 dwc2_host_start(hsotg);
2239}
2240
2241/*
2242 * Reset work queue function
2243 */
2244static void dwc2_hcd_reset_func(struct work_struct *work)
2245{
2246 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
2247 reset_work.work);
2248 u32 hprt0;
2249
2250 dev_dbg(hsotg->dev, "USB RESET function called\n");
2251 hprt0 = dwc2_read_hprt0(hsotg);
2252 hprt0 &= ~HPRT0_RST;
2253 writel(hprt0, hsotg->regs + HPRT0);
2254 hsotg->flags.b.port_reset_change = 1;
2255}
2256
2257/*
2258 * =========================================================================
2259 * Linux HC Driver Functions
2260 * =========================================================================
2261 */
2262
2263/*
2264 * Initializes the DWC_otg controller and its root hub and prepares it for host
2265 * mode operation. Activates the root port. Returns 0 on success and a negative
2266 * error code on failure.
2267 */
2268static int _dwc2_hcd_start(struct usb_hcd *hcd)
2269{
2270 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2271 struct usb_bus *bus = hcd_to_bus(hcd);
2272 unsigned long flags;
2273
2274 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
2275
2276 spin_lock_irqsave(&hsotg->lock, flags);
2277
2278 hcd->state = HC_STATE_RUNNING;
2279
2280 if (dwc2_is_device_mode(hsotg)) {
2281 spin_unlock_irqrestore(&hsotg->lock, flags);
2282 return 0; /* why 0 ?? */
2283 }
2284
2285 dwc2_hcd_reinit(hsotg);
2286
2287 /* Initialize and connect root hub if one is not already attached */
2288 if (bus->root_hub) {
2289 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
2290 /* Inform the HUB driver to resume */
2291 usb_hcd_resume_root_hub(hcd);
2292 }
2293
2294 spin_unlock_irqrestore(&hsotg->lock, flags);
2295 return 0;
2296}
2297
2298/*
2299 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
2300 * stopped.
2301 */
2302static void _dwc2_hcd_stop(struct usb_hcd *hcd)
2303{
2304 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2305 unsigned long flags;
2306
2307 spin_lock_irqsave(&hsotg->lock, flags);
2308 dwc2_hcd_stop(hsotg);
2309 spin_unlock_irqrestore(&hsotg->lock, flags);
2310
2311 usleep_range(1000, 3000);
2312}
2313
Gregory Herrero99a65792015-04-29 22:09:13 +02002314static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
2315{
2316 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2317
2318 hsotg->lx_state = DWC2_L2;
2319 return 0;
2320}
2321
2322static int _dwc2_hcd_resume(struct usb_hcd *hcd)
2323{
2324 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2325
2326 hsotg->lx_state = DWC2_L0;
2327 return 0;
2328}
2329
Paul Zimmerman7359d482013-03-11 17:47:59 -07002330/* Returns the current frame number */
2331static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
2332{
2333 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2334
2335 return dwc2_hcd_get_frame_number(hsotg);
2336}
2337
2338static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
2339 char *fn_name)
2340{
2341#ifdef VERBOSE_DEBUG
2342 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2343 char *pipetype;
2344 char *speed;
2345
2346 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
2347 dev_vdbg(hsotg->dev, " Device address: %d\n",
2348 usb_pipedevice(urb->pipe));
2349 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
2350 usb_pipeendpoint(urb->pipe),
2351 usb_pipein(urb->pipe) ? "IN" : "OUT");
2352
2353 switch (usb_pipetype(urb->pipe)) {
2354 case PIPE_CONTROL:
2355 pipetype = "CONTROL";
2356 break;
2357 case PIPE_BULK:
2358 pipetype = "BULK";
2359 break;
2360 case PIPE_INTERRUPT:
2361 pipetype = "INTERRUPT";
2362 break;
2363 case PIPE_ISOCHRONOUS:
2364 pipetype = "ISOCHRONOUS";
2365 break;
2366 default:
2367 pipetype = "UNKNOWN";
2368 break;
2369 }
2370
2371 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
2372 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
2373 "IN" : "OUT");
2374
2375 switch (urb->dev->speed) {
2376 case USB_SPEED_HIGH:
2377 speed = "HIGH";
2378 break;
2379 case USB_SPEED_FULL:
2380 speed = "FULL";
2381 break;
2382 case USB_SPEED_LOW:
2383 speed = "LOW";
2384 break;
2385 default:
2386 speed = "UNKNOWN";
2387 break;
2388 }
2389
2390 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
2391 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
2392 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
2393 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
2394 urb->transfer_buffer_length);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07002395 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
2396 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
2397 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
2398 urb->setup_packet, (unsigned long)urb->setup_dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002399 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
2400
2401 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2402 int i;
2403
2404 for (i = 0; i < urb->number_of_packets; i++) {
2405 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
2406 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
2407 urb->iso_frame_desc[i].offset,
2408 urb->iso_frame_desc[i].length);
2409 }
2410 }
2411#endif
2412}
2413
2414/*
2415 * Starts processing a USB transfer request specified by a USB Request Block
2416 * (URB). mem_flags indicates the type of memory allocation to use while
2417 * processing this URB.
2418 */
2419static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
2420 gfp_t mem_flags)
2421{
2422 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2423 struct usb_host_endpoint *ep = urb->ep;
2424 struct dwc2_hcd_urb *dwc2_urb;
2425 int i;
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002426 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002427 int alloc_bandwidth = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002428 u8 ep_type = 0;
2429 u32 tflags = 0;
2430 void *buf;
2431 unsigned long flags;
2432
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002433 if (dbg_urb(urb)) {
2434 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
2435 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
2436 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002437
2438 if (ep == NULL)
2439 return -EINVAL;
2440
2441 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
2442 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2443 spin_lock_irqsave(&hsotg->lock, flags);
2444 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
2445 alloc_bandwidth = 1;
2446 spin_unlock_irqrestore(&hsotg->lock, flags);
2447 }
2448
2449 switch (usb_pipetype(urb->pipe)) {
2450 case PIPE_CONTROL:
2451 ep_type = USB_ENDPOINT_XFER_CONTROL;
2452 break;
2453 case PIPE_ISOCHRONOUS:
2454 ep_type = USB_ENDPOINT_XFER_ISOC;
2455 break;
2456 case PIPE_BULK:
2457 ep_type = USB_ENDPOINT_XFER_BULK;
2458 break;
2459 case PIPE_INTERRUPT:
2460 ep_type = USB_ENDPOINT_XFER_INT;
2461 break;
2462 default:
2463 dev_warn(hsotg->dev, "Wrong ep type\n");
2464 }
2465
2466 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
2467 mem_flags);
2468 if (!dwc2_urb)
2469 return -ENOMEM;
2470
2471 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
2472 usb_pipeendpoint(urb->pipe), ep_type,
2473 usb_pipein(urb->pipe),
2474 usb_maxpacket(urb->dev, urb->pipe,
2475 !(usb_pipein(urb->pipe))));
2476
2477 buf = urb->transfer_buffer;
Paul Zimmerman25a49442013-07-13 14:53:53 -07002478
Paul Zimmerman7359d482013-03-11 17:47:59 -07002479 if (hcd->self.uses_dma) {
Paul Zimmerman25a49442013-07-13 14:53:53 -07002480 if (!buf && (urb->transfer_dma & 3)) {
2481 dev_err(hsotg->dev,
2482 "%s: unaligned transfer with no transfer_buffer",
2483 __func__);
2484 retval = -EINVAL;
Gregory Herrero33ad2612015-04-29 22:09:15 +02002485 goto fail0;
Paul Zimmerman25a49442013-07-13 14:53:53 -07002486 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002487 }
2488
2489 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
2490 tflags |= URB_GIVEBACK_ASAP;
2491 if (urb->transfer_flags & URB_ZERO_PACKET)
2492 tflags |= URB_SEND_ZERO_PACKET;
2493
2494 dwc2_urb->priv = urb;
2495 dwc2_urb->buf = buf;
2496 dwc2_urb->dma = urb->transfer_dma;
2497 dwc2_urb->length = urb->transfer_buffer_length;
2498 dwc2_urb->setup_packet = urb->setup_packet;
2499 dwc2_urb->setup_dma = urb->setup_dma;
2500 dwc2_urb->flags = tflags;
2501 dwc2_urb->interval = urb->interval;
2502 dwc2_urb->status = -EINPROGRESS;
2503
2504 for (i = 0; i < urb->number_of_packets; ++i)
2505 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
2506 urb->iso_frame_desc[i].offset,
2507 urb->iso_frame_desc[i].length);
2508
2509 urb->hcpriv = dwc2_urb;
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002510
2511 spin_lock_irqsave(&hsotg->lock, flags);
2512 retval = usb_hcd_link_urb_to_ep(hcd, urb);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002513 if (retval)
2514 goto fail1;
2515
2516 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, &ep->hcpriv, mem_flags);
2517 if (retval)
2518 goto fail2;
2519
2520 if (alloc_bandwidth) {
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002521 dwc2_allocate_bus_bandwidth(hcd,
2522 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
2523 urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002524 }
2525
Gregory Herrero33ad2612015-04-29 22:09:15 +02002526 spin_unlock_irqrestore(&hsotg->lock, flags);
2527
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002528 return 0;
2529
2530fail2:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002531 dwc2_urb->priv = NULL;
2532 usb_hcd_unlink_urb_from_ep(hcd, urb);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002533fail1:
Gregory Herrero33ad2612015-04-29 22:09:15 +02002534 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002535 urb->hcpriv = NULL;
Gregory Herrero33ad2612015-04-29 22:09:15 +02002536fail0:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002537 kfree(dwc2_urb);
2538
Paul Zimmerman7359d482013-03-11 17:47:59 -07002539 return retval;
2540}
2541
2542/*
2543 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
2544 */
2545static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
2546 int status)
2547{
2548 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002549 int rc;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002550 unsigned long flags;
2551
2552 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
2553 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
2554
2555 spin_lock_irqsave(&hsotg->lock, flags);
2556
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002557 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
2558 if (rc)
2559 goto out;
2560
Paul Zimmerman7359d482013-03-11 17:47:59 -07002561 if (!urb->hcpriv) {
2562 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
2563 goto out;
2564 }
2565
2566 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
2567
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002568 usb_hcd_unlink_urb_from_ep(hcd, urb);
2569
Paul Zimmerman7359d482013-03-11 17:47:59 -07002570 kfree(urb->hcpriv);
2571 urb->hcpriv = NULL;
2572
2573 /* Higher layer software sets URB status */
2574 spin_unlock(&hsotg->lock);
2575 usb_hcd_giveback_urb(hcd, urb, status);
2576 spin_lock(&hsotg->lock);
2577
2578 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
2579 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
2580out:
2581 spin_unlock_irqrestore(&hsotg->lock, flags);
2582
2583 return rc;
2584}
2585
2586/*
2587 * Frees resources in the DWC_otg controller related to a given endpoint. Also
2588 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
2589 * must already be dequeued.
2590 */
2591static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
2592 struct usb_host_endpoint *ep)
2593{
2594 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2595
2596 dev_dbg(hsotg->dev,
2597 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
2598 ep->desc.bEndpointAddress, ep->hcpriv);
2599 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
2600}
2601
2602/*
2603 * Resets endpoint specific parameter values, in current version used to reset
2604 * the data toggle (as a WA). This function can be called from usb_clear_halt
2605 * routine.
2606 */
2607static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
2608 struct usb_host_endpoint *ep)
2609{
2610 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002611 unsigned long flags;
2612
2613 dev_dbg(hsotg->dev,
2614 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
2615 ep->desc.bEndpointAddress);
2616
Paul Zimmerman7359d482013-03-11 17:47:59 -07002617 spin_lock_irqsave(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002618 dwc2_hcd_endpoint_reset(hsotg, ep);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002619 spin_unlock_irqrestore(&hsotg->lock, flags);
2620}
2621
2622/*
2623 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
2624 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
2625 * interrupt.
2626 *
2627 * This function is called by the USB core when an interrupt occurs
2628 */
2629static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
2630{
2631 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002632
Matthijs Kooijmanca18f4a2013-04-25 23:39:15 +02002633 return dwc2_handle_hcd_intr(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002634}
2635
2636/*
2637 * Creates Status Change bitmap for the root hub and root port. The bitmap is
2638 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
2639 * is the status change indicator for the single root port. Returns 1 if either
2640 * change indicator is 1, otherwise returns 0.
2641 */
2642static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
2643{
2644 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2645
2646 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
2647 return buf[0] != 0;
2648}
2649
2650/* Handles hub class-specific requests */
2651static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
2652 u16 windex, char *buf, u16 wlength)
2653{
2654 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
2655 wvalue, windex, buf, wlength);
2656 return retval;
2657}
2658
2659/* Handles hub TT buffer clear completions */
2660static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
2661 struct usb_host_endpoint *ep)
2662{
2663 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2664 struct dwc2_qh *qh;
2665 unsigned long flags;
2666
2667 qh = ep->hcpriv;
2668 if (!qh)
2669 return;
2670
2671 spin_lock_irqsave(&hsotg->lock, flags);
2672 qh->tt_buffer_dirty = 0;
2673
2674 if (hsotg->flags.b.port_connect_status)
2675 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
2676
2677 spin_unlock_irqrestore(&hsotg->lock, flags);
2678}
2679
2680static struct hc_driver dwc2_hc_driver = {
2681 .description = "dwc2_hsotg",
2682 .product_desc = "DWC OTG Controller",
2683 .hcd_priv_size = sizeof(struct wrapper_priv_data),
2684
2685 .irq = _dwc2_hcd_irq,
2686 .flags = HCD_MEMORY | HCD_USB2,
2687
2688 .start = _dwc2_hcd_start,
2689 .stop = _dwc2_hcd_stop,
2690 .urb_enqueue = _dwc2_hcd_urb_enqueue,
2691 .urb_dequeue = _dwc2_hcd_urb_dequeue,
2692 .endpoint_disable = _dwc2_hcd_endpoint_disable,
2693 .endpoint_reset = _dwc2_hcd_endpoint_reset,
2694 .get_frame_number = _dwc2_hcd_get_frame_number,
2695
2696 .hub_status_data = _dwc2_hcd_hub_status_data,
2697 .hub_control = _dwc2_hcd_hub_control,
2698 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
Gregory Herrero99a65792015-04-29 22:09:13 +02002699
2700 .bus_suspend = _dwc2_hcd_suspend,
2701 .bus_resume = _dwc2_hcd_resume,
Paul Zimmerman7359d482013-03-11 17:47:59 -07002702};
2703
2704/*
2705 * Frees secondary storage associated with the dwc2_hsotg structure contained
2706 * in the struct usb_hcd field
2707 */
2708static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
2709{
2710 u32 ahbcfg;
2711 u32 dctl;
2712 int i;
2713
2714 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
2715
2716 /* Free memory for QH/QTD lists */
2717 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
2718 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
2719 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
2720 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
2721 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
2722 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
2723
2724 /* Free memory for the host channels */
2725 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
2726 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
2727
2728 if (chan != NULL) {
2729 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
2730 i, chan);
2731 hsotg->hc_ptr_array[i] = NULL;
2732 kfree(chan);
2733 }
2734 }
2735
2736 if (hsotg->core_params->dma_enable > 0) {
2737 if (hsotg->status_buf) {
2738 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
2739 hsotg->status_buf,
2740 hsotg->status_buf_dma);
2741 hsotg->status_buf = NULL;
2742 }
2743 } else {
2744 kfree(hsotg->status_buf);
2745 hsotg->status_buf = NULL;
2746 }
2747
2748 ahbcfg = readl(hsotg->regs + GAHBCFG);
2749
2750 /* Disable all interrupts */
2751 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
2752 writel(ahbcfg, hsotg->regs + GAHBCFG);
2753 writel(0, hsotg->regs + GINTMSK);
2754
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002755 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002756 dctl = readl(hsotg->regs + DCTL);
2757 dctl |= DCTL_SFTDISCON;
2758 writel(dctl, hsotg->regs + DCTL);
2759 }
2760
2761 if (hsotg->wq_otg) {
2762 if (!cancel_work_sync(&hsotg->wf_otg))
2763 flush_workqueue(hsotg->wq_otg);
2764 destroy_workqueue(hsotg->wq_otg);
2765 }
2766
Paul Zimmerman7359d482013-03-11 17:47:59 -07002767 del_timer(&hsotg->wkp_timer);
2768}
2769
2770static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
2771{
2772 /* Turn off all host-specific interrupts */
2773 dwc2_disable_host_interrupts(hsotg);
2774
2775 dwc2_hcd_free(hsotg);
2776}
2777
Matthijs Kooijman8284f932013-04-11 18:43:47 +02002778/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07002779 * Initializes the HCD. This function allocates memory for and initializes the
2780 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
2781 * USB bus with the core and calls the hc_driver->start() function. It returns
2782 * a negative error on failure.
2783 */
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02002784int dwc2_hcd_init(struct dwc2_hsotg *hsotg, int irq)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002785{
2786 struct usb_hcd *hcd;
2787 struct dwc2_host_chan *channel;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002788 u32 hcfg;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002789 int i, num_channels;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002790 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002791
Dinh Nguyenf5500ec2014-11-11 11:13:39 -06002792 if (usb_disabled())
2793 return -ENODEV;
2794
Paul Zimmermane62662c2013-03-25 17:03:35 -07002795 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07002796
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002797 retval = -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002798
2799 hcfg = readl(hsotg->regs + HCFG);
2800 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002801
2802#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
2803 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
2804 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
2805 if (!hsotg->frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07002806 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002807 hsotg->last_frame_num_array = kzalloc(
2808 sizeof(*hsotg->last_frame_num_array) *
2809 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
2810 if (!hsotg->last_frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07002811 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002812 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
2813#endif
2814
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02002815 /* Check if the bus driver or platform code has setup a dma_mask */
2816 if (hsotg->core_params->dma_enable > 0 &&
2817 hsotg->dev->dma_mask == NULL) {
2818 dev_warn(hsotg->dev,
2819 "dma_mask not set, disabling DMA\n");
2820 hsotg->core_params->dma_enable = 0;
2821 hsotg->core_params->dma_desc_enable = 0;
2822 }
2823
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07002824 /* Set device flags indicating whether the HCD supports DMA */
2825 if (hsotg->core_params->dma_enable > 0) {
Paul Zimmerman30885312013-05-24 16:27:56 -07002826 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
2827 dev_warn(hsotg->dev, "can't set DMA mask\n");
Paul Zimmerman25a49442013-07-13 14:53:53 -07002828 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
2829 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07002830 }
2831
2832 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
2833 if (!hcd)
2834 goto error1;
2835
Matthijs Kooijman7de76ee2013-07-19 11:34:23 +02002836 if (hsotg->core_params->dma_enable <= 0)
2837 hcd->self.uses_dma = 0;
2838
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07002839 hcd->has_tt = 1;
2840
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07002841 ((struct wrapper_priv_data *) &hcd->hcd_priv)->hsotg = hsotg;
2842 hsotg->priv = hcd;
2843
Paul Zimmerman7359d482013-03-11 17:47:59 -07002844 /*
2845 * Disable the global interrupt until all the interrupt handlers are
2846 * installed
2847 */
2848 dwc2_disable_global_interrupts(hsotg);
2849
Matthijs Kooijman6706c722013-04-11 17:52:41 +02002850 /* Initialize the DWC_otg core, and select the Phy type */
2851 retval = dwc2_core_init(hsotg, true, irq);
2852 if (retval)
2853 goto error2;
2854
Paul Zimmerman7359d482013-03-11 17:47:59 -07002855 /* Create new workqueue and init work */
Wei Yongjun53510352013-04-12 22:41:48 +08002856 retval = -ENOMEM;
Matthijs Kooijman050232a2013-04-11 18:43:46 +02002857 hsotg->wq_otg = create_singlethread_workqueue("dwc2");
Paul Zimmerman7359d482013-03-11 17:47:59 -07002858 if (!hsotg->wq_otg) {
2859 dev_err(hsotg->dev, "Failed to create workqueue\n");
2860 goto error2;
2861 }
2862 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
2863
Paul Zimmerman7359d482013-03-11 17:47:59 -07002864 setup_timer(&hsotg->wkp_timer, dwc2_wakeup_detected,
2865 (unsigned long)hsotg);
2866
2867 /* Initialize the non-periodic schedule */
2868 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
2869 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
2870
2871 /* Initialize the periodic schedule */
2872 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
2873 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
2874 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
2875 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
2876
2877 /*
2878 * Create a host channel descriptor for each host channel implemented
2879 * in the controller. Initialize the channel descriptor array.
2880 */
2881 INIT_LIST_HEAD(&hsotg->free_hc_list);
2882 num_channels = hsotg->core_params->host_channels;
2883 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
2884
2885 for (i = 0; i < num_channels; i++) {
2886 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2887 if (channel == NULL)
2888 goto error3;
2889 channel->hc_num = i;
2890 hsotg->hc_ptr_array[i] = channel;
2891 }
2892
Dom Cobley20f2eb92013-09-23 14:23:34 -07002893 if (hsotg->core_params->uframe_sched > 0)
2894 dwc2_hcd_init_usecs(hsotg);
2895
Paul Zimmerman7359d482013-03-11 17:47:59 -07002896 /* Initialize hsotg start work */
2897 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
2898
2899 /* Initialize port reset work */
2900 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
2901
2902 /*
2903 * Allocate space for storing data on status transactions. Normally no
2904 * data is sent, but this space acts as a bit bucket. This must be
2905 * done after usb_add_hcd since that function allocates the DMA buffer
2906 * pool.
2907 */
2908 if (hsotg->core_params->dma_enable > 0)
2909 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
2910 DWC2_HCD_STATUS_BUF_SIZE,
2911 &hsotg->status_buf_dma, GFP_KERNEL);
2912 else
2913 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
2914 GFP_KERNEL);
2915
2916 if (!hsotg->status_buf)
2917 goto error3;
2918
2919 hsotg->otg_port = 1;
2920 hsotg->frame_list = NULL;
2921 hsotg->frame_list_dma = 0;
2922 hsotg->periodic_qh_count = 0;
2923
2924 /* Initiate lx_state to L3 disconnected state */
2925 hsotg->lx_state = DWC2_L3;
2926
2927 hcd->self.otg_port = hsotg->otg_port;
2928
2929 /* Don't support SG list at this point */
2930 hcd->self.sg_tablesize = 0;
2931
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02002932 if (!IS_ERR_OR_NULL(hsotg->uphy))
2933 otg_set_host(hsotg->uphy->otg, &hcd->self);
2934
Paul Zimmerman7359d482013-03-11 17:47:59 -07002935 /*
2936 * Finish generic HCD initialization and start the HCD. This function
2937 * allocates the DMA buffer pool, registers the USB bus, requests the
2938 * IRQ line, and calls hcd_start method.
2939 */
Matthijs Kooijman66513f42013-04-25 23:39:13 +02002940 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002941 if (retval < 0)
2942 goto error3;
2943
Peter Chen3c9740a2013-11-05 10:46:02 +08002944 device_wakeup_enable(hcd->self.controller);
2945
Paul Zimmerman7359d482013-03-11 17:47:59 -07002946 dwc2_hcd_dump_state(hsotg);
2947
2948 dwc2_enable_global_interrupts(hsotg);
2949
2950 return 0;
2951
2952error3:
2953 dwc2_hcd_release(hsotg);
2954error2:
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07002955 usb_put_hcd(hcd);
2956error1:
Paul Zimmerman7359d482013-03-11 17:47:59 -07002957 kfree(hsotg->core_params);
2958
2959#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
2960 kfree(hsotg->last_frame_num_array);
2961 kfree(hsotg->frame_num_array);
2962#endif
2963
Paul Zimmermane62662c2013-03-25 17:03:35 -07002964 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002965 return retval;
2966}
2967EXPORT_SYMBOL_GPL(dwc2_hcd_init);
2968
2969/*
2970 * Removes the HCD.
2971 * Frees memory and resources associated with the HCD and deregisters the bus.
2972 */
Paul Zimmermane62662c2013-03-25 17:03:35 -07002973void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002974{
2975 struct usb_hcd *hcd;
2976
Paul Zimmermane62662c2013-03-25 17:03:35 -07002977 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07002978
2979 hcd = dwc2_hsotg_to_hcd(hsotg);
Paul Zimmermane62662c2013-03-25 17:03:35 -07002980 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002981
2982 if (!hcd) {
Paul Zimmermane62662c2013-03-25 17:03:35 -07002983 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
Paul Zimmerman7359d482013-03-11 17:47:59 -07002984 __func__);
2985 return;
2986 }
2987
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02002988 if (!IS_ERR_OR_NULL(hsotg->uphy))
2989 otg_set_host(hsotg->uphy->otg, NULL);
2990
Paul Zimmerman7359d482013-03-11 17:47:59 -07002991 usb_remove_hcd(hcd);
2992 hsotg->priv = NULL;
2993 dwc2_hcd_release(hsotg);
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07002994 usb_put_hcd(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002995
2996#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
2997 kfree(hsotg->last_frame_num_array);
2998 kfree(hsotg->frame_num_array);
2999#endif
Paul Zimmerman7359d482013-03-11 17:47:59 -07003000}
3001EXPORT_SYMBOL_GPL(dwc2_hcd_remove);