1 /******************************************************************************
3 * Copyright (C) 2017 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 ******************************************************************************/
33 /*****************************************************************************/
36 * @file xil_sleeptimer.c
38 * This file provides the common helper routines for the sleep API's
41 * MODIFICATION HISTORY :
43 * Ver Who Date Changes
44 * ----- ---- -------- -------------------------------------------------------
45 * 6.6 srm 10/18/17 First Release.
48 *****************************************************************************/
50 /**************************** Include Files ********************************/
53 #include "xil_sleeptimer.h"
56 /**************************** Constant Definitions *************************/
59 /* Function definitions are applicable only when TTC3 is present*/
60 #if defined (SLEEP_TIMER_BASEADDR)
61 /****************************************************************************/
64 * This is a helper function used by sleep/usleep APIs to
65 * have delay in sec/usec
67 * @param delay - delay time in seconds/micro seconds
69 * @param frequency - Number of counts per second/micro second
75 *****************************************************************************/
76 void Xil_SleepTTCCommon(u32 delay, u64 frequency)
80 XCntrVal TimeHighVal = 0U;
81 XCntrVal TimeLowVal1 = 0U;
82 XCntrVal TimeLowVal2 = 0U;
84 TimeLowVal1 = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
85 XSLEEP_TIMER_TTC_COUNT_VALUE_OFFSET);
86 tEnd = (INTPTR)TimeLowVal1 + ((INTPTR)(delay) * frequency);
89 TimeLowVal2 = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
90 XSLEEP_TIMER_TTC_COUNT_VALUE_OFFSET);
91 if (TimeLowVal2 < TimeLowVal1) {
94 TimeLowVal1 = TimeLowVal2;
95 tCur = (((INTPTR) TimeHighVal) << XSLEEP_TIMER_REG_SHIFT) |
101 /*****************************************************************************/
104 * This API starts the Triple Timer Counter
112 *****************************************************************************/
113 void XTime_StartTTCTimer()
118 #if (defined (__aarch64__) && EL3==1) || defined (ARMR5) || defined (ARMA53_32)
121 LpdRst = XSleep_ReadCounterVal(RST_LPD_IOU2);
123 /* check if the timer is reset */
124 if (((LpdRst & (RST_LPD_IOU2_TTC_BASE_RESET_MASK <<
125 XSLEEP_TTC_INSTANCE)) != 0 )) {
126 LpdRst = LpdRst & (~(RST_LPD_IOU2_TTC_BASE_RESET_MASK <<
127 XSLEEP_TTC_INSTANCE));
128 Xil_Out32(RST_LPD_IOU2, LpdRst);
131 TimerCntrl = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
132 XSLEEP_TIMER_TTC_CNT_CNTRL_OFFSET);
133 /* check if Timer is disabled */
134 if ((TimerCntrl & XSLEEP_TIMER_TTC_CNT_CNTRL_DIS_MASK) == 0) {
135 TimerPrescalar = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
136 XSLEEP_TIMER_TTC_CLK_CNTRL_OFFSET);
137 /* check if Timer is configured with proper functionalty for sleep */
138 if ((TimerPrescalar & XSLEEP_TIMER_TTC_CLK_CNTRL_PS_EN_MASK) == 0)
141 #if (defined (__aarch64__) && EL3==1) || defined (ARMR5) || defined (ARMA53_32)
144 /* Disable the timer to configure */
145 TimerCntrl = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
146 XSLEEP_TIMER_TTC_CNT_CNTRL_OFFSET);
147 TimerCntrl = TimerCntrl | XSLEEP_TIMER_TTC_CNT_CNTRL_DIS_MASK;
148 Xil_Out32(SLEEP_TIMER_BASEADDR + XSLEEP_TIMER_TTC_CNT_CNTRL_OFFSET,
150 /* Disable the prescalar */
151 TimerPrescalar = XSleep_ReadCounterVal(SLEEP_TIMER_BASEADDR +
152 XSLEEP_TIMER_TTC_CLK_CNTRL_OFFSET);
153 TimerPrescalar = TimerPrescalar & (~XSLEEP_TIMER_TTC_CLK_CNTRL_PS_EN_MASK);
154 Xil_Out32(SLEEP_TIMER_BASEADDR + XSLEEP_TIMER_TTC_CLK_CNTRL_OFFSET,
156 /* Enable the Timer */
157 TimerCntrl = TimerCntrl & (~XSLEEP_TIMER_TTC_CNT_CNTRL_DIS_MASK);
158 Xil_Out32(SLEEP_TIMER_BASEADDR + XSLEEP_TIMER_TTC_CNT_CNTRL_OFFSET,