1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <assert.h>
32 #include "rdc_semaphore.h"
33
34 /*******************************************************************************
35 * Code
36 ******************************************************************************/
37
38 /*******************************************************************************
39 * Private Functions
40 ******************************************************************************/
RDC_SEMAPHORE_GetGate(uint32_t * pdap)41 static RDC_SEMAPHORE_Type *RDC_SEMAPHORE_GetGate(uint32_t *pdap)
42 {
43 RDC_SEMAPHORE_Type *semaphore;
44
45 if (*pdap < 64)
46 semaphore = RDC_SEMAPHORE1;
47 else
48 {
49 semaphore = RDC_SEMAPHORE2;
50 *pdap -= 64;
51 }
52
53 return semaphore;
54 }
55
56 /*FUNCTION**********************************************************************
57 *
58 * Function Name : RDC_SEMAPHORE_TryLock
59 * Description : Lock RDC semaphore for shared peripheral access
60 *
61 *END**************************************************************************/
RDC_SEMAPHORE_TryLock(uint32_t pdap)62 rdc_semaphore_status_t RDC_SEMAPHORE_TryLock(uint32_t pdap)
63 {
64 RDC_SEMAPHORE_Type *semaphore;
65 uint32_t index = pdap;
66
67 semaphore = RDC_SEMAPHORE_GetGate(&index);
68
69 semaphore->GATE[index] = RDC_SEMAPHORE_GATE_GTFSM(RDC_SEMAPHORE_MASTER_SELF + 1);
70
71 return ((semaphore->GATE[index] & RDC_SEMAPHORE_GATE_GTFSM_MASK) ==
72 RDC_SEMAPHORE_GATE_GTFSM(RDC_SEMAPHORE_MASTER_SELF + 1)) ?
73 statusRdcSemaphoreSuccess : statusRdcSemaphoreBusy;
74 }
75
76 /*FUNCTION**********************************************************************
77 *
78 * Function Name : RDC_SEMAPHORE_Lock
79 * Description : Lock RDC semaphore for shared peripheral access, polling until
80 * success.
81 *
82 *END**************************************************************************/
RDC_SEMAPHORE_Lock(uint32_t pdap)83 void RDC_SEMAPHORE_Lock(uint32_t pdap)
84 {
85 RDC_SEMAPHORE_Type *semaphore;
86 uint32_t index = pdap;
87
88 semaphore = RDC_SEMAPHORE_GetGate(&index);
89
90 do {
91 /* Wait gate status free */
92 while (semaphore->GATE[index] & RDC_SEMAPHORE_GATE_GTFSM_MASK) { }
93 semaphore->GATE[index] = RDC_SEMAPHORE_GATE_GTFSM(RDC_SEMAPHORE_MASTER_SELF + 1);
94 } while ((semaphore->GATE[index] & RDC_SEMAPHORE_GATE_GTFSM_MASK) !=
95 RDC_SEMAPHORE_GATE_GTFSM(RDC_SEMAPHORE_MASTER_SELF + 1));
96 }
97
98 /*FUNCTION**********************************************************************
99 *
100 * Function Name : RDC_SEMAPHORE_Unlock
101 * Description : Unlock RDC semaphore
102 *
103 *END**************************************************************************/
RDC_SEMAPHORE_Unlock(uint32_t pdap)104 void RDC_SEMAPHORE_Unlock(uint32_t pdap)
105 {
106 RDC_SEMAPHORE_Type *semaphore;
107 uint32_t index = pdap;
108
109 semaphore = RDC_SEMAPHORE_GetGate(&index);
110
111 semaphore->GATE[index] = RDC_SEMAPHORE_GATE_GTFSM(0);
112 }
113
114 /*FUNCTION**********************************************************************
115 *
116 * Function Name : RDC_SEMAPHORE_GetLockDomainID
117 * Description : Get domain ID which locks the semaphore
118 *
119 *END**************************************************************************/
RDC_SEMAPHORE_GetLockDomainID(uint32_t pdap)120 uint32_t RDC_SEMAPHORE_GetLockDomainID(uint32_t pdap)
121 {
122 RDC_SEMAPHORE_Type *semaphore;
123 uint32_t index = pdap;
124
125 semaphore = RDC_SEMAPHORE_GetGate(&index);
126
127 return (semaphore->GATE[index] & RDC_SEMAPHORE_GATE_LDOM_MASK) >> RDC_SEMAPHORE_GATE_LDOM_SHIFT;
128 }
129
130 /*FUNCTION**********************************************************************
131 *
132 * Function Name : RDC_SEMAPHORE_GetLockMaster
133 * Description : Get master index which locks the semaphore
134 *
135 *END**************************************************************************/
RDC_SEMAPHORE_GetLockMaster(uint32_t pdap)136 uint32_t RDC_SEMAPHORE_GetLockMaster(uint32_t pdap)
137 {
138 RDC_SEMAPHORE_Type *semaphore;
139 uint32_t index = pdap;
140 uint8_t master;
141
142 semaphore = RDC_SEMAPHORE_GetGate(&index);
143
144 master = (semaphore->GATE[index] & RDC_SEMAPHORE_GATE_GTFSM_MASK) >> RDC_SEMAPHORE_GATE_GTFSM_SHIFT;
145
146 return master == 0 ? RDC_SEMAPHORE_MASTER_NONE : master - 1;
147 }
148
149 /*FUNCTION**********************************************************************
150 *
151 * Function Name : RDC_SEMAPHORE_Reset
152 * Description : Reset RDC semaphore to unlocked status
153 *
154 *END**************************************************************************/
RDC_SEMAPHORE_Reset(uint32_t pdap)155 void RDC_SEMAPHORE_Reset(uint32_t pdap)
156 {
157 RDC_SEMAPHORE_Type *semaphore;
158 uint32_t index = pdap;
159
160 semaphore = RDC_SEMAPHORE_GetGate(&index);
161
162 /* The reset state machine must be in idle state */
163 assert ((semaphore->RSTGT_R & RDC_SEMAPHORE_RSTGT_R_RSTGSM_MASK) == 0);
164
165 semaphore->RSTGT_W = 0xE2;
166 semaphore->RSTGT_W = 0x1D | RDC_SEMAPHORE_RSTGT_W_RSTGTN(index);
167 }
168
169 /*FUNCTION**********************************************************************
170 *
171 * Function Name : RDC_SEMAPHORE_ResetAll
172 * Description : Reset all RDC semaphores to unlocked status for certain
173 * RDC_SEMAPHORE instance
174 *
175 *END**************************************************************************/
RDC_SEMAPHORE_ResetAll(RDC_SEMAPHORE_Type * base)176 void RDC_SEMAPHORE_ResetAll(RDC_SEMAPHORE_Type *base)
177 {
178 /* The reset state machine must be in idle state */
179 assert ((base->RSTGT_R & RDC_SEMAPHORE_RSTGT_R_RSTGSM_MASK) == 0);
180
181 base->RSTGT_W = 0xE2;
182 base->RSTGT_W = 0x1D | RDC_SEMAPHORE_RSTGT_W_RSTGTN_MASK;
183 }
184
185 /*******************************************************************************
186 * EOF
187 ******************************************************************************/
188