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