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 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions, and the following disclaimer, |
| 14 | * without modification. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. The names of the above-listed copyright holders may not be used |
| 19 | * to endorse or promote products derived from this software without |
| 20 | * specific prior written permission. |
| 21 | * |
| 22 | * ALTERNATIVELY, this software may be distributed under the terms of the |
| 23 | * GNU General Public License ("GPL") version 2, as published by the Free |
| 24 | * Software Foundation. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 27 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 28 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 29 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 30 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 32 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 33 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 36 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
Felipe Balbi | a72e658 | 2011-09-05 13:37:28 +0300 | [diff] [blame] | 39 | #include <linux/module.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 40 | #include <linux/kernel.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/spinlock.h> |
| 43 | #include <linux/platform_device.h> |
| 44 | #include <linux/pm_runtime.h> |
| 45 | #include <linux/interrupt.h> |
| 46 | #include <linux/ioport.h> |
| 47 | #include <linux/io.h> |
| 48 | #include <linux/list.h> |
| 49 | #include <linux/delay.h> |
| 50 | #include <linux/dma-mapping.h> |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 51 | #include <linux/of.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 52 | |
| 53 | #include <linux/usb/ch9.h> |
| 54 | #include <linux/usb/gadget.h> |
| 55 | |
| 56 | #include "core.h" |
| 57 | #include "gadget.h" |
| 58 | #include "io.h" |
| 59 | |
| 60 | #include "debug.h" |
| 61 | |
Felipe Balbi | 6c167fc | 2011-10-07 22:55:04 +0300 | [diff] [blame] | 62 | static char *maximum_speed = "super"; |
| 63 | module_param(maximum_speed, charp, 0); |
| 64 | MODULE_PARM_DESC(maximum_speed, "Maximum supported speed."); |
| 65 | |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 66 | /* -------------------------------------------------------------------------- */ |
| 67 | |
| 68 | #define DWC3_DEVS_POSSIBLE 32 |
| 69 | |
| 70 | static DECLARE_BITMAP(dwc3_devs, DWC3_DEVS_POSSIBLE); |
| 71 | |
| 72 | int dwc3_get_device_id(void) |
| 73 | { |
| 74 | int id; |
| 75 | |
| 76 | again: |
| 77 | id = find_first_zero_bit(dwc3_devs, DWC3_DEVS_POSSIBLE); |
| 78 | if (id < DWC3_DEVS_POSSIBLE) { |
| 79 | int old; |
| 80 | |
| 81 | old = test_and_set_bit(id, dwc3_devs); |
| 82 | if (old) |
| 83 | goto again; |
| 84 | } else { |
| 85 | pr_err("dwc3: no space for new device\n"); |
| 86 | id = -ENOMEM; |
| 87 | } |
| 88 | |
Dan Carpenter | 075cd14 | 2012-02-04 16:37:14 +0300 | [diff] [blame] | 89 | return id; |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 90 | } |
| 91 | EXPORT_SYMBOL_GPL(dwc3_get_device_id); |
| 92 | |
| 93 | void dwc3_put_device_id(int id) |
| 94 | { |
| 95 | int ret; |
| 96 | |
| 97 | if (id < 0) |
| 98 | return; |
| 99 | |
| 100 | ret = test_bit(id, dwc3_devs); |
| 101 | WARN(!ret, "dwc3: ID %d not in use\n", id); |
Oliver Neukum | 5fdeeb8 | 2012-08-26 21:34:19 +0200 | [diff] [blame] | 102 | smp_mb__before_clear_bit(); |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 103 | clear_bit(id, dwc3_devs); |
| 104 | } |
| 105 | EXPORT_SYMBOL_GPL(dwc3_put_device_id); |
| 106 | |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 107 | void dwc3_set_mode(struct dwc3 *dwc, u32 mode) |
| 108 | { |
| 109 | u32 reg; |
| 110 | |
| 111 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 112 | reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); |
| 113 | reg |= DWC3_GCTL_PRTCAPDIR(mode); |
Vijayavardhan Vennapusa | ba79f6b | 2013-07-30 13:39:52 +0530 | [diff] [blame] | 114 | /* |
| 115 | * Set this bit so that device attempts three more times at SS, even |
| 116 | * if it failed previously to operate in SS mode. |
| 117 | */ |
| 118 | reg |= DWC3_GCTL_U2RSTECN; |
| 119 | if (mode == DWC3_GCTL_PRTCAP_HOST) { |
| 120 | /* |
| 121 | * Allow ITP generated off of ref clk based counter instead |
| 122 | * of UTMI/ULPI clk based counter, when superspeed only is |
| 123 | * active so that UTMI/ULPI PHY can be suspened. |
| 124 | */ |
| 125 | reg |= DWC3_GCTL_SOFITPSYNC; |
| 126 | reg &= ~(DWC3_GCTL_PWRDNSCALEMASK); |
| 127 | reg |= DWC3_GCTL_PWRDNSCALE(2); |
| 128 | } else if (mode == DWC3_GCTL_PRTCAP_DEVICE) { |
| 129 | reg &= ~(DWC3_GCTL_PWRDNSCALEMASK); |
| 130 | reg |= DWC3_GCTL_PWRDNSCALE(2); |
| 131 | reg &= ~(DWC3_GCTL_SOFITPSYNC); |
| 132 | } |
| 133 | reg |= DWC3_GCTL_U2EXIT_LFPS; |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 134 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
Vijayavardhan Vennapusa | ba79f6b | 2013-07-30 13:39:52 +0530 | [diff] [blame] | 135 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 136 | reg |= DWC3_GUSB3PIPECTL_SUSPHY; |
| 137 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 138 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 139 | reg |= DWC3_GUSB2PHYCFG_SUSPHY; |
| 140 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 141 | } |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 142 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 143 | /** |
| 144 | * dwc3_core_soft_reset - Issues core soft reset and PHY reset |
| 145 | * @dwc: pointer to our context structure |
| 146 | */ |
| 147 | static void dwc3_core_soft_reset(struct dwc3 *dwc) |
| 148 | { |
| 149 | u32 reg; |
| 150 | |
| 151 | /* Before Resetting PHY, put Core in Reset */ |
| 152 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 153 | reg |= DWC3_GCTL_CORESOFTRESET; |
| 154 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 155 | |
Vijayavardhan Vennapusa | ba79f6b | 2013-07-30 13:39:52 +0530 | [diff] [blame] | 156 | if (dwc->revision >= DWC3_REVISION_230A) |
| 157 | dwc3_notify_event(dwc, DWC3_CONTROLLER_RESET_EVENT); |
| 158 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 159 | /* Assert USB3 PHY reset */ |
| 160 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 161 | reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST; |
| 162 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 163 | |
| 164 | /* Assert USB2 PHY reset */ |
| 165 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 166 | reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST; |
| 167 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 168 | |
| 169 | mdelay(100); |
| 170 | |
| 171 | /* Clear USB3 PHY reset */ |
| 172 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 173 | reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST; |
| 174 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 175 | |
| 176 | /* Clear USB2 PHY reset */ |
| 177 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 178 | reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST; |
| 179 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 180 | |
Pratyush Anand | 38a535c | 2012-06-21 17:44:28 +0530 | [diff] [blame] | 181 | mdelay(100); |
| 182 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 183 | /* After PHYs are stable we can take Core out of reset state */ |
| 184 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 185 | reg &= ~DWC3_GCTL_CORESOFTRESET; |
| 186 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
Vijayavardhan Vennapusa | ba79f6b | 2013-07-30 13:39:52 +0530 | [diff] [blame] | 187 | |
| 188 | if (dwc->revision >= DWC3_REVISION_230A) |
| 189 | dwc3_notify_event(dwc, DWC3_CONTROLLER_POST_RESET_EVENT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * dwc3_free_one_event_buffer - Frees one event buffer |
| 194 | * @dwc: Pointer to our controller context structure |
| 195 | * @evt: Pointer to event buffer to be freed |
| 196 | */ |
| 197 | static void dwc3_free_one_event_buffer(struct dwc3 *dwc, |
| 198 | struct dwc3_event_buffer *evt) |
| 199 | { |
| 200 | dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma); |
| 201 | kfree(evt); |
| 202 | } |
| 203 | |
| 204 | /** |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 205 | * dwc3_alloc_one_event_buffer - Allocates one event buffer structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 206 | * @dwc: Pointer to our controller context structure |
| 207 | * @length: size of the event buffer |
| 208 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 209 | * Returns a pointer to the allocated event buffer structure on success |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 210 | * otherwise ERR_PTR(errno). |
| 211 | */ |
| 212 | static struct dwc3_event_buffer *__devinit |
| 213 | dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length) |
| 214 | { |
| 215 | struct dwc3_event_buffer *evt; |
| 216 | |
| 217 | evt = kzalloc(sizeof(*evt), GFP_KERNEL); |
| 218 | if (!evt) |
| 219 | return ERR_PTR(-ENOMEM); |
| 220 | |
| 221 | evt->dwc = dwc; |
| 222 | evt->length = length; |
| 223 | evt->buf = dma_alloc_coherent(dwc->dev, length, |
| 224 | &evt->dma, GFP_KERNEL); |
| 225 | if (!evt->buf) { |
| 226 | kfree(evt); |
| 227 | return ERR_PTR(-ENOMEM); |
| 228 | } |
| 229 | |
| 230 | return evt; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * dwc3_free_event_buffers - frees all allocated event buffers |
| 235 | * @dwc: Pointer to our controller context structure |
| 236 | */ |
| 237 | static void dwc3_free_event_buffers(struct dwc3 *dwc) |
| 238 | { |
| 239 | struct dwc3_event_buffer *evt; |
| 240 | int i; |
| 241 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 242 | for (i = 0; i < dwc->num_event_buffers; i++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 243 | evt = dwc->ev_buffs[i]; |
Anton Tikhomirov | 64b6c8a | 2012-03-06 17:05:15 +0900 | [diff] [blame] | 244 | if (evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 245 | dwc3_free_one_event_buffer(dwc, evt); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 246 | } |
Anton Tikhomirov | 64b6c8a | 2012-03-06 17:05:15 +0900 | [diff] [blame] | 247 | |
| 248 | kfree(dwc->ev_buffs); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
| 252 | * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 253 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 254 | * @length: size of event buffer |
| 255 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 256 | * Returns 0 on success otherwise negative errno. In the error case, dwc |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 257 | * may contain some buffers allocated but not all which were requested. |
| 258 | */ |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 259 | static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 260 | { |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 261 | int num; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 262 | int i; |
| 263 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 264 | num = DWC3_NUM_INT(dwc->hwparams.hwparams1); |
| 265 | dwc->num_event_buffers = num; |
| 266 | |
Felipe Balbi | 457d3f2 | 2011-10-24 12:03:13 +0300 | [diff] [blame] | 267 | dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL); |
| 268 | if (!dwc->ev_buffs) { |
| 269 | dev_err(dwc->dev, "can't allocate event buffers array\n"); |
| 270 | return -ENOMEM; |
| 271 | } |
| 272 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 273 | for (i = 0; i < num; i++) { |
| 274 | struct dwc3_event_buffer *evt; |
| 275 | |
Vijayavardhan Vennapusa | cf45f02 | 2013-05-30 13:39:00 +0530 | [diff] [blame] | 276 | /* |
| 277 | * As SW workaround, allocate 8 bytes more than size of event |
| 278 | * buffer given to USB Controller to avoid possible memory |
| 279 | * corruption caused by event buffer overflow when Hw writes |
| 280 | * Vendor Device test event which could be of 12 bytes. |
| 281 | */ |
| 282 | evt = dwc3_alloc_one_event_buffer(dwc, (length + 8)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 283 | if (IS_ERR(evt)) { |
| 284 | dev_err(dwc->dev, "can't allocate event buffer\n"); |
| 285 | return PTR_ERR(evt); |
| 286 | } |
| 287 | dwc->ev_buffs[i] = evt; |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * dwc3_event_buffers_setup - setup our allocated event buffers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 295 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 296 | * |
| 297 | * Returns 0 on success otherwise negative errno. |
| 298 | */ |
Wesley Cheng | 446ad8d | 2013-06-05 16:15:01 +0530 | [diff] [blame] | 299 | int dwc3_event_buffers_setup(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 300 | { |
| 301 | struct dwc3_event_buffer *evt; |
| 302 | int n; |
| 303 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 304 | for (n = 0; n < dwc->num_event_buffers; n++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 305 | evt = dwc->ev_buffs[n]; |
| 306 | dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n", |
| 307 | evt->buf, (unsigned long long) evt->dma, |
| 308 | evt->length); |
| 309 | |
Paul Zimmerman | 43fa01a | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 310 | evt->lpos = 0; |
| 311 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 312 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), |
| 313 | lower_32_bits(evt->dma)); |
| 314 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), |
| 315 | upper_32_bits(evt->dma)); |
| 316 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), |
Vijayavardhan Vennapusa | cf45f02 | 2013-05-30 13:39:00 +0530 | [diff] [blame] | 317 | (evt->length - 8) & 0xffff); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 318 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) |
| 325 | { |
| 326 | struct dwc3_event_buffer *evt; |
| 327 | int n; |
| 328 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 329 | for (n = 0; n < dwc->num_event_buffers; n++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 330 | evt = dwc->ev_buffs[n]; |
Paul Zimmerman | 43fa01a | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 331 | |
| 332 | evt->lpos = 0; |
| 333 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 334 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0); |
| 335 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0); |
| 336 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0); |
| 337 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); |
| 338 | } |
| 339 | } |
| 340 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 341 | static void dwc3_cache_hwparams(struct dwc3 *dwc) |
Felipe Balbi | 26ceca9 | 2011-09-30 10:58:49 +0300 | [diff] [blame] | 342 | { |
| 343 | struct dwc3_hwparams *parms = &dwc->hwparams; |
| 344 | |
| 345 | parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); |
| 346 | parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); |
| 347 | parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); |
| 348 | parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); |
| 349 | parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); |
| 350 | parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); |
| 351 | parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); |
| 352 | parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); |
| 353 | parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); |
| 354 | } |
| 355 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 356 | /** |
| 357 | * dwc3_core_init - Low-level initialization of DWC3 Core |
| 358 | * @dwc: Pointer to our controller context structure |
| 359 | * |
| 360 | * Returns 0 on success otherwise negative errno. |
| 361 | */ |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 362 | static int dwc3_core_init(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 363 | { |
| 364 | unsigned long timeout; |
| 365 | u32 reg; |
| 366 | int ret; |
| 367 | |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 368 | reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); |
| 369 | /* This should read as U3 followed by revision number */ |
| 370 | if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) { |
| 371 | dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); |
| 372 | ret = -ENODEV; |
| 373 | goto err0; |
| 374 | } |
Felipe Balbi | 248b122 | 2011-12-14 21:59:30 +0200 | [diff] [blame] | 375 | dwc->revision = reg; |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 376 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 377 | /* issue device SoftReset too */ |
| 378 | timeout = jiffies + msecs_to_jiffies(500); |
| 379 | dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); |
| 380 | do { |
| 381 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 382 | if (!(reg & DWC3_DCTL_CSFTRST)) |
| 383 | break; |
| 384 | |
| 385 | if (time_after(jiffies, timeout)) { |
| 386 | dev_err(dwc->dev, "Reset Timed Out\n"); |
| 387 | ret = -ETIMEDOUT; |
| 388 | goto err0; |
| 389 | } |
| 390 | |
| 391 | cpu_relax(); |
| 392 | } while (true); |
| 393 | |
Pratyush Anand | 99d4da8 | 2012-06-21 17:44:29 +0530 | [diff] [blame] | 394 | dwc3_core_soft_reset(dwc); |
| 395 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 396 | dwc3_cache_hwparams(dwc); |
| 397 | |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 398 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
Paul Zimmerman | 3e87c42 | 2012-02-24 17:32:13 -0800 | [diff] [blame] | 399 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 400 | reg &= ~DWC3_GCTL_DISSCRAMBLE; |
| 401 | |
Sebastian Andrzej Siewior | 164d773 | 2011-11-24 11:22:05 +0100 | [diff] [blame] | 402 | switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 403 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
| 404 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; |
| 405 | break; |
| 406 | default: |
| 407 | dev_dbg(dwc->dev, "No power optimization available\n"); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * WORKAROUND: DWC3 revisions <1.90a have a bug |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 412 | * where the device can fail to connect at SuperSpeed |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 413 | * and falls back to high-speed mode which causes |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 414 | * the device to enter a Connect/Disconnect loop |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 415 | */ |
| 416 | if (dwc->revision < DWC3_REVISION_190A) |
| 417 | reg |= DWC3_GCTL_U2RSTECN; |
| 418 | |
| 419 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 420 | |
Pavankumar Kondeti | fe2c063 | 2012-06-12 15:21:13 +0530 | [diff] [blame] | 421 | /* |
Pavankumar Kondeti | c6e15aa | 2012-07-16 11:37:15 +0530 | [diff] [blame] | 422 | * The default value of GUCTL[31:22] should be 0x8. But on cores |
| 423 | * revision < 2.30a, the default value is mistakenly overridden |
| 424 | * with 0x0. Restore the correct default value. |
| 425 | */ |
| 426 | if (dwc->revision < DWC3_REVISION_230A) { |
| 427 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL); |
| 428 | reg &= ~DWC3_GUCTL_REFCLKPER; |
| 429 | reg |= 0x8 << __ffs(DWC3_GUCTL_REFCLKPER); |
| 430 | dwc3_writel(dwc->regs, DWC3_GUCTL, reg); |
| 431 | } |
| 432 | /* |
Pavankumar Kondeti | fe2c063 | 2012-06-12 15:21:13 +0530 | [diff] [blame] | 433 | * Currently, the default and the recommended value for GUSB3PIPECTL |
| 434 | * [21:19] in the RTL is 3'b100 or 32 consecutive errors. Based on |
| 435 | * analysis and experiments in the lab, it is found that there is a |
| 436 | * relatively low probability of getting 32 consecutive word errors |
| 437 | * in the presence of random recovered noise (during electrical idle). |
| 438 | * This can delay the entry to a low power state such that for |
| 439 | * applications where the link stays in a non-U0 state for a short |
| 440 | * duration (< 1 microsecond), the local PHY does not enter the low |
| 441 | * power state prior to receiving a potential LFPS wakeup. This causes |
| 442 | * the PHY CDR (Clock and Data Recovery) operation to be unstable for |
| 443 | * some Synopsys PHYs. |
| 444 | * |
| 445 | * The proposal now is to change the default and the recommended value |
| 446 | * for GUSB3PIPECTL[21:19] in the RTL from 3'b100 to a minimum of |
| 447 | * 3'b001. Perform the same in software for controllers prior to 2.30a |
| 448 | * revision. |
| 449 | */ |
| 450 | |
| 451 | if (dwc->revision < DWC3_REVISION_230A) { |
| 452 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 453 | reg &= ~DWC3_GUSB3PIPECTL_DELAY_P1P2P3; |
| 454 | reg |= 1 << __ffs(DWC3_GUSB3PIPECTL_DELAY_P1P2P3); |
Pavankumar Kondeti | 5acb4ba | 2012-07-16 11:44:46 +0530 | [diff] [blame] | 455 | /* |
| 456 | * Receiver Detection in U3/Rx.Det is mistakenly disabled in |
| 457 | * cores < 2.30a. Fix it here. |
| 458 | */ |
| 459 | reg &= ~DWC3_GUSB3PIPECTL_DIS_RXDET_U3_RXDET; |
Pavankumar Kondeti | fe2c063 | 2012-06-12 15:21:13 +0530 | [diff] [blame] | 460 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 461 | } |
Vijayavardhan Vennapusa | d0136a7 | 2013-06-07 13:22:18 +0530 | [diff] [blame] | 462 | /* |
| 463 | * clear Elastic buffer mode in GUSBPIPE_CTRL(0) register, otherwise |
| 464 | * it results in high link errors and could cause SS mode transfer |
| 465 | * failure. |
| 466 | */ |
| 467 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 468 | reg &= ~DWC3_GUSB3PIPECTL_ELASTIC_BUF_MODE; |
| 469 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
Pavankumar Kondeti | fe2c063 | 2012-06-12 15:21:13 +0530 | [diff] [blame] | 470 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 471 | if (!dwc->ev_buffs) { |
| 472 | ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); |
| 473 | if (ret) { |
| 474 | dev_err(dwc->dev, "failed to allocate event buffers\n"); |
| 475 | ret = -ENOMEM; |
| 476 | goto err1; |
| 477 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | ret = dwc3_event_buffers_setup(dwc); |
| 481 | if (ret) { |
| 482 | dev_err(dwc->dev, "failed to setup event buffers\n"); |
| 483 | goto err1; |
| 484 | } |
| 485 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 486 | return 0; |
| 487 | |
| 488 | err1: |
| 489 | dwc3_free_event_buffers(dwc); |
| 490 | |
| 491 | err0: |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | static void dwc3_core_exit(struct dwc3 *dwc) |
| 496 | { |
| 497 | dwc3_event_buffers_cleanup(dwc); |
| 498 | dwc3_free_event_buffers(dwc); |
| 499 | } |
| 500 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 501 | /* XHCI reset, resets other CORE registers as well, re-init those */ |
| 502 | void dwc3_post_host_reset_core_init(struct dwc3 *dwc) |
| 503 | { |
| 504 | dwc3_core_init(dwc); |
| 505 | dwc3_gadget_restart(dwc); |
| 506 | } |
| 507 | |
Vijayavardhan Vennapusa | 8a011c9 | 2013-07-29 09:06:48 +0530 | [diff] [blame] | 508 | static void (*notify_event) (struct dwc3 *, unsigned); |
| 509 | void dwc3_set_notifier(void (*notify)(struct dwc3 *, unsigned)) |
| 510 | { |
| 511 | notify_event = notify; |
| 512 | } |
| 513 | EXPORT_SYMBOL(dwc3_set_notifier); |
| 514 | |
| 515 | void dwc3_notify_event(struct dwc3 *dwc, unsigned event) |
| 516 | { |
| 517 | if (dwc->notify_event) |
| 518 | dwc->notify_event(dwc, event); |
| 519 | } |
| 520 | EXPORT_SYMBOL(dwc3_notify_event); |
| 521 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 522 | #define DWC3_ALIGN_MASK (16 - 1) |
| 523 | |
Vijayavardhan Vennapusa | 8eb6873 | 2013-03-26 13:05:38 +0530 | [diff] [blame] | 524 | static u64 dwc3_dma_mask = DMA_BIT_MASK(64); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 525 | static int __devinit dwc3_probe(struct platform_device *pdev) |
| 526 | { |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 527 | struct device_node *node = pdev->dev.of_node; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 528 | struct resource *res; |
| 529 | struct dwc3 *dwc; |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 530 | struct device *dev = &pdev->dev; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 531 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 532 | int ret = -ENOMEM; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 533 | |
| 534 | void __iomem *regs; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 535 | void *mem; |
| 536 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 537 | u8 mode; |
Manu Gautam | bb825d7 | 2013-03-12 16:25:42 +0530 | [diff] [blame] | 538 | bool host_only_mode; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 539 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 540 | mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 541 | if (!mem) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 542 | dev_err(dev, "not enough memory\n"); |
| 543 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 544 | } |
| 545 | dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); |
| 546 | dwc->mem = mem; |
| 547 | |
Vijayavardhan Vennapusa | 8eb6873 | 2013-03-26 13:05:38 +0530 | [diff] [blame] | 548 | if (!dev->dma_mask) |
| 549 | dev->dma_mask = &dwc3_dma_mask; |
| 550 | if (!dev->coherent_dma_mask) |
Hemant Kumar | 1b378d9 | 2013-04-19 11:24:05 -0700 | [diff] [blame] | 551 | dev->coherent_dma_mask = DMA_BIT_MASK(64); |
Vijayavardhan Vennapusa | 8eb6873 | 2013-03-26 13:05:38 +0530 | [diff] [blame] | 552 | |
Vijayavardhan Vennapusa | 8a011c9 | 2013-07-29 09:06:48 +0530 | [diff] [blame] | 553 | dwc->notify_event = notify_event; |
Ido Shayevitz | 4a18733 | 2012-04-23 14:53:37 +0200 | [diff] [blame] | 554 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 555 | if (!res) { |
Ido Shayevitz | 4a18733 | 2012-04-23 14:53:37 +0200 | [diff] [blame] | 556 | dev_err(dev, "missing IRQ\n"); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 557 | return -ENODEV; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 558 | } |
Kishon Vijay Abraham I | 2f8ae02 | 2012-08-21 14:56:16 +0530 | [diff] [blame] | 559 | dwc->xhci_resources[1].start = res->start; |
| 560 | dwc->xhci_resources[1].end = res->end; |
| 561 | dwc->xhci_resources[1].flags = res->flags; |
| 562 | dwc->xhci_resources[1].name = res->name; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 563 | |
Ido Shayevitz | 4a18733 | 2012-04-23 14:53:37 +0200 | [diff] [blame] | 564 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 565 | if (!res) { |
| 566 | dev_err(dev, "missing memory resource\n"); |
| 567 | return -ENODEV; |
| 568 | } |
Kishon Vijay Abraham I | 2f8ae02 | 2012-08-21 14:56:16 +0530 | [diff] [blame] | 569 | dwc->xhci_resources[0].start = res->start; |
Ido Shayevitz | 4a18733 | 2012-04-23 14:53:37 +0200 | [diff] [blame] | 570 | dwc->xhci_resources[0].end = dwc->xhci_resources[0].start + |
| 571 | DWC3_XHCI_REGS_END; |
Kishon Vijay Abraham I | 2f8ae02 | 2012-08-21 14:56:16 +0530 | [diff] [blame] | 572 | dwc->xhci_resources[0].flags = res->flags; |
| 573 | dwc->xhci_resources[0].name = res->name; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 574 | |
Ido Shayevitz | 4a18733 | 2012-04-23 14:53:37 +0200 | [diff] [blame] | 575 | /* |
| 576 | * Request memory region but exclude xHCI regs, |
| 577 | * since it will be requested by the xhci-plat driver. |
| 578 | */ |
| 579 | res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START, |
| 580 | resource_size(res) - DWC3_GLOBALS_REGS_START, |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 581 | dev_name(dev)); |
Ido Shayevitz | 4a18733 | 2012-04-23 14:53:37 +0200 | [diff] [blame] | 582 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 583 | if (!res) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 584 | dev_err(dev, "can't request mem region\n"); |
| 585 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 586 | } |
| 587 | |
Felipe Balbi | 497a2a3 | 2012-08-10 09:16:43 +0300 | [diff] [blame] | 588 | regs = devm_ioremap_nocache(dev, res->start, resource_size(res)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 589 | if (!regs) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 590 | dev_err(dev, "ioremap failed\n"); |
| 591 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 592 | } |
| 593 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 594 | spin_lock_init(&dwc->lock); |
| 595 | platform_set_drvdata(pdev, dwc); |
| 596 | |
| 597 | dwc->regs = regs; |
| 598 | dwc->regs_size = resource_size(res); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 599 | dwc->dev = dev; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 600 | |
Felipe Balbi | 6c167fc | 2011-10-07 22:55:04 +0300 | [diff] [blame] | 601 | if (!strncmp("super", maximum_speed, 5)) |
| 602 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; |
| 603 | else if (!strncmp("high", maximum_speed, 4)) |
| 604 | dwc->maximum_speed = DWC3_DCFG_HIGHSPEED; |
| 605 | else if (!strncmp("full", maximum_speed, 4)) |
| 606 | dwc->maximum_speed = DWC3_DCFG_FULLSPEED1; |
| 607 | else if (!strncmp("low", maximum_speed, 3)) |
| 608 | dwc->maximum_speed = DWC3_DCFG_LOWSPEED; |
| 609 | else |
| 610 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; |
| 611 | |
Kishon Vijay Abraham I | bdc707a | 2013-03-18 12:18:57 +0530 | [diff] [blame] | 612 | dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize"); |
Manu Gautam | bb825d7 | 2013-03-12 16:25:42 +0530 | [diff] [blame] | 613 | host_only_mode = of_property_read_bool(node, "host-only-mode"); |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 614 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 615 | pm_runtime_no_callbacks(dev); |
| 616 | pm_runtime_set_active(dev); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 617 | pm_runtime_enable(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 618 | |
| 619 | ret = dwc3_core_init(dwc); |
| 620 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 621 | dev_err(dev, "failed to initialize core\n"); |
| 622 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 623 | } |
| 624 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 625 | mode = DWC3_MODE(dwc->hwparams.hwparams0); |
| 626 | |
Manu Gautam | bb825d7 | 2013-03-12 16:25:42 +0530 | [diff] [blame] | 627 | /* Override mode if user selects host-only config with DRD core */ |
| 628 | if (host_only_mode && (mode == DWC3_MODE_DRD)) { |
| 629 | dev_dbg(dev, "host only mode selected\n"); |
| 630 | mode = DWC3_MODE_HOST; |
| 631 | } |
| 632 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 633 | switch (mode) { |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 634 | case DWC3_MODE_DEVICE: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 635 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 636 | ret = dwc3_gadget_init(dwc); |
| 637 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 638 | dev_err(dev, "failed to initialize gadget\n"); |
| 639 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 640 | } |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 641 | break; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 642 | case DWC3_MODE_HOST: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 643 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 644 | ret = dwc3_host_init(dwc); |
| 645 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 646 | dev_err(dev, "failed to initialize host\n"); |
| 647 | goto err1; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 648 | } |
| 649 | break; |
| 650 | case DWC3_MODE_DRD: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 651 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 652 | ret = dwc3_otg_init(dwc); |
| 653 | if (ret) { |
| 654 | dev_err(dev, "failed to initialize otg\n"); |
| 655 | goto err1; |
| 656 | } |
| 657 | |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 658 | ret = dwc3_host_init(dwc); |
| 659 | if (ret) { |
| 660 | dev_err(dev, "failed to initialize host\n"); |
| 661 | dwc3_otg_exit(dwc); |
| 662 | goto err1; |
| 663 | } |
| 664 | |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 665 | ret = dwc3_gadget_init(dwc); |
| 666 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 667 | dev_err(dev, "failed to initialize gadget\n"); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 668 | dwc3_host_exit(dwc); |
| 669 | dwc3_otg_exit(dwc); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 670 | goto err1; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 671 | } |
| 672 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 673 | default: |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 674 | dev_err(dev, "Unsupported mode of operation %d\n", mode); |
| 675 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 676 | } |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 677 | dwc->mode = mode; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 678 | |
| 679 | ret = dwc3_debugfs_init(dwc); |
| 680 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 681 | dev_err(dev, "failed to initialize debugfs\n"); |
| 682 | goto err2; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 683 | } |
| 684 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 685 | return 0; |
| 686 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 687 | err2: |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 688 | switch (mode) { |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 689 | case DWC3_MODE_DEVICE: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 690 | dwc3_gadget_exit(dwc); |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 691 | break; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 692 | case DWC3_MODE_HOST: |
| 693 | dwc3_host_exit(dwc); |
| 694 | break; |
| 695 | case DWC3_MODE_DRD: |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 696 | dwc3_gadget_exit(dwc); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 697 | dwc3_host_exit(dwc); |
| 698 | dwc3_otg_exit(dwc); |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 699 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 700 | default: |
| 701 | /* do nothing */ |
| 702 | break; |
| 703 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 704 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 705 | err1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 706 | dwc3_core_exit(dwc); |
| 707 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 708 | return ret; |
| 709 | } |
| 710 | |
| 711 | static int __devexit dwc3_remove(struct platform_device *pdev) |
| 712 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 713 | struct dwc3 *dwc = platform_get_drvdata(pdev); |
| 714 | struct resource *res; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 715 | |
| 716 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 717 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 718 | pm_runtime_disable(&pdev->dev); |
| 719 | |
| 720 | dwc3_debugfs_exit(dwc); |
| 721 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 722 | switch (dwc->mode) { |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 723 | case DWC3_MODE_DEVICE: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 724 | dwc3_gadget_exit(dwc); |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 725 | break; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 726 | case DWC3_MODE_HOST: |
| 727 | dwc3_host_exit(dwc); |
| 728 | break; |
| 729 | case DWC3_MODE_DRD: |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 730 | dwc3_gadget_exit(dwc); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 731 | dwc3_host_exit(dwc); |
| 732 | dwc3_otg_exit(dwc); |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 733 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 734 | default: |
| 735 | /* do nothing */ |
| 736 | break; |
| 737 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 738 | |
| 739 | dwc3_core_exit(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
Kishon Vijay Abraham I | bdc707a | 2013-03-18 12:18:57 +0530 | [diff] [blame] | 744 | #ifdef CONFIG_OF |
| 745 | static const struct of_device_id of_dwc3_match[] = { |
| 746 | { |
| 747 | .compatible = "synopsys,dwc3" |
| 748 | }, |
| 749 | { }, |
| 750 | }; |
| 751 | MODULE_DEVICE_TABLE(of, of_dwc3_match); |
| 752 | #endif |
| 753 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 754 | static struct platform_driver dwc3_driver = { |
| 755 | .probe = dwc3_probe, |
| 756 | .remove = __devexit_p(dwc3_remove), |
| 757 | .driver = { |
| 758 | .name = "dwc3", |
Kishon Vijay Abraham I | bdc707a | 2013-03-18 12:18:57 +0530 | [diff] [blame] | 759 | .of_match_table = of_match_ptr(of_dwc3_match), |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 760 | }, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 761 | }; |
| 762 | |
Tobias Klauser | b1116dc | 2012-02-28 12:57:20 +0100 | [diff] [blame] | 763 | module_platform_driver(dwc3_driver); |
| 764 | |
Sebastian Andrzej Siewior | 7ae4fc4 | 2011-10-19 19:39:50 +0200 | [diff] [blame] | 765 | MODULE_ALIAS("platform:dwc3"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 766 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
| 767 | MODULE_LICENSE("Dual BSD/GPL"); |
| 768 | MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); |