xhci-omap.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * OMAP USB HOST xHCI Controller
  3. *
  4. * (C) Copyright 2013
  5. * Texas Instruments, <www.ti.com>
  6. *
  7. * Author: Dan Murphy <dmurphy@ti.com>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <usb.h>
  13. #include <asm-generic/errno.h>
  14. #include <asm/omap_common.h>
  15. #include <asm/arch/cpu.h>
  16. #include <asm/arch/sys_proto.h>
  17. #include <linux/compat.h>
  18. #include <linux/usb/dwc3.h>
  19. #include <linux/usb/xhci-omap.h>
  20. #include "xhci.h"
  21. /* Declare global data pointer */
  22. DECLARE_GLOBAL_DATA_PTR;
  23. static struct omap_xhci omap;
  24. struct usb_dpll_params {
  25. u16 m;
  26. u8 n;
  27. u8 freq:3;
  28. u8 sd;
  29. u32 mf;
  30. };
  31. #define NUM_USB_CLKS 6
  32. static struct usb_dpll_params omap_usb3_dpll_params[NUM_USB_CLKS] = {
  33. {1250, 5, 4, 20, 0}, /* 12 MHz */
  34. {3125, 20, 4, 20, 0}, /* 16.8 MHz */
  35. {1172, 8, 4, 20, 65537}, /* 19.2 MHz */
  36. {1250, 12, 4, 20, 0}, /* 26 MHz */
  37. {3125, 47, 4, 20, 92843}, /* 38.4 MHz */
  38. {1000, 7, 4, 10, 0}, /* 20 MHz */
  39. };
  40. static void omap_usb_dpll_relock(struct omap_usb3_phy *phy_regs)
  41. {
  42. u32 val;
  43. writel(SET_PLL_GO, &phy_regs->pll_go);
  44. do {
  45. val = readl(&phy_regs->pll_status);
  46. if (val & PLL_LOCK)
  47. break;
  48. } while (1);
  49. }
  50. static void omap_usb_dpll_lock(struct omap_usb3_phy *phy_regs)
  51. {
  52. u32 clk_index = get_sys_clk_index();
  53. u32 val;
  54. val = readl(&phy_regs->pll_config_1);
  55. val &= ~PLL_REGN_MASK;
  56. val |= omap_usb3_dpll_params[clk_index].n << PLL_REGN_SHIFT;
  57. writel(val, &phy_regs->pll_config_1);
  58. val = readl(&phy_regs->pll_config_2);
  59. val &= ~PLL_SELFREQDCO_MASK;
  60. val |= omap_usb3_dpll_params[clk_index].freq << PLL_SELFREQDCO_SHIFT;
  61. writel(val, &phy_regs->pll_config_2);
  62. val = readl(&phy_regs->pll_config_1);
  63. val &= ~PLL_REGM_MASK;
  64. val |= omap_usb3_dpll_params[clk_index].m << PLL_REGM_SHIFT;
  65. writel(val, &phy_regs->pll_config_1);
  66. val = readl(&phy_regs->pll_config_4);
  67. val &= ~PLL_REGM_F_MASK;
  68. val |= omap_usb3_dpll_params[clk_index].mf << PLL_REGM_F_SHIFT;
  69. writel(val, &phy_regs->pll_config_4);
  70. val = readl(&phy_regs->pll_config_3);
  71. val &= ~PLL_SD_MASK;
  72. val |= omap_usb3_dpll_params[clk_index].sd << PLL_SD_SHIFT;
  73. writel(val, &phy_regs->pll_config_3);
  74. omap_usb_dpll_relock(phy_regs);
  75. }
  76. static void usb3_phy_partial_powerup(struct omap_usb3_phy *phy_regs)
  77. {
  78. u32 rate = get_sys_clk_freq()/1000000;
  79. u32 val;
  80. val = readl((*ctrl)->control_phy_power_usb);
  81. val &= ~(USB3_PWRCTL_CLK_CMD_MASK | USB3_PWRCTL_CLK_FREQ_MASK);
  82. val |= (USB3_PHY_PARTIAL_RX_POWERON | USB3_PHY_TX_RX_POWERON);
  83. val |= rate << USB3_PWRCTL_CLK_FREQ_SHIFT;
  84. writel(val, (*ctrl)->control_phy_power_usb);
  85. }
  86. static void usb3_phy_power(int on)
  87. {
  88. u32 val;
  89. val = readl((*ctrl)->control_phy_power_usb);
  90. if (on) {
  91. val &= ~USB3_PWRCTL_CLK_CMD_MASK;
  92. val |= USB3_PHY_TX_RX_POWERON;
  93. } else {
  94. val &= (~USB3_PWRCTL_CLK_CMD_MASK & ~USB3_PHY_TX_RX_POWERON);
  95. }
  96. writel(val, (*ctrl)->control_phy_power_usb);
  97. }
  98. static void dwc_usb3_phy_init(struct omap_usb3_phy *phy_regs)
  99. {
  100. omap_usb_dpll_lock(phy_regs);
  101. usb3_phy_partial_powerup(phy_regs);
  102. /*
  103. * Give enough time for the PHY to partially power-up before
  104. * powering it up completely. delay value suggested by the HW
  105. * team.
  106. */
  107. mdelay(100);
  108. usb3_phy_power(1);
  109. }
  110. static void omap_enable_phy_clocks(struct omap_xhci *omap)
  111. {
  112. u32 val;
  113. /* Setting OCP2SCP1 register */
  114. setbits_le32((*prcm)->cm_l3init_ocp2scp1_clkctrl,
  115. OCP2SCP1_CLKCTRL_MODULEMODE_HW);
  116. /* Turn on 32K AON clk */
  117. setbits_le32((*prcm)->cm_coreaon_usb_phy_core_clkctrl,
  118. USBPHY_CORE_CLKCTRL_OPTFCLKEN_CLK32K);
  119. /* Setting CM_L3INIT_CLKSTCTRL to 0x0 i.e NO sleep */
  120. writel(0x0, (*prcm)->cm_l3init_clkstctrl);
  121. val = (USBOTGSS_DMADISABLE |
  122. USBOTGSS_STANDBYMODE_SMRT_WKUP |
  123. USBOTGSS_IDLEMODE_NOIDLE);
  124. writel(val, &omap->otg_wrapper->sysconfig);
  125. /* Clear the utmi OTG status */
  126. val = readl(&omap->otg_wrapper->utmi_otg_status);
  127. writel(val, &omap->otg_wrapper->utmi_otg_status);
  128. /* Enable interrupts */
  129. writel(USBOTGSS_COREIRQ_EN, &omap->otg_wrapper->irqenable_set_0);
  130. val = (USBOTGSS_IRQ_SET_1_IDPULLUP_FALL_EN |
  131. USBOTGSS_IRQ_SET_1_DISCHRGVBUS_FALL_EN |
  132. USBOTGSS_IRQ_SET_1_CHRGVBUS_FALL_EN |
  133. USBOTGSS_IRQ_SET_1_DRVVBUS_FALL_EN |
  134. USBOTGSS_IRQ_SET_1_IDPULLUP_RISE_EN |
  135. USBOTGSS_IRQ_SET_1_DISCHRGVBUS_RISE_EN |
  136. USBOTGSS_IRQ_SET_1_CHRGVBUS_RISE_EN |
  137. USBOTGSS_IRQ_SET_1_DRVVBUS_RISE_EN |
  138. USBOTGSS_IRQ_SET_1_OEVT_EN);
  139. writel(val, &omap->otg_wrapper->irqenable_set_1);
  140. /* Clear the IRQ status */
  141. val = readl(&omap->otg_wrapper->irqstatus_1);
  142. writel(val, &omap->otg_wrapper->irqstatus_1);
  143. val = readl(&omap->otg_wrapper->irqstatus_0);
  144. writel(val, &omap->otg_wrapper->irqstatus_0);
  145. /* Enable the USB OTG Super speed clocks */
  146. val = (OPTFCLKEN_REFCLK960M | OTG_SS_CLKCTRL_MODULEMODE_HW);
  147. setbits_le32((*prcm)->cm_l3init_usb_otg_ss_clkctrl, val);
  148. };
  149. inline int __board_usb_init(void)
  150. {
  151. return 0;
  152. }
  153. int board_usb_init(void) __attribute__((weak, alias("__board_usb_init")));
  154. static void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
  155. {
  156. clrsetbits_le32(&dwc3_reg->g_ctl,
  157. DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
  158. DWC3_GCTL_PRTCAPDIR(mode));
  159. }
  160. static void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
  161. {
  162. /* Before Resetting PHY, put Core in Reset */
  163. setbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
  164. /* Assert USB3 PHY reset */
  165. setbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
  166. /* Assert USB2 PHY reset */
  167. setbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
  168. mdelay(100);
  169. /* Clear USB3 PHY reset */
  170. clrbits_le32(&dwc3_reg->g_usb3pipectl[0], DWC3_GUSB3PIPECTL_PHYSOFTRST);
  171. /* Clear USB2 PHY reset */
  172. clrbits_le32(&dwc3_reg->g_usb2phycfg, DWC3_GUSB2PHYCFG_PHYSOFTRST);
  173. /* After PHYs are stable we can take Core out of reset state */
  174. clrbits_le32(&dwc3_reg->g_ctl, DWC3_GCTL_CORESOFTRESET);
  175. }
  176. static int dwc3_core_init(struct dwc3 *dwc3_reg)
  177. {
  178. u32 reg;
  179. u32 revision;
  180. unsigned int dwc3_hwparams1;
  181. revision = readl(&dwc3_reg->g_snpsid);
  182. /* This should read as U3 followed by revision number */
  183. if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
  184. puts("this is not a DesignWare USB3 DRD Core\n");
  185. return -1;
  186. }
  187. dwc3_core_soft_reset(dwc3_reg);
  188. dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
  189. reg = readl(&dwc3_reg->g_ctl);
  190. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  191. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  192. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
  193. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  194. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  195. break;
  196. default:
  197. debug("No power optimization available\n");
  198. }
  199. /*
  200. * WORKAROUND: DWC3 revisions <1.90a have a bug
  201. * where the device can fail to connect at SuperSpeed
  202. * and falls back to high-speed mode which causes
  203. * the device to enter a Connect/Disconnect loop
  204. */
  205. if ((revision & DWC3_REVISION_MASK) < 0x190a)
  206. reg |= DWC3_GCTL_U2RSTECN;
  207. writel(reg, &dwc3_reg->g_ctl);
  208. return 0;
  209. }
  210. static int omap_xhci_core_init(struct omap_xhci *omap)
  211. {
  212. int ret = 0;
  213. omap_enable_phy_clocks(omap);
  214. dwc_usb3_phy_init(omap->usb3_phy);
  215. ret = dwc3_core_init(omap->dwc3_reg);
  216. if (ret) {
  217. debug("%s:failed to initialize core\n", __func__);
  218. return ret;
  219. }
  220. /* We are hard-coding DWC3 core to Host Mode */
  221. dwc3_set_mode(omap->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
  222. return ret;
  223. }
  224. static void omap_xhci_core_exit(struct omap_xhci *omap)
  225. {
  226. usb3_phy_power(0);
  227. }
  228. int xhci_hcd_init(int index, struct xhci_hccr **hccr, struct xhci_hcor **hcor)
  229. {
  230. struct omap_xhci *ctx = &omap;
  231. int ret = 0;
  232. ctx->hcd = (struct xhci_hccr *)OMAP_XHCI_BASE;
  233. ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
  234. ctx->usb3_phy = (struct omap_usb3_phy *)OMAP_OCP1_SCP_BASE;
  235. ctx->otg_wrapper = (struct omap_dwc_wrapper *)OMAP_OTG_WRAPPER_BASE;
  236. ret = board_usb_init();
  237. if (ret != 0) {
  238. puts("Failed to initialize board for USB\n");
  239. return ret;
  240. }
  241. ret = omap_xhci_core_init(ctx);
  242. if (ret < 0) {
  243. puts("Failed to initialize xhci\n");
  244. return ret;
  245. }
  246. *hccr = (struct xhci_hccr *)(OMAP_XHCI_BASE);
  247. *hcor = (struct xhci_hcor *)((uint32_t) *hccr
  248. + HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
  249. debug("omap-xhci: init hccr %x and hcor %x hc_length %d\n",
  250. (uint32_t)*hccr, (uint32_t)*hcor,
  251. (uint32_t)HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase)));
  252. return ret;
  253. }
  254. void xhci_hcd_stop(int index)
  255. {
  256. struct omap_xhci *ctx = &omap;
  257. omap_xhci_core_exit(ctx);
  258. }