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