Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ST EHCI driver
0004  *
0005  * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
0006  *
0007  * Author: Peter Griffin <peter.griffin@linaro.org>
0008  *
0009  * Derived from ehci-platform.c
0010  */
0011 
0012 #include <linux/clk.h>
0013 #include <linux/dma-mapping.h>
0014 #include <linux/err.h>
0015 #include <linux/kernel.h>
0016 #include <linux/hrtimer.h>
0017 #include <linux/io.h>
0018 #include <linux/module.h>
0019 #include <linux/of.h>
0020 #include <linux/phy/phy.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/reset.h>
0023 #include <linux/usb.h>
0024 #include <linux/usb/hcd.h>
0025 #include <linux/usb/ehci_pdriver.h>
0026 #include <linux/pinctrl/consumer.h>
0027 
0028 #include "ehci.h"
0029 
0030 #define USB_MAX_CLKS 3
0031 
0032 struct st_ehci_platform_priv {
0033     struct clk *clks[USB_MAX_CLKS];
0034     struct clk *clk48;
0035     struct reset_control *rst;
0036     struct reset_control *pwr;
0037     struct phy *phy;
0038 };
0039 
0040 #define DRIVER_DESC "EHCI STMicroelectronics driver"
0041 
0042 #define hcd_to_ehci_priv(h) \
0043     ((struct st_ehci_platform_priv *)hcd_to_ehci(h)->priv)
0044 
0045 static const char hcd_name[] = "ehci-st";
0046 
0047 #define EHCI_CAPS_SIZE 0x10
0048 #define AHB2STBUS_INSREG01 (EHCI_CAPS_SIZE + 0x84)
0049 
0050 static int st_ehci_platform_reset(struct usb_hcd *hcd)
0051 {
0052     struct platform_device *pdev = to_platform_device(hcd->self.controller);
0053     struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
0054     struct ehci_hcd *ehci = hcd_to_ehci(hcd);
0055     u32 threshold;
0056 
0057     /* Set EHCI packet buffer IN/OUT threshold to 128 bytes */
0058     threshold = 128 | (128 << 16);
0059     writel(threshold, hcd->regs + AHB2STBUS_INSREG01);
0060 
0061     ehci->caps = hcd->regs + pdata->caps_offset;
0062     return ehci_setup(hcd);
0063 }
0064 
0065 static int st_ehci_platform_power_on(struct platform_device *dev)
0066 {
0067     struct usb_hcd *hcd = platform_get_drvdata(dev);
0068     struct st_ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
0069     int clk, ret;
0070 
0071     ret = reset_control_deassert(priv->pwr);
0072     if (ret)
0073         return ret;
0074 
0075     ret = reset_control_deassert(priv->rst);
0076     if (ret)
0077         goto err_assert_power;
0078 
0079     /* some SoCs don't have a dedicated 48Mhz clock, but those that do
0080        need the rate to be explicitly set */
0081     if (priv->clk48) {
0082         ret = clk_set_rate(priv->clk48, 48000000);
0083         if (ret)
0084             goto err_assert_reset;
0085     }
0086 
0087     for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
0088         ret = clk_prepare_enable(priv->clks[clk]);
0089         if (ret)
0090             goto err_disable_clks;
0091     }
0092 
0093     ret = phy_init(priv->phy);
0094     if (ret)
0095         goto err_disable_clks;
0096 
0097     ret = phy_power_on(priv->phy);
0098     if (ret)
0099         goto err_exit_phy;
0100 
0101     return 0;
0102 
0103 err_exit_phy:
0104     phy_exit(priv->phy);
0105 err_disable_clks:
0106     while (--clk >= 0)
0107         clk_disable_unprepare(priv->clks[clk]);
0108 err_assert_reset:
0109     reset_control_assert(priv->rst);
0110 err_assert_power:
0111     reset_control_assert(priv->pwr);
0112 
0113     return ret;
0114 }
0115 
0116 static void st_ehci_platform_power_off(struct platform_device *dev)
0117 {
0118     struct usb_hcd *hcd = platform_get_drvdata(dev);
0119     struct st_ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
0120     int clk;
0121 
0122     reset_control_assert(priv->pwr);
0123 
0124     reset_control_assert(priv->rst);
0125 
0126     phy_power_off(priv->phy);
0127 
0128     phy_exit(priv->phy);
0129 
0130     for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
0131         if (priv->clks[clk])
0132             clk_disable_unprepare(priv->clks[clk]);
0133 
0134 }
0135 
0136 static struct hc_driver __read_mostly ehci_platform_hc_driver;
0137 
0138 static const struct ehci_driver_overrides platform_overrides __initconst = {
0139     .reset =        st_ehci_platform_reset,
0140     .extra_priv_size =  sizeof(struct st_ehci_platform_priv),
0141 };
0142 
0143 static struct usb_ehci_pdata ehci_platform_defaults = {
0144     .power_on =     st_ehci_platform_power_on,
0145     .power_suspend =    st_ehci_platform_power_off,
0146     .power_off =        st_ehci_platform_power_off,
0147 };
0148 
0149 static int st_ehci_platform_probe(struct platform_device *dev)
0150 {
0151     struct usb_hcd *hcd;
0152     struct resource *res_mem;
0153     struct usb_ehci_pdata *pdata = &ehci_platform_defaults;
0154     struct st_ehci_platform_priv *priv;
0155     int err, irq, clk = 0;
0156 
0157     if (usb_disabled())
0158         return -ENODEV;
0159 
0160     irq = platform_get_irq(dev, 0);
0161     if (irq < 0)
0162         return irq;
0163     res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
0164     if (!res_mem) {
0165         dev_err(&dev->dev, "no memory resource provided");
0166         return -ENXIO;
0167     }
0168 
0169     hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
0170                  dev_name(&dev->dev));
0171     if (!hcd)
0172         return -ENOMEM;
0173 
0174     platform_set_drvdata(dev, hcd);
0175     dev->dev.platform_data = pdata;
0176     priv = hcd_to_ehci_priv(hcd);
0177 
0178     priv->phy = devm_phy_get(&dev->dev, "usb");
0179     if (IS_ERR(priv->phy)) {
0180         err = PTR_ERR(priv->phy);
0181         goto err_put_hcd;
0182     }
0183 
0184     for (clk = 0; clk < USB_MAX_CLKS; clk++) {
0185         priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
0186         if (IS_ERR(priv->clks[clk])) {
0187             err = PTR_ERR(priv->clks[clk]);
0188             if (err == -EPROBE_DEFER)
0189                 goto err_put_clks;
0190             priv->clks[clk] = NULL;
0191             break;
0192         }
0193     }
0194 
0195     /* some SoCs don't have a dedicated 48Mhz clock, but those that
0196        do need the rate to be explicitly set */
0197     priv->clk48 = devm_clk_get(&dev->dev, "clk48");
0198     if (IS_ERR(priv->clk48)) {
0199         dev_info(&dev->dev, "48MHz clk not found\n");
0200         priv->clk48 = NULL;
0201     }
0202 
0203     priv->pwr =
0204         devm_reset_control_get_optional_shared(&dev->dev, "power");
0205     if (IS_ERR(priv->pwr)) {
0206         err = PTR_ERR(priv->pwr);
0207         if (err == -EPROBE_DEFER)
0208             goto err_put_clks;
0209         priv->pwr = NULL;
0210     }
0211 
0212     priv->rst =
0213         devm_reset_control_get_optional_shared(&dev->dev, "softreset");
0214     if (IS_ERR(priv->rst)) {
0215         err = PTR_ERR(priv->rst);
0216         if (err == -EPROBE_DEFER)
0217             goto err_put_clks;
0218         priv->rst = NULL;
0219     }
0220 
0221     if (pdata->power_on) {
0222         err = pdata->power_on(dev);
0223         if (err < 0)
0224             goto err_put_clks;
0225     }
0226 
0227     hcd->rsrc_start = res_mem->start;
0228     hcd->rsrc_len = resource_size(res_mem);
0229 
0230     hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
0231     if (IS_ERR(hcd->regs)) {
0232         err = PTR_ERR(hcd->regs);
0233         goto err_put_clks;
0234     }
0235 
0236     err = usb_add_hcd(hcd, irq, IRQF_SHARED);
0237     if (err)
0238         goto err_put_clks;
0239 
0240     device_wakeup_enable(hcd->self.controller);
0241     platform_set_drvdata(dev, hcd);
0242 
0243     return err;
0244 
0245 err_put_clks:
0246     while (--clk >= 0)
0247         clk_put(priv->clks[clk]);
0248 err_put_hcd:
0249     if (pdata == &ehci_platform_defaults)
0250         dev->dev.platform_data = NULL;
0251 
0252     usb_put_hcd(hcd);
0253 
0254     return err;
0255 }
0256 
0257 static int st_ehci_platform_remove(struct platform_device *dev)
0258 {
0259     struct usb_hcd *hcd = platform_get_drvdata(dev);
0260     struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
0261     struct st_ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
0262     int clk;
0263 
0264     usb_remove_hcd(hcd);
0265 
0266     if (pdata->power_off)
0267         pdata->power_off(dev);
0268 
0269     for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
0270         clk_put(priv->clks[clk]);
0271 
0272     usb_put_hcd(hcd);
0273 
0274     if (pdata == &ehci_platform_defaults)
0275         dev->dev.platform_data = NULL;
0276 
0277     return 0;
0278 }
0279 
0280 #ifdef CONFIG_PM_SLEEP
0281 
0282 static int st_ehci_suspend(struct device *dev)
0283 {
0284     struct usb_hcd *hcd = dev_get_drvdata(dev);
0285     struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
0286     struct platform_device *pdev = to_platform_device(dev);
0287     bool do_wakeup = device_may_wakeup(dev);
0288     int ret;
0289 
0290     ret = ehci_suspend(hcd, do_wakeup);
0291     if (ret)
0292         return ret;
0293 
0294     if (pdata->power_suspend)
0295         pdata->power_suspend(pdev);
0296 
0297     pinctrl_pm_select_sleep_state(dev);
0298 
0299     return ret;
0300 }
0301 
0302 static int st_ehci_resume(struct device *dev)
0303 {
0304     struct usb_hcd *hcd = dev_get_drvdata(dev);
0305     struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
0306     struct platform_device *pdev = to_platform_device(dev);
0307     int err;
0308 
0309     pinctrl_pm_select_default_state(dev);
0310 
0311     if (pdata->power_on) {
0312         err = pdata->power_on(pdev);
0313         if (err < 0)
0314             return err;
0315     }
0316 
0317     ehci_resume(hcd, false);
0318     return 0;
0319 }
0320 
0321 static SIMPLE_DEV_PM_OPS(st_ehci_pm_ops, st_ehci_suspend, st_ehci_resume);
0322 
0323 #endif /* CONFIG_PM_SLEEP */
0324 
0325 static const struct of_device_id st_ehci_ids[] = {
0326     { .compatible = "st,st-ehci-300x", },
0327     { /* sentinel */ }
0328 };
0329 MODULE_DEVICE_TABLE(of, st_ehci_ids);
0330 
0331 static struct platform_driver ehci_platform_driver = {
0332     .probe      = st_ehci_platform_probe,
0333     .remove     = st_ehci_platform_remove,
0334     .shutdown   = usb_hcd_platform_shutdown,
0335     .driver     = {
0336         .name   = "st-ehci",
0337 #ifdef CONFIG_PM_SLEEP
0338         .pm = &st_ehci_pm_ops,
0339 #endif
0340         .of_match_table = st_ehci_ids,
0341     }
0342 };
0343 
0344 static int __init ehci_platform_init(void)
0345 {
0346     if (usb_disabled())
0347         return -ENODEV;
0348 
0349     pr_info("%s: " DRIVER_DESC "\n", hcd_name);
0350 
0351     ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
0352     return platform_driver_register(&ehci_platform_driver);
0353 }
0354 module_init(ehci_platform_init);
0355 
0356 static void __exit ehci_platform_cleanup(void)
0357 {
0358     platform_driver_unregister(&ehci_platform_driver);
0359 }
0360 module_exit(ehci_platform_cleanup);
0361 
0362 MODULE_DESCRIPTION(DRIVER_DESC);
0363 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
0364 MODULE_LICENSE("GPL");