1 // SPDX-License-Identifier: GPL-2.0+
3 * SAMSUNG EXYNOS USB HOST EHCI Controller
5 * Copyright (C) 2011 Samsung Electronics Co.Ltd
6 * Author: Jingoo Han <jg1.han@samsung.com>
7 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/of_gpio.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/usb.h>
20 #include <linux/usb/hcd.h>
24 #define DRIVER_DESC "EHCI EXYNOS driver"
26 #define EHCI_INSNREG00(base) (base + 0x90)
27 #define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
28 #define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
29 #define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
30 #define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
31 #define EHCI_INSNREG00_ENABLE_DMA_BURST \
32 (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
33 EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
35 static const char hcd_name
[] = "ehci-exynos";
36 static struct hc_driver __read_mostly exynos_ehci_hc_driver
;
40 struct exynos_ehci_hcd
{
42 struct device_node
*of_node
;
43 struct phy
*phy
[PHY_NUMBER
];
47 #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
49 static int exynos_ehci_get_phy(struct device
*dev
,
50 struct exynos_ehci_hcd
*exynos_ehci
)
52 struct device_node
*child
;
54 int phy_number
, num_phys
;
57 /* Get PHYs for the controller */
58 num_phys
= of_count_phandle_with_args(dev
->of_node
, "phys",
60 for (phy_number
= 0; phy_number
< num_phys
; phy_number
++) {
61 phy
= devm_of_phy_get_by_index(dev
, dev
->of_node
, phy_number
);
64 exynos_ehci
->phy
[phy_number
] = phy
;
69 /* Get PHYs using legacy bindings */
70 for_each_available_child_of_node(dev
->of_node
, child
) {
71 ret
= of_property_read_u32(child
, "reg", &phy_number
);
73 dev_err(dev
, "Failed to parse device tree\n");
78 if (phy_number
>= PHY_NUMBER
) {
79 dev_err(dev
, "Invalid number of PHYs\n");
84 phy
= devm_of_phy_get(dev
, child
, NULL
);
85 exynos_ehci
->phy
[phy_number
] = phy
;
88 if (ret
== -EPROBE_DEFER
) {
91 } else if (ret
!= -ENOSYS
&& ret
!= -ENODEV
) {
93 "Error retrieving usb2 phy: %d\n", ret
);
100 exynos_ehci
->legacy_phy
= true;
104 static int exynos_ehci_phy_enable(struct device
*dev
)
106 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
107 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
111 for (i
= 0; ret
== 0 && i
< PHY_NUMBER
; i
++)
112 if (!IS_ERR(exynos_ehci
->phy
[i
]))
113 ret
= phy_power_on(exynos_ehci
->phy
[i
]);
115 for (i
--; i
>= 0; i
--)
116 if (!IS_ERR(exynos_ehci
->phy
[i
]))
117 phy_power_off(exynos_ehci
->phy
[i
]);
122 static void exynos_ehci_phy_disable(struct device
*dev
)
124 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
125 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
128 for (i
= 0; i
< PHY_NUMBER
; i
++)
129 if (!IS_ERR(exynos_ehci
->phy
[i
]))
130 phy_power_off(exynos_ehci
->phy
[i
]);
133 static void exynos_setup_vbus_gpio(struct device
*dev
)
141 gpio
= of_get_named_gpio(dev
->of_node
, "samsung,vbus-gpio", 0);
142 if (!gpio_is_valid(gpio
))
145 err
= devm_gpio_request_one(dev
, gpio
, GPIOF_OUT_INIT_HIGH
,
148 dev_err(dev
, "can't request ehci vbus gpio %d", gpio
);
151 static int exynos_ehci_probe(struct platform_device
*pdev
)
153 struct exynos_ehci_hcd
*exynos_ehci
;
155 struct ehci_hcd
*ehci
;
156 struct resource
*res
;
161 * Right now device-tree probed devices don't get dma_mask set.
162 * Since shared usb code relies on it, set it here for now.
163 * Once we move to full device tree support this will vanish off.
165 err
= dma_coerce_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
169 exynos_setup_vbus_gpio(&pdev
->dev
);
171 hcd
= usb_create_hcd(&exynos_ehci_hc_driver
,
172 &pdev
->dev
, dev_name(&pdev
->dev
));
174 dev_err(&pdev
->dev
, "Unable to create HCD\n");
177 exynos_ehci
= to_exynos_ehci(hcd
);
179 err
= exynos_ehci_get_phy(&pdev
->dev
, exynos_ehci
);
183 exynos_ehci
->clk
= devm_clk_get(&pdev
->dev
, "usbhost");
185 if (IS_ERR(exynos_ehci
->clk
)) {
186 dev_err(&pdev
->dev
, "Failed to get usbhost clock\n");
187 err
= PTR_ERR(exynos_ehci
->clk
);
191 err
= clk_prepare_enable(exynos_ehci
->clk
);
195 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
196 hcd
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
197 if (IS_ERR(hcd
->regs
)) {
198 err
= PTR_ERR(hcd
->regs
);
202 hcd
->rsrc_start
= res
->start
;
203 hcd
->rsrc_len
= resource_size(res
);
205 irq
= platform_get_irq(pdev
, 0);
207 dev_err(&pdev
->dev
, "Failed to get IRQ\n");
212 err
= exynos_ehci_phy_enable(&pdev
->dev
);
214 dev_err(&pdev
->dev
, "Failed to enable USB phy\n");
218 ehci
= hcd_to_ehci(hcd
);
219 ehci
->caps
= hcd
->regs
;
222 * Workaround: reset of_node pointer to avoid conflict between legacy
223 * Exynos EHCI port subnodes and generic USB device bindings
225 exynos_ehci
->of_node
= pdev
->dev
.of_node
;
226 if (exynos_ehci
->legacy_phy
)
227 pdev
->dev
.of_node
= NULL
;
229 /* DMA burst Enable */
230 writel(EHCI_INSNREG00_ENABLE_DMA_BURST
, EHCI_INSNREG00(hcd
->regs
));
232 err
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
234 dev_err(&pdev
->dev
, "Failed to add USB HCD\n");
237 device_wakeup_enable(hcd
->self
.controller
);
239 platform_set_drvdata(pdev
, hcd
);
244 exynos_ehci_phy_disable(&pdev
->dev
);
245 pdev
->dev
.of_node
= exynos_ehci
->of_node
;
247 clk_disable_unprepare(exynos_ehci
->clk
);
253 static int exynos_ehci_remove(struct platform_device
*pdev
)
255 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
256 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
258 pdev
->dev
.of_node
= exynos_ehci
->of_node
;
262 exynos_ehci_phy_disable(&pdev
->dev
);
264 clk_disable_unprepare(exynos_ehci
->clk
);
272 static int exynos_ehci_suspend(struct device
*dev
)
274 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
275 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
277 bool do_wakeup
= device_may_wakeup(dev
);
280 rc
= ehci_suspend(hcd
, do_wakeup
);
284 exynos_ehci_phy_disable(dev
);
286 clk_disable_unprepare(exynos_ehci
->clk
);
291 static int exynos_ehci_resume(struct device
*dev
)
293 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
294 struct exynos_ehci_hcd
*exynos_ehci
= to_exynos_ehci(hcd
);
297 ret
= clk_prepare_enable(exynos_ehci
->clk
);
301 ret
= exynos_ehci_phy_enable(dev
);
303 dev_err(dev
, "Failed to enable USB phy\n");
304 clk_disable_unprepare(exynos_ehci
->clk
);
308 /* DMA burst Enable */
309 writel(EHCI_INSNREG00_ENABLE_DMA_BURST
, EHCI_INSNREG00(hcd
->regs
));
311 ehci_resume(hcd
, false);
315 #define exynos_ehci_suspend NULL
316 #define exynos_ehci_resume NULL
319 static const struct dev_pm_ops exynos_ehci_pm_ops
= {
320 .suspend
= exynos_ehci_suspend
,
321 .resume
= exynos_ehci_resume
,
325 static const struct of_device_id exynos_ehci_match
[] = {
326 { .compatible
= "samsung,exynos4210-ehci" },
329 MODULE_DEVICE_TABLE(of
, exynos_ehci_match
);
332 static struct platform_driver exynos_ehci_driver
= {
333 .probe
= exynos_ehci_probe
,
334 .remove
= exynos_ehci_remove
,
335 .shutdown
= usb_hcd_platform_shutdown
,
337 .name
= "exynos-ehci",
338 .pm
= &exynos_ehci_pm_ops
,
339 .of_match_table
= of_match_ptr(exynos_ehci_match
),
342 static const struct ehci_driver_overrides exynos_overrides __initconst
= {
343 .extra_priv_size
= sizeof(struct exynos_ehci_hcd
),
346 static int __init
ehci_exynos_init(void)
351 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
352 ehci_init_driver(&exynos_ehci_hc_driver
, &exynos_overrides
);
353 return platform_driver_register(&exynos_ehci_driver
);
355 module_init(ehci_exynos_init
);
357 static void __exit
ehci_exynos_cleanup(void)
359 platform_driver_unregister(&exynos_ehci_driver
);
361 module_exit(ehci_exynos_cleanup
);
363 MODULE_DESCRIPTION(DRIVER_DESC
);
364 MODULE_ALIAS("platform:exynos-ehci");
365 MODULE_AUTHOR("Jingoo Han");
366 MODULE_AUTHOR("Joonyoung Shim");
367 MODULE_LICENSE("GPL v2");