blob: aea612eef15e4a1c8c619579a2abdf67a5639daa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk12490652004-04-18 21:13:41 +00002/*
Michal Simek922ce202007-03-11 13:48:24 +01003 * (C) Copyright 2007 Michal Simek
wdenk12490652004-04-18 21:13:41 +00004 * (C) Copyright 2004 Atmark Techno, Inc.
5 *
Michal Simek922ce202007-03-11 13:48:24 +01006 * Michal SIMEK <monstr@monstr.eu>
wdenk12490652004-04-18 21:13:41 +00007 * Yasushi SHOJI <yashi@atmark-techno.com>
wdenk12490652004-04-18 21:13:41 +00008 */
9
Michal Simek922ce202007-03-11 13:48:24 +010010#include <common.h>
11#include <command.h>
Michal Simekcf848852016-02-15 12:10:32 +010012#include <fdtdec.h>
Michal Simek251ed2c2012-07-10 10:31:31 +020013#include <malloc.h>
Michal Simek922ce202007-03-11 13:48:24 +010014#include <asm/microblaze_intc.h>
Michal Simek77a1e242007-05-07 17:22:25 +020015#include <asm/asm.h>
Michal Simek922ce202007-03-11 13:48:24 +010016
Michal Simekcf848852016-02-15 12:10:32 +010017DECLARE_GLOBAL_DATA_PTR;
18
Michal Simek8b4bb3a2012-06-29 13:27:28 +020019void enable_interrupts(void)
wdenk12490652004-04-18 21:13:41 +000020{
Michal Simek056dbe82015-01-26 15:25:32 +010021 debug("Enable interrupts for the whole CPU\n");
Michal Simek98c19792007-05-07 23:58:31 +020022 MSRSET(0x2);
wdenk12490652004-04-18 21:13:41 +000023}
24
Michal Simek8b4bb3a2012-06-29 13:27:28 +020025int disable_interrupts(void)
wdenk12490652004-04-18 21:13:41 +000026{
Michal Simekc94f95e2010-12-21 08:30:39 +010027 unsigned int msr;
28
29 MFS(msr, rmsr);
Michal Simek98c19792007-05-07 23:58:31 +020030 MSRCLR(0x2);
Michal Simekc94f95e2010-12-21 08:30:39 +010031 return (msr & 0x2) != 0;
wdenk12490652004-04-18 21:13:41 +000032}
Michal Simek922ce202007-03-11 13:48:24 +010033
Michal Simek251ed2c2012-07-10 10:31:31 +020034static struct irq_action *vecs;
35static u32 irq_no;
Michal Simek922ce202007-03-11 13:48:24 +010036
37/* mapping structure to interrupt controller */
Michal Simek251ed2c2012-07-10 10:31:31 +020038microblaze_intc_t *intc;
Michal Simek922ce202007-03-11 13:48:24 +010039
40/* default handler */
Michal Simek251ed2c2012-07-10 10:31:31 +020041static void def_hdlr(void)
Michal Simek922ce202007-03-11 13:48:24 +010042{
Michal Simek8b4bb3a2012-06-29 13:27:28 +020043 puts("def_hdlr\n");
Michal Simek922ce202007-03-11 13:48:24 +010044}
45
Michal Simek251ed2c2012-07-10 10:31:31 +020046static void enable_one_interrupt(int irq)
Michal Simek922ce202007-03-11 13:48:24 +010047{
48 int mask;
49 int offset = 1;
Michal Simek8b4bb3a2012-06-29 13:27:28 +020050
Michal Simek922ce202007-03-11 13:48:24 +010051 offset <<= irq;
52 mask = intc->ier;
53 intc->ier = (mask | offset);
Michal Simekbaf51752015-01-26 14:37:52 +010054
55 debug("Enable one interrupt irq %x - mask %x,ier %x\n", offset, mask,
56 intc->ier);
57 debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
58 intc->iar, intc->mer);
Michal Simek922ce202007-03-11 13:48:24 +010059}
60
Michal Simek251ed2c2012-07-10 10:31:31 +020061static void disable_one_interrupt(int irq)
Michal Simek922ce202007-03-11 13:48:24 +010062{
63 int mask;
64 int offset = 1;
Michal Simek8b4bb3a2012-06-29 13:27:28 +020065
Michal Simek922ce202007-03-11 13:48:24 +010066 offset <<= irq;
67 mask = intc->ier;
68 intc->ier = (mask & ~offset);
Michal Simekbaf51752015-01-26 14:37:52 +010069
70 debug("Disable one interrupt irq %x - mask %x,ier %x\n", irq, mask,
71 intc->ier);
72 debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
73 intc->iar, intc->mer);
Michal Simek922ce202007-03-11 13:48:24 +010074}
75
Michal Simeke76a06e2012-06-29 14:21:52 +020076int install_interrupt_handler(int irq, interrupt_handler_t *hdlr, void *arg)
Michal Simek922ce202007-03-11 13:48:24 +010077{
78 struct irq_action *act;
Michal Simek8b4bb3a2012-06-29 13:27:28 +020079
Michal Simek922ce202007-03-11 13:48:24 +010080 /* irq out of range */
Michal Simek251ed2c2012-07-10 10:31:31 +020081 if ((irq < 0) || (irq > irq_no)) {
Michal Simek8b4bb3a2012-06-29 13:27:28 +020082 puts("IRQ out of range\n");
Michal Simeke76a06e2012-06-29 14:21:52 +020083 return -1;
Michal Simek922ce202007-03-11 13:48:24 +010084 }
85 act = &vecs[irq];
86 if (hdlr) { /* enable */
87 act->handler = hdlr;
88 act->arg = arg;
89 act->count = 0;
Michal Simek5e6c7472015-01-26 14:39:22 +010090 enable_one_interrupt(irq);
Michal Simeke76a06e2012-06-29 14:21:52 +020091 return 0;
Michal Simek922ce202007-03-11 13:48:24 +010092 }
Michal Simeke76a06e2012-06-29 14:21:52 +020093
94 /* Disable */
Michal Simek5e6c7472015-01-26 14:39:22 +010095 act->handler = (interrupt_handler_t *)def_hdlr;
Michal Simeke76a06e2012-06-29 14:21:52 +020096 act->arg = (void *)irq;
97 disable_one_interrupt(irq);
98 return 1;
Michal Simek922ce202007-03-11 13:48:24 +010099}
100
101/* initialization interrupt controller - hardware */
Michal Simek251ed2c2012-07-10 10:31:31 +0200102static void intc_init(void)
Michal Simek922ce202007-03-11 13:48:24 +0100103{
104 intc->mer = 0;
105 intc->ier = 0;
106 intc->iar = 0xFFFFFFFF;
107 /* XIntc_Start - hw_interrupt enable and all interrupt enable */
108 intc->mer = 0x3;
Michal Simekbaf51752015-01-26 14:37:52 +0100109
110 debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
111 intc->iar, intc->mer);
Michal Simek922ce202007-03-11 13:48:24 +0100112}
113
Michal Simekb78df3d2015-01-27 12:44:12 +0100114int interrupt_init(void)
Michal Simek922ce202007-03-11 13:48:24 +0100115{
116 int i;
Michal Simekcf848852016-02-15 12:10:32 +0100117 const void *blob = gd->fdt_blob;
118 int node = 0;
119
120 debug("INTC: Initialization\n");
121
122 node = fdt_node_offset_by_compatible(blob, node,
123 "xlnx,xps-intc-1.00.a");
124 if (node != -1) {
125 fdt_addr_t base = fdtdec_get_addr(blob, node, "reg");
126 if (base == FDT_ADDR_T_NONE)
127 return -1;
128
129 debug("INTC: Base addr %lx\n", base);
130 intc = (microblaze_intc_t *)base;
131 irq_no = fdtdec_get_int(blob, node, "xlnx,num-intr-inputs", 0);
132 debug("INTC: IRQ NO %x\n", irq_no);
133 } else {
134 return node;
135 }
Michal Simekc3d51b92016-02-15 13:44:19 +0100136
Michal Simek251ed2c2012-07-10 10:31:31 +0200137 if (irq_no) {
138 vecs = calloc(1, sizeof(struct irq_action) * irq_no);
139 if (vecs == NULL) {
140 puts("Interrupt vector allocation failed\n");
141 return -1;
142 }
143
144 /* initialize irq list */
145 for (i = 0; i < irq_no; i++) {
Michal Simek5e6c7472015-01-26 14:39:22 +0100146 vecs[i].handler = (interrupt_handler_t *)def_hdlr;
Michal Simek251ed2c2012-07-10 10:31:31 +0200147 vecs[i].arg = (void *)i;
148 vecs[i].count = 0;
149 }
150 /* initialize intc controller */
151 intc_init();
152 enable_interrupts();
153 } else {
154 puts("Undefined interrupt controller\n");
Michal Simek922ce202007-03-11 13:48:24 +0100155 }
Michal Simek922ce202007-03-11 13:48:24 +0100156 return 0;
157}
158
Michal Simek8b4bb3a2012-06-29 13:27:28 +0200159void interrupt_handler(void)
Michal Simek922ce202007-03-11 13:48:24 +0100160{
Michal Simek2c004492010-04-16 11:51:59 +0200161 int irqs = intc->ivr; /* find active interrupt */
162 int mask = 1;
Michal Simek77a1e242007-05-07 17:22:25 +0200163 int value;
Michal Simek2c004492010-04-16 11:51:59 +0200164 struct irq_action *act = vecs + irqs;
165
Michal Simekbaf51752015-01-26 14:37:52 +0100166 debug("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
167 intc->iar, intc->mer);
168#ifdef DEBUG
169 R14(value);
Michal Simek922ce202007-03-11 13:48:24 +0100170#endif
Michal Simekbaf51752015-01-26 14:37:52 +0100171 debug("Interrupt handler on %x line, r14 %x\n", irqs, value);
172
173 debug("Jumping to interrupt handler rutine addr %x,count %x,arg %x\n",
174 (u32)act->handler, act->count, (u32)act->arg);
Michal Simek5e6c7472015-01-26 14:39:22 +0100175 act->handler(act->arg);
Michal Simek2c004492010-04-16 11:51:59 +0200176 act->count++;
Michal Simek77a1e242007-05-07 17:22:25 +0200177
Stephan Linz5db50d62012-02-22 19:12:43 +0100178 intc->iar = mask << irqs;
179
Michal Simekbaf51752015-01-26 14:37:52 +0100180 debug("Dump INTC reg, isr %x, ier %x, iar %x, mer %x\n", intc->isr,
181 intc->ier, intc->iar, intc->mer);
182#ifdef DEBUG
Michal Simek77a1e242007-05-07 17:22:25 +0200183 R14(value);
Michal Simek922ce202007-03-11 13:48:24 +0100184#endif
Michal Simekbaf51752015-01-26 14:37:52 +0100185 debug("Interrupt handler on %x line, r14 %x\n", irqs, value);
Michal Simek922ce202007-03-11 13:48:24 +0100186}
Michal Simek922ce202007-03-11 13:48:24 +0100187
Jon Loeliger526e5ce2007-07-09 19:06:00 -0500188#if defined(CONFIG_CMD_IRQ)
Michal Simek251ed2c2012-07-10 10:31:31 +0200189int do_irqinfo(cmd_tbl_t *cmdtp, int flag, int argc, const char *argv[])
Michal Simek922ce202007-03-11 13:48:24 +0100190{
191 int i;
192 struct irq_action *act = vecs;
193
Michal Simek251ed2c2012-07-10 10:31:31 +0200194 if (irq_no) {
195 puts("\nInterrupt-Information:\n\n"
196 "Nr Routine Arg Count\n"
197 "-----------------------------\n");
Michal Simek922ce202007-03-11 13:48:24 +0100198
Michal Simek251ed2c2012-07-10 10:31:31 +0200199 for (i = 0; i < irq_no; i++) {
Michal Simek5e6c7472015-01-26 14:39:22 +0100200 if (act->handler != (interrupt_handler_t *)def_hdlr) {
Michal Simek251ed2c2012-07-10 10:31:31 +0200201 printf("%02d %08x %08x %d\n", i,
Michal Simek5e6c7472015-01-26 14:39:22 +0100202 (int)act->handler, (int)act->arg,
203 act->count);
Michal Simek251ed2c2012-07-10 10:31:31 +0200204 }
205 act++;
Michal Simek922ce202007-03-11 13:48:24 +0100206 }
Michal Simek251ed2c2012-07-10 10:31:31 +0200207 puts("\n");
208 } else {
209 puts("Undefined interrupt controller\n");
Michal Simek922ce202007-03-11 13:48:24 +0100210 }
Michal Simek251ed2c2012-07-10 10:31:31 +0200211 return 0;
Michal Simek922ce202007-03-11 13:48:24 +0100212}
213#endif