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