Merge branch 'mpc86xx'
[oweals/u-boot.git] / cpu / ixp / interrupts.c
1 /*
2  * (C) Copyright 2006
3  * Stefan Roese, DENX Software Engineering, sr@denx.de.
4  *
5  * (C) Copyright 2002
6  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Marius Groeger <mgroeger@sysgo.de>
8  *
9  * (C) Copyright 2002
10  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11  * Alex Zuepke <azu@sysgo.de>
12  *
13  * See file CREDITS for list of people who contributed to this
14  * project.
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License as
18  * published by the Free Software Foundation; either version 2 of
19  * the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29  * MA 02111-1307 USA
30  */
31
32 #include <common.h>
33 #include <asm/arch/ixp425.h>
34
35 #ifdef CONFIG_USE_IRQ
36 /*
37  * When interrupts are enabled, use timer 2 for time/delay generation...
38  */
39
40 #define FREQ            66666666
41 #define CLOCK_TICK_RATE (((FREQ / CFG_HZ & ~IXP425_OST_RELOAD_MASK) + 1) * CFG_HZ)
42 #define LATCH           ((CLOCK_TICK_RATE + CFG_HZ/2) / CFG_HZ) /* For divider */
43
44 struct _irq_handler {
45         void                *m_data;
46         void (*m_func)( void *data);
47 };
48
49 static struct _irq_handler IRQ_HANDLER[N_IRQS];
50
51 static volatile ulong timestamp;
52
53 /* enable IRQ/FIQ interrupts */
54 void enable_interrupts(void)
55 {
56         unsigned long temp;
57         __asm__ __volatile__("mrs %0, cpsr\n"
58                              "bic %0, %0, #0x80\n"
59                              "msr cpsr_c, %0"
60                              : "=r" (temp)
61                              :
62                              : "memory");
63 }
64
65 /*
66  * disable IRQ/FIQ interrupts
67  * returns true if interrupts had been enabled before we disabled them
68  */
69 int disable_interrupts(void)
70 {
71         unsigned long old,temp;
72         __asm__ __volatile__("mrs %0, cpsr\n"
73                              "orr %1, %0, #0x80\n"
74                              "msr cpsr_c, %1"
75                              : "=r" (old), "=r" (temp)
76                              :
77                              : "memory");
78         return (old & 0x80) == 0;
79 }
80
81 static void default_isr(void *data)
82 {
83         printf("default_isr():  called for IRQ %d, Interrupt Status=%x PR=%x\n",
84                (int)data, *IXP425_ICIP, *IXP425_ICIH);
85 }
86
87 static int next_irq(void)
88 {
89         return (((*IXP425_ICIH & 0x000000fc) >> 2) - 1);
90 }
91
92 static void timer_isr(void *data)
93 {
94         unsigned int *pTime = (unsigned int *)data;
95
96         (*pTime)++;
97
98         /*
99          * Reset IRQ source
100          */
101         *IXP425_OSST = IXP425_OSST_TIMER_2_PEND;
102 }
103
104 ulong get_timer (ulong base)
105 {
106         return timestamp - base;
107 }
108
109 void reset_timer (void)
110 {
111         timestamp = 0;
112 }
113
114 #else /* #ifdef CONFIG_USE_IRQ */
115 void enable_interrupts (void)
116 {
117         return;
118 }
119 int disable_interrupts (void)
120 {
121         return 0;
122 }
123 #endif /* #ifdef CONFIG_USE_IRQ */
124
125 void bad_mode (void)
126 {
127         panic ("Resetting CPU ...\n");
128         reset_cpu (0);
129 }
130
131 void show_regs (struct pt_regs *regs)
132 {
133         unsigned long flags;
134         const char *processor_modes[] = {
135         "USER_26",      "FIQ_26",       "IRQ_26",       "SVC_26",
136         "UK4_26",       "UK5_26",       "UK6_26",       "UK7_26",
137         "UK8_26",       "UK9_26",       "UK10_26",      "UK11_26",
138         "UK12_26",      "UK13_26",      "UK14_26",      "UK15_26",
139         "USER_32",      "FIQ_32",       "IRQ_32",       "SVC_32",
140         "UK4_32",       "UK5_32",       "UK6_32",       "ABT_32",
141         "UK8_32",       "UK9_32",       "UK10_32",      "UND_32",
142         "UK12_32",      "UK13_32",      "UK14_32",      "SYS_32"
143         };
144
145         flags = condition_codes (regs);
146
147         printf ("pc : [<%08lx>]    lr : [<%08lx>]\n"
148                 "sp : %08lx  ip : %08lx  fp : %08lx\n",
149                 instruction_pointer (regs),
150                 regs->ARM_lr, regs->ARM_sp, regs->ARM_ip, regs->ARM_fp);
151         printf ("r10: %08lx  r9 : %08lx  r8 : %08lx\n",
152                 regs->ARM_r10, regs->ARM_r9, regs->ARM_r8);
153         printf ("r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
154                 regs->ARM_r7, regs->ARM_r6, regs->ARM_r5, regs->ARM_r4);
155         printf ("r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
156                 regs->ARM_r3, regs->ARM_r2, regs->ARM_r1, regs->ARM_r0);
157         printf ("Flags: %c%c%c%c",
158                 flags & CC_N_BIT ? 'N' : 'n',
159                 flags & CC_Z_BIT ? 'Z' : 'z',
160                 flags & CC_C_BIT ? 'C' : 'c', flags & CC_V_BIT ? 'V' : 'v');
161         printf ("  IRQs %s  FIQs %s  Mode %s%s\n",
162                 interrupts_enabled (regs) ? "on" : "off",
163                 fast_interrupts_enabled (regs) ? "on" : "off",
164                 processor_modes[processor_mode (regs)],
165                 thumb_mode (regs) ? " (T)" : "");
166 }
167
168 void do_undefined_instruction (struct pt_regs *pt_regs)
169 {
170         printf ("undefined instruction\n");
171         show_regs (pt_regs);
172         bad_mode ();
173 }
174
175 void do_software_interrupt (struct pt_regs *pt_regs)
176 {
177         printf ("software interrupt\n");
178         show_regs (pt_regs);
179         bad_mode ();
180 }
181
182 void do_prefetch_abort (struct pt_regs *pt_regs)
183 {
184         printf ("prefetch abort\n");
185         show_regs (pt_regs);
186         bad_mode ();
187 }
188
189 void do_data_abort (struct pt_regs *pt_regs)
190 {
191         printf ("data abort\n");
192         show_regs (pt_regs);
193         bad_mode ();
194 }
195
196 void do_not_used (struct pt_regs *pt_regs)
197 {
198         printf ("not used\n");
199         show_regs (pt_regs);
200         bad_mode ();
201 }
202
203 void do_fiq (struct pt_regs *pt_regs)
204 {
205         printf ("fast interrupt request\n");
206         show_regs (pt_regs);
207         printf("IRQ=%08lx FIQ=%08lx\n", *IXP425_ICIH, *IXP425_ICFH);
208 }
209
210 void do_irq (struct pt_regs *pt_regs)
211 {
212 #ifdef CONFIG_USE_IRQ
213         int irq = next_irq();
214
215         IRQ_HANDLER[irq].m_func(IRQ_HANDLER[irq].m_data);
216 #else
217         printf ("interrupt request\n");
218         show_regs (pt_regs);
219         bad_mode ();
220 #endif
221 }
222
223 int interrupt_init (void)
224 {
225 #ifdef CONFIG_USE_IRQ
226         int i;
227
228         /* install default interrupt handlers */
229         for (i = 0; i < N_IRQS; i++) {
230                 IRQ_HANDLER[i].m_data = (void *)i;
231                 IRQ_HANDLER[i].m_func = default_isr;
232         }
233
234         /* install interrupt handler for timer */
235         IRQ_HANDLER[IXP425_TIMER_2_IRQ].m_data = (void *)&timestamp;
236         IRQ_HANDLER[IXP425_TIMER_2_IRQ].m_func = timer_isr;
237
238         /* setup the Timer counter value */
239         *IXP425_OSRT2 = (LATCH & ~IXP425_OST_RELOAD_MASK) | IXP425_OST_ENABLE;
240
241         /* configure interrupts for IRQ mode */
242         *IXP425_ICLR = 0x00000000;
243
244         /* enable timer irq */
245         *IXP425_ICMR = (1 << IXP425_TIMER_2_IRQ);
246 #endif
247
248         return (0);
249 }