linux/drivers/usb/host/ohci-platform.c
<<
>>
Prefs
   1/*
   2 * Generic platform ohci driver
   3 *
   4 * Copyright 2007 Michael Buesch <m@bues.ch>
   5 * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
   6 * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
   7 *
   8 * Derived from the OCHI-SSB driver
   9 * Derived from the OHCI-PCI driver
  10 * Copyright 1999 Roman Weissgaerber
  11 * Copyright 2000-2002 David Brownell
  12 * Copyright 1999 Linus Torvalds
  13 * Copyright 1999 Gregory P. Smith
  14 *
  15 * Licensed under the GNU/GPL. See COPYING for details.
  16 */
  17
  18#include <linux/clk.h>
  19#include <linux/dma-mapping.h>
  20#include <linux/hrtimer.h>
  21#include <linux/io.h>
  22#include <linux/kernel.h>
  23#include <linux/module.h>
  24#include <linux/err.h>
  25#include <linux/phy/phy.h>
  26#include <linux/platform_device.h>
  27#include <linux/pm_runtime.h>
  28#include <linux/reset.h>
  29#include <linux/usb/ohci_pdriver.h>
  30#include <linux/usb.h>
  31#include <linux/usb/hcd.h>
  32
  33#include "ohci.h"
  34
  35#define DRIVER_DESC "OHCI generic platform driver"
  36#define OHCI_MAX_CLKS 3
  37#define OHCI_MAX_RESETS 2
  38#define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
  39
  40struct ohci_platform_priv {
  41        struct clk *clks[OHCI_MAX_CLKS];
  42        struct reset_control *resets[OHCI_MAX_RESETS];
  43        struct phy **phys;
  44        int num_phys;
  45};
  46
  47static const char hcd_name[] = "ohci-platform";
  48
  49static int ohci_platform_power_on(struct platform_device *dev)
  50{
  51        struct usb_hcd *hcd = platform_get_drvdata(dev);
  52        struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  53        int clk, ret, phy_num;
  54
  55        for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
  56                ret = clk_prepare_enable(priv->clks[clk]);
  57                if (ret)
  58                        goto err_disable_clks;
  59        }
  60
  61        for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  62                ret = phy_init(priv->phys[phy_num]);
  63                if (ret)
  64                        goto err_exit_phy;
  65                ret = phy_power_on(priv->phys[phy_num]);
  66                if (ret) {
  67                        phy_exit(priv->phys[phy_num]);
  68                        goto err_exit_phy;
  69                }
  70        }
  71
  72        return 0;
  73
  74err_exit_phy:
  75        while (--phy_num >= 0) {
  76                phy_power_off(priv->phys[phy_num]);
  77                phy_exit(priv->phys[phy_num]);
  78        }
  79err_disable_clks:
  80        while (--clk >= 0)
  81                clk_disable_unprepare(priv->clks[clk]);
  82
  83        return ret;
  84}
  85
  86static void ohci_platform_power_off(struct platform_device *dev)
  87{
  88        struct usb_hcd *hcd = platform_get_drvdata(dev);
  89        struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
  90        int clk, phy_num;
  91
  92        for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
  93                phy_power_off(priv->phys[phy_num]);
  94                phy_exit(priv->phys[phy_num]);
  95        }
  96
  97        for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
  98                if (priv->clks[clk])
  99                        clk_disable_unprepare(priv->clks[clk]);
 100}
 101
 102static struct hc_driver __read_mostly ohci_platform_hc_driver;
 103
 104static const struct ohci_driver_overrides platform_overrides __initconst = {
 105        .product_desc =         "Generic Platform OHCI controller",
 106        .extra_priv_size =      sizeof(struct ohci_platform_priv),
 107};
 108
 109static struct usb_ohci_pdata ohci_platform_defaults = {
 110        .power_on =             ohci_platform_power_on,
 111        .power_suspend =        ohci_platform_power_off,
 112        .power_off =            ohci_platform_power_off,
 113};
 114
 115static int ohci_platform_probe(struct platform_device *dev)
 116{
 117        struct usb_hcd *hcd;
 118        struct resource *res_mem;
 119        struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
 120        struct ohci_platform_priv *priv;
 121        struct ohci_hcd *ohci;
 122        int err, irq, phy_num, clk = 0, rst = 0;
 123
 124        if (usb_disabled())
 125                return -ENODEV;
 126
 127        /*
 128         * Use reasonable defaults so platforms don't have to provide these
 129         * with DT probing on ARM.
 130         */
 131        if (!pdata)
 132                pdata = &ohci_platform_defaults;
 133
 134        err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
 135        if (err)
 136                return err;
 137
 138        irq = platform_get_irq(dev, 0);
 139        if (irq < 0) {
 140                dev_err(&dev->dev, "no irq provided");
 141                return irq;
 142        }
 143
 144        hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
 145                        dev_name(&dev->dev));
 146        if (!hcd)
 147                return -ENOMEM;
 148
 149        platform_set_drvdata(dev, hcd);
 150        dev->dev.platform_data = pdata;
 151        priv = hcd_to_ohci_priv(hcd);
 152        ohci = hcd_to_ohci(hcd);
 153
 154        if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
 155                if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
 156                        ohci->flags |= OHCI_QUIRK_BE_MMIO;
 157
 158                if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
 159                        ohci->flags |= OHCI_QUIRK_BE_DESC;
 160
 161                if (of_property_read_bool(dev->dev.of_node, "big-endian"))
 162                        ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
 163
 164                if (of_property_read_bool(dev->dev.of_node, "no-big-frame-no"))
 165                        ohci->flags |= OHCI_QUIRK_FRAME_NO;
 166
 167                if (of_property_read_bool(dev->dev.of_node,
 168                                          "remote-wakeup-connected"))
 169                        ohci->hc_control = OHCI_CTRL_RWC;
 170
 171                of_property_read_u32(dev->dev.of_node, "num-ports",
 172                                     &ohci->num_ports);
 173
 174                priv->num_phys = of_count_phandle_with_args(dev->dev.of_node,
 175                                "phys", "#phy-cells");
 176
 177                if (priv->num_phys > 0) {
 178                        priv->phys = devm_kcalloc(&dev->dev, priv->num_phys,
 179                                            sizeof(struct phy *), GFP_KERNEL);
 180                        if (!priv->phys)
 181                                return -ENOMEM;
 182                } else
 183                        priv->num_phys = 0;
 184
 185                for (phy_num = 0; phy_num < priv->num_phys; phy_num++) {
 186                        priv->phys[phy_num] = devm_of_phy_get_by_index(
 187                                        &dev->dev, dev->dev.of_node, phy_num);
 188                        if (IS_ERR(priv->phys[phy_num])) {
 189                                err = PTR_ERR(priv->phys[phy_num]);
 190                                goto err_put_hcd;
 191                        } else if (!hcd->phy) {
 192                                /* Avoiding phy_get() in usb_add_hcd() */
 193                                hcd->phy = priv->phys[phy_num];
 194                        }
 195                }
 196
 197                for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
 198                        priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
 199                        if (IS_ERR(priv->clks[clk])) {
 200                                err = PTR_ERR(priv->clks[clk]);
 201                                if (err == -EPROBE_DEFER)
 202                                        goto err_put_clks;
 203                                priv->clks[clk] = NULL;
 204                                break;
 205                        }
 206                }
 207                for (rst = 0; rst < OHCI_MAX_RESETS; rst++) {
 208                        priv->resets[rst] =
 209                                devm_reset_control_get_shared_by_index(
 210                                                                &dev->dev, rst);
 211                        if (IS_ERR(priv->resets[rst])) {
 212                                err = PTR_ERR(priv->resets[rst]);
 213                                if (err == -EPROBE_DEFER)
 214                                        goto err_reset;
 215                                priv->resets[rst] = NULL;
 216                                break;
 217                        }
 218                        err = reset_control_deassert(priv->resets[rst]);
 219                        if (err)
 220                                goto err_reset;
 221                }
 222        }
 223
 224        if (pdata->big_endian_desc)
 225                ohci->flags |= OHCI_QUIRK_BE_DESC;
 226        if (pdata->big_endian_mmio)
 227                ohci->flags |= OHCI_QUIRK_BE_MMIO;
 228        if (pdata->no_big_frame_no)
 229                ohci->flags |= OHCI_QUIRK_FRAME_NO;
 230        if (pdata->num_ports)
 231                ohci->num_ports = pdata->num_ports;
 232
 233#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
 234        if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
 235                dev_err(&dev->dev,
 236                        "Error: CONFIG_USB_OHCI_BIG_ENDIAN_MMIO not set\n");
 237                err = -EINVAL;
 238                goto err_reset;
 239        }
 240#endif
 241#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
 242        if (ohci->flags & OHCI_QUIRK_BE_DESC) {
 243                dev_err(&dev->dev,
 244                        "Error: CONFIG_USB_OHCI_BIG_ENDIAN_DESC not set\n");
 245                err = -EINVAL;
 246                goto err_reset;
 247        }
 248#endif
 249
 250        pm_runtime_set_active(&dev->dev);
 251        pm_runtime_enable(&dev->dev);
 252        if (pdata->power_on) {
 253                err = pdata->power_on(dev);
 254                if (err < 0)
 255                        goto err_reset;
 256        }
 257
 258        res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
 259        hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
 260        if (IS_ERR(hcd->regs)) {
 261                err = PTR_ERR(hcd->regs);
 262                goto err_power;
 263        }
 264        hcd->rsrc_start = res_mem->start;
 265        hcd->rsrc_len = resource_size(res_mem);
 266
 267        err = usb_add_hcd(hcd, irq, IRQF_SHARED);
 268        if (err)
 269                goto err_power;
 270
 271        device_wakeup_enable(hcd->self.controller);
 272
 273        platform_set_drvdata(dev, hcd);
 274
 275        return err;
 276
 277err_power:
 278        if (pdata->power_off)
 279                pdata->power_off(dev);
 280err_reset:
 281        pm_runtime_disable(&dev->dev);
 282        while (--rst >= 0)
 283                reset_control_assert(priv->resets[rst]);
 284err_put_clks:
 285        while (--clk >= 0)
 286                clk_put(priv->clks[clk]);
 287err_put_hcd:
 288        if (pdata == &ohci_platform_defaults)
 289                dev->dev.platform_data = NULL;
 290
 291        usb_put_hcd(hcd);
 292
 293        return err;
 294}
 295
 296static int ohci_platform_remove(struct platform_device *dev)
 297{
 298        struct usb_hcd *hcd = platform_get_drvdata(dev);
 299        struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
 300        struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
 301        int clk, rst;
 302
 303        pm_runtime_get_sync(&dev->dev);
 304        usb_remove_hcd(hcd);
 305
 306        if (pdata->power_off)
 307                pdata->power_off(dev);
 308
 309        for (rst = 0; rst < OHCI_MAX_RESETS && priv->resets[rst]; rst++)
 310                reset_control_assert(priv->resets[rst]);
 311
 312        for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
 313                clk_put(priv->clks[clk]);
 314
 315        usb_put_hcd(hcd);
 316
 317        pm_runtime_put_sync(&dev->dev);
 318        pm_runtime_disable(&dev->dev);
 319
 320        if (pdata == &ohci_platform_defaults)
 321                dev->dev.platform_data = NULL;
 322
 323        return 0;
 324}
 325
 326#ifdef CONFIG_PM_SLEEP
 327static int ohci_platform_suspend(struct device *dev)
 328{
 329        struct usb_hcd *hcd = dev_get_drvdata(dev);
 330        struct usb_ohci_pdata *pdata = dev->platform_data;
 331        struct platform_device *pdev = to_platform_device(dev);
 332        bool do_wakeup = device_may_wakeup(dev);
 333        int ret;
 334
 335        ret = ohci_suspend(hcd, do_wakeup);
 336        if (ret)
 337                return ret;
 338
 339        if (pdata->power_suspend)
 340                pdata->power_suspend(pdev);
 341
 342        return ret;
 343}
 344
 345static int ohci_platform_resume(struct device *dev)
 346{
 347        struct usb_hcd *hcd = dev_get_drvdata(dev);
 348        struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
 349        struct platform_device *pdev = to_platform_device(dev);
 350
 351        if (pdata->power_on) {
 352                int err = pdata->power_on(pdev);
 353                if (err < 0)
 354                        return err;
 355        }
 356
 357        ohci_resume(hcd, false);
 358        return 0;
 359}
 360#endif /* CONFIG_PM_SLEEP */
 361
 362static const struct of_device_id ohci_platform_ids[] = {
 363        { .compatible = "generic-ohci", },
 364        { .compatible = "cavium,octeon-6335-ohci", },
 365        { .compatible = "ti,ohci-omap3", },
 366        { }
 367};
 368MODULE_DEVICE_TABLE(of, ohci_platform_ids);
 369
 370static const struct platform_device_id ohci_platform_table[] = {
 371        { "ohci-platform", 0 },
 372        { }
 373};
 374MODULE_DEVICE_TABLE(platform, ohci_platform_table);
 375
 376static SIMPLE_DEV_PM_OPS(ohci_platform_pm_ops, ohci_platform_suspend,
 377        ohci_platform_resume);
 378
 379static struct platform_driver ohci_platform_driver = {
 380        .id_table       = ohci_platform_table,
 381        .probe          = ohci_platform_probe,
 382        .remove         = ohci_platform_remove,
 383        .shutdown       = usb_hcd_platform_shutdown,
 384        .driver         = {
 385                .name   = "ohci-platform",
 386                .pm     = &ohci_platform_pm_ops,
 387                .of_match_table = ohci_platform_ids,
 388        }
 389};
 390
 391static int __init ohci_platform_init(void)
 392{
 393        if (usb_disabled())
 394                return -ENODEV;
 395
 396        pr_info("%s: " DRIVER_DESC "\n", hcd_name);
 397
 398        ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
 399        return platform_driver_register(&ohci_platform_driver);
 400}
 401module_init(ohci_platform_init);
 402
 403static void __exit ohci_platform_cleanup(void)
 404{
 405        platform_driver_unregister(&ohci_platform_driver);
 406}
 407module_exit(ohci_platform_cleanup);
 408
 409MODULE_DESCRIPTION(DRIVER_DESC);
 410MODULE_AUTHOR("Hauke Mehrtens");
 411MODULE_AUTHOR("Alan Stern");
 412MODULE_LICENSE("GPL");
 413