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