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