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