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