wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 1 | /* |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 2 | * (C) Copyright 2007 Michal Simek |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 3 | * (C) Copyright 2004 Atmark Techno, Inc. |
| 4 | * |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 5 | * Michal SIMEK <monstr@monstr.eu> |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 6 | * Yasushi SHOJI <yashi@atmark-techno.com> |
| 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 27 | #include <common.h> |
| 28 | #include <command.h> |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 29 | #include <malloc.h> |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 30 | #include <asm/microblaze_intc.h> |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 31 | #include <asm/asm.h> |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 32 | |
| 33 | #undef DEBUG_INT |
| 34 | |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 35 | void enable_interrupts(void) |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 36 | { |
Michal Simek | 98c1979 | 2007-05-07 23:58:31 +0200 | [diff] [blame] | 37 | MSRSET(0x2); |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 40 | int disable_interrupts(void) |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 41 | { |
Michal Simek | c94f95e | 2010-12-21 08:30:39 +0100 | [diff] [blame] | 42 | unsigned int msr; |
| 43 | |
| 44 | MFS(msr, rmsr); |
Michal Simek | 98c1979 | 2007-05-07 23:58:31 +0200 | [diff] [blame] | 45 | MSRCLR(0x2); |
Michal Simek | c94f95e | 2010-12-21 08:30:39 +0100 | [diff] [blame] | 46 | return (msr & 0x2) != 0; |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 47 | } |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 48 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 49 | static struct irq_action *vecs; |
| 50 | static u32 irq_no; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 51 | |
| 52 | /* mapping structure to interrupt controller */ |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 53 | microblaze_intc_t *intc; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 54 | |
| 55 | /* default handler */ |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 56 | static void def_hdlr(void) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 57 | { |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 58 | puts("def_hdlr\n"); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 61 | static void enable_one_interrupt(int irq) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 62 | { |
| 63 | int mask; |
| 64 | int offset = 1; |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 65 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 66 | offset <<= irq; |
| 67 | mask = intc->ier; |
| 68 | intc->ier = (mask | offset); |
| 69 | #ifdef DEBUG_INT |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 70 | printf("Enable one interrupt irq %x - mask %x,ier %x\n", offset, mask, |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 71 | intc->ier); |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 72 | printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 73 | intc->iar, intc->mer); |
| 74 | #endif |
| 75 | } |
| 76 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 77 | static void disable_one_interrupt(int irq) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 78 | { |
| 79 | int mask; |
| 80 | int offset = 1; |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 81 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 82 | offset <<= irq; |
| 83 | mask = intc->ier; |
| 84 | intc->ier = (mask & ~offset); |
| 85 | #ifdef DEBUG_INT |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 86 | printf("Disable one interrupt irq %x - mask %x,ier %x\n", irq, mask, |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 87 | intc->ier); |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 88 | printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 89 | intc->iar, intc->mer); |
| 90 | #endif |
| 91 | } |
| 92 | |
Michal Simek | e76a06e | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 93 | int install_interrupt_handler(int irq, interrupt_handler_t *hdlr, void *arg) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 94 | { |
| 95 | struct irq_action *act; |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 96 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 97 | /* irq out of range */ |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 98 | if ((irq < 0) || (irq > irq_no)) { |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 99 | puts("IRQ out of range\n"); |
Michal Simek | e76a06e | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 100 | return -1; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 101 | } |
| 102 | act = &vecs[irq]; |
| 103 | if (hdlr) { /* enable */ |
| 104 | act->handler = hdlr; |
| 105 | act->arg = arg; |
| 106 | act->count = 0; |
| 107 | enable_one_interrupt (irq); |
Michal Simek | e76a06e | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 108 | return 0; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 109 | } |
Michal Simek | e76a06e | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 110 | |
| 111 | /* Disable */ |
| 112 | act->handler = (interrupt_handler_t *) def_hdlr; |
| 113 | act->arg = (void *)irq; |
| 114 | disable_one_interrupt(irq); |
| 115 | return 1; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* initialization interrupt controller - hardware */ |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 119 | static void intc_init(void) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 120 | { |
| 121 | intc->mer = 0; |
| 122 | intc->ier = 0; |
| 123 | intc->iar = 0xFFFFFFFF; |
| 124 | /* XIntc_Start - hw_interrupt enable and all interrupt enable */ |
| 125 | intc->mer = 0x3; |
| 126 | #ifdef DEBUG_INT |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 127 | printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 128 | intc->iar, intc->mer); |
| 129 | #endif |
| 130 | } |
| 131 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 132 | int interrupts_init(void) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 133 | { |
| 134 | int i; |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 135 | |
| 136 | #if defined(CONFIG_SYS_INTC_0_ADDR) && defined(CONFIG_SYS_INTC_0_NUM) |
| 137 | intc = (microblaze_intc_t *) (CONFIG_SYS_INTC_0_ADDR); |
| 138 | irq_no = CONFIG_SYS_INTC_0_NUM; |
| 139 | #endif |
| 140 | if (irq_no) { |
| 141 | vecs = calloc(1, sizeof(struct irq_action) * irq_no); |
| 142 | if (vecs == NULL) { |
| 143 | puts("Interrupt vector allocation failed\n"); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | /* initialize irq list */ |
| 148 | for (i = 0; i < irq_no; i++) { |
| 149 | vecs[i].handler = (interrupt_handler_t *) def_hdlr; |
| 150 | vecs[i].arg = (void *)i; |
| 151 | vecs[i].count = 0; |
| 152 | } |
| 153 | /* initialize intc controller */ |
| 154 | intc_init(); |
| 155 | enable_interrupts(); |
| 156 | } else { |
| 157 | puts("Undefined interrupt controller\n"); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 158 | } |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 162 | void interrupt_handler(void) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 163 | { |
Michal Simek | 2c00449 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 164 | int irqs = intc->ivr; /* find active interrupt */ |
| 165 | int mask = 1; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 166 | #ifdef DEBUG_INT |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 167 | int value; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 168 | printf ("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
| 169 | intc->iar, intc->mer); |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 170 | R14(value); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 171 | printf ("Interrupt handler on %x line, r14 %x\n", irqs, value); |
| 172 | #endif |
Michal Simek | 2c00449 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 173 | struct irq_action *act = vecs + irqs; |
| 174 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 175 | #ifdef DEBUG_INT |
Michal Simek | 2c00449 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 176 | printf |
| 177 | ("Jumping to interrupt handler rutine addr %x,count %x,arg %x\n", |
| 178 | act->handler, act->count, act->arg); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 179 | #endif |
Michal Simek | 2c00449 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 180 | act->handler (act->arg); |
| 181 | act->count++; |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 182 | |
Stephan Linz | 5db50d6 | 2012-02-22 19:12:43 +0100 | [diff] [blame] | 183 | intc->iar = mask << irqs; |
| 184 | |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 185 | #ifdef DEBUG_INT |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 186 | printf ("Dump INTC reg, isr %x, ier %x, iar %x, mer %x\n", intc->isr, |
| 187 | intc->ier, intc->iar, intc->mer); |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 188 | R14(value); |
| 189 | printf ("Interrupt handler on %x line, r14 %x\n", irqs, value); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 190 | #endif |
| 191 | } |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 192 | |
Jon Loeliger | 526e5ce | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 193 | #if defined(CONFIG_CMD_IRQ) |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 194 | int do_irqinfo(cmd_tbl_t *cmdtp, int flag, int argc, const char *argv[]) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 195 | { |
| 196 | int i; |
| 197 | struct irq_action *act = vecs; |
| 198 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 199 | if (irq_no) { |
| 200 | puts("\nInterrupt-Information:\n\n" |
| 201 | "Nr Routine Arg Count\n" |
| 202 | "-----------------------------\n"); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 203 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 204 | for (i = 0; i < irq_no; i++) { |
| 205 | if (act->handler != (interrupt_handler_t *) def_hdlr) { |
| 206 | printf("%02d %08x %08x %d\n", i, |
| 207 | (int)act->handler, (int)act->arg, |
| 208 | act->count); |
| 209 | } |
| 210 | act++; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 211 | } |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 212 | puts("\n"); |
| 213 | } else { |
| 214 | puts("Undefined interrupt controller\n"); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 215 | } |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 216 | return 0; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 217 | } |
| 218 | #endif |