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