1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017, 2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_gint.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.gint"
14 #endif
15 
16 /*******************************************************************************
17  * Variables
18  ******************************************************************************/
19 /*! @brief Pointers to GINT bases for each instance. */
20 static GINT_Type *const s_gintBases[FSL_FEATURE_SOC_GINT_COUNT] = GINT_BASE_PTRS;
21 
22 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
23 /*! @brief Clocks for each instance. */
24 static const clock_ip_name_t s_gintClocks[FSL_FEATURE_SOC_GINT_COUNT] = GINT_CLOCKS;
25 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
26 
27 #if !(defined(FSL_SDK_DISABLE_DRIVER_RESET_CONTROL) && FSL_SDK_DISABLE_DRIVER_RESET_CONTROL)
28 /*! @brief Resets for each instance. */
29 static const reset_ip_name_t s_gintResets[FSL_FEATURE_SOC_GINT_COUNT] = GINT_RSTS;
30 #endif /* FSL_SDK_DISABLE_DRIVER_RESET_CONTROL */
31 
32 /* @brief Irq number for each instance */
33 static const IRQn_Type s_gintIRQ[FSL_FEATURE_SOC_GINT_COUNT] = GINT_IRQS;
34 
35 /*! @brief Callback function array for GINT(s). */
36 static gint_cb_t s_gintCallback[FSL_FEATURE_SOC_GINT_COUNT];
37 
38 /*******************************************************************************
39  * Code
40  ******************************************************************************/
41 
GINT_GetInstance(GINT_Type * base)42 static uint32_t GINT_GetInstance(GINT_Type *base)
43 {
44     uint32_t instance;
45 
46     /* Find the instance index from base address mappings. */
47     for (instance = 0; instance < ARRAY_SIZE(s_gintBases); instance++)
48     {
49         if (s_gintBases[instance] == base)
50         {
51             break;
52         }
53     }
54 
55     assert(instance < ARRAY_SIZE(s_gintBases));
56 
57     return instance;
58 }
59 
60 /*!
61  * brief	Initialize GINT peripheral.
62 
63  * This function initializes the GINT peripheral and enables the clock.
64  *
65  * param base Base address of the GINT peripheral.
66  *
67  * retval None.
68  */
GINT_Init(GINT_Type * base)69 void GINT_Init(GINT_Type *base)
70 {
71     uint32_t instance;
72 
73     instance = GINT_GetInstance(base);
74 
75     s_gintCallback[instance] = NULL;
76 
77 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
78     /* Enable the peripheral clock */
79     CLOCK_EnableClock(s_gintClocks[instance]);
80 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
81 
82 #if !(defined(FSL_SDK_DISABLE_DRIVER_RESET_CONTROL) && FSL_SDK_DISABLE_DRIVER_RESET_CONTROL)
83     /* Reset the module. */
84     RESET_PeripheralReset(s_gintResets[instance]);
85 #endif /* FSL_SDK_DISABLE_DRIVER_RESET_CONTROL */
86 }
87 
88 /*!
89  * brief	Setup GINT peripheral control parameters.
90 
91  * This function sets the control parameters of GINT peripheral.
92  *
93  * param base Base address of the GINT peripheral.
94  * param comb Controls if the enabled inputs are logically ORed or ANDed for interrupt generation.
95  * param trig Controls if the enabled inputs are level or edge sensitive based on polarity.
96  * param callback This function is called when configured group interrupt is generated.
97  *
98  * retval None.
99  */
GINT_SetCtrl(GINT_Type * base,gint_comb_t comb,gint_trig_t trig,gint_cb_t callback)100 void GINT_SetCtrl(GINT_Type *base, gint_comb_t comb, gint_trig_t trig, gint_cb_t callback)
101 {
102     uint32_t instance;
103 
104     instance = GINT_GetInstance(base);
105 
106     base->CTRL = (GINT_CTRL_COMB(comb) | GINT_CTRL_TRIG(trig));
107 
108     /* Save callback pointer */
109     s_gintCallback[instance] = callback;
110 }
111 
112 /*!
113  * brief	Get GINT peripheral control parameters.
114 
115  * This function returns the control parameters of GINT peripheral.
116  *
117  * param base Base address of the GINT peripheral.
118  * param comb Pointer to store combine input value.
119  * param trig Pointer to store trigger value.
120  * param callback Pointer to store callback function.
121  *
122  * retval None.
123  */
GINT_GetCtrl(GINT_Type * base,gint_comb_t * comb,gint_trig_t * trig,gint_cb_t * callback)124 void GINT_GetCtrl(GINT_Type *base, gint_comb_t *comb, gint_trig_t *trig, gint_cb_t *callback)
125 {
126     uint32_t instance;
127     uint32_t combValue;
128     uint32_t trigValue;
129 
130     instance = GINT_GetInstance(base);
131 
132     combValue = (base->CTRL & GINT_CTRL_COMB_MASK) >> GINT_CTRL_COMB_SHIFT;
133     *comb     = (gint_comb_t)combValue;
134     trigValue = (base->CTRL & GINT_CTRL_TRIG_MASK) >> GINT_CTRL_TRIG_SHIFT;
135     *trig     = (gint_trig_t)trigValue;
136     *callback = s_gintCallback[instance];
137 }
138 
139 /*!
140  * brief	Configure GINT peripheral pins.
141 
142  * This function enables and controls the polarity of enabled pin(s) of a given port.
143  *
144  * param base Base address of the GINT peripheral.
145  * param port Port number.
146  * param polarityMask Each bit position selects the polarity of the corresponding enabled pin.
147  *        0 = The pin is active LOW. 1 = The pin is active HIGH.
148  * param enableMask Each bit position selects if the corresponding pin is enabled or not.
149  *        0 = The pin is disabled. 1 = The pin is enabled.
150  *
151  * retval None.
152  */
GINT_ConfigPins(GINT_Type * base,gint_port_t port,uint32_t polarityMask,uint32_t enableMask)153 void GINT_ConfigPins(GINT_Type *base, gint_port_t port, uint32_t polarityMask, uint32_t enableMask)
154 {
155     base->PORT_POL[port] = polarityMask;
156     base->PORT_ENA[port] = enableMask;
157 }
158 
159 /*!
160  * brief	Get GINT peripheral pin configuration.
161 
162  * This function returns the pin configuration of a given port.
163  *
164  * param base Base address of the GINT peripheral.
165  * param port Port number.
166  * param polarityMask Pointer to store the polarity mask Each bit position indicates the polarity of the corresponding
167  enabled pin.
168  *        0 = The pin is active LOW. 1 = The pin is active HIGH.
169  * param enableMask Pointer to store the enable mask. Each bit position indicates if the corresponding pin is enabled
170  or not.
171  *        0 = The pin is disabled. 1 = The pin is enabled.
172  *
173  * retval None.
174  */
GINT_GetConfigPins(GINT_Type * base,gint_port_t port,uint32_t * polarityMask,uint32_t * enableMask)175 void GINT_GetConfigPins(GINT_Type *base, gint_port_t port, uint32_t *polarityMask, uint32_t *enableMask)
176 {
177     *polarityMask = base->PORT_POL[port];
178     *enableMask   = base->PORT_ENA[port];
179 }
180 
181 /*!
182  * brief	Enable callback.
183 
184  * This function enables the interrupt for the selected GINT peripheral. Although the pin(s) are monitored
185  * as soon as they are enabled, the callback function is not enabled until this function is called.
186  *
187  * param base Base address of the GINT peripheral.
188  *
189  * retval None.
190  */
GINT_EnableCallback(GINT_Type * base)191 void GINT_EnableCallback(GINT_Type *base)
192 {
193     uint32_t instance;
194 
195     instance = GINT_GetInstance(base);
196     /* If GINT is configured in "AND" mode a spurious interrupt is generated.
197        Clear status and pending interrupt before enabling the irq in NVIC. */
198     GINT_ClrStatus(base);
199     NVIC_ClearPendingIRQ(s_gintIRQ[instance]);
200     (void)EnableIRQ(s_gintIRQ[instance]);
201 }
202 
203 /*!
204  * brief	Disable callback.
205 
206  * This function disables the interrupt for the selected GINT peripheral. Although the pins are still
207  * being monitored but the callback function is not called.
208  *
209  * param base Base address of the peripheral.
210  *
211  * retval None.
212  */
GINT_DisableCallback(GINT_Type * base)213 void GINT_DisableCallback(GINT_Type *base)
214 {
215     uint32_t instance;
216 
217     instance = GINT_GetInstance(base);
218     (void)DisableIRQ(s_gintIRQ[instance]);
219     GINT_ClrStatus(base);
220     NVIC_ClearPendingIRQ(s_gintIRQ[instance]);
221 }
222 
223 /*!
224  * brief	Deinitialize GINT peripheral.
225 
226  * This function disables the GINT clock.
227  *
228  * param base Base address of the GINT peripheral.
229  *
230  * retval None.
231  */
GINT_Deinit(GINT_Type * base)232 void GINT_Deinit(GINT_Type *base)
233 {
234     uint32_t instance;
235 
236     instance = GINT_GetInstance(base);
237 
238     /* Cleanup */
239     GINT_DisableCallback(base);
240     s_gintCallback[instance] = NULL;
241 
242 #if !(defined(FSL_SDK_DISABLE_DRIVER_RESET_CONTROL) && FSL_SDK_DISABLE_DRIVER_RESET_CONTROL)
243     /* Reset the module. */
244     RESET_PeripheralReset(s_gintResets[instance]);
245 #endif /* FSL_SDK_DISABLE_DRIVER_RESET_CONTROL */
246 
247 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
248     /* Disable the peripheral clock */
249     CLOCK_DisableClock(s_gintClocks[instance]);
250 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
251 }
252 
253 /* IRQ handler functions overloading weak symbols in the startup */
254 #if defined(GINT0)
255 void GINT0_DriverIRQHandler(void);
GINT0_DriverIRQHandler(void)256 void GINT0_DriverIRQHandler(void)
257 {
258     /* Clear interrupt before callback */
259     s_gintBases[0]->CTRL |= GINT_CTRL_INT_MASK;
260     /* Call user function */
261     if (s_gintCallback[0] != NULL)
262     {
263         s_gintCallback[0]();
264     }
265     SDK_ISR_EXIT_BARRIER;
266 }
267 #endif
268 
269 #if defined(GINT1)
270 void GINT1_DriverIRQHandler(void);
GINT1_DriverIRQHandler(void)271 void GINT1_DriverIRQHandler(void)
272 {
273     /* Clear interrupt before callback */
274     s_gintBases[1]->CTRL |= GINT_CTRL_INT_MASK;
275     /* Call user function */
276     if (s_gintCallback[1] != NULL)
277     {
278         s_gintCallback[1]();
279     }
280     SDK_ISR_EXIT_BARRIER;
281 }
282 #endif
283 
284 #if defined(GINT2)
285 void GINT2_DriverIRQHandler(void);
GINT2_DriverIRQHandler(void)286 void GINT2_DriverIRQHandler(void)
287 {
288     /* Clear interrupt before callback */
289     s_gintBases[2]->CTRL |= GINT_CTRL_INT_MASK;
290     /* Call user function */
291     if (s_gintCallback[2] != NULL)
292     {
293         s_gintCallback[2]();
294     }
295     SDK_ISR_EXIT_BARRIER;
296 }
297 #endif
298 
299 #if defined(GINT3)
300 void GINT3_DriverIRQHandler(void);
GINT3_DriverIRQHandler(void)301 void GINT3_DriverIRQHandler(void)
302 {
303     /* Clear interrupt before callback */
304     s_gintBases[3]->CTRL |= GINT_CTRL_INT_MASK;
305     /* Call user function */
306     if (s_gintCallback[3] != NULL)
307     {
308         s_gintCallback[3]();
309     }
310     SDK_ISR_EXIT_BARRIER;
311 }
312 #endif
313 
314 #if defined(GINT4)
315 void GINT4_DriverIRQHandler(void);
GINT4_DriverIRQHandler(void)316 void GINT4_DriverIRQHandler(void)
317 {
318     /* Clear interrupt before callback */
319     s_gintBases[4]->CTRL |= GINT_CTRL_INT_MASK;
320     /* Call user function */
321     if (s_gintCallback[4] != NULL)
322     {
323         s_gintCallback[4]();
324     }
325     SDK_ISR_EXIT_BARRIER;
326 }
327 #endif
328 
329 #if defined(GINT5)
330 void GINT5_DriverIRQHandler(void);
GINT5_DriverIRQHandler(void)331 void GINT5_DriverIRQHandler(void)
332 {
333     /* Clear interrupt before callback */
334     s_gintBases[5]->CTRL |= GINT_CTRL_INT_MASK;
335     /* Call user function */
336     if (s_gintCallback[5] != NULL)
337     {
338         s_gintCallback[5]();
339     }
340     SDK_ISR_EXIT_BARRIER;
341 }
342 #endif
343 
344 #if defined(GINT6)
345 void GINT6_DriverIRQHandler(void);
GINT6_DriverIRQHandler(void)346 void GINT6_DriverIRQHandler(void)
347 {
348     /* Clear interrupt before callback */
349     s_gintBases[6]->CTRL |= GINT_CTRL_INT_MASK;
350     /* Call user function */
351     if (s_gintCallback[6] != NULL)
352     {
353         s_gintCallback[6]();
354     }
355     SDK_ISR_EXIT_BARRIER;
356 }
357 #endif
358 
359 #if defined(GINT7)
360 void GINT7_DriverIRQHandler(void);
GINT7_DriverIRQHandler(void)361 void GINT7_DriverIRQHandler(void)
362 {
363     /* Clear interrupt before callback */
364     s_gintBases[7]->CTRL |= GINT_CTRL_INT_MASK;
365     /* Call user function */
366     if (s_gintCallback[7] != NULL)
367     {
368         s_gintCallback[7]();
369     }
370     SDK_ISR_EXIT_BARRIER;
371 }
372 #endif
373