usb_hc_dwc2.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_hub.h"
  8. #include "usb_dwc2_reg.h"
  9. #include "usb_dwc2_param.h"
  10. #define USB_OTG_GLB ((DWC2_GlobalTypeDef *)(bus->hcd.reg_base))
  11. #define USB_OTG_PCGCCTL *(__IO uint32_t *)((uint32_t)bus->hcd.reg_base + USB_OTG_PCGCCTL_BASE)
  12. #define USB_OTG_HPRT *(__IO uint32_t *)((uint32_t)bus->hcd.reg_base + USB_OTG_HOST_PORT_BASE)
  13. #define USB_OTG_HOST ((DWC2_HostTypeDef *)(bus->hcd.reg_base + USB_OTG_HOST_BASE))
  14. #define USB_OTG_HC(i) ((DWC2_HostChannelTypeDef *)(bus->hcd.reg_base + USB_OTG_HOST_CHANNEL_BASE + ((i)*USB_OTG_HOST_CHANNEL_SIZE)))
  15. struct dwc2_chan {
  16. uint8_t ep0_state;
  17. uint16_t num_packets;
  18. uint32_t xferlen;
  19. uint8_t chidx;
  20. bool inuse;
  21. bool do_ssplit;
  22. bool do_csplit;
  23. uint8_t hub_addr;
  24. uint8_t hub_port;
  25. uint16_t ssplit_frame;
  26. usb_osal_sem_t waitsem;
  27. struct usbh_urb *urb;
  28. uint32_t iso_frame_idx;
  29. };
  30. struct dwc2_hcd {
  31. volatile bool port_csc;
  32. volatile bool port_pec;
  33. volatile bool port_occ;
  34. struct dwc2_hw_params hw_params;
  35. struct dwc2_user_params user_params;
  36. struct dwc2_chan chan_pool[16];
  37. } g_dwc2_hcd[CONFIG_USBHOST_MAX_BUS];
  38. #define DWC2_EP0_STATE_SETUP 0
  39. #define DWC2_EP0_STATE_INDATA 1
  40. #define DWC2_EP0_STATE_OUTDATA 2
  41. #define DWC2_EP0_STATE_INSTATUS 3
  42. #define DWC2_EP0_STATE_OUTSTATUS 4
  43. static inline int dwc2_reset(struct usbh_bus *bus)
  44. {
  45. volatile uint32_t count = 0U;
  46. /* Wait for AHB master IDLE state. */
  47. do {
  48. if (++count > 200000U) {
  49. return -1;
  50. }
  51. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  52. /* Core Soft Reset */
  53. count = 0U;
  54. USB_OTG_GLB->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
  55. if (g_dwc2_hcd[bus->hcd.hcd_id].hw_params.snpsid < 0x4F54420AU) {
  56. do {
  57. if (++count > 200000U) {
  58. USB_LOG_ERR("DWC2 reset timeout\r\n");
  59. return -1;
  60. }
  61. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);
  62. } else {
  63. do {
  64. if (++count > 200000U) {
  65. USB_LOG_ERR("DWC2 reset timeout\r\n");
  66. return -1;
  67. }
  68. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_CSRSTDONE) != USB_OTG_GRSTCTL_CSRSTDONE);
  69. USB_OTG_GLB->GRSTCTL &= ~USB_OTG_GRSTCTL_CSRST;
  70. USB_OTG_GLB->GRSTCTL |= USB_OTG_GRSTCTL_CSRSTDONE;
  71. }
  72. return 0;
  73. }
  74. static inline int dwc2_core_init(struct usbh_bus *bus)
  75. {
  76. int ret;
  77. uint32_t regval;
  78. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  79. /* Select FS Embedded PHY */
  80. USB_OTG_GLB->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
  81. } else {
  82. regval = USB_OTG_GLB->GUSBCFG;
  83. regval &= ~USB_OTG_GUSBCFG_PHYSEL;
  84. /* disable external vbus source */
  85. regval &= ~(USB_OTG_GUSBCFG_ULPIEVBUSD | USB_OTG_GUSBCFG_ULPIEVBUSI);
  86. /* disable ULPI FS/LS */
  87. regval &= ~(USB_OTG_GUSBCFG_ULPIFSLS | USB_OTG_GUSBCFG_ULPICSM);
  88. switch (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_type) {
  89. case DWC2_PHY_TYPE_PARAM_ULPI:
  90. regval |= USB_OTG_GUSBCFG_ULPI_UTMI_SEL;
  91. regval &= ~USB_OTG_GUSBCFG_PHYIF16;
  92. regval &= ~USB_OTG_GUSBCFG_DDR_SEL;
  93. break;
  94. case DWC2_PHY_TYPE_PARAM_UTMI:
  95. regval &= ~USB_OTG_GUSBCFG_ULPI_UTMI_SEL;
  96. regval &= ~USB_OTG_GUSBCFG_PHYIF16;
  97. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_utmi_width == 16) {
  98. regval |= USB_OTG_GUSBCFG_PHYIF16;
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. USB_OTG_GLB->GUSBCFG = regval;
  105. }
  106. /* Reset after a PHY select */
  107. ret = dwc2_reset(bus);
  108. return ret;
  109. }
  110. static inline void dwc2_set_mode(struct usbh_bus *bus, uint8_t mode)
  111. {
  112. USB_OTG_GLB->GUSBCFG &= ~(USB_OTG_GUSBCFG_FHMOD | USB_OTG_GUSBCFG_FDMOD);
  113. if (mode == USB_OTG_MODE_HOST) {
  114. USB_OTG_GLB->GUSBCFG |= USB_OTG_GUSBCFG_FHMOD;
  115. } else if (mode == USB_OTG_MODE_DEVICE) {
  116. USB_OTG_GLB->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
  117. }
  118. while (1) {
  119. if ((USB_OTG_GLB->GINTSTS & 0x1U) == USB_OTG_MODE_HOST) {
  120. break;
  121. }
  122. usb_osal_msleep(10);
  123. }
  124. }
  125. static inline int dwc2_flush_rxfifo(struct usbh_bus *bus)
  126. {
  127. volatile uint32_t count = 0U;
  128. /* Wait for AHB master IDLE state. */
  129. do {
  130. if (++count > 200000U) {
  131. return -1;
  132. }
  133. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  134. count = 0;
  135. USB_OTG_GLB->GRSTCTL = USB_OTG_GRSTCTL_RXFFLSH;
  136. do {
  137. if (++count > 200000U) {
  138. return -1;
  139. }
  140. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) == USB_OTG_GRSTCTL_RXFFLSH);
  141. return 0;
  142. }
  143. static inline int dwc2_flush_txfifo(struct usbh_bus *bus, uint32_t num)
  144. {
  145. volatile uint32_t count = 0U;
  146. /* Wait for AHB master IDLE state. */
  147. do {
  148. if (++count > 200000U) {
  149. return -1;
  150. }
  151. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  152. count = 0;
  153. USB_OTG_GLB->GRSTCTL = (USB_OTG_GRSTCTL_TXFFLSH | (num << 6));
  154. do {
  155. if (++count > 200000U) {
  156. return -1;
  157. }
  158. } while ((USB_OTG_GLB->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH) == USB_OTG_GRSTCTL_TXFFLSH);
  159. return 0;
  160. }
  161. static inline void dwc2_drivebus(struct usbh_bus *bus, uint8_t state)
  162. {
  163. __IO uint32_t hprt0 = 0U;
  164. hprt0 = USB_OTG_HPRT;
  165. hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  166. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  167. if (((hprt0 & USB_OTG_HPRT_PPWR) == 0U) && (state == 1U)) {
  168. USB_OTG_HPRT = (USB_OTG_HPRT_PPWR | hprt0);
  169. }
  170. if (((hprt0 & USB_OTG_HPRT_PPWR) == USB_OTG_HPRT_PPWR) && (state == 0U)) {
  171. USB_OTG_HPRT = ((~USB_OTG_HPRT_PPWR) & hprt0);
  172. }
  173. }
  174. static inline uint8_t usbh_get_port_speed(struct usbh_bus *bus, const uint8_t port)
  175. {
  176. __IO uint32_t hprt0 = 0U;
  177. uint8_t speed;
  178. hprt0 = USB_OTG_HPRT;
  179. speed = (hprt0 & USB_OTG_HPRT_PSPD) >> 17;
  180. if (speed == HPRT0_PRTSPD_HIGH_SPEED) {
  181. return USB_SPEED_HIGH;
  182. } else if (speed == HPRT0_PRTSPD_FULL_SPEED) {
  183. return USB_SPEED_FULL;
  184. } else if (speed == HPRT0_PRTSPD_LOW_SPEED) {
  185. return USB_SPEED_LOW;
  186. } else {
  187. return USB_SPEED_UNKNOWN;
  188. }
  189. }
  190. static inline void dwc2_chan_char_init(struct usbh_bus *bus,
  191. uint8_t ch_num,
  192. uint8_t dev_addr,
  193. uint8_t ep_addr,
  194. uint8_t ep_type,
  195. uint16_t ep_mps,
  196. uint8_t ep_mult,
  197. uint8_t speed)
  198. {
  199. uint32_t regval;
  200. /* Program the HCCHAR register */
  201. regval = (((uint32_t)ep_mps << USB_OTG_HCCHAR_MPSIZ_Pos) & USB_OTG_HCCHAR_MPSIZ) |
  202. ((((uint32_t)ep_addr & 0x7FU) << USB_OTG_HCCHAR_EPNUM_Pos) & USB_OTG_HCCHAR_EPNUM) |
  203. (((uint32_t)ep_type << USB_OTG_HCCHAR_EPTYP_Pos) & USB_OTG_HCCHAR_EPTYP) |
  204. (((uint32_t)ep_mult << USB_OTG_HCCHAR_MC_Pos) & USB_OTG_HCCHAR_MC) |
  205. (((uint32_t)dev_addr << USB_OTG_HCCHAR_DAD_Pos) & USB_OTG_HCCHAR_DAD);
  206. if ((ep_addr & 0x80U) == 0x80U) {
  207. regval |= USB_OTG_HCCHAR_EPDIR;
  208. }
  209. /* LS device plugged to HUB */
  210. if ((speed == USB_SPEED_LOW) && (usbh_get_port_speed(bus, 0) != USB_SPEED_LOW)) {
  211. regval |= USB_OTG_HCCHAR_LSDEV;
  212. }
  213. if (ep_type == USB_ENDPOINT_TYPE_INTERRUPT) {
  214. regval |= USB_OTG_HCCHAR_ODDFRM;
  215. }
  216. USB_OTG_HC((uint32_t)ch_num)->HCCHAR = regval;
  217. }
  218. static inline void dwc2_chan_splt_init(struct usbh_bus *bus, uint8_t ch_num)
  219. {
  220. struct dwc2_chan *chan;
  221. uint32_t hcsplt;
  222. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[ch_num];
  223. if (chan->do_ssplit) {
  224. hcsplt = USB_OTG_HCSPLT_SPLITEN;
  225. hcsplt |= (chan->hub_addr << USB_OTG_HCSPLT_HUBADDR_Pos);
  226. hcsplt |= chan->hub_port;
  227. if (chan->do_csplit) {
  228. hcsplt |= USB_OTG_HCSPLT_COMPLSPLT;
  229. } else {
  230. hcsplt &= ~USB_OTG_HCSPLT_COMPLSPLT;
  231. }
  232. USB_OTG_HC((uint32_t)ch_num)->HCSPLT = hcsplt;
  233. } else {
  234. USB_OTG_HC((uint32_t)ch_num)->HCSPLT = 0U;
  235. }
  236. }
  237. static void dwc2_chan_init(struct usbh_bus *bus,
  238. uint8_t ch_num,
  239. uint8_t dev_addr,
  240. uint8_t ep_addr,
  241. uint8_t ep_type,
  242. uint16_t ep_mps,
  243. uint8_t ep_mult,
  244. uint8_t speed)
  245. {
  246. /* Clear old interrupt conditions for this host channel. */
  247. USB_OTG_HC((uint32_t)ch_num)->HCINT = 0xFFFFFFFFU;
  248. /* Enable channel interrupts required for this transfer. */
  249. USB_OTG_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_CHHM;
  250. /* Enable the top level host channel interrupt. */
  251. USB_OTG_HOST->HAINTMSK |= 1UL << (ch_num & 0xFU);
  252. dwc2_chan_char_init(bus, ch_num, dev_addr, ep_addr, ep_type, ep_mps, ep_mult, speed);
  253. dwc2_chan_splt_init(bus, ch_num);
  254. }
  255. static inline void dwc2_chan_transfer(struct usbh_bus *bus, uint8_t ch_num, uint8_t ep_addr, uint8_t *buf, uint32_t size, uint16_t num_packets, uint8_t pid)
  256. {
  257. __IO uint32_t tmpreg;
  258. uint8_t is_oddframe;
  259. /* Initialize the HCTSIZn register */
  260. USB_OTG_HC(ch_num)->HCTSIZ = (size & USB_OTG_HCTSIZ_XFRSIZ) |
  261. (((uint32_t)num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |
  262. (((uint32_t)pid << 29) & USB_OTG_HCTSIZ_DPID);
  263. /* xfer_buff MUST be 32-bits aligned */
  264. USB_OTG_HC(ch_num)->HCDMA = (uint32_t)buf;
  265. is_oddframe = (((uint32_t)USB_OTG_HOST->HFNUM & 0x01U) != 0U) ? 0U : 1U;
  266. USB_OTG_HC(ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_ODDFRM;
  267. USB_OTG_HC(ch_num)->HCCHAR |= (uint32_t)is_oddframe << 29;
  268. /* Set host channel enable */
  269. tmpreg = USB_OTG_HC(ch_num)->HCCHAR;
  270. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  271. tmpreg |= USB_OTG_HCCHAR_CHENA;
  272. USB_OTG_HC(ch_num)->HCCHAR = tmpreg;
  273. }
  274. static inline void dwc2_chan_enable_csplit(struct usbh_bus *bus, uint8_t ch_num, bool enable)
  275. {
  276. if (enable) {
  277. USB_OTG_HC((uint32_t)ch_num)->HCSPLT |= USB_OTG_HCSPLT_COMPLSPLT;
  278. } else {
  279. USB_OTG_HC((uint32_t)ch_num)->HCSPLT &= ~USB_OTG_HCSPLT_COMPLSPLT;
  280. }
  281. }
  282. static void dwc2_halt(struct usbh_bus *bus, uint8_t ch_num)
  283. {
  284. volatile uint32_t ChannelEna = (USB_OTG_HC(ch_num)->HCCHAR & USB_OTG_HCCHAR_CHENA) >> 31;
  285. volatile uint32_t count = 0U;
  286. volatile uint32_t value;
  287. if (((USB_OTG_GLB->GAHBCFG & USB_OTG_GAHBCFG_DMAEN) == USB_OTG_GAHBCFG_DMAEN) &&
  288. (ChannelEna == 0U)) {
  289. return;
  290. }
  291. USB_OTG_HC(ch_num)->HCINTMSK = 0;
  292. value = USB_OTG_HC(ch_num)->HCCHAR;
  293. value |= USB_OTG_HCCHAR_CHDIS;
  294. value |= USB_OTG_HCCHAR_CHENA;
  295. USB_OTG_HC(ch_num)->HCCHAR = value;
  296. do {
  297. if (++count > 200000U) {
  298. break;
  299. }
  300. } while (USB_OTG_HC(ch_num)->HCCHAR & USB_OTG_HCCHAR_CHENA);
  301. USB_OTG_HC(ch_num)->HCINT = USB_OTG_HC(ch_num)->HCINT;
  302. }
  303. static int usbh_reset_port(struct usbh_bus *bus, const uint8_t port)
  304. {
  305. __IO uint32_t hprt0 = 0U;
  306. volatile uint32_t timeout = 0;
  307. hprt0 = USB_OTG_HPRT;
  308. hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  309. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  310. USB_OTG_HPRT = (USB_OTG_HPRT_PRST | hprt0);
  311. usb_osal_msleep(100U);
  312. USB_OTG_HPRT = ((~USB_OTG_HPRT_PRST) & hprt0);
  313. usb_osal_msleep(10U);
  314. while (!(USB_OTG_HPRT & USB_OTG_HPRT_PENA)) {
  315. if (!(USB_OTG_HPRT & USB_OTG_HPRT_PCSTS)) {
  316. return -USB_ERR_NOTCONN; /* Port not connected */
  317. }
  318. timeout++;
  319. if (timeout > 10) {
  320. USB_LOG_ERR("Reset port timeout\r\n");
  321. return -USB_ERR_TIMEOUT;
  322. }
  323. usb_osal_msleep(10U);
  324. }
  325. return 0;
  326. }
  327. /**
  328. * @brief dwc2_get_glb_intstatus: return the global USB interrupt status
  329. * @retval status
  330. */
  331. static inline uint32_t dwc2_get_glb_intstatus(struct usbh_bus *bus)
  332. {
  333. uint32_t tmpreg;
  334. tmpreg = USB_OTG_GLB->GINTSTS;
  335. tmpreg &= USB_OTG_GLB->GINTMSK;
  336. return tmpreg;
  337. }
  338. static inline uint16_t dwc2_get_full_frame_num(struct usbh_bus *bus)
  339. {
  340. uint16_t frame = usbh_get_frame_number(bus);
  341. /* USB_OTG_HFNUM_FRNUM_Msk is 0xFFFF but max frame num is 0x3FFF */
  342. return ((frame & 0x3FFF) >> 3);
  343. }
  344. /**
  345. * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
  346. * the HFIR register according to PHY type and speed
  347. *
  348. * NOTE: The caller can modify the value of the HFIR register only after the
  349. * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
  350. * has been set
  351. */
  352. uint32_t dwc2_calc_frame_interval(struct usbh_bus *bus)
  353. {
  354. uint32_t usbcfg;
  355. uint32_t hprt0;
  356. int clock = 60; /* default value */
  357. usbcfg = USB_OTG_GLB->GUSBCFG;
  358. hprt0 = USB_OTG_HPRT;
  359. if (!(usbcfg & USB_OTG_GUSBCFG_PHYSEL) && (usbcfg & USB_OTG_GUSBCFG_ULPI_UTMI_SEL) &&
  360. !(usbcfg & USB_OTG_GUSBCFG_PHYIF16))
  361. clock = 60;
  362. if ((usbcfg & USB_OTG_GUSBCFG_PHYSEL) && g_dwc2_hcd[bus->hcd.hcd_id].hw_params.fs_phy_type ==
  363. GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
  364. clock = 48;
  365. if (!(usbcfg & USB_OTG_GUSBCFG_PHYLPCS) && !(usbcfg & USB_OTG_GUSBCFG_PHYSEL) &&
  366. !(usbcfg & USB_OTG_GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & USB_OTG_GUSBCFG_PHYIF16))
  367. clock = 30;
  368. if (!(usbcfg & USB_OTG_GUSBCFG_PHYLPCS) && !(usbcfg & USB_OTG_GUSBCFG_PHYSEL) &&
  369. !(usbcfg & USB_OTG_GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & USB_OTG_GUSBCFG_PHYIF16))
  370. clock = 60;
  371. if ((usbcfg & USB_OTG_GUSBCFG_PHYLPCS) && !(usbcfg & USB_OTG_GUSBCFG_PHYSEL) &&
  372. !(usbcfg & USB_OTG_GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & USB_OTG_GUSBCFG_PHYIF16))
  373. clock = 48;
  374. if ((usbcfg & USB_OTG_GUSBCFG_PHYSEL) && !(usbcfg & USB_OTG_GUSBCFG_PHYIF16) &&
  375. g_dwc2_hcd[bus->hcd.hcd_id].hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
  376. clock = 48;
  377. if ((usbcfg & USB_OTG_GUSBCFG_PHYSEL) &&
  378. g_dwc2_hcd[bus->hcd.hcd_id].hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  379. clock = 48;
  380. if ((hprt0 & USB_OTG_HPRT_PSPD) >> USB_OTG_HPRT_PSPD_Pos == HPRT0_PRTSPD_HIGH_SPEED)
  381. /* High speed case */
  382. return 125 * clock - 1;
  383. /* FS/LS case */
  384. return 1000 * clock - 1;
  385. }
  386. static int dwc2_chan_alloc(struct usbh_bus *bus)
  387. {
  388. size_t flags;
  389. int chidx;
  390. flags = usb_osal_enter_critical_section();
  391. for (chidx = 0; chidx < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; chidx++) {
  392. if (!g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].inuse) {
  393. g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].inuse = true;
  394. usb_osal_leave_critical_section(flags);
  395. g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].do_ssplit = 0;
  396. g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].do_csplit = 0;
  397. return chidx;
  398. }
  399. }
  400. usb_osal_leave_critical_section(flags);
  401. return -1;
  402. }
  403. static void dwc2_chan_free(struct dwc2_chan *chan)
  404. {
  405. size_t flags;
  406. flags = usb_osal_enter_critical_section();
  407. if (chan->urb) {
  408. chan->urb->hcpriv = NULL;
  409. chan->urb = NULL;
  410. }
  411. chan->inuse = false;
  412. usb_osal_leave_critical_section(flags);
  413. }
  414. static uint16_t dwc2_calculate_packet_num(uint32_t input_size, uint8_t ep_addr, uint16_t ep_mps, uint32_t *output_size)
  415. {
  416. uint16_t num_packets;
  417. num_packets = (uint16_t)((input_size + ep_mps - 1U) / ep_mps);
  418. if (num_packets > 0x3FF) {
  419. num_packets = 0x3FF; // pktcnt 10bits
  420. }
  421. if (input_size == 0) {
  422. num_packets = 1;
  423. }
  424. if (ep_addr & 0x80) {
  425. input_size = num_packets * ep_mps;
  426. } else {
  427. }
  428. *output_size = input_size;
  429. return num_packets;
  430. }
  431. static void dwc2_control_urb_init(struct usbh_bus *bus, uint8_t chidx, struct usbh_urb *urb, struct usb_setup_packet *setup, uint8_t *buffer, uint32_t buflen)
  432. {
  433. struct dwc2_chan *chan;
  434. uint32_t datalen;
  435. uint8_t data_pid;
  436. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
  437. /* split buflen with ep mps */
  438. if (chan->do_ssplit && (chan->ep0_state == DWC2_EP0_STATE_INDATA || chan->ep0_state == DWC2_EP0_STATE_OUTDATA)) {
  439. if (buflen > USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize)) {
  440. datalen = USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize);
  441. } else {
  442. datalen = buflen;
  443. }
  444. if (urb->data_toggle == 0) {
  445. data_pid = HC_PID_DATA0;
  446. } else {
  447. data_pid = HC_PID_DATA1;
  448. }
  449. } else {
  450. datalen = buflen; // buflen = setup->wLength
  451. data_pid = HC_PID_DATA1;
  452. }
  453. if (chan->ep0_state == DWC2_EP0_STATE_SETUP) /* fill setup */
  454. {
  455. chan->num_packets = dwc2_calculate_packet_num(8, 0x00, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  456. dwc2_chan_init(bus,
  457. chidx,
  458. urb->hport->dev_addr,
  459. 0x00,
  460. USB_ENDPOINT_TYPE_CONTROL,
  461. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  462. 1,
  463. urb->hport->speed);
  464. dwc2_chan_transfer(bus, chidx, 0x00, (uint8_t *)setup, chan->xferlen, chan->num_packets, HC_PID_SETUP);
  465. } else if (chan->ep0_state == DWC2_EP0_STATE_INDATA) /* fill in data */
  466. {
  467. chan->num_packets = dwc2_calculate_packet_num(datalen, 0x80, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  468. dwc2_chan_init(bus,
  469. chidx,
  470. urb->hport->dev_addr,
  471. 0x80,
  472. USB_ENDPOINT_TYPE_CONTROL,
  473. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  474. 1,
  475. urb->hport->speed);
  476. dwc2_chan_transfer(bus, chidx, 0x80, buffer, chan->xferlen, chan->num_packets, data_pid);
  477. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTDATA) /* fill out data */
  478. {
  479. chan->num_packets = dwc2_calculate_packet_num(datalen, 0x00, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  480. dwc2_chan_init(bus,
  481. chidx,
  482. urb->hport->dev_addr,
  483. 0x00,
  484. USB_ENDPOINT_TYPE_CONTROL,
  485. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  486. 1,
  487. urb->hport->speed);
  488. dwc2_chan_transfer(bus, chidx, 0x00, buffer, chan->xferlen, chan->num_packets, data_pid);
  489. } else if (chan->ep0_state == DWC2_EP0_STATE_INSTATUS) /* fill in status */
  490. {
  491. chan->num_packets = dwc2_calculate_packet_num(0, 0x80, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  492. dwc2_chan_init(bus,
  493. chidx,
  494. urb->hport->dev_addr,
  495. 0x80,
  496. USB_ENDPOINT_TYPE_CONTROL,
  497. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  498. 1,
  499. urb->hport->speed);
  500. dwc2_chan_transfer(bus, chidx, 0x80, NULL, chan->xferlen, chan->num_packets, HC_PID_DATA1);
  501. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTSTATUS) /* fill out status */
  502. {
  503. chan->num_packets = dwc2_calculate_packet_num(0, 0x00, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  504. dwc2_chan_init(bus,
  505. chidx,
  506. urb->hport->dev_addr,
  507. 0x00,
  508. USB_ENDPOINT_TYPE_CONTROL,
  509. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  510. 1,
  511. urb->hport->speed);
  512. dwc2_chan_transfer(bus, chidx, 0x00, NULL, chan->xferlen, chan->num_packets, HC_PID_DATA1);
  513. }
  514. }
  515. static void dwc2_bulk_intr_urb_init(struct usbh_bus *bus, uint8_t chidx, struct usbh_urb *urb, uint8_t *buffer, uint32_t buflen)
  516. {
  517. struct dwc2_chan *chan;
  518. uint32_t datalen;
  519. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
  520. if (chan->do_ssplit) {
  521. if (buflen > USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize)) {
  522. datalen = USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize);
  523. } else {
  524. datalen = buflen;
  525. }
  526. } else {
  527. datalen = buflen;
  528. }
  529. chan->num_packets = dwc2_calculate_packet_num(datalen, urb->ep->bEndpointAddress, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  530. dwc2_chan_init(bus,
  531. chidx,
  532. urb->hport->dev_addr,
  533. urb->ep->bEndpointAddress,
  534. USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes),
  535. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  536. USB_GET_MULT(urb->ep->wMaxPacketSize) + 1,
  537. urb->hport->speed);
  538. dwc2_chan_transfer(bus, chidx, urb->ep->bEndpointAddress, buffer, chan->xferlen, chan->num_packets, urb->data_toggle == 0 ? HC_PID_DATA0 : HC_PID_DATA1);
  539. }
  540. #if 0
  541. static void dwc2_iso_urb_init(struct usbh_bus *bus, uint8_t chidx, struct usbh_urb *urb, struct usbh_iso_frame_packet *iso_packet)
  542. {
  543. struct dwc2_chan *chan;
  544. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
  545. chan->num_packets = dwc2_calculate_packet_num(iso_packet->transfer_buffer_length, urb->ep->bEndpointAddress, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  546. dwc2_chan_init(bus, chidx, urb->hport->dev_addr, urb->ep->bEndpointAddress, USB_ENDPOINT_TYPE_ISOCHRONOUS, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), urb->hport->speed);
  547. dwc2_chan_transfer(bus, chidx, urb->ep->bEndpointAddress, iso_packet->transfer_buffer, chan->xferlen, chan->num_packets, HC_PID_DATA0);
  548. }
  549. #endif
  550. __WEAK void usb_hc_low_level_init(struct usbh_bus *bus)
  551. {
  552. (void)bus;
  553. }
  554. __WEAK void usb_hc_low_level_deinit(struct usbh_bus *bus)
  555. {
  556. (void)bus;
  557. }
  558. int usb_hc_init(struct usbh_bus *bus)
  559. {
  560. int ret;
  561. memset(&g_dwc2_hcd[bus->hcd.hcd_id], 0, sizeof(struct dwc2_hcd));
  562. usb_hc_low_level_init(bus);
  563. USB_LOG_INFO("========== dwc2 hcd params ==========\r\n");
  564. USB_LOG_INFO("CID:%08x\r\n", (unsigned int)USB_OTG_GLB->CID);
  565. USB_LOG_INFO("GSNPSID:%08x\r\n", (unsigned int)USB_OTG_GLB->GSNPSID);
  566. USB_LOG_INFO("GHWCFG1:%08x\r\n", (unsigned int)USB_OTG_GLB->GHWCFG1);
  567. USB_LOG_INFO("GHWCFG2:%08x\r\n", (unsigned int)USB_OTG_GLB->GHWCFG2);
  568. USB_LOG_INFO("GHWCFG3:%08x\r\n", (unsigned int)USB_OTG_GLB->GHWCFG3);
  569. USB_LOG_INFO("GHWCFG4:%08x\r\n", (unsigned int)USB_OTG_GLB->GHWCFG4);
  570. dwc2_get_hwparams(bus->hcd.reg_base, &g_dwc2_hcd[bus->hcd.hcd_id].hw_params);
  571. dwc2_get_user_params(bus->hcd.reg_base, &g_dwc2_hcd[bus->hcd.hcd_id].user_params);
  572. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_utmi_width == 0) {
  573. g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_utmi_width = 8;
  574. }
  575. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.total_fifo_size == 0) {
  576. g_dwc2_hcd[bus->hcd.hcd_id].user_params.total_fifo_size = g_dwc2_hcd[bus->hcd.hcd_id].hw_params.total_fifo_size;
  577. }
  578. for (uint8_t chidx = 0; chidx < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; chidx++) {
  579. g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].waitsem = usb_osal_sem_create(0);
  580. }
  581. USB_LOG_INFO("dwc2 has %d channels and dfifo depth(32-bit words) is %d\r\n",
  582. g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels,
  583. g_dwc2_hcd[bus->hcd.hcd_id].user_params.total_fifo_size);
  584. USB_ASSERT_MSG(g_dwc2_hcd[bus->hcd.hcd_id].hw_params.arch == GHWCFG2_INT_DMA_ARCH, "This dwc2 version does not support dma mode, so stop working");
  585. USB_ASSERT_MSG((g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size +
  586. g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_nperio_tx_fifo_size +
  587. g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_perio_tx_fifo_size) <=
  588. g_dwc2_hcd[bus->hcd.hcd_id].user_params.total_fifo_size,
  589. "Your fifo config is overflow, please check");
  590. USB_OTG_GLB->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
  591. /* This is vendor register */
  592. USB_OTG_GLB->GCCFG = g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_gccfg;
  593. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_type != DWC2_PHY_TYPE_PARAM_FS) {
  594. USB_ASSERT_MSG(g_dwc2_hcd[bus->hcd.hcd_id].hw_params.hs_phy_type != 0, "This dwc2 version does not support hs, so stop working");
  595. }
  596. ret = dwc2_core_init(bus);
  597. /* Force Host Mode*/
  598. dwc2_set_mode(bus, USB_OTG_MODE_HOST);
  599. USB_ASSERT_MSG((USB_OTG_GLB->GRXFSIZ & 0xffff) >= g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size,
  600. "host_rx_fifo_size cannot be larger than power_on_value %u", (unsigned int)(USB_OTG_GLB->GRXFSIZ & 0xffff));
  601. USB_ASSERT_MSG(((USB_OTG_GLB->DIEPTXF0_HNPTXFSIZ >> 16) & 0xffff) >= g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_nperio_tx_fifo_size,
  602. "host_nperio_tx_fifo_size cannot be larger than power_on_value %u", (unsigned int)((USB_OTG_GLB->DIEPTXF0_HNPTXFSIZ >> 16) & 0xffff));
  603. USB_ASSERT_MSG(((USB_OTG_GLB->HPTXFSIZ >> 16) & 0xffff) >= g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_perio_tx_fifo_size,
  604. "host_perio_tx_fifo_size cannot be larger than power_on_value %u", (unsigned int)((USB_OTG_GLB->HPTXFSIZ >> 16) & 0xffff));
  605. /* B-peripheral session valid override enable */
  606. USB_OTG_GLB->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOEN;
  607. USB_OTG_GLB->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOVAL;
  608. USB_OTG_GLB->GUSBCFG |= USB_OTG_GUSBCFG_TOCAL;
  609. /* Restart the Phy Clock */
  610. USB_OTG_PCGCCTL = 0U;
  611. /* Set default Max speed support */
  612. USB_OTG_HOST->HCFG &= ~USB_OTG_HCFG_FSLSS;
  613. USB_OTG_HOST->HCFG &= ~USB_OTG_HCFG_FSLSPCS;
  614. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  615. bus->hcd.roothub.speed = USB_SPEED_FULL;
  616. USB_OTG_HOST->HCFG |= USB_OTG_HCFG_FSLSPCLKSEL_48_MHZ;
  617. } else {
  618. bus->hcd.roothub.speed = USB_SPEED_HIGH;
  619. USB_OTG_HOST->HCFG |= USB_OTG_HCFG_FSLSPCLKSEL_30_60_MHZ;
  620. }
  621. if (g_dwc2_hcd[bus->hcd.hcd_id].hw_params.snpsid > 0x4F54292AU) {
  622. USB_OTG_HOST->HFIR |= USB_OTG_HFIR_RELOAD_CTRL;
  623. }
  624. /* Clear all pending HC Interrupts */
  625. for (uint8_t i = 0U; i < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; i++) {
  626. USB_OTG_HC(i)->HCINT = 0xFFFFFFFFU;
  627. USB_OTG_HC(i)->HCINTMSK = 0U;
  628. }
  629. /* Disable all interrupts. */
  630. USB_OTG_GLB->GINTMSK = 0U;
  631. /* Clear any pending interrupts */
  632. USB_OTG_GLB->GINTSTS = 0xFFFFFFFFU;
  633. /* set FIFO size */
  634. USB_OTG_GLB->GRXFSIZ = g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size;
  635. USB_OTG_GLB->DIEPTXF0_HNPTXFSIZ = (uint32_t)(((g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_nperio_tx_fifo_size << 16) & USB_OTG_NPTXFD) |
  636. g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size);
  637. USB_OTG_GLB->HPTXFSIZ = (uint32_t)(((g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_perio_tx_fifo_size << 16) & USB_OTG_HPTXFSIZ_PTXFD) |
  638. (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size + g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_nperio_tx_fifo_size));
  639. ret = dwc2_flush_txfifo(bus, 0x10U);
  640. ret = dwc2_flush_rxfifo(bus);
  641. USB_OTG_GLB->GAHBCFG &= ~USB_OTG_GAHBCFG_HBSTLEN;
  642. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_HBSTLEN_4;
  643. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_DMAEN;
  644. /* Enable interrupts matching to the Host mode ONLY */
  645. USB_OTG_GLB->GINTMSK |= (USB_OTG_GINTMSK_PRTIM | USB_OTG_GINTMSK_HCIM |
  646. USB_OTG_GINTSTS_DISCINT);
  647. dwc2_drivebus(bus, 1);
  648. usb_osal_msleep(200);
  649. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
  650. return ret;
  651. }
  652. int usb_hc_deinit(struct usbh_bus *bus)
  653. {
  654. USB_OTG_GLB->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
  655. dwc2_flush_txfifo(bus, 0x10U);
  656. dwc2_flush_rxfifo(bus);
  657. /* Flush out any leftover queued requests. */
  658. for (uint8_t chidx = 0; chidx < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; chidx++) {
  659. dwc2_halt(bus, chidx);
  660. }
  661. /* Disable all interrupts. */
  662. USB_OTG_GLB->GINTMSK = 0U;
  663. /* Clear any pending Host interrupts */
  664. USB_OTG_HOST->HAINT = 0xFFFFFFFFU;
  665. USB_OTG_GLB->GINTSTS = 0xFFFFFFFFU;
  666. dwc2_drivebus(bus, 0);
  667. usb_osal_msleep(200);
  668. for (uint8_t chidx = 0; chidx < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; chidx++) {
  669. usb_osal_sem_delete(g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].waitsem);
  670. }
  671. usb_hc_low_level_deinit(bus);
  672. return 0;
  673. }
  674. uint16_t usbh_get_frame_number(struct usbh_bus *bus)
  675. {
  676. return (USB_OTG_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
  677. }
  678. int usbh_roothub_control(struct usbh_bus *bus, struct usb_setup_packet *setup, uint8_t *buf)
  679. {
  680. __IO uint32_t hprt0;
  681. uint8_t nports;
  682. uint8_t port;
  683. uint32_t status;
  684. nports = CONFIG_USBHOST_MAX_RHPORTS;
  685. port = setup->wIndex;
  686. if (setup->bmRequestType & USB_REQUEST_RECIPIENT_DEVICE) {
  687. switch (setup->bRequest) {
  688. case HUB_REQUEST_CLEAR_FEATURE:
  689. switch (setup->wValue) {
  690. case HUB_FEATURE_HUB_C_LOCALPOWER:
  691. break;
  692. case HUB_FEATURE_HUB_C_OVERCURRENT:
  693. break;
  694. default:
  695. return -USB_ERR_NOTSUPP;
  696. }
  697. break;
  698. case HUB_REQUEST_SET_FEATURE:
  699. switch (setup->wValue) {
  700. case HUB_FEATURE_HUB_C_LOCALPOWER:
  701. break;
  702. case HUB_FEATURE_HUB_C_OVERCURRENT:
  703. break;
  704. default:
  705. return -USB_ERR_NOTSUPP;
  706. }
  707. break;
  708. case HUB_REQUEST_GET_DESCRIPTOR:
  709. break;
  710. case HUB_REQUEST_GET_STATUS:
  711. memset(buf, 0, 4);
  712. break;
  713. default:
  714. break;
  715. }
  716. } else if (setup->bmRequestType & USB_REQUEST_RECIPIENT_OTHER) {
  717. switch (setup->bRequest) {
  718. case HUB_REQUEST_CLEAR_FEATURE:
  719. if (!port || port > nports) {
  720. return -USB_ERR_INVAL;
  721. }
  722. switch (setup->wValue) {
  723. case HUB_PORT_FEATURE_ENABLE:
  724. USB_OTG_HPRT &= ~USB_OTG_HPRT_PENA;
  725. break;
  726. case HUB_PORT_FEATURE_SUSPEND:
  727. case HUB_PORT_FEATURE_C_SUSPEND:
  728. break;
  729. case HUB_PORT_FEATURE_POWER:
  730. dwc2_drivebus(bus, 0);
  731. break;
  732. case HUB_PORT_FEATURE_C_CONNECTION:
  733. g_dwc2_hcd[bus->hcd.hcd_id].port_csc = 0;
  734. break;
  735. case HUB_PORT_FEATURE_C_ENABLE:
  736. g_dwc2_hcd[bus->hcd.hcd_id].port_pec = 0;
  737. break;
  738. case HUB_PORT_FEATURE_C_OVER_CURREN:
  739. g_dwc2_hcd[bus->hcd.hcd_id].port_occ = 0;
  740. break;
  741. case HUB_PORT_FEATURE_C_RESET:
  742. break;
  743. default:
  744. return -USB_ERR_NOTSUPP;
  745. }
  746. break;
  747. case HUB_REQUEST_SET_FEATURE:
  748. if (!port || port > nports) {
  749. return -USB_ERR_INVAL;
  750. }
  751. switch (setup->wValue) {
  752. case HUB_PORT_FEATURE_SUSPEND:
  753. break;
  754. case HUB_PORT_FEATURE_POWER:
  755. dwc2_drivebus(bus, 1);
  756. break;
  757. case HUB_PORT_FEATURE_RESET:
  758. return usbh_reset_port(bus, port);
  759. default:
  760. return -USB_ERR_NOTSUPP;
  761. }
  762. break;
  763. case HUB_REQUEST_GET_STATUS:
  764. if (!port || port > nports) {
  765. return -USB_ERR_INVAL;
  766. }
  767. hprt0 = USB_OTG_HPRT;
  768. status = 0;
  769. if (g_dwc2_hcd[bus->hcd.hcd_id].port_csc) {
  770. status |= (1 << HUB_PORT_FEATURE_C_CONNECTION);
  771. }
  772. if (g_dwc2_hcd[bus->hcd.hcd_id].port_pec) {
  773. status |= (1 << HUB_PORT_FEATURE_C_ENABLE);
  774. }
  775. if (g_dwc2_hcd[bus->hcd.hcd_id].port_occ) {
  776. status |= (1 << HUB_PORT_FEATURE_C_OVER_CURREN);
  777. }
  778. if (hprt0 & USB_OTG_HPRT_PCSTS) {
  779. status |= (1 << HUB_PORT_FEATURE_CONNECTION);
  780. }
  781. if (hprt0 & USB_OTG_HPRT_PENA) {
  782. status |= (1 << HUB_PORT_FEATURE_ENABLE);
  783. if (usbh_get_port_speed(bus, port) == USB_SPEED_LOW) {
  784. status |= (1 << HUB_PORT_FEATURE_LOWSPEED);
  785. } else if (usbh_get_port_speed(bus, port) == USB_SPEED_HIGH) {
  786. status |= (1 << HUB_PORT_FEATURE_HIGHSPEED);
  787. }
  788. }
  789. if (hprt0 & USB_OTG_HPRT_POCA) {
  790. status |= (1 << HUB_PORT_FEATURE_OVERCURRENT);
  791. }
  792. if (hprt0 & USB_OTG_HPRT_PRST) {
  793. status |= (1 << HUB_PORT_FEATURE_RESET);
  794. }
  795. if (hprt0 & USB_OTG_HPRT_PPWR) {
  796. status |= (1 << HUB_PORT_FEATURE_POWER);
  797. }
  798. memcpy(buf, &status, 4);
  799. break;
  800. default:
  801. break;
  802. }
  803. }
  804. return 0;
  805. }
  806. int usbh_submit_urb(struct usbh_urb *urb)
  807. {
  808. struct dwc2_chan *chan;
  809. struct usbh_bus *bus;
  810. size_t flags;
  811. int ret = 0;
  812. int chidx;
  813. if (!urb || !urb->hport || !urb->ep || !urb->hport->bus) {
  814. return -USB_ERR_INVAL;
  815. }
  816. /* dma addr must be aligned 4 bytes */
  817. USB_ASSERT_MSG(!((uintptr_t)urb->setup % 4) && !((uintptr_t)urb->transfer_buffer % 4),
  818. "urb->setup or urb->transfer_buffer is not aligned 4 bytes");
  819. #ifdef CONFIG_USB_DCACHE_ENABLE
  820. USB_ASSERT_MSG(!((uintptr_t)urb->setup % CONFIG_USB_ALIGN_SIZE) &&
  821. !((uintptr_t)urb->transfer_buffer % CONFIG_USB_ALIGN_SIZE),
  822. "urb->setup or urb->transfer_buffer is not aligned %d", CONFIG_USB_ALIGN_SIZE);
  823. #endif
  824. bus = urb->hport->bus;
  825. if (!(USB_OTG_HPRT & USB_OTG_HPRT_PCSTS) || !urb->hport->connected) {
  826. return -USB_ERR_NOTCONN;
  827. }
  828. if (urb->errorcode == -USB_ERR_BUSY) {
  829. return -USB_ERR_BUSY;
  830. }
  831. if (urb->ep->bEndpointAddress & 0x80) {
  832. /* Check if pipe rx fifo is overflow */
  833. if (USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) > (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size * 4)) {
  834. return -USB_ERR_RANGE;
  835. }
  836. } else {
  837. /* Check if intr and iso pipe tx fifo is overflow */
  838. if (((USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS) ||
  839. (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT)) &&
  840. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) > (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_perio_tx_fifo_size * 4)) {
  841. return -USB_ERR_RANGE;
  842. } else {
  843. /* Check if control and bulk pipe tx fifo is overflow */
  844. if (USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) > (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_nperio_tx_fifo_size * 4)) {
  845. return -USB_ERR_RANGE;
  846. }
  847. }
  848. }
  849. chidx = dwc2_chan_alloc(bus);
  850. if (chidx == -1) {
  851. return -USB_ERR_NOMEM;
  852. }
  853. flags = usb_osal_enter_critical_section();
  854. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
  855. chan->chidx = chidx;
  856. chan->urb = urb;
  857. chan->do_ssplit = 0;
  858. if (urb->hport->speed != USB_SPEED_HIGH &&
  859. usbh_get_port_speed(bus, 0) == USB_SPEED_HIGH) {
  860. chan->do_ssplit = 1;
  861. chan->do_csplit = 0;
  862. chan->hub_port = urb->hport->port;
  863. chan->hub_addr = urb->hport->parent->hub_addr;
  864. }
  865. urb->hcpriv = chan;
  866. urb->errorcode = -USB_ERR_BUSY;
  867. urb->actual_length = 0;
  868. usb_osal_leave_critical_section(flags);
  869. if (urb->setup) {
  870. usb_dcache_clean((uintptr_t)urb->setup, USB_ALIGN_UP(sizeof(struct usb_setup_packet), CONFIG_USB_ALIGN_SIZE));
  871. if (urb->transfer_buffer) {
  872. if (urb->setup->bmRequestType & 0x80) {
  873. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  874. } else {
  875. usb_dcache_clean((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  876. }
  877. }
  878. } else if (urb->transfer_buffer && (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) != USB_ENDPOINT_TYPE_ISOCHRONOUS)) {
  879. if (urb->ep->bEndpointAddress & 0x80) {
  880. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  881. } else {
  882. usb_dcache_clean((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  883. }
  884. } else {
  885. }
  886. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  887. case USB_ENDPOINT_TYPE_CONTROL:
  888. chan->ep0_state = DWC2_EP0_STATE_SETUP;
  889. dwc2_control_urb_init(bus, chidx, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  890. break;
  891. case USB_ENDPOINT_TYPE_BULK:
  892. case USB_ENDPOINT_TYPE_INTERRUPT:
  893. dwc2_bulk_intr_urb_init(bus, chidx, urb, urb->transfer_buffer, urb->transfer_buffer_length);
  894. break;
  895. case USB_ENDPOINT_TYPE_ISOCHRONOUS:
  896. break;
  897. default:
  898. break;
  899. }
  900. if (urb->timeout > 0) {
  901. /* wait until timeout or sem give */
  902. ret = usb_osal_sem_take(chan->waitsem, urb->timeout);
  903. if (ret < 0) {
  904. goto errout_timeout;
  905. }
  906. urb->timeout = 0;
  907. ret = urb->errorcode;
  908. /* we can free chan when waitsem is done */
  909. dwc2_chan_free(chan);
  910. }
  911. return ret;
  912. errout_timeout:
  913. urb->timeout = 0;
  914. usbh_kill_urb(urb);
  915. return ret;
  916. }
  917. int usbh_kill_urb(struct usbh_urb *urb)
  918. {
  919. struct dwc2_chan *chan;
  920. struct usbh_bus *bus;
  921. size_t flags;
  922. if (!urb || !urb->hcpriv || !urb->hport->bus) {
  923. return -USB_ERR_INVAL;
  924. }
  925. bus = urb->hport->bus;
  926. flags = usb_osal_enter_critical_section();
  927. chan = (struct dwc2_chan *)urb->hcpriv;
  928. dwc2_halt(bus, chan->chidx);
  929. urb->errorcode = -USB_ERR_SHUTDOWN;
  930. if (urb->timeout) {
  931. usb_osal_sem_give(chan->waitsem);
  932. } else {
  933. dwc2_chan_free(chan);
  934. }
  935. if (urb->complete) {
  936. urb->complete(urb->arg, urb->errorcode);
  937. }
  938. usb_osal_leave_critical_section(flags);
  939. return 0;
  940. }
  941. static inline void dwc2_urb_waitup(struct usbh_urb *urb)
  942. {
  943. struct dwc2_chan *chan;
  944. chan = (struct dwc2_chan *)urb->hcpriv;
  945. if (urb->timeout) {
  946. usb_osal_sem_give(chan->waitsem);
  947. } else {
  948. dwc2_chan_free(chan);
  949. }
  950. if (urb->complete) {
  951. if (urb->errorcode < 0) {
  952. urb->complete(urb->arg, urb->errorcode);
  953. } else {
  954. urb->complete(urb->arg, urb->actual_length);
  955. }
  956. }
  957. }
  958. static void dwc2_inchan_irq_handler(struct usbh_bus *bus, uint8_t ch_num)
  959. {
  960. uint32_t chan_intstatus;
  961. struct dwc2_chan *chan;
  962. struct usbh_urb *urb;
  963. chan_intstatus = USB_OTG_HC(ch_num)->HCINT;
  964. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[ch_num];
  965. urb = chan->urb;
  966. //printf("s1:%08x\r\n", chan_intstatus);
  967. if (chan_intstatus & USB_OTG_HCINT_CHH) {
  968. USB_OTG_HC(ch_num)->HCINT = chan_intstatus;
  969. if (chan_intstatus & USB_OTG_HCINT_XFRC) {
  970. uint32_t count = chan->xferlen - (USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ); /* how many size has received */
  971. uint8_t data_toggle = ((USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_DPID) >> USB_OTG_HCTSIZ_DPID_Pos);
  972. urb->actual_length += count;
  973. urb->transfer_buffer_length -= count;
  974. if (data_toggle == HC_PID_DATA0) {
  975. urb->data_toggle = 0;
  976. } else {
  977. urb->data_toggle = 1;
  978. }
  979. if (chan->do_csplit) {
  980. chan->do_csplit = 0;
  981. dwc2_chan_enable_csplit(bus, ch_num, false);
  982. }
  983. if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_CONTROL) {
  984. if (chan->ep0_state == DWC2_EP0_STATE_INDATA) {
  985. if (chan->do_ssplit && urb->transfer_buffer_length > 0 && (count == USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize))) {
  986. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  987. } else {
  988. chan->ep0_state = DWC2_EP0_STATE_OUTSTATUS;
  989. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  990. }
  991. } else if (chan->ep0_state == DWC2_EP0_STATE_INSTATUS) {
  992. chan->ep0_state = DWC2_EP0_STATE_SETUP;
  993. urb->errorcode = 0;
  994. dwc2_urb_waitup(urb);
  995. }
  996. } else if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  997. } else {
  998. if (chan->do_ssplit && urb->transfer_buffer_length > 0 && (count == USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize))) {
  999. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1000. } else {
  1001. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->actual_length, CONFIG_USB_ALIGN_SIZE));
  1002. urb->errorcode = 0;
  1003. dwc2_urb_waitup(urb);
  1004. }
  1005. }
  1006. } else if (chan_intstatus & USB_OTG_HCINT_AHBERR) {
  1007. urb->errorcode = -USB_ERR_IO;
  1008. dwc2_urb_waitup(urb);
  1009. } else if (chan_intstatus & USB_OTG_HCINT_STALL) {
  1010. urb->errorcode = -USB_ERR_STALL;
  1011. dwc2_urb_waitup(urb);
  1012. } else if (chan_intstatus & USB_OTG_HCINT_NAK) {
  1013. if (chan->do_ssplit) {
  1014. /* restart ssplit transfer */
  1015. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1016. case USB_ENDPOINT_TYPE_CONTROL:
  1017. chan->do_csplit = 0;
  1018. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1019. break;
  1020. case USB_ENDPOINT_TYPE_BULK:
  1021. chan->do_csplit = 0;
  1022. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1023. break;
  1024. case USB_ENDPOINT_TYPE_INTERRUPT:
  1025. chan->do_csplit = 0;
  1026. dwc2_chan_enable_csplit(bus, ch_num, false);
  1027. urb->errorcode = -USB_ERR_NAK;
  1028. dwc2_urb_waitup(urb);
  1029. break;
  1030. default:
  1031. break;
  1032. }
  1033. } else {
  1034. urb->errorcode = -USB_ERR_NAK;
  1035. dwc2_urb_waitup(urb);
  1036. }
  1037. } else if (chan_intstatus & USB_OTG_HCINT_ACK) {
  1038. if (chan->do_ssplit) {
  1039. /* start csplit transfer */
  1040. chan->do_csplit = 1;
  1041. chan->ssplit_frame = dwc2_get_full_frame_num(bus);
  1042. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1043. case USB_ENDPOINT_TYPE_CONTROL:
  1044. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1045. break;
  1046. case USB_ENDPOINT_TYPE_BULK:
  1047. case USB_ENDPOINT_TYPE_INTERRUPT:
  1048. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1049. break;
  1050. default:
  1051. break;
  1052. }
  1053. }
  1054. } else if (chan_intstatus & USB_OTG_HCINT_NYET) {
  1055. if (chan->do_ssplit) {
  1056. /* restart csplit transfer */
  1057. chan->do_csplit = 1;
  1058. chan->ssplit_frame = dwc2_get_full_frame_num(bus);
  1059. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1060. case USB_ENDPOINT_TYPE_CONTROL:
  1061. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1062. break;
  1063. case USB_ENDPOINT_TYPE_BULK:
  1064. case USB_ENDPOINT_TYPE_INTERRUPT:
  1065. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1066. break;
  1067. default:
  1068. break;
  1069. }
  1070. } else {
  1071. urb->errorcode = -USB_ERR_NAK;
  1072. dwc2_urb_waitup(urb);
  1073. }
  1074. } else if (chan_intstatus & USB_OTG_HCINT_TXERR) {
  1075. urb->errorcode = -USB_ERR_IO;
  1076. dwc2_urb_waitup(urb);
  1077. } else if (chan_intstatus & USB_OTG_HCINT_BBERR) {
  1078. urb->errorcode = -USB_ERR_BABBLE;
  1079. dwc2_urb_waitup(urb);
  1080. } else if (chan_intstatus & USB_OTG_HCINT_DTERR) {
  1081. urb->errorcode = -USB_ERR_DT;
  1082. dwc2_urb_waitup(urb);
  1083. } else if (chan_intstatus & USB_OTG_HCINT_FRMOR) {
  1084. urb->errorcode = -USB_ERR_IO;
  1085. dwc2_urb_waitup(urb);
  1086. }
  1087. }
  1088. }
  1089. static void dwc2_outchan_irq_handler(struct usbh_bus *bus, uint8_t ch_num)
  1090. {
  1091. uint32_t chan_intstatus;
  1092. struct dwc2_chan *chan;
  1093. struct usbh_urb *urb;
  1094. chan_intstatus = USB_OTG_HC(ch_num)->HCINT;
  1095. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[ch_num];
  1096. urb = chan->urb;
  1097. //printf("s2:%08x\r\n", chan_intstatus);
  1098. if (chan_intstatus & USB_OTG_HCINT_CHH) {
  1099. USB_OTG_HC(ch_num)->HCINT = chan_intstatus;
  1100. if (chan_intstatus & USB_OTG_HCINT_XFRC) {
  1101. uint32_t count = USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ; /* last packet size */
  1102. uint32_t has_used_packets = chan->num_packets - ((USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) >> 19); /* how many packets have used */
  1103. uint32_t olen = (has_used_packets - 1) * USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) + count; /* the same with urb->actual_length += chan->xferlen; */
  1104. uint8_t data_toggle = ((USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_DPID) >> USB_OTG_HCTSIZ_DPID_Pos);
  1105. urb->actual_length += olen;
  1106. if (chan->ep0_state == DWC2_EP0_STATE_OUTDATA || urb->setup == NULL) {
  1107. if (urb->transfer_buffer_length > olen) {
  1108. urb->transfer_buffer_length -= olen;
  1109. } else {
  1110. urb->transfer_buffer_length = 0;
  1111. }
  1112. }
  1113. if (data_toggle == HC_PID_DATA0) {
  1114. urb->data_toggle = 0;
  1115. } else {
  1116. urb->data_toggle = 1;
  1117. }
  1118. if (chan->do_csplit) {
  1119. chan->do_csplit = 0;
  1120. dwc2_chan_enable_csplit(bus, ch_num, false);
  1121. }
  1122. if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_CONTROL) {
  1123. if (chan->ep0_state == DWC2_EP0_STATE_SETUP) {
  1124. if (urb->setup->wLength) {
  1125. if (urb->setup->bmRequestType & 0x80) {
  1126. chan->ep0_state = DWC2_EP0_STATE_INDATA;
  1127. } else {
  1128. chan->ep0_state = DWC2_EP0_STATE_OUTDATA;
  1129. }
  1130. } else {
  1131. chan->ep0_state = DWC2_EP0_STATE_INSTATUS;
  1132. }
  1133. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  1134. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTDATA) {
  1135. if (chan->do_ssplit && urb->transfer_buffer_length > 0) {
  1136. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1137. } else {
  1138. chan->ep0_state = DWC2_EP0_STATE_INSTATUS;
  1139. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  1140. }
  1141. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTSTATUS) {
  1142. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->actual_length - 8, CONFIG_USB_ALIGN_SIZE));
  1143. chan->ep0_state = DWC2_EP0_STATE_SETUP;
  1144. urb->errorcode = 0;
  1145. dwc2_urb_waitup(urb);
  1146. }
  1147. } else if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  1148. } else {
  1149. if (chan->do_ssplit && urb->transfer_buffer_length > 0) {
  1150. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1151. } else {
  1152. urb->errorcode = 0;
  1153. dwc2_urb_waitup(urb);
  1154. }
  1155. }
  1156. } else if (chan_intstatus & USB_OTG_HCINT_AHBERR) {
  1157. urb->errorcode = -USB_ERR_IO;
  1158. dwc2_urb_waitup(urb);
  1159. } else if (chan_intstatus & USB_OTG_HCINT_STALL) {
  1160. urb->errorcode = -USB_ERR_STALL;
  1161. dwc2_urb_waitup(urb);
  1162. } else if (chan_intstatus & USB_OTG_HCINT_NAK) {
  1163. if (chan->do_ssplit) {
  1164. /* restart ssplit transfer */
  1165. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1166. case USB_ENDPOINT_TYPE_CONTROL:
  1167. chan->do_csplit = 0;
  1168. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1169. break;
  1170. case USB_ENDPOINT_TYPE_BULK:
  1171. chan->do_csplit = 0;
  1172. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1173. break;
  1174. case USB_ENDPOINT_TYPE_INTERRUPT:
  1175. chan->do_csplit = 0;
  1176. dwc2_chan_enable_csplit(bus, ch_num, false);
  1177. urb->errorcode = -USB_ERR_NAK;
  1178. dwc2_urb_waitup(urb);
  1179. break;
  1180. default:
  1181. break;
  1182. }
  1183. } else {
  1184. urb->errorcode = -USB_ERR_NAK;
  1185. dwc2_urb_waitup(urb);
  1186. }
  1187. } else if (chan_intstatus & USB_OTG_HCINT_ACK) {
  1188. if (chan->do_ssplit) {
  1189. /* start csplit transfer */
  1190. chan->do_csplit = 1;
  1191. chan->ssplit_frame = dwc2_get_full_frame_num(bus);
  1192. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1193. case USB_ENDPOINT_TYPE_CONTROL:
  1194. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1195. break;
  1196. case USB_ENDPOINT_TYPE_BULK:
  1197. case USB_ENDPOINT_TYPE_INTERRUPT:
  1198. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1199. break;
  1200. default:
  1201. break;
  1202. }
  1203. }
  1204. } else if (chan_intstatus & USB_OTG_HCINT_NYET) {
  1205. if (chan->do_ssplit) {
  1206. /* restart csplit transfer */
  1207. chan->do_csplit = 1;
  1208. chan->ssplit_frame = dwc2_get_full_frame_num(bus);
  1209. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1210. case USB_ENDPOINT_TYPE_CONTROL:
  1211. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1212. break;
  1213. case USB_ENDPOINT_TYPE_BULK:
  1214. case USB_ENDPOINT_TYPE_INTERRUPT:
  1215. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1216. break;
  1217. default:
  1218. break;
  1219. }
  1220. } else {
  1221. urb->errorcode = -USB_ERR_NAK;
  1222. dwc2_urb_waitup(urb);
  1223. }
  1224. } else if (chan_intstatus & USB_OTG_HCINT_TXERR) {
  1225. urb->errorcode = -USB_ERR_IO;
  1226. dwc2_urb_waitup(urb);
  1227. } else if (chan_intstatus & USB_OTG_HCINT_BBERR) {
  1228. urb->errorcode = -USB_ERR_BABBLE;
  1229. dwc2_urb_waitup(urb);
  1230. } else if (chan_intstatus & USB_OTG_HCINT_DTERR) {
  1231. urb->errorcode = -USB_ERR_DT;
  1232. dwc2_urb_waitup(urb);
  1233. } else if (chan_intstatus & USB_OTG_HCINT_FRMOR) {
  1234. urb->errorcode = -USB_ERR_IO;
  1235. dwc2_urb_waitup(urb);
  1236. }
  1237. }
  1238. }
  1239. static void dwc2_port_irq_handler(struct usbh_bus *bus)
  1240. {
  1241. __IO uint32_t hprt0, hprt0_dup, regval;
  1242. /* Handle Host Port Interrupts */
  1243. hprt0 = USB_OTG_HPRT;
  1244. hprt0_dup = USB_OTG_HPRT;
  1245. hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  1246. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  1247. /* Check whether Port Connect detected */
  1248. if ((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET) {
  1249. if ((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS) {
  1250. bus->hcd.roothub.int_buffer[0] = (1 << 1);
  1251. usbh_hub_thread_wakeup(&bus->hcd.roothub);
  1252. }
  1253. hprt0_dup |= USB_OTG_HPRT_PCDET;
  1254. g_dwc2_hcd[bus->hcd.hcd_id].port_csc = 1;
  1255. }
  1256. /* Check whether Port Enable Changed */
  1257. if ((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG) {
  1258. hprt0_dup |= USB_OTG_HPRT_PENCHNG;
  1259. g_dwc2_hcd[bus->hcd.hcd_id].port_pec = 1;
  1260. if ((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA) {
  1261. regval = USB_OTG_HOST->HFIR;
  1262. regval &= ~USB_OTG_HFIR_FRIVL;
  1263. regval |= dwc2_calc_frame_interval(bus) & USB_OTG_HFIR_FRIVL;
  1264. USB_OTG_HOST->HFIR = regval;
  1265. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  1266. if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17)) {
  1267. if ((USB_OTG_HOST->HCFG & USB_OTG_HCFG_FSLSPCS) != USB_OTG_HCFG_FSLSPCLKSEL_6_MHZ) {
  1268. regval = USB_OTG_HOST->HCFG;
  1269. regval &= ~USB_OTG_HCFG_FSLSPCS;
  1270. regval |= USB_OTG_HCFG_FSLSPCLKSEL_6_MHZ;
  1271. USB_OTG_HOST->HCFG = regval;
  1272. }
  1273. } else {
  1274. if ((USB_OTG_HOST->HCFG & USB_OTG_HCFG_FSLSPCS) != USB_OTG_HCFG_FSLSPCLKSEL_48_MHZ) {
  1275. regval = USB_OTG_HOST->HCFG;
  1276. regval &= ~USB_OTG_HCFG_FSLSPCS;
  1277. regval |= USB_OTG_HCFG_FSLSPCLKSEL_48_MHZ;
  1278. USB_OTG_HOST->HCFG = regval;
  1279. }
  1280. }
  1281. }
  1282. } else {
  1283. }
  1284. }
  1285. /* Check for an overcurrent */
  1286. if ((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG) {
  1287. hprt0_dup |= USB_OTG_HPRT_POCCHNG;
  1288. g_dwc2_hcd[bus->hcd.hcd_id].port_occ = 1;
  1289. }
  1290. /* Clear Port Interrupts */
  1291. USB_OTG_HPRT = hprt0_dup;
  1292. }
  1293. void USBH_IRQHandler(uint8_t busid)
  1294. {
  1295. uint32_t gint_status, chan_int;
  1296. struct usbh_bus *bus;
  1297. bus = &g_usbhost_bus[busid];
  1298. gint_status = dwc2_get_glb_intstatus(bus);
  1299. if ((USB_OTG_GLB->GINTSTS & 0x1U) == USB_OTG_MODE_HOST) {
  1300. /* Avoid spurious interrupt */
  1301. if (gint_status == 0) {
  1302. return;
  1303. }
  1304. if (gint_status & USB_OTG_GINTSTS_HPRTINT) {
  1305. dwc2_port_irq_handler(bus);
  1306. }
  1307. if (gint_status & USB_OTG_GINTSTS_DISCINT) {
  1308. g_dwc2_hcd[bus->hcd.hcd_id].port_csc = 1;
  1309. bus->hcd.roothub.int_buffer[0] = (1 << 1);
  1310. usbh_hub_thread_wakeup(&bus->hcd.roothub);
  1311. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_DISCINT;
  1312. }
  1313. if (gint_status & USB_OTG_GINTSTS_HCINT) {
  1314. chan_int = (USB_OTG_HOST->HAINT & USB_OTG_HOST->HAINTMSK) & 0xFFFFU;
  1315. for (uint8_t i = 0U; i < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; i++) {
  1316. if ((chan_int & (1UL << (i & 0xFU))) != 0U) {
  1317. if ((USB_OTG_HC(i)->HCCHAR & USB_OTG_HCCHAR_EPDIR) == USB_OTG_HCCHAR_EPDIR) {
  1318. dwc2_inchan_irq_handler(bus, i);
  1319. } else {
  1320. dwc2_outchan_irq_handler(bus, i);
  1321. }
  1322. }
  1323. }
  1324. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_HCINT;
  1325. }
  1326. }
  1327. }