blob: 0f9a63281612993c7529d9cbe430df5a024ce38e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302/**
3 * core.c - DesignWare USB3 DRD Controller Core file
4 *
Kishon Vijay Abraham Id1e431a2015-02-23 18:39:52 +05305 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05306 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 *
Kishon Vijay Abraham Id1e431a2015-02-23 18:39:52 +053010 * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/core.c) and ported
11 * to uboot.
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053012 *
Kishon Vijay Abraham Id1e431a2015-02-23 18:39:52 +053013 * commit cd72f890d2 : usb: dwc3: core: enable phy suspend quirk on non-FPGA
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053014 */
15
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +053016#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070017#include <cpu_func.h>
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +053018#include <malloc.h>
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +053019#include <dwc3-uboot.h>
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +053020#include <asm/dma-mapping.h>
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053021#include <linux/ioport.h>
Mugunthan V N121f93c2018-05-18 13:10:27 +020022#include <dm.h>
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +010023#include <generic-phy.h>
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053024#include <linux/usb/ch9.h>
25#include <linux/usb/gadget.h>
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053026
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053027#include "core.h"
28#include "gadget.h"
29#include "io.h"
30
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +053031#include "linux-compat.h"
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053032
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +053033static LIST_HEAD(dwc3_list);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053034/* -------------------------------------------------------------------------- */
35
Joonyoung Shimbf35c602015-03-03 17:32:09 +010036static void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053037{
38 u32 reg;
39
40 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
41 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
42 reg |= DWC3_GCTL_PRTCAPDIR(mode);
43 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
44}
45
46/**
47 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
48 * @dwc: pointer to our context structure
49 */
50static int dwc3_core_soft_reset(struct dwc3 *dwc)
51{
52 u32 reg;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053053
54 /* Before Resetting PHY, put Core in Reset */
55 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
56 reg |= DWC3_GCTL_CORESOFTRESET;
57 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
58
59 /* Assert USB3 PHY reset */
60 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
61 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
62 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
63
64 /* Assert USB2 PHY reset */
65 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
66 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
67 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
68
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053069 mdelay(100);
70
71 /* Clear USB3 PHY reset */
72 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
73 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
74 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
75
76 /* Clear USB2 PHY reset */
77 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
78 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
79 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
80
81 mdelay(100);
82
83 /* After PHYs are stable we can take Core out of reset state */
84 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
85 reg &= ~DWC3_GCTL_CORESOFTRESET;
86 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
87
88 return 0;
89}
90
91/**
92 * dwc3_free_one_event_buffer - Frees one event buffer
93 * @dwc: Pointer to our controller context structure
94 * @evt: Pointer to event buffer to be freed
95 */
96static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
97 struct dwc3_event_buffer *evt)
98{
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +053099 dma_free_coherent(evt->buf);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530100}
101
102/**
103 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
104 * @dwc: Pointer to our controller context structure
105 * @length: size of the event buffer
106 *
107 * Returns a pointer to the allocated event buffer structure on success
108 * otherwise ERR_PTR(errno).
109 */
110static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
111 unsigned length)
112{
113 struct dwc3_event_buffer *evt;
114
Mugunthan V N121f93c2018-05-18 13:10:27 +0200115 evt = devm_kzalloc((struct udevice *)dwc->dev, sizeof(*evt),
116 GFP_KERNEL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530117 if (!evt)
118 return ERR_PTR(-ENOMEM);
119
120 evt->dwc = dwc;
121 evt->length = length;
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530122 evt->buf = dma_alloc_coherent(length,
123 (unsigned long *)&evt->dma);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530124 if (!evt->buf)
125 return ERR_PTR(-ENOMEM);
126
Philipp Tomsich8e17c162017-04-06 16:58:53 +0200127 dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
128
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530129 return evt;
130}
131
132/**
133 * dwc3_free_event_buffers - frees all allocated event buffers
134 * @dwc: Pointer to our controller context structure
135 */
136static void dwc3_free_event_buffers(struct dwc3 *dwc)
137{
138 struct dwc3_event_buffer *evt;
139 int i;
140
141 for (i = 0; i < dwc->num_event_buffers; i++) {
142 evt = dwc->ev_buffs[i];
143 if (evt)
144 dwc3_free_one_event_buffer(dwc, evt);
145 }
146}
147
148/**
149 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
150 * @dwc: pointer to our controller context structure
151 * @length: size of event buffer
152 *
153 * Returns 0 on success otherwise negative errno. In the error case, dwc
154 * may contain some buffers allocated but not all which were requested.
155 */
156static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
157{
158 int num;
159 int i;
160
161 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
162 dwc->num_event_buffers = num;
163
Kishon Vijay Abraham Ic7bdfe32015-02-23 18:40:13 +0530164 dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE,
165 sizeof(*dwc->ev_buffs) * num);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530166 if (!dwc->ev_buffs)
167 return -ENOMEM;
168
169 for (i = 0; i < num; i++) {
170 struct dwc3_event_buffer *evt;
171
172 evt = dwc3_alloc_one_event_buffer(dwc, length);
173 if (IS_ERR(evt)) {
174 dev_err(dwc->dev, "can't allocate event buffer\n");
175 return PTR_ERR(evt);
176 }
177 dwc->ev_buffs[i] = evt;
178 }
179
180 return 0;
181}
182
183/**
184 * dwc3_event_buffers_setup - setup our allocated event buffers
185 * @dwc: pointer to our controller context structure
186 *
187 * Returns 0 on success otherwise negative errno.
188 */
189static int dwc3_event_buffers_setup(struct dwc3 *dwc)
190{
191 struct dwc3_event_buffer *evt;
192 int n;
193
194 for (n = 0; n < dwc->num_event_buffers; n++) {
195 evt = dwc->ev_buffs[n];
196 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
197 evt->buf, (unsigned long long) evt->dma,
198 evt->length);
199
200 evt->lpos = 0;
201
202 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
203 lower_32_bits(evt->dma));
204 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
205 upper_32_bits(evt->dma));
206 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
207 DWC3_GEVNTSIZ_SIZE(evt->length));
208 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
209 }
210
211 return 0;
212}
213
214static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
215{
216 struct dwc3_event_buffer *evt;
217 int n;
218
219 for (n = 0; n < dwc->num_event_buffers; n++) {
220 evt = dwc->ev_buffs[n];
221
222 evt->lpos = 0;
223
224 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
225 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
226 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
227 | DWC3_GEVNTSIZ_SIZE(0));
228 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
229 }
230}
231
232static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
233{
234 if (!dwc->has_hibernation)
235 return 0;
236
237 if (!dwc->nr_scratch)
238 return 0;
239
240 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
241 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
242 if (!dwc->scratchbuf)
243 return -ENOMEM;
244
245 return 0;
246}
247
248static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
249{
250 dma_addr_t scratch_addr;
251 u32 param;
252 int ret;
253
254 if (!dwc->has_hibernation)
255 return 0;
256
257 if (!dwc->nr_scratch)
258 return 0;
259
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530260 scratch_addr = dma_map_single(dwc->scratchbuf,
261 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
262 DMA_BIDIRECTIONAL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530263 if (dma_mapping_error(dwc->dev, scratch_addr)) {
264 dev_err(dwc->dev, "failed to map scratch buffer\n");
265 ret = -EFAULT;
266 goto err0;
267 }
268
269 dwc->scratch_addr = scratch_addr;
270
271 param = lower_32_bits(scratch_addr);
272
273 ret = dwc3_send_gadget_generic_command(dwc,
274 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
275 if (ret < 0)
276 goto err1;
277
278 param = upper_32_bits(scratch_addr);
279
280 ret = dwc3_send_gadget_generic_command(dwc,
281 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
282 if (ret < 0)
283 goto err1;
284
285 return 0;
286
287err1:
Michal Simek698cd6f2015-10-30 16:24:06 +0100288 dma_unmap_single((void *)(uintptr_t)dwc->scratch_addr, dwc->nr_scratch *
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530289 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530290
291err0:
292 return ret;
293}
294
295static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
296{
297 if (!dwc->has_hibernation)
298 return;
299
300 if (!dwc->nr_scratch)
301 return;
302
Michal Simek698cd6f2015-10-30 16:24:06 +0100303 dma_unmap_single((void *)(uintptr_t)dwc->scratch_addr, dwc->nr_scratch *
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530304 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530305 kfree(dwc->scratchbuf);
306}
307
308static void dwc3_core_num_eps(struct dwc3 *dwc)
309{
310 struct dwc3_hwparams *parms = &dwc->hwparams;
311
312 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
313 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
314
315 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
316 dwc->num_in_eps, dwc->num_out_eps);
317}
318
319static void dwc3_cache_hwparams(struct dwc3 *dwc)
320{
321 struct dwc3_hwparams *parms = &dwc->hwparams;
322
323 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
324 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
325 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
326 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
327 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
328 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
329 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
330 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
331 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
332}
333
334/**
335 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
336 * @dwc: Pointer to our controller context structure
337 */
338static void dwc3_phy_setup(struct dwc3 *dwc)
339{
340 u32 reg;
341
342 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
343
344 /*
345 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
346 * to '0' during coreConsultant configuration. So default value
347 * will be '0' when the core is reset. Application needs to set it
348 * to '1' after the core initialization is completed.
349 */
350 if (dwc->revision > DWC3_REVISION_194A)
351 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
352
353 if (dwc->u2ss_inp3_quirk)
354 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
355
356 if (dwc->req_p1p2p3_quirk)
357 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
358
359 if (dwc->del_p1p2p3_quirk)
360 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
361
362 if (dwc->del_phy_power_chg_quirk)
363 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
364
365 if (dwc->lfps_filter_quirk)
366 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
367
368 if (dwc->rx_detect_poll_quirk)
369 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
370
371 if (dwc->tx_de_emphasis_quirk)
372 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
373
374 if (dwc->dis_u3_susphy_quirk)
375 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
376
377 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
378
379 mdelay(100);
380
381 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
382
383 /*
384 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
385 * '0' during coreConsultant configuration. So default value will
386 * be '0' when the core is reset. Application needs to set it to
387 * '1' after the core initialization is completed.
388 */
389 if (dwc->revision > DWC3_REVISION_194A)
390 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
391
392 if (dwc->dis_u2_susphy_quirk)
393 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
394
395 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
396
397 mdelay(100);
398}
399
400/**
401 * dwc3_core_init - Low-level initialization of DWC3 Core
402 * @dwc: Pointer to our controller context structure
403 *
404 * Returns 0 on success otherwise negative errno.
405 */
406static int dwc3_core_init(struct dwc3 *dwc)
407{
408 unsigned long timeout;
409 u32 hwparams4 = dwc->hwparams.hwparams4;
410 u32 reg;
411 int ret;
412
413 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
414 /* This should read as U3 followed by revision number */
415 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
416 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
417 ret = -ENODEV;
418 goto err0;
419 }
420 dwc->revision = reg;
421
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530422 /* Handle USB2.0-only core configuration */
423 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
424 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
425 if (dwc->maximum_speed == USB_SPEED_SUPER)
426 dwc->maximum_speed = USB_SPEED_HIGH;
427 }
428
429 /* issue device SoftReset too */
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530430 timeout = 5000;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530431 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530432 while (timeout--) {
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530433 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
434 if (!(reg & DWC3_DCTL_CSFTRST))
435 break;
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530436 };
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530437
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530438 if (!timeout) {
439 dev_err(dwc->dev, "Reset Timed Out\n");
440 ret = -ETIMEDOUT;
441 goto err0;
442 }
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530443
T Karthik Reddy7bb245a2019-05-01 10:14:49 +0530444 dwc3_phy_setup(dwc);
445
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530446 ret = dwc3_core_soft_reset(dwc);
447 if (ret)
448 goto err0;
449
450 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
451 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
452
453 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
454 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
455 /**
456 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
457 * issue which would cause xHCI compliance tests to fail.
458 *
459 * Because of that we cannot enable clock gating on such
460 * configurations.
461 *
462 * Refers to:
463 *
464 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
465 * SOF/ITP Mode Used
466 */
467 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
468 dwc->dr_mode == USB_DR_MODE_OTG) &&
469 (dwc->revision >= DWC3_REVISION_210A &&
470 dwc->revision <= DWC3_REVISION_250A))
471 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
472 else
473 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
474 break;
475 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
476 /* enable hibernation here */
477 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
478
479 /*
480 * REVISIT Enabling this bit so that host-mode hibernation
481 * will work. Device-mode hibernation is not yet implemented.
482 */
483 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
484 break;
485 default:
486 dev_dbg(dwc->dev, "No power optimization available\n");
487 }
488
489 /* check if current dwc3 is on simulation board */
490 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
491 dev_dbg(dwc->dev, "it is on FPGA board\n");
492 dwc->is_fpga = true;
493 }
494
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530495 if(dwc->disable_scramble_quirk && !dwc->is_fpga)
496 WARN(true,
497 "disable_scramble cannot be used on non-FPGA builds\n");
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530498
499 if (dwc->disable_scramble_quirk && dwc->is_fpga)
500 reg |= DWC3_GCTL_DISSCRAMBLE;
501 else
502 reg &= ~DWC3_GCTL_DISSCRAMBLE;
503
504 if (dwc->u2exit_lfps_quirk)
505 reg |= DWC3_GCTL_U2EXIT_LFPS;
506
507 /*
508 * WORKAROUND: DWC3 revisions <1.90a have a bug
509 * where the device can fail to connect at SuperSpeed
510 * and falls back to high-speed mode which causes
511 * the device to enter a Connect/Disconnect loop
512 */
513 if (dwc->revision < DWC3_REVISION_190A)
514 reg |= DWC3_GCTL_U2RSTECN;
515
516 dwc3_core_num_eps(dwc);
517
518 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
519
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530520 ret = dwc3_alloc_scratch_buffers(dwc);
521 if (ret)
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530522 goto err0;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530523
524 ret = dwc3_setup_scratch_buffers(dwc);
525 if (ret)
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530526 goto err1;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530527
528 return 0;
529
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530530err1:
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530531 dwc3_free_scratch_buffers(dwc);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530532
533err0:
534 return ret;
535}
536
537static void dwc3_core_exit(struct dwc3 *dwc)
538{
539 dwc3_free_scratch_buffers(dwc);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530540}
541
542static int dwc3_core_init_mode(struct dwc3 *dwc)
543{
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530544 int ret;
545
546 switch (dwc->dr_mode) {
547 case USB_DR_MODE_PERIPHERAL:
548 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
549 ret = dwc3_gadget_init(dwc);
550 if (ret) {
551 dev_err(dev, "failed to initialize gadget\n");
552 return ret;
553 }
554 break;
555 case USB_DR_MODE_HOST:
556 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
557 ret = dwc3_host_init(dwc);
558 if (ret) {
559 dev_err(dev, "failed to initialize host\n");
560 return ret;
561 }
562 break;
563 case USB_DR_MODE_OTG:
564 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
565 ret = dwc3_host_init(dwc);
566 if (ret) {
567 dev_err(dev, "failed to initialize host\n");
568 return ret;
569 }
570
571 ret = dwc3_gadget_init(dwc);
572 if (ret) {
573 dev_err(dev, "failed to initialize gadget\n");
574 return ret;
575 }
576 break;
577 default:
578 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
579 return -EINVAL;
580 }
581
582 return 0;
583}
584
Jean-Jacques Hiblot73a1b8b2019-09-11 11:33:45 +0200585static void dwc3_gadget_run(struct dwc3 *dwc)
586{
587 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_RUN_STOP);
588 mdelay(100);
589}
590
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530591static void dwc3_core_exit_mode(struct dwc3 *dwc)
592{
593 switch (dwc->dr_mode) {
594 case USB_DR_MODE_PERIPHERAL:
595 dwc3_gadget_exit(dwc);
596 break;
597 case USB_DR_MODE_HOST:
598 dwc3_host_exit(dwc);
599 break;
600 case USB_DR_MODE_OTG:
601 dwc3_host_exit(dwc);
602 dwc3_gadget_exit(dwc);
603 break;
604 default:
605 /* do nothing */
606 break;
607 }
Jean-Jacques Hiblot73a1b8b2019-09-11 11:33:45 +0200608
609 /*
610 * switch back to peripheral mode
611 * This enables the phy to enter idle and then, if enabled, suspend.
612 */
613 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
614 dwc3_gadget_run(dwc);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530615}
616
Jagan Teki106c71f2019-11-19 13:56:20 +0530617static void dwc3_uboot_hsphy_mode(struct dwc3_device *dwc3_dev,
618 struct dwc3 *dwc)
619{
620 enum usb_phy_interface hsphy_mode = dwc3_dev->hsphy_mode;
621 u32 reg;
622
623 /* Set dwc3 usb2 phy config */
624 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
625 reg |= DWC3_GUSB2PHYCFG_PHYIF;
626 reg &= ~DWC3_GUSB2PHYCFG_USBTRDTIM_MASK;
627
628 switch (hsphy_mode) {
629 case USBPHY_INTERFACE_MODE_UTMI:
630 reg |= DWC3_GUSB2PHYCFG_USBTRDTIM_8BIT;
631 break;
632 case USBPHY_INTERFACE_MODE_UTMIW:
633 reg |= DWC3_GUSB2PHYCFG_USBTRDTIM_16BIT;
634 break;
635 default:
636 break;
637 }
638
639 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
640}
641
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530642#define DWC3_ALIGN_MASK (16 - 1)
643
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530644/**
645 * dwc3_uboot_init - dwc3 core uboot initialization code
646 * @dwc3_dev: struct dwc3_device containing initialization data
647 *
648 * Entry point for dwc3 driver (equivalent to dwc3_probe in linux
649 * kernel driver). Pointer to dwc3_device should be passed containing
650 * base address and other initialization data. Returns '0' on success and
651 * a negative value on failure.
652 *
653 * Generally called from board_usb_init() implemented in board file.
654 */
655int dwc3_uboot_init(struct dwc3_device *dwc3_dev)
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530656{
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530657 struct dwc3 *dwc;
Felipe Balbi424305f2015-10-01 14:22:18 -0500658 struct device *dev = NULL;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530659 u8 lpm_nyet_threshold;
660 u8 tx_de_emphasis;
661 u8 hird_threshold;
662
663 int ret;
664
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530665 void *mem;
666
Mugunthan V N121f93c2018-05-18 13:10:27 +0200667 mem = devm_kzalloc((struct udevice *)dev,
668 sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530669 if (!mem)
670 return -ENOMEM;
671
672 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
673 dwc->mem = mem;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530674
Michal Simek698cd6f2015-10-30 16:24:06 +0100675 dwc->regs = (void *)(uintptr_t)(dwc3_dev->base +
676 DWC3_GLOBALS_REGS_START);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530677
678 /* default to highest possible threshold */
679 lpm_nyet_threshold = 0xff;
680
681 /* default to -3.5dB de-emphasis */
682 tx_de_emphasis = 1;
683
684 /*
685 * default to assert utmi_sleep_n and use maximum allowed HIRD
686 * threshold value of 0b1100
687 */
688 hird_threshold = 12;
689
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530690 dwc->maximum_speed = dwc3_dev->maximum_speed;
691 dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum;
692 if (dwc3_dev->lpm_nyet_threshold)
693 lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold;
694 dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend;
695 if (dwc3_dev->hird_threshold)
696 hird_threshold = dwc3_dev->hird_threshold;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530697
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530698 dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize;
699 dwc->dr_mode = dwc3_dev->dr_mode;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530700
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530701 dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk;
702 dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk;
703 dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk;
704 dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk;
705 dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk;
706 dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk;
707 dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk;
708 dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk;
709 dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk;
710 dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530711
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530712 dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk;
713 if (dwc3_dev->tx_de_emphasis)
714 tx_de_emphasis = dwc3_dev->tx_de_emphasis;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530715
716 /* default to superspeed if no maximum_speed passed */
717 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
718 dwc->maximum_speed = USB_SPEED_SUPER;
719
720 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
721 dwc->tx_de_emphasis = tx_de_emphasis;
722
723 dwc->hird_threshold = hird_threshold
724 | (dwc->is_utmi_l1_suspend << 4);
725
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530726 dwc->index = dwc3_dev->index;
727
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530728 dwc3_cache_hwparams(dwc);
729
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");
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530733 return -ENOMEM;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530734 }
735
Jean-Jacques Hiblot731a2a32019-09-11 11:33:53 +0200736 if (!IS_ENABLED(CONFIG_USB_DWC3_GADGET))
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530737 dwc->dr_mode = USB_DR_MODE_HOST;
Jean-Jacques Hiblot731a2a32019-09-11 11:33:53 +0200738 else if (!IS_ENABLED(CONFIG_USB_HOST))
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530739 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
740
741 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
742 dwc->dr_mode = USB_DR_MODE_OTG;
743
744 ret = dwc3_core_init(dwc);
745 if (ret) {
746 dev_err(dev, "failed to initialize core\n");
747 goto err0;
748 }
749
Jagan Teki106c71f2019-11-19 13:56:20 +0530750 dwc3_uboot_hsphy_mode(dwc3_dev, dwc);
751
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530752 ret = dwc3_event_buffers_setup(dwc);
753 if (ret) {
754 dev_err(dwc->dev, "failed to setup event buffers\n");
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530755 goto err1;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530756 }
757
758 ret = dwc3_core_init_mode(dwc);
759 if (ret)
760 goto err2;
761
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530762 list_add_tail(&dwc->list, &dwc3_list);
763
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530764 return 0;
765
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530766err2:
767 dwc3_event_buffers_cleanup(dwc);
768
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530769err1:
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530770 dwc3_core_exit(dwc);
771
772err0:
773 dwc3_free_event_buffers(dwc);
774
775 return ret;
776}
777
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530778/**
779 * dwc3_uboot_exit - dwc3 core uboot cleanup code
780 * @index: index of this controller
781 *
782 * Performs cleanup of memory allocated in dwc3_uboot_init and other misc
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530783 * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller
784 * should be passed and should match with the index passed in
785 * dwc3_device during init.
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530786 *
787 * Generally called from board file.
788 */
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530789void dwc3_uboot_exit(int index)
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530790{
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530791 struct dwc3 *dwc;
792
793 list_for_each_entry(dwc, &dwc3_list, list) {
794 if (dwc->index != index)
795 continue;
796
797 dwc3_core_exit_mode(dwc);
798 dwc3_event_buffers_cleanup(dwc);
799 dwc3_free_event_buffers(dwc);
800 dwc3_core_exit(dwc);
801 list_del(&dwc->list);
802 kfree(dwc->mem);
803 break;
804 }
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530805}
806
Kishon Vijay Abraham I1cee7b12015-02-23 18:40:06 +0530807/**
808 * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt
809 * @index: index of this controller
810 *
811 * Invokes dwc3 gadget interrupts.
812 *
813 * Generally called from board file.
814 */
815void dwc3_uboot_handle_interrupt(int index)
816{
817 struct dwc3 *dwc = NULL;
818
819 list_for_each_entry(dwc, &dwc3_list, list) {
820 if (dwc->index != index)
821 continue;
822
823 dwc3_gadget_uboot_handle_interrupt(dwc);
824 break;
825 }
826}
827
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530828MODULE_ALIAS("platform:dwc3");
829MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
830MODULE_LICENSE("GPL v2");
831MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
Mugunthan V N5f7ff712018-05-18 13:15:04 +0200832
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100833#if CONFIG_IS_ENABLED(PHY) && CONFIG_IS_ENABLED(DM_USB)
834int dwc3_setup_phy(struct udevice *dev, struct phy **array, int *num_phys)
835{
836 int i, ret, count;
837 struct phy *usb_phys;
838
839 /* Return if no phy declared */
840 if (!dev_read_prop(dev, "phys", NULL))
841 return 0;
842 count = dev_count_phandle_with_args(dev, "phys", "#phy-cells");
843 if (count <= 0)
844 return count;
845
846 usb_phys = devm_kcalloc(dev, count, sizeof(struct phy),
847 GFP_KERNEL);
848 if (!usb_phys)
849 return -ENOMEM;
850
851 for (i = 0; i < count; i++) {
852 ret = generic_phy_get_by_index(dev, i, &usb_phys[i]);
853 if (ret && ret != -ENOENT) {
854 pr_err("Failed to get USB PHY%d for %s\n",
855 i, dev->name);
856 return ret;
857 }
858 }
859
860 for (i = 0; i < count; i++) {
861 ret = generic_phy_init(&usb_phys[i]);
862 if (ret) {
863 pr_err("Can't init USB PHY%d for %s\n",
864 i, dev->name);
865 goto phys_init_err;
866 }
867 }
868
869 for (i = 0; i < count; i++) {
870 ret = generic_phy_power_on(&usb_phys[i]);
871 if (ret) {
872 pr_err("Can't power USB PHY%d for %s\n",
873 i, dev->name);
874 goto phys_poweron_err;
875 }
876 }
877
878 *array = usb_phys;
879 *num_phys = count;
880 return 0;
881
882phys_poweron_err:
883 for (i = count - 1; i >= 0; i--)
884 generic_phy_power_off(&usb_phys[i]);
885
886 for (i = 0; i < count; i++)
887 generic_phy_exit(&usb_phys[i]);
888
889 return ret;
890
891phys_init_err:
892 for (; i >= 0; i--)
893 generic_phy_exit(&usb_phys[i]);
894
895 return ret;
896}
897
898int dwc3_shutdown_phy(struct udevice *dev, struct phy *usb_phys, int num_phys)
899{
900 int i, ret;
901
902 for (i = 0; i < num_phys; i++) {
903 if (!generic_phy_valid(&usb_phys[i]))
904 continue;
905
906 ret = generic_phy_power_off(&usb_phys[i]);
907 ret |= generic_phy_exit(&usb_phys[i]);
908 if (ret) {
909 pr_err("Can't shutdown USB PHY%d for %s\n",
910 i, dev->name);
911 }
912 }
913
914 return 0;
915}
916#endif
917
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200918#if CONFIG_IS_ENABLED(DM_USB)
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +0200919void dwc3_of_parse(struct dwc3 *dwc)
920{
921 const u8 *tmp;
922 struct udevice *dev = dwc->dev;
923 u8 lpm_nyet_threshold;
924 u8 tx_de_emphasis;
925 u8 hird_threshold;
926
927 /* default to highest possible threshold */
928 lpm_nyet_threshold = 0xff;
929
930 /* default to -3.5dB de-emphasis */
931 tx_de_emphasis = 1;
932
933 /*
934 * default to assert utmi_sleep_n and use maximum allowed HIRD
935 * threshold value of 0b1100
936 */
937 hird_threshold = 12;
938
939 dwc->has_lpm_erratum = dev_read_bool(dev,
940 "snps,has-lpm-erratum");
941 tmp = dev_read_u8_array_ptr(dev, "snps,lpm-nyet-threshold", 1);
942 if (tmp)
943 lpm_nyet_threshold = *tmp;
944
945 dwc->is_utmi_l1_suspend = dev_read_bool(dev,
946 "snps,is-utmi-l1-suspend");
947 tmp = dev_read_u8_array_ptr(dev, "snps,hird-threshold", 1);
948 if (tmp)
949 hird_threshold = *tmp;
950
951 dwc->disable_scramble_quirk = dev_read_bool(dev,
952 "snps,disable_scramble_quirk");
953 dwc->u2exit_lfps_quirk = dev_read_bool(dev,
954 "snps,u2exit_lfps_quirk");
955 dwc->u2ss_inp3_quirk = dev_read_bool(dev,
956 "snps,u2ss_inp3_quirk");
957 dwc->req_p1p2p3_quirk = dev_read_bool(dev,
958 "snps,req_p1p2p3_quirk");
959 dwc->del_p1p2p3_quirk = dev_read_bool(dev,
960 "snps,del_p1p2p3_quirk");
961 dwc->del_phy_power_chg_quirk = dev_read_bool(dev,
962 "snps,del_phy_power_chg_quirk");
963 dwc->lfps_filter_quirk = dev_read_bool(dev,
964 "snps,lfps_filter_quirk");
965 dwc->rx_detect_poll_quirk = dev_read_bool(dev,
966 "snps,rx_detect_poll_quirk");
967 dwc->dis_u3_susphy_quirk = dev_read_bool(dev,
968 "snps,dis_u3_susphy_quirk");
969 dwc->dis_u2_susphy_quirk = dev_read_bool(dev,
970 "snps,dis_u2_susphy_quirk");
971 dwc->tx_de_emphasis_quirk = dev_read_bool(dev,
972 "snps,tx_de_emphasis_quirk");
973 tmp = dev_read_u8_array_ptr(dev, "snps,tx_de_emphasis", 1);
974 if (tmp)
975 tx_de_emphasis = *tmp;
976
977 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
978 dwc->tx_de_emphasis = tx_de_emphasis;
979
980 dwc->hird_threshold = hird_threshold
981 | (dwc->is_utmi_l1_suspend << 4);
982}
983
Mugunthan V N5f7ff712018-05-18 13:15:04 +0200984int dwc3_init(struct dwc3 *dwc)
985{
986 int ret;
987
988 dwc3_cache_hwparams(dwc);
989
990 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
991 if (ret) {
992 dev_err(dwc->dev, "failed to allocate event buffers\n");
993 return -ENOMEM;
994 }
995
996 ret = dwc3_core_init(dwc);
997 if (ret) {
998 dev_err(dev, "failed to initialize core\n");
999 goto core_fail;
1000 }
1001
1002 ret = dwc3_event_buffers_setup(dwc);
1003 if (ret) {
1004 dev_err(dwc->dev, "failed to setup event buffers\n");
1005 goto event_fail;
1006 }
1007
1008 ret = dwc3_core_init_mode(dwc);
1009 if (ret)
1010 goto mode_fail;
1011
1012 return 0;
1013
1014mode_fail:
1015 dwc3_event_buffers_cleanup(dwc);
1016
1017event_fail:
1018 dwc3_core_exit(dwc);
1019
1020core_fail:
1021 dwc3_free_event_buffers(dwc);
1022
1023 return ret;
1024}
1025
1026void dwc3_remove(struct dwc3 *dwc)
1027{
1028 dwc3_core_exit_mode(dwc);
1029 dwc3_event_buffers_cleanup(dwc);
1030 dwc3_free_event_buffers(dwc);
1031 dwc3_core_exit(dwc);
1032 kfree(dwc->mem);
1033}
Mugunthan V N5f7ff712018-05-18 13:15:04 +02001034#endif