]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53_bsp/psu_cortexa53_0/libsrc/scugic_v3_2/src/xscugic_hw.c
Update the Xilinx UltraScale+ 64-bit demo to use the hardware definition and BSP...
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53_bsp / psu_cortexa53_0 / libsrc / scugic_v3_2 / src / xscugic_hw.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2015 Xilinx, Inc.  All rights reserved.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
18 *
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
25 * SOFTWARE.
26 *
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.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 *
35 * @file xscugic_hw.c
36 * @addtogroup scugic_v3_1
37 * @{
38 *
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.
47 *
48 * <pre>
49 * MODIFICATION HISTORY:
50 *
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
55 *                     702687).
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.
65 *
66 * </pre>
67 *
68 ******************************************************************************/
69
70
71 /***************************** Include Files *********************************/
72
73 #include "xil_types.h"
74 #include "xil_assert.h"
75 #include "xscugic.h"
76 #include "xparameters.h"
77
78 /************************** Constant Definitions *****************************/
79
80 /**************************** Type Definitions *******************************/
81
82 /***************** Macros (Inline Functions) Definitions *********************/
83
84 /************************** Function Prototypes ******************************/
85
86 static void DistInit(XScuGic_Config *Config, u32 CpuID);
87 static void CPUInit(XScuGic_Config *Config);
88 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress);
89
90 /************************** Variable Definitions *****************************/
91
92 extern XScuGic_Config XScuGic_ConfigTable[XPAR_XSCUGIC_NUM_INSTANCES];
93
94 /*****************************************************************************/
95 /**
96 *
97 * DistInit initializes the distributor of the GIC. The
98 * initialization entails:
99 *
100 * - Write the trigger mode, priority and target CPU
101 * - All interrupt sources are disabled
102 * - Enable the distributor
103 *
104 * @param        InstancePtr is a pointer to the XScuGic instance.
105 * @param        CpuID is the Cpu ID to be initialized.
106 *
107 * @return       None
108 *
109 * @note         None.
110 *
111 ******************************************************************************/
112 static void DistInit(XScuGic_Config *Config, u32 CpuID)
113 {
114         u32 Int_Id;
115         u32 LocalCpuID = CpuID;
116
117 #if USE_AMP==1
118         #warning "Building GIC for AMP"
119
120         /*
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
123          * case.
124          */
125         return;
126 #endif
127
128         XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET, 0U);
129
130         /*
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.
134          */
135
136
137         /*
138          * For the Shared Peripheral Interrupts INT_ID[MAX..32], set:
139          */
140
141         /*
142          * 1. The trigger mode in the int_config register
143          * Only write to the SPI interrupts, so start at 32
144          */
145         for (Int_Id = 32U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+16U) {
146         /*
147          * Each INT_ID uses two bits, or 16 INT_ID per register
148          * Set them all to be level sensitive, active HIGH.
149          */
150                 XScuGic_WriteReg(Config->DistBaseAddress,
151                         XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id), 0U);
152         }
153
154
155 #define DEFAULT_PRIORITY        0xa0a0a0a0U
156         for (Int_Id = 0U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+4U) {
157                 /*
158                  * 2. The priority using int the priority_level register
159                  * The priority_level and spi_target registers use one byte per
160                  * INT_ID.
161                  * Write a default value that can be changed elsewhere.
162                  */
163                 XScuGic_WriteReg(Config->DistBaseAddress,
164                                 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
165                                 DEFAULT_PRIORITY);
166         }
167
168         for (Int_Id = 32U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+4U) {
169                 /*
170                  * 3. The CPU interface in the spi_target register
171                  * Only write to the SPI interrupts, so start at 32
172                  */
173                 LocalCpuID |= LocalCpuID << 8U;
174                 LocalCpuID |= LocalCpuID << 16U;
175
176                 XScuGic_WriteReg(Config->DistBaseAddress,
177                                 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id), LocalCpuID);
178         }
179
180         for (Int_Id = 0U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+32U) {
181         /*
182          * 4. Enable the SPI using the enable_set register. Leave all disabled
183          * for now.
184          */
185                 XScuGic_WriteReg(Config->DistBaseAddress,
186                 XSCUGIC_EN_DIS_OFFSET_CALC(XSCUGIC_DISABLE_OFFSET,
187                 Int_Id),
188                 0xFFFFFFFFU);
189
190         }
191
192         XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET,
193                                                 XSCUGIC_EN_INT_MASK);
194
195 }
196
197 /*****************************************************************************/
198 /**
199 *
200 * CPUInit initializes the CPU Interface of the GIC. The initialization entails:
201 *
202 * - Set the priority of the CPU.
203 * - Enable the CPU interface
204 *
205 * @param        ConfigPtr is a pointer to a config table for the particular
206 *               device this driver is associated with.
207 *
208 * @return       None
209 *
210 * @note         None.
211 *
212 ******************************************************************************/
213 static void CPUInit(XScuGic_Config *Config)
214 {
215         /*
216          * Program the priority mask of the CPU using the Priority mask
217          * register
218          */
219         XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CPU_PRIOR_OFFSET,
220                                                                         0xF0U);
221
222         /*
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
230          */
231
232         /*
233          * If the CPU operates only in the secure domain, setup the
234          * control_s register.
235          * 1. Set FIQen=1,
236          * 2. Set EnableS=1, to enable the CPU interface to signal secure .
237          * interrupts Only enable the IRQ output unless secure interrupts
238          * are needed.
239          */
240         XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CONTROL_OFFSET, 0x07U);
241
242 }
243
244 /*****************************************************************************/
245 /**
246 *
247 * CfgInitialize a specific interrupt controller instance/driver. The
248 * initialization entails:
249 *
250 * - Initialize fields of the XScuGic structure
251 * - Initial vector table with stub function calls
252 * - All interrupt sources are disabled
253 *
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.
264 *
265 * @return
266 *
267 * - XST_SUCCESS if initialization was successful
268 *
269 * @note
270 *
271 * None.
272 *
273 ******************************************************************************/
274 s32 XScuGic_DeviceInitialize(u32 DeviceId)
275 {
276         XScuGic_Config *Config;
277         u32 Cpu_Id = (u32)XPAR_CPU_ID + (u32)1;
278
279         Config = &XScuGic_ConfigTable[(u32 )DeviceId];
280
281         DistInit(Config, Cpu_Id);
282
283         CPUInit(Config);
284
285         return XST_SUCCESS;
286 }
287
288 /*****************************************************************************/
289 /**
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.
296 *
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.
300 *
301 * @param        DeviceId is the unique identifier for the ScuGic device.
302 *
303 * @return       None.
304 *
305 * @note         None.
306 *
307 ******************************************************************************/
308 void XScuGic_DeviceInterruptHandler(void *DeviceId)
309 {
310
311         u32 InterruptID;
312         u32 IntIDFull;
313         XScuGic_VectorTableEntry *TablePtr;
314         XScuGic_Config *CfgPtr;
315
316         CfgPtr = &XScuGic_ConfigTable[(INTPTR )DeviceId];
317
318         /*
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.
322          */
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){
326                 goto IntrExit;
327         }
328
329         /*
330          * If the interrupt is shared, do some locking here if there are
331          * multiple processors.
332          */
333         /*
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
337          */
338
339         /*
340          * If we need to change security domains, issue a SMC instruction here.
341          */
342
343         /*
344          * Execute the ISR. Jump into the Interrupt service routine based on
345          * the IRQSource. A software trigger is cleared by the ACK.
346          */
347         TablePtr = &(CfgPtr->HandlerTable[InterruptID]);
348         if(TablePtr != NULL) {
349                 TablePtr->Handler(TablePtr->CallBackRef);
350         }
351
352 IntrExit:
353         /*
354          * Write to the EOI register, we are all done here.
355          * Let this function return, the boot code will restore the stack.
356          */
357         XScuGic_WriteReg(CfgPtr->CpuBaseAddress, XSCUGIC_EOI_OFFSET, IntIDFull);
358
359         /*
360          * Return from the interrupt. Change security domains could happen
361          * here.
362          */
363 }
364
365 /*****************************************************************************/
366 /**
367 *
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
371 * interrupt ID.
372 *
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
376 *               handler.
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.
381 *
382 * @return       None.
383 *
384 * @note
385 *
386 * Note that this function has no effect if the input base address is invalid.
387 *
388 ******************************************************************************/
389 void XScuGic_RegisterHandler(u32 BaseAddress, s32 InterruptID,
390                              Xil_InterruptHandler IntrHandler, void *CallBackRef)
391 {
392         XScuGic_Config *CfgPtr;
393         CfgPtr = LookupConfigByBaseAddress(BaseAddress);
394
395         if(CfgPtr != NULL) {
396                 if( IntrHandler != NULL) {
397                         CfgPtr->HandlerTable[InterruptID].Handler = IntrHandler;
398                 }
399                 if( CallBackRef != NULL) {
400                         CfgPtr->HandlerTable[InterruptID].CallBackRef = CallBackRef;
401                 }
402         }
403 }
404
405 /*****************************************************************************/
406 /**
407 *
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
410 * system.
411 *
412 * @param        CpuBaseAddress is the CPU Interface Register base address.
413 *
414 * @return       A pointer to the configuration structure for the specified
415 *               device, or NULL if the device was not found.
416 *
417 * @note         None.
418 *
419 ******************************************************************************/
420 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress)
421 {
422         XScuGic_Config *CfgPtr = NULL;
423         u32 Index;
424
425         for (Index = 0U; Index < XPAR_SCUGIC_NUM_INSTANCES; Index++) {
426                 if (XScuGic_ConfigTable[Index].CpuBaseAddress ==
427                                 CpuBaseAddress) {
428                         CfgPtr = &XScuGic_ConfigTable[Index];
429                         break;
430                 }
431         }
432
433         return (XScuGic_Config *)CfgPtr;
434 }
435
436 /****************************************************************************/
437 /**
438 * Sets the interrupt priority and trigger type for the specificd IRQ source.
439 *
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/
455 *
456 * @return       None.
457 *
458 * @note         This API has the similar functionality of XScuGic_SetPriority
459 *               TriggerType() and should be used when there is no InstancePtr.
460 *
461 *****************************************************************************/
462 void XScuGic_SetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
463                                         u8 Priority, u8 Trigger)
464 {
465         u32 RegValue;
466         u8 LocalPriority = Priority;
467
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);
471
472         /*
473          * Determine the register to write to using the Int_Id.
474          */
475         RegValue = XScuGic_ReadReg(DistBaseAddress,
476                         XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
477
478         /*
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.
483          */
484         LocalPriority = LocalPriority & XSCUGIC_INTR_PRIO_MASK;
485         /*
486          * Shift and Mask the correct bits for the priority and trigger in the
487          * register
488          */
489         RegValue &= ~(XSCUGIC_PRIORITY_MASK << ((Int_Id%4U)*8U));
490         RegValue |= (u32)LocalPriority << ((Int_Id%4U)*8U);
491
492         /*
493          * Write the value back to the register.
494          */
495         XScuGic_WriteReg(DistBaseAddress, XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
496                                         RegValue);
497         /*
498          * Determine the register to write to using the Int_Id.
499          */
500         RegValue = XScuGic_ReadReg(DistBaseAddress,
501                         XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
502
503         /*
504          * Shift and Mask the correct bits for the priority and trigger in the
505          * register
506          */
507         RegValue &= ~(XSCUGIC_INT_CFG_MASK << ((Int_Id%16U)*2U));
508         RegValue |= (u32)Trigger << ((Int_Id%16U)*2U);
509
510         /*
511          * Write the value back to the register.
512          */
513         XScuGic_WriteReg(DistBaseAddress, XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id),
514                                 RegValue);
515 }
516
517 /****************************************************************************/
518 /**
519 * Gets the interrupt priority and trigger type for the specificd IRQ source.
520 *
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.
527 *
528 * @return       None.
529 *
530 * @note         This API has the similar functionality of XScuGic_GetPriority
531 *               TriggerType() and should be used when there is no InstancePtr.
532 *
533 *****************************************************************************/
534 void XScuGic_GetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
535                                         u8 *Priority, u8 *Trigger)
536 {
537         u32 RegValue;
538
539         Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
540         Xil_AssertVoid(Priority != NULL);
541         Xil_AssertVoid(Trigger != NULL);
542
543         /*
544          * Determine the register to read to using the Int_Id.
545          */
546         RegValue = XScuGic_ReadReg(DistBaseAddress,
547             XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
548
549         /*
550          * Shift and Mask the correct bits for the priority and trigger in the
551          * register
552          */
553         RegValue = RegValue >> ((Int_Id%4U)*8U);
554         *Priority = (u8)(RegValue & XSCUGIC_PRIORITY_MASK);
555
556         /*
557          * Determine the register to read to using the Int_Id.
558          */
559         RegValue = XScuGic_ReadReg(DistBaseAddress,
560             XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
561
562         /*
563          * Shift and Mask the correct bits for the priority and trigger in the
564          * register
565          */
566         RegValue = RegValue >> ((Int_Id%16U)*2U);
567
568         *Trigger = (u8)(RegValue & XSCUGIC_INT_CFG_MASK);
569 }
570 /** @} */