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