blob: bc4bdbc1534ec52707c7eb38e8b2e2bc4e9610ac [file] [log] [blame]
Paul Zimmerman7359d482013-03-11 17:47:59 -07001/*
2 * hcd.c - DesignWare HS OTG Controller host-mode routines
3 *
4 * Copyright (C) 2004-2013 Synopsys, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
22 * later version.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * This file contains the core HCD code, and implements the Linux hc_driver
39 * API
40 */
41#include <linux/kernel.h>
42#include <linux/module.h>
43#include <linux/spinlock.h>
44#include <linux/interrupt.h>
45#include <linux/dma-mapping.h>
46#include <linux/delay.h>
47#include <linux/io.h>
48#include <linux/slab.h>
49#include <linux/usb.h>
50
51#include <linux/usb/hcd.h>
52#include <linux/usb/ch11.h>
53
54#include "core.h"
55#include "hcd.h"
56
57/**
58 * dwc2_dump_channel_info() - Prints the state of a host channel
59 *
60 * @hsotg: Programming view of DWC_otg controller
61 * @chan: Pointer to the channel to dump
62 *
63 * Must be called with interrupt disabled and spinlock held
64 *
65 * NOTE: This function will be removed once the peripheral controller code
66 * is integrated and the driver is stable
67 */
68static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
69 struct dwc2_host_chan *chan)
70{
71#ifdef VERBOSE_DEBUG
72 int num_channels = hsotg->core_params->host_channels;
73 struct dwc2_qh *qh;
74 u32 hcchar;
75 u32 hcsplt;
76 u32 hctsiz;
77 u32 hc_dma;
78 int i;
79
80 if (chan == NULL)
81 return;
82
Antti Seppälä95c8bc32015-08-20 21:41:07 +030083 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
84 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
85 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chan->hc_num));
86 hc_dma = dwc2_readl(hsotg->regs + HCDMA(chan->hc_num));
Paul Zimmerman7359d482013-03-11 17:47:59 -070087
88 dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
89 dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
90 hcchar, hcsplt);
91 dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
92 hctsiz, hc_dma);
93 dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
94 chan->dev_addr, chan->ep_num, chan->ep_is_in);
95 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
96 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
97 dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
98 dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
99 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
100 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
101 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
102 (unsigned long)chan->xfer_dma);
103 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
104 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
105 dev_dbg(hsotg->dev, " NP inactive sched:\n");
106 list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
107 qh_list_entry)
108 dev_dbg(hsotg->dev, " %p\n", qh);
109 dev_dbg(hsotg->dev, " NP active sched:\n");
110 list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
111 qh_list_entry)
112 dev_dbg(hsotg->dev, " %p\n", qh);
113 dev_dbg(hsotg->dev, " Channels:\n");
114 for (i = 0; i < num_channels; i++) {
115 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
116
117 dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
118 }
119#endif /* VERBOSE_DEBUG */
120}
121
122/*
123 * Processes all the URBs in a single list of QHs. Completes them with
124 * -ETIMEDOUT and frees the QTD.
125 *
126 * Must be called with interrupt disabled and spinlock held
127 */
128static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
129 struct list_head *qh_list)
130{
131 struct dwc2_qh *qh, *qh_tmp;
132 struct dwc2_qtd *qtd, *qtd_tmp;
133
134 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
135 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
136 qtd_list_entry) {
Gregory Herrero2e84da62015-09-22 15:16:53 +0200137 dwc2_host_complete(hsotg, qtd, -ECONNRESET);
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700138 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700139 }
140 }
141}
142
143static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
144 struct list_head *qh_list)
145{
146 struct dwc2_qtd *qtd, *qtd_tmp;
147 struct dwc2_qh *qh, *qh_tmp;
148 unsigned long flags;
149
150 if (!qh_list->next)
151 /* The list hasn't been initialized yet */
152 return;
153
154 spin_lock_irqsave(&hsotg->lock, flags);
155
156 /* Ensure there are no QTDs or URBs left */
157 dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
158
159 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
160 dwc2_hcd_qh_unlink(hsotg, qh);
161
162 /* Free each QTD in the QH's QTD list */
163 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
164 qtd_list_entry)
165 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
166
167 spin_unlock_irqrestore(&hsotg->lock, flags);
168 dwc2_hcd_qh_free(hsotg, qh);
169 spin_lock_irqsave(&hsotg->lock, flags);
170 }
171
172 spin_unlock_irqrestore(&hsotg->lock, flags);
173}
174
175/*
176 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
177 * and periodic schedules. The QTD associated with each URB is removed from
178 * the schedule and freed. This function may be called when a disconnect is
179 * detected or when the HCD is being stopped.
180 *
181 * Must be called with interrupt disabled and spinlock held
182 */
183static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
184{
185 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
186 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
187 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
188 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
189 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
190 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
191}
192
193/**
194 * dwc2_hcd_start() - Starts the HCD when switching to Host mode
195 *
196 * @hsotg: Pointer to struct dwc2_hsotg
197 */
198void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
199{
200 u32 hprt0;
201
202 if (hsotg->op_state == OTG_STATE_B_HOST) {
203 /*
204 * Reset the port. During a HNP mode switch the reset
205 * needs to occur within 1ms and have a duration of at
206 * least 50ms.
207 */
208 hprt0 = dwc2_read_hprt0(hsotg);
209 hprt0 |= HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300210 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700211 }
212
213 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
214 msecs_to_jiffies(50));
215}
216
217/* Must be called with interrupt disabled and spinlock held */
218static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
219{
220 int num_channels = hsotg->core_params->host_channels;
221 struct dwc2_host_chan *channel;
222 u32 hcchar;
223 int i;
224
225 if (hsotg->core_params->dma_enable <= 0) {
226 /* Flush out any channel requests in slave mode */
227 for (i = 0; i < num_channels; i++) {
228 channel = hsotg->hc_ptr_array[i];
229 if (!list_empty(&channel->hc_list_entry))
230 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300231 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700232 if (hcchar & HCCHAR_CHENA) {
233 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
234 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300235 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700236 }
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;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300244 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700245 if (hcchar & HCCHAR_CHENA) {
246 /* Halt the channel */
247 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300248 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700249 }
250
251 dwc2_hc_cleanup(hsotg, channel);
252 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
253 /*
254 * Added for Descriptor DMA to prevent channel double cleanup in
255 * release_channel_ddma(), which is called from ep_disable when
256 * device disconnects
257 */
258 channel->qh = NULL;
259 }
Vincent Palatin7252f1b2015-03-15 13:24:32 -0700260 /* All channels have been freed, mark them available */
261 if (hsotg->core_params->uframe_sched > 0) {
262 hsotg->available_host_channels =
263 hsotg->core_params->host_channels;
264 } else {
265 hsotg->non_periodic_channels = 0;
266 hsotg->periodic_channels = 0;
267 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700268}
269
270/**
Douglas Anderson6a659532015-11-19 13:23:14 -0800271 * dwc2_hcd_connect() - Handles connect of the HCD
Paul Zimmerman7359d482013-03-11 17:47:59 -0700272 *
273 * @hsotg: Pointer to struct dwc2_hsotg
274 *
275 * Must be called with interrupt disabled and spinlock held
276 */
Douglas Anderson6a659532015-11-19 13:23:14 -0800277void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
278{
279 if (hsotg->lx_state != DWC2_L0)
280 usb_hcd_resume_root_hub(hsotg->priv);
281
282 hsotg->flags.b.port_connect_status_change = 1;
283 hsotg->flags.b.port_connect_status = 1;
284}
285
286/**
287 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
288 *
289 * @hsotg: Pointer to struct dwc2_hsotg
290 * @force: If true, we won't try to reconnect even if we see device connected.
291 *
292 * Must be called with interrupt disabled and spinlock held
293 */
294void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700295{
296 u32 intr;
Douglas Anderson6a659532015-11-19 13:23:14 -0800297 u32 hprt0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700298
299 /* Set status flags for the hub driver */
300 hsotg->flags.b.port_connect_status_change = 1;
301 hsotg->flags.b.port_connect_status = 0;
302
303 /*
304 * Shutdown any transfers in process by clearing the Tx FIFO Empty
305 * interrupt mask and status bits and disabling subsequent host
306 * channel interrupts.
307 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300308 intr = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700309 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300310 dwc2_writel(intr, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700311 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300312 dwc2_writel(intr, hsotg->regs + GINTSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700313
314 /*
315 * Turn off the vbus power only if the core has transitioned to device
316 * mode. If still in host mode, need to keep power on to detect a
317 * reconnection.
318 */
319 if (dwc2_is_device_mode(hsotg)) {
320 if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
321 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300322 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700323 }
324
325 dwc2_disable_host_interrupts(hsotg);
326 }
327
328 /* Respond with an error status to all URBs in the schedule */
329 dwc2_kill_all_urbs(hsotg);
330
331 if (dwc2_is_host_mode(hsotg))
332 /* Clean up any host channels that were in use */
333 dwc2_hcd_cleanup_channels(hsotg);
334
335 dwc2_host_disconnect(hsotg);
Douglas Anderson6a659532015-11-19 13:23:14 -0800336
337 /*
338 * Add an extra check here to see if we're actually connected but
339 * we don't have a detection interrupt pending. This can happen if:
340 * 1. hardware sees connect
341 * 2. hardware sees disconnect
342 * 3. hardware sees connect
343 * 4. dwc2_port_intr() - clears connect interrupt
344 * 5. dwc2_handle_common_intr() - calls here
345 *
346 * Without the extra check here we will end calling disconnect
347 * and won't get any future interrupts to handle the connect.
348 */
349 if (!force) {
350 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
351 if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
352 dwc2_hcd_connect(hsotg);
353 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700354}
355
356/**
357 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
358 *
359 * @hsotg: Pointer to struct dwc2_hsotg
360 */
361static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
362{
Douglas Anderson1fb7f122015-10-22 13:05:03 -0700363 if (hsotg->bus_suspended) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700364 hsotg->flags.b.port_suspend_change = 1;
Gregory Herrerob46146d52015-01-30 09:09:26 +0100365 usb_hcd_resume_root_hub(hsotg->priv);
Gregory Herrerob46146d52015-01-30 09:09:26 +0100366 }
Douglas Anderson1fb7f122015-10-22 13:05:03 -0700367
368 if (hsotg->lx_state == DWC2_L1)
369 hsotg->flags.b.port_l1_change = 1;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700370}
371
372/**
373 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
374 *
375 * @hsotg: Pointer to struct dwc2_hsotg
376 *
377 * Must be called with interrupt disabled and spinlock held
378 */
379void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
380{
381 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
382
383 /*
384 * The root hub should be disconnected before this function is called.
385 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
386 * and the QH lists (via ..._hcd_endpoint_disable).
387 */
388
389 /* Turn off all host-specific interrupts */
390 dwc2_disable_host_interrupts(hsotg);
391
392 /* Turn off the vbus power */
393 dev_dbg(hsotg->dev, "PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300394 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700395}
396
Gregory Herrero33ad2612015-04-29 22:09:15 +0200397/* Caller must hold driver lock */
Paul Zimmerman7359d482013-03-11 17:47:59 -0700398static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +0200399 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +0200400 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700401{
Paul Zimmerman7359d482013-03-11 17:47:59 -0700402 u32 intr_mask;
403 int retval;
Nick Hudson9f8144c2013-12-06 14:01:44 -0800404 int dev_speed;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700405
406 if (!hsotg->flags.b.port_connect_status) {
407 /* No longer connected */
408 dev_err(hsotg->dev, "Not connected\n");
409 return -ENODEV;
410 }
411
Nick Hudson9f8144c2013-12-06 14:01:44 -0800412 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
413
414 /* Some configurations cannot support LS traffic on a FS root port */
415 if ((dev_speed == USB_SPEED_LOW) &&
416 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
417 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300418 u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Nick Hudson9f8144c2013-12-06 14:01:44 -0800419 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
420
421 if (prtspd == HPRT0_SPD_FULL_SPEED)
422 return -ENODEV;
423 }
424
Paul Zimmerman7359d482013-03-11 17:47:59 -0700425 if (!qtd)
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +0200426 return -EINVAL;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700427
428 dwc2_hcd_qtd_init(qtd, urb);
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +0200429 retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800430 if (retval) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700431 dev_err(hsotg->dev,
432 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
433 retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700434 return retval;
435 }
436
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300437 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800438 if (!(intr_mask & GINTSTS_SOF)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700439 enum dwc2_transaction_type tr_type;
440
441 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
442 !(qtd->urb->flags & URB_GIVEBACK_ASAP))
443 /*
444 * Do not schedule SG transactions until qtd has
445 * URB_GIVEBACK_ASAP set
446 */
447 return 0;
448
Paul Zimmerman7359d482013-03-11 17:47:59 -0700449 tr_type = dwc2_hcd_select_transactions(hsotg);
450 if (tr_type != DWC2_TRANSACTION_NONE)
451 dwc2_hcd_queue_transactions(hsotg, tr_type);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700452 }
453
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -0800454 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700455}
456
457/* Must be called with interrupt disabled and spinlock held */
458static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
459 struct dwc2_hcd_urb *urb)
460{
461 struct dwc2_qh *qh;
462 struct dwc2_qtd *urb_qtd;
463
464 urb_qtd = urb->qtd;
465 if (!urb_qtd) {
466 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
467 return -EINVAL;
468 }
469
470 qh = urb_qtd->qh;
471 if (!qh) {
472 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
473 return -EINVAL;
474 }
475
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700476 urb->priv = NULL;
477
Paul Zimmerman7359d482013-03-11 17:47:59 -0700478 if (urb_qtd->in_process && qh->channel) {
479 dwc2_dump_channel_info(hsotg, qh->channel);
480
481 /* The QTD is in process (it has been assigned to a channel) */
482 if (hsotg->flags.b.port_connect_status)
483 /*
484 * If still connected (i.e. in host mode), halt the
485 * channel so it can be used for other transfers. If
486 * no longer connected, the host registers can't be
487 * written to halt the channel since the core is in
488 * device mode.
489 */
490 dwc2_hc_halt(hsotg, qh->channel,
491 DWC2_HC_XFER_URB_DEQUEUE);
492 }
493
494 /*
495 * Free the QTD and clean up the associated QH. Leave the QH in the
496 * schedule if it has any remaining QTDs.
497 */
498 if (hsotg->core_params->dma_desc_enable <= 0) {
499 u8 in_process = urb_qtd->in_process;
500
501 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
502 if (in_process) {
503 dwc2_hcd_qh_deactivate(hsotg, qh, 0);
504 qh->channel = NULL;
505 } else if (list_empty(&qh->qtd_list)) {
506 dwc2_hcd_qh_unlink(hsotg, qh);
507 }
508 } else {
509 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
510 }
511
512 return 0;
513}
514
515/* Must NOT be called with interrupt disabled or spinlock held */
516static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
517 struct usb_host_endpoint *ep, int retry)
518{
519 struct dwc2_qtd *qtd, *qtd_tmp;
520 struct dwc2_qh *qh;
521 unsigned long flags;
522 int rc;
523
524 spin_lock_irqsave(&hsotg->lock, flags);
525
526 qh = ep->hcpriv;
527 if (!qh) {
528 rc = -EINVAL;
529 goto err;
530 }
531
532 while (!list_empty(&qh->qtd_list) && retry--) {
533 if (retry == 0) {
534 dev_err(hsotg->dev,
535 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
536 rc = -EBUSY;
537 goto err;
538 }
539
540 spin_unlock_irqrestore(&hsotg->lock, flags);
541 usleep_range(20000, 40000);
542 spin_lock_irqsave(&hsotg->lock, flags);
543 qh = ep->hcpriv;
544 if (!qh) {
545 rc = -EINVAL;
546 goto err;
547 }
548 }
549
550 dwc2_hcd_qh_unlink(hsotg, qh);
551
552 /* Free each QTD in the QH's QTD list */
553 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
554 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
555
556 ep->hcpriv = NULL;
557 spin_unlock_irqrestore(&hsotg->lock, flags);
558 dwc2_hcd_qh_free(hsotg, qh);
559
560 return 0;
561
562err:
563 ep->hcpriv = NULL;
564 spin_unlock_irqrestore(&hsotg->lock, flags);
565
566 return rc;
567}
568
569/* Must be called with interrupt disabled and spinlock held */
570static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
571 struct usb_host_endpoint *ep)
572{
573 struct dwc2_qh *qh = ep->hcpriv;
574
575 if (!qh)
576 return -EINVAL;
577
578 qh->data_toggle = DWC2_HC_PID_DATA0;
579
580 return 0;
581}
582
583/*
584 * Initializes dynamic portions of the DWC_otg HCD state
585 *
586 * Must be called with interrupt disabled and spinlock held
587 */
588static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
589{
590 struct dwc2_host_chan *chan, *chan_tmp;
591 int num_channels;
592 int i;
593
594 hsotg->flags.d32 = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700595 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700596
597 if (hsotg->core_params->uframe_sched > 0) {
598 hsotg->available_host_channels =
599 hsotg->core_params->host_channels;
600 } else {
601 hsotg->non_periodic_channels = 0;
602 hsotg->periodic_channels = 0;
603 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700604
605 /*
606 * Put all channels in the free channel list and clean up channel
607 * states
608 */
609 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
610 hc_list_entry)
611 list_del_init(&chan->hc_list_entry);
612
613 num_channels = hsotg->core_params->host_channels;
614 for (i = 0; i < num_channels; i++) {
615 chan = hsotg->hc_ptr_array[i];
616 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
617 dwc2_hc_cleanup(hsotg, chan);
618 }
619
620 /* Initialize the DWC core for host mode operation */
621 dwc2_core_host_init(hsotg);
622}
623
624static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
625 struct dwc2_host_chan *chan,
626 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
627{
628 int hub_addr, hub_port;
629
630 chan->do_split = 1;
631 chan->xact_pos = qtd->isoc_split_pos;
632 chan->complete_split = qtd->complete_split;
633 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
634 chan->hub_addr = (u8)hub_addr;
635 chan->hub_port = (u8)hub_port;
636}
637
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800638static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
639 struct dwc2_host_chan *chan,
640 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700641{
642 struct dwc2_hcd_urb *urb = qtd->urb;
643 struct dwc2_hcd_iso_packet_desc *frame_desc;
644
645 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
646 case USB_ENDPOINT_XFER_CONTROL:
647 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
648
649 switch (qtd->control_phase) {
650 case DWC2_CONTROL_SETUP:
651 dev_vdbg(hsotg->dev, " Control setup transaction\n");
652 chan->do_ping = 0;
653 chan->ep_is_in = 0;
654 chan->data_pid_start = DWC2_HC_PID_SETUP;
655 if (hsotg->core_params->dma_enable > 0)
656 chan->xfer_dma = urb->setup_dma;
657 else
658 chan->xfer_buf = urb->setup_packet;
659 chan->xfer_len = 8;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700660 break;
661
662 case DWC2_CONTROL_DATA:
663 dev_vdbg(hsotg->dev, " Control data transaction\n");
664 chan->data_pid_start = qtd->data_toggle;
665 break;
666
667 case DWC2_CONTROL_STATUS:
668 /*
669 * Direction is opposite of data direction or IN if no
670 * data
671 */
672 dev_vdbg(hsotg->dev, " Control status transaction\n");
673 if (urb->length == 0)
674 chan->ep_is_in = 1;
675 else
676 chan->ep_is_in =
677 dwc2_hcd_is_pipe_out(&urb->pipe_info);
678 if (chan->ep_is_in)
679 chan->do_ping = 0;
680 chan->data_pid_start = DWC2_HC_PID_DATA1;
681 chan->xfer_len = 0;
682 if (hsotg->core_params->dma_enable > 0)
683 chan->xfer_dma = hsotg->status_buf_dma;
684 else
685 chan->xfer_buf = hsotg->status_buf;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700686 break;
687 }
688 break;
689
690 case USB_ENDPOINT_XFER_BULK:
691 chan->ep_type = USB_ENDPOINT_XFER_BULK;
692 break;
693
694 case USB_ENDPOINT_XFER_INT:
695 chan->ep_type = USB_ENDPOINT_XFER_INT;
696 break;
697
698 case USB_ENDPOINT_XFER_ISOC:
699 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
700 if (hsotg->core_params->dma_desc_enable > 0)
701 break;
702
703 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
704 frame_desc->status = 0;
705
706 if (hsotg->core_params->dma_enable > 0) {
707 chan->xfer_dma = urb->dma;
708 chan->xfer_dma += frame_desc->offset +
709 qtd->isoc_split_offset;
710 } else {
711 chan->xfer_buf = urb->buf;
712 chan->xfer_buf += frame_desc->offset +
713 qtd->isoc_split_offset;
714 }
715
716 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
717
Paul Zimmerman7359d482013-03-11 17:47:59 -0700718 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
719 if (chan->xfer_len <= 188)
720 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
721 else
722 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
723 }
724 break;
725 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700726}
727
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800728#define DWC2_USB_DMA_ALIGN 4
729
730struct dma_aligned_buffer {
731 void *kmalloc_ptr;
732 void *old_xfer_buffer;
733 u8 data[0];
734};
735
736static void dwc2_free_dma_aligned_buffer(struct urb *urb)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700737{
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800738 struct dma_aligned_buffer *temp;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700739
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800740 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
741 return;
Paul Zimmerman5dce9552014-09-16 13:47:27 -0700742
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800743 temp = container_of(urb->transfer_buffer,
744 struct dma_aligned_buffer, data);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700745
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800746 if (usb_urb_dir_in(urb))
747 memcpy(temp->old_xfer_buffer, temp->data,
748 urb->transfer_buffer_length);
749 urb->transfer_buffer = temp->old_xfer_buffer;
750 kfree(temp->kmalloc_ptr);
Paul Zimmerman5dce9552014-09-16 13:47:27 -0700751
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800752 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
753}
Paul Zimmerman7359d482013-03-11 17:47:59 -0700754
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800755static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
756{
757 struct dma_aligned_buffer *temp, *kmalloc_ptr;
758 size_t kmalloc_size;
Gregory Herrerodb62b9a2015-04-29 22:09:16 +0200759
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800760 if (urb->num_sgs || urb->sg ||
761 urb->transfer_buffer_length == 0 ||
762 !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
763 return 0;
764
765 /* Allocate a buffer with enough padding for alignment */
766 kmalloc_size = urb->transfer_buffer_length +
767 sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1;
768
769 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
770 if (!kmalloc_ptr)
771 return -ENOMEM;
772
773 /* Position our struct dma_aligned_buffer such that data is aligned */
774 temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1;
775 temp->kmalloc_ptr = kmalloc_ptr;
776 temp->old_xfer_buffer = urb->transfer_buffer;
777 if (usb_urb_dir_out(urb))
778 memcpy(temp->data, urb->transfer_buffer,
779 urb->transfer_buffer_length);
780 urb->transfer_buffer = temp->data;
781
782 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
783
Paul Zimmerman7359d482013-03-11 17:47:59 -0700784 return 0;
785}
786
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800787static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
788 gfp_t mem_flags)
789{
790 int ret;
791
792 /* We assume setup_dma is always aligned; warn if not */
793 WARN_ON_ONCE(urb->setup_dma &&
794 (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
795
796 ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
797 if (ret)
798 return ret;
799
800 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
801 if (ret)
802 dwc2_free_dma_aligned_buffer(urb);
803
804 return ret;
805}
806
807static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
808{
809 usb_hcd_unmap_urb_for_dma(hcd, urb);
810 dwc2_free_dma_aligned_buffer(urb);
811}
812
Paul Zimmerman7359d482013-03-11 17:47:59 -0700813/**
814 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
815 * channel and initializes the host channel to perform the transactions. The
816 * host channel is removed from the free list.
817 *
818 * @hsotg: The HCD state structure
819 * @qh: Transactions from the first QTD for this QH are selected and assigned
820 * to a free host channel
821 */
Dom Cobley20f2eb92013-09-23 14:23:34 -0700822static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700823{
824 struct dwc2_host_chan *chan;
825 struct dwc2_hcd_urb *urb;
826 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700827
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200828 if (dbg_qh(qh))
829 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700830
831 if (list_empty(&qh->qtd_list)) {
832 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -0700833 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700834 }
835
836 if (list_empty(&hsotg->free_hc_list)) {
837 dev_dbg(hsotg->dev, "No free channel to assign\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -0700838 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700839 }
840
841 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
842 hc_list_entry);
843
Dom Cobley20f2eb92013-09-23 14:23:34 -0700844 /* Remove host channel from free list */
Paul Zimmerman7359d482013-03-11 17:47:59 -0700845 list_del_init(&chan->hc_list_entry);
846
847 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
848 urb = qtd->urb;
849 qh->channel = chan;
850 qtd->in_process = 1;
851
852 /*
853 * Use usb_pipedevice to determine device address. This address is
854 * 0 before the SET_ADDRESS command and the correct address afterward.
855 */
856 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
857 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
858 chan->speed = qh->dev_speed;
859 chan->max_packet = dwc2_max_packet(qh->maxp);
860
861 chan->xfer_started = 0;
862 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
863 chan->error_state = (qtd->error_count > 0);
864 chan->halt_on_queue = 0;
865 chan->halt_pending = 0;
866 chan->requests = 0;
867
868 /*
869 * The following values may be modified in the transfer type section
870 * below. The xfer_len value may be reduced when the transfer is
871 * started to accommodate the max widths of the XferSize and PktCnt
872 * fields in the HCTSIZn register.
873 */
874
875 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
876 if (chan->ep_is_in)
877 chan->do_ping = 0;
878 else
879 chan->do_ping = qh->ping_state;
880
881 chan->data_pid_start = qh->data_toggle;
882 chan->multi_count = 1;
883
Rashika Kheriabb6c3422013-10-26 23:11:22 +0530884 if (urb->actual_length > urb->length &&
885 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
Paul Zimmerman84181082013-09-23 14:23:33 -0700886 urb->actual_length = urb->length;
887
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800888 if (hsotg->core_params->dma_enable > 0)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700889 chan->xfer_dma = urb->dma + urb->actual_length;
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800890 else
Paul Zimmerman7359d482013-03-11 17:47:59 -0700891 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700892
893 chan->xfer_len = urb->length - urb->actual_length;
894 chan->xfer_count = 0;
895
896 /* Set the split attributes if required */
897 if (qh->do_split)
898 dwc2_hc_init_split(hsotg, chan, qtd, urb);
899 else
900 chan->do_split = 0;
901
902 /* Set the transfer attributes */
Douglas Anderson3bc04e22016-01-28 18:19:53 -0800903 dwc2_hc_init_xfer(hsotg, chan, qtd);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700904
905 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
906 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
907 /*
908 * This value may be modified when the transfer is started
909 * to reflect the actual transfer length
910 */
911 chan->multi_count = dwc2_hb_mult(qh->maxp);
912
Gregory Herrero95105a92015-11-20 11:49:29 +0100913 if (hsotg->core_params->dma_desc_enable > 0) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700914 chan->desc_list_addr = qh->desc_list_dma;
Gregory Herrero95105a92015-11-20 11:49:29 +0100915 chan->desc_list_sz = qh->desc_list_sz;
916 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700917
918 dwc2_hc_init(hsotg, chan);
919 chan->qh = qh;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700920
921 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700922}
923
924/**
925 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
926 * schedule and assigns them to available host channels. Called from the HCD
927 * interrupt handler functions.
928 *
929 * @hsotg: The HCD state structure
930 *
931 * Return: The types of new transactions that were assigned to host channels
932 */
933enum dwc2_transaction_type dwc2_hcd_select_transactions(
934 struct dwc2_hsotg *hsotg)
935{
936 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
937 struct list_head *qh_ptr;
938 struct dwc2_qh *qh;
939 int num_channels;
940
941#ifdef DWC2_DEBUG_SOF
942 dev_vdbg(hsotg->dev, " Select Transactions\n");
943#endif
944
945 /* Process entries in the periodic ready list */
946 qh_ptr = hsotg->periodic_sched_ready.next;
947 while (qh_ptr != &hsotg->periodic_sched_ready) {
948 if (list_empty(&hsotg->free_hc_list))
949 break;
Dom Cobley20f2eb92013-09-23 14:23:34 -0700950 if (hsotg->core_params->uframe_sched > 0) {
951 if (hsotg->available_host_channels <= 1)
952 break;
953 hsotg->available_host_channels--;
954 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700955 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
Dom Cobley20f2eb92013-09-23 14:23:34 -0700956 if (dwc2_assign_and_init_hc(hsotg, qh))
957 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700958
959 /*
960 * Move the QH from the periodic ready schedule to the
961 * periodic assigned schedule
962 */
963 qh_ptr = qh_ptr->next;
964 list_move(&qh->qh_list_entry, &hsotg->periodic_sched_assigned);
965 ret_val = DWC2_TRANSACTION_PERIODIC;
966 }
967
968 /*
969 * Process entries in the inactive portion of the non-periodic
970 * schedule. Some free host channels may not be used if they are
971 * reserved for periodic transfers.
972 */
973 num_channels = hsotg->core_params->host_channels;
974 qh_ptr = hsotg->non_periodic_sched_inactive.next;
975 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
Dom Cobley20f2eb92013-09-23 14:23:34 -0700976 if (hsotg->core_params->uframe_sched <= 0 &&
977 hsotg->non_periodic_channels >= num_channels -
Paul Zimmerman7359d482013-03-11 17:47:59 -0700978 hsotg->periodic_channels)
979 break;
980 if (list_empty(&hsotg->free_hc_list))
981 break;
982 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
Dom Cobley20f2eb92013-09-23 14:23:34 -0700983 if (hsotg->core_params->uframe_sched > 0) {
984 if (hsotg->available_host_channels < 1)
985 break;
986 hsotg->available_host_channels--;
987 }
988
989 if (dwc2_assign_and_init_hc(hsotg, qh))
990 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700991
992 /*
993 * Move the QH from the non-periodic inactive schedule to the
994 * non-periodic active schedule
995 */
996 qh_ptr = qh_ptr->next;
997 list_move(&qh->qh_list_entry,
998 &hsotg->non_periodic_sched_active);
999
1000 if (ret_val == DWC2_TRANSACTION_NONE)
1001 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
1002 else
1003 ret_val = DWC2_TRANSACTION_ALL;
1004
Dom Cobley20f2eb92013-09-23 14:23:34 -07001005 if (hsotg->core_params->uframe_sched <= 0)
1006 hsotg->non_periodic_channels++;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001007 }
1008
1009 return ret_val;
1010}
1011
1012/**
1013 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
1014 * a host channel associated with either a periodic or non-periodic transfer
1015 *
1016 * @hsotg: The HCD state structure
1017 * @chan: Host channel descriptor associated with either a periodic or
1018 * non-periodic transfer
1019 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
1020 * for periodic transfers or the non-periodic Tx FIFO
1021 * for non-periodic transfers
1022 *
1023 * Return: 1 if a request is queued and more requests may be needed to
1024 * complete the transfer, 0 if no more requests are required for this
1025 * transfer, -1 if there is insufficient space in the Tx FIFO
1026 *
1027 * This function assumes that there is space available in the appropriate
1028 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
1029 * it checks whether space is available in the appropriate Tx FIFO.
1030 *
1031 * Must be called with interrupt disabled and spinlock held
1032 */
1033static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
1034 struct dwc2_host_chan *chan,
1035 u16 fifo_dwords_avail)
1036{
1037 int retval = 0;
1038
1039 if (hsotg->core_params->dma_enable > 0) {
1040 if (hsotg->core_params->dma_desc_enable > 0) {
1041 if (!chan->xfer_started ||
1042 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1043 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
1044 chan->qh->ping_state = 0;
1045 }
1046 } else if (!chan->xfer_started) {
1047 dwc2_hc_start_transfer(hsotg, chan);
1048 chan->qh->ping_state = 0;
1049 }
1050 } else if (chan->halt_pending) {
1051 /* Don't queue a request if the channel has been halted */
1052 } else if (chan->halt_on_queue) {
1053 dwc2_hc_halt(hsotg, chan, chan->halt_status);
1054 } else if (chan->do_ping) {
1055 if (!chan->xfer_started)
1056 dwc2_hc_start_transfer(hsotg, chan);
1057 } else if (!chan->ep_is_in ||
1058 chan->data_pid_start == DWC2_HC_PID_SETUP) {
1059 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
1060 if (!chan->xfer_started) {
1061 dwc2_hc_start_transfer(hsotg, chan);
1062 retval = 1;
1063 } else {
1064 retval = dwc2_hc_continue_transfer(hsotg, chan);
1065 }
1066 } else {
1067 retval = -1;
1068 }
1069 } else {
1070 if (!chan->xfer_started) {
1071 dwc2_hc_start_transfer(hsotg, chan);
1072 retval = 1;
1073 } else {
1074 retval = dwc2_hc_continue_transfer(hsotg, chan);
1075 }
1076 }
1077
1078 return retval;
1079}
1080
1081/*
1082 * Processes periodic channels for the next frame and queues transactions for
1083 * these channels to the DWC_otg controller. After queueing transactions, the
1084 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
1085 * to queue as Periodic Tx FIFO or request queue space becomes available.
1086 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
1087 *
1088 * Must be called with interrupt disabled and spinlock held
1089 */
1090static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
1091{
1092 struct list_head *qh_ptr;
1093 struct dwc2_qh *qh;
1094 u32 tx_status;
1095 u32 fspcavail;
1096 u32 gintmsk;
1097 int status;
1098 int no_queue_space = 0;
1099 int no_fifo_space = 0;
1100 u32 qspcavail;
1101
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001102 if (dbg_perio())
1103 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07001104
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001105 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001106 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1107 TXSTS_QSPCAVAIL_SHIFT;
1108 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1109 TXSTS_FSPCAVAIL_SHIFT;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001110
1111 if (dbg_perio()) {
1112 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
1113 qspcavail);
1114 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
1115 fspcavail);
1116 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001117
1118 qh_ptr = hsotg->periodic_sched_assigned.next;
1119 while (qh_ptr != &hsotg->periodic_sched_assigned) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001120 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmanacdb9042013-08-30 18:45:16 +02001121 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1122 TXSTS_QSPCAVAIL_SHIFT;
1123 if (qspcavail == 0) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001124 no_queue_space = 1;
1125 break;
1126 }
1127
1128 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
1129 if (!qh->channel) {
1130 qh_ptr = qh_ptr->next;
1131 continue;
1132 }
1133
1134 /* Make sure EP's TT buffer is clean before queueing qtds */
1135 if (qh->tt_buffer_dirty) {
1136 qh_ptr = qh_ptr->next;
1137 continue;
1138 }
1139
1140 /*
1141 * Set a flag if we're queuing high-bandwidth in slave mode.
1142 * The flag prevents any halts to get into the request queue in
1143 * the middle of multiple high-bandwidth packets getting queued.
1144 */
1145 if (hsotg->core_params->dma_enable <= 0 &&
1146 qh->channel->multi_count > 1)
1147 hsotg->queuing_high_bandwidth = 1;
1148
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001149 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1150 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001151 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
1152 if (status < 0) {
1153 no_fifo_space = 1;
1154 break;
1155 }
1156
1157 /*
1158 * In Slave mode, stay on the current transfer until there is
1159 * nothing more to do or the high-bandwidth request count is
1160 * reached. In DMA mode, only need to queue one request. The
1161 * controller automatically handles multiple packets for
1162 * high-bandwidth transfers.
1163 */
1164 if (hsotg->core_params->dma_enable > 0 || status == 0 ||
1165 qh->channel->requests == qh->channel->multi_count) {
1166 qh_ptr = qh_ptr->next;
1167 /*
1168 * Move the QH from the periodic assigned schedule to
1169 * the periodic queued schedule
1170 */
1171 list_move(&qh->qh_list_entry,
1172 &hsotg->periodic_sched_queued);
1173
1174 /* done queuing high bandwidth */
1175 hsotg->queuing_high_bandwidth = 0;
1176 }
1177 }
1178
1179 if (hsotg->core_params->dma_enable <= 0) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001180 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001181 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1182 TXSTS_QSPCAVAIL_SHIFT;
1183 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1184 TXSTS_FSPCAVAIL_SHIFT;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001185 if (dbg_perio()) {
1186 dev_vdbg(hsotg->dev,
1187 " P Tx Req Queue Space Avail (after queue): %d\n",
1188 qspcavail);
1189 dev_vdbg(hsotg->dev,
1190 " P Tx FIFO Space Avail (after queue): %d\n",
1191 fspcavail);
1192 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001193
1194 if (!list_empty(&hsotg->periodic_sched_assigned) ||
1195 no_queue_space || no_fifo_space) {
1196 /*
1197 * May need to queue more transactions as the request
1198 * queue or Tx FIFO empties. Enable the periodic Tx
1199 * FIFO empty interrupt. (Always use the half-empty
1200 * level to ensure that new requests are loaded as
1201 * soon as possible.)
1202 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001203 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001204 gintmsk |= GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001205 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001206 } else {
1207 /*
1208 * Disable the Tx FIFO empty interrupt since there are
1209 * no more transactions that need to be queued right
1210 * now. This function is called from interrupt
1211 * handlers to queue more transactions as transfer
1212 * states change.
1213 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001214 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001215 gintmsk &= ~GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001216 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001217 }
1218 }
1219}
1220
1221/*
1222 * Processes active non-periodic channels and queues transactions for these
1223 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
1224 * FIFO Empty interrupt is enabled if there are more transactions to queue as
1225 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
1226 * FIFO Empty interrupt is disabled.
1227 *
1228 * Must be called with interrupt disabled and spinlock held
1229 */
1230static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
1231{
1232 struct list_head *orig_qh_ptr;
1233 struct dwc2_qh *qh;
1234 u32 tx_status;
1235 u32 qspcavail;
1236 u32 fspcavail;
1237 u32 gintmsk;
1238 int status;
1239 int no_queue_space = 0;
1240 int no_fifo_space = 0;
1241 int more_to_do = 0;
1242
1243 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
1244
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001245 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001246 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1247 TXSTS_QSPCAVAIL_SHIFT;
1248 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1249 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001250 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
1251 qspcavail);
1252 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
1253 fspcavail);
1254
1255 /*
1256 * Keep track of the starting point. Skip over the start-of-list
1257 * entry.
1258 */
1259 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
1260 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
1261 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
1262
1263 /*
1264 * Process once through the active list or until no more space is
1265 * available in the request queue or the Tx FIFO
1266 */
1267 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001268 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001269 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1270 TXSTS_QSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001271 if (hsotg->core_params->dma_enable <= 0 && qspcavail == 0) {
1272 no_queue_space = 1;
1273 break;
1274 }
1275
1276 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
1277 qh_list_entry);
1278 if (!qh->channel)
1279 goto next;
1280
1281 /* Make sure EP's TT buffer is clean before queueing qtds */
1282 if (qh->tt_buffer_dirty)
1283 goto next;
1284
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001285 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1286 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001287 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
1288
1289 if (status > 0) {
1290 more_to_do = 1;
1291 } else if (status < 0) {
1292 no_fifo_space = 1;
1293 break;
1294 }
1295next:
1296 /* Advance to next QH, skipping start-of-list entry */
1297 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
1298 if (hsotg->non_periodic_qh_ptr ==
1299 &hsotg->non_periodic_sched_active)
1300 hsotg->non_periodic_qh_ptr =
1301 hsotg->non_periodic_qh_ptr->next;
1302 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
1303
1304 if (hsotg->core_params->dma_enable <= 0) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001305 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001306 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
1307 TXSTS_QSPCAVAIL_SHIFT;
1308 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
1309 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001310 dev_vdbg(hsotg->dev,
1311 " NP Tx Req Queue Space Avail (after queue): %d\n",
1312 qspcavail);
1313 dev_vdbg(hsotg->dev,
1314 " NP Tx FIFO Space Avail (after queue): %d\n",
1315 fspcavail);
1316
1317 if (more_to_do || no_queue_space || no_fifo_space) {
1318 /*
1319 * May need to queue more transactions as the request
1320 * queue or Tx FIFO empties. Enable the non-periodic
1321 * Tx FIFO empty interrupt. (Always use the half-empty
1322 * level to ensure that new requests are loaded as
1323 * soon as possible.)
1324 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001325 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001326 gintmsk |= GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001327 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001328 } else {
1329 /*
1330 * Disable the Tx FIFO empty interrupt since there are
1331 * no more transactions that need to be queued right
1332 * now. This function is called from interrupt
1333 * handlers to queue more transactions as transfer
1334 * states change.
1335 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001336 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001337 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001338 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001339 }
1340 }
1341}
1342
1343/**
1344 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
1345 * and queues transactions for these channels to the DWC_otg controller. Called
1346 * from the HCD interrupt handler functions.
1347 *
1348 * @hsotg: The HCD state structure
1349 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
1350 * or both)
1351 *
1352 * Must be called with interrupt disabled and spinlock held
1353 */
1354void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
1355 enum dwc2_transaction_type tr_type)
1356{
1357#ifdef DWC2_DEBUG_SOF
1358 dev_vdbg(hsotg->dev, "Queue Transactions\n");
1359#endif
1360 /* Process host channels associated with periodic transfers */
1361 if ((tr_type == DWC2_TRANSACTION_PERIODIC ||
1362 tr_type == DWC2_TRANSACTION_ALL) &&
1363 !list_empty(&hsotg->periodic_sched_assigned))
1364 dwc2_process_periodic_channels(hsotg);
1365
1366 /* Process host channels associated with non-periodic transfers */
1367 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
1368 tr_type == DWC2_TRANSACTION_ALL) {
1369 if (!list_empty(&hsotg->non_periodic_sched_active)) {
1370 dwc2_process_non_periodic_channels(hsotg);
1371 } else {
1372 /*
1373 * Ensure NP Tx FIFO empty interrupt is disabled when
1374 * there are no non-periodic transfers to process
1375 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001376 u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001377
1378 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001379 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001380 }
1381 }
1382}
1383
1384static void dwc2_conn_id_status_change(struct work_struct *work)
1385{
1386 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
1387 wf_otg);
1388 u32 count = 0;
1389 u32 gotgctl;
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02001390 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001391
1392 dev_dbg(hsotg->dev, "%s()\n", __func__);
1393
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001394 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001395 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
1396 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
1397 !!(gotgctl & GOTGCTL_CONID_B));
1398
1399 /* B-Device connector (Device Mode) */
1400 if (gotgctl & GOTGCTL_CONID_B) {
1401 /* Wait for switch to device mode */
1402 dev_dbg(hsotg->dev, "connId B\n");
1403 while (!dwc2_is_device_mode(hsotg)) {
1404 dev_info(hsotg->dev,
1405 "Waiting for Peripheral Mode, Mode=%s\n",
1406 dwc2_is_host_mode(hsotg) ? "Host" :
1407 "Peripheral");
1408 usleep_range(20000, 40000);
1409 if (++count > 250)
1410 break;
1411 }
1412 if (count > 250)
1413 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07001414 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07001415 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Douglas Anderson0fe239b2015-12-17 11:14:40 -08001416 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001417 dwc2_enable_global_interrupts(hsotg);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02001418 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001419 dwc2_hsotg_core_init_disconnected(hsotg, false);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02001420 spin_unlock_irqrestore(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001421 dwc2_hsotg_core_connect(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001422 } else {
1423 /* A-Device connector (Host Mode) */
1424 dev_dbg(hsotg->dev, "connId A\n");
1425 while (!dwc2_is_host_mode(hsotg)) {
1426 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
1427 dwc2_is_host_mode(hsotg) ?
1428 "Host" : "Peripheral");
1429 usleep_range(20000, 40000);
1430 if (++count > 250)
1431 break;
1432 }
1433 if (count > 250)
1434 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07001435 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07001436 hsotg->op_state = OTG_STATE_A_HOST;
1437
1438 /* Initialize the Core for Host mode */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08001439 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001440 dwc2_enable_global_interrupts(hsotg);
1441 dwc2_hcd_start(hsotg);
1442 }
1443}
1444
1445static void dwc2_wakeup_detected(unsigned long data)
1446{
1447 struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)data;
1448 u32 hprt0;
1449
1450 dev_dbg(hsotg->dev, "%s()\n", __func__);
1451
1452 /*
1453 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
1454 * so that OPT tests pass with all PHYs.)
1455 */
1456 hprt0 = dwc2_read_hprt0(hsotg);
1457 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
1458 hprt0 &= ~HPRT0_RES;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001459 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001460 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001461 dwc2_readl(hsotg->regs + HPRT0));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001462
1463 dwc2_hcd_rem_wakeup(hsotg);
Douglas Anderson1fb7f122015-10-22 13:05:03 -07001464 hsotg->bus_suspended = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001465
1466 /* Change to L0 state */
1467 hsotg->lx_state = DWC2_L0;
1468}
1469
1470static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
1471{
1472 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
1473
1474 return hcd->self.b_hnp_enable;
1475}
1476
1477/* Must NOT be called with interrupt disabled or spinlock held */
1478static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
1479{
1480 unsigned long flags;
1481 u32 hprt0;
1482 u32 pcgctl;
1483 u32 gotgctl;
1484
1485 dev_dbg(hsotg->dev, "%s()\n", __func__);
1486
1487 spin_lock_irqsave(&hsotg->lock, flags);
1488
1489 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001490 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001491 gotgctl |= GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001492 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001493 hsotg->op_state = OTG_STATE_A_SUSPEND;
1494 }
1495
1496 hprt0 = dwc2_read_hprt0(hsotg);
1497 hprt0 |= HPRT0_SUSP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001498 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001499
Gregory Herrero734643d2015-09-22 15:16:39 +02001500 hsotg->bus_suspended = 1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001501
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02001502 /*
1503 * If hibernation is supported, Phy clock will be suspended
1504 * after registers are backuped.
1505 */
1506 if (!hsotg->core_params->hibernation) {
1507 /* Suspend the Phy Clock */
1508 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
1509 pcgctl |= PCGCTL_STOPPCLK;
1510 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
1511 udelay(10);
1512 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001513
1514 /* For HNP the bus must be suspended for at least 200ms */
1515 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001516 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001517 pcgctl &= ~PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001518 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001519
1520 spin_unlock_irqrestore(&hsotg->lock, flags);
1521
1522 usleep_range(200000, 250000);
1523 } else {
1524 spin_unlock_irqrestore(&hsotg->lock, flags);
1525 }
1526}
1527
Gregory Herrero30db1032015-09-22 15:16:38 +02001528/* Must NOT be called with interrupt disabled or spinlock held */
1529static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
1530{
1531 unsigned long flags;
1532 u32 hprt0;
1533 u32 pcgctl;
1534
Douglas Anderson4d273c22015-10-14 15:58:27 -07001535 spin_lock_irqsave(&hsotg->lock, flags);
1536
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02001537 /*
1538 * If hibernation is supported, Phy clock is already resumed
1539 * after registers restore.
1540 */
1541 if (!hsotg->core_params->hibernation) {
1542 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
1543 pcgctl &= ~PCGCTL_STOPPCLK;
1544 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Douglas Anderson4d273c22015-10-14 15:58:27 -07001545 spin_unlock_irqrestore(&hsotg->lock, flags);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02001546 usleep_range(20000, 40000);
Douglas Anderson4d273c22015-10-14 15:58:27 -07001547 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02001548 }
Gregory Herrero30db1032015-09-22 15:16:38 +02001549
Gregory Herrero30db1032015-09-22 15:16:38 +02001550 hprt0 = dwc2_read_hprt0(hsotg);
1551 hprt0 |= HPRT0_RES;
1552 hprt0 &= ~HPRT0_SUSP;
1553 dwc2_writel(hprt0, hsotg->regs + HPRT0);
1554 spin_unlock_irqrestore(&hsotg->lock, flags);
1555
1556 msleep(USB_RESUME_TIMEOUT);
1557
1558 spin_lock_irqsave(&hsotg->lock, flags);
1559 hprt0 = dwc2_read_hprt0(hsotg);
1560 hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
1561 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Gregory Herrero734643d2015-09-22 15:16:39 +02001562 hsotg->bus_suspended = 0;
Gregory Herrero30db1032015-09-22 15:16:38 +02001563 spin_unlock_irqrestore(&hsotg->lock, flags);
1564}
1565
Paul Zimmerman7359d482013-03-11 17:47:59 -07001566/* Handles hub class-specific requests */
1567static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
1568 u16 wvalue, u16 windex, char *buf, u16 wlength)
1569{
1570 struct usb_hub_descriptor *hub_desc;
1571 int retval = 0;
1572 u32 hprt0;
1573 u32 port_status;
1574 u32 speed;
1575 u32 pcgctl;
1576
1577 switch (typereq) {
1578 case ClearHubFeature:
1579 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
1580
1581 switch (wvalue) {
1582 case C_HUB_LOCAL_POWER:
1583 case C_HUB_OVER_CURRENT:
1584 /* Nothing required here */
1585 break;
1586
1587 default:
1588 retval = -EINVAL;
1589 dev_err(hsotg->dev,
1590 "ClearHubFeature request %1xh unknown\n",
1591 wvalue);
1592 }
1593 break;
1594
1595 case ClearPortFeature:
1596 if (wvalue != USB_PORT_FEAT_L1)
1597 if (!windex || windex > 1)
1598 goto error;
1599 switch (wvalue) {
1600 case USB_PORT_FEAT_ENABLE:
1601 dev_dbg(hsotg->dev,
1602 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
1603 hprt0 = dwc2_read_hprt0(hsotg);
1604 hprt0 |= HPRT0_ENA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001605 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001606 break;
1607
1608 case USB_PORT_FEAT_SUSPEND:
1609 dev_dbg(hsotg->dev,
1610 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
Paul Zimmermanb0bb9bb2015-01-15 19:21:46 +00001611
Gregory Herrerobea78552015-09-22 15:16:44 +02001612 if (hsotg->bus_suspended)
1613 dwc2_port_resume(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001614 break;
1615
1616 case USB_PORT_FEAT_POWER:
1617 dev_dbg(hsotg->dev,
1618 "ClearPortFeature USB_PORT_FEAT_POWER\n");
1619 hprt0 = dwc2_read_hprt0(hsotg);
1620 hprt0 &= ~HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001621 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001622 break;
1623
1624 case USB_PORT_FEAT_INDICATOR:
1625 dev_dbg(hsotg->dev,
1626 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
1627 /* Port indicator not supported */
1628 break;
1629
1630 case USB_PORT_FEAT_C_CONNECTION:
1631 /*
1632 * Clears driver's internal Connect Status Change flag
1633 */
1634 dev_dbg(hsotg->dev,
1635 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
1636 hsotg->flags.b.port_connect_status_change = 0;
1637 break;
1638
1639 case USB_PORT_FEAT_C_RESET:
1640 /* Clears driver's internal Port Reset Change flag */
1641 dev_dbg(hsotg->dev,
1642 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
1643 hsotg->flags.b.port_reset_change = 0;
1644 break;
1645
1646 case USB_PORT_FEAT_C_ENABLE:
1647 /*
1648 * Clears the driver's internal Port Enable/Disable
1649 * Change flag
1650 */
1651 dev_dbg(hsotg->dev,
1652 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
1653 hsotg->flags.b.port_enable_change = 0;
1654 break;
1655
1656 case USB_PORT_FEAT_C_SUSPEND:
1657 /*
1658 * Clears the driver's internal Port Suspend Change
1659 * flag, which is set when resume signaling on the host
1660 * port is complete
1661 */
1662 dev_dbg(hsotg->dev,
1663 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
1664 hsotg->flags.b.port_suspend_change = 0;
1665 break;
1666
1667 case USB_PORT_FEAT_C_PORT_L1:
1668 dev_dbg(hsotg->dev,
1669 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
1670 hsotg->flags.b.port_l1_change = 0;
1671 break;
1672
1673 case USB_PORT_FEAT_C_OVER_CURRENT:
1674 dev_dbg(hsotg->dev,
1675 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
1676 hsotg->flags.b.port_over_current_change = 0;
1677 break;
1678
1679 default:
1680 retval = -EINVAL;
1681 dev_err(hsotg->dev,
1682 "ClearPortFeature request %1xh unknown or unsupported\n",
1683 wvalue);
1684 }
1685 break;
1686
1687 case GetHubDescriptor:
1688 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
1689 hub_desc = (struct usb_hub_descriptor *)buf;
1690 hub_desc->bDescLength = 9;
Sergei Shtylyova5dd0392015-03-29 01:36:28 +03001691 hub_desc->bDescriptorType = USB_DT_HUB;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001692 hub_desc->bNbrPorts = 1;
Sergei Shtylyov3d040de2015-01-19 01:54:15 +03001693 hub_desc->wHubCharacteristics =
1694 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
1695 HUB_CHAR_INDV_PORT_OCPM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001696 hub_desc->bPwrOn2PwrGood = 1;
1697 hub_desc->bHubContrCurrent = 0;
1698 hub_desc->u.hs.DeviceRemovable[0] = 0;
1699 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
1700 break;
1701
1702 case GetHubStatus:
1703 dev_dbg(hsotg->dev, "GetHubStatus\n");
1704 memset(buf, 0, 4);
1705 break;
1706
1707 case GetPortStatus:
Paul Zimmermanb8313412013-05-24 16:32:12 -07001708 dev_vdbg(hsotg->dev,
1709 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
1710 hsotg->flags.d32);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001711 if (!windex || windex > 1)
1712 goto error;
1713
1714 port_status = 0;
1715 if (hsotg->flags.b.port_connect_status_change)
1716 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
1717 if (hsotg->flags.b.port_enable_change)
1718 port_status |= USB_PORT_STAT_C_ENABLE << 16;
1719 if (hsotg->flags.b.port_suspend_change)
1720 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
1721 if (hsotg->flags.b.port_l1_change)
1722 port_status |= USB_PORT_STAT_C_L1 << 16;
1723 if (hsotg->flags.b.port_reset_change)
1724 port_status |= USB_PORT_STAT_C_RESET << 16;
1725 if (hsotg->flags.b.port_over_current_change) {
1726 dev_warn(hsotg->dev, "Overcurrent change detected\n");
1727 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
1728 }
1729
1730 if (!hsotg->flags.b.port_connect_status) {
1731 /*
1732 * The port is disconnected, which means the core is
1733 * either in device mode or it soon will be. Just
1734 * return 0's for the remainder of the port status
1735 * since the port register can't be read if the core
1736 * is in device mode.
1737 */
1738 *(__le32 *)buf = cpu_to_le32(port_status);
1739 break;
1740 }
1741
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001742 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmermanb8313412013-05-24 16:32:12 -07001743 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001744
1745 if (hprt0 & HPRT0_CONNSTS)
1746 port_status |= USB_PORT_STAT_CONNECTION;
1747 if (hprt0 & HPRT0_ENA)
1748 port_status |= USB_PORT_STAT_ENABLE;
1749 if (hprt0 & HPRT0_SUSP)
1750 port_status |= USB_PORT_STAT_SUSPEND;
1751 if (hprt0 & HPRT0_OVRCURRACT)
1752 port_status |= USB_PORT_STAT_OVERCURRENT;
1753 if (hprt0 & HPRT0_RST)
1754 port_status |= USB_PORT_STAT_RESET;
1755 if (hprt0 & HPRT0_PWR)
1756 port_status |= USB_PORT_STAT_POWER;
1757
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02001758 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001759 if (speed == HPRT0_SPD_HIGH_SPEED)
1760 port_status |= USB_PORT_STAT_HIGH_SPEED;
1761 else if (speed == HPRT0_SPD_LOW_SPEED)
1762 port_status |= USB_PORT_STAT_LOW_SPEED;
1763
1764 if (hprt0 & HPRT0_TSTCTL_MASK)
1765 port_status |= USB_PORT_STAT_TEST;
1766 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
1767
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01001768 if (hsotg->core_params->dma_desc_fs_enable) {
1769 /*
1770 * Enable descriptor DMA only if a full speed
1771 * device is connected.
1772 */
1773 if (hsotg->new_connection &&
1774 ((port_status &
1775 (USB_PORT_STAT_CONNECTION |
1776 USB_PORT_STAT_HIGH_SPEED |
1777 USB_PORT_STAT_LOW_SPEED)) ==
1778 USB_PORT_STAT_CONNECTION)) {
1779 u32 hcfg;
1780
1781 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
1782 hsotg->core_params->dma_desc_enable = 1;
1783 hcfg = dwc2_readl(hsotg->regs + HCFG);
1784 hcfg |= HCFG_DESCDMA;
1785 dwc2_writel(hcfg, hsotg->regs + HCFG);
1786 hsotg->new_connection = false;
1787 }
1788 }
1789
Paul Zimmermanb8313412013-05-24 16:32:12 -07001790 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001791 *(__le32 *)buf = cpu_to_le32(port_status);
1792 break;
1793
1794 case SetHubFeature:
1795 dev_dbg(hsotg->dev, "SetHubFeature\n");
1796 /* No HUB features supported */
1797 break;
1798
1799 case SetPortFeature:
1800 dev_dbg(hsotg->dev, "SetPortFeature\n");
1801 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
1802 goto error;
1803
1804 if (!hsotg->flags.b.port_connect_status) {
1805 /*
1806 * The port is disconnected, which means the core is
1807 * either in device mode or it soon will be. Just
1808 * return without doing anything since the port
1809 * register can't be written if the core is in device
1810 * mode.
1811 */
1812 break;
1813 }
1814
1815 switch (wvalue) {
1816 case USB_PORT_FEAT_SUSPEND:
1817 dev_dbg(hsotg->dev,
1818 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
1819 if (windex != hsotg->otg_port)
1820 goto error;
1821 dwc2_port_suspend(hsotg, windex);
1822 break;
1823
1824 case USB_PORT_FEAT_POWER:
1825 dev_dbg(hsotg->dev,
1826 "SetPortFeature - USB_PORT_FEAT_POWER\n");
1827 hprt0 = dwc2_read_hprt0(hsotg);
1828 hprt0 |= HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001829 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001830 break;
1831
1832 case USB_PORT_FEAT_RESET:
1833 hprt0 = dwc2_read_hprt0(hsotg);
1834 dev_dbg(hsotg->dev,
1835 "SetPortFeature - USB_PORT_FEAT_RESET\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001836 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001837 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001838 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001839 /* ??? Original driver does this */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001840 dwc2_writel(0, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001841
1842 hprt0 = dwc2_read_hprt0(hsotg);
1843 /* Clear suspend bit if resetting from suspend state */
1844 hprt0 &= ~HPRT0_SUSP;
1845
1846 /*
1847 * When B-Host the Port reset bit is set in the Start
1848 * HCD Callback function, so that the reset is started
1849 * within 1ms of the HNP success interrupt
1850 */
1851 if (!dwc2_hcd_is_b_host(hsotg)) {
1852 hprt0 |= HPRT0_PWR | HPRT0_RST;
1853 dev_dbg(hsotg->dev,
1854 "In host mode, hprt0=%08x\n", hprt0);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001855 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001856 }
1857
1858 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
1859 usleep_range(50000, 70000);
1860 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001861 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001862 hsotg->lx_state = DWC2_L0; /* Now back to On state */
1863 break;
1864
1865 case USB_PORT_FEAT_INDICATOR:
1866 dev_dbg(hsotg->dev,
1867 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
1868 /* Not supported */
1869 break;
1870
Jingwu Lin96d480e2015-04-29 22:09:17 +02001871 case USB_PORT_FEAT_TEST:
1872 hprt0 = dwc2_read_hprt0(hsotg);
1873 dev_dbg(hsotg->dev,
1874 "SetPortFeature - USB_PORT_FEAT_TEST\n");
1875 hprt0 &= ~HPRT0_TSTCTL_MASK;
1876 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001877 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Jingwu Lin96d480e2015-04-29 22:09:17 +02001878 break;
1879
Paul Zimmerman7359d482013-03-11 17:47:59 -07001880 default:
1881 retval = -EINVAL;
1882 dev_err(hsotg->dev,
1883 "SetPortFeature %1xh unknown or unsupported\n",
1884 wvalue);
1885 break;
1886 }
1887 break;
1888
1889 default:
1890error:
1891 retval = -EINVAL;
1892 dev_dbg(hsotg->dev,
1893 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
1894 typereq, windex, wvalue);
1895 break;
1896 }
1897
1898 return retval;
1899}
1900
1901static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
1902{
1903 int retval;
1904
Paul Zimmerman7359d482013-03-11 17:47:59 -07001905 if (port != 1)
1906 return -EINVAL;
1907
1908 retval = (hsotg->flags.b.port_connect_status_change ||
1909 hsotg->flags.b.port_reset_change ||
1910 hsotg->flags.b.port_enable_change ||
1911 hsotg->flags.b.port_suspend_change ||
1912 hsotg->flags.b.port_over_current_change);
1913
1914 if (retval) {
1915 dev_dbg(hsotg->dev,
1916 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
1917 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
1918 hsotg->flags.b.port_connect_status_change);
1919 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
1920 hsotg->flags.b.port_reset_change);
1921 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
1922 hsotg->flags.b.port_enable_change);
1923 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
1924 hsotg->flags.b.port_suspend_change);
1925 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
1926 hsotg->flags.b.port_over_current_change);
1927 }
1928
1929 return retval;
1930}
1931
1932int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
1933{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001934 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001935
1936#ifdef DWC2_DEBUG_SOF
1937 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001938 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001939#endif
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001940 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001941}
1942
1943int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
1944{
Aldo Iljazi6bf2e2a2013-11-30 19:33:57 +02001945 return hsotg->op_state == OTG_STATE_B_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001946}
1947
1948static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
1949 int iso_desc_count,
1950 gfp_t mem_flags)
1951{
1952 struct dwc2_hcd_urb *urb;
1953 u32 size = sizeof(*urb) + iso_desc_count *
1954 sizeof(struct dwc2_hcd_iso_packet_desc);
1955
1956 urb = kzalloc(size, mem_flags);
1957 if (urb)
1958 urb->packet_count = iso_desc_count;
1959 return urb;
1960}
1961
1962static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
1963 struct dwc2_hcd_urb *urb, u8 dev_addr,
1964 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
1965{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001966 if (dbg_perio() ||
1967 ep_type == USB_ENDPOINT_XFER_BULK ||
1968 ep_type == USB_ENDPOINT_XFER_CONTROL)
1969 dev_vdbg(hsotg->dev,
1970 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
1971 dev_addr, ep_num, ep_dir, ep_type, mps);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001972 urb->pipe_info.dev_addr = dev_addr;
1973 urb->pipe_info.ep_num = ep_num;
1974 urb->pipe_info.pipe_type = ep_type;
1975 urb->pipe_info.pipe_dir = ep_dir;
1976 urb->pipe_info.mps = mps;
1977}
1978
1979/*
1980 * NOTE: This function will be removed once the peripheral controller code
1981 * is integrated and the driver is stable
1982 */
1983void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
1984{
1985#ifdef DEBUG
1986 struct dwc2_host_chan *chan;
1987 struct dwc2_hcd_urb *urb;
1988 struct dwc2_qtd *qtd;
1989 int num_channels;
1990 u32 np_tx_status;
1991 u32 p_tx_status;
1992 int i;
1993
1994 num_channels = hsotg->core_params->host_channels;
1995 dev_dbg(hsotg->dev, "\n");
1996 dev_dbg(hsotg->dev,
1997 "************************************************************\n");
1998 dev_dbg(hsotg->dev, "HCD State:\n");
1999 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
2000
2001 for (i = 0; i < num_channels; i++) {
2002 chan = hsotg->hc_ptr_array[i];
2003 dev_dbg(hsotg->dev, " Channel %d:\n", i);
2004 dev_dbg(hsotg->dev,
2005 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
2006 chan->dev_addr, chan->ep_num, chan->ep_is_in);
2007 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
2008 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
2009 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
2010 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
2011 chan->data_pid_start);
2012 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
2013 dev_dbg(hsotg->dev, " xfer_started: %d\n",
2014 chan->xfer_started);
2015 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
2016 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
2017 (unsigned long)chan->xfer_dma);
2018 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
2019 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
2020 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
2021 chan->halt_on_queue);
2022 dev_dbg(hsotg->dev, " halt_pending: %d\n",
2023 chan->halt_pending);
2024 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
2025 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
2026 dev_dbg(hsotg->dev, " complete_split: %d\n",
2027 chan->complete_split);
2028 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
2029 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
2030 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
2031 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
2032 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
2033
2034 if (chan->xfer_started) {
2035 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
2036
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002037 hfnum = dwc2_readl(hsotg->regs + HFNUM);
2038 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2039 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
2040 hcint = dwc2_readl(hsotg->regs + HCINT(i));
2041 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07002042 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
2043 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
2044 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
2045 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
2046 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
2047 }
2048
2049 if (!(chan->xfer_started && chan->qh))
2050 continue;
2051
2052 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
2053 if (!qtd->in_process)
2054 break;
2055 urb = qtd->urb;
2056 dev_dbg(hsotg->dev, " URB Info:\n");
2057 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
2058 qtd, urb);
2059 if (urb) {
2060 dev_dbg(hsotg->dev,
2061 " Dev: %d, EP: %d %s\n",
2062 dwc2_hcd_get_dev_addr(&urb->pipe_info),
2063 dwc2_hcd_get_ep_num(&urb->pipe_info),
2064 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
2065 "IN" : "OUT");
2066 dev_dbg(hsotg->dev,
2067 " Max packet size: %d\n",
2068 dwc2_hcd_get_mps(&urb->pipe_info));
2069 dev_dbg(hsotg->dev,
2070 " transfer_buffer: %p\n",
2071 urb->buf);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07002072 dev_dbg(hsotg->dev,
2073 " transfer_dma: %08lx\n",
2074 (unsigned long)urb->dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002075 dev_dbg(hsotg->dev,
2076 " transfer_buffer_length: %d\n",
2077 urb->length);
2078 dev_dbg(hsotg->dev, " actual_length: %d\n",
2079 urb->actual_length);
2080 }
2081 }
2082 }
2083
2084 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
2085 hsotg->non_periodic_channels);
2086 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
2087 hsotg->periodic_channels);
2088 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002089 np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002090 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02002091 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002092 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02002093 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002094 p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002095 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02002096 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002097 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02002098 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002099 dwc2_hcd_dump_frrem(hsotg);
2100 dwc2_dump_global_registers(hsotg);
2101 dwc2_dump_host_registers(hsotg);
2102 dev_dbg(hsotg->dev,
2103 "************************************************************\n");
2104 dev_dbg(hsotg->dev, "\n");
2105#endif
2106}
2107
2108/*
2109 * NOTE: This function will be removed once the peripheral controller code
2110 * is integrated and the driver is stable
2111 */
2112void dwc2_hcd_dump_frrem(struct dwc2_hsotg *hsotg)
2113{
2114#ifdef DWC2_DUMP_FRREM
2115 dev_dbg(hsotg->dev, "Frame remaining at SOF:\n");
2116 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2117 hsotg->frrem_samples, hsotg->frrem_accum,
2118 hsotg->frrem_samples > 0 ?
2119 hsotg->frrem_accum / hsotg->frrem_samples : 0);
2120 dev_dbg(hsotg->dev, "\n");
2121 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 7):\n");
2122 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2123 hsotg->hfnum_7_samples,
2124 hsotg->hfnum_7_frrem_accum,
2125 hsotg->hfnum_7_samples > 0 ?
2126 hsotg->hfnum_7_frrem_accum / hsotg->hfnum_7_samples : 0);
2127 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 0):\n");
2128 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2129 hsotg->hfnum_0_samples,
2130 hsotg->hfnum_0_frrem_accum,
2131 hsotg->hfnum_0_samples > 0 ?
2132 hsotg->hfnum_0_frrem_accum / hsotg->hfnum_0_samples : 0);
2133 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 1-6):\n");
2134 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2135 hsotg->hfnum_other_samples,
2136 hsotg->hfnum_other_frrem_accum,
2137 hsotg->hfnum_other_samples > 0 ?
2138 hsotg->hfnum_other_frrem_accum / hsotg->hfnum_other_samples :
2139 0);
2140 dev_dbg(hsotg->dev, "\n");
2141 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 7):\n");
2142 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2143 hsotg->hfnum_7_samples_a, hsotg->hfnum_7_frrem_accum_a,
2144 hsotg->hfnum_7_samples_a > 0 ?
2145 hsotg->hfnum_7_frrem_accum_a / hsotg->hfnum_7_samples_a : 0);
2146 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 0):\n");
2147 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2148 hsotg->hfnum_0_samples_a, hsotg->hfnum_0_frrem_accum_a,
2149 hsotg->hfnum_0_samples_a > 0 ?
2150 hsotg->hfnum_0_frrem_accum_a / hsotg->hfnum_0_samples_a : 0);
2151 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 1-6):\n");
2152 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2153 hsotg->hfnum_other_samples_a, hsotg->hfnum_other_frrem_accum_a,
2154 hsotg->hfnum_other_samples_a > 0 ?
2155 hsotg->hfnum_other_frrem_accum_a / hsotg->hfnum_other_samples_a
2156 : 0);
2157 dev_dbg(hsotg->dev, "\n");
2158 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 7):\n");
2159 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2160 hsotg->hfnum_7_samples_b, hsotg->hfnum_7_frrem_accum_b,
2161 hsotg->hfnum_7_samples_b > 0 ?
2162 hsotg->hfnum_7_frrem_accum_b / hsotg->hfnum_7_samples_b : 0);
2163 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 0):\n");
2164 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2165 hsotg->hfnum_0_samples_b, hsotg->hfnum_0_frrem_accum_b,
2166 (hsotg->hfnum_0_samples_b > 0) ?
2167 hsotg->hfnum_0_frrem_accum_b / hsotg->hfnum_0_samples_b : 0);
2168 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 1-6):\n");
2169 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
2170 hsotg->hfnum_other_samples_b, hsotg->hfnum_other_frrem_accum_b,
2171 (hsotg->hfnum_other_samples_b > 0) ?
2172 hsotg->hfnum_other_frrem_accum_b / hsotg->hfnum_other_samples_b
2173 : 0);
2174#endif
2175}
2176
2177struct wrapper_priv_data {
2178 struct dwc2_hsotg *hsotg;
2179};
2180
2181/* Gets the dwc2_hsotg from a usb_hcd */
2182static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
2183{
2184 struct wrapper_priv_data *p;
2185
2186 p = (struct wrapper_priv_data *) &hcd->hcd_priv;
2187 return p->hsotg;
2188}
2189
2190static int _dwc2_hcd_start(struct usb_hcd *hcd);
2191
2192void dwc2_host_start(struct dwc2_hsotg *hsotg)
2193{
2194 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
2195
2196 hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
2197 _dwc2_hcd_start(hcd);
2198}
2199
2200void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
2201{
2202 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
2203
2204 hcd->self.is_b_host = 0;
2205}
2206
2207void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context, int *hub_addr,
2208 int *hub_port)
2209{
2210 struct urb *urb = context;
2211
2212 if (urb->dev->tt)
2213 *hub_addr = urb->dev->tt->hub->devnum;
2214 else
2215 *hub_addr = 0;
2216 *hub_port = urb->dev->ttport;
2217}
2218
2219int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
2220{
2221 struct urb *urb = context;
2222
2223 return urb->dev->speed;
2224}
2225
2226static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
2227 struct urb *urb)
2228{
2229 struct usb_bus *bus = hcd_to_bus(hcd);
2230
2231 if (urb->interval)
2232 bus->bandwidth_allocated += bw / urb->interval;
2233 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2234 bus->bandwidth_isoc_reqs++;
2235 else
2236 bus->bandwidth_int_reqs++;
2237}
2238
2239static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
2240 struct urb *urb)
2241{
2242 struct usb_bus *bus = hcd_to_bus(hcd);
2243
2244 if (urb->interval)
2245 bus->bandwidth_allocated -= bw / urb->interval;
2246 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
2247 bus->bandwidth_isoc_reqs--;
2248 else
2249 bus->bandwidth_int_reqs--;
2250}
2251
2252/*
2253 * Sets the final status of an URB and returns it to the upper layer. Any
2254 * required cleanup of the URB is performed.
2255 *
2256 * Must be called with interrupt disabled and spinlock held
2257 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002258void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
2259 int status)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002260{
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002261 struct urb *urb;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002262 int i;
2263
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002264 if (!qtd) {
2265 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
2266 return;
2267 }
2268
2269 if (!qtd->urb) {
2270 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
2271 return;
2272 }
2273
2274 urb = qtd->urb->priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002275 if (!urb) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002276 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002277 return;
2278 }
2279
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002280 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002281
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002282 if (dbg_urb(urb))
2283 dev_vdbg(hsotg->dev,
2284 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
2285 __func__, urb, usb_pipedevice(urb->pipe),
2286 usb_pipeendpoint(urb->pipe),
2287 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
2288 urb->actual_length);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002289
Paul Zimmerman7359d482013-03-11 17:47:59 -07002290
2291 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002292 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002293 for (i = 0; i < urb->number_of_packets; ++i) {
2294 urb->iso_frame_desc[i].actual_length =
2295 dwc2_hcd_urb_get_iso_desc_actual_length(
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002296 qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002297 urb->iso_frame_desc[i].status =
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002298 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002299 }
2300 }
2301
Gregory Herrerofe9b1772015-09-22 15:16:51 +02002302 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
2303 for (i = 0; i < urb->number_of_packets; i++)
2304 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
2305 i, urb->iso_frame_desc[i].status);
2306 }
2307
Paul Zimmerman7359d482013-03-11 17:47:59 -07002308 urb->status = status;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002309 if (!status) {
2310 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
2311 urb->actual_length < urb->transfer_buffer_length)
2312 urb->status = -EREMOTEIO;
2313 }
2314
2315 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
2316 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2317 struct usb_host_endpoint *ep = urb->ep;
2318
2319 if (ep)
2320 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
2321 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
2322 urb);
2323 }
2324
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002325 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002326 urb->hcpriv = NULL;
2327 kfree(qtd->urb);
2328 qtd->urb = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002329
2330 spin_unlock(&hsotg->lock);
2331 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
2332 spin_lock(&hsotg->lock);
2333}
2334
2335/*
2336 * Work queue function for starting the HCD when A-Cable is connected
2337 */
2338static void dwc2_hcd_start_func(struct work_struct *work)
2339{
2340 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
2341 start_work.work);
2342
2343 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
2344 dwc2_host_start(hsotg);
2345}
2346
2347/*
2348 * Reset work queue function
2349 */
2350static void dwc2_hcd_reset_func(struct work_struct *work)
2351{
2352 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
2353 reset_work.work);
Douglas Anderson4a065c72015-11-20 09:06:27 -08002354 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002355 u32 hprt0;
2356
2357 dev_dbg(hsotg->dev, "USB RESET function called\n");
Douglas Anderson4a065c72015-11-20 09:06:27 -08002358
2359 spin_lock_irqsave(&hsotg->lock, flags);
2360
Paul Zimmerman7359d482013-03-11 17:47:59 -07002361 hprt0 = dwc2_read_hprt0(hsotg);
2362 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002363 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002364 hsotg->flags.b.port_reset_change = 1;
Douglas Anderson4a065c72015-11-20 09:06:27 -08002365
2366 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002367}
2368
2369/*
2370 * =========================================================================
2371 * Linux HC Driver Functions
2372 * =========================================================================
2373 */
2374
2375/*
2376 * Initializes the DWC_otg controller and its root hub and prepares it for host
2377 * mode operation. Activates the root port. Returns 0 on success and a negative
2378 * error code on failure.
2379 */
2380static int _dwc2_hcd_start(struct usb_hcd *hcd)
2381{
2382 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2383 struct usb_bus *bus = hcd_to_bus(hcd);
2384 unsigned long flags;
2385
2386 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
2387
2388 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero31927b62015-09-22 15:16:41 +02002389 hsotg->lx_state = DWC2_L0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002390 hcd->state = HC_STATE_RUNNING;
Gregory Herrero31927b62015-09-22 15:16:41 +02002391 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002392
2393 if (dwc2_is_device_mode(hsotg)) {
2394 spin_unlock_irqrestore(&hsotg->lock, flags);
2395 return 0; /* why 0 ?? */
2396 }
2397
2398 dwc2_hcd_reinit(hsotg);
2399
2400 /* Initialize and connect root hub if one is not already attached */
2401 if (bus->root_hub) {
2402 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
2403 /* Inform the HUB driver to resume */
2404 usb_hcd_resume_root_hub(hcd);
2405 }
2406
2407 spin_unlock_irqrestore(&hsotg->lock, flags);
2408 return 0;
2409}
2410
2411/*
2412 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
2413 * stopped.
2414 */
2415static void _dwc2_hcd_stop(struct usb_hcd *hcd)
2416{
2417 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2418 unsigned long flags;
2419
Gregory Herrero5bbf6ce2015-09-22 15:16:48 +02002420 /* Turn off all host-specific interrupts */
2421 dwc2_disable_host_interrupts(hsotg);
2422
Gregory Herrero091473a2015-09-22 15:16:46 +02002423 /* Wait for interrupt processing to finish */
2424 synchronize_irq(hcd->irq);
2425
Paul Zimmerman7359d482013-03-11 17:47:59 -07002426 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero091473a2015-09-22 15:16:46 +02002427 /* Ensure hcd is disconnected */
Douglas Anderson6a659532015-11-19 13:23:14 -08002428 dwc2_hcd_disconnect(hsotg, true);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002429 dwc2_hcd_stop(hsotg);
Gregory Herrero31927b62015-09-22 15:16:41 +02002430 hsotg->lx_state = DWC2_L3;
2431 hcd->state = HC_STATE_HALT;
2432 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002433 spin_unlock_irqrestore(&hsotg->lock, flags);
2434
2435 usleep_range(1000, 3000);
2436}
2437
Gregory Herrero99a65792015-04-29 22:09:13 +02002438static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
2439{
2440 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02002441 unsigned long flags;
2442 int ret = 0;
2443 u32 hprt0;
Gregory Herrero99a65792015-04-29 22:09:13 +02002444
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02002445 spin_lock_irqsave(&hsotg->lock, flags);
2446
2447 if (hsotg->lx_state != DWC2_L0)
2448 goto unlock;
2449
2450 if (!HCD_HW_ACCESSIBLE(hcd))
2451 goto unlock;
2452
2453 if (!hsotg->core_params->hibernation)
2454 goto skip_power_saving;
2455
2456 /*
2457 * Drive USB suspend and disable port Power
2458 * if usb bus is not suspended.
2459 */
2460 if (!hsotg->bus_suspended) {
2461 hprt0 = dwc2_read_hprt0(hsotg);
2462 hprt0 |= HPRT0_SUSP;
2463 hprt0 &= ~HPRT0_PWR;
2464 dwc2_writel(hprt0, hsotg->regs + HPRT0);
2465 }
2466
2467 /* Enter hibernation */
2468 ret = dwc2_enter_hibernation(hsotg);
2469 if (ret) {
2470 if (ret != -ENOTSUPP)
2471 dev_err(hsotg->dev,
2472 "enter hibernation failed\n");
2473 goto skip_power_saving;
2474 }
2475
2476 /* Ask phy to be suspended */
2477 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
2478 spin_unlock_irqrestore(&hsotg->lock, flags);
2479 usb_phy_set_suspend(hsotg->uphy, true);
2480 spin_lock_irqsave(&hsotg->lock, flags);
2481 }
2482
2483 /* After entering hibernation, hardware is no more accessible */
2484 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2485
2486skip_power_saving:
Gregory Herrero99a65792015-04-29 22:09:13 +02002487 hsotg->lx_state = DWC2_L2;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02002488unlock:
2489 spin_unlock_irqrestore(&hsotg->lock, flags);
2490
2491 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02002492}
2493
2494static int _dwc2_hcd_resume(struct usb_hcd *hcd)
2495{
2496 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02002497 unsigned long flags;
2498 int ret = 0;
2499
2500 spin_lock_irqsave(&hsotg->lock, flags);
2501
2502 if (hsotg->lx_state != DWC2_L2)
2503 goto unlock;
2504
2505 if (!hsotg->core_params->hibernation) {
2506 hsotg->lx_state = DWC2_L0;
2507 goto unlock;
2508 }
2509
2510 /*
2511 * Set HW accessible bit before powering on the controller
2512 * since an interrupt may rise.
2513 */
2514 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2515
2516 /*
2517 * Enable power if not already done.
2518 * This must not be spinlocked since duration
2519 * of this call is unknown.
2520 */
2521 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
2522 spin_unlock_irqrestore(&hsotg->lock, flags);
2523 usb_phy_set_suspend(hsotg->uphy, false);
2524 spin_lock_irqsave(&hsotg->lock, flags);
2525 }
2526
2527 /* Exit hibernation */
2528 ret = dwc2_exit_hibernation(hsotg, true);
2529 if (ret && (ret != -ENOTSUPP))
2530 dev_err(hsotg->dev, "exit hibernation failed\n");
Gregory Herrero99a65792015-04-29 22:09:13 +02002531
2532 hsotg->lx_state = DWC2_L0;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02002533
2534 spin_unlock_irqrestore(&hsotg->lock, flags);
2535
2536 if (hsotg->bus_suspended) {
2537 spin_lock_irqsave(&hsotg->lock, flags);
2538 hsotg->flags.b.port_suspend_change = 1;
2539 spin_unlock_irqrestore(&hsotg->lock, flags);
2540 dwc2_port_resume(hsotg);
2541 } else {
Gregory Herrero5634e012015-09-22 15:16:50 +02002542 /* Wait for controller to correctly update D+/D- level */
2543 usleep_range(3000, 5000);
2544
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02002545 /*
2546 * Clear Port Enable and Port Status changes.
2547 * Enable Port Power.
2548 */
2549 dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
2550 HPRT0_ENACHG, hsotg->regs + HPRT0);
2551 /* Wait for controller to detect Port Connect */
Gregory Herrero5634e012015-09-22 15:16:50 +02002552 usleep_range(5000, 7000);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02002553 }
2554
2555 return ret;
2556unlock:
2557 spin_unlock_irqrestore(&hsotg->lock, flags);
2558
2559 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02002560}
2561
Paul Zimmerman7359d482013-03-11 17:47:59 -07002562/* Returns the current frame number */
2563static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
2564{
2565 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2566
2567 return dwc2_hcd_get_frame_number(hsotg);
2568}
2569
2570static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
2571 char *fn_name)
2572{
2573#ifdef VERBOSE_DEBUG
2574 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2575 char *pipetype;
2576 char *speed;
2577
2578 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
2579 dev_vdbg(hsotg->dev, " Device address: %d\n",
2580 usb_pipedevice(urb->pipe));
2581 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
2582 usb_pipeendpoint(urb->pipe),
2583 usb_pipein(urb->pipe) ? "IN" : "OUT");
2584
2585 switch (usb_pipetype(urb->pipe)) {
2586 case PIPE_CONTROL:
2587 pipetype = "CONTROL";
2588 break;
2589 case PIPE_BULK:
2590 pipetype = "BULK";
2591 break;
2592 case PIPE_INTERRUPT:
2593 pipetype = "INTERRUPT";
2594 break;
2595 case PIPE_ISOCHRONOUS:
2596 pipetype = "ISOCHRONOUS";
2597 break;
2598 default:
2599 pipetype = "UNKNOWN";
2600 break;
2601 }
2602
2603 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
2604 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
2605 "IN" : "OUT");
2606
2607 switch (urb->dev->speed) {
2608 case USB_SPEED_HIGH:
2609 speed = "HIGH";
2610 break;
2611 case USB_SPEED_FULL:
2612 speed = "FULL";
2613 break;
2614 case USB_SPEED_LOW:
2615 speed = "LOW";
2616 break;
2617 default:
2618 speed = "UNKNOWN";
2619 break;
2620 }
2621
2622 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
2623 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
2624 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
2625 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
2626 urb->transfer_buffer_length);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07002627 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
2628 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
2629 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
2630 urb->setup_packet, (unsigned long)urb->setup_dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002631 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
2632
2633 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2634 int i;
2635
2636 for (i = 0; i < urb->number_of_packets; i++) {
2637 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
2638 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
2639 urb->iso_frame_desc[i].offset,
2640 urb->iso_frame_desc[i].length);
2641 }
2642 }
2643#endif
2644}
2645
2646/*
2647 * Starts processing a USB transfer request specified by a USB Request Block
2648 * (URB). mem_flags indicates the type of memory allocation to use while
2649 * processing this URB.
2650 */
2651static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
2652 gfp_t mem_flags)
2653{
2654 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2655 struct usb_host_endpoint *ep = urb->ep;
2656 struct dwc2_hcd_urb *dwc2_urb;
2657 int i;
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002658 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002659 int alloc_bandwidth = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002660 u8 ep_type = 0;
2661 u32 tflags = 0;
2662 void *buf;
2663 unsigned long flags;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002664 struct dwc2_qh *qh;
2665 bool qh_allocated = false;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002666 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002667
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002668 if (dbg_urb(urb)) {
2669 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
2670 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
2671 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002672
2673 if (ep == NULL)
2674 return -EINVAL;
2675
2676 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
2677 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
2678 spin_lock_irqsave(&hsotg->lock, flags);
2679 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
2680 alloc_bandwidth = 1;
2681 spin_unlock_irqrestore(&hsotg->lock, flags);
2682 }
2683
2684 switch (usb_pipetype(urb->pipe)) {
2685 case PIPE_CONTROL:
2686 ep_type = USB_ENDPOINT_XFER_CONTROL;
2687 break;
2688 case PIPE_ISOCHRONOUS:
2689 ep_type = USB_ENDPOINT_XFER_ISOC;
2690 break;
2691 case PIPE_BULK:
2692 ep_type = USB_ENDPOINT_XFER_BULK;
2693 break;
2694 case PIPE_INTERRUPT:
2695 ep_type = USB_ENDPOINT_XFER_INT;
2696 break;
2697 default:
2698 dev_warn(hsotg->dev, "Wrong ep type\n");
2699 }
2700
2701 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
2702 mem_flags);
2703 if (!dwc2_urb)
2704 return -ENOMEM;
2705
2706 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
2707 usb_pipeendpoint(urb->pipe), ep_type,
2708 usb_pipein(urb->pipe),
2709 usb_maxpacket(urb->dev, urb->pipe,
2710 !(usb_pipein(urb->pipe))));
2711
2712 buf = urb->transfer_buffer;
Paul Zimmerman25a49442013-07-13 14:53:53 -07002713
Paul Zimmerman7359d482013-03-11 17:47:59 -07002714 if (hcd->self.uses_dma) {
Paul Zimmerman25a49442013-07-13 14:53:53 -07002715 if (!buf && (urb->transfer_dma & 3)) {
2716 dev_err(hsotg->dev,
2717 "%s: unaligned transfer with no transfer_buffer",
2718 __func__);
2719 retval = -EINVAL;
Gregory Herrero33ad2612015-04-29 22:09:15 +02002720 goto fail0;
Paul Zimmerman25a49442013-07-13 14:53:53 -07002721 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002722 }
2723
2724 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
2725 tflags |= URB_GIVEBACK_ASAP;
2726 if (urb->transfer_flags & URB_ZERO_PACKET)
2727 tflags |= URB_SEND_ZERO_PACKET;
2728
2729 dwc2_urb->priv = urb;
2730 dwc2_urb->buf = buf;
2731 dwc2_urb->dma = urb->transfer_dma;
2732 dwc2_urb->length = urb->transfer_buffer_length;
2733 dwc2_urb->setup_packet = urb->setup_packet;
2734 dwc2_urb->setup_dma = urb->setup_dma;
2735 dwc2_urb->flags = tflags;
2736 dwc2_urb->interval = urb->interval;
2737 dwc2_urb->status = -EINPROGRESS;
2738
2739 for (i = 0; i < urb->number_of_packets; ++i)
2740 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
2741 urb->iso_frame_desc[i].offset,
2742 urb->iso_frame_desc[i].length);
2743
2744 urb->hcpriv = dwc2_urb;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002745 qh = (struct dwc2_qh *) ep->hcpriv;
2746 /* Create QH for the endpoint if it doesn't exist */
2747 if (!qh) {
2748 qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
2749 if (!qh) {
2750 retval = -ENOMEM;
2751 goto fail0;
2752 }
2753 ep->hcpriv = qh;
2754 qh_allocated = true;
2755 }
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002756
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002757 qtd = kzalloc(sizeof(*qtd), mem_flags);
2758 if (!qtd) {
2759 retval = -ENOMEM;
2760 goto fail1;
2761 }
2762
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002763 spin_lock_irqsave(&hsotg->lock, flags);
2764 retval = usb_hcd_link_urb_to_ep(hcd, urb);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002765 if (retval)
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002766 goto fail2;
2767
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002768 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
2769 if (retval)
2770 goto fail3;
2771
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002772 if (alloc_bandwidth) {
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002773 dwc2_allocate_bus_bandwidth(hcd,
2774 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
2775 urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002776 }
2777
Gregory Herrero33ad2612015-04-29 22:09:15 +02002778 spin_unlock_irqrestore(&hsotg->lock, flags);
2779
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002780 return 0;
2781
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002782fail3:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002783 dwc2_urb->priv = NULL;
2784 usb_hcd_unlink_urb_from_ep(hcd, urb);
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002785fail2:
Gregory Herrero33ad2612015-04-29 22:09:15 +02002786 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002787 urb->hcpriv = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002788 kfree(qtd);
2789fail1:
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002790 if (qh_allocated) {
2791 struct dwc2_qtd *qtd2, *qtd2_tmp;
2792
2793 ep->hcpriv = NULL;
2794 dwc2_hcd_qh_unlink(hsotg, qh);
2795 /* Free each QTD in the QH's QTD list */
2796 list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
2797 qtd_list_entry)
2798 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
2799 dwc2_hcd_qh_free(hsotg, qh);
2800 }
Gregory Herrero33ad2612015-04-29 22:09:15 +02002801fail0:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002802 kfree(dwc2_urb);
2803
Paul Zimmerman7359d482013-03-11 17:47:59 -07002804 return retval;
2805}
2806
2807/*
2808 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
2809 */
2810static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
2811 int status)
2812{
2813 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002814 int rc;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002815 unsigned long flags;
2816
2817 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
2818 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
2819
2820 spin_lock_irqsave(&hsotg->lock, flags);
2821
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002822 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
2823 if (rc)
2824 goto out;
2825
Paul Zimmerman7359d482013-03-11 17:47:59 -07002826 if (!urb->hcpriv) {
2827 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
2828 goto out;
2829 }
2830
2831 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
2832
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07002833 usb_hcd_unlink_urb_from_ep(hcd, urb);
2834
Paul Zimmerman7359d482013-03-11 17:47:59 -07002835 kfree(urb->hcpriv);
2836 urb->hcpriv = NULL;
2837
2838 /* Higher layer software sets URB status */
2839 spin_unlock(&hsotg->lock);
2840 usb_hcd_giveback_urb(hcd, urb, status);
2841 spin_lock(&hsotg->lock);
2842
2843 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
2844 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
2845out:
2846 spin_unlock_irqrestore(&hsotg->lock, flags);
2847
2848 return rc;
2849}
2850
2851/*
2852 * Frees resources in the DWC_otg controller related to a given endpoint. Also
2853 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
2854 * must already be dequeued.
2855 */
2856static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
2857 struct usb_host_endpoint *ep)
2858{
2859 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2860
2861 dev_dbg(hsotg->dev,
2862 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
2863 ep->desc.bEndpointAddress, ep->hcpriv);
2864 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
2865}
2866
2867/*
2868 * Resets endpoint specific parameter values, in current version used to reset
2869 * the data toggle (as a WA). This function can be called from usb_clear_halt
2870 * routine.
2871 */
2872static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
2873 struct usb_host_endpoint *ep)
2874{
2875 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002876 unsigned long flags;
2877
2878 dev_dbg(hsotg->dev,
2879 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
2880 ep->desc.bEndpointAddress);
2881
Paul Zimmerman7359d482013-03-11 17:47:59 -07002882 spin_lock_irqsave(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002883 dwc2_hcd_endpoint_reset(hsotg, ep);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002884 spin_unlock_irqrestore(&hsotg->lock, flags);
2885}
2886
2887/*
2888 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
2889 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
2890 * interrupt.
2891 *
2892 * This function is called by the USB core when an interrupt occurs
2893 */
2894static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
2895{
2896 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002897
Matthijs Kooijmanca18f4a2013-04-25 23:39:15 +02002898 return dwc2_handle_hcd_intr(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002899}
2900
2901/*
2902 * Creates Status Change bitmap for the root hub and root port. The bitmap is
2903 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
2904 * is the status change indicator for the single root port. Returns 1 if either
2905 * change indicator is 1, otherwise returns 0.
2906 */
2907static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
2908{
2909 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2910
2911 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
2912 return buf[0] != 0;
2913}
2914
2915/* Handles hub class-specific requests */
2916static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
2917 u16 windex, char *buf, u16 wlength)
2918{
2919 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
2920 wvalue, windex, buf, wlength);
2921 return retval;
2922}
2923
2924/* Handles hub TT buffer clear completions */
2925static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
2926 struct usb_host_endpoint *ep)
2927{
2928 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
2929 struct dwc2_qh *qh;
2930 unsigned long flags;
2931
2932 qh = ep->hcpriv;
2933 if (!qh)
2934 return;
2935
2936 spin_lock_irqsave(&hsotg->lock, flags);
2937 qh->tt_buffer_dirty = 0;
2938
2939 if (hsotg->flags.b.port_connect_status)
2940 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
2941
2942 spin_unlock_irqrestore(&hsotg->lock, flags);
2943}
2944
2945static struct hc_driver dwc2_hc_driver = {
2946 .description = "dwc2_hsotg",
2947 .product_desc = "DWC OTG Controller",
2948 .hcd_priv_size = sizeof(struct wrapper_priv_data),
2949
2950 .irq = _dwc2_hcd_irq,
2951 .flags = HCD_MEMORY | HCD_USB2,
2952
2953 .start = _dwc2_hcd_start,
2954 .stop = _dwc2_hcd_stop,
2955 .urb_enqueue = _dwc2_hcd_urb_enqueue,
2956 .urb_dequeue = _dwc2_hcd_urb_dequeue,
2957 .endpoint_disable = _dwc2_hcd_endpoint_disable,
2958 .endpoint_reset = _dwc2_hcd_endpoint_reset,
2959 .get_frame_number = _dwc2_hcd_get_frame_number,
2960
2961 .hub_status_data = _dwc2_hcd_hub_status_data,
2962 .hub_control = _dwc2_hcd_hub_control,
2963 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
Gregory Herrero99a65792015-04-29 22:09:13 +02002964
2965 .bus_suspend = _dwc2_hcd_suspend,
2966 .bus_resume = _dwc2_hcd_resume,
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002967
2968 .map_urb_for_dma = dwc2_map_urb_for_dma,
2969 .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
Paul Zimmerman7359d482013-03-11 17:47:59 -07002970};
2971
2972/*
2973 * Frees secondary storage associated with the dwc2_hsotg structure contained
2974 * in the struct usb_hcd field
2975 */
2976static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
2977{
2978 u32 ahbcfg;
2979 u32 dctl;
2980 int i;
2981
2982 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
2983
2984 /* Free memory for QH/QTD lists */
2985 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
2986 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
2987 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
2988 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
2989 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
2990 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
2991
2992 /* Free memory for the host channels */
2993 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
2994 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
2995
2996 if (chan != NULL) {
2997 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
2998 i, chan);
2999 hsotg->hc_ptr_array[i] = NULL;
3000 kfree(chan);
3001 }
3002 }
3003
3004 if (hsotg->core_params->dma_enable > 0) {
3005 if (hsotg->status_buf) {
3006 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
3007 hsotg->status_buf,
3008 hsotg->status_buf_dma);
3009 hsotg->status_buf = NULL;
3010 }
3011 } else {
3012 kfree(hsotg->status_buf);
3013 hsotg->status_buf = NULL;
3014 }
3015
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003016 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003017
3018 /* Disable all interrupts */
3019 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003020 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
3021 dwc2_writel(0, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003022
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003023 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003024 dctl = dwc2_readl(hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003025 dctl |= DCTL_SFTDISCON;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003026 dwc2_writel(dctl, hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003027 }
3028
3029 if (hsotg->wq_otg) {
3030 if (!cancel_work_sync(&hsotg->wf_otg))
3031 flush_workqueue(hsotg->wq_otg);
3032 destroy_workqueue(hsotg->wq_otg);
3033 }
3034
Paul Zimmerman7359d482013-03-11 17:47:59 -07003035 del_timer(&hsotg->wkp_timer);
3036}
3037
3038static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
3039{
3040 /* Turn off all host-specific interrupts */
3041 dwc2_disable_host_interrupts(hsotg);
3042
3043 dwc2_hcd_free(hsotg);
3044}
3045
Matthijs Kooijman8284f932013-04-11 18:43:47 +02003046/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07003047 * Initializes the HCD. This function allocates memory for and initializes the
3048 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
3049 * USB bus with the core and calls the hc_driver->start() function. It returns
3050 * a negative error on failure.
3051 */
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02003052int dwc2_hcd_init(struct dwc2_hsotg *hsotg, int irq)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003053{
3054 struct usb_hcd *hcd;
3055 struct dwc2_host_chan *channel;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003056 u32 hcfg;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003057 int i, num_channels;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003058 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003059
Dinh Nguyenf5500ec2014-11-11 11:13:39 -06003060 if (usb_disabled())
3061 return -ENODEV;
3062
Paul Zimmermane62662c2013-03-25 17:03:35 -07003063 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003064
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003065 retval = -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003066
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003067 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003068 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003069
3070#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
3071 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
3072 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
3073 if (!hsotg->frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07003074 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003075 hsotg->last_frame_num_array = kzalloc(
3076 sizeof(*hsotg->last_frame_num_array) *
3077 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
3078 if (!hsotg->last_frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07003079 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003080 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
3081#endif
3082
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02003083 /* Check if the bus driver or platform code has setup a dma_mask */
3084 if (hsotg->core_params->dma_enable > 0 &&
3085 hsotg->dev->dma_mask == NULL) {
3086 dev_warn(hsotg->dev,
3087 "dma_mask not set, disabling DMA\n");
3088 hsotg->core_params->dma_enable = 0;
3089 hsotg->core_params->dma_desc_enable = 0;
3090 }
3091
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07003092 /* Set device flags indicating whether the HCD supports DMA */
3093 if (hsotg->core_params->dma_enable > 0) {
Paul Zimmerman30885312013-05-24 16:27:56 -07003094 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
3095 dev_warn(hsotg->dev, "can't set DMA mask\n");
Paul Zimmerman25a49442013-07-13 14:53:53 -07003096 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
3097 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07003098 }
3099
3100 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
3101 if (!hcd)
3102 goto error1;
3103
Matthijs Kooijman7de76ee2013-07-19 11:34:23 +02003104 if (hsotg->core_params->dma_enable <= 0)
3105 hcd->self.uses_dma = 0;
3106
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07003107 hcd->has_tt = 1;
3108
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07003109 ((struct wrapper_priv_data *) &hcd->hcd_priv)->hsotg = hsotg;
3110 hsotg->priv = hcd;
3111
Paul Zimmerman7359d482013-03-11 17:47:59 -07003112 /*
3113 * Disable the global interrupt until all the interrupt handlers are
3114 * installed
3115 */
3116 dwc2_disable_global_interrupts(hsotg);
3117
Matthijs Kooijman6706c722013-04-11 17:52:41 +02003118 /* Initialize the DWC_otg core, and select the Phy type */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003119 retval = dwc2_core_init(hsotg, true);
Matthijs Kooijman6706c722013-04-11 17:52:41 +02003120 if (retval)
3121 goto error2;
3122
Paul Zimmerman7359d482013-03-11 17:47:59 -07003123 /* Create new workqueue and init work */
Wei Yongjun53510352013-04-12 22:41:48 +08003124 retval = -ENOMEM;
Matthijs Kooijman050232a2013-04-11 18:43:46 +02003125 hsotg->wq_otg = create_singlethread_workqueue("dwc2");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003126 if (!hsotg->wq_otg) {
3127 dev_err(hsotg->dev, "Failed to create workqueue\n");
3128 goto error2;
3129 }
3130 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
3131
Paul Zimmerman7359d482013-03-11 17:47:59 -07003132 setup_timer(&hsotg->wkp_timer, dwc2_wakeup_detected,
3133 (unsigned long)hsotg);
3134
3135 /* Initialize the non-periodic schedule */
3136 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
3137 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
3138
3139 /* Initialize the periodic schedule */
3140 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
3141 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
3142 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
3143 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
3144
3145 /*
3146 * Create a host channel descriptor for each host channel implemented
3147 * in the controller. Initialize the channel descriptor array.
3148 */
3149 INIT_LIST_HEAD(&hsotg->free_hc_list);
3150 num_channels = hsotg->core_params->host_channels;
3151 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
3152
3153 for (i = 0; i < num_channels; i++) {
3154 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
3155 if (channel == NULL)
3156 goto error3;
3157 channel->hc_num = i;
3158 hsotg->hc_ptr_array[i] = channel;
3159 }
3160
Dom Cobley20f2eb92013-09-23 14:23:34 -07003161 if (hsotg->core_params->uframe_sched > 0)
3162 dwc2_hcd_init_usecs(hsotg);
3163
Paul Zimmerman7359d482013-03-11 17:47:59 -07003164 /* Initialize hsotg start work */
3165 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
3166
3167 /* Initialize port reset work */
3168 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
3169
3170 /*
3171 * Allocate space for storing data on status transactions. Normally no
3172 * data is sent, but this space acts as a bit bucket. This must be
3173 * done after usb_add_hcd since that function allocates the DMA buffer
3174 * pool.
3175 */
3176 if (hsotg->core_params->dma_enable > 0)
3177 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
3178 DWC2_HCD_STATUS_BUF_SIZE,
3179 &hsotg->status_buf_dma, GFP_KERNEL);
3180 else
3181 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
3182 GFP_KERNEL);
3183
3184 if (!hsotg->status_buf)
3185 goto error3;
3186
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01003187 /*
3188 * Create kmem caches to handle descriptor buffers in descriptor
3189 * DMA mode.
3190 * Alignment must be set to 512 bytes.
3191 */
3192 if (hsotg->core_params->dma_desc_enable ||
3193 hsotg->core_params->dma_desc_fs_enable) {
3194 hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
3195 sizeof(struct dwc2_hcd_dma_desc) *
3196 MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
3197 NULL);
3198 if (!hsotg->desc_gen_cache) {
3199 dev_err(hsotg->dev,
3200 "unable to create dwc2 generic desc cache\n");
3201
3202 /*
3203 * Disable descriptor dma mode since it will not be
3204 * usable.
3205 */
3206 hsotg->core_params->dma_desc_enable = 0;
3207 hsotg->core_params->dma_desc_fs_enable = 0;
3208 }
3209
3210 hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
3211 sizeof(struct dwc2_hcd_dma_desc) *
3212 MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
3213 if (!hsotg->desc_hsisoc_cache) {
3214 dev_err(hsotg->dev,
3215 "unable to create dwc2 hs isoc desc cache\n");
3216
3217 kmem_cache_destroy(hsotg->desc_gen_cache);
3218
3219 /*
3220 * Disable descriptor dma mode since it will not be
3221 * usable.
3222 */
3223 hsotg->core_params->dma_desc_enable = 0;
3224 hsotg->core_params->dma_desc_fs_enable = 0;
3225 }
3226 }
3227
Paul Zimmerman7359d482013-03-11 17:47:59 -07003228 hsotg->otg_port = 1;
3229 hsotg->frame_list = NULL;
3230 hsotg->frame_list_dma = 0;
3231 hsotg->periodic_qh_count = 0;
3232
3233 /* Initiate lx_state to L3 disconnected state */
3234 hsotg->lx_state = DWC2_L3;
3235
3236 hcd->self.otg_port = hsotg->otg_port;
3237
3238 /* Don't support SG list at this point */
3239 hcd->self.sg_tablesize = 0;
3240
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02003241 if (!IS_ERR_OR_NULL(hsotg->uphy))
3242 otg_set_host(hsotg->uphy->otg, &hcd->self);
3243
Paul Zimmerman7359d482013-03-11 17:47:59 -07003244 /*
3245 * Finish generic HCD initialization and start the HCD. This function
3246 * allocates the DMA buffer pool, registers the USB bus, requests the
3247 * IRQ line, and calls hcd_start method.
3248 */
Matthijs Kooijman66513f42013-04-25 23:39:13 +02003249 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003250 if (retval < 0)
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01003251 goto error4;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003252
Peter Chen3c9740a2013-11-05 10:46:02 +08003253 device_wakeup_enable(hcd->self.controller);
3254
Paul Zimmerman7359d482013-03-11 17:47:59 -07003255 dwc2_hcd_dump_state(hsotg);
3256
3257 dwc2_enable_global_interrupts(hsotg);
3258
3259 return 0;
3260
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01003261error4:
3262 kmem_cache_destroy(hsotg->desc_gen_cache);
3263 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003264error3:
3265 dwc2_hcd_release(hsotg);
3266error2:
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07003267 usb_put_hcd(hcd);
3268error1:
Paul Zimmerman7359d482013-03-11 17:47:59 -07003269 kfree(hsotg->core_params);
3270
3271#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
3272 kfree(hsotg->last_frame_num_array);
3273 kfree(hsotg->frame_num_array);
3274#endif
3275
Paul Zimmermane62662c2013-03-25 17:03:35 -07003276 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003277 return retval;
3278}
Paul Zimmerman7359d482013-03-11 17:47:59 -07003279
3280/*
3281 * Removes the HCD.
3282 * Frees memory and resources associated with the HCD and deregisters the bus.
3283 */
Paul Zimmermane62662c2013-03-25 17:03:35 -07003284void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003285{
3286 struct usb_hcd *hcd;
3287
Paul Zimmermane62662c2013-03-25 17:03:35 -07003288 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003289
3290 hcd = dwc2_hsotg_to_hcd(hsotg);
Paul Zimmermane62662c2013-03-25 17:03:35 -07003291 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003292
3293 if (!hcd) {
Paul Zimmermane62662c2013-03-25 17:03:35 -07003294 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
Paul Zimmerman7359d482013-03-11 17:47:59 -07003295 __func__);
3296 return;
3297 }
3298
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02003299 if (!IS_ERR_OR_NULL(hsotg->uphy))
3300 otg_set_host(hsotg->uphy->otg, NULL);
3301
Paul Zimmerman7359d482013-03-11 17:47:59 -07003302 usb_remove_hcd(hcd);
3303 hsotg->priv = NULL;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01003304
3305 kmem_cache_destroy(hsotg->desc_gen_cache);
3306 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
3307
Paul Zimmerman7359d482013-03-11 17:47:59 -07003308 dwc2_hcd_release(hsotg);
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07003309 usb_put_hcd(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003310
3311#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
3312 kfree(hsotg->last_frame_num_array);
3313 kfree(hsotg->frame_num_array);
3314#endif
Paul Zimmerman7359d482013-03-11 17:47:59 -07003315}