Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[cascardo/linux.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/platform_device.h>
23 #include <linux/usb/phy.h>
24 #include <linux/usb/samsung_usb_phy.h>
25 #include <linux/usb.h>
26 #include <linux/usb/hcd.h>
27 #include <linux/usb/otg.h>
28
29 #include "ehci.h"
30
31 #define DRIVER_DESC "EHCI EXYNOS driver"
32
33 #define EHCI_INSNREG00(base)                    (base + 0x90)
34 #define EHCI_INSNREG00_ENA_INCR16               (0x1 << 25)
35 #define EHCI_INSNREG00_ENA_INCR8                (0x1 << 24)
36 #define EHCI_INSNREG00_ENA_INCR4                (0x1 << 23)
37 #define EHCI_INSNREG00_ENA_INCRX_ALIGN          (0x1 << 22)
38 #define EHCI_INSNREG00_ENABLE_DMA_BURST \
39         (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
40          EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
41
42 static const char hcd_name[] = "ehci-exynos";
43 static struct hc_driver __read_mostly exynos_ehci_hc_driver;
44
45 struct exynos_ehci_hcd {
46         struct clk *clk;
47         struct usb_phy *phy;
48         struct usb_otg *otg;
49 };
50
51 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
52
53 static void exynos_setup_vbus_gpio(struct platform_device *pdev)
54 {
55         struct device *dev = &pdev->dev;
56         int err;
57         int gpio;
58
59         if (!dev->of_node)
60                 return;
61
62         gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
63         if (!gpio_is_valid(gpio))
64                 return;
65
66         err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
67                                     "ehci_vbus_gpio");
68         if (err)
69                 dev_err(dev, "can't request ehci vbus gpio %d", gpio);
70 }
71
72 static int exynos_ehci_probe(struct platform_device *pdev)
73 {
74         struct exynos_ehci_hcd *exynos_ehci;
75         struct usb_hcd *hcd;
76         struct ehci_hcd *ehci;
77         struct resource *res;
78         struct usb_phy *phy;
79         int irq;
80         int err;
81
82         /*
83          * Right now device-tree probed devices don't get dma_mask set.
84          * Since shared usb code relies on it, set it here for now.
85          * Once we move to full device tree support this will vanish off.
86          */
87         if (!pdev->dev.dma_mask)
88                 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
89         if (!pdev->dev.coherent_dma_mask)
90                 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
91
92         exynos_setup_vbus_gpio(pdev);
93
94         hcd = usb_create_hcd(&exynos_ehci_hc_driver,
95                              &pdev->dev, dev_name(&pdev->dev));
96         if (!hcd) {
97                 dev_err(&pdev->dev, "Unable to create HCD\n");
98                 return -ENOMEM;
99         }
100         exynos_ehci = to_exynos_ehci(hcd);
101
102         if (of_device_is_compatible(pdev->dev.of_node,
103                                         "samsung,exynos5440-ehci"))
104                 goto skip_phy;
105
106         phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
107         if (IS_ERR(phy)) {
108                 usb_put_hcd(hcd);
109                 dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
110                 return -EPROBE_DEFER;
111         } else {
112                 exynos_ehci->phy = phy;
113                 exynos_ehci->otg = phy->otg;
114         }
115
116 skip_phy:
117
118         exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
119
120         if (IS_ERR(exynos_ehci->clk)) {
121                 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
122                 err = PTR_ERR(exynos_ehci->clk);
123                 goto fail_clk;
124         }
125
126         err = clk_prepare_enable(exynos_ehci->clk);
127         if (err)
128                 goto fail_clk;
129
130         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131         if (!res) {
132                 dev_err(&pdev->dev, "Failed to get I/O memory\n");
133                 err = -ENXIO;
134                 goto fail_io;
135         }
136
137         hcd->rsrc_start = res->start;
138         hcd->rsrc_len = resource_size(res);
139         hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
140         if (!hcd->regs) {
141                 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
142                 err = -ENOMEM;
143                 goto fail_io;
144         }
145
146         irq = platform_get_irq(pdev, 0);
147         if (!irq) {
148                 dev_err(&pdev->dev, "Failed to get IRQ\n");
149                 err = -ENODEV;
150                 goto fail_io;
151         }
152
153         if (exynos_ehci->otg)
154                 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
155
156         if (exynos_ehci->phy)
157                 usb_phy_init(exynos_ehci->phy);
158
159         ehci = hcd_to_ehci(hcd);
160         ehci->caps = hcd->regs;
161
162         /* DMA burst Enable */
163         writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
164
165         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
166         if (err) {
167                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
168                 goto fail_add_hcd;
169         }
170
171         platform_set_drvdata(pdev, hcd);
172
173         return 0;
174
175 fail_add_hcd:
176         if (exynos_ehci->phy)
177                 usb_phy_shutdown(exynos_ehci->phy);
178 fail_io:
179         clk_disable_unprepare(exynos_ehci->clk);
180 fail_clk:
181         usb_put_hcd(hcd);
182         return err;
183 }
184
185 static int exynos_ehci_remove(struct platform_device *pdev)
186 {
187         struct usb_hcd *hcd = platform_get_drvdata(pdev);
188         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
189
190         usb_remove_hcd(hcd);
191
192         if (exynos_ehci->otg)
193                 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
194
195         if (exynos_ehci->phy)
196                 usb_phy_shutdown(exynos_ehci->phy);
197
198         clk_disable_unprepare(exynos_ehci->clk);
199
200         usb_put_hcd(hcd);
201
202         return 0;
203 }
204
205 #ifdef CONFIG_PM
206 static int exynos_ehci_suspend(struct device *dev)
207 {
208         struct usb_hcd *hcd = dev_get_drvdata(dev);
209         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
210
211         bool do_wakeup = device_may_wakeup(dev);
212         int rc;
213
214         rc = ehci_suspend(hcd, do_wakeup);
215
216         if (exynos_ehci->otg)
217                 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
218
219         if (exynos_ehci->phy)
220                 usb_phy_shutdown(exynos_ehci->phy);
221
222         clk_disable_unprepare(exynos_ehci->clk);
223
224         return rc;
225 }
226
227 static int exynos_ehci_resume(struct device *dev)
228 {
229         struct usb_hcd *hcd = dev_get_drvdata(dev);
230         struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
231
232         clk_prepare_enable(exynos_ehci->clk);
233
234         if (exynos_ehci->otg)
235                 exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
236
237         if (exynos_ehci->phy)
238                 usb_phy_init(exynos_ehci->phy);
239
240         /* DMA burst Enable */
241         writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
242
243         ehci_resume(hcd, false);
244         return 0;
245 }
246 #else
247 #define exynos_ehci_suspend     NULL
248 #define exynos_ehci_resume      NULL
249 #endif
250
251 static const struct dev_pm_ops exynos_ehci_pm_ops = {
252         .suspend        = exynos_ehci_suspend,
253         .resume         = exynos_ehci_resume,
254 };
255
256 #ifdef CONFIG_OF
257 static const struct of_device_id exynos_ehci_match[] = {
258         { .compatible = "samsung,exynos4210-ehci" },
259         { .compatible = "samsung,exynos5440-ehci" },
260         {},
261 };
262 MODULE_DEVICE_TABLE(of, exynos_ehci_match);
263 #endif
264
265 static struct platform_driver exynos_ehci_driver = {
266         .probe          = exynos_ehci_probe,
267         .remove         = exynos_ehci_remove,
268         .shutdown       = usb_hcd_platform_shutdown,
269         .driver = {
270                 .name   = "exynos-ehci",
271                 .owner  = THIS_MODULE,
272                 .pm     = &exynos_ehci_pm_ops,
273                 .of_match_table = of_match_ptr(exynos_ehci_match),
274         }
275 };
276 static const struct ehci_driver_overrides exynos_overrides __initdata = {
277         .extra_priv_size = sizeof(struct exynos_ehci_hcd),
278 };
279
280 static int __init ehci_exynos_init(void)
281 {
282         if (usb_disabled())
283                 return -ENODEV;
284
285         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
286         ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
287         return platform_driver_register(&exynos_ehci_driver);
288 }
289 module_init(ehci_exynos_init);
290
291 static void __exit ehci_exynos_cleanup(void)
292 {
293         platform_driver_unregister(&exynos_ehci_driver);
294 }
295 module_exit(ehci_exynos_cleanup);
296
297 MODULE_DESCRIPTION(DRIVER_DESC);
298 MODULE_ALIAS("platform:exynos-ehci");
299 MODULE_AUTHOR("Jingoo Han");
300 MODULE_AUTHOR("Joonyoung Shim");
301 MODULE_LICENSE("GPL v2");