usb_phy.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016 - 2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <usb/include/usb.h>
  31. #include "fsl_device_registers.h"
  32. #include <usb/phy/usb_phy.h>
  33. void *USB_EhciPhyGetBase(uint8_t controllerId)
  34. {
  35. void *usbPhyBase = NULL;
  36. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  37. uint32_t instance;
  38. uint32_t newinstance = 0;
  39. uint32_t usbphy_base_temp[] = USBPHY_BASE_ADDRS;
  40. uint32_t usbphy_base[] = USBPHY_BASE_ADDRS;
  41. if (controllerId < kUSB_ControllerEhci0)
  42. {
  43. return NULL;
  44. }
  45. controllerId = controllerId - kUSB_ControllerEhci0;
  46. for (instance = 0; instance < (sizeof(usbphy_base_temp) / sizeof(usbphy_base_temp[0])); instance++)
  47. {
  48. if (usbphy_base_temp[instance])
  49. {
  50. usbphy_base[newinstance++] = usbphy_base_temp[instance];
  51. }
  52. }
  53. if (controllerId > newinstance)
  54. {
  55. return NULL;
  56. }
  57. usbPhyBase = (void *)usbphy_base[controllerId];
  58. #endif
  59. return usbPhyBase;
  60. }
  61. /*!
  62. * @brief ehci phy initialization.
  63. *
  64. * This function initialize ehci phy IP.
  65. *
  66. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  67. * @param[in] freq the external input clock.
  68. * for example: if the external input clock is 16M, the parameter freq should be 16000000.
  69. *
  70. * @retval kStatus_USB_Success cancel successfully.
  71. * @retval kStatus_USB_Error the freq value is incorrect.
  72. */
  73. uint32_t USB_EhciPhyInit(uint8_t controllerId, uint32_t freq, usb_phy_config_struct_t *phyConfig)
  74. {
  75. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  76. USBPHY_Type *usbPhyBase;
  77. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  78. if (NULL == usbPhyBase)
  79. {
  80. return kStatus_USB_Error;
  81. }
  82. #if ((defined FSL_FEATURE_SOC_ANATOP_COUNT) && (FSL_FEATURE_SOC_ANATOP_COUNT > 0U))
  83. ANATOP->HW_ANADIG_REG_3P0.RW =
  84. (ANATOP->HW_ANADIG_REG_3P0.RW &
  85. (~(ANATOP_HW_ANADIG_REG_3P0_OUTPUT_TRG(0x1F) | ANATOP_HW_ANADIG_REG_3P0_ENABLE_ILIMIT_MASK))) |
  86. ANATOP_HW_ANADIG_REG_3P0_OUTPUT_TRG(0x17) | ANATOP_HW_ANADIG_REG_3P0_ENABLE_LINREG_MASK;
  87. ANATOP->HW_ANADIG_USB2_CHRG_DETECT.SET =
  88. ANATOP_HW_ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B_MASK | ANATOP_HW_ANADIG_USB2_CHRG_DETECT_EN_B_MASK;
  89. #endif
  90. #if (defined USB_ANALOG)
  91. USB_ANALOG->INSTANCE[controllerId - kUSB_ControllerEhci0].CHRG_DETECT_SET = USB_ANALOG_CHRG_DETECT_CHK_CHRG_B(1) | USB_ANALOG_CHRG_DETECT_EN_B(1);
  92. #endif
  93. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  94. usbPhyBase->TRIM_OVERRIDE_EN = 0x001fU; /* override IFR value */
  95. #endif
  96. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL2_MASK; /* support LS device. */
  97. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL3_MASK; /* support external FS Hub with LS device connected. */
  98. /* PWD register provides overall control of the PHY power state */
  99. usbPhyBase->PWD = 0U;
  100. /* Decode to trim the nominal 17.78mA current source for the High Speed TX drivers on USB_DP and USB_DM. */
  101. usbPhyBase->TX =
  102. ((usbPhyBase->TX & (~(USBPHY_TX_D_CAL_MASK | USBPHY_TX_TXCAL45DM_MASK | USBPHY_TX_TXCAL45DP_MASK))) |
  103. (USBPHY_TX_D_CAL(phyConfig->D_CAL) | USBPHY_TX_TXCAL45DP(phyConfig->TXCAL45DP) |
  104. USBPHY_TX_TXCAL45DM(phyConfig->TXCAL45DM)));
  105. #endif
  106. return kStatus_USB_Success;
  107. }
  108. /*!
  109. * @brief ehci phy initialization for suspend and resume.
  110. *
  111. * This function initialize ehci phy IP for suspend and resume.
  112. *
  113. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  114. * @param[in] freq the external input clock.
  115. * for example: if the external input clock is 16M, the parameter freq should be 16000000.
  116. *
  117. * @retval kStatus_USB_Success cancel successfully.
  118. * @retval kStatus_USB_Error the freq value is incorrect.
  119. */
  120. uint32_t USB_EhciLowPowerPhyInit(uint8_t controllerId, uint32_t freq, usb_phy_config_struct_t *phyConfig)
  121. {
  122. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  123. USBPHY_Type *usbPhyBase;
  124. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  125. if (NULL == usbPhyBase)
  126. {
  127. return kStatus_USB_Error;
  128. }
  129. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  130. usbPhyBase->TRIM_OVERRIDE_EN = 0x001fU; /* override IFR value */
  131. #endif
  132. #if ((defined USBPHY_CTRL_AUTORESUME_EN_MASK) && (USBPHY_CTRL_AUTORESUME_EN_MASK > 0U))
  133. usbPhyBase->CTRL |= USBPHY_CTRL_AUTORESUME_EN_MASK;
  134. #else
  135. usbPhyBase->CTRL |= USBPHY_CTRL_ENAUTO_PWRON_PLL_MASK;
  136. #endif
  137. usbPhyBase->CTRL |= USBPHY_CTRL_ENAUTOCLR_CLKGATE_MASK | USBPHY_CTRL_ENAUTOCLR_PHY_PWD_MASK;
  138. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL2_MASK; /* support LS device. */
  139. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL3_MASK; /* support external FS Hub with LS device connected. */
  140. /* PWD register provides overall control of the PHY power state */
  141. usbPhyBase->PWD = 0U;
  142. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  143. /* now the 480MHz USB clock is up, then configure fractional divider after PLL with PFD
  144. * pfd clock = 480MHz*18/N, where N=18~35
  145. * Please note that USB1PFDCLK has to be less than 180MHz for RUN or HSRUN mode
  146. */
  147. usbPhyBase->ANACTRL |= USBPHY_ANACTRL_PFD_FRAC(24); /* N=24 */
  148. usbPhyBase->ANACTRL |= USBPHY_ANACTRL_PFD_CLK_SEL(1); /* div by 4 */
  149. usbPhyBase->ANACTRL &= ~USBPHY_ANACTRL_DEV_PULLDOWN_MASK;
  150. usbPhyBase->ANACTRL &= ~USBPHY_ANACTRL_PFD_CLKGATE_MASK;
  151. while (!(usbPhyBase->ANACTRL & USBPHY_ANACTRL_PFD_STABLE_MASK))
  152. {
  153. }
  154. #endif
  155. /* Decode to trim the nominal 17.78mA current source for the High Speed TX drivers on USB_DP and USB_DM. */
  156. usbPhyBase->TX =
  157. ((usbPhyBase->TX & (~(USBPHY_TX_D_CAL_MASK | USBPHY_TX_TXCAL45DM_MASK | USBPHY_TX_TXCAL45DP_MASK))) |
  158. (USBPHY_TX_D_CAL(phyConfig->D_CAL) | USBPHY_TX_TXCAL45DP(phyConfig->TXCAL45DP) |
  159. USBPHY_TX_TXCAL45DM(phyConfig->TXCAL45DM)));
  160. #endif
  161. return kStatus_USB_Success;
  162. }
  163. /*!
  164. * @brief ehci phy de-initialization.
  165. *
  166. * This function de-initialize ehci phy IP.
  167. *
  168. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  169. */
  170. void USB_EhciPhyDeinit(uint8_t controllerId)
  171. {
  172. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  173. USBPHY_Type *usbPhyBase;
  174. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  175. if (NULL == usbPhyBase)
  176. {
  177. return;
  178. }
  179. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  180. usbPhyBase->PLL_SIC &= ~USBPHY_PLL_SIC_PLL_POWER_MASK; /* power down PLL */
  181. usbPhyBase->PLL_SIC &= ~USBPHY_PLL_SIC_PLL_EN_USB_CLKS_MASK; /* disable USB clock output from USB PHY PLL */
  182. #endif
  183. usbPhyBase->CTRL |= USBPHY_CTRL_CLKGATE_MASK; /* set to 1U to gate clocks */
  184. #endif
  185. }
  186. /*!
  187. * @brief ehci phy disconnect detection enable or disable.
  188. *
  189. * This function enable/disable host ehci disconnect detection.
  190. *
  191. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  192. * @param[in] enable
  193. * 1U - enable;
  194. * 0U - disable;
  195. */
  196. void USB_EhcihostPhyDisconnectDetectCmd(uint8_t controllerId, uint8_t enable)
  197. {
  198. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  199. USBPHY_Type *usbPhyBase;
  200. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  201. if (NULL == usbPhyBase)
  202. {
  203. return;
  204. }
  205. if (enable)
  206. {
  207. usbPhyBase->CTRL |= USBPHY_CTRL_ENHOSTDISCONDETECT_MASK;
  208. }
  209. else
  210. {
  211. usbPhyBase->CTRL &= (~USBPHY_CTRL_ENHOSTDISCONDETECT_MASK);
  212. }
  213. #endif
  214. }