1 // SPDX-License-Identifier: GPL-2.0+
3 * Samsung Exynos USB HOST EHCI Controller
5 * Copyright (C) 2011 Samsung Electronics Co.Ltd
6 * Author: Jingoo Han <jg1.han@samsung.com>
7 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/usb.h>
20 #include <linux/usb/hcd.h>
24 #define DRIVER_DESC "EHCI Exynos driver"
26 #define EHCI_INSNREG00(base) (base + 0x90)
27 #define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
28 #define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
29 #define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
30 #define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
31 #define EHCI_INSNREG00_ENABLE_DMA_BURST \
32 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
33 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
35 static struct hc_driver __read_mostly exynos_ehci_hc_driver;
39 struct exynos_ehci_hcd {
41 struct device_node *of_node;
42 struct phy *phy[PHY_NUMBER];
46 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
48 static int exynos_ehci_get_phy(struct device *dev,
49 struct exynos_ehci_hcd *exynos_ehci)
51 struct device_node *child;
53 int phy_number, num_phys;
56 /* Get PHYs for the controller */
57 num_phys = of_count_phandle_with_args(dev->of_node, "phys",
59 for (phy_number = 0; phy_number < num_phys; phy_number++) {
60 phy = devm_of_phy_get_by_index(dev, dev->of_node, phy_number);
63 exynos_ehci->phy[phy_number] = phy;
68 /* Get PHYs using legacy bindings */
69 for_each_available_child_of_node(dev->of_node, child) {
70 ret = of_property_read_u32(child, "reg", &phy_number);
72 dev_err(dev, "Failed to parse device tree\n");
77 if (phy_number >= PHY_NUMBER) {
78 dev_err(dev, "Invalid number of PHYs\n");
83 phy = devm_of_phy_optional_get(dev, child, NULL);
84 exynos_ehci->phy[phy_number] = phy;
91 exynos_ehci->legacy_phy = true;
95 static int exynos_ehci_phy_enable(struct device *dev)
97 struct usb_hcd *hcd = dev_get_drvdata(dev);
98 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
102 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
103 ret = phy_power_on(exynos_ehci->phy[i]);
105 for (i--; i >= 0; i--)
106 phy_power_off(exynos_ehci->phy[i]);
111 static void exynos_ehci_phy_disable(struct device *dev)
113 struct usb_hcd *hcd = dev_get_drvdata(dev);
114 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
117 for (i = 0; i < PHY_NUMBER; i++)
118 phy_power_off(exynos_ehci->phy[i]);
121 static void exynos_setup_vbus_gpio(struct device *dev)
123 struct gpio_desc *gpio;
126 gpio = devm_gpiod_get_optional(dev, "samsung,vbus", GPIOD_OUT_HIGH);
127 err = PTR_ERR_OR_ZERO(gpio);
129 dev_err(dev, "can't request ehci vbus gpio: %d\n", err);
132 static int exynos_ehci_probe(struct platform_device *pdev)
134 struct exynos_ehci_hcd *exynos_ehci;
136 struct ehci_hcd *ehci;
137 struct resource *res;
142 * Right now device-tree probed devices don't get dma_mask set.
143 * Since shared usb code relies on it, set it here for now.
144 * Once we move to full device tree support this will vanish off.
146 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
150 exynos_setup_vbus_gpio(&pdev->dev);
152 hcd = usb_create_hcd(&exynos_ehci_hc_driver,
153 &pdev->dev, dev_name(&pdev->dev));
155 dev_err(&pdev->dev, "Unable to create HCD\n");
158 exynos_ehci = to_exynos_ehci(hcd);
160 err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
164 exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
166 if (IS_ERR(exynos_ehci->clk)) {
167 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
168 err = PTR_ERR(exynos_ehci->clk);
172 err = clk_prepare_enable(exynos_ehci->clk);
176 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
177 if (IS_ERR(hcd->regs)) {
178 err = PTR_ERR(hcd->regs);
182 hcd->rsrc_start = res->start;
183 hcd->rsrc_len = resource_size(res);
185 irq = platform_get_irq(pdev, 0);
191 err = exynos_ehci_phy_enable(&pdev->dev);
193 dev_err(&pdev->dev, "Failed to enable USB phy\n");
197 ehci = hcd_to_ehci(hcd);
198 ehci->caps = hcd->regs;
201 * Workaround: reset of_node pointer to avoid conflict between legacy
202 * Exynos EHCI port subnodes and generic USB device bindings
204 exynos_ehci->of_node = pdev->dev.of_node;
205 if (exynos_ehci->legacy_phy)
206 pdev->dev.of_node = NULL;
208 /* DMA burst Enable */
209 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
211 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
213 dev_err(&pdev->dev, "Failed to add USB HCD\n");
216 device_wakeup_enable(hcd->self.controller);
218 platform_set_drvdata(pdev, hcd);
223 exynos_ehci_phy_disable(&pdev->dev);
224 pdev->dev.of_node = exynos_ehci->of_node;
226 clk_disable_unprepare(exynos_ehci->clk);
232 static void exynos_ehci_remove(struct platform_device *pdev)
234 struct usb_hcd *hcd = platform_get_drvdata(pdev);
235 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
237 pdev->dev.of_node = exynos_ehci->of_node;
241 exynos_ehci_phy_disable(&pdev->dev);
243 clk_disable_unprepare(exynos_ehci->clk);
249 static int exynos_ehci_suspend(struct device *dev)
251 struct usb_hcd *hcd = dev_get_drvdata(dev);
252 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
254 bool do_wakeup = device_may_wakeup(dev);
257 rc = ehci_suspend(hcd, do_wakeup);
261 exynos_ehci_phy_disable(dev);
263 clk_disable_unprepare(exynos_ehci->clk);
268 static int exynos_ehci_resume(struct device *dev)
270 struct usb_hcd *hcd = dev_get_drvdata(dev);
271 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
274 ret = clk_prepare_enable(exynos_ehci->clk);
278 ret = exynos_ehci_phy_enable(dev);
280 dev_err(dev, "Failed to enable USB phy\n");
281 clk_disable_unprepare(exynos_ehci->clk);
285 /* DMA burst Enable */
286 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
288 ehci_resume(hcd, false);
292 #define exynos_ehci_suspend NULL
293 #define exynos_ehci_resume NULL
296 static const struct dev_pm_ops exynos_ehci_pm_ops = {
297 .suspend = exynos_ehci_suspend,
298 .resume = exynos_ehci_resume,
302 static const struct of_device_id exynos_ehci_match[] = {
303 { .compatible = "samsung,exynos4210-ehci" },
306 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
309 static struct platform_driver exynos_ehci_driver = {
310 .probe = exynos_ehci_probe,
311 .remove_new = exynos_ehci_remove,
312 .shutdown = usb_hcd_platform_shutdown,
314 .name = "exynos-ehci",
315 .pm = &exynos_ehci_pm_ops,
316 .of_match_table = of_match_ptr(exynos_ehci_match),
319 static const struct ehci_driver_overrides exynos_overrides __initconst = {
320 .extra_priv_size = sizeof(struct exynos_ehci_hcd),
323 static int __init ehci_exynos_init(void)
328 ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
329 return platform_driver_register(&exynos_ehci_driver);
331 module_init(ehci_exynos_init);
333 static void __exit ehci_exynos_cleanup(void)
335 platform_driver_unregister(&exynos_ehci_driver);
337 module_exit(ehci_exynos_cleanup);
339 MODULE_DESCRIPTION(DRIVER_DESC);
340 MODULE_ALIAS("platform:exynos-ehci");
341 MODULE_AUTHOR("Jingoo Han");
342 MODULE_AUTHOR("Joonyoung Shim");
343 MODULE_LICENSE("GPL v2");