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