2 * OHCI HCD (Host Controller Driver) for USB.
4 * TI DA8xx (OMAP-L1x) Bus Glue
6 * Derived from: ohci-omap.c and ohci-s3c2410.c
7 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
14 #include <linux/clk.h>
16 #include <linux/interrupt.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/phy/phy.h>
22 #include <linux/platform_data/usb-davinci.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
26 #include <asm/unaligned.h>
30 #define DRIVER_DESC "DA8XX"
31 #define DRV_NAME "ohci-da8xx"
33 static struct hc_driver __read_mostly ohci_da8xx_hc_driver
;
35 static int (*orig_ohci_hub_control
)(struct usb_hcd
*hcd
, u16 typeReq
,
36 u16 wValue
, u16 wIndex
, char *buf
, u16 wLength
);
37 static int (*orig_ohci_hub_status_data
)(struct usb_hcd
*hcd
, char *buf
);
39 struct da8xx_ohci_hcd
{
41 struct clk
*usb11_clk
;
42 struct phy
*usb11_phy
;
43 struct regulator
*vbus_reg
;
44 struct notifier_block nb
;
45 unsigned int reg_enabled
;
48 #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
50 /* Over-current indicator change bitmask */
51 static volatile u16 ocic_mask
;
53 static int ohci_da8xx_enable(struct usb_hcd
*hcd
)
55 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
58 ret
= clk_prepare_enable(da8xx_ohci
->usb11_clk
);
62 ret
= phy_init(da8xx_ohci
->usb11_phy
);
66 ret
= phy_power_on(da8xx_ohci
->usb11_phy
);
68 goto err_phy_power_on
;
73 phy_exit(da8xx_ohci
->usb11_phy
);
75 clk_disable_unprepare(da8xx_ohci
->usb11_clk
);
80 static void ohci_da8xx_disable(struct usb_hcd
*hcd
)
82 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
84 phy_power_off(da8xx_ohci
->usb11_phy
);
85 phy_exit(da8xx_ohci
->usb11_phy
);
86 clk_disable_unprepare(da8xx_ohci
->usb11_clk
);
89 static int ohci_da8xx_set_power(struct usb_hcd
*hcd
, int on
)
91 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
92 struct device
*dev
= hcd
->self
.controller
;
93 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
96 if (hub
&& hub
->set_power
)
97 return hub
->set_power(1, on
);
99 if (!da8xx_ohci
->vbus_reg
)
102 if (on
&& !da8xx_ohci
->reg_enabled
) {
103 ret
= regulator_enable(da8xx_ohci
->vbus_reg
);
105 dev_err(dev
, "Failed to enable regulator: %d\n", ret
);
108 da8xx_ohci
->reg_enabled
= 1;
110 } else if (!on
&& da8xx_ohci
->reg_enabled
) {
111 ret
= regulator_disable(da8xx_ohci
->vbus_reg
);
113 dev_err(dev
, "Failed to disable regulator: %d\n", ret
);
116 da8xx_ohci
->reg_enabled
= 0;
122 static int ohci_da8xx_get_power(struct usb_hcd
*hcd
)
124 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
125 struct device
*dev
= hcd
->self
.controller
;
126 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
128 if (hub
&& hub
->get_power
)
129 return hub
->get_power(1);
131 if (da8xx_ohci
->vbus_reg
)
132 return regulator_is_enabled(da8xx_ohci
->vbus_reg
);
137 static int ohci_da8xx_get_oci(struct usb_hcd
*hcd
)
139 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
140 struct device
*dev
= hcd
->self
.controller
;
141 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
145 if (hub
&& hub
->get_oci
)
146 return hub
->get_oci(1);
148 if (!da8xx_ohci
->vbus_reg
)
151 ret
= regulator_get_error_flags(da8xx_ohci
->vbus_reg
, &flags
);
155 if (flags
& REGULATOR_ERROR_OVER_CURRENT
)
161 static int ohci_da8xx_has_set_power(struct usb_hcd
*hcd
)
163 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
164 struct device
*dev
= hcd
->self
.controller
;
165 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
167 if (hub
&& hub
->set_power
)
170 if (da8xx_ohci
->vbus_reg
)
176 static int ohci_da8xx_has_oci(struct usb_hcd
*hcd
)
178 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
179 struct device
*dev
= hcd
->self
.controller
;
180 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
182 if (hub
&& hub
->get_oci
)
185 if (da8xx_ohci
->vbus_reg
)
191 static int ohci_da8xx_has_potpgt(struct usb_hcd
*hcd
)
193 struct device
*dev
= hcd
->self
.controller
;
194 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
196 if (hub
&& hub
->potpgt
)
203 * Handle the port over-current indicator change.
205 static void ohci_da8xx_ocic_handler(struct da8xx_ohci_root_hub
*hub
,
208 ocic_mask
|= 1 << port
;
210 /* Once over-current is detected, the port needs to be powered down */
211 if (hub
->get_oci(port
) > 0)
212 hub
->set_power(port
, 0);
215 static int ohci_da8xx_regulator_event(struct notifier_block
*nb
,
216 unsigned long event
, void *data
)
218 struct da8xx_ohci_hcd
*da8xx_ohci
=
219 container_of(nb
, struct da8xx_ohci_hcd
, nb
);
221 if (event
& REGULATOR_EVENT_OVER_CURRENT
) {
223 ohci_da8xx_set_power(da8xx_ohci
->hcd
, 0);
229 static int ohci_da8xx_register_notify(struct usb_hcd
*hcd
)
231 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
232 struct device
*dev
= hcd
->self
.controller
;
233 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
236 if (hub
&& hub
->ocic_notify
) {
237 ret
= hub
->ocic_notify(ohci_da8xx_ocic_handler
);
238 } else if (da8xx_ohci
->vbus_reg
) {
239 da8xx_ohci
->nb
.notifier_call
= ohci_da8xx_regulator_event
;
240 ret
= devm_regulator_register_notifier(da8xx_ohci
->vbus_reg
,
245 dev_err(dev
, "Failed to register notifier: %d\n", ret
);
250 static void ohci_da8xx_unregister_notify(struct usb_hcd
*hcd
)
252 struct device
*dev
= hcd
->self
.controller
;
253 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
255 if (hub
&& hub
->ocic_notify
)
256 hub
->ocic_notify(NULL
);
259 static int ohci_da8xx_reset(struct usb_hcd
*hcd
)
261 struct device
*dev
= hcd
->self
.controller
;
262 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
263 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
267 dev_dbg(dev
, "starting USB controller\n");
269 result
= ohci_da8xx_enable(hcd
);
274 * DA8xx only have 1 port connected to the pins but the HC root hub
275 * register A reports 2 ports, thus we'll have to override it...
279 result
= ohci_setup(hcd
);
281 ohci_da8xx_disable(hcd
);
286 * Since we're providing a board-specific root hub port power control
287 * and over-current reporting, we have to override the HC root hub A
288 * register's default value, so that ohci_hub_control() could return
289 * the correct hub descriptor...
291 rh_a
= ohci_readl(ohci
, &ohci
->regs
->roothub
.a
);
292 if (ohci_da8xx_has_set_power(hcd
)) {
296 if (ohci_da8xx_has_oci(hcd
)) {
300 if (ohci_da8xx_has_potpgt(hcd
)) {
301 rh_a
&= ~RH_A_POTPGT
;
302 rh_a
|= hub
->potpgt
<< 24;
304 ohci_writel(ohci
, rh_a
, &ohci
->regs
->roothub
.a
);
310 * Update the status data from the hub with the over-current indicator change.
312 static int ohci_da8xx_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
314 int length
= orig_ohci_hub_status_data(hcd
, buf
);
316 /* See if we have OCIC bit set on port 1 */
317 if (ocic_mask
& (1 << 1)) {
318 dev_dbg(hcd
->self
.controller
, "over-current indicator change "
330 * Look at the control requests to the root hub and see if we need to override.
332 static int ohci_da8xx_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
333 u16 wIndex
, char *buf
, u16 wLength
)
335 struct device
*dev
= hcd
->self
.controller
;
340 /* Check the port number */
344 dev_dbg(dev
, "GetPortStatus(%u)\n", wIndex
);
346 temp
= roothub_portstatus(hcd_to_ohci(hcd
), wIndex
- 1);
348 /* The port power status (PPS) bit defaults to 1 */
349 if (!ohci_da8xx_get_power(hcd
))
352 /* The port over-current indicator (POCI) bit is always 0 */
353 if (ohci_da8xx_get_oci(hcd
) > 0)
356 /* The over-current indicator change (OCIC) bit is 0 too */
357 if (ocic_mask
& (1 << wIndex
))
360 put_unaligned(cpu_to_le32(temp
), (__le32
*)buf
);
365 case ClearPortFeature
:
369 /* Check the port number */
374 case USB_PORT_FEAT_POWER
:
375 dev_dbg(dev
, "%sPortFeature(%u): %s\n",
376 temp
? "Set" : "Clear", wIndex
, "POWER");
378 return ohci_da8xx_set_power(hcd
, temp
) ? -EPIPE
: 0;
379 case USB_PORT_FEAT_C_OVER_CURRENT
:
380 dev_dbg(dev
, "%sPortFeature(%u): %s\n",
381 temp
? "Set" : "Clear", wIndex
,
385 ocic_mask
|= 1 << wIndex
;
387 ocic_mask
&= ~(1 << wIndex
);
392 return orig_ohci_hub_control(hcd
, typeReq
, wValue
,
393 wIndex
, buf
, wLength
);
396 /*-------------------------------------------------------------------------*/
398 static const struct of_device_id da8xx_ohci_ids
[] = {
399 { .compatible
= "ti,da830-ohci" },
402 MODULE_DEVICE_TABLE(of
, da8xx_ohci_ids
);
405 static int ohci_da8xx_probe(struct platform_device
*pdev
)
407 struct da8xx_ohci_hcd
*da8xx_ohci
;
409 struct resource
*mem
;
411 hcd
= usb_create_hcd(&ohci_da8xx_hc_driver
, &pdev
->dev
,
412 dev_name(&pdev
->dev
));
416 da8xx_ohci
= to_da8xx_ohci(hcd
);
417 da8xx_ohci
->hcd
= hcd
;
419 da8xx_ohci
->usb11_clk
= devm_clk_get(&pdev
->dev
, "usb11");
420 if (IS_ERR(da8xx_ohci
->usb11_clk
)) {
421 error
= PTR_ERR(da8xx_ohci
->usb11_clk
);
422 if (error
!= -EPROBE_DEFER
)
423 dev_err(&pdev
->dev
, "Failed to get clock.\n");
427 da8xx_ohci
->usb11_phy
= devm_phy_get(&pdev
->dev
, "usb-phy");
428 if (IS_ERR(da8xx_ohci
->usb11_phy
)) {
429 error
= PTR_ERR(da8xx_ohci
->usb11_phy
);
430 if (error
!= -EPROBE_DEFER
)
431 dev_err(&pdev
->dev
, "Failed to get phy.\n");
435 da8xx_ohci
->vbus_reg
= devm_regulator_get_optional(&pdev
->dev
, "vbus");
436 if (IS_ERR(da8xx_ohci
->vbus_reg
)) {
437 error
= PTR_ERR(da8xx_ohci
->vbus_reg
);
438 if (error
== -ENODEV
) {
439 da8xx_ohci
->vbus_reg
= NULL
;
440 } else if (error
== -EPROBE_DEFER
) {
443 dev_err(&pdev
->dev
, "Failed to get regulator\n");
448 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
449 hcd
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
450 if (IS_ERR(hcd
->regs
)) {
451 error
= PTR_ERR(hcd
->regs
);
454 hcd
->rsrc_start
= mem
->start
;
455 hcd
->rsrc_len
= resource_size(mem
);
457 irq
= platform_get_irq(pdev
, 0);
463 error
= usb_add_hcd(hcd
, irq
, 0);
467 device_wakeup_enable(hcd
->self
.controller
);
469 error
= ohci_da8xx_register_notify(hcd
);
482 static int ohci_da8xx_remove(struct platform_device
*pdev
)
484 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
486 ohci_da8xx_unregister_notify(hcd
);
494 static int ohci_da8xx_suspend(struct platform_device
*pdev
,
495 pm_message_t message
)
497 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
498 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
499 bool do_wakeup
= device_may_wakeup(&pdev
->dev
);
503 if (time_before(jiffies
, ohci
->next_statechange
))
505 ohci
->next_statechange
= jiffies
;
507 ret
= ohci_suspend(hcd
, do_wakeup
);
511 ohci_da8xx_disable(hcd
);
512 hcd
->state
= HC_STATE_SUSPENDED
;
517 static int ohci_da8xx_resume(struct platform_device
*dev
)
519 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
520 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
523 if (time_before(jiffies
, ohci
->next_statechange
))
525 ohci
->next_statechange
= jiffies
;
527 ret
= ohci_da8xx_enable(hcd
);
531 ohci_resume(hcd
, false);
537 static const struct ohci_driver_overrides da8xx_overrides __initconst
= {
538 .reset
= ohci_da8xx_reset
,
539 .extra_priv_size
= sizeof(struct da8xx_ohci_hcd
),
543 * Driver definition to register with platform structure.
545 static struct platform_driver ohci_hcd_da8xx_driver
= {
546 .probe
= ohci_da8xx_probe
,
547 .remove
= ohci_da8xx_remove
,
548 .shutdown
= usb_hcd_platform_shutdown
,
550 .suspend
= ohci_da8xx_suspend
,
551 .resume
= ohci_da8xx_resume
,
555 .of_match_table
= of_match_ptr(da8xx_ohci_ids
),
559 static int __init
ohci_da8xx_init(void)
565 pr_info("%s: " DRIVER_DESC
"\n", DRV_NAME
);
566 ohci_init_driver(&ohci_da8xx_hc_driver
, &da8xx_overrides
);
569 * The Davinci da8xx HW has some unusual quirks, which require
570 * da8xx-specific workarounds. We override certain hc_driver
571 * functions here to achieve that. We explicitly do not enhance
572 * ohci_driver_overrides to allow this more easily, since this
573 * is an unusual case, and we don't want to encourage others to
574 * override these functions by making it too easy.
577 orig_ohci_hub_control
= ohci_da8xx_hc_driver
.hub_control
;
578 orig_ohci_hub_status_data
= ohci_da8xx_hc_driver
.hub_status_data
;
580 ohci_da8xx_hc_driver
.hub_status_data
= ohci_da8xx_hub_status_data
;
581 ohci_da8xx_hc_driver
.hub_control
= ohci_da8xx_hub_control
;
583 return platform_driver_register(&ohci_hcd_da8xx_driver
);
585 module_init(ohci_da8xx_init
);
587 static void __exit
ohci_da8xx_exit(void)
589 platform_driver_unregister(&ohci_hcd_da8xx_driver
);
591 module_exit(ohci_da8xx_exit
);
592 MODULE_DESCRIPTION(DRIVER_DESC
);
593 MODULE_LICENSE("GPL");
594 MODULE_ALIAS("platform:" DRV_NAME
);