Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1 | /** |
| 2 | * core.c - DesignWare USB3 DRD Controller Core file |
| 3 | * |
| 4 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 5 | * |
| 6 | * Authors: Felipe Balbi <balbi@ti.com>, |
| 7 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 8 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 of |
| 11 | * the License as published by the Free Software Foundation. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 12 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 17 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 20 | */ |
| 21 | |
Felipe Balbi | fa0ea13 | 2014-09-19 15:51:11 -0500 | [diff] [blame] | 22 | #include <linux/version.h> |
Felipe Balbi | a72e658 | 2011-09-05 13:37:28 +0300 | [diff] [blame] | 23 | #include <linux/module.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/pm_runtime.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/ioport.h> |
| 31 | #include <linux/io.h> |
| 32 | #include <linux/list.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/dma-mapping.h> |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 35 | #include <linux/of.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 36 | |
| 37 | #include <linux/usb/ch9.h> |
| 38 | #include <linux/usb/gadget.h> |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 39 | #include <linux/usb/of.h> |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 40 | #include <linux/usb/otg.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 41 | |
Felipe Balbi | 6462cbd | 2013-06-30 14:19:33 +0300 | [diff] [blame] | 42 | #include "platform_data.h" |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 43 | #include "core.h" |
| 44 | #include "gadget.h" |
| 45 | #include "io.h" |
| 46 | |
| 47 | #include "debug.h" |
| 48 | |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 49 | /* -------------------------------------------------------------------------- */ |
| 50 | |
Sebastian Andrzej Siewior | 3140e8cb | 2011-10-31 22:25:40 +0100 | [diff] [blame] | 51 | void dwc3_set_mode(struct dwc3 *dwc, u32 mode) |
| 52 | { |
| 53 | u32 reg; |
| 54 | |
| 55 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 56 | reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); |
| 57 | reg |= DWC3_GCTL_PRTCAPDIR(mode); |
| 58 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 59 | } |
Felipe Balbi | 8300dd2 | 2011-10-18 13:54:01 +0300 | [diff] [blame] | 60 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 61 | /** |
| 62 | * dwc3_core_soft_reset - Issues core soft reset and PHY reset |
| 63 | * @dwc: pointer to our context structure |
| 64 | */ |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 65 | static int dwc3_core_soft_reset(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 66 | { |
| 67 | u32 reg; |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 68 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 69 | |
| 70 | /* Before Resetting PHY, put Core in Reset */ |
| 71 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 72 | reg |= DWC3_GCTL_CORESOFTRESET; |
| 73 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 74 | |
| 75 | /* Assert USB3 PHY reset */ |
| 76 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 77 | reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST; |
| 78 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 79 | |
| 80 | /* Assert USB2 PHY reset */ |
| 81 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 82 | reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST; |
| 83 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 84 | |
Felipe Balbi | 51e1e7b | 2012-07-19 14:09:48 +0300 | [diff] [blame] | 85 | usb_phy_init(dwc->usb2_phy); |
| 86 | usb_phy_init(dwc->usb3_phy); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 87 | ret = phy_init(dwc->usb2_generic_phy); |
| 88 | if (ret < 0) |
| 89 | return ret; |
| 90 | |
| 91 | ret = phy_init(dwc->usb3_generic_phy); |
| 92 | if (ret < 0) { |
| 93 | phy_exit(dwc->usb2_generic_phy); |
| 94 | return ret; |
| 95 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 96 | mdelay(100); |
| 97 | |
| 98 | /* Clear USB3 PHY reset */ |
| 99 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); |
| 100 | reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST; |
| 101 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); |
| 102 | |
| 103 | /* Clear USB2 PHY reset */ |
| 104 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 105 | reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST; |
| 106 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 107 | |
Pratyush Anand | 45627ac | 2012-06-21 17:44:28 +0530 | [diff] [blame] | 108 | mdelay(100); |
| 109 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 110 | /* After PHYs are stable we can take Core out of reset state */ |
| 111 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 112 | reg &= ~DWC3_GCTL_CORESOFTRESET; |
| 113 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 114 | |
| 115 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * dwc3_free_one_event_buffer - Frees one event buffer |
| 120 | * @dwc: Pointer to our controller context structure |
| 121 | * @evt: Pointer to event buffer to be freed |
| 122 | */ |
| 123 | static void dwc3_free_one_event_buffer(struct dwc3 *dwc, |
| 124 | struct dwc3_event_buffer *evt) |
| 125 | { |
| 126 | dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 130 | * dwc3_alloc_one_event_buffer - Allocates one event buffer structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 131 | * @dwc: Pointer to our controller context structure |
| 132 | * @length: size of the event buffer |
| 133 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 134 | * Returns a pointer to the allocated event buffer structure on success |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 135 | * otherwise ERR_PTR(errno). |
| 136 | */ |
Felipe Balbi | 67d0b50 | 2013-02-22 16:31:07 +0200 | [diff] [blame] | 137 | static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc, |
| 138 | unsigned length) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 139 | { |
| 140 | struct dwc3_event_buffer *evt; |
| 141 | |
Felipe Balbi | 380f0d2 | 2012-10-11 13:48:36 +0300 | [diff] [blame] | 142 | evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 143 | if (!evt) |
| 144 | return ERR_PTR(-ENOMEM); |
| 145 | |
| 146 | evt->dwc = dwc; |
| 147 | evt->length = length; |
| 148 | evt->buf = dma_alloc_coherent(dwc->dev, length, |
| 149 | &evt->dma, GFP_KERNEL); |
Felipe Balbi | e32672f | 2012-11-08 15:26:41 +0200 | [diff] [blame] | 150 | if (!evt->buf) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 151 | return ERR_PTR(-ENOMEM); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 152 | |
| 153 | return evt; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * dwc3_free_event_buffers - frees all allocated event buffers |
| 158 | * @dwc: Pointer to our controller context structure |
| 159 | */ |
| 160 | static void dwc3_free_event_buffers(struct dwc3 *dwc) |
| 161 | { |
| 162 | struct dwc3_event_buffer *evt; |
| 163 | int i; |
| 164 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 165 | for (i = 0; i < dwc->num_event_buffers; i++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 166 | evt = dwc->ev_buffs[i]; |
Anton Tikhomirov | 64b6c8a | 2012-03-06 17:05:15 +0900 | [diff] [blame] | 167 | if (evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 168 | dwc3_free_one_event_buffer(dwc, evt); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 174 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 175 | * @length: size of event buffer |
| 176 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 177 | * Returns 0 on success otherwise negative errno. In the error case, dwc |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 178 | * may contain some buffers allocated but not all which were requested. |
| 179 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 180 | static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 181 | { |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 182 | int num; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 183 | int i; |
| 184 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 185 | num = DWC3_NUM_INT(dwc->hwparams.hwparams1); |
| 186 | dwc->num_event_buffers = num; |
| 187 | |
Felipe Balbi | 380f0d2 | 2012-10-11 13:48:36 +0300 | [diff] [blame] | 188 | dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num, |
| 189 | GFP_KERNEL); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 190 | if (!dwc->ev_buffs) |
Felipe Balbi | 457d3f2 | 2011-10-24 12:03:13 +0300 | [diff] [blame] | 191 | return -ENOMEM; |
Felipe Balbi | 457d3f2 | 2011-10-24 12:03:13 +0300 | [diff] [blame] | 192 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 193 | for (i = 0; i < num; i++) { |
| 194 | struct dwc3_event_buffer *evt; |
| 195 | |
| 196 | evt = dwc3_alloc_one_event_buffer(dwc, length); |
| 197 | if (IS_ERR(evt)) { |
| 198 | dev_err(dwc->dev, "can't allocate event buffer\n"); |
| 199 | return PTR_ERR(evt); |
| 200 | } |
| 201 | dwc->ev_buffs[i] = evt; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * dwc3_event_buffers_setup - setup our allocated event buffers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 209 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 210 | * |
| 211 | * Returns 0 on success otherwise negative errno. |
| 212 | */ |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 213 | static int dwc3_event_buffers_setup(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 214 | { |
| 215 | struct dwc3_event_buffer *evt; |
| 216 | int n; |
| 217 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 218 | for (n = 0; n < dwc->num_event_buffers; n++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 219 | evt = dwc->ev_buffs[n]; |
| 220 | dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n", |
| 221 | evt->buf, (unsigned long long) evt->dma, |
| 222 | evt->length); |
| 223 | |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 224 | evt->lpos = 0; |
| 225 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 226 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), |
| 227 | lower_32_bits(evt->dma)); |
| 228 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), |
| 229 | upper_32_bits(evt->dma)); |
| 230 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), |
Felipe Balbi | 68d6a01 | 2013-06-12 21:09:26 +0300 | [diff] [blame] | 231 | DWC3_GEVNTSIZ_SIZE(evt->length)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 232 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static void dwc3_event_buffers_cleanup(struct dwc3 *dwc) |
| 239 | { |
| 240 | struct dwc3_event_buffer *evt; |
| 241 | int n; |
| 242 | |
Felipe Balbi | 9f622b2 | 2011-10-12 10:31:04 +0300 | [diff] [blame] | 243 | for (n = 0; n < dwc->num_event_buffers; n++) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 244 | evt = dwc->ev_buffs[n]; |
Paul Zimmerman | 7acd85e | 2012-04-27 14:28:02 +0300 | [diff] [blame] | 245 | |
| 246 | evt->lpos = 0; |
| 247 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 248 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0); |
| 249 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0); |
Felipe Balbi | 68d6a01 | 2013-06-12 21:09:26 +0300 | [diff] [blame] | 250 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK |
| 251 | | DWC3_GEVNTSIZ_SIZE(0)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 252 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0); |
| 253 | } |
| 254 | } |
| 255 | |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 256 | static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc) |
| 257 | { |
| 258 | if (!dwc->has_hibernation) |
| 259 | return 0; |
| 260 | |
| 261 | if (!dwc->nr_scratch) |
| 262 | return 0; |
| 263 | |
| 264 | dwc->scratchbuf = kmalloc_array(dwc->nr_scratch, |
| 265 | DWC3_SCRATCHBUF_SIZE, GFP_KERNEL); |
| 266 | if (!dwc->scratchbuf) |
| 267 | return -ENOMEM; |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int dwc3_setup_scratch_buffers(struct dwc3 *dwc) |
| 273 | { |
| 274 | dma_addr_t scratch_addr; |
| 275 | u32 param; |
| 276 | int ret; |
| 277 | |
| 278 | if (!dwc->has_hibernation) |
| 279 | return 0; |
| 280 | |
| 281 | if (!dwc->nr_scratch) |
| 282 | return 0; |
| 283 | |
| 284 | /* should never fall here */ |
| 285 | if (!WARN_ON(dwc->scratchbuf)) |
| 286 | return 0; |
| 287 | |
| 288 | scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf, |
| 289 | dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE, |
| 290 | DMA_BIDIRECTIONAL); |
| 291 | if (dma_mapping_error(dwc->dev, scratch_addr)) { |
| 292 | dev_err(dwc->dev, "failed to map scratch buffer\n"); |
| 293 | ret = -EFAULT; |
| 294 | goto err0; |
| 295 | } |
| 296 | |
| 297 | dwc->scratch_addr = scratch_addr; |
| 298 | |
| 299 | param = lower_32_bits(scratch_addr); |
| 300 | |
| 301 | ret = dwc3_send_gadget_generic_command(dwc, |
| 302 | DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param); |
| 303 | if (ret < 0) |
| 304 | goto err1; |
| 305 | |
| 306 | param = upper_32_bits(scratch_addr); |
| 307 | |
| 308 | ret = dwc3_send_gadget_generic_command(dwc, |
| 309 | DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param); |
| 310 | if (ret < 0) |
| 311 | goto err1; |
| 312 | |
| 313 | return 0; |
| 314 | |
| 315 | err1: |
| 316 | dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch * |
| 317 | DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); |
| 318 | |
| 319 | err0: |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | static void dwc3_free_scratch_buffers(struct dwc3 *dwc) |
| 324 | { |
| 325 | if (!dwc->has_hibernation) |
| 326 | return; |
| 327 | |
| 328 | if (!dwc->nr_scratch) |
| 329 | return; |
| 330 | |
| 331 | /* should never fall here */ |
| 332 | if (!WARN_ON(dwc->scratchbuf)) |
| 333 | return; |
| 334 | |
| 335 | dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch * |
| 336 | DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL); |
| 337 | kfree(dwc->scratchbuf); |
| 338 | } |
| 339 | |
Felipe Balbi | 789451f6 | 2011-05-05 15:53:10 +0300 | [diff] [blame] | 340 | static void dwc3_core_num_eps(struct dwc3 *dwc) |
| 341 | { |
| 342 | struct dwc3_hwparams *parms = &dwc->hwparams; |
| 343 | |
| 344 | dwc->num_in_eps = DWC3_NUM_IN_EPS(parms); |
| 345 | dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps; |
| 346 | |
| 347 | dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n", |
| 348 | dwc->num_in_eps, dwc->num_out_eps); |
| 349 | } |
| 350 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 351 | static void dwc3_cache_hwparams(struct dwc3 *dwc) |
Felipe Balbi | 26ceca9 | 2011-09-30 10:58:49 +0300 | [diff] [blame] | 352 | { |
| 353 | struct dwc3_hwparams *parms = &dwc->hwparams; |
| 354 | |
| 355 | parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); |
| 356 | parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); |
| 357 | parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); |
| 358 | parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); |
| 359 | parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); |
| 360 | parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); |
| 361 | parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); |
| 362 | parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); |
| 363 | parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); |
| 364 | } |
| 365 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 366 | /** |
| 367 | * dwc3_core_init - Low-level initialization of DWC3 Core |
| 368 | * @dwc: Pointer to our controller context structure |
| 369 | * |
| 370 | * Returns 0 on success otherwise negative errno. |
| 371 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 372 | static int dwc3_core_init(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 373 | { |
| 374 | unsigned long timeout; |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 375 | u32 hwparams4 = dwc->hwparams.hwparams4; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 376 | u32 reg; |
| 377 | int ret; |
| 378 | |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 379 | reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); |
| 380 | /* This should read as U3 followed by revision number */ |
| 381 | if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) { |
| 382 | dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); |
| 383 | ret = -ENODEV; |
| 384 | goto err0; |
| 385 | } |
Felipe Balbi | 248b122 | 2011-12-14 21:59:30 +0200 | [diff] [blame] | 386 | dwc->revision = reg; |
Sebastian Andrzej Siewior | 7650bd7 | 2011-08-29 13:56:36 +0200 | [diff] [blame] | 387 | |
Felipe Balbi | fa0ea13 | 2014-09-19 15:51:11 -0500 | [diff] [blame] | 388 | /* |
| 389 | * Write Linux Version Code to our GUID register so it's easy to figure |
| 390 | * out which kernel version a bug was found. |
| 391 | */ |
| 392 | dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE); |
| 393 | |
Paul Zimmerman | 0e1e5c4 | 2014-05-23 11:39:24 -0700 | [diff] [blame] | 394 | /* Handle USB2.0-only core configuration */ |
| 395 | if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) == |
| 396 | DWC3_GHWPARAMS3_SSPHY_IFC_DIS) { |
| 397 | if (dwc->maximum_speed == USB_SPEED_SUPER) |
| 398 | dwc->maximum_speed = USB_SPEED_HIGH; |
| 399 | } |
| 400 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 401 | /* issue device SoftReset too */ |
| 402 | timeout = jiffies + msecs_to_jiffies(500); |
| 403 | dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); |
| 404 | do { |
| 405 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 406 | if (!(reg & DWC3_DCTL_CSFTRST)) |
| 407 | break; |
| 408 | |
| 409 | if (time_after(jiffies, timeout)) { |
| 410 | dev_err(dwc->dev, "Reset Timed Out\n"); |
| 411 | ret = -ETIMEDOUT; |
| 412 | goto err0; |
| 413 | } |
| 414 | |
| 415 | cpu_relax(); |
| 416 | } while (true); |
| 417 | |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 418 | ret = dwc3_core_soft_reset(dwc); |
| 419 | if (ret) |
| 420 | goto err0; |
Pratyush Anand | 58a0f23 | 2012-06-21 17:44:29 +0530 | [diff] [blame] | 421 | |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 422 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
Paul Zimmerman | 3e87c42 | 2012-02-24 17:32:13 -0800 | [diff] [blame] | 423 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 424 | reg &= ~DWC3_GCTL_DISSCRAMBLE; |
| 425 | |
Sebastian Andrzej Siewior | 164d773 | 2011-11-24 11:22:05 +0100 | [diff] [blame] | 426 | switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) { |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 427 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
Felipe Balbi | 32a4a13 | 2014-02-25 14:00:13 -0600 | [diff] [blame] | 428 | /** |
| 429 | * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an |
| 430 | * issue which would cause xHCI compliance tests to fail. |
| 431 | * |
| 432 | * Because of that we cannot enable clock gating on such |
| 433 | * configurations. |
| 434 | * |
| 435 | * Refers to: |
| 436 | * |
| 437 | * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based |
| 438 | * SOF/ITP Mode Used |
| 439 | */ |
| 440 | if ((dwc->dr_mode == USB_DR_MODE_HOST || |
| 441 | dwc->dr_mode == USB_DR_MODE_OTG) && |
| 442 | (dwc->revision >= DWC3_REVISION_210A && |
| 443 | dwc->revision <= DWC3_REVISION_250A)) |
| 444 | reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC; |
| 445 | else |
| 446 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 447 | break; |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 448 | case DWC3_GHWPARAMS1_EN_PWROPT_HIB: |
| 449 | /* enable hibernation here */ |
| 450 | dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4); |
| 451 | break; |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 452 | default: |
| 453 | dev_dbg(dwc->dev, "No power optimization available\n"); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * WORKAROUND: DWC3 revisions <1.90a have a bug |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 458 | * where the device can fail to connect at SuperSpeed |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 459 | * and falls back to high-speed mode which causes |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 460 | * the device to enter a Connect/Disconnect loop |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 461 | */ |
| 462 | if (dwc->revision < DWC3_REVISION_190A) |
| 463 | reg |= DWC3_GCTL_U2RSTECN; |
| 464 | |
Felipe Balbi | 789451f6 | 2011-05-05 15:53:10 +0300 | [diff] [blame] | 465 | dwc3_core_num_eps(dwc); |
| 466 | |
Sebastian Andrzej Siewior | 4878a02 | 2011-10-31 22:25:41 +0100 | [diff] [blame] | 467 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 468 | |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 469 | ret = dwc3_alloc_scratch_buffers(dwc); |
| 470 | if (ret) |
| 471 | goto err1; |
| 472 | |
| 473 | ret = dwc3_setup_scratch_buffers(dwc); |
| 474 | if (ret) |
| 475 | goto err2; |
| 476 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 477 | return 0; |
| 478 | |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 479 | err2: |
| 480 | dwc3_free_scratch_buffers(dwc); |
| 481 | |
| 482 | err1: |
| 483 | usb_phy_shutdown(dwc->usb2_phy); |
| 484 | usb_phy_shutdown(dwc->usb3_phy); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 485 | phy_exit(dwc->usb2_generic_phy); |
| 486 | phy_exit(dwc->usb3_generic_phy); |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 487 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 488 | err0: |
| 489 | return ret; |
| 490 | } |
| 491 | |
| 492 | static void dwc3_core_exit(struct dwc3 *dwc) |
| 493 | { |
Felipe Balbi | 0ffcaf3 | 2013-12-19 13:04:28 -0600 | [diff] [blame] | 494 | dwc3_free_scratch_buffers(dwc); |
Vivek Gautam | 01b8daf | 2012-10-13 19:20:18 +0530 | [diff] [blame] | 495 | usb_phy_shutdown(dwc->usb2_phy); |
| 496 | usb_phy_shutdown(dwc->usb3_phy); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 497 | phy_exit(dwc->usb2_generic_phy); |
| 498 | phy_exit(dwc->usb3_generic_phy); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 499 | } |
| 500 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 501 | static int dwc3_core_get_phy(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 502 | { |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 503 | struct device *dev = dwc->dev; |
Felipe Balbi | 941ea36 | 2013-07-31 09:21:25 +0300 | [diff] [blame] | 504 | struct device_node *node = dev->of_node; |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 505 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 506 | |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 507 | if (node) { |
| 508 | dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0); |
| 509 | dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1); |
Felipe Balbi | bb67490 | 2013-08-14 13:21:23 -0500 | [diff] [blame] | 510 | } else { |
| 511 | dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); |
| 512 | dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3); |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 513 | } |
| 514 | |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 515 | if (IS_ERR(dwc->usb2_phy)) { |
| 516 | ret = PTR_ERR(dwc->usb2_phy); |
Kishon Vijay Abraham I | 122f06e | 2014-03-03 17:08:10 +0530 | [diff] [blame] | 517 | if (ret == -ENXIO || ret == -ENODEV) { |
| 518 | dwc->usb2_phy = NULL; |
| 519 | } else if (ret == -EPROBE_DEFER) { |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 520 | return ret; |
Kishon Vijay Abraham I | 122f06e | 2014-03-03 17:08:10 +0530 | [diff] [blame] | 521 | } else { |
| 522 | dev_err(dev, "no usb2 phy configured\n"); |
| 523 | return ret; |
| 524 | } |
Felipe Balbi | 51e1e7b | 2012-07-19 14:09:48 +0300 | [diff] [blame] | 525 | } |
| 526 | |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 527 | if (IS_ERR(dwc->usb3_phy)) { |
Ruchika Kharwar | 315955d7 | 2013-07-04 00:59:34 -0500 | [diff] [blame] | 528 | ret = PTR_ERR(dwc->usb3_phy); |
Kishon Vijay Abraham I | 122f06e | 2014-03-03 17:08:10 +0530 | [diff] [blame] | 529 | if (ret == -ENXIO || ret == -ENODEV) { |
| 530 | dwc->usb3_phy = NULL; |
| 531 | } else if (ret == -EPROBE_DEFER) { |
Felipe Balbi | d105e7f | 2013-03-15 10:52:08 +0200 | [diff] [blame] | 532 | return ret; |
Kishon Vijay Abraham I | 122f06e | 2014-03-03 17:08:10 +0530 | [diff] [blame] | 533 | } else { |
| 534 | dev_err(dev, "no usb3 phy configured\n"); |
| 535 | return ret; |
| 536 | } |
Felipe Balbi | 51e1e7b | 2012-07-19 14:09:48 +0300 | [diff] [blame] | 537 | } |
| 538 | |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 539 | dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy"); |
| 540 | if (IS_ERR(dwc->usb2_generic_phy)) { |
| 541 | ret = PTR_ERR(dwc->usb2_generic_phy); |
| 542 | if (ret == -ENOSYS || ret == -ENODEV) { |
| 543 | dwc->usb2_generic_phy = NULL; |
| 544 | } else if (ret == -EPROBE_DEFER) { |
| 545 | return ret; |
| 546 | } else { |
| 547 | dev_err(dev, "no usb2 phy configured\n"); |
| 548 | return ret; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy"); |
| 553 | if (IS_ERR(dwc->usb3_generic_phy)) { |
| 554 | ret = PTR_ERR(dwc->usb3_generic_phy); |
| 555 | if (ret == -ENOSYS || ret == -ENODEV) { |
| 556 | dwc->usb3_generic_phy = NULL; |
| 557 | } else if (ret == -EPROBE_DEFER) { |
| 558 | return ret; |
| 559 | } else { |
| 560 | dev_err(dev, "no usb3 phy configured\n"); |
| 561 | return ret; |
| 562 | } |
| 563 | } |
| 564 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 568 | static int dwc3_core_init_mode(struct dwc3 *dwc) |
| 569 | { |
| 570 | struct device *dev = dwc->dev; |
| 571 | int ret; |
| 572 | |
| 573 | switch (dwc->dr_mode) { |
| 574 | case USB_DR_MODE_PERIPHERAL: |
| 575 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE); |
| 576 | ret = dwc3_gadget_init(dwc); |
| 577 | if (ret) { |
| 578 | dev_err(dev, "failed to initialize gadget\n"); |
| 579 | return ret; |
| 580 | } |
| 581 | break; |
| 582 | case USB_DR_MODE_HOST: |
| 583 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST); |
| 584 | ret = dwc3_host_init(dwc); |
| 585 | if (ret) { |
| 586 | dev_err(dev, "failed to initialize host\n"); |
| 587 | return ret; |
| 588 | } |
| 589 | break; |
| 590 | case USB_DR_MODE_OTG: |
| 591 | dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG); |
| 592 | ret = dwc3_host_init(dwc); |
| 593 | if (ret) { |
| 594 | dev_err(dev, "failed to initialize host\n"); |
| 595 | return ret; |
| 596 | } |
| 597 | |
| 598 | ret = dwc3_gadget_init(dwc); |
| 599 | if (ret) { |
| 600 | dev_err(dev, "failed to initialize gadget\n"); |
| 601 | return ret; |
| 602 | } |
| 603 | break; |
| 604 | default: |
| 605 | dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode); |
| 606 | return -EINVAL; |
| 607 | } |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static void dwc3_core_exit_mode(struct dwc3 *dwc) |
| 613 | { |
| 614 | switch (dwc->dr_mode) { |
| 615 | case USB_DR_MODE_PERIPHERAL: |
| 616 | dwc3_gadget_exit(dwc); |
| 617 | break; |
| 618 | case USB_DR_MODE_HOST: |
| 619 | dwc3_host_exit(dwc); |
| 620 | break; |
| 621 | case USB_DR_MODE_OTG: |
| 622 | dwc3_host_exit(dwc); |
| 623 | dwc3_gadget_exit(dwc); |
| 624 | break; |
| 625 | default: |
| 626 | /* do nothing */ |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 631 | #define DWC3_ALIGN_MASK (16 - 1) |
| 632 | |
| 633 | static int dwc3_probe(struct platform_device *pdev) |
| 634 | { |
| 635 | struct device *dev = &pdev->dev; |
| 636 | struct dwc3_platform_data *pdata = dev_get_platdata(dev); |
| 637 | struct device_node *node = dev->of_node; |
| 638 | struct resource *res; |
| 639 | struct dwc3 *dwc; |
| 640 | |
Andy Shevchenko | b09e99e | 2014-05-15 15:53:32 +0300 | [diff] [blame] | 641 | int ret; |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 642 | |
| 643 | void __iomem *regs; |
| 644 | void *mem; |
| 645 | |
| 646 | mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 647 | if (!mem) |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 648 | return -ENOMEM; |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 649 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 650 | dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1); |
| 651 | dwc->mem = mem; |
| 652 | dwc->dev = dev; |
| 653 | |
| 654 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 655 | if (!res) { |
| 656 | dev_err(dev, "missing IRQ\n"); |
| 657 | return -ENODEV; |
| 658 | } |
| 659 | dwc->xhci_resources[1].start = res->start; |
| 660 | dwc->xhci_resources[1].end = res->end; |
| 661 | dwc->xhci_resources[1].flags = res->flags; |
| 662 | dwc->xhci_resources[1].name = res->name; |
| 663 | |
| 664 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 665 | if (!res) { |
| 666 | dev_err(dev, "missing memory resource\n"); |
| 667 | return -ENODEV; |
| 668 | } |
| 669 | |
Vivek Gautam | f32a5e2 | 2014-06-04 14:34:52 +0530 | [diff] [blame] | 670 | dwc->xhci_resources[0].start = res->start; |
| 671 | dwc->xhci_resources[0].end = dwc->xhci_resources[0].start + |
| 672 | DWC3_XHCI_REGS_END; |
| 673 | dwc->xhci_resources[0].flags = res->flags; |
| 674 | dwc->xhci_resources[0].name = res->name; |
| 675 | |
| 676 | res->start += DWC3_GLOBALS_REGS_START; |
| 677 | |
| 678 | /* |
| 679 | * Request memory region but exclude xHCI regs, |
| 680 | * since it will be requested by the xhci-plat driver. |
| 681 | */ |
| 682 | regs = devm_ioremap_resource(dev, res); |
| 683 | if (IS_ERR(regs)) |
| 684 | return PTR_ERR(regs); |
| 685 | |
| 686 | dwc->regs = regs; |
| 687 | dwc->regs_size = resource_size(res); |
| 688 | /* |
| 689 | * restore res->start back to its original value so that, |
| 690 | * in case the probe is deferred, we don't end up getting error in |
| 691 | * request the memory region the next time probe is called. |
| 692 | */ |
| 693 | res->start -= DWC3_GLOBALS_REGS_START; |
| 694 | |
Felipe Balbi | 3c9f94a | 2014-04-16 15:08:29 -0500 | [diff] [blame] | 695 | if (node) { |
| 696 | dwc->maximum_speed = of_usb_get_maximum_speed(node); |
| 697 | |
| 698 | dwc->needs_fifo_resize = of_property_read_bool(node, "tx-fifo-resize"); |
| 699 | dwc->dr_mode = of_usb_get_dr_mode(node); |
| 700 | } else if (pdata) { |
| 701 | dwc->maximum_speed = pdata->maximum_speed; |
| 702 | |
| 703 | dwc->needs_fifo_resize = pdata->tx_fifo_resize; |
| 704 | dwc->dr_mode = pdata->dr_mode; |
| 705 | } |
| 706 | |
| 707 | /* default to superspeed if no maximum_speed passed */ |
| 708 | if (dwc->maximum_speed == USB_SPEED_UNKNOWN) |
| 709 | dwc->maximum_speed = USB_SPEED_SUPER; |
| 710 | |
| 711 | ret = dwc3_core_get_phy(dwc); |
| 712 | if (ret) |
| 713 | return ret; |
| 714 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 715 | spin_lock_init(&dwc->lock); |
| 716 | platform_set_drvdata(pdev, dwc); |
| 717 | |
Heikki Krogerus | 19bacdc | 2014-09-24 11:00:38 +0300 | [diff] [blame^] | 718 | if (!dev->dma_mask) { |
| 719 | dev->dma_mask = dev->parent->dma_mask; |
| 720 | dev->dma_parms = dev->parent->dma_parms; |
| 721 | dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask); |
| 722 | } |
Kishon Vijay Abraham I | ddff14f | 2013-03-07 18:51:43 +0530 | [diff] [blame] | 723 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 724 | pm_runtime_enable(dev); |
| 725 | pm_runtime_get_sync(dev); |
| 726 | pm_runtime_forbid(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 727 | |
Kishon Vijay Abraham I | 4fd2448 | 2012-11-16 12:07:54 +0530 | [diff] [blame] | 728 | dwc3_cache_hwparams(dwc); |
| 729 | |
Felipe Balbi | 3921426 | 2012-10-11 13:54:36 +0300 | [diff] [blame] | 730 | ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); |
| 731 | if (ret) { |
| 732 | dev_err(dwc->dev, "failed to allocate event buffers\n"); |
| 733 | ret = -ENOMEM; |
| 734 | goto err0; |
| 735 | } |
| 736 | |
Felipe Balbi | 32a4a13 | 2014-02-25 14:00:13 -0600 | [diff] [blame] | 737 | if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) |
| 738 | dwc->dr_mode = USB_DR_MODE_HOST; |
| 739 | else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) |
| 740 | dwc->dr_mode = USB_DR_MODE_PERIPHERAL; |
| 741 | |
| 742 | if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) |
| 743 | dwc->dr_mode = USB_DR_MODE_OTG; |
| 744 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 745 | ret = dwc3_core_init(dwc); |
| 746 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 747 | dev_err(dev, "failed to initialize core\n"); |
Felipe Balbi | 3921426 | 2012-10-11 13:54:36 +0300 | [diff] [blame] | 748 | goto err0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 749 | } |
| 750 | |
Kishon Vijay Abraham I | 3088f10 | 2013-11-25 15:31:21 +0530 | [diff] [blame] | 751 | usb_phy_set_suspend(dwc->usb2_phy, 0); |
| 752 | usb_phy_set_suspend(dwc->usb3_phy, 0); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 753 | ret = phy_power_on(dwc->usb2_generic_phy); |
| 754 | if (ret < 0) |
| 755 | goto err1; |
| 756 | |
| 757 | ret = phy_power_on(dwc->usb3_generic_phy); |
| 758 | if (ret < 0) |
| 759 | goto err_usb2phy_power; |
Kishon Vijay Abraham I | 3088f10 | 2013-11-25 15:31:21 +0530 | [diff] [blame] | 760 | |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 761 | ret = dwc3_event_buffers_setup(dwc); |
| 762 | if (ret) { |
| 763 | dev_err(dwc->dev, "failed to setup event buffers\n"); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 764 | goto err_usb3phy_power; |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 767 | ret = dwc3_core_init_mode(dwc); |
| 768 | if (ret) |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 769 | goto err2; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 770 | |
| 771 | ret = dwc3_debugfs_init(dwc); |
| 772 | if (ret) { |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 773 | dev_err(dev, "failed to initialize debugfs\n"); |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 774 | goto err3; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 775 | } |
| 776 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 777 | pm_runtime_allow(dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 778 | |
| 779 | return 0; |
| 780 | |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 781 | err3: |
Felipe Balbi | 5f94adf | 2014-04-16 15:13:45 -0500 | [diff] [blame] | 782 | dwc3_core_exit_mode(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 783 | |
Felipe Balbi | f122d33 | 2013-02-08 15:15:11 +0200 | [diff] [blame] | 784 | err2: |
| 785 | dwc3_event_buffers_cleanup(dwc); |
| 786 | |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 787 | err_usb3phy_power: |
| 788 | phy_power_off(dwc->usb3_generic_phy); |
| 789 | |
| 790 | err_usb2phy_power: |
| 791 | phy_power_off(dwc->usb2_generic_phy); |
| 792 | |
Chanho Park | 802ca85 | 2012-02-15 18:27:55 +0900 | [diff] [blame] | 793 | err1: |
Kishon Vijay Abraham I | 501fae5 | 2013-11-25 15:31:22 +0530 | [diff] [blame] | 794 | usb_phy_set_suspend(dwc->usb2_phy, 1); |
| 795 | usb_phy_set_suspend(dwc->usb3_phy, 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 796 | dwc3_core_exit(dwc); |
| 797 | |
Felipe Balbi | 3921426 | 2012-10-11 13:54:36 +0300 | [diff] [blame] | 798 | err0: |
| 799 | dwc3_free_event_buffers(dwc); |
| 800 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 801 | return ret; |
| 802 | } |
| 803 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 804 | static int dwc3_remove(struct platform_device *pdev) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 805 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 806 | struct dwc3 *dwc = platform_get_drvdata(pdev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 807 | |
Felipe Balbi | dc99f16 | 2014-09-03 16:13:37 -0500 | [diff] [blame] | 808 | dwc3_debugfs_exit(dwc); |
| 809 | dwc3_core_exit_mode(dwc); |
| 810 | dwc3_event_buffers_cleanup(dwc); |
| 811 | dwc3_free_event_buffers(dwc); |
| 812 | |
Kishon Vijay Abraham I | 8ba007a | 2013-01-25 08:30:54 +0530 | [diff] [blame] | 813 | usb_phy_set_suspend(dwc->usb2_phy, 1); |
| 814 | usb_phy_set_suspend(dwc->usb3_phy, 1); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 815 | phy_power_off(dwc->usb2_generic_phy); |
| 816 | phy_power_off(dwc->usb3_generic_phy); |
Kishon Vijay Abraham I | 8ba007a | 2013-01-25 08:30:54 +0530 | [diff] [blame] | 817 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 818 | dwc3_core_exit(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 819 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 820 | pm_runtime_put_sync(&pdev->dev); |
| 821 | pm_runtime_disable(&pdev->dev); |
| 822 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 823 | return 0; |
| 824 | } |
| 825 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 826 | #ifdef CONFIG_PM_SLEEP |
| 827 | static int dwc3_prepare(struct device *dev) |
| 828 | { |
| 829 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 830 | unsigned long flags; |
| 831 | |
| 832 | spin_lock_irqsave(&dwc->lock, flags); |
| 833 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 834 | switch (dwc->dr_mode) { |
| 835 | case USB_DR_MODE_PERIPHERAL: |
| 836 | case USB_DR_MODE_OTG: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 837 | dwc3_gadget_prepare(dwc); |
| 838 | /* FALLTHROUGH */ |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 839 | case USB_DR_MODE_HOST: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 840 | default: |
| 841 | dwc3_event_buffers_cleanup(dwc); |
| 842 | break; |
| 843 | } |
| 844 | |
| 845 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | static void dwc3_complete(struct device *dev) |
| 851 | { |
| 852 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 853 | unsigned long flags; |
| 854 | |
| 855 | spin_lock_irqsave(&dwc->lock, flags); |
| 856 | |
Roger Quadros | f45e5f0 | 2014-03-26 11:43:09 +0200 | [diff] [blame] | 857 | dwc3_event_buffers_setup(dwc); |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 858 | switch (dwc->dr_mode) { |
| 859 | case USB_DR_MODE_PERIPHERAL: |
| 860 | case USB_DR_MODE_OTG: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 861 | dwc3_gadget_complete(dwc); |
| 862 | /* FALLTHROUGH */ |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 863 | case USB_DR_MODE_HOST: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 864 | default: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | |
| 868 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 869 | } |
| 870 | |
| 871 | static int dwc3_suspend(struct device *dev) |
| 872 | { |
| 873 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 874 | unsigned long flags; |
| 875 | |
| 876 | spin_lock_irqsave(&dwc->lock, flags); |
| 877 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 878 | switch (dwc->dr_mode) { |
| 879 | case USB_DR_MODE_PERIPHERAL: |
| 880 | case USB_DR_MODE_OTG: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 881 | dwc3_gadget_suspend(dwc); |
| 882 | /* FALLTHROUGH */ |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 883 | case USB_DR_MODE_HOST: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 884 | default: |
| 885 | /* do nothing */ |
| 886 | break; |
| 887 | } |
| 888 | |
| 889 | dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 890 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 891 | |
| 892 | usb_phy_shutdown(dwc->usb3_phy); |
| 893 | usb_phy_shutdown(dwc->usb2_phy); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 894 | phy_exit(dwc->usb2_generic_phy); |
| 895 | phy_exit(dwc->usb3_generic_phy); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 896 | |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | static int dwc3_resume(struct device *dev) |
| 901 | { |
| 902 | struct dwc3 *dwc = dev_get_drvdata(dev); |
| 903 | unsigned long flags; |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 904 | int ret; |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 905 | |
| 906 | usb_phy_init(dwc->usb3_phy); |
| 907 | usb_phy_init(dwc->usb2_phy); |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 908 | ret = phy_init(dwc->usb2_generic_phy); |
| 909 | if (ret < 0) |
| 910 | return ret; |
| 911 | |
| 912 | ret = phy_init(dwc->usb3_generic_phy); |
| 913 | if (ret < 0) |
| 914 | goto err_usb2phy_init; |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 915 | |
| 916 | spin_lock_irqsave(&dwc->lock, flags); |
| 917 | |
| 918 | dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl); |
| 919 | |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 920 | switch (dwc->dr_mode) { |
| 921 | case USB_DR_MODE_PERIPHERAL: |
| 922 | case USB_DR_MODE_OTG: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 923 | dwc3_gadget_resume(dwc); |
| 924 | /* FALLTHROUGH */ |
Ruchika Kharwar | a45c82b8 | 2013-07-06 07:52:49 -0500 | [diff] [blame] | 925 | case USB_DR_MODE_HOST: |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 926 | default: |
| 927 | /* do nothing */ |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 932 | |
| 933 | pm_runtime_disable(dev); |
| 934 | pm_runtime_set_active(dev); |
| 935 | pm_runtime_enable(dev); |
| 936 | |
| 937 | return 0; |
Kishon Vijay Abraham I | 5730348 | 2014-03-03 17:08:11 +0530 | [diff] [blame] | 938 | |
| 939 | err_usb2phy_init: |
| 940 | phy_exit(dwc->usb2_generic_phy); |
| 941 | |
| 942 | return ret; |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | static const struct dev_pm_ops dwc3_dev_pm_ops = { |
| 946 | .prepare = dwc3_prepare, |
| 947 | .complete = dwc3_complete, |
| 948 | |
| 949 | SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume) |
| 950 | }; |
| 951 | |
| 952 | #define DWC3_PM_OPS &(dwc3_dev_pm_ops) |
| 953 | #else |
| 954 | #define DWC3_PM_OPS NULL |
| 955 | #endif |
| 956 | |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 957 | #ifdef CONFIG_OF |
| 958 | static const struct of_device_id of_dwc3_match[] = { |
| 959 | { |
Felipe Balbi | 22a5aa1 | 2013-07-02 21:20:24 +0300 | [diff] [blame] | 960 | .compatible = "snps,dwc3" |
| 961 | }, |
| 962 | { |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 963 | .compatible = "synopsys,dwc3" |
| 964 | }, |
| 965 | { }, |
| 966 | }; |
| 967 | MODULE_DEVICE_TABLE(of, of_dwc3_match); |
| 968 | #endif |
| 969 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 970 | static struct platform_driver dwc3_driver = { |
| 971 | .probe = dwc3_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 972 | .remove = dwc3_remove, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 973 | .driver = { |
| 974 | .name = "dwc3", |
Kishon Vijay Abraham I | 5088b6f | 2013-01-25 16:36:53 +0530 | [diff] [blame] | 975 | .of_match_table = of_match_ptr(of_dwc3_match), |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 976 | .pm = DWC3_PM_OPS, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 977 | }, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 978 | }; |
| 979 | |
Tobias Klauser | b1116dc | 2012-02-28 12:57:20 +0100 | [diff] [blame] | 980 | module_platform_driver(dwc3_driver); |
| 981 | |
Sebastian Andrzej Siewior | 7ae4fc4 | 2011-10-19 19:39:50 +0200 | [diff] [blame] | 982 | MODULE_ALIAS("platform:dwc3"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 983 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 984 | MODULE_LICENSE("GPL v2"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 985 | MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); |