1 /***************************************************************************//**
2 * \file cy_syspm_btss.c
3 * \version 5.150
4 *
5 * Provides implementation of the BTSS PDL driver.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright 2016-2021 Cypress Semiconductor Corporation
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 *     http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *******************************************************************************/
24 
25 #include "cy_device.h"
26 
27 #if defined (CY_IP_MXS40BLE52SS)
28 
29 #include "cy_syspm_btss.h"
30 
31 #define CY_BTSS_RADIO_XTAL_OSC_FREQ    (24000000UL) /* Hz */
32 
33 /* BTSS Lock counter */
34 static uint32_t cy_btss_lock_count;
35 
36 /* CPUSS Lock counter */
37 static uint32_t cy_cpuss_lock_count;
38 
39 
Cy_BTSS_GetXtalOscFreq(void)40 uint32_t Cy_BTSS_GetXtalOscFreq(void)
41 {
42     return CY_BTSS_RADIO_XTAL_OSC_FREQ;
43 }
44 
Cy_BTSS_PowerDep(bool enable)45 cy_en_btss_status_t Cy_BTSS_PowerDep(bool enable)
46 {
47     cy_en_btss_status_t retVal = CY_BTSS_INVALID_STATE;
48     uint32_t interruptState;
49     uint32_t timeoutus = CY_BTSS_SL_CTL3_WAIT_DELAY_US;
50 
51     if(enable)
52     {
53         interruptState = Cy_SysLib_EnterCriticalSection();
54         /* Set only if cy_btss_lock_count == 0 */
55         if(cy_btss_lock_count == 0UL)
56         {
57             (void)cy_pd_pdcm_set_dependency(CY_PD_PDCM_BTSS, CY_PD_PDCM_CPUSS); /* Suppress a compiler warning about unused return value */
58         }
59         cy_btss_lock_count++;
60 
61         Cy_SysLib_ExitCriticalSection(interruptState);
62 
63         for(;(Cy_SysClk_PeriGroupGetSlaveCtl(CY_PERI_BLESS_GROUP_NR,CY_SYSCLK_PERI_GROUP_SL_CTL3) == 0UL) &&
64             (0UL != timeoutus);
65             timeoutus--)
66         {
67             Cy_SysLib_DelayUs(1U);
68         }
69         if(0UL == timeoutus)
70         {
71             interruptState = Cy_SysLib_EnterCriticalSection();
72             cy_btss_lock_count--;
73             Cy_SysLib_ExitCriticalSection(interruptState);
74 
75             retVal = CY_BTSS_TIMEOUT;
76         }
77         else
78         {
79             retVal = CY_BTSS_SUCCESS;
80         }
81     }
82     else
83     {
84         /* Dont Decrement if already in Disable State */
85         interruptState = Cy_SysLib_EnterCriticalSection();
86         if(cy_btss_lock_count != 0UL)
87         {
88             cy_btss_lock_count--;
89         }
90 
91         /* Clear only if cy_btss_lock_count == 0 */
92         if(cy_btss_lock_count  == 0UL)
93         {
94             (void)cy_pd_pdcm_clear_dependency(CY_PD_PDCM_BTSS, CY_PD_PDCM_CPUSS); /* Suppress a compiler warning about unused return value */
95         }
96         Cy_SysLib_ExitCriticalSection(interruptState);
97 
98         retVal = CY_BTSS_SUCCESS;
99     }
100 
101     return retVal;
102 }
103 
Cy_BTSS_PowerDepResetCount(void)104 void Cy_BTSS_PowerDepResetCount(void)
105 {
106     uint32_t interruptState;
107 
108     interruptState = Cy_SysLib_EnterCriticalSection();
109     cy_btss_lock_count = 0UL;
110     Cy_SysLib_ExitCriticalSection(interruptState);
111 }
112 
113 
Cy_BTSS_CPUSSPowerDep(bool enable)114 cy_en_btss_status_t Cy_BTSS_CPUSSPowerDep(bool enable)
115 {
116     cy_en_btss_status_t retVal = CY_BTSS_INVALID_STATE;
117     uint32_t interruptState;
118 
119     interruptState = Cy_SysLib_EnterCriticalSection();
120 
121     if(enable)
122     {
123         /* Set only if cy_btss_lock_count == 0 */
124         if(cy_cpuss_lock_count == 0UL)
125         {
126             (void)cy_pd_pdcm_set_dependency(CY_PD_PDCM_CPUSS, CY_PD_PDCM_BTSS); /* Suppress a compiler warning about unused return value */
127         }
128 
129         cy_cpuss_lock_count++;
130 
131         retVal = CY_BTSS_SUCCESS;
132     }
133     else
134     {
135         /* Dont Decrement if already in Disable State */
136         if(cy_cpuss_lock_count != 0UL)
137         {
138             cy_cpuss_lock_count--;
139         }
140 
141         /* Clear only if cy_btss_lock_count == 0 */
142         if(cy_cpuss_lock_count  == 0UL)
143         {
144             (void)cy_pd_pdcm_clear_dependency(CY_PD_PDCM_CPUSS, CY_PD_PDCM_BTSS); /* Suppress a compiler warning about unused return value */
145         }
146 
147         retVal = CY_BTSS_SUCCESS;
148     }
149 
150     Cy_SysLib_ExitCriticalSection(interruptState);
151 
152     return retVal;
153 }
154 
155 
Cy_BTSS_AssertReset(bool assertEn)156 void Cy_BTSS_AssertReset(bool assertEn)
157 {
158     (void)Cy_SysClk_PeriGroupSetSlaveCtl(CY_PERI_BLESS_GROUP_NR, CY_SYSCLK_PERI_GROUP_SL_CTL2, (assertEn ? 0x1U : 0x0U));
159 }
160 
Cy_BTSS_IsResetAsserted(void)161 bool Cy_BTSS_IsResetAsserted(void)
162 {
163     return (Cy_SysClk_PeriGroupGetSlaveCtl(CY_PERI_BLESS_GROUP_NR, CY_SYSCLK_PERI_GROUP_SL_CTL2) != 0x0U);
164 }
165 
166 #endif /* CY_IP_MXS40BLE52SS */
167 /* [] END OF FILE */
168 
169