usb_phy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. #ifdef SOC_IMXRT1170_SERIES
  34. void *USB_EhciPhyGetBase(uint8_t controllerId)
  35. {
  36. void *usbPhyBase = NULL;
  37. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  38. uint32_t instance;
  39. uint32_t newinstance = 0;
  40. uint32_t usbphy_base_temp[] = USBPHY_BASE_ADDRS;
  41. uint32_t usbphy_base[] = USBPHY_BASE_ADDRS;
  42. uint32_t *temp;
  43. if (controllerId < (uint8_t)kUSB_ControllerEhci0)
  44. {
  45. return NULL;
  46. }
  47. if ((controllerId == (uint8_t)kUSB_ControllerEhci0) || (controllerId == (uint8_t)kUSB_ControllerEhci1))
  48. {
  49. controllerId = controllerId - (uint8_t)kUSB_ControllerEhci0;
  50. }
  51. else if ((controllerId == (uint8_t)kUSB_ControllerLpcIp3511Hs0) ||
  52. (controllerId == (uint8_t)kUSB_ControllerLpcIp3511Hs1))
  53. {
  54. controllerId = controllerId - (uint8_t)kUSB_ControllerLpcIp3511Hs0;
  55. }
  56. else if ((controllerId == (uint8_t)kUSB_ControllerIp3516Hs0) || (controllerId == (uint8_t)kUSB_ControllerIp3516Hs1))
  57. {
  58. controllerId = controllerId - (uint8_t)kUSB_ControllerIp3516Hs0;
  59. }
  60. else
  61. {
  62. /*no action*/
  63. }
  64. for (instance = 0; instance < (sizeof(usbphy_base_temp) / sizeof(usbphy_base_temp[0])); instance++)
  65. {
  66. if (0U != usbphy_base_temp[instance])
  67. {
  68. usbphy_base[newinstance++] = usbphy_base_temp[instance];
  69. }
  70. }
  71. if (controllerId > newinstance)
  72. {
  73. return NULL;
  74. }
  75. temp = (uint32_t *)usbphy_base[controllerId];
  76. usbPhyBase = (void *)temp;
  77. #endif
  78. return usbPhyBase;
  79. }
  80. /*!
  81. * @brief ehci phy initialization.
  82. *
  83. * This function initialize ehci phy IP.
  84. *
  85. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  86. * @param[in] freq the external input clock.
  87. * for example: if the external input clock is 16M, the parameter freq should be 16000000.
  88. *
  89. * @retval kStatus_USB_Success cancel successfully.
  90. * @retval kStatus_USB_Error the freq value is incorrect.
  91. */
  92. uint32_t USB_EhciPhyInit(uint8_t controllerId, uint32_t freq, usb_phy_config_struct_t *phyConfig)
  93. {
  94. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  95. USBPHY_Type *usbPhyBase;
  96. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  97. if (NULL == usbPhyBase)
  98. {
  99. return (uint8_t)kStatus_USB_Error;
  100. }
  101. #if ((defined FSL_FEATURE_SOC_ANATOP_COUNT) && (FSL_FEATURE_SOC_ANATOP_COUNT > 0U))
  102. ANATOP->HW_ANADIG_REG_3P0.RW =
  103. (ANATOP->HW_ANADIG_REG_3P0.RW &
  104. (~(ANATOP_HW_ANADIG_REG_3P0_OUTPUT_TRG(0x1F) | ANATOP_HW_ANADIG_REG_3P0_ENABLE_ILIMIT_MASK))) |
  105. ANATOP_HW_ANADIG_REG_3P0_OUTPUT_TRG(0x17) | ANATOP_HW_ANADIG_REG_3P0_ENABLE_LINREG_MASK;
  106. ANATOP->HW_ANADIG_USB2_CHRG_DETECT.SET =
  107. ANATOP_HW_ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B_MASK | ANATOP_HW_ANADIG_USB2_CHRG_DETECT_EN_B_MASK;
  108. #endif
  109. #if (defined USB_ANALOG)
  110. USB_ANALOG->INSTANCE[controllerId - (uint8_t)kUSB_ControllerEhci0].CHRG_DETECT_SET =
  111. USB_ANALOG_CHRG_DETECT_CHK_CHRG_B(1) | USB_ANALOG_CHRG_DETECT_EN_B(1);
  112. #endif
  113. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  114. usbPhyBase->TRIM_OVERRIDE_EN = 0x001fU; /* override IFR value */
  115. #endif
  116. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL2_MASK; /* support LS device. */
  117. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL3_MASK; /* support external FS Hub with LS device connected. */
  118. /* PWD register provides overall control of the PHY power state */
  119. usbPhyBase->PWD = 0U;
  120. if (((uint8_t)kUSB_ControllerIp3516Hs0 == controllerId) || ((uint8_t)kUSB_ControllerIp3516Hs1 == controllerId) ||
  121. ((uint8_t)kUSB_ControllerLpcIp3511Hs0 == controllerId) ||
  122. ((uint8_t)kUSB_ControllerLpcIp3511Hs1 == controllerId))
  123. {
  124. usbPhyBase->CTRL_SET = USBPHY_CTRL_SET_ENAUTOCLR_CLKGATE_MASK;
  125. usbPhyBase->CTRL_SET = USBPHY_CTRL_SET_ENAUTOCLR_PHY_PWD_MASK;
  126. }
  127. if (NULL != phyConfig)
  128. {
  129. /* Decode to trim the nominal 17.78mA current source for the High Speed TX drivers on USB_DP and USB_DM. */
  130. usbPhyBase->TX =
  131. ((usbPhyBase->TX & (~(USBPHY_TX_D_CAL_MASK | USBPHY_TX_TXCAL45DM_MASK | USBPHY_TX_TXCAL45DP_MASK))) |
  132. (USBPHY_TX_D_CAL(phyConfig->D_CAL) | USBPHY_TX_TXCAL45DP(phyConfig->TXCAL45DP) |
  133. USBPHY_TX_TXCAL45DM(phyConfig->TXCAL45DM)));
  134. }
  135. #endif
  136. return (uint8_t)kStatus_USB_Success;
  137. }
  138. /*!
  139. * @brief ehci phy initialization for suspend and resume.
  140. *
  141. * This function initialize ehci phy IP for suspend and resume.
  142. *
  143. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  144. * @param[in] freq the external input clock.
  145. * for example: if the external input clock is 16M, the parameter freq should be 16000000.
  146. *
  147. * @retval kStatus_USB_Success cancel successfully.
  148. * @retval kStatus_USB_Error the freq value is incorrect.
  149. */
  150. uint32_t USB_EhciLowPowerPhyInit(uint8_t controllerId, uint32_t freq, usb_phy_config_struct_t *phyConfig)
  151. {
  152. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  153. USBPHY_Type *usbPhyBase;
  154. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  155. if (NULL == usbPhyBase)
  156. {
  157. return (uint8_t)kStatus_USB_Error;
  158. }
  159. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  160. usbPhyBase->TRIM_OVERRIDE_EN = 0x001fU; /* override IFR value */
  161. #endif
  162. #if ((defined USBPHY_CTRL_AUTORESUME_EN_MASK) && (USBPHY_CTRL_AUTORESUME_EN_MASK > 0U))
  163. usbPhyBase->CTRL_CLR |= USBPHY_CTRL_AUTORESUME_EN_MASK;
  164. #else
  165. usbPhyBase->CTRL |= USBPHY_CTRL_ENAUTO_PWRON_PLL_MASK;
  166. #endif
  167. usbPhyBase->CTRL |= USBPHY_CTRL_ENAUTOCLR_CLKGATE_MASK | USBPHY_CTRL_ENAUTOCLR_PHY_PWD_MASK;
  168. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL2_MASK; /* support LS device. */
  169. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL3_MASK; /* support external FS Hub with LS device connected. */
  170. /* PWD register provides overall control of the PHY power state */
  171. usbPhyBase->PWD = 0U;
  172. #if (defined USBPHY_ANACTRL_PFD_CLKGATE_MASK)
  173. /* now the 480MHz USB clock is up, then configure fractional divider after PLL with PFD
  174. * pfd clock = 480MHz*18/N, where N=18~35
  175. * Please note that USB1PFDCLK has to be less than 180MHz for RUN or HSRUN mode
  176. */
  177. usbPhyBase->ANACTRL |= USBPHY_ANACTRL_PFD_FRAC(24); /* N=24 */
  178. usbPhyBase->ANACTRL |= USBPHY_ANACTRL_PFD_CLK_SEL(1); /* div by 4 */
  179. usbPhyBase->ANACTRL &= ~USBPHY_ANACTRL_DEV_PULLDOWN_MASK;
  180. usbPhyBase->ANACTRL &= ~USBPHY_ANACTRL_PFD_CLKGATE_MASK;
  181. while (0U == (usbPhyBase->ANACTRL & USBPHY_ANACTRL_PFD_STABLE_MASK))
  182. {
  183. }
  184. #endif
  185. if (NULL != phyConfig)
  186. {
  187. /* Decode to trim the nominal 17.78mA current source for the High Speed TX drivers on USB_DP and USB_DM. */
  188. usbPhyBase->TX =
  189. ((usbPhyBase->TX & (~(USBPHY_TX_D_CAL_MASK | USBPHY_TX_TXCAL45DM_MASK | USBPHY_TX_TXCAL45DP_MASK))) |
  190. (USBPHY_TX_D_CAL(phyConfig->D_CAL) | USBPHY_TX_TXCAL45DP(phyConfig->TXCAL45DP) |
  191. USBPHY_TX_TXCAL45DM(phyConfig->TXCAL45DM)));
  192. }
  193. #endif
  194. return (uint8_t)kStatus_USB_Success;
  195. }
  196. /*!
  197. * @brief ehci phy de-initialization.
  198. *
  199. * This function de-initialize ehci phy IP.
  200. *
  201. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  202. */
  203. void USB_EhciPhyDeinit(uint8_t controllerId)
  204. {
  205. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  206. USBPHY_Type *usbPhyBase;
  207. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  208. if (NULL == usbPhyBase)
  209. {
  210. return;
  211. }
  212. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  213. usbPhyBase->PLL_SIC &= ~USBPHY_PLL_SIC_PLL_POWER_MASK; /* power down PLL */
  214. usbPhyBase->PLL_SIC &= ~USBPHY_PLL_SIC_PLL_EN_USB_CLKS_MASK; /* disable USB clock output from USB PHY PLL */
  215. #endif
  216. usbPhyBase->CTRL |= USBPHY_CTRL_CLKGATE_MASK; /* set to 1U to gate clocks */
  217. #endif
  218. }
  219. /*!
  220. * @brief ehci phy disconnect detection enable or disable.
  221. *
  222. * This function enable/disable host ehci disconnect detection.
  223. *
  224. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  225. * @param[in] enable
  226. * 1U - enable;
  227. * 0U - disable;
  228. */
  229. void USB_EhcihostPhyDisconnectDetectCmd(uint8_t controllerId, uint8_t enable)
  230. {
  231. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  232. USBPHY_Type *usbPhyBase;
  233. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  234. if (NULL == usbPhyBase)
  235. {
  236. return;
  237. }
  238. if (0U != enable)
  239. {
  240. usbPhyBase->CTRL |= USBPHY_CTRL_ENHOSTDISCONDETECT_MASK;
  241. }
  242. else
  243. {
  244. usbPhyBase->CTRL &= (~USBPHY_CTRL_ENHOSTDISCONDETECT_MASK);
  245. }
  246. #endif
  247. }
  248. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  249. #if ((defined FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE) && (FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE > 0U))
  250. void USB_PhyDeviceForceEnterFSMode(uint8_t controllerId, uint8_t enable)
  251. {
  252. USBPHY_Type *usbPhyBase;
  253. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  254. if (NULL == usbPhyBase)
  255. {
  256. return;
  257. }
  258. if (0U != enable)
  259. {
  260. uint32_t delay = 1000000;
  261. usbPhyBase->DEBUG0_CLR = USBPHY_DEBUG0_CLKGATE_MASK;
  262. while ((0U != (usbPhyBase->USB1_VBUS_DET_STAT & USBPHY_USB1_VBUS_DET_STAT_VBUS_VALID_3V_MASK)) && (0U != delay))
  263. {
  264. delay--;
  265. }
  266. usbPhyBase->USB1_LOOPBACK_SET = USBPHY_USB1_LOOPBACK_UTMI_TESTSTART_MASK;
  267. }
  268. else
  269. {
  270. usbPhyBase->DEBUG0_CLR = USBPHY_DEBUG0_CLKGATE_MASK;
  271. usbPhyBase->USB1_LOOPBACK_CLR = USBPHY_USB1_LOOPBACK_UTMI_TESTSTART_MASK;
  272. }
  273. }
  274. #endif
  275. #endif
  276. #else
  277. void *USB_EhciPhyGetBase(uint8_t controllerId)
  278. {
  279. void *usbPhyBase = NULL;
  280. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  281. uint32_t instance;
  282. uint32_t newinstance = 0;
  283. uint32_t usbphy_base_temp[] = USBPHY_BASE_ADDRS;
  284. uint32_t usbphy_base[] = USBPHY_BASE_ADDRS;
  285. if (controllerId < kUSB_ControllerEhci0)
  286. {
  287. return NULL;
  288. }
  289. controllerId = controllerId - kUSB_ControllerEhci0;
  290. for (instance = 0; instance < (sizeof(usbphy_base_temp) / sizeof(usbphy_base_temp[0])); instance++)
  291. {
  292. if (usbphy_base_temp[instance])
  293. {
  294. usbphy_base[newinstance++] = usbphy_base_temp[instance];
  295. }
  296. }
  297. if (controllerId > newinstance)
  298. {
  299. return NULL;
  300. }
  301. usbPhyBase = (void *)usbphy_base[controllerId];
  302. #endif
  303. return usbPhyBase;
  304. }
  305. /*!
  306. * @brief ehci phy initialization.
  307. *
  308. * This function initialize ehci phy IP.
  309. *
  310. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  311. * @param[in] freq the external input clock.
  312. * for example: if the external input clock is 16M, the parameter freq should be 16000000.
  313. *
  314. * @retval kStatus_USB_Success cancel successfully.
  315. * @retval kStatus_USB_Error the freq value is incorrect.
  316. */
  317. uint32_t USB_EhciPhyInit(uint8_t controllerId, uint32_t freq, usb_phy_config_struct_t *phyConfig)
  318. {
  319. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  320. USBPHY_Type *usbPhyBase;
  321. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  322. if (NULL == usbPhyBase)
  323. {
  324. return kStatus_USB_Error;
  325. }
  326. #if ((defined FSL_FEATURE_SOC_ANATOP_COUNT) && (FSL_FEATURE_SOC_ANATOP_COUNT > 0U))
  327. ANATOP->HW_ANADIG_REG_3P0.RW =
  328. (ANATOP->HW_ANADIG_REG_3P0.RW &
  329. (~(ANATOP_HW_ANADIG_REG_3P0_OUTPUT_TRG(0x1F) | ANATOP_HW_ANADIG_REG_3P0_ENABLE_ILIMIT_MASK))) |
  330. ANATOP_HW_ANADIG_REG_3P0_OUTPUT_TRG(0x17) | ANATOP_HW_ANADIG_REG_3P0_ENABLE_LINREG_MASK;
  331. ANATOP->HW_ANADIG_USB2_CHRG_DETECT.SET =
  332. ANATOP_HW_ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B_MASK | ANATOP_HW_ANADIG_USB2_CHRG_DETECT_EN_B_MASK;
  333. #endif
  334. #if (defined USB_ANALOG)
  335. USB_ANALOG->INSTANCE[controllerId - kUSB_ControllerEhci0].CHRG_DETECT_SET = USB_ANALOG_CHRG_DETECT_CHK_CHRG_B(1) | USB_ANALOG_CHRG_DETECT_EN_B(1);
  336. #endif
  337. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  338. usbPhyBase->TRIM_OVERRIDE_EN = 0x001fU; /* override IFR value */
  339. #endif
  340. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL2_MASK; /* support LS device. */
  341. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL3_MASK; /* support external FS Hub with LS device connected. */
  342. /* PWD register provides overall control of the PHY power state */
  343. usbPhyBase->PWD = 0U;
  344. /* Decode to trim the nominal 17.78mA current source for the High Speed TX drivers on USB_DP and USB_DM. */
  345. usbPhyBase->TX =
  346. ((usbPhyBase->TX & (~(USBPHY_TX_D_CAL_MASK | USBPHY_TX_TXCAL45DM_MASK | USBPHY_TX_TXCAL45DP_MASK))) |
  347. (USBPHY_TX_D_CAL(phyConfig->D_CAL) | USBPHY_TX_TXCAL45DP(phyConfig->TXCAL45DP) |
  348. USBPHY_TX_TXCAL45DM(phyConfig->TXCAL45DM)));
  349. #endif
  350. return kStatus_USB_Success;
  351. }
  352. /*!
  353. * @brief ehci phy initialization for suspend and resume.
  354. *
  355. * This function initialize ehci phy IP for suspend and resume.
  356. *
  357. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  358. * @param[in] freq the external input clock.
  359. * for example: if the external input clock is 16M, the parameter freq should be 16000000.
  360. *
  361. * @retval kStatus_USB_Success cancel successfully.
  362. * @retval kStatus_USB_Error the freq value is incorrect.
  363. */
  364. uint32_t USB_EhciLowPowerPhyInit(uint8_t controllerId, uint32_t freq, usb_phy_config_struct_t *phyConfig)
  365. {
  366. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  367. USBPHY_Type *usbPhyBase;
  368. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  369. if (NULL == usbPhyBase)
  370. {
  371. return kStatus_USB_Error;
  372. }
  373. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  374. usbPhyBase->TRIM_OVERRIDE_EN = 0x001fU; /* override IFR value */
  375. #endif
  376. #if ((defined USBPHY_CTRL_AUTORESUME_EN_MASK) && (USBPHY_CTRL_AUTORESUME_EN_MASK > 0U))
  377. usbPhyBase->CTRL |= USBPHY_CTRL_AUTORESUME_EN_MASK;
  378. #else
  379. usbPhyBase->CTRL |= USBPHY_CTRL_ENAUTO_PWRON_PLL_MASK;
  380. #endif
  381. usbPhyBase->CTRL |= USBPHY_CTRL_ENAUTOCLR_CLKGATE_MASK | USBPHY_CTRL_ENAUTOCLR_PHY_PWD_MASK;
  382. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL2_MASK; /* support LS device. */
  383. usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL3_MASK; /* support external FS Hub with LS device connected. */
  384. /* PWD register provides overall control of the PHY power state */
  385. usbPhyBase->PWD = 0U;
  386. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  387. /* now the 480MHz USB clock is up, then configure fractional divider after PLL with PFD
  388. * pfd clock = 480MHz*18/N, where N=18~35
  389. * Please note that USB1PFDCLK has to be less than 180MHz for RUN or HSRUN mode
  390. */
  391. usbPhyBase->ANACTRL |= USBPHY_ANACTRL_PFD_FRAC(24); /* N=24 */
  392. usbPhyBase->ANACTRL |= USBPHY_ANACTRL_PFD_CLK_SEL(1); /* div by 4 */
  393. usbPhyBase->ANACTRL &= ~USBPHY_ANACTRL_DEV_PULLDOWN_MASK;
  394. usbPhyBase->ANACTRL &= ~USBPHY_ANACTRL_PFD_CLKGATE_MASK;
  395. while (!(usbPhyBase->ANACTRL & USBPHY_ANACTRL_PFD_STABLE_MASK))
  396. {
  397. }
  398. #endif
  399. /* Decode to trim the nominal 17.78mA current source for the High Speed TX drivers on USB_DP and USB_DM. */
  400. usbPhyBase->TX =
  401. ((usbPhyBase->TX & (~(USBPHY_TX_D_CAL_MASK | USBPHY_TX_TXCAL45DM_MASK | USBPHY_TX_TXCAL45DP_MASK))) |
  402. (USBPHY_TX_D_CAL(phyConfig->D_CAL) | USBPHY_TX_TXCAL45DP(phyConfig->TXCAL45DP) |
  403. USBPHY_TX_TXCAL45DM(phyConfig->TXCAL45DM)));
  404. #endif
  405. return kStatus_USB_Success;
  406. }
  407. /*!
  408. * @brief ehci phy de-initialization.
  409. *
  410. * This function de-initialize ehci phy IP.
  411. *
  412. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  413. */
  414. void USB_EhciPhyDeinit(uint8_t controllerId)
  415. {
  416. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  417. USBPHY_Type *usbPhyBase;
  418. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  419. if (NULL == usbPhyBase)
  420. {
  421. return;
  422. }
  423. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))
  424. usbPhyBase->PLL_SIC &= ~USBPHY_PLL_SIC_PLL_POWER_MASK; /* power down PLL */
  425. usbPhyBase->PLL_SIC &= ~USBPHY_PLL_SIC_PLL_EN_USB_CLKS_MASK; /* disable USB clock output from USB PHY PLL */
  426. #endif
  427. usbPhyBase->CTRL |= USBPHY_CTRL_CLKGATE_MASK; /* set to 1U to gate clocks */
  428. #endif
  429. }
  430. /*!
  431. * @brief ehci phy disconnect detection enable or disable.
  432. *
  433. * This function enable/disable host ehci disconnect detection.
  434. *
  435. * @param[in] controllerId ehci controller id, please reference to #usb_controller_index_t.
  436. * @param[in] enable
  437. * 1U - enable;
  438. * 0U - disable;
  439. */
  440. void USB_EhcihostPhyDisconnectDetectCmd(uint8_t controllerId, uint8_t enable)
  441. {
  442. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  443. USBPHY_Type *usbPhyBase;
  444. usbPhyBase = (USBPHY_Type *)USB_EhciPhyGetBase(controllerId);
  445. if (NULL == usbPhyBase)
  446. {
  447. return;
  448. }
  449. if (enable)
  450. {
  451. usbPhyBase->CTRL |= USBPHY_CTRL_ENHOSTDISCONDETECT_MASK;
  452. }
  453. else
  454. {
  455. usbPhyBase->CTRL &= (~USBPHY_CTRL_ENHOSTDISCONDETECT_MASK);
  456. }
  457. #endif
  458. }
  459. #endif