xgpiops.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /******************************************************************************
  2. * Copyright (C) 2010 - 2020 Xilinx, Inc. All rights reserved.
  3. * SPDX-License-Identifier: MIT
  4. ******************************************************************************/
  5. /*****************************************************************************/
  6. /**
  7. *
  8. * @file xgpiops.c
  9. * @addtogroup gpiops_v3_7
  10. * @{
  11. *
  12. * The XGpioPs driver. Functions in this file are the minimum required functions
  13. * for this driver. See xgpiops.h for a detailed description of the driver.
  14. *
  15. * <pre>
  16. * MODIFICATION HISTORY:
  17. *
  18. * Ver Who Date Changes
  19. * ----- ---- -------- -----------------------------------------------
  20. * 1.00a sv 01/15/10 First Release
  21. * 1.01a sv 04/15/12 Removed the APIs XGpioPs_SetMode, XGpioPs_SetModePin
  22. * XGpioPs_GetMode, XGpioPs_GetModePin as they are not
  23. * relevant to Zynq device. The interrupts are disabled
  24. * for output pins on all banks during initialization.
  25. * 2.1 hk 04/29/14 Use Input data register DATA_RO for read. CR# 771667.
  26. * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
  27. * 3.1 kvn 04/13/15 Add support for Zynq Ultrascale+ MP. CR# 856980.
  28. * 3.1 aru 07/13/18 Resolved doxygen reported warnings. CR# 1006331.
  29. * 3.4 aru 08/17/18 Resolved MISRA-C mandatory violations. CR# 1007751
  30. * 3.5 sne 03/01/19 Fixes violations according to MISRAC-2012
  31. * in safety mode and modified the code such as
  32. * Use of mixed mode arithmetic,Declared the pointer param
  33. * as Pointer to const,Casting operation to a pointer,
  34. * Literal value requires a U suffix.
  35. * 3.5 sne 03/13/19 Added Versal support.
  36. * </pre>
  37. *
  38. ******************************************************************************/
  39. /***************************** Include Files *********************************/
  40. #include "xgpiops.h"
  41. /************************** Constant Definitions *****************************/
  42. /**************************** Type Definitions *******************************/
  43. /***************** Macros (Inline Functions) Definitions *********************/
  44. /************************** Variable Definitions *****************************/
  45. /************************** Function Prototypes ******************************/
  46. extern void StubHandler(void *CallBackRef, u32 Bank, u32 Status);
  47. /*****************************************************************************/
  48. /*
  49. *
  50. * This function initializes a XGpioPs instance/driver.
  51. * All members of the XGpioPs instance structure are initialized and
  52. * StubHandlers are assigned to the Bank Status Handlers.
  53. *
  54. * @param InstancePtr is a pointer to the XGpioPs instance.
  55. * @param ConfigPtr points to the XGpioPs device configuration structure.
  56. * @param EffectiveAddr is the device base address in the virtual memory
  57. * address space. If the address translation is not used then the
  58. * physical address should be passed.
  59. * Unexpected errors may occur if the address mapping is changed
  60. * after this function is invoked.
  61. *
  62. * @return XST_SUCCESS always.
  63. *
  64. * @note None.
  65. *
  66. ******************************************************************************/
  67. s32 XGpioPs_CfgInitialize(XGpioPs *InstancePtr, const XGpioPs_Config *ConfigPtr,
  68. u32 EffectiveAddr)
  69. {
  70. s32 Status = XST_SUCCESS;
  71. u8 i;
  72. Xil_AssertNonvoid(InstancePtr != NULL);
  73. Xil_AssertNonvoid(ConfigPtr != NULL);
  74. Xil_AssertNonvoid(EffectiveAddr != (u32)0);
  75. /*
  76. * Set some default values for instance data, don't indicate the device
  77. * is ready to use until everything has been initialized successfully.
  78. */
  79. InstancePtr->IsReady = 0U;
  80. InstancePtr->GpioConfig.BaseAddr = EffectiveAddr;
  81. InstancePtr->GpioConfig.DeviceId = ConfigPtr->DeviceId;
  82. InstancePtr->Handler = (XGpioPs_Handler)StubHandler;
  83. InstancePtr->Platform = XGetPlatform_Info();
  84. /* Initialize the Bank data based on platform */
  85. if (InstancePtr->Platform == (u32)XPLAT_ZYNQ_ULTRA_MP) {
  86. /*
  87. * Max pins in the ZynqMP GPIO device
  88. * 0 - 25, Bank 0
  89. * 26 - 51, Bank 1
  90. * 52 - 77, Bank 2
  91. * 78 - 109, Bank 3
  92. * 110 - 141, Bank 4
  93. * 142 - 173, Bank 5
  94. */
  95. InstancePtr->MaxPinNum = (u32)174;
  96. InstancePtr->MaxBanks = (u8)6;
  97. }
  98. else if (InstancePtr->Platform == (u32)XPLAT_VERSAL)
  99. {
  100. if(InstancePtr->PmcGpio == (u32)FALSE)
  101. {
  102. /* Max pins in the PS_GPIO devices
  103. * 0 -25, Bank 0
  104. * 26-57, Bank 3
  105. */
  106. InstancePtr->MaxPinNum = (u32)58;
  107. InstancePtr->MaxBanks = (u8)4;
  108. }
  109. else
  110. {
  111. /* Max pins in the PMC_GPIO devices
  112. * 0 - 25,Bank 0
  113. * 26 - 51,Bank 1
  114. * 52 - 83,Bank 3
  115. * 84 - 115, Bank 4
  116. */
  117. InstancePtr->MaxPinNum = (u32)116;
  118. InstancePtr->MaxBanks = (u8)5;
  119. }
  120. }
  121. else {
  122. /*
  123. * Max pins in the GPIO device
  124. * 0 - 31, Bank 0
  125. * 32 - 53, Bank 1
  126. * 54 - 85, Bank 2
  127. * 86 - 117, Bank 3
  128. */
  129. InstancePtr->MaxPinNum = (u32)118;
  130. InstancePtr->MaxBanks = (u8)4;
  131. }
  132. /*
  133. * By default, interrupts are not masked in GPIO. Disable
  134. * interrupts for all pins in all the 4 banks.
  135. */
  136. for (i=(u8)0U;i<InstancePtr->MaxBanks;i++) {
  137. if (InstancePtr->Platform == XPLAT_VERSAL){
  138. if(InstancePtr->PmcGpio == (u32)FALSE)
  139. {
  140. if((i== (u8)XGPIOPS_ONE)||(i== (u8)XGPIOPS_TWO))
  141. {
  142. continue;
  143. }
  144. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
  145. ((u32)(i) * XGPIOPS_REG_MASK_OFFSET) +
  146. XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFFU);
  147. }
  148. else
  149. {
  150. if(i==(u32)XGPIOPS_TWO)
  151. {
  152. continue;
  153. }
  154. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
  155. ((u32)(i) * XGPIOPS_REG_MASK_OFFSET) +
  156. XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFFU);
  157. }
  158. }
  159. else
  160. {
  161. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
  162. ((u32)(i) * XGPIOPS_REG_MASK_OFFSET) +
  163. XGPIOPS_INTDIS_OFFSET, 0xFFFFFFFFU);
  164. }
  165. }
  166. /* Indicate the component is now ready to use. */
  167. InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
  168. return Status;
  169. }
  170. /****************************************************************************/
  171. /**
  172. *
  173. * Read the Data register of the specified GPIO bank.
  174. *
  175. * @param InstancePtr is a pointer to the XGpioPs instance.
  176. * @param Bank is the bank number of the GPIO to operate on.
  177. * Valid values are 0-3 in Zynq and 0-5 in Zynq Ultrascale+ MP.
  178. *
  179. * @return Current value of the Data register.
  180. *
  181. * @note This function is used for reading the state of all the GPIO pins
  182. * of specified bank.
  183. *
  184. *****************************************************************************/
  185. u32 XGpioPs_Read(const XGpioPs *InstancePtr, u8 Bank)
  186. {
  187. Xil_AssertNonvoid(InstancePtr != NULL);
  188. Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  189. Xil_AssertNonvoid(Bank < InstancePtr->MaxBanks);
  190. #ifdef versal
  191. if(InstancePtr->PmcGpio == TRUE) {
  192. Xil_AssertNonvoid(Bank != XGPIOPS_TWO);
  193. } else {
  194. Xil_AssertNonvoid((Bank !=XGPIOPS_ONE) && (Bank !=XGPIOPS_TWO));
  195. }
  196. #endif
  197. return XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
  198. ((u32)(Bank) * XGPIOPS_DATA_BANK_OFFSET) +
  199. XGPIOPS_DATA_RO_OFFSET);
  200. }
  201. /****************************************************************************/
  202. /**
  203. *
  204. * Read Data from the specified pin.
  205. *
  206. * @param InstancePtr is a pointer to the XGpioPs instance.
  207. * @param Pin is the pin number for which the data has to be read.
  208. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
  209. * See xgpiops.h for the mapping of the pin numbers in the banks.
  210. *
  211. * @return Current value of the Pin (0 or 1).
  212. *
  213. * @note This function is used for reading the state of the specified
  214. * GPIO pin.
  215. *
  216. *****************************************************************************/
  217. u32 XGpioPs_ReadPin(const XGpioPs *InstancePtr, u32 Pin)
  218. {
  219. u8 Bank;
  220. u8 PinNumber;
  221. Xil_AssertNonvoid(InstancePtr != NULL);
  222. Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  223. Xil_AssertNonvoid(Pin < InstancePtr->MaxPinNum);
  224. /* Get the Bank number and Pin number within the bank. */
  225. #ifdef versal
  226. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
  227. #else
  228. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
  229. #endif
  230. return (XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
  231. ((u32)(Bank) * XGPIOPS_DATA_BANK_OFFSET) +
  232. XGPIOPS_DATA_RO_OFFSET) >> (u32)PinNumber) & (u32)1;
  233. }
  234. /****************************************************************************/
  235. /**
  236. *
  237. * Write to the Data register of the specified GPIO bank.
  238. *
  239. * @param InstancePtr is a pointer to the XGpioPs instance.
  240. * @param Bank is the bank number of the GPIO to operate on.
  241. * Valid values are 0-3 in Zynq and 0-5 in Zynq Ultrascale+ MP.
  242. * @param Data is the value to be written to the Data register.
  243. *
  244. * @return None.
  245. *
  246. * @note This function is used for writing to all the GPIO pins of
  247. * the bank. The previous state of the pins is not maintained.
  248. *
  249. *****************************************************************************/
  250. void XGpioPs_Write(const XGpioPs *InstancePtr, u8 Bank, u32 Data)
  251. {
  252. Xil_AssertVoid(InstancePtr != NULL);
  253. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  254. Xil_AssertVoid(Bank < InstancePtr->MaxBanks);
  255. #ifdef versal
  256. if(InstancePtr->PmcGpio == TRUE) {
  257. Xil_AssertVoid(Bank != XGPIOPS_TWO);
  258. } else {
  259. Xil_AssertVoid((Bank !=XGPIOPS_ONE) && (Bank !=XGPIOPS_TWO));
  260. }
  261. #endif
  262. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
  263. ((u32)(Bank) * XGPIOPS_DATA_BANK_OFFSET) +
  264. XGPIOPS_DATA_OFFSET, Data);
  265. }
  266. /****************************************************************************/
  267. /**
  268. *
  269. * Write data to the specified pin.
  270. *
  271. * @param InstancePtr is a pointer to the XGpioPs instance.
  272. * @param Pin is the pin number to which the Data is to be written.
  273. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
  274. * @param Data is the data to be written to the specified pin (0 or 1).
  275. *
  276. * @return None.
  277. *
  278. * @note This function does a masked write to the specified pin of
  279. * the specified GPIO bank. The previous state of other pins
  280. * is maintained.
  281. *
  282. *****************************************************************************/
  283. void XGpioPs_WritePin(const XGpioPs *InstancePtr, u32 Pin, u32 Data)
  284. {
  285. u32 RegOffset;
  286. u32 Value;
  287. u8 Bank;
  288. u8 PinNumber;
  289. u32 DataVar = Data;
  290. Xil_AssertVoid(InstancePtr != NULL);
  291. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  292. Xil_AssertVoid(Pin < InstancePtr->MaxPinNum);
  293. /* Get the Bank number and Pin number within the bank. */
  294. #ifdef versal
  295. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
  296. #else
  297. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
  298. #endif
  299. if (PinNumber > 15U) {
  300. /* There are only 16 data bits in bit maskable register. */
  301. PinNumber -= (u8)16;
  302. RegOffset = XGPIOPS_DATA_MSW_OFFSET;
  303. } else {
  304. RegOffset = XGPIOPS_DATA_LSW_OFFSET;
  305. }
  306. /*
  307. * Get the 32 bit value to be written to the Mask/Data register where
  308. * the upper 16 bits is the mask and lower 16 bits is the data.
  309. */
  310. DataVar &= (u32)0x01;
  311. Value = ~((u32)1 << (PinNumber + 16U)) & ((DataVar << PinNumber) | 0xFFFF0000U);
  312. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
  313. ((u32)(Bank) * XGPIOPS_DATA_MASK_OFFSET) +
  314. RegOffset, Value);
  315. }
  316. /****************************************************************************/
  317. /**
  318. *
  319. * Set the Direction of the pins of the specified GPIO Bank.
  320. *
  321. * @param InstancePtr is a pointer to the XGpioPs instance.
  322. * @param Bank is the bank number of the GPIO to operate on.
  323. * Valid values are 0-3 in Zynq and 0-5 in Zynq Ultrascale+ MP.
  324. * @param Direction is the 32 bit mask of the Pin direction to be set for
  325. * all the pins in the Bank. Bits with 0 are set to Input mode,
  326. * bits with 1 are set to Output Mode.
  327. *
  328. * @return None.
  329. *
  330. * @note This function is used for setting the direction of all the pins
  331. * in the specified bank. The previous state of the pins is
  332. * not maintained.
  333. *
  334. *****************************************************************************/
  335. void XGpioPs_SetDirection(const XGpioPs *InstancePtr, u8 Bank, u32 Direction)
  336. {
  337. Xil_AssertVoid(InstancePtr != NULL);
  338. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  339. Xil_AssertVoid(Bank < InstancePtr->MaxBanks);
  340. #ifdef versal
  341. if(InstancePtr->PmcGpio == TRUE) {
  342. Xil_AssertVoid(Bank != XGPIOPS_TWO);
  343. } else {
  344. Xil_AssertVoid((Bank !=XGPIOPS_ONE) && (Bank !=XGPIOPS_TWO));
  345. }
  346. #endif
  347. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
  348. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  349. XGPIOPS_DIRM_OFFSET, Direction);
  350. }
  351. /****************************************************************************/
  352. /**
  353. *
  354. * Set the Direction of the specified pin.
  355. *
  356. * @param InstancePtr is a pointer to the XGpioPs instance.
  357. * @param Pin is the pin number to which the Data is to be written.
  358. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
  359. * @param Direction is the direction to be set for the specified pin.
  360. * Valid values are 0 for Input Direction, 1 for Output Direction.
  361. *
  362. * @return None.
  363. *
  364. *****************************************************************************/
  365. void XGpioPs_SetDirectionPin(const XGpioPs *InstancePtr, u32 Pin, u32 Direction)
  366. {
  367. u8 Bank;
  368. u8 PinNumber;
  369. u32 DirModeReg;
  370. Xil_AssertVoid(InstancePtr != NULL);
  371. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  372. Xil_AssertVoid(Pin < InstancePtr->MaxPinNum);
  373. Xil_AssertVoid(Direction <= (u32)1);
  374. /* Get the Bank number and Pin number within the bank. */
  375. #ifdef versal
  376. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
  377. #else
  378. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
  379. #endif
  380. DirModeReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
  381. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  382. XGPIOPS_DIRM_OFFSET);
  383. if (Direction!=(u32)0) { /* Output Direction */
  384. DirModeReg |= ((u32)1 << (u32)PinNumber);
  385. } else { /* Input Direction */
  386. DirModeReg &= ~ ((u32)1 << (u32)PinNumber);
  387. }
  388. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
  389. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  390. XGPIOPS_DIRM_OFFSET, DirModeReg);
  391. }
  392. /****************************************************************************/
  393. /**
  394. *
  395. * Get the Direction of the pins of the specified GPIO Bank.
  396. *
  397. * @param InstancePtr is a pointer to the XGpioPs instance.
  398. * @param Bank is the bank number of the GPIO to operate on.
  399. * Valid values are 0-3 in Zynq and 0-5 in Zynq Ultrascale+ MP.
  400. *
  401. * @return Returns a 32 bit mask of the Direction register. Bits with 0 are
  402. * in Input mode, bits with 1 are in Output Mode.
  403. *
  404. * @note None.
  405. *
  406. *****************************************************************************/
  407. u32 XGpioPs_GetDirection(const XGpioPs *InstancePtr, u8 Bank)
  408. {
  409. Xil_AssertNonvoid(InstancePtr != NULL);
  410. Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  411. Xil_AssertNonvoid(Bank < InstancePtr->MaxBanks);
  412. #ifdef versal
  413. if(InstancePtr->PmcGpio == TRUE) {
  414. Xil_AssertNonvoid(Bank != XGPIOPS_TWO);
  415. } else {
  416. Xil_AssertNonvoid((Bank !=XGPIOPS_ONE) && (Bank !=XGPIOPS_TWO));
  417. }
  418. #endif
  419. return XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
  420. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  421. XGPIOPS_DIRM_OFFSET);
  422. }
  423. /****************************************************************************/
  424. /**
  425. *
  426. * Get the Direction of the specified pin.
  427. *
  428. * @param InstancePtr is a pointer to the XGpioPs instance.
  429. * @param Pin is the pin number for which the Direction is to be
  430. * retrieved.
  431. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
  432. *
  433. * @return Direction of the specified pin.
  434. * - 0 for Input Direction
  435. * - 1 for Output Direction
  436. *
  437. * @note None.
  438. *
  439. *****************************************************************************/
  440. u32 XGpioPs_GetDirectionPin(const XGpioPs *InstancePtr, u32 Pin)
  441. {
  442. u8 Bank;
  443. u8 PinNumber;
  444. Xil_AssertNonvoid(InstancePtr != NULL);
  445. Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  446. Xil_AssertNonvoid(Pin < InstancePtr->MaxPinNum);
  447. /* Get the Bank number and Pin number within the bank. */
  448. #ifdef versal
  449. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
  450. #else
  451. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
  452. #endif
  453. return (XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
  454. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  455. XGPIOPS_DIRM_OFFSET) >> (u32)PinNumber) & (u32)1;
  456. }
  457. /****************************************************************************/
  458. /**
  459. *
  460. * Set the Output Enable of the pins of the specified GPIO Bank.
  461. *
  462. * @param InstancePtr is a pointer to the XGpioPs instance.
  463. * @param Bank is the bank number of the GPIO to operate on.
  464. * Valid values are 0-3 in Zynq and 0-5 in Zynq Ultrascale+ MP.
  465. * @param OpEnable is the 32 bit mask of the Output Enables to be set for
  466. * all the pins in the Bank. The Output Enable of bits with 0 are
  467. * disabled, the Output Enable of bits with 1 are enabled.
  468. *
  469. * @return None.
  470. *
  471. * @note This function is used for setting the Output Enables of all the
  472. * pins in the specified bank. The previous state of the Output
  473. * Enables is not maintained.
  474. *
  475. *****************************************************************************/
  476. void XGpioPs_SetOutputEnable(const XGpioPs *InstancePtr, u8 Bank, u32 OpEnable)
  477. {
  478. Xil_AssertVoid(InstancePtr != NULL);
  479. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  480. Xil_AssertVoid(Bank < InstancePtr->MaxBanks);
  481. #ifdef versal
  482. if(InstancePtr->PmcGpio == TRUE) {
  483. Xil_AssertVoid(Bank != XGPIOPS_TWO);
  484. } else {
  485. Xil_AssertVoid((Bank !=XGPIOPS_ONE) && (Bank !=XGPIOPS_TWO));
  486. }
  487. #endif
  488. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
  489. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  490. XGPIOPS_OUTEN_OFFSET, OpEnable);
  491. }
  492. /****************************************************************************/
  493. /**
  494. *
  495. * Set the Output Enable of the specified pin.
  496. *
  497. * @param InstancePtr is a pointer to the XGpioPs instance.
  498. * @param Pin is the pin number to which the Data is to be written.
  499. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
  500. * @param OpEnable specifies whether the Output Enable for the specified
  501. * pin should be enabled.
  502. * Valid values are 0 for Disabling Output Enable,
  503. * 1 for Enabling Output Enable.
  504. *
  505. * @return None.
  506. *
  507. * @note None.
  508. *
  509. *****************************************************************************/
  510. void XGpioPs_SetOutputEnablePin(const XGpioPs *InstancePtr, u32 Pin, u32 OpEnable)
  511. {
  512. u8 Bank;
  513. u8 PinNumber;
  514. u32 OpEnableReg;
  515. Xil_AssertVoid(InstancePtr != NULL);
  516. Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  517. Xil_AssertVoid(Pin < InstancePtr->MaxPinNum);
  518. Xil_AssertVoid(OpEnable <= (u32)1);
  519. /* Get the Bank number and Pin number within the bank. */
  520. #ifdef versal
  521. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
  522. #else
  523. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
  524. #endif
  525. OpEnableReg = XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
  526. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  527. XGPIOPS_OUTEN_OFFSET);
  528. if (OpEnable != (u32)0) { /* Enable Output Enable */
  529. OpEnableReg |= ((u32)1 << (u32)PinNumber);
  530. } else { /* Disable Output Enable */
  531. OpEnableReg &= ~ ((u32)1 << (u32)PinNumber);
  532. }
  533. XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,
  534. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  535. XGPIOPS_OUTEN_OFFSET, OpEnableReg);
  536. }
  537. /****************************************************************************/
  538. /**
  539. *
  540. * Get the Output Enable status of the pins of the specified GPIO Bank.
  541. *
  542. * @param InstancePtr is a pointer to the XGpioPs instance.
  543. * @param Bank is the bank number of the GPIO to operate on.
  544. * Valid values are 0-3 in Zynq and 0-5 in Zynq Ultrascale+ MP.
  545. *
  546. * @return Returns a a 32 bit mask of the Output Enable register.
  547. * Bits with 0 are in Disabled state, bits with 1 are in
  548. * Enabled State.
  549. *
  550. * @note None.
  551. *
  552. *****************************************************************************/
  553. u32 XGpioPs_GetOutputEnable(const XGpioPs *InstancePtr, u8 Bank)
  554. {
  555. Xil_AssertNonvoid(InstancePtr != NULL);
  556. Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  557. Xil_AssertNonvoid(Bank < InstancePtr->MaxBanks);
  558. #ifdef versal
  559. if(InstancePtr->PmcGpio == TRUE) {
  560. Xil_AssertNonvoid(Bank != XGPIOPS_TWO);
  561. } else {
  562. Xil_AssertNonvoid((Bank !=XGPIOPS_ONE) && (Bank !=XGPIOPS_TWO));
  563. }
  564. #endif
  565. return XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
  566. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  567. XGPIOPS_OUTEN_OFFSET);
  568. }
  569. /****************************************************************************/
  570. /**
  571. *
  572. * Get the Output Enable status of the specified pin.
  573. *
  574. * @param InstancePtr is a pointer to the XGpioPs instance.
  575. * @param Pin is the pin number for which the Output Enable status is to
  576. * be retrieved.
  577. * Valid values are 0-117 in Zynq and 0-173 in Zynq Ultrascale+ MP.
  578. *
  579. * @return Output Enable of the specified pin.
  580. * - 0 if Output Enable is disabled for this pin
  581. * - 1 if Output Enable is enabled for this pin
  582. *
  583. * @note None.
  584. *
  585. *****************************************************************************/
  586. u32 XGpioPs_GetOutputEnablePin(const XGpioPs *InstancePtr, u32 Pin)
  587. {
  588. u8 Bank;
  589. u8 PinNumber;
  590. Xil_AssertNonvoid(InstancePtr != NULL);
  591. Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
  592. Xil_AssertNonvoid(Pin < InstancePtr->MaxPinNum);
  593. /* Get the Bank number and Pin number within the bank. */
  594. #ifdef versal
  595. XGpioPs_GetBankPin(InstancePtr,(u8)Pin, &Bank, &PinNumber);
  596. #else
  597. XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);
  598. #endif
  599. return (XGpioPs_ReadReg(InstancePtr->GpioConfig.BaseAddr,
  600. ((u32)(Bank) * XGPIOPS_REG_MASK_OFFSET) +
  601. XGPIOPS_OUTEN_OFFSET) >> (u32)PinNumber) & (u32)1;
  602. }
  603. /****************************************************************************/
  604. /*
  605. *
  606. * Get the Bank number and the Pin number in the Bank, for the given PinNumber
  607. * in the GPIO device.
  608. *
  609. * @param PinNumber is the Pin number in the GPIO device.
  610. * @param BankNumber returns the Bank in which this GPIO pin is present.
  611. * Valid values are 0 to XGPIOPS_MAX_BANKS - 1.
  612. * @param PinNumberInBank returns the Pin Number within the Bank.
  613. *
  614. * @return None.
  615. *
  616. * @note None.
  617. *
  618. *****************************************************************************/
  619. #ifdef versal
  620. void XGpioPs_GetBankPin(const XGpioPs *InstancePtr,u8 PinNumber, u8 *BankNumber, u8 *PinNumberInBank)
  621. #else
  622. void XGpioPs_GetBankPin(u8 PinNumber, u8 *BankNumber, u8 *PinNumberInBank)
  623. #endif
  624. {
  625. u32 XGpioPsPinTable[6] = {0};
  626. #ifdef versal
  627. u8 i=(u8)0;
  628. #endif
  629. u32 Platform = XGetPlatform_Info();
  630. if (Platform == (u32)XPLAT_ZYNQ_ULTRA_MP) {
  631. /*
  632. * This structure defines the mapping of the pin numbers to the banks when
  633. * the driver APIs are used for working on the individual pins.
  634. */
  635. XGpioPsPinTable[0] = (u32)25; /* 0 - 25, Bank 0 */
  636. XGpioPsPinTable[1] = (u32)51; /* 26 - 51, Bank 1 */
  637. XGpioPsPinTable[2] = (u32)77; /* 52 - 77, Bank 2 */
  638. XGpioPsPinTable[3] = (u32)109; /* 78 - 109, Bank 3 */
  639. XGpioPsPinTable[4] = (u32)141; /* 110 - 141, Bank 4 */
  640. XGpioPsPinTable[5] = (u32)173; /* 142 - 173 Bank 5 */
  641. *BankNumber = 0U;
  642. while (*BankNumber < XGPIOPS_SIX) {
  643. if (PinNumber <= XGpioPsPinTable[*BankNumber]) {
  644. break;
  645. }
  646. (*BankNumber)++;
  647. }
  648. }
  649. #ifdef versal
  650. else if(Platform == XPLAT_VERSAL)
  651. {
  652. if(InstancePtr->PmcGpio == (u32)(FALSE))
  653. {
  654. XGpioPsPinTable[0] = (u32)25; /* 0 - 25, Bank 0 */
  655. XGpioPsPinTable[1] = (u32)57; /* 26 - 57, Bank 3 */
  656. *BankNumber =0U;
  657. if(PinNumber <= XGpioPsPinTable[*BankNumber])
  658. {
  659. *BankNumber = (u8)XGPIOPS_ZERO;
  660. }
  661. else
  662. {
  663. *BankNumber = (u8)XGPIOPS_THREE;
  664. }
  665. }
  666. else
  667. {
  668. XGpioPsPinTable[0] = (u32)25; /* 0 - 25, Bank 0 */
  669. XGpioPsPinTable[1] = (u32)51; /* 26 - 51, Bank 1 */
  670. XGpioPsPinTable[2] = (u32)83; /* 52 - 83, Bank 3 */
  671. XGpioPsPinTable[3] = (u32)115; /*84 - 115, Bank 4 */
  672. *BankNumber =0U;
  673. while(i < XGPIOPS_FOUR)
  674. {
  675. if(i <= (u8)XGPIOPS_ONE)
  676. {
  677. if (PinNumber <= XGpioPsPinTable[i])
  678. {
  679. *BankNumber = (u8)i;
  680. break;
  681. }
  682. i++;
  683. }
  684. else
  685. {
  686. if (PinNumber <= XGpioPsPinTable[i])
  687. {
  688. *BankNumber = (u8)i+1U;
  689. break;
  690. }
  691. i++;
  692. }
  693. }
  694. }
  695. }
  696. #endif
  697. else {
  698. XGpioPsPinTable[0] = (u32)31; /* 0 - 31, Bank 0 */
  699. XGpioPsPinTable[1] = (u32)53; /* 32 - 53, Bank 1 */
  700. XGpioPsPinTable[2] = (u32)85; /* 54 - 85, Bank 2 */
  701. XGpioPsPinTable[3] = (u32)117; /* 86 - 117 Bank 3 */
  702. *BankNumber = 0U;
  703. while (*BankNumber < XGPIOPS_FOUR) {
  704. if (PinNumber <= XGpioPsPinTable[*BankNumber]) {
  705. break;
  706. }
  707. (*BankNumber)++;
  708. }
  709. }
  710. if (*BankNumber == (u8)0) {
  711. *PinNumberInBank = PinNumber;
  712. }
  713. #ifdef versal
  714. else if(Platform == XPLAT_VERSAL)
  715. {
  716. if(InstancePtr->PmcGpio == (u32)(FALSE))
  717. {
  718. *PinNumberInBank = (u8)((u32)PinNumber - (XGpioPsPinTable[0] + (u32)1));
  719. }
  720. else {
  721. if((*BankNumber ==(u8)XGPIOPS_THREE) || (*BankNumber ==(u8)XGPIOPS_FOUR))
  722. {
  723. *PinNumberInBank = (u8)((u32)PinNumber %
  724. (XGpioPsPinTable[*BankNumber - (u8)XGPIOPS_TWO] + (u32)1));
  725. }
  726. else
  727. {
  728. *PinNumberInBank = (u8)((u32)PinNumber %
  729. (XGpioPsPinTable[*BankNumber - (u8)1] + (u32)1));
  730. }
  731. }
  732. }
  733. #endif
  734. else {
  735. *PinNumberInBank = (u8)((u32)PinNumber %
  736. (XGpioPsPinTable[*BankNumber - (u8)1] + (u32)1));
  737. }
  738. }
  739. /** @} */