2 * otg.c - DesignWare USB3 DRD Controller OTG file
4 * Copyright (C) 2016 Xilinx, Inc. All rights reserved.
6 * Author: Manish Narani <mnarani@xilinx.com>
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.
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.
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>
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>
36 #include <../drivers/usb/host/xhci.h>
37 #include "platform_data.h"
43 #include <linux/ulpi/regs.h>
44 #include <linux/ulpi/driver.h>
47 /* Print the hardware registers' value for debugging purpose */
48 static void print_debug_regs(struct dwc3_otg *otg)
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);
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);
79 /* Check whether the hardware supports HNP or not */
80 static int hnp_capable(struct dwc3_otg *otg)
82 if (otg->hwparams6 & GHWPARAMS6_HNP_SUPPORT_ENABLED)
87 /* Check whether the hardware supports SRP or not */
88 static int srp_capable(struct dwc3_otg *otg)
90 if (otg->hwparams6 & GHWPARAMS6_SRP_SUPPORT_ENABLED)
95 /* Wakeup main thread to execute the OTG flow after an event */
96 static void wakeup_main_thread(struct dwc3_otg *otg)
98 if (!otg->main_thread)
102 /* Tell the main thread that something has happened */
103 otg->main_wakeup_needed = 1;
104 wake_up_interruptible(&otg->main_wq);
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)
113 if (signal_pending(current)) {
114 otg_dbg(otg, "Main thread signal pending\n");
118 if (otg->main_wakeup_needed) {
119 otg_dbg(otg, "Main thread wakeup needed\n");
124 jiffies = msecs_to_jiffies(msecs);
125 rc = wait_event_freezable_timeout(otg->main_wq,
126 otg->main_wakeup_needed,
130 rc = jiffies_to_msecs(rc);
133 otg->main_wakeup_needed = 0;
137 /* Sleep main thread to wait for an event to occur */
138 static int sleep_main_thread(struct dwc3_otg *otg)
143 rc = sleep_main_thread_timeout(otg, 5000);
149 static void get_events(struct dwc3_otg *otg, u32 *otg_events, u32 *user_events)
153 spin_lock_irqsave(&otg->lock, flags);
156 *otg_events = otg->otg_events;
159 *user_events = otg->user_events;
161 spin_unlock_irqrestore(&otg->lock, flags);
164 static void get_and_clear_events(struct dwc3_otg *otg, u32 *otg_events,
169 spin_lock_irqsave(&otg->lock, flags);
172 *otg_events = otg->otg_events;
175 *user_events = otg->user_events;
178 otg->user_events = 0;
180 spin_unlock_irqrestore(&otg->lock, flags);
183 static int check_event(struct dwc3_otg *otg, u32 otg_mask, u32 user_mask)
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);
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)
204 /* Enable the events */
206 otg_write(otg, OEVTEN, otg_mask);
208 /* Wait until it occurs, or timeout, or interrupt. */
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);
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));
219 /* Disable the events */
220 otg_write(otg, OEVTEN, 0);
222 otg_vdbg(otg, "Woke up rc=%d\n", rc);
224 get_and_clear_events(otg, otg_events, user_events);
229 static void set_capabilities(struct dwc3_otg *otg)
234 if (srp_capable(otg))
235 ocfg |= OCFG_SRP_CAP;
237 if (hnp_capable(otg))
238 ocfg |= OCFG_HNP_CAP;
240 otg_write(otg, OCFG, ocfg);
242 otg_dbg(otg, "Enabled SRP and HNP capabilities in OCFG\n");
245 static int otg3_handshake(struct dwc3_otg *otg, u32 reg, u32 mask, u32 done,
249 u32 usec = msec * 1000;
251 otg_vdbg(otg, "reg=%08x, mask=%08x, value=%08x\n", reg, mask, done);
253 result = otg_read(otg, reg);
254 if ((result & mask) == done)
263 static int reset_port(struct dwc3_otg *otg)
268 return usb_bus_start_enum(otg->otg.host, 1);
271 static int set_peri_mode(struct dwc3_otg *otg, int mode)
276 octl = otg_read(otg, OCTL);
278 octl |= OCTL_PERI_MODE;
280 octl &= ~OCTL_PERI_MODE;
282 otg_write(otg, OCTL, octl);
283 otg_dbg(otg, "set OCTL PERI_MODE = %d in OCTL\n", mode);
286 return otg3_handshake(otg, OSTS, OSTS_PERIP_MODE,
287 OSTS_PERIP_MODE, 100);
289 return otg3_handshake(otg, OSTS, OSTS_PERIP_MODE, 0, 100);
294 static int start_host(struct dwc3_otg *otg)
303 struct xhci_hcd *xhci;
311 * Prevent the host USBCMD.HCRST from resetting OTG core by setting
314 ocfg = otg_read(otg, OCFG);
315 ocfg |= DWC3_OCFG_SFTRSTMASK;
316 otg_write(otg, OCFG, ocfg);
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);
325 if (!set_peri_mode(otg, PERI_MODE_HOST)) {
326 otg_err(otg, "Failed to start host\n");
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);
334 if (otg->host_started) {
335 otg_info(otg, "Host already started\n");
339 /* Start host driver */
341 *(struct xhci_hcd **)hcd->hcd_priv = xhci;
342 ret = usb_add_hcd(hcd, otg->hcd_irq, IRQF_SHARED);
344 otg_err(otg, "%s: failed to start primary hcd, ret=%d\n",
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);
354 "%s: failed to start secondary hcd, ret=%d\n",
361 otg->host_started = 1;
363 hcd->self.otg_port = 1;
364 if (xhci->shared_hcd)
365 xhci->shared_hcd->self.otg_port = 1;
367 set_capabilities(otg);
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,
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);
384 otg_dbg(otg, "Port is not powered by xhci-hcd\n");
391 static int stop_host(struct dwc3_otg *otg)
394 struct xhci_hcd *xhci;
398 if (!otg->host_started) {
399 otg_info(otg, "Host already stopped\n");
406 otg_dbg(otg, "%s: turn off host %s\n",
407 __func__, otg->otg.host->bus_name);
409 if (work_pending(&otg->hp_work.work)) {
410 while (!cancel_delayed_work(&otg->hp_work))
414 hcd = container_of(otg->otg.host, struct usb_hcd, self);
415 xhci = hcd_to_xhci(hcd);
417 if (xhci->shared_hcd)
418 usb_remove_hcd(xhci->shared_hcd);
421 otg->host_started = 0;
426 int dwc3_otg_host_release(struct usb_hcd *hcd)
429 struct usb_device *rh;
430 struct usb_device *udev;
440 udev = usb_hub_find_child(rh, bus->otg_port);
444 if (udev->config && udev->parent == udev->bus->root_hub) {
445 struct usb_otg20_descriptor *desc;
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) {
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,
460 NULL, 0, USB_CTRL_SET_TIMEOUT);
463 "can't initiate HNP from host: %d\n",
469 dev_info(&udev->dev, "didn't find OTG descriptor\n");
473 "udev->config NULL or udev->parent != udev->bus->root_hub\n");
479 /* Sends the host release set feature request */
480 static void host_release(struct dwc3_otg *otg)
483 struct xhci_hcd *xhci;
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);
495 static void dwc3_otg_setup_event_buffers(struct dwc3_otg *otg)
497 if (dwc3_readl(otg->dwc->regs, DWC3_GEVNTADRLO(0)) == 0x0) {
499 otg_dbg(otg, "setting up event buffers\n");
500 dwc3_event_buffers_setup(otg->dwc);
505 static void start_peripheral(struct dwc3_otg *otg)
507 struct usb_gadget *gadget = otg->otg.gadget;
508 struct dwc3 *dwc = otg->dwc;
516 * Prevent the gadget DCTL.CSFTRST from resetting OTG core by setting
519 ocfg = otg_read(otg, OCFG);
520 ocfg |= DWC3_OCFG_SFTRSTMASK;
521 otg_write(otg, OCFG, ocfg);
523 if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
524 otg_err(otg, "Failed to set peripheral mode\n");
526 if (otg->peripheral_started) {
527 otg_info(otg, "Peripheral already started\n");
531 set_capabilities(otg);
533 dwc3_otg_setup_event_buffers(otg);
535 if (dwc->gadget_driver) {
539 spin_lock(&otg->lock);
542 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
548 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
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);
557 otg_dbg(otg, "enabled irq\n");
558 dwc3_gadget_enable_irq(dwc);
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);
565 gadget->b_hnp_enable = 0;
566 gadget->host_request_flag = 0;
568 otg->peripheral_started = 1;
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
579 __dwc3_gadget_ep_disable(dwc->eps[0]);
585 static void stop_peripheral(struct dwc3_otg *otg)
587 struct usb_gadget *gadget = otg->otg.gadget;
588 struct dwc3 *dwc = otg->dwc;
592 if (!otg->peripheral_started) {
593 otg_info(otg, "Peripheral already stopped\n");
600 otg_dbg(otg, "disabled ep in gadget driver\n");
601 spin_lock(&otg->lock);
603 dwc3_gadget_disable_irq(dwc);
604 __dwc3_gadget_ep_disable(dwc->eps[0]);
605 __dwc3_gadget_ep_disable(dwc->eps[1]);
607 spin_unlock(&otg->lock);
609 otg->peripheral_started = 0;
613 static void set_b_host(struct dwc3_otg *otg, int val)
615 otg->otg.host->is_b_host = val;
618 static enum usb_otg_state do_b_idle(struct dwc3_otg *otg);
620 static int init_b_device(struct dwc3_otg *otg)
623 set_capabilities(otg);
625 if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
626 otg_err(otg, "Failed to start peripheral\n");
628 return do_b_idle(otg);
631 static int init_a_device(struct dwc3_otg *otg)
633 otg_write(otg, OCFG, 0);
634 otg_write(otg, OCTL, 0);
636 otg_dbg(otg, "Write 0 to OCFG and OCTL\n");
637 return OTG_STATE_A_IDLE;
640 static enum usb_otg_state do_connector_id_status(struct dwc3_otg *otg)
642 enum usb_otg_state state;
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);
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);
659 otg_dbg(otg, "Connector ID is B\n");
661 state = init_b_device(otg);
664 /* TODO: This is a workaround for latest hibernation-enabled bitfiles
665 * which have problems before initializing SRP.
672 static void reset_hw(struct dwc3_otg *otg)
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);
687 #define SRP_TIMEOUT 6000
689 static void start_srp(struct dwc3_otg *otg)
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");
699 static void start_b_hnp(struct dwc3_otg *otg)
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");
709 static void stop_b_hnp(struct dwc3_otg *otg)
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");
719 static void start_a_hnp(struct dwc3_otg *otg)
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");
729 static void stop_a_hnp(struct dwc3_otg *otg)
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");
739 static enum usb_otg_state do_a_hnp_init(struct dwc3_otg *otg)
746 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
747 OEVT_A_DEV_HNP_CHNG_EVNT;
753 rc = sleep_until_event(otg,
755 &otg_events, NULL, rc);
758 return OTG_STATE_UNDEFINED;
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;
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;
772 otg_dbg(otg, "A-HNP Failed\n");
773 return OTG_STATE_A_WAIT_VFALL;
776 } else if (rc == 0) {
777 otg_dbg(otg, "A-HNP Failed (Timed out)\n");
778 return OTG_STATE_A_WAIT_VFALL;
785 return OTG_STATE_UNDEFINED;
788 static enum usb_otg_state do_a_host(struct dwc3_otg *otg)
798 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
799 OEVT_A_DEV_SESS_END_DET_EVNT;
800 user_mask = USER_SRP_EVENT |
803 rc = sleep_until_event(otg,
805 &otg_events, &user_events, 0);
807 return OTG_STATE_UNDEFINED;
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;
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;
818 } else if (user_events & USER_HNP_EVENT) {
819 otg_dbg(otg, "USER_HNP_EVENT\n");
820 return OTG_STATE_A_SUSPEND;
824 return OTG_STATE_UNDEFINED;
827 #define A_WAIT_VFALL_TIMEOUT 1000
829 static enum usb_otg_state do_a_wait_vfall(struct dwc3_otg *otg)
837 otg_mask = OEVT_A_DEV_IDLE_EVNT;
839 rc = A_WAIT_VFALL_TIMEOUT;
840 rc = sleep_until_event(otg,
842 &otg_events, NULL, rc);
844 return OTG_STATE_UNDEFINED;
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;
850 } else if (rc == 0) {
851 otg_dbg(otg, "A_WAIT_VFALL_TIMEOUT\n");
852 return OTG_STATE_A_IDLE;
856 return OTG_STATE_UNDEFINED;
860 #define A_WAIT_BCON_TIMEOUT 1000
862 static enum usb_otg_state do_a_wait_bconn(struct dwc3_otg *otg)
870 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
871 OEVT_A_DEV_SESS_END_DET_EVNT |
872 OEVT_A_DEV_HOST_EVNT;
874 rc = A_WAIT_BCON_TIMEOUT;
875 rc = sleep_until_event(otg,
877 &otg_events, NULL, rc);
879 return OTG_STATE_UNDEFINED;
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;
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;
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;
894 } else if (rc == 0) {
895 if (otg_read(otg, OCTL) & OCTL_PRT_PWR_CTL)
896 return OTG_STATE_A_HOST;
898 return OTG_STATE_A_WAIT_VFALL;
902 return OTG_STATE_UNDEFINED;
905 #define A_WAIT_VRISE_TIMEOUT 100
907 static enum usb_otg_state do_a_wait_vrise(struct dwc3_otg *otg)
913 struct xhci_hcd *xhci;
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);
924 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
925 OEVT_A_DEV_SESS_END_DET_EVNT;
927 rc = A_WAIT_VRISE_TIMEOUT;
929 rc = sleep_until_event(otg,
931 &otg_events, NULL, rc);
933 return OTG_STATE_UNDEFINED;
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;
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;
944 } else if (rc == 0) {
945 if (otg_read(otg, OCTL) & OCTL_PRT_PWR_CTL)
946 return OTG_STATE_A_WAIT_BCON;
948 return OTG_STATE_A_WAIT_VFALL;
952 return OTG_STATE_UNDEFINED;
955 static enum usb_otg_state do_a_idle(struct dwc3_otg *otg)
965 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT | OEVT_A_DEV_SRP_DET_EVNT;
966 user_mask = USER_SRP_EVENT;
968 rc = sleep_until_event(otg,
970 &otg_events, &user_events,
974 return OTG_STATE_UNDEFINED;
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;
987 return OTG_STATE_UNDEFINED;
990 static enum usb_otg_state do_a_peripheral(struct dwc3_otg *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;
1001 rc = sleep_until_event(otg,
1003 &otg_events, NULL, 0);
1005 return OTG_STATE_UNDEFINED;
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;
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;
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;
1020 return OTG_STATE_UNDEFINED;
1023 #define HNP_TIMEOUT 4000
1025 static enum usb_otg_state do_b_hnp_init(struct dwc3_otg *otg)
1032 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1033 OEVT_B_DEV_HNP_CHNG_EVNT |
1034 OEVT_B_DEV_VBUS_CHNG_EVNT;
1040 rc = sleep_until_event(otg,
1046 return OTG_STATE_UNDEFINED;
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;
1061 otg_err(otg, "B-HNP Failed\n");
1062 return OTG_STATE_B_PERIPHERAL;
1064 } else if (rc == 0) {
1066 otg_err(otg, "HNP timed out!\n");
1067 return OTG_STATE_B_PERIPHERAL;
1073 return OTG_STATE_UNDEFINED;
1076 static enum usb_otg_state do_b_peripheral(struct dwc3_otg *otg)
1082 u32 user_events = 0;
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;
1090 rc = sleep_until_event(otg,
1091 otg_mask, user_mask,
1092 &otg_events, &user_events, 0);
1094 return OTG_STATE_UNDEFINED;
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");
1102 if (otg_events & OEVT_B_SES_VLD_EVT) {
1103 otg_dbg(otg, "Session valid\n");
1106 otg_dbg(otg, "Session not valid\n");
1107 return OTG_STATE_B_IDLE;
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;
1118 return OTG_STATE_UNDEFINED;
1121 static enum usb_otg_state do_b_wait_acon(struct dwc3_otg *otg)
1127 u32 user_events = 0;
1128 struct usb_hcd *hcd;
1129 struct xhci_hcd *xhci;
1134 otg_mask = OEVT_B_DEV_B_HOST_END_EVNT;
1135 otg_write(otg, OEVTEN, otg_mask);
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);
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;
1151 rc = sleep_until_event(otg,
1152 otg_mask, user_mask,
1153 &otg_events, &user_events, 0);
1155 return OTG_STATE_UNDEFINED;
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");
1170 otg_dbg(otg, "Session not valid\n");
1171 return OTG_STATE_B_IDLE;
1173 } else if (user_events & USER_A_CONN_EVENT) {
1174 otg_dbg(otg, "A-device connected\n");
1175 return OTG_STATE_B_HOST;
1179 return OTG_STATE_UNDEFINED;
1182 static enum usb_otg_state do_b_host(struct dwc3_otg *otg)
1188 u32 user_events = 0;
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;
1198 rc = sleep_until_event(otg,
1199 otg_mask, user_mask,
1200 &otg_events, &user_events, 0);
1202 return OTG_STATE_UNDEFINED;
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");
1217 otg_dbg(otg, "Session not valid\n");
1218 return OTG_STATE_B_IDLE;
1223 return OTG_STATE_UNDEFINED;
1226 static enum usb_otg_state do_b_idle(struct dwc3_otg *otg)
1232 u32 user_events = 0;
1236 if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
1237 otg_err(otg, "Failed to set peripheral mode\n");
1239 dwc3_otg_setup_event_buffers(otg);
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;
1247 rc = sleep_until_event(otg,
1248 otg_mask, user_mask,
1249 &otg_events, &user_events, 0);
1252 return OTG_STATE_UNDEFINED;
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;
1265 otg_dbg(otg, "Session not valid\n");
1268 } else if (user_events & USER_SRP_EVENT) {
1269 otg_dbg(otg, "USER_SRP_EVENT\n");
1270 return OTG_STATE_B_SRP_INIT;
1273 return OTG_STATE_UNDEFINED;
1276 static enum usb_otg_state do_b_srp_init(struct dwc3_otg *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;
1287 otg_write(otg, OEVTEN, otg_mask);
1293 rc = sleep_until_event(otg,
1297 return OTG_STATE_UNDEFINED;
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;
1314 return OTG_STATE_UNDEFINED;
1317 int otg_main_thread(void *data)
1319 struct dwc3_otg *otg = (struct dwc3_otg *)data;
1320 enum usb_otg_state prev = OTG_STATE_UNDEFINED;
1322 #ifdef VERBOSE_DEBUG
1323 u32 snpsid = otg_read(otg, 0xc120);
1325 otg_vdbg(otg, "io_priv=%p\n", otg->regs);
1326 otg_vdbg(otg, "c120: %x\n", snpsid);
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.
1332 allow_signal(SIGINT);
1333 allow_signal(SIGTERM);
1334 allow_signal(SIGKILL);
1335 allow_signal(SIGUSR1);
1337 /* Allow the thread to be frozen */
1340 /* Allow host/peripheral driver load to finish */
1346 stop_peripheral(otg);
1348 otg_dbg(otg, "Thread running\n");
1350 enum usb_otg_state next = OTG_STATE_UNDEFINED;
1352 otg_vdbg(otg, "Main thread entering state\n");
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);
1360 case OTG_STATE_A_IDLE:
1361 otg_dbg(otg, "OTG_STATE_A_IDLE\n");
1362 stop_peripheral(otg);
1364 if (prev == OTG_STATE_UNDEFINED)
1365 next = OTG_STATE_A_WAIT_VRISE;
1367 next = do_a_idle(otg);
1370 case OTG_STATE_A_WAIT_VRISE:
1371 otg_dbg(otg, "OTG_STATE_A_WAIT_VRISE\n");
1372 next = do_a_wait_vrise(otg);
1375 case OTG_STATE_A_WAIT_BCON:
1376 otg_dbg(otg, "OTG_STATE_A_WAIT_BCON\n");
1377 next = do_a_wait_bconn(otg);
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
1389 if (next != OTG_STATE_A_SUSPEND)
1393 case OTG_STATE_A_SUSPEND:
1394 otg_dbg(otg, "OTG_STATE_A_SUSPEND\n");
1395 next = do_a_hnp_init(otg);
1397 /* Stop the host. */
1401 case OTG_STATE_A_WAIT_VFALL:
1402 otg_dbg(otg, "OTG_STATE_A_WAIT_VFALL\n");
1403 next = do_a_wait_vfall(otg);
1407 case OTG_STATE_A_PERIPHERAL:
1408 otg_dbg(otg, "OTG_STATE_A_PERIPHERAL\n");
1410 start_peripheral(otg);
1411 next = do_a_peripheral(otg);
1412 stop_peripheral(otg);
1415 case OTG_STATE_B_IDLE:
1416 otg_dbg(otg, "OTG_STATE_B_IDLE\n");
1417 next = do_b_idle(otg);
1420 case OTG_STATE_B_PERIPHERAL:
1421 otg_dbg(otg, "OTG_STATE_B_PERIPHERAL\n");
1423 start_peripheral(otg);
1424 next = do_b_peripheral(otg);
1425 stop_peripheral(otg);
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);
1434 case OTG_STATE_B_WAIT_ACON:
1435 otg_dbg(otg, "OTG_STATE_B_WAIT_ACON\n");
1436 next = do_b_wait_acon(otg);
1439 case OTG_STATE_B_HOST:
1440 otg_dbg(otg, "OTG_STATE_B_HOST\n");
1441 next = do_b_host(otg);
1446 otg_err(otg, "Unknown state %d, sleeping...\n",
1448 sleep_main_thread(otg);
1452 prev = otg->otg.state;
1453 otg->otg.state = next;
1454 if (kthread_should_stop())
1458 otg->main_thread = NULL;
1459 otg_dbg(otg, "OTG main thread exiting....\n");
1464 static void start_main_thread(struct dwc3_otg *otg)
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);
1473 static inline struct dwc3_otg *otg_to_dwc3_otg(struct usb_otg *x)
1475 return container_of(x, struct dwc3_otg, otg);
1478 static irqreturn_t dwc3_otg_irq(int irq, void *_otg)
1480 struct dwc3_otg *otg;
1486 u32 otg_mask = OEVT_ALL;
1491 otg = (struct dwc3_otg *)_otg;
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);
1499 /* Clear handled events */
1500 otg_write(otg, OEVT, oevt);
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);
1509 otg_vdbg(otg, "oevt[DeviceMode] = %s\n",
1510 oevt & OEVT_DEV_MOD_EVNT ? "Device" : "Host");
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");
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);
1549 static void hnp_polling_work(struct work_struct *w)
1551 struct dwc3_otg *otg = container_of(w, struct dwc3_otg,
1553 struct usb_bus *bus;
1554 struct usb_device *udev;
1555 struct usb_hcd *hcd;
1560 hcd = container_of(otg->otg.host, struct usb_hcd, self);
1568 udev = usb_hub_find_child(bus->root_hub, bus->otg_port);
1572 otgstatus = kmalloc(sizeof(*otgstatus), GFP_NOIO);
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);
1581 if (ret == sizeof(*otgstatus) && (*otgstatus & 0x1)) {
1582 /* enable HNP before suspend, it's simpler */
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);
1594 /* OTG MESSAGE: report errors here,
1595 * customize to match your product.
1597 otg_info(otg, "ERROR : Device no response\n");
1598 dev_info(&udev->dev, "can't set HNP mode: %d\n",
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)
1604 dev_dbg(&udev->dev, "HNP fail, %d\n",
1608 /* Device wants role-switch, suspend the bus. */
1609 static struct usb_phy *phy;
1611 phy = usb_get_phy(USB_PHY_TYPE_USB3);
1612 otg_start_hnp(phy->otg);
1615 if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND) < 0)
1616 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
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);
1628 schedule_delayed_work(&otg->hp_work, 1 * HZ);
1634 static int dwc3_otg_notify_connect(struct usb_phy *phy,
1635 enum usb_device_speed speed)
1637 struct usb_bus *bus;
1638 struct usb_device *udev;
1639 struct usb_hcd *hcd;
1640 struct dwc3_otg *otg;
1643 otg = otg_to_dwc3_otg(phy->otg);
1645 hcd = container_of(phy->otg->host, struct usb_hcd, self);
1653 udev = usb_hub_find_child(bus->root_hub, bus->otg_port);
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.
1662 if (udev->config && udev->parent == udev->bus->root_hub) {
1663 struct usb_otg20_descriptor *desc = NULL;
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))
1672 if (udev->portnum == udev->bus->otg_port) {
1673 INIT_DELAYED_WORK(&otg->hp_work,
1675 schedule_delayed_work(&otg->hp_work, HZ);
1683 static int dwc3_otg_notify_disconnect(struct usb_phy *phy,
1684 enum usb_device_speed speed)
1686 struct dwc3_otg *otg;
1688 otg = otg_to_dwc3_otg(phy->otg);
1690 if (work_pending(&otg->hp_work.work)) {
1691 while (!cancel_delayed_work(&otg->hp_work))
1697 static void dwc3_otg_set_peripheral(struct usb_otg *_otg, int yes)
1699 struct dwc3_otg *otg;
1704 otg = otg_to_dwc3_otg(_otg);
1708 if (otg->hwparams6 == 0xdeadbeef)
1709 otg->hwparams6 = otg_read(otg, GHWPARAMS6);
1712 stop_peripheral(otg);
1715 set_peri_mode(otg, yes);
1717 EXPORT_SYMBOL(dwc3_otg_set_peripheral);
1719 static int dwc3_otg_set_periph(struct usb_otg *_otg, struct usb_gadget *gadget)
1721 struct dwc3_otg *otg;
1726 otg = otg_to_dwc3_otg(_otg);
1729 if ((long)gadget == 1) {
1730 dwc3_otg_set_peripheral(_otg, 1);
1735 otg->otg.gadget = NULL;
1739 otg->otg.gadget = gadget;
1740 otg->otg.gadget->hnp_polling_support = 1;
1741 otg->otg.state = OTG_STATE_B_IDLE;
1743 start_main_thread(otg);
1747 static int dwc3_otg_set_host(struct usb_otg *_otg, struct usb_bus *host)
1749 struct dwc3_otg *otg;
1750 struct usb_hcd *hcd;
1751 struct xhci_hcd *xhci;
1756 otg = otg_to_dwc3_otg(_otg);
1759 if ((long)host == 1) {
1760 dwc3_otg_set_peripheral(_otg, 0);
1765 otg->otg.host = NULL;
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);
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);
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);
1785 otg->host_started = 1;
1787 start_main_thread(otg);
1791 static int dwc3_otg_start_srp(struct usb_otg *x)
1793 unsigned long flags;
1794 struct dwc3_otg *otg;
1799 otg = otg_to_dwc3_otg(x);
1802 if (!otg->otg.host || !otg->otg.gadget)
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);
1812 static int dwc3_otg_start_hnp(struct usb_otg *x)
1814 unsigned long flags;
1815 struct dwc3_otg *otg;
1820 otg = otg_to_dwc3_otg(x);
1823 if (!otg->otg.host || !otg->otg.gadget)
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);
1833 static int dwc3_otg_end_session(struct usb_otg *x)
1835 unsigned long flags;
1836 struct dwc3_otg *otg;
1841 otg = otg_to_dwc3_otg(x);
1844 if (!otg->otg.host || !otg->otg.gadget)
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);
1854 static int otg_end_session(struct usb_otg *otg)
1856 return dwc3_otg_end_session(otg);
1858 EXPORT_SYMBOL(otg_end_session);
1860 static int dwc3_otg_received_host_release(struct usb_otg *x)
1862 struct dwc3_otg *otg;
1863 unsigned long flags;
1868 otg = otg_to_dwc3_otg(x);
1871 if (!otg->otg.host || !otg->otg.gadget)
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);
1881 int otg_host_release(struct usb_otg *otg)
1883 return dwc3_otg_received_host_release(otg);
1885 EXPORT_SYMBOL(otg_host_release);
1887 static void dwc3_otg_enable_irq(struct dwc3_otg *otg)
1891 /* Enable OTG IRQs */
1894 otg_write(otg, OEVTEN, reg);
1897 static ssize_t store_srp(struct device *dev, struct device_attribute *attr,
1898 const char *buf, size_t count)
1900 struct usb_phy *phy;
1901 struct usb_otg *otg;
1903 phy = usb_get_phy(USB_PHY_TYPE_USB3);
1904 if (IS_ERR(phy) || !phy) {
1920 static DEVICE_ATTR(srp, 0220, NULL, store_srp);
1922 static ssize_t store_end(struct device *dev, struct device_attribute *attr,
1923 const char *buf, size_t count)
1925 struct usb_phy *phy;
1926 struct usb_otg *otg;
1928 phy = usb_get_phy(USB_PHY_TYPE_USB3);
1929 if (IS_ERR(phy) || !phy) {
1941 otg_end_session(otg);
1945 static DEVICE_ATTR(end, 0220, NULL, store_end);
1947 static ssize_t store_hnp(struct device *dev, struct device_attribute *attr,
1948 const char *buf, size_t count)
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;
1954 dev_dbg(dwc->dev, "%s()\n", __func__);
1956 if (IS_ERR(phy) || !phy) {
1957 dev_info(dwc->dev, "NO PHY!!\n");
1965 dev_info(dwc->dev, "NO OTG!!\n");
1970 dev_info(dev, "b_hnp_enable is FALSE\n");
1971 dwc->gadget.host_request_flag = 1;
1976 static DEVICE_ATTR(hnp, 0220, NULL, store_hnp);
1978 static ssize_t store_a_hnp_reqd(struct device *dev,
1979 struct device_attribute *attr, const char *buf, size_t count)
1981 struct dwc3 *dwc = dev_get_drvdata(dev);
1982 struct dwc3_otg *otg;
1988 static DEVICE_ATTR(a_hnp_reqd, 0220, NULL, store_a_hnp_reqd);
1990 static ssize_t store_print_dbg(struct device *dev,
1991 struct device_attribute *attr, const char *buf, size_t count)
1993 struct dwc3 *dwc = dev_get_drvdata(dev);
1994 struct dwc3_otg *otg;
1997 print_debug_regs(otg);
2001 static DEVICE_ATTR(print_dbg, 0220, NULL, store_print_dbg);
2003 void dwc_usb3_remove_dev_files(struct device *dev)
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);
2012 int dwc3_otg_create_dev_files(struct device *dev)
2016 retval = device_create_file(dev, &dev_attr_hnp);
2020 retval = device_create_file(dev, &dev_attr_srp);
2024 retval = device_create_file(dev, &dev_attr_end);
2028 retval = device_create_file(dev, &dev_attr_a_hnp_reqd);
2032 retval = device_create_file(dev, &dev_attr_print_dbg);
2039 dev_err(dev, "Failed to create one or more sysfs files!!\n");
2043 void dwc3_otg_init(struct dwc3 *dwc)
2045 struct dwc3_otg *otg;
2049 dev_dbg(dwc->dev, "dwc3_otg_init\n");
2052 * GHWPARAMS6[10] bit is SRPSupport.
2053 * This bit also reflects DWC_USB3_EN_OTG
2055 reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
2056 if (!(reg & GHWPARAMS6_SRP_SUPPORT_ENABLED)) {
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.
2062 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
2066 otg = kzalloc(sizeof(*otg), GFP_KERNEL);
2068 dev_err(otg->dev, "failed to allocate memroy\n");
2073 otg->dev = dwc->dev;
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;
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;
2090 otg->hwparams6 = reg;
2091 otg->state = OTG_STATE_UNDEFINED;
2093 spin_lock_init(&otg->lock);
2094 init_waitqueue_head(&otg->main_wq);
2096 err = usb_add_phy(otg->otg.usb_phy, USB_PHY_TYPE_USB3);
2098 dev_err(otg->dev, "can't register transceiver, err: %d\n",
2103 otg->irq = platform_get_irq(to_platform_device(otg->dev), 1);
2105 dwc3_otg_create_dev_files(otg->dev);
2107 /* Set irq handler */
2108 err = request_irq(otg->irq, dwc3_otg_irq, IRQF_SHARED, "dwc3_otg", otg);
2110 dev_err(otg->otg.usb_phy->dev, "failed to request irq #%d --> %d\n",
2115 dwc3_otg_enable_irq(otg);
2117 err = dwc3_gadget_init(dwc);
2119 if (err != -EPROBE_DEFER)
2120 dev_err(otg->otg.usb_phy->dev,
2121 "failed to initialize gadget\n");
2125 err = dwc3_host_init(dwc);
2127 if (err != -EPROBE_DEFER)
2128 dev_err(otg->otg.usb_phy->dev,
2129 "failed to initialize host\n");
2136 kfree(otg->otg.usb_phy);
2140 void dwc3_otg_exit(struct dwc3 *dwc)
2142 struct dwc3_otg *otg = dwc->otg;
2145 usb_remove_phy(otg->otg.usb_phy);
2146 kfree(otg->otg.usb_phy);