b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) |
| 2 | /* |
| 3 | * platform.c - DesignWare HS OTG Controller platform driver |
| 4 | * |
| 5 | * Copyright (C) Matthijs Kooijman <matthijs@stdin.nl> |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions, and the following disclaimer, |
| 12 | * without modification. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. The names of the above-listed copyright holders may not be used |
| 17 | * to endorse or promote products derived from this software without |
| 18 | * specific prior written permission. |
| 19 | * |
| 20 | * ALTERNATIVELY, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") as published by the Free Software |
| 22 | * Foundation; either version 2 of the License, or (at your option) any |
| 23 | * later version. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 26 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 27 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 29 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 30 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 31 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 32 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 34 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | */ |
| 37 | |
| 38 | #include <linux/kernel.h> |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/clk.h> |
| 42 | #include <linux/device.h> |
| 43 | #include <linux/dma-mapping.h> |
| 44 | #include <linux/of_device.h> |
| 45 | #include <linux/mutex.h> |
| 46 | #include <linux/platform_device.h> |
| 47 | #include <linux/phy/phy.h> |
| 48 | #include <linux/platform_data/s3c-hsotg.h> |
| 49 | #include <linux/reset.h> |
| 50 | |
| 51 | #include <linux/usb/of.h> |
| 52 | #include <soc/asr/regs-addr.h> |
| 53 | #include <linux/platform_data/mv_usb.h> |
| 54 | #include <linux/usb/mv_usb2_phy.h> |
| 55 | |
| 56 | #include "core.h" |
| 57 | #include "hcd.h" |
| 58 | #include "debug.h" |
| 59 | |
| 60 | static const char dwc2_driver_name[] = "dwc2"; |
| 61 | |
| 62 | #define USB_HANDLE_TIME_MSEC (5000) |
| 63 | |
| 64 | #define APMU_USB_WAKE_CLR (0x07c) |
| 65 | |
| 66 | #define USB_VBUS_WAKE_EN (0x1 << 11) |
| 67 | #define USB_ID_WAKE_EN (0x1 << 22) |
| 68 | #define USB_LINEST_WAKE_EN ((0x1 << 9) | (0x1 << 10)) |
| 69 | |
| 70 | #define USB_VBUS_WAKE_CLR (0x1 << 4) |
| 71 | #define USB_ID_WAKE_CLR (0x1 << 23) |
| 72 | #define USB_LINEST_WAKE_CLR (0x1 << 7) |
| 73 | extern void dwc2_release_pm_qos(void); |
| 74 | extern void dwc2_acquire_pm_qos(void); |
| 75 | |
| 76 | static irqreturn_t vbus_irq(int irq, void *dev) |
| 77 | { |
| 78 | struct dwc2_hsotg *hsotg_dev = (struct dwc2_hsotg *)dev; |
| 79 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 80 | |
| 81 | /* wait 50ms for vbus to be stable */ |
| 82 | msleep(50); |
| 83 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 84 | | (USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR |
| 85 | | USB_ID_WAKE_CLR), |
| 86 | apmu_base + APMU_USB_WAKE_CLR); |
| 87 | |
| 88 | pm_wakeup_event(hsotg_dev->dev, USB_HANDLE_TIME_MSEC); |
| 89 | pxa_usb_notify(PXA_USB_DEV_OTG, EVENT_VBUS, 0); |
| 90 | dev_info(hsotg_dev->dev, "asr-usb vbus interrupt is served..\n"); |
| 91 | return IRQ_HANDLED; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Check the dr_mode against the module configuration and hardware |
| 96 | * capabilities. |
| 97 | * |
| 98 | * The hardware, module, and dr_mode, can each be set to host, device, |
| 99 | * or otg. Check that all these values are compatible and adjust the |
| 100 | * value of dr_mode if possible. |
| 101 | * |
| 102 | * actual |
| 103 | * HW MOD dr_mode dr_mode |
| 104 | * ------------------------------ |
| 105 | * HST HST any : HST |
| 106 | * HST DEV any : --- |
| 107 | * HST OTG any : HST |
| 108 | * |
| 109 | * DEV HST any : --- |
| 110 | * DEV DEV any : DEV |
| 111 | * DEV OTG any : DEV |
| 112 | * |
| 113 | * OTG HST any : HST |
| 114 | * OTG DEV any : DEV |
| 115 | * OTG OTG any : dr_mode |
| 116 | */ |
| 117 | static int dwc2_get_dr_mode(struct dwc2_hsotg *hsotg) |
| 118 | { |
| 119 | enum usb_dr_mode mode; |
| 120 | |
| 121 | hsotg->dr_mode = usb_get_dr_mode(hsotg->dev); |
| 122 | if (hsotg->dr_mode == USB_DR_MODE_UNKNOWN) |
| 123 | hsotg->dr_mode = USB_DR_MODE_OTG; |
| 124 | |
| 125 | mode = hsotg->dr_mode; |
| 126 | |
| 127 | if (dwc2_hw_is_device(hsotg)) { |
| 128 | if (IS_ENABLED(CONFIG_USB_DWC2_HOST)) { |
| 129 | dev_err(hsotg->dev, |
| 130 | "Controller does not support host mode.\n"); |
| 131 | return -EINVAL; |
| 132 | } |
| 133 | mode = USB_DR_MODE_PERIPHERAL; |
| 134 | } else if (dwc2_hw_is_host(hsotg)) { |
| 135 | if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL)) { |
| 136 | dev_err(hsotg->dev, |
| 137 | "Controller does not support device mode.\n"); |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | mode = USB_DR_MODE_HOST; |
| 141 | } else { |
| 142 | if (IS_ENABLED(CONFIG_USB_DWC2_HOST)) |
| 143 | mode = USB_DR_MODE_HOST; |
| 144 | else if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL)) |
| 145 | mode = USB_DR_MODE_PERIPHERAL; |
| 146 | } |
| 147 | |
| 148 | if (mode != hsotg->dr_mode) { |
| 149 | dev_warn(hsotg->dev, |
| 150 | "Configuration mismatch. dr_mode forced to %s\n", |
| 151 | mode == USB_DR_MODE_HOST ? "host" : "device"); |
| 152 | |
| 153 | hsotg->dr_mode = mode; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg) |
| 160 | { |
| 161 | struct platform_device *pdev = to_platform_device(hsotg->dev); |
| 162 | int ret; |
| 163 | |
| 164 | ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies), |
| 165 | hsotg->supplies); |
| 166 | if (ret) |
| 167 | return ret; |
| 168 | |
| 169 | if (hsotg->clk) { |
| 170 | ret = clk_prepare_enable(hsotg->clk); |
| 171 | if (ret) |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | if (hsotg->uphy) { |
| 176 | ret = usb_phy_init(hsotg->uphy); |
| 177 | } else if (hsotg->plat && hsotg->plat->phy_init) { |
| 178 | ret = hsotg->plat->phy_init(pdev, hsotg->plat->phy_type); |
| 179 | } else { |
| 180 | ret = phy_init(hsotg->phy); |
| 181 | if (ret == 0) |
| 182 | ret = phy_power_on(hsotg->phy); |
| 183 | } |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * dwc2_lowlevel_hw_enable - enable platform lowlevel hw resources |
| 190 | * @hsotg: The driver state |
| 191 | * |
| 192 | * A wrapper for platform code responsible for controlling |
| 193 | * low-level USB platform resources (phy, clock, regulators) |
| 194 | */ |
| 195 | int dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg) |
| 196 | { |
| 197 | int ret = __dwc2_lowlevel_hw_enable(hsotg); |
| 198 | |
| 199 | if (ret == 0) |
| 200 | hsotg->ll_hw_enabled = true; |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static int __dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg) |
| 205 | { |
| 206 | struct platform_device *pdev = to_platform_device(hsotg->dev); |
| 207 | int ret = 0; |
| 208 | |
| 209 | #ifdef CONFIG_CPU_ASR18XX |
| 210 | return 0; |
| 211 | #endif |
| 212 | |
| 213 | if (hsotg->uphy) { |
| 214 | usb_phy_shutdown(hsotg->uphy); |
| 215 | } else if (hsotg->plat && hsotg->plat->phy_exit) { |
| 216 | ret = hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type); |
| 217 | } else { |
| 218 | ret = phy_power_off(hsotg->phy); |
| 219 | if (ret == 0) |
| 220 | ret = phy_exit(hsotg->phy); |
| 221 | } |
| 222 | if (ret) |
| 223 | return ret; |
| 224 | |
| 225 | if (hsotg->clk) |
| 226 | clk_disable_unprepare(hsotg->clk); |
| 227 | |
| 228 | ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), |
| 229 | hsotg->supplies); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * dwc2_lowlevel_hw_disable - disable platform lowlevel hw resources |
| 236 | * @hsotg: The driver state |
| 237 | * |
| 238 | * A wrapper for platform code responsible for controlling |
| 239 | * low-level USB platform resources (phy, clock, regulators) |
| 240 | */ |
| 241 | int dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg) |
| 242 | { |
| 243 | int ret = __dwc2_lowlevel_hw_disable(hsotg); |
| 244 | |
| 245 | if (ret == 0) |
| 246 | hsotg->ll_hw_enabled = false; |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static int dwc2_lowlevel_hw_init(struct dwc2_hsotg *hsotg) |
| 251 | { |
| 252 | int i, ret; |
| 253 | |
| 254 | hsotg->reset = devm_reset_control_get_optional(hsotg->dev, "dwc2"); |
| 255 | if (IS_ERR(hsotg->reset)) { |
| 256 | ret = PTR_ERR(hsotg->reset); |
| 257 | dev_err(hsotg->dev, "error getting reset control %d\n", ret); |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | reset_control_deassert(hsotg->reset); |
| 262 | |
| 263 | hsotg->reset_ecc = devm_reset_control_get_optional(hsotg->dev, "dwc2-ecc"); |
| 264 | if (IS_ERR(hsotg->reset_ecc)) { |
| 265 | ret = PTR_ERR(hsotg->reset_ecc); |
| 266 | dev_err(hsotg->dev, "error getting reset control for ecc %d\n", ret); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | reset_control_deassert(hsotg->reset_ecc); |
| 271 | |
| 272 | /* |
| 273 | * Attempt to find a generic PHY, then look for an old style |
| 274 | * USB PHY and then fall back to pdata |
| 275 | */ |
| 276 | hsotg->phy = devm_phy_get(hsotg->dev, "usb2-phy"); |
| 277 | if (IS_ERR(hsotg->phy)) { |
| 278 | ret = PTR_ERR(hsotg->phy); |
| 279 | switch (ret) { |
| 280 | case -ENODEV: |
| 281 | case -ENOSYS: |
| 282 | hsotg->phy = NULL; |
| 283 | break; |
| 284 | case -EPROBE_DEFER: |
| 285 | return ret; |
| 286 | default: |
| 287 | dev_err(hsotg->dev, "error getting phy %d\n", ret); |
| 288 | return ret; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if (!hsotg->phy) { |
| 293 | hsotg->uphy = devm_usb_get_phy(hsotg->dev, USB_PHY_TYPE_USB2); |
| 294 | if (IS_ERR(hsotg->uphy)) { |
| 295 | ret = PTR_ERR(hsotg->uphy); |
| 296 | switch (ret) { |
| 297 | case -ENODEV: |
| 298 | case -ENXIO: |
| 299 | hsotg->uphy = NULL; |
| 300 | break; |
| 301 | case -EPROBE_DEFER: |
| 302 | return ret; |
| 303 | default: |
| 304 | dev_err(hsotg->dev, "error getting usb phy %d\n", |
| 305 | ret); |
| 306 | return ret; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | hsotg->plat = dev_get_platdata(hsotg->dev); |
| 312 | |
| 313 | /* Clock */ |
| 314 | hsotg->clk = devm_clk_get_optional(hsotg->dev, "otg"); |
| 315 | if (IS_ERR(hsotg->clk)) { |
| 316 | dev_err(hsotg->dev, "cannot get otg clock\n"); |
| 317 | return PTR_ERR(hsotg->clk); |
| 318 | } |
| 319 | |
| 320 | /* Regulators */ |
| 321 | for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++) |
| 322 | hsotg->supplies[i].supply = dwc2_hsotg_supply_names[i]; |
| 323 | |
| 324 | ret = devm_regulator_bulk_get(hsotg->dev, ARRAY_SIZE(hsotg->supplies), |
| 325 | hsotg->supplies); |
| 326 | if (ret) { |
| 327 | dev_err(hsotg->dev, "failed to request supplies: %d\n", ret); |
| 328 | return ret; |
| 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * dwc2_driver_remove() - Called when the DWC_otg core is unregistered with the |
| 335 | * DWC_otg driver |
| 336 | * |
| 337 | * @dev: Platform device |
| 338 | * |
| 339 | * This routine is called, for example, when the rmmod command is executed. The |
| 340 | * device may or may not be electrically present. If it is present, the driver |
| 341 | * stops device processing. Any resources used on behalf of this device are |
| 342 | * freed. |
| 343 | */ |
| 344 | static int dwc2_driver_remove(struct platform_device *dev) |
| 345 | { |
| 346 | struct dwc2_hsotg *hsotg = platform_get_drvdata(dev); |
| 347 | |
| 348 | dwc2_debugfs_exit(hsotg); |
| 349 | if (hsotg->hcd_enabled) |
| 350 | dwc2_hcd_remove(hsotg); |
| 351 | if (hsotg->gadget_enabled) |
| 352 | dwc2_hsotg_remove(hsotg); |
| 353 | |
| 354 | if (hsotg->ll_hw_enabled) |
| 355 | dwc2_lowlevel_hw_disable(hsotg); |
| 356 | |
| 357 | reset_control_assert(hsotg->reset); |
| 358 | reset_control_assert(hsotg->reset_ecc); |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * dwc2_driver_shutdown() - Called on device shutdown |
| 365 | * |
| 366 | * @dev: Platform device |
| 367 | * |
| 368 | * In specific conditions (involving usb hubs) dwc2 devices can create a |
| 369 | * lot of interrupts, even to the point of overwhelming devices running |
| 370 | * at low frequencies. Some devices need to do special clock handling |
| 371 | * at shutdown-time which may bring the system clock below the threshold |
| 372 | * of being able to handle the dwc2 interrupts. Disabling dwc2-irqs |
| 373 | * prevents reboots/poweroffs from getting stuck in such cases. |
| 374 | */ |
| 375 | static void dwc2_driver_shutdown(struct platform_device *dev) |
| 376 | { |
| 377 | struct dwc2_hsotg *hsotg = platform_get_drvdata(dev); |
| 378 | |
| 379 | dwc2_disable_global_interrupts(hsotg); |
| 380 | synchronize_irq(hsotg->irq); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * dwc2_check_core_endianness() - Returns true if core and AHB have |
| 385 | * opposite endianness. |
| 386 | * @hsotg: Programming view of the DWC_otg controller. |
| 387 | */ |
| 388 | static bool dwc2_check_core_endianness(struct dwc2_hsotg *hsotg) |
| 389 | { |
| 390 | u32 snpsid; |
| 391 | |
| 392 | snpsid = ioread32(hsotg->regs + GSNPSID); |
| 393 | if ((snpsid & GSNPSID_ID_MASK) == DWC2_OTG_ID || |
| 394 | (snpsid & GSNPSID_ID_MASK) == DWC2_FS_IOT_ID || |
| 395 | (snpsid & GSNPSID_ID_MASK) == DWC2_HS_IOT_ID) |
| 396 | return false; |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * dwc2_driver_probe() - Called when the DWC_otg core is bound to the DWC_otg |
| 402 | * driver |
| 403 | * |
| 404 | * @dev: Platform device |
| 405 | * |
| 406 | * This routine creates the driver components required to control the device |
| 407 | * (core, HCD, and PCD) and initializes the device. The driver components are |
| 408 | * stored in a dwc2_hsotg structure. A reference to the dwc2_hsotg is saved |
| 409 | * in the device private data. This allows the driver to access the dwc2_hsotg |
| 410 | * structure on subsequent calls to driver methods for this device. |
| 411 | */ |
| 412 | static int dwc2_driver_probe(struct platform_device *dev) |
| 413 | { |
| 414 | struct dwc2_hsotg *hsotg; |
| 415 | struct resource *res; |
| 416 | int retval; |
| 417 | void __iomem *apmu_base; |
| 418 | |
| 419 | hsotg = devm_kzalloc(&dev->dev, sizeof(*hsotg), GFP_KERNEL); |
| 420 | if (!hsotg) |
| 421 | return -ENOMEM; |
| 422 | |
| 423 | hsotg->dev = &dev->dev; |
| 424 | |
| 425 | /* |
| 426 | * Use reasonable defaults so platforms don't have to provide these. |
| 427 | */ |
| 428 | if (!dev->dev.dma_mask) |
| 429 | dev->dev.dma_mask = &dev->dev.coherent_dma_mask; |
| 430 | retval = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32)); |
| 431 | if (retval) { |
| 432 | dev_err(&dev->dev, "can't set coherent DMA mask: %d\n", retval); |
| 433 | return retval; |
| 434 | } |
| 435 | |
| 436 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 437 | hsotg->regs = devm_ioremap_resource(&dev->dev, res); |
| 438 | if (IS_ERR(hsotg->regs)) |
| 439 | return PTR_ERR(hsotg->regs); |
| 440 | |
| 441 | dev_dbg(&dev->dev, "mapped PA %08lx to VA %p\n", |
| 442 | (unsigned long)res->start, hsotg->regs); |
| 443 | |
| 444 | retval = dwc2_lowlevel_hw_init(hsotg); |
| 445 | if (retval) |
| 446 | return retval; |
| 447 | |
| 448 | spin_lock_init(&hsotg->lock); |
| 449 | |
| 450 | hsotg->vbus_supply = devm_regulator_get_optional(hsotg->dev, "vbus"); |
| 451 | if (IS_ERR(hsotg->vbus_supply)) { |
| 452 | retval = PTR_ERR(hsotg->vbus_supply); |
| 453 | hsotg->vbus_supply = NULL; |
| 454 | if (retval != -ENODEV) |
| 455 | return retval; |
| 456 | } |
| 457 | |
| 458 | retval = dwc2_lowlevel_hw_enable(hsotg); |
| 459 | if (retval) |
| 460 | return retval; |
| 461 | |
| 462 | hsotg->needs_byte_swap = dwc2_check_core_endianness(hsotg); |
| 463 | |
| 464 | retval = dwc2_get_dr_mode(hsotg); |
| 465 | if (retval) |
| 466 | goto error; |
| 467 | |
| 468 | hsotg->need_phy_for_wake = |
| 469 | of_property_read_bool(dev->dev.of_node, |
| 470 | "snps,need-phy-for-wake"); |
| 471 | hsotg->no_acchg_det = |
| 472 | of_property_read_bool(dev->dev.of_node, |
| 473 | "snps,no-acchg-det"); |
| 474 | |
| 475 | /* |
| 476 | * Reset before dwc2_get_hwparams() then it could get power-on real |
| 477 | * reset value form registers. |
| 478 | */ |
| 479 | retval = dwc2_core_reset(hsotg, false); |
| 480 | if (retval) |
| 481 | goto error; |
| 482 | |
| 483 | /* Detect config values from hardware */ |
| 484 | retval = dwc2_get_hwparams(hsotg); |
| 485 | if (retval) |
| 486 | goto error; |
| 487 | |
| 488 | hsotg->irq = platform_get_irq(dev, 0); |
| 489 | if (hsotg->irq < 0) { |
| 490 | retval = hsotg->irq; |
| 491 | goto error; |
| 492 | } |
| 493 | |
| 494 | dev_dbg(hsotg->dev, "registering common handler for irq%d\n", |
| 495 | hsotg->irq); |
| 496 | retval = devm_request_irq(hsotg->dev, hsotg->irq, |
| 497 | dwc2_handle_common_intr, IRQF_SHARED, |
| 498 | dev_name(hsotg->dev), hsotg); |
| 499 | if (retval) |
| 500 | goto error; |
| 501 | |
| 502 | /* |
| 503 | * For OTG cores, set the force mode bits to reflect the value |
| 504 | * of dr_mode. Force mode bits should not be touched at any |
| 505 | * other time after this. |
| 506 | */ |
| 507 | dwc2_force_dr_mode(hsotg); |
| 508 | |
| 509 | retval = dwc2_init_params(hsotg); |
| 510 | if (retval) |
| 511 | goto error; |
| 512 | |
| 513 | if (hsotg->dr_mode != USB_DR_MODE_HOST) { |
| 514 | retval = dwc2_gadget_init(hsotg); |
| 515 | if (retval) |
| 516 | goto error; |
| 517 | hsotg->gadget_enabled = 1; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * If we need PHY for wakeup we must be wakeup capable. |
| 522 | * When we have a device that can wake without the PHY we |
| 523 | * can adjust this condition. |
| 524 | */ |
| 525 | if (hsotg->need_phy_for_wake) |
| 526 | device_set_wakeup_capable(&dev->dev, true); |
| 527 | |
| 528 | hsotg->reset_phy_on_wake = |
| 529 | of_property_read_bool(dev->dev.of_node, |
| 530 | "snps,reset-phy-on-wake"); |
| 531 | if (hsotg->reset_phy_on_wake && !hsotg->phy) { |
| 532 | dev_warn(hsotg->dev, |
| 533 | "Quirk reset-phy-on-wake only supports generic PHYs\n"); |
| 534 | hsotg->reset_phy_on_wake = false; |
| 535 | } |
| 536 | |
| 537 | if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) { |
| 538 | retval = dwc2_hcd_init(hsotg); |
| 539 | if (retval) { |
| 540 | if (hsotg->gadget_enabled) |
| 541 | dwc2_hsotg_remove(hsotg); |
| 542 | goto error; |
| 543 | } |
| 544 | hsotg->hcd_enabled = 1; |
| 545 | } |
| 546 | |
| 547 | platform_set_drvdata(dev, hsotg); |
| 548 | hsotg->hibernated = 0; |
| 549 | |
| 550 | dwc2_debugfs_init(hsotg); |
| 551 | |
| 552 | /* Gadget code manages lowlevel hw on its own */ |
| 553 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 554 | dwc2_lowlevel_hw_disable(hsotg); |
| 555 | |
| 556 | #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \ |
| 557 | IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE) |
| 558 | /* Postponed adding a new gadget to the udc class driver list */ |
| 559 | if (hsotg->gadget_enabled) { |
| 560 | retval = usb_add_gadget_udc(hsotg->dev, &hsotg->gadget); |
| 561 | if (retval) { |
| 562 | hsotg->gadget.udc = NULL; |
| 563 | dwc2_hsotg_remove(hsotg); |
| 564 | goto error; |
| 565 | } |
| 566 | } |
| 567 | #endif /* CONFIG_USB_DWC2_PERIPHERAL || CONFIG_USB_DWC2_DUAL_ROLE */ |
| 568 | |
| 569 | hsotg->vbus_irq = platform_get_irq(dev, 1); |
| 570 | if (hsotg->vbus_irq < 0) { |
| 571 | dev_err(&dev->dev, "failed to get vbus irq\n"); |
| 572 | retval = -ENXIO; |
| 573 | goto error; |
| 574 | } |
| 575 | |
| 576 | retval = devm_request_threaded_irq(&dev->dev, hsotg->vbus_irq, |
| 577 | NULL, vbus_irq, |
| 578 | IRQF_ONESHOT | IRQF_NO_SUSPEND, |
| 579 | "asr-usb-vbus", hsotg); |
| 580 | if (retval) { |
| 581 | dev_info(&dev->dev, |
| 582 | "Can not request irq for VBUS\n"); |
| 583 | goto error; |
| 584 | } |
| 585 | apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 586 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) | USB_VBUS_WAKE_EN, |
| 587 | apmu_base + APMU_USB_WAKE_CLR); |
| 588 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 589 | | (USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR | USB_ID_WAKE_CLR), |
| 590 | apmu_base + APMU_USB_WAKE_CLR); |
| 591 | |
| 592 | device_init_wakeup(&dev->dev, 1); |
| 593 | pm_stay_awake(&dev->dev); |
| 594 | dev_info(hsotg->dev, "probe done\n"); |
| 595 | |
| 596 | return 0; |
| 597 | |
| 598 | error: |
| 599 | if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) |
| 600 | dwc2_lowlevel_hw_disable(hsotg); |
| 601 | return retval; |
| 602 | } |
| 603 | |
| 604 | static int __maybe_unused dwc2_suspend(struct device *dev) |
| 605 | { |
| 606 | struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev); |
| 607 | bool is_device_mode = dwc2_is_device_mode(dwc2); |
| 608 | int ret = 0; |
| 609 | |
| 610 | /* asr private */ |
| 611 | return 0; |
| 612 | |
| 613 | if (is_device_mode) |
| 614 | dwc2_hsotg_suspend(dwc2); |
| 615 | |
| 616 | if (dwc2->ll_hw_enabled && |
| 617 | (is_device_mode || dwc2_host_can_poweroff_phy(dwc2))) { |
| 618 | ret = __dwc2_lowlevel_hw_disable(dwc2); |
| 619 | dwc2->phy_off_for_suspend = true; |
| 620 | } |
| 621 | |
| 622 | return ret; |
| 623 | } |
| 624 | |
| 625 | static int __maybe_unused dwc2_resume(struct device *dev) |
| 626 | { |
| 627 | struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev); |
| 628 | int ret = 0; |
| 629 | |
| 630 | /* asr private */ |
| 631 | return 0; |
| 632 | |
| 633 | if (dwc2->phy_off_for_suspend && dwc2->ll_hw_enabled) { |
| 634 | ret = __dwc2_lowlevel_hw_enable(dwc2); |
| 635 | if (ret) |
| 636 | return ret; |
| 637 | } |
| 638 | dwc2->phy_off_for_suspend = false; |
| 639 | |
| 640 | if (dwc2_is_device_mode(dwc2)) |
| 641 | ret = dwc2_hsotg_resume(dwc2); |
| 642 | |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | static int dwc2_noirq_suspend(struct device *dev) |
| 647 | { |
| 648 | struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev); |
| 649 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 650 | |
| 651 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 652 | | (USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR | USB_ID_WAKE_CLR |
| 653 | | USB_VBUS_WAKE_EN | USB_LINEST_WAKE_EN | USB_ID_WAKE_EN), |
| 654 | apmu_base + APMU_USB_WAKE_CLR); |
| 655 | |
| 656 | if (dwc2->allow_suspend) { |
| 657 | if (dwc2->lx_state == DWC2_L2) |
| 658 | usb_phy_set_suspend2(dwc2->uphy, 1); |
| 659 | else |
| 660 | pr_info("dwc2 lx_state: %d\n", dwc2->lx_state); |
| 661 | } |
| 662 | |
| 663 | enable_irq_wake(dwc2->vbus_irq); |
| 664 | |
| 665 | dwc2_release_pm_qos(); |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static int dwc2_noirq_resume(struct device *dev) |
| 670 | { |
| 671 | volatile u32 value; |
| 672 | struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev); |
| 673 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 674 | |
| 675 | if (dwc2->vbus_active) |
| 676 | dwc2_acquire_pm_qos(); |
| 677 | |
| 678 | disable_irq_wake(dwc2->vbus_irq); |
| 679 | if (dwc2->allow_suspend) { |
| 680 | if (dwc2->vbus_active) { |
| 681 | usb_phy_set_suspend2(dwc2->uphy, 0); |
| 682 | } else { |
| 683 | pr_info("dwc2 vbus off, lx_state: %d\n", dwc2->lx_state); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /* clear linestat wakeup and disable linestat/pmu wake en */ |
| 688 | value = readl(apmu_base + APMU_USB_WAKE_CLR); |
| 689 | value |= (USB_LINEST_WAKE_CLR); |
| 690 | writel(value, apmu_base + APMU_USB_WAKE_CLR); |
| 691 | udelay(50); |
| 692 | value = readl(apmu_base + APMU_USB_WAKE_CLR); |
| 693 | value &= ~(USB_LINEST_WAKE_EN); |
| 694 | writel(value, apmu_base + APMU_USB_WAKE_CLR); |
| 695 | |
| 696 | dev_info(dwc2->dev, "dwc2 resume\n"); |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static const struct dev_pm_ops dwc2_dev_pm_ops = { |
| 701 | SET_SYSTEM_SLEEP_PM_OPS(dwc2_suspend, dwc2_resume) |
| 702 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dwc2_noirq_suspend, dwc2_noirq_resume) |
| 703 | }; |
| 704 | |
| 705 | static struct platform_driver dwc2_platform_driver = { |
| 706 | .driver = { |
| 707 | .name = dwc2_driver_name, |
| 708 | .of_match_table = dwc2_of_match_table, |
| 709 | .pm = &dwc2_dev_pm_ops, |
| 710 | }, |
| 711 | .probe = dwc2_driver_probe, |
| 712 | .remove = dwc2_driver_remove, |
| 713 | .shutdown = dwc2_driver_shutdown, |
| 714 | }; |
| 715 | |
| 716 | module_platform_driver(dwc2_platform_driver); |
| 717 | |
| 718 | MODULE_DESCRIPTION("DESIGNWARE HS OTG Platform Glue"); |
| 719 | MODULE_AUTHOR("Matthijs Kooijman <matthijs@stdin.nl>"); |
| 720 | MODULE_LICENSE("Dual BSD/GPL"); |