Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Samsung Exynos USB HOST EHCI Controller
0004  *
0005  * Copyright (C) 2011 Samsung Electronics Co.Ltd
0006  * Author: Jingoo Han <jg1.han@samsung.com>
0007  * Author: Joonyoung Shim <jy0922.shim@samsung.com>
0008  */
0009 
0010 #include <linux/clk.h>
0011 #include <linux/dma-mapping.h>
0012 #include <linux/io.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/of.h>
0016 #include <linux/of_gpio.h>
0017 #include <linux/phy/phy.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/usb.h>
0020 #include <linux/usb/hcd.h>
0021 
0022 #include "ehci.h"
0023 
0024 #define DRIVER_DESC "EHCI Exynos driver"
0025 
0026 #define EHCI_INSNREG00(base)            (base + 0x90)
0027 #define EHCI_INSNREG00_ENA_INCR16       (0x1 << 25)
0028 #define EHCI_INSNREG00_ENA_INCR8        (0x1 << 24)
0029 #define EHCI_INSNREG00_ENA_INCR4        (0x1 << 23)
0030 #define EHCI_INSNREG00_ENA_INCRX_ALIGN      (0x1 << 22)
0031 #define EHCI_INSNREG00_ENABLE_DMA_BURST \
0032     (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
0033      EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
0034 
0035 static const char hcd_name[] = "ehci-exynos";
0036 static struct hc_driver __read_mostly exynos_ehci_hc_driver;
0037 
0038 #define PHY_NUMBER 3
0039 
0040 struct exynos_ehci_hcd {
0041     struct clk *clk;
0042     struct device_node *of_node;
0043     struct phy *phy[PHY_NUMBER];
0044     bool legacy_phy;
0045 };
0046 
0047 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
0048 
0049 static int exynos_ehci_get_phy(struct device *dev,
0050                 struct exynos_ehci_hcd *exynos_ehci)
0051 {
0052     struct device_node *child;
0053     struct phy *phy;
0054     int phy_number, num_phys;
0055     int ret;
0056 
0057     /* Get PHYs for the controller */
0058     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
0059                           "#phy-cells");
0060     for (phy_number = 0; phy_number < num_phys; phy_number++) {
0061         phy = devm_of_phy_get_by_index(dev, dev->of_node, phy_number);
0062         if (IS_ERR(phy))
0063             return PTR_ERR(phy);
0064         exynos_ehci->phy[phy_number] = phy;
0065     }
0066     if (num_phys > 0)
0067         return 0;
0068 
0069     /* Get PHYs using legacy bindings */
0070     for_each_available_child_of_node(dev->of_node, child) {
0071         ret = of_property_read_u32(child, "reg", &phy_number);
0072         if (ret) {
0073             dev_err(dev, "Failed to parse device tree\n");
0074             of_node_put(child);
0075             return ret;
0076         }
0077 
0078         if (phy_number >= PHY_NUMBER) {
0079             dev_err(dev, "Invalid number of PHYs\n");
0080             of_node_put(child);
0081             return -EINVAL;
0082         }
0083 
0084         phy = devm_of_phy_get(dev, child, NULL);
0085         exynos_ehci->phy[phy_number] = phy;
0086         if (IS_ERR(phy)) {
0087             ret = PTR_ERR(phy);
0088             if (ret == -EPROBE_DEFER) {
0089                 of_node_put(child);
0090                 return ret;
0091             } else if (ret != -ENOSYS && ret != -ENODEV) {
0092                 dev_err(dev,
0093                     "Error retrieving usb2 phy: %d\n", ret);
0094                 of_node_put(child);
0095                 return ret;
0096             }
0097         }
0098     }
0099 
0100     exynos_ehci->legacy_phy = true;
0101     return 0;
0102 }
0103 
0104 static int exynos_ehci_phy_enable(struct device *dev)
0105 {
0106     struct usb_hcd *hcd = dev_get_drvdata(dev);
0107     struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
0108     int i;
0109     int ret = 0;
0110 
0111     for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
0112         if (!IS_ERR(exynos_ehci->phy[i]))
0113             ret = phy_power_on(exynos_ehci->phy[i]);
0114     if (ret)
0115         for (i--; i >= 0; i--)
0116             if (!IS_ERR(exynos_ehci->phy[i]))
0117                 phy_power_off(exynos_ehci->phy[i]);
0118 
0119     return ret;
0120 }
0121 
0122 static void exynos_ehci_phy_disable(struct device *dev)
0123 {
0124     struct usb_hcd *hcd = dev_get_drvdata(dev);
0125     struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
0126     int i;
0127 
0128     for (i = 0; i < PHY_NUMBER; i++)
0129         if (!IS_ERR(exynos_ehci->phy[i]))
0130             phy_power_off(exynos_ehci->phy[i]);
0131 }
0132 
0133 static void exynos_setup_vbus_gpio(struct device *dev)
0134 {
0135     int err;
0136     int gpio;
0137 
0138     if (!dev->of_node)
0139         return;
0140 
0141     gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
0142     if (!gpio_is_valid(gpio))
0143         return;
0144 
0145     err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
0146                     "ehci_vbus_gpio");
0147     if (err)
0148         dev_err(dev, "can't request ehci vbus gpio %d", gpio);
0149 }
0150 
0151 static int exynos_ehci_probe(struct platform_device *pdev)
0152 {
0153     struct exynos_ehci_hcd *exynos_ehci;
0154     struct usb_hcd *hcd;
0155     struct ehci_hcd *ehci;
0156     struct resource *res;
0157     int irq;
0158     int err;
0159 
0160     /*
0161      * Right now device-tree probed devices don't get dma_mask set.
0162      * Since shared usb code relies on it, set it here for now.
0163      * Once we move to full device tree support this will vanish off.
0164      */
0165     err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
0166     if (err)
0167         return err;
0168 
0169     exynos_setup_vbus_gpio(&pdev->dev);
0170 
0171     hcd = usb_create_hcd(&exynos_ehci_hc_driver,
0172                  &pdev->dev, dev_name(&pdev->dev));
0173     if (!hcd) {
0174         dev_err(&pdev->dev, "Unable to create HCD\n");
0175         return -ENOMEM;
0176     }
0177     exynos_ehci = to_exynos_ehci(hcd);
0178 
0179     err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
0180     if (err)
0181         goto fail_clk;
0182 
0183     exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
0184 
0185     if (IS_ERR(exynos_ehci->clk)) {
0186         dev_err(&pdev->dev, "Failed to get usbhost clock\n");
0187         err = PTR_ERR(exynos_ehci->clk);
0188         goto fail_clk;
0189     }
0190 
0191     err = clk_prepare_enable(exynos_ehci->clk);
0192     if (err)
0193         goto fail_clk;
0194 
0195     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0196     hcd->regs = devm_ioremap_resource(&pdev->dev, res);
0197     if (IS_ERR(hcd->regs)) {
0198         err = PTR_ERR(hcd->regs);
0199         goto fail_io;
0200     }
0201 
0202     hcd->rsrc_start = res->start;
0203     hcd->rsrc_len = resource_size(res);
0204 
0205     irq = platform_get_irq(pdev, 0);
0206     if (irq < 0) {
0207         err = irq;
0208         goto fail_io;
0209     }
0210 
0211     err = exynos_ehci_phy_enable(&pdev->dev);
0212     if (err) {
0213         dev_err(&pdev->dev, "Failed to enable USB phy\n");
0214         goto fail_io;
0215     }
0216 
0217     ehci = hcd_to_ehci(hcd);
0218     ehci->caps = hcd->regs;
0219 
0220     /*
0221      * Workaround: reset of_node pointer to avoid conflict between legacy
0222      * Exynos EHCI port subnodes and generic USB device bindings
0223      */
0224     exynos_ehci->of_node = pdev->dev.of_node;
0225     if (exynos_ehci->legacy_phy)
0226         pdev->dev.of_node = NULL;
0227 
0228     /* DMA burst Enable */
0229     writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
0230 
0231     err = usb_add_hcd(hcd, irq, IRQF_SHARED);
0232     if (err) {
0233         dev_err(&pdev->dev, "Failed to add USB HCD\n");
0234         goto fail_add_hcd;
0235     }
0236     device_wakeup_enable(hcd->self.controller);
0237 
0238     platform_set_drvdata(pdev, hcd);
0239 
0240     return 0;
0241 
0242 fail_add_hcd:
0243     exynos_ehci_phy_disable(&pdev->dev);
0244     pdev->dev.of_node = exynos_ehci->of_node;
0245 fail_io:
0246     clk_disable_unprepare(exynos_ehci->clk);
0247 fail_clk:
0248     usb_put_hcd(hcd);
0249     return err;
0250 }
0251 
0252 static int exynos_ehci_remove(struct platform_device *pdev)
0253 {
0254     struct usb_hcd *hcd = platform_get_drvdata(pdev);
0255     struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
0256 
0257     pdev->dev.of_node = exynos_ehci->of_node;
0258 
0259     usb_remove_hcd(hcd);
0260 
0261     exynos_ehci_phy_disable(&pdev->dev);
0262 
0263     clk_disable_unprepare(exynos_ehci->clk);
0264 
0265     usb_put_hcd(hcd);
0266 
0267     return 0;
0268 }
0269 
0270 #ifdef CONFIG_PM
0271 static int exynos_ehci_suspend(struct device *dev)
0272 {
0273     struct usb_hcd *hcd = dev_get_drvdata(dev);
0274     struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
0275 
0276     bool do_wakeup = device_may_wakeup(dev);
0277     int rc;
0278 
0279     rc = ehci_suspend(hcd, do_wakeup);
0280     if (rc)
0281         return rc;
0282 
0283     exynos_ehci_phy_disable(dev);
0284 
0285     clk_disable_unprepare(exynos_ehci->clk);
0286 
0287     return rc;
0288 }
0289 
0290 static int exynos_ehci_resume(struct device *dev)
0291 {
0292     struct usb_hcd *hcd = dev_get_drvdata(dev);
0293     struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
0294     int ret;
0295 
0296     ret = clk_prepare_enable(exynos_ehci->clk);
0297     if (ret)
0298         return ret;
0299 
0300     ret = exynos_ehci_phy_enable(dev);
0301     if (ret) {
0302         dev_err(dev, "Failed to enable USB phy\n");
0303         clk_disable_unprepare(exynos_ehci->clk);
0304         return ret;
0305     }
0306 
0307     /* DMA burst Enable */
0308     writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
0309 
0310     ehci_resume(hcd, false);
0311     return 0;
0312 }
0313 #else
0314 #define exynos_ehci_suspend NULL
0315 #define exynos_ehci_resume  NULL
0316 #endif
0317 
0318 static const struct dev_pm_ops exynos_ehci_pm_ops = {
0319     .suspend    = exynos_ehci_suspend,
0320     .resume     = exynos_ehci_resume,
0321 };
0322 
0323 #ifdef CONFIG_OF
0324 static const struct of_device_id exynos_ehci_match[] = {
0325     { .compatible = "samsung,exynos4210-ehci" },
0326     {},
0327 };
0328 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
0329 #endif
0330 
0331 static struct platform_driver exynos_ehci_driver = {
0332     .probe      = exynos_ehci_probe,
0333     .remove     = exynos_ehci_remove,
0334     .shutdown   = usb_hcd_platform_shutdown,
0335     .driver = {
0336         .name   = "exynos-ehci",
0337         .pm = &exynos_ehci_pm_ops,
0338         .of_match_table = of_match_ptr(exynos_ehci_match),
0339     }
0340 };
0341 static const struct ehci_driver_overrides exynos_overrides __initconst = {
0342     .extra_priv_size = sizeof(struct exynos_ehci_hcd),
0343 };
0344 
0345 static int __init ehci_exynos_init(void)
0346 {
0347     if (usb_disabled())
0348         return -ENODEV;
0349 
0350     pr_info("%s: " DRIVER_DESC "\n", hcd_name);
0351     ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
0352     return platform_driver_register(&exynos_ehci_driver);
0353 }
0354 module_init(ehci_exynos_init);
0355 
0356 static void __exit ehci_exynos_cleanup(void)
0357 {
0358     platform_driver_unregister(&exynos_ehci_driver);
0359 }
0360 module_exit(ehci_exynos_cleanup);
0361 
0362 MODULE_DESCRIPTION(DRIVER_DESC);
0363 MODULE_ALIAS("platform:exynos-ehci");
0364 MODULE_AUTHOR("Jingoo Han");
0365 MODULE_AUTHOR("Joonyoung Shim");
0366 MODULE_LICENSE("GPL v2");