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