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)
999 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1000 OEVT_A_DEV_SESS_END_DET_EVNT |
1001 OEVT_A_DEV_B_DEV_HOST_END_EVNT;
1002 user_mask = USER_HNP_END_SESSION;
1004 rc = sleep_until_event(otg,
1005 otg_mask, user_mask,
1006 &otg_events, &user_events, 0);
1008 return OTG_STATE_UNDEFINED;
1010 if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1011 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1012 return OTG_STATE_UNDEFINED;
1014 } else if (otg_events & OEVT_A_DEV_SESS_END_DET_EVNT) {
1015 otg_dbg(otg, "OEVT_A_DEV_SESS_END_DET_EVNT\n");
1016 return OTG_STATE_A_WAIT_VFALL;
1018 } else if (otg_events & OEVT_A_DEV_B_DEV_HOST_END_EVNT) {
1019 otg_dbg(otg, "OEVT_A_DEV_B_DEV_HOST_END_EVNT\n");
1020 return OTG_STATE_A_WAIT_VRISE;
1021 } else if (user_events & USER_HNP_END_SESSION) {
1022 otg_dbg(otg, "USER_HNP_END_SESSION\n");
1023 return OTG_STATE_A_WAIT_VRISE;
1026 return OTG_STATE_UNDEFINED;
1029 #define HNP_TIMEOUT 4000
1031 static enum usb_otg_state do_b_hnp_init(struct dwc3_otg *otg)
1038 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1039 OEVT_B_DEV_HNP_CHNG_EVNT |
1040 OEVT_B_DEV_VBUS_CHNG_EVNT;
1046 rc = sleep_until_event(otg,
1052 return OTG_STATE_UNDEFINED;
1054 if (events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1055 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1056 return OTG_STATE_UNDEFINED;
1057 } else if (events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1058 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1059 return OTG_STATE_B_IDLE;
1060 } else if (events & OEVT_B_DEV_HNP_CHNG_EVNT) {
1061 otg_dbg(otg, "OEVT_B_DEV_HNP_CHNG_EVNT\n");
1062 if (events & OEVT_HST_NEG_SCS) {
1063 otg_dbg(otg, "B-HNP Success\n");
1064 return OTG_STATE_B_WAIT_ACON;
1067 otg_err(otg, "B-HNP Failed\n");
1068 return OTG_STATE_B_PERIPHERAL;
1070 } else if (rc == 0) {
1072 otg_err(otg, "HNP timed out!\n");
1073 return OTG_STATE_B_PERIPHERAL;
1079 return OTG_STATE_UNDEFINED;
1082 static enum usb_otg_state do_b_peripheral(struct dwc3_otg *otg)
1088 u32 user_events = 0;
1091 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT | OEVT_B_DEV_VBUS_CHNG_EVNT;
1092 user_mask = USER_HNP_EVENT | USER_END_SESSION |
1093 USER_SRP_EVENT | INITIAL_SRP;
1096 rc = sleep_until_event(otg,
1097 otg_mask, user_mask,
1098 &otg_events, &user_events, 0);
1100 return OTG_STATE_UNDEFINED;
1102 if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1103 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1104 return OTG_STATE_UNDEFINED;
1105 } else if (otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1106 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1108 if (otg_events & OEVT_B_SES_VLD_EVT) {
1109 otg_dbg(otg, "Session valid\n");
1112 otg_dbg(otg, "Session not valid\n");
1113 return OTG_STATE_B_IDLE;
1116 } else if (user_events & USER_HNP_EVENT) {
1117 otg_dbg(otg, "USER_HNP_EVENT\n");
1118 return do_b_hnp_init(otg);
1119 } else if (user_events & USER_END_SESSION) {
1120 otg_dbg(otg, "USER_END_SESSION\n");
1121 return OTG_STATE_B_IDLE;
1124 return OTG_STATE_UNDEFINED;
1127 static enum usb_otg_state do_b_wait_acon(struct dwc3_otg *otg)
1133 u32 user_events = 0;
1134 struct usb_hcd *hcd;
1135 struct xhci_hcd *xhci;
1140 otg_mask = OEVT_B_DEV_B_HOST_END_EVNT;
1141 otg_write(otg, OEVTEN, otg_mask);
1144 hcd = container_of(otg->otg.host, struct usb_hcd, self);
1145 xhci = hcd_to_xhci(hcd);
1146 usb_kick_hub_wq(hcd->self.root_hub);
1147 if (xhci->shared_hcd)
1148 usb_kick_hub_wq(xhci->shared_hcd->self.root_hub);
1150 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1151 OEVT_B_DEV_B_HOST_END_EVNT |
1152 OEVT_B_DEV_VBUS_CHNG_EVNT |
1153 OEVT_HOST_ROLE_REQ_INIT_EVNT;
1154 user_mask = USER_A_CONN_EVENT | USER_HNP_END_SESSION;
1157 rc = sleep_until_event(otg,
1158 otg_mask, user_mask,
1159 &otg_events, &user_events, 0);
1161 return OTG_STATE_UNDEFINED;
1163 /* Higher priority first */
1164 if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1165 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1166 return OTG_STATE_UNDEFINED;
1167 } else if (otg_events & OEVT_B_DEV_B_HOST_END_EVNT) {
1168 otg_dbg(otg, "OEVT_B_DEV_B_HOST_END_EVNT\n");
1169 return OTG_STATE_B_PERIPHERAL;
1170 } else if (otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1171 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1172 if (otg_events & OEVT_B_SES_VLD_EVT) {
1173 otg_dbg(otg, "Session valid\n");
1176 otg_dbg(otg, "Session not valid\n");
1177 return OTG_STATE_B_IDLE;
1179 } else if (user_events & USER_A_CONN_EVENT) {
1180 otg_dbg(otg, "A-device connected\n");
1181 return OTG_STATE_B_HOST;
1182 } else if (user_events & USER_HNP_END_SESSION) {
1183 otg_dbg(otg, "USER_HNP_END_SESSION\n");
1184 return OTG_STATE_B_PERIPHERAL;
1188 return OTG_STATE_UNDEFINED;
1191 static enum usb_otg_state do_b_host(struct dwc3_otg *otg)
1197 u32 user_events = 0;
1201 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1202 OEVT_B_DEV_B_HOST_END_EVNT |
1203 OEVT_B_DEV_VBUS_CHNG_EVNT |
1204 OEVT_HOST_ROLE_REQ_INIT_EVNT;
1205 user_mask = USER_HNP_END_SESSION;
1208 rc = sleep_until_event(otg,
1209 otg_mask, user_mask,
1210 &otg_events, &user_events, 0);
1212 return OTG_STATE_UNDEFINED;
1214 /* Higher priority first */
1215 if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1216 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1217 return OTG_STATE_UNDEFINED;
1218 } else if (otg_events & OEVT_B_DEV_B_HOST_END_EVNT) {
1219 otg_dbg(otg, "OEVT_B_DEV_B_HOST_END_EVNT\n");
1220 return OTG_STATE_B_PERIPHERAL;
1221 } else if (otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) {
1222 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1223 if (otg_events & OEVT_B_SES_VLD_EVT) {
1224 otg_dbg(otg, "Session valid\n");
1227 otg_dbg(otg, "Session not valid\n");
1228 return OTG_STATE_B_IDLE;
1230 } else if (user_events & USER_HNP_END_SESSION) {
1231 otg_dbg(otg, "USER_HNP_END_SESSION\n");
1232 return OTG_STATE_B_PERIPHERAL;
1236 return OTG_STATE_UNDEFINED;
1239 static enum usb_otg_state do_b_idle(struct dwc3_otg *otg)
1245 u32 user_events = 0;
1249 if (!set_peri_mode(otg, PERI_MODE_PERIPHERAL))
1250 otg_err(otg, "Failed to set peripheral mode\n");
1252 dwc3_otg_setup_event_buffers(otg);
1254 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1255 OEVT_B_DEV_SES_VLD_DET_EVNT |
1256 OEVT_B_DEV_VBUS_CHNG_EVNT;
1257 user_mask = USER_SRP_EVENT;
1260 rc = sleep_until_event(otg,
1261 otg_mask, user_mask,
1262 &otg_events, &user_events, 0);
1265 return OTG_STATE_UNDEFINED;
1267 if (otg_events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1268 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1269 return OTG_STATE_UNDEFINED;
1270 } else if ((otg_events & OEVT_B_DEV_VBUS_CHNG_EVNT) ||
1271 (otg_events & OEVT_B_DEV_SES_VLD_DET_EVNT)) {
1272 otg_dbg(otg, "OEVT_B_DEV_VBUS_CHNG_EVNT\n");
1273 if (otg_events & OEVT_B_SES_VLD_EVT) {
1274 otg_dbg(otg, "Session valid\n");
1275 return OTG_STATE_B_PERIPHERAL;
1278 otg_dbg(otg, "Session not valid\n");
1281 } else if (user_events & USER_SRP_EVENT) {
1282 otg_dbg(otg, "USER_SRP_EVENT\n");
1283 return OTG_STATE_B_SRP_INIT;
1286 return OTG_STATE_UNDEFINED;
1289 static enum usb_otg_state do_b_srp_init(struct dwc3_otg *otg)
1296 otg_mask = OEVT_CONN_ID_STS_CHNG_EVNT |
1297 OEVT_B_DEV_SES_VLD_DET_EVNT |
1298 OEVT_B_DEV_VBUS_CHNG_EVNT;
1300 otg_write(otg, OEVTEN, otg_mask);
1306 rc = sleep_until_event(otg,
1310 return OTG_STATE_UNDEFINED;
1312 if (events & OEVT_CONN_ID_STS_CHNG_EVNT) {
1313 otg_dbg(otg, "OEVT_CONN_ID_STS_CHNG_EVNT\n");
1314 return OTG_STATE_UNDEFINED;
1315 } else if (events & OEVT_B_DEV_SES_VLD_DET_EVNT) {
1316 otg_dbg(otg, "OEVT_B_DEV_SES_VLD_DET_EVNT\n");
1317 return OTG_STATE_B_PERIPHERAL;
1318 } else if (rc == 0) {
1319 otg_dbg(otg, "SRP Timeout (rc=%d)\n", rc);
1320 otg_info(otg, "DEVICE NO RESPONSE FOR SRP\n");
1321 return OTG_STATE_B_IDLE;
1327 return OTG_STATE_UNDEFINED;
1330 int otg_main_thread(void *data)
1332 struct dwc3_otg *otg = (struct dwc3_otg *)data;
1333 enum usb_otg_state prev = OTG_STATE_UNDEFINED;
1335 #ifdef VERBOSE_DEBUG
1336 u32 snpsid = otg_read(otg, 0xc120);
1338 otg_vdbg(otg, "io_priv=%p\n", otg->regs);
1339 otg_vdbg(otg, "c120: %x\n", snpsid);
1342 /* Allow the thread to be killed by a signal, but set the signal mask
1343 * to block everything but INT, TERM, KILL, and USR1.
1345 allow_signal(SIGINT);
1346 allow_signal(SIGTERM);
1347 allow_signal(SIGKILL);
1348 allow_signal(SIGUSR1);
1350 /* Allow the thread to be frozen */
1353 /* Allow host/peripheral driver load to finish */
1359 stop_peripheral(otg);
1361 otg_dbg(otg, "Thread running\n");
1363 enum usb_otg_state next = OTG_STATE_UNDEFINED;
1365 otg_vdbg(otg, "Main thread entering state\n");
1367 switch (otg->otg.state) {
1368 case OTG_STATE_UNDEFINED:
1369 otg_dbg(otg, "OTG_STATE_UNDEFINED\n");
1370 next = do_connector_id_status(otg);
1373 case OTG_STATE_A_IDLE:
1374 otg_dbg(otg, "OTG_STATE_A_IDLE\n");
1375 stop_peripheral(otg);
1377 if (prev == OTG_STATE_UNDEFINED)
1378 next = OTG_STATE_A_WAIT_VRISE;
1380 next = do_a_idle(otg);
1383 case OTG_STATE_A_WAIT_VRISE:
1384 otg_dbg(otg, "OTG_STATE_A_WAIT_VRISE\n");
1385 next = do_a_wait_vrise(otg);
1388 case OTG_STATE_A_WAIT_BCON:
1389 otg_dbg(otg, "OTG_STATE_A_WAIT_BCON\n");
1390 next = do_a_wait_bconn(otg);
1393 case OTG_STATE_A_HOST:
1394 otg_dbg(otg, "OTG_STATE_A_HOST\n");
1395 stop_peripheral(otg);
1396 next = do_a_host(otg);
1397 /* Don't stop the host here if we are going into
1398 * A_SUSPEND. We need to delay that until later. It
1399 * will be stopped when coming out of A_SUSPEND
1402 if (next != OTG_STATE_A_SUSPEND)
1406 case OTG_STATE_A_SUSPEND:
1407 otg_dbg(otg, "OTG_STATE_A_SUSPEND\n");
1408 next = do_a_hnp_init(otg);
1410 /* Stop the host. */
1414 case OTG_STATE_A_WAIT_VFALL:
1415 otg_dbg(otg, "OTG_STATE_A_WAIT_VFALL\n");
1416 next = do_a_wait_vfall(otg);
1420 case OTG_STATE_A_PERIPHERAL:
1421 otg_dbg(otg, "OTG_STATE_A_PERIPHERAL\n");
1423 start_peripheral(otg);
1424 next = do_a_peripheral(otg);
1425 stop_peripheral(otg);
1428 case OTG_STATE_B_IDLE:
1429 otg_dbg(otg, "OTG_STATE_B_IDLE\n");
1430 next = do_b_idle(otg);
1433 case OTG_STATE_B_PERIPHERAL:
1434 otg_dbg(otg, "OTG_STATE_B_PERIPHERAL\n");
1436 start_peripheral(otg);
1437 next = do_b_peripheral(otg);
1438 stop_peripheral(otg);
1441 case OTG_STATE_B_SRP_INIT:
1442 otg_dbg(otg, "OTG_STATE_B_SRP_INIT\n");
1443 otg_read(otg, OSTS);
1444 next = do_b_srp_init(otg);
1447 case OTG_STATE_B_WAIT_ACON:
1448 otg_dbg(otg, "OTG_STATE_B_WAIT_ACON\n");
1449 next = do_b_wait_acon(otg);
1452 case OTG_STATE_B_HOST:
1453 otg_dbg(otg, "OTG_STATE_B_HOST\n");
1454 next = do_b_host(otg);
1459 otg_err(otg, "Unknown state %d, sleeping...\n",
1461 sleep_main_thread(otg);
1465 prev = otg->otg.state;
1466 otg->otg.state = next;
1467 if (kthread_should_stop())
1471 otg->main_thread = NULL;
1472 otg_dbg(otg, "OTG main thread exiting....\n");
1477 static void start_main_thread(struct dwc3_otg *otg)
1479 if (!otg->main_thread && otg->otg.gadget && otg->otg.host) {
1480 otg_dbg(otg, "Starting OTG main thread\n");
1481 otg->main_thread = kthread_create(otg_main_thread, otg, "otg");
1482 wake_up_process(otg->main_thread);
1486 static inline struct dwc3_otg *otg_to_dwc3_otg(struct usb_otg *x)
1488 return container_of(x, struct dwc3_otg, otg);
1491 static irqreturn_t dwc3_otg_irq(int irq, void *_otg)
1493 struct dwc3_otg *otg;
1499 u32 otg_mask = OEVT_ALL;
1504 otg = (struct dwc3_otg *)_otg;
1506 oevt = otg_read(otg, OEVT);
1507 osts = otg_read(otg, OSTS);
1508 octl = otg_read(otg, OCTL);
1509 ocfg = otg_read(otg, OCFG);
1510 oevten = otg_read(otg, OEVTEN);
1512 /* Clear handled events */
1513 otg_write(otg, OEVT, oevt);
1515 otg_vdbg(otg, "\n");
1516 otg_vdbg(otg, " oevt = %08x\n", oevt);
1517 otg_vdbg(otg, " osts = %08x\n", osts);
1518 otg_vdbg(otg, " octl = %08x\n", octl);
1519 otg_vdbg(otg, " ocfg = %08x\n", ocfg);
1520 otg_vdbg(otg, " oevten = %08x\n", oevten);
1522 otg_vdbg(otg, "oevt[DeviceMode] = %s\n",
1523 oevt & OEVT_DEV_MOD_EVNT ? "Device" : "Host");
1525 if (oevt & OEVT_CONN_ID_STS_CHNG_EVNT)
1526 otg_dbg(otg, "Connector ID Status Change Event\n");
1527 if (oevt & OEVT_HOST_ROLE_REQ_INIT_EVNT)
1528 otg_dbg(otg, "Host Role Request Init Notification Event\n");
1529 if (oevt & OEVT_HOST_ROLE_REQ_CONFIRM_EVNT)
1530 otg_dbg(otg, "Host Role Request Confirm Notification Event\n");
1531 if (oevt & OEVT_A_DEV_B_DEV_HOST_END_EVNT)
1532 otg_dbg(otg, "A-Device B-Host End Event\n");
1533 if (oevt & OEVT_A_DEV_HOST_EVNT)
1534 otg_dbg(otg, "A-Device Host Event\n");
1535 if (oevt & OEVT_A_DEV_HNP_CHNG_EVNT)
1536 otg_dbg(otg, "A-Device HNP Change Event\n");
1537 if (oevt & OEVT_A_DEV_SRP_DET_EVNT)
1538 otg_dbg(otg, "A-Device SRP Detect Event\n");
1539 if (oevt & OEVT_A_DEV_SESS_END_DET_EVNT)
1540 otg_dbg(otg, "A-Device Session End Detected Event\n");
1541 if (oevt & OEVT_B_DEV_B_HOST_END_EVNT)
1542 otg_dbg(otg, "B-Device B-Host End Event\n");
1543 if (oevt & OEVT_B_DEV_HNP_CHNG_EVNT)
1544 otg_dbg(otg, "B-Device HNP Change Event\n");
1545 if (oevt & OEVT_B_DEV_SES_VLD_DET_EVNT)
1546 otg_dbg(otg, "B-Device Session Valid Detect Event\n");
1547 if (oevt & OEVT_B_DEV_VBUS_CHNG_EVNT)
1548 otg_dbg(otg, "B-Device VBUS Change Event\n");
1550 if (oevt & otg_mask) {
1551 /* Pass event to main thread */
1552 spin_lock(&otg->lock);
1553 otg->otg_events |= oevt;
1554 wakeup_main_thread(otg);
1555 spin_unlock(&otg->lock);
1562 static void hnp_polling_work(struct work_struct *w)
1564 struct dwc3_otg *otg = container_of(w, struct dwc3_otg,
1566 struct usb_bus *bus;
1567 struct usb_device *udev;
1568 struct usb_hcd *hcd;
1573 hcd = container_of(otg->otg.host, struct usb_hcd, self);
1581 udev = usb_hub_find_child(bus->root_hub, bus->otg_port);
1585 otgstatus = kmalloc(sizeof(*otgstatus), GFP_NOIO);
1589 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1590 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RECIP_DEVICE,
1591 0, 0xf000, otgstatus, sizeof(*otgstatus),
1592 USB_CTRL_GET_TIMEOUT);
1594 if (ret == sizeof(*otgstatus) && (*otgstatus & 0x1)) {
1595 /* enable HNP before suspend, it's simpler */
1597 udev->bus->b_hnp_enable = 1;
1598 err = usb_control_msg(udev,
1599 usb_sndctrlpipe(udev, 0),
1600 USB_REQ_SET_FEATURE, 0,
1601 udev->bus->b_hnp_enable
1602 ? USB_DEVICE_B_HNP_ENABLE
1603 : USB_DEVICE_A_ALT_HNP_SUPPORT,
1604 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1607 /* OTG MESSAGE: report errors here,
1608 * customize to match your product.
1610 otg_info(otg, "ERROR : Device no response\n");
1611 dev_info(&udev->dev, "can't set HNP mode: %d\n",
1613 udev->bus->b_hnp_enable = 0;
1614 if (le16_to_cpu(udev->descriptor.idVendor) == 0x1a0a) {
1615 if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND)
1617 dev_dbg(&udev->dev, "HNP fail, %d\n",
1621 /* Device wants role-switch, suspend the bus. */
1622 static struct usb_phy *phy;
1624 phy = usb_get_phy(USB_PHY_TYPE_USB3);
1625 otg_start_hnp(phy->otg);
1628 if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND) < 0)
1629 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1631 } else if (ret < 0) {
1632 udev->bus->b_hnp_enable = 1;
1633 err = usb_control_msg(udev,
1634 usb_sndctrlpipe(udev, 0),
1635 USB_REQ_SET_FEATURE, 0,
1636 USB_DEVICE_B_HNP_ENABLE,
1637 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1638 if (usb_port_suspend(udev, PMSG_AUTO_SUSPEND) < 0)
1639 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1641 schedule_delayed_work(&otg->hp_work, 1 * HZ);
1647 static int dwc3_otg_notify_connect(struct usb_phy *phy,
1648 enum usb_device_speed speed)
1650 struct usb_bus *bus;
1651 struct usb_device *udev;
1652 struct usb_hcd *hcd;
1653 struct dwc3_otg *otg;
1656 otg = otg_to_dwc3_otg(phy->otg);
1658 hcd = container_of(phy->otg->host, struct usb_hcd, self);
1666 udev = usb_hub_find_child(bus->root_hub, bus->otg_port);
1671 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1672 * to wake us after we've powered off VBUS; and HNP, switching roles
1673 * "host" to "peripheral". The OTG descriptor helps figure this out.
1675 if (udev->config && udev->parent == udev->bus->root_hub) {
1676 struct usb_otg20_descriptor *desc = NULL;
1678 /* descriptor may appear anywhere in config */
1679 err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
1680 le16_to_cpu(udev->config[0].desc.wTotalLength),
1681 USB_DT_OTG, (void **) &desc);
1682 if (err || !(desc->bmAttributes & USB_OTG_HNP))
1685 if (udev->portnum == udev->bus->otg_port) {
1686 INIT_DELAYED_WORK(&otg->hp_work,
1688 schedule_delayed_work(&otg->hp_work, HZ);
1696 static int dwc3_otg_notify_disconnect(struct usb_phy *phy,
1697 enum usb_device_speed speed)
1699 struct dwc3_otg *otg;
1701 otg = otg_to_dwc3_otg(phy->otg);
1703 if (work_pending(&otg->hp_work.work)) {
1704 while (!cancel_delayed_work(&otg->hp_work))
1710 static void dwc3_otg_set_peripheral(struct usb_otg *_otg, int yes)
1712 struct dwc3_otg *otg;
1717 otg = otg_to_dwc3_otg(_otg);
1721 if (otg->hwparams6 == 0xdeadbeef)
1722 otg->hwparams6 = otg_read(otg, GHWPARAMS6);
1725 stop_peripheral(otg);
1728 set_peri_mode(otg, yes);
1730 EXPORT_SYMBOL(dwc3_otg_set_peripheral);
1732 static int dwc3_otg_set_periph(struct usb_otg *_otg, struct usb_gadget *gadget)
1734 struct dwc3_otg *otg;
1739 otg = otg_to_dwc3_otg(_otg);
1742 if ((long)gadget == 1) {
1743 dwc3_otg_set_peripheral(_otg, 1);
1748 otg->otg.gadget = NULL;
1752 otg->otg.gadget = gadget;
1753 otg->otg.gadget->hnp_polling_support = 1;
1754 otg->otg.state = OTG_STATE_B_IDLE;
1756 start_main_thread(otg);
1760 static int dwc3_otg_set_host(struct usb_otg *_otg, struct usb_bus *host)
1762 struct dwc3_otg *otg;
1763 struct usb_hcd *hcd;
1764 struct xhci_hcd *xhci;
1769 otg = otg_to_dwc3_otg(_otg);
1772 if ((long)host == 1) {
1773 dwc3_otg_set_peripheral(_otg, 0);
1778 otg->otg.host = NULL;
1783 hcd = container_of(host, struct usb_hcd, self);
1784 xhci = hcd_to_xhci(hcd);
1785 otg_dbg(otg, "hcd=%p xhci=%p\n", hcd, xhci);
1787 hcd->self.otg_port = 1;
1788 if (xhci->shared_hcd) {
1789 xhci->shared_hcd->self.otg_port = 1;
1790 otg_dbg(otg, "shared_hcd=%p\n", xhci->shared_hcd);
1793 otg->otg.host = host;
1794 otg->hcd_irq = hcd->irq;
1795 otg_dbg(otg, "host=%p irq=%d\n", otg->otg.host, otg->hcd_irq);
1798 otg->host_started = 1;
1800 start_main_thread(otg);
1804 static int dwc3_otg_start_srp(struct usb_otg *x)
1806 unsigned long flags;
1807 struct dwc3_otg *otg;
1812 otg = otg_to_dwc3_otg(x);
1815 if (!otg->otg.host || !otg->otg.gadget)
1818 spin_lock_irqsave(&otg->lock, flags);
1819 otg->user_events |= USER_SRP_EVENT;
1820 wakeup_main_thread(otg);
1821 spin_unlock_irqrestore(&otg->lock, flags);
1825 static int dwc3_otg_start_hnp(struct usb_otg *x)
1827 unsigned long flags;
1828 struct dwc3_otg *otg;
1833 otg = otg_to_dwc3_otg(x);
1836 if (!otg->otg.host || !otg->otg.gadget)
1839 spin_lock_irqsave(&otg->lock, flags);
1840 otg->user_events |= USER_HNP_EVENT;
1841 wakeup_main_thread(otg);
1842 spin_unlock_irqrestore(&otg->lock, flags);
1846 static int dwc3_otg_end_session(struct usb_otg *x)
1848 unsigned long flags;
1849 struct dwc3_otg *otg;
1854 otg = otg_to_dwc3_otg(x);
1857 if (!otg->otg.host || !otg->otg.gadget)
1860 spin_lock_irqsave(&otg->lock, flags);
1861 otg->user_events |= USER_END_SESSION;
1862 wakeup_main_thread(otg);
1863 spin_unlock_irqrestore(&otg->lock, flags);
1867 static int otg_end_session(struct usb_otg *otg)
1869 return dwc3_otg_end_session(otg);
1871 EXPORT_SYMBOL(otg_end_session);
1873 static int dwc3_otg_received_host_release(struct usb_otg *x)
1875 struct dwc3_otg *otg;
1876 unsigned long flags;
1881 otg = otg_to_dwc3_otg(x);
1884 if (!otg->otg.host || !otg->otg.gadget)
1887 spin_lock_irqsave(&otg->lock, flags);
1888 otg->user_events |= PCD_RECEIVED_HOST_RELEASE_EVENT;
1889 wakeup_main_thread(otg);
1890 spin_unlock_irqrestore(&otg->lock, flags);
1894 int otg_host_release(struct usb_otg *otg)
1896 return dwc3_otg_received_host_release(otg);
1898 EXPORT_SYMBOL(otg_host_release);
1900 static void dwc3_otg_enable_irq(struct dwc3_otg *otg)
1904 /* Enable OTG IRQs */
1907 otg_write(otg, OEVTEN, reg);
1910 static ssize_t store_srp(struct device *dev, struct device_attribute *attr,
1911 const char *buf, size_t count)
1913 struct usb_phy *phy;
1914 struct usb_otg *otg;
1916 phy = usb_get_phy(USB_PHY_TYPE_USB3);
1917 if (IS_ERR(phy) || !phy) {
1933 static DEVICE_ATTR(srp, 0220, NULL, store_srp);
1935 static ssize_t store_end(struct device *dev, struct device_attribute *attr,
1936 const char *buf, size_t count)
1938 struct usb_phy *phy;
1939 struct usb_otg *otg;
1941 phy = usb_get_phy(USB_PHY_TYPE_USB3);
1942 if (IS_ERR(phy) || !phy) {
1954 otg_end_session(otg);
1958 static DEVICE_ATTR(end, 0220, NULL, store_end);
1960 static ssize_t store_hnp(struct device *dev, struct device_attribute *attr,
1961 const char *buf, size_t count)
1963 struct dwc3 *dwc = dev_get_drvdata(dev);
1964 struct usb_phy *phy = usb_get_phy(USB_PHY_TYPE_USB3);
1965 struct usb_otg *otg;
1967 dev_dbg(dwc->dev, "%s()\n", __func__);
1969 if (IS_ERR(phy) || !phy) {
1970 dev_info(dwc->dev, "NO PHY!!\n");
1978 dev_info(dwc->dev, "NO OTG!!\n");
1983 dev_info(dev, "b_hnp_enable is FALSE\n");
1984 dwc->gadget.host_request_flag = 1;
1989 static DEVICE_ATTR(hnp, 0220, NULL, store_hnp);
1991 static ssize_t store_hnp_end(struct device *dev, struct device_attribute *attr,
1992 const char *buf, size_t count)
1994 struct usb_phy *phy;
1995 struct usb_otg *otg;
1996 unsigned long flags;
1997 struct dwc3_otg *dwc_otg;
1999 phy = usb_get_phy(USB_PHY_TYPE_USB3);
2000 if (IS_ERR(phy) || !phy) {
2012 dwc_otg = otg_to_dwc3_otg(otg);
2014 spin_lock_irqsave(&dwc_otg->lock, flags);
2015 dwc_otg->user_events |= USER_HNP_END_SESSION;
2016 wakeup_main_thread(dwc_otg);
2017 spin_unlock_irqrestore(&dwc_otg->lock, flags);
2022 static DEVICE_ATTR(hnp_end, 0220, NULL, store_hnp_end);
2024 static ssize_t store_a_hnp_reqd(struct device *dev,
2025 struct device_attribute *attr, const char *buf, size_t count)
2027 struct dwc3 *dwc = dev_get_drvdata(dev);
2028 struct dwc3_otg *otg;
2034 static DEVICE_ATTR(a_hnp_reqd, 0220, NULL, store_a_hnp_reqd);
2036 static ssize_t store_print_dbg(struct device *dev,
2037 struct device_attribute *attr, const char *buf, size_t count)
2039 struct dwc3 *dwc = dev_get_drvdata(dev);
2040 struct dwc3_otg *otg;
2043 print_debug_regs(otg);
2047 static DEVICE_ATTR(print_dbg, 0220, NULL, store_print_dbg);
2049 void dwc_usb3_remove_dev_files(struct device *dev)
2051 device_remove_file(dev, &dev_attr_print_dbg);
2052 device_remove_file(dev, &dev_attr_a_hnp_reqd);
2053 device_remove_file(dev, &dev_attr_end);
2054 device_remove_file(dev, &dev_attr_srp);
2055 device_remove_file(dev, &dev_attr_hnp);
2056 device_remove_file(dev, &dev_attr_hnp_end);
2059 int dwc3_otg_create_dev_files(struct device *dev)
2063 retval = device_create_file(dev, &dev_attr_hnp);
2067 retval = device_create_file(dev, &dev_attr_hnp_end);
2071 retval = device_create_file(dev, &dev_attr_srp);
2075 retval = device_create_file(dev, &dev_attr_end);
2079 retval = device_create_file(dev, &dev_attr_a_hnp_reqd);
2083 retval = device_create_file(dev, &dev_attr_print_dbg);
2090 dev_err(dev, "Failed to create one or more sysfs files!!\n");
2094 void dwc3_otg_init(struct dwc3 *dwc)
2096 struct dwc3_otg *otg;
2100 dev_dbg(dwc->dev, "dwc3_otg_init\n");
2103 * GHWPARAMS6[10] bit is SRPSupport.
2104 * This bit also reflects DWC_USB3_EN_OTG
2106 reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
2107 if (!(reg & GHWPARAMS6_SRP_SUPPORT_ENABLED)) {
2109 * No OTG support in the HW core.
2110 * We return 0 to indicate no error, since this is acceptable
2111 * situation, just continue probe the dwc3 driver without otg.
2113 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
2117 otg = kzalloc(sizeof(*otg), GFP_KERNEL);
2119 dev_err(otg->dev, "failed to allocate memroy\n");
2124 otg->dev = dwc->dev;
2127 otg->regs = dwc->regs - DWC3_GLOBALS_REGS_START;
2128 otg->otg.usb_phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
2129 otg->otg.usb_phy->dev = otg->dev;
2130 otg->otg.usb_phy->label = "dwc3_otg";
2131 otg->otg.state = OTG_STATE_UNDEFINED;
2132 otg->otg.usb_phy->otg = &otg->otg;
2133 otg->otg.usb_phy->notify_connect = dwc3_otg_notify_connect;
2134 otg->otg.usb_phy->notify_disconnect = dwc3_otg_notify_disconnect;
2136 otg->otg.start_srp = dwc3_otg_start_srp;
2137 otg->otg.start_hnp = dwc3_otg_start_hnp;
2138 otg->otg.set_host = dwc3_otg_set_host;
2139 otg->otg.set_peripheral = dwc3_otg_set_periph;
2141 otg->hwparams6 = reg;
2142 otg->state = OTG_STATE_UNDEFINED;
2144 spin_lock_init(&otg->lock);
2145 init_waitqueue_head(&otg->main_wq);
2147 err = usb_add_phy(otg->otg.usb_phy, USB_PHY_TYPE_USB3);
2149 dev_err(otg->dev, "can't register transceiver, err: %d\n",
2154 otg->irq = platform_get_irq(to_platform_device(otg->dev), 1);
2156 dwc3_otg_create_dev_files(otg->dev);
2158 /* Set irq handler */
2159 err = request_irq(otg->irq, dwc3_otg_irq, IRQF_SHARED, "dwc3_otg", otg);
2161 dev_err(otg->otg.usb_phy->dev, "failed to request irq #%d --> %d\n",
2166 dwc3_otg_enable_irq(otg);
2168 err = dwc3_gadget_init(dwc);
2170 if (err != -EPROBE_DEFER)
2171 dev_err(otg->otg.usb_phy->dev,
2172 "failed to initialize gadget\n");
2176 err = dwc3_host_init(dwc);
2178 if (err != -EPROBE_DEFER)
2179 dev_err(otg->otg.usb_phy->dev,
2180 "failed to initialize host\n");
2187 kfree(otg->otg.usb_phy);
2191 void dwc3_otg_exit(struct dwc3 *dwc)
2193 struct dwc3_otg *otg = dwc->otg;
2196 usb_remove_phy(otg->otg.usb_phy);
2197 kfree(otg->otg.usb_phy);