blob: 6270581fc7a9141460cdbb3cde27a4d04a539725 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * core.c - DesignWare USB3 DRD Controller Core file
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * 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 Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * 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 Balbi72246da2011-08-19 18:10:58 +030017 *
Felipe Balbi5945f782013-06-30 14:15:11 +030018 * 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 Balbi72246da2011-08-19 18:10:58 +030020 */
21
Felipe Balbifa0ea132014-09-19 15:51:11 -050022#include <linux/version.h>
Felipe Balbia72e6582011-09-05 13:37:28 +030023#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030024#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 Balbi457e84b2012-01-18 18:04:09 +020035#include <linux/of.h>
Heikki Krogerus404905a2014-09-25 10:57:02 +030036#include <linux/acpi.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030037
38#include <linux/usb/ch9.h>
39#include <linux/usb/gadget.h>
Felipe Balbif7e846f2013-06-30 14:29:51 +030040#include <linux/usb/of.h>
Ruchika Kharwara45c82b82013-07-06 07:52:49 -050041#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030042
Felipe Balbi6462cbd2013-06-30 14:19:33 +030043#include "platform_data.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030044#include "core.h"
45#include "gadget.h"
46#include "io.h"
47
48#include "debug.h"
49
Felipe Balbi8300dd22011-10-18 13:54:01 +030050/* -------------------------------------------------------------------------- */
51
Sebastian Andrzej Siewior3140e8c2011-10-31 22:25:40 +010052void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
53{
54 u32 reg;
55
56 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
57 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
58 reg |= DWC3_GCTL_PRTCAPDIR(mode);
59 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
60}
Felipe Balbi8300dd22011-10-18 13:54:01 +030061
Felipe Balbi72246da2011-08-19 18:10:58 +030062/**
63 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
64 * @dwc: pointer to our context structure
65 */
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +053066static int dwc3_core_soft_reset(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +030067{
68 u32 reg;
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +053069 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +030070
71 /* Before Resetting PHY, put Core in Reset */
72 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
73 reg |= DWC3_GCTL_CORESOFTRESET;
74 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
75
76 /* Assert USB3 PHY reset */
77 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
78 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
79 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
80
81 /* Assert USB2 PHY reset */
82 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
83 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
84 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
85
Felipe Balbi51e1e7b2012-07-19 14:09:48 +030086 usb_phy_init(dwc->usb2_phy);
87 usb_phy_init(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +053088 ret = phy_init(dwc->usb2_generic_phy);
89 if (ret < 0)
90 return ret;
91
92 ret = phy_init(dwc->usb3_generic_phy);
93 if (ret < 0) {
94 phy_exit(dwc->usb2_generic_phy);
95 return ret;
96 }
Felipe Balbi72246da2011-08-19 18:10:58 +030097 mdelay(100);
98
99 /* Clear USB3 PHY reset */
100 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
101 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
102 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
103
104 /* Clear USB2 PHY reset */
105 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
106 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
107 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
108
Pratyush Anand45627ac2012-06-21 17:44:28 +0530109 mdelay(100);
110
Felipe Balbi72246da2011-08-19 18:10:58 +0300111 /* After PHYs are stable we can take Core out of reset state */
112 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
113 reg &= ~DWC3_GCTL_CORESOFTRESET;
114 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530115
116 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300117}
118
119/**
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300120 * dwc3_soft_reset - Issue soft reset
121 * @dwc: Pointer to our controller context structure
122 */
123static int dwc3_soft_reset(struct dwc3 *dwc)
124{
125 unsigned long timeout;
126 u32 reg;
127
128 timeout = jiffies + msecs_to_jiffies(500);
129 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
130 do {
131 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
132 if (!(reg & DWC3_DCTL_CSFTRST))
133 break;
134
135 if (time_after(jiffies, timeout)) {
136 dev_err(dwc->dev, "Reset Timed Out\n");
137 return -ETIMEDOUT;
138 }
139
140 cpu_relax();
141 } while (true);
142
143 return 0;
144}
145
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530146/*
147 * dwc3_frame_length_adjustment - Adjusts frame length if required
148 * @dwc3: Pointer to our controller context structure
149 * @fladj: Value of GFLADJ_30MHZ to adjust frame length
150 */
151static void dwc3_frame_length_adjustment(struct dwc3 *dwc, u32 fladj)
152{
153 u32 reg;
154 u32 dft;
155
156 if (dwc->revision < DWC3_REVISION_250A)
157 return;
158
159 if (fladj == 0)
160 return;
161
162 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
163 dft = reg & DWC3_GFLADJ_30MHZ_MASK;
164 if (!dev_WARN_ONCE(dwc->dev, dft == fladj,
165 "request value same as default, ignoring\n")) {
166 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
167 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | fladj;
168 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
169 }
170}
171
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300172/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300173 * dwc3_free_one_event_buffer - Frees one event buffer
174 * @dwc: Pointer to our controller context structure
175 * @evt: Pointer to event buffer to be freed
176 */
177static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
178 struct dwc3_event_buffer *evt)
179{
180 dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300181}
182
183/**
Paul Zimmerman1d046792012-02-15 18:56:56 -0800184 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300185 * @dwc: Pointer to our controller context structure
186 * @length: size of the event buffer
187 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800188 * Returns a pointer to the allocated event buffer structure on success
Felipe Balbi72246da2011-08-19 18:10:58 +0300189 * otherwise ERR_PTR(errno).
190 */
Felipe Balbi67d0b502013-02-22 16:31:07 +0200191static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
192 unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300193{
194 struct dwc3_event_buffer *evt;
195
Felipe Balbi380f0d22012-10-11 13:48:36 +0300196 evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +0300197 if (!evt)
198 return ERR_PTR(-ENOMEM);
199
200 evt->dwc = dwc;
201 evt->length = length;
202 evt->buf = dma_alloc_coherent(dwc->dev, length,
203 &evt->dma, GFP_KERNEL);
Felipe Balbie32672f2012-11-08 15:26:41 +0200204 if (!evt->buf)
Felipe Balbi72246da2011-08-19 18:10:58 +0300205 return ERR_PTR(-ENOMEM);
Felipe Balbi72246da2011-08-19 18:10:58 +0300206
207 return evt;
208}
209
210/**
211 * dwc3_free_event_buffers - frees all allocated event buffers
212 * @dwc: Pointer to our controller context structure
213 */
214static void dwc3_free_event_buffers(struct dwc3 *dwc)
215{
216 struct dwc3_event_buffer *evt;
217 int i;
218
Felipe Balbi9f622b22011-10-12 10:31:04 +0300219 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300220 evt = dwc->ev_buffs[i];
Anton Tikhomirov64b6c8a2012-03-06 17:05:15 +0900221 if (evt)
Felipe Balbi72246da2011-08-19 18:10:58 +0300222 dwc3_free_one_event_buffer(dwc, evt);
Felipe Balbi72246da2011-08-19 18:10:58 +0300223 }
224}
225
226/**
227 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
Paul Zimmerman1d046792012-02-15 18:56:56 -0800228 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300229 * @length: size of event buffer
230 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800231 * Returns 0 on success otherwise negative errno. In the error case, dwc
Felipe Balbi72246da2011-08-19 18:10:58 +0300232 * may contain some buffers allocated but not all which were requested.
233 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500234static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
Felipe Balbi72246da2011-08-19 18:10:58 +0300235{
Felipe Balbi9f622b22011-10-12 10:31:04 +0300236 int num;
Felipe Balbi72246da2011-08-19 18:10:58 +0300237 int i;
238
Felipe Balbi9f622b22011-10-12 10:31:04 +0300239 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
240 dwc->num_event_buffers = num;
241
Felipe Balbi380f0d22012-10-11 13:48:36 +0300242 dwc->ev_buffs = devm_kzalloc(dwc->dev, sizeof(*dwc->ev_buffs) * num,
243 GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +0900244 if (!dwc->ev_buffs)
Felipe Balbi457d3f22011-10-24 12:03:13 +0300245 return -ENOMEM;
Felipe Balbi457d3f22011-10-24 12:03:13 +0300246
Felipe Balbi72246da2011-08-19 18:10:58 +0300247 for (i = 0; i < num; i++) {
248 struct dwc3_event_buffer *evt;
249
250 evt = dwc3_alloc_one_event_buffer(dwc, length);
251 if (IS_ERR(evt)) {
252 dev_err(dwc->dev, "can't allocate event buffer\n");
253 return PTR_ERR(evt);
254 }
255 dwc->ev_buffs[i] = evt;
256 }
257
258 return 0;
259}
260
261/**
262 * dwc3_event_buffers_setup - setup our allocated event buffers
Paul Zimmerman1d046792012-02-15 18:56:56 -0800263 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +0300264 *
265 * Returns 0 on success otherwise negative errno.
266 */
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300267static int dwc3_event_buffers_setup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300268{
269 struct dwc3_event_buffer *evt;
270 int n;
271
Felipe Balbi9f622b22011-10-12 10:31:04 +0300272 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300273 evt = dwc->ev_buffs[n];
274 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
275 evt->buf, (unsigned long long) evt->dma,
276 evt->length);
277
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300278 evt->lpos = 0;
279
Felipe Balbi72246da2011-08-19 18:10:58 +0300280 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
281 lower_32_bits(evt->dma));
282 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
283 upper_32_bits(evt->dma));
284 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
Felipe Balbi68d6a012013-06-12 21:09:26 +0300285 DWC3_GEVNTSIZ_SIZE(evt->length));
Felipe Balbi72246da2011-08-19 18:10:58 +0300286 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
287 }
288
289 return 0;
290}
291
292static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
293{
294 struct dwc3_event_buffer *evt;
295 int n;
296
Felipe Balbi9f622b22011-10-12 10:31:04 +0300297 for (n = 0; n < dwc->num_event_buffers; n++) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300298 evt = dwc->ev_buffs[n];
Paul Zimmerman7acd85e2012-04-27 14:28:02 +0300299
300 evt->lpos = 0;
301
Felipe Balbi72246da2011-08-19 18:10:58 +0300302 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
303 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
Felipe Balbi68d6a012013-06-12 21:09:26 +0300304 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
305 | DWC3_GEVNTSIZ_SIZE(0));
Felipe Balbi72246da2011-08-19 18:10:58 +0300306 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
307 }
308}
309
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600310static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
311{
312 if (!dwc->has_hibernation)
313 return 0;
314
315 if (!dwc->nr_scratch)
316 return 0;
317
318 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
319 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
320 if (!dwc->scratchbuf)
321 return -ENOMEM;
322
323 return 0;
324}
325
326static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
327{
328 dma_addr_t scratch_addr;
329 u32 param;
330 int ret;
331
332 if (!dwc->has_hibernation)
333 return 0;
334
335 if (!dwc->nr_scratch)
336 return 0;
337
338 /* should never fall here */
339 if (!WARN_ON(dwc->scratchbuf))
340 return 0;
341
342 scratch_addr = dma_map_single(dwc->dev, dwc->scratchbuf,
343 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
344 DMA_BIDIRECTIONAL);
345 if (dma_mapping_error(dwc->dev, scratch_addr)) {
346 dev_err(dwc->dev, "failed to map scratch buffer\n");
347 ret = -EFAULT;
348 goto err0;
349 }
350
351 dwc->scratch_addr = scratch_addr;
352
353 param = lower_32_bits(scratch_addr);
354
355 ret = dwc3_send_gadget_generic_command(dwc,
356 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
357 if (ret < 0)
358 goto err1;
359
360 param = upper_32_bits(scratch_addr);
361
362 ret = dwc3_send_gadget_generic_command(dwc,
363 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
364 if (ret < 0)
365 goto err1;
366
367 return 0;
368
369err1:
370 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
371 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
372
373err0:
374 return ret;
375}
376
377static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
378{
379 if (!dwc->has_hibernation)
380 return;
381
382 if (!dwc->nr_scratch)
383 return;
384
385 /* should never fall here */
386 if (!WARN_ON(dwc->scratchbuf))
387 return;
388
389 dma_unmap_single(dwc->dev, dwc->scratch_addr, dwc->nr_scratch *
390 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
391 kfree(dwc->scratchbuf);
392}
393
Felipe Balbi789451f62011-05-05 15:53:10 +0300394static void dwc3_core_num_eps(struct dwc3 *dwc)
395{
396 struct dwc3_hwparams *parms = &dwc->hwparams;
397
398 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
399 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
400
Felipe Balbi73815282015-01-27 13:48:14 -0600401 dwc3_trace(trace_dwc3_core, "found %d IN and %d OUT endpoints",
Felipe Balbi789451f62011-05-05 15:53:10 +0300402 dwc->num_in_eps, dwc->num_out_eps);
403}
404
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500405static void dwc3_cache_hwparams(struct dwc3 *dwc)
Felipe Balbi26ceca92011-09-30 10:58:49 +0300406{
407 struct dwc3_hwparams *parms = &dwc->hwparams;
408
409 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
410 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
411 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
412 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
413 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
414 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
415 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
416 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
417 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
418}
419
Felipe Balbi72246da2011-08-19 18:10:58 +0300420/**
Huang Ruib5a65c42014-10-28 19:54:28 +0800421 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
422 * @dwc: Pointer to our controller context structure
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300423 *
424 * Returns 0 on success. The USB PHY interfaces are configured but not
425 * initialized. The PHY interfaces and the PHYs get initialized together with
426 * the core in dwc3_core_init.
Huang Ruib5a65c42014-10-28 19:54:28 +0800427 */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300428static int dwc3_phy_setup(struct dwc3 *dwc)
Huang Ruib5a65c42014-10-28 19:54:28 +0800429{
430 u32 reg;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300431 int ret;
Huang Ruib5a65c42014-10-28 19:54:28 +0800432
433 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
434
Huang Rui2164a472014-10-28 19:54:35 +0800435 /*
436 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
437 * to '0' during coreConsultant configuration. So default value
438 * will be '0' when the core is reset. Application needs to set it
439 * to '1' after the core initialization is completed.
440 */
441 if (dwc->revision > DWC3_REVISION_194A)
442 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
443
Huang Ruib5a65c42014-10-28 19:54:28 +0800444 if (dwc->u2ss_inp3_quirk)
445 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
446
Huang Ruidf31f5b2014-10-28 19:54:29 +0800447 if (dwc->req_p1p2p3_quirk)
448 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
449
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800450 if (dwc->del_p1p2p3_quirk)
451 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
452
Huang Rui41c06ff2014-10-28 19:54:31 +0800453 if (dwc->del_phy_power_chg_quirk)
454 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
455
Huang Ruifb67afc2014-10-28 19:54:32 +0800456 if (dwc->lfps_filter_quirk)
457 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
458
Huang Rui14f4ac52014-10-28 19:54:33 +0800459 if (dwc->rx_detect_poll_quirk)
460 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
461
Huang Rui6b6a0c92014-10-31 11:11:12 +0800462 if (dwc->tx_de_emphasis_quirk)
463 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
464
Felipe Balbicd72f892014-11-06 11:31:00 -0600465 if (dwc->dis_u3_susphy_quirk)
Huang Rui59acfa22014-10-31 11:11:13 +0800466 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
467
Huang Ruib5a65c42014-10-28 19:54:28 +0800468 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
469
Huang Rui2164a472014-10-28 19:54:35 +0800470 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
471
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300472 /* Select the HS PHY interface */
473 switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) {
474 case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI:
Felipe Balbi43cacb02015-07-01 22:03:09 -0500475 if (dwc->hsphy_interface &&
476 !strncmp(dwc->hsphy_interface, "utmi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300477 reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300478 break;
Felipe Balbi43cacb02015-07-01 22:03:09 -0500479 } else if (dwc->hsphy_interface &&
480 !strncmp(dwc->hsphy_interface, "ulpi", 4)) {
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300481 reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI;
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300482 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300483 } else {
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300484 /* Relying on default value. */
485 if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI))
486 break;
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300487 }
488 /* FALLTHROUGH */
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300489 case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI:
490 /* Making sure the interface and PHY are operational */
491 ret = dwc3_soft_reset(dwc);
492 if (ret)
493 return ret;
494
495 udelay(1);
496
497 ret = dwc3_ulpi_init(dwc);
498 if (ret)
499 return ret;
500 /* FALLTHROUGH */
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300501 default:
502 break;
503 }
504
Huang Rui2164a472014-10-28 19:54:35 +0800505 /*
506 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
507 * '0' during coreConsultant configuration. So default value will
508 * be '0' when the core is reset. Application needs to set it to
509 * '1' after the core initialization is completed.
510 */
511 if (dwc->revision > DWC3_REVISION_194A)
512 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
513
Felipe Balbicd72f892014-11-06 11:31:00 -0600514 if (dwc->dis_u2_susphy_quirk)
Huang Rui0effe0a2014-10-31 11:11:14 +0800515 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
516
Huang Rui2164a472014-10-28 19:54:35 +0800517 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300518
519 return 0;
Huang Ruib5a65c42014-10-28 19:54:28 +0800520}
521
522/**
Felipe Balbi72246da2011-08-19 18:10:58 +0300523 * dwc3_core_init - Low-level initialization of DWC3 Core
524 * @dwc: Pointer to our controller context structure
525 *
526 * Returns 0 on success otherwise negative errno.
527 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500528static int dwc3_core_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300529{
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600530 u32 hwparams4 = dwc->hwparams.hwparams4;
Felipe Balbi72246da2011-08-19 18:10:58 +0300531 u32 reg;
532 int ret;
533
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200534 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
535 /* This should read as U3 followed by revision number */
536 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
537 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
538 ret = -ENODEV;
539 goto err0;
540 }
Felipe Balbi248b1222011-12-14 21:59:30 +0200541 dwc->revision = reg;
Sebastian Andrzej Siewior7650bd72011-08-29 13:56:36 +0200542
Felipe Balbifa0ea132014-09-19 15:51:11 -0500543 /*
544 * Write Linux Version Code to our GUID register so it's easy to figure
545 * out which kernel version a bug was found.
546 */
547 dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE);
548
Paul Zimmerman0e1e5c42014-05-23 11:39:24 -0700549 /* Handle USB2.0-only core configuration */
550 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
551 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
552 if (dwc->maximum_speed == USB_SPEED_SUPER)
553 dwc->maximum_speed = USB_SPEED_HIGH;
554 }
555
Felipe Balbi72246da2011-08-19 18:10:58 +0300556 /* issue device SoftReset too */
Heikki Krogerusc5cc74e2015-05-13 15:26:47 +0300557 ret = dwc3_soft_reset(dwc);
558 if (ret)
559 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300560
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530561 ret = dwc3_core_soft_reset(dwc);
562 if (ret)
563 goto err0;
Pratyush Anand58a0f232012-06-21 17:44:29 +0530564
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100565 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
Paul Zimmerman3e87c422012-02-24 17:32:13 -0800566 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100567
Sebastian Andrzej Siewior164d7732011-11-24 11:22:05 +0100568 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100569 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
Felipe Balbi32a4a132014-02-25 14:00:13 -0600570 /**
571 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
572 * issue which would cause xHCI compliance tests to fail.
573 *
574 * Because of that we cannot enable clock gating on such
575 * configurations.
576 *
577 * Refers to:
578 *
579 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
580 * SOF/ITP Mode Used
581 */
582 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
583 dwc->dr_mode == USB_DR_MODE_OTG) &&
584 (dwc->revision >= DWC3_REVISION_210A &&
585 dwc->revision <= DWC3_REVISION_250A))
586 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
587 else
588 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100589 break;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600590 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
591 /* enable hibernation here */
592 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
Huang Rui2eac3992014-10-28 19:54:22 +0800593
594 /*
595 * REVISIT Enabling this bit so that host-mode hibernation
596 * will work. Device-mode hibernation is not yet implemented.
597 */
598 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600599 break;
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100600 default:
601 dev_dbg(dwc->dev, "No power optimization available\n");
602 }
603
Huang Rui946bd572014-10-28 19:54:23 +0800604 /* check if current dwc3 is on simulation board */
605 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
606 dev_dbg(dwc->dev, "it is on FPGA board\n");
607 dwc->is_fpga = true;
608 }
609
Huang Rui3b812212014-10-28 19:54:25 +0800610 WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga,
611 "disable_scramble cannot be used on non-FPGA builds\n");
612
613 if (dwc->disable_scramble_quirk && dwc->is_fpga)
614 reg |= DWC3_GCTL_DISSCRAMBLE;
615 else
616 reg &= ~DWC3_GCTL_DISSCRAMBLE;
617
Huang Rui9a5b2f32014-10-28 19:54:27 +0800618 if (dwc->u2exit_lfps_quirk)
619 reg |= DWC3_GCTL_U2EXIT_LFPS;
620
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100621 /*
622 * WORKAROUND: DWC3 revisions <1.90a have a bug
Paul Zimmerman1d046792012-02-15 18:56:56 -0800623 * where the device can fail to connect at SuperSpeed
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100624 * and falls back to high-speed mode which causes
Paul Zimmerman1d046792012-02-15 18:56:56 -0800625 * the device to enter a Connect/Disconnect loop
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100626 */
627 if (dwc->revision < DWC3_REVISION_190A)
628 reg |= DWC3_GCTL_U2RSTECN;
629
Felipe Balbi789451f62011-05-05 15:53:10 +0300630 dwc3_core_num_eps(dwc);
631
Sebastian Andrzej Siewior4878a022011-10-31 22:25:41 +0100632 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
633
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600634 ret = dwc3_alloc_scratch_buffers(dwc);
635 if (ret)
636 goto err1;
637
638 ret = dwc3_setup_scratch_buffers(dwc);
639 if (ret)
640 goto err2;
641
Felipe Balbi72246da2011-08-19 18:10:58 +0300642 return 0;
643
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600644err2:
645 dwc3_free_scratch_buffers(dwc);
646
647err1:
648 usb_phy_shutdown(dwc->usb2_phy);
649 usb_phy_shutdown(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530650 phy_exit(dwc->usb2_generic_phy);
651 phy_exit(dwc->usb3_generic_phy);
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600652
Felipe Balbi72246da2011-08-19 18:10:58 +0300653err0:
654 return ret;
655}
656
657static void dwc3_core_exit(struct dwc3 *dwc)
658{
Felipe Balbi0ffcaf32013-12-19 13:04:28 -0600659 dwc3_free_scratch_buffers(dwc);
Vivek Gautam01b8daf2012-10-13 19:20:18 +0530660 usb_phy_shutdown(dwc->usb2_phy);
661 usb_phy_shutdown(dwc->usb3_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530662 phy_exit(dwc->usb2_generic_phy);
663 phy_exit(dwc->usb3_generic_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +0300664}
665
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500666static int dwc3_core_get_phy(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300667{
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500668 struct device *dev = dwc->dev;
Felipe Balbi941ea362013-07-31 09:21:25 +0300669 struct device_node *node = dev->of_node;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500670 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300671
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530672 if (node) {
673 dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
674 dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1);
Felipe Balbibb674902013-08-14 13:21:23 -0500675 } else {
676 dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
677 dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +0530678 }
679
Felipe Balbid105e7f2013-03-15 10:52:08 +0200680 if (IS_ERR(dwc->usb2_phy)) {
681 ret = PTR_ERR(dwc->usb2_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530682 if (ret == -ENXIO || ret == -ENODEV) {
683 dwc->usb2_phy = NULL;
684 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200685 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530686 } else {
687 dev_err(dev, "no usb2 phy configured\n");
688 return ret;
689 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300690 }
691
Felipe Balbid105e7f2013-03-15 10:52:08 +0200692 if (IS_ERR(dwc->usb3_phy)) {
Ruchika Kharwar315955d72013-07-04 00:59:34 -0500693 ret = PTR_ERR(dwc->usb3_phy);
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530694 if (ret == -ENXIO || ret == -ENODEV) {
695 dwc->usb3_phy = NULL;
696 } else if (ret == -EPROBE_DEFER) {
Felipe Balbid105e7f2013-03-15 10:52:08 +0200697 return ret;
Kishon Vijay Abraham I122f06e2014-03-03 17:08:10 +0530698 } else {
699 dev_err(dev, "no usb3 phy configured\n");
700 return ret;
701 }
Felipe Balbi51e1e7b2012-07-19 14:09:48 +0300702 }
703
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +0530704 dwc->usb2_generic_phy = devm_phy_get(dev, "usb2-phy");
705 if (IS_ERR(dwc->usb2_generic_phy)) {
706 ret = PTR_ERR(dwc->usb2_generic_phy);
707 if (ret == -ENOSYS || ret == -ENODEV) {
708 dwc->usb2_generic_phy = NULL;
709 } else if (ret == -EPROBE_DEFER) {
710 return ret;
711 } else {
712 dev_err(dev, "no usb2 phy configured\n");
713 return ret;
714 }
715 }
716
717 dwc->usb3_generic_phy = devm_phy_get(dev, "usb3-phy");
718 if (IS_ERR(dwc->usb3_generic_phy)) {
719 ret = PTR_ERR(dwc->usb3_generic_phy);
720 if (ret == -ENOSYS || ret == -ENODEV) {
721 dwc->usb3_generic_phy = NULL;
722 } else if (ret == -EPROBE_DEFER) {
723 return ret;
724 } else {
725 dev_err(dev, "no usb3 phy configured\n");
726 return ret;
727 }
728 }
729
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500730 return 0;
731}
732
Felipe Balbi5f94adf2014-04-16 15:13:45 -0500733static int dwc3_core_init_mode(struct dwc3 *dwc)
734{
735 struct device *dev = dwc->dev;
736 int ret;
737
738 switch (dwc->dr_mode) {
739 case USB_DR_MODE_PERIPHERAL:
740 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
741 ret = dwc3_gadget_init(dwc);
742 if (ret) {
743 dev_err(dev, "failed to initialize gadget\n");
744 return ret;
745 }
746 break;
747 case USB_DR_MODE_HOST:
748 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
749 ret = dwc3_host_init(dwc);
750 if (ret) {
751 dev_err(dev, "failed to initialize host\n");
752 return ret;
753 }
754 break;
755 case USB_DR_MODE_OTG:
756 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
757 ret = dwc3_host_init(dwc);
758 if (ret) {
759 dev_err(dev, "failed to initialize host\n");
760 return ret;
761 }
762
763 ret = dwc3_gadget_init(dwc);
764 if (ret) {
765 dev_err(dev, "failed to initialize gadget\n");
766 return ret;
767 }
768 break;
769 default:
770 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
771 return -EINVAL;
772 }
773
774 return 0;
775}
776
777static void dwc3_core_exit_mode(struct dwc3 *dwc)
778{
779 switch (dwc->dr_mode) {
780 case USB_DR_MODE_PERIPHERAL:
781 dwc3_gadget_exit(dwc);
782 break;
783 case USB_DR_MODE_HOST:
784 dwc3_host_exit(dwc);
785 break;
786 case USB_DR_MODE_OTG:
787 dwc3_host_exit(dwc);
788 dwc3_gadget_exit(dwc);
789 break;
790 default:
791 /* do nothing */
792 break;
793 }
794}
795
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500796#define DWC3_ALIGN_MASK (16 - 1)
797
798static int dwc3_probe(struct platform_device *pdev)
799{
800 struct device *dev = &pdev->dev;
801 struct dwc3_platform_data *pdata = dev_get_platdata(dev);
802 struct device_node *node = dev->of_node;
803 struct resource *res;
804 struct dwc3 *dwc;
Huang Rui80caf7d2014-10-28 19:54:26 +0800805 u8 lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +0800806 u8 tx_de_emphasis;
Huang Rui460d0982014-10-31 11:11:18 +0800807 u8 hird_threshold;
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530808 u32 fladj = 0;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500809
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300810 int ret;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500811
812 void __iomem *regs;
813 void *mem;
814
815 mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +0900816 if (!mem)
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500817 return -ENOMEM;
Jingoo Han734d5a52014-07-17 12:45:11 +0900818
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500819 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
820 dwc->mem = mem;
821 dwc->dev = dev;
822
823 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
824 if (!res) {
825 dev_err(dev, "missing IRQ\n");
826 return -ENODEV;
827 }
828 dwc->xhci_resources[1].start = res->start;
829 dwc->xhci_resources[1].end = res->end;
830 dwc->xhci_resources[1].flags = res->flags;
831 dwc->xhci_resources[1].name = res->name;
832
833 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
834 if (!res) {
835 dev_err(dev, "missing memory resource\n");
836 return -ENODEV;
837 }
838
Vivek Gautamf32a5e22014-06-04 14:34:52 +0530839 dwc->xhci_resources[0].start = res->start;
840 dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
841 DWC3_XHCI_REGS_END;
842 dwc->xhci_resources[0].flags = res->flags;
843 dwc->xhci_resources[0].name = res->name;
844
845 res->start += DWC3_GLOBALS_REGS_START;
846
847 /*
848 * Request memory region but exclude xHCI regs,
849 * since it will be requested by the xhci-plat driver.
850 */
851 regs = devm_ioremap_resource(dev, res);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -0500852 if (IS_ERR(regs)) {
853 ret = PTR_ERR(regs);
854 goto err0;
855 }
Vivek Gautamf32a5e22014-06-04 14:34:52 +0530856
857 dwc->regs = regs;
858 dwc->regs_size = resource_size(res);
Vivek Gautamf32a5e22014-06-04 14:34:52 +0530859
Huang Rui80caf7d2014-10-28 19:54:26 +0800860 /* default to highest possible threshold */
861 lpm_nyet_threshold = 0xff;
862
Huang Rui6b6a0c92014-10-31 11:11:12 +0800863 /* default to -3.5dB de-emphasis */
864 tx_de_emphasis = 1;
865
Huang Rui460d0982014-10-31 11:11:18 +0800866 /*
867 * default to assert utmi_sleep_n and use maximum allowed HIRD
868 * threshold value of 0b1100
869 */
870 hird_threshold = 12;
871
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500872 if (node) {
873 dwc->maximum_speed = of_usb_get_maximum_speed(node);
Huang Rui80caf7d2014-10-28 19:54:26 +0800874 dwc->has_lpm_erratum = of_property_read_bool(node,
875 "snps,has-lpm-erratum");
876 of_property_read_u8(node, "snps,lpm-nyet-threshold",
877 &lpm_nyet_threshold);
Huang Rui460d0982014-10-31 11:11:18 +0800878 dwc->is_utmi_l1_suspend = of_property_read_bool(node,
879 "snps,is-utmi-l1-suspend");
880 of_property_read_u8(node, "snps,hird-threshold",
881 &hird_threshold);
Robert Baldygaeac68e82015-03-09 15:06:12 +0100882 dwc->usb3_lpm_capable = of_property_read_bool(node,
883 "snps,usb3_lpm_capable");
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500884
Huang Rui80caf7d2014-10-28 19:54:26 +0800885 dwc->needs_fifo_resize = of_property_read_bool(node,
886 "tx-fifo-resize");
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500887 dwc->dr_mode = of_usb_get_dr_mode(node);
Huang Rui3b812212014-10-28 19:54:25 +0800888
889 dwc->disable_scramble_quirk = of_property_read_bool(node,
890 "snps,disable_scramble_quirk");
Huang Rui9a5b2f32014-10-28 19:54:27 +0800891 dwc->u2exit_lfps_quirk = of_property_read_bool(node,
892 "snps,u2exit_lfps_quirk");
Huang Ruib5a65c42014-10-28 19:54:28 +0800893 dwc->u2ss_inp3_quirk = of_property_read_bool(node,
894 "snps,u2ss_inp3_quirk");
Huang Ruidf31f5b2014-10-28 19:54:29 +0800895 dwc->req_p1p2p3_quirk = of_property_read_bool(node,
896 "snps,req_p1p2p3_quirk");
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800897 dwc->del_p1p2p3_quirk = of_property_read_bool(node,
898 "snps,del_p1p2p3_quirk");
Huang Rui41c06ff2014-10-28 19:54:31 +0800899 dwc->del_phy_power_chg_quirk = of_property_read_bool(node,
900 "snps,del_phy_power_chg_quirk");
Huang Ruifb67afc2014-10-28 19:54:32 +0800901 dwc->lfps_filter_quirk = of_property_read_bool(node,
902 "snps,lfps_filter_quirk");
Huang Rui14f4ac52014-10-28 19:54:33 +0800903 dwc->rx_detect_poll_quirk = of_property_read_bool(node,
904 "snps,rx_detect_poll_quirk");
Huang Rui59acfa22014-10-31 11:11:13 +0800905 dwc->dis_u3_susphy_quirk = of_property_read_bool(node,
906 "snps,dis_u3_susphy_quirk");
Huang Rui0effe0a2014-10-31 11:11:14 +0800907 dwc->dis_u2_susphy_quirk = of_property_read_bool(node,
908 "snps,dis_u2_susphy_quirk");
Huang Rui6b6a0c92014-10-31 11:11:12 +0800909
910 dwc->tx_de_emphasis_quirk = of_property_read_bool(node,
911 "snps,tx_de_emphasis_quirk");
912 of_property_read_u8(node, "snps,tx_de_emphasis",
913 &tx_de_emphasis);
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300914 of_property_read_string(node, "snps,hsphy_interface",
915 &dwc->hsphy_interface);
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530916 of_property_read_u32(node,
917 "snps,quirk-frame-length-adjustment",
918 &fladj);
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500919 } else if (pdata) {
920 dwc->maximum_speed = pdata->maximum_speed;
Huang Rui80caf7d2014-10-28 19:54:26 +0800921 dwc->has_lpm_erratum = pdata->has_lpm_erratum;
922 if (pdata->lpm_nyet_threshold)
923 lpm_nyet_threshold = pdata->lpm_nyet_threshold;
Huang Rui460d0982014-10-31 11:11:18 +0800924 dwc->is_utmi_l1_suspend = pdata->is_utmi_l1_suspend;
925 if (pdata->hird_threshold)
926 hird_threshold = pdata->hird_threshold;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500927
928 dwc->needs_fifo_resize = pdata->tx_fifo_resize;
Robert Baldygaeac68e82015-03-09 15:06:12 +0100929 dwc->usb3_lpm_capable = pdata->usb3_lpm_capable;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500930 dwc->dr_mode = pdata->dr_mode;
Huang Rui3b812212014-10-28 19:54:25 +0800931
932 dwc->disable_scramble_quirk = pdata->disable_scramble_quirk;
Huang Rui9a5b2f32014-10-28 19:54:27 +0800933 dwc->u2exit_lfps_quirk = pdata->u2exit_lfps_quirk;
Huang Ruib5a65c42014-10-28 19:54:28 +0800934 dwc->u2ss_inp3_quirk = pdata->u2ss_inp3_quirk;
Huang Ruidf31f5b2014-10-28 19:54:29 +0800935 dwc->req_p1p2p3_quirk = pdata->req_p1p2p3_quirk;
Huang Ruia2a1d0f2014-10-28 19:54:30 +0800936 dwc->del_p1p2p3_quirk = pdata->del_p1p2p3_quirk;
Huang Rui41c06ff2014-10-28 19:54:31 +0800937 dwc->del_phy_power_chg_quirk = pdata->del_phy_power_chg_quirk;
Huang Ruifb67afc2014-10-28 19:54:32 +0800938 dwc->lfps_filter_quirk = pdata->lfps_filter_quirk;
Huang Rui14f4ac52014-10-28 19:54:33 +0800939 dwc->rx_detect_poll_quirk = pdata->rx_detect_poll_quirk;
Huang Rui59acfa22014-10-31 11:11:13 +0800940 dwc->dis_u3_susphy_quirk = pdata->dis_u3_susphy_quirk;
Huang Rui0effe0a2014-10-31 11:11:14 +0800941 dwc->dis_u2_susphy_quirk = pdata->dis_u2_susphy_quirk;
Huang Rui6b6a0c92014-10-31 11:11:12 +0800942
943 dwc->tx_de_emphasis_quirk = pdata->tx_de_emphasis_quirk;
944 if (pdata->tx_de_emphasis)
945 tx_de_emphasis = pdata->tx_de_emphasis;
Heikki Krogerus3e10a2c2015-05-13 15:26:49 +0300946
947 dwc->hsphy_interface = pdata->hsphy_interface;
Nikhil Badoladb2be4e2015-09-04 10:15:58 +0530948 fladj = pdata->fladj_value;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500949 }
950
951 /* default to superspeed if no maximum_speed passed */
952 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
953 dwc->maximum_speed = USB_SPEED_SUPER;
954
Huang Rui80caf7d2014-10-28 19:54:26 +0800955 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
Huang Rui6b6a0c92014-10-31 11:11:12 +0800956 dwc->tx_de_emphasis = tx_de_emphasis;
Huang Rui80caf7d2014-10-28 19:54:26 +0800957
Huang Rui460d0982014-10-31 11:11:18 +0800958 dwc->hird_threshold = hird_threshold
959 | (dwc->is_utmi_l1_suspend << 4);
960
Heikki Krogerus6c89cce02015-05-13 15:26:45 +0300961 platform_set_drvdata(pdev, dwc);
Heikki Krogerus2917e712015-05-13 15:26:46 +0300962 dwc3_cache_hwparams(dwc);
Heikki Krogerus6c89cce02015-05-13 15:26:45 +0300963
Heikki Krogerus88bc9d12015-05-13 15:26:51 +0300964 ret = dwc3_phy_setup(dwc);
965 if (ret)
966 goto err0;
Heikki Krogerus45bb7de2015-05-13 15:26:48 +0300967
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500968 ret = dwc3_core_get_phy(dwc);
969 if (ret)
Felipe Balbi3da1f6e2014-09-02 15:19:43 -0500970 goto err0;
Felipe Balbi3c9f94a2014-04-16 15:08:29 -0500971
Felipe Balbi72246da2011-08-19 18:10:58 +0300972 spin_lock_init(&dwc->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +0300973
Heikki Krogerus19bacdc2014-09-24 11:00:38 +0300974 if (!dev->dma_mask) {
975 dev->dma_mask = dev->parent->dma_mask;
976 dev->dma_parms = dev->parent->dma_parms;
977 dma_set_coherent_mask(dev, dev->parent->coherent_dma_mask);
978 }
Kishon Vijay Abraham Iddff14f2013-03-07 18:51:43 +0530979
Chanho Park802ca852012-02-15 18:27:55 +0900980 pm_runtime_enable(dev);
981 pm_runtime_get_sync(dev);
982 pm_runtime_forbid(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300983
Felipe Balbi39214262012-10-11 13:54:36 +0300984 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
985 if (ret) {
986 dev_err(dwc->dev, "failed to allocate event buffers\n");
987 ret = -ENOMEM;
Felipe Balbi3da1f6e2014-09-02 15:19:43 -0500988 goto err1;
Felipe Balbi39214262012-10-11 13:54:36 +0300989 }
990
Felipe Balbi32a4a132014-02-25 14:00:13 -0600991 if (IS_ENABLED(CONFIG_USB_DWC3_HOST))
992 dwc->dr_mode = USB_DR_MODE_HOST;
993 else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET))
994 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
995
996 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
997 dwc->dr_mode = USB_DR_MODE_OTG;
998
Felipe Balbi72246da2011-08-19 18:10:58 +0300999 ret = dwc3_core_init(dwc);
1000 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +09001001 dev_err(dev, "failed to initialize core\n");
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001002 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001003 }
1004
Nikhil Badoladb2be4e2015-09-04 10:15:58 +05301005 /* Adjust Frame Length */
1006 dwc3_frame_length_adjustment(dwc, fladj);
1007
Kishon Vijay Abraham I3088f102013-11-25 15:31:21 +05301008 usb_phy_set_suspend(dwc->usb2_phy, 0);
1009 usb_phy_set_suspend(dwc->usb3_phy, 0);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301010 ret = phy_power_on(dwc->usb2_generic_phy);
1011 if (ret < 0)
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001012 goto err2;
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301013
1014 ret = phy_power_on(dwc->usb3_generic_phy);
1015 if (ret < 0)
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001016 goto err3;
Kishon Vijay Abraham I3088f102013-11-25 15:31:21 +05301017
Felipe Balbif122d332013-02-08 15:15:11 +02001018 ret = dwc3_event_buffers_setup(dwc);
1019 if (ret) {
1020 dev_err(dwc->dev, "failed to setup event buffers\n");
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001021 goto err4;
Felipe Balbif122d332013-02-08 15:15:11 +02001022 }
1023
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001024 ret = dwc3_core_init_mode(dwc);
1025 if (ret)
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001026 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03001027
1028 ret = dwc3_debugfs_init(dwc);
1029 if (ret) {
Chanho Park802ca852012-02-15 18:27:55 +09001030 dev_err(dev, "failed to initialize debugfs\n");
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001031 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03001032 }
1033
Chanho Park802ca852012-02-15 18:27:55 +09001034 pm_runtime_allow(dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001035
1036 return 0;
1037
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001038err6:
Felipe Balbi5f94adf2014-04-16 15:13:45 -05001039 dwc3_core_exit_mode(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001040
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001041err5:
Felipe Balbif122d332013-02-08 15:15:11 +02001042 dwc3_event_buffers_cleanup(dwc);
1043
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001044err4:
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301045 phy_power_off(dwc->usb3_generic_phy);
1046
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001047err3:
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301048 phy_power_off(dwc->usb2_generic_phy);
1049
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001050err2:
Kishon Vijay Abraham I501fae52013-11-25 15:31:22 +05301051 usb_phy_set_suspend(dwc->usb2_phy, 1);
1052 usb_phy_set_suspend(dwc->usb3_phy, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001053 dwc3_core_exit(dwc);
1054
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001055err1:
Felipe Balbi39214262012-10-11 13:54:36 +03001056 dwc3_free_event_buffers(dwc);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +03001057 dwc3_ulpi_exit(dwc);
Felipe Balbi39214262012-10-11 13:54:36 +03001058
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001059err0:
1060 /*
1061 * restore res->start back to its original value so that, in case the
1062 * probe is deferred, we don't end up getting error in request the
1063 * memory region the next time probe is called.
1064 */
1065 res->start -= DWC3_GLOBALS_REGS_START;
1066
Felipe Balbi72246da2011-08-19 18:10:58 +03001067 return ret;
1068}
1069
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001070static int dwc3_remove(struct platform_device *pdev)
Felipe Balbi72246da2011-08-19 18:10:58 +03001071{
Felipe Balbi72246da2011-08-19 18:10:58 +03001072 struct dwc3 *dwc = platform_get_drvdata(pdev);
Felipe Balbi3da1f6e2014-09-02 15:19:43 -05001073 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1074
1075 /*
1076 * restore res->start back to its original value so that, in case the
1077 * probe is deferred, we don't end up getting error in request the
1078 * memory region the next time probe is called.
1079 */
1080 res->start -= DWC3_GLOBALS_REGS_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001081
Felipe Balbidc99f162014-09-03 16:13:37 -05001082 dwc3_debugfs_exit(dwc);
1083 dwc3_core_exit_mode(dwc);
1084 dwc3_event_buffers_cleanup(dwc);
1085 dwc3_free_event_buffers(dwc);
1086
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +05301087 usb_phy_set_suspend(dwc->usb2_phy, 1);
1088 usb_phy_set_suspend(dwc->usb3_phy, 1);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301089 phy_power_off(dwc->usb2_generic_phy);
1090 phy_power_off(dwc->usb3_generic_phy);
Kishon Vijay Abraham I8ba007a2013-01-25 08:30:54 +05301091
Felipe Balbi72246da2011-08-19 18:10:58 +03001092 dwc3_core_exit(dwc);
Heikki Krogerus88bc9d12015-05-13 15:26:51 +03001093 dwc3_ulpi_exit(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001094
Felipe Balbi7415f172012-04-30 14:56:33 +03001095 pm_runtime_put_sync(&pdev->dev);
1096 pm_runtime_disable(&pdev->dev);
1097
Felipe Balbi72246da2011-08-19 18:10:58 +03001098 return 0;
1099}
1100
Felipe Balbi7415f172012-04-30 14:56:33 +03001101#ifdef CONFIG_PM_SLEEP
Felipe Balbi7415f172012-04-30 14:56:33 +03001102static int dwc3_suspend(struct device *dev)
1103{
1104 struct dwc3 *dwc = dev_get_drvdata(dev);
1105 unsigned long flags;
1106
1107 spin_lock_irqsave(&dwc->lock, flags);
1108
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001109 switch (dwc->dr_mode) {
1110 case USB_DR_MODE_PERIPHERAL:
1111 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +03001112 dwc3_gadget_suspend(dwc);
1113 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001114 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +03001115 default:
Felipe Balbi0b0231a2014-10-07 10:19:23 -05001116 dwc3_event_buffers_cleanup(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03001117 break;
1118 }
1119
1120 dwc->gctl = dwc3_readl(dwc->regs, DWC3_GCTL);
1121 spin_unlock_irqrestore(&dwc->lock, flags);
1122
1123 usb_phy_shutdown(dwc->usb3_phy);
1124 usb_phy_shutdown(dwc->usb2_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301125 phy_exit(dwc->usb2_generic_phy);
1126 phy_exit(dwc->usb3_generic_phy);
Felipe Balbi7415f172012-04-30 14:56:33 +03001127
1128 return 0;
1129}
1130
1131static int dwc3_resume(struct device *dev)
1132{
1133 struct dwc3 *dwc = dev_get_drvdata(dev);
1134 unsigned long flags;
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301135 int ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03001136
1137 usb_phy_init(dwc->usb3_phy);
1138 usb_phy_init(dwc->usb2_phy);
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301139 ret = phy_init(dwc->usb2_generic_phy);
1140 if (ret < 0)
1141 return ret;
1142
1143 ret = phy_init(dwc->usb3_generic_phy);
1144 if (ret < 0)
1145 goto err_usb2phy_init;
Felipe Balbi7415f172012-04-30 14:56:33 +03001146
1147 spin_lock_irqsave(&dwc->lock, flags);
1148
Felipe Balbi0b0231a2014-10-07 10:19:23 -05001149 dwc3_event_buffers_setup(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03001150 dwc3_writel(dwc->regs, DWC3_GCTL, dwc->gctl);
1151
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001152 switch (dwc->dr_mode) {
1153 case USB_DR_MODE_PERIPHERAL:
1154 case USB_DR_MODE_OTG:
Felipe Balbi7415f172012-04-30 14:56:33 +03001155 dwc3_gadget_resume(dwc);
1156 /* FALLTHROUGH */
Ruchika Kharwara45c82b82013-07-06 07:52:49 -05001157 case USB_DR_MODE_HOST:
Felipe Balbi7415f172012-04-30 14:56:33 +03001158 default:
1159 /* do nothing */
1160 break;
1161 }
1162
1163 spin_unlock_irqrestore(&dwc->lock, flags);
1164
1165 pm_runtime_disable(dev);
1166 pm_runtime_set_active(dev);
1167 pm_runtime_enable(dev);
1168
1169 return 0;
Kishon Vijay Abraham I57303482014-03-03 17:08:11 +05301170
1171err_usb2phy_init:
1172 phy_exit(dwc->usb2_generic_phy);
1173
1174 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03001175}
1176
1177static const struct dev_pm_ops dwc3_dev_pm_ops = {
Felipe Balbi7415f172012-04-30 14:56:33 +03001178 SET_SYSTEM_SLEEP_PM_OPS(dwc3_suspend, dwc3_resume)
1179};
1180
1181#define DWC3_PM_OPS &(dwc3_dev_pm_ops)
1182#else
1183#define DWC3_PM_OPS NULL
1184#endif
1185
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301186#ifdef CONFIG_OF
1187static const struct of_device_id of_dwc3_match[] = {
1188 {
Felipe Balbi22a5aa12013-07-02 21:20:24 +03001189 .compatible = "snps,dwc3"
1190 },
1191 {
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301192 .compatible = "synopsys,dwc3"
1193 },
1194 { },
1195};
1196MODULE_DEVICE_TABLE(of, of_dwc3_match);
1197#endif
1198
Heikki Krogerus404905a2014-09-25 10:57:02 +03001199#ifdef CONFIG_ACPI
1200
1201#define ACPI_ID_INTEL_BSW "808622B7"
1202
1203static const struct acpi_device_id dwc3_acpi_match[] = {
1204 { ACPI_ID_INTEL_BSW, 0 },
1205 { },
1206};
1207MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match);
1208#endif
1209
Felipe Balbi72246da2011-08-19 18:10:58 +03001210static struct platform_driver dwc3_driver = {
1211 .probe = dwc3_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001212 .remove = dwc3_remove,
Felipe Balbi72246da2011-08-19 18:10:58 +03001213 .driver = {
1214 .name = "dwc3",
Kishon Vijay Abraham I5088b6f2013-01-25 16:36:53 +05301215 .of_match_table = of_match_ptr(of_dwc3_match),
Heikki Krogerus404905a2014-09-25 10:57:02 +03001216 .acpi_match_table = ACPI_PTR(dwc3_acpi_match),
Felipe Balbi7415f172012-04-30 14:56:33 +03001217 .pm = DWC3_PM_OPS,
Felipe Balbi72246da2011-08-19 18:10:58 +03001218 },
Felipe Balbi72246da2011-08-19 18:10:58 +03001219};
1220
Tobias Klauserb1116dc2012-02-28 12:57:20 +01001221module_platform_driver(dwc3_driver);
1222
Sebastian Andrzej Siewior7ae4fc42011-10-19 19:39:50 +02001223MODULE_ALIAS("platform:dwc3");
Felipe Balbi72246da2011-08-19 18:10:58 +03001224MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
Felipe Balbi5945f782013-06-30 14:15:11 +03001225MODULE_LICENSE("GPL v2");
Felipe Balbi72246da2011-08-19 18:10:58 +03001226MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");