Merge tag 'dm-pull-3dec19' of https://gitlab.denx.de/u-boot/custodians/u-boot-dm
[oweals/u-boot.git] / arch / mips / lib / traps.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 1994 - 1999, 2000, 01, 06 Ralf Baechle
4  * Copyright (C) 1995, 1996 Paul M. Antoine
5  * Copyright (C) 1998 Ulf Carlsson
6  * Copyright (C) 1999 Silicon Graphics, Inc.
7  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
8  * Copyright (C) 2002, 2003, 2004, 2005, 2007  Maciej W. Rozycki
9  * Copyright (C) 2000, 2001, 2012 MIPS Technologies, Inc.  All rights reserved.
10  * Copyright (C) 2014, Imagination Technologies Ltd.
11  */
12
13 #include <common.h>
14 #include <cpu_func.h>
15 #include <init.h>
16 #include <asm/mipsregs.h>
17 #include <asm/addrspace.h>
18 #include <asm/system.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 static void show_regs(const struct pt_regs *regs)
23 {
24         const int field = 2 * sizeof(unsigned long);
25         unsigned int cause = regs->cp0_cause;
26         unsigned int exccode;
27         int i;
28
29         /*
30          * Saved main processor registers
31          */
32         for (i = 0; i < 32; ) {
33                 if ((i % 4) == 0)
34                         printf("$%2d   :", i);
35                 if (i == 0)
36                         printf(" %0*lx", field, 0UL);
37                 else if (i == 26 || i == 27)
38                         printf(" %*s", field, "");
39                 else
40                         printf(" %0*lx", field, regs->regs[i]);
41
42                 i++;
43                 if ((i % 4) == 0)
44                         puts("\n");
45         }
46
47         printf("Hi    : %0*lx\n", field, regs->hi);
48         printf("Lo    : %0*lx\n", field, regs->lo);
49
50         /*
51          * Saved cp0 registers
52          */
53         printf("epc   : %0*lx (text %0*lx)\n", field, regs->cp0_epc,
54                field, regs->cp0_epc - gd->reloc_off);
55         printf("ra    : %0*lx (text %0*lx)\n", field, regs->regs[31],
56                field, regs->regs[31] - gd->reloc_off);
57
58         printf("Status: %08x\n", (uint32_t) regs->cp0_status);
59
60         exccode = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
61         printf("Cause : %08x (ExcCode %02x)\n", cause, exccode);
62
63         if (1 <= exccode && exccode <= 5)
64                 printf("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
65
66         printf("PrId  : %08x\n", read_c0_prid());
67 }
68
69 void do_reserved(const struct pt_regs *regs)
70 {
71         puts("\nOoops:\n");
72         show_regs(regs);
73         hang();
74 }
75
76 void do_ejtag_debug(const struct pt_regs *regs)
77 {
78         const int field = 2 * sizeof(unsigned long);
79         unsigned long depc;
80         unsigned int debug;
81
82         depc = read_c0_depc();
83         debug = read_c0_debug();
84
85         printf("SDBBP EJTAG debug exception: c0_depc = %0*lx, DEBUG = %08x\n",
86                field, depc, debug);
87 }
88
89 static void set_handler(unsigned long offset, void *addr, unsigned long size)
90 {
91         unsigned long ebase = gd->irq_sp;
92
93         memcpy((void *)(ebase + offset), addr, size);
94         flush_cache(ebase + offset, size);
95 }
96
97 void trap_init(ulong reloc_addr)
98 {
99         unsigned long ebase = gd->irq_sp;
100
101         set_handler(0x180, &except_vec3_generic, 0x80);
102         set_handler(0x280, &except_vec_ejtag_debug, 0x80);
103
104         write_c0_ebase(ebase);
105         clear_c0_status(ST0_BEV);
106         execution_hazard_barrier();
107 }