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