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; |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 411 | struct device *dev = &pdev->dev; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 412 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 413 | int ret = -ENOMEM; |
| 414 | int irq; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 415 | |
| 416 | void __iomem *regs; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 417 | void *mem; |
| 418 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 419 | u8 mode; |
| 420 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 421 | mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 422 | if (!mem) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 423 | dev_err(dev, "not enough memory\n"); |
| 424 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 425 | } |
| 426 | dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); |
| 427 | dwc->mem = mem; |
| 428 | |
| 429 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 430 | if (!res) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 431 | dev_err(dev, "missing resource\n"); |
| 432 | return -ENODEV; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 433 | } |
| 434 | |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 435 | dwc->res = res; |
| 436 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 437 | res = devm_request_mem_region(dev, res->start, resource_size(res), |
| 438 | dev_name(dev)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 439 | if (!res) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 440 | dev_err(dev, "can't request mem region\n"); |
| 441 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 442 | } |
| 443 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 444 | regs = devm_ioremap(dev, res->start, resource_size(res)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 445 | if (!regs) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 446 | dev_err(dev, "ioremap failed\n"); |
| 447 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | irq = platform_get_irq(pdev, 0); |
| 451 | if (irq < 0) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 452 | dev_err(dev, "missing IRQ\n"); |
| 453 | return -ENODEV; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | spin_lock_init(&dwc->lock); |
| 457 | platform_set_drvdata(pdev, dwc); |
| 458 | |
| 459 | dwc->regs = regs; |
| 460 | dwc->regs_size = resource_size(res); |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 461 | dwc->dev = dev; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 462 | dwc->irq = irq; |
| 463 | |
Felipe Balbi | 6c167fc | 2011-10-07 22:55:04 +0300 | [diff] [blame] | 464 | if (!strncmp("super", maximum_speed, 5)) |
| 465 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; |
| 466 | else if (!strncmp("high", maximum_speed, 4)) |
| 467 | dwc->maximum_speed = DWC3_DCFG_HIGHSPEED; |
| 468 | else if (!strncmp("full", maximum_speed, 4)) |
| 469 | dwc->maximum_speed = DWC3_DCFG_FULLSPEED1; |
| 470 | else if (!strncmp("low", maximum_speed, 3)) |
| 471 | dwc->maximum_speed = DWC3_DCFG_LOWSPEED; |
| 472 | else |
| 473 | dwc->maximum_speed = DWC3_DCFG_SUPERSPEED; |
| 474 | |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 475 | if (of_get_property(node, "tx-fifo-resize", NULL)) |
| 476 | dwc->needs_fifo_resize = true; |
| 477 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 478 | pm_runtime_enable(dev); |
| 479 | pm_runtime_get_sync(dev); |
| 480 | pm_runtime_forbid(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 481 | |
| 482 | ret = dwc3_core_init(dwc); |
| 483 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 484 | dev_err(dev, "failed to initialize core\n"); |
| 485 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 486 | } |
| 487 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 488 | mode = DWC3_MODE(dwc->hwparams.hwparams0); |
| 489 | |
| 490 | switch (mode) { |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 491 | case DWC3_MODE_DEVICE: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 492 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 493 | ret = dwc3_gadget_init(dwc); |
| 494 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 495 | dev_err(dev, "failed to initialize gadget\n"); |
| 496 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 497 | } |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 498 | break; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 499 | case DWC3_MODE_HOST: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 500 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 501 | ret = dwc3_host_init(dwc); |
| 502 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 503 | dev_err(dev, "failed to initialize host\n"); |
| 504 | goto err1; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 505 | } |
| 506 | break; |
| 507 | case DWC3_MODE_DRD: |
Sebastian Andrzej Siewior | 3140e8c | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 508 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 509 | ret = dwc3_host_init(dwc); |
| 510 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 511 | dev_err(dev, "failed to initialize host\n"); |
| 512 | goto err1; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | ret = dwc3_gadget_init(dwc); |
| 516 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 517 | dev_err(dev, "failed to initialize gadget\n"); |
| 518 | goto err1; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 519 | } |
| 520 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 521 | default: |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 522 | dev_err(dev, "Unsupported mode of operation %d\n", mode); |
| 523 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 524 | } |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 525 | dwc->mode = mode; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 526 | |
| 527 | ret = dwc3_debugfs_init(dwc); |
| 528 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 529 | dev_err(dev, "failed to initialize debugfs\n"); |
| 530 | goto err2; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 531 | } |
| 532 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 533 | pm_runtime_allow(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 534 | |
| 535 | return 0; |
| 536 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 537 | err2: |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 538 | switch (mode) { |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 539 | case DWC3_MODE_DEVICE: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 540 | dwc3_gadget_exit(dwc); |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 541 | break; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 542 | case DWC3_MODE_HOST: |
| 543 | dwc3_host_exit(dwc); |
| 544 | break; |
| 545 | case DWC3_MODE_DRD: |
| 546 | dwc3_host_exit(dwc); |
| 547 | dwc3_gadget_exit(dwc); |
| 548 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 549 | default: |
| 550 | /* do nothing */ |
| 551 | break; |
| 552 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 553 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame^] | 554 | err1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 555 | dwc3_core_exit(dwc); |
| 556 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | static int __devexit dwc3_remove(struct platform_device *pdev) |
| 561 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 562 | struct dwc3 *dwc = platform_get_drvdata(pdev); |
| 563 | struct resource *res; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 564 | |
| 565 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 566 | |
| 567 | pm_runtime_put(&pdev->dev); |
| 568 | pm_runtime_disable(&pdev->dev); |
| 569 | |
| 570 | dwc3_debugfs_exit(dwc); |
| 571 | |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 572 | switch (dwc->mode) { |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 573 | case DWC3_MODE_DEVICE: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 574 | dwc3_gadget_exit(dwc); |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 575 | break; |
Felipe Balbi | d07e881 | 2011-10-12 14:08:26 +0300 | [diff] [blame] | 576 | case DWC3_MODE_HOST: |
| 577 | dwc3_host_exit(dwc); |
| 578 | break; |
| 579 | case DWC3_MODE_DRD: |
| 580 | dwc3_host_exit(dwc); |
| 581 | dwc3_gadget_exit(dwc); |
| 582 | break; |
Felipe Balbi | 0949e99 | 2011-10-12 10:44:56 +0300 | [diff] [blame] | 583 | default: |
| 584 | /* do nothing */ |
| 585 | break; |
| 586 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 587 | |
| 588 | dwc3_core_exit(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 593 | static struct platform_driver dwc3_driver = { |
| 594 | .probe = dwc3_probe, |
| 595 | .remove = __devexit_p(dwc3_remove), |
| 596 | .driver = { |
| 597 | .name = "dwc3", |
| 598 | }, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 599 | }; |
| 600 | |
Sebastian Andrzej Siewior | 7ae4fc4 | 2011-10-19 19:39:50 +0200 | [diff] [blame] | 601 | MODULE_ALIAS("platform:dwc3"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 602 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
| 603 | MODULE_LICENSE("Dual BSD/GPL"); |
| 604 | MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); |
| 605 | |
| 606 | static int __devinit dwc3_init(void) |
| 607 | { |
| 608 | return platform_driver_register(&dwc3_driver); |
| 609 | } |
| 610 | module_init(dwc3_init); |
| 611 | |
| 612 | static void __exit dwc3_exit(void) |
| 613 | { |
| 614 | platform_driver_unregister(&dwc3_driver); |
| 615 | } |
| 616 | module_exit(dwc3_exit); |