Linux 4.16.11
[linux/fpc-iii.git] / drivers / usb / host / ehci-exynos.c
blobd9145a8f35d2e5515ee8f16ef9d7b5d237158ca8
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
8 */
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.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>
22 #include "ehci.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;
38 #define PHY_NUMBER 3
40 struct exynos_ehci_hcd {
41 struct clk *clk;
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;
51 struct phy *phy;
52 int phy_number;
53 int ret;
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);
58 if (ret) {
59 dev_err(dev, "Failed to parse device tree\n");
60 of_node_put(child);
61 return ret;
64 if (phy_number >= PHY_NUMBER) {
65 dev_err(dev, "Invalid number of PHYs\n");
66 of_node_put(child);
67 return -EINVAL;
70 phy = devm_of_phy_get(dev, child, NULL);
71 exynos_ehci->phy[phy_number] = phy;
72 if (IS_ERR(phy)) {
73 ret = PTR_ERR(phy);
74 if (ret == -EPROBE_DEFER) {
75 of_node_put(child);
76 return ret;
77 } else if (ret != -ENOSYS && ret != -ENODEV) {
78 dev_err(dev,
79 "Error retrieving usb2 phy: %d\n", ret);
80 of_node_put(child);
81 return ret;
86 return 0;
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);
93 int i;
94 int ret = 0;
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]);
99 if (ret)
100 for (i--; i >= 0; i--)
101 if (!IS_ERR(exynos_ehci->phy[i]))
102 phy_power_off(exynos_ehci->phy[i]);
104 return ret;
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);
111 int i;
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)
120 int err;
121 int gpio;
123 if (!dev->of_node)
124 return;
126 gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
127 if (!gpio_is_valid(gpio))
128 return;
130 err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
131 "ehci_vbus_gpio");
132 if (err)
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;
139 struct usb_hcd *hcd;
140 struct ehci_hcd *ehci;
141 struct resource *res;
142 int irq;
143 int err;
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));
151 if (err)
152 return err;
154 exynos_setup_vbus_gpio(&pdev->dev);
156 hcd = usb_create_hcd(&exynos_ehci_hc_driver,
157 &pdev->dev, dev_name(&pdev->dev));
158 if (!hcd) {
159 dev_err(&pdev->dev, "Unable to create HCD\n");
160 return -ENOMEM;
162 exynos_ehci = to_exynos_ehci(hcd);
164 if (of_device_is_compatible(pdev->dev.of_node,
165 "samsung,exynos5440-ehci"))
166 goto skip_phy;
168 err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
169 if (err)
170 goto fail_clk;
172 skip_phy:
174 exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
176 if (IS_ERR(exynos_ehci->clk)) {
177 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
178 err = PTR_ERR(exynos_ehci->clk);
179 goto fail_clk;
182 err = clk_prepare_enable(exynos_ehci->clk);
183 if (err)
184 goto fail_clk;
186 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
187 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
188 if (IS_ERR(hcd->regs)) {
189 err = PTR_ERR(hcd->regs);
190 goto fail_io;
193 hcd->rsrc_start = res->start;
194 hcd->rsrc_len = resource_size(res);
196 irq = platform_get_irq(pdev, 0);
197 if (!irq) {
198 dev_err(&pdev->dev, "Failed to get IRQ\n");
199 err = -ENODEV;
200 goto fail_io;
203 err = exynos_ehci_phy_enable(&pdev->dev);
204 if (err) {
205 dev_err(&pdev->dev, "Failed to enable USB phy\n");
206 goto fail_io;
209 ehci = hcd_to_ehci(hcd);
210 ehci->caps = hcd->regs;
212 /* DMA burst Enable */
213 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
215 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
216 if (err) {
217 dev_err(&pdev->dev, "Failed to add USB HCD\n");
218 goto fail_add_hcd;
220 device_wakeup_enable(hcd->self.controller);
222 platform_set_drvdata(pdev, hcd);
224 return 0;
226 fail_add_hcd:
227 exynos_ehci_phy_disable(&pdev->dev);
228 fail_io:
229 clk_disable_unprepare(exynos_ehci->clk);
230 fail_clk:
231 usb_put_hcd(hcd);
232 return err;
235 static int exynos_ehci_remove(struct platform_device *pdev)
237 struct usb_hcd *hcd = platform_get_drvdata(pdev);
238 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
240 usb_remove_hcd(hcd);
242 exynos_ehci_phy_disable(&pdev->dev);
244 clk_disable_unprepare(exynos_ehci->clk);
246 usb_put_hcd(hcd);
248 return 0;
251 #ifdef CONFIG_PM
252 static int exynos_ehci_suspend(struct device *dev)
254 struct usb_hcd *hcd = dev_get_drvdata(dev);
255 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
257 bool do_wakeup = device_may_wakeup(dev);
258 int rc;
260 rc = ehci_suspend(hcd, do_wakeup);
261 if (rc)
262 return rc;
264 exynos_ehci_phy_disable(dev);
266 clk_disable_unprepare(exynos_ehci->clk);
268 return rc;
271 static int exynos_ehci_resume(struct device *dev)
273 struct usb_hcd *hcd = dev_get_drvdata(dev);
274 struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
275 int ret;
277 ret = clk_prepare_enable(exynos_ehci->clk);
278 if (ret)
279 return ret;
281 ret = exynos_ehci_phy_enable(dev);
282 if (ret) {
283 dev_err(dev, "Failed to enable USB phy\n");
284 clk_disable_unprepare(exynos_ehci->clk);
285 return ret;
288 /* DMA burst Enable */
289 writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
291 ehci_resume(hcd, false);
292 return 0;
294 #else
295 #define exynos_ehci_suspend NULL
296 #define exynos_ehci_resume NULL
297 #endif
299 static const struct dev_pm_ops exynos_ehci_pm_ops = {
300 .suspend = exynos_ehci_suspend,
301 .resume = exynos_ehci_resume,
304 #ifdef CONFIG_OF
305 static const struct of_device_id exynos_ehci_match[] = {
306 { .compatible = "samsung,exynos4210-ehci" },
307 { .compatible = "samsung,exynos5440-ehci" },
310 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
311 #endif
313 static struct platform_driver exynos_ehci_driver = {
314 .probe = exynos_ehci_probe,
315 .remove = exynos_ehci_remove,
316 .shutdown = usb_hcd_platform_shutdown,
317 .driver = {
318 .name = "exynos-ehci",
319 .pm = &exynos_ehci_pm_ops,
320 .of_match_table = of_match_ptr(exynos_ehci_match),
323 static const struct ehci_driver_overrides exynos_overrides __initconst = {
324 .extra_priv_size = sizeof(struct exynos_ehci_hcd),
327 static int __init ehci_exynos_init(void)
329 if (usb_disabled())
330 return -ENODEV;
332 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
333 ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
334 return platform_driver_register(&exynos_ehci_driver);
336 module_init(ehci_exynos_init);
338 static void __exit ehci_exynos_cleanup(void)
340 platform_driver_unregister(&exynos_ehci_driver);
342 module_exit(ehci_exynos_cleanup);
344 MODULE_DESCRIPTION(DRIVER_DESC);
345 MODULE_ALIAS("platform:exynos-ehci");
346 MODULE_AUTHOR("Jingoo Han");
347 MODULE_AUTHOR("Joonyoung Shim");
348 MODULE_LICENSE("GPL v2");