2 * SAMSUNG EXYNOS USB HOST EHCI Controller
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Author: Jingoo Han <jg1.han@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
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.
15 #include <linux/clk.h>
16 #include <linux/dma-mapping.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 #include <linux/of_gpio.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
29 #define DRIVER_DESC "EHCI EXYNOS driver"
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)
40 static const char hcd_name
[] = "ehci-exynos";
41 static struct hc_driver __read_mostly exynos_ehci_hc_driver
;
45 struct exynos_ehci_hcd
{
47 struct phy
*phy
[PHY_NUMBER
];
50 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
52 static int exynos_ehci_get_phy(struct device
*dev
,
53 struct exynos_ehci_hcd
*exynos_ehci
)
55 struct device_node
*child
;
60 /* Get PHYs for the controller */
61 for_each_available_child_of_node(dev
->of_node
, child
) {
62 ret
= of_property_read_u32(child
, "reg", &phy_number
);
64 dev_err(dev
, "Failed to parse device tree\n");
69 if (phy_number
>= PHY_NUMBER
) {
70 dev_err(dev
, "Invalid number of PHYs\n");
75 phy
= devm_of_phy_get(dev
, child
, NULL
);
76 exynos_ehci
->phy
[phy_number
] = phy
;
79 if (ret
== -EPROBE_DEFER
) {
82 } else if (ret
!= -ENOSYS
&& ret
!= -ENODEV
) {
84 "Error retrieving usb2 phy: %d\n", ret
);
94 static int exynos_ehci_phy_enable(struct device
*dev
)
96 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
97 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
101 for (i
= 0; ret
== 0 && i
< PHY_NUMBER
; i
++)
102 if (!IS_ERR(exynos_ehci
->phy
[i
]))
103 ret
= phy_power_on(exynos_ehci
->phy
[i
]);
105 for (i
--; i
>= 0; i
--)
106 if (!IS_ERR(exynos_ehci
->phy
[i
]))
107 phy_power_off(exynos_ehci
->phy
[i
]);
112 static void exynos_ehci_phy_disable(struct device
*dev
)
114 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
115 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
118 for (i
= 0; i
< PHY_NUMBER
; i
++)
119 if (!IS_ERR(exynos_ehci
->phy
[i
]))
120 phy_power_off(exynos_ehci
->phy
[i
]);
123 static void exynos_setup_vbus_gpio(struct device
*dev
)
131 gpio
= of_get_named_gpio(dev
->of_node
, "samsung,vbus-gpio", 0);
132 if (!gpio_is_valid(gpio
))
135 err
= devm_gpio_request_one(dev
, gpio
, GPIOF_OUT_INIT_HIGH
,
138 dev_err(dev
, "can't request ehci vbus gpio %d", gpio
);
141 static int exynos_ehci_probe(struct platform_device
*pdev
)
143 struct exynos_ehci_hcd
*exynos_ehci
;
145 struct ehci_hcd
*ehci
;
146 struct resource
*res
;
151 * Right now device-tree probed devices don't get dma_mask set.
152 * Since shared usb code relies on it, set it here for now.
153 * Once we move to full device tree support this will vanish off.
155 err
= dma_coerce_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
159 exynos_setup_vbus_gpio(&pdev
->dev
);
161 hcd
= usb_create_hcd(&exynos_ehci_hc_driver
,
162 &pdev
->dev
, dev_name(&pdev
->dev
));
164 dev_err(&pdev
->dev
, "Unable to create HCD\n");
167 exynos_ehci
= to_exynos_ehci(hcd
);
169 if (of_device_is_compatible(pdev
->dev
.of_node
,
170 "samsung,exynos5440-ehci"))
173 err
= exynos_ehci_get_phy(&pdev
->dev
, exynos_ehci
);
179 exynos_ehci
->clk
= devm_clk_get(&pdev
->dev
, "usbhost");
181 if (IS_ERR(exynos_ehci
->clk
)) {
182 dev_err(&pdev
->dev
, "Failed to get usbhost clock\n");
183 err
= PTR_ERR(exynos_ehci
->clk
);
187 err
= clk_prepare_enable(exynos_ehci
->clk
);
191 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
192 hcd
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
193 if (IS_ERR(hcd
->regs
)) {
194 err
= PTR_ERR(hcd
->regs
);
198 hcd
->rsrc_start
= res
->start
;
199 hcd
->rsrc_len
= resource_size(res
);
201 irq
= platform_get_irq(pdev
, 0);
203 dev_err(&pdev
->dev
, "Failed to get IRQ\n");
208 err
= exynos_ehci_phy_enable(&pdev
->dev
);
210 dev_err(&pdev
->dev
, "Failed to enable USB phy\n");
214 ehci
= hcd_to_ehci(hcd
);
215 ehci
->caps
= hcd
->regs
;
217 /* DMA burst Enable */
218 writel(EHCI_INSNREG00_ENABLE_DMA_BURST
, EHCI_INSNREG00(hcd
->regs
));
220 err
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
222 dev_err(&pdev
->dev
, "Failed to add USB HCD\n");
225 device_wakeup_enable(hcd
->self
.controller
);
227 platform_set_drvdata(pdev
, hcd
);
232 exynos_ehci_phy_disable(&pdev
->dev
);
234 clk_disable_unprepare(exynos_ehci
->clk
);
240 static int exynos_ehci_remove(struct platform_device
*pdev
)
242 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
243 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
247 exynos_ehci_phy_disable(&pdev
->dev
);
249 clk_disable_unprepare(exynos_ehci
->clk
);
257 static int exynos_ehci_suspend(struct device
*dev
)
259 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
260 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
262 bool do_wakeup
= device_may_wakeup(dev
);
265 rc
= ehci_suspend(hcd
, do_wakeup
);
269 exynos_ehci_phy_disable(dev
);
271 clk_disable_unprepare(exynos_ehci
->clk
);
276 static int exynos_ehci_resume(struct device
*dev
)
278 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
279 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
282 ret
= clk_prepare_enable(exynos_ehci
->clk
);
286 ret
= exynos_ehci_phy_enable(dev
);
288 dev_err(dev
, "Failed to enable USB phy\n");
289 clk_disable_unprepare(exynos_ehci
->clk
);
293 /* DMA burst Enable */
294 writel(EHCI_INSNREG00_ENABLE_DMA_BURST
, EHCI_INSNREG00(hcd
->regs
));
296 ehci_resume(hcd
, false);
300 #define exynos_ehci_suspend NULL
301 #define exynos_ehci_resume NULL
304 static const struct dev_pm_ops exynos_ehci_pm_ops
= {
305 .suspend
= exynos_ehci_suspend
,
306 .resume
= exynos_ehci_resume
,
310 static const struct of_device_id exynos_ehci_match
[] = {
311 { .compatible
= "samsung,exynos4210-ehci" },
312 { .compatible
= "samsung,exynos5440-ehci" },
315 MODULE_DEVICE_TABLE(of
, exynos_ehci_match
);
318 static struct platform_driver exynos_ehci_driver
= {
319 .probe
= exynos_ehci_probe
,
320 .remove
= exynos_ehci_remove
,
321 .shutdown
= usb_hcd_platform_shutdown
,
323 .name
= "exynos-ehci",
324 .pm
= &exynos_ehci_pm_ops
,
325 .of_match_table
= of_match_ptr(exynos_ehci_match
),
328 static const struct ehci_driver_overrides exynos_overrides __initconst
= {
329 .extra_priv_size
= sizeof(struct exynos_ehci_hcd
),
332 static int __init
ehci_exynos_init(void)
337 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
338 ehci_init_driver(&exynos_ehci_hc_driver
, &exynos_overrides
);
339 return platform_driver_register(&exynos_ehci_driver
);
341 module_init(ehci_exynos_init
);
343 static void __exit
ehci_exynos_cleanup(void)
345 platform_driver_unregister(&exynos_ehci_driver
);
347 module_exit(ehci_exynos_cleanup
);
349 MODULE_DESCRIPTION(DRIVER_DESC
);
350 MODULE_ALIAS("platform:exynos-ehci");
351 MODULE_AUTHOR("Jingoo Han");
352 MODULE_AUTHOR("Joonyoung Shim");
353 MODULE_LICENSE("GPL v2");