1 /******************************************************************************
3 * Copyright (C) 2002 - 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 * The implementation of the XGpio driver's basic functionality. See xgpio.h
37 * for more information about the driver.
44 * MODIFICATION HISTORY:
46 * Ver Who Date Changes
47 * ----- ---- -------- -----------------------------------------------
48 * 1.00a rmm 02/04/02 First release
49 * 2.00a jhl 12/16/02 Update for dual channel and interrupt support
50 * 2.01a jvb 12/13/05 Changed Initialize() into CfgInitialize(), and made
51 * CfgInitialize() take a pointer to a config structure
52 * instead of a device id. Moved Initialize() into
53 * xgpio_sinit.c, and had Initialize() call CfgInitialize()
54 * after it retrieved the config structure using the device
55 * id. Removed include of xparameters.h along with any
56 * dependencies on xparameters.h and the _g.c config table.
57 * 2.11a mta 03/21/07 Updated to new coding style, added GetDataDirection
58 * 2.12a sv 11/21/07 Updated driver to support access through DCR bus
59 * 3.00a sv 11/21/09 Updated to use HAL Processor APIs. Renamed the
60 * macros to remove _m from the name.
63 *****************************************************************************/
65 /***************************** Include Files ********************************/
70 /************************** Constant Definitions ****************************/
72 /**************************** Type Definitions ******************************/
74 /***************** Macros (Inline Functions) Definitions ********************/
76 /************************** Variable Definitions ****************************/
79 /************************** Function Prototypes *****************************/
82 /****************************************************************************/
84 * Initialize the XGpio instance provided by the caller based on the
85 * given configuration data.
87 * Nothing is done except to initialize the InstancePtr.
89 * @param InstancePtr is a pointer to an XGpio instance. The memory the
90 * pointer references must be pre-allocated by the caller. Further
91 * calls to manipulate the driver through the XGpio API must be
92 * made with this pointer.
93 * @param Config is a reference to a structure containing information
94 * about a specific GPIO device. This function initializes an
95 * InstancePtr object for a specific device specified by the
96 * contents of Config. This function can initialize multiple
97 * instance objects with the use of multiple calls giving different
98 * Config information on each call.
99 * @param EffectiveAddr is the device base address in the virtual memory
100 * address space. The caller is responsible for keeping the address
101 * mapping from EffectiveAddr to the device physical base address
102 * unchanged once this function is invoked. Unexpected errors may
103 * occur if the address mapping changes after this function is
104 * called. If address translation is not used, use
105 * Config->BaseAddress for this parameters, passing the physical
109 * - XST_SUCCESS Initialization was successfull.
113 *****************************************************************************/
114 int XGpio_CfgInitialize(XGpio * InstancePtr, XGpio_Config * Config,
120 Xil_AssertNonvoid(InstancePtr != NULL);
123 * Set some default values.
125 #if (XPAR_XGPIO_USE_DCR_BRIDGE != 0)
126 InstancePtr->BaseAddress = ((EffectiveAddr >> 2)) & 0xFFF;
128 InstancePtr->BaseAddress = EffectiveAddr;
131 InstancePtr->InterruptPresent = Config->InterruptPresent;
132 InstancePtr->IsDual = Config->IsDual;
135 * Indicate the instance is now ready to use, initialized without error
137 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
138 return (XST_SUCCESS);
142 /****************************************************************************/
144 * Set the input/output direction of all discrete signals for the specified
147 * @param InstancePtr is a pointer to an XGpio instance to be worked on.
148 * @param Channel contains the channel of the GPIO (1 or 2) to operate on.
149 * @param DirectionMask is a bitmask specifying which discretes are input
150 * and which are output. Bits set to 0 are output and bits set to 1
155 * @note The hardware must be built for dual channels if this function
156 * is used with any channel other than 1. If it is not, this
157 * function will assert.
159 *****************************************************************************/
160 void XGpio_SetDataDirection(XGpio * InstancePtr, unsigned Channel,
163 Xil_AssertVoid(InstancePtr != NULL);
164 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
165 Xil_AssertVoid((Channel == 1) ||
166 ((Channel == 2) && (InstancePtr->IsDual == TRUE)));
168 XGpio_WriteReg(InstancePtr->BaseAddress,
169 ((Channel - 1) * XGPIO_CHAN_OFFSET) + XGPIO_TRI_OFFSET,
173 /****************************************************************************/
175 * Get the input/output direction of all discrete signals for the specified
178 * @param InstancePtr is a pointer to an XGpio instance to be worked on.
179 * @param Channel contains the channel of the GPIO (1 or 2) to operate on.
181 * @return Bitmask specifying which discretes are input and
182 * which are output. Bits set to 0 are output and bits set to 1 are
187 * The hardware must be built for dual channels if this function is used
188 * with any channel other than 1. If it is not, this function will assert.
190 *****************************************************************************/
191 u32 XGpio_GetDataDirection(XGpio *InstancePtr, unsigned Channel)
193 Xil_AssertNonvoid(InstancePtr != NULL);
194 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
195 Xil_AssertNonvoid((Channel == 1) ||
197 (InstancePtr->IsDual == TRUE)));
199 return XGpio_ReadReg(InstancePtr->BaseAddress,
200 ((Channel - 1) * XGPIO_CHAN_OFFSET) + XGPIO_TRI_OFFSET);
203 /****************************************************************************/
205 * Read state of discretes for the specified GPIO channnel.
207 * @param InstancePtr is a pointer to an XGpio instance to be worked on.
208 * @param Channel contains the channel of the GPIO (1 or 2) to operate on.
210 * @return Current copy of the discretes register.
212 * @note The hardware must be built for dual channels if this function
213 * is used with any channel other than 1. If it is not, this
214 * function will assert.
216 *****************************************************************************/
217 u32 XGpio_DiscreteRead(XGpio * InstancePtr, unsigned Channel)
219 Xil_AssertNonvoid(InstancePtr != NULL);
220 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
221 Xil_AssertNonvoid((Channel == 1) ||
222 ((Channel == 2) && (InstancePtr->IsDual == TRUE)));
224 return XGpio_ReadReg(InstancePtr->BaseAddress,
225 ((Channel - 1) * XGPIO_CHAN_OFFSET) +
229 /****************************************************************************/
231 * Write to discretes register for the specified GPIO channel.
233 * @param InstancePtr is a pointer to an XGpio instance to be worked on.
234 * @param Channel contains the channel of the GPIO (1 or 2) to operate on.
235 * @param Data is the value to be written to the discretes register.
239 * @note The hardware must be built for dual channels if this function
240 * is used with any channel other than 1. If it is not, this
241 * function will assert. See also XGpio_DiscreteSet() and
242 * XGpio_DiscreteClear().
244 *****************************************************************************/
245 void XGpio_DiscreteWrite(XGpio * InstancePtr, unsigned Channel, u32 Data)
247 Xil_AssertVoid(InstancePtr != NULL);
248 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
249 Xil_AssertVoid((Channel == 1) ||
250 ((Channel == 2) && (InstancePtr->IsDual == TRUE)));
252 XGpio_WriteReg(InstancePtr->BaseAddress,
253 ((Channel - 1) * XGPIO_CHAN_OFFSET) + XGPIO_DATA_OFFSET,