Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 2 | /* |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 3 | * (C) Copyright 2007 Michal Simek |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 4 | * (C) Copyright 2004 Atmark Techno, Inc. |
| 5 | * |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 6 | * Michal SIMEK <monstr@monstr.eu> |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 7 | * Yasushi SHOJI <yashi@atmark-techno.com> |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <command.h> |
Michal Simek | cf84885 | 2016-02-15 12:10:32 +0100 | [diff] [blame] | 12 | #include <fdtdec.h> |
Simon Glass | 9b61c7c | 2019-11-14 12:57:41 -0700 | [diff] [blame] | 13 | #include <irq_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 17 | #include <asm/microblaze_intc.h> |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 18 | #include <asm/asm.h> |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 19 | |
Michal Simek | cf84885 | 2016-02-15 12:10:32 +0100 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 22 | void enable_interrupts(void) |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 23 | { |
Michal Simek | 056dbe8 | 2015-01-26 15:25:32 +0100 | [diff] [blame] | 24 | debug("Enable interrupts for the whole CPU\n"); |
Michal Simek | 98c1979 | 2007-05-07 23:58:31 +0200 | [diff] [blame] | 25 | MSRSET(0x2); |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 28 | int disable_interrupts(void) |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 29 | { |
Michal Simek | c94f95e | 2010-12-21 08:30:39 +0100 | [diff] [blame] | 30 | unsigned int msr; |
| 31 | |
| 32 | MFS(msr, rmsr); |
Michal Simek | 98c1979 | 2007-05-07 23:58:31 +0200 | [diff] [blame] | 33 | MSRCLR(0x2); |
Michal Simek | c94f95e | 2010-12-21 08:30:39 +0100 | [diff] [blame] | 34 | return (msr & 0x2) != 0; |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 35 | } |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 36 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 37 | static struct irq_action *vecs; |
| 38 | static u32 irq_no; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 39 | |
| 40 | /* mapping structure to interrupt controller */ |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 41 | microblaze_intc_t *intc; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 42 | |
| 43 | /* default handler */ |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 44 | static void def_hdlr(void) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 45 | { |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 46 | puts("def_hdlr\n"); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 49 | static void enable_one_interrupt(int irq) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 50 | { |
| 51 | int mask; |
| 52 | int offset = 1; |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 53 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 54 | offset <<= irq; |
| 55 | mask = intc->ier; |
| 56 | intc->ier = (mask | offset); |
Michal Simek | baf5175 | 2015-01-26 14:37:52 +0100 | [diff] [blame] | 57 | |
| 58 | debug("Enable one interrupt irq %x - mask %x,ier %x\n", offset, mask, |
| 59 | intc->ier); |
| 60 | debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
| 61 | intc->iar, intc->mer); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 64 | static void disable_one_interrupt(int irq) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 65 | { |
| 66 | int mask; |
| 67 | int offset = 1; |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 68 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 69 | offset <<= irq; |
| 70 | mask = intc->ier; |
| 71 | intc->ier = (mask & ~offset); |
Michal Simek | baf5175 | 2015-01-26 14:37:52 +0100 | [diff] [blame] | 72 | |
| 73 | debug("Disable one interrupt irq %x - mask %x,ier %x\n", irq, mask, |
| 74 | intc->ier); |
| 75 | debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
| 76 | intc->iar, intc->mer); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Michal Simek | e76a06e | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 79 | int install_interrupt_handler(int irq, interrupt_handler_t *hdlr, void *arg) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 80 | { |
| 81 | struct irq_action *act; |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 82 | |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 83 | /* irq out of range */ |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 84 | if ((irq < 0) || (irq > irq_no)) { |
Michal Simek | 8b4bb3a | 2012-06-29 13:27:28 +0200 | [diff] [blame] | 85 | puts("IRQ out of range\n"); |
Michal Simek | e76a06e | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 86 | return -1; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 87 | } |
| 88 | act = &vecs[irq]; |
| 89 | if (hdlr) { /* enable */ |
| 90 | act->handler = hdlr; |
| 91 | act->arg = arg; |
| 92 | act->count = 0; |
Michal Simek | 5e6c747 | 2015-01-26 14:39:22 +0100 | [diff] [blame] | 93 | enable_one_interrupt(irq); |
Michal Simek | e76a06e | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 94 | return 0; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 95 | } |
Michal Simek | e76a06e | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 96 | |
| 97 | /* Disable */ |
Michal Simek | 5e6c747 | 2015-01-26 14:39:22 +0100 | [diff] [blame] | 98 | act->handler = (interrupt_handler_t *)def_hdlr; |
Michal Simek | e76a06e | 2012-06-29 14:21:52 +0200 | [diff] [blame] | 99 | act->arg = (void *)irq; |
| 100 | disable_one_interrupt(irq); |
| 101 | return 1; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* initialization interrupt controller - hardware */ |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 105 | static void intc_init(void) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 106 | { |
| 107 | intc->mer = 0; |
| 108 | intc->ier = 0; |
| 109 | intc->iar = 0xFFFFFFFF; |
| 110 | /* XIntc_Start - hw_interrupt enable and all interrupt enable */ |
| 111 | intc->mer = 0x3; |
Michal Simek | baf5175 | 2015-01-26 14:37:52 +0100 | [diff] [blame] | 112 | |
| 113 | debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
| 114 | intc->iar, intc->mer); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Michal Simek | b78df3d | 2015-01-27 12:44:12 +0100 | [diff] [blame] | 117 | int interrupt_init(void) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 118 | { |
| 119 | int i; |
Michal Simek | cf84885 | 2016-02-15 12:10:32 +0100 | [diff] [blame] | 120 | const void *blob = gd->fdt_blob; |
| 121 | int node = 0; |
| 122 | |
| 123 | debug("INTC: Initialization\n"); |
| 124 | |
| 125 | node = fdt_node_offset_by_compatible(blob, node, |
| 126 | "xlnx,xps-intc-1.00.a"); |
| 127 | if (node != -1) { |
| 128 | fdt_addr_t base = fdtdec_get_addr(blob, node, "reg"); |
| 129 | if (base == FDT_ADDR_T_NONE) |
| 130 | return -1; |
| 131 | |
| 132 | debug("INTC: Base addr %lx\n", base); |
| 133 | intc = (microblaze_intc_t *)base; |
| 134 | irq_no = fdtdec_get_int(blob, node, "xlnx,num-intr-inputs", 0); |
| 135 | debug("INTC: IRQ NO %x\n", irq_no); |
| 136 | } else { |
| 137 | return node; |
| 138 | } |
Michal Simek | c3d51b9 | 2016-02-15 13:44:19 +0100 | [diff] [blame] | 139 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 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++) { |
Michal Simek | 5e6c747 | 2015-01-26 14:39:22 +0100 | [diff] [blame] | 149 | vecs[i].handler = (interrupt_handler_t *)def_hdlr; |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 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 | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 166 | int value; |
Michal Simek | 2c00449 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 167 | struct irq_action *act = vecs + irqs; |
| 168 | |
Michal Simek | baf5175 | 2015-01-26 14:37:52 +0100 | [diff] [blame] | 169 | debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier, |
| 170 | intc->iar, intc->mer); |
| 171 | #ifdef DEBUG |
| 172 | R14(value); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 173 | #endif |
Michal Simek | baf5175 | 2015-01-26 14:37:52 +0100 | [diff] [blame] | 174 | debug("Interrupt handler on %x line, r14 %x\n", irqs, value); |
| 175 | |
| 176 | debug("Jumping to interrupt handler rutine addr %x,count %x,arg %x\n", |
| 177 | (u32)act->handler, act->count, (u32)act->arg); |
Michal Simek | 5e6c747 | 2015-01-26 14:39:22 +0100 | [diff] [blame] | 178 | act->handler(act->arg); |
Michal Simek | 2c00449 | 2010-04-16 11:51:59 +0200 | [diff] [blame] | 179 | act->count++; |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 180 | |
Stephan Linz | 5db50d6 | 2012-02-22 19:12:43 +0100 | [diff] [blame] | 181 | intc->iar = mask << irqs; |
| 182 | |
Michal Simek | baf5175 | 2015-01-26 14:37:52 +0100 | [diff] [blame] | 183 | debug("Dump INTC reg, isr %x, ier %x, iar %x, mer %x\n", intc->isr, |
| 184 | intc->ier, intc->iar, intc->mer); |
| 185 | #ifdef DEBUG |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 186 | R14(value); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 187 | #endif |
Michal Simek | baf5175 | 2015-01-26 14:37:52 +0100 | [diff] [blame] | 188 | debug("Interrupt handler on %x line, r14 %x\n", irqs, value); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 189 | } |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 190 | |
Jon Loeliger | 526e5ce | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 191 | #if defined(CONFIG_CMD_IRQ) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 192 | int do_irqinfo(struct cmd_tbl *cmdtp, int flag, int argc, const char *argv[]) |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 193 | { |
| 194 | int i; |
| 195 | struct irq_action *act = vecs; |
| 196 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 197 | if (irq_no) { |
| 198 | puts("\nInterrupt-Information:\n\n" |
| 199 | "Nr Routine Arg Count\n" |
| 200 | "-----------------------------\n"); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 201 | |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 202 | for (i = 0; i < irq_no; i++) { |
Michal Simek | 5e6c747 | 2015-01-26 14:39:22 +0100 | [diff] [blame] | 203 | if (act->handler != (interrupt_handler_t *)def_hdlr) { |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 204 | printf("%02d %08x %08x %d\n", i, |
Michal Simek | 5e6c747 | 2015-01-26 14:39:22 +0100 | [diff] [blame] | 205 | (int)act->handler, (int)act->arg, |
| 206 | act->count); |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 207 | } |
| 208 | act++; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 209 | } |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 210 | puts("\n"); |
| 211 | } else { |
| 212 | puts("Undefined interrupt controller\n"); |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 213 | } |
Michal Simek | 251ed2c | 2012-07-10 10:31:31 +0200 | [diff] [blame] | 214 | return 0; |
Michal Simek | 922ce20 | 2007-03-11 13:48:24 +0100 | [diff] [blame] | 215 | } |
| 216 | #endif |