1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
36 * @addtogroup scugic_v3_1
39 * This file contains low-level driver functions that can be used to access the
40 * device. The user should refer to the hardware device specification for more
41 * details of the device operation.
42 * These routines are used when the user does not want to create an instance of
43 * XScuGic structure but still wants to use the ScuGic device. Hence the
44 * routines provided here take device id or scugic base address as arguments.
45 * Separate static versions of DistInit and CPUInit are provided to implement
46 * the low level driver routines.
49 * MODIFICATION HISTORY:
51 * Ver Who Date Changes
52 * ----- ---- -------- -------------------------------------------------------
53 * 1.01a sdm 07/18/11 First release
54 * 1.03a srt 02/27/13 Moved Offset calculation macros from *_hw.c (CR
56 * Added support to direct interrupts to the appropriate CPU.
57 * Earlier interrupts were directed to CPU1 (hard coded). Now
58 * depending upon the CPU selected by the user (xparameters.h),
59 * interrupts will be directed to the relevant CPU.
60 * This fixes CR 699688.
61 * 1.04a hk 05/04/13 Fix for CR#705621. Moved functions
62 * XScuGic_SetPriTrigTypeByDistAddr and
63 * XScuGic_GetPriTrigTypeByDistAddr here from xscugic.c
64 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
68 ******************************************************************************/
71 /***************************** Include Files *********************************/
73 #include "xil_types.h"
74 #include "xil_assert.h"
76 #include "xparameters.h"
78 /************************** Constant Definitions *****************************/
80 /**************************** Type Definitions *******************************/
82 /***************** Macros (Inline Functions) Definitions *********************/
84 /************************** Function Prototypes ******************************/
86 static void DistInit(XScuGic_Config *Config, u32 CpuID);
87 static void CPUInit(XScuGic_Config *Config);
88 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress);
90 /************************** Variable Definitions *****************************/
92 extern XScuGic_Config XScuGic_ConfigTable[XPAR_XSCUGIC_NUM_INSTANCES];
94 /*****************************************************************************/
97 * DistInit initializes the distributor of the GIC. The
98 * initialization entails:
100 * - Write the trigger mode, priority and target CPU
101 * - All interrupt sources are disabled
102 * - Enable the distributor
104 * @param InstancePtr is a pointer to the XScuGic instance.
105 * @param CpuID is the Cpu ID to be initialized.
111 ******************************************************************************/
112 static void DistInit(XScuGic_Config *Config, u32 CpuID)
115 u32 LocalCpuID = CpuID;
118 #warning "Building GIC for AMP"
121 * The distrubutor should not be initialized by FreeRTOS in the case of
122 * AMP -- it is assumed that Linux is the master of this device in that
128 XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET, 0U);
131 * Set the security domains in the int_security registers for non-secure
132 * interrupts. All are secure, so leave at the default. Set to 1 for
133 * non-secure interrupts.
138 * For the Shared Peripheral Interrupts INT_ID[MAX..32], set:
142 * 1. The trigger mode in the int_config register
143 * Only write to the SPI interrupts, so start at 32
145 for (Int_Id = 32U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+16U) {
147 * Each INT_ID uses two bits, or 16 INT_ID per register
148 * Set them all to be level sensitive, active HIGH.
150 XScuGic_WriteReg(Config->DistBaseAddress,
151 XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id), 0U);
155 #define DEFAULT_PRIORITY 0xa0a0a0a0U
156 for (Int_Id = 0U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+4U) {
158 * 2. The priority using int the priority_level register
159 * The priority_level and spi_target registers use one byte per
161 * Write a default value that can be changed elsewhere.
163 XScuGic_WriteReg(Config->DistBaseAddress,
164 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
168 for (Int_Id = 32U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+4U) {
170 * 3. The CPU interface in the spi_target register
171 * Only write to the SPI interrupts, so start at 32
173 LocalCpuID |= LocalCpuID << 8U;
174 LocalCpuID |= LocalCpuID << 16U;
176 XScuGic_WriteReg(Config->DistBaseAddress,
177 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id), LocalCpuID);
180 for (Int_Id = 0U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+32U) {
182 * 4. Enable the SPI using the enable_set register. Leave all disabled
185 XScuGic_WriteReg(Config->DistBaseAddress,
186 XSCUGIC_EN_DIS_OFFSET_CALC(XSCUGIC_DISABLE_OFFSET,
192 XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET,
193 XSCUGIC_EN_INT_MASK);
197 /*****************************************************************************/
200 * CPUInit initializes the CPU Interface of the GIC. The initialization entails:
202 * - Set the priority of the CPU.
203 * - Enable the CPU interface
205 * @param ConfigPtr is a pointer to a config table for the particular
206 * device this driver is associated with.
212 ******************************************************************************/
213 static void CPUInit(XScuGic_Config *Config)
216 * Program the priority mask of the CPU using the Priority mask
219 XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CPU_PRIOR_OFFSET,
223 * If the CPU operates in both security domains, set parameters in the
224 * control_s register.
225 * 1. Set FIQen=1 to use FIQ for secure interrupts,
226 * 2. Program the AckCtl bit
227 * 3. Program the SBPR bit to select the binary pointer behavior
228 * 4. Set EnableS = 1 to enable secure interrupts
229 * 5. Set EnbleNS = 1 to enable non secure interrupts
233 * If the CPU operates only in the secure domain, setup the
234 * control_s register.
236 * 2. Set EnableS=1, to enable the CPU interface to signal secure .
237 * interrupts Only enable the IRQ output unless secure interrupts
240 XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CONTROL_OFFSET, 0x07U);
244 /*****************************************************************************/
247 * CfgInitialize a specific interrupt controller instance/driver. The
248 * initialization entails:
250 * - Initialize fields of the XScuGic structure
251 * - Initial vector table with stub function calls
252 * - All interrupt sources are disabled
254 * @param InstancePtr is a pointer to the XScuGic instance to be worked on.
255 * @param ConfigPtr is a pointer to a config table for the particular device
256 * this driver is associated with.
257 * @param EffectiveAddr is the device base address in the virtual memory address
258 * space. The caller is responsible for keeping the address mapping
259 * from EffectiveAddr to the device physical base address unchanged
260 * once this function is invoked. Unexpected errors may occur if the
261 * address mapping changes after this function is called. If address
262 * translation is not used, use Config->BaseAddress for this parameters,
263 * passing the physical address instead.
267 * - XST_SUCCESS if initialization was successful
273 ******************************************************************************/
274 s32 XScuGic_DeviceInitialize(u32 DeviceId)
276 XScuGic_Config *Config;
277 u32 Cpu_Id = (u32)XPAR_CPU_ID + (u32)1;
279 Config = &XScuGic_ConfigTable[(u32 )DeviceId];
281 DistInit(Config, Cpu_Id);
288 /*****************************************************************************/
290 * This function is the primary interrupt handler for the driver. It must be
291 * connected to the interrupt source such that it is called when an interrupt of
292 * the interrupt controller is active. It will resolve which interrupts are
293 * active and enabled and call the appropriate interrupt handler. It uses
294 * the Interrupt Type information to determine when to acknowledge the
295 * interrupt.Highest priority interrupts are serviced first.
297 * This function assumes that an interrupt vector table has been previously
298 * initialized. It does not verify that entries in the table are valid before
299 * calling an interrupt handler.
301 * @param DeviceId is the unique identifier for the ScuGic device.
307 ******************************************************************************/
308 void XScuGic_DeviceInterruptHandler(void *DeviceId)
313 XScuGic_VectorTableEntry *TablePtr;
314 XScuGic_Config *CfgPtr;
316 CfgPtr = &XScuGic_ConfigTable[(INTPTR )DeviceId];
319 * Read the int_ack register to identify the highest priority
320 * interrupt ID and make sure it is valid. Reading Int_Ack will
321 * clear the interrupt in the GIC.
323 IntIDFull = XScuGic_ReadReg(CfgPtr->CpuBaseAddress, XSCUGIC_INT_ACK_OFFSET);
324 InterruptID = IntIDFull & XSCUGIC_ACK_INTID_MASK;
325 if(XSCUGIC_MAX_NUM_INTR_INPUTS < InterruptID){
330 * If the interrupt is shared, do some locking here if there are
331 * multiple processors.
334 * If pre-eption is required:
335 * Re-enable pre-emption by setting the CPSR I bit for non-secure ,
336 * interrupts or the F bit for secure interrupts
340 * If we need to change security domains, issue a SMC instruction here.
344 * Execute the ISR. Jump into the Interrupt service routine based on
345 * the IRQSource. A software trigger is cleared by the ACK.
347 TablePtr = &(CfgPtr->HandlerTable[InterruptID]);
348 if(TablePtr != NULL) {
349 TablePtr->Handler(TablePtr->CallBackRef);
354 * Write to the EOI register, we are all done here.
355 * Let this function return, the boot code will restore the stack.
357 XScuGic_WriteReg(CfgPtr->CpuBaseAddress, XSCUGIC_EOI_OFFSET, IntIDFull);
360 * Return from the interrupt. Change security domains could happen
365 /*****************************************************************************/
368 * Register a handler function for a specific interrupt ID. The vector table
369 * of the interrupt controller is updated, overwriting any previous handler.
370 * The handler function will be called when an interrupt occurs for the given
373 * @param BaseAddress is the CPU Interface Register base address of the
374 * interrupt controller whose vector table will be modified.
375 * @param InterruptId is the interrupt ID to be associated with the input
377 * @param Handler is the function pointer that will be added to
378 * the vector table for the given interrupt ID.
379 * @param CallBackRef is the argument that will be passed to the new
380 * handler function when it is called. This is user-specific.
386 * Note that this function has no effect if the input base address is invalid.
388 ******************************************************************************/
389 void XScuGic_RegisterHandler(u32 BaseAddress, s32 InterruptID,
390 Xil_InterruptHandler IntrHandler, void *CallBackRef)
392 XScuGic_Config *CfgPtr;
393 CfgPtr = LookupConfigByBaseAddress(BaseAddress);
396 if( IntrHandler != NULL) {
397 CfgPtr->HandlerTable[InterruptID].Handler = IntrHandler;
399 if( CallBackRef != NULL) {
400 CfgPtr->HandlerTable[InterruptID].CallBackRef = CallBackRef;
405 /*****************************************************************************/
408 * Looks up the device configuration based on the CPU interface base address of
409 * the device. A table contains the configuration info for each device in the
412 * @param CpuBaseAddress is the CPU Interface Register base address.
414 * @return A pointer to the configuration structure for the specified
415 * device, or NULL if the device was not found.
419 ******************************************************************************/
420 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress)
422 XScuGic_Config *CfgPtr = NULL;
425 for (Index = 0U; Index < XPAR_SCUGIC_NUM_INSTANCES; Index++) {
426 if (XScuGic_ConfigTable[Index].CpuBaseAddress ==
428 CfgPtr = &XScuGic_ConfigTable[Index];
433 return (XScuGic_Config *)CfgPtr;
436 /****************************************************************************/
438 * Sets the interrupt priority and trigger type for the specificd IRQ source.
440 * @param BaseAddr is the device base address
441 * @param Int_Id is the IRQ source number to modify
442 * @param Priority is the new priority for the IRQ source. 0 is highest
443 * priority, 0xF8 (248) is lowest. There are 32 priority levels
444 * supported with a step of 8. Hence the supported priorities are
445 * 0, 8, 16, 32, 40 ..., 248.
446 * @param Trigger is the new trigger type for the IRQ source.
447 * Each bit pair describes the configuration for an INT_ID.
448 * SFI Read Only b10 always
449 * PPI Read Only depending on how the PPIs are configured.
450 * b01 Active HIGH level sensitive
451 * b11 Rising edge sensitive
452 * SPI LSB is read only.
453 * b01 Active HIGH level sensitive
454 * b11 Rising edge sensitive/
458 * @note This API has the similar functionality of XScuGic_SetPriority
459 * TriggerType() and should be used when there is no InstancePtr.
461 *****************************************************************************/
462 void XScuGic_SetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
463 u8 Priority, u8 Trigger)
466 u8 LocalPriority = Priority;
468 Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
469 Xil_AssertVoid(Trigger <= XSCUGIC_INT_CFG_MASK);
470 Xil_AssertVoid(LocalPriority <= XSCUGIC_MAX_INTR_PRIO_VAL);
473 * Determine the register to write to using the Int_Id.
475 RegValue = XScuGic_ReadReg(DistBaseAddress,
476 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
479 * The priority bits are Bits 7 to 3 in GIC Priority Register. This
480 * means the number of priority levels supported are 32 and they are
481 * in steps of 8. The priorities can be 0, 8, 16, 32, 48, ... etc.
482 * The lower order 3 bits are masked before putting it in the register.
484 LocalPriority = LocalPriority & XSCUGIC_INTR_PRIO_MASK;
486 * Shift and Mask the correct bits for the priority and trigger in the
489 RegValue &= ~(XSCUGIC_PRIORITY_MASK << ((Int_Id%4U)*8U));
490 RegValue |= (u32)LocalPriority << ((Int_Id%4U)*8U);
493 * Write the value back to the register.
495 XScuGic_WriteReg(DistBaseAddress, XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
498 * Determine the register to write to using the Int_Id.
500 RegValue = XScuGic_ReadReg(DistBaseAddress,
501 XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
504 * Shift and Mask the correct bits for the priority and trigger in the
507 RegValue &= ~(XSCUGIC_INT_CFG_MASK << ((Int_Id%16U)*2U));
508 RegValue |= (u32)Trigger << ((Int_Id%16U)*2U);
511 * Write the value back to the register.
513 XScuGic_WriteReg(DistBaseAddress, XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id),
517 /****************************************************************************/
519 * Gets the interrupt priority and trigger type for the specificd IRQ source.
521 * @param BaseAddr is the device base address
522 * @param Int_Id is the IRQ source number to modify
523 * @param Priority is a pointer to the value of the priority of the IRQ
524 * source. This is a return value.
525 * @param Trigger is pointer to the value of the trigger of the IRQ
526 * source. This is a return value.
530 * @note This API has the similar functionality of XScuGic_GetPriority
531 * TriggerType() and should be used when there is no InstancePtr.
533 *****************************************************************************/
534 void XScuGic_GetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
535 u8 *Priority, u8 *Trigger)
539 Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
540 Xil_AssertVoid(Priority != NULL);
541 Xil_AssertVoid(Trigger != NULL);
544 * Determine the register to read to using the Int_Id.
546 RegValue = XScuGic_ReadReg(DistBaseAddress,
547 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
550 * Shift and Mask the correct bits for the priority and trigger in the
553 RegValue = RegValue >> ((Int_Id%4U)*8U);
554 *Priority = (u8)(RegValue & XSCUGIC_PRIORITY_MASK);
557 * Determine the register to read to using the Int_Id.
559 RegValue = XScuGic_ReadReg(DistBaseAddress,
560 XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
563 * Shift and Mask the correct bits for the priority and trigger in the
566 RegValue = RegValue >> ((Int_Id%16U)*2U);
568 *Trigger = (u8)(RegValue & XSCUGIC_INT_CFG_MASK);