blob: 604d2dcd1a5954721c0ab624f4c4786b17dc3fd6 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 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 <err.h>
24#include <sys/types.h>
25#include <debug.h>
26#include <reg.h>
27#include <kernel/thread.h>
28#include <platform/interrupts.h>
29#include <platform/armemu.h>
30#include <arch/ops.h>
31#include <arch/arm.h>
32#include "platform_p.h"
33
34struct int_handler_struct {
35 int_handler handler;
36 void *arg;
37};
38
39static struct int_handler_struct int_handler_table[PIC_MAX_INT];
40
41void platform_init_interrupts(void)
42{
43 // mask all the interrupts
44 *REG32(PIC_MASK_LATCH) = 0xffffffff;
45}
46
47status_t mask_interrupt(unsigned int vector, bool *oldstate)
48{
49 if (vector >= PIC_MAX_INT)
50 return ERR_INVALID_ARGS;
51
52// dprintf("%s: vector %d\n", __PRETTY_FUNCTION__, vector);
53
54 enter_critical_section();
55
56 if (oldstate)
57 *oldstate = *REG32(PIC_MASK) & (1<<vector);
58 *REG32(PIC_MASK_LATCH) = 1 << vector;
59
60 exit_critical_section();
61
62 return NO_ERROR;
63}
64
65status_t unmask_interrupt(unsigned int vector, bool *oldstate)
66{
67 if (vector >= PIC_MAX_INT)
68 return ERR_INVALID_ARGS;
69
70// dprintf("%s: vector %d\n", __PRETTY_FUNCTION__, vector);
71
72 enter_critical_section();
73
74 if (oldstate)
75 *oldstate = *REG32(PIC_MASK) & (1<<vector);
76 *REG32(PIC_UNMASK_LATCH) = 1 << vector;
77
78 exit_critical_section();
79
80 return NO_ERROR;
81}
82
83enum handler_return platform_irq(struct arm_iframe *frame)
84{
85 // get the current vector
86 unsigned int vector = *REG32(PIC_CURRENT_NUM);
87 if (vector == 0xffffffff)
88 return INT_NO_RESCHEDULE;
89
90// dprintf("platform_irq: spsr 0x%x, pc 0x%x, currthread %p, vector %d\n", frame->spsr, frame->pc, current_thread, vector);
91
92 // deliver the interrupt
93 enum handler_return ret;
94
95 ret = INT_NO_RESCHEDULE;
96 if (int_handler_table[vector].handler)
97 ret = int_handler_table[vector].handler(int_handler_table[vector].arg);
98
99// dprintf("platform_irq: exit %d\n", ret);
100
101 return ret;
102}
103
104void platform_fiq(struct arm_iframe *frame)
105{
106 panic("FIQ: unimplemented\n");
107}
108
109void register_int_handler(unsigned int vector, int_handler handler, void *arg)
110{
111 if (vector >= PIC_MAX_INT)
112 panic("register_int_handler: vector out of range %d\n", vector);
113
114 enter_critical_section();
115
116 int_handler_table[vector].handler = handler;
117 int_handler_table[vector].arg = arg;
118
119 exit_critical_section();
120}
121