dwc2: hcd: use HUB_CHAR_*
[deliverable/linux.git] / drivers / usb / dwc2 / hcd.c
1 /*
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 */
68 static 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 */
128 static 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) {
137 dwc2_host_complete(hsotg, qtd, -ETIMEDOUT);
138 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
139 }
140 }
141 }
142
143 static 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 */
183 static 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 */
198 void 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 */
218 static 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 }
260 }
261
262 /**
263 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
264 *
265 * @hsotg: Pointer to struct dwc2_hsotg
266 *
267 * Must be called with interrupt disabled and spinlock held
268 */
269 void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg)
270 {
271 u32 intr;
272
273 /* Set status flags for the hub driver */
274 hsotg->flags.b.port_connect_status_change = 1;
275 hsotg->flags.b.port_connect_status = 0;
276
277 /*
278 * Shutdown any transfers in process by clearing the Tx FIFO Empty
279 * interrupt mask and status bits and disabling subsequent host
280 * channel interrupts.
281 */
282 intr = readl(hsotg->regs + GINTMSK);
283 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
284 writel(intr, hsotg->regs + GINTMSK);
285 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
286 writel(intr, hsotg->regs + GINTSTS);
287
288 /*
289 * Turn off the vbus power only if the core has transitioned to device
290 * mode. If still in host mode, need to keep power on to detect a
291 * reconnection.
292 */
293 if (dwc2_is_device_mode(hsotg)) {
294 if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
295 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
296 writel(0, hsotg->regs + HPRT0);
297 }
298
299 dwc2_disable_host_interrupts(hsotg);
300 }
301
302 /* Respond with an error status to all URBs in the schedule */
303 dwc2_kill_all_urbs(hsotg);
304
305 if (dwc2_is_host_mode(hsotg))
306 /* Clean up any host channels that were in use */
307 dwc2_hcd_cleanup_channels(hsotg);
308
309 dwc2_host_disconnect(hsotg);
310 }
311
312 /**
313 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
314 *
315 * @hsotg: Pointer to struct dwc2_hsotg
316 */
317 static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
318 {
319 if (hsotg->lx_state == DWC2_L2)
320 hsotg->flags.b.port_suspend_change = 1;
321 else
322 hsotg->flags.b.port_l1_change = 1;
323 }
324
325 /**
326 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
327 *
328 * @hsotg: Pointer to struct dwc2_hsotg
329 *
330 * Must be called with interrupt disabled and spinlock held
331 */
332 void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
333 {
334 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
335
336 /*
337 * The root hub should be disconnected before this function is called.
338 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
339 * and the QH lists (via ..._hcd_endpoint_disable).
340 */
341
342 /* Turn off all host-specific interrupts */
343 dwc2_disable_host_interrupts(hsotg);
344
345 /* Turn off the vbus power */
346 dev_dbg(hsotg->dev, "PortPower off\n");
347 writel(0, hsotg->regs + HPRT0);
348 }
349
350 static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
351 struct dwc2_hcd_urb *urb, void **ep_handle,
352 gfp_t mem_flags)
353 {
354 struct dwc2_qtd *qtd;
355 unsigned long flags;
356 u32 intr_mask;
357 int retval;
358 int dev_speed;
359
360 if (!hsotg->flags.b.port_connect_status) {
361 /* No longer connected */
362 dev_err(hsotg->dev, "Not connected\n");
363 return -ENODEV;
364 }
365
366 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
367
368 /* Some configurations cannot support LS traffic on a FS root port */
369 if ((dev_speed == USB_SPEED_LOW) &&
370 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
371 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
372 u32 hprt0 = readl(hsotg->regs + HPRT0);
373 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
374
375 if (prtspd == HPRT0_SPD_FULL_SPEED)
376 return -ENODEV;
377 }
378
379 qtd = kzalloc(sizeof(*qtd), mem_flags);
380 if (!qtd)
381 return -ENOMEM;
382
383 dwc2_hcd_qtd_init(qtd, urb);
384 retval = dwc2_hcd_qtd_add(hsotg, qtd, (struct dwc2_qh **)ep_handle,
385 mem_flags);
386 if (retval) {
387 dev_err(hsotg->dev,
388 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
389 retval);
390 kfree(qtd);
391 return retval;
392 }
393
394 intr_mask = readl(hsotg->regs + GINTMSK);
395 if (!(intr_mask & GINTSTS_SOF)) {
396 enum dwc2_transaction_type tr_type;
397
398 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
399 !(qtd->urb->flags & URB_GIVEBACK_ASAP))
400 /*
401 * Do not schedule SG transactions until qtd has
402 * URB_GIVEBACK_ASAP set
403 */
404 return 0;
405
406 spin_lock_irqsave(&hsotg->lock, flags);
407 tr_type = dwc2_hcd_select_transactions(hsotg);
408 if (tr_type != DWC2_TRANSACTION_NONE)
409 dwc2_hcd_queue_transactions(hsotg, tr_type);
410 spin_unlock_irqrestore(&hsotg->lock, flags);
411 }
412
413 return 0;
414 }
415
416 /* Must be called with interrupt disabled and spinlock held */
417 static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
418 struct dwc2_hcd_urb *urb)
419 {
420 struct dwc2_qh *qh;
421 struct dwc2_qtd *urb_qtd;
422
423 urb_qtd = urb->qtd;
424 if (!urb_qtd) {
425 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
426 return -EINVAL;
427 }
428
429 qh = urb_qtd->qh;
430 if (!qh) {
431 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
432 return -EINVAL;
433 }
434
435 urb->priv = NULL;
436
437 if (urb_qtd->in_process && qh->channel) {
438 dwc2_dump_channel_info(hsotg, qh->channel);
439
440 /* The QTD is in process (it has been assigned to a channel) */
441 if (hsotg->flags.b.port_connect_status)
442 /*
443 * If still connected (i.e. in host mode), halt the
444 * channel so it can be used for other transfers. If
445 * no longer connected, the host registers can't be
446 * written to halt the channel since the core is in
447 * device mode.
448 */
449 dwc2_hc_halt(hsotg, qh->channel,
450 DWC2_HC_XFER_URB_DEQUEUE);
451 }
452
453 /*
454 * Free the QTD and clean up the associated QH. Leave the QH in the
455 * schedule if it has any remaining QTDs.
456 */
457 if (hsotg->core_params->dma_desc_enable <= 0) {
458 u8 in_process = urb_qtd->in_process;
459
460 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
461 if (in_process) {
462 dwc2_hcd_qh_deactivate(hsotg, qh, 0);
463 qh->channel = NULL;
464 } else if (list_empty(&qh->qtd_list)) {
465 dwc2_hcd_qh_unlink(hsotg, qh);
466 }
467 } else {
468 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
469 }
470
471 return 0;
472 }
473
474 /* Must NOT be called with interrupt disabled or spinlock held */
475 static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
476 struct usb_host_endpoint *ep, int retry)
477 {
478 struct dwc2_qtd *qtd, *qtd_tmp;
479 struct dwc2_qh *qh;
480 unsigned long flags;
481 int rc;
482
483 spin_lock_irqsave(&hsotg->lock, flags);
484
485 qh = ep->hcpriv;
486 if (!qh) {
487 rc = -EINVAL;
488 goto err;
489 }
490
491 while (!list_empty(&qh->qtd_list) && retry--) {
492 if (retry == 0) {
493 dev_err(hsotg->dev,
494 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
495 rc = -EBUSY;
496 goto err;
497 }
498
499 spin_unlock_irqrestore(&hsotg->lock, flags);
500 usleep_range(20000, 40000);
501 spin_lock_irqsave(&hsotg->lock, flags);
502 qh = ep->hcpriv;
503 if (!qh) {
504 rc = -EINVAL;
505 goto err;
506 }
507 }
508
509 dwc2_hcd_qh_unlink(hsotg, qh);
510
511 /* Free each QTD in the QH's QTD list */
512 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
513 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
514
515 ep->hcpriv = NULL;
516 spin_unlock_irqrestore(&hsotg->lock, flags);
517 dwc2_hcd_qh_free(hsotg, qh);
518
519 return 0;
520
521 err:
522 ep->hcpriv = NULL;
523 spin_unlock_irqrestore(&hsotg->lock, flags);
524
525 return rc;
526 }
527
528 /* Must be called with interrupt disabled and spinlock held */
529 static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
530 struct usb_host_endpoint *ep)
531 {
532 struct dwc2_qh *qh = ep->hcpriv;
533
534 if (!qh)
535 return -EINVAL;
536
537 qh->data_toggle = DWC2_HC_PID_DATA0;
538
539 return 0;
540 }
541
542 /*
543 * Initializes dynamic portions of the DWC_otg HCD state
544 *
545 * Must be called with interrupt disabled and spinlock held
546 */
547 static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
548 {
549 struct dwc2_host_chan *chan, *chan_tmp;
550 int num_channels;
551 int i;
552
553 hsotg->flags.d32 = 0;
554 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
555
556 if (hsotg->core_params->uframe_sched > 0) {
557 hsotg->available_host_channels =
558 hsotg->core_params->host_channels;
559 } else {
560 hsotg->non_periodic_channels = 0;
561 hsotg->periodic_channels = 0;
562 }
563
564 /*
565 * Put all channels in the free channel list and clean up channel
566 * states
567 */
568 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
569 hc_list_entry)
570 list_del_init(&chan->hc_list_entry);
571
572 num_channels = hsotg->core_params->host_channels;
573 for (i = 0; i < num_channels; i++) {
574 chan = hsotg->hc_ptr_array[i];
575 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
576 dwc2_hc_cleanup(hsotg, chan);
577 }
578
579 /* Initialize the DWC core for host mode operation */
580 dwc2_core_host_init(hsotg);
581 }
582
583 static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
584 struct dwc2_host_chan *chan,
585 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
586 {
587 int hub_addr, hub_port;
588
589 chan->do_split = 1;
590 chan->xact_pos = qtd->isoc_split_pos;
591 chan->complete_split = qtd->complete_split;
592 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
593 chan->hub_addr = (u8)hub_addr;
594 chan->hub_port = (u8)hub_port;
595 }
596
597 static void *dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
598 struct dwc2_host_chan *chan,
599 struct dwc2_qtd *qtd, void *bufptr)
600 {
601 struct dwc2_hcd_urb *urb = qtd->urb;
602 struct dwc2_hcd_iso_packet_desc *frame_desc;
603
604 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
605 case USB_ENDPOINT_XFER_CONTROL:
606 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
607
608 switch (qtd->control_phase) {
609 case DWC2_CONTROL_SETUP:
610 dev_vdbg(hsotg->dev, " Control setup transaction\n");
611 chan->do_ping = 0;
612 chan->ep_is_in = 0;
613 chan->data_pid_start = DWC2_HC_PID_SETUP;
614 if (hsotg->core_params->dma_enable > 0)
615 chan->xfer_dma = urb->setup_dma;
616 else
617 chan->xfer_buf = urb->setup_packet;
618 chan->xfer_len = 8;
619 bufptr = NULL;
620 break;
621
622 case DWC2_CONTROL_DATA:
623 dev_vdbg(hsotg->dev, " Control data transaction\n");
624 chan->data_pid_start = qtd->data_toggle;
625 break;
626
627 case DWC2_CONTROL_STATUS:
628 /*
629 * Direction is opposite of data direction or IN if no
630 * data
631 */
632 dev_vdbg(hsotg->dev, " Control status transaction\n");
633 if (urb->length == 0)
634 chan->ep_is_in = 1;
635 else
636 chan->ep_is_in =
637 dwc2_hcd_is_pipe_out(&urb->pipe_info);
638 if (chan->ep_is_in)
639 chan->do_ping = 0;
640 chan->data_pid_start = DWC2_HC_PID_DATA1;
641 chan->xfer_len = 0;
642 if (hsotg->core_params->dma_enable > 0)
643 chan->xfer_dma = hsotg->status_buf_dma;
644 else
645 chan->xfer_buf = hsotg->status_buf;
646 bufptr = NULL;
647 break;
648 }
649 break;
650
651 case USB_ENDPOINT_XFER_BULK:
652 chan->ep_type = USB_ENDPOINT_XFER_BULK;
653 break;
654
655 case USB_ENDPOINT_XFER_INT:
656 chan->ep_type = USB_ENDPOINT_XFER_INT;
657 break;
658
659 case USB_ENDPOINT_XFER_ISOC:
660 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
661 if (hsotg->core_params->dma_desc_enable > 0)
662 break;
663
664 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
665 frame_desc->status = 0;
666
667 if (hsotg->core_params->dma_enable > 0) {
668 chan->xfer_dma = urb->dma;
669 chan->xfer_dma += frame_desc->offset +
670 qtd->isoc_split_offset;
671 } else {
672 chan->xfer_buf = urb->buf;
673 chan->xfer_buf += frame_desc->offset +
674 qtd->isoc_split_offset;
675 }
676
677 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
678
679 /* For non-dword aligned buffers */
680 if (hsotg->core_params->dma_enable > 0 &&
681 (chan->xfer_dma & 0x3))
682 bufptr = (u8 *)urb->buf + frame_desc->offset +
683 qtd->isoc_split_offset;
684 else
685 bufptr = NULL;
686
687 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
688 if (chan->xfer_len <= 188)
689 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
690 else
691 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
692 }
693 break;
694 }
695
696 return bufptr;
697 }
698
699 static int dwc2_hc_setup_align_buf(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
700 struct dwc2_host_chan *chan,
701 struct dwc2_hcd_urb *urb, void *bufptr)
702 {
703 u32 buf_size;
704 struct urb *usb_urb;
705 struct usb_hcd *hcd;
706
707 if (!qh->dw_align_buf) {
708 if (chan->ep_type != USB_ENDPOINT_XFER_ISOC)
709 buf_size = hsotg->core_params->max_transfer_size;
710 else
711 /* 3072 = 3 max-size Isoc packets */
712 buf_size = 3072;
713
714 qh->dw_align_buf = dma_alloc_coherent(hsotg->dev, buf_size,
715 &qh->dw_align_buf_dma,
716 GFP_ATOMIC);
717 if (!qh->dw_align_buf)
718 return -ENOMEM;
719 qh->dw_align_buf_size = buf_size;
720 }
721
722 if (chan->xfer_len) {
723 dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", __func__);
724 usb_urb = urb->priv;
725
726 if (usb_urb) {
727 if (usb_urb->transfer_flags &
728 (URB_SETUP_MAP_SINGLE | URB_DMA_MAP_SG |
729 URB_DMA_MAP_PAGE | URB_DMA_MAP_SINGLE)) {
730 hcd = dwc2_hsotg_to_hcd(hsotg);
731 usb_hcd_unmap_urb_for_dma(hcd, usb_urb);
732 }
733 if (!chan->ep_is_in)
734 memcpy(qh->dw_align_buf, bufptr,
735 chan->xfer_len);
736 } else {
737 dev_warn(hsotg->dev, "no URB in dwc2_urb\n");
738 }
739 }
740
741 chan->align_buf = qh->dw_align_buf_dma;
742 return 0;
743 }
744
745 /**
746 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
747 * channel and initializes the host channel to perform the transactions. The
748 * host channel is removed from the free list.
749 *
750 * @hsotg: The HCD state structure
751 * @qh: Transactions from the first QTD for this QH are selected and assigned
752 * to a free host channel
753 */
754 static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
755 {
756 struct dwc2_host_chan *chan;
757 struct dwc2_hcd_urb *urb;
758 struct dwc2_qtd *qtd;
759 void *bufptr = NULL;
760
761 if (dbg_qh(qh))
762 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
763
764 if (list_empty(&qh->qtd_list)) {
765 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
766 return -ENOMEM;
767 }
768
769 if (list_empty(&hsotg->free_hc_list)) {
770 dev_dbg(hsotg->dev, "No free channel to assign\n");
771 return -ENOMEM;
772 }
773
774 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
775 hc_list_entry);
776
777 /* Remove host channel from free list */
778 list_del_init(&chan->hc_list_entry);
779
780 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
781 urb = qtd->urb;
782 qh->channel = chan;
783 qtd->in_process = 1;
784
785 /*
786 * Use usb_pipedevice to determine device address. This address is
787 * 0 before the SET_ADDRESS command and the correct address afterward.
788 */
789 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
790 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
791 chan->speed = qh->dev_speed;
792 chan->max_packet = dwc2_max_packet(qh->maxp);
793
794 chan->xfer_started = 0;
795 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
796 chan->error_state = (qtd->error_count > 0);
797 chan->halt_on_queue = 0;
798 chan->halt_pending = 0;
799 chan->requests = 0;
800
801 /*
802 * The following values may be modified in the transfer type section
803 * below. The xfer_len value may be reduced when the transfer is
804 * started to accommodate the max widths of the XferSize and PktCnt
805 * fields in the HCTSIZn register.
806 */
807
808 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
809 if (chan->ep_is_in)
810 chan->do_ping = 0;
811 else
812 chan->do_ping = qh->ping_state;
813
814 chan->data_pid_start = qh->data_toggle;
815 chan->multi_count = 1;
816
817 if (urb->actual_length > urb->length &&
818 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
819 urb->actual_length = urb->length;
820
821 if (hsotg->core_params->dma_enable > 0) {
822 chan->xfer_dma = urb->dma + urb->actual_length;
823
824 /* For non-dword aligned case */
825 if (hsotg->core_params->dma_desc_enable <= 0 &&
826 (chan->xfer_dma & 0x3))
827 bufptr = (u8 *)urb->buf + urb->actual_length;
828 } else {
829 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
830 }
831
832 chan->xfer_len = urb->length - urb->actual_length;
833 chan->xfer_count = 0;
834
835 /* Set the split attributes if required */
836 if (qh->do_split)
837 dwc2_hc_init_split(hsotg, chan, qtd, urb);
838 else
839 chan->do_split = 0;
840
841 /* Set the transfer attributes */
842 bufptr = dwc2_hc_init_xfer(hsotg, chan, qtd, bufptr);
843
844 /* Non DWORD-aligned buffer case */
845 if (bufptr) {
846 dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
847 if (dwc2_hc_setup_align_buf(hsotg, qh, chan, urb, bufptr)) {
848 dev_err(hsotg->dev,
849 "%s: Failed to allocate memory to handle non-dword aligned buffer\n",
850 __func__);
851 /* Add channel back to free list */
852 chan->align_buf = 0;
853 chan->multi_count = 0;
854 list_add_tail(&chan->hc_list_entry,
855 &hsotg->free_hc_list);
856 qtd->in_process = 0;
857 qh->channel = NULL;
858 return -ENOMEM;
859 }
860 } else {
861 chan->align_buf = 0;
862 }
863
864 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
865 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
866 /*
867 * This value may be modified when the transfer is started
868 * to reflect the actual transfer length
869 */
870 chan->multi_count = dwc2_hb_mult(qh->maxp);
871
872 if (hsotg->core_params->dma_desc_enable > 0)
873 chan->desc_list_addr = qh->desc_list_dma;
874
875 dwc2_hc_init(hsotg, chan);
876 chan->qh = qh;
877
878 return 0;
879 }
880
881 /**
882 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
883 * schedule and assigns them to available host channels. Called from the HCD
884 * interrupt handler functions.
885 *
886 * @hsotg: The HCD state structure
887 *
888 * Return: The types of new transactions that were assigned to host channels
889 */
890 enum dwc2_transaction_type dwc2_hcd_select_transactions(
891 struct dwc2_hsotg *hsotg)
892 {
893 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
894 struct list_head *qh_ptr;
895 struct dwc2_qh *qh;
896 int num_channels;
897
898 #ifdef DWC2_DEBUG_SOF
899 dev_vdbg(hsotg->dev, " Select Transactions\n");
900 #endif
901
902 /* Process entries in the periodic ready list */
903 qh_ptr = hsotg->periodic_sched_ready.next;
904 while (qh_ptr != &hsotg->periodic_sched_ready) {
905 if (list_empty(&hsotg->free_hc_list))
906 break;
907 if (hsotg->core_params->uframe_sched > 0) {
908 if (hsotg->available_host_channels <= 1)
909 break;
910 hsotg->available_host_channels--;
911 }
912 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
913 if (dwc2_assign_and_init_hc(hsotg, qh))
914 break;
915
916 /*
917 * Move the QH from the periodic ready schedule to the
918 * periodic assigned schedule
919 */
920 qh_ptr = qh_ptr->next;
921 list_move(&qh->qh_list_entry, &hsotg->periodic_sched_assigned);
922 ret_val = DWC2_TRANSACTION_PERIODIC;
923 }
924
925 /*
926 * Process entries in the inactive portion of the non-periodic
927 * schedule. Some free host channels may not be used if they are
928 * reserved for periodic transfers.
929 */
930 num_channels = hsotg->core_params->host_channels;
931 qh_ptr = hsotg->non_periodic_sched_inactive.next;
932 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
933 if (hsotg->core_params->uframe_sched <= 0 &&
934 hsotg->non_periodic_channels >= num_channels -
935 hsotg->periodic_channels)
936 break;
937 if (list_empty(&hsotg->free_hc_list))
938 break;
939 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
940 if (hsotg->core_params->uframe_sched > 0) {
941 if (hsotg->available_host_channels < 1)
942 break;
943 hsotg->available_host_channels--;
944 }
945
946 if (dwc2_assign_and_init_hc(hsotg, qh))
947 break;
948
949 /*
950 * Move the QH from the non-periodic inactive schedule to the
951 * non-periodic active schedule
952 */
953 qh_ptr = qh_ptr->next;
954 list_move(&qh->qh_list_entry,
955 &hsotg->non_periodic_sched_active);
956
957 if (ret_val == DWC2_TRANSACTION_NONE)
958 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
959 else
960 ret_val = DWC2_TRANSACTION_ALL;
961
962 if (hsotg->core_params->uframe_sched <= 0)
963 hsotg->non_periodic_channels++;
964 }
965
966 return ret_val;
967 }
968
969 /**
970 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
971 * a host channel associated with either a periodic or non-periodic transfer
972 *
973 * @hsotg: The HCD state structure
974 * @chan: Host channel descriptor associated with either a periodic or
975 * non-periodic transfer
976 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
977 * for periodic transfers or the non-periodic Tx FIFO
978 * for non-periodic transfers
979 *
980 * Return: 1 if a request is queued and more requests may be needed to
981 * complete the transfer, 0 if no more requests are required for this
982 * transfer, -1 if there is insufficient space in the Tx FIFO
983 *
984 * This function assumes that there is space available in the appropriate
985 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
986 * it checks whether space is available in the appropriate Tx FIFO.
987 *
988 * Must be called with interrupt disabled and spinlock held
989 */
990 static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
991 struct dwc2_host_chan *chan,
992 u16 fifo_dwords_avail)
993 {
994 int retval = 0;
995
996 if (hsotg->core_params->dma_enable > 0) {
997 if (hsotg->core_params->dma_desc_enable > 0) {
998 if (!chan->xfer_started ||
999 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1000 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
1001 chan->qh->ping_state = 0;
1002 }
1003 } else if (!chan->xfer_started) {
1004 dwc2_hc_start_transfer(hsotg, chan);
1005 chan->qh->ping_state = 0;
1006 }
1007 } else if (chan->halt_pending) {
1008 /* Don't queue a request if the channel has been halted */
1009 } else if (chan->halt_on_queue) {
1010 dwc2_hc_halt(hsotg, chan, chan->halt_status);
1011 } else if (chan->do_ping) {
1012 if (!chan->xfer_started)
1013 dwc2_hc_start_transfer(hsotg, chan);
1014 } else if (!chan->ep_is_in ||
1015 chan->data_pid_start == DWC2_HC_PID_SETUP) {
1016 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
1017 if (!chan->xfer_started) {
1018 dwc2_hc_start_transfer(hsotg, chan);
1019 retval = 1;
1020 } else {
1021 retval = dwc2_hc_continue_transfer(hsotg, chan);
1022 }
1023 } else {
1024 retval = -1;
1025 }
1026 } else {
1027 if (!chan->xfer_started) {
1028 dwc2_hc_start_transfer(hsotg, chan);
1029 retval = 1;
1030 } else {
1031 retval = dwc2_hc_continue_transfer(hsotg, chan);
1032 }
1033 }
1034
1035 return retval;
1036 }
1037
1038 /*
1039 * Processes periodic channels for the next frame and queues transactions for
1040 * these channels to the DWC_otg controller. After queueing transactions, the
1041 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
1042 * to queue as Periodic Tx FIFO or request queue space becomes available.
1043 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
1044 *
1045 * Must be called with interrupt disabled and spinlock held
1046 */
1047 static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
1048 {
1049 struct list_head *qh_ptr;
1050 struct dwc2_qh *qh;
1051 u32 tx_status;
1052 u32 fspcavail;
1053 u32 gintmsk;
1054 int status;
1055 int no_queue_space = 0;
1056 int no_fifo_space = 0;
1057 u32 qspcavail;
1058
1059 if (dbg_perio())
1060 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
1061
1062 tx_status = readl(hsotg->regs + HPTXSTS);
1063 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1064 TXSTS_QSPCAVAIL_SHIFT;
1065 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1066 TXSTS_FSPCAVAIL_SHIFT;
1067
1068 if (dbg_perio()) {
1069 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
1070 qspcavail);
1071 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
1072 fspcavail);
1073 }
1074
1075 qh_ptr = hsotg->periodic_sched_assigned.next;
1076 while (qh_ptr != &hsotg->periodic_sched_assigned) {
1077 tx_status = readl(hsotg->regs + HPTXSTS);
1078 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1079 TXSTS_QSPCAVAIL_SHIFT;
1080 if (qspcavail == 0) {
1081 no_queue_space = 1;
1082 break;
1083 }
1084
1085 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
1086 if (!qh->channel) {
1087 qh_ptr = qh_ptr->next;
1088 continue;
1089 }
1090
1091 /* Make sure EP's TT buffer is clean before queueing qtds */
1092 if (qh->tt_buffer_dirty) {
1093 qh_ptr = qh_ptr->next;
1094 continue;
1095 }
1096
1097 /*
1098 * Set a flag if we're queuing high-bandwidth in slave mode.
1099 * The flag prevents any halts to get into the request queue in
1100 * the middle of multiple high-bandwidth packets getting queued.
1101 */
1102 if (hsotg->core_params->dma_enable <= 0 &&
1103 qh->channel->multi_count > 1)
1104 hsotg->queuing_high_bandwidth = 1;
1105
1106 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1107 TXSTS_FSPCAVAIL_SHIFT;
1108 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
1109 if (status < 0) {
1110 no_fifo_space = 1;
1111 break;
1112 }
1113
1114 /*
1115 * In Slave mode, stay on the current transfer until there is
1116 * nothing more to do or the high-bandwidth request count is
1117 * reached. In DMA mode, only need to queue one request. The
1118 * controller automatically handles multiple packets for
1119 * high-bandwidth transfers.
1120 */
1121 if (hsotg->core_params->dma_enable > 0 || status == 0 ||
1122 qh->channel->requests == qh->channel->multi_count) {
1123 qh_ptr = qh_ptr->next;
1124 /*
1125 * Move the QH from the periodic assigned schedule to
1126 * the periodic queued schedule
1127 */
1128 list_move(&qh->qh_list_entry,
1129 &hsotg->periodic_sched_queued);
1130
1131 /* done queuing high bandwidth */
1132 hsotg->queuing_high_bandwidth = 0;
1133 }
1134 }
1135
1136 if (hsotg->core_params->dma_enable <= 0) {
1137 tx_status = readl(hsotg->regs + HPTXSTS);
1138 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1139 TXSTS_QSPCAVAIL_SHIFT;
1140 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1141 TXSTS_FSPCAVAIL_SHIFT;
1142 if (dbg_perio()) {
1143 dev_vdbg(hsotg->dev,
1144 " P Tx Req Queue Space Avail (after queue): %d\n",
1145 qspcavail);
1146 dev_vdbg(hsotg->dev,
1147 " P Tx FIFO Space Avail (after queue): %d\n",
1148 fspcavail);
1149 }
1150
1151 if (!list_empty(&hsotg->periodic_sched_assigned) ||
1152 no_queue_space || no_fifo_space) {
1153 /*
1154 * May need to queue more transactions as the request
1155 * queue or Tx FIFO empties. Enable the periodic Tx
1156 * FIFO empty interrupt. (Always use the half-empty
1157 * level to ensure that new requests are loaded as
1158 * soon as possible.)
1159 */
1160 gintmsk = readl(hsotg->regs + GINTMSK);
1161 gintmsk |= GINTSTS_PTXFEMP;
1162 writel(gintmsk, hsotg->regs + GINTMSK);
1163 } else {
1164 /*
1165 * Disable the Tx FIFO empty interrupt since there are
1166 * no more transactions that need to be queued right
1167 * now. This function is called from interrupt
1168 * handlers to queue more transactions as transfer
1169 * states change.
1170 */
1171 gintmsk = readl(hsotg->regs + GINTMSK);
1172 gintmsk &= ~GINTSTS_PTXFEMP;
1173 writel(gintmsk, hsotg->regs + GINTMSK);
1174 }
1175 }
1176 }
1177
1178 /*
1179 * Processes active non-periodic channels and queues transactions for these
1180 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
1181 * FIFO Empty interrupt is enabled if there are more transactions to queue as
1182 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
1183 * FIFO Empty interrupt is disabled.
1184 *
1185 * Must be called with interrupt disabled and spinlock held
1186 */
1187 static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
1188 {
1189 struct list_head *orig_qh_ptr;
1190 struct dwc2_qh *qh;
1191 u32 tx_status;
1192 u32 qspcavail;
1193 u32 fspcavail;
1194 u32 gintmsk;
1195 int status;
1196 int no_queue_space = 0;
1197 int no_fifo_space = 0;
1198 int more_to_do = 0;
1199
1200 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
1201
1202 tx_status = readl(hsotg->regs + GNPTXSTS);
1203 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1204 TXSTS_QSPCAVAIL_SHIFT;
1205 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1206 TXSTS_FSPCAVAIL_SHIFT;
1207 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
1208 qspcavail);
1209 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
1210 fspcavail);
1211
1212 /*
1213 * Keep track of the starting point. Skip over the start-of-list
1214 * entry.
1215 */
1216 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
1217 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
1218 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
1219
1220 /*
1221 * Process once through the active list or until no more space is
1222 * available in the request queue or the Tx FIFO
1223 */
1224 do {
1225 tx_status = readl(hsotg->regs + GNPTXSTS);
1226 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1227 TXSTS_QSPCAVAIL_SHIFT;
1228 if (hsotg->core_params->dma_enable <= 0 && qspcavail == 0) {
1229 no_queue_space = 1;
1230 break;
1231 }
1232
1233 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
1234 qh_list_entry);
1235 if (!qh->channel)
1236 goto next;
1237
1238 /* Make sure EP's TT buffer is clean before queueing qtds */
1239 if (qh->tt_buffer_dirty)
1240 goto next;
1241
1242 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1243 TXSTS_FSPCAVAIL_SHIFT;
1244 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
1245
1246 if (status > 0) {
1247 more_to_do = 1;
1248 } else if (status < 0) {
1249 no_fifo_space = 1;
1250 break;
1251 }
1252 next:
1253 /* Advance to next QH, skipping start-of-list entry */
1254 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
1255 if (hsotg->non_periodic_qh_ptr ==
1256 &hsotg->non_periodic_sched_active)
1257 hsotg->non_periodic_qh_ptr =
1258 hsotg->non_periodic_qh_ptr->next;
1259 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
1260
1261 if (hsotg->core_params->dma_enable <= 0) {
1262 tx_status = readl(hsotg->regs + GNPTXSTS);
1263 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1264 TXSTS_QSPCAVAIL_SHIFT;
1265 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1266 TXSTS_FSPCAVAIL_SHIFT;
1267 dev_vdbg(hsotg->dev,
1268 " NP Tx Req Queue Space Avail (after queue): %d\n",
1269 qspcavail);
1270 dev_vdbg(hsotg->dev,
1271 " NP Tx FIFO Space Avail (after queue): %d\n",
1272 fspcavail);
1273
1274 if (more_to_do || no_queue_space || no_fifo_space) {
1275 /*
1276 * May need to queue more transactions as the request
1277 * queue or Tx FIFO empties. Enable the non-periodic
1278 * Tx FIFO empty interrupt. (Always use the half-empty
1279 * level to ensure that new requests are loaded as
1280 * soon as possible.)
1281 */
1282 gintmsk = readl(hsotg->regs + GINTMSK);
1283 gintmsk |= GINTSTS_NPTXFEMP;
1284 writel(gintmsk, hsotg->regs + GINTMSK);
1285 } else {
1286 /*
1287 * Disable the Tx FIFO empty interrupt since there are
1288 * no more transactions that need to be queued right
1289 * now. This function is called from interrupt
1290 * handlers to queue more transactions as transfer
1291 * states change.
1292 */
1293 gintmsk = readl(hsotg->regs + GINTMSK);
1294 gintmsk &= ~GINTSTS_NPTXFEMP;
1295 writel(gintmsk, hsotg->regs + GINTMSK);
1296 }
1297 }
1298 }
1299
1300 /**
1301 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
1302 * and queues transactions for these channels to the DWC_otg controller. Called
1303 * from the HCD interrupt handler functions.
1304 *
1305 * @hsotg: The HCD state structure
1306 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
1307 * or both)
1308 *
1309 * Must be called with interrupt disabled and spinlock held
1310 */
1311 void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
1312 enum dwc2_transaction_type tr_type)
1313 {
1314 #ifdef DWC2_DEBUG_SOF
1315 dev_vdbg(hsotg->dev, "Queue Transactions\n");
1316 #endif
1317 /* Process host channels associated with periodic transfers */
1318 if ((tr_type == DWC2_TRANSACTION_PERIODIC ||
1319 tr_type == DWC2_TRANSACTION_ALL) &&
1320 !list_empty(&hsotg->periodic_sched_assigned))
1321 dwc2_process_periodic_channels(hsotg);
1322
1323 /* Process host channels associated with non-periodic transfers */
1324 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
1325 tr_type == DWC2_TRANSACTION_ALL) {
1326 if (!list_empty(&hsotg->non_periodic_sched_active)) {
1327 dwc2_process_non_periodic_channels(hsotg);
1328 } else {
1329 /*
1330 * Ensure NP Tx FIFO empty interrupt is disabled when
1331 * there are no non-periodic transfers to process
1332 */
1333 u32 gintmsk = readl(hsotg->regs + GINTMSK);
1334
1335 gintmsk &= ~GINTSTS_NPTXFEMP;
1336 writel(gintmsk, hsotg->regs + GINTMSK);
1337 }
1338 }
1339 }
1340
1341 static void dwc2_conn_id_status_change(struct work_struct *work)
1342 {
1343 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
1344 wf_otg);
1345 u32 count = 0;
1346 u32 gotgctl;
1347
1348 dev_dbg(hsotg->dev, "%s()\n", __func__);
1349
1350 gotgctl = readl(hsotg->regs + GOTGCTL);
1351 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
1352 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
1353 !!(gotgctl & GOTGCTL_CONID_B));
1354
1355 /* B-Device connector (Device Mode) */
1356 if (gotgctl & GOTGCTL_CONID_B) {
1357 /* Wait for switch to device mode */
1358 dev_dbg(hsotg->dev, "connId B\n");
1359 while (!dwc2_is_device_mode(hsotg)) {
1360 dev_info(hsotg->dev,
1361 "Waiting for Peripheral Mode, Mode=%s\n",
1362 dwc2_is_host_mode(hsotg) ? "Host" :
1363 "Peripheral");
1364 usleep_range(20000, 40000);
1365 if (++count > 250)
1366 break;
1367 }
1368 if (count > 250)
1369 dev_err(hsotg->dev,
1370 "Connection id status change timed out\n");
1371 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
1372 dwc2_core_init(hsotg, false, -1);
1373 dwc2_enable_global_interrupts(hsotg);
1374 s3c_hsotg_core_init_disconnected(hsotg);
1375 s3c_hsotg_core_connect(hsotg);
1376 } else {
1377 /* A-Device connector (Host Mode) */
1378 dev_dbg(hsotg->dev, "connId A\n");
1379 while (!dwc2_is_host_mode(hsotg)) {
1380 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
1381 dwc2_is_host_mode(hsotg) ?
1382 "Host" : "Peripheral");
1383 usleep_range(20000, 40000);
1384 if (++count > 250)
1385 break;
1386 }
1387 if (count > 250)
1388 dev_err(hsotg->dev,
1389 "Connection id status change timed out\n");
1390 hsotg->op_state = OTG_STATE_A_HOST;
1391
1392 /* Initialize the Core for Host mode */
1393 dwc2_core_init(hsotg, false, -1);
1394 dwc2_enable_global_interrupts(hsotg);
1395 dwc2_hcd_start(hsotg);
1396 }
1397 }
1398
1399 static void dwc2_wakeup_detected(unsigned long data)
1400 {
1401 struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)data;
1402 u32 hprt0;
1403
1404 dev_dbg(hsotg->dev, "%s()\n", __func__);
1405
1406 /*
1407 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
1408 * so that OPT tests pass with all PHYs.)
1409 */
1410 hprt0 = dwc2_read_hprt0(hsotg);
1411 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
1412 hprt0 &= ~HPRT0_RES;
1413 writel(hprt0, hsotg->regs + HPRT0);
1414 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
1415 readl(hsotg->regs + HPRT0));
1416
1417 dwc2_hcd_rem_wakeup(hsotg);
1418
1419 /* Change to L0 state */
1420 hsotg->lx_state = DWC2_L0;
1421 }
1422
1423 static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
1424 {
1425 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
1426
1427 return hcd->self.b_hnp_enable;
1428 }
1429
1430 /* Must NOT be called with interrupt disabled or spinlock held */
1431 static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
1432 {
1433 unsigned long flags;
1434 u32 hprt0;
1435 u32 pcgctl;
1436 u32 gotgctl;
1437
1438 dev_dbg(hsotg->dev, "%s()\n", __func__);
1439
1440 spin_lock_irqsave(&hsotg->lock, flags);
1441
1442 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
1443 gotgctl = readl(hsotg->regs + GOTGCTL);
1444 gotgctl |= GOTGCTL_HSTSETHNPEN;
1445 writel(gotgctl, hsotg->regs + GOTGCTL);
1446 hsotg->op_state = OTG_STATE_A_SUSPEND;
1447 }
1448
1449 hprt0 = dwc2_read_hprt0(hsotg);
1450 hprt0 |= HPRT0_SUSP;
1451 writel(hprt0, hsotg->regs + HPRT0);
1452
1453 /* Update lx_state */
1454 hsotg->lx_state = DWC2_L2;
1455
1456 /* Suspend the Phy Clock */
1457 pcgctl = readl(hsotg->regs + PCGCTL);
1458 pcgctl |= PCGCTL_STOPPCLK;
1459 writel(pcgctl, hsotg->regs + PCGCTL);
1460 udelay(10);
1461
1462 /* For HNP the bus must be suspended for at least 200ms */
1463 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
1464 pcgctl = readl(hsotg->regs + PCGCTL);
1465 pcgctl &= ~PCGCTL_STOPPCLK;
1466 writel(pcgctl, hsotg->regs + PCGCTL);
1467
1468 spin_unlock_irqrestore(&hsotg->lock, flags);
1469
1470 usleep_range(200000, 250000);
1471 } else {
1472 spin_unlock_irqrestore(&hsotg->lock, flags);
1473 }
1474 }
1475
1476 static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
1477 {
1478 u32 hprt0;
1479
1480 /* After clear the Stop PHY clock bit, we should wait for a moment
1481 * for PLL work stable with clock output.
1482 */
1483 writel(0, hsotg->regs + PCGCTL);
1484 usleep_range(2000, 4000);
1485
1486 hprt0 = dwc2_read_hprt0(hsotg);
1487 hprt0 |= HPRT0_RES;
1488 writel(hprt0, hsotg->regs + HPRT0);
1489 hprt0 &= ~HPRT0_SUSP;
1490 /* according to USB2.0 Spec 7.1.7.7, the host must send the resume
1491 * signal for at least 20ms
1492 */
1493 usleep_range(20000, 25000);
1494
1495 hprt0 &= ~HPRT0_RES;
1496 writel(hprt0, hsotg->regs + HPRT0);
1497 hsotg->lx_state = DWC2_L0;
1498 }
1499
1500 /* Handles hub class-specific requests */
1501 static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
1502 u16 wvalue, u16 windex, char *buf, u16 wlength)
1503 {
1504 struct usb_hub_descriptor *hub_desc;
1505 int retval = 0;
1506 u32 hprt0;
1507 u32 port_status;
1508 u32 speed;
1509 u32 pcgctl;
1510
1511 switch (typereq) {
1512 case ClearHubFeature:
1513 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
1514
1515 switch (wvalue) {
1516 case C_HUB_LOCAL_POWER:
1517 case C_HUB_OVER_CURRENT:
1518 /* Nothing required here */
1519 break;
1520
1521 default:
1522 retval = -EINVAL;
1523 dev_err(hsotg->dev,
1524 "ClearHubFeature request %1xh unknown\n",
1525 wvalue);
1526 }
1527 break;
1528
1529 case ClearPortFeature:
1530 if (wvalue != USB_PORT_FEAT_L1)
1531 if (!windex || windex > 1)
1532 goto error;
1533 switch (wvalue) {
1534 case USB_PORT_FEAT_ENABLE:
1535 dev_dbg(hsotg->dev,
1536 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
1537 hprt0 = dwc2_read_hprt0(hsotg);
1538 hprt0 |= HPRT0_ENA;
1539 writel(hprt0, hsotg->regs + HPRT0);
1540 break;
1541
1542 case USB_PORT_FEAT_SUSPEND:
1543 dev_dbg(hsotg->dev,
1544 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
1545 dwc2_port_resume(hsotg);
1546 break;
1547
1548 case USB_PORT_FEAT_POWER:
1549 dev_dbg(hsotg->dev,
1550 "ClearPortFeature USB_PORT_FEAT_POWER\n");
1551 hprt0 = dwc2_read_hprt0(hsotg);
1552 hprt0 &= ~HPRT0_PWR;
1553 writel(hprt0, hsotg->regs + HPRT0);
1554 break;
1555
1556 case USB_PORT_FEAT_INDICATOR:
1557 dev_dbg(hsotg->dev,
1558 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
1559 /* Port indicator not supported */
1560 break;
1561
1562 case USB_PORT_FEAT_C_CONNECTION:
1563 /*
1564 * Clears driver's internal Connect Status Change flag
1565 */
1566 dev_dbg(hsotg->dev,
1567 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
1568 hsotg->flags.b.port_connect_status_change = 0;
1569 break;
1570
1571 case USB_PORT_FEAT_C_RESET:
1572 /* Clears driver's internal Port Reset Change flag */
1573 dev_dbg(hsotg->dev,
1574 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
1575 hsotg->flags.b.port_reset_change = 0;
1576 break;
1577
1578 case USB_PORT_FEAT_C_ENABLE:
1579 /*
1580 * Clears the driver's internal Port Enable/Disable
1581 * Change flag
1582 */
1583 dev_dbg(hsotg->dev,
1584 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
1585 hsotg->flags.b.port_enable_change = 0;
1586 break;
1587
1588 case USB_PORT_FEAT_C_SUSPEND:
1589 /*
1590 * Clears the driver's internal Port Suspend Change
1591 * flag, which is set when resume signaling on the host
1592 * port is complete
1593 */
1594 dev_dbg(hsotg->dev,
1595 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
1596 hsotg->flags.b.port_suspend_change = 0;
1597 break;
1598
1599 case USB_PORT_FEAT_C_PORT_L1:
1600 dev_dbg(hsotg->dev,
1601 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
1602 hsotg->flags.b.port_l1_change = 0;
1603 break;
1604
1605 case USB_PORT_FEAT_C_OVER_CURRENT:
1606 dev_dbg(hsotg->dev,
1607 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
1608 hsotg->flags.b.port_over_current_change = 0;
1609 break;
1610
1611 default:
1612 retval = -EINVAL;
1613 dev_err(hsotg->dev,
1614 "ClearPortFeature request %1xh unknown or unsupported\n",
1615 wvalue);
1616 }
1617 break;
1618
1619 case GetHubDescriptor:
1620 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
1621 hub_desc = (struct usb_hub_descriptor *)buf;
1622 hub_desc->bDescLength = 9;
1623 hub_desc->bDescriptorType = 0x29;
1624 hub_desc->bNbrPorts = 1;
1625 hub_desc->wHubCharacteristics =
1626 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
1627 HUB_CHAR_INDV_PORT_OCPM);
1628 hub_desc->bPwrOn2PwrGood = 1;
1629 hub_desc->bHubContrCurrent = 0;
1630 hub_desc->u.hs.DeviceRemovable[0] = 0;
1631 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
1632 break;
1633
1634 case GetHubStatus:
1635 dev_dbg(hsotg->dev, "GetHubStatus\n");
1636 memset(buf, 0, 4);
1637 break;
1638
1639 case GetPortStatus:
1640 dev_vdbg(hsotg->dev,
1641 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
1642 hsotg->flags.d32);
1643 if (!windex || windex > 1)
1644 goto error;
1645
1646 port_status = 0;
1647 if (hsotg->flags.b.port_connect_status_change)
1648 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
1649 if (hsotg->flags.b.port_enable_change)
1650 port_status |= USB_PORT_STAT_C_ENABLE << 16;
1651 if (hsotg->flags.b.port_suspend_change)
1652 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
1653 if (hsotg->flags.b.port_l1_change)
1654 port_status |= USB_PORT_STAT_C_L1 << 16;
1655 if (hsotg->flags.b.port_reset_change)
1656 port_status |= USB_PORT_STAT_C_RESET << 16;
1657 if (hsotg->flags.b.port_over_current_change) {
1658 dev_warn(hsotg->dev, "Overcurrent change detected\n");
1659 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1660 }
1661
1662 if (!hsotg->flags.b.port_connect_status) {
1663 /*
1664 * The port is disconnected, which means the core is
1665 * either in device mode or it soon will be. Just
1666 * return 0's for the remainder of the port status
1667 * since the port register can't be read if the core
1668 * is in device mode.
1669 */
1670 *(__le32 *)buf = cpu_to_le32(port_status);
1671 break;
1672 }
1673
1674 hprt0 = readl(hsotg->regs + HPRT0);
1675 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
1676
1677 if (hprt0 & HPRT0_CONNSTS)
1678 port_status |= USB_PORT_STAT_CONNECTION;
1679 if (hprt0 & HPRT0_ENA)
1680 port_status |= USB_PORT_STAT_ENABLE;
1681 if (hprt0 & HPRT0_SUSP)
1682 port_status |= USB_PORT_STAT_SUSPEND;
1683 if (hprt0 & HPRT0_OVRCURRACT)
1684 port_status |= USB_PORT_STAT_OVERCURRENT;
1685 if (hprt0 & HPRT0_RST)
1686 port_status |= USB_PORT_STAT_RESET;
1687 if (hprt0 & HPRT0_PWR)
1688 port_status |= USB_PORT_STAT_POWER;
1689
1690 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
1691 if (speed == HPRT0_SPD_HIGH_SPEED)
1692 port_status |= USB_PORT_STAT_HIGH_SPEED;
1693 else if (speed == HPRT0_SPD_LOW_SPEED)
1694 port_status |= USB_PORT_STAT_LOW_SPEED;
1695
1696 if (hprt0 & HPRT0_TSTCTL_MASK)
1697 port_status |= USB_PORT_STAT_TEST;
1698 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
1699
1700 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
1701 *(__le32 *)buf = cpu_to_le32(port_status);
1702 break;
1703
1704 case SetHubFeature:
1705 dev_dbg(hsotg->dev, "SetHubFeature\n");
1706 /* No HUB features supported */
1707 break;
1708
1709 case SetPortFeature:
1710 dev_dbg(hsotg->dev, "SetPortFeature\n");
1711 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
1712 goto error;
1713
1714 if (!hsotg->flags.b.port_connect_status) {
1715 /*
1716 * The port is disconnected, which means the core is
1717 * either in device mode or it soon will be. Just
1718 * return without doing anything since the port
1719 * register can't be written if the core is in device
1720 * mode.
1721 */
1722 break;
1723 }
1724
1725 switch (wvalue) {
1726 case USB_PORT_FEAT_SUSPEND:
1727 dev_dbg(hsotg->dev,
1728 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
1729 if (windex != hsotg->otg_port)
1730 goto error;
1731 dwc2_port_suspend(hsotg, windex);
1732 break;
1733
1734 case USB_PORT_FEAT_POWER:
1735 dev_dbg(hsotg->dev,
1736 "SetPortFeature - USB_PORT_FEAT_POWER\n");
1737 hprt0 = dwc2_read_hprt0(hsotg);
1738 hprt0 |= HPRT0_PWR;
1739 writel(hprt0, hsotg->regs + HPRT0);
1740 break;
1741
1742 case USB_PORT_FEAT_RESET:
1743 hprt0 = dwc2_read_hprt0(hsotg);
1744 dev_dbg(hsotg->dev,
1745 "SetPortFeature - USB_PORT_FEAT_RESET\n");
1746 pcgctl = readl(hsotg->regs + PCGCTL);
1747 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
1748 writel(pcgctl, hsotg->regs + PCGCTL);
1749 /* ??? Original driver does this */
1750 writel(0, hsotg->regs + PCGCTL);
1751
1752 hprt0 = dwc2_read_hprt0(hsotg);
1753 /* Clear suspend bit if resetting from suspend state */
1754 hprt0 &= ~HPRT0_SUSP;
1755
1756 /*
1757 * When B-Host the Port reset bit is set in the Start
1758 * HCD Callback function, so that the reset is started
1759 * within 1ms of the HNP success interrupt
1760 */
1761 if (!dwc2_hcd_is_b_host(hsotg)) {
1762 hprt0 |= HPRT0_PWR | HPRT0_RST;
1763 dev_dbg(hsotg->dev,
1764 "In host mode, hprt0=%08x\n", hprt0);
1765 writel(hprt0, hsotg->regs + HPRT0);
1766 }
1767
1768 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
1769 usleep_range(50000, 70000);
1770 hprt0 &= ~HPRT0_RST;
1771 writel(hprt0, hsotg->regs + HPRT0);
1772 hsotg->lx_state = DWC2_L0; /* Now back to On state */
1773 break;
1774
1775 case USB_PORT_FEAT_INDICATOR:
1776 dev_dbg(hsotg->dev,
1777 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
1778 /* Not supported */
1779 break;
1780
1781 default:
1782 retval = -EINVAL;
1783 dev_err(hsotg->dev,
1784 "SetPortFeature %1xh unknown or unsupported\n",
1785 wvalue);
1786 break;
1787 }
1788 break;
1789
1790 default:
1791 error:
1792 retval = -EINVAL;
1793 dev_dbg(hsotg->dev,
1794 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
1795 typereq, windex, wvalue);
1796 break;
1797 }
1798
1799 return retval;
1800 }
1801
1802 static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
1803 {
1804 int retval;
1805
1806 if (port != 1)
1807 return -EINVAL;
1808
1809 retval = (hsotg->flags.b.port_connect_status_change ||
1810 hsotg->flags.b.port_reset_change ||
1811 hsotg->flags.b.port_enable_change ||
1812 hsotg->flags.b.port_suspend_change ||
1813 hsotg->flags.b.port_over_current_change);
1814
1815 if (retval) {
1816 dev_dbg(hsotg->dev,
1817 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
1818 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
1819 hsotg->flags.b.port_connect_status_change);
1820 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
1821 hsotg->flags.b.port_reset_change);
1822 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
1823 hsotg->flags.b.port_enable_change);
1824 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
1825 hsotg->flags.b.port_suspend_change);
1826 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
1827 hsotg->flags.b.port_over_current_change);
1828 }
1829
1830 return retval;
1831 }
1832
1833 int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
1834 {
1835 u32 hfnum = readl(hsotg->regs + HFNUM);
1836
1837 #ifdef DWC2_DEBUG_SOF
1838 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
1839 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
1840 #endif
1841 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
1842 }
1843
1844 int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
1845 {
1846 return hsotg->op_state == OTG_STATE_B_HOST;
1847 }
1848
1849 static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
1850 int iso_desc_count,
1851 gfp_t mem_flags)
1852 {
1853 struct dwc2_hcd_urb *urb;
1854 u32 size = sizeof(*urb) + iso_desc_count *
1855 sizeof(struct dwc2_hcd_iso_packet_desc);
1856
1857 urb = kzalloc(size, mem_flags);
1858 if (urb)
1859 urb->packet_count = iso_desc_count;
1860 return urb;
1861 }
1862
1863 static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
1864 struct dwc2_hcd_urb *urb, u8 dev_addr,
1865 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
1866 {
1867 if (dbg_perio() ||
1868 ep_type == USB_ENDPOINT_XFER_BULK ||
1869 ep_type == USB_ENDPOINT_XFER_CONTROL)
1870 dev_vdbg(hsotg->dev,
1871 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
1872 dev_addr, ep_num, ep_dir, ep_type, mps);
1873 urb->pipe_info.dev_addr = dev_addr;
1874 urb->pipe_info.ep_num = ep_num;
1875 urb->pipe_info.pipe_type = ep_type;
1876 urb->pipe_info.pipe_dir = ep_dir;
1877 urb->pipe_info.mps = mps;
1878 }
1879
1880 /*
1881 * NOTE: This function will be removed once the peripheral controller code
1882 * is integrated and the driver is stable
1883 */
1884 void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
1885 {
1886 #ifdef DEBUG
1887 struct dwc2_host_chan *chan;
1888 struct dwc2_hcd_urb *urb;
1889 struct dwc2_qtd *qtd;
1890 int num_channels;
1891 u32 np_tx_status;
1892 u32 p_tx_status;
1893 int i;
1894
1895 num_channels = hsotg->core_params->host_channels;
1896 dev_dbg(hsotg->dev, "\n");
1897 dev_dbg(hsotg->dev,
1898 "************************************************************\n");
1899 dev_dbg(hsotg->dev, "HCD State:\n");
1900 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
1901
1902 for (i = 0; i < num_channels; i++) {
1903 chan = hsotg->hc_ptr_array[i];
1904 dev_dbg(hsotg->dev, " Channel %d:\n", i);
1905 dev_dbg(hsotg->dev,
1906 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
1907 chan->dev_addr, chan->ep_num, chan->ep_is_in);
1908 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
1909 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
1910 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
1911 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
1912 chan->data_pid_start);
1913 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
1914 dev_dbg(hsotg->dev, " xfer_started: %d\n",
1915 chan->xfer_started);
1916 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
1917 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
1918 (unsigned long)chan->xfer_dma);
1919 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
1920 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
1921 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
1922 chan->halt_on_queue);
1923 dev_dbg(hsotg->dev, " halt_pending: %d\n",
1924 chan->halt_pending);
1925 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
1926 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
1927 dev_dbg(hsotg->dev, " complete_split: %d\n",
1928 chan->complete_split);
1929 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
1930 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
1931 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
1932 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
1933 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
1934
1935 if (chan->xfer_started) {
1936 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
1937
1938 hfnum = readl(hsotg->regs + HFNUM);
1939 hcchar = readl(hsotg->regs + HCCHAR(i));
1940 hctsiz = readl(hsotg->regs + HCTSIZ(i));
1941 hcint = readl(hsotg->regs + HCINT(i));
1942 hcintmsk = readl(hsotg->regs + HCINTMSK(i));
1943 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
1944 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
1945 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
1946 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
1947 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
1948 }
1949
1950 if (!(chan->xfer_started && chan->qh))
1951 continue;
1952
1953 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
1954 if (!qtd->in_process)
1955 break;
1956 urb = qtd->urb;
1957 dev_dbg(hsotg->dev, " URB Info:\n");
1958 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
1959 qtd, urb);
1960 if (urb) {
1961 dev_dbg(hsotg->dev,
1962 " Dev: %d, EP: %d %s\n",
1963 dwc2_hcd_get_dev_addr(&urb->pipe_info),
1964 dwc2_hcd_get_ep_num(&urb->pipe_info),
1965 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
1966 "IN" : "OUT");
1967 dev_dbg(hsotg->dev,
1968 " Max packet size: %d\n",
1969 dwc2_hcd_get_mps(&urb->pipe_info));
1970 dev_dbg(hsotg->dev,
1971 " transfer_buffer: %p\n",
1972 urb->buf);
1973 dev_dbg(hsotg->dev,
1974 " transfer_dma: %08lx\n",
1975 (unsigned long)urb->dma);
1976 dev_dbg(hsotg->dev,
1977 " transfer_buffer_length: %d\n",
1978 urb->length);
1979 dev_dbg(hsotg->dev, " actual_length: %d\n",
1980 urb->actual_length);
1981 }
1982 }
1983 }
1984
1985 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
1986 hsotg->non_periodic_channels);
1987 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
1988 hsotg->periodic_channels);
1989 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
1990 np_tx_status = readl(hsotg->regs + GNPTXSTS);
1991 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
1992 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
1993 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
1994 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
1995 p_tx_status = readl(hsotg->regs + HPTXSTS);
1996 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
1997 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
1998 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
1999 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
2000 dwc2_hcd_dump_frrem(hsotg);
2001 dwc2_dump_global_registers(hsotg);
2002 dwc2_dump_host_registers(hsotg);
2003 dev_dbg(hsotg->dev,
2004 "************************************************************\n");
2005 dev_dbg(hsotg->dev, "\n");
2006 #endif
2007 }
2008
2009 /*
2010 * NOTE: This function will be removed once the peripheral controller code
2011 * is integrated and the driver is stable
2012 */
2013 void dwc2_hcd_dump_frrem(struct dwc2_hsotg *hsotg)
2014 {
2015 #ifdef DWC2_DUMP_FRREM
2016 dev_dbg(hsotg->dev, "Frame remaining at SOF:\n");
2017 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2018 hsotg->frrem_samples, hsotg->frrem_accum,
2019 hsotg->frrem_samples > 0 ?
2020 hsotg->frrem_accum / hsotg->frrem_samples : 0);
2021 dev_dbg(hsotg->dev, "\n");
2022 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 7):\n");
2023 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2024 hsotg->hfnum_7_samples,
2025 hsotg->hfnum_7_frrem_accum,
2026 hsotg->hfnum_7_samples > 0 ?
2027 hsotg->hfnum_7_frrem_accum / hsotg->hfnum_7_samples : 0);
2028 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 0):\n");
2029 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2030 hsotg->hfnum_0_samples,
2031 hsotg->hfnum_0_frrem_accum,
2032 hsotg->hfnum_0_samples > 0 ?
2033 hsotg->hfnum_0_frrem_accum / hsotg->hfnum_0_samples : 0);
2034 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 1-6):\n");
2035 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2036 hsotg->hfnum_other_samples,
2037 hsotg->hfnum_other_frrem_accum,
2038 hsotg->hfnum_other_samples > 0 ?
2039 hsotg->hfnum_other_frrem_accum / hsotg->hfnum_other_samples :
2040 0);
2041 dev_dbg(hsotg->dev, "\n");
2042 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 7):\n");
2043 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2044 hsotg->hfnum_7_samples_a, hsotg->hfnum_7_frrem_accum_a,
2045 hsotg->hfnum_7_samples_a > 0 ?
2046 hsotg->hfnum_7_frrem_accum_a / hsotg->hfnum_7_samples_a : 0);
2047 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 0):\n");
2048 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2049 hsotg->hfnum_0_samples_a, hsotg->hfnum_0_frrem_accum_a,
2050 hsotg->hfnum_0_samples_a > 0 ?
2051 hsotg->hfnum_0_frrem_accum_a / hsotg->hfnum_0_samples_a : 0);
2052 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 1-6):\n");
2053 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2054 hsotg->hfnum_other_samples_a, hsotg->hfnum_other_frrem_accum_a,
2055 hsotg->hfnum_other_samples_a > 0 ?
2056 hsotg->hfnum_other_frrem_accum_a / hsotg->hfnum_other_samples_a
2057 : 0);
2058 dev_dbg(hsotg->dev, "\n");
2059 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 7):\n");
2060 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2061 hsotg->hfnum_7_samples_b, hsotg->hfnum_7_frrem_accum_b,
2062 hsotg->hfnum_7_samples_b > 0 ?
2063 hsotg->hfnum_7_frrem_accum_b / hsotg->hfnum_7_samples_b : 0);
2064 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 0):\n");
2065 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2066 hsotg->hfnum_0_samples_b, hsotg->hfnum_0_frrem_accum_b,
2067 (hsotg->hfnum_0_samples_b > 0) ?
2068 hsotg->hfnum_0_frrem_accum_b / hsotg->hfnum_0_samples_b : 0);
2069 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 1-6):\n");
2070 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2071 hsotg->hfnum_other_samples_b, hsotg->hfnum_other_frrem_accum_b,
2072 (hsotg->hfnum_other_samples_b > 0) ?
2073 hsotg->hfnum_other_frrem_accum_b / hsotg->hfnum_other_samples_b
2074 : 0);
2075 #endif
2076 }
2077
2078 struct wrapper_priv_data {
2079 struct dwc2_hsotg *hsotg;
2080 };
2081
2082 /* Gets the dwc2_hsotg from a usb_hcd */
2083 static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
2084 {
2085 struct wrapper_priv_data *p;
2086
2087 p = (struct wrapper_priv_data *) &hcd->hcd_priv;
2088 return p->hsotg;
2089 }
2090
2091 static int _dwc2_hcd_start(struct usb_hcd *hcd);
2092
2093 void dwc2_host_start(struct dwc2_hsotg *hsotg)
2094 {
2095 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
2096
2097 hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
2098 _dwc2_hcd_start(hcd);
2099 }
2100
2101 void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
2102 {
2103 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
2104
2105 hcd->self.is_b_host = 0;
2106 }
2107
2108 void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, int *hub_addr,
2109 int *hub_port)
2110 {
2111 struct urb *urb = context;
2112
2113 if (urb->dev->tt)
2114 *hub_addr = urb->dev->tt->hub->devnum;
2115 else
2116 *hub_addr = 0;
2117 *hub_port = urb->dev->ttport;
2118 }
2119
2120 int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
2121 {
2122 struct urb *urb = context;
2123
2124 return urb->dev->speed;
2125 }
2126
2127 static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
2128 struct urb *urb)
2129 {
2130 struct usb_bus *bus = hcd_to_bus(hcd);
2131
2132 if (urb->interval)
2133 bus->bandwidth_allocated += bw / urb->interval;
2134 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2135 bus->bandwidth_isoc_reqs++;
2136 else
2137 bus->bandwidth_int_reqs++;
2138 }
2139
2140 static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
2141 struct urb *urb)
2142 {
2143 struct usb_bus *bus = hcd_to_bus(hcd);
2144
2145 if (urb->interval)
2146 bus->bandwidth_allocated -= bw / urb->interval;
2147 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2148 bus->bandwidth_isoc_reqs--;
2149 else
2150 bus->bandwidth_int_reqs--;
2151 }
2152
2153 /*
2154 * Sets the final status of an URB and returns it to the upper layer. Any
2155 * required cleanup of the URB is performed.
2156 *
2157 * Must be called with interrupt disabled and spinlock held
2158 */
2159 void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
2160 int status)
2161 {
2162 struct urb *urb;
2163 int i;
2164
2165 if (!qtd) {
2166 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
2167 return;
2168 }
2169
2170 if (!qtd->urb) {
2171 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
2172 return;
2173 }
2174
2175 urb = qtd->urb->priv;
2176 if (!urb) {
2177 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
2178 return;
2179 }
2180
2181 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
2182
2183 if (dbg_urb(urb))
2184 dev_vdbg(hsotg->dev,
2185 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
2186 __func__, urb, usb_pipedevice(urb->pipe),
2187 usb_pipeendpoint(urb->pipe),
2188 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
2189 urb->actual_length);
2190
2191 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
2192 for (i = 0; i < urb->number_of_packets; i++)
2193 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
2194 i, urb->iso_frame_desc[i].status);
2195 }
2196
2197 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2198 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
2199 for (i = 0; i < urb->number_of_packets; ++i) {
2200 urb->iso_frame_desc[i].actual_length =
2201 dwc2_hcd_urb_get_iso_desc_actual_length(
2202 qtd->urb, i);
2203 urb->iso_frame_desc[i].status =
2204 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
2205 }
2206 }
2207
2208 urb->status = status;
2209 if (!status) {
2210 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
2211 urb->actual_length < urb->transfer_buffer_length)
2212 urb->status = -EREMOTEIO;
2213 }
2214
2215 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
2216 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2217 struct usb_host_endpoint *ep = urb->ep;
2218
2219 if (ep)
2220 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
2221 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
2222 urb);
2223 }
2224
2225 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
2226 urb->hcpriv = NULL;
2227 kfree(qtd->urb);
2228 qtd->urb = NULL;
2229
2230 spin_unlock(&hsotg->lock);
2231 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
2232 spin_lock(&hsotg->lock);
2233 }
2234
2235 /*
2236 * Work queue function for starting the HCD when A-Cable is connected
2237 */
2238 static void dwc2_hcd_start_func(struct work_struct *work)
2239 {
2240 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
2241 start_work.work);
2242
2243 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
2244 dwc2_host_start(hsotg);
2245 }
2246
2247 /*
2248 * Reset work queue function
2249 */
2250 static void dwc2_hcd_reset_func(struct work_struct *work)
2251 {
2252 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
2253 reset_work.work);
2254 u32 hprt0;
2255
2256 dev_dbg(hsotg->dev, "USB RESET function called\n");
2257 hprt0 = dwc2_read_hprt0(hsotg);
2258 hprt0 &= ~HPRT0_RST;
2259 writel(hprt0, hsotg->regs + HPRT0);
2260 hsotg->flags.b.port_reset_change = 1;
2261 }
2262
2263 /*
2264 * =========================================================================
2265 * Linux HC Driver Functions
2266 * =========================================================================
2267 */
2268
2269 /*
2270 * Initializes the DWC_otg controller and its root hub and prepares it for host
2271 * mode operation. Activates the root port. Returns 0 on success and a negative
2272 * error code on failure.
2273 */
2274 static int _dwc2_hcd_start(struct usb_hcd *hcd)
2275 {
2276 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2277 struct usb_bus *bus = hcd_to_bus(hcd);
2278 unsigned long flags;
2279
2280 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
2281
2282 spin_lock_irqsave(&hsotg->lock, flags);
2283
2284 hcd->state = HC_STATE_RUNNING;
2285
2286 if (dwc2_is_device_mode(hsotg)) {
2287 spin_unlock_irqrestore(&hsotg->lock, flags);
2288 return 0; /* why 0 ?? */
2289 }
2290
2291 dwc2_hcd_reinit(hsotg);
2292
2293 /* Initialize and connect root hub if one is not already attached */
2294 if (bus->root_hub) {
2295 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
2296 /* Inform the HUB driver to resume */
2297 usb_hcd_resume_root_hub(hcd);
2298 }
2299
2300 spin_unlock_irqrestore(&hsotg->lock, flags);
2301 return 0;
2302 }
2303
2304 /*
2305 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
2306 * stopped.
2307 */
2308 static void _dwc2_hcd_stop(struct usb_hcd *hcd)
2309 {
2310 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2311 unsigned long flags;
2312
2313 spin_lock_irqsave(&hsotg->lock, flags);
2314 dwc2_hcd_stop(hsotg);
2315 spin_unlock_irqrestore(&hsotg->lock, flags);
2316
2317 usleep_range(1000, 3000);
2318 }
2319
2320 static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
2321 {
2322 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2323 u32 hprt0;
2324
2325 if (!((hsotg->op_state == OTG_STATE_B_HOST) ||
2326 (hsotg->op_state == OTG_STATE_A_HOST)))
2327 return 0;
2328
2329 /* TODO: We get into suspend from 'on' state, maybe we need to do
2330 * something if we get here from DWC2_L1(LPM sleep) state one day.
2331 */
2332 if (hsotg->lx_state != DWC2_L0)
2333 return 0;
2334
2335 hprt0 = dwc2_read_hprt0(hsotg);
2336 if (hprt0 & HPRT0_CONNSTS) {
2337 dwc2_port_suspend(hsotg, 1);
2338 } else {
2339 u32 pcgctl = readl(hsotg->regs + PCGCTL);
2340
2341 pcgctl |= PCGCTL_STOPPCLK;
2342 writel(pcgctl, hsotg->regs + PCGCTL);
2343 }
2344
2345 return 0;
2346 }
2347
2348 static int _dwc2_hcd_resume(struct usb_hcd *hcd)
2349 {
2350 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2351 u32 hprt0;
2352
2353 if (!((hsotg->op_state == OTG_STATE_B_HOST) ||
2354 (hsotg->op_state == OTG_STATE_A_HOST)))
2355 return 0;
2356
2357 if (hsotg->lx_state != DWC2_L2)
2358 return 0;
2359
2360 hprt0 = dwc2_read_hprt0(hsotg);
2361 if ((hprt0 & HPRT0_CONNSTS) && (hprt0 & HPRT0_SUSP))
2362 dwc2_port_resume(hsotg);
2363 else
2364 writel(0, hsotg->regs + PCGCTL);
2365
2366 return 0;
2367 }
2368
2369 /* Returns the current frame number */
2370 static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
2371 {
2372 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2373
2374 return dwc2_hcd_get_frame_number(hsotg);
2375 }
2376
2377 static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
2378 char *fn_name)
2379 {
2380 #ifdef VERBOSE_DEBUG
2381 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2382 char *pipetype;
2383 char *speed;
2384
2385 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
2386 dev_vdbg(hsotg->dev, " Device address: %d\n",
2387 usb_pipedevice(urb->pipe));
2388 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
2389 usb_pipeendpoint(urb->pipe),
2390 usb_pipein(urb->pipe) ? "IN" : "OUT");
2391
2392 switch (usb_pipetype(urb->pipe)) {
2393 case PIPE_CONTROL:
2394 pipetype = "CONTROL";
2395 break;
2396 case PIPE_BULK:
2397 pipetype = "BULK";
2398 break;
2399 case PIPE_INTERRUPT:
2400 pipetype = "INTERRUPT";
2401 break;
2402 case PIPE_ISOCHRONOUS:
2403 pipetype = "ISOCHRONOUS";
2404 break;
2405 default:
2406 pipetype = "UNKNOWN";
2407 break;
2408 }
2409
2410 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
2411 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
2412 "IN" : "OUT");
2413
2414 switch (urb->dev->speed) {
2415 case USB_SPEED_HIGH:
2416 speed = "HIGH";
2417 break;
2418 case USB_SPEED_FULL:
2419 speed = "FULL";
2420 break;
2421 case USB_SPEED_LOW:
2422 speed = "LOW";
2423 break;
2424 default:
2425 speed = "UNKNOWN";
2426 break;
2427 }
2428
2429 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
2430 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
2431 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
2432 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
2433 urb->transfer_buffer_length);
2434 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
2435 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
2436 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
2437 urb->setup_packet, (unsigned long)urb->setup_dma);
2438 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
2439
2440 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2441 int i;
2442
2443 for (i = 0; i < urb->number_of_packets; i++) {
2444 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
2445 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
2446 urb->iso_frame_desc[i].offset,
2447 urb->iso_frame_desc[i].length);
2448 }
2449 }
2450 #endif
2451 }
2452
2453 /*
2454 * Starts processing a USB transfer request specified by a USB Request Block
2455 * (URB). mem_flags indicates the type of memory allocation to use while
2456 * processing this URB.
2457 */
2458 static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
2459 gfp_t mem_flags)
2460 {
2461 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2462 struct usb_host_endpoint *ep = urb->ep;
2463 struct dwc2_hcd_urb *dwc2_urb;
2464 int i;
2465 int retval;
2466 int alloc_bandwidth = 0;
2467 u8 ep_type = 0;
2468 u32 tflags = 0;
2469 void *buf;
2470 unsigned long flags;
2471
2472 if (dbg_urb(urb)) {
2473 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
2474 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
2475 }
2476
2477 if (ep == NULL)
2478 return -EINVAL;
2479
2480 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
2481 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2482 spin_lock_irqsave(&hsotg->lock, flags);
2483 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
2484 alloc_bandwidth = 1;
2485 spin_unlock_irqrestore(&hsotg->lock, flags);
2486 }
2487
2488 switch (usb_pipetype(urb->pipe)) {
2489 case PIPE_CONTROL:
2490 ep_type = USB_ENDPOINT_XFER_CONTROL;
2491 break;
2492 case PIPE_ISOCHRONOUS:
2493 ep_type = USB_ENDPOINT_XFER_ISOC;
2494 break;
2495 case PIPE_BULK:
2496 ep_type = USB_ENDPOINT_XFER_BULK;
2497 break;
2498 case PIPE_INTERRUPT:
2499 ep_type = USB_ENDPOINT_XFER_INT;
2500 break;
2501 default:
2502 dev_warn(hsotg->dev, "Wrong ep type\n");
2503 }
2504
2505 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
2506 mem_flags);
2507 if (!dwc2_urb)
2508 return -ENOMEM;
2509
2510 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
2511 usb_pipeendpoint(urb->pipe), ep_type,
2512 usb_pipein(urb->pipe),
2513 usb_maxpacket(urb->dev, urb->pipe,
2514 !(usb_pipein(urb->pipe))));
2515
2516 buf = urb->transfer_buffer;
2517
2518 if (hcd->self.uses_dma) {
2519 if (!buf && (urb->transfer_dma & 3)) {
2520 dev_err(hsotg->dev,
2521 "%s: unaligned transfer with no transfer_buffer",
2522 __func__);
2523 retval = -EINVAL;
2524 goto fail1;
2525 }
2526 }
2527
2528 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
2529 tflags |= URB_GIVEBACK_ASAP;
2530 if (urb->transfer_flags & URB_ZERO_PACKET)
2531 tflags |= URB_SEND_ZERO_PACKET;
2532
2533 dwc2_urb->priv = urb;
2534 dwc2_urb->buf = buf;
2535 dwc2_urb->dma = urb->transfer_dma;
2536 dwc2_urb->length = urb->transfer_buffer_length;
2537 dwc2_urb->setup_packet = urb->setup_packet;
2538 dwc2_urb->setup_dma = urb->setup_dma;
2539 dwc2_urb->flags = tflags;
2540 dwc2_urb->interval = urb->interval;
2541 dwc2_urb->status = -EINPROGRESS;
2542
2543 for (i = 0; i < urb->number_of_packets; ++i)
2544 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
2545 urb->iso_frame_desc[i].offset,
2546 urb->iso_frame_desc[i].length);
2547
2548 urb->hcpriv = dwc2_urb;
2549
2550 spin_lock_irqsave(&hsotg->lock, flags);
2551 retval = usb_hcd_link_urb_to_ep(hcd, urb);
2552 spin_unlock_irqrestore(&hsotg->lock, flags);
2553 if (retval)
2554 goto fail1;
2555
2556 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, &ep->hcpriv, mem_flags);
2557 if (retval)
2558 goto fail2;
2559
2560 if (alloc_bandwidth) {
2561 spin_lock_irqsave(&hsotg->lock, flags);
2562 dwc2_allocate_bus_bandwidth(hcd,
2563 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
2564 urb);
2565 spin_unlock_irqrestore(&hsotg->lock, flags);
2566 }
2567
2568 return 0;
2569
2570 fail2:
2571 spin_lock_irqsave(&hsotg->lock, flags);
2572 dwc2_urb->priv = NULL;
2573 usb_hcd_unlink_urb_from_ep(hcd, urb);
2574 spin_unlock_irqrestore(&hsotg->lock, flags);
2575 fail1:
2576 urb->hcpriv = NULL;
2577 kfree(dwc2_urb);
2578
2579 return retval;
2580 }
2581
2582 /*
2583 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
2584 */
2585 static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
2586 int status)
2587 {
2588 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2589 int rc;
2590 unsigned long flags;
2591
2592 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
2593 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
2594
2595 spin_lock_irqsave(&hsotg->lock, flags);
2596
2597 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
2598 if (rc)
2599 goto out;
2600
2601 if (!urb->hcpriv) {
2602 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
2603 goto out;
2604 }
2605
2606 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
2607
2608 usb_hcd_unlink_urb_from_ep(hcd, urb);
2609
2610 kfree(urb->hcpriv);
2611 urb->hcpriv = NULL;
2612
2613 /* Higher layer software sets URB status */
2614 spin_unlock(&hsotg->lock);
2615 usb_hcd_giveback_urb(hcd, urb, status);
2616 spin_lock(&hsotg->lock);
2617
2618 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
2619 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
2620 out:
2621 spin_unlock_irqrestore(&hsotg->lock, flags);
2622
2623 return rc;
2624 }
2625
2626 /*
2627 * Frees resources in the DWC_otg controller related to a given endpoint. Also
2628 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
2629 * must already be dequeued.
2630 */
2631 static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
2632 struct usb_host_endpoint *ep)
2633 {
2634 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2635
2636 dev_dbg(hsotg->dev,
2637 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
2638 ep->desc.bEndpointAddress, ep->hcpriv);
2639 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
2640 }
2641
2642 /*
2643 * Resets endpoint specific parameter values, in current version used to reset
2644 * the data toggle (as a WA). This function can be called from usb_clear_halt
2645 * routine.
2646 */
2647 static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
2648 struct usb_host_endpoint *ep)
2649 {
2650 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2651 unsigned long flags;
2652
2653 dev_dbg(hsotg->dev,
2654 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
2655 ep->desc.bEndpointAddress);
2656
2657 spin_lock_irqsave(&hsotg->lock, flags);
2658 dwc2_hcd_endpoint_reset(hsotg, ep);
2659 spin_unlock_irqrestore(&hsotg->lock, flags);
2660 }
2661
2662 /*
2663 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
2664 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
2665 * interrupt.
2666 *
2667 * This function is called by the USB core when an interrupt occurs
2668 */
2669 static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
2670 {
2671 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2672
2673 return dwc2_handle_hcd_intr(hsotg);
2674 }
2675
2676 /*
2677 * Creates Status Change bitmap for the root hub and root port. The bitmap is
2678 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
2679 * is the status change indicator for the single root port. Returns 1 if either
2680 * change indicator is 1, otherwise returns 0.
2681 */
2682 static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
2683 {
2684 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2685
2686 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
2687 return buf[0] != 0;
2688 }
2689
2690 /* Handles hub class-specific requests */
2691 static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
2692 u16 windex, char *buf, u16 wlength)
2693 {
2694 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
2695 wvalue, windex, buf, wlength);
2696 return retval;
2697 }
2698
2699 /* Handles hub TT buffer clear completions */
2700 static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
2701 struct usb_host_endpoint *ep)
2702 {
2703 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2704 struct dwc2_qh *qh;
2705 unsigned long flags;
2706
2707 qh = ep->hcpriv;
2708 if (!qh)
2709 return;
2710
2711 spin_lock_irqsave(&hsotg->lock, flags);
2712 qh->tt_buffer_dirty = 0;
2713
2714 if (hsotg->flags.b.port_connect_status)
2715 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
2716
2717 spin_unlock_irqrestore(&hsotg->lock, flags);
2718 }
2719
2720 static struct hc_driver dwc2_hc_driver = {
2721 .description = "dwc2_hsotg",
2722 .product_desc = "DWC OTG Controller",
2723 .hcd_priv_size = sizeof(struct wrapper_priv_data),
2724
2725 .irq = _dwc2_hcd_irq,
2726 .flags = HCD_MEMORY | HCD_USB2,
2727
2728 .start = _dwc2_hcd_start,
2729 .stop = _dwc2_hcd_stop,
2730 .urb_enqueue = _dwc2_hcd_urb_enqueue,
2731 .urb_dequeue = _dwc2_hcd_urb_dequeue,
2732 .endpoint_disable = _dwc2_hcd_endpoint_disable,
2733 .endpoint_reset = _dwc2_hcd_endpoint_reset,
2734 .get_frame_number = _dwc2_hcd_get_frame_number,
2735
2736 .hub_status_data = _dwc2_hcd_hub_status_data,
2737 .hub_control = _dwc2_hcd_hub_control,
2738 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
2739
2740 .bus_suspend = _dwc2_hcd_suspend,
2741 .bus_resume = _dwc2_hcd_resume,
2742 };
2743
2744 /*
2745 * Frees secondary storage associated with the dwc2_hsotg structure contained
2746 * in the struct usb_hcd field
2747 */
2748 static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
2749 {
2750 u32 ahbcfg;
2751 u32 dctl;
2752 int i;
2753
2754 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
2755
2756 /* Free memory for QH/QTD lists */
2757 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
2758 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
2759 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
2760 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
2761 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
2762 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
2763
2764 /* Free memory for the host channels */
2765 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
2766 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
2767
2768 if (chan != NULL) {
2769 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
2770 i, chan);
2771 hsotg->hc_ptr_array[i] = NULL;
2772 kfree(chan);
2773 }
2774 }
2775
2776 if (hsotg->core_params->dma_enable > 0) {
2777 if (hsotg->status_buf) {
2778 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
2779 hsotg->status_buf,
2780 hsotg->status_buf_dma);
2781 hsotg->status_buf = NULL;
2782 }
2783 } else {
2784 kfree(hsotg->status_buf);
2785 hsotg->status_buf = NULL;
2786 }
2787
2788 ahbcfg = readl(hsotg->regs + GAHBCFG);
2789
2790 /* Disable all interrupts */
2791 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
2792 writel(ahbcfg, hsotg->regs + GAHBCFG);
2793 writel(0, hsotg->regs + GINTMSK);
2794
2795 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
2796 dctl = readl(hsotg->regs + DCTL);
2797 dctl |= DCTL_SFTDISCON;
2798 writel(dctl, hsotg->regs + DCTL);
2799 }
2800
2801 if (hsotg->wq_otg) {
2802 if (!cancel_work_sync(&hsotg->wf_otg))
2803 flush_workqueue(hsotg->wq_otg);
2804 destroy_workqueue(hsotg->wq_otg);
2805 }
2806
2807 kfree(hsotg->core_params);
2808 hsotg->core_params = NULL;
2809 del_timer(&hsotg->wkp_timer);
2810 }
2811
2812 static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
2813 {
2814 /* Turn off all host-specific interrupts */
2815 dwc2_disable_host_interrupts(hsotg);
2816
2817 dwc2_hcd_free(hsotg);
2818 }
2819
2820 /*
2821 * Sets all parameters to the given value.
2822 *
2823 * Assumes that the dwc2_core_params struct contains only integers.
2824 */
2825 void dwc2_set_all_params(struct dwc2_core_params *params, int value)
2826 {
2827 int *p = (int *)params;
2828 size_t size = sizeof(*params) / sizeof(*p);
2829 int i;
2830
2831 for (i = 0; i < size; i++)
2832 p[i] = value;
2833 }
2834 EXPORT_SYMBOL_GPL(dwc2_set_all_params);
2835
2836 /*
2837 * Initializes the HCD. This function allocates memory for and initializes the
2838 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
2839 * USB bus with the core and calls the hc_driver->start() function. It returns
2840 * a negative error on failure.
2841 */
2842 int dwc2_hcd_init(struct dwc2_hsotg *hsotg, int irq,
2843 const struct dwc2_core_params *params)
2844 {
2845 struct usb_hcd *hcd;
2846 struct dwc2_host_chan *channel;
2847 u32 hcfg;
2848 int i, num_channels;
2849 int retval;
2850
2851 if (usb_disabled())
2852 return -ENODEV;
2853
2854 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
2855
2856 /* Detect config values from hardware */
2857 retval = dwc2_get_hwparams(hsotg);
2858
2859 if (retval)
2860 return retval;
2861
2862 retval = -ENOMEM;
2863
2864 hcfg = readl(hsotg->regs + HCFG);
2865 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
2866
2867 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
2868 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
2869 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
2870 if (!hsotg->frame_num_array)
2871 goto error1;
2872 hsotg->last_frame_num_array = kzalloc(
2873 sizeof(*hsotg->last_frame_num_array) *
2874 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
2875 if (!hsotg->last_frame_num_array)
2876 goto error1;
2877 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
2878 #endif
2879
2880 hsotg->core_params = kzalloc(sizeof(*hsotg->core_params), GFP_KERNEL);
2881 if (!hsotg->core_params)
2882 goto error1;
2883
2884 dwc2_set_all_params(hsotg->core_params, -1);
2885
2886 /* Validate parameter values */
2887 dwc2_set_parameters(hsotg, params);
2888
2889 /* Check if the bus driver or platform code has setup a dma_mask */
2890 if (hsotg->core_params->dma_enable > 0 &&
2891 hsotg->dev->dma_mask == NULL) {
2892 dev_warn(hsotg->dev,
2893 "dma_mask not set, disabling DMA\n");
2894 hsotg->core_params->dma_enable = 0;
2895 hsotg->core_params->dma_desc_enable = 0;
2896 }
2897
2898 /* Set device flags indicating whether the HCD supports DMA */
2899 if (hsotg->core_params->dma_enable > 0) {
2900 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
2901 dev_warn(hsotg->dev, "can't set DMA mask\n");
2902 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
2903 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
2904 }
2905
2906 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
2907 if (!hcd)
2908 goto error1;
2909
2910 if (hsotg->core_params->dma_enable <= 0)
2911 hcd->self.uses_dma = 0;
2912
2913 hcd->has_tt = 1;
2914
2915 ((struct wrapper_priv_data *) &hcd->hcd_priv)->hsotg = hsotg;
2916 hsotg->priv = hcd;
2917
2918 /*
2919 * Disable the global interrupt until all the interrupt handlers are
2920 * installed
2921 */
2922 dwc2_disable_global_interrupts(hsotg);
2923
2924 /* Initialize the DWC_otg core, and select the Phy type */
2925 retval = dwc2_core_init(hsotg, true, irq);
2926 if (retval)
2927 goto error2;
2928
2929 /* Create new workqueue and init work */
2930 retval = -ENOMEM;
2931 hsotg->wq_otg = create_singlethread_workqueue("dwc2");
2932 if (!hsotg->wq_otg) {
2933 dev_err(hsotg->dev, "Failed to create workqueue\n");
2934 goto error2;
2935 }
2936 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
2937
2938 setup_timer(&hsotg->wkp_timer, dwc2_wakeup_detected,
2939 (unsigned long)hsotg);
2940
2941 /* Initialize the non-periodic schedule */
2942 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
2943 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
2944
2945 /* Initialize the periodic schedule */
2946 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
2947 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
2948 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
2949 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
2950
2951 /*
2952 * Create a host channel descriptor for each host channel implemented
2953 * in the controller. Initialize the channel descriptor array.
2954 */
2955 INIT_LIST_HEAD(&hsotg->free_hc_list);
2956 num_channels = hsotg->core_params->host_channels;
2957 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
2958
2959 for (i = 0; i < num_channels; i++) {
2960 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2961 if (channel == NULL)
2962 goto error3;
2963 channel->hc_num = i;
2964 hsotg->hc_ptr_array[i] = channel;
2965 }
2966
2967 if (hsotg->core_params->uframe_sched > 0)
2968 dwc2_hcd_init_usecs(hsotg);
2969
2970 /* Initialize hsotg start work */
2971 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
2972
2973 /* Initialize port reset work */
2974 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
2975
2976 /*
2977 * Allocate space for storing data on status transactions. Normally no
2978 * data is sent, but this space acts as a bit bucket. This must be
2979 * done after usb_add_hcd since that function allocates the DMA buffer
2980 * pool.
2981 */
2982 if (hsotg->core_params->dma_enable > 0)
2983 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
2984 DWC2_HCD_STATUS_BUF_SIZE,
2985 &hsotg->status_buf_dma, GFP_KERNEL);
2986 else
2987 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
2988 GFP_KERNEL);
2989
2990 if (!hsotg->status_buf)
2991 goto error3;
2992
2993 hsotg->otg_port = 1;
2994 hsotg->frame_list = NULL;
2995 hsotg->frame_list_dma = 0;
2996 hsotg->periodic_qh_count = 0;
2997
2998 /* Initiate lx_state to L3 disconnected state */
2999 hsotg->lx_state = DWC2_L3;
3000
3001 hcd->self.otg_port = hsotg->otg_port;
3002
3003 /* Don't support SG list at this point */
3004 hcd->self.sg_tablesize = 0;
3005
3006 /*
3007 * Finish generic HCD initialization and start the HCD. This function
3008 * allocates the DMA buffer pool, registers the USB bus, requests the
3009 * IRQ line, and calls hcd_start method.
3010 */
3011 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
3012 if (retval < 0)
3013 goto error3;
3014
3015 device_wakeup_enable(hcd->self.controller);
3016
3017 dwc2_hcd_dump_state(hsotg);
3018
3019 dwc2_enable_global_interrupts(hsotg);
3020
3021 return 0;
3022
3023 error3:
3024 dwc2_hcd_release(hsotg);
3025 error2:
3026 usb_put_hcd(hcd);
3027 error1:
3028 kfree(hsotg->core_params);
3029
3030 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
3031 kfree(hsotg->last_frame_num_array);
3032 kfree(hsotg->frame_num_array);
3033 #endif
3034
3035 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
3036 return retval;
3037 }
3038 EXPORT_SYMBOL_GPL(dwc2_hcd_init);
3039
3040 /*
3041 * Removes the HCD.
3042 * Frees memory and resources associated with the HCD and deregisters the bus.
3043 */
3044 void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
3045 {
3046 struct usb_hcd *hcd;
3047
3048 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
3049
3050 hcd = dwc2_hsotg_to_hcd(hsotg);
3051 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
3052
3053 if (!hcd) {
3054 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
3055 __func__);
3056 return;
3057 }
3058
3059 usb_remove_hcd(hcd);
3060 hsotg->priv = NULL;
3061 dwc2_hcd_release(hsotg);
3062 usb_put_hcd(hcd);
3063
3064 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
3065 kfree(hsotg->last_frame_num_array);
3066 kfree(hsotg->frame_num_array);
3067 #endif
3068 }
3069 EXPORT_SYMBOL_GPL(dwc2_hcd_remove);
This page took 0.094004 seconds and 5 git commands to generate.