Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0 |
bfad65ee | 2 | /* |
72246da4 FB |
3 | * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link |
4 | * | |
10623b87 | 5 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com |
72246da4 FB |
6 | * |
7 | * Authors: Felipe Balbi <balbi@ti.com>, | |
8 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> | |
72246da4 FB |
9 | */ |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/delay.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/spinlock.h> | |
15 | #include <linux/platform_device.h> | |
16 | #include <linux/pm_runtime.h> | |
17 | #include <linux/interrupt.h> | |
18 | #include <linux/io.h> | |
19 | #include <linux/list.h> | |
20 | #include <linux/dma-mapping.h> | |
21 | ||
22 | #include <linux/usb/ch9.h> | |
23 | #include <linux/usb/gadget.h> | |
24 | ||
80977dc9 | 25 | #include "debug.h" |
72246da4 FB |
26 | #include "core.h" |
27 | #include "gadget.h" | |
28 | #include "io.h" | |
29 | ||
d5370106 | 30 | #define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \ |
f62afb49 FB |
31 | & ~((d)->interval - 1)) |
32 | ||
04a9bfcd | 33 | /** |
bfad65ee | 34 | * dwc3_gadget_set_test_mode - enables usb2 test modes |
04a9bfcd FB |
35 | * @dwc: pointer to our context structure |
36 | * @mode: the mode to set (J, K SE0 NAK, Force Enable) | |
37 | * | |
bfad65ee FB |
38 | * Caller should take care of locking. This function will return 0 on |
39 | * success or -EINVAL if wrong Test Selector is passed. | |
04a9bfcd FB |
40 | */ |
41 | int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) | |
42 | { | |
43 | u32 reg; | |
44 | ||
45 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
46 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; | |
47 | ||
48 | switch (mode) { | |
62fb45d3 GKH |
49 | case USB_TEST_J: |
50 | case USB_TEST_K: | |
51 | case USB_TEST_SE0_NAK: | |
52 | case USB_TEST_PACKET: | |
53 | case USB_TEST_FORCE_ENABLE: | |
04a9bfcd FB |
54 | reg |= mode << 1; |
55 | break; | |
56 | default: | |
57 | return -EINVAL; | |
58 | } | |
59 | ||
5b738211 | 60 | dwc3_gadget_dctl_write_safe(dwc, reg); |
04a9bfcd FB |
61 | |
62 | return 0; | |
63 | } | |
64 | ||
911f1f88 | 65 | /** |
bfad65ee | 66 | * dwc3_gadget_get_link_state - gets current state of usb link |
911f1f88 PZ |
67 | * @dwc: pointer to our context structure |
68 | * | |
69 | * Caller should take care of locking. This function will | |
70 | * return the link state on success (>= 0) or -ETIMEDOUT. | |
71 | */ | |
72 | int dwc3_gadget_get_link_state(struct dwc3 *dwc) | |
73 | { | |
74 | u32 reg; | |
75 | ||
76 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
77 | ||
78 | return DWC3_DSTS_USBLNKST(reg); | |
79 | } | |
80 | ||
8598bde7 | 81 | /** |
bfad65ee | 82 | * dwc3_gadget_set_link_state - sets usb link to a particular state |
8598bde7 FB |
83 | * @dwc: pointer to our context structure |
84 | * @state: the state to put link into | |
85 | * | |
86 | * Caller should take care of locking. This function will | |
aee63e3c | 87 | * return 0 on success or -ETIMEDOUT. |
8598bde7 FB |
88 | */ |
89 | int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) | |
90 | { | |
aee63e3c | 91 | int retries = 10000; |
8598bde7 FB |
92 | u32 reg; |
93 | ||
802fde98 PZ |
94 | /* |
95 | * Wait until device controller is ready. Only applies to 1.94a and | |
96 | * later RTL. | |
97 | */ | |
9af21dd6 | 98 | if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) { |
802fde98 PZ |
99 | while (--retries) { |
100 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
101 | if (reg & DWC3_DSTS_DCNRD) | |
102 | udelay(5); | |
103 | else | |
104 | break; | |
105 | } | |
106 | ||
107 | if (retries <= 0) | |
108 | return -ETIMEDOUT; | |
109 | } | |
110 | ||
8598bde7 FB |
111 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
112 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; | |
113 | ||
2e708fa3 TN |
114 | /* set no action before sending new link state change */ |
115 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
116 | ||
8598bde7 FB |
117 | /* set requested state */ |
118 | reg |= DWC3_DCTL_ULSTCHNGREQ(state); | |
119 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
120 | ||
802fde98 PZ |
121 | /* |
122 | * The following code is racy when called from dwc3_gadget_wakeup, | |
123 | * and is not needed, at least on newer versions | |
124 | */ | |
9af21dd6 | 125 | if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) |
802fde98 PZ |
126 | return 0; |
127 | ||
8598bde7 | 128 | /* wait for a change in DSTS */ |
aed430e5 | 129 | retries = 10000; |
8598bde7 FB |
130 | while (--retries) { |
131 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
132 | ||
8598bde7 FB |
133 | if (DWC3_DSTS_USBLNKST(reg) == state) |
134 | return 0; | |
135 | ||
aee63e3c | 136 | udelay(5); |
8598bde7 FB |
137 | } |
138 | ||
8598bde7 FB |
139 | return -ETIMEDOUT; |
140 | } | |
141 | ||
dca0119c | 142 | /** |
bfad65ee FB |
143 | * dwc3_ep_inc_trb - increment a trb index. |
144 | * @index: Pointer to the TRB index to increment. | |
dca0119c JY |
145 | * |
146 | * The index should never point to the link TRB. After incrementing, | |
147 | * if it is point to the link TRB, wrap around to the beginning. The | |
148 | * link TRB is always at the last TRB entry. | |
149 | */ | |
150 | static void dwc3_ep_inc_trb(u8 *index) | |
457e84b6 | 151 | { |
dca0119c JY |
152 | (*index)++; |
153 | if (*index == (DWC3_TRB_NUM - 1)) | |
154 | *index = 0; | |
ef966b9d | 155 | } |
457e84b6 | 156 | |
bfad65ee FB |
157 | /** |
158 | * dwc3_ep_inc_enq - increment endpoint's enqueue pointer | |
159 | * @dep: The endpoint whose enqueue pointer we're incrementing | |
160 | */ | |
dca0119c | 161 | static void dwc3_ep_inc_enq(struct dwc3_ep *dep) |
ef966b9d | 162 | { |
dca0119c | 163 | dwc3_ep_inc_trb(&dep->trb_enqueue); |
ef966b9d | 164 | } |
457e84b6 | 165 | |
bfad65ee FB |
166 | /** |
167 | * dwc3_ep_inc_deq - increment endpoint's dequeue pointer | |
168 | * @dep: The endpoint whose enqueue pointer we're incrementing | |
169 | */ | |
dca0119c | 170 | static void dwc3_ep_inc_deq(struct dwc3_ep *dep) |
ef966b9d | 171 | { |
dca0119c | 172 | dwc3_ep_inc_trb(&dep->trb_dequeue); |
457e84b6 FB |
173 | } |
174 | ||
69102510 | 175 | static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep, |
c91815b5 | 176 | struct dwc3_request *req, int status) |
72246da4 FB |
177 | { |
178 | struct dwc3 *dwc = dep->dwc; | |
179 | ||
72246da4 | 180 | list_del(&req->list); |
e62c5bc5 | 181 | req->remaining = 0; |
bd674224 | 182 | req->needs_extra_trb = false; |
72246da4 FB |
183 | |
184 | if (req->request.status == -EINPROGRESS) | |
185 | req->request.status = status; | |
186 | ||
4a71fcb8 JP |
187 | if (req->trb) |
188 | usb_gadget_unmap_request_by_dev(dwc->sysdev, | |
c91815b5 | 189 | &req->request, req->direction); |
4a71fcb8 JP |
190 | |
191 | req->trb = NULL; | |
2c4cbe6e | 192 | trace_dwc3_gadget_giveback(req); |
72246da4 | 193 | |
c91815b5 FB |
194 | if (dep->number > 1) |
195 | pm_runtime_put(dwc->dev); | |
196 | } | |
197 | ||
198 | /** | |
199 | * dwc3_gadget_giveback - call struct usb_request's ->complete callback | |
200 | * @dep: The endpoint to whom the request belongs to | |
201 | * @req: The request we're giving back | |
202 | * @status: completion code for the request | |
203 | * | |
204 | * Must be called with controller's lock held and interrupts disabled. This | |
205 | * function will unmap @req and call its ->complete() callback to notify upper | |
206 | * layers that it has completed. | |
207 | */ | |
208 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, | |
209 | int status) | |
210 | { | |
211 | struct dwc3 *dwc = dep->dwc; | |
212 | ||
213 | dwc3_gadget_del_and_unmap_request(dep, req, status); | |
a3af5e3a | 214 | req->status = DWC3_REQUEST_STATUS_COMPLETED; |
c91815b5 | 215 | |
72246da4 | 216 | spin_unlock(&dwc->lock); |
304f7e5e | 217 | usb_gadget_giveback_request(&dep->endpoint, &req->request); |
72246da4 FB |
218 | spin_lock(&dwc->lock); |
219 | } | |
220 | ||
bfad65ee FB |
221 | /** |
222 | * dwc3_send_gadget_generic_command - issue a generic command for the controller | |
223 | * @dwc: pointer to the controller context | |
224 | * @cmd: the command to be issued | |
225 | * @param: command parameter | |
226 | * | |
227 | * Caller should take care of locking. Issue @cmd with a given @param to @dwc | |
228 | * and wait for its completion. | |
229 | */ | |
e319bd62 FB |
230 | int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd, |
231 | u32 param) | |
b09bb642 FB |
232 | { |
233 | u32 timeout = 500; | |
71f7e702 | 234 | int status = 0; |
0fe886cd | 235 | int ret = 0; |
b09bb642 FB |
236 | u32 reg; |
237 | ||
238 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); | |
239 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); | |
240 | ||
241 | do { | |
242 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); | |
243 | if (!(reg & DWC3_DGCMD_CMDACT)) { | |
71f7e702 FB |
244 | status = DWC3_DGCMD_STATUS(reg); |
245 | if (status) | |
0fe886cd FB |
246 | ret = -EINVAL; |
247 | break; | |
b09bb642 | 248 | } |
e3aee486 | 249 | } while (--timeout); |
0fe886cd FB |
250 | |
251 | if (!timeout) { | |
0fe886cd | 252 | ret = -ETIMEDOUT; |
71f7e702 | 253 | status = -ETIMEDOUT; |
0fe886cd FB |
254 | } |
255 | ||
71f7e702 FB |
256 | trace_dwc3_gadget_generic_cmd(cmd, param, status); |
257 | ||
0fe886cd | 258 | return ret; |
b09bb642 FB |
259 | } |
260 | ||
c36d8e94 FB |
261 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc); |
262 | ||
bfad65ee FB |
263 | /** |
264 | * dwc3_send_gadget_ep_cmd - issue an endpoint command | |
265 | * @dep: the endpoint to which the command is going to be issued | |
266 | * @cmd: the command to be issued | |
267 | * @params: parameters to the command | |
268 | * | |
269 | * Caller should handle locking. This function will issue @cmd with given | |
270 | * @params to @dep and wait for its completion. | |
271 | */ | |
e319bd62 | 272 | int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd, |
2cd4718d | 273 | struct dwc3_gadget_ep_cmd_params *params) |
72246da4 | 274 | { |
8897a761 | 275 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
2cd4718d | 276 | struct dwc3 *dwc = dep->dwc; |
1c0e69ae | 277 | u32 timeout = 5000; |
87dd9611 | 278 | u32 saved_config = 0; |
72246da4 FB |
279 | u32 reg; |
280 | ||
0933df15 | 281 | int cmd_status = 0; |
c0ca324d | 282 | int ret = -EINVAL; |
72246da4 | 283 | |
2b0f11df | 284 | /* |
87dd9611 TN |
285 | * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or |
286 | * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an | |
287 | * endpoint command. | |
2b0f11df | 288 | * |
87dd9611 TN |
289 | * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY |
290 | * settings. Restore them after the command is completed. | |
291 | * | |
292 | * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2 | |
2b0f11df | 293 | */ |
e81a7018 | 294 | if (dwc->gadget->speed <= USB_SPEED_HIGH) { |
ab2a92e7 FB |
295 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
296 | if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { | |
87dd9611 | 297 | saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; |
ab2a92e7 | 298 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
ab2a92e7 | 299 | } |
87dd9611 TN |
300 | |
301 | if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) { | |
302 | saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM; | |
303 | reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; | |
304 | } | |
305 | ||
306 | if (saved_config) | |
307 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); | |
2b0f11df FB |
308 | } |
309 | ||
5999914f | 310 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { |
c560e763 | 311 | int link_state; |
c36d8e94 | 312 | |
c560e763 TN |
313 | link_state = dwc3_gadget_get_link_state(dwc); |
314 | if (link_state == DWC3_LINK_STATE_U1 || | |
315 | link_state == DWC3_LINK_STATE_U2 || | |
316 | link_state == DWC3_LINK_STATE_U3) { | |
c36d8e94 FB |
317 | ret = __dwc3_gadget_wakeup(dwc); |
318 | dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n", | |
319 | ret); | |
320 | } | |
321 | } | |
322 | ||
2eb88016 FB |
323 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); |
324 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); | |
325 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); | |
72246da4 | 326 | |
8897a761 FB |
327 | /* |
328 | * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're | |
329 | * not relying on XferNotReady, we can make use of a special "No | |
330 | * Response Update Transfer" command where we should clear both CmdAct | |
331 | * and CmdIOC bits. | |
332 | * | |
333 | * With this, we don't need to wait for command completion and can | |
334 | * straight away issue further commands to the endpoint. | |
335 | * | |
336 | * NOTICE: We're making an assumption that control endpoints will never | |
337 | * make use of Update Transfer command. This is a safe assumption | |
338 | * because we can never have more than one request at a time with | |
339 | * Control Endpoints. If anybody changes that assumption, this chunk | |
340 | * needs to be updated accordingly. | |
341 | */ | |
342 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER && | |
343 | !usb_endpoint_xfer_isoc(desc)) | |
344 | cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT); | |
345 | else | |
346 | cmd |= DWC3_DEPCMD_CMDACT; | |
347 | ||
348 | dwc3_writel(dep->regs, DWC3_DEPCMD, cmd); | |
72246da4 | 349 | do { |
2eb88016 | 350 | reg = dwc3_readl(dep->regs, DWC3_DEPCMD); |
72246da4 | 351 | if (!(reg & DWC3_DEPCMD_CMDACT)) { |
0933df15 | 352 | cmd_status = DWC3_DEPCMD_STATUS(reg); |
7b9cc7a2 | 353 | |
7b9cc7a2 KL |
354 | switch (cmd_status) { |
355 | case 0: | |
356 | ret = 0; | |
357 | break; | |
358 | case DEPEVT_TRANSFER_NO_RESOURCE: | |
f7ac582e TN |
359 | dev_WARN(dwc->dev, "No resource for %s\n", |
360 | dep->name); | |
7b9cc7a2 | 361 | ret = -EINVAL; |
c0ca324d | 362 | break; |
7b9cc7a2 KL |
363 | case DEPEVT_TRANSFER_BUS_EXPIRY: |
364 | /* | |
365 | * SW issues START TRANSFER command to | |
366 | * isochronous ep with future frame interval. If | |
367 | * future interval time has already passed when | |
368 | * core receives the command, it will respond | |
369 | * with an error status of 'Bus Expiry'. | |
370 | * | |
371 | * Instead of always returning -EINVAL, let's | |
372 | * give a hint to the gadget driver that this is | |
373 | * the case by returning -EAGAIN. | |
374 | */ | |
7b9cc7a2 KL |
375 | ret = -EAGAIN; |
376 | break; | |
377 | default: | |
378 | dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); | |
379 | } | |
380 | ||
c0ca324d | 381 | break; |
72246da4 | 382 | } |
f6bb225b | 383 | } while (--timeout); |
72246da4 | 384 | |
f6bb225b | 385 | if (timeout == 0) { |
f6bb225b | 386 | ret = -ETIMEDOUT; |
0933df15 | 387 | cmd_status = -ETIMEDOUT; |
f6bb225b | 388 | } |
c0ca324d | 389 | |
0933df15 FB |
390 | trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status); |
391 | ||
9bc3395c TN |
392 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { |
393 | if (ret == 0) | |
394 | dep->flags |= DWC3_EP_TRANSFER_STARTED; | |
395 | ||
396 | if (ret != -ETIMEDOUT) | |
397 | dwc3_gadget_ep_get_transfer_index(dep); | |
6cb2e4e3 FB |
398 | } |
399 | ||
87dd9611 | 400 | if (saved_config) { |
2b0f11df | 401 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
87dd9611 | 402 | reg |= saved_config; |
2b0f11df FB |
403 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
404 | } | |
405 | ||
c0ca324d | 406 | return ret; |
72246da4 FB |
407 | } |
408 | ||
50c763f8 JY |
409 | static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) |
410 | { | |
411 | struct dwc3 *dwc = dep->dwc; | |
412 | struct dwc3_gadget_ep_cmd_params params; | |
413 | u32 cmd = DWC3_DEPCMD_CLEARSTALL; | |
414 | ||
415 | /* | |
416 | * As of core revision 2.60a the recommended programming model | |
417 | * is to set the ClearPendIN bit when issuing a Clear Stall EP | |
418 | * command for IN endpoints. This is to prevent an issue where | |
419 | * some (non-compliant) hosts may not send ACK TPs for pending | |
420 | * IN transfers due to a mishandled error condition. Synopsys | |
421 | * STAR 9000614252. | |
422 | */ | |
9af21dd6 TN |
423 | if (dep->direction && |
424 | !DWC3_VER_IS_PRIOR(DWC3, 260A) && | |
e81a7018 | 425 | (dwc->gadget->speed >= USB_SPEED_SUPER)) |
50c763f8 JY |
426 | cmd |= DWC3_DEPCMD_CLEARPENDIN; |
427 | ||
428 | memset(¶ms, 0, sizeof(params)); | |
429 | ||
2cd4718d | 430 | return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
50c763f8 JY |
431 | } |
432 | ||
72246da4 | 433 | static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, |
f6bafc6a | 434 | struct dwc3_trb *trb) |
72246da4 | 435 | { |
c439ef87 | 436 | u32 offset = (char *) trb - (char *) dep->trb_pool; |
72246da4 FB |
437 | |
438 | return dep->trb_pool_dma + offset; | |
439 | } | |
440 | ||
441 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) | |
442 | { | |
443 | struct dwc3 *dwc = dep->dwc; | |
444 | ||
445 | if (dep->trb_pool) | |
446 | return 0; | |
447 | ||
d64ff406 | 448 | dep->trb_pool = dma_alloc_coherent(dwc->sysdev, |
72246da4 FB |
449 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
450 | &dep->trb_pool_dma, GFP_KERNEL); | |
451 | if (!dep->trb_pool) { | |
452 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", | |
453 | dep->name); | |
454 | return -ENOMEM; | |
455 | } | |
456 | ||
457 | return 0; | |
458 | } | |
459 | ||
460 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) | |
461 | { | |
462 | struct dwc3 *dwc = dep->dwc; | |
463 | ||
d64ff406 | 464 | dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
72246da4 FB |
465 | dep->trb_pool, dep->trb_pool_dma); |
466 | ||
467 | dep->trb_pool = NULL; | |
468 | dep->trb_pool_dma = 0; | |
469 | } | |
470 | ||
20d1d43f FB |
471 | static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep) |
472 | { | |
473 | struct dwc3_gadget_ep_cmd_params params; | |
474 | ||
475 | memset(¶ms, 0x00, sizeof(params)); | |
476 | ||
477 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); | |
478 | ||
479 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, | |
480 | ¶ms); | |
481 | } | |
c4509601 JY |
482 | |
483 | /** | |
bfad65ee | 484 | * dwc3_gadget_start_config - configure ep resources |
c4509601 JY |
485 | * @dep: endpoint that is being enabled |
486 | * | |
bfad65ee FB |
487 | * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's |
488 | * completion, it will set Transfer Resource for all available endpoints. | |
c4509601 | 489 | * |
bfad65ee FB |
490 | * The assignment of transfer resources cannot perfectly follow the data book |
491 | * due to the fact that the controller driver does not have all knowledge of the | |
492 | * configuration in advance. It is given this information piecemeal by the | |
493 | * composite gadget framework after every SET_CONFIGURATION and | |
494 | * SET_INTERFACE. Trying to follow the databook programming model in this | |
495 | * scenario can cause errors. For two reasons: | |
c4509601 | 496 | * |
bfad65ee FB |
497 | * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every |
498 | * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is | |
499 | * incorrect in the scenario of multiple interfaces. | |
500 | * | |
501 | * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new | |
c4509601 JY |
502 | * endpoint on alt setting (8.1.6). |
503 | * | |
504 | * The following simplified method is used instead: | |
505 | * | |
bfad65ee FB |
506 | * All hardware endpoints can be assigned a transfer resource and this setting |
507 | * will stay persistent until either a core reset or hibernation. So whenever we | |
508 | * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do | |
509 | * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are | |
c4509601 JY |
510 | * guaranteed that there are as many transfer resources as endpoints. |
511 | * | |
bfad65ee FB |
512 | * This function is called for each endpoint when it is being enabled but is |
513 | * triggered only when called for EP0-out, which always happens first, and which | |
514 | * should only happen in one of the above conditions. | |
c4509601 | 515 | */ |
b07c2db8 | 516 | static int dwc3_gadget_start_config(struct dwc3_ep *dep) |
72246da4 FB |
517 | { |
518 | struct dwc3_gadget_ep_cmd_params params; | |
b07c2db8 | 519 | struct dwc3 *dwc; |
72246da4 | 520 | u32 cmd; |
c4509601 JY |
521 | int i; |
522 | int ret; | |
523 | ||
524 | if (dep->number) | |
525 | return 0; | |
72246da4 FB |
526 | |
527 | memset(¶ms, 0x00, sizeof(params)); | |
c4509601 | 528 | cmd = DWC3_DEPCMD_DEPSTARTCFG; |
b07c2db8 | 529 | dwc = dep->dwc; |
72246da4 | 530 | |
2cd4718d | 531 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
c4509601 JY |
532 | if (ret) |
533 | return ret; | |
534 | ||
535 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { | |
536 | struct dwc3_ep *dep = dwc->eps[i]; | |
72246da4 | 537 | |
c4509601 JY |
538 | if (!dep) |
539 | continue; | |
540 | ||
b07c2db8 | 541 | ret = dwc3_gadget_set_xfer_resource(dep); |
c4509601 JY |
542 | if (ret) |
543 | return ret; | |
72246da4 FB |
544 | } |
545 | ||
546 | return 0; | |
547 | } | |
548 | ||
b07c2db8 | 549 | static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action) |
72246da4 | 550 | { |
39ebb05c JY |
551 | const struct usb_ss_ep_comp_descriptor *comp_desc; |
552 | const struct usb_endpoint_descriptor *desc; | |
72246da4 | 553 | struct dwc3_gadget_ep_cmd_params params; |
b07c2db8 | 554 | struct dwc3 *dwc = dep->dwc; |
72246da4 | 555 | |
39ebb05c JY |
556 | comp_desc = dep->endpoint.comp_desc; |
557 | desc = dep->endpoint.desc; | |
558 | ||
72246da4 FB |
559 | memset(¶ms, 0x00, sizeof(params)); |
560 | ||
dc1c70a7 | 561 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) |
d2e9a13a CP |
562 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); |
563 | ||
564 | /* Burst size is only needed in SuperSpeed mode */ | |
e81a7018 | 565 | if (dwc->gadget->speed >= USB_SPEED_SUPER) { |
676e3497 | 566 | u32 burst = dep->endpoint.maxburst; |
e319bd62 | 567 | |
676e3497 | 568 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); |
d2e9a13a | 569 | } |
72246da4 | 570 | |
a2d23f08 FB |
571 | params.param0 |= action; |
572 | if (action == DWC3_DEPCFG_ACTION_RESTORE) | |
265b70a7 | 573 | params.param2 |= dep->saved_state; |
265b70a7 | 574 | |
4bc48c97 FB |
575 | if (usb_endpoint_xfer_control(desc)) |
576 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; | |
13fa2e69 FB |
577 | |
578 | if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) | |
579 | params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; | |
72246da4 | 580 | |
18b7ede5 | 581 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { |
dc1c70a7 | 582 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE |
548f8b31 | 583 | | DWC3_DEPCFG_XFER_COMPLETE_EN |
dc1c70a7 | 584 | | DWC3_DEPCFG_STREAM_EVENT_EN; |
879631aa FB |
585 | dep->stream_capable = true; |
586 | } | |
587 | ||
0b93a4c8 | 588 | if (!usb_endpoint_xfer_control(desc)) |
dc1c70a7 | 589 | params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; |
72246da4 FB |
590 | |
591 | /* | |
592 | * We are doing 1:1 mapping for endpoints, meaning | |
593 | * Physical Endpoints 2 maps to Logical Endpoint 2 and | |
594 | * so on. We consider the direction bit as part of the physical | |
595 | * endpoint number. So USB endpoint 0x81 is 0x03. | |
596 | */ | |
dc1c70a7 | 597 | params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); |
72246da4 FB |
598 | |
599 | /* | |
600 | * We must use the lower 16 TX FIFOs even though | |
601 | * HW might have more | |
602 | */ | |
603 | if (dep->direction) | |
dc1c70a7 | 604 | params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); |
72246da4 FB |
605 | |
606 | if (desc->bInterval) { | |
a1679af8 TN |
607 | u8 bInterval_m1; |
608 | ||
609 | /* | |
3232a3ce TN |
610 | * Valid range for DEPCFG.bInterval_m1 is from 0 to 13. |
611 | * | |
612 | * NOTE: The programming guide incorrectly stated bInterval_m1 | |
613 | * must be set to 0 when operating in fullspeed. Internally the | |
614 | * controller does not have this limitation. See DWC_usb3x | |
615 | * programming guide section 3.2.2.1. | |
a1679af8 TN |
616 | */ |
617 | bInterval_m1 = min_t(u8, desc->bInterval - 1, 13); | |
a1679af8 | 618 | |
4b049f55 TN |
619 | if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT && |
620 | dwc->gadget->speed == USB_SPEED_FULL) | |
621 | dep->interval = desc->bInterval; | |
622 | else | |
623 | dep->interval = 1 << (desc->bInterval - 1); | |
624 | ||
a1679af8 | 625 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1); |
72246da4 FB |
626 | } |
627 | ||
2cd4718d | 628 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); |
72246da4 FB |
629 | } |
630 | ||
140ca4cf TN |
631 | static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, |
632 | bool interrupt); | |
633 | ||
9f607a30 WC |
634 | /** |
635 | * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value | |
636 | * @dwc: pointer to the DWC3 context | |
637 | * @nfifos: number of fifos to calculate for | |
638 | * | |
639 | * Calculates the size value based on the equation below: | |
640 | * | |
641 | * DWC3 revision 280A and prior: | |
642 | * fifo_size = mult * (max_packet / mdwidth) + 1; | |
643 | * | |
644 | * DWC3 revision 290A and onwards: | |
645 | * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 | |
646 | * | |
647 | * The max packet size is set to 1024, as the txfifo requirements mainly apply | |
648 | * to super speed USB use cases. However, it is safe to overestimate the fifo | |
649 | * allocations for other scenarios, i.e. high speed USB. | |
650 | */ | |
651 | static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult) | |
652 | { | |
653 | int max_packet = 1024; | |
654 | int fifo_size; | |
655 | int mdwidth; | |
656 | ||
657 | mdwidth = dwc3_mdwidth(dwc); | |
658 | ||
659 | /* MDWIDTH is represented in bits, we need it in bytes */ | |
660 | mdwidth >>= 3; | |
661 | ||
662 | if (DWC3_VER_IS_PRIOR(DWC3, 290A)) | |
663 | fifo_size = mult * (max_packet / mdwidth) + 1; | |
664 | else | |
665 | fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1; | |
666 | return fifo_size; | |
667 | } | |
668 | ||
669 | /** | |
670 | * dwc3_gadget_clear_tx_fifo_size - Clears txfifo allocation | |
671 | * @dwc: pointer to the DWC3 context | |
672 | * | |
673 | * Iterates through all the endpoint registers and clears the previous txfifo | |
674 | * allocations. | |
675 | */ | |
676 | void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc) | |
677 | { | |
678 | struct dwc3_ep *dep; | |
679 | int fifo_depth; | |
680 | int size; | |
681 | int num; | |
682 | ||
683 | if (!dwc->do_fifo_resize) | |
684 | return; | |
685 | ||
686 | /* Read ep0IN related TXFIFO size */ | |
687 | dep = dwc->eps[1]; | |
688 | size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); | |
689 | if (DWC3_IP_IS(DWC3)) | |
690 | fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size); | |
691 | else | |
692 | fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size); | |
693 | ||
694 | dwc->last_fifo_depth = fifo_depth; | |
695 | /* Clear existing TXFIFO for all IN eps except ep0 */ | |
696 | for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM); | |
697 | num += 2) { | |
698 | dep = dwc->eps[num]; | |
699 | /* Don't change TXFRAMNUM on usb31 version */ | |
700 | size = DWC3_IP_IS(DWC3) ? 0 : | |
701 | dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) & | |
702 | DWC31_GTXFIFOSIZ_TXFRAMNUM; | |
703 | ||
704 | dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size); | |
876a75cb | 705 | dep->flags &= ~DWC3_EP_TXFIFO_RESIZED; |
9f607a30 WC |
706 | } |
707 | dwc->num_ep_resized = 0; | |
708 | } | |
709 | ||
710 | /* | |
711 | * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case | |
712 | * @dwc: pointer to our context structure | |
713 | * | |
714 | * This function will a best effort FIFO allocation in order | |
715 | * to improve FIFO usage and throughput, while still allowing | |
716 | * us to enable as many endpoints as possible. | |
717 | * | |
718 | * Keep in mind that this operation will be highly dependent | |
719 | * on the configured size for RAM1 - which contains TxFifo -, | |
720 | * the amount of endpoints enabled on coreConsultant tool, and | |
721 | * the width of the Master Bus. | |
722 | * | |
723 | * In general, FIFO depths are represented with the following equation: | |
724 | * | |
725 | * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1 | |
726 | * | |
727 | * In conjunction with dwc3_gadget_check_config(), this resizing logic will | |
728 | * ensure that all endpoints will have enough internal memory for one max | |
729 | * packet per endpoint. | |
730 | */ | |
731 | static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep) | |
732 | { | |
733 | struct dwc3 *dwc = dep->dwc; | |
734 | int fifo_0_start; | |
735 | int ram1_depth; | |
736 | int fifo_size; | |
737 | int min_depth; | |
738 | int num_in_ep; | |
739 | int remaining; | |
740 | int num_fifos = 1; | |
741 | int fifo; | |
742 | int tmp; | |
743 | ||
744 | if (!dwc->do_fifo_resize) | |
745 | return 0; | |
746 | ||
747 | /* resize IN endpoints except ep0 */ | |
748 | if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1) | |
749 | return 0; | |
750 | ||
876a75cb JP |
751 | /* bail if already resized */ |
752 | if (dep->flags & DWC3_EP_TXFIFO_RESIZED) | |
753 | return 0; | |
754 | ||
9f607a30 WC |
755 | ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); |
756 | ||
757 | if ((dep->endpoint.maxburst > 1 && | |
758 | usb_endpoint_xfer_bulk(dep->endpoint.desc)) || | |
759 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) | |
760 | num_fifos = 3; | |
761 | ||
762 | if (dep->endpoint.maxburst > 6 && | |
763 | usb_endpoint_xfer_bulk(dep->endpoint.desc) && DWC3_IP_IS(DWC31)) | |
764 | num_fifos = dwc->tx_fifo_resize_max_num; | |
765 | ||
766 | /* FIFO size for a single buffer */ | |
767 | fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1); | |
768 | ||
769 | /* Calculate the number of remaining EPs w/o any FIFO */ | |
770 | num_in_ep = dwc->max_cfg_eps; | |
771 | num_in_ep -= dwc->num_ep_resized; | |
772 | ||
773 | /* Reserve at least one FIFO for the number of IN EPs */ | |
774 | min_depth = num_in_ep * (fifo + 1); | |
775 | remaining = ram1_depth - min_depth - dwc->last_fifo_depth; | |
776 | remaining = max_t(int, 0, remaining); | |
777 | /* | |
778 | * We've already reserved 1 FIFO per EP, so check what we can fit in | |
779 | * addition to it. If there is not enough remaining space, allocate | |
780 | * all the remaining space to the EP. | |
781 | */ | |
782 | fifo_size = (num_fifos - 1) * fifo; | |
783 | if (remaining < fifo_size) | |
784 | fifo_size = remaining; | |
785 | ||
786 | fifo_size += fifo; | |
787 | /* Last increment according to the TX FIFO size equation */ | |
788 | fifo_size++; | |
789 | ||
790 | /* Check if TXFIFOs start at non-zero addr */ | |
791 | tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); | |
792 | fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp); | |
793 | ||
794 | fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16)); | |
795 | if (DWC3_IP_IS(DWC3)) | |
796 | dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); | |
797 | else | |
798 | dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); | |
799 | ||
800 | /* Check fifo size allocation doesn't exceed available RAM size. */ | |
801 | if (dwc->last_fifo_depth >= ram1_depth) { | |
802 | dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n", | |
803 | dwc->last_fifo_depth, ram1_depth, | |
804 | dep->endpoint.name, fifo_size); | |
805 | if (DWC3_IP_IS(DWC3)) | |
806 | fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); | |
807 | else | |
808 | fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); | |
809 | ||
810 | dwc->last_fifo_depth -= fifo_size; | |
811 | return -ENOMEM; | |
812 | } | |
813 | ||
814 | dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size); | |
876a75cb | 815 | dep->flags |= DWC3_EP_TXFIFO_RESIZED; |
9f607a30 WC |
816 | dwc->num_ep_resized++; |
817 | ||
818 | return 0; | |
819 | } | |
820 | ||
72246da4 | 821 | /** |
bfad65ee | 822 | * __dwc3_gadget_ep_enable - initializes a hw endpoint |
72246da4 | 823 | * @dep: endpoint to be initialized |
a2d23f08 | 824 | * @action: one of INIT, MODIFY or RESTORE |
72246da4 | 825 | * |
bfad65ee FB |
826 | * Caller should take care of locking. Execute all necessary commands to |
827 | * initialize a HW endpoint so it can be used by a gadget driver. | |
72246da4 | 828 | */ |
a2d23f08 | 829 | static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action) |
72246da4 | 830 | { |
39ebb05c | 831 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
72246da4 | 832 | struct dwc3 *dwc = dep->dwc; |
39ebb05c | 833 | |
72246da4 | 834 | u32 reg; |
b09e99ee | 835 | int ret; |
72246da4 FB |
836 | |
837 | if (!(dep->flags & DWC3_EP_ENABLED)) { | |
9f607a30 WC |
838 | ret = dwc3_gadget_resize_tx_fifos(dep); |
839 | if (ret) | |
840 | return ret; | |
841 | ||
b07c2db8 | 842 | ret = dwc3_gadget_start_config(dep); |
72246da4 FB |
843 | if (ret) |
844 | return ret; | |
845 | } | |
846 | ||
b07c2db8 | 847 | ret = dwc3_gadget_set_ep_config(dep, action); |
72246da4 FB |
848 | if (ret) |
849 | return ret; | |
850 | ||
851 | if (!(dep->flags & DWC3_EP_ENABLED)) { | |
f6bafc6a FB |
852 | struct dwc3_trb *trb_st_hw; |
853 | struct dwc3_trb *trb_link; | |
72246da4 | 854 | |
72246da4 FB |
855 | dep->type = usb_endpoint_type(desc); |
856 | dep->flags |= DWC3_EP_ENABLED; | |
857 | ||
858 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); | |
859 | reg |= DWC3_DALEPENA_EP(dep->number); | |
860 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); | |
861 | ||
36b68aae | 862 | if (usb_endpoint_xfer_control(desc)) |
2870e501 | 863 | goto out; |
72246da4 | 864 | |
0d25744a JY |
865 | /* Initialize the TRB ring */ |
866 | dep->trb_dequeue = 0; | |
867 | dep->trb_enqueue = 0; | |
868 | memset(dep->trb_pool, 0, | |
869 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM); | |
870 | ||
36b68aae | 871 | /* Link TRB. The HWO bit is never reset */ |
72246da4 FB |
872 | trb_st_hw = &dep->trb_pool[0]; |
873 | ||
f6bafc6a | 874 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; |
f6bafc6a FB |
875 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
876 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); | |
877 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; | |
878 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; | |
72246da4 FB |
879 | } |
880 | ||
a97ea994 FB |
881 | /* |
882 | * Issue StartTransfer here with no-op TRB so we can always rely on No | |
883 | * Response Update Transfer command. | |
884 | */ | |
140ca4cf | 885 | if (usb_endpoint_xfer_bulk(desc) || |
52fcc0be | 886 | usb_endpoint_xfer_int(desc)) { |
a97ea994 FB |
887 | struct dwc3_gadget_ep_cmd_params params; |
888 | struct dwc3_trb *trb; | |
889 | dma_addr_t trb_dma; | |
890 | u32 cmd; | |
891 | ||
892 | memset(¶ms, 0, sizeof(params)); | |
893 | trb = &dep->trb_pool[0]; | |
894 | trb_dma = dwc3_trb_dma_offset(dep, trb); | |
895 | ||
896 | params.param0 = upper_32_bits(trb_dma); | |
897 | params.param1 = lower_32_bits(trb_dma); | |
898 | ||
899 | cmd = DWC3_DEPCMD_STARTTRANSFER; | |
900 | ||
901 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); | |
902 | if (ret < 0) | |
903 | return ret; | |
140ca4cf TN |
904 | |
905 | if (dep->stream_capable) { | |
906 | /* | |
907 | * For streams, at start, there maybe a race where the | |
908 | * host primes the endpoint before the function driver | |
909 | * queues a request to initiate a stream. In that case, | |
910 | * the controller will not see the prime to generate the | |
911 | * ERDY and start stream. To workaround this, issue a | |
912 | * no-op TRB as normal, but end it immediately. As a | |
913 | * result, when the function driver queues the request, | |
914 | * the next START_TRANSFER command will cause the | |
915 | * controller to generate an ERDY to initiate the | |
916 | * stream. | |
917 | */ | |
918 | dwc3_stop_active_transfer(dep, true, true); | |
919 | ||
920 | /* | |
921 | * All stream eps will reinitiate stream on NoStream | |
922 | * rejection until we can determine that the host can | |
923 | * prime after the first transfer. | |
ddae7979 TN |
924 | * |
925 | * However, if the controller is capable of | |
926 | * TXF_FLUSH_BYPASS, then IN direction endpoints will | |
927 | * automatically restart the stream without the driver | |
928 | * initiation. | |
140ca4cf | 929 | */ |
ddae7979 TN |
930 | if (!dep->direction || |
931 | !(dwc->hwparams.hwparams9 & | |
932 | DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS)) | |
933 | dep->flags |= DWC3_EP_FORCE_RESTART_STREAM; | |
140ca4cf | 934 | } |
a97ea994 FB |
935 | } |
936 | ||
2870e501 FB |
937 | out: |
938 | trace_dwc3_gadget_ep_enable(dep); | |
939 | ||
72246da4 FB |
940 | return 0; |
941 | } | |
942 | ||
624407f9 | 943 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) |
72246da4 FB |
944 | { |
945 | struct dwc3_request *req; | |
946 | ||
c5353b22 | 947 | dwc3_stop_active_transfer(dep, true, false); |
624407f9 | 948 | |
0e146028 FB |
949 | /* - giveback all requests to gadget driver */ |
950 | while (!list_empty(&dep->started_list)) { | |
951 | req = next_request(&dep->started_list); | |
1591633e | 952 | |
0e146028 | 953 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
ea53b882 FB |
954 | } |
955 | ||
aa3342c8 FB |
956 | while (!list_empty(&dep->pending_list)) { |
957 | req = next_request(&dep->pending_list); | |
72246da4 | 958 | |
d8eca64e FB |
959 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
960 | } | |
961 | ||
962 | while (!list_empty(&dep->cancelled_list)) { | |
963 | req = next_request(&dep->cancelled_list); | |
964 | ||
624407f9 | 965 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
72246da4 | 966 | } |
72246da4 FB |
967 | } |
968 | ||
969 | /** | |
bfad65ee | 970 | * __dwc3_gadget_ep_disable - disables a hw endpoint |
72246da4 FB |
971 | * @dep: the endpoint to disable |
972 | * | |
bfad65ee FB |
973 | * This function undoes what __dwc3_gadget_ep_enable did and also removes |
974 | * requests which are currently being processed by the hardware and those which | |
975 | * are not yet scheduled. | |
976 | * | |
624407f9 | 977 | * Caller should take care of locking. |
72246da4 | 978 | */ |
72246da4 FB |
979 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) |
980 | { | |
981 | struct dwc3 *dwc = dep->dwc; | |
982 | u32 reg; | |
983 | ||
2870e501 | 984 | trace_dwc3_gadget_ep_disable(dep); |
7eaeac5c | 985 | |
687ef981 FB |
986 | /* make sure HW endpoint isn't stalled */ |
987 | if (dep->flags & DWC3_EP_STALL) | |
7a608559 | 988 | __dwc3_gadget_ep_set_halt(dep, 0, false); |
687ef981 | 989 | |
72246da4 FB |
990 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
991 | reg &= ~DWC3_DALEPENA_EP(dep->number); | |
992 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); | |
993 | ||
39ebb05c JY |
994 | /* Clear out the ep descriptors for non-ep0 */ |
995 | if (dep->number > 1) { | |
996 | dep->endpoint.comp_desc = NULL; | |
997 | dep->endpoint.desc = NULL; | |
998 | } | |
999 | ||
f09ddcfc WC |
1000 | dwc3_remove_requests(dwc, dep); |
1001 | ||
5aef6297 WC |
1002 | dep->stream_capable = false; |
1003 | dep->type = 0; | |
876a75cb | 1004 | dep->flags &= DWC3_EP_TXFIFO_RESIZED; |
5aef6297 | 1005 | |
72246da4 FB |
1006 | return 0; |
1007 | } | |
1008 | ||
1009 | /* -------------------------------------------------------------------------- */ | |
1010 | ||
1011 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, | |
1012 | const struct usb_endpoint_descriptor *desc) | |
1013 | { | |
1014 | return -EINVAL; | |
1015 | } | |
1016 | ||
1017 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) | |
1018 | { | |
1019 | return -EINVAL; | |
1020 | } | |
1021 | ||
1022 | /* -------------------------------------------------------------------------- */ | |
1023 | ||
1024 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, | |
1025 | const struct usb_endpoint_descriptor *desc) | |
1026 | { | |
1027 | struct dwc3_ep *dep; | |
1028 | struct dwc3 *dwc; | |
1029 | unsigned long flags; | |
1030 | int ret; | |
1031 | ||
1032 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { | |
1033 | pr_debug("dwc3: invalid parameters\n"); | |
1034 | return -EINVAL; | |
1035 | } | |
1036 | ||
1037 | if (!desc->wMaxPacketSize) { | |
1038 | pr_debug("dwc3: missing wMaxPacketSize\n"); | |
1039 | return -EINVAL; | |
1040 | } | |
1041 | ||
1042 | dep = to_dwc3_ep(ep); | |
1043 | dwc = dep->dwc; | |
1044 | ||
95ca961c FB |
1045 | if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, |
1046 | "%s is already enabled\n", | |
1047 | dep->name)) | |
c6f83f38 | 1048 | return 0; |
c6f83f38 | 1049 | |
72246da4 | 1050 | spin_lock_irqsave(&dwc->lock, flags); |
a2d23f08 | 1051 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
72246da4 FB |
1052 | spin_unlock_irqrestore(&dwc->lock, flags); |
1053 | ||
1054 | return ret; | |
1055 | } | |
1056 | ||
1057 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) | |
1058 | { | |
1059 | struct dwc3_ep *dep; | |
1060 | struct dwc3 *dwc; | |
1061 | unsigned long flags; | |
1062 | int ret; | |
1063 | ||
1064 | if (!ep) { | |
1065 | pr_debug("dwc3: invalid parameters\n"); | |
1066 | return -EINVAL; | |
1067 | } | |
1068 | ||
1069 | dep = to_dwc3_ep(ep); | |
1070 | dwc = dep->dwc; | |
1071 | ||
95ca961c FB |
1072 | if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), |
1073 | "%s is already disabled\n", | |
1074 | dep->name)) | |
72246da4 | 1075 | return 0; |
72246da4 | 1076 | |
72246da4 FB |
1077 | spin_lock_irqsave(&dwc->lock, flags); |
1078 | ret = __dwc3_gadget_ep_disable(dep); | |
1079 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1080 | ||
1081 | return ret; | |
1082 | } | |
1083 | ||
1084 | static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, | |
0bd0f6d2 | 1085 | gfp_t gfp_flags) |
72246da4 FB |
1086 | { |
1087 | struct dwc3_request *req; | |
1088 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
72246da4 FB |
1089 | |
1090 | req = kzalloc(sizeof(*req), gfp_flags); | |
734d5a53 | 1091 | if (!req) |
72246da4 | 1092 | return NULL; |
72246da4 | 1093 | |
31a2f5a7 | 1094 | req->direction = dep->direction; |
72246da4 FB |
1095 | req->epnum = dep->number; |
1096 | req->dep = dep; | |
a3af5e3a | 1097 | req->status = DWC3_REQUEST_STATUS_UNKNOWN; |
72246da4 | 1098 | |
2c4cbe6e FB |
1099 | trace_dwc3_alloc_request(req); |
1100 | ||
72246da4 FB |
1101 | return &req->request; |
1102 | } | |
1103 | ||
1104 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, | |
1105 | struct usb_request *request) | |
1106 | { | |
1107 | struct dwc3_request *req = to_dwc3_request(request); | |
1108 | ||
2c4cbe6e | 1109 | trace_dwc3_free_request(req); |
72246da4 FB |
1110 | kfree(req); |
1111 | } | |
1112 | ||
42626919 FB |
1113 | /** |
1114 | * dwc3_ep_prev_trb - returns the previous TRB in the ring | |
1115 | * @dep: The endpoint with the TRB ring | |
1116 | * @index: The index of the current TRB in the ring | |
1117 | * | |
1118 | * Returns the TRB prior to the one pointed to by the index. If the | |
1119 | * index is 0, we will wrap backwards, skip the link TRB, and return | |
1120 | * the one just before that. | |
1121 | */ | |
1122 | static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) | |
1123 | { | |
1124 | u8 tmp = index; | |
1125 | ||
1126 | if (!tmp) | |
1127 | tmp = DWC3_TRB_NUM - 1; | |
1128 | ||
1129 | return &dep->trb_pool[tmp - 1]; | |
1130 | } | |
1131 | ||
1132 | static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) | |
1133 | { | |
42626919 FB |
1134 | u8 trbs_left; |
1135 | ||
1136 | /* | |
51f1954a TN |
1137 | * If the enqueue & dequeue are equal then the TRB ring is either full |
1138 | * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs | |
1139 | * pending to be processed by the driver. | |
42626919 FB |
1140 | */ |
1141 | if (dep->trb_enqueue == dep->trb_dequeue) { | |
51f1954a TN |
1142 | /* |
1143 | * If there is any request remained in the started_list at | |
1144 | * this point, that means there is no TRB available. | |
1145 | */ | |
1146 | if (!list_empty(&dep->started_list)) | |
42626919 FB |
1147 | return 0; |
1148 | ||
1149 | return DWC3_TRB_NUM - 1; | |
1150 | } | |
1151 | ||
1152 | trbs_left = dep->trb_dequeue - dep->trb_enqueue; | |
1153 | trbs_left &= (DWC3_TRB_NUM - 1); | |
1154 | ||
1155 | if (dep->trb_dequeue < dep->trb_enqueue) | |
1156 | trbs_left--; | |
1157 | ||
1158 | return trbs_left; | |
1159 | } | |
2c78c029 | 1160 | |
e49d3cf4 | 1161 | static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb, |
e319bd62 FB |
1162 | dma_addr_t dma, unsigned int length, unsigned int chain, |
1163 | unsigned int node, unsigned int stream_id, | |
1164 | unsigned int short_not_ok, unsigned int no_interrupt, | |
f9cc581b | 1165 | unsigned int is_last, bool must_interrupt) |
c71fc37c | 1166 | { |
6b9018d4 | 1167 | struct dwc3 *dwc = dep->dwc; |
e81a7018 | 1168 | struct usb_gadget *gadget = dwc->gadget; |
6b9018d4 | 1169 | enum usb_device_speed speed = gadget->speed; |
c71fc37c | 1170 | |
f6bafc6a FB |
1171 | trb->size = DWC3_TRB_SIZE_LENGTH(length); |
1172 | trb->bpl = lower_32_bits(dma); | |
1173 | trb->bph = upper_32_bits(dma); | |
c71fc37c | 1174 | |
16e78db7 | 1175 | switch (usb_endpoint_type(dep->endpoint.desc)) { |
c71fc37c | 1176 | case USB_ENDPOINT_XFER_CONTROL: |
f6bafc6a | 1177 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; |
c71fc37c FB |
1178 | break; |
1179 | ||
1180 | case USB_ENDPOINT_XFER_ISOC: | |
6b9018d4 | 1181 | if (!node) { |
e5ba5ec8 | 1182 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; |
6b9018d4 | 1183 | |
40d829fb MG |
1184 | /* |
1185 | * USB Specification 2.0 Section 5.9.2 states that: "If | |
1186 | * there is only a single transaction in the microframe, | |
1187 | * only a DATA0 data packet PID is used. If there are | |
1188 | * two transactions per microframe, DATA1 is used for | |
1189 | * the first transaction data packet and DATA0 is used | |
1190 | * for the second transaction data packet. If there are | |
1191 | * three transactions per microframe, DATA2 is used for | |
1192 | * the first transaction data packet, DATA1 is used for | |
1193 | * the second, and DATA0 is used for the third." | |
1194 | * | |
1195 | * IOW, we should satisfy the following cases: | |
1196 | * | |
1197 | * 1) length <= maxpacket | |
1198 | * - DATA0 | |
1199 | * | |
1200 | * 2) maxpacket < length <= (2 * maxpacket) | |
1201 | * - DATA1, DATA0 | |
1202 | * | |
1203 | * 3) (2 * maxpacket) < length <= (3 * maxpacket) | |
1204 | * - DATA2, DATA1, DATA0 | |
1205 | */ | |
6b9018d4 FB |
1206 | if (speed == USB_SPEED_HIGH) { |
1207 | struct usb_ep *ep = &dep->endpoint; | |
ec5bb87e | 1208 | unsigned int mult = 2; |
40d829fb MG |
1209 | unsigned int maxp = usb_endpoint_maxp(ep->desc); |
1210 | ||
1211 | if (length <= (2 * maxp)) | |
1212 | mult--; | |
1213 | ||
1214 | if (length <= maxp) | |
1215 | mult--; | |
1216 | ||
1217 | trb->size |= DWC3_TRB_SIZE_PCM1(mult); | |
6b9018d4 FB |
1218 | } |
1219 | } else { | |
e5ba5ec8 | 1220 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; |
6b9018d4 | 1221 | } |
ca4d44ea FB |
1222 | |
1223 | /* always enable Interrupt on Missed ISOC */ | |
1224 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; | |
c71fc37c FB |
1225 | break; |
1226 | ||
1227 | case USB_ENDPOINT_XFER_BULK: | |
1228 | case USB_ENDPOINT_XFER_INT: | |
f6bafc6a | 1229 | trb->ctrl = DWC3_TRBCTL_NORMAL; |
c71fc37c FB |
1230 | break; |
1231 | default: | |
1232 | /* | |
1233 | * This is only possible with faulty memory because we | |
1234 | * checked it already :) | |
1235 | */ | |
0a695d4c FB |
1236 | dev_WARN(dwc->dev, "Unknown endpoint type %d\n", |
1237 | usb_endpoint_type(dep->endpoint.desc)); | |
c71fc37c FB |
1238 | } |
1239 | ||
244add8e TJ |
1240 | /* |
1241 | * Enable Continue on Short Packet | |
1242 | * when endpoint is not a stream capable | |
1243 | */ | |
c9508c8c | 1244 | if (usb_endpoint_dir_out(dep->endpoint.desc)) { |
244add8e TJ |
1245 | if (!dep->stream_capable) |
1246 | trb->ctrl |= DWC3_TRB_CTRL_CSP; | |
f3af3651 | 1247 | |
e49d3cf4 | 1248 | if (short_not_ok) |
c9508c8c FB |
1249 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
1250 | } | |
1251 | ||
8dbbe48c | 1252 | if ((!no_interrupt && !chain) || must_interrupt) |
c9508c8c | 1253 | trb->ctrl |= DWC3_TRB_CTRL_IOC; |
f3af3651 | 1254 | |
e5ba5ec8 PA |
1255 | if (chain) |
1256 | trb->ctrl |= DWC3_TRB_CTRL_CHN; | |
3eaecd0c TN |
1257 | else if (dep->stream_capable && is_last) |
1258 | trb->ctrl |= DWC3_TRB_CTRL_LST; | |
e5ba5ec8 | 1259 | |
16e78db7 | 1260 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) |
e49d3cf4 | 1261 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); |
c71fc37c | 1262 | |
f6bafc6a | 1263 | trb->ctrl |= DWC3_TRB_CTRL_HWO; |
2c4cbe6e | 1264 | |
b7a4fbe2 AKV |
1265 | dwc3_ep_inc_enq(dep); |
1266 | ||
2c4cbe6e | 1267 | trace_dwc3_prepare_trb(dep, trb); |
c71fc37c FB |
1268 | } |
1269 | ||
e49d3cf4 FB |
1270 | /** |
1271 | * dwc3_prepare_one_trb - setup one TRB from one request | |
1272 | * @dep: endpoint for which this request is prepared | |
1273 | * @req: dwc3_request pointer | |
5d187c04 | 1274 | * @trb_length: buffer size of the TRB |
e49d3cf4 FB |
1275 | * @chain: should this TRB be chained to the next? |
1276 | * @node: only for isochronous endpoints. First TRB needs different type. | |
2b80357b | 1277 | * @use_bounce_buffer: set to use bounce buffer |
f9cc581b | 1278 | * @must_interrupt: set to interrupt on TRB completion |
e49d3cf4 FB |
1279 | */ |
1280 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, | |
5d187c04 | 1281 | struct dwc3_request *req, unsigned int trb_length, |
f9cc581b TN |
1282 | unsigned int chain, unsigned int node, bool use_bounce_buffer, |
1283 | bool must_interrupt) | |
e49d3cf4 FB |
1284 | { |
1285 | struct dwc3_trb *trb; | |
a31e63b6 | 1286 | dma_addr_t dma; |
e319bd62 FB |
1287 | unsigned int stream_id = req->request.stream_id; |
1288 | unsigned int short_not_ok = req->request.short_not_ok; | |
1289 | unsigned int no_interrupt = req->request.no_interrupt; | |
1290 | unsigned int is_last = req->request.is_last; | |
a31e63b6 | 1291 | |
2b80357b TN |
1292 | if (use_bounce_buffer) |
1293 | dma = dep->dwc->bounce_addr; | |
1294 | else if (req->request.num_sgs > 0) | |
a31e63b6 | 1295 | dma = sg_dma_address(req->start_sg); |
5d187c04 | 1296 | else |
a31e63b6 | 1297 | dma = req->request.dma; |
e49d3cf4 FB |
1298 | |
1299 | trb = &dep->trb_pool[dep->trb_enqueue]; | |
1300 | ||
1301 | if (!req->trb) { | |
1302 | dwc3_gadget_move_started_request(req); | |
1303 | req->trb = trb; | |
1304 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); | |
e49d3cf4 FB |
1305 | } |
1306 | ||
09fe1f8d FB |
1307 | req->num_trbs++; |
1308 | ||
5d187c04 | 1309 | __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node, |
f9cc581b TN |
1310 | stream_id, short_not_ok, no_interrupt, is_last, |
1311 | must_interrupt); | |
1312 | } | |
1313 | ||
1314 | static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req) | |
1315 | { | |
1316 | unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); | |
1317 | unsigned int rem = req->request.length % maxp; | |
1318 | ||
1319 | if ((req->request.length && req->request.zero && !rem && | |
1320 | !usb_endpoint_xfer_isoc(dep->endpoint.desc)) || | |
1321 | (!req->direction && rem)) | |
1322 | return true; | |
1323 | ||
1324 | return false; | |
e49d3cf4 FB |
1325 | } |
1326 | ||
cb1b3997 TN |
1327 | /** |
1328 | * dwc3_prepare_last_sg - prepare TRBs for the last SG entry | |
1329 | * @dep: The endpoint that the request belongs to | |
1330 | * @req: The request to prepare | |
1331 | * @entry_length: The last SG entry size | |
1332 | * @node: Indicates whether this is not the first entry (for isoc only) | |
1333 | * | |
1334 | * Return the number of TRBs prepared. | |
1335 | */ | |
1336 | static int dwc3_prepare_last_sg(struct dwc3_ep *dep, | |
1337 | struct dwc3_request *req, unsigned int entry_length, | |
1338 | unsigned int node) | |
1339 | { | |
1340 | unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); | |
1341 | unsigned int rem = req->request.length % maxp; | |
1342 | unsigned int num_trbs = 1; | |
1343 | ||
f9cc581b | 1344 | if (dwc3_needs_extra_trb(dep, req)) |
cb1b3997 TN |
1345 | num_trbs++; |
1346 | ||
1347 | if (dwc3_calc_trbs_left(dep) < num_trbs) | |
1348 | return 0; | |
1349 | ||
1350 | req->needs_extra_trb = num_trbs > 1; | |
1351 | ||
1352 | /* Prepare a normal TRB */ | |
1353 | if (req->direction || req->request.length) | |
1354 | dwc3_prepare_one_trb(dep, req, entry_length, | |
f9cc581b | 1355 | req->needs_extra_trb, node, false, false); |
cb1b3997 TN |
1356 | |
1357 | /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */ | |
1358 | if ((!req->direction && !req->request.length) || req->needs_extra_trb) | |
1359 | dwc3_prepare_one_trb(dep, req, | |
1360 | req->direction ? 0 : maxp - rem, | |
f9cc581b | 1361 | false, 1, true, false); |
cb1b3997 TN |
1362 | |
1363 | return num_trbs; | |
1364 | } | |
1365 | ||
7f2958d9 | 1366 | static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep, |
7ae7df49 | 1367 | struct dwc3_request *req) |
5ee85d89 | 1368 | { |
a31e63b6 | 1369 | struct scatterlist *sg = req->start_sg; |
5ee85d89 | 1370 | struct scatterlist *s; |
5ee85d89 | 1371 | int i; |
5d187c04 | 1372 | unsigned int length = req->request.length; |
c96e6725 AKV |
1373 | unsigned int remaining = req->request.num_mapped_sgs |
1374 | - req->num_queued_sgs; | |
13111fcb | 1375 | unsigned int num_trbs = req->num_trbs; |
f9cc581b | 1376 | bool needs_extra_trb = dwc3_needs_extra_trb(dep, req); |
c96e6725 | 1377 | |
5d187c04 TN |
1378 | /* |
1379 | * If we resume preparing the request, then get the remaining length of | |
1380 | * the request and resume where we left off. | |
1381 | */ | |
1382 | for_each_sg(req->request.sg, s, req->num_queued_sgs, i) | |
1383 | length -= sg_dma_len(s); | |
1384 | ||
c96e6725 | 1385 | for_each_sg(sg, s, remaining, i) { |
8dbbe48c | 1386 | unsigned int num_trbs_left = dwc3_calc_trbs_left(dep); |
5d187c04 | 1387 | unsigned int trb_length; |
f9cc581b | 1388 | bool must_interrupt = false; |
cb1b3997 | 1389 | bool last_sg = false; |
5ee85d89 | 1390 | |
5d187c04 TN |
1391 | trb_length = min_t(unsigned int, length, sg_dma_len(s)); |
1392 | ||
1393 | length -= trb_length; | |
1394 | ||
dad2aff3 PP |
1395 | /* |
1396 | * IOMMU driver is coalescing the list of sgs which shares a | |
1397 | * page boundary into one and giving it to USB driver. With | |
1398 | * this the number of sgs mapped is not equal to the number of | |
1399 | * sgs passed. So mark the chain bit to false if it isthe last | |
1400 | * mapped sg. | |
1401 | */ | |
5d187c04 | 1402 | if ((i == remaining - 1) || !length) |
cb1b3997 | 1403 | last_sg = true; |
5ee85d89 | 1404 | |
8dbbe48c | 1405 | if (!num_trbs_left) |
13111fcb TN |
1406 | break; |
1407 | ||
cb1b3997 TN |
1408 | if (last_sg) { |
1409 | if (!dwc3_prepare_last_sg(dep, req, trb_length, i)) | |
f9cc581b | 1410 | break; |
c6267a51 | 1411 | } else { |
f9cc581b TN |
1412 | /* |
1413 | * Look ahead to check if we have enough TRBs for the | |
8dbbe48c TN |
1414 | * next SG entry. If not, set interrupt on this TRB to |
1415 | * resume preparing the next SG entry when more TRBs are | |
f9cc581b TN |
1416 | * free. |
1417 | */ | |
8dbbe48c TN |
1418 | if (num_trbs_left == 1 || (needs_extra_trb && |
1419 | num_trbs_left <= 2 && | |
1420 | sg_dma_len(sg_next(s)) >= length)) | |
f9cc581b TN |
1421 | must_interrupt = true; |
1422 | ||
1423 | dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false, | |
1424 | must_interrupt); | |
c6267a51 | 1425 | } |
5ee85d89 | 1426 | |
a31e63b6 AKV |
1427 | /* |
1428 | * There can be a situation where all sgs in sglist are not | |
1429 | * queued because of insufficient trb number. To handle this | |
1430 | * case, update start_sg to next sg to be queued, so that | |
1431 | * we have free trbs we can continue queuing from where we | |
1432 | * previously stopped | |
1433 | */ | |
cb1b3997 | 1434 | if (!last_sg) |
a31e63b6 AKV |
1435 | req->start_sg = sg_next(s); |
1436 | ||
c96e6725 | 1437 | req->num_queued_sgs++; |
25dda9fc | 1438 | req->num_pending_sgs--; |
c96e6725 | 1439 | |
5d187c04 TN |
1440 | /* |
1441 | * The number of pending SG entries may not correspond to the | |
1442 | * number of mapped SG entries. If all the data are queued, then | |
1443 | * don't include unused SG entries. | |
1444 | */ | |
1445 | if (length == 0) { | |
25dda9fc | 1446 | req->num_pending_sgs = 0; |
5d187c04 TN |
1447 | break; |
1448 | } | |
1449 | ||
8dbbe48c | 1450 | if (must_interrupt) |
5ee85d89 FB |
1451 | break; |
1452 | } | |
13111fcb | 1453 | |
13111fcb | 1454 | return req->num_trbs - num_trbs; |
5ee85d89 FB |
1455 | } |
1456 | ||
7f2958d9 | 1457 | static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep, |
7ae7df49 | 1458 | struct dwc3_request *req) |
5ee85d89 | 1459 | { |
cb1b3997 | 1460 | return dwc3_prepare_last_sg(dep, req, req->request.length, 0); |
5ee85d89 FB |
1461 | } |
1462 | ||
72246da4 FB |
1463 | /* |
1464 | * dwc3_prepare_trbs - setup TRBs from requests | |
1465 | * @dep: endpoint for which requests are being prepared | |
72246da4 | 1466 | * |
1d046793 PZ |
1467 | * The function goes through the requests list and sets up TRBs for the |
1468 | * transfers. The function returns once there are no more TRBs available or | |
1469 | * it runs out of requests. | |
490410b2 TN |
1470 | * |
1471 | * Returns the number of TRBs prepared or negative errno. | |
72246da4 | 1472 | */ |
490410b2 | 1473 | static int dwc3_prepare_trbs(struct dwc3_ep *dep) |
72246da4 | 1474 | { |
68e823e2 | 1475 | struct dwc3_request *req, *n; |
490410b2 | 1476 | int ret = 0; |
72246da4 FB |
1477 | |
1478 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); | |
1479 | ||
d86c5a67 FB |
1480 | /* |
1481 | * We can get in a situation where there's a request in the started list | |
1482 | * but there weren't enough TRBs to fully kick it in the first time | |
1483 | * around, so it has been waiting for more TRBs to be freed up. | |
1484 | * | |
1485 | * In that case, we should check if we have a request with pending_sgs | |
1486 | * in the started list and prepare TRBs for that request first, | |
1487 | * otherwise we will prepare TRBs completely out of order and that will | |
1488 | * break things. | |
1489 | */ | |
1490 | list_for_each_entry(req, &dep->started_list, list) { | |
490410b2 | 1491 | if (req->num_pending_sgs > 0) { |
7f2958d9 | 1492 | ret = dwc3_prepare_trbs_sg(dep, req); |
346a15cd | 1493 | if (!ret || req->num_pending_sgs) |
490410b2 TN |
1494 | return ret; |
1495 | } | |
d86c5a67 FB |
1496 | |
1497 | if (!dwc3_calc_trbs_left(dep)) | |
490410b2 | 1498 | return ret; |
63c7bb29 TN |
1499 | |
1500 | /* | |
1501 | * Don't prepare beyond a transfer. In DWC_usb32, its transfer | |
1502 | * burst capability may try to read and use TRBs beyond the | |
1503 | * active transfer instead of stopping. | |
1504 | */ | |
1505 | if (dep->stream_capable && req->request.is_last) | |
490410b2 | 1506 | return ret; |
d86c5a67 FB |
1507 | } |
1508 | ||
aa3342c8 | 1509 | list_for_each_entry_safe(req, n, &dep->pending_list, list) { |
cdb55b39 | 1510 | struct dwc3 *dwc = dep->dwc; |
cdb55b39 FB |
1511 | |
1512 | ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, | |
1513 | dep->direction); | |
1514 | if (ret) | |
490410b2 | 1515 | return ret; |
cdb55b39 FB |
1516 | |
1517 | req->sg = req->request.sg; | |
a31e63b6 | 1518 | req->start_sg = req->sg; |
c96e6725 | 1519 | req->num_queued_sgs = 0; |
cdb55b39 FB |
1520 | req->num_pending_sgs = req->request.num_mapped_sgs; |
1521 | ||
346a15cd | 1522 | if (req->num_pending_sgs > 0) { |
7f2958d9 | 1523 | ret = dwc3_prepare_trbs_sg(dep, req); |
346a15cd TN |
1524 | if (req->num_pending_sgs) |
1525 | return ret; | |
1526 | } else { | |
7f2958d9 | 1527 | ret = dwc3_prepare_trbs_linear(dep, req); |
346a15cd | 1528 | } |
72246da4 | 1529 | |
490410b2 TN |
1530 | if (!ret || !dwc3_calc_trbs_left(dep)) |
1531 | return ret; | |
aefe3d23 TN |
1532 | |
1533 | /* | |
1534 | * Don't prepare beyond a transfer. In DWC_usb32, its transfer | |
1535 | * burst capability may try to read and use TRBs beyond the | |
1536 | * active transfer instead of stopping. | |
1537 | */ | |
1538 | if (dep->stream_capable && req->request.is_last) | |
490410b2 | 1539 | return ret; |
72246da4 | 1540 | } |
490410b2 TN |
1541 | |
1542 | return ret; | |
72246da4 FB |
1543 | } |
1544 | ||
8d99087c TN |
1545 | static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep); |
1546 | ||
7fdca766 | 1547 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep) |
72246da4 FB |
1548 | { |
1549 | struct dwc3_gadget_ep_cmd_params params; | |
1550 | struct dwc3_request *req; | |
4fae2e3e | 1551 | int starting; |
72246da4 FB |
1552 | int ret; |
1553 | u32 cmd; | |
1554 | ||
d72ecc08 TN |
1555 | /* |
1556 | * Note that it's normal to have no new TRBs prepared (i.e. ret == 0). | |
1557 | * This happens when we need to stop and restart a transfer such as in | |
1558 | * the case of reinitiating a stream or retrying an isoc transfer. | |
1559 | */ | |
490410b2 | 1560 | ret = dwc3_prepare_trbs(dep); |
d72ecc08 | 1561 | if (ret < 0) |
490410b2 | 1562 | return ret; |
ccb94ebf | 1563 | |
1912cbc6 | 1564 | starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); |
72246da4 | 1565 | |
2338484d TN |
1566 | /* |
1567 | * If there's no new TRB prepared and we don't need to restart a | |
1568 | * transfer, there's no need to update the transfer. | |
1569 | */ | |
1570 | if (!ret && !starting) | |
1571 | return ret; | |
1572 | ||
4fae2e3e | 1573 | req = next_request(&dep->started_list); |
72246da4 FB |
1574 | if (!req) { |
1575 | dep->flags |= DWC3_EP_PENDING_REQUEST; | |
1576 | return 0; | |
1577 | } | |
1578 | ||
1579 | memset(¶ms, 0, sizeof(params)); | |
72246da4 | 1580 | |
4fae2e3e | 1581 | if (starting) { |
1877d6c9 PA |
1582 | params.param0 = upper_32_bits(req->trb_dma); |
1583 | params.param1 = lower_32_bits(req->trb_dma); | |
7fdca766 FB |
1584 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
1585 | ||
a7351807 AKV |
1586 | if (dep->stream_capable) |
1587 | cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id); | |
1588 | ||
7fdca766 FB |
1589 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
1590 | cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); | |
1877d6c9 | 1591 | } else { |
b6b1c6db FB |
1592 | cmd = DWC3_DEPCMD_UPDATETRANSFER | |
1593 | DWC3_DEPCMD_PARAM(dep->resource_index); | |
1877d6c9 | 1594 | } |
72246da4 | 1595 | |
2cd4718d | 1596 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
72246da4 | 1597 | if (ret < 0) { |
8d99087c TN |
1598 | struct dwc3_request *tmp; |
1599 | ||
1600 | if (ret == -EAGAIN) | |
1601 | return ret; | |
1602 | ||
1603 | dwc3_stop_active_transfer(dep, true, true); | |
1604 | ||
1605 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) | |
04dd6e76 | 1606 | dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED); |
8d99087c TN |
1607 | |
1608 | /* If ep isn't started, then there's no end transfer pending */ | |
1609 | if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) | |
1610 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); | |
1611 | ||
72246da4 FB |
1612 | return ret; |
1613 | } | |
1614 | ||
e0d19563 TN |
1615 | if (dep->stream_capable && req->request.is_last) |
1616 | dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE; | |
1617 | ||
72246da4 FB |
1618 | return 0; |
1619 | } | |
1620 | ||
6cb2e4e3 FB |
1621 | static int __dwc3_gadget_get_frame(struct dwc3 *dwc) |
1622 | { | |
1623 | u32 reg; | |
1624 | ||
1625 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
1626 | return DWC3_DSTS_SOFFN(reg); | |
1627 | } | |
1628 | ||
d92021f6 TN |
1629 | /** |
1630 | * dwc3_gadget_start_isoc_quirk - workaround invalid frame number | |
1631 | * @dep: isoc endpoint | |
1632 | * | |
1633 | * This function tests for the correct combination of BIT[15:14] from the 16-bit | |
1634 | * microframe number reported by the XferNotReady event for the future frame | |
1635 | * number to start the isoc transfer. | |
1636 | * | |
1637 | * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed | |
1638 | * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the | |
1639 | * XferNotReady event are invalid. The driver uses this number to schedule the | |
1640 | * isochronous transfer and passes it to the START TRANSFER command. Because | |
1641 | * this number is invalid, the command may fail. If BIT[15:14] matches the | |
1642 | * internal 16-bit microframe, the START TRANSFER command will pass and the | |
1643 | * transfer will start at the scheduled time, if it is off by 1, the command | |
1644 | * will still pass, but the transfer will start 2 seconds in the future. For all | |
1645 | * other conditions, the START TRANSFER command will fail with bus-expiry. | |
1646 | * | |
1647 | * In order to workaround this issue, we can test for the correct combination of | |
1648 | * BIT[15:14] by sending START TRANSFER commands with different values of | |
1649 | * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart | |
1650 | * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status. | |
1651 | * As the result, within the 4 possible combinations for BIT[15:14], there will | |
1652 | * be 2 successful and 2 failure START COMMAND status. One of the 2 successful | |
1653 | * command status will result in a 2-second delay start. The smaller BIT[15:14] | |
1654 | * value is the correct combination. | |
1655 | * | |
1656 | * Since there are only 4 outcomes and the results are ordered, we can simply | |
1657 | * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to | |
1658 | * deduce the smaller successful combination. | |
1659 | * | |
1660 | * Let test0 = test status for combination 'b00 and test1 = test status for 'b01 | |
1661 | * of BIT[15:14]. The correct combination is as follow: | |
1662 | * | |
1663 | * if test0 fails and test1 passes, BIT[15:14] is 'b01 | |
1664 | * if test0 fails and test1 fails, BIT[15:14] is 'b10 | |
1665 | * if test0 passes and test1 fails, BIT[15:14] is 'b11 | |
1666 | * if test0 passes and test1 passes, BIT[15:14] is 'b00 | |
1667 | * | |
1668 | * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN | |
1669 | * endpoints. | |
1670 | */ | |
25abad6a | 1671 | static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) |
d92021f6 TN |
1672 | { |
1673 | int cmd_status = 0; | |
1674 | bool test0; | |
1675 | bool test1; | |
1676 | ||
1677 | while (dep->combo_num < 2) { | |
1678 | struct dwc3_gadget_ep_cmd_params params; | |
1679 | u32 test_frame_number; | |
1680 | u32 cmd; | |
1681 | ||
1682 | /* | |
1683 | * Check if we can start isoc transfer on the next interval or | |
1684 | * 4 uframes in the future with BIT[15:14] as dep->combo_num | |
1685 | */ | |
ca143785 | 1686 | test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK; |
d92021f6 TN |
1687 | test_frame_number |= dep->combo_num << 14; |
1688 | test_frame_number += max_t(u32, 4, dep->interval); | |
1689 | ||
1690 | params.param0 = upper_32_bits(dep->dwc->bounce_addr); | |
1691 | params.param1 = lower_32_bits(dep->dwc->bounce_addr); | |
1692 | ||
1693 | cmd = DWC3_DEPCMD_STARTTRANSFER; | |
1694 | cmd |= DWC3_DEPCMD_PARAM(test_frame_number); | |
1695 | cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); | |
1696 | ||
1697 | /* Redo if some other failure beside bus-expiry is received */ | |
1698 | if (cmd_status && cmd_status != -EAGAIN) { | |
1699 | dep->start_cmd_status = 0; | |
1700 | dep->combo_num = 0; | |
25abad6a | 1701 | return 0; |
d92021f6 TN |
1702 | } |
1703 | ||
1704 | /* Store the first test status */ | |
1705 | if (dep->combo_num == 0) | |
1706 | dep->start_cmd_status = cmd_status; | |
1707 | ||
1708 | dep->combo_num++; | |
1709 | ||
1710 | /* | |
1711 | * End the transfer if the START_TRANSFER command is successful | |
1712 | * to wait for the next XferNotReady to test the command again | |
1713 | */ | |
1714 | if (cmd_status == 0) { | |
c5353b22 | 1715 | dwc3_stop_active_transfer(dep, true, true); |
25abad6a | 1716 | return 0; |
d92021f6 TN |
1717 | } |
1718 | } | |
1719 | ||
1720 | /* test0 and test1 are both completed at this point */ | |
1721 | test0 = (dep->start_cmd_status == 0); | |
1722 | test1 = (cmd_status == 0); | |
1723 | ||
1724 | if (!test0 && test1) | |
1725 | dep->combo_num = 1; | |
1726 | else if (!test0 && !test1) | |
1727 | dep->combo_num = 2; | |
1728 | else if (test0 && !test1) | |
1729 | dep->combo_num = 3; | |
1730 | else if (test0 && test1) | |
1731 | dep->combo_num = 0; | |
1732 | ||
ca143785 | 1733 | dep->frame_number &= DWC3_FRNUMBER_MASK; |
d92021f6 TN |
1734 | dep->frame_number |= dep->combo_num << 14; |
1735 | dep->frame_number += max_t(u32, 4, dep->interval); | |
1736 | ||
1737 | /* Reinitialize test variables */ | |
1738 | dep->start_cmd_status = 0; | |
1739 | dep->combo_num = 0; | |
1740 | ||
25abad6a | 1741 | return __dwc3_gadget_kick_transfer(dep); |
d92021f6 TN |
1742 | } |
1743 | ||
25abad6a | 1744 | static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) |
d6d6ec7b | 1745 | { |
c5a7092f | 1746 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
d92021f6 | 1747 | struct dwc3 *dwc = dep->dwc; |
d5370106 FB |
1748 | int ret; |
1749 | int i; | |
d92021f6 | 1750 | |
36f05d36 TN |
1751 | if (list_empty(&dep->pending_list) && |
1752 | list_empty(&dep->started_list)) { | |
f4a53c55 | 1753 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
25abad6a | 1754 | return -EAGAIN; |
d6d6ec7b PA |
1755 | } |
1756 | ||
9af21dd6 TN |
1757 | if (!dwc->dis_start_transfer_quirk && |
1758 | (DWC3_VER_IS_PRIOR(DWC31, 170A) || | |
1759 | DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) { | |
e81a7018 | 1760 | if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction) |
25abad6a | 1761 | return dwc3_gadget_start_isoc_quirk(dep); |
d6d6ec7b PA |
1762 | } |
1763 | ||
c5a7092f | 1764 | if (desc->bInterval <= 14 && |
e81a7018 | 1765 | dwc->gadget->speed >= USB_SPEED_HIGH) { |
c5a7092f MO |
1766 | u32 frame = __dwc3_gadget_get_frame(dwc); |
1767 | bool rollover = frame < | |
1768 | (dep->frame_number & DWC3_FRNUMBER_MASK); | |
1769 | ||
1770 | /* | |
1771 | * frame_number is set from XferNotReady and may be already | |
1772 | * out of date. DSTS only provides the lower 14 bit of the | |
1773 | * current frame number. So add the upper two bits of | |
1774 | * frame_number and handle a possible rollover. | |
1775 | * This will provide the correct frame_number unless more than | |
1776 | * rollover has happened since XferNotReady. | |
1777 | */ | |
1778 | ||
1779 | dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) | | |
1780 | frame; | |
1781 | if (rollover) | |
1782 | dep->frame_number += BIT(14); | |
1783 | } | |
1784 | ||
d5370106 FB |
1785 | for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) { |
1786 | dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1); | |
1787 | ||
1788 | ret = __dwc3_gadget_kick_transfer(dep); | |
1789 | if (ret != -EAGAIN) | |
1790 | break; | |
1791 | } | |
1792 | ||
36f05d36 TN |
1793 | /* |
1794 | * After a number of unsuccessful start attempts due to bus-expiry | |
1795 | * status, issue END_TRANSFER command and retry on the next XferNotReady | |
1796 | * event. | |
1797 | */ | |
1798 | if (ret == -EAGAIN) { | |
1799 | struct dwc3_gadget_ep_cmd_params params; | |
1800 | u32 cmd; | |
1801 | ||
1802 | cmd = DWC3_DEPCMD_ENDTRANSFER | | |
1803 | DWC3_DEPCMD_CMDIOC | | |
1804 | DWC3_DEPCMD_PARAM(dep->resource_index); | |
1805 | ||
1806 | dep->resource_index = 0; | |
1807 | memset(¶ms, 0, sizeof(params)); | |
1808 | ||
1809 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); | |
1810 | if (!ret) | |
1811 | dep->flags |= DWC3_EP_END_TRANSFER_PENDING; | |
1812 | } | |
1813 | ||
d5370106 | 1814 | return ret; |
d6d6ec7b PA |
1815 | } |
1816 | ||
72246da4 FB |
1817 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
1818 | { | |
0fc9a1be | 1819 | struct dwc3 *dwc = dep->dwc; |
0fc9a1be | 1820 | |
f09ddcfc | 1821 | if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { |
b851f7c7 | 1822 | dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n", |
5eb30ced | 1823 | dep->name); |
bb423984 FB |
1824 | return -ESHUTDOWN; |
1825 | } | |
1826 | ||
04fb365c FB |
1827 | if (WARN(req->dep != dep, "request %pK belongs to '%s'\n", |
1828 | &req->request, req->dep->name)) | |
bb423984 | 1829 | return -EINVAL; |
bb423984 | 1830 | |
b2b6d601 FB |
1831 | if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED, |
1832 | "%s: request %pK already in flight\n", | |
1833 | dep->name, &req->request)) | |
1834 | return -EINVAL; | |
1835 | ||
fc8bb91b FB |
1836 | pm_runtime_get(dwc->dev); |
1837 | ||
72246da4 FB |
1838 | req->request.actual = 0; |
1839 | req->request.status = -EINPROGRESS; | |
72246da4 | 1840 | |
fe84f522 FB |
1841 | trace_dwc3_ep_queue(req); |
1842 | ||
aa3342c8 | 1843 | list_add_tail(&req->list, &dep->pending_list); |
a3af5e3a | 1844 | req->status = DWC3_REQUEST_STATUS_QUEUED; |
72246da4 | 1845 | |
e0d19563 TN |
1846 | if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE) |
1847 | return 0; | |
1848 | ||
c503672a TN |
1849 | /* |
1850 | * Start the transfer only after the END_TRANSFER is completed | |
1851 | * and endpoint STALL is cleared. | |
1852 | */ | |
1853 | if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || | |
1854 | (dep->flags & DWC3_EP_WEDGE) || | |
1855 | (dep->flags & DWC3_EP_STALL)) { | |
da10bcdd TN |
1856 | dep->flags |= DWC3_EP_DELAY_START; |
1857 | return 0; | |
1858 | } | |
1859 | ||
d889c23c FB |
1860 | /* |
1861 | * NOTICE: Isochronous endpoints should NEVER be prestarted. We must | |
1862 | * wait for a XferNotReady event so we will know what's the current | |
1863 | * (micro-)frame number. | |
1864 | * | |
1865 | * Without this trick, we are very, very likely gonna get Bus Expiry | |
1866 | * errors which will force us issue EndTransfer command. | |
1867 | */ | |
1868 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { | |
fe990cea FB |
1869 | if (!(dep->flags & DWC3_EP_PENDING_REQUEST) && |
1870 | !(dep->flags & DWC3_EP_TRANSFER_STARTED)) | |
1871 | return 0; | |
1872 | ||
6cb2e4e3 | 1873 | if ((dep->flags & DWC3_EP_PENDING_REQUEST)) { |
e319bd62 | 1874 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) |
25abad6a | 1875 | return __dwc3_gadget_start_isoc(dep); |
08a36b54 | 1876 | } |
64e01080 | 1877 | } |
b997ada5 | 1878 | |
18ffa988 WC |
1879 | __dwc3_gadget_kick_transfer(dep); |
1880 | ||
1881 | return 0; | |
72246da4 FB |
1882 | } |
1883 | ||
1884 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, | |
1885 | gfp_t gfp_flags) | |
1886 | { | |
1887 | struct dwc3_request *req = to_dwc3_request(request); | |
1888 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
1889 | struct dwc3 *dwc = dep->dwc; | |
1890 | ||
1891 | unsigned long flags; | |
1892 | ||
1893 | int ret; | |
1894 | ||
fdee4eba | 1895 | spin_lock_irqsave(&dwc->lock, flags); |
72246da4 FB |
1896 | ret = __dwc3_gadget_ep_queue(dep, req); |
1897 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1898 | ||
1899 | return ret; | |
1900 | } | |
1901 | ||
7746a8df FB |
1902 | static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req) |
1903 | { | |
1904 | int i; | |
1905 | ||
cb11ea56 TN |
1906 | /* If req->trb is not set, then the request has not started */ |
1907 | if (!req->trb) | |
1908 | return; | |
1909 | ||
7746a8df FB |
1910 | /* |
1911 | * If request was already started, this means we had to | |
1912 | * stop the transfer. With that we also need to ignore | |
1913 | * all TRBs used by the request, however TRBs can only | |
1914 | * be modified after completion of END_TRANSFER | |
1915 | * command. So what we do here is that we wait for | |
1916 | * END_TRANSFER completion and only after that, we jump | |
1917 | * over TRBs by clearing HWO and incrementing dequeue | |
1918 | * pointer. | |
1919 | */ | |
1920 | for (i = 0; i < req->num_trbs; i++) { | |
1921 | struct dwc3_trb *trb; | |
1922 | ||
2dedea03 | 1923 | trb = &dep->trb_pool[dep->trb_dequeue]; |
7746a8df FB |
1924 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
1925 | dwc3_ep_inc_deq(dep); | |
1926 | } | |
c7152763 TN |
1927 | |
1928 | req->num_trbs = 0; | |
7746a8df FB |
1929 | } |
1930 | ||
d4f1afe5 FB |
1931 | static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep) |
1932 | { | |
1933 | struct dwc3_request *req; | |
1934 | struct dwc3_request *tmp; | |
04dd6e76 | 1935 | struct dwc3 *dwc = dep->dwc; |
d4f1afe5 | 1936 | |
664cc971 | 1937 | list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) { |
d4f1afe5 | 1938 | dwc3_gadget_ep_skip_trbs(dep, req); |
04dd6e76 RC |
1939 | switch (req->status) { |
1940 | case DWC3_REQUEST_STATUS_DISCONNECTED: | |
1941 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); | |
1942 | break; | |
1943 | case DWC3_REQUEST_STATUS_DEQUEUED: | |
1944 | dwc3_gadget_giveback(dep, req, -ECONNRESET); | |
1945 | break; | |
1946 | case DWC3_REQUEST_STATUS_STALLED: | |
1947 | dwc3_gadget_giveback(dep, req, -EPIPE); | |
1948 | break; | |
1949 | default: | |
1950 | dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status); | |
1951 | dwc3_gadget_giveback(dep, req, -ECONNRESET); | |
1952 | break; | |
1953 | } | |
d4f1afe5 FB |
1954 | } |
1955 | } | |
1956 | ||
72246da4 FB |
1957 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, |
1958 | struct usb_request *request) | |
1959 | { | |
1960 | struct dwc3_request *req = to_dwc3_request(request); | |
1961 | struct dwc3_request *r = NULL; | |
1962 | ||
1963 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
1964 | struct dwc3 *dwc = dep->dwc; | |
1965 | ||
1966 | unsigned long flags; | |
1967 | int ret = 0; | |
1968 | ||
2c4cbe6e FB |
1969 | trace_dwc3_ep_dequeue(req); |
1970 | ||
72246da4 FB |
1971 | spin_lock_irqsave(&dwc->lock, flags); |
1972 | ||
a7027ca6 | 1973 | list_for_each_entry(r, &dep->cancelled_list, list) { |
72246da4 | 1974 | if (r == req) |
fcd2def6 | 1975 | goto out; |
72246da4 FB |
1976 | } |
1977 | ||
aa3342c8 | 1978 | list_for_each_entry(r, &dep->pending_list, list) { |
fcd2def6 TN |
1979 | if (r == req) { |
1980 | dwc3_gadget_giveback(dep, req, -ECONNRESET); | |
1981 | goto out; | |
72246da4 | 1982 | } |
72246da4 FB |
1983 | } |
1984 | ||
fcd2def6 | 1985 | list_for_each_entry(r, &dep->started_list, list) { |
72246da4 | 1986 | if (r == req) { |
a7027ca6 TN |
1987 | struct dwc3_request *t; |
1988 | ||
72246da4 | 1989 | /* wait until it is processed */ |
c5353b22 | 1990 | dwc3_stop_active_transfer(dep, true, true); |
cf3113d8 | 1991 | |
a7027ca6 TN |
1992 | /* |
1993 | * Remove any started request if the transfer is | |
1994 | * cancelled. | |
1995 | */ | |
1996 | list_for_each_entry_safe(r, t, &dep->started_list, list) | |
04dd6e76 RC |
1997 | dwc3_gadget_move_cancelled_request(r, |
1998 | DWC3_REQUEST_STATUS_DEQUEUED); | |
cf3113d8 | 1999 | |
a5c7682a TN |
2000 | dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; |
2001 | ||
fcd2def6 | 2002 | goto out; |
72246da4 | 2003 | } |
72246da4 FB |
2004 | } |
2005 | ||
fcd2def6 TN |
2006 | dev_err(dwc->dev, "request %pK was not queued to %s\n", |
2007 | request, ep->name); | |
2008 | ret = -EINVAL; | |
2009 | out: | |
72246da4 FB |
2010 | spin_unlock_irqrestore(&dwc->lock, flags); |
2011 | ||
2012 | return ret; | |
2013 | } | |
2014 | ||
7a608559 | 2015 | int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) |
72246da4 FB |
2016 | { |
2017 | struct dwc3_gadget_ep_cmd_params params; | |
2018 | struct dwc3 *dwc = dep->dwc; | |
cb11ea56 TN |
2019 | struct dwc3_request *req; |
2020 | struct dwc3_request *tmp; | |
72246da4 FB |
2021 | int ret; |
2022 | ||
5ad02fb8 FB |
2023 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
2024 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); | |
2025 | return -EINVAL; | |
2026 | } | |
2027 | ||
72246da4 FB |
2028 | memset(¶ms, 0x00, sizeof(params)); |
2029 | ||
2030 | if (value) { | |
69450c4d FB |
2031 | struct dwc3_trb *trb; |
2032 | ||
e319bd62 FB |
2033 | unsigned int transfer_in_flight; |
2034 | unsigned int started; | |
69450c4d FB |
2035 | |
2036 | if (dep->number > 1) | |
2037 | trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); | |
2038 | else | |
2039 | trb = &dwc->ep0_trb[dep->trb_enqueue]; | |
2040 | ||
2041 | transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; | |
2042 | started = !list_empty(&dep->started_list); | |
2043 | ||
2044 | if (!protocol && ((dep->direction && transfer_in_flight) || | |
2045 | (!dep->direction && started))) { | |
7a608559 FB |
2046 | return -EAGAIN; |
2047 | } | |
2048 | ||
2cd4718d FB |
2049 | ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, |
2050 | ¶ms); | |
72246da4 | 2051 | if (ret) |
3f89204b | 2052 | dev_err(dwc->dev, "failed to set STALL on %s\n", |
72246da4 FB |
2053 | dep->name); |
2054 | else | |
2055 | dep->flags |= DWC3_EP_STALL; | |
2056 | } else { | |
cb11ea56 TN |
2057 | /* |
2058 | * Don't issue CLEAR_STALL command to control endpoints. The | |
2059 | * controller automatically clears the STALL when it receives | |
2060 | * the SETUP token. | |
2061 | */ | |
2062 | if (dep->number <= 1) { | |
2063 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); | |
2064 | return 0; | |
2065 | } | |
2cd4718d | 2066 | |
d97c78a1 TN |
2067 | dwc3_stop_active_transfer(dep, true, true); |
2068 | ||
2069 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) | |
04dd6e76 | 2070 | dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED); |
d97c78a1 TN |
2071 | |
2072 | if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) { | |
2073 | dep->flags |= DWC3_EP_PENDING_CLEAR_STALL; | |
2074 | return 0; | |
2075 | } | |
2076 | ||
2077 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); | |
2078 | ||
50c763f8 | 2079 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
cb11ea56 | 2080 | if (ret) { |
3f89204b | 2081 | dev_err(dwc->dev, "failed to clear STALL on %s\n", |
72246da4 | 2082 | dep->name); |
cb11ea56 TN |
2083 | return ret; |
2084 | } | |
2085 | ||
2086 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); | |
2087 | ||
c503672a TN |
2088 | if ((dep->flags & DWC3_EP_DELAY_START) && |
2089 | !usb_endpoint_xfer_isoc(dep->endpoint.desc)) | |
2090 | __dwc3_gadget_kick_transfer(dep); | |
2091 | ||
2092 | dep->flags &= ~DWC3_EP_DELAY_START; | |
72246da4 | 2093 | } |
5275455a | 2094 | |
72246da4 FB |
2095 | return ret; |
2096 | } | |
2097 | ||
2098 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) | |
2099 | { | |
2100 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
2101 | struct dwc3 *dwc = dep->dwc; | |
2102 | ||
2103 | unsigned long flags; | |
2104 | ||
2105 | int ret; | |
2106 | ||
2107 | spin_lock_irqsave(&dwc->lock, flags); | |
7a608559 | 2108 | ret = __dwc3_gadget_ep_set_halt(dep, value, false); |
72246da4 FB |
2109 | spin_unlock_irqrestore(&dwc->lock, flags); |
2110 | ||
2111 | return ret; | |
2112 | } | |
2113 | ||
2114 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) | |
2115 | { | |
2116 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
249a4569 PZ |
2117 | struct dwc3 *dwc = dep->dwc; |
2118 | unsigned long flags; | |
95aa4e8d | 2119 | int ret; |
72246da4 | 2120 | |
249a4569 | 2121 | spin_lock_irqsave(&dwc->lock, flags); |
72246da4 FB |
2122 | dep->flags |= DWC3_EP_WEDGE; |
2123 | ||
08f0d966 | 2124 | if (dep->number == 0 || dep->number == 1) |
95aa4e8d | 2125 | ret = __dwc3_gadget_ep0_set_halt(ep, 1); |
08f0d966 | 2126 | else |
7a608559 | 2127 | ret = __dwc3_gadget_ep_set_halt(dep, 1, false); |
95aa4e8d FB |
2128 | spin_unlock_irqrestore(&dwc->lock, flags); |
2129 | ||
2130 | return ret; | |
72246da4 FB |
2131 | } |
2132 | ||
2133 | /* -------------------------------------------------------------------------- */ | |
2134 | ||
2135 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { | |
2136 | .bLength = USB_DT_ENDPOINT_SIZE, | |
2137 | .bDescriptorType = USB_DT_ENDPOINT, | |
2138 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, | |
2139 | }; | |
2140 | ||
2141 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { | |
2142 | .enable = dwc3_gadget_ep0_enable, | |
2143 | .disable = dwc3_gadget_ep0_disable, | |
2144 | .alloc_request = dwc3_gadget_ep_alloc_request, | |
2145 | .free_request = dwc3_gadget_ep_free_request, | |
2146 | .queue = dwc3_gadget_ep0_queue, | |
2147 | .dequeue = dwc3_gadget_ep_dequeue, | |
08f0d966 | 2148 | .set_halt = dwc3_gadget_ep0_set_halt, |
72246da4 FB |
2149 | .set_wedge = dwc3_gadget_ep_set_wedge, |
2150 | }; | |
2151 | ||
2152 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { | |
2153 | .enable = dwc3_gadget_ep_enable, | |
2154 | .disable = dwc3_gadget_ep_disable, | |
2155 | .alloc_request = dwc3_gadget_ep_alloc_request, | |
2156 | .free_request = dwc3_gadget_ep_free_request, | |
2157 | .queue = dwc3_gadget_ep_queue, | |
2158 | .dequeue = dwc3_gadget_ep_dequeue, | |
2159 | .set_halt = dwc3_gadget_ep_set_halt, | |
2160 | .set_wedge = dwc3_gadget_ep_set_wedge, | |
2161 | }; | |
2162 | ||
2163 | /* -------------------------------------------------------------------------- */ | |
2164 | ||
2165 | static int dwc3_gadget_get_frame(struct usb_gadget *g) | |
2166 | { | |
2167 | struct dwc3 *dwc = gadget_to_dwc(g); | |
72246da4 | 2168 | |
6cb2e4e3 | 2169 | return __dwc3_gadget_get_frame(dwc); |
72246da4 FB |
2170 | } |
2171 | ||
218ef7b6 | 2172 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc) |
72246da4 | 2173 | { |
d6011f6f | 2174 | int retries; |
72246da4 | 2175 | |
218ef7b6 | 2176 | int ret; |
72246da4 FB |
2177 | u32 reg; |
2178 | ||
72246da4 | 2179 | u8 link_state; |
72246da4 | 2180 | |
72246da4 FB |
2181 | /* |
2182 | * According to the Databook Remote wakeup request should | |
2183 | * be issued only when the device is in early suspend state. | |
2184 | * | |
2185 | * We can check that via USB Link State bits in DSTS register. | |
2186 | */ | |
2187 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
2188 | ||
72246da4 FB |
2189 | link_state = DWC3_DSTS_USBLNKST(reg); |
2190 | ||
2191 | switch (link_state) { | |
d0550cd2 | 2192 | case DWC3_LINK_STATE_RESET: |
72246da4 FB |
2193 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ |
2194 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ | |
c560e763 TN |
2195 | case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */ |
2196 | case DWC3_LINK_STATE_U1: | |
d0550cd2 | 2197 | case DWC3_LINK_STATE_RESUME: |
72246da4 FB |
2198 | break; |
2199 | default: | |
218ef7b6 | 2200 | return -EINVAL; |
72246da4 FB |
2201 | } |
2202 | ||
8598bde7 FB |
2203 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
2204 | if (ret < 0) { | |
2205 | dev_err(dwc->dev, "failed to put link in Recovery\n"); | |
218ef7b6 | 2206 | return ret; |
8598bde7 | 2207 | } |
72246da4 | 2208 | |
802fde98 | 2209 | /* Recent versions do this automatically */ |
9af21dd6 | 2210 | if (DWC3_VER_IS_PRIOR(DWC3, 194A)) { |
802fde98 | 2211 | /* write zeroes to Link Change Request */ |
fcc023c7 | 2212 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
802fde98 PZ |
2213 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
2214 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2215 | } | |
72246da4 | 2216 | |
1d046793 | 2217 | /* poll until Link State changes to ON */ |
d6011f6f | 2218 | retries = 20000; |
72246da4 | 2219 | |
d6011f6f | 2220 | while (retries--) { |
72246da4 FB |
2221 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
2222 | ||
2223 | /* in HS, means ON */ | |
2224 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) | |
2225 | break; | |
2226 | } | |
2227 | ||
2228 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { | |
2229 | dev_err(dwc->dev, "failed to send remote wakeup\n"); | |
218ef7b6 | 2230 | return -EINVAL; |
72246da4 FB |
2231 | } |
2232 | ||
218ef7b6 FB |
2233 | return 0; |
2234 | } | |
2235 | ||
2236 | static int dwc3_gadget_wakeup(struct usb_gadget *g) | |
2237 | { | |
2238 | struct dwc3 *dwc = gadget_to_dwc(g); | |
2239 | unsigned long flags; | |
2240 | int ret; | |
2241 | ||
2242 | spin_lock_irqsave(&dwc->lock, flags); | |
2243 | ret = __dwc3_gadget_wakeup(dwc); | |
72246da4 FB |
2244 | spin_unlock_irqrestore(&dwc->lock, flags); |
2245 | ||
2246 | return ret; | |
2247 | } | |
2248 | ||
2249 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, | |
2250 | int is_selfpowered) | |
2251 | { | |
2252 | struct dwc3 *dwc = gadget_to_dwc(g); | |
249a4569 | 2253 | unsigned long flags; |
72246da4 | 2254 | |
249a4569 | 2255 | spin_lock_irqsave(&dwc->lock, flags); |
bcdea503 | 2256 | g->is_selfpowered = !!is_selfpowered; |
249a4569 | 2257 | spin_unlock_irqrestore(&dwc->lock, flags); |
72246da4 FB |
2258 | |
2259 | return 0; | |
2260 | } | |
2261 | ||
ae7e8610 WC |
2262 | static void dwc3_stop_active_transfers(struct dwc3 *dwc) |
2263 | { | |
2264 | u32 epnum; | |
2265 | ||
2266 | for (epnum = 2; epnum < dwc->num_eps; epnum++) { | |
2267 | struct dwc3_ep *dep; | |
2268 | ||
2269 | dep = dwc->eps[epnum]; | |
2270 | if (!dep) | |
2271 | continue; | |
2272 | ||
2273 | dwc3_remove_requests(dwc, dep); | |
2274 | } | |
2275 | } | |
2276 | ||
072cab8a TN |
2277 | static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc) |
2278 | { | |
2279 | enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate; | |
2280 | u32 reg; | |
2281 | ||
2282 | if (ssp_rate == USB_SSP_GEN_UNKNOWN) | |
2283 | ssp_rate = dwc->max_ssp_rate; | |
2284 | ||
2285 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
2286 | reg &= ~DWC3_DCFG_SPEED_MASK; | |
2287 | reg &= ~DWC3_DCFG_NUMLANES(~0); | |
2288 | ||
2289 | if (ssp_rate == USB_SSP_GEN_1x2) | |
2290 | reg |= DWC3_DCFG_SUPERSPEED; | |
2291 | else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2) | |
2292 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; | |
2293 | ||
2294 | if (ssp_rate != USB_SSP_GEN_2x1 && | |
2295 | dwc->max_ssp_rate != USB_SSP_GEN_2x1) | |
2296 | reg |= DWC3_DCFG_NUMLANES(1); | |
2297 | ||
2298 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
2299 | } | |
2300 | ||
7c9a2598 WC |
2301 | static void __dwc3_gadget_set_speed(struct dwc3 *dwc) |
2302 | { | |
450b9e9f | 2303 | enum usb_device_speed speed; |
7c9a2598 WC |
2304 | u32 reg; |
2305 | ||
450b9e9f | 2306 | speed = dwc->gadget_max_speed; |
93f1d43c | 2307 | if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed) |
450b9e9f TN |
2308 | speed = dwc->maximum_speed; |
2309 | ||
2310 | if (speed == USB_SPEED_SUPER_PLUS && | |
072cab8a TN |
2311 | DWC3_IP_IS(DWC32)) { |
2312 | __dwc3_gadget_set_ssp_rate(dwc); | |
2313 | return; | |
2314 | } | |
2315 | ||
7c9a2598 WC |
2316 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
2317 | reg &= ~(DWC3_DCFG_SPEED_MASK); | |
2318 | ||
2319 | /* | |
2320 | * WORKAROUND: DWC3 revision < 2.20a have an issue | |
2321 | * which would cause metastability state on Run/Stop | |
2322 | * bit if we try to force the IP to USB2-only mode. | |
2323 | * | |
2324 | * Because of that, we cannot configure the IP to any | |
2325 | * speed other than the SuperSpeed | |
2326 | * | |
2327 | * Refers to: | |
2328 | * | |
2329 | * STAR#9000525659: Clock Domain Crossing on DCTL in | |
2330 | * USB 2.0 Mode | |
2331 | */ | |
2332 | if (DWC3_VER_IS_PRIOR(DWC3, 220A) && | |
2333 | !dwc->dis_metastability_quirk) { | |
2334 | reg |= DWC3_DCFG_SUPERSPEED; | |
2335 | } else { | |
450b9e9f | 2336 | switch (speed) { |
7c9a2598 WC |
2337 | case USB_SPEED_FULL: |
2338 | reg |= DWC3_DCFG_FULLSPEED; | |
2339 | break; | |
2340 | case USB_SPEED_HIGH: | |
2341 | reg |= DWC3_DCFG_HIGHSPEED; | |
2342 | break; | |
2343 | case USB_SPEED_SUPER: | |
2344 | reg |= DWC3_DCFG_SUPERSPEED; | |
2345 | break; | |
2346 | case USB_SPEED_SUPER_PLUS: | |
2347 | if (DWC3_IP_IS(DWC3)) | |
2348 | reg |= DWC3_DCFG_SUPERSPEED; | |
2349 | else | |
2350 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; | |
2351 | break; | |
2352 | default: | |
450b9e9f | 2353 | dev_err(dwc->dev, "invalid speed (%d)\n", speed); |
7c9a2598 WC |
2354 | |
2355 | if (DWC3_IP_IS(DWC3)) | |
2356 | reg |= DWC3_DCFG_SUPERSPEED; | |
2357 | else | |
2358 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; | |
2359 | } | |
2360 | } | |
f551037c TN |
2361 | |
2362 | if (DWC3_IP_IS(DWC32) && | |
450b9e9f TN |
2363 | speed > USB_SPEED_UNKNOWN && |
2364 | speed < USB_SPEED_SUPER_PLUS) | |
f551037c TN |
2365 | reg &= ~DWC3_DCFG_NUMLANES(~0); |
2366 | ||
7c9a2598 WC |
2367 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
2368 | } | |
2369 | ||
7b2a0368 | 2370 | static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) |
72246da4 FB |
2371 | { |
2372 | u32 reg; | |
61d58242 | 2373 | u32 timeout = 500; |
72246da4 | 2374 | |
fc8bb91b FB |
2375 | if (pm_runtime_suspended(dwc->dev)) |
2376 | return 0; | |
2377 | ||
72246da4 | 2378 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
8db7ed15 | 2379 | if (is_on) { |
9af21dd6 | 2380 | if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) { |
802fde98 PZ |
2381 | reg &= ~DWC3_DCTL_TRGTULST_MASK; |
2382 | reg |= DWC3_DCTL_TRGTULST_RX_DET; | |
2383 | } | |
2384 | ||
9af21dd6 | 2385 | if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) |
802fde98 PZ |
2386 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
2387 | reg |= DWC3_DCTL_RUN_STOP; | |
7b2a0368 FB |
2388 | |
2389 | if (dwc->has_hibernation) | |
2390 | reg |= DWC3_DCTL_KEEP_CONNECT; | |
2391 | ||
7c9a2598 | 2392 | __dwc3_gadget_set_speed(dwc); |
9fcb3bd8 | 2393 | dwc->pullups_connected = true; |
8db7ed15 | 2394 | } else { |
72246da4 | 2395 | reg &= ~DWC3_DCTL_RUN_STOP; |
7b2a0368 FB |
2396 | |
2397 | if (dwc->has_hibernation && !suspend) | |
2398 | reg &= ~DWC3_DCTL_KEEP_CONNECT; | |
2399 | ||
9fcb3bd8 | 2400 | dwc->pullups_connected = false; |
8db7ed15 | 2401 | } |
72246da4 | 2402 | |
5b738211 | 2403 | dwc3_gadget_dctl_write_safe(dwc, reg); |
72246da4 FB |
2404 | |
2405 | do { | |
2406 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
b6d4e16e FB |
2407 | reg &= DWC3_DSTS_DEVCTRLHLT; |
2408 | } while (--timeout && !(!is_on ^ !reg)); | |
f2df679b FB |
2409 | |
2410 | if (!timeout) | |
2411 | return -ETIMEDOUT; | |
72246da4 | 2412 | |
6f17f74b | 2413 | return 0; |
72246da4 FB |
2414 | } |
2415 | ||
ae7e8610 WC |
2416 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc); |
2417 | static void __dwc3_gadget_stop(struct dwc3 *dwc); | |
a1383b35 | 2418 | static int __dwc3_gadget_start(struct dwc3 *dwc); |
ae7e8610 | 2419 | |
72246da4 FB |
2420 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) |
2421 | { | |
2422 | struct dwc3 *dwc = gadget_to_dwc(g); | |
2423 | unsigned long flags; | |
6f17f74b | 2424 | int ret; |
72246da4 FB |
2425 | |
2426 | is_on = !!is_on; | |
8217f07a | 2427 | dwc->softconnect = is_on; |
bb014736 BW |
2428 | /* |
2429 | * Per databook, when we want to stop the gadget, if a control transfer | |
2430 | * is still in process, complete it and get the core into setup phase. | |
2431 | */ | |
2432 | if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) { | |
2433 | reinit_completion(&dwc->ep0_in_setup); | |
2434 | ||
2435 | ret = wait_for_completion_timeout(&dwc->ep0_in_setup, | |
2436 | msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); | |
4a1e25c0 WC |
2437 | if (ret == 0) |
2438 | dev_warn(dwc->dev, "timed out waiting for SETUP phase\n"); | |
bb014736 BW |
2439 | } |
2440 | ||
cb10f68a WC |
2441 | /* |
2442 | * Avoid issuing a runtime resume if the device is already in the | |
2443 | * suspended state during gadget disconnect. DWC3 gadget was already | |
2444 | * halted/stopped during runtime suspend. | |
2445 | */ | |
2446 | if (!is_on) { | |
2447 | pm_runtime_barrier(dwc->dev); | |
2448 | if (pm_runtime_suspended(dwc->dev)) | |
2449 | return 0; | |
2450 | } | |
2451 | ||
77adb8bd WC |
2452 | /* |
2453 | * Check the return value for successful resume, or error. For a | |
2454 | * successful resume, the DWC3 runtime PM resume routine will handle | |
2455 | * the run stop sequence, so avoid duplicate operations here. | |
2456 | */ | |
2457 | ret = pm_runtime_get_sync(dwc->dev); | |
2458 | if (!ret || ret < 0) { | |
2459 | pm_runtime_put(dwc->dev); | |
2460 | return 0; | |
2461 | } | |
2462 | ||
ae7e8610 | 2463 | /* |
82129373 WC |
2464 | * Synchronize and disable any further event handling while controller |
2465 | * is being enabled/disabled. | |
ae7e8610 | 2466 | */ |
82129373 | 2467 | disable_irq(dwc->irq_gadget); |
ae7e8610 | 2468 | |
72246da4 | 2469 | spin_lock_irqsave(&dwc->lock, flags); |
ae7e8610 WC |
2470 | |
2471 | if (!is_on) { | |
2472 | u32 count; | |
2473 | ||
f09ddcfc | 2474 | dwc->connected = false; |
ae7e8610 WC |
2475 | /* |
2476 | * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a | |
2477 | * Section 4.1.8 Table 4-7, it states that for a device-initiated | |
2478 | * disconnect, the SW needs to ensure that it sends "a DEPENDXFER | |
2479 | * command for any active transfers" before clearing the RunStop | |
2480 | * bit. | |
2481 | */ | |
2482 | dwc3_stop_active_transfers(dwc); | |
2483 | __dwc3_gadget_stop(dwc); | |
2484 | ||
2485 | /* | |
2486 | * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a | |
2487 | * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the | |
2488 | * "software needs to acknowledge the events that are generated | |
2489 | * (by writing to GEVNTCOUNTn) while it is waiting for this bit | |
2490 | * to be set to '1'." | |
2491 | */ | |
2492 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); | |
2493 | count &= DWC3_GEVNTCOUNT_MASK; | |
2494 | if (count > 0) { | |
2495 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); | |
2496 | dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) % | |
2497 | dwc->ev_buf->length; | |
2498 | } | |
a1383b35 WC |
2499 | } else { |
2500 | __dwc3_gadget_start(dwc); | |
ae7e8610 WC |
2501 | } |
2502 | ||
7b2a0368 | 2503 | ret = dwc3_gadget_run_stop(dwc, is_on, false); |
72246da4 | 2504 | spin_unlock_irqrestore(&dwc->lock, flags); |
82129373 WC |
2505 | enable_irq(dwc->irq_gadget); |
2506 | ||
77adb8bd | 2507 | pm_runtime_put(dwc->dev); |
72246da4 | 2508 | |
6f17f74b | 2509 | return ret; |
72246da4 FB |
2510 | } |
2511 | ||
8698e2ac FB |
2512 | static void dwc3_gadget_enable_irq(struct dwc3 *dwc) |
2513 | { | |
2514 | u32 reg; | |
2515 | ||
2516 | /* Enable all but Start and End of Frame IRQs */ | |
132ee0da | 2517 | reg = (DWC3_DEVTEN_EVNTOVERFLOWEN | |
8698e2ac FB |
2518 | DWC3_DEVTEN_CMDCMPLTEN | |
2519 | DWC3_DEVTEN_ERRTICERREN | | |
2520 | DWC3_DEVTEN_WKUPEVTEN | | |
8698e2ac FB |
2521 | DWC3_DEVTEN_CONNECTDONEEN | |
2522 | DWC3_DEVTEN_USBRSTEN | | |
2523 | DWC3_DEVTEN_DISCONNEVTEN); | |
2524 | ||
9af21dd6 | 2525 | if (DWC3_VER_IS_PRIOR(DWC3, 250A)) |
799e9dc8 FB |
2526 | reg |= DWC3_DEVTEN_ULSTCNGEN; |
2527 | ||
d1d90dd2 JP |
2528 | /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */ |
2529 | if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) | |
6f26ebb7 | 2530 | reg |= DWC3_DEVTEN_U3L2L1SUSPEN; |
d1d90dd2 | 2531 | |
8698e2ac FB |
2532 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
2533 | } | |
2534 | ||
2535 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc) | |
2536 | { | |
2537 | /* mask all interrupts */ | |
2538 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); | |
2539 | } | |
2540 | ||
2541 | static irqreturn_t dwc3_interrupt(int irq, void *_dwc); | |
b15a762f | 2542 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); |
8698e2ac | 2543 | |
4e99472b | 2544 | /** |
bfad65ee FB |
2545 | * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG |
2546 | * @dwc: pointer to our context structure | |
4e99472b FB |
2547 | * |
2548 | * The following looks like complex but it's actually very simple. In order to | |
2549 | * calculate the number of packets we can burst at once on OUT transfers, we're | |
2550 | * gonna use RxFIFO size. | |
2551 | * | |
2552 | * To calculate RxFIFO size we need two numbers: | |
2553 | * MDWIDTH = size, in bits, of the internal memory bus | |
2554 | * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) | |
2555 | * | |
2556 | * Given these two numbers, the formula is simple: | |
2557 | * | |
2558 | * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; | |
2559 | * | |
2560 | * 24 bytes is for 3x SETUP packets | |
2561 | * 16 bytes is a clock domain crossing tolerance | |
2562 | * | |
2563 | * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; | |
2564 | */ | |
2565 | static void dwc3_gadget_setup_nump(struct dwc3 *dwc) | |
2566 | { | |
2567 | u32 ram2_depth; | |
2568 | u32 mdwidth; | |
2569 | u32 nump; | |
2570 | u32 reg; | |
2571 | ||
2572 | ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); | |
d00be779 | 2573 | mdwidth = dwc3_mdwidth(dwc); |
4e99472b FB |
2574 | |
2575 | nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; | |
2576 | nump = min_t(u32, nump, 16); | |
2577 | ||
2578 | /* update NumP */ | |
2579 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
2580 | reg &= ~DWC3_DCFG_NUMP_MASK; | |
2581 | reg |= nump << DWC3_DCFG_NUMP_SHIFT; | |
2582 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
2583 | } | |
2584 | ||
d7be2952 | 2585 | static int __dwc3_gadget_start(struct dwc3 *dwc) |
72246da4 | 2586 | { |
72246da4 | 2587 | struct dwc3_ep *dep; |
72246da4 FB |
2588 | int ret = 0; |
2589 | u32 reg; | |
2590 | ||
cf40b86b JY |
2591 | /* |
2592 | * Use IMOD if enabled via dwc->imod_interval. Otherwise, if | |
2593 | * the core supports IMOD, disable it. | |
2594 | */ | |
2595 | if (dwc->imod_interval) { | |
2596 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); | |
2597 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); | |
2598 | } else if (dwc3_has_imod(dwc)) { | |
2599 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); | |
2600 | } | |
2601 | ||
2a58f9c1 FB |
2602 | /* |
2603 | * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP | |
2604 | * field instead of letting dwc3 itself calculate that automatically. | |
2605 | * | |
2606 | * This way, we maximize the chances that we'll be able to get several | |
2607 | * bursts of data without going through any sort of endpoint throttling. | |
2608 | */ | |
2609 | reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); | |
9af21dd6 | 2610 | if (DWC3_IP_IS(DWC3)) |
01b0e2cc | 2611 | reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; |
9af21dd6 TN |
2612 | else |
2613 | reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL; | |
01b0e2cc | 2614 | |
2a58f9c1 FB |
2615 | dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); |
2616 | ||
4e99472b FB |
2617 | dwc3_gadget_setup_nump(dwc); |
2618 | ||
e66bbfb0 TN |
2619 | /* |
2620 | * Currently the controller handles single stream only. So, Ignore | |
2621 | * Packet Pending bit for stream selection and don't search for another | |
2622 | * stream if the host sends Data Packet with PP=0 (for OUT direction) or | |
2623 | * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves | |
2624 | * the stream performance. | |
2625 | */ | |
2626 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
2627 | reg |= DWC3_DCFG_IGNSTRMPP; | |
2628 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
2629 | ||
72246da4 FB |
2630 | /* Start with SuperSpeed Default */ |
2631 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); | |
2632 | ||
2633 | dep = dwc->eps[0]; | |
a2d23f08 | 2634 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
72246da4 FB |
2635 | if (ret) { |
2636 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
d7be2952 | 2637 | goto err0; |
72246da4 FB |
2638 | } |
2639 | ||
2640 | dep = dwc->eps[1]; | |
a2d23f08 | 2641 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
72246da4 FB |
2642 | if (ret) { |
2643 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
d7be2952 | 2644 | goto err1; |
72246da4 FB |
2645 | } |
2646 | ||
2647 | /* begin to receive SETUP packets */ | |
c7fcdeb2 | 2648 | dwc->ep0state = EP0_SETUP_PHASE; |
88b1bb1f | 2649 | dwc->link_state = DWC3_LINK_STATE_SS_DIS; |
4a1e25c0 | 2650 | dwc->delayed_status = false; |
72246da4 FB |
2651 | dwc3_ep0_out_start(dwc); |
2652 | ||
8698e2ac FB |
2653 | dwc3_gadget_enable_irq(dwc); |
2654 | ||
72246da4 FB |
2655 | return 0; |
2656 | ||
b0d7ffd4 | 2657 | err1: |
d7be2952 | 2658 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
b0d7ffd4 FB |
2659 | |
2660 | err0: | |
72246da4 FB |
2661 | return ret; |
2662 | } | |
2663 | ||
d7be2952 FB |
2664 | static int dwc3_gadget_start(struct usb_gadget *g, |
2665 | struct usb_gadget_driver *driver) | |
72246da4 FB |
2666 | { |
2667 | struct dwc3 *dwc = gadget_to_dwc(g); | |
2668 | unsigned long flags; | |
8cf9045b | 2669 | int ret; |
8698e2ac | 2670 | int irq; |
72246da4 | 2671 | |
9522def4 | 2672 | irq = dwc->irq_gadget; |
d7be2952 FB |
2673 | ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, |
2674 | IRQF_SHARED, "dwc3", dwc->ev_buf); | |
2675 | if (ret) { | |
2676 | dev_err(dwc->dev, "failed to request irq #%d --> %d\n", | |
2677 | irq, ret); | |
8cf9045b | 2678 | return ret; |
d7be2952 FB |
2679 | } |
2680 | ||
72246da4 | 2681 | spin_lock_irqsave(&dwc->lock, flags); |
d7be2952 | 2682 | dwc->gadget_driver = driver; |
d7be2952 FB |
2683 | spin_unlock_irqrestore(&dwc->lock, flags); |
2684 | ||
2685 | return 0; | |
d7be2952 | 2686 | } |
72246da4 | 2687 | |
d7be2952 FB |
2688 | static void __dwc3_gadget_stop(struct dwc3 *dwc) |
2689 | { | |
8698e2ac | 2690 | dwc3_gadget_disable_irq(dwc); |
72246da4 FB |
2691 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
2692 | __dwc3_gadget_ep_disable(dwc->eps[1]); | |
d7be2952 | 2693 | } |
72246da4 | 2694 | |
d7be2952 FB |
2695 | static int dwc3_gadget_stop(struct usb_gadget *g) |
2696 | { | |
2697 | struct dwc3 *dwc = gadget_to_dwc(g); | |
2698 | unsigned long flags; | |
72246da4 | 2699 | |
d7be2952 | 2700 | spin_lock_irqsave(&dwc->lock, flags); |
d7be2952 | 2701 | dwc->gadget_driver = NULL; |
9f607a30 | 2702 | dwc->max_cfg_eps = 0; |
72246da4 FB |
2703 | spin_unlock_irqrestore(&dwc->lock, flags); |
2704 | ||
3f308d17 | 2705 | free_irq(dwc->irq_gadget, dwc->ev_buf); |
b0d7ffd4 | 2706 | |
72246da4 FB |
2707 | return 0; |
2708 | } | |
802fde98 | 2709 | |
729dcffd AKV |
2710 | static void dwc3_gadget_config_params(struct usb_gadget *g, |
2711 | struct usb_dcd_config_params *params) | |
2712 | { | |
2713 | struct dwc3 *dwc = gadget_to_dwc(g); | |
2714 | ||
54fb5ba6 TN |
2715 | params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED; |
2716 | params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED; | |
2717 | ||
2718 | /* Recommended BESL */ | |
2719 | if (!dwc->dis_enblslpm_quirk) { | |
17b63704 TN |
2720 | /* |
2721 | * If the recommended BESL baseline is 0 or if the BESL deep is | |
2722 | * less than 2, Microsoft's Windows 10 host usb stack will issue | |
2723 | * a usb reset immediately after it receives the extended BOS | |
2724 | * descriptor and the enumeration will fail. To maintain | |
2725 | * compatibility with the Windows' usb stack, let's set the | |
2726 | * recommended BESL baseline to 1 and clamp the BESL deep to be | |
2727 | * within 2 to 15. | |
2728 | */ | |
2729 | params->besl_baseline = 1; | |
54fb5ba6 | 2730 | if (dwc->is_utmi_l1_suspend) |
17b63704 TN |
2731 | params->besl_deep = |
2732 | clamp_t(u8, dwc->hird_threshold, 2, 15); | |
54fb5ba6 TN |
2733 | } |
2734 | ||
729dcffd AKV |
2735 | /* U1 Device exit Latency */ |
2736 | if (dwc->dis_u1_entry_quirk) | |
2737 | params->bU1devExitLat = 0; | |
2738 | else | |
2739 | params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT; | |
2740 | ||
2741 | /* U2 Device exit Latency */ | |
2742 | if (dwc->dis_u2_entry_quirk) | |
2743 | params->bU2DevExitLat = 0; | |
2744 | else | |
2745 | params->bU2DevExitLat = | |
2746 | cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT); | |
2747 | } | |
2748 | ||
7d8d0639 FB |
2749 | static void dwc3_gadget_set_speed(struct usb_gadget *g, |
2750 | enum usb_device_speed speed) | |
2751 | { | |
2752 | struct dwc3 *dwc = gadget_to_dwc(g); | |
2753 | unsigned long flags; | |
7d8d0639 FB |
2754 | |
2755 | spin_lock_irqsave(&dwc->lock, flags); | |
7c9a2598 | 2756 | dwc->gadget_max_speed = speed; |
7d8d0639 FB |
2757 | spin_unlock_irqrestore(&dwc->lock, flags); |
2758 | } | |
2759 | ||
072cab8a TN |
2760 | static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g, |
2761 | enum usb_ssp_rate rate) | |
2762 | { | |
2763 | struct dwc3 *dwc = gadget_to_dwc(g); | |
2764 | unsigned long flags; | |
2765 | ||
2766 | spin_lock_irqsave(&dwc->lock, flags); | |
cdb651b6 | 2767 | dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS; |
072cab8a TN |
2768 | dwc->gadget_ssp_rate = rate; |
2769 | spin_unlock_irqrestore(&dwc->lock, flags); | |
2770 | } | |
2771 | ||
82c46b8e WC |
2772 | static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA) |
2773 | { | |
2774 | struct dwc3 *dwc = gadget_to_dwc(g); | |
99288de3 RC |
2775 | union power_supply_propval val = {0}; |
2776 | int ret; | |
82c46b8e WC |
2777 | |
2778 | if (dwc->usb2_phy) | |
2779 | return usb_phy_set_power(dwc->usb2_phy, mA); | |
2780 | ||
99288de3 RC |
2781 | if (!dwc->usb_psy) |
2782 | return -EOPNOTSUPP; | |
2783 | ||
8a5b5c3c | 2784 | val.intval = 1000 * mA; |
99288de3 RC |
2785 | ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val); |
2786 | ||
2787 | return ret; | |
82c46b8e WC |
2788 | } |
2789 | ||
9f607a30 WC |
2790 | /** |
2791 | * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration | |
2792 | * @g: pointer to the USB gadget | |
2793 | * | |
2794 | * Used to record the maximum number of endpoints being used in a USB composite | |
2795 | * device. (across all configurations) This is to be used in the calculation | |
2796 | * of the TXFIFO sizes when resizing internal memory for individual endpoints. | |
2797 | * It will help ensured that the resizing logic reserves enough space for at | |
2798 | * least one max packet. | |
2799 | */ | |
2800 | static int dwc3_gadget_check_config(struct usb_gadget *g) | |
2801 | { | |
2802 | struct dwc3 *dwc = gadget_to_dwc(g); | |
2803 | struct usb_ep *ep; | |
2804 | int fifo_size = 0; | |
2805 | int ram1_depth; | |
2806 | int ep_num = 0; | |
2807 | ||
2808 | if (!dwc->do_fifo_resize) | |
2809 | return 0; | |
2810 | ||
2811 | list_for_each_entry(ep, &g->ep_list, ep_list) { | |
2812 | /* Only interested in the IN endpoints */ | |
2813 | if (ep->claimed && (ep->address & USB_DIR_IN)) | |
2814 | ep_num++; | |
2815 | } | |
2816 | ||
2817 | if (ep_num <= dwc->max_cfg_eps) | |
2818 | return 0; | |
2819 | ||
2820 | /* Update the max number of eps in the composition */ | |
2821 | dwc->max_cfg_eps = ep_num; | |
2822 | ||
2823 | fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps); | |
2824 | /* Based on the equation, increment by one for every ep */ | |
2825 | fifo_size += dwc->max_cfg_eps; | |
2826 | ||
2827 | /* Check if we can fit a single fifo per endpoint */ | |
2828 | ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); | |
2829 | if (fifo_size > ram1_depth) | |
2830 | return -ENOMEM; | |
2831 | ||
2832 | return 0; | |
2833 | } | |
2834 | ||
40edb522 LY |
2835 | static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable) |
2836 | { | |
2837 | struct dwc3 *dwc = gadget_to_dwc(g); | |
2838 | unsigned long flags; | |
2839 | ||
2840 | spin_lock_irqsave(&dwc->lock, flags); | |
2841 | dwc->async_callbacks = enable; | |
2842 | spin_unlock_irqrestore(&dwc->lock, flags); | |
2843 | } | |
2844 | ||
72246da4 FB |
2845 | static const struct usb_gadget_ops dwc3_gadget_ops = { |
2846 | .get_frame = dwc3_gadget_get_frame, | |
2847 | .wakeup = dwc3_gadget_wakeup, | |
2848 | .set_selfpowered = dwc3_gadget_set_selfpowered, | |
2849 | .pullup = dwc3_gadget_pullup, | |
2850 | .udc_start = dwc3_gadget_start, | |
2851 | .udc_stop = dwc3_gadget_stop, | |
7d8d0639 | 2852 | .udc_set_speed = dwc3_gadget_set_speed, |
072cab8a | 2853 | .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate, |
729dcffd | 2854 | .get_config_params = dwc3_gadget_config_params, |
82c46b8e | 2855 | .vbus_draw = dwc3_gadget_vbus_draw, |
9f607a30 | 2856 | .check_config = dwc3_gadget_check_config, |
40edb522 | 2857 | .udc_async_callbacks = dwc3_gadget_async_callbacks, |
72246da4 FB |
2858 | }; |
2859 | ||
2860 | /* -------------------------------------------------------------------------- */ | |
2861 | ||
8f1c99cd | 2862 | static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) |
72246da4 | 2863 | { |
8f1c99cd | 2864 | struct dwc3 *dwc = dep->dwc; |
72246da4 | 2865 | |
8f1c99cd FB |
2866 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
2867 | dep->endpoint.maxburst = 1; | |
2868 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; | |
2869 | if (!dep->direction) | |
e81a7018 | 2870 | dwc->gadget->ep0 = &dep->endpoint; |
f3bcfc7e | 2871 | |
8f1c99cd | 2872 | dep->endpoint.caps.type_control = true; |
72246da4 | 2873 | |
8f1c99cd FB |
2874 | return 0; |
2875 | } | |
72246da4 | 2876 | |
8f1c99cd FB |
2877 | static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) |
2878 | { | |
2879 | struct dwc3 *dwc = dep->dwc; | |
d00be779 | 2880 | u32 mdwidth; |
8f1c99cd | 2881 | int size; |
72246da4 | 2882 | |
d00be779 | 2883 | mdwidth = dwc3_mdwidth(dwc); |
4244ba02 | 2884 | |
8f1c99cd FB |
2885 | /* MDWIDTH is represented in bits, we need it in bytes */ |
2886 | mdwidth /= 8; | |
6a1e3ef4 | 2887 | |
8f1c99cd | 2888 | size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); |
9af21dd6 | 2889 | if (DWC3_IP_IS(DWC3)) |
586f4335 | 2890 | size = DWC3_GTXFIFOSIZ_TXFDEP(size); |
9af21dd6 TN |
2891 | else |
2892 | size = DWC31_GTXFIFOSIZ_TXFDEP(size); | |
39ebb05c | 2893 | |
8f1c99cd FB |
2894 | /* FIFO Depth is in MDWDITH bytes. Multiply */ |
2895 | size *= mdwidth; | |
39ebb05c | 2896 | |
8f1c99cd | 2897 | /* |
d94ea531 TN |
2898 | * To meet performance requirement, a minimum TxFIFO size of 3x |
2899 | * MaxPacketSize is recommended for endpoints that support burst and a | |
2900 | * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't | |
2901 | * support burst. Use those numbers and we can calculate the max packet | |
2902 | * limit as below. | |
8f1c99cd | 2903 | */ |
d94ea531 TN |
2904 | if (dwc->maximum_speed >= USB_SPEED_SUPER) |
2905 | size /= 3; | |
2906 | else | |
2907 | size /= 2; | |
28781789 | 2908 | |
8f1c99cd | 2909 | usb_ep_set_maxpacket_limit(&dep->endpoint, size); |
28781789 | 2910 | |
e0a93d98 | 2911 | dep->endpoint.max_streams = 16; |
8f1c99cd FB |
2912 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
2913 | list_add_tail(&dep->endpoint.ep_list, | |
e81a7018 | 2914 | &dwc->gadget->ep_list); |
8f1c99cd FB |
2915 | dep->endpoint.caps.type_iso = true; |
2916 | dep->endpoint.caps.type_bulk = true; | |
2917 | dep->endpoint.caps.type_int = true; | |
28781789 | 2918 | |
8f1c99cd FB |
2919 | return dwc3_alloc_trb_pool(dep); |
2920 | } | |
28781789 | 2921 | |
8f1c99cd FB |
2922 | static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) |
2923 | { | |
2924 | struct dwc3 *dwc = dep->dwc; | |
d00be779 | 2925 | u32 mdwidth; |
d94ea531 TN |
2926 | int size; |
2927 | ||
d00be779 | 2928 | mdwidth = dwc3_mdwidth(dwc); |
d94ea531 TN |
2929 | |
2930 | /* MDWIDTH is represented in bits, convert to bytes */ | |
2931 | mdwidth /= 8; | |
28781789 | 2932 | |
d94ea531 TN |
2933 | /* All OUT endpoints share a single RxFIFO space */ |
2934 | size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0)); | |
9af21dd6 | 2935 | if (DWC3_IP_IS(DWC3)) |
d94ea531 | 2936 | size = DWC3_GRXFIFOSIZ_RXFDEP(size); |
9af21dd6 TN |
2937 | else |
2938 | size = DWC31_GRXFIFOSIZ_RXFDEP(size); | |
d94ea531 TN |
2939 | |
2940 | /* FIFO depth is in MDWDITH bytes */ | |
2941 | size *= mdwidth; | |
2942 | ||
2943 | /* | |
2944 | * To meet performance requirement, a minimum recommended RxFIFO size | |
2945 | * is defined as follow: | |
2946 | * RxFIFO size >= (3 x MaxPacketSize) + | |
2947 | * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin) | |
2948 | * | |
2949 | * Then calculate the max packet limit as below. | |
2950 | */ | |
2951 | size -= (3 * 8) + 16; | |
2952 | if (size < 0) | |
2953 | size = 0; | |
2954 | else | |
2955 | size /= 3; | |
2956 | ||
2957 | usb_ep_set_maxpacket_limit(&dep->endpoint, size); | |
e0a93d98 | 2958 | dep->endpoint.max_streams = 16; |
8f1c99cd FB |
2959 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
2960 | list_add_tail(&dep->endpoint.ep_list, | |
e81a7018 | 2961 | &dwc->gadget->ep_list); |
8f1c99cd FB |
2962 | dep->endpoint.caps.type_iso = true; |
2963 | dep->endpoint.caps.type_bulk = true; | |
2964 | dep->endpoint.caps.type_int = true; | |
72246da4 | 2965 | |
8f1c99cd FB |
2966 | return dwc3_alloc_trb_pool(dep); |
2967 | } | |
72246da4 | 2968 | |
8f1c99cd FB |
2969 | static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum) |
2970 | { | |
2971 | struct dwc3_ep *dep; | |
2972 | bool direction = epnum & 1; | |
2973 | int ret; | |
2974 | u8 num = epnum >> 1; | |
25b8ff68 | 2975 | |
8f1c99cd FB |
2976 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
2977 | if (!dep) | |
2978 | return -ENOMEM; | |
2979 | ||
2980 | dep->dwc = dwc; | |
2981 | dep->number = epnum; | |
2982 | dep->direction = direction; | |
2983 | dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); | |
2984 | dwc->eps[epnum] = dep; | |
d92021f6 TN |
2985 | dep->combo_num = 0; |
2986 | dep->start_cmd_status = 0; | |
8f1c99cd FB |
2987 | |
2988 | snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, | |
2989 | direction ? "in" : "out"); | |
2990 | ||
2991 | dep->endpoint.name = dep->name; | |
2992 | ||
2993 | if (!(dep->number > 1)) { | |
2994 | dep->endpoint.desc = &dwc3_gadget_ep0_desc; | |
2995 | dep->endpoint.comp_desc = NULL; | |
2996 | } | |
2997 | ||
8f1c99cd FB |
2998 | if (num == 0) |
2999 | ret = dwc3_gadget_init_control_endpoint(dep); | |
3000 | else if (direction) | |
3001 | ret = dwc3_gadget_init_in_endpoint(dep); | |
3002 | else | |
3003 | ret = dwc3_gadget_init_out_endpoint(dep); | |
3004 | ||
3005 | if (ret) | |
3006 | return ret; | |
a474d3b7 | 3007 | |
8f1c99cd FB |
3008 | dep->endpoint.caps.dir_in = direction; |
3009 | dep->endpoint.caps.dir_out = !direction; | |
a474d3b7 | 3010 | |
8f1c99cd FB |
3011 | INIT_LIST_HEAD(&dep->pending_list); |
3012 | INIT_LIST_HEAD(&dep->started_list); | |
d5443bbf | 3013 | INIT_LIST_HEAD(&dep->cancelled_list); |
8f1c99cd | 3014 | |
5ff90af9 JP |
3015 | dwc3_debugfs_create_endpoint_dir(dep); |
3016 | ||
8f1c99cd FB |
3017 | return 0; |
3018 | } | |
3019 | ||
3020 | static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) | |
3021 | { | |
3022 | u8 epnum; | |
3023 | ||
e81a7018 | 3024 | INIT_LIST_HEAD(&dwc->gadget->ep_list); |
8f1c99cd FB |
3025 | |
3026 | for (epnum = 0; epnum < total; epnum++) { | |
3027 | int ret; | |
3028 | ||
3029 | ret = dwc3_gadget_init_endpoint(dwc, epnum); | |
3030 | if (ret) | |
3031 | return ret; | |
72246da4 FB |
3032 | } |
3033 | ||
3034 | return 0; | |
3035 | } | |
3036 | ||
3037 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) | |
3038 | { | |
3039 | struct dwc3_ep *dep; | |
3040 | u8 epnum; | |
3041 | ||
3042 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { | |
3043 | dep = dwc->eps[epnum]; | |
6a1e3ef4 FB |
3044 | if (!dep) |
3045 | continue; | |
5bf8fae3 GC |
3046 | /* |
3047 | * Physical endpoints 0 and 1 are special; they form the | |
3048 | * bi-directional USB endpoint 0. | |
3049 | * | |
3050 | * For those two physical endpoints, we don't allocate a TRB | |
3051 | * pool nor do we add them the endpoints list. Due to that, we | |
3052 | * shouldn't do these two operations otherwise we would end up | |
3053 | * with all sorts of bugs when removing dwc3.ko. | |
3054 | */ | |
3055 | if (epnum != 0 && epnum != 1) { | |
3056 | dwc3_free_trb_pool(dep); | |
72246da4 | 3057 | list_del(&dep->endpoint.ep_list); |
5bf8fae3 | 3058 | } |
72246da4 | 3059 | |
8562d5bf GKH |
3060 | debugfs_remove_recursive(debugfs_lookup(dep->name, |
3061 | debugfs_lookup(dev_name(dep->dwc->dev), | |
3062 | usb_debug_root))); | |
72246da4 FB |
3063 | kfree(dep); |
3064 | } | |
3065 | } | |
3066 | ||
72246da4 | 3067 | /* -------------------------------------------------------------------------- */ |
e5caff68 | 3068 | |
8f608e8a FB |
3069 | static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, |
3070 | struct dwc3_request *req, struct dwc3_trb *trb, | |
3071 | const struct dwc3_event_depevt *event, int status, int chain) | |
72246da4 | 3072 | { |
72246da4 | 3073 | unsigned int count; |
72246da4 | 3074 | |
dc55c67e | 3075 | dwc3_ep_inc_deq(dep); |
a9c3ca5f | 3076 | |
2c4cbe6e | 3077 | trace_dwc3_complete_trb(dep, trb); |
09fe1f8d | 3078 | req->num_trbs--; |
2c4cbe6e | 3079 | |
e5b36ae2 FB |
3080 | /* |
3081 | * If we're in the middle of series of chained TRBs and we | |
3082 | * receive a short transfer along the way, DWC3 will skip | |
3083 | * through all TRBs including the last TRB in the chain (the | |
3084 | * where CHN bit is zero. DWC3 will also avoid clearing HWO | |
3085 | * bit and SW has to do it manually. | |
3086 | * | |
3087 | * We're going to do that here to avoid problems of HW trying | |
3088 | * to use bogus TRBs for transfers. | |
3089 | */ | |
3090 | if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) | |
3091 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; | |
3092 | ||
6abfa0f5 TN |
3093 | /* |
3094 | * For isochronous transfers, the first TRB in a service interval must | |
3095 | * have the Isoc-First type. Track and report its interval frame number. | |
3096 | */ | |
3097 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && | |
3098 | (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { | |
3099 | unsigned int frame_number; | |
3100 | ||
3101 | frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); | |
3102 | frame_number &= ~(dep->interval - 1); | |
3103 | req->request.frame_number = frame_number; | |
3104 | } | |
3105 | ||
c6267a51 | 3106 | /* |
a2841f41 TN |
3107 | * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If |
3108 | * this TRB points to the bounce buffer address, it's a MPS alignment | |
3109 | * TRB. Don't add it to req->remaining calculation. | |
c6267a51 | 3110 | */ |
a2841f41 TN |
3111 | if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) && |
3112 | trb->bph == upper_32_bits(dep->dwc->bounce_addr)) { | |
c6267a51 FB |
3113 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
3114 | return 1; | |
3115 | } | |
3116 | ||
e5ba5ec8 | 3117 | count = trb->size & DWC3_TRB_SIZE_MASK; |
e62c5bc5 | 3118 | req->remaining += count; |
e5ba5ec8 | 3119 | |
35b2719e FB |
3120 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
3121 | return 1; | |
3122 | ||
d80fe1b6 | 3123 | if (event->status & DEPEVT_STATUS_SHORT && !chain) |
e5ba5ec8 | 3124 | return 1; |
f99f53f2 | 3125 | |
5ee85897 AKV |
3126 | if ((trb->ctrl & DWC3_TRB_CTRL_IOC) || |
3127 | (trb->ctrl & DWC3_TRB_CTRL_LST)) | |
e5ba5ec8 | 3128 | return 1; |
f99f53f2 | 3129 | |
e5ba5ec8 PA |
3130 | return 0; |
3131 | } | |
3132 | ||
d3692953 FB |
3133 | static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep, |
3134 | struct dwc3_request *req, const struct dwc3_event_depevt *event, | |
3135 | int status) | |
3136 | { | |
3137 | struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; | |
3138 | struct scatterlist *sg = req->sg; | |
3139 | struct scatterlist *s; | |
25dda9fc | 3140 | unsigned int num_queued = req->num_queued_sgs; |
d3692953 FB |
3141 | unsigned int i; |
3142 | int ret = 0; | |
3143 | ||
25dda9fc | 3144 | for_each_sg(sg, s, num_queued, i) { |
d3692953 FB |
3145 | trb = &dep->trb_pool[dep->trb_dequeue]; |
3146 | ||
d3692953 | 3147 | req->sg = sg_next(s); |
25dda9fc | 3148 | req->num_queued_sgs--; |
d3692953 FB |
3149 | |
3150 | ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, | |
3151 | trb, event, status, true); | |
3152 | if (ret) | |
3153 | break; | |
3154 | } | |
3155 | ||
3156 | return ret; | |
3157 | } | |
3158 | ||
3159 | static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep, | |
3160 | struct dwc3_request *req, const struct dwc3_event_depevt *event, | |
3161 | int status) | |
3162 | { | |
3163 | struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; | |
3164 | ||
3165 | return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb, | |
3166 | event, status, false); | |
3167 | } | |
3168 | ||
e0c42ce5 FB |
3169 | static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) |
3170 | { | |
25dda9fc | 3171 | return req->num_pending_sgs == 0 && req->num_queued_sgs == 0; |
e0c42ce5 FB |
3172 | } |
3173 | ||
f38e35dd FB |
3174 | static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, |
3175 | const struct dwc3_event_depevt *event, | |
3176 | struct dwc3_request *req, int status) | |
3177 | { | |
3178 | int ret; | |
3179 | ||
25dda9fc | 3180 | if (req->request.num_mapped_sgs) |
f38e35dd FB |
3181 | ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, |
3182 | status); | |
3183 | else | |
3184 | ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, | |
3185 | status); | |
3186 | ||
690e5c2d TN |
3187 | req->request.actual = req->request.length - req->remaining; |
3188 | ||
3189 | if (!dwc3_gadget_ep_request_completed(req)) | |
3190 | goto out; | |
3191 | ||
1a22ec64 | 3192 | if (req->needs_extra_trb) { |
f38e35dd FB |
3193 | ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, |
3194 | status); | |
1a22ec64 | 3195 | req->needs_extra_trb = false; |
f38e35dd FB |
3196 | } |
3197 | ||
f38e35dd FB |
3198 | dwc3_gadget_giveback(dep, req, status); |
3199 | ||
3200 | out: | |
3201 | return ret; | |
3202 | } | |
3203 | ||
12a3a4ad | 3204 | static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep, |
8f608e8a | 3205 | const struct dwc3_event_depevt *event, int status) |
e5ba5ec8 | 3206 | { |
6afbdb57 FB |
3207 | struct dwc3_request *req; |
3208 | struct dwc3_request *tmp; | |
e5ba5ec8 | 3209 | |
664cc971 | 3210 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) { |
fee73e61 | 3211 | int ret; |
e5b36ae2 | 3212 | |
f38e35dd FB |
3213 | ret = dwc3_gadget_ep_cleanup_completed_request(dep, event, |
3214 | req, status); | |
58f0218a | 3215 | if (ret) |
72246da4 | 3216 | break; |
31162af4 | 3217 | } |
72246da4 FB |
3218 | } |
3219 | ||
d9feef97 TN |
3220 | static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep) |
3221 | { | |
3222 | struct dwc3_request *req; | |
02fa4b98 WC |
3223 | struct dwc3 *dwc = dep->dwc; |
3224 | ||
3225 | if (!dep->endpoint.desc || !dwc->pullups_connected || | |
3226 | !dwc->connected) | |
3227 | return false; | |
d9feef97 TN |
3228 | |
3229 | if (!list_empty(&dep->pending_list)) | |
3230 | return true; | |
3231 | ||
3232 | /* | |
3233 | * We only need to check the first entry of the started list. We can | |
3234 | * assume the completed requests are removed from the started list. | |
3235 | */ | |
3236 | req = next_request(&dep->started_list); | |
3237 | if (!req) | |
3238 | return false; | |
3239 | ||
3240 | return !dwc3_gadget_ep_request_completed(req); | |
3241 | } | |
3242 | ||
ee3638b8 FB |
3243 | static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep, |
3244 | const struct dwc3_event_depevt *event) | |
3245 | { | |
f62afb49 | 3246 | dep->frame_number = event->parameters; |
ee3638b8 FB |
3247 | } |
3248 | ||
2e6e9e4b TN |
3249 | static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep, |
3250 | const struct dwc3_event_depevt *event, int status) | |
72246da4 | 3251 | { |
8f608e8a | 3252 | struct dwc3 *dwc = dep->dwc; |
2e6e9e4b | 3253 | bool no_started_trb = true; |
6d8a0196 | 3254 | |
5f2e7975 | 3255 | dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); |
fae2b904 | 3256 | |
b6842d49 TN |
3257 | if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) |
3258 | goto out; | |
3259 | ||
f5e46aa4 MG |
3260 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
3261 | list_empty(&dep->started_list) && | |
3262 | (list_empty(&dep->pending_list) || status == -EXDEV)) | |
c5353b22 | 3263 | dwc3_stop_active_transfer(dep, true, true); |
d9feef97 | 3264 | else if (dwc3_gadget_ep_should_continue(dep)) |
2e6e9e4b TN |
3265 | if (__dwc3_gadget_kick_transfer(dep) == 0) |
3266 | no_started_trb = false; | |
6d8a0196 | 3267 | |
b6842d49 | 3268 | out: |
fae2b904 FB |
3269 | /* |
3270 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. | |
3271 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. | |
3272 | */ | |
9af21dd6 | 3273 | if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { |
fae2b904 FB |
3274 | u32 reg; |
3275 | int i; | |
3276 | ||
3277 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { | |
348e026f | 3278 | dep = dwc->eps[i]; |
fae2b904 FB |
3279 | |
3280 | if (!(dep->flags & DWC3_EP_ENABLED)) | |
3281 | continue; | |
3282 | ||
aa3342c8 | 3283 | if (!list_empty(&dep->started_list)) |
2e6e9e4b | 3284 | return no_started_trb; |
fae2b904 FB |
3285 | } |
3286 | ||
3287 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
3288 | reg |= dwc->u1u2; | |
3289 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
3290 | ||
3291 | dwc->u1u2 = 0; | |
3292 | } | |
2e6e9e4b TN |
3293 | |
3294 | return no_started_trb; | |
3295 | } | |
3296 | ||
3297 | static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep, | |
3298 | const struct dwc3_event_depevt *event) | |
3299 | { | |
3300 | int status = 0; | |
3301 | ||
3302 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) | |
3303 | dwc3_gadget_endpoint_frame_from_event(dep, event); | |
3304 | ||
3305 | if (event->status & DEPEVT_STATUS_BUSERR) | |
3306 | status = -ECONNRESET; | |
3307 | ||
3308 | if (event->status & DEPEVT_STATUS_MISSED_ISOC) | |
3309 | status = -EXDEV; | |
3310 | ||
3311 | dwc3_gadget_endpoint_trbs_complete(dep, event, status); | |
72246da4 FB |
3312 | } |
3313 | ||
3eaecd0c TN |
3314 | static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep, |
3315 | const struct dwc3_event_depevt *event) | |
3316 | { | |
3317 | int status = 0; | |
3318 | ||
3319 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; | |
3320 | ||
3321 | if (event->status & DEPEVT_STATUS_BUSERR) | |
3322 | status = -ECONNRESET; | |
3323 | ||
e0d19563 TN |
3324 | if (dwc3_gadget_endpoint_trbs_complete(dep, event, status)) |
3325 | dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; | |
72246da4 FB |
3326 | } |
3327 | ||
8f608e8a FB |
3328 | static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep, |
3329 | const struct dwc3_event_depevt *event) | |
32033865 | 3330 | { |
ee3638b8 | 3331 | dwc3_gadget_endpoint_frame_from_event(dep, event); |
36f05d36 TN |
3332 | |
3333 | /* | |
3334 | * The XferNotReady event is generated only once before the endpoint | |
3335 | * starts. It will be generated again when END_TRANSFER command is | |
3336 | * issued. For some controller versions, the XferNotReady event may be | |
3337 | * generated while the END_TRANSFER command is still in process. Ignore | |
3338 | * it and wait for the next XferNotReady event after the command is | |
3339 | * completed. | |
3340 | */ | |
3341 | if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) | |
3342 | return; | |
3343 | ||
25abad6a | 3344 | (void) __dwc3_gadget_start_isoc(dep); |
32033865 FB |
3345 | } |
3346 | ||
8266b08e TN |
3347 | static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep, |
3348 | const struct dwc3_event_depevt *event) | |
3349 | { | |
3350 | u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters); | |
3351 | ||
3352 | if (cmd != DWC3_DEPCMD_ENDTRANSFER) | |
3353 | return; | |
3354 | ||
3355 | dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; | |
3356 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; | |
3357 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); | |
3358 | ||
3359 | if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) { | |
3360 | struct dwc3 *dwc = dep->dwc; | |
3361 | ||
3362 | dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL; | |
3363 | if (dwc3_send_clear_stall_ep_cmd(dep)) { | |
3364 | struct usb_ep *ep0 = &dwc->eps[0]->endpoint; | |
3365 | ||
3366 | dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name); | |
3367 | if (dwc->delayed_status) | |
3368 | __dwc3_gadget_ep0_set_halt(ep0, 1); | |
3369 | return; | |
3370 | } | |
3371 | ||
3372 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); | |
3373 | if (dwc->delayed_status) | |
3374 | dwc3_ep0_send_delayed_status(dwc); | |
3375 | } | |
3376 | ||
3377 | if ((dep->flags & DWC3_EP_DELAY_START) && | |
3378 | !usb_endpoint_xfer_isoc(dep->endpoint.desc)) | |
3379 | __dwc3_gadget_kick_transfer(dep); | |
3380 | ||
3381 | dep->flags &= ~DWC3_EP_DELAY_START; | |
3382 | } | |
3383 | ||
140ca4cf TN |
3384 | static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep, |
3385 | const struct dwc3_event_depevt *event) | |
3386 | { | |
3387 | struct dwc3 *dwc = dep->dwc; | |
3388 | ||
3389 | if (event->status == DEPEVT_STREAMEVT_FOUND) { | |
3390 | dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; | |
3391 | goto out; | |
3392 | } | |
3393 | ||
3394 | /* Note: NoStream rejection event param value is 0 and not 0xFFFF */ | |
3395 | switch (event->parameters) { | |
3396 | case DEPEVT_STREAM_PRIME: | |
3397 | /* | |
3398 | * If the host can properly transition the endpoint state from | |
3399 | * idle to prime after a NoStream rejection, there's no need to | |
3400 | * force restarting the endpoint to reinitiate the stream. To | |
3401 | * simplify the check, assume the host follows the USB spec if | |
3402 | * it primed the endpoint more than once. | |
3403 | */ | |
3404 | if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) { | |
3405 | if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED) | |
3406 | dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM; | |
3407 | else | |
3408 | dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; | |
3409 | } | |
3410 | ||
3411 | break; | |
3412 | case DEPEVT_STREAM_NOSTREAM: | |
3413 | if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) || | |
3414 | !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) || | |
3415 | !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)) | |
3416 | break; | |
3417 | ||
3418 | /* | |
3419 | * If the host rejects a stream due to no active stream, by the | |
3420 | * USB and xHCI spec, the endpoint will be put back to idle | |
3421 | * state. When the host is ready (buffer added/updated), it will | |
3422 | * prime the endpoint to inform the usb device controller. This | |
3423 | * triggers the device controller to issue ERDY to restart the | |
3424 | * stream. However, some hosts don't follow this and keep the | |
3425 | * endpoint in the idle state. No prime will come despite host | |
3426 | * streams are updated, and the device controller will not be | |
3427 | * triggered to generate ERDY to move the next stream data. To | |
3428 | * workaround this and maintain compatibility with various | |
3429 | * hosts, force to reinitate the stream until the host is ready | |
3430 | * instead of waiting for the host to prime the endpoint. | |
3431 | */ | |
b10e1c25 TN |
3432 | if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) { |
3433 | unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME; | |
3434 | ||
3435 | dwc3_send_gadget_generic_command(dwc, cmd, dep->number); | |
3436 | } else { | |
3437 | dep->flags |= DWC3_EP_DELAY_START; | |
3438 | dwc3_stop_active_transfer(dep, true, true); | |
3439 | return; | |
3440 | } | |
3441 | break; | |
140ca4cf TN |
3442 | } |
3443 | ||
3444 | out: | |
3445 | dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; | |
3446 | } | |
3447 | ||
72246da4 FB |
3448 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, |
3449 | const struct dwc3_event_depevt *event) | |
3450 | { | |
3451 | struct dwc3_ep *dep; | |
3452 | u8 epnum = event->endpoint_number; | |
3453 | ||
3454 | dep = dwc->eps[epnum]; | |
3455 | ||
d7fd41c6 | 3456 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
3aec9915 | 3457 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) |
d7fd41c6 JD |
3458 | return; |
3459 | ||
3460 | /* Handle only EPCMDCMPLT when EP disabled */ | |
3461 | if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) | |
3462 | return; | |
3463 | } | |
3336abb5 | 3464 | |
72246da4 FB |
3465 | if (epnum == 0 || epnum == 1) { |
3466 | dwc3_ep0_interrupt(dwc, event); | |
3467 | return; | |
3468 | } | |
3469 | ||
3470 | switch (event->endpoint_event) { | |
72246da4 | 3471 | case DWC3_DEPEVT_XFERINPROGRESS: |
8f608e8a | 3472 | dwc3_gadget_endpoint_transfer_in_progress(dep, event); |
72246da4 FB |
3473 | break; |
3474 | case DWC3_DEPEVT_XFERNOTREADY: | |
8f608e8a | 3475 | dwc3_gadget_endpoint_transfer_not_ready(dep, event); |
879631aa | 3476 | break; |
72246da4 | 3477 | case DWC3_DEPEVT_EPCMDCMPLT: |
8266b08e | 3478 | dwc3_gadget_endpoint_command_complete(dep, event); |
76a638f8 | 3479 | break; |
742a4fff | 3480 | case DWC3_DEPEVT_XFERCOMPLETE: |
3eaecd0c TN |
3481 | dwc3_gadget_endpoint_transfer_complete(dep, event); |
3482 | break; | |
3483 | case DWC3_DEPEVT_STREAMEVT: | |
140ca4cf TN |
3484 | dwc3_gadget_endpoint_stream_event(dep, event); |
3485 | break; | |
76a638f8 | 3486 | case DWC3_DEPEVT_RXTXFIFOEVT: |
72246da4 FB |
3487 | break; |
3488 | } | |
3489 | } | |
3490 | ||
3491 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) | |
3492 | { | |
40edb522 | 3493 | if (dwc->async_callbacks && dwc->gadget_driver->disconnect) { |
72246da4 | 3494 | spin_unlock(&dwc->lock); |
e81a7018 | 3495 | dwc->gadget_driver->disconnect(dwc->gadget); |
72246da4 FB |
3496 | spin_lock(&dwc->lock); |
3497 | } | |
3498 | } | |
3499 | ||
bc5ba2e0 FB |
3500 | static void dwc3_suspend_gadget(struct dwc3 *dwc) |
3501 | { | |
40edb522 | 3502 | if (dwc->async_callbacks && dwc->gadget_driver->suspend) { |
bc5ba2e0 | 3503 | spin_unlock(&dwc->lock); |
e81a7018 | 3504 | dwc->gadget_driver->suspend(dwc->gadget); |
bc5ba2e0 FB |
3505 | spin_lock(&dwc->lock); |
3506 | } | |
3507 | } | |
3508 | ||
3509 | static void dwc3_resume_gadget(struct dwc3 *dwc) | |
3510 | { | |
40edb522 | 3511 | if (dwc->async_callbacks && dwc->gadget_driver->resume) { |
bc5ba2e0 | 3512 | spin_unlock(&dwc->lock); |
e81a7018 | 3513 | dwc->gadget_driver->resume(dwc->gadget); |
5c7b3b02 | 3514 | spin_lock(&dwc->lock); |
8e74475b FB |
3515 | } |
3516 | } | |
3517 | ||
3518 | static void dwc3_reset_gadget(struct dwc3 *dwc) | |
3519 | { | |
3520 | if (!dwc->gadget_driver) | |
3521 | return; | |
3522 | ||
40edb522 | 3523 | if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) { |
8e74475b | 3524 | spin_unlock(&dwc->lock); |
e81a7018 | 3525 | usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); |
bc5ba2e0 FB |
3526 | spin_lock(&dwc->lock); |
3527 | } | |
3528 | } | |
3529 | ||
c5353b22 FB |
3530 | static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, |
3531 | bool interrupt) | |
72246da4 | 3532 | { |
72246da4 FB |
3533 | struct dwc3_gadget_ep_cmd_params params; |
3534 | u32 cmd; | |
3535 | int ret; | |
3536 | ||
c58d8bfc TN |
3537 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) || |
3538 | (dep->flags & DWC3_EP_END_TRANSFER_PENDING)) | |
3daf74d7 PA |
3539 | return; |
3540 | ||
57911504 PA |
3541 | /* |
3542 | * NOTICE: We are violating what the Databook says about the | |
3543 | * EndTransfer command. Ideally we would _always_ wait for the | |
3544 | * EndTransfer Command Completion IRQ, but that's causing too | |
3545 | * much trouble synchronizing between us and gadget driver. | |
3546 | * | |
3547 | * We have discussed this with the IP Provider and it was | |
cf2f8b63 | 3548 | * suggested to giveback all requests here. |
57911504 PA |
3549 | * |
3550 | * Note also that a similar handling was tested by Synopsys | |
3551 | * (thanks a lot Paul) and nothing bad has come out of it. | |
cf2f8b63 TN |
3552 | * In short, what we're doing is issuing EndTransfer with |
3553 | * CMDIOC bit set and delay kicking transfer until the | |
3554 | * EndTransfer command had completed. | |
06281d46 JY |
3555 | * |
3556 | * As of IP version 3.10a of the DWC_usb3 IP, the controller | |
3557 | * supports a mode to work around the above limitation. The | |
3558 | * software can poll the CMDACT bit in the DEPCMD register | |
3559 | * after issuing a EndTransfer command. This mode is enabled | |
3560 | * by writing GUCTL2[14]. This polling is already done in the | |
3561 | * dwc3_send_gadget_ep_cmd() function so if the mode is | |
3562 | * enabled, the EndTransfer command will have completed upon | |
cf2f8b63 | 3563 | * returning from this function. |
06281d46 JY |
3564 | * |
3565 | * This mode is NOT available on the DWC_usb31 IP. | |
57911504 PA |
3566 | */ |
3567 | ||
3daf74d7 | 3568 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
b992e681 | 3569 | cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; |
c5353b22 | 3570 | cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0; |
b4996a86 | 3571 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
3daf74d7 | 3572 | memset(¶ms, 0, sizeof(params)); |
2cd4718d | 3573 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
3daf74d7 | 3574 | WARN_ON_ONCE(ret); |
b4996a86 | 3575 | dep->resource_index = 0; |
06281d46 | 3576 | |
140ca4cf TN |
3577 | /* |
3578 | * The END_TRANSFER command will cause the controller to generate a | |
3579 | * NoStream Event, and it's not due to the host DP NoStream rejection. | |
3580 | * Ignore the next NoStream event. | |
3581 | */ | |
3582 | if (dep->stream_capable) | |
3583 | dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM; | |
3584 | ||
d3abda5a TN |
3585 | if (!interrupt) |
3586 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; | |
c58d8bfc TN |
3587 | else |
3588 | dep->flags |= DWC3_EP_END_TRANSFER_PENDING; | |
72246da4 FB |
3589 | } |
3590 | ||
72246da4 FB |
3591 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) |
3592 | { | |
3593 | u32 epnum; | |
3594 | ||
3595 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { | |
3596 | struct dwc3_ep *dep; | |
72246da4 FB |
3597 | int ret; |
3598 | ||
3599 | dep = dwc->eps[epnum]; | |
6a1e3ef4 FB |
3600 | if (!dep) |
3601 | continue; | |
72246da4 FB |
3602 | |
3603 | if (!(dep->flags & DWC3_EP_STALL)) | |
3604 | continue; | |
3605 | ||
3606 | dep->flags &= ~DWC3_EP_STALL; | |
3607 | ||
50c763f8 | 3608 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
72246da4 FB |
3609 | WARN_ON_ONCE(ret); |
3610 | } | |
3611 | } | |
3612 | ||
3613 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) | |
3614 | { | |
c4430a26 FB |
3615 | int reg; |
3616 | ||
1b6009ea TN |
3617 | dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET); |
3618 | ||
72246da4 FB |
3619 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
3620 | reg &= ~DWC3_DCTL_INITU1ENA; | |
72246da4 | 3621 | reg &= ~DWC3_DCTL_INITU2ENA; |
5b738211 | 3622 | dwc3_gadget_dctl_write_safe(dwc, reg); |
72246da4 | 3623 | |
72246da4 FB |
3624 | dwc3_disconnect_gadget(dwc); |
3625 | ||
e81a7018 | 3626 | dwc->gadget->speed = USB_SPEED_UNKNOWN; |
df62df56 | 3627 | dwc->setup_packet_pending = false; |
e81a7018 | 3628 | usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); |
fc8bb91b FB |
3629 | |
3630 | dwc->connected = false; | |
72246da4 FB |
3631 | } |
3632 | ||
72246da4 FB |
3633 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) |
3634 | { | |
3635 | u32 reg; | |
3636 | ||
71ca43f3 WC |
3637 | /* |
3638 | * Ideally, dwc3_reset_gadget() would trigger the function | |
3639 | * drivers to stop any active transfers through ep disable. | |
3640 | * However, for functions which defer ep disable, such as mass | |
3641 | * storage, we will need to rely on the call to stop active | |
3642 | * transfers here, and avoid allowing of request queuing. | |
3643 | */ | |
3644 | dwc->connected = false; | |
3645 | ||
df62df56 FB |
3646 | /* |
3647 | * WORKAROUND: DWC3 revisions <1.88a have an issue which | |
3648 | * would cause a missing Disconnect Event if there's a | |
3649 | * pending Setup Packet in the FIFO. | |
3650 | * | |
3651 | * There's no suggested workaround on the official Bug | |
3652 | * report, which states that "unless the driver/application | |
3653 | * is doing any special handling of a disconnect event, | |
3654 | * there is no functional issue". | |
3655 | * | |
3656 | * Unfortunately, it turns out that we _do_ some special | |
3657 | * handling of a disconnect event, namely complete all | |
3658 | * pending transfers, notify gadget driver of the | |
3659 | * disconnection, and so on. | |
3660 | * | |
3661 | * Our suggested workaround is to follow the Disconnect | |
3662 | * Event steps here, instead, based on a setup_packet_pending | |
b5d335e5 FB |
3663 | * flag. Such flag gets set whenever we have a SETUP_PENDING |
3664 | * status for EP0 TRBs and gets cleared on XferComplete for the | |
df62df56 FB |
3665 | * same endpoint. |
3666 | * | |
3667 | * Refers to: | |
3668 | * | |
3669 | * STAR#9000466709: RTL: Device : Disconnect event not | |
3670 | * generated if setup packet pending in FIFO | |
3671 | */ | |
9af21dd6 | 3672 | if (DWC3_VER_IS_PRIOR(DWC3, 188A)) { |
df62df56 FB |
3673 | if (dwc->setup_packet_pending) |
3674 | dwc3_gadget_disconnect_interrupt(dwc); | |
3675 | } | |
3676 | ||
8e74475b | 3677 | dwc3_reset_gadget(dwc); |
ae7e8610 WC |
3678 | /* |
3679 | * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a | |
3680 | * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW | |
3681 | * needs to ensure that it sends "a DEPENDXFER command for any active | |
3682 | * transfers." | |
3683 | */ | |
3684 | dwc3_stop_active_transfers(dwc); | |
f09ddcfc | 3685 | dwc->connected = true; |
72246da4 FB |
3686 | |
3687 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
3688 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; | |
5b738211 | 3689 | dwc3_gadget_dctl_write_safe(dwc, reg); |
3b637367 | 3690 | dwc->test_mode = false; |
72246da4 FB |
3691 | dwc3_clear_stall_all_ep(dwc); |
3692 | ||
3693 | /* Reset device address to zero */ | |
3694 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
3695 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); | |
3696 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
72246da4 FB |
3697 | } |
3698 | ||
72246da4 FB |
3699 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) |
3700 | { | |
72246da4 FB |
3701 | struct dwc3_ep *dep; |
3702 | int ret; | |
3703 | u32 reg; | |
f551037c | 3704 | u8 lanes = 1; |
72246da4 FB |
3705 | u8 speed; |
3706 | ||
72246da4 FB |
3707 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
3708 | speed = reg & DWC3_DSTS_CONNECTSPD; | |
3709 | dwc->speed = speed; | |
3710 | ||
f551037c TN |
3711 | if (DWC3_IP_IS(DWC32)) |
3712 | lanes = DWC3_DSTS_CONNLANES(reg) + 1; | |
3713 | ||
3714 | dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; | |
3715 | ||
5fb6fdaf JY |
3716 | /* |
3717 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed | |
3718 | * each time on Connect Done. | |
3719 | * | |
3720 | * Currently we always use the reset value. If any platform | |
3721 | * wants to set this to a different value, we need to add a | |
3722 | * setting and update GCTL.RAMCLKSEL here. | |
3723 | */ | |
72246da4 FB |
3724 | |
3725 | switch (speed) { | |
2da9ad76 | 3726 | case DWC3_DSTS_SUPERSPEED_PLUS: |
7580862b | 3727 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
e81a7018 PC |
3728 | dwc->gadget->ep0->maxpacket = 512; |
3729 | dwc->gadget->speed = USB_SPEED_SUPER_PLUS; | |
f551037c TN |
3730 | |
3731 | if (lanes > 1) | |
3732 | dwc->gadget->ssp_rate = USB_SSP_GEN_2x2; | |
3733 | else | |
3734 | dwc->gadget->ssp_rate = USB_SSP_GEN_2x1; | |
7580862b | 3735 | break; |
2da9ad76 | 3736 | case DWC3_DSTS_SUPERSPEED: |
05870c5b FB |
3737 | /* |
3738 | * WORKAROUND: DWC3 revisions <1.90a have an issue which | |
3739 | * would cause a missing USB3 Reset event. | |
3740 | * | |
3741 | * In such situations, we should force a USB3 Reset | |
3742 | * event by calling our dwc3_gadget_reset_interrupt() | |
3743 | * routine. | |
3744 | * | |
3745 | * Refers to: | |
3746 | * | |
3747 | * STAR#9000483510: RTL: SS : USB3 reset event may | |
3748 | * not be generated always when the link enters poll | |
3749 | */ | |
9af21dd6 | 3750 | if (DWC3_VER_IS_PRIOR(DWC3, 190A)) |
05870c5b FB |
3751 | dwc3_gadget_reset_interrupt(dwc); |
3752 | ||
72246da4 | 3753 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
e81a7018 PC |
3754 | dwc->gadget->ep0->maxpacket = 512; |
3755 | dwc->gadget->speed = USB_SPEED_SUPER; | |
f551037c TN |
3756 | |
3757 | if (lanes > 1) { | |
3758 | dwc->gadget->speed = USB_SPEED_SUPER_PLUS; | |
3759 | dwc->gadget->ssp_rate = USB_SSP_GEN_1x2; | |
3760 | } | |
72246da4 | 3761 | break; |
2da9ad76 | 3762 | case DWC3_DSTS_HIGHSPEED: |
72246da4 | 3763 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
e81a7018 PC |
3764 | dwc->gadget->ep0->maxpacket = 64; |
3765 | dwc->gadget->speed = USB_SPEED_HIGH; | |
72246da4 | 3766 | break; |
9418ee15 | 3767 | case DWC3_DSTS_FULLSPEED: |
72246da4 | 3768 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
e81a7018 PC |
3769 | dwc->gadget->ep0->maxpacket = 64; |
3770 | dwc->gadget->speed = USB_SPEED_FULL; | |
72246da4 | 3771 | break; |
72246da4 FB |
3772 | } |
3773 | ||
e81a7018 | 3774 | dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket; |
61800263 | 3775 | |
2b758350 PA |
3776 | /* Enable USB2 LPM Capability */ |
3777 | ||
9af21dd6 | 3778 | if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) && |
475e8be5 | 3779 | !dwc->usb2_gadget_lpm_disable && |
2da9ad76 JY |
3780 | (speed != DWC3_DSTS_SUPERSPEED) && |
3781 | (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { | |
2b758350 PA |
3782 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
3783 | reg |= DWC3_DCFG_LPM_CAP; | |
3784 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
3785 | ||
3786 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
3787 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); | |
3788 | ||
16fe4f30 TN |
3789 | reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold | |
3790 | (dwc->is_utmi_l1_suspend << 4)); | |
2b758350 | 3791 | |
80caf7d2 HR |
3792 | /* |
3793 | * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and | |
3794 | * DCFG.LPMCap is set, core responses with an ACK and the | |
3795 | * BESL value in the LPM token is less than or equal to LPM | |
3796 | * NYET threshold. | |
3797 | */ | |
9af21dd6 | 3798 | WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum, |
9165dabb | 3799 | "LPM Erratum not available on dwc3 revisions < 2.40a\n"); |
80caf7d2 | 3800 | |
9af21dd6 | 3801 | if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) |
2e487d28 | 3802 | reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold); |
80caf7d2 | 3803 | |
5b738211 | 3804 | dwc3_gadget_dctl_write_safe(dwc, reg); |
356363bf | 3805 | } else { |
475e8be5 TN |
3806 | if (dwc->usb2_gadget_lpm_disable) { |
3807 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
3808 | reg &= ~DWC3_DCFG_LPM_CAP; | |
3809 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
3810 | } | |
3811 | ||
356363bf FB |
3812 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
3813 | reg &= ~DWC3_DCTL_HIRD_THRES_MASK; | |
5b738211 | 3814 | dwc3_gadget_dctl_write_safe(dwc, reg); |
2b758350 PA |
3815 | } |
3816 | ||
72246da4 | 3817 | dep = dwc->eps[0]; |
a2d23f08 | 3818 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); |
72246da4 FB |
3819 | if (ret) { |
3820 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
3821 | return; | |
3822 | } | |
3823 | ||
3824 | dep = dwc->eps[1]; | |
a2d23f08 | 3825 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); |
72246da4 FB |
3826 | if (ret) { |
3827 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
3828 | return; | |
3829 | } | |
3830 | ||
3831 | /* | |
3832 | * Configure PHY via GUSB3PIPECTLn if required. | |
3833 | * | |
3834 | * Update GTXFIFOSIZn | |
3835 | * | |
3836 | * In both cases reset values should be sufficient. | |
3837 | */ | |
3838 | } | |
3839 | ||
3840 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) | |
3841 | { | |
72246da4 FB |
3842 | /* |
3843 | * TODO take core out of low power mode when that's | |
3844 | * implemented. | |
3845 | */ | |
3846 | ||
40edb522 | 3847 | if (dwc->async_callbacks && dwc->gadget_driver->resume) { |
ad14d4e0 | 3848 | spin_unlock(&dwc->lock); |
e81a7018 | 3849 | dwc->gadget_driver->resume(dwc->gadget); |
ad14d4e0 JL |
3850 | spin_lock(&dwc->lock); |
3851 | } | |
72246da4 FB |
3852 | } |
3853 | ||
3854 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, | |
3855 | unsigned int evtinfo) | |
3856 | { | |
fae2b904 | 3857 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
0b0cc1cd FB |
3858 | unsigned int pwropt; |
3859 | ||
3860 | /* | |
3861 | * WORKAROUND: DWC3 < 2.50a have an issue when configured without | |
3862 | * Hibernation mode enabled which would show up when device detects | |
3863 | * host-initiated U3 exit. | |
3864 | * | |
3865 | * In that case, device will generate a Link State Change Interrupt | |
3866 | * from U3 to RESUME which is only necessary if Hibernation is | |
3867 | * configured in. | |
3868 | * | |
3869 | * There are no functional changes due to such spurious event and we | |
3870 | * just need to ignore it. | |
3871 | * | |
3872 | * Refers to: | |
3873 | * | |
3874 | * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation | |
3875 | * operational mode | |
3876 | */ | |
3877 | pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); | |
9af21dd6 | 3878 | if (DWC3_VER_IS_PRIOR(DWC3, 250A) && |
0b0cc1cd FB |
3879 | (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { |
3880 | if ((dwc->link_state == DWC3_LINK_STATE_U3) && | |
3881 | (next == DWC3_LINK_STATE_RESUME)) { | |
0b0cc1cd FB |
3882 | return; |
3883 | } | |
3884 | } | |
fae2b904 FB |
3885 | |
3886 | /* | |
3887 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending | |
3888 | * on the link partner, the USB session might do multiple entry/exit | |
3889 | * of low power states before a transfer takes place. | |
3890 | * | |
3891 | * Due to this problem, we might experience lower throughput. The | |
3892 | * suggested workaround is to disable DCTL[12:9] bits if we're | |
3893 | * transitioning from U1/U2 to U0 and enable those bits again | |
3894 | * after a transfer completes and there are no pending transfers | |
3895 | * on any of the enabled endpoints. | |
3896 | * | |
3897 | * This is the first half of that workaround. | |
3898 | * | |
3899 | * Refers to: | |
3900 | * | |
3901 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us | |
3902 | * core send LGO_Ux entering U0 | |
3903 | */ | |
9af21dd6 | 3904 | if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { |
fae2b904 FB |
3905 | if (next == DWC3_LINK_STATE_U0) { |
3906 | u32 u1u2; | |
3907 | u32 reg; | |
3908 | ||
3909 | switch (dwc->link_state) { | |
3910 | case DWC3_LINK_STATE_U1: | |
3911 | case DWC3_LINK_STATE_U2: | |
3912 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
3913 | u1u2 = reg & (DWC3_DCTL_INITU2ENA | |
3914 | | DWC3_DCTL_ACCEPTU2ENA | |
3915 | | DWC3_DCTL_INITU1ENA | |
3916 | | DWC3_DCTL_ACCEPTU1ENA); | |
3917 | ||
3918 | if (!dwc->u1u2) | |
3919 | dwc->u1u2 = reg & u1u2; | |
3920 | ||
3921 | reg &= ~u1u2; | |
3922 | ||
5b738211 | 3923 | dwc3_gadget_dctl_write_safe(dwc, reg); |
fae2b904 FB |
3924 | break; |
3925 | default: | |
3926 | /* do nothing */ | |
3927 | break; | |
3928 | } | |
3929 | } | |
3930 | } | |
3931 | ||
bc5ba2e0 FB |
3932 | switch (next) { |
3933 | case DWC3_LINK_STATE_U1: | |
3934 | if (dwc->speed == USB_SPEED_SUPER) | |
3935 | dwc3_suspend_gadget(dwc); | |
3936 | break; | |
3937 | case DWC3_LINK_STATE_U2: | |
3938 | case DWC3_LINK_STATE_U3: | |
3939 | dwc3_suspend_gadget(dwc); | |
3940 | break; | |
3941 | case DWC3_LINK_STATE_RESUME: | |
3942 | dwc3_resume_gadget(dwc); | |
3943 | break; | |
3944 | default: | |
3945 | /* do nothing */ | |
3946 | break; | |
3947 | } | |
3948 | ||
e57ebc1d | 3949 | dwc->link_state = next; |
72246da4 FB |
3950 | } |
3951 | ||
72704f87 BW |
3952 | static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, |
3953 | unsigned int evtinfo) | |
3954 | { | |
3955 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; | |
3956 | ||
3957 | if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) | |
3958 | dwc3_suspend_gadget(dwc); | |
3959 | ||
3960 | dwc->link_state = next; | |
3961 | } | |
3962 | ||
e1dadd3b FB |
3963 | static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, |
3964 | unsigned int evtinfo) | |
3965 | { | |
3966 | unsigned int is_ss = evtinfo & BIT(4); | |
3967 | ||
bfad65ee | 3968 | /* |
e1dadd3b FB |
3969 | * WORKAROUND: DWC3 revison 2.20a with hibernation support |
3970 | * have a known issue which can cause USB CV TD.9.23 to fail | |
3971 | * randomly. | |
3972 | * | |
3973 | * Because of this issue, core could generate bogus hibernation | |
3974 | * events which SW needs to ignore. | |
3975 | * | |
3976 | * Refers to: | |
3977 | * | |
3978 | * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 | |
3979 | * Device Fallback from SuperSpeed | |
3980 | */ | |
3981 | if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) | |
3982 | return; | |
3983 | ||
3984 | /* enter hibernation here */ | |
3985 | } | |
3986 | ||
72246da4 FB |
3987 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, |
3988 | const struct dwc3_event_devt *event) | |
3989 | { | |
3990 | switch (event->type) { | |
3991 | case DWC3_DEVICE_EVENT_DISCONNECT: | |
3992 | dwc3_gadget_disconnect_interrupt(dwc); | |
3993 | break; | |
3994 | case DWC3_DEVICE_EVENT_RESET: | |
3995 | dwc3_gadget_reset_interrupt(dwc); | |
3996 | break; | |
3997 | case DWC3_DEVICE_EVENT_CONNECT_DONE: | |
3998 | dwc3_gadget_conndone_interrupt(dwc); | |
3999 | break; | |
4000 | case DWC3_DEVICE_EVENT_WAKEUP: | |
4001 | dwc3_gadget_wakeup_interrupt(dwc); | |
4002 | break; | |
e1dadd3b FB |
4003 | case DWC3_DEVICE_EVENT_HIBER_REQ: |
4004 | if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, | |
4005 | "unexpected hibernation event\n")) | |
4006 | break; | |
4007 | ||
4008 | dwc3_gadget_hibernation_interrupt(dwc, event->event_info); | |
4009 | break; | |
72246da4 FB |
4010 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: |
4011 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); | |
4012 | break; | |
6f26ebb7 | 4013 | case DWC3_DEVICE_EVENT_SUSPEND: |
72704f87 | 4014 | /* It changed to be suspend event for version 2.30a and above */ |
9af21dd6 | 4015 | if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) { |
72704f87 BW |
4016 | /* |
4017 | * Ignore suspend event until the gadget enters into | |
4018 | * USB_STATE_CONFIGURED state. | |
4019 | */ | |
e81a7018 | 4020 | if (dwc->gadget->state >= USB_STATE_CONFIGURED) |
72704f87 BW |
4021 | dwc3_gadget_suspend_interrupt(dwc, |
4022 | event->event_info); | |
4023 | } | |
72246da4 FB |
4024 | break; |
4025 | case DWC3_DEVICE_EVENT_SOF: | |
72246da4 | 4026 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: |
72246da4 | 4027 | case DWC3_DEVICE_EVENT_CMD_CMPL: |
72246da4 | 4028 | case DWC3_DEVICE_EVENT_OVERFLOW: |
72246da4 FB |
4029 | break; |
4030 | default: | |
e9f2aa87 | 4031 | dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); |
72246da4 FB |
4032 | } |
4033 | } | |
4034 | ||
4035 | static void dwc3_process_event_entry(struct dwc3 *dwc, | |
4036 | const union dwc3_event *event) | |
4037 | { | |
43c96be1 | 4038 | trace_dwc3_event(event->raw, dwc); |
2c4cbe6e | 4039 | |
dfc5e805 FB |
4040 | if (!event->type.is_devspec) |
4041 | dwc3_endpoint_interrupt(dwc, &event->depevt); | |
4042 | else if (event->type.type == DWC3_EVENT_TYPE_DEV) | |
72246da4 | 4043 | dwc3_gadget_interrupt(dwc, &event->devt); |
dfc5e805 | 4044 | else |
72246da4 | 4045 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); |
72246da4 FB |
4046 | } |
4047 | ||
dea520a4 | 4048 | static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) |
b15a762f | 4049 | { |
dea520a4 | 4050 | struct dwc3 *dwc = evt->dwc; |
b15a762f | 4051 | irqreturn_t ret = IRQ_NONE; |
f42f2447 | 4052 | int left; |
e8adfc30 | 4053 | u32 reg; |
b15a762f | 4054 | |
f42f2447 | 4055 | left = evt->count; |
b15a762f | 4056 | |
f42f2447 FB |
4057 | if (!(evt->flags & DWC3_EVENT_PENDING)) |
4058 | return IRQ_NONE; | |
b15a762f | 4059 | |
f42f2447 FB |
4060 | while (left > 0) { |
4061 | union dwc3_event event; | |
b15a762f | 4062 | |
ebbb2d59 | 4063 | event.raw = *(u32 *) (evt->cache + evt->lpos); |
b15a762f | 4064 | |
f42f2447 | 4065 | dwc3_process_event_entry(dwc, &event); |
b15a762f | 4066 | |
f42f2447 FB |
4067 | /* |
4068 | * FIXME we wrap around correctly to the next entry as | |
4069 | * almost all entries are 4 bytes in size. There is one | |
4070 | * entry which has 12 bytes which is a regular entry | |
4071 | * followed by 8 bytes data. ATM I don't know how | |
4072 | * things are organized if we get next to the a | |
4073 | * boundary so I worry about that once we try to handle | |
4074 | * that. | |
4075 | */ | |
caefe6c7 | 4076 | evt->lpos = (evt->lpos + 4) % evt->length; |
f42f2447 | 4077 | left -= 4; |
f42f2447 | 4078 | } |
b15a762f | 4079 | |
f42f2447 FB |
4080 | evt->count = 0; |
4081 | evt->flags &= ~DWC3_EVENT_PENDING; | |
4082 | ret = IRQ_HANDLED; | |
b15a762f | 4083 | |
f42f2447 | 4084 | /* Unmask interrupt */ |
660e9bde | 4085 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
f42f2447 | 4086 | reg &= ~DWC3_GEVNTSIZ_INTMASK; |
660e9bde | 4087 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
b15a762f | 4088 | |
cf40b86b JY |
4089 | if (dwc->imod_interval) { |
4090 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); | |
4091 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); | |
4092 | } | |
4093 | ||
f42f2447 FB |
4094 | return ret; |
4095 | } | |
e8adfc30 | 4096 | |
dea520a4 | 4097 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) |
f42f2447 | 4098 | { |
dea520a4 FB |
4099 | struct dwc3_event_buffer *evt = _evt; |
4100 | struct dwc3 *dwc = evt->dwc; | |
e5f68b4a | 4101 | unsigned long flags; |
f42f2447 | 4102 | irqreturn_t ret = IRQ_NONE; |
f42f2447 | 4103 | |
e5f68b4a | 4104 | spin_lock_irqsave(&dwc->lock, flags); |
dea520a4 | 4105 | ret = dwc3_process_event_buf(evt); |
e5f68b4a | 4106 | spin_unlock_irqrestore(&dwc->lock, flags); |
b15a762f FB |
4107 | |
4108 | return ret; | |
4109 | } | |
4110 | ||
dea520a4 | 4111 | static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) |
72246da4 | 4112 | { |
dea520a4 | 4113 | struct dwc3 *dwc = evt->dwc; |
ebbb2d59 | 4114 | u32 amount; |
72246da4 | 4115 | u32 count; |
e8adfc30 | 4116 | u32 reg; |
72246da4 | 4117 | |
fc8bb91b FB |
4118 | if (pm_runtime_suspended(dwc->dev)) { |
4119 | pm_runtime_get(dwc->dev); | |
4120 | disable_irq_nosync(dwc->irq_gadget); | |
4121 | dwc->pending_events = true; | |
4122 | return IRQ_HANDLED; | |
4123 | } | |
4124 | ||
d325a1de TN |
4125 | /* |
4126 | * With PCIe legacy interrupt, test shows that top-half irq handler can | |
4127 | * be called again after HW interrupt deassertion. Check if bottom-half | |
4128 | * irq event handler completes before caching new event to prevent | |
4129 | * losing events. | |
4130 | */ | |
4131 | if (evt->flags & DWC3_EVENT_PENDING) | |
4132 | return IRQ_HANDLED; | |
4133 | ||
660e9bde | 4134 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); |
72246da4 FB |
4135 | count &= DWC3_GEVNTCOUNT_MASK; |
4136 | if (!count) | |
4137 | return IRQ_NONE; | |
4138 | ||
b15a762f FB |
4139 | evt->count = count; |
4140 | evt->flags |= DWC3_EVENT_PENDING; | |
72246da4 | 4141 | |
e8adfc30 | 4142 | /* Mask interrupt */ |
660e9bde | 4143 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
e8adfc30 | 4144 | reg |= DWC3_GEVNTSIZ_INTMASK; |
660e9bde | 4145 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
e8adfc30 | 4146 | |
ebbb2d59 JY |
4147 | amount = min(count, evt->length - evt->lpos); |
4148 | memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); | |
4149 | ||
4150 | if (amount < count) | |
4151 | memcpy(evt->cache, evt->buf, count - amount); | |
4152 | ||
65aca320 JY |
4153 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); |
4154 | ||
b15a762f | 4155 | return IRQ_WAKE_THREAD; |
72246da4 FB |
4156 | } |
4157 | ||
dea520a4 | 4158 | static irqreturn_t dwc3_interrupt(int irq, void *_evt) |
72246da4 | 4159 | { |
dea520a4 | 4160 | struct dwc3_event_buffer *evt = _evt; |
72246da4 | 4161 | |
dea520a4 | 4162 | return dwc3_check_event_buf(evt); |
72246da4 FB |
4163 | } |
4164 | ||
6db3812e FB |
4165 | static int dwc3_gadget_get_irq(struct dwc3 *dwc) |
4166 | { | |
4167 | struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); | |
4168 | int irq; | |
4169 | ||
f146b40b | 4170 | irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral"); |
6db3812e FB |
4171 | if (irq > 0) |
4172 | goto out; | |
4173 | ||
4174 | if (irq == -EPROBE_DEFER) | |
4175 | goto out; | |
4176 | ||
f146b40b | 4177 | irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3"); |
6db3812e FB |
4178 | if (irq > 0) |
4179 | goto out; | |
4180 | ||
4181 | if (irq == -EPROBE_DEFER) | |
4182 | goto out; | |
4183 | ||
4184 | irq = platform_get_irq(dwc3_pdev, 0); | |
4185 | if (irq > 0) | |
4186 | goto out; | |
4187 | ||
6db3812e FB |
4188 | if (!irq) |
4189 | irq = -EINVAL; | |
4190 | ||
4191 | out: | |
4192 | return irq; | |
4193 | } | |
4194 | ||
e81a7018 PC |
4195 | static void dwc_gadget_release(struct device *dev) |
4196 | { | |
4197 | struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); | |
4198 | ||
4199 | kfree(gadget); | |
4200 | } | |
4201 | ||
72246da4 | 4202 | /** |
bfad65ee | 4203 | * dwc3_gadget_init - initializes gadget related registers |
1d046793 | 4204 | * @dwc: pointer to our controller context structure |
72246da4 FB |
4205 | * |
4206 | * Returns 0 on success otherwise negative errno. | |
4207 | */ | |
41ac7b3a | 4208 | int dwc3_gadget_init(struct dwc3 *dwc) |
72246da4 | 4209 | { |
6db3812e FB |
4210 | int ret; |
4211 | int irq; | |
e81a7018 | 4212 | struct device *dev; |
9522def4 | 4213 | |
6db3812e FB |
4214 | irq = dwc3_gadget_get_irq(dwc); |
4215 | if (irq < 0) { | |
4216 | ret = irq; | |
4217 | goto err0; | |
9522def4 RQ |
4218 | } |
4219 | ||
4220 | dwc->irq_gadget = irq; | |
72246da4 | 4221 | |
d64ff406 AB |
4222 | dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, |
4223 | sizeof(*dwc->ep0_trb) * 2, | |
4224 | &dwc->ep0_trb_addr, GFP_KERNEL); | |
72246da4 FB |
4225 | if (!dwc->ep0_trb) { |
4226 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); | |
4227 | ret = -ENOMEM; | |
7d5e650a | 4228 | goto err0; |
72246da4 FB |
4229 | } |
4230 | ||
4199c5f8 | 4231 | dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); |
72246da4 | 4232 | if (!dwc->setup_buf) { |
72246da4 | 4233 | ret = -ENOMEM; |
7d5e650a | 4234 | goto err1; |
72246da4 FB |
4235 | } |
4236 | ||
905dc04e FB |
4237 | dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, |
4238 | &dwc->bounce_addr, GFP_KERNEL); | |
4239 | if (!dwc->bounce) { | |
4240 | ret = -ENOMEM; | |
d6e5a549 | 4241 | goto err2; |
905dc04e FB |
4242 | } |
4243 | ||
bb014736 | 4244 | init_completion(&dwc->ep0_in_setup); |
e81a7018 PC |
4245 | dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL); |
4246 | if (!dwc->gadget) { | |
4247 | ret = -ENOMEM; | |
4248 | goto err3; | |
4249 | } | |
bb014736 | 4250 | |
e81a7018 | 4251 | |
268bbde7 | 4252 | usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release); |
e81a7018 PC |
4253 | dev = &dwc->gadget->dev; |
4254 | dev->platform_data = dwc; | |
4255 | dwc->gadget->ops = &dwc3_gadget_ops; | |
4256 | dwc->gadget->speed = USB_SPEED_UNKNOWN; | |
f551037c | 4257 | dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; |
e81a7018 PC |
4258 | dwc->gadget->sg_supported = true; |
4259 | dwc->gadget->name = "dwc3-gadget"; | |
475e8be5 | 4260 | dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable; |
72246da4 | 4261 | |
b9e51b2b BM |
4262 | /* |
4263 | * FIXME We might be setting max_speed to <SUPER, however versions | |
4264 | * <2.20a of dwc3 have an issue with metastability (documented | |
4265 | * elsewhere in this driver) which tells us we can't set max speed to | |
4266 | * anything lower than SUPER. | |
4267 | * | |
4268 | * Because gadget.max_speed is only used by composite.c and function | |
4269 | * drivers (i.e. it won't go into dwc3's registers) we are allowing this | |
4270 | * to happen so we avoid sending SuperSpeed Capability descriptor | |
4271 | * together with our BOS descriptor as that could confuse host into | |
4272 | * thinking we can handle super speed. | |
4273 | * | |
4274 | * Note that, in fact, we won't even support GetBOS requests when speed | |
4275 | * is less than super speed because we don't have means, yet, to tell | |
4276 | * composite.c that we are USB 2.0 + LPM ECN. | |
4277 | */ | |
9af21dd6 | 4278 | if (DWC3_VER_IS_PRIOR(DWC3, 220A) && |
42bf02ec | 4279 | !dwc->dis_metastability_quirk) |
5eb30ced | 4280 | dev_info(dwc->dev, "changing max_speed on rev %08x\n", |
b9e51b2b BM |
4281 | dwc->revision); |
4282 | ||
e81a7018 | 4283 | dwc->gadget->max_speed = dwc->maximum_speed; |
67848146 | 4284 | dwc->gadget->max_ssp_rate = dwc->max_ssp_rate; |
b9e51b2b | 4285 | |
72246da4 FB |
4286 | /* |
4287 | * REVISIT: Here we should clear all pending IRQs to be | |
4288 | * sure we're starting from a well known location. | |
4289 | */ | |
4290 | ||
f3bcfc7e | 4291 | ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); |
72246da4 | 4292 | if (ret) |
e81a7018 | 4293 | goto err4; |
72246da4 | 4294 | |
e81a7018 | 4295 | ret = usb_add_gadget(dwc->gadget); |
72246da4 | 4296 | if (ret) { |
e81a7018 PC |
4297 | dev_err(dwc->dev, "failed to add gadget\n"); |
4298 | goto err5; | |
72246da4 FB |
4299 | } |
4300 | ||
072cab8a TN |
4301 | if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS) |
4302 | dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate); | |
4303 | else | |
4304 | dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed); | |
169e3b68 | 4305 | |
72246da4 FB |
4306 | return 0; |
4307 | ||
e81a7018 | 4308 | err5: |
d6e5a549 | 4309 | dwc3_gadget_free_endpoints(dwc); |
e81a7018 PC |
4310 | err4: |
4311 | usb_put_gadget(dwc->gadget); | |
03715ea2 | 4312 | dwc->gadget = NULL; |
7d5e650a | 4313 | err3: |
d6e5a549 FB |
4314 | dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, |
4315 | dwc->bounce_addr); | |
5812b1c2 | 4316 | |
7d5e650a | 4317 | err2: |
0fc9a1be | 4318 | kfree(dwc->setup_buf); |
72246da4 | 4319 | |
7d5e650a | 4320 | err1: |
d64ff406 | 4321 | dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, |
72246da4 FB |
4322 | dwc->ep0_trb, dwc->ep0_trb_addr); |
4323 | ||
72246da4 FB |
4324 | err0: |
4325 | return ret; | |
4326 | } | |
4327 | ||
7415f17c FB |
4328 | /* -------------------------------------------------------------------------- */ |
4329 | ||
72246da4 FB |
4330 | void dwc3_gadget_exit(struct dwc3 *dwc) |
4331 | { | |
03715ea2 JP |
4332 | if (!dwc->gadget) |
4333 | return; | |
4334 | ||
bb9c74a5 | 4335 | usb_del_gadget(dwc->gadget); |
72246da4 | 4336 | dwc3_gadget_free_endpoints(dwc); |
bb9c74a5 | 4337 | usb_put_gadget(dwc->gadget); |
905dc04e | 4338 | dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, |
d6e5a549 | 4339 | dwc->bounce_addr); |
0fc9a1be | 4340 | kfree(dwc->setup_buf); |
d64ff406 | 4341 | dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, |
d6e5a549 | 4342 | dwc->ep0_trb, dwc->ep0_trb_addr); |
72246da4 | 4343 | } |
7415f17c | 4344 | |
0b0231aa | 4345 | int dwc3_gadget_suspend(struct dwc3 *dwc) |
7415f17c | 4346 | { |
9772b47a RQ |
4347 | if (!dwc->gadget_driver) |
4348 | return 0; | |
4349 | ||
1551e35e | 4350 | dwc3_gadget_run_stop(dwc, false, false); |
9f8a67b6 FB |
4351 | dwc3_disconnect_gadget(dwc); |
4352 | __dwc3_gadget_stop(dwc); | |
7415f17c FB |
4353 | |
4354 | return 0; | |
4355 | } | |
4356 | ||
4357 | int dwc3_gadget_resume(struct dwc3 *dwc) | |
4358 | { | |
7415f17c FB |
4359 | int ret; |
4360 | ||
8217f07a | 4361 | if (!dwc->gadget_driver || !dwc->softconnect) |
9772b47a RQ |
4362 | return 0; |
4363 | ||
9f8a67b6 FB |
4364 | ret = __dwc3_gadget_start(dwc); |
4365 | if (ret < 0) | |
7415f17c FB |
4366 | goto err0; |
4367 | ||
9f8a67b6 FB |
4368 | ret = dwc3_gadget_run_stop(dwc, true, false); |
4369 | if (ret < 0) | |
7415f17c FB |
4370 | goto err1; |
4371 | ||
7415f17c FB |
4372 | return 0; |
4373 | ||
4374 | err1: | |
9f8a67b6 | 4375 | __dwc3_gadget_stop(dwc); |
7415f17c FB |
4376 | |
4377 | err0: | |
4378 | return ret; | |
4379 | } | |
fc8bb91b FB |
4380 | |
4381 | void dwc3_gadget_process_pending_events(struct dwc3 *dwc) | |
4382 | { | |
4383 | if (dwc->pending_events) { | |
4384 | dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf); | |
4385 | dwc->pending_events = false; | |
4386 | enable_irq(dwc->irq_gadget); | |
4387 | } | |
4388 | } |