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