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