Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1 | /** |
| 2 | * core.c - DesignWare USB3 DRD Controller Core file |
| 3 | * |
| 4 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 5 | * |
| 6 | * Authors: Felipe Balbi <balbi@ti.com>, |
| 7 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 8 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 of |
| 11 | * the License as published by the Free Software Foundation. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 12 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 17 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 20 | */ |
| 21 | |
Felipe Balbi | fa0ea13 | 2014-09-19 15:51:11 -0500 | [diff] [blame] | 22 | #include <linux/version.h> |
Felipe Balbi | a72e658 | 2011-09-05 13:37:28 +0300 | [diff] [blame] | 23 | #include <linux/module.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/pm_runtime.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/ioport.h> |
| 31 | #include <linux/io.h> |
| 32 | #include <linux/list.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/dma-mapping.h> |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 35 | #include <linux/of.h> |
Heikki Krogerus | 404905a | 2014-09-25 10:57:02 +0300 | [diff] [blame] | 36 | #include <linux/acpi.h> |
Sekhar Nori | 6344475 | 2015-08-31 21:09:08 +0530 | [diff] [blame] | 37 | #include <linux/pinctrl/consumer.h> |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 38 | #include <linux/irq.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 39 | |
| 40 | #include <linux/usb/ch9.h> |
| 41 | #include <linux/usb/gadget.h> |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 42 | #include <linux/usb/of.h> |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 43 | #include <linux/usb/otg.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 44 | |
| 45 | #include "core.h" |
| 46 | #include "gadget.h" |
| 47 | #include "io.h" |
| 48 | |
| 49 | #include "debug.h" |
| 50 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 51 | #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */ |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 52 | |
Mayank Rana | 861da2b | 2016-07-13 13:47:57 -0700 | [diff] [blame] | 53 | static int count; |
| 54 | static struct dwc3 *dwc3_instance[DWC_CTRL_COUNT]; |
| 55 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 56 | void dwc3_usb3_phy_suspend(struct dwc3 *dwc, int suspend) |
| 57 | { |
| 58 | u32 reg; |
| 59 | |
| 60 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 61 | |
| 62 | if (suspend) |
| 63 | reg |= DWC3_GUSB3PIPECTL_SUSPHY; |
| 64 | else |
| 65 | reg &= ~DWC3_GUSB3PIPECTL_SUSPHY; |
| 66 | |
| 67 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 68 | } |
| 69 | |
Thinh Nguyen | 9d6173e | 2016-09-06 19:22:03 -0700 | [diff] [blame] | 70 | /** |
| 71 | * dwc3_get_dr_mode - Validates and sets dr_mode |
| 72 | * @dwc: pointer to our context structure |
| 73 | */ |
| 74 | static int dwc3_get_dr_mode(struct dwc3 *dwc) |
| 75 | { |
| 76 | enum usb_dr_mode mode; |
| 77 | struct device *dev = dwc->dev; |
| 78 | unsigned int hw_mode; |
| 79 | |
Thinh Nguyen | 9d6173e | 2016-09-06 19:22:03 -0700 | [diff] [blame] | 80 | |
Mayank Rana | fb9cd93 | 2016-11-03 23:26:38 -0700 | [diff] [blame] | 81 | dwc->is_drd = 0; |
Thinh Nguyen | 9d6173e | 2016-09-06 19:22:03 -0700 | [diff] [blame] | 82 | mode = dwc->dr_mode; |
| 83 | hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0); |
| 84 | |
| 85 | switch (hw_mode) { |
| 86 | case DWC3_GHWPARAMS0_MODE_GADGET: |
| 87 | if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) { |
| 88 | dev_err(dev, |
| 89 | "Controller does not support host mode.\n"); |
| 90 | return -EINVAL; |
| 91 | } |
| 92 | mode = USB_DR_MODE_PERIPHERAL; |
| 93 | break; |
| 94 | case DWC3_GHWPARAMS0_MODE_HOST: |
| 95 | if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) { |
| 96 | dev_err(dev, |
| 97 | "Controller does not support device mode.\n"); |
| 98 | return -EINVAL; |
| 99 | } |
| 100 | mode = USB_DR_MODE_HOST; |
| 101 | break; |
| 102 | default: |
| 103 | if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) |
| 104 | mode = USB_DR_MODE_HOST; |
| 105 | else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) |
| 106 | mode = USB_DR_MODE_PERIPHERAL; |
| 107 | } |
| 108 | |
| 109 | if (mode != dwc->dr_mode) { |
| 110 | dev_warn(dev, |
| 111 | "Configuration mismatch. dr_mode forced to %s\n", |
| 112 | mode == USB_DR_MODE_HOST ? "host" : "gadget"); |
| 113 | |
| 114 | dwc->dr_mode = mode; |
| 115 | } |
| 116 | |
Mayank Rana | fb9cd93 | 2016-11-03 23:26:38 -0700 | [diff] [blame] | 117 | if (dwc->dr_mode == USB_DR_MODE_OTG) |
| 118 | dwc->is_drd = 1; |
| 119 | |
Thinh Nguyen | 9d6173e | 2016-09-06 19:22:03 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 123 | void dwc3_set_mode(struct dwc3 *dwc, u32 mode) |
| 124 | { |
| 125 | u32 reg; |
| 126 | |
| 127 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 128 | reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); |
| 129 | reg |= DWC3_GCTL_PRTCAPDIR(mode); |
| 130 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Set this bit so that device attempts three more times at SS, even |
| 134 | * if it failed previously to operate in SS mode. |
| 135 | */ |
| 136 | reg |= DWC3_GCTL_U2RSTECN; |
| 137 | reg &= ~(DWC3_GCTL_SOFITPSYNC); |
| 138 | reg &= ~(DWC3_GCTL_PWRDNSCALEMASK); |
| 139 | reg |= DWC3_GCTL_PWRDNSCALE(2); |
| 140 | reg |= DWC3_GCTL_U2EXIT_LFPS; |
| 141 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 142 | |
| 143 | if (mode == DWC3_GCTL_PRTCAP_OTG || mode == DWC3_GCTL_PRTCAP_HOST) { |
| 144 | /* |
| 145 | * Allow ITP generated off of ref clk based counter instead |
| 146 | * of UTMI/ULPI clk based counter, when superspeed only is |
| 147 | * active so that UTMI/ULPI PHY can be suspened. |
| 148 | * |
| 149 | * Starting with revision 2.50A, GFLADJ_REFCLK_LPM_SEL is used |
| 150 | * instead. |
| 151 | */ |
| 152 | if (dwc->revision < DWC3_REVISION_250A) { |
| 153 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 154 | reg |= DWC3_GCTL_SOFITPSYNC; |
| 155 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 156 | } else { |
| 157 | reg = dwc3_readl(dwc->regs, DWC3_GFLADJ); |
| 158 | reg |= DWC3_GFLADJ_REFCLK_LPM_SEL; |
| 159 | dwc3_writel(dwc->regs, DWC3_GFLADJ, reg); |
| 160 | } |
| 161 | } |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 162 | } |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 163 | |
Felipe Balbi | cf6d867 | 2016-04-14 15:03:39 +0300 | [diff] [blame] | 164 | u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type) |
| 165 | { |
| 166 | struct dwc3 *dwc = dep->dwc; |
| 167 | u32 reg; |
| 168 | |
| 169 | dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE, |
| 170 | DWC3_GDBGFIFOSPACE_NUM(dep->number) | |
| 171 | DWC3_GDBGFIFOSPACE_TYPE(type)); |
| 172 | |
| 173 | reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE); |
| 174 | |
| 175 | return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg); |
| 176 | } |
| 177 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 178 | /** |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 179 | * Peforms initialization of HS and SS PHYs. |
| 180 | * If used as a part of POR or init sequence it is recommended |
| 181 | * that we should perform hard reset of the PHYs prior to invoking |
| 182 | * this function. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 183 | * @dwc: pointer to our context structure |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 184 | */ |
| 185 | static int dwc3_init_usb_phys(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 186 | { |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 187 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 188 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 189 | /* Bring up PHYs */ |
| 190 | ret = usb_phy_init(dwc->usb2_phy); |
| 191 | if (ret) { |
| 192 | pr_err("%s: usb_phy_init(dwc->usb2_phy) returned %d\n", |
| 193 | __func__, ret); |
| 194 | return ret; |
| 195 | } |
| 196 | |
Hemant Kumar | de1df69 | 2016-04-26 19:36:48 -0700 | [diff] [blame] | 197 | if (dwc->maximum_speed == USB_SPEED_HIGH) |
| 198 | goto generic_phy_init; |
| 199 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 200 | ret = usb_phy_init(dwc->usb3_phy); |
| 201 | if (ret == -EBUSY) { |
| 202 | /* |
| 203 | * Setting Max speed as high when USB3 PHY initialiation |
| 204 | * is failing and USB superspeed can't be supported. |
| 205 | */ |
| 206 | dwc->maximum_speed = USB_SPEED_HIGH; |
| 207 | } else if (ret) { |
| 208 | pr_err("%s: usb_phy_init(dwc->usb3_phy) returned %d\n", |
| 209 | __func__, ret); |
| 210 | return ret; |
| 211 | } |
Hemant Kumar | de1df69 | 2016-04-26 19:36:48 -0700 | [diff] [blame] | 212 | |
| 213 | generic_phy_init: |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 214 | ret = phy_init(dwc->usb2_generic_phy); |
| 215 | if (ret < 0) |
| 216 | return ret; |
| 217 | |
| 218 | ret = phy_init(dwc->usb3_generic_phy); |
| 219 | if (ret < 0) { |
| 220 | phy_exit(dwc->usb2_generic_phy); |
| 221 | return ret; |
| 222 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 223 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 226 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 227 | /** |
| 228 | * dwc3_core_reset - Issues core soft reset and PHY reset |
| 229 | * @dwc: pointer to our context structure |
| 230 | */ |
| 231 | static int dwc3_core_reset(struct dwc3 *dwc) |
| 232 | { |
| 233 | int ret; |
Mayank Rana | f8ebb7f | 2016-09-08 11:09:37 -0700 | [diff] [blame] | 234 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 235 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 236 | /* Reset PHYs */ |
| 237 | usb_phy_reset(dwc->usb2_phy); |
Hemant Kumar | de1df69 | 2016-04-26 19:36:48 -0700 | [diff] [blame] | 238 | |
| 239 | if (dwc->maximum_speed == USB_SPEED_SUPER) |
| 240 | usb_phy_reset(dwc->usb3_phy); |
Pratyush Anand | 45627ac | 2012-06-21 17:44:28 +0530 | [diff] [blame] | 241 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 242 | /* Initialize PHYs */ |
| 243 | ret = dwc3_init_usb_phys(dwc); |
| 244 | if (ret) { |
| 245 | pr_err("%s: dwc3_init_phys returned %d\n", |
| 246 | __func__, ret); |
| 247 | return ret; |
| 248 | } |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 249 | |
Mayank Rana | f8ebb7f | 2016-09-08 11:09:37 -0700 | [diff] [blame] | 250 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 251 | reg &= ~DWC3_GUSB3PIPECTL_DELAYP1TRANS; |
Hemant Kumar | 58eb1df | 2017-01-27 14:51:07 -0800 | [diff] [blame] | 252 | |
| 253 | /* core exits U1/U2/U3 only in PHY power state P1/P2/P3 respectively */ |
| 254 | if (dwc->revision <= DWC3_REVISION_310A) |
| 255 | reg |= DWC3_GUSB3PIPECTL_UX_EXIT_IN_PX; |
| 256 | |
Mayank Rana | f8ebb7f | 2016-09-08 11:09:37 -0700 | [diff] [blame] | 257 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 258 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 259 | dwc3_notify_event(dwc, DWC3_CONTROLLER_RESET_EVENT); |
| 260 | |
| 261 | dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_RESET_EVENT); |
| 262 | |
| 263 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
Heikki Krogerus | c5cc74e | 2015-05-13 15:26:47 +0300 | [diff] [blame] | 267 | * dwc3_soft_reset - Issue soft reset |
| 268 | * @dwc: Pointer to our controller context structure |
| 269 | */ |
| 270 | static int dwc3_soft_reset(struct dwc3 *dwc) |
| 271 | { |
| 272 | unsigned long timeout; |
| 273 | u32 reg; |
| 274 | |
| 275 | timeout = jiffies + msecs_to_jiffies(500); |
| 276 | dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); |
| 277 | do { |
| 278 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 279 | if (!(reg & DWC3_DCTL_CSFTRST)) |
| 280 | break; |
| 281 | |
| 282 | if (time_after(jiffies, timeout)) { |
| 283 | dev_err(dwc->dev, "Reset Timed Out\n"); |
| 284 | return -ETIMEDOUT; |
| 285 | } |
| 286 | |
| 287 | cpu_relax(); |
| 288 | } while (true); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Nikhil Badola | db2be4e | 2015-09-04 10:15:58 +0530 | [diff] [blame] | 293 | /* |
| 294 | * dwc3_frame_length_adjustment - Adjusts frame length if required |
| 295 | * @dwc3: Pointer to our controller context structure |
Nikhil Badola | db2be4e | 2015-09-04 10:15:58 +0530 | [diff] [blame] | 296 | */ |
Felipe Balbi | bcdb327 | 2016-05-16 10:42:23 +0300 | [diff] [blame] | 297 | static void dwc3_frame_length_adjustment(struct dwc3 *dwc) |
Nikhil Badola | db2be4e | 2015-09-04 10:15:58 +0530 | [diff] [blame] | 298 | { |
| 299 | u32 reg; |
| 300 | u32 dft; |
| 301 | |
| 302 | if (dwc->revision < DWC3_REVISION_250A) |
| 303 | return; |
| 304 | |
Felipe Balbi | bcdb327 | 2016-05-16 10:42:23 +0300 | [diff] [blame] | 305 | if (dwc->fladj == 0) |
Nikhil Badola | db2be4e | 2015-09-04 10:15:58 +0530 | [diff] [blame] | 306 | return; |
| 307 | |
| 308 | reg = dwc3_readl(dwc->regs, DWC3_GFLADJ); |
| 309 | dft = reg & DWC3_GFLADJ_30MHZ_MASK; |
Felipe Balbi | bcdb327 | 2016-05-16 10:42:23 +0300 | [diff] [blame] | 310 | if (!dev_WARN_ONCE(dwc->dev, dft == dwc->fladj, |
Nikhil Badola | db2be4e | 2015-09-04 10:15:58 +0530 | [diff] [blame] | 311 | "request value same as default, ignoring\n")) { |
| 312 | reg &= ~DWC3_GFLADJ_30MHZ_MASK; |
Felipe Balbi | bcdb327 | 2016-05-16 10:42:23 +0300 | [diff] [blame] | 313 | reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj; |
Nikhil Badola | db2be4e | 2015-09-04 10:15:58 +0530 | [diff] [blame] | 314 | dwc3_writel(dwc->regs, DWC3_GFLADJ, reg); |
| 315 | } |
| 316 | } |
| 317 | |
Heikki Krogerus | c5cc74e | 2015-05-13 15:26:47 +0300 | [diff] [blame] | 318 | /** |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 319 | * dwc3_free_one_event_buffer - Frees one event buffer |
| 320 | * @dwc: Pointer to our controller context structure |
| 321 | * @evt: Pointer to event buffer to be freed |
| 322 | */ |
| 323 | static void dwc3_free_one_event_buffer(struct dwc3 *dwc, |
| 324 | struct dwc3_event_buffer *evt) |
| 325 | { |
Arnd Bergmann | 42695fc | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 326 | dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /** |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 330 | * dwc3_alloc_one_event_buffer - Allocates one event buffer structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 331 | * @dwc: Pointer to our controller context structure |
| 332 | * @length: size of the event buffer |
| 333 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 334 | * Returns a pointer to the allocated event buffer structure on success |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 335 | * otherwise ERR_PTR(errno). |
| 336 | */ |
Felipe Balbi | 67d0b50 | 2013-02-22 16:31:07 +0200 | [diff] [blame] | 337 | static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc, |
| 338 | unsigned length) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 339 | { |
| 340 | struct dwc3_event_buffer *evt; |
| 341 | |
Felipe Balbi | 380f0d2 | 2012-10-11 13:48:36 +0300 | [diff] [blame] | 342 | evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 343 | if (!evt) |
| 344 | return ERR_PTR(-ENOMEM); |
| 345 | |
| 346 | evt->dwc = dwc; |
| 347 | evt->length = length; |
Arnd Bergmann | 42695fc | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 348 | evt->buf = dma_alloc_coherent(dwc->sysdev, length, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 349 | &evt->dma, GFP_KERNEL); |
Felipe Balbi | e32672f | 2012-11-08 15:26:41 +0200 | [diff] [blame] | 350 | if (!evt->buf) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 351 | return ERR_PTR(-ENOMEM); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 352 | |
| 353 | return evt; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * dwc3_free_event_buffers - frees all allocated event buffers |
| 358 | * @dwc: Pointer to our controller context structure |
| 359 | */ |
| 360 | static void dwc3_free_event_buffers(struct dwc3 *dwc) |
| 361 | { |
| 362 | struct dwc3_event_buffer *evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 363 | |
Felipe Balbi | 696c8b1 | 2016-03-30 09:37:03 +0300 | [diff] [blame] | 364 | evt = dwc->ev_buf; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 365 | if (evt) |
| 366 | dwc3_free_one_event_buffer(dwc, evt); |
Mayank Rana | f4918d3 | 2016-12-15 13:35:55 -0800 | [diff] [blame] | 367 | |
| 368 | /* free GSI related event buffers */ |
| 369 | dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_FREE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
| 373 | * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 374 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 375 | * @length: size of event buffer |
| 376 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 377 | * Returns 0 on success otherwise negative errno. In the error case, dwc |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 378 | * may contain some buffers allocated but not all which were requested. |
| 379 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 380 | static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 381 | { |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 382 | struct dwc3_event_buffer *evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 383 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 384 | evt = dwc3_alloc_one_event_buffer(dwc, length); |
| 385 | if (IS_ERR(evt)) { |
| 386 | dev_err(dwc->dev, "can't allocate event buffer\n"); |
| 387 | return PTR_ERR(evt); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 388 | } |
Felipe Balbi | 696c8b1 | 2016-03-30 09:37:03 +0300 | [diff] [blame] | 389 | dwc->ev_buf = evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 390 | |
Mayank Rana | f4918d3 | 2016-12-15 13:35:55 -0800 | [diff] [blame] | 391 | /* alloc GSI related event buffers */ |
| 392 | dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_ALLOC); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * dwc3_event_buffers_setup - setup our allocated event buffers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 398 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 399 | * |
| 400 | * Returns 0 on success otherwise negative errno. |
| 401 | */ |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 402 | int dwc3_event_buffers_setup(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 403 | { |
| 404 | struct dwc3_event_buffer *evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 405 | |
Felipe Balbi | 696c8b1 | 2016-03-30 09:37:03 +0300 | [diff] [blame] | 406 | evt = dwc->ev_buf; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 407 | dwc3_trace(trace_dwc3_core, |
| 408 | "Event buf %p dma %08llx length %d\n", |
| 409 | evt->buf, (unsigned long long) evt->dma, |
| 410 | evt->length); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 411 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 412 | evt->lpos = 0; |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 413 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 414 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), |
| 415 | lower_32_bits(evt->dma)); |
| 416 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), |
| 417 | upper_32_bits(evt->dma)); |
| 418 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), |
| 419 | DWC3_GEVNTSIZ_SIZE(evt->length)); |
| 420 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 421 | |
Mayank Rana | f4918d3 | 2016-12-15 13:35:55 -0800 | [diff] [blame] | 422 | /* setup GSI related event buffers */ |
| 423 | dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_SETUP); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) |
| 428 | { |
| 429 | struct dwc3_event_buffer *evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 430 | |
Felipe Balbi | 696c8b1 | 2016-03-30 09:37:03 +0300 | [diff] [blame] | 431 | evt = dwc->ev_buf; |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 432 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 433 | evt->lpos = 0; |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 434 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 435 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0); |
| 436 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0); |
| 437 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK |
| 438 | | DWC3_GEVNTSIZ_SIZE(0)); |
| 439 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 0); |
Mayank Rana | f4918d3 | 2016-12-15 13:35:55 -0800 | [diff] [blame] | 440 | |
| 441 | /* cleanup GSI related event buffers */ |
| 442 | dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_CLEANUP); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 443 | } |
| 444 | |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 445 | static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc) |
| 446 | { |
| 447 | if (!dwc->has_hibernation) |
| 448 | return 0; |
| 449 | |
| 450 | if (!dwc->nr_scratch) |
| 451 | return 0; |
| 452 | |
| 453 | dwc->scratchbuf = kmalloc_array(dwc->nr_scratch, |
| 454 | DWC3_SCRATCHBUF_SIZE, GFP_KERNEL); |
| 455 | if (!dwc->scratchbuf) |
| 456 | return -ENOMEM; |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int dwc3_setup_scratch_buffers(struct dwc3 *dwc) |
| 462 | { |
| 463 | dma_addr_t scratch_addr; |
| 464 | u32 param; |
| 465 | int ret; |
| 466 | |
| 467 | if (!dwc->has_hibernation) |
| 468 | return 0; |
| 469 | |
| 470 | if (!dwc->nr_scratch) |
| 471 | return 0; |
| 472 | |
| 473 | /* should never fall here */ |
| 474 | if (!WARN_ON(dwc->scratchbuf)) |
| 475 | return 0; |
| 476 | |
Arnd Bergmann | 42695fc | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 477 | scratch_addr = dma_map_single(dwc->sysdev, dwc->scratchbuf, |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 478 | dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE, |
| 479 | DMA_BIDIRECTIONAL); |
Arnd Bergmann | 42695fc | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 480 | if (dma_mapping_error(dwc->sysdev, scratch_addr)) { |
| 481 | dev_err(dwc->sysdev, "failed to map scratch buffer\n"); |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 482 | ret = -EFAULT; |
| 483 | goto err0; |
| 484 | } |
| 485 | |
| 486 | dwc->scratch_addr = scratch_addr; |
| 487 | |
| 488 | param = lower_32_bits(scratch_addr); |
| 489 | |
| 490 | ret = dwc3_send_gadget_generic_command(dwc, |
| 491 | DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param); |
| 492 | if (ret < 0) |
| 493 | goto err1; |
| 494 | |
| 495 | param = upper_32_bits(scratch_addr); |
| 496 | |
| 497 | ret = dwc3_send_gadget_generic_command(dwc, |
| 498 | DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param); |
| 499 | if (ret < 0) |
| 500 | goto err1; |
| 501 | |
| 502 | return 0; |
| 503 | |
| 504 | err1: |
Arnd Bergmann | 42695fc | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 505 | dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch * |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 506 | DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); |
| 507 | |
| 508 | err0: |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | static void dwc3_free_scratch_buffers(struct dwc3 *dwc) |
| 513 | { |
| 514 | if (!dwc->has_hibernation) |
| 515 | return; |
| 516 | |
| 517 | if (!dwc->nr_scratch) |
| 518 | return; |
| 519 | |
| 520 | /* should never fall here */ |
| 521 | if (!WARN_ON(dwc->scratchbuf)) |
| 522 | return; |
| 523 | |
Arnd Bergmann | 42695fc | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 524 | dma_unmap_single(dwc->sysdev, dwc->scratch_addr, dwc->nr_scratch * |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 525 | DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); |
| 526 | kfree(dwc->scratchbuf); |
| 527 | } |
| 528 | |
Felipe Balbi | 789451f6 | 2011-05-05 15:53:10 +0300 | [diff] [blame] | 529 | static void dwc3_core_num_eps(struct dwc3 *dwc) |
| 530 | { |
| 531 | struct dwc3_hwparams *parms = &dwc->hwparams; |
| 532 | |
| 533 | dwc->num_in_eps = DWC3_NUM_IN_EPS(parms); |
| 534 | dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps; |
| 535 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 536 | dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints", |
Felipe Balbi | 789451f6 | 2011-05-05 15:53:10 +0300 | [diff] [blame] | 537 | dwc->num_in_eps, dwc->num_out_eps); |
| 538 | } |
| 539 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 540 | static void dwc3_cache_hwparams(struct dwc3 *dwc) |
Felipe Balbi | 26ceca9 | 2011-09-30 10:58:49 +0300 | [diff] [blame] | 541 | { |
| 542 | struct dwc3_hwparams *parms = &dwc->hwparams; |
| 543 | |
| 544 | parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); |
| 545 | parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); |
| 546 | parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); |
| 547 | parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); |
| 548 | parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); |
| 549 | parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); |
| 550 | parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); |
| 551 | parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); |
| 552 | parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); |
| 553 | } |
| 554 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 555 | /** |
Huang Rui | b5a65c4 | 2014-10-28 19:54:28 +0800 | [diff] [blame] | 556 | * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core |
| 557 | * @dwc: Pointer to our controller context structure |
Heikki Krogerus | 88bc9d1 | 2015-05-13 15:26:51 +0300 | [diff] [blame] | 558 | * |
| 559 | * Returns 0 on success. The USB PHY interfaces are configured but not |
| 560 | * initialized. The PHY interfaces and the PHYs get initialized together with |
| 561 | * the core in dwc3_core_init. |
Huang Rui | b5a65c4 | 2014-10-28 19:54:28 +0800 | [diff] [blame] | 562 | */ |
Heikki Krogerus | 88bc9d1 | 2015-05-13 15:26:51 +0300 | [diff] [blame] | 563 | static int dwc3_phy_setup(struct dwc3 *dwc) |
Huang Rui | b5a65c4 | 2014-10-28 19:54:28 +0800 | [diff] [blame] | 564 | { |
| 565 | u32 reg; |
Heikki Krogerus | 88bc9d1 | 2015-05-13 15:26:51 +0300 | [diff] [blame] | 566 | int ret; |
Huang Rui | b5a65c4 | 2014-10-28 19:54:28 +0800 | [diff] [blame] | 567 | |
| 568 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 569 | |
Huang Rui | 2164a47 | 2014-10-28 19:54:35 +0800 | [diff] [blame] | 570 | /* |
| 571 | * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY |
| 572 | * to '0' during coreConsultant configuration. So default value |
| 573 | * will be '0' when the core is reset. Application needs to set it |
| 574 | * to '1' after the core initialization is completed. |
| 575 | */ |
| 576 | if (dwc->revision > DWC3_REVISION_194A) |
| 577 | reg |= DWC3_GUSB3PIPECTL_SUSPHY; |
| 578 | |
Huang Rui | b5a65c4 | 2014-10-28 19:54:28 +0800 | [diff] [blame] | 579 | if (dwc->u2ss_inp3_quirk) |
| 580 | reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK; |
| 581 | |
Rajesh Bhagat | e58dd35 | 2016-03-14 14:40:50 +0530 | [diff] [blame] | 582 | if (dwc->dis_rxdet_inp3_quirk) |
| 583 | reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3; |
| 584 | |
Huang Rui | df31f5b | 2014-10-28 19:54:29 +0800 | [diff] [blame] | 585 | if (dwc->req_p1p2p3_quirk) |
| 586 | reg |= DWC3_GUSB3PIPECTL_REQP1P2P3; |
| 587 | |
Huang Rui | a2a1d0f | 2014-10-28 19:54:30 +0800 | [diff] [blame] | 588 | if (dwc->del_p1p2p3_quirk) |
| 589 | reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN; |
| 590 | |
Huang Rui | 41c06ff | 2014-10-28 19:54:31 +0800 | [diff] [blame] | 591 | if (dwc->del_phy_power_chg_quirk) |
| 592 | reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE; |
| 593 | |
Huang Rui | fb67afc | 2014-10-28 19:54:32 +0800 | [diff] [blame] | 594 | if (dwc->lfps_filter_quirk) |
| 595 | reg |= DWC3_GUSB3PIPECTL_LFPSFILT; |
| 596 | |
Huang Rui | 14f4ac5 | 2014-10-28 19:54:33 +0800 | [diff] [blame] | 597 | if (dwc->rx_detect_poll_quirk) |
| 598 | reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL; |
| 599 | |
Huang Rui | 6b6a0c9 | 2014-10-31 11:11:12 +0800 | [diff] [blame] | 600 | if (dwc->tx_de_emphasis_quirk) |
| 601 | reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis); |
| 602 | |
Felipe Balbi | cd72f89 | 2014-11-06 11:31:00 -0600 | [diff] [blame] | 603 | if (dwc->dis_u3_susphy_quirk) |
Huang Rui | 59acfa2 | 2014-10-31 11:11:13 +0800 | [diff] [blame] | 604 | reg &= ~DWC3_GUSB3PIPECTL_SUSPHY; |
| 605 | |
William Wu | 00fe081 | 2016-08-16 22:44:39 +0800 | [diff] [blame] | 606 | if (dwc->dis_del_phy_power_chg_quirk) |
| 607 | reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE; |
| 608 | |
Huang Rui | b5a65c4 | 2014-10-28 19:54:28 +0800 | [diff] [blame] | 609 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 610 | |
Huang Rui | 2164a47 | 2014-10-28 19:54:35 +0800 | [diff] [blame] | 611 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 612 | |
Heikki Krogerus | 3e10a2c | 2015-05-13 15:26:49 +0300 | [diff] [blame] | 613 | /* Select the HS PHY interface */ |
| 614 | switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) { |
| 615 | case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI: |
Felipe Balbi | 43cacb0 | 2015-07-01 22:03:09 -0500 | [diff] [blame] | 616 | if (dwc->hsphy_interface && |
| 617 | !strncmp(dwc->hsphy_interface, "utmi", 4)) { |
Heikki Krogerus | 3e10a2c | 2015-05-13 15:26:49 +0300 | [diff] [blame] | 618 | reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI; |
Heikki Krogerus | 88bc9d1 | 2015-05-13 15:26:51 +0300 | [diff] [blame] | 619 | break; |
Felipe Balbi | 43cacb0 | 2015-07-01 22:03:09 -0500 | [diff] [blame] | 620 | } else if (dwc->hsphy_interface && |
| 621 | !strncmp(dwc->hsphy_interface, "ulpi", 4)) { |
Heikki Krogerus | 3e10a2c | 2015-05-13 15:26:49 +0300 | [diff] [blame] | 622 | reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI; |
Heikki Krogerus | 88bc9d1 | 2015-05-13 15:26:51 +0300 | [diff] [blame] | 623 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
Heikki Krogerus | 3e10a2c | 2015-05-13 15:26:49 +0300 | [diff] [blame] | 624 | } else { |
Heikki Krogerus | 88bc9d1 | 2015-05-13 15:26:51 +0300 | [diff] [blame] | 625 | /* Relying on default value. */ |
| 626 | if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI)) |
| 627 | break; |
Heikki Krogerus | 3e10a2c | 2015-05-13 15:26:49 +0300 | [diff] [blame] | 628 | } |
| 629 | /* FALLTHROUGH */ |
Heikki Krogerus | 88bc9d1 | 2015-05-13 15:26:51 +0300 | [diff] [blame] | 630 | case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI: |
| 631 | /* Making sure the interface and PHY are operational */ |
| 632 | ret = dwc3_soft_reset(dwc); |
| 633 | if (ret) |
| 634 | return ret; |
| 635 | |
| 636 | udelay(1); |
| 637 | |
| 638 | ret = dwc3_ulpi_init(dwc); |
| 639 | if (ret) |
| 640 | return ret; |
| 641 | /* FALLTHROUGH */ |
Heikki Krogerus | 3e10a2c | 2015-05-13 15:26:49 +0300 | [diff] [blame] | 642 | default: |
| 643 | break; |
| 644 | } |
| 645 | |
William Wu | 32f2ed8 | 2016-08-16 22:44:38 +0800 | [diff] [blame] | 646 | switch (dwc->hsphy_mode) { |
| 647 | case USBPHY_INTERFACE_MODE_UTMI: |
| 648 | reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK | |
| 649 | DWC3_GUSB2PHYCFG_USBTRDTIM_MASK); |
| 650 | reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) | |
| 651 | DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT); |
| 652 | break; |
| 653 | case USBPHY_INTERFACE_MODE_UTMIW: |
| 654 | reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK | |
| 655 | DWC3_GUSB2PHYCFG_USBTRDTIM_MASK); |
| 656 | reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) | |
| 657 | DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT); |
| 658 | break; |
| 659 | default: |
| 660 | break; |
| 661 | } |
| 662 | |
Huang Rui | 2164a47 | 2014-10-28 19:54:35 +0800 | [diff] [blame] | 663 | /* |
| 664 | * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to |
| 665 | * '0' during coreConsultant configuration. So default value will |
| 666 | * be '0' when the core is reset. Application needs to set it to |
| 667 | * '1' after the core initialization is completed. |
| 668 | */ |
| 669 | if (dwc->revision > DWC3_REVISION_194A) |
| 670 | reg |= DWC3_GUSB2PHYCFG_SUSPHY; |
| 671 | |
Felipe Balbi | cd72f89 | 2014-11-06 11:31:00 -0600 | [diff] [blame] | 672 | if (dwc->dis_u2_susphy_quirk) |
Huang Rui | 0effe0a | 2014-10-31 11:11:14 +0800 | [diff] [blame] | 673 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
| 674 | |
John Youn | ec791d1 | 2015-10-02 20:30:57 -0700 | [diff] [blame] | 675 | if (dwc->dis_enblslpm_quirk) |
| 676 | reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; |
| 677 | |
William Wu | 16199f3 | 2016-08-16 22:44:37 +0800 | [diff] [blame] | 678 | if (dwc->dis_u2_freeclk_exists_quirk) |
| 679 | reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS; |
| 680 | |
Huang Rui | 2164a47 | 2014-10-28 19:54:35 +0800 | [diff] [blame] | 681 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
Heikki Krogerus | 88bc9d1 | 2015-05-13 15:26:51 +0300 | [diff] [blame] | 682 | |
| 683 | return 0; |
Huang Rui | b5a65c4 | 2014-10-28 19:54:28 +0800 | [diff] [blame] | 684 | } |
| 685 | |
Felipe Balbi | c499ff7 | 2016-05-16 10:49:01 +0300 | [diff] [blame] | 686 | static void dwc3_core_exit(struct dwc3 *dwc) |
| 687 | { |
| 688 | dwc3_event_buffers_cleanup(dwc); |
| 689 | |
| 690 | usb_phy_shutdown(dwc->usb2_phy); |
| 691 | usb_phy_shutdown(dwc->usb3_phy); |
| 692 | phy_exit(dwc->usb2_generic_phy); |
| 693 | phy_exit(dwc->usb3_generic_phy); |
| 694 | |
| 695 | usb_phy_set_suspend(dwc->usb2_phy, 1); |
| 696 | usb_phy_set_suspend(dwc->usb3_phy, 1); |
| 697 | phy_power_off(dwc->usb2_generic_phy); |
| 698 | phy_power_off(dwc->usb3_generic_phy); |
| 699 | } |
| 700 | |
Huang Rui | b5a65c4 | 2014-10-28 19:54:28 +0800 | [diff] [blame] | 701 | /** |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 702 | * dwc3_core_init - Low-level initialization of DWC3 Core |
| 703 | * @dwc: Pointer to our controller context structure |
| 704 | * |
| 705 | * Returns 0 on success otherwise negative errno. |
| 706 | */ |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 707 | int dwc3_core_init(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 708 | { |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 709 | u32 hwparams4 = dwc->hwparams.hwparams4; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 710 | u32 reg; |
| 711 | int ret; |
| 712 | |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 713 | reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); |
| 714 | /* This should read as U3 followed by revision number */ |
John Youn | 690fb37 | 2015-09-04 19:15:10 -0700 | [diff] [blame] | 715 | if ((reg & DWC3_GSNPSID_MASK) == 0x55330000) { |
| 716 | /* Detected DWC_usb3 IP */ |
| 717 | dwc->revision = reg; |
| 718 | } else if ((reg & DWC3_GSNPSID_MASK) == 0x33310000) { |
| 719 | /* Detected DWC_usb31 IP */ |
| 720 | dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER); |
| 721 | dwc->revision |= DWC3_REVISION_IS_DWC31; |
| 722 | } else { |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 723 | dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); |
| 724 | ret = -ENODEV; |
| 725 | goto err0; |
| 726 | } |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 727 | |
Felipe Balbi | fa0ea13 | 2014-09-19 15:51:11 -0500 | [diff] [blame] | 728 | /* |
| 729 | * Write Linux Version Code to our GUID register so it's easy to figure |
| 730 | * out which kernel version a bug was found. |
| 731 | */ |
| 732 | dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE); |
| 733 | |
Paul Zimmerman | 0e1e5c4 | 2014-05-23 11:39:24 -0700 | [diff] [blame] | 734 | /* Handle USB2.0-only core configuration */ |
| 735 | if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) == |
| 736 | DWC3_GHWPARAMS3_SSPHY_IFC_DIS) { |
Vamsi Krishna Samavedam | 86ed20b | 2017-01-31 13:55:38 -0800 | [diff] [blame] | 737 | if (dwc->max_hw_supp_speed == USB_SPEED_SUPER) { |
| 738 | dwc->max_hw_supp_speed = USB_SPEED_HIGH; |
| 739 | dwc->maximum_speed = dwc->max_hw_supp_speed; |
| 740 | } |
Paul Zimmerman | 0e1e5c4 | 2014-05-23 11:39:24 -0700 | [diff] [blame] | 741 | } |
| 742 | |
John Youn | 7ecdb05 | 2016-11-14 12:32:45 -0800 | [diff] [blame] | 743 | /* |
| 744 | * Workaround for STAR 9000961433 which affects only version |
| 745 | * 3.00a of the DWC_usb3 core. This prevents the controller |
| 746 | * interrupt from being masked while handling events. IMOD |
| 747 | * allows us to work around this issue. Enable it for the |
| 748 | * affected version. |
| 749 | */ |
| 750 | if (!dwc->imod_interval && (dwc->revision == DWC3_REVISION_300A)) |
| 751 | dwc->imod_interval = 1; |
| 752 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 753 | /* issue device SoftReset too */ |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 754 | ret = dwc3_core_reset(dwc); |
Heikki Krogerus | c5cc74e | 2015-05-13 15:26:47 +0300 | [diff] [blame] | 755 | if (ret) |
| 756 | goto err0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 757 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 758 | /* issue device SoftReset too */ |
| 759 | ret = dwc3_soft_reset(dwc); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 760 | if (ret) |
| 761 | goto err0; |
Pratyush Anand | 58a0f23 | 2012-06-21 17:44:29 +0530 | [diff] [blame] | 762 | |
Felipe Balbi | c499ff7 | 2016-05-16 10:49:01 +0300 | [diff] [blame] | 763 | ret = dwc3_phy_setup(dwc); |
| 764 | if (ret) |
| 765 | goto err0; |
| 766 | |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 767 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
Paul Zimmerman | 3e87c42 | 2012-02-24 17:32:13 -0800 | [diff] [blame] | 768 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 769 | |
Sebastian Andrzej Siewior | 164d773 | 2011-11-24 11:22:05 +0100 | [diff] [blame] | 770 | switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 771 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
Felipe Balbi | 32a4a13 | 2014-02-25 14:00:13 -0600 | [diff] [blame] | 772 | /** |
| 773 | * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an |
| 774 | * issue which would cause xHCI compliance tests to fail. |
| 775 | * |
| 776 | * Because of that we cannot enable clock gating on such |
| 777 | * configurations. |
| 778 | * |
| 779 | * Refers to: |
| 780 | * |
| 781 | * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based |
| 782 | * SOF/ITP Mode Used |
| 783 | */ |
| 784 | if ((dwc->dr_mode == USB_DR_MODE_HOST || |
| 785 | dwc->dr_mode == USB_DR_MODE_OTG) && |
| 786 | (dwc->revision >= DWC3_REVISION_210A && |
| 787 | dwc->revision <= DWC3_REVISION_250A)) |
| 788 | reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC; |
| 789 | else |
| 790 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 791 | break; |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 792 | case DWC3_GHWPARAMS1_EN_PWROPT_HIB: |
| 793 | /* enable hibernation here */ |
| 794 | dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4); |
Huang Rui | 2eac399 | 2014-10-28 19:54:22 +0800 | [diff] [blame] | 795 | |
| 796 | /* |
| 797 | * REVISIT Enabling this bit so that host-mode hibernation |
| 798 | * will work. Device-mode hibernation is not yet implemented. |
| 799 | */ |
| 800 | reg |= DWC3_GCTL_GBLHIBERNATIONEN; |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 801 | break; |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 802 | default: |
Felipe Balbi | 1407bf1 | 2015-11-16 16:06:37 -0600 | [diff] [blame] | 803 | dwc3_trace(trace_dwc3_core, "No power optimization available\n"); |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Huang Rui | 946bd57 | 2014-10-28 19:54:23 +0800 | [diff] [blame] | 806 | /* check if current dwc3 is on simulation board */ |
| 807 | if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) { |
Felipe Balbi | 1407bf1 | 2015-11-16 16:06:37 -0600 | [diff] [blame] | 808 | dwc3_trace(trace_dwc3_core, |
| 809 | "running on FPGA platform\n"); |
Huang Rui | 946bd57 | 2014-10-28 19:54:23 +0800 | [diff] [blame] | 810 | dwc->is_fpga = true; |
| 811 | } |
| 812 | |
Huang Rui | 3b81221 | 2014-10-28 19:54:25 +0800 | [diff] [blame] | 813 | WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga, |
| 814 | "disable_scramble cannot be used on non-FPGA builds\n"); |
| 815 | |
| 816 | if (dwc->disable_scramble_quirk && dwc->is_fpga) |
| 817 | reg |= DWC3_GCTL_DISSCRAMBLE; |
| 818 | else |
| 819 | reg &= ~DWC3_GCTL_DISSCRAMBLE; |
| 820 | |
Huang Rui | 9a5b2f3 | 2014-10-28 19:54:27 +0800 | [diff] [blame] | 821 | if (dwc->u2exit_lfps_quirk) |
| 822 | reg |= DWC3_GCTL_U2EXIT_LFPS; |
| 823 | |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 824 | /* |
| 825 | * WORKAROUND: DWC3 revisions <1.90a have a bug |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 826 | * where the device can fail to connect at SuperSpeed |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 827 | * and falls back to high-speed mode which causes |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 828 | * the device to enter a Connect/Disconnect loop |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 829 | */ |
| 830 | if (dwc->revision < DWC3_REVISION_190A) |
| 831 | reg |= DWC3_GCTL_U2RSTECN; |
| 832 | |
Mayank Rana | fb9cd93 | 2016-11-03 23:26:38 -0700 | [diff] [blame] | 833 | ret = dwc3_get_dr_mode(dwc); |
| 834 | if (ret) |
| 835 | goto err0; |
| 836 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 837 | dwc3_core_num_eps(dwc); |
| 838 | |
| 839 | /* |
| 840 | * Disable clock gating to work around a known HW bug that causes the |
| 841 | * internal RAM clock to get stuck when entering low power modes. |
| 842 | */ |
| 843 | if (dwc->disable_clk_gating) { |
| 844 | dev_dbg(dwc->dev, "Disabling controller clock gating.\n"); |
| 845 | reg |= DWC3_GCTL_DSBLCLKGTNG; |
| 846 | } |
| 847 | |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 848 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 849 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 850 | ret = dwc3_alloc_scratch_buffers(dwc); |
| 851 | if (ret) |
| 852 | goto err1; |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 853 | |
| 854 | ret = dwc3_setup_scratch_buffers(dwc); |
| 855 | if (ret) |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 856 | goto err2; |
Felipe Balbi | c499ff7 | 2016-05-16 10:49:01 +0300 | [diff] [blame] | 857 | |
| 858 | /* Adjust Frame Length */ |
| 859 | dwc3_frame_length_adjustment(dwc); |
| 860 | |
| 861 | usb_phy_set_suspend(dwc->usb2_phy, 0); |
| 862 | usb_phy_set_suspend(dwc->usb3_phy, 0); |
| 863 | ret = phy_power_on(dwc->usb2_generic_phy); |
| 864 | if (ret < 0) |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 865 | goto err2; |
| 866 | |
Baolin Wang | 00af623 | 2016-07-15 17:13:27 +0800 | [diff] [blame] | 867 | switch (dwc->dr_mode) { |
| 868 | case USB_DR_MODE_PERIPHERAL: |
| 869 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); |
| 870 | break; |
| 871 | case USB_DR_MODE_HOST: |
| 872 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); |
| 873 | break; |
| 874 | case USB_DR_MODE_OTG: |
| 875 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); |
| 876 | break; |
| 877 | default: |
| 878 | dev_warn(dwc->dev, "Unsupported mode %d\n", dwc->dr_mode); |
| 879 | break; |
| 880 | } |
| 881 | |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 882 | /* |
| 883 | * ENDXFER polling is available on version 3.10a and later of |
| 884 | * the DWC_usb3 controller. It is NOT available in the |
| 885 | * DWC_usb31 controller. |
| 886 | */ |
| 887 | if (!dwc3_is_usb31(dwc) && dwc->revision >= DWC3_REVISION_310A) { |
| 888 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL2); |
| 889 | reg |= DWC3_GUCTL2_RST_ACTBITLATER; |
| 890 | dwc3_writel(dwc->regs, DWC3_GUCTL2, reg); |
| 891 | } |
| 892 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 893 | return 0; |
| 894 | |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 895 | err2: |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 896 | dwc3_free_scratch_buffers(dwc); |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 897 | err1: |
| 898 | usb_phy_shutdown(dwc->usb2_phy); |
| 899 | usb_phy_shutdown(dwc->usb3_phy); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 900 | phy_exit(dwc->usb2_generic_phy); |
| 901 | phy_exit(dwc->usb3_generic_phy); |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 902 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 903 | err0: |
| 904 | return ret; |
| 905 | } |
| 906 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 907 | static int dwc3_core_get_phy(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 908 | { |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 909 | struct device *dev = dwc->dev; |
Felipe Balbi | 941ea36 | 2013-07-31 09:21:25 +0300 | [diff] [blame] | 910 | struct device_node *node = dev->of_node; |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 911 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 912 | |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 913 | if (node) { |
| 914 | dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0); |
| 915 | dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1); |
Felipe Balbi | bb67490 | 2013-08-14 13:21:23 -0500 | [diff] [blame] | 916 | } else { |
| 917 | dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); |
| 918 | dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3); |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 919 | } |
| 920 | |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 921 | if (IS_ERR(dwc->usb2_phy)) { |
| 922 | ret = PTR_ERR(dwc->usb2_phy); |
Kishon Vijay Abraham I | 122f06e | 2014-03-03 17:08:10 +0530 | [diff] [blame] | 923 | if (ret == -ENXIO || ret == -ENODEV) { |
| 924 | dwc->usb2_phy = NULL; |
| 925 | } else if (ret == -EPROBE_DEFER) { |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 926 | return ret; |
Kishon Vijay Abraham I | 122f06e | 2014-03-03 17:08:10 +0530 | [diff] [blame] | 927 | } else { |
| 928 | dev_err(dev, "no usb2 phy configured\n"); |
| 929 | return ret; |
| 930 | } |
Felipe Balbi | 51e1e7b | 2012-07-19 14:09:48 +0300 | [diff] [blame] | 931 | } |
| 932 | |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 933 | if (IS_ERR(dwc->usb3_phy)) { |
Ruchika Kharwar | 315955d7 | 2013-07-04 00:59:34 -0500 | [diff] [blame] | 934 | ret = PTR_ERR(dwc->usb3_phy); |
Kishon Vijay Abraham I | 122f06e | 2014-03-03 17:08:10 +0530 | [diff] [blame] | 935 | if (ret == -ENXIO || ret == -ENODEV) { |
| 936 | dwc->usb3_phy = NULL; |
| 937 | } else if (ret == -EPROBE_DEFER) { |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 938 | return ret; |
Kishon Vijay Abraham I | 122f06e | 2014-03-03 17:08:10 +0530 | [diff] [blame] | 939 | } else { |
| 940 | dev_err(dev, "no usb3 phy configured\n"); |
| 941 | return ret; |
| 942 | } |
Felipe Balbi | 51e1e7b | 2012-07-19 14:09:48 +0300 | [diff] [blame] | 943 | } |
| 944 | |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 945 | dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy"); |
| 946 | if (IS_ERR(dwc->usb2_generic_phy)) { |
| 947 | ret = PTR_ERR(dwc->usb2_generic_phy); |
| 948 | if (ret == -ENOSYS || ret == -ENODEV) { |
| 949 | dwc->usb2_generic_phy = NULL; |
| 950 | } else if (ret == -EPROBE_DEFER) { |
| 951 | return ret; |
| 952 | } else { |
| 953 | dev_err(dev, "no usb2 phy configured\n"); |
| 954 | return ret; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy"); |
| 959 | if (IS_ERR(dwc->usb3_generic_phy)) { |
| 960 | ret = PTR_ERR(dwc->usb3_generic_phy); |
| 961 | if (ret == -ENOSYS || ret == -ENODEV) { |
| 962 | dwc->usb3_generic_phy = NULL; |
| 963 | } else if (ret == -EPROBE_DEFER) { |
| 964 | return ret; |
| 965 | } else { |
| 966 | dev_err(dev, "no usb3 phy configured\n"); |
| 967 | return ret; |
| 968 | } |
| 969 | } |
| 970 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 971 | return 0; |
| 972 | } |
| 973 | |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 974 | static int dwc3_core_init_mode(struct dwc3 *dwc) |
| 975 | { |
| 976 | struct device *dev = dwc->dev; |
| 977 | int ret; |
| 978 | |
| 979 | switch (dwc->dr_mode) { |
| 980 | case USB_DR_MODE_PERIPHERAL: |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 981 | ret = dwc3_gadget_init(dwc); |
| 982 | if (ret) { |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 983 | if (ret != -EPROBE_DEFER) |
| 984 | dev_err(dev, "failed to initialize gadget\n"); |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 985 | return ret; |
| 986 | } |
| 987 | break; |
| 988 | case USB_DR_MODE_HOST: |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 989 | ret = dwc3_host_init(dwc); |
| 990 | if (ret) { |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 991 | if (ret != -EPROBE_DEFER) |
| 992 | dev_err(dev, "failed to initialize host\n"); |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 993 | return ret; |
| 994 | } |
| 995 | break; |
| 996 | case USB_DR_MODE_OTG: |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 997 | ret = dwc3_host_init(dwc); |
| 998 | if (ret) { |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 999 | if (ret != -EPROBE_DEFER) |
| 1000 | dev_err(dev, "failed to initialize host\n"); |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 1001 | return ret; |
| 1002 | } |
| 1003 | |
| 1004 | ret = dwc3_gadget_init(dwc); |
| 1005 | if (ret) { |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 1006 | if (ret != -EPROBE_DEFER) |
| 1007 | dev_err(dev, "failed to initialize gadget\n"); |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 1008 | return ret; |
| 1009 | } |
| 1010 | break; |
| 1011 | default: |
| 1012 | dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode); |
| 1013 | return -EINVAL; |
| 1014 | } |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | static void dwc3_core_exit_mode(struct dwc3 *dwc) |
| 1020 | { |
| 1021 | switch (dwc->dr_mode) { |
| 1022 | case USB_DR_MODE_PERIPHERAL: |
| 1023 | dwc3_gadget_exit(dwc); |
| 1024 | break; |
| 1025 | case USB_DR_MODE_HOST: |
| 1026 | dwc3_host_exit(dwc); |
| 1027 | break; |
| 1028 | case USB_DR_MODE_OTG: |
| 1029 | dwc3_host_exit(dwc); |
| 1030 | dwc3_gadget_exit(dwc); |
| 1031 | break; |
| 1032 | default: |
| 1033 | /* do nothing */ |
| 1034 | break; |
| 1035 | } |
| 1036 | } |
| 1037 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1038 | /* XHCI reset, resets other CORE registers as well, re-init those */ |
| 1039 | void dwc3_post_host_reset_core_init(struct dwc3 *dwc) |
| 1040 | { |
| 1041 | dwc3_core_init(dwc); |
| 1042 | dwc3_gadget_restart(dwc); |
| 1043 | } |
| 1044 | |
| 1045 | static void (*notify_event)(struct dwc3 *, unsigned int); |
| 1046 | void dwc3_set_notifier(void (*notify)(struct dwc3 *, unsigned int)) |
| 1047 | { |
| 1048 | notify_event = notify; |
| 1049 | } |
| 1050 | EXPORT_SYMBOL(dwc3_set_notifier); |
| 1051 | |
| 1052 | int dwc3_notify_event(struct dwc3 *dwc, unsigned int event) |
| 1053 | { |
| 1054 | int ret = 0; |
| 1055 | |
| 1056 | if (dwc->notify_event) |
| 1057 | dwc->notify_event(dwc, event); |
| 1058 | else |
| 1059 | ret = -ENODEV; |
| 1060 | |
| 1061 | return ret; |
| 1062 | } |
| 1063 | EXPORT_SYMBOL(dwc3_notify_event); |
| 1064 | |
| 1065 | int dwc3_core_pre_init(struct dwc3 *dwc) |
| 1066 | { |
| 1067 | int ret; |
| 1068 | |
| 1069 | dwc3_cache_hwparams(dwc); |
| 1070 | |
| 1071 | ret = dwc3_phy_setup(dwc); |
| 1072 | if (ret) |
| 1073 | goto err0; |
| 1074 | |
| 1075 | if (!dwc->ev_buf) { |
| 1076 | ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); |
| 1077 | if (ret) { |
| 1078 | dev_err(dwc->dev, "failed to allocate event buffers\n"); |
| 1079 | ret = -ENOMEM; |
| 1080 | goto err1; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | ret = dwc3_core_init(dwc); |
| 1085 | if (ret) { |
| 1086 | dev_err(dwc->dev, "failed to initialize core\n"); |
| 1087 | goto err2; |
| 1088 | } |
| 1089 | |
| 1090 | ret = phy_power_on(dwc->usb2_generic_phy); |
| 1091 | if (ret < 0) |
| 1092 | goto err3; |
| 1093 | |
| 1094 | ret = phy_power_on(dwc->usb3_generic_phy); |
| 1095 | if (ret < 0) |
| 1096 | goto err4; |
| 1097 | |
| 1098 | ret = dwc3_event_buffers_setup(dwc); |
| 1099 | if (ret) { |
| 1100 | dev_err(dwc->dev, "failed to setup event buffers\n"); |
| 1101 | goto err5; |
| 1102 | } |
| 1103 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1104 | return ret; |
| 1105 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1106 | err5: |
| 1107 | phy_power_off(dwc->usb3_generic_phy); |
| 1108 | err4: |
| 1109 | phy_power_off(dwc->usb2_generic_phy); |
| 1110 | err3: |
| 1111 | dwc3_core_exit(dwc); |
| 1112 | err2: |
| 1113 | dwc3_free_event_buffers(dwc); |
| 1114 | err1: |
| 1115 | dwc3_ulpi_exit(dwc); |
| 1116 | err0: |
| 1117 | return ret; |
| 1118 | } |
| 1119 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1120 | #define DWC3_ALIGN_MASK (16 - 1) |
| 1121 | |
John Youn | 26cac20 | 2016-11-14 12:32:43 -0800 | [diff] [blame] | 1122 | /* check whether the core supports IMOD */ |
| 1123 | bool dwc3_has_imod(struct dwc3 *dwc) |
| 1124 | { |
| 1125 | return ((dwc3_is_usb3(dwc) && |
| 1126 | dwc->revision >= DWC3_REVISION_300A) || |
| 1127 | (dwc3_is_usb31(dwc) && |
| 1128 | dwc->revision >= DWC3_USB31_REVISION_120A)); |
| 1129 | } |
| 1130 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1131 | static int dwc3_probe(struct platform_device *pdev) |
| 1132 | { |
| 1133 | struct device *dev = &pdev->dev; |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1134 | struct resource *res; |
| 1135 | struct dwc3 *dwc; |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 1136 | u8 lpm_nyet_threshold; |
Huang Rui | 6b6a0c9 | 2014-10-31 11:11:12 +0800 | [diff] [blame] | 1137 | u8 tx_de_emphasis; |
Huang Rui | 460d098 | 2014-10-31 11:11:18 +0800 | [diff] [blame] | 1138 | u8 hird_threshold; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1139 | int irq; |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1140 | |
Andy Shevchenko | b09e99e | 2014-05-15 15:53:32 +0300 | [diff] [blame] | 1141 | int ret; |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1142 | |
| 1143 | void __iomem *regs; |
| 1144 | void *mem; |
| 1145 | |
Mayank Rana | 861da2b | 2016-07-13 13:47:57 -0700 | [diff] [blame] | 1146 | if (count >= DWC_CTRL_COUNT) { |
| 1147 | dev_err(dev, "Err dwc instance %d >= %d available\n", |
| 1148 | count, DWC_CTRL_COUNT); |
| 1149 | ret = -EINVAL; |
| 1150 | return ret; |
| 1151 | } |
| 1152 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1153 | mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 1154 | if (!mem) |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1155 | return -ENOMEM; |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 1156 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1157 | dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); |
| 1158 | dwc->mem = mem; |
| 1159 | dwc->dev = dev; |
| 1160 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1161 | dwc->notify_event = notify_event; |
| 1162 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 1163 | if (!res) { |
| 1164 | dev_err(dev, "missing IRQ\n"); |
| 1165 | return -ENODEV; |
| 1166 | } |
| 1167 | dwc->xhci_resources[1].start = res->start; |
| 1168 | dwc->xhci_resources[1].end = res->end; |
| 1169 | dwc->xhci_resources[1].flags = res->flags; |
| 1170 | dwc->xhci_resources[1].name = res->name; |
| 1171 | |
| 1172 | irq = platform_get_irq(to_platform_device(dwc->dev), 0); |
| 1173 | |
| 1174 | /* will be enabled in dwc3_msm_resume() */ |
| 1175 | irq_set_status_flags(irq, IRQ_NOAUTOEN); |
Mayank Rana | f616a7f | 2017-03-20 16:10:39 -0700 | [diff] [blame] | 1176 | ret = devm_request_irq(dev, irq, dwc3_interrupt, IRQF_SHARED, "dwc3", |
| 1177 | dwc); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1178 | if (ret) { |
| 1179 | dev_err(dwc->dev, "failed to request irq #%d --> %d\n", |
| 1180 | irq, ret); |
| 1181 | return -ENODEV; |
| 1182 | } |
| 1183 | |
| 1184 | dwc->irq = irq; |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1185 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1186 | if (!res) { |
| 1187 | dev_err(dev, "missing memory resource\n"); |
| 1188 | return -ENODEV; |
| 1189 | } |
| 1190 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1191 | dwc->reg_phys = res->start; |
Vivek Gautam | f32a5e2 | 2014-06-04 14:34:52 +0530 | [diff] [blame] | 1192 | dwc->xhci_resources[0].start = res->start; |
| 1193 | dwc->xhci_resources[0].end = dwc->xhci_resources[0].start + |
| 1194 | DWC3_XHCI_REGS_END; |
| 1195 | dwc->xhci_resources[0].flags = res->flags; |
| 1196 | dwc->xhci_resources[0].name = res->name; |
| 1197 | |
| 1198 | res->start += DWC3_GLOBALS_REGS_START; |
| 1199 | |
| 1200 | /* |
| 1201 | * Request memory region but exclude xHCI regs, |
| 1202 | * since it will be requested by the xhci-plat driver. |
| 1203 | */ |
| 1204 | regs = devm_ioremap_resource(dev, res); |
Felipe Balbi | 3da1f6e | 2014-09-02 15:19:43 -0500 | [diff] [blame] | 1205 | if (IS_ERR(regs)) { |
| 1206 | ret = PTR_ERR(regs); |
| 1207 | goto err0; |
| 1208 | } |
Vivek Gautam | f32a5e2 | 2014-06-04 14:34:52 +0530 | [diff] [blame] | 1209 | |
| 1210 | dwc->regs = regs; |
| 1211 | dwc->regs_size = resource_size(res); |
Vivek Gautam | f32a5e2 | 2014-06-04 14:34:52 +0530 | [diff] [blame] | 1212 | |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 1213 | /* default to highest possible threshold */ |
Hemant Kumar | 628b891 | 2017-02-08 16:15:57 -0800 | [diff] [blame] | 1214 | lpm_nyet_threshold = 0xf; |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 1215 | |
Huang Rui | 6b6a0c9 | 2014-10-31 11:11:12 +0800 | [diff] [blame] | 1216 | /* default to -3.5dB de-emphasis */ |
| 1217 | tx_de_emphasis = 1; |
| 1218 | |
Huang Rui | 460d098 | 2014-10-31 11:11:18 +0800 | [diff] [blame] | 1219 | /* |
| 1220 | * default to assert utmi_sleep_n and use maximum allowed HIRD |
| 1221 | * threshold value of 0b1100 |
| 1222 | */ |
| 1223 | hird_threshold = 12; |
| 1224 | |
Heikki Krogerus | 63863b9 | 2015-09-21 11:14:32 +0300 | [diff] [blame] | 1225 | dwc->maximum_speed = usb_get_maximum_speed(dev); |
Vamsi Krishna Samavedam | 86ed20b | 2017-01-31 13:55:38 -0800 | [diff] [blame] | 1226 | dwc->max_hw_supp_speed = dwc->maximum_speed; |
Heikki Krogerus | 06e7114 | 2015-09-21 11:14:34 +0300 | [diff] [blame] | 1227 | dwc->dr_mode = usb_get_dr_mode(dev); |
Mayank Rana | fb9cd93 | 2016-11-03 23:26:38 -0700 | [diff] [blame] | 1228 | |
| 1229 | if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) { |
| 1230 | dwc->dr_mode = USB_DR_MODE_OTG; |
| 1231 | dwc->is_drd = 1; |
| 1232 | } |
| 1233 | |
William Wu | 32f2ed8 | 2016-08-16 22:44:38 +0800 | [diff] [blame] | 1234 | dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node); |
Heikki Krogerus | 63863b9 | 2015-09-21 11:14:32 +0300 | [diff] [blame] | 1235 | |
Arnd Bergmann | 42695fc | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 1236 | dwc->sysdev_is_parent = device_property_read_bool(dev, |
| 1237 | "linux,sysdev_is_parent"); |
| 1238 | if (dwc->sysdev_is_parent) |
| 1239 | dwc->sysdev = dwc->dev->parent; |
| 1240 | else |
| 1241 | dwc->sysdev = dwc->dev; |
| 1242 | |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1243 | dwc->has_lpm_erratum = device_property_read_bool(dev, |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 1244 | "snps,has-lpm-erratum"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1245 | device_property_read_u8(dev, "snps,lpm-nyet-threshold", |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 1246 | &lpm_nyet_threshold); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1247 | dwc->is_utmi_l1_suspend = device_property_read_bool(dev, |
Huang Rui | 460d098 | 2014-10-31 11:11:18 +0800 | [diff] [blame] | 1248 | "snps,is-utmi-l1-suspend"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1249 | device_property_read_u8(dev, "snps,hird-threshold", |
Huang Rui | 460d098 | 2014-10-31 11:11:18 +0800 | [diff] [blame] | 1250 | &hird_threshold); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1251 | dwc->usb3_lpm_capable = device_property_read_bool(dev, |
Robert Baldyga | eac68e8 | 2015-03-09 15:06:12 +0100 | [diff] [blame] | 1252 | "snps,usb3_lpm_capable"); |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1253 | |
Mayank Rana | a8e4de6 | 2016-12-13 17:11:15 -0800 | [diff] [blame] | 1254 | dwc->needs_fifo_resize = device_property_read_bool(dev, |
| 1255 | "tx-fifo-resize"); |
| 1256 | |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1257 | dwc->disable_scramble_quirk = device_property_read_bool(dev, |
Huang Rui | 3b81221 | 2014-10-28 19:54:25 +0800 | [diff] [blame] | 1258 | "snps,disable_scramble_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1259 | dwc->u2exit_lfps_quirk = device_property_read_bool(dev, |
Huang Rui | 9a5b2f3 | 2014-10-28 19:54:27 +0800 | [diff] [blame] | 1260 | "snps,u2exit_lfps_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1261 | dwc->u2ss_inp3_quirk = device_property_read_bool(dev, |
Huang Rui | b5a65c4 | 2014-10-28 19:54:28 +0800 | [diff] [blame] | 1262 | "snps,u2ss_inp3_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1263 | dwc->req_p1p2p3_quirk = device_property_read_bool(dev, |
Huang Rui | df31f5b | 2014-10-28 19:54:29 +0800 | [diff] [blame] | 1264 | "snps,req_p1p2p3_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1265 | dwc->del_p1p2p3_quirk = device_property_read_bool(dev, |
Huang Rui | a2a1d0f | 2014-10-28 19:54:30 +0800 | [diff] [blame] | 1266 | "snps,del_p1p2p3_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1267 | dwc->del_phy_power_chg_quirk = device_property_read_bool(dev, |
Huang Rui | 41c06ff | 2014-10-28 19:54:31 +0800 | [diff] [blame] | 1268 | "snps,del_phy_power_chg_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1269 | dwc->lfps_filter_quirk = device_property_read_bool(dev, |
Huang Rui | fb67afc | 2014-10-28 19:54:32 +0800 | [diff] [blame] | 1270 | "snps,lfps_filter_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1271 | dwc->rx_detect_poll_quirk = device_property_read_bool(dev, |
Huang Rui | 14f4ac5 | 2014-10-28 19:54:33 +0800 | [diff] [blame] | 1272 | "snps,rx_detect_poll_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1273 | dwc->dis_u3_susphy_quirk = device_property_read_bool(dev, |
Huang Rui | 59acfa2 | 2014-10-31 11:11:13 +0800 | [diff] [blame] | 1274 | "snps,dis_u3_susphy_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1275 | dwc->dis_u2_susphy_quirk = device_property_read_bool(dev, |
Huang Rui | 0effe0a | 2014-10-31 11:11:14 +0800 | [diff] [blame] | 1276 | "snps,dis_u2_susphy_quirk"); |
John Youn | ec791d1 | 2015-10-02 20:30:57 -0700 | [diff] [blame] | 1277 | dwc->dis_enblslpm_quirk = device_property_read_bool(dev, |
| 1278 | "snps,dis_enblslpm_quirk"); |
Rajesh Bhagat | e58dd35 | 2016-03-14 14:40:50 +0530 | [diff] [blame] | 1279 | dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev, |
| 1280 | "snps,dis_rxdet_inp3_quirk"); |
William Wu | 16199f3 | 2016-08-16 22:44:37 +0800 | [diff] [blame] | 1281 | dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev, |
| 1282 | "snps,dis-u2-freeclk-exists-quirk"); |
William Wu | 00fe081 | 2016-08-16 22:44:39 +0800 | [diff] [blame] | 1283 | dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev, |
| 1284 | "snps,dis-del-phy-power-chg-quirk"); |
Huang Rui | 6b6a0c9 | 2014-10-31 11:11:12 +0800 | [diff] [blame] | 1285 | |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1286 | dwc->tx_de_emphasis_quirk = device_property_read_bool(dev, |
Huang Rui | 6b6a0c9 | 2014-10-31 11:11:12 +0800 | [diff] [blame] | 1287 | "snps,tx_de_emphasis_quirk"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1288 | device_property_read_u8(dev, "snps,tx_de_emphasis", |
Huang Rui | 6b6a0c9 | 2014-10-31 11:11:12 +0800 | [diff] [blame] | 1289 | &tx_de_emphasis); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1290 | device_property_read_string(dev, "snps,hsphy_interface", |
| 1291 | &dwc->hsphy_interface); |
| 1292 | device_property_read_u32(dev, "snps,quirk-frame-length-adjustment", |
Felipe Balbi | bcdb327 | 2016-05-16 10:42:23 +0300 | [diff] [blame] | 1293 | &dwc->fladj); |
Mayank Rana | 00b0398 | 2015-06-10 11:43:09 -0700 | [diff] [blame] | 1294 | dwc->disable_clk_gating = device_property_read_bool(dev, |
| 1295 | "snps,disable-clk-gating"); |
Heikki Krogerus | 3d12891 | 2015-09-21 11:14:35 +0300 | [diff] [blame] | 1296 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1297 | if (dwc->enable_bus_suspend) { |
| 1298 | pm_runtime_set_autosuspend_delay(dev, 500); |
| 1299 | pm_runtime_use_autosuspend(dev); |
| 1300 | } |
| 1301 | |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 1302 | dwc->lpm_nyet_threshold = lpm_nyet_threshold; |
Huang Rui | 6b6a0c9 | 2014-10-31 11:11:12 +0800 | [diff] [blame] | 1303 | dwc->tx_de_emphasis = tx_de_emphasis; |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 1304 | |
Huang Rui | 460d098 | 2014-10-31 11:11:18 +0800 | [diff] [blame] | 1305 | dwc->hird_threshold = hird_threshold |
| 1306 | | (dwc->is_utmi_l1_suspend << 4); |
| 1307 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1308 | init_waitqueue_head(&dwc->wait_linkstate); |
Heikki Krogerus | 6c89cce0 | 2015-05-13 15:26:45 +0300 | [diff] [blame] | 1309 | platform_set_drvdata(pdev, dwc); |
| 1310 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1311 | ret = dwc3_core_get_phy(dwc); |
| 1312 | if (ret) |
Felipe Balbi | 3da1f6e | 2014-09-02 15:19:43 -0500 | [diff] [blame] | 1313 | goto err0; |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 1314 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1315 | spin_lock_init(&dwc->lock); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1316 | |
Mayank Rana | f616a7f | 2017-03-20 16:10:39 -0700 | [diff] [blame] | 1317 | dwc->dwc_wq = alloc_ordered_workqueue("dwc_wq", WQ_HIGHPRI); |
| 1318 | if (!dwc->dwc_wq) { |
| 1319 | pr_err("%s: Unable to create workqueue dwc_wq\n", __func__); |
| 1320 | return -ENOMEM; |
| 1321 | } |
| 1322 | |
| 1323 | INIT_WORK(&dwc->bh_work, dwc3_bh_work); |
| 1324 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1325 | pm_runtime_no_callbacks(dev); |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1326 | pm_runtime_set_active(dev); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 1327 | pm_runtime_enable(dev); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 1328 | pm_runtime_forbid(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1329 | |
John Youn | 77966eb | 2016-02-19 17:31:01 -0800 | [diff] [blame] | 1330 | /* Check the maximum_speed parameter */ |
| 1331 | switch (dwc->maximum_speed) { |
| 1332 | case USB_SPEED_LOW: |
| 1333 | case USB_SPEED_FULL: |
| 1334 | case USB_SPEED_HIGH: |
| 1335 | case USB_SPEED_SUPER: |
| 1336 | case USB_SPEED_SUPER_PLUS: |
| 1337 | break; |
| 1338 | default: |
| 1339 | dev_err(dev, "invalid maximum_speed parameter %d\n", |
| 1340 | dwc->maximum_speed); |
| 1341 | /* fall through */ |
| 1342 | case USB_SPEED_UNKNOWN: |
| 1343 | /* default to superspeed */ |
John Youn | 2c7f1bd | 2016-02-05 17:08:59 -0800 | [diff] [blame] | 1344 | dwc->maximum_speed = USB_SPEED_SUPER; |
| 1345 | |
| 1346 | /* |
| 1347 | * default to superspeed plus if we are capable. |
| 1348 | */ |
| 1349 | if (dwc3_is_usb31(dwc) && |
| 1350 | (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) == |
| 1351 | DWC3_GHWPARAMS3_SSPHY_IFC_GEN2)) |
| 1352 | dwc->maximum_speed = USB_SPEED_SUPER_PLUS; |
John Youn | 77966eb | 2016-02-19 17:31:01 -0800 | [diff] [blame] | 1353 | |
Vamsi Krishna Samavedam | 86ed20b | 2017-01-31 13:55:38 -0800 | [diff] [blame] | 1354 | dwc->max_hw_supp_speed = dwc->maximum_speed; |
John Youn | 77966eb | 2016-02-19 17:31:01 -0800 | [diff] [blame] | 1355 | break; |
John Youn | 2c7f1bd | 2016-02-05 17:08:59 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1358 | /* Adjust Frame Length */ |
| 1359 | dwc3_frame_length_adjustment(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1360 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1361 | /* Hardcode number of eps */ |
| 1362 | dwc->num_in_eps = 16; |
| 1363 | dwc->num_out_eps = 16; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1364 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1365 | ret = dwc3_core_init_mode(dwc); |
| 1366 | if (ret) |
Kyle Yan | 65be4a5 | 2016-10-31 15:05:00 -0700 | [diff] [blame] | 1367 | goto err0; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1368 | |
| 1369 | ret = dwc3_debugfs_init(dwc); |
| 1370 | if (ret) { |
| 1371 | dev_err(dev, "failed to initialize debugfs\n"); |
Kyle Yan | 65be4a5 | 2016-10-31 15:05:00 -0700 | [diff] [blame] | 1372 | goto err_core_init; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Mayank Rana | 861da2b | 2016-07-13 13:47:57 -0700 | [diff] [blame] | 1375 | dwc->dwc_ipc_log_ctxt = ipc_log_context_create(NUM_LOG_PAGES, |
| 1376 | dev_name(dwc->dev), 0); |
| 1377 | if (!dwc->dwc_ipc_log_ctxt) |
| 1378 | dev_err(dwc->dev, "Error getting ipc_log_ctxt\n"); |
| 1379 | |
| 1380 | dwc3_instance[count] = dwc; |
| 1381 | dwc->index = count; |
| 1382 | count++; |
| 1383 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1384 | pm_runtime_allow(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1385 | return 0; |
| 1386 | |
Kyle Yan | 65be4a5 | 2016-10-31 15:05:00 -0700 | [diff] [blame] | 1387 | err_core_init: |
| 1388 | dwc3_core_exit_mode(dwc); |
Felipe Balbi | 3da1f6e | 2014-09-02 15:19:43 -0500 | [diff] [blame] | 1389 | err0: |
| 1390 | /* |
| 1391 | * restore res->start back to its original value so that, in case the |
| 1392 | * probe is deferred, we don't end up getting error in request the |
| 1393 | * memory region the next time probe is called. |
| 1394 | */ |
| 1395 | res->start -= DWC3_GLOBALS_REGS_START; |
Mayank Rana | f616a7f | 2017-03-20 16:10:39 -0700 | [diff] [blame] | 1396 | destroy_workqueue(dwc->dwc_wq); |
Felipe Balbi | 3da1f6e | 2014-09-02 15:19:43 -0500 | [diff] [blame] | 1397 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1398 | return ret; |
| 1399 | } |
| 1400 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 1401 | static int dwc3_remove(struct platform_device *pdev) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1402 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1403 | struct dwc3 *dwc = platform_get_drvdata(pdev); |
Felipe Balbi | 3da1f6e | 2014-09-02 15:19:43 -0500 | [diff] [blame] | 1404 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1405 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1406 | pm_runtime_get_sync(&pdev->dev); |
Felipe Balbi | 3da1f6e | 2014-09-02 15:19:43 -0500 | [diff] [blame] | 1407 | /* |
| 1408 | * restore res->start back to its original value so that, in case the |
| 1409 | * probe is deferred, we don't end up getting error in request the |
| 1410 | * memory region the next time probe is called. |
| 1411 | */ |
| 1412 | res->start -= DWC3_GLOBALS_REGS_START; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1413 | |
Felipe Balbi | dc99f16 | 2014-09-03 16:13:37 -0500 | [diff] [blame] | 1414 | dwc3_debugfs_exit(dwc); |
| 1415 | dwc3_core_exit_mode(dwc); |
Kishon Vijay Abraham I | 8ba007a | 2013-01-25 08:30:54 +0530 | [diff] [blame] | 1416 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1417 | dwc3_core_exit(dwc); |
Heikki Krogerus | 88bc9d1 | 2015-05-13 15:26:51 +0300 | [diff] [blame] | 1418 | dwc3_ulpi_exit(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1419 | |
Mayank Rana | f616a7f | 2017-03-20 16:10:39 -0700 | [diff] [blame] | 1420 | destroy_workqueue(dwc->dwc_wq); |
| 1421 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1422 | pm_runtime_put_sync(&pdev->dev); |
| 1423 | pm_runtime_allow(&pdev->dev); |
| 1424 | pm_runtime_disable(&pdev->dev); |
| 1425 | |
Felipe Balbi | c499ff7 | 2016-05-16 10:49:01 +0300 | [diff] [blame] | 1426 | dwc3_free_event_buffers(dwc); |
| 1427 | dwc3_free_scratch_buffers(dwc); |
| 1428 | |
Mayank Rana | 861da2b | 2016-07-13 13:47:57 -0700 | [diff] [blame] | 1429 | ipc_log_context_destroy(dwc->dwc_ipc_log_ctxt); |
| 1430 | dwc->dwc_ipc_log_ctxt = NULL; |
| 1431 | count--; |
| 1432 | dwc3_instance[dwc->index] = NULL; |
| 1433 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1434 | return 0; |
| 1435 | } |
| 1436 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1437 | #ifdef CONFIG_PM |
| 1438 | static int dwc3_suspend_common(struct dwc3 *dwc) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1439 | { |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1440 | unsigned long flags; |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1441 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 1442 | switch (dwc->dr_mode) { |
| 1443 | case USB_DR_MODE_PERIPHERAL: |
| 1444 | case USB_DR_MODE_OTG: |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1445 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1446 | dwc3_gadget_suspend(dwc); |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1447 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 51f5d49 | 2016-05-16 10:52:58 +0300 | [diff] [blame] | 1448 | break; |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 1449 | case USB_DR_MODE_HOST: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1450 | default: |
Felipe Balbi | 51f5d49 | 2016-05-16 10:52:58 +0300 | [diff] [blame] | 1451 | /* do nothing */ |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1452 | break; |
| 1453 | } |
| 1454 | |
Felipe Balbi | 51f5d49 | 2016-05-16 10:52:58 +0300 | [diff] [blame] | 1455 | dwc3_core_exit(dwc); |
Felipe Balbi | 5c4ad318 | 2016-04-11 17:12:34 +0300 | [diff] [blame] | 1456 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1457 | return 0; |
| 1458 | } |
| 1459 | |
| 1460 | static int dwc3_resume_common(struct dwc3 *dwc) |
| 1461 | { |
| 1462 | unsigned long flags; |
| 1463 | int ret; |
| 1464 | |
| 1465 | ret = dwc3_core_init(dwc); |
| 1466 | if (ret) |
| 1467 | return ret; |
| 1468 | |
| 1469 | switch (dwc->dr_mode) { |
| 1470 | case USB_DR_MODE_PERIPHERAL: |
| 1471 | case USB_DR_MODE_OTG: |
| 1472 | spin_lock_irqsave(&dwc->lock, flags); |
| 1473 | dwc3_gadget_resume(dwc); |
| 1474 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1475 | /* FALLTHROUGH */ |
| 1476 | case USB_DR_MODE_HOST: |
| 1477 | default: |
| 1478 | /* do nothing */ |
| 1479 | break; |
| 1480 | } |
| 1481 | |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | static int dwc3_runtime_checks(struct dwc3 *dwc) |
| 1486 | { |
| 1487 | switch (dwc->dr_mode) { |
| 1488 | case USB_DR_MODE_PERIPHERAL: |
| 1489 | case USB_DR_MODE_OTG: |
| 1490 | if (dwc->connected) |
| 1491 | return -EBUSY; |
| 1492 | break; |
| 1493 | case USB_DR_MODE_HOST: |
| 1494 | default: |
| 1495 | /* do nothing */ |
| 1496 | break; |
| 1497 | } |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | static int dwc3_runtime_suspend(struct device *dev) |
| 1503 | { |
| 1504 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 1505 | int ret; |
| 1506 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1507 | /* Check if platform glue driver handling PM, if not then handle here */ |
| 1508 | if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT)) |
| 1509 | return 0; |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1510 | |
| 1511 | ret = dwc3_suspend_common(dwc); |
| 1512 | if (ret) |
| 1513 | return ret; |
| 1514 | |
| 1515 | device_init_wakeup(dev, true); |
| 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | static int dwc3_runtime_resume(struct device *dev) |
| 1521 | { |
| 1522 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 1523 | int ret; |
| 1524 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1525 | /* Check if platform glue driver handling PM, if not then handle here */ |
| 1526 | if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT)) |
| 1527 | return 0; |
| 1528 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1529 | device_init_wakeup(dev, false); |
| 1530 | |
| 1531 | ret = dwc3_resume_common(dwc); |
| 1532 | if (ret) |
| 1533 | return ret; |
| 1534 | |
| 1535 | switch (dwc->dr_mode) { |
| 1536 | case USB_DR_MODE_PERIPHERAL: |
| 1537 | case USB_DR_MODE_OTG: |
| 1538 | dwc3_gadget_process_pending_events(dwc); |
| 1539 | break; |
| 1540 | case USB_DR_MODE_HOST: |
| 1541 | default: |
| 1542 | /* do nothing */ |
| 1543 | break; |
| 1544 | } |
| 1545 | |
| 1546 | pm_runtime_mark_last_busy(dev); |
Felipe Balbi | b74c2d8 | 2016-07-28 13:07:07 +0300 | [diff] [blame] | 1547 | pm_runtime_put(dev); |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1548 | |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | static int dwc3_runtime_idle(struct device *dev) |
| 1553 | { |
| 1554 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 1555 | |
| 1556 | switch (dwc->dr_mode) { |
| 1557 | case USB_DR_MODE_PERIPHERAL: |
| 1558 | case USB_DR_MODE_OTG: |
| 1559 | if (dwc3_runtime_checks(dwc)) |
| 1560 | return -EBUSY; |
| 1561 | break; |
| 1562 | case USB_DR_MODE_HOST: |
| 1563 | default: |
| 1564 | /* do nothing */ |
| 1565 | break; |
| 1566 | } |
| 1567 | |
| 1568 | pm_runtime_mark_last_busy(dev); |
| 1569 | pm_runtime_autosuspend(dev); |
| 1570 | |
| 1571 | return 0; |
| 1572 | } |
| 1573 | #endif /* CONFIG_PM */ |
| 1574 | |
| 1575 | #ifdef CONFIG_PM_SLEEP |
| 1576 | static int dwc3_suspend(struct device *dev) |
| 1577 | { |
| 1578 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 1579 | int ret; |
| 1580 | |
Mayank Rana | 5ca4112 | 2017-03-24 11:30:25 -0700 | [diff] [blame] | 1581 | /* Check if platform glue driver handling PM, if not then handle here */ |
| 1582 | if (!dwc3_notify_event(dwc, DWC3_CORE_PM_SUSPEND_EVENT)) |
| 1583 | return 0; |
| 1584 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1585 | ret = dwc3_suspend_common(dwc); |
| 1586 | if (ret) |
| 1587 | return ret; |
| 1588 | |
Sekhar Nori | 6344475 | 2015-08-31 21:09:08 +0530 | [diff] [blame] | 1589 | pinctrl_pm_select_sleep_state(dev); |
| 1590 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | static int dwc3_resume(struct device *dev) |
| 1595 | { |
| 1596 | struct dwc3 *dwc = dev_get_drvdata(dev); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 1597 | int ret; |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1598 | |
Mayank Rana | 5ca4112 | 2017-03-24 11:30:25 -0700 | [diff] [blame] | 1599 | /* Check if platform glue driver handling PM, if not then handle here */ |
| 1600 | if (!dwc3_notify_event(dwc, DWC3_CORE_PM_RESUME_EVENT)) |
| 1601 | return 0; |
| 1602 | |
Sekhar Nori | 6344475 | 2015-08-31 21:09:08 +0530 | [diff] [blame] | 1603 | pinctrl_pm_select_default_state(dev); |
| 1604 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1605 | ret = dwc3_resume_common(dwc); |
Felipe Balbi | 51f5d49 | 2016-05-16 10:52:58 +0300 | [diff] [blame] | 1606 | if (ret) |
Felipe Balbi | 5c4ad318 | 2016-04-11 17:12:34 +0300 | [diff] [blame] | 1607 | return ret; |
| 1608 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1609 | pm_runtime_disable(dev); |
| 1610 | pm_runtime_set_active(dev); |
| 1611 | pm_runtime_enable(dev); |
| 1612 | |
| 1613 | return 0; |
| 1614 | } |
Felipe Balbi | 7f370ed | 2016-05-09 15:27:01 +0300 | [diff] [blame] | 1615 | #endif /* CONFIG_PM_SLEEP */ |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1616 | |
| 1617 | static const struct dev_pm_ops dwc3_dev_pm_ops = { |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1618 | SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume) |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1619 | SET_RUNTIME_PM_OPS(dwc3_runtime_suspend, dwc3_runtime_resume, |
| 1620 | dwc3_runtime_idle) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 1621 | }; |
| 1622 | |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 1623 | #ifdef CONFIG_OF |
| 1624 | static const struct of_device_id of_dwc3_match[] = { |
| 1625 | { |
Felipe Balbi | 22a5aa1 | 2013-07-02 21:20:24 +0300 | [diff] [blame] | 1626 | .compatible = "snps,dwc3" |
| 1627 | }, |
| 1628 | { |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 1629 | .compatible = "synopsys,dwc3" |
| 1630 | }, |
| 1631 | { }, |
| 1632 | }; |
| 1633 | MODULE_DEVICE_TABLE(of, of_dwc3_match); |
| 1634 | #endif |
| 1635 | |
Heikki Krogerus | 404905a | 2014-09-25 10:57:02 +0300 | [diff] [blame] | 1636 | #ifdef CONFIG_ACPI |
| 1637 | |
| 1638 | #define ACPI_ID_INTEL_BSW "808622B7" |
| 1639 | |
| 1640 | static const struct acpi_device_id dwc3_acpi_match[] = { |
| 1641 | { ACPI_ID_INTEL_BSW, 0 }, |
| 1642 | { }, |
| 1643 | }; |
| 1644 | MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match); |
| 1645 | #endif |
| 1646 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1647 | static struct platform_driver dwc3_driver = { |
| 1648 | .probe = dwc3_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 1649 | .remove = dwc3_remove, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1650 | .driver = { |
| 1651 | .name = "dwc3", |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 1652 | .of_match_table = of_match_ptr(of_dwc3_match), |
Heikki Krogerus | 404905a | 2014-09-25 10:57:02 +0300 | [diff] [blame] | 1653 | .acpi_match_table = ACPI_PTR(dwc3_acpi_match), |
Felipe Balbi | 7f370ed | 2016-05-09 15:27:01 +0300 | [diff] [blame] | 1654 | .pm = &dwc3_dev_pm_ops, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1655 | }, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1656 | }; |
| 1657 | |
Tobias Klauser | b1116dc | 2012-02-28 12:57:20 +0100 | [diff] [blame] | 1658 | module_platform_driver(dwc3_driver); |
| 1659 | |
Sebastian Andrzej Siewior | 7ae4fc4 | 2011-10-19 19:39:50 +0200 | [diff] [blame] | 1660 | MODULE_ALIAS("platform:dwc3"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1661 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 1662 | MODULE_LICENSE("GPL v2"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1663 | MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); |