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>
19 #include <linux/platform_device.h>
20 #include <linux/phy/phy.h>
21 #include <linux/platform_data/usb-davinci.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25 #include <asm/unaligned.h>
29 #define DRIVER_DESC "DA8XX"
30 #define DRV_NAME "ohci-da8xx"
32 static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
34 static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq,
35 u16 wValue, u16 wIndex, char *buf, u16 wLength);
36 static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
38 struct da8xx_ohci_hcd {
40 struct clk *usb11_clk;
41 struct phy *usb11_phy;
42 struct regulator *vbus_reg;
43 struct notifier_block nb;
44 struct gpio_desc *oc_gpio;
47 #define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
49 /* Over-current indicator change bitmask */
50 static volatile u16 ocic_mask;
52 static int ohci_da8xx_enable(struct usb_hcd *hcd)
54 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
57 ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
61 ret = phy_init(da8xx_ohci->usb11_phy);
65 ret = phy_power_on(da8xx_ohci->usb11_phy);
67 goto err_phy_power_on;
72 phy_exit(da8xx_ohci->usb11_phy);
74 clk_disable_unprepare(da8xx_ohci->usb11_clk);
79 static void ohci_da8xx_disable(struct usb_hcd *hcd)
81 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
83 phy_power_off(da8xx_ohci->usb11_phy);
84 phy_exit(da8xx_ohci->usb11_phy);
85 clk_disable_unprepare(da8xx_ohci->usb11_clk);
88 static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
90 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
91 struct device *dev = hcd->self.controller;
94 if (!da8xx_ohci->vbus_reg)
98 ret = regulator_enable(da8xx_ohci->vbus_reg);
100 dev_err(dev, "Failed to enable regulator: %d\n", ret);
104 ret = regulator_disable(da8xx_ohci->vbus_reg);
106 dev_err(dev, "Failed to disable regulator: %d\n", ret);
114 static int ohci_da8xx_get_power(struct usb_hcd *hcd)
116 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
118 if (da8xx_ohci->vbus_reg)
119 return regulator_is_enabled(da8xx_ohci->vbus_reg);
124 static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
126 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
130 if (da8xx_ohci->oc_gpio)
131 return gpiod_get_value_cansleep(da8xx_ohci->oc_gpio);
133 if (!da8xx_ohci->vbus_reg)
136 ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags);
140 if (flags & REGULATOR_ERROR_OVER_CURRENT)
146 static int ohci_da8xx_has_set_power(struct usb_hcd *hcd)
148 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
150 if (da8xx_ohci->vbus_reg)
156 static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
158 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
160 if (da8xx_ohci->oc_gpio)
163 if (da8xx_ohci->vbus_reg)
169 static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd)
171 struct device *dev = hcd->self.controller;
172 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
174 if (hub && hub->potpgt)
180 static int ohci_da8xx_regulator_event(struct notifier_block *nb,
181 unsigned long event, void *data)
183 struct da8xx_ohci_hcd *da8xx_ohci =
184 container_of(nb, struct da8xx_ohci_hcd, nb);
186 if (event & REGULATOR_EVENT_OVER_CURRENT) {
188 ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
194 static irqreturn_t ohci_da8xx_oc_thread(int irq, void *data)
196 struct da8xx_ohci_hcd *da8xx_ohci = data;
197 struct device *dev = da8xx_ohci->hcd->self.controller;
200 if (gpiod_get_value_cansleep(da8xx_ohci->oc_gpio) &&
201 da8xx_ohci->vbus_reg) {
202 ret = regulator_disable(da8xx_ohci->vbus_reg);
204 dev_err(dev, "Failed to disable regulator: %d\n", ret);
210 static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
212 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
213 struct device *dev = hcd->self.controller;
216 if (!da8xx_ohci->oc_gpio && da8xx_ohci->vbus_reg) {
217 da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
218 ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
223 dev_err(dev, "Failed to register notifier: %d\n", ret);
228 static int ohci_da8xx_reset(struct usb_hcd *hcd)
230 struct device *dev = hcd->self.controller;
231 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
232 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
236 dev_dbg(dev, "starting USB controller\n");
238 result = ohci_da8xx_enable(hcd);
243 * DA8xx only have 1 port connected to the pins but the HC root hub
244 * register A reports 2 ports, thus we'll have to override it...
248 result = ohci_setup(hcd);
250 ohci_da8xx_disable(hcd);
255 * Since we're providing a board-specific root hub port power control
256 * and over-current reporting, we have to override the HC root hub A
257 * register's default value, so that ohci_hub_control() could return
258 * the correct hub descriptor...
260 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
261 if (ohci_da8xx_has_set_power(hcd)) {
265 if (ohci_da8xx_has_oci(hcd)) {
269 if (ohci_da8xx_has_potpgt(hcd)) {
270 rh_a &= ~RH_A_POTPGT;
271 rh_a |= hub->potpgt << 24;
273 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
279 * Update the status data from the hub with the over-current indicator change.
281 static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
283 int length = orig_ohci_hub_status_data(hcd, buf);
285 /* See if we have OCIC bit set on port 1 */
286 if (ocic_mask & (1 << 1)) {
287 dev_dbg(hcd->self.controller, "over-current indicator change "
299 * Look at the control requests to the root hub and see if we need to override.
301 static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
302 u16 wIndex, char *buf, u16 wLength)
304 struct device *dev = hcd->self.controller;
309 /* Check the port number */
313 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
315 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
317 /* The port power status (PPS) bit defaults to 1 */
318 if (!ohci_da8xx_get_power(hcd))
321 /* The port over-current indicator (POCI) bit is always 0 */
322 if (ohci_da8xx_get_oci(hcd) > 0)
325 /* The over-current indicator change (OCIC) bit is 0 too */
326 if (ocic_mask & (1 << wIndex))
329 put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
334 case ClearPortFeature:
338 /* Check the port number */
343 case USB_PORT_FEAT_POWER:
344 dev_dbg(dev, "%sPortFeature(%u): %s\n",
345 temp ? "Set" : "Clear", wIndex, "POWER");
347 return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
348 case USB_PORT_FEAT_C_OVER_CURRENT:
349 dev_dbg(dev, "%sPortFeature(%u): %s\n",
350 temp ? "Set" : "Clear", wIndex,
354 ocic_mask |= 1 << wIndex;
356 ocic_mask &= ~(1 << wIndex);
361 return orig_ohci_hub_control(hcd, typeReq, wValue,
362 wIndex, buf, wLength);
365 /*-------------------------------------------------------------------------*/
367 static const struct of_device_id da8xx_ohci_ids[] = {
368 { .compatible = "ti,da830-ohci" },
371 MODULE_DEVICE_TABLE(of, da8xx_ohci_ids);
374 static int ohci_da8xx_probe(struct platform_device *pdev)
376 struct da8xx_ohci_hcd *da8xx_ohci;
377 struct device *dev = &pdev->dev;
378 int error, hcd_irq, oc_irq;
380 struct resource *mem;
382 hcd = usb_create_hcd(&ohci_da8xx_hc_driver, dev, dev_name(dev));
386 da8xx_ohci = to_da8xx_ohci(hcd);
387 da8xx_ohci->hcd = hcd;
389 da8xx_ohci->usb11_clk = devm_clk_get(dev, NULL);
390 if (IS_ERR(da8xx_ohci->usb11_clk)) {
391 error = PTR_ERR(da8xx_ohci->usb11_clk);
392 if (error != -EPROBE_DEFER)
393 dev_err(dev, "Failed to get clock.\n");
397 da8xx_ohci->usb11_phy = devm_phy_get(dev, "usb-phy");
398 if (IS_ERR(da8xx_ohci->usb11_phy)) {
399 error = PTR_ERR(da8xx_ohci->usb11_phy);
400 if (error != -EPROBE_DEFER)
401 dev_err(dev, "Failed to get phy.\n");
405 da8xx_ohci->vbus_reg = devm_regulator_get_optional(dev, "vbus");
406 if (IS_ERR(da8xx_ohci->vbus_reg)) {
407 error = PTR_ERR(da8xx_ohci->vbus_reg);
408 if (error == -ENODEV) {
409 da8xx_ohci->vbus_reg = NULL;
410 } else if (error == -EPROBE_DEFER) {
413 dev_err(dev, "Failed to get regulator\n");
418 da8xx_ohci->oc_gpio = devm_gpiod_get_optional(dev, "oc", GPIOD_IN);
419 if (IS_ERR(da8xx_ohci->oc_gpio)) {
420 error = PTR_ERR(da8xx_ohci->oc_gpio);
424 if (da8xx_ohci->oc_gpio) {
425 oc_irq = gpiod_to_irq(da8xx_ohci->oc_gpio);
431 error = devm_request_threaded_irq(dev, oc_irq, NULL,
432 ohci_da8xx_oc_thread, IRQF_TRIGGER_RISING |
433 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
434 "OHCI over-current indicator", da8xx_ohci);
439 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &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 void ohci_da8xx_remove(struct platform_device *pdev)
474 struct usb_hcd *hcd = platform_get_drvdata(pdev);
481 static int ohci_da8xx_suspend(struct platform_device *pdev,
482 pm_message_t message)
484 struct usb_hcd *hcd = platform_get_drvdata(pdev);
485 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
486 bool do_wakeup = device_may_wakeup(&pdev->dev);
490 if (time_before(jiffies, ohci->next_statechange))
492 ohci->next_statechange = jiffies;
494 ret = ohci_suspend(hcd, do_wakeup);
498 ohci_da8xx_disable(hcd);
499 hcd->state = HC_STATE_SUSPENDED;
504 static int ohci_da8xx_resume(struct platform_device *dev)
506 struct usb_hcd *hcd = platform_get_drvdata(dev);
507 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
510 if (time_before(jiffies, ohci->next_statechange))
512 ohci->next_statechange = jiffies;
514 ret = ohci_da8xx_enable(hcd);
518 ohci_resume(hcd, false);
524 static const struct ohci_driver_overrides da8xx_overrides __initconst = {
525 .reset = ohci_da8xx_reset,
526 .extra_priv_size = sizeof(struct da8xx_ohci_hcd),
530 * Driver definition to register with platform structure.
532 static struct platform_driver ohci_hcd_da8xx_driver = {
533 .probe = ohci_da8xx_probe,
534 .remove_new = ohci_da8xx_remove,
535 .shutdown = usb_hcd_platform_shutdown,
537 .suspend = ohci_da8xx_suspend,
538 .resume = ohci_da8xx_resume,
542 .of_match_table = of_match_ptr(da8xx_ohci_ids),
546 static int __init ohci_da8xx_init(void)
552 ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
555 * The Davinci da8xx HW has some unusual quirks, which require
556 * da8xx-specific workarounds. We override certain hc_driver
557 * functions here to achieve that. We explicitly do not enhance
558 * ohci_driver_overrides to allow this more easily, since this
559 * is an unusual case, and we don't want to encourage others to
560 * override these functions by making it too easy.
563 orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
564 orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
566 ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data;
567 ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control;
569 return platform_driver_register(&ohci_hcd_da8xx_driver);
571 module_init(ohci_da8xx_init);
573 static void __exit ohci_da8xx_exit(void)
575 platform_driver_unregister(&ohci_hcd_da8xx_driver);
577 module_exit(ohci_da8xx_exit);
578 MODULE_DESCRIPTION(DRIVER_DESC);
579 MODULE_LICENSE("GPL");
580 MODULE_ALIAS("platform:" DRV_NAME);