usb_dc_dwc2.c 45 KB

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