Modules/armv7 - Just a little debug
[tpg/acess2.git] / Modules / armv7 / GIC / gic.c
1 /*
2  * ARMv7 GIC Support
3  * - By John Hodge (thePowersGang)
4  * 
5  * gic.c
6  * - GIC Core
7  */
8 #define DEBUG   1
9
10 #include <acess.h>
11 #include <modules.h>
12 #include "gic.h"
13
14 #define N_IRQS  1024
15
16 // === IMPORTS ===
17 extern void     *gpIRQHandler;
18
19 // === TYPES ===
20 typedef void (*tIRQ_Handler)(int, void*);
21
22 // === PROTOTYPES ===
23  int    GIC_Install(char **Arguments);
24 void    GIC_IRQHandler(void);
25
26 // === GLOBALS ===
27 MODULE_DEFINE(0, 0x100, armv7_GIC, GIC_Install, NULL, NULL);
28 Uint32  *gpGIC_DistributorBase;
29 Uint32  *gpGIC_InterfaceBase;
30 tPAddr  gGIC_DistributorAddr;
31 tPAddr  gGIC_InterfaceAddr;
32 tIRQ_Handler    gaIRQ_Handlers[N_IRQS];
33 void    *gaIRQ_HandlerData[N_IRQS];
34
35 // === CODE ===
36 int GIC_Install(char **Arguments)
37 {
38         // Realview PB
39         gGIC_InterfaceAddr   = 0x1e000000;
40         gGIC_DistributorAddr = 0x1e001000;
41
42         // Initialise
43         gpGIC_InterfaceBase = (void*)MM_MapHWPages(gGIC_InterfaceAddr, 1);
44         LOG("gpGIC_InterfaceBase = %p", gpGIC_InterfaceBase);
45         gpGIC_DistributorBase = (void*)MM_MapHWPages(gGIC_DistributorAddr, 1);
46         LOG("gpGIC_DistributorBase = %p", gpGIC_DistributorBase);
47
48         gpGIC_InterfaceBase[GICC_PMR] = 0xFF;   
49         gpGIC_InterfaceBase[GICC_CTLR] = 1;     // Enable CPU
50         gpGIC_DistributorBase[GICD_CTLR] = 1;   // Enable Distributor
51
52         gpIRQHandler = GIC_IRQHandler;
53
54         __asm__ __volatile__ ("cpsie if");      // Enable IRQs and FIQs
55
56         return MODULE_ERR_OK;
57 }
58
59 void GIC_IRQHandler(void)
60 {
61         Uint32  num = gpGIC_InterfaceBase[GICC_IAR];
62 //      Log_Debug("GIC", "IRQ 0x%x", num);
63         gaIRQ_Handlers[num]( num, gaIRQ_HandlerData[num] );
64         gpGIC_InterfaceBase[GICC_EOIR] = num;
65 }
66
67 int IRQ_AddHandler(int IRQ, tIRQ_Handler Handler, void *Ptr)
68 {
69         if( IRQ < 0 || IRQ >= N_IRQS-32 ) {
70                 return 1;
71         }
72         
73         LOG("IRQ = %i", IRQ);
74         IRQ += 32;      // 32 internal IRQs
75         LOG("IRQ = %i (after adjust)", IRQ);
76         LOG("mask = 0x%x", 1 << (IRQ & (31-1)));
77         gpGIC_DistributorBase[GICD_ISENABLER0+IRQ/32] = 1 << (IRQ & (32-1));
78         ((Uint8*)&gpGIC_DistributorBase[GICD_ITARGETSR0])[IRQ] = 1;
79         
80 //      Log_Warning("GIC", "TODO: Implement IRQ_AddHandler");
81         
82         if( gaIRQ_Handlers[IRQ] )
83                 return 2;
84         
85         gaIRQ_Handlers[IRQ] = Handler;
86         gaIRQ_HandlerData[IRQ] = Ptr;
87         
88         return 0;
89 }
90

UCC git Repository :: git.ucc.asn.au