usb_dc_dwc2.c 45 KB

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