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