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