blob: 7f2ee64ca0af1c8de5c3993fb224b7c232242998 [file] [log] [blame]
wdenk507bbe32004-04-18 21:13:41 +00001/*
Michal Simekcfc67112007-03-11 13:48:24 +01002 * (C) Copyright 2007 Michal Simek
wdenk507bbe32004-04-18 21:13:41 +00003 * (C) Copyright 2004 Atmark Techno, Inc.
4 *
Michal Simekcfc67112007-03-11 13:48:24 +01005 * Michal SIMEK <monstr@monstr.eu>
wdenk507bbe32004-04-18 21:13:41 +00006 * Yasushi SHOJI <yashi@atmark-techno.com>
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Michal Simekcfc67112007-03-11 13:48:24 +010018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenk507bbe32004-04-18 21:13:41 +000019 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
Michal Simekcfc67112007-03-11 13:48:24 +010027#include <common.h>
28#include <command.h>
Michal Simek575a3d22012-07-10 10:31:31 +020029#include <malloc.h>
Michal Simekcfc67112007-03-11 13:48:24 +010030#include <asm/microblaze_intc.h>
Michal Simek42efed62007-05-07 17:22:25 +020031#include <asm/asm.h>
Michal Simekcfc67112007-03-11 13:48:24 +010032
33#undef DEBUG_INT
34
Michal Simek26e6da82012-06-29 13:27:28 +020035void enable_interrupts(void)
wdenk507bbe32004-04-18 21:13:41 +000036{
Michal Simekfb05f6d2007-05-07 23:58:31 +020037 MSRSET(0x2);
wdenk507bbe32004-04-18 21:13:41 +000038}
39
Michal Simek26e6da82012-06-29 13:27:28 +020040int disable_interrupts(void)
wdenk507bbe32004-04-18 21:13:41 +000041{
Michal Simek68e99e52010-12-21 08:30:39 +010042 unsigned int msr;
43
44 MFS(msr, rmsr);
Michal Simekfb05f6d2007-05-07 23:58:31 +020045 MSRCLR(0x2);
Michal Simek68e99e52010-12-21 08:30:39 +010046 return (msr & 0x2) != 0;
wdenk507bbe32004-04-18 21:13:41 +000047}
Michal Simekcfc67112007-03-11 13:48:24 +010048
Michal Simek575a3d22012-07-10 10:31:31 +020049static struct irq_action *vecs;
50static u32 irq_no;
Michal Simekcfc67112007-03-11 13:48:24 +010051
52/* mapping structure to interrupt controller */
Michal Simek575a3d22012-07-10 10:31:31 +020053microblaze_intc_t *intc;
Michal Simekcfc67112007-03-11 13:48:24 +010054
55/* default handler */
Michal Simek575a3d22012-07-10 10:31:31 +020056static void def_hdlr(void)
Michal Simekcfc67112007-03-11 13:48:24 +010057{
Michal Simek26e6da82012-06-29 13:27:28 +020058 puts("def_hdlr\n");
Michal Simekcfc67112007-03-11 13:48:24 +010059}
60
Michal Simek575a3d22012-07-10 10:31:31 +020061static void enable_one_interrupt(int irq)
Michal Simekcfc67112007-03-11 13:48:24 +010062{
63 int mask;
64 int offset = 1;
Michal Simek26e6da82012-06-29 13:27:28 +020065
Michal Simekcfc67112007-03-11 13:48:24 +010066 offset <<= irq;
67 mask = intc->ier;
68 intc->ier = (mask | offset);
69#ifdef DEBUG_INT
Michal Simek26e6da82012-06-29 13:27:28 +020070 printf("Enable one interrupt irq %x - mask %x,ier %x\n", offset, mask,
Michal Simekcfc67112007-03-11 13:48:24 +010071 intc->ier);
Michal Simek26e6da82012-06-29 13:27:28 +020072 printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
Michal Simekcfc67112007-03-11 13:48:24 +010073 intc->iar, intc->mer);
74#endif
75}
76
Michal Simek575a3d22012-07-10 10:31:31 +020077static void disable_one_interrupt(int irq)
Michal Simekcfc67112007-03-11 13:48:24 +010078{
79 int mask;
80 int offset = 1;
Michal Simek26e6da82012-06-29 13:27:28 +020081
Michal Simekcfc67112007-03-11 13:48:24 +010082 offset <<= irq;
83 mask = intc->ier;
84 intc->ier = (mask & ~offset);
85#ifdef DEBUG_INT
Michal Simek26e6da82012-06-29 13:27:28 +020086 printf("Disable one interrupt irq %x - mask %x,ier %x\n", irq, mask,
Michal Simekcfc67112007-03-11 13:48:24 +010087 intc->ier);
Michal Simek26e6da82012-06-29 13:27:28 +020088 printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
Michal Simekcfc67112007-03-11 13:48:24 +010089 intc->iar, intc->mer);
90#endif
91}
92
Michal Simek87069082012-06-29 14:21:52 +020093int install_interrupt_handler(int irq, interrupt_handler_t *hdlr, void *arg)
Michal Simekcfc67112007-03-11 13:48:24 +010094{
95 struct irq_action *act;
Michal Simek26e6da82012-06-29 13:27:28 +020096
Michal Simekcfc67112007-03-11 13:48:24 +010097 /* irq out of range */
Michal Simek575a3d22012-07-10 10:31:31 +020098 if ((irq < 0) || (irq > irq_no)) {
Michal Simek26e6da82012-06-29 13:27:28 +020099 puts("IRQ out of range\n");
Michal Simek87069082012-06-29 14:21:52 +0200100 return -1;
Michal Simekcfc67112007-03-11 13:48:24 +0100101 }
102 act = &vecs[irq];
103 if (hdlr) { /* enable */
104 act->handler = hdlr;
105 act->arg = arg;
106 act->count = 0;
107 enable_one_interrupt (irq);
Michal Simek87069082012-06-29 14:21:52 +0200108 return 0;
Michal Simekcfc67112007-03-11 13:48:24 +0100109 }
Michal Simek87069082012-06-29 14:21:52 +0200110
111 /* Disable */
112 act->handler = (interrupt_handler_t *) def_hdlr;
113 act->arg = (void *)irq;
114 disable_one_interrupt(irq);
115 return 1;
Michal Simekcfc67112007-03-11 13:48:24 +0100116}
117
118/* initialization interrupt controller - hardware */
Michal Simek575a3d22012-07-10 10:31:31 +0200119static void intc_init(void)
Michal Simekcfc67112007-03-11 13:48:24 +0100120{
121 intc->mer = 0;
122 intc->ier = 0;
123 intc->iar = 0xFFFFFFFF;
124 /* XIntc_Start - hw_interrupt enable and all interrupt enable */
125 intc->mer = 0x3;
126#ifdef DEBUG_INT
Michal Simek26e6da82012-06-29 13:27:28 +0200127 printf("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
Michal Simekcfc67112007-03-11 13:48:24 +0100128 intc->iar, intc->mer);
129#endif
130}
131
Michal Simek575a3d22012-07-10 10:31:31 +0200132int interrupts_init(void)
Michal Simekcfc67112007-03-11 13:48:24 +0100133{
134 int i;
Michal Simek575a3d22012-07-10 10:31:31 +0200135
136#if defined(CONFIG_SYS_INTC_0_ADDR) && defined(CONFIG_SYS_INTC_0_NUM)
137 intc = (microblaze_intc_t *) (CONFIG_SYS_INTC_0_ADDR);
138 irq_no = CONFIG_SYS_INTC_0_NUM;
139#endif
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++) {
149 vecs[i].handler = (interrupt_handler_t *) def_hdlr;
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 Simekcfc67112007-03-11 13:48:24 +0100158 }
Michal Simekcfc67112007-03-11 13:48:24 +0100159 return 0;
160}
161
Michal Simek26e6da82012-06-29 13:27:28 +0200162void interrupt_handler(void)
Michal Simekcfc67112007-03-11 13:48:24 +0100163{
Michal Simek8125c982010-04-16 11:51:59 +0200164 int irqs = intc->ivr; /* find active interrupt */
165 int mask = 1;
Michal Simekcfc67112007-03-11 13:48:24 +0100166#ifdef DEBUG_INT
Michal Simek42efed62007-05-07 17:22:25 +0200167 int value;
Michal Simekcfc67112007-03-11 13:48:24 +0100168 printf ("INTC isr %x, ier %x, iar %x, mer %x\n", intc->isr, intc->ier,
169 intc->iar, intc->mer);
Michal Simek42efed62007-05-07 17:22:25 +0200170 R14(value);
Michal Simekcfc67112007-03-11 13:48:24 +0100171 printf ("Interrupt handler on %x line, r14 %x\n", irqs, value);
172#endif
Michal Simek8125c982010-04-16 11:51:59 +0200173 struct irq_action *act = vecs + irqs;
174
Michal Simekcfc67112007-03-11 13:48:24 +0100175#ifdef DEBUG_INT
Michal Simek8125c982010-04-16 11:51:59 +0200176 printf
177 ("Jumping to interrupt handler rutine addr %x,count %x,arg %x\n",
178 act->handler, act->count, act->arg);
Michal Simekcfc67112007-03-11 13:48:24 +0100179#endif
Michal Simek8125c982010-04-16 11:51:59 +0200180 act->handler (act->arg);
181 act->count++;
Michal Simek42efed62007-05-07 17:22:25 +0200182
Stephan Linz0f883262012-02-22 19:12:43 +0100183 intc->iar = mask << irqs;
184
Michal Simek42efed62007-05-07 17:22:25 +0200185#ifdef DEBUG_INT
Michal Simekcfc67112007-03-11 13:48:24 +0100186 printf ("Dump INTC reg, isr %x, ier %x, iar %x, mer %x\n", intc->isr,
187 intc->ier, intc->iar, intc->mer);
Michal Simek42efed62007-05-07 17:22:25 +0200188 R14(value);
189 printf ("Interrupt handler on %x line, r14 %x\n", irqs, value);
Michal Simekcfc67112007-03-11 13:48:24 +0100190#endif
191}
Michal Simekcfc67112007-03-11 13:48:24 +0100192
Jon Loeliger44312832007-07-09 19:06:00 -0500193#if defined(CONFIG_CMD_IRQ)
Michal Simek575a3d22012-07-10 10:31:31 +0200194int do_irqinfo(cmd_tbl_t *cmdtp, int flag, int argc, const char *argv[])
Michal Simekcfc67112007-03-11 13:48:24 +0100195{
196 int i;
197 struct irq_action *act = vecs;
198
Michal Simek575a3d22012-07-10 10:31:31 +0200199 if (irq_no) {
200 puts("\nInterrupt-Information:\n\n"
201 "Nr Routine Arg Count\n"
202 "-----------------------------\n");
Michal Simekcfc67112007-03-11 13:48:24 +0100203
Michal Simek575a3d22012-07-10 10:31:31 +0200204 for (i = 0; i < irq_no; i++) {
205 if (act->handler != (interrupt_handler_t *) def_hdlr) {
206 printf("%02d %08x %08x %d\n", i,
207 (int)act->handler, (int)act->arg,
208 act->count);
209 }
210 act++;
Michal Simekcfc67112007-03-11 13:48:24 +0100211 }
Michal Simek575a3d22012-07-10 10:31:31 +0200212 puts("\n");
213 } else {
214 puts("Undefined interrupt controller\n");
Michal Simekcfc67112007-03-11 13:48:24 +0100215 }
Michal Simek575a3d22012-07-10 10:31:31 +0200216 return 0;
Michal Simekcfc67112007-03-11 13:48:24 +0100217}
Jon Loeliger44312832007-07-09 19:06:00 -0500218#endif