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 /*****************************************************************************/
37 * This file contains low-level driver functions that can be used to access the
38 * device. The user should refer to the hardware device specification for more
39 * details of the device operation.
40 * These routines are used when the user does not want to create an instance of
41 * XScuGic structure but still wants to use the ScuGic device. Hence the
42 * routines provided here take device id or scugic base address as arguments.
43 * Separate static versions of DistInit and CPUInit are provided to implement
44 * the low level driver routines.
47 * MODIFICATION HISTORY:
49 * Ver Who Date Changes
50 * ----- ---- -------- -------------------------------------------------------
51 * 1.01a sdm 07/18/11 First release
52 * 1.03a srt 02/27/13 Moved Offset calculation macros from *_hw.c (CR
54 * Added support to direct interrupts to the appropriate CPU.
55 * Earlier interrupts were directed to CPU1 (hard coded). Now
56 * depending upon the CPU selected by the user (xparameters.h),
57 * interrupts will be directed to the relevant CPU.
58 * This fixes CR 699688.
59 * 1.04a hk 05/04/13 Fix for CR#705621. Moved functions
60 * XScuGic_SetPriTrigTypeByDistAddr and
61 * XScuGic_GetPriTrigTypeByDistAddr here from xscugic.c
62 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
66 ******************************************************************************/
69 /***************************** Include Files *********************************/
71 #include "xil_types.h"
72 #include "xil_assert.h"
74 #include "xparameters.h"
76 /************************** Constant Definitions *****************************/
78 /**************************** Type Definitions *******************************/
80 /***************** Macros (Inline Functions) Definitions *********************/
82 /************************** Function Prototypes ******************************/
84 static void DistInit(XScuGic_Config *Config, u32 CpuID);
85 static void CPUInit(XScuGic_Config *Config);
86 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress);
88 /************************** Variable Definitions *****************************/
90 extern XScuGic_Config XScuGic_ConfigTable[XPAR_XSCUGIC_NUM_INSTANCES];
92 /*****************************************************************************/
95 * DistInit initializes the distributor of the GIC. The
96 * initialization entails:
98 * - Write the trigger mode, priority and target CPU
99 * - All interrupt sources are disabled
100 * - Enable the distributor
102 * @param InstancePtr is a pointer to the XScuGic instance.
103 * @param CpuID is the Cpu ID to be initialized.
109 ******************************************************************************/
110 static void DistInit(XScuGic_Config *Config, u32 CpuID)
113 u32 LocalCpuID = CpuID;
116 #warning "Building GIC for AMP"
119 * The distrubutor should not be initialized by FreeRTOS in the case of
120 * AMP -- it is assumed that Linux is the master of this device in that
126 XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET, 0U);
129 * Set the security domains in the int_security registers for non-secure
130 * interrupts. All are secure, so leave at the default. Set to 1 for
131 * non-secure interrupts.
136 * For the Shared Peripheral Interrupts INT_ID[MAX..32], set:
140 * 1. The trigger mode in the int_config register
141 * Only write to the SPI interrupts, so start at 32
143 for (Int_Id = 32U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+16U) {
145 * Each INT_ID uses two bits, or 16 INT_ID per register
146 * Set them all to be level sensitive, active HIGH.
148 XScuGic_WriteReg(Config->DistBaseAddress,
149 XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id), 0U);
153 #define DEFAULT_PRIORITY 0xa0a0a0a0U
154 for (Int_Id = 0U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+4U) {
156 * 2. The priority using int the priority_level register
157 * The priority_level and spi_target registers use one byte per
159 * Write a default value that can be changed elsewhere.
161 XScuGic_WriteReg(Config->DistBaseAddress,
162 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
166 for (Int_Id = 32U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+4U) {
168 * 3. The CPU interface in the spi_target register
169 * Only write to the SPI interrupts, so start at 32
171 LocalCpuID |= LocalCpuID << 8U;
172 LocalCpuID |= LocalCpuID << 16U;
174 XScuGic_WriteReg(Config->DistBaseAddress,
175 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id), LocalCpuID);
178 for (Int_Id = 0U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+32U) {
180 * 4. Enable the SPI using the enable_set register. Leave all disabled
183 XScuGic_WriteReg(Config->DistBaseAddress,
184 XSCUGIC_EN_DIS_OFFSET_CALC(XSCUGIC_DISABLE_OFFSET,
190 XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET,
191 XSCUGIC_EN_INT_MASK);
195 /*****************************************************************************/
198 * CPUInit initializes the CPU Interface of the GIC. The initialization entails:
200 * - Set the priority of the CPU.
201 * - Enable the CPU interface
203 * @param ConfigPtr is a pointer to a config table for the particular
204 * device this driver is associated with.
210 ******************************************************************************/
211 static void CPUInit(XScuGic_Config *Config)
214 * Program the priority mask of the CPU using the Priority mask
217 XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CPU_PRIOR_OFFSET,
221 * If the CPU operates in both security domains, set parameters in the
222 * control_s register.
223 * 1. Set FIQen=1 to use FIQ for secure interrupts,
224 * 2. Program the AckCtl bit
225 * 3. Program the SBPR bit to select the binary pointer behavior
226 * 4. Set EnableS = 1 to enable secure interrupts
227 * 5. Set EnbleNS = 1 to enable non secure interrupts
231 * If the CPU operates only in the secure domain, setup the
232 * control_s register.
234 * 2. Set EnableS=1, to enable the CPU interface to signal secure .
235 * interrupts Only enable the IRQ output unless secure interrupts
238 XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CONTROL_OFFSET, 0x07U);
242 /*****************************************************************************/
245 * CfgInitialize a specific interrupt controller instance/driver. The
246 * initialization entails:
248 * - Initialize fields of the XScuGic structure
249 * - Initial vector table with stub function calls
250 * - All interrupt sources are disabled
252 * @param InstancePtr is a pointer to the XScuGic instance to be worked on.
253 * @param ConfigPtr is a pointer to a config table for the particular device
254 * this driver is associated with.
255 * @param EffectiveAddr is the device base address in the virtual memory address
256 * space. The caller is responsible for keeping the address mapping
257 * from EffectiveAddr to the device physical base address unchanged
258 * once this function is invoked. Unexpected errors may occur if the
259 * address mapping changes after this function is called. If address
260 * translation is not used, use Config->BaseAddress for this parameters,
261 * passing the physical address instead.
265 * - XST_SUCCESS if initialization was successful
271 ******************************************************************************/
272 s32 XScuGic_DeviceInitialize(u32 DeviceId)
274 XScuGic_Config *Config;
275 u32 Cpu_Id = (u32)XPAR_CPU_ID + (u32)1;
277 Config = &XScuGic_ConfigTable[(u32 )DeviceId];
279 DistInit(Config, Cpu_Id);
286 /*****************************************************************************/
288 * This function is the primary interrupt handler for the driver. It must be
289 * connected to the interrupt source such that it is called when an interrupt of
290 * the interrupt controller is active. It will resolve which interrupts are
291 * active and enabled and call the appropriate interrupt handler. It uses
292 * the Interrupt Type information to determine when to acknowledge the
293 * interrupt.Highest priority interrupts are serviced first.
295 * This function assumes that an interrupt vector table has been previously
296 * initialized. It does not verify that entries in the table are valid before
297 * calling an interrupt handler.
299 * @param DeviceId is the unique identifier for the ScuGic device.
305 ******************************************************************************/
306 void XScuGic_DeviceInterruptHandler(void *DeviceId)
311 XScuGic_VectorTableEntry *TablePtr;
312 XScuGic_Config *CfgPtr;
314 CfgPtr = &XScuGic_ConfigTable[(INTPTR )DeviceId];
317 * Read the int_ack register to identify the highest priority
318 * interrupt ID and make sure it is valid. Reading Int_Ack will
319 * clear the interrupt in the GIC.
321 IntIDFull = XScuGic_ReadReg(CfgPtr->CpuBaseAddress, XSCUGIC_INT_ACK_OFFSET);
322 InterruptID = IntIDFull & XSCUGIC_ACK_INTID_MASK;
323 if(XSCUGIC_MAX_NUM_INTR_INPUTS < InterruptID){
328 * If the interrupt is shared, do some locking here if there are
329 * multiple processors.
332 * If pre-eption is required:
333 * Re-enable pre-emption by setting the CPSR I bit for non-secure ,
334 * interrupts or the F bit for secure interrupts
338 * If we need to change security domains, issue a SMC instruction here.
342 * Execute the ISR. Jump into the Interrupt service routine based on
343 * the IRQSource. A software trigger is cleared by the ACK.
345 TablePtr = &(CfgPtr->HandlerTable[InterruptID]);
346 if(TablePtr != NULL) {
347 TablePtr->Handler(TablePtr->CallBackRef);
352 * Write to the EOI register, we are all done here.
353 * Let this function return, the boot code will restore the stack.
355 XScuGic_WriteReg(CfgPtr->CpuBaseAddress, XSCUGIC_EOI_OFFSET, IntIDFull);
358 * Return from the interrupt. Change security domains could happen
363 /*****************************************************************************/
366 * Register a handler function for a specific interrupt ID. The vector table
367 * of the interrupt controller is updated, overwriting any previous handler.
368 * The handler function will be called when an interrupt occurs for the given
371 * @param BaseAddress is the CPU Interface Register base address of the
372 * interrupt controller whose vector table will be modified.
373 * @param InterruptId is the interrupt ID to be associated with the input
375 * @param Handler is the function pointer that will be added to
376 * the vector table for the given interrupt ID.
377 * @param CallBackRef is the argument that will be passed to the new
378 * handler function when it is called. This is user-specific.
384 * Note that this function has no effect if the input base address is invalid.
386 ******************************************************************************/
387 void XScuGic_RegisterHandler(u32 BaseAddress, s32 InterruptID,
388 Xil_InterruptHandler IntrHandler, void *CallBackRef)
390 XScuGic_Config *CfgPtr;
391 CfgPtr = LookupConfigByBaseAddress(BaseAddress);
394 if( IntrHandler != NULL) {
395 CfgPtr->HandlerTable[InterruptID].Handler = IntrHandler;
397 if( CallBackRef != NULL) {
398 CfgPtr->HandlerTable[InterruptID].CallBackRef = CallBackRef;
403 /*****************************************************************************/
406 * Looks up the device configuration based on the CPU interface base address of
407 * the device. A table contains the configuration info for each device in the
410 * @param CpuBaseAddress is the CPU Interface Register base address.
412 * @return A pointer to the configuration structure for the specified
413 * device, or NULL if the device was not found.
417 ******************************************************************************/
418 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress)
420 XScuGic_Config *CfgPtr = NULL;
423 for (Index = 0U; Index < XPAR_SCUGIC_NUM_INSTANCES; Index++) {
424 if (XScuGic_ConfigTable[Index].CpuBaseAddress ==
426 CfgPtr = &XScuGic_ConfigTable[Index];
431 return (XScuGic_Config *)CfgPtr;
434 /****************************************************************************/
436 * Sets the interrupt priority and trigger type for the specificd IRQ source.
438 * @param BaseAddr is the device base address
439 * @param Int_Id is the IRQ source number to modify
440 * @param Priority is the new priority for the IRQ source. 0 is highest
441 * priority, 0xF8 (248) is lowest. There are 32 priority levels
442 * supported with a step of 8. Hence the supported priorities are
443 * 0, 8, 16, 32, 40 ..., 248.
444 * @param Trigger is the new trigger type for the IRQ source.
445 * Each bit pair describes the configuration for an INT_ID.
446 * SFI Read Only b10 always
447 * PPI Read Only depending on how the PPIs are configured.
448 * b01 Active HIGH level sensitive
449 * b11 Rising edge sensitive
450 * SPI LSB is read only.
451 * b01 Active HIGH level sensitive
452 * b11 Rising edge sensitive/
456 * @note This API has the similar functionality of XScuGic_SetPriority
457 * TriggerType() and should be used when there is no InstancePtr.
459 *****************************************************************************/
460 void XScuGic_SetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
461 u8 Priority, u8 Trigger)
464 u8 LocalPriority = Priority;
466 Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
467 Xil_AssertVoid(Trigger <= XSCUGIC_INT_CFG_MASK);
468 Xil_AssertVoid(LocalPriority <= XSCUGIC_MAX_INTR_PRIO_VAL);
471 * Determine the register to write to using the Int_Id.
473 RegValue = XScuGic_ReadReg(DistBaseAddress,
474 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
477 * The priority bits are Bits 7 to 3 in GIC Priority Register. This
478 * means the number of priority levels supported are 32 and they are
479 * in steps of 8. The priorities can be 0, 8, 16, 32, 48, ... etc.
480 * The lower order 3 bits are masked before putting it in the register.
482 LocalPriority = LocalPriority & XSCUGIC_INTR_PRIO_MASK;
484 * Shift and Mask the correct bits for the priority and trigger in the
487 RegValue &= ~(XSCUGIC_PRIORITY_MASK << ((Int_Id%4U)*8U));
488 RegValue |= (u32)LocalPriority << ((Int_Id%4U)*8U);
491 * Write the value back to the register.
493 XScuGic_WriteReg(DistBaseAddress, XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
496 * Determine the register to write to using the Int_Id.
498 RegValue = XScuGic_ReadReg(DistBaseAddress,
499 XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
502 * Shift and Mask the correct bits for the priority and trigger in the
505 RegValue &= ~(XSCUGIC_INT_CFG_MASK << ((Int_Id%16U)*2U));
506 RegValue |= (u32)Trigger << ((Int_Id%16U)*2U);
509 * Write the value back to the register.
511 XScuGic_WriteReg(DistBaseAddress, XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id),
515 /****************************************************************************/
517 * Gets the interrupt priority and trigger type for the specificd IRQ source.
519 * @param BaseAddr is the device base address
520 * @param Int_Id is the IRQ source number to modify
521 * @param Priority is a pointer to the value of the priority of the IRQ
522 * source. This is a return value.
523 * @param Trigger is pointer to the value of the trigger of the IRQ
524 * source. This is a return value.
528 * @note This API has the similar functionality of XScuGic_GetPriority
529 * TriggerType() and should be used when there is no InstancePtr.
531 *****************************************************************************/
532 void XScuGic_GetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
533 u8 *Priority, u8 *Trigger)
537 Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
538 Xil_AssertVoid(Priority != NULL);
539 Xil_AssertVoid(Trigger != NULL);
542 * Determine the register to read to using the Int_Id.
544 RegValue = XScuGic_ReadReg(DistBaseAddress,
545 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
548 * Shift and Mask the correct bits for the priority and trigger in the
551 RegValue = RegValue >> ((Int_Id%4U)*8U);
552 *Priority = (u8)(RegValue & XSCUGIC_PRIORITY_MASK);
555 * Determine the register to read to using the Int_Id.
557 RegValue = XScuGic_ReadReg(DistBaseAddress,
558 XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
561 * Shift and Mask the correct bits for the priority and trigger in the
564 RegValue = RegValue >> ((Int_Id%16U)*2U);
566 *Trigger = (u8)(RegValue & XSCUGIC_INT_CFG_MASK);