wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 1 | /* |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 2 | * (C) Copyright 2007 Michal Simek |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 3 | * (C) Copyright 2004 Atmark Techno, Inc. |
| 4 | * |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 5 | * Michal SIMEK <monstr@monstr.eu> |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 6 | * Yasushi SHOJI <yashi@atmark-techno.com> |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <command.h> |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 13 | #include <malloc.h> |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 14 | #include <asm/microblaze_intc.h> |
Michal Simek | 42efed6 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 15 | #include <asm/asm.h> |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 16 | |
| 17 | #undef DEBUG_INT |
| 18 | |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 19 | void enable_interrupts(void) |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 20 | { |
Michal Simek | fb05f6d | 2007-05-07 23:58:31 +0200 | [diff] [blame] | 21 | MSRSET(0x2); |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 24 | int disable_interrupts(void) |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 25 | { |
Michal Simek | 68e99e5 | 2010-12-21 08:30:39 +0100 | [diff] [blame] | 26 | unsigned int msr; |
| 27 | |
| 28 | MFS(msr, rmsr); |
Michal Simek | fb05f6d | 2007-05-07 23:58:31 +0200 | [diff] [blame] | 29 | MSRCLR(0x2); |
Michal Simek | 68e99e5 | 2010-12-21 08:30:39 +0100 | [diff] [blame] | 30 | return (msr & 0x2) != 0; |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 31 | } |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 32 | |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 33 | static struct irq_action *vecs; |
| 34 | static u32 irq_no; |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 35 | |
| 36 | /* mapping structure to interrupt controller */ |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 37 | microblaze_intc_t *intc; |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 38 | |
| 39 | /* default handler */ |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 40 | static void def_hdlr(void) |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 41 | { |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 42 | puts("def_hdlr\n"); |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 45 | static void enable_one_interrupt(int irq) |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 46 | { |
| 47 | int mask; |
| 48 | int offset = 1; |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 49 | |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 50 | offset <<= irq; |
| 51 | mask = intc->ier; |
| 52 | intc->ier = (mask | offset); |
| 53 | #ifdef DEBUG_INT |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 54 | printf("Enable one interrupt irq %x - mask %x,ier %x\n", offset, mask, |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 55 | intc->ier); |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 56 | printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 57 | intc->iar, intc->mer); |
| 58 | #endif |
| 59 | } |
| 60 | |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 61 | static void disable_one_interrupt(int irq) |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 62 | { |
| 63 | int mask; |
| 64 | int offset = 1; |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 65 | |
Michal Simek | cfc6711 | 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 | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 70 | printf("Disable one interrupt irq %x - mask %x,ier %x\n", irq, mask, |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 71 | intc->ier); |
Michal Simek | 26e6da8 | 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 | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 73 | intc->iar, intc->mer); |
| 74 | #endif |
| 75 | } |
| 76 | |
Michal Simek | 8706908 | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 77 | int install_interrupt_handler(int irq, interrupt_handler_t *hdlr, void *arg) |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 78 | { |
| 79 | struct irq_action *act; |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 80 | |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 81 | /* irq out of range */ |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 82 | if ((irq < 0) || (irq > irq_no)) { |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 83 | puts("IRQ out of range\n"); |
Michal Simek | 8706908 | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 84 | return -1; |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 85 | } |
| 86 | act = &vecs[irq]; |
| 87 | if (hdlr) { /* enable */ |
| 88 | act->handler = hdlr; |
| 89 | act->arg = arg; |
| 90 | act->count = 0; |
| 91 | enable_one_interrupt (irq); |
Michal Simek | 8706908 | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 92 | return 0; |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 93 | } |
Michal Simek | 8706908 | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 94 | |
| 95 | /* Disable */ |
| 96 | act->handler = (interrupt_handler_t *) def_hdlr; |
| 97 | act->arg = (void *)irq; |
| 98 | disable_one_interrupt(irq); |
| 99 | return 1; |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* initialization interrupt controller - hardware */ |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 103 | static void intc_init(void) |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 104 | { |
| 105 | intc->mer = 0; |
| 106 | intc->ier = 0; |
| 107 | intc->iar = 0xFFFFFFFF; |
| 108 | /* XIntc_Start - hw_interrupt enable and all interrupt enable */ |
| 109 | intc->mer = 0x3; |
| 110 | #ifdef DEBUG_INT |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 111 | printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 112 | intc->iar, intc->mer); |
| 113 | #endif |
| 114 | } |
| 115 | |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 116 | int interrupts_init(void) |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 117 | { |
| 118 | int i; |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 119 | |
| 120 | #if defined(CONFIG_SYS_INTC_0_ADDR) && defined(CONFIG_SYS_INTC_0_NUM) |
| 121 | intc = (microblaze_intc_t *) (CONFIG_SYS_INTC_0_ADDR); |
| 122 | irq_no = CONFIG_SYS_INTC_0_NUM; |
| 123 | #endif |
| 124 | if (irq_no) { |
| 125 | vecs = calloc(1, sizeof(struct irq_action) * irq_no); |
| 126 | if (vecs == NULL) { |
| 127 | puts("Interrupt vector allocation failed\n"); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | /* initialize irq list */ |
| 132 | for (i = 0; i < irq_no; i++) { |
| 133 | vecs[i].handler = (interrupt_handler_t *) def_hdlr; |
| 134 | vecs[i].arg = (void *)i; |
| 135 | vecs[i].count = 0; |
| 136 | } |
| 137 | /* initialize intc controller */ |
| 138 | intc_init(); |
| 139 | enable_interrupts(); |
| 140 | } else { |
| 141 | puts("Undefined interrupt controller\n"); |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 142 | } |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
Michal Simek | 26e6da8 | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 146 | void interrupt_handler(void) |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 147 | { |
Michal Simek | 8125c98 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 148 | int irqs = intc->ivr; /* find active interrupt */ |
| 149 | int mask = 1; |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 150 | #ifdef DEBUG_INT |
Michal Simek | 42efed6 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 151 | int value; |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 152 | printf ("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
| 153 | intc->iar, intc->mer); |
Michal Simek | 42efed6 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 154 | R14(value); |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 155 | printf ("Interrupt handler on %x line, r14 %x\n", irqs, value); |
| 156 | #endif |
Michal Simek | 8125c98 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 157 | struct irq_action *act = vecs + irqs; |
| 158 | |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 159 | #ifdef DEBUG_INT |
Michal Simek | 8125c98 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 160 | printf |
| 161 | ("Jumping to interrupt handler rutine addr %x,count %x,arg %x\n", |
| 162 | act->handler, act->count, act->arg); |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 163 | #endif |
Michal Simek | 8125c98 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 164 | act->handler (act->arg); |
| 165 | act->count++; |
Michal Simek | 42efed6 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 166 | |
Stephan Linz | 0f88326 | 2012-02-22 19:12:43 +0100 | [diff] [blame] | 167 | intc->iar = mask << irqs; |
| 168 | |
Michal Simek | 42efed6 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 169 | #ifdef DEBUG_INT |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 170 | printf ("Dump INTC reg, isr %x, ier %x, iar %x, mer %x\n", intc->isr, |
| 171 | intc->ier, intc->iar, intc->mer); |
Michal Simek | 42efed6 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 172 | R14(value); |
| 173 | printf ("Interrupt handler on %x line, r14 %x\n", irqs, value); |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 174 | #endif |
| 175 | } |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 176 | |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 177 | #if defined(CONFIG_CMD_IRQ) |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 178 | int do_irqinfo(cmd_tbl_t *cmdtp, int flag, int argc, const char *argv[]) |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 179 | { |
| 180 | int i; |
| 181 | struct irq_action *act = vecs; |
| 182 | |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 183 | if (irq_no) { |
| 184 | puts("\nInterrupt-Information:\n\n" |
| 185 | "Nr Routine Arg Count\n" |
| 186 | "-----------------------------\n"); |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 187 | |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 188 | for (i = 0; i < irq_no; i++) { |
| 189 | if (act->handler != (interrupt_handler_t *) def_hdlr) { |
| 190 | printf("%02d %08x %08x %d\n", i, |
| 191 | (int)act->handler, (int)act->arg, |
| 192 | act->count); |
| 193 | } |
| 194 | act++; |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 195 | } |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 196 | puts("\n"); |
| 197 | } else { |
| 198 | puts("Undefined interrupt controller\n"); |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 199 | } |
Michal Simek | 575a3d2 | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 200 | return 0; |
Michal Simek | cfc6711 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 201 | } |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 202 | #endif |