1 // SPDX-License-Identifier: GPL-2.0+
3 * SAMSUNG EXYNOS USB HOST EHCI Controller
5 * Copyright (C) 2011 Samsung Electronics Co.Ltd
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/of_gpio.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 const char hcd_name[] = "ehci-exynos";
36 static struct hc_driver __read_mostly exynos_ehci_hc_driver;
40 struct exynos_ehci_hcd {
42 struct phy *phy[PHY_NUMBER];
45 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
47 static int exynos_ehci_get_phy(struct device *dev,
48 struct exynos_ehci_hcd *exynos_ehci)
50 struct device_node *child;
55 /* Get PHYs for the controller */
56 for_each_available_child_of_node(dev->of_node, child) {
57 ret = of_property_read_u32(child, "reg", &phy_number);
59 dev_err(dev, "Failed to parse device tree\n");
64 if (phy_number >= PHY_NUMBER) {
65 dev_err(dev, "Invalid number of PHYs\n");
70 phy = devm_of_phy_get(dev, child, NULL);
71 exynos_ehci->phy[phy_number] = phy;
74 if (ret == -EPROBE_DEFER) {
77 } else if (ret != -ENOSYS && ret != -ENODEV) {
79 "Error retrieving usb2 phy: %d\n", ret);
89 static int exynos_ehci_phy_enable(struct device *dev)
91 struct usb_hcd *hcd = dev_get_drvdata(dev);
92 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
96 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
97 if (!IS_ERR(exynos_ehci->phy[i]))
98 ret = phy_power_on(exynos_ehci->phy[i]);
100 for (i--; i >= 0; i--)
101 if (!IS_ERR(exynos_ehci->phy[i]))
102 phy_power_off(exynos_ehci->phy[i]);
107 static void exynos_ehci_phy_disable(struct device *dev)
109 struct usb_hcd *hcd = dev_get_drvdata(dev);
110 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
113 for (i = 0; i < PHY_NUMBER; i++)
114 if (!IS_ERR(exynos_ehci->phy[i]))
115 phy_power_off(exynos_ehci->phy[i]);
118 static void exynos_setup_vbus_gpio(struct device *dev)
126 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
127 if (!gpio_is_valid(gpio))
130 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
133 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
136 static int exynos_ehci_probe(struct platform_device *pdev)
138 struct exynos_ehci_hcd *exynos_ehci;
140 struct ehci_hcd *ehci;
141 struct resource *res;
146 * Right now device-tree probed devices don't get dma_mask set.
147 * Since shared usb code relies on it, set it here for now.
148 * Once we move to full device tree support this will vanish off.
150 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
154 exynos_setup_vbus_gpio(&pdev->dev);
156 hcd = usb_create_hcd(&exynos_ehci_hc_driver,
157 &pdev->dev, dev_name(&pdev->dev));
159 dev_err(&pdev->dev, "Unable to create HCD\n");
162 exynos_ehci = to_exynos_ehci(hcd);
164 err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
168 exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
170 if (IS_ERR(exynos_ehci->clk)) {
171 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
172 err = PTR_ERR(exynos_ehci->clk);
176 err = clk_prepare_enable(exynos_ehci->clk);
180 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
181 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
182 if (IS_ERR(hcd->regs)) {
183 err = PTR_ERR(hcd->regs);
187 hcd->rsrc_start = res->start;
188 hcd->rsrc_len = resource_size(res);
190 irq = platform_get_irq(pdev, 0);
192 dev_err(&pdev->dev, "Failed to get IRQ\n");
197 err = exynos_ehci_phy_enable(&pdev->dev);
199 dev_err(&pdev->dev, "Failed to enable USB phy\n");
203 ehci = hcd_to_ehci(hcd);
204 ehci->caps = hcd->regs;
206 /* DMA burst Enable */
207 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
209 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
211 dev_err(&pdev->dev, "Failed to add USB HCD\n");
214 device_wakeup_enable(hcd->self.controller);
216 platform_set_drvdata(pdev, hcd);
221 exynos_ehci_phy_disable(&pdev->dev);
223 clk_disable_unprepare(exynos_ehci->clk);
229 static int exynos_ehci_remove(struct platform_device *pdev)
231 struct usb_hcd *hcd = platform_get_drvdata(pdev);
232 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
236 exynos_ehci_phy_disable(&pdev->dev);
238 clk_disable_unprepare(exynos_ehci->clk);
246 static int exynos_ehci_suspend(struct device *dev)
248 struct usb_hcd *hcd = dev_get_drvdata(dev);
249 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
251 bool do_wakeup = device_may_wakeup(dev);
254 rc = ehci_suspend(hcd, do_wakeup);
258 exynos_ehci_phy_disable(dev);
260 clk_disable_unprepare(exynos_ehci->clk);
265 static int exynos_ehci_resume(struct device *dev)
267 struct usb_hcd *hcd = dev_get_drvdata(dev);
268 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
271 ret = clk_prepare_enable(exynos_ehci->clk);
275 ret = exynos_ehci_phy_enable(dev);
277 dev_err(dev, "Failed to enable USB phy\n");
278 clk_disable_unprepare(exynos_ehci->clk);
282 /* DMA burst Enable */
283 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
285 ehci_resume(hcd, false);
289 #define exynos_ehci_suspend NULL
290 #define exynos_ehci_resume NULL
293 static const struct dev_pm_ops exynos_ehci_pm_ops = {
294 .suspend = exynos_ehci_suspend,
295 .resume = exynos_ehci_resume,
299 static const struct of_device_id exynos_ehci_match[] = {
300 { .compatible = "samsung,exynos4210-ehci" },
303 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
306 static struct platform_driver exynos_ehci_driver = {
307 .probe = exynos_ehci_probe,
308 .remove = exynos_ehci_remove,
309 .shutdown = usb_hcd_platform_shutdown,
311 .name = "exynos-ehci",
312 .pm = &exynos_ehci_pm_ops,
313 .of_match_table = of_match_ptr(exynos_ehci_match),
316 static const struct ehci_driver_overrides exynos_overrides __initconst = {
317 .extra_priv_size = sizeof(struct exynos_ehci_hcd),
320 static int __init ehci_exynos_init(void)
325 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
326 ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
327 return platform_driver_register(&exynos_ehci_driver);
329 module_init(ehci_exynos_init);
331 static void __exit ehci_exynos_cleanup(void)
333 platform_driver_unregister(&exynos_ehci_driver);
335 module_exit(ehci_exynos_cleanup);
337 MODULE_DESCRIPTION(DRIVER_DESC);
338 MODULE_ALIAS("platform:exynos-ehci");
339 MODULE_AUTHOR("Jingoo Han");
340 MODULE_AUTHOR("Joonyoung Shim");
341 MODULE_LICENSE("GPL v2");