usb: dwc3: add dis_del_phy_power_chg_quirk
[oweals/u-boot.git] / drivers / usb / dwc3 / core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3  * core.c - DesignWare USB3 DRD Controller Core file
4  *
5  * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
6  *
7  * Authors: Felipe Balbi <balbi@ti.com>,
8  *          Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9  *
10  * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/core.c) and ported
11  * to uboot.
12  *
13  * commit cd72f890d2 : usb: dwc3: core: enable phy suspend quirk on non-FPGA
14  */
15
16 #include <common.h>
17 #include <cpu_func.h>
18 #include <malloc.h>
19 #include <dwc3-uboot.h>
20 #include <dm/device_compat.h>
21 #include <dm/devres.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/err.h>
24 #include <linux/ioport.h>
25 #include <dm.h>
26 #include <generic-phy.h>
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
29
30 #include "core.h"
31 #include "gadget.h"
32 #include "io.h"
33
34 #include "linux-compat.h"
35
36 static LIST_HEAD(dwc3_list);
37 /* -------------------------------------------------------------------------- */
38
39 static void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
40 {
41         u32 reg;
42
43         reg = dwc3_readl(dwc->regs, DWC3_GCTL);
44         reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
45         reg |= DWC3_GCTL_PRTCAPDIR(mode);
46         dwc3_writel(dwc->regs, DWC3_GCTL, reg);
47 }
48
49 /**
50  * dwc3_core_soft_reset - Issues core soft reset and PHY reset
51  * @dwc: pointer to our context structure
52  */
53 static int dwc3_core_soft_reset(struct dwc3 *dwc)
54 {
55         u32             reg;
56
57         /* Before Resetting PHY, put Core in Reset */
58         reg = dwc3_readl(dwc->regs, DWC3_GCTL);
59         reg |= DWC3_GCTL_CORESOFTRESET;
60         dwc3_writel(dwc->regs, DWC3_GCTL, reg);
61
62         /* Assert USB3 PHY reset */
63         reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
64         reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
65         dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
66
67         /* Assert USB2 PHY reset */
68         reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
69         reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
70         dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
71
72         mdelay(100);
73
74         /* Clear USB3 PHY reset */
75         reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
76         reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
77         dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
78
79         /* Clear USB2 PHY reset */
80         reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
81         reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
82         dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
83
84         mdelay(100);
85
86         /* After PHYs are stable we can take Core out of reset state */
87         reg = dwc3_readl(dwc->regs, DWC3_GCTL);
88         reg &= ~DWC3_GCTL_CORESOFTRESET;
89         dwc3_writel(dwc->regs, DWC3_GCTL, reg);
90
91         return 0;
92 }
93
94 /**
95  * dwc3_free_one_event_buffer - Frees one event buffer
96  * @dwc: Pointer to our controller context structure
97  * @evt: Pointer to event buffer to be freed
98  */
99 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
100                 struct dwc3_event_buffer *evt)
101 {
102         dma_free_coherent(evt->buf);
103 }
104
105 /**
106  * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
107  * @dwc: Pointer to our controller context structure
108  * @length: size of the event buffer
109  *
110  * Returns a pointer to the allocated event buffer structure on success
111  * otherwise ERR_PTR(errno).
112  */
113 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
114                 unsigned length)
115 {
116         struct dwc3_event_buffer        *evt;
117
118         evt = devm_kzalloc((struct udevice *)dwc->dev, sizeof(*evt),
119                            GFP_KERNEL);
120         if (!evt)
121                 return ERR_PTR(-ENOMEM);
122
123         evt->dwc        = dwc;
124         evt->length     = length;
125         evt->buf        = dma_alloc_coherent(length,
126                                              (unsigned long *)&evt->dma);
127         if (!evt->buf)
128                 return ERR_PTR(-ENOMEM);
129
130         dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
131
132         return evt;
133 }
134
135 /**
136  * dwc3_free_event_buffers - frees all allocated event buffers
137  * @dwc: Pointer to our controller context structure
138  */
139 static void dwc3_free_event_buffers(struct dwc3 *dwc)
140 {
141         struct dwc3_event_buffer        *evt;
142         int i;
143
144         for (i = 0; i < dwc->num_event_buffers; i++) {
145                 evt = dwc->ev_buffs[i];
146                 if (evt)
147                         dwc3_free_one_event_buffer(dwc, evt);
148         }
149 }
150
151 /**
152  * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
153  * @dwc: pointer to our controller context structure
154  * @length: size of event buffer
155  *
156  * Returns 0 on success otherwise negative errno. In the error case, dwc
157  * may contain some buffers allocated but not all which were requested.
158  */
159 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
160 {
161         int                     num;
162         int                     i;
163
164         num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
165         dwc->num_event_buffers = num;
166
167         dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE,
168                                  sizeof(*dwc->ev_buffs) * num);
169         if (!dwc->ev_buffs)
170                 return -ENOMEM;
171
172         for (i = 0; i < num; i++) {
173                 struct dwc3_event_buffer        *evt;
174
175                 evt = dwc3_alloc_one_event_buffer(dwc, length);
176                 if (IS_ERR(evt)) {
177                         dev_err(dwc->dev, "can't allocate event buffer\n");
178                         return PTR_ERR(evt);
179                 }
180                 dwc->ev_buffs[i] = evt;
181         }
182
183         return 0;
184 }
185
186 /**
187  * dwc3_event_buffers_setup - setup our allocated event buffers
188  * @dwc: pointer to our controller context structure
189  *
190  * Returns 0 on success otherwise negative errno.
191  */
192 static int dwc3_event_buffers_setup(struct dwc3 *dwc)
193 {
194         struct dwc3_event_buffer        *evt;
195         int                             n;
196
197         for (n = 0; n < dwc->num_event_buffers; n++) {
198                 evt = dwc->ev_buffs[n];
199                 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
200                                 evt->buf, (unsigned long long) evt->dma,
201                                 evt->length);
202
203                 evt->lpos = 0;
204
205                 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
206                                 lower_32_bits(evt->dma));
207                 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
208                                 upper_32_bits(evt->dma));
209                 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
210                                 DWC3_GEVNTSIZ_SIZE(evt->length));
211                 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
212         }
213
214         return 0;
215 }
216
217 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
218 {
219         struct dwc3_event_buffer        *evt;
220         int                             n;
221
222         for (n = 0; n < dwc->num_event_buffers; n++) {
223                 evt = dwc->ev_buffs[n];
224
225                 evt->lpos = 0;
226
227                 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
228                 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
229                 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
230                                 | DWC3_GEVNTSIZ_SIZE(0));
231                 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
232         }
233 }
234
235 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
236 {
237         if (!dwc->has_hibernation)
238                 return 0;
239
240         if (!dwc->nr_scratch)
241                 return 0;
242
243         dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
244                         DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
245         if (!dwc->scratchbuf)
246                 return -ENOMEM;
247
248         return 0;
249 }
250
251 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
252 {
253         dma_addr_t scratch_addr;
254         u32 param;
255         int ret;
256
257         if (!dwc->has_hibernation)
258                 return 0;
259
260         if (!dwc->nr_scratch)
261                 return 0;
262
263         scratch_addr = dma_map_single(dwc->scratchbuf,
264                                       dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
265                                       DMA_BIDIRECTIONAL);
266         if (dma_mapping_error(dwc->dev, scratch_addr)) {
267                 dev_err(dwc->dev, "failed to map scratch buffer\n");
268                 ret = -EFAULT;
269                 goto err0;
270         }
271
272         dwc->scratch_addr = scratch_addr;
273
274         param = lower_32_bits(scratch_addr);
275
276         ret = dwc3_send_gadget_generic_command(dwc,
277                         DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
278         if (ret < 0)
279                 goto err1;
280
281         param = upper_32_bits(scratch_addr);
282
283         ret = dwc3_send_gadget_generic_command(dwc,
284                         DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
285         if (ret < 0)
286                 goto err1;
287
288         return 0;
289
290 err1:
291         dma_unmap_single(scratch_addr, dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
292                          DMA_BIDIRECTIONAL);
293
294 err0:
295         return ret;
296 }
297
298 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
299 {
300         if (!dwc->has_hibernation)
301                 return;
302
303         if (!dwc->nr_scratch)
304                 return;
305
306         dma_unmap_single(dwc->scratch_addr, dwc->nr_scratch *
307                          DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
308         kfree(dwc->scratchbuf);
309 }
310
311 static void dwc3_core_num_eps(struct dwc3 *dwc)
312 {
313         struct dwc3_hwparams    *parms = &dwc->hwparams;
314
315         dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
316         dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
317
318         dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
319                         dwc->num_in_eps, dwc->num_out_eps);
320 }
321
322 static void dwc3_cache_hwparams(struct dwc3 *dwc)
323 {
324         struct dwc3_hwparams    *parms = &dwc->hwparams;
325
326         parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
327         parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
328         parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
329         parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
330         parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
331         parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
332         parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
333         parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
334         parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
335 }
336
337 /**
338  * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
339  * @dwc: Pointer to our controller context structure
340  */
341 static void dwc3_phy_setup(struct dwc3 *dwc)
342 {
343         u32 reg;
344
345         reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
346
347         /*
348          * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
349          * to '0' during coreConsultant configuration. So default value
350          * will be '0' when the core is reset. Application needs to set it
351          * to '1' after the core initialization is completed.
352          */
353         if (dwc->revision > DWC3_REVISION_194A)
354                 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
355
356         if (dwc->u2ss_inp3_quirk)
357                 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
358
359         if (dwc->req_p1p2p3_quirk)
360                 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
361
362         if (dwc->del_p1p2p3_quirk)
363                 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
364
365         if (dwc->del_phy_power_chg_quirk)
366                 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
367
368         if (dwc->lfps_filter_quirk)
369                 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
370
371         if (dwc->rx_detect_poll_quirk)
372                 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
373
374         if (dwc->tx_de_emphasis_quirk)
375                 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
376
377         if (dwc->dis_u3_susphy_quirk)
378                 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
379
380         if (dwc->dis_del_phy_power_chg_quirk)
381                 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
382
383         dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
384
385         mdelay(100);
386
387         reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
388
389         /*
390          * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
391          * '0' during coreConsultant configuration. So default value will
392          * be '0' when the core is reset. Application needs to set it to
393          * '1' after the core initialization is completed.
394          */
395         if (dwc->revision > DWC3_REVISION_194A)
396                 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
397
398         if (dwc->dis_u2_susphy_quirk)
399                 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
400
401         dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
402
403         mdelay(100);
404 }
405
406 /**
407  * dwc3_core_init - Low-level initialization of DWC3 Core
408  * @dwc: Pointer to our controller context structure
409  *
410  * Returns 0 on success otherwise negative errno.
411  */
412 static int dwc3_core_init(struct dwc3 *dwc)
413 {
414         unsigned long           timeout;
415         u32                     hwparams4 = dwc->hwparams.hwparams4;
416         u32                     reg;
417         int                     ret;
418
419         reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
420         /* This should read as U3 followed by revision number */
421         if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
422                 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
423                 ret = -ENODEV;
424                 goto err0;
425         }
426         dwc->revision = reg;
427
428         /* Handle USB2.0-only core configuration */
429         if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
430                         DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
431                 if (dwc->maximum_speed == USB_SPEED_SUPER)
432                         dwc->maximum_speed = USB_SPEED_HIGH;
433         }
434
435         /* issue device SoftReset too */
436         timeout = 5000;
437         dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
438         while (timeout--) {
439                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
440                 if (!(reg & DWC3_DCTL_CSFTRST))
441                         break;
442         };
443
444         if (!timeout) {
445                 dev_err(dwc->dev, "Reset Timed Out\n");
446                 ret = -ETIMEDOUT;
447                 goto err0;
448         }
449
450         dwc3_phy_setup(dwc);
451
452         ret = dwc3_core_soft_reset(dwc);
453         if (ret)
454                 goto err0;
455
456         reg = dwc3_readl(dwc->regs, DWC3_GCTL);
457         reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
458
459         switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
460         case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
461                 /**
462                  * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
463                  * issue which would cause xHCI compliance tests to fail.
464                  *
465                  * Because of that we cannot enable clock gating on such
466                  * configurations.
467                  *
468                  * Refers to:
469                  *
470                  * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
471                  * SOF/ITP Mode Used
472                  */
473                 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
474                                 dwc->dr_mode == USB_DR_MODE_OTG) &&
475                                 (dwc->revision >= DWC3_REVISION_210A &&
476                                 dwc->revision <= DWC3_REVISION_250A))
477                         reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
478                 else
479                         reg &= ~DWC3_GCTL_DSBLCLKGTNG;
480                 break;
481         case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
482                 /* enable hibernation here */
483                 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
484
485                 /*
486                  * REVISIT Enabling this bit so that host-mode hibernation
487                  * will work. Device-mode hibernation is not yet implemented.
488                  */
489                 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
490                 break;
491         default:
492                 dev_dbg(dwc->dev, "No power optimization available\n");
493         }
494
495         /* check if current dwc3 is on simulation board */
496         if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
497                 dev_dbg(dwc->dev, "it is on FPGA board\n");
498                 dwc->is_fpga = true;
499         }
500
501         if(dwc->disable_scramble_quirk && !dwc->is_fpga)
502                 WARN(true,
503                      "disable_scramble cannot be used on non-FPGA builds\n");
504
505         if (dwc->disable_scramble_quirk && dwc->is_fpga)
506                 reg |= DWC3_GCTL_DISSCRAMBLE;
507         else
508                 reg &= ~DWC3_GCTL_DISSCRAMBLE;
509
510         if (dwc->u2exit_lfps_quirk)
511                 reg |= DWC3_GCTL_U2EXIT_LFPS;
512
513         /*
514          * WORKAROUND: DWC3 revisions <1.90a have a bug
515          * where the device can fail to connect at SuperSpeed
516          * and falls back to high-speed mode which causes
517          * the device to enter a Connect/Disconnect loop
518          */
519         if (dwc->revision < DWC3_REVISION_190A)
520                 reg |= DWC3_GCTL_U2RSTECN;
521
522         dwc3_core_num_eps(dwc);
523
524         dwc3_writel(dwc->regs, DWC3_GCTL, reg);
525
526         ret = dwc3_alloc_scratch_buffers(dwc);
527         if (ret)
528                 goto err0;
529
530         ret = dwc3_setup_scratch_buffers(dwc);
531         if (ret)
532                 goto err1;
533
534         return 0;
535
536 err1:
537         dwc3_free_scratch_buffers(dwc);
538
539 err0:
540         return ret;
541 }
542
543 static void dwc3_core_exit(struct dwc3 *dwc)
544 {
545         dwc3_free_scratch_buffers(dwc);
546 }
547
548 static int dwc3_core_init_mode(struct dwc3 *dwc)
549 {
550         int ret;
551
552         switch (dwc->dr_mode) {
553         case USB_DR_MODE_PERIPHERAL:
554                 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
555                 ret = dwc3_gadget_init(dwc);
556                 if (ret) {
557                         dev_err(dev, "failed to initialize gadget\n");
558                         return ret;
559                 }
560                 break;
561         case USB_DR_MODE_HOST:
562                 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
563                 ret = dwc3_host_init(dwc);
564                 if (ret) {
565                         dev_err(dev, "failed to initialize host\n");
566                         return ret;
567                 }
568                 break;
569         case USB_DR_MODE_OTG:
570                 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
571                 ret = dwc3_host_init(dwc);
572                 if (ret) {
573                         dev_err(dev, "failed to initialize host\n");
574                         return ret;
575                 }
576
577                 ret = dwc3_gadget_init(dwc);
578                 if (ret) {
579                         dev_err(dev, "failed to initialize gadget\n");
580                         return ret;
581                 }
582                 break;
583         default:
584                 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
585                 return -EINVAL;
586         }
587
588         return 0;
589 }
590
591 static void dwc3_gadget_run(struct dwc3 *dwc)
592 {
593         dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_RUN_STOP);
594         mdelay(100);
595 }
596
597 static void dwc3_core_exit_mode(struct dwc3 *dwc)
598 {
599         switch (dwc->dr_mode) {
600         case USB_DR_MODE_PERIPHERAL:
601                 dwc3_gadget_exit(dwc);
602                 break;
603         case USB_DR_MODE_HOST:
604                 dwc3_host_exit(dwc);
605                 break;
606         case USB_DR_MODE_OTG:
607                 dwc3_host_exit(dwc);
608                 dwc3_gadget_exit(dwc);
609                 break;
610         default:
611                 /* do nothing */
612                 break;
613         }
614
615         /*
616          * switch back to peripheral mode
617          * This enables the phy to enter idle and then, if enabled, suspend.
618          */
619         dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
620         dwc3_gadget_run(dwc);
621 }
622
623 static void dwc3_uboot_hsphy_mode(struct dwc3_device *dwc3_dev,
624                                   struct dwc3 *dwc)
625 {
626         enum usb_phy_interface hsphy_mode = dwc3_dev->hsphy_mode;
627         u32 reg;
628
629         /* Set dwc3 usb2 phy config */
630         reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
631
632         switch (hsphy_mode) {
633         case USBPHY_INTERFACE_MODE_UTMI:
634                 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
635                         DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
636                 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
637                         DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
638                 break;
639         case USBPHY_INTERFACE_MODE_UTMIW:
640                 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
641                         DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
642                 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
643                         DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
644                 break;
645         default:
646                 break;
647         }
648
649         dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
650 }
651
652 #define DWC3_ALIGN_MASK         (16 - 1)
653
654 /**
655  * dwc3_uboot_init - dwc3 core uboot initialization code
656  * @dwc3_dev: struct dwc3_device containing initialization data
657  *
658  * Entry point for dwc3 driver (equivalent to dwc3_probe in linux
659  * kernel driver). Pointer to dwc3_device should be passed containing
660  * base address and other initialization data. Returns '0' on success and
661  * a negative value on failure.
662  *
663  * Generally called from board_usb_init() implemented in board file.
664  */
665 int dwc3_uboot_init(struct dwc3_device *dwc3_dev)
666 {
667         struct dwc3             *dwc;
668         struct device           *dev = NULL;
669         u8                      lpm_nyet_threshold;
670         u8                      tx_de_emphasis;
671         u8                      hird_threshold;
672
673         int                     ret;
674
675         void                    *mem;
676
677         mem = devm_kzalloc((struct udevice *)dev,
678                            sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
679         if (!mem)
680                 return -ENOMEM;
681
682         dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
683         dwc->mem = mem;
684
685         dwc->regs = (void *)(uintptr_t)(dwc3_dev->base +
686                                         DWC3_GLOBALS_REGS_START);
687
688         /* default to highest possible threshold */
689         lpm_nyet_threshold = 0xff;
690
691         /* default to -3.5dB de-emphasis */
692         tx_de_emphasis = 1;
693
694         /*
695          * default to assert utmi_sleep_n and use maximum allowed HIRD
696          * threshold value of 0b1100
697          */
698         hird_threshold = 12;
699
700         dwc->maximum_speed = dwc3_dev->maximum_speed;
701         dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum;
702         if (dwc3_dev->lpm_nyet_threshold)
703                 lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold;
704         dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend;
705         if (dwc3_dev->hird_threshold)
706                 hird_threshold = dwc3_dev->hird_threshold;
707
708         dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize;
709         dwc->dr_mode = dwc3_dev->dr_mode;
710
711         dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk;
712         dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk;
713         dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk;
714         dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk;
715         dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk;
716         dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk;
717         dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk;
718         dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk;
719         dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk;
720         dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk;
721         dwc->dis_del_phy_power_chg_quirk = dwc3_dev->dis_del_phy_power_chg_quirk;
722
723         dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk;
724         if (dwc3_dev->tx_de_emphasis)
725                 tx_de_emphasis = dwc3_dev->tx_de_emphasis;
726
727         /* default to superspeed if no maximum_speed passed */
728         if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
729                 dwc->maximum_speed = USB_SPEED_SUPER;
730
731         dwc->lpm_nyet_threshold = lpm_nyet_threshold;
732         dwc->tx_de_emphasis = tx_de_emphasis;
733
734         dwc->hird_threshold = hird_threshold
735                 | (dwc->is_utmi_l1_suspend << 4);
736
737         dwc->index = dwc3_dev->index;
738
739         dwc3_cache_hwparams(dwc);
740
741         ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
742         if (ret) {
743                 dev_err(dwc->dev, "failed to allocate event buffers\n");
744                 return -ENOMEM;
745         }
746
747         if (!IS_ENABLED(CONFIG_USB_DWC3_GADGET))
748                 dwc->dr_mode = USB_DR_MODE_HOST;
749         else if (!IS_ENABLED(CONFIG_USB_HOST))
750                 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
751
752         if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
753                 dwc->dr_mode = USB_DR_MODE_OTG;
754
755         ret = dwc3_core_init(dwc);
756         if (ret) {
757                 dev_err(dev, "failed to initialize core\n");
758                 goto err0;
759         }
760
761         dwc3_uboot_hsphy_mode(dwc3_dev, dwc);
762
763         ret = dwc3_event_buffers_setup(dwc);
764         if (ret) {
765                 dev_err(dwc->dev, "failed to setup event buffers\n");
766                 goto err1;
767         }
768
769         ret = dwc3_core_init_mode(dwc);
770         if (ret)
771                 goto err2;
772
773         list_add_tail(&dwc->list, &dwc3_list);
774
775         return 0;
776
777 err2:
778         dwc3_event_buffers_cleanup(dwc);
779
780 err1:
781         dwc3_core_exit(dwc);
782
783 err0:
784         dwc3_free_event_buffers(dwc);
785
786         return ret;
787 }
788
789 /**
790  * dwc3_uboot_exit - dwc3 core uboot cleanup code
791  * @index: index of this controller
792  *
793  * Performs cleanup of memory allocated in dwc3_uboot_init and other misc
794  * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller
795  * should be passed and should match with the index passed in
796  * dwc3_device during init.
797  *
798  * Generally called from board file.
799  */
800 void dwc3_uboot_exit(int index)
801 {
802         struct dwc3 *dwc;
803
804         list_for_each_entry(dwc, &dwc3_list, list) {
805                 if (dwc->index != index)
806                         continue;
807
808                 dwc3_core_exit_mode(dwc);
809                 dwc3_event_buffers_cleanup(dwc);
810                 dwc3_free_event_buffers(dwc);
811                 dwc3_core_exit(dwc);
812                 list_del(&dwc->list);
813                 kfree(dwc->mem);
814                 break;
815         }
816 }
817
818 /**
819  * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt
820  * @index: index of this controller
821  *
822  * Invokes dwc3 gadget interrupts.
823  *
824  * Generally called from board file.
825  */
826 void dwc3_uboot_handle_interrupt(int index)
827 {
828         struct dwc3 *dwc = NULL;
829
830         list_for_each_entry(dwc, &dwc3_list, list) {
831                 if (dwc->index != index)
832                         continue;
833
834                 dwc3_gadget_uboot_handle_interrupt(dwc);
835                 break;
836         }
837 }
838
839 MODULE_ALIAS("platform:dwc3");
840 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
841 MODULE_LICENSE("GPL v2");
842 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
843
844 #if CONFIG_IS_ENABLED(PHY) && CONFIG_IS_ENABLED(DM_USB)
845 int dwc3_setup_phy(struct udevice *dev, struct phy_bulk *phys)
846 {
847         int ret;
848
849         ret = generic_phy_get_bulk(dev, phys);
850         if (ret)
851                 return ret;
852
853         ret = generic_phy_init_bulk(phys);
854         if (ret)
855                 return ret;
856
857         ret = generic_phy_power_on_bulk(phys);
858         if (ret)
859                 generic_phy_exit_bulk(phys);
860
861         return ret;
862 }
863
864 int dwc3_shutdown_phy(struct udevice *dev, struct phy_bulk *phys)
865 {
866         int ret;
867
868         ret = generic_phy_power_off_bulk(phys);
869         ret |= generic_phy_exit_bulk(phys);
870         return ret;
871 }
872 #endif
873
874 #if CONFIG_IS_ENABLED(DM_USB)
875 void dwc3_of_parse(struct dwc3 *dwc)
876 {
877         const u8 *tmp;
878         struct udevice *dev = dwc->dev;
879         u8 lpm_nyet_threshold;
880         u8 tx_de_emphasis;
881         u8 hird_threshold;
882
883         /* default to highest possible threshold */
884         lpm_nyet_threshold = 0xff;
885
886         /* default to -3.5dB de-emphasis */
887         tx_de_emphasis = 1;
888
889         /*
890          * default to assert utmi_sleep_n and use maximum allowed HIRD
891          * threshold value of 0b1100
892          */
893         hird_threshold = 12;
894
895         dwc->has_lpm_erratum = dev_read_bool(dev,
896                                 "snps,has-lpm-erratum");
897         tmp = dev_read_u8_array_ptr(dev, "snps,lpm-nyet-threshold", 1);
898         if (tmp)
899                 lpm_nyet_threshold = *tmp;
900
901         dwc->is_utmi_l1_suspend = dev_read_bool(dev,
902                                 "snps,is-utmi-l1-suspend");
903         tmp = dev_read_u8_array_ptr(dev, "snps,hird-threshold", 1);
904         if (tmp)
905                 hird_threshold = *tmp;
906
907         dwc->disable_scramble_quirk = dev_read_bool(dev,
908                                 "snps,disable_scramble_quirk");
909         dwc->u2exit_lfps_quirk = dev_read_bool(dev,
910                                 "snps,u2exit_lfps_quirk");
911         dwc->u2ss_inp3_quirk = dev_read_bool(dev,
912                                 "snps,u2ss_inp3_quirk");
913         dwc->req_p1p2p3_quirk = dev_read_bool(dev,
914                                 "snps,req_p1p2p3_quirk");
915         dwc->del_p1p2p3_quirk = dev_read_bool(dev,
916                                 "snps,del_p1p2p3_quirk");
917         dwc->del_phy_power_chg_quirk = dev_read_bool(dev,
918                                 "snps,del_phy_power_chg_quirk");
919         dwc->lfps_filter_quirk = dev_read_bool(dev,
920                                 "snps,lfps_filter_quirk");
921         dwc->rx_detect_poll_quirk = dev_read_bool(dev,
922                                 "snps,rx_detect_poll_quirk");
923         dwc->dis_u3_susphy_quirk = dev_read_bool(dev,
924                                 "snps,dis_u3_susphy_quirk");
925         dwc->dis_u2_susphy_quirk = dev_read_bool(dev,
926                                 "snps,dis_u2_susphy_quirk");
927         dwc->dis_del_phy_power_chg_quirk = dev_read_bool(dev,
928                                 "snps,dis-del-phy-power-chg-quirk");
929         dwc->tx_de_emphasis_quirk = dev_read_bool(dev,
930                                 "snps,tx_de_emphasis_quirk");
931         tmp = dev_read_u8_array_ptr(dev, "snps,tx_de_emphasis", 1);
932         if (tmp)
933                 tx_de_emphasis = *tmp;
934
935         dwc->lpm_nyet_threshold = lpm_nyet_threshold;
936         dwc->tx_de_emphasis = tx_de_emphasis;
937
938         dwc->hird_threshold = hird_threshold
939                 | (dwc->is_utmi_l1_suspend << 4);
940 }
941
942 int dwc3_init(struct dwc3 *dwc)
943 {
944         int ret;
945
946         dwc3_cache_hwparams(dwc);
947
948         ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
949         if (ret) {
950                 dev_err(dwc->dev, "failed to allocate event buffers\n");
951                 return -ENOMEM;
952         }
953
954         ret = dwc3_core_init(dwc);
955         if (ret) {
956                 dev_err(dev, "failed to initialize core\n");
957                 goto core_fail;
958         }
959
960         ret = dwc3_event_buffers_setup(dwc);
961         if (ret) {
962                 dev_err(dwc->dev, "failed to setup event buffers\n");
963                 goto event_fail;
964         }
965
966         ret = dwc3_core_init_mode(dwc);
967         if (ret)
968                 goto mode_fail;
969
970         return 0;
971
972 mode_fail:
973         dwc3_event_buffers_cleanup(dwc);
974
975 event_fail:
976         dwc3_core_exit(dwc);
977
978 core_fail:
979         dwc3_free_event_buffers(dwc);
980
981         return ret;
982 }
983
984 void dwc3_remove(struct dwc3 *dwc)
985 {
986         dwc3_core_exit_mode(dwc);
987         dwc3_event_buffers_cleanup(dwc);
988         dwc3_free_event_buffers(dwc);
989         dwc3_core_exit(dwc);
990         kfree(dwc->mem);
991 }
992 #endif