Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-1.0+
0002 /*
0003  * OHCI HCD (Host Controller Driver) for USB.
0004  *
0005  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
0006  * (C) Copyright 2000-2005 David Brownell
0007  * (C) Copyright 2002 Hewlett-Packard Company
0008  *
0009  * OMAP Bus Glue
0010  *
0011  * Modified for OMAP by Tony Lindgren <tony@atomide.com>
0012  * Based on the 2.4 OMAP OHCI driver originally done by MontaVista Software Inc.
0013  * and on ohci-sa1111.c by Christopher Hoover <ch@hpl.hp.com>
0014  *
0015  * This file is licenced under the GPL.
0016  */
0017 
0018 #include <linux/clk.h>
0019 #include <linux/dma-mapping.h>
0020 #include <linux/err.h>
0021 #include <linux/gpio/consumer.h>
0022 #include <linux/io.h>
0023 #include <linux/jiffies.h>
0024 #include <linux/kernel.h>
0025 #include <linux/module.h>
0026 #include <linux/usb/otg.h>
0027 #include <linux/platform_device.h>
0028 #include <linux/platform_data/usb-omap1.h>
0029 #include <linux/soc/ti/omap1-usb.h>
0030 #include <linux/soc/ti/omap1-mux.h>
0031 #include <linux/soc/ti/omap1-soc.h>
0032 #include <linux/soc/ti/omap1-io.h>
0033 #include <linux/signal.h>
0034 #include <linux/usb.h>
0035 #include <linux/usb/hcd.h>
0036 
0037 #include "ohci.h"
0038 
0039 #include <asm/io.h>
0040 #include <asm/mach-types.h>
0041 
0042 #define DRIVER_DESC "OHCI OMAP driver"
0043 
0044 struct ohci_omap_priv {
0045     struct clk *usb_host_ck;
0046     struct clk *usb_dc_ck;
0047     struct gpio_desc *power;
0048     struct gpio_desc *overcurrent;
0049 };
0050 
0051 static const char hcd_name[] = "ohci-omap";
0052 static struct hc_driver __read_mostly ohci_omap_hc_driver;
0053 
0054 #define hcd_to_ohci_omap_priv(h) \
0055     ((struct ohci_omap_priv *)hcd_to_ohci(h)->priv)
0056 
0057 static void omap_ohci_clock_power(struct ohci_omap_priv *priv, int on)
0058 {
0059     if (on) {
0060         clk_enable(priv->usb_dc_ck);
0061         clk_enable(priv->usb_host_ck);
0062         /* guesstimate for T5 == 1x 32K clock + APLL lock time */
0063         udelay(100);
0064     } else {
0065         clk_disable(priv->usb_host_ck);
0066         clk_disable(priv->usb_dc_ck);
0067     }
0068 }
0069 
0070 #ifdef  CONFIG_USB_OTG
0071 
0072 static void start_hnp(struct ohci_hcd *ohci)
0073 {
0074     struct usb_hcd *hcd = ohci_to_hcd(ohci);
0075     const unsigned  port = hcd->self.otg_port - 1;
0076     unsigned long   flags;
0077     u32 l;
0078 
0079     otg_start_hnp(hcd->usb_phy->otg);
0080 
0081     local_irq_save(flags);
0082     hcd->usb_phy->otg->state = OTG_STATE_A_SUSPEND;
0083     writel (RH_PS_PSS, &ohci->regs->roothub.portstatus [port]);
0084     l = omap_readl(OTG_CTRL);
0085     l &= ~OTG_A_BUSREQ;
0086     omap_writel(l, OTG_CTRL);
0087     local_irq_restore(flags);
0088 }
0089 
0090 #endif
0091 
0092 /*-------------------------------------------------------------------------*/
0093 
0094 static int ohci_omap_reset(struct usb_hcd *hcd)
0095 {
0096     struct ohci_hcd     *ohci = hcd_to_ohci(hcd);
0097     struct omap_usb_config  *config = dev_get_platdata(hcd->self.controller);
0098     struct ohci_omap_priv   *priv = hcd_to_ohci_omap_priv(hcd);
0099     int         need_transceiver = (config->otg != 0);
0100     int         ret;
0101 
0102     dev_dbg(hcd->self.controller, "starting USB Controller\n");
0103 
0104     if (config->otg) {
0105         hcd->self.otg_port = config->otg;
0106         /* default/minimum OTG power budget:  8 mA */
0107         hcd->power_budget = 8;
0108     }
0109 
0110     /* boards can use OTG transceivers in non-OTG modes */
0111     need_transceiver = need_transceiver
0112             || machine_is_omap_h2() || machine_is_omap_h3();
0113 
0114     /* XXX OMAP16xx only */
0115     if (config->ocpi_enable)
0116         config->ocpi_enable();
0117 
0118 #ifdef  CONFIG_USB_OTG
0119     if (need_transceiver) {
0120         hcd->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
0121         if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
0122             int status = otg_set_host(hcd->usb_phy->otg,
0123                         &ohci_to_hcd(ohci)->self);
0124             dev_dbg(hcd->self.controller, "init %s phy, status %d\n",
0125                     hcd->usb_phy->label, status);
0126             if (status) {
0127                 usb_put_phy(hcd->usb_phy);
0128                 return status;
0129             }
0130         } else {
0131             return -EPROBE_DEFER;
0132         }
0133         hcd->skip_phy_initialization = 1;
0134         ohci->start_hnp = start_hnp;
0135     }
0136 #endif
0137 
0138     omap_ohci_clock_power(priv, 1);
0139 
0140     if (config->lb_reset)
0141         config->lb_reset();
0142 
0143     ret = ohci_setup(hcd);
0144     if (ret < 0)
0145         return ret;
0146 
0147     if (config->otg || config->rwc) {
0148         ohci->hc_control = OHCI_CTRL_RWC;
0149         writel(OHCI_CTRL_RWC, &ohci->regs->control);
0150     }
0151 
0152     /* board-specific power switching and overcurrent support */
0153     if (machine_is_omap_osk() || machine_is_omap_innovator()) {
0154         u32 rh = roothub_a (ohci);
0155 
0156         /* power switching (ganged by default) */
0157         rh &= ~RH_A_NPS;
0158 
0159         /* TPS2045 switch for internal transceiver (port 1) */
0160         if (machine_is_omap_osk()) {
0161             ohci_to_hcd(ohci)->power_budget = 250;
0162 
0163             rh &= ~RH_A_NOCP;
0164 
0165             /* gpio9 for overcurrent detction */
0166             omap_cfg_reg(W8_1610_GPIO9);
0167 
0168             /* for paranoia's sake:  disable USB.PUEN */
0169             omap_cfg_reg(W4_USB_HIGHZ);
0170         }
0171         ohci_writel(ohci, rh, &ohci->regs->roothub.a);
0172         ohci->flags &= ~OHCI_QUIRK_HUB_POWER;
0173     } else if (machine_is_nokia770()) {
0174         /* We require a self-powered hub, which should have
0175          * plenty of power. */
0176         ohci_to_hcd(ohci)->power_budget = 0;
0177     }
0178 
0179     /* FIXME hub_wq hub requests should manage power switching */
0180     if (config->transceiver_power)
0181         return config->transceiver_power(1);
0182 
0183     if (priv->power)
0184         gpiod_set_value_cansleep(priv->power, 0);
0185 
0186     /* board init will have already handled HMC and mux setup.
0187      * any external transceiver should already be initialized
0188      * too, so all configured ports use the right signaling now.
0189      */
0190 
0191     return 0;
0192 }
0193 
0194 /*-------------------------------------------------------------------------*/
0195 
0196 /**
0197  * ohci_hcd_omap_probe - initialize OMAP-based HCDs
0198  * @pdev:   USB controller to probe
0199  *
0200  * Context: task context, might sleep
0201  *
0202  * Allocates basic resources for this USB host controller, and
0203  * then invokes the start() method for the HCD associated with it
0204  * through the hotplug entry's driver_data.
0205  */
0206 static int ohci_hcd_omap_probe(struct platform_device *pdev)
0207 {
0208     int retval, irq;
0209     struct usb_hcd *hcd = 0;
0210     struct ohci_omap_priv *priv;
0211 
0212     if (pdev->num_resources != 2) {
0213         dev_err(&pdev->dev, "invalid num_resources: %i\n",
0214                pdev->num_resources);
0215         return -ENODEV;
0216     }
0217 
0218     if (pdev->resource[0].flags != IORESOURCE_MEM
0219             || pdev->resource[1].flags != IORESOURCE_IRQ) {
0220         dev_err(&pdev->dev, "invalid resource type\n");
0221         return -ENODEV;
0222     }
0223 
0224     hcd = usb_create_hcd(&ohci_omap_hc_driver, &pdev->dev,
0225             dev_name(&pdev->dev));
0226     if (!hcd)
0227         return -ENOMEM;
0228 
0229     hcd->rsrc_start = pdev->resource[0].start;
0230     hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
0231     priv = hcd_to_ohci_omap_priv(hcd);
0232 
0233     /* Obtain two optional GPIO lines */
0234     priv->power = devm_gpiod_get_optional(&pdev->dev, "power", GPIOD_ASIS);
0235     if (IS_ERR(priv->power)) {
0236         retval = PTR_ERR(priv->power);
0237         goto err_put_hcd;
0238     }
0239     if (priv->power)
0240         gpiod_set_consumer_name(priv->power, "OHCI power");
0241 
0242     /*
0243      * This "overcurrent" GPIO line isn't really used in the code,
0244      * but has a designated hardware function.
0245      * TODO: implement proper overcurrent handling.
0246      */
0247     priv->overcurrent = devm_gpiod_get_optional(&pdev->dev, "overcurrent",
0248                             GPIOD_IN);
0249     if (IS_ERR(priv->overcurrent)) {
0250         retval = PTR_ERR(priv->overcurrent);
0251         goto err_put_hcd;
0252     }
0253     if (priv->overcurrent)
0254         gpiod_set_consumer_name(priv->overcurrent, "OHCI overcurrent");
0255 
0256     priv->usb_host_ck = clk_get(&pdev->dev, "usb_hhc_ck");
0257     if (IS_ERR(priv->usb_host_ck)) {
0258         retval = PTR_ERR(priv->usb_host_ck);
0259         goto err_put_hcd;
0260     }
0261 
0262     retval = clk_prepare(priv->usb_host_ck);
0263     if (retval)
0264         goto err_put_host_ck;
0265 
0266     if (!cpu_is_omap15xx())
0267         priv->usb_dc_ck = clk_get(&pdev->dev, "usb_dc_ck");
0268     else
0269         priv->usb_dc_ck = clk_get(&pdev->dev, "lb_ck");
0270 
0271     if (IS_ERR(priv->usb_dc_ck)) {
0272         retval = PTR_ERR(priv->usb_dc_ck);
0273         goto err_unprepare_host_ck;
0274     }
0275 
0276     retval = clk_prepare(priv->usb_dc_ck);
0277     if (retval)
0278         goto err_put_dc_ck;
0279 
0280     if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
0281         dev_dbg(&pdev->dev, "request_mem_region failed\n");
0282         retval = -EBUSY;
0283         goto err_unprepare_dc_ck;
0284     }
0285 
0286     hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
0287     if (!hcd->regs) {
0288         dev_err(&pdev->dev, "can't ioremap OHCI HCD\n");
0289         retval = -ENOMEM;
0290         goto err2;
0291     }
0292 
0293     irq = platform_get_irq(pdev, 0);
0294     if (irq < 0) {
0295         retval = irq;
0296         goto err3;
0297     }
0298     retval = usb_add_hcd(hcd, irq, 0);
0299     if (retval)
0300         goto err3;
0301 
0302     device_wakeup_enable(hcd->self.controller);
0303     return 0;
0304 err3:
0305     iounmap(hcd->regs);
0306 err2:
0307     release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
0308 err_unprepare_dc_ck:
0309     clk_unprepare(priv->usb_dc_ck);
0310 err_put_dc_ck:
0311     clk_put(priv->usb_dc_ck);
0312 err_unprepare_host_ck:
0313     clk_unprepare(priv->usb_host_ck);
0314 err_put_host_ck:
0315     clk_put(priv->usb_host_ck);
0316 err_put_hcd:
0317     usb_put_hcd(hcd);
0318     return retval;
0319 }
0320 
0321 
0322 /* may be called with controller, bus, and devices active */
0323 
0324 /**
0325  * ohci_hcd_omap_remove - shutdown processing for OMAP-based HCDs
0326  * @pdev: USB Host Controller being removed
0327  *
0328  * Context: task context, might sleep
0329  *
0330  * Reverses the effect of ohci_hcd_omap_probe(), first invoking
0331  * the HCD's stop() method.  It is always called from a thread
0332  * context, normally "rmmod", "apmd", or something similar.
0333  */
0334 static int ohci_hcd_omap_remove(struct platform_device *pdev)
0335 {
0336     struct usb_hcd  *hcd = platform_get_drvdata(pdev);
0337     struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
0338 
0339     dev_dbg(hcd->self.controller, "stopping USB Controller\n");
0340     usb_remove_hcd(hcd);
0341     omap_ohci_clock_power(priv, 0);
0342     if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
0343         (void) otg_set_host(hcd->usb_phy->otg, 0);
0344         usb_put_phy(hcd->usb_phy);
0345     }
0346     iounmap(hcd->regs);
0347     release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
0348     clk_unprepare(priv->usb_dc_ck);
0349     clk_put(priv->usb_dc_ck);
0350     clk_unprepare(priv->usb_host_ck);
0351     clk_put(priv->usb_host_ck);
0352     usb_put_hcd(hcd);
0353     return 0;
0354 }
0355 
0356 /*-------------------------------------------------------------------------*/
0357 
0358 #ifdef  CONFIG_PM
0359 
0360 static int ohci_omap_suspend(struct platform_device *pdev, pm_message_t message)
0361 {
0362     struct usb_hcd *hcd = platform_get_drvdata(pdev);
0363     struct ohci_hcd *ohci = hcd_to_ohci(hcd);
0364     struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
0365     bool do_wakeup = device_may_wakeup(&pdev->dev);
0366     int ret;
0367 
0368     if (time_before(jiffies, ohci->next_statechange))
0369         msleep(5);
0370     ohci->next_statechange = jiffies;
0371 
0372     ret = ohci_suspend(hcd, do_wakeup);
0373     if (ret)
0374         return ret;
0375 
0376     omap_ohci_clock_power(priv, 0);
0377     return ret;
0378 }
0379 
0380 static int ohci_omap_resume(struct platform_device *dev)
0381 {
0382     struct usb_hcd  *hcd = platform_get_drvdata(dev);
0383     struct ohci_hcd *ohci = hcd_to_ohci(hcd);
0384     struct ohci_omap_priv *priv = hcd_to_ohci_omap_priv(hcd);
0385 
0386     if (time_before(jiffies, ohci->next_statechange))
0387         msleep(5);
0388     ohci->next_statechange = jiffies;
0389 
0390     omap_ohci_clock_power(priv, 1);
0391     ohci_resume(hcd, false);
0392     return 0;
0393 }
0394 
0395 #endif
0396 
0397 /*-------------------------------------------------------------------------*/
0398 
0399 /*
0400  * Driver definition to register with the OMAP bus
0401  */
0402 static struct platform_driver ohci_hcd_omap_driver = {
0403     .probe      = ohci_hcd_omap_probe,
0404     .remove     = ohci_hcd_omap_remove,
0405     .shutdown   = usb_hcd_platform_shutdown,
0406 #ifdef  CONFIG_PM
0407     .suspend    = ohci_omap_suspend,
0408     .resume     = ohci_omap_resume,
0409 #endif
0410     .driver     = {
0411         .name   = "ohci",
0412     },
0413 };
0414 
0415 static const struct ohci_driver_overrides omap_overrides __initconst = {
0416     .product_desc   = "OMAP OHCI",
0417     .reset      = ohci_omap_reset,
0418     .extra_priv_size = sizeof(struct ohci_omap_priv),
0419 };
0420 
0421 static int __init ohci_omap_init(void)
0422 {
0423     if (usb_disabled())
0424         return -ENODEV;
0425 
0426     pr_info("%s: " DRIVER_DESC "\n", hcd_name);
0427 
0428     ohci_init_driver(&ohci_omap_hc_driver, &omap_overrides);
0429     return platform_driver_register(&ohci_hcd_omap_driver);
0430 }
0431 module_init(ohci_omap_init);
0432 
0433 static void __exit ohci_omap_cleanup(void)
0434 {
0435     platform_driver_unregister(&ohci_hcd_omap_driver);
0436 }
0437 module_exit(ohci_omap_cleanup);
0438 
0439 MODULE_DESCRIPTION(DRIVER_DESC);
0440 MODULE_ALIAS("platform:ohci");
0441 MODULE_LICENSE("GPL");