UPSTREAM: usb: dwc3: core: move fladj to dwc3 structure
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc3 / core.c
index 22b4797383cd0021953b58e1f8d60178eeb89389..acfe949b6a6862b610466977764d9c915fcba053 100644 (file)
@@ -60,6 +60,20 @@ void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
        dwc3_writel(dwc->regs, DWC3_GCTL, reg);
 }
 
+u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type)
+{
+       struct dwc3             *dwc = dep->dwc;
+       u32                     reg;
+
+       dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE,
+                       DWC3_GDBGFIFOSPACE_NUM(dep->number) |
+                       DWC3_GDBGFIFOSPACE_TYPE(type));
+
+       reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE);
+
+       return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg);
+}
+
 /**
  * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  * @dwc: pointer to our context structure
@@ -67,23 +81,9 @@ void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
 static int dwc3_core_soft_reset(struct dwc3 *dwc)
 {
        u32             reg;
+       int             retries = 1000;
        int             ret;
 
-       /* Before Resetting PHY, put Core in Reset */
-       reg = dwc3_readl(dwc->regs, DWC3_GCTL);
-       reg |= DWC3_GCTL_CORESOFTRESET;
-       dwc3_writel(dwc->regs, DWC3_GCTL, reg);
-
-       /* Assert USB3 PHY reset */
-       reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
-       reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
-       dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
-
-       /* Assert USB2 PHY reset */
-       reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
-       reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
-       dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
-
        usb_phy_init(dwc->usb2_phy);
        usb_phy_init(dwc->usb3_phy);
        ret = phy_init(dwc->usb2_generic_phy);
@@ -95,26 +95,28 @@ static int dwc3_core_soft_reset(struct dwc3 *dwc)
                phy_exit(dwc->usb2_generic_phy);
                return ret;
        }
-       mdelay(100);
 
-       /* Clear USB3 PHY reset */
-       reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
-       reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
-       dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
+       /*
+        * We're resetting only the device side because, if we're in host mode,
+        * XHCI driver will reset the host block. If dwc3 was configured for
+        * host-only mode, then we can return early.
+        */
+       if (dwc->dr_mode == USB_DR_MODE_HOST)
+               return 0;
 
-       /* Clear USB2 PHY reset */
-       reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
-       reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
-       dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
+       reg = dwc3_readl(dwc->regs, DWC3_DCTL);
+       reg |= DWC3_DCTL_CSFTRST;
+       dwc3_writel(dwc->regs, DWC3_DCTL, reg);
 
-       mdelay(100);
+       do {
+               reg = dwc3_readl(dwc->regs, DWC3_DCTL);
+               if (!(reg & DWC3_DCTL_CSFTRST))
+                       return 0;
 
-       /* After PHYs are stable we can take Core out of reset state */
-       reg = dwc3_readl(dwc->regs, DWC3_GCTL);
-       reg &= ~DWC3_GCTL_CORESOFTRESET;
-       dwc3_writel(dwc->regs, DWC3_GCTL, reg);
+               udelay(1);
+       } while (--retries);
 
-       return 0;
+       return -ETIMEDOUT;
 }
 
 /**
@@ -147,9 +149,8 @@ static int dwc3_soft_reset(struct dwc3 *dwc)
 /*
  * dwc3_frame_length_adjustment - Adjusts frame length if required
  * @dwc3: Pointer to our controller context structure
- * @fladj: Value of GFLADJ_30MHZ to adjust frame length
  */
-static void dwc3_frame_length_adjustment(struct dwc3 *dwc, u32 fladj)
+static void dwc3_frame_length_adjustment(struct dwc3 *dwc)
 {
        u32 reg;
        u32 dft;
@@ -157,15 +158,15 @@ static void dwc3_frame_length_adjustment(struct dwc3 *dwc, u32 fladj)
        if (dwc->revision < DWC3_REVISION_250A)
                return;
 
-       if (fladj == 0)
+       if (dwc->fladj == 0)
                return;
 
        reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
        dft = reg & DWC3_GFLADJ_30MHZ_MASK;
-       if (!dev_WARN_ONCE(dwc->dev, dft == fladj,
+       if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj,
            "request value same as default, ignoring\n")) {
                reg &= ~DWC3_GFLADJ_30MHZ_MASK;
-               reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | fladj;
+               reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj;
                dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
        }
 }
@@ -215,13 +216,10 @@ static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
 static void dwc3_free_event_buffers(struct dwc3 *dwc)
 {
        struct dwc3_event_buffer        *evt;
-       int i;
 
-       for (i = 0; i < dwc->num_event_buffers; i++) {
-               evt = dwc->ev_buffs[i];
-               if (evt)
-                       dwc3_free_one_event_buffer(dwc, evt);
-       }
+       evt = dwc->ev_buf;
+       if (evt)
+               dwc3_free_one_event_buffer(dwc, evt);
 }
 
 /**
@@ -234,27 +232,14 @@ static void dwc3_free_event_buffers(struct dwc3 *dwc)
  */
 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
 {
-       int                     num;
-       int                     i;
+       struct dwc3_event_buffer *evt;
 
-       num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
-       dwc->num_event_buffers = num;
-
-       dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
-                       GFP_KERNEL);
-       if (!dwc->ev_buffs)
-               return -ENOMEM;
-
-       for (i = 0; i < num; i++) {
-               struct dwc3_event_buffer        *evt;
-
-               evt = dwc3_alloc_one_event_buffer(dwc, length);
-               if (IS_ERR(evt)) {
-                       dev_err(dwc->dev, "can't allocate event buffer\n");
-                       return PTR_ERR(evt);
-               }
-               dwc->ev_buffs[i] = evt;
+       evt = dwc3_alloc_one_event_buffer(dwc, length);
+       if (IS_ERR(evt)) {
+               dev_err(dwc->dev, "can't allocate event buffer\n");
+               return PTR_ERR(evt);
        }
+       dwc->ev_buf = evt;
 
        return 0;
 }
@@ -268,24 +253,22 @@ static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
 static int dwc3_event_buffers_setup(struct dwc3 *dwc)
 {
        struct dwc3_event_buffer        *evt;
-       int                             n;
-
-       for (n = 0; n < dwc->num_event_buffers; n++) {
-               evt = dwc->ev_buffs[n];
-               dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
-                               evt->buf, (unsigned long long) evt->dma,
-                               evt->length);
-
-               evt->lpos = 0;
-
-               dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
-                               lower_32_bits(evt->dma));
-               dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
-                               upper_32_bits(evt->dma));
-               dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
-                               DWC3_GEVNTSIZ_SIZE(evt->length));
-               dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
-       }
+
+       evt = dwc->ev_buf;
+       dwc3_trace(trace_dwc3_core,
+                       "Event buf %p dma %08llx length %d\n",
+                       evt->buf, (unsigned long long) evt->dma,
+                       evt->length);
+
+       evt->lpos = 0;
+
+       dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0),
+                       lower_32_bits(evt->dma));
+       dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0),
+                       upper_32_bits(evt->dma));
+       dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
+                       DWC3_GEVNTSIZ_SIZE(evt->length));
+       dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
 
        return 0;
 }
@@ -293,19 +276,16 @@ static int dwc3_event_buffers_setup(struct dwc3 *dwc)
 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
 {
        struct dwc3_event_buffer        *evt;
-       int                             n;
 
-       for (n = 0; n < dwc->num_event_buffers; n++) {
-               evt = dwc->ev_buffs[n];
+       evt = dwc->ev_buf;
 
-               evt->lpos = 0;
+       evt->lpos = 0;
 
-               dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
-               dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
-               dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
-                               | DWC3_GEVNTSIZ_SIZE(0));
-               dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
-       }
+       dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0);
+       dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0);
+       dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK
+                       | DWC3_GEVNTSIZ_SIZE(0));
+       dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0);
 }
 
 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
@@ -445,6 +425,9 @@ static int dwc3_phy_setup(struct dwc3 *dwc)
        if (dwc->u2ss_inp3_quirk)
                reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
 
+       if (dwc->dis_rxdet_inp3_quirk)
+               reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3;
+
        if (dwc->req_p1p2p3_quirk)
                reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
 
@@ -608,12 +591,13 @@ static int dwc3_core_init(struct dwc3 *dwc)
                reg |= DWC3_GCTL_GBLHIBERNATIONEN;
                break;
        default:
-               dev_dbg(dwc->dev, "No power optimization available\n");
+               dwc3_trace(trace_dwc3_core, "No power optimization available\n");
        }
 
        /* check if current dwc3 is on simulation board */
        if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
-               dev_dbg(dwc->dev, "it is on FPGA board\n");
+               dwc3_trace(trace_dwc3_core,
+                               "running on FPGA platform\n");
                dwc->is_fpga = true;
        }
 
@@ -814,7 +798,6 @@ static int dwc3_probe(struct platform_device *pdev)
        u8                      lpm_nyet_threshold;
        u8                      tx_de_emphasis;
        u8                      hird_threshold;
-       u32                     fladj = 0;
 
        int                     ret;
 
@@ -829,6 +812,21 @@ static int dwc3_probe(struct platform_device *pdev)
        dwc->mem = mem;
        dwc->dev = dev;
 
+       /* Try to set 64-bit DMA first */
+       if (!pdev->dev.dma_mask)
+               /* Platform did not initialize dma_mask */
+               ret = dma_coerce_mask_and_coherent(&pdev->dev,
+                                                  DMA_BIT_MASK(64));
+       else
+               ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
+
+       /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
+       if (ret) {
+               ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
+               if (ret)
+                       return ret;
+       }
+
        res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
        if (!res) {
                dev_err(dev, "missing IRQ\n");
@@ -892,9 +890,6 @@ static int dwc3_probe(struct platform_device *pdev)
        dwc->usb3_lpm_capable = device_property_read_bool(dev,
                                "snps,usb3_lpm_capable");
 
-       dwc->needs_fifo_resize = device_property_read_bool(dev,
-                               "tx-fifo-resize");
-
        dwc->disable_scramble_quirk = device_property_read_bool(dev,
                                "snps,disable_scramble_quirk");
        dwc->u2exit_lfps_quirk = device_property_read_bool(dev,
@@ -917,6 +912,8 @@ static int dwc3_probe(struct platform_device *pdev)
                                "snps,dis_u2_susphy_quirk");
        dwc->dis_enblslpm_quirk = device_property_read_bool(dev,
                                "snps,dis_enblslpm_quirk");
+       dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev,
+                               "snps,dis_rxdet_inp3_quirk");
 
        dwc->tx_de_emphasis_quirk = device_property_read_bool(dev,
                                "snps,tx_de_emphasis_quirk");
@@ -925,7 +922,7 @@ static int dwc3_probe(struct platform_device *pdev)
        device_property_read_string(dev, "snps,hsphy_interface",
                                    &dwc->hsphy_interface);
        device_property_read_u32(dev, "snps,quirk-frame-length-adjustment",
-                                &fladj);
+                                &dwc->fladj);
 
        if (pdata) {
                dwc->maximum_speed = pdata->maximum_speed;
@@ -936,7 +933,6 @@ static int dwc3_probe(struct platform_device *pdev)
                if (pdata->hird_threshold)
                        hird_threshold = pdata->hird_threshold;
 
-               dwc->needs_fifo_resize = pdata->tx_fifo_resize;
                dwc->usb3_lpm_capable = pdata->usb3_lpm_capable;
                dwc->dr_mode = pdata->dr_mode;
 
@@ -951,13 +947,14 @@ static int dwc3_probe(struct platform_device *pdev)
                dwc->dis_u3_susphy_quirk = pdata->dis_u3_susphy_quirk;
                dwc->dis_u2_susphy_quirk = pdata->dis_u2_susphy_quirk;
                dwc->dis_enblslpm_quirk = pdata->dis_enblslpm_quirk;
+               dwc->dis_rxdet_inp3_quirk = pdata->dis_rxdet_inp3_quirk;
 
                dwc->tx_de_emphasis_quirk = pdata->tx_de_emphasis_quirk;
                if (pdata->tx_de_emphasis)
                        tx_de_emphasis = pdata->tx_de_emphasis;
 
                dwc->hsphy_interface = pdata->hsphy_interface;
-               fladj = pdata->fladj_value;
+               dwc->fladj = pdata->fladj_value;
        }
 
        /* default to superspeed if no maximum_speed passed */
@@ -1015,7 +1012,7 @@ static int dwc3_probe(struct platform_device *pdev)
        }
 
        /* Adjust Frame Length */
-       dwc3_frame_length_adjustment(dwc, fladj);
+       dwc3_frame_length_adjustment(dwc);
 
        usb_phy_set_suspend(dwc->usb2_phy, 0);
        usb_phy_set_suspend(dwc->usb3_phy, 0);
@@ -1037,19 +1034,11 @@ static int dwc3_probe(struct platform_device *pdev)
        if (ret)
                goto err5;
 
-       ret = dwc3_debugfs_init(dwc);
-       if (ret) {
-               dev_err(dev, "failed to initialize debugfs\n");
-               goto err6;
-       }
-
+       dwc3_debugfs_init(dwc);
        pm_runtime_allow(dev);
 
        return 0;
 
-err6:
-       dwc3_core_exit_mode(dwc);
-
 err5:
        dwc3_event_buffers_cleanup(dwc);
 
@@ -1137,6 +1126,11 @@ static int dwc3_suspend(struct device *dev)
        phy_exit(dwc->usb2_generic_phy);
        phy_exit(dwc->usb3_generic_phy);
 
+       usb_phy_set_suspend(dwc->usb2_phy, 1);
+       usb_phy_set_suspend(dwc->usb3_phy, 1);
+       WARN_ON(phy_power_off(dwc->usb2_generic_phy) < 0);
+       WARN_ON(phy_power_off(dwc->usb3_generic_phy) < 0);
+
        pinctrl_pm_select_sleep_state(dev);
 
        return 0;
@@ -1150,11 +1144,21 @@ static int dwc3_resume(struct device *dev)
 
        pinctrl_pm_select_default_state(dev);
 
+       usb_phy_set_suspend(dwc->usb2_phy, 0);
+       usb_phy_set_suspend(dwc->usb3_phy, 0);
+       ret = phy_power_on(dwc->usb2_generic_phy);
+       if (ret < 0)
+               return ret;
+
+       ret = phy_power_on(dwc->usb3_generic_phy);
+       if (ret < 0)
+               goto err_usb2phy_power;
+
        usb_phy_init(dwc->usb3_phy);
        usb_phy_init(dwc->usb2_phy);
        ret = phy_init(dwc->usb2_generic_phy);
        if (ret < 0)
-               return ret;
+               goto err_usb3phy_power;
 
        ret = phy_init(dwc->usb3_generic_phy);
        if (ret < 0)
@@ -1187,18 +1191,20 @@ static int dwc3_resume(struct device *dev)
 err_usb2phy_init:
        phy_exit(dwc->usb2_generic_phy);
 
+err_usb3phy_power:
+       phy_power_off(dwc->usb3_generic_phy);
+
+err_usb2phy_power:
+       phy_power_off(dwc->usb2_generic_phy);
+
        return ret;
 }
+#endif /* CONFIG_PM_SLEEP */
 
 static const struct dev_pm_ops dwc3_dev_pm_ops = {
        SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
 };
 
-#define DWC3_PM_OPS    &(dwc3_dev_pm_ops)
-#else
-#define DWC3_PM_OPS    NULL
-#endif
-
 #ifdef CONFIG_OF
 static const struct of_device_id of_dwc3_match[] = {
        {
@@ -1230,7 +1236,7 @@ static struct platform_driver dwc3_driver = {
                .name   = "dwc3",
                .of_match_table = of_match_ptr(of_dwc3_match),
                .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
-               .pm     = DWC3_PM_OPS,
+               .pm     = &dwc3_dev_pm_ops,
        },
 };