1 /*
2 * Copyright (c) 2016-2017, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * ======== HwiP_tirtos.c ========
34 */
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include <stdlib.h>
40 #include <ti/osal/src/nonos/Nonos_config.h>
41 #include <ti/csl/tistdtypes.h>
43 /* Local structure definition */
44 typedef struct HwiP_nonOs_s {
45 bool used;
46 Hwi_Struct hwi;
47 } HwiP_nonOs;
49 /* Local hwi structures */
50 static HwiP_nonOs hwiStructs[OSAL_NONOS_CONFIGNUM_HWI] = {{0}};
54 /*
55 * Dummy function to check size during compile time
56 * ======== HwiP_compileTime_SizeChk ========
57 */
59 void OsalArch_compileTime_SizeChk(void)
60 {
61 #if defined(__GNUC__) && !defined(__ti__)
62 #pragma GCC diagnostic push
63 #pragma GCC diagnostic ignored "-Wunused-variable"
64 #else
65 /* TI compiler */
66 #pragma diag_suppress 179
67 #endif
68 OSAL_COMPILE_TIME_SIZE_CHECK (sizeof(HwiP_nonOs),OSAL_NONOS_HWIP_SIZE_BYTES);
69 #if defined(__GNUC__) && !defined(__ti__)
70 #pragma GCC diagnostic pop
71 #endif
72 }
74 /* This function enables the interrupt for a given interrupt number */
75 void OsalArch_enableInterrupt(uint32_t intNum)
76 {
77 Intc_SystemEnable((unsigned int)intNum);
78 return;
79 }
81 /* This function disables the interrupt for a given interrupt number */
82 void OsalArch_disableInterrupt(uint32_t intNum)
83 {
84 Intc_SystemDisable((uint32_t)intNum);
85 return;
86 }
88 /* Below function clears interrupt in the chip level */
89 void OsalArch_clearInterrupt(uint32_t intNum)
90 {
91 IntSystemStatusClear((uint32_t)intNum);
92 return;
93 }
94 /* Below function globally disable interrupt in the chip level */
95 uintptr_t OsalArch_globalDisableInterrupt(void)
96 {
97 uintptr_t status = Intc_IntDisable();
99 return (status);
100 }
102 /* Below function globally restore interrupt in the chip level */
103 void OsalArch_globalRestoreInterrupt (uintptr_t restoreValue)
104 {
105 Intc_IntEnable((uint32_t) restoreValue);
106 }
108 /* Below function registers the interrupt for a given ISR */
109 HwiP_Handle OsalArch_HwiPCreate(int32_t interruptNum, HwiP_Fxn hwiFxn,
110 const HwiP_Params *params)
111 {
112 uint32_t i;
113 uint32_t channelNo;
114 uintptr_t key;
116 uintptr_t temp;
117 HwiP_nonOs *hwiPool;
118 uint32_t maxHwi;
119 HwiP_Handle retHandle;
121 /* Check if user has specified any memory block to be used, which gets
122 * the precedence over the internal static memory block
123 */
124 if (gOsal_HwAttrs.extHwiPBlock.base != (uintptr_t)NULL_PTR)
125 {
126 /* pick up the external memory block configured */
127 hwiPool = (HwiP_nonOs *) gOsal_HwAttrs.extHwiPBlock.base;
128 temp = ((uintptr_t) hwiPool) + gOsal_HwAttrs.extHwiPBlock.size;
129 maxHwi = (uint32_t)(temp/(sizeof(Hwi_Struct)));
130 }
131 else
132 {
133 /* Pick up the internal static memory block */
134 hwiPool = (HwiP_nonOs *) &hwiStructs[0];
135 maxHwi = OSAL_NONOS_CONFIGNUM_HWI;
136 }
138 if (params == NULL_PTR)
139 {
140 return (NULL_PTR);
141 }
143 key = OsalArch_globalDisableInterrupt();
144 for (i = 0u; i < maxHwi; i++) {
145 if (hwiPool[i].used == FALSE) {
146 hwiPool[i].used = TRUE;
147 break;
148 }
149 }
150 OsalArch_globalRestoreInterrupt(key);
152 if (i != maxHwi)
153 {
154 retHandle = (HwiP_Handle)&hwiPool[i];
155 }
156 else
157 {
158 retHandle = (HwiP_Handle)(NULL_PTR);
159 }
161 if (retHandle != (HwiP_Handle) NULL_PTR)
162 {
163 Intc_Init();
165 /* Enable the interrupt */
166 OsalArch_globalRestoreInterrupt(0);
168 /* Registering the Interrupt Service Routine(ISR). */
169 Intc_IntRegister((uint32_t)interruptNum, (IntrFuncPtr) hwiFxn);
171 /*Set the channel number for a system interrupt. Channel numbers 0-1
172 are mapped to FIQ and Channel numbers 2-31 are mapped to IRQ of ARM.*/
173 channelNo = ((params->priority > 31) || (params->priority < 2)) ? 0x12U : params->priority;
174 IntChannelSet((uint32_t)interruptNum, channelNo);
176 /* Enabling the interrupt if configured */
177 if (params->enableIntr == TRUE)
178 {
179 /* Enabling the interrupt in INTC. */
180 OsalArch_enableInterrupt(interruptNum);
181 }
182 else
183 {
184 /* Disabling the interrupt in INTC. */
185 OsalArch_disableInterrupt(interruptNum);
186 }
188 /* Enable the Globle interrupt AINTC */
189 IntMasterIRQEnable();
190 IntGlobalEnable();
191 IntIRQEnable();
192 }
194 return ( (HwiP_Handle) (retHandle) ); /* __TI_ARM_V5__ */
196 }
198 /* Below function deletes the hwi created */
199 HwiP_Status OsalArch_HwiPDelete(HwiP_Handle handle)
200 {
201 HwiP_nonOs *hwi_hnd = (HwiP_nonOs*) handle;
202 uintptr_t key;
204 /* mark that index as free */
205 key = OsalArch_globalDisableInterrupt();
206 hwi_hnd->used = FALSE;
207 OsalArch_globalRestoreInterrupt(key);
209 return (HwiP_OK);
210 }
212 /* Nothing past this point */