xref: /openbmc/linux/drivers/usb/host/ohci-s3c2410.c (revision 83146efc)
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * OHCI HCD (Host Controller Driver) for USB.
4  *
5  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
6  * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
7  * (C) Copyright 2002 Hewlett-Packard Company
8  *
9  * USB Bus Glue for Samsung S3C2410
10  *
11  * Written by Christopher Hoover <ch@hpl.hp.com>
12  * Based on fragments of previous driver by Russell King et al.
13  *
14  * Modified for S3C2410 from ohci-sa1111.c, ohci-omap.c and ohci-lh7a40.c
15  *	by Ben Dooks, <ben@simtec.co.uk>
16  *	Copyright (C) 2004 Simtec Electronics
17  *
18  * Thanks to basprog@mail.ru for updates to newer kernels
19  *
20  * This file is licenced under the GPL.
21 */
22 
23 #include <linux/clk.h>
24 #include <linux/io.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/platform_data/usb-ohci-s3c2410.h>
29 #include <linux/usb.h>
30 #include <linux/usb/hcd.h>
31 
32 #include "ohci.h"
33 
34 
35 #define valid_port(idx) ((idx) == 1 || (idx) == 2)
36 
37 /* clock device associated with the hcd */
38 
39 
40 #define DRIVER_DESC "OHCI S3C2410 driver"
41 
42 static struct clk *clk;
43 static struct clk *usb_clk;
44 
45 static struct hc_driver __read_mostly ohci_s3c2410_hc_driver;
46 
47 /* forward definitions */
48 
49 static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
50 
51 /* conversion functions */
52 
53 static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
54 {
55 	return dev_get_platdata(hcd->self.controller);
56 }
57 
58 static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
59 {
60 	struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
61 
62 	dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
63 
64 	clk_prepare_enable(usb_clk);
65 	mdelay(2);			/* let the bus clock stabilise */
66 
67 	clk_prepare_enable(clk);
68 
69 	if (info != NULL) {
70 		info->hcd	= hcd;
71 		info->report_oc = s3c2410_hcd_oc;
72 
73 		if (info->enable_oc != NULL)
74 			(info->enable_oc)(info, 1);
75 	}
76 }
77 
78 static void s3c2410_stop_hc(struct platform_device *dev)
79 {
80 	struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
81 
82 	dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
83 
84 	if (info != NULL) {
85 		info->report_oc = NULL;
86 		info->hcd	= NULL;
87 
88 		if (info->enable_oc != NULL)
89 			(info->enable_oc)(info, 0);
90 	}
91 
92 	clk_disable_unprepare(clk);
93 	clk_disable_unprepare(usb_clk);
94 }
95 
96 /* ohci_s3c2410_hub_status_data
97  *
98  * update the status data from the hub with anything that
99  * has been detected by our system
100 */
101 
102 static int
103 ohci_s3c2410_hub_status_data(struct usb_hcd *hcd, char *buf)
104 {
105 	struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
106 	struct s3c2410_hcd_port *port;
107 	int orig;
108 	int portno;
109 
110 	orig = ohci_hub_status_data(hcd, buf);
111 
112 	if (info == NULL)
113 		return orig;
114 
115 	port = &info->port[0];
116 
117 	/* mark any changed port as changed */
118 
119 	for (portno = 0; portno < 2; port++, portno++) {
120 		if (port->oc_changed == 1 &&
121 		    port->flags & S3C_HCDFLG_USED) {
122 			dev_dbg(hcd->self.controller,
123 				"oc change on port %d\n", portno);
124 
125 			if (orig < 1)
126 				orig = 1;
127 
128 			buf[0] |= 1<<(portno+1);
129 		}
130 	}
131 
132 	return orig;
133 }
134 
135 /* s3c2410_usb_set_power
136  *
137  * configure the power on a port, by calling the platform device
138  * routine registered with the platform device
139 */
140 
141 static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
142 				  int port, int to)
143 {
144 	if (info == NULL)
145 		return;
146 
147 	if (info->power_control != NULL) {
148 		info->port[port-1].power = to;
149 		(info->power_control)(port-1, to);
150 	}
151 }
152 
153 /* ohci_s3c2410_hub_control
154  *
155  * look at control requests to the hub, and see if we need
156  * to take any action or over-ride the results from the
157  * request.
158 */
159 
160 static int ohci_s3c2410_hub_control(
161 	struct usb_hcd	*hcd,
162 	u16		typeReq,
163 	u16		wValue,
164 	u16		wIndex,
165 	char		*buf,
166 	u16		wLength)
167 {
168 	struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
169 	struct usb_hub_descriptor *desc;
170 	int ret = -EINVAL;
171 	u32 *data = (u32 *)buf;
172 
173 	dev_dbg(hcd->self.controller,
174 		"s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
175 		hcd, typeReq, wValue, wIndex, buf, wLength);
176 
177 	/* if we are only an humble host without any special capabilities
178 	 * process the request straight away and exit */
179 
180 	if (info == NULL) {
181 		ret = ohci_hub_control(hcd, typeReq, wValue,
182 				       wIndex, buf, wLength);
183 		goto out;
184 	}
185 
186 	/* check the request to see if it needs handling */
187 
188 	switch (typeReq) {
189 	case SetPortFeature:
190 		if (wValue == USB_PORT_FEAT_POWER) {
191 			dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
192 			s3c2410_usb_set_power(info, wIndex, 1);
193 			goto out;
194 		}
195 		break;
196 
197 	case ClearPortFeature:
198 		switch (wValue) {
199 		case USB_PORT_FEAT_C_OVER_CURRENT:
200 			dev_dbg(hcd->self.controller,
201 				"ClearPortFeature: C_OVER_CURRENT\n");
202 
203 			if (valid_port(wIndex)) {
204 				info->port[wIndex-1].oc_changed = 0;
205 				info->port[wIndex-1].oc_status = 0;
206 			}
207 
208 			goto out;
209 
210 		case USB_PORT_FEAT_OVER_CURRENT:
211 			dev_dbg(hcd->self.controller,
212 				"ClearPortFeature: OVER_CURRENT\n");
213 
214 			if (valid_port(wIndex))
215 				info->port[wIndex-1].oc_status = 0;
216 
217 			goto out;
218 
219 		case USB_PORT_FEAT_POWER:
220 			dev_dbg(hcd->self.controller,
221 				"ClearPortFeature: POWER\n");
222 
223 			if (valid_port(wIndex)) {
224 				s3c2410_usb_set_power(info, wIndex, 0);
225 				return 0;
226 			}
227 		}
228 		break;
229 	}
230 
231 	ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
232 	if (ret)
233 		goto out;
234 
235 	switch (typeReq) {
236 	case GetHubDescriptor:
237 
238 		/* update the hub's descriptor */
239 
240 		desc = (struct usb_hub_descriptor *)buf;
241 
242 		if (info->power_control == NULL)
243 			return ret;
244 
245 		dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
246 			desc->wHubCharacteristics);
247 
248 		/* remove the old configurations for power-switching, and
249 		 * over-current protection, and insert our new configuration
250 		 */
251 
252 		desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
253 		desc->wHubCharacteristics |= cpu_to_le16(
254 			HUB_CHAR_INDV_PORT_LPSM);
255 
256 		if (info->enable_oc) {
257 			desc->wHubCharacteristics &= ~cpu_to_le16(
258 				HUB_CHAR_OCPM);
259 			desc->wHubCharacteristics |=  cpu_to_le16(
260 				HUB_CHAR_INDV_PORT_OCPM);
261 		}
262 
263 		dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
264 			desc->wHubCharacteristics);
265 
266 		return ret;
267 
268 	case GetPortStatus:
269 		/* check port status */
270 
271 		dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
272 
273 		if (valid_port(wIndex)) {
274 			if (info->port[wIndex-1].oc_changed)
275 				*data |= cpu_to_le32(RH_PS_OCIC);
276 
277 			if (info->port[wIndex-1].oc_status)
278 				*data |= cpu_to_le32(RH_PS_POCI);
279 		}
280 	}
281 
282  out:
283 	return ret;
284 }
285 
286 /* s3c2410_hcd_oc
287  *
288  * handle an over-current report
289 */
290 
291 static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
292 {
293 	struct s3c2410_hcd_port *port;
294 	unsigned long flags;
295 	int portno;
296 
297 	if (info == NULL)
298 		return;
299 
300 	port = &info->port[0];
301 
302 	local_irq_save(flags);
303 
304 	for (portno = 0; portno < 2; port++, portno++) {
305 		if (port_oc & (1<<portno) &&
306 		    port->flags & S3C_HCDFLG_USED) {
307 			port->oc_status = 1;
308 			port->oc_changed = 1;
309 
310 			/* ok, once over-current is detected,
311 			   the port needs to be powered down */
312 			s3c2410_usb_set_power(info, portno+1, 0);
313 		}
314 	}
315 
316 	local_irq_restore(flags);
317 }
318 
319 /* may be called without controller electrically present */
320 /* may be called with controller, bus, and devices active */
321 
322 /*
323  * ohci_hcd_s3c2410_remove - shutdown processing for HCD
324  * @dev: USB Host Controller being removed
325  *
326  * Context: task context, might sleep
327  *
328  * Reverses the effect of ohci_hcd_3c2410_probe(), first invoking
329  * the HCD's stop() method.  It is always called from a thread
330  * context, normally "rmmod", "apmd", or something similar.
331  */
332 static void
333 ohci_hcd_s3c2410_remove(struct platform_device *dev)
334 {
335 	struct usb_hcd *hcd = platform_get_drvdata(dev);
336 
337 	usb_remove_hcd(hcd);
338 	s3c2410_stop_hc(dev);
339 	usb_put_hcd(hcd);
340 }
341 
342 /*
343  * ohci_hcd_s3c2410_probe - initialize S3C2410-based HCDs
344  * @dev: USB Host Controller to be probed
345  *
346  * Context: task context, might sleep
347  *
348  * Allocates basic resources for this USB host controller, and
349  * then invokes the start() method for the HCD associated with it
350  * through the hotplug entry's driver_data.
351  */
352 static int ohci_hcd_s3c2410_probe(struct platform_device *dev)
353 {
354 	struct usb_hcd *hcd = NULL;
355 	struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
356 	int retval, irq;
357 
358 	s3c2410_usb_set_power(info, 1, 1);
359 	s3c2410_usb_set_power(info, 2, 1);
360 
361 	hcd = usb_create_hcd(&ohci_s3c2410_hc_driver, &dev->dev, "s3c24xx");
362 	if (hcd == NULL)
363 		return -ENOMEM;
364 
365 	hcd->rsrc_start = dev->resource[0].start;
366 	hcd->rsrc_len	= resource_size(&dev->resource[0]);
367 
368 	hcd->regs = devm_ioremap_resource(&dev->dev, &dev->resource[0]);
369 	if (IS_ERR(hcd->regs)) {
370 		retval = PTR_ERR(hcd->regs);
371 		goto err_put;
372 	}
373 
374 	clk = devm_clk_get(&dev->dev, "usb-host");
375 	if (IS_ERR(clk)) {
376 		dev_err(&dev->dev, "cannot get usb-host clock\n");
377 		retval = PTR_ERR(clk);
378 		goto err_put;
379 	}
380 
381 	usb_clk = devm_clk_get(&dev->dev, "usb-bus-host");
382 	if (IS_ERR(usb_clk)) {
383 		dev_err(&dev->dev, "cannot get usb-bus-host clock\n");
384 		retval = PTR_ERR(usb_clk);
385 		goto err_put;
386 	}
387 
388 	irq = platform_get_irq(dev, 0);
389 	if (irq < 0) {
390 		retval = irq;
391 		goto err_put;
392 	}
393 
394 	s3c2410_start_hc(dev, hcd);
395 
396 	retval = usb_add_hcd(hcd, irq, 0);
397 	if (retval != 0)
398 		goto err_ioremap;
399 
400 	device_wakeup_enable(hcd->self.controller);
401 	return 0;
402 
403  err_ioremap:
404 	s3c2410_stop_hc(dev);
405 
406  err_put:
407 	usb_put_hcd(hcd);
408 	return retval;
409 }
410 
411 /*-------------------------------------------------------------------------*/
412 
413 #ifdef CONFIG_PM
414 static int ohci_hcd_s3c2410_drv_suspend(struct device *dev)
415 {
416 	struct usb_hcd *hcd = dev_get_drvdata(dev);
417 	struct platform_device *pdev = to_platform_device(dev);
418 	bool do_wakeup = device_may_wakeup(dev);
419 	int rc = 0;
420 
421 	rc = ohci_suspend(hcd, do_wakeup);
422 	if (rc)
423 		return rc;
424 
425 	s3c2410_stop_hc(pdev);
426 
427 	return rc;
428 }
429 
430 static int ohci_hcd_s3c2410_drv_resume(struct device *dev)
431 {
432 	struct usb_hcd *hcd = dev_get_drvdata(dev);
433 	struct platform_device *pdev = to_platform_device(dev);
434 
435 	s3c2410_start_hc(pdev, hcd);
436 
437 	ohci_resume(hcd, false);
438 
439 	return 0;
440 }
441 #else
442 #define ohci_hcd_s3c2410_drv_suspend	NULL
443 #define ohci_hcd_s3c2410_drv_resume	NULL
444 #endif
445 
446 static const struct dev_pm_ops ohci_hcd_s3c2410_pm_ops = {
447 	.suspend	= ohci_hcd_s3c2410_drv_suspend,
448 	.resume		= ohci_hcd_s3c2410_drv_resume,
449 };
450 
451 static const struct of_device_id ohci_hcd_s3c2410_dt_ids[] = {
452 	{ .compatible = "samsung,s3c2410-ohci" },
453 	{ /* sentinel */ }
454 };
455 
456 MODULE_DEVICE_TABLE(of, ohci_hcd_s3c2410_dt_ids);
457 
458 static struct platform_driver ohci_hcd_s3c2410_driver = {
459 	.probe		= ohci_hcd_s3c2410_probe,
460 	.remove_new	= ohci_hcd_s3c2410_remove,
461 	.shutdown	= usb_hcd_platform_shutdown,
462 	.driver		= {
463 		.name	= "s3c2410-ohci",
464 		.pm	= &ohci_hcd_s3c2410_pm_ops,
465 		.of_match_table	= ohci_hcd_s3c2410_dt_ids,
466 	},
467 };
468 
469 static int __init ohci_s3c2410_init(void)
470 {
471 	if (usb_disabled())
472 		return -ENODEV;
473 
474 	ohci_init_driver(&ohci_s3c2410_hc_driver, NULL);
475 
476 	/*
477 	 * The Samsung HW has some unusual quirks, which require
478 	 * Sumsung-specific workarounds. We override certain hc_driver
479 	 * functions here to achieve that. We explicitly do not enhance
480 	 * ohci_driver_overrides to allow this more easily, since this
481 	 * is an unusual case, and we don't want to encourage others to
482 	 * override these functions by making it too easy.
483 	 */
484 
485 	ohci_s3c2410_hc_driver.hub_status_data	= ohci_s3c2410_hub_status_data;
486 	ohci_s3c2410_hc_driver.hub_control	= ohci_s3c2410_hub_control;
487 
488 	return platform_driver_register(&ohci_hcd_s3c2410_driver);
489 }
490 module_init(ohci_s3c2410_init);
491 
492 static void __exit ohci_s3c2410_cleanup(void)
493 {
494 	platform_driver_unregister(&ohci_hcd_s3c2410_driver);
495 }
496 module_exit(ohci_s3c2410_cleanup);
497 
498 MODULE_DESCRIPTION(DRIVER_DESC);
499 MODULE_LICENSE("GPL");
500 MODULE_ALIAS("platform:s3c2410-ohci");
501