Back to home page

OSCL-LXR

 
 

    


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