usb_dc_dwc2.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usb_dwc2_reg.h"
  8. // clang-format off
  9. #if defined ( __CC_ARM )
  10. #ifndef __UNALIGNED_UINT32_WRITE
  11. #define __UNALIGNED_UINT32_WRITE(addr, val) ((*((__packed uint32_t *)(addr))) = (val))
  12. #endif
  13. #ifndef __UNALIGNED_UINT32_READ
  14. #define __UNALIGNED_UINT32_READ(addr) (*((const __packed uint32_t *)(addr)))
  15. #endif
  16. #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  17. #ifndef __UNALIGNED_UINT32_WRITE
  18. #pragma clang diagnostic push
  19. #pragma clang diagnostic ignored "-Wpacked"
  20. /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
  21. __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
  22. #pragma clang diagnostic pop
  23. #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
  24. #endif
  25. #ifndef __UNALIGNED_UINT32_READ
  26. #pragma clang diagnostic push
  27. #pragma clang diagnostic ignored "-Wpacked"
  28. /*lint -esym(9058, T_UINT32_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_READ */
  29. __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
  30. #pragma clang diagnostic pop
  31. #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
  32. #endif
  33. #elif defined ( __GNUC__ )
  34. #ifndef __UNALIGNED_UINT32_WRITE
  35. #pragma GCC diagnostic push
  36. #pragma GCC diagnostic ignored "-Wpacked"
  37. #pragma GCC diagnostic ignored "-Wattributes"
  38. __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
  39. #pragma GCC diagnostic pop
  40. #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
  41. #endif
  42. #ifndef __UNALIGNED_UINT32_READ
  43. #pragma GCC diagnostic push
  44. #pragma GCC diagnostic ignored "-Wpacked"
  45. #pragma GCC diagnostic ignored "-Wattributes"
  46. __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
  47. #pragma GCC diagnostic pop
  48. #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
  49. #endif
  50. #endif
  51. // clang-format on
  52. #ifndef CONFIG_USB_DWC2_RXALL_FIFO_SIZE
  53. #define CONFIG_USB_DWC2_RXALL_FIFO_SIZE (1024 / 4)
  54. #endif
  55. #ifndef CONFIG_USB_DWC2_TX0_FIFO_SIZE
  56. #define CONFIG_USB_DWC2_TX0_FIFO_SIZE (64 / 4)
  57. #endif
  58. #ifndef CONFIG_USB_DWC2_TX1_FIFO_SIZE
  59. #define CONFIG_USB_DWC2_TX1_FIFO_SIZE (1024 / 4)
  60. #endif
  61. #ifndef CONFIG_USB_DWC2_TX2_FIFO_SIZE
  62. #define CONFIG_USB_DWC2_TX2_FIFO_SIZE (64 / 4)
  63. #endif
  64. #ifndef CONFIG_USB_DWC2_TX3_FIFO_SIZE
  65. #define CONFIG_USB_DWC2_TX3_FIFO_SIZE (64 / 4)
  66. #endif
  67. #ifndef CONFIG_USB_DWC2_TX4_FIFO_SIZE
  68. #define CONFIG_USB_DWC2_TX4_FIFO_SIZE (64 / 4)
  69. #endif
  70. #ifndef CONFIG_USB_DWC2_TX5_FIFO_SIZE
  71. #define CONFIG_USB_DWC2_TX5_FIFO_SIZE (64 / 4)
  72. #endif
  73. #ifndef CONFIG_USB_DWC2_TX6_FIFO_SIZE
  74. #define CONFIG_USB_DWC2_TX6_FIFO_SIZE (0 / 4)
  75. #endif
  76. #ifndef CONFIG_USB_DWC2_TX7_FIFO_SIZE
  77. #define CONFIG_USB_DWC2_TX7_FIFO_SIZE (0 / 4)
  78. #endif
  79. #ifndef CONFIG_USB_DWC2_TX8_FIFO_SIZE
  80. #define CONFIG_USB_DWC2_TX8_FIFO_SIZE (0 / 4)
  81. #endif
  82. #define USBD_BASE (g_usbdev_bus[busid].reg_base)
  83. #define USB_OTG_GLB ((DWC2_GlobalTypeDef *)(USBD_BASE))
  84. #define USB_OTG_DEV ((DWC2_DeviceTypeDef *)(USBD_BASE + USB_OTG_DEVICE_BASE))
  85. #define USB_OTG_PCGCCTL *(__IO uint32_t *)((uint32_t)USBD_BASE + USB_OTG_PCGCCTL_BASE)
  86. #define USB_OTG_INEP(i) ((DWC2_INEndpointTypeDef *)(USBD_BASE + USB_OTG_IN_ENDPOINT_BASE + ((i)*USB_OTG_EP_REG_SIZE)))
  87. #define USB_OTG_OUTEP(i) ((DWC2_OUTEndpointTypeDef *)(USBD_BASE + USB_OTG_OUT_ENDPOINT_BASE + ((i)*USB_OTG_EP_REG_SIZE)))
  88. #define USB_OTG_FIFO(i) *(__IO uint32_t *)(USBD_BASE + USB_OTG_FIFO_BASE + ((i)*USB_OTG_FIFO_SIZE))
  89. extern uint32_t SystemCoreClock;
  90. /* Endpoint state */
  91. struct dwc2_ep_state {
  92. uint16_t ep_mps; /* Endpoint max packet size */
  93. uint8_t ep_type; /* Endpoint type */
  94. uint8_t ep_stalled; /* Endpoint stall flag */
  95. uint8_t *xfer_buf;
  96. uint32_t xfer_len;
  97. uint32_t actual_xfer_len;
  98. };
  99. /* Driver state */
  100. USB_NOCACHE_RAM_SECTION struct dwc2_udc {
  101. __attribute__((aligned(32))) struct usb_setup_packet setup;
  102. struct dwc2_ep_state in_ep[CONFIG_USBDEV_EP_NUM]; /*!< IN endpoint parameters*/
  103. struct dwc2_ep_state out_ep[CONFIG_USBDEV_EP_NUM]; /*!< OUT endpoint parameters */
  104. } g_dwc2_udc[CONFIG_USBDEV_MAX_BUS];
  105. static inline int dwc2_reset(uint8_t busid)
  106. {
  107. volatile uint32_t count = 0U;
  108. /* Wait for AHB master IDLE state. */
  109. do {
  110. if (++count > 200000U) {
  111. return -1;
  112. }
  113. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  114. /* Core Soft Reset */
  115. count = 0U;
  116. USB_OTG_GLB->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
  117. do {
  118. if (++count > 200000U) {
  119. break;
  120. }
  121. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);
  122. USB_OTG_GLB->GRSTCTL &= ~USB_OTG_GRSTCTL_CSRST;
  123. return 0;
  124. }
  125. static inline int dwc2_core_init(uint8_t busid)
  126. {
  127. int ret;
  128. #if defined(CONFIG_USB_HS)
  129. /* Init The ULPI Interface */
  130. USB_OTG_GLB->GUSBCFG &= ~(USB_OTG_GUSBCFG_TSDPS | USB_OTG_GUSBCFG_ULPIFSLS | USB_OTG_GUSBCFG_PHYSEL);
  131. /* Select vbus source */
  132. USB_OTG_GLB->GUSBCFG &= ~(USB_OTG_GUSBCFG_ULPIEVBUSD | USB_OTG_GUSBCFG_ULPIEVBUSI);
  133. /* Reset after a PHY select */
  134. ret = dwc2_reset(busid);
  135. #else
  136. /* Select FS Embedded PHY */
  137. USB_OTG_GLB->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
  138. /* Reset after a PHY select */
  139. ret = dwc2_reset(busid);
  140. #endif
  141. return ret;
  142. }
  143. static inline void dwc2_set_mode(uint8_t busid, uint8_t mode)
  144. {
  145. USB_OTG_GLB->GUSBCFG &= ~(USB_OTG_GUSBCFG_FHMOD | USB_OTG_GUSBCFG_FDMOD);
  146. if (mode == USB_OTG_MODE_HOST) {
  147. USB_OTG_GLB->GUSBCFG |= USB_OTG_GUSBCFG_FHMOD;
  148. } else if (mode == USB_OTG_MODE_DEVICE) {
  149. USB_OTG_GLB->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
  150. }
  151. usbd_dwc2_delay_ms(50);
  152. }
  153. static inline int dwc2_flush_rxfifo(uint8_t busid)
  154. {
  155. volatile uint32_t count = 0U;
  156. /* Wait for AHB master IDLE state. */
  157. do {
  158. if (++count > 200000U) {
  159. return -1;
  160. }
  161. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  162. count = 0;
  163. USB_OTG_GLB->GRSTCTL = USB_OTG_GRSTCTL_RXFFLSH;
  164. do {
  165. if (++count > 200000U) {
  166. return -1;
  167. }
  168. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) == USB_OTG_GRSTCTL_RXFFLSH);
  169. return 0;
  170. }
  171. static inline int dwc2_flush_txfifo(uint8_t busid, uint32_t num)
  172. {
  173. volatile uint32_t count = 0U;
  174. /* Wait for AHB master IDLE state. */
  175. do {
  176. if (++count > 200000U) {
  177. return -1;
  178. }
  179. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  180. count = 0;
  181. USB_OTG_GLB->GRSTCTL = (USB_OTG_GRSTCTL_TXFFLSH | (num << 6));
  182. do {
  183. if (++count > 200000U) {
  184. return -1;
  185. }
  186. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH) == USB_OTG_GRSTCTL_TXFFLSH);
  187. return 0;
  188. }
  189. static void dwc2_set_turnaroundtime(uint8_t busid, uint32_t hclk, uint8_t speed)
  190. {
  191. uint32_t UsbTrd;
  192. /* The USBTRD is configured according to the tables below, depending on AHB frequency
  193. used by application. In the low AHB frequency range it is used to stretch enough the USB response
  194. time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
  195. latency to the Data FIFO */
  196. if (speed == USB_OTG_SPEED_FULL) {
  197. if ((hclk >= 14200000U) && (hclk < 15000000U)) {
  198. /* hclk Clock Range between 14.2-15 MHz */
  199. UsbTrd = 0xFU;
  200. } else if ((hclk >= 15000000U) && (hclk < 16000000U)) {
  201. /* hclk Clock Range between 15-16 MHz */
  202. UsbTrd = 0xEU;
  203. } else if ((hclk >= 16000000U) && (hclk < 17200000U)) {
  204. /* hclk Clock Range between 16-17.2 MHz */
  205. UsbTrd = 0xDU;
  206. } else if ((hclk >= 17200000U) && (hclk < 18500000U)) {
  207. /* hclk Clock Range between 17.2-18.5 MHz */
  208. UsbTrd = 0xCU;
  209. } else if ((hclk >= 18500000U) && (hclk < 20000000U)) {
  210. /* hclk Clock Range between 18.5-20 MHz */
  211. UsbTrd = 0xBU;
  212. } else if ((hclk >= 20000000U) && (hclk < 21800000U)) {
  213. /* hclk Clock Range between 20-21.8 MHz */
  214. UsbTrd = 0xAU;
  215. } else if ((hclk >= 21800000U) && (hclk < 24000000U)) {
  216. /* hclk Clock Range between 21.8-24 MHz */
  217. UsbTrd = 0x9U;
  218. } else if ((hclk >= 24000000U) && (hclk < 27700000U)) {
  219. /* hclk Clock Range between 24-27.7 MHz */
  220. UsbTrd = 0x8U;
  221. } else if ((hclk >= 27700000U) && (hclk < 32000000U)) {
  222. /* hclk Clock Range between 27.7-32 MHz */
  223. UsbTrd = 0x7U;
  224. } else /* if(hclk >= 32000000) */
  225. {
  226. /* hclk Clock Range between 32-200 MHz */
  227. UsbTrd = 0x6U;
  228. }
  229. } else if (speed == USB_OTG_SPEED_HIGH) {
  230. UsbTrd = USBD_HS_TRDT_VALUE;
  231. } else {
  232. UsbTrd = USBD_DEFAULT_TRDT_VALUE;
  233. }
  234. USB_OTG_GLB->GUSBCFG |= USB_OTG_GUSBCFG_TOCAL;
  235. USB_OTG_GLB->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
  236. USB_OTG_GLB->GUSBCFG |= (uint32_t)((UsbTrd << USB_OTG_GUSBCFG_TRDT_Pos) & USB_OTG_GUSBCFG_TRDT);
  237. }
  238. static void dwc2_set_txfifo(uint8_t busid, uint8_t fifo, uint16_t size)
  239. {
  240. uint8_t i;
  241. uint32_t tx_offset;
  242. /* TXn min size = 16 words. (n : Transmit FIFO index)
  243. When a TxFIFO is not used, the Configuration should be as follows:
  244. case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes)
  245. --> Txm can use the space allocated for Txn.
  246. case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes)
  247. --> Txn should be configured with the minimum space of 16 words
  248. The FIFO is used optimally when used TxFIFOs are allocated in the top
  249. of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
  250. When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
  251. tx_offset = USB_OTG_GLB->GRXFSIZ;
  252. if (fifo == 0U) {
  253. USB_OTG_GLB->DIEPTXF0_HNPTXFSIZ = ((uint32_t)size << 16) | tx_offset;
  254. } else {
  255. tx_offset += (USB_OTG_GLB->DIEPTXF0_HNPTXFSIZ) >> 16;
  256. for (i = 0U; i < (fifo - 1U); i++) {
  257. tx_offset += (USB_OTG_GLB->DIEPTXF[i] >> 16);
  258. }
  259. /* Multiply Tx_Size by 2 to get higher performance */
  260. USB_OTG_GLB->DIEPTXF[fifo - 1U] = ((uint32_t)size << 16) | tx_offset;
  261. }
  262. USB_LOG_INFO("fifo%d size:%04x, offset:%04x\r\n", fifo, size, tx_offset);
  263. }
  264. static uint8_t dwc2_get_devspeed(uint8_t busid)
  265. {
  266. uint8_t speed;
  267. uint32_t DevEnumSpeed = USB_OTG_DEV->DSTS & USB_OTG_DSTS_ENUMSPD;
  268. if (DevEnumSpeed == DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ) {
  269. speed = USB_OTG_SPEED_HIGH;
  270. } else if ((DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ) ||
  271. (DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_48MHZ)) {
  272. speed = USB_OTG_SPEED_FULL;
  273. } else {
  274. speed = 0xFU;
  275. }
  276. return speed;
  277. }
  278. static void dwc2_ep0_start_read_setup(uint8_t busid, uint8_t *psetup)
  279. {
  280. USB_OTG_OUTEP(0U)->DOEPTSIZ = 0U;
  281. USB_OTG_OUTEP(0U)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
  282. USB_OTG_OUTEP(0U)->DOEPTSIZ |= (3U * 8U);
  283. USB_OTG_OUTEP(0U)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_STUPCNT;
  284. #ifdef CONFIG_USB_DWC2_DMA_ENABLE
  285. USB_OTG_OUTEP(0U)->DOEPDMA = (uint32_t)psetup;
  286. /* EP enable */
  287. USB_OTG_OUTEP(0U)->DOEPCTL |= USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP;
  288. #endif
  289. }
  290. void dwc2_ep_write(uint8_t busid, uint8_t ep_idx, uint8_t *src, uint16_t len)
  291. {
  292. uint32_t *pSrc = (uint32_t *)src;
  293. uint32_t count32b, i;
  294. count32b = ((uint32_t)len + 3U) / 4U;
  295. for (i = 0U; i < count32b; i++) {
  296. USB_OTG_FIFO((uint32_t)ep_idx) = __UNALIGNED_UINT32_READ(pSrc);
  297. pSrc++;
  298. }
  299. }
  300. void dwc2_ep_read(uint8_t busid, uint8_t *dest, uint16_t len)
  301. {
  302. uint32_t *pDest = (uint32_t *)dest;
  303. uint32_t i;
  304. uint32_t count32b = ((uint32_t)len + 3U) / 4U;
  305. for (i = 0U; i < count32b; i++) {
  306. __UNALIGNED_UINT32_WRITE(pDest, USB_OTG_FIFO(0U));
  307. pDest++;
  308. }
  309. }
  310. static void dwc2_tx_fifo_empty_procecss(uint8_t busid, uint8_t ep_idx)
  311. {
  312. uint32_t len;
  313. uint32_t len32b;
  314. uint32_t fifoemptymsk;
  315. len = g_dwc2_udc[busid].in_ep[ep_idx].xfer_len - g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len;
  316. if (len > g_dwc2_udc[busid].in_ep[ep_idx].ep_mps) {
  317. len = g_dwc2_udc[busid].in_ep[ep_idx].ep_mps;
  318. }
  319. len32b = (len + 3U) / 4U;
  320. while (((USB_OTG_INEP(ep_idx)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= len32b) &&
  321. (g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len < g_dwc2_udc[busid].in_ep[ep_idx].xfer_len) && (g_dwc2_udc[busid].in_ep[ep_idx].xfer_len != 0U)) {
  322. /* Write the FIFO */
  323. len = g_dwc2_udc[busid].in_ep[ep_idx].xfer_len - g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len;
  324. if (len > g_dwc2_udc[busid].in_ep[ep_idx].ep_mps) {
  325. len = g_dwc2_udc[busid].in_ep[ep_idx].ep_mps;
  326. }
  327. if (g_dwc2_udc[busid].in_ep[ep_idx].ep_type == USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  328. if ((USB_OTG_DEV->DSTS & (1U << 8)) == 0U) {
  329. USB_OTG_INEP(ep_idx)->DIEPCTL &= ~USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
  330. USB_OTG_INEP(ep_idx)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
  331. } else {
  332. USB_OTG_INEP(ep_idx)->DIEPCTL &= ~USB_OTG_DIEPCTL_SODDFRM;
  333. USB_OTG_INEP(ep_idx)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
  334. }
  335. USB_OTG_INEP(ep_idx)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_MULCNT);
  336. USB_OTG_INEP(ep_idx)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_MULCNT & (1U << 29));
  337. }
  338. dwc2_ep_write(busid, ep_idx, g_dwc2_udc[busid].in_ep[ep_idx].xfer_buf, len);
  339. g_dwc2_udc[busid].in_ep[ep_idx].xfer_buf += len;
  340. g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len += len;
  341. }
  342. if (g_dwc2_udc[busid].in_ep[ep_idx].xfer_len <= g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len) {
  343. fifoemptymsk = (uint32_t)(0x1UL << (ep_idx & 0x0f));
  344. USB_OTG_DEV->DIEPEMPMSK &= ~fifoemptymsk;
  345. }
  346. }
  347. /**
  348. * @brief dwc2_get_glb_intstatus: return the global USB interrupt status
  349. * @retval status
  350. */
  351. static inline uint32_t dwc2_get_glb_intstatus(uint8_t busid)
  352. {
  353. uint32_t tmpreg;
  354. tmpreg = USB_OTG_GLB->GINTSTS;
  355. tmpreg &= USB_OTG_GLB->GINTMSK;
  356. return tmpreg;
  357. }
  358. /**
  359. * @brief dwc2_get_outeps_intstatus: return the USB device OUT endpoints interrupt status
  360. * @retval status
  361. */
  362. static inline uint32_t dwc2_get_outeps_intstatus(uint8_t busid)
  363. {
  364. uint32_t tmpreg;
  365. tmpreg = USB_OTG_DEV->DAINT;
  366. tmpreg &= USB_OTG_DEV->DAINTMSK;
  367. return ((tmpreg & 0xffff0000U) >> 16);
  368. }
  369. /**
  370. * @brief dwc2_get_ineps_intstatus: return the USB device IN endpoints interrupt status
  371. * @retval status
  372. */
  373. static inline uint32_t dwc2_get_ineps_intstatus(uint8_t busid)
  374. {
  375. uint32_t tmpreg;
  376. tmpreg = USB_OTG_DEV->DAINT;
  377. tmpreg &= USB_OTG_DEV->DAINTMSK;
  378. return ((tmpreg & 0xFFFFU));
  379. }
  380. /**
  381. * @brief Returns Device OUT EP Interrupt register
  382. * @param epnum endpoint number
  383. * This parameter can be a value from 0 to 15
  384. * @retval Device OUT EP Interrupt register
  385. */
  386. static inline uint32_t dwc2_get_outep_intstatus(uint8_t busid, uint8_t epnum)
  387. {
  388. uint32_t tmpreg;
  389. tmpreg = USB_OTG_OUTEP((uint32_t)epnum)->DOEPINT;
  390. USB_OTG_OUTEP((uint32_t)epnum)->DOEPINT = tmpreg;
  391. tmpreg = tmpreg & USB_OTG_DEV->DOEPMSK;
  392. return tmpreg;
  393. }
  394. /**
  395. * @brief Returns Device IN EP Interrupt register
  396. * @param epnum endpoint number
  397. * This parameter can be a value from 0 to 15
  398. * @retval Device IN EP Interrupt register
  399. */
  400. static inline uint32_t dwc2_get_inep_intstatus(uint8_t busid, uint8_t epnum)
  401. {
  402. uint32_t tmpreg, msk, emp;
  403. msk = USB_OTG_DEV->DIEPMSK;
  404. emp = USB_OTG_DEV->DIEPEMPMSK;
  405. msk |= ((emp >> (epnum & 0x0F)) & 0x1U) << 7;
  406. tmpreg = USB_OTG_INEP((uint32_t)epnum)->DIEPINT;
  407. USB_OTG_INEP((uint32_t)epnum)->DIEPINT = tmpreg;
  408. tmpreg = tmpreg & msk;
  409. return tmpreg;
  410. }
  411. int usb_dc_init(uint8_t busid)
  412. {
  413. int ret;
  414. uint8_t fsphy_type;
  415. uint8_t hsphy_type;
  416. uint8_t dma_support;
  417. uint8_t endpoints;
  418. uint32_t fifo_num;
  419. memset(&g_dwc2_udc[busid], 0, sizeof(struct dwc2_udc));
  420. usb_dc_low_level_init(busid);
  421. /*
  422. Full-Speed PHY Interface Type (FSPhyType)
  423. 2'b00: Full-speed interface not supported
  424. 2'b01: Dedicated full-speed interface
  425. 2'b10: FS pins shared with UTMI+ pins
  426. 2'b11: FS pins shared with ULPI pins
  427. High-Speed PHY Interface Type (HSPhyType)
  428. 2'b00: High-Speed interface not supported
  429. 2'b01: UTMI+
  430. 2'b10: ULPI
  431. 2'b11: UTMI+ and ULPI
  432. Architecture (OtgArch)
  433. 2'b00: Slave-Only
  434. 2'b01: External DMA
  435. 2'b10: Internal DMA
  436. Others: Reserved
  437. */
  438. fsphy_type = ((USB_OTG_GLB->GHWCFG2 & (0x03 << 8)) >> 8);
  439. hsphy_type = ((USB_OTG_GLB->GHWCFG2 & (0x03 << 6)) >> 6);
  440. dma_support = ((USB_OTG_GLB->GHWCFG2 & (0x03 << 3)) >> 3);
  441. endpoints = ((USB_OTG_GLB->GHWCFG2 & (0x0f << 10)) >> 10) + 1;
  442. USB_LOG_INFO("========== dwc2 udc params ==========\r\n");
  443. USB_LOG_INFO("CID:%08x\r\n", USB_OTG_GLB->CID);
  444. USB_LOG_INFO("GSNPSID:%08x\r\n", USB_OTG_GLB->GSNPSID);
  445. USB_LOG_INFO("GHWCFG1:%08x\r\n", USB_OTG_GLB->GHWCFG1);
  446. USB_LOG_INFO("GHWCFG2:%08x\r\n", USB_OTG_GLB->GHWCFG2);
  447. USB_LOG_INFO("GHWCFG3:%08x\r\n", USB_OTG_GLB->GHWCFG3);
  448. USB_LOG_INFO("GHWCFG4:%08x\r\n", USB_OTG_GLB->GHWCFG4);
  449. USB_LOG_INFO("dwc2 fsphy type:%d, hsphy type:%d, dma support:%d\r\n", fsphy_type, hsphy_type, dma_support);
  450. USB_LOG_INFO("dwc2 has %d endpoints and dfifo depth(32-bit words) is %d, default config: %d endpoints\r\n", endpoints, (USB_OTG_GLB->GHWCFG3 >> 16), CONFIG_USBDEV_EP_NUM);
  451. USB_LOG_INFO("=================================\r\n");
  452. if (endpoints < CONFIG_USBDEV_EP_NUM) {
  453. USB_LOG_ERR("dwc2 has less endpoints than config, please check\r\n");
  454. while (1) {
  455. }
  456. }
  457. USB_OTG_DEV->DCTL |= USB_OTG_DCTL_SDIS;
  458. USB_OTG_GLB->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
  459. /* This is vendor register */
  460. USB_OTG_GLB->GCCFG = usbd_get_dwc2_gccfg_conf(USBD_BASE);
  461. ret = dwc2_core_init(busid);
  462. /* Force Device Mode*/
  463. dwc2_set_mode(busid, USB_OTG_MODE_DEVICE);
  464. for (uint8_t i = 0U; i < 15U; i++) {
  465. USB_OTG_GLB->DIEPTXF[i] = 0U;
  466. }
  467. /* Restart the Phy Clock */
  468. USB_OTG_PCGCCTL = 0U;
  469. /* Device speed configuration */
  470. USB_OTG_DEV->DCFG &= ~USB_OTG_DCFG_DSPD;
  471. #if defined(CONFIG_USB_HS)
  472. USB_OTG_DEV->DCFG |= USB_OTG_SPEED_HIGH;
  473. #else
  474. if (hsphy_type == 0) {
  475. USB_OTG_DEV->DCFG |= USB_OTG_SPEED_FULL;
  476. } else {
  477. USB_OTG_DEV->DCFG |= USB_OTG_SPEED_HIGH_IN_FULL;
  478. }
  479. #endif
  480. /* Clear all pending Device Interrupts */
  481. USB_OTG_DEV->DIEPMSK = 0U;
  482. USB_OTG_DEV->DOEPMSK = 0U;
  483. USB_OTG_DEV->DAINTMSK = 0U;
  484. /* Disable all interrupts. */
  485. USB_OTG_GLB->GINTMSK = 0U;
  486. /* Clear any pending interrupts */
  487. USB_OTG_GLB->GINTSTS = 0xBFFFFFFFU;
  488. /* Enable interrupts matching to the Device mode ONLY */
  489. USB_OTG_GLB->GINTMSK = USB_OTG_GINTMSK_USBRST | USB_OTG_GINTMSK_ENUMDNEM |
  490. USB_OTG_GINTMSK_OEPINT | USB_OTG_GINTMSK_IEPINT |
  491. USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_WUIM;
  492. #ifdef CONFIG_USB_DWC2_DMA_ENABLE
  493. if (((USB_OTG_GLB->GHWCFG2 & (0x3U << 3)) >> 3) != 2) {
  494. USB_LOG_ERR("This dwc2 version does not support dma mode, so stop working\r\n");
  495. while (1) {
  496. }
  497. }
  498. USB_OTG_DEV->DCFG &= ~USB_OTG_DCFG_DESCDMA;
  499. USB_OTG_GLB->GAHBCFG &= ~USB_OTG_GAHBCFG_HBSTLEN;
  500. USB_OTG_GLB->GAHBCFG |= (USB_OTG_GAHBCFG_DMAEN | USB_OTG_GAHBCFG_HBSTLEN_4);
  501. #else
  502. USB_OTG_GLB->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
  503. #endif
  504. #if CONFIG_DWC2_VBUS_SENSING
  505. USB_OTG_GLB->GINTMSK |= (USB_OTG_GINTMSK_OTGINT | USB_OTG_GINTMSK_SRQIM);
  506. #endif
  507. #if 0
  508. USB_OTG_GLB->GINTMSK |= USB_OTG_GINTMSK_SOFM;
  509. #endif
  510. USB_OTG_GLB->GRXFSIZ = (CONFIG_USB_DWC2_RXALL_FIFO_SIZE);
  511. dwc2_set_txfifo(busid, 0, CONFIG_USB_DWC2_TX0_FIFO_SIZE);
  512. dwc2_set_txfifo(busid, 1, CONFIG_USB_DWC2_TX1_FIFO_SIZE);
  513. dwc2_set_txfifo(busid, 2, CONFIG_USB_DWC2_TX2_FIFO_SIZE);
  514. dwc2_set_txfifo(busid, 3, CONFIG_USB_DWC2_TX3_FIFO_SIZE);
  515. fifo_num = CONFIG_USB_DWC2_RXALL_FIFO_SIZE;
  516. fifo_num += CONFIG_USB_DWC2_TX0_FIFO_SIZE;
  517. fifo_num += CONFIG_USB_DWC2_TX1_FIFO_SIZE;
  518. fifo_num += CONFIG_USB_DWC2_TX2_FIFO_SIZE;
  519. fifo_num += CONFIG_USB_DWC2_TX3_FIFO_SIZE;
  520. #if CONFIG_USBDEV_EP_NUM > 4
  521. dwc2_set_txfifo(busid, 4, CONFIG_USB_DWC2_TX4_FIFO_SIZE);
  522. fifo_num += CONFIG_USB_DWC2_TX4_FIFO_SIZE;
  523. #endif
  524. #if CONFIG_USBDEV_EP_NUM > 5
  525. dwc2_set_txfifo(busid, 5, CONFIG_USB_DWC2_TX5_FIFO_SIZE);
  526. fifo_num += CONFIG_USB_DWC2_TX5_FIFO_SIZE;
  527. #endif
  528. #if CONFIG_USBDEV_EP_NUM > 6
  529. dwc2_set_txfifo(busid, 6, CONFIG_USB_DWC2_TX6_FIFO_SIZE);
  530. fifo_num += CONFIG_USB_DWC2_TX6_FIFO_SIZE;
  531. #endif
  532. #if CONFIG_USBDEV_EP_NUM > 7
  533. dwc2_set_txfifo(busid, 7, CONFIG_USB_DWC2_TX7_FIFO_SIZE);
  534. fifo_num += CONFIG_USB_DWC2_TX7_FIFO_SIZE;
  535. #endif
  536. #if CONFIG_USBDEV_EP_NUM > 8
  537. dwc2_set_txfifo(busid, 8, CONFIG_USB_DWC2_TX8_FIFO_SIZE);
  538. fifo_num += CONFIG_USB_DWC2_TX8_FIFO_SIZE;
  539. #endif
  540. if (fifo_num > (USB_OTG_GLB->GHWCFG3 >> 16)) {
  541. USB_LOG_ERR("Your fifo config is overflow, please check\r\n");
  542. while (1) {
  543. }
  544. }
  545. /* xxx32 chips do not follow (USB_OTG_GLB->GHWCFG3 >> 16) if hsphy_type is zero, they use 1.25KB(320 DWORD) */
  546. if ((hsphy_type == 0) && (fifo_num > 320)) {
  547. USB_LOG_ERR("Your fifo config is larger than 320 , please check\r\n");
  548. while (1) {
  549. }
  550. }
  551. ret = dwc2_flush_txfifo(busid, 0x10U);
  552. ret = dwc2_flush_rxfifo(busid);
  553. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
  554. USB_OTG_DEV->DCTL &= ~USB_OTG_DCTL_SDIS;
  555. return ret;
  556. }
  557. int usb_dc_deinit(uint8_t busid)
  558. {
  559. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
  560. USB_OTG_DEV->DCTL |= USB_OTG_DCTL_SDIS;
  561. /* Clear Pending interrupt */
  562. for (uint8_t i = 0U; i < 15U; i++) {
  563. USB_OTG_INEP(i)->DIEPINT = 0xFB7FU;
  564. USB_OTG_OUTEP(i)->DOEPINT = 0xFB7FU;
  565. }
  566. /* Clear interrupt masks */
  567. USB_OTG_DEV->DIEPMSK = 0U;
  568. USB_OTG_DEV->DOEPMSK = 0U;
  569. USB_OTG_DEV->DAINTMSK = 0U;
  570. /* Flush the FIFO */
  571. dwc2_flush_txfifo(busid, 0x10U);
  572. dwc2_flush_rxfifo(busid);
  573. usb_dc_low_level_deinit(busid);
  574. return 0;
  575. }
  576. int usbd_set_address(uint8_t busid, const uint8_t addr)
  577. {
  578. USB_OTG_DEV->DCFG &= ~(USB_OTG_DCFG_DAD);
  579. USB_OTG_DEV->DCFG |= ((uint32_t)addr << 4) & USB_OTG_DCFG_DAD;
  580. return 0;
  581. }
  582. int usbd_set_remote_wakeup(uint8_t busid)
  583. {
  584. if (!(USB_OTG_DEV->DSTS & USB_OTG_DSTS_SUSPSTS)) {
  585. return -1;
  586. }
  587. USB_OTG_DEV->DCTL |= USB_OTG_DCTL_RWUSIG;
  588. usbd_dwc2_delay_ms(10);
  589. USB_OTG_DEV->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  590. return 0;
  591. }
  592. uint8_t usbd_get_port_speed(uint8_t busid)
  593. {
  594. uint8_t speed;
  595. uint32_t DevEnumSpeed = USB_OTG_DEV->DSTS & USB_OTG_DSTS_ENUMSPD;
  596. if (DevEnumSpeed == DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ) {
  597. speed = USB_SPEED_HIGH;
  598. } else if ((DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ) ||
  599. (DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_48MHZ)) {
  600. speed = USB_SPEED_FULL;
  601. } else {
  602. speed = USB_SPEED_FULL;
  603. }
  604. return speed;
  605. }
  606. int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep)
  607. {
  608. uint8_t ep_idx = USB_EP_GET_IDX(ep->bEndpointAddress);
  609. if (ep_idx > (CONFIG_USBDEV_EP_NUM - 1)) {
  610. USB_LOG_ERR("Ep addr %02x overflow\r\n", ep->bEndpointAddress);
  611. return -1;
  612. }
  613. if (USB_EP_DIR_IS_OUT(ep->bEndpointAddress)) {
  614. g_dwc2_udc[busid].out_ep[ep_idx].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
  615. g_dwc2_udc[busid].out_ep[ep_idx].ep_type = USB_GET_ENDPOINT_TYPE(ep->bmAttributes);
  616. USB_OTG_DEV->DAINTMSK |= USB_OTG_DAINTMSK_OEPM & (uint32_t)(1UL << (16 + ep_idx));
  617. if ((USB_OTG_OUTEP(ep_idx)->DOEPCTL & USB_OTG_DOEPCTL_USBAEP) == 0) {
  618. USB_OTG_OUTEP(ep_idx)->DOEPCTL |= (USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize) & USB_OTG_DOEPCTL_MPSIZ) |
  619. ((uint32_t)USB_GET_ENDPOINT_TYPE(ep->bmAttributes) << 18) |
  620. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  621. USB_OTG_DOEPCTL_USBAEP;
  622. }
  623. } else {
  624. uint16_t fifo_size;
  625. if (ep_idx == 0) {
  626. fifo_size = (USB_OTG_GLB->DIEPTXF0_HNPTXFSIZ >> 16);
  627. } else {
  628. fifo_size = (USB_OTG_GLB->DIEPTXF[ep_idx - 1U] >> 16);
  629. }
  630. if ((fifo_size * 4) < USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize)) {
  631. USB_LOG_ERR("Ep addr %02x fifo overflow\r\n", ep->bEndpointAddress);
  632. return -2;
  633. }
  634. g_dwc2_udc[busid].in_ep[ep_idx].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
  635. g_dwc2_udc[busid].in_ep[ep_idx].ep_type = USB_GET_ENDPOINT_TYPE(ep->bmAttributes);
  636. USB_OTG_DEV->DAINTMSK |= USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << ep_idx);
  637. if ((USB_OTG_INEP(ep_idx)->DIEPCTL & USB_OTG_DIEPCTL_USBAEP) == 0) {
  638. USB_OTG_INEP(ep_idx)->DIEPCTL |= (USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize) & USB_OTG_DIEPCTL_MPSIZ) |
  639. ((uint32_t)USB_GET_ENDPOINT_TYPE(ep->bmAttributes) << 18) | (ep_idx << 22) |
  640. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  641. USB_OTG_DIEPCTL_USBAEP;
  642. }
  643. dwc2_flush_txfifo(busid, ep_idx);
  644. }
  645. return 0;
  646. }
  647. int usbd_ep_close(uint8_t busid, const uint8_t ep)
  648. {
  649. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  650. volatile uint32_t count = 0U;
  651. if (USB_EP_DIR_IS_OUT(ep)) {
  652. if (USB_OTG_OUTEP(ep_idx)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) {
  653. USB_OTG_OUTEP(ep_idx)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  654. USB_OTG_OUTEP(ep_idx)->DOEPCTL |= USB_OTG_DOEPCTL_EPDIS;
  655. /* Wait for endpoint disabled interrupt */
  656. count = 0;
  657. do {
  658. if (++count > 50000) {
  659. break;
  660. }
  661. } while ((USB_OTG_OUTEP(ep_idx)->DOEPINT & USB_OTG_DOEPINT_EPDISD) != USB_OTG_DOEPINT_EPDISD);
  662. /* Clear and unmask endpoint disabled interrupt */
  663. USB_OTG_OUTEP(ep_idx)->DOEPINT = USB_OTG_DOEPINT_EPDISD;
  664. }
  665. USB_OTG_DEV->DEACHMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep_idx & 0x07)) << 16));
  666. USB_OTG_DEV->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep_idx & 0x07)) << 16));
  667. USB_OTG_OUTEP(ep_idx)->DOEPCTL = 0;
  668. } else {
  669. if (USB_OTG_INEP(ep_idx)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) {
  670. USB_OTG_INEP(ep_idx)->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
  671. USB_OTG_INEP(ep_idx)->DIEPCTL |= USB_OTG_DIEPCTL_EPDIS;
  672. /* Wait for endpoint disabled interrupt */
  673. count = 0;
  674. do {
  675. if (++count > 50000) {
  676. break;
  677. }
  678. } while ((USB_OTG_INEP(ep_idx)->DIEPINT & USB_OTG_DIEPINT_EPDISD) != USB_OTG_DIEPINT_EPDISD);
  679. /* Clear and unmask endpoint disabled interrupt */
  680. USB_OTG_INEP(ep_idx)->DIEPINT = USB_OTG_DIEPINT_EPDISD;
  681. }
  682. USB_OTG_DEV->DEACHMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep_idx & 0x07)));
  683. USB_OTG_DEV->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep_idx & 0x07)));
  684. USB_OTG_INEP(ep_idx)->DIEPCTL = 0;
  685. }
  686. return 0;
  687. }
  688. int usbd_ep_set_stall(uint8_t busid, const uint8_t ep)
  689. {
  690. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  691. if (USB_EP_DIR_IS_OUT(ep)) {
  692. if (((USB_OTG_OUTEP(ep_idx)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == 0U) && (ep_idx != 0U)) {
  693. USB_OTG_OUTEP(ep_idx)->DOEPCTL &= ~(USB_OTG_DOEPCTL_EPDIS);
  694. }
  695. USB_OTG_OUTEP(ep_idx)->DOEPCTL |= USB_OTG_DOEPCTL_STALL;
  696. } else {
  697. if (((USB_OTG_INEP(ep_idx)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == 0U) && (ep_idx != 0U)) {
  698. USB_OTG_INEP(ep_idx)->DIEPCTL &= ~(USB_OTG_DIEPCTL_EPDIS);
  699. }
  700. USB_OTG_INEP(ep_idx)->DIEPCTL |= USB_OTG_DIEPCTL_STALL;
  701. }
  702. #ifdef CONFIG_USB_DWC2_DMA_ENABLE
  703. if (ep_idx == 0) {
  704. dwc2_ep0_start_read_setup(busid, (uint8_t *)&g_dwc2_udc[busid].setup);
  705. }
  706. #endif
  707. return 0;
  708. }
  709. int usbd_ep_clear_stall(uint8_t busid, const uint8_t ep)
  710. {
  711. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  712. if (USB_EP_DIR_IS_OUT(ep)) {
  713. USB_OTG_OUTEP(ep_idx)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  714. if ((g_dwc2_udc[busid].out_ep[ep_idx].ep_type == USB_ENDPOINT_TYPE_INTERRUPT) ||
  715. (g_dwc2_udc[busid].out_ep[ep_idx].ep_type == USB_ENDPOINT_TYPE_BULK)) {
  716. USB_OTG_OUTEP(ep_idx)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM; /* DATA0 */
  717. }
  718. } else {
  719. USB_OTG_INEP(ep_idx)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  720. if ((g_dwc2_udc[busid].in_ep[ep_idx].ep_type == USB_ENDPOINT_TYPE_INTERRUPT) ||
  721. (g_dwc2_udc[busid].in_ep[ep_idx].ep_type == USB_ENDPOINT_TYPE_BULK)) {
  722. USB_OTG_INEP(ep_idx)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM; /* DATA0 */
  723. }
  724. }
  725. return 0;
  726. }
  727. int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
  728. {
  729. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  730. if (USB_EP_DIR_IS_OUT(ep)) {
  731. if (USB_OTG_OUTEP(ep_idx)->DOEPCTL & USB_OTG_DOEPCTL_STALL) {
  732. *stalled = 1;
  733. } else {
  734. *stalled = 0;
  735. }
  736. } else {
  737. if (USB_OTG_INEP(ep_idx)->DIEPCTL & USB_OTG_DIEPCTL_STALL) {
  738. *stalled = 1;
  739. } else {
  740. *stalled = 0;
  741. }
  742. }
  743. return 0;
  744. }
  745. int usbd_ep_start_write(uint8_t busid, const uint8_t ep, const uint8_t *data, uint32_t data_len)
  746. {
  747. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  748. uint32_t pktcnt = 0;
  749. if (!data && data_len) {
  750. return -1;
  751. }
  752. #if 0 /* some chips have confused with this, so disable as default */
  753. if (USB_OTG_INEP(ep_idx)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) {
  754. return -2;
  755. }
  756. #endif
  757. if (ep_idx && !(USB_OTG_INEP(ep_idx)->DIEPCTL & USB_OTG_DIEPCTL_MPSIZ)) {
  758. return -3;
  759. }
  760. if ((uint32_t)data & 0x03) {
  761. return -4;
  762. }
  763. g_dwc2_udc[busid].in_ep[ep_idx].xfer_buf = (uint8_t *)data;
  764. g_dwc2_udc[busid].in_ep[ep_idx].xfer_len = data_len;
  765. g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len = 0;
  766. USB_OTG_INEP(ep_idx)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
  767. USB_OTG_INEP(ep_idx)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
  768. if (data_len == 0) {
  769. USB_OTG_INEP(ep_idx)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
  770. USB_OTG_INEP(ep_idx)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
  771. return 0;
  772. }
  773. if (ep_idx == 0) {
  774. if (data_len > g_dwc2_udc[busid].in_ep[ep_idx].ep_mps) {
  775. data_len = g_dwc2_udc[busid].in_ep[ep_idx].ep_mps;
  776. }
  777. g_dwc2_udc[busid].in_ep[ep_idx].xfer_len = data_len;
  778. USB_OTG_INEP(ep_idx)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
  779. USB_OTG_INEP(ep_idx)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & data_len);
  780. } else {
  781. pktcnt = (uint16_t)((data_len + g_dwc2_udc[busid].in_ep[ep_idx].ep_mps - 1U) / g_dwc2_udc[busid].in_ep[ep_idx].ep_mps);
  782. USB_OTG_INEP(ep_idx)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (pktcnt << 19));
  783. USB_OTG_INEP(ep_idx)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & data_len);
  784. }
  785. if (g_dwc2_udc[busid].in_ep[ep_idx].ep_type == USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  786. if ((USB_OTG_DEV->DSTS & (1U << 8)) == 0U) {
  787. USB_OTG_INEP(ep_idx)->DIEPCTL &= ~USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
  788. USB_OTG_INEP(ep_idx)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
  789. } else {
  790. USB_OTG_INEP(ep_idx)->DIEPCTL &= ~USB_OTG_DIEPCTL_SODDFRM;
  791. USB_OTG_INEP(ep_idx)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
  792. }
  793. USB_OTG_INEP(ep_idx)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_MULCNT);
  794. USB_OTG_INEP(ep_idx)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_MULCNT & (1U << 29));
  795. }
  796. #ifdef CONFIG_USB_DWC2_DMA_ENABLE
  797. USB_OTG_INEP(ep_idx)->DIEPDMA = (uint32_t)data;
  798. USB_OTG_INEP(ep_idx)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
  799. #else
  800. USB_OTG_INEP(ep_idx)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
  801. /* Enable the Tx FIFO Empty Interrupt for this EP */
  802. if (data_len > 0U) {
  803. USB_OTG_DEV->DIEPEMPMSK |= 1UL << (ep_idx & 0x0f);
  804. }
  805. #endif
  806. return 0;
  807. }
  808. int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t data_len)
  809. {
  810. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  811. uint32_t pktcnt = 0;
  812. if (!data && data_len) {
  813. return -1;
  814. }
  815. #if 0 /* some chips have confused with this, so disable as default */
  816. if (USB_OTG_OUTEP(ep_idx)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) {
  817. return -2;
  818. }
  819. #endif
  820. if (ep_idx && !(USB_OTG_OUTEP(ep_idx)->DOEPCTL & USB_OTG_DOEPCTL_MPSIZ)) {
  821. return -3;
  822. }
  823. if (((uint32_t)data) & 0x03) {
  824. return -4;
  825. }
  826. g_dwc2_udc[busid].out_ep[ep_idx].xfer_buf = (uint8_t *)data;
  827. g_dwc2_udc[busid].out_ep[ep_idx].xfer_len = data_len;
  828. g_dwc2_udc[busid].out_ep[ep_idx].actual_xfer_len = 0;
  829. USB_OTG_OUTEP(ep_idx)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
  830. USB_OTG_OUTEP(ep_idx)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
  831. if (data_len == 0) {
  832. USB_OTG_OUTEP(ep_idx)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1 << 19));
  833. USB_OTG_OUTEP(ep_idx)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & g_dwc2_udc[busid].out_ep[ep_idx].ep_mps);
  834. USB_OTG_OUTEP(ep_idx)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
  835. return 0;
  836. }
  837. if (ep_idx == 0) {
  838. if (data_len > g_dwc2_udc[busid].out_ep[ep_idx].ep_mps) {
  839. data_len = g_dwc2_udc[busid].out_ep[ep_idx].ep_mps;
  840. }
  841. g_dwc2_udc[busid].out_ep[ep_idx].xfer_len = data_len;
  842. USB_OTG_OUTEP(ep_idx)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
  843. USB_OTG_OUTEP(ep_idx)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & data_len);
  844. } else {
  845. pktcnt = (uint16_t)((data_len + g_dwc2_udc[busid].out_ep[ep_idx].ep_mps - 1U) / g_dwc2_udc[busid].out_ep[ep_idx].ep_mps);
  846. USB_OTG_OUTEP(ep_idx)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (pktcnt << 19));
  847. USB_OTG_OUTEP(ep_idx)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & data_len);
  848. }
  849. #ifdef CONFIG_USB_DWC2_DMA_ENABLE
  850. USB_OTG_OUTEP(ep_idx)->DOEPDMA = (uint32_t)data;
  851. #endif
  852. if (g_dwc2_udc[busid].out_ep[ep_idx].ep_type == USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  853. if ((USB_OTG_DEV->DSTS & (1U << 8)) == 0U) {
  854. USB_OTG_OUTEP(ep_idx)->DOEPCTL &= ~USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
  855. USB_OTG_OUTEP(ep_idx)->DOEPCTL |= USB_OTG_DOEPCTL_SODDFRM;
  856. } else {
  857. USB_OTG_OUTEP(ep_idx)->DOEPCTL &= ~USB_OTG_DOEPCTL_SODDFRM;
  858. USB_OTG_OUTEP(ep_idx)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
  859. }
  860. }
  861. USB_OTG_OUTEP(ep_idx)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
  862. return 0;
  863. }
  864. void USBD_IRQHandler(uint8_t busid)
  865. {
  866. uint32_t gint_status, temp, ep_idx, ep_intr, epint, read_count, daintmask;
  867. gint_status = dwc2_get_glb_intstatus(busid);
  868. if ((USB_OTG_GLB->GINTSTS & 0x1U) == USB_OTG_MODE_DEVICE) {
  869. /* Avoid spurious interrupt */
  870. if (gint_status == 0) {
  871. return;
  872. }
  873. #ifndef CONFIG_USB_DWC2_DMA_ENABLE
  874. /* Handle RxQLevel Interrupt */
  875. if (gint_status & USB_OTG_GINTSTS_RXFLVL) {
  876. USB_MASK_INTERRUPT(USB_OTG_GLB, USB_OTG_GINTSTS_RXFLVL);
  877. temp = USB_OTG_GLB->GRXSTSP;
  878. ep_idx = temp & USB_OTG_GRXSTSP_EPNUM;
  879. if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> USB_OTG_GRXSTSP_PKTSTS_Pos) == STS_DATA_UPDT) {
  880. read_count = (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  881. if (read_count != 0) {
  882. dwc2_ep_read(busid, g_dwc2_udc[busid].out_ep[ep_idx].xfer_buf, read_count);
  883. g_dwc2_udc[busid].out_ep[ep_idx].xfer_buf += read_count;
  884. }
  885. } else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> USB_OTG_GRXSTSP_PKTSTS_Pos) == STS_SETUP_UPDT) {
  886. read_count = (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  887. dwc2_ep_read(busid, (uint8_t *)&g_dwc2_udc[busid].setup, read_count);
  888. } else {
  889. /* ... */
  890. }
  891. USB_UNMASK_INTERRUPT(USB_OTG_GLB, USB_OTG_GINTSTS_RXFLVL);
  892. }
  893. #endif
  894. if (gint_status & USB_OTG_GINTSTS_OEPINT) {
  895. ep_idx = 0;
  896. ep_intr = dwc2_get_outeps_intstatus(busid);
  897. while (ep_intr != 0U) {
  898. if ((ep_intr & 0x1U) != 0U) {
  899. epint = dwc2_get_outep_intstatus(busid, ep_idx);
  900. if ((epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC) {
  901. if (ep_idx == 0) {
  902. if (g_dwc2_udc[busid].out_ep[ep_idx].xfer_len == 0) {
  903. /* Out status, start reading setup */
  904. dwc2_ep0_start_read_setup(busid, (uint8_t *)&g_dwc2_udc[busid].setup);
  905. } else {
  906. g_dwc2_udc[busid].out_ep[ep_idx].actual_xfer_len = g_dwc2_udc[busid].out_ep[ep_idx].xfer_len - ((USB_OTG_OUTEP(ep_idx)->DOEPTSIZ) & USB_OTG_DOEPTSIZ_XFRSIZ);
  907. g_dwc2_udc[busid].out_ep[ep_idx].xfer_len = 0;
  908. usbd_event_ep_out_complete_handler(busid, 0x00, g_dwc2_udc[busid].out_ep[ep_idx].actual_xfer_len);
  909. }
  910. } else {
  911. g_dwc2_udc[busid].out_ep[ep_idx].actual_xfer_len = g_dwc2_udc[busid].out_ep[ep_idx].xfer_len - ((USB_OTG_OUTEP(ep_idx)->DOEPTSIZ) & USB_OTG_DOEPTSIZ_XFRSIZ);
  912. g_dwc2_udc[busid].out_ep[ep_idx].xfer_len = 0;
  913. usbd_event_ep_out_complete_handler(busid, ep_idx, g_dwc2_udc[busid].out_ep[ep_idx].actual_xfer_len);
  914. }
  915. }
  916. if ((epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP) {
  917. usbd_event_ep0_setup_complete_handler(busid, (uint8_t *)&g_dwc2_udc[busid].setup);
  918. }
  919. }
  920. ep_intr >>= 1U;
  921. ep_idx++;
  922. }
  923. }
  924. if (gint_status & USB_OTG_GINTSTS_IEPINT) {
  925. ep_idx = 0U;
  926. ep_intr = dwc2_get_ineps_intstatus(busid);
  927. while (ep_intr != 0U) {
  928. if ((ep_intr & 0x1U) != 0U) {
  929. epint = dwc2_get_inep_intstatus(busid, ep_idx);
  930. if ((epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC) {
  931. if (ep_idx == 0) {
  932. g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len = g_dwc2_udc[busid].in_ep[ep_idx].xfer_len - ((USB_OTG_INEP(ep_idx)->DIEPTSIZ) & USB_OTG_DIEPTSIZ_XFRSIZ);
  933. g_dwc2_udc[busid].in_ep[ep_idx].xfer_len = 0;
  934. usbd_event_ep_in_complete_handler(busid, 0x80, g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len);
  935. if (g_dwc2_udc[busid].setup.wLength && ((g_dwc2_udc[busid].setup.bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT)) {
  936. /* In status, start reading setup */
  937. dwc2_ep0_start_read_setup(busid, (uint8_t *)&g_dwc2_udc[busid].setup);
  938. } else if (g_dwc2_udc[busid].setup.wLength == 0) {
  939. /* In status, start reading setup */
  940. dwc2_ep0_start_read_setup(busid, (uint8_t *)&g_dwc2_udc[busid].setup);
  941. }
  942. } else {
  943. g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len = g_dwc2_udc[busid].in_ep[ep_idx].xfer_len - ((USB_OTG_INEP(ep_idx)->DIEPTSIZ) & USB_OTG_DIEPTSIZ_XFRSIZ);
  944. g_dwc2_udc[busid].in_ep[ep_idx].xfer_len = 0;
  945. usbd_event_ep_in_complete_handler(busid, ep_idx | 0x80, g_dwc2_udc[busid].in_ep[ep_idx].actual_xfer_len);
  946. }
  947. }
  948. if ((epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE) {
  949. dwc2_tx_fifo_empty_procecss(busid, ep_idx);
  950. }
  951. }
  952. ep_intr >>= 1U;
  953. ep_idx++;
  954. }
  955. }
  956. if (gint_status & USB_OTG_GINTSTS_USBRST) {
  957. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_USBRST;
  958. USB_OTG_DEV->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  959. dwc2_flush_txfifo(busid, 0x10U);
  960. dwc2_flush_rxfifo(busid);
  961. for (uint8_t i = 0U; i < CONFIG_USBDEV_EP_NUM; i++) {
  962. if (i == 0U) {
  963. USB_OTG_INEP(i)->DIEPCTL = USB_OTG_DIEPCTL_SNAK;
  964. USB_OTG_OUTEP(i)->DOEPCTL = USB_OTG_DOEPCTL_SNAK;
  965. } else {
  966. if (USB_OTG_INEP(i)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) {
  967. USB_OTG_INEP(i)->DIEPCTL = (USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_SNAK);
  968. } else {
  969. USB_OTG_INEP(i)->DIEPCTL = 0;
  970. }
  971. if (USB_OTG_OUTEP(i)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) {
  972. USB_OTG_OUTEP(i)->DOEPCTL = (USB_OTG_DOEPCTL_EPDIS | USB_OTG_DOEPCTL_SNAK);
  973. } else {
  974. USB_OTG_OUTEP(i)->DOEPCTL = 0;
  975. }
  976. }
  977. USB_OTG_INEP(i)->DIEPTSIZ = 0U;
  978. USB_OTG_INEP(i)->DIEPINT = 0xFBFFU;
  979. USB_OTG_OUTEP(i)->DOEPTSIZ = 0U;
  980. USB_OTG_OUTEP(i)->DOEPINT = 0xFBFFU;
  981. }
  982. USB_OTG_DEV->DAINTMSK |= 0x10001U;
  983. USB_OTG_DEV->DOEPMSK = USB_OTG_DOEPMSK_STUPM |
  984. USB_OTG_DOEPMSK_XFRCM;
  985. USB_OTG_DEV->DIEPMSK = USB_OTG_DIEPMSK_XFRCM;
  986. memset(&g_dwc2_udc[busid], 0, sizeof(struct dwc2_udc));
  987. usbd_event_reset_handler(busid);
  988. /* Start reading setup */
  989. dwc2_ep0_start_read_setup(busid, (uint8_t *)&g_dwc2_udc[busid].setup);
  990. }
  991. if (gint_status & USB_OTG_GINTSTS_ENUMDNE) {
  992. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_ENUMDNE;
  993. dwc2_set_turnaroundtime(busid, SystemCoreClock, dwc2_get_devspeed(busid));
  994. USB_OTG_DEV->DCTL |= USB_OTG_DCTL_CGINAK;
  995. }
  996. if (gint_status & USB_OTG_GINTSTS_PXFR_INCOMPISOOUT) {
  997. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_PXFR_INCOMPISOOUT;
  998. }
  999. if (gint_status & USB_OTG_GINTSTS_IISOIXFR) {
  1000. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_IISOIXFR;
  1001. }
  1002. if (gint_status & USB_OTG_GINTSTS_SOF) {
  1003. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_SOF;
  1004. }
  1005. if (gint_status & USB_OTG_GINTSTS_USBSUSP) {
  1006. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_USBSUSP;
  1007. usbd_event_suspend_handler(busid);
  1008. }
  1009. if (gint_status & USB_OTG_GINTSTS_WKUINT) {
  1010. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_WKUINT;
  1011. usbd_event_resume_handler(busid);
  1012. }
  1013. if (gint_status & USB_OTG_GINTSTS_OTGINT) {
  1014. temp = USB_OTG_GLB->GOTGINT;
  1015. if ((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET) {
  1016. } else {
  1017. }
  1018. USB_OTG_GLB->GOTGINT |= temp;
  1019. }
  1020. }
  1021. }