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 struct gpio_desc
*oc_gpio
;
46 #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
48 /* Over-current indicator change bitmask */
49 static volatile u16 ocic_mask
;
51 static int ohci_da8xx_enable(struct usb_hcd
*hcd
)
53 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
56 ret
= clk_prepare_enable(da8xx_ohci
->usb11_clk
);
60 ret
= phy_init(da8xx_ohci
->usb11_phy
);
64 ret
= phy_power_on(da8xx_ohci
->usb11_phy
);
66 goto err_phy_power_on
;
71 phy_exit(da8xx_ohci
->usb11_phy
);
73 clk_disable_unprepare(da8xx_ohci
->usb11_clk
);
78 static void ohci_da8xx_disable(struct usb_hcd
*hcd
)
80 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
82 phy_power_off(da8xx_ohci
->usb11_phy
);
83 phy_exit(da8xx_ohci
->usb11_phy
);
84 clk_disable_unprepare(da8xx_ohci
->usb11_clk
);
87 static int ohci_da8xx_set_power(struct usb_hcd
*hcd
, int on
)
89 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
90 struct device
*dev
= hcd
->self
.controller
;
93 if (!da8xx_ohci
->vbus_reg
)
97 ret
= regulator_enable(da8xx_ohci
->vbus_reg
);
99 dev_err(dev
, "Failed to enable regulator: %d\n", ret
);
103 ret
= regulator_disable(da8xx_ohci
->vbus_reg
);
105 dev_err(dev
, "Failed to disable regulator: %d\n", ret
);
113 static int ohci_da8xx_get_power(struct usb_hcd
*hcd
)
115 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
117 if (da8xx_ohci
->vbus_reg
)
118 return regulator_is_enabled(da8xx_ohci
->vbus_reg
);
123 static int ohci_da8xx_get_oci(struct usb_hcd
*hcd
)
125 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
129 if (da8xx_ohci
->oc_gpio
)
130 return gpiod_get_value_cansleep(da8xx_ohci
->oc_gpio
);
132 if (!da8xx_ohci
->vbus_reg
)
135 ret
= regulator_get_error_flags(da8xx_ohci
->vbus_reg
, &flags
);
139 if (flags
& REGULATOR_ERROR_OVER_CURRENT
)
145 static int ohci_da8xx_has_set_power(struct usb_hcd
*hcd
)
147 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
149 if (da8xx_ohci
->vbus_reg
)
155 static int ohci_da8xx_has_oci(struct usb_hcd
*hcd
)
157 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
159 if (da8xx_ohci
->oc_gpio
)
162 if (da8xx_ohci
->vbus_reg
)
168 static int ohci_da8xx_has_potpgt(struct usb_hcd
*hcd
)
170 struct device
*dev
= hcd
->self
.controller
;
171 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
173 if (hub
&& hub
->potpgt
)
179 static int ohci_da8xx_regulator_event(struct notifier_block
*nb
,
180 unsigned long event
, void *data
)
182 struct da8xx_ohci_hcd
*da8xx_ohci
=
183 container_of(nb
, struct da8xx_ohci_hcd
, nb
);
185 if (event
& REGULATOR_EVENT_OVER_CURRENT
) {
187 ohci_da8xx_set_power(da8xx_ohci
->hcd
, 0);
193 static irqreturn_t
ohci_da8xx_oc_thread(int irq
, void *data
)
195 struct da8xx_ohci_hcd
*da8xx_ohci
= data
;
196 struct device
*dev
= da8xx_ohci
->hcd
->self
.controller
;
199 if (gpiod_get_value_cansleep(da8xx_ohci
->oc_gpio
) &&
200 da8xx_ohci
->vbus_reg
) {
201 ret
= regulator_disable(da8xx_ohci
->vbus_reg
);
203 dev_err(dev
, "Failed to disable regulator: %d\n", ret
);
209 static int ohci_da8xx_register_notify(struct usb_hcd
*hcd
)
211 struct da8xx_ohci_hcd
*da8xx_ohci
= to_da8xx_ohci(hcd
);
212 struct device
*dev
= hcd
->self
.controller
;
215 if (!da8xx_ohci
->oc_gpio
&& da8xx_ohci
->vbus_reg
) {
216 da8xx_ohci
->nb
.notifier_call
= ohci_da8xx_regulator_event
;
217 ret
= devm_regulator_register_notifier(da8xx_ohci
->vbus_reg
,
222 dev_err(dev
, "Failed to register notifier: %d\n", ret
);
227 static int ohci_da8xx_reset(struct usb_hcd
*hcd
)
229 struct device
*dev
= hcd
->self
.controller
;
230 struct da8xx_ohci_root_hub
*hub
= dev_get_platdata(dev
);
231 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
235 dev_dbg(dev
, "starting USB controller\n");
237 result
= ohci_da8xx_enable(hcd
);
242 * DA8xx only have 1 port connected to the pins but the HC root hub
243 * register A reports 2 ports, thus we'll have to override it...
247 result
= ohci_setup(hcd
);
249 ohci_da8xx_disable(hcd
);
254 * Since we're providing a board-specific root hub port power control
255 * and over-current reporting, we have to override the HC root hub A
256 * register's default value, so that ohci_hub_control() could return
257 * the correct hub descriptor...
259 rh_a
= ohci_readl(ohci
, &ohci
->regs
->roothub
.a
);
260 if (ohci_da8xx_has_set_power(hcd
)) {
264 if (ohci_da8xx_has_oci(hcd
)) {
268 if (ohci_da8xx_has_potpgt(hcd
)) {
269 rh_a
&= ~RH_A_POTPGT
;
270 rh_a
|= hub
->potpgt
<< 24;
272 ohci_writel(ohci
, rh_a
, &ohci
->regs
->roothub
.a
);
278 * Update the status data from the hub with the over-current indicator change.
280 static int ohci_da8xx_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
282 int length
= orig_ohci_hub_status_data(hcd
, buf
);
284 /* See if we have OCIC bit set on port 1 */
285 if (ocic_mask
& (1 << 1)) {
286 dev_dbg(hcd
->self
.controller
, "over-current indicator change "
298 * Look at the control requests to the root hub and see if we need to override.
300 static int ohci_da8xx_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
301 u16 wIndex
, char *buf
, u16 wLength
)
303 struct device
*dev
= hcd
->self
.controller
;
308 /* Check the port number */
312 dev_dbg(dev
, "GetPortStatus(%u)\n", wIndex
);
314 temp
= roothub_portstatus(hcd_to_ohci(hcd
), wIndex
- 1);
316 /* The port power status (PPS) bit defaults to 1 */
317 if (!ohci_da8xx_get_power(hcd
))
320 /* The port over-current indicator (POCI) bit is always 0 */
321 if (ohci_da8xx_get_oci(hcd
) > 0)
324 /* The over-current indicator change (OCIC) bit is 0 too */
325 if (ocic_mask
& (1 << wIndex
))
328 put_unaligned(cpu_to_le32(temp
), (__le32
*)buf
);
333 case ClearPortFeature
:
337 /* Check the port number */
342 case USB_PORT_FEAT_POWER
:
343 dev_dbg(dev
, "%sPortFeature(%u): %s\n",
344 temp
? "Set" : "Clear", wIndex
, "POWER");
346 return ohci_da8xx_set_power(hcd
, temp
) ? -EPIPE
: 0;
347 case USB_PORT_FEAT_C_OVER_CURRENT
:
348 dev_dbg(dev
, "%sPortFeature(%u): %s\n",
349 temp
? "Set" : "Clear", wIndex
,
353 ocic_mask
|= 1 << wIndex
;
355 ocic_mask
&= ~(1 << wIndex
);
360 return orig_ohci_hub_control(hcd
, typeReq
, wValue
,
361 wIndex
, buf
, wLength
);
364 /*-------------------------------------------------------------------------*/
366 static const struct of_device_id da8xx_ohci_ids
[] = {
367 { .compatible
= "ti,da830-ohci" },
370 MODULE_DEVICE_TABLE(of
, da8xx_ohci_ids
);
373 static int ohci_da8xx_probe(struct platform_device
*pdev
)
375 struct da8xx_ohci_hcd
*da8xx_ohci
;
376 struct device
*dev
= &pdev
->dev
;
377 int error
, hcd_irq
, oc_irq
;
379 struct resource
*mem
;
381 hcd
= usb_create_hcd(&ohci_da8xx_hc_driver
, dev
, dev_name(dev
));
385 da8xx_ohci
= to_da8xx_ohci(hcd
);
386 da8xx_ohci
->hcd
= hcd
;
388 da8xx_ohci
->usb11_clk
= devm_clk_get(dev
, NULL
);
389 if (IS_ERR(da8xx_ohci
->usb11_clk
)) {
390 error
= PTR_ERR(da8xx_ohci
->usb11_clk
);
391 if (error
!= -EPROBE_DEFER
)
392 dev_err(dev
, "Failed to get clock.\n");
396 da8xx_ohci
->usb11_phy
= devm_phy_get(dev
, "usb-phy");
397 if (IS_ERR(da8xx_ohci
->usb11_phy
)) {
398 error
= PTR_ERR(da8xx_ohci
->usb11_phy
);
399 if (error
!= -EPROBE_DEFER
)
400 dev_err(dev
, "Failed to get phy.\n");
404 da8xx_ohci
->vbus_reg
= devm_regulator_get_optional(dev
, "vbus");
405 if (IS_ERR(da8xx_ohci
->vbus_reg
)) {
406 error
= PTR_ERR(da8xx_ohci
->vbus_reg
);
407 if (error
== -ENODEV
) {
408 da8xx_ohci
->vbus_reg
= NULL
;
409 } else if (error
== -EPROBE_DEFER
) {
412 dev_err(dev
, "Failed to get regulator\n");
417 da8xx_ohci
->oc_gpio
= devm_gpiod_get_optional(dev
, "oc", GPIOD_IN
);
418 if (IS_ERR(da8xx_ohci
->oc_gpio
)) {
419 error
= PTR_ERR(da8xx_ohci
->oc_gpio
);
423 if (da8xx_ohci
->oc_gpio
) {
424 oc_irq
= gpiod_to_irq(da8xx_ohci
->oc_gpio
);
430 error
= devm_request_threaded_irq(dev
, oc_irq
, NULL
,
431 ohci_da8xx_oc_thread
, IRQF_TRIGGER_RISING
|
432 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
433 "OHCI over-current indicator", da8xx_ohci
);
438 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
439 hcd
->regs
= devm_ioremap_resource(dev
, mem
);
440 if (IS_ERR(hcd
->regs
)) {
441 error
= PTR_ERR(hcd
->regs
);
444 hcd
->rsrc_start
= mem
->start
;
445 hcd
->rsrc_len
= resource_size(mem
);
447 hcd_irq
= platform_get_irq(pdev
, 0);
453 error
= usb_add_hcd(hcd
, hcd_irq
, 0);
457 device_wakeup_enable(hcd
->self
.controller
);
459 error
= ohci_da8xx_register_notify(hcd
);
472 static int ohci_da8xx_remove(struct platform_device
*pdev
)
474 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
483 static int ohci_da8xx_suspend(struct platform_device
*pdev
,
484 pm_message_t message
)
486 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
487 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
488 bool do_wakeup
= device_may_wakeup(&pdev
->dev
);
492 if (time_before(jiffies
, ohci
->next_statechange
))
494 ohci
->next_statechange
= jiffies
;
496 ret
= ohci_suspend(hcd
, do_wakeup
);
500 ohci_da8xx_disable(hcd
);
501 hcd
->state
= HC_STATE_SUSPENDED
;
506 static int ohci_da8xx_resume(struct platform_device
*dev
)
508 struct usb_hcd
*hcd
= platform_get_drvdata(dev
);
509 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
512 if (time_before(jiffies
, ohci
->next_statechange
))
514 ohci
->next_statechange
= jiffies
;
516 ret
= ohci_da8xx_enable(hcd
);
520 ohci_resume(hcd
, false);
526 static const struct ohci_driver_overrides da8xx_overrides __initconst
= {
527 .reset
= ohci_da8xx_reset
,
528 .extra_priv_size
= sizeof(struct da8xx_ohci_hcd
),
532 * Driver definition to register with platform structure.
534 static struct platform_driver ohci_hcd_da8xx_driver
= {
535 .probe
= ohci_da8xx_probe
,
536 .remove
= ohci_da8xx_remove
,
537 .shutdown
= usb_hcd_platform_shutdown
,
539 .suspend
= ohci_da8xx_suspend
,
540 .resume
= ohci_da8xx_resume
,
544 .of_match_table
= of_match_ptr(da8xx_ohci_ids
),
548 static int __init
ohci_da8xx_init(void)
554 pr_info("%s: " DRIVER_DESC
"\n", DRV_NAME
);
555 ohci_init_driver(&ohci_da8xx_hc_driver
, &da8xx_overrides
);
558 * The Davinci da8xx HW has some unusual quirks, which require
559 * da8xx-specific workarounds. We override certain hc_driver
560 * functions here to achieve that. We explicitly do not enhance
561 * ohci_driver_overrides to allow this more easily, since this
562 * is an unusual case, and we don't want to encourage others to
563 * override these functions by making it too easy.
566 orig_ohci_hub_control
= ohci_da8xx_hc_driver
.hub_control
;
567 orig_ohci_hub_status_data
= ohci_da8xx_hc_driver
.hub_status_data
;
569 ohci_da8xx_hc_driver
.hub_status_data
= ohci_da8xx_hub_status_data
;
570 ohci_da8xx_hc_driver
.hub_control
= ohci_da8xx_hub_control
;
572 return platform_driver_register(&ohci_hcd_da8xx_driver
);
574 module_init(ohci_da8xx_init
);
576 static void __exit
ohci_da8xx_exit(void)
578 platform_driver_unregister(&ohci_hcd_da8xx_driver
);
580 module_exit(ohci_da8xx_exit
);
581 MODULE_DESCRIPTION(DRIVER_DESC
);
582 MODULE_LICENSE("GPL");
583 MODULE_ALIAS("platform:" DRV_NAME
);