1*5fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2d1158372SPeter Griffin /* 3d1158372SPeter Griffin * ST OHCI driver 4d1158372SPeter Griffin * 5d1158372SPeter Griffin * Copyright (C) 2014 STMicroelectronics – All Rights Reserved 6d1158372SPeter Griffin * 7d1158372SPeter Griffin * Author: Peter Griffin <peter.griffin@linaro.org> 8d1158372SPeter Griffin * 9d1158372SPeter Griffin * Derived from ohci-platform.c 10d1158372SPeter Griffin * 11d1158372SPeter Griffin * This program is free software; you can redistribute it and/or modify 12d1158372SPeter Griffin * it under the terms of the GNU General Public License version 2 as 13d1158372SPeter Griffin * published by the Free Software Foundation. 14d1158372SPeter Griffin */ 15d1158372SPeter Griffin 16d1158372SPeter Griffin #include <linux/clk.h> 17d1158372SPeter Griffin #include <linux/dma-mapping.h> 18d1158372SPeter Griffin #include <linux/hrtimer.h> 19d1158372SPeter Griffin #include <linux/io.h> 20d1158372SPeter Griffin #include <linux/kernel.h> 21d1158372SPeter Griffin #include <linux/module.h> 22d1158372SPeter Griffin #include <linux/err.h> 23d1158372SPeter Griffin #include <linux/phy/phy.h> 24d1158372SPeter Griffin #include <linux/platform_device.h> 25d1158372SPeter Griffin #include <linux/reset.h> 26d1158372SPeter Griffin #include <linux/usb/ohci_pdriver.h> 27d1158372SPeter Griffin #include <linux/usb.h> 28d1158372SPeter Griffin #include <linux/usb/hcd.h> 29d1158372SPeter Griffin 30d1158372SPeter Griffin #include "ohci.h" 31d1158372SPeter Griffin 32d1158372SPeter Griffin #define USB_MAX_CLKS 3 33d1158372SPeter Griffin 34d1158372SPeter Griffin struct st_ohci_platform_priv { 35d1158372SPeter Griffin struct clk *clks[USB_MAX_CLKS]; 36d1158372SPeter Griffin struct clk *clk48; 37d1158372SPeter Griffin struct reset_control *rst; 38d1158372SPeter Griffin struct reset_control *pwr; 39d1158372SPeter Griffin struct phy *phy; 40d1158372SPeter Griffin }; 41d1158372SPeter Griffin 42d1158372SPeter Griffin #define DRIVER_DESC "OHCI STMicroelectronics driver" 43d1158372SPeter Griffin 44d1158372SPeter Griffin #define hcd_to_ohci_priv(h) \ 45d1158372SPeter Griffin ((struct st_ohci_platform_priv *)hcd_to_ohci(h)->priv) 46d1158372SPeter Griffin 47d1158372SPeter Griffin static const char hcd_name[] = "ohci-st"; 48d1158372SPeter Griffin 49d1158372SPeter Griffin static int st_ohci_platform_power_on(struct platform_device *dev) 50d1158372SPeter Griffin { 51d1158372SPeter Griffin struct usb_hcd *hcd = platform_get_drvdata(dev); 52d1158372SPeter Griffin struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd); 53d1158372SPeter Griffin int clk, ret; 54d1158372SPeter Griffin 55d1158372SPeter Griffin ret = reset_control_deassert(priv->pwr); 56d1158372SPeter Griffin if (ret) 57d1158372SPeter Griffin return ret; 58d1158372SPeter Griffin 59d1158372SPeter Griffin ret = reset_control_deassert(priv->rst); 60d1158372SPeter Griffin if (ret) 61d1158372SPeter Griffin goto err_assert_power; 62d1158372SPeter Griffin 63d1158372SPeter Griffin /* some SoCs don't have a dedicated 48Mhz clock, but those that do 64d1158372SPeter Griffin need the rate to be explicitly set */ 65d1158372SPeter Griffin if (priv->clk48) { 66d1158372SPeter Griffin ret = clk_set_rate(priv->clk48, 48000000); 67d1158372SPeter Griffin if (ret) 68d1158372SPeter Griffin goto err_assert_reset; 69d1158372SPeter Griffin } 70d1158372SPeter Griffin 71d1158372SPeter Griffin for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) { 72d1158372SPeter Griffin ret = clk_prepare_enable(priv->clks[clk]); 73d1158372SPeter Griffin if (ret) 74d1158372SPeter Griffin goto err_disable_clks; 75d1158372SPeter Griffin } 76d1158372SPeter Griffin 77d1158372SPeter Griffin ret = phy_init(priv->phy); 78d1158372SPeter Griffin if (ret) 79d1158372SPeter Griffin goto err_disable_clks; 80d1158372SPeter Griffin 81d1158372SPeter Griffin ret = phy_power_on(priv->phy); 82d1158372SPeter Griffin if (ret) 83d1158372SPeter Griffin goto err_exit_phy; 84d1158372SPeter Griffin 85d1158372SPeter Griffin return 0; 86d1158372SPeter Griffin 87d1158372SPeter Griffin err_exit_phy: 88d1158372SPeter Griffin phy_exit(priv->phy); 89d1158372SPeter Griffin err_disable_clks: 90d1158372SPeter Griffin while (--clk >= 0) 91d1158372SPeter Griffin clk_disable_unprepare(priv->clks[clk]); 92d1158372SPeter Griffin err_assert_reset: 93d1158372SPeter Griffin reset_control_assert(priv->rst); 94d1158372SPeter Griffin err_assert_power: 95d1158372SPeter Griffin reset_control_assert(priv->pwr); 96d1158372SPeter Griffin 97d1158372SPeter Griffin return ret; 98d1158372SPeter Griffin } 99d1158372SPeter Griffin 100d1158372SPeter Griffin static void st_ohci_platform_power_off(struct platform_device *dev) 101d1158372SPeter Griffin { 102d1158372SPeter Griffin struct usb_hcd *hcd = platform_get_drvdata(dev); 103d1158372SPeter Griffin struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd); 104d1158372SPeter Griffin 105d1158372SPeter Griffin int clk; 106d1158372SPeter Griffin 107d1158372SPeter Griffin reset_control_assert(priv->pwr); 108d1158372SPeter Griffin 109d1158372SPeter Griffin reset_control_assert(priv->rst); 110d1158372SPeter Griffin 111d1158372SPeter Griffin phy_power_off(priv->phy); 112d1158372SPeter Griffin 113d1158372SPeter Griffin phy_exit(priv->phy); 114d1158372SPeter Griffin 115d1158372SPeter Griffin for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--) 116d1158372SPeter Griffin if (priv->clks[clk]) 117d1158372SPeter Griffin clk_disable_unprepare(priv->clks[clk]); 118d1158372SPeter Griffin } 119d1158372SPeter Griffin 120d1158372SPeter Griffin static struct hc_driver __read_mostly ohci_platform_hc_driver; 121d1158372SPeter Griffin 122d1158372SPeter Griffin static const struct ohci_driver_overrides platform_overrides __initconst = { 123d1158372SPeter Griffin .product_desc = "ST OHCI controller", 124d1158372SPeter Griffin .extra_priv_size = sizeof(struct st_ohci_platform_priv), 125d1158372SPeter Griffin }; 126d1158372SPeter Griffin 127d1158372SPeter Griffin static struct usb_ohci_pdata ohci_platform_defaults = { 128d1158372SPeter Griffin .power_on = st_ohci_platform_power_on, 129d1158372SPeter Griffin .power_suspend = st_ohci_platform_power_off, 130d1158372SPeter Griffin .power_off = st_ohci_platform_power_off, 131d1158372SPeter Griffin }; 132d1158372SPeter Griffin 133d1158372SPeter Griffin static int st_ohci_platform_probe(struct platform_device *dev) 134d1158372SPeter Griffin { 135d1158372SPeter Griffin struct usb_hcd *hcd; 136d1158372SPeter Griffin struct resource *res_mem; 137d1158372SPeter Griffin struct usb_ohci_pdata *pdata = &ohci_platform_defaults; 138d1158372SPeter Griffin struct st_ohci_platform_priv *priv; 139d1158372SPeter Griffin struct ohci_hcd *ohci; 140d1158372SPeter Griffin int err, irq, clk = 0; 141d1158372SPeter Griffin 142d1158372SPeter Griffin if (usb_disabled()) 143d1158372SPeter Griffin return -ENODEV; 144d1158372SPeter Griffin 145d1158372SPeter Griffin irq = platform_get_irq(dev, 0); 146d1158372SPeter Griffin if (irq < 0) { 147d1158372SPeter Griffin dev_err(&dev->dev, "no irq provided"); 148d1158372SPeter Griffin return irq; 149d1158372SPeter Griffin } 150d1158372SPeter Griffin 151d1158372SPeter Griffin res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0); 152d1158372SPeter Griffin if (!res_mem) { 153d1158372SPeter Griffin dev_err(&dev->dev, "no memory resource provided"); 154d1158372SPeter Griffin return -ENXIO; 155d1158372SPeter Griffin } 156d1158372SPeter Griffin 157d1158372SPeter Griffin hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev, 158d1158372SPeter Griffin dev_name(&dev->dev)); 159d1158372SPeter Griffin if (!hcd) 160d1158372SPeter Griffin return -ENOMEM; 161d1158372SPeter Griffin 162d1158372SPeter Griffin platform_set_drvdata(dev, hcd); 163d1158372SPeter Griffin dev->dev.platform_data = pdata; 164d1158372SPeter Griffin priv = hcd_to_ohci_priv(hcd); 165d1158372SPeter Griffin ohci = hcd_to_ohci(hcd); 166d1158372SPeter Griffin 167d1158372SPeter Griffin priv->phy = devm_phy_get(&dev->dev, "usb"); 168d1158372SPeter Griffin if (IS_ERR(priv->phy)) { 169d1158372SPeter Griffin err = PTR_ERR(priv->phy); 170d1158372SPeter Griffin goto err_put_hcd; 171d1158372SPeter Griffin } 172d1158372SPeter Griffin 173d1158372SPeter Griffin for (clk = 0; clk < USB_MAX_CLKS; clk++) { 174d1158372SPeter Griffin priv->clks[clk] = of_clk_get(dev->dev.of_node, clk); 175d1158372SPeter Griffin if (IS_ERR(priv->clks[clk])) { 176d1158372SPeter Griffin err = PTR_ERR(priv->clks[clk]); 177d1158372SPeter Griffin if (err == -EPROBE_DEFER) 178d1158372SPeter Griffin goto err_put_clks; 179d1158372SPeter Griffin priv->clks[clk] = NULL; 180d1158372SPeter Griffin break; 181d1158372SPeter Griffin } 182d1158372SPeter Griffin } 183d1158372SPeter Griffin 184d1158372SPeter Griffin /* some SoCs don't have a dedicated 48Mhz clock, but those that 185d1158372SPeter Griffin do need the rate to be explicitly set */ 186d1158372SPeter Griffin priv->clk48 = devm_clk_get(&dev->dev, "clk48"); 187d1158372SPeter Griffin if (IS_ERR(priv->clk48)) { 188d1158372SPeter Griffin dev_info(&dev->dev, "48MHz clk not found\n"); 189d1158372SPeter Griffin priv->clk48 = NULL; 190d1158372SPeter Griffin } 191d1158372SPeter Griffin 192f5ee3f2bSLee Jones priv->pwr = 193f5ee3f2bSLee Jones devm_reset_control_get_optional_shared(&dev->dev, "power"); 194d1158372SPeter Griffin if (IS_ERR(priv->pwr)) { 195d1158372SPeter Griffin err = PTR_ERR(priv->pwr); 196d1158372SPeter Griffin goto err_put_clks; 197d1158372SPeter Griffin } 198d1158372SPeter Griffin 199f5ee3f2bSLee Jones priv->rst = 200f5ee3f2bSLee Jones devm_reset_control_get_optional_shared(&dev->dev, "softreset"); 201d1158372SPeter Griffin if (IS_ERR(priv->rst)) { 202d1158372SPeter Griffin err = PTR_ERR(priv->rst); 203d1158372SPeter Griffin goto err_put_clks; 204d1158372SPeter Griffin } 205d1158372SPeter Griffin 206d1158372SPeter Griffin if (pdata->power_on) { 207d1158372SPeter Griffin err = pdata->power_on(dev); 208d1158372SPeter Griffin if (err < 0) 209d1158372SPeter Griffin goto err_power; 210d1158372SPeter Griffin } 211d1158372SPeter Griffin 212d1158372SPeter Griffin hcd->rsrc_start = res_mem->start; 213d1158372SPeter Griffin hcd->rsrc_len = resource_size(res_mem); 214d1158372SPeter Griffin 215d1158372SPeter Griffin hcd->regs = devm_ioremap_resource(&dev->dev, res_mem); 216d1158372SPeter Griffin if (IS_ERR(hcd->regs)) { 217d1158372SPeter Griffin err = PTR_ERR(hcd->regs); 218d1158372SPeter Griffin goto err_power; 219d1158372SPeter Griffin } 220d1158372SPeter Griffin err = usb_add_hcd(hcd, irq, IRQF_SHARED); 221d1158372SPeter Griffin if (err) 222d1158372SPeter Griffin goto err_power; 223d1158372SPeter Griffin 224d1158372SPeter Griffin device_wakeup_enable(hcd->self.controller); 225d1158372SPeter Griffin 226d1158372SPeter Griffin platform_set_drvdata(dev, hcd); 227d1158372SPeter Griffin 228d1158372SPeter Griffin return err; 229d1158372SPeter Griffin 230d1158372SPeter Griffin err_power: 231d1158372SPeter Griffin if (pdata->power_off) 232d1158372SPeter Griffin pdata->power_off(dev); 233d1158372SPeter Griffin 234d1158372SPeter Griffin err_put_clks: 235d1158372SPeter Griffin while (--clk >= 0) 236d1158372SPeter Griffin clk_put(priv->clks[clk]); 237d1158372SPeter Griffin err_put_hcd: 238d1158372SPeter Griffin if (pdata == &ohci_platform_defaults) 239d1158372SPeter Griffin dev->dev.platform_data = NULL; 240d1158372SPeter Griffin 241d1158372SPeter Griffin usb_put_hcd(hcd); 242d1158372SPeter Griffin 243d1158372SPeter Griffin return err; 244d1158372SPeter Griffin } 245d1158372SPeter Griffin 246d1158372SPeter Griffin static int st_ohci_platform_remove(struct platform_device *dev) 247d1158372SPeter Griffin { 248d1158372SPeter Griffin struct usb_hcd *hcd = platform_get_drvdata(dev); 249d1158372SPeter Griffin struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev); 250d1158372SPeter Griffin struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd); 251d1158372SPeter Griffin int clk; 252d1158372SPeter Griffin 253d1158372SPeter Griffin usb_remove_hcd(hcd); 254d1158372SPeter Griffin 255d1158372SPeter Griffin if (pdata->power_off) 256d1158372SPeter Griffin pdata->power_off(dev); 257d1158372SPeter Griffin 258d1158372SPeter Griffin 259d1158372SPeter Griffin for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) 260d1158372SPeter Griffin clk_put(priv->clks[clk]); 261d1158372SPeter Griffin 262d1158372SPeter Griffin usb_put_hcd(hcd); 263d1158372SPeter Griffin 264d1158372SPeter Griffin if (pdata == &ohci_platform_defaults) 265d1158372SPeter Griffin dev->dev.platform_data = NULL; 266d1158372SPeter Griffin 267d1158372SPeter Griffin return 0; 268d1158372SPeter Griffin } 269d1158372SPeter Griffin 270d1158372SPeter Griffin #ifdef CONFIG_PM_SLEEP 271d1158372SPeter Griffin 272d1158372SPeter Griffin static int st_ohci_suspend(struct device *dev) 273d1158372SPeter Griffin { 274d1158372SPeter Griffin struct usb_hcd *hcd = dev_get_drvdata(dev); 275d1158372SPeter Griffin struct usb_ohci_pdata *pdata = dev->platform_data; 27620db5513SGeliang Tang struct platform_device *pdev = to_platform_device(dev); 277d1158372SPeter Griffin bool do_wakeup = device_may_wakeup(dev); 278d1158372SPeter Griffin int ret; 279d1158372SPeter Griffin 280d1158372SPeter Griffin ret = ohci_suspend(hcd, do_wakeup); 281d1158372SPeter Griffin if (ret) 282d1158372SPeter Griffin return ret; 283d1158372SPeter Griffin 284d1158372SPeter Griffin if (pdata->power_suspend) 285d1158372SPeter Griffin pdata->power_suspend(pdev); 286d1158372SPeter Griffin 287d1158372SPeter Griffin return ret; 288d1158372SPeter Griffin } 289d1158372SPeter Griffin 290d1158372SPeter Griffin static int st_ohci_resume(struct device *dev) 291d1158372SPeter Griffin { 292d1158372SPeter Griffin struct usb_hcd *hcd = dev_get_drvdata(dev); 293d1158372SPeter Griffin struct usb_ohci_pdata *pdata = dev_get_platdata(dev); 29420db5513SGeliang Tang struct platform_device *pdev = to_platform_device(dev); 295d1158372SPeter Griffin int err; 296d1158372SPeter Griffin 297d1158372SPeter Griffin if (pdata->power_on) { 298d1158372SPeter Griffin err = pdata->power_on(pdev); 299d1158372SPeter Griffin if (err < 0) 300d1158372SPeter Griffin return err; 301d1158372SPeter Griffin } 302d1158372SPeter Griffin 303d1158372SPeter Griffin ohci_resume(hcd, false); 304d1158372SPeter Griffin return 0; 305d1158372SPeter Griffin } 306d1158372SPeter Griffin 307d1158372SPeter Griffin static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume); 308d1158372SPeter Griffin 309d1158372SPeter Griffin #endif /* CONFIG_PM_SLEEP */ 310d1158372SPeter Griffin 311d1158372SPeter Griffin static const struct of_device_id st_ohci_platform_ids[] = { 312d1158372SPeter Griffin { .compatible = "st,st-ohci-300x", }, 313d1158372SPeter Griffin { /* sentinel */ } 314d1158372SPeter Griffin }; 315d1158372SPeter Griffin MODULE_DEVICE_TABLE(of, st_ohci_platform_ids); 316d1158372SPeter Griffin 317d1158372SPeter Griffin static struct platform_driver ohci_platform_driver = { 318d1158372SPeter Griffin .probe = st_ohci_platform_probe, 319d1158372SPeter Griffin .remove = st_ohci_platform_remove, 320d1158372SPeter Griffin .shutdown = usb_hcd_platform_shutdown, 321d1158372SPeter Griffin .driver = { 322d1158372SPeter Griffin .name = "st-ohci", 323d1158372SPeter Griffin #ifdef CONFIG_PM_SLEEP 324d1158372SPeter Griffin .pm = &st_ohci_pm_ops, 325d1158372SPeter Griffin #endif 326d1158372SPeter Griffin .of_match_table = st_ohci_platform_ids, 327d1158372SPeter Griffin } 328d1158372SPeter Griffin }; 329d1158372SPeter Griffin 330d1158372SPeter Griffin static int __init ohci_platform_init(void) 331d1158372SPeter Griffin { 332d1158372SPeter Griffin if (usb_disabled()) 333d1158372SPeter Griffin return -ENODEV; 334d1158372SPeter Griffin 335d1158372SPeter Griffin pr_info("%s: " DRIVER_DESC "\n", hcd_name); 336d1158372SPeter Griffin 337d1158372SPeter Griffin ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides); 338d1158372SPeter Griffin return platform_driver_register(&ohci_platform_driver); 339d1158372SPeter Griffin } 340d1158372SPeter Griffin module_init(ohci_platform_init); 341d1158372SPeter Griffin 342d1158372SPeter Griffin static void __exit ohci_platform_cleanup(void) 343d1158372SPeter Griffin { 344d1158372SPeter Griffin platform_driver_unregister(&ohci_platform_driver); 345d1158372SPeter Griffin } 346d1158372SPeter Griffin module_exit(ohci_platform_cleanup); 347d1158372SPeter Griffin 348d1158372SPeter Griffin MODULE_DESCRIPTION(DRIVER_DESC); 349d1158372SPeter Griffin MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>"); 350d1158372SPeter Griffin MODULE_LICENSE("GPL"); 351