From patchwork Wed Sep 2 14:24:20 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Quadros X-Patchwork-Id: 52979 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f71.google.com (mail-la0-f71.google.com [209.85.215.71]) by patches.linaro.org (Postfix) with ESMTPS id E90B02300B for ; Wed, 2 Sep 2015 14:26:52 +0000 (UTC) Received: by lagj9 with SMTP id j9sf5114009lag.0 for ; Wed, 02 Sep 2015 07:26:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type:sender:precedence :list-id:x-original-sender:x-original-authentication-results :mailing-list:list-post:list-help:list-archive:list-unsubscribe; bh=WPZRZIVjzSbisKrYUti8iSmSU5oA6Ey1isb3Q/bNEPs=; b=kB+UacQFOtFdV8ZH+hhCqlB24mO3gaYjMXpuwr/gK0/1w1D4Rg7rp+e/NMfUO8BG4z 5MRYQ9rJ+9xJDyckXgxv0lEsRUXV29GE6UBCr2z3XLlbi/X+Xzq8Ng+Oa72kEZm3ZwQU +qoJgm+oEJGoSisaE14G74wgL+Eu0DOQijUn+0OiwoI9yLVymwlvpZVqH85mW9Bxvokh HmDySUWDu30+mPySpJiZ5Pzqbz042MIXWQNryi02lJYqtZwZvg4sSxmo1d4skjDS8r++ PZAvWYuDZjXNjxp8je7AJxC9C+RMG+2WhMYcEZxccYsyZczZ5hCRP/U32a9zn+fPOviZ YWew== X-Gm-Message-State: ALoCoQnAhhlsHZfxPbAfswdr2KcGIXZ5UE5kZPo7wO/mIp1f/Xxx/kTvEFcCLOch5t9bC46PiufY X-Received: by 10.112.140.195 with SMTP id ri3mr9247687lbb.22.1441204011901; Wed, 02 Sep 2015 07:26:51 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.6.168 with SMTP id c8ls73936laa.106.gmail; Wed, 02 Sep 2015 07:26:51 -0700 (PDT) X-Received: by 10.152.36.226 with SMTP id t2mr16270992laj.6.1441204011760; Wed, 02 Sep 2015 07:26:51 -0700 (PDT) Received: from mail-la0-f48.google.com (mail-la0-f48.google.com. [209.85.215.48]) by mx.google.com with ESMTPS id zn4si19776123lbb.177.2015.09.02.07.26.51 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Sep 2015 07:26:51 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.48 as permitted sender) client-ip=209.85.215.48; Received: by lanb10 with SMTP id b10so8253332lan.3 for ; Wed, 02 Sep 2015 07:26:51 -0700 (PDT) X-Received: by 10.112.166.106 with SMTP id zf10mr16206164lbb.36.1441204011499; Wed, 02 Sep 2015 07:26:51 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.164.42 with SMTP id yn10csp615881lbb; Wed, 2 Sep 2015 07:26:50 -0700 (PDT) X-Received: by 10.68.235.99 with SMTP id ul3mr41375514pbc.138.1441203998158; Wed, 02 Sep 2015 07:26:38 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id vg7si5404077pbc.225.2015.09.02.07.26.37; Wed, 02 Sep 2015 07:26:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-usb-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753894AbbIBO0f (ORCPT + 4 others); Wed, 2 Sep 2015 10:26:35 -0400 Received: from bear.ext.ti.com ([192.94.94.41]:56910 "EHLO bear.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752763AbbIBOYt (ORCPT ); Wed, 2 Sep 2015 10:24:49 -0400 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by bear.ext.ti.com (8.13.7/8.13.7) with ESMTP id t82EOhh9014579; Wed, 2 Sep 2015 09:24:44 -0500 Received: from DFLE73.ent.ti.com (dfle73.ent.ti.com [128.247.5.110]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id t82EOhSa005368; Wed, 2 Sep 2015 09:24:43 -0500 Received: from dflp32.itg.ti.com (10.64.6.15) by DFLE73.ent.ti.com (128.247.5.110) with Microsoft SMTP Server id 14.3.224.2; Wed, 2 Sep 2015 09:24:43 -0500 Received: from rockdesk.ti.com (ileax41-snat.itg.ti.com [10.172.224.153]) by dflp32.itg.ti.com (8.14.3/8.13.8) with ESMTP id t82EOPFS024150; Wed, 2 Sep 2015 09:24:41 -0500 From: Roger Quadros To: CC: , , , , , , , , Roger Quadros Subject: [PATCH v4 5/9] usb: dwc3: core: make dual-role work with OTG irq Date: Wed, 2 Sep 2015 17:24:20 +0300 Message-ID: <1441203864-15786-6-git-send-email-rogerq@ti.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1441203864-15786-1-git-send-email-rogerq@ti.com> References: <1441203864-15786-1-git-send-email-rogerq@ti.com> MIME-Version: 1.0 Sender: linux-usb-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: rogerq@ti.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.48 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , If the ID pin event is not available over extcon then we rely on the OTG controller to provide us ID and VBUS information. We still don't support any OTG features but just dual-role operation. Signed-off-by: Roger Quadros --- drivers/usb/dwc3/core.c | 217 ++++++++++++++++++++++++++++++++++++++++++++---- drivers/usb/dwc3/core.h | 3 + 2 files changed, 205 insertions(+), 15 deletions(-) diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index 38b31df..632ee53 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -704,6 +704,63 @@ static int dwc3_core_get_phy(struct dwc3 *dwc) return 0; } +/* Get OTG events and sync it to OTG fsm */ +static void dwc3_otg_fsm_sync(struct dwc3 *dwc) +{ + u32 reg; + int id, vbus; + + reg = dwc3_readl(dwc->regs, DWC3_OSTS); + dev_dbg(dwc->dev, "otgstatus 0x%x\n", reg); + + id = !!(reg & DWC3_OSTS_CONIDSTS); + vbus = !!(reg & DWC3_OSTS_BSESVLD); + + if (id != dwc->fsm->id || vbus != dwc->fsm->b_sess_vld) { + dev_dbg(dwc->dev, "id %d vbus %d\n", id, vbus); + dwc->fsm->id = id; + dwc->fsm->b_sess_vld = vbus; + usb_otg_sync_inputs(dwc->fsm); + } +} + +static irqreturn_t dwc3_otg_thread_irq(int irq, void *_dwc) +{ + struct dwc3 *dwc = _dwc; + unsigned long flags; + irqreturn_t ret = IRQ_NONE; + + spin_lock_irqsave(&dwc->lock, flags); + dwc3_otg_fsm_sync(dwc); + /* unmask interrupts */ + dwc3_writel(dwc->regs, DWC3_OEVTEN, dwc->oevten); + spin_unlock_irqrestore(&dwc->lock, flags); + + return ret; +} + +static irqreturn_t dwc3_otg_irq(int irq, void *_dwc) +{ + struct dwc3 *dwc = _dwc; + irqreturn_t ret = IRQ_NONE; + u32 reg; + + spin_lock(&dwc->lock); + + reg = dwc3_readl(dwc->regs, DWC3_OEVT); + if (reg) { + dwc3_writel(dwc->regs, DWC3_OEVT, reg); + /* mask interrupts till processed */ + dwc->oevten = dwc3_readl(dwc->regs, DWC3_OEVTEN); + dwc3_writel(dwc->regs, DWC3_OEVTEN, 0); + ret = IRQ_WAKE_THREAD; + } + + spin_unlock(&dwc->lock); + + return ret; +} + /* --------------------- Dual-Role management ------------------------------- */ static void dwc3_drd_fsm_sync(struct dwc3 *dwc) @@ -728,15 +785,44 @@ static int dwc3_drd_start_host(struct otg_fsm *fsm, int on) { struct device *dev = usb_otg_fsm_to_dev(fsm); struct dwc3 *dwc = dev_get_drvdata(dev); + u32 reg; dev_dbg(dwc->dev, "%s: %d\n", __func__, on); + if (dwc->edev) { + if (on) { + dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); + /* start the HCD */ + usb_otg_start_host(fsm, true); + } else { + /* stop the HCD */ + usb_otg_start_host(fsm, false); + } + + return 0; + } + + /* switch OTG core */ if (on) { - dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); + /* OCTL.PeriMode = 0 */ + reg = dwc3_readl(dwc->regs, DWC3_OCTL); + reg &= ~DWC3_OCTL_PERIMODE; + dwc3_writel(dwc->regs, DWC3_OCTL, reg); + /* unconditionally turn on VBUS */ + reg |= DWC3_OCTL_PRTPWRCTL; + dwc3_writel(dwc->regs, DWC3_OCTL, reg); /* start the HCD */ usb_otg_start_host(fsm, true); } else { /* stop the HCD */ usb_otg_start_host(fsm, false); + /* turn off VBUS */ + reg = dwc3_readl(dwc->regs, DWC3_OCTL); + reg &= ~DWC3_OCTL_PRTPWRCTL; + dwc3_writel(dwc->regs, DWC3_OCTL, reg); + /* OCTL.PeriMode = 1 */ + reg = dwc3_readl(dwc->regs, DWC3_OCTL); + reg |= DWC3_OCTL_PERIMODE; + dwc3_writel(dwc->regs, DWC3_OCTL, reg); } return 0; @@ -746,15 +832,48 @@ static int dwc3_drd_start_gadget(struct otg_fsm *fsm, int on) { struct device *dev = usb_otg_fsm_to_dev(fsm); struct dwc3 *dwc = dev_get_drvdata(dev); + u32 reg; dev_dbg(dwc->dev, "%s: %d\n", __func__, on); - if (on) { - dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); + if (on) dwc3_event_buffers_setup(dwc); + if (dwc->edev) { + if (on) { + dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); + usb_otg_start_gadget(fsm, true); + } else { + usb_otg_start_gadget(fsm, false); + } + + return 0; + } + + /* switch OTG core */ + if (on) { + /* OCTL.PeriMode = 1 */ + reg = dwc3_readl(dwc->regs, DWC3_OCTL); + reg |= DWC3_OCTL_PERIMODE; + dwc3_writel(dwc->regs, DWC3_OCTL, reg); + /* GUSB2PHYCFG0.SusPHY = 1 */ + if (!dwc->dis_u2_susphy_quirk) { + reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); + reg |= DWC3_GUSB2PHYCFG_SUSPHY; + dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); + } /* start the UDC */ usb_otg_start_gadget(fsm, true); } else { + /* GUSB2PHYCFG0.SusPHY=0 */ + if (!dwc->dis_u2_susphy_quirk) { + reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); + reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; + dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); + } + /* OCTL.PeriMode = 1 */ + reg = dwc3_readl(dwc->regs, DWC3_OCTL); + reg |= DWC3_OCTL_PERIMODE; + dwc3_writel(dwc->regs, DWC3_OCTL, reg); /* stop the UDC */ usb_otg_start_gadget(fsm, false); } @@ -777,10 +896,30 @@ static int dwc3_drd_notifier(struct notifier_block *nb, return NOTIFY_DONE; } +static int dwc3_drd_register(struct dwc3 *dwc) +{ + int ret; + + /* register parent as DRD device with OTG core */ + dwc->fsm = usb_otg_register(dwc->dev, &dwc->otg_config); + if (IS_ERR(dwc->fsm)) { + ret = PTR_ERR(dwc->fsm); + if (ret == -ENOTSUPP) + dev_err(dwc->dev, "CONFIG_USB_OTG needed for dual-role\n"); + else + dev_err(dwc->dev, "Failed to register with OTG core\n"); + + return ret; + } + + return 0; +} + static int dwc3_drd_init(struct dwc3 *dwc) { int ret, id, vbus; struct usb_otg_caps *otgcaps = &dwc->otg_config.otg_caps; + u32 reg; otgcaps->otg_rev = 0; otgcaps->hnp_support = false; @@ -788,9 +927,10 @@ static int dwc3_drd_init(struct dwc3 *dwc) otgcaps->adp_support = false; dwc->otg_config.fsm_ops = &dwc3_drd_ops; + /* If extcon device is not present we rely on OTG core for ID event */ if (!dwc->edev) { - dev_err(dwc->dev, "No extcon device found for OTG mode\n"); - return -ENODEV; + dev_dbg(dwc->dev, "No extcon device found for OTG mode\n"); + goto try_otg_core; } dwc->otg_nb.notifier_call = dwc3_drd_notifier; @@ -818,17 +958,9 @@ static int dwc3_drd_init(struct dwc3 *dwc) goto fail; } - /* register as DRD device with OTG core */ - dwc->fsm = usb_otg_register(dwc->dev, &dwc->otg_config); - if (IS_ERR(dwc->fsm)) { - ret = PTR_ERR(dwc->fsm); - if (ret == -ENOTSUPP) - dev_err(dwc->dev, "CONFIG_USB_OTG needed for dual-role\n"); - else - dev_err(dwc->dev, "Failed to register with OTG core\n"); - + ret = dwc3_drd_register(dwc); + if (ret) goto fail; - } dwc3_drd_fsm_sync(dwc); @@ -839,6 +971,61 @@ extcon_fail: extcon_unregister_notifier(dwc->edev, EXTCON_USB, &dwc->otg_nb); return ret; + +try_otg_core: + ret = dwc3_drd_register(dwc); + if (ret) + return ret; + + /* disable all irqs */ + dwc3_writel(dwc->regs, DWC3_OEVTEN, 0); + /* clear all events */ + dwc3_writel(dwc->regs, DWC3_OEVT, ~0); + + ret = request_threaded_irq(dwc->otg_irq, dwc3_otg_irq, + dwc3_otg_thread_irq, + IRQF_SHARED, "dwc3-otg", dwc); + if (ret) { + dev_err(dwc->dev, "failed to request irq #%d --> %d\n", + dwc->otg_irq, ret); + ret = -ENODEV; + goto error; + } + + /* we need to set OTG to get events from OTG core */ + dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); + /* GUSB2PHYCFG0.SusPHY=0 */ + if (!dwc->dis_u2_susphy_quirk) { + reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); + reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; + dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); + } + + /* Initialize OTG registers */ + /* + * Prevent host/device reset from resetting OTG core. + * If we don't do this then xhci_reset (USBCMD.HCRST) will reset + * the signal outputs sent to the PHY, the OTG FSM logic of the + * core and also the resets to the VBUS filters inside the core. + */ + reg = DWC3_OCFG_SFTRSTMASK; + dwc3_writel(dwc->regs, DWC3_OCFG, reg); + /* Enable ID event interrupt */ + dwc3_writel(dwc->regs, DWC3_OEVTEN, DWC3_OEVTEN_CONIDSTSCHNGEN | + DWC3_OEVTEN_BDEVVBUSCHNGE | + DWC3_OEVTEN_BDEVSESSVLDDETEN); + /* OCTL.PeriMode = 1 */ + dwc3_writel(dwc->regs, DWC3_OCTL, DWC3_OCTL_PERIMODE); + + dwc3_otg_fsm_sync(dwc); + usb_otg_sync_inputs(dwc->fsm); + + return 0; + +error: + usb_otg_unregister(dwc->dev); + + return ret; } static void dwc3_drd_exit(struct dwc3 *dwc) diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index bd32cb2..129ef37 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -736,6 +736,7 @@ struct dwc3_scratchpad_array { * @gadget_driver: pointer to the gadget driver * @regs: base address for our registers * @regs_size: address space size + * @oevten: otg interrupt enable mask copy * @nr_scratch: number of scratch buffers * @num_event_buffers: calculated number of event buffers * @u1u2: only used on revisions <1.83a for workaround @@ -858,6 +859,8 @@ struct dwc3 { u32 dcfg; u32 gctl; + u32 oevten; + u32 nr_scratch; u32 num_event_buffers; u32 u1u2;