stm32mp1_usb.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <drivers/delay_timer.h>
  11. #include <drivers/st/stm32mp1_usb.h>
  12. #include <lib/mmio.h>
  13. #include <platform_def.h>
  14. #define USB_OTG_MODE_DEVICE 0U
  15. #define USB_OTG_MODE_HOST 1U
  16. #define USB_OTG_MODE_DRD 2U
  17. #define EP_TYPE_CTRL 0U
  18. #define EP_TYPE_ISOC 1U
  19. #define EP_TYPE_BULK 2U
  20. #define EP_TYPE_INTR 3U
  21. #define USBD_FIFO_FLUSH_TIMEOUT_US 1000U
  22. #define EP0_FIFO_SIZE 64U
  23. /* OTG registers offsets */
  24. #define OTG_GOTGINT 0x004U
  25. #define OTG_GAHBCFG 0x008U
  26. #define OTG_GUSBCFG 0x00CU
  27. #define OTG_GRSTCTL 0x010U
  28. #define OTG_GINTSTS 0x014U
  29. #define OTG_GINTMSK 0x018U
  30. #define OTG_GRXSTSP 0x020U
  31. #define OTG_GLPMCFG 0x054U
  32. #define OTG_DCFG 0x800U
  33. #define OTG_DCTL 0x804U
  34. #define OTG_DSTS 0x808U
  35. #define OTG_DIEPMSK 0x810U
  36. #define OTG_DOEPMSK 0x814U
  37. #define OTG_DAINT 0x818U
  38. #define OTG_DAINTMSK 0x81CU
  39. #define OTG_DIEPEMPMSK 0x834U
  40. /* Definitions for OTG_DIEPx registers */
  41. #define OTG_DIEP_BASE 0x900U
  42. #define OTG_DIEP_SIZE 0x20U
  43. #define OTG_DIEPCTL 0x00U
  44. #define OTG_DIEPINT 0x08U
  45. #define OTG_DIEPTSIZ 0x10U
  46. #define OTG_DIEPDMA 0x14U
  47. #define OTG_DTXFSTS 0x18U
  48. #define OTG_DIEP_MAX_NB 9U
  49. /* Definitions for OTG_DOEPx registers */
  50. #define OTG_DOEP_BASE 0xB00U
  51. #define OTG_DOEP_SIZE 0x20U
  52. #define OTG_DOEPCTL 0x00U
  53. #define OTG_DOEPINT 0x08U
  54. #define OTG_DOEPTSIZ 0x10U
  55. #define OTG_DOEPDMA 0x14U
  56. #define OTG_D0EP_MAX_NB 9U
  57. /* Definitions for OTG_DAINT registers */
  58. #define OTG_DAINT_OUT_MASK GENMASK(31, 16)
  59. #define OTG_DAINT_OUT_SHIFT 16U
  60. #define OTG_DAINT_IN_MASK GENMASK(15, 0)
  61. #define OTG_DAINT_IN_SHIFT 0U
  62. #define OTG_DAINT_EP0_IN BIT(16)
  63. #define OTG_DAINT_EP0_OUT BIT(0)
  64. /* Definitions for FIFOs */
  65. #define OTG_FIFO_BASE 0x1000U
  66. #define OTG_FIFO_SIZE 0x1000U
  67. /* Bit definitions for OTG_GOTGINT register */
  68. #define OTG_GOTGINT_SEDET BIT(2)
  69. /* Bit definitions for OTG_GAHBCFG register */
  70. #define OTG_GAHBCFG_GINT BIT(0)
  71. /* Bit definitions for OTG_GUSBCFG register */
  72. #define OTG_GUSBCFG_TRDT GENMASK(13, 10)
  73. #define OTG_GUSBCFG_TRDT_SHIFT 10U
  74. #define USBD_HS_TRDT_VALUE 9U
  75. /* Bit definitions for OTG_GRSTCTL register */
  76. #define OTG_GRSTCTL_RXFFLSH BIT(4)
  77. #define OTG_GRSTCTL_TXFFLSH BIT(5)
  78. #define OTG_GRSTCTL_TXFNUM_SHIFT 6U
  79. /* Bit definitions for OTG_GINTSTS register */
  80. #define OTG_GINTSTS_CMOD BIT(0)
  81. #define OTG_GINTSTS_MMIS BIT(1)
  82. #define OTG_GINTSTS_OTGINT BIT(2)
  83. #define OTG_GINTSTS_SOF BIT(3)
  84. #define OTG_GINTSTS_RXFLVL BIT(4)
  85. #define OTG_GINTSTS_USBSUSP BIT(11)
  86. #define OTG_GINTSTS_USBRST BIT(12)
  87. #define OTG_GINTSTS_ENUMDNE BIT(13)
  88. #define OTG_GINTSTS_IEPINT BIT(18)
  89. #define OTG_GINTSTS_OEPINT BIT(19)
  90. #define OTG_GINTSTS_IISOIXFR BIT(20)
  91. #define OTG_GINTSTS_IPXFR_INCOMPISOOUT BIT(21)
  92. #define OTG_GINTSTS_LPMINT BIT(27)
  93. #define OTG_GINTSTS_SRQINT BIT(30)
  94. #define OTG_GINTSTS_WKUPINT BIT(31)
  95. /* Bit definitions for OTG_GRXSTSP register */
  96. #define OTG_GRXSTSP_EPNUM GENMASK(3, 0)
  97. #define OTG_GRXSTSP_BCNT GENMASK(14, 4)
  98. #define OTG_GRXSTSP_BCNT_SHIFT 4U
  99. #define OTG_GRXSTSP_PKTSTS GENMASK(20, 17)
  100. #define OTG_GRXSTSP_PKTSTS_SHIFT 17U
  101. #define STS_GOUT_NAK 1U
  102. #define STS_DATA_UPDT 2U
  103. #define STS_XFER_COMP 3U
  104. #define STS_SETUP_COMP 4U
  105. #define STS_SETUP_UPDT 6U
  106. /* Bit definitions for OTG_GLPMCFG register */
  107. #define OTG_GLPMCFG_BESL GENMASK(5, 2)
  108. /* Bit definitions for OTG_DCFG register */
  109. #define OTG_DCFG_DAD GENMASK(10, 4)
  110. #define OTG_DCFG_DAD_SHIFT 4U
  111. /* Bit definitions for OTG_DCTL register */
  112. #define OTG_DCTL_RWUSIG BIT(0)
  113. #define OTG_DCTL_SDIS BIT(1)
  114. #define OTG_DCTL_CGINAK BIT(8)
  115. /* Bit definitions for OTG_DSTS register */
  116. #define OTG_DSTS_SUSPSTS BIT(0)
  117. #define OTG_DSTS_ENUMSPD_MASK GENMASK(2, 1)
  118. #define OTG_DSTS_FNSOF0 BIT(8)
  119. #define OTG_DSTS_ENUMSPD(val) ((val) << 1)
  120. #define OTG_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ OTG_DSTS_ENUMSPD(0U)
  121. #define OTG_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ OTG_DSTS_ENUMSPD(1U)
  122. #define OTG_DSTS_ENUMSPD_LS_PHY_6MHZ OTG_DSTS_ENUMSPD(2U)
  123. #define OTG_DSTS_ENUMSPD_FS_PHY_48MHZ OTG_DSTS_ENUMSPD(3U)
  124. /* Bit definitions for OTG_DIEPMSK register */
  125. #define OTG_DIEPMSK_XFRCM BIT(0)
  126. #define OTG_DIEPMSK_EPDM BIT(1)
  127. #define OTG_DIEPMSK_TOM BIT(3)
  128. /* Bit definitions for OTG_DOEPMSK register */
  129. #define OTG_DOEPMSK_XFRCM BIT(0)
  130. #define OTG_DOEPMSK_EPDM BIT(1)
  131. #define OTG_DOEPMSK_STUPM BIT(3)
  132. /* Bit definitions for OTG_DIEPCTLx registers */
  133. #define OTG_DIEPCTL_MPSIZ GENMASK(10, 0)
  134. #define OTG_DIEPCTL_STALL BIT(21)
  135. #define OTG_DIEPCTL_CNAK BIT(26)
  136. #define OTG_DIEPCTL_SD0PID_SEVNFRM BIT(28)
  137. #define OTG_DIEPCTL_SODDFRM BIT(29)
  138. #define OTG_DIEPCTL_EPDIS BIT(30)
  139. #define OTG_DIEPCTL_EPENA BIT(31)
  140. /* Bit definitions for OTG_DIEPINTx registers */
  141. #define OTG_DIEPINT_XFRC BIT(0)
  142. #define OTG_DIEPINT_EPDISD BIT(1)
  143. #define OTG_DIEPINT_TOC BIT(3)
  144. #define OTG_DIEPINT_ITTXFE BIT(4)
  145. #define OTG_DIEPINT_INEPNE BIT(6)
  146. #define OTG_DIEPINT_TXFE BIT(7)
  147. #define OTG_DIEPINT_TXFE_SHIFT 7U
  148. #define OTG_DIEPINT_MASK (BIT(13) | BIT(11) | GENMASK(9, 0))
  149. /* Bit definitions for OTG_DIEPTSIZx registers */
  150. #define OTG_DIEPTSIZ_XFRSIZ GENMASK(18, 0)
  151. #define OTG_DIEPTSIZ_PKTCNT GENMASK(28, 19)
  152. #define OTG_DIEPTSIZ_PKTCNT_SHIFT 19U
  153. #define OTG_DIEPTSIZ_MCNT_MASK GENMASK(30, 29)
  154. #define OTG_DIEPTSIZ_MCNT_DATA0 BIT(29)
  155. #define OTG_DIEPTSIZ_PKTCNT_1 BIT(19)
  156. /* Bit definitions for OTG_DTXFSTSx registers */
  157. #define OTG_DTXFSTS_INEPTFSAV GENMASK(15, 0)
  158. /* Bit definitions for OTG_DOEPCTLx registers */
  159. #define OTG_DOEPCTL_STALL BIT(21)
  160. #define OTG_DOEPCTL_CNAK BIT(26)
  161. #define OTG_DOEPCTL_SD0PID_SEVNFRM BIT(28) /* other than endpoint 0 */
  162. #define OTG_DOEPCTL_SD1PID_SODDFRM BIT(29) /* other than endpoint 0 */
  163. #define OTG_DOEPCTL_EPDIS BIT(30)
  164. #define OTG_DOEPCTL_EPENA BIT(31)
  165. /* Bit definitions for OTG_DOEPTSIZx registers */
  166. #define OTG_DOEPTSIZ_XFRSIZ GENMASK(18, 0)
  167. #define OTG_DOEPTSIZ_PKTCNT GENMASK(28, 19)
  168. #define OTG_DOEPTSIZ_RXDPID_STUPCNT GENMASK(30, 29)
  169. /* Bit definitions for OTG_DOEPINTx registers */
  170. #define OTG_DOEPINT_XFRC BIT(0)
  171. #define OTG_DOEPINT_STUP BIT(3)
  172. #define OTG_DOEPINT_OTEPDIS BIT(4)
  173. #define OTG_DOEPINT_MASK (GENMASK(15, 12) | GENMASK(9, 8) | GENMASK(6, 0))
  174. #define EP_NB 15U
  175. #define EP_ALL 0x10U
  176. /*
  177. * Flush TX FIFO.
  178. * handle: PCD handle.
  179. * num: FIFO number.
  180. * This parameter can be a value from 1 to 15 or EP_ALL.
  181. * EP_ALL= 0x10 means Flush all TX FIFOs
  182. * return: USB status.
  183. */
  184. static enum usb_status usb_dwc2_flush_tx_fifo(void *handle, uint32_t num)
  185. {
  186. uintptr_t usb_base_addr = (uintptr_t)handle;
  187. uint64_t timeout = timeout_init_us(USBD_FIFO_FLUSH_TIMEOUT_US);
  188. mmio_write_32(usb_base_addr + OTG_GRSTCTL,
  189. OTG_GRSTCTL_TXFFLSH | (uint32_t)(num << OTG_GRSTCTL_TXFNUM_SHIFT));
  190. while ((mmio_read_32(usb_base_addr + OTG_GRSTCTL) &
  191. OTG_GRSTCTL_TXFFLSH) == OTG_GRSTCTL_TXFFLSH) {
  192. if (timeout_elapsed(timeout)) {
  193. return USBD_TIMEOUT;
  194. }
  195. }
  196. return USBD_OK;
  197. }
  198. /*
  199. * Flush RX FIFO.
  200. * handle: PCD handle.
  201. * return: USB status.
  202. */
  203. static enum usb_status usb_dwc2_flush_rx_fifo(void *handle)
  204. {
  205. uintptr_t usb_base_addr = (uintptr_t)handle;
  206. uint64_t timeout = timeout_init_us(USBD_FIFO_FLUSH_TIMEOUT_US);
  207. mmio_write_32(usb_base_addr + OTG_GRSTCTL, OTG_GRSTCTL_RXFFLSH);
  208. while ((mmio_read_32(usb_base_addr + OTG_GRSTCTL) &
  209. OTG_GRSTCTL_RXFFLSH) == OTG_GRSTCTL_RXFFLSH) {
  210. if (timeout_elapsed(timeout)) {
  211. return USBD_TIMEOUT;
  212. }
  213. }
  214. return USBD_OK;
  215. }
  216. /*
  217. * Return the global USB interrupt status.
  218. * handle: PCD handle.
  219. * return: Interrupt register value.
  220. */
  221. static uint32_t usb_dwc2_read_int(void *handle)
  222. {
  223. uintptr_t usb_base_addr = (uintptr_t)handle;
  224. return mmio_read_32(usb_base_addr + OTG_GINTSTS) &
  225. mmio_read_32(usb_base_addr + OTG_GINTMSK);
  226. }
  227. /*
  228. * Return the USB device OUT endpoints interrupt.
  229. * handle: PCD handle.
  230. * return: Device OUT endpoint interrupts.
  231. */
  232. static uint32_t usb_dwc2_all_out_ep_int(void *handle)
  233. {
  234. uintptr_t usb_base_addr = (uintptr_t)handle;
  235. return ((mmio_read_32(usb_base_addr + OTG_DAINT) &
  236. mmio_read_32(usb_base_addr + OTG_DAINTMSK)) &
  237. OTG_DAINT_OUT_MASK) >> OTG_DAINT_OUT_SHIFT;
  238. }
  239. /*
  240. * Return the USB device IN endpoints interrupt.
  241. * handle: PCD handle.
  242. * return: Device IN endpoint interrupts.
  243. */
  244. static uint32_t usb_dwc2_all_in_ep_int(void *handle)
  245. {
  246. uintptr_t usb_base_addr = (uintptr_t)handle;
  247. return ((mmio_read_32(usb_base_addr + OTG_DAINT) &
  248. mmio_read_32(usb_base_addr + OTG_DAINTMSK)) &
  249. OTG_DAINT_IN_MASK) >> OTG_DAINT_IN_SHIFT;
  250. }
  251. /*
  252. * Return Device OUT EP interrupt register.
  253. * handle: PCD handle.
  254. * epnum: Endpoint number.
  255. * This parameter can be a value from 0 to 15.
  256. * return: Device OUT EP Interrupt register.
  257. */
  258. static uint32_t usb_dwc2_out_ep_int(void *handle, uint8_t epnum)
  259. {
  260. uintptr_t usb_base_addr = (uintptr_t)handle;
  261. return mmio_read_32(usb_base_addr + OTG_DOEP_BASE +
  262. (epnum * OTG_DOEP_SIZE) + OTG_DOEPINT) &
  263. mmio_read_32(usb_base_addr + OTG_DOEPMSK);
  264. }
  265. /*
  266. * Return Device IN EP interrupt register.
  267. * handle: PCD handle.
  268. * epnum: Endpoint number.
  269. * This parameter can be a value from 0 to 15.
  270. * return: Device IN EP Interrupt register.
  271. */
  272. static uint32_t usb_dwc2_in_ep_int(void *handle, uint8_t epnum)
  273. {
  274. uintptr_t usb_base_addr = (uintptr_t)handle;
  275. uint32_t msk;
  276. uint32_t emp;
  277. msk = mmio_read_32(usb_base_addr + OTG_DIEPMSK);
  278. emp = mmio_read_32(usb_base_addr + OTG_DIEPEMPMSK);
  279. msk |= ((emp >> epnum) << OTG_DIEPINT_TXFE_SHIFT) & OTG_DIEPINT_TXFE;
  280. return mmio_read_32(usb_base_addr + OTG_DIEP_BASE +
  281. (epnum * OTG_DIEP_SIZE) + OTG_DIEPINT) & msk;
  282. }
  283. /*
  284. * Return USB core mode.
  285. * handle: PCD handle.
  286. * return: Core mode.
  287. * This parameter can be 0 (host) or 1 (device).
  288. */
  289. static uint32_t usb_dwc2_get_mode(void *handle)
  290. {
  291. uintptr_t usb_base_addr = (uintptr_t)handle;
  292. return mmio_read_32(usb_base_addr + OTG_GINTSTS) & OTG_GINTSTS_CMOD;
  293. }
  294. /*
  295. * Activate EP0 for detup transactions.
  296. * handle: PCD handle.
  297. * return: USB status.
  298. */
  299. static enum usb_status usb_dwc2_activate_setup(void *handle)
  300. {
  301. uintptr_t usb_base_addr = (uintptr_t)handle;
  302. uintptr_t reg_offset = usb_base_addr + OTG_DIEP_BASE;
  303. /* Set the MPS of the IN EP based on the enumeration speed */
  304. mmio_clrbits_32(reg_offset + OTG_DIEPCTL, OTG_DIEPCTL_MPSIZ);
  305. if ((mmio_read_32(usb_base_addr + OTG_DSTS) & OTG_DSTS_ENUMSPD_MASK) ==
  306. OTG_DSTS_ENUMSPD_LS_PHY_6MHZ) {
  307. mmio_setbits_32(reg_offset + OTG_DIEPCTL, 3U);
  308. }
  309. mmio_setbits_32(usb_base_addr + OTG_DCTL, OTG_DCTL_CGINAK);
  310. return USBD_OK;
  311. }
  312. /*
  313. * Prepare the EP0 to start the first control setup.
  314. * handle: Selected device.
  315. * return: USB status.
  316. */
  317. static enum usb_status usb_dwc2_ep0_out_start(void *handle)
  318. {
  319. uintptr_t usb_base_addr = (uintptr_t)handle;
  320. uintptr_t reg_offset = usb_base_addr + OTG_DIEP_BASE + OTG_DIEPTSIZ;
  321. uint32_t reg_value = 0U;
  322. /* PKTCNT = 1 and XFRSIZ = 24 bytes for endpoint 0 */
  323. reg_value |= OTG_DIEPTSIZ_PKTCNT_1;
  324. reg_value |= (EP0_FIFO_SIZE & OTG_DIEPTSIZ_XFRSIZ);
  325. reg_value |= OTG_DOEPTSIZ_RXDPID_STUPCNT;
  326. mmio_write_32(reg_offset, reg_value);
  327. return USBD_OK;
  328. }
  329. /*
  330. * Write a packet into the TX FIFO associated with the EP/channel.
  331. * handle: Selected device.
  332. * src: Pointer to source buffer.
  333. * ch_ep_num: Endpoint or host channel number.
  334. * len: Number of bytes to write.
  335. * return: USB status.
  336. */
  337. static enum usb_status usb_dwc2_write_packet(void *handle, uint8_t *src,
  338. uint8_t ch_ep_num, uint16_t len)
  339. {
  340. uint32_t reg_offset;
  341. uint32_t count32b = (len + 3U) / 4U;
  342. uint32_t i;
  343. reg_offset = (uintptr_t)handle + OTG_FIFO_BASE +
  344. (ch_ep_num * OTG_FIFO_SIZE);
  345. for (i = 0U; i < count32b; i++) {
  346. uint32_t src_copy = 0U;
  347. uint32_t j;
  348. /* Data written to FIFO need to be 4 bytes aligned */
  349. for (j = 0U; j < 4U; j++) {
  350. src_copy += (*(src + j)) << (8U * j);
  351. }
  352. mmio_write_32(reg_offset, src_copy);
  353. src += 4U;
  354. }
  355. return USBD_OK;
  356. }
  357. /*
  358. * Read a packet from the RX FIFO associated with the EP/channel.
  359. * handle: Selected device.
  360. * dst: Destination pointer.
  361. * len: Number of bytes to read.
  362. * return: Pointer to destination buffer.
  363. */
  364. static void *usb_dwc2_read_packet(void *handle, uint8_t *dest, uint16_t len)
  365. {
  366. uint32_t reg_offset;
  367. uint32_t count32b = (len + 3U) / 4U;
  368. uint32_t i;
  369. VERBOSE("read packet length %i to 0x%lx\n", len, (uintptr_t)dest);
  370. reg_offset = (uintptr_t)handle + OTG_FIFO_BASE;
  371. for (i = 0U; i < count32b; i++) {
  372. *(uint32_t *)dest = mmio_read_32(reg_offset);
  373. dest += 4U;
  374. dsb();
  375. }
  376. return (void *)dest;
  377. }
  378. /*
  379. * Setup and start a transfer over an EP.
  380. * handle: Selected device
  381. * ep: Pointer to endpoint structure.
  382. * return: USB status.
  383. */
  384. static enum usb_status usb_dwc2_ep_start_xfer(void *handle, struct usbd_ep *ep)
  385. {
  386. uintptr_t usb_base_addr = (uintptr_t)handle;
  387. uint32_t reg_offset;
  388. uint32_t reg_value;
  389. uint32_t clear_value;
  390. if (ep->is_in) {
  391. reg_offset = usb_base_addr + OTG_DIEP_BASE + (ep->num * OTG_DIEP_SIZE);
  392. clear_value = OTG_DIEPTSIZ_PKTCNT | OTG_DIEPTSIZ_XFRSIZ;
  393. if (ep->xfer_len == 0U) {
  394. reg_value = OTG_DIEPTSIZ_PKTCNT_1;
  395. } else {
  396. /*
  397. * Program the transfer size and packet count
  398. * as follows:
  399. * xfersize = N * maxpacket + short_packet
  400. * pktcnt = N + (short_packet exist ? 1 : 0)
  401. */
  402. reg_value = (OTG_DIEPTSIZ_PKTCNT &
  403. (((ep->xfer_len + ep->maxpacket - 1U) /
  404. ep->maxpacket) << OTG_DIEPTSIZ_PKTCNT_SHIFT))
  405. | ep->xfer_len;
  406. if (ep->type == EP_TYPE_ISOC) {
  407. clear_value |= OTG_DIEPTSIZ_MCNT_MASK;
  408. reg_value |= OTG_DIEPTSIZ_MCNT_DATA0;
  409. }
  410. }
  411. mmio_clrsetbits_32(reg_offset + OTG_DIEPTSIZ, clear_value, reg_value);
  412. if ((ep->type != EP_TYPE_ISOC) && (ep->xfer_len > 0U)) {
  413. /* Enable the TX FIFO empty interrupt for this EP */
  414. mmio_setbits_32(usb_base_addr + OTG_DIEPEMPMSK, BIT(ep->num));
  415. }
  416. /* EP enable, IN data in FIFO */
  417. reg_value = OTG_DIEPCTL_CNAK | OTG_DIEPCTL_EPENA;
  418. if (ep->type == EP_TYPE_ISOC) {
  419. if ((mmio_read_32(usb_base_addr + OTG_DSTS) & OTG_DSTS_FNSOF0) == 0U) {
  420. reg_value |= OTG_DIEPCTL_SODDFRM;
  421. } else {
  422. reg_value |= OTG_DIEPCTL_SD0PID_SEVNFRM;
  423. }
  424. }
  425. mmio_setbits_32(reg_offset + OTG_DIEPCTL, reg_value);
  426. if (ep->type == EP_TYPE_ISOC) {
  427. usb_dwc2_write_packet(handle, ep->xfer_buff, ep->num, ep->xfer_len);
  428. }
  429. } else {
  430. reg_offset = usb_base_addr + OTG_DOEP_BASE + (ep->num * OTG_DOEP_SIZE);
  431. /*
  432. * Program the transfer size and packet count as follows:
  433. * pktcnt = N
  434. * xfersize = N * maxpacket
  435. */
  436. if (ep->xfer_len == 0U) {
  437. reg_value = ep->maxpacket | OTG_DIEPTSIZ_PKTCNT_1;
  438. } else {
  439. uint16_t pktcnt = (ep->xfer_len + ep->maxpacket - 1U) / ep->maxpacket;
  440. reg_value = (pktcnt << OTG_DIEPTSIZ_PKTCNT_SHIFT) |
  441. (ep->maxpacket * pktcnt);
  442. }
  443. mmio_clrsetbits_32(reg_offset + OTG_DOEPTSIZ,
  444. OTG_DOEPTSIZ_XFRSIZ & OTG_DOEPTSIZ_PKTCNT,
  445. reg_value);
  446. /* EP enable */
  447. reg_value = OTG_DOEPCTL_CNAK | OTG_DOEPCTL_EPENA;
  448. if (ep->type == EP_TYPE_ISOC) {
  449. if ((mmio_read_32(usb_base_addr + OTG_DSTS) & OTG_DSTS_FNSOF0) == 0U) {
  450. reg_value |= OTG_DOEPCTL_SD1PID_SODDFRM;
  451. } else {
  452. reg_value |= OTG_DOEPCTL_SD0PID_SEVNFRM;
  453. }
  454. }
  455. mmio_setbits_32(reg_offset + OTG_DOEPCTL, reg_value);
  456. }
  457. return USBD_OK;
  458. }
  459. /*
  460. * Setup and start a transfer over the EP0.
  461. * handle: Selected device.
  462. * ep: Pointer to endpoint structure.
  463. * return: USB status.
  464. */
  465. static enum usb_status usb_dwc2_ep0_start_xfer(void *handle, struct usbd_ep *ep)
  466. {
  467. uintptr_t usb_base_addr = (uintptr_t)handle;
  468. uint32_t reg_offset;
  469. uint32_t reg_value;
  470. if (ep->is_in) {
  471. reg_offset = usb_base_addr + OTG_DIEP_BASE +
  472. (ep->num * OTG_DIEP_SIZE);
  473. if (ep->xfer_len == 0U) {
  474. reg_value = OTG_DIEPTSIZ_PKTCNT_1;
  475. } else {
  476. /*
  477. * Program the transfer size and packet count
  478. * as follows:
  479. * xfersize = N * maxpacket + short_packet
  480. * pktcnt = N + (short_packet exist ? 1 : 0)
  481. */
  482. if (ep->xfer_len > ep->maxpacket) {
  483. ep->xfer_len = ep->maxpacket;
  484. }
  485. reg_value = OTG_DIEPTSIZ_PKTCNT_1 | ep->xfer_len;
  486. }
  487. mmio_clrsetbits_32(reg_offset + OTG_DIEPTSIZ,
  488. OTG_DIEPTSIZ_XFRSIZ | OTG_DIEPTSIZ_PKTCNT,
  489. reg_value);
  490. /* Enable the TX FIFO empty interrupt for this EP */
  491. if (ep->xfer_len > 0U) {
  492. mmio_setbits_32(usb_base_addr + OTG_DIEPEMPMSK,
  493. BIT(ep->num));
  494. }
  495. /* EP enable, IN data in FIFO */
  496. mmio_setbits_32(reg_offset + OTG_DIEPCTL,
  497. OTG_DIEPCTL_CNAK | OTG_DIEPCTL_EPENA);
  498. } else {
  499. reg_offset = usb_base_addr + OTG_DOEP_BASE +
  500. (ep->num * OTG_DOEP_SIZE);
  501. /*
  502. * Program the transfer size and packet count as follows:
  503. * pktcnt = N
  504. * xfersize = N * maxpacket
  505. */
  506. if (ep->xfer_len > 0U) {
  507. ep->xfer_len = ep->maxpacket;
  508. }
  509. reg_value = OTG_DIEPTSIZ_PKTCNT_1 | ep->maxpacket;
  510. mmio_clrsetbits_32(reg_offset + OTG_DIEPTSIZ,
  511. OTG_DIEPTSIZ_XFRSIZ | OTG_DIEPTSIZ_PKTCNT,
  512. reg_value);
  513. /* EP enable */
  514. mmio_setbits_32(reg_offset + OTG_DOEPCTL,
  515. OTG_DOEPCTL_CNAK | OTG_DOEPCTL_EPENA);
  516. }
  517. return USBD_OK;
  518. }
  519. /*
  520. * Set a stall condition over an EP.
  521. * handle: Selected device.
  522. * ep: Pointer to endpoint structure.
  523. * return: USB status.
  524. */
  525. static enum usb_status usb_dwc2_ep_set_stall(void *handle, struct usbd_ep *ep)
  526. {
  527. uintptr_t usb_base_addr = (uintptr_t)handle;
  528. uint32_t reg_offset;
  529. uint32_t reg_value;
  530. if (ep->is_in) {
  531. reg_offset = usb_base_addr + OTG_DIEP_BASE +
  532. (ep->num * OTG_DIEP_SIZE);
  533. reg_value = mmio_read_32(reg_offset + OTG_DIEPCTL);
  534. if ((reg_value & OTG_DIEPCTL_EPENA) == 0U) {
  535. reg_value &= ~OTG_DIEPCTL_EPDIS;
  536. }
  537. reg_value |= OTG_DIEPCTL_STALL;
  538. mmio_write_32(reg_offset + OTG_DIEPCTL, reg_value);
  539. } else {
  540. reg_offset = usb_base_addr + OTG_DOEP_BASE +
  541. (ep->num * OTG_DOEP_SIZE);
  542. reg_value = mmio_read_32(reg_offset + OTG_DOEPCTL);
  543. if ((reg_value & OTG_DOEPCTL_EPENA) == 0U) {
  544. reg_value &= ~OTG_DOEPCTL_EPDIS;
  545. }
  546. reg_value |= OTG_DOEPCTL_STALL;
  547. mmio_write_32(reg_offset + OTG_DOEPCTL, reg_value);
  548. }
  549. return USBD_OK;
  550. }
  551. /*
  552. * Stop the USB device mode.
  553. * handle: Selected device.
  554. * return: USB status.
  555. */
  556. static enum usb_status usb_dwc2_stop_device(void *handle)
  557. {
  558. uintptr_t usb_base_addr = (uintptr_t)handle;
  559. uint32_t i;
  560. /* Disable Int */
  561. mmio_clrbits_32(usb_base_addr + OTG_GAHBCFG, OTG_GAHBCFG_GINT);
  562. /* Clear pending interrupts */
  563. for (i = 0U; i < EP_NB; i++) {
  564. mmio_write_32(usb_base_addr + OTG_DIEP_BASE + (i * OTG_DIEP_SIZE) + OTG_DIEPINT,
  565. OTG_DIEPINT_MASK);
  566. mmio_write_32(usb_base_addr + OTG_DOEP_BASE + (i * OTG_DOEP_SIZE) + OTG_DOEPINT,
  567. OTG_DOEPINT_MASK);
  568. }
  569. mmio_write_32(usb_base_addr + OTG_DAINT, OTG_DAINT_IN_MASK | OTG_DAINT_OUT_MASK);
  570. /* Clear interrupt masks */
  571. mmio_write_32(usb_base_addr + OTG_DIEPMSK, 0U);
  572. mmio_write_32(usb_base_addr + OTG_DOEPMSK, 0U);
  573. mmio_write_32(usb_base_addr + OTG_DAINTMSK, 0U);
  574. /* Flush the FIFO */
  575. usb_dwc2_flush_rx_fifo(handle);
  576. usb_dwc2_flush_tx_fifo(handle, EP_ALL);
  577. /* Disconnect the USB device by disabling the pull-up/pull-down */
  578. mmio_setbits_32((uintptr_t)handle + OTG_DCTL, OTG_DCTL_SDIS);
  579. return USBD_OK;
  580. }
  581. /*
  582. * Stop the USB device mode.
  583. * handle: Selected device.
  584. * address: New device address to be assigned.
  585. * This parameter can be a value from 0 to 255.
  586. * return: USB status.
  587. */
  588. static enum usb_status usb_dwc2_set_address(void *handle, uint8_t address)
  589. {
  590. uintptr_t usb_base_addr = (uintptr_t)handle;
  591. mmio_clrsetbits_32(usb_base_addr + OTG_DCFG,
  592. OTG_DCFG_DAD,
  593. address << OTG_DCFG_DAD_SHIFT);
  594. return USBD_OK;
  595. }
  596. /*
  597. * Check FIFO for the next packet to be loaded.
  598. * handle: Selected device.
  599. * epnum : Endpoint number.
  600. * xfer_len: Block length.
  601. * xfer_count: Number of blocks.
  602. * maxpacket: Max packet length.
  603. * xfer_buff: Buffer pointer.
  604. * return: USB status.
  605. */
  606. static enum usb_status usb_dwc2_write_empty_tx_fifo(void *handle,
  607. uint32_t epnum,
  608. uint32_t xfer_len,
  609. uint32_t *xfer_count,
  610. uint32_t maxpacket,
  611. uint8_t **xfer_buff)
  612. {
  613. uintptr_t usb_base_addr = (uintptr_t)handle;
  614. uint32_t reg_offset;
  615. int32_t len;
  616. uint32_t len32b;
  617. enum usb_status ret;
  618. len = xfer_len - *xfer_count;
  619. if ((len > 0) && ((uint32_t)len > maxpacket)) {
  620. len = maxpacket;
  621. }
  622. len32b = (len + 3U) / 4U;
  623. reg_offset = usb_base_addr + OTG_DIEP_BASE + (epnum * OTG_DIEP_SIZE);
  624. while (((mmio_read_32(reg_offset + OTG_DTXFSTS) &
  625. OTG_DTXFSTS_INEPTFSAV) > len32b) &&
  626. (*xfer_count < xfer_len) && (xfer_len != 0U)) {
  627. /* Write the FIFO */
  628. len = xfer_len - *xfer_count;
  629. if ((len > 0) && ((uint32_t)len > maxpacket)) {
  630. len = maxpacket;
  631. }
  632. len32b = (len + 3U) / 4U;
  633. ret = usb_dwc2_write_packet(handle, *xfer_buff, epnum, len);
  634. if (ret != USBD_OK) {
  635. return ret;
  636. }
  637. *xfer_buff += len;
  638. *xfer_count += len;
  639. }
  640. if (len <= 0) {
  641. mmio_clrbits_32(usb_base_addr + OTG_DIEPEMPMSK, BIT(epnum));
  642. }
  643. return USBD_OK;
  644. }
  645. /*
  646. * Handle PCD interrupt request.
  647. * handle: PCD handle.
  648. * param: Pointer to information updated by the IT handling.
  649. * return: Action to do after IT handling.
  650. */
  651. static enum usb_action usb_dwc2_it_handler(void *handle, uint32_t *param)
  652. {
  653. uintptr_t usb_base_addr = (uintptr_t)handle;
  654. uint32_t ep_intr;
  655. uint32_t epint;
  656. uint32_t epnum;
  657. uint32_t temp;
  658. enum usb_status __unused ret;
  659. if (usb_dwc2_get_mode(handle) != USB_OTG_MODE_DEVICE) {
  660. return USB_NOTHING;
  661. }
  662. /* Avoid spurious interrupt */
  663. if (usb_dwc2_read_int(handle) == 0U) {
  664. return USB_NOTHING;
  665. }
  666. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_MMIS) != 0U) {
  667. /* Incorrect mode, acknowledge the interrupt */
  668. mmio_write_32(usb_base_addr + OTG_GINTSTS, OTG_GINTSTS_MMIS);
  669. }
  670. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_OEPINT) != 0U) {
  671. uint32_t reg_offset;
  672. /* Read in the device interrupt bits */
  673. ep_intr = usb_dwc2_all_out_ep_int(handle);
  674. epnum = 0U;
  675. while ((ep_intr & BIT(0)) != BIT(0)) {
  676. epnum++;
  677. ep_intr >>= 1;
  678. }
  679. reg_offset = usb_base_addr + OTG_DOEP_BASE + (epnum * OTG_DOEP_SIZE) + OTG_DOEPINT;
  680. epint = usb_dwc2_out_ep_int(handle, epnum);
  681. if ((epint & OTG_DOEPINT_XFRC) == OTG_DOEPINT_XFRC) {
  682. mmio_write_32(reg_offset, OTG_DOEPINT_XFRC);
  683. *param = epnum;
  684. return USB_DATA_OUT;
  685. }
  686. if ((epint & OTG_DOEPINT_STUP) == OTG_DOEPINT_STUP) {
  687. /* Inform that a setup packet is available */
  688. mmio_write_32(reg_offset, OTG_DOEPINT_STUP);
  689. return USB_SETUP;
  690. }
  691. if ((epint & OTG_DOEPINT_OTEPDIS) == OTG_DOEPINT_OTEPDIS) {
  692. mmio_write_32(reg_offset, OTG_DOEPINT_OTEPDIS);
  693. }
  694. }
  695. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_IEPINT) != 0U) {
  696. uint32_t reg_offset;
  697. /* Read in the device interrupt bits */
  698. ep_intr = usb_dwc2_all_in_ep_int(handle);
  699. epnum = 0U;
  700. while ((ep_intr & BIT(0)) != BIT(0)) {
  701. epnum++;
  702. ep_intr >>= 1;
  703. }
  704. reg_offset = usb_base_addr + OTG_DIEP_BASE + (epnum * OTG_DIEP_SIZE) + OTG_DIEPINT;
  705. epint = usb_dwc2_in_ep_int(handle, epnum);
  706. if ((epint & OTG_DIEPINT_XFRC) == OTG_DIEPINT_XFRC) {
  707. mmio_clrbits_32(usb_base_addr + OTG_DIEPEMPMSK, BIT(epnum));
  708. mmio_write_32(reg_offset, OTG_DIEPINT_XFRC);
  709. *param = epnum;
  710. return USB_DATA_IN;
  711. }
  712. if ((epint & OTG_DIEPINT_TOC) == OTG_DIEPINT_TOC) {
  713. mmio_write_32(reg_offset, OTG_DIEPINT_TOC);
  714. }
  715. if ((epint & OTG_DIEPINT_ITTXFE) == OTG_DIEPINT_ITTXFE) {
  716. mmio_write_32(reg_offset, OTG_DIEPINT_ITTXFE);
  717. }
  718. if ((epint & OTG_DIEPINT_INEPNE) == OTG_DIEPINT_INEPNE) {
  719. mmio_write_32(reg_offset, OTG_DIEPINT_INEPNE);
  720. }
  721. if ((epint & OTG_DIEPINT_EPDISD) == OTG_DIEPINT_EPDISD) {
  722. mmio_write_32(reg_offset, OTG_DIEPINT_EPDISD);
  723. }
  724. if ((epint & OTG_DIEPINT_TXFE) == OTG_DIEPINT_TXFE) {
  725. *param = epnum;
  726. return USB_WRITE_EMPTY;
  727. }
  728. }
  729. /* Handle resume interrupt */
  730. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_WKUPINT) != 0U) {
  731. INFO("handle USB : Resume\n");
  732. /* Clear the remote wake-up signaling */
  733. mmio_clrbits_32(usb_base_addr + OTG_DCTL, OTG_DCTL_RWUSIG);
  734. mmio_write_32(usb_base_addr + OTG_GINTSTS, OTG_GINTSTS_WKUPINT);
  735. return USB_RESUME;
  736. }
  737. /* Handle suspend interrupt */
  738. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_USBSUSP) != 0U) {
  739. INFO("handle USB : Suspend int\n");
  740. mmio_write_32(usb_base_addr + OTG_GINTSTS, OTG_GINTSTS_USBSUSP);
  741. if ((mmio_read_32(usb_base_addr + OTG_DSTS) &
  742. OTG_DSTS_SUSPSTS) == OTG_DSTS_SUSPSTS) {
  743. return USB_SUSPEND;
  744. }
  745. }
  746. /* Handle LPM interrupt */
  747. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_LPMINT) != 0U) {
  748. INFO("handle USB : LPM int enter in suspend\n");
  749. mmio_write_32(usb_base_addr + OTG_GINTSTS, OTG_GINTSTS_LPMINT);
  750. *param = (mmio_read_32(usb_base_addr + OTG_GLPMCFG) &
  751. OTG_GLPMCFG_BESL) >> 2;
  752. return USB_LPM;
  753. }
  754. /* Handle reset interrupt */
  755. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_USBRST) != 0U) {
  756. INFO("handle USB : Reset\n");
  757. mmio_clrbits_32(usb_base_addr + OTG_DCTL, OTG_DCTL_RWUSIG);
  758. usb_dwc2_flush_tx_fifo(handle, 0U);
  759. mmio_write_32(usb_base_addr + OTG_DAINT, OTG_DAINT_IN_MASK | OTG_DAINT_OUT_MASK);
  760. mmio_setbits_32(usb_base_addr + OTG_DAINTMSK, OTG_DAINT_EP0_IN | OTG_DAINT_EP0_OUT);
  761. mmio_setbits_32(usb_base_addr + OTG_DOEPMSK, OTG_DOEPMSK_STUPM |
  762. OTG_DOEPMSK_XFRCM |
  763. OTG_DOEPMSK_EPDM);
  764. mmio_setbits_32(usb_base_addr + OTG_DIEPMSK, OTG_DIEPMSK_TOM |
  765. OTG_DIEPMSK_XFRCM |
  766. OTG_DIEPMSK_EPDM);
  767. /* Set default address to 0 */
  768. mmio_clrbits_32(usb_base_addr + OTG_DCFG, OTG_DCFG_DAD);
  769. /* Setup EP0 to receive SETUP packets */
  770. ret = usb_dwc2_ep0_out_start(handle);
  771. assert(ret == USBD_OK);
  772. mmio_write_32(usb_base_addr + OTG_GINTSTS, OTG_GINTSTS_USBRST);
  773. return USB_RESET;
  774. }
  775. /* Handle enumeration done interrupt */
  776. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_ENUMDNE) != 0U) {
  777. ret = usb_dwc2_activate_setup(handle);
  778. assert(ret == USBD_OK);
  779. mmio_clrbits_32(usb_base_addr + OTG_GUSBCFG, OTG_GUSBCFG_TRDT);
  780. mmio_setbits_32(usb_base_addr + OTG_GUSBCFG,
  781. (USBD_HS_TRDT_VALUE << OTG_GUSBCFG_TRDT_SHIFT) & OTG_GUSBCFG_TRDT);
  782. mmio_write_32(usb_base_addr + OTG_GINTSTS, OTG_GINTSTS_ENUMDNE);
  783. return USB_ENUM_DONE;
  784. }
  785. /* Handle RXQLevel interrupt */
  786. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_RXFLVL) != 0U) {
  787. mmio_clrbits_32(usb_base_addr + OTG_GINTMSK,
  788. OTG_GINTSTS_RXFLVL);
  789. temp = mmio_read_32(usb_base_addr + OTG_GRXSTSP);
  790. *param = temp & OTG_GRXSTSP_EPNUM;
  791. *param |= (temp & OTG_GRXSTSP_BCNT) << (USBD_OUT_COUNT_SHIFT -
  792. OTG_GRXSTSP_BCNT_SHIFT);
  793. if (((temp & OTG_GRXSTSP_PKTSTS) >> OTG_GRXSTSP_PKTSTS_SHIFT) == STS_DATA_UPDT) {
  794. if ((temp & OTG_GRXSTSP_BCNT) != 0U) {
  795. mmio_setbits_32(usb_base_addr + OTG_GINTMSK, OTG_GINTSTS_RXFLVL);
  796. return USB_READ_DATA_PACKET;
  797. }
  798. } else if (((temp & OTG_GRXSTSP_PKTSTS) >> OTG_GRXSTSP_PKTSTS_SHIFT) ==
  799. STS_SETUP_UPDT) {
  800. mmio_setbits_32(usb_base_addr + OTG_GINTMSK, OTG_GINTSTS_RXFLVL);
  801. return USB_READ_SETUP_PACKET;
  802. }
  803. mmio_setbits_32(usb_base_addr + OTG_GINTMSK, OTG_GINTSTS_RXFLVL);
  804. }
  805. /* Handle SOF interrupt */
  806. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_SOF) != 0U) {
  807. INFO("handle USB : SOF\n");
  808. mmio_write_32(usb_base_addr + OTG_GINTSTS, OTG_GINTSTS_SOF);
  809. return USB_SOF;
  810. }
  811. /* Handle incomplete ISO IN interrupt */
  812. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_IISOIXFR) != 0U) {
  813. INFO("handle USB : ISO IN\n");
  814. mmio_write_32(usb_base_addr + OTG_GINTSTS,
  815. OTG_GINTSTS_IISOIXFR);
  816. }
  817. /* Handle incomplete ISO OUT interrupt */
  818. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_IPXFR_INCOMPISOOUT) !=
  819. 0U) {
  820. INFO("handle USB : ISO OUT\n");
  821. mmio_write_32(usb_base_addr + OTG_GINTSTS,
  822. OTG_GINTSTS_IPXFR_INCOMPISOOUT);
  823. }
  824. /* Handle connection event interrupt */
  825. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_SRQINT) != 0U) {
  826. INFO("handle USB : Connect\n");
  827. mmio_write_32(usb_base_addr + OTG_GINTSTS, OTG_GINTSTS_SRQINT);
  828. }
  829. /* Handle disconnection event interrupt */
  830. if ((usb_dwc2_read_int(handle) & OTG_GINTSTS_OTGINT) != 0U) {
  831. INFO("handle USB : Disconnect\n");
  832. temp = mmio_read_32(usb_base_addr + OTG_GOTGINT);
  833. if ((temp & OTG_GOTGINT_SEDET) == OTG_GOTGINT_SEDET) {
  834. return USB_DISCONNECT;
  835. }
  836. }
  837. return USB_NOTHING;
  838. }
  839. /*
  840. * Start the usb device mode
  841. * usb_core_handle: USB core driver handle.
  842. * return USB status.
  843. */
  844. static enum usb_status usb_dwc2_start_device(void *handle)
  845. {
  846. uintptr_t usb_base_addr = (uintptr_t)handle;
  847. mmio_clrbits_32(usb_base_addr + OTG_DCTL, OTG_DCTL_SDIS);
  848. mmio_setbits_32(usb_base_addr + OTG_GAHBCFG, OTG_GAHBCFG_GINT);
  849. return USBD_OK;
  850. }
  851. static const struct usb_driver usb_dwc2driver = {
  852. .ep0_out_start = usb_dwc2_ep0_out_start,
  853. .ep_start_xfer = usb_dwc2_ep_start_xfer,
  854. .ep0_start_xfer = usb_dwc2_ep0_start_xfer,
  855. .write_packet = usb_dwc2_write_packet,
  856. .read_packet = usb_dwc2_read_packet,
  857. .ep_set_stall = usb_dwc2_ep_set_stall,
  858. .start_device = usb_dwc2_start_device,
  859. .stop_device = usb_dwc2_stop_device,
  860. .set_address = usb_dwc2_set_address,
  861. .write_empty_tx_fifo = usb_dwc2_write_empty_tx_fifo,
  862. .it_handler = usb_dwc2_it_handler
  863. };
  864. /*
  865. * Initialize USB DWC2 driver.
  866. * usb_core_handle: USB core driver handle.
  867. * pcd_handle: PCD handle.
  868. * base_register: USB global register base address.
  869. */
  870. void stm32mp1_usb_init_driver(struct usb_handle *usb_core_handle,
  871. struct pcd_handle *pcd_handle,
  872. void *base_register)
  873. {
  874. register_usb_driver(usb_core_handle, pcd_handle, &usb_dwc2driver,
  875. base_register);
  876. }