interrupt.c 6.2 KB
Newer Older
B
bigmagic 已提交
1
/*
B
bigamgic 已提交
2
 * Copyright (c) 2006-2020, RT-Thread Development Team
B
bigmagic 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-04-05     bigmagic     Initial version
 */

/**
 * @addtogroup ls2k
 */

/*@{*/

#include <rtthread.h>
#include <rthw.h>
#include <exception.h>
B
bigamgic 已提交
20 21 22
#include <drivers/pin.h>
#include "ls2k1000.h"
#include "interrupt.h"
B
bigmagic 已提交
23 24 25 26 27 28 29 30

static struct rt_irq_desc irq_handle_table[MAX_INTR];

static void rt_hw_interrupt_handler(int vector, void *param)
{
    rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
}

B
bigamgic 已提交
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
static void liointc_init(void)
{
    int i;
    /* Router for LIOINTC0, to Core 0, INT0 (CPU IP2) */
    for (i = 0; i < 32; i ++)
    {
        HWREG8(LIOINTC0_BASE + i) = LIOINTC_COREx_INTy(0, 0);
    }

    /* Router for LIOINTC1, to Core 0, INT1 (CPU IP3) */
    for (i = 0; i < 32; i ++)
    {
        HWREG8(LIOINTC1_BASE + i) = LIOINTC_COREx_INTy(0, 1);
    }

    /* Disable all IRQs */
    HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_DISABLE) = 0xffffffff;
    HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_DISABLE) = 0xffffffff;

    /* Set all IRQs to low level triggered (Default?) */
    HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_POL) = 0x0;
    HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_EDGE) = 0x0;

    HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_POL) = 0x0;
    HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_EDGE) = 0x0;
}

//set irq mode
void liointc_set_irq_mode(int irq, int mode)
{
0
0xcccccccccccc 已提交
61
    if (irq < 32)
B
bigamgic 已提交
62
    {
0
0xcccccccccccc 已提交
63
        if (mode == PIN_IRQ_MODE_RISING)
B
bigamgic 已提交
64 65 66 67
        {
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_POL)  |= (0x0 << (irq));
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_EDGE) |= (0x1 << (irq));
        }
0
0xcccccccccccc 已提交
68
        else if (mode == PIN_IRQ_MODE_FALLING)
B
bigamgic 已提交
69 70 71 72
        {
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_POL)  |= (0x1 << (irq));
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_EDGE) |= (0x1 << (irq));
        }
0
0xcccccccccccc 已提交
73
        else if (mode == PIN_IRQ_MODE_HIGH_LEVEL)
B
bigamgic 已提交
74 75 76 77
        {
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_POL)  |= (0x1 << (irq));
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_EDGE) |= (0x0 << (irq));
        }
0
0xcccccccccccc 已提交
78
        else if (mode == PIN_IRQ_MODE_LOW_LEVEL)
B
bigamgic 已提交
79 80 81 82 83 84 85 86 87 88 89 90
        {
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_POL)  |= (0x0 << (irq));
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_EDGE) |= (0x0 << (irq));
        }
        else
        {
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_POL)  |= (0x1 << (irq));
            HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_EDGE) |= (0x1 << (irq));
        }
    }
    else
    {
0
0xcccccccccccc 已提交
91
        if (mode == PIN_IRQ_MODE_RISING)
B
bigamgic 已提交
92 93 94 95
        {
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_POL)  |= (0x0 << (irq - 32));
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_EDGE) |= (0x1 << (irq - 32));
        }
0
0xcccccccccccc 已提交
96
        else if (mode == PIN_IRQ_MODE_FALLING)
B
bigamgic 已提交
97 98 99 100
        {
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_POL)  |= (0x1 << (irq - 32));
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_EDGE) |= (0x1 << (irq - 32));
        }
0
0xcccccccccccc 已提交
101
        else if (mode == PIN_IRQ_MODE_HIGH_LEVEL)
B
bigamgic 已提交
102 103 104 105
        {
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_POL)  |= (0x1 << (irq - 32));
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_EDGE) |= (0x0 << (irq - 32));
        }
0
0xcccccccccccc 已提交
106
        else if (mode == PIN_IRQ_MODE_LOW_LEVEL)
B
bigamgic 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        {
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_POL)  |= (0x0 << (irq - 32));
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_EDGE) |= (0x0 << (irq - 32));
        }
        else
        {
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_POL)  |= (0x1 << (irq - 32));
            HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_EDGE) |= (0x1 << (irq - 32));
        }
    }
}

static void liointc_isr(rt_ubase_t reg_base, rt_ubase_t isr_base,
                        int irq_base)
{
    rt_uint32_t isr, tmp;
    int intx;
    /* Mask to clear ISR */
    isr = HWREG32(isr_base);

    tmp = isr;
    /* Handle each of them */
    while (tmp)
    {
        rt_isr_handler_t irq_func;
        void *param;
        int irq;

        intx = __rt_ffs(isr) - 1;
        tmp &= ~(1 << intx);

        irq = intx + irq_base;

        irq_func = irq_handle_table[irq].handler;
        param = irq_handle_table[irq].param;
        irq_func(irq, param);
#ifdef RT_USING_INTERRUPT_INFO
        irq_handle_table[irq].counter++;
#endif
    }

    /* Enable them again */
    HWREG32(reg_base + LIOINTC_REG_INTC_ENABLE) = isr;
}

B
bigmagic 已提交
152 153 154 155 156 157
/**
 * This function will initialize hardware interrupt
 */
void rt_hw_interrupt_init(void)
{
    rt_uint32_t idx;
B
bigamgic 已提交
158

B
bigmagic 已提交
159 160 161 162 163
    rt_memset(irq_handle_table, 0x00, sizeof(irq_handle_table));
    for (idx = 0; idx < MAX_INTR; idx ++)
    {
        irq_handle_table[idx].handler = rt_hw_interrupt_handler;
    }
B
bigamgic 已提交
164 165

    liointc_init();
B
bigmagic 已提交
166 167 168
}

rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
0
0xcccccccccccc 已提交
169
        void *param, const char *name)
B
bigmagic 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
    rt_isr_handler_t old_handler = RT_NULL;

    if (vector >= 0 && vector < MAX_INTR)
    {
        old_handler = irq_handle_table[vector].handler;

#ifdef RT_USING_INTERRUPT_INFO
        rt_strncpy(irq_handle_table[vector].name, name, RT_NAME_MAX);
#endif /* RT_USING_INTERRUPT_INFO */
        irq_handle_table[vector].handler = handler;
        irq_handle_table[vector].param = param;
    }

0
0xcccccccccccc 已提交
184
    if (vector <= 32)
B
bigamgic 已提交
185 186 187 188 189 190 191
    {
        mips_unmask_cpu_irq(2);
    }
    else
    {
        mips_unmask_cpu_irq(3);
    }
0
0xcccccccccccc 已提交
192

B
bigmagic 已提交
193 194 195 196 197 198 199
    return old_handler;
}

void rt_do_mips_cpu_irq(rt_uint32_t ip)
{
    void *param;
    rt_isr_handler_t irq_func;
B
bigamgic 已提交
200 201
    if (ip == 7)
    {
B
bigmagic 已提交
202
        rt_hw_timer_handler();
B
bigamgic 已提交
203 204 205 206 207 208 209 210
    }
    else if (ip == 2)
    {
        liointc_isr(LIOINTC0_BASE, CORE0_INTISR0, LIOINTC0_IRQBASE);
    }
    else if (ip == 3)
    {
        liointc_isr(LIOINTC1_BASE, CORE0_INTISR1, LIOINTC1_IRQBASE);
B
bigmagic 已提交
211 212 213 214 215
    }
}

void rt_hw_interrupt_umask(int irq)
{
0
0xcccccccccccc 已提交
216
    if (irq < LIOINTC0_IRQBASE + 32)
B
bigamgic 已提交
217 218 219
    {
        HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_ENABLE) = (1 << irq);
    }
0
0xcccccccccccc 已提交
220
    else if (irq < LIOINTC1_IRQBASE + 32)
B
bigamgic 已提交
221 222 223
    {
        HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_ENABLE) = (1 << (irq - 32));
    }
B
bigmagic 已提交
224 225 226 227
}

void rt_hw_interrupt_mask(int irq)
{
0
0xcccccccccccc 已提交
228
    if (irq < LIOINTC0_IRQBASE + 32)
B
bigamgic 已提交
229 230 231
    {
        HWREG32(LIOINTC0_BASE + LIOINTC_REG_INTC_DISABLE) = (1 << irq);
    }
0
0xcccccccccccc 已提交
232
    else if (irq < LIOINTC1_IRQBASE + 32)
B
bigamgic 已提交
233 234 235
    {
        HWREG32(LIOINTC1_BASE + LIOINTC_REG_INTC_DISABLE) = (1 << (irq - 32));
    }
B
bigmagic 已提交
236 237
}
/*@}*/