2 * SAMSUNG EXYNOS USB HOST EHCI Controller
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/clk.h>
16 #include <linux/dma-mapping.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 #include <linux/of_gpio.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
29 #define DRIVER_DESC "EHCI EXYNOS driver"
31 #define EHCI_INSNREG00(base) (base + 0x90)
32 #define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
33 #define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
34 #define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
35 #define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
36 #define EHCI_INSNREG00_ENABLE_DMA_BURST \
37 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
38 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
40 static const char hcd_name[] = "ehci-exynos";
41 static struct hc_driver __read_mostly exynos_ehci_hc_driver;
45 struct exynos_ehci_hcd {
47 struct phy *phy[PHY_NUMBER];
50 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
52 static int exynos_ehci_get_phy(struct device *dev,
53 struct exynos_ehci_hcd *exynos_ehci)
55 struct device_node *child;
60 /* Get PHYs for the controller */
61 for_each_available_child_of_node(dev->of_node, child) {
62 ret = of_property_read_u32(child, "reg", &phy_number);
64 dev_err(dev, "Failed to parse device tree\n");
69 if (phy_number >= PHY_NUMBER) {
70 dev_err(dev, "Invalid number of PHYs\n");
75 phy = devm_of_phy_get(dev, child, NULL);
76 exynos_ehci->phy[phy_number] = phy;
79 if (ret == -EPROBE_DEFER) {
81 } else if (ret != -ENOSYS && ret != -ENODEV) {
83 "Error retrieving usb2 phy: %d\n", ret);
92 static int exynos_ehci_phy_enable(struct device *dev)
94 struct usb_hcd *hcd = dev_get_drvdata(dev);
95 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
99 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
100 if (!IS_ERR(exynos_ehci->phy[i]))
101 ret = phy_power_on(exynos_ehci->phy[i]);
103 for (i--; i >= 0; i--)
104 if (!IS_ERR(exynos_ehci->phy[i]))
105 phy_power_off(exynos_ehci->phy[i]);
110 static void exynos_ehci_phy_disable(struct device *dev)
112 struct usb_hcd *hcd = dev_get_drvdata(dev);
113 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
116 for (i = 0; i < PHY_NUMBER; i++)
117 if (!IS_ERR(exynos_ehci->phy[i]))
118 phy_power_off(exynos_ehci->phy[i]);
121 static void exynos_setup_vbus_gpio(struct device *dev)
129 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
130 if (!gpio_is_valid(gpio))
133 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
136 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
139 static int exynos_ehci_probe(struct platform_device *pdev)
141 struct exynos_ehci_hcd *exynos_ehci;
143 struct ehci_hcd *ehci;
144 struct resource *res;
149 * Right now device-tree probed devices don't get dma_mask set.
150 * Since shared usb code relies on it, set it here for now.
151 * Once we move to full device tree support this will vanish off.
153 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
157 exynos_setup_vbus_gpio(&pdev->dev);
159 hcd = usb_create_hcd(&exynos_ehci_hc_driver,
160 &pdev->dev, dev_name(&pdev->dev));
162 dev_err(&pdev->dev, "Unable to create HCD\n");
165 exynos_ehci = to_exynos_ehci(hcd);
167 if (of_device_is_compatible(pdev->dev.of_node,
168 "samsung,exynos5440-ehci"))
171 err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
177 exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
179 if (IS_ERR(exynos_ehci->clk)) {
180 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
181 err = PTR_ERR(exynos_ehci->clk);
185 err = clk_prepare_enable(exynos_ehci->clk);
189 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
190 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
191 if (IS_ERR(hcd->regs)) {
192 err = PTR_ERR(hcd->regs);
196 hcd->rsrc_start = res->start;
197 hcd->rsrc_len = resource_size(res);
199 irq = platform_get_irq(pdev, 0);
201 dev_err(&pdev->dev, "Failed to get IRQ\n");
206 err = exynos_ehci_phy_enable(&pdev->dev);
208 dev_err(&pdev->dev, "Failed to enable USB phy\n");
212 ehci = hcd_to_ehci(hcd);
213 ehci->caps = hcd->regs;
215 /* DMA burst Enable */
216 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
218 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
220 dev_err(&pdev->dev, "Failed to add USB HCD\n");
223 device_wakeup_enable(hcd->self.controller);
225 platform_set_drvdata(pdev, hcd);
230 exynos_ehci_phy_disable(&pdev->dev);
232 clk_disable_unprepare(exynos_ehci->clk);
238 static int exynos_ehci_remove(struct platform_device *pdev)
240 struct usb_hcd *hcd = platform_get_drvdata(pdev);
241 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
245 exynos_ehci_phy_disable(&pdev->dev);
247 clk_disable_unprepare(exynos_ehci->clk);
255 static int exynos_ehci_suspend(struct device *dev)
257 struct usb_hcd *hcd = dev_get_drvdata(dev);
258 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
260 bool do_wakeup = device_may_wakeup(dev);
263 rc = ehci_suspend(hcd, do_wakeup);
267 exynos_ehci_phy_disable(dev);
269 clk_disable_unprepare(exynos_ehci->clk);
274 static int exynos_ehci_resume(struct device *dev)
276 struct usb_hcd *hcd = dev_get_drvdata(dev);
277 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
280 clk_prepare_enable(exynos_ehci->clk);
282 ret = exynos_ehci_phy_enable(dev);
284 dev_err(dev, "Failed to enable USB phy\n");
285 clk_disable_unprepare(exynos_ehci->clk);
289 /* DMA burst Enable */
290 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
292 ehci_resume(hcd, false);
296 #define exynos_ehci_suspend NULL
297 #define exynos_ehci_resume NULL
300 static const struct dev_pm_ops exynos_ehci_pm_ops = {
301 .suspend = exynos_ehci_suspend,
302 .resume = exynos_ehci_resume,
306 static const struct of_device_id exynos_ehci_match[] = {
307 { .compatible = "samsung,exynos4210-ehci" },
308 { .compatible = "samsung,exynos5440-ehci" },
311 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
314 static struct platform_driver exynos_ehci_driver = {
315 .probe = exynos_ehci_probe,
316 .remove = exynos_ehci_remove,
317 .shutdown = usb_hcd_platform_shutdown,
319 .name = "exynos-ehci",
320 .pm = &exynos_ehci_pm_ops,
321 .of_match_table = of_match_ptr(exynos_ehci_match),
324 static const struct ehci_driver_overrides exynos_overrides __initconst = {
325 .extra_priv_size = sizeof(struct exynos_ehci_hcd),
328 static int __init ehci_exynos_init(void)
333 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
334 ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
335 return platform_driver_register(&exynos_ehci_driver);
337 module_init(ehci_exynos_init);
339 static void __exit ehci_exynos_cleanup(void)
341 platform_driver_unregister(&exynos_ehci_driver);
343 module_exit(ehci_exynos_cleanup);
345 MODULE_DESCRIPTION(DRIVER_DESC);
346 MODULE_ALIAS("platform:exynos-ehci");
347 MODULE_AUTHOR("Jingoo Han");
348 MODULE_AUTHOR("Joonyoung Shim");
349 MODULE_LICENSE("GPL v2");