usb_glue_st.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usb_config.h"
  7. #include "stdint.h"
  8. #include "usb_dwc2_reg.h"
  9. /* you can find this config in function: USB_DevInit, file:stm32xxx_ll_usb.c, for example:
  10. *
  11. * USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  12. * USBx->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
  13. * USBx->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
  14. * USBx->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
  15. *
  16. */
  17. #if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F730xx) || defined(STM32F732xx) || defined(STM32F733xx)
  18. /**
  19. * @brief USB_HS_PHY_Registers
  20. */
  21. typedef struct
  22. {
  23. __IO uint32_t USB_HS_PHYC_PLL; /*!< This register is used to control the PLL of the HS PHY. 000h */
  24. __IO uint32_t Reserved04; /*!< Reserved 004h */
  25. __IO uint32_t Reserved08; /*!< Reserved 008h */
  26. __IO uint32_t USB_HS_PHYC_TUNE; /*!< This register is used to control the tuning interface of the High Speed PHY. 00Ch */
  27. __IO uint32_t Reserved10; /*!< Reserved 010h */
  28. __IO uint32_t Reserved14; /*!< Reserved 014h */
  29. __IO uint32_t USB_HS_PHYC_LDO; /*!< This register is used to control the regulator (LDO). 018h */
  30. } USB_HS_PHYC_GlobalTypeDef;
  31. #define USB_HS_PHYC_CONTROLLER_BASE 0x40017C00UL
  32. #define USB_HS_PHYC ((USB_HS_PHYC_GlobalTypeDef *) USB_HS_PHYC_CONTROLLER_BASE)
  33. /******************** Bit definition for USBPHYC_PLL1 register ********************/
  34. #define USB_HS_PHYC_PLL1_PLLEN_Pos (0U)
  35. #define USB_HS_PHYC_PLL1_PLLEN_Msk (0x1UL << USB_HS_PHYC_PLL1_PLLEN_Pos) /*!< 0x00000001 */
  36. #define USB_HS_PHYC_PLL1_PLLEN USB_HS_PHYC_PLL1_PLLEN_Msk /*!< Enable PLL */
  37. #define USB_HS_PHYC_PLL1_PLLSEL_Pos (1U)
  38. #define USB_HS_PHYC_PLL1_PLLSEL_Msk (0x7UL << USB_HS_PHYC_PLL1_PLLSEL_Pos) /*!< 0x0000000E */
  39. #define USB_HS_PHYC_PLL1_PLLSEL USB_HS_PHYC_PLL1_PLLSEL_Msk /*!< Controls PHY frequency operation selection */
  40. #define USB_HS_PHYC_PLL1_PLLSEL_1 (0x1UL << USB_HS_PHYC_PLL1_PLLSEL_Pos) /*!< 0x00000002 */
  41. #define USB_HS_PHYC_PLL1_PLLSEL_2 (0x2UL << USB_HS_PHYC_PLL1_PLLSEL_Pos) /*!< 0x00000004 */
  42. #define USB_HS_PHYC_PLL1_PLLSEL_3 (0x4UL << USB_HS_PHYC_PLL1_PLLSEL_Pos) /*!< 0x00000008 */
  43. #define USB_HS_PHYC_PLL1_PLLSEL_12MHZ 0x00000000U /*!< PHY PLL1 input clock frequency 12 MHz */
  44. #define USB_HS_PHYC_PLL1_PLLSEL_12_5MHZ USB_HS_PHYC_PLL1_PLLSEL_1 /*!< PHY PLL1 input clock frequency 12.5 MHz */
  45. #define USB_HS_PHYC_PLL1_PLLSEL_16MHZ (uint32_t)(USB_HS_PHYC_PLL1_PLLSEL_1 | USB_HS_PHYC_PLL1_PLLSEL_2) /*!< PHY PLL1 input clock frequency 16 MHz */
  46. #define USB_HS_PHYC_PLL1_PLLSEL_24MHZ USB_HS_PHYC_PLL1_PLLSEL_3 /*!< PHY PLL1 input clock frequency 24 MHz */
  47. #define USB_HS_PHYC_PLL1_PLLSEL_25MHZ (uint32_t)(USB_HS_PHYC_PLL1_PLLSEL_2 | USB_HS_PHYC_PLL1_PLLSEL_3) /*!< PHY PLL1 input clock frequency 25 MHz */
  48. /******************** Bit definition for USBPHYC_LDO register ********************/
  49. #define USB_HS_PHYC_LDO_USED_Pos (0U)
  50. #define USB_HS_PHYC_LDO_USED_Msk (0x1UL << USB_HS_PHYC_LDO_USED_Pos) /*!< 0x00000001 */
  51. #define USB_HS_PHYC_LDO_USED USB_HS_PHYC_LDO_USED_Msk /*!< Monitors the usage status of the PHY's LDO */
  52. #define USB_HS_PHYC_LDO_STATUS_Pos (1U)
  53. #define USB_HS_PHYC_LDO_STATUS_Msk (0x1UL << USB_HS_PHYC_LDO_STATUS_Pos) /*!< 0x00000002 */
  54. #define USB_HS_PHYC_LDO_STATUS USB_HS_PHYC_LDO_STATUS_Msk /*!< Monitors the status of the PHY's LDO. */
  55. #define USB_HS_PHYC_LDO_DISABLE_Pos (2U)
  56. #define USB_HS_PHYC_LDO_DISABLE_Msk (0x1UL << USB_HS_PHYC_LDO_DISABLE_Pos) /*!< 0x00000004 */
  57. #define USB_HS_PHYC_LDO_DISABLE USB_HS_PHYC_LDO_DISABLE_Msk /*!< Controls disable of the High Speed PHY's LDO */
  58. /* Legacy */
  59. #define USB_HS_PHYC_PLL_PLLEN_Pos USB_HS_PHYC_PLL1_PLLEN_Pos
  60. #define USB_HS_PHYC_PLL_PLLEN_Msk USB_HS_PHYC_PLL1_PLLEN_Msk
  61. #define USB_HS_PHYC_PLL_PLLEN USB_HS_PHYC_PLL1_PLLEN
  62. #define USB_HS_PHYC_PLL_PLLSEL_Pos USB_HS_PHYC_PLL1_PLLSEL_Pos
  63. #define USB_HS_PHYC_PLL_PLLSEL_Msk USB_HS_PHYC_PLL1_PLLSEL_Msk
  64. #define USB_HS_PHYC_PLL_PLLSEL USB_HS_PHYC_PLL1_PLLSEL
  65. #define USB_HS_PHYC_PLL_PLLSEL_1 USB_HS_PHYC_PLL1_PLLSEL_1
  66. #define USB_HS_PHYC_PLL_PLLSEL_2 USB_HS_PHYC_PLL1_PLLSEL_2
  67. #define USB_HS_PHYC_PLL_PLLSEL_3 USB_HS_PHYC_PLL1_PLLSEL_3
  68. #define USB_HS_PHYC_LDO_ENABLE_Pos USB_HS_PHYC_LDO_DISABLE_Pos
  69. #define USB_HS_PHYC_LDO_ENABLE_Msk USB_HS_PHYC_LDO_DISABLE_Msk
  70. #define USB_HS_PHYC_LDO_ENABLE USB_HS_PHYC_LDO_DISABLE
  71. #if !defined (USB_HS_PHYC_TUNE_VALUE)
  72. #define USB_HS_PHYC_TUNE_VALUE 0x00000F13U /*!< Value of USB HS PHY Tune */
  73. #endif /* USB_HS_PHYC_TUNE_VALUE */
  74. /**
  75. * @brief Enables control of a High Speed USB PHY
  76. * Init the low level hardware : GPIO, CLOCK, NVIC...
  77. * @param USBx Selected device
  78. * @retval HAL status
  79. */
  80. static int usb_hsphy_init(uint32_t hse_value)
  81. {
  82. __IO uint32_t count = 0U;
  83. /* Enable LDO */
  84. USB_HS_PHYC->USB_HS_PHYC_LDO |= USB_HS_PHYC_LDO_ENABLE;
  85. /* wait for LDO Ready */
  86. while ((USB_HS_PHYC->USB_HS_PHYC_LDO & USB_HS_PHYC_LDO_STATUS) == 0U)
  87. {
  88. count++;
  89. if (count > 200000U)
  90. {
  91. return -1;
  92. }
  93. }
  94. /* Controls PHY frequency operation selection */
  95. if (hse_value == 12000000U) /* HSE = 12MHz */
  96. {
  97. USB_HS_PHYC->USB_HS_PHYC_PLL = (0x0U << 1);
  98. }
  99. else if (hse_value == 12500000U) /* HSE = 12.5MHz */
  100. {
  101. USB_HS_PHYC->USB_HS_PHYC_PLL = (0x2U << 1);
  102. }
  103. else if (hse_value == 16000000U) /* HSE = 16MHz */
  104. {
  105. USB_HS_PHYC->USB_HS_PHYC_PLL = (0x3U << 1);
  106. }
  107. else if (hse_value == 24000000U) /* HSE = 24MHz */
  108. {
  109. USB_HS_PHYC->USB_HS_PHYC_PLL = (0x4U << 1);
  110. }
  111. else if (hse_value == 25000000U) /* HSE = 25MHz */
  112. {
  113. USB_HS_PHYC->USB_HS_PHYC_PLL = (0x5U << 1);
  114. }
  115. else if (hse_value == 32000000U) /* HSE = 32MHz */
  116. {
  117. USB_HS_PHYC->USB_HS_PHYC_PLL = (0x7U << 1);
  118. }
  119. else
  120. {
  121. /* ... */
  122. }
  123. /* Control the tuning interface of the High Speed PHY */
  124. USB_HS_PHYC->USB_HS_PHYC_TUNE |= USB_HS_PHYC_TUNE_VALUE;
  125. /* Enable PLL internal PHY */
  126. USB_HS_PHYC->USB_HS_PHYC_PLL |= USB_HS_PHYC_PLL_PLLEN;
  127. /* 2ms Delay required to get internal phy clock stable */
  128. HAL_Delay(2U);
  129. return 0;
  130. }
  131. #endif
  132. uint32_t usbd_get_dwc2_gccfg_conf(uint32_t reg_base)
  133. {
  134. #if __has_include("stm32h7xx.h") || __has_include("stm32f7xx.h") || __has_include("stm32l4xx.h")
  135. #define USB_OTG_GLB ((DWC2_GlobalTypeDef *)(reg_base))
  136. /* B-peripheral session valid override enable */
  137. USB_OTG_GLB->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN;
  138. USB_OTG_GLB->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL;
  139. #endif
  140. #ifdef CONFIG_USB_HS
  141. #if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F730xx) || defined(STM32F732xx) || defined(STM32F733xx)
  142. USB_OTG_GLB->GCCFG = (1 << 23);
  143. usb_hsphy_init(25000000U);
  144. return (1 << 23); /* Enable USB HS PHY USBx->GCCFG |= USB_OTG_GCCFG_PHYHSEN;*/
  145. #else
  146. return 0;
  147. #endif
  148. #else
  149. #if __has_include("stm32h7xx.h") || __has_include("stm32f7xx.h") || __has_include("stm32l4xx.h")
  150. return (1 << 16);
  151. #else
  152. return ((1 << 16) | (1 << 21));
  153. #endif
  154. #endif
  155. }
  156. uint32_t usbh_get_dwc2_gccfg_conf(uint32_t reg_base)
  157. {
  158. #if __has_include("stm32h7xx.h") || __has_include("stm32f7xx.h") || __has_include("stm32l4xx.h")
  159. #define USB_OTG_GLB ((DWC2_GlobalTypeDef *)(reg_base))
  160. /* B-peripheral session valid override enable */
  161. USB_OTG_GLB->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOEN;
  162. USB_OTG_GLB->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOVAL;
  163. #endif
  164. #ifdef CONFIG_USB_HS
  165. #if defined(STM32F722xx) || defined(STM32F723xx) || defined(STM32F730xx) || defined(STM32F732xx) || defined(STM32F733xx)
  166. USB_OTG_GLB->GCCFG = (1 << 23);
  167. usb_hsphy_init(25000000U);
  168. return (1 << 23); /* Enable USB HS PHY USBx->GCCFG |= USB_OTG_GCCFG_PHYHSEN;*/
  169. #else
  170. return 0;
  171. #endif
  172. #else
  173. #if __has_include("stm32h7xx.h") || __has_include("stm32f7xx.h") || __has_include("stm32l4xx.h")
  174. return (1 << 16);
  175. #else
  176. return ((1 << 16) | (1 << 21));
  177. #endif
  178. #endif
  179. }