Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 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 <sys/types.h> |
| 24 | #include <debug.h> |
| 25 | #include <err.h> |
| 26 | #include <reg.h> |
| 27 | #include <kernel/thread.h> |
| 28 | #include <platform/interrupts.h> |
| 29 | #include <arch/ops.h> |
| 30 | #include <arch/arm.h> |
| 31 | #include "platform_p.h" |
| 32 | #include <platform/omap3.h> |
| 33 | |
| 34 | struct int_handler_struct { |
| 35 | int_handler handler; |
| 36 | void *arg; |
| 37 | }; |
| 38 | |
| 39 | static struct int_handler_struct int_handler_table[INT_VECTORS]; |
| 40 | |
| 41 | #define vectorToController(vector) ((vector) / 32) |
| 42 | |
| 43 | void platform_init_interrupts(void) |
| 44 | { |
| 45 | unsigned int i; |
| 46 | |
| 47 | // reset the controller |
| 48 | *REG32(INTC_SYSCONFIG) = 0x2; // start a reset |
| 49 | while ((*REG32(INTC_SYSSTATUS) & 0x1) == 0) |
| 50 | ; |
| 51 | |
| 52 | // mask all interrupts |
| 53 | *REG32(INTC_MIR(0)) = 0xffffffff; |
| 54 | *REG32(INTC_MIR(1)) = 0xffffffff; |
| 55 | *REG32(INTC_MIR(2)) = 0xffffffff; |
| 56 | |
| 57 | // set up each of the interrupts |
| 58 | for (i = 0; i < INT_VECTORS; i++) { |
| 59 | // set each vector up as high priority IRQ |
| 60 | *REG32(INTC_ILR(i)) = 0; |
| 61 | //*ICReg(i / 32, INTCON_ILR_BASE + 4*(i%32)) = ((level_trigger[i/32] & (1<<(i%32))) ? (1<<1) : (0<<1)) | 0; |
| 62 | } |
| 63 | |
| 64 | // disable the priority threshold |
| 65 | *REG32(INTC_THRESHOLD) = 0xff; |
| 66 | |
| 67 | // clear any pending sw interrupts |
| 68 | *REG32(INTC_ISR_CLEAR(0)) = 0xffffffff; |
| 69 | *REG32(INTC_ISR_CLEAR(1)) = 0xffffffff; |
| 70 | *REG32(INTC_ISR_CLEAR(2)) = 0xffffffff; |
| 71 | |
| 72 | // globally unmask interrupts |
| 73 | *REG32(INTC_CONTROL) = 3; // reset and enable the controller |
| 74 | } |
| 75 | |
Travis Geiselbrecht | f1cdea3 | 2008-09-13 14:13:54 -0700 | [diff] [blame] | 76 | status_t mask_interrupt(unsigned int vector) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 77 | { |
| 78 | if (vector >= INT_VECTORS) |
| 79 | return ERR_INVALID_ARGS; |
| 80 | |
| 81 | // dprintf("%s: vector %d\n", __PRETTY_FUNCTION__, vector); |
| 82 | |
| 83 | enter_critical_section(); |
| 84 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 85 | *REG32(INTC_MIR_SET(vectorToController(vector))) = 1 << (vector % 32); |
| 86 | |
| 87 | exit_critical_section(); |
| 88 | |
| 89 | return NO_ERROR; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | void platform_mask_irqs(void) |
| 94 | { |
| 95 | int i; |
| 96 | for (i=0; i<INT_VECTORS; i++) |
Travis Geiselbrecht | f1cdea3 | 2008-09-13 14:13:54 -0700 | [diff] [blame] | 97 | mask_interrupt(i); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Travis Geiselbrecht | f1cdea3 | 2008-09-13 14:13:54 -0700 | [diff] [blame] | 100 | status_t unmask_interrupt(unsigned int vector) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 101 | { |
| 102 | if (vector >= INT_VECTORS) |
| 103 | return ERR_INVALID_ARGS; |
| 104 | |
| 105 | // dprintf("%s: vector %d\n", __PRETTY_FUNCTION__, vector); |
| 106 | |
| 107 | enter_critical_section(); |
| 108 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 109 | *REG32(INTC_MIR_CLEAR(vectorToController(vector))) = 1 << (vector % 32); |
| 110 | |
| 111 | exit_critical_section(); |
| 112 | |
| 113 | return NO_ERROR; |
| 114 | } |
| 115 | |
| 116 | enum handler_return platform_irq(struct arm_iframe *frame) |
| 117 | { |
| 118 | // get the current vector |
| 119 | unsigned int vector; |
| 120 | |
| 121 | // read the currently active IRQ |
| 122 | vector = *REG32(INTC_SIR_IRQ) & 0x7f; |
| 123 | |
| 124 | // TRACEF("spsr 0x%x, pc 0x%x, currthread %p, vector %d, handler %p\n", frame->spsr, frame->pc, current_thread, vector, int_handler_table[vector].handler); |
| 125 | |
| 126 | #if THREAD_STATS |
| 127 | thread_stats.interrupts++; |
| 128 | #endif |
| 129 | |
| 130 | // deliver the interrupt |
| 131 | enum handler_return ret; |
| 132 | |
| 133 | ret = INT_NO_RESCHEDULE; |
| 134 | if (int_handler_table[vector].handler) |
| 135 | ret = int_handler_table[vector].handler(int_handler_table[vector].arg); |
| 136 | |
| 137 | // ack the interrupt |
| 138 | *REG32(INTC_CONTROL) = 0x1; |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | void platform_fiq(struct arm_iframe *frame) |
| 144 | { |
| 145 | PANIC_UNIMPLEMENTED; |
| 146 | } |
| 147 | |
| 148 | void register_int_handler(unsigned int vector, int_handler handler, void *arg) |
| 149 | { |
| 150 | if (vector >= INT_VECTORS) |
| 151 | panic("register_int_handler: vector out of range %d\n", vector); |
| 152 | |
| 153 | enter_critical_section(); |
| 154 | |
| 155 | int_handler_table[vector].arg = arg; |
| 156 | int_handler_table[vector].handler = handler; |
| 157 | |
| 158 | exit_critical_section(); |
| 159 | } |
| 160 | |
| 161 | |