blob: b592a487e001f576a092cb2efc67b8a8985c4afa [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>
Simon Glass9bc15642020-02-03 07:36:16 -070020#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070021#include <dm/devres.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060022#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Masahiro Yamada6373a172020-02-14 16:40:19 +090024#include <linux/dma-mapping.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070025#include <linux/err.h>
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053026#include <linux/ioport.h>
Mugunthan V N121f93c2018-05-18 13:10:27 +020027#include <dm.h>
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +010028#include <generic-phy.h>
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053029#include <linux/usb/ch9.h>
30#include <linux/usb/gadget.h>
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053031
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053032#include "core.h"
33#include "gadget.h"
34#include "io.h"
35
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +053036#include "linux-compat.h"
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053037
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +053038static LIST_HEAD(dwc3_list);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053039/* -------------------------------------------------------------------------- */
40
Joonyoung Shimbf35c602015-03-03 17:32:09 +010041static void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053042{
43 u32 reg;
44
45 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
46 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
47 reg |= DWC3_GCTL_PRTCAPDIR(mode);
48 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
49}
50
51/**
52 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
53 * @dwc: pointer to our context structure
54 */
55static int dwc3_core_soft_reset(struct dwc3 *dwc)
56{
57 u32 reg;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053058
59 /* Before Resetting PHY, put Core in Reset */
60 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
61 reg |= DWC3_GCTL_CORESOFTRESET;
62 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
63
64 /* Assert USB3 PHY reset */
65 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
66 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
67 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
68
69 /* Assert USB2 PHY reset */
70 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
71 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
72 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
73
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +053074 mdelay(100);
75
76 /* Clear 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 /* Clear 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
86 mdelay(100);
87
88 /* After PHYs are stable we can take Core out of reset state */
89 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
90 reg &= ~DWC3_GCTL_CORESOFTRESET;
91 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
92
93 return 0;
94}
95
Michael Walle679f13c2021-10-15 15:15:21 +020096/*
97 * dwc3_frame_length_adjustment - Adjusts frame length if required
98 * @dwc3: Pointer to our controller context structure
99 * @fladj: Value of GFLADJ_30MHZ to adjust frame length
100 */
101static void dwc3_frame_length_adjustment(struct dwc3 *dwc, u32 fladj)
102{
103 u32 reg;
104
105 if (dwc->revision < DWC3_REVISION_250A)
106 return;
107
108 if (fladj == 0)
109 return;
110
111 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
112 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
113 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | fladj;
114 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
115}
116
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530117/**
118 * dwc3_free_one_event_buffer - Frees one event buffer
119 * @dwc: Pointer to our controller context structure
120 * @evt: Pointer to event buffer to be freed
121 */
122static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
123 struct dwc3_event_buffer *evt)
124{
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530125 dma_free_coherent(evt->buf);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530126}
127
128/**
129 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
130 * @dwc: Pointer to our controller context structure
131 * @length: size of the event buffer
132 *
133 * Returns a pointer to the allocated event buffer structure on success
134 * otherwise ERR_PTR(errno).
135 */
136static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
137 unsigned length)
138{
139 struct dwc3_event_buffer *evt;
140
Mugunthan V N121f93c2018-05-18 13:10:27 +0200141 evt = devm_kzalloc((struct udevice *)dwc->dev, sizeof(*evt),
142 GFP_KERNEL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530143 if (!evt)
144 return ERR_PTR(-ENOMEM);
145
146 evt->dwc = dwc;
147 evt->length = length;
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530148 evt->buf = dma_alloc_coherent(length,
149 (unsigned long *)&evt->dma);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530150 if (!evt->buf)
151 return ERR_PTR(-ENOMEM);
152
Philipp Tomsich8e17c162017-04-06 16:58:53 +0200153 dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
154
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530155 return evt;
156}
157
158/**
159 * dwc3_free_event_buffers - frees all allocated event buffers
160 * @dwc: Pointer to our controller context structure
161 */
162static void dwc3_free_event_buffers(struct dwc3 *dwc)
163{
164 struct dwc3_event_buffer *evt;
165 int i;
166
167 for (i = 0; i < dwc->num_event_buffers; i++) {
168 evt = dwc->ev_buffs[i];
169 if (evt)
170 dwc3_free_one_event_buffer(dwc, evt);
171 }
172}
173
174/**
175 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
176 * @dwc: pointer to our controller context structure
177 * @length: size of event buffer
178 *
179 * Returns 0 on success otherwise negative errno. In the error case, dwc
180 * may contain some buffers allocated but not all which were requested.
181 */
182static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
183{
184 int num;
185 int i;
186
187 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
188 dwc->num_event_buffers = num;
189
Kishon Vijay Abraham Ic7bdfe32015-02-23 18:40:13 +0530190 dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE,
191 sizeof(*dwc->ev_buffs) * num);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530192 if (!dwc->ev_buffs)
193 return -ENOMEM;
194
195 for (i = 0; i < num; i++) {
196 struct dwc3_event_buffer *evt;
197
198 evt = dwc3_alloc_one_event_buffer(dwc, length);
199 if (IS_ERR(evt)) {
200 dev_err(dwc->dev, "can't allocate event buffer\n");
201 return PTR_ERR(evt);
202 }
203 dwc->ev_buffs[i] = evt;
204 }
205
206 return 0;
207}
208
209/**
210 * dwc3_event_buffers_setup - setup our allocated event buffers
211 * @dwc: pointer to our controller context structure
212 *
213 * Returns 0 on success otherwise negative errno.
214 */
215static int dwc3_event_buffers_setup(struct dwc3 *dwc)
216{
217 struct dwc3_event_buffer *evt;
218 int n;
219
220 for (n = 0; n < dwc->num_event_buffers; n++) {
221 evt = dwc->ev_buffs[n];
222 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
223 evt->buf, (unsigned long long) evt->dma,
224 evt->length);
225
226 evt->lpos = 0;
227
228 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
229 lower_32_bits(evt->dma));
230 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
231 upper_32_bits(evt->dma));
232 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
233 DWC3_GEVNTSIZ_SIZE(evt->length));
234 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
235 }
236
237 return 0;
238}
239
240static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
241{
242 struct dwc3_event_buffer *evt;
243 int n;
244
245 for (n = 0; n < dwc->num_event_buffers; n++) {
246 evt = dwc->ev_buffs[n];
247
248 evt->lpos = 0;
249
250 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
251 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
252 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
253 | DWC3_GEVNTSIZ_SIZE(0));
254 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
255 }
256}
257
258static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
259{
260 if (!dwc->has_hibernation)
261 return 0;
262
263 if (!dwc->nr_scratch)
264 return 0;
265
266 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
267 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
268 if (!dwc->scratchbuf)
269 return -ENOMEM;
270
271 return 0;
272}
273
274static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
275{
276 dma_addr_t scratch_addr;
277 u32 param;
278 int ret;
279
280 if (!dwc->has_hibernation)
281 return 0;
282
283 if (!dwc->nr_scratch)
284 return 0;
285
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530286 scratch_addr = dma_map_single(dwc->scratchbuf,
287 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
288 DMA_BIDIRECTIONAL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530289 if (dma_mapping_error(dwc->dev, scratch_addr)) {
290 dev_err(dwc->dev, "failed to map scratch buffer\n");
291 ret = -EFAULT;
292 goto err0;
293 }
294
295 dwc->scratch_addr = scratch_addr;
296
297 param = lower_32_bits(scratch_addr);
298
299 ret = dwc3_send_gadget_generic_command(dwc,
300 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
301 if (ret < 0)
302 goto err1;
303
304 param = upper_32_bits(scratch_addr);
305
306 ret = dwc3_send_gadget_generic_command(dwc,
307 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
308 if (ret < 0)
309 goto err1;
310
311 return 0;
312
313err1:
Masahiro Yamada05a5dba2020-02-14 16:40:18 +0900314 dma_unmap_single(scratch_addr, dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
315 DMA_BIDIRECTIONAL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530316
317err0:
318 return ret;
319}
320
321static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
322{
323 if (!dwc->has_hibernation)
324 return;
325
326 if (!dwc->nr_scratch)
327 return;
328
Masahiro Yamada05a5dba2020-02-14 16:40:18 +0900329 dma_unmap_single(dwc->scratch_addr, dwc->nr_scratch *
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530330 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530331 kfree(dwc->scratchbuf);
332}
333
334static void dwc3_core_num_eps(struct dwc3 *dwc)
335{
336 struct dwc3_hwparams *parms = &dwc->hwparams;
337
338 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
339 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
340
341 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
342 dwc->num_in_eps, dwc->num_out_eps);
343}
344
345static void dwc3_cache_hwparams(struct dwc3 *dwc)
346{
347 struct dwc3_hwparams *parms = &dwc->hwparams;
348
349 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
350 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
351 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
352 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
353 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
354 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
355 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
356 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
357 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
358}
359
Frank Wanga4a29122020-05-26 11:34:30 +0800360static void dwc3_hsphy_mode_setup(struct dwc3 *dwc)
361{
362 enum usb_phy_interface hsphy_mode = dwc->hsphy_mode;
363 u32 reg;
364
365 /* Set dwc3 usb2 phy config */
366 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
367
368 switch (hsphy_mode) {
369 case USBPHY_INTERFACE_MODE_UTMI:
370 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
371 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
372 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
373 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
374 break;
375 case USBPHY_INTERFACE_MODE_UTMIW:
376 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
377 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
378 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
379 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
380 break;
381 default:
382 break;
383 }
384
385 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
386}
387
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530388/**
389 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
390 * @dwc: Pointer to our controller context structure
391 */
392static void dwc3_phy_setup(struct dwc3 *dwc)
393{
394 u32 reg;
395
396 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
397
398 /*
399 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
400 * to '0' during coreConsultant configuration. So default value
401 * will be '0' when the core is reset. Application needs to set it
402 * to '1' after the core initialization is completed.
403 */
404 if (dwc->revision > DWC3_REVISION_194A)
405 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
406
407 if (dwc->u2ss_inp3_quirk)
408 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
409
410 if (dwc->req_p1p2p3_quirk)
411 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
412
413 if (dwc->del_p1p2p3_quirk)
414 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
415
416 if (dwc->del_phy_power_chg_quirk)
417 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
418
419 if (dwc->lfps_filter_quirk)
420 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
421
422 if (dwc->rx_detect_poll_quirk)
423 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
424
425 if (dwc->tx_de_emphasis_quirk)
426 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
427
428 if (dwc->dis_u3_susphy_quirk)
429 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
430
Jagan Tekic1157dc2020-05-06 13:20:25 +0530431 if (dwc->dis_del_phy_power_chg_quirk)
432 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
433
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530434 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
435
Frank Wanga4a29122020-05-26 11:34:30 +0800436 dwc3_hsphy_mode_setup(dwc);
437
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530438 mdelay(100);
439
440 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
441
442 /*
443 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
444 * '0' during coreConsultant configuration. So default value will
445 * be '0' when the core is reset. Application needs to set it to
446 * '1' after the core initialization is completed.
447 */
448 if (dwc->revision > DWC3_REVISION_194A)
449 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
450
451 if (dwc->dis_u2_susphy_quirk)
452 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
453
Frank Wang0c3b6f52020-05-26 11:33:46 +0800454 if (dwc->dis_enblslpm_quirk)
455 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
456
Frank Wangb29bcd72020-05-26 11:33:47 +0800457 if (dwc->dis_u2_freeclk_exists_quirk)
458 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
459
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530460 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
461
462 mdelay(100);
463}
464
Michael Walle668bdf62021-10-15 15:15:22 +0200465/* set global incr burst type configuration registers */
466static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
467{
468 struct udevice *dev = dwc->dev;
469 u32 cfg;
470
471 if (!dwc->incrx_size)
472 return;
473
474 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
475
476 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
477 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
478 if (dwc->incrx_mode)
479 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
480 switch (dwc->incrx_size) {
481 case 256:
482 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
483 break;
484 case 128:
485 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
486 break;
487 case 64:
488 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
489 break;
490 case 32:
491 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
492 break;
493 case 16:
494 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
495 break;
496 case 8:
497 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
498 break;
499 case 4:
500 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
501 break;
502 case 1:
503 break;
504 default:
505 dev_err(dev, "Invalid property\n");
506 break;
507 }
508
509 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
510}
511
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530512/**
513 * dwc3_core_init - Low-level initialization of DWC3 Core
514 * @dwc: Pointer to our controller context structure
515 *
516 * Returns 0 on success otherwise negative errno.
517 */
518static int dwc3_core_init(struct dwc3 *dwc)
519{
520 unsigned long timeout;
521 u32 hwparams4 = dwc->hwparams.hwparams4;
522 u32 reg;
523 int ret;
524
525 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
526 /* This should read as U3 followed by revision number */
527 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
528 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
529 ret = -ENODEV;
530 goto err0;
531 }
532 dwc->revision = reg;
533
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530534 /* Handle USB2.0-only core configuration */
535 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
536 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
537 if (dwc->maximum_speed == USB_SPEED_SUPER)
538 dwc->maximum_speed = USB_SPEED_HIGH;
539 }
540
541 /* issue device SoftReset too */
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530542 timeout = 5000;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530543 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530544 while (timeout--) {
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530545 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
546 if (!(reg & DWC3_DCTL_CSFTRST))
547 break;
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530548 };
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530549
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530550 if (!timeout) {
551 dev_err(dwc->dev, "Reset Timed Out\n");
552 ret = -ETIMEDOUT;
553 goto err0;
554 }
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530555
T Karthik Reddy7bb245a2019-05-01 10:14:49 +0530556 dwc3_phy_setup(dwc);
557
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530558 ret = dwc3_core_soft_reset(dwc);
559 if (ret)
560 goto err0;
561
562 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
563 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
564
565 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
566 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
567 /**
568 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
569 * issue which would cause xHCI compliance tests to fail.
570 *
571 * Because of that we cannot enable clock gating on such
572 * configurations.
573 *
574 * Refers to:
575 *
576 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
577 * SOF/ITP Mode Used
578 */
579 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
580 dwc->dr_mode == USB_DR_MODE_OTG) &&
581 (dwc->revision >= DWC3_REVISION_210A &&
582 dwc->revision <= DWC3_REVISION_250A))
583 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
584 else
585 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
586 break;
587 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
588 /* enable hibernation here */
589 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
590
591 /*
592 * REVISIT Enabling this bit so that host-mode hibernation
593 * will work. Device-mode hibernation is not yet implemented.
594 */
595 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
596 break;
597 default:
598 dev_dbg(dwc->dev, "No power optimization available\n");
599 }
600
601 /* check if current dwc3 is on simulation board */
602 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
603 dev_dbg(dwc->dev, "it is on FPGA board\n");
604 dwc->is_fpga = true;
605 }
606
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530607 if(dwc->disable_scramble_quirk && !dwc->is_fpga)
608 WARN(true,
609 "disable_scramble cannot be used on non-FPGA builds\n");
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530610
611 if (dwc->disable_scramble_quirk && dwc->is_fpga)
612 reg |= DWC3_GCTL_DISSCRAMBLE;
613 else
614 reg &= ~DWC3_GCTL_DISSCRAMBLE;
615
616 if (dwc->u2exit_lfps_quirk)
617 reg |= DWC3_GCTL_U2EXIT_LFPS;
618
619 /*
620 * WORKAROUND: DWC3 revisions <1.90a have a bug
621 * where the device can fail to connect at SuperSpeed
622 * and falls back to high-speed mode which causes
623 * the device to enter a Connect/Disconnect loop
624 */
625 if (dwc->revision < DWC3_REVISION_190A)
626 reg |= DWC3_GCTL_U2RSTECN;
627
628 dwc3_core_num_eps(dwc);
629
630 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
631
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530632 ret = dwc3_alloc_scratch_buffers(dwc);
633 if (ret)
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530634 goto err0;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530635
636 ret = dwc3_setup_scratch_buffers(dwc);
637 if (ret)
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530638 goto err1;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530639
Michael Walle679f13c2021-10-15 15:15:21 +0200640 /* Adjust Frame Length */
641 dwc3_frame_length_adjustment(dwc, dwc->fladj);
642
Michael Walle668bdf62021-10-15 15:15:22 +0200643 dwc3_set_incr_burst_type(dwc);
644
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530645 return 0;
646
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530647err1:
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530648 dwc3_free_scratch_buffers(dwc);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530649
650err0:
651 return ret;
652}
653
654static void dwc3_core_exit(struct dwc3 *dwc)
655{
656 dwc3_free_scratch_buffers(dwc);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530657}
658
659static int dwc3_core_init_mode(struct dwc3 *dwc)
660{
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530661 int ret;
662
663 switch (dwc->dr_mode) {
664 case USB_DR_MODE_PERIPHERAL:
665 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
666 ret = dwc3_gadget_init(dwc);
667 if (ret) {
Sean Anderson2789ce82020-09-15 10:45:16 -0400668 dev_err(dwc->dev, "failed to initialize gadget\n");
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530669 return ret;
670 }
671 break;
672 case USB_DR_MODE_HOST:
673 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
674 ret = dwc3_host_init(dwc);
675 if (ret) {
Sean Anderson2789ce82020-09-15 10:45:16 -0400676 dev_err(dwc->dev, "failed to initialize host\n");
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530677 return ret;
678 }
679 break;
680 case USB_DR_MODE_OTG:
681 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
682 ret = dwc3_host_init(dwc);
683 if (ret) {
Sean Anderson2789ce82020-09-15 10:45:16 -0400684 dev_err(dwc->dev, "failed to initialize host\n");
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530685 return ret;
686 }
687
688 ret = dwc3_gadget_init(dwc);
689 if (ret) {
Sean Anderson2789ce82020-09-15 10:45:16 -0400690 dev_err(dwc->dev, "failed to initialize gadget\n");
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530691 return ret;
692 }
693 break;
694 default:
Sean Anderson2789ce82020-09-15 10:45:16 -0400695 dev_err(dwc->dev,
696 "Unsupported mode of operation %d\n", dwc->dr_mode);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530697 return -EINVAL;
698 }
699
700 return 0;
701}
702
Jean-Jacques Hiblot73a1b8b2019-09-11 11:33:45 +0200703static void dwc3_gadget_run(struct dwc3 *dwc)
704{
705 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_RUN_STOP);
706 mdelay(100);
707}
708
Angus Ainslieeefabee2022-02-02 15:08:55 -0800709static void dwc3_core_stop(struct dwc3 *dwc)
710{
711 u32 reg;
712
713 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
714 dwc3_writel(dwc->regs, DWC3_DCTL, reg & ~(DWC3_DCTL_RUN_STOP));
715}
716
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530717static void dwc3_core_exit_mode(struct dwc3 *dwc)
718{
719 switch (dwc->dr_mode) {
720 case USB_DR_MODE_PERIPHERAL:
721 dwc3_gadget_exit(dwc);
722 break;
723 case USB_DR_MODE_HOST:
724 dwc3_host_exit(dwc);
725 break;
726 case USB_DR_MODE_OTG:
727 dwc3_host_exit(dwc);
728 dwc3_gadget_exit(dwc);
729 break;
730 default:
731 /* do nothing */
732 break;
733 }
Jean-Jacques Hiblot73a1b8b2019-09-11 11:33:45 +0200734
735 /*
736 * switch back to peripheral mode
737 * This enables the phy to enter idle and then, if enabled, suspend.
738 */
739 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
740 dwc3_gadget_run(dwc);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530741}
742
743#define DWC3_ALIGN_MASK (16 - 1)
744
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530745/**
746 * dwc3_uboot_init - dwc3 core uboot initialization code
747 * @dwc3_dev: struct dwc3_device containing initialization data
748 *
749 * Entry point for dwc3 driver (equivalent to dwc3_probe in linux
750 * kernel driver). Pointer to dwc3_device should be passed containing
751 * base address and other initialization data. Returns '0' on success and
752 * a negative value on failure.
753 *
754 * Generally called from board_usb_init() implemented in board file.
755 */
756int dwc3_uboot_init(struct dwc3_device *dwc3_dev)
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530757{
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530758 struct dwc3 *dwc;
Felipe Balbi424305f2015-10-01 14:22:18 -0500759 struct device *dev = NULL;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530760 u8 lpm_nyet_threshold;
761 u8 tx_de_emphasis;
762 u8 hird_threshold;
763
764 int ret;
765
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530766 void *mem;
767
Mugunthan V N121f93c2018-05-18 13:10:27 +0200768 mem = devm_kzalloc((struct udevice *)dev,
769 sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530770 if (!mem)
771 return -ENOMEM;
772
773 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
774 dwc->mem = mem;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530775
Michal Simek698cd6f2015-10-30 16:24:06 +0100776 dwc->regs = (void *)(uintptr_t)(dwc3_dev->base +
777 DWC3_GLOBALS_REGS_START);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530778
779 /* default to highest possible threshold */
780 lpm_nyet_threshold = 0xff;
781
782 /* default to -3.5dB de-emphasis */
783 tx_de_emphasis = 1;
784
785 /*
786 * default to assert utmi_sleep_n and use maximum allowed HIRD
787 * threshold value of 0b1100
788 */
789 hird_threshold = 12;
790
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530791 dwc->maximum_speed = dwc3_dev->maximum_speed;
792 dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum;
793 if (dwc3_dev->lpm_nyet_threshold)
794 lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold;
795 dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend;
796 if (dwc3_dev->hird_threshold)
797 hird_threshold = dwc3_dev->hird_threshold;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530798
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530799 dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize;
800 dwc->dr_mode = dwc3_dev->dr_mode;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530801
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530802 dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk;
803 dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk;
804 dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk;
805 dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk;
806 dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk;
807 dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk;
808 dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk;
809 dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk;
810 dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk;
811 dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk;
Jagan Tekic1157dc2020-05-06 13:20:25 +0530812 dwc->dis_del_phy_power_chg_quirk = dwc3_dev->dis_del_phy_power_chg_quirk;
Jagan Teki0ece2f72020-05-26 11:33:48 +0800813 dwc->dis_tx_ipgap_linecheck_quirk = dwc3_dev->dis_tx_ipgap_linecheck_quirk;
Frank Wang0c3b6f52020-05-26 11:33:46 +0800814 dwc->dis_enblslpm_quirk = dwc3_dev->dis_enblslpm_quirk;
Frank Wangb29bcd72020-05-26 11:33:47 +0800815 dwc->dis_u2_freeclk_exists_quirk = dwc3_dev->dis_u2_freeclk_exists_quirk;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530816
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530817 dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk;
818 if (dwc3_dev->tx_de_emphasis)
819 tx_de_emphasis = dwc3_dev->tx_de_emphasis;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530820
821 /* default to superspeed if no maximum_speed passed */
822 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
823 dwc->maximum_speed = USB_SPEED_SUPER;
824
825 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
826 dwc->tx_de_emphasis = tx_de_emphasis;
827
828 dwc->hird_threshold = hird_threshold
829 | (dwc->is_utmi_l1_suspend << 4);
830
Frank Wanga4a29122020-05-26 11:34:30 +0800831 dwc->hsphy_mode = dwc3_dev->hsphy_mode;
832
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530833 dwc->index = dwc3_dev->index;
834
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530835 dwc3_cache_hwparams(dwc);
836
837 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
838 if (ret) {
839 dev_err(dwc->dev, "failed to allocate event buffers\n");
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530840 return -ENOMEM;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530841 }
842
Jean-Jacques Hiblot731a2a32019-09-11 11:33:53 +0200843 if (!IS_ENABLED(CONFIG_USB_DWC3_GADGET))
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530844 dwc->dr_mode = USB_DR_MODE_HOST;
Jean-Jacques Hiblot731a2a32019-09-11 11:33:53 +0200845 else if (!IS_ENABLED(CONFIG_USB_HOST))
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530846 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
847
848 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
849 dwc->dr_mode = USB_DR_MODE_OTG;
850
851 ret = dwc3_core_init(dwc);
852 if (ret) {
Sean Anderson2789ce82020-09-15 10:45:16 -0400853 dev_err(dwc->dev, "failed to initialize core\n");
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530854 goto err0;
855 }
856
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530857 ret = dwc3_event_buffers_setup(dwc);
858 if (ret) {
859 dev_err(dwc->dev, "failed to setup event buffers\n");
Kishon Vijay Abraham I99030d72015-02-23 18:40:02 +0530860 goto err1;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530861 }
862
863 ret = dwc3_core_init_mode(dwc);
864 if (ret)
865 goto err2;
866
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530867 list_add_tail(&dwc->list, &dwc3_list);
868
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530869 return 0;
870
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530871err2:
872 dwc3_event_buffers_cleanup(dwc);
873
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530874err1:
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530875 dwc3_core_exit(dwc);
876
877err0:
878 dwc3_free_event_buffers(dwc);
879
880 return ret;
881}
882
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530883/**
884 * dwc3_uboot_exit - dwc3 core uboot cleanup code
885 * @index: index of this controller
886 *
887 * Performs cleanup of memory allocated in dwc3_uboot_init and other misc
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530888 * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller
889 * should be passed and should match with the index passed in
890 * dwc3_device during init.
Kishon Vijay Abraham Ibfbf05d2015-02-23 18:40:04 +0530891 *
892 * Generally called from board file.
893 */
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530894void dwc3_uboot_exit(int index)
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530895{
Kishon Vijay Abraham Idc5c6532015-02-23 18:40:05 +0530896 struct dwc3 *dwc;
897
898 list_for_each_entry(dwc, &dwc3_list, list) {
899 if (dwc->index != index)
900 continue;
901
902 dwc3_core_exit_mode(dwc);
903 dwc3_event_buffers_cleanup(dwc);
904 dwc3_free_event_buffers(dwc);
905 dwc3_core_exit(dwc);
906 list_del(&dwc->list);
907 kfree(dwc->mem);
908 break;
909 }
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530910}
911
Kishon Vijay Abraham I1cee7b12015-02-23 18:40:06 +0530912/**
913 * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt
914 * @index: index of this controller
915 *
916 * Invokes dwc3 gadget interrupts.
917 *
918 * Generally called from board file.
919 */
920void dwc3_uboot_handle_interrupt(int index)
921{
922 struct dwc3 *dwc = NULL;
923
924 list_for_each_entry(dwc, &dwc3_list, list) {
925 if (dwc->index != index)
926 continue;
927
928 dwc3_gadget_uboot_handle_interrupt(dwc);
929 break;
930 }
931}
932
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +0530933MODULE_ALIAS("platform:dwc3");
934MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
935MODULE_LICENSE("GPL v2");
936MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
Mugunthan V N5f7ff712018-05-18 13:15:04 +0200937
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100938#if CONFIG_IS_ENABLED(PHY) && CONFIG_IS_ENABLED(DM_USB)
developerf8bced12020-05-02 11:35:13 +0200939int dwc3_setup_phy(struct udevice *dev, struct phy_bulk *phys)
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100940{
developerf8bced12020-05-02 11:35:13 +0200941 int ret;
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100942
developerf8bced12020-05-02 11:35:13 +0200943 ret = generic_phy_get_bulk(dev, phys);
944 if (ret)
945 return ret;
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100946
developerf8bced12020-05-02 11:35:13 +0200947 ret = generic_phy_init_bulk(phys);
948 if (ret)
949 return ret;
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100950
developerf8bced12020-05-02 11:35:13 +0200951 ret = generic_phy_power_on_bulk(phys);
952 if (ret)
953 generic_phy_exit_bulk(phys);
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100954
955 return ret;
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100956}
957
developerf8bced12020-05-02 11:35:13 +0200958int dwc3_shutdown_phy(struct udevice *dev, struct phy_bulk *phys)
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100959{
developerf8bced12020-05-02 11:35:13 +0200960 int ret;
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100961
developerf8bced12020-05-02 11:35:13 +0200962 ret = generic_phy_power_off_bulk(phys);
963 ret |= generic_phy_exit_bulk(phys);
964 return ret;
Jean-Jacques Hiblot3de978a2018-11-29 10:52:45 +0100965}
966#endif
967
Jean-Jacques Hiblot175cd7c2019-09-11 11:33:50 +0200968#if CONFIG_IS_ENABLED(DM_USB)
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +0200969void dwc3_of_parse(struct dwc3 *dwc)
970{
971 const u8 *tmp;
972 struct udevice *dev = dwc->dev;
973 u8 lpm_nyet_threshold;
974 u8 tx_de_emphasis;
975 u8 hird_threshold;
Michael Walle668bdf62021-10-15 15:15:22 +0200976 u32 val;
977 int i;
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +0200978
979 /* default to highest possible threshold */
980 lpm_nyet_threshold = 0xff;
981
982 /* default to -3.5dB de-emphasis */
983 tx_de_emphasis = 1;
984
985 /*
986 * default to assert utmi_sleep_n and use maximum allowed HIRD
987 * threshold value of 0b1100
988 */
989 hird_threshold = 12;
990
Simon Glassa7ece582020-12-19 10:40:14 -0700991 dwc->hsphy_mode = usb_get_phy_mode(dev_ofnode(dev));
Frank Wanga4a29122020-05-26 11:34:30 +0800992
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +0200993 dwc->has_lpm_erratum = dev_read_bool(dev,
994 "snps,has-lpm-erratum");
995 tmp = dev_read_u8_array_ptr(dev, "snps,lpm-nyet-threshold", 1);
996 if (tmp)
997 lpm_nyet_threshold = *tmp;
998
999 dwc->is_utmi_l1_suspend = dev_read_bool(dev,
1000 "snps,is-utmi-l1-suspend");
1001 tmp = dev_read_u8_array_ptr(dev, "snps,hird-threshold", 1);
1002 if (tmp)
1003 hird_threshold = *tmp;
1004
1005 dwc->disable_scramble_quirk = dev_read_bool(dev,
1006 "snps,disable_scramble_quirk");
1007 dwc->u2exit_lfps_quirk = dev_read_bool(dev,
1008 "snps,u2exit_lfps_quirk");
1009 dwc->u2ss_inp3_quirk = dev_read_bool(dev,
1010 "snps,u2ss_inp3_quirk");
1011 dwc->req_p1p2p3_quirk = dev_read_bool(dev,
1012 "snps,req_p1p2p3_quirk");
1013 dwc->del_p1p2p3_quirk = dev_read_bool(dev,
1014 "snps,del_p1p2p3_quirk");
1015 dwc->del_phy_power_chg_quirk = dev_read_bool(dev,
1016 "snps,del_phy_power_chg_quirk");
1017 dwc->lfps_filter_quirk = dev_read_bool(dev,
1018 "snps,lfps_filter_quirk");
1019 dwc->rx_detect_poll_quirk = dev_read_bool(dev,
1020 "snps,rx_detect_poll_quirk");
1021 dwc->dis_u3_susphy_quirk = dev_read_bool(dev,
1022 "snps,dis_u3_susphy_quirk");
1023 dwc->dis_u2_susphy_quirk = dev_read_bool(dev,
1024 "snps,dis_u2_susphy_quirk");
Jagan Tekic1157dc2020-05-06 13:20:25 +05301025 dwc->dis_del_phy_power_chg_quirk = dev_read_bool(dev,
1026 "snps,dis-del-phy-power-chg-quirk");
Jagan Teki0ece2f72020-05-26 11:33:48 +08001027 dwc->dis_tx_ipgap_linecheck_quirk = dev_read_bool(dev,
1028 "snps,dis-tx-ipgap-linecheck-quirk");
Frank Wang0c3b6f52020-05-26 11:33:46 +08001029 dwc->dis_enblslpm_quirk = dev_read_bool(dev,
1030 "snps,dis_enblslpm_quirk");
Frank Wangb29bcd72020-05-26 11:33:47 +08001031 dwc->dis_u2_freeclk_exists_quirk = dev_read_bool(dev,
1032 "snps,dis-u2-freeclk-exists-quirk");
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +02001033 dwc->tx_de_emphasis_quirk = dev_read_bool(dev,
1034 "snps,tx_de_emphasis_quirk");
1035 tmp = dev_read_u8_array_ptr(dev, "snps,tx_de_emphasis", 1);
1036 if (tmp)
1037 tx_de_emphasis = *tmp;
1038
1039 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1040 dwc->tx_de_emphasis = tx_de_emphasis;
1041
1042 dwc->hird_threshold = hird_threshold
1043 | (dwc->is_utmi_l1_suspend << 4);
Michael Walle679f13c2021-10-15 15:15:21 +02001044
1045 dev_read_u32(dev, "snps,quirk-frame-length-adjustment", &dwc->fladj);
Michael Walle668bdf62021-10-15 15:15:22 +02001046
1047 /*
1048 * Handle property "snps,incr-burst-type-adjustment".
1049 * Get the number of value from this property:
1050 * result <= 0, means this property is not supported.
1051 * result = 1, means INCRx burst mode supported.
1052 * result > 1, means undefined length burst mode supported.
1053 */
1054 dwc->incrx_mode = INCRX_BURST_MODE;
1055 dwc->incrx_size = 0;
1056 for (i = 0; i < 8; i++) {
1057 if (dev_read_u32_index(dev, "snps,incr-burst-type-adjustment",
1058 i, &val))
1059 break;
1060
1061 dwc->incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
1062 dwc->incrx_size = max(dwc->incrx_size, val);
1063 }
Jean-Jacques Hiblotce868d02019-09-11 11:33:52 +02001064}
1065
Mugunthan V N5f7ff712018-05-18 13:15:04 +02001066int dwc3_init(struct dwc3 *dwc)
1067{
1068 int ret;
Jagan Teki0ece2f72020-05-26 11:33:48 +08001069 u32 reg;
Mugunthan V N5f7ff712018-05-18 13:15:04 +02001070
1071 dwc3_cache_hwparams(dwc);
1072
1073 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1074 if (ret) {
1075 dev_err(dwc->dev, "failed to allocate event buffers\n");
1076 return -ENOMEM;
1077 }
1078
1079 ret = dwc3_core_init(dwc);
1080 if (ret) {
Sean Anderson2789ce82020-09-15 10:45:16 -04001081 dev_err(dwc->dev, "failed to initialize core\n");
Mugunthan V N5f7ff712018-05-18 13:15:04 +02001082 goto core_fail;
1083 }
1084
1085 ret = dwc3_event_buffers_setup(dwc);
1086 if (ret) {
1087 dev_err(dwc->dev, "failed to setup event buffers\n");
1088 goto event_fail;
1089 }
1090
Jagan Teki0ece2f72020-05-26 11:33:48 +08001091 if (dwc->revision >= DWC3_REVISION_250A) {
1092 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1093
1094 /*
1095 * Enable hardware control of sending remote wakeup
1096 * in HS when the device is in the L1 state.
1097 */
1098 if (dwc->revision >= DWC3_REVISION_290A)
1099 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1100
1101 if (dwc->dis_tx_ipgap_linecheck_quirk)
1102 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1103
1104 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1105 }
1106
Jagan Teki461409b2020-05-26 11:34:29 +08001107 if (dwc->dr_mode == USB_DR_MODE_HOST ||
1108 dwc->dr_mode == USB_DR_MODE_OTG) {
1109 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
1110
1111 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1112
1113 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1114 }
1115
Mugunthan V N5f7ff712018-05-18 13:15:04 +02001116 ret = dwc3_core_init_mode(dwc);
1117 if (ret)
1118 goto mode_fail;
1119
1120 return 0;
1121
1122mode_fail:
1123 dwc3_event_buffers_cleanup(dwc);
1124
1125event_fail:
1126 dwc3_core_exit(dwc);
1127
1128core_fail:
1129 dwc3_free_event_buffers(dwc);
1130
1131 return ret;
1132}
1133
1134void dwc3_remove(struct dwc3 *dwc)
1135{
1136 dwc3_core_exit_mode(dwc);
1137 dwc3_event_buffers_cleanup(dwc);
1138 dwc3_free_event_buffers(dwc);
Angus Ainslieeefabee2022-02-02 15:08:55 -08001139 dwc3_core_stop(dwc);
Mugunthan V N5f7ff712018-05-18 13:15:04 +02001140 dwc3_core_exit(dwc);
1141 kfree(dwc->mem);
1142}
Mugunthan V N5f7ff712018-05-18 13:15:04 +02001143#endif