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%lu %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 static enum usb_status dwc3_ep_clear_stall(dwc3_handle_t *dwc3_handle, usb_dwc3_endpoint_t *dwc3_ep) 1527 { 1528 dwc3_epcmd_params_t params; 1529 1530 (void)memset(¶ms, 0x00, sizeof(params)); 1531 1532 return dwc3_execute_dep_cmd(dwc3_handle, dwc3_ep->phy_epnum, USB_DWC3_DEPCMD_CLEARSTALL, 1533 ¶ms); 1534 } 1535 1536 static uint8_t dwc3_get_dev_speed(dwc3_handle_t *dwc3_handle) 1537 { 1538 uint32_t reg; 1539 uint8_t ret; 1540 1541 reg = DWC3_regread(dwc3_handle->usb_device, DWC3_DSTS) & USB3_DSTS_CONNECTSPD; 1542 1543 switch (reg) { 1544 case DWC3_DSTS_SUPERSPEED: 1545 ret = USB_DWC3_SPEED_SUPER; 1546 INFO("%s = SuperSpeed\n", __func__); 1547 break; 1548 case DWC3_DSTS_HIGHSPEED: 1549 ret = USB_DWC3_SPEED_HIGH; 1550 INFO("%s = HighSpeed\n", __func__); 1551 break; 1552 case DWC3_DSTS_FULLSPEED1: 1553 ret = USB_DWC3_SPEED_FULL_48; 1554 INFO("%s = FullSpeed_48M\n", __func__); 1555 break; 1556 case DWC3_DSTS_FULLSPEED2: 1557 ret = USB_DWC3_SPEED_FULL; 1558 INFO("%s = FullSpeed\n", __func__); 1559 break; 1560 case DWC3_DSTS_LOWSPEED: 1561 ret = USB_DWC3_SPEED_LOW; 1562 INFO("%s = LowSpeed\n", __func__); 1563 break; 1564 default: /* Invalid */ 1565 ret = USB_DWC3_SPEED_INVALID; 1566 INFO("%s = Invalid\n", __func__); 1567 break; 1568 } 1569 1570 return ret; 1571 } 1572 1573 static uint32_t dwc3_get_epcfg_action(uint8_t action) 1574 { 1575 uint32_t ret; 1576 1577 switch (action) { 1578 case USB_DWC3_DEPCFG_ACTION_INIT: 1579 ret = DWC3_DEPCFG_ACTION_INIT; 1580 break; 1581 case USB_DWC3_DEPCFG_ACTION_RESTORE: 1582 ret = DWC3_DEPCFG_ACTION_RESTORE; 1583 break; 1584 case USB_DWC3_DEPCFG_ACTION_MODIFY: 1585 ret = DWC3_DEPCFG_ACTION_MODIFY; 1586 break; 1587 default: 1588 ret = 0U; 1589 break; 1590 } 1591 1592 return ret; 1593 } 1594 1595 static enum usb_status dwc3_ep_configure(dwc3_handle_t *dwc3_handle, uint8_t epnum, bool is_in, 1596 uint8_t type, uint32_t max_packet, uint8_t tx_fifo, 1597 uint16_t binterval, uint8_t phy_epnum, uint8_t intr_num, 1598 uint8_t action) 1599 { 1600 dwc3_epcmd_params_t params; 1601 1602 (void)memset(¶ms, 0x00, sizeof(params)); 1603 params.param0 = DWC3_DEPCFG_EP_TYPE(type) | DWC3_DEPCFG_MAX_PACKET_SIZE(max_packet); 1604 1605 /* Burst size is only needed in SuperSpeed mode */ 1606 //params.param0 |= DWC3_DEPCFG_BURST_SIZE(0); 1607 1608 params.param0 |= dwc3_get_epcfg_action(action); 1609 1610 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN | DWC3_DEPCFG_XFER_NOT_READY_EN; 1611 if (type == EP_TYPE_ISOC) { 1612 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; 1613 } 1614 1615 params.param1 |= DWC3_DEPCFG_EP_NUMBER(((uint32_t)epnum << 1) + (is_in ? 1UL : 0UL)); 1616 1617 params.param1 |= DWC3_DEPCFG_EP_INTR_NUM(intr_num); 1618 1619 if (is_in) { 1620 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(tx_fifo); 1621 } 1622 1623 if (binterval != 0U) { 1624 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(binterval - 1UL); 1625 } 1626 1627 return dwc3_execute_dep_cmd(dwc3_handle, phy_epnum, USB_DWC3_DEPCMD_SETEPCONFIG, ¶ms); 1628 } 1629 1630 static inline uint8_t dwc3_read_dev_evt_linkstate(uint32_t event) 1631 { 1632 return (uint8_t)((event & DWC3_EVT_DEVEVT_LNKSTS_MASK) >> DWC3_EVT_DEVEVT_LNKSTS_BITPOS); 1633 } 1634 1635 static inline const char *dwc3_get_linkstate_str(uint8_t state) 1636 { 1637 const char *ret; 1638 1639 switch (state) { 1640 case DWC3_LINK_STATE_ON: 1641 ret = "ON"; 1642 break; 1643 case DWC3_LINK_STATE_L1: 1644 ret = "L1"; 1645 break; 1646 case DWC3_LINK_STATE_L2: 1647 ret = "L2"; 1648 break; 1649 case DWC3_LINK_STATE_DIS: 1650 ret = "Disconnect"; 1651 break; 1652 case DWC3_LINK_STATE_EARLY_SUS: 1653 ret = "Early Suspend"; 1654 break; 1655 case DWC3_LINK_STATE_RESET: 1656 ret = "Reset"; 1657 break; 1658 case DWC3_LINK_STATE_RESUME: 1659 ret = "Resume"; 1660 break; 1661 default: 1662 ret = "UNKNOWN state"; 1663 break; 1664 } 1665 1666 return ret; 1667 } 1668 1669 static enum usb_action dwc3_handle_dev_event(dwc3_handle_t *dwc3_handle, uint32_t event, 1670 uint32_t *param) 1671 { 1672 enum usb_action action = USB_NOTHING; 1673 uint8_t type = dwc3_read_dev_evt_type(event); 1674 uint8_t i, speed; 1675 usb_dwc3_endpoint_t *ep; 1676 enum usb_status ret; 1677 uint32_t ep0_mps = USB3_MAX_PACKET_SIZE; 1678 1679 switch (type) { 1680 case USB_DWC3_DEVICE_EVENT_DISCONNECT: 1681 INFO("Event: Device Disconnect\n"); 1682 /* For SuperSpeed, set DCTL[8:5] to 5*/ 1683 1684 action = USB_DISCONNECT; 1685 break; 1686 1687 case USB_DWC3_DEVICE_EVENT_RESET: 1688 INFO("Event: Device Reset\n"); 1689 /* 1690 * Spec says, Wait till EP0 finishes current transfer and reaches Setup-Stage. 1691 * But there is no wait in Linux driver which means that Host will take care while 1692 * issuing a reset. Also its tricky to implement since we can't wait here 1693 * (in interrupt context) for further control EP interrupts 1694 */ 1695 1696 if (dwc3_handle->EP0_State != HAL_PCD_EP0_SETUP_QUEUED) { 1697 ret = dwc3_ep_stop_xfer(dwc3_handle, &dwc3_handle->pcd_handle->out_ep[0]); 1698 if (ret != USBD_OK) { 1699 ERROR("%s: %d\n", __func__, __LINE__); 1700 } 1701 ret = dwc3_epaddr_set_stall(dwc3_handle, EP0_OUT); // OUT EP0 1702 if (ret != USBD_OK) { 1703 ERROR("%s: %d\n", __func__, __LINE__); 1704 } 1705 } 1706 1707 /* 1708 * Stop transfers for all(USB_DWC3_NUM_IN_EPS) EP 1709 * except EP0IN k = USB_DWC3_NUM_IN_EP 1710 */ 1711 for (i = 0; i < USB_DWC3_NUM_IN_EP; i++) { 1712 dwc3_ep_stop_xfer(dwc3_handle, 1713 &dwc3_handle->pcd_handle->in_ep[i]); 1714 } 1715 1716 /* Stop transfers for all EP except EP0OUT k = USB_DWC3_NUM_OUT_EP */ 1717 for (i = 1; i < USB_DWC3_NUM_OUT_EP; i++) { 1718 dwc3_ep_stop_xfer(dwc3_handle, 1719 &dwc3_handle->pcd_handle->out_ep[i]); 1720 } 1721 1722 /* Clear Stall for all EP except EP0IN k = USB_DWC3_NUM_IN_EP */ 1723 for (i = 1; i < USB_DWC3_NUM_IN_EP; i++) { 1724 ep = &dwc3_handle->IN_ep[i]; 1725 1726 if (!ep->is_stall) { 1727 continue; 1728 } 1729 1730 ep->is_stall = false; 1731 1732 ret = dwc3_ep_clear_stall(dwc3_handle, ep); 1733 if (ret != USBD_OK) { 1734 ERROR("%s: %d\n", __func__, __LINE__); 1735 return action; 1736 } 1737 } 1738 1739 /* Clear Stall for all EP except EP0OUT k = USB_DWC3_NUM_OUT_EP */ 1740 for (i = 1; i < USB_DWC3_NUM_OUT_EP; i++) { 1741 ep = &dwc3_handle->OUT_ep[i]; 1742 1743 if (!ep->is_stall) { 1744 continue; 1745 } 1746 1747 ep->is_stall = false; 1748 //ep->num = ep_addr & ADDRESS_MASK; 1749 //ep->is_in = ((ep_addr & 0x80) == 0x80); 1750 1751 ret = dwc3_ep_clear_stall(dwc3_handle, ep); 1752 if (ret != USBD_OK) { 1753 ERROR("%s: %d\n", __func__, __LINE__); 1754 return action; 1755 } 1756 } 1757 1758 /* Reset device address to zero */ 1759 ret = usb_dwc3_set_address(dwc3_handle, 0); 1760 if (ret != USBD_OK) { 1761 ERROR("%s: %d\n", __func__, __LINE__); 1762 return action; 1763 } 1764 1765 action = USB_RESET; 1766 1767 break; 1768 1769 case USB_DWC3_DEVICE_EVENT_CONNECT_DONE: 1770 INFO("Event: Device Connection Done\n"); 1771 1772 /* 1773 * hpcd->Init.ep0_mps = MaxPacketSize to 512 (SuperSpeed), 64 (High-Speed), 1774 * 8/16/32/64 (Full-Speed), or 8 (Low-Speed). 1775 */ 1776 speed = dwc3_get_dev_speed(dwc3_handle); 1777 switch (speed) { 1778 case USB_DWC3_SPEED_SUPER: 1779 ep0_mps = 512; 1780 break; 1781 case USB_DWC3_SPEED_HIGH: 1782 ep0_mps = 64; 1783 break; 1784 case USB_DWC3_SPEED_FULL_48: 1785 case USB_DWC3_SPEED_FULL: 1786 ep0_mps = 64; 1787 break; 1788 case USB_DWC3_SPEED_LOW: 1789 ep0_mps = 8; 1790 break; 1791 default: 1792 ERROR("%s: %d\n", __func__, __LINE__); 1793 break; 1794 } 1795 1796 /* Update DWC3_GCTL_RAMCLKSEL, but only for Superspeed */ 1797 1798 /* Modify max packet size for EP 0 & 1 */ 1799 ret = dwc3_ep_configure(dwc3_handle, 0, false, EP_TYPE_CTRL, ep0_mps, 0, 0, 0, 1800 dwc3_handle->OUT_ep[0].intr_num, 1801 USB_DWC3_DEPCFG_ACTION_MODIFY); 1802 if (ret != USBD_OK) { 1803 ERROR("%s: %d\n", __func__, __LINE__); 1804 return action; 1805 } 1806 ret = dwc3_ep_configure(dwc3_handle, 0, true, EP_TYPE_CTRL, ep0_mps, 0, 0, 1, 1807 dwc3_handle->IN_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 1814 dwc3_handle->pcd_handle->out_ep[0].maxpacket = ep0_mps; 1815 dwc3_handle->pcd_handle->in_ep[0].maxpacket = ep0_mps; 1816 1817 /* 1818 * Depending on the connected speed, write to the other 1819 * PHY's control register to suspend it. 1820 */ 1821 1822 /* Use default values for GTXFIFOSIZn and GRXFIFOSIZ0 */ 1823 1824 action = USB_ENUM_DONE; 1825 1826 break; 1827 1828 case USB_DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: 1829 INFO("Event: Link Status Change : %s(%d)\n", 1830 dwc3_get_linkstate_str(dwc3_read_dev_evt_linkstate(event)), 1831 dwc3_read_dev_evt_linkstate(event)); 1832 /* Handle link state changes, U0,U1,U2,U3 with suspend/resume functions */ 1833 1834 break; 1835 1836 case USB_DWC3_DEVICE_EVENT_WAKEUP: 1837 INFO("Event: Wake-UP\n"); 1838 // resume function 1839 action = USB_RESUME; 1840 break; 1841 1842 case USB_DWC3_DEVICE_EVENT_HIBER_REQ: 1843 INFO("Event: Hibernation Request\n"); 1844 /* hibernation function */ 1845 break; 1846 1847 case USB_DWC3_DEVICE_EVENT_EOPF: /* End of Periodic Frame */ 1848 INFO("Event: Suspend\n"); 1849 action = USB_SUSPEND; 1850 break; 1851 1852 case USB_DWC3_DEVICE_EVENT_SOF: /* Start of Periodic Frame */ 1853 INFO("Event: Start of Periodic Frame\n"); 1854 break; 1855 1856 case USB_DWC3_DEVICE_EVENT_L1SUSPEND: 1857 INFO("Event: L1 Suspend\n"); 1858 action = USB_LPM; 1859 break; 1860 1861 case USB_DWC3_DEVICE_EVENT_ERRATIC_ERROR: /* Erratic Error */ 1862 INFO("Event: Erratic Error\n"); 1863 break; 1864 1865 case USB_DWC3_DEVICE_EVENT_CMD_CMPL: /* Command Complete */ 1866 INFO("Event: Command Complete\n"); 1867 break; 1868 1869 case USB_DWC3_DEVICE_EVENT_OVERFLOW: /* Overflow */ 1870 INFO("Event: Overflow\n"); 1871 break; 1872 1873 case USB_DWC3_DEVICE_EVENT_L1WAKEUP: 1874 INFO("Event: L1 Wake-UP\n"); 1875 // resume function 1876 action = USB_RESUME; 1877 break; 1878 1879 default: /* UNKNOWN IRQ type */ 1880 break; 1881 } 1882 1883 return action; 1884 } 1885 1886 static inline bool dwc3_is_ep_event(uint32_t event) 1887 { 1888 return ((event & DWC3_EVT_TYPE_MASK) == (DWC3_EVT_TYPE_EP << DWC3_EVT_TYPE_BITPOS)); 1889 } 1890 1891 static inline bool dwc3_is_dev_event(uint32_t event) 1892 { 1893 return ((event & DWC3_EVT_TYPE_MASK) == (DWC3_EVT_TYPE_DEVSPEC << DWC3_EVT_TYPE_BITPOS)); 1894 } 1895 1896 static enum usb_action usb_dwc3_it_handler(void *handle, uint32_t *param) 1897 { 1898 dwc3_handle_t *dwc3_handle = (dwc3_handle_t *)handle; 1899 enum usb_action action = USB_NOTHING; 1900 uint32_t evtcnt, evt; 1901 uint8_t i; 1902 1903 /* Use same handler for all the Evt-Buf IRQs */ 1904 1905 /* Check for all Event Buffer interrupt k = USB_DWC3_INT_INUSE */ 1906 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 1907 evtcnt = dwc3_read_intr_count(dwc3_handle, i); 1908 1909 if (!evtcnt) { 1910 continue; 1911 } 1912 1913 VERBOSE("Interrupt Count %u\n", evtcnt); 1914 1915 evt = __HAL_PCD_READ_EVENT(dwc3_handle, i); 1916 VERBOSE("Event %08x\n", evt); 1917 1918 if (dwc3_is_ep_event(evt)) { /* EP event */ 1919 action = dwc3_handle_ep_event(dwc3_handle, evt, param); 1920 } else { /* NON-EP event */ 1921 if (dwc3_is_dev_event(evt)) { /* Device specific event */ 1922 action = dwc3_handle_dev_event(dwc3_handle, evt, param); 1923 } else { 1924 /* Error, non supported events */ 1925 } 1926 } 1927 1928 __HAL_PCD_INCR_EVENT_POS(dwc3_handle, i, USB_DWC3_EVENT_SIZE); 1929 1930 dwc3_ack_evt_count(dwc3_handle, i, USB_DWC3_EVENT_SIZE); 1931 } 1932 1933 return action; 1934 } 1935 1936 static enum usb_status usb_dwc3_write_packet(void *handle __unused, uint8_t *src __unused, 1937 uint8_t ch_ep_num __unused, uint16_t len __unused) 1938 { 1939 return USBD_OK; 1940 } 1941 1942 static void *usb_dwc3_read_packet(void *handle __unused, uint8_t *dest __unused, 1943 uint16_t len __unused) 1944 { 1945 return NULL; 1946 } 1947 1948 static enum usb_status usb_dwc3_write_empty_tx_fifo(void *handle __unused, uint32_t epnum, 1949 uint32_t xfer_len __unused, 1950 uint32_t *xfer_count __unused, 1951 uint32_t maxpacket __unused, 1952 uint8_t **xfer_buff __unused) 1953 { 1954 return USBD_OK; 1955 } 1956 1957 static const struct usb_driver usb_dwc3driver = { 1958 .ep0_out_start = usb_dwc3_ep0_out_start, 1959 .ep_start_xfer = usb_dwc3_ep_start_xfer, 1960 .ep0_start_xfer = usb_dwc3_ep0_start_xfer, 1961 .write_packet = usb_dwc3_write_packet, 1962 .read_packet = usb_dwc3_read_packet, 1963 .ep_set_stall = usb_dwc3_ep_set_stall, 1964 .start_device = usb_dwc3_start_device, 1965 .stop_device = usb_dwc3_stop_device, 1966 .set_address = usb_dwc3_set_address, 1967 .write_empty_tx_fifo = usb_dwc3_write_empty_tx_fifo, 1968 .it_handler = usb_dwc3_it_handler 1969 }; 1970 1971 /* USB2 PHY Mask 0xf */ 1972 #define USBPHY_ULPI 1U 1973 #define USBPHY_UTMI 2U 1974 #define USBPHY_EMBEDDED_USB2 8U 1975 #define USB2PHY_MASK 15U 1976 /* USB3 PHY Mask (0xf << 4) */ 1977 #define USBPHY_PIPE3 16U 1978 #define USBPHY_EMBEDDED_USB3 128U 1979 #define USB3PHY_MASK (15U << 4) 1980 1981 static enum usb_status dwc3_soft_reset(dwc3_handle_t *dwc3_handle) 1982 { 1983 uint64_t timeout; 1984 1985 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCTL, USB3_DCTL_CSFTRST); 1986 1987 timeout = timeout_init_us(500*1000); /* msec */ 1988 1989 while (DWC3_regread(dwc3_handle->usb_device, DWC3_DCTL) & USB3_DCTL_CSFTRST) { 1990 1991 if (timeout_elapsed(timeout)) { /* "Reset Timed Out" */ 1992 return USBD_TIMEOUT; 1993 } 1994 } 1995 1996 return USBD_OK; 1997 } 1998 1999 static enum usb_status dwc3_core_init(dwc3_handle_t *dwc3_handle, uint32_t phy_itface) 2000 { 2001 enum usb_status ret; 2002 2003 VERBOSE("Core ID %08x\n", (uint32_t)DWC3_regread(dwc3_handle->usb_global, DWC3_GSNPSID)); 2004 2005 /* Set GUSB3PIPECTL for all USB3 ports (1-n), currently doing only for 0 */ 2006 if ((phy_itface & USB3PHY_MASK) != 0U) { 2007 /* Relying on default value */ 2008 } 2009 2010 /* Set GUSB2PHYCFG for all USB2 ports (1-m), currently doing only for 0 */ 2011 if ((phy_itface & USB2PHY_MASK) != 0U) { 2012 switch (phy_itface) { 2013 case USBPHY_UTMI: 2014 DWC3_regupdateclr(dwc3_handle->usb_global, DWC3_GUSB2PHYCFG(0UL), 2015 USB3_GUSB2PHYCFG_ULPI_UTMI_SEL); 2016 break; 2017 case USBPHY_ULPI: 2018 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GUSB2PHYCFG(0UL), 2019 USB3_GUSB2PHYCFG_ULPI_UTMI_SEL); 2020 break; 2021 default: 2022 /* Relying on default value. */ 2023 break; 2024 } 2025 } 2026 2027 /* issue device SoftReset */ 2028 ret = dwc3_soft_reset(dwc3_handle); 2029 if (ret != USBD_OK) { 2030 ERROR("%s: %d\n", __func__, __LINE__); 2031 return ret; 2032 } 2033 2034 /* Put PHYs in reset */ 2035 /* Before Resetting PHY, put Core in Reset */ 2036 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GCTL, USB3_GCTL_CORESOFTRESET); 2037 2038 /* Assert USB3 PHY reset for all USB3 ports (1-n), currently doing only for 0 */ 2039 if ((phy_itface & USB3PHY_MASK) != 0U) { 2040 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GUSB3PIPECTL(0UL), 2041 USB3_GUSB3PIPECTL_PHYSOFTRST); 2042 } 2043 2044 /* Assert USB2 PHY reset for all USB2 ports (1-m), currently doing only for 0 */ 2045 if ((phy_itface & USB2PHY_MASK) != 0U) { 2046 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GUSB2PHYCFG(0UL), 2047 USB3_GUSB2PHYCFG_PHYSOFTRST); 2048 } 2049 2050 // Program PHY signals - PHY specific 2051 2052 2053 // Phy Init not needed since will be done by bootrom 2054 2055 /* Release PHY out of reset */ 2056 2057 /* Clear USB3 PHY reset for all USB3 ports (1-n), currently doing only for 0 */ 2058 if ((phy_itface & USB3PHY_MASK) != 0U) { 2059 DWC3_regupdateclr(dwc3_handle->usb_global, DWC3_GUSB3PIPECTL(0UL), 2060 USB3_GUSB3PIPECTL_PHYSOFTRST); 2061 } 2062 2063 /* Clear USB2 PHY reset */ 2064 if ((phy_itface & USB2PHY_MASK) != 0U) { 2065 DWC3_regupdateclr(dwc3_handle->usb_global, DWC3_GUSB2PHYCFG(0UL), 2066 USB3_GUSB2PHYCFG_PHYSOFTRST); 2067 } 2068 2069 // delay needed for phy reset 2070 mdelay(100); // 100ms 2071 2072 /* After PHYs are stable we can take Core out of reset state */ 2073 DWC3_regupdateclr(dwc3_handle->usb_global, DWC3_GCTL, USB3_GCTL_CORESOFTRESET); 2074 2075 /* Disable Scale down mode - check if disabled by default */ 2076 //DWC3_regupdateclr(USBx, DWC3_GCTL, DWC3_GCTL_SCALEDOWN_MASK); ***ToCheck 2077 2078 /* Alloc and Setup Scratch buffers for hibernation */ 2079 // ***ToCheck 2080 2081 // From Linux Driver 2082 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GSBUSCFG0, 0xe); 2083 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GSBUSCFG1, 0xf00); 2084 2085 /* Use default values for GTXFIFOSIZn and GRXFIFOSIZ0 */ 2086 2087 return USBD_OK; 2088 } 2089 2090 typedef enum { 2091 USB_DWC3_DEVICE_MODE = 0U, 2092 USB_DWC3_HOST_MODE = 1U, 2093 USB_DWC3_DRD_MODE = 2U, 2094 USB_DWC3_INVALID_MODE = 0xFU 2095 } USB_DWC3_modetypedef; 2096 2097 static enum usb_status dwc3_set_current_mode(dwc3_handle_t *dwc3_handle, USB_DWC3_modetypedef mode) 2098 { 2099 uint32_t reg; 2100 enum usb_status ret = USBD_OK; 2101 2102 reg = DWC3_regread(dwc3_handle->usb_global, DWC3_GCTL) & ~USB3_GCTL_PRTCAPDIR_MSK; 2103 2104 switch (mode) { 2105 case USB_DWC3_DEVICE_MODE: 2106 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GCTL, reg | 2107 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE)); 2108 break; 2109 case USB_DWC3_HOST_MODE: 2110 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GCTL, reg | 2111 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_HOST)); 2112 break; 2113 case USB_DWC3_DRD_MODE: 2114 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GCTL, reg | 2115 DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); 2116 break; 2117 default: 2118 ret = USBD_FAIL; 2119 break; 2120 } 2121 2122 return ret; 2123 } 2124 2125 static enum usb_status dwc3_set_dev_speed(dwc3_handle_t *dwc3_handle, uint8_t speed) 2126 { 2127 uint32_t reg; 2128 enum usb_status ret = USBD_OK; 2129 2130 reg = DWC3_regread(dwc3_handle->usb_device, DWC3_DCFG) & ~USB3_DCFG_DEVSPD_MSK; 2131 2132 switch (speed) { 2133 case USB_DWC3_SPEED_SUPER: 2134 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_SUPERSPEED); 2135 INFO("%s = SuperSpeed\n", __func__); 2136 break; 2137 case USB_DWC3_SPEED_HIGH: 2138 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_HIGHSPEED); 2139 INFO("%s = HighSpeed\n", __func__); 2140 break; 2141 case USB_DWC3_SPEED_FULL_48: 2142 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_FULLSPEED1); 2143 INFO("%s = FullSpeed_48M\n", __func__); 2144 break; 2145 case USB_DWC3_SPEED_FULL: 2146 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_FULLSPEED2); 2147 INFO("%s = FullSpeed\n", __func__); 2148 break; 2149 case USB_DWC3_SPEED_LOW: 2150 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | DWC3_DSTS_LOWSPEED); 2151 INFO("%s = LowSpeed\n", __func__); 2152 break; 2153 default: 2154 ret = USBD_FAIL; 2155 break; 2156 } 2157 2158 return ret; 2159 } 2160 2161 /* 2162 * @brief USB_DevInit : Initializes the USB3 controller registers 2163 * for device mode 2164 * @param USBx Selected device 2165 * @param cfg pointer to a USB_DWC3_CfgTypeDef structure that contains 2166 * the configuration information for the specified USBx peripheral. 2167 * @retval HAL status 2168 */ 2169 enum usb_status dwc3_dev_init(dwc3_handle_t *dwc3_handle, uint8_t speed, uint8_t intr_dev) 2170 { 2171 uint32_t reg; 2172 uint8_t i; 2173 dwc3_epcmd_params_t params; 2174 enum usb_status ret; 2175 2176 /* Setup event buffers k = USB_DWC3_INT_INUSE */ 2177 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 2178 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GEVNTADRLO(i), 2179 lower_32_bits(dwc3_handle->intbuffers.evtbuffer_dma_addr[i])); 2180 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GEVNTADRHI(i), 2181 upper_32_bits(dwc3_handle->intbuffers.evtbuffer_dma_addr[i])); 2182 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GEVNTSIZ(i), 2183 USB_DWC3_EVENT_BUFFER_SIZE); 2184 DWC3_regwrite(dwc3_handle->usb_global, DWC3_GEVNTCOUNT(i), 0); 2185 2186 /* EvtBufferPos[i] = 0; Implicit since static done in HAL */ 2187 } 2188 2189 /* 2190 * Need to set GUCTL2 RST_ACTBITLATER, so the driver can poll for CMDACT bit 2191 * when issuing the ENDTRANSFER command. 2192 */ 2193 DWC3_regupdateset(dwc3_handle->usb_global, DWC3_GUCTL2, DWC3_GUCTL2_RST_ACTBITLATER); 2194 2195 ret = dwc3_set_current_mode(dwc3_handle, USB_DWC3_DEVICE_MODE); 2196 if (ret != USBD_OK) { 2197 ERROR("%s: %d\n", __func__, __LINE__); 2198 return ret; 2199 } 2200 2201 ret = dwc3_set_dev_speed(dwc3_handle, speed); 2202 if (ret != USBD_OK) { 2203 ERROR("%s: %d\n", __func__, __LINE__); 2204 return ret; 2205 } 2206 2207 /* Issue a DEPSTARTCFG command with DEPCMD0.XferRscIdx set to 0, 2208 * to initialize the transfer resource allocation 2209 */ 2210 zeromem(¶ms, sizeof(params)); 2211 ret = dwc3_execute_dep_cmd(dwc3_handle, 0, DWC3_DEPCMD_PARAM(0) | 2212 USB_DWC3_DEPCMD_DEPSTARTCFG, ¶ms); 2213 if (ret != USBD_OK) { 2214 ERROR("%s: %d\n", __func__, __LINE__); 2215 return ret; 2216 } 2217 2218 /* Configure Control EP 0 & 1 mapped to physical EP 0 & 1*/ 2219 ret = dwc3_ep_configure(dwc3_handle, 0, false, EP_TYPE_CTRL, 2220 dwc3_handle->pcd_handle->out_ep[0].maxpacket, 0, 0, 0, 2221 dwc3_handle->OUT_ep[0].intr_num, USB_DWC3_DEPCFG_ACTION_INIT); 2222 if (ret != USBD_OK) { 2223 ERROR("%s: %d\n", __func__, __LINE__); 2224 return ret; 2225 } 2226 ret = dwc3_ep_configure(dwc3_handle, 0, true, EP_TYPE_CTRL, 2227 dwc3_handle->pcd_handle->in_ep[0].maxpacket, 0, 0, 1, 2228 dwc3_handle->IN_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 2234 /* Transfer Resource for Control EP 0 & 1*/ 2235 (void)memset(¶ms, 0x00, sizeof(params)); 2236 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1U); 2237 /* As per databook, "Issue a DEPSTARTCFG command with DEPCMD0.XferRscIdx set to 2 to 2238 * re-initialize the transfer resource allocation on SetConfiguration or SetInterface 2239 * Request", value is 2 since the EP0 & EP1 take up the transfer resource 1 & 2, hence 2240 * further EPs will take resources 2,3,&up.. But in HAL we are not decoding the 2241 * Setup-packet requests, this should ideally come from USB-MW core. 2242 * Instead we follow the same method as Linux dwc3-gadget controller driver, All hardware 2243 * endpoints can be assigned a transfer resource and this setting will stay persistent 2244 * until either a core reset or hibernation. So whenever we do a DEPSTARTCFG(0) we can go 2245 * ahead and do DEPXFERCFG for every hardware endpoint as well. 2246 * For all k = DWC3_IP_NUM_EPS 2247 */ 2248 for (i = 0; i < DWC3_IP_NUM_EPS; i++) { 2249 ret = dwc3_execute_dep_cmd(dwc3_handle, i, USB_DWC3_DEPCMD_SETTRANSFRESOURCE, 2250 ¶ms); 2251 if (ret != USBD_OK) { 2252 ERROR("%s: %d\n", __func__, __LINE__); 2253 return ret; 2254 } 2255 } 2256 2257 /* setup EP0 to receive SETUP packets */ 2258 ret = dwc3_ep0_out_start(dwc3_handle, dwc3_handle->setup_dma_addr); 2259 if (ret != USBD_OK) { 2260 ERROR("%s: %d\n", __func__, __LINE__); 2261 return ret; 2262 } 2263 2264 /* Enable EP 0 & 1 */ 2265 DWC3_regupdateset(dwc3_handle->usb_device, DWC3_DALEPENA, DWC3_DALEPENA_EP(0) | 2266 DWC3_DALEPENA_EP(1)); 2267 2268 /* 2269 * Set interrupt/EventQ number on which non-endpoint-specific device-related 2270 * interrupts are generated 2271 */ 2272 reg = DWC3_regread(dwc3_handle->usb_device, DWC3_DCFG) & ~USB3_DCFG_INTRNUM_MSK; 2273 DWC3_regwrite(dwc3_handle->usb_device, DWC3_DCFG, reg | 2274 ((uint32_t)intr_dev << USB3_DCFG_INTRNUM_POS)); 2275 2276 /* Enable all events but Start and End of Frame IRQs */ 2277 DWC3_regupdateset(dwc3_handle->usb_device, DWC3_DEVTEN, USB3_DEVTEN_VENDEVTSTRCVDEN | 2278 USB3_DEVTEN_EVNTOVERFLOWEN | USB3_DEVTEN_CMDCMPLTEN | 2279 USB3_DEVTEN_ERRTICERREVTEN | USB3_DEVTEN_U3L2L1SUSPEN | 2280 USB3_DEVTEN_HIBERNATIONREQEVTEN | USB3_DEVTEN_WKUPEVTEN | 2281 USB3_DEVTEN_ULSTCNGEN | USB3_DEVTEN_CONNECTDONEEVTEN | 2282 USB3_DEVTEN_USBRSTEVTEN | USB3_DEVTEN_DISSCONNEVTEN | 2283 USB3_DEVTEN_L1SUSPEN | USB3_DEVTEN_L1WKUPEVTEN 2284 /* | USB3_DEVTEN_SOFTEVTEN*/); 2285 2286 /* Enable Event Buffer interrupt k = USB_DWC3_INT_INUSE */ 2287 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 2288 __HAL_PCD_ENABLE_INTR(dwc3_handle, i); 2289 } 2290 2291 return USBD_OK; 2292 } 2293 2294 void usb_dwc3_init_driver(struct usb_handle *usb_core_handle, struct pcd_handle *pcd_handle, 2295 dwc3_handle_t *dwc3_handle, void *base_addr) 2296 { 2297 uint32_t i = 0; 2298 enum usb_status ret; 2299 uintptr_t base = (uintptr_t)base_addr; 2300 2301 dwc3_handle->usb_global = (usb_dwc3_global_t *) 2302 (base + USB_DWC3_GLOBAL_BASE); 2303 2304 dwc3_handle->usb_device = (usb_dwc3_device_t *) 2305 (base + USB_DWC3_DEVICE_BASE); 2306 2307 dwc3_handle->pcd_handle = pcd_handle; 2308 2309 /* Check hpcd->State is HAL_PCD_STATE_RESET, otherwise error */ 2310 assert(dwc3_handle->State == HAL_PCD_STATE_RESET); 2311 2312 dwc3_handle->State = HAL_PCD_STATE_BUSY; 2313 2314 /* Disable the Interrupts */ 2315 // Not required since USB device and EP interrupts are disabled at boot, ***ToCheck 2316 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 2317 __HAL_PCD_DISABLE_INTR(dwc3_handle, i); 2318 } 2319 2320 /* Init the Core (common init.) */ 2321 ret = dwc3_core_init(dwc3_handle, USBPHY_UTMI); 2322 if (ret != USBD_OK) { 2323 panic(); 2324 } 2325 2326 /* Init endpoints structures */ 2327 for (i = 0; i < USB_DWC3_NUM_IN_EP ; i++) { 2328 /* Init ep structure */ 2329 pcd_handle->in_ep[i].is_in = true; 2330 pcd_handle->in_ep[i].num = i; 2331 dwc3_handle->IN_ep[i].tx_fifo_num = i; 2332 /* Control until ep is activated */ 2333 pcd_handle->in_ep[i].type = EP_TYPE_CTRL; 2334 2335 /* 2336 * We are doing 1:1 alternate mapping for endpoints, meaning 2337 * IN Endpoint X maps to Physical Endpoint 2*X +1 and 2338 * OUT Endpoint X maps to Physical Endpoint 2*X. 2339 * So USB endpoint 0x81 is 0x03. 2340 */ 2341 dwc3_handle->IN_ep[i].phy_epnum = __HAL_PCD_EPADDR_TO_PHYEPNUM(i | EP_DIR_IN); 2342 2343 dwc3_handle->IN_ep[i].intr_num = PCD_DEV_EVENTS_INTR; 2344 } 2345 2346 for (i = 0; i < USB_DWC3_NUM_OUT_EP ; i++) { 2347 /* Init ep structure */ 2348 pcd_handle->out_ep[i].is_in = false; 2349 pcd_handle->out_ep[i].num = i; 2350 dwc3_handle->OUT_ep[i].tx_fifo_num = i; 2351 /* Control until ep is activated */ 2352 pcd_handle->out_ep[i].type = EP_TYPE_CTRL; 2353 2354 /* 2355 * We are doing 1:1 alternate mapping for endpoints, meaning 2356 * IN Endpoint X maps to Physical Endpoint 2*X +1 and 2357 * OUT Endpoint X maps to Physical Endpoint 2*X. 2358 * So USB endpoint 0x81 is 0x03. 2359 */ 2360 dwc3_handle->OUT_ep[i].phy_epnum = __HAL_PCD_EPADDR_TO_PHYEPNUM(i); 2361 2362 dwc3_handle->OUT_ep[i].intr_num = PCD_DEV_EVENTS_INTR; 2363 2364 dwc3_handle->OUT_ep[i].bounce_buf = dwc3_handle->bounce_bufs[i].bounce_buf; 2365 } 2366 2367 #define PHYS_AREA STM32MP_USB_DWC3_BASE 2368 2369 #define EVTBUF_AREA_OFFSET 0U 2370 #define TRB_OUT_AREA_OFFSET (EVTBUF_AREA_OFFSET + USB_DWC3_EVENT_BUFFER_SIZE) 2371 #define TRB_IN_AREA_OFFSET (TRB_OUT_AREA_OFFSET + sizeof(usb_dwc3_trb_t)) 2372 #define SETUP_AREA_OFFSET (TRB_IN_AREA_OFFSET + sizeof(usb_dwc3_trb_t)) 2373 2374 void *coh_area = (void *)(uintptr_t)PHYS_AREA; 2375 2376 #define EVTBUF_AREA (coh_area + EVTBUF_AREA_OFFSET) 2377 #define TRB_OUT_AREA (coh_area + TRB_OUT_AREA_OFFSET) 2378 #define TRB_IN_AREA (coh_area + TRB_IN_AREA_OFFSET) 2379 #define SETUP_AREA (coh_area + SETUP_AREA_OFFSET) 2380 2381 dwc3_handle->setup_dma_addr = (uintptr_t)api_getdmaaddr((void *)SETUP_AREA, 2382 USB_SETUP_PACKET_SIZE, 1); 2383 assert(dwc3_handle->setup_dma_addr != 0U); 2384 2385 dwc3_handle->setup_addr = SETUP_AREA; 2386 assert(dwc3_handle->setup_addr != NULL); 2387 2388 /* Map DMA and Coherent address for event buffers k = USB_DWC3_INT_INUSE */ 2389 for (i = 0; i < USB_DWC3_INT_INUSE; i++) { 2390 dwc3_handle->intbuffers.evtbuffer_dma_addr[i] = 2391 api_getdmaaddr((void *)EVTBUF_AREA, USB_DWC3_EVENT_BUFFER_SIZE, 1); 2392 assert(dwc3_handle->intbuffers.evtbuffer_dma_addr[i] != 0U); 2393 2394 dwc3_handle->intbuffers.evtbuffer_addr[i] = EVTBUF_AREA; 2395 assert(dwc3_handle->intbuffers.evtbuffer_addr[i] != NULL); 2396 2397 dwc3_handle->intbuffers.evtbufferpos[i] = 0; 2398 2399 INFO("EventBuffer%u: BuffArea=%lx DmaAddr=%08x CoherentMapAddr=%p\n", i, 2400 (PHYS_AREA + EVTBUF_AREA_OFFSET), 2401 (uint32_t)dwc3_handle->intbuffers.evtbuffer_dma_addr[i], 2402 dwc3_handle->intbuffers.evtbuffer_addr[i]); 2403 } 2404 2405 /* MAP TRB Coherent and DMA address for EP0IN and EP0OUT */ 2406 dwc3_handle->IN_ep[0].trb_dma_addr = (uint32_t)api_getdmaaddr((void *)TRB_IN_AREA, 2407 sizeof(usb_dwc3_trb_t), 1); 2408 assert(dwc3_handle->IN_ep[0].trb_dma_addr != 0U); 2409 2410 dwc3_handle->IN_ep[0].trb_addr = (usb_dwc3_trb_t *)TRB_IN_AREA; 2411 assert(dwc3_handle->IN_ep[0].trb_addr != NULL); 2412 2413 dwc3_handle->OUT_ep[0].trb_dma_addr = (uint32_t)api_getdmaaddr((void *)TRB_OUT_AREA, 2414 sizeof(usb_dwc3_trb_t), 2415 1); 2416 assert(dwc3_handle->OUT_ep[0].trb_dma_addr != 0U); 2417 2418 dwc3_handle->OUT_ep[0].trb_addr = (usb_dwc3_trb_t *)TRB_OUT_AREA; 2419 assert(dwc3_handle->OUT_ep[0].trb_addr != NULL); 2420 2421 /* Init Device */ 2422 dwc3_handle->EP0_State = HAL_PCD_EP0_SETUP_QUEUED; 2423 2424 ret = dwc3_dev_init(dwc3_handle, USB_DWC3_SPEED_HIGH, PCD_DEV_EVENTS_INTR); 2425 assert(ret == USBD_OK); 2426 2427 dwc3_handle->State = HAL_PCD_STATE_READY; 2428 2429 register_usb_driver(usb_core_handle, pcd_handle, &usb_dwc3driver, 2430 dwc3_handle); 2431 } 2432