1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2015,2016 Freescale Semiconductor, Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * FSL USB HOST xHCI Controller
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Author: Ramneek Mehresh<ramneek.mehresh@freescale.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <common.h>
12*4882a593Smuzhiyun #include <usb.h>
13*4882a593Smuzhiyun #include <linux/errno.h>
14*4882a593Smuzhiyun #include <linux/compat.h>
15*4882a593Smuzhiyun #include <linux/usb/xhci-fsl.h>
16*4882a593Smuzhiyun #include <linux/usb/dwc3.h>
17*4882a593Smuzhiyun #include <usb/xhci.h>
18*4882a593Smuzhiyun #include <fsl_errata.h>
19*4882a593Smuzhiyun #include <fsl_usb.h>
20*4882a593Smuzhiyun #include <dm.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(DM_USB)
23*4882a593Smuzhiyun static struct fsl_xhci fsl_xhci;
24*4882a593Smuzhiyun unsigned long ctr_addr[] = FSL_USB_XHCI_ADDR;
25*4882a593Smuzhiyun #else
26*4882a593Smuzhiyun struct xhci_fsl_priv {
27*4882a593Smuzhiyun struct xhci_ctrl xhci;
28*4882a593Smuzhiyun fdt_addr_t hcd_base;
29*4882a593Smuzhiyun struct fsl_xhci ctx;
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun #endif
32*4882a593Smuzhiyun
__board_usb_init(int index,enum usb_init_type init)33*4882a593Smuzhiyun __weak int __board_usb_init(int index, enum usb_init_type init)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun return 0;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
erratum_a008751(void)38*4882a593Smuzhiyun static int erratum_a008751(void)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun #if defined(CONFIG_TARGET_LS2080AQDS) || defined(CONFIG_TARGET_LS2080ARDB) ||\
41*4882a593Smuzhiyun defined(CONFIG_TARGET_LS2080AQDS)
42*4882a593Smuzhiyun u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE;
43*4882a593Smuzhiyun writel(SCFG_USB3PRM1CR_INIT, scfg + SCFG_USB3PRM1CR / 4);
44*4882a593Smuzhiyun return 0;
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun return 1;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
fsl_apply_xhci_errata(void)49*4882a593Smuzhiyun static void fsl_apply_xhci_errata(void)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun int ret;
52*4882a593Smuzhiyun if (has_erratum_a008751()) {
53*4882a593Smuzhiyun ret = erratum_a008751();
54*4882a593Smuzhiyun if (ret != 0)
55*4882a593Smuzhiyun puts("Failed to apply erratum a008751\n");
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
fsl_xhci_set_beat_burst_length(struct dwc3 * dwc3_reg)59*4882a593Smuzhiyun static void fsl_xhci_set_beat_burst_length(struct dwc3 *dwc3_reg)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun clrsetbits_le32(&dwc3_reg->g_sbuscfg0, USB3_ENABLE_BEAT_BURST_MASK,
62*4882a593Smuzhiyun USB3_ENABLE_BEAT_BURST);
63*4882a593Smuzhiyun setbits_le32(&dwc3_reg->g_sbuscfg1, USB3_SET_BEAT_BURST_LIMIT);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
fsl_xhci_core_init(struct fsl_xhci * fsl_xhci)66*4882a593Smuzhiyun static int fsl_xhci_core_init(struct fsl_xhci *fsl_xhci)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun int ret = 0;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun ret = dwc3_core_init(fsl_xhci->dwc3_reg);
71*4882a593Smuzhiyun if (ret) {
72*4882a593Smuzhiyun debug("%s:failed to initialize core\n", __func__);
73*4882a593Smuzhiyun return ret;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /* We are hard-coding DWC3 core to Host Mode */
77*4882a593Smuzhiyun dwc3_set_mode(fsl_xhci->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* Set GFLADJ_30MHZ as 20h as per XHCI spec default value */
80*4882a593Smuzhiyun dwc3_set_fladj(fsl_xhci->dwc3_reg, GFLADJ_30MHZ_DEFAULT);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* Change beat burst and outstanding pipelined transfers requests */
83*4882a593Smuzhiyun fsl_xhci_set_beat_burst_length(fsl_xhci->dwc3_reg);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * A-010151: The dwc3 phy TSMC 28-nm HPM 0.9/1.8 V does not
87*4882a593Smuzhiyun * reliably support Rx Detect in P3 mode(P3 is the default
88*4882a593Smuzhiyun * setting). Therefore, some USB3.0 devices may not be detected
89*4882a593Smuzhiyun * reliably in Super Speed mode. So, USB controller to configure
90*4882a593Smuzhiyun * USB in P2 mode whenever the Receive Detect feature is required.
91*4882a593Smuzhiyun * whenever the Receive Detect feature is required.
92*4882a593Smuzhiyun */
93*4882a593Smuzhiyun if (has_erratum_a010151())
94*4882a593Smuzhiyun clrsetbits_le32(&fsl_xhci->dwc3_reg->g_usb3pipectl[0],
95*4882a593Smuzhiyun DWC3_GUSB3PIPECTL_DISRXDETP3,
96*4882a593Smuzhiyun DWC3_GUSB3PIPECTL_DISRXDETP3);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun return ret;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
fsl_xhci_core_exit(struct fsl_xhci * fsl_xhci)101*4882a593Smuzhiyun static int fsl_xhci_core_exit(struct fsl_xhci *fsl_xhci)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * Currently fsl socs do not support PHY shutdown from
105*4882a593Smuzhiyun * sw. But this support may be added in future socs.
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun return 0;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(DM_USB)
xhci_fsl_probe(struct udevice * dev)111*4882a593Smuzhiyun static int xhci_fsl_probe(struct udevice *dev)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun struct xhci_fsl_priv *priv = dev_get_priv(dev);
114*4882a593Smuzhiyun struct xhci_hccr *hccr;
115*4882a593Smuzhiyun struct xhci_hcor *hcor;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun int ret = 0;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * Get the base address for XHCI controller from the device node
121*4882a593Smuzhiyun */
122*4882a593Smuzhiyun priv->hcd_base = devfdt_get_addr(dev);
123*4882a593Smuzhiyun if (priv->hcd_base == FDT_ADDR_T_NONE) {
124*4882a593Smuzhiyun debug("Can't get the XHCI register base address\n");
125*4882a593Smuzhiyun return -ENXIO;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun priv->ctx.hcd = (struct xhci_hccr *)priv->hcd_base;
128*4882a593Smuzhiyun priv->ctx.dwc3_reg = (struct dwc3 *)((char *)(priv->hcd_base) +
129*4882a593Smuzhiyun DWC3_REG_OFFSET);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun fsl_apply_xhci_errata();
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun ret = fsl_xhci_core_init(&priv->ctx);
134*4882a593Smuzhiyun if (ret < 0) {
135*4882a593Smuzhiyun puts("Failed to initialize xhci\n");
136*4882a593Smuzhiyun return ret;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun hccr = (struct xhci_hccr *)(priv->ctx.hcd);
140*4882a593Smuzhiyun hcor = (struct xhci_hcor *)((uintptr_t) hccr
141*4882a593Smuzhiyun + HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun debug("xhci-fsl: init hccr %lx and hcor %lx hc_length %lx\n",
144*4882a593Smuzhiyun (uintptr_t)hccr, (uintptr_t)hcor,
145*4882a593Smuzhiyun (uintptr_t)HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun return xhci_register(dev, hccr, hcor);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
xhci_fsl_remove(struct udevice * dev)150*4882a593Smuzhiyun static int xhci_fsl_remove(struct udevice *dev)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun struct xhci_fsl_priv *priv = dev_get_priv(dev);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun fsl_xhci_core_exit(&priv->ctx);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun return xhci_deregister(dev);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun static const struct udevice_id xhci_usb_ids[] = {
160*4882a593Smuzhiyun { .compatible = "fsl,layerscape-dwc3", },
161*4882a593Smuzhiyun { }
162*4882a593Smuzhiyun };
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun U_BOOT_DRIVER(xhci_fsl) = {
165*4882a593Smuzhiyun .name = "xhci_fsl",
166*4882a593Smuzhiyun .id = UCLASS_USB,
167*4882a593Smuzhiyun .of_match = xhci_usb_ids,
168*4882a593Smuzhiyun .probe = xhci_fsl_probe,
169*4882a593Smuzhiyun .remove = xhci_fsl_remove,
170*4882a593Smuzhiyun .ops = &xhci_usb_ops,
171*4882a593Smuzhiyun .platdata_auto_alloc_size = sizeof(struct usb_platdata),
172*4882a593Smuzhiyun .priv_auto_alloc_size = sizeof(struct xhci_fsl_priv),
173*4882a593Smuzhiyun .flags = DM_FLAG_ALLOC_PRIV_DMA,
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun #else
xhci_hcd_init(int index,struct xhci_hccr ** hccr,struct xhci_hcor ** hcor)176*4882a593Smuzhiyun int xhci_hcd_init(int index, struct xhci_hccr **hccr, struct xhci_hcor **hcor)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun struct fsl_xhci *ctx = &fsl_xhci;
179*4882a593Smuzhiyun int ret = 0;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun ctx->hcd = (struct xhci_hccr *)ctr_addr[index];
182*4882a593Smuzhiyun ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun ret = board_usb_init(index, USB_INIT_HOST);
185*4882a593Smuzhiyun if (ret != 0) {
186*4882a593Smuzhiyun puts("Failed to initialize board for USB\n");
187*4882a593Smuzhiyun return ret;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun fsl_apply_xhci_errata();
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun ret = fsl_xhci_core_init(ctx);
193*4882a593Smuzhiyun if (ret < 0) {
194*4882a593Smuzhiyun puts("Failed to initialize xhci\n");
195*4882a593Smuzhiyun return ret;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun *hccr = (struct xhci_hccr *)ctx->hcd;
199*4882a593Smuzhiyun *hcor = (struct xhci_hcor *)((uintptr_t) *hccr
200*4882a593Smuzhiyun + HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun debug("fsl-xhci: init hccr %lx and hcor %lx hc_length %lx\n",
203*4882a593Smuzhiyun (uintptr_t)*hccr, (uintptr_t)*hcor,
204*4882a593Smuzhiyun (uintptr_t)HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun return ret;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
xhci_hcd_stop(int index)209*4882a593Smuzhiyun void xhci_hcd_stop(int index)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun struct fsl_xhci *ctx = &fsl_xhci;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun fsl_xhci_core_exit(ctx);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun #endif
216