xref: /openbmc/linux/drivers/usb/host/ehci-platform.c (revision 2359ccdd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic platform ehci driver
4  *
5  * Copyright 2007 Steven Brown <sbrown@cortland.com>
6  * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
7  * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
8  *
9  * Derived from the ohci-ssb driver
10  * Copyright 2007 Michael Buesch <m@bues.ch>
11  *
12  * Derived from the EHCI-PCI driver
13  * Copyright (c) 2000-2004 by David Brownell
14  *
15  * Derived from the ohci-pci driver
16  * Copyright 1999 Roman Weissgaerber
17  * Copyright 2000-2002 David Brownell
18  * Copyright 1999 Linus Torvalds
19  * Copyright 1999 Gregory P. Smith
20  */
21 #include <linux/acpi.h>
22 #include <linux/clk.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/err.h>
25 #include <linux/kernel.h>
26 #include <linux/hrtimer.h>
27 #include <linux/io.h>
28 #include <linux/module.h>
29 #include <linux/of.h>
30 #include <linux/platform_device.h>
31 #include <linux/reset.h>
32 #include <linux/usb.h>
33 #include <linux/usb/hcd.h>
34 #include <linux/usb/ehci_pdriver.h>
35 #include <linux/usb/of.h>
36 
37 #include "ehci.h"
38 
39 #define DRIVER_DESC "EHCI generic platform driver"
40 #define EHCI_MAX_CLKS 4
41 #define hcd_to_ehci_priv(h) ((struct ehci_platform_priv *)hcd_to_ehci(h)->priv)
42 
43 struct ehci_platform_priv {
44 	struct clk *clks[EHCI_MAX_CLKS];
45 	struct reset_control *rsts;
46 	bool reset_on_resume;
47 };
48 
49 static const char hcd_name[] = "ehci-platform";
50 
51 static int ehci_platform_reset(struct usb_hcd *hcd)
52 {
53 	struct platform_device *pdev = to_platform_device(hcd->self.controller);
54 	struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
55 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
56 	int retval;
57 
58 	ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
59 
60 	if (pdata->pre_setup) {
61 		retval = pdata->pre_setup(hcd);
62 		if (retval < 0)
63 			return retval;
64 	}
65 
66 	ehci->caps = hcd->regs + pdata->caps_offset;
67 	retval = ehci_setup(hcd);
68 	if (retval)
69 		return retval;
70 
71 	if (pdata->no_io_watchdog)
72 		ehci->need_io_watchdog = 0;
73 	return 0;
74 }
75 
76 static int ehci_platform_power_on(struct platform_device *dev)
77 {
78 	struct usb_hcd *hcd = platform_get_drvdata(dev);
79 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
80 	int clk, ret;
81 
82 	for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++) {
83 		ret = clk_prepare_enable(priv->clks[clk]);
84 		if (ret)
85 			goto err_disable_clks;
86 	}
87 
88 	return 0;
89 
90 err_disable_clks:
91 	while (--clk >= 0)
92 		clk_disable_unprepare(priv->clks[clk]);
93 
94 	return ret;
95 }
96 
97 static void ehci_platform_power_off(struct platform_device *dev)
98 {
99 	struct usb_hcd *hcd = platform_get_drvdata(dev);
100 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
101 	int clk;
102 
103 	for (clk = EHCI_MAX_CLKS - 1; clk >= 0; clk--)
104 		if (priv->clks[clk])
105 			clk_disable_unprepare(priv->clks[clk]);
106 }
107 
108 static struct hc_driver __read_mostly ehci_platform_hc_driver;
109 
110 static const struct ehci_driver_overrides platform_overrides __initconst = {
111 	.reset =		ehci_platform_reset,
112 	.extra_priv_size =	sizeof(struct ehci_platform_priv),
113 };
114 
115 static struct usb_ehci_pdata ehci_platform_defaults = {
116 	.power_on =		ehci_platform_power_on,
117 	.power_suspend =	ehci_platform_power_off,
118 	.power_off =		ehci_platform_power_off,
119 };
120 
121 static int ehci_platform_probe(struct platform_device *dev)
122 {
123 	struct usb_hcd *hcd;
124 	struct resource *res_mem;
125 	struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
126 	struct ehci_platform_priv *priv;
127 	struct ehci_hcd *ehci;
128 	int err, irq, clk = 0;
129 
130 	if (usb_disabled())
131 		return -ENODEV;
132 
133 	/*
134 	 * Use reasonable defaults so platforms don't have to provide these
135 	 * with DT probing on ARM.
136 	 */
137 	if (!pdata)
138 		pdata = &ehci_platform_defaults;
139 
140 	err = dma_coerce_mask_and_coherent(&dev->dev,
141 		pdata->dma_mask_64 ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
142 	if (err) {
143 		dev_err(&dev->dev, "Error: DMA mask configuration failed\n");
144 		return err;
145 	}
146 
147 	irq = platform_get_irq(dev, 0);
148 	if (irq < 0) {
149 		dev_err(&dev->dev, "no irq provided");
150 		return irq;
151 	}
152 
153 	hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
154 			     dev_name(&dev->dev));
155 	if (!hcd)
156 		return -ENOMEM;
157 
158 	platform_set_drvdata(dev, hcd);
159 	dev->dev.platform_data = pdata;
160 	priv = hcd_to_ehci_priv(hcd);
161 	ehci = hcd_to_ehci(hcd);
162 
163 	if (pdata == &ehci_platform_defaults && dev->dev.of_node) {
164 		if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
165 			ehci->big_endian_mmio = 1;
166 
167 		if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
168 			ehci->big_endian_desc = 1;
169 
170 		if (of_property_read_bool(dev->dev.of_node, "big-endian"))
171 			ehci->big_endian_mmio = ehci->big_endian_desc = 1;
172 
173 		if (of_property_read_bool(dev->dev.of_node,
174 					  "needs-reset-on-resume"))
175 			priv->reset_on_resume = true;
176 
177 		if (of_property_read_bool(dev->dev.of_node,
178 					  "has-transaction-translator"))
179 			hcd->has_tt = 1;
180 
181 		for (clk = 0; clk < EHCI_MAX_CLKS; clk++) {
182 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
183 			if (IS_ERR(priv->clks[clk])) {
184 				err = PTR_ERR(priv->clks[clk]);
185 				if (err == -EPROBE_DEFER)
186 					goto err_put_clks;
187 				priv->clks[clk] = NULL;
188 				break;
189 			}
190 		}
191 	}
192 
193 	priv->rsts = devm_reset_control_array_get_optional_shared(&dev->dev);
194 	if (IS_ERR(priv->rsts)) {
195 		err = PTR_ERR(priv->rsts);
196 		goto err_put_clks;
197 	}
198 
199 	err = reset_control_deassert(priv->rsts);
200 	if (err)
201 		goto err_put_clks;
202 
203 	if (pdata->big_endian_desc)
204 		ehci->big_endian_desc = 1;
205 	if (pdata->big_endian_mmio)
206 		ehci->big_endian_mmio = 1;
207 	if (pdata->has_tt)
208 		hcd->has_tt = 1;
209 	if (pdata->reset_on_resume)
210 		priv->reset_on_resume = true;
211 
212 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
213 	if (ehci->big_endian_mmio) {
214 		dev_err(&dev->dev,
215 			"Error: CONFIG_USB_EHCI_BIG_ENDIAN_MMIO not set\n");
216 		err = -EINVAL;
217 		goto err_reset;
218 	}
219 #endif
220 #ifndef CONFIG_USB_EHCI_BIG_ENDIAN_DESC
221 	if (ehci->big_endian_desc) {
222 		dev_err(&dev->dev,
223 			"Error: CONFIG_USB_EHCI_BIG_ENDIAN_DESC not set\n");
224 		err = -EINVAL;
225 		goto err_reset;
226 	}
227 #endif
228 
229 	if (pdata->power_on) {
230 		err = pdata->power_on(dev);
231 		if (err < 0)
232 			goto err_reset;
233 	}
234 
235 	res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
236 	hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
237 	if (IS_ERR(hcd->regs)) {
238 		err = PTR_ERR(hcd->regs);
239 		goto err_power;
240 	}
241 	hcd->rsrc_start = res_mem->start;
242 	hcd->rsrc_len = resource_size(res_mem);
243 
244 	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
245 	if (err)
246 		goto err_power;
247 
248 	device_wakeup_enable(hcd->self.controller);
249 	device_enable_async_suspend(hcd->self.controller);
250 	platform_set_drvdata(dev, hcd);
251 
252 	return err;
253 
254 err_power:
255 	if (pdata->power_off)
256 		pdata->power_off(dev);
257 err_reset:
258 	reset_control_assert(priv->rsts);
259 err_put_clks:
260 	while (--clk >= 0)
261 		clk_put(priv->clks[clk]);
262 
263 	if (pdata == &ehci_platform_defaults)
264 		dev->dev.platform_data = NULL;
265 
266 	usb_put_hcd(hcd);
267 
268 	return err;
269 }
270 
271 static int ehci_platform_remove(struct platform_device *dev)
272 {
273 	struct usb_hcd *hcd = platform_get_drvdata(dev);
274 	struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
275 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
276 	int clk;
277 
278 	usb_remove_hcd(hcd);
279 
280 	if (pdata->power_off)
281 		pdata->power_off(dev);
282 
283 	reset_control_assert(priv->rsts);
284 
285 	for (clk = 0; clk < EHCI_MAX_CLKS && priv->clks[clk]; clk++)
286 		clk_put(priv->clks[clk]);
287 
288 	usb_put_hcd(hcd);
289 
290 	if (pdata == &ehci_platform_defaults)
291 		dev->dev.platform_data = NULL;
292 
293 	return 0;
294 }
295 
296 #ifdef CONFIG_PM_SLEEP
297 static int ehci_platform_suspend(struct device *dev)
298 {
299 	struct usb_hcd *hcd = dev_get_drvdata(dev);
300 	struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
301 	struct platform_device *pdev = to_platform_device(dev);
302 	bool do_wakeup = device_may_wakeup(dev);
303 	int ret;
304 
305 	ret = ehci_suspend(hcd, do_wakeup);
306 	if (ret)
307 		return ret;
308 
309 	if (pdata->power_suspend)
310 		pdata->power_suspend(pdev);
311 
312 	return ret;
313 }
314 
315 static int ehci_platform_resume(struct device *dev)
316 {
317 	struct usb_hcd *hcd = dev_get_drvdata(dev);
318 	struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
319 	struct platform_device *pdev = to_platform_device(dev);
320 	struct ehci_platform_priv *priv = hcd_to_ehci_priv(hcd);
321 	struct device *companion_dev;
322 
323 	if (pdata->power_on) {
324 		int err = pdata->power_on(pdev);
325 		if (err < 0)
326 			return err;
327 	}
328 
329 	companion_dev = usb_of_get_companion_dev(hcd->self.controller);
330 	if (companion_dev) {
331 		device_pm_wait_for_dev(hcd->self.controller, companion_dev);
332 		put_device(companion_dev);
333 	}
334 
335 	ehci_resume(hcd, priv->reset_on_resume);
336 	return 0;
337 }
338 #endif /* CONFIG_PM_SLEEP */
339 
340 static const struct of_device_id vt8500_ehci_ids[] = {
341 	{ .compatible = "via,vt8500-ehci", },
342 	{ .compatible = "wm,prizm-ehci", },
343 	{ .compatible = "generic-ehci", },
344 	{ .compatible = "cavium,octeon-6335-ehci", },
345 	{}
346 };
347 MODULE_DEVICE_TABLE(of, vt8500_ehci_ids);
348 
349 static const struct acpi_device_id ehci_acpi_match[] = {
350 	{ "PNP0D20", 0 }, /* EHCI controller without debug */
351 	{ }
352 };
353 MODULE_DEVICE_TABLE(acpi, ehci_acpi_match);
354 
355 static const struct platform_device_id ehci_platform_table[] = {
356 	{ "ehci-platform", 0 },
357 	{ }
358 };
359 MODULE_DEVICE_TABLE(platform, ehci_platform_table);
360 
361 static SIMPLE_DEV_PM_OPS(ehci_platform_pm_ops, ehci_platform_suspend,
362 	ehci_platform_resume);
363 
364 static struct platform_driver ehci_platform_driver = {
365 	.id_table	= ehci_platform_table,
366 	.probe		= ehci_platform_probe,
367 	.remove		= ehci_platform_remove,
368 	.shutdown	= usb_hcd_platform_shutdown,
369 	.driver		= {
370 		.name	= "ehci-platform",
371 		.pm	= &ehci_platform_pm_ops,
372 		.of_match_table = vt8500_ehci_ids,
373 		.acpi_match_table = ACPI_PTR(ehci_acpi_match),
374 	}
375 };
376 
377 static int __init ehci_platform_init(void)
378 {
379 	if (usb_disabled())
380 		return -ENODEV;
381 
382 	pr_info("%s: " DRIVER_DESC "\n", hcd_name);
383 
384 	ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
385 	return platform_driver_register(&ehci_platform_driver);
386 }
387 module_init(ehci_platform_init);
388 
389 static void __exit ehci_platform_cleanup(void)
390 {
391 	platform_driver_unregister(&ehci_platform_driver);
392 }
393 module_exit(ehci_platform_cleanup);
394 
395 MODULE_DESCRIPTION(DRIVER_DESC);
396 MODULE_AUTHOR("Hauke Mehrtens");
397 MODULE_AUTHOR("Alan Stern");
398 MODULE_LICENSE("GPL");
399