Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/usb/host/ehci-orion.c
0004  *
0005  * Tzachi Perelstein <tzachi@marvell.com>
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/mbus.h>
0012 #include <linux/clk.h>
0013 #include <linux/platform_data/usb-ehci-orion.h>
0014 #include <linux/of.h>
0015 #include <linux/phy/phy.h>
0016 #include <linux/of_device.h>
0017 #include <linux/of_irq.h>
0018 #include <linux/usb.h>
0019 #include <linux/usb/hcd.h>
0020 #include <linux/io.h>
0021 #include <linux/dma-mapping.h>
0022 
0023 #include "ehci.h"
0024 
0025 #define rdl(off)    readl_relaxed(hcd->regs + (off))
0026 #define wrl(off, val)   writel_relaxed((val), hcd->regs + (off))
0027 
0028 #define USB_CMD         0x140
0029 #define   USB_CMD_RUN       BIT(0)
0030 #define   USB_CMD_RESET     BIT(1)
0031 #define USB_MODE        0x1a8
0032 #define   USB_MODE_MASK     GENMASK(1, 0)
0033 #define   USB_MODE_DEVICE   0x2
0034 #define   USB_MODE_HOST     0x3
0035 #define   USB_MODE_SDIS     BIT(4)
0036 #define USB_CAUSE       0x310
0037 #define USB_MASK        0x314
0038 #define USB_WINDOW_CTRL(i)  (0x320 + ((i) << 4))
0039 #define USB_WINDOW_BASE(i)  (0x324 + ((i) << 4))
0040 #define USB_IPG         0x360
0041 #define USB_PHY_PWR_CTRL    0x400
0042 #define USB_PHY_TX_CTRL     0x420
0043 #define USB_PHY_RX_CTRL     0x430
0044 #define USB_PHY_IVREF_CTRL  0x440
0045 #define USB_PHY_TST_GRP_CTRL    0x450
0046 
0047 #define USB_SBUSCFG     0x90
0048 
0049 /* BAWR = BARD = 3 : Align read/write bursts packets larger than 128 bytes */
0050 #define USB_SBUSCFG_BAWR_ALIGN_128B (0x3 << 6)
0051 #define USB_SBUSCFG_BARD_ALIGN_128B (0x3 << 3)
0052 /* AHBBRST = 3     : Align AHB Burst to INCR16 (64 bytes) */
0053 #define USB_SBUSCFG_AHBBRST_INCR16  (0x3 << 0)
0054 
0055 #define USB_SBUSCFG_DEF_VAL (USB_SBUSCFG_BAWR_ALIGN_128B    \
0056                  | USB_SBUSCFG_BARD_ALIGN_128B  \
0057                  | USB_SBUSCFG_AHBBRST_INCR16)
0058 
0059 #define DRIVER_DESC "EHCI orion driver"
0060 
0061 #define hcd_to_orion_priv(h) ((struct orion_ehci_hcd *)hcd_to_ehci(h)->priv)
0062 
0063 struct orion_ehci_hcd {
0064     struct clk *clk;
0065     struct phy *phy;
0066 };
0067 
0068 static const char hcd_name[] = "ehci-orion";
0069 
0070 static struct hc_driver __read_mostly ehci_orion_hc_driver;
0071 
0072 /*
0073  * Implement Orion USB controller specification guidelines
0074  */
0075 static void orion_usb_phy_v1_setup(struct usb_hcd *hcd)
0076 {
0077     /* The below GLs are according to the Orion Errata document */
0078     /*
0079      * Clear interrupt cause and mask
0080      */
0081     wrl(USB_CAUSE, 0);
0082     wrl(USB_MASK, 0);
0083 
0084     /*
0085      * Reset controller
0086      */
0087     wrl(USB_CMD, rdl(USB_CMD) | USB_CMD_RESET);
0088     while (rdl(USB_CMD) & USB_CMD_RESET);
0089 
0090     /*
0091      * GL# USB-10: Set IPG for non start of frame packets
0092      * Bits[14:8]=0xc
0093      */
0094     wrl(USB_IPG, (rdl(USB_IPG) & ~0x7f00) | 0xc00);
0095 
0096     /*
0097      * GL# USB-9: USB 2.0 Power Control
0098      * BG_VSEL[7:6]=0x1
0099      */
0100     wrl(USB_PHY_PWR_CTRL, (rdl(USB_PHY_PWR_CTRL) & ~0xc0)| 0x40);
0101 
0102     /*
0103      * GL# USB-1: USB PHY Tx Control - force calibration to '8'
0104      * TXDATA_BLOCK_EN[21]=0x1, EXT_RCAL_EN[13]=0x1, IMP_CAL[6:3]=0x8
0105      */
0106     wrl(USB_PHY_TX_CTRL, (rdl(USB_PHY_TX_CTRL) & ~0x78) | 0x202040);
0107 
0108     /*
0109      * GL# USB-3 GL# USB-9: USB PHY Rx Control
0110      * RXDATA_BLOCK_LENGHT[31:30]=0x3, EDGE_DET_SEL[27:26]=0,
0111      * CDR_FASTLOCK_EN[21]=0, DISCON_THRESHOLD[9:8]=0, SQ_THRESH[7:4]=0x1
0112      */
0113     wrl(USB_PHY_RX_CTRL, (rdl(USB_PHY_RX_CTRL) & ~0xc2003f0) | 0xc0000010);
0114 
0115     /*
0116      * GL# USB-3 GL# USB-9: USB PHY IVREF Control
0117      * PLLVDD12[1:0]=0x2, RXVDD[5:4]=0x3, Reserved[19]=0
0118      */
0119     wrl(USB_PHY_IVREF_CTRL, (rdl(USB_PHY_IVREF_CTRL) & ~0x80003 ) | 0x32);
0120 
0121     /*
0122      * GL# USB-3 GL# USB-9: USB PHY Test Group Control
0123      * REG_FIFO_SQ_RST[15]=0
0124      */
0125     wrl(USB_PHY_TST_GRP_CTRL, rdl(USB_PHY_TST_GRP_CTRL) & ~0x8000);
0126 
0127     /*
0128      * Stop and reset controller
0129      */
0130     wrl(USB_CMD, rdl(USB_CMD) & ~USB_CMD_RUN);
0131     wrl(USB_CMD, rdl(USB_CMD) | USB_CMD_RESET);
0132     while (rdl(USB_CMD) & USB_CMD_RESET);
0133 
0134     /*
0135      * GL# USB-5 Streaming disable REG_USB_MODE[4]=1
0136      * TBD: This need to be done after each reset!
0137      * GL# USB-4 Setup USB Host mode
0138      */
0139     wrl(USB_MODE, USB_MODE_SDIS | USB_MODE_HOST);
0140 }
0141 
0142 static void
0143 ehci_orion_conf_mbus_windows(struct usb_hcd *hcd,
0144                  const struct mbus_dram_target_info *dram)
0145 {
0146     int i;
0147 
0148     for (i = 0; i < 4; i++) {
0149         wrl(USB_WINDOW_CTRL(i), 0);
0150         wrl(USB_WINDOW_BASE(i), 0);
0151     }
0152 
0153     for (i = 0; i < dram->num_cs; i++) {
0154         const struct mbus_dram_window *cs = dram->cs + i;
0155 
0156         wrl(USB_WINDOW_CTRL(i), ((cs->size - 1) & 0xffff0000) |
0157                     (cs->mbus_attr << 8) |
0158                     (dram->mbus_dram_target_id << 4) | 1);
0159         wrl(USB_WINDOW_BASE(i), cs->base);
0160     }
0161 }
0162 
0163 static int ehci_orion_drv_reset(struct usb_hcd *hcd)
0164 {
0165     struct device *dev = hcd->self.controller;
0166     int ret;
0167 
0168     ret = ehci_setup(hcd);
0169     if (ret)
0170         return ret;
0171 
0172     /*
0173      * For SoC without hlock, need to program sbuscfg value to guarantee
0174      * AHB master's burst would not overrun or underrun FIFO.
0175      *
0176      * sbuscfg reg has to be set after usb controller reset, otherwise
0177      * the value would be override to 0.
0178      */
0179     if (of_device_is_compatible(dev->of_node, "marvell,armada-3700-ehci"))
0180         wrl(USB_SBUSCFG, USB_SBUSCFG_DEF_VAL);
0181 
0182     return ret;
0183 }
0184 
0185 static int __maybe_unused ehci_orion_drv_suspend(struct device *dev)
0186 {
0187     struct usb_hcd *hcd = dev_get_drvdata(dev);
0188 
0189     return ehci_suspend(hcd, device_may_wakeup(dev));
0190 }
0191 
0192 static int __maybe_unused ehci_orion_drv_resume(struct device *dev)
0193 {
0194     struct usb_hcd *hcd = dev_get_drvdata(dev);
0195 
0196     return ehci_resume(hcd, false);
0197 }
0198 
0199 static SIMPLE_DEV_PM_OPS(ehci_orion_pm_ops, ehci_orion_drv_suspend,
0200              ehci_orion_drv_resume);
0201 
0202 static const struct ehci_driver_overrides orion_overrides __initconst = {
0203     .extra_priv_size =  sizeof(struct orion_ehci_hcd),
0204     .reset = ehci_orion_drv_reset,
0205 };
0206 
0207 static int ehci_orion_drv_probe(struct platform_device *pdev)
0208 {
0209     struct orion_ehci_data *pd = dev_get_platdata(&pdev->dev);
0210     const struct mbus_dram_target_info *dram;
0211     struct resource *res;
0212     struct usb_hcd *hcd;
0213     struct ehci_hcd *ehci;
0214     void __iomem *regs;
0215     int irq, err;
0216     enum orion_ehci_phy_ver phy_version;
0217     struct orion_ehci_hcd *priv;
0218 
0219     if (usb_disabled())
0220         return -ENODEV;
0221 
0222     pr_debug("Initializing Orion-SoC USB Host Controller\n");
0223 
0224     irq = platform_get_irq(pdev, 0);
0225     if (irq <= 0) {
0226         err = -ENODEV;
0227         goto err;
0228     }
0229 
0230     /*
0231      * Right now device-tree probed devices don't get dma_mask
0232      * set. Since shared usb code relies on it, set it here for
0233      * now. Once we have dma capability bindings this can go away.
0234      */
0235     err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
0236     if (err)
0237         goto err;
0238 
0239     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0240     regs = devm_ioremap_resource(&pdev->dev, res);
0241     if (IS_ERR(regs)) {
0242         err = PTR_ERR(regs);
0243         goto err;
0244     }
0245 
0246     hcd = usb_create_hcd(&ehci_orion_hc_driver,
0247             &pdev->dev, dev_name(&pdev->dev));
0248     if (!hcd) {
0249         err = -ENOMEM;
0250         goto err;
0251     }
0252 
0253     hcd->rsrc_start = res->start;
0254     hcd->rsrc_len = resource_size(res);
0255     hcd->regs = regs;
0256 
0257     ehci = hcd_to_ehci(hcd);
0258     ehci->caps = hcd->regs + 0x100;
0259     hcd->has_tt = 1;
0260 
0261     priv = hcd_to_orion_priv(hcd);
0262     /*
0263      * Not all platforms can gate the clock, so it is not an error if
0264      * the clock does not exists.
0265      */
0266     priv->clk = devm_clk_get(&pdev->dev, NULL);
0267     if (!IS_ERR(priv->clk)) {
0268         err = clk_prepare_enable(priv->clk);
0269         if (err)
0270             goto err_put_hcd;
0271     }
0272 
0273     priv->phy = devm_phy_optional_get(&pdev->dev, "usb");
0274     if (IS_ERR(priv->phy)) {
0275         err = PTR_ERR(priv->phy);
0276         if (err != -ENOSYS)
0277             goto err_dis_clk;
0278     }
0279 
0280     /*
0281      * (Re-)program MBUS remapping windows if we are asked to.
0282      */
0283     dram = mv_mbus_dram_info();
0284     if (dram)
0285         ehci_orion_conf_mbus_windows(hcd, dram);
0286 
0287     /*
0288      * setup Orion USB controller.
0289      */
0290     if (pdev->dev.of_node)
0291         phy_version = EHCI_PHY_NA;
0292     else
0293         phy_version = pd->phy_version;
0294 
0295     switch (phy_version) {
0296     case EHCI_PHY_NA:   /* dont change USB phy settings */
0297         break;
0298     case EHCI_PHY_ORION:
0299         orion_usb_phy_v1_setup(hcd);
0300         break;
0301     case EHCI_PHY_DD:
0302     case EHCI_PHY_KW:
0303     default:
0304         dev_warn(&pdev->dev, "USB phy version isn't supported.\n");
0305     }
0306 
0307     err = usb_add_hcd(hcd, irq, IRQF_SHARED);
0308     if (err)
0309         goto err_dis_clk;
0310 
0311     device_wakeup_enable(hcd->self.controller);
0312     return 0;
0313 
0314 err_dis_clk:
0315     if (!IS_ERR(priv->clk))
0316         clk_disable_unprepare(priv->clk);
0317 err_put_hcd:
0318     usb_put_hcd(hcd);
0319 err:
0320     dev_err(&pdev->dev, "init %s fail, %d\n",
0321         dev_name(&pdev->dev), err);
0322 
0323     return err;
0324 }
0325 
0326 static int ehci_orion_drv_remove(struct platform_device *pdev)
0327 {
0328     struct usb_hcd *hcd = platform_get_drvdata(pdev);
0329     struct orion_ehci_hcd *priv = hcd_to_orion_priv(hcd);
0330 
0331     usb_remove_hcd(hcd);
0332 
0333     if (!IS_ERR(priv->clk))
0334         clk_disable_unprepare(priv->clk);
0335 
0336     usb_put_hcd(hcd);
0337 
0338     return 0;
0339 }
0340 
0341 static const struct of_device_id ehci_orion_dt_ids[] = {
0342     { .compatible = "marvell,orion-ehci", },
0343     { .compatible = "marvell,armada-3700-ehci", },
0344     {},
0345 };
0346 MODULE_DEVICE_TABLE(of, ehci_orion_dt_ids);
0347 
0348 static struct platform_driver ehci_orion_driver = {
0349     .probe      = ehci_orion_drv_probe,
0350     .remove     = ehci_orion_drv_remove,
0351     .shutdown   = usb_hcd_platform_shutdown,
0352     .driver = {
0353         .name   = "orion-ehci",
0354         .of_match_table = ehci_orion_dt_ids,
0355         .pm = &ehci_orion_pm_ops,
0356     },
0357 };
0358 
0359 static int __init ehci_orion_init(void)
0360 {
0361     if (usb_disabled())
0362         return -ENODEV;
0363 
0364     pr_info("%s: " DRIVER_DESC "\n", hcd_name);
0365 
0366     ehci_init_driver(&ehci_orion_hc_driver, &orion_overrides);
0367     return platform_driver_register(&ehci_orion_driver);
0368 }
0369 module_init(ehci_orion_init);
0370 
0371 static void __exit ehci_orion_cleanup(void)
0372 {
0373     platform_driver_unregister(&ehci_orion_driver);
0374 }
0375 module_exit(ehci_orion_cleanup);
0376 
0377 MODULE_DESCRIPTION(DRIVER_DESC);
0378 MODULE_ALIAS("platform:orion-ehci");
0379 MODULE_AUTHOR("Tzachi Perelstein");
0380 MODULE_LICENSE("GPL v2");