Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / usb / host / ohci-s3c2410.c
blobb006b93126f7847e925908c3d6d7321f15bfec51
1 /*
2 * OHCI HCD (Host Controller Driver) for USB.
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
8 * USB Bus Glue for Samsung S3C2410
10 * Written by Christopher Hoover <ch@hpl.hp.com>
11 * Based on fragments of previous driver by Russell King et al.
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
17 * Thanks to basprog@mail.ru for updates to newer kernels
19 * This file is licenced under the GPL.
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>
31 #include "ohci.h"
34 #define valid_port(idx) ((idx) == 1 || (idx) == 2)
36 /* clock device associated with the hcd */
39 #define DRIVER_DESC "OHCI S3C2410 driver"
41 static const char hcd_name[] = "ohci-s3c2410";
43 static struct clk *clk;
44 static struct clk *usb_clk;
46 static struct hc_driver __read_mostly ohci_s3c2410_hc_driver;
48 /* forward definitions */
50 static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
52 /* conversion functions */
54 static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
56 return dev_get_platdata(hcd->self.controller);
59 static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
61 struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
63 dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
65 clk_prepare_enable(usb_clk);
66 mdelay(2); /* let the bus clock stabilise */
68 clk_prepare_enable(clk);
70 if (info != NULL) {
71 info->hcd = hcd;
72 info->report_oc = s3c2410_hcd_oc;
74 if (info->enable_oc != NULL)
75 (info->enable_oc)(info, 1);
79 static void s3c2410_stop_hc(struct platform_device *dev)
81 struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
83 dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
85 if (info != NULL) {
86 info->report_oc = NULL;
87 info->hcd = NULL;
89 if (info->enable_oc != NULL)
90 (info->enable_oc)(info, 0);
93 clk_disable_unprepare(clk);
94 clk_disable_unprepare(usb_clk);
97 /* ohci_s3c2410_hub_status_data
99 * update the status data from the hub with anything that
100 * has been detected by our system
103 static int
104 ohci_s3c2410_hub_status_data(struct usb_hcd *hcd, char *buf)
106 struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
107 struct s3c2410_hcd_port *port;
108 int orig;
109 int portno;
111 orig = ohci_hub_status_data(hcd, buf);
113 if (info == NULL)
114 return orig;
116 port = &info->port[0];
118 /* mark any changed port as changed */
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);
126 if (orig < 1)
127 orig = 1;
129 buf[0] |= 1<<(portno+1);
133 return orig;
136 /* s3c2410_usb_set_power
138 * configure the power on a port, by calling the platform device
139 * routine registered with the platform device
142 static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
143 int port, int to)
145 if (info == NULL)
146 return;
148 if (info->power_control != NULL) {
149 info->port[port-1].power = to;
150 (info->power_control)(port-1, to);
154 /* ohci_s3c2410_hub_control
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.
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)
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;
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);
178 /* if we are only an humble host without any special capabilities
179 * process the request straight away and exit */
181 if (info == NULL) {
182 ret = ohci_hub_control(hcd, typeReq, wValue,
183 wIndex, buf, wLength);
184 goto out;
187 /* check the request to see if it needs handling */
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;
196 break;
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");
204 if (valid_port(wIndex)) {
205 info->port[wIndex-1].oc_changed = 0;
206 info->port[wIndex-1].oc_status = 0;
209 goto out;
211 case USB_PORT_FEAT_OVER_CURRENT:
212 dev_dbg(hcd->self.controller,
213 "ClearPortFeature: OVER_CURRENT\n");
215 if (valid_port(wIndex))
216 info->port[wIndex-1].oc_status = 0;
218 goto out;
220 case USB_PORT_FEAT_POWER:
221 dev_dbg(hcd->self.controller,
222 "ClearPortFeature: POWER\n");
224 if (valid_port(wIndex)) {
225 s3c2410_usb_set_power(info, wIndex, 0);
226 return 0;
229 break;
232 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
233 if (ret)
234 goto out;
236 switch (typeReq) {
237 case GetHubDescriptor:
239 /* update the hub's descriptor */
241 desc = (struct usb_hub_descriptor *)buf;
243 if (info->power_control == NULL)
244 return ret;
246 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
247 desc->wHubCharacteristics);
249 /* remove the old configurations for power-switching, and
250 * over-current protection, and insert our new configuration
253 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
254 desc->wHubCharacteristics |= cpu_to_le16(
255 HUB_CHAR_INDV_PORT_LPSM);
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);
264 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
265 desc->wHubCharacteristics);
267 return ret;
269 case GetPortStatus:
270 /* check port status */
272 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
274 if (valid_port(wIndex)) {
275 if (info->port[wIndex-1].oc_changed)
276 *data |= cpu_to_le32(RH_PS_OCIC);
278 if (info->port[wIndex-1].oc_status)
279 *data |= cpu_to_le32(RH_PS_POCI);
283 out:
284 return ret;
287 /* s3c2410_hcd_oc
289 * handle an over-current report
292 static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
294 struct s3c2410_hcd_port *port;
295 struct usb_hcd *hcd;
296 unsigned long flags;
297 int portno;
299 if (info == NULL)
300 return;
302 port = &info->port[0];
303 hcd = info->hcd;
305 local_irq_save(flags);
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;
313 /* ok, once over-current is detected,
314 the port needs to be powered down */
315 s3c2410_usb_set_power(info, portno+1, 0);
319 local_irq_restore(flags);
322 /* may be called without controller electrically present */
323 /* may be called with controller, bus, and devices active */
326 * ohci_hcd_s3c2410_remove - shutdown processing for HCD
327 * @dev: USB Host Controller being removed
328 * Context: !in_interrupt()
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.
336 static int
337 ohci_hcd_s3c2410_remove(struct platform_device *dev)
339 struct usb_hcd *hcd = platform_get_drvdata(dev);
341 usb_remove_hcd(hcd);
342 s3c2410_stop_hc(dev);
343 usb_put_hcd(hcd);
344 return 0;
348 * ohci_hcd_s3c2410_probe - initialize S3C2410-based HCDs
349 * Context: !in_interrupt()
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.
356 static int ohci_hcd_s3c2410_probe(struct platform_device *dev)
358 struct usb_hcd *hcd = NULL;
359 struct s3c2410_hcd_info *info = dev_get_platdata(&dev->dev);
360 int retval;
362 s3c2410_usb_set_power(info, 1, 1);
363 s3c2410_usb_set_power(info, 2, 1);
365 hcd = usb_create_hcd(&ohci_s3c2410_hc_driver, &dev->dev, "s3c24xx");
366 if (hcd == NULL)
367 return -ENOMEM;
369 hcd->rsrc_start = dev->resource[0].start;
370 hcd->rsrc_len = resource_size(&dev->resource[0]);
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;
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;
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;
392 s3c2410_start_hc(dev, hcd);
394 retval = usb_add_hcd(hcd, dev->resource[1].start, 0);
395 if (retval != 0)
396 goto err_ioremap;
398 device_wakeup_enable(hcd->self.controller);
399 return 0;
401 err_ioremap:
402 s3c2410_stop_hc(dev);
404 err_put:
405 usb_put_hcd(hcd);
406 return retval;
409 /*-------------------------------------------------------------------------*/
411 #ifdef CONFIG_PM
412 static int ohci_hcd_s3c2410_drv_suspend(struct device *dev)
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;
419 rc = ohci_suspend(hcd, do_wakeup);
420 if (rc)
421 return rc;
423 s3c2410_stop_hc(pdev);
425 return rc;
428 static int ohci_hcd_s3c2410_drv_resume(struct device *dev)
430 struct usb_hcd *hcd = dev_get_drvdata(dev);
431 struct platform_device *pdev = to_platform_device(dev);
433 s3c2410_start_hc(pdev, hcd);
435 ohci_resume(hcd, false);
437 return 0;
439 #else
440 #define ohci_hcd_s3c2410_drv_suspend NULL
441 #define ohci_hcd_s3c2410_drv_resume NULL
442 #endif
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,
449 static const struct of_device_id ohci_hcd_s3c2410_dt_ids[] = {
450 { .compatible = "samsung,s3c2410-ohci" },
451 { /* sentinel */ }
454 MODULE_DEVICE_TABLE(of, ohci_hcd_s3c2410_dt_ids);
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,
467 static int __init ohci_s3c2410_init(void)
469 if (usb_disabled())
470 return -ENODEV;
472 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
473 ohci_init_driver(&ohci_s3c2410_hc_driver, NULL);
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.
484 ohci_s3c2410_hc_driver.hub_status_data = ohci_s3c2410_hub_status_data;
485 ohci_s3c2410_hc_driver.hub_control = ohci_s3c2410_hub_control;
487 return platform_driver_register(&ohci_hcd_s3c2410_driver);
489 module_init(ohci_s3c2410_init);
491 static void __exit ohci_s3c2410_cleanup(void)
493 platform_driver_unregister(&ohci_hcd_s3c2410_driver);
495 module_exit(ohci_s3c2410_cleanup);
497 MODULE_DESCRIPTION(DRIVER_DESC);
498 MODULE_LICENSE("GPL");
499 MODULE_ALIAS("platform:s3c2410-ohci");