]> git.sur5r.net Git - u-boot/blob - cpu/mpc8xx/interrupts.c
Compile fixes
[u-boot] / cpu / mpc8xx / interrupts.c
1 /*
2  * (C) Copyright 2000-2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <watchdog.h>
26 #include <mpc8xx.h>
27 #include <mpc8xx_irq.h>
28 #include <asm/processor.h>
29 #include <commproc.h>
30
31 /************************************************************************/
32
33 unsigned decrementer_count;     /* count value for 1e6/HZ microseconds  */
34
35 /************************************************************************/
36
37 /*
38  * CPM interrupt vector functions.
39  */
40 struct interrupt_action {
41         interrupt_handler_t *handler;
42         void *arg;
43 };
44
45 static struct interrupt_action cpm_vecs[CPMVEC_NR];
46 static struct interrupt_action irq_vecs[NR_IRQS];
47
48 static void cpm_interrupt_init (void);
49 static void cpm_interrupt (void *regs);
50
51 /************************************************************************/
52
53 static __inline__ unsigned long get_msr (void)
54 {
55         unsigned long msr;
56
57         asm volatile ("mfmsr %0":"=r" (msr):);
58
59         return msr;
60 }
61
62 static __inline__ void set_msr (unsigned long msr)
63 {
64         asm volatile ("mtmsr %0"::"r" (msr));
65 }
66
67 static __inline__ unsigned long get_dec (void)
68 {
69         unsigned long val;
70
71         asm volatile ("mfdec %0":"=r" (val):);
72
73         return val;
74 }
75
76
77 static __inline__ void set_dec (unsigned long val)
78 {
79         asm volatile ("mtdec %0"::"r" (val));
80 }
81
82
83 void enable_interrupts (void)
84 {
85         set_msr (get_msr () | MSR_EE);
86 }
87
88 /* returns flag if MSR_EE was set before */
89 int disable_interrupts (void)
90 {
91         ulong msr = get_msr ();
92
93         set_msr (msr & ~MSR_EE);
94         return ((msr & MSR_EE) != 0);
95 }
96
97 /************************************************************************/
98
99 int interrupt_init (void)
100 {
101         volatile immap_t *immr = (immap_t *) CFG_IMMR;
102
103         decrementer_count = get_tbclk () / CFG_HZ;
104
105         /* disable all interrupts */
106         immr->im_siu_conf.sc_simask = 0;
107
108         /* Configure CPM interrupts */
109         cpm_interrupt_init ();
110
111         set_dec (decrementer_count);
112
113         set_msr (get_msr () | MSR_EE);
114
115         return (0);
116 }
117
118 /************************************************************************/
119
120 /*
121  * Handle external interrupts
122  */
123 void external_interrupt (struct pt_regs *regs)
124 {
125         volatile immap_t *immr = (immap_t *) CFG_IMMR;
126         int irq;
127         ulong simask, newmask;
128         ulong vec, v_bit;
129
130         /*
131          * read the SIVEC register and shift the bits down
132          * to get the irq number
133          */
134         vec = immr->im_siu_conf.sc_sivec;
135         irq = vec >> 26;
136         v_bit = 0x80000000UL >> irq;
137
138         /*
139          * Read Interrupt Mask Register and Mask Interrupts
140          */
141         simask = immr->im_siu_conf.sc_simask;
142         newmask = simask & (~(0xFFFF0000 >> irq));
143         immr->im_siu_conf.sc_simask = newmask;
144
145         if (!(irq & 0x1)) {             /* External Interrupt ?     */
146                 ulong siel;
147
148                 /*
149                  * Read Interrupt Edge/Level Register
150                  */
151                 siel = immr->im_siu_conf.sc_siel;
152
153                 if (siel & v_bit) {     /* edge triggered interrupt ?   */
154                         /*
155                          * Rewrite SIPEND Register to clear interrupt
156                          */
157                         immr->im_siu_conf.sc_sipend = v_bit;
158                 }
159         }
160
161         if (irq_vecs[irq].handler != NULL) {
162                 irq_vecs[irq].handler (irq_vecs[irq].arg);
163         } else {
164                 printf ("\nBogus External Interrupt IRQ %d Vector %ld\n",
165                                 irq, vec);
166                 /* turn off the bogus interrupt to avoid it from now */
167                 simask &= ~v_bit;
168         }
169         /*
170          * Re-Enable old Interrupt Mask
171          */
172         immr->im_siu_conf.sc_simask = simask;
173 }
174
175 /************************************************************************/
176
177 /*
178  * CPM interrupt handler
179  */
180 static void cpm_interrupt (void *regs)
181 {
182         volatile immap_t *immr = (immap_t *) CFG_IMMR;
183         uint vec;
184
185         /*
186          * Get the vector by setting the ACK bit
187          * and then reading the register.
188          */
189         immr->im_cpic.cpic_civr = 1;
190         vec = immr->im_cpic.cpic_civr;
191         vec >>= 11;
192
193         if (cpm_vecs[vec].handler != NULL) {
194                 (*cpm_vecs[vec].handler) (cpm_vecs[vec].arg);
195         } else {
196                 immr->im_cpic.cpic_cimr &= ~(1 << vec);
197                 printf ("Masking bogus CPM interrupt vector 0x%x\n", vec);
198         }
199         /*
200          * After servicing the interrupt,
201          * we have to remove the status indicator.
202          */
203         immr->im_cpic.cpic_cisr |= (1 << vec);
204 }
205
206 /*
207  * The CPM can generate the error interrupt when there is a race
208  * condition between generating and masking interrupts. All we have
209  * to do is ACK it and return. This is a no-op function so we don't
210  * need any special tests in the interrupt handler.
211  */
212 static void cpm_error_interrupt (void *dummy)
213 {
214 }
215
216 /************************************************************************/
217 /*
218  * Install and free an interrupt handler
219  */
220 void irq_install_handler (int vec, interrupt_handler_t * handler,
221                                                   void *arg)
222 {
223         volatile immap_t *immr = (immap_t *) CFG_IMMR;
224
225         if ((vec & CPMVEC_OFFSET) != 0) {
226                 /* CPM interrupt */
227                 vec &= 0xffff;
228                 if (cpm_vecs[vec].handler != NULL) {
229                         printf ("CPM interrupt 0x%x replacing 0x%x\n",
230                                 (uint) handler,
231                                 (uint) cpm_vecs[vec].handler);
232                 }
233                 cpm_vecs[vec].handler = handler;
234                 cpm_vecs[vec].arg = arg;
235                 immr->im_cpic.cpic_cimr |= (1 << vec);
236 #if 0
237                 printf ("Install CPM interrupt for vector %d ==> %p\n",
238                         vec, handler);
239 #endif
240         } else {
241                 /* SIU interrupt */
242                 if (irq_vecs[vec].handler != NULL) {
243                         printf ("SIU interrupt %d 0x%x replacing 0x%x\n",
244                                 vec,
245                                 (uint) handler,
246                                 (uint) cpm_vecs[vec].handler);
247                 }
248                 irq_vecs[vec].handler = handler;
249                 irq_vecs[vec].arg = arg;
250                 immr->im_siu_conf.sc_simask |= 1 << (31 - vec);
251 #if 0
252                 printf ("Install SIU interrupt for vector %d ==> %p\n",
253                         vec, handler);
254 #endif
255         }
256 }
257
258 void irq_free_handler (int vec)
259 {
260         volatile immap_t *immr = (immap_t *) CFG_IMMR;
261
262         if ((vec & CPMVEC_OFFSET) != 0) {
263                 /* CPM interrupt */
264                 vec &= 0xffff;
265 #if 0
266                 printf ("Free CPM interrupt for vector %d ==> %p\n",
267                         vec, cpm_vecs[vec].handler);
268 #endif
269                 immr->im_cpic.cpic_cimr &= ~(1 << vec);
270                 cpm_vecs[vec].handler = NULL;
271                 cpm_vecs[vec].arg = NULL;
272         } else {
273                 /* SIU interrupt */
274 #if 0
275                 printf ("Free CPM interrupt for vector %d ==> %p\n",
276                         vec, cpm_vecs[vec].handler);
277 #endif
278                 immr->im_siu_conf.sc_simask &= ~(1 << (31 - vec));
279                 irq_vecs[vec].handler = NULL;
280                 irq_vecs[vec].arg = NULL;
281         }
282 }
283
284 /************************************************************************/
285
286 static void cpm_interrupt_init (void)
287 {
288         volatile immap_t *immr = (immap_t *) CFG_IMMR;
289
290         /*
291          * Initialize the CPM interrupt controller.
292          */
293
294         immr->im_cpic.cpic_cicr =
295                 (CICR_SCD_SCC4 |
296                  CICR_SCC_SCC3 |
297                  CICR_SCB_SCC2 |
298                  CICR_SCA_SCC1) | ((CPM_INTERRUPT / 2) << 13) | CICR_HP_MASK;
299
300         immr->im_cpic.cpic_cimr = 0;
301
302         /*
303          * Install the error handler.
304          */
305         irq_install_handler (CPMVEC_ERROR, cpm_error_interrupt, NULL);
306
307         immr->im_cpic.cpic_cicr |= CICR_IEN;
308
309         /*
310          * Install the cpm interrupt handler
311          */
312         irq_install_handler (CPM_INTERRUPT, cpm_interrupt, NULL);
313 }
314
315 /************************************************************************/
316
317 volatile ulong timestamp = 0;
318
319 /*
320  * timer_interrupt - gets called when the decrementer overflows,
321  * with interrupts disabled.
322  * Trivial implementation - no need to be really accurate.
323  */
324 void timer_interrupt (struct pt_regs *regs)
325 {
326         volatile immap_t *immr = (immap_t *) CFG_IMMR;
327
328 #ifdef CONFIG_STATUS_LED
329         extern void status_led_tick (ulong);
330 #endif
331 #if 0
332         printf ("*** Timer Interrupt *** ");
333 #endif
334         /* Reset Timer Expired and Timers Interrupt Status */
335         immr->im_clkrstk.cark_plprcrk = KAPWR_KEY;
336         __asm__ ("nop");
337         immr->im_clkrst.car_plprcr |= PLPRCR_TEXPS | PLPRCR_TMIST;
338         /* Restore Decrementer Count */
339         set_dec (decrementer_count);
340
341         timestamp++;
342
343 #ifdef CONFIG_STATUS_LED
344         status_led_tick (timestamp);
345 #endif /* CONFIG_STATUS_LED */
346
347 #if defined(CONFIG_WATCHDOG) || defined(CFG_CMA_LCD_HEARTBEAT)
348
349         /*
350          * The shortest watchdog period of all boards (except LWMON)
351          * is approx. 1 sec, thus re-trigger watchdog at least
352          * every 500 ms = CFG_HZ / 2
353          */
354 #ifndef CONFIG_LWMON
355         if ((timestamp % (CFG_HZ / 2)) == 0) {
356 #else
357         if ((timestamp % (CFG_HZ / 20)) == 0) {
358 #endif
359
360 #if defined(CFG_CMA_LCD_HEARTBEAT)
361                 extern void lcd_heartbeat (void);
362
363                 lcd_heartbeat ();
364 #endif /* CFG_CMA_LCD_HEARTBEAT */
365
366 #if defined(CONFIG_WATCHDOG)
367                 reset_8xx_watchdog (immr);
368 #endif /* CONFIG_WATCHDOG */
369
370         }
371 #endif /* CONFIG_WATCHDOG || CFG_CMA_LCD_HEARTBEAT */
372 }
373
374 /************************************************************************/
375
376 void reset_timer (void)
377 {
378         timestamp = 0;
379 }
380
381 ulong get_timer (ulong base)
382 {
383         return (timestamp - base);
384 }
385
386 void set_timer (ulong t)
387 {
388         timestamp = t;
389 }
390
391 /************************************************************************/