1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 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 /*****************************************************************************/
37 * The XGpioPs driver. Functions in this file are the minimum required functions
38 * for this driver. See xgpiops.h for a detailed description of the driver.
41 * MODIFICATION HISTORY:
43 * Ver Who Date Changes
44 * ----- ---- -------- -----------------------------------------------
45 * 1.00a sv 01/15/10 First Release
46 * 1.01a sv 04/15/12 Removed the APIs XGpioPs_SetMode, XGpioPs_SetModePin
47 * XGpioPs_GetMode, XGpioPs_GetModePin as they are not
48 * relevant to Zynq device. The interrupts are disabled
49 * for output pins on all banks during initialization.
50 * 2.1 hk 04/29/14 Use Input data register DATA_RO for read. CR# 771667.
51 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
55 ******************************************************************************/
57 /***************************** Include Files *********************************/
61 /************************** Constant Definitions *****************************/
63 /**************************** Type Definitions *******************************/
65 /***************** Macros (Inline Functions) Definitions *********************/
67 /************************** Variable Definitions *****************************/
70 /************************** Function Prototypes ******************************/
72 extern void StubHandler(void *CallBackRef, u32 Bank, u32 Status);
74 /*****************************************************************************/
77 * This function initializes a XGpioPs instance/driver.
78 * All members of the XGpioPs instance structure are initialized and
79 * StubHandlers are assigned to the Bank Status Handlers.
81 * @param InstancePtr is a pointer to the XGpioPs instance.
82 * @param ConfigPtr points to the XGpioPs device configuration structure.
83 * @param EffectiveAddr is the device base address in the virtual memory
84 * address space. If the address translation is not used then the
85 * physical address should be passed.
86 * Unexpected errors may occur if the address mapping is changed
87 * after this function is invoked.
89 * @return XST_SUCCESS always.
93 ******************************************************************************/
94 s32 XGpioPs_CfgInitialize(XGpioPs *InstancePtr, XGpioPs_Config *ConfigPtr,
97 s32 Status = XST_SUCCESS;
98 Xil_AssertNonvoid(InstancePtr != NULL);
99 Xil_AssertNonvoid(ConfigPtr != NULL);
100 Xil_AssertNonvoid(EffectiveAddr != (u32)0);
102 * Set some default values for instance data, don't indicate the device
103 * is ready to use until everything has been initialized successfully.
105 InstancePtr->IsReady = 0U;
106 InstancePtr->GpioConfig.BaseAddr = EffectiveAddr;
107 InstancePtr->GpioConfig.DeviceId = ConfigPtr->DeviceId;
108 InstancePtr->Handler = StubHandler;
111 * By default, interrupts are not masked in GPIO. Disable
112 * interrupts for all pins in all the 4 banks.
114 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
115 XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFFU);
117 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
118 ((u32)(1) * XGPIOPS_REG_MASK_OFFSET) +
119 XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFFU);
121 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
122 ((u32)(2) * XGPIOPS_REG_MASK_OFFSET) +
123 XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFFU);
125 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
126 ((u32)(3) * XGPIOPS_REG_MASK_OFFSET) +
127 XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFFU);
130 * Indicate the component is now ready to use.
132 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
137 /****************************************************************************/
140 * Read the Data register of the specified GPIO bank.
142 * @param InstancePtr is a pointer to the XGpioPs instance.
143 * @param Bank is the bank number of the GPIO to operate on.
144 * Valid values are 0 to XGPIOPS_MAX_BANKS - 1.
146 * @return Current value of the Data register.
148 * @note This function is used for reading the state of all the GPIO pins
151 *****************************************************************************/
152 u32 XGpioPs_Read(XGpioPs *InstancePtr, u8 Bank)
154 Xil_AssertNonvoid(InstancePtr != NULL);
155 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
156 Xil_AssertNonvoid(Bank < XGPIOPS_MAX_BANKS);
158 return XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
159 ((u32)(Bank) * XGPIOPS_DATA_BANK_OFFSET) +
160 XGPIOPS_DATA_RO_OFFSET);
163 /****************************************************************************/
166 * Read Data from the specified pin.
168 * @param InstancePtr is a pointer to the XGpioPs instance.
169 * @param Pin is the pin number for which the data has to be read.
170 * Valid values are 0 to XGPIOPS_DEVICE_MAX_PIN_NUM - 1.
171 * See xgpiops.h for the mapping of the pin numbers in the banks.
173 * @return Current value of the Pin (0 or 1).
175 * @note This function is used for reading the state of the specified
178 *****************************************************************************/
179 u32 XGpioPs_ReadPin(XGpioPs *InstancePtr, u32 Pin)
184 Xil_AssertNonvoid(InstancePtr != NULL);
185 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
186 Xil_AssertNonvoid(Pin < XGPIOPS_DEVICE_MAX_PIN_NUM);
189 * Get the Bank number and Pin number within the bank.
191 XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
193 return (XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
194 ((u32)(Bank) * XGPIOPS_DATA_BANK_OFFSET) +
195 XGPIOPS_DATA_RO_OFFSET) >> (u32)PinNumber) & (u32)1;
199 /****************************************************************************/
202 * Write to the Data register of the specified GPIO bank.
204 * @param InstancePtr is a pointer to the XGpioPs instance.
205 * @param Bank is the bank number of the GPIO to operate on.
206 * Valid values are 0 to XGPIOPS_MAX_BANKS - 1.
207 * @param Data is the value to be written to the Data register.
211 * @note This function is used for writing to all the GPIO pins of
212 * the bank. The previous state of the pins is not maintained.
214 *****************************************************************************/
215 void XGpioPs_Write(XGpioPs *InstancePtr, u8 Bank, u32 Data)
217 Xil_AssertVoid(InstancePtr != NULL);
218 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
219 Xil_AssertVoid(Bank < XGPIOPS_MAX_BANKS);
221 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
222 ((u32)(Bank) * XGPIOPS_DATA_BANK_OFFSET) +
223 XGPIOPS_DATA_OFFSET, Data);
226 /****************************************************************************/
229 * Write data to the specified pin.
231 * @param InstancePtr is a pointer to the XGpioPs instance.
232 * @param Pin is the pin number to which the Data is to be written.
233 * Valid values are 0 to XGPIOPS_DEVICE_MAX_PIN_NUM - 1.
234 * @param Data is the data to be written to the specified pin (0 or 1).
238 * @note This function does a masked write to the specified pin of
239 * the specified GPIO bank. The previous state of other pins
242 *****************************************************************************/
243 void XGpioPs_WritePin(XGpioPs *InstancePtr, u32 Pin, u32 Data)
251 Xil_AssertVoid(InstancePtr != NULL);
252 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
253 Xil_AssertVoid(Pin < XGPIOPS_DEVICE_MAX_PIN_NUM);
256 * Get the Bank number and Pin number within the bank.
258 XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
260 if (PinNumber > 15U) {
262 * There are only 16 data bits in bit maskable register.
265 RegOffset = XGPIOPS_DATA_MSW_OFFSET;
267 RegOffset = XGPIOPS_DATA_LSW_OFFSET;
271 * Get the 32 bit value to be written to the Mask/Data register where
272 * the upper 16 bits is the mask and lower 16 bits is the data.
274 DataVar &= (u32)0x01;
275 Value = ~((u32)1 << (PinNumber + 16U)) & ((DataVar << PinNumber) | 0xFFFF0000U);
276 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
277 ((u32)(Bank) * XGPIOPS_DATA_MASK_OFFSET) +
283 /****************************************************************************/
286 * Set the Direction of the pins of the specified GPIO Bank.
288 * @param InstancePtr is a pointer to the XGpioPs instance.
289 * @param Bank is the bank number of the GPIO to operate on.
290 * Valid values are 0 to XGPIOPS_MAX_BANKS - 1.
291 * @param Direction is the 32 bit mask of the Pin direction to be set for
292 * all the pins in the Bank. Bits with 0 are set to Input mode,
293 * bits with 1 are set to Output Mode.
297 * @note This function is used for setting the direction of all the pins
298 * in the specified bank. The previous state of the pins is
301 *****************************************************************************/
302 void XGpioPs_SetDirection(XGpioPs *InstancePtr, u8 Bank, u32 Direction)
304 Xil_AssertVoid(InstancePtr != NULL);
305 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
306 Xil_AssertVoid(Bank < XGPIOPS_MAX_BANKS);
308 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
309 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
310 XGPIOPS_DIRM_OFFSET, Direction);
313 /****************************************************************************/
316 * Set the Direction of the specified pin.
318 * @param InstancePtr is a pointer to the XGpioPs instance.
319 * @param Pin is the pin number to which the Data is to be written.
320 * Valid values are 0 to XGPIOPS_DEVICE_MAX_PIN_NUM - 1.
321 * @param Direction is the direction to be set for the specified pin.
322 * Valid values are 0 for Input Direction, 1 for Output Direction.
326 *****************************************************************************/
327 void XGpioPs_SetDirectionPin(XGpioPs *InstancePtr, u32 Pin, u32 Direction)
333 Xil_AssertVoid(InstancePtr != NULL);
334 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
335 Xil_AssertVoid(Pin < XGPIOPS_DEVICE_MAX_PIN_NUM);
336 Xil_AssertVoid(Direction <= (u32)1);
339 * Get the Bank number and Pin number within the bank.
341 XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
343 DirModeReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
344 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
345 XGPIOPS_DIRM_OFFSET);
347 if (Direction!=(u32)0) { /* Output Direction */
348 DirModeReg |= ((u32)1 << (u32)PinNumber);
349 } else { /* Input Direction */
350 DirModeReg &= ~ ((u32)1 << (u32)PinNumber);
353 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
354 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
355 XGPIOPS_DIRM_OFFSET, DirModeReg);
358 /****************************************************************************/
361 * Get the Direction of the pins of the specified GPIO Bank.
363 * @param InstancePtr is a pointer to the XGpioPs instance.
364 * @param Bank is the bank number of the GPIO to operate on.
365 * Valid values are 0 to XGPIOPS_MAX_BANKS - 1.
367 * return Returns a 32 bit mask of the Direction register. Bits with 0 are
368 * in Input mode, bits with 1 are in Output Mode.
372 *****************************************************************************/
373 u32 XGpioPs_GetDirection(XGpioPs *InstancePtr, u8 Bank)
375 Xil_AssertNonvoid(InstancePtr != NULL);
376 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
377 Xil_AssertNonvoid(Bank < XGPIOPS_MAX_BANKS);
379 return XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
380 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
381 XGPIOPS_DIRM_OFFSET);
384 /****************************************************************************/
387 * Get the Direction of the specified pin.
389 * @param InstancePtr is a pointer to the XGpioPs instance.
390 * @param Pin is the pin number for which the Direction is to be
392 * Valid values are 0 to XGPIOPS_DEVICE_MAX_PIN_NUM - 1.
394 * @return Direction of the specified pin.
395 * - 0 for Input Direction
396 * - 1 for Output Direction
400 *****************************************************************************/
401 u32 XGpioPs_GetDirectionPin(XGpioPs *InstancePtr, u32 Pin)
406 Xil_AssertNonvoid(InstancePtr != NULL);
407 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
408 Xil_AssertNonvoid(Pin < XGPIOPS_DEVICE_MAX_PIN_NUM);
411 * Get the Bank number and Pin number within the bank.
413 XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
415 return (XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
416 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
417 XGPIOPS_DIRM_OFFSET) >> (u32)PinNumber) & (u32)1;
420 /****************************************************************************/
423 * Set the Output Enable of the pins of the specified GPIO Bank.
425 * @param InstancePtr is a pointer to the XGpioPs instance.
426 * @param Bank is the bank number of the GPIO to operate on.
427 * Valid values are 0 to XGPIOPS_MAX_BANKS - 1.
428 * @param OpEnable is the 32 bit mask of the Output Enables to be set for
429 * all the pins in the Bank. The Output Enable of bits with 0 are
430 * disabled, the Output Enable of bits with 1 are enabled.
434 * @note This function is used for setting the Output Enables of all the
435 * pins in the specified bank. The previous state of the Output
436 * Enables is not maintained.
438 *****************************************************************************/
439 void XGpioPs_SetOutputEnable(XGpioPs *InstancePtr, u8 Bank, u32 OpEnable)
441 Xil_AssertVoid(InstancePtr != NULL);
442 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
443 Xil_AssertVoid(Bank < XGPIOPS_MAX_BANKS);
445 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
446 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
447 XGPIOPS_OUTEN_OFFSET, OpEnable);
450 /****************************************************************************/
453 * Set the Output Enable of the specified pin.
455 * @param InstancePtr is a pointer to the XGpioPs instance.
456 * @param Pin is the pin number to which the Data is to be written.
457 * Valid values are 0 to XGPIOPS_DEVICE_MAX_PIN_NUM - 1.
458 * @param OpEnable specifies whether the Output Enable for the specified
459 * pin should be enabled.
460 * Valid values are 0 for Disabling Output Enable,
461 * 1 for Enabling Output Enable.
467 *****************************************************************************/
468 void XGpioPs_SetOutputEnablePin(XGpioPs *InstancePtr, u32 Pin, u32 OpEnable)
474 Xil_AssertVoid(InstancePtr != NULL);
475 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
476 Xil_AssertVoid(Pin < XGPIOPS_DEVICE_MAX_PIN_NUM);
477 Xil_AssertVoid(OpEnable <= (u32)1);
480 * Get the Bank number and Pin number within the bank.
482 XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
484 OpEnableReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
485 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
486 XGPIOPS_OUTEN_OFFSET);
488 if (OpEnable != (u32)0) { /* Enable Output Enable */
489 OpEnableReg |= ((u32)1 << (u32)PinNumber);
490 } else { /* Disable Output Enable */
491 OpEnableReg &= ~ ((u32)1 << (u32)PinNumber);
494 XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
495 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
496 XGPIOPS_OUTEN_OFFSET, OpEnableReg);
498 /****************************************************************************/
501 * Get the Output Enable status of the pins of the specified GPIO Bank.
503 * @param InstancePtr is a pointer to the XGpioPs instance.
504 * @param Bank is the bank number of the GPIO to operate on.
505 * Valid values are 0 to XGPIOPS_MAX_BANKS - 1.
507 * return Returns a a 32 bit mask of the Output Enable register.
508 * Bits with 0 are in Disabled state, bits with 1 are in
513 *****************************************************************************/
514 u32 XGpioPs_GetOutputEnable(XGpioPs *InstancePtr, u8 Bank)
516 Xil_AssertNonvoid(InstancePtr != NULL);
517 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
518 Xil_AssertNonvoid(Bank < XGPIOPS_MAX_BANKS);
520 return XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
521 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
522 XGPIOPS_OUTEN_OFFSET);
525 /****************************************************************************/
528 * Get the Output Enable status of the specified pin.
530 * @param InstancePtr is a pointer to the XGpioPs instance.
531 * @param Pin is the pin number for which the Output Enable status is to
533 * Valid values are 0 to XGPIOPS_DEVICE_MAX_PIN_NUM - 1.
535 * @return Output Enable of the specified pin.
536 * - 0 if Output Enable is disabled for this pin
537 * - 1 if Output Enable is enabled for this pin
541 *****************************************************************************/
542 u32 XGpioPs_GetOutputEnablePin(XGpioPs *InstancePtr, u32 Pin)
547 Xil_AssertNonvoid(InstancePtr != NULL);
548 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
549 Xil_AssertNonvoid(Pin < XGPIOPS_DEVICE_MAX_PIN_NUM);
552 * Get the Bank number and Pin number within the bank.
554 XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
556 return (XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
557 ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
558 XGPIOPS_OUTEN_OFFSET) >> (u32)PinNumber) & (u32)1;
561 /****************************************************************************/
564 * Get the Bank number and the Pin number in the Bank, for the given PinNumber
565 * in the GPIO device.
567 * @param PinNumber is the Pin number in the GPIO device.
568 * @param BankNumber returns the Bank in which this GPIO pin is present.
569 * Valid values are 0 to XGPIOPS_MAX_BANKS - 1.
570 * @param PinNumberInBank returns the Pin Number within the Bank.
576 *****************************************************************************/
577 void XGpioPs_GetBankPin(u8 PinNumber, u8 *BankNumber, u8 *PinNumberInBank)
580 * This structure defines the mapping of the pin numbers to the banks when
581 * the driver APIs are used for working on the individual pins.
583 #ifdef XPAR_PSU_GPIO_0_BASEADDR
584 u32 XGpioPsPinTable[] = {
585 (u32)25, /* 0 - 25, Bank 0 */
586 (u32)51, /* 26 - 51, Bank 1 */
587 (u32)77, /* 52 - 77, Bank 2 */
588 (u32)109, /* 78 - 109, Bank 3 */
589 (u32)141, /* 110 - 141, Bank 4 */
590 (u32)173 /* 142 - 173 Bank 5 */
593 while (*BankNumber < 6U) {
594 if (PinNumber <= XGpioPsPinTable[*BankNumber]) {
600 u32 XGpioPsPinTable[] = {
601 (u32)31, /* 0 - 31, Bank 0 */
602 (u32)53, /* 32 - 53, Bank 1 */
603 (u32)85, /* 54 - 85, Bank 2 */
604 (u32)117 /* 86 - 117 Bank 3 */
607 while (*BankNumber < 4U) {
608 if (PinNumber <= XGpioPsPinTable[*BankNumber]) {
614 if (*BankNumber == (u8)0) {
615 *PinNumberInBank = PinNumber;
617 *PinNumberInBank = (u8)((u32)PinNumber %
618 (XGpioPsPinTable[*BankNumber - (u8)1] + (u32)1));