2 * U-boot - traps.c Routines related to interrupts and exceptions
4 * Copyright (c) 2005-2008 Analog Devices Inc.
6 * This file is based on
7 * No original Copyright holder listed,
8 * Probabily original (C) Roman Zippel (assigned DJD, 1999)
10 * Copyright 2003 Metrowerks - for Blackfin
14 * (C) Copyright 2000-2004
17 * Licensed under the GPL-2 or later.
22 #include <linux/types.h>
23 #include <asm/traps.h>
26 #include <asm/mach-common/bits/core.h>
27 #include <asm/mach-common/bits/mpu.h>
28 #include <asm/mach-common/bits/trace.h>
29 #include <asm/deferred.h>
32 #ifdef CONFIG_DEBUG_DUMP
33 # define ENABLE_DUMP 1
35 # define ENABLE_DUMP 0
38 #define trace_buffer_save(x) \
42 (x) = bfin_read_TBUFCTL(); \
43 bfin_write_TBUFCTL((x) & ~TBUFEN); \
46 #define trace_buffer_restore(x) \
50 bfin_write_TBUFCTL((x)); \
53 /* The purpose of this map is to provide a mapping of address<->cplb settings
54 * rather than an exact map of what is actually addressable on the part. This
55 * map covers all current Blackfin parts. If you try to access an address that
56 * is in this map but not actually on the part, you won't get an exception and
57 * reboot, you'll get an external hardware addressing error and reboot. Since
58 * only the ends matter (you did something wrong and the board reset), the means
59 * are largely irrelevant.
63 uint32_t data_flags, inst_flags;
65 const struct memory_map const bfin_memory_map[] = {
66 { /* external memory */
69 .data_flags = SDRAM_DGENERIC,
70 .inst_flags = SDRAM_IGENERIC,
75 .data_flags = SDRAM_EBIU,
76 .inst_flags = SDRAM_INON_CHBL,
78 { /* everything on chip */
81 .data_flags = L1_DMEMORY,
82 .inst_flags = L1_IMEMORY,
86 #ifdef CONFIG_EXCEPTION_DEFER
87 unsigned int deferred_regs[deferred_regs_last];
91 * Handle all exceptions while running in EVT3 or EVT5
93 int trap_c(struct pt_regs *regs, uint32_t level)
96 uint32_t trapnr = (regs->seqstat & EXCAUSE);
101 * Keep the trace buffer so that a miss here points people
102 * to the right place (their code). Crashes here rarely
103 * happen. If they do, only the Blackfin maintainer cares.
105 trace_buffer_save(tflags);
108 /* 0x26 - Data CPLB Miss */
111 if (ANOMALY_05000261) {
112 static uint32_t last_cplb_fault_retx;
114 * Work around an anomaly: if we see a new DCPLB fault,
115 * return without doing anything. Then,
116 * if we get the same fault again, handle it.
118 if (last_cplb_fault_retx != regs->retx) {
119 last_cplb_fault_retx = regs->retx;
127 /* 0x27 - Instruction CPLB Miss */
129 volatile uint32_t *CPLB_ADDR_BASE, *CPLB_DATA_BASE, *CPLB_ADDR, *CPLB_DATA;
130 uint32_t new_cplb_addr = 0, new_cplb_data = 0;
131 static size_t last_evicted;
134 #ifdef CONFIG_EXCEPTION_DEFER
135 /* This should never happen */
140 new_cplb_addr = (data ? bfin_read_DCPLB_FAULT_ADDR() : bfin_read_ICPLB_FAULT_ADDR()) & ~(4 * 1024 * 1024 - 1);
142 for (i = 0; i < ARRAY_SIZE(bfin_memory_map); ++i) {
143 /* if the exception is inside this range, lets use it */
144 if (new_cplb_addr >= bfin_memory_map[i].start &&
145 new_cplb_addr < bfin_memory_map[i].end)
148 if (i == ARRAY_SIZE(bfin_memory_map)) {
149 printf("%cCPLB exception outside of memory map at 0x%p\n",
150 (data ? 'D' : 'I'), (void *)new_cplb_addr);
153 debug("CPLB addr %p matches map 0x%p - 0x%p\n",
154 (void *)new_cplb_addr,
155 (void *)bfin_memory_map[i].start,
156 (void *)bfin_memory_map[i].end);
157 new_cplb_data = (data ? bfin_memory_map[i].data_flags : bfin_memory_map[i].inst_flags);
160 CPLB_ADDR_BASE = (uint32_t *)DCPLB_ADDR0;
161 CPLB_DATA_BASE = (uint32_t *)DCPLB_DATA0;
163 CPLB_ADDR_BASE = (uint32_t *)ICPLB_ADDR0;
164 CPLB_DATA_BASE = (uint32_t *)ICPLB_DATA0;
167 /* find the next unlocked entry and evict it */
168 i = last_evicted & 0xF;
169 debug("last evicted = %zu\n", i);
170 CPLB_DATA = CPLB_DATA_BASE + i;
171 while (*CPLB_DATA & CPLB_LOCK) {
172 debug("skipping %zu %p - %08X\n", i, CPLB_DATA, *CPLB_DATA);
173 i = (i + 1) & 0xF; /* wrap around */
174 CPLB_DATA = CPLB_DATA_BASE + i;
176 CPLB_ADDR = CPLB_ADDR_BASE + i;
178 debug("evicting entry %zu: 0x%p 0x%08X\n", i,
179 (void *)*CPLB_ADDR, *CPLB_DATA);
180 last_evicted = i + 1;
182 /* need to turn off cplbs whenever we muck with the cplb table */
183 #if ENDCPLB != ENICPLB
184 # error cplb enable bit violates my sanity
186 uint32_t mem_control = (data ? DMEM_CONTROL : IMEM_CONTROL);
187 bfin_write32(mem_control, bfin_read32(mem_control) & ~ENDCPLB);
188 *CPLB_ADDR = new_cplb_addr;
189 *CPLB_DATA = new_cplb_data;
190 bfin_write32(mem_control, bfin_read32(mem_control) | ENDCPLB);
193 /* dump current table for debugging purposes */
194 CPLB_ADDR = CPLB_ADDR_BASE;
195 CPLB_DATA = CPLB_DATA_BASE;
196 for (i = 0; i < 16; ++i)
197 debug("%2zu 0x%p 0x%08X\n", i,
198 (void *)*CPLB_ADDR++, *CPLB_DATA++);
202 #ifdef CONFIG_CMD_KGDB
204 * if we are in IRQ5, just ignore, otherwise defer, and handle it in kgdb
208 /* If we just returned from an interrupt, the single step
209 * event is for the RTI instruction.
211 if (regs->retx == regs->pc)
213 /* we just return if we are single stepping through IRQ5 */
214 if (regs->ipend & 0x20)
216 /* Otherwise, turn single stepping off & fall through,
217 * which defers to IRQ5
224 #ifdef CONFIG_CMD_KGDB
226 /* We need to handle this at EVT5, so try again */
231 if (debugger_exception_handler && (*debugger_exception_handler)(regs))
237 trace_buffer_restore(tflags);
242 #ifndef CONFIG_KALLSYMS
243 const char *symbol_lookup(unsigned long addr, unsigned long *caddr)
250 static void decode_address(char *buf, unsigned long address)
252 unsigned long sym_addr;
253 void *paddr = (void *)address;
254 const char *sym = symbol_lookup(address, &sym_addr);
257 sprintf(buf, "<0x%p> { %s + 0x%lx }", paddr, sym, address - sym_addr);
262 sprintf(buf, "<0x%p> /* Maybe null pointer? */", paddr);
263 else if (address >= CONFIG_SYS_MONITOR_BASE &&
264 address < CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
265 sprintf(buf, "<0x%p> /* somewhere in u-boot */", paddr);
267 sprintf(buf, "<0x%p> /* unknown address */", paddr);
270 static char *strhwerrcause(uint16_t hwerrcause)
272 switch (hwerrcause) {
273 case 0x02: return "system mmr error";
274 case 0x03: return "external memory addressing error";
275 case 0x12: return "performance monitor overflow";
276 case 0x18: return "raise 5 instruction";
277 default: return "undef";
281 static char *strexcause(uint16_t excause)
284 case 0x00 ... 0xf: return "custom exception";
285 case 0x10: return "single step";
286 case 0x11: return "trace buffer full";
287 case 0x21: return "undef inst";
288 case 0x22: return "illegal inst";
289 case 0x23: return "dcplb prot violation";
290 case 0x24: return "misaligned data";
291 case 0x25: return "unrecoverable event";
292 case 0x26: return "dcplb miss";
293 case 0x27: return "multiple dcplb hit";
294 case 0x28: return "emulation watchpoint";
295 case 0x2a: return "misaligned inst";
296 case 0x2b: return "icplb prot violation";
297 case 0x2c: return "icplb miss";
298 case 0x2d: return "multiple icplb hit";
299 case 0x2e: return "illegal use of supervisor resource";
300 default: return "undef";
304 void dump(struct pt_regs *fp)
308 uint16_t hwerrcause, excause;
313 #ifndef CONFIG_CMD_KGDB
314 /* fp->ipend is normally garbage, so load it ourself */
315 fp->ipend = bfin_read_IPEND();
318 hwerrcause = (fp->seqstat & HWERRCAUSE) >> HWERRCAUSE_P;
319 excause = (fp->seqstat & EXCAUSE) >> EXCAUSE_P;
321 printf("SEQUENCER STATUS:\n");
322 printf(" SEQSTAT: %08lx IPEND: %04lx SYSCFG: %04lx\n",
323 fp->seqstat, fp->ipend, fp->syscfg);
324 printf(" HWERRCAUSE: 0x%x: %s\n", hwerrcause, strhwerrcause(hwerrcause));
325 printf(" EXCAUSE : 0x%x: %s\n", excause, strexcause(excause));
326 for (i = 6; i <= 15; ++i) {
327 if (fp->ipend & (1 << i)) {
328 decode_address(buf, bfin_read32(EVT0 + 4*i));
329 printf(" physical IVG%i asserted : %s\n", i, buf);
332 decode_address(buf, fp->rete);
333 printf(" RETE: %s\n", buf);
334 decode_address(buf, fp->retn);
335 printf(" RETN: %s\n", buf);
336 decode_address(buf, fp->retx);
337 printf(" RETX: %s\n", buf);
338 decode_address(buf, fp->rets);
339 printf(" RETS: %s\n", buf);
340 /* we lie and store RETI in "pc" */
341 decode_address(buf, fp->pc);
342 printf(" RETI: %s\n", buf);
344 if (fp->seqstat & EXCAUSE) {
345 decode_address(buf, bfin_read_DCPLB_FAULT_ADDR());
346 printf("DCPLB_FAULT_ADDR: %s\n", buf);
347 decode_address(buf, bfin_read_ICPLB_FAULT_ADDR());
348 printf("ICPLB_FAULT_ADDR: %s\n", buf);
351 printf("\nPROCESSOR STATE:\n");
352 printf(" R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
353 fp->r0, fp->r1, fp->r2, fp->r3);
354 printf(" R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
355 fp->r4, fp->r5, fp->r6, fp->r7);
356 printf(" P0 : %08lx P1 : %08lx P2 : %08lx P3 : %08lx\n",
357 fp->p0, fp->p1, fp->p2, fp->p3);
358 printf(" P4 : %08lx P5 : %08lx FP : %08lx SP : %08lx\n",
359 fp->p4, fp->p5, fp->fp, (unsigned long)fp);
360 printf(" LB0: %08lx LT0: %08lx LC0: %08lx\n",
361 fp->lb0, fp->lt0, fp->lc0);
362 printf(" LB1: %08lx LT1: %08lx LC1: %08lx\n",
363 fp->lb1, fp->lt1, fp->lc1);
364 printf(" B0 : %08lx L0 : %08lx M0 : %08lx I0 : %08lx\n",
365 fp->b0, fp->l0, fp->m0, fp->i0);
366 printf(" B1 : %08lx L1 : %08lx M1 : %08lx I1 : %08lx\n",
367 fp->b1, fp->l1, fp->m1, fp->i1);
368 printf(" B2 : %08lx L2 : %08lx M2 : %08lx I2 : %08lx\n",
369 fp->b2, fp->l2, fp->m2, fp->i2);
370 printf(" B3 : %08lx L3 : %08lx M3 : %08lx I3 : %08lx\n",
371 fp->b3, fp->l3, fp->m3, fp->i3);
372 printf("A0.w: %08lx A0.x: %08lx A1.w: %08lx A1.x: %08lx\n",
373 fp->a0w, fp->a0x, fp->a1w, fp->a1x);
375 printf("USP : %08lx ASTAT: %08lx\n",
381 static void _dump_bfin_trace_buffer(void)
389 printf("Hardware Trace:\n");
391 if (bfin_read_TBUFSTAT() & TBUFCNT) {
392 for (; bfin_read_TBUFSTAT() & TBUFCNT; i++) {
393 decode_address(buf, bfin_read_TBUF());
394 printf("%4i Target : %s\n", i, buf);
395 decode_address(buf, bfin_read_TBUF());
396 printf(" Source : %s\n", buf);
401 void dump_bfin_trace_buffer(void)
403 unsigned long tflags;
404 trace_buffer_save(tflags);
405 _dump_bfin_trace_buffer();
406 trace_buffer_restore(tflags);
409 void bfin_dump(struct pt_regs *regs)
411 unsigned long tflags;
413 trace_buffer_save(tflags);
419 "Ack! Something bad happened to the Blackfin!\n"
423 _dump_bfin_trace_buffer();
426 trace_buffer_restore(tflags);
429 void bfin_panic(struct pt_regs *regs)
431 unsigned long tflags;
432 trace_buffer_save(tflags);
434 panic("PANIC: Blackfin internal error");