1 /* 2 * Copyright (c) 2015-2025, STMicroelectronics - All Rights Reserved 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <stdint.h> 9 10 #include <arch_helpers.h> 11 #include <common/debug.h> 12 #include <drivers/delay_timer.h> 13 #include <drivers/st/usb_dwc3.h> 14 #include <lib/mmio.h> 15 #include <lib/utils.h> 16 #include <lib/xlat_tables/xlat_tables_v2.h> 17 18 #include "usb_dwc3_regs.h" 19 20 /* 21 * @brief USB EP Type 22 */ 23 #define EP_TYPE_CTRL 0U 24 #define EP_TYPE_ISOC 1U 25 #define EP_TYPE_BULK 2U 26 #define EP_TYPE_INTR 3U 27 #define EP_TYPE_MSK 3U 28 29 #define USB_DWC3_GLOBAL_BASE 0xc100 30 #define USB_DWC3_DEVICE_BASE 0xc700 31 32 /* Global Registers */ 33 #define DWC3_GSBUSCFG0 _DWC3_GSBUSCFG0 34 #define DWC3_GSBUSCFG1 _DWC3_GSBUSCFG1 35 #define DWC3_GTXTHRCFG _DWC3_GTXTHRCFG 36 #define DWC3_GRXTHRCFG _DWC3_GRXTHRCFG 37 #define DWC3_GCTL _DWC3_GCTL 38 #define DWC3_GEVTEN _DWC3_GEVTEN 39 #define DWC3_GSTS _DWC3_GSTS 40 #define DWC3_GUCTL1 _DWC3_GUCTL1 41 #define DWC3_GSNPSID _DWC3_GSNPSID 42 #define DWC3_GGPIO _DWC3_GGPIO 43 #define DWC3_GUID _DWC3_GUID 44 #define DWC3_GUCTL _DWC3_GUCTL 45 #define DWC3_GBUSERRADDR0 _DWC3_GBUSERRADDRLO 46 #define DWC3_GBUSERRADDR1 _DWC3_GBUSERRADDRHI 47 #define DWC3_GPRTBIMAP0 _DWC3_GPRTBIMAPLO 48 #define DWC3_GPRTBIMAP1 _DWC3_GPRTBIMAPHI 49 #define DWC3_GHWPARAMS0 _DWC3_GHWPARAMS0 50 #define DWC3_GHWPARAMS1 _DWC3_GHWPARAMS1 51 #define DWC3_GHWPARAMS2 _DWC3_GHWPARAMS2 52 #define DWC3_GHWPARAMS3 _DWC3_GHWPARAMS3 53 #define DWC3_GHWPARAMS4 _DWC3_GHWPARAMS4 54 #define DWC3_GHWPARAMS5 _DWC3_GHWPARAMS5 55 #define DWC3_GHWPARAMS6 _DWC3_GHWPARAMS6 56 #define DWC3_GHWPARAMS7 _DWC3_GHWPARAMS7 57 #define DWC3_GDBGFIFOSPACE _DWC3_GDBGFIFOSPACE 58 #define DWC3_GDBGLTSSM _DWC3_GDBGLTSSM 59 #define DWC3_GPRTBIMAP_HS0 _DWC3_GPRTBIMAP_HSLO 60 #define DWC3_GPRTBIMAP_HS1 _DWC3_GPRTBIMAP_HSHI 61 #define DWC3_GPRTBIMAP_FS0 _DWC3_GPRTBIMAP_FSLO 62 #define DWC3_GPRTBIMAP_FS1 _DWC3_GPRTBIMAP_FSHI 63 #define DWC3_GUCTL2 _DWC3_GUCTL2 64 65 #define DWC3_GUSB2PHYCFG(n) (_DWC3_GUSB2PHYCFG + (4UL * (n))) 66 #define DWC3_GUSB2I2CCTL(n) (_DWC3_GUSB2I2CCTL + (4UL * (n))) 67 68 #define DWC3_GUSB2PHYACC(n) (_DWC3_GUSB2PHYACC + (4UL * (n))) 69 70 #define DWC3_GUSB3PIPECTL(n) (_DWC3_GUSB3PIPECTL + (4UL * (n))) 71 72 #define DWC3_GTXFIFOSIZ(n) (_DWC3_GTXFIFOSIZ0 + (4UL * (n))) 73 #define DWC3_GRXFIFOSIZ(n) (_DWC3_GRXFIFOSIZ0 + (4UL * (n))) 74 75 #define DWC3_GEVNTADRLO(n) (_DWC3_GEVNTADRLO + (16UL * (n))) 76 #define DWC3_GEVNTADRHI(n) (_DWC3_GEVNTADRHI + (16UL * (n))) 77 #define DWC3_GEVNTSIZ(n) (_DWC3_GEVNTSIZ + (16UL * (n))) 78 #define DWC3_GEVNTCOUNT(n) (_DWC3_GEVNTCOUNT + (16UL * (n))) 79 80 #define DWC3_GUSB2PHYACC_ADDR(n) ((n) << USB3_GUSB2PHYACC_ULPI_REGADDR_POS) 81 #define DWC3_GUSB2PHYACC_EXTEND_ADDR(n) ((n) << USB3_GUSB2PHYACC_ULPI_EXTREGADDR_POS) 82 #define DWC3_GUSB2PHYACC_DATA(n) ((n) & USB3_GUSB2PHYACC_ULPI_REGDATA_MSK) 83 84 #define DWC3_GUCTL2_RST_ACTBITLATER _DWC3_GUCTL2_RST_ACTBITLATER 85 86 /* Device Registers */ 87 #define DWC3_DCFG _DWC3_DCFG 88 #define DWC3_DCTL _DWC3_DCTL 89 #define DWC3_DEVTEN _DWC3_DEVTEN 90 #define DWC3_DSTS _DWC3_DSTS 91 #define DWC3_DGCMDPAR _DWC3_DGCMDPAR 92 #define DWC3_DGCMD _DWC3_DGCMD 93 #define DWC3_DALEPENA _DWC3_DALEPENA 94 #define DWC3_DEPCMDPAR2(n) (_DWC3_DEPCMDPAR2 + (16UL * (n))) 95 #define DWC3_DEPCMDPAR1(n) (_DWC3_DEPCMDPAR1 + (16UL * (n))) 96 #define DWC3_DEPCMDPAR0(n) (_DWC3_DEPCMDPAR0 + (16UL * (n))) 97 #define DWC3_DEPCMD(n) (_DWC3_DEPCMD + (16UL * (n))) 98 99 /* The EP number goes 0..31 so ep0 is always out and ep1 is always in */ 100 #define DWC3_DALEPENA_EP(n) (1UL << (n)) 101 102 /* 103 * @brief USB EP Type 104 */ 105 #define EP_TYPE_CTRL 0U 106 #define EP_TYPE_ISOC 1U 107 #define EP_TYPE_BULK 2U 108 #define EP_TYPE_INTR 3U 109 #define EP_TYPE_MSK 3U 110 111 /* Event Size */ 112 #define USB_DWC3_EVENT_SIZE 4U 113 114 /* USB_ExecuteDevEPCmd::USB_DWC3_EPCmd */ 115 #define USB_DWC3_DEPCMD_DEPSTARTCFG (0x09UL << USB3_DEPCMD_CMDTYP_POS) 116 #define USB_DWC3_DEPCMD_ENDTRANSFER (0x08UL << USB3_DEPCMD_CMDTYP_POS) 117 #define USB_DWC3_DEPCMD_UPDATETRANSFER (0x07UL << USB3_DEPCMD_CMDTYP_POS) 118 #define USB_DWC3_DEPCMD_STARTTRANSFER (0x06UL << USB3_DEPCMD_CMDTYP_POS) 119 #define USB_DWC3_DEPCMD_CLEARSTALL (0x05UL << USB3_DEPCMD_CMDTYP_POS) 120 #define USB_DWC3_DEPCMD_SETSTALL (0x04UL << USB3_DEPCMD_CMDTYP_POS) 121 /* This applies for core versions 1.94a and later */ 122 #define USB_DWC3_DEPCMD_GETEPSTATE (0x03UL << USB3_DEPCMD_CMDTYP_POS) 123 #define USB_DWC3_DEPCMD_SETTRANSFRESOURCE (0x02UL << USB3_DEPCMD_CMDTYP_POS) 124 #define USB_DWC3_DEPCMD_SETEPCONFIG (0x01UL << USB3_DEPCMD_CMDTYP_POS) 125 126 /* USB_ConfigureEP::action */ 127 #define USB_DWC3_DEPCFG_ACTION_INIT 0 128 #define USB_DWC3_DEPCFG_ACTION_RESTORE 1 129 #define USB_DWC3_DEPCFG_ACTION_MODIFY 2 130 131 /* USB_ReadEndpointEventType @return EPEventType */ 132 #define USB_DWC3_DEPEVT_XFERCOMPLETE 0x01 133 #define USB_DWC3_DEPEVT_XFERINPROGRESS 0x02 134 #define USB_DWC3_DEPEVT_XFERNOTREADY 0x03 135 #define USB_DWC3_DEPEVT_RXTXFIFOEVT 0x04 136 #define USB_DWC3_DEPEVT_STREAMEVT 0x06 137 #define USB_DWC3_DEPEVT_EPCMDCMPLT 0x07 138 139 /* USB_ReadEndpointEventStatus @return EPEventStatus */ 140 #define USB_DWC3_DEPEVT_XFERNOTREADY_STATUS_CTRL_DATA 0x01 141 #define USB_DWC3_DEPEVT_XFERNOTREADY_STATUS_CTRL_STATUS 0x02 142 143 /* USB_ReadDeviceEventType @return DevEventType */ 144 #define USB_DWC3_DEVICE_EVENT_DISCONNECT 0 145 #define USB_DWC3_DEVICE_EVENT_RESET 1 146 #define USB_DWC3_DEVICE_EVENT_CONNECT_DONE 2 147 #define USB_DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE 3 148 #define USB_DWC3_DEVICE_EVENT_WAKEUP 4 149 #define USB_DWC3_DEVICE_EVENT_HIBER_REQ 5 150 #define USB_DWC3_DEVICE_EVENT_EOPF 6 151 #define USB_DWC3_DEVICE_EVENT_SOF 7 152 #define USB_DWC3_DEVICE_EVENT_L1SUSPEND 8 153 #define USB_DWC3_DEVICE_EVENT_ERRATIC_ERROR 9 154 #define USB_DWC3_DEVICE_EVENT_CMD_CMPL 10 155 #define USB_DWC3_DEVICE_EVENT_OVERFLOW 11 156 #define USB_DWC3_DEVICE_EVENT_L1WAKEUP 14 157 158 #define USB_DWC3_TRBSTS_OK 0 159 #define USB_DWC3_TRBSTS_MISSED_ISOC 1 160 #define USB_DWC3_TRBSTS_SETUP_PENDING 2 161 #define USB_DWC3_TRBSTS_XFER_IN_PROG 4 162 163 #define USB_DWC3_SPEED_SUPER 0U 164 #define USB_DWC3_SPEED_HIGH 1U 165 #define USB_DWC3_SPEED_FULL_48 2U 166 #define USB_DWC3_SPEED_FULL 3U 167 #define USB_DWC3_SPEED_LOW 4U 168 #define USB_DWC3_SPEED_INVALID 15U 169 170 #define DWC3_DEPCMD_TYPE_CONTROL 0 171 #define DWC3_DEPCMD_TYPE_ISOC 1 172 #define DWC3_DEPCMD_TYPE_BULK 2 173 #define DWC3_DEPCMD_TYPE_INTR 3 174 175 #define DWC3_GCTL_PRTCAP(n) (((n) & USB3_GCTL_PRTCAPDIR_MSK) >> USB3_GCTL_PRTCAPDIR_POS) 176 #define DWC3_GCTL_PRTCAPDIR(n) ((n) << USB3_GCTL_PRTCAPDIR_POS) 177 #define DWC3_GCTL_PRTCAP_HOST 1 178 #define DWC3_GCTL_PRTCAP_DEVICE 2 179 #define DWC3_GCTL_PRTCAP_OTG 3 180 181 #define DWC3_DSTS_SUPERSPEED (4U << USB3_DSTS_CONNECTSPD_POS) 182 #define DWC3_DSTS_HIGHSPEED (0U << USB3_DSTS_CONNECTSPD_POS) 183 #define DWC3_DSTS_FULLSPEED2 BIT(USB3_DSTS_CONNECTSPD_POS) 184 #define DWC3_DSTS_LOWSPEED (2U << USB3_DSTS_CONNECTSPD_POS) 185 #define DWC3_DSTS_FULLSPEED1 (3U << USB3_DSTS_CONNECTSPD_POS) 186 187 #define DWC3_GEVNTCOUNT_MASK 0xfffcU 188 #define DWC3_GSNPSID_MASK 0xffff0000 189 #define DWC3_GSNPSREV_MASK 0xffff 190 191 #define DWC3_DCFG_DEVADDR(addr) ((addr) << 3) 192 193 #define DWC3_EVENT_TYPE_DEV 0 194 #define DWC3_EVENT_TYPE_CARKIT 3 195 #define DWC3_EVENT_TYPE_I2C 4 196 197 #define DWC3_DEVICE_EVENT_DISCONNECT 0 198 #define DWC3_DEVICE_EVENT_RESET 1 199 #define DWC3_DEVICE_EVENT_CONNECT_DONE 2 200 #define DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE 3 201 #define DWC3_DEVICE_EVENT_WAKEUP 4 202 #define DWC3_DEVICE_EVENT_HIBER_REQ 5 203 #define DWC3_DEVICE_EVENT_EOPF 6 204 #define DWC3_DEVICE_EVENT_SOF 7 205 #define DWC3_DEVICE_EVENT_ERRATIC_ERROR 9 206 #define DWC3_DEVICE_EVENT_CMD_CMPL 10 207 #define DWC3_DEVICE_EVENT_OVERFLOW 11 208 209 /* DEPCFG parameter 1 */ 210 #define DWC3_DEPCFG_INT_NUM(n) (((n) & 0x1f) << 0) 211 #define DWC3_DEPCFG_XFER_COMPLETE_EN BIT(8) 212 #define DWC3_DEPCFG_XFER_IN_PROGRESS_EN BIT(9) 213 #define DWC3_DEPCFG_XFER_NOT_READY_EN BIT(10) 214 #define DWC3_DEPCFG_FIFO_ERROR_EN BIT(11) 215 #define DWC3_DEPCFG_STREAM_EVENT_EN BIT(13) 216 #define DWC3_DEPCFG_BINTERVAL_M1(n) (((n) & 0xffU) << 16) 217 #define DWC3_DEPCFG_STREAM_CAPABLE BIT(24) 218 #define DWC3_DEPCFG_EP_NUMBER(n) (((n) & 0x1fUL) << 25) 219 #define DWC3_DEPCFG_BULK_BASED BIT(30) 220 #define DWC3_DEPCFG_FIFO_BASED BIT(31) 221 222 /* DEPCFG parameter 0 */ 223 #define DWC3_DEPCFG_EP_TYPE(n) (((n) & 0x3UL) << 1) 224 #define DWC3_DEPCFG_MAX_PACKET_SIZE(n) (((n) & 0x7ffU) << 3) 225 #define DWC3_DEPCFG_FIFO_NUMBER(n) (((n) & 0x1fUL) << 17) 226 #define DWC3_DEPCFG_BURST_SIZE(n) (((n) & 0xf) << 22) 227 #define DWC3_DEPCFG_DATA_SEQ_NUM(n) ((n) << 26) 228 /* DEPCFG parameter 1 */ 229 #define DWC3_DEPCFG_EP_INTR_NUM(n) ((n) & 0x1fUL) 230 /* This applies for core versions earlier than 1.94a */ 231 #define DWC3_DEPCFG_IGN_SEQ_NUM BIT(31) 232 /* These apply for core versions 1.94a and later */ 233 #define DWC3_DEPCFG_ACTION_INIT (0U << 30) 234 #define DWC3_DEPCFG_ACTION_RESTORE BIT(30) 235 #define DWC3_DEPCFG_ACTION_MODIFY (2U << 30) 236 #define DWC3_DEPCFG_ACTION_VALUE(n) (((n) & 0x3) << 30) 237 238 /* DEPXFERCFG parameter 0 */ 239 #define DWC3_DEPXFERCFG_NUM_XFER_RES(n) ((n) & 0xffffU) 240 241 #define DWC3_DEPCMD_GET_RSC_IDX(x) (((x) >> USB3_DEPCMD_COMMANDPARAM_POS) & 0x7fU) 242 #define DWC3_DEPCMD_STATUS(x) (((x) & USB3_DEPCMD_CMDSTATUS_MSK) >> 12) 243 #define DWC3_DGCMD_STATUS(n) (((n) & USB3_DGCMD_CMDSTATUS_MSK) >> USB3_DGCMD_CMDSTATUS_POS) 244 245 #define DWC3_DEPCMD_PARAM(x) ((x) << USB3_DEPCMD_COMMANDPARAM_POS) 246 247 #define DWC3_LINK_STATE_SS_U0 0x00 /* in HS, means ON */ 248 #define DWC3_LINK_STATE_SS_U1 0x01 249 #define DWC3_LINK_STATE_SS_U2 0x02 /* in HS, means SLEEP */ 250 #define DWC3_LINK_STATE_SS_U3 0x03 /* in HS, means SUSPEND */ 251 #define DWC3_LINK_STATE_SS_DIS 0x04 /* Default State */ 252 #define DWC3_LINK_STATE_SS_RX_DET 0x05 /* in HS, means Early Suspend */ 253 #define DWC3_LINK_STATE_SS_INACT 0x06 254 #define DWC3_LINK_STATE_SS_POLL 0x07 255 #define DWC3_LINK_STATE_SS_RECOV 0x08 256 #define DWC3_LINK_STATE_SS_HRESET 0x09 257 #define DWC3_LINK_STATE_SS_CMPLY 0x0a 258 #define DWC3_LINK_STATE_SS_LPBK 0x0b 259 #define DWC3_LINK_STATE_SS_RESET 0x0e 260 #define DWC3_LINK_STATE_SS_RESUME 0x0f 261 262 #define DWC3_LINK_STATE_ON 0x00U /* in HS, means ON */ 263 #define DWC3_LINK_STATE_L1 0x02U /* in HS, means SLEEP */ 264 #define DWC3_LINK_STATE_L2 0x03U /* in HS, means SUSPEND */ 265 #define DWC3_LINK_STATE_DIS 0x04U /* Default State */ 266 #define DWC3_LINK_STATE_EARLY_SUS 0x05U /* in HS, means Early Suspend */ 267 #define DWC3_LINK_STATE_RESET 0x0eU 268 #define DWC3_LINK_STATE_RESUME 0x0fU 269 270 #define DWC3_DCTL_ULSTCHNG_NO_ACTION 0 271 #define DWC3_DCTL_ULSTCHNG_SS_DISABLED 4 272 #define DWC3_DCTL_ULSTCHNG_RX_DETECT 5 273 #define DWC3_DCTL_ULSTCHNG_SS_INACTIVE 6 274 #define DWC3_DCTL_ULSTCHNG_RECOVERY 8 275 #define DWC3_DCTL_ULSTCHNG_COMPLIANCE 10 276 #define DWC3_DCTL_ULSTCHNG_LOOPBACK 11 277 278 #define DWC3_DCTL_ULSTCHNGREQ(n) (((n) << USB3_DCTL_ULSTCHNGREQ_POS) & \ 279 USB3_DCTL_ULSTCHNGREQ_MSK) 280 281 #define DWC3_DSTS_USBLNKST(n) (((n) & USB3_DSTS_USBLNKST_MSK) >> USB3_DSTS_USBLNKST_POS) 282 283 /* TRB Length, PCM and Status */ 284 #define DWC3_TRB_SIZE_MASK (0x00ffffffU) 285 #define DWC3_TRB_SIZE_LENGTH(n) ((n) & DWC3_TRB_SIZE_MASK) 286 #define DWC3_TRB_SIZE_PCM1(n) (((n) & 0x03) << 24) 287 #define DWC3_TRB_SIZE_TRBSTS(n) (((n) & (0x0fU << 28)) >> 28) 288 289 #define DWC3_TRBSTS_OK 0 290 #define DWC3_TRBSTS_MISSED_ISOC 1 291 #define DWC3_TRBSTS_SETUP_PENDING 2 292 #define DWC3_TRBSTS_XFER_IN_PROG 4 293 294 /* TRB Control */ 295 #define DWC3_TRB_CTRL_HWO BIT(0) 296 #define DWC3_TRB_CTRL_LST BIT(1) 297 #define DWC3_TRB_CTRL_CHN BIT(2) 298 #define DWC3_TRB_CTRL_CSP BIT(3) 299 #define DWC3_TRB_CTRL_TRBCTL(n) (((n) & 0x3fU) << 4) 300 #define DWC3_TRB_CTRL_ISP_IMI BIT(10) 301 #define DWC3_TRB_CTRL_IOC BIT(11) 302 #define DWC3_TRB_CTRL_SID_SOFN(n) (((n) & 0xffffU) << 14) 303 304 #define DWC3_TRBCTL_NORMAL DWC3_TRB_CTRL_TRBCTL(1U) 305 #define DWC3_TRBCTL_CONTROL_SETUP DWC3_TRB_CTRL_TRBCTL(2U) 306 #define DWC3_TRBCTL_CONTROL_STATUS2 DWC3_TRB_CTRL_TRBCTL(3U) 307 #define DWC3_TRBCTL_CONTROL_STATUS3 DWC3_TRB_CTRL_TRBCTL(4U) 308 #define DWC3_TRBCTL_CONTROL_DATA DWC3_TRB_CTRL_TRBCTL(5U) 309 #define DWC3_TRBCTL_ISOCHRONOUS_FIRST DWC3_TRB_CTRL_TRBCTL(6U) 310 #define DWC3_TRBCTL_ISOCHRONOUS DWC3_TRB_CTRL_TRBCTL(7U) 311 #define DWC3_TRBCTL_LINK_TRB DWC3_TRB_CTRL_TRBCTL(8U) 312 313 #define _MASK(len, pos) GENMASK_32((len-1) + pos, pos) 314 315 /* event */ 316 #define DWC3_EVT_TYPE_EP 0x0U 317 #define DWC3_EVT_TYPE_LEN 0x1 318 #define DWC3_EVT_TYPE_BITPOS 0x0 319 #define DWC3_EVT_TYPE_MASK _MASK(DWC3_EVT_TYPE_LEN, DWC3_EVT_TYPE_BITPOS) 320 #define DWC3_EVT_TYPE_DEVSPEC 0x1U 321 #define DWC3_EVT_TYPE_NEP_TYPE_BITPOS 1 322 #define DWC3_EVT_TYPE_NEP_TYPE_LEN 7 323 #define DWC3_EVT_TYPE_NEP_TYPE_MASK _MASK(DWC3_EVT_TYPE_NEP_TYPE_LEN, \ 324 DWC3_EVT_TYPE_NEP_TYPE_BITPOS) 325 326 #define DWC3_DEPEVT_XFERCOMPLETE 0x01 327 #define DWC3_DEPEVT_XFERINPROGRESS 0x02 328 #define DWC3_DEPEVT_XFERNOTREADY 0x03 329 #define DWC3_DEPEVT_RXTXFIFOEVT 0x04 330 #define DWC3_DEPEVT_STREAMEVT 0x06 331 #define DWC3_DEPEVT_EPCMDCMPLT 0x07 332 333 #define DWC3_EVT_DEPEVT_TYPE_BITPOS 6 334 #define DWC3_EVT_DEPEVT_TYPE_LEN 4 335 #define DWC3_EVT_DEPEVT_TYPE_MASK _MASK(DWC3_EVT_DEPEVT_TYPE_LEN, DWC3_EVT_DEPEVT_TYPE_BITPOS) 336 337 #define DWC3_EVT_DEPEVT_EPNUM_BITPOS 1 338 #define DWC3_EVT_DEPEVT_EPNUM_LEN 5 339 #define DWC3_EVT_DEPEVT_EPNUM_MASK _MASK(DWC3_EVT_DEPEVT_EPNUM_LEN, \ 340 DWC3_EVT_DEPEVT_EPNUM_BITPOS) 341 342 #define DWC3_EVT_DEPEVT_STATUS_BITPOS 12 343 #define DWC3_EVT_DEPEVT_STATUS_LEN 4 344 #define DWC3_EVT_DEPEVT_STATUS_MASK _MASK(DWC3_EVT_DEPEVT_STATUS_LEN, \ 345 DWC3_EVT_DEPEVT_STATUS_BITPOS) 346 347 /* Control-only Status */ 348 #define DWC3_EVT_DEPEVT_STATUS_CONTROL_DATA 1 349 #define DWC3_EVT_DEPEVT_STATUS_CONTROL_STATUS 2 350 351 #define DWC3_EVT_DEPEVT_PARAM_BITPOS 16 352 #define DWC3_EVT_DEPEVT_PARAM_LEN 16 353 #define DWC3_EVT_DEPEVT_PARAM_MASK _MASK(DWC3_EVT_DEPEVT_PARAM_LEN, \ 354 DWC3_EVT_DEPEVT_PARAM_BITPOS) 355 356 #define DWC3_EVT_DEVEVT_TYPE_BITPOS 8 357 #define DWC3_EVT_DEVEVT_TYPE_LEN 4 358 #define DWC3_EVT_DEVEVT_TYPE_MASK _MASK(DWC3_EVT_DEVEVT_TYPE_LEN, DWC3_EVT_DEVEVT_TYPE_BITPOS) 359 360 #define DWC3_EVT_DEVEVT_LNKSTS_BITPOS 16 361 #define DWC3_EVT_DEVEVT_LNKSTS_LEN 4 362 #define DWC3_EVT_DEVEVT_LNKSTS_MASK _MASK(DWC3_EVT_DEVEVT_LNKSTS_LEN, \ 363 DWC3_EVT_DEVEVT_LNKSTS_BITPOS) 364 365 /* Bit fields for USB3_GCTL register */ 366 #define USB3_GCTL_CORESOFTRESET _DWC3_GCTL_CORESOFTRESET 367 #define USB3_GCTL_PRTCAPDIR_POS _DWC3_GCTL_PRTCAPDIR_SHIFT 368 #define USB3_GCTL_PRTCAPDIR_MSK _DWC3_GCTL_PRTCAPDIR_MASK 369 370 /* Bit fields for USB3_GUSB2PHYCFG register */ 371 #define USB3_GUSB2PHYCFG_ULPI_UTMI_SEL _DWC3_GUSB2PHYCFG_ULPI_UTMI_SEL 372 #define USB3_GUSB2PHYCFG_PHYSOFTRST _DWC3_GUSB2PHYCFG_PHYSOFTRST 373 374 /* Bit fields for USB3_GUSB3PIPECTL register */ 375 #define USB3_GUSB3PIPECTL_PHYSOFTRST _DWC3_GUSB3PIPECTL_PHYSOFTRST 376 377 /* Bit fields for USB3_GEVNTSIZ register */ 378 #define USB3_GEVNTSIZ_EVNTINTRPTMASK_MSK _DWC3_GEVNTSIZ_EVNTINTRPTMASK 379 380 /* Bit fields for USB3_DCFG register */ 381 #define USB3_DCFG_DEVSPD_POS _DWC3_DCFG_DEVSPD_SHIFT 382 #define USB3_DCFG_DEVSPD_MSK _DWC3_DCFG_DEVSPD_MASK 383 #define USB3_DCFG_DEVADDR_POS _DWC3_DCFG_DEVADDR_SHIFT 384 #define USB3_DCFG_DEVADDR_MSK _DWC3_DCFG_DEVADDR_MASK 385 #define USB3_DCFG_INTRNUM_POS _DWC3_DCFG_INTRNUM_SHIFT 386 #define USB3_DCFG_INTRNUM_MSK _DWC3_DCFG_INTRNUM_MASK 387 #define USB3_DCFG_NUMP_POS _DWC3_DCFG_NUMP_SHIFT 388 #define USB3_DCFG_NUMP_MSK _DWC3_DCFG_NUMP_MASK 389 #define USB3_DCFG_LPMCAP _DWC3_DCFG_LPMCAP 390 #define USB3_DCFG_IGNSTRMPP _DWC3_DCFG_IGNSTRMPP 391 392 /* Bit fields for USB3_DCTL register */ 393 #define USB3_DCTL_TSTCTL_POS _DWC3_DCTL_TSTCTL_SHIFT 394 #define USB3_DCTL_TSTCTL_MSK _DWC3_DCTL_TSTCTL_MASK 395 #define USB3_DCTL_ULSTCHNGREQ_POS _DWC3_DCTL_ULSTCHNGREQ_SHIFT 396 #define USB3_DCTL_ULSTCHNGREQ_MSK _DWC3_DCTL_ULSTCHNGREQ_MASK 397 #define USB3_DCTL_ACCEPTU1ENA _DWC3_DCTL_ACCEPTU1ENA 398 #define USB3_DCTL_INITU1ENA _DWC3_DCTL_INITU1ENA 399 #define USB3_DCTL_ACCEPTU2ENA _DWC3_DCTL_ACCEPTU2ENA 400 #define USB3_DCTL_INITU2ENA _DWC3_DCTL_INITU2ENA 401 #define USB3_DCTL_CSS _DWC3_DCTL_CSS 402 #define USB3_DCTL_CRS _DWC3_DCTL_CRS 403 #define USB3_DCTL_L1HIBERNATIONEN _DWC3_DCTL_L1HIBERNATIONEN 404 #define USB3_DCTL_KEEPCONNECT _DWC3_DCTL_KEEPCONNECT 405 #define USB3_DCTL_LPM_NYET_THRES_POS _DWC3_DCTL_LPM_NYET_THRES_SHIFT 406 #define USB3_DCTL_LPM_NYET_THRES_MSK _DWC3_DCTL_LPM_NYET_THRES_MASK 407 #define USB3_DCTL_HIRDTHRES_POS _DWC3_DCTL_HIRDTHRES_SHIFT 408 #define USB3_DCTL_HIRDTHRES_MSK _DWC3_DCTL_HIRDTHRES_MASK 409 #define USB3_DCTL_CSFTRST _DWC3_DCTL_CSFTRST 410 #define USB3_DCTL_RUN_STOP _DWC3_DCTL_RUN_STOP 411 412 /* Bit fields for USB3_DEVTEN register */ 413 #define USB3_DEVTEN_DISSCONNEVTEN _DWC3_DEVTEN_DISSCONNEVTEN 414 #define USB3_DEVTEN_USBRSTEVTEN _DWC3_DEVTEN_USBRSTEVTEN 415 #define USB3_DEVTEN_CONNECTDONEEVTEN _DWC3_DEVTEN_CONNECTDONEEVTEN 416 #define USB3_DEVTEN_ULSTCNGEN _DWC3_DEVTEN_ULSTCNGEN 417 #define USB3_DEVTEN_WKUPEVTEN _DWC3_DEVTEN_WKUPEVTEN 418 #define USB3_DEVTEN_HIBERNATIONREQEVTEN _DWC3_DEVTEN_HIBERNATIONREQEVTEN 419 #define USB3_DEVTEN_U3L2L1SUSPEN _DWC3_DEVTEN_U3L2L1SUSPEN 420 #define USB3_DEVTEN_SOFTEVTEN _DWC3_DEVTEN_SOFTEVTEN 421 #define USB3_DEVTEN_L1SUSPEN _DWC3_DEVTEN_L1SUSPEN 422 #define USB3_DEVTEN_ERRTICERREVTEN _DWC3_DEVTEN_ERRTICERREVTEN 423 #define USB3_DEVTEN_CMDCMPLTEN _DWC3_DEVTEN_CMDCMPLTEN 424 #define USB3_DEVTEN_EVNTOVERFLOWEN _DWC3_DEVTEN_EVNTOVERFLOWEN 425 #define USB3_DEVTEN_VENDEVTSTRCVDEN _DWC3_DEVTEN_VENDEVTSTRCVDEN 426 #define USB3_DEVTEN_L1WKUPEVTEN _DWC3_DEVTEN_L1WKUPEVTEN 427 #define USB3_DEVTEN_ECCERREN _DWC3_DEVTEN_ECCERREN 428 429 /* Bit fields for USB3_DSTS register */ 430 #define USB3_DSTS_CONNECTSPD_POS _DWC3_DSTS_CONNECTSPD_SHIFT 431 #define USB3_DSTS_CONNECTSPD_MSK _DWC3_DSTS_CONNECTSPD_MASK 432 #define USB3_DSTS_CONNECTSPD _DWC3_DSTS_CONNECTSPD_MASK 433 #define USB3_DSTS_SOFFN_POS _DWC3_DSTS_SOFFN_SHIFT 434 #define USB3_DSTS_SOFFN_MSK _DWC3_DSTS_SOFFN_MASK 435 #define USB3_DSTS_RXFIFOEMPTY _DWC3_DSTS_RXFIFOEMPTY 436 #define USB3_DSTS_USBLNKST_POS _DWC3_DSTS_USBLNKST_SHIFT 437 #define USB3_DSTS_USBLNKST_MSK _DWC3_DSTS_USBLNKST_MASK 438 #define USB3_DSTS_DEVCTRLHLT _DWC3_DSTS_DEVCTRLHLT 439 #define USB3_DSTS_COREIDLE _DWC3_DSTS_COREIDLE 440 #define USB3_DSTS_SSS _DWC3_DSTS_SSS 441 #define USB3_DSTS_RSS _DWC3_DSTS_RSS 442 #define USB3_DSTS_SRE _DWC3_DSTS_SRE 443 #define USB3_DSTS_DCNRD _DWC3_DSTS_DCNRD 444 445 /* Bit fields for USB3_DGCMD register */ 446 #define USB3_DGCMD_CMDTYP_POS _DWC3_DEPCMD_CMDTYP_SHIFT 447 #define USB3_DGCMD_CMDTYP_MSK _DWC3_DEPCMD_CMDTYP_MASK 448 #define USB3_DGCMD_CMDIOC _DWC3_DGCMD_CMDIOC 449 #define USB3_DGCMD_CMDACT _DWC3_DGCMD_CMDACT 450 #define USB3_DGCMD_CMDSTATUS_POS _DWC3_DGCMD_CMDSTATUS_SHIFT 451 #define USB3_DGCMD_CMDSTATUS_MSK _DWC3_DGCMD_CMDSTATUS_MASK 452 453 /* Bit fields for USB3_DEPCMD register */ 454 #define USB3_DEPCMD_CMDTYP_POS _DWC3_DEPCMD_CMDTYP_SHIFT 455 #define USB3_DEPCMD_CMDTYP_MSK _DWC3_DEPCMD_CMDTYP_MASK 456 #define USB3_DEPCMD_CMDTYP _DWC3_DEPCMD_CMDTYP_MASK 457 #define USB3_DEPCMD_CMDIOC _DWC3_DEPCMD_CMDIOC 458 #define USB3_DEPCMD_CMDACT _DWC3_DEPCMD_CMDACT 459 #define USB3_DEPCMD_HIPRI_FORCERM _DWC3_DEPCMD_HIPRI_FORCERM 460 #define USB3_DEPCMD_CMDSTATUS_POS _DWC3_DEPCMD_CMDSTATUS_SHIFT 461 #define USB3_DEPCMD_CMDSTATUS_MSK _DWC3_DEPCMD_CMDSTATUS_MASK 462 #define USB3_DEPCMD_COMMANDPARAM_POS _DWC3_DEPCMD_COMMANDPARAM_SHIFT 463 #define USB3_DEPCMD_COMMANDPARAM_MSK _DWC3_DEPCMD_COMMANDPARAM_MASK 464 465 /* Bit fields for USB3_DEV_IMOD register */ 466 #define USB3_DEV_IMOD_DEVICE_IMODI_POS _DWC3_DEV_IMOD_DEVICE_IMODI_SHIFT 467 #define USB3_DEV_IMOD_DEVICE_IMODI_MSK _DWC3_DEV_IMOD_DEVICE_IMODI_MASK 468 #define USB3_DEV_IMOD_DEVICE_IMODC_POS _DWC3_DEV_IMOD_DEVICE_IMODC_SHIFT 469 #define USB3_DEV_IMOD_DEVICE_IMODC_MSK _DWC3_DEV_IMOD_DEVICE_IMODC_MASK 470 471 #define IS_MULTIPLE(x, a) (((x) % (a)) == 0U) 472 #define ALIGN_MULTIPLE(x, a) ((a) * (((x) / (a)) + ((((x) % (a)) != 0U) ? 1U : 0U))) 473 #define PAGE_ALIGN(addr, mask) ((addr) & ~(mask)) 474 475 #define upper_32_bits(n) ((uint32_t)(((n) >> 16) >> 16)) 476 #define lower_32_bits(n) ((uint32_t)(n)) 477 478 /* DWC3 IP Parameter */ 479 #define DWC3_IP_DEVICE_NUM_INT 2 480 #define DWC3_IP_NUM_EPS 32U 481 #define DWC3_IP_NUM_IN_EPS 16 482 483 /* HAL_PCD defines */ 484 #define __HAL_PCD_ENABLE_INTR(__HANDLE__, intr) usb_dwc3_enable_eventint(__HANDLE__, intr) 485 #define __HAL_PCD_DISABLE_INTR(__HANDLE__, intr) usb_dwc3_disable_eventint(__HANDLE__, intr) 486 487 #define __HAL_PCD_SETUP_REQ_LEN(addr) (((uint16_t)(*((uint8_t *)(addr) + 6))) + \ 488 (((uint16_t)(*((uint8_t *)(addr) + 7))) << 8)) 489 490 #define __HAL_PCD_SETUP_REQ_DATA_DIR_IN(addr) ((*(uint8_t *)(addr)) & EP_DIR_IN) 491 492 #define __HAL_PCD_INCR_EVENT_POS(__HANDLE__, intr, incr) \ 493 (__HANDLE__)->intbuffers.evtbufferpos[intr] = \ 494 ((__HANDLE__)->intbuffers.evtbufferpos[intr] + \ 495 (incr)) % USB_DWC3_EVENT_BUFFER_SIZE 496 #define __HAL_PCD_READ_EVENT(__HANDLE__, intr) *(volatile uint32_t *)&((__HANDLE__)->\ 497 intbuffers.evtbuffer_addr[intr][(__HANDLE__)->\ 498 intbuffers.evtbufferpos[intr]]) 499 500 #define __HAL_PCD_EPADDR_TO_PHYEPNUM(ep_addr) ((2U * ((ep_addr) & ADDRESS_MASK)) + \ 501 ((((ep_addr) & EP_DIR_MASK) != 0U) ? 1U : 0U)) 502 #define __HAL_PCD_PHYEPNUM_TO_EPADDR(phy_epnum) (((phy_epnum) / 2U) | \ 503 ((((phy_epnum) & 0x1U) != 0U) ? EP_DIR_IN : 0U)) 504 505 #define PCD_DEV_EVENTS_INTR 0 /* Interrupt to use for device events */ 506 507 #define GET_DWC3EP_FROM_USBEP(__HANDLE__, usb_ep) *(volatile uint32_t *)&((__HANDLE__)->\ 508 intbuffers.evtbuffer_addr[intr][(__HANDLE__)->\ 509 intbuffers.evtbufferpos[intr]]) 510 511 typedef uint32_t dwc3_epcmd_t; 512 typedef struct { 513 uint32_t param2; 514 uint32_t param1; 515 uint32_t param0; 516 } dwc3_epcmd_params_t; 517 518 static uint32_t DWC3_regread(void *base, uint32_t offset) 519 { 520 return mmio_read_32((uintptr_t)base + offset); 521 } 522 523 static void DWC3_regwrite(void *base, uint32_t offset, uint32_t value) 524 { 525 mmio_write_32((uintptr_t)base + offset, value); 526 } 527 528 static void DWC3_regupdateset(void *base, uint32_t offset, uint32_t set_mask) 529 { 530 mmio_setbits_32((uintptr_t)base + offset, set_mask); 531 } 532 533 static void DWC3_regupdateclr(void *base, uint32_t offset, uint32_t clr_mask) 534 { 535 mmio_clrbits_32((uintptr_t)base + offset, clr_mask); 536 } 537 538 static void usb_dwc3_enable_eventint(dwc3_handle_t *handle, uint8_t intr_num) 539 { 540 DWC3_regupdateclr(handle->usb_global, DWC3_GEVNTSIZ(intr_num), 541 USB3_GEVNTSIZ_EVNTINTRPTMASK_MSK); 542 } 543 544 static void usb_dwc3_disable_eventint(dwc3_handle_t *handle, uint8_t intr_num) 545 { 546 DWC3_regupdateset(handle->usb_global, DWC3_GEVNTSIZ(intr_num), 547 USB3_GEVNTSIZ_EVNTINTRPTMASK_MSK); 548 } 549 550 static uintptr_t api_mapdmaaddr(uint8_t buf[], uint32_t size, uint8_t to_device) 551 { 552 if (to_device) { 553 clean_dcache_range((uintptr_t)buf, size); 554 } else { 555 inv_dcache_range((uintptr_t)buf, size); 556 } 557 558 return (uintptr_t)buf; 559 } 560 561 static void api_unmapdmaaddr(uintptr_t dma_addr __unused, uint32_t size __unused, 562 uint8_t to_device __unused) 563 { 564 } 565 566 static uintptr_t api_getdmaaddr(uint8_t buf[], uint32_t size __unused, uint8_t to_device __unused) 567 { 568 return (uintptr_t)buf; 569 } 570 571 __unused static void api_putdmaaddr(uintptr_t dma_addr __unused, uint32_t size __unused, 572 uint8_t to_device __unused) 573 { 574 } 575 576 static void api_memcpy(void *dest, const void *src, uint32_t n) 577 { 578 #ifdef AVOID_COMPILER_MEMCPY 579 uint8_t *pcdst = (uint8_t *)dest; 580 uint8_t const *pcsrc = (uint8_t const *)src; 581 582 while (n--) { 583 *pcdst++ = *pcsrc++; 584 } 585 #else 586 (void)memcpy(dest, src, n); 587 #endif 588 } 589 590 static uint32_t dwc3_get_trb_ctltype(uint32_t trb_type) 591 { 592 uint32_t ret; 593 594 switch (trb_type) { 595 case USB_DWC3_TRBCTL_NORMAL: 596 ret = DWC3_TRBCTL_NORMAL; 597 break; 598 case USB_DWC3_TRBCTL_CONTROL_SETUP: 599 ret = DWC3_TRBCTL_CONTROL_SETUP; 600 break; 601 case USB_DWC3_TRBCTL_CONTROL_STATUS2: 602 ret = DWC3_TRBCTL_CONTROL_STATUS2; 603 break; 604 case USB_DWC3_TRBCTL_CONTROL_STATUS3: 605 ret = DWC3_TRBCTL_CONTROL_STATUS3; 606 break; 607 case USB_DWC3_TRBCTL_CONTROL_DATA: 608 ret = DWC3_TRBCTL_CONTROL_DATA; 609 break; 610 case USB_DWC3_TRBCTL_ISOCHRONOUS_FIRST: 611 ret = DWC3_TRBCTL_ISOCHRONOUS_FIRST; 612 break; 613 case USB_DWC3_TRBCTL_ISOCHRONOUS: 614 ret = DWC3_TRBCTL_ISOCHRONOUS; 615 break; 616 case USB_DWC3_TRBCTL_LINK_TRB: 617 ret = DWC3_TRBCTL_LINK_TRB; 618 break; 619 default: 620 ret = 0U; 621 break; 622 } 623 624 return ret; 625 } 626 627 static inline const char *dwc3_get_ep_cmd_str(uint32_t cmd) 628 { 629 const char *ret; 630 631 switch (cmd & USB3_DEPCMD_CMDTYP_MSK) { 632 case USB_DWC3_DEPCMD_DEPSTARTCFG: 633 ret = "Start New Configuration"; 634 break; 635 case USB_DWC3_DEPCMD_ENDTRANSFER: 636 ret = "End Transfer"; 637 break; 638 case USB_DWC3_DEPCMD_UPDATETRANSFER: 639 ret = "Update Transfer"; 640 break; 641 case USB_DWC3_DEPCMD_STARTTRANSFER: 642 ret = "Start Transfer"; 643 break; 644 case USB_DWC3_DEPCMD_CLEARSTALL: 645 ret = "Clear Stall"; 646 break; 647 case USB_DWC3_DEPCMD_SETSTALL: 648 ret = "Set Stall"; 649 break; 650 case USB_DWC3_DEPCMD_GETEPSTATE: 651 ret = "Get Endpoint State"; 652 break; 653 case USB_DWC3_DEPCMD_SETTRANSFRESOURCE: 654 ret = "Set Endpoint Transfer Resource"; 655 break; 656 case USB_DWC3_DEPCMD_SETEPCONFIG: 657 ret = "Set Endpoint Configuration"; 658 break; 659 default: 660 ret = "UNKNOWN command"; 661 break; 662 } 663 664 return ret; 665 } 666 667 static enum usb_status dwc3_execute_dep_cmd(dwc3_handle_t *dwc3_handle, uint8_t phy_epnum, 668 dwc3_epcmd_t cmd, dwc3_epcmd_params_t *params) 669 { 670 uint32_t reg; 671 uint64_t timeout; 672 673 VERBOSE("PHYEP%d: cmd '%s' %08x params %08x %08x %08x\n", phy_epnum, 674 dwc3_get_ep_cmd_str(cmd), (uint32_t)cmd, (uint32_t)params->param0, 675 (uint32_t)params->param1, (uint32_t)params->param2); 676 677 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DEPCMDPAR0(phy_epnum), params->param0); 678 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DEPCMDPAR1(phy_epnum), params->param1); 679 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DEPCMDPAR2(phy_epnum), params->param2); 680 681 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DEPCMD(phy_epnum), cmd | USB3_DEPCMD_CMDACT); 682 683 timeout = timeout_init_us(500); /* usec */ 684 do { 685 reg = DWC3_regread(dwc3_handle->usb_device, DWC3_DEPCMD(phy_epnum)); 686 /* "Command Complete" */ 687 if ((reg & USB3_DEPCMD_CMDACT) == 0U) { 688 VERBOSE("Command Complete --> %u PHYEP%u %x\n", DWC3_DEPCMD_STATUS(reg), 689 phy_epnum, reg); 690 if (DWC3_DEPCMD_STATUS(reg) != 0U) { 691 return USBD_FAIL; 692 } 693 return USBD_OK; 694 } 695 696 /* Can be called from interrupt context hence cannot wait for Tick */ 697 if (timeout_elapsed(timeout)) { 698 ERROR("TIMEOUT Command Complete --> %u PHYEP%u %x\n", 699 DWC3_DEPCMD_STATUS(reg), phy_epnum, reg); 700 /* "Command Timed Out" */ 701 return USBD_TIMEOUT; 702 } 703 } while (true); 704 705 return USBD_OK; 706 } 707 708 static bool dwc3_is_ep_enabled(dwc3_handle_t *dwc3_handle, uint8_t phy_epnum) 709 { 710 if ((DWC3_regread(dwc3_handle->usb_device, 711 DWC3_DALEPENA) & DWC3_DALEPENA_EP(phy_epnum)) != 0U) { 712 return true; 713 } 714 715 return false; 716 } 717 718 static enum usb_status dwc3_ep_start_xfer(dwc3_handle_t *dwc3_handle, struct usbd_ep *ep) 719 { 720 enum usb_status ret; 721 dwc3_epcmd_t cmd; 722 dwc3_epcmd_params_t params; 723 724 usb_dwc3_endpoint_t *dwc3_ep = ((ep->is_in) ? &dwc3_handle->IN_ep[ep->num] : 725 &dwc3_handle->OUT_ep[ep->num]); 726 727 VERBOSE("%s PHYEP%d %x\n", __func__, dwc3_ep->phy_epnum, dwc3_ep->flags); 728 729 /* Wait for XferNotReady to get the uF interval to srat ISOC transfers */ 730 if ((ep->type == EP_TYPE_ISOC) && 731 ((dwc3_ep->flags & USB_DWC3_EP_ISOC_START_PENDING) == 0U) && 732 ((dwc3_ep->flags & USB_DWC3_EP_ISOC_STARTED) == 0U)) { 733 dwc3_ep->flags |= USB_DWC3_EP_ISOC_START_PENDING; 734 VERBOSE("INSIDE IF\n"); 735 return USBD_OK; 736 } 737 738 if ((ep->type == EP_TYPE_ISOC) && 739 ((dwc3_ep->flags & USB_DWC3_EP_ISOC_START_PENDING) != 0U)) { 740 dwc3_ep->flags &= ~USB_DWC3_EP_ISOC_START_PENDING; 741 } 742 743 dwc3_ep->trb_addr->bpl = lower_32_bits(dwc3_ep->dma_addr); 744 dwc3_ep->trb_addr->bph = upper_32_bits(dwc3_ep->dma_addr); 745 dwc3_ep->trb_addr->size = DWC3_TRB_SIZE_LENGTH(ep->xfer_len); 746 /* also initializes other bits to 0 */ 747 dwc3_ep->trb_addr->ctrl = dwc3_get_trb_ctltype(dwc3_ep->trb_flag & USB_DWC3_TRBCTL_MASK); 748 749 dwc3_ep->trb_addr->ctrl |= ((ep->num == 0U) ? DWC3_TRB_CTRL_ISP_IMI : 0U); 750 751 dwc3_ep->trb_addr->ctrl |= DWC3_TRB_CTRL_IOC; 752 753 if ((ep->type == EP_TYPE_ISOC) && ((dwc3_ep->flags & USB_DWC3_EP_ISOC_STARTED) != 0U)) { 754 cmd = USB_DWC3_DEPCMD_UPDATETRANSFER; 755 } else { 756 cmd = USB_DWC3_DEPCMD_STARTTRANSFER; 757 } 758 759 if (ep->type != EP_TYPE_ISOC) { 760 dwc3_ep->trb_addr->ctrl |= DWC3_TRB_CTRL_LST; 761 } 762 763 dwc3_ep->trb_addr->ctrl |= DWC3_TRB_CTRL_HWO; 764 765 (void)memset(¶ms, 0x00, sizeof(params)); 766 767 if ((cmd & USB3_DEPCMD_CMDTYP_MSK) == USB_DWC3_DEPCMD_STARTTRANSFER) { 768 params.param0 = upper_32_bits(dwc3_ep->trb_dma_addr); 769 params.param1 = lower_32_bits(dwc3_ep->trb_dma_addr); 770 } 771 772 dwc3_ep->flags |= USB_DWC3_EP_REQ_QUEUED; 773 if ((ep->type == EP_TYPE_ISOC) && 774 ((cmd & USB3_DEPCMD_CMDTYP_MSK) == USB_DWC3_DEPCMD_STARTTRANSFER)) { 775 dwc3_ep->flags |= USB_DWC3_EP_ISOC_STARTED; 776 } 777 778 VERBOSE("EP%d%s: trb %p:%08x:%08x:%08x:%08x length %u %d\n", 779 ep->num, ep->is_in ? "IN" : "OUT", dwc3_ep->trb_addr, 780 (uint32_t)dwc3_ep->trb_addr->bph, (uint32_t)dwc3_ep->trb_addr->bpl, 781 (uint32_t)dwc3_ep->trb_addr->size, (uint32_t)dwc3_ep->trb_addr->ctrl, 782 ep->xfer_len, dwc3_ep->flags); 783 784 ret = dwc3_execute_dep_cmd(dwc3_handle, dwc3_ep->phy_epnum, cmd, ¶ms); 785 if (ret != USBD_OK) { 786 dwc3_ep->flags &= ~USB_DWC3_EP_REQ_QUEUED; 787 if ((ep->type == EP_TYPE_ISOC) && 788 ((cmd & USB3_DEPCMD_CMDTYP_MSK) == USB_DWC3_DEPCMD_STARTTRANSFER)) { 789 dwc3_ep->flags &= ~USB_DWC3_EP_ISOC_STARTED; 790 } 791 return ret; 792 } 793 794 if ((cmd & USB3_DEPCMD_CMDTYP_MSK) == USB_DWC3_DEPCMD_STARTTRANSFER) { 795 dwc3_ep->resc_idx = 796 (uint8_t)DWC3_DEPCMD_GET_RSC_IDX(DWC3_regread(dwc3_handle->usb_device, 797 DWC3_DEPCMD(dwc3_ep->phy_epnum))); 798 } 799 800 return ret; 801 } 802 803 static enum usb_status usb_dwc3_ep_start_xfer(void *handle, struct usbd_ep *ep) 804 { 805 dwc3_handle_t *dwc3_handle = (dwc3_handle_t *)handle; 806 usb_dwc3_endpoint_t *dwc3_ep = ((ep->is_in) ? &dwc3_handle->IN_ep[ep->num] : 807 &dwc3_handle->OUT_ep[ep->num]); 808 uint32_t len = ep->xfer_len; 809 810 if (!dwc3_is_ep_enabled(dwc3_handle, dwc3_ep->phy_epnum) && ep->num != 0U) { 811 return USBD_FAIL; 812 } 813 814 if (!ep->is_in) { 815 if ((len > ep->maxpacket) && !IS_MULTIPLE(len, ep->maxpacket)) { 816 VERBOSE("Packet size exceeds Max_packet and is not an multiple of it\n"); 817 } 818 819 /*setup and start the Xfer */ 820 if ((len != 0U) && (len < ep->maxpacket)) { 821 dwc3_ep->xfer_dest_buff = ep->xfer_buff; 822 dwc3_ep->xfer_dest_len = ep->xfer_len; 823 ep->xfer_buff = dwc3_ep->bounce_buf; 824 ep->xfer_len = ALIGN_MULTIPLE(len, ep->maxpacket); 825 } else { 826 // ep->xfer_buff = pBuf; 827 ep->xfer_len = ALIGN_MULTIPLE(len, ep->maxpacket); 828 } 829 } 830 831 dwc3_ep->dma_addr = ((ep->xfer_buff != 0) ? 832 api_mapdmaaddr(ep->xfer_buff, ep->xfer_len, ep->is_in ? 1 : 0) : 833 0x0U); 834 835 if (ep->num == 0U) { 836 /* For Status ZLP packet */ 837 if (len == 0U) { 838 /* 2-stage/3-stage control transfer */ 839 if (__HAL_PCD_SETUP_REQ_LEN(dwc3_handle->setup_addr) == 0U) { 840 dwc3_ep->trb_flag = USB_DWC3_TRBCTL_CONTROL_STATUS2; 841 } else { 842 dwc3_ep->trb_flag = USB_DWC3_TRBCTL_CONTROL_STATUS3; 843 } 844 //udelay(10); 845 } else { 846 dwc3_ep->trb_flag = USB_DWC3_TRBCTL_CONTROL_DATA; 847 } 848 } else { 849 if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_BULK)) { 850 dwc3_ep->trb_flag = USB_DWC3_TRBCTL_NORMAL; 851 } else { 852 dwc3_ep->trb_flag = USB_DWC3_TRBCTL_ISOCHRONOUS_FIRST; 853 } 854 } 855 856 return dwc3_ep_start_xfer(dwc3_handle, ep); 857 } 858 859 static enum usb_status dwc3_ep0_out_start(dwc3_handle_t *dwc3_handle, uintptr_t setup_buf_dma_addr) 860 { 861 dwc3_handle->OUT_ep[0].dma_addr = setup_buf_dma_addr; 862 dwc3_handle->pcd_handle->out_ep[0].xfer_len = 8; 863 dwc3_handle->OUT_ep[0].trb_flag = USB_DWC3_TRBCTL_CONTROL_SETUP; 864 865 return dwc3_ep_start_xfer(dwc3_handle, &dwc3_handle->pcd_handle->out_ep[0]); 866 } 867 868 static enum usb_status usb_dwc3_ep0_out_start(void *handle) 869 { 870 dwc3_handle_t *dwc3_handle = (dwc3_handle_t *)handle; 871 872 dwc3_handle->EP0_State = HAL_PCD_EP0_SETUP_QUEUED; 873 return dwc3_ep0_out_start(dwc3_handle, dwc3_handle->setup_dma_addr); 874 } 875 876 static enum usb_status usb_dwc3_start_device(void *handle) 877 { 878 dwc3_handle_t *dwc3_handle = (dwc3_handle_t *)handle; 879 enum usb_status ret; 880 881 if (dwc3_handle->EP0_State != HAL_PCD_EP0_SETUP_QUEUED) { 882 dwc3_handle->EP0_State = HAL_PCD_EP0_SETUP_QUEUED; 883 884 ret = dwc3_ep0_out_start(dwc3_handle, dwc3_handle->setup_dma_addr); 885 if (ret != USBD_OK) { 886 ERROR("%s: %d\n", __func__, __LINE__); 887 return ret; 888 } 889 } 890 891 DWC3_regupdateset(dwc3_handle->usb_device, DWC3_DCTL, USB3_DCTL_RUN_STOP); 892 893 return USBD_OK; 894 } 895 896 static inline void dwc3_ack_evt_count(dwc3_handle_t *dwc3_handle, uint8_t intr_num, 897 uint32_t evt_count) 898 { 899 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GEVNTCOUNT(intr_num), evt_count); 900 } 901 902 static inline uint32_t dwc3_read_intr_count(dwc3_handle_t *dwc3_handle, uint8_t intr_num) 903 { 904 return DWC3_regread(dwc3_handle->usb_global, DWC3_GEVNTCOUNT(intr_num)) & 905 DWC3_GEVNTCOUNT_MASK; 906 } 907 908 static enum usb_status dwc3_ep_stop_xfer(dwc3_handle_t *dwc3_handle, struct usbd_ep *ep) 909 { 910 usb_dwc3_endpoint_t *dwc3_ep = ((ep->is_in) ? &dwc3_handle->IN_ep[ep->num] : 911 &dwc3_handle->OUT_ep[ep->num]); 912 enum usb_status ret; 913 dwc3_epcmd_t cmd; 914 dwc3_epcmd_params_t params; 915 916 VERBOSE("%s PHYEP%d %x\n", __func__, dwc3_ep->phy_epnum, dwc3_ep->flags); 917 918 /* Reset ISOC flags */ 919 if (ep->type == EP_TYPE_ISOC) { 920 dwc3_ep->flags &= ~(USB_DWC3_EP_ISOC_START_PENDING | USB_DWC3_EP_ISOC_STARTED); 921 } 922 923 if ((dwc3_ep->flags & USB_DWC3_EP_REQ_QUEUED) == 0U) { 924 return USBD_FAIL; 925 } 926 927 (void)memset(¶ms, 0x00, sizeof(params)); 928 cmd = USB_DWC3_DEPCMD_ENDTRANSFER | USB3_DEPCMD_HIPRI_FORCERM | USB3_DEPCMD_CMDIOC | 929 DWC3_DEPCMD_PARAM((uint32_t)dwc3_ep->resc_idx); 930 931 ret = dwc3_execute_dep_cmd(dwc3_handle, dwc3_ep->phy_epnum, cmd, ¶ms); 932 /* Need Delay 100us as mentioned in Linux Driver */ 933 udelay(100); 934 935 ep->xfer_count = ep->xfer_len - (dwc3_ep->trb_addr->size & DWC3_TRB_SIZE_MASK); 936 937 dwc3_ep->flags &= ~USB_DWC3_EP_REQ_QUEUED; 938 939 if ((!ep->is_in) && (ep->num == 0U)) { 940 dwc3_handle->EP0_State = HAL_PCD_EP0_SETUP_COMPLETED; 941 } 942 943 return ret; 944 } 945 946 static enum usb_status dwc3_ep_set_stall(dwc3_handle_t *dwc3_handle, 947 const usb_dwc3_endpoint_t *dwc3_ep) 948 { 949 dwc3_epcmd_params_t params; 950 951 (void)memset(¶ms, 0x00, sizeof(params)); 952 953 return dwc3_execute_dep_cmd(dwc3_handle, dwc3_ep->phy_epnum, USB_DWC3_DEPCMD_SETSTALL, 954 ¶ms); 955 } 956 957 static enum usb_status usb_dwc3_stop_device(void *handle) 958 { 959 dwc3_handle_t *dwc3_handle = (dwc3_handle_t *)handle; 960 uint64_t timeout; 961 uint8_t i; 962 uint32_t evtcnt; 963 enum usb_status ret; 964 965 /* 966 * Stop transfers for all(USB_DWC3_NUM_IN_EPS) EP 967 * except EP0IN k = USB_DWC3_NUM_IN_EP 968 */ 969 for (i = 0; i < USB_DWC3_NUM_IN_EP; i++) { 970 dwc3_ep_stop_xfer(dwc3_handle, &dwc3_handle->pcd_handle->in_ep[i]); 971 } 972 973 /* Stop transfers for all EP except EP0OUT k = USB_DWC3_NUM_OUT_EP */ 974 for (i = 0; i < USB_DWC3_NUM_OUT_EP; i++) { 975 dwc3_ep_stop_xfer(dwc3_handle, &dwc3_handle->pcd_handle->out_ep[i]); 976 } 977 978 /* Issue SetStall on EP0 to reset Ctrl-EP state machine */ 979 ret = dwc3_ep_set_stall(dwc3_handle, &dwc3_handle->OUT_ep[0]); 980 if (ret != USBD_OK) { 981 ERROR("%s: EP0 stall failed %u\n", __func__, ret); 982 } 983 984 /* 985 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a 986 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the 987 * "software needs to acknowledge the events that are generated 988 * (by writing to GEVNTCOUNTn) while it is waiting for this bit 989 * to be set to '1'." 990 */ 991 992 /* Check for all Event Buffer interrupt k = USB_DWC3_INT_INUSE */ 993 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 994 evtcnt = dwc3_read_intr_count(dwc3_handle, i); 995 996 if (!evtcnt) { 997 continue; 998 } 999 1000 __HAL_PCD_INCR_EVENT_POS(dwc3_handle, i, evtcnt); 1001 1002 dwc3_ack_evt_count(dwc3_handle, i, evtcnt); 1003 } 1004 1005 1006 DWC3_regupdateclr(dwc3_handle->usb_device, DWC3_DCTL, USB3_DCTL_RUN_STOP); 1007 1008 timeout = timeout_init_us(500); /* usec */ 1009 1010 while ((DWC3_regread(dwc3_handle->usb_device, DWC3_DSTS) & 1011 USB3_DSTS_DEVCTRLHLT) == 0U) { 1012 /* Can be called from interrupt context hence cannot wait for Tick */ 1013 if (timeout_elapsed(timeout)) { /* "Reset Timed Out" */ 1014 ERROR("TIMEOUT Stop Device\n"); 1015 return USBD_TIMEOUT; 1016 } 1017 } 1018 1019 // "Halt Complete" 1020 1021 return USBD_OK; 1022 } 1023 1024 static enum usb_status usb_dwc3_set_address(void *handle, uint8_t address) 1025 { 1026 dwc3_handle_t *dwc3_handle = (dwc3_handle_t *)handle; 1027 1028 VERBOSE("%s: %d\n", __func__, address); 1029 1030 /* set device address */ 1031 DWC3_regupdateclr(dwc3_handle->usb_device, DWC3_DCFG, USB3_DCFG_DEVADDR_MSK); 1032 DWC3_regupdateset(dwc3_handle->usb_device, DWC3_DCFG, DWC3_DCFG_DEVADDR((uint32_t)address)); 1033 1034 return USBD_OK; 1035 } 1036 1037 static enum usb_status usb_dwc3_ep0_start_xfer(void *handle, struct usbd_ep *ep) 1038 { 1039 return usb_dwc3_ep_start_xfer(handle, ep); 1040 } 1041 1042 static enum usb_status usb_dwc3_ep_set_stall(void *handle, struct usbd_ep *ep) 1043 { 1044 dwc3_handle_t *dwc3_handle = (dwc3_handle_t *)handle; 1045 usb_dwc3_endpoint_t *dwc3_ep = ((ep->is_in) ? &dwc3_handle->IN_ep[ep->num] : 1046 &dwc3_handle->OUT_ep[ep->num]); 1047 1048 if (dwc3_ep->is_stall) { 1049 return USBD_OK; 1050 } 1051 1052 dwc3_ep->is_stall = true; 1053 1054 return dwc3_ep_set_stall(dwc3_handle, dwc3_ep); 1055 } 1056 1057 static uint8_t dwc3_read_ep_evt_type(uint32_t event) 1058 { 1059 uint8_t ret; 1060 1061 switch ((event & DWC3_EVT_DEPEVT_TYPE_MASK) >> DWC3_EVT_DEPEVT_TYPE_BITPOS) { 1062 case DWC3_DEPEVT_XFERCOMPLETE: 1063 ret = USB_DWC3_DEPEVT_XFERCOMPLETE; 1064 break; 1065 case DWC3_DEPEVT_XFERINPROGRESS: 1066 ret = USB_DWC3_DEPEVT_XFERINPROGRESS; 1067 break; 1068 case DWC3_DEPEVT_XFERNOTREADY: 1069 ret = USB_DWC3_DEPEVT_XFERNOTREADY; 1070 break; 1071 case DWC3_DEPEVT_RXTXFIFOEVT: 1072 ret = USB_DWC3_DEPEVT_RXTXFIFOEVT; 1073 break; 1074 case DWC3_DEPEVT_STREAMEVT: 1075 ret = USB_DWC3_DEPEVT_STREAMEVT; 1076 break; 1077 case DWC3_DEPEVT_EPCMDCMPLT: 1078 ret = USB_DWC3_DEPEVT_EPCMDCMPLT; 1079 break; 1080 default: 1081 ret = (event & DWC3_EVT_DEPEVT_TYPE_MASK) >> DWC3_EVT_DEPEVT_TYPE_BITPOS; 1082 break; 1083 } 1084 1085 return ret; 1086 } 1087 1088 static uint8_t dwc3_read_ep_evt_epnum(uint32_t event) 1089 { 1090 return (uint8_t)((event & DWC3_EVT_DEPEVT_EPNUM_MASK) >> DWC3_EVT_DEPEVT_EPNUM_BITPOS); 1091 } 1092 1093 static uint8_t dwc3_read_ep_evt_status(uint32_t event) 1094 { 1095 uint8_t ret; 1096 1097 switch ((event & DWC3_EVT_DEPEVT_STATUS_MASK) >> DWC3_EVT_DEPEVT_STATUS_BITPOS) { 1098 case DWC3_EVT_DEPEVT_STATUS_CONTROL_DATA: 1099 ret = USB_DWC3_DEPEVT_XFERNOTREADY_STATUS_CTRL_DATA; 1100 break; 1101 case DWC3_EVT_DEPEVT_STATUS_CONTROL_STATUS: 1102 ret = USB_DWC3_DEPEVT_XFERNOTREADY_STATUS_CTRL_STATUS; 1103 break; 1104 default: 1105 ret = (event & DWC3_EVT_DEPEVT_STATUS_MASK) >> DWC3_EVT_DEPEVT_STATUS_BITPOS; 1106 break; 1107 } 1108 1109 return ret; 1110 } 1111 1112 static enum usb_status dwc3_epaddr_set_stall(dwc3_handle_t *dwc3_handle, uint8_t ep_addr) 1113 { 1114 struct usbd_ep *ep; 1115 enum usb_status ret; 1116 usb_dwc3_endpoint_t *dwc3_ep; 1117 1118 if ((ep_addr & EP_DIR_MASK) == EP_DIR_IN) { 1119 ep = &dwc3_handle->pcd_handle->in_ep[ep_addr & ADDRESS_MASK]; 1120 dwc3_ep = &dwc3_handle->IN_ep[ep_addr & ADDRESS_MASK]; 1121 } else { 1122 ep = &dwc3_handle->pcd_handle->out_ep[ep_addr]; 1123 dwc3_ep = &dwc3_handle->OUT_ep[ep_addr]; 1124 } 1125 1126 /* For control endpoints, the application issues only the Set Stall command, and only on the 1127 * OUT direction of the control endpoint. The controller automatically clears the STALL when 1128 * it receives a SETUP token for the endpoint. The application must not issue the Clear 1129 * Stall command on a control endpoint 1130 */ 1131 if (ep_addr == EP0_IN) { 1132 return USBD_OK; 1133 } 1134 1135 if (dwc3_ep->is_stall) { 1136 return USBD_OK; 1137 } 1138 1139 dwc3_ep->is_stall = true; 1140 ep->num = ep_addr & ADDRESS_MASK; 1141 ep->is_in = ((ep_addr & EP_DIR_MASK) == EP_DIR_IN); 1142 1143 ret = dwc3_ep_set_stall(dwc3_handle, dwc3_ep); 1144 if (ret != USBD_OK) { 1145 return ret; 1146 } 1147 1148 if ((ep_addr & ADDRESS_MASK) == 0U) { 1149 dwc3_handle->EP0_State = HAL_PCD_EP0_SETUP_QUEUED; 1150 ret = dwc3_ep0_out_start(dwc3_handle, dwc3_handle->setup_dma_addr); 1151 } 1152 1153 return ret; 1154 } 1155 1156 static uint32_t dwc3_get_ep_trblen(usb_dwc3_endpoint_t *ep) 1157 { 1158 return DWC3_TRB_SIZE_LENGTH(ep->trb_addr->size); 1159 } 1160 1161 static uint32_t dwc3_get_ep_trbstatus(usb_dwc3_endpoint_t *ep) 1162 { 1163 return DWC3_TRB_SIZE_TRBSTS(ep->trb_addr->size); 1164 } 1165 1166 static enum usb_action dwc3_handle_ep0_xfernotready_event(dwc3_handle_t *dwc3_handle, 1167 uint32_t event, uint32_t *param) 1168 { 1169 enum usb_action action = USB_NOTHING; 1170 uint8_t phy_epnum = dwc3_read_ep_evt_epnum(event); 1171 struct pcd_handle *pcd_handle = dwc3_handle->pcd_handle; 1172 uint8_t status = dwc3_read_ep_evt_status(event); 1173 uint8_t ep_addr, ep_num; 1174 enum usb_status ret; 1175 1176 ep_addr = __HAL_PCD_PHYEPNUM_TO_EPADDR(phy_epnum); 1177 ep_num = ep_addr & ADDRESS_MASK; 1178 1179 if (__HAL_PCD_SETUP_REQ_LEN(dwc3_handle->setup_addr) == 0U) { 1180 switch (status) { 1181 case USB_DWC3_DEPEVT_XFERNOTREADY_STATUS_CTRL_STATUS: 1182 api_memcpy(dwc3_handle->pcd_handle->setup, 1183 dwc3_handle->setup_addr, 1184 sizeof(dwc3_handle->pcd_handle->setup)); 1185 action = USB_SETUP; 1186 break; 1187 case USB_DWC3_DEPEVT_XFERNOTREADY_STATUS_CTRL_DATA: 1188 ret = dwc3_epaddr_set_stall(dwc3_handle, ep_num); // OUT EP0 1189 if (ret != USBD_OK) { 1190 ERROR("%s: %d\n", __func__, __LINE__); 1191 } 1192 break; 1193 default: 1194 VERBOSE("Invalid Status %d: %s: %d\n", status, __func__, __LINE__); 1195 break; 1196 } 1197 } else { 1198 switch (status) { 1199 case USB_DWC3_DEPEVT_XFERNOTREADY_STATUS_CTRL_STATUS: 1200 if ((ep_addr & EP_DIR_MASK) == EP_DIR_IN) { 1201 *param = ep_num; 1202 action = USB_DATA_OUT; 1203 } else { 1204 *param = ep_num; 1205 action = USB_DATA_IN; 1206 } 1207 break; 1208 case USB_DWC3_DEPEVT_XFERNOTREADY_STATUS_CTRL_DATA: 1209 if ((ep_addr & EP_DIR_MASK) == EP_DIR_IN) { 1210 /* if wrong direction */ 1211 if (__HAL_PCD_SETUP_REQ_DATA_DIR_IN(dwc3_handle->setup_addr) == 1212 0U) { 1213 ret = dwc3_ep_stop_xfer(dwc3_handle, 1214 &pcd_handle->out_ep[0]); 1215 if (ret != USBD_OK) { 1216 ERROR("%s: %d\n", __func__, __LINE__); 1217 } 1218 ret = dwc3_epaddr_set_stall(dwc3_handle, ep_num); // OUT EP0 1219 if (ret != USBD_OK) { 1220 ERROR("%s: %d\n", __func__, __LINE__); 1221 } 1222 } 1223 /* 1224 * last packet is MPS multiple, so send ZLP packet, 1225 * handled by USBD_core.c 1226 */ 1227 } else { 1228 /* if wrong direction */ 1229 if (__HAL_PCD_SETUP_REQ_DATA_DIR_IN(dwc3_handle->setup_addr) != 1230 0U) { 1231 ret = dwc3_ep_stop_xfer(dwc3_handle, 1232 &pcd_handle->in_ep[0]); 1233 if (ret != USBD_OK) { 1234 ERROR("%s: %d\n", __func__, __LINE__); 1235 } 1236 ret = dwc3_epaddr_set_stall(dwc3_handle, ep_num); // OUT EP0 1237 if (ret != USBD_OK) { 1238 ERROR("%s: %d\n", __func__, __LINE__); 1239 } 1240 } 1241 } 1242 break; 1243 default: 1244 VERBOSE("Invalid Status %d: %s: %d\n", status, __func__, __LINE__); 1245 break; 1246 } 1247 } 1248 1249 return action; 1250 } 1251 1252 static enum usb_action dwc3_handle_ep_event(dwc3_handle_t *dwc3_handle, uint32_t event, 1253 uint32_t *param) 1254 { 1255 enum usb_action action = USB_NOTHING; 1256 uint8_t type = dwc3_read_ep_evt_type(event); 1257 uint8_t phy_epnum = dwc3_read_ep_evt_epnum(event); 1258 uint8_t ep_addr, ep_num; 1259 struct usbd_ep *ep; 1260 usb_dwc3_endpoint_t *dwc3_ep; 1261 enum usb_status ret; 1262 1263 ep_addr = __HAL_PCD_PHYEPNUM_TO_EPADDR(phy_epnum); 1264 ep_num = ep_addr & ADDRESS_MASK; 1265 1266 if ((ep_addr & EP_DIR_MASK) == EP_DIR_IN) { 1267 ep = &dwc3_handle->pcd_handle->in_ep[ep_num]; 1268 dwc3_ep = &dwc3_handle->IN_ep[ep_num]; 1269 } else { 1270 ep = &dwc3_handle->pcd_handle->out_ep[ep_num]; 1271 dwc3_ep = &dwc3_handle->OUT_ep[ep_num]; 1272 } 1273 1274 switch (type) { 1275 case USB_DWC3_DEPEVT_XFERCOMPLETE: 1276 VERBOSE("EP%d%s: Transfer Complete Event=%x\n", ep_num, 1277 ep->is_in ? "IN" : "OUT", event); 1278 1279 if (ep->type == EP_TYPE_ISOC) { 1280 VERBOSE("EP%d%s is an Isochronous endpoint\n", ep_num, 1281 ep->is_in ? "IN" : "OUT"); 1282 break; 1283 } 1284 1285 /* Just forr Log, since ctrl expects sw to follow the whole control 1286 * programming sequence, as also mentioned in Linux driver 1287 */ 1288 if ((ep_num == 0U) && 1289 (dwc3_get_ep_trbstatus(dwc3_ep) == USB_DWC3_TRBSTS_SETUP_PENDING)) { 1290 VERBOSE("EP%d%s Setup Pending received\n", ep_num, 1291 ep->is_in ? "IN" : "OUT"); 1292 } 1293 1294 VERBOSE("EP%d%s: Transfer Complete trb %p:%08x:%08x:%08x:%08x TRB-Status %u\n", 1295 ep_num, ep->is_in ? "IN" : "OUT", dwc3_ep->trb_addr, 1296 (uint32_t)dwc3_ep->trb_addr->bph, (uint32_t)dwc3_ep->trb_addr->bpl, 1297 (uint32_t)dwc3_ep->trb_addr->size, (uint32_t)dwc3_ep->trb_addr->ctrl, 1298 dwc3_get_ep_trbstatus(dwc3_ep)); 1299 1300 if ((ep_addr & EP_DIR_MASK) == EP_DIR_IN) { /* IN EP */ 1301 ep->xfer_count = ep->xfer_len - dwc3_get_ep_trblen(dwc3_ep); 1302 1303 dwc3_ep->flags &= ~USB_DWC3_EP_REQ_QUEUED; 1304 1305 if (ep->xfer_buff != NULL) { 1306 api_unmapdmaaddr(dwc3_ep->dma_addr, ep->xfer_len, 1); 1307 } 1308 1309 /* 1310 * For EP0IN, if packet is not sent completely, 1311 * then error, as done in Linux driver 1312 */ 1313 if ((ep_num == 0U) && (ep->xfer_len != ep->xfer_count)) { 1314 ret = dwc3_epaddr_set_stall(dwc3_handle, ep_num); // OUT EP0 1315 if (ret != USBD_OK) { 1316 ERROR("%s: %d\n", __func__, __LINE__); 1317 } 1318 } 1319 1320 /* 1321 * if 3-stage then wait for XFERNOTREADY(control-status) before 1322 * sending data to upper layer 1323 */ 1324 if ((ep_num != 0U) || (ep->xfer_len == 0U)) { 1325 *param = ep_num; 1326 action = USB_DATA_IN; 1327 } 1328 1329 /* For EP0, this is ZLP, so prepare EP0 for next setup */ 1330 if ((ep_num == 0U) && (ep->xfer_len == 0U)) { 1331 /* prepare to rx more setup packets */ 1332 dwc3_handle->EP0_State = HAL_PCD_EP0_SETUP_QUEUED; 1333 1334 ret = dwc3_ep0_out_start(dwc3_handle, dwc3_handle->setup_dma_addr); 1335 if (ret != USBD_OK) { 1336 ERROR("%s: %d\n", __func__, __LINE__); 1337 return action; 1338 } 1339 } 1340 } else { 1341 ep->xfer_count = ep->xfer_len - dwc3_get_ep_trblen(dwc3_ep); 1342 1343 dwc3_ep->flags &= ~USB_DWC3_EP_REQ_QUEUED; 1344 1345 /* For EP0OUT */ 1346 if ((ep_addr == 0U) && 1347 (dwc3_handle->EP0_State == HAL_PCD_EP0_SETUP_QUEUED)) { 1348 1349 dwc3_handle->EP0_State = HAL_PCD_EP0_SETUP_COMPLETED; 1350 dwc3_ep->is_stall = false; 1351 VERBOSE("EP%d%s: SetupData %02x%02x%02x%02x:%02x%02x%02x%02x\n", 1352 ep_num, ep->is_in ? "IN" : "OUT", 1353 dwc3_handle->setup_addr[0], dwc3_handle->setup_addr[1], 1354 dwc3_handle->setup_addr[2], dwc3_handle->setup_addr[3], 1355 dwc3_handle->setup_addr[4], dwc3_handle->setup_addr[5], 1356 dwc3_handle->setup_addr[6], dwc3_handle->setup_addr[7]); 1357 1358 // if 2-stage then wait for the XFERNOTREADY(control-status) 1359 if (__HAL_PCD_SETUP_REQ_LEN(dwc3_handle->setup_addr) != 0U) { 1360 api_memcpy(dwc3_handle->pcd_handle->setup, 1361 dwc3_handle->setup_addr, 1362 sizeof(dwc3_handle->pcd_handle->setup)); 1363 action = USB_SETUP; 1364 } 1365 } else { 1366 1367 if (ep->xfer_buff != NULL) { 1368 api_unmapdmaaddr(dwc3_ep->dma_addr, ep->xfer_len, 0); 1369 } 1370 1371 if (ep->xfer_buff == dwc3_ep->bounce_buf) { 1372 api_memcpy(dwc3_ep->xfer_dest_buff, ep->xfer_buff, 1373 ep->xfer_count); 1374 ep->xfer_buff = dwc3_ep->xfer_dest_buff; 1375 } 1376 1377 /* 1378 * if 3-stage then wait for XFERNOTREADY(control-status) before 1379 * sending data to upper layer 1380 */ 1381 if ((ep_num != 0U) || (ep->xfer_len == 0U)) { 1382 *param = ep_num; 1383 action = USB_DATA_OUT; 1384 } 1385 1386 /* For EP0, this is ZLP, so prepare EP0 for next setup */ 1387 if ((ep_num == 0U) && (ep->xfer_len == 0U)) { 1388 /* prepare to rx more setup packets */ 1389 dwc3_handle->EP0_State = HAL_PCD_EP0_SETUP_QUEUED; 1390 1391 ret = dwc3_ep0_out_start(dwc3_handle, 1392 dwc3_handle->setup_dma_addr); 1393 if (ret != USBD_OK) { 1394 ERROR("%s: %d\n", __func__, __LINE__); 1395 return action; 1396 } 1397 } 1398 } 1399 } 1400 1401 break; 1402 1403 case USB_DWC3_DEPEVT_XFERINPROGRESS: 1404 VERBOSE("EP%d%s: Transfer In-Progress %u Event=%x\n", ep_num, 1405 ep->is_in ? "IN" : "OUT", dwc3_handle->intbuffers.evtbufferpos[0], 1406 event); 1407 1408 if (ep_num == 0U) { // ***ToCheck 1409 break; 1410 } 1411 1412 /* 1413 * currently not planned to issue multiple TRB transfer, hence there shouldn't 1414 * be any event like this 1415 */ 1416 1417 /* 1418 * But for isoc transfers we need get it since transfer started with 1419 * loop(link) trb 1420 */ 1421 if (ep->type != EP_TYPE_ISOC) { 1422 break; 1423 } 1424 1425 break; 1426 1427 case USB_DWC3_DEPEVT_XFERNOTREADY: 1428 VERBOSE("EP%d%s: Transfer Not Ready Event=%x\n", ep_num, 1429 ep->is_in ? "IN" : "OUT", event); 1430 /* For EP0IN or EP0OUT, currently no plan to use xfernotready interrupt */ 1431 1432 //active = PCD_READ_DEPEVT_STATUS(event); 1433 1434 if (ep_num == 0U) { 1435 1436 if (dwc3_handle->EP0_State == HAL_PCD_EP0_SETUP_QUEUED) { 1437 ret = dwc3_epaddr_set_stall(dwc3_handle, ep_num); // OUT EP0 1438 if (ret != USBD_OK) { 1439 ERROR("%s: %d\n", __func__, __LINE__); 1440 return action; 1441 } 1442 } else { 1443 action = dwc3_handle_ep0_xfernotready_event(dwc3_handle, event, 1444 param); 1445 } 1446 } 1447 1448 /* 1449 * Currently we dont implement provide request queue to support On-demand transfers 1450 * for non-isoc EP, it only supports preset transfers so this event will not be 1451 * used, Section 9.2.4 "Transfer Setup Recommendations" 1452 */ 1453 1454 /* 1455 * But for isoc transfers we need to issue the request from here using the 1456 * micro-frame number 1457 */ 1458 if (ep->type != EP_TYPE_ISOC) { 1459 break; 1460 } 1461 1462 break; 1463 1464 case USB_DWC3_DEPEVT_STREAMEVT: /* Stream event, Not used */ 1465 break; 1466 case USB_DWC3_DEPEVT_RXTXFIFOEVT: /* FIFO Overrun */ 1467 break; 1468 case USB_DWC3_DEPEVT_EPCMDCMPLT: /* Endpoint Command Complete */ 1469 VERBOSE("EP%d%s: Endpoint Command Complete Event=%x\n", ep_num, 1470 ep->is_in ? "IN" : "OUT", event); 1471 break; 1472 default: 1473 VERBOSE("Invalid type %d: %s: %d\n", type, __func__, __LINE__); 1474 break; 1475 } 1476 1477 return action; 1478 } 1479 1480 static uint8_t dwc3_read_dev_evt_type(uint32_t event) 1481 { 1482 uint8_t ret; 1483 1484 switch ((event & DWC3_EVT_DEVEVT_TYPE_MASK) >> DWC3_EVT_DEVEVT_TYPE_BITPOS) { 1485 case DWC3_DEVICE_EVENT_DISCONNECT: 1486 ret = USB_DWC3_DEVICE_EVENT_DISCONNECT; 1487 break; 1488 case DWC3_DEVICE_EVENT_RESET: 1489 ret = USB_DWC3_DEVICE_EVENT_RESET; 1490 break; 1491 case DWC3_DEVICE_EVENT_CONNECT_DONE: 1492 ret = USB_DWC3_DEVICE_EVENT_CONNECT_DONE; 1493 break; 1494 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: 1495 ret = USB_DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE; 1496 break; 1497 case DWC3_DEVICE_EVENT_WAKEUP: 1498 ret = USB_DWC3_DEVICE_EVENT_WAKEUP; 1499 break; 1500 case DWC3_DEVICE_EVENT_HIBER_REQ: 1501 ret = USB_DWC3_DEVICE_EVENT_HIBER_REQ; 1502 break; 1503 case DWC3_DEVICE_EVENT_EOPF: 1504 ret = USB_DWC3_DEVICE_EVENT_EOPF; 1505 break; 1506 case DWC3_DEVICE_EVENT_SOF: 1507 ret = USB_DWC3_DEVICE_EVENT_SOF; 1508 break; 1509 case DWC3_DEVICE_EVENT_ERRATIC_ERROR: 1510 ret = USB_DWC3_DEVICE_EVENT_ERRATIC_ERROR; 1511 break; 1512 case DWC3_DEVICE_EVENT_CMD_CMPL: 1513 ret = USB_DWC3_DEVICE_EVENT_CMD_CMPL; 1514 break; 1515 case DWC3_DEVICE_EVENT_OVERFLOW: 1516 ret = USB_DWC3_DEVICE_EVENT_OVERFLOW; 1517 break; 1518 default: 1519 ret = 0; 1520 break; 1521 } 1522 1523 return ret; 1524 } 1525 1526 #if (USB_DWC3_NUM_IN_EP > 1) || (USB_DWC3_NUM_OUT_EP > 1) 1527 static enum usb_status dwc3_ep_clear_stall(dwc3_handle_t *dwc3_handle, usb_dwc3_endpoint_t *dwc3_ep) 1528 { 1529 dwc3_epcmd_params_t params; 1530 1531 (void)memset(¶ms, 0x00, sizeof(params)); 1532 1533 return dwc3_execute_dep_cmd(dwc3_handle, dwc3_ep->phy_epnum, USB_DWC3_DEPCMD_CLEARSTALL, 1534 ¶ms); 1535 } 1536 #endif 1537 1538 static uint8_t dwc3_get_dev_speed(dwc3_handle_t *dwc3_handle) 1539 { 1540 uint32_t reg; 1541 uint8_t ret; 1542 1543 reg = DWC3_regread(dwc3_handle->usb_device, DWC3_DSTS) & USB3_DSTS_CONNECTSPD; 1544 1545 switch (reg) { 1546 case DWC3_DSTS_SUPERSPEED: 1547 ret = USB_DWC3_SPEED_SUPER; 1548 INFO("%s = SuperSpeed\n", __func__); 1549 break; 1550 case DWC3_DSTS_HIGHSPEED: 1551 ret = USB_DWC3_SPEED_HIGH; 1552 INFO("%s = HighSpeed\n", __func__); 1553 break; 1554 case DWC3_DSTS_FULLSPEED1: 1555 ret = USB_DWC3_SPEED_FULL_48; 1556 INFO("%s = FullSpeed_48M\n", __func__); 1557 break; 1558 case DWC3_DSTS_FULLSPEED2: 1559 ret = USB_DWC3_SPEED_FULL; 1560 INFO("%s = FullSpeed\n", __func__); 1561 break; 1562 case DWC3_DSTS_LOWSPEED: 1563 ret = USB_DWC3_SPEED_LOW; 1564 INFO("%s = LowSpeed\n", __func__); 1565 break; 1566 default: /* Invalid */ 1567 ret = USB_DWC3_SPEED_INVALID; 1568 INFO("%s = Invalid\n", __func__); 1569 break; 1570 } 1571 1572 return ret; 1573 } 1574 1575 static uint32_t dwc3_get_epcfg_action(uint8_t action) 1576 { 1577 uint32_t ret; 1578 1579 switch (action) { 1580 case USB_DWC3_DEPCFG_ACTION_INIT: 1581 ret = DWC3_DEPCFG_ACTION_INIT; 1582 break; 1583 case USB_DWC3_DEPCFG_ACTION_RESTORE: 1584 ret = DWC3_DEPCFG_ACTION_RESTORE; 1585 break; 1586 case USB_DWC3_DEPCFG_ACTION_MODIFY: 1587 ret = DWC3_DEPCFG_ACTION_MODIFY; 1588 break; 1589 default: 1590 ret = 0U; 1591 break; 1592 } 1593 1594 return ret; 1595 } 1596 1597 static enum usb_status dwc3_ep_configure(dwc3_handle_t *dwc3_handle, uint8_t epnum, bool is_in, 1598 uint8_t type, uint32_t max_packet, uint8_t tx_fifo, 1599 uint16_t binterval, uint8_t phy_epnum, uint8_t intr_num, 1600 uint8_t action) 1601 { 1602 dwc3_epcmd_params_t params; 1603 1604 (void)memset(¶ms, 0x00, sizeof(params)); 1605 params.param0 = DWC3_DEPCFG_EP_TYPE(type) | DWC3_DEPCFG_MAX_PACKET_SIZE(max_packet); 1606 1607 /* Burst size is only needed in SuperSpeed mode */ 1608 //params.param0 |= DWC3_DEPCFG_BURST_SIZE(0); 1609 1610 params.param0 |= dwc3_get_epcfg_action(action); 1611 1612 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN | DWC3_DEPCFG_XFER_NOT_READY_EN; 1613 if (type == EP_TYPE_ISOC) { 1614 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; 1615 } 1616 1617 params.param1 |= DWC3_DEPCFG_EP_NUMBER(((uint32_t)epnum << 1) + (is_in ? 1UL : 0UL)); 1618 1619 params.param1 |= DWC3_DEPCFG_EP_INTR_NUM(intr_num); 1620 1621 if (is_in) { 1622 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(tx_fifo); 1623 } 1624 1625 if (binterval != 0U) { 1626 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(binterval - 1UL); 1627 } 1628 1629 return dwc3_execute_dep_cmd(dwc3_handle, phy_epnum, USB_DWC3_DEPCMD_SETEPCONFIG, ¶ms); 1630 } 1631 1632 static inline uint8_t dwc3_read_dev_evt_linkstate(uint32_t event) 1633 { 1634 return (uint8_t)((event & DWC3_EVT_DEVEVT_LNKSTS_MASK) >> DWC3_EVT_DEVEVT_LNKSTS_BITPOS); 1635 } 1636 1637 static inline const char *dwc3_get_linkstate_str(uint8_t state) 1638 { 1639 const char *ret; 1640 1641 switch (state) { 1642 case DWC3_LINK_STATE_ON: 1643 ret = "ON"; 1644 break; 1645 case DWC3_LINK_STATE_L1: 1646 ret = "L1"; 1647 break; 1648 case DWC3_LINK_STATE_L2: 1649 ret = "L2"; 1650 break; 1651 case DWC3_LINK_STATE_DIS: 1652 ret = "Disconnect"; 1653 break; 1654 case DWC3_LINK_STATE_EARLY_SUS: 1655 ret = "Early Suspend"; 1656 break; 1657 case DWC3_LINK_STATE_RESET: 1658 ret = "Reset"; 1659 break; 1660 case DWC3_LINK_STATE_RESUME: 1661 ret = "Resume"; 1662 break; 1663 default: 1664 ret = "UNKNOWN state"; 1665 break; 1666 } 1667 1668 return ret; 1669 } 1670 1671 static enum usb_action dwc3_handle_dev_event(dwc3_handle_t *dwc3_handle, uint32_t event, 1672 uint32_t *param) 1673 { 1674 enum usb_action action = USB_NOTHING; 1675 uint8_t type = dwc3_read_dev_evt_type(event); 1676 uint8_t i, speed; 1677 enum usb_status ret; 1678 uint32_t ep0_mps = USB3_MAX_PACKET_SIZE; 1679 1680 switch (type) { 1681 case USB_DWC3_DEVICE_EVENT_DISCONNECT: 1682 INFO("Event: Device Disconnect\n"); 1683 /* For SuperSpeed, set DCTL[8:5] to 5*/ 1684 1685 action = USB_DISCONNECT; 1686 break; 1687 1688 case USB_DWC3_DEVICE_EVENT_RESET: 1689 INFO("Event: Device Reset\n"); 1690 /* 1691 * Spec says, Wait till EP0 finishes current transfer and reaches Setup-Stage. 1692 * But there is no wait in Linux driver which means that Host will take care while 1693 * issuing a reset. Also its tricky to implement since we can't wait here 1694 * (in interrupt context) for further control EP interrupts 1695 */ 1696 1697 if (dwc3_handle->EP0_State != HAL_PCD_EP0_SETUP_QUEUED) { 1698 ret = dwc3_ep_stop_xfer(dwc3_handle, &dwc3_handle->pcd_handle->out_ep[0]); 1699 if (ret != USBD_OK) { 1700 ERROR("%s: %d\n", __func__, __LINE__); 1701 } 1702 ret = dwc3_epaddr_set_stall(dwc3_handle, EP0_OUT); // OUT EP0 1703 if (ret != USBD_OK) { 1704 ERROR("%s: %d\n", __func__, __LINE__); 1705 } 1706 } 1707 1708 /* 1709 * Stop transfers for all(USB_DWC3_NUM_IN_EPS) EP 1710 * except EP0IN k = USB_DWC3_NUM_IN_EP 1711 */ 1712 for (i = 0; i < USB_DWC3_NUM_IN_EP; i++) { 1713 dwc3_ep_stop_xfer(dwc3_handle, 1714 &dwc3_handle->pcd_handle->in_ep[i]); 1715 } 1716 1717 #if USB_DWC3_NUM_OUT_EP > 1 1718 /* Stop transfers for all EP except EP0OUT k = USB_DWC3_NUM_OUT_EP */ 1719 for (i = 1; i < USB_DWC3_NUM_OUT_EP; i++) { 1720 dwc3_ep_stop_xfer(dwc3_handle, 1721 &dwc3_handle->pcd_handle->out_ep[i]); 1722 } 1723 #endif 1724 1725 #if USB_DWC3_NUM_IN_EP > 1 1726 /* Clear Stall for all EP except EP0IN k = USB_DWC3_NUM_IN_EP */ 1727 for (i = 1; i < USB_DWC3_NUM_IN_EP; i++) { 1728 usb_dwc3_endpoint_t *ep = &dwc3_handle->IN_ep[i]; 1729 1730 if (!ep->is_stall) { 1731 continue; 1732 } 1733 1734 ep->is_stall = false; 1735 1736 ret = dwc3_ep_clear_stall(dwc3_handle, ep); 1737 if (ret != USBD_OK) { 1738 ERROR("%s: %d\n", __func__, __LINE__); 1739 return action; 1740 } 1741 } 1742 #endif 1743 1744 #if USB_DWC3_NUM_OUT_EP > 1 1745 /* Clear Stall for all EP except EP0OUT k = USB_DWC3_NUM_OUT_EP */ 1746 for (i = 1; i < USB_DWC3_NUM_OUT_EP; i++) { 1747 usb_dwc3_endpoint_t *ep = &dwc3_handle->OUT_ep[i]; 1748 1749 if (!ep->is_stall) { 1750 continue; 1751 } 1752 1753 ep->is_stall = false; 1754 //ep->num = ep_addr & ADDRESS_MASK; 1755 //ep->is_in = ((ep_addr & 0x80) == 0x80); 1756 1757 ret = dwc3_ep_clear_stall(dwc3_handle, ep); 1758 if (ret != USBD_OK) { 1759 ERROR("%s: %d\n", __func__, __LINE__); 1760 return action; 1761 } 1762 } 1763 #endif 1764 1765 /* Reset device address to zero */ 1766 ret = usb_dwc3_set_address(dwc3_handle, 0); 1767 if (ret != USBD_OK) { 1768 ERROR("%s: %d\n", __func__, __LINE__); 1769 return action; 1770 } 1771 1772 action = USB_RESET; 1773 1774 break; 1775 1776 case USB_DWC3_DEVICE_EVENT_CONNECT_DONE: 1777 INFO("Event: Device Connection Done\n"); 1778 1779 /* 1780 * hpcd->Init.ep0_mps = MaxPacketSize to 512 (SuperSpeed), 64 (High-Speed), 1781 * 8/16/32/64 (Full-Speed), or 8 (Low-Speed). 1782 */ 1783 speed = dwc3_get_dev_speed(dwc3_handle); 1784 switch (speed) { 1785 case USB_DWC3_SPEED_SUPER: 1786 ep0_mps = 512; 1787 break; 1788 case USB_DWC3_SPEED_HIGH: 1789 ep0_mps = 64; 1790 break; 1791 case USB_DWC3_SPEED_FULL_48: 1792 case USB_DWC3_SPEED_FULL: 1793 ep0_mps = 64; 1794 break; 1795 case USB_DWC3_SPEED_LOW: 1796 ep0_mps = 8; 1797 break; 1798 default: 1799 ERROR("%s: %d\n", __func__, __LINE__); 1800 break; 1801 } 1802 1803 /* Update DWC3_GCTL_RAMCLKSEL, but only for Superspeed */ 1804 1805 /* Modify max packet size for EP 0 & 1 */ 1806 ret = dwc3_ep_configure(dwc3_handle, 0, false, EP_TYPE_CTRL, ep0_mps, 0, 0, 0, 1807 dwc3_handle->OUT_ep[0].intr_num, 1808 USB_DWC3_DEPCFG_ACTION_MODIFY); 1809 if (ret != USBD_OK) { 1810 ERROR("%s: %d\n", __func__, __LINE__); 1811 return action; 1812 } 1813 ret = dwc3_ep_configure(dwc3_handle, 0, true, EP_TYPE_CTRL, ep0_mps, 0, 0, 1, 1814 dwc3_handle->IN_ep[0].intr_num, 1815 USB_DWC3_DEPCFG_ACTION_MODIFY); 1816 if (ret != USBD_OK) { 1817 ERROR("%s: %d\n", __func__, __LINE__); 1818 return action; 1819 } 1820 1821 dwc3_handle->pcd_handle->out_ep[0].maxpacket = ep0_mps; 1822 dwc3_handle->pcd_handle->in_ep[0].maxpacket = ep0_mps; 1823 1824 /* 1825 * Depending on the connected speed, write to the other 1826 * PHY's control register to suspend it. 1827 */ 1828 1829 /* Use default values for GTXFIFOSIZn and GRXFIFOSIZ0 */ 1830 1831 action = USB_ENUM_DONE; 1832 1833 break; 1834 1835 case USB_DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: 1836 INFO("Event: Link Status Change : %s(%d)\n", 1837 dwc3_get_linkstate_str(dwc3_read_dev_evt_linkstate(event)), 1838 dwc3_read_dev_evt_linkstate(event)); 1839 /* Handle link state changes, U0,U1,U2,U3 with suspend/resume functions */ 1840 1841 break; 1842 1843 case USB_DWC3_DEVICE_EVENT_WAKEUP: 1844 INFO("Event: Wake-UP\n"); 1845 // resume function 1846 action = USB_RESUME; 1847 break; 1848 1849 case USB_DWC3_DEVICE_EVENT_HIBER_REQ: 1850 INFO("Event: Hibernation Request\n"); 1851 /* hibernation function */ 1852 break; 1853 1854 case USB_DWC3_DEVICE_EVENT_EOPF: /* End of Periodic Frame */ 1855 INFO("Event: Suspend\n"); 1856 action = USB_SUSPEND; 1857 break; 1858 1859 case USB_DWC3_DEVICE_EVENT_SOF: /* Start of Periodic Frame */ 1860 INFO("Event: Start of Periodic Frame\n"); 1861 break; 1862 1863 case USB_DWC3_DEVICE_EVENT_L1SUSPEND: 1864 INFO("Event: L1 Suspend\n"); 1865 action = USB_LPM; 1866 break; 1867 1868 case USB_DWC3_DEVICE_EVENT_ERRATIC_ERROR: /* Erratic Error */ 1869 INFO("Event: Erratic Error\n"); 1870 break; 1871 1872 case USB_DWC3_DEVICE_EVENT_CMD_CMPL: /* Command Complete */ 1873 INFO("Event: Command Complete\n"); 1874 break; 1875 1876 case USB_DWC3_DEVICE_EVENT_OVERFLOW: /* Overflow */ 1877 INFO("Event: Overflow\n"); 1878 break; 1879 1880 case USB_DWC3_DEVICE_EVENT_L1WAKEUP: 1881 INFO("Event: L1 Wake-UP\n"); 1882 // resume function 1883 action = USB_RESUME; 1884 break; 1885 1886 default: /* UNKNOWN IRQ type */ 1887 break; 1888 } 1889 1890 return action; 1891 } 1892 1893 static inline bool dwc3_is_ep_event(uint32_t event) 1894 { 1895 return ((event & DWC3_EVT_TYPE_MASK) == (DWC3_EVT_TYPE_EP << DWC3_EVT_TYPE_BITPOS)); 1896 } 1897 1898 static inline bool dwc3_is_dev_event(uint32_t event) 1899 { 1900 return ((event & DWC3_EVT_TYPE_MASK) == (DWC3_EVT_TYPE_DEVSPEC << DWC3_EVT_TYPE_BITPOS)); 1901 } 1902 1903 static enum usb_action usb_dwc3_it_handler(void *handle, uint32_t *param) 1904 { 1905 dwc3_handle_t *dwc3_handle = (dwc3_handle_t *)handle; 1906 enum usb_action action = USB_NOTHING; 1907 uint32_t evtcnt, evt; 1908 uint8_t i; 1909 1910 /* Use same handler for all the Evt-Buf IRQs */ 1911 1912 /* Check for all Event Buffer interrupt k = USB_DWC3_INT_INUSE */ 1913 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 1914 evtcnt = dwc3_read_intr_count(dwc3_handle, i); 1915 1916 if (!evtcnt) { 1917 continue; 1918 } 1919 1920 VERBOSE("Interrupt Count %u\n", evtcnt); 1921 1922 evt = __HAL_PCD_READ_EVENT(dwc3_handle, i); 1923 VERBOSE("Event %08x\n", evt); 1924 1925 if (dwc3_is_ep_event(evt)) { /* EP event */ 1926 action = dwc3_handle_ep_event(dwc3_handle, evt, param); 1927 } else { /* NON-EP event */ 1928 if (dwc3_is_dev_event(evt)) { /* Device specific event */ 1929 action = dwc3_handle_dev_event(dwc3_handle, evt, param); 1930 } else { 1931 /* Error, non supported events */ 1932 } 1933 } 1934 1935 __HAL_PCD_INCR_EVENT_POS(dwc3_handle, i, USB_DWC3_EVENT_SIZE); 1936 1937 dwc3_ack_evt_count(dwc3_handle, i, USB_DWC3_EVENT_SIZE); 1938 } 1939 1940 return action; 1941 } 1942 1943 static enum usb_status usb_dwc3_write_packet(void *handle __unused, uint8_t *src __unused, 1944 uint8_t ch_ep_num __unused, uint16_t len __unused) 1945 { 1946 return USBD_OK; 1947 } 1948 1949 static void *usb_dwc3_read_packet(void *handle __unused, uint8_t *dest __unused, 1950 uint16_t len __unused) 1951 { 1952 return NULL; 1953 } 1954 1955 static enum usb_status usb_dwc3_write_empty_tx_fifo(void *handle __unused, uint32_t epnum, 1956 uint32_t xfer_len __unused, 1957 uint32_t *xfer_count __unused, 1958 uint32_t maxpacket __unused, 1959 uint8_t **xfer_buff __unused) 1960 { 1961 return USBD_OK; 1962 } 1963 1964 static const struct usb_driver usb_dwc3driver = { 1965 .ep0_out_start = usb_dwc3_ep0_out_start, 1966 .ep_start_xfer = usb_dwc3_ep_start_xfer, 1967 .ep0_start_xfer = usb_dwc3_ep0_start_xfer, 1968 .write_packet = usb_dwc3_write_packet, 1969 .read_packet = usb_dwc3_read_packet, 1970 .ep_set_stall = usb_dwc3_ep_set_stall, 1971 .start_device = usb_dwc3_start_device, 1972 .stop_device = usb_dwc3_stop_device, 1973 .set_address = usb_dwc3_set_address, 1974 .write_empty_tx_fifo = usb_dwc3_write_empty_tx_fifo, 1975 .it_handler = usb_dwc3_it_handler 1976 }; 1977 1978 /* USB2 PHY Mask 0xf */ 1979 #define USBPHY_ULPI 1U 1980 #define USBPHY_UTMI 2U 1981 #define USBPHY_EMBEDDED_USB2 8U 1982 #define USB2PHY_MASK 15U 1983 /* USB3 PHY Mask (0xf << 4) */ 1984 #define USBPHY_PIPE3 16U 1985 #define USBPHY_EMBEDDED_USB3 128U 1986 #define USB3PHY_MASK (15U << 4) 1987 1988 static enum usb_status dwc3_soft_reset(dwc3_handle_t *dwc3_handle) 1989 { 1990 uint64_t timeout; 1991 1992 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCTL, USB3_DCTL_CSFTRST); 1993 1994 timeout = timeout_init_us(500*1000); /* msec */ 1995 1996 while (DWC3_regread(dwc3_handle->usb_device, DWC3_DCTL) & USB3_DCTL_CSFTRST) { 1997 1998 if (timeout_elapsed(timeout)) { /* "Reset Timed Out" */ 1999 return USBD_TIMEOUT; 2000 } 2001 } 2002 2003 return USBD_OK; 2004 } 2005 2006 static enum usb_status dwc3_core_init(dwc3_handle_t *dwc3_handle, uint32_t phy_itface) 2007 { 2008 enum usb_status ret; 2009 2010 VERBOSE("Core ID %08x\n", (uint32_t)DWC3_regread(dwc3_handle->usb_global, DWC3_GSNPSID)); 2011 2012 /* Set GUSB3PIPECTL for all USB3 ports (1-n), currently doing only for 0 */ 2013 if ((phy_itface & USB3PHY_MASK) != 0U) { 2014 /* Relying on default value */ 2015 } 2016 2017 /* Set GUSB2PHYCFG for all USB2 ports (1-m), currently doing only for 0 */ 2018 if ((phy_itface & USB2PHY_MASK) != 0U) { 2019 switch (phy_itface) { 2020 case USBPHY_UTMI: 2021 DWC3_regupdateclr(dwc3_handle->usb_global, DWC3_GUSB2PHYCFG(0UL), 2022 USB3_GUSB2PHYCFG_ULPI_UTMI_SEL); 2023 break; 2024 case USBPHY_ULPI: 2025 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GUSB2PHYCFG(0UL), 2026 USB3_GUSB2PHYCFG_ULPI_UTMI_SEL); 2027 break; 2028 default: 2029 /* Relying on default value. */ 2030 break; 2031 } 2032 } 2033 2034 /* issue device SoftReset */ 2035 ret = dwc3_soft_reset(dwc3_handle); 2036 if (ret != USBD_OK) { 2037 ERROR("%s: %d\n", __func__, __LINE__); 2038 return ret; 2039 } 2040 2041 /* Put PHYs in reset */ 2042 /* Before Resetting PHY, put Core in Reset */ 2043 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GCTL, USB3_GCTL_CORESOFTRESET); 2044 2045 /* Assert USB3 PHY reset for all USB3 ports (1-n), currently doing only for 0 */ 2046 if ((phy_itface & USB3PHY_MASK) != 0U) { 2047 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GUSB3PIPECTL(0UL), 2048 USB3_GUSB3PIPECTL_PHYSOFTRST); 2049 } 2050 2051 /* Assert USB2 PHY reset for all USB2 ports (1-m), currently doing only for 0 */ 2052 if ((phy_itface & USB2PHY_MASK) != 0U) { 2053 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GUSB2PHYCFG(0UL), 2054 USB3_GUSB2PHYCFG_PHYSOFTRST); 2055 } 2056 2057 // Program PHY signals - PHY specific 2058 2059 2060 // Phy Init not needed since will be done by bootrom 2061 2062 /* Release PHY out of reset */ 2063 2064 /* Clear USB3 PHY reset for all USB3 ports (1-n), currently doing only for 0 */ 2065 if ((phy_itface & USB3PHY_MASK) != 0U) { 2066 DWC3_regupdateclr(dwc3_handle->usb_global, DWC3_GUSB3PIPECTL(0UL), 2067 USB3_GUSB3PIPECTL_PHYSOFTRST); 2068 } 2069 2070 /* Clear USB2 PHY reset */ 2071 if ((phy_itface & USB2PHY_MASK) != 0U) { 2072 DWC3_regupdateclr(dwc3_handle->usb_global, DWC3_GUSB2PHYCFG(0UL), 2073 USB3_GUSB2PHYCFG_PHYSOFTRST); 2074 } 2075 2076 // delay needed for phy reset 2077 mdelay(100); // 100ms 2078 2079 /* After PHYs are stable we can take Core out of reset state */ 2080 DWC3_regupdateclr(dwc3_handle->usb_global, DWC3_GCTL, USB3_GCTL_CORESOFTRESET); 2081 2082 /* Disable Scale down mode - check if disabled by default */ 2083 //DWC3_regupdateclr(USBx, DWC3_GCTL, DWC3_GCTL_SCALEDOWN_MASK); ***ToCheck 2084 2085 /* Alloc and Setup Scratch buffers for hibernation */ 2086 // ***ToCheck 2087 2088 // From Linux Driver 2089 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GSBUSCFG0, 0xe); 2090 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GSBUSCFG1, 0xf00); 2091 2092 /* Use default values for GTXFIFOSIZn and GRXFIFOSIZ0 */ 2093 2094 return USBD_OK; 2095 } 2096 2097 typedef enum { 2098 USB_DWC3_DEVICE_MODE = 0U, 2099 USB_DWC3_HOST_MODE = 1U, 2100 USB_DWC3_DRD_MODE = 2U, 2101 USB_DWC3_INVALID_MODE = 0xFU 2102 } USB_DWC3_modetypedef; 2103 2104 static enum usb_status dwc3_set_current_mode(dwc3_handle_t *dwc3_handle, USB_DWC3_modetypedef mode) 2105 { 2106 uint32_t reg; 2107 enum usb_status ret = USBD_OK; 2108 2109 reg = DWC3_regread(dwc3_handle->usb_global, DWC3_GCTL) & ~USB3_GCTL_PRTCAPDIR_MSK; 2110 2111 switch (mode) { 2112 case USB_DWC3_DEVICE_MODE: 2113 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GCTL, reg | 2114 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE)); 2115 break; 2116 case USB_DWC3_HOST_MODE: 2117 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GCTL, reg | 2118 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_HOST)); 2119 break; 2120 case USB_DWC3_DRD_MODE: 2121 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GCTL, reg | 2122 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); 2123 break; 2124 default: 2125 ret = USBD_FAIL; 2126 break; 2127 } 2128 2129 return ret; 2130 } 2131 2132 static enum usb_status dwc3_set_dev_speed(dwc3_handle_t *dwc3_handle, uint8_t speed) 2133 { 2134 uint32_t reg; 2135 enum usb_status ret = USBD_OK; 2136 2137 reg = DWC3_regread(dwc3_handle->usb_device, DWC3_DCFG) & ~USB3_DCFG_DEVSPD_MSK; 2138 2139 switch (speed) { 2140 case USB_DWC3_SPEED_SUPER: 2141 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_SUPERSPEED); 2142 INFO("%s = SuperSpeed\n", __func__); 2143 break; 2144 case USB_DWC3_SPEED_HIGH: 2145 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_HIGHSPEED); 2146 INFO("%s = HighSpeed\n", __func__); 2147 break; 2148 case USB_DWC3_SPEED_FULL_48: 2149 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_FULLSPEED1); 2150 INFO("%s = FullSpeed_48M\n", __func__); 2151 break; 2152 case USB_DWC3_SPEED_FULL: 2153 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_FULLSPEED2); 2154 INFO("%s = FullSpeed\n", __func__); 2155 break; 2156 case USB_DWC3_SPEED_LOW: 2157 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_LOWSPEED); 2158 INFO("%s = LowSpeed\n", __func__); 2159 break; 2160 default: 2161 ret = USBD_FAIL; 2162 break; 2163 } 2164 2165 return ret; 2166 } 2167 2168 /* 2169 * @brief USB_DevInit : Initializes the USB3 controller registers 2170 * for device mode 2171 * @param USBx Selected device 2172 * @param cfg pointer to a USB_DWC3_CfgTypeDef structure that contains 2173 * the configuration information for the specified USBx peripheral. 2174 * @retval HAL status 2175 */ 2176 enum usb_status dwc3_dev_init(dwc3_handle_t *dwc3_handle, uint8_t speed, uint8_t intr_dev) 2177 { 2178 uint32_t reg; 2179 uint8_t i; 2180 dwc3_epcmd_params_t params; 2181 enum usb_status ret; 2182 2183 /* Setup event buffers k = USB_DWC3_INT_INUSE */ 2184 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 2185 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GEVNTADRLO(i), 2186 lower_32_bits(dwc3_handle->intbuffers.evtbuffer_dma_addr[i])); 2187 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GEVNTADRHI(i), 2188 upper_32_bits(dwc3_handle->intbuffers.evtbuffer_dma_addr[i])); 2189 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GEVNTSIZ(i), 2190 USB_DWC3_EVENT_BUFFER_SIZE); 2191 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GEVNTCOUNT(i), 0); 2192 2193 /* EvtBufferPos[i] = 0; Implicit since static done in HAL */ 2194 } 2195 2196 /* 2197 * Need to set GUCTL2 RST_ACTBITLATER, so the driver can poll for CMDACT bit 2198 * when issuing the ENDTRANSFER command. 2199 */ 2200 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GUCTL2, DWC3_GUCTL2_RST_ACTBITLATER); 2201 2202 ret = dwc3_set_current_mode(dwc3_handle, USB_DWC3_DEVICE_MODE); 2203 if (ret != USBD_OK) { 2204 ERROR("%s: %d\n", __func__, __LINE__); 2205 return ret; 2206 } 2207 2208 ret = dwc3_set_dev_speed(dwc3_handle, speed); 2209 if (ret != USBD_OK) { 2210 ERROR("%s: %d\n", __func__, __LINE__); 2211 return ret; 2212 } 2213 2214 /* Issue a DEPSTARTCFG command with DEPCMD0.XferRscIdx set to 0, 2215 * to initialize the transfer resource allocation 2216 */ 2217 zeromem(¶ms, sizeof(params)); 2218 ret = dwc3_execute_dep_cmd(dwc3_handle, 0, DWC3_DEPCMD_PARAM(0) | 2219 USB_DWC3_DEPCMD_DEPSTARTCFG, ¶ms); 2220 if (ret != USBD_OK) { 2221 ERROR("%s: %d\n", __func__, __LINE__); 2222 return ret; 2223 } 2224 2225 /* Configure Control EP 0 & 1 mapped to physical EP 0 & 1*/ 2226 ret = dwc3_ep_configure(dwc3_handle, 0, false, EP_TYPE_CTRL, 2227 dwc3_handle->pcd_handle->out_ep[0].maxpacket, 0, 0, 0, 2228 dwc3_handle->OUT_ep[0].intr_num, USB_DWC3_DEPCFG_ACTION_INIT); 2229 if (ret != USBD_OK) { 2230 ERROR("%s: %d\n", __func__, __LINE__); 2231 return ret; 2232 } 2233 ret = dwc3_ep_configure(dwc3_handle, 0, true, EP_TYPE_CTRL, 2234 dwc3_handle->pcd_handle->in_ep[0].maxpacket, 0, 0, 1, 2235 dwc3_handle->IN_ep[0].intr_num, USB_DWC3_DEPCFG_ACTION_INIT); 2236 if (ret != USBD_OK) { 2237 ERROR("%s: %d\n", __func__, __LINE__); 2238 return ret; 2239 } 2240 2241 /* Transfer Resource for Control EP 0 & 1*/ 2242 (void)memset(¶ms, 0x00, sizeof(params)); 2243 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1U); 2244 /* As per databook, "Issue a DEPSTARTCFG command with DEPCMD0.XferRscIdx set to 2 to 2245 * re-initialize the transfer resource allocation on SetConfiguration or SetInterface 2246 * Request", value is 2 since the EP0 & EP1 take up the transfer resource 1 & 2, hence 2247 * further EPs will take resources 2,3,&up.. But in HAL we are not decoding the 2248 * Setup-packet requests, this should ideally come from USB-MW core. 2249 * Instead we follow the same method as Linux dwc3-gadget controller driver, All hardware 2250 * endpoints can be assigned a transfer resource and this setting will stay persistent 2251 * until either a core reset or hibernation. So whenever we do a DEPSTARTCFG(0) we can go 2252 * ahead and do DEPXFERCFG for every hardware endpoint as well. 2253 * For all k = DWC3_IP_NUM_EPS 2254 */ 2255 for (i = 0; i < DWC3_IP_NUM_EPS; i++) { 2256 ret = dwc3_execute_dep_cmd(dwc3_handle, i, USB_DWC3_DEPCMD_SETTRANSFRESOURCE, 2257 ¶ms); 2258 if (ret != USBD_OK) { 2259 ERROR("%s: %d\n", __func__, __LINE__); 2260 return ret; 2261 } 2262 } 2263 2264 /* setup EP0 to receive SETUP packets */ 2265 ret = dwc3_ep0_out_start(dwc3_handle, dwc3_handle->setup_dma_addr); 2266 if (ret != USBD_OK) { 2267 ERROR("%s: %d\n", __func__, __LINE__); 2268 return ret; 2269 } 2270 2271 /* Enable EP 0 & 1 */ 2272 DWC3_regupdateset(dwc3_handle->usb_device, DWC3_DALEPENA, DWC3_DALEPENA_EP(0) | 2273 DWC3_DALEPENA_EP(1)); 2274 2275 /* 2276 * Set interrupt/EventQ number on which non-endpoint-specific device-related 2277 * interrupts are generated 2278 */ 2279 reg = DWC3_regread(dwc3_handle->usb_device, DWC3_DCFG) & ~USB3_DCFG_INTRNUM_MSK; 2280 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | 2281 ((uint32_t)intr_dev << USB3_DCFG_INTRNUM_POS)); 2282 2283 /* Enable all events but Start and End of Frame IRQs */ 2284 DWC3_regupdateset(dwc3_handle->usb_device, DWC3_DEVTEN, USB3_DEVTEN_VENDEVTSTRCVDEN | 2285 USB3_DEVTEN_EVNTOVERFLOWEN | USB3_DEVTEN_CMDCMPLTEN | 2286 USB3_DEVTEN_ERRTICERREVTEN | USB3_DEVTEN_U3L2L1SUSPEN | 2287 USB3_DEVTEN_HIBERNATIONREQEVTEN | USB3_DEVTEN_WKUPEVTEN | 2288 USB3_DEVTEN_ULSTCNGEN | USB3_DEVTEN_CONNECTDONEEVTEN | 2289 USB3_DEVTEN_USBRSTEVTEN | USB3_DEVTEN_DISSCONNEVTEN | 2290 USB3_DEVTEN_L1SUSPEN | USB3_DEVTEN_L1WKUPEVTEN 2291 /* | USB3_DEVTEN_SOFTEVTEN*/); 2292 2293 /* Enable Event Buffer interrupt k = USB_DWC3_INT_INUSE */ 2294 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 2295 __HAL_PCD_ENABLE_INTR(dwc3_handle, i); 2296 } 2297 2298 return USBD_OK; 2299 } 2300 2301 void usb_dwc3_init_driver(struct usb_handle *usb_core_handle, struct pcd_handle *pcd_handle, 2302 dwc3_handle_t *dwc3_handle, void *base_addr) 2303 { 2304 uint32_t i = 0; 2305 enum usb_status ret; 2306 uintptr_t base = (uintptr_t)base_addr; 2307 2308 dwc3_handle->usb_global = (usb_dwc3_global_t *) 2309 (base + USB_DWC3_GLOBAL_BASE); 2310 2311 dwc3_handle->usb_device = (usb_dwc3_device_t *) 2312 (base + USB_DWC3_DEVICE_BASE); 2313 2314 dwc3_handle->pcd_handle = pcd_handle; 2315 2316 /* Check hpcd->State is HAL_PCD_STATE_RESET, otherwise error */ 2317 assert(dwc3_handle->State == HAL_PCD_STATE_RESET); 2318 2319 dwc3_handle->State = HAL_PCD_STATE_BUSY; 2320 2321 /* Disable the Interrupts */ 2322 // Not required since USB device and EP interrupts are disabled at boot, ***ToCheck 2323 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 2324 __HAL_PCD_DISABLE_INTR(dwc3_handle, i); 2325 } 2326 2327 /* Init the Core (common init.) */ 2328 ret = dwc3_core_init(dwc3_handle, USBPHY_UTMI); 2329 if (ret != USBD_OK) { 2330 panic(); 2331 } 2332 2333 /* Init endpoints structures */ 2334 for (i = 0; i < USB_DWC3_NUM_IN_EP ; i++) { 2335 /* Init ep structure */ 2336 pcd_handle->in_ep[i].is_in = true; 2337 pcd_handle->in_ep[i].num = i; 2338 dwc3_handle->IN_ep[i].tx_fifo_num = i; 2339 /* Control until ep is activated */ 2340 pcd_handle->in_ep[i].type = EP_TYPE_CTRL; 2341 2342 /* 2343 * We are doing 1:1 alternate mapping for endpoints, meaning 2344 * IN Endpoint X maps to Physical Endpoint 2*X +1 and 2345 * OUT Endpoint X maps to Physical Endpoint 2*X. 2346 * So USB endpoint 0x81 is 0x03. 2347 */ 2348 #if USB_DWC3_NUM_IN_EP > 1 2349 dwc3_handle->IN_ep[i].phy_epnum = __HAL_PCD_EPADDR_TO_PHYEPNUM(i | EP_DIR_IN); 2350 #else 2351 dwc3_handle->IN_ep[i].phy_epnum = 1U; 2352 #endif 2353 2354 dwc3_handle->IN_ep[i].intr_num = PCD_DEV_EVENTS_INTR; 2355 } 2356 2357 for (i = 0; i < USB_DWC3_NUM_OUT_EP ; i++) { 2358 /* Init ep structure */ 2359 pcd_handle->out_ep[i].is_in = false; 2360 pcd_handle->out_ep[i].num = i; 2361 dwc3_handle->OUT_ep[i].tx_fifo_num = i; 2362 /* Control until ep is activated */ 2363 pcd_handle->out_ep[i].type = EP_TYPE_CTRL; 2364 2365 /* 2366 * We are doing 1:1 alternate mapping for endpoints, meaning 2367 * IN Endpoint X maps to Physical Endpoint 2*X +1 and 2368 * OUT Endpoint X maps to Physical Endpoint 2*X. 2369 * So USB endpoint 0x81 is 0x03. 2370 */ 2371 dwc3_handle->OUT_ep[i].phy_epnum = __HAL_PCD_EPADDR_TO_PHYEPNUM(i); 2372 2373 dwc3_handle->OUT_ep[i].intr_num = PCD_DEV_EVENTS_INTR; 2374 2375 dwc3_handle->OUT_ep[i].bounce_buf = dwc3_handle->bounce_bufs[i].bounce_buf; 2376 } 2377 2378 #define PHYS_AREA STM32MP_USB_DWC3_BASE 2379 2380 #define EVTBUF_AREA_OFFSET 0U 2381 #define TRB_OUT_AREA_OFFSET (EVTBUF_AREA_OFFSET + USB_DWC3_EVENT_BUFFER_SIZE) 2382 #define TRB_IN_AREA_OFFSET (TRB_OUT_AREA_OFFSET + sizeof(usb_dwc3_trb_t)) 2383 #define SETUP_AREA_OFFSET (TRB_IN_AREA_OFFSET + sizeof(usb_dwc3_trb_t)) 2384 2385 void *coh_area = (void *)(uintptr_t)PHYS_AREA; 2386 2387 #define EVTBUF_AREA (coh_area + EVTBUF_AREA_OFFSET) 2388 #define TRB_OUT_AREA (coh_area + TRB_OUT_AREA_OFFSET) 2389 #define TRB_IN_AREA (coh_area + TRB_IN_AREA_OFFSET) 2390 #define SETUP_AREA (coh_area + SETUP_AREA_OFFSET) 2391 2392 dwc3_handle->setup_dma_addr = (uintptr_t)api_getdmaaddr((void *)SETUP_AREA, 2393 USB_SETUP_PACKET_SIZE, 1); 2394 assert(dwc3_handle->setup_dma_addr != 0U); 2395 2396 dwc3_handle->setup_addr = SETUP_AREA; 2397 assert(dwc3_handle->setup_addr != NULL); 2398 2399 /* Map DMA and Coherent address for event buffers k = USB_DWC3_INT_INUSE */ 2400 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 2401 dwc3_handle->intbuffers.evtbuffer_dma_addr[i] = 2402 api_getdmaaddr((void *)EVTBUF_AREA, USB_DWC3_EVENT_BUFFER_SIZE, 1); 2403 assert(dwc3_handle->intbuffers.evtbuffer_dma_addr[i] != 0U); 2404 2405 dwc3_handle->intbuffers.evtbuffer_addr[i] = EVTBUF_AREA; 2406 assert(dwc3_handle->intbuffers.evtbuffer_addr[i] != NULL); 2407 2408 dwc3_handle->intbuffers.evtbufferpos[i] = 0; 2409 2410 INFO("EventBuffer%u: BuffArea=%lx DmaAddr=%08x CoherentMapAddr=%p\n", i, 2411 (PHYS_AREA + EVTBUF_AREA_OFFSET), 2412 (uint32_t)dwc3_handle->intbuffers.evtbuffer_dma_addr[i], 2413 dwc3_handle->intbuffers.evtbuffer_addr[i]); 2414 } 2415 2416 /* MAP TRB Coherent and DMA address for EP0IN and EP0OUT */ 2417 dwc3_handle->IN_ep[0].trb_dma_addr = (uint32_t)api_getdmaaddr((void *)TRB_IN_AREA, 2418 sizeof(usb_dwc3_trb_t), 1); 2419 assert(dwc3_handle->IN_ep[0].trb_dma_addr != 0U); 2420 2421 dwc3_handle->IN_ep[0].trb_addr = (usb_dwc3_trb_t *)TRB_IN_AREA; 2422 assert(dwc3_handle->IN_ep[0].trb_addr != NULL); 2423 2424 dwc3_handle->OUT_ep[0].trb_dma_addr = (uint32_t)api_getdmaaddr((void *)TRB_OUT_AREA, 2425 sizeof(usb_dwc3_trb_t), 2426 1); 2427 assert(dwc3_handle->OUT_ep[0].trb_dma_addr != 0U); 2428 2429 dwc3_handle->OUT_ep[0].trb_addr = (usb_dwc3_trb_t *)TRB_OUT_AREA; 2430 assert(dwc3_handle->OUT_ep[0].trb_addr != NULL); 2431 2432 /* Init Device */ 2433 dwc3_handle->EP0_State = HAL_PCD_EP0_SETUP_QUEUED; 2434 2435 ret = dwc3_dev_init(dwc3_handle, USB_DWC3_SPEED_HIGH, PCD_DEV_EVENTS_INTR); 2436 assert(ret == USBD_OK); 2437 2438 dwc3_handle->State = HAL_PCD_STATE_READY; 2439 2440 register_usb_driver(usb_core_handle, pcd_handle, &usb_dwc3driver, 2441 dwc3_handle); 2442 } 2443