blob: 9364e2fa9c9b0fb1e810ad2d2f88a6cd7deafdd0 [file] [log] [blame]
wdenk12490652004-04-18 21:13:41 +00001/*
Michal Simek922ce202007-03-11 13:48:24 +01002 * (C) Copyright 2007 Michal Simek
wdenk12490652004-04-18 21:13:41 +00003 * (C) Copyright 2004 Atmark Techno, Inc.
4 *
Michal Simek922ce202007-03-11 13:48:24 +01005 * Michal SIMEK <monstr@monstr.eu>
wdenk12490652004-04-18 21:13:41 +00006 * Yasushi SHOJI <yashi@atmark-techno.com>
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk12490652004-04-18 21:13:41 +00009 */
10
Michal Simek922ce202007-03-11 13:48:24 +010011#include <common.h>
12#include <command.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
17#undef DEBUG_INT
18
Michal Simek8b4bb3a2012-06-29 13:27:28 +020019void enable_interrupts(void)
wdenk12490652004-04-18 21:13:41 +000020{
Michal Simek98c19792007-05-07 23:58:31 +020021 MSRSET(0x2);
wdenk12490652004-04-18 21:13:41 +000022}
23
Michal Simek8b4bb3a2012-06-29 13:27:28 +020024int disable_interrupts(void)
wdenk12490652004-04-18 21:13:41 +000025{
Michal Simekc94f95e2010-12-21 08:30:39 +010026 unsigned int msr;
27
28 MFS(msr, rmsr);
Michal Simek98c19792007-05-07 23:58:31 +020029 MSRCLR(0x2);
Michal Simekc94f95e2010-12-21 08:30:39 +010030 return (msr & 0x2) != 0;
wdenk12490652004-04-18 21:13:41 +000031}
Michal Simek922ce202007-03-11 13:48:24 +010032
Michal Simek251ed2c2012-07-10 10:31:31 +020033static struct irq_action *vecs;
34static u32 irq_no;
Michal Simek922ce202007-03-11 13:48:24 +010035
36/* mapping structure to interrupt controller */
Michal Simek251ed2c2012-07-10 10:31:31 +020037microblaze_intc_t *intc;
Michal Simek922ce202007-03-11 13:48:24 +010038
39/* default handler */
Michal Simek251ed2c2012-07-10 10:31:31 +020040static void def_hdlr(void)
Michal Simek922ce202007-03-11 13:48:24 +010041{
Michal Simek8b4bb3a2012-06-29 13:27:28 +020042 puts("def_hdlr\n");
Michal Simek922ce202007-03-11 13:48:24 +010043}
44
Michal Simek251ed2c2012-07-10 10:31:31 +020045static void enable_one_interrupt(int irq)
Michal Simek922ce202007-03-11 13:48:24 +010046{
47 int mask;
48 int offset = 1;
Michal Simek8b4bb3a2012-06-29 13:27:28 +020049
Michal Simek922ce202007-03-11 13:48:24 +010050 offset <<= irq;
51 mask = intc->ier;
52 intc->ier = (mask | offset);
53#ifdef DEBUG_INT
Michal Simek8b4bb3a2012-06-29 13:27:28 +020054 printf("Enable one interrupt irq %x - mask %x,ier %x\n", offset, mask,
Michal Simek922ce202007-03-11 13:48:24 +010055 intc->ier);
Michal Simek8b4bb3a2012-06-29 13:27:28 +020056 printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
Michal Simek922ce202007-03-11 13:48:24 +010057 intc->iar, intc->mer);
58#endif
59}
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);
69#ifdef DEBUG_INT
Michal Simek8b4bb3a2012-06-29 13:27:28 +020070 printf("Disable one interrupt irq %x - mask %x,ier %x\n", irq, mask,
Michal Simek922ce202007-03-11 13:48:24 +010071 intc->ier);
Michal Simek8b4bb3a2012-06-29 13:27:28 +020072 printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
Michal Simek922ce202007-03-11 13:48:24 +010073 intc->iar, intc->mer);
74#endif
75}
76
Michal Simeke76a06e2012-06-29 14:21:52 +020077int install_interrupt_handler(int irq, interrupt_handler_t *hdlr, void *arg)
Michal Simek922ce202007-03-11 13:48:24 +010078{
79 struct irq_action *act;
Michal Simek8b4bb3a2012-06-29 13:27:28 +020080
Michal Simek922ce202007-03-11 13:48:24 +010081 /* irq out of range */
Michal Simek251ed2c2012-07-10 10:31:31 +020082 if ((irq < 0) || (irq > irq_no)) {
Michal Simek8b4bb3a2012-06-29 13:27:28 +020083 puts("IRQ out of range\n");
Michal Simeke76a06e2012-06-29 14:21:52 +020084 return -1;
Michal Simek922ce202007-03-11 13:48:24 +010085 }
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 Simeke76a06e2012-06-29 14:21:52 +020092 return 0;
Michal Simek922ce202007-03-11 13:48:24 +010093 }
Michal Simeke76a06e2012-06-29 14:21:52 +020094
95 /* Disable */
96 act->handler = (interrupt_handler_t *) def_hdlr;
97 act->arg = (void *)irq;
98 disable_one_interrupt(irq);
99 return 1;
Michal Simek922ce202007-03-11 13:48:24 +0100100}
101
102/* initialization interrupt controller - hardware */
Michal Simek251ed2c2012-07-10 10:31:31 +0200103static void intc_init(void)
Michal Simek922ce202007-03-11 13:48:24 +0100104{
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 Simek8b4bb3a2012-06-29 13:27:28 +0200111 printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
Michal Simek922ce202007-03-11 13:48:24 +0100112 intc->iar, intc->mer);
113#endif
114}
115
Michal Simek251ed2c2012-07-10 10:31:31 +0200116int interrupts_init(void)
Michal Simek922ce202007-03-11 13:48:24 +0100117{
118 int i;
Michal Simek251ed2c2012-07-10 10:31:31 +0200119
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 Simek922ce202007-03-11 13:48:24 +0100142 }
Michal Simek922ce202007-03-11 13:48:24 +0100143 return 0;
144}
145
Michal Simek8b4bb3a2012-06-29 13:27:28 +0200146void interrupt_handler(void)
Michal Simek922ce202007-03-11 13:48:24 +0100147{
Michal Simek2c004492010-04-16 11:51:59 +0200148 int irqs = intc->ivr; /* find active interrupt */
149 int mask = 1;
Michal Simek922ce202007-03-11 13:48:24 +0100150#ifdef DEBUG_INT
Michal Simek77a1e242007-05-07 17:22:25 +0200151 int value;
Michal Simek922ce202007-03-11 13:48:24 +0100152 printf ("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
153 intc->iar, intc->mer);
Michal Simek77a1e242007-05-07 17:22:25 +0200154 R14(value);
Michal Simek922ce202007-03-11 13:48:24 +0100155 printf ("Interrupt handler on %x line, r14 %x\n", irqs, value);
156#endif
Michal Simek2c004492010-04-16 11:51:59 +0200157 struct irq_action *act = vecs + irqs;
158
Michal Simek922ce202007-03-11 13:48:24 +0100159#ifdef DEBUG_INT
Michal Simek2c004492010-04-16 11:51:59 +0200160 printf
161 ("Jumping to interrupt handler rutine addr %x,count %x,arg %x\n",
162 act->handler, act->count, act->arg);
Michal Simek922ce202007-03-11 13:48:24 +0100163#endif
Michal Simek2c004492010-04-16 11:51:59 +0200164 act->handler (act->arg);
165 act->count++;
Michal Simek77a1e242007-05-07 17:22:25 +0200166
Stephan Linz5db50d62012-02-22 19:12:43 +0100167 intc->iar = mask << irqs;
168
Michal Simek77a1e242007-05-07 17:22:25 +0200169#ifdef DEBUG_INT
Michal Simek922ce202007-03-11 13:48:24 +0100170 printf ("Dump INTC reg, isr %x, ier %x, iar %x, mer %x\n", intc->isr,
171 intc->ier, intc->iar, intc->mer);
Michal Simek77a1e242007-05-07 17:22:25 +0200172 R14(value);
173 printf ("Interrupt handler on %x line, r14 %x\n", irqs, value);
Michal Simek922ce202007-03-11 13:48:24 +0100174#endif
175}
Michal Simek922ce202007-03-11 13:48:24 +0100176
Jon Loeliger526e5ce2007-07-09 19:06:00 -0500177#if defined(CONFIG_CMD_IRQ)
Michal Simek251ed2c2012-07-10 10:31:31 +0200178int do_irqinfo(cmd_tbl_t *cmdtp, int flag, int argc, const char *argv[])
Michal Simek922ce202007-03-11 13:48:24 +0100179{
180 int i;
181 struct irq_action *act = vecs;
182
Michal Simek251ed2c2012-07-10 10:31:31 +0200183 if (irq_no) {
184 puts("\nInterrupt-Information:\n\n"
185 "Nr Routine Arg Count\n"
186 "-----------------------------\n");
Michal Simek922ce202007-03-11 13:48:24 +0100187
Michal Simek251ed2c2012-07-10 10:31:31 +0200188 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 Simek922ce202007-03-11 13:48:24 +0100195 }
Michal Simek251ed2c2012-07-10 10:31:31 +0200196 puts("\n");
197 } else {
198 puts("Undefined interrupt controller\n");
Michal Simek922ce202007-03-11 13:48:24 +0100199 }
Michal Simek251ed2c2012-07-10 10:31:31 +0200200 return 0;
Michal Simek922ce202007-03-11 13:48:24 +0100201}
202#endif