1 /*
2  * Copyright 2022-2023 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "fsl_msgintr.h"
8 
9 /* Component ID definition, used by tools. */
10 #ifndef FSL_COMPONENT_ID
11 #define FSL_COMPONENT_ID "platform.drivers.msgintr"
12 #endif
13 
14 #ifndef MSGINTR_IRQS
15 #define MSGINTR_IRQS                                                                                            \
16     {                                                                                                           \
17         NotAvail_IRQn, MSGINTR1_IRQn, MSGINTR2_IRQn, MSGINTR3_IRQn, MSGINTR4_IRQn, MSGINTR5_IRQn, MSGINTR6_IRQn \
18     }
19 #endif
20 
21 /*! @brief Pointers to MSGINTR bases for each instance. */
22 static MSGINTR_Type *const s_msgintrBases[] = MSGINTR_BASE_PTRS;
23 
24 /*! @brief Pointers to MSGINTR IRQ number for each instance. */
25 static const IRQn_Type s_msgintrIrqId[] = MSGINTR_IRQS;
26 
27 /*! @brief Pointers to MSGINTR callbacks for each instance. */
28 static msgintr_cb_t s_msgintrCallback[ARRAY_SIZE(s_msgintrBases)] = {0};
29 
MSGINTR_GetInstance(MSGINTR_Type * base)30 static uint32_t MSGINTR_GetInstance(MSGINTR_Type *base)
31 {
32     uint32_t instance;
33 
34     /* Find the instance index from base address mappings. */
35     for (instance = 0; instance < ARRAY_SIZE(s_msgintrBases); instance++)
36     {
37         if (s_msgintrBases[instance] == base)
38         {
39             break;
40         }
41     }
42 
43     assert(instance < ARRAY_SIZE(s_msgintrBases));
44 
45     return instance;
46 }
47 
MSGINTR_Init(MSGINTR_Type * base,msgintr_cb_t callback)48 status_t MSGINTR_Init(MSGINTR_Type *base, msgintr_cb_t callback)
49 {
50     uint32_t instance = MSGINTR_GetInstance(base);
51 
52     (void)EnableIRQ(s_msgintrIrqId[instance]);
53     s_msgintrCallback[instance] = callback;
54 
55     return kStatus_Success;
56 }
57 
MSGINTR_Deinit(MSGINTR_Type * base)58 status_t MSGINTR_Deinit(MSGINTR_Type *base)
59 {
60     uint32_t instance = MSGINTR_GetInstance(base);
61 
62     (void)DisableIRQ(s_msgintrIrqId[instance]);
63     s_msgintrCallback[instance] = NULL;
64 
65     /* Read and clean the pending bits. */
66     for (uint8_t channel = 0; channel < FSL_MSGINTR_CHANNEL_NUM; channel++)
67     {
68         (void)base->MSI[channel].MSIR;
69     }
70 
71     return kStatus_Success;
72 }
73 
74 #if defined(MSGINTR1)
75 void MSGINTR1_IRQHandler(void);
MSGINTR1_IRQHandler(void)76 void MSGINTR1_IRQHandler(void)
77 {
78     uint32_t pendingIntr;
79 
80     for (uint8_t channel = 0; channel < FSL_MSGINTR_CHANNEL_NUM; channel++)
81     {
82         pendingIntr = MSGINTR1->MSI[channel].MSIR;
83         if ((pendingIntr != 0U) && (s_msgintrCallback[1] != NULL))
84         {
85             s_msgintrCallback[1](MSGINTR1, channel, pendingIntr);
86         }
87     }
88     SDK_ISR_EXIT_BARRIER;
89 }
90 #endif
91 
92 #if defined(MSGINTR2)
93 void MSGINTR2_IRQHandler(void);
MSGINTR2_IRQHandler(void)94 void MSGINTR2_IRQHandler(void)
95 {
96     uint32_t pendingIntr;
97 
98     for (uint8_t channel = 0; channel < FSL_MSGINTR_CHANNEL_NUM; channel++)
99     {
100         pendingIntr = MSGINTR2->MSI[channel].MSIR;
101         if ((pendingIntr != 0U) && (s_msgintrCallback[2] != NULL))
102         {
103             s_msgintrCallback[2](MSGINTR2, channel, pendingIntr);
104         }
105     }
106     SDK_ISR_EXIT_BARRIER;
107 }
108 #endif
109 
110 #if defined(MSGINTR3)
111 void MSGINTR3_IRQHandler(void);
MSGINTR3_IRQHandler(void)112 void MSGINTR3_IRQHandler(void)
113 {
114     uint32_t pendingIntr;
115 
116     for (uint8_t channel = 0; channel < FSL_MSGINTR_CHANNEL_NUM; channel++)
117     {
118         pendingIntr = MSGINTR2->MSI[channel].MSIR;
119         if ((pendingIntr != 0U) && (s_msgintrCallback[3] != NULL))
120         {
121             s_msgintrCallback[3](MSGINTR3, channel, pendingIntr);
122         }
123     }
124     SDK_ISR_EXIT_BARRIER;
125 }
126 #endif
127 
128 #if defined(MSGINTR4)
129 void MSGINTR4_IRQHandler(void);
MSGINTR4_IRQHandler(void)130 void MSGINTR4_IRQHandler(void)
131 {
132     uint32_t pendingIntr;
133 
134     for (uint8_t channel = 0; channel < FSL_MSGINTR_CHANNEL_NUM; channel++)
135     {
136         pendingIntr = MSGINTR2->MSI[channel].MSIR;
137         if ((pendingIntr != 0U) && (s_msgintrCallback[4] != NULL))
138         {
139             s_msgintrCallback[4](MSGINTR4, channel, pendingIntr);
140         }
141     }
142     SDK_ISR_EXIT_BARRIER;
143 }
144 #endif
145 
146 #if defined(MSGINTR5)
147 void MSGINTR5_IRQHandler(void);
MSGINTR5_IRQHandler(void)148 void MSGINTR5_IRQHandler(void)
149 {
150     uint32_t pendingIntr;
151 
152     for (uint8_t channel = 0; channel < FSL_MSGINTR_CHANNEL_NUM; channel++)
153     {
154         pendingIntr = MSGINTR2->MSI[channel].MSIR;
155         if ((pendingIntr != 0U) && (s_msgintrCallback[5] != NULL))
156         {
157             s_msgintrCallback[5](MSGINTR5, channel, pendingIntr);
158         }
159     }
160     SDK_ISR_EXIT_BARRIER;
161 }
162 #endif
163 
164 #if defined(MSGINTR6)
165 void MSGINTR6_IRQHandler(void);
MSGINTR6_IRQHandler(void)166 void MSGINTR6_IRQHandler(void)
167 {
168     uint32_t pendingIntr;
169 
170     for (uint8_t channel = 0; channel < FSL_MSGINTR_CHANNEL_NUM; channel++)
171     {
172         pendingIntr = MSGINTR2->MSI[channel].MSIR;
173         if ((pendingIntr != 0U) && (s_msgintrCallback[6] != NULL))
174         {
175             s_msgintrCallback[6](MSGINTR6, channel, pendingIntr);
176         }
177     }
178     SDK_ISR_EXIT_BARRIER;
179 }
180 #endif
181