]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0 |
85d5e707 KVA |
2 | /** |
3 | * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link | |
4 | * | |
a94a4071 | 5 | * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com |
85d5e707 KVA |
6 | * |
7 | * Authors: Felipe Balbi <balbi@ti.com>, | |
8 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> | |
9 | * | |
30c31d58 KVA |
10 | * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/gadget.c) and ported |
11 | * to uboot. | |
85d5e707 | 12 | * |
30c31d58 | 13 | * commit 8e74475b0e : usb: dwc3: gadget: use udc-core's reset notifier |
85d5e707 KVA |
14 | */ |
15 | ||
1eb69ae4 | 16 | #include <cpu_func.h> |
f7ae49fc | 17 | #include <log.h> |
747a0a5b | 18 | #include <malloc.h> |
df5eabcb | 19 | #include <dm.h> |
336d4615 | 20 | #include <dm/device_compat.h> |
61b29b82 | 21 | #include <dm/devres.h> |
84b8bf6d | 22 | #include <linux/bug.h> |
c05ed00a | 23 | #include <linux/delay.h> |
9d86b89c | 24 | #include <linux/dma-mapping.h> |
85d5e707 | 25 | #include <linux/list.h> |
1e94b46f | 26 | #include <linux/printk.h> |
85d5e707 KVA |
27 | |
28 | #include <linux/usb/ch9.h> | |
29 | #include <linux/usb/gadget.h> | |
30 | ||
85d5e707 KVA |
31 | #include "core.h" |
32 | #include "gadget.h" | |
33 | #include "io.h" | |
34 | ||
747a0a5b KVA |
35 | #include "linux-compat.h" |
36 | ||
85d5e707 KVA |
37 | /** |
38 | * dwc3_gadget_set_test_mode - Enables USB2 Test Modes | |
39 | * @dwc: pointer to our context structure | |
40 | * @mode: the mode to set (J, K SE0 NAK, Force Enable) | |
41 | * | |
42 | * Caller should take care of locking. This function will | |
43 | * return 0 on success or -EINVAL if wrong Test Selector | |
44 | * is passed | |
45 | */ | |
46 | int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) | |
47 | { | |
48 | u32 reg; | |
49 | ||
50 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
51 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; | |
52 | ||
53 | switch (mode) { | |
54 | case TEST_J: | |
55 | case TEST_K: | |
56 | case TEST_SE0_NAK: | |
57 | case TEST_PACKET: | |
58 | case TEST_FORCE_EN: | |
59 | reg |= mode << 1; | |
60 | break; | |
61 | default: | |
62 | return -EINVAL; | |
63 | } | |
64 | ||
65 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
70 | /** | |
71 | * dwc3_gadget_get_link_state - Gets current state of USB Link | |
72 | * @dwc: pointer to our context structure | |
73 | * | |
74 | * Caller should take care of locking. This function will | |
75 | * return the link state on success (>= 0) or -ETIMEDOUT. | |
76 | */ | |
77 | int dwc3_gadget_get_link_state(struct dwc3 *dwc) | |
78 | { | |
79 | u32 reg; | |
80 | ||
81 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
82 | ||
83 | return DWC3_DSTS_USBLNKST(reg); | |
84 | } | |
85 | ||
86 | /** | |
87 | * dwc3_gadget_set_link_state - Sets USB Link to a particular State | |
88 | * @dwc: pointer to our context structure | |
89 | * @state: the state to put link into | |
90 | * | |
91 | * Caller should take care of locking. This function will | |
92 | * return 0 on success or -ETIMEDOUT. | |
93 | */ | |
94 | int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) | |
95 | { | |
96 | int retries = 10000; | |
97 | u32 reg; | |
98 | ||
99 | /* | |
100 | * Wait until device controller is ready. Only applies to 1.94a and | |
101 | * later RTL. | |
102 | */ | |
103 | if (dwc->revision >= DWC3_REVISION_194A) { | |
104 | while (--retries) { | |
105 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
106 | if (reg & DWC3_DSTS_DCNRD) | |
107 | udelay(5); | |
108 | else | |
109 | break; | |
110 | } | |
111 | ||
112 | if (retries <= 0) | |
113 | return -ETIMEDOUT; | |
114 | } | |
115 | ||
116 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
117 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; | |
118 | ||
119 | /* set requested state */ | |
120 | reg |= DWC3_DCTL_ULSTCHNGREQ(state); | |
121 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
122 | ||
123 | /* | |
124 | * The following code is racy when called from dwc3_gadget_wakeup, | |
125 | * and is not needed, at least on newer versions | |
126 | */ | |
127 | if (dwc->revision >= DWC3_REVISION_194A) | |
128 | return 0; | |
129 | ||
130 | /* wait for a change in DSTS */ | |
131 | retries = 10000; | |
132 | while (--retries) { | |
133 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
134 | ||
135 | if (DWC3_DSTS_USBLNKST(reg) == state) | |
136 | return 0; | |
137 | ||
138 | udelay(5); | |
139 | } | |
140 | ||
141 | dev_vdbg(dwc->dev, "link state change request timed out\n"); | |
142 | ||
143 | return -ETIMEDOUT; | |
144 | } | |
145 | ||
146 | /** | |
147 | * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case | |
148 | * @dwc: pointer to our context structure | |
149 | * | |
150 | * This function will a best effort FIFO allocation in order | |
151 | * to improve FIFO usage and throughput, while still allowing | |
152 | * us to enable as many endpoints as possible. | |
153 | * | |
154 | * Keep in mind that this operation will be highly dependent | |
155 | * on the configured size for RAM1 - which contains TxFifo -, | |
156 | * the amount of endpoints enabled on coreConsultant tool, and | |
157 | * the width of the Master Bus. | |
158 | * | |
159 | * In the ideal world, we would always be able to satisfy the | |
160 | * following equation: | |
161 | * | |
162 | * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \ | |
163 | * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes | |
164 | * | |
165 | * Unfortunately, due to many variables that's not always the case. | |
166 | */ | |
167 | int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc) | |
168 | { | |
169 | int last_fifo_depth = 0; | |
85d5e707 KVA |
170 | int fifo_size; |
171 | int mdwidth; | |
172 | int num; | |
173 | ||
174 | if (!dwc->needs_fifo_resize) | |
175 | return 0; | |
176 | ||
85d5e707 KVA |
177 | mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); |
178 | ||
179 | /* MDWIDTH is represented in bits, we need it in bytes */ | |
180 | mdwidth >>= 3; | |
181 | ||
182 | /* | |
183 | * FIXME For now we will only allocate 1 wMaxPacketSize space | |
184 | * for each enabled endpoint, later patches will come to | |
185 | * improve this algorithm so that we better use the internal | |
186 | * FIFO space | |
187 | */ | |
188 | for (num = 0; num < dwc->num_in_eps; num++) { | |
189 | /* bit0 indicates direction; 1 means IN ep */ | |
190 | struct dwc3_ep *dep = dwc->eps[(num << 1) | 1]; | |
191 | int mult = 1; | |
192 | int tmp; | |
193 | ||
194 | if (!(dep->flags & DWC3_EP_ENABLED)) | |
195 | continue; | |
196 | ||
197 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) | |
198 | || usb_endpoint_xfer_isoc(dep->endpoint.desc)) | |
199 | mult = 3; | |
200 | ||
201 | /* | |
202 | * REVISIT: the following assumes we will always have enough | |
203 | * space available on the FIFO RAM for all possible use cases. | |
204 | * Make sure that's true somehow and change FIFO allocation | |
205 | * accordingly. | |
206 | * | |
207 | * If we have Bulk or Isochronous endpoints, we want | |
208 | * them to be able to be very, very fast. So we're giving | |
209 | * those endpoints a fifo_size which is enough for 3 full | |
210 | * packets | |
211 | */ | |
212 | tmp = mult * (dep->endpoint.maxpacket + mdwidth); | |
213 | tmp += mdwidth; | |
214 | ||
215 | fifo_size = DIV_ROUND_UP(tmp, mdwidth); | |
216 | ||
217 | fifo_size |= (last_fifo_depth << 16); | |
218 | ||
219 | dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n", | |
220 | dep->name, last_fifo_depth, fifo_size & 0xffff); | |
221 | ||
222 | dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num), fifo_size); | |
223 | ||
224 | last_fifo_depth += (fifo_size & 0xffff); | |
225 | } | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
230 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, | |
231 | int status) | |
232 | { | |
233 | struct dwc3 *dwc = dep->dwc; | |
85d5e707 KVA |
234 | |
235 | if (req->queued) { | |
747a0a5b KVA |
236 | dep->busy_slot++; |
237 | /* | |
238 | * Skip LINK TRB. We can't use req->trb and check for | |
239 | * DWC3_TRBCTL_LINK_TRB because it points the TRB we | |
240 | * just completed (not the LINK TRB). | |
241 | */ | |
242 | if (((dep->busy_slot & DWC3_TRB_MASK) == | |
243 | DWC3_TRB_NUM- 1) && | |
244 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) | |
85d5e707 | 245 | dep->busy_slot++; |
85d5e707 KVA |
246 | req->queued = false; |
247 | } | |
747a0a5b | 248 | |
85d5e707 KVA |
249 | list_del(&req->list); |
250 | req->trb = NULL; | |
fd15b58c MS |
251 | if (req->request.length) |
252 | dwc3_flush_cache((uintptr_t)req->request.dma, req->request.length); | |
85d5e707 KVA |
253 | |
254 | if (req->request.status == -EINPROGRESS) | |
255 | req->request.status = status; | |
256 | ||
257 | if (dwc->ep0_bounced && dep->number == 0) | |
258 | dwc->ep0_bounced = false; | |
259 | else | |
260 | usb_gadget_unmap_request(&dwc->gadget, &req->request, | |
261 | req->direction); | |
262 | ||
263 | dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n", | |
264 | req, dep->name, req->request.actual, | |
265 | req->request.length, status); | |
85d5e707 KVA |
266 | |
267 | spin_unlock(&dwc->lock); | |
268 | usb_gadget_giveback_request(&dep->endpoint, &req->request); | |
269 | spin_lock(&dwc->lock); | |
270 | } | |
271 | ||
272 | int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param) | |
273 | { | |
274 | u32 timeout = 500; | |
275 | u32 reg; | |
276 | ||
85d5e707 KVA |
277 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); |
278 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); | |
279 | ||
280 | do { | |
281 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); | |
282 | if (!(reg & DWC3_DGCMD_CMDACT)) { | |
283 | dev_vdbg(dwc->dev, "Command Complete --> %d\n", | |
284 | DWC3_DGCMD_STATUS(reg)); | |
285 | return 0; | |
286 | } | |
287 | ||
288 | /* | |
289 | * We can't sleep here, because it's also called from | |
290 | * interrupt context. | |
291 | */ | |
292 | timeout--; | |
293 | if (!timeout) | |
294 | return -ETIMEDOUT; | |
295 | udelay(1); | |
296 | } while (1); | |
297 | } | |
298 | ||
299 | int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep, | |
300 | unsigned cmd, struct dwc3_gadget_ep_cmd_params *params) | |
301 | { | |
85d5e707 KVA |
302 | u32 timeout = 500; |
303 | u32 reg; | |
967b31c3 FB |
304 | |
305 | int susphy = false; | |
13395507 | 306 | int ret = -EINVAL; |
85d5e707 | 307 | |
967b31c3 FB |
308 | /* |
309 | * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if | |
310 | * we're issuing an endpoint command, we must check if | |
311 | * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it. | |
312 | * | |
313 | * We will also set SUSPHY bit to what it was before returning as stated | |
314 | * by the same section on Synopsys databook. | |
315 | */ | |
316 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); | |
317 | if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { | |
318 | susphy = true; | |
319 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; | |
320 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); | |
321 | } | |
322 | ||
85d5e707 KVA |
323 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0); |
324 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1); | |
325 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2); | |
326 | ||
327 | dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT); | |
328 | do { | |
329 | reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep)); | |
330 | if (!(reg & DWC3_DEPCMD_CMDACT)) { | |
331 | dev_vdbg(dwc->dev, "Command Complete --> %d\n", | |
332 | DWC3_DEPCMD_STATUS(reg)); | |
13395507 FB |
333 | ret = 0; |
334 | break; | |
85d5e707 KVA |
335 | } |
336 | ||
337 | /* | |
338 | * We can't sleep here, because it is also called from | |
339 | * interrupt context. | |
340 | */ | |
341 | timeout--; | |
13395507 FB |
342 | if (!timeout) { |
343 | ret = -ETIMEDOUT; | |
344 | break; | |
345 | } | |
85d5e707 KVA |
346 | |
347 | udelay(1); | |
348 | } while (1); | |
13395507 | 349 | |
967b31c3 FB |
350 | if (unlikely(susphy)) { |
351 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); | |
352 | reg |= DWC3_GUSB2PHYCFG_SUSPHY; | |
353 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); | |
354 | } | |
355 | ||
13395507 | 356 | return ret; |
85d5e707 KVA |
357 | } |
358 | ||
359 | static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, | |
360 | struct dwc3_trb *trb) | |
361 | { | |
362 | u32 offset = (char *) trb - (char *) dep->trb_pool; | |
363 | ||
364 | return dep->trb_pool_dma + offset; | |
365 | } | |
366 | ||
367 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) | |
368 | { | |
85d5e707 KVA |
369 | if (dep->trb_pool) |
370 | return 0; | |
371 | ||
372 | if (dep->number == 0 || dep->number == 1) | |
373 | return 0; | |
374 | ||
747a0a5b KVA |
375 | dep->trb_pool = dma_alloc_coherent(sizeof(struct dwc3_trb) * |
376 | DWC3_TRB_NUM, | |
377 | (unsigned long *)&dep->trb_pool_dma); | |
85d5e707 KVA |
378 | if (!dep->trb_pool) { |
379 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", | |
380 | dep->name); | |
381 | return -ENOMEM; | |
382 | } | |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
387 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) | |
388 | { | |
747a0a5b | 389 | dma_free_coherent(dep->trb_pool); |
85d5e707 KVA |
390 | |
391 | dep->trb_pool = NULL; | |
392 | dep->trb_pool_dma = 0; | |
393 | } | |
394 | ||
395 | static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep) | |
396 | { | |
397 | struct dwc3_gadget_ep_cmd_params params; | |
398 | u32 cmd; | |
399 | ||
400 | memset(¶ms, 0x00, sizeof(params)); | |
401 | ||
402 | if (dep->number != 1) { | |
403 | cmd = DWC3_DEPCMD_DEPSTARTCFG; | |
404 | /* XferRscIdx == 0 for ep0 and 2 for the remaining */ | |
405 | if (dep->number > 1) { | |
406 | if (dwc->start_config_issued) | |
407 | return 0; | |
408 | dwc->start_config_issued = true; | |
409 | cmd |= DWC3_DEPCMD_PARAM(2); | |
410 | } | |
411 | ||
412 | return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, ¶ms); | |
413 | } | |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
418 | static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep, | |
419 | const struct usb_endpoint_descriptor *desc, | |
420 | const struct usb_ss_ep_comp_descriptor *comp_desc, | |
421 | bool ignore, bool restore) | |
422 | { | |
423 | struct dwc3_gadget_ep_cmd_params params; | |
424 | ||
425 | memset(¶ms, 0x00, sizeof(params)); | |
426 | ||
427 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) | |
428 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); | |
429 | ||
430 | /* Burst size is only needed in SuperSpeed mode */ | |
431 | if (dwc->gadget.speed == USB_SPEED_SUPER) { | |
432 | u32 burst = dep->endpoint.maxburst - 1; | |
433 | ||
434 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst); | |
435 | } | |
436 | ||
437 | if (ignore) | |
438 | params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM; | |
439 | ||
440 | if (restore) { | |
441 | params.param0 |= DWC3_DEPCFG_ACTION_RESTORE; | |
442 | params.param2 |= dep->saved_state; | |
443 | } | |
444 | ||
445 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN | |
446 | | DWC3_DEPCFG_XFER_NOT_READY_EN; | |
447 | ||
448 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { | |
449 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE | |
450 | | DWC3_DEPCFG_STREAM_EVENT_EN; | |
451 | dep->stream_capable = true; | |
452 | } | |
453 | ||
454 | if (!usb_endpoint_xfer_control(desc)) | |
455 | params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; | |
456 | ||
457 | /* | |
458 | * We are doing 1:1 mapping for endpoints, meaning | |
459 | * Physical Endpoints 2 maps to Logical Endpoint 2 and | |
460 | * so on. We consider the direction bit as part of the physical | |
461 | * endpoint number. So USB endpoint 0x81 is 0x03. | |
462 | */ | |
463 | params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); | |
464 | ||
465 | /* | |
466 | * We must use the lower 16 TX FIFOs even though | |
467 | * HW might have more | |
468 | */ | |
469 | if (dep->direction) | |
470 | params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); | |
471 | ||
472 | if (desc->bInterval) { | |
473 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1); | |
474 | dep->interval = 1 << (desc->bInterval - 1); | |
475 | } | |
476 | ||
477 | return dwc3_send_gadget_ep_cmd(dwc, dep->number, | |
478 | DWC3_DEPCMD_SETEPCONFIG, ¶ms); | |
479 | } | |
480 | ||
481 | static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep) | |
482 | { | |
483 | struct dwc3_gadget_ep_cmd_params params; | |
484 | ||
485 | memset(¶ms, 0x00, sizeof(params)); | |
486 | ||
487 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); | |
488 | ||
489 | return dwc3_send_gadget_ep_cmd(dwc, dep->number, | |
490 | DWC3_DEPCMD_SETTRANSFRESOURCE, ¶ms); | |
491 | } | |
492 | ||
493 | /** | |
494 | * __dwc3_gadget_ep_enable - Initializes a HW endpoint | |
495 | * @dep: endpoint to be initialized | |
496 | * @desc: USB Endpoint Descriptor | |
497 | * | |
498 | * Caller should take care of locking | |
499 | */ | |
500 | static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, | |
501 | const struct usb_endpoint_descriptor *desc, | |
502 | const struct usb_ss_ep_comp_descriptor *comp_desc, | |
503 | bool ignore, bool restore) | |
504 | { | |
505 | struct dwc3 *dwc = dep->dwc; | |
506 | u32 reg; | |
507 | int ret; | |
508 | ||
509 | dev_vdbg(dwc->dev, "Enabling %s\n", dep->name); | |
510 | ||
511 | if (!(dep->flags & DWC3_EP_ENABLED)) { | |
512 | ret = dwc3_gadget_start_config(dwc, dep); | |
513 | if (ret) | |
514 | return ret; | |
515 | } | |
516 | ||
517 | ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore, | |
518 | restore); | |
519 | if (ret) | |
520 | return ret; | |
521 | ||
522 | if (!(dep->flags & DWC3_EP_ENABLED)) { | |
523 | struct dwc3_trb *trb_st_hw; | |
524 | struct dwc3_trb *trb_link; | |
525 | ||
526 | ret = dwc3_gadget_set_xfer_resource(dwc, dep); | |
527 | if (ret) | |
528 | return ret; | |
529 | ||
530 | dep->endpoint.desc = desc; | |
531 | dep->comp_desc = comp_desc; | |
532 | dep->type = usb_endpoint_type(desc); | |
533 | dep->flags |= DWC3_EP_ENABLED; | |
534 | ||
535 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); | |
536 | reg |= DWC3_DALEPENA_EP(dep->number); | |
537 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); | |
538 | ||
539 | if (!usb_endpoint_xfer_isoc(desc)) | |
540 | return 0; | |
541 | ||
542 | /* Link TRB for ISOC. The HWO bit is never reset */ | |
543 | trb_st_hw = &dep->trb_pool[0]; | |
544 | ||
545 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; | |
546 | memset(trb_link, 0, sizeof(*trb_link)); | |
547 | ||
548 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); | |
549 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); | |
550 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; | |
551 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; | |
552 | } | |
553 | ||
554 | return 0; | |
555 | } | |
556 | ||
557 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force); | |
558 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) | |
559 | { | |
560 | struct dwc3_request *req; | |
561 | ||
562 | if (!list_empty(&dep->req_queued)) { | |
563 | dwc3_stop_active_transfer(dwc, dep->number, true); | |
564 | ||
565 | /* - giveback all requests to gadget driver */ | |
566 | while (!list_empty(&dep->req_queued)) { | |
567 | req = next_request(&dep->req_queued); | |
568 | ||
569 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); | |
570 | } | |
571 | } | |
572 | ||
573 | while (!list_empty(&dep->request_list)) { | |
574 | req = next_request(&dep->request_list); | |
575 | ||
576 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); | |
577 | } | |
578 | } | |
579 | ||
580 | /** | |
581 | * __dwc3_gadget_ep_disable - Disables a HW endpoint | |
582 | * @dep: the endpoint to disable | |
583 | * | |
584 | * This function also removes requests which are currently processed ny the | |
585 | * hardware and those which are not yet scheduled. | |
586 | * Caller should take care of locking. | |
587 | */ | |
588 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) | |
589 | { | |
590 | struct dwc3 *dwc = dep->dwc; | |
591 | u32 reg; | |
592 | ||
593 | dwc3_remove_requests(dwc, dep); | |
594 | ||
595 | /* make sure HW endpoint isn't stalled */ | |
596 | if (dep->flags & DWC3_EP_STALL) | |
597 | __dwc3_gadget_ep_set_halt(dep, 0, false); | |
598 | ||
599 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); | |
600 | reg &= ~DWC3_DALEPENA_EP(dep->number); | |
601 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); | |
602 | ||
603 | dep->stream_capable = false; | |
604 | dep->endpoint.desc = NULL; | |
605 | dep->comp_desc = NULL; | |
606 | dep->type = 0; | |
607 | dep->flags = 0; | |
608 | ||
609 | return 0; | |
610 | } | |
611 | ||
612 | /* -------------------------------------------------------------------------- */ | |
613 | ||
614 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, | |
615 | const struct usb_endpoint_descriptor *desc) | |
616 | { | |
617 | return -EINVAL; | |
618 | } | |
619 | ||
620 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) | |
621 | { | |
622 | return -EINVAL; | |
623 | } | |
624 | ||
625 | /* -------------------------------------------------------------------------- */ | |
626 | ||
627 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, | |
628 | const struct usb_endpoint_descriptor *desc) | |
629 | { | |
630 | struct dwc3_ep *dep; | |
85d5e707 KVA |
631 | unsigned long flags; |
632 | int ret; | |
633 | ||
634 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { | |
635 | pr_debug("dwc3: invalid parameters\n"); | |
636 | return -EINVAL; | |
637 | } | |
638 | ||
639 | if (!desc->wMaxPacketSize) { | |
640 | pr_debug("dwc3: missing wMaxPacketSize\n"); | |
641 | return -EINVAL; | |
642 | } | |
643 | ||
644 | dep = to_dwc3_ep(ep); | |
85d5e707 KVA |
645 | |
646 | if (dep->flags & DWC3_EP_ENABLED) { | |
747a0a5b | 647 | WARN(true, "%s is already enabled\n", |
85d5e707 KVA |
648 | dep->name); |
649 | return 0; | |
650 | } | |
651 | ||
652 | switch (usb_endpoint_type(desc)) { | |
653 | case USB_ENDPOINT_XFER_CONTROL: | |
654 | strlcat(dep->name, "-control", sizeof(dep->name)); | |
655 | break; | |
656 | case USB_ENDPOINT_XFER_ISOC: | |
657 | strlcat(dep->name, "-isoc", sizeof(dep->name)); | |
658 | break; | |
659 | case USB_ENDPOINT_XFER_BULK: | |
660 | strlcat(dep->name, "-bulk", sizeof(dep->name)); | |
661 | break; | |
662 | case USB_ENDPOINT_XFER_INT: | |
663 | strlcat(dep->name, "-int", sizeof(dep->name)); | |
664 | break; | |
665 | default: | |
df5eabcb | 666 | dev_err(dep->dwc->dev, "invalid endpoint transfer type\n"); |
85d5e707 KVA |
667 | } |
668 | ||
669 | spin_lock_irqsave(&dwc->lock, flags); | |
670 | ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false); | |
671 | spin_unlock_irqrestore(&dwc->lock, flags); | |
672 | ||
673 | return ret; | |
674 | } | |
675 | ||
676 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) | |
677 | { | |
678 | struct dwc3_ep *dep; | |
85d5e707 KVA |
679 | unsigned long flags; |
680 | int ret; | |
681 | ||
682 | if (!ep) { | |
683 | pr_debug("dwc3: invalid parameters\n"); | |
684 | return -EINVAL; | |
685 | } | |
686 | ||
687 | dep = to_dwc3_ep(ep); | |
85d5e707 KVA |
688 | |
689 | if (!(dep->flags & DWC3_EP_ENABLED)) { | |
747a0a5b | 690 | WARN(true, "%s is already disabled\n", |
85d5e707 KVA |
691 | dep->name); |
692 | return 0; | |
693 | } | |
694 | ||
695 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", | |
696 | dep->number >> 1, | |
697 | (dep->number & 1) ? "in" : "out"); | |
698 | ||
699 | spin_lock_irqsave(&dwc->lock, flags); | |
700 | ret = __dwc3_gadget_ep_disable(dep); | |
701 | spin_unlock_irqrestore(&dwc->lock, flags); | |
702 | ||
703 | return ret; | |
704 | } | |
705 | ||
706 | static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, | |
707 | gfp_t gfp_flags) | |
708 | { | |
709 | struct dwc3_request *req; | |
710 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
711 | ||
712 | req = kzalloc(sizeof(*req), gfp_flags); | |
713 | if (!req) | |
714 | return NULL; | |
715 | ||
716 | req->epnum = dep->number; | |
717 | req->dep = dep; | |
718 | ||
85d5e707 KVA |
719 | return &req->request; |
720 | } | |
721 | ||
722 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, | |
723 | struct usb_request *request) | |
724 | { | |
725 | struct dwc3_request *req = to_dwc3_request(request); | |
726 | ||
85d5e707 KVA |
727 | kfree(req); |
728 | } | |
729 | ||
730 | /** | |
731 | * dwc3_prepare_one_trb - setup one TRB from one request | |
732 | * @dep: endpoint for which this request is prepared | |
733 | * @req: dwc3_request pointer | |
734 | */ | |
735 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, | |
736 | struct dwc3_request *req, dma_addr_t dma, | |
737 | unsigned length, unsigned last, unsigned chain, unsigned node) | |
738 | { | |
85d5e707 KVA |
739 | struct dwc3_trb *trb; |
740 | ||
df5eabcb SA |
741 | dev_vdbg(dep->dwc->dev, "%s: req %p dma %08llx length %d%s%s\n", |
742 | dep->name, req, (unsigned long long)dma, | |
743 | length, last ? " last" : "", chain ? " chain" : ""); | |
85d5e707 KVA |
744 | |
745 | ||
746 | trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK]; | |
747 | ||
748 | if (!req->trb) { | |
749 | dwc3_gadget_move_request_queued(req); | |
750 | req->trb = trb; | |
751 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); | |
752 | req->start_slot = dep->free_slot & DWC3_TRB_MASK; | |
753 | } | |
754 | ||
755 | dep->free_slot++; | |
756 | /* Skip the LINK-TRB on ISOC */ | |
757 | if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) && | |
758 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) | |
759 | dep->free_slot++; | |
760 | ||
761 | trb->size = DWC3_TRB_SIZE_LENGTH(length); | |
762 | trb->bpl = lower_32_bits(dma); | |
763 | trb->bph = upper_32_bits(dma); | |
764 | ||
765 | switch (usb_endpoint_type(dep->endpoint.desc)) { | |
766 | case USB_ENDPOINT_XFER_CONTROL: | |
767 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; | |
768 | break; | |
769 | ||
770 | case USB_ENDPOINT_XFER_ISOC: | |
771 | if (!node) | |
772 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; | |
773 | else | |
774 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; | |
775 | break; | |
776 | ||
777 | case USB_ENDPOINT_XFER_BULK: | |
778 | case USB_ENDPOINT_XFER_INT: | |
779 | trb->ctrl = DWC3_TRBCTL_NORMAL; | |
780 | break; | |
781 | default: | |
782 | /* | |
783 | * This is only possible with faulty memory because we | |
784 | * checked it already :) | |
785 | */ | |
786 | BUG(); | |
787 | } | |
788 | ||
789 | if (!req->request.no_interrupt && !chain) | |
790 | trb->ctrl |= DWC3_TRB_CTRL_IOC; | |
791 | ||
792 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { | |
793 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; | |
794 | trb->ctrl |= DWC3_TRB_CTRL_CSP; | |
795 | } else if (last) { | |
796 | trb->ctrl |= DWC3_TRB_CTRL_LST; | |
797 | } | |
798 | ||
799 | if (chain) | |
800 | trb->ctrl |= DWC3_TRB_CTRL_CHN; | |
801 | ||
802 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) | |
803 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id); | |
804 | ||
805 | trb->ctrl |= DWC3_TRB_CTRL_HWO; | |
526a50f8 | 806 | |
b7bf4a95 PT |
807 | dwc3_flush_cache((uintptr_t)dma, length); |
808 | dwc3_flush_cache((uintptr_t)trb, sizeof(*trb)); | |
85d5e707 KVA |
809 | } |
810 | ||
811 | /* | |
812 | * dwc3_prepare_trbs - setup TRBs from requests | |
813 | * @dep: endpoint for which requests are being prepared | |
814 | * @starting: true if the endpoint is idle and no requests are queued. | |
815 | * | |
816 | * The function goes through the requests list and sets up TRBs for the | |
817 | * transfers. The function returns once there are no more TRBs available or | |
818 | * it runs out of requests. | |
819 | */ | |
820 | static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting) | |
821 | { | |
822 | struct dwc3_request *req, *n; | |
823 | u32 trbs_left; | |
824 | u32 max; | |
85d5e707 KVA |
825 | |
826 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); | |
827 | ||
828 | /* the first request must not be queued */ | |
829 | trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK; | |
830 | ||
831 | /* Can't wrap around on a non-isoc EP since there's no link TRB */ | |
832 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) { | |
833 | max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK); | |
834 | if (trbs_left > max) | |
835 | trbs_left = max; | |
836 | } | |
837 | ||
838 | /* | |
839 | * If busy & slot are equal than it is either full or empty. If we are | |
840 | * starting to process requests then we are empty. Otherwise we are | |
841 | * full and don't do anything | |
842 | */ | |
843 | if (!trbs_left) { | |
844 | if (!starting) | |
845 | return; | |
846 | trbs_left = DWC3_TRB_NUM; | |
847 | /* | |
848 | * In case we start from scratch, we queue the ISOC requests | |
849 | * starting from slot 1. This is done because we use ring | |
850 | * buffer and have no LST bit to stop us. Instead, we place | |
851 | * IOC bit every TRB_NUM/4. We try to avoid having an interrupt | |
852 | * after the first request so we start at slot 1 and have | |
853 | * 7 requests proceed before we hit the first IOC. | |
854 | * Other transfer types don't use the ring buffer and are | |
855 | * processed from the first TRB until the last one. Since we | |
856 | * don't wrap around we have to start at the beginning. | |
857 | */ | |
858 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { | |
859 | dep->busy_slot = 1; | |
860 | dep->free_slot = 1; | |
861 | } else { | |
862 | dep->busy_slot = 0; | |
863 | dep->free_slot = 0; | |
864 | } | |
865 | } | |
866 | ||
867 | /* The last TRB is a link TRB, not used for xfer */ | |
868 | if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc)) | |
869 | return; | |
870 | ||
871 | list_for_each_entry_safe(req, n, &dep->request_list, list) { | |
872 | unsigned length; | |
873 | dma_addr_t dma; | |
85d5e707 | 874 | |
747a0a5b KVA |
875 | dma = req->request.dma; |
876 | length = req->request.length; | |
85d5e707 | 877 | |
747a0a5b | 878 | dwc3_prepare_one_trb(dep, req, dma, length, |
29e7fc19 | 879 | true, false, 0); |
85d5e707 | 880 | |
29e7fc19 | 881 | break; |
85d5e707 KVA |
882 | } |
883 | } | |
884 | ||
885 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param, | |
886 | int start_new) | |
887 | { | |
888 | struct dwc3_gadget_ep_cmd_params params; | |
889 | struct dwc3_request *req; | |
890 | struct dwc3 *dwc = dep->dwc; | |
891 | int ret; | |
892 | u32 cmd; | |
893 | ||
894 | if (start_new && (dep->flags & DWC3_EP_BUSY)) { | |
895 | dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name); | |
896 | return -EBUSY; | |
897 | } | |
898 | dep->flags &= ~DWC3_EP_PENDING_REQUEST; | |
899 | ||
900 | /* | |
901 | * If we are getting here after a short-out-packet we don't enqueue any | |
902 | * new requests as we try to set the IOC bit only on the last request. | |
903 | */ | |
904 | if (start_new) { | |
905 | if (list_empty(&dep->req_queued)) | |
906 | dwc3_prepare_trbs(dep, start_new); | |
907 | ||
908 | /* req points to the first request which will be sent */ | |
909 | req = next_request(&dep->req_queued); | |
910 | } else { | |
911 | dwc3_prepare_trbs(dep, start_new); | |
912 | ||
913 | /* | |
914 | * req points to the first request where HWO changed from 0 to 1 | |
915 | */ | |
916 | req = next_request(&dep->req_queued); | |
917 | } | |
918 | if (!req) { | |
919 | dep->flags |= DWC3_EP_PENDING_REQUEST; | |
920 | return 0; | |
921 | } | |
922 | ||
923 | memset(¶ms, 0, sizeof(params)); | |
924 | ||
925 | if (start_new) { | |
926 | params.param0 = upper_32_bits(req->trb_dma); | |
927 | params.param1 = lower_32_bits(req->trb_dma); | |
928 | cmd = DWC3_DEPCMD_STARTTRANSFER; | |
929 | } else { | |
930 | cmd = DWC3_DEPCMD_UPDATETRANSFER; | |
931 | } | |
932 | ||
933 | cmd |= DWC3_DEPCMD_PARAM(cmd_param); | |
934 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); | |
935 | if (ret < 0) { | |
936 | dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n"); | |
937 | ||
938 | /* | |
939 | * FIXME we need to iterate over the list of requests | |
940 | * here and stop, unmap, free and del each of the linked | |
941 | * requests instead of what we do now. | |
942 | */ | |
943 | usb_gadget_unmap_request(&dwc->gadget, &req->request, | |
944 | req->direction); | |
945 | list_del(&req->list); | |
946 | return ret; | |
947 | } | |
948 | ||
949 | dep->flags |= DWC3_EP_BUSY; | |
950 | ||
951 | if (start_new) { | |
952 | dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc, | |
953 | dep->number); | |
954 | WARN_ON_ONCE(!dep->resource_index); | |
955 | } | |
956 | ||
957 | return 0; | |
958 | } | |
959 | ||
960 | static void __dwc3_gadget_start_isoc(struct dwc3 *dwc, | |
961 | struct dwc3_ep *dep, u32 cur_uf) | |
962 | { | |
963 | u32 uf; | |
964 | ||
965 | if (list_empty(&dep->request_list)) { | |
966 | dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n", | |
967 | dep->name); | |
968 | dep->flags |= DWC3_EP_PENDING_REQUEST; | |
969 | return; | |
970 | } | |
971 | ||
972 | /* 4 micro frames in the future */ | |
973 | uf = cur_uf + dep->interval * 4; | |
974 | ||
975 | __dwc3_gadget_kick_transfer(dep, uf, 1); | |
976 | } | |
977 | ||
978 | static void dwc3_gadget_start_isoc(struct dwc3 *dwc, | |
979 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) | |
980 | { | |
981 | u32 cur_uf, mask; | |
982 | ||
983 | mask = ~(dep->interval - 1); | |
984 | cur_uf = event->parameters & mask; | |
985 | ||
986 | __dwc3_gadget_start_isoc(dwc, dep, cur_uf); | |
987 | } | |
988 | ||
989 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) | |
990 | { | |
991 | struct dwc3 *dwc = dep->dwc; | |
992 | int ret; | |
993 | ||
994 | req->request.actual = 0; | |
995 | req->request.status = -EINPROGRESS; | |
996 | req->direction = dep->direction; | |
997 | req->epnum = dep->number; | |
998 | ||
5dc4538b MS |
999 | /* |
1000 | * DWC3 hangs on OUT requests smaller than maxpacket size, | |
1001 | * so HACK the request length | |
1002 | */ | |
1003 | if (dep->direction == 0 && | |
1004 | req->request.length < dep->endpoint.maxpacket) | |
1005 | req->request.length = dep->endpoint.maxpacket; | |
1006 | ||
85d5e707 KVA |
1007 | /* |
1008 | * We only add to our list of requests now and | |
1009 | * start consuming the list once we get XferNotReady | |
1010 | * IRQ. | |
1011 | * | |
1012 | * That way, we avoid doing anything that we don't need | |
1013 | * to do now and defer it until the point we receive a | |
1014 | * particular token from the Host side. | |
1015 | * | |
1016 | * This will also avoid Host cancelling URBs due to too | |
1017 | * many NAKs. | |
1018 | */ | |
1019 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, | |
1020 | dep->direction); | |
1021 | if (ret) | |
1022 | return ret; | |
1023 | ||
1024 | list_add_tail(&req->list, &dep->request_list); | |
1025 | ||
1026 | /* | |
1027 | * There are a few special cases: | |
1028 | * | |
1029 | * 1. XferNotReady with empty list of requests. We need to kick the | |
1030 | * transfer here in that situation, otherwise we will be NAKing | |
1031 | * forever. If we get XferNotReady before gadget driver has a | |
1032 | * chance to queue a request, we will ACK the IRQ but won't be | |
1033 | * able to receive the data until the next request is queued. | |
1034 | * The following code is handling exactly that. | |
1035 | * | |
1036 | */ | |
1037 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { | |
1038 | /* | |
1039 | * If xfernotready is already elapsed and it is a case | |
1040 | * of isoc transfer, then issue END TRANSFER, so that | |
1041 | * you can receive xfernotready again and can have | |
1042 | * notion of current microframe. | |
1043 | */ | |
1044 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { | |
1045 | if (list_empty(&dep->req_queued)) { | |
1046 | dwc3_stop_active_transfer(dwc, dep->number, true); | |
1047 | dep->flags = DWC3_EP_ENABLED; | |
1048 | } | |
1049 | return 0; | |
1050 | } | |
1051 | ||
1052 | ret = __dwc3_gadget_kick_transfer(dep, 0, true); | |
1053 | if (ret && ret != -EBUSY) | |
1054 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", | |
1055 | dep->name); | |
1056 | return ret; | |
1057 | } | |
1058 | ||
1059 | /* | |
1060 | * 2. XferInProgress on Isoc EP with an active transfer. We need to | |
1061 | * kick the transfer here after queuing a request, otherwise the | |
1062 | * core may not see the modified TRB(s). | |
1063 | */ | |
1064 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && | |
1065 | (dep->flags & DWC3_EP_BUSY) && | |
1066 | !(dep->flags & DWC3_EP_MISSED_ISOC)) { | |
1067 | WARN_ON_ONCE(!dep->resource_index); | |
1068 | ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index, | |
1069 | false); | |
1070 | if (ret && ret != -EBUSY) | |
1071 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", | |
1072 | dep->name); | |
1073 | return ret; | |
1074 | } | |
1075 | ||
1076 | /* | |
1077 | * 4. Stream Capable Bulk Endpoints. We need to start the transfer | |
1078 | * right away, otherwise host will not know we have streams to be | |
1079 | * handled. | |
1080 | */ | |
1081 | if (dep->stream_capable) { | |
1082 | int ret; | |
1083 | ||
1084 | ret = __dwc3_gadget_kick_transfer(dep, 0, true); | |
1085 | if (ret && ret != -EBUSY) { | |
85d5e707 KVA |
1086 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", |
1087 | dep->name); | |
1088 | } | |
1089 | } | |
1090 | ||
1091 | return 0; | |
1092 | } | |
1093 | ||
1094 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, | |
1095 | gfp_t gfp_flags) | |
1096 | { | |
1097 | struct dwc3_request *req = to_dwc3_request(request); | |
1098 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
85d5e707 KVA |
1099 | |
1100 | unsigned long flags; | |
1101 | ||
1102 | int ret; | |
1103 | ||
1104 | spin_lock_irqsave(&dwc->lock, flags); | |
1105 | if (!dep->endpoint.desc) { | |
df5eabcb SA |
1106 | dev_dbg(dep->dwc->dev, |
1107 | "trying to queue request %p to disabled %s\n", request, | |
1108 | ep->name); | |
85d5e707 KVA |
1109 | ret = -ESHUTDOWN; |
1110 | goto out; | |
1111 | } | |
1112 | ||
747a0a5b | 1113 | if (req->dep != dep) { |
df5eabcb SA |
1114 | WARN(true, "request %p belongs to '%s'\n", request, |
1115 | req->dep->name); | |
85d5e707 KVA |
1116 | ret = -EINVAL; |
1117 | goto out; | |
1118 | } | |
1119 | ||
df5eabcb SA |
1120 | dev_vdbg(dep->dwc->dev, "queing request %p to %s length %d\n", |
1121 | request, ep->name, request->length); | |
85d5e707 KVA |
1122 | |
1123 | ret = __dwc3_gadget_ep_queue(dep, req); | |
1124 | ||
1125 | out: | |
1126 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1127 | ||
1128 | return ret; | |
1129 | } | |
1130 | ||
1131 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, | |
1132 | struct usb_request *request) | |
1133 | { | |
1134 | struct dwc3_request *req = to_dwc3_request(request); | |
1135 | struct dwc3_request *r = NULL; | |
1136 | ||
1137 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
1138 | struct dwc3 *dwc = dep->dwc; | |
1139 | ||
1140 | unsigned long flags; | |
1141 | int ret = 0; | |
1142 | ||
85d5e707 KVA |
1143 | spin_lock_irqsave(&dwc->lock, flags); |
1144 | ||
1145 | list_for_each_entry(r, &dep->request_list, list) { | |
1146 | if (r == req) | |
1147 | break; | |
1148 | } | |
1149 | ||
1150 | if (r != req) { | |
1151 | list_for_each_entry(r, &dep->req_queued, list) { | |
1152 | if (r == req) | |
1153 | break; | |
1154 | } | |
1155 | if (r == req) { | |
1156 | /* wait until it is processed */ | |
1157 | dwc3_stop_active_transfer(dwc, dep->number, true); | |
1158 | goto out1; | |
1159 | } | |
1160 | dev_err(dwc->dev, "request %p was not queued to %s\n", | |
1161 | request, ep->name); | |
1162 | ret = -EINVAL; | |
1163 | goto out0; | |
1164 | } | |
1165 | ||
1166 | out1: | |
1167 | /* giveback the request */ | |
1168 | dwc3_gadget_giveback(dep, req, -ECONNRESET); | |
1169 | ||
1170 | out0: | |
1171 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1172 | ||
1173 | return ret; | |
1174 | } | |
1175 | ||
1176 | int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) | |
1177 | { | |
1178 | struct dwc3_gadget_ep_cmd_params params; | |
1179 | struct dwc3 *dwc = dep->dwc; | |
1180 | int ret; | |
1181 | ||
1182 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { | |
1183 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); | |
1184 | return -EINVAL; | |
1185 | } | |
1186 | ||
1187 | memset(¶ms, 0x00, sizeof(params)); | |
1188 | ||
1189 | if (value) { | |
1190 | if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) || | |
1191 | (!list_empty(&dep->req_queued) || | |
1192 | !list_empty(&dep->request_list)))) { | |
1193 | dev_dbg(dwc->dev, "%s: pending request, cannot halt\n", | |
1194 | dep->name); | |
1195 | return -EAGAIN; | |
1196 | } | |
1197 | ||
1198 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, | |
1199 | DWC3_DEPCMD_SETSTALL, ¶ms); | |
1200 | if (ret) | |
1201 | dev_err(dwc->dev, "failed to set STALL on %s\n", | |
1202 | dep->name); | |
1203 | else | |
1204 | dep->flags |= DWC3_EP_STALL; | |
1205 | } else { | |
1206 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, | |
1207 | DWC3_DEPCMD_CLEARSTALL, ¶ms); | |
1208 | if (ret) | |
1209 | dev_err(dwc->dev, "failed to clear STALL on %s\n", | |
1210 | dep->name); | |
1211 | else | |
1212 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); | |
1213 | } | |
1214 | ||
1215 | return ret; | |
1216 | } | |
1217 | ||
1218 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) | |
1219 | { | |
1220 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
85d5e707 KVA |
1221 | |
1222 | unsigned long flags; | |
1223 | ||
1224 | int ret; | |
1225 | ||
1226 | spin_lock_irqsave(&dwc->lock, flags); | |
1227 | ret = __dwc3_gadget_ep_set_halt(dep, value, false); | |
1228 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1229 | ||
1230 | return ret; | |
1231 | } | |
1232 | ||
1233 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) | |
1234 | { | |
1235 | struct dwc3_ep *dep = to_dwc3_ep(ep); | |
85d5e707 KVA |
1236 | unsigned long flags; |
1237 | int ret; | |
1238 | ||
1239 | spin_lock_irqsave(&dwc->lock, flags); | |
1240 | dep->flags |= DWC3_EP_WEDGE; | |
1241 | ||
1242 | if (dep->number == 0 || dep->number == 1) | |
1243 | ret = __dwc3_gadget_ep0_set_halt(ep, 1); | |
1244 | else | |
1245 | ret = __dwc3_gadget_ep_set_halt(dep, 1, false); | |
1246 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1247 | ||
1248 | return ret; | |
1249 | } | |
1250 | ||
1251 | /* -------------------------------------------------------------------------- */ | |
1252 | ||
1253 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { | |
1254 | .bLength = USB_DT_ENDPOINT_SIZE, | |
1255 | .bDescriptorType = USB_DT_ENDPOINT, | |
1256 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, | |
1257 | }; | |
1258 | ||
1259 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { | |
1260 | .enable = dwc3_gadget_ep0_enable, | |
1261 | .disable = dwc3_gadget_ep0_disable, | |
1262 | .alloc_request = dwc3_gadget_ep_alloc_request, | |
1263 | .free_request = dwc3_gadget_ep_free_request, | |
1264 | .queue = dwc3_gadget_ep0_queue, | |
1265 | .dequeue = dwc3_gadget_ep_dequeue, | |
1266 | .set_halt = dwc3_gadget_ep0_set_halt, | |
1267 | .set_wedge = dwc3_gadget_ep_set_wedge, | |
1268 | }; | |
1269 | ||
1270 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { | |
1271 | .enable = dwc3_gadget_ep_enable, | |
1272 | .disable = dwc3_gadget_ep_disable, | |
1273 | .alloc_request = dwc3_gadget_ep_alloc_request, | |
1274 | .free_request = dwc3_gadget_ep_free_request, | |
1275 | .queue = dwc3_gadget_ep_queue, | |
1276 | .dequeue = dwc3_gadget_ep_dequeue, | |
1277 | .set_halt = dwc3_gadget_ep_set_halt, | |
1278 | .set_wedge = dwc3_gadget_ep_set_wedge, | |
1279 | }; | |
1280 | ||
1281 | /* -------------------------------------------------------------------------- */ | |
1282 | ||
1283 | static int dwc3_gadget_get_frame(struct usb_gadget *g) | |
1284 | { | |
1285 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1286 | u32 reg; | |
1287 | ||
1288 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
1289 | return DWC3_DSTS_SOFFN(reg); | |
1290 | } | |
1291 | ||
1292 | static int dwc3_gadget_wakeup(struct usb_gadget *g) | |
1293 | { | |
1294 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1295 | ||
1296 | unsigned long timeout; | |
1297 | unsigned long flags; | |
1298 | ||
1299 | u32 reg; | |
1300 | ||
1301 | int ret = 0; | |
1302 | ||
1303 | u8 link_state; | |
1304 | u8 speed; | |
1305 | ||
1306 | spin_lock_irqsave(&dwc->lock, flags); | |
1307 | ||
1308 | /* | |
1309 | * According to the Databook Remote wakeup request should | |
1310 | * be issued only when the device is in early suspend state. | |
1311 | * | |
1312 | * We can check that via USB Link State bits in DSTS register. | |
1313 | */ | |
1314 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
1315 | ||
1316 | speed = reg & DWC3_DSTS_CONNECTSPD; | |
1317 | if (speed == DWC3_DSTS_SUPERSPEED) { | |
1318 | dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n"); | |
1319 | ret = -EINVAL; | |
1320 | goto out; | |
1321 | } | |
1322 | ||
1323 | link_state = DWC3_DSTS_USBLNKST(reg); | |
1324 | ||
1325 | switch (link_state) { | |
1326 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ | |
1327 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ | |
1328 | break; | |
1329 | default: | |
1330 | dev_dbg(dwc->dev, "can't wakeup from link state %d\n", | |
1331 | link_state); | |
1332 | ret = -EINVAL; | |
1333 | goto out; | |
1334 | } | |
1335 | ||
1336 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); | |
1337 | if (ret < 0) { | |
1338 | dev_err(dwc->dev, "failed to put link in Recovery\n"); | |
1339 | goto out; | |
1340 | } | |
1341 | ||
1342 | /* Recent versions do this automatically */ | |
1343 | if (dwc->revision < DWC3_REVISION_194A) { | |
1344 | /* write zeroes to Link Change Request */ | |
1345 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
1346 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; | |
1347 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
1348 | } | |
1349 | ||
1350 | /* poll until Link State changes to ON */ | |
747a0a5b | 1351 | timeout = 1000; |
85d5e707 | 1352 | |
747a0a5b | 1353 | while (timeout--) { |
85d5e707 KVA |
1354 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
1355 | ||
1356 | /* in HS, means ON */ | |
1357 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) | |
1358 | break; | |
1359 | } | |
1360 | ||
1361 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { | |
1362 | dev_err(dwc->dev, "failed to send remote wakeup\n"); | |
1363 | ret = -EINVAL; | |
1364 | } | |
1365 | ||
1366 | out: | |
1367 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1368 | ||
1369 | return ret; | |
1370 | } | |
1371 | ||
1372 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, | |
1373 | int is_selfpowered) | |
1374 | { | |
1375 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1376 | unsigned long flags; | |
1377 | ||
1378 | spin_lock_irqsave(&dwc->lock, flags); | |
1379 | dwc->is_selfpowered = !!is_selfpowered; | |
1380 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1381 | ||
1382 | return 0; | |
1383 | } | |
1384 | ||
1385 | static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) | |
1386 | { | |
1387 | u32 reg; | |
1388 | u32 timeout = 500; | |
1389 | ||
1390 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
1391 | if (is_on) { | |
1392 | if (dwc->revision <= DWC3_REVISION_187A) { | |
1393 | reg &= ~DWC3_DCTL_TRGTULST_MASK; | |
1394 | reg |= DWC3_DCTL_TRGTULST_RX_DET; | |
1395 | } | |
1396 | ||
1397 | if (dwc->revision >= DWC3_REVISION_194A) | |
1398 | reg &= ~DWC3_DCTL_KEEP_CONNECT; | |
1399 | reg |= DWC3_DCTL_RUN_STOP; | |
1400 | ||
1401 | if (dwc->has_hibernation) | |
1402 | reg |= DWC3_DCTL_KEEP_CONNECT; | |
1403 | ||
1404 | dwc->pullups_connected = true; | |
1405 | } else { | |
1406 | reg &= ~DWC3_DCTL_RUN_STOP; | |
1407 | ||
1408 | if (dwc->has_hibernation && !suspend) | |
1409 | reg &= ~DWC3_DCTL_KEEP_CONNECT; | |
1410 | ||
1411 | dwc->pullups_connected = false; | |
1412 | } | |
1413 | ||
1414 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
1415 | ||
1416 | do { | |
1417 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
1418 | if (is_on) { | |
1419 | if (!(reg & DWC3_DSTS_DEVCTRLHLT)) | |
1420 | break; | |
1421 | } else { | |
1422 | if (reg & DWC3_DSTS_DEVCTRLHLT) | |
1423 | break; | |
1424 | } | |
1425 | timeout--; | |
1426 | if (!timeout) | |
1427 | return -ETIMEDOUT; | |
1428 | udelay(1); | |
1429 | } while (1); | |
1430 | ||
1431 | dev_vdbg(dwc->dev, "gadget %s data soft-%s\n", | |
1432 | dwc->gadget_driver | |
1433 | ? dwc->gadget_driver->function : "no-function", | |
1434 | is_on ? "connect" : "disconnect"); | |
1435 | ||
1436 | return 0; | |
1437 | } | |
1438 | ||
1439 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) | |
1440 | { | |
1441 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1442 | unsigned long flags; | |
1443 | int ret; | |
1444 | ||
1445 | is_on = !!is_on; | |
1446 | ||
1447 | spin_lock_irqsave(&dwc->lock, flags); | |
1448 | ret = dwc3_gadget_run_stop(dwc, is_on, false); | |
1449 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1450 | ||
1451 | return ret; | |
1452 | } | |
1453 | ||
1454 | static void dwc3_gadget_enable_irq(struct dwc3 *dwc) | |
1455 | { | |
1456 | u32 reg; | |
1457 | ||
1458 | /* Enable all but Start and End of Frame IRQs */ | |
1459 | reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | | |
1460 | DWC3_DEVTEN_EVNTOVERFLOWEN | | |
1461 | DWC3_DEVTEN_CMDCMPLTEN | | |
1462 | DWC3_DEVTEN_ERRTICERREN | | |
1463 | DWC3_DEVTEN_WKUPEVTEN | | |
1464 | DWC3_DEVTEN_ULSTCNGEN | | |
1465 | DWC3_DEVTEN_CONNECTDONEEN | | |
1466 | DWC3_DEVTEN_USBRSTEN | | |
1467 | DWC3_DEVTEN_DISCONNEVTEN); | |
1468 | ||
1469 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); | |
1470 | } | |
1471 | ||
1472 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc) | |
1473 | { | |
1474 | /* mask all interrupts */ | |
1475 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); | |
1476 | } | |
1477 | ||
85d5e707 KVA |
1478 | static int dwc3_gadget_start(struct usb_gadget *g, |
1479 | struct usb_gadget_driver *driver) | |
1480 | { | |
1481 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1482 | struct dwc3_ep *dep; | |
1483 | unsigned long flags; | |
1484 | int ret = 0; | |
85d5e707 KVA |
1485 | u32 reg; |
1486 | ||
85d5e707 KVA |
1487 | spin_lock_irqsave(&dwc->lock, flags); |
1488 | ||
1489 | if (dwc->gadget_driver) { | |
1490 | dev_err(dwc->dev, "%s is already bound to %s\n", | |
1491 | dwc->gadget.name, | |
747a0a5b | 1492 | dwc->gadget_driver->function); |
85d5e707 KVA |
1493 | ret = -EBUSY; |
1494 | goto err1; | |
1495 | } | |
1496 | ||
1497 | dwc->gadget_driver = driver; | |
1498 | ||
1499 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
1500 | reg &= ~(DWC3_DCFG_SPEED_MASK); | |
1501 | ||
1502 | /** | |
1503 | * WORKAROUND: DWC3 revision < 2.20a have an issue | |
1504 | * which would cause metastability state on Run/Stop | |
1505 | * bit if we try to force the IP to USB2-only mode. | |
1506 | * | |
1507 | * Because of that, we cannot configure the IP to any | |
1508 | * speed other than the SuperSpeed | |
1509 | * | |
1510 | * Refers to: | |
1511 | * | |
1512 | * STAR#9000525659: Clock Domain Crossing on DCTL in | |
1513 | * USB 2.0 Mode | |
1514 | */ | |
1515 | if (dwc->revision < DWC3_REVISION_220A) { | |
1516 | reg |= DWC3_DCFG_SUPERSPEED; | |
1517 | } else { | |
1518 | switch (dwc->maximum_speed) { | |
1519 | case USB_SPEED_LOW: | |
1520 | reg |= DWC3_DSTS_LOWSPEED; | |
1521 | break; | |
1522 | case USB_SPEED_FULL: | |
1523 | reg |= DWC3_DSTS_FULLSPEED1; | |
1524 | break; | |
1525 | case USB_SPEED_HIGH: | |
1526 | reg |= DWC3_DSTS_HIGHSPEED; | |
1527 | break; | |
1528 | case USB_SPEED_SUPER: /* FALLTHROUGH */ | |
1529 | case USB_SPEED_UNKNOWN: /* FALTHROUGH */ | |
1530 | default: | |
1531 | reg |= DWC3_DSTS_SUPERSPEED; | |
1532 | } | |
1533 | } | |
1534 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
1535 | ||
1536 | dwc->start_config_issued = false; | |
1537 | ||
1538 | /* Start with SuperSpeed Default */ | |
1539 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); | |
1540 | ||
1541 | dep = dwc->eps[0]; | |
1542 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, | |
1543 | false); | |
1544 | if (ret) { | |
1545 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
1546 | goto err2; | |
1547 | } | |
1548 | ||
1549 | dep = dwc->eps[1]; | |
1550 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, | |
1551 | false); | |
1552 | if (ret) { | |
1553 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
1554 | goto err3; | |
1555 | } | |
1556 | ||
1557 | /* begin to receive SETUP packets */ | |
1558 | dwc->ep0state = EP0_SETUP_PHASE; | |
1559 | dwc3_ep0_out_start(dwc); | |
1560 | ||
1561 | dwc3_gadget_enable_irq(dwc); | |
1562 | ||
1563 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1564 | ||
1565 | return 0; | |
1566 | ||
1567 | err3: | |
1568 | __dwc3_gadget_ep_disable(dwc->eps[0]); | |
1569 | ||
1570 | err2: | |
1571 | dwc->gadget_driver = NULL; | |
1572 | ||
1573 | err1: | |
1574 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1575 | ||
85d5e707 KVA |
1576 | return ret; |
1577 | } | |
1578 | ||
1579 | static int dwc3_gadget_stop(struct usb_gadget *g) | |
1580 | { | |
1581 | struct dwc3 *dwc = gadget_to_dwc(g); | |
1582 | unsigned long flags; | |
85d5e707 KVA |
1583 | |
1584 | spin_lock_irqsave(&dwc->lock, flags); | |
1585 | ||
1586 | dwc3_gadget_disable_irq(dwc); | |
1587 | __dwc3_gadget_ep_disable(dwc->eps[0]); | |
1588 | __dwc3_gadget_ep_disable(dwc->eps[1]); | |
1589 | ||
1590 | dwc->gadget_driver = NULL; | |
1591 | ||
1592 | spin_unlock_irqrestore(&dwc->lock, flags); | |
1593 | ||
85d5e707 KVA |
1594 | return 0; |
1595 | } | |
1596 | ||
1597 | static const struct usb_gadget_ops dwc3_gadget_ops = { | |
1598 | .get_frame = dwc3_gadget_get_frame, | |
1599 | .wakeup = dwc3_gadget_wakeup, | |
1600 | .set_selfpowered = dwc3_gadget_set_selfpowered, | |
1601 | .pullup = dwc3_gadget_pullup, | |
1602 | .udc_start = dwc3_gadget_start, | |
1603 | .udc_stop = dwc3_gadget_stop, | |
1604 | }; | |
1605 | ||
1606 | /* -------------------------------------------------------------------------- */ | |
1607 | ||
1608 | static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc, | |
1609 | u8 num, u32 direction) | |
1610 | { | |
1611 | struct dwc3_ep *dep; | |
1612 | u8 i; | |
1613 | ||
1614 | for (i = 0; i < num; i++) { | |
1615 | u8 epnum = (i << 1) | (!!direction); | |
1616 | ||
1617 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); | |
1618 | if (!dep) | |
1619 | return -ENOMEM; | |
1620 | ||
1621 | dep->dwc = dwc; | |
1622 | dep->number = epnum; | |
1623 | dep->direction = !!direction; | |
1624 | dwc->eps[epnum] = dep; | |
1625 | ||
1626 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1, | |
1627 | (epnum & 1) ? "in" : "out"); | |
1628 | ||
1629 | dep->endpoint.name = dep->name; | |
1630 | ||
1631 | dev_vdbg(dwc->dev, "initializing %s\n", dep->name); | |
1632 | ||
1633 | if (epnum == 0 || epnum == 1) { | |
1634 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); | |
1635 | dep->endpoint.maxburst = 1; | |
1636 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; | |
1637 | if (!epnum) | |
1638 | dwc->gadget.ep0 = &dep->endpoint; | |
1639 | } else { | |
1640 | int ret; | |
1641 | ||
afa093bf | 1642 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
85d5e707 KVA |
1643 | dep->endpoint.max_streams = 15; |
1644 | dep->endpoint.ops = &dwc3_gadget_ep_ops; | |
1645 | list_add_tail(&dep->endpoint.ep_list, | |
1646 | &dwc->gadget.ep_list); | |
1647 | ||
1648 | ret = dwc3_alloc_trb_pool(dep); | |
1649 | if (ret) | |
1650 | return ret; | |
1651 | } | |
1652 | ||
1653 | INIT_LIST_HEAD(&dep->request_list); | |
1654 | INIT_LIST_HEAD(&dep->req_queued); | |
1655 | } | |
1656 | ||
1657 | return 0; | |
1658 | } | |
1659 | ||
1660 | static int dwc3_gadget_init_endpoints(struct dwc3 *dwc) | |
1661 | { | |
1662 | int ret; | |
1663 | ||
1664 | INIT_LIST_HEAD(&dwc->gadget.ep_list); | |
1665 | ||
1666 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0); | |
1667 | if (ret < 0) { | |
1668 | dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n"); | |
1669 | return ret; | |
1670 | } | |
1671 | ||
1672 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1); | |
1673 | if (ret < 0) { | |
1674 | dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n"); | |
1675 | return ret; | |
1676 | } | |
1677 | ||
1678 | return 0; | |
1679 | } | |
1680 | ||
1681 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) | |
1682 | { | |
1683 | struct dwc3_ep *dep; | |
1684 | u8 epnum; | |
1685 | ||
1686 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { | |
1687 | dep = dwc->eps[epnum]; | |
1688 | if (!dep) | |
1689 | continue; | |
1690 | /* | |
1691 | * Physical endpoints 0 and 1 are special; they form the | |
1692 | * bi-directional USB endpoint 0. | |
1693 | * | |
1694 | * For those two physical endpoints, we don't allocate a TRB | |
1695 | * pool nor do we add them the endpoints list. Due to that, we | |
1696 | * shouldn't do these two operations otherwise we would end up | |
1697 | * with all sorts of bugs when removing dwc3.ko. | |
1698 | */ | |
1699 | if (epnum != 0 && epnum != 1) { | |
1700 | dwc3_free_trb_pool(dep); | |
1701 | list_del(&dep->endpoint.ep_list); | |
1702 | } | |
1703 | ||
1704 | kfree(dep); | |
1705 | } | |
1706 | } | |
1707 | ||
1708 | /* -------------------------------------------------------------------------- */ | |
1709 | ||
1710 | static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep, | |
1711 | struct dwc3_request *req, struct dwc3_trb *trb, | |
1712 | const struct dwc3_event_depevt *event, int status) | |
1713 | { | |
1714 | unsigned int count; | |
1715 | unsigned int s_pkt = 0; | |
1716 | unsigned int trb_status; | |
1717 | ||
85d5e707 KVA |
1718 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
1719 | /* | |
1720 | * We continue despite the error. There is not much we | |
1721 | * can do. If we don't clean it up we loop forever. If | |
1722 | * we skip the TRB then it gets overwritten after a | |
1723 | * while since we use them in a ring buffer. A BUG() | |
1724 | * would help. Lets hope that if this occurs, someone | |
1725 | * fixes the root cause instead of looking away :) | |
1726 | */ | |
1727 | dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n", | |
1728 | dep->name, trb); | |
1729 | count = trb->size & DWC3_TRB_SIZE_MASK; | |
1730 | ||
1731 | if (dep->direction) { | |
1732 | if (count) { | |
1733 | trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size); | |
1734 | if (trb_status == DWC3_TRBSTS_MISSED_ISOC) { | |
1735 | dev_dbg(dwc->dev, "incomplete IN transfer %s\n", | |
1736 | dep->name); | |
1737 | /* | |
1738 | * If missed isoc occurred and there is | |
1739 | * no request queued then issue END | |
1740 | * TRANSFER, so that core generates | |
1741 | * next xfernotready and we will issue | |
1742 | * a fresh START TRANSFER. | |
1743 | * If there are still queued request | |
1744 | * then wait, do not issue either END | |
1745 | * or UPDATE TRANSFER, just attach next | |
1746 | * request in request_list during | |
1747 | * giveback.If any future queued request | |
1748 | * is successfully transferred then we | |
1749 | * will issue UPDATE TRANSFER for all | |
1750 | * request in the request_list. | |
1751 | */ | |
1752 | dep->flags |= DWC3_EP_MISSED_ISOC; | |
1753 | } else { | |
1754 | dev_err(dwc->dev, "incomplete IN transfer %s\n", | |
1755 | dep->name); | |
1756 | status = -ECONNRESET; | |
1757 | } | |
1758 | } else { | |
1759 | dep->flags &= ~DWC3_EP_MISSED_ISOC; | |
1760 | } | |
1761 | } else { | |
1762 | if (count && (event->status & DEPEVT_STATUS_SHORT)) | |
1763 | s_pkt = 1; | |
1764 | } | |
1765 | ||
1766 | /* | |
1767 | * We assume here we will always receive the entire data block | |
1768 | * which we should receive. Meaning, if we program RX to | |
1769 | * receive 4K but we receive only 2K, we assume that's all we | |
1770 | * should receive and we simply bounce the request back to the | |
1771 | * gadget driver for further processing. | |
1772 | */ | |
1773 | req->request.actual += req->request.length - count; | |
1774 | if (s_pkt) | |
1775 | return 1; | |
1776 | if ((event->status & DEPEVT_STATUS_LST) && | |
1777 | (trb->ctrl & (DWC3_TRB_CTRL_LST | | |
1778 | DWC3_TRB_CTRL_HWO))) | |
1779 | return 1; | |
1780 | if ((event->status & DEPEVT_STATUS_IOC) && | |
1781 | (trb->ctrl & DWC3_TRB_CTRL_IOC)) | |
1782 | return 1; | |
1783 | return 0; | |
1784 | } | |
1785 | ||
1786 | static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, | |
1787 | const struct dwc3_event_depevt *event, int status) | |
1788 | { | |
1789 | struct dwc3_request *req; | |
1790 | struct dwc3_trb *trb; | |
1791 | unsigned int slot; | |
747a0a5b | 1792 | |
3621b3b8 ŁM |
1793 | req = next_request(&dep->req_queued); |
1794 | if (!req) { | |
1795 | WARN_ON_ONCE(1); | |
1796 | return 1; | |
1797 | } | |
85d5e707 | 1798 | |
3621b3b8 ŁM |
1799 | slot = req->start_slot; |
1800 | if ((slot == DWC3_TRB_NUM - 1) && | |
1801 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) | |
1802 | slot++; | |
1803 | slot %= DWC3_TRB_NUM; | |
1804 | trb = &dep->trb_pool[slot]; | |
85d5e707 | 1805 | |
b7bf4a95 | 1806 | dwc3_flush_cache((uintptr_t)trb, sizeof(*trb)); |
3621b3b8 ŁM |
1807 | __dwc3_cleanup_done_trbs(dwc, dep, req, trb, event, status); |
1808 | dwc3_gadget_giveback(dep, req, status); | |
85d5e707 KVA |
1809 | |
1810 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && | |
1811 | list_empty(&dep->req_queued)) { | |
1812 | if (list_empty(&dep->request_list)) { | |
1813 | /* | |
1814 | * If there is no entry in request list then do | |
1815 | * not issue END TRANSFER now. Just set PENDING | |
1816 | * flag, so that END TRANSFER is issued when an | |
1817 | * entry is added into request list. | |
1818 | */ | |
1819 | dep->flags = DWC3_EP_PENDING_REQUEST; | |
1820 | } else { | |
1821 | dwc3_stop_active_transfer(dwc, dep->number, true); | |
1822 | dep->flags = DWC3_EP_ENABLED; | |
1823 | } | |
1824 | return 1; | |
1825 | } | |
1826 | ||
1827 | return 1; | |
1828 | } | |
1829 | ||
1830 | static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc, | |
1831 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) | |
1832 | { | |
1833 | unsigned status = 0; | |
1834 | int clean_busy; | |
1835 | ||
1836 | if (event->status & DEPEVT_STATUS_BUSERR) | |
1837 | status = -ECONNRESET; | |
1838 | ||
1839 | clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status); | |
1840 | if (clean_busy) | |
1841 | dep->flags &= ~DWC3_EP_BUSY; | |
1842 | ||
1843 | /* | |
1844 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. | |
1845 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. | |
1846 | */ | |
1847 | if (dwc->revision < DWC3_REVISION_183A) { | |
1848 | u32 reg; | |
1849 | int i; | |
1850 | ||
1851 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { | |
1852 | dep = dwc->eps[i]; | |
1853 | ||
1854 | if (!(dep->flags & DWC3_EP_ENABLED)) | |
1855 | continue; | |
1856 | ||
1857 | if (!list_empty(&dep->req_queued)) | |
1858 | return; | |
1859 | } | |
1860 | ||
1861 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
1862 | reg |= dwc->u1u2; | |
1863 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
1864 | ||
1865 | dwc->u1u2 = 0; | |
1866 | } | |
1867 | } | |
1868 | ||
1869 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, | |
1870 | const struct dwc3_event_depevt *event) | |
1871 | { | |
1872 | struct dwc3_ep *dep; | |
1873 | u8 epnum = event->endpoint_number; | |
1874 | ||
1875 | dep = dwc->eps[epnum]; | |
1876 | ||
1877 | if (!(dep->flags & DWC3_EP_ENABLED)) | |
1878 | return; | |
1879 | ||
1880 | if (epnum == 0 || epnum == 1) { | |
1881 | dwc3_ep0_interrupt(dwc, event); | |
1882 | return; | |
1883 | } | |
1884 | ||
1885 | switch (event->endpoint_event) { | |
1886 | case DWC3_DEPEVT_XFERCOMPLETE: | |
1887 | dep->resource_index = 0; | |
1888 | ||
1889 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { | |
1890 | dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n", | |
1891 | dep->name); | |
1892 | return; | |
1893 | } | |
1894 | ||
1895 | dwc3_endpoint_transfer_complete(dwc, dep, event); | |
1896 | break; | |
1897 | case DWC3_DEPEVT_XFERINPROGRESS: | |
1898 | dwc3_endpoint_transfer_complete(dwc, dep, event); | |
1899 | break; | |
1900 | case DWC3_DEPEVT_XFERNOTREADY: | |
1901 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { | |
1902 | dwc3_gadget_start_isoc(dwc, dep, event); | |
1903 | } else { | |
1904 | int ret; | |
1905 | ||
1906 | dev_vdbg(dwc->dev, "%s: reason %s\n", | |
1907 | dep->name, event->status & | |
1908 | DEPEVT_STATUS_TRANSFER_ACTIVE | |
1909 | ? "Transfer Active" | |
1910 | : "Transfer Not Active"); | |
1911 | ||
1912 | ret = __dwc3_gadget_kick_transfer(dep, 0, 1); | |
1913 | if (!ret || ret == -EBUSY) | |
1914 | return; | |
1915 | ||
1916 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", | |
1917 | dep->name); | |
1918 | } | |
1919 | ||
1920 | break; | |
1921 | case DWC3_DEPEVT_STREAMEVT: | |
1922 | if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) { | |
1923 | dev_err(dwc->dev, "Stream event for non-Bulk %s\n", | |
1924 | dep->name); | |
1925 | return; | |
1926 | } | |
1927 | ||
1928 | switch (event->status) { | |
1929 | case DEPEVT_STREAMEVT_FOUND: | |
1930 | dev_vdbg(dwc->dev, "Stream %d found and started\n", | |
1931 | event->parameters); | |
1932 | ||
1933 | break; | |
1934 | case DEPEVT_STREAMEVT_NOTFOUND: | |
1935 | /* FALLTHROUGH */ | |
1936 | default: | |
1937 | dev_dbg(dwc->dev, "Couldn't find suitable stream\n"); | |
1938 | } | |
1939 | break; | |
1940 | case DWC3_DEPEVT_RXTXFIFOEVT: | |
1941 | dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name); | |
1942 | break; | |
1943 | case DWC3_DEPEVT_EPCMDCMPLT: | |
1944 | dev_vdbg(dwc->dev, "Endpoint Command Complete\n"); | |
1945 | break; | |
1946 | } | |
1947 | } | |
1948 | ||
1949 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) | |
1950 | { | |
1951 | if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { | |
1952 | spin_unlock(&dwc->lock); | |
1953 | dwc->gadget_driver->disconnect(&dwc->gadget); | |
1954 | spin_lock(&dwc->lock); | |
1955 | } | |
1956 | } | |
1957 | ||
1958 | static void dwc3_suspend_gadget(struct dwc3 *dwc) | |
1959 | { | |
1960 | if (dwc->gadget_driver && dwc->gadget_driver->suspend) { | |
1961 | spin_unlock(&dwc->lock); | |
1962 | dwc->gadget_driver->suspend(&dwc->gadget); | |
1963 | spin_lock(&dwc->lock); | |
1964 | } | |
1965 | } | |
1966 | ||
1967 | static void dwc3_resume_gadget(struct dwc3 *dwc) | |
1968 | { | |
1969 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { | |
1970 | spin_unlock(&dwc->lock); | |
1971 | dwc->gadget_driver->resume(&dwc->gadget); | |
1972 | } | |
1973 | } | |
1974 | ||
1975 | static void dwc3_reset_gadget(struct dwc3 *dwc) | |
1976 | { | |
1977 | if (!dwc->gadget_driver) | |
1978 | return; | |
1979 | ||
1980 | if (dwc->gadget.speed != USB_SPEED_UNKNOWN) { | |
1981 | spin_unlock(&dwc->lock); | |
1982 | usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver); | |
1983 | spin_lock(&dwc->lock); | |
1984 | } | |
1985 | } | |
1986 | ||
1987 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force) | |
1988 | { | |
1989 | struct dwc3_ep *dep; | |
1990 | struct dwc3_gadget_ep_cmd_params params; | |
1991 | u32 cmd; | |
1992 | int ret; | |
1993 | ||
1994 | dep = dwc->eps[epnum]; | |
1995 | ||
1996 | if (!dep->resource_index) | |
1997 | return; | |
1998 | ||
1999 | /* | |
2000 | * NOTICE: We are violating what the Databook says about the | |
2001 | * EndTransfer command. Ideally we would _always_ wait for the | |
2002 | * EndTransfer Command Completion IRQ, but that's causing too | |
2003 | * much trouble synchronizing between us and gadget driver. | |
2004 | * | |
2005 | * We have discussed this with the IP Provider and it was | |
2006 | * suggested to giveback all requests here, but give HW some | |
2007 | * extra time to synchronize with the interconnect. We're using | |
2008 | * an arbitraty 100us delay for that. | |
2009 | * | |
2010 | * Note also that a similar handling was tested by Synopsys | |
2011 | * (thanks a lot Paul) and nothing bad has come out of it. | |
2012 | * In short, what we're doing is: | |
2013 | * | |
2014 | * - Issue EndTransfer WITH CMDIOC bit set | |
2015 | * - Wait 100us | |
2016 | */ | |
2017 | ||
2018 | cmd = DWC3_DEPCMD_ENDTRANSFER; | |
2019 | cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; | |
2020 | cmd |= DWC3_DEPCMD_CMDIOC; | |
2021 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); | |
2022 | memset(¶ms, 0, sizeof(params)); | |
2023 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); | |
2024 | WARN_ON_ONCE(ret); | |
2025 | dep->resource_index = 0; | |
2026 | dep->flags &= ~DWC3_EP_BUSY; | |
2027 | udelay(100); | |
2028 | } | |
2029 | ||
2030 | static void dwc3_stop_active_transfers(struct dwc3 *dwc) | |
2031 | { | |
2032 | u32 epnum; | |
2033 | ||
2034 | for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) { | |
2035 | struct dwc3_ep *dep; | |
2036 | ||
2037 | dep = dwc->eps[epnum]; | |
2038 | if (!dep) | |
2039 | continue; | |
2040 | ||
2041 | if (!(dep->flags & DWC3_EP_ENABLED)) | |
2042 | continue; | |
2043 | ||
2044 | dwc3_remove_requests(dwc, dep); | |
2045 | } | |
2046 | } | |
2047 | ||
2048 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) | |
2049 | { | |
2050 | u32 epnum; | |
2051 | ||
2052 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { | |
2053 | struct dwc3_ep *dep; | |
2054 | struct dwc3_gadget_ep_cmd_params params; | |
2055 | int ret; | |
2056 | ||
2057 | dep = dwc->eps[epnum]; | |
2058 | if (!dep) | |
2059 | continue; | |
2060 | ||
2061 | if (!(dep->flags & DWC3_EP_STALL)) | |
2062 | continue; | |
2063 | ||
2064 | dep->flags &= ~DWC3_EP_STALL; | |
2065 | ||
2066 | memset(¶ms, 0, sizeof(params)); | |
2067 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, | |
2068 | DWC3_DEPCMD_CLEARSTALL, ¶ms); | |
2069 | WARN_ON_ONCE(ret); | |
2070 | } | |
2071 | } | |
2072 | ||
2073 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) | |
2074 | { | |
2075 | int reg; | |
2076 | ||
2077 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2078 | reg &= ~DWC3_DCTL_INITU1ENA; | |
2079 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2080 | ||
2081 | reg &= ~DWC3_DCTL_INITU2ENA; | |
2082 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2083 | ||
2084 | dwc3_disconnect_gadget(dwc); | |
2085 | dwc->start_config_issued = false; | |
2086 | ||
2087 | dwc->gadget.speed = USB_SPEED_UNKNOWN; | |
2088 | dwc->setup_packet_pending = false; | |
2089 | usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED); | |
2090 | } | |
2091 | ||
2092 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) | |
2093 | { | |
2094 | u32 reg; | |
2095 | ||
2096 | /* | |
2097 | * WORKAROUND: DWC3 revisions <1.88a have an issue which | |
2098 | * would cause a missing Disconnect Event if there's a | |
2099 | * pending Setup Packet in the FIFO. | |
2100 | * | |
2101 | * There's no suggested workaround on the official Bug | |
2102 | * report, which states that "unless the driver/application | |
2103 | * is doing any special handling of a disconnect event, | |
2104 | * there is no functional issue". | |
2105 | * | |
2106 | * Unfortunately, it turns out that we _do_ some special | |
2107 | * handling of a disconnect event, namely complete all | |
2108 | * pending transfers, notify gadget driver of the | |
2109 | * disconnection, and so on. | |
2110 | * | |
2111 | * Our suggested workaround is to follow the Disconnect | |
2112 | * Event steps here, instead, based on a setup_packet_pending | |
2113 | * flag. Such flag gets set whenever we have a XferNotReady | |
2114 | * event on EP0 and gets cleared on XferComplete for the | |
2115 | * same endpoint. | |
2116 | * | |
2117 | * Refers to: | |
2118 | * | |
2119 | * STAR#9000466709: RTL: Device : Disconnect event not | |
2120 | * generated if setup packet pending in FIFO | |
2121 | */ | |
2122 | if (dwc->revision < DWC3_REVISION_188A) { | |
2123 | if (dwc->setup_packet_pending) | |
2124 | dwc3_gadget_disconnect_interrupt(dwc); | |
2125 | } | |
2126 | ||
2127 | dwc3_reset_gadget(dwc); | |
2128 | ||
2129 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2130 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; | |
2131 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2132 | dwc->test_mode = false; | |
2133 | ||
2134 | dwc3_stop_active_transfers(dwc); | |
2135 | dwc3_clear_stall_all_ep(dwc); | |
2136 | dwc->start_config_issued = false; | |
2137 | ||
2138 | /* Reset device address to zero */ | |
2139 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
2140 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); | |
2141 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
2142 | } | |
2143 | ||
2144 | static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed) | |
2145 | { | |
2146 | u32 reg; | |
2147 | u32 usb30_clock = DWC3_GCTL_CLK_BUS; | |
2148 | ||
2149 | /* | |
2150 | * We change the clock only at SS but I dunno why I would want to do | |
2151 | * this. Maybe it becomes part of the power saving plan. | |
2152 | */ | |
2153 | ||
2154 | if (speed != DWC3_DSTS_SUPERSPEED) | |
2155 | return; | |
2156 | ||
2157 | /* | |
2158 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed | |
2159 | * each time on Connect Done. | |
2160 | */ | |
2161 | if (!usb30_clock) | |
2162 | return; | |
2163 | ||
2164 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); | |
2165 | reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock); | |
2166 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
2167 | } | |
2168 | ||
2169 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) | |
2170 | { | |
2171 | struct dwc3_ep *dep; | |
2172 | int ret; | |
2173 | u32 reg; | |
2174 | u8 speed; | |
2175 | ||
2176 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
2177 | speed = reg & DWC3_DSTS_CONNECTSPD; | |
2178 | dwc->speed = speed; | |
2179 | ||
2180 | dwc3_update_ram_clk_sel(dwc, speed); | |
2181 | ||
2182 | switch (speed) { | |
2183 | case DWC3_DCFG_SUPERSPEED: | |
2184 | /* | |
2185 | * WORKAROUND: DWC3 revisions <1.90a have an issue which | |
2186 | * would cause a missing USB3 Reset event. | |
2187 | * | |
2188 | * In such situations, we should force a USB3 Reset | |
2189 | * event by calling our dwc3_gadget_reset_interrupt() | |
2190 | * routine. | |
2191 | * | |
2192 | * Refers to: | |
2193 | * | |
2194 | * STAR#9000483510: RTL: SS : USB3 reset event may | |
2195 | * not be generated always when the link enters poll | |
2196 | */ | |
2197 | if (dwc->revision < DWC3_REVISION_190A) | |
2198 | dwc3_gadget_reset_interrupt(dwc); | |
2199 | ||
2200 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); | |
2201 | dwc->gadget.ep0->maxpacket = 512; | |
2202 | dwc->gadget.speed = USB_SPEED_SUPER; | |
2203 | break; | |
2204 | case DWC3_DCFG_HIGHSPEED: | |
2205 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); | |
2206 | dwc->gadget.ep0->maxpacket = 64; | |
2207 | dwc->gadget.speed = USB_SPEED_HIGH; | |
2208 | break; | |
2209 | case DWC3_DCFG_FULLSPEED2: | |
2210 | case DWC3_DCFG_FULLSPEED1: | |
2211 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); | |
2212 | dwc->gadget.ep0->maxpacket = 64; | |
2213 | dwc->gadget.speed = USB_SPEED_FULL; | |
2214 | break; | |
2215 | case DWC3_DCFG_LOWSPEED: | |
2216 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); | |
2217 | dwc->gadget.ep0->maxpacket = 8; | |
2218 | dwc->gadget.speed = USB_SPEED_LOW; | |
2219 | break; | |
2220 | } | |
2221 | ||
2222 | /* Enable USB2 LPM Capability */ | |
2223 | ||
2224 | if ((dwc->revision > DWC3_REVISION_194A) | |
2225 | && (speed != DWC3_DCFG_SUPERSPEED)) { | |
2226 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); | |
2227 | reg |= DWC3_DCFG_LPM_CAP; | |
2228 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); | |
2229 | ||
2230 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2231 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); | |
2232 | ||
2233 | reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold); | |
2234 | ||
2235 | /* | |
2236 | * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and | |
2237 | * DCFG.LPMCap is set, core responses with an ACK and the | |
2238 | * BESL value in the LPM token is less than or equal to LPM | |
2239 | * NYET threshold. | |
2240 | */ | |
0cf207ec | 2241 | if (dwc->revision < DWC3_REVISION_240A && dwc->has_lpm_erratum) |
747a0a5b | 2242 | WARN(true, "LPM Erratum not available on dwc3 revisisions < 2.40a\n"); |
85d5e707 KVA |
2243 | |
2244 | if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A) | |
2245 | reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold); | |
2246 | ||
2247 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2248 | } else { | |
2249 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2250 | reg &= ~DWC3_DCTL_HIRD_THRES_MASK; | |
2251 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2252 | } | |
2253 | ||
2254 | dep = dwc->eps[0]; | |
2255 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, | |
2256 | false); | |
2257 | if (ret) { | |
2258 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
2259 | return; | |
2260 | } | |
2261 | ||
2262 | dep = dwc->eps[1]; | |
2263 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, | |
2264 | false); | |
2265 | if (ret) { | |
2266 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); | |
2267 | return; | |
2268 | } | |
2269 | ||
2270 | /* | |
2271 | * Configure PHY via GUSB3PIPECTLn if required. | |
2272 | * | |
2273 | * Update GTXFIFOSIZn | |
2274 | * | |
2275 | * In both cases reset values should be sufficient. | |
2276 | */ | |
2277 | } | |
2278 | ||
2279 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) | |
2280 | { | |
2281 | /* | |
2282 | * TODO take core out of low power mode when that's | |
2283 | * implemented. | |
2284 | */ | |
2285 | ||
2286 | dwc->gadget_driver->resume(&dwc->gadget); | |
2287 | } | |
2288 | ||
2289 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, | |
2290 | unsigned int evtinfo) | |
2291 | { | |
2292 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; | |
2293 | unsigned int pwropt; | |
2294 | ||
2295 | /* | |
2296 | * WORKAROUND: DWC3 < 2.50a have an issue when configured without | |
2297 | * Hibernation mode enabled which would show up when device detects | |
2298 | * host-initiated U3 exit. | |
2299 | * | |
2300 | * In that case, device will generate a Link State Change Interrupt | |
2301 | * from U3 to RESUME which is only necessary if Hibernation is | |
2302 | * configured in. | |
2303 | * | |
2304 | * There are no functional changes due to such spurious event and we | |
2305 | * just need to ignore it. | |
2306 | * | |
2307 | * Refers to: | |
2308 | * | |
2309 | * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation | |
2310 | * operational mode | |
2311 | */ | |
2312 | pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); | |
2313 | if ((dwc->revision < DWC3_REVISION_250A) && | |
2314 | (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { | |
2315 | if ((dwc->link_state == DWC3_LINK_STATE_U3) && | |
2316 | (next == DWC3_LINK_STATE_RESUME)) { | |
2317 | dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n"); | |
2318 | return; | |
2319 | } | |
2320 | } | |
2321 | ||
2322 | /* | |
2323 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending | |
2324 | * on the link partner, the USB session might do multiple entry/exit | |
2325 | * of low power states before a transfer takes place. | |
2326 | * | |
2327 | * Due to this problem, we might experience lower throughput. The | |
2328 | * suggested workaround is to disable DCTL[12:9] bits if we're | |
2329 | * transitioning from U1/U2 to U0 and enable those bits again | |
2330 | * after a transfer completes and there are no pending transfers | |
2331 | * on any of the enabled endpoints. | |
2332 | * | |
2333 | * This is the first half of that workaround. | |
2334 | * | |
2335 | * Refers to: | |
2336 | * | |
2337 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us | |
2338 | * core send LGO_Ux entering U0 | |
2339 | */ | |
2340 | if (dwc->revision < DWC3_REVISION_183A) { | |
2341 | if (next == DWC3_LINK_STATE_U0) { | |
2342 | u32 u1u2; | |
2343 | u32 reg; | |
2344 | ||
2345 | switch (dwc->link_state) { | |
2346 | case DWC3_LINK_STATE_U1: | |
2347 | case DWC3_LINK_STATE_U2: | |
2348 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
2349 | u1u2 = reg & (DWC3_DCTL_INITU2ENA | |
2350 | | DWC3_DCTL_ACCEPTU2ENA | |
2351 | | DWC3_DCTL_INITU1ENA | |
2352 | | DWC3_DCTL_ACCEPTU1ENA); | |
2353 | ||
2354 | if (!dwc->u1u2) | |
2355 | dwc->u1u2 = reg & u1u2; | |
2356 | ||
2357 | reg &= ~u1u2; | |
2358 | ||
2359 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); | |
2360 | break; | |
2361 | default: | |
2362 | /* do nothing */ | |
2363 | break; | |
2364 | } | |
2365 | } | |
2366 | } | |
2367 | ||
2368 | switch (next) { | |
2369 | case DWC3_LINK_STATE_U1: | |
2370 | if (dwc->speed == USB_SPEED_SUPER) | |
2371 | dwc3_suspend_gadget(dwc); | |
2372 | break; | |
2373 | case DWC3_LINK_STATE_U2: | |
2374 | case DWC3_LINK_STATE_U3: | |
2375 | dwc3_suspend_gadget(dwc); | |
2376 | break; | |
2377 | case DWC3_LINK_STATE_RESUME: | |
2378 | dwc3_resume_gadget(dwc); | |
2379 | break; | |
2380 | default: | |
2381 | /* do nothing */ | |
2382 | break; | |
2383 | } | |
2384 | ||
2385 | dwc->link_state = next; | |
2386 | } | |
2387 | ||
2388 | static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, | |
2389 | unsigned int evtinfo) | |
2390 | { | |
2252d150 | 2391 | unsigned int is_ss = evtinfo & (1UL << 4); |
85d5e707 KVA |
2392 | |
2393 | /** | |
2394 | * WORKAROUND: DWC3 revison 2.20a with hibernation support | |
2395 | * have a known issue which can cause USB CV TD.9.23 to fail | |
2396 | * randomly. | |
2397 | * | |
2398 | * Because of this issue, core could generate bogus hibernation | |
2399 | * events which SW needs to ignore. | |
2400 | * | |
2401 | * Refers to: | |
2402 | * | |
2403 | * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 | |
2404 | * Device Fallback from SuperSpeed | |
2405 | */ | |
2406 | if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) | |
2407 | return; | |
2408 | ||
2409 | /* enter hibernation here */ | |
2410 | } | |
2411 | ||
2412 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, | |
2413 | const struct dwc3_event_devt *event) | |
2414 | { | |
2415 | switch (event->type) { | |
2416 | case DWC3_DEVICE_EVENT_DISCONNECT: | |
2417 | dwc3_gadget_disconnect_interrupt(dwc); | |
2418 | break; | |
2419 | case DWC3_DEVICE_EVENT_RESET: | |
2420 | dwc3_gadget_reset_interrupt(dwc); | |
2421 | break; | |
2422 | case DWC3_DEVICE_EVENT_CONNECT_DONE: | |
2423 | dwc3_gadget_conndone_interrupt(dwc); | |
2424 | break; | |
2425 | case DWC3_DEVICE_EVENT_WAKEUP: | |
2426 | dwc3_gadget_wakeup_interrupt(dwc); | |
2427 | break; | |
2428 | case DWC3_DEVICE_EVENT_HIBER_REQ: | |
747a0a5b KVA |
2429 | if (!dwc->has_hibernation) { |
2430 | WARN(1 ,"unexpected hibernation event\n"); | |
85d5e707 | 2431 | break; |
747a0a5b | 2432 | } |
85d5e707 KVA |
2433 | dwc3_gadget_hibernation_interrupt(dwc, event->event_info); |
2434 | break; | |
2435 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: | |
2436 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); | |
2437 | break; | |
2438 | case DWC3_DEVICE_EVENT_EOPF: | |
2439 | dev_vdbg(dwc->dev, "End of Periodic Frame\n"); | |
2440 | break; | |
2441 | case DWC3_DEVICE_EVENT_SOF: | |
2442 | dev_vdbg(dwc->dev, "Start of Periodic Frame\n"); | |
2443 | break; | |
2444 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: | |
2445 | dev_vdbg(dwc->dev, "Erratic Error\n"); | |
2446 | break; | |
2447 | case DWC3_DEVICE_EVENT_CMD_CMPL: | |
2448 | dev_vdbg(dwc->dev, "Command Complete\n"); | |
2449 | break; | |
2450 | case DWC3_DEVICE_EVENT_OVERFLOW: | |
2451 | dev_vdbg(dwc->dev, "Overflow\n"); | |
2452 | break; | |
2453 | default: | |
2454 | dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type); | |
2455 | } | |
2456 | } | |
2457 | ||
2458 | static void dwc3_process_event_entry(struct dwc3 *dwc, | |
2459 | const union dwc3_event *event) | |
2460 | { | |
85d5e707 KVA |
2461 | /* Endpoint IRQ, handle it and return early */ |
2462 | if (event->type.is_devspec == 0) { | |
2463 | /* depevt */ | |
2464 | return dwc3_endpoint_interrupt(dwc, &event->depevt); | |
2465 | } | |
2466 | ||
2467 | switch (event->type.type) { | |
2468 | case DWC3_EVENT_TYPE_DEV: | |
2469 | dwc3_gadget_interrupt(dwc, &event->devt); | |
2470 | break; | |
2471 | /* REVISIT what to do with Carkit and I2C events ? */ | |
2472 | default: | |
2473 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); | |
2474 | } | |
2475 | } | |
2476 | ||
2477 | static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf) | |
2478 | { | |
2479 | struct dwc3_event_buffer *evt; | |
2480 | irqreturn_t ret = IRQ_NONE; | |
2481 | int left; | |
2482 | u32 reg; | |
2483 | ||
2484 | evt = dwc->ev_buffs[buf]; | |
2485 | left = evt->count; | |
2486 | ||
2487 | if (!(evt->flags & DWC3_EVENT_PENDING)) | |
2488 | return IRQ_NONE; | |
2489 | ||
2490 | while (left > 0) { | |
2491 | union dwc3_event event; | |
2492 | ||
2493 | event.raw = *(u32 *) (evt->buf + evt->lpos); | |
2494 | ||
2495 | dwc3_process_event_entry(dwc, &event); | |
2496 | ||
2497 | /* | |
2498 | * FIXME we wrap around correctly to the next entry as | |
2499 | * almost all entries are 4 bytes in size. There is one | |
2500 | * entry which has 12 bytes which is a regular entry | |
2501 | * followed by 8 bytes data. ATM I don't know how | |
2502 | * things are organized if we get next to the a | |
2503 | * boundary so I worry about that once we try to handle | |
2504 | * that. | |
2505 | */ | |
2506 | evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE; | |
2507 | left -= 4; | |
2508 | ||
2509 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4); | |
2510 | } | |
2511 | ||
2512 | evt->count = 0; | |
2513 | evt->flags &= ~DWC3_EVENT_PENDING; | |
2514 | ret = IRQ_HANDLED; | |
2515 | ||
2516 | /* Unmask interrupt */ | |
2517 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf)); | |
2518 | reg &= ~DWC3_GEVNTSIZ_INTMASK; | |
2519 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg); | |
2520 | ||
2521 | return ret; | |
2522 | } | |
2523 | ||
2524 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc) | |
2525 | { | |
2526 | struct dwc3 *dwc = _dwc; | |
2527 | unsigned long flags; | |
2528 | irqreturn_t ret = IRQ_NONE; | |
2529 | int i; | |
2530 | ||
2531 | spin_lock_irqsave(&dwc->lock, flags); | |
2532 | ||
2533 | for (i = 0; i < dwc->num_event_buffers; i++) | |
2534 | ret |= dwc3_process_event_buf(dwc, i); | |
2535 | ||
2536 | spin_unlock_irqrestore(&dwc->lock, flags); | |
2537 | ||
2538 | return ret; | |
2539 | } | |
2540 | ||
2541 | static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf) | |
2542 | { | |
2543 | struct dwc3_event_buffer *evt; | |
2544 | u32 count; | |
2545 | u32 reg; | |
2546 | ||
2547 | evt = dwc->ev_buffs[buf]; | |
2548 | ||
2549 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf)); | |
2550 | count &= DWC3_GEVNTCOUNT_MASK; | |
2551 | if (!count) | |
2552 | return IRQ_NONE; | |
2553 | ||
2554 | evt->count = count; | |
2555 | evt->flags |= DWC3_EVENT_PENDING; | |
2556 | ||
2557 | /* Mask interrupt */ | |
2558 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf)); | |
2559 | reg |= DWC3_GEVNTSIZ_INTMASK; | |
2560 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg); | |
2561 | ||
2562 | return IRQ_WAKE_THREAD; | |
2563 | } | |
2564 | ||
2565 | static irqreturn_t dwc3_interrupt(int irq, void *_dwc) | |
2566 | { | |
2567 | struct dwc3 *dwc = _dwc; | |
2568 | int i; | |
2569 | irqreturn_t ret = IRQ_NONE; | |
2570 | ||
2571 | spin_lock(&dwc->lock); | |
2572 | ||
2573 | for (i = 0; i < dwc->num_event_buffers; i++) { | |
2574 | irqreturn_t status; | |
2575 | ||
2576 | status = dwc3_check_event_buf(dwc, i); | |
2577 | if (status == IRQ_WAKE_THREAD) | |
2578 | ret = status; | |
2579 | } | |
2580 | ||
2581 | spin_unlock(&dwc->lock); | |
2582 | ||
2583 | return ret; | |
2584 | } | |
2585 | ||
2586 | /** | |
2587 | * dwc3_gadget_init - Initializes gadget related registers | |
2588 | * @dwc: pointer to our controller context structure | |
2589 | * | |
2590 | * Returns 0 on success otherwise negative errno. | |
2591 | */ | |
2592 | int dwc3_gadget_init(struct dwc3 *dwc) | |
2593 | { | |
2594 | int ret; | |
2595 | ||
747a0a5b KVA |
2596 | dwc->ctrl_req = dma_alloc_coherent(sizeof(*dwc->ctrl_req), |
2597 | (unsigned long *)&dwc->ctrl_req_addr); | |
85d5e707 KVA |
2598 | if (!dwc->ctrl_req) { |
2599 | dev_err(dwc->dev, "failed to allocate ctrl request\n"); | |
2600 | ret = -ENOMEM; | |
2601 | goto err0; | |
2602 | } | |
2603 | ||
8d488f3e | 2604 | dwc->ep0_trb = dma_alloc_coherent(sizeof(*dwc->ep0_trb) * 2, |
747a0a5b | 2605 | (unsigned long *)&dwc->ep0_trb_addr); |
85d5e707 KVA |
2606 | if (!dwc->ep0_trb) { |
2607 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); | |
2608 | ret = -ENOMEM; | |
2609 | goto err1; | |
2610 | } | |
2611 | ||
526a50f8 KVA |
2612 | dwc->setup_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, |
2613 | DWC3_EP0_BOUNCE_SIZE); | |
85d5e707 KVA |
2614 | if (!dwc->setup_buf) { |
2615 | ret = -ENOMEM; | |
2616 | goto err2; | |
2617 | } | |
2618 | ||
747a0a5b KVA |
2619 | dwc->ep0_bounce = dma_alloc_coherent(DWC3_EP0_BOUNCE_SIZE, |
2620 | (unsigned long *)&dwc->ep0_bounce_addr); | |
85d5e707 KVA |
2621 | if (!dwc->ep0_bounce) { |
2622 | dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n"); | |
2623 | ret = -ENOMEM; | |
2624 | goto err3; | |
2625 | } | |
2626 | ||
2627 | dwc->gadget.ops = &dwc3_gadget_ops; | |
2628 | dwc->gadget.max_speed = USB_SPEED_SUPER; | |
2629 | dwc->gadget.speed = USB_SPEED_UNKNOWN; | |
85d5e707 KVA |
2630 | dwc->gadget.name = "dwc3-gadget"; |
2631 | ||
2632 | /* | |
2633 | * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize | |
2634 | * on ep out. | |
2635 | */ | |
2636 | dwc->gadget.quirk_ep_out_aligned_size = true; | |
2637 | ||
2638 | /* | |
2639 | * REVISIT: Here we should clear all pending IRQs to be | |
2640 | * sure we're starting from a well known location. | |
2641 | */ | |
2642 | ||
2643 | ret = dwc3_gadget_init_endpoints(dwc); | |
2644 | if (ret) | |
2645 | goto err4; | |
2646 | ||
23ba2d63 | 2647 | ret = usb_add_gadget_udc((struct device *)dwc->dev, &dwc->gadget); |
85d5e707 KVA |
2648 | if (ret) { |
2649 | dev_err(dwc->dev, "failed to register udc\n"); | |
2650 | goto err4; | |
2651 | } | |
2652 | ||
2653 | return 0; | |
2654 | ||
2655 | err4: | |
2656 | dwc3_gadget_free_endpoints(dwc); | |
747a0a5b | 2657 | dma_free_coherent(dwc->ep0_bounce); |
85d5e707 KVA |
2658 | |
2659 | err3: | |
2660 | kfree(dwc->setup_buf); | |
2661 | ||
2662 | err2: | |
747a0a5b | 2663 | dma_free_coherent(dwc->ep0_trb); |
85d5e707 KVA |
2664 | |
2665 | err1: | |
747a0a5b | 2666 | dma_free_coherent(dwc->ctrl_req); |
85d5e707 KVA |
2667 | |
2668 | err0: | |
2669 | return ret; | |
2670 | } | |
2671 | ||
2672 | /* -------------------------------------------------------------------------- */ | |
2673 | ||
2674 | void dwc3_gadget_exit(struct dwc3 *dwc) | |
2675 | { | |
2676 | usb_del_gadget_udc(&dwc->gadget); | |
2677 | ||
2678 | dwc3_gadget_free_endpoints(dwc); | |
2679 | ||
747a0a5b | 2680 | dma_free_coherent(dwc->ep0_bounce); |
85d5e707 KVA |
2681 | |
2682 | kfree(dwc->setup_buf); | |
2683 | ||
747a0a5b | 2684 | dma_free_coherent(dwc->ep0_trb); |
85d5e707 | 2685 | |
747a0a5b KVA |
2686 | dma_free_coherent(dwc->ctrl_req); |
2687 | } | |
2688 | ||
2689 | /** | |
2690 | * dwc3_gadget_uboot_handle_interrupt - handle dwc3 gadget interrupt | |
2691 | * @dwc: struct dwce * | |
2692 | * | |
2693 | * Handles ep0 and gadget interrupt | |
2694 | * | |
2695 | * Should be called from dwc3 core. | |
2696 | */ | |
2697 | void dwc3_gadget_uboot_handle_interrupt(struct dwc3 *dwc) | |
2698 | { | |
137f7c59 MS |
2699 | int ret = dwc3_interrupt(0, dwc); |
2700 | ||
2701 | if (ret == IRQ_WAKE_THREAD) { | |
2702 | int i; | |
2703 | struct dwc3_event_buffer *evt; | |
2704 | ||
889239d6 PT |
2705 | dwc3_thread_interrupt(0, dwc); |
2706 | ||
2707 | /* Clean + Invalidate the buffers after touching them */ | |
137f7c59 MS |
2708 | for (i = 0; i < dwc->num_event_buffers; i++) { |
2709 | evt = dwc->ev_buffs[i]; | |
b7bf4a95 | 2710 | dwc3_flush_cache((uintptr_t)evt->buf, evt->length); |
137f7c59 | 2711 | } |
137f7c59 | 2712 | } |
85d5e707 | 2713 | } |