blob: f70b9a70c2d1f9348adc4f17a3a831d02131208f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <reg.h>
24#include <err.h>
25#include <trace.h>
26#include <lk/init.h>
27#include <kernel/thread.h>
28#include <platform.h>
29#include <platform/interrupts.h>
30#include <platform/debug.h>
31#include <sys/types.h>
32#include <target/microblaze-config.h>
33
34#define LOCAL_TRACE 0
35
36#define R_ISR 0
37#define R_IPR 1
38#define R_IER 2
39#define R_IAR 3
40#define R_SIE 4
41#define R_CIE 5
42#define R_IVR 6
43#define R_MER 7
44#define R_MAX 8
45
46#define INTC_REG(reg) (*REG32(INTC_BASEADDR + (reg) * 4))
47
48static spin_lock_t lock;
49
50struct int_handler_struct {
51 int_handler handler;
52 void *arg;
53};
54
55static struct int_handler_struct int_handler_table[MAX_INT];
56
57void register_int_handler(unsigned int vector, int_handler handler, void *arg)
58{
59 LTRACEF("vector %u, handler %p, arg %p\n", vector, handler, arg);
60
61 if (vector >= MAX_INT)
62 return;
63
64 spin_lock_saved_state_t state;
65 spin_lock_irqsave(&lock, state);
66
67 int_handler_table[vector].handler = handler;
68 int_handler_table[vector].arg = arg;
69
70 spin_unlock_irqrestore(&lock, state);
71}
72
73status_t mask_interrupt(unsigned int vector)
74{
75 LTRACEF("vector %u\n", vector);
76
77 INTC_REG(R_CIE) = 1 << vector;
78
79 return NO_ERROR;
80}
81
82status_t unmask_interrupt(unsigned int vector)
83{
84 LTRACEF("vector %u\n", vector);
85
86 INTC_REG(R_SIE) = 1 << vector;
87
88 return NO_ERROR;
89}
90
91enum handler_return platform_irq_handler(void)
92{
93 enum handler_return ret = INT_NO_RESCHEDULE;
94
95 uint irq = INTC_REG(R_IVR);
96 LTRACEF("irq %u, IPR 0x%x, ISR 0x%x\n", irq, INTC_REG(R_IPR), INTC_REG(R_ISR));
97
98 if (irq < MAX_INT && int_handler_table[irq].handler)
99 ret = int_handler_table[irq].handler(int_handler_table[irq].arg);
100
101 INTC_REG(R_IAR) = 1 << irq;
102
103 return ret;
104}
105
106static void intc_init(uint level)
107{
108 LTRACE;
109
110 INTC_REG(R_CIE) = 0xffffffff;
111 INTC_REG(R_MER) = 0x3;
112}
113
114LK_INIT_HOOK(intc, intc_init, LK_INIT_LEVEL_PLATFORM_EARLY);
115