1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * SAMSUNG EXYNOS5 USB HOST XHCI Controller
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2012 Samsung Electronics Co.Ltd
5*4882a593Smuzhiyun * Vivek Gautam <gautam.vivek@samsung.com>
6*4882a593Smuzhiyun * Vikas Sajjan <vikas.sajjan@samsung.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * This file is a conglomeration for DWC3-init sequence and further
13*4882a593Smuzhiyun * exynos5 specific PHY-init sequence.
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <common.h>
17*4882a593Smuzhiyun #include <dm.h>
18*4882a593Smuzhiyun #include <fdtdec.h>
19*4882a593Smuzhiyun #include <linux/libfdt.h>
20*4882a593Smuzhiyun #include <malloc.h>
21*4882a593Smuzhiyun #include <usb.h>
22*4882a593Smuzhiyun #include <watchdog.h>
23*4882a593Smuzhiyun #include <asm/arch/cpu.h>
24*4882a593Smuzhiyun #include <asm/arch/power.h>
25*4882a593Smuzhiyun #include <asm/arch/xhci-exynos.h>
26*4882a593Smuzhiyun #include <asm/gpio.h>
27*4882a593Smuzhiyun #include <linux/errno.h>
28*4882a593Smuzhiyun #include <linux/compat.h>
29*4882a593Smuzhiyun #include <linux/usb/dwc3.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include <usb/xhci.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /* Declare global data pointer */
34*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun struct exynos_xhci_platdata {
37*4882a593Smuzhiyun fdt_addr_t hcd_base;
38*4882a593Smuzhiyun fdt_addr_t phy_base;
39*4882a593Smuzhiyun struct gpio_desc vbus_gpio;
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun * Contains pointers to register base addresses
44*4882a593Smuzhiyun * for the usb controller.
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun struct exynos_xhci {
47*4882a593Smuzhiyun struct usb_platdata usb_plat;
48*4882a593Smuzhiyun struct xhci_ctrl ctrl;
49*4882a593Smuzhiyun struct exynos_usb3_phy *usb3_phy;
50*4882a593Smuzhiyun struct xhci_hccr *hcd;
51*4882a593Smuzhiyun struct dwc3 *dwc3_reg;
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun
xhci_usb_ofdata_to_platdata(struct udevice * dev)54*4882a593Smuzhiyun static int xhci_usb_ofdata_to_platdata(struct udevice *dev)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun struct exynos_xhci_platdata *plat = dev_get_platdata(dev);
57*4882a593Smuzhiyun const void *blob = gd->fdt_blob;
58*4882a593Smuzhiyun unsigned int node;
59*4882a593Smuzhiyun int depth;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun * Get the base address for XHCI controller from the device node
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun plat->hcd_base = devfdt_get_addr(dev);
65*4882a593Smuzhiyun if (plat->hcd_base == FDT_ADDR_T_NONE) {
66*4882a593Smuzhiyun debug("Can't get the XHCI register base address\n");
67*4882a593Smuzhiyun return -ENXIO;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun depth = 0;
71*4882a593Smuzhiyun node = fdtdec_next_compatible_subnode(blob, dev_of_offset(dev),
72*4882a593Smuzhiyun COMPAT_SAMSUNG_EXYNOS5_USB3_PHY, &depth);
73*4882a593Smuzhiyun if (node <= 0) {
74*4882a593Smuzhiyun debug("XHCI: Can't get device node for usb3-phy controller\n");
75*4882a593Smuzhiyun return -ENODEV;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun * Get the base address for usbphy from the device node
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun plat->phy_base = fdtdec_get_addr(blob, node, "reg");
82*4882a593Smuzhiyun if (plat->phy_base == FDT_ADDR_T_NONE) {
83*4882a593Smuzhiyun debug("Can't get the usbphy register address\n");
84*4882a593Smuzhiyun return -ENXIO;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Vbus gpio */
88*4882a593Smuzhiyun gpio_request_by_name(dev, "samsung,vbus-gpio", 0,
89*4882a593Smuzhiyun &plat->vbus_gpio, GPIOD_IS_OUT);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun return 0;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
exynos5_usb3_phy_init(struct exynos_usb3_phy * phy)94*4882a593Smuzhiyun static void exynos5_usb3_phy_init(struct exynos_usb3_phy *phy)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun u32 reg;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* enabling usb_drd phy */
99*4882a593Smuzhiyun set_usbdrd_phy_ctrl(POWER_USB_DRD_PHY_CTRL_EN);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* Reset USB 3.0 PHY */
102*4882a593Smuzhiyun writel(0x0, &phy->phy_reg0);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun clrbits_le32(&phy->phy_param0,
105*4882a593Smuzhiyun /* Select PHY CLK source */
106*4882a593Smuzhiyun PHYPARAM0_REF_USE_PAD |
107*4882a593Smuzhiyun /* Set Loss-of-Signal Detector sensitivity */
108*4882a593Smuzhiyun PHYPARAM0_REF_LOSLEVEL_MASK);
109*4882a593Smuzhiyun setbits_le32(&phy->phy_param0, PHYPARAM0_REF_LOSLEVEL);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun writel(0x0, &phy->phy_resume);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun * Setting the Frame length Adj value[6:1] to default 0x20
115*4882a593Smuzhiyun * See xHCI 1.0 spec, 5.2.4
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun setbits_le32(&phy->link_system,
118*4882a593Smuzhiyun LINKSYSTEM_XHCI_VERSION_CONTROL |
119*4882a593Smuzhiyun LINKSYSTEM_FLADJ(0x20));
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* Set Tx De-Emphasis level */
122*4882a593Smuzhiyun clrbits_le32(&phy->phy_param1, PHYPARAM1_PCS_TXDEEMPH_MASK);
123*4882a593Smuzhiyun setbits_le32(&phy->phy_param1, PHYPARAM1_PCS_TXDEEMPH);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun setbits_le32(&phy->phy_batchg, PHYBATCHG_UTMI_CLKSEL);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* PHYTEST POWERDOWN Control */
128*4882a593Smuzhiyun clrbits_le32(&phy->phy_test,
129*4882a593Smuzhiyun PHYTEST_POWERDOWN_SSP |
130*4882a593Smuzhiyun PHYTEST_POWERDOWN_HSP);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* UTMI Power Control */
133*4882a593Smuzhiyun writel(PHYUTMI_OTGDISABLE, &phy->phy_utmi);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* Use core clock from main PLL */
136*4882a593Smuzhiyun reg = PHYCLKRST_REFCLKSEL_EXT_REFCLK |
137*4882a593Smuzhiyun /* Default 24Mhz crystal clock */
138*4882a593Smuzhiyun PHYCLKRST_FSEL(FSEL_CLKSEL_24M) |
139*4882a593Smuzhiyun PHYCLKRST_MPLL_MULTIPLIER_24MHZ_REF |
140*4882a593Smuzhiyun PHYCLKRST_SSC_REFCLKSEL(0x88) |
141*4882a593Smuzhiyun /* Force PortReset of PHY */
142*4882a593Smuzhiyun PHYCLKRST_PORTRESET |
143*4882a593Smuzhiyun /* Digital power supply in normal operating mode */
144*4882a593Smuzhiyun PHYCLKRST_RETENABLEN |
145*4882a593Smuzhiyun /* Enable ref clock for SS function */
146*4882a593Smuzhiyun PHYCLKRST_REF_SSP_EN |
147*4882a593Smuzhiyun /* Enable spread spectrum */
148*4882a593Smuzhiyun PHYCLKRST_SSC_EN |
149*4882a593Smuzhiyun /* Power down HS Bias and PLL blocks in suspend mode */
150*4882a593Smuzhiyun PHYCLKRST_COMMONONN;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun writel(reg, &phy->phy_clk_rst);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /* giving time to Phy clock to settle before resetting */
155*4882a593Smuzhiyun udelay(10);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun reg &= ~PHYCLKRST_PORTRESET;
158*4882a593Smuzhiyun writel(reg, &phy->phy_clk_rst);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
exynos5_usb3_phy_exit(struct exynos_usb3_phy * phy)161*4882a593Smuzhiyun static void exynos5_usb3_phy_exit(struct exynos_usb3_phy *phy)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun setbits_le32(&phy->phy_utmi,
164*4882a593Smuzhiyun PHYUTMI_OTGDISABLE |
165*4882a593Smuzhiyun PHYUTMI_FORCESUSPEND |
166*4882a593Smuzhiyun PHYUTMI_FORCESLEEP);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun clrbits_le32(&phy->phy_clk_rst,
169*4882a593Smuzhiyun PHYCLKRST_REF_SSP_EN |
170*4882a593Smuzhiyun PHYCLKRST_SSC_EN |
171*4882a593Smuzhiyun PHYCLKRST_COMMONONN);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* PHYTEST POWERDOWN Control to remove leakage current */
174*4882a593Smuzhiyun setbits_le32(&phy->phy_test,
175*4882a593Smuzhiyun PHYTEST_POWERDOWN_SSP |
176*4882a593Smuzhiyun PHYTEST_POWERDOWN_HSP);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /* disabling usb_drd phy */
179*4882a593Smuzhiyun set_usbdrd_phy_ctrl(POWER_USB_DRD_PHY_CTRL_DISABLE);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
exynos_xhci_core_init(struct exynos_xhci * exynos)182*4882a593Smuzhiyun static int exynos_xhci_core_init(struct exynos_xhci *exynos)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun int ret;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun exynos5_usb3_phy_init(exynos->usb3_phy);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun ret = dwc3_core_init(exynos->dwc3_reg);
189*4882a593Smuzhiyun if (ret) {
190*4882a593Smuzhiyun debug("failed to initialize core\n");
191*4882a593Smuzhiyun return -EINVAL;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* We are hard-coding DWC3 core to Host Mode */
195*4882a593Smuzhiyun dwc3_set_mode(exynos->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun return 0;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
exynos_xhci_core_exit(struct exynos_xhci * exynos)200*4882a593Smuzhiyun static void exynos_xhci_core_exit(struct exynos_xhci *exynos)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun exynos5_usb3_phy_exit(exynos->usb3_phy);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
xhci_usb_probe(struct udevice * dev)205*4882a593Smuzhiyun static int xhci_usb_probe(struct udevice *dev)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct exynos_xhci_platdata *plat = dev_get_platdata(dev);
208*4882a593Smuzhiyun struct exynos_xhci *ctx = dev_get_priv(dev);
209*4882a593Smuzhiyun struct xhci_hcor *hcor;
210*4882a593Smuzhiyun int ret;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun ctx->hcd = (struct xhci_hccr *)plat->hcd_base;
213*4882a593Smuzhiyun ctx->usb3_phy = (struct exynos_usb3_phy *)plat->phy_base;
214*4882a593Smuzhiyun ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
215*4882a593Smuzhiyun hcor = (struct xhci_hcor *)((uint32_t)ctx->hcd +
216*4882a593Smuzhiyun HC_LENGTH(xhci_readl(&ctx->hcd->cr_capbase)));
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /* setup the Vbus gpio here */
219*4882a593Smuzhiyun if (dm_gpio_is_valid(&plat->vbus_gpio))
220*4882a593Smuzhiyun dm_gpio_set_value(&plat->vbus_gpio, 1);
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun ret = exynos_xhci_core_init(ctx);
223*4882a593Smuzhiyun if (ret) {
224*4882a593Smuzhiyun puts("XHCI: failed to initialize controller\n");
225*4882a593Smuzhiyun return -EINVAL;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun return xhci_register(dev, ctx->hcd, hcor);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
xhci_usb_remove(struct udevice * dev)231*4882a593Smuzhiyun static int xhci_usb_remove(struct udevice *dev)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun struct exynos_xhci *ctx = dev_get_priv(dev);
234*4882a593Smuzhiyun int ret;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun ret = xhci_deregister(dev);
237*4882a593Smuzhiyun if (ret)
238*4882a593Smuzhiyun return ret;
239*4882a593Smuzhiyun exynos_xhci_core_exit(ctx);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun return 0;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun static const struct udevice_id xhci_usb_ids[] = {
245*4882a593Smuzhiyun { .compatible = "samsung,exynos5250-xhci" },
246*4882a593Smuzhiyun { }
247*4882a593Smuzhiyun };
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun U_BOOT_DRIVER(usb_xhci) = {
250*4882a593Smuzhiyun .name = "xhci_exynos",
251*4882a593Smuzhiyun .id = UCLASS_USB,
252*4882a593Smuzhiyun .of_match = xhci_usb_ids,
253*4882a593Smuzhiyun .ofdata_to_platdata = xhci_usb_ofdata_to_platdata,
254*4882a593Smuzhiyun .probe = xhci_usb_probe,
255*4882a593Smuzhiyun .remove = xhci_usb_remove,
256*4882a593Smuzhiyun .ops = &xhci_usb_ops,
257*4882a593Smuzhiyun .platdata_auto_alloc_size = sizeof(struct exynos_xhci_platdata),
258*4882a593Smuzhiyun .priv_auto_alloc_size = sizeof(struct exynos_xhci),
259*4882a593Smuzhiyun .flags = DM_FLAG_ALLOC_PRIV_DMA,
260*4882a593Smuzhiyun };
261