usb_hc_dwc2.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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. bus->hcd.roothub.speed = USB_SPEED_FULL;
  623. USB_OTG_HOST->HCFG |= USB_OTG_HCFG_FSLSPCLKSEL_48_MHZ;
  624. } else {
  625. bus->hcd.roothub.speed = USB_SPEED_HIGH;
  626. USB_OTG_HOST->HCFG |= USB_OTG_HCFG_FSLSPCLKSEL_30_60_MHZ;
  627. }
  628. if (g_dwc2_hcd[bus->hcd.hcd_id].hw_params.snpsid > 0x4F54292AU) {
  629. USB_OTG_HOST->HCFG |= USB_OTG_HFIR_RELOAD_CTRL;
  630. }
  631. /* Clear all pending HC Interrupts */
  632. for (uint8_t i = 0U; i < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; i++) {
  633. USB_OTG_HC(i)->HCINT = 0xFFFFFFFFU;
  634. USB_OTG_HC(i)->HCINTMSK = 0U;
  635. }
  636. /* Disable all interrupts. */
  637. USB_OTG_GLB->GINTMSK = 0U;
  638. /* Clear any pending interrupts */
  639. USB_OTG_GLB->GINTSTS = 0xFFFFFFFFU;
  640. /* set Rx FIFO size */
  641. USB_OTG_GLB->GRXFSIZ = g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size;
  642. 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) |
  643. g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size);
  644. 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) |
  645. (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));
  646. ret = dwc2_flush_txfifo(bus, 0x10U);
  647. ret = dwc2_flush_rxfifo(bus);
  648. USB_OTG_GLB->GAHBCFG &= ~USB_OTG_GAHBCFG_HBSTLEN;
  649. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_HBSTLEN_4;
  650. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_DMAEN;
  651. /* Enable interrupts matching to the Host mode ONLY */
  652. USB_OTG_GLB->GINTMSK |= (USB_OTG_GINTMSK_PRTIM | USB_OTG_GINTMSK_HCIM |
  653. USB_OTG_GINTSTS_DISCINT);
  654. dwc2_drivebus(bus, 1);
  655. usb_osal_msleep(200);
  656. USB_OTG_GLB->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
  657. return ret;
  658. }
  659. int usb_hc_deinit(struct usbh_bus *bus)
  660. {
  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 (uint8_t chidx = 0; chidx < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; chidx++) {
  666. dwc2_halt(bus, chidx);
  667. }
  668. /* Disable all interrupts. */
  669. USB_OTG_GLB->GINTMSK = 0U;
  670. /* Clear any pending Host interrupts */
  671. USB_OTG_HOST->HAINT = 0xFFFFFFFFU;
  672. USB_OTG_GLB->GINTSTS = 0xFFFFFFFFU;
  673. dwc2_drivebus(bus, 0);
  674. usb_osal_msleep(200);
  675. for (uint8_t chidx = 0; chidx < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; chidx++) {
  676. usb_osal_sem_delete(g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx].waitsem);
  677. }
  678. usb_hc_low_level_deinit(bus);
  679. return 0;
  680. }
  681. uint16_t usbh_get_frame_number(struct usbh_bus *bus)
  682. {
  683. return (USB_OTG_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
  684. }
  685. int usbh_roothub_control(struct usbh_bus *bus, struct usb_setup_packet *setup, uint8_t *buf)
  686. {
  687. __IO uint32_t hprt0;
  688. uint8_t nports;
  689. uint8_t port;
  690. uint32_t status;
  691. nports = CONFIG_USBHOST_MAX_RHPORTS;
  692. port = setup->wIndex;
  693. if (setup->bmRequestType & USB_REQUEST_RECIPIENT_DEVICE) {
  694. switch (setup->bRequest) {
  695. case HUB_REQUEST_CLEAR_FEATURE:
  696. switch (setup->wValue) {
  697. case HUB_FEATURE_HUB_C_LOCALPOWER:
  698. break;
  699. case HUB_FEATURE_HUB_C_OVERCURRENT:
  700. break;
  701. default:
  702. return -USB_ERR_NOTSUPP;
  703. }
  704. break;
  705. case HUB_REQUEST_SET_FEATURE:
  706. switch (setup->wValue) {
  707. case HUB_FEATURE_HUB_C_LOCALPOWER:
  708. break;
  709. case HUB_FEATURE_HUB_C_OVERCURRENT:
  710. break;
  711. default:
  712. return -USB_ERR_NOTSUPP;
  713. }
  714. break;
  715. case HUB_REQUEST_GET_DESCRIPTOR:
  716. break;
  717. case HUB_REQUEST_GET_STATUS:
  718. memset(buf, 0, 4);
  719. break;
  720. default:
  721. break;
  722. }
  723. } else if (setup->bmRequestType & USB_REQUEST_RECIPIENT_OTHER) {
  724. switch (setup->bRequest) {
  725. case HUB_REQUEST_CLEAR_FEATURE:
  726. if (!port || port > nports) {
  727. return -USB_ERR_INVAL;
  728. }
  729. switch (setup->wValue) {
  730. case HUB_PORT_FEATURE_ENABLE:
  731. USB_OTG_HPRT &= ~USB_OTG_HPRT_PENA;
  732. break;
  733. case HUB_PORT_FEATURE_SUSPEND:
  734. case HUB_PORT_FEATURE_C_SUSPEND:
  735. break;
  736. case HUB_PORT_FEATURE_POWER:
  737. dwc2_drivebus(bus, 0);
  738. break;
  739. case HUB_PORT_FEATURE_C_CONNECTION:
  740. g_dwc2_hcd[bus->hcd.hcd_id].port_csc = 0;
  741. break;
  742. case HUB_PORT_FEATURE_C_ENABLE:
  743. g_dwc2_hcd[bus->hcd.hcd_id].port_pec = 0;
  744. break;
  745. case HUB_PORT_FEATURE_C_OVER_CURREN:
  746. g_dwc2_hcd[bus->hcd.hcd_id].port_occ = 0;
  747. break;
  748. case HUB_PORT_FEATURE_C_RESET:
  749. break;
  750. default:
  751. return -USB_ERR_NOTSUPP;
  752. }
  753. break;
  754. case HUB_REQUEST_SET_FEATURE:
  755. if (!port || port > nports) {
  756. return -USB_ERR_INVAL;
  757. }
  758. switch (setup->wValue) {
  759. case HUB_PORT_FEATURE_SUSPEND:
  760. break;
  761. case HUB_PORT_FEATURE_POWER:
  762. dwc2_drivebus(bus, 1);
  763. break;
  764. case HUB_PORT_FEATURE_RESET:
  765. return usbh_reset_port(bus, port);
  766. default:
  767. return -USB_ERR_NOTSUPP;
  768. }
  769. break;
  770. case HUB_REQUEST_GET_STATUS:
  771. if (!port || port > nports) {
  772. return -USB_ERR_INVAL;
  773. }
  774. hprt0 = USB_OTG_HPRT;
  775. status = 0;
  776. if (g_dwc2_hcd[bus->hcd.hcd_id].port_csc) {
  777. status |= (1 << HUB_PORT_FEATURE_C_CONNECTION);
  778. }
  779. if (g_dwc2_hcd[bus->hcd.hcd_id].port_pec) {
  780. status |= (1 << HUB_PORT_FEATURE_C_ENABLE);
  781. }
  782. if (g_dwc2_hcd[bus->hcd.hcd_id].port_occ) {
  783. status |= (1 << HUB_PORT_FEATURE_C_OVER_CURREN);
  784. }
  785. if (hprt0 & USB_OTG_HPRT_PCSTS) {
  786. status |= (1 << HUB_PORT_FEATURE_CONNECTION);
  787. }
  788. if (hprt0 & USB_OTG_HPRT_PENA) {
  789. status |= (1 << HUB_PORT_FEATURE_ENABLE);
  790. if (usbh_get_port_speed(bus, port) == USB_SPEED_LOW) {
  791. status |= (1 << HUB_PORT_FEATURE_LOWSPEED);
  792. } else if (usbh_get_port_speed(bus, port) == USB_SPEED_HIGH) {
  793. status |= (1 << HUB_PORT_FEATURE_HIGHSPEED);
  794. }
  795. }
  796. if (hprt0 & USB_OTG_HPRT_POCA) {
  797. status |= (1 << HUB_PORT_FEATURE_OVERCURRENT);
  798. }
  799. if (hprt0 & USB_OTG_HPRT_PRST) {
  800. status |= (1 << HUB_PORT_FEATURE_RESET);
  801. }
  802. if (hprt0 & USB_OTG_HPRT_PPWR) {
  803. status |= (1 << HUB_PORT_FEATURE_POWER);
  804. }
  805. memcpy(buf, &status, 4);
  806. break;
  807. default:
  808. break;
  809. }
  810. }
  811. return 0;
  812. }
  813. int usbh_submit_urb(struct usbh_urb *urb)
  814. {
  815. struct dwc2_chan *chan;
  816. struct usbh_bus *bus;
  817. size_t flags;
  818. int ret = 0;
  819. int chidx;
  820. if (!urb || !urb->hport || !urb->ep || !urb->hport->bus) {
  821. return -USB_ERR_INVAL;
  822. }
  823. /* dma addr must be aligned 4 bytes */
  824. USB_ASSERT_MSG(!((uintptr_t)urb->setup % 4) && !((uintptr_t)urb->transfer_buffer % 4),
  825. "urb->setup or urb->transfer_buffer is not aligned 4 bytes");
  826. #ifdef CONFIG_USB_DCACHE_ENABLE
  827. USB_ASSERT_MSG(!((uintptr_t)urb->setup % CONFIG_USB_ALIGN_SIZE) &&
  828. !((uintptr_t)urb->transfer_buffer % CONFIG_USB_ALIGN_SIZE),
  829. "urb->setup or urb->transfer_buffer is not aligned %d", CONFIG_USB_ALIGN_SIZE);
  830. #endif
  831. bus = urb->hport->bus;
  832. if (!(USB_OTG_HPRT & USB_OTG_HPRT_PCSTS) || !urb->hport->connected) {
  833. return -USB_ERR_NOTCONN;
  834. }
  835. if (urb->errorcode == -USB_ERR_BUSY) {
  836. return -USB_ERR_BUSY;
  837. }
  838. if (urb->ep->bEndpointAddress & 0x80) {
  839. /* Check if pipe rx fifo is overflow */
  840. if (USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) > (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_rx_fifo_size * 4)) {
  841. return -USB_ERR_RANGE;
  842. }
  843. } else {
  844. /* Check if intr and iso pipe tx fifo is overflow */
  845. if (((USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS) ||
  846. (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT)) &&
  847. USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) > (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_perio_tx_fifo_size * 4)) {
  848. return -USB_ERR_RANGE;
  849. } else {
  850. /* Check if control and bulk pipe tx fifo is overflow */
  851. if (USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) > (g_dwc2_hcd[bus->hcd.hcd_id].user_params.host_nperio_tx_fifo_size * 4)) {
  852. return -USB_ERR_RANGE;
  853. }
  854. }
  855. }
  856. chidx = dwc2_chan_alloc(bus);
  857. if (chidx == -1) {
  858. return -USB_ERR_NOMEM;
  859. }
  860. flags = usb_osal_enter_critical_section();
  861. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
  862. chan->chidx = chidx;
  863. chan->urb = urb;
  864. chan->do_ssplit = 0;
  865. if (urb->hport->speed != USB_SPEED_HIGH &&
  866. usbh_get_port_speed(bus, 0) == USB_SPEED_HIGH) {
  867. chan->do_ssplit = 1;
  868. chan->do_csplit = 0;
  869. chan->hub_port = urb->hport->port;
  870. chan->hub_addr = urb->hport->parent->hub_addr;
  871. }
  872. urb->hcpriv = chan;
  873. urb->errorcode = -USB_ERR_BUSY;
  874. urb->actual_length = 0;
  875. usb_osal_leave_critical_section(flags);
  876. if (urb->setup) {
  877. usb_dcache_clean((uintptr_t)urb->setup, USB_ALIGN_UP(sizeof(struct usb_setup_packet), CONFIG_USB_ALIGN_SIZE));
  878. if (urb->transfer_buffer) {
  879. if (urb->setup->bmRequestType & 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. }
  885. } else if (urb->transfer_buffer && (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) != USB_ENDPOINT_TYPE_ISOCHRONOUS)) {
  886. if (urb->ep->bEndpointAddress & 0x80) {
  887. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  888. } else {
  889. usb_dcache_clean((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->transfer_buffer_length, CONFIG_USB_ALIGN_SIZE));
  890. }
  891. } else {
  892. }
  893. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  894. case USB_ENDPOINT_TYPE_CONTROL:
  895. chan->ep0_state = DWC2_EP0_STATE_SETUP;
  896. dwc2_control_urb_init(bus, chidx, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  897. break;
  898. case USB_ENDPOINT_TYPE_BULK:
  899. case USB_ENDPOINT_TYPE_INTERRUPT:
  900. dwc2_bulk_intr_urb_init(bus, chidx, urb, urb->transfer_buffer, urb->transfer_buffer_length);
  901. break;
  902. case USB_ENDPOINT_TYPE_ISOCHRONOUS:
  903. break;
  904. default:
  905. break;
  906. }
  907. if (urb->timeout > 0) {
  908. /* wait until timeout or sem give */
  909. ret = usb_osal_sem_take(chan->waitsem, urb->timeout);
  910. if (ret < 0) {
  911. goto errout_timeout;
  912. }
  913. urb->timeout = 0;
  914. ret = urb->errorcode;
  915. /* we can free chan when waitsem is done */
  916. dwc2_chan_free(chan);
  917. }
  918. return ret;
  919. errout_timeout:
  920. urb->timeout = 0;
  921. usbh_kill_urb(urb);
  922. return ret;
  923. }
  924. int usbh_kill_urb(struct usbh_urb *urb)
  925. {
  926. struct dwc2_chan *chan;
  927. struct usbh_bus *bus;
  928. size_t flags;
  929. if (!urb || !urb->hcpriv || !urb->hport->bus) {
  930. return -USB_ERR_INVAL;
  931. }
  932. bus = urb->hport->bus;
  933. flags = usb_osal_enter_critical_section();
  934. chan = (struct dwc2_chan *)urb->hcpriv;
  935. dwc2_halt(bus, chan->chidx);
  936. urb->errorcode = -USB_ERR_SHUTDOWN;
  937. if (urb->timeout) {
  938. usb_osal_sem_give(chan->waitsem);
  939. } else {
  940. dwc2_chan_free(chan);
  941. }
  942. if (urb->complete) {
  943. urb->complete(urb->arg, urb->errorcode);
  944. }
  945. usb_osal_leave_critical_section(flags);
  946. return 0;
  947. }
  948. static inline void dwc2_urb_waitup(struct usbh_urb *urb)
  949. {
  950. struct dwc2_chan *chan;
  951. chan = (struct dwc2_chan *)urb->hcpriv;
  952. if (urb->timeout) {
  953. usb_osal_sem_give(chan->waitsem);
  954. } else {
  955. dwc2_chan_free(chan);
  956. }
  957. if (urb->complete) {
  958. if (urb->errorcode < 0) {
  959. urb->complete(urb->arg, urb->errorcode);
  960. } else {
  961. urb->complete(urb->arg, urb->actual_length);
  962. }
  963. }
  964. }
  965. static void dwc2_inchan_irq_handler(struct usbh_bus *bus, uint8_t ch_num)
  966. {
  967. uint32_t chan_intstatus;
  968. struct dwc2_chan *chan;
  969. struct usbh_urb *urb;
  970. chan_intstatus = USB_OTG_HC(ch_num)->HCINT;
  971. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[ch_num];
  972. urb = chan->urb;
  973. //printf("s1:%08x\r\n", chan_intstatus);
  974. if (chan_intstatus & USB_OTG_HCINT_CHH) {
  975. USB_OTG_HC(ch_num)->HCINT = chan_intstatus;
  976. if (chan_intstatus & USB_OTG_HCINT_XFRC) {
  977. uint32_t count = chan->xferlen - (USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ); /* how many size has received */
  978. uint8_t data_toggle = ((USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_DPID) >> USB_OTG_HCTSIZ_DPID_Pos);
  979. urb->actual_length += count;
  980. urb->transfer_buffer_length -= count;
  981. if (data_toggle == HC_PID_DATA0) {
  982. urb->data_toggle = 0;
  983. } else {
  984. urb->data_toggle = 1;
  985. }
  986. if (chan->do_csplit) {
  987. chan->do_csplit = 0;
  988. dwc2_chan_enable_csplit(bus, ch_num, false);
  989. }
  990. if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_CONTROL) {
  991. if (chan->ep0_state == DWC2_EP0_STATE_INDATA) {
  992. if (chan->do_ssplit && urb->transfer_buffer_length > 0 && (count == USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize))) {
  993. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  994. } else {
  995. chan->ep0_state = DWC2_EP0_STATE_OUTSTATUS;
  996. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  997. }
  998. } else if (chan->ep0_state == DWC2_EP0_STATE_INSTATUS) {
  999. chan->ep0_state = DWC2_EP0_STATE_SETUP;
  1000. urb->errorcode = 0;
  1001. dwc2_urb_waitup(urb);
  1002. }
  1003. } else if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  1004. } else {
  1005. if (chan->do_ssplit && urb->transfer_buffer_length > 0 && (count == USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize))) {
  1006. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1007. } else {
  1008. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->actual_length, CONFIG_USB_ALIGN_SIZE));
  1009. urb->errorcode = 0;
  1010. dwc2_urb_waitup(urb);
  1011. }
  1012. }
  1013. } else if (chan_intstatus & USB_OTG_HCINT_AHBERR) {
  1014. urb->errorcode = -USB_ERR_IO;
  1015. dwc2_urb_waitup(urb);
  1016. } else if (chan_intstatus & USB_OTG_HCINT_STALL) {
  1017. urb->errorcode = -USB_ERR_STALL;
  1018. dwc2_urb_waitup(urb);
  1019. } else if (chan_intstatus & USB_OTG_HCINT_NAK) {
  1020. if (chan->do_ssplit) {
  1021. /* restart ssplit transfer */
  1022. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1023. case USB_ENDPOINT_TYPE_CONTROL:
  1024. case USB_ENDPOINT_TYPE_BULK:
  1025. chan->do_csplit = 0;
  1026. dwc2_chan_enable_csplit(bus, ch_num, false);
  1027. dwc2_chan_reenable(bus, ch_num);
  1028. break;
  1029. case USB_ENDPOINT_TYPE_INTERRUPT:
  1030. dwc2_chan_enable_csplit(bus, ch_num, false);
  1031. urb->errorcode = -USB_ERR_NAK;
  1032. dwc2_urb_waitup(urb);
  1033. break;
  1034. default:
  1035. break;
  1036. }
  1037. } else {
  1038. urb->errorcode = -USB_ERR_NAK;
  1039. dwc2_urb_waitup(urb);
  1040. }
  1041. } else if (chan_intstatus & USB_OTG_HCINT_ACK) {
  1042. if (chan->do_ssplit) {
  1043. /* start ssplit transfer */
  1044. chan->do_csplit = 1;
  1045. chan->ssplit_frame = dwc2_get_full_frame_num(bus);
  1046. dwc2_chan_enable_csplit(bus, ch_num, true);
  1047. dwc2_chan_reenable(bus, ch_num);
  1048. }
  1049. } else if (chan_intstatus & USB_OTG_HCINT_NYET) {
  1050. if (chan->do_ssplit) {
  1051. /* restart csplit transfer */
  1052. dwc2_chan_enable_csplit(bus, ch_num, true);
  1053. dwc2_chan_reenable(bus, ch_num);
  1054. } else {
  1055. urb->errorcode = -USB_ERR_NAK;
  1056. dwc2_urb_waitup(urb);
  1057. }
  1058. } else if (chan_intstatus & USB_OTG_HCINT_TXERR) {
  1059. urb->errorcode = -USB_ERR_IO;
  1060. dwc2_urb_waitup(urb);
  1061. } else if (chan_intstatus & USB_OTG_HCINT_BBERR) {
  1062. urb->errorcode = -USB_ERR_BABBLE;
  1063. dwc2_urb_waitup(urb);
  1064. } else if (chan_intstatus & USB_OTG_HCINT_DTERR) {
  1065. urb->errorcode = -USB_ERR_DT;
  1066. dwc2_urb_waitup(urb);
  1067. } else if (chan_intstatus & USB_OTG_HCINT_FRMOR) {
  1068. urb->errorcode = -USB_ERR_IO;
  1069. dwc2_urb_waitup(urb);
  1070. }
  1071. }
  1072. }
  1073. static void dwc2_outchan_irq_handler(struct usbh_bus *bus, uint8_t ch_num)
  1074. {
  1075. uint32_t chan_intstatus;
  1076. struct dwc2_chan *chan;
  1077. struct usbh_urb *urb;
  1078. chan_intstatus = USB_OTG_HC(ch_num)->HCINT;
  1079. chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[ch_num];
  1080. urb = chan->urb;
  1081. //printf("s2:%08x\r\n", chan_intstatus);
  1082. if (chan_intstatus & USB_OTG_HCINT_CHH) {
  1083. USB_OTG_HC(ch_num)->HCINT = chan_intstatus;
  1084. if (chan_intstatus & USB_OTG_HCINT_XFRC) {
  1085. uint32_t count = USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ; /* last packet size */
  1086. uint32_t has_used_packets = chan->num_packets - ((USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) >> 19); /* how many packets have used */
  1087. uint32_t olen = (has_used_packets - 1) * USB_GET_MAXPACKETSIZE(urb->ep->wMaxPacketSize) + count; /* the same with urb->actual_length += chan->xferlen; */
  1088. uint8_t data_toggle = ((USB_OTG_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_DPID) >> USB_OTG_HCTSIZ_DPID_Pos);
  1089. urb->actual_length += olen;
  1090. if (chan->ep0_state == DWC2_EP0_STATE_OUTDATA || urb->setup == NULL) {
  1091. if (urb->transfer_buffer_length > olen) {
  1092. urb->transfer_buffer_length -= olen;
  1093. } else {
  1094. urb->transfer_buffer_length = 0;
  1095. }
  1096. }
  1097. if (data_toggle == HC_PID_DATA0) {
  1098. urb->data_toggle = 0;
  1099. } else {
  1100. urb->data_toggle = 1;
  1101. }
  1102. if (chan->do_csplit) {
  1103. chan->do_csplit = 0;
  1104. dwc2_chan_enable_csplit(bus, ch_num, false);
  1105. }
  1106. if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_CONTROL) {
  1107. if (chan->ep0_state == DWC2_EP0_STATE_SETUP) {
  1108. if (urb->setup->wLength) {
  1109. if (urb->setup->bmRequestType & 0x80) {
  1110. chan->ep0_state = DWC2_EP0_STATE_INDATA;
  1111. } else {
  1112. chan->ep0_state = DWC2_EP0_STATE_OUTDATA;
  1113. }
  1114. } else {
  1115. chan->ep0_state = DWC2_EP0_STATE_INSTATUS;
  1116. }
  1117. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  1118. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTDATA) {
  1119. if (chan->do_ssplit && urb->transfer_buffer_length > 0) {
  1120. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer + urb->actual_length - 8, urb->transfer_buffer_length);
  1121. } else {
  1122. chan->ep0_state = DWC2_EP0_STATE_INSTATUS;
  1123. dwc2_control_urb_init(bus, ch_num, urb, urb->setup, urb->transfer_buffer, urb->transfer_buffer_length);
  1124. }
  1125. } else if (chan->ep0_state == DWC2_EP0_STATE_OUTSTATUS) {
  1126. usb_dcache_invalidate((uintptr_t)urb->transfer_buffer, USB_ALIGN_UP(urb->actual_length - 8, CONFIG_USB_ALIGN_SIZE));
  1127. chan->ep0_state = DWC2_EP0_STATE_SETUP;
  1128. urb->errorcode = 0;
  1129. dwc2_urb_waitup(urb);
  1130. }
  1131. } else if (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes) == USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  1132. } else {
  1133. if (chan->do_ssplit && urb->transfer_buffer_length > 0) {
  1134. dwc2_bulk_intr_urb_init(bus, ch_num, urb, urb->transfer_buffer + urb->actual_length, urb->transfer_buffer_length);
  1135. } else {
  1136. urb->errorcode = 0;
  1137. dwc2_urb_waitup(urb);
  1138. }
  1139. }
  1140. } else if (chan_intstatus & USB_OTG_HCINT_AHBERR) {
  1141. urb->errorcode = -USB_ERR_IO;
  1142. dwc2_urb_waitup(urb);
  1143. } else if (chan_intstatus & USB_OTG_HCINT_STALL) {
  1144. urb->errorcode = -USB_ERR_STALL;
  1145. dwc2_urb_waitup(urb);
  1146. } else if (chan_intstatus & USB_OTG_HCINT_NAK) {
  1147. if (chan->do_ssplit) {
  1148. /* restart ssplit transfer */
  1149. switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
  1150. case USB_ENDPOINT_TYPE_CONTROL:
  1151. case USB_ENDPOINT_TYPE_BULK:
  1152. chan->do_csplit = 0;
  1153. dwc2_chan_enable_csplit(bus, ch_num, false);
  1154. dwc2_chan_reenable(bus, ch_num);
  1155. break;
  1156. case USB_ENDPOINT_TYPE_INTERRUPT:
  1157. dwc2_chan_enable_csplit(bus, ch_num, false);
  1158. urb->errorcode = -USB_ERR_NAK;
  1159. dwc2_urb_waitup(urb);
  1160. break;
  1161. default:
  1162. break;
  1163. }
  1164. } else {
  1165. urb->errorcode = -USB_ERR_NAK;
  1166. dwc2_urb_waitup(urb);
  1167. }
  1168. } else if (chan_intstatus & USB_OTG_HCINT_ACK) {
  1169. if (chan->do_ssplit) {
  1170. /* start ssplit transfer */
  1171. chan->do_csplit = 1;
  1172. chan->ssplit_frame = dwc2_get_full_frame_num(bus);
  1173. dwc2_chan_enable_csplit(bus, ch_num, true);
  1174. dwc2_chan_reenable(bus, ch_num);
  1175. }
  1176. } else if (chan_intstatus & USB_OTG_HCINT_NYET) {
  1177. if (chan->do_ssplit) {
  1178. /* restart csplit transfer */
  1179. dwc2_chan_enable_csplit(bus, ch_num, true);
  1180. dwc2_chan_reenable(bus, ch_num);
  1181. } else {
  1182. urb->errorcode = -USB_ERR_NAK;
  1183. dwc2_urb_waitup(urb);
  1184. }
  1185. } else if (chan_intstatus & USB_OTG_HCINT_TXERR) {
  1186. urb->errorcode = -USB_ERR_IO;
  1187. dwc2_urb_waitup(urb);
  1188. } else if (chan_intstatus & USB_OTG_HCINT_BBERR) {
  1189. urb->errorcode = -USB_ERR_BABBLE;
  1190. dwc2_urb_waitup(urb);
  1191. } else if (chan_intstatus & USB_OTG_HCINT_DTERR) {
  1192. urb->errorcode = -USB_ERR_DT;
  1193. dwc2_urb_waitup(urb);
  1194. } else if (chan_intstatus & USB_OTG_HCINT_FRMOR) {
  1195. urb->errorcode = -USB_ERR_IO;
  1196. dwc2_urb_waitup(urb);
  1197. }
  1198. }
  1199. }
  1200. static void dwc2_port_irq_handler(struct usbh_bus *bus)
  1201. {
  1202. __IO uint32_t hprt0, hprt0_dup, regval;
  1203. /* Handle Host Port Interrupts */
  1204. hprt0 = USB_OTG_HPRT;
  1205. hprt0_dup = USB_OTG_HPRT;
  1206. hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  1207. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  1208. /* Check whether Port Connect detected */
  1209. if ((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET) {
  1210. if ((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS) {
  1211. bus->hcd.roothub.int_buffer[0] = (1 << 1);
  1212. usbh_hub_thread_wakeup(&bus->hcd.roothub);
  1213. }
  1214. hprt0_dup |= USB_OTG_HPRT_PCDET;
  1215. g_dwc2_hcd[bus->hcd.hcd_id].port_csc = 1;
  1216. }
  1217. /* Check whether Port Enable Changed */
  1218. if ((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG) {
  1219. hprt0_dup |= USB_OTG_HPRT_PENCHNG;
  1220. g_dwc2_hcd[bus->hcd.hcd_id].port_pec = 1;
  1221. if ((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA) {
  1222. regval = USB_OTG_HOST->HFIR;
  1223. regval &= ~USB_OTG_HFIR_FRIVL;
  1224. regval |= dwc2_calc_frame_interval(bus) & USB_OTG_HFIR_FRIVL;
  1225. USB_OTG_HOST->HFIR = regval;
  1226. if (g_dwc2_hcd[bus->hcd.hcd_id].user_params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
  1227. if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17)) {
  1228. if ((USB_OTG_HOST->HCFG & USB_OTG_HCFG_FSLSPCS) != USB_OTG_HCFG_FSLSPCLKSEL_6_MHZ) {
  1229. regval = USB_OTG_HOST->HCFG;
  1230. regval &= ~USB_OTG_HCFG_FSLSPCS;
  1231. regval |= USB_OTG_HCFG_FSLSPCLKSEL_6_MHZ;
  1232. USB_OTG_HOST->HCFG = regval;
  1233. }
  1234. } else {
  1235. if ((USB_OTG_HOST->HCFG & USB_OTG_HCFG_FSLSPCS) != USB_OTG_HCFG_FSLSPCLKSEL_48_MHZ) {
  1236. regval = USB_OTG_HOST->HCFG;
  1237. regval &= ~USB_OTG_HCFG_FSLSPCS;
  1238. regval |= USB_OTG_HCFG_FSLSPCLKSEL_48_MHZ;
  1239. USB_OTG_HOST->HCFG = regval;
  1240. }
  1241. }
  1242. }
  1243. } else {
  1244. }
  1245. }
  1246. /* Check for an overcurrent */
  1247. if ((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG) {
  1248. hprt0_dup |= USB_OTG_HPRT_POCCHNG;
  1249. g_dwc2_hcd[bus->hcd.hcd_id].port_occ = 1;
  1250. }
  1251. /* Clear Port Interrupts */
  1252. USB_OTG_HPRT = hprt0_dup;
  1253. }
  1254. void USBH_IRQHandler(uint8_t busid)
  1255. {
  1256. uint32_t gint_status, chan_int;
  1257. struct usbh_bus *bus;
  1258. bus = &g_usbhost_bus[busid];
  1259. gint_status = dwc2_get_glb_intstatus(bus);
  1260. if ((USB_OTG_GLB->GINTSTS & 0x1U) == USB_OTG_MODE_HOST) {
  1261. /* Avoid spurious interrupt */
  1262. if (gint_status == 0) {
  1263. return;
  1264. }
  1265. if (gint_status & USB_OTG_GINTSTS_HPRTINT) {
  1266. dwc2_port_irq_handler(bus);
  1267. }
  1268. if (gint_status & USB_OTG_GINTSTS_DISCINT) {
  1269. g_dwc2_hcd[bus->hcd.hcd_id].port_csc = 1;
  1270. bus->hcd.roothub.int_buffer[0] = (1 << 1);
  1271. usbh_hub_thread_wakeup(&bus->hcd.roothub);
  1272. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_DISCINT;
  1273. }
  1274. if (gint_status & USB_OTG_GINTSTS_HCINT) {
  1275. chan_int = (USB_OTG_HOST->HAINT & USB_OTG_HOST->HAINTMSK) & 0xFFFFU;
  1276. for (uint8_t i = 0U; i < g_dwc2_hcd[bus->hcd.hcd_id].hw_params.host_channels; i++) {
  1277. if ((chan_int & (1UL << (i & 0xFU))) != 0U) {
  1278. if ((USB_OTG_HC(i)->HCCHAR & USB_OTG_HCCHAR_EPDIR) == USB_OTG_HCCHAR_EPDIR) {
  1279. dwc2_inchan_irq_handler(bus, i);
  1280. } else {
  1281. dwc2_outchan_irq_handler(bus, i);
  1282. }
  1283. }
  1284. }
  1285. USB_OTG_GLB->GINTSTS = USB_OTG_GINTSTS_HCINT;
  1286. }
  1287. }
  1288. }