Commit | Line | Data |
---|---|---|
1bcc5aa8 | 1 | /* |
29824c16 | 2 | * SAMSUNG EXYNOS USB HOST EHCI Controller |
1bcc5aa8 JS |
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> |
1c17675d | 22 | #include <linux/phy/phy.h> |
7edb3daf | 23 | #include <linux/platform_device.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 | ||
29824c16 | 32 | #define DRIVER_DESC "EHCI EXYNOS 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 | ||
29824c16 JH |
43 | static const char hcd_name[] = "ehci-exynos"; |
44 | static struct hc_driver __read_mostly exynos_ehci_hc_driver; | |
7edb3daf | 45 | |
1c17675d KD |
46 | #define PHY_NUMBER 3 |
47 | ||
29824c16 | 48 | struct exynos_ehci_hcd { |
1bcc5aa8 | 49 | struct clk *clk; |
d233c196 VG |
50 | struct usb_phy *phy; |
51 | struct usb_otg *otg; | |
1c17675d | 52 | struct phy *phy_g[PHY_NUMBER]; |
1bcc5aa8 JS |
53 | }; |
54 | ||
29824c16 | 55 | #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv) |
d233c196 | 56 | |
1c17675d KD |
57 | static int exynos_ehci_get_phy(struct device *dev, |
58 | struct exynos_ehci_hcd *exynos_ehci) | |
59 | { | |
60 | struct device_node *child; | |
61 | struct phy *phy; | |
62 | int phy_number; | |
63 | int ret = 0; | |
64 | ||
65 | exynos_ehci->phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); | |
66 | if (IS_ERR(exynos_ehci->phy)) { | |
67 | ret = PTR_ERR(exynos_ehci->phy); | |
68 | if (ret != -ENXIO && ret != -ENODEV) { | |
69 | dev_err(dev, "no usb2 phy configured\n"); | |
70 | return ret; | |
71 | } | |
72 | dev_dbg(dev, "Failed to get usb2 phy\n"); | |
73 | } else { | |
74 | exynos_ehci->otg = exynos_ehci->phy->otg; | |
75 | } | |
76 | ||
77 | for_each_available_child_of_node(dev->of_node, child) { | |
78 | ret = of_property_read_u32(child, "reg", &phy_number); | |
79 | if (ret) { | |
80 | dev_err(dev, "Failed to parse device tree\n"); | |
81 | of_node_put(child); | |
82 | return ret; | |
83 | } | |
84 | ||
85 | if (phy_number >= PHY_NUMBER) { | |
86 | dev_err(dev, "Invalid number of PHYs\n"); | |
87 | of_node_put(child); | |
88 | return -EINVAL; | |
89 | } | |
90 | ||
91 | phy = devm_of_phy_get(dev, child, 0); | |
92 | of_node_put(child); | |
93 | if (IS_ERR(phy)) { | |
94 | ret = PTR_ERR(phy); | |
95 | if (ret != -ENOSYS && ret != -ENODEV) { | |
96 | dev_err(dev, "no usb2 phy configured\n"); | |
97 | return ret; | |
98 | } | |
99 | dev_dbg(dev, "Failed to get usb2 phy\n"); | |
100 | } | |
101 | exynos_ehci->phy_g[phy_number] = phy; | |
102 | } | |
103 | ||
104 | return ret; | |
105 | } | |
106 | ||
107 | static int exynos_ehci_phy_enable(struct device *dev) | |
108 | { | |
109 | struct usb_hcd *hcd = dev_get_drvdata(dev); | |
110 | struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd); | |
111 | int i; | |
112 | int ret = 0; | |
113 | ||
114 | if (!IS_ERR(exynos_ehci->phy)) | |
115 | return usb_phy_init(exynos_ehci->phy); | |
116 | ||
117 | for (i = 0; ret == 0 && i < PHY_NUMBER; i++) | |
118 | if (!IS_ERR(exynos_ehci->phy_g[i])) | |
119 | ret = phy_power_on(exynos_ehci->phy_g[i]); | |
120 | if (ret) | |
121 | for (i--; i >= 0; i--) | |
122 | if (!IS_ERR(exynos_ehci->phy_g[i])) | |
123 | phy_power_off(exynos_ehci->phy_g[i]); | |
124 | ||
125 | return ret; | |
126 | } | |
127 | ||
128 | static void exynos_ehci_phy_disable(struct device *dev) | |
129 | { | |
130 | struct usb_hcd *hcd = dev_get_drvdata(dev); | |
131 | struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd); | |
132 | int i; | |
133 | ||
134 | if (!IS_ERR(exynos_ehci->phy)) { | |
135 | usb_phy_shutdown(exynos_ehci->phy); | |
136 | return; | |
137 | } | |
138 | ||
139 | for (i = 0; i < PHY_NUMBER; i++) | |
140 | if (!IS_ERR(exynos_ehci->phy_g[i])) | |
141 | phy_power_off(exynos_ehci->phy_g[i]); | |
142 | } | |
143 | ||
91a9677a | 144 | static void exynos_setup_vbus_gpio(struct device *dev) |
fd81d59c VG |
145 | { |
146 | int err; | |
147 | int gpio; | |
148 | ||
3f3b55bf | 149 | if (!dev->of_node) |
fd81d59c VG |
150 | return; |
151 | ||
3f3b55bf | 152 | gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0); |
fd81d59c VG |
153 | if (!gpio_is_valid(gpio)) |
154 | return; | |
155 | ||
3f3b55bf DA |
156 | err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH, |
157 | "ehci_vbus_gpio"); | |
fd81d59c | 158 | if (err) |
3f3b55bf | 159 | dev_err(dev, "can't request ehci vbus gpio %d", gpio); |
fd81d59c VG |
160 | } |
161 | ||
29824c16 | 162 | static int exynos_ehci_probe(struct platform_device *pdev) |
1bcc5aa8 | 163 | { |
29824c16 | 164 | struct exynos_ehci_hcd *exynos_ehci; |
1bcc5aa8 JS |
165 | struct usb_hcd *hcd; |
166 | struct ehci_hcd *ehci; | |
167 | struct resource *res; | |
168 | int irq; | |
169 | int err; | |
170 | ||
2c026e2b VG |
171 | /* |
172 | * Right now device-tree probed devices don't get dma_mask set. | |
173 | * Since shared usb code relies on it, set it here for now. | |
174 | * Once we move to full device tree support this will vanish off. | |
175 | */ | |
e1fd7341 | 176 | err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
22d9d8e8 RK |
177 | if (err) |
178 | return err; | |
2c026e2b | 179 | |
91a9677a | 180 | exynos_setup_vbus_gpio(&pdev->dev); |
fd81d59c | 181 | |
29824c16 | 182 | hcd = usb_create_hcd(&exynos_ehci_hc_driver, |
7edb3daf MG |
183 | &pdev->dev, dev_name(&pdev->dev)); |
184 | if (!hcd) { | |
185 | dev_err(&pdev->dev, "Unable to create HCD\n"); | |
1bcc5aa8 | 186 | return -ENOMEM; |
7edb3daf | 187 | } |
29824c16 | 188 | exynos_ehci = to_exynos_ehci(hcd); |
e6b0166f TA |
189 | |
190 | if (of_device_is_compatible(pdev->dev.of_node, | |
57ae1605 | 191 | "samsung,exynos5440-ehci")) |
e6b0166f | 192 | goto skip_phy; |
e6b0166f | 193 | |
1c17675d KD |
194 | err = exynos_ehci_get_phy(&pdev->dev, exynos_ehci); |
195 | if (err) | |
196 | goto fail_clk; | |
d233c196 | 197 | |
e6b0166f TA |
198 | skip_phy: |
199 | ||
29824c16 | 200 | exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost"); |
1bcc5aa8 | 201 | |
29824c16 | 202 | if (IS_ERR(exynos_ehci->clk)) { |
1bcc5aa8 | 203 | dev_err(&pdev->dev, "Failed to get usbhost clock\n"); |
29824c16 | 204 | err = PTR_ERR(exynos_ehci->clk); |
1bcc5aa8 JS |
205 | goto fail_clk; |
206 | } | |
207 | ||
29824c16 | 208 | err = clk_prepare_enable(exynos_ehci->clk); |
1bcc5aa8 | 209 | if (err) |
63fd0ae4 | 210 | goto fail_clk; |
1bcc5aa8 JS |
211 | |
212 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
213 | if (!res) { | |
214 | dev_err(&pdev->dev, "Failed to get I/O memory\n"); | |
215 | err = -ENXIO; | |
216 | goto fail_io; | |
217 | } | |
218 | ||
219 | hcd->rsrc_start = res->start; | |
220 | hcd->rsrc_len = resource_size(res); | |
9cb07563 | 221 | hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len); |
1bcc5aa8 JS |
222 | if (!hcd->regs) { |
223 | dev_err(&pdev->dev, "Failed to remap I/O memory\n"); | |
224 | err = -ENOMEM; | |
225 | goto fail_io; | |
226 | } | |
227 | ||
228 | irq = platform_get_irq(pdev, 0); | |
229 | if (!irq) { | |
230 | dev_err(&pdev->dev, "Failed to get IRQ\n"); | |
231 | err = -ENODEV; | |
9cb07563 | 232 | goto fail_io; |
1bcc5aa8 JS |
233 | } |
234 | ||
29824c16 JH |
235 | if (exynos_ehci->otg) |
236 | exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self); | |
d233c196 | 237 | |
1c17675d KD |
238 | err = exynos_ehci_phy_enable(&pdev->dev); |
239 | if (err) { | |
240 | dev_err(&pdev->dev, "Failed to enable USB phy\n"); | |
241 | goto fail_io; | |
242 | } | |
1bcc5aa8 JS |
243 | |
244 | ehci = hcd_to_ehci(hcd); | |
245 | ehci->caps = hcd->regs; | |
1bcc5aa8 | 246 | |
88555a63 JH |
247 | /* DMA burst Enable */ |
248 | writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs)); | |
249 | ||
b5dd18d8 | 250 | err = usb_add_hcd(hcd, irq, IRQF_SHARED); |
1bcc5aa8 JS |
251 | if (err) { |
252 | dev_err(&pdev->dev, "Failed to add USB HCD\n"); | |
d233c196 | 253 | goto fail_add_hcd; |
1bcc5aa8 | 254 | } |
3c9740a1 | 255 | device_wakeup_enable(hcd->self.controller); |
1bcc5aa8 | 256 | |
bbcd85a0 | 257 | platform_set_drvdata(pdev, hcd); |
1bcc5aa8 JS |
258 | |
259 | return 0; | |
260 | ||
d233c196 | 261 | fail_add_hcd: |
1c17675d | 262 | exynos_ehci_phy_disable(&pdev->dev); |
1bcc5aa8 | 263 | fail_io: |
29824c16 | 264 | clk_disable_unprepare(exynos_ehci->clk); |
1bcc5aa8 JS |
265 | fail_clk: |
266 | usb_put_hcd(hcd); | |
1bcc5aa8 JS |
267 | return err; |
268 | } | |
269 | ||
29824c16 | 270 | static int exynos_ehci_remove(struct platform_device *pdev) |
1bcc5aa8 | 271 | { |
7edb3daf | 272 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
29824c16 | 273 | struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd); |
1bcc5aa8 JS |
274 | |
275 | usb_remove_hcd(hcd); | |
276 | ||
29824c16 JH |
277 | if (exynos_ehci->otg) |
278 | exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self); | |
d233c196 | 279 | |
1c17675d | 280 | exynos_ehci_phy_disable(&pdev->dev); |
1bcc5aa8 | 281 | |
29824c16 | 282 | clk_disable_unprepare(exynos_ehci->clk); |
1bcc5aa8 JS |
283 | |
284 | usb_put_hcd(hcd); | |
1bcc5aa8 JS |
285 | |
286 | return 0; | |
287 | } | |
288 | ||
1acb30ef | 289 | #ifdef CONFIG_PM |
29824c16 | 290 | static int exynos_ehci_suspend(struct device *dev) |
1acb30ef | 291 | { |
7edb3daf | 292 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
29824c16 | 293 | struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd); |
7edb3daf | 294 | |
c5cf9212 | 295 | bool do_wakeup = device_may_wakeup(dev); |
c5cf9212 | 296 | int rc; |
1acb30ef | 297 | |
c5cf9212 | 298 | rc = ehci_suspend(hcd, do_wakeup); |
d7217510 VG |
299 | if (rc) |
300 | return rc; | |
1acb30ef | 301 | |
29824c16 JH |
302 | if (exynos_ehci->otg) |
303 | exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self); | |
d233c196 | 304 | |
1c17675d | 305 | exynos_ehci_phy_disable(dev); |
1acb30ef | 306 | |
29824c16 | 307 | clk_disable_unprepare(exynos_ehci->clk); |
8b4fc8c7 | 308 | |
1acb30ef JH |
309 | return rc; |
310 | } | |
311 | ||
29824c16 | 312 | static int exynos_ehci_resume(struct device *dev) |
1acb30ef | 313 | { |
7edb3daf | 314 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
29824c16 | 315 | struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd); |
1c17675d | 316 | int ret; |
1acb30ef | 317 | |
29824c16 | 318 | clk_prepare_enable(exynos_ehci->clk); |
8b4fc8c7 | 319 | |
29824c16 JH |
320 | if (exynos_ehci->otg) |
321 | exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self); | |
d233c196 | 322 | |
1c17675d KD |
323 | ret = exynos_ehci_phy_enable(dev); |
324 | if (ret) { | |
325 | dev_err(dev, "Failed to enable USB phy\n"); | |
326 | clk_disable_unprepare(exynos_ehci->clk); | |
327 | return ret; | |
328 | } | |
1acb30ef | 329 | |
88555a63 JH |
330 | /* DMA burst Enable */ |
331 | writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs)); | |
332 | ||
c5cf9212 | 333 | ehci_resume(hcd, false); |
1acb30ef JH |
334 | return 0; |
335 | } | |
336 | #else | |
29824c16 JH |
337 | #define exynos_ehci_suspend NULL |
338 | #define exynos_ehci_resume NULL | |
1acb30ef JH |
339 | #endif |
340 | ||
29824c16 JH |
341 | static const struct dev_pm_ops exynos_ehci_pm_ops = { |
342 | .suspend = exynos_ehci_suspend, | |
343 | .resume = exynos_ehci_resume, | |
1acb30ef JH |
344 | }; |
345 | ||
2c026e2b VG |
346 | #ifdef CONFIG_OF |
347 | static const struct of_device_id exynos_ehci_match[] = { | |
6e247777 | 348 | { .compatible = "samsung,exynos4210-ehci" }, |
e6b0166f | 349 | { .compatible = "samsung,exynos5440-ehci" }, |
2c026e2b VG |
350 | {}, |
351 | }; | |
352 | MODULE_DEVICE_TABLE(of, exynos_ehci_match); | |
353 | #endif | |
354 | ||
29824c16 JH |
355 | static struct platform_driver exynos_ehci_driver = { |
356 | .probe = exynos_ehci_probe, | |
357 | .remove = exynos_ehci_remove, | |
aaf6b52d | 358 | .shutdown = usb_hcd_platform_shutdown, |
1bcc5aa8 | 359 | .driver = { |
29824c16 | 360 | .name = "exynos-ehci", |
1bcc5aa8 | 361 | .owner = THIS_MODULE, |
29824c16 | 362 | .pm = &exynos_ehci_pm_ops, |
2c026e2b | 363 | .of_match_table = of_match_ptr(exynos_ehci_match), |
1bcc5aa8 JS |
364 | } |
365 | }; | |
29824c16 JH |
366 | static const struct ehci_driver_overrides exynos_overrides __initdata = { |
367 | .extra_priv_size = sizeof(struct exynos_ehci_hcd), | |
7edb3daf MG |
368 | }; |
369 | ||
29824c16 | 370 | static int __init ehci_exynos_init(void) |
7edb3daf MG |
371 | { |
372 | if (usb_disabled()) | |
373 | return -ENODEV; | |
374 | ||
375 | pr_info("%s: " DRIVER_DESC "\n", hcd_name); | |
29824c16 JH |
376 | ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides); |
377 | return platform_driver_register(&exynos_ehci_driver); | |
7edb3daf | 378 | } |
29824c16 | 379 | module_init(ehci_exynos_init); |
7edb3daf | 380 | |
29824c16 | 381 | static void __exit ehci_exynos_cleanup(void) |
7edb3daf | 382 | { |
29824c16 | 383 | platform_driver_unregister(&exynos_ehci_driver); |
7edb3daf | 384 | } |
29824c16 | 385 | module_exit(ehci_exynos_cleanup); |
1bcc5aa8 | 386 | |
7edb3daf | 387 | MODULE_DESCRIPTION(DRIVER_DESC); |
29824c16 | 388 | MODULE_ALIAS("platform:exynos-ehci"); |
7edb3daf MG |
389 | MODULE_AUTHOR("Jingoo Han"); |
390 | MODULE_AUTHOR("Joonyoung Shim"); | |
391 | MODULE_LICENSE("GPL v2"); |