blob: 9d2d37b3a25fd99a5eef6902470cdf1c75ff7373 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
4 * Author: Chao Xie <chao.xie@marvell.com>
5 * Neil Zhang <zhangwm@marvell.com>
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/platform_device.h>
11#include <linux/clk.h>
12#include <linux/of.h>
13#include <linux/of_device.h>
14#include <linux/err.h>
15#include <linux/usb/otg.h>
16#include <linux/platform_data/mv_usb.h>
17#include <linux/io.h>
18#include <linux/usb/mv_usb2_phy.h>
19#include <linux/usb/hcd.h>
20#include <linux/cputype.h>
21#include <linux/pm_qos.h>
22#include "ehci.h"
23
24/* registers */
25#define U2x_CAPREGS_OFFSET 0x100
26
27#define CAPLENGTH_MASK (0xff)
28
29#define hcd_to_ehci_hcd_mv(h) ((struct ehci_hcd_mv *)hcd_to_ehci(h)->priv)
30
31struct ehci_hcd_mv {
32 struct usb_hcd *hcd;
33 struct usb_phy *phy;
34
35 /* Which mode does this ehci running OTG/Host ? */
36 int mode;
37
38 void __iomem *base;
39 void __iomem *cap_regs;
40 void __iomem *op_regs;
41
42 struct usb_phy *otg;
43
44 struct mv_usb_platform_data *pdata;
45
46 struct clk *clk;
47
48 int (*set_vbus)(unsigned int vbus);
49
50#ifdef CONFIG_CPU_ASR18XX
51 struct pm_qos_request qos_idle;
52 s32 lpm_qos;
53#endif
54};
55
56static int mv_ehci_enable(struct ehci_hcd_mv *ehci_mv)
57{
58 int retval;
59
60 retval = clk_prepare_enable(ehci_mv->clk);
61 if (retval) {
62 pr_err("%s: clk_prepare_enable failed\n", __func__);
63 return retval;
64 }
65
66 retval = usb_phy_init(ehci_mv->phy);
67 if (retval) {
68 pr_err("%s: usb phy init failed\n", __func__);
69 clk_disable_unprepare(ehci_mv->clk);
70 }
71 return retval;
72}
73
74static void mv_ehci_disable(struct ehci_hcd_mv *ehci_mv)
75{
76 usb_phy_shutdown(ehci_mv->phy);
77 clk_disable_unprepare(ehci_mv->clk);
78}
79
80static int mv_ehci_reset(struct usb_hcd *hcd)
81{
82 struct device *dev = hcd->self.controller;
83 struct ehci_hcd_mv *ehci_mv = hcd_to_ehci_hcd_mv(hcd);
84 int retval;
85
86 if (ehci_mv == NULL) {
87 dev_err(dev, "Can not find private ehci data\n");
88 return -ENODEV;
89 }
90
91 hcd->has_tt = 1;
92
93 retval = ehci_setup(hcd);
94 if (retval)
95 dev_err(dev, "ehci_setup failed %d\n", retval);
96
97 return retval;
98}
99
100static struct hc_driver __read_mostly ehci_platform_hc_driver;
101
102static const struct ehci_driver_overrides platform_overrides __initconst = {
103 .reset = mv_ehci_reset,
104 .extra_priv_size = sizeof(struct ehci_hcd_mv),
105};
106
107static int mv_ehci_probe(struct platform_device *pdev)
108{
109 struct mv_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
110 struct usb_hcd *hcd;
111 struct ehci_hcd *ehci;
112 struct ehci_hcd_mv *ehci_mv;
113 struct resource *r;
114 int retval = -ENODEV;
115 u32 offset;
116 const __be32 *prop;
117 unsigned int proplen;
118
119 if (!pdata) {
120 dev_err(&pdev->dev, "missing platform_data\n");
121 return -ENODEV;
122 }
123 if (usb_disabled()) {
124 pr_err("usb disabled\n");
125 return -ENODEV;
126 }
127 hcd = usb_create_hcd(&ehci_platform_hc_driver, &pdev->dev, "mv ehci");
128 if (!hcd) {
129 pr_err("usb_create_hcd failed\n");
130 return -ENOMEM;
131 }
132 platform_set_drvdata(pdev, hcd);
133 ehci_mv = hcd_to_ehci_hcd_mv(hcd);
134
135 ehci_mv->mode = MV_USB_MODE_HOST;
136 if (pdata) {
137 ehci_mv->mode = pdata->mode;
138 /* ehci_mv->set_vbus = pdata->set_vbus; */
139 }
140
141 ehci_mv->pdata = pdata;
142 ehci_mv->hcd = hcd;
143
144 ehci_mv->clk = devm_clk_get(&pdev->dev, NULL);
145 if (IS_ERR(ehci_mv->clk)) {
146 dev_err(&pdev->dev, "error getting clock\n");
147 retval = PTR_ERR(ehci_mv->clk);
148 goto err_clear_drvdata;
149 }
150
151 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
152 if (!r) {
153 dev_err(&pdev->dev, "no I/O memory resource defined\n");
154 retval = -ENODEV;
155 goto err_clear_drvdata;
156 }
157 ehci_mv->base = devm_ioremap_resource(&pdev->dev, r);
158 if (IS_ERR(ehci_mv->base)) {
159 retval = PTR_ERR(ehci_mv->base);
160 dev_err(&pdev->dev, "failed to map I/O memory\n");
161 goto err_clear_drvdata;
162 }
163 ehci_mv->phy = devm_usb_get_phy_dev(&pdev->dev, MV_USB2_PHY_INDEX);
164 if (IS_ERR_OR_NULL(ehci_mv->phy)) {
165 retval = PTR_ERR(ehci_mv->phy);
166 if (retval != -EPROBE_DEFER && retval != -ENODEV)
167 dev_err(&pdev->dev, "failed to get the outer phy\n");
168 else {
169 usb_put_hcd(hcd);
170 devm_kfree(&pdev->dev, ehci_mv);
171 platform_set_drvdata(pdev, NULL);
172 dev_err(&pdev->dev, "EPROBE_DEFER\n");
173 return -EPROBE_DEFER;
174 }
175 goto err_clear_drvdata;
176 }
177 retval = mv_ehci_enable(ehci_mv);
178 if (retval) {
179 dev_err(&pdev->dev, "init phy error %d\n", retval);
180 goto err_clear_drvdata;
181 }
182
183 ehci_mv->cap_regs =
184 (void __iomem *) ((unsigned long) ehci_mv->base + U2x_CAPREGS_OFFSET);
185 offset = readl(ehci_mv->cap_regs) & CAPLENGTH_MASK;
186 ehci_mv->op_regs =
187 (void __iomem *) ((unsigned long) ehci_mv->cap_regs + offset);
188
189 hcd->rsrc_start = r->start;
190 hcd->rsrc_len = resource_size(r);
191 hcd->regs = ehci_mv->op_regs;
192#if defined(CONFIG_CPU_ASR18XX)
193 hcd->usb_phy = ehci_mv->phy;
194 hcd->usb_phy->io_op_regs = ehci_mv->op_regs;
195#endif
196
197 retval = platform_get_irq(pdev, 0);
198 if (retval < 0) {
199 pr_err("platform_get_irq failed\n");
200 goto err_disable_clk;
201 }
202 hcd->irq = retval;
203
204 ehci = hcd_to_ehci(hcd);
205 ehci->caps = (struct ehci_caps *) ehci_mv->cap_regs;
206
207 if (ehci_mv->mode == MV_USB_MODE_OTG) {
208 ehci_mv->otg = devm_usb_get_phy_dev(&pdev->dev,
209 MV_USB2_OTG_PHY_INDEX);
210 if (IS_ERR(ehci_mv->otg)) {
211 retval = PTR_ERR(ehci_mv->otg);
212
213 if (retval == -ENXIO)
214 dev_info(&pdev->dev, "MV_USB_MODE_OTG "
215 "must have CONFIG_USB_PHY enabled\n");
216 else
217 dev_err(&pdev->dev,
218 "unable to find transceiver\n");
219 goto err_disable_clk;
220 }
221
222 retval = otg_set_host(ehci_mv->otg->otg, &hcd->self);
223 if (retval < 0) {
224 dev_err(&pdev->dev,
225 "unable to register with transceiver\n");
226 retval = -ENODEV;
227 goto err_disable_clk;
228 }
229 /* otg will enable clock before use as host */
230 mv_ehci_disable(ehci_mv);
231 } else {
232 prop = of_get_property(pdev->dev.of_node, "lpm-qos", &proplen);
233 if (!prop) {
234 pr_err("lpm-qos config in DT for mv_hsic_host is not defined\n");
235 goto err_disable_clk;
236 } else
237 ehci_mv->lpm_qos = be32_to_cpup(prop);
238
239 pxa_usb_extern_call(pdata->id, vbus, set_vbus, 1);
240 retval = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
241 if (retval) {
242 dev_err(&pdev->dev,
243 "failed to add hcd with err %d\n", retval);
244 goto err_set_vbus;
245 } else {
246#ifdef CONFIG_CPU_ASR18XX
247 ehci_mv->qos_idle.name = pdev->name;
248 pm_qos_add_request(&ehci_mv->qos_idle, PM_QOS_CPUIDLE_BLOCK,
249 PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE);
250 pm_qos_update_request(&ehci_mv->qos_idle, ehci_mv->lpm_qos);
251#endif
252 }
253 device_wakeup_enable(hcd->self.controller);
254 }
255
256 dev_info(&pdev->dev,
257 "successful find EHCI device with regs 0x%p irq %d"
258 " working in %s mode\n", hcd->regs, hcd->irq,
259 ehci_mv->mode == MV_USB_MODE_OTG ? "OTG" : "Host");
260
261 return 0;
262
263err_set_vbus:
264 pxa_usb_extern_call(pdata->id, vbus, set_vbus, 0);
265err_disable_clk:
266 mv_ehci_disable(ehci_mv);
267err_clear_drvdata:
268 platform_set_drvdata(pdev, NULL);
269 usb_put_hcd(hcd);
270
271 return retval;
272}
273
274static int mv_ehci_remove(struct platform_device *pdev)
275{
276 struct usb_hcd *hcd = platform_get_drvdata(pdev);
277 struct ehci_hcd_mv *ehci_mv = hcd_to_ehci_hcd_mv(hcd);
278
279 if (hcd->rh_registered)
280 usb_remove_hcd(hcd);
281
282#ifdef CONFIG_CPU_ASR18XX
283 pm_qos_update_request(&ehci_mv->qos_idle,
284 PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE);
285 pm_qos_remove_request(&ehci_mv->qos_idle);
286#endif
287
288 if (!IS_ERR_OR_NULL(ehci_mv->otg))
289 otg_set_host(ehci_mv->otg->otg, NULL);
290
291 if (ehci_mv->mode == MV_USB_MODE_HOST) {
292 pxa_usb_extern_call(ehci_mv->pdata->id, vbus, set_vbus, 0);
293
294 mv_ehci_disable(ehci_mv);
295 }
296
297 platform_set_drvdata(pdev, NULL);
298
299 usb_put_hcd(hcd);
300
301 return 0;
302}
303
304MODULE_ALIAS("mv-ehci");
305
306static const struct platform_device_id ehci_id_table[] = {
307 {"pxa-u2oehci", PXA_U2OEHCI},
308 {"pxa-sph", PXA_SPH},
309 {"mmp3-hsic", MMP3_HSIC},
310 {"mmp3-fsic", MMP3_FSIC},
311 {},
312};
313
314static void mv_ehci_shutdown(struct platform_device *pdev)
315{
316 struct usb_hcd *hcd = platform_get_drvdata(pdev);
317
318 if (!hcd->rh_registered)
319 return;
320
321 if (hcd->driver->shutdown)
322 hcd->driver->shutdown(hcd);
323}
324
325#ifdef CONFIG_PM
326static int mv_ehci_suspend_noirq(struct device *dev)
327{
328 struct usb_hcd *hcd = dev_get_drvdata(dev);
329 struct ehci_hcd_mv *ehci_mv = hcd_to_ehci_hcd_mv(hcd);
330
331 if (ehci_mv && ehci_mv->mode != MV_USB_MODE_OTG)
332 pm_qos_update_request(&ehci_mv->qos_idle,
333 PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE);
334 return 0;
335}
336
337static int mv_ehci_resume_noirq(struct device *dev)
338{
339 struct usb_hcd *hcd = dev_get_drvdata(dev);
340 struct ehci_hcd_mv *ehci_mv = hcd_to_ehci_hcd_mv(hcd);
341
342 if (ehci_mv && ehci_mv->mode != MV_USB_MODE_OTG)
343 pm_qos_update_request(&ehci_mv->qos_idle, ehci_mv->lpm_qos);
344
345 return 0;
346}
347
348static struct dev_pm_ops mv_ehci_pm_ops = {
349 .suspend_noirq = mv_ehci_suspend_noirq,
350 .resume_noirq = mv_ehci_resume_noirq,
351};
352#endif
353
354static const struct of_device_id ehci_mv_dt_ids[] = {
355 { .compatible = "marvell,pxau2o-ehci", },
356 {},
357};
358
359static struct platform_driver ehci_mv_driver = {
360 .probe = mv_ehci_probe,
361 .remove = mv_ehci_remove,
362 .shutdown = mv_ehci_shutdown,
363 .driver = {
364 .name = "mv-ehci",
365 .bus = &platform_bus_type,
366 .of_match_table = ehci_mv_dt_ids,
367#ifdef CONFIG_PM
368 .pm = &mv_ehci_pm_ops,
369#endif
370 },
371 .id_table = ehci_id_table,
372};
373
374static int __init ehci_platform_init(void)
375{
376 if (usb_disabled())
377 return -ENODEV;
378
379 ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
380 return platform_driver_register(&ehci_mv_driver);
381}
382module_init(ehci_platform_init);
383
384static void __exit ehci_platform_cleanup(void)
385{
386 platform_driver_unregister(&ehci_mv_driver);
387}
388module_exit(ehci_platform_cleanup);
389
390MODULE_DESCRIPTION("Marvell EHCI driver");
391MODULE_AUTHOR("Chao Xie <chao.xie@marvell.com>");
392MODULE_AUTHOR("Neil Zhang <zhangwm@marvell.com>");
393MODULE_ALIAS("mv-ehci");
394MODULE_LICENSE("GPL");
395MODULE_DEVICE_TABLE(of, ehci_mv_dt_ids);