usb_hc_dwc2.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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 inline void dwc2_chan_reenable(struct usbh_bus *bus, uint8_t ch_num)
  283. {
  284. __IO uint32_t tmpreg;
  285. uint8_t is_oddframe;
  286. is_oddframe = (((uint32_t)USB_OTG_HOST->HFNUM & 0x01U) != 0U) ? 0U : 1U;
  287. USB_OTG_HC(ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_ODDFRM;
  288. USB_OTG_HC(ch_num)->HCCHAR |= (uint32_t)is_oddframe << 29;
  289. /* Set host channel enable */
  290. tmpreg = USB_OTG_HC(ch_num)->HCCHAR;
  291. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  292. tmpreg |= USB_OTG_HCCHAR_CHENA;
  293. USB_OTG_HC(ch_num)->HCCHAR = tmpreg;
  294. }
  295. static void dwc2_halt(struct usbh_bus *bus, uint8_t ch_num)
  296. {
  297. volatile uint32_t ChannelEna = (USB_OTG_HC(ch_num)->HCCHAR & USB_OTG_HCCHAR_CHENA) >> 31;
  298. volatile uint32_t count = 0U;
  299. volatile uint32_t value;
  300. if (((USB_OTG_GLB->GAHBCFG & USB_OTG_GAHBCFG_DMAEN) == USB_OTG_GAHBCFG_DMAEN) &&
  301. (ChannelEna == 0U)) {
  302. return;
  303. }
  304. USB_OTG_HC(ch_num)->HCINTMSK = 0;
  305. value = USB_OTG_HC(ch_num)->HCCHAR;
  306. value |= USB_OTG_HCCHAR_CHDIS;
  307. value |= USB_OTG_HCCHAR_CHENA;
  308. USB_OTG_HC(ch_num)->HCCHAR = value;
  309. do {
  310. if (++count > 200000U) {
  311. break;
  312. }
  313. } while (USB_OTG_HC(ch_num)->HCCHAR & USB_OTG_HCCHAR_CHENA);
  314. USB_OTG_HC(ch_num)->HCINT = USB_OTG_HC(ch_num)->HCINT;
  315. }
  316. static int usbh_reset_port(struct usbh_bus *bus, const uint8_t port)
  317. {
  318. __IO uint32_t hprt0 = 0U;
  319. volatile uint32_t timeout = 0;
  320. hprt0 = USB_OTG_HPRT;
  321. hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  322. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  323. USB_OTG_HPRT = (USB_OTG_HPRT_PRST | hprt0);
  324. usb_osal_msleep(100U);
  325. USB_OTG_HPRT = ((~USB_OTG_HPRT_PRST) & hprt0);
  326. usb_osal_msleep(10U);
  327. while (!(USB_OTG_HPRT & USB_OTG_HPRT_PENA)) {
  328. if (!(USB_OTG_HPRT & USB_OTG_HPRT_PCSTS)) {
  329. return -USB_ERR_NOTCONN; /* Port not connected */
  330. }
  331. timeout++;
  332. if (timeout > 10) {
  333. USB_LOG_ERR("Reset port timeout\r\n");
  334. return -USB_ERR_TIMEOUT;
  335. }
  336. usb_osal_msleep(10U);
  337. }
  338. return 0;
  339. }
  340. /**
  341. * @brief dwc2_get_glb_intstatus: return the global USB interrupt status
  342. * @retval status
  343. */
  344. static inline uint32_t dwc2_get_glb_intstatus(struct usbh_bus *bus)
  345. {
  346. uint32_t tmpreg;
  347. tmpreg = USB_OTG_GLB->GINTSTS;
  348. tmpreg &= USB_OTG_GLB->GINTMSK;
  349. return tmpreg;
  350. }
  351. static inline uint16_t dwc2_get_full_frame_num(struct usbh_bus *bus)
  352. {
  353. uint16_t frame = usbh_get_frame_number(bus);
  354. /* USB_OTG_HFNUM_FRNUM_Msk is 0xFFFF but max frame num is 0x3FFF */
  355. return ((frame & 0x3FFF) >> 3);
  356. }
  357. /**
  358. * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
  359. * the HFIR register according to PHY type and speed
  360. *
  361. * NOTE: The caller can modify the value of the HFIR register only after the
  362. * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
  363. * has been set
  364. */
  365. uint32_t dwc2_calc_frame_interval(struct usbh_bus *bus)
  366. {
  367. uint32_t usbcfg;
  368. uint32_t hprt0;
  369. int clock = 60; /* default value */
  370. usbcfg = USB_OTG_GLB->GUSBCFG;
  371. hprt0 = USB_OTG_HPRT;
  372. if (!(usbcfg & USB_OTG_GUSBCFG_PHYSEL) && (usbcfg & USB_OTG_GUSBCFG_ULPI_UTMI_SEL) &&
  373. !(usbcfg & USB_OTG_GUSBCFG_PHYIF16))
  374. clock = 60;
  375. if ((usbcfg & USB_OTG_GUSBCFG_PHYSEL) && g_dwc2_hcd[bus->hcd.hcd_id].hw_params.fs_phy_type ==
  376. GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
  377. clock = 48;
  378. if (!(usbcfg & USB_OTG_GUSBCFG_PHYLPCS) && !(usbcfg & USB_OTG_GUSBCFG_PHYSEL) &&
  379. !(usbcfg & USB_OTG_GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & USB_OTG_GUSBCFG_PHYIF16))
  380. clock = 30;
  381. if (!(usbcfg & USB_OTG_GUSBCFG_PHYLPCS) && !(usbcfg & USB_OTG_GUSBCFG_PHYSEL) &&
  382. !(usbcfg & USB_OTG_GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & USB_OTG_GUSBCFG_PHYIF16))
  383. clock = 60;
  384. if ((usbcfg & USB_OTG_GUSBCFG_PHYLPCS) && !(usbcfg & USB_OTG_GUSBCFG_PHYSEL) &&
  385. !(usbcfg & USB_OTG_GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & USB_OTG_GUSBCFG_PHYIF16))
  386. clock = 48;
  387. if ((usbcfg & USB_OTG_GUSBCFG_PHYSEL) && !(usbcfg & USB_OTG_GUSBCFG_PHYIF16) &&
  388. g_dwc2_hcd[bus->hcd.hcd_id].hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
  389. clock = 48;
  390. if ((usbcfg & USB_OTG_GUSBCFG_PHYSEL) &&
  391. g_dwc2_hcd[bus->hcd.hcd_id].hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
  392. clock = 48;
  393. if ((hprt0 & USB_OTG_HPRT_PSPD) >> USB_OTG_HPRT_PSPD_Pos == HPRT0_PRTSPD_HIGH_SPEED)
  394. /* High speed case */
  395. return 125 * clock - 1;
  396. /* FS/LS case */
  397. return 1000 * clock - 1;
  398. }
  399. static int dwc2_chan_alloc(struct usbh_bus *bus)
  400. {
  401. size_t flags;
  402. int chidx;
  403. flags = usb_osal_enter_critical_section();
  404. for (chidx = 0; chidx < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; chidx++) {
  405. if (!g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].inuse) {
  406. g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].inuse = true;
  407. usb_osal_leave_critical_section(flags);
  408. g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].do_ssplit = 0;
  409. g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].do_csplit = 0;
  410. return chidx;
  411. }
  412. }
  413. usb_osal_leave_critical_section(flags);
  414. return -1;
  415. }
  416. static void dwc2_chan_free(struct dwc2_chan *chan)
  417. {
  418. size_t flags;
  419. flags = usb_osal_enter_critical_section();
  420. if (chan->urb) {
  421. chan->urb->hcpriv = NULL;
  422. chan->urb = NULL;
  423. }
  424. chan->inuse = false;
  425. usb_osal_leave_critical_section(flags);
  426. }
  427. static uint16_t dwc2_calculate_packet_num(uint32_t input_size, uint8_t ep_addr, uint16_t ep_mps, uint32_t *output_size)
  428. {
  429. uint16_t num_packets;
  430. num_packets = (uint16_t)((input_size + ep_mps - 1U) / ep_mps);
  431. if (num_packets > 0x3FF) {
  432. num_packets = 0x3FF; // pktcnt 10bits
  433. }
  434. if (input_size == 0) {
  435. num_packets = 1;
  436. }
  437. if (ep_addr & 0x80) {
  438. input_size = num_packets * ep_mps;
  439. } else {
  440. }
  441. *output_size = input_size;
  442. return num_packets;
  443. }
  444. 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)
  445. {
  446. struct dwc2_chan *chan;
  447. uint32_t datalen;
  448. uint8_t data_pid;
  449. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
  450. /* split buflen with ep mps */
  451. if (chan->do_ssplit && (chan->ep0_state == DWC2_EP0_STATE_INDATA || chan->ep0_state == DWC2_EP0_STATE_OUTDATA)) {
  452. if (buflen > USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize)) {
  453. datalen = USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize);
  454. } else {
  455. datalen = buflen;
  456. }
  457. if (urb->data_toggle == 0) {
  458. data_pid = HC_PID_DATA0;
  459. } else {
  460. data_pid = HC_PID_DATA1;
  461. }
  462. } else {
  463. datalen = buflen; // buflen = setup->wLength
  464. data_pid = HC_PID_DATA1;
  465. }
  466. if (chan->ep0_state == DWC2_EP0_STATE_SETUP) /* fill setup */
  467. {
  468. chan->num_packets = dwc2_calculate_packet_num(8, 0x00, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  469. dwc2_chan_init(bus,
  470. chidx,
  471. urb->hport->dev_addr,
  472. 0x00,
  473. USB_ENDPOINT_TYPE_CONTROL,
  474. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  475. 1,
  476. urb->hport->speed);
  477. dwc2_chan_transfer(bus, chidx, 0x00, (uint8_t *)setup, chan->xferlen, chan->num_packets, HC_PID_SETUP);
  478. } else if (chan->ep0_state == DWC2_EP0_STATE_INDATA) /* fill in data */
  479. {
  480. chan->num_packets = dwc2_calculate_packet_num(datalen, 0x80, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  481. dwc2_chan_init(bus,
  482. chidx,
  483. urb->hport->dev_addr,
  484. 0x80,
  485. USB_ENDPOINT_TYPE_CONTROL,
  486. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  487. 1,
  488. urb->hport->speed);
  489. dwc2_chan_transfer(bus, chidx, 0x80, buffer, chan->xferlen, chan->num_packets, data_pid);
  490. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTDATA) /* fill out data */
  491. {
  492. chan->num_packets = dwc2_calculate_packet_num(datalen, 0x00, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  493. dwc2_chan_init(bus,
  494. chidx,
  495. urb->hport->dev_addr,
  496. 0x00,
  497. USB_ENDPOINT_TYPE_CONTROL,
  498. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  499. 1,
  500. urb->hport->speed);
  501. dwc2_chan_transfer(bus, chidx, 0x00, buffer, chan->xferlen, chan->num_packets, data_pid);
  502. } else if (chan->ep0_state == DWC2_EP0_STATE_INSTATUS) /* fill in status */
  503. {
  504. chan->num_packets = dwc2_calculate_packet_num(0, 0x80, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  505. dwc2_chan_init(bus,
  506. chidx,
  507. urb->hport->dev_addr,
  508. 0x80,
  509. USB_ENDPOINT_TYPE_CONTROL,
  510. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  511. 1,
  512. urb->hport->speed);
  513. dwc2_chan_transfer(bus, chidx, 0x80, NULL, chan->xferlen, chan->num_packets, HC_PID_DATA1);
  514. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTSTATUS) /* fill out status */
  515. {
  516. chan->num_packets = dwc2_calculate_packet_num(0, 0x00, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  517. dwc2_chan_init(bus,
  518. chidx,
  519. urb->hport->dev_addr,
  520. 0x00,
  521. USB_ENDPOINT_TYPE_CONTROL,
  522. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  523. 1,
  524. urb->hport->speed);
  525. dwc2_chan_transfer(bus, chidx, 0x00, NULL, chan->xferlen, chan->num_packets, HC_PID_DATA1);
  526. }
  527. }
  528. static void dwc2_bulk_intr_urb_init(struct usbh_bus *bus, uint8_t chidx, struct usbh_urb *urb, uint8_t *buffer, uint32_t buflen)
  529. {
  530. struct dwc2_chan *chan;
  531. uint32_t datalen;
  532. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
  533. if (chan->do_ssplit) {
  534. if (buflen > USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize)) {
  535. datalen = USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize);
  536. } else {
  537. datalen = buflen;
  538. }
  539. } else {
  540. datalen = buflen;
  541. }
  542. chan->num_packets = dwc2_calculate_packet_num(datalen, urb->ep->bEndpointAddress, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  543. dwc2_chan_init(bus,
  544. chidx,
  545. urb->hport->dev_addr,
  546. urb->ep->bEndpointAddress,
  547. USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes),
  548. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize),
  549. USB_GET_MULT(urb->ep->wMaxPacketSize) + 1,
  550. urb->hport->speed);
  551. dwc2_chan_transfer(bus, chidx, urb->ep->bEndpointAddress, buffer, chan->xferlen, chan->num_packets, urb->data_toggle == 0 ? HC_PID_DATA0 : HC_PID_DATA1);
  552. }
  553. #if 0
  554. static void dwc2_iso_urb_init(struct usbh_bus *bus, uint8_t chidx, struct usbh_urb *urb, struct usbh_iso_frame_packet *iso_packet)
  555. {
  556. struct dwc2_chan *chan;
  557. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
  558. chan->num_packets = dwc2_calculate_packet_num(iso_packet->transfer_buffer_length, urb->ep->bEndpointAddress, USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize), &chan->xferlen);
  559. 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);
  560. dwc2_chan_transfer(bus, chidx, urb->ep->bEndpointAddress, iso_packet->transfer_buffer, chan->xferlen, chan->num_packets, HC_PID_DATA0);
  561. }
  562. #endif
  563. __WEAK void usb_hc_low_level_init(struct usbh_bus *bus)
  564. {
  565. (void)bus;
  566. }
  567. __WEAK void usb_hc_low_level_deinit(struct usbh_bus *bus)
  568. {
  569. (void)bus;
  570. }
  571. int usb_hc_init(struct usbh_bus *bus)
  572. {
  573. int ret;
  574. memset(&g_dwc2_hcd[bus->hcd.hcd_id], 0, sizeof(struct dwc2_hcd));
  575. usb_hc_low_level_init(bus);
  576. USB_LOG_INFO("========== dwc2 hcd params ==========\r\n");
  577. USB_LOG_INFO("CID:%08x\r\n", (unsigned int)USB_OTG_GLB->CID);
  578. USB_LOG_INFO("GSNPSID:%08x\r\n", (unsigned int)USB_OTG_GLB->GSNPSID);
  579. USB_LOG_INFO("GHWCFG1:%08x\r\n", (unsigned int)USB_OTG_GLB->GHWCFG1);
  580. USB_LOG_INFO("GHWCFG2:%08x\r\n", (unsigned int)USB_OTG_GLB->GHWCFG2);
  581. USB_LOG_INFO("GHWCFG3:%08x\r\n", (unsigned int)USB_OTG_GLB->GHWCFG3);
  582. USB_LOG_INFO("GHWCFG4:%08x\r\n", (unsigned int)USB_OTG_GLB->GHWCFG4);
  583. dwc2_get_hwparams(bus->hcd.reg_base, &g_dwc2_hcd[bus->hcd.hcd_id].hw_params);
  584. dwc2_get_user_params(bus->hcd.reg_base, &g_dwc2_hcd[bus->hcd.hcd_id].user_params);
  585. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_utmi_width == 0) {
  586. g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_utmi_width = 8;
  587. }
  588. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.total_fifo_size == 0) {
  589. g_dwc2_hcd[bus->hcd.hcd_id].user_params.total_fifo_size = g_dwc2_hcd[bus->hcd.hcd_id].hw_params.total_fifo_size;
  590. }
  591. for (uint8_t chidx = 0; chidx < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; chidx++) {
  592. g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].waitsem = usb_osal_sem_create(0);
  593. }
  594. USB_LOG_INFO("dwc2 has %d channels and dfifo depth(32-bit words) is %d\r\n",
  595. g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels,
  596. g_dwc2_hcd[bus->hcd.hcd_id].user_params.total_fifo_size);
  597. 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");
  598. USB_ASSERT_MSG((g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size +
  599. g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_nperio_tx_fifo_size +
  600. g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_perio_tx_fifo_size) <=
  601. g_dwc2_hcd[bus->hcd.hcd_id].user_params.total_fifo_size,
  602. "Your fifo config is overflow, please check");
  603. USB_OTG_GLB->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
  604. /* This is vendor register */
  605. USB_OTG_GLB->GCCFG = g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_gccfg;
  606. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_type != DWC2_PHY_TYPE_PARAM_FS) {
  607. 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");
  608. }
  609. ret = dwc2_core_init(bus);
  610. /* Force Host Mode*/
  611. dwc2_set_mode(bus, USB_OTG_MODE_HOST);
  612. /* B-peripheral session valid override enable */
  613. USB_OTG_GLB->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOEN;
  614. USB_OTG_GLB->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOVAL;
  615. USB_OTG_GLB->GUSBCFG |= USB_OTG_GUSBCFG_TOCAL;
  616. /* Restart the Phy Clock */
  617. USB_OTG_PCGCCTL = 0U;
  618. /* Set default Max speed support */
  619. USB_OTG_HOST->HCFG &= ~USB_OTG_HCFG_FSLSS;
  620. USB_OTG_HOST->HCFG &= ~USB_OTG_HCFG_FSLSPCS;
  621. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  622. USB_OTG_HOST->HCFG |= USB_OTG_HCFG_FSLSPCLKSEL_48_MHZ;
  623. } else {
  624. USB_OTG_HOST->HCFG |= USB_OTG_HCFG_FSLSPCLKSEL_30_60_MHZ;
  625. }
  626. if (g_dwc2_hcd[bus->hcd.hcd_id].hw_params.snpsid > 0x4F54292AU) {
  627. USB_OTG_HOST->HCFG |= USB_OTG_HFIR_RELOAD_CTRL;
  628. }
  629. /* Clear all pending HC Interrupts */
  630. for (uint8_t i = 0U; i < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; i++) {
  631. USB_OTG_HC(i)->HCINT = 0xFFFFFFFFU;
  632. USB_OTG_HC(i)->HCINTMSK = 0U;
  633. }
  634. /* Disable all interrupts. */
  635. USB_OTG_GLB->GINTMSK = 0U;
  636. /* Clear any pending interrupts */
  637. USB_OTG_GLB->GINTSTS = 0xFFFFFFFFU;
  638. /* set Rx FIFO size */
  639. USB_OTG_GLB->GRXFSIZ = g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size;
  640. 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) |
  641. g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size);
  642. 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) |
  643. (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));
  644. ret = dwc2_flush_txfifo(bus, 0x10U);
  645. ret = dwc2_flush_rxfifo(bus);
  646. USB_OTG_GLB->GAHBCFG &= ~USB_OTG_GAHBCFG_HBSTLEN;
  647. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_HBSTLEN_4;
  648. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_DMAEN;
  649. /* Enable interrupts matching to the Host mode ONLY */
  650. USB_OTG_GLB->GINTMSK |= (USB_OTG_GINTMSK_PRTIM | USB_OTG_GINTMSK_HCIM |
  651. USB_OTG_GINTSTS_DISCINT);
  652. dwc2_drivebus(bus, 1);
  653. usb_osal_msleep(200);
  654. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
  655. return ret;
  656. }
  657. int usb_hc_deinit(struct usbh_bus *bus)
  658. {
  659. volatile uint32_t count = 0U;
  660. uint32_t value;
  661. USB_OTG_GLB->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
  662. dwc2_flush_txfifo(bus, 0x10U);
  663. dwc2_flush_rxfifo(bus);
  664. /* Flush out any leftover queued requests. */
  665. for (uint32_t i = 0U; i < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; i++) {
  666. value = USB_OTG_HC(i)->HCCHAR;
  667. value |= USB_OTG_HCCHAR_CHDIS;
  668. value &= ~USB_OTG_HCCHAR_CHENA;
  669. value &= ~USB_OTG_HCCHAR_EPDIR;
  670. USB_OTG_HC(i)->HCCHAR = value;
  671. }
  672. /* Halt all channels to put them into a known state. */
  673. for (uint32_t i = 0U; i < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; i++) {
  674. value = USB_OTG_HC(i)->HCCHAR;
  675. value |= USB_OTG_HCCHAR_CHDIS;
  676. value |= USB_OTG_HCCHAR_CHENA;
  677. value &= ~USB_OTG_HCCHAR_EPDIR;
  678. USB_OTG_HC(i)->HCCHAR = value;
  679. do {
  680. if (++count > 1000U) {
  681. return -USB_ERR_TIMEOUT;
  682. }
  683. } while ((USB_OTG_HC(i)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  684. }
  685. /* Disable all interrupts. */
  686. USB_OTG_GLB->GINTMSK = 0U;
  687. /* Clear any pending Host interrupts */
  688. USB_OTG_HOST->HAINT = 0xFFFFFFFFU;
  689. USB_OTG_GLB->GINTSTS = 0xFFFFFFFFU;
  690. dwc2_drivebus(bus, 0);
  691. usb_osal_msleep(200);
  692. for (uint8_t chidx = 0; chidx < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; chidx++) {
  693. usb_osal_sem_delete(g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].waitsem);
  694. }
  695. usb_hc_low_level_deinit(bus);
  696. return 0;
  697. }
  698. uint16_t usbh_get_frame_number(struct usbh_bus *bus)
  699. {
  700. return (USB_OTG_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
  701. }
  702. int usbh_roothub_control(struct usbh_bus *bus, struct usb_setup_packet *setup, uint8_t *buf)
  703. {
  704. __IO uint32_t hprt0;
  705. uint8_t nports;
  706. uint8_t port;
  707. uint32_t status;
  708. nports = CONFIG_USBHOST_MAX_RHPORTS;
  709. port = setup->wIndex;
  710. if (setup->bmRequestType & USB_REQUEST_RECIPIENT_DEVICE) {
  711. switch (setup->bRequest) {
  712. case HUB_REQUEST_CLEAR_FEATURE:
  713. switch (setup->wValue) {
  714. case HUB_FEATURE_HUB_C_LOCALPOWER:
  715. break;
  716. case HUB_FEATURE_HUB_C_OVERCURRENT:
  717. break;
  718. default:
  719. return -USB_ERR_NOTSUPP;
  720. }
  721. break;
  722. case HUB_REQUEST_SET_FEATURE:
  723. switch (setup->wValue) {
  724. case HUB_FEATURE_HUB_C_LOCALPOWER:
  725. break;
  726. case HUB_FEATURE_HUB_C_OVERCURRENT:
  727. break;
  728. default:
  729. return -USB_ERR_NOTSUPP;
  730. }
  731. break;
  732. case HUB_REQUEST_GET_DESCRIPTOR:
  733. break;
  734. case HUB_REQUEST_GET_STATUS:
  735. memset(buf, 0, 4);
  736. break;
  737. default:
  738. break;
  739. }
  740. } else if (setup->bmRequestType & USB_REQUEST_RECIPIENT_OTHER) {
  741. switch (setup->bRequest) {
  742. case HUB_REQUEST_CLEAR_FEATURE:
  743. if (!port || port > nports) {
  744. return -USB_ERR_INVAL;
  745. }
  746. switch (setup->wValue) {
  747. case HUB_PORT_FEATURE_ENABLE:
  748. USB_OTG_HPRT &= ~USB_OTG_HPRT_PENA;
  749. break;
  750. case HUB_PORT_FEATURE_SUSPEND:
  751. case HUB_PORT_FEATURE_C_SUSPEND:
  752. break;
  753. case HUB_PORT_FEATURE_POWER:
  754. dwc2_drivebus(bus, 0);
  755. break;
  756. case HUB_PORT_FEATURE_C_CONNECTION:
  757. g_dwc2_hcd[bus->hcd.hcd_id].port_csc = 0;
  758. break;
  759. case HUB_PORT_FEATURE_C_ENABLE:
  760. g_dwc2_hcd[bus->hcd.hcd_id].port_pec = 0;
  761. break;
  762. case HUB_PORT_FEATURE_C_OVER_CURREN:
  763. g_dwc2_hcd[bus->hcd.hcd_id].port_occ = 0;
  764. break;
  765. case HUB_PORT_FEATURE_C_RESET:
  766. break;
  767. default:
  768. return -USB_ERR_NOTSUPP;
  769. }
  770. break;
  771. case HUB_REQUEST_SET_FEATURE:
  772. if (!port || port > nports) {
  773. return -USB_ERR_INVAL;
  774. }
  775. switch (setup->wValue) {
  776. case HUB_PORT_FEATURE_SUSPEND:
  777. break;
  778. case HUB_PORT_FEATURE_POWER:
  779. dwc2_drivebus(bus, 1);
  780. break;
  781. case HUB_PORT_FEATURE_RESET:
  782. return usbh_reset_port(bus, port);
  783. default:
  784. return -USB_ERR_NOTSUPP;
  785. }
  786. break;
  787. case HUB_REQUEST_GET_STATUS:
  788. if (!port || port > nports) {
  789. return -USB_ERR_INVAL;
  790. }
  791. hprt0 = USB_OTG_HPRT;
  792. status = 0;
  793. if (g_dwc2_hcd[bus->hcd.hcd_id].port_csc) {
  794. status |= (1 << HUB_PORT_FEATURE_C_CONNECTION);
  795. }
  796. if (g_dwc2_hcd[bus->hcd.hcd_id].port_pec) {
  797. status |= (1 << HUB_PORT_FEATURE_C_ENABLE);
  798. }
  799. if (g_dwc2_hcd[bus->hcd.hcd_id].port_occ) {
  800. status |= (1 << HUB_PORT_FEATURE_C_OVER_CURREN);
  801. }
  802. if (hprt0 & USB_OTG_HPRT_PCSTS) {
  803. status |= (1 << HUB_PORT_FEATURE_CONNECTION);
  804. }
  805. if (hprt0 & USB_OTG_HPRT_PENA) {
  806. status |= (1 << HUB_PORT_FEATURE_ENABLE);
  807. if (usbh_get_port_speed(bus, port) == USB_SPEED_LOW) {
  808. status |= (1 << HUB_PORT_FEATURE_LOWSPEED);
  809. } else if (usbh_get_port_speed(bus, port) == USB_SPEED_HIGH) {
  810. status |= (1 << HUB_PORT_FEATURE_HIGHSPEED);
  811. }
  812. }
  813. if (hprt0 & USB_OTG_HPRT_POCA) {
  814. status |= (1 << HUB_PORT_FEATURE_OVERCURRENT);
  815. }
  816. if (hprt0 & USB_OTG_HPRT_PRST) {
  817. status |= (1 << HUB_PORT_FEATURE_RESET);
  818. }
  819. if (hprt0 & USB_OTG_HPRT_PPWR) {
  820. status |= (1 << HUB_PORT_FEATURE_POWER);
  821. }
  822. memcpy(buf, &status, 4);
  823. break;
  824. default:
  825. break;
  826. }
  827. }
  828. return 0;
  829. }
  830. int usbh_submit_urb(struct usbh_urb *urb)
  831. {
  832. struct dwc2_chan *chan;
  833. struct usbh_bus *bus;
  834. size_t flags;
  835. int ret = 0;
  836. int chidx;
  837. if (!urb || !urb->hport || !urb->ep || !urb->hport->bus) {
  838. return -USB_ERR_INVAL;
  839. }
  840. /* dma addr must be aligned 4 bytes */
  841. USB_ASSERT_MSG(!((uintptr_t)urb->setup % 4) && !((uintptr_t)urb->transfer_buffer % 4),
  842. "urb->setup or urb->transfer_buffer is not aligned 4 bytes");
  843. #ifdef CONFIG_USB_DCACHE_ENABLE
  844. USB_ASSERT_MSG(!((uintptr_t)urb->setup % CONFIG_USB_ALIGN_SIZE) &&
  845. !((uintptr_t)urb->transfer_buffer % CONFIG_USB_ALIGN_SIZE),
  846. "urb->setup or urb->transfer_buffer is not aligned %d", CONFIG_USB_ALIGN_SIZE);
  847. #endif
  848. bus = urb->hport->bus;
  849. if (!(USB_OTG_HPRT & USB_OTG_HPRT_PCSTS) || !urb->hport->connected) {
  850. return -USB_ERR_NOTCONN;
  851. }
  852. if (urb->errorcode == -USB_ERR_BUSY) {
  853. return -USB_ERR_BUSY;
  854. }
  855. if (urb->ep->bEndpointAddress & 0x80) {
  856. /* Check if pipe rx fifo is overflow */
  857. if (USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) > (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size * 4)) {
  858. return -USB_ERR_RANGE;
  859. }
  860. } else {
  861. /* Check if intr and iso pipe tx fifo is overflow */
  862. if (((USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS) ||
  863. (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT)) &&
  864. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) > (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_perio_tx_fifo_size * 4)) {
  865. return -USB_ERR_RANGE;
  866. } else {
  867. /* Check if control and bulk pipe tx fifo is overflow */
  868. if (USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) > (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_nperio_tx_fifo_size * 4)) {
  869. return -USB_ERR_RANGE;
  870. }
  871. }
  872. }
  873. chidx = dwc2_chan_alloc(bus);
  874. if (chidx == -1) {
  875. return -USB_ERR_NOMEM;
  876. }
  877. flags = usb_osal_enter_critical_section();
  878. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
  879. chan->chidx = chidx;
  880. chan->urb = urb;
  881. chan->do_ssplit = 0;
  882. if (urb->hport->speed != USB_SPEED_HIGH &&
  883. usbh_get_port_speed(bus, 0) == USB_SPEED_HIGH) {
  884. chan->do_ssplit = 1;
  885. chan->do_csplit = 0;
  886. chan->hub_port = urb->hport->port;
  887. chan->hub_addr = urb->hport->parent->hub_addr;
  888. }
  889. urb->hcpriv = chan;
  890. urb->errorcode = -USB_ERR_BUSY;
  891. urb->actual_length = 0;
  892. usb_osal_leave_critical_section(flags);
  893. if (urb->setup) {
  894. usb_dcache_clean((uintptr_t)urb->setup, USB_ALIGN_UP(sizeof(struct usb_setup_packet), CONFIG_USB_ALIGN_SIZE));
  895. if (urb->transfer_buffer) {
  896. if (urb->setup->bmRequestType & 0x80) {
  897. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  898. } else {
  899. usb_dcache_clean((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  900. }
  901. }
  902. } else if (urb->transfer_buffer && (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) != USB_ENDPOINT_TYPE_ISOCHRONOUS)) {
  903. if (urb->ep->bEndpointAddress & 0x80) {
  904. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  905. } else {
  906. usb_dcache_clean((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  907. }
  908. } else {
  909. }
  910. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  911. case USB_ENDPOINT_TYPE_CONTROL:
  912. chan->ep0_state = DWC2_EP0_STATE_SETUP;
  913. dwc2_control_urb_init(bus, chidx, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  914. break;
  915. case USB_ENDPOINT_TYPE_BULK:
  916. case USB_ENDPOINT_TYPE_INTERRUPT:
  917. dwc2_bulk_intr_urb_init(bus, chidx, urb, urb->transfer_buffer, urb->transfer_buffer_length);
  918. break;
  919. case USB_ENDPOINT_TYPE_ISOCHRONOUS:
  920. break;
  921. default:
  922. break;
  923. }
  924. if (urb->timeout > 0) {
  925. /* wait until timeout or sem give */
  926. ret = usb_osal_sem_take(chan->waitsem, urb->timeout);
  927. if (ret < 0) {
  928. goto errout_timeout;
  929. }
  930. urb->timeout = 0;
  931. ret = urb->errorcode;
  932. /* we can free chan when waitsem is done */
  933. dwc2_chan_free(chan);
  934. }
  935. return ret;
  936. errout_timeout:
  937. urb->timeout = 0;
  938. usbh_kill_urb(urb);
  939. return ret;
  940. }
  941. int usbh_kill_urb(struct usbh_urb *urb)
  942. {
  943. struct dwc2_chan *chan;
  944. struct usbh_bus *bus;
  945. size_t flags;
  946. if (!urb || !urb->hcpriv || !urb->hport->bus) {
  947. return -USB_ERR_INVAL;
  948. }
  949. bus = urb->hport->bus;
  950. flags = usb_osal_enter_critical_section();
  951. chan = (struct dwc2_chan *)urb->hcpriv;
  952. dwc2_halt(bus, chan->chidx);
  953. urb->errorcode = -USB_ERR_SHUTDOWN;
  954. if (urb->timeout) {
  955. usb_osal_sem_give(chan->waitsem);
  956. } else {
  957. dwc2_chan_free(chan);
  958. }
  959. if (urb->complete) {
  960. urb->complete(urb->arg, urb->errorcode);
  961. }
  962. usb_osal_leave_critical_section(flags);
  963. return 0;
  964. }
  965. static inline void dwc2_urb_waitup(struct usbh_urb *urb)
  966. {
  967. struct dwc2_chan *chan;
  968. chan = (struct dwc2_chan *)urb->hcpriv;
  969. if (urb->timeout) {
  970. usb_osal_sem_give(chan->waitsem);
  971. } else {
  972. dwc2_chan_free(chan);
  973. }
  974. if (urb->complete) {
  975. if (urb->errorcode < 0) {
  976. urb->complete(urb->arg, urb->errorcode);
  977. } else {
  978. urb->complete(urb->arg, urb->actual_length);
  979. }
  980. }
  981. }
  982. static void dwc2_inchan_irq_handler(struct usbh_bus *bus, uint8_t ch_num)
  983. {
  984. uint32_t chan_intstatus;
  985. struct dwc2_chan *chan;
  986. struct usbh_urb *urb;
  987. chan_intstatus = USB_OTG_HC(ch_num)->HCINT;
  988. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[ch_num];
  989. urb = chan->urb;
  990. //printf("s1:%08x\r\n", chan_intstatus);
  991. if (chan_intstatus & USB_OTG_HCINT_CHH) {
  992. USB_OTG_HC(ch_num)->HCINT = chan_intstatus;
  993. if (chan_intstatus & USB_OTG_HCINT_XFRC) {
  994. uint32_t count = chan->xferlen - (USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ); /* how many size has received */
  995. uint8_t data_toggle = ((USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_DPID) >> USB_OTG_HCTSIZ_DPID_Pos);
  996. urb->actual_length += count;
  997. urb->transfer_buffer_length -= count;
  998. if (data_toggle == HC_PID_DATA0) {
  999. urb->data_toggle = 0;
  1000. } else {
  1001. urb->data_toggle = 1;
  1002. }
  1003. if (chan->do_csplit) {
  1004. chan->do_csplit = 0;
  1005. dwc2_chan_enable_csplit(bus, ch_num, false);
  1006. }
  1007. if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_CONTROL) {
  1008. if (chan->ep0_state == DWC2_EP0_STATE_INDATA) {
  1009. if (chan->do_ssplit && urb->transfer_buffer_length > 0 && (count == USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize))) {
  1010. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1011. } else {
  1012. chan->ep0_state = DWC2_EP0_STATE_OUTSTATUS;
  1013. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  1014. }
  1015. } else if (chan->ep0_state == DWC2_EP0_STATE_INSTATUS) {
  1016. chan->ep0_state = DWC2_EP0_STATE_SETUP;
  1017. urb->errorcode = 0;
  1018. dwc2_urb_waitup(urb);
  1019. }
  1020. } else if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  1021. } else {
  1022. if (chan->do_ssplit && urb->transfer_buffer_length > 0 && (count == USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize))) {
  1023. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1024. } else {
  1025. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->actual_length, CONFIG_USB_ALIGN_SIZE));
  1026. urb->errorcode = 0;
  1027. dwc2_urb_waitup(urb);
  1028. }
  1029. }
  1030. } else if (chan_intstatus & USB_OTG_HCINT_AHBERR) {
  1031. urb->errorcode = -USB_ERR_IO;
  1032. dwc2_urb_waitup(urb);
  1033. } else if (chan_intstatus & USB_OTG_HCINT_STALL) {
  1034. urb->errorcode = -USB_ERR_STALL;
  1035. dwc2_urb_waitup(urb);
  1036. } else if (chan_intstatus & USB_OTG_HCINT_NAK) {
  1037. if (chan->do_ssplit) {
  1038. /* restart ssplit transfer */
  1039. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1040. case USB_ENDPOINT_TYPE_CONTROL:
  1041. case USB_ENDPOINT_TYPE_BULK:
  1042. chan->do_csplit = 0;
  1043. dwc2_chan_enable_csplit(bus, ch_num, false);
  1044. dwc2_chan_reenable(bus, ch_num);
  1045. break;
  1046. case USB_ENDPOINT_TYPE_INTERRUPT:
  1047. dwc2_chan_enable_csplit(bus, ch_num, false);
  1048. urb->errorcode = -USB_ERR_NAK;
  1049. dwc2_urb_waitup(urb);
  1050. break;
  1051. default:
  1052. break;
  1053. }
  1054. } else {
  1055. urb->errorcode = -USB_ERR_NAK;
  1056. dwc2_urb_waitup(urb);
  1057. }
  1058. } else if (chan_intstatus & USB_OTG_HCINT_ACK) {
  1059. if (chan->do_ssplit) {
  1060. /* start ssplit transfer */
  1061. chan->do_csplit = 1;
  1062. chan->ssplit_frame = dwc2_get_full_frame_num(bus);
  1063. dwc2_chan_enable_csplit(bus, ch_num, true);
  1064. dwc2_chan_reenable(bus, ch_num);
  1065. }
  1066. } else if (chan_intstatus & USB_OTG_HCINT_NYET) {
  1067. if (chan->do_ssplit) {
  1068. /* restart csplit transfer */
  1069. dwc2_chan_enable_csplit(bus, ch_num, true);
  1070. dwc2_chan_reenable(bus, ch_num);
  1071. } else {
  1072. urb->errorcode = -USB_ERR_NAK;
  1073. dwc2_urb_waitup(urb);
  1074. }
  1075. } else if (chan_intstatus & USB_OTG_HCINT_TXERR) {
  1076. urb->errorcode = -USB_ERR_IO;
  1077. dwc2_urb_waitup(urb);
  1078. } else if (chan_intstatus & USB_OTG_HCINT_BBERR) {
  1079. urb->errorcode = -USB_ERR_BABBLE;
  1080. dwc2_urb_waitup(urb);
  1081. } else if (chan_intstatus & USB_OTG_HCINT_DTERR) {
  1082. urb->errorcode = -USB_ERR_DT;
  1083. dwc2_urb_waitup(urb);
  1084. } else if (chan_intstatus & USB_OTG_HCINT_FRMOR) {
  1085. urb->errorcode = -USB_ERR_IO;
  1086. dwc2_urb_waitup(urb);
  1087. }
  1088. }
  1089. }
  1090. static void dwc2_outchan_irq_handler(struct usbh_bus *bus, uint8_t ch_num)
  1091. {
  1092. uint32_t chan_intstatus;
  1093. struct dwc2_chan *chan;
  1094. struct usbh_urb *urb;
  1095. chan_intstatus = USB_OTG_HC(ch_num)->HCINT;
  1096. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[ch_num];
  1097. urb = chan->urb;
  1098. //printf("s2:%08x\r\n", chan_intstatus);
  1099. if (chan_intstatus & USB_OTG_HCINT_CHH) {
  1100. USB_OTG_HC(ch_num)->HCINT = chan_intstatus;
  1101. if (chan_intstatus & USB_OTG_HCINT_XFRC) {
  1102. uint32_t count = USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ; /* last packet size */
  1103. uint32_t has_used_packets = chan->num_packets - ((USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) >> 19); /* how many packets have used */
  1104. uint32_t olen = (has_used_packets - 1) * USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) + count; /* the same with urb->actual_length += chan->xferlen; */
  1105. uint8_t data_toggle = ((USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_DPID) >> USB_OTG_HCTSIZ_DPID_Pos);
  1106. urb->actual_length += olen;
  1107. if (chan->ep0_state == DWC2_EP0_STATE_OUTDATA || urb->setup == NULL) {
  1108. if (urb->transfer_buffer_length > olen) {
  1109. urb->transfer_buffer_length -= olen;
  1110. } else {
  1111. urb->transfer_buffer_length = 0;
  1112. }
  1113. }
  1114. if (data_toggle == HC_PID_DATA0) {
  1115. urb->data_toggle = 0;
  1116. } else {
  1117. urb->data_toggle = 1;
  1118. }
  1119. if (chan->do_csplit) {
  1120. chan->do_csplit = 0;
  1121. dwc2_chan_enable_csplit(bus, ch_num, false);
  1122. }
  1123. if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_CONTROL) {
  1124. if (chan->ep0_state == DWC2_EP0_STATE_SETUP) {
  1125. if (urb->setup->wLength) {
  1126. if (urb->setup->bmRequestType & 0x80) {
  1127. chan->ep0_state = DWC2_EP0_STATE_INDATA;
  1128. } else {
  1129. chan->ep0_state = DWC2_EP0_STATE_OUTDATA;
  1130. }
  1131. } else {
  1132. chan->ep0_state = DWC2_EP0_STATE_INSTATUS;
  1133. }
  1134. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  1135. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTDATA) {
  1136. if (chan->do_ssplit && urb->transfer_buffer_length > 0) {
  1137. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1138. } else {
  1139. chan->ep0_state = DWC2_EP0_STATE_INSTATUS;
  1140. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  1141. }
  1142. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTSTATUS) {
  1143. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->actual_length - 8, CONFIG_USB_ALIGN_SIZE));
  1144. chan->ep0_state = DWC2_EP0_STATE_SETUP;
  1145. urb->errorcode = 0;
  1146. dwc2_urb_waitup(urb);
  1147. }
  1148. } else if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  1149. } else {
  1150. if (chan->do_ssplit && urb->transfer_buffer_length > 0) {
  1151. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1152. } else {
  1153. urb->errorcode = 0;
  1154. dwc2_urb_waitup(urb);
  1155. }
  1156. }
  1157. } else if (chan_intstatus & USB_OTG_HCINT_AHBERR) {
  1158. urb->errorcode = -USB_ERR_IO;
  1159. dwc2_urb_waitup(urb);
  1160. } else if (chan_intstatus & USB_OTG_HCINT_STALL) {
  1161. urb->errorcode = -USB_ERR_STALL;
  1162. dwc2_urb_waitup(urb);
  1163. } else if (chan_intstatus & USB_OTG_HCINT_NAK) {
  1164. if (chan->do_ssplit) {
  1165. /* restart ssplit transfer */
  1166. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1167. case USB_ENDPOINT_TYPE_CONTROL:
  1168. case USB_ENDPOINT_TYPE_BULK:
  1169. chan->do_csplit = 0;
  1170. dwc2_chan_enable_csplit(bus, ch_num, false);
  1171. dwc2_chan_reenable(bus, ch_num);
  1172. break;
  1173. case USB_ENDPOINT_TYPE_INTERRUPT:
  1174. dwc2_chan_enable_csplit(bus, ch_num, false);
  1175. urb->errorcode = -USB_ERR_NAK;
  1176. dwc2_urb_waitup(urb);
  1177. break;
  1178. default:
  1179. break;
  1180. }
  1181. } else {
  1182. urb->errorcode = -USB_ERR_NAK;
  1183. dwc2_urb_waitup(urb);
  1184. }
  1185. } else if (chan_intstatus & USB_OTG_HCINT_ACK) {
  1186. if (chan->do_ssplit) {
  1187. /* start ssplit transfer */
  1188. chan->do_csplit = 1;
  1189. chan->ssplit_frame = dwc2_get_full_frame_num(bus);
  1190. dwc2_chan_enable_csplit(bus, ch_num, true);
  1191. dwc2_chan_reenable(bus, ch_num);
  1192. }
  1193. } else if (chan_intstatus & USB_OTG_HCINT_NYET) {
  1194. if (chan->do_ssplit) {
  1195. /* restart csplit transfer */
  1196. dwc2_chan_enable_csplit(bus, ch_num, true);
  1197. dwc2_chan_reenable(bus, ch_num);
  1198. } else {
  1199. urb->errorcode = -USB_ERR_NAK;
  1200. dwc2_urb_waitup(urb);
  1201. }
  1202. } else if (chan_intstatus & USB_OTG_HCINT_TXERR) {
  1203. urb->errorcode = -USB_ERR_IO;
  1204. dwc2_urb_waitup(urb);
  1205. } else if (chan_intstatus & USB_OTG_HCINT_BBERR) {
  1206. urb->errorcode = -USB_ERR_BABBLE;
  1207. dwc2_urb_waitup(urb);
  1208. } else if (chan_intstatus & USB_OTG_HCINT_DTERR) {
  1209. urb->errorcode = -USB_ERR_DT;
  1210. dwc2_urb_waitup(urb);
  1211. } else if (chan_intstatus & USB_OTG_HCINT_FRMOR) {
  1212. urb->errorcode = -USB_ERR_IO;
  1213. dwc2_urb_waitup(urb);
  1214. }
  1215. }
  1216. }
  1217. static void dwc2_port_irq_handler(struct usbh_bus *bus)
  1218. {
  1219. __IO uint32_t hprt0, hprt0_dup, regval;
  1220. /* Handle Host Port Interrupts */
  1221. hprt0 = USB_OTG_HPRT;
  1222. hprt0_dup = USB_OTG_HPRT;
  1223. hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  1224. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  1225. /* Check whether Port Connect detected */
  1226. if ((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET) {
  1227. if ((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS) {
  1228. bus->hcd.roothub.int_buffer[0] = (1 << 1);
  1229. usbh_hub_thread_wakeup(&bus->hcd.roothub);
  1230. }
  1231. hprt0_dup |= USB_OTG_HPRT_PCDET;
  1232. g_dwc2_hcd[bus->hcd.hcd_id].port_csc = 1;
  1233. }
  1234. /* Check whether Port Enable Changed */
  1235. if ((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG) {
  1236. hprt0_dup |= USB_OTG_HPRT_PENCHNG;
  1237. g_dwc2_hcd[bus->hcd.hcd_id].port_pec = 1;
  1238. if ((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA) {
  1239. regval = USB_OTG_HOST->HFIR;
  1240. regval &= ~USB_OTG_HFIR_FRIVL;
  1241. regval |= dwc2_calc_frame_interval(bus) & USB_OTG_HFIR_FRIVL;
  1242. USB_OTG_HOST->HFIR = regval;
  1243. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  1244. if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17)) {
  1245. if ((USB_OTG_HOST->HCFG & USB_OTG_HCFG_FSLSPCS) != USB_OTG_HCFG_FSLSPCLKSEL_6_MHZ) {
  1246. regval = USB_OTG_HOST->HCFG;
  1247. regval &= ~USB_OTG_HCFG_FSLSPCS;
  1248. regval |= USB_OTG_HCFG_FSLSPCLKSEL_6_MHZ;
  1249. USB_OTG_HOST->HCFG = regval;
  1250. }
  1251. } else {
  1252. if ((USB_OTG_HOST->HCFG & USB_OTG_HCFG_FSLSPCS) != USB_OTG_HCFG_FSLSPCLKSEL_48_MHZ) {
  1253. regval = USB_OTG_HOST->HCFG;
  1254. regval &= ~USB_OTG_HCFG_FSLSPCS;
  1255. regval |= USB_OTG_HCFG_FSLSPCLKSEL_48_MHZ;
  1256. USB_OTG_HOST->HCFG = regval;
  1257. }
  1258. }
  1259. }
  1260. } else {
  1261. }
  1262. }
  1263. /* Check for an overcurrent */
  1264. if ((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG) {
  1265. hprt0_dup |= USB_OTG_HPRT_POCCHNG;
  1266. g_dwc2_hcd[bus->hcd.hcd_id].port_occ = 1;
  1267. }
  1268. /* Clear Port Interrupts */
  1269. USB_OTG_HPRT = hprt0_dup;
  1270. }
  1271. void USBH_IRQHandler(uint8_t busid)
  1272. {
  1273. uint32_t gint_status, chan_int;
  1274. struct usbh_bus *bus;
  1275. bus = &g_usbhost_bus[busid];
  1276. gint_status = dwc2_get_glb_intstatus(bus);
  1277. if ((USB_OTG_GLB->GINTSTS & 0x1U) == USB_OTG_MODE_HOST) {
  1278. /* Avoid spurious interrupt */
  1279. if (gint_status == 0) {
  1280. return;
  1281. }
  1282. if (gint_status & USB_OTG_GINTSTS_HPRTINT) {
  1283. dwc2_port_irq_handler(bus);
  1284. }
  1285. if (gint_status & USB_OTG_GINTSTS_DISCINT) {
  1286. g_dwc2_hcd[bus->hcd.hcd_id].port_csc = 1;
  1287. bus->hcd.roothub.int_buffer[0] = (1 << 1);
  1288. usbh_hub_thread_wakeup(&bus->hcd.roothub);
  1289. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_DISCINT;
  1290. }
  1291. if (gint_status & USB_OTG_GINTSTS_HCINT) {
  1292. chan_int = (USB_OTG_HOST->HAINT & USB_OTG_HOST->HAINTMSK) & 0xFFFFU;
  1293. for (uint8_t i = 0U; i < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; i++) {
  1294. if ((chan_int & (1UL << (i & 0xFU))) != 0U) {
  1295. if ((USB_OTG_HC(i)->HCCHAR & USB_OTG_HCCHAR_EPDIR) == USB_OTG_HCCHAR_EPDIR) {
  1296. dwc2_inchan_irq_handler(bus, i);
  1297. } else {
  1298. dwc2_outchan_irq_handler(bus, i);
  1299. }
  1300. }
  1301. }
  1302. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_HCINT;
  1303. }
  1304. }
  1305. }