usb_hc_dwc2.c 56 KB

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