xref: /rk3399_ARM-atf/include/drivers/st/usb_dwc3.h (revision c8e1a2d9d27d4f7e3a919b7994e82f2a886f3e6a)
1*867cd155SPankaj Dev /*
2*867cd155SPankaj Dev  * Copyright (c) 2015-2025, STMicroelectronics - All Rights Reserved
3*867cd155SPankaj Dev  *
4*867cd155SPankaj Dev  * SPDX-License-Identifier: BSD-3-Clause
5*867cd155SPankaj Dev  */
6*867cd155SPankaj Dev 
7*867cd155SPankaj Dev #ifndef __USB_DWC3_H
8*867cd155SPankaj Dev #define __USB_DWC3_H
9*867cd155SPankaj Dev 
10*867cd155SPankaj Dev #include <drivers/usb_device.h>
11*867cd155SPankaj Dev #include <platform_def.h>
12*867cd155SPankaj Dev 
13*867cd155SPankaj Dev /* define value use in register */
14*867cd155SPankaj Dev 
15*867cd155SPankaj Dev #define USB3_HS_MAX_PACKET_SIZE     512U
16*867cd155SPankaj Dev #define USB3_FS_MAX_PACKET_SIZE     64U
17*867cd155SPankaj Dev #ifdef USE_USB_FS
18*867cd155SPankaj Dev #define USB3_MAX_PACKET_SIZE        USB3_FS_MAX_PACKET_SIZE
19*867cd155SPankaj Dev #else
20*867cd155SPankaj Dev #define USB3_MAX_PACKET_SIZE        USB3_HS_MAX_PACKET_SIZE
21*867cd155SPankaj Dev #endif
22*867cd155SPankaj Dev #define USB_SETUP_PACKET_SIZE       12U
23*867cd155SPankaj Dev 
24*867cd155SPankaj Dev /* USB_DWC3_EPTypeDef->trb_flag */
25*867cd155SPankaj Dev #define USB_DWC3_TRBCTL_NORMAL            1
26*867cd155SPankaj Dev #define USB_DWC3_TRBCTL_CONTROL_SETUP     2
27*867cd155SPankaj Dev #define USB_DWC3_TRBCTL_CONTROL_STATUS2   3
28*867cd155SPankaj Dev #define USB_DWC3_TRBCTL_CONTROL_STATUS3   4
29*867cd155SPankaj Dev #define USB_DWC3_TRBCTL_CONTROL_DATA      5
30*867cd155SPankaj Dev #define USB_DWC3_TRBCTL_ISOCHRONOUS_FIRST 6
31*867cd155SPankaj Dev #define USB_DWC3_TRBCTL_ISOCHRONOUS       7
32*867cd155SPankaj Dev #define USB_DWC3_TRBCTL_LINK_TRB          8
33*867cd155SPankaj Dev #define USB_DWC3_TRBCTL_MASK              0x3fUL
34*867cd155SPankaj Dev 
35*867cd155SPankaj Dev /* USB_DWC3_EPTypeDef->flags */
36*867cd155SPankaj Dev #define USB_DWC3_EP_REQ_QUEUED          BIT(0)
37*867cd155SPankaj Dev #define USB_DWC3_EP_ISOC_START_PENDING  BIT(1)
38*867cd155SPankaj Dev #define USB_DWC3_EP_ISOC_STARTED        BIT(2)
39*867cd155SPankaj Dev 
40*867cd155SPankaj Dev /*
41*867cd155SPankaj Dev  * DWC3 Specific Configurable macros
42*867cd155SPankaj Dev  */
43*867cd155SPankaj Dev #define USB_DWC3_INT_INUSE          1U
44*867cd155SPankaj Dev #define USB_DWC3_EVENT_BUFFER_SIZE  2048U
45*867cd155SPankaj Dev #define USB_DWC3_NUM_IN_EP          USBD_EP_NB
46*867cd155SPankaj Dev #define USB_DWC3_NUM_OUT_EP         USBD_EP_NB
47*867cd155SPankaj Dev 
48*867cd155SPankaj Dev typedef struct {
49*867cd155SPankaj Dev 	uint32_t dcfg;             /* @ offset 0x0 */
50*867cd155SPankaj Dev 	uint32_t dctl;             /* @ offset 0x4 */
51*867cd155SPankaj Dev 	uint32_t devten;           /* @ offset 0x8 */
52*867cd155SPankaj Dev 	uint32_t dsts;             /* @ offset 0xc */
53*867cd155SPankaj Dev 	uint32_t dgcmdpar;         /* @ offset 0x10 */
54*867cd155SPankaj Dev 	uint32_t dgcmd;            /* @ offset 0x14 */
55*867cd155SPankaj Dev 	uint32_t reserved_18[2];   /* Reserved @ offset 0x18 */
56*867cd155SPankaj Dev 	uint32_t dalepena;         /* @ offset 0x20 */
57*867cd155SPankaj Dev 	uint32_t reserved_24;      /* @ offset 0x24 */
58*867cd155SPankaj Dev 	uint32_t reserved_28[54];  /* Reserved @ offset 0x28 */
59*867cd155SPankaj Dev 	uint32_t depcmdpar2;       /* @ offset 0x100 */
60*867cd155SPankaj Dev 	uint32_t depcmdpar1;       /* @ offset 0x104 */
61*867cd155SPankaj Dev 	uint32_t depcmdpar0;       /* @ offset 0x108 */
62*867cd155SPankaj Dev 	uint32_t depcmd;           /* @ offset 0x10c */
63*867cd155SPankaj Dev 	uint32_t reserved_110[124];/* Reserved @ offset 0x110 */
64*867cd155SPankaj Dev 	uint32_t dev_imod;         /* @ offset 0x300 */
65*867cd155SPankaj Dev 	uint32_t reserved_304[0xfc / 4]; /* Reserved @ offset 0x304 */
66*867cd155SPankaj Dev } usb_dwc3_device_t;
67*867cd155SPankaj Dev 
68*867cd155SPankaj Dev typedef struct {
69*867cd155SPankaj Dev 	uint32_t gsbuscfg0;         /* @ offset 0x0 */
70*867cd155SPankaj Dev 	uint32_t gsbuscfg1;         /* @ offset 0x4 */
71*867cd155SPankaj Dev 	uint32_t gtxthrcfg;         /* @ offset 0x8 */
72*867cd155SPankaj Dev 	uint32_t grxthrcfg;         /* @ offset 0xc */
73*867cd155SPankaj Dev 	uint32_t gctl;              /* @ offset 0x10 */
74*867cd155SPankaj Dev 	uint32_t gpmsts;            /* @ offset 0x14 */
75*867cd155SPankaj Dev 	uint32_t gsts;              /* @ offset 0x18 */
76*867cd155SPankaj Dev 	uint32_t guctl1;            /* @ offset 0x1c */
77*867cd155SPankaj Dev 	uint32_t gsnpsid;           /* @ offset 0x20 */
78*867cd155SPankaj Dev 	uint32_t ggpio;             /* @ offset 0x24 */
79*867cd155SPankaj Dev 	uint32_t guid;              /* @ offset 0x28 */
80*867cd155SPankaj Dev 	uint32_t guctl;             /* @ offset 0x2c */
81*867cd155SPankaj Dev 	uint32_t gbuserraddrlo;     /* @ offset 0x30 */
82*867cd155SPankaj Dev 	uint32_t gbuserraddrhi;     /* @ offset 0x34 */
83*867cd155SPankaj Dev 	uint32_t gprtbimaplo;       /* @ offset 0x38 */
84*867cd155SPankaj Dev 	uint32_t gprtbimaphi;       /* @ offset 0x3c */
85*867cd155SPankaj Dev 	uint32_t ghwparams0;        /* @ offset 0x40 */
86*867cd155SPankaj Dev 	uint32_t ghwparams1;        /* @ offset 0x44 */
87*867cd155SPankaj Dev 	uint32_t ghwparams2;        /* @ offset 0x48 */
88*867cd155SPankaj Dev 	uint32_t ghwparams3;        /* @ offset 0x4c */
89*867cd155SPankaj Dev 	uint32_t ghwparams4;        /* @ offset 0x50 */
90*867cd155SPankaj Dev 	uint32_t ghwparams5;        /* @ offset 0x54 */
91*867cd155SPankaj Dev 	uint32_t ghwparams6;        /* @ offset 0x58 */
92*867cd155SPankaj Dev 	uint32_t ghwparams7;        /* @ offset 0x5c */
93*867cd155SPankaj Dev 	uint32_t gdbgfifospace;     /* @ offset 0x60 */
94*867cd155SPankaj Dev 	uint32_t gdbgltssm;         /* @ offset 0x64 */
95*867cd155SPankaj Dev 	uint32_t gdbglnmcc;         /* @ offset 0x68 */
96*867cd155SPankaj Dev 	uint32_t gdbgbmu;           /* @ offset 0x6c */
97*867cd155SPankaj Dev 	uint32_t gdbglspmux_hst;    /* @ offset 0x70 */
98*867cd155SPankaj Dev 	uint32_t gdbglsp;           /* @ offset 0x74 */
99*867cd155SPankaj Dev 	uint32_t gdbgepinfo0;       /* @ offset 0x78 */
100*867cd155SPankaj Dev 	uint32_t gdbgepinfo1;       /* @ offset 0x7c */
101*867cd155SPankaj Dev 	uint32_t gprtbimap_hslo;    /* @ offset 0x80 */
102*867cd155SPankaj Dev 	uint32_t gprtbimap_hshi;    /* @ offset 0x84 */
103*867cd155SPankaj Dev 	uint32_t gprtbimap_fslo;    /* @ offset 0x88 */
104*867cd155SPankaj Dev 	uint32_t gprtbimap_fshi;    /* @ offset 0x8c */
105*867cd155SPankaj Dev 	uint32_t reserved_90;       /* Reserved @ offset 0x90 */
106*867cd155SPankaj Dev 	uint32_t reserved_94;       /* @ offset 0x94 */
107*867cd155SPankaj Dev 	uint32_t reserved_98;       /* @ offset 0x98 */
108*867cd155SPankaj Dev 	uint32_t guctl2;            /* @ offset 0x9c */
109*867cd155SPankaj Dev 	uint32_t reserved_A0[24];   /* Reserved @ offset 0xa0 */
110*867cd155SPankaj Dev 	uint32_t gusb2phycfg;       /* @ offset 0x100 */
111*867cd155SPankaj Dev 	uint32_t reserved_104[15];  /* Reserved @ offset 0x104 */
112*867cd155SPankaj Dev 	uint32_t gusb2i2cctl;       /* @ offset 0x140 */
113*867cd155SPankaj Dev 	uint32_t reserved_144[15];  /* Reserved @ offset 0x144 */
114*867cd155SPankaj Dev 	uint32_t gusb2phyacc_ulpi;  /* @ offset 0x180 */
115*867cd155SPankaj Dev 	uint32_t reserved_184[15];  /* Reserved @ offset 0x184 */
116*867cd155SPankaj Dev 	uint32_t gusb3pipectl;      /* @ offset 0x1c0 */
117*867cd155SPankaj Dev 	uint32_t reserved_1c4[15];  /* Reserved @ offset 0x1c4 */
118*867cd155SPankaj Dev 	uint32_t gtxfifosiz0;       /* @ offset 0x200 */
119*867cd155SPankaj Dev 	uint32_t gtxfifosiz1;       /* @ offset 0x204 */
120*867cd155SPankaj Dev 	uint32_t gtxfifosiz2;       /* @ offset 0x208 */
121*867cd155SPankaj Dev 	uint32_t gtxfifosiz3;       /* @ offset 0x20c */
122*867cd155SPankaj Dev 	uint32_t gtxfifosiz4;       /* @ offset 0x210 */
123*867cd155SPankaj Dev 	uint32_t gtxfifosiz5;       /* @ offset 0x214 */
124*867cd155SPankaj Dev 	uint32_t gtxfifosiz6;       /* @ offset 0x218 */
125*867cd155SPankaj Dev 	uint32_t gtxfifosiz7;       /* @ offset 0x21c */
126*867cd155SPankaj Dev 	uint32_t gtxfifosiz8;       /* @ offset 0x220 */
127*867cd155SPankaj Dev 	uint32_t gtxfifosiz9;       /* @ offset 0x224 */
128*867cd155SPankaj Dev 	uint32_t gtxfifosiz10;      /* @ offset 0x228 */
129*867cd155SPankaj Dev 	uint32_t gtxfifosiz11;      /* @ offset 0x22c */
130*867cd155SPankaj Dev 	uint32_t reserved_230[20];  /* Reserved @ offset 0x230 */
131*867cd155SPankaj Dev 	uint32_t grxfifosiz0;       /* @ offset 0x280 */
132*867cd155SPankaj Dev 	uint32_t grxfifosiz1;       /* @ offset 0x284 */
133*867cd155SPankaj Dev 	uint32_t grxfifosiz2;       /* @ offset 0x288 */
134*867cd155SPankaj Dev 	uint32_t reserved_28c[29];  /* Reserved @ offset 0x28c */
135*867cd155SPankaj Dev 	uint32_t gevntadrlo;        /* @ offset 0x300 */
136*867cd155SPankaj Dev 	uint32_t gevntadrhi;        /* @ offset 0x304 */
137*867cd155SPankaj Dev 	uint32_t gevntsiz;          /* @ offset 0x308 */
138*867cd155SPankaj Dev 	uint32_t gevntcount;        /* @ offset 0x30c */
139*867cd155SPankaj Dev 	uint32_t reserved_310[124]; /* Reserved @ offset 0x310 */
140*867cd155SPankaj Dev 	uint32_t ghwparams8;        /* @ offset 0x500 */
141*867cd155SPankaj Dev 	uint32_t reserved_504[3];   /* Reserved @ offset 0x504 */
142*867cd155SPankaj Dev 	uint32_t gtxfifopridev;     /* @ offset 0x510 */
143*867cd155SPankaj Dev 	uint32_t reserved_514;      /* Reserved @ offset 0x514 */
144*867cd155SPankaj Dev 	uint32_t gtxfifoprihst;     /* @ offset 0x518 */
145*867cd155SPankaj Dev 	uint32_t grxfifoprihst;     /* @ offset 0x51c */
146*867cd155SPankaj Dev 	uint32_t reserved_520;      /* Reserved @ offset 0x520 */
147*867cd155SPankaj Dev 	uint32_t gdmahlratio;       /* @ offset 0x524 */
148*867cd155SPankaj Dev 	uint32_t reserved_528[2];   /* Reserved @ offset 0x528 */
149*867cd155SPankaj Dev 	uint32_t gfladj;            /* @ offset 0x530 */
150*867cd155SPankaj Dev 	uint32_t reserved_534[0xcc / 4];  /* Reserved @ offset 0x534 */
151*867cd155SPankaj Dev } usb_dwc3_global_t;
152*867cd155SPankaj Dev 
153*867cd155SPankaj Dev typedef struct {
154*867cd155SPankaj Dev 	uint32_t bpl;      /* Buffer Pointer Low Address */
155*867cd155SPankaj Dev 	uint32_t bph;      /* Buffer Pointer High Address */
156*867cd155SPankaj Dev 	uint32_t size;     /* Buffer Size */
157*867cd155SPankaj Dev 	uint32_t ctrl;     /* Control and Status field */
158*867cd155SPankaj Dev } __packed usb_dwc3_trb_t __aligned(16);
159*867cd155SPankaj Dev 
160*867cd155SPankaj Dev #define CACHED_MEMORY
161*867cd155SPankaj Dev #define CACHE_LINE_SIZE CACHE_WRITEBACK_GRANULE
162*867cd155SPankaj Dev 
163*867cd155SPankaj Dev typedef struct {
164*867cd155SPankaj Dev 	bool is_stall;				/* Endpoint stall condition */
165*867cd155SPankaj Dev 
166*867cd155SPankaj Dev 	volatile usb_dwc3_trb_t *trb_addr; /* Trb descriptor coherent address */
167*867cd155SPankaj Dev 	uintptr_t trb_dma_addr;   /* 32 bits aligned trb bus address */
168*867cd155SPankaj Dev 	uint8_t trb_flag;         /* TRB Control Flag */
169*867cd155SPankaj Dev 	uint8_t flags;            /* Endpoint state flags condition */
170*867cd155SPankaj Dev 	uint8_t phy_epnum;        /* physical EP number 0 - 31 */
171*867cd155SPankaj Dev 	uint8_t intr_num;         /* Interrupt number to get events for this EP */
172*867cd155SPankaj Dev 	/* to be used for receiving packet < EP-Max-Packet-Size */
173*867cd155SPankaj Dev 	uint8_t *bounce_buf;
174*867cd155SPankaj Dev 	/* Transmission FIFO number. Number between Min_Data = 1 and Max_Data = 15 */
175*867cd155SPankaj Dev 	uint8_t tx_fifo_num;
176*867cd155SPankaj Dev 	uint8_t resc_idx;       /* Resource index */
177*867cd155SPankaj Dev 	uintptr_t dma_addr;     /* 32 bits aligned transfer buffer address */
178*867cd155SPankaj Dev 	/* Pointer to destination buffer, used for receive case, where bounce_buf is used */
179*867cd155SPankaj Dev 	uint8_t *xfer_dest_buff;
180*867cd155SPankaj Dev 	/* length of destination buffer, used for receive case, where bounce_buf is used */
181*867cd155SPankaj Dev 	uint32_t xfer_dest_len;
182*867cd155SPankaj Dev } usb_dwc3_endpoint_t;
183*867cd155SPankaj Dev 
184*867cd155SPankaj Dev typedef struct {
185*867cd155SPankaj Dev 	uint8_t bounce_buf[USB3_MAX_PACKET_SIZE];  /* Event Buffer area */
186*867cd155SPankaj Dev } usb_dwc3_endpoint_bouncebuf_t __aligned(CACHE_LINE_SIZE);
187*867cd155SPankaj Dev 
188*867cd155SPankaj Dev /* Aligned to make EvtBuffer start address evtbuffer-size aligned */
189*867cd155SPankaj Dev typedef struct {
190*867cd155SPankaj Dev 	uint8_t EvtBufferArea[USB_DWC3_EVENT_BUFFER_SIZE];  /* Event Buffer area */
191*867cd155SPankaj Dev } PCD_intbufferareadef __aligned(USB_DWC3_EVENT_BUFFER_SIZE);
192*867cd155SPankaj Dev 
193*867cd155SPankaj Dev typedef struct {
194*867cd155SPankaj Dev 	/* shield needed for the cache line (8 words) intersection with other members of structure,
195*867cd155SPankaj Dev 	 * only when USB_DWC3_EVENT_BUFFER_SIZE is non-cache_line aligned
196*867cd155SPankaj Dev 	 */
197*867cd155SPankaj Dev 
198*867cd155SPankaj Dev 	uint8_t *evtbuffer_addr[USB_DWC3_INT_INUSE]; /* Event Buffer coherent addr */
199*867cd155SPankaj Dev 	uintptr_t evtbuffer_dma_addr[USB_DWC3_INT_INUSE]; /* Event Buffer dma addr */
200*867cd155SPankaj Dev 	uint32_t evtbufferpos[USB_DWC3_INT_INUSE];   /*!< Read Position inside the Event Buffer */
201*867cd155SPankaj Dev } PCD_intbuffersdef;
202*867cd155SPankaj Dev 
203*867cd155SPankaj Dev typedef enum {
204*867cd155SPankaj Dev 	HAL_PCD_STATE_RESET   = 0x00U,
205*867cd155SPankaj Dev 	HAL_PCD_STATE_READY   = 0x01U,
206*867cd155SPankaj Dev 	HAL_PCD_STATE_ERROR   = 0x02U,
207*867cd155SPankaj Dev 	HAL_PCD_STATE_BUSY    = 0x03U,
208*867cd155SPankaj Dev 	HAL_PCD_STATE_TIMEOUT = 0x04U
209*867cd155SPankaj Dev } usb_dwc3_state_t;
210*867cd155SPankaj Dev 
211*867cd155SPankaj Dev typedef enum {
212*867cd155SPankaj Dev 	HAL_PCD_EP0_SETUP_COMPLETED = 0x00U,
213*867cd155SPankaj Dev 	HAL_PCD_EP0_SETUP_QUEUED  = 0x01U
214*867cd155SPankaj Dev } usb_dwc3_ep0state_t;
215*867cd155SPankaj Dev 
216*867cd155SPankaj Dev typedef struct {
217*867cd155SPankaj Dev 	/*
218*867cd155SPankaj Dev 	 * Kept at top to avoid struct size wastage since bounce_buf
219*867cd155SPankaj Dev 	 * needs to be CACHE_LINE_SIZE aligned
220*867cd155SPankaj Dev 	 */
221*867cd155SPankaj Dev 	usb_dwc3_endpoint_bouncebuf_t bounce_bufs[USB_DWC3_NUM_OUT_EP];
222*867cd155SPankaj Dev 	PCD_intbuffersdef intbuffers;
223*867cd155SPankaj Dev 	usb_dwc3_global_t *usb_global;
224*867cd155SPankaj Dev 	usb_dwc3_device_t *usb_device;
225*867cd155SPankaj Dev 	usb_dwc3_endpoint_t IN_ep[USB_DWC3_NUM_IN_EP];
226*867cd155SPankaj Dev 	usb_dwc3_endpoint_t OUT_ep[USB_DWC3_NUM_OUT_EP];
227*867cd155SPankaj Dev 
228*867cd155SPankaj Dev 	usb_dwc3_state_t State;      /*!< PCD communication state            */
229*867cd155SPankaj Dev 	usb_dwc3_ep0state_t EP0_State;  /*!< EP0 State */
230*867cd155SPankaj Dev 
231*867cd155SPankaj Dev 	uint8_t *setup_addr;  /*!< Setup packet coherent address    */
232*867cd155SPankaj Dev 	uintptr_t setup_dma_addr;  /*!< Setup packet buffer DMA Bus address */
233*867cd155SPankaj Dev 
234*867cd155SPankaj Dev 	struct pcd_handle *pcd_handle;
235*867cd155SPankaj Dev } dwc3_handle_t;
236*867cd155SPankaj Dev 
237*867cd155SPankaj Dev void usb_dwc3_init_driver(struct usb_handle *usb_core_handle,
238*867cd155SPankaj Dev 			  struct pcd_handle *pcd_handle, dwc3_handle_t *dwc3_handle,
239*867cd155SPankaj Dev 			  void *base_addr);
240*867cd155SPankaj Dev 
241*867cd155SPankaj Dev #endif /* __USB_DWC3_H */
242