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 | #include <linux/usb.h> |
| 56 | #include <linux/usb/ch9.h> |
| 57 | #include <linux/usb/otg.h> |
| 58 | #include <linux/usb/gadget.h> |
| 59 | #include <linux/usb/hcd.h> |
| 60 | #include <linux/pm_qos.h> |
| 61 | #include <linux/gpio.h> |
| 62 | #include <linux/edge_wakeup_mmp.h> |
| 63 | |
| 64 | #include "core.h" |
| 65 | #include "hcd.h" |
| 66 | #include "debug.h" |
| 67 | |
| 68 | static const char dwc2_driver_name[] = "dwc2"; |
| 69 | |
| 70 | #define USB_HANDLE_TIME_MSEC (5000) |
| 71 | |
| 72 | #define APMU_USB_WAKE_CLR (0x07c) |
| 73 | |
| 74 | #define USB_VBUS_WAKE_EN (0x1 << 11) |
| 75 | #define USB_ID_WAKE_EN (0x1 << 22) |
| 76 | #define USB_LINEST_WAKE_EN ((0x1 << 9) | (0x1 << 10)) |
| 77 | |
| 78 | #define USB_VBUS_WAKE_CLR (0x1 << 4) |
| 79 | #define USB_ID_WAKE_CLR (0x1 << 23) |
| 80 | #define USB_LINEST_WAKE_CLR (0x1 << 7) |
| 81 | |
| 82 | #define ENNUM -1 |
| 83 | #define DWC2_MAX_HOST_CFG (16) |
| 84 | #define DWC2_MAX_DEVICE_CFG (64) |
| 85 | |
| 86 | #define APMU_USB_CLK_CTL (0x05C) /* fact */ |
| 87 | #define VBUS_RISE_FALL_MS (10) |
| 88 | |
| 89 | struct dwc2_reg_val { |
| 90 | u32 val; |
| 91 | u32 reg; |
| 92 | }; |
| 93 | |
| 94 | extern void dwc2_release_pm_qos(void); |
| 95 | extern void dwc2_acquire_pm_qos(void); |
| 96 | |
| 97 | static u32 force_host = 0; |
| 98 | static u32 force_dev = 0; |
| 99 | static bool usb_host_vbus_on; |
| 100 | static struct dwc2_hsotg *g_hsotg; |
| 101 | |
| 102 | module_param(force_dev, uint, S_IRUGO|S_IWUSR); |
| 103 | MODULE_PARM_DESC(force_dev, "dwc2 otg force device mode"); |
| 104 | |
| 105 | module_param(force_host, uint, S_IRUGO|S_IWUSR); |
| 106 | MODULE_PARM_DESC(force_host, "dwc2 otg force host mode"); |
| 107 | |
| 108 | struct dwc2_reg_val dwc2_host_global_cfg[] = { |
| 109 | {.val = 0x00000000, .reg = 0x00000008}, |
| 110 | {.val = 0x20001400, .reg = 0x0000000c}, |
| 111 | {.val = 0x20001400, .reg = 0x0000000c}, |
| 112 | {.val = 0x00000026, .reg = 0x00000008}, |
| 113 | {.val = 0x20001400, .reg = 0x0000000c}, |
| 114 | {.val = 0x00280000, .reg = 0x00000000}, |
| 115 | {.val = 0xffffffff, .reg = 0x00000004}, |
| 116 | {.val = 0xffffffff, .reg = 0x00000014}, |
| 117 | {.val = 0xd0000806, .reg = 0x00000018}, |
| 118 | {.val = 0xF3000806, .reg = 0x00000018}, |
| 119 | }; |
| 120 | |
| 121 | struct dwc2_reg_val dwc2_device_global_cfg[] = { |
| 122 | {.val = 0x40001400, .reg = 0x0000000c}, |
| 123 | {.val = 0x00000800, .reg = 0x00000900}, |
| 124 | {.val = 0x00000800, .reg = 0x00000b00}, |
| 125 | {.val = 0x00001000, .reg = 0x00000920}, |
| 126 | {.val = 0x00001000, .reg = 0x00000b20}, |
| 127 | {.val = 0x00001800, .reg = 0x00000940}, |
| 128 | {.val = 0x00001800, .reg = 0x00000b40}, |
| 129 | {.val = 0x00002000, .reg = 0x00000960}, |
| 130 | {.val = 0x00002000, .reg = 0x00000b60}, |
| 131 | {.val = 0x00002800, .reg = 0x00000980}, |
| 132 | {.val = 0x00002800, .reg = 0x00000b80}, |
| 133 | {.val = 0x00003000, .reg = 0x000009a0}, |
| 134 | {.val = 0x00003000, .reg = 0x00000ba0}, |
| 135 | {.val = 0x00003800, .reg = 0x000009c0}, |
| 136 | {.val = 0x00003800, .reg = 0x00000bc0}, |
| 137 | {.val = 0x00004000, .reg = 0x000009e0}, |
| 138 | {.val = 0x00004000, .reg = 0x00000be0}, |
| 139 | {.val = 0x00004800, .reg = 0x00000a00}, |
| 140 | {.val = 0x00004800, .reg = 0x00000c00}, |
| 141 | {.val = 0x00005000, .reg = 0x00000a20}, |
| 142 | {.val = 0x00005000, .reg = 0x00000c20}, |
| 143 | {.val = 0x00005800, .reg = 0x00000a40}, |
| 144 | {.val = 0x00005800, .reg = 0x00000c40}, |
| 145 | {.val = 0x00006000, .reg = 0x00000a60}, |
| 146 | {.val = 0x00006000, .reg = 0x00000c60}, |
| 147 | {.val = 0x00006800, .reg = 0x00000a80}, |
| 148 | {.val = 0x00006800, .reg = 0x00000c80}, |
| 149 | {.val = 0x00007000, .reg = 0x00000aa0}, |
| 150 | {.val = 0x00007000, .reg = 0x00000ca0}, |
| 151 | {.val = 0x00000000, .reg = 0x00000ac0}, |
| 152 | {.val = 0x00000000, .reg = 0x00000cc0}, |
| 153 | {.val = 0x00000800, .reg = 0x00000ae0}, |
| 154 | {.val = 0x00000800, .reg = 0x00000ce0}, |
| 155 | }; |
| 156 | |
| 157 | bool is_otg_host_vbus_on(void) |
| 158 | { |
| 159 | return usb_host_vbus_on; |
| 160 | } |
| 161 | |
| 162 | int usb_otg_set_vbus(struct dwc2_hsotg *hsotg, bool on) |
| 163 | { |
| 164 | int ret = 0; |
| 165 | |
| 166 | usb_host_vbus_on = on; |
| 167 | if (hsotg->gpio_num >= 0) |
| 168 | ret = gpio_direction_output(hsotg->gpio_num, on); |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static void __dwc2_wait_for_mode(struct dwc2_hsotg *hsotg, |
| 173 | bool host_mode) |
| 174 | { |
| 175 | ktime_t start; |
| 176 | ktime_t end; |
| 177 | unsigned int timeout = 110; |
| 178 | |
| 179 | dev_vdbg(hsotg->dev, "Waiting for %s mode\n", |
| 180 | host_mode ? "host" : "device"); |
| 181 | |
| 182 | start = ktime_get(); |
| 183 | |
| 184 | while (1) { |
| 185 | s64 ms; |
| 186 | |
| 187 | if (dwc2_is_host_mode(hsotg) == host_mode) { |
| 188 | dev_info(hsotg->dev, "%s mode set\n", |
| 189 | host_mode ? "Host" : "Device"); |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | end = ktime_get(); |
| 194 | ms = ktime_to_ms(ktime_sub(end, start)); |
| 195 | |
| 196 | if (ms >= (s64)timeout) { |
| 197 | dev_warn(hsotg->dev, "!!!!%s: Couldn't set %s mode\n", |
| 198 | __func__, host_mode ? "host" : "device"); |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | usleep_range(1000, 2000); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static void _dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host) |
| 207 | { |
| 208 | u32 gusbcfg; |
| 209 | u32 set; |
| 210 | u32 clear; |
| 211 | |
| 212 | dev_info(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device"); |
| 213 | |
| 214 | gusbcfg = dwc2_readl(hsotg, GUSBCFG); |
| 215 | |
| 216 | set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE; |
| 217 | clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE; |
| 218 | |
| 219 | gusbcfg &= ~clear; |
| 220 | gusbcfg |= set; |
| 221 | dwc2_writel(hsotg, gusbcfg, GUSBCFG); |
| 222 | |
| 223 | __dwc2_wait_for_mode(hsotg, host); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | static void __maybe_unused dwc2_force_host_mode(struct dwc2_hsotg *hsotg) |
| 228 | { |
| 229 | _dwc2_force_mode(hsotg, true); |
| 230 | } |
| 231 | |
| 232 | static void dwc2_force_device_mode(struct dwc2_hsotg *hsotg) |
| 233 | { |
| 234 | _dwc2_force_mode(hsotg, false); |
| 235 | } |
| 236 | |
| 237 | int hsotg_controller_reset(struct dwc2_hsotg *hsotg) |
| 238 | { |
| 239 | int ret; |
| 240 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 241 | |
| 242 | /* Add global reset and phy reinit to guarantee safe reset per ASIC */ |
| 243 | writel(0x0, apmu_base + APMU_USB_CLK_CTL); |
| 244 | udelay(200); |
| 245 | writel(0xb, apmu_base + APMU_USB_CLK_CTL); |
| 246 | hsotg->usb_do_restart = 0; |
| 247 | usb_phy_shutdown(hsotg->uphy); |
| 248 | usb_phy_init(hsotg->uphy); |
| 249 | usb_phy_set_suspend(hsotg->uphy, 0); |
| 250 | |
| 251 | ret = dwc2_core_reset(hsotg, false); |
| 252 | if (ret) { |
| 253 | dev_err(g_hsotg->dev, "!!!!!dwc2_core_reset failed(%d)\n", ret); |
| 254 | } |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | int hsotg_restore_host_cfgs(struct dwc2_hsotg *hsotg) |
| 260 | { |
| 261 | int i; |
| 262 | |
| 263 | dev_info(hsotg->dev, "restore host cfgs\n"); |
| 264 | for (i = 0; i < ARRAY_SIZE(dwc2_host_global_cfg); i++) |
| 265 | dwc2_writel(hsotg, dwc2_host_global_cfg[i].val, dwc2_host_global_cfg[i].reg); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | int hsotg_restore_device_cfgs(struct dwc2_hsotg *hsotg) |
| 271 | { |
| 272 | int i; |
| 273 | |
| 274 | dev_info(hsotg->dev, "restore dev cfgs\n"); |
| 275 | for (i = 0; i < ARRAY_SIZE(dwc2_device_global_cfg); i++) |
| 276 | dwc2_writel(hsotg, dwc2_device_global_cfg[i].val, dwc2_device_global_cfg[i].reg); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static void hsotg_otg_start_host(struct dwc2_hsotg *hsotg, int on) |
| 281 | { |
| 282 | struct usb_hcd *hcd = (struct usb_hcd *)g_hsotg->priv; |
| 283 | |
| 284 | if (!hcd) { |
| 285 | dev_err(g_hsotg->dev, "!!!!!hsotg->hcd is not set!\n"); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | dev_info(g_hsotg->dev, "%s host\n", on ? "start" : "stop"); |
| 290 | |
| 291 | if (on) { |
| 292 | /* set constraint before turn on vbus */ |
| 293 | pm_stay_awake(hsotg->dev); |
| 294 | pm_qos_update_request(&hsotg->qos_idle, hsotg->lpm_qos); |
| 295 | hsotg_controller_reset(hsotg); |
| 296 | dwc2_force_host_mode(hsotg); |
| 297 | hsotg_restore_host_cfgs(hsotg); |
| 298 | usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED); |
| 299 | dwc2_enable_global_interrupts(hsotg); |
| 300 | } else { |
| 301 | usb_remove_hcd(hcd); |
| 302 | hsotg_controller_reset(hsotg); |
| 303 | usb_phy_set_suspend(hsotg->uphy, 1); |
| 304 | pm_qos_update_request(&hsotg->qos_idle, PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE); |
| 305 | pm_relax(hsotg->dev); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static void hsotg_otg_start_peripherals(struct dwc2_hsotg *hsotg, int on) |
| 310 | { |
| 311 | struct usb_gadget *gadget = (struct usb_gadget *)&g_hsotg->gadget; |
| 312 | |
| 313 | if (!hsotg->gadget_enabled) { |
| 314 | dev_err(g_hsotg->dev, "!!!!!hsotg->gadget is not enabled!\n"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | dev_info(g_hsotg->dev, "gadget %s\n", on ? "on" : "off"); |
| 319 | pm_wakeup_event(hsotg->dev, USB_HANDLE_TIME_MSEC); |
| 320 | pm_qos_update_request_timeout(&hsotg->qos_idle, hsotg->lpm_qos, USB_HANDLE_TIME_MSEC * 1000); |
| 321 | if (on) { |
| 322 | hsotg_controller_reset(hsotg); |
| 323 | dwc2_force_device_mode(hsotg); |
| 324 | hsotg_restore_device_cfgs(hsotg); |
| 325 | usb_gadget_vbus_connect(gadget); |
| 326 | } else { |
| 327 | usb_gadget_vbus_disconnect(gadget); |
| 328 | /* usb_phy_set_suspend(hsotg->uphy, 1); */ |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | static void usb_otg_work_fn(struct work_struct *work) |
| 333 | { |
| 334 | int vbus, ret; |
| 335 | struct dwc2_hsotg *hsotg = g_hsotg; |
| 336 | int old_otg_state; |
| 337 | |
| 338 | /* check ID and VBUS and update cable state */ |
| 339 | if (hsotg->usbid_gpio >= 0) |
| 340 | hsotg->cur_usbid_val = gpio_get_value(hsotg->usbid_gpio); |
| 341 | else |
| 342 | hsotg->cur_usbid_val = 1; |
| 343 | mutex_lock(&hsotg->mtx_lock); |
| 344 | ret = pxa_usb_extern_call(PXA_USB_DEV_OTG, vbus, get_vbus, &vbus); |
| 345 | if (ret) { |
| 346 | vbus = usb_phy_get_vbus(hsotg->uphy); |
| 347 | } |
| 348 | hsotg->cur_vbus_val = vbus; |
| 349 | |
| 350 | if (force_host) |
| 351 | hsotg->cur_usbid_val = 0; |
| 352 | else if (force_dev) |
| 353 | hsotg->cur_usbid_val = 1; |
| 354 | |
| 355 | old_otg_state = hsotg->otg_state; |
| 356 | pr_info("=>old_otg_state: %d, usbid: %d vbus: %d\n", |
| 357 | old_otg_state, hsotg->cur_usbid_val, hsotg->cur_vbus_val); |
| 358 | |
| 359 | /* at first we clean states which are no longer active */ |
| 360 | if (hsotg->otg_state == OTG_STATE_B_IDLE) { |
| 361 | if (!hsotg->cur_usbid_val) { |
| 362 | printk("disable vbus irq\n"); |
| 363 | disable_irq(hsotg->vbus_irq); |
| 364 | usb_otg_set_vbus(hsotg, true); |
| 365 | msleep(VBUS_RISE_FALL_MS); |
| 366 | hsotg->otg_state = OTG_STATE_A_HOST; |
| 367 | hsotg->op_state = hsotg->otg_state; |
| 368 | hsotg_otg_start_host(hsotg, 1); |
| 369 | } else { |
| 370 | if (vbus) |
| 371 | hsotg->otg_state = OTG_STATE_B_PERIPHERAL; |
| 372 | else |
| 373 | hsotg->otg_state = OTG_STATE_B_IDLE; |
| 374 | hsotg->op_state = hsotg->otg_state; |
| 375 | hsotg_otg_start_peripherals(hsotg, vbus); |
| 376 | } |
| 377 | } else if (hsotg->otg_state == OTG_STATE_B_PERIPHERAL) { |
| 378 | if (!hsotg->cur_vbus_val) { |
| 379 | hsotg->otg_state = OTG_STATE_B_IDLE; |
| 380 | hsotg_otg_start_peripherals(hsotg, 0); |
| 381 | } |
| 382 | } else if (hsotg->otg_state == OTG_STATE_A_HOST) { |
| 383 | if (hsotg->cur_usbid_val) { |
| 384 | hsotg->otg_state = OTG_STATE_B_IDLE; |
| 385 | hsotg->op_state = hsotg->otg_state; |
| 386 | hsotg_otg_start_host(hsotg, 0); |
| 387 | msleep(1); |
| 388 | usb_otg_set_vbus(hsotg, false); |
| 389 | msleep(VBUS_RISE_FALL_MS); |
| 390 | printk("enable vbus irq\n"); |
| 391 | enable_irq(hsotg->vbus_irq); |
| 392 | } |
| 393 | } |
| 394 | mutex_unlock(&hsotg->mtx_lock); |
| 395 | pr_info("cur_otg_state: [%d->%d], usbid: %d vbus: %d\n", |
| 396 | old_otg_state, hsotg->otg_state, |
| 397 | hsotg->cur_usbid_val, hsotg->cur_vbus_val); |
| 398 | } |
| 399 | |
| 400 | static irqreturn_t vbus_irq(int irq, void *dev); |
| 401 | static irqreturn_t usbid_irq(int irq, void *dev_id) |
| 402 | { |
| 403 | struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)g_hsotg; |
| 404 | dev_info(hsotg->dev, "dwc2 usbid_irq is served..\n"); |
| 405 | vbus_irq(irq, dev_id); |
| 406 | return IRQ_HANDLED; |
| 407 | } |
| 408 | |
| 409 | static void usbid_wakeup_handler(int gpio, void *data) |
| 410 | { |
| 411 | } |
| 412 | |
| 413 | static int usbid_irq_init(struct platform_device *pdev, struct dwc2_hsotg *hsotg) |
| 414 | { |
| 415 | int ret = -1; |
| 416 | |
| 417 | ret = of_property_read_u32(pdev->dev.of_node, |
| 418 | "usbid_gpio", &hsotg->usbid_gpio); |
| 419 | pr_info("dwc2:usbid_gpio: %d\n", hsotg->usbid_gpio); |
| 420 | |
| 421 | if (ret) { |
| 422 | hsotg->usbid_gpio = -1; |
| 423 | pr_err("%s no usbid-gpio defined\n", __func__); |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | of_property_read_u32(pdev->dev.of_node, |
| 428 | "edge_detect_gpio", &hsotg->edge_det_gpio); |
| 429 | if (hsotg->edge_det_gpio > 0) { |
| 430 | ret = request_mfp_edge_wakeup(hsotg->edge_det_gpio, |
| 431 | usbid_wakeup_handler, |
| 432 | NULL, &pdev->dev); |
| 433 | if (ret) { |
| 434 | dev_err(hsotg->dev, "failed to request edge wakeup.\n"); |
| 435 | goto out; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | hsotg->cur_usbid_val = gpio_get_value(hsotg->usbid_gpio); |
| 440 | ret = gpio_request(hsotg->usbid_gpio, "dwc2-usbid"); |
| 441 | gpio_direction_input(hsotg->usbid_gpio); |
| 442 | |
| 443 | hsotg->usbid_irq = gpio_to_irq(hsotg->usbid_gpio); |
| 444 | ret = |
| 445 | request_threaded_irq(hsotg->usbid_irq, NULL, usbid_irq, |
| 446 | IRQF_SHARED | IRQF_TRIGGER_RISING | |
| 447 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "dwc2-usbid", |
| 448 | hsotg); |
| 449 | if (ret < 0) { |
| 450 | dev_err(hsotg->dev, "%s: request irq failed!\n", |
| 451 | __func__); |
| 452 | } |
| 453 | |
| 454 | out: |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | static irqreturn_t vbus_irq(int irq, void *dev) |
| 459 | { |
| 460 | struct dwc2_hsotg *hsotg_dev = (struct dwc2_hsotg *)dev; |
| 461 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 462 | dev_info(hsotg_dev->dev, "asr-usb vbus int enter..\n"); |
| 463 | /* wait 50ms for vbus to be stable */ |
| 464 | msleep(50); |
| 465 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 466 | | (USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR |
| 467 | | USB_ID_WAKE_CLR), |
| 468 | apmu_base + APMU_USB_WAKE_CLR); |
| 469 | |
| 470 | pm_wakeup_event(hsotg_dev->dev, USB_HANDLE_TIME_MSEC); |
| 471 | if (work_pending(&g_hsotg->otg_work.work)) { |
| 472 | dev_info(hsotg_dev->dev, "cancel otg work..."); |
| 473 | cancel_delayed_work_sync(&g_hsotg->otg_work); |
| 474 | dev_info(hsotg_dev->dev, "done\n"); |
| 475 | pm_wakeup_event(hsotg_dev->dev, USB_HANDLE_TIME_MSEC); |
| 476 | } |
| 477 | schedule_delayed_work(&g_hsotg->otg_work, 0); |
| 478 | dev_info(hsotg_dev->dev, "asr-usb vbus interrupt is served..\n"); |
| 479 | return IRQ_HANDLED; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Check the dr_mode against the module configuration and hardware |
| 484 | * capabilities. |
| 485 | * |
| 486 | * The hardware, module, and dr_mode, can each be set to host, device, |
| 487 | * or otg. Check that all these values are compatible and adjust the |
| 488 | * value of dr_mode if possible. |
| 489 | * |
| 490 | * actual |
| 491 | * HW MOD dr_mode dr_mode |
| 492 | * ------------------------------ |
| 493 | * HST HST any : HST |
| 494 | * HST DEV any : --- |
| 495 | * HST OTG any : HST |
| 496 | * |
| 497 | * DEV HST any : --- |
| 498 | * DEV DEV any : DEV |
| 499 | * DEV OTG any : DEV |
| 500 | * |
| 501 | * OTG HST any : HST |
| 502 | * OTG DEV any : DEV |
| 503 | * OTG OTG any : dr_mode |
| 504 | */ |
| 505 | static int dwc2_get_dr_mode(struct dwc2_hsotg *hsotg) |
| 506 | { |
| 507 | enum usb_dr_mode mode; |
| 508 | |
| 509 | hsotg->dr_mode = usb_get_dr_mode(hsotg->dev); |
| 510 | if (hsotg->dr_mode == USB_DR_MODE_UNKNOWN) |
| 511 | hsotg->dr_mode = USB_DR_MODE_OTG; |
| 512 | |
| 513 | mode = hsotg->dr_mode; |
| 514 | |
| 515 | if (dwc2_hw_is_device(hsotg)) { |
| 516 | if (IS_ENABLED(CONFIG_USB_DWC2_HOST)) { |
| 517 | dev_err(hsotg->dev, |
| 518 | "Controller does not support host mode.\n"); |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | mode = USB_DR_MODE_PERIPHERAL; |
| 522 | } else if (dwc2_hw_is_host(hsotg)) { |
| 523 | if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL)) { |
| 524 | dev_err(hsotg->dev, |
| 525 | "Controller does not support device mode.\n"); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | mode = USB_DR_MODE_HOST; |
| 529 | } else { |
| 530 | if (IS_ENABLED(CONFIG_USB_DWC2_HOST)) |
| 531 | mode = USB_DR_MODE_HOST; |
| 532 | else if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL)) |
| 533 | mode = USB_DR_MODE_PERIPHERAL; |
| 534 | } |
| 535 | |
| 536 | if (mode != hsotg->dr_mode) { |
| 537 | dev_warn(hsotg->dev, |
| 538 | "Configuration mismatch. dr_mode forced to %s\n", |
| 539 | mode == USB_DR_MODE_HOST ? "host" : "device"); |
| 540 | |
| 541 | hsotg->dr_mode = mode; |
| 542 | } |
| 543 | |
| 544 | dev_info(hsotg->dev, "dr_mode: %d\n", hsotg->dr_mode); |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg) |
| 549 | { |
| 550 | struct platform_device *pdev = to_platform_device(hsotg->dev); |
| 551 | int ret; |
| 552 | |
| 553 | ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies), |
| 554 | hsotg->supplies); |
| 555 | if (ret) |
| 556 | return ret; |
| 557 | |
| 558 | if (hsotg->clk) { |
| 559 | ret = clk_prepare_enable(hsotg->clk); |
| 560 | if (ret) |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | if (hsotg->uphy) { |
| 565 | ret = usb_phy_init(hsotg->uphy); |
| 566 | } else if (hsotg->plat && hsotg->plat->phy_init) { |
| 567 | ret = hsotg->plat->phy_init(pdev, hsotg->plat->phy_type); |
| 568 | } else { |
| 569 | ret = phy_power_on(hsotg->phy); |
| 570 | if (ret == 0) |
| 571 | ret = phy_init(hsotg->phy); |
| 572 | } |
| 573 | |
| 574 | return ret; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * dwc2_lowlevel_hw_enable - enable platform lowlevel hw resources |
| 579 | * @hsotg: The driver state |
| 580 | * |
| 581 | * A wrapper for platform code responsible for controlling |
| 582 | * low-level USB platform resources (phy, clock, regulators) |
| 583 | */ |
| 584 | int dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg) |
| 585 | { |
| 586 | int ret = __dwc2_lowlevel_hw_enable(hsotg); |
| 587 | |
| 588 | if (ret == 0) |
| 589 | hsotg->ll_hw_enabled = true; |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | static int __dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg) |
| 594 | { |
| 595 | struct platform_device *pdev = to_platform_device(hsotg->dev); |
| 596 | int ret = 0; |
| 597 | |
| 598 | #ifdef CONFIG_CPU_ASR18XX |
| 599 | return 0; |
| 600 | #endif |
| 601 | |
| 602 | if (hsotg->uphy) { |
| 603 | usb_phy_shutdown(hsotg->uphy); |
| 604 | } else if (hsotg->plat && hsotg->plat->phy_exit) { |
| 605 | ret = hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type); |
| 606 | } else { |
| 607 | ret = phy_exit(hsotg->phy); |
| 608 | if (ret == 0) |
| 609 | ret = phy_power_off(hsotg->phy); |
| 610 | } |
| 611 | if (ret) |
| 612 | return ret; |
| 613 | |
| 614 | if (hsotg->clk) |
| 615 | clk_disable_unprepare(hsotg->clk); |
| 616 | |
| 617 | ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), |
| 618 | hsotg->supplies); |
| 619 | |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * dwc2_lowlevel_hw_disable - disable platform lowlevel hw resources |
| 625 | * @hsotg: The driver state |
| 626 | * |
| 627 | * A wrapper for platform code responsible for controlling |
| 628 | * low-level USB platform resources (phy, clock, regulators) |
| 629 | */ |
| 630 | int dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg) |
| 631 | { |
| 632 | int ret = __dwc2_lowlevel_hw_disable(hsotg); |
| 633 | |
| 634 | if (ret == 0) |
| 635 | hsotg->ll_hw_enabled = false; |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | static int dwc2_lowlevel_hw_init(struct dwc2_hsotg *hsotg) |
| 640 | { |
| 641 | int i, ret; |
| 642 | |
| 643 | hsotg->reset = devm_reset_control_get_optional(hsotg->dev, "dwc2"); |
| 644 | if (IS_ERR(hsotg->reset)) { |
| 645 | ret = PTR_ERR(hsotg->reset); |
| 646 | dev_err(hsotg->dev, "error getting reset control %d\n", ret); |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | reset_control_deassert(hsotg->reset); |
| 651 | |
| 652 | hsotg->reset_ecc = devm_reset_control_get_optional(hsotg->dev, "dwc2-ecc"); |
| 653 | if (IS_ERR(hsotg->reset_ecc)) { |
| 654 | ret = PTR_ERR(hsotg->reset_ecc); |
| 655 | dev_err(hsotg->dev, "error getting reset control for ecc %d\n", ret); |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | reset_control_deassert(hsotg->reset_ecc); |
| 660 | |
| 661 | /* |
| 662 | * Attempt to find a generic PHY, then look for an old style |
| 663 | * USB PHY and then fall back to pdata |
| 664 | */ |
| 665 | hsotg->phy = devm_phy_get(hsotg->dev, "usb2-phy"); |
| 666 | if (IS_ERR(hsotg->phy)) { |
| 667 | ret = PTR_ERR(hsotg->phy); |
| 668 | switch (ret) { |
| 669 | case -ENODEV: |
| 670 | case -ENOSYS: |
| 671 | hsotg->phy = NULL; |
| 672 | break; |
| 673 | case -EPROBE_DEFER: |
| 674 | return ret; |
| 675 | default: |
| 676 | dev_err(hsotg->dev, "error getting phy %d\n", ret); |
| 677 | return ret; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | if (!hsotg->phy) { |
| 682 | hsotg->uphy = devm_usb_get_phy(hsotg->dev, USB_PHY_TYPE_USB2); |
| 683 | if (IS_ERR(hsotg->uphy)) { |
| 684 | ret = PTR_ERR(hsotg->uphy); |
| 685 | switch (ret) { |
| 686 | case -ENODEV: |
| 687 | case -ENXIO: |
| 688 | hsotg->uphy = NULL; |
| 689 | break; |
| 690 | case -EPROBE_DEFER: |
| 691 | return ret; |
| 692 | default: |
| 693 | dev_err(hsotg->dev, "error getting usb phy %d\n", |
| 694 | ret); |
| 695 | return ret; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | hsotg->plat = dev_get_platdata(hsotg->dev); |
| 701 | |
| 702 | /* Clock */ |
| 703 | hsotg->clk = devm_clk_get_optional(hsotg->dev, "otg"); |
| 704 | if (IS_ERR(hsotg->clk)) { |
| 705 | dev_err(hsotg->dev, "cannot get otg clock\n"); |
| 706 | return PTR_ERR(hsotg->clk); |
| 707 | } |
| 708 | |
| 709 | /* Regulators */ |
| 710 | for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++) |
| 711 | hsotg->supplies[i].supply = dwc2_hsotg_supply_names[i]; |
| 712 | |
| 713 | ret = devm_regulator_bulk_get(hsotg->dev, ARRAY_SIZE(hsotg->supplies), |
| 714 | hsotg->supplies); |
| 715 | if (ret) { |
| 716 | dev_err(hsotg->dev, "failed to request supplies: %d\n", ret); |
| 717 | return ret; |
| 718 | } |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * dwc2_driver_remove() - Called when the DWC_otg core is unregistered with the |
| 724 | * DWC_otg driver |
| 725 | * |
| 726 | * @dev: Platform device |
| 727 | * |
| 728 | * This routine is called, for example, when the rmmod command is executed. The |
| 729 | * device may or may not be electrically present. If it is present, the driver |
| 730 | * stops device processing. Any resources used on behalf of this device are |
| 731 | * freed. |
| 732 | */ |
| 733 | static int dwc2_driver_remove(struct platform_device *dev) |
| 734 | { |
| 735 | struct dwc2_hsotg *hsotg = platform_get_drvdata(dev); |
| 736 | |
| 737 | dwc2_debugfs_exit(hsotg); |
| 738 | if (hsotg->hcd_enabled) |
| 739 | dwc2_hcd_remove(hsotg); |
| 740 | if (hsotg->gadget_enabled) |
| 741 | dwc2_hsotg_remove(hsotg); |
| 742 | |
| 743 | if (hsotg->ll_hw_enabled) |
| 744 | dwc2_lowlevel_hw_disable(hsotg); |
| 745 | |
| 746 | reset_control_assert(hsotg->reset); |
| 747 | reset_control_assert(hsotg->reset_ecc); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * dwc2_driver_shutdown() - Called on device shutdown |
| 754 | * |
| 755 | * @dev: Platform device |
| 756 | * |
| 757 | * In specific conditions (involving usb hubs) dwc2 devices can create a |
| 758 | * lot of interrupts, even to the point of overwhelming devices running |
| 759 | * at low frequencies. Some devices need to do special clock handling |
| 760 | * at shutdown-time which may bring the system clock below the threshold |
| 761 | * of being able to handle the dwc2 interrupts. Disabling dwc2-irqs |
| 762 | * prevents reboots/poweroffs from getting stuck in such cases. |
| 763 | */ |
| 764 | static void dwc2_driver_shutdown(struct platform_device *dev) |
| 765 | { |
| 766 | struct dwc2_hsotg *hsotg = platform_get_drvdata(dev); |
| 767 | |
| 768 | dwc2_disable_global_interrupts(hsotg); |
| 769 | synchronize_irq(hsotg->irq); |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * dwc2_check_core_endianness() - Returns true if core and AHB have |
| 774 | * opposite endianness. |
| 775 | * @hsotg: Programming view of the DWC_otg controller. |
| 776 | */ |
| 777 | static bool dwc2_check_core_endianness(struct dwc2_hsotg *hsotg) |
| 778 | { |
| 779 | u32 snpsid; |
| 780 | |
| 781 | snpsid = ioread32(hsotg->regs + GSNPSID); |
| 782 | if ((snpsid & GSNPSID_ID_MASK) == DWC2_OTG_ID || |
| 783 | (snpsid & GSNPSID_ID_MASK) == DWC2_FS_IOT_ID || |
| 784 | (snpsid & GSNPSID_ID_MASK) == DWC2_HS_IOT_ID) |
| 785 | return false; |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | static ssize_t otg_mode_store(struct device *pdev, struct device_attribute *attr, |
| 790 | const char *buf, size_t count) |
| 791 | { |
| 792 | struct dwc2_hsotg *hsotg = dev_get_drvdata(pdev); |
| 793 | enum usb_dr_mode dr_mode; |
| 794 | |
| 795 | mutex_lock(&hsotg->mtx_lock); |
| 796 | if (!strncmp(buf, "host", 4)) { |
| 797 | if(hsotg->otg_state == OTG_STATE_A_HOST) { |
| 798 | pr_err("already in host mode\n"); |
| 799 | goto out; |
| 800 | } |
| 801 | disable_irq(hsotg->vbus_irq); |
| 802 | pr_info("disable vbus irq\n"); |
| 803 | |
| 804 | if (hsotg->otg_state == OTG_STATE_B_PERIPHERAL) { |
| 805 | hsotg->otg_state = OTG_STATE_B_IDLE; |
| 806 | hsotg->op_state = hsotg->otg_state; |
| 807 | hsotg_otg_start_peripherals(hsotg, 0); |
| 808 | } |
| 809 | |
| 810 | if (hsotg->otg_state == OTG_STATE_B_IDLE) { |
| 811 | force_host = 1; |
| 812 | force_dev = 0; |
| 813 | usb_otg_set_vbus(hsotg, true); |
| 814 | msleep(VBUS_RISE_FALL_MS); |
| 815 | hsotg->otg_state = OTG_STATE_A_HOST; |
| 816 | hsotg->op_state = hsotg->otg_state; |
| 817 | dr_mode = USB_DR_MODE_HOST; |
| 818 | hsotg->dr_mode = dr_mode; |
| 819 | hsotg_otg_start_host(hsotg, 1); |
| 820 | dev_info(pdev, "userspace set host: otg_mode: %d\n", hsotg->otg_state); |
| 821 | } |
| 822 | } else if (!strncmp(buf, "device", 6)) { |
| 823 | if(hsotg->otg_state == OTG_STATE_B_PERIPHERAL) { |
| 824 | pr_err("already in device mode\n"); |
| 825 | goto out; |
| 826 | } |
| 827 | |
| 828 | if (hsotg->otg_state == OTG_STATE_A_HOST) { |
| 829 | hsotg->otg_state = OTG_STATE_B_IDLE; |
| 830 | hsotg->op_state = hsotg->otg_state; |
| 831 | hsotg_otg_start_host(hsotg, 0); |
| 832 | msleep(1); |
| 833 | usb_otg_set_vbus(hsotg, false); |
| 834 | msleep(VBUS_RISE_FALL_MS); |
| 835 | } |
| 836 | |
| 837 | if (hsotg->otg_state == OTG_STATE_B_IDLE) { |
| 838 | force_host = 0; |
| 839 | force_dev = 1; |
| 840 | dr_mode = USB_DR_MODE_PERIPHERAL; |
| 841 | hsotg->dr_mode = dr_mode; |
| 842 | hsotg->otg_state = OTG_STATE_B_PERIPHERAL; |
| 843 | hsotg->op_state = hsotg->otg_state; |
| 844 | hsotg_otg_start_peripherals(hsotg, 1); |
| 845 | dev_info(pdev, "userspace set device: otg_mode: %d\n", hsotg->otg_state); |
| 846 | } |
| 847 | |
| 848 | enable_irq(hsotg->vbus_irq); |
| 849 | pr_err("enable vbus irq\n"); |
| 850 | } else { |
| 851 | force_host = 0; |
| 852 | force_dev = 0; |
| 853 | if (hsotg->otg_state == OTG_STATE_B_PERIPHERAL) { |
| 854 | hsotg->otg_state = OTG_STATE_B_IDLE; |
| 855 | hsotg_otg_start_peripherals(hsotg, 0); |
| 856 | } else if (hsotg->otg_state == OTG_STATE_A_HOST) { |
| 857 | hsotg->otg_state = OTG_STATE_B_IDLE; |
| 858 | hsotg->op_state = hsotg->otg_state; |
| 859 | hsotg_otg_start_host(hsotg, 0); |
| 860 | msleep(1); |
| 861 | usb_otg_set_vbus(hsotg, false); |
| 862 | msleep(VBUS_RISE_FALL_MS); |
| 863 | printk("enable vbus irq\n"); |
| 864 | enable_irq(hsotg->vbus_irq); |
| 865 | } else { |
| 866 | dev_info(pdev, "already in idle none host/device mode: %d\n", hsotg->otg_state); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | out: |
| 871 | mutex_unlock(&hsotg->mtx_lock); |
| 872 | |
| 873 | return count; |
| 874 | } |
| 875 | |
| 876 | static ssize_t otg_mode_show(struct device *pdev, struct device_attribute *attr, char *buf) |
| 877 | { |
| 878 | struct dwc2_hsotg *hsotg = dev_get_drvdata(pdev); |
| 879 | char *host_dev_str; |
| 880 | |
| 881 | if (hsotg->otg_state == OTG_STATE_A_HOST) |
| 882 | host_dev_str = "host"; |
| 883 | else if (hsotg->otg_state == OTG_STATE_B_PERIPHERAL) |
| 884 | host_dev_str = "device"; |
| 885 | else |
| 886 | host_dev_str = "idle"; |
| 887 | |
| 888 | return sprintf(buf, "otg_state:%d, otg mode: %s\n", hsotg->otg_state, host_dev_str); |
| 889 | } |
| 890 | |
| 891 | static DEVICE_ATTR(otg_mode, S_IWUSR |S_IRUGO, otg_mode_show, otg_mode_store); |
| 892 | |
| 893 | /** |
| 894 | * dwc2_driver_probe() - Called when the DWC_otg core is bound to the DWC_otg |
| 895 | * driver |
| 896 | * |
| 897 | * @dev: Platform device |
| 898 | * |
| 899 | * This routine creates the driver components required to control the device |
| 900 | * (core, HCD, and PCD) and initializes the device. The driver components are |
| 901 | * stored in a dwc2_hsotg structure. A reference to the dwc2_hsotg is saved |
| 902 | * in the device private data. This allows the driver to access the dwc2_hsotg |
| 903 | * structure on subsequent calls to driver methods for this device. |
| 904 | */ |
| 905 | static int dwc2_driver_probe(struct platform_device *dev) |
| 906 | { |
| 907 | struct dwc2_hsotg *hsotg; |
| 908 | struct resource *res; |
| 909 | int retval; |
| 910 | void __iomem *apmu_base; |
| 911 | struct device_node *node = dev->dev.of_node; |
| 912 | u32 data; |
| 913 | |
| 914 | hsotg = devm_kzalloc(&dev->dev, sizeof(*hsotg), GFP_KERNEL); |
| 915 | if (!hsotg) |
| 916 | return -ENOMEM; |
| 917 | |
| 918 | hsotg->dev = &dev->dev; |
| 919 | |
| 920 | /* |
| 921 | * Use reasonable defaults so platforms don't have to provide these. |
| 922 | */ |
| 923 | if (!dev->dev.dma_mask) |
| 924 | dev->dev.dma_mask = &dev->dev.coherent_dma_mask; |
| 925 | retval = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32)); |
| 926 | if (retval) { |
| 927 | dev_err(&dev->dev, "can't set coherent DMA mask: %d\n", retval); |
| 928 | return retval; |
| 929 | } |
| 930 | |
| 931 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 932 | hsotg->regs = devm_ioremap_resource(&dev->dev, res); |
| 933 | if (IS_ERR(hsotg->regs)) |
| 934 | return PTR_ERR(hsotg->regs); |
| 935 | |
| 936 | dev_dbg(&dev->dev, "mapped PA %08lx to VA %p\n", |
| 937 | (unsigned long)res->start, hsotg->regs); |
| 938 | |
| 939 | retval = dwc2_lowlevel_hw_init(hsotg); |
| 940 | if (retval) |
| 941 | return retval; |
| 942 | |
| 943 | spin_lock_init(&hsotg->lock); |
| 944 | mutex_init(&hsotg->mtx_lock); |
| 945 | |
| 946 | hsotg->irq = platform_get_irq(dev, 0); |
| 947 | if (hsotg->irq < 0) |
| 948 | return hsotg->irq; |
| 949 | |
| 950 | dev_dbg(hsotg->dev, "registering common handler for irq%d\n", |
| 951 | hsotg->irq); |
| 952 | retval = devm_request_irq(hsotg->dev, hsotg->irq, |
| 953 | dwc2_handle_common_intr, IRQF_SHARED, |
| 954 | dev_name(hsotg->dev), hsotg); |
| 955 | if (retval) |
| 956 | return retval; |
| 957 | |
| 958 | hsotg->vbus_supply = devm_regulator_get_optional(hsotg->dev, "vbus"); |
| 959 | if (IS_ERR(hsotg->vbus_supply)) { |
| 960 | retval = PTR_ERR(hsotg->vbus_supply); |
| 961 | hsotg->vbus_supply = NULL; |
| 962 | if (retval != -ENODEV) |
| 963 | return retval; |
| 964 | } |
| 965 | |
| 966 | retval = dwc2_lowlevel_hw_enable(hsotg); |
| 967 | if (retval) |
| 968 | return retval; |
| 969 | |
| 970 | hsotg->needs_byte_swap = dwc2_check_core_endianness(hsotg); |
| 971 | |
| 972 | retval = dwc2_get_dr_mode(hsotg); |
| 973 | if (retval) |
| 974 | goto error; |
| 975 | |
| 976 | hsotg->need_phy_for_wake = |
| 977 | of_property_read_bool(dev->dev.of_node, |
| 978 | "snps,need-phy-for-wake"); |
| 979 | hsotg->no_acchg_det = |
| 980 | of_property_read_bool(dev->dev.of_node, |
| 981 | "snps,no-acchg-det"); |
| 982 | |
| 983 | /* |
| 984 | * Reset before dwc2_get_hwparams() then it could get power-on real |
| 985 | * reset value form registers. |
| 986 | */ |
| 987 | retval = dwc2_core_reset(hsotg, false); |
| 988 | if (retval) |
| 989 | goto error; |
| 990 | |
| 991 | /* Detect config values from hardware */ |
| 992 | retval = dwc2_get_hwparams(hsotg); |
| 993 | if (retval) |
| 994 | goto error; |
| 995 | |
| 996 | /* |
| 997 | * For OTG cores, set the force mode bits to reflect the value |
| 998 | * of dr_mode. Force mode bits should not be touched at any |
| 999 | * other time after this. |
| 1000 | */ |
| 1001 | //dwc2_force_dr_mode(hsotg); |
| 1002 | retval = dwc2_init_params(hsotg); |
| 1003 | if (retval) |
| 1004 | goto error; |
| 1005 | |
| 1006 | if (hsotg->dr_mode != USB_DR_MODE_HOST) { |
| 1007 | dwc2_force_device_mode(hsotg); |
| 1008 | retval = dwc2_gadget_init(hsotg); |
| 1009 | if (retval) |
| 1010 | goto error; |
| 1011 | hsotg->gadget_enabled = 1; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | * If we need PHY for wakeup we must be wakeup capable. |
| 1016 | * When we have a device that can wake without the PHY we |
| 1017 | * can adjust this condition. |
| 1018 | */ |
| 1019 | if (hsotg->need_phy_for_wake) |
| 1020 | device_set_wakeup_capable(&dev->dev, true); |
| 1021 | |
| 1022 | if (!of_property_read_u32(dev->dev.of_node, "otg-force-host-mode", &data)) { |
| 1023 | dev_info(hsotg->dev, "otg force host mode\n"); |
| 1024 | force_host = 1; |
| 1025 | force_dev = 0; |
| 1026 | } else if (!of_property_read_u32(dev->dev.of_node, "otg-force-dev-mode", &data)) { |
| 1027 | dev_info(hsotg->dev, "otg force dev mode\n"); |
| 1028 | force_dev = 1; |
| 1029 | force_host = 0; |
| 1030 | } |
| 1031 | |
| 1032 | hsotg->reset_phy_on_wake = |
| 1033 | of_property_read_bool(dev->dev.of_node, |
| 1034 | "snps,reset-phy-on-wake"); |
| 1035 | if (hsotg->reset_phy_on_wake && !hsotg->phy) { |
| 1036 | dev_warn(hsotg->dev, |
| 1037 | "Quirk reset-phy-on-wake only supports generic PHYs\n"); |
| 1038 | hsotg->reset_phy_on_wake = false; |
| 1039 | } |
| 1040 | |
| 1041 | if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) { |
| 1042 | hsotg_controller_reset(hsotg); |
| 1043 | dwc2_force_host_mode(hsotg); |
| 1044 | retval = dwc2_hcd_init(hsotg); |
| 1045 | if (retval) { |
| 1046 | if (hsotg->gadget_enabled) |
| 1047 | dwc2_hsotg_remove(hsotg); |
| 1048 | goto error; |
| 1049 | } |
| 1050 | hsotg->hcd_enabled = 1; |
| 1051 | } |
| 1052 | |
| 1053 | platform_set_drvdata(dev, hsotg); |
| 1054 | hsotg->hibernated = 0; |
| 1055 | |
| 1056 | dwc2_debugfs_init(hsotg); |
| 1057 | |
| 1058 | retval = sysfs_create_file(&dev->dev.kobj, &dev_attr_otg_mode.attr); |
| 1059 | if(retval){ |
| 1060 | dev_err(&dev->dev, "create host_dev mode failed"); |
| 1061 | goto error; |
| 1062 | } |
| 1063 | |
| 1064 | /* Gadget code manages lowlevel hw on its own */ |
| 1065 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 1066 | dwc2_lowlevel_hw_disable(hsotg); |
| 1067 | |
| 1068 | #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \ |
| 1069 | IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE) |
| 1070 | /* Postponed adding a new gadget to the udc class driver list */ |
| 1071 | if (hsotg->gadget_enabled) { |
| 1072 | retval = usb_add_gadget_udc(hsotg->dev, &hsotg->gadget); |
| 1073 | if (retval) { |
| 1074 | hsotg->gadget.udc = NULL; |
| 1075 | dwc2_hsotg_remove(hsotg); |
| 1076 | goto error; |
| 1077 | } |
| 1078 | } |
| 1079 | #endif /* CONFIG_USB_DWC2_PERIPHERAL || CONFIG_USB_DWC2_DUAL_ROLE */ |
| 1080 | |
| 1081 | hsotg->vbus_irq = platform_get_irq(dev, 1); |
| 1082 | if (hsotg->vbus_irq < 0) { |
| 1083 | dev_err(&dev->dev, "failed to get vbus irq\n"); |
| 1084 | retval = -ENXIO; |
| 1085 | goto error; |
| 1086 | } |
| 1087 | |
| 1088 | retval = devm_request_threaded_irq(&dev->dev, hsotg->vbus_irq, |
| 1089 | NULL, vbus_irq, |
| 1090 | IRQF_ONESHOT | IRQF_NO_SUSPEND, |
| 1091 | "asr-usb-vbus", hsotg); |
| 1092 | if (retval) { |
| 1093 | dev_info(&dev->dev, |
| 1094 | "Can not request irq for VBUS\n"); |
| 1095 | goto error; |
| 1096 | } |
| 1097 | |
| 1098 | usbid_irq_init(dev, hsotg); |
| 1099 | INIT_DELAYED_WORK(&hsotg->otg_work, usb_otg_work_fn); |
| 1100 | |
| 1101 | if (of_property_read_bool(node , "otg,use-gpio-vbus")) { |
| 1102 | if (of_property_read_u32(node , "gpio-num", &hsotg->gpio_num)) { |
| 1103 | hsotg->gpio_num = ENNUM; |
| 1104 | dev_info(&dev->dev, "failed to find GPIO number in dts\n"); |
| 1105 | } else { |
| 1106 | if (gpio_request(hsotg->gpio_num, "OTGVBUS")) { |
| 1107 | dev_err(&dev->dev , "OTG Request GPIO failed, gpio: %d\n" , |
| 1108 | hsotg->gpio_num); |
| 1109 | hsotg->gpio_num = ENNUM; |
| 1110 | } else |
| 1111 | gpio_direction_output(hsotg->gpio_num , 0); |
| 1112 | } |
| 1113 | } else |
| 1114 | hsotg->gpio_num = ENNUM; |
| 1115 | /** |
| 1116 | prop = of_get_property(node, "lpm-qos", &proplen); |
| 1117 | if (!prop) { |
| 1118 | pr_err("lpm-qos for dwc otg is not defined\n"); |
| 1119 | goto error; |
| 1120 | } else |
| 1121 | hsotg->lpm_qos = be32_to_cpup(prop); |
| 1122 | |
| 1123 | hsotg->qos_idle.name = "dwc2-otg"; |
| 1124 | pm_qos_add_request(&hsotg->qos_idle, PM_QOS_CPUIDLE_BLOCK, |
| 1125 | PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE); |
| 1126 | **/ |
| 1127 | g_hsotg = hsotg; |
| 1128 | schedule_delayed_work(&g_hsotg->otg_work, HZ); |
| 1129 | g_hsotg->otg_state = OTG_STATE_B_IDLE; |
| 1130 | apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 1131 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) | USB_VBUS_WAKE_EN |
| 1132 | | (USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR | USB_ID_WAKE_CLR), |
| 1133 | apmu_base + APMU_USB_WAKE_CLR); |
| 1134 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 1135 | | (USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR | USB_ID_WAKE_CLR), |
| 1136 | apmu_base + APMU_USB_WAKE_CLR); |
| 1137 | |
| 1138 | device_init_wakeup(&dev->dev, 1); |
| 1139 | pm_stay_awake(&dev->dev); |
| 1140 | dev_info(hsotg->dev, "probe done\n"); |
| 1141 | |
| 1142 | return 0; |
| 1143 | |
| 1144 | error: |
| 1145 | if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) |
| 1146 | dwc2_lowlevel_hw_disable(hsotg); |
| 1147 | return retval; |
| 1148 | } |
| 1149 | |
| 1150 | static int __maybe_unused dwc2_suspend(struct device *dev) |
| 1151 | { |
| 1152 | struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev); |
| 1153 | bool is_device_mode = dwc2_is_device_mode(dwc2); |
| 1154 | int ret = 0; |
| 1155 | |
| 1156 | /* asr private */ |
| 1157 | return 0; |
| 1158 | |
| 1159 | if (is_device_mode) |
| 1160 | dwc2_hsotg_suspend(dwc2); |
| 1161 | |
| 1162 | if (dwc2->ll_hw_enabled && |
| 1163 | (is_device_mode || dwc2_host_can_poweroff_phy(dwc2))) { |
| 1164 | ret = __dwc2_lowlevel_hw_disable(dwc2); |
| 1165 | dwc2->phy_off_for_suspend = true; |
| 1166 | } |
| 1167 | |
| 1168 | return ret; |
| 1169 | } |
| 1170 | |
| 1171 | static int __maybe_unused dwc2_resume(struct device *dev) |
| 1172 | { |
| 1173 | struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev); |
| 1174 | int ret = 0; |
| 1175 | |
| 1176 | /* asr private */ |
| 1177 | return 0; |
| 1178 | |
| 1179 | if (dwc2->phy_off_for_suspend && dwc2->ll_hw_enabled) { |
| 1180 | ret = __dwc2_lowlevel_hw_enable(dwc2); |
| 1181 | if (ret) |
| 1182 | return ret; |
| 1183 | } |
| 1184 | dwc2->phy_off_for_suspend = false; |
| 1185 | |
| 1186 | if (dwc2_is_device_mode(dwc2)) |
| 1187 | ret = dwc2_hsotg_resume(dwc2); |
| 1188 | |
| 1189 | return ret; |
| 1190 | } |
| 1191 | |
| 1192 | static int dwc2_noirq_suspend(struct device *dev) |
| 1193 | { |
| 1194 | struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev); |
| 1195 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 1196 | |
| 1197 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 1198 | | (USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR | USB_ID_WAKE_CLR |
| 1199 | | USB_VBUS_WAKE_EN | USB_LINEST_WAKE_EN | USB_ID_WAKE_EN), |
| 1200 | apmu_base + APMU_USB_WAKE_CLR); |
| 1201 | |
| 1202 | if (dwc2->allow_suspend) { |
| 1203 | if (dwc2->lx_state == DWC2_L2) |
| 1204 | usb_phy_set_suspend2(dwc2->uphy, 1); |
| 1205 | else |
| 1206 | pr_info("dwc2 lx_state: %d\n", dwc2->lx_state); |
| 1207 | } |
| 1208 | |
| 1209 | enable_irq_wake(dwc2->vbus_irq); |
| 1210 | |
| 1211 | dwc2_release_pm_qos(); |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | static int dwc2_noirq_resume(struct device *dev) |
| 1216 | { |
| 1217 | volatile u32 value; |
| 1218 | struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev); |
| 1219 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 1220 | |
| 1221 | if (dwc2->vbus_active) |
| 1222 | dwc2_acquire_pm_qos(); |
| 1223 | |
| 1224 | disable_irq_wake(dwc2->vbus_irq); |
| 1225 | if (dwc2->allow_suspend) { |
| 1226 | if (dwc2->vbus_active) { |
| 1227 | usb_phy_set_suspend2(dwc2->uphy, 0); |
| 1228 | } else { |
| 1229 | pr_info("dwc2 vbus off, lx_state: %d\n", dwc2->lx_state); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | /* clear linestat wakeup and disable linestat/pmu wake en */ |
| 1234 | value = readl(apmu_base + APMU_USB_WAKE_CLR); |
| 1235 | value |= (USB_LINEST_WAKE_CLR); |
| 1236 | writel(value, apmu_base + APMU_USB_WAKE_CLR); |
| 1237 | udelay(50); |
| 1238 | value = readl(apmu_base + APMU_USB_WAKE_CLR); |
| 1239 | value &= ~(USB_LINEST_WAKE_EN); |
| 1240 | writel(value, apmu_base + APMU_USB_WAKE_CLR); |
| 1241 | |
| 1242 | dev_info(dwc2->dev, "dwc2 resume\n"); |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static const struct dev_pm_ops dwc2_dev_pm_ops = { |
| 1247 | SET_SYSTEM_SLEEP_PM_OPS(dwc2_suspend, dwc2_resume) |
| 1248 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dwc2_noirq_suspend, dwc2_noirq_resume) |
| 1249 | }; |
| 1250 | |
| 1251 | static struct platform_driver dwc2_platform_driver = { |
| 1252 | .driver = { |
| 1253 | .name = dwc2_driver_name, |
| 1254 | .of_match_table = dwc2_of_match_table, |
| 1255 | .pm = &dwc2_dev_pm_ops, |
| 1256 | }, |
| 1257 | .probe = dwc2_driver_probe, |
| 1258 | .remove = dwc2_driver_remove, |
| 1259 | .shutdown = dwc2_driver_shutdown, |
| 1260 | }; |
| 1261 | |
| 1262 | module_platform_driver(dwc2_platform_driver); |
| 1263 | |
| 1264 | MODULE_DESCRIPTION("DESIGNWARE HS OTG Platform Glue"); |
| 1265 | MODULE_AUTHOR("Matthijs Kooijman <matthijs@stdin.nl>"); |
| 1266 | MODULE_LICENSE("Dual BSD/GPL"); |