xhci-exynos5.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * SAMSUNG EXYNOS5 USB HOST XHCI Controller
  3. *
  4. * Copyright (C) 2012 Samsung Electronics Co.Ltd
  5. * Vivek Gautam <gautam.vivek@samsung.com>
  6. * Vikas Sajjan <vikas.sajjan@samsung.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /*
  11. * This file is a conglomeration for DWC3-init sequence and further
  12. * exynos5 specific PHY-init sequence.
  13. */
  14. #include <common.h>
  15. #include <fdtdec.h>
  16. #include <libfdt.h>
  17. #include <malloc.h>
  18. #include <usb.h>
  19. #include <watchdog.h>
  20. #include <asm/arch/cpu.h>
  21. #include <asm/arch/power.h>
  22. #include <asm/arch/xhci-exynos.h>
  23. #include <asm/gpio.h>
  24. #include <asm-generic/errno.h>
  25. #include <linux/compat.h>
  26. #include <linux/usb/dwc3.h>
  27. #include "xhci.h"
  28. /* Declare global data pointer */
  29. DECLARE_GLOBAL_DATA_PTR;
  30. /**
  31. * Contains pointers to register base addresses
  32. * for the usb controller.
  33. */
  34. struct exynos_xhci {
  35. struct exynos_usb3_phy *usb3_phy;
  36. struct xhci_hccr *hcd;
  37. struct dwc3 *dwc3_reg;
  38. struct fdt_gpio_state vbus_gpio;
  39. };
  40. static struct exynos_xhci exynos;
  41. #ifdef CONFIG_OF_CONTROL
  42. static int exynos_usb3_parse_dt(const void *blob, struct exynos_xhci *exynos)
  43. {
  44. fdt_addr_t addr;
  45. unsigned int node;
  46. int depth;
  47. node = fdtdec_next_compatible(blob, 0, COMPAT_SAMSUNG_EXYNOS5_XHCI);
  48. if (node <= 0) {
  49. debug("XHCI: Can't get device node for xhci\n");
  50. return -ENODEV;
  51. }
  52. /*
  53. * Get the base address for XHCI controller from the device node
  54. */
  55. addr = fdtdec_get_addr(blob, node, "reg");
  56. if (addr == FDT_ADDR_T_NONE) {
  57. debug("Can't get the XHCI register base address\n");
  58. return -ENXIO;
  59. }
  60. exynos->hcd = (struct xhci_hccr *)addr;
  61. /* Vbus gpio */
  62. fdtdec_decode_gpio(blob, node, "samsung,vbus-gpio", &exynos->vbus_gpio);
  63. depth = 0;
  64. node = fdtdec_next_compatible_subnode(blob, node,
  65. COMPAT_SAMSUNG_EXYNOS5_USB3_PHY, &depth);
  66. if (node <= 0) {
  67. debug("XHCI: Can't get device node for usb3-phy controller\n");
  68. return -ENODEV;
  69. }
  70. /*
  71. * Get the base address for usbphy from the device node
  72. */
  73. exynos->usb3_phy = (struct exynos_usb3_phy *)fdtdec_get_addr(blob, node,
  74. "reg");
  75. if (exynos->usb3_phy == NULL) {
  76. debug("Can't get the usbphy register address\n");
  77. return -ENXIO;
  78. }
  79. return 0;
  80. }
  81. #endif
  82. static void exynos5_usb3_phy_init(struct exynos_usb3_phy *phy)
  83. {
  84. u32 reg;
  85. /* enabling usb_drd phy */
  86. set_usbdrd_phy_ctrl(POWER_USB_DRD_PHY_CTRL_EN);
  87. /* Reset USB 3.0 PHY */
  88. writel(0x0, &phy->phy_reg0);
  89. clrbits_le32(&phy->phy_param0,
  90. /* Select PHY CLK source */
  91. PHYPARAM0_REF_USE_PAD |
  92. /* Set Loss-of-Signal Detector sensitivity */
  93. PHYPARAM0_REF_LOSLEVEL_MASK);
  94. setbits_le32(&phy->phy_param0, PHYPARAM0_REF_LOSLEVEL);
  95. writel(0x0, &phy->phy_resume);
  96. /*
  97. * Setting the Frame length Adj value[6:1] to default 0x20
  98. * See xHCI 1.0 spec, 5.2.4
  99. */
  100. setbits_le32(&phy->link_system,
  101. LINKSYSTEM_XHCI_VERSION_CONTROL |
  102. LINKSYSTEM_FLADJ(0x20));
  103. /* Set Tx De-Emphasis level */
  104. clrbits_le32(&phy->phy_param1, PHYPARAM1_PCS_TXDEEMPH_MASK);
  105. setbits_le32(&phy->phy_param1, PHYPARAM1_PCS_TXDEEMPH);
  106. setbits_le32(&phy->phy_batchg, PHYBATCHG_UTMI_CLKSEL);
  107. /* PHYTEST POWERDOWN Control */
  108. clrbits_le32(&phy->phy_test,
  109. PHYTEST_POWERDOWN_SSP |
  110. PHYTEST_POWERDOWN_HSP);
  111. /* UTMI Power Control */
  112. writel(PHYUTMI_OTGDISABLE, &phy->phy_utmi);
  113. /* Use core clock from main PLL */
  114. reg = PHYCLKRST_REFCLKSEL_EXT_REFCLK |
  115. /* Default 24Mhz crystal clock */
  116. PHYCLKRST_FSEL(FSEL_CLKSEL_24M) |
  117. PHYCLKRST_MPLL_MULTIPLIER_24MHZ_REF |
  118. PHYCLKRST_SSC_REFCLKSEL(0x88) |
  119. /* Force PortReset of PHY */
  120. PHYCLKRST_PORTRESET |
  121. /* Digital power supply in normal operating mode */
  122. PHYCLKRST_RETENABLEN |
  123. /* Enable ref clock for SS function */
  124. PHYCLKRST_REF_SSP_EN |
  125. /* Enable spread spectrum */
  126. PHYCLKRST_SSC_EN |
  127. /* Power down HS Bias and PLL blocks in suspend mode */
  128. PHYCLKRST_COMMONONN;
  129. writel(reg, &phy->phy_clk_rst);
  130. /* giving time to Phy clock to settle before resetting */
  131. udelay(10);
  132. reg &= ~PHYCLKRST_PORTRESET;
  133. writel(reg, &phy->phy_clk_rst);
  134. }
  135. static void exynos5_usb3_phy_exit(struct exynos_usb3_phy *phy)
  136. {
  137. setbits_le32(&phy->phy_utmi,
  138. PHYUTMI_OTGDISABLE |
  139. PHYUTMI_FORCESUSPEND |
  140. PHYUTMI_FORCESLEEP);
  141. clrbits_le32(&phy->phy_clk_rst,
  142. PHYCLKRST_REF_SSP_EN |
  143. PHYCLKRST_SSC_EN |
  144. PHYCLKRST_COMMONONN);
  145. /* PHYTEST POWERDOWN Control to remove leakage current */
  146. setbits_le32(&phy->phy_test,
  147. PHYTEST_POWERDOWN_SSP |
  148. PHYTEST_POWERDOWN_HSP);
  149. /* disabling usb_drd phy */
  150. set_usbdrd_phy_ctrl(POWER_USB_DRD_PHY_CTRL_DISABLE);
  151. }
  152. void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
  153. {
  154. clrsetbits_le32(&dwc3_reg->g_ctl,
  155. DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
  156. DWC3_GCTL_PRTCAPDIR(mode));
  157. }
  158. static void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
  159. {
  160. /* Before Resetting PHY, put Core in Reset */
  161. setbits_le32(&dwc3_reg->g_ctl,
  162. DWC3_GCTL_CORESOFTRESET);
  163. /* Assert USB3 PHY reset */
  164. setbits_le32(&dwc3_reg->g_usb3pipectl[0],
  165. DWC3_GUSB3PIPECTL_PHYSOFTRST);
  166. /* Assert USB2 PHY reset */
  167. setbits_le32(&dwc3_reg->g_usb2phycfg,
  168. DWC3_GUSB2PHYCFG_PHYSOFTRST);
  169. mdelay(100);
  170. /* Clear USB3 PHY reset */
  171. clrbits_le32(&dwc3_reg->g_usb3pipectl[0],
  172. DWC3_GUSB3PIPECTL_PHYSOFTRST);
  173. /* Clear USB2 PHY reset */
  174. clrbits_le32(&dwc3_reg->g_usb2phycfg,
  175. DWC3_GUSB2PHYCFG_PHYSOFTRST);
  176. /* After PHYs are stable we can take Core out of reset state */
  177. clrbits_le32(&dwc3_reg->g_ctl,
  178. DWC3_GCTL_CORESOFTRESET);
  179. }
  180. static int dwc3_core_init(struct dwc3 *dwc3_reg)
  181. {
  182. u32 reg;
  183. u32 revision;
  184. unsigned int dwc3_hwparams1;
  185. revision = readl(&dwc3_reg->g_snpsid);
  186. /* This should read as U3 followed by revision number */
  187. if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
  188. puts("this is not a DesignWare USB3 DRD Core\n");
  189. return -EINVAL;
  190. }
  191. dwc3_core_soft_reset(dwc3_reg);
  192. dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
  193. reg = readl(&dwc3_reg->g_ctl);
  194. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  195. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  196. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
  197. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  198. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  199. break;
  200. default:
  201. debug("No power optimization available\n");
  202. }
  203. /*
  204. * WORKAROUND: DWC3 revisions <1.90a have a bug
  205. * where the device can fail to connect at SuperSpeed
  206. * and falls back to high-speed mode which causes
  207. * the device to enter a Connect/Disconnect loop
  208. */
  209. if ((revision & DWC3_REVISION_MASK) < 0x190a)
  210. reg |= DWC3_GCTL_U2RSTECN;
  211. writel(reg, &dwc3_reg->g_ctl);
  212. return 0;
  213. }
  214. static int exynos_xhci_core_init(struct exynos_xhci *exynos)
  215. {
  216. int ret;
  217. exynos5_usb3_phy_init(exynos->usb3_phy);
  218. ret = dwc3_core_init(exynos->dwc3_reg);
  219. if (ret) {
  220. debug("failed to initialize core\n");
  221. return -EINVAL;
  222. }
  223. /* We are hard-coding DWC3 core to Host Mode */
  224. dwc3_set_mode(exynos->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
  225. return 0;
  226. }
  227. static void exynos_xhci_core_exit(struct exynos_xhci *exynos)
  228. {
  229. exynos5_usb3_phy_exit(exynos->usb3_phy);
  230. }
  231. int xhci_hcd_init(int index, struct xhci_hccr **hccr, struct xhci_hcor **hcor)
  232. {
  233. struct exynos_xhci *ctx = &exynos;
  234. int ret;
  235. #ifdef CONFIG_OF_CONTROL
  236. exynos_usb3_parse_dt(gd->fdt_blob, ctx);
  237. #else
  238. ctx->usb3_phy = (struct exynos_usb3_phy *)samsung_get_base_usb3_phy();
  239. ctx->hcd = (struct xhci_hccr *)samsung_get_base_usb_xhci();
  240. #endif
  241. ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
  242. #ifdef CONFIG_OF_CONTROL
  243. /* setup the Vbus gpio here */
  244. if (fdt_gpio_isvalid(&ctx->vbus_gpio) &&
  245. !fdtdec_setup_gpio(&ctx->vbus_gpio))
  246. gpio_direction_output(ctx->vbus_gpio.gpio, 1);
  247. #endif
  248. ret = exynos_xhci_core_init(ctx);
  249. if (ret) {
  250. puts("XHCI: failed to initialize controller\n");
  251. return -EINVAL;
  252. }
  253. *hccr = (ctx->hcd);
  254. *hcor = (struct xhci_hcor *)((uint32_t) *hccr
  255. + HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
  256. debug("Exynos5-xhci: init hccr %x and hcor %x hc_length %d\n",
  257. (uint32_t)*hccr, (uint32_t)*hcor,
  258. (uint32_t)HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
  259. return 0;
  260. }
  261. void xhci_hcd_stop(int index)
  262. {
  263. struct exynos_xhci *ctx = &exynos;
  264. exynos_xhci_core_exit(ctx);
  265. }