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