GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / usb / host / ehci-exynos.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * SAMSUNG EXYNOS USB HOST EHCI Controller
4  *
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  */
9
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>
21
22 #include "ehci.h"
23
24 #define DRIVER_DESC "EHCI EXYNOS driver"
25
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)
34
35 static const char hcd_name[] = "ehci-exynos";
36 static struct hc_driver __read_mostly exynos_ehci_hc_driver;
37
38 #define PHY_NUMBER 3
39
40 struct exynos_ehci_hcd {
41         struct clk *clk;
42         struct phy *phy[PHY_NUMBER];
43 };
44
45 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
46
47 static int exynos_ehci_get_phy(struct device *dev,
48                                 struct exynos_ehci_hcd *exynos_ehci)
49 {
50         struct device_node *child;
51         struct phy *phy;
52         int phy_number;
53         int ret;
54
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;
62                 }
63
64                 if (phy_number >= PHY_NUMBER) {
65                         dev_err(dev, "Invalid number of PHYs\n");
66                         of_node_put(child);
67                         return -EINVAL;
68                 }
69
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;
82                         }
83                 }
84         }
85
86         return 0;
87 }
88
89 static int exynos_ehci_phy_enable(struct device *dev)
90 {
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;
95
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]);
103
104         return ret;
105 }
106
107 static void exynos_ehci_phy_disable(struct device *dev)
108 {
109         struct usb_hcd *hcd = dev_get_drvdata(dev);
110         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
111         int i;
112
113         for (i = 0; i < PHY_NUMBER; i++)
114                 if (!IS_ERR(exynos_ehci->phy[i]))
115                         phy_power_off(exynos_ehci->phy[i]);
116 }
117
118 static void exynos_setup_vbus_gpio(struct device *dev)
119 {
120         int err;
121         int gpio;
122
123         if (!dev->of_node)
124                 return;
125
126         gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
127         if (!gpio_is_valid(gpio))
128                 return;
129
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);
134 }
135
136 static int exynos_ehci_probe(struct platform_device *pdev)
137 {
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;
144
145         /*
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.
149          */
150         err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
151         if (err)
152                 return err;
153
154         exynos_setup_vbus_gpio(&pdev->dev);
155
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;
161         }
162         exynos_ehci = to_exynos_ehci(hcd);
163
164         err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
165         if (err)
166                 goto fail_clk;
167
168         exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
169
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);
173                 goto fail_clk;
174         }
175
176         err = clk_prepare_enable(exynos_ehci->clk);
177         if (err)
178                 goto fail_clk;
179
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);
184                 goto fail_io;
185         }
186
187         hcd->rsrc_start = res->start;
188         hcd->rsrc_len = resource_size(res);
189
190         irq = platform_get_irq(pdev, 0);
191         if (irq < 0) {
192                 err = irq;
193                 goto fail_io;
194         }
195
196         err = exynos_ehci_phy_enable(&pdev->dev);
197         if (err) {
198                 dev_err(&pdev->dev, "Failed to enable USB phy\n");
199                 goto fail_io;
200         }
201
202         ehci = hcd_to_ehci(hcd);
203         ehci->caps = hcd->regs;
204
205         /* DMA burst Enable */
206         writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
207
208         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
209         if (err) {
210                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
211                 goto fail_add_hcd;
212         }
213         device_wakeup_enable(hcd->self.controller);
214
215         platform_set_drvdata(pdev, hcd);
216
217         return 0;
218
219 fail_add_hcd:
220         exynos_ehci_phy_disable(&pdev->dev);
221 fail_io:
222         clk_disable_unprepare(exynos_ehci->clk);
223 fail_clk:
224         usb_put_hcd(hcd);
225         return err;
226 }
227
228 static int exynos_ehci_remove(struct platform_device *pdev)
229 {
230         struct usb_hcd *hcd = platform_get_drvdata(pdev);
231         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
232
233         usb_remove_hcd(hcd);
234
235         exynos_ehci_phy_disable(&pdev->dev);
236
237         clk_disable_unprepare(exynos_ehci->clk);
238
239         usb_put_hcd(hcd);
240
241         return 0;
242 }
243
244 #ifdef CONFIG_PM
245 static int exynos_ehci_suspend(struct device *dev)
246 {
247         struct usb_hcd *hcd = dev_get_drvdata(dev);
248         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
249
250         bool do_wakeup = device_may_wakeup(dev);
251         int rc;
252
253         rc = ehci_suspend(hcd, do_wakeup);
254         if (rc)
255                 return rc;
256
257         exynos_ehci_phy_disable(dev);
258
259         clk_disable_unprepare(exynos_ehci->clk);
260
261         return rc;
262 }
263
264 static int exynos_ehci_resume(struct device *dev)
265 {
266         struct usb_hcd *hcd = dev_get_drvdata(dev);
267         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
268         int ret;
269
270         ret = clk_prepare_enable(exynos_ehci->clk);
271         if (ret)
272                 return ret;
273
274         ret = exynos_ehci_phy_enable(dev);
275         if (ret) {
276                 dev_err(dev, "Failed to enable USB phy\n");
277                 clk_disable_unprepare(exynos_ehci->clk);
278                 return ret;
279         }
280
281         /* DMA burst Enable */
282         writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
283
284         ehci_resume(hcd, false);
285         return 0;
286 }
287 #else
288 #define exynos_ehci_suspend     NULL
289 #define exynos_ehci_resume      NULL
290 #endif
291
292 static const struct dev_pm_ops exynos_ehci_pm_ops = {
293         .suspend        = exynos_ehci_suspend,
294         .resume         = exynos_ehci_resume,
295 };
296
297 #ifdef CONFIG_OF
298 static const struct of_device_id exynos_ehci_match[] = {
299         { .compatible = "samsung,exynos4210-ehci" },
300         {},
301 };
302 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
303 #endif
304
305 static struct platform_driver exynos_ehci_driver = {
306         .probe          = exynos_ehci_probe,
307         .remove         = exynos_ehci_remove,
308         .shutdown       = usb_hcd_platform_shutdown,
309         .driver = {
310                 .name   = "exynos-ehci",
311                 .pm     = &exynos_ehci_pm_ops,
312                 .of_match_table = of_match_ptr(exynos_ehci_match),
313         }
314 };
315 static const struct ehci_driver_overrides exynos_overrides __initconst = {
316         .extra_priv_size = sizeof(struct exynos_ehci_hcd),
317 };
318
319 static int __init ehci_exynos_init(void)
320 {
321         if (usb_disabled())
322                 return -ENODEV;
323
324         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
325         ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
326         return platform_driver_register(&exynos_ehci_driver);
327 }
328 module_init(ehci_exynos_init);
329
330 static void __exit ehci_exynos_cleanup(void)
331 {
332         platform_driver_unregister(&exynos_ehci_driver);
333 }
334 module_exit(ehci_exynos_cleanup);
335
336 MODULE_DESCRIPTION(DRIVER_DESC);
337 MODULE_ALIAS("platform:exynos-ehci");
338 MODULE_AUTHOR("Jingoo Han");
339 MODULE_AUTHOR("Joonyoung Shim");
340 MODULE_LICENSE("GPL v2");