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 "sema4.h"
33 
34 /*******************************************************************************
35  * Code
36  ******************************************************************************/
37 
38 /*FUNCTION**********************************************************************
39  *
40  * Function Name : SEMA4_TryLock
41  * Description   : Lock SEMA4 gate for exclusive access between multicore
42  *
43  *END**************************************************************************/
SEMA4_TryLock(SEMA4_Type * base,uint32_t gateIndex)44 sema4_status_t SEMA4_TryLock(SEMA4_Type *base, uint32_t gateIndex)
45 {
46     __IO uint8_t *gate;
47 
48     assert(gateIndex < 16);
49 
50     gate = &base->GATE00 + gateIndex;
51 
52     *gate = SEMA4_GATE00_GTFSM(SEMA4_PROCESSOR_SELF + 1);
53 
54     return ((*gate & SEMA4_GATE00_GTFSM_MASK) == SEMA4_GATE00_GTFSM(SEMA4_PROCESSOR_SELF + 1)) ?
55            statusSema4Success : statusSema4Busy;
56 }
57 
58 /*FUNCTION**********************************************************************
59  *
60  * Function Name : SEMA4_Lock
61  * Description   : Lock SEMA4 gate for exclusive access between multicore,
62  *                 polling until success
63  *
64  *END**************************************************************************/
SEMA4_Lock(SEMA4_Type * base,uint32_t gateIndex)65 void SEMA4_Lock(SEMA4_Type *base, uint32_t gateIndex)
66 {
67     __IO uint8_t *gate;
68 
69     assert(gateIndex < 16);
70 
71     gate = &base->GATE00 + gateIndex;
72 
73     do {
74         /* Wait gate status free */
75         while (*gate & SEMA4_GATE00_GTFSM_MASK) { }
76         *gate = SEMA4_GATE00_GTFSM(SEMA4_PROCESSOR_SELF + 1);
77     } while ((*gate & SEMA4_GATE00_GTFSM_MASK) != SEMA4_GATE00_GTFSM(SEMA4_PROCESSOR_SELF + 1));
78 }
79 
80 /*FUNCTION**********************************************************************
81  *
82  * Function Name : SEMA4_Unlock
83  * Description   : Unlock SEMA4 gate
84  *
85  *END**************************************************************************/
SEMA4_Unlock(SEMA4_Type * base,uint32_t gateIndex)86 void SEMA4_Unlock(SEMA4_Type *base, uint32_t gateIndex)
87 {
88     __IO uint8_t *gate;
89 
90     assert(gateIndex < 16);
91 
92     gate = &base->GATE00 + gateIndex;
93 
94     *gate = SEMA4_GATE00_GTFSM(0);
95 }
96 
97 /*FUNCTION**********************************************************************
98  *
99  * Function Name : SEMA4_GetLockProcessor
100  * Description   : Get master index which locks the semaphore
101  *
102  *END**************************************************************************/
SEMA4_GetLockProcessor(SEMA4_Type * base,uint32_t gateIndex)103 uint32_t SEMA4_GetLockProcessor(SEMA4_Type *base, uint32_t gateIndex)
104 {
105     __IO uint8_t *gate;
106     uint8_t proc;
107 
108     assert(gateIndex < 16);
109 
110     gate = &base->GATE00 + gateIndex;
111 
112     proc = (*gate & SEMA4_GATE00_GTFSM_MASK) >> SEMA4_GATE00_GTFSM_SHIFT;
113 
114     return proc == 0 ? SEMA4_PROCESSOR_NONE : proc - 1;
115 }
116 
117 /*FUNCTION**********************************************************************
118  *
119  * Function Name : SEMA4_ResetGate
120  * Description   : Reset SEMA4 gate to unlocked status
121  *
122  *END**************************************************************************/
SEMA4_ResetGate(SEMA4_Type * base,uint32_t gateIndex)123 void SEMA4_ResetGate(SEMA4_Type *base, uint32_t gateIndex)
124 {
125     assert(gateIndex < 16);
126 
127     /* The reset state machine must be in idle state */
128     assert ((base->RSTGT & 0x30) == 0);
129 
130     base->RSTGT = 0xE2;
131     base->RSTGT = 0x1D | SEMA4_RSTGT_RSTGTN(gateIndex);
132 }
133 
134 /*FUNCTION**********************************************************************
135  *
136  * Function Name : SEMA4_ResetAllGates
137  * Description   : Reset all SEMA4 gates to unlocked status for certain
138  *                 SEMA4 instance
139  *
140  *END**************************************************************************/
SEMA4_ResetAllGates(SEMA4_Type * base)141 void SEMA4_ResetAllGates(SEMA4_Type *base)
142 {
143     /* The reset state machine must be in idle state */
144     assert ((base->RSTGT & 0x30) == 0);
145 
146     base->RSTGT = 0xE2;
147     base->RSTGT = 0x1D | SEMA4_RSTGT_RSTGTN_MASK;
148 }
149 
150 /*FUNCTION**********************************************************************
151  *
152  * Function Name : SEMA4_ResetNotification
153  * Description   : Reset SEMA4 IRQ notifications
154  *
155  *END**************************************************************************/
SEMA4_ResetNotification(SEMA4_Type * base,uint32_t gateIndex)156 void SEMA4_ResetNotification(SEMA4_Type *base, uint32_t gateIndex)
157 {
158     assert(gateIndex < 16);
159 
160     /* The reset state machine must be in idle state */
161     assert ((base->RSTNTF & 0x30) == 0);
162 
163     base->RSTNTF = 0x47;
164     base->RSTNTF = 0xB8 | SEMA4_RSTNTF_RSTNTN(gateIndex);
165 }
166 
167 /*FUNCTION**********************************************************************
168  *
169  * Function Name : SEMA4_ResetAllNotifications
170  * Description   : Reset all SEMA4 gates to unlocked status for certain
171  *                 SEMA4 instance
172  *
173  *END**************************************************************************/
SEMA4_ResetAllNotifications(SEMA4_Type * base)174 void SEMA4_ResetAllNotifications(SEMA4_Type *base)
175 {
176     /* The reset state machine must be in idle state */
177     assert ((base->RSTNTF & 0x30) == 0);
178 
179     base->RSTNTF = 0x47;
180     base->RSTNTF = 0xB8 | SEMA4_RSTNTF_RSTNTN_MASK;
181 }
182 
183 /*FUNCTION**********************************************************************
184  *
185  * Function Name : SEMA4_SetIntCmd
186  * Description   : Enable or disable SEMA4 IRQ notification.
187  *
188  *END**************************************************************************/
SEMA4_SetIntCmd(SEMA4_Type * base,uint16_t intMask,bool enable)189 void SEMA4_SetIntCmd(SEMA4_Type * base, uint16_t intMask, bool enable)
190 {
191     if (enable)
192         base->CPnINE[SEMA4_PROCESSOR_SELF].INE |= intMask;
193     else
194         base->CPnINE[SEMA4_PROCESSOR_SELF].INE &= ~intMask;
195 }
196 
197 /*******************************************************************************
198  * EOF
199  ******************************************************************************/
200