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