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); |
| 102 | clear_bit(id, dwc3_devs); |
| 103 | } |
| 104 | EXPORT_SYMBOL_GPL(dwc3_put_device_id); |
| 105 | |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 106 | void dwc3_set_mode(struct dwc3 *dwc, u32 mode) |
| 107 | { |
| 108 | u32 reg; |
| 109 | |
| 110 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 111 | reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); |
| 112 | reg |= DWC3_GCTL_PRTCAPDIR(mode); |
| 113 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 114 | } |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 115 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 116 | /** |
| 117 | * dwc3_core_soft_reset - Issues core soft reset and PHY reset |
| 118 | * @dwc: pointer to our context structure |
| 119 | */ |
| 120 | static void dwc3_core_soft_reset(struct dwc3 *dwc) |
| 121 | { |
| 122 | u32 reg; |
| 123 | |
| 124 | /* Before Resetting PHY, put Core in Reset */ |
| 125 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 126 | reg |= DWC3_GCTL_CORESOFTRESET; |
| 127 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 128 | |
| 129 | /* Assert USB3 PHY reset */ |
| 130 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 131 | reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST; |
| 132 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 133 | |
| 134 | /* Assert USB2 PHY reset */ |
| 135 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 136 | reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST; |
| 137 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 138 | |
| 139 | mdelay(100); |
| 140 | |
| 141 | /* Clear USB3 PHY reset */ |
| 142 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 143 | reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST; |
| 144 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 145 | |
| 146 | /* Clear USB2 PHY reset */ |
| 147 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 148 | reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST; |
| 149 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 150 | |
| 151 | /* After PHYs are stable we can take Core out of reset state */ |
| 152 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 153 | reg &= ~DWC3_GCTL_CORESOFTRESET; |
| 154 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * dwc3_free_one_event_buffer - Frees one event buffer |
| 159 | * @dwc: Pointer to our controller context structure |
| 160 | * @evt: Pointer to event buffer to be freed |
| 161 | */ |
| 162 | static void dwc3_free_one_event_buffer(struct dwc3 *dwc, |
| 163 | struct dwc3_event_buffer *evt) |
| 164 | { |
| 165 | dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma); |
| 166 | kfree(evt); |
| 167 | } |
| 168 | |
| 169 | /** |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 170 | * dwc3_alloc_one_event_buffer - Allocates one event buffer structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 171 | * @dwc: Pointer to our controller context structure |
| 172 | * @length: size of the event buffer |
| 173 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 174 | * Returns a pointer to the allocated event buffer structure on success |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 175 | * otherwise ERR_PTR(errno). |
| 176 | */ |
| 177 | static struct dwc3_event_buffer *__devinit |
| 178 | dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length) |
| 179 | { |
| 180 | struct dwc3_event_buffer *evt; |
| 181 | |
| 182 | evt = kzalloc(sizeof(*evt), GFP_KERNEL); |
| 183 | if (!evt) |
| 184 | return ERR_PTR(-ENOMEM); |
| 185 | |
| 186 | evt->dwc = dwc; |
| 187 | evt->length = length; |
| 188 | evt->buf = dma_alloc_coherent(dwc->dev, length, |
| 189 | &evt->dma, GFP_KERNEL); |
| 190 | if (!evt->buf) { |
| 191 | kfree(evt); |
| 192 | return ERR_PTR(-ENOMEM); |
| 193 | } |
| 194 | |
| 195 | return evt; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * dwc3_free_event_buffers - frees all allocated event buffers |
| 200 | * @dwc: Pointer to our controller context structure |
| 201 | */ |
| 202 | static void dwc3_free_event_buffers(struct dwc3 *dwc) |
| 203 | { |
| 204 | struct dwc3_event_buffer *evt; |
| 205 | int i; |
| 206 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 207 | for (i = 0; i < dwc->num_event_buffers; i++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 208 | evt = dwc->ev_buffs[i]; |
Anton Tikhomirov | 64b6c8a | 2012-03-06 17:05:15 +0900 | [diff] [blame] | 209 | if (evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 210 | dwc3_free_one_event_buffer(dwc, evt); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 211 | } |
Anton Tikhomirov | 64b6c8a | 2012-03-06 17:05:15 +0900 | [diff] [blame] | 212 | |
| 213 | kfree(dwc->ev_buffs); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
| 217 | * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 218 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 219 | * @length: size of event buffer |
| 220 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 221 | * Returns 0 on success otherwise negative errno. In the error case, dwc |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 222 | * may contain some buffers allocated but not all which were requested. |
| 223 | */ |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 224 | static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 225 | { |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 226 | int num; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 227 | int i; |
| 228 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 229 | num = DWC3_NUM_INT(dwc->hwparams.hwparams1); |
| 230 | dwc->num_event_buffers = num; |
| 231 | |
Felipe Balbi | 457d3f2 | 2011-10-24 12:03:13 +0300 | [diff] [blame] | 232 | dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL); |
| 233 | if (!dwc->ev_buffs) { |
| 234 | dev_err(dwc->dev, "can't allocate event buffers array\n"); |
| 235 | return -ENOMEM; |
| 236 | } |
| 237 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 238 | for (i = 0; i < num; i++) { |
| 239 | struct dwc3_event_buffer *evt; |
| 240 | |
| 241 | evt = dwc3_alloc_one_event_buffer(dwc, length); |
| 242 | if (IS_ERR(evt)) { |
| 243 | dev_err(dwc->dev, "can't allocate event buffer\n"); |
| 244 | return PTR_ERR(evt); |
| 245 | } |
| 246 | dwc->ev_buffs[i] = evt; |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * dwc3_event_buffers_setup - setup our allocated event buffers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 254 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 255 | * |
| 256 | * Returns 0 on success otherwise negative errno. |
| 257 | */ |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame^] | 258 | static int dwc3_event_buffers_setup(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 259 | { |
| 260 | struct dwc3_event_buffer *evt; |
| 261 | int n; |
| 262 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 263 | for (n = 0; n < dwc->num_event_buffers; n++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 264 | evt = dwc->ev_buffs[n]; |
| 265 | dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n", |
| 266 | evt->buf, (unsigned long long) evt->dma, |
| 267 | evt->length); |
| 268 | |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame^] | 269 | evt->lpos = 0; |
| 270 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 271 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), |
| 272 | lower_32_bits(evt->dma)); |
| 273 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), |
| 274 | upper_32_bits(evt->dma)); |
| 275 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), |
| 276 | evt->length & 0xffff); |
| 277 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) |
| 284 | { |
| 285 | struct dwc3_event_buffer *evt; |
| 286 | int n; |
| 287 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 288 | for (n = 0; n < dwc->num_event_buffers; n++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 289 | evt = dwc->ev_buffs[n]; |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame^] | 290 | |
| 291 | evt->lpos = 0; |
| 292 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 293 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0); |
| 294 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0); |
| 295 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0); |
| 296 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); |
| 297 | } |
| 298 | } |
| 299 | |
Felipe Balbi | 26ceca9 | 2011-09-30 10:58:49 +0300 | [diff] [blame] | 300 | static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc) |
| 301 | { |
| 302 | struct dwc3_hwparams *parms = &dwc->hwparams; |
| 303 | |
| 304 | parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); |
| 305 | parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); |
| 306 | parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); |
| 307 | parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); |
| 308 | parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); |
| 309 | parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); |
| 310 | parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); |
| 311 | parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); |
| 312 | parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); |
| 313 | } |
| 314 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 315 | /** |
| 316 | * dwc3_core_init - Low-level initialization of DWC3 Core |
| 317 | * @dwc: Pointer to our controller context structure |
| 318 | * |
| 319 | * Returns 0 on success otherwise negative errno. |
| 320 | */ |
| 321 | static int __devinit dwc3_core_init(struct dwc3 *dwc) |
| 322 | { |
| 323 | unsigned long timeout; |
| 324 | u32 reg; |
| 325 | int ret; |
| 326 | |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 327 | reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); |
| 328 | /* This should read as U3 followed by revision number */ |
| 329 | if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) { |
| 330 | dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); |
| 331 | ret = -ENODEV; |
| 332 | goto err0; |
| 333 | } |
Felipe Balbi | 248b122 | 2011-12-14 21:59:30 +0200 | [diff] [blame] | 334 | dwc->revision = reg; |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 335 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 336 | dwc3_core_soft_reset(dwc); |
| 337 | |
| 338 | /* issue device SoftReset too */ |
| 339 | timeout = jiffies + msecs_to_jiffies(500); |
| 340 | dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); |
| 341 | do { |
| 342 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 343 | if (!(reg & DWC3_DCTL_CSFTRST)) |
| 344 | break; |
| 345 | |
| 346 | if (time_after(jiffies, timeout)) { |
| 347 | dev_err(dwc->dev, "Reset Timed Out\n"); |
| 348 | ret = -ETIMEDOUT; |
| 349 | goto err0; |
| 350 | } |
| 351 | |
| 352 | cpu_relax(); |
| 353 | } while (true); |
| 354 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 355 | dwc3_cache_hwparams(dwc); |
| 356 | |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 357 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
Paul Zimmerman | 3e87c42 | 2012-02-24 17:32:13 -0800 | [diff] [blame] | 358 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 359 | reg &= ~DWC3_GCTL_DISSCRAMBLE; |
| 360 | |
Sebastian Andrzej Siewior | 164d773 | 2011-11-24 11:22:05 +0100 | [diff] [blame] | 361 | switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 362 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
| 363 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; |
| 364 | break; |
| 365 | default: |
| 366 | dev_dbg(dwc->dev, "No power optimization available\n"); |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * WORKAROUND: DWC3 revisions <1.90a have a bug |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 371 | * where the device can fail to connect at SuperSpeed |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 372 | * and falls back to high-speed mode which causes |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 373 | * the device to enter a Connect/Disconnect loop |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 374 | */ |
| 375 | if (dwc->revision < DWC3_REVISION_190A) |
| 376 | reg |= DWC3_GCTL_U2RSTECN; |
| 377 | |
| 378 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 379 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 380 | ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 381 | if (ret) { |
| 382 | dev_err(dwc->dev, "failed to allocate event buffers\n"); |
| 383 | ret = -ENOMEM; |
| 384 | goto err1; |
| 385 | } |
| 386 | |
| 387 | ret = dwc3_event_buffers_setup(dwc); |
| 388 | if (ret) { |
| 389 | dev_err(dwc->dev, "failed to setup event buffers\n"); |
| 390 | goto err1; |
| 391 | } |
| 392 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 393 | return 0; |
| 394 | |
| 395 | err1: |
| 396 | dwc3_free_event_buffers(dwc); |
| 397 | |
| 398 | err0: |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | static void dwc3_core_exit(struct dwc3 *dwc) |
| 403 | { |
| 404 | dwc3_event_buffers_cleanup(dwc); |
| 405 | dwc3_free_event_buffers(dwc); |
| 406 | } |
| 407 | |
| 408 | #define DWC3_ALIGN_MASK (16 - 1) |
| 409 | |
| 410 | static int __devinit dwc3_probe(struct platform_device *pdev) |
| 411 | { |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 412 | struct device_node *node = pdev->dev.of_node; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 413 | struct resource *res; |
| 414 | struct dwc3 *dwc; |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 415 | struct device *dev = &pdev->dev; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 416 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 417 | int ret = -ENOMEM; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 418 | |
| 419 | void __iomem *regs; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 420 | void *mem; |
| 421 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 422 | u8 mode; |
| 423 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 424 | mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 425 | if (!mem) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 426 | dev_err(dev, "not enough memory\n"); |
| 427 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 428 | } |
| 429 | dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); |
| 430 | dwc->mem = mem; |
| 431 | |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 432 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 433 | if (!res) { |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 434 | dev_err(dev, "missing IRQ\n"); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 435 | return -ENODEV; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 436 | } |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 437 | dwc->xhci_resources[1] = *res; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 438 | |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 439 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 440 | if (!res) { |
| 441 | dev_err(dev, "missing memory resource\n"); |
| 442 | return -ENODEV; |
| 443 | } |
| 444 | dwc->xhci_resources[0] = *res; |
| 445 | dwc->xhci_resources[0].end = dwc->xhci_resources[0].start + |
| 446 | DWC3_XHCI_REGS_END; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 447 | |
Ido Shayevitz | 51249dc | 2012-04-24 14:18:39 +0300 | [diff] [blame] | 448 | /* |
| 449 | * Request memory region but exclude xHCI regs, |
| 450 | * since it will be requested by the xhci-plat driver. |
| 451 | */ |
| 452 | res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START, |
| 453 | resource_size(res) - DWC3_GLOBALS_REGS_START, |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 454 | dev_name(dev)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 455 | if (!res) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 456 | dev_err(dev, "can't request mem region\n"); |
| 457 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 458 | } |
| 459 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 460 | regs = devm_ioremap(dev, res->start, resource_size(res)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 461 | if (!regs) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 462 | dev_err(dev, "ioremap failed\n"); |
| 463 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 464 | } |
| 465 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 466 | spin_lock_init(&dwc->lock); |
| 467 | platform_set_drvdata(pdev, dwc); |
| 468 | |
| 469 | dwc->regs = regs; |
| 470 | dwc->regs_size = resource_size(res); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 471 | dwc->dev = dev; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 472 | |
Felipe Balbi | 6c167fc | 2011-10-07 22:55:04 +0300 | [diff] [blame] | 473 | if (!strncmp("super", maximum_speed, 5)) |
| 474 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; |
| 475 | else if (!strncmp("high", maximum_speed, 4)) |
| 476 | dwc->maximum_speed = DWC3_DCFG_HIGHSPEED; |
| 477 | else if (!strncmp("full", maximum_speed, 4)) |
| 478 | dwc->maximum_speed = DWC3_DCFG_FULLSPEED1; |
| 479 | else if (!strncmp("low", maximum_speed, 3)) |
| 480 | dwc->maximum_speed = DWC3_DCFG_LOWSPEED; |
| 481 | else |
| 482 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; |
| 483 | |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 484 | if (of_get_property(node, "tx-fifo-resize", NULL)) |
| 485 | dwc->needs_fifo_resize = true; |
| 486 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 487 | pm_runtime_enable(dev); |
| 488 | pm_runtime_get_sync(dev); |
| 489 | pm_runtime_forbid(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 490 | |
| 491 | ret = dwc3_core_init(dwc); |
| 492 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 493 | dev_err(dev, "failed to initialize core\n"); |
| 494 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 495 | } |
| 496 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 497 | mode = DWC3_MODE(dwc->hwparams.hwparams0); |
| 498 | |
| 499 | switch (mode) { |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 500 | case DWC3_MODE_DEVICE: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 501 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 502 | ret = dwc3_gadget_init(dwc); |
| 503 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 504 | dev_err(dev, "failed to initialize gadget\n"); |
| 505 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 506 | } |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 507 | break; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 508 | case DWC3_MODE_HOST: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 509 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 510 | ret = dwc3_host_init(dwc); |
| 511 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 512 | dev_err(dev, "failed to initialize host\n"); |
| 513 | goto err1; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 514 | } |
| 515 | break; |
| 516 | case DWC3_MODE_DRD: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 517 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); |
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"); |
| 521 | goto err1; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | ret = dwc3_gadget_init(dwc); |
| 525 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 526 | dev_err(dev, "failed to initialize gadget\n"); |
| 527 | goto err1; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 528 | } |
| 529 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 530 | default: |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 531 | dev_err(dev, "Unsupported mode of operation %d\n", mode); |
| 532 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 533 | } |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 534 | dwc->mode = mode; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 535 | |
| 536 | ret = dwc3_debugfs_init(dwc); |
| 537 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 538 | dev_err(dev, "failed to initialize debugfs\n"); |
| 539 | goto err2; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 540 | } |
| 541 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 542 | pm_runtime_allow(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 543 | |
| 544 | return 0; |
| 545 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 546 | err2: |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 547 | switch (mode) { |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 548 | case DWC3_MODE_DEVICE: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 549 | dwc3_gadget_exit(dwc); |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 550 | break; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 551 | case DWC3_MODE_HOST: |
| 552 | dwc3_host_exit(dwc); |
| 553 | break; |
| 554 | case DWC3_MODE_DRD: |
| 555 | dwc3_host_exit(dwc); |
| 556 | dwc3_gadget_exit(dwc); |
| 557 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 558 | default: |
| 559 | /* do nothing */ |
| 560 | break; |
| 561 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 562 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 563 | err1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 564 | dwc3_core_exit(dwc); |
| 565 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | static int __devexit dwc3_remove(struct platform_device *pdev) |
| 570 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 571 | struct dwc3 *dwc = platform_get_drvdata(pdev); |
| 572 | struct resource *res; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 573 | |
| 574 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 575 | |
| 576 | pm_runtime_put(&pdev->dev); |
| 577 | pm_runtime_disable(&pdev->dev); |
| 578 | |
| 579 | dwc3_debugfs_exit(dwc); |
| 580 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 581 | switch (dwc->mode) { |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 582 | case DWC3_MODE_DEVICE: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 583 | dwc3_gadget_exit(dwc); |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 584 | break; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 585 | case DWC3_MODE_HOST: |
| 586 | dwc3_host_exit(dwc); |
| 587 | break; |
| 588 | case DWC3_MODE_DRD: |
| 589 | dwc3_host_exit(dwc); |
| 590 | dwc3_gadget_exit(dwc); |
| 591 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 592 | default: |
| 593 | /* do nothing */ |
| 594 | break; |
| 595 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 596 | |
| 597 | dwc3_core_exit(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 602 | static struct platform_driver dwc3_driver = { |
| 603 | .probe = dwc3_probe, |
| 604 | .remove = __devexit_p(dwc3_remove), |
| 605 | .driver = { |
| 606 | .name = "dwc3", |
| 607 | }, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 608 | }; |
| 609 | |
Tobias Klauser | b1116dc | 2012-02-28 12:57:20 +0100 | [diff] [blame] | 610 | module_platform_driver(dwc3_driver); |
| 611 | |
Sebastian Andrzej Siewior | 7ae4fc4 | 2011-10-19 19:39:50 +0200 | [diff] [blame] | 612 | MODULE_ALIAS("platform:dwc3"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 613 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
| 614 | MODULE_LICENSE("Dual BSD/GPL"); |
| 615 | MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); |