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