2 * SAMSUNG EXYNOS USB HOST EHCI Controller
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Author: Jingoo Han <jg1.han@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
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/phy.h>
25 #include <linux/usb/samsung_usb_phy.h>
26 #include <linux/usb.h>
27 #include <linux/usb/hcd.h>
28 #include <linux/usb/otg.h>
32 #define DRIVER_DESC "EHCI EXYNOS driver"
34 #define EHCI_INSNREG00(base) (base + 0x90)
35 #define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
36 #define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
37 #define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
38 #define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
39 #define EHCI_INSNREG00_ENABLE_DMA_BURST \
40 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
41 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
43 static const char hcd_name[] = "ehci-exynos";
44 static struct hc_driver __read_mostly exynos_ehci_hc_driver;
48 struct exynos_ehci_hcd {
52 struct phy *phy_g[PHY_NUMBER];
55 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
57 static int exynos_ehci_get_phy(struct device *dev,
58 struct exynos_ehci_hcd *exynos_ehci)
60 struct device_node *child;
65 exynos_ehci->phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
66 if (IS_ERR(exynos_ehci->phy)) {
67 ret = PTR_ERR(exynos_ehci->phy);
68 if (ret != -ENXIO && ret != -ENODEV) {
69 dev_err(dev, "no usb2 phy configured\n");
72 dev_dbg(dev, "Failed to get usb2 phy\n");
74 exynos_ehci->otg = exynos_ehci->phy->otg;
77 for_each_available_child_of_node(dev->of_node, child) {
78 ret = of_property_read_u32(child, "reg", &phy_number);
80 dev_err(dev, "Failed to parse device tree\n");
85 if (phy_number >= PHY_NUMBER) {
86 dev_err(dev, "Invalid number of PHYs\n");
91 phy = devm_of_phy_get(dev, child, NULL);
95 if (ret != -ENOSYS && ret != -ENODEV) {
96 dev_err(dev, "no usb2 phy configured\n");
99 dev_dbg(dev, "Failed to get usb2 phy\n");
101 exynos_ehci->phy_g[phy_number] = phy;
107 static int exynos_ehci_phy_enable(struct device *dev)
109 struct usb_hcd *hcd = dev_get_drvdata(dev);
110 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
114 if (!IS_ERR(exynos_ehci->phy))
115 return usb_phy_init(exynos_ehci->phy);
117 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
118 if (!IS_ERR(exynos_ehci->phy_g[i]))
119 ret = phy_power_on(exynos_ehci->phy_g[i]);
121 for (i--; i >= 0; i--)
122 if (!IS_ERR(exynos_ehci->phy_g[i]))
123 phy_power_off(exynos_ehci->phy_g[i]);
128 static void exynos_ehci_phy_disable(struct device *dev)
130 struct usb_hcd *hcd = dev_get_drvdata(dev);
131 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
134 if (!IS_ERR(exynos_ehci->phy)) {
135 usb_phy_shutdown(exynos_ehci->phy);
139 for (i = 0; i < PHY_NUMBER; i++)
140 if (!IS_ERR(exynos_ehci->phy_g[i]))
141 phy_power_off(exynos_ehci->phy_g[i]);
144 static void exynos_setup_vbus_gpio(struct device *dev)
152 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
153 if (!gpio_is_valid(gpio))
156 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
159 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
162 static int exynos_ehci_probe(struct platform_device *pdev)
164 struct exynos_ehci_hcd *exynos_ehci;
166 struct ehci_hcd *ehci;
167 struct resource *res;
172 * Right now device-tree probed devices don't get dma_mask set.
173 * Since shared usb code relies on it, set it here for now.
174 * Once we move to full device tree support this will vanish off.
176 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
180 exynos_setup_vbus_gpio(&pdev->dev);
182 hcd = usb_create_hcd(&exynos_ehci_hc_driver,
183 &pdev->dev, dev_name(&pdev->dev));
185 dev_err(&pdev->dev, "Unable to create HCD\n");
188 exynos_ehci = to_exynos_ehci(hcd);
190 if (of_device_is_compatible(pdev->dev.of_node,
191 "samsung,exynos5440-ehci"))
194 err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
200 exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
202 if (IS_ERR(exynos_ehci->clk)) {
203 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
204 err = PTR_ERR(exynos_ehci->clk);
208 err = clk_prepare_enable(exynos_ehci->clk);
212 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 dev_err(&pdev->dev, "Failed to get I/O memory\n");
219 hcd->rsrc_start = res->start;
220 hcd->rsrc_len = resource_size(res);
221 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
222 if (IS_ERR(hcd->regs)) {
223 err = PTR_ERR(hcd->regs);
227 irq = platform_get_irq(pdev, 0);
229 dev_err(&pdev->dev, "Failed to get IRQ\n");
234 if (exynos_ehci->otg)
235 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
237 err = exynos_ehci_phy_enable(&pdev->dev);
239 dev_err(&pdev->dev, "Failed to enable USB phy\n");
243 ehci = hcd_to_ehci(hcd);
244 ehci->caps = hcd->regs;
246 /* DMA burst Enable */
247 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
249 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
251 dev_err(&pdev->dev, "Failed to add USB HCD\n");
254 device_wakeup_enable(hcd->self.controller);
256 platform_set_drvdata(pdev, hcd);
261 exynos_ehci_phy_disable(&pdev->dev);
263 clk_disable_unprepare(exynos_ehci->clk);
269 static int exynos_ehci_remove(struct platform_device *pdev)
271 struct usb_hcd *hcd = platform_get_drvdata(pdev);
272 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
276 if (exynos_ehci->otg)
277 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
279 exynos_ehci_phy_disable(&pdev->dev);
281 clk_disable_unprepare(exynos_ehci->clk);
289 static int exynos_ehci_suspend(struct device *dev)
291 struct usb_hcd *hcd = dev_get_drvdata(dev);
292 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
294 bool do_wakeup = device_may_wakeup(dev);
297 rc = ehci_suspend(hcd, do_wakeup);
301 if (exynos_ehci->otg)
302 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
304 exynos_ehci_phy_disable(dev);
306 clk_disable_unprepare(exynos_ehci->clk);
311 static int exynos_ehci_resume(struct device *dev)
313 struct usb_hcd *hcd = dev_get_drvdata(dev);
314 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
317 clk_prepare_enable(exynos_ehci->clk);
319 if (exynos_ehci->otg)
320 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
322 ret = exynos_ehci_phy_enable(dev);
324 dev_err(dev, "Failed to enable USB phy\n");
325 clk_disable_unprepare(exynos_ehci->clk);
329 /* DMA burst Enable */
330 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
332 ehci_resume(hcd, false);
336 #define exynos_ehci_suspend NULL
337 #define exynos_ehci_resume NULL
340 static const struct dev_pm_ops exynos_ehci_pm_ops = {
341 .suspend = exynos_ehci_suspend,
342 .resume = exynos_ehci_resume,
346 static const struct of_device_id exynos_ehci_match[] = {
347 { .compatible = "samsung,exynos4210-ehci" },
348 { .compatible = "samsung,exynos5440-ehci" },
351 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
354 static struct platform_driver exynos_ehci_driver = {
355 .probe = exynos_ehci_probe,
356 .remove = exynos_ehci_remove,
357 .shutdown = usb_hcd_platform_shutdown,
359 .name = "exynos-ehci",
360 .owner = THIS_MODULE,
361 .pm = &exynos_ehci_pm_ops,
362 .of_match_table = of_match_ptr(exynos_ehci_match),
365 static const struct ehci_driver_overrides exynos_overrides __initdata = {
366 .extra_priv_size = sizeof(struct exynos_ehci_hcd),
369 static int __init ehci_exynos_init(void)
374 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
375 ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
376 return platform_driver_register(&exynos_ehci_driver);
378 module_init(ehci_exynos_init);
380 static void __exit ehci_exynos_cleanup(void)
382 platform_driver_unregister(&exynos_ehci_driver);
384 module_exit(ehci_exynos_cleanup);
386 MODULE_DESCRIPTION(DRIVER_DESC);
387 MODULE_ALIAS("platform:exynos-ehci");
388 MODULE_AUTHOR("Jingoo Han");
389 MODULE_AUTHOR("Joonyoung Shim");
390 MODULE_LICENSE("GPL v2");