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