Merge branch 'x86-platform-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.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                 of_node_put(child);
78                 if (IS_ERR(phy)) {
79                         ret = PTR_ERR(phy);
80                         if (ret == -EPROBE_DEFER) {
81                                 return ret;
82                         } else if (ret != -ENOSYS && ret != -ENODEV) {
83                                 dev_err(dev,
84                                         "Error retrieving usb2 phy: %d\n", ret);
85                                 return ret;
86                         }
87                 }
88         }
89
90         return 0;
91 }
92
93 static int exynos_ehci_phy_enable(struct device *dev)
94 {
95         struct usb_hcd *hcd = dev_get_drvdata(dev);
96         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
97         int i;
98         int ret = 0;
99
100         for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
101                 if (!IS_ERR(exynos_ehci->phy[i]))
102                         ret = phy_power_on(exynos_ehci->phy[i]);
103         if (ret)
104                 for (i--; i >= 0; i--)
105                         if (!IS_ERR(exynos_ehci->phy[i]))
106                                 phy_power_off(exynos_ehci->phy[i]);
107
108         return ret;
109 }
110
111 static void exynos_ehci_phy_disable(struct device *dev)
112 {
113         struct usb_hcd *hcd = dev_get_drvdata(dev);
114         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
115         int i;
116
117         for (i = 0; i < PHY_NUMBER; i++)
118                 if (!IS_ERR(exynos_ehci->phy[i]))
119                         phy_power_off(exynos_ehci->phy[i]);
120 }
121
122 static void exynos_setup_vbus_gpio(struct device *dev)
123 {
124         int err;
125         int gpio;
126
127         if (!dev->of_node)
128                 return;
129
130         gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
131         if (!gpio_is_valid(gpio))
132                 return;
133
134         err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
135                                     "ehci_vbus_gpio");
136         if (err)
137                 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
138 }
139
140 static int exynos_ehci_probe(struct platform_device *pdev)
141 {
142         struct exynos_ehci_hcd *exynos_ehci;
143         struct usb_hcd *hcd;
144         struct ehci_hcd *ehci;
145         struct resource *res;
146         int irq;
147         int err;
148
149         /*
150          * Right now device-tree probed devices don't get dma_mask set.
151          * Since shared usb code relies on it, set it here for now.
152          * Once we move to full device tree support this will vanish off.
153          */
154         err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
155         if (err)
156                 return err;
157
158         exynos_setup_vbus_gpio(&pdev->dev);
159
160         hcd = usb_create_hcd(&exynos_ehci_hc_driver,
161                              &pdev->dev, dev_name(&pdev->dev));
162         if (!hcd) {
163                 dev_err(&pdev->dev, "Unable to create HCD\n");
164                 return -ENOMEM;
165         }
166         exynos_ehci = to_exynos_ehci(hcd);
167
168         if (of_device_is_compatible(pdev->dev.of_node,
169                                         "samsung,exynos5440-ehci"))
170                 goto skip_phy;
171
172         err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci);
173         if (err)
174                 goto fail_clk;
175
176 skip_phy:
177
178         exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
179
180         if (IS_ERR(exynos_ehci->clk)) {
181                 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
182                 err = PTR_ERR(exynos_ehci->clk);
183                 goto fail_clk;
184         }
185
186         err = clk_prepare_enable(exynos_ehci->clk);
187         if (err)
188                 goto fail_clk;
189
190         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191         if (!res) {
192                 dev_err(&pdev->dev, "Failed to get I/O memory\n");
193                 err = -ENXIO;
194                 goto fail_io;
195         }
196
197         hcd->rsrc_start = res->start;
198         hcd->rsrc_len = resource_size(res);
199         hcd->regs = devm_ioremap_resource(&pdev->dev, res);
200         if (IS_ERR(hcd->regs)) {
201                 err = PTR_ERR(hcd->regs);
202                 goto fail_io;
203         }
204
205         irq = platform_get_irq(pdev, 0);
206         if (!irq) {
207                 dev_err(&pdev->dev, "Failed to get IRQ\n");
208                 err = -ENODEV;
209                 goto fail_io;
210         }
211
212         err = exynos_ehci_phy_enable(&pdev->dev);
213         if (err) {
214                 dev_err(&pdev->dev, "Failed to enable USB phy\n");
215                 goto fail_io;
216         }
217
218         ehci = hcd_to_ehci(hcd);
219         ehci->caps = hcd->regs;
220
221         /* DMA burst Enable */
222         writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
223
224         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
225         if (err) {
226                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
227                 goto fail_add_hcd;
228         }
229         device_wakeup_enable(hcd->self.controller);
230
231         platform_set_drvdata(pdev, hcd);
232
233         return 0;
234
235 fail_add_hcd:
236         exynos_ehci_phy_disable(&pdev->dev);
237 fail_io:
238         clk_disable_unprepare(exynos_ehci->clk);
239 fail_clk:
240         usb_put_hcd(hcd);
241         return err;
242 }
243
244 static int exynos_ehci_remove(struct platform_device *pdev)
245 {
246         struct usb_hcd *hcd = platform_get_drvdata(pdev);
247         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
248
249         usb_remove_hcd(hcd);
250
251         exynos_ehci_phy_disable(&pdev->dev);
252
253         clk_disable_unprepare(exynos_ehci->clk);
254
255         usb_put_hcd(hcd);
256
257         return 0;
258 }
259
260 #ifdef CONFIG_PM
261 static int exynos_ehci_suspend(struct device *dev)
262 {
263         struct usb_hcd *hcd = dev_get_drvdata(dev);
264         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
265
266         bool do_wakeup = device_may_wakeup(dev);
267         int rc;
268
269         rc = ehci_suspend(hcd, do_wakeup);
270         if (rc)
271                 return rc;
272
273         exynos_ehci_phy_disable(dev);
274
275         clk_disable_unprepare(exynos_ehci->clk);
276
277         return rc;
278 }
279
280 static int exynos_ehci_resume(struct device *dev)
281 {
282         struct usb_hcd *hcd = dev_get_drvdata(dev);
283         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
284         int ret;
285
286         clk_prepare_enable(exynos_ehci->clk);
287
288         ret = exynos_ehci_phy_enable(dev);
289         if (ret) {
290                 dev_err(dev, "Failed to enable USB phy\n");
291                 clk_disable_unprepare(exynos_ehci->clk);
292                 return ret;
293         }
294
295         /* DMA burst Enable */
296         writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
297
298         ehci_resume(hcd, false);
299         return 0;
300 }
301 #else
302 #define exynos_ehci_suspend     NULL
303 #define exynos_ehci_resume      NULL
304 #endif
305
306 static const struct dev_pm_ops exynos_ehci_pm_ops = {
307         .suspend        = exynos_ehci_suspend,
308         .resume         = exynos_ehci_resume,
309 };
310
311 #ifdef CONFIG_OF
312 static const struct of_device_id exynos_ehci_match[] = {
313         { .compatible = "samsung,exynos4210-ehci" },
314         { .compatible = "samsung,exynos5440-ehci" },
315         {},
316 };
317 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
318 #endif
319
320 static struct platform_driver exynos_ehci_driver = {
321         .probe          = exynos_ehci_probe,
322         .remove         = exynos_ehci_remove,
323         .shutdown       = usb_hcd_platform_shutdown,
324         .driver = {
325                 .name   = "exynos-ehci",
326                 .owner  = THIS_MODULE,
327                 .pm     = &exynos_ehci_pm_ops,
328                 .of_match_table = of_match_ptr(exynos_ehci_match),
329         }
330 };
331 static const struct ehci_driver_overrides exynos_overrides __initdata = {
332         .extra_priv_size = sizeof(struct exynos_ehci_hcd),
333 };
334
335 static int __init ehci_exynos_init(void)
336 {
337         if (usb_disabled())
338                 return -ENODEV;
339
340         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
341         ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
342         return platform_driver_register(&exynos_ehci_driver);
343 }
344 module_init(ehci_exynos_init);
345
346 static void __exit ehci_exynos_cleanup(void)
347 {
348         platform_driver_unregister(&exynos_ehci_driver);
349 }
350 module_exit(ehci_exynos_cleanup);
351
352 MODULE_DESCRIPTION(DRIVER_DESC);
353 MODULE_ALIAS("platform:exynos-ehci");
354 MODULE_AUTHOR("Jingoo Han");
355 MODULE_AUTHOR("Joonyoung Shim");
356 MODULE_LICENSE("GPL v2");