1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/moduleparam.h>
43#include <linux/spinlock.h>
44#include <linux/interrupt.h>
45#include <linux/dma-mapping.h>
46#include <linux/io.h>
47#include <linux/slab.h>
48#include <linux/usb.h>
49
50#include <linux/usb/hcd.h>
51#include <linux/usb/ch11.h>
52
53#include "core.h"
54#include "hcd.h"
55
56static const char *dwc2_op_state_str(struct dwc2_hsotg *hsotg)
57{
58 switch (hsotg->op_state) {
59 case OTG_STATE_A_HOST:
60 return "a_host";
61 case OTG_STATE_A_SUSPEND:
62 return "a_suspend";
63 case OTG_STATE_A_PERIPHERAL:
64 return "a_peripheral";
65 case OTG_STATE_B_PERIPHERAL:
66 return "b_peripheral";
67 case OTG_STATE_B_HOST:
68 return "b_host";
69 default:
70 return "unknown";
71 }
72}
73
74
75
76
77
78
79
80
81static void dwc2_handle_usb_port_intr(struct dwc2_hsotg *hsotg)
82{
83 u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
84
85 if (hprt0 & HPRT0_ENACHG) {
86 hprt0 &= ~HPRT0_ENA;
87 dwc2_writel(hprt0, hsotg->regs + HPRT0);
88 }
89}
90
91
92
93
94
95
96static void dwc2_handle_mode_mismatch_intr(struct dwc2_hsotg *hsotg)
97{
98
99 dwc2_writel(GINTSTS_MODEMIS, hsotg->regs + GINTSTS);
100
101 dev_warn(hsotg->dev, "Mode Mismatch Interrupt: currently in %s mode\n",
102 dwc2_is_host_mode(hsotg) ? "Host" : "Device");
103}
104
105
106
107
108
109
110
111static void dwc2_handle_otg_intr(struct dwc2_hsotg *hsotg)
112{
113 u32 gotgint;
114 u32 gotgctl;
115 u32 gintmsk;
116
117 gotgint = dwc2_readl(hsotg->regs + GOTGINT);
118 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
119 dev_dbg(hsotg->dev, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint,
120 dwc2_op_state_str(hsotg));
121
122 if (gotgint & GOTGINT_SES_END_DET) {
123 dev_dbg(hsotg->dev,
124 " ++OTG Interrupt: Session End Detected++ (%s)\n",
125 dwc2_op_state_str(hsotg));
126 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
127
128 if (dwc2_is_device_mode(hsotg))
129 dwc2_hsotg_disconnect(hsotg);
130
131 if (hsotg->op_state == OTG_STATE_B_HOST) {
132 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
133 } else {
134
135
136
137
138 if (gotgctl & GOTGCTL_DEVHNPEN) {
139 dev_dbg(hsotg->dev, "Session End Detected\n");
140 dev_err(hsotg->dev,
141 "Device Not Connected/Responding!\n");
142 }
143
144
145
146
147
148
149 hsotg->lx_state = DWC2_L0;
150 }
151
152 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
153 gotgctl &= ~GOTGCTL_DEVHNPEN;
154 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
155 }
156
157 if (gotgint & GOTGINT_SES_REQ_SUC_STS_CHNG) {
158 dev_dbg(hsotg->dev,
159 " ++OTG Interrupt: Session Request Success Status Change++\n");
160 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
161 if (gotgctl & GOTGCTL_SESREQSCS) {
162 if (hsotg->core_params->phy_type ==
163 DWC2_PHY_TYPE_PARAM_FS
164 && hsotg->core_params->i2c_enable > 0) {
165 hsotg->srp_success = 1;
166 } else {
167
168 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
169 gotgctl &= ~GOTGCTL_SESREQ;
170 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
171 }
172 }
173 }
174
175 if (gotgint & GOTGINT_HST_NEG_SUC_STS_CHNG) {
176
177
178
179
180 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
181
182
183
184
185 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a)
186 udelay(100);
187 if (gotgctl & GOTGCTL_HSTNEGSCS) {
188 if (dwc2_is_host_mode(hsotg)) {
189 hsotg->op_state = OTG_STATE_B_HOST;
190
191
192
193
194
195
196
197
198
199
200 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
201 gintmsk &= ~GINTSTS_SOF;
202 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
203
204
205
206
207
208 spin_unlock(&hsotg->lock);
209
210
211 dwc2_hcd_start(hsotg);
212 spin_lock(&hsotg->lock);
213 hsotg->op_state = OTG_STATE_B_HOST;
214 }
215 } else {
216 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
217 gotgctl &= ~(GOTGCTL_HNPREQ | GOTGCTL_DEVHNPEN);
218 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
219 dev_dbg(hsotg->dev, "HNP Failed\n");
220 dev_err(hsotg->dev,
221 "Device Not Connected/Responding\n");
222 }
223 }
224
225 if (gotgint & GOTGINT_HST_NEG_DET) {
226
227
228
229
230
231
232 dev_dbg(hsotg->dev,
233 " ++OTG Interrupt: Host Negotiation Detected++ (%s)\n",
234 (dwc2_is_host_mode(hsotg) ? "Host" : "Device"));
235 if (dwc2_is_device_mode(hsotg)) {
236 dev_dbg(hsotg->dev, "a_suspend->a_peripheral (%d)\n",
237 hsotg->op_state);
238 spin_unlock(&hsotg->lock);
239 dwc2_hcd_disconnect(hsotg, false);
240 spin_lock(&hsotg->lock);
241 hsotg->op_state = OTG_STATE_A_PERIPHERAL;
242 } else {
243
244 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
245 gintmsk &= ~GINTSTS_SOF;
246 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
247 spin_unlock(&hsotg->lock);
248 dwc2_hcd_start(hsotg);
249 spin_lock(&hsotg->lock);
250 hsotg->op_state = OTG_STATE_A_HOST;
251 }
252 }
253
254 if (gotgint & GOTGINT_A_DEV_TOUT_CHG)
255 dev_dbg(hsotg->dev,
256 " ++OTG Interrupt: A-Device Timeout Change++\n");
257 if (gotgint & GOTGINT_DBNCE_DONE)
258 dev_dbg(hsotg->dev, " ++OTG Interrupt: Debounce Done++\n");
259
260
261 dwc2_writel(gotgint, hsotg->regs + GOTGINT);
262}
263
264
265
266
267
268
269
270
271
272
273
274static void dwc2_handle_conn_id_status_change_intr(struct dwc2_hsotg *hsotg)
275{
276 u32 gintmsk;
277
278
279 dwc2_writel(GINTSTS_CONIDSTSCHNG, hsotg->regs + GINTSTS);
280
281
282 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
283 gintmsk &= ~GINTSTS_SOF;
284 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
285
286 dev_dbg(hsotg->dev, " ++Connector ID Status Change Interrupt++ (%s)\n",
287 dwc2_is_host_mode(hsotg) ? "Host" : "Device");
288
289
290
291
292
293
294 if (hsotg->wq_otg) {
295 spin_unlock(&hsotg->lock);
296 queue_work(hsotg->wq_otg, &hsotg->wf_otg);
297 spin_lock(&hsotg->lock);
298 }
299}
300
301
302
303
304
305
306
307
308
309
310
311
312static void dwc2_handle_session_req_intr(struct dwc2_hsotg *hsotg)
313{
314 int ret;
315
316
317 dwc2_writel(GINTSTS_SESSREQINT, hsotg->regs + GINTSTS);
318
319 dev_dbg(hsotg->dev, "Session request interrupt - lx_state=%d\n",
320 hsotg->lx_state);
321
322 if (dwc2_is_device_mode(hsotg)) {
323 if (hsotg->lx_state == DWC2_L2) {
324 ret = dwc2_exit_hibernation(hsotg, true);
325 if (ret && (ret != -ENOTSUPP))
326 dev_err(hsotg->dev,
327 "exit hibernation failed\n");
328 }
329
330
331
332
333
334 dwc2_hsotg_disconnect(hsotg);
335 }
336}
337
338
339
340
341
342
343
344
345static void dwc2_handle_wakeup_detected_intr(struct dwc2_hsotg *hsotg)
346{
347 int ret;
348
349
350 dwc2_writel(GINTSTS_WKUPINT, hsotg->regs + GINTSTS);
351
352 dev_dbg(hsotg->dev, "++Resume or Remote Wakeup Detected Interrupt++\n");
353 dev_dbg(hsotg->dev, "%s lxstate = %d\n", __func__, hsotg->lx_state);
354
355 if (dwc2_is_device_mode(hsotg)) {
356 dev_dbg(hsotg->dev, "DSTS=0x%0x\n",
357 dwc2_readl(hsotg->regs + DSTS));
358 if (hsotg->lx_state == DWC2_L2) {
359 u32 dctl = dwc2_readl(hsotg->regs + DCTL);
360
361
362 dctl &= ~DCTL_RMTWKUPSIG;
363 dwc2_writel(dctl, hsotg->regs + DCTL);
364 ret = dwc2_exit_hibernation(hsotg, true);
365 if (ret && (ret != -ENOTSUPP))
366 dev_err(hsotg->dev, "exit hibernation failed\n");
367
368 call_gadget(hsotg, resume);
369 }
370
371 hsotg->lx_state = DWC2_L0;
372 } else {
373 if (hsotg->core_params->hibernation)
374 return;
375
376 if (hsotg->lx_state != DWC2_L1) {
377 u32 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
378
379
380 pcgcctl &= ~PCGCTL_STOPPCLK;
381 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
382 mod_timer(&hsotg->wkp_timer,
383 jiffies + msecs_to_jiffies(71));
384 } else {
385
386 hsotg->lx_state = DWC2_L0;
387 }
388 }
389}
390
391
392
393
394
395static void dwc2_handle_disconnect_intr(struct dwc2_hsotg *hsotg)
396{
397 dwc2_writel(GINTSTS_DISCONNINT, hsotg->regs + GINTSTS);
398
399 dev_dbg(hsotg->dev, "++Disconnect Detected Interrupt++ (%s) %s\n",
400 dwc2_is_host_mode(hsotg) ? "Host" : "Device",
401 dwc2_op_state_str(hsotg));
402
403 if (hsotg->op_state == OTG_STATE_A_HOST)
404 dwc2_hcd_disconnect(hsotg, false);
405}
406
407
408
409
410
411
412
413
414
415static void dwc2_handle_usb_suspend_intr(struct dwc2_hsotg *hsotg)
416{
417 u32 dsts;
418 int ret;
419
420
421 dwc2_writel(GINTSTS_USBSUSP, hsotg->regs + GINTSTS);
422
423 dev_dbg(hsotg->dev, "USB SUSPEND\n");
424
425 if (dwc2_is_device_mode(hsotg)) {
426
427
428
429
430 dsts = dwc2_readl(hsotg->regs + DSTS);
431 dev_dbg(hsotg->dev, "DSTS=0x%0x\n", dsts);
432 dev_dbg(hsotg->dev,
433 "DSTS.Suspend Status=%d HWCFG4.Power Optimize=%d\n",
434 !!(dsts & DSTS_SUSPSTS),
435 hsotg->hw_params.power_optimized);
436 if ((dsts & DSTS_SUSPSTS) && hsotg->hw_params.power_optimized) {
437
438 if (!dwc2_is_device_connected(hsotg)) {
439 dev_dbg(hsotg->dev,
440 "ignore suspend request before enumeration\n");
441 return;
442 }
443
444 ret = dwc2_enter_hibernation(hsotg);
445 if (ret) {
446 if (ret != -ENOTSUPP)
447 dev_err(hsotg->dev,
448 "enter hibernation failed\n");
449 goto skip_power_saving;
450 }
451
452 udelay(100);
453
454
455 if (!IS_ERR_OR_NULL(hsotg->uphy))
456 usb_phy_set_suspend(hsotg->uphy, true);
457skip_power_saving:
458
459
460
461
462 hsotg->lx_state = DWC2_L2;
463
464
465 call_gadget(hsotg, suspend);
466 }
467 } else {
468 if (hsotg->op_state == OTG_STATE_A_PERIPHERAL) {
469 dev_dbg(hsotg->dev, "a_peripheral->a_host\n");
470
471
472 hsotg->lx_state = DWC2_L2;
473
474 spin_unlock(&hsotg->lock);
475 dwc2_hcd_start(hsotg);
476 spin_lock(&hsotg->lock);
477 hsotg->op_state = OTG_STATE_A_HOST;
478 }
479 }
480}
481
482#define GINTMSK_COMMON (GINTSTS_WKUPINT | GINTSTS_SESSREQINT | \
483 GINTSTS_CONIDSTSCHNG | GINTSTS_OTGINT | \
484 GINTSTS_MODEMIS | GINTSTS_DISCONNINT | \
485 GINTSTS_USBSUSP | GINTSTS_PRTINT)
486
487
488
489
490static u32 dwc2_read_common_intr(struct dwc2_hsotg *hsotg)
491{
492 u32 gintsts;
493 u32 gintmsk;
494 u32 gahbcfg;
495 u32 gintmsk_common = GINTMSK_COMMON;
496
497 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
498 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
499 gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
500
501
502 if (gintsts & gintmsk_common)
503 dev_dbg(hsotg->dev, "gintsts=%08x gintmsk=%08x\n",
504 gintsts, gintmsk);
505
506 if (gahbcfg & GAHBCFG_GLBL_INTR_EN)
507 return gintsts & gintmsk & gintmsk_common;
508 else
509 return 0;
510}
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525irqreturn_t dwc2_handle_common_intr(int irq, void *dev)
526{
527 struct dwc2_hsotg *hsotg = dev;
528 u32 gintsts;
529 irqreturn_t retval = IRQ_NONE;
530
531 spin_lock(&hsotg->lock);
532
533 if (!dwc2_is_controller_alive(hsotg)) {
534 dev_warn(hsotg->dev, "Controller is dead\n");
535 goto out;
536 }
537
538 gintsts = dwc2_read_common_intr(hsotg);
539 if (gintsts & ~GINTSTS_PRTINT)
540 retval = IRQ_HANDLED;
541
542 if (gintsts & GINTSTS_MODEMIS)
543 dwc2_handle_mode_mismatch_intr(hsotg);
544 if (gintsts & GINTSTS_OTGINT)
545 dwc2_handle_otg_intr(hsotg);
546 if (gintsts & GINTSTS_CONIDSTSCHNG)
547 dwc2_handle_conn_id_status_change_intr(hsotg);
548 if (gintsts & GINTSTS_DISCONNINT)
549 dwc2_handle_disconnect_intr(hsotg);
550 if (gintsts & GINTSTS_SESSREQINT)
551 dwc2_handle_session_req_intr(hsotg);
552 if (gintsts & GINTSTS_WKUPINT)
553 dwc2_handle_wakeup_detected_intr(hsotg);
554 if (gintsts & GINTSTS_USBSUSP)
555 dwc2_handle_usb_suspend_intr(hsotg);
556
557 if (gintsts & GINTSTS_PRTINT) {
558
559
560
561
562 if (dwc2_is_device_mode(hsotg)) {
563 dev_dbg(hsotg->dev,
564 " --Port interrupt received in Device mode--\n");
565 dwc2_handle_usb_port_intr(hsotg);
566 retval = IRQ_HANDLED;
567 }
568 }
569
570out:
571 spin_unlock(&hsotg->lock);
572 return retval;
573}
574