1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __USB_DEVICE_EHCI_H__
10 #define __USB_DEVICE_EHCI_H__
11 
12 /*!
13  * @addtogroup usb_device_controller_ehci_driver
14  * @{
15  */
16 
17 /*******************************************************************************
18  * Definitions
19  ******************************************************************************/
20 
21 /*! @brief The maximum value of ISO type maximum packet size for HS in USB specification 2.0 */
22 #define USB_DEVICE_MAX_HS_ISO_MAX_PACKET_SIZE (1024U)
23 
24 /*! @brief The maximum value of interrupt type maximum packet size for HS in USB specification 2.0 */
25 #define USB_DEVICE_MAX_HS_INTERUPT_MAX_PACKET_SIZE (1024U)
26 
27 /*! @brief The maximum value of bulk type maximum packet size for HS in USB specification 2.0 */
28 #define USB_DEVICE_MAX_HS_BULK_MAX_PACKET_SIZE (512U)
29 
30 /*! @brief The maximum value of control type maximum packet size for HS in USB specification 2.0 */
31 #define USB_DEVICE_MAX_HS_CONTROL_MAX_PACKET_SIZE (64U)
32 
33 #define USB_DEVICE_MAX_TRANSFER_PRIME_TIMES \
34     (10000000U) /* The max prime times of EPPRIME, if still doesn't take effect, means status has been reset*/
35 
36 /* Device QH */
37 #define USB_DEVICE_EHCI_QH_POINTER_MASK         (0xFFFFFFC0U)
38 #define USB_DEVICE_EHCI_QH_MULT_MASK            (0xC0000000U)
39 #define USB_DEVICE_EHCI_QH_ZLT_MASK             (0x20000000U)
40 #define USB_DEVICE_EHCI_QH_MAX_PACKET_SIZE_MASK (0x07FF0000U)
41 #define USB_DEVICE_EHCI_QH_MAX_PACKET_SIZE      (0x00000800U)
42 #define USB_DEVICE_EHCI_QH_IOS_MASK             (0x00008000U)
43 
44 /* Device DTD */
45 #define USB_DEVICE_ECHI_DTD_POINTER_MASK             (0xFFFFFFE0U)
46 #define USB_DEVICE_ECHI_DTD_TERMINATE_MASK           (0x00000001U)
47 #define USB_DEVICE_ECHI_DTD_PAGE_MASK                (0xFFFFF000U)
48 #define USB_DEVICE_ECHI_DTD_PAGE_OFFSET_MASK         (0x00000FFFU)
49 #define USB_DEVICE_ECHI_DTD_PAGE_BLOCK               (0x00001000U)
50 #define USB_DEVICE_ECHI_DTD_TOTAL_BYTES_MASK         (0x7FFF0000U)
51 #define USB_DEVICE_ECHI_DTD_TOTAL_BYTES              (0x00004000U)
52 #define USB_DEVICE_ECHI_DTD_IOC_MASK                 (0x00008000U)
53 #define USB_DEVICE_ECHI_DTD_MULTIO_MASK              (0x00000C00U)
54 #define USB_DEVICE_ECHI_DTD_STATUS_MASK              (0x000000FFU)
55 #define USB_DEVICE_EHCI_DTD_STATUS_ERROR_MASK        (0x00000068U)
56 #define USB_DEVICE_ECHI_DTD_STATUS_ACTIVE            (0x00000080U)
57 #define USB_DEVICE_ECHI_DTD_STATUS_HALTED            (0x00000040U)
58 #define USB_DEVICE_ECHI_DTD_STATUS_DATA_BUFFER_ERROR (0x00000020U)
59 #define USB_DEVICE_ECHI_DTD_STATUS_TRANSACTION_ERROR (0x00000008U)
60 
61 typedef struct _usb_device_ehci_qh_struct
62 {
63     union
64     {
65         volatile uint32_t capabilttiesCharacteristics;
66         struct
67         {
68             volatile uint32_t reserved1 : 15;
69             volatile uint32_t ios : 1;
70             volatile uint32_t maxPacketSize : 11;
71             volatile uint32_t reserved2 : 2;
72             volatile uint32_t zlt : 1;
73             volatile uint32_t mult : 2;
74         } capabilttiesCharacteristicsBitmap;
75     } capabilttiesCharacteristicsUnion;
76     volatile uint32_t currentDtdPointer;
77     volatile uint32_t nextDtdPointer;
78     union
79     {
80         volatile uint32_t dtdToken;
81         struct
82         {
83             volatile uint32_t status : 8;
84             volatile uint32_t reserved1 : 2;
85             volatile uint32_t multiplierOverride : 2;
86             volatile uint32_t reserved2 : 3;
87             volatile uint32_t ioc : 1;
88             volatile uint32_t totalBytes : 15;
89             volatile uint32_t reserved3 : 1;
90         } dtdTokenBitmap;
91     } dtdTokenUnion;
92     volatile uint32_t bufferPointerPage[5];
93     volatile uint32_t reserved1;
94     uint32_t setupBuffer[2];
95     uint32_t setupBufferBack[2];
96     union
97     {
98         uint32_t endpointStatus;
99         struct
100         {
101             uint32_t isOpened : 1;
102             uint32_t zlt : 1;
103             uint32_t : 30;
104         } endpointStatusBitmap;
105     } endpointStatusUnion;
106     uint32_t reserved2;
107 } usb_device_ehci_qh_struct_t;
108 
109 typedef struct _usb_device_ehci_dtd_struct
110 {
111     volatile uint32_t nextDtdPointer;
112     union
113     {
114         volatile uint32_t dtdToken;
115         struct
116         {
117             volatile uint32_t status : 8;
118             volatile uint32_t reserved1 : 2;
119             volatile uint32_t multiplierOverride : 2;
120             volatile uint32_t reserved2 : 3;
121             volatile uint32_t ioc : 1;
122             volatile uint32_t totalBytes : 15;
123             volatile uint32_t reserved3 : 1;
124         } dtdTokenBitmap;
125     } dtdTokenUnion;
126     volatile uint32_t bufferPointerPage[5];
127     union
128     {
129         volatile uint32_t reserved;
130         struct
131         {
132             uint32_t originalBufferOffest : 12;
133             uint32_t originalBufferLength : 19;
134             uint32_t dtdInvalid : 1;
135         } originalBufferInfo;
136     } reservedUnion;
137 } usb_device_ehci_dtd_struct_t;
138 
139 /*! @brief EHCI state structure */
140 typedef struct _usb_device_ehci_state_struct
141 {
142     usb_device_struct_t *deviceHandle; /*!< Device handle used to identify the device object is belonged to */
143     USBHS_Type *registerBase;          /*!< The base address of the register */
144 #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
145 #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
146     USBPHY_Type *registerPhyBase; /*!< The base address of the PHY register */
147 #endif
148 #if (defined(FSL_FEATURE_SOC_USBNC_COUNT) && (FSL_FEATURE_SOC_USBNC_COUNT > 0U))
149     USBNC_Type *registerNcBase; /*!< The base address of the USBNC register */
150 #endif
151 #endif
152 #if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
153     ((defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U)) ||    \
154      (defined(FSL_FEATURE_SOC_USB_ANALOG_COUNT) && (FSL_FEATURE_SOC_USB_ANALOG_COUNT > 0U)))
155     void *dcdHandle; /*!< Dcd handle used to identify the device object belongs to */
156 #endif
157     usb_device_ehci_qh_struct_t *qh;       /*!< The QH structure base address */
158     usb_device_ehci_dtd_struct_t *dtd;     /*!< The DTD structure base address */
159     usb_device_ehci_dtd_struct_t *dtdFree; /*!< The idle DTD list head */
160     usb_device_ehci_dtd_struct_t
161         *dtdHard[USB_DEVICE_CONFIG_ENDPOINTS * 2]; /*!< The transferring DTD list head for each endpoint */
162     usb_device_ehci_dtd_struct_t
163         *dtdTail[USB_DEVICE_CONFIG_ENDPOINTS * 2]; /*!< The transferring DTD list tail for each endpoint */
164     uint8_t dtdCount;                              /*!< The idle DTD node count */
165     uint8_t endpointCount;                         /*!< The endpoint number of EHCI */
166     uint8_t isResetting;                           /*!< Whether a PORT reset is occurring or not  */
167     uint8_t controllerId;                          /*!< Controller ID */
168     uint8_t speed;                                 /*!< Current speed of EHCI */
169     uint8_t isSuspending;                          /*!< Is suspending of the PORT */
170 #if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
171 #if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
172     uint8_t lpmRemoteWakeUp;
173 #endif
174 #endif
175 } usb_device_ehci_state_struct_t;
176 
177 #if defined(__cplusplus)
178 extern "C" {
179 #endif
180 
181 /*!
182  * @name USB device EHCI functions
183  * @{
184  */
185 
186 /*******************************************************************************
187  * API
188  ******************************************************************************/
189 
190 /*!
191  * @brief Initializes the USB device EHCI instance.
192  *
193  * This function initializes the USB device EHCI module specified by the controllerId.
194  *
195  * @param[in] controllerId The controller ID of the USB IP. See the enumeration type usb_controller_index_t.
196  * @param[in] handle        Pointer of the device handle used to identify the device object is belonged to.
197  * @param[out] ehciHandle   An out parameter used to return the pointer of the device EHCI handle to the caller.
198  *
199  * @return A USB error code or kStatus_USB_Success.
200  */
201 usb_status_t USB_DeviceEhciInit(uint8_t controllerId,
202                                 usb_device_handle handle,
203                                 usb_device_controller_handle *ehciHandle);
204 
205 /*!
206  * @brief Deinitializes the USB device EHCI instance.
207  *
208  * This function deinitializes the USB device EHCI module.
209  *
210  * @param[in] ehciHandle   Pointer of the device EHCI handle.
211  *
212  * @return A USB error code or kStatus_USB_Success.
213  */
214 usb_status_t USB_DeviceEhciDeinit(usb_device_controller_handle ehciHandle);
215 
216 /*!
217  * @brief Sends data through a specified endpoint.
218  *
219  * This function sends data through a specified endpoint.
220  *
221  * @param[in] ehciHandle      Pointer of the device EHCI handle.
222  * @param[in] endpointAddress Endpoint index.
223  * @param[in] buffer           The memory address to hold the data need to be sent.
224  * @param[in] length           The data length to be sent.
225  *
226  * @return A USB error code or kStatus_USB_Success.
227  *
228  * @note The return value means whether the sending request is successful or not. The transfer completion is indicated
229  * by the
230  * corresponding callback function.
231  * Currently, only one transfer request can be supported for a specific endpoint.
232  * If there is a specific requirement to support multiple transfer requests for a specific endpoint, the application
233  * should implement a queue in the application level.
234  * The subsequent transfer can begin only when the previous transfer is done (a notification is received through the
235  * endpoint
236  * callback).
237  */
238 usb_status_t USB_DeviceEhciSend(usb_device_controller_handle ehciHandle,
239                                 uint8_t endpointAddress,
240                                 uint8_t *buffer,
241                                 uint32_t length);
242 
243 /*!
244  * @brief Receive data through a specified endpoint.
245  *
246  * This function Receives data through a specified endpoint.
247  *
248  * @param[in] ehciHandle      Pointer of the device EHCI handle.
249  * @param[in] endpointAddress Endpoint index.
250  * @param[in] buffer           The memory address to save the received data.
251  * @param[in] length           The data length want to be received.
252  *
253  * @return A USB error code or kStatus_USB_Success.
254  *
255  * @note The return value just means if the receiving request is successful or not; the transfer done is notified by the
256  * corresponding callback function.
257  * Currently, only one transfer request can be supported for one specific endpoint.
258  * If there is a specific requirement to support multiple transfer requests for one specific endpoint, the application
259  * should implement a queue in the application level.
260  * The subsequent transfer could begin only when the previous transfer is done (get notification through the endpoint
261  * callback).
262  */
263 usb_status_t USB_DeviceEhciRecv(usb_device_controller_handle ehciHandle,
264                                 uint8_t endpointAddress,
265                                 uint8_t *buffer,
266                                 uint32_t length);
267 
268 /*!
269  * @brief Cancels the pending transfer in a specified endpoint.
270  *
271  * The function is used to cancel the pending transfer in a specified endpoint.
272  *
273  * @param[in] ehciHandle      Pointer of the device EHCI handle.
274  * @param[in] ep               Endpoint address, bit7 is the direction of endpoint, 1U - IN, 0U - OUT.
275  *
276  * @return A USB error code or kStatus_USB_Success.
277  */
278 usb_status_t USB_DeviceEhciCancel(usb_device_controller_handle ehciHandle, uint8_t ep);
279 
280 /*!
281  * @brief Controls the status of the selected item.
282  *
283  * The function is used to control the status of the selected item.
284  *
285  * @param[in] ehciHandle      Pointer of the device EHCI handle.
286  * @param[in] type             The selected item. See enumeration type usb_device_control_type_t.
287  * @param[in,out] param            The parameter type is determined by the selected item.
288  *
289  * @return A USB error code or kStatus_USB_Success.
290  */
291 usb_status_t USB_DeviceEhciControl(usb_device_controller_handle ehciHandle,
292                                    usb_device_control_type_t type,
293                                    void *param);
294 
295 /*! @} */
296 
297 #if defined(__cplusplus)
298 }
299 #endif
300 
301 /*! @} */
302 
303 #endif /* __USB_DEVICE_EHCI_H__ */
304