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 | a72e658 | 2011-09-05 13:37:28 +0300 | [diff] [blame] | 22 | #include <linux/module.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 23 | #include <linux/kernel.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/pm_runtime.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/ioport.h> |
| 30 | #include <linux/io.h> |
| 31 | #include <linux/list.h> |
| 32 | #include <linux/delay.h> |
| 33 | #include <linux/dma-mapping.h> |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 34 | #include <linux/of.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 35 | |
Felipe Balbi | 51e1e7b | 2012-07-19 14:09:48 +0300 | [diff] [blame] | 36 | #include <linux/usb/otg.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 37 | #include <linux/usb/ch9.h> |
| 38 | #include <linux/usb/gadget.h> |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 39 | #include <linux/usb/of.h> |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 40 | #include <linux/usb/otg.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 41 | |
Felipe Balbi | 6462cbd | 2013-06-30 14:19:33 +0300 | [diff] [blame] | 42 | #include "platform_data.h" |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 43 | #include "core.h" |
| 44 | #include "gadget.h" |
| 45 | #include "io.h" |
| 46 | |
| 47 | #include "debug.h" |
| 48 | |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 49 | /* -------------------------------------------------------------------------- */ |
| 50 | |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 51 | void dwc3_set_mode(struct dwc3 *dwc, u32 mode) |
| 52 | { |
| 53 | u32 reg; |
| 54 | |
| 55 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 56 | reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); |
| 57 | reg |= DWC3_GCTL_PRTCAPDIR(mode); |
| 58 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 59 | } |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 60 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 61 | /** |
| 62 | * dwc3_core_soft_reset - Issues core soft reset and PHY reset |
| 63 | * @dwc: pointer to our context structure |
| 64 | */ |
| 65 | static void dwc3_core_soft_reset(struct dwc3 *dwc) |
| 66 | { |
| 67 | u32 reg; |
| 68 | |
| 69 | /* Before Resetting PHY, put Core in Reset */ |
| 70 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 71 | reg |= DWC3_GCTL_CORESOFTRESET; |
| 72 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 73 | |
| 74 | /* Assert USB3 PHY reset */ |
| 75 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 76 | reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST; |
| 77 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 78 | |
| 79 | /* Assert USB2 PHY reset */ |
| 80 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 81 | reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST; |
| 82 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 83 | |
Felipe Balbi | 51e1e7b | 2012-07-19 14:09:48 +0300 | [diff] [blame] | 84 | usb_phy_init(dwc->usb2_phy); |
| 85 | usb_phy_init(dwc->usb3_phy); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 86 | mdelay(100); |
| 87 | |
| 88 | /* Clear USB3 PHY reset */ |
| 89 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 90 | reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST; |
| 91 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 92 | |
| 93 | /* Clear USB2 PHY reset */ |
| 94 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 95 | reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST; |
| 96 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 97 | |
Pratyush Anand | 45627ac | 2012-06-21 17:44:28 +0530 | [diff] [blame] | 98 | mdelay(100); |
| 99 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 100 | /* After PHYs are stable we can take Core out of reset state */ |
| 101 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 102 | reg &= ~DWC3_GCTL_CORESOFTRESET; |
| 103 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * dwc3_free_one_event_buffer - Frees one event buffer |
| 108 | * @dwc: Pointer to our controller context structure |
| 109 | * @evt: Pointer to event buffer to be freed |
| 110 | */ |
| 111 | static void dwc3_free_one_event_buffer(struct dwc3 *dwc, |
| 112 | struct dwc3_event_buffer *evt) |
| 113 | { |
| 114 | dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 118 | * dwc3_alloc_one_event_buffer - Allocates one event buffer structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 119 | * @dwc: Pointer to our controller context structure |
| 120 | * @length: size of the event buffer |
| 121 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 122 | * Returns a pointer to the allocated event buffer structure on success |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 123 | * otherwise ERR_PTR(errno). |
| 124 | */ |
Felipe Balbi | 67d0b50 | 2013-02-22 16:31:07 +0200 | [diff] [blame] | 125 | static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc, |
| 126 | unsigned length) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 127 | { |
| 128 | struct dwc3_event_buffer *evt; |
| 129 | |
Felipe Balbi | 380f0d2 | 2012-10-11 13:48:36 +0300 | [diff] [blame] | 130 | evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 131 | if (!evt) |
| 132 | return ERR_PTR(-ENOMEM); |
| 133 | |
| 134 | evt->dwc = dwc; |
| 135 | evt->length = length; |
| 136 | evt->buf = dma_alloc_coherent(dwc->dev, length, |
| 137 | &evt->dma, GFP_KERNEL); |
Felipe Balbi | e32672f | 2012-11-08 15:26:41 +0200 | [diff] [blame] | 138 | if (!evt->buf) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 139 | return ERR_PTR(-ENOMEM); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 140 | |
| 141 | return evt; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * dwc3_free_event_buffers - frees all allocated event buffers |
| 146 | * @dwc: Pointer to our controller context structure |
| 147 | */ |
| 148 | static void dwc3_free_event_buffers(struct dwc3 *dwc) |
| 149 | { |
| 150 | struct dwc3_event_buffer *evt; |
| 151 | int i; |
| 152 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 153 | for (i = 0; i < dwc->num_event_buffers; i++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 154 | evt = dwc->ev_buffs[i]; |
Anton Tikhomirov | 64b6c8a | 2012-03-06 17:05:15 +0900 | [diff] [blame] | 155 | if (evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 156 | dwc3_free_one_event_buffer(dwc, evt); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 162 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 163 | * @length: size of event buffer |
| 164 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 165 | * Returns 0 on success otherwise negative errno. In the error case, dwc |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 166 | * may contain some buffers allocated but not all which were requested. |
| 167 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 168 | static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 169 | { |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 170 | int num; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 171 | int i; |
| 172 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 173 | num = DWC3_NUM_INT(dwc->hwparams.hwparams1); |
| 174 | dwc->num_event_buffers = num; |
| 175 | |
Felipe Balbi | 380f0d2 | 2012-10-11 13:48:36 +0300 | [diff] [blame] | 176 | dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num, |
| 177 | GFP_KERNEL); |
Felipe Balbi | 457d3f2 | 2011-10-24 12:03:13 +0300 | [diff] [blame] | 178 | if (!dwc->ev_buffs) { |
| 179 | dev_err(dwc->dev, "can't allocate event buffers array\n"); |
| 180 | return -ENOMEM; |
| 181 | } |
| 182 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 183 | for (i = 0; i < num; i++) { |
| 184 | struct dwc3_event_buffer *evt; |
| 185 | |
| 186 | evt = dwc3_alloc_one_event_buffer(dwc, length); |
| 187 | if (IS_ERR(evt)) { |
| 188 | dev_err(dwc->dev, "can't allocate event buffer\n"); |
| 189 | return PTR_ERR(evt); |
| 190 | } |
| 191 | dwc->ev_buffs[i] = evt; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * dwc3_event_buffers_setup - setup our allocated event buffers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 199 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 200 | * |
| 201 | * Returns 0 on success otherwise negative errno. |
| 202 | */ |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 203 | static int dwc3_event_buffers_setup(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 204 | { |
| 205 | struct dwc3_event_buffer *evt; |
| 206 | int n; |
| 207 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 208 | for (n = 0; n < dwc->num_event_buffers; n++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 209 | evt = dwc->ev_buffs[n]; |
| 210 | dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n", |
| 211 | evt->buf, (unsigned long long) evt->dma, |
| 212 | evt->length); |
| 213 | |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 214 | evt->lpos = 0; |
| 215 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 216 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), |
| 217 | lower_32_bits(evt->dma)); |
| 218 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), |
| 219 | upper_32_bits(evt->dma)); |
| 220 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), |
| 221 | evt->length & 0xffff); |
| 222 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) |
| 229 | { |
| 230 | struct dwc3_event_buffer *evt; |
| 231 | int n; |
| 232 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 233 | for (n = 0; n < dwc->num_event_buffers; n++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 234 | evt = dwc->ev_buffs[n]; |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 235 | |
| 236 | evt->lpos = 0; |
| 237 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 238 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0); |
| 239 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0); |
| 240 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0); |
| 241 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); |
| 242 | } |
| 243 | } |
| 244 | |
Felipe Balbi | 789451f6 | 2011-05-05 15:53:10 +0300 | [diff] [blame] | 245 | static void dwc3_core_num_eps(struct dwc3 *dwc) |
| 246 | { |
| 247 | struct dwc3_hwparams *parms = &dwc->hwparams; |
| 248 | |
| 249 | dwc->num_in_eps = DWC3_NUM_IN_EPS(parms); |
| 250 | dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps; |
| 251 | |
| 252 | dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n", |
| 253 | dwc->num_in_eps, dwc->num_out_eps); |
| 254 | } |
| 255 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 256 | static void dwc3_cache_hwparams(struct dwc3 *dwc) |
Felipe Balbi | 26ceca9 | 2011-09-30 10:58:49 +0300 | [diff] [blame] | 257 | { |
| 258 | struct dwc3_hwparams *parms = &dwc->hwparams; |
| 259 | |
| 260 | parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); |
| 261 | parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); |
| 262 | parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); |
| 263 | parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); |
| 264 | parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); |
| 265 | parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); |
| 266 | parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); |
| 267 | parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); |
| 268 | parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); |
| 269 | } |
| 270 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 271 | /** |
| 272 | * dwc3_core_init - Low-level initialization of DWC3 Core |
| 273 | * @dwc: Pointer to our controller context structure |
| 274 | * |
| 275 | * Returns 0 on success otherwise negative errno. |
| 276 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 277 | static int dwc3_core_init(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 278 | { |
| 279 | unsigned long timeout; |
| 280 | u32 reg; |
| 281 | int ret; |
| 282 | |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 283 | reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); |
| 284 | /* This should read as U3 followed by revision number */ |
| 285 | if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) { |
| 286 | dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); |
| 287 | ret = -ENODEV; |
| 288 | goto err0; |
| 289 | } |
Felipe Balbi | 248b122 | 2011-12-14 21:59:30 +0200 | [diff] [blame] | 290 | dwc->revision = reg; |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 291 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 292 | /* issue device SoftReset too */ |
| 293 | timeout = jiffies + msecs_to_jiffies(500); |
| 294 | dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); |
| 295 | do { |
| 296 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 297 | if (!(reg & DWC3_DCTL_CSFTRST)) |
| 298 | break; |
| 299 | |
| 300 | if (time_after(jiffies, timeout)) { |
| 301 | dev_err(dwc->dev, "Reset Timed Out\n"); |
| 302 | ret = -ETIMEDOUT; |
| 303 | goto err0; |
| 304 | } |
| 305 | |
| 306 | cpu_relax(); |
| 307 | } while (true); |
| 308 | |
Pratyush Anand | 58a0f23 | 2012-06-21 17:44:29 +0530 | [diff] [blame] | 309 | dwc3_core_soft_reset(dwc); |
| 310 | |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 311 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
Paul Zimmerman | 3e87c42 | 2012-02-24 17:32:13 -0800 | [diff] [blame] | 312 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 313 | reg &= ~DWC3_GCTL_DISSCRAMBLE; |
| 314 | |
Sebastian Andrzej Siewior | 164d773 | 2011-11-24 11:22:05 +0100 | [diff] [blame] | 315 | switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 316 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
| 317 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; |
| 318 | break; |
| 319 | default: |
| 320 | dev_dbg(dwc->dev, "No power optimization available\n"); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * WORKAROUND: DWC3 revisions <1.90a have a bug |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 325 | * where the device can fail to connect at SuperSpeed |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 326 | * and falls back to high-speed mode which causes |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 327 | * the device to enter a Connect/Disconnect loop |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 328 | */ |
| 329 | if (dwc->revision < DWC3_REVISION_190A) |
| 330 | reg |= DWC3_GCTL_U2RSTECN; |
| 331 | |
Felipe Balbi | 789451f6 | 2011-05-05 15:53:10 +0300 | [diff] [blame] | 332 | dwc3_core_num_eps(dwc); |
| 333 | |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 334 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 335 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 336 | return 0; |
| 337 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 338 | err0: |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | static void dwc3_core_exit(struct dwc3 *dwc) |
| 343 | { |
Vivek Gautam | 01b8daf | 2012-10-13 19:20:18 +0530 | [diff] [blame] | 344 | usb_phy_shutdown(dwc->usb2_phy); |
| 345 | usb_phy_shutdown(dwc->usb3_phy); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | #define DWC3_ALIGN_MASK (16 - 1) |
| 349 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 350 | static int dwc3_probe(struct platform_device *pdev) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 351 | { |
Felipe Balbi | 6462cbd | 2013-06-30 14:19:33 +0300 | [diff] [blame] | 352 | struct dwc3_platform_data *pdata = pdev->dev.platform_data; |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 353 | struct device_node *node = pdev->dev.of_node; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 354 | struct resource *res; |
| 355 | struct dwc3 *dwc; |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 356 | struct device *dev = &pdev->dev; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 357 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 358 | int ret = -ENOMEM; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 359 | |
| 360 | void __iomem *regs; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 361 | void *mem; |
| 362 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 363 | mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 364 | if (!mem) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 365 | dev_err(dev, "not enough memory\n"); |
| 366 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 367 | } |
| 368 | dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); |
| 369 | dwc->mem = mem; |
| 370 | |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 371 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 372 | if (!res) { |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 373 | dev_err(dev, "missing IRQ\n"); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 374 | return -ENODEV; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 375 | } |
Kishon Vijay Abraham I | 066618b | 2012-08-21 14:56:16 +0530 | [diff] [blame] | 376 | dwc->xhci_resources[1].start = res->start; |
| 377 | dwc->xhci_resources[1].end = res->end; |
| 378 | dwc->xhci_resources[1].flags = res->flags; |
| 379 | dwc->xhci_resources[1].name = res->name; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 380 | |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 381 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 382 | if (!res) { |
| 383 | dev_err(dev, "missing memory resource\n"); |
| 384 | return -ENODEV; |
| 385 | } |
Kishon Vijay Abraham I | 066618b | 2012-08-21 14:56:16 +0530 | [diff] [blame] | 386 | dwc->xhci_resources[0].start = res->start; |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 387 | dwc->xhci_resources[0].end = dwc->xhci_resources[0].start + |
| 388 | DWC3_XHCI_REGS_END; |
Kishon Vijay Abraham I | 066618b | 2012-08-21 14:56:16 +0530 | [diff] [blame] | 389 | dwc->xhci_resources[0].flags = res->flags; |
| 390 | dwc->xhci_resources[0].name = res->name; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 391 | |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 392 | /* |
| 393 | * Request memory region but exclude xHCI regs, |
| 394 | * since it will be requested by the xhci-plat driver. |
| 395 | */ |
| 396 | res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START, |
| 397 | resource_size(res) - DWC3_GLOBALS_REGS_START, |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 398 | dev_name(dev)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 399 | if (!res) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 400 | dev_err(dev, "can't request mem region\n"); |
| 401 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 402 | } |
| 403 | |
Felipe Balbi | b7e38aa | 2012-08-10 09:16:43 +0300 | [diff] [blame] | 404 | regs = devm_ioremap_nocache(dev, res->start, resource_size(res)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 405 | if (!regs) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 406 | dev_err(dev, "ioremap failed\n"); |
| 407 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 408 | } |
| 409 | |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 410 | if (node) { |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 411 | dwc->maximum_speed = of_usb_get_maximum_speed(node); |
| 412 | |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 413 | dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0); |
| 414 | dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1); |
Felipe Balbi | 6462cbd | 2013-06-30 14:19:33 +0300 | [diff] [blame] | 415 | |
| 416 | dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize"); |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 417 | dwc->dr_mode = of_usb_get_dr_mode(node); |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 418 | } else { |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 419 | dwc->maximum_speed = pdata->maximum_speed; |
| 420 | |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 421 | dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); |
| 422 | dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3); |
Felipe Balbi | 6462cbd | 2013-06-30 14:19:33 +0300 | [diff] [blame] | 423 | |
| 424 | dwc->needs_fifo_resize = pdata->tx_fifo_resize; |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 425 | dwc->dr_mode = pdata->dr_mode; |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 426 | } |
| 427 | |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 428 | /* default to superspeed if no maximum_speed passed */ |
| 429 | if (dwc->maximum_speed == USB_SPEED_UNKNOWN) |
| 430 | dwc->maximum_speed = USB_SPEED_SUPER; |
| 431 | |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 432 | if (IS_ERR(dwc->usb2_phy)) { |
| 433 | ret = PTR_ERR(dwc->usb2_phy); |
| 434 | |
| 435 | /* |
| 436 | * if -ENXIO is returned, it means PHY layer wasn't |
| 437 | * enabled, so it makes no sense to return -EPROBE_DEFER |
| 438 | * in that case, since no PHY driver will ever probe. |
| 439 | */ |
| 440 | if (ret == -ENXIO) |
| 441 | return ret; |
| 442 | |
Felipe Balbi | 51e1e7b | 2012-07-19 14:09:48 +0300 | [diff] [blame] | 443 | dev_err(dev, "no usb2 phy configured\n"); |
| 444 | return -EPROBE_DEFER; |
| 445 | } |
| 446 | |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 447 | if (IS_ERR(dwc->usb3_phy)) { |
Ruchika Kharwar | 315955d7 | 2013-07-04 00:59:34 -0500 | [diff] [blame] | 448 | ret = PTR_ERR(dwc->usb3_phy); |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 449 | |
| 450 | /* |
| 451 | * if -ENXIO is returned, it means PHY layer wasn't |
| 452 | * enabled, so it makes no sense to return -EPROBE_DEFER |
| 453 | * in that case, since no PHY driver will ever probe. |
| 454 | */ |
| 455 | if (ret == -ENXIO) |
| 456 | return ret; |
| 457 | |
Felipe Balbi | 51e1e7b | 2012-07-19 14:09:48 +0300 | [diff] [blame] | 458 | dev_err(dev, "no usb3 phy configured\n"); |
| 459 | return -EPROBE_DEFER; |
| 460 | } |
| 461 | |
Kishon Vijay Abraham I | 8ba007a | 2013-01-25 08:30:54 +0530 | [diff] [blame] | 462 | usb_phy_set_suspend(dwc->usb2_phy, 0); |
| 463 | usb_phy_set_suspend(dwc->usb3_phy, 0); |
| 464 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 465 | spin_lock_init(&dwc->lock); |
| 466 | platform_set_drvdata(pdev, dwc); |
| 467 | |
| 468 | dwc->regs = regs; |
| 469 | dwc->regs_size = resource_size(res); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 470 | dwc->dev = dev; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 471 | |
Kishon Vijay Abraham I | ddff14f | 2013-03-07 18:51:43 +0530 | [diff] [blame] | 472 | dev->dma_mask = dev->parent->dma_mask; |
| 473 | dev->dma_parms = dev->parent->dma_parms; |
| 474 | dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask); |
| 475 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 476 | pm_runtime_enable(dev); |
| 477 | pm_runtime_get_sync(dev); |
| 478 | pm_runtime_forbid(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 479 | |
Kishon Vijay Abraham I | 4fd2448 | 2012-11-16 12:07:54 +0530 | [diff] [blame] | 480 | dwc3_cache_hwparams(dwc); |
| 481 | |
Felipe Balbi | 3921426 | 2012-10-11 13:54:36 +0300 | [diff] [blame] | 482 | ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); |
| 483 | if (ret) { |
| 484 | dev_err(dwc->dev, "failed to allocate event buffers\n"); |
| 485 | ret = -ENOMEM; |
| 486 | goto err0; |
| 487 | } |
| 488 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 489 | ret = dwc3_core_init(dwc); |
| 490 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 491 | dev_err(dev, "failed to initialize core\n"); |
Felipe Balbi | 3921426 | 2012-10-11 13:54:36 +0300 | [diff] [blame] | 492 | goto err0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 493 | } |
| 494 | |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 495 | ret = dwc3_event_buffers_setup(dwc); |
| 496 | if (ret) { |
| 497 | dev_err(dwc->dev, "failed to setup event buffers\n"); |
| 498 | goto err1; |
| 499 | } |
| 500 | |
Vivek Gautam | cd051da | 2013-03-02 18:55:24 +0530 | [diff] [blame] | 501 | if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 502 | dwc->dr_mode = USB_DR_MODE_HOST; |
Vivek Gautam | cd051da | 2013-03-02 18:55:24 +0530 | [diff] [blame] | 503 | else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 504 | dwc->dr_mode = USB_DR_MODE_PERIPHERAL; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 505 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 506 | if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) |
| 507 | dwc->dr_mode = USB_DR_MODE_OTG; |
| 508 | |
| 509 | switch (dwc->dr_mode) { |
| 510 | case USB_DR_MODE_PERIPHERAL: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 511 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 512 | ret = dwc3_gadget_init(dwc); |
| 513 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 514 | dev_err(dev, "failed to initialize gadget\n"); |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 515 | goto err2; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 516 | } |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 517 | break; |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 518 | case USB_DR_MODE_HOST: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 519 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 520 | ret = dwc3_host_init(dwc); |
| 521 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 522 | dev_err(dev, "failed to initialize host\n"); |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 523 | goto err2; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 524 | } |
| 525 | break; |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 526 | case USB_DR_MODE_OTG: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 527 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 528 | ret = dwc3_host_init(dwc); |
| 529 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 530 | dev_err(dev, "failed to initialize host\n"); |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 531 | goto err2; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | ret = dwc3_gadget_init(dwc); |
| 535 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 536 | dev_err(dev, "failed to initialize gadget\n"); |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 537 | goto err2; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 538 | } |
| 539 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 540 | default: |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 541 | dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode); |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 542 | goto err2; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | ret = dwc3_debugfs_init(dwc); |
| 546 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 547 | dev_err(dev, "failed to initialize debugfs\n"); |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 548 | goto err3; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 549 | } |
| 550 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 551 | pm_runtime_allow(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 552 | |
| 553 | return 0; |
| 554 | |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 555 | err3: |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 556 | switch (dwc->dr_mode) { |
| 557 | case USB_DR_MODE_PERIPHERAL: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 558 | dwc3_gadget_exit(dwc); |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 559 | break; |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 560 | case USB_DR_MODE_HOST: |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 561 | dwc3_host_exit(dwc); |
| 562 | break; |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 563 | case USB_DR_MODE_OTG: |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 564 | dwc3_host_exit(dwc); |
| 565 | dwc3_gadget_exit(dwc); |
| 566 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 567 | default: |
| 568 | /* do nothing */ |
| 569 | break; |
| 570 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 571 | |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 572 | err2: |
| 573 | dwc3_event_buffers_cleanup(dwc); |
| 574 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 575 | err1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 576 | dwc3_core_exit(dwc); |
| 577 | |
Felipe Balbi | 3921426 | 2012-10-11 13:54:36 +0300 | [diff] [blame] | 578 | err0: |
| 579 | dwc3_free_event_buffers(dwc); |
| 580 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 581 | return ret; |
| 582 | } |
| 583 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 584 | static int dwc3_remove(struct platform_device *pdev) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 585 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 586 | struct dwc3 *dwc = platform_get_drvdata(pdev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 587 | |
Kishon Vijay Abraham I | 8ba007a | 2013-01-25 08:30:54 +0530 | [diff] [blame] | 588 | usb_phy_set_suspend(dwc->usb2_phy, 1); |
| 589 | usb_phy_set_suspend(dwc->usb3_phy, 1); |
| 590 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 591 | pm_runtime_put(&pdev->dev); |
| 592 | pm_runtime_disable(&pdev->dev); |
| 593 | |
| 594 | dwc3_debugfs_exit(dwc); |
| 595 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 596 | switch (dwc->dr_mode) { |
| 597 | case USB_DR_MODE_PERIPHERAL: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 598 | dwc3_gadget_exit(dwc); |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 599 | break; |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 600 | case USB_DR_MODE_HOST: |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 601 | dwc3_host_exit(dwc); |
| 602 | break; |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 603 | case USB_DR_MODE_OTG: |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 604 | dwc3_host_exit(dwc); |
| 605 | dwc3_gadget_exit(dwc); |
| 606 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 607 | default: |
| 608 | /* do nothing */ |
| 609 | break; |
| 610 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 611 | |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 612 | dwc3_event_buffers_cleanup(dwc); |
Felipe Balbi | d9b4330 | 2013-02-08 15:14:16 +0200 | [diff] [blame] | 613 | dwc3_free_event_buffers(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 614 | dwc3_core_exit(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
Jingoo Han | 19fda7c | 2013-03-26 01:52:48 +0000 | [diff] [blame] | 619 | #ifdef CONFIG_PM_SLEEP |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 620 | static int dwc3_prepare(struct device *dev) |
| 621 | { |
| 622 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 623 | unsigned long flags; |
| 624 | |
| 625 | spin_lock_irqsave(&dwc->lock, flags); |
| 626 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 627 | switch (dwc->dr_mode) { |
| 628 | case USB_DR_MODE_PERIPHERAL: |
| 629 | case USB_DR_MODE_OTG: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 630 | dwc3_gadget_prepare(dwc); |
| 631 | /* FALLTHROUGH */ |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 632 | case USB_DR_MODE_HOST: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 633 | default: |
| 634 | dwc3_event_buffers_cleanup(dwc); |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | static void dwc3_complete(struct device *dev) |
| 644 | { |
| 645 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 646 | unsigned long flags; |
| 647 | |
| 648 | spin_lock_irqsave(&dwc->lock, flags); |
| 649 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 650 | switch (dwc->dr_mode) { |
| 651 | case USB_DR_MODE_PERIPHERAL: |
| 652 | case USB_DR_MODE_OTG: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 653 | dwc3_gadget_complete(dwc); |
| 654 | /* FALLTHROUGH */ |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 655 | case USB_DR_MODE_HOST: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 656 | default: |
| 657 | dwc3_event_buffers_setup(dwc); |
| 658 | break; |
| 659 | } |
| 660 | |
| 661 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 662 | } |
| 663 | |
| 664 | static int dwc3_suspend(struct device *dev) |
| 665 | { |
| 666 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 667 | unsigned long flags; |
| 668 | |
| 669 | spin_lock_irqsave(&dwc->lock, flags); |
| 670 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 671 | switch (dwc->dr_mode) { |
| 672 | case USB_DR_MODE_PERIPHERAL: |
| 673 | case USB_DR_MODE_OTG: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 674 | dwc3_gadget_suspend(dwc); |
| 675 | /* FALLTHROUGH */ |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 676 | case USB_DR_MODE_HOST: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 677 | default: |
| 678 | /* do nothing */ |
| 679 | break; |
| 680 | } |
| 681 | |
| 682 | dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 683 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 684 | |
| 685 | usb_phy_shutdown(dwc->usb3_phy); |
| 686 | usb_phy_shutdown(dwc->usb2_phy); |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static int dwc3_resume(struct device *dev) |
| 692 | { |
| 693 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 694 | unsigned long flags; |
| 695 | |
| 696 | usb_phy_init(dwc->usb3_phy); |
| 697 | usb_phy_init(dwc->usb2_phy); |
| 698 | msleep(100); |
| 699 | |
| 700 | spin_lock_irqsave(&dwc->lock, flags); |
| 701 | |
| 702 | dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl); |
| 703 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 704 | switch (dwc->dr_mode) { |
| 705 | case USB_DR_MODE_PERIPHERAL: |
| 706 | case USB_DR_MODE_OTG: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 707 | dwc3_gadget_resume(dwc); |
| 708 | /* FALLTHROUGH */ |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame^] | 709 | case USB_DR_MODE_HOST: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 710 | default: |
| 711 | /* do nothing */ |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 716 | |
| 717 | pm_runtime_disable(dev); |
| 718 | pm_runtime_set_active(dev); |
| 719 | pm_runtime_enable(dev); |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static const struct dev_pm_ops dwc3_dev_pm_ops = { |
| 725 | .prepare = dwc3_prepare, |
| 726 | .complete = dwc3_complete, |
| 727 | |
| 728 | SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume) |
| 729 | }; |
| 730 | |
| 731 | #define DWC3_PM_OPS &(dwc3_dev_pm_ops) |
| 732 | #else |
| 733 | #define DWC3_PM_OPS NULL |
| 734 | #endif |
| 735 | |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 736 | #ifdef CONFIG_OF |
| 737 | static const struct of_device_id of_dwc3_match[] = { |
| 738 | { |
Felipe Balbi | 22a5aa1 | 2013-07-02 21:20:24 +0300 | [diff] [blame] | 739 | .compatible = "snps,dwc3" |
| 740 | }, |
| 741 | { |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 742 | .compatible = "synopsys,dwc3" |
| 743 | }, |
| 744 | { }, |
| 745 | }; |
| 746 | MODULE_DEVICE_TABLE(of, of_dwc3_match); |
| 747 | #endif |
| 748 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 749 | static struct platform_driver dwc3_driver = { |
| 750 | .probe = dwc3_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 751 | .remove = dwc3_remove, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 752 | .driver = { |
| 753 | .name = "dwc3", |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 754 | .of_match_table = of_match_ptr(of_dwc3_match), |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 755 | .pm = DWC3_PM_OPS, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 756 | }, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 757 | }; |
| 758 | |
Tobias Klauser | b1116dc | 2012-02-28 12:57:20 +0100 | [diff] [blame] | 759 | module_platform_driver(dwc3_driver); |
| 760 | |
Sebastian Andrzej Siewior | 7ae4fc4 | 2011-10-19 19:39:50 +0200 | [diff] [blame] | 761 | MODULE_ALIAS("platform:dwc3"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 762 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 763 | MODULE_LICENSE("GPL v2"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 764 | MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); |