ppc4xx: Add pci_pre_init() for 405 boards
[oweals/u-boot.git] / cpu / ppc4xx / traps.c
1 /*
2  * linux/arch/ppc/kernel/traps.c
3  *
4  * Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
5  *
6  * Modified by Cort Dougan (cort@cs.nmt.edu)
7  * and Paul Mackerras (paulus@cs.anu.edu.au)
8  *
9  * (C) Copyright 2000
10  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11  *
12  * See file CREDITS for list of people who contributed to this
13  * project.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License as
17  * published by the Free Software Foundation; either version 2 of
18  * the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28  * MA 02111-1307 USA
29  */
30
31 /*
32  * This file handles the architecture-dependent parts of hardware exceptions
33  */
34
35 #include <common.h>
36 #include <command.h>
37 #include <asm/processor.h>
38
39 DECLARE_GLOBAL_DATA_PTR;
40
41 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
42 int (*debugger_exception_handler)(struct pt_regs *) = 0;
43 #endif
44
45 /* Returns 0 if exception not found and fixup otherwise.  */
46 extern unsigned long search_exception_table(unsigned long);
47
48 /* THIS NEEDS CHANGING to use the board info structure.
49  */
50 #define END_OF_MEM      (gd->bd->bi_memstart + gd->bd->bi_memsize)
51
52 static __inline__ void set_tsr(unsigned long val)
53 {
54 #if defined(CONFIG_440)
55         asm volatile("mtspr 0x150, %0" : : "r" (val));
56 #else
57         asm volatile("mttsr %0" : : "r" (val));
58 #endif
59 }
60
61 static __inline__ unsigned long get_esr(void)
62 {
63         unsigned long val;
64
65 #if defined(CONFIG_440)
66         asm volatile("mfspr %0, 0x03e" : "=r" (val) :);
67 #else
68         asm volatile("mfesr %0" : "=r" (val) :);
69 #endif
70         return val;
71 }
72
73 #define ESR_MCI 0x80000000
74 #define ESR_PIL 0x08000000
75 #define ESR_PPR 0x04000000
76 #define ESR_PTR 0x02000000
77 #define ESR_DST 0x00800000
78 #define ESR_DIZ 0x00400000
79 #define ESR_U0F 0x00008000
80
81 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
82 extern void do_bedbug_breakpoint(struct pt_regs *);
83 #endif
84
85 /*
86  * Trap & Exception support
87  */
88
89 void
90 print_backtrace(unsigned long *sp)
91 {
92         int cnt = 0;
93         unsigned long i;
94
95         printf("Call backtrace: ");
96         while (sp) {
97                 if ((uint)sp > END_OF_MEM)
98                         break;
99
100                 i = sp[1];
101                 if (cnt++ % 7 == 0)
102                         printf("\n");
103                 printf("%08lX ", i);
104                 if (cnt > 32) break;
105                 sp = (unsigned long *)*sp;
106         }
107         printf("\n");
108 }
109
110 void show_regs(struct pt_regs * regs)
111 {
112         int i;
113
114         printf("NIP: %08lX XER: %08lX LR: %08lX REGS: %p TRAP: %04lx DEAR: %08lX\n",
115                regs->nip, regs->xer, regs->link, regs, regs->trap, regs->dar);
116         printf("MSR: %08lx EE: %01x PR: %01x FP: %01x ME: %01x IR/DR: %01x%01x\n",
117                regs->msr, regs->msr&MSR_EE ? 1 : 0, regs->msr&MSR_PR ? 1 : 0,
118                regs->msr & MSR_FP ? 1 : 0,regs->msr&MSR_ME ? 1 : 0,
119                regs->msr&MSR_IR ? 1 : 0,
120                regs->msr&MSR_DR ? 1 : 0);
121
122         printf("\n");
123         for (i = 0;  i < 32;  i++) {
124                 if ((i % 8) == 0) {
125                         printf("GPR%02d: ", i);
126                 }
127
128                 printf("%08lX ", regs->gpr[i]);
129                 if ((i % 8) == 7) {
130                         printf("\n");
131                 }
132         }
133 }
134
135
136 void
137 _exception(int signr, struct pt_regs *regs)
138 {
139         show_regs(regs);
140         print_backtrace((unsigned long *)regs->gpr[1]);
141         panic("Exception");
142 }
143
144 void
145 MachineCheckException(struct pt_regs *regs)
146 {
147         unsigned long fixup, val;
148
149         /* Probing PCI using config cycles cause this exception
150          * when a device is not present.  Catch it and return to
151          * the PCI exception handler.
152          */
153         if ((fixup = search_exception_table(regs->nip)) != 0) {
154                 regs->nip = fixup;
155                 return;
156         }
157
158 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
159         if (debugger_exception_handler && (*debugger_exception_handler)(regs))
160                 return;
161 #endif
162
163         printf("Machine Check Exception.\n");
164         printf("Caused by (from msr): ");
165         printf("regs %p ", regs);
166
167         val = get_esr();
168
169 #if !defined(CONFIG_440)
170         if (val& ESR_IMCP) {
171                 printf("Instruction");
172                 mtspr(ESR, val & ~ESR_IMCP);
173         } else {
174                 printf("Data");
175         }
176         printf(" machine check.\n");
177
178 #elif defined(CONFIG_440)
179         if (val& ESR_IMCP){
180                 printf("Instruction Synchronous Machine Check exception\n");
181                 mtspr(SPRN_ESR, val & ~ESR_IMCP);
182         } else {
183                 val = mfspr(MCSR);
184                 if (val & MCSR_IB)
185                         printf("Instruction Read PLB Error\n");
186                 if (val & MCSR_DRB)
187                         printf("Data Read PLB Error\n");
188                 if (val & MCSR_DWB)
189                         printf("Data Write PLB Error\n");
190                 if (val & MCSR_TLBP)
191                         printf("TLB Parity Error\n");
192                 if (val & MCSR_ICP){
193                         /*flush_instruction_cache(); */
194                         printf("I-Cache Parity Error\n");
195                 }
196                 if (val & MCSR_DCSP)
197                         printf("D-Cache Search Parity Error\n");
198                 if (val & MCSR_DCFP)
199                         printf("D-Cache Flush Parity Error\n");
200                 if (val & MCSR_IMPE)
201                         printf("Machine Check exception is imprecise\n");
202
203                 /* Clear MCSR */
204                 mtspr(SPRN_MCSR, val);
205         }
206 #endif
207         show_regs(regs);
208         print_backtrace((unsigned long *)regs->gpr[1]);
209         panic("machine check");
210 }
211
212 void
213 AlignmentException(struct pt_regs *regs)
214 {
215 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
216         if (debugger_exception_handler && (*debugger_exception_handler)(regs))
217                 return;
218 #endif
219
220         show_regs(regs);
221         print_backtrace((unsigned long *)regs->gpr[1]);
222         panic("Alignment Exception");
223 }
224
225 void
226 ProgramCheckException(struct pt_regs *regs)
227 {
228         long esr_val;
229
230 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
231         if (debugger_exception_handler && (*debugger_exception_handler)(regs))
232                 return;
233 #endif
234
235         show_regs(regs);
236
237         esr_val = get_esr();
238         if( esr_val & ESR_PIL )
239                 printf( "** Illegal Instruction **\n" );
240         else if( esr_val & ESR_PPR )
241                 printf( "** Privileged Instruction **\n" );
242         else if( esr_val & ESR_PTR )
243                 printf( "** Trap Instruction **\n" );
244
245         print_backtrace((unsigned long *)regs->gpr[1]);
246         panic("Program Check Exception");
247 }
248
249 void
250 DecrementerPITException(struct pt_regs *regs)
251 {
252         /*
253          * Reset PIT interrupt
254          */
255         set_tsr(0x08000000);
256
257         /*
258          * Call timer_interrupt routine in interrupts.c
259          */
260         timer_interrupt(NULL);
261 }
262
263
264 void
265 UnknownException(struct pt_regs *regs)
266 {
267 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
268         if (debugger_exception_handler && (*debugger_exception_handler)(regs))
269                 return;
270 #endif
271
272         printf("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
273                regs->nip, regs->msr, regs->trap);
274         _exception(0, regs);
275 }
276
277 void
278 DebugException(struct pt_regs *regs)
279 {
280         printf("Debugger trap at @ %lx\n", regs->nip );
281         show_regs(regs);
282 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
283         do_bedbug_breakpoint( regs );
284 #endif
285 }
286
287 /* Probe an address by reading.  If not present, return -1, otherwise
288  * return 0.
289  */
290 int
291 addr_probe(uint *addr)
292 {
293 #if 0
294         int     retval;
295
296         __asm__ __volatile__(                   \
297                 "1:     lwz %0,0(%1)\n"         \
298                 "       eieio\n"                \
299                 "       li %0,0\n"              \
300                 "2:\n"                          \
301                 ".section .fixup,\"ax\"\n"      \
302                 "3:     li %0,-1\n"             \
303                 "       b 2b\n"                 \
304                 ".section __ex_table,\"a\"\n"   \
305                 "       .align 2\n"             \
306                 "       .long 1b,3b\n"          \
307                 ".text"                         \
308                 : "=r" (retval) : "r"(addr));
309
310         return (retval);
311 #endif
312         return 0;
313 }