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