2 * SAMSUNG EXYNOS5 USB HOST XHCI Controller
4 * Copyright (C) 2012 Samsung Electronics Co.Ltd
5 * Vivek Gautam <gautam.vivek@samsung.com>
6 * Vikas Sajjan <vikas.sajjan@samsung.com>
8 * SPDX-License-Identifier: GPL-2.0+
12 * This file is a conglomeration for DWC3-init sequence and further
13 * exynos5 specific PHY-init sequence.
23 #include <asm/arch/cpu.h>
24 #include <asm/arch/power.h>
25 #include <asm/arch/xhci-exynos.h>
27 #include <asm-generic/errno.h>
28 #include <linux/compat.h>
29 #include <linux/usb/dwc3.h>
33 /* Declare global data pointer */
34 DECLARE_GLOBAL_DATA_PTR;
37 struct exynos_xhci_platdata {
40 struct gpio_desc vbus_gpio;
45 * Contains pointers to register base addresses
46 * for the usb controller.
50 struct usb_platdata usb_plat;
52 struct xhci_ctrl ctrl;
53 struct exynos_usb3_phy *usb3_phy;
54 struct xhci_hccr *hcd;
55 struct dwc3 *dwc3_reg;
57 struct gpio_desc vbus_gpio;
62 static struct exynos_xhci exynos;
66 static int xhci_usb_ofdata_to_platdata(struct udevice *dev)
68 struct exynos_xhci_platdata *plat = dev_get_platdata(dev);
69 const void *blob = gd->fdt_blob;
74 * Get the base address for XHCI controller from the device node
76 plat->hcd_base = fdtdec_get_addr(blob, dev->of_offset, "reg");
77 if (plat->hcd_base == FDT_ADDR_T_NONE) {
78 debug("Can't get the XHCI register base address\n");
83 node = fdtdec_next_compatible_subnode(blob, dev->of_offset,
84 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY, &depth);
86 debug("XHCI: Can't get device node for usb3-phy controller\n");
91 * Get the base address for usbphy from the device node
93 plat->phy_base = fdtdec_get_addr(blob, node, "reg");
94 if (plat->phy_base == FDT_ADDR_T_NONE) {
95 debug("Can't get the usbphy register address\n");
100 gpio_request_by_name(dev, "samsung,vbus-gpio", 0,
101 &plat->vbus_gpio, GPIOD_IS_OUT);
106 static int exynos_usb3_parse_dt(const void *blob, struct exynos_xhci *exynos)
112 node = fdtdec_next_compatible(blob, 0, COMPAT_SAMSUNG_EXYNOS5_XHCI);
114 debug("XHCI: Can't get device node for xhci\n");
119 * Get the base address for XHCI controller from the device node
121 addr = fdtdec_get_addr(blob, node, "reg");
122 if (addr == FDT_ADDR_T_NONE) {
123 debug("Can't get the XHCI register base address\n");
126 exynos->hcd = (struct xhci_hccr *)addr;
129 gpio_request_by_name_nodev(blob, node, "samsung,vbus-gpio", 0,
130 &exynos->vbus_gpio, GPIOD_IS_OUT);
133 node = fdtdec_next_compatible_subnode(blob, node,
134 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY, &depth);
136 debug("XHCI: Can't get device node for usb3-phy controller\n");
141 * Get the base address for usbphy from the device node
143 exynos->usb3_phy = (struct exynos_usb3_phy *)fdtdec_get_addr(blob, node,
145 if (exynos->usb3_phy == NULL) {
146 debug("Can't get the usbphy register address\n");
154 static void exynos5_usb3_phy_init(struct exynos_usb3_phy *phy)
158 /* enabling usb_drd phy */
159 set_usbdrd_phy_ctrl(POWER_USB_DRD_PHY_CTRL_EN);
161 /* Reset USB 3.0 PHY */
162 writel(0x0, &phy->phy_reg0);
164 clrbits_le32(&phy->phy_param0,
165 /* Select PHY CLK source */
166 PHYPARAM0_REF_USE_PAD |
167 /* Set Loss-of-Signal Detector sensitivity */
168 PHYPARAM0_REF_LOSLEVEL_MASK);
169 setbits_le32(&phy->phy_param0, PHYPARAM0_REF_LOSLEVEL);
171 writel(0x0, &phy->phy_resume);
174 * Setting the Frame length Adj value[6:1] to default 0x20
175 * See xHCI 1.0 spec, 5.2.4
177 setbits_le32(&phy->link_system,
178 LINKSYSTEM_XHCI_VERSION_CONTROL |
179 LINKSYSTEM_FLADJ(0x20));
181 /* Set Tx De-Emphasis level */
182 clrbits_le32(&phy->phy_param1, PHYPARAM1_PCS_TXDEEMPH_MASK);
183 setbits_le32(&phy->phy_param1, PHYPARAM1_PCS_TXDEEMPH);
185 setbits_le32(&phy->phy_batchg, PHYBATCHG_UTMI_CLKSEL);
187 /* PHYTEST POWERDOWN Control */
188 clrbits_le32(&phy->phy_test,
189 PHYTEST_POWERDOWN_SSP |
190 PHYTEST_POWERDOWN_HSP);
192 /* UTMI Power Control */
193 writel(PHYUTMI_OTGDISABLE, &phy->phy_utmi);
195 /* Use core clock from main PLL */
196 reg = PHYCLKRST_REFCLKSEL_EXT_REFCLK |
197 /* Default 24Mhz crystal clock */
198 PHYCLKRST_FSEL(FSEL_CLKSEL_24M) |
199 PHYCLKRST_MPLL_MULTIPLIER_24MHZ_REF |
200 PHYCLKRST_SSC_REFCLKSEL(0x88) |
201 /* Force PortReset of PHY */
202 PHYCLKRST_PORTRESET |
203 /* Digital power supply in normal operating mode */
204 PHYCLKRST_RETENABLEN |
205 /* Enable ref clock for SS function */
206 PHYCLKRST_REF_SSP_EN |
207 /* Enable spread spectrum */
209 /* Power down HS Bias and PLL blocks in suspend mode */
212 writel(reg, &phy->phy_clk_rst);
214 /* giving time to Phy clock to settle before resetting */
217 reg &= ~PHYCLKRST_PORTRESET;
218 writel(reg, &phy->phy_clk_rst);
221 static void exynos5_usb3_phy_exit(struct exynos_usb3_phy *phy)
223 setbits_le32(&phy->phy_utmi,
225 PHYUTMI_FORCESUSPEND |
228 clrbits_le32(&phy->phy_clk_rst,
229 PHYCLKRST_REF_SSP_EN |
231 PHYCLKRST_COMMONONN);
233 /* PHYTEST POWERDOWN Control to remove leakage current */
234 setbits_le32(&phy->phy_test,
235 PHYTEST_POWERDOWN_SSP |
236 PHYTEST_POWERDOWN_HSP);
238 /* disabling usb_drd phy */
239 set_usbdrd_phy_ctrl(POWER_USB_DRD_PHY_CTRL_DISABLE);
242 static void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
244 clrsetbits_le32(&dwc3_reg->g_ctl,
245 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
246 DWC3_GCTL_PRTCAPDIR(mode));
249 static void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
251 /* Before Resetting PHY, put Core in Reset */
252 setbits_le32(&dwc3_reg->g_ctl,
253 DWC3_GCTL_CORESOFTRESET);
255 /* Assert USB3 PHY reset */
256 setbits_le32(&dwc3_reg->g_usb3pipectl[0],
257 DWC3_GUSB3PIPECTL_PHYSOFTRST);
259 /* Assert USB2 PHY reset */
260 setbits_le32(&dwc3_reg->g_usb2phycfg,
261 DWC3_GUSB2PHYCFG_PHYSOFTRST);
265 /* Clear USB3 PHY reset */
266 clrbits_le32(&dwc3_reg->g_usb3pipectl[0],
267 DWC3_GUSB3PIPECTL_PHYSOFTRST);
269 /* Clear USB2 PHY reset */
270 clrbits_le32(&dwc3_reg->g_usb2phycfg,
271 DWC3_GUSB2PHYCFG_PHYSOFTRST);
273 /* After PHYs are stable we can take Core out of reset state */
274 clrbits_le32(&dwc3_reg->g_ctl,
275 DWC3_GCTL_CORESOFTRESET);
278 static int dwc3_core_init(struct dwc3 *dwc3_reg)
282 unsigned int dwc3_hwparams1;
284 revision = readl(&dwc3_reg->g_snpsid);
285 /* This should read as U3 followed by revision number */
286 if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
287 puts("this is not a DesignWare USB3 DRD Core\n");
291 dwc3_core_soft_reset(dwc3_reg);
293 dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
295 reg = readl(&dwc3_reg->g_ctl);
296 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
297 reg &= ~DWC3_GCTL_DISSCRAMBLE;
298 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
299 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
300 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
303 debug("No power optimization available\n");
307 * WORKAROUND: DWC3 revisions <1.90a have a bug
308 * where the device can fail to connect at SuperSpeed
309 * and falls back to high-speed mode which causes
310 * the device to enter a Connect/Disconnect loop
312 if ((revision & DWC3_REVISION_MASK) < 0x190a)
313 reg |= DWC3_GCTL_U2RSTECN;
315 writel(reg, &dwc3_reg->g_ctl);
320 static int exynos_xhci_core_init(struct exynos_xhci *exynos)
324 exynos5_usb3_phy_init(exynos->usb3_phy);
326 ret = dwc3_core_init(exynos->dwc3_reg);
328 debug("failed to initialize core\n");
332 /* We are hard-coding DWC3 core to Host Mode */
333 dwc3_set_mode(exynos->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
338 static void exynos_xhci_core_exit(struct exynos_xhci *exynos)
340 exynos5_usb3_phy_exit(exynos->usb3_phy);
343 #ifndef CONFIG_DM_USB
344 int xhci_hcd_init(int index, struct xhci_hccr **hccr, struct xhci_hcor **hcor)
346 struct exynos_xhci *ctx = &exynos;
349 #ifdef CONFIG_OF_CONTROL
350 exynos_usb3_parse_dt(gd->fdt_blob, ctx);
352 ctx->usb3_phy = (struct exynos_usb3_phy *)samsung_get_base_usb3_phy();
353 ctx->hcd = (struct xhci_hccr *)samsung_get_base_usb_xhci();
356 ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
358 #ifdef CONFIG_OF_CONTROL
359 /* setup the Vbus gpio here */
360 if (dm_gpio_is_valid(&ctx->vbus_gpio))
361 dm_gpio_set_value(&ctx->vbus_gpio, 1);
364 ret = exynos_xhci_core_init(ctx);
366 puts("XHCI: failed to initialize controller\n");
371 *hcor = (struct xhci_hcor *)((uint32_t) *hccr
372 + HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
374 debug("Exynos5-xhci: init hccr %x and hcor %x hc_length %d\n",
375 (uint32_t)*hccr, (uint32_t)*hcor,
376 (uint32_t)HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
381 void xhci_hcd_stop(int index)
383 struct exynos_xhci *ctx = &exynos;
385 exynos_xhci_core_exit(ctx);
390 static int xhci_usb_probe(struct udevice *dev)
392 struct exynos_xhci_platdata *plat = dev_get_platdata(dev);
393 struct exynos_xhci *ctx = dev_get_priv(dev);
394 struct xhci_hcor *hcor;
397 ctx->hcd = (struct xhci_hccr *)plat->hcd_base;
398 ctx->usb3_phy = (struct exynos_usb3_phy *)plat->phy_base;
399 ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
400 hcor = (struct xhci_hcor *)((uint32_t)ctx->hcd +
401 HC_LENGTH(xhci_readl(&ctx->hcd->cr_capbase)));
403 /* setup the Vbus gpio here */
404 if (dm_gpio_is_valid(&plat->vbus_gpio))
405 dm_gpio_set_value(&plat->vbus_gpio, 1);
407 ret = exynos_xhci_core_init(ctx);
409 puts("XHCI: failed to initialize controller\n");
413 return xhci_register(dev, ctx->hcd, hcor);
416 static int xhci_usb_remove(struct udevice *dev)
418 struct exynos_xhci *ctx = dev_get_priv(dev);
421 ret = xhci_deregister(dev);
424 exynos_xhci_core_exit(ctx);
429 static const struct udevice_id xhci_usb_ids[] = {
430 { .compatible = "samsung,exynos5250-xhci" },
434 U_BOOT_DRIVER(usb_xhci) = {
435 .name = "xhci_exynos",
437 .of_match = xhci_usb_ids,
438 .ofdata_to_platdata = xhci_usb_ofdata_to_platdata,
439 .probe = xhci_usb_probe,
440 .remove = xhci_usb_remove,
441 .ops = &xhci_usb_ops,
442 .platdata_auto_alloc_size = sizeof(struct exynos_xhci_platdata),
443 .priv_auto_alloc_size = sizeof(struct exynos_xhci),
444 .flags = DM_FLAG_ALLOC_PRIV_DMA,