]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/scugic_v2_1/src/xscugic_hw.c
bc91f97ee72c981ccd816dc3176988a40acc5b6b
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / scugic_v2_1 / src / xscugic_hw.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2014 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_v2_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 *
65 * </pre>
66 *
67 ******************************************************************************/
68
69
70 /***************************** Include Files *********************************/
71
72 #include "xparameters.h"
73 #include "xil_types.h"
74 #include "xil_assert.h"
75 #include "xscugic.h"
76
77 /************************** Constant Definitions *****************************/
78
79 /**************************** Type Definitions *******************************/
80
81 /***************** Macros (Inline Functions) Definitions *********************/
82
83 /************************** Function Prototypes ******************************/
84
85 static void DistInit(XScuGic_Config *Config, u32 CpuID);
86 static void CPUInit(XScuGic_Config *Config);
87 static XScuGic_Config *LookupConfigByBaseAddress(u32 BaseAddress);
88
89 /************************** Variable Definitions *****************************/
90
91 extern XScuGic_Config XScuGic_ConfigTable[];
92
93 /*****************************************************************************/
94 /**
95 *
96 * DistInit initializes the distributor of the GIC. The
97 * initialization entails:
98 *
99 * - Write the trigger mode, priority and target CPU
100 * - All interrupt sources are disabled
101 * - Enable the distributor
102 *
103 * @param        InstancePtr is a pointer to the XScuGic instance.
104 * @param        CpuID is the Cpu ID to be initialized.
105 *
106 * @return       None
107 *
108 * @note         None.
109 *
110 ******************************************************************************/
111 static void DistInit(XScuGic_Config *Config, u32 CpuID)
112 {
113         u32 Int_Id;
114
115 #if USE_AMP==1
116         #warning "Building GIC for AMP"
117
118         /*
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
121          * case.
122          */
123         return;
124 #endif
125
126         XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET, 0UL);
127
128         /*
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.
132          */
133
134
135         /*
136          * For the Shared Peripheral Interrupts INT_ID[MAX..32], set:
137          */
138
139         /*
140          * 1. The trigger mode in the int_config register
141          * Only write to the SPI interrupts, so start at 32
142          */
143         for (Int_Id = 32; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id+=16) {
144         /*
145          * Each INT_ID uses two bits, or 16 INT_ID per register
146          * Set them all to be level sensitive, active HIGH.
147          */
148                 XScuGic_WriteReg(Config->DistBaseAddress,
149                         XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id), 0UL);
150         }
151
152
153 #define DEFAULT_PRIORITY        0xa0a0a0a0UL
154         for (Int_Id = 0; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id+=4) {
155                 /*
156                  * 2. The priority using int the priority_level register
157                  * The priority_level and spi_target registers use one byte per
158                  * INT_ID.
159                  * Write a default value that can be changed elsewhere.
160                  */
161                 XScuGic_WriteReg(Config->DistBaseAddress,
162                                 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
163                                 DEFAULT_PRIORITY);
164         }
165
166         for (Int_Id = 32; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id+=4) {
167                 /*
168                  * 3. The CPU interface in the spi_target register
169                  * Only write to the SPI interrupts, so start at 32
170                  */
171                 CpuID |= CpuID << 8;
172                 CpuID |= CpuID << 16;
173
174                 XScuGic_WriteReg(Config->DistBaseAddress,
175                                 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id), CpuID);
176         }
177
178         for (Int_Id = 0; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id+=32) {
179         /*
180          * 4. Enable the SPI using the enable_set register. Leave all disabled
181          * for now.
182          */
183                 XScuGic_WriteReg(Config->DistBaseAddress,
184                 XSCUGIC_ENABLE_DISABLE_OFFSET_CALC(XSCUGIC_DISABLE_OFFSET,
185                 Int_Id),
186                 0xFFFFFFFFUL);
187
188         }
189
190         XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET,
191                                                 XSCUGIC_EN_INT_MASK);
192
193 }
194
195 /*****************************************************************************/
196 /**
197 *
198 * CPUInit initializes the CPU Interface of the GIC. The initialization entails:
199 *
200 * - Set the priority of the CPU.
201 * - Enable the CPU interface
202 *
203 * @param        ConfigPtr is a pointer to a config table for the particular
204 *               device this driver is associated with.
205 *
206 * @return       None
207 *
208 * @note         None.
209 *
210 ******************************************************************************/
211 static void CPUInit(XScuGic_Config *Config)
212 {
213         /*
214          * Program the priority mask of the CPU using the Priority mask
215          * register
216          */
217         XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CPU_PRIOR_OFFSET,
218                                                                         0xF0);
219
220         /*
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
228          */
229
230         /*
231          * If the CPU operates only in the secure domain, setup the
232          * control_s register.
233          * 1. Set FIQen=1,
234          * 2. Set EnableS=1, to enable the CPU interface to signal secure .
235          * interrupts Only enable the IRQ output unless secure interrupts
236          * are needed.
237          */
238         XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CONTROL_OFFSET, 0x07);
239
240 }
241
242 /*****************************************************************************/
243 /**
244 *
245 * CfgInitialize a specific interrupt controller instance/driver. The
246 * initialization entails:
247 *
248 * - Initialize fields of the XScuGic structure
249 * - Initial vector table with stub function calls
250 * - All interrupt sources are disabled
251 *
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.
262 *
263 * @return
264 *
265 * - XST_SUCCESS if initialization was successful
266 *
267 * @note
268 *
269 * None.
270 *
271 ******************************************************************************/
272 int XScuGic_DeviceInitialize(u32 DeviceId)
273 {
274         XScuGic_Config *Config;
275         u8 Cpu_Id = XPAR_CPU_ID + 1;
276
277         Config = &XScuGic_ConfigTable[(u32 )DeviceId];
278
279         DistInit(Config, Cpu_Id);
280
281         CPUInit(Config);
282
283         return XST_SUCCESS;
284 }
285
286 /*****************************************************************************/
287 /**
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.
294 *
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.
298 *
299 * @param        DeviceId is the unique identifier for the ScuGic device.
300 *
301 * @return       None.
302 *
303 * @note         None.
304 *
305 ******************************************************************************/
306 void XScuGic_DeviceInterruptHandler(void *DeviceId)
307 {
308
309         u32 IntID;
310         XScuGic_VectorTableEntry *TablePtr;
311         XScuGic_Config *CfgPtr;
312
313         CfgPtr = &XScuGic_ConfigTable[(u32 )DeviceId];
314
315         /*
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.
319          */
320         IntID = XScuGic_ReadReg(CfgPtr->CpuBaseAddress, XSCUGIC_INT_ACK_OFFSET)
321                                         & XSCUGIC_ACK_INTID_MASK;
322         if(XSCUGIC_MAX_NUM_INTR_INPUTS < IntID){
323                 goto IntrExit;
324         }
325
326         /*
327          * If the interrupt is shared, do some locking here if there are
328          * multiple processors.
329          */
330         /*
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
334          */
335
336         /*
337          * If we need to change security domains, issue a SMC instruction here.
338          */
339
340         /*
341          * Execute the ISR. Jump into the Interrupt service routine based on
342          * the IRQSource. A software trigger is cleared by the ACK.
343          */
344         TablePtr = &(CfgPtr->HandlerTable[IntID]);
345         TablePtr->Handler(TablePtr->CallBackRef);
346
347 IntrExit:
348         /*
349          * Write to the EOI register, we are all done here.
350          * Let this function return, the boot code will restore the stack.
351          */
352         XScuGic_WriteReg(CfgPtr->CpuBaseAddress, XSCUGIC_EOI_OFFSET, IntID);
353
354         /*
355          * Return from the interrupt. Change security domains could happen
356          * here.
357          */
358 }
359
360 /*****************************************************************************/
361 /**
362 *
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
366 * interrupt ID.
367 *
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
371 *               handler.
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.
376 *
377 * @return       None.
378 *
379 * @note
380 *
381 * Note that this function has no effect if the input base address is invalid.
382 *
383 ******************************************************************************/
384 void XScuGic_RegisterHandler(u32 BaseAddress, int InterruptId,
385                              Xil_InterruptHandler Handler, void *CallBackRef)
386 {
387         XScuGic_Config *CfgPtr;
388
389         CfgPtr = LookupConfigByBaseAddress(BaseAddress);
390         if (CfgPtr != NULL) {
391                 CfgPtr->HandlerTable[InterruptId].Handler = Handler;
392                 CfgPtr->HandlerTable[InterruptId].CallBackRef = CallBackRef;
393         }
394 }
395
396 /*****************************************************************************/
397 /**
398 *
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
401 * system.
402 *
403 * @param        CpuBaseAddress is the CPU Interface Register base address.
404 *
405 * @return       A pointer to the configuration structure for the specified
406 *               device, or NULL if the device was not found.
407 *
408 * @note         None.
409 *
410 ******************************************************************************/
411 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress)
412 {
413         XScuGic_Config *CfgPtr = NULL;
414         int Index;
415
416         for (Index = 0; Index < XPAR_SCUGIC_NUM_INSTANCES; Index++) {
417                 if (XScuGic_ConfigTable[Index].CpuBaseAddress ==
418                                 CpuBaseAddress) {
419                         CfgPtr = &XScuGic_ConfigTable[Index];
420                         break;
421                 }
422         }
423
424         return CfgPtr;
425 }
426
427 /****************************************************************************/
428 /**
429 * Sets the interrupt priority and trigger type for the specificd IRQ source.
430 *
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/
446 *
447 * @return       None.
448 *
449 * @note         This API has the similar functionality of XScuGic_SetPriority
450 *               TriggerType() and should be used when there is no InstancePtr.
451 *
452 *****************************************************************************/
453 void XScuGic_SetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
454                                         u8 Priority, u8 Trigger)
455 {
456         u32 RegValue;
457
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);
461
462         /*
463          * Determine the register to write to using the Int_Id.
464          */
465         RegValue = XScuGic_ReadReg(DistBaseAddress,
466                         XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
467
468         /*
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.
473          */
474         Priority = Priority & XSCUGIC_INTR_PRIO_MASK;
475         /*
476          * Shift and Mask the correct bits for the priority and trigger in the
477          * register
478          */
479         RegValue &= ~(XSCUGIC_PRIORITY_MASK << ((Int_Id%4)*8));
480         RegValue |= Priority << ((Int_Id%4)*8);
481
482         /*
483          * Write the value back to the register.
484          */
485         XScuGic_WriteReg(DistBaseAddress, XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
486                                         RegValue);
487         /*
488          * Determine the register to write to using the Int_Id.
489          */
490         RegValue = XScuGic_ReadReg(DistBaseAddress,
491                         XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
492
493         /*
494          * Shift and Mask the correct bits for the priority and trigger in the
495          * register
496          */
497         RegValue &= ~(XSCUGIC_INT_CFG_MASK << ((Int_Id%16)*2));
498         RegValue |= Trigger << ((Int_Id%16)*2);
499
500         /*
501          * Write the value back to the register.
502          */
503         XScuGic_WriteReg(DistBaseAddress, XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id),
504                                 RegValue);
505 }
506
507 /****************************************************************************/
508 /**
509 * Gets the interrupt priority and trigger type for the specificd IRQ source.
510 *
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.
517 *
518 * @return       None.
519 *
520 * @note         This API has the similar functionality of XScuGic_GetPriority
521 *               TriggerType() and should be used when there is no InstancePtr.
522 *
523 *****************************************************************************/
524 void XScuGic_GetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
525                                         u8 *Priority, u8 *Trigger)
526 {
527         u32 RegValue;
528
529         Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
530         Xil_AssertVoid(Priority != NULL);
531         Xil_AssertVoid(Trigger != NULL);
532
533         /*
534          * Determine the register to read to using the Int_Id.
535          */
536         RegValue = XScuGic_ReadReg(DistBaseAddress,
537             XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
538
539         /*
540          * Shift and Mask the correct bits for the priority and trigger in the
541          * register
542          */
543         RegValue = RegValue >> ((Int_Id%4)*8);
544         *Priority = RegValue & XSCUGIC_PRIORITY_MASK;
545
546         /*
547          * Determine the register to read to using the Int_Id.
548          */
549         RegValue = XScuGic_ReadReg(DistBaseAddress,
550             XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
551
552         /*
553          * Shift and Mask the correct bits for the priority and trigger in the
554          * register
555          */
556         RegValue = RegValue >> ((Int_Id%16)*2);
557
558         *Trigger = RegValue & XSCUGIC_INT_CFG_MASK;
559 }
560
561 /** @} */