1 // SPDX-License-Identifier: GPL-2.0
3 * OHCI HCD (Host Controller Driver) for USB.
5 * TI DA8xx (OMAP-L1x) Bus Glue
7 * Derived from: ohci-omap.c and ohci-s3c2410.c
8 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
11 #include <linux/clk.h>
12 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/jiffies.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_data/usb-davinci.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/usb.h>
23 #include <linux/usb/hcd.h>
24 #include <asm/unaligned.h>
28 #define DRIVER_DESC "DA8XX"
29 #define DRV_NAME "ohci-da8xx"
31 static struct hc_driver __read_mostly ohci_da8xx_hc_driver
;
33 static int (*orig_ohci_hub_control
)(struct usb_hcd
*hcd
, u16 typeReq
,
34 u16 wValue
, u16 wIndex
, char *buf
, u16 wLength
);
35 static int (*orig_ohci_hub_status_data
)(struct usb_hcd
*hcd
, char *buf
);
37 struct da8xx_ohci_hcd
{
39 struct clk
*usb11_clk
;
40 struct phy
*usb11_phy
;
41 struct regulator
*vbus_reg
;
42 struct notifier_block nb
;
43 unsigned int reg_enabled
;
44 struct gpio_desc
*vbus_gpio
;
45 struct gpio_desc
*oc_gpio
;
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
;
95 if (da8xx_ohci
->vbus_gpio
) {
96 gpiod_set_value_cansleep(da8xx_ohci
->vbus_gpio
, on
);
100 if (!da8xx_ohci
->vbus_reg
)
103 if (on
&& !da8xx_ohci
->reg_enabled
) {
104 ret
= regulator_enable(da8xx_ohci
->vbus_reg
);
106 dev_err(dev
, "Failed to enable regulator: %d\n", ret
);
109 da8xx_ohci
->reg_enabled
= 1;
111 } else if (!on
&& da8xx_ohci
->reg_enabled
) {
112 ret
= regulator_disable(da8xx_ohci
->vbus_reg
);
114 dev_err(dev
, "Failed to disable regulator: %d\n", ret
);
117 da8xx_ohci
->reg_enabled
= 0;
123 static int ohci_da8xx_get_power(struct usb_hcd
*hcd
)
125 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
127 if (da8xx_ohci
->vbus_gpio
)
128 return gpiod_get_value_cansleep(da8xx_ohci
->vbus_gpio
);
130 if (da8xx_ohci
->vbus_reg
)
131 return regulator_is_enabled(da8xx_ohci
->vbus_reg
);
136 static int ohci_da8xx_get_oci(struct usb_hcd
*hcd
)
138 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
142 if (da8xx_ohci
->oc_gpio
)
143 return gpiod_get_value_cansleep(da8xx_ohci
->oc_gpio
);
145 if (!da8xx_ohci
->vbus_reg
)
148 ret
= regulator_get_error_flags(da8xx_ohci
->vbus_reg
, &flags
);
152 if (flags
& REGULATOR_ERROR_OVER_CURRENT
)
158 static int ohci_da8xx_has_set_power(struct usb_hcd
*hcd
)
160 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
162 if (da8xx_ohci
->vbus_gpio
)
165 if (da8xx_ohci
->vbus_reg
)
171 static int ohci_da8xx_has_oci(struct usb_hcd
*hcd
)
173 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
175 if (da8xx_ohci
->oc_gpio
)
178 if (da8xx_ohci
->vbus_reg
)
184 static int ohci_da8xx_has_potpgt(struct usb_hcd
*hcd
)
186 struct device
*dev
= hcd
->self
.controller
;
187 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
189 if (hub
&& hub
->potpgt
)
195 static int ohci_da8xx_regulator_event(struct notifier_block
*nb
,
196 unsigned long event
, void *data
)
198 struct da8xx_ohci_hcd
*da8xx_ohci
=
199 container_of(nb
, struct da8xx_ohci_hcd
, nb
);
201 if (event
& REGULATOR_EVENT_OVER_CURRENT
) {
203 ohci_da8xx_set_power(da8xx_ohci
->hcd
, 0);
209 static irqreturn_t
ohci_da8xx_oc_handler(int irq
, void *data
)
211 struct da8xx_ohci_hcd
*da8xx_ohci
= data
;
213 if (gpiod_get_value(da8xx_ohci
->oc_gpio
))
214 gpiod_set_value(da8xx_ohci
->vbus_gpio
, 0);
219 static int ohci_da8xx_register_notify(struct usb_hcd
*hcd
)
221 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
222 struct device
*dev
= hcd
->self
.controller
;
225 if (!da8xx_ohci
->oc_gpio
&& da8xx_ohci
->vbus_reg
) {
226 da8xx_ohci
->nb
.notifier_call
= ohci_da8xx_regulator_event
;
227 ret
= devm_regulator_register_notifier(da8xx_ohci
->vbus_reg
,
232 dev_err(dev
, "Failed to register notifier: %d\n", ret
);
237 static int ohci_da8xx_reset(struct usb_hcd
*hcd
)
239 struct device
*dev
= hcd
->self
.controller
;
240 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
241 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
245 dev_dbg(dev
, "starting USB controller\n");
247 result
= ohci_da8xx_enable(hcd
);
252 * DA8xx only have 1 port connected to the pins but the HC root hub
253 * register A reports 2 ports, thus we'll have to override it...
257 result
= ohci_setup(hcd
);
259 ohci_da8xx_disable(hcd
);
264 * Since we're providing a board-specific root hub port power control
265 * and over-current reporting, we have to override the HC root hub A
266 * register's default value, so that ohci_hub_control() could return
267 * the correct hub descriptor...
269 rh_a
= ohci_readl(ohci
, &ohci
->regs
->roothub
.a
);
270 if (ohci_da8xx_has_set_power(hcd
)) {
274 if (ohci_da8xx_has_oci(hcd
)) {
278 if (ohci_da8xx_has_potpgt(hcd
)) {
279 rh_a
&= ~RH_A_POTPGT
;
280 rh_a
|= hub
->potpgt
<< 24;
282 ohci_writel(ohci
, rh_a
, &ohci
->regs
->roothub
.a
);
288 * Update the status data from the hub with the over-current indicator change.
290 static int ohci_da8xx_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
292 int length
= orig_ohci_hub_status_data(hcd
, buf
);
294 /* See if we have OCIC bit set on port 1 */
295 if (ocic_mask
& (1 << 1)) {
296 dev_dbg(hcd
->self
.controller
, "over-current indicator change "
308 * Look at the control requests to the root hub and see if we need to override.
310 static int ohci_da8xx_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
311 u16 wIndex
, char *buf
, u16 wLength
)
313 struct device
*dev
= hcd
->self
.controller
;
318 /* Check the port number */
322 dev_dbg(dev
, "GetPortStatus(%u)\n", wIndex
);
324 temp
= roothub_portstatus(hcd_to_ohci(hcd
), wIndex
- 1);
326 /* The port power status (PPS) bit defaults to 1 */
327 if (!ohci_da8xx_get_power(hcd
))
330 /* The port over-current indicator (POCI) bit is always 0 */
331 if (ohci_da8xx_get_oci(hcd
) > 0)
334 /* The over-current indicator change (OCIC) bit is 0 too */
335 if (ocic_mask
& (1 << wIndex
))
338 put_unaligned(cpu_to_le32(temp
), (__le32
*)buf
);
343 case ClearPortFeature
:
347 /* Check the port number */
352 case USB_PORT_FEAT_POWER
:
353 dev_dbg(dev
, "%sPortFeature(%u): %s\n",
354 temp
? "Set" : "Clear", wIndex
, "POWER");
356 return ohci_da8xx_set_power(hcd
, temp
) ? -EPIPE
: 0;
357 case USB_PORT_FEAT_C_OVER_CURRENT
:
358 dev_dbg(dev
, "%sPortFeature(%u): %s\n",
359 temp
? "Set" : "Clear", wIndex
,
363 ocic_mask
|= 1 << wIndex
;
365 ocic_mask
&= ~(1 << wIndex
);
370 return orig_ohci_hub_control(hcd
, typeReq
, wValue
,
371 wIndex
, buf
, wLength
);
374 /*-------------------------------------------------------------------------*/
376 static const struct of_device_id da8xx_ohci_ids
[] = {
377 { .compatible
= "ti,da830-ohci" },
380 MODULE_DEVICE_TABLE(of
, da8xx_ohci_ids
);
383 static int ohci_da8xx_probe(struct platform_device
*pdev
)
385 struct da8xx_ohci_hcd
*da8xx_ohci
;
386 struct device
*dev
= &pdev
->dev
;
387 int error
, hcd_irq
, oc_irq
;
389 struct resource
*mem
;
391 hcd
= usb_create_hcd(&ohci_da8xx_hc_driver
, dev
, dev_name(dev
));
395 da8xx_ohci
= to_da8xx_ohci(hcd
);
396 da8xx_ohci
->hcd
= hcd
;
398 da8xx_ohci
->usb11_clk
= devm_clk_get(dev
, NULL
);
399 if (IS_ERR(da8xx_ohci
->usb11_clk
)) {
400 error
= PTR_ERR(da8xx_ohci
->usb11_clk
);
401 if (error
!= -EPROBE_DEFER
)
402 dev_err(dev
, "Failed to get clock.\n");
406 da8xx_ohci
->usb11_phy
= devm_phy_get(dev
, "usb-phy");
407 if (IS_ERR(da8xx_ohci
->usb11_phy
)) {
408 error
= PTR_ERR(da8xx_ohci
->usb11_phy
);
409 if (error
!= -EPROBE_DEFER
)
410 dev_err(dev
, "Failed to get phy.\n");
414 da8xx_ohci
->vbus_reg
= devm_regulator_get_optional(dev
, "vbus");
415 if (IS_ERR(da8xx_ohci
->vbus_reg
)) {
416 error
= PTR_ERR(da8xx_ohci
->vbus_reg
);
417 if (error
== -ENODEV
) {
418 da8xx_ohci
->vbus_reg
= NULL
;
419 } else if (error
== -EPROBE_DEFER
) {
422 dev_err(dev
, "Failed to get regulator\n");
427 da8xx_ohci
->vbus_gpio
= devm_gpiod_get_optional(dev
, "vbus",
429 if (IS_ERR(da8xx_ohci
->vbus_gpio
))
432 da8xx_ohci
->oc_gpio
= devm_gpiod_get_optional(dev
, "oc", GPIOD_IN
);
433 if (IS_ERR(da8xx_ohci
->oc_gpio
))
436 if (da8xx_ohci
->oc_gpio
) {
437 oc_irq
= gpiod_to_irq(da8xx_ohci
->oc_gpio
);
441 error
= devm_request_irq(dev
, oc_irq
, ohci_da8xx_oc_handler
,
442 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
443 "OHCI over-current indicator", da8xx_ohci
);
448 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
449 hcd
->regs
= devm_ioremap_resource(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 hcd_irq
= platform_get_irq(pdev
, 0);
463 error
= usb_add_hcd(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
);
493 static int ohci_da8xx_suspend(struct platform_device
*pdev
,
494 pm_message_t message
)
496 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
497 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
498 bool do_wakeup
= device_may_wakeup(&pdev
->dev
);
502 if (time_before(jiffies
, ohci
->next_statechange
))
504 ohci
->next_statechange
= jiffies
;
506 ret
= ohci_suspend(hcd
, do_wakeup
);
510 ohci_da8xx_disable(hcd
);
511 hcd
->state
= HC_STATE_SUSPENDED
;
516 static int ohci_da8xx_resume(struct platform_device
*dev
)
518 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
519 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
522 if (time_before(jiffies
, ohci
->next_statechange
))
524 ohci
->next_statechange
= jiffies
;
526 ret
= ohci_da8xx_enable(hcd
);
530 ohci_resume(hcd
, false);
536 static const struct ohci_driver_overrides da8xx_overrides __initconst
= {
537 .reset
= ohci_da8xx_reset
,
538 .extra_priv_size
= sizeof(struct da8xx_ohci_hcd
),
542 * Driver definition to register with platform structure.
544 static struct platform_driver ohci_hcd_da8xx_driver
= {
545 .probe
= ohci_da8xx_probe
,
546 .remove
= ohci_da8xx_remove
,
547 .shutdown
= usb_hcd_platform_shutdown
,
549 .suspend
= ohci_da8xx_suspend
,
550 .resume
= ohci_da8xx_resume
,
554 .of_match_table
= of_match_ptr(da8xx_ohci_ids
),
558 static int __init
ohci_da8xx_init(void)
564 pr_info("%s: " DRIVER_DESC
"\n", DRV_NAME
);
565 ohci_init_driver(&ohci_da8xx_hc_driver
, &da8xx_overrides
);
568 * The Davinci da8xx HW has some unusual quirks, which require
569 * da8xx-specific workarounds. We override certain hc_driver
570 * functions here to achieve that. We explicitly do not enhance
571 * ohci_driver_overrides to allow this more easily, since this
572 * is an unusual case, and we don't want to encourage others to
573 * override these functions by making it too easy.
576 orig_ohci_hub_control
= ohci_da8xx_hc_driver
.hub_control
;
577 orig_ohci_hub_status_data
= ohci_da8xx_hc_driver
.hub_status_data
;
579 ohci_da8xx_hc_driver
.hub_status_data
= ohci_da8xx_hub_status_data
;
580 ohci_da8xx_hc_driver
.hub_control
= ohci_da8xx_hub_control
;
582 return platform_driver_register(&ohci_hcd_da8xx_driver
);
584 module_init(ohci_da8xx_init
);
586 static void __exit
ohci_da8xx_exit(void)
588 platform_driver_unregister(&ohci_hcd_da8xx_driver
);
590 module_exit(ohci_da8xx_exit
);
591 MODULE_DESCRIPTION(DRIVER_DESC
);
592 MODULE_LICENSE("GPL");
593 MODULE_ALIAS("platform:" DRV_NAME
);