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