xref: /openbmc/linux/drivers/usb/host/ohci-platform.c (revision d088d6b6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic platform ohci driver
4  *
5  * Copyright 2007 Michael Buesch <m@bues.ch>
6  * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
7  * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
8  *
9  * Derived from the OCHI-SSB driver
10  * Derived from the OHCI-PCI driver
11  * Copyright 1999 Roman Weissgaerber
12  * Copyright 2000-2002 David Brownell
13  * Copyright 1999 Linus Torvalds
14  * Copyright 1999 Gregory P. Smith
15  */
16 
17 #include <linux/clk.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/hrtimer.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/err.h>
24 #include <linux/of.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/reset.h>
28 #include <linux/usb/ohci_pdriver.h>
29 #include <linux/usb.h>
30 #include <linux/usb/hcd.h>
31 #include <linux/usb/of.h>
32 
33 #include "ohci.h"
34 
35 #define DRIVER_DESC "OHCI generic platform driver"
36 #define OHCI_MAX_CLKS 4
37 #define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
38 
39 struct ohci_platform_priv {
40 	struct clk *clks[OHCI_MAX_CLKS];
41 	struct reset_control *resets;
42 };
43 
44 static int ohci_platform_power_on(struct platform_device *dev)
45 {
46 	struct usb_hcd *hcd = platform_get_drvdata(dev);
47 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
48 	int clk, ret;
49 
50 	for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
51 		ret = clk_prepare_enable(priv->clks[clk]);
52 		if (ret)
53 			goto err_disable_clks;
54 	}
55 
56 	return 0;
57 
58 err_disable_clks:
59 	while (--clk >= 0)
60 		clk_disable_unprepare(priv->clks[clk]);
61 
62 	return ret;
63 }
64 
65 static void ohci_platform_power_off(struct platform_device *dev)
66 {
67 	struct usb_hcd *hcd = platform_get_drvdata(dev);
68 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
69 	int clk;
70 
71 	for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
72 		if (priv->clks[clk])
73 			clk_disable_unprepare(priv->clks[clk]);
74 }
75 
76 static struct hc_driver __read_mostly ohci_platform_hc_driver;
77 
78 static const struct ohci_driver_overrides platform_overrides __initconst = {
79 	.product_desc =		"Generic Platform OHCI controller",
80 	.extra_priv_size =	sizeof(struct ohci_platform_priv),
81 };
82 
83 static struct usb_ohci_pdata ohci_platform_defaults = {
84 	.power_on =		ohci_platform_power_on,
85 	.power_suspend =	ohci_platform_power_off,
86 	.power_off =		ohci_platform_power_off,
87 };
88 
89 static int ohci_platform_probe(struct platform_device *dev)
90 {
91 	struct usb_hcd *hcd;
92 	struct resource *res_mem;
93 	struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
94 	struct ohci_platform_priv *priv;
95 	struct ohci_hcd *ohci;
96 	int err, irq, clk = 0;
97 
98 	if (usb_disabled())
99 		return -ENODEV;
100 
101 	/*
102 	 * Use reasonable defaults so platforms don't have to provide these
103 	 * with DT probing on ARM.
104 	 */
105 	if (!pdata)
106 		pdata = &ohci_platform_defaults;
107 
108 	err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
109 	if (err)
110 		return err;
111 
112 	irq = platform_get_irq(dev, 0);
113 	if (irq < 0)
114 		return irq;
115 
116 	hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
117 			dev_name(&dev->dev));
118 	if (!hcd)
119 		return -ENOMEM;
120 
121 	platform_set_drvdata(dev, hcd);
122 	dev->dev.platform_data = pdata;
123 	priv = hcd_to_ohci_priv(hcd);
124 	ohci = hcd_to_ohci(hcd);
125 
126 	if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
127 		if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
128 			ohci->flags |= OHCI_QUIRK_BE_MMIO;
129 
130 		if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
131 			ohci->flags |= OHCI_QUIRK_BE_DESC;
132 
133 		if (of_property_read_bool(dev->dev.of_node, "big-endian"))
134 			ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
135 
136 		if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
137 			ohci->flags |= OHCI_QUIRK_FRAME_NO;
138 
139 		if (of_property_read_bool(dev->dev.of_node,
140 					  "remote-wakeup-connected"))
141 			ohci->hc_control = OHCI_CTRL_RWC;
142 
143 		of_property_read_u32(dev->dev.of_node, "num-ports",
144 				     &ohci->num_ports);
145 
146 		for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
147 			priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
148 			if (IS_ERR(priv->clks[clk])) {
149 				err = PTR_ERR(priv->clks[clk]);
150 				if (err == -EPROBE_DEFER)
151 					goto err_put_clks;
152 				priv->clks[clk] = NULL;
153 				break;
154 			}
155 		}
156 
157 		priv->resets = devm_reset_control_array_get_optional_shared(
158 								&dev->dev);
159 		if (IS_ERR(priv->resets)) {
160 			err = PTR_ERR(priv->resets);
161 			goto err_put_clks;
162 		}
163 
164 		err = reset_control_deassert(priv->resets);
165 		if (err)
166 			goto err_put_clks;
167 	}
168 
169 	if (pdata->big_endian_desc)
170 		ohci->flags |= OHCI_QUIRK_BE_DESC;
171 	if (pdata->big_endian_mmio)
172 		ohci->flags |= OHCI_QUIRK_BE_MMIO;
173 	if (pdata->no_big_frame_no)
174 		ohci->flags |= OHCI_QUIRK_FRAME_NO;
175 	if (pdata->num_ports)
176 		ohci->num_ports = pdata->num_ports;
177 
178 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
179 	if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
180 		dev_err(&dev->dev,
181 			"Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
182 		err = -EINVAL;
183 		goto err_reset;
184 	}
185 #endif
186 #ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
187 	if (ohci->flags & OHCI_QUIRK_BE_DESC) {
188 		dev_err(&dev->dev,
189 			"Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
190 		err = -EINVAL;
191 		goto err_reset;
192 	}
193 #endif
194 
195 	pm_runtime_set_active(&dev->dev);
196 	pm_runtime_enable(&dev->dev);
197 	if (pdata->power_on) {
198 		err = pdata->power_on(dev);
199 		if (err < 0)
200 			goto err_reset;
201 	}
202 
203 	res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
204 	hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
205 	if (IS_ERR(hcd->regs)) {
206 		err = PTR_ERR(hcd->regs);
207 		goto err_power;
208 	}
209 	hcd->rsrc_start = res_mem->start;
210 	hcd->rsrc_len = resource_size(res_mem);
211 
212 	hcd->tpl_support = of_usb_host_tpl_support(dev->dev.of_node);
213 
214 	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
215 	if (err)
216 		goto err_power;
217 
218 	device_wakeup_enable(hcd->self.controller);
219 
220 	platform_set_drvdata(dev, hcd);
221 
222 	return err;
223 
224 err_power:
225 	if (pdata->power_off)
226 		pdata->power_off(dev);
227 err_reset:
228 	pm_runtime_disable(&dev->dev);
229 	reset_control_assert(priv->resets);
230 err_put_clks:
231 	while (--clk >= 0)
232 		clk_put(priv->clks[clk]);
233 
234 	if (pdata == &ohci_platform_defaults)
235 		dev->dev.platform_data = NULL;
236 
237 	usb_put_hcd(hcd);
238 
239 	return err;
240 }
241 
242 static void ohci_platform_remove(struct platform_device *dev)
243 {
244 	struct usb_hcd *hcd = platform_get_drvdata(dev);
245 	struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
246 	struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
247 	int clk;
248 
249 	pm_runtime_get_sync(&dev->dev);
250 	usb_remove_hcd(hcd);
251 
252 	if (pdata->power_off)
253 		pdata->power_off(dev);
254 
255 	reset_control_assert(priv->resets);
256 
257 	for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
258 		clk_put(priv->clks[clk]);
259 
260 	usb_put_hcd(hcd);
261 
262 	pm_runtime_put_sync(&dev->dev);
263 	pm_runtime_disable(&dev->dev);
264 
265 	if (pdata == &ohci_platform_defaults)
266 		dev->dev.platform_data = NULL;
267 }
268 
269 #ifdef CONFIG_PM_SLEEP
270 static int ohci_platform_suspend(struct device *dev)
271 {
272 	struct usb_hcd *hcd = dev_get_drvdata(dev);
273 	struct usb_ohci_pdata *pdata = dev->platform_data;
274 	struct platform_device *pdev = to_platform_device(dev);
275 	bool do_wakeup = device_may_wakeup(dev);
276 	int ret;
277 
278 	ret = ohci_suspend(hcd, do_wakeup);
279 	if (ret)
280 		return ret;
281 
282 	if (pdata->power_suspend)
283 		pdata->power_suspend(pdev);
284 
285 	return ret;
286 }
287 
288 static int ohci_platform_resume_common(struct device *dev, bool hibernated)
289 {
290 	struct usb_hcd *hcd = dev_get_drvdata(dev);
291 	struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
292 	struct platform_device *pdev = to_platform_device(dev);
293 
294 	if (pdata->power_on) {
295 		int err = pdata->power_on(pdev);
296 		if (err < 0)
297 			return err;
298 	}
299 
300 	ohci_resume(hcd, hibernated);
301 
302 	pm_runtime_disable(dev);
303 	pm_runtime_set_active(dev);
304 	pm_runtime_enable(dev);
305 
306 	return 0;
307 }
308 
309 static int ohci_platform_resume(struct device *dev)
310 {
311 	return ohci_platform_resume_common(dev, false);
312 }
313 
314 static int ohci_platform_restore(struct device *dev)
315 {
316 	return ohci_platform_resume_common(dev, true);
317 }
318 #endif /* CONFIG_PM_SLEEP */
319 
320 static const struct of_device_id ohci_platform_ids[] = {
321 	{ .compatible = "generic-ohci", },
322 	{ .compatible = "cavium,octeon-6335-ohci", },
323 	{ .compatible = "ti,ohci-omap3", },
324 	{ }
325 };
326 MODULE_DEVICE_TABLE(of, ohci_platform_ids);
327 
328 static const struct platform_device_id ohci_platform_table[] = {
329 	{ "ohci-platform", 0 },
330 	{ }
331 };
332 MODULE_DEVICE_TABLE(platform, ohci_platform_table);
333 
334 #ifdef CONFIG_PM_SLEEP
335 static const struct dev_pm_ops ohci_platform_pm_ops = {
336 	.suspend = ohci_platform_suspend,
337 	.resume = ohci_platform_resume,
338 	.freeze = ohci_platform_suspend,
339 	.thaw = ohci_platform_resume,
340 	.poweroff = ohci_platform_suspend,
341 	.restore = ohci_platform_restore,
342 };
343 #endif
344 
345 static struct platform_driver ohci_platform_driver = {
346 	.id_table	= ohci_platform_table,
347 	.probe		= ohci_platform_probe,
348 	.remove_new	= ohci_platform_remove,
349 	.shutdown	= usb_hcd_platform_shutdown,
350 	.driver		= {
351 		.name	= "ohci-platform",
352 #ifdef CONFIG_PM_SLEEP
353 		.pm	= &ohci_platform_pm_ops,
354 #endif
355 		.of_match_table = ohci_platform_ids,
356 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
357 	}
358 };
359 
360 static int __init ohci_platform_init(void)
361 {
362 	if (usb_disabled())
363 		return -ENODEV;
364 
365 	ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
366 	return platform_driver_register(&ohci_platform_driver);
367 }
368 module_init(ohci_platform_init);
369 
370 static void __exit ohci_platform_cleanup(void)
371 {
372 	platform_driver_unregister(&ohci_platform_driver);
373 }
374 module_exit(ohci_platform_cleanup);
375 
376 MODULE_DESCRIPTION(DRIVER_DESC);
377 MODULE_AUTHOR("Hauke Mehrtens");
378 MODULE_AUTHOR("Alan Stern");
379 MODULE_LICENSE("GPL");
380