aboutsummaryrefslogtreecommitdiff
path: root/platform/microblaze/intc.c
blob: f70b9a70c2d1f9348adc4f17a3a831d02131208f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (c) 2015 Travis Geiselbrecht
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <reg.h>
#include <err.h>
#include <trace.h>
#include <lk/init.h>
#include <kernel/thread.h>
#include <platform.h>
#include <platform/interrupts.h>
#include <platform/debug.h>
#include <sys/types.h>
#include <target/microblaze-config.h>

#define LOCAL_TRACE 0

#define R_ISR       0
#define R_IPR       1
#define R_IER       2
#define R_IAR       3
#define R_SIE       4
#define R_CIE       5
#define R_IVR       6
#define R_MER       7
#define R_MAX       8

#define INTC_REG(reg) (*REG32(INTC_BASEADDR + (reg) * 4))

static spin_lock_t lock;

struct int_handler_struct {
    int_handler handler;
    void *arg;
};

static struct int_handler_struct int_handler_table[MAX_INT];

void register_int_handler(unsigned int vector, int_handler handler, void *arg)
{
    LTRACEF("vector %u, handler %p, arg %p\n", vector, handler, arg);

    if (vector >= MAX_INT)
        return;

    spin_lock_saved_state_t state;
    spin_lock_irqsave(&lock, state);

    int_handler_table[vector].handler = handler;
    int_handler_table[vector].arg = arg;

    spin_unlock_irqrestore(&lock, state);
}

status_t mask_interrupt(unsigned int vector)
{
    LTRACEF("vector %u\n", vector);

    INTC_REG(R_CIE) = 1 << vector;

    return NO_ERROR;
}

status_t unmask_interrupt(unsigned int vector)
{
    LTRACEF("vector %u\n", vector);

    INTC_REG(R_SIE) = 1 << vector;

    return NO_ERROR;
}

enum handler_return platform_irq_handler(void)
{
    enum handler_return ret = INT_NO_RESCHEDULE;

    uint irq = INTC_REG(R_IVR);
    LTRACEF("irq %u, IPR 0x%x, ISR 0x%x\n", irq, INTC_REG(R_IPR), INTC_REG(R_ISR));

    if (irq < MAX_INT && int_handler_table[irq].handler)
        ret = int_handler_table[irq].handler(int_handler_table[irq].arg);

    INTC_REG(R_IAR) = 1 << irq;

    return ret;
}

static void intc_init(uint level)
{
    LTRACE;

    INTC_REG(R_CIE) = 0xffffffff;
    INTC_REG(R_MER) = 0x3;
}

LK_INIT_HOOK(intc, intc_init, LK_INIT_LEVEL_PLATFORM_EARLY);