GNU Linux-libre 4.4.296-gnu1
[releases.git] / drivers / usb / host / ehci-exynos.c
1 /*
2  * SAMSUNG EXYNOS USB HOST EHCI Controller
3  *
4  * Copyright (C) 2011 Samsung Electronics Co.Ltd
5  * Author: Jingoo Han <jg1.han@samsung.com>
6  * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7  *
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.
12  *
13  */
14
15 #include <linux/clk.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/io.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_gpio.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
26
27 #include "ehci.h"
28
29 #define DRIVER_DESC "EHCI EXYNOS driver"
30
31 #define EHCI_INSNREG00(base)                    (base + 0x90)
32 #define EHCI_INSNREG00_ENA_INCR16               (0x1 << 25)
33 #define EHCI_INSNREG00_ENA_INCR8                (0x1 << 24)
34 #define EHCI_INSNREG00_ENA_INCR4                (0x1 << 23)
35 #define EHCI_INSNREG00_ENA_INCRX_ALIGN          (0x1 << 22)
36 #define EHCI_INSNREG00_ENABLE_DMA_BURST \
37         (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
38          EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
39
40 static const char hcd_name[] = "ehci-exynos";
41 static struct hc_driver __read_mostly exynos_ehci_hc_driver;
42
43 #define PHY_NUMBER 3
44
45 struct exynos_ehci_hcd {
46         struct clk *clk;
47         struct phy *phy[PHY_NUMBER];
48 };
49
50 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
51
52 static int exynos_ehci_get_phy(struct device *dev,
53                                 struct exynos_ehci_hcd *exynos_ehci)
54 {
55         struct device_node *child;
56         struct phy *phy;
57         int phy_number;
58         int ret;
59
60         /* Get PHYs for the controller */
61         for_each_available_child_of_node(dev->of_node, child) {
62                 ret = of_property_read_u32(child, "reg", &phy_number);
63                 if (ret) {
64                         dev_err(dev, "Failed to parse device tree\n");
65                         of_node_put(child);
66                         return ret;
67                 }
68
69                 if (phy_number >= PHY_NUMBER) {
70                         dev_err(dev, "Invalid number of PHYs\n");
71                         of_node_put(child);
72                         return -EINVAL;
73                 }
74
75                 phy = devm_of_phy_get(dev, child, NULL);
76                 exynos_ehci->phy[phy_number] = phy;
77                 if (IS_ERR(phy)) {
78                         ret = PTR_ERR(phy);
79                         if (ret == -EPROBE_DEFER) {
80                                 of_node_put(child);
81                                 return ret;
82                         } else if (ret != -ENOSYS && ret != -ENODEV) {
83                                 dev_err(dev,
84                                         "Error retrieving usb2 phy: %d\n", ret);
85                                 of_node_put(child);
86                                 return ret;
87                         }
88                 }
89         }
90
91         return 0;
92 }
93
94 static int exynos_ehci_phy_enable(struct device *dev)
95 {
96         struct usb_hcd *hcd = dev_get_drvdata(dev);
97         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
98         int i;
99         int ret = 0;
100
101         for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
102                 if (!IS_ERR(exynos_ehci->phy[i]))
103                         ret = phy_power_on(exynos_ehci->phy[i]);
104         if (ret)
105                 for (i--; i >= 0; i--)
106                         if (!IS_ERR(exynos_ehci->phy[i]))
107                                 phy_power_off(exynos_ehci->phy[i]);
108
109         return ret;
110 }
111
112 static void exynos_ehci_phy_disable(struct device *dev)
113 {
114         struct usb_hcd *hcd = dev_get_drvdata(dev);
115         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
116         int i;
117
118         for (i = 0; i < PHY_NUMBER; i++)
119                 if (!IS_ERR(exynos_ehci->phy[i]))
120                         phy_power_off(exynos_ehci->phy[i]);
121 }
122
123 static void exynos_setup_vbus_gpio(struct device *dev)
124 {
125         int err;
126         int gpio;
127
128         if (!dev->of_node)
129                 return;
130
131         gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
132         if (!gpio_is_valid(gpio))
133                 return;
134
135         err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
136                                     "ehci_vbus_gpio");
137         if (err)
138                 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
139 }
140
141 static int exynos_ehci_probe(struct platform_device *pdev)
142 {
143         struct exynos_ehci_hcd *exynos_ehci;
144         struct usb_hcd *hcd;
145         struct ehci_hcd *ehci;
146         struct resource *res;
147         int irq;
148         int err;
149
150         /*
151          * Right now device-tree probed devices don't get dma_mask set.
152          * Since shared usb code relies on it, set it here for now.
153          * Once we move to full device tree support this will vanish off.
154          */
155         err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
156         if (err)
157                 return err;
158
159         exynos_setup_vbus_gpio(&pdev->dev);
160
161         hcd = usb_create_hcd(&exynos_ehci_hc_driver,
162                              &pdev->dev, dev_name(&pdev->dev));
163         if (!hcd) {
164                 dev_err(&pdev->dev, "Unable to create HCD\n");
165                 return -ENOMEM;
166         }
167         exynos_ehci = to_exynos_ehci(hcd);
168
169         if (of_device_is_compatible(pdev->dev.of_node,
170                                         "samsung,exynos5440-ehci"))
171                 goto skip_phy;
172
173         err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
174         if (err)
175                 goto fail_clk;
176
177 skip_phy:
178
179         exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
180
181         if (IS_ERR(exynos_ehci->clk)) {
182                 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
183                 err = PTR_ERR(exynos_ehci->clk);
184                 goto fail_clk;
185         }
186
187         err = clk_prepare_enable(exynos_ehci->clk);
188         if (err)
189                 goto fail_clk;
190
191         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
192         hcd->regs = devm_ioremap_resource(&pdev->dev, res);
193         if (IS_ERR(hcd->regs)) {
194                 err = PTR_ERR(hcd->regs);
195                 goto fail_io;
196         }
197
198         hcd->rsrc_start = res->start;
199         hcd->rsrc_len = resource_size(res);
200
201         irq = platform_get_irq(pdev, 0);
202         if (irq < 0) {
203                 err = irq;
204                 goto fail_io;
205         }
206
207         err = exynos_ehci_phy_enable(&pdev->dev);
208         if (err) {
209                 dev_err(&pdev->dev, "Failed to enable USB phy\n");
210                 goto fail_io;
211         }
212
213         ehci = hcd_to_ehci(hcd);
214         ehci->caps = hcd->regs;
215
216         /* DMA burst Enable */
217         writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
218
219         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
220         if (err) {
221                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
222                 goto fail_add_hcd;
223         }
224         device_wakeup_enable(hcd->self.controller);
225
226         platform_set_drvdata(pdev, hcd);
227
228         return 0;
229
230 fail_add_hcd:
231         exynos_ehci_phy_disable(&pdev->dev);
232 fail_io:
233         clk_disable_unprepare(exynos_ehci->clk);
234 fail_clk:
235         usb_put_hcd(hcd);
236         return err;
237 }
238
239 static int exynos_ehci_remove(struct platform_device *pdev)
240 {
241         struct usb_hcd *hcd = platform_get_drvdata(pdev);
242         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
243
244         usb_remove_hcd(hcd);
245
246         exynos_ehci_phy_disable(&pdev->dev);
247
248         clk_disable_unprepare(exynos_ehci->clk);
249
250         usb_put_hcd(hcd);
251
252         return 0;
253 }
254
255 #ifdef CONFIG_PM
256 static int exynos_ehci_suspend(struct device *dev)
257 {
258         struct usb_hcd *hcd = dev_get_drvdata(dev);
259         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
260
261         bool do_wakeup = device_may_wakeup(dev);
262         int rc;
263
264         rc = ehci_suspend(hcd, do_wakeup);
265         if (rc)
266                 return rc;
267
268         exynos_ehci_phy_disable(dev);
269
270         clk_disable_unprepare(exynos_ehci->clk);
271
272         return rc;
273 }
274
275 static int exynos_ehci_resume(struct device *dev)
276 {
277         struct usb_hcd *hcd = dev_get_drvdata(dev);
278         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
279         int ret;
280
281         clk_prepare_enable(exynos_ehci->clk);
282
283         ret = exynos_ehci_phy_enable(dev);
284         if (ret) {
285                 dev_err(dev, "Failed to enable USB phy\n");
286                 clk_disable_unprepare(exynos_ehci->clk);
287                 return ret;
288         }
289
290         /* DMA burst Enable */
291         writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
292
293         ehci_resume(hcd, false);
294         return 0;
295 }
296 #else
297 #define exynos_ehci_suspend     NULL
298 #define exynos_ehci_resume      NULL
299 #endif
300
301 static const struct dev_pm_ops exynos_ehci_pm_ops = {
302         .suspend        = exynos_ehci_suspend,
303         .resume         = exynos_ehci_resume,
304 };
305
306 #ifdef CONFIG_OF
307 static const struct of_device_id exynos_ehci_match[] = {
308         { .compatible = "samsung,exynos4210-ehci" },
309         { .compatible = "samsung,exynos5440-ehci" },
310         {},
311 };
312 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
313 #endif
314
315 static struct platform_driver exynos_ehci_driver = {
316         .probe          = exynos_ehci_probe,
317         .remove         = exynos_ehci_remove,
318         .shutdown       = usb_hcd_platform_shutdown,
319         .driver = {
320                 .name   = "exynos-ehci",
321                 .pm     = &exynos_ehci_pm_ops,
322                 .of_match_table = of_match_ptr(exynos_ehci_match),
323         }
324 };
325 static const struct ehci_driver_overrides exynos_overrides __initdata = {
326         .extra_priv_size = sizeof(struct exynos_ehci_hcd),
327 };
328
329 static int __init ehci_exynos_init(void)
330 {
331         if (usb_disabled())
332                 return -ENODEV;
333
334         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
335         ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
336         return platform_driver_register(&exynos_ehci_driver);
337 }
338 module_init(ehci_exynos_init);
339
340 static void __exit ehci_exynos_cleanup(void)
341 {
342         platform_driver_unregister(&exynos_ehci_driver);
343 }
344 module_exit(ehci_exynos_cleanup);
345
346 MODULE_DESCRIPTION(DRIVER_DESC);
347 MODULE_ALIAS("platform:exynos-ehci");
348 MODULE_AUTHOR("Jingoo Han");
349 MODULE_AUTHOR("Joonyoung Shim");
350 MODULE_LICENSE("GPL v2");