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