]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/usb/dwc3/otg.c
e72a05cf74abaf5c4eb9111f0de5d85c50d69837
[zynq/linux.git] / drivers / usb / dwc3 / otg.c
1 /**
2  * otg.c - DesignWare USB3 DRD Controller OTG file
3  *
4  * Copyright (C) 2016 Xilinx, Inc. All rights reserved.
5  *
6  * Author:  Manish Narani <mnarani@xilinx.com>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2  of
10  * the License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/pci.h>
22 #include <linux/slab.h>
23 #include <linux/sched/signal.h>
24 #include <linux/sched.h>
25 #include <linux/freezer.h>
26 #include <linux/kthread.h>
27 #include <linux/version.h>
28 #include <linux/sysfs.h>
29
30 #include <linux/usb.h>
31 #include <linux/usb/hcd.h>
32 #include <linux/usb/gadget.h>
33 #include <linux/usb/otg.h>
34 #include <linux/usb/phy.h>
35
36 #include <../drivers/usb/host/xhci.h>
37 #include "platform_data.h"
38 #include "core.h"
39 #include "gadget.h"
40 #include "io.h"
41 #include "otg.h"
42
43 #include <linux/ulpi/regs.h>
44 #include <linux/ulpi/driver.h>
45 #include "debug.h"
46
47 /* Print the hardware registers' value for debugging purpose */
48 static void print_debug_regs(struct dwc3_otg *otg)
49 {
50         u32 gctl = otg_read(otg, DWC3_GCTL);
51         u32 gsts = otg_read(otg, DWC3_GSTS);
52         u32 gdbgltssm = otg_read(otg, DWC3_GDBGLTSSM);
53         u32 gusb2phycfg0 = otg_read(otg, DWC3_GUSB2PHYCFG(0));
54         u32 gusb3pipectl0 = otg_read(otg, DWC3_GUSB3PIPECTL(0));
55         u32 dcfg = otg_read(otg, DWC3_DCFG);
56         u32 dctl = otg_read(otg, DWC3_DCTL);
57         u32 dsts = otg_read(otg, DWC3_DSTS);
58         u32 ocfg = otg_read(otg, OCFG);
59         u32 octl = otg_read(otg, OCTL);
60         u32 oevt = otg_read(otg, OEVT);
61         u32 oevten = otg_read(otg, OEVTEN);
62         u32 osts = otg_read(otg, OSTS);
63
64         otg_info(otg, "gctl = %08x\n", gctl);
65         otg_info(otg, "gsts = %08x\n", gsts);
66         otg_info(otg, "gdbgltssm = %08x\n", gdbgltssm);
67         otg_info(otg, "gusb2phycfg0 = %08x\n", gusb2phycfg0);
68         otg_info(otg, "gusb3pipectl0 = %08x\n", gusb3pipectl0);
69         otg_info(otg, "dcfg = %08x\n", dcfg);
70         otg_info(otg, "dctl = %08x\n", dctl);
71         otg_info(otg, "dsts = %08x\n", dsts);
72         otg_info(otg, "ocfg = %08x\n", ocfg);
73         otg_info(otg, "octl = %08x\n", octl);
74         otg_info(otg, "oevt = %08x\n", oevt);
75         otg_info(otg, "oevten = %08x\n", oevten);
76         otg_info(otg, "osts = %08x\n", osts);
77 }
78
79 /* Check whether the hardware supports HNP or not */
80 static int hnp_capable(struct dwc3_otg *otg)
81 {
82         if (otg->hwparams6 & GHWPARAMS6_HNP_SUPPORT_ENABLED)
83                 return 1;
84         return 0;
85 }
86
87 /* Check whether the hardware supports SRP or not */
88 static int srp_capable(struct dwc3_otg *otg)
89 {
90         if (otg->hwparams6 & GHWPARAMS6_SRP_SUPPORT_ENABLED)
91                 return 1;
92         return 0;
93 }
94
95 /* Wakeup main thread to execute the OTG flow after an event */
96 static void wakeup_main_thread(struct dwc3_otg *otg)
97 {
98         if (!otg->main_thread)
99                 return;
100
101         otg_vdbg(otg, "\n");
102         /* Tell the main thread that something has happened */
103         otg->main_wakeup_needed = 1;
104         wake_up_interruptible(&otg->main_wq);
105 }
106
107 /* Sleep main thread for 'msecs' to wait for an event to occur */
108 static int sleep_main_thread_timeout(struct dwc3_otg *otg, int msecs)
109 {
110         signed long jiffies;
111         int rc = msecs;
112
113         if (signal_pending(current)) {
114                 otg_dbg(otg, "Main thread signal pending\n");
115                 rc = -EINTR;
116                 goto done;
117         }
118         if (otg->main_wakeup_needed) {
119                 otg_dbg(otg, "Main thread wakeup needed\n");
120                 rc = msecs;
121                 goto done;
122         }
123
124         jiffies = msecs_to_jiffies(msecs);
125         rc = wait_event_freezable_timeout(otg->main_wq,
126                                           otg->main_wakeup_needed,
127                                           jiffies);
128
129         if (rc > 0)
130                 rc = jiffies_to_msecs(rc);
131
132 done:
133         otg->main_wakeup_needed = 0;
134         return rc;
135 }
136
137 /* Sleep main thread to wait for an event to occur */
138 static int sleep_main_thread(struct dwc3_otg *otg)
139 {
140         int rc;
141
142         do {
143                 rc = sleep_main_thread_timeout(otg, 5000);
144         } while (rc == 0);
145
146         return rc;
147 }
148
149 static void get_events(struct dwc3_otg *otg, u32 *otg_events, u32 *user_events)
150 {
151         unsigned long flags;
152
153         spin_lock_irqsave(&otg->lock, flags);
154
155         if (otg_events)
156                 *otg_events = otg->otg_events;
157
158         if (user_events)
159                 *user_events = otg->user_events;
160
161         spin_unlock_irqrestore(&otg->lock, flags);
162 }
163
164 static void get_and_clear_events(struct dwc3_otg *otg, u32 *otg_events,
165                 u32 *user_events)
166 {
167         unsigned long flags;
168
169         spin_lock_irqsave(&otg->lock, flags);
170
171         if (otg_events)
172                 *otg_events = otg->otg_events;
173
174         if (user_events)
175                 *user_events = otg->user_events;
176
177         otg->otg_events = 0;
178         otg->user_events = 0;
179
180         spin_unlock_irqrestore(&otg->lock, flags);
181 }
182
183 static int check_event(struct dwc3_otg *otg, u32 otg_mask, u32 user_mask)
184 {
185         u32 otg_events;
186         u32 user_events;
187
188         get_events(otg, &otg_events, &user_events);
189         if ((otg_events & otg_mask) || (user_events & user_mask)) {
190                 otg_dbg(otg, "Event occurred: otg_events=%x, otg_mask=%x, \
191                                 user_events=%x, user_mask=%x\n", otg_events,
192                                 otg_mask, user_events, user_mask);
193                 return 1;
194         }
195
196         return 0;
197 }
198
199 static int sleep_until_event(struct dwc3_otg *otg, u32 otg_mask, u32 user_mask,
200                 u32 *otg_events, u32 *user_events, int timeout)
201 {
202         int rc;
203
204         /* Enable the events */
205         if (otg_mask)
206                 otg_write(otg, OEVTEN, otg_mask);
207
208         /* Wait until it occurs, or timeout, or interrupt. */
209         if (timeout) {
210                 otg_vdbg(otg, "Waiting for event (timeout=%d)...\n", timeout);
211                 rc = sleep_main_thread_until_condition_timeout(otg,
212                         check_event(otg, otg_mask, user_mask), timeout);
213         } else {
214                 otg_vdbg(otg, "Waiting for event (no timeout)...\n");
215                 rc = sleep_main_thread_until_condition(otg,
216                         check_event(otg, otg_mask, user_mask));
217         }
218
219         /* Disable the events */
220         otg_write(otg, OEVTEN, 0);
221
222         otg_vdbg(otg, "Woke up rc=%d\n", rc);
223         if (rc >= 0)
224                 get_and_clear_events(otg, otg_events, user_events);
225
226         return rc;
227 }
228
229 static void set_capabilities(struct dwc3_otg *otg)
230 {
231         u32 ocfg = 0;
232
233         otg_dbg(otg, "\n");
234         if (srp_capable(otg))
235                 ocfg |= OCFG_SRP_CAP;
236
237         if (hnp_capable(otg))
238                 ocfg |= OCFG_HNP_CAP;
239
240         otg_write(otg, OCFG, ocfg);
241
242         otg_dbg(otg, "Enabled SRP and HNP capabilities in OCFG\n");
243 }
244
245 static int otg3_handshake(struct dwc3_otg *otg, u32 reg, u32 mask, u32 done,
246                 u32 msec)
247 {
248         u32 result;
249         u32 usec = msec * 1000;
250
251         otg_vdbg(otg, "reg=%08x, mask=%08x, value=%08x\n", reg, mask, done);
252         do {
253                 result = otg_read(otg, reg);
254                 if ((result & mask) == done)
255                         return 1;
256                 udelay(1);
257                 usec -= 1;
258         } while (usec > 0);
259
260         return 0;
261 }
262
263 static int reset_port(struct dwc3_otg *otg)
264 {
265         otg_dbg(otg, "\n");
266         if (!otg->otg.host)
267                 return -ENODEV;
268         return usb_bus_start_enum(otg->otg.host, 1);
269 }
270
271 static int set_peri_mode(struct dwc3_otg *otg, int mode)
272 {
273         u32 octl;
274
275         /* Set peri_mode */
276         octl = otg_read(otg, OCTL);
277         if (mode)
278                 octl |= OCTL_PERI_MODE;
279         else
280                 octl &= ~OCTL_PERI_MODE;
281
282         otg_write(otg, OCTL, octl);
283         otg_dbg(otg, "set OCTL PERI_MODE = %d in OCTL\n", mode);
284
285         if (mode)
286                 return otg3_handshake(otg, OSTS, OSTS_PERIP_MODE,
287                                 OSTS_PERIP_MODE, 100);
288         else
289                 return otg3_handshake(otg, OSTS, OSTS_PERIP_MODE, 0, 100);
290
291         msleep(20);
292 }
293
294 static int start_host(struct dwc3_otg *otg)
295 {
296         int ret = -ENODEV;
297         int flg;
298         u32 octl;
299         u32 osts;
300         u32 ocfg;
301         u32 dctl;
302         struct usb_hcd *hcd;
303         struct xhci_hcd *xhci;
304
305         otg_dbg(otg, "\n");
306
307         if (!otg->otg.host)
308                 return -ENODEV;
309
310         /*
311          * Prevent the host USBCMD.HCRST from resetting OTG core by setting
312          * OCFG.OTGSftRstMsk
313          */
314         ocfg = otg_read(otg, OCFG);
315         ocfg |= DWC3_OCFG_SFTRSTMASK;
316         otg_write(otg, OCFG, ocfg);
317
318         dctl = otg_read(otg, DCTL);
319         if (dctl & DWC3_DCTL_RUN_STOP) {
320                 otg_dbg(otg, "Disabling the RUN/STOP bit\n");
321                 dctl &= ~DWC3_DCTL_RUN_STOP;
322                 otg_write(otg, DCTL, dctl);
323         }
324
325         if (!set_peri_mode(otg, PERI_MODE_HOST)) {
326                 otg_err(otg, "Failed to start host\n");
327                 return -EINVAL;
328         }
329
330         hcd = container_of(otg->otg.host, struct usb_hcd, self);
331         xhci = hcd_to_xhci(hcd);
332         otg_dbg(otg, "hcd=%p xhci=%p\n", hcd, xhci);
333
334         if (otg->host_started) {
335                 otg_info(otg, "Host already started\n");
336                 goto skip;
337         }
338
339         /* Start host driver */
340
341         *(struct xhci_hcd **)hcd->hcd_priv = xhci;
342         ret = usb_add_hcd(hcd, otg->hcd_irq, IRQF_SHARED);
343         if (ret) {
344                 otg_err(otg, "%s: failed to start primary hcd, ret=%d\n",
345                         __func__, ret);
346                 return ret;
347         }
348
349         *(struct xhci_hcd **)xhci->shared_hcd->hcd_priv = xhci;
350         if (xhci->shared_hcd) {
351                 ret = usb_add_hcd(xhci->shared_hcd, otg->hcd_irq, IRQF_SHARED);
352                 if (ret) {
353                         otg_err(otg,
354                                 "%s: failed to start secondary hcd, ret=%d\n",
355                                 __func__, ret);
356                         usb_remove_hcd(hcd);
357                         return ret;
358                 }
359         }
360
361         otg->host_started = 1;
362 skip:
363         hcd->self.otg_port = 1;
364         if (xhci->shared_hcd)
365                 xhci->shared_hcd->self.otg_port = 1;
366
367         set_capabilities(otg);
368
369         /* Power the port only for A-host */
370         if (otg->otg.state == OTG_STATE_A_WAIT_VRISE) {
371                 /* Spin on xhciPrtPwr bit until it becomes 1 */
372                 osts = otg_read(otg, OSTS);
373                 flg = otg3_handshake(otg, OSTS,
374                                 OSTS_XHCI_PRT_PWR,
375                                 OSTS_XHCI_PRT_PWR,
376                                 1000);
377                 if (flg) {
378                         otg_dbg(otg, "Port is powered by xhci-hcd\n");
379                         /* Set port power control bit */
380                         octl = otg_read(otg, OCTL);
381                         octl |= OCTL_PRT_PWR_CTL;
382                         otg_write(otg, OCTL, octl);
383                 } else {
384                         otg_dbg(otg, "Port is not powered by xhci-hcd\n");
385                 }
386         }
387
388         return ret;
389 }
390
391 static int stop_host(struct dwc3_otg *otg)
392 {
393         struct usb_hcd *hcd;
394         struct xhci_hcd *xhci;
395
396         otg_dbg(otg, "\n");
397
398         if (!otg->host_started) {
399                 otg_info(otg, "Host already stopped\n");
400                 return 1;
401         }
402
403         if (!otg->otg.host)
404                 return -ENODEV;
405
406         otg_dbg(otg, "%s: turn off host %s\n",
407                 __func__, otg->otg.host->bus_name);
408
409         if (work_pending(&otg->hp_work.work)) {
410                 while (!cancel_delayed_work(&otg->hp_work))
411                         msleep(20);
412         }
413
414         hcd = container_of(otg->otg.host, struct usb_hcd, self);
415         xhci = hcd_to_xhci(hcd);
416
417         if (xhci->shared_hcd)
418                 usb_remove_hcd(xhci->shared_hcd);
419         usb_remove_hcd(hcd);
420
421         otg->host_started = 0;
422         otg->dev_enum = 0;
423         return 0;
424 }
425
426 int dwc3_otg_host_release(struct usb_hcd *hcd)
427 {
428         struct usb_bus *bus;
429         struct usb_device *rh;
430         struct usb_device *udev;
431
432         if (!hcd)
433                 return -EINVAL;
434
435         bus = &hcd->self;
436         if (!bus->otg_port)
437                 return 0;
438
439         rh = bus->root_hub;
440         udev = usb_hub_find_child(rh, bus->otg_port);
441         if (!udev)
442                 return 0;
443
444         if (udev->config && udev->parent == udev->bus->root_hub) {
445                 struct usb_otg20_descriptor *desc;
446
447                 if (__usb_get_extra_descriptor(udev->rawdescriptors[0],
448                                 le16_to_cpu(udev->config[0].desc.wTotalLength),
449                                 USB_DT_OTG, (void **) &desc) == 0) {
450                         int err;
451
452                         dev_info(&udev->dev, "found OTG descriptor\n");
453                         if ((desc->bcdOTG >= 0x0200) &&
454                             (udev->speed == USB_SPEED_HIGH)) {
455                                 err = usb_control_msg(udev,
456                                                 usb_sndctrlpipe(udev, 0),
457                                                 USB_REQ_SET_FEATURE, 0,
458                                                 USB_DEVICE_TEST_MODE,
459                                                 7 << 8,
460                                                 NULL, 0, USB_CTRL_SET_TIMEOUT);
461                                 if (err < 0) {
462                                         dev_info(&udev->dev,
463                                                 "can't initiate HNP from host: %d\n",
464                                                 err);
465                                         return -1;
466                                 }
467                         }
468                 } else {
469                         dev_info(&udev->dev, "didn't find OTG descriptor\n");
470                 }
471         } else {
472                 dev_info(&udev->dev,
473                          "udev->config NULL or udev->parent != udev->bus->root_hub\n");
474         }
475
476         return 0;
477 }
478
479 /* Sends the host release set feature request */
480 static void host_release(struct dwc3_otg *otg)
481 {
482         struct usb_hcd *hcd;
483         struct xhci_hcd *xhci;
484
485         otg_dbg(otg, "\n");
486         if (!otg->otg.host)
487                 return;
488         hcd = container_of(otg->otg.host, struct usb_hcd, self);
489         xhci = hcd_to_xhci(hcd);
490         dwc3_otg_host_release(hcd);
491         if (xhci->shared_hcd)
492                 dwc3_otg_host_release(xhci->shared_hcd);
493 }
494
495 static void dwc3_otg_setup_event_buffers(struct dwc3_otg *otg)
496 {
497         if (dwc3_readl(otg->dwc->regs, DWC3_GEVNTADRLO(0)) == 0x0) {
498
499                 otg_dbg(otg, "setting up event buffers\n");
500                 dwc3_event_buffers_setup(otg->dwc);
501         }
502
503 }
504
505 static void start_peripheral(struct dwc3_otg *otg)
506 {
507         struct usb_gadget *gadget = otg->otg.gadget;
508         struct dwc3 *dwc = otg->dwc;
509         u32 ocfg;
510
511         otg_dbg(otg, "\n");
512         if (!gadget)
513                 return;
514
515         /*
516          * Prevent the gadget DCTL.CSFTRST from resetting OTG core by setting
517          * OCFG.OTGSftRstMsk
518          */
519         ocfg = otg_read(otg, OCFG);
520         ocfg |= DWC3_OCFG_SFTRSTMASK;
521         otg_write(otg, OCFG, ocfg);
522
523         if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
524                 otg_err(otg, "Failed to set peripheral mode\n");
525
526         if (otg->peripheral_started) {
527                 otg_info(otg, "Peripheral already started\n");
528                 return;
529         }
530
531         set_capabilities(otg);
532
533         dwc3_otg_setup_event_buffers(otg);
534
535         if (dwc->gadget_driver) {
536                 struct dwc3_ep          *dep;
537                 int                     ret;
538
539                 spin_lock(&otg->lock);
540                 dep = dwc->eps[0];
541
542                 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
543                 if (ret)
544                         goto err0;
545
546                 dep = dwc->eps[1];
547
548                 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
549                 if (ret)
550                         goto err1;
551
552                 otg_dbg(otg, "enabled ep in gadget driver\n");
553                 /* begin to receive SETUP packets */
554                 dwc->ep0state = EP0_SETUP_PHASE;
555                 dwc3_ep0_out_start(dwc);
556
557                 otg_dbg(otg, "enabled irq\n");
558                 dwc3_gadget_enable_irq(dwc);
559
560                 otg_write(otg, DCTL, otg_read(otg, DCTL) | DCTL_RUN_STOP);
561                 otg_dbg(otg, "Setting DCTL_RUN_STOP to 1 in DCTL\n");
562                 spin_unlock(&otg->lock);
563         }
564
565         gadget->b_hnp_enable = 0;
566         gadget->host_request_flag = 0;
567
568         otg->peripheral_started = 1;
569
570         /*
571          * During HNP the bus shouldn't be idle for more than 155 ms, so
572          * give enough time for the host to load the stack before start
573          * triggerring events
574          */
575         msleep(500);
576
577         return;
578 err1:
579                 __dwc3_gadget_ep_disable(dwc->eps[0]);
580
581 err0:
582                 return;
583 }
584
585 static void stop_peripheral(struct dwc3_otg *otg)
586 {
587         struct usb_gadget *gadget = otg->otg.gadget;
588         struct dwc3 *dwc = otg->dwc;
589
590         otg_dbg(otg, "\n");
591
592         if (!otg->peripheral_started) {
593                 otg_info(otg, "Peripheral already stopped\n");
594                 return;
595         }
596
597         if (!gadget)
598                 return;
599
600         otg_dbg(otg, "disabled ep in gadget driver\n");
601         spin_lock(&otg->lock);
602
603         dwc3_gadget_disable_irq(dwc);
604         __dwc3_gadget_ep_disable(dwc->eps[0]);
605         __dwc3_gadget_ep_disable(dwc->eps[1]);
606
607         spin_unlock(&otg->lock);
608
609         otg->peripheral_started = 0;
610         msleep(20);
611 }
612
613 static void set_b_host(struct dwc3_otg *otg, int val)
614 {
615         otg->otg.host->is_b_host = val;
616 }
617
618 static enum usb_otg_state do_b_idle(struct dwc3_otg *otg);
619
620 static int init_b_device(struct dwc3_otg *otg)
621 {
622         otg_dbg(otg, "\n");
623         set_capabilities(otg);
624
625         if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
626                 otg_err(otg, "Failed to start peripheral\n");
627
628         return do_b_idle(otg);
629 }
630
631 static int init_a_device(struct dwc3_otg *otg)
632 {
633         otg_write(otg, OCFG, 0);
634         otg_write(otg, OCTL, 0);
635
636         otg_dbg(otg, "Write 0 to OCFG and OCTL\n");
637         return OTG_STATE_A_IDLE;
638 }
639
640 static enum usb_otg_state do_connector_id_status(struct dwc3_otg *otg)
641 {
642         enum usb_otg_state state;
643         u32 osts;
644
645         otg_dbg(otg, "\n");
646
647         otg_write(otg, OCFG, 0);
648         otg_write(otg, OEVTEN, 0);
649         otg_write(otg, OEVT, 0xffffffff);
650         otg_write(otg, OEVTEN, OEVT_CONN_ID_STS_CHNG_EVNT);
651
652         msleep(60);
653
654         osts = otg_read(otg, OSTS);
655         if (!(osts & OSTS_CONN_ID_STS)) {
656                 otg_dbg(otg, "Connector ID is A\n");
657                 state = init_a_device(otg);
658         } else {
659                 otg_dbg(otg, "Connector ID is B\n");
660                 stop_host(otg);
661                 state = init_b_device(otg);
662         }
663
664         /* TODO: This is a workaround for latest hibernation-enabled bitfiles
665          * which have problems before initializing SRP.
666          */
667         msleep(50);
668
669         return state;
670 }
671
672 static void reset_hw(struct dwc3_otg *otg)
673 {
674         u32 temp;
675
676         otg_dbg(otg, "\n");
677
678         otg_write(otg, OEVTEN, 0);
679         temp = otg_read(otg, OCTL);
680         temp &= OCTL_PERI_MODE;
681         otg_write(otg, OCTL, temp);
682         temp = otg_read(otg, GCTL);
683         temp |= GCTL_PRT_CAP_DIR_OTG << GCTL_PRT_CAP_DIR_SHIFT;
684         otg_write(otg, GCTL, temp);
685 }
686
687 #define SRP_TIMEOUT                     6000
688
689 static void start_srp(struct dwc3_otg *otg)
690 {
691         u32 octl;
692
693         octl = otg_read(otg, OCTL);
694         octl |= OCTL_SES_REQ;
695         otg_write(otg, OCTL, octl);
696         otg_dbg(otg, "set OCTL_SES_REQ in OCTL\n");
697 }
698
699 static void start_b_hnp(struct dwc3_otg *otg)
700 {
701         u32 octl;
702
703         octl = otg_read(otg, OCTL);
704         octl |= OCTL_HNP_REQ | OCTL_DEV_SET_HNP_EN;
705         otg_write(otg, OCTL, octl);
706         otg_dbg(otg, "set (OCTL_HNP_REQ | OCTL_DEV_SET_HNP_EN) in OCTL\n");
707 }
708
709 static void stop_b_hnp(struct dwc3_otg *otg)
710 {
711         u32 octl;
712
713         octl = otg_read(otg, OCTL);
714         octl &= ~(OCTL_HNP_REQ | OCTL_DEV_SET_HNP_EN);
715         otg_write(otg, OCTL, octl);
716         otg_dbg(otg, "Clear ~(OCTL_HNP_REQ | OCTL_DEV_SET_HNP_EN) in OCTL\n");
717 }
718
719 static void start_a_hnp(struct dwc3_otg *otg)
720 {
721         u32 octl;
722
723         octl = otg_read(otg, OCTL);
724         octl |= OCTL_HST_SET_HNP_EN;
725         otg_write(otg, OCTL, octl);
726         otg_dbg(otg, "set OCTL_HST_SET_HNP_EN in OCTL\n");
727 }
728
729 static void stop_a_hnp(struct dwc3_otg *otg)
730 {
731         u32 octl;
732
733         octl = otg_read(otg, OCTL);
734         octl &= ~OCTL_HST_SET_HNP_EN;
735         otg_write(otg, OCTL, octl);
736         otg_dbg(otg, "clear OCTL_HST_SET_HNP_EN in OCTL\n");
737 }
738
739 static enum usb_otg_state do_a_hnp_init(struct dwc3_otg *otg)
740 {
741         int rc;
742         u32 otg_mask;
743         u32 otg_events = 0;
744
745         otg_dbg(otg, "");
746         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
747                 OEVT_A_DEV_HNP_CHNG_EVNT;
748
749         start_a_hnp(otg);
750         rc = 3000;
751
752 again:
753         rc = sleep_until_event(otg,
754                         otg_mask, 0,
755                         &otg_events, NULL, rc);
756         stop_a_hnp(otg);
757         if (rc < 0)
758                 return OTG_STATE_UNDEFINED;
759
760         /* Higher priority first */
761         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
762                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
763                 return OTG_STATE_UNDEFINED;
764
765         } else if (otg_events & OEVT_A_DEV_HNP_CHNG_EVNT) {
766                 otg_dbg(otg, "OEVT_A_DEV_HNP_CHNG_EVNT\n");
767                 if (otg_events & OEVT_HST_NEG_SCS) {
768                         otg_dbg(otg, "A-HNP Success\n");
769                         return OTG_STATE_A_PERIPHERAL;
770
771                 } else {
772                         otg_dbg(otg, "A-HNP Failed\n");
773                         return OTG_STATE_A_WAIT_VFALL;
774                 }
775
776         } else if (rc == 0) {
777                 otg_dbg(otg, "A-HNP Failed (Timed out)\n");
778                 return OTG_STATE_A_WAIT_VFALL;
779
780         } else {
781                 goto again;
782         }
783
784         /* Invalid state */
785         return OTG_STATE_UNDEFINED;
786 }
787
788 static enum usb_otg_state do_a_host(struct dwc3_otg *otg)
789 {
790         int rc;
791         u32 otg_mask;
792         u32 user_mask;
793         u32 otg_events = 0;
794         u32 user_events = 0;
795
796         otg_dbg(otg, "");
797
798         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
799                 OEVT_A_DEV_SESS_END_DET_EVNT;
800         user_mask = USER_SRP_EVENT |
801                 USER_HNP_EVENT;
802
803         rc = sleep_until_event(otg,
804                         otg_mask, user_mask,
805                         &otg_events, &user_events, 0);
806         if (rc < 0)
807                 return OTG_STATE_UNDEFINED;
808
809         /* Higher priority first */
810         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
811                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
812                 return OTG_STATE_UNDEFINED;
813
814         } else if (otg_events & OEVT_A_DEV_SESS_END_DET_EVNT) {
815                 otg_dbg(otg, "OEVT_A_DEV_SESS_END_DET_EVNT\n");
816                 return OTG_STATE_A_WAIT_VFALL;
817
818         } else if (user_events & USER_HNP_EVENT) {
819                 otg_dbg(otg, "USER_HNP_EVENT\n");
820                 return OTG_STATE_A_SUSPEND;
821         }
822
823         /* Invalid state */
824         return OTG_STATE_UNDEFINED;
825 }
826
827 #define A_WAIT_VFALL_TIMEOUT 1000
828
829 static enum usb_otg_state do_a_wait_vfall(struct dwc3_otg *otg)
830 {
831         int rc;
832         u32 otg_mask;
833         u32 otg_events = 0;
834
835         otg_dbg(otg, "");
836
837         otg_mask = OEVT_A_DEV_IDLE_EVNT;
838
839         rc = A_WAIT_VFALL_TIMEOUT;
840         rc = sleep_until_event(otg,
841                         otg_mask, 0,
842                         &otg_events, NULL, rc);
843         if (rc < 0)
844                 return OTG_STATE_UNDEFINED;
845
846         if (otg_events & OEVT_A_DEV_IDLE_EVNT) {
847                 otg_dbg(otg, "OEVT_A_DEV_IDLE_EVNT\n");
848                 return OTG_STATE_A_IDLE;
849
850         } else if (rc == 0) {
851                 otg_dbg(otg, "A_WAIT_VFALL_TIMEOUT\n");
852                 return OTG_STATE_A_IDLE;
853         }
854
855         /* Invalid state */
856         return OTG_STATE_UNDEFINED;
857
858 }
859
860 #define A_WAIT_BCON_TIMEOUT 1000
861
862 static enum usb_otg_state do_a_wait_bconn(struct dwc3_otg *otg)
863 {
864         int rc;
865         u32 otg_mask;
866         u32 otg_events = 0;
867
868         otg_dbg(otg, "");
869
870         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
871                 OEVT_A_DEV_SESS_END_DET_EVNT |
872                 OEVT_A_DEV_HOST_EVNT;
873
874         rc = A_WAIT_BCON_TIMEOUT;
875         rc = sleep_until_event(otg,
876                         otg_mask, 0,
877                         &otg_events, NULL, rc);
878         if (rc < 0)
879                 return OTG_STATE_UNDEFINED;
880
881         /* Higher priority first */
882         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
883                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
884                 return OTG_STATE_UNDEFINED;
885
886         } else if (otg_events & OEVT_A_DEV_SESS_END_DET_EVNT) {
887                 otg_dbg(otg, "OEVT_A_DEV_SESS_END_DET_EVNT\n");
888                 return OTG_STATE_A_WAIT_VFALL;
889
890         } else if (otg_events & OEVT_A_DEV_HOST_EVNT) {
891                 otg_dbg(otg, "OEVT_A_DEV_HOST_EVNT\n");
892                 return OTG_STATE_A_HOST;
893
894         } else if (rc == 0) {
895                 if (otg_read(otg, OCTL) & OCTL_PRT_PWR_CTL)
896                         return OTG_STATE_A_HOST;
897                 else
898                         return OTG_STATE_A_WAIT_VFALL;
899         }
900
901         /* Invalid state */
902         return OTG_STATE_UNDEFINED;
903 }
904
905 #define A_WAIT_VRISE_TIMEOUT 100
906
907 static enum usb_otg_state do_a_wait_vrise(struct dwc3_otg *otg)
908 {
909         int rc;
910         u32 otg_mask;
911         u32 otg_events = 0;
912         struct usb_hcd *hcd;
913         struct xhci_hcd *xhci;
914
915         otg_dbg(otg, "");
916         set_b_host(otg, 0);
917         start_host(otg);
918         hcd = container_of(otg->otg.host, struct usb_hcd, self);
919         xhci = hcd_to_xhci(hcd);
920         usb_kick_hub_wq(hcd->self.root_hub);
921         if (xhci->shared_hcd)
922                 usb_kick_hub_wq(xhci->shared_hcd->self.root_hub);
923
924         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
925                 OEVT_A_DEV_SESS_END_DET_EVNT;
926
927         rc = A_WAIT_VRISE_TIMEOUT;
928
929         rc = sleep_until_event(otg,
930                         otg_mask, 0,
931                         &otg_events, NULL, rc);
932         if (rc < 0)
933                 return OTG_STATE_UNDEFINED;
934
935         /* Higher priority first */
936         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
937                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
938                 return OTG_STATE_UNDEFINED;
939
940         } else if (otg_events & OEVT_A_DEV_SESS_END_DET_EVNT) {
941                 otg_dbg(otg, "OEVT_A_DEV_SESS_END_DET_EVNT\n");
942                 return OTG_STATE_A_WAIT_VFALL;
943
944         } else if (rc == 0) {
945                 if (otg_read(otg, OCTL) & OCTL_PRT_PWR_CTL)
946                         return OTG_STATE_A_WAIT_BCON;
947                 else
948                         return OTG_STATE_A_WAIT_VFALL;
949         }
950
951         /* Invalid state */
952         return OTG_STATE_UNDEFINED;
953 }
954
955 static enum usb_otg_state do_a_idle(struct dwc3_otg *otg)
956 {
957         int rc;
958         u32 otg_mask;
959         u32 user_mask;
960         u32 otg_events = 0;
961         u32 user_events = 0;
962
963         otg_dbg(otg, "");
964
965         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT | OEVT_A_DEV_SRP_DET_EVNT;
966         user_mask = USER_SRP_EVENT;
967
968         rc = sleep_until_event(otg,
969                         otg_mask, user_mask,
970                         &otg_events, &user_events,
971                         0);
972
973         if (rc < 0)
974                 return OTG_STATE_UNDEFINED;
975
976         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
977                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
978                 return OTG_STATE_UNDEFINED;
979         } else if (otg_events & OEVT_A_DEV_SRP_DET_EVNT) {
980                 otg_dbg(otg, "OEVT_A_DEV_SRP_DET_EVNT\n");
981                 return OTG_STATE_A_WAIT_VRISE;
982         } else if (user_events & USER_SRP_EVENT) {
983                 otg_dbg(otg, "User initiated VBUS\n");
984                 return OTG_STATE_A_WAIT_VRISE;
985         }
986
987         return OTG_STATE_UNDEFINED;
988 }
989
990 static enum usb_otg_state do_a_peripheral(struct dwc3_otg *otg)
991 {
992         int rc;
993         u32 otg_mask;
994         u32 otg_events = 0;
995
996         otg_dbg(otg, "");
997         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
998                 OEVT_A_DEV_SESS_END_DET_EVNT |
999                 OEVT_A_DEV_B_DEV_HOST_END_EVNT;
1000
1001         rc = sleep_until_event(otg,
1002                         otg_mask, 0,
1003                         &otg_events, NULL, 0);
1004         if (rc < 0)
1005                 return OTG_STATE_UNDEFINED;
1006
1007         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1008                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1009                 return OTG_STATE_UNDEFINED;
1010
1011         } else if (otg_events & OEVT_A_DEV_SESS_END_DET_EVNT) {
1012                 otg_dbg(otg, "OEVT_A_DEV_SESS_END_DET_EVNT\n");
1013                 return OTG_STATE_A_WAIT_VFALL;
1014
1015         } else if (otg_events & OEVT_A_DEV_B_DEV_HOST_END_EVNT) {
1016                 otg_dbg(otg, "OEVT_A_DEV_B_DEV_HOST_END_EVNT\n");
1017                 return OTG_STATE_A_WAIT_VRISE;
1018         }
1019
1020         return OTG_STATE_UNDEFINED;
1021 }
1022
1023 #define HNP_TIMEOUT     4000
1024
1025 static enum usb_otg_state do_b_hnp_init(struct dwc3_otg *otg)
1026 {
1027         int rc;
1028         u32 otg_mask;
1029         u32 events = 0;
1030
1031         otg_dbg(otg, "");
1032         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1033                 OEVT_B_DEV_HNP_CHNG_EVNT |
1034                 OEVT_B_DEV_VBUS_CHNG_EVNT;
1035
1036         start_b_hnp(otg);
1037         rc = HNP_TIMEOUT;
1038
1039 again:
1040         rc = sleep_until_event(otg,
1041                         otg_mask, 0,
1042                         &events, NULL, rc);
1043         stop_b_hnp(otg);
1044
1045         if (rc < 0)
1046                 return OTG_STATE_UNDEFINED;
1047
1048         if (events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1049                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1050                 return OTG_STATE_UNDEFINED;
1051         } else if (events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1052                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1053                 return OTG_STATE_B_IDLE;
1054         } else if (events & OEVT_B_DEV_HNP_CHNG_EVNT) {
1055                 otg_dbg(otg, "OEVT_B_DEV_HNP_CHNG_EVNT\n");
1056                 if (events & OEVT_HST_NEG_SCS) {
1057                         otg_dbg(otg, "B-HNP Success\n");
1058                         return OTG_STATE_B_WAIT_ACON;
1059
1060                 } else {
1061                         otg_err(otg, "B-HNP Failed\n");
1062                         return OTG_STATE_B_PERIPHERAL;
1063                 }
1064         } else if (rc == 0) {
1065                 /* Timeout */
1066                 otg_err(otg, "HNP timed out!\n");
1067                 return OTG_STATE_B_PERIPHERAL;
1068
1069         } else {
1070                 goto again;
1071         }
1072
1073         return OTG_STATE_UNDEFINED;
1074 }
1075
1076 static enum usb_otg_state do_b_peripheral(struct dwc3_otg *otg)
1077 {
1078         int rc;
1079         u32 otg_mask;
1080         u32 user_mask;
1081         u32 otg_events = 0;
1082         u32 user_events = 0;
1083
1084         otg_dbg(otg, "");
1085         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT | OEVT_B_DEV_VBUS_CHNG_EVNT;
1086         user_mask = USER_HNP_EVENT | USER_END_SESSION |
1087                 USER_SRP_EVENT | INITIAL_SRP;
1088
1089 again:
1090         rc = sleep_until_event(otg,
1091                         otg_mask, user_mask,
1092                         &otg_events, &user_events, 0);
1093         if (rc < 0)
1094                 return OTG_STATE_UNDEFINED;
1095
1096         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1097                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1098                 return OTG_STATE_UNDEFINED;
1099         } else if (otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1100                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1101
1102                 if (otg_events & OEVT_B_SES_VLD_EVT) {
1103                         otg_dbg(otg, "Session valid\n");
1104                         goto again;
1105                 } else {
1106                         otg_dbg(otg, "Session not valid\n");
1107                         return OTG_STATE_B_IDLE;
1108                 }
1109
1110         } else if (user_events & USER_HNP_EVENT) {
1111                 otg_dbg(otg, "USER_HNP_EVENT\n");
1112                 return do_b_hnp_init(otg);
1113         } else if (user_events & USER_END_SESSION) {
1114                 otg_dbg(otg, "USER_END_SESSION\n");
1115                 return OTG_STATE_B_IDLE;
1116         }
1117
1118         return OTG_STATE_UNDEFINED;
1119 }
1120
1121 static enum usb_otg_state do_b_wait_acon(struct dwc3_otg *otg)
1122 {
1123         int rc;
1124         u32 otg_mask;
1125         u32 user_mask = 0;
1126         u32 otg_events = 0;
1127         u32 user_events = 0;
1128         struct usb_hcd *hcd;
1129         struct xhci_hcd *xhci;
1130
1131         otg_dbg(otg, "");
1132         set_b_host(otg, 1);
1133         start_host(otg);
1134         otg_mask = OEVT_B_DEV_B_HOST_END_EVNT;
1135         otg_write(otg, OEVTEN, otg_mask);
1136         reset_port(otg);
1137
1138         hcd = container_of(otg->otg.host, struct usb_hcd, self);
1139         xhci = hcd_to_xhci(hcd);
1140         usb_kick_hub_wq(hcd->self.root_hub);
1141         if (xhci->shared_hcd)
1142                 usb_kick_hub_wq(xhci->shared_hcd->self.root_hub);
1143
1144         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1145                 OEVT_B_DEV_B_HOST_END_EVNT |
1146                 OEVT_B_DEV_VBUS_CHNG_EVNT |
1147                 OEVT_HOST_ROLE_REQ_INIT_EVNT;
1148         user_mask = USER_A_CONN_EVENT;
1149
1150 again:
1151         rc = sleep_until_event(otg,
1152                         otg_mask, user_mask,
1153                         &otg_events, &user_events, 0);
1154         if (rc < 0)
1155                 return OTG_STATE_UNDEFINED;
1156
1157         /* Higher priority first */
1158         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1159                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1160                 return OTG_STATE_UNDEFINED;
1161         } else if (otg_events & OEVT_B_DEV_B_HOST_END_EVNT) {
1162                 otg_dbg(otg, "OEVT_B_DEV_B_HOST_END_EVNT\n");
1163                 return OTG_STATE_B_PERIPHERAL;
1164         } else if (otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1165                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1166                 if (otg_events & OEVT_B_SES_VLD_EVT) {
1167                         otg_dbg(otg, "Session valid\n");
1168                         goto again;
1169                 } else {
1170                         otg_dbg(otg, "Session not valid\n");
1171                         return OTG_STATE_B_IDLE;
1172                 }
1173         } else if (user_events & USER_A_CONN_EVENT) {
1174                 otg_dbg(otg, "A-device connected\n");
1175                 return OTG_STATE_B_HOST;
1176         }
1177
1178         /* Invalid state */
1179         return OTG_STATE_UNDEFINED;
1180 }
1181
1182 static enum usb_otg_state do_b_host(struct dwc3_otg *otg)
1183 {
1184         int rc;
1185         u32 otg_mask;
1186         u32 user_mask = 0;
1187         u32 otg_events = 0;
1188         u32 user_events = 0;
1189
1190         otg_dbg(otg, "");
1191
1192         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1193                 OEVT_B_DEV_B_HOST_END_EVNT |
1194                 OEVT_B_DEV_VBUS_CHNG_EVNT |
1195                 OEVT_HOST_ROLE_REQ_INIT_EVNT;
1196
1197 again:
1198         rc = sleep_until_event(otg,
1199                         otg_mask, user_mask,
1200                         &otg_events, &user_events, 0);
1201         if (rc < 0)
1202                 return OTG_STATE_UNDEFINED;
1203
1204         /* Higher priority first */
1205         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1206                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1207                 return OTG_STATE_UNDEFINED;
1208         } else if (otg_events & OEVT_B_DEV_B_HOST_END_EVNT) {
1209                 otg_dbg(otg, "OEVT_B_DEV_B_HOST_END_EVNT\n");
1210                 return OTG_STATE_B_PERIPHERAL;
1211         } else if (otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1212                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1213                 if (otg_events & OEVT_B_SES_VLD_EVT) {
1214                         otg_dbg(otg, "Session valid\n");
1215                         goto again;
1216                 } else {
1217                         otg_dbg(otg, "Session not valid\n");
1218                         return OTG_STATE_B_IDLE;
1219                 }
1220         }
1221
1222         /* Invalid state */
1223         return OTG_STATE_UNDEFINED;
1224 }
1225
1226 static enum usb_otg_state do_b_idle(struct dwc3_otg *otg)
1227 {
1228         int rc;
1229         u32 otg_mask;
1230         u32 user_mask;
1231         u32 otg_events = 0;
1232         u32 user_events = 0;
1233
1234         otg_dbg(otg, "");
1235
1236         if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
1237                 otg_err(otg, "Failed to set peripheral mode\n");
1238
1239         dwc3_otg_setup_event_buffers(otg);
1240
1241         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1242                 OEVT_B_DEV_SES_VLD_DET_EVNT |
1243                 OEVT_B_DEV_VBUS_CHNG_EVNT;
1244         user_mask = USER_SRP_EVENT;
1245
1246 again:
1247         rc = sleep_until_event(otg,
1248                         otg_mask, user_mask,
1249                         &otg_events, &user_events, 0);
1250
1251         if (rc < 0)
1252                 return OTG_STATE_UNDEFINED;
1253
1254         if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1255                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1256                 return OTG_STATE_UNDEFINED;
1257         } else if ((otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) ||
1258                 (otg_events & OEVT_B_DEV_SES_VLD_DET_EVNT)) {
1259                 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1260                 if (otg_events & OEVT_B_SES_VLD_EVT) {
1261                         otg_dbg(otg, "Session valid\n");
1262                         return OTG_STATE_B_PERIPHERAL;
1263
1264                 } else {
1265                         otg_dbg(otg, "Session not valid\n");
1266                         goto again;
1267                 }
1268         } else if (user_events & USER_SRP_EVENT) {
1269                 otg_dbg(otg, "USER_SRP_EVENT\n");
1270                 return OTG_STATE_B_SRP_INIT;
1271         }
1272
1273         return OTG_STATE_UNDEFINED;
1274 }
1275
1276 static enum usb_otg_state do_b_srp_init(struct dwc3_otg *otg)
1277 {
1278         int rc;
1279         u32 otg_mask;
1280         u32 events = 0;
1281
1282         otg_dbg(otg, "");
1283         otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1284                 OEVT_B_DEV_SES_VLD_DET_EVNT |
1285                 OEVT_B_DEV_VBUS_CHNG_EVNT;
1286
1287         otg_write(otg, OEVTEN, otg_mask);
1288         start_srp(otg);
1289
1290         rc = SRP_TIMEOUT;
1291
1292 again:
1293         rc = sleep_until_event(otg,
1294                         otg_mask, 0,
1295                         &events, NULL, rc);
1296         if (rc < 0)
1297                 return OTG_STATE_UNDEFINED;
1298
1299         if (events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1300                 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1301                 return OTG_STATE_UNDEFINED;
1302         } else if (events & OEVT_B_DEV_SES_VLD_DET_EVNT) {
1303                 otg_dbg(otg, "OEVT_B_DEV_SES_VLD_DET_EVNT\n");
1304                 return OTG_STATE_B_PERIPHERAL;
1305         } else if (rc == 0) {
1306                 otg_dbg(otg, "SRP Timeout (rc=%d)\n", rc);
1307                 otg_info(otg, "DEVICE NO RESPONSE FOR SRP\n");
1308                 return OTG_STATE_B_IDLE;
1309
1310         } else {
1311                 goto again;
1312         }
1313
1314         return OTG_STATE_UNDEFINED;
1315 }
1316
1317 int otg_main_thread(void *data)
1318 {
1319         struct dwc3_otg *otg = (struct dwc3_otg *)data;
1320         enum usb_otg_state prev = OTG_STATE_UNDEFINED;
1321
1322 #ifdef VERBOSE_DEBUG
1323         u32 snpsid = otg_read(otg, 0xc120);
1324
1325         otg_vdbg(otg, "io_priv=%p\n", otg->regs);
1326         otg_vdbg(otg, "c120: %x\n", snpsid);
1327 #endif
1328
1329         /* Allow the thread to be killed by a signal, but set the signal mask
1330          * to block everything but INT, TERM, KILL, and USR1.
1331          */
1332         allow_signal(SIGINT);
1333         allow_signal(SIGTERM);
1334         allow_signal(SIGKILL);
1335         allow_signal(SIGUSR1);
1336
1337         /* Allow the thread to be frozen */
1338         set_freezable();
1339
1340         /* Allow host/peripheral driver load to finish */
1341         msleep(100);
1342
1343         reset_hw(otg);
1344
1345         stop_host(otg);
1346         stop_peripheral(otg);
1347
1348         otg_dbg(otg, "Thread running\n");
1349         while (1) {
1350                 enum usb_otg_state next = OTG_STATE_UNDEFINED;
1351
1352                 otg_vdbg(otg, "Main thread entering state\n");
1353
1354                 switch (otg->otg.state) {
1355                 case OTG_STATE_UNDEFINED:
1356                         otg_dbg(otg, "OTG_STATE_UNDEFINED\n");
1357                         next = do_connector_id_status(otg);
1358                         break;
1359
1360                 case OTG_STATE_A_IDLE:
1361                         otg_dbg(otg, "OTG_STATE_A_IDLE\n");
1362                         stop_peripheral(otg);
1363
1364                         if (prev == OTG_STATE_UNDEFINED)
1365                                 next = OTG_STATE_A_WAIT_VRISE;
1366                         else
1367                                 next = do_a_idle(otg);
1368                         break;
1369
1370                 case OTG_STATE_A_WAIT_VRISE:
1371                         otg_dbg(otg, "OTG_STATE_A_WAIT_VRISE\n");
1372                         next = do_a_wait_vrise(otg);
1373                         break;
1374
1375                 case OTG_STATE_A_WAIT_BCON:
1376                         otg_dbg(otg, "OTG_STATE_A_WAIT_BCON\n");
1377                         next = do_a_wait_bconn(otg);
1378                         break;
1379
1380                 case OTG_STATE_A_HOST:
1381                         otg_dbg(otg, "OTG_STATE_A_HOST\n");
1382                         stop_peripheral(otg);
1383                         next = do_a_host(otg);
1384                         /* Don't stop the host here if we are going into
1385                          * A_SUSPEND. We need to delay that until later. It
1386                          * will be stopped when coming out of A_SUSPEND
1387                          * state.
1388                          */
1389                         if (next != OTG_STATE_A_SUSPEND)
1390                                 stop_host(otg);
1391                         break;
1392
1393                 case OTG_STATE_A_SUSPEND:
1394                         otg_dbg(otg, "OTG_STATE_A_SUSPEND\n");
1395                         next = do_a_hnp_init(otg);
1396
1397                         /* Stop the host. */
1398                         stop_host(otg);
1399                         break;
1400
1401                 case OTG_STATE_A_WAIT_VFALL:
1402                         otg_dbg(otg, "OTG_STATE_A_WAIT_VFALL\n");
1403                         next = do_a_wait_vfall(otg);
1404                         stop_host(otg);
1405                         break;
1406
1407                 case OTG_STATE_A_PERIPHERAL:
1408                         otg_dbg(otg, "OTG_STATE_A_PERIPHERAL\n");
1409                         stop_host(otg);
1410                         start_peripheral(otg);
1411                         next = do_a_peripheral(otg);
1412                         stop_peripheral(otg);
1413                         break;
1414
1415                 case OTG_STATE_B_IDLE:
1416                         otg_dbg(otg, "OTG_STATE_B_IDLE\n");
1417                         next = do_b_idle(otg);
1418                         break;
1419
1420                 case OTG_STATE_B_PERIPHERAL:
1421                         otg_dbg(otg, "OTG_STATE_B_PERIPHERAL\n");
1422                         stop_host(otg);
1423                         start_peripheral(otg);
1424                         next = do_b_peripheral(otg);
1425                         stop_peripheral(otg);
1426                         break;
1427
1428                 case OTG_STATE_B_SRP_INIT:
1429                         otg_dbg(otg, "OTG_STATE_B_SRP_INIT\n");
1430                         otg_read(otg, OSTS);
1431                         next = do_b_srp_init(otg);
1432                         break;
1433
1434                 case OTG_STATE_B_WAIT_ACON:
1435                         otg_dbg(otg, "OTG_STATE_B_WAIT_ACON\n");
1436                         next = do_b_wait_acon(otg);
1437                         break;
1438
1439                 case OTG_STATE_B_HOST:
1440                         otg_dbg(otg, "OTG_STATE_B_HOST\n");
1441                         next = do_b_host(otg);
1442                         stop_host(otg);
1443                         break;
1444
1445                 default:
1446                         otg_err(otg, "Unknown state %d, sleeping...\n",
1447                                         otg->state);
1448                         sleep_main_thread(otg);
1449                         break;
1450                 }
1451
1452                 prev = otg->otg.state;
1453                 otg->otg.state = next;
1454                 if (kthread_should_stop())
1455                         break;
1456         }
1457
1458         otg->main_thread = NULL;
1459         otg_dbg(otg, "OTG main thread exiting....\n");
1460
1461         return 0;
1462 }
1463
1464 static void start_main_thread(struct dwc3_otg *otg)
1465 {
1466         if (!otg->main_thread && otg->otg.gadget && otg->otg.host) {
1467                 otg_dbg(otg, "Starting OTG main thread\n");
1468                 otg->main_thread = kthread_create(otg_main_thread, otg, "otg");
1469                 wake_up_process(otg->main_thread);
1470         }
1471 }
1472
1473 static inline struct dwc3_otg *otg_to_dwc3_otg(struct usb_otg *x)
1474 {
1475         return container_of(x, struct dwc3_otg, otg);
1476 }
1477
1478 static irqreturn_t dwc3_otg_irq(int irq, void *_otg)
1479 {
1480         struct dwc3_otg *otg;
1481         u32 oevt;
1482         u32 osts;
1483         u32 octl;
1484         u32 ocfg;
1485         u32 oevten;
1486         u32 otg_mask = OEVT_ALL;
1487
1488         if (!_otg)
1489                 return 0;
1490
1491         otg = (struct dwc3_otg *)_otg;
1492
1493         oevt = otg_read(otg, OEVT);
1494         osts = otg_read(otg, OSTS);
1495         octl = otg_read(otg, OCTL);
1496         ocfg = otg_read(otg, OCFG);
1497         oevten = otg_read(otg, OEVTEN);
1498
1499         /* Clear handled events */
1500         otg_write(otg, OEVT, oevt);
1501
1502         otg_vdbg(otg, "\n");
1503         otg_vdbg(otg, "    oevt = %08x\n", oevt);
1504         otg_vdbg(otg, "    osts = %08x\n", osts);
1505         otg_vdbg(otg, "    octl = %08x\n", octl);
1506         otg_vdbg(otg, "    ocfg = %08x\n", ocfg);
1507         otg_vdbg(otg, "  oevten = %08x\n", oevten);
1508
1509         otg_vdbg(otg, "oevt[DeviceMode] = %s\n",
1510                         oevt & OEVT_DEV_MOD_EVNT ? "Device" : "Host");
1511
1512         if (oevt & OEVT_CONN_ID_STS_CHNG_EVNT)
1513                 otg_dbg(otg, "Connector ID Status Change Event\n");
1514         if (oevt & OEVT_HOST_ROLE_REQ_INIT_EVNT)
1515                 otg_dbg(otg, "Host Role Request Init Notification Event\n");
1516         if (oevt & OEVT_HOST_ROLE_REQ_CONFIRM_EVNT)
1517                 otg_dbg(otg, "Host Role Request Confirm Notification Event\n");
1518         if (oevt & OEVT_A_DEV_B_DEV_HOST_END_EVNT)
1519                 otg_dbg(otg, "A-Device B-Host End Event\n");
1520         if (oevt & OEVT_A_DEV_HOST_EVNT)
1521                 otg_dbg(otg, "A-Device Host Event\n");
1522         if (oevt & OEVT_A_DEV_HNP_CHNG_EVNT)
1523                 otg_dbg(otg, "A-Device HNP Change Event\n");
1524         if (oevt & OEVT_A_DEV_SRP_DET_EVNT)
1525                 otg_dbg(otg, "A-Device SRP Detect Event\n");
1526         if (oevt & OEVT_A_DEV_SESS_END_DET_EVNT)
1527                 otg_dbg(otg, "A-Device Session End Detected Event\n");
1528         if (oevt & OEVT_B_DEV_B_HOST_END_EVNT)
1529                 otg_dbg(otg, "B-Device B-Host End Event\n");
1530         if (oevt & OEVT_B_DEV_HNP_CHNG_EVNT)
1531                 otg_dbg(otg, "B-Device HNP Change Event\n");
1532         if (oevt & OEVT_B_DEV_SES_VLD_DET_EVNT)
1533                 otg_dbg(otg, "B-Device Session Valid Detect Event\n");
1534         if (oevt & OEVT_B_DEV_VBUS_CHNG_EVNT)
1535                 otg_dbg(otg, "B-Device VBUS Change Event\n");
1536
1537         if (oevt & otg_mask) {
1538                 /* Pass event to main thread */
1539                 spin_lock(&otg->lock);
1540                 otg->otg_events |= oevt;
1541                 wakeup_main_thread(otg);
1542                 spin_unlock(&otg->lock);
1543                 return 1;
1544         }
1545
1546         return IRQ_HANDLED;
1547 }
1548
1549 static void hnp_polling_work(struct work_struct *w)
1550 {
1551         struct dwc3_otg *otg = container_of(w, struct dwc3_otg,
1552                         hp_work.work);
1553         struct usb_bus *bus;
1554         struct usb_device *udev;
1555         struct usb_hcd *hcd;
1556         u8 *otgstatus;
1557         int ret;
1558         int err;
1559
1560         hcd = container_of(otg->otg.host, struct usb_hcd, self);
1561         if (!hcd)
1562                 return;
1563
1564         bus = &hcd->self;
1565         if (!bus->otg_port)
1566                 return;
1567
1568         udev = usb_hub_find_child(bus->root_hub, bus->otg_port);
1569         if (!udev)
1570                 return;
1571
1572         otgstatus = kmalloc(sizeof(*otgstatus), GFP_NOIO);
1573         if (!otgstatus)
1574                 return;
1575
1576         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1577                         USB_REQ_GET_STATUS, USB_DIR_IN | USB_RECIP_DEVICE,
1578                         0, 0xf000, otgstatus, sizeof(*otgstatus),
1579                         USB_CTRL_GET_TIMEOUT);
1580
1581         if (ret == sizeof(*otgstatus) && (*otgstatus & 0x1)) {
1582                 /* enable HNP before suspend, it's simpler */
1583
1584                 udev->bus->b_hnp_enable = 1;
1585                 err = usb_control_msg(udev,
1586                                 usb_sndctrlpipe(udev, 0),
1587                                 USB_REQ_SET_FEATURE, 0,
1588                                 udev->bus->b_hnp_enable
1589                                 ? USB_DEVICE_B_HNP_ENABLE
1590                                 : USB_DEVICE_A_ALT_HNP_SUPPORT,
1591                                 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1592
1593                 if (err < 0) {
1594                         /* OTG MESSAGE: report errors here,
1595                          * customize to match your product.
1596                          */
1597                         otg_info(otg, "ERROR : Device no response\n");
1598                         dev_info(&udev->dev, "can't set HNP mode: %d\n",
1599                                         err);
1600                         udev->bus->b_hnp_enable = 0;
1601                         if (le16_to_cpu(udev->descriptor.idVendor) == 0x1a0a) {
1602                                 if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND)
1603                                                 < 0)
1604                                         dev_dbg(&udev->dev, "HNP fail, %d\n",
1605                                                         err);
1606                         }
1607                 } else {
1608                         /* Device wants role-switch, suspend the bus. */
1609                         static struct usb_phy *phy;
1610
1611                         phy = usb_get_phy(USB_PHY_TYPE_USB3);
1612                         otg_start_hnp(phy->otg);
1613                         usb_put_phy(phy);
1614
1615                         if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND) < 0)
1616                                 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1617                 }
1618         } else if (ret < 0) {
1619                 udev->bus->b_hnp_enable = 1;
1620                 err = usb_control_msg(udev,
1621                                 usb_sndctrlpipe(udev, 0),
1622                                 USB_REQ_SET_FEATURE, 0,
1623                                 USB_DEVICE_B_HNP_ENABLE,
1624                                 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1625                 if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND) < 0)
1626                         dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1627         } else {
1628                 schedule_delayed_work(&otg->hp_work, 1 * HZ);
1629         }
1630
1631         kfree(otgstatus);
1632 }
1633
1634 static int dwc3_otg_notify_connect(struct usb_phy *phy,
1635                 enum usb_device_speed speed)
1636 {
1637         struct usb_bus *bus;
1638         struct usb_device *udev;
1639         struct usb_hcd *hcd;
1640         struct dwc3_otg *otg;
1641         int err = 0;
1642
1643         otg = otg_to_dwc3_otg(phy->otg);
1644
1645         hcd = container_of(phy->otg->host, struct usb_hcd, self);
1646         if (!hcd)
1647                 return -EINVAL;
1648
1649         bus = &hcd->self;
1650         if (!bus->otg_port)
1651                 return 0;
1652
1653         udev = usb_hub_find_child(bus->root_hub, bus->otg_port);
1654         if (!udev)
1655                 return 0;
1656
1657         /*
1658          * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1659          * to wake us after we've powered off VBUS; and HNP, switching roles
1660          * "host" to "peripheral".  The OTG descriptor helps figure this out.
1661          */
1662         if (udev->config && udev->parent == udev->bus->root_hub) {
1663                 struct usb_otg20_descriptor     *desc = NULL;
1664
1665                 /* descriptor may appear anywhere in config */
1666                 err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
1667                                 le16_to_cpu(udev->config[0].desc.wTotalLength),
1668                                 USB_DT_OTG, (void **) &desc);
1669                 if (err || !(desc->bmAttributes & USB_OTG_HNP))
1670                         return 0;
1671
1672                 if (udev->portnum == udev->bus->otg_port) {
1673                         INIT_DELAYED_WORK(&otg->hp_work,
1674                                         hnp_polling_work);
1675                         schedule_delayed_work(&otg->hp_work, HZ);
1676                 }
1677
1678         }
1679
1680         return err;
1681 }
1682
1683 static int dwc3_otg_notify_disconnect(struct usb_phy *phy,
1684                 enum usb_device_speed speed)
1685 {
1686         struct dwc3_otg *otg;
1687
1688         otg = otg_to_dwc3_otg(phy->otg);
1689
1690         if (work_pending(&otg->hp_work.work)) {
1691                 while (!cancel_delayed_work(&otg->hp_work))
1692                         msleep(20);
1693         }
1694         return 0;
1695 }
1696
1697 static void dwc3_otg_set_peripheral(struct usb_otg *_otg, int yes)
1698 {
1699         struct dwc3_otg *otg;
1700
1701         if (!_otg)
1702                 return;
1703
1704         otg = otg_to_dwc3_otg(_otg);
1705         otg_dbg(otg, "\n");
1706
1707         if (yes) {
1708                 if (otg->hwparams6 == 0xdeadbeef)
1709                         otg->hwparams6 = otg_read(otg, GHWPARAMS6);
1710                 stop_host(otg);
1711         } else {
1712                 stop_peripheral(otg);
1713         }
1714
1715         set_peri_mode(otg, yes);
1716 }
1717 EXPORT_SYMBOL(dwc3_otg_set_peripheral);
1718
1719 static int dwc3_otg_set_periph(struct usb_otg *_otg, struct usb_gadget *gadget)
1720 {
1721         struct dwc3_otg *otg;
1722
1723         if (!_otg)
1724                 return -ENODEV;
1725
1726         otg = otg_to_dwc3_otg(_otg);
1727         otg_dbg(otg, "\n");
1728
1729         if ((long)gadget == 1) {
1730                 dwc3_otg_set_peripheral(_otg, 1);
1731                 return 0;
1732         }
1733
1734         if (!gadget) {
1735                 otg->otg.gadget = NULL;
1736                 return -ENODEV;
1737         }
1738
1739         otg->otg.gadget = gadget;
1740         otg->otg.gadget->hnp_polling_support = 1;
1741         otg->otg.state = OTG_STATE_B_IDLE;
1742
1743         start_main_thread(otg);
1744         return 0;
1745 }
1746
1747 static int dwc3_otg_set_host(struct usb_otg *_otg, struct usb_bus *host)
1748 {
1749         struct dwc3_otg *otg;
1750         struct usb_hcd *hcd;
1751         struct xhci_hcd *xhci;
1752
1753         if (!_otg)
1754                 return -ENODEV;
1755
1756         otg = otg_to_dwc3_otg(_otg);
1757         otg_dbg(otg, "\n");
1758
1759         if ((long)host == 1) {
1760                 dwc3_otg_set_peripheral(_otg, 0);
1761                 return 0;
1762         }
1763
1764         if (!host) {
1765                 otg->otg.host = NULL;
1766                 otg->hcd_irq = 0;
1767                 return -ENODEV;
1768         }
1769
1770         hcd = container_of(host, struct usb_hcd, self);
1771         xhci = hcd_to_xhci(hcd);
1772         otg_dbg(otg, "hcd=%p xhci=%p\n", hcd, xhci);
1773
1774         hcd->self.otg_port = 1;
1775         if (xhci->shared_hcd) {
1776                 xhci->shared_hcd->self.otg_port = 1;
1777                 otg_dbg(otg, "shared_hcd=%p\n", xhci->shared_hcd);
1778         }
1779
1780         otg->otg.host = host;
1781         otg->hcd_irq = hcd->irq;
1782         otg_dbg(otg, "host=%p irq=%d\n", otg->otg.host, otg->hcd_irq);
1783
1784
1785         otg->host_started = 1;
1786         otg->dev_enum = 0;
1787         start_main_thread(otg);
1788         return 0;
1789 }
1790
1791 static int dwc3_otg_start_srp(struct usb_otg *x)
1792 {
1793         unsigned long flags;
1794         struct dwc3_otg *otg;
1795
1796         if (!x)
1797                 return -ENODEV;
1798
1799         otg = otg_to_dwc3_otg(x);
1800         otg_dbg(otg, "\n");
1801
1802         if (!otg->otg.host || !otg->otg.gadget)
1803                 return -ENODEV;
1804
1805         spin_lock_irqsave(&otg->lock, flags);
1806         otg->user_events |= USER_SRP_EVENT;
1807         wakeup_main_thread(otg);
1808         spin_unlock_irqrestore(&otg->lock, flags);
1809         return 0;
1810 }
1811
1812 static int dwc3_otg_start_hnp(struct usb_otg *x)
1813 {
1814         unsigned long flags;
1815         struct dwc3_otg *otg;
1816
1817         if (!x)
1818                 return -ENODEV;
1819
1820         otg = otg_to_dwc3_otg(x);
1821         otg_dbg(otg, "\n");
1822
1823         if (!otg->otg.host || !otg->otg.gadget)
1824                 return -ENODEV;
1825
1826         spin_lock_irqsave(&otg->lock, flags);
1827         otg->user_events |= USER_HNP_EVENT;
1828         wakeup_main_thread(otg);
1829         spin_unlock_irqrestore(&otg->lock, flags);
1830         return 0;
1831 }
1832
1833 static int dwc3_otg_end_session(struct usb_otg *x)
1834 {
1835         unsigned long flags;
1836         struct dwc3_otg *otg;
1837
1838         if (!x)
1839                 return -ENODEV;
1840
1841         otg = otg_to_dwc3_otg(x);
1842         otg_dbg(otg, "\n");
1843
1844         if (!otg->otg.host || !otg->otg.gadget)
1845                 return -ENODEV;
1846
1847         spin_lock_irqsave(&otg->lock, flags);
1848         otg->user_events |= USER_END_SESSION;
1849         wakeup_main_thread(otg);
1850         spin_unlock_irqrestore(&otg->lock, flags);
1851         return 0;
1852 }
1853
1854 static int otg_end_session(struct usb_otg *otg)
1855 {
1856         return dwc3_otg_end_session(otg);
1857 }
1858 EXPORT_SYMBOL(otg_end_session);
1859
1860 static int dwc3_otg_received_host_release(struct usb_otg *x)
1861 {
1862         struct dwc3_otg *otg;
1863         unsigned long flags;
1864
1865         if (!x)
1866                 return -ENODEV;
1867
1868         otg = otg_to_dwc3_otg(x);
1869         otg_dbg(otg, "\n");
1870
1871         if (!otg->otg.host || !otg->otg.gadget)
1872                 return -ENODEV;
1873
1874         spin_lock_irqsave(&otg->lock, flags);
1875         otg->user_events |= PCD_RECEIVED_HOST_RELEASE_EVENT;
1876         wakeup_main_thread(otg);
1877         spin_unlock_irqrestore(&otg->lock, flags);
1878         return 0;
1879 }
1880
1881 int otg_host_release(struct usb_otg *otg)
1882 {
1883         return dwc3_otg_received_host_release(otg);
1884 }
1885 EXPORT_SYMBOL(otg_host_release);
1886
1887 static void dwc3_otg_enable_irq(struct dwc3_otg *otg)
1888 {
1889         u32                     reg;
1890
1891         /* Enable OTG IRQs */
1892         reg = OEVT_ALL;
1893
1894         otg_write(otg, OEVTEN, reg);
1895 }
1896
1897 static ssize_t store_srp(struct device *dev, struct device_attribute *attr,
1898                          const char *buf, size_t count)
1899 {
1900         struct usb_phy *phy;
1901         struct usb_otg *otg;
1902
1903         phy = usb_get_phy(USB_PHY_TYPE_USB3);
1904         if (IS_ERR(phy) || !phy) {
1905                 if (!IS_ERR(phy))
1906                         usb_put_phy(phy);
1907                 return count;
1908         }
1909
1910         otg = phy->otg;
1911         if (!otg) {
1912                 usb_put_phy(phy);
1913                 return count;
1914         }
1915
1916         otg_start_srp(otg);
1917         usb_put_phy(phy);
1918         return count;
1919 }
1920 static DEVICE_ATTR(srp, 0220, NULL, store_srp);
1921
1922 static ssize_t store_end(struct device *dev, struct device_attribute *attr,
1923                          const char *buf, size_t count)
1924 {
1925         struct usb_phy *phy;
1926         struct usb_otg *otg;
1927
1928         phy = usb_get_phy(USB_PHY_TYPE_USB3);
1929         if (IS_ERR(phy) || !phy) {
1930                 if (!IS_ERR(phy))
1931                         usb_put_phy(phy);
1932                 return count;
1933         }
1934
1935         otg = phy->otg;
1936         if (!otg) {
1937                 usb_put_phy(phy);
1938                 return count;
1939         }
1940
1941         otg_end_session(otg);
1942         usb_put_phy(phy);
1943         return count;
1944 }
1945 static DEVICE_ATTR(end, 0220, NULL, store_end);
1946
1947 static ssize_t store_hnp(struct device *dev, struct device_attribute *attr,
1948                          const char *buf, size_t count)
1949 {
1950         struct dwc3 *dwc = dev_get_drvdata(dev);
1951         struct usb_phy *phy = usb_get_phy(USB_PHY_TYPE_USB3);
1952         struct usb_otg *otg;
1953
1954         dev_dbg(dwc->dev, "%s()\n", __func__);
1955
1956         if (IS_ERR(phy) || !phy) {
1957                 dev_info(dwc->dev, "NO PHY!!\n");
1958                 if (!IS_ERR(phy))
1959                         usb_put_phy(phy);
1960                 return count;
1961         }
1962
1963         otg = phy->otg;
1964         if (!otg) {
1965                 dev_info(dwc->dev, "NO OTG!!\n");
1966                 usb_put_phy(phy);
1967                 return count;
1968         }
1969
1970         dev_info(dev, "b_hnp_enable is FALSE\n");
1971         dwc->gadget.host_request_flag = 1;
1972
1973         usb_put_phy(phy);
1974         return count;
1975 }
1976 static DEVICE_ATTR(hnp, 0220, NULL, store_hnp);
1977
1978 static ssize_t store_a_hnp_reqd(struct device *dev,
1979                 struct device_attribute *attr, const char *buf, size_t count)
1980 {
1981         struct dwc3 *dwc = dev_get_drvdata(dev);
1982         struct dwc3_otg *otg;
1983
1984         otg = dwc->otg;
1985         host_release(otg);
1986         return count;
1987 }
1988 static DEVICE_ATTR(a_hnp_reqd, 0220, NULL, store_a_hnp_reqd);
1989
1990 static ssize_t store_print_dbg(struct device *dev,
1991                 struct device_attribute *attr, const char *buf, size_t count)
1992 {
1993         struct dwc3 *dwc = dev_get_drvdata(dev);
1994         struct dwc3_otg *otg;
1995
1996         otg = dwc->otg;
1997         print_debug_regs(otg);
1998
1999         return count;
2000 }
2001 static DEVICE_ATTR(print_dbg, 0220, NULL, store_print_dbg);
2002
2003 void dwc_usb3_remove_dev_files(struct device *dev)
2004 {
2005         device_remove_file(dev, &dev_attr_print_dbg);
2006         device_remove_file(dev, &dev_attr_a_hnp_reqd);
2007         device_remove_file(dev, &dev_attr_end);
2008         device_remove_file(dev, &dev_attr_srp);
2009         device_remove_file(dev, &dev_attr_hnp);
2010 }
2011
2012 int dwc3_otg_create_dev_files(struct device *dev)
2013 {
2014         int retval;
2015
2016         retval = device_create_file(dev, &dev_attr_hnp);
2017         if (retval)
2018                 goto fail;
2019
2020         retval = device_create_file(dev, &dev_attr_srp);
2021         if (retval)
2022                 goto fail;
2023
2024         retval = device_create_file(dev, &dev_attr_end);
2025         if (retval)
2026                 goto fail;
2027
2028         retval = device_create_file(dev, &dev_attr_a_hnp_reqd);
2029         if (retval)
2030                 goto fail;
2031
2032         retval = device_create_file(dev, &dev_attr_print_dbg);
2033         if (retval)
2034                 goto fail;
2035
2036         return 0;
2037
2038 fail:
2039         dev_err(dev, "Failed to create one or more sysfs files!!\n");
2040         return retval;
2041 }
2042
2043 void dwc3_otg_init(struct dwc3 *dwc)
2044 {
2045         struct dwc3_otg *otg;
2046         int err;
2047         u32 reg;
2048
2049         dev_dbg(dwc->dev, "dwc3_otg_init\n");
2050
2051         /*
2052          * GHWPARAMS6[10] bit is SRPSupport.
2053          * This bit also reflects DWC_USB3_EN_OTG
2054          */
2055         reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
2056         if (!(reg & GHWPARAMS6_SRP_SUPPORT_ENABLED)) {
2057                 /*
2058                  * No OTG support in the HW core.
2059                  * We return 0 to indicate no error, since this is acceptable
2060                  * situation, just continue probe the dwc3 driver without otg.
2061                  */
2062                 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
2063                 return;
2064         }
2065
2066         otg = kzalloc(sizeof(*otg), GFP_KERNEL);
2067         if (!otg) {
2068                 dev_err(otg->dev, "failed to allocate memroy\n");
2069                 return;
2070         }
2071
2072         dwc->otg = otg;
2073         otg->dev = dwc->dev;
2074         otg->dwc = dwc;
2075
2076         otg->regs = dwc->regs - DWC3_GLOBALS_REGS_START;
2077         otg->otg.usb_phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
2078         otg->otg.usb_phy->dev = otg->dev;
2079         otg->otg.usb_phy->label = "dwc3_otg";
2080         otg->otg.state = OTG_STATE_UNDEFINED;
2081         otg->otg.usb_phy->otg = &otg->otg;
2082         otg->otg.usb_phy->notify_connect = dwc3_otg_notify_connect;
2083         otg->otg.usb_phy->notify_disconnect = dwc3_otg_notify_disconnect;
2084
2085         otg->otg.start_srp = dwc3_otg_start_srp;
2086         otg->otg.start_hnp = dwc3_otg_start_hnp;
2087         otg->otg.set_host = dwc3_otg_set_host;
2088         otg->otg.set_peripheral = dwc3_otg_set_periph;
2089
2090         otg->hwparams6 = reg;
2091         otg->state = OTG_STATE_UNDEFINED;
2092
2093         spin_lock_init(&otg->lock);
2094         init_waitqueue_head(&otg->main_wq);
2095
2096         err = usb_add_phy(otg->otg.usb_phy, USB_PHY_TYPE_USB3);
2097         if (err) {
2098                 dev_err(otg->dev, "can't register transceiver, err: %d\n",
2099                         err);
2100                 goto exit;
2101         }
2102
2103         otg->irq = platform_get_irq(to_platform_device(otg->dev), 1);
2104
2105         dwc3_otg_create_dev_files(otg->dev);
2106
2107         /* Set irq handler */
2108         err = request_irq(otg->irq, dwc3_otg_irq, IRQF_SHARED, "dwc3_otg", otg);
2109         if (err) {
2110                 dev_err(otg->otg.usb_phy->dev, "failed to request irq #%d --> %d\n",
2111                                 otg->irq, err);
2112                 goto exit;
2113         }
2114
2115         dwc3_otg_enable_irq(otg);
2116
2117         err = dwc3_gadget_init(dwc);
2118         if (err) {
2119                 if (err != -EPROBE_DEFER)
2120                         dev_err(otg->otg.usb_phy->dev,
2121                                 "failed to initialize gadget\n");
2122                 goto exit;
2123         }
2124
2125         err = dwc3_host_init(dwc);
2126         if (err) {
2127                 if (err != -EPROBE_DEFER)
2128                         dev_err(otg->otg.usb_phy->dev,
2129                                 "failed to initialize host\n");
2130                 goto exit;
2131         }
2132
2133         return;
2134
2135 exit:
2136         kfree(otg->otg.usb_phy);
2137         kfree(otg);
2138 }
2139
2140 void dwc3_otg_exit(struct dwc3 *dwc)
2141 {
2142         struct dwc3_otg *otg = dwc->otg;
2143
2144         otg_dbg(otg, "\n");
2145         usb_remove_phy(otg->otg.usb_phy);
2146         kfree(otg->otg.usb_phy);
2147         kfree(otg);
2148 }