]>
Commit | Line | Data |
---|---|---|
72246da4 FB |
1 | /** |
2 | * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link | |
3 | * | |
4 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com | |
72246da4 FB |
5 | * |
6 | * Authors: Felipe Balbi <[email protected]>, | |
7 | * Sebastian Andrzej Siewior <[email protected]> | |
8 | * | |
5945f789 FB |
9 | * This program is free software: you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License version 2 of | |
11 | * the License as published by the Free Software Foundation. | |
72246da4 | 12 | * |
5945f789 FB |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
72246da4 FB |
17 | */ |
18 | ||
19 | #include <linux/kernel.h> | |
20 | #include <linux/delay.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/spinlock.h> | |
23 | #include <linux/platform_device.h> | |
24 | #include <linux/pm_runtime.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/io.h> | |
27 | #include <linux/list.h> | |
28 | #include <linux/dma-mapping.h> | |
29 | ||
30 | #include <linux/usb/ch9.h> | |
31 | #include <linux/usb/gadget.h> | |
32 | ||
80977dc9 | 33 | #include "debug.h" |
72246da4 FB |
34 | #include "core.h" |
35 | #include "gadget.h" | |
36 | #include "io.h" | |
37 | ||
04a9bfcd FB |
38 | /** |
39 | * dwc3_gadget_set_test_mode - Enables USB2 Test Modes | |
40 | * @dwc: pointer to our context structure | |
41 | * @mode: the mode to set (J, K SE0 NAK, Force Enable) | |
42 | * | |
43 | * Caller should take care of locking. This function will | |
44 | * return 0 on success or -EINVAL if wrong Test Selector | |
45 | * is passed | |
46 | */ | |
47 | int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) | |
48 | { | |
49 | u32 reg; | |
50 | ||
51 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
52 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; | |
53 | ||
54 | switch (mode) { | |
55 | case TEST_J: | |
56 | case TEST_K: | |
57 | case TEST_SE0_NAK: | |
58 | case TEST_PACKET: | |
59 | case TEST_FORCE_EN: | |
60 | reg |= mode << 1; | |
61 | break; | |
62 | default: | |
63 | return -EINVAL; | |
64 | } | |
65 | ||
66 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
67 | ||
68 | return 0; | |
69 | } | |
70 | ||
911f1f88 PZ |
71 | /** |
72 | * dwc3_gadget_get_link_state - Gets current state of USB Link | |
73 | * @dwc: pointer to our context structure | |
74 | * | |
75 | * Caller should take care of locking. This function will | |
76 | * return the link state on success (>= 0) or -ETIMEDOUT. | |
77 | */ | |
78 | int dwc3_gadget_get_link_state(struct dwc3 *dwc) | |
79 | { | |
80 | u32 reg; | |
81 | ||
82 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
83 | ||
84 | return DWC3_DSTS_USBLNKST(reg); | |
85 | } | |
86 | ||
8598bde7 FB |
87 | /** |
88 | * dwc3_gadget_set_link_state - Sets USB Link to a particular State | |
89 | * @dwc: pointer to our context structure | |
90 | * @state: the state to put link into | |
91 | * | |
92 | * Caller should take care of locking. This function will | |
aee63e3c | 93 | * return 0 on success or -ETIMEDOUT. |
8598bde7 FB |
94 | */ |
95 | int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) | |
96 | { | |
aee63e3c | 97 | int retries = 10000; |
8598bde7 FB |
98 | u32 reg; |
99 | ||
802fde98 PZ |
100 | /* |
101 | * Wait until device controller is ready. Only applies to 1.94a and | |
102 | * later RTL. | |
103 | */ | |
104 | if (dwc->revision >= DWC3_REVISION_194A) { | |
105 | while (--retries) { | |
106 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
107 | if (reg & DWC3_DSTS_DCNRD) | |
108 | udelay(5); | |
109 | else | |
110 | break; | |
111 | } | |
112 | ||
113 | if (retries <= 0) | |
114 | return -ETIMEDOUT; | |
115 | } | |
116 | ||
8598bde7 FB |
117 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
118 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; | |
119 | ||
120 | /* set requested state */ | |
121 | reg |= DWC3_DCTL_ULSTCHNGREQ(state); | |
122 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
123 | ||
802fde98 PZ |
124 | /* |
125 | * The following code is racy when called from dwc3_gadget_wakeup, | |
126 | * and is not needed, at least on newer versions | |
127 | */ | |
128 | if (dwc->revision >= DWC3_REVISION_194A) | |
129 | return 0; | |
130 | ||
8598bde7 | 131 | /* wait for a change in DSTS */ |
aed430e5 | 132 | retries = 10000; |
8598bde7 FB |
133 | while (--retries) { |
134 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
135 | ||
8598bde7 FB |
136 | if (DWC3_DSTS_USBLNKST(reg) == state) |
137 | return 0; | |
138 | ||
aee63e3c | 139 | udelay(5); |
8598bde7 FB |
140 | } |
141 | ||
73815280 FB |
142 | dwc3_trace(trace_dwc3_gadget, |
143 | "link state change request timed out"); | |
8598bde7 FB |
144 | |
145 | return -ETIMEDOUT; | |
146 | } | |
147 | ||
ef966b9d | 148 | static void dwc3_ep_inc_enq(struct dwc3_ep *dep) |
457e84b6 | 149 | { |
ef966b9d | 150 | dep->trb_enqueue++; |
4faf7550 | 151 | dep->trb_enqueue %= DWC3_TRB_NUM; |
ef966b9d | 152 | } |
457e84b6 | 153 | |
ef966b9d FB |
154 | static void dwc3_ep_inc_deq(struct dwc3_ep *dep) |
155 | { | |
156 | dep->trb_dequeue++; | |
4faf7550 | 157 | dep->trb_dequeue %= DWC3_TRB_NUM; |
ef966b9d | 158 | } |
457e84b6 | 159 | |
ef966b9d FB |
160 | static int dwc3_ep_is_last_trb(unsigned int index) |
161 | { | |
4faf7550 | 162 | return index == DWC3_TRB_NUM - 1; |
457e84b6 FB |
163 | } |
164 | ||
72246da4 FB |
165 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, |
166 | int status) | |
167 | { | |
168 | struct dwc3 *dwc = dep->dwc; | |
e5ba5ec8 | 169 | int i; |
72246da4 | 170 | |
aa3342c8 | 171 | if (req->started) { |
e5ba5ec8 PA |
172 | i = 0; |
173 | do { | |
ef966b9d | 174 | dwc3_ep_inc_deq(dep); |
e5ba5ec8 PA |
175 | /* |
176 | * Skip LINK TRB. We can't use req->trb and check for | |
177 | * DWC3_TRBCTL_LINK_TRB because it points the TRB we | |
178 | * just completed (not the LINK TRB). | |
179 | */ | |
36b68aae | 180 | if (dwc3_ep_is_last_trb(dep->trb_dequeue)) |
ef966b9d | 181 | dwc3_ep_inc_deq(dep); |
e5ba5ec8 | 182 | } while(++i < req->request.num_mapped_sgs); |
aa3342c8 | 183 | req->started = false; |
72246da4 FB |
184 | } |
185 | list_del(&req->list); | |
eeb720fb | 186 | req->trb = NULL; |
72246da4 FB |
187 | |
188 | if (req->request.status == -EINPROGRESS) | |
189 | req->request.status = status; | |
190 | ||
0416e494 PA |
191 | if (dwc->ep0_bounced && dep->number == 0) |
192 | dwc->ep0_bounced = false; | |
193 | else | |
194 | usb_gadget_unmap_request(&dwc->gadget, &req->request, | |
195 | req->direction); | |
72246da4 | 196 | |
2c4cbe6e | 197 | trace_dwc3_gadget_giveback(req); |
72246da4 FB |
198 | |
199 | spin_unlock(&dwc->lock); | |
304f7e5e | 200 | usb_gadget_giveback_request(&dep->endpoint, &req->request); |
72246da4 FB |
201 | spin_lock(&dwc->lock); |
202 | } | |
203 | ||
3ece0ec4 | 204 | int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param) |
b09bb642 FB |
205 | { |
206 | u32 timeout = 500; | |
207 | u32 reg; | |
208 | ||
2c4cbe6e | 209 | trace_dwc3_gadget_generic_cmd(cmd, param); |
427c3df6 | 210 | |
b09bb642 FB |
211 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); |
212 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); | |
213 | ||
214 | do { | |
215 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); | |
216 | if (!(reg & DWC3_DGCMD_CMDACT)) { | |
73815280 FB |
217 | dwc3_trace(trace_dwc3_gadget, |
218 | "Command Complete --> %d", | |
b09bb642 | 219 | DWC3_DGCMD_STATUS(reg)); |
891b1dc0 SSB |
220 | if (DWC3_DGCMD_STATUS(reg)) |
221 | return -EINVAL; | |
b09bb642 FB |
222 | return 0; |
223 | } | |
224 | ||
225 | /* | |
226 | * We can't sleep here, because it's also called from | |
227 | * interrupt context. | |
228 | */ | |
229 | timeout--; | |
73815280 FB |
230 | if (!timeout) { |
231 | dwc3_trace(trace_dwc3_gadget, | |
232 | "Command Timed Out"); | |
b09bb642 | 233 | return -ETIMEDOUT; |
73815280 | 234 | } |
b09bb642 FB |
235 | udelay(1); |
236 | } while (1); | |
237 | } | |
238 | ||
c36d8e94 FB |
239 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc); |
240 | ||
2cd4718d FB |
241 | int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd, |
242 | struct dwc3_gadget_ep_cmd_params *params) | |
72246da4 | 243 | { |
2cd4718d | 244 | struct dwc3 *dwc = dep->dwc; |
61d58242 | 245 | u32 timeout = 500; |
72246da4 FB |
246 | u32 reg; |
247 | ||
2b0f11df | 248 | int susphy = false; |
c0ca324d | 249 | int ret = -EINVAL; |
72246da4 | 250 | |
2c4cbe6e | 251 | trace_dwc3_gadget_ep_cmd(dep, cmd, params); |
72246da4 | 252 | |
2b0f11df FB |
253 | /* |
254 | * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if | |
255 | * we're issuing an endpoint command, we must check if | |
256 | * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it. | |
257 | * | |
258 | * We will also set SUSPHY bit to what it was before returning as stated | |
259 | * by the same section on Synopsys databook. | |
260 | */ | |
261 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); | |
262 | if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { | |
263 | susphy = true; | |
264 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; | |
265 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); | |
266 | } | |
267 | ||
c36d8e94 FB |
268 | if (cmd == DWC3_DEPCMD_STARTTRANSFER) { |
269 | int needs_wakeup; | |
270 | ||
271 | needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 || | |
272 | dwc->link_state == DWC3_LINK_STATE_U2 || | |
273 | dwc->link_state == DWC3_LINK_STATE_U3); | |
274 | ||
275 | if (unlikely(needs_wakeup)) { | |
276 | ret = __dwc3_gadget_wakeup(dwc); | |
277 | dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n", | |
278 | ret); | |
279 | } | |
280 | } | |
281 | ||
2eb88016 FB |
282 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); |
283 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); | |
284 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); | |
72246da4 | 285 | |
2eb88016 | 286 | dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT); |
72246da4 | 287 | do { |
2eb88016 | 288 | reg = dwc3_readl(dep->regs, DWC3_DEPCMD); |
72246da4 | 289 | if (!(reg & DWC3_DEPCMD_CMDACT)) { |
7b9cc7a2 KL |
290 | int cmd_status = DWC3_DEPCMD_STATUS(reg); |
291 | ||
73815280 FB |
292 | dwc3_trace(trace_dwc3_gadget, |
293 | "Command Complete --> %d", | |
7b9cc7a2 KL |
294 | cmd_status); |
295 | ||
296 | switch (cmd_status) { | |
297 | case 0: | |
298 | ret = 0; | |
299 | break; | |
300 | case DEPEVT_TRANSFER_NO_RESOURCE: | |
301 | dwc3_trace(trace_dwc3_gadget, "%s: no resource available"); | |
302 | ret = -EINVAL; | |
c0ca324d | 303 | break; |
7b9cc7a2 KL |
304 | case DEPEVT_TRANSFER_BUS_EXPIRY: |
305 | /* | |
306 | * SW issues START TRANSFER command to | |
307 | * isochronous ep with future frame interval. If | |
308 | * future interval time has already passed when | |
309 | * core receives the command, it will respond | |
310 | * with an error status of 'Bus Expiry'. | |
311 | * | |
312 | * Instead of always returning -EINVAL, let's | |
313 | * give a hint to the gadget driver that this is | |
314 | * the case by returning -EAGAIN. | |
315 | */ | |
316 | dwc3_trace(trace_dwc3_gadget, "%s: bus expiry"); | |
317 | ret = -EAGAIN; | |
318 | break; | |
319 | default: | |
320 | dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); | |
321 | } | |
322 | ||
c0ca324d | 323 | break; |
72246da4 FB |
324 | } |
325 | ||
326 | /* | |
72246da4 FB |
327 | * We can't sleep here, because it is also called from |
328 | * interrupt context. | |
329 | */ | |
330 | timeout--; | |
73815280 FB |
331 | if (!timeout) { |
332 | dwc3_trace(trace_dwc3_gadget, | |
333 | "Command Timed Out"); | |
c0ca324d FB |
334 | ret = -ETIMEDOUT; |
335 | break; | |
73815280 | 336 | } |
72246da4 | 337 | } while (1); |
c0ca324d | 338 | |
2b0f11df FB |
339 | if (unlikely(susphy)) { |
340 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); | |
341 | reg |= DWC3_GUSB2PHYCFG_SUSPHY; | |
342 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); | |
343 | } | |
344 | ||
c0ca324d | 345 | return ret; |
72246da4 FB |
346 | } |
347 | ||
50c763f8 JY |
348 | static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) |
349 | { | |
350 | struct dwc3 *dwc = dep->dwc; | |
351 | struct dwc3_gadget_ep_cmd_params params; | |
352 | u32 cmd = DWC3_DEPCMD_CLEARSTALL; | |
353 | ||
354 | /* | |
355 | * As of core revision 2.60a the recommended programming model | |
356 | * is to set the ClearPendIN bit when issuing a Clear Stall EP | |
357 | * command for IN endpoints. This is to prevent an issue where | |
358 | * some (non-compliant) hosts may not send ACK TPs for pending | |
359 | * IN transfers due to a mishandled error condition. Synopsys | |
360 | * STAR 9000614252. | |
361 | */ | |
362 | if (dep->direction && (dwc->revision >= DWC3_REVISION_260A)) | |
363 | cmd |= DWC3_DEPCMD_CLEARPENDIN; | |
364 | ||
365 | memset(¶ms, 0, sizeof(params)); | |
366 | ||
2cd4718d | 367 | return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
50c763f8 JY |
368 | } |
369 | ||
72246da4 | 370 | static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, |
f6bafc6a | 371 | struct dwc3_trb *trb) |
72246da4 | 372 | { |
c439ef87 | 373 | u32 offset = (char *) trb - (char *) dep->trb_pool; |
72246da4 FB |
374 | |
375 | return dep->trb_pool_dma + offset; | |
376 | } | |
377 | ||
378 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) | |
379 | { | |
380 | struct dwc3 *dwc = dep->dwc; | |
381 | ||
382 | if (dep->trb_pool) | |
383 | return 0; | |
384 | ||
72246da4 FB |
385 | dep->trb_pool = dma_alloc_coherent(dwc->dev, |
386 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM, | |
387 | &dep->trb_pool_dma, GFP_KERNEL); | |
388 | if (!dep->trb_pool) { | |
389 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", | |
390 | dep->name); | |
391 | return -ENOMEM; | |
392 | } | |
393 | ||
394 | return 0; | |
395 | } | |
396 | ||
397 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) | |
398 | { | |
399 | struct dwc3 *dwc = dep->dwc; | |
400 | ||
401 | dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, | |
402 | dep->trb_pool, dep->trb_pool_dma); | |
403 | ||
404 | dep->trb_pool = NULL; | |
405 | dep->trb_pool_dma = 0; | |
406 | } | |
407 | ||
c4509601 JY |
408 | static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep); |
409 | ||
410 | /** | |
411 | * dwc3_gadget_start_config - Configure EP resources | |
412 | * @dwc: pointer to our controller context structure | |
413 | * @dep: endpoint that is being enabled | |
414 | * | |
415 | * The assignment of transfer resources cannot perfectly follow the | |
416 | * data book due to the fact that the controller driver does not have | |
417 | * all knowledge of the configuration in advance. It is given this | |
418 | * information piecemeal by the composite gadget framework after every | |
419 | * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook | |
420 | * programming model in this scenario can cause errors. For two | |
421 | * reasons: | |
422 | * | |
423 | * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION | |
424 | * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of | |
425 | * multiple interfaces. | |
426 | * | |
427 | * 2) The databook does not mention doing more DEPXFERCFG for new | |
428 | * endpoint on alt setting (8.1.6). | |
429 | * | |
430 | * The following simplified method is used instead: | |
431 | * | |
432 | * All hardware endpoints can be assigned a transfer resource and this | |
433 | * setting will stay persistent until either a core reset or | |
434 | * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and | |
435 | * do DEPXFERCFG for every hardware endpoint as well. We are | |
436 | * guaranteed that there are as many transfer resources as endpoints. | |
437 | * | |
438 | * This function is called for each endpoint when it is being enabled | |
439 | * but is triggered only when called for EP0-out, which always happens | |
440 | * first, and which should only happen in one of the above conditions. | |
441 | */ | |
72246da4 FB |
442 | static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep) |
443 | { | |
444 | struct dwc3_gadget_ep_cmd_params params; | |
445 | u32 cmd; | |
c4509601 JY |
446 | int i; |
447 | int ret; | |
448 | ||
449 | if (dep->number) | |
450 | return 0; | |
72246da4 FB |
451 | |
452 | memset(¶ms, 0x00, sizeof(params)); | |
c4509601 | 453 | cmd = DWC3_DEPCMD_DEPSTARTCFG; |
72246da4 | 454 | |
2cd4718d | 455 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
c4509601 JY |
456 | if (ret) |
457 | return ret; | |
458 | ||
459 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { | |
460 | struct dwc3_ep *dep = dwc->eps[i]; | |
72246da4 | 461 | |
c4509601 JY |
462 | if (!dep) |
463 | continue; | |
464 | ||
465 | ret = dwc3_gadget_set_xfer_resource(dwc, dep); | |
466 | if (ret) | |
467 | return ret; | |
72246da4 FB |
468 | } |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
473 | static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep, | |
c90bfaec | 474 | const struct usb_endpoint_descriptor *desc, |
4b345c9a | 475 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
265b70a7 | 476 | bool ignore, bool restore) |
72246da4 FB |
477 | { |
478 | struct dwc3_gadget_ep_cmd_params params; | |
479 | ||
480 | memset(¶ms, 0x00, sizeof(params)); | |
481 | ||
dc1c70a7 | 482 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) |
d2e9a13a CP |
483 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); |
484 | ||
485 | /* Burst size is only needed in SuperSpeed mode */ | |
ee5cd41c | 486 | if (dwc->gadget.speed >= USB_SPEED_SUPER) { |
676e3497 | 487 | u32 burst = dep->endpoint.maxburst; |
676e3497 | 488 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); |
d2e9a13a | 489 | } |
72246da4 | 490 | |
4b345c9a FB |
491 | if (ignore) |
492 | params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM; | |
493 | ||
265b70a7 PZ |
494 | if (restore) { |
495 | params.param0 |= DWC3_DEPCFG_ACTION_RESTORE; | |
496 | params.param2 |= dep->saved_state; | |
497 | } | |
498 | ||
dc1c70a7 FB |
499 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN |
500 | | DWC3_DEPCFG_XFER_NOT_READY_EN; | |
72246da4 | 501 | |
18b7ede5 | 502 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { |
dc1c70a7 FB |
503 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE |
504 | | DWC3_DEPCFG_STREAM_EVENT_EN; | |
879631aa FB |
505 | dep->stream_capable = true; |
506 | } | |
507 | ||
0b93a4c8 | 508 | if (!usb_endpoint_xfer_control(desc)) |
dc1c70a7 | 509 | params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; |
72246da4 FB |
510 | |
511 | /* | |
512 | * We are doing 1:1 mapping for endpoints, meaning | |
513 | * Physical Endpoints 2 maps to Logical Endpoint 2 and | |
514 | * so on. We consider the direction bit as part of the physical | |
515 | * endpoint number. So USB endpoint 0x81 is 0x03. | |
516 | */ | |
dc1c70a7 | 517 | params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); |
72246da4 FB |
518 | |
519 | /* | |
520 | * We must use the lower 16 TX FIFOs even though | |
521 | * HW might have more | |
522 | */ | |
523 | if (dep->direction) | |
dc1c70a7 | 524 | params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); |
72246da4 FB |
525 | |
526 | if (desc->bInterval) { | |
dc1c70a7 | 527 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1); |
72246da4 FB |
528 | dep->interval = 1 << (desc->bInterval - 1); |
529 | } | |
530 | ||
2cd4718d | 531 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); |
72246da4 FB |
532 | } |
533 | ||
534 | static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep) | |
535 | { | |
536 | struct dwc3_gadget_ep_cmd_params params; | |
537 | ||
538 | memset(¶ms, 0x00, sizeof(params)); | |
539 | ||
dc1c70a7 | 540 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); |
72246da4 | 541 | |
2cd4718d FB |
542 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, |
543 | ¶ms); | |
72246da4 FB |
544 | } |
545 | ||
546 | /** | |
547 | * __dwc3_gadget_ep_enable - Initializes a HW endpoint | |
548 | * @dep: endpoint to be initialized | |
549 | * @desc: USB Endpoint Descriptor | |
550 | * | |
551 | * Caller should take care of locking | |
552 | */ | |
553 | static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, | |
c90bfaec | 554 | const struct usb_endpoint_descriptor *desc, |
4b345c9a | 555 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
265b70a7 | 556 | bool ignore, bool restore) |
72246da4 FB |
557 | { |
558 | struct dwc3 *dwc = dep->dwc; | |
559 | u32 reg; | |
b09e99ee | 560 | int ret; |
72246da4 | 561 | |
73815280 | 562 | dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name); |
ff62d6b6 | 563 | |
72246da4 FB |
564 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
565 | ret = dwc3_gadget_start_config(dwc, dep); | |
566 | if (ret) | |
567 | return ret; | |
568 | } | |
569 | ||
265b70a7 PZ |
570 | ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore, |
571 | restore); | |
72246da4 FB |
572 | if (ret) |
573 | return ret; | |
574 | ||
575 | if (!(dep->flags & DWC3_EP_ENABLED)) { | |
f6bafc6a FB |
576 | struct dwc3_trb *trb_st_hw; |
577 | struct dwc3_trb *trb_link; | |
72246da4 | 578 | |
16e78db7 | 579 | dep->endpoint.desc = desc; |
c90bfaec | 580 | dep->comp_desc = comp_desc; |
72246da4 FB |
581 | dep->type = usb_endpoint_type(desc); |
582 | dep->flags |= DWC3_EP_ENABLED; | |
583 | ||
584 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); | |
585 | reg |= DWC3_DALEPENA_EP(dep->number); | |
586 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); | |
587 | ||
36b68aae | 588 | if (usb_endpoint_xfer_control(desc)) |
e901aa15 | 589 | goto out; |
72246da4 | 590 | |
36b68aae | 591 | /* Link TRB. The HWO bit is never reset */ |
72246da4 FB |
592 | trb_st_hw = &dep->trb_pool[0]; |
593 | ||
f6bafc6a | 594 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; |
1200a82a | 595 | memset(trb_link, 0, sizeof(*trb_link)); |
72246da4 | 596 | |
f6bafc6a FB |
597 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
598 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); | |
599 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; | |
600 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; | |
72246da4 FB |
601 | } |
602 | ||
e901aa15 | 603 | out: |
aa739974 FB |
604 | switch (usb_endpoint_type(desc)) { |
605 | case USB_ENDPOINT_XFER_CONTROL: | |
e901aa15 | 606 | /* don't change name */ |
aa739974 FB |
607 | break; |
608 | case USB_ENDPOINT_XFER_ISOC: | |
609 | strlcat(dep->name, "-isoc", sizeof(dep->name)); | |
610 | break; | |
611 | case USB_ENDPOINT_XFER_BULK: | |
612 | strlcat(dep->name, "-bulk", sizeof(dep->name)); | |
613 | break; | |
614 | case USB_ENDPOINT_XFER_INT: | |
615 | strlcat(dep->name, "-int", sizeof(dep->name)); | |
616 | break; | |
617 | default: | |
618 | dev_err(dwc->dev, "invalid endpoint transfer type\n"); | |
619 | } | |
620 | ||
72246da4 FB |
621 | return 0; |
622 | } | |
623 | ||
b992e681 | 624 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force); |
624407f9 | 625 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) |
72246da4 FB |
626 | { |
627 | struct dwc3_request *req; | |
628 | ||
aa3342c8 | 629 | if (!list_empty(&dep->started_list)) { |
b992e681 | 630 | dwc3_stop_active_transfer(dwc, dep->number, true); |
624407f9 | 631 | |
57911504 | 632 | /* - giveback all requests to gadget driver */ |
aa3342c8 FB |
633 | while (!list_empty(&dep->started_list)) { |
634 | req = next_request(&dep->started_list); | |
1591633e PA |
635 | |
636 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); | |
637 | } | |
ea53b882 FB |
638 | } |
639 | ||
aa3342c8 FB |
640 | while (!list_empty(&dep->pending_list)) { |
641 | req = next_request(&dep->pending_list); | |
72246da4 | 642 | |
624407f9 | 643 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
72246da4 | 644 | } |
72246da4 FB |
645 | } |
646 | ||
647 | /** | |
648 | * __dwc3_gadget_ep_disable - Disables a HW endpoint | |
649 | * @dep: the endpoint to disable | |
650 | * | |
624407f9 SAS |
651 | * This function also removes requests which are currently processed ny the |
652 | * hardware and those which are not yet scheduled. | |
653 | * Caller should take care of locking. | |
72246da4 | 654 | */ |
72246da4 FB |
655 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) |
656 | { | |
657 | struct dwc3 *dwc = dep->dwc; | |
658 | u32 reg; | |
659 | ||
7eaeac5c FB |
660 | dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name); |
661 | ||
624407f9 | 662 | dwc3_remove_requests(dwc, dep); |
72246da4 | 663 | |
687ef981 FB |
664 | /* make sure HW endpoint isn't stalled */ |
665 | if (dep->flags & DWC3_EP_STALL) | |
7a608559 | 666 | __dwc3_gadget_ep_set_halt(dep, 0, false); |
687ef981 | 667 | |
72246da4 FB |
668 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
669 | reg &= ~DWC3_DALEPENA_EP(dep->number); | |
670 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); | |
671 | ||
879631aa | 672 | dep->stream_capable = false; |
f9c56cdd | 673 | dep->endpoint.desc = NULL; |
c90bfaec | 674 | dep->comp_desc = NULL; |
72246da4 | 675 | dep->type = 0; |
879631aa | 676 | dep->flags = 0; |
72246da4 | 677 | |
aa739974 FB |
678 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", |
679 | dep->number >> 1, | |
680 | (dep->number & 1) ? "in" : "out"); | |
681 | ||
72246da4 FB |
682 | return 0; |
683 | } | |
684 | ||
685 | /* -------------------------------------------------------------------------- */ | |
686 | ||
687 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, | |
688 | const struct usb_endpoint_descriptor *desc) | |
689 | { | |
690 | return -EINVAL; | |
691 | } | |
692 | ||
693 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) | |
694 | { | |
695 | return -EINVAL; | |
696 | } | |
697 | ||
698 | /* -------------------------------------------------------------------------- */ | |
699 | ||
700 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, | |
701 | const struct usb_endpoint_descriptor *desc) | |
702 | { | |
703 | struct dwc3_ep *dep; | |
704 | struct dwc3 *dwc; | |
705 | unsigned long flags; | |
706 | int ret; | |
707 | ||
708 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { | |
709 | pr_debug("dwc3: invalid parameters\n"); | |
710 | return -EINVAL; | |
711 | } | |
712 | ||
713 | if (!desc->wMaxPacketSize) { | |
714 | pr_debug("dwc3: missing wMaxPacketSize\n"); | |
715 | return -EINVAL; | |
716 | } | |
717 | ||
718 | dep = to_dwc3_ep(ep); | |
719 | dwc = dep->dwc; | |
720 | ||
95ca961c FB |
721 | if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, |
722 | "%s is already enabled\n", | |
723 | dep->name)) | |
c6f83f38 | 724 | return 0; |
c6f83f38 | 725 | |
72246da4 | 726 | spin_lock_irqsave(&dwc->lock, flags); |
265b70a7 | 727 | ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false); |
72246da4 FB |
728 | spin_unlock_irqrestore(&dwc->lock, flags); |
729 | ||
730 | return ret; | |
731 | } | |
732 | ||
733 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) | |
734 | { | |
735 | struct dwc3_ep *dep; | |
736 | struct dwc3 *dwc; | |
737 | unsigned long flags; | |
738 | int ret; | |
739 | ||
740 | if (!ep) { | |
741 | pr_debug("dwc3: invalid parameters\n"); | |
742 | return -EINVAL; | |
743 | } | |
744 | ||
745 | dep = to_dwc3_ep(ep); | |
746 | dwc = dep->dwc; | |
747 | ||
95ca961c FB |
748 | if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), |
749 | "%s is already disabled\n", | |
750 | dep->name)) | |
72246da4 | 751 | return 0; |
72246da4 | 752 | |
72246da4 FB |
753 | spin_lock_irqsave(&dwc->lock, flags); |
754 | ret = __dwc3_gadget_ep_disable(dep); | |
755 | spin_unlock_irqrestore(&dwc->lock, flags); | |
756 | ||
757 | return ret; | |
758 | } | |
759 | ||
760 | static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, | |
761 | gfp_t gfp_flags) | |
762 | { | |
763 | struct dwc3_request *req; | |
764 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
72246da4 FB |
765 | |
766 | req = kzalloc(sizeof(*req), gfp_flags); | |
734d5a53 | 767 | if (!req) |
72246da4 | 768 | return NULL; |
72246da4 FB |
769 | |
770 | req->epnum = dep->number; | |
771 | req->dep = dep; | |
72246da4 | 772 | |
2c4cbe6e FB |
773 | trace_dwc3_alloc_request(req); |
774 | ||
72246da4 FB |
775 | return &req->request; |
776 | } | |
777 | ||
778 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, | |
779 | struct usb_request *request) | |
780 | { | |
781 | struct dwc3_request *req = to_dwc3_request(request); | |
782 | ||
2c4cbe6e | 783 | trace_dwc3_free_request(req); |
72246da4 FB |
784 | kfree(req); |
785 | } | |
786 | ||
c71fc37c FB |
787 | /** |
788 | * dwc3_prepare_one_trb - setup one TRB from one request | |
789 | * @dep: endpoint for which this request is prepared | |
790 | * @req: dwc3_request pointer | |
791 | */ | |
68e823e2 | 792 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, |
eeb720fb | 793 | struct dwc3_request *req, dma_addr_t dma, |
e5ba5ec8 | 794 | unsigned length, unsigned last, unsigned chain, unsigned node) |
c71fc37c | 795 | { |
f6bafc6a | 796 | struct dwc3_trb *trb; |
c71fc37c | 797 | |
73815280 | 798 | dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s", |
eeb720fb FB |
799 | dep->name, req, (unsigned long long) dma, |
800 | length, last ? " last" : "", | |
801 | chain ? " chain" : ""); | |
802 | ||
915e202a | 803 | |
4faf7550 | 804 | trb = &dep->trb_pool[dep->trb_enqueue]; |
c71fc37c | 805 | |
eeb720fb | 806 | if (!req->trb) { |
aa3342c8 | 807 | dwc3_gadget_move_started_request(req); |
f6bafc6a FB |
808 | req->trb = trb; |
809 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); | |
4faf7550 | 810 | req->first_trb_index = dep->trb_enqueue; |
eeb720fb | 811 | } |
c71fc37c | 812 | |
ef966b9d | 813 | dwc3_ep_inc_enq(dep); |
36b68aae FB |
814 | /* Skip the LINK-TRB */ |
815 | if (dwc3_ep_is_last_trb(dep->trb_enqueue)) | |
ef966b9d | 816 | dwc3_ep_inc_enq(dep); |
e5ba5ec8 | 817 | |
f6bafc6a FB |
818 | trb->size = DWC3_TRB_SIZE_LENGTH(length); |
819 | trb->bpl = lower_32_bits(dma); | |
820 | trb->bph = upper_32_bits(dma); | |
c71fc37c | 821 | |
16e78db7 | 822 | switch (usb_endpoint_type(dep->endpoint.desc)) { |
c71fc37c | 823 | case USB_ENDPOINT_XFER_CONTROL: |
f6bafc6a | 824 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; |
c71fc37c FB |
825 | break; |
826 | ||
827 | case USB_ENDPOINT_XFER_ISOC: | |
e5ba5ec8 PA |
828 | if (!node) |
829 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; | |
830 | else | |
831 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; | |
ca4d44ea FB |
832 | |
833 | /* always enable Interrupt on Missed ISOC */ | |
834 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; | |
c71fc37c FB |
835 | break; |
836 | ||
837 | case USB_ENDPOINT_XFER_BULK: | |
838 | case USB_ENDPOINT_XFER_INT: | |
f6bafc6a | 839 | trb->ctrl = DWC3_TRBCTL_NORMAL; |
c71fc37c FB |
840 | break; |
841 | default: | |
842 | /* | |
843 | * This is only possible with faulty memory because we | |
844 | * checked it already :) | |
845 | */ | |
846 | BUG(); | |
847 | } | |
848 | ||
ca4d44ea FB |
849 | /* always enable Continue on Short Packet */ |
850 | trb->ctrl |= DWC3_TRB_CTRL_CSP; | |
f3af3651 | 851 | |
f3af3651 | 852 | if (!req->request.no_interrupt && !chain) |
ca4d44ea | 853 | trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI; |
f3af3651 | 854 | |
ca4d44ea | 855 | if (last) |
e5ba5ec8 | 856 | trb->ctrl |= DWC3_TRB_CTRL_LST; |
c71fc37c | 857 | |
e5ba5ec8 PA |
858 | if (chain) |
859 | trb->ctrl |= DWC3_TRB_CTRL_CHN; | |
860 | ||
16e78db7 | 861 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) |
f6bafc6a | 862 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id); |
c71fc37c | 863 | |
f6bafc6a | 864 | trb->ctrl |= DWC3_TRB_CTRL_HWO; |
2c4cbe6e FB |
865 | |
866 | trace_dwc3_prepare_trb(dep, trb); | |
c71fc37c FB |
867 | } |
868 | ||
c4233573 FB |
869 | static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) |
870 | { | |
871 | struct dwc3_trb *tmp; | |
872 | ||
873 | /* | |
874 | * If enqueue & dequeue are equal than it is either full or empty. | |
875 | * | |
876 | * One way to know for sure is if the TRB right before us has HWO bit | |
877 | * set or not. If it has, then we're definitely full and can't fit any | |
878 | * more transfers in our ring. | |
879 | */ | |
880 | if (dep->trb_enqueue == dep->trb_dequeue) { | |
881 | /* If we're full, enqueue/dequeue are > 0 */ | |
882 | if (dep->trb_enqueue) { | |
883 | tmp = &dep->trb_pool[dep->trb_enqueue - 1]; | |
884 | if (tmp->ctrl & DWC3_TRB_CTRL_HWO) | |
885 | return 0; | |
886 | } | |
887 | ||
888 | return DWC3_TRB_NUM - 1; | |
889 | } | |
890 | ||
891 | return dep->trb_dequeue - dep->trb_enqueue; | |
892 | } | |
893 | ||
5ee85d89 FB |
894 | static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep, |
895 | struct dwc3_request *req, unsigned int trbs_left) | |
896 | { | |
897 | struct usb_request *request = &req->request; | |
898 | struct scatterlist *sg = request->sg; | |
899 | struct scatterlist *s; | |
900 | unsigned int last = false; | |
901 | unsigned int length; | |
902 | dma_addr_t dma; | |
903 | int i; | |
904 | ||
905 | for_each_sg(sg, s, request->num_mapped_sgs, i) { | |
906 | unsigned chain = true; | |
907 | ||
908 | length = sg_dma_len(s); | |
909 | dma = sg_dma_address(s); | |
910 | ||
911 | if (sg_is_last(s)) { | |
912 | if (list_is_last(&req->list, &dep->pending_list)) | |
913 | last = true; | |
914 | ||
915 | chain = false; | |
916 | } | |
917 | ||
918 | if (!trbs_left) | |
919 | last = true; | |
920 | ||
921 | if (last) | |
922 | chain = false; | |
923 | ||
924 | dwc3_prepare_one_trb(dep, req, dma, length, | |
925 | last, chain, i); | |
926 | ||
927 | if (last) | |
928 | break; | |
929 | } | |
930 | } | |
931 | ||
932 | static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep, | |
933 | struct dwc3_request *req, unsigned int trbs_left) | |
934 | { | |
935 | unsigned int last = false; | |
936 | unsigned int length; | |
937 | dma_addr_t dma; | |
938 | ||
939 | dma = req->request.dma; | |
940 | length = req->request.length; | |
941 | ||
942 | if (!trbs_left) | |
943 | last = true; | |
944 | ||
945 | /* Is this the last request? */ | |
946 | if (list_is_last(&req->list, &dep->pending_list)) | |
947 | last = true; | |
948 | ||
949 | dwc3_prepare_one_trb(dep, req, dma, length, | |
950 | last, false, 0); | |
951 | } | |
952 | ||
72246da4 FB |
953 | /* |
954 | * dwc3_prepare_trbs - setup TRBs from requests | |
955 | * @dep: endpoint for which requests are being prepared | |
72246da4 | 956 | * |
1d046793 PZ |
957 | * The function goes through the requests list and sets up TRBs for the |
958 | * transfers. The function returns once there are no more TRBs available or | |
959 | * it runs out of requests. | |
72246da4 | 960 | */ |
c4233573 | 961 | static void dwc3_prepare_trbs(struct dwc3_ep *dep) |
72246da4 | 962 | { |
68e823e2 | 963 | struct dwc3_request *req, *n; |
72246da4 FB |
964 | u32 trbs_left; |
965 | ||
966 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); | |
967 | ||
c4233573 | 968 | trbs_left = dwc3_calc_trbs_left(dep); |
72246da4 | 969 | |
aa3342c8 | 970 | list_for_each_entry_safe(req, n, &dep->pending_list, list) { |
5ee85d89 FB |
971 | if (req->request.num_mapped_sgs > 0) |
972 | dwc3_prepare_one_trb_sg(dep, req, trbs_left--); | |
973 | else | |
974 | dwc3_prepare_one_trb_linear(dep, req, trbs_left--); | |
72246da4 | 975 | |
5ee85d89 FB |
976 | if (!trbs_left) |
977 | return; | |
72246da4 | 978 | } |
72246da4 FB |
979 | } |
980 | ||
4fae2e3e | 981 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param) |
72246da4 FB |
982 | { |
983 | struct dwc3_gadget_ep_cmd_params params; | |
984 | struct dwc3_request *req; | |
985 | struct dwc3 *dwc = dep->dwc; | |
4fae2e3e | 986 | int starting; |
72246da4 FB |
987 | int ret; |
988 | u32 cmd; | |
989 | ||
4fae2e3e | 990 | starting = !(dep->flags & DWC3_EP_BUSY); |
72246da4 | 991 | |
4fae2e3e FB |
992 | dwc3_prepare_trbs(dep); |
993 | req = next_request(&dep->started_list); | |
72246da4 FB |
994 | if (!req) { |
995 | dep->flags |= DWC3_EP_PENDING_REQUEST; | |
996 | return 0; | |
997 | } | |
998 | ||
999 | memset(¶ms, 0, sizeof(params)); | |
72246da4 | 1000 | |
4fae2e3e | 1001 | if (starting) { |
1877d6c9 PA |
1002 | params.param0 = upper_32_bits(req->trb_dma); |
1003 | params.param1 = lower_32_bits(req->trb_dma); | |
72246da4 | 1004 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
1877d6c9 | 1005 | } else { |
72246da4 | 1006 | cmd = DWC3_DEPCMD_UPDATETRANSFER; |
1877d6c9 | 1007 | } |
72246da4 FB |
1008 | |
1009 | cmd |= DWC3_DEPCMD_PARAM(cmd_param); | |
2cd4718d | 1010 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
72246da4 | 1011 | if (ret < 0) { |
72246da4 FB |
1012 | /* |
1013 | * FIXME we need to iterate over the list of requests | |
1014 | * here and stop, unmap, free and del each of the linked | |
1d046793 | 1015 | * requests instead of what we do now. |
72246da4 | 1016 | */ |
0fc9a1be FB |
1017 | usb_gadget_unmap_request(&dwc->gadget, &req->request, |
1018 | req->direction); | |
72246da4 FB |
1019 | list_del(&req->list); |
1020 | return ret; | |
1021 | } | |
1022 | ||
1023 | dep->flags |= DWC3_EP_BUSY; | |
25b8ff68 | 1024 | |
4fae2e3e | 1025 | if (starting) { |
2eb88016 | 1026 | dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep); |
b4996a86 | 1027 | WARN_ON_ONCE(!dep->resource_index); |
f898ae09 | 1028 | } |
25b8ff68 | 1029 | |
72246da4 FB |
1030 | return 0; |
1031 | } | |
1032 | ||
d6d6ec7b PA |
1033 | static void __dwc3_gadget_start_isoc(struct dwc3 *dwc, |
1034 | struct dwc3_ep *dep, u32 cur_uf) | |
1035 | { | |
1036 | u32 uf; | |
1037 | ||
aa3342c8 | 1038 | if (list_empty(&dep->pending_list)) { |
73815280 FB |
1039 | dwc3_trace(trace_dwc3_gadget, |
1040 | "ISOC ep %s run out for requests", | |
1041 | dep->name); | |
f4a53c55 | 1042 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
d6d6ec7b PA |
1043 | return; |
1044 | } | |
1045 | ||
1046 | /* 4 micro frames in the future */ | |
1047 | uf = cur_uf + dep->interval * 4; | |
1048 | ||
4fae2e3e | 1049 | __dwc3_gadget_kick_transfer(dep, uf); |
d6d6ec7b PA |
1050 | } |
1051 | ||
1052 | static void dwc3_gadget_start_isoc(struct dwc3 *dwc, | |
1053 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) | |
1054 | { | |
1055 | u32 cur_uf, mask; | |
1056 | ||
1057 | mask = ~(dep->interval - 1); | |
1058 | cur_uf = event->parameters & mask; | |
1059 | ||
1060 | __dwc3_gadget_start_isoc(dwc, dep, cur_uf); | |
1061 | } | |
1062 | ||
72246da4 FB |
1063 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
1064 | { | |
0fc9a1be FB |
1065 | struct dwc3 *dwc = dep->dwc; |
1066 | int ret; | |
1067 | ||
bb423984 | 1068 | if (!dep->endpoint.desc) { |
ec5e795c FB |
1069 | dwc3_trace(trace_dwc3_gadget, |
1070 | "trying to queue request %p to disabled %s\n", | |
bb423984 FB |
1071 | &req->request, dep->endpoint.name); |
1072 | return -ESHUTDOWN; | |
1073 | } | |
1074 | ||
1075 | if (WARN(req->dep != dep, "request %p belongs to '%s'\n", | |
1076 | &req->request, req->dep->name)) { | |
ec5e795c FB |
1077 | dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n", |
1078 | &req->request, req->dep->name); | |
bb423984 FB |
1079 | return -EINVAL; |
1080 | } | |
1081 | ||
72246da4 FB |
1082 | req->request.actual = 0; |
1083 | req->request.status = -EINPROGRESS; | |
1084 | req->direction = dep->direction; | |
1085 | req->epnum = dep->number; | |
1086 | ||
fe84f522 FB |
1087 | trace_dwc3_ep_queue(req); |
1088 | ||
72246da4 FB |
1089 | /* |
1090 | * We only add to our list of requests now and | |
1091 | * start consuming the list once we get XferNotReady | |
1092 | * IRQ. | |
1093 | * | |
1094 | * That way, we avoid doing anything that we don't need | |
1095 | * to do now and defer it until the point we receive a | |
1096 | * particular token from the Host side. | |
1097 | * | |
1098 | * This will also avoid Host cancelling URBs due to too | |
1d046793 | 1099 | * many NAKs. |
72246da4 | 1100 | */ |
0fc9a1be FB |
1101 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
1102 | dep->direction); | |
1103 | if (ret) | |
1104 | return ret; | |
1105 | ||
aa3342c8 | 1106 | list_add_tail(&req->list, &dep->pending_list); |
72246da4 | 1107 | |
1d6a3918 FB |
1108 | /* |
1109 | * If there are no pending requests and the endpoint isn't already | |
1110 | * busy, we will just start the request straight away. | |
1111 | * | |
1112 | * This will save one IRQ (XFER_NOT_READY) and possibly make it a | |
1113 | * little bit faster. | |
1114 | */ | |
1115 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) && | |
62e345ae | 1116 | !usb_endpoint_xfer_int(dep->endpoint.desc) && |
1d6a3918 | 1117 | !(dep->flags & DWC3_EP_BUSY)) { |
4fae2e3e | 1118 | ret = __dwc3_gadget_kick_transfer(dep, 0); |
a8f32817 | 1119 | goto out; |
1d6a3918 FB |
1120 | } |
1121 | ||
72246da4 | 1122 | /* |
b511e5e7 | 1123 | * There are a few special cases: |
72246da4 | 1124 | * |
f898ae09 PZ |
1125 | * 1. XferNotReady with empty list of requests. We need to kick the |
1126 | * transfer here in that situation, otherwise we will be NAKing | |
1127 | * forever. If we get XferNotReady before gadget driver has a | |
1128 | * chance to queue a request, we will ACK the IRQ but won't be | |
1129 | * able to receive the data until the next request is queued. | |
1130 | * The following code is handling exactly that. | |
72246da4 | 1131 | * |
72246da4 FB |
1132 | */ |
1133 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { | |
f4a53c55 PA |
1134 | /* |
1135 | * If xfernotready is already elapsed and it is a case | |
1136 | * of isoc transfer, then issue END TRANSFER, so that | |
1137 | * you can receive xfernotready again and can have | |
1138 | * notion of current microframe. | |
1139 | */ | |
1140 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { | |
aa3342c8 | 1141 | if (list_empty(&dep->started_list)) { |
b992e681 | 1142 | dwc3_stop_active_transfer(dwc, dep->number, true); |
cdc359dd PA |
1143 | dep->flags = DWC3_EP_ENABLED; |
1144 | } | |
f4a53c55 PA |
1145 | return 0; |
1146 | } | |
1147 | ||
4fae2e3e | 1148 | ret = __dwc3_gadget_kick_transfer(dep, 0); |
89185916 FB |
1149 | if (!ret) |
1150 | dep->flags &= ~DWC3_EP_PENDING_REQUEST; | |
1151 | ||
a8f32817 | 1152 | goto out; |
b511e5e7 | 1153 | } |
72246da4 | 1154 | |
b511e5e7 FB |
1155 | /* |
1156 | * 2. XferInProgress on Isoc EP with an active transfer. We need to | |
1157 | * kick the transfer here after queuing a request, otherwise the | |
1158 | * core may not see the modified TRB(s). | |
1159 | */ | |
1160 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && | |
79c9046e PA |
1161 | (dep->flags & DWC3_EP_BUSY) && |
1162 | !(dep->flags & DWC3_EP_MISSED_ISOC)) { | |
b4996a86 | 1163 | WARN_ON_ONCE(!dep->resource_index); |
4fae2e3e | 1164 | ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index); |
a8f32817 | 1165 | goto out; |
a0925324 | 1166 | } |
72246da4 | 1167 | |
b997ada5 FB |
1168 | /* |
1169 | * 4. Stream Capable Bulk Endpoints. We need to start the transfer | |
1170 | * right away, otherwise host will not know we have streams to be | |
1171 | * handled. | |
1172 | */ | |
a8f32817 | 1173 | if (dep->stream_capable) |
4fae2e3e | 1174 | ret = __dwc3_gadget_kick_transfer(dep, 0); |
b997ada5 | 1175 | |
a8f32817 FB |
1176 | out: |
1177 | if (ret && ret != -EBUSY) | |
ec5e795c FB |
1178 | dwc3_trace(trace_dwc3_gadget, |
1179 | "%s: failed to kick transfers\n", | |
a8f32817 FB |
1180 | dep->name); |
1181 | if (ret == -EBUSY) | |
1182 | ret = 0; | |
1183 | ||
1184 | return ret; | |
72246da4 FB |
1185 | } |
1186 | ||
04c03d10 FB |
1187 | static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep, |
1188 | struct usb_request *request) | |
1189 | { | |
1190 | dwc3_gadget_ep_free_request(ep, request); | |
1191 | } | |
1192 | ||
1193 | static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep) | |
1194 | { | |
1195 | struct dwc3_request *req; | |
1196 | struct usb_request *request; | |
1197 | struct usb_ep *ep = &dep->endpoint; | |
1198 | ||
1199 | dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n"); | |
1200 | request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC); | |
1201 | if (!request) | |
1202 | return -ENOMEM; | |
1203 | ||
1204 | request->length = 0; | |
1205 | request->buf = dwc->zlp_buf; | |
1206 | request->complete = __dwc3_gadget_ep_zlp_complete; | |
1207 | ||
1208 | req = to_dwc3_request(request); | |
1209 | ||
1210 | return __dwc3_gadget_ep_queue(dep, req); | |
1211 | } | |
1212 | ||
72246da4 FB |
1213 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, |
1214 | gfp_t gfp_flags) | |
1215 | { | |
1216 | struct dwc3_request *req = to_dwc3_request(request); | |
1217 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
1218 | struct dwc3 *dwc = dep->dwc; | |
1219 | ||
1220 | unsigned long flags; | |
1221 | ||
1222 | int ret; | |
1223 | ||
fdee4eba | 1224 | spin_lock_irqsave(&dwc->lock, flags); |
72246da4 | 1225 | ret = __dwc3_gadget_ep_queue(dep, req); |
04c03d10 FB |
1226 | |
1227 | /* | |
1228 | * Okay, here's the thing, if gadget driver has requested for a ZLP by | |
1229 | * setting request->zero, instead of doing magic, we will just queue an | |
1230 | * extra usb_request ourselves so that it gets handled the same way as | |
1231 | * any other request. | |
1232 | */ | |
d9261898 JY |
1233 | if (ret == 0 && request->zero && request->length && |
1234 | (request->length % ep->maxpacket == 0)) | |
04c03d10 FB |
1235 | ret = __dwc3_gadget_ep_queue_zlp(dwc, dep); |
1236 | ||
72246da4 FB |
1237 | spin_unlock_irqrestore(&dwc->lock, flags); |
1238 | ||
1239 | return ret; | |
1240 | } | |
1241 | ||
1242 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, | |
1243 | struct usb_request *request) | |
1244 | { | |
1245 | struct dwc3_request *req = to_dwc3_request(request); | |
1246 | struct dwc3_request *r = NULL; | |
1247 | ||
1248 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
1249 | struct dwc3 *dwc = dep->dwc; | |
1250 | ||
1251 | unsigned long flags; | |
1252 | int ret = 0; | |
1253 | ||
2c4cbe6e FB |
1254 | trace_dwc3_ep_dequeue(req); |
1255 | ||
72246da4 FB |
1256 | spin_lock_irqsave(&dwc->lock, flags); |
1257 | ||
aa3342c8 | 1258 | list_for_each_entry(r, &dep->pending_list, list) { |
72246da4 FB |
1259 | if (r == req) |
1260 | break; | |
1261 | } | |
1262 | ||
1263 | if (r != req) { | |
aa3342c8 | 1264 | list_for_each_entry(r, &dep->started_list, list) { |
72246da4 FB |
1265 | if (r == req) |
1266 | break; | |
1267 | } | |
1268 | if (r == req) { | |
1269 | /* wait until it is processed */ | |
b992e681 | 1270 | dwc3_stop_active_transfer(dwc, dep->number, true); |
e8d4e8be | 1271 | goto out1; |
72246da4 FB |
1272 | } |
1273 | dev_err(dwc->dev, "request %p was not queued to %s\n", | |
1274 | request, ep->name); | |
1275 | ret = -EINVAL; | |
1276 | goto out0; | |
1277 | } | |
1278 | ||
e8d4e8be | 1279 | out1: |
72246da4 FB |
1280 | /* giveback the request */ |
1281 | dwc3_gadget_giveback(dep, req, -ECONNRESET); | |
1282 | ||
1283 | out0: | |
1284 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1285 | ||
1286 | return ret; | |
1287 | } | |
1288 | ||
7a608559 | 1289 | int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) |
72246da4 FB |
1290 | { |
1291 | struct dwc3_gadget_ep_cmd_params params; | |
1292 | struct dwc3 *dwc = dep->dwc; | |
1293 | int ret; | |
1294 | ||
5ad02fb8 FB |
1295 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
1296 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); | |
1297 | return -EINVAL; | |
1298 | } | |
1299 | ||
72246da4 FB |
1300 | memset(¶ms, 0x00, sizeof(params)); |
1301 | ||
1302 | if (value) { | |
7a608559 | 1303 | if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) || |
aa3342c8 FB |
1304 | (!list_empty(&dep->started_list) || |
1305 | !list_empty(&dep->pending_list)))) { | |
ec5e795c | 1306 | dwc3_trace(trace_dwc3_gadget, |
052ba52e | 1307 | "%s: pending request, cannot halt", |
7a608559 FB |
1308 | dep->name); |
1309 | return -EAGAIN; | |
1310 | } | |
1311 | ||
2cd4718d FB |
1312 | ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, |
1313 | ¶ms); | |
72246da4 | 1314 | if (ret) |
3f89204b | 1315 | dev_err(dwc->dev, "failed to set STALL on %s\n", |
72246da4 FB |
1316 | dep->name); |
1317 | else | |
1318 | dep->flags |= DWC3_EP_STALL; | |
1319 | } else { | |
2cd4718d | 1320 | |
50c763f8 | 1321 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
72246da4 | 1322 | if (ret) |
3f89204b | 1323 | dev_err(dwc->dev, "failed to clear STALL on %s\n", |
72246da4 FB |
1324 | dep->name); |
1325 | else | |
a535d81c | 1326 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
72246da4 | 1327 | } |
5275455a | 1328 | |
72246da4 FB |
1329 | return ret; |
1330 | } | |
1331 | ||
1332 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) | |
1333 | { | |
1334 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
1335 | struct dwc3 *dwc = dep->dwc; | |
1336 | ||
1337 | unsigned long flags; | |
1338 | ||
1339 | int ret; | |
1340 | ||
1341 | spin_lock_irqsave(&dwc->lock, flags); | |
7a608559 | 1342 | ret = __dwc3_gadget_ep_set_halt(dep, value, false); |
72246da4 FB |
1343 | spin_unlock_irqrestore(&dwc->lock, flags); |
1344 | ||
1345 | return ret; | |
1346 | } | |
1347 | ||
1348 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) | |
1349 | { | |
1350 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
249a4569 PZ |
1351 | struct dwc3 *dwc = dep->dwc; |
1352 | unsigned long flags; | |
95aa4e8d | 1353 | int ret; |
72246da4 | 1354 | |
249a4569 | 1355 | spin_lock_irqsave(&dwc->lock, flags); |
72246da4 FB |
1356 | dep->flags |= DWC3_EP_WEDGE; |
1357 | ||
08f0d966 | 1358 | if (dep->number == 0 || dep->number == 1) |
95aa4e8d | 1359 | ret = __dwc3_gadget_ep0_set_halt(ep, 1); |
08f0d966 | 1360 | else |
7a608559 | 1361 | ret = __dwc3_gadget_ep_set_halt(dep, 1, false); |
95aa4e8d FB |
1362 | spin_unlock_irqrestore(&dwc->lock, flags); |
1363 | ||
1364 | return ret; | |
72246da4 FB |
1365 | } |
1366 | ||
1367 | /* -------------------------------------------------------------------------- */ | |
1368 | ||
1369 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { | |
1370 | .bLength = USB_DT_ENDPOINT_SIZE, | |
1371 | .bDescriptorType = USB_DT_ENDPOINT, | |
1372 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, | |
1373 | }; | |
1374 | ||
1375 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { | |
1376 | .enable = dwc3_gadget_ep0_enable, | |
1377 | .disable = dwc3_gadget_ep0_disable, | |
1378 | .alloc_request = dwc3_gadget_ep_alloc_request, | |
1379 | .free_request = dwc3_gadget_ep_free_request, | |
1380 | .queue = dwc3_gadget_ep0_queue, | |
1381 | .dequeue = dwc3_gadget_ep_dequeue, | |
08f0d966 | 1382 | .set_halt = dwc3_gadget_ep0_set_halt, |
72246da4 FB |
1383 | .set_wedge = dwc3_gadget_ep_set_wedge, |
1384 | }; | |
1385 | ||
1386 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { | |
1387 | .enable = dwc3_gadget_ep_enable, | |
1388 | .disable = dwc3_gadget_ep_disable, | |
1389 | .alloc_request = dwc3_gadget_ep_alloc_request, | |
1390 | .free_request = dwc3_gadget_ep_free_request, | |
1391 | .queue = dwc3_gadget_ep_queue, | |
1392 | .dequeue = dwc3_gadget_ep_dequeue, | |
1393 | .set_halt = dwc3_gadget_ep_set_halt, | |
1394 | .set_wedge = dwc3_gadget_ep_set_wedge, | |
1395 | }; | |
1396 | ||
1397 | /* -------------------------------------------------------------------------- */ | |
1398 | ||
1399 | static int dwc3_gadget_get_frame(struct usb_gadget *g) | |
1400 | { | |
1401 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1402 | u32 reg; | |
1403 | ||
1404 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
1405 | return DWC3_DSTS_SOFFN(reg); | |
1406 | } | |
1407 | ||
218ef7b6 | 1408 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc) |
72246da4 | 1409 | { |
72246da4 | 1410 | unsigned long timeout; |
72246da4 | 1411 | |
218ef7b6 | 1412 | int ret; |
72246da4 FB |
1413 | u32 reg; |
1414 | ||
72246da4 FB |
1415 | u8 link_state; |
1416 | u8 speed; | |
1417 | ||
72246da4 FB |
1418 | /* |
1419 | * According to the Databook Remote wakeup request should | |
1420 | * be issued only when the device is in early suspend state. | |
1421 | * | |
1422 | * We can check that via USB Link State bits in DSTS register. | |
1423 | */ | |
1424 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
1425 | ||
1426 | speed = reg & DWC3_DSTS_CONNECTSPD; | |
ee5cd41c JY |
1427 | if ((speed == DWC3_DSTS_SUPERSPEED) || |
1428 | (speed == DWC3_DSTS_SUPERSPEED_PLUS)) { | |
ec5e795c | 1429 | dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n"); |
6b742899 | 1430 | return 0; |
72246da4 FB |
1431 | } |
1432 | ||
1433 | link_state = DWC3_DSTS_USBLNKST(reg); | |
1434 | ||
1435 | switch (link_state) { | |
1436 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ | |
1437 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ | |
1438 | break; | |
1439 | default: | |
ec5e795c FB |
1440 | dwc3_trace(trace_dwc3_gadget, |
1441 | "can't wakeup from '%s'\n", | |
1442 | dwc3_gadget_link_string(link_state)); | |
218ef7b6 | 1443 | return -EINVAL; |
72246da4 FB |
1444 | } |
1445 | ||
8598bde7 FB |
1446 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
1447 | if (ret < 0) { | |
1448 | dev_err(dwc->dev, "failed to put link in Recovery\n"); | |
218ef7b6 | 1449 | return ret; |
8598bde7 | 1450 | } |
72246da4 | 1451 | |
802fde98 PZ |
1452 | /* Recent versions do this automatically */ |
1453 | if (dwc->revision < DWC3_REVISION_194A) { | |
1454 | /* write zeroes to Link Change Request */ | |
fcc023c7 | 1455 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
802fde98 PZ |
1456 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
1457 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
1458 | } | |
72246da4 | 1459 | |
1d046793 | 1460 | /* poll until Link State changes to ON */ |
72246da4 FB |
1461 | timeout = jiffies + msecs_to_jiffies(100); |
1462 | ||
1d046793 | 1463 | while (!time_after(jiffies, timeout)) { |
72246da4 FB |
1464 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
1465 | ||
1466 | /* in HS, means ON */ | |
1467 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) | |
1468 | break; | |
1469 | } | |
1470 | ||
1471 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { | |
1472 | dev_err(dwc->dev, "failed to send remote wakeup\n"); | |
218ef7b6 | 1473 | return -EINVAL; |
72246da4 FB |
1474 | } |
1475 | ||
218ef7b6 FB |
1476 | return 0; |
1477 | } | |
1478 | ||
1479 | static int dwc3_gadget_wakeup(struct usb_gadget *g) | |
1480 | { | |
1481 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1482 | unsigned long flags; | |
1483 | int ret; | |
1484 | ||
1485 | spin_lock_irqsave(&dwc->lock, flags); | |
1486 | ret = __dwc3_gadget_wakeup(dwc); | |
72246da4 FB |
1487 | spin_unlock_irqrestore(&dwc->lock, flags); |
1488 | ||
1489 | return ret; | |
1490 | } | |
1491 | ||
1492 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, | |
1493 | int is_selfpowered) | |
1494 | { | |
1495 | struct dwc3 *dwc = gadget_to_dwc(g); | |
249a4569 | 1496 | unsigned long flags; |
72246da4 | 1497 | |
249a4569 | 1498 | spin_lock_irqsave(&dwc->lock, flags); |
bcdea503 | 1499 | g->is_selfpowered = !!is_selfpowered; |
249a4569 | 1500 | spin_unlock_irqrestore(&dwc->lock, flags); |
72246da4 FB |
1501 | |
1502 | return 0; | |
1503 | } | |
1504 | ||
7b2a0368 | 1505 | static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) |
72246da4 FB |
1506 | { |
1507 | u32 reg; | |
61d58242 | 1508 | u32 timeout = 500; |
72246da4 FB |
1509 | |
1510 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
8db7ed15 | 1511 | if (is_on) { |
802fde98 PZ |
1512 | if (dwc->revision <= DWC3_REVISION_187A) { |
1513 | reg &= ~DWC3_DCTL_TRGTULST_MASK; | |
1514 | reg |= DWC3_DCTL_TRGTULST_RX_DET; | |
1515 | } | |
1516 | ||
1517 | if (dwc->revision >= DWC3_REVISION_194A) | |
1518 | reg &= ~DWC3_DCTL_KEEP_CONNECT; | |
1519 | reg |= DWC3_DCTL_RUN_STOP; | |
7b2a0368 FB |
1520 | |
1521 | if (dwc->has_hibernation) | |
1522 | reg |= DWC3_DCTL_KEEP_CONNECT; | |
1523 | ||
9fcb3bd8 | 1524 | dwc->pullups_connected = true; |
8db7ed15 | 1525 | } else { |
72246da4 | 1526 | reg &= ~DWC3_DCTL_RUN_STOP; |
7b2a0368 FB |
1527 | |
1528 | if (dwc->has_hibernation && !suspend) | |
1529 | reg &= ~DWC3_DCTL_KEEP_CONNECT; | |
1530 | ||
9fcb3bd8 | 1531 | dwc->pullups_connected = false; |
8db7ed15 | 1532 | } |
72246da4 FB |
1533 | |
1534 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
1535 | ||
1536 | do { | |
1537 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
1538 | if (is_on) { | |
1539 | if (!(reg & DWC3_DSTS_DEVCTRLHLT)) | |
1540 | break; | |
1541 | } else { | |
1542 | if (reg & DWC3_DSTS_DEVCTRLHLT) | |
1543 | break; | |
1544 | } | |
72246da4 FB |
1545 | timeout--; |
1546 | if (!timeout) | |
6f17f74b | 1547 | return -ETIMEDOUT; |
61d58242 | 1548 | udelay(1); |
72246da4 FB |
1549 | } while (1); |
1550 | ||
73815280 | 1551 | dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s", |
72246da4 FB |
1552 | dwc->gadget_driver |
1553 | ? dwc->gadget_driver->function : "no-function", | |
1554 | is_on ? "connect" : "disconnect"); | |
6f17f74b PA |
1555 | |
1556 | return 0; | |
72246da4 FB |
1557 | } |
1558 | ||
1559 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) | |
1560 | { | |
1561 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1562 | unsigned long flags; | |
6f17f74b | 1563 | int ret; |
72246da4 FB |
1564 | |
1565 | is_on = !!is_on; | |
1566 | ||
1567 | spin_lock_irqsave(&dwc->lock, flags); | |
7b2a0368 | 1568 | ret = dwc3_gadget_run_stop(dwc, is_on, false); |
72246da4 FB |
1569 | spin_unlock_irqrestore(&dwc->lock, flags); |
1570 | ||
6f17f74b | 1571 | return ret; |
72246da4 FB |
1572 | } |
1573 | ||
8698e2ac FB |
1574 | static void dwc3_gadget_enable_irq(struct dwc3 *dwc) |
1575 | { | |
1576 | u32 reg; | |
1577 | ||
1578 | /* Enable all but Start and End of Frame IRQs */ | |
1579 | reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | | |
1580 | DWC3_DEVTEN_EVNTOVERFLOWEN | | |
1581 | DWC3_DEVTEN_CMDCMPLTEN | | |
1582 | DWC3_DEVTEN_ERRTICERREN | | |
1583 | DWC3_DEVTEN_WKUPEVTEN | | |
1584 | DWC3_DEVTEN_ULSTCNGEN | | |
1585 | DWC3_DEVTEN_CONNECTDONEEN | | |
1586 | DWC3_DEVTEN_USBRSTEN | | |
1587 | DWC3_DEVTEN_DISCONNEVTEN); | |
1588 | ||
1589 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); | |
1590 | } | |
1591 | ||
1592 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc) | |
1593 | { | |
1594 | /* mask all interrupts */ | |
1595 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); | |
1596 | } | |
1597 | ||
1598 | static irqreturn_t dwc3_interrupt(int irq, void *_dwc); | |
b15a762f | 1599 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); |
8698e2ac | 1600 | |
4e99472b FB |
1601 | /** |
1602 | * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG | |
1603 | * dwc: pointer to our context structure | |
1604 | * | |
1605 | * The following looks like complex but it's actually very simple. In order to | |
1606 | * calculate the number of packets we can burst at once on OUT transfers, we're | |
1607 | * gonna use RxFIFO size. | |
1608 | * | |
1609 | * To calculate RxFIFO size we need two numbers: | |
1610 | * MDWIDTH = size, in bits, of the internal memory bus | |
1611 | * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) | |
1612 | * | |
1613 | * Given these two numbers, the formula is simple: | |
1614 | * | |
1615 | * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; | |
1616 | * | |
1617 | * 24 bytes is for 3x SETUP packets | |
1618 | * 16 bytes is a clock domain crossing tolerance | |
1619 | * | |
1620 | * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; | |
1621 | */ | |
1622 | static void dwc3_gadget_setup_nump(struct dwc3 *dwc) | |
1623 | { | |
1624 | u32 ram2_depth; | |
1625 | u32 mdwidth; | |
1626 | u32 nump; | |
1627 | u32 reg; | |
1628 | ||
1629 | ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); | |
1630 | mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0); | |
1631 | ||
1632 | nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; | |
1633 | nump = min_t(u32, nump, 16); | |
1634 | ||
1635 | /* update NumP */ | |
1636 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
1637 | reg &= ~DWC3_DCFG_NUMP_MASK; | |
1638 | reg |= nump << DWC3_DCFG_NUMP_SHIFT; | |
1639 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
1640 | } | |
1641 | ||
d7be2952 | 1642 | static int __dwc3_gadget_start(struct dwc3 *dwc) |
72246da4 | 1643 | { |
72246da4 | 1644 | struct dwc3_ep *dep; |
72246da4 FB |
1645 | int ret = 0; |
1646 | u32 reg; | |
1647 | ||
72246da4 FB |
1648 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
1649 | reg &= ~(DWC3_DCFG_SPEED_MASK); | |
07e7f47b FB |
1650 | |
1651 | /** | |
1652 | * WORKAROUND: DWC3 revision < 2.20a have an issue | |
1653 | * which would cause metastability state on Run/Stop | |
1654 | * bit if we try to force the IP to USB2-only mode. | |
1655 | * | |
1656 | * Because of that, we cannot configure the IP to any | |
1657 | * speed other than the SuperSpeed | |
1658 | * | |
1659 | * Refers to: | |
1660 | * | |
1661 | * STAR#9000525659: Clock Domain Crossing on DCTL in | |
1662 | * USB 2.0 Mode | |
1663 | */ | |
f7e846f0 | 1664 | if (dwc->revision < DWC3_REVISION_220A) { |
07e7f47b | 1665 | reg |= DWC3_DCFG_SUPERSPEED; |
f7e846f0 FB |
1666 | } else { |
1667 | switch (dwc->maximum_speed) { | |
1668 | case USB_SPEED_LOW: | |
1669 | reg |= DWC3_DSTS_LOWSPEED; | |
1670 | break; | |
1671 | case USB_SPEED_FULL: | |
1672 | reg |= DWC3_DSTS_FULLSPEED1; | |
1673 | break; | |
1674 | case USB_SPEED_HIGH: | |
1675 | reg |= DWC3_DSTS_HIGHSPEED; | |
1676 | break; | |
7580862b JY |
1677 | case USB_SPEED_SUPER_PLUS: |
1678 | reg |= DWC3_DSTS_SUPERSPEED_PLUS; | |
1679 | break; | |
f7e846f0 | 1680 | default: |
77966eb8 JY |
1681 | dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n", |
1682 | dwc->maximum_speed); | |
1683 | /* fall through */ | |
1684 | case USB_SPEED_SUPER: | |
1685 | reg |= DWC3_DCFG_SUPERSPEED; | |
1686 | break; | |
f7e846f0 FB |
1687 | } |
1688 | } | |
72246da4 FB |
1689 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
1690 | ||
2a58f9c1 FB |
1691 | /* |
1692 | * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP | |
1693 | * field instead of letting dwc3 itself calculate that automatically. | |
1694 | * | |
1695 | * This way, we maximize the chances that we'll be able to get several | |
1696 | * bursts of data without going through any sort of endpoint throttling. | |
1697 | */ | |
1698 | reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); | |
1699 | reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; | |
1700 | dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); | |
1701 | ||
4e99472b FB |
1702 | dwc3_gadget_setup_nump(dwc); |
1703 | ||
72246da4 FB |
1704 | /* Start with SuperSpeed Default */ |
1705 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); | |
1706 | ||
1707 | dep = dwc->eps[0]; | |
265b70a7 PZ |
1708 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
1709 | false); | |
72246da4 FB |
1710 | if (ret) { |
1711 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
d7be2952 | 1712 | goto err0; |
72246da4 FB |
1713 | } |
1714 | ||
1715 | dep = dwc->eps[1]; | |
265b70a7 PZ |
1716 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
1717 | false); | |
72246da4 FB |
1718 | if (ret) { |
1719 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
d7be2952 | 1720 | goto err1; |
72246da4 FB |
1721 | } |
1722 | ||
1723 | /* begin to receive SETUP packets */ | |
c7fcdeb2 | 1724 | dwc->ep0state = EP0_SETUP_PHASE; |
72246da4 FB |
1725 | dwc3_ep0_out_start(dwc); |
1726 | ||
8698e2ac FB |
1727 | dwc3_gadget_enable_irq(dwc); |
1728 | ||
72246da4 FB |
1729 | return 0; |
1730 | ||
b0d7ffd4 | 1731 | err1: |
d7be2952 | 1732 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
b0d7ffd4 FB |
1733 | |
1734 | err0: | |
72246da4 FB |
1735 | return ret; |
1736 | } | |
1737 | ||
d7be2952 FB |
1738 | static int dwc3_gadget_start(struct usb_gadget *g, |
1739 | struct usb_gadget_driver *driver) | |
72246da4 FB |
1740 | { |
1741 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1742 | unsigned long flags; | |
d7be2952 | 1743 | int ret = 0; |
8698e2ac | 1744 | int irq; |
72246da4 | 1745 | |
d7be2952 FB |
1746 | irq = platform_get_irq(to_platform_device(dwc->dev), 0); |
1747 | ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, | |
1748 | IRQF_SHARED, "dwc3", dwc->ev_buf); | |
1749 | if (ret) { | |
1750 | dev_err(dwc->dev, "failed to request irq #%d --> %d\n", | |
1751 | irq, ret); | |
1752 | goto err0; | |
1753 | } | |
3f308d17 | 1754 | dwc->irq_gadget = irq; |
d7be2952 | 1755 | |
72246da4 | 1756 | spin_lock_irqsave(&dwc->lock, flags); |
d7be2952 FB |
1757 | if (dwc->gadget_driver) { |
1758 | dev_err(dwc->dev, "%s is already bound to %s\n", | |
1759 | dwc->gadget.name, | |
1760 | dwc->gadget_driver->driver.name); | |
1761 | ret = -EBUSY; | |
1762 | goto err1; | |
1763 | } | |
1764 | ||
1765 | dwc->gadget_driver = driver; | |
1766 | ||
1767 | __dwc3_gadget_start(dwc); | |
1768 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1769 | ||
1770 | return 0; | |
1771 | ||
1772 | err1: | |
1773 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1774 | free_irq(irq, dwc); | |
1775 | ||
1776 | err0: | |
1777 | return ret; | |
1778 | } | |
72246da4 | 1779 | |
d7be2952 FB |
1780 | static void __dwc3_gadget_stop(struct dwc3 *dwc) |
1781 | { | |
8698e2ac | 1782 | dwc3_gadget_disable_irq(dwc); |
72246da4 FB |
1783 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
1784 | __dwc3_gadget_ep_disable(dwc->eps[1]); | |
d7be2952 | 1785 | } |
72246da4 | 1786 | |
d7be2952 FB |
1787 | static int dwc3_gadget_stop(struct usb_gadget *g) |
1788 | { | |
1789 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1790 | unsigned long flags; | |
72246da4 | 1791 | |
d7be2952 FB |
1792 | spin_lock_irqsave(&dwc->lock, flags); |
1793 | __dwc3_gadget_stop(dwc); | |
1794 | dwc->gadget_driver = NULL; | |
72246da4 FB |
1795 | spin_unlock_irqrestore(&dwc->lock, flags); |
1796 | ||
3f308d17 | 1797 | free_irq(dwc->irq_gadget, dwc->ev_buf); |
b0d7ffd4 | 1798 | |
72246da4 FB |
1799 | return 0; |
1800 | } | |
802fde98 | 1801 | |
72246da4 FB |
1802 | static const struct usb_gadget_ops dwc3_gadget_ops = { |
1803 | .get_frame = dwc3_gadget_get_frame, | |
1804 | .wakeup = dwc3_gadget_wakeup, | |
1805 | .set_selfpowered = dwc3_gadget_set_selfpowered, | |
1806 | .pullup = dwc3_gadget_pullup, | |
1807 | .udc_start = dwc3_gadget_start, | |
1808 | .udc_stop = dwc3_gadget_stop, | |
1809 | }; | |
1810 | ||
1811 | /* -------------------------------------------------------------------------- */ | |
1812 | ||
6a1e3ef4 FB |
1813 | static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc, |
1814 | u8 num, u32 direction) | |
72246da4 FB |
1815 | { |
1816 | struct dwc3_ep *dep; | |
6a1e3ef4 | 1817 | u8 i; |
72246da4 | 1818 | |
6a1e3ef4 FB |
1819 | for (i = 0; i < num; i++) { |
1820 | u8 epnum = (i << 1) | (!!direction); | |
72246da4 | 1821 | |
72246da4 | 1822 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
734d5a53 | 1823 | if (!dep) |
72246da4 | 1824 | return -ENOMEM; |
72246da4 FB |
1825 | |
1826 | dep->dwc = dwc; | |
1827 | dep->number = epnum; | |
9aa62ae4 | 1828 | dep->direction = !!direction; |
2eb88016 | 1829 | dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); |
72246da4 FB |
1830 | dwc->eps[epnum] = dep; |
1831 | ||
1832 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1, | |
1833 | (epnum & 1) ? "in" : "out"); | |
6a1e3ef4 | 1834 | |
72246da4 | 1835 | dep->endpoint.name = dep->name; |
72246da4 | 1836 | |
73815280 | 1837 | dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name); |
653df35e | 1838 | |
72246da4 | 1839 | if (epnum == 0 || epnum == 1) { |
e117e742 | 1840 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
6048e4c6 | 1841 | dep->endpoint.maxburst = 1; |
72246da4 FB |
1842 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; |
1843 | if (!epnum) | |
1844 | dwc->gadget.ep0 = &dep->endpoint; | |
1845 | } else { | |
1846 | int ret; | |
1847 | ||
e117e742 | 1848 | usb_ep_set_maxpacket_limit(&dep->endpoint, 1024); |
12d36c16 | 1849 | dep->endpoint.max_streams = 15; |
72246da4 FB |
1850 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
1851 | list_add_tail(&dep->endpoint.ep_list, | |
1852 | &dwc->gadget.ep_list); | |
1853 | ||
1854 | ret = dwc3_alloc_trb_pool(dep); | |
25b8ff68 | 1855 | if (ret) |
72246da4 | 1856 | return ret; |
72246da4 | 1857 | } |
25b8ff68 | 1858 | |
a474d3b7 RB |
1859 | if (epnum == 0 || epnum == 1) { |
1860 | dep->endpoint.caps.type_control = true; | |
1861 | } else { | |
1862 | dep->endpoint.caps.type_iso = true; | |
1863 | dep->endpoint.caps.type_bulk = true; | |
1864 | dep->endpoint.caps.type_int = true; | |
1865 | } | |
1866 | ||
1867 | dep->endpoint.caps.dir_in = !!direction; | |
1868 | dep->endpoint.caps.dir_out = !direction; | |
1869 | ||
aa3342c8 FB |
1870 | INIT_LIST_HEAD(&dep->pending_list); |
1871 | INIT_LIST_HEAD(&dep->started_list); | |
72246da4 FB |
1872 | } |
1873 | ||
1874 | return 0; | |
1875 | } | |
1876 | ||
6a1e3ef4 FB |
1877 | static int dwc3_gadget_init_endpoints(struct dwc3 *dwc) |
1878 | { | |
1879 | int ret; | |
1880 | ||
1881 | INIT_LIST_HEAD(&dwc->gadget.ep_list); | |
1882 | ||
1883 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0); | |
1884 | if (ret < 0) { | |
73815280 FB |
1885 | dwc3_trace(trace_dwc3_gadget, |
1886 | "failed to allocate OUT endpoints"); | |
6a1e3ef4 FB |
1887 | return ret; |
1888 | } | |
1889 | ||
1890 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1); | |
1891 | if (ret < 0) { | |
73815280 FB |
1892 | dwc3_trace(trace_dwc3_gadget, |
1893 | "failed to allocate IN endpoints"); | |
6a1e3ef4 FB |
1894 | return ret; |
1895 | } | |
1896 | ||
1897 | return 0; | |
1898 | } | |
1899 | ||
72246da4 FB |
1900 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) |
1901 | { | |
1902 | struct dwc3_ep *dep; | |
1903 | u8 epnum; | |
1904 | ||
1905 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { | |
1906 | dep = dwc->eps[epnum]; | |
6a1e3ef4 FB |
1907 | if (!dep) |
1908 | continue; | |
5bf8fae3 GC |
1909 | /* |
1910 | * Physical endpoints 0 and 1 are special; they form the | |
1911 | * bi-directional USB endpoint 0. | |
1912 | * | |
1913 | * For those two physical endpoints, we don't allocate a TRB | |
1914 | * pool nor do we add them the endpoints list. Due to that, we | |
1915 | * shouldn't do these two operations otherwise we would end up | |
1916 | * with all sorts of bugs when removing dwc3.ko. | |
1917 | */ | |
1918 | if (epnum != 0 && epnum != 1) { | |
1919 | dwc3_free_trb_pool(dep); | |
72246da4 | 1920 | list_del(&dep->endpoint.ep_list); |
5bf8fae3 | 1921 | } |
72246da4 FB |
1922 | |
1923 | kfree(dep); | |
1924 | } | |
1925 | } | |
1926 | ||
72246da4 | 1927 | /* -------------------------------------------------------------------------- */ |
e5caff68 | 1928 | |
e5ba5ec8 PA |
1929 | static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep, |
1930 | struct dwc3_request *req, struct dwc3_trb *trb, | |
72246da4 FB |
1931 | const struct dwc3_event_depevt *event, int status) |
1932 | { | |
72246da4 FB |
1933 | unsigned int count; |
1934 | unsigned int s_pkt = 0; | |
d6d6ec7b | 1935 | unsigned int trb_status; |
72246da4 | 1936 | |
2c4cbe6e FB |
1937 | trace_dwc3_complete_trb(dep, trb); |
1938 | ||
e5ba5ec8 PA |
1939 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
1940 | /* | |
1941 | * We continue despite the error. There is not much we | |
1942 | * can do. If we don't clean it up we loop forever. If | |
1943 | * we skip the TRB then it gets overwritten after a | |
1944 | * while since we use them in a ring buffer. A BUG() | |
1945 | * would help. Lets hope that if this occurs, someone | |
1946 | * fixes the root cause instead of looking away :) | |
1947 | */ | |
1948 | dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n", | |
1949 | dep->name, trb); | |
1950 | count = trb->size & DWC3_TRB_SIZE_MASK; | |
1951 | ||
1952 | if (dep->direction) { | |
1953 | if (count) { | |
1954 | trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size); | |
1955 | if (trb_status == DWC3_TRBSTS_MISSED_ISOC) { | |
ec5e795c FB |
1956 | dwc3_trace(trace_dwc3_gadget, |
1957 | "%s: incomplete IN transfer\n", | |
e5ba5ec8 PA |
1958 | dep->name); |
1959 | /* | |
1960 | * If missed isoc occurred and there is | |
1961 | * no request queued then issue END | |
1962 | * TRANSFER, so that core generates | |
1963 | * next xfernotready and we will issue | |
1964 | * a fresh START TRANSFER. | |
1965 | * If there are still queued request | |
1966 | * then wait, do not issue either END | |
1967 | * or UPDATE TRANSFER, just attach next | |
aa3342c8 | 1968 | * request in pending_list during |
e5ba5ec8 PA |
1969 | * giveback.If any future queued request |
1970 | * is successfully transferred then we | |
1971 | * will issue UPDATE TRANSFER for all | |
aa3342c8 | 1972 | * request in the pending_list. |
e5ba5ec8 PA |
1973 | */ |
1974 | dep->flags |= DWC3_EP_MISSED_ISOC; | |
1975 | } else { | |
1976 | dev_err(dwc->dev, "incomplete IN transfer %s\n", | |
1977 | dep->name); | |
1978 | status = -ECONNRESET; | |
1979 | } | |
1980 | } else { | |
1981 | dep->flags &= ~DWC3_EP_MISSED_ISOC; | |
1982 | } | |
1983 | } else { | |
1984 | if (count && (event->status & DEPEVT_STATUS_SHORT)) | |
1985 | s_pkt = 1; | |
1986 | } | |
1987 | ||
1988 | /* | |
1989 | * We assume here we will always receive the entire data block | |
1990 | * which we should receive. Meaning, if we program RX to | |
1991 | * receive 4K but we receive only 2K, we assume that's all we | |
1992 | * should receive and we simply bounce the request back to the | |
1993 | * gadget driver for further processing. | |
1994 | */ | |
1995 | req->request.actual += req->request.length - count; | |
1996 | if (s_pkt) | |
1997 | return 1; | |
1998 | if ((event->status & DEPEVT_STATUS_LST) && | |
1999 | (trb->ctrl & (DWC3_TRB_CTRL_LST | | |
2000 | DWC3_TRB_CTRL_HWO))) | |
2001 | return 1; | |
2002 | if ((event->status & DEPEVT_STATUS_IOC) && | |
2003 | (trb->ctrl & DWC3_TRB_CTRL_IOC)) | |
2004 | return 1; | |
2005 | return 0; | |
2006 | } | |
2007 | ||
2008 | static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, | |
2009 | const struct dwc3_event_depevt *event, int status) | |
2010 | { | |
2011 | struct dwc3_request *req; | |
2012 | struct dwc3_trb *trb; | |
2013 | unsigned int slot; | |
2014 | unsigned int i; | |
2015 | int ret; | |
2016 | ||
72246da4 | 2017 | do { |
aa3342c8 | 2018 | req = next_request(&dep->started_list); |
ac7bdcc1 | 2019 | if (WARN_ON_ONCE(!req)) |
d115d705 | 2020 | return 1; |
ac7bdcc1 | 2021 | |
d115d705 VS |
2022 | i = 0; |
2023 | do { | |
53fd8818 | 2024 | slot = req->first_trb_index + i; |
36b68aae | 2025 | if (slot == DWC3_TRB_NUM - 1) |
d115d705 VS |
2026 | slot++; |
2027 | slot %= DWC3_TRB_NUM; | |
2028 | trb = &dep->trb_pool[slot]; | |
2029 | ||
2030 | ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb, | |
2031 | event, status); | |
2032 | if (ret) | |
2033 | break; | |
2034 | } while (++i < req->request.num_mapped_sgs); | |
2035 | ||
2036 | dwc3_gadget_giveback(dep, req, status); | |
e5ba5ec8 PA |
2037 | |
2038 | if (ret) | |
72246da4 | 2039 | break; |
d115d705 | 2040 | } while (1); |
72246da4 | 2041 | |
cdc359dd | 2042 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
aa3342c8 FB |
2043 | list_empty(&dep->started_list)) { |
2044 | if (list_empty(&dep->pending_list)) { | |
cdc359dd PA |
2045 | /* |
2046 | * If there is no entry in request list then do | |
2047 | * not issue END TRANSFER now. Just set PENDING | |
2048 | * flag, so that END TRANSFER is issued when an | |
2049 | * entry is added into request list. | |
2050 | */ | |
2051 | dep->flags = DWC3_EP_PENDING_REQUEST; | |
2052 | } else { | |
b992e681 | 2053 | dwc3_stop_active_transfer(dwc, dep->number, true); |
cdc359dd PA |
2054 | dep->flags = DWC3_EP_ENABLED; |
2055 | } | |
7efea86c PA |
2056 | return 1; |
2057 | } | |
2058 | ||
72246da4 FB |
2059 | return 1; |
2060 | } | |
2061 | ||
2062 | static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc, | |
029d97ff | 2063 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) |
72246da4 FB |
2064 | { |
2065 | unsigned status = 0; | |
2066 | int clean_busy; | |
e18b7975 FB |
2067 | u32 is_xfer_complete; |
2068 | ||
2069 | is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE); | |
72246da4 FB |
2070 | |
2071 | if (event->status & DEPEVT_STATUS_BUSERR) | |
2072 | status = -ECONNRESET; | |
2073 | ||
1d046793 | 2074 | clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status); |
e18b7975 FB |
2075 | if (clean_busy && (is_xfer_complete || |
2076 | usb_endpoint_xfer_isoc(dep->endpoint.desc))) | |
72246da4 | 2077 | dep->flags &= ~DWC3_EP_BUSY; |
fae2b904 FB |
2078 | |
2079 | /* | |
2080 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. | |
2081 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. | |
2082 | */ | |
2083 | if (dwc->revision < DWC3_REVISION_183A) { | |
2084 | u32 reg; | |
2085 | int i; | |
2086 | ||
2087 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { | |
348e026f | 2088 | dep = dwc->eps[i]; |
fae2b904 FB |
2089 | |
2090 | if (!(dep->flags & DWC3_EP_ENABLED)) | |
2091 | continue; | |
2092 | ||
aa3342c8 | 2093 | if (!list_empty(&dep->started_list)) |
fae2b904 FB |
2094 | return; |
2095 | } | |
2096 | ||
2097 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2098 | reg |= dwc->u1u2; | |
2099 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2100 | ||
2101 | dwc->u1u2 = 0; | |
2102 | } | |
8a1a9c9e | 2103 | |
e6e709b7 | 2104 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
8a1a9c9e FB |
2105 | int ret; |
2106 | ||
4fae2e3e | 2107 | ret = __dwc3_gadget_kick_transfer(dep, 0); |
8a1a9c9e FB |
2108 | if (!ret || ret == -EBUSY) |
2109 | return; | |
2110 | } | |
72246da4 FB |
2111 | } |
2112 | ||
72246da4 FB |
2113 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, |
2114 | const struct dwc3_event_depevt *event) | |
2115 | { | |
2116 | struct dwc3_ep *dep; | |
2117 | u8 epnum = event->endpoint_number; | |
2118 | ||
2119 | dep = dwc->eps[epnum]; | |
2120 | ||
3336abb5 FB |
2121 | if (!(dep->flags & DWC3_EP_ENABLED)) |
2122 | return; | |
2123 | ||
72246da4 FB |
2124 | if (epnum == 0 || epnum == 1) { |
2125 | dwc3_ep0_interrupt(dwc, event); | |
2126 | return; | |
2127 | } | |
2128 | ||
2129 | switch (event->endpoint_event) { | |
2130 | case DWC3_DEPEVT_XFERCOMPLETE: | |
b4996a86 | 2131 | dep->resource_index = 0; |
c2df85ca | 2132 | |
16e78db7 | 2133 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
ec5e795c FB |
2134 | dwc3_trace(trace_dwc3_gadget, |
2135 | "%s is an Isochronous endpoint\n", | |
72246da4 FB |
2136 | dep->name); |
2137 | return; | |
2138 | } | |
2139 | ||
029d97ff | 2140 | dwc3_endpoint_transfer_complete(dwc, dep, event); |
72246da4 FB |
2141 | break; |
2142 | case DWC3_DEPEVT_XFERINPROGRESS: | |
029d97ff | 2143 | dwc3_endpoint_transfer_complete(dwc, dep, event); |
72246da4 FB |
2144 | break; |
2145 | case DWC3_DEPEVT_XFERNOTREADY: | |
16e78db7 | 2146 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
72246da4 FB |
2147 | dwc3_gadget_start_isoc(dwc, dep, event); |
2148 | } else { | |
6bb4fe12 | 2149 | int active; |
72246da4 FB |
2150 | int ret; |
2151 | ||
6bb4fe12 FB |
2152 | active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE; |
2153 | ||
73815280 | 2154 | dwc3_trace(trace_dwc3_gadget, "%s: reason %s", |
6bb4fe12 | 2155 | dep->name, active ? "Transfer Active" |
72246da4 FB |
2156 | : "Transfer Not Active"); |
2157 | ||
4fae2e3e | 2158 | ret = __dwc3_gadget_kick_transfer(dep, 0); |
72246da4 FB |
2159 | if (!ret || ret == -EBUSY) |
2160 | return; | |
2161 | ||
ec5e795c FB |
2162 | dwc3_trace(trace_dwc3_gadget, |
2163 | "%s: failed to kick transfers\n", | |
72246da4 FB |
2164 | dep->name); |
2165 | } | |
2166 | ||
879631aa FB |
2167 | break; |
2168 | case DWC3_DEPEVT_STREAMEVT: | |
16e78db7 | 2169 | if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) { |
879631aa FB |
2170 | dev_err(dwc->dev, "Stream event for non-Bulk %s\n", |
2171 | dep->name); | |
2172 | return; | |
2173 | } | |
2174 | ||
2175 | switch (event->status) { | |
2176 | case DEPEVT_STREAMEVT_FOUND: | |
73815280 FB |
2177 | dwc3_trace(trace_dwc3_gadget, |
2178 | "Stream %d found and started", | |
879631aa FB |
2179 | event->parameters); |
2180 | ||
2181 | break; | |
2182 | case DEPEVT_STREAMEVT_NOTFOUND: | |
2183 | /* FALLTHROUGH */ | |
2184 | default: | |
ec5e795c FB |
2185 | dwc3_trace(trace_dwc3_gadget, |
2186 | "unable to find suitable stream\n"); | |
879631aa | 2187 | } |
72246da4 FB |
2188 | break; |
2189 | case DWC3_DEPEVT_RXTXFIFOEVT: | |
ec5e795c | 2190 | dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name); |
72246da4 | 2191 | break; |
72246da4 | 2192 | case DWC3_DEPEVT_EPCMDCMPLT: |
73815280 | 2193 | dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete"); |
72246da4 FB |
2194 | break; |
2195 | } | |
2196 | } | |
2197 | ||
2198 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) | |
2199 | { | |
2200 | if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { | |
2201 | spin_unlock(&dwc->lock); | |
2202 | dwc->gadget_driver->disconnect(&dwc->gadget); | |
2203 | spin_lock(&dwc->lock); | |
2204 | } | |
2205 | } | |
2206 | ||
bc5ba2e0 FB |
2207 | static void dwc3_suspend_gadget(struct dwc3 *dwc) |
2208 | { | |
73a30bfc | 2209 | if (dwc->gadget_driver && dwc->gadget_driver->suspend) { |
bc5ba2e0 FB |
2210 | spin_unlock(&dwc->lock); |
2211 | dwc->gadget_driver->suspend(&dwc->gadget); | |
2212 | spin_lock(&dwc->lock); | |
2213 | } | |
2214 | } | |
2215 | ||
2216 | static void dwc3_resume_gadget(struct dwc3 *dwc) | |
2217 | { | |
73a30bfc | 2218 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
bc5ba2e0 FB |
2219 | spin_unlock(&dwc->lock); |
2220 | dwc->gadget_driver->resume(&dwc->gadget); | |
5c7b3b02 | 2221 | spin_lock(&dwc->lock); |
8e74475b FB |
2222 | } |
2223 | } | |
2224 | ||
2225 | static void dwc3_reset_gadget(struct dwc3 *dwc) | |
2226 | { | |
2227 | if (!dwc->gadget_driver) | |
2228 | return; | |
2229 | ||
2230 | if (dwc->gadget.speed != USB_SPEED_UNKNOWN) { | |
2231 | spin_unlock(&dwc->lock); | |
2232 | usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver); | |
bc5ba2e0 FB |
2233 | spin_lock(&dwc->lock); |
2234 | } | |
2235 | } | |
2236 | ||
b992e681 | 2237 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force) |
72246da4 FB |
2238 | { |
2239 | struct dwc3_ep *dep; | |
2240 | struct dwc3_gadget_ep_cmd_params params; | |
2241 | u32 cmd; | |
2242 | int ret; | |
2243 | ||
2244 | dep = dwc->eps[epnum]; | |
2245 | ||
b4996a86 | 2246 | if (!dep->resource_index) |
3daf74d7 PA |
2247 | return; |
2248 | ||
57911504 PA |
2249 | /* |
2250 | * NOTICE: We are violating what the Databook says about the | |
2251 | * EndTransfer command. Ideally we would _always_ wait for the | |
2252 | * EndTransfer Command Completion IRQ, but that's causing too | |
2253 | * much trouble synchronizing between us and gadget driver. | |
2254 | * | |
2255 | * We have discussed this with the IP Provider and it was | |
2256 | * suggested to giveback all requests here, but give HW some | |
2257 | * extra time to synchronize with the interconnect. We're using | |
dc93b41a | 2258 | * an arbitrary 100us delay for that. |
57911504 PA |
2259 | * |
2260 | * Note also that a similar handling was tested by Synopsys | |
2261 | * (thanks a lot Paul) and nothing bad has come out of it. | |
2262 | * In short, what we're doing is: | |
2263 | * | |
2264 | * - Issue EndTransfer WITH CMDIOC bit set | |
2265 | * - Wait 100us | |
2266 | */ | |
2267 | ||
3daf74d7 | 2268 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
b992e681 PZ |
2269 | cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; |
2270 | cmd |= DWC3_DEPCMD_CMDIOC; | |
b4996a86 | 2271 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
3daf74d7 | 2272 | memset(¶ms, 0, sizeof(params)); |
2cd4718d | 2273 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
3daf74d7 | 2274 | WARN_ON_ONCE(ret); |
b4996a86 | 2275 | dep->resource_index = 0; |
041d81f4 | 2276 | dep->flags &= ~DWC3_EP_BUSY; |
57911504 | 2277 | udelay(100); |
72246da4 FB |
2278 | } |
2279 | ||
2280 | static void dwc3_stop_active_transfers(struct dwc3 *dwc) | |
2281 | { | |
2282 | u32 epnum; | |
2283 | ||
2284 | for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) { | |
2285 | struct dwc3_ep *dep; | |
2286 | ||
2287 | dep = dwc->eps[epnum]; | |
6a1e3ef4 FB |
2288 | if (!dep) |
2289 | continue; | |
2290 | ||
72246da4 FB |
2291 | if (!(dep->flags & DWC3_EP_ENABLED)) |
2292 | continue; | |
2293 | ||
624407f9 | 2294 | dwc3_remove_requests(dwc, dep); |
72246da4 FB |
2295 | } |
2296 | } | |
2297 | ||
2298 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) | |
2299 | { | |
2300 | u32 epnum; | |
2301 | ||
2302 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { | |
2303 | struct dwc3_ep *dep; | |
72246da4 FB |
2304 | int ret; |
2305 | ||
2306 | dep = dwc->eps[epnum]; | |
6a1e3ef4 FB |
2307 | if (!dep) |
2308 | continue; | |
72246da4 FB |
2309 | |
2310 | if (!(dep->flags & DWC3_EP_STALL)) | |
2311 | continue; | |
2312 | ||
2313 | dep->flags &= ~DWC3_EP_STALL; | |
2314 | ||
50c763f8 | 2315 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
72246da4 FB |
2316 | WARN_ON_ONCE(ret); |
2317 | } | |
2318 | } | |
2319 | ||
2320 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) | |
2321 | { | |
c4430a26 FB |
2322 | int reg; |
2323 | ||
72246da4 FB |
2324 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
2325 | reg &= ~DWC3_DCTL_INITU1ENA; | |
2326 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2327 | ||
2328 | reg &= ~DWC3_DCTL_INITU2ENA; | |
2329 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
72246da4 | 2330 | |
72246da4 FB |
2331 | dwc3_disconnect_gadget(dwc); |
2332 | ||
2333 | dwc->gadget.speed = USB_SPEED_UNKNOWN; | |
df62df56 | 2334 | dwc->setup_packet_pending = false; |
06a374ed | 2335 | usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED); |
72246da4 FB |
2336 | } |
2337 | ||
72246da4 FB |
2338 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) |
2339 | { | |
2340 | u32 reg; | |
2341 | ||
df62df56 FB |
2342 | /* |
2343 | * WORKAROUND: DWC3 revisions <1.88a have an issue which | |
2344 | * would cause a missing Disconnect Event if there's a | |
2345 | * pending Setup Packet in the FIFO. | |
2346 | * | |
2347 | * There's no suggested workaround on the official Bug | |
2348 | * report, which states that "unless the driver/application | |
2349 | * is doing any special handling of a disconnect event, | |
2350 | * there is no functional issue". | |
2351 | * | |
2352 | * Unfortunately, it turns out that we _do_ some special | |
2353 | * handling of a disconnect event, namely complete all | |
2354 | * pending transfers, notify gadget driver of the | |
2355 | * disconnection, and so on. | |
2356 | * | |
2357 | * Our suggested workaround is to follow the Disconnect | |
2358 | * Event steps here, instead, based on a setup_packet_pending | |
b5d335e5 FB |
2359 | * flag. Such flag gets set whenever we have a SETUP_PENDING |
2360 | * status for EP0 TRBs and gets cleared on XferComplete for the | |
df62df56 FB |
2361 | * same endpoint. |
2362 | * | |
2363 | * Refers to: | |
2364 | * | |
2365 | * STAR#9000466709: RTL: Device : Disconnect event not | |
2366 | * generated if setup packet pending in FIFO | |
2367 | */ | |
2368 | if (dwc->revision < DWC3_REVISION_188A) { | |
2369 | if (dwc->setup_packet_pending) | |
2370 | dwc3_gadget_disconnect_interrupt(dwc); | |
2371 | } | |
2372 | ||
8e74475b | 2373 | dwc3_reset_gadget(dwc); |
72246da4 FB |
2374 | |
2375 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2376 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; | |
2377 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
3b637367 | 2378 | dwc->test_mode = false; |
72246da4 FB |
2379 | |
2380 | dwc3_stop_active_transfers(dwc); | |
2381 | dwc3_clear_stall_all_ep(dwc); | |
2382 | ||
2383 | /* Reset device address to zero */ | |
2384 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
2385 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); | |
2386 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
72246da4 FB |
2387 | } |
2388 | ||
2389 | static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed) | |
2390 | { | |
2391 | u32 reg; | |
2392 | u32 usb30_clock = DWC3_GCTL_CLK_BUS; | |
2393 | ||
2394 | /* | |
2395 | * We change the clock only at SS but I dunno why I would want to do | |
2396 | * this. Maybe it becomes part of the power saving plan. | |
2397 | */ | |
2398 | ||
ee5cd41c JY |
2399 | if ((speed != DWC3_DSTS_SUPERSPEED) && |
2400 | (speed != DWC3_DSTS_SUPERSPEED_PLUS)) | |
72246da4 FB |
2401 | return; |
2402 | ||
2403 | /* | |
2404 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed | |
2405 | * each time on Connect Done. | |
2406 | */ | |
2407 | if (!usb30_clock) | |
2408 | return; | |
2409 | ||
2410 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); | |
2411 | reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock); | |
2412 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
2413 | } | |
2414 | ||
72246da4 FB |
2415 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) |
2416 | { | |
72246da4 FB |
2417 | struct dwc3_ep *dep; |
2418 | int ret; | |
2419 | u32 reg; | |
2420 | u8 speed; | |
2421 | ||
72246da4 FB |
2422 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
2423 | speed = reg & DWC3_DSTS_CONNECTSPD; | |
2424 | dwc->speed = speed; | |
2425 | ||
2426 | dwc3_update_ram_clk_sel(dwc, speed); | |
2427 | ||
2428 | switch (speed) { | |
7580862b JY |
2429 | case DWC3_DCFG_SUPERSPEED_PLUS: |
2430 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); | |
2431 | dwc->gadget.ep0->maxpacket = 512; | |
2432 | dwc->gadget.speed = USB_SPEED_SUPER_PLUS; | |
2433 | break; | |
72246da4 | 2434 | case DWC3_DCFG_SUPERSPEED: |
05870c5b FB |
2435 | /* |
2436 | * WORKAROUND: DWC3 revisions <1.90a have an issue which | |
2437 | * would cause a missing USB3 Reset event. | |
2438 | * | |
2439 | * In such situations, we should force a USB3 Reset | |
2440 | * event by calling our dwc3_gadget_reset_interrupt() | |
2441 | * routine. | |
2442 | * | |
2443 | * Refers to: | |
2444 | * | |
2445 | * STAR#9000483510: RTL: SS : USB3 reset event may | |
2446 | * not be generated always when the link enters poll | |
2447 | */ | |
2448 | if (dwc->revision < DWC3_REVISION_190A) | |
2449 | dwc3_gadget_reset_interrupt(dwc); | |
2450 | ||
72246da4 FB |
2451 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
2452 | dwc->gadget.ep0->maxpacket = 512; | |
2453 | dwc->gadget.speed = USB_SPEED_SUPER; | |
2454 | break; | |
2455 | case DWC3_DCFG_HIGHSPEED: | |
2456 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); | |
2457 | dwc->gadget.ep0->maxpacket = 64; | |
2458 | dwc->gadget.speed = USB_SPEED_HIGH; | |
2459 | break; | |
2460 | case DWC3_DCFG_FULLSPEED2: | |
2461 | case DWC3_DCFG_FULLSPEED1: | |
2462 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); | |
2463 | dwc->gadget.ep0->maxpacket = 64; | |
2464 | dwc->gadget.speed = USB_SPEED_FULL; | |
2465 | break; | |
2466 | case DWC3_DCFG_LOWSPEED: | |
2467 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); | |
2468 | dwc->gadget.ep0->maxpacket = 8; | |
2469 | dwc->gadget.speed = USB_SPEED_LOW; | |
2470 | break; | |
2471 | } | |
2472 | ||
2b758350 PA |
2473 | /* Enable USB2 LPM Capability */ |
2474 | ||
ee5cd41c JY |
2475 | if ((dwc->revision > DWC3_REVISION_194A) && |
2476 | (speed != DWC3_DCFG_SUPERSPEED) && | |
2477 | (speed != DWC3_DCFG_SUPERSPEED_PLUS)) { | |
2b758350 PA |
2478 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
2479 | reg |= DWC3_DCFG_LPM_CAP; | |
2480 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
2481 | ||
2482 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2483 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); | |
2484 | ||
460d098c | 2485 | reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold); |
2b758350 | 2486 | |
80caf7d2 HR |
2487 | /* |
2488 | * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and | |
2489 | * DCFG.LPMCap is set, core responses with an ACK and the | |
2490 | * BESL value in the LPM token is less than or equal to LPM | |
2491 | * NYET threshold. | |
2492 | */ | |
2493 | WARN_ONCE(dwc->revision < DWC3_REVISION_240A | |
2494 | && dwc->has_lpm_erratum, | |
2495 | "LPM Erratum not available on dwc3 revisisions < 2.40a\n"); | |
2496 | ||
2497 | if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A) | |
2498 | reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold); | |
2499 | ||
356363bf FB |
2500 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
2501 | } else { | |
2502 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2503 | reg &= ~DWC3_DCTL_HIRD_THRES_MASK; | |
2b758350 PA |
2504 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
2505 | } | |
2506 | ||
72246da4 | 2507 | dep = dwc->eps[0]; |
265b70a7 PZ |
2508 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, |
2509 | false); | |
72246da4 FB |
2510 | if (ret) { |
2511 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
2512 | return; | |
2513 | } | |
2514 | ||
2515 | dep = dwc->eps[1]; | |
265b70a7 PZ |
2516 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, |
2517 | false); | |
72246da4 FB |
2518 | if (ret) { |
2519 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
2520 | return; | |
2521 | } | |
2522 | ||
2523 | /* | |
2524 | * Configure PHY via GUSB3PIPECTLn if required. | |
2525 | * | |
2526 | * Update GTXFIFOSIZn | |
2527 | * | |
2528 | * In both cases reset values should be sufficient. | |
2529 | */ | |
2530 | } | |
2531 | ||
2532 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) | |
2533 | { | |
72246da4 FB |
2534 | /* |
2535 | * TODO take core out of low power mode when that's | |
2536 | * implemented. | |
2537 | */ | |
2538 | ||
ad14d4e0 JL |
2539 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
2540 | spin_unlock(&dwc->lock); | |
2541 | dwc->gadget_driver->resume(&dwc->gadget); | |
2542 | spin_lock(&dwc->lock); | |
2543 | } | |
72246da4 FB |
2544 | } |
2545 | ||
2546 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, | |
2547 | unsigned int evtinfo) | |
2548 | { | |
fae2b904 | 2549 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
0b0cc1cd FB |
2550 | unsigned int pwropt; |
2551 | ||
2552 | /* | |
2553 | * WORKAROUND: DWC3 < 2.50a have an issue when configured without | |
2554 | * Hibernation mode enabled which would show up when device detects | |
2555 | * host-initiated U3 exit. | |
2556 | * | |
2557 | * In that case, device will generate a Link State Change Interrupt | |
2558 | * from U3 to RESUME which is only necessary if Hibernation is | |
2559 | * configured in. | |
2560 | * | |
2561 | * There are no functional changes due to such spurious event and we | |
2562 | * just need to ignore it. | |
2563 | * | |
2564 | * Refers to: | |
2565 | * | |
2566 | * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation | |
2567 | * operational mode | |
2568 | */ | |
2569 | pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); | |
2570 | if ((dwc->revision < DWC3_REVISION_250A) && | |
2571 | (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { | |
2572 | if ((dwc->link_state == DWC3_LINK_STATE_U3) && | |
2573 | (next == DWC3_LINK_STATE_RESUME)) { | |
73815280 FB |
2574 | dwc3_trace(trace_dwc3_gadget, |
2575 | "ignoring transition U3 -> Resume"); | |
0b0cc1cd FB |
2576 | return; |
2577 | } | |
2578 | } | |
fae2b904 FB |
2579 | |
2580 | /* | |
2581 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending | |
2582 | * on the link partner, the USB session might do multiple entry/exit | |
2583 | * of low power states before a transfer takes place. | |
2584 | * | |
2585 | * Due to this problem, we might experience lower throughput. The | |
2586 | * suggested workaround is to disable DCTL[12:9] bits if we're | |
2587 | * transitioning from U1/U2 to U0 and enable those bits again | |
2588 | * after a transfer completes and there are no pending transfers | |
2589 | * on any of the enabled endpoints. | |
2590 | * | |
2591 | * This is the first half of that workaround. | |
2592 | * | |
2593 | * Refers to: | |
2594 | * | |
2595 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us | |
2596 | * core send LGO_Ux entering U0 | |
2597 | */ | |
2598 | if (dwc->revision < DWC3_REVISION_183A) { | |
2599 | if (next == DWC3_LINK_STATE_U0) { | |
2600 | u32 u1u2; | |
2601 | u32 reg; | |
2602 | ||
2603 | switch (dwc->link_state) { | |
2604 | case DWC3_LINK_STATE_U1: | |
2605 | case DWC3_LINK_STATE_U2: | |
2606 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2607 | u1u2 = reg & (DWC3_DCTL_INITU2ENA | |
2608 | | DWC3_DCTL_ACCEPTU2ENA | |
2609 | | DWC3_DCTL_INITU1ENA | |
2610 | | DWC3_DCTL_ACCEPTU1ENA); | |
2611 | ||
2612 | if (!dwc->u1u2) | |
2613 | dwc->u1u2 = reg & u1u2; | |
2614 | ||
2615 | reg &= ~u1u2; | |
2616 | ||
2617 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2618 | break; | |
2619 | default: | |
2620 | /* do nothing */ | |
2621 | break; | |
2622 | } | |
2623 | } | |
2624 | } | |
2625 | ||
bc5ba2e0 FB |
2626 | switch (next) { |
2627 | case DWC3_LINK_STATE_U1: | |
2628 | if (dwc->speed == USB_SPEED_SUPER) | |
2629 | dwc3_suspend_gadget(dwc); | |
2630 | break; | |
2631 | case DWC3_LINK_STATE_U2: | |
2632 | case DWC3_LINK_STATE_U3: | |
2633 | dwc3_suspend_gadget(dwc); | |
2634 | break; | |
2635 | case DWC3_LINK_STATE_RESUME: | |
2636 | dwc3_resume_gadget(dwc); | |
2637 | break; | |
2638 | default: | |
2639 | /* do nothing */ | |
2640 | break; | |
2641 | } | |
2642 | ||
e57ebc1d | 2643 | dwc->link_state = next; |
72246da4 FB |
2644 | } |
2645 | ||
e1dadd3b FB |
2646 | static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, |
2647 | unsigned int evtinfo) | |
2648 | { | |
2649 | unsigned int is_ss = evtinfo & BIT(4); | |
2650 | ||
2651 | /** | |
2652 | * WORKAROUND: DWC3 revison 2.20a with hibernation support | |
2653 | * have a known issue which can cause USB CV TD.9.23 to fail | |
2654 | * randomly. | |
2655 | * | |
2656 | * Because of this issue, core could generate bogus hibernation | |
2657 | * events which SW needs to ignore. | |
2658 | * | |
2659 | * Refers to: | |
2660 | * | |
2661 | * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 | |
2662 | * Device Fallback from SuperSpeed | |
2663 | */ | |
2664 | if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) | |
2665 | return; | |
2666 | ||
2667 | /* enter hibernation here */ | |
2668 | } | |
2669 | ||
72246da4 FB |
2670 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, |
2671 | const struct dwc3_event_devt *event) | |
2672 | { | |
2673 | switch (event->type) { | |
2674 | case DWC3_DEVICE_EVENT_DISCONNECT: | |
2675 | dwc3_gadget_disconnect_interrupt(dwc); | |
2676 | break; | |
2677 | case DWC3_DEVICE_EVENT_RESET: | |
2678 | dwc3_gadget_reset_interrupt(dwc); | |
2679 | break; | |
2680 | case DWC3_DEVICE_EVENT_CONNECT_DONE: | |
2681 | dwc3_gadget_conndone_interrupt(dwc); | |
2682 | break; | |
2683 | case DWC3_DEVICE_EVENT_WAKEUP: | |
2684 | dwc3_gadget_wakeup_interrupt(dwc); | |
2685 | break; | |
e1dadd3b FB |
2686 | case DWC3_DEVICE_EVENT_HIBER_REQ: |
2687 | if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, | |
2688 | "unexpected hibernation event\n")) | |
2689 | break; | |
2690 | ||
2691 | dwc3_gadget_hibernation_interrupt(dwc, event->event_info); | |
2692 | break; | |
72246da4 FB |
2693 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: |
2694 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); | |
2695 | break; | |
2696 | case DWC3_DEVICE_EVENT_EOPF: | |
73815280 | 2697 | dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame"); |
72246da4 FB |
2698 | break; |
2699 | case DWC3_DEVICE_EVENT_SOF: | |
73815280 | 2700 | dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame"); |
72246da4 FB |
2701 | break; |
2702 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: | |
73815280 | 2703 | dwc3_trace(trace_dwc3_gadget, "Erratic Error"); |
72246da4 FB |
2704 | break; |
2705 | case DWC3_DEVICE_EVENT_CMD_CMPL: | |
73815280 | 2706 | dwc3_trace(trace_dwc3_gadget, "Command Complete"); |
72246da4 FB |
2707 | break; |
2708 | case DWC3_DEVICE_EVENT_OVERFLOW: | |
73815280 | 2709 | dwc3_trace(trace_dwc3_gadget, "Overflow"); |
72246da4 FB |
2710 | break; |
2711 | default: | |
e9f2aa87 | 2712 | dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); |
72246da4 FB |
2713 | } |
2714 | } | |
2715 | ||
2716 | static void dwc3_process_event_entry(struct dwc3 *dwc, | |
2717 | const union dwc3_event *event) | |
2718 | { | |
2c4cbe6e FB |
2719 | trace_dwc3_event(event->raw); |
2720 | ||
72246da4 FB |
2721 | /* Endpoint IRQ, handle it and return early */ |
2722 | if (event->type.is_devspec == 0) { | |
2723 | /* depevt */ | |
2724 | return dwc3_endpoint_interrupt(dwc, &event->depevt); | |
2725 | } | |
2726 | ||
2727 | switch (event->type.type) { | |
2728 | case DWC3_EVENT_TYPE_DEV: | |
2729 | dwc3_gadget_interrupt(dwc, &event->devt); | |
2730 | break; | |
2731 | /* REVISIT what to do with Carkit and I2C events ? */ | |
2732 | default: | |
2733 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); | |
2734 | } | |
2735 | } | |
2736 | ||
dea520a4 | 2737 | static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) |
b15a762f | 2738 | { |
dea520a4 | 2739 | struct dwc3 *dwc = evt->dwc; |
b15a762f | 2740 | irqreturn_t ret = IRQ_NONE; |
f42f2447 | 2741 | int left; |
e8adfc30 | 2742 | u32 reg; |
b15a762f | 2743 | |
f42f2447 | 2744 | left = evt->count; |
b15a762f | 2745 | |
f42f2447 FB |
2746 | if (!(evt->flags & DWC3_EVENT_PENDING)) |
2747 | return IRQ_NONE; | |
b15a762f | 2748 | |
f42f2447 FB |
2749 | while (left > 0) { |
2750 | union dwc3_event event; | |
b15a762f | 2751 | |
f42f2447 | 2752 | event.raw = *(u32 *) (evt->buf + evt->lpos); |
b15a762f | 2753 | |
f42f2447 | 2754 | dwc3_process_event_entry(dwc, &event); |
b15a762f | 2755 | |
f42f2447 FB |
2756 | /* |
2757 | * FIXME we wrap around correctly to the next entry as | |
2758 | * almost all entries are 4 bytes in size. There is one | |
2759 | * entry which has 12 bytes which is a regular entry | |
2760 | * followed by 8 bytes data. ATM I don't know how | |
2761 | * things are organized if we get next to the a | |
2762 | * boundary so I worry about that once we try to handle | |
2763 | * that. | |
2764 | */ | |
2765 | evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE; | |
2766 | left -= 4; | |
b15a762f | 2767 | |
660e9bde | 2768 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4); |
f42f2447 | 2769 | } |
b15a762f | 2770 | |
f42f2447 FB |
2771 | evt->count = 0; |
2772 | evt->flags &= ~DWC3_EVENT_PENDING; | |
2773 | ret = IRQ_HANDLED; | |
b15a762f | 2774 | |
f42f2447 | 2775 | /* Unmask interrupt */ |
660e9bde | 2776 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
f42f2447 | 2777 | reg &= ~DWC3_GEVNTSIZ_INTMASK; |
660e9bde | 2778 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
b15a762f | 2779 | |
f42f2447 FB |
2780 | return ret; |
2781 | } | |
e8adfc30 | 2782 | |
dea520a4 | 2783 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) |
f42f2447 | 2784 | { |
dea520a4 FB |
2785 | struct dwc3_event_buffer *evt = _evt; |
2786 | struct dwc3 *dwc = evt->dwc; | |
e5f68b4a | 2787 | unsigned long flags; |
f42f2447 | 2788 | irqreturn_t ret = IRQ_NONE; |
f42f2447 | 2789 | |
e5f68b4a | 2790 | spin_lock_irqsave(&dwc->lock, flags); |
dea520a4 | 2791 | ret = dwc3_process_event_buf(evt); |
e5f68b4a | 2792 | spin_unlock_irqrestore(&dwc->lock, flags); |
b15a762f FB |
2793 | |
2794 | return ret; | |
2795 | } | |
2796 | ||
dea520a4 | 2797 | static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) |
72246da4 | 2798 | { |
dea520a4 | 2799 | struct dwc3 *dwc = evt->dwc; |
72246da4 | 2800 | u32 count; |
e8adfc30 | 2801 | u32 reg; |
72246da4 | 2802 | |
660e9bde | 2803 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); |
72246da4 FB |
2804 | count &= DWC3_GEVNTCOUNT_MASK; |
2805 | if (!count) | |
2806 | return IRQ_NONE; | |
2807 | ||
b15a762f FB |
2808 | evt->count = count; |
2809 | evt->flags |= DWC3_EVENT_PENDING; | |
72246da4 | 2810 | |
e8adfc30 | 2811 | /* Mask interrupt */ |
660e9bde | 2812 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
e8adfc30 | 2813 | reg |= DWC3_GEVNTSIZ_INTMASK; |
660e9bde | 2814 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
e8adfc30 | 2815 | |
b15a762f | 2816 | return IRQ_WAKE_THREAD; |
72246da4 FB |
2817 | } |
2818 | ||
dea520a4 | 2819 | static irqreturn_t dwc3_interrupt(int irq, void *_evt) |
72246da4 | 2820 | { |
dea520a4 | 2821 | struct dwc3_event_buffer *evt = _evt; |
72246da4 | 2822 | |
dea520a4 | 2823 | return dwc3_check_event_buf(evt); |
72246da4 FB |
2824 | } |
2825 | ||
2826 | /** | |
2827 | * dwc3_gadget_init - Initializes gadget related registers | |
1d046793 | 2828 | * @dwc: pointer to our controller context structure |
72246da4 FB |
2829 | * |
2830 | * Returns 0 on success otherwise negative errno. | |
2831 | */ | |
41ac7b3a | 2832 | int dwc3_gadget_init(struct dwc3 *dwc) |
72246da4 | 2833 | { |
72246da4 | 2834 | int ret; |
72246da4 FB |
2835 | |
2836 | dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req), | |
2837 | &dwc->ctrl_req_addr, GFP_KERNEL); | |
2838 | if (!dwc->ctrl_req) { | |
2839 | dev_err(dwc->dev, "failed to allocate ctrl request\n"); | |
2840 | ret = -ENOMEM; | |
2841 | goto err0; | |
2842 | } | |
2843 | ||
2abd9d5f | 2844 | dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2, |
72246da4 FB |
2845 | &dwc->ep0_trb_addr, GFP_KERNEL); |
2846 | if (!dwc->ep0_trb) { | |
2847 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); | |
2848 | ret = -ENOMEM; | |
2849 | goto err1; | |
2850 | } | |
2851 | ||
3ef35faf | 2852 | dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL); |
72246da4 | 2853 | if (!dwc->setup_buf) { |
72246da4 FB |
2854 | ret = -ENOMEM; |
2855 | goto err2; | |
2856 | } | |
2857 | ||
5812b1c2 | 2858 | dwc->ep0_bounce = dma_alloc_coherent(dwc->dev, |
3ef35faf FB |
2859 | DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr, |
2860 | GFP_KERNEL); | |
5812b1c2 FB |
2861 | if (!dwc->ep0_bounce) { |
2862 | dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n"); | |
2863 | ret = -ENOMEM; | |
2864 | goto err3; | |
2865 | } | |
2866 | ||
04c03d10 FB |
2867 | dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL); |
2868 | if (!dwc->zlp_buf) { | |
2869 | ret = -ENOMEM; | |
2870 | goto err4; | |
2871 | } | |
2872 | ||
72246da4 | 2873 | dwc->gadget.ops = &dwc3_gadget_ops; |
72246da4 | 2874 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
eeb720fb | 2875 | dwc->gadget.sg_supported = true; |
72246da4 | 2876 | dwc->gadget.name = "dwc3-gadget"; |
6a4290cc | 2877 | dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG; |
72246da4 | 2878 | |
b9e51b2b BM |
2879 | /* |
2880 | * FIXME We might be setting max_speed to <SUPER, however versions | |
2881 | * <2.20a of dwc3 have an issue with metastability (documented | |
2882 | * elsewhere in this driver) which tells us we can't set max speed to | |
2883 | * anything lower than SUPER. | |
2884 | * | |
2885 | * Because gadget.max_speed is only used by composite.c and function | |
2886 | * drivers (i.e. it won't go into dwc3's registers) we are allowing this | |
2887 | * to happen so we avoid sending SuperSpeed Capability descriptor | |
2888 | * together with our BOS descriptor as that could confuse host into | |
2889 | * thinking we can handle super speed. | |
2890 | * | |
2891 | * Note that, in fact, we won't even support GetBOS requests when speed | |
2892 | * is less than super speed because we don't have means, yet, to tell | |
2893 | * composite.c that we are USB 2.0 + LPM ECN. | |
2894 | */ | |
2895 | if (dwc->revision < DWC3_REVISION_220A) | |
2896 | dwc3_trace(trace_dwc3_gadget, | |
2897 | "Changing max_speed on rev %08x\n", | |
2898 | dwc->revision); | |
2899 | ||
2900 | dwc->gadget.max_speed = dwc->maximum_speed; | |
2901 | ||
a4b9d94b DC |
2902 | /* |
2903 | * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize | |
2904 | * on ep out. | |
2905 | */ | |
2906 | dwc->gadget.quirk_ep_out_aligned_size = true; | |
2907 | ||
72246da4 FB |
2908 | /* |
2909 | * REVISIT: Here we should clear all pending IRQs to be | |
2910 | * sure we're starting from a well known location. | |
2911 | */ | |
2912 | ||
2913 | ret = dwc3_gadget_init_endpoints(dwc); | |
2914 | if (ret) | |
04c03d10 | 2915 | goto err5; |
72246da4 | 2916 | |
72246da4 FB |
2917 | ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget); |
2918 | if (ret) { | |
2919 | dev_err(dwc->dev, "failed to register udc\n"); | |
04c03d10 | 2920 | goto err5; |
72246da4 FB |
2921 | } |
2922 | ||
2923 | return 0; | |
2924 | ||
04c03d10 FB |
2925 | err5: |
2926 | kfree(dwc->zlp_buf); | |
2927 | ||
5812b1c2 | 2928 | err4: |
e1f80467 | 2929 | dwc3_gadget_free_endpoints(dwc); |
3ef35faf FB |
2930 | dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE, |
2931 | dwc->ep0_bounce, dwc->ep0_bounce_addr); | |
5812b1c2 | 2932 | |
72246da4 | 2933 | err3: |
0fc9a1be | 2934 | kfree(dwc->setup_buf); |
72246da4 FB |
2935 | |
2936 | err2: | |
2937 | dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb), | |
2938 | dwc->ep0_trb, dwc->ep0_trb_addr); | |
2939 | ||
2940 | err1: | |
2941 | dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req), | |
2942 | dwc->ctrl_req, dwc->ctrl_req_addr); | |
2943 | ||
2944 | err0: | |
2945 | return ret; | |
2946 | } | |
2947 | ||
7415f17c FB |
2948 | /* -------------------------------------------------------------------------- */ |
2949 | ||
72246da4 FB |
2950 | void dwc3_gadget_exit(struct dwc3 *dwc) |
2951 | { | |
72246da4 | 2952 | usb_del_gadget_udc(&dwc->gadget); |
72246da4 | 2953 | |
72246da4 FB |
2954 | dwc3_gadget_free_endpoints(dwc); |
2955 | ||
3ef35faf FB |
2956 | dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE, |
2957 | dwc->ep0_bounce, dwc->ep0_bounce_addr); | |
5812b1c2 | 2958 | |
0fc9a1be | 2959 | kfree(dwc->setup_buf); |
04c03d10 | 2960 | kfree(dwc->zlp_buf); |
72246da4 FB |
2961 | |
2962 | dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb), | |
2963 | dwc->ep0_trb, dwc->ep0_trb_addr); | |
2964 | ||
2965 | dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req), | |
2966 | dwc->ctrl_req, dwc->ctrl_req_addr); | |
72246da4 | 2967 | } |
7415f17c | 2968 | |
0b0231aa | 2969 | int dwc3_gadget_suspend(struct dwc3 *dwc) |
7415f17c | 2970 | { |
9f8a67b6 FB |
2971 | int ret; |
2972 | ||
9772b47a RQ |
2973 | if (!dwc->gadget_driver) |
2974 | return 0; | |
2975 | ||
9f8a67b6 FB |
2976 | ret = dwc3_gadget_run_stop(dwc, false, false); |
2977 | if (ret < 0) | |
2978 | return ret; | |
7415f17c | 2979 | |
9f8a67b6 FB |
2980 | dwc3_disconnect_gadget(dwc); |
2981 | __dwc3_gadget_stop(dwc); | |
7415f17c FB |
2982 | |
2983 | return 0; | |
2984 | } | |
2985 | ||
2986 | int dwc3_gadget_resume(struct dwc3 *dwc) | |
2987 | { | |
7415f17c FB |
2988 | int ret; |
2989 | ||
9772b47a RQ |
2990 | if (!dwc->gadget_driver) |
2991 | return 0; | |
2992 | ||
9f8a67b6 FB |
2993 | ret = __dwc3_gadget_start(dwc); |
2994 | if (ret < 0) | |
7415f17c FB |
2995 | goto err0; |
2996 | ||
9f8a67b6 FB |
2997 | ret = dwc3_gadget_run_stop(dwc, true, false); |
2998 | if (ret < 0) | |
7415f17c FB |
2999 | goto err1; |
3000 | ||
7415f17c FB |
3001 | return 0; |
3002 | ||
3003 | err1: | |
9f8a67b6 | 3004 | __dwc3_gadget_stop(dwc); |
7415f17c FB |
3005 | |
3006 | err0: | |
3007 | return ret; | |
3008 | } |