blob: 09388aa3d53a15ed8b34d188732ee31eded5499f [file] [log] [blame]
Mike Frysinger9171fc82008-03-30 15:46:13 -04001/*
2 * U-boot - traps.c Routines related to interrupts and exceptions
3 *
4 * Copyright (c) 2005-2008 Analog Devices Inc.
5 *
6 * This file is based on
7 * No original Copyright holder listed,
8 * Probabily original (C) Roman Zippel (assigned DJD, 1999)
9 *
10 * Copyright 2003 Metrowerks - for Blackfin
11 * Copyright 2000-2001 Lineo, Inc. D. Jeff Dionne <jeff@lineo.ca>
12 * Copyright 1999-2000 D. Jeff Dionne, <jeff@uclinux.org>
13 *
14 * (C) Copyright 2000-2004
15 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
16 *
17 * Licensed under the GPL-2 or later.
18 */
19
20#include <common.h>
Robin Getzf19fd872009-12-21 16:35:48 -050021#include <kgdb.h>
Mike Frysinger9171fc82008-03-30 15:46:13 -040022#include <linux/types.h>
23#include <asm/traps.h>
24#include <asm/cplb.h>
25#include <asm/io.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>
Robin Getzf19fd872009-12-21 16:35:48 -050029#include <asm/deferred.h>
Mike Frysinger9171fc82008-03-30 15:46:13 -040030#include "cpu.h"
31
Mike Frysinger66a49092010-04-29 00:31:36 -040032#ifdef CONFIG_DEBUG_DUMP
33# define ENABLE_DUMP 1
34#else
35# define ENABLE_DUMP 0
36#endif
37
Mike Frysinger9171fc82008-03-30 15:46:13 -040038#define trace_buffer_save(x) \
39 do { \
Mike Frysinger66a49092010-04-29 00:31:36 -040040 if (!ENABLE_DUMP) \
41 break; \
Mike Frysinger9171fc82008-03-30 15:46:13 -040042 (x) = bfin_read_TBUFCTL(); \
43 bfin_write_TBUFCTL((x) & ~TBUFEN); \
44 } while (0)
45
46#define trace_buffer_restore(x) \
Mike Frysinger66a49092010-04-29 00:31:36 -040047 do { \
48 if (!ENABLE_DUMP) \
49 break; \
50 bfin_write_TBUFCTL((x)); \
51 } while (0);
Mike Frysinger9171fc82008-03-30 15:46:13 -040052
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.
60 */
61struct memory_map {
62 uint32_t start, end;
63 uint32_t data_flags, inst_flags;
64};
65const struct memory_map const bfin_memory_map[] = {
66 { /* external memory */
67 .start = 0x00000000,
68 .end = 0x20000000,
69 .data_flags = SDRAM_DGENERIC,
70 .inst_flags = SDRAM_IGENERIC,
71 },
72 { /* async banks */
73 .start = 0x20000000,
74 .end = 0x30000000,
75 .data_flags = SDRAM_EBIU,
76 .inst_flags = SDRAM_INON_CHBL,
77 },
78 { /* everything on chip */
79 .start = 0xE0000000,
80 .end = 0xFFFFFFFF,
81 .data_flags = L1_DMEMORY,
82 .inst_flags = L1_IMEMORY,
83 }
84};
85
Robin Getzf19fd872009-12-21 16:35:48 -050086#ifdef CONFIG_EXCEPTION_DEFER
87unsigned int deferred_regs[deferred_regs_last];
88#endif
89
90/*
91 * Handle all exceptions while running in EVT3 or EVT5
92 */
93int trap_c(struct pt_regs *regs, uint32_t level)
Mike Frysinger9171fc82008-03-30 15:46:13 -040094{
Robin Getzf19fd872009-12-21 16:35:48 -050095 uint32_t ret = 0;
Mike Frysinger9171fc82008-03-30 15:46:13 -040096 uint32_t trapnr = (regs->seqstat & EXCAUSE);
Mike Frysinger66a49092010-04-29 00:31:36 -040097 unsigned long tflags;
Mike Frysinger9171fc82008-03-30 15:46:13 -040098 bool data = false;
99
Mike Frysinger66a49092010-04-29 00:31:36 -0400100 /*
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.
104 */
105 trace_buffer_save(tflags);
106
Mike Frysinger9171fc82008-03-30 15:46:13 -0400107 switch (trapnr) {
108 /* 0x26 - Data CPLB Miss */
109 case VEC_CPLB_M:
110
111 if (ANOMALY_05000261) {
112 static uint32_t last_cplb_fault_retx;
113 /*
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.
117 */
118 if (last_cplb_fault_retx != regs->retx) {
119 last_cplb_fault_retx = regs->retx;
Mike Frysinger66a49092010-04-29 00:31:36 -0400120 break;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400121 }
122 }
123
124 data = true;
125 /* fall through */
126
127 /* 0x27 - Instruction CPLB Miss */
128 case VEC_CPLB_I_M: {
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;
132 size_t i;
Robin Getzb6db2832009-12-21 16:59:21 -0500133
Robin Getzf19fd872009-12-21 16:35:48 -0500134#ifdef CONFIG_EXCEPTION_DEFER
135 /* This should never happen */
136 if (level == 5)
137 bfin_panic(regs);
138#endif
139
Mike Frysinger9171fc82008-03-30 15:46:13 -0400140 new_cplb_addr = (data ? bfin_read_DCPLB_FAULT_ADDR() : bfin_read_ICPLB_FAULT_ADDR()) & ~(4 * 1024 * 1024 - 1);
141
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)
146 break;
147 }
148 if (i == ARRAY_SIZE(bfin_memory_map)) {
149 printf("%cCPLB exception outside of memory map at 0x%p\n",
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400150 (data ? 'D' : 'I'), (void *)new_cplb_addr);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400151 bfin_panic(regs);
152 } else
153 debug("CPLB addr %p matches map 0x%p - 0x%p\n", new_cplb_addr, bfin_memory_map[i].start, bfin_memory_map[i].end);
154 new_cplb_data = (data ? bfin_memory_map[i].data_flags : bfin_memory_map[i].inst_flags);
155
Mike Frysinger9171fc82008-03-30 15:46:13 -0400156 if (data) {
157 CPLB_ADDR_BASE = (uint32_t *)DCPLB_ADDR0;
158 CPLB_DATA_BASE = (uint32_t *)DCPLB_DATA0;
159 } else {
160 CPLB_ADDR_BASE = (uint32_t *)ICPLB_ADDR0;
161 CPLB_DATA_BASE = (uint32_t *)ICPLB_DATA0;
162 }
163
164 /* find the next unlocked entry and evict it */
165 i = last_evicted & 0xF;
166 debug("last evicted = %i\n", i);
167 CPLB_DATA = CPLB_DATA_BASE + i;
168 while (*CPLB_DATA & CPLB_LOCK) {
169 debug("skipping %i %p - %08X\n", i, CPLB_DATA, *CPLB_DATA);
170 i = (i + 1) & 0xF; /* wrap around */
171 CPLB_DATA = CPLB_DATA_BASE + i;
172 }
173 CPLB_ADDR = CPLB_ADDR_BASE + i;
174
175 debug("evicting entry %i: 0x%p 0x%08X\n", i, *CPLB_ADDR, *CPLB_DATA);
176 last_evicted = i + 1;
Mike Frysinger0332e4d2008-08-07 18:39:27 -0400177
178 /* need to turn off cplbs whenever we muck with the cplb table */
179#if ENDCPLB != ENICPLB
180# error cplb enable bit violates my sanity
181#endif
182 uint32_t mem_control = (data ? DMEM_CONTROL : IMEM_CONTROL);
183 bfin_write32(mem_control, bfin_read32(mem_control) & ~ENDCPLB);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400184 *CPLB_ADDR = new_cplb_addr;
185 *CPLB_DATA = new_cplb_data;
Mike Frysinger0332e4d2008-08-07 18:39:27 -0400186 bfin_write32(mem_control, bfin_read32(mem_control) | ENDCPLB);
187 SSYNC();
Mike Frysinger9171fc82008-03-30 15:46:13 -0400188
189 /* dump current table for debugging purposes */
190 CPLB_ADDR = CPLB_ADDR_BASE;
191 CPLB_DATA = CPLB_DATA_BASE;
192 for (i = 0; i < 16; ++i)
193 debug("%2i 0x%p 0x%08X\n", i, *CPLB_ADDR++, *CPLB_DATA++);
194
Mike Frysinger9171fc82008-03-30 15:46:13 -0400195 break;
196 }
Robin Getzf19fd872009-12-21 16:35:48 -0500197#ifdef CONFIG_CMD_KGDB
198 /* Single step
199 * if we are in IRQ5, just ignore, otherwise defer, and handle it in kgdb
200 */
201 case VEC_STEP:
202 if (level == 3) {
203 /* If we just returned from an interrupt, the single step
204 * event is for the RTI instruction.
205 */
206 if (regs->retx == regs->pc)
207 break;
208 /* we just return if we are single stepping through IRQ5 */
209 if (regs->ipend & 0x20)
210 break;
211 /* Otherwise, turn single stepping off & fall through,
212 * which defers to IRQ5
213 */
214 regs->syscfg &= ~1;
215 }
216 /* fall through */
217#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400218 default:
Robin Getzf19fd872009-12-21 16:35:48 -0500219#ifdef CONFIG_CMD_KGDB
220 if (level == 3) {
221 /* We need to handle this at EVT5, so try again */
Mike Frysinger66a49092010-04-29 00:31:36 -0400222 bfin_dump(regs);
Robin Getzf19fd872009-12-21 16:35:48 -0500223 ret = 1;
224 break;
225 }
226 if (debugger_exception_handler && (*debugger_exception_handler)(regs))
Mike Frysinger66a49092010-04-29 00:31:36 -0400227 break;
Robin Getzf19fd872009-12-21 16:35:48 -0500228#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400229 bfin_panic(regs);
230 }
Mike Frysinger66a49092010-04-29 00:31:36 -0400231
232 trace_buffer_restore(tflags);
233
Robin Getzf19fd872009-12-21 16:35:48 -0500234 return ret;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400235}
236
Mike Frysingerecb1dc82009-05-20 04:35:14 -0400237#ifndef CONFIG_KALLSYMS
238const char *symbol_lookup(unsigned long addr, unsigned long *caddr)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400239{
Mike Frysingerecb1dc82009-05-20 04:35:14 -0400240 *caddr = addr;
241 return "N/A";
Mike Frysinger9171fc82008-03-30 15:46:13 -0400242}
Mike Frysingerecb1dc82009-05-20 04:35:14 -0400243#endif
Mike Frysinger9171fc82008-03-30 15:46:13 -0400244
245static void decode_address(char *buf, unsigned long address)
246{
247 unsigned long sym_addr;
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400248 void *paddr = (void *)address;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400249 const char *sym = symbol_lookup(address, &sym_addr);
250
251 if (sym) {
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400252 sprintf(buf, "<0x%p> { %s + 0x%lx }", paddr, sym, address - sym_addr);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400253 return;
254 }
255
256 if (!address)
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400257 sprintf(buf, "<0x%p> /* Maybe null pointer? */", paddr);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200258 else if (address >= CONFIG_SYS_MONITOR_BASE &&
259 address < CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400260 sprintf(buf, "<0x%p> /* somewhere in u-boot */", paddr);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400261 else
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400262 sprintf(buf, "<0x%p> /* unknown address */", paddr);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400263}
264
Mike Frysinger0ba1da12008-10-06 04:21:41 -0400265static char *strhwerrcause(uint16_t hwerrcause)
266{
267 switch (hwerrcause) {
268 case 0x02: return "system mmr error";
269 case 0x03: return "external memory addressing error";
270 case 0x12: return "performance monitor overflow";
271 case 0x18: return "raise 5 instruction";
272 default: return "undef";
273 }
274}
275
276static char *strexcause(uint16_t excause)
277{
278 switch (excause) {
279 case 0x00 ... 0xf: return "custom exception";
280 case 0x10: return "single step";
281 case 0x11: return "trace buffer full";
282 case 0x21: return "undef inst";
283 case 0x22: return "illegal inst";
284 case 0x23: return "dcplb prot violation";
285 case 0x24: return "misaligned data";
286 case 0x25: return "unrecoverable event";
287 case 0x26: return "dcplb miss";
288 case 0x27: return "multiple dcplb hit";
289 case 0x28: return "emulation watchpoint";
290 case 0x2a: return "misaligned inst";
291 case 0x2b: return "icplb prot violation";
292 case 0x2c: return "icplb miss";
293 case 0x2d: return "multiple icplb hit";
294 case 0x2e: return "illegal use of supervisor resource";
295 default: return "undef";
296 }
297}
298
Mike Frysinger9171fc82008-03-30 15:46:13 -0400299void dump(struct pt_regs *fp)
300{
301 char buf[150];
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400302 int i;
Mike Frysinger0ba1da12008-10-06 04:21:41 -0400303 uint16_t hwerrcause, excause;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400304
305 if (!ENABLE_DUMP)
306 return;
307
Robin Getzf19fd872009-12-21 16:35:48 -0500308#ifndef CONFIG_CMD_KGDB
309 /* fp->ipend is normally garbage, so load it ourself */
Mike Frysinger2de95bb2008-10-06 04:20:54 -0400310 fp->ipend = bfin_read_IPEND();
Robin Getzf19fd872009-12-21 16:35:48 -0500311#endif
Mike Frysinger2de95bb2008-10-06 04:20:54 -0400312
Mike Frysinger0ba1da12008-10-06 04:21:41 -0400313 hwerrcause = (fp->seqstat & HWERRCAUSE) >> HWERRCAUSE_P;
314 excause = (fp->seqstat & EXCAUSE) >> EXCAUSE_P;
315
Mike Frysinger9171fc82008-03-30 15:46:13 -0400316 printf("SEQUENCER STATUS:\n");
317 printf(" SEQSTAT: %08lx IPEND: %04lx SYSCFG: %04lx\n",
318 fp->seqstat, fp->ipend, fp->syscfg);
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400319 printf(" HWERRCAUSE: 0x%x: %s\n", hwerrcause, strhwerrcause(hwerrcause));
320 printf(" EXCAUSE : 0x%x: %s\n", excause, strexcause(excause));
Mike Frysinger9171fc82008-03-30 15:46:13 -0400321 for (i = 6; i <= 15; ++i) {
322 if (fp->ipend & (1 << i)) {
323 decode_address(buf, bfin_read32(EVT0 + 4*i));
324 printf(" physical IVG%i asserted : %s\n", i, buf);
325 }
326 }
327 decode_address(buf, fp->rete);
328 printf(" RETE: %s\n", buf);
329 decode_address(buf, fp->retn);
330 printf(" RETN: %s\n", buf);
331 decode_address(buf, fp->retx);
332 printf(" RETX: %s\n", buf);
333 decode_address(buf, fp->rets);
334 printf(" RETS: %s\n", buf);
Mike Frysinger2de95bb2008-10-06 04:20:54 -0400335 /* we lie and store RETI in "pc" */
Mike Frysinger9171fc82008-03-30 15:46:13 -0400336 decode_address(buf, fp->pc);
Mike Frysinger2de95bb2008-10-06 04:20:54 -0400337 printf(" RETI: %s\n", buf);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400338
339 if (fp->seqstat & EXCAUSE) {
340 decode_address(buf, bfin_read_DCPLB_FAULT_ADDR());
341 printf("DCPLB_FAULT_ADDR: %s\n", buf);
342 decode_address(buf, bfin_read_ICPLB_FAULT_ADDR());
343 printf("ICPLB_FAULT_ADDR: %s\n", buf);
344 }
345
346 printf("\nPROCESSOR STATE:\n");
347 printf(" R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
348 fp->r0, fp->r1, fp->r2, fp->r3);
349 printf(" R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
350 fp->r4, fp->r5, fp->r6, fp->r7);
351 printf(" P0 : %08lx P1 : %08lx P2 : %08lx P3 : %08lx\n",
352 fp->p0, fp->p1, fp->p2, fp->p3);
353 printf(" P4 : %08lx P5 : %08lx FP : %08lx SP : %08lx\n",
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400354 fp->p4, fp->p5, fp->fp, (unsigned long)fp);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400355 printf(" LB0: %08lx LT0: %08lx LC0: %08lx\n",
356 fp->lb0, fp->lt0, fp->lc0);
357 printf(" LB1: %08lx LT1: %08lx LC1: %08lx\n",
358 fp->lb1, fp->lt1, fp->lc1);
359 printf(" B0 : %08lx L0 : %08lx M0 : %08lx I0 : %08lx\n",
360 fp->b0, fp->l0, fp->m0, fp->i0);
361 printf(" B1 : %08lx L1 : %08lx M1 : %08lx I1 : %08lx\n",
362 fp->b1, fp->l1, fp->m1, fp->i1);
363 printf(" B2 : %08lx L2 : %08lx M2 : %08lx I2 : %08lx\n",
364 fp->b2, fp->l2, fp->m2, fp->i2);
365 printf(" B3 : %08lx L3 : %08lx M3 : %08lx I3 : %08lx\n",
366 fp->b3, fp->l3, fp->m3, fp->i3);
367 printf("A0.w: %08lx A0.x: %08lx A1.w: %08lx A1.x: %08lx\n",
368 fp->a0w, fp->a0x, fp->a1w, fp->a1x);
369
370 printf("USP : %08lx ASTAT: %08lx\n",
371 fp->usp, fp->astat);
372
373 printf("\n");
374}
375
Mike Frysinger66a49092010-04-29 00:31:36 -0400376static void _dump_bfin_trace_buffer(void)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400377{
378 char buf[150];
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400379 int i = 0;
Mike Frysinger9171fc82008-03-30 15:46:13 -0400380
381 if (!ENABLE_DUMP)
382 return;
383
Mike Frysinger9171fc82008-03-30 15:46:13 -0400384 printf("Hardware Trace:\n");
385
386 if (bfin_read_TBUFSTAT() & TBUFCNT) {
387 for (; bfin_read_TBUFSTAT() & TBUFCNT; i++) {
388 decode_address(buf, bfin_read_TBUF());
389 printf("%4i Target : %s\n", i, buf);
390 decode_address(buf, bfin_read_TBUF());
391 printf(" Source : %s\n", buf);
392 }
393 }
Mike Frysinger66a49092010-04-29 00:31:36 -0400394}
Mike Frysinger9171fc82008-03-30 15:46:13 -0400395
Mike Frysinger66a49092010-04-29 00:31:36 -0400396void dump_bfin_trace_buffer(void)
397{
398 unsigned long tflags;
399 trace_buffer_save(tflags);
400 _dump_bfin_trace_buffer();
Mike Frysinger9171fc82008-03-30 15:46:13 -0400401 trace_buffer_restore(tflags);
402}
403
Mike Frysinger66a49092010-04-29 00:31:36 -0400404void bfin_dump(struct pt_regs *regs)
Mike Frysinger9171fc82008-03-30 15:46:13 -0400405{
Mike Frysinger66a49092010-04-29 00:31:36 -0400406 unsigned long tflags;
407
408 trace_buffer_save(tflags);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400409
410 puts(
411 "\n"
412 "\n"
413 "\n"
414 "Ack! Something bad happened to the Blackfin!\n"
415 "\n"
416 );
417 dump(regs);
Mike Frysinger66a49092010-04-29 00:31:36 -0400418 _dump_bfin_trace_buffer();
Mike Frysinger71339992008-10-06 04:19:34 -0400419 puts("\n");
Mike Frysinger66a49092010-04-29 00:31:36 -0400420
421 trace_buffer_restore(tflags);
422}
423
424void bfin_panic(struct pt_regs *regs)
425{
426 unsigned long tflags;
427 trace_buffer_save(tflags);
428 bfin_dump(regs);
Mike Frysinger9171fc82008-03-30 15:46:13 -0400429 bfin_reset_or_hang();
430}