blob: 6ff450cc758ea95960c8b1dd56bc34a0461bf387 [file] [log] [blame]
Ramneek Mehreshba92ee02015-05-29 14:47:19 +05301/*
Rajesh Bhagat707c8662016-07-01 18:51:47 +05302 * Copyright 2015,2016 Freescale Semiconductor, Inc.
Ramneek Mehreshba92ee02015-05-29 14:47:19 +05303 *
4 * FSL USB HOST xHCI Controller
5 *
6 * Author: Ramneek Mehresh<ramneek.mehresh@freescale.com>
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <common.h>
12#include <usb.h>
13#include <asm-generic/errno.h>
Ramneek Mehreshba92ee02015-05-29 14:47:19 +053014#include <linux/compat.h>
15#include <linux/usb/xhci-fsl.h>
16#include <linux/usb/dwc3.h>
17#include "xhci.h"
Sriram Dashef53b8c2016-06-13 09:58:36 +053018#include <fsl_errata.h>
19#include <fsl_usb.h>
Rajesh Bhagat707c8662016-07-01 18:51:47 +053020#include <dm.h>
Ramneek Mehreshba92ee02015-05-29 14:47:19 +053021
22/* Declare global data pointer */
23DECLARE_GLOBAL_DATA_PTR;
24
Rajesh Bhagat707c8662016-07-01 18:51:47 +053025#ifndef CONFIG_DM_USB
Ramneek Mehreshba92ee02015-05-29 14:47:19 +053026static struct fsl_xhci fsl_xhci;
27unsigned long ctr_addr[] = FSL_USB_XHCI_ADDR;
Rajesh Bhagat707c8662016-07-01 18:51:47 +053028#else
29struct xhci_fsl_priv {
30 struct xhci_ctrl xhci;
31 fdt_addr_t hcd_base;
32 struct fsl_xhci ctx;
33};
34#endif
Ramneek Mehreshba92ee02015-05-29 14:47:19 +053035
36__weak int __board_usb_init(int index, enum usb_init_type init)
37{
38 return 0;
39}
40
Sriram Dashef53b8c2016-06-13 09:58:36 +053041static int erratum_a008751(void)
42{
43#if defined(CONFIG_TARGET_LS2080AQDS) || defined(CONFIG_TARGET_LS2080ARDB)
44 u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE;
45 writel(SCFG_USB3PRM1CR_INIT, scfg + SCFG_USB3PRM1CR / 4);
46 return 0;
47#endif
48 return 1;
49}
50
51static void fsl_apply_xhci_errata(void)
52{
53 int ret;
54 if (has_erratum_a008751()) {
55 ret = erratum_a008751();
56 if (ret != 0)
57 puts("Failed to apply erratum a008751\n");
58 }
59}
60
Ramneek Mehreshba92ee02015-05-29 14:47:19 +053061static int fsl_xhci_core_init(struct fsl_xhci *fsl_xhci)
62{
63 int ret = 0;
64
65 ret = dwc3_core_init(fsl_xhci->dwc3_reg);
66 if (ret) {
67 debug("%s:failed to initialize core\n", __func__);
68 return ret;
69 }
70
71 /* We are hard-coding DWC3 core to Host Mode */
72 dwc3_set_mode(fsl_xhci->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
73
Nikhil Badola667f4dd2015-06-23 09:17:49 +053074 /* Set GFLADJ_30MHZ as 20h as per XHCI spec default value */
75 dwc3_set_fladj(fsl_xhci->dwc3_reg, GFLADJ_30MHZ_DEFAULT);
76
Ramneek Mehreshba92ee02015-05-29 14:47:19 +053077 return ret;
78}
79
80static int fsl_xhci_core_exit(struct fsl_xhci *fsl_xhci)
81{
82 /*
83 * Currently fsl socs do not support PHY shutdown from
84 * sw. But this support may be added in future socs.
85 */
86 return 0;
87}
88
Rajesh Bhagat707c8662016-07-01 18:51:47 +053089#ifdef CONFIG_DM_USB
90static int xhci_fsl_probe(struct udevice *dev)
91{
92 struct xhci_fsl_priv *priv = dev_get_priv(dev);
93 struct xhci_hccr *hccr;
94 struct xhci_hcor *hcor;
95
96 int ret = 0;
97
98 /*
99 * Get the base address for XHCI controller from the device node
100 */
101 priv->hcd_base = dev_get_addr(dev);
102 if (priv->hcd_base == FDT_ADDR_T_NONE) {
103 debug("Can't get the XHCI register base address\n");
104 return -ENXIO;
105 }
106 priv->ctx.hcd = (struct xhci_hccr *)priv->hcd_base;
107 priv->ctx.dwc3_reg = (struct dwc3 *)((char *)(priv->hcd_base) +
108 DWC3_REG_OFFSET);
109
110 fsl_apply_xhci_errata();
111
112 ret = fsl_xhci_core_init(&priv->ctx);
113 if (ret < 0) {
114 puts("Failed to initialize xhci\n");
115 return ret;
116 }
117
118 hccr = (struct xhci_hccr *)(priv->ctx.hcd);
119 hcor = (struct xhci_hcor *)((uintptr_t) hccr
120 + HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
121
122 debug("xhci-fsl: init hccr %lx and hcor %lx hc_length %lx\n",
123 (uintptr_t)hccr, (uintptr_t)hcor,
124 (uintptr_t)HC_LENGTH(xhci_readl(&hccr->cr_capbase)));
125
126 return xhci_register(dev, hccr, hcor);
127}
128
129static int xhci_fsl_remove(struct udevice *dev)
130{
131 struct xhci_fsl_priv *priv = dev_get_priv(dev);
Rajesh Bhagat707c8662016-07-01 18:51:47 +0530132
133 fsl_xhci_core_exit(&priv->ctx);
134
Masahiro Yamada8319aeb2016-09-06 22:17:35 +0900135 return xhci_deregister(dev);
Rajesh Bhagat707c8662016-07-01 18:51:47 +0530136}
137
138static const struct udevice_id xhci_usb_ids[] = {
139 { .compatible = "fsl,layerscape-dwc3", },
140 { }
141};
142
143U_BOOT_DRIVER(xhci_fsl) = {
144 .name = "xhci_fsl",
145 .id = UCLASS_USB,
146 .of_match = xhci_usb_ids,
147 .probe = xhci_fsl_probe,
148 .remove = xhci_fsl_remove,
149 .ops = &xhci_usb_ops,
150 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
151 .priv_auto_alloc_size = sizeof(struct xhci_fsl_priv),
152 .flags = DM_FLAG_ALLOC_PRIV_DMA,
153};
154#else
Ramneek Mehreshba92ee02015-05-29 14:47:19 +0530155int xhci_hcd_init(int index, struct xhci_hccr **hccr, struct xhci_hcor **hcor)
156{
157 struct fsl_xhci *ctx = &fsl_xhci;
158 int ret = 0;
159
160 ctx->hcd = (struct xhci_hccr *)ctr_addr[index];
161 ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
162
163 ret = board_usb_init(index, USB_INIT_HOST);
164 if (ret != 0) {
165 puts("Failed to initialize board for USB\n");
166 return ret;
167 }
168
Sriram Dashef53b8c2016-06-13 09:58:36 +0530169 fsl_apply_xhci_errata();
170
Ramneek Mehreshba92ee02015-05-29 14:47:19 +0530171 ret = fsl_xhci_core_init(ctx);
172 if (ret < 0) {
173 puts("Failed to initialize xhci\n");
174 return ret;
175 }
176
177 *hccr = (struct xhci_hccr *)ctx->hcd;
Nikhil Badola7e5a32f2015-06-23 09:17:32 +0530178 *hcor = (struct xhci_hcor *)((uintptr_t) *hccr
Ramneek Mehreshba92ee02015-05-29 14:47:19 +0530179 + HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
180
Nikhil Badola7e5a32f2015-06-23 09:17:32 +0530181 debug("fsl-xhci: init hccr %lx and hcor %lx hc_length %lx\n",
182 (uintptr_t)*hccr, (uintptr_t)*hcor,
183 (uintptr_t)HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
Ramneek Mehreshba92ee02015-05-29 14:47:19 +0530184
185 return ret;
186}
187
188void xhci_hcd_stop(int index)
189{
190 struct fsl_xhci *ctx = &fsl_xhci;
191
192 fsl_xhci_core_exit(ctx);
193}
Rajesh Bhagat707c8662016-07-01 18:51:47 +0530194#endif