1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
7 * Author: Peter Griffin <peter.griffin@linaro.org>
9 * Derived from ehci-platform.c
12 #include <linux/clk.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/hrtimer.h>
18 #include <linux/module.h>
20 #include <linux/phy/phy.h>
21 #include <linux/platform_device.h>
22 #include <linux/reset.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25 #include <linux/usb/ehci_pdriver.h>
26 #include <linux/pinctrl/consumer.h>
30 #define USB_MAX_CLKS 3
32 struct st_ehci_platform_priv
{
33 struct clk
*clks
[USB_MAX_CLKS
];
35 struct reset_control
*rst
;
36 struct reset_control
*pwr
;
40 #define DRIVER_DESC "EHCI STMicroelectronics driver"
42 #define hcd_to_ehci_priv(h) \
43 ((struct st_ehci_platform_priv *)hcd_to_ehci(h)->priv)
45 static const char hcd_name
[] = "ehci-st";
47 #define EHCI_CAPS_SIZE 0x10
48 #define AHB2STBUS_INSREG01 (EHCI_CAPS_SIZE + 0x84)
50 static int st_ehci_platform_reset(struct usb_hcd
*hcd
)
52 struct platform_device
*pdev
= to_platform_device(hcd
->self
.controller
);
53 struct usb_ehci_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
54 struct ehci_hcd
*ehci
= hcd_to_ehci(hcd
);
57 /* Set EHCI packet buffer IN/OUT threshold to 128 bytes */
58 threshold
= 128 | (128 << 16);
59 writel(threshold
, hcd
->regs
+ AHB2STBUS_INSREG01
);
61 ehci
->caps
= hcd
->regs
+ pdata
->caps_offset
;
62 return ehci_setup(hcd
);
65 static int st_ehci_platform_power_on(struct platform_device
*dev
)
67 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
68 struct st_ehci_platform_priv
*priv
= hcd_to_ehci_priv(hcd
);
71 ret
= reset_control_deassert(priv
->pwr
);
75 ret
= reset_control_deassert(priv
->rst
);
77 goto err_assert_power
;
79 /* some SoCs don't have a dedicated 48Mhz clock, but those that do
80 need the rate to be explicitly set */
82 ret
= clk_set_rate(priv
->clk48
, 48000000);
84 goto err_assert_reset
;
87 for (clk
= 0; clk
< USB_MAX_CLKS
&& priv
->clks
[clk
]; clk
++) {
88 ret
= clk_prepare_enable(priv
->clks
[clk
]);
90 goto err_disable_clks
;
93 ret
= phy_init(priv
->phy
);
95 goto err_disable_clks
;
97 ret
= phy_power_on(priv
->phy
);
107 clk_disable_unprepare(priv
->clks
[clk
]);
109 reset_control_assert(priv
->rst
);
111 reset_control_assert(priv
->pwr
);
116 static void st_ehci_platform_power_off(struct platform_device
*dev
)
118 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
119 struct st_ehci_platform_priv
*priv
= hcd_to_ehci_priv(hcd
);
122 reset_control_assert(priv
->pwr
);
124 reset_control_assert(priv
->rst
);
126 phy_power_off(priv
->phy
);
130 for (clk
= USB_MAX_CLKS
- 1; clk
>= 0; clk
--)
132 clk_disable_unprepare(priv
->clks
[clk
]);
136 static struct hc_driver __read_mostly ehci_platform_hc_driver
;
138 static const struct ehci_driver_overrides platform_overrides __initconst
= {
139 .reset
= st_ehci_platform_reset
,
140 .extra_priv_size
= sizeof(struct st_ehci_platform_priv
),
143 static struct usb_ehci_pdata ehci_platform_defaults
= {
144 .power_on
= st_ehci_platform_power_on
,
145 .power_suspend
= st_ehci_platform_power_off
,
146 .power_off
= st_ehci_platform_power_off
,
149 static int st_ehci_platform_probe(struct platform_device
*dev
)
152 struct resource
*res_mem
;
153 struct usb_ehci_pdata
*pdata
= &ehci_platform_defaults
;
154 struct st_ehci_platform_priv
*priv
;
155 int err
, irq
, clk
= 0;
160 irq
= platform_get_irq(dev
, 0);
163 res_mem
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
165 dev_err(&dev
->dev
, "no memory resource provided");
169 hcd
= usb_create_hcd(&ehci_platform_hc_driver
, &dev
->dev
,
170 dev_name(&dev
->dev
));
174 platform_set_drvdata(dev
, hcd
);
175 dev
->dev
.platform_data
= pdata
;
176 priv
= hcd_to_ehci_priv(hcd
);
178 priv
->phy
= devm_phy_get(&dev
->dev
, "usb");
179 if (IS_ERR(priv
->phy
)) {
180 err
= PTR_ERR(priv
->phy
);
184 for (clk
= 0; clk
< USB_MAX_CLKS
; clk
++) {
185 priv
->clks
[clk
] = of_clk_get(dev
->dev
.of_node
, clk
);
186 if (IS_ERR(priv
->clks
[clk
])) {
187 err
= PTR_ERR(priv
->clks
[clk
]);
188 if (err
== -EPROBE_DEFER
)
190 priv
->clks
[clk
] = NULL
;
195 /* some SoCs don't have a dedicated 48Mhz clock, but those that
196 do need the rate to be explicitly set */
197 priv
->clk48
= devm_clk_get(&dev
->dev
, "clk48");
198 if (IS_ERR(priv
->clk48
)) {
199 dev_info(&dev
->dev
, "48MHz clk not found\n");
204 devm_reset_control_get_optional_shared(&dev
->dev
, "power");
205 if (IS_ERR(priv
->pwr
)) {
206 err
= PTR_ERR(priv
->pwr
);
207 if (err
== -EPROBE_DEFER
)
213 devm_reset_control_get_optional_shared(&dev
->dev
, "softreset");
214 if (IS_ERR(priv
->rst
)) {
215 err
= PTR_ERR(priv
->rst
);
216 if (err
== -EPROBE_DEFER
)
221 if (pdata
->power_on
) {
222 err
= pdata
->power_on(dev
);
227 hcd
->rsrc_start
= res_mem
->start
;
228 hcd
->rsrc_len
= resource_size(res_mem
);
230 hcd
->regs
= devm_ioremap_resource(&dev
->dev
, res_mem
);
231 if (IS_ERR(hcd
->regs
)) {
232 err
= PTR_ERR(hcd
->regs
);
236 err
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
240 device_wakeup_enable(hcd
->self
.controller
);
241 platform_set_drvdata(dev
, hcd
);
247 clk_put(priv
->clks
[clk
]);
249 if (pdata
== &ehci_platform_defaults
)
250 dev
->dev
.platform_data
= NULL
;
257 static int st_ehci_platform_remove(struct platform_device
*dev
)
259 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
260 struct usb_ehci_pdata
*pdata
= dev_get_platdata(&dev
->dev
);
261 struct st_ehci_platform_priv
*priv
= hcd_to_ehci_priv(hcd
);
266 if (pdata
->power_off
)
267 pdata
->power_off(dev
);
269 for (clk
= 0; clk
< USB_MAX_CLKS
&& priv
->clks
[clk
]; clk
++)
270 clk_put(priv
->clks
[clk
]);
274 if (pdata
== &ehci_platform_defaults
)
275 dev
->dev
.platform_data
= NULL
;
280 #ifdef CONFIG_PM_SLEEP
282 static int st_ehci_suspend(struct device
*dev
)
284 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
285 struct usb_ehci_pdata
*pdata
= dev_get_platdata(dev
);
286 struct platform_device
*pdev
= to_platform_device(dev
);
287 bool do_wakeup
= device_may_wakeup(dev
);
290 ret
= ehci_suspend(hcd
, do_wakeup
);
294 if (pdata
->power_suspend
)
295 pdata
->power_suspend(pdev
);
297 pinctrl_pm_select_sleep_state(dev
);
302 static int st_ehci_resume(struct device
*dev
)
304 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
305 struct usb_ehci_pdata
*pdata
= dev_get_platdata(dev
);
306 struct platform_device
*pdev
= to_platform_device(dev
);
309 pinctrl_pm_select_default_state(dev
);
311 if (pdata
->power_on
) {
312 err
= pdata
->power_on(pdev
);
317 ehci_resume(hcd
, false);
321 static SIMPLE_DEV_PM_OPS(st_ehci_pm_ops
, st_ehci_suspend
, st_ehci_resume
);
323 #endif /* CONFIG_PM_SLEEP */
325 static const struct of_device_id st_ehci_ids
[] = {
326 { .compatible
= "st,st-ehci-300x", },
329 MODULE_DEVICE_TABLE(of
, st_ehci_ids
);
331 static struct platform_driver ehci_platform_driver
= {
332 .probe
= st_ehci_platform_probe
,
333 .remove
= st_ehci_platform_remove
,
334 .shutdown
= usb_hcd_platform_shutdown
,
337 #ifdef CONFIG_PM_SLEEP
338 .pm
= &st_ehci_pm_ops
,
340 .of_match_table
= st_ehci_ids
,
344 static int __init
ehci_platform_init(void)
349 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
351 ehci_init_driver(&ehci_platform_hc_driver
, &platform_overrides
);
352 return platform_driver_register(&ehci_platform_driver
);
354 module_init(ehci_platform_init
);
356 static void __exit
ehci_platform_cleanup(void)
358 platform_driver_unregister(&ehci_platform_driver
);
360 module_exit(ehci_platform_cleanup
);
362 MODULE_DESCRIPTION(DRIVER_DESC
);
363 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
364 MODULE_LICENSE("GPL");