1 /******************************************************************************
3 * Copyright (C) 2010 - 2014 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_v2_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
67 ******************************************************************************/
70 /***************************** Include Files *********************************/
72 #include "xparameters.h"
73 #include "xil_types.h"
74 #include "xil_assert.h"
77 /************************** Constant Definitions *****************************/
79 /**************************** Type Definitions *******************************/
81 /***************** Macros (Inline Functions) Definitions *********************/
83 /************************** Function Prototypes ******************************/
85 static void DistInit(XScuGic_Config *Config, u32 CpuID);
86 static void CPUInit(XScuGic_Config *Config);
87 static XScuGic_Config *LookupConfigByBaseAddress(u32 BaseAddress);
89 /************************** Variable Definitions *****************************/
91 extern XScuGic_Config XScuGic_ConfigTable[];
93 /*****************************************************************************/
96 * DistInit initializes the distributor of the GIC. The
97 * initialization entails:
99 * - Write the trigger mode, priority and target CPU
100 * - All interrupt sources are disabled
101 * - Enable the distributor
103 * @param InstancePtr is a pointer to the XScuGic instance.
104 * @param CpuID is the Cpu ID to be initialized.
110 ******************************************************************************/
111 static void DistInit(XScuGic_Config *Config, u32 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, 0UL);
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 = 32; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id+=16) {
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), 0UL);
153 #define DEFAULT_PRIORITY 0xa0a0a0a0UL
154 for (Int_Id = 0; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id+=4) {
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 = 32; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id+=4) {
168 * 3. The CPU interface in the spi_target register
169 * Only write to the SPI interrupts, so start at 32
172 CpuID |= CpuID << 16;
174 XScuGic_WriteReg(Config->DistBaseAddress,
175 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id), CpuID);
178 for (Int_Id = 0; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id+=32) {
180 * 4. Enable the SPI using the enable_set register. Leave all disabled
183 XScuGic_WriteReg(Config->DistBaseAddress,
184 XSCUGIC_ENABLE_DISABLE_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, 0x07);
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 int XScuGic_DeviceInitialize(u32 DeviceId)
274 XScuGic_Config *Config;
275 u8 Cpu_Id = XPAR_CPU_ID + 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)
310 XScuGic_VectorTableEntry *TablePtr;
311 XScuGic_Config *CfgPtr;
313 CfgPtr = &XScuGic_ConfigTable[(u32 )DeviceId];
316 * Read the int_ack register to identify the highest priority
317 * interrupt ID and make sure it is valid. Reading Int_Ack will
318 * clear the interrupt in the GIC.
320 IntID = XScuGic_ReadReg(CfgPtr->CpuBaseAddress, XSCUGIC_INT_ACK_OFFSET)
321 & XSCUGIC_ACK_INTID_MASK;
322 if(XSCUGIC_MAX_NUM_INTR_INPUTS < IntID){
327 * If the interrupt is shared, do some locking here if there are
328 * multiple processors.
331 * If pre-eption is required:
332 * Re-enable pre-emption by setting the CPSR I bit for non-secure ,
333 * interrupts or the F bit for secure interrupts
337 * If we need to change security domains, issue a SMC instruction here.
341 * Execute the ISR. Jump into the Interrupt service routine based on
342 * the IRQSource. A software trigger is cleared by the ACK.
344 TablePtr = &(CfgPtr->HandlerTable[IntID]);
345 TablePtr->Handler(TablePtr->CallBackRef);
349 * Write to the EOI register, we are all done here.
350 * Let this function return, the boot code will restore the stack.
352 XScuGic_WriteReg(CfgPtr->CpuBaseAddress, XSCUGIC_EOI_OFFSET, IntID);
355 * Return from the interrupt. Change security domains could happen
360 /*****************************************************************************/
363 * Register a handler function for a specific interrupt ID. The vector table
364 * of the interrupt controller is updated, overwriting any previous handler.
365 * The handler function will be called when an interrupt occurs for the given
368 * @param BaseAddress is the CPU Interface Register base address of the
369 * interrupt controller whose vector table will be modified.
370 * @param InterruptId is the interrupt ID to be associated with the input
372 * @param Handler is the function pointer that will be added to
373 * the vector table for the given interrupt ID.
374 * @param CallBackRef is the argument that will be passed to the new
375 * handler function when it is called. This is user-specific.
381 * Note that this function has no effect if the input base address is invalid.
383 ******************************************************************************/
384 void XScuGic_RegisterHandler(u32 BaseAddress, int InterruptId,
385 Xil_InterruptHandler Handler, void *CallBackRef)
387 XScuGic_Config *CfgPtr;
389 CfgPtr = LookupConfigByBaseAddress(BaseAddress);
390 if (CfgPtr != NULL) {
391 CfgPtr->HandlerTable[InterruptId].Handler = Handler;
392 CfgPtr->HandlerTable[InterruptId].CallBackRef = CallBackRef;
396 /*****************************************************************************/
399 * Looks up the device configuration based on the CPU interface base address of
400 * the device. A table contains the configuration info for each device in the
403 * @param CpuBaseAddress is the CPU Interface Register base address.
405 * @return A pointer to the configuration structure for the specified
406 * device, or NULL if the device was not found.
410 ******************************************************************************/
411 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress)
413 XScuGic_Config *CfgPtr = NULL;
416 for (Index = 0; Index < XPAR_SCUGIC_NUM_INSTANCES; Index++) {
417 if (XScuGic_ConfigTable[Index].CpuBaseAddress ==
419 CfgPtr = &XScuGic_ConfigTable[Index];
427 /****************************************************************************/
429 * Sets the interrupt priority and trigger type for the specificd IRQ source.
431 * @param BaseAddr is the device base address
432 * @param Int_Id is the IRQ source number to modify
433 * @param Priority is the new priority for the IRQ source. 0 is highest
434 * priority, 0xF8 (248) is lowest. There are 32 priority levels
435 * supported with a step of 8. Hence the supported priorities are
436 * 0, 8, 16, 32, 40 ..., 248.
437 * @param Trigger is the new trigger type for the IRQ source.
438 * Each bit pair describes the configuration for an INT_ID.
439 * SFI Read Only b10 always
440 * PPI Read Only depending on how the PPIs are configured.
441 * b01 Active HIGH level sensitive
442 * b11 Rising edge sensitive
443 * SPI LSB is read only.
444 * b01 Active HIGH level sensitive
445 * b11 Rising edge sensitive/
449 * @note This API has the similar functionality of XScuGic_SetPriority
450 * TriggerType() and should be used when there is no InstancePtr.
452 *****************************************************************************/
453 void XScuGic_SetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
454 u8 Priority, u8 Trigger)
458 Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
459 Xil_AssertVoid(Trigger <= XSCUGIC_INT_CFG_MASK);
460 Xil_AssertVoid(Priority <= XSCUGIC_MAX_INTR_PRIO_VAL);
463 * Determine the register to write to using the Int_Id.
465 RegValue = XScuGic_ReadReg(DistBaseAddress,
466 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
469 * The priority bits are Bits 7 to 3 in GIC Priority Register. This
470 * means the number of priority levels supported are 32 and they are
471 * in steps of 8. The priorities can be 0, 8, 16, 32, 48, ... etc.
472 * The lower order 3 bits are masked before putting it in the register.
474 Priority = Priority & XSCUGIC_INTR_PRIO_MASK;
476 * Shift and Mask the correct bits for the priority and trigger in the
479 RegValue &= ~(XSCUGIC_PRIORITY_MASK << ((Int_Id%4)*8));
480 RegValue |= Priority << ((Int_Id%4)*8);
483 * Write the value back to the register.
485 XScuGic_WriteReg(DistBaseAddress, XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
488 * Determine the register to write to using the Int_Id.
490 RegValue = XScuGic_ReadReg(DistBaseAddress,
491 XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
494 * Shift and Mask the correct bits for the priority and trigger in the
497 RegValue &= ~(XSCUGIC_INT_CFG_MASK << ((Int_Id%16)*2));
498 RegValue |= Trigger << ((Int_Id%16)*2);
501 * Write the value back to the register.
503 XScuGic_WriteReg(DistBaseAddress, XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id),
507 /****************************************************************************/
509 * Gets the interrupt priority and trigger type for the specificd IRQ source.
511 * @param BaseAddr is the device base address
512 * @param Int_Id is the IRQ source number to modify
513 * @param Priority is a pointer to the value of the priority of the IRQ
514 * source. This is a return value.
515 * @param Trigger is pointer to the value of the trigger of the IRQ
516 * source. This is a return value.
520 * @note This API has the similar functionality of XScuGic_GetPriority
521 * TriggerType() and should be used when there is no InstancePtr.
523 *****************************************************************************/
524 void XScuGic_GetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
525 u8 *Priority, u8 *Trigger)
529 Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
530 Xil_AssertVoid(Priority != NULL);
531 Xil_AssertVoid(Trigger != NULL);
534 * Determine the register to read to using the Int_Id.
536 RegValue = XScuGic_ReadReg(DistBaseAddress,
537 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
540 * Shift and Mask the correct bits for the priority and trigger in the
543 RegValue = RegValue >> ((Int_Id%4)*8);
544 *Priority = RegValue & XSCUGIC_PRIORITY_MASK;
547 * Determine the register to read to using the Int_Id.
549 RegValue = XScuGic_ReadReg(DistBaseAddress,
550 XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
553 * Shift and Mask the correct bits for the priority and trigger in the
556 RegValue = RegValue >> ((Int_Id%16)*2);
558 *Trigger = RegValue & XSCUGIC_INT_CFG_MASK;