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