1 /******************************************************************************
3 * Copyright (C) 2010 - 2014 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
36 * @addtogroup usbps_v2_1
40 * This file contains the implementation of the XUsbPs driver. It is the
41 * driver for an USB controller in DEVICE or HOST mode.
43 * <h2>Introduction</h2>
45 * The Spartan-3AF Embedded Peripheral Block contains a USB controller for
46 * communication with serial peripherals or hosts. The USB controller supports
47 * Host, Device and On the Go (OTG) applications.
49 * <h2>USB Controller Features</h2>
51 * - Supports Low Speed USB 1.1 (1.5Mbps), Full Speed USB 1.1 (12Mbps), and
52 * High Speed USB 2.0 (480Mbps) data speeds
53 * - Supports Device, Host and OTG operational modes
54 * - ULPI transceiver interface for USB 2.0 operation
55 * - Integrated USB Full and Low speed serial transceiver interfaces for lowest
58 * <h2>Initialization & Configuration</h2>
60 * The configuration of the USB driver happens in multiple stages:
62 * - (a) Configuration of the basic parameters:
63 * In this stage the basic parameters for the driver are configured,
64 * including the base address and the controller ID.
66 * - (b) Configuration of the DEVICE endpoints (if applicable):
67 * If DEVICE mode is desired, the endpoints of the controller need to be
68 * configured using the XUsbPs_DeviceConfig data structure. Once the
69 * endpoint configuration is set up in the data structure, The user then
70 * needs to allocate the required amount of DMAable memory and
71 * finalize the configuration of the XUsbPs_DeviceConfig data structure,
72 * e.g. setting the DMAMemVirt and DMAMemPhys members.
74 * - (c) Configuration of the DEVICE modes:
75 * In the second stage the parameters for DEVICE are configured.
76 * The caller only needs to configure the modes that are
77 * actually used. Configuration is done with the:
78 * XUsbPs_ConfigureDevice()
79 * Configuration parameters are defined and passed
80 * into these functions using the:
81 * XUsbPs_DeviceConfig data structures.
84 * <h2>USB Device Endpoints</h2>
86 * The USB core supports up to 4 endpoints. Each endpoint has two directions,
87 * an OUT (RX) and an IN (TX) direction. Note that the direction is viewed from
88 * the host's perspective. Endpoint 0 defaults to be the control endpoint and
89 * does not need to be set up. Other endpoints need to be configured and set up
90 * depending on the application. Only endpoints that are actuelly used by the
91 * application need to be initialized.
92 * See the example code (xusbps_intr_example.c) for more information.
95 * <h2>Interrupt Handling</h2>
97 * The USB core uses one interrupt line to report interrupts to the CPU.
98 * Interrupts are handled by the driver's interrupt handler function
99 * XUsbPs_IntrHandler().
100 * It has to be registered with the OS's interrupt subsystem. The driver's
101 * interrupt handler divides incoming interrupts into two categories:
103 * - General device interrupts
104 * - Endopint related interrupts
106 * The user (typically the adapter layer) can register general interrupt
107 * handler fucntions and endpoint specific interrupt handler functions with the
108 * driver to receive those interrupts by calling the
109 * XUsbPs_IntrSetHandler()
111 * XUsbPs_EpSetHandler()
112 * functions respectively. Calling these functions with a NULL pointer as the
113 * argument for the function pointer will "clear" the handler function.
115 * The user can register one handler function for the generic interrupts and
116 * two handler functions for each endpoint, one for the RX (OUT) and one for
117 * the TX (IN) direction. For some applications it may be useful to register a
118 * single endpoint handler function for muliple endpoints/directions.
120 * When a callback function is called by the driver, parameters identifying the
121 * type of the interrupt will be passed into the handler functions. For general
122 * interrupts the interrupt mask will be passed into the handler function. For
123 * endpoint interrupts the parameters include the number of the endpoint, the
124 * direction (OUT/IN) and the type of the interrupt.
127 * <h2>Data buffer handling</h2>
129 * Data buffers are sent to and received from endpoint using the
130 * XUsbPs_EpBufferSend(), XUsbPs_EpBufferSendWithZLT()
132 * XUsbPs_EpBufferReceive()
135 * User data buffer size is limited to 16 Kbytes. If the user wants to send a
136 * data buffer that is bigger than this limit it needs to break down the data
137 * buffer into multiple fragments and send the fragments individually.
139 * From the controller perspective Data buffers can be aligned at any boundary.
140 * if the buffers are from cache region then the buffer and buffer size should
141 * be aligned to cache line aligned
146 * The driver uses a zero copy mechanism which imposes certain restrictions to
147 * the way the user can handle the data buffers.
149 * One restriction is that the user needs to release a buffer after it is done
150 * processing the data in the buffer.
152 * Similarly, when the user sends a data buffer it MUST not re-use the buffer
153 * until it is notified by the driver that the buffer has been transmitted. The
154 * driver will notify the user via the registered endpoint interrupt handling
155 * function by sending a XUSBPS_EP_EVENT_DATA_TX event.
160 * The driver uses DMA internally to move data from/to memory. This behaviour
161 * is transparent to the user. Keeping the DMA handling hidden from the user
162 * has the advantage that the same API can be used with USB cores that do not
167 * MODIFICATION HISTORY:
169 * Ver Who Date Changes
170 * ----- ---- -------- ----------------------------------------------------------
171 * 1.00a wgr 10/10/10 First release
172 * 1.02a wgr 05/16/12 Removed comments as they are showing up in SDK
174 * 1.03a nm 09/21/12 Fixed CR#678977. Added proper sequence for setup packet
176 * 1.04a nm 10/23/12 Fixed CR# 679106.
177 * 11/02/12 Fixed CR# 683931. Mult bits are set properly in dQH.
178 * 2.00a kpc 04/03/14 Fixed CR#777763. Corrected the setup tripwire macro val.
179 * 2.1 kpc 04/28/14 Removed unused function prototypes
182 ******************************************************************************/
191 /***************************** Include Files *********************************/
193 #include "xusbps_hw.h"
194 #include "xil_types.h"
197 /************************** Constant Definitions *****************************/
200 * @name System hang prevention Timeout counter value.
202 * This value is used throughout the code to initialize a Timeout counter that
203 * is used when hard polling a register. The ides is to initialize the Timeout
204 * counter to a value that is longer than any expected Timeout but short enough
205 * so the system will continue to work and report an error while the user is
206 * still paying attention. A reasonable Timeout time would be about 10 seconds.
207 * The XUSBPS_TIMEOUT_COUNTER value should be chosen so a polling loop would
208 * run about 10 seconds before a Timeout is detected. For example:
210 * int Timeout = XUSBPS_TIMEOUT_COUNTER;
211 * while ((XUsbPs_ReadReg(InstancePtr->Config.BaseAddress,
212 * XUSBPS_CMD_OFFSET) &
213 * XUSBPS_CMD_RST_MASK) && --Timeout) {
216 * if (0 == Timeout) {
217 * return XST_FAILURE;
221 #define XUSBPS_TIMEOUT_COUNTER 1000000
225 * @name Endpoint Direction (bitmask)
226 * Definitions to be used with Endpoint related function that require a
227 * 'Direction' parameter.
230 * The direction is always defined from the perspective of the HOST! This
231 * means that an IN endpoint on the controller is used for sending data while
232 * the OUT endpoint on the controller is used for receiving data.
235 #define XUSBPS_EP_DIRECTION_IN 0x01 /**< Endpoint direction IN. */
236 #define XUSBPS_EP_DIRECTION_OUT 0x02 /**< Endpoint direction OUT. */
241 * @name Endpoint Type
242 * Definitions to be used with Endpoint related functions that require a 'Type'
246 #define XUSBPS_EP_TYPE_NONE 0 /**< Endpoint is not used. */
247 #define XUSBPS_EP_TYPE_CONTROL 1 /**< Endpoint for Control Transfers */
248 #define XUSBPS_EP_TYPE_ISOCHRONOUS 2 /**< Endpoint for isochronous data */
249 #define XUSBPS_EP_TYPE_BULK 3 /**< Endpoint for BULK Transfers. */
250 #define XUSBPS_EP_TYPE_INTERRUPT 4 /**< Endpoint for interrupt Transfers */
254 * Endpoint Max Packet Length in DeviceConfig is a coded value, ch9.6.6.
258 #define ENDPOINT_MAXP_LENGTH 0x400
259 #define ENDPOINT_MAXP_MULT_MASK 0xC00
260 #define ENDPOINT_MAXP_MULT_SHIFT 10
264 * @name Field names for status retrieval
265 * Definitions for the XUsbPs_GetStatus() function call 'StatusType'
269 #define XUSBPS_EP_STS_ADDRESS 1 /**< Address of controller. */
270 #define XUSBPS_EP_STS_CONTROLLER_STATE 2 /**< Current controller state. */
276 * @name USB Default alternate setting
280 #define XUSBPS_DEFAULT_ALT_SETTING 0 /**< The default alternate setting is 0 */
284 * @name Endpoint event types
285 * Definitions that are used to identify events that occur on endpoints. Passed
286 * to the endpoint event handler functions registered with
287 * XUsbPs_EpSetHandler().
290 #define XUSBPS_EP_EVENT_SETUP_DATA_RECEIVED 0x01
291 /**< Setup data has been received on the enpoint. */
292 #define XUSBPS_EP_EVENT_DATA_RX 0x02
293 /**< Data frame has been received on the endpoint. */
294 #define XUSBPS_EP_EVENT_DATA_TX 0x03
295 /**< Data frame has been sent on the endpoint. */
300 * Maximum packet size for endpoint, 1024
303 #define XUSBPS_MAX_PACKET_SIZE 1024
304 /**< Maximum value can be put into the queue head */
306 /**************************** Type Definitions *******************************/
308 /******************************************************************************
309 * This data type defines the callback function to be used for Endpoint
312 * @param CallBackRef is the Callback reference passed in by the upper
313 * layer when setting the handler, and is passed back to the upper
314 * layer when the handler is called.
315 * @param EpNum is the Number of the endpoint that caused the event.
316 * @param EventType is the type of the event that occured on the endpoint.
317 * @param Data is a pointer to user data pointer specified when callback
320 typedef void (*XUsbPs_EpHandlerFunc)(void *CallBackRef,
321 u8 EpNum, u8 EventType, void *Data);
324 /******************************************************************************
325 * This data type defines the callback function to be used for the general
328 * @param CallBackRef is the Callback reference passed in by the upper
329 * layer when setting the handler, and is passed back to the upper
330 * layer when the handler is called.
331 * @param IrqMask is the Content of the interrupt status register. This
332 * value can be used by the callback function to distinguish the
333 * individual interrupt types.
335 typedef void (*XUsbPs_IntrHandlerFunc)(void *CallBackRef, u32 IrqMask);
338 /******************************************************************************/
340 /* The following type definitions are used for referencing Queue Heads and
341 * Transfer Descriptors. The structures themselves are not used, however, the
342 * types are used in the API to avoid using (void *) pointers.
344 typedef u8 XUsbPs_dQH[XUSBPS_dQH_ALIGN];
345 typedef u8 XUsbPs_dTD[XUSBPS_dTD_ALIGN];
349 * The following data structures are used internally by the L0/L1 driver.
350 * Their contents MUST NOT be changed by the upper layers.
354 * The following data structure represents OUT endpoint.
358 /**< Pointer to the Queue Head structure of the endpoint. */
361 /**< Pointer to the first dTD of the dTD list for this
365 /**< Buffer to the currently processed descriptor. */
368 /**< Pointer to the first buffer of the buffer list for this
371 XUsbPs_EpHandlerFunc HandlerFunc;
372 /**< Handler function for this endpoint. */
374 /**< User data reference for the handler. */
379 * The following data structure represents IN endpoint.
383 /**< Pointer to the Queue Head structure of the endpoint. */
386 /**< List of pointers to the Transfer Descriptors of the
390 /**< Buffer to the next available descriptor in the list. */
393 /**< Buffer to the last unsent descriptor in the list*/
395 XUsbPs_EpHandlerFunc HandlerFunc;
396 /**< Handler function for this endpoint. */
398 /**< User data reference for the handler. */
403 * The following data structure represents an endpoint used internally
404 * by the L0/L1 driver.
407 /* Each endpoint has an OUT and an IN component.
409 XUsbPs_EpOut Out; /**< OUT endpoint structure */
410 XUsbPs_EpIn In; /**< IN endpoint structure */
416 * The following structure is used by the user to receive Setup Data from an
417 * endpoint. Using this structure simplifies the process of interpreting the
418 * setup data in the core's data fields.
420 * The naming scheme for the members of this structure is different from the
421 * naming scheme found elsewhere in the code. The members of this structure are
422 * defined in the Chapter 9 USB reference guide. Using this naming scheme makes
423 * it easier for people familiar with the standard to read the code.
426 u8 bmRequestType; /**< bmRequestType in setup data */
427 u8 bRequest; /**< bRequest in setup data */
428 u16 wValue; /**< wValue in setup data */
429 u16 wIndex; /**< wIndex in setup data */
430 u16 wLength; /**< wLength in setup data */
436 * Data structures used to configure endpoints.
441 - XUSBPS_EP_TYPE_CONTROL
442 - XUSBPS_EP_TYPE_ISOCHRONOUS
443 - XUSBPS_EP_TYPE_BULK
444 - XUSBPS_EP_TYPE_INTERRUPT */
447 /**< Number of buffers to be handled by this endpoint. */
449 /**< Buffer size. Only relevant for OUT (receive) Endpoints. */
452 /**< Maximum packet size for this endpoint. This number will
453 * define the maximum number of bytes sent on the wire per
454 * transaction. Range: 0..1024 */
459 * Endpoint configuration structure.
462 XUsbPs_EpSetup Out; /**< OUT component of endpoint. */
463 XUsbPs_EpSetup In; /**< IN component of endpoint. */
468 * The XUsbPs_DeviceConfig structure contains the configuration information to
469 * configure the USB controller for DEVICE mode. This data structure is used
470 * with the XUsbPs_ConfigureDevice() function call.
473 u8 NumEndpoints; /**< Number of Endpoints for the controller.
474 This number depends on the runtime
475 configuration of driver. The driver may
476 configure fewer endpoints than are available
479 XUsbPs_EpConfig EpCfg[XUSBPS_MAX_ENDPOINTS];
480 /**< List of endpoint configurations. */
483 u32 DMAMemPhys; /**< Physical base address of DMAable memory
484 allocated for the driver. */
486 /* The following members are used internally by the L0/L1 driver. They
487 * MUST NOT be accesses and/or modified in any way by the upper layers.
489 * The reason for having these members is that we generally try to
490 * avoid allocating memory in the L0/L1 driver as we want to be OS
491 * independent. In order to avoid allocating memory for this data
492 * structure wihin L0/L1 we put it into the XUsbPs_DeviceConfig
493 * structure which is allocated by the caller.
495 XUsbPs_Endpoint Ep[XUSBPS_MAX_ENDPOINTS];
496 /**< List of endpoint metadata structures. */
498 u32 PhysAligned; /**< 64 byte aligned base address of the DMA
499 memory block. Will be computed and set by
501 } XUsbPs_DeviceConfig;
505 * The XUsbPs_Config structure contains configuration information for the USB
508 * This structure only contains the basic configuration for the controller. The
509 * caller also needs to initialize the controller for the DEVICE mode
510 * using the XUsbPs_DeviceConfig data structures with the
511 * XUsbPs_ConfigureDevice() function call
514 u16 DeviceID; /**< Unique ID of controller. */
515 u32 BaseAddress; /**< Core register base address. */
520 * The XUsbPs driver instance data. The user is required to allocate a
521 * variable of this type for every USB controller in the system. A pointer to a
522 * variable of this type is then passed to the driver API functions.
525 XUsbPs_Config Config; /**< Configuration structure */
527 int CurrentAltSetting; /**< Current alternative setting of interface */
529 void *UserDataPtr; /**< Data pointer to be used by upper layers to
530 store application dependent data structures.
531 The upper layers are responsible to allocated
532 and free the memory. The driver will not
533 mofidy this data pointer. */
536 * The following structures hold the configuration for DEVICE mode
537 * of the controller. They are initialized using the
538 * XUsbPs_ConfigureDevice() function call.
540 XUsbPs_DeviceConfig DeviceConfig;
541 /**< Configuration for the DEVICE mode. */
543 XUsbPs_IntrHandlerFunc HandlerFunc;
544 /**< Handler function for the controller. */
546 /**< User data reference for the handler. */
548 /**< User interrupt mask. Defines which interrupts will cause
549 * the callback to be called. */
553 /***************** Macros (Inline Functions) Definitions *********************/
555 /******************************************************************************
557 * USB CONTROLLER RELATED MACROS
559 ******************************************************************************/
560 /*****************************************************************************/
562 * This macro returns the current frame number.
564 * @param InstancePtr is a pointer to the XUsbPs instance of the
567 * @return The current frame number.
569 * @note C-style signature:
570 * u32 XUsbPs_GetFrameNum(const XUsbPs *InstancePtr)
572 ******************************************************************************/
573 #define XUsbPs_GetFrameNum(InstancePtr) \
574 XUsbPs_ReadReg((InstancePtr)->Config.BaseAddress, XUSBPS_FRAME_OFFSET)
577 /*****************************************************************************/
579 * This macro starts the USB engine.
581 * @param InstancePtr is a pointer to the XUsbPs instance of the
584 * @note C-style signature:
585 * void XUsbPs_Start(XUsbPs *InstancePtr)
587 ******************************************************************************/
588 #define XUsbPs_Start(InstancePtr) \
589 XUsbPs_SetBits(InstancePtr, XUSBPS_CMD_OFFSET, XUSBPS_CMD_RS_MASK)
592 /*****************************************************************************/
594 * This macro stops the USB engine.
596 * @param InstancePtr is a pointer to the XUsbPs instance of the
599 * @note C-style signature:
600 * void XUsbPs_Stop(XUsbPs *InstancePtr)
602 ******************************************************************************/
603 #define XUsbPs_Stop(InstancePtr) \
604 XUsbPs_ClrBits(InstancePtr, XUSBPS_CMD_OFFSET, XUSBPS_CMD_RS_MASK)
607 /*****************************************************************************/
609 * This macro forces the USB engine to be in Full Speed (FS) mode.
611 * @param InstancePtr is a pointer to the XUsbPs instance of the
614 * @note C-style signature:
615 * void XUsbPs_ForceFS(XUsbPs *InstancePtr)
617 ******************************************************************************/
618 #define XUsbPs_ForceFS(InstancePtr) \
619 XUsbPs_SetBits(InstancePtr, XUSBPS_PORTSCR1_OFFSET, \
620 XUSBPS_PORTSCR_PFSC_MASK)
623 /*****************************************************************************/
625 * This macro starts the USB Timer 0, with repeat option for period of
628 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
629 * @param Interval is the interval for Timer0 to generate an interrupt
631 * @note C-style signature:
632 * void XUsbPs_StartTimer0(XUsbPs *InstancePtr, u32 Interval)
634 ******************************************************************************/
635 #define XUsbPs_StartTimer0(InstancePtr, Interval) \
637 XUsbPs_WriteReg((InstancePtr)->Config.BaseAddress, \
638 XUSBPS_TIMER0_LD_OFFSET, (Interval)); \
639 XUsbPs_SetBits(InstancePtr, XUSBPS_TIMER0_CTL_OFFSET, \
640 XUSBPS_TIMER_RUN_MASK | \
641 XUSBPS_TIMER_RESET_MASK | \
642 XUSBPS_TIMER_REPEAT_MASK); \
646 /*****************************************************************************/
648 * This macro stops Timer 0.
650 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
652 * @note C-style signature:
653 * void XUsbPs_StopTimer0(XUsbPs *InstancePtr)
655 ******************************************************************************/
656 #define XUsbPs_StopTimer0(InstancePtr) \
657 XUsbPs_ClrBits(InstancePtr, XUSBPS_TIMER0_CTL_OFFSET, \
658 XUSBPS_TIMER_RUN_MASK)
661 /*****************************************************************************/
663 * This macro reads Timer 0.
665 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
667 * @note C-style signature:
668 * void XUsbPs_ReadTimer0(XUsbPs *InstancePtr)
670 ******************************************************************************/
671 #define XUsbPs_ReadTimer0(InstancePtr) \
672 XUsbPs_ReadReg((InstancePtr)->Config.BaseAddress, \
673 XUSBPS_TIMER0_CTL_OFFSET) & \
674 XUSBPS_TIMER_COUNTER_MASK
677 /*****************************************************************************/
679 * This macro force remote wakeup on host
681 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
683 * @note C-style signature:
684 * void XUsbPs_RemoteWakeup(XUsbPs *InstancePtr)
686 ******************************************************************************/
687 #define XUsbPs_RemoteWakeup(InstancePtr) \
688 XUsbPs_SetBits(InstancePtr, XUSBPS_PORTSCR1_OFFSET, \
689 XUSBPS_PORTSCR_FPR_MASK)
692 /******************************************************************************
694 * ENDPOINT RELATED MACROS
696 ******************************************************************************/
697 /*****************************************************************************/
699 * This macro enables the given endpoint for the given direction.
701 * @param InstancePtr is a pointer to the XUsbPs instance of the
703 * @param EpNum is number of the endpoint to enable.
704 * @param Dir is direction of the endpoint (bitfield):
705 * - XUSBPS_EP_DIRECTION_OUT
706 * - XUSBPS_EP_DIRECTION_IN
708 * @note C-style signature:
709 * void XUsbPs_EpEnable(XUsbPs *InstancePtr, u8 EpNum, u8 Dir)
711 ******************************************************************************/
712 #define XUsbPs_EpEnable(InstancePtr, EpNum, Dir) \
713 XUsbPs_SetBits(InstancePtr, XUSBPS_EPCRn_OFFSET(EpNum), \
714 ((Dir) & XUSBPS_EP_DIRECTION_OUT ? XUSBPS_EPCR_RXE_MASK : 0) | \
715 ((Dir) & XUSBPS_EP_DIRECTION_IN ? XUSBPS_EPCR_TXE_MASK : 0))
718 /*****************************************************************************/
720 * This macro disables the given endpoint for the given direction.
722 * @param InstancePtr is a pointer to the XUsbPs instance of the
724 * @param EpNum is the number of the endpoint to disable.
725 * @param Dir is the direction of the endpoint (bitfield):
726 * - XUSBPS_EP_DIRECTION_OUT
727 * - XUSBPS_EP_DIRECTION_IN
729 * @note C-style signature:
730 * void XUsbPs_EpDisable(XUsbPs *InstancePtr, u8 EpNum, u8 Dir)
732 ******************************************************************************/
733 #define XUsbPs_EpDisable(InstancePtr, EpNum, Dir) \
734 XUsbPs_ClrBits(InstancePtr, XUSBPS_EPCRn_OFFSET(EpNum), \
735 ((Dir) & XUSBPS_EP_DIRECTION_OUT ? XUSBPS_EPCR_RXE_MASK : 0) | \
736 ((Dir) & XUSBPS_EP_DIRECTION_IN ? XUSBPS_EPCR_TXE_MASK : 0))
739 /*****************************************************************************/
741 * This macro stalls the given endpoint for the given direction, and flush
744 * @param InstancePtr is a pointer to the XUsbPs instance of the
746 * @param EpNum is number of the endpoint to stall.
747 * @param Dir is the direction of the endpoint (bitfield):
748 * - XUSBPS_EP_DIRECTION_OUT
749 * - XUSBPS_EP_DIRECTION_IN
751 * @note C-style signature:
752 * void XUsbPs_EpStall(XUsbPs *InstancePtr, u8 EpNum, u8 Dir)
754 ******************************************************************************/
755 #define XUsbPs_EpStall(InstancePtr, EpNum, Dir) \
756 XUsbPs_SetBits(InstancePtr, XUSBPS_EPCRn_OFFSET(EpNum), \
757 ((Dir) & XUSBPS_EP_DIRECTION_OUT ? XUSBPS_EPCR_RXS_MASK : 0) | \
758 ((Dir) & XUSBPS_EP_DIRECTION_IN ? XUSBPS_EPCR_TXS_MASK : 0))
761 /*****************************************************************************/
763 * This macro unstalls the given endpoint for the given direction.
765 * @param InstancePtr is a pointer to the XUsbPs instance of the
767 * @param EpNum is the Number of the endpoint to unstall.
768 * @param Dir is the Direction of the endpoint (bitfield):
769 * - XUSBPS_EP_DIRECTION_OUT
770 * - XUSBPS_EP_DIRECTION_IN
772 * @note C-style signature:
773 * void XUsbPs_EpUnStall(XUsbPs *InstancePtr, u8 EpNum, u8 Dir)
775 ******************************************************************************/
776 #define XUsbPs_EpUnStall(InstancePtr, EpNum, Dir) \
777 XUsbPs_ClrBits(InstancePtr, XUSBPS_EPCRn_OFFSET(EpNum), \
778 ((Dir) & XUSBPS_EP_DIRECTION_OUT ? XUSBPS_EPCR_RXS_MASK : 0) | \
779 ((Dir) & XUSBPS_EP_DIRECTION_IN ? XUSBPS_EPCR_TXS_MASK : 0))
782 /*****************************************************************************/
784 * This macro flush an endpoint upon interface disable
786 * @param InstancePtr is a pointer to the XUsbPs instance of the
788 * @param EpNum is the number of the endpoint to flush.
789 * @param Dir is the direction of the endpoint (bitfield):
790 * - XUSBPS_EP_DIRECTION_OUT
791 * - XUSBPS_EP_DIRECTION_IN
793 * @note C-style signature:
794 * void XUsbPs_EpFlush(XUsbPs *InstancePtr, u8 EpNum, u8 Dir)
796 ******************************************************************************/
797 #define XUsbPs_EpFlush(InstancePtr, EpNum, Dir) \
798 XUsbPs_SetBits(InstancePtr, XUSBPS_EPFLUSH_OFFSET, \
799 EpNum << ((Dir) & XUSBPS_EP_DIRECTION_OUT ? \
800 XUSBPS_EPFLUSH_RX_SHIFT:XUSBPS_EPFLUSH_TX_SHIFT)) \
802 /*****************************************************************************/
804 * This macro enables the interrupts defined by the bit mask.
806 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
807 * @param IntrMask is the Bit mask of interrupts to be enabled.
809 * @note C-style signature:
810 * void XUsbPs_IntrEnable(XUsbPs *InstancePtr, u32 IntrMask)
812 ******************************************************************************/
813 #define XUsbPs_IntrEnable(InstancePtr, IntrMask) \
814 XUsbPs_SetBits(InstancePtr, XUSBPS_IER_OFFSET, IntrMask)
817 /*****************************************************************************/
819 * This function disables the interrupts defined by the bit mask.
822 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
823 * @param IntrMask is a Bit mask of interrupts to be disabled.
825 * @note C-style signature:
826 * void XUsbPs_IntrDisable(XUsbPs *InstancePtr, u32 IntrMask)
828 ******************************************************************************/
829 #define XUsbPs_IntrDisable(InstancePtr, IntrMask) \
830 XUsbPs_ClrBits(InstancePtr, XUSBPS_IER_OFFSET, IntrMask)
833 /*****************************************************************************/
835 * This macro enables the endpoint NAK interrupts defined by the bit mask.
837 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
838 * @param NakIntrMask is the Bit mask of endpoint NAK interrupts to be
840 * @note C-style signature:
841 * void XUsbPs_NakIntrEnable(XUsbPs *InstancePtr, u32 NakIntrMask)
843 ******************************************************************************/
844 #define XUsbPs_NakIntrEnable(InstancePtr, NakIntrMask) \
845 XUsbPs_SetBits(InstancePtr, XUSBPS_EPNAKIER_OFFSET, NakIntrMask)
848 /*****************************************************************************/
850 * This macro disables the endpoint NAK interrupts defined by the bit mask.
852 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
853 * @param NakIntrMask is a Bit mask of endpoint NAK interrupts to be
858 * void XUsbPs_NakIntrDisable(XUsbPs *InstancePtr, u32 NakIntrMask)
860 ******************************************************************************/
861 #define XUsbPs_NakIntrDisable(InstancePtr, NakIntrMask) \
862 XUsbPs_ClrBits(InstancePtr, XUSBPS_EPNAKIER_OFFSET, NakIntrMask)
865 /*****************************************************************************/
867 * This function clears the endpoint NAK interrupts status defined by the
870 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
871 * @param NakIntrMask is the Bit mask of endpoint NAK interrupts to be cleared.
873 * @note C-style signature:
874 * void XUsbPs_NakIntrClear(XUsbPs *InstancePtr, u32 NakIntrMask)
876 ******************************************************************************/
877 #define XUsbPs_NakIntrClear(InstancePtr, NakIntrMask) \
878 XUsbPs_WriteReg((InstancePtr)->Config.BaseAddress, \
879 XUSBPS_EPNAKISR_OFFSET, NakIntrMask)
883 /*****************************************************************************/
885 * This macro sets the Interrupt Threshold value in the control register
887 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
888 * @param Threshold is the Interrupt threshold to be set.
890 * - XUSBPS_CMD_ITHRESHOLD_0 - Immediate interrupt
891 * - XUSBPS_CMD_ITHRESHOLD_1 - 1 Frame
892 * - XUSBPS_CMD_ITHRESHOLD_2 - 2 Frames
893 * - XUSBPS_CMD_ITHRESHOLD_4 - 4 Frames
894 * - XUSBPS_CMD_ITHRESHOLD_8 - 8 Frames
895 * - XUSBPS_CMD_ITHRESHOLD_16 - 16 Frames
896 * - XUSBPS_CMD_ITHRESHOLD_32 - 32 Frames
897 * - XUSBPS_CMD_ITHRESHOLD_64 - 64 Frames
901 * void XUsbPs_SetIntrThreshold(XUsbPs *InstancePtr, u8 Threshold)
903 ******************************************************************************/
904 #define XUsbPs_SetIntrThreshold(InstancePtr, Threshold) \
905 XUsbPs_WriteReg((InstancePtr)->Config.BaseAddress, \
906 XUSBPS_CMD_OFFSET, (Threshold))\
909 /*****************************************************************************/
911 * This macro sets the Tripwire bit in the USB command register.
913 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
915 * @note C-style signature:
916 * void XUsbPs_SetTripwire(XUsbPs *InstancePtr)
918 ******************************************************************************/
919 #define XUsbPs_SetSetupTripwire(InstancePtr) \
920 XUsbPs_SetBits(InstancePtr, XUSBPS_CMD_OFFSET, \
921 XUSBPS_CMD_SUTW_MASK)
924 /*****************************************************************************/
926 * This macro clears the Tripwire bit in the USB command register.
928 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
930 * @note C-style signature:
931 * void XUsbPs_ClrTripwire(XUsbPs *InstancePtr)
933 ******************************************************************************/
934 #define XUsbPs_ClrSetupTripwire(InstancePtr) \
935 XUsbPs_ClrBits(InstancePtr, XUSBPS_CMD_OFFSET, \
936 XUSBPS_CMD_SUTW_MASK)
939 /*****************************************************************************/
941 * This macro checks if the Tripwire bit in the USB command register is set.
943 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
946 * - TRUE: The tripwire bit is still set.
947 * - FALSE: The tripwire bit has been cleared.
949 * @note C-style signature:
950 * int XUsbPs_TripwireIsSet(XUsbPs *InstancePtr)
952 ******************************************************************************/
953 #define XUsbPs_SetupTripwireIsSet(InstancePtr) \
954 (XUsbPs_ReadReg((InstancePtr)->Config.BaseAddress, \
955 XUSBPS_CMD_OFFSET) & \
956 XUSBPS_CMD_SUTW_MASK ? TRUE : FALSE)
959 /******************************************************************************
961 * GENERAL REGISTER / BIT MANIPULATION MACROS
963 ******************************************************************************/
964 /****************************************************************************/
966 * This macro sets the given bit mask in the register.
968 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
969 * @param RegOffset is the register offset to be written.
970 * @param Bits is the Bits to be set in the register
974 * @note C-style signature:
975 * void XUsbPs_SetBits(u32 BaseAddress, u32 RegOffset, u32 Bits)
977 *****************************************************************************/
978 #define XUsbPs_SetBits(InstancePtr, RegOffset, Bits) \
979 XUsbPs_WriteReg((InstancePtr)->Config.BaseAddress, RegOffset, \
980 XUsbPs_ReadReg((InstancePtr)->Config.BaseAddress, \
981 RegOffset) | (Bits));
984 /****************************************************************************/
987 * This macro clears the given bits in the register.
989 * @param InstancePtr is a pointer to XUsbPs instance of the controller.
990 * @param RegOffset is the register offset to be written.
991 * @param Bits are the bits to be cleared in the register
997 * void XUsbPs_ClrBits(u32 BaseAddress, u32 RegOffset, u32 Bits)
999 *****************************************************************************/
1000 #define XUsbPs_ClrBits(InstancePtr, RegOffset, Bits) \
1001 XUsbPs_WriteReg((InstancePtr)->Config.BaseAddress, RegOffset, \
1002 XUsbPs_ReadReg((InstancePtr)->Config.BaseAddress, \
1003 RegOffset) & ~(Bits));
1006 /************************** Function Prototypes ******************************/
1009 * Setup / Initialize functions.
1011 * Implemented in file xusbps.c
1013 int XUsbPs_CfgInitialize(XUsbPs *InstancePtr,
1014 const XUsbPs_Config *ConfigPtr, u32 BaseAddress);
1016 int XUsbPs_ConfigureDevice(XUsbPs *InstancePtr,
1017 const XUsbPs_DeviceConfig *CfgPtr);
1020 * Common functions used for DEVICE/HOST mode.
1022 int XUsbPs_Reset(XUsbPs *InstancePtr);
1025 * DEVICE mode specific functions.
1027 int XUsbPs_BusReset(XUsbPs *InstancePtr);
1028 int XUsbPs_SetDeviceAddress(XUsbPs *InstancePtr, u8 Address);
1032 * Handling Suspend and Resume.
1034 * Implemented in xusbps.c
1036 int XUsbPs_Suspend(const XUsbPs *InstancePtr);
1037 int XUsbPs_Resume(const XUsbPs *InstancePtr);
1038 int XUsbPs_RequestHostResume(const XUsbPs *InstancePtr);
1042 * Functions for managing Endpoints / Transfers
1044 * Implemented in file xusbps_endpoint.c
1046 int XUsbPs_EpBufferSend(XUsbPs *InstancePtr, u8 EpNum,
1047 const u8 *BufferPtr, u32 BufferLen);
1048 int XUsbPs_EpBufferSendWithZLT(XUsbPs *InstancePtr, u8 EpNum,
1049 const u8 *BufferPtr, u32 BufferLen);
1050 int XUsbPs_EpBufferReceive(XUsbPs *InstancePtr, u8 EpNum,
1051 u8 **BufferPtr, u32 *BufferLenPtr, u32 *Handle);
1052 void XUsbPs_EpBufferRelease(u32 Handle);
1054 int XUsbPs_EpSetHandler(XUsbPs *InstancePtr, u8 EpNum, u8 Direction,
1055 XUsbPs_EpHandlerFunc CallBackFunc,
1057 int XUsbPs_EpGetSetupData(XUsbPs *InstancePtr, int EpNum,
1058 XUsbPs_SetupData *SetupDataPtr);
1060 int XUsbPs_EpPrime(XUsbPs *InstancePtr, u8 EpNum, u8 Direction);
1062 int XUsbPs_ReconfigureEp(XUsbPs *InstancePtr, XUsbPs_DeviceConfig *CfgPtr,
1063 int EpNum, unsigned short NewDirection, int DirectionChanged);
1066 * Interrupt handling functions
1068 * Implemented in file xusbps_intr.c
1070 void XUsbPs_IntrHandler(void *InstancePtr);
1072 int XUsbPs_IntrSetHandler(XUsbPs *InstancePtr,
1073 XUsbPs_IntrHandlerFunc CallBackFunc,
1074 void *CallBackRef, u32 Mask);
1076 * Helper functions for static configuration.
1077 * Implemented in xusbps_sinit.c
1079 XUsbPs_Config *XUsbPs_LookupConfig(u16 DeviceId);
1085 #endif /* XUSBPS_H */