Merge https://gitlab.denx.de/u-boot/custodians/u-boot-fsl-qoriq
[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 <hang.h>
16 #include <init.h>
17 #include <asm/mipsregs.h>
18 #include <asm/addrspace.h>
19 #include <asm/system.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 static unsigned long saved_ebase;
24
25 static void show_regs(const struct pt_regs *regs)
26 {
27         const int field = 2 * sizeof(unsigned long);
28         unsigned int cause = regs->cp0_cause;
29         unsigned int exccode;
30         int i;
31
32         /*
33          * Saved main processor registers
34          */
35         for (i = 0; i < 32; ) {
36                 if ((i % 4) == 0)
37                         printf("$%2d   :", i);
38                 if (i == 0)
39                         printf(" %0*lx", field, 0UL);
40                 else if (i == 26 || i == 27)
41                         printf(" %*s", field, "");
42                 else
43                         printf(" %0*lx", field, regs->regs[i]);
44
45                 i++;
46                 if ((i % 4) == 0)
47                         puts("\n");
48         }
49
50         printf("Hi    : %0*lx\n", field, regs->hi);
51         printf("Lo    : %0*lx\n", field, regs->lo);
52
53         /*
54          * Saved cp0 registers
55          */
56         printf("epc   : %0*lx (text %0*lx)\n", field, regs->cp0_epc,
57                field, regs->cp0_epc - gd->reloc_off);
58         printf("ra    : %0*lx (text %0*lx)\n", field, regs->regs[31],
59                field, regs->regs[31] - gd->reloc_off);
60
61         printf("Status: %08x\n", (uint32_t) regs->cp0_status);
62
63         exccode = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
64         printf("Cause : %08x (ExcCode %02x)\n", cause, exccode);
65
66         if (1 <= exccode && exccode <= 5)
67                 printf("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
68
69         printf("PrId  : %08x\n", read_c0_prid());
70 }
71
72 void do_reserved(const struct pt_regs *regs)
73 {
74         puts("\nOoops:\n");
75         show_regs(regs);
76         hang();
77 }
78
79 void do_ejtag_debug(const struct pt_regs *regs)
80 {
81         const int field = 2 * sizeof(unsigned long);
82         unsigned long depc;
83         unsigned int debug;
84
85         depc = read_c0_depc();
86         debug = read_c0_debug();
87
88         printf("SDBBP EJTAG debug exception: c0_depc = %0*lx, DEBUG = %08x\n",
89                field, depc, debug);
90 }
91
92 static void set_handler(unsigned long offset, void *addr, unsigned long size)
93 {
94         unsigned long ebase = gd->irq_sp;
95
96         memcpy((void *)(ebase + offset), addr, size);
97         flush_cache(ebase + offset, size);
98 }
99
100 void trap_init(ulong reloc_addr)
101 {
102         unsigned long ebase = gd->irq_sp;
103
104         set_handler(0x180, &except_vec3_generic, 0x80);
105         set_handler(0x280, &except_vec_ejtag_debug, 0x80);
106
107         saved_ebase = read_c0_ebase() & 0xfffff000;
108
109         write_c0_ebase(ebase);
110         clear_c0_status(ST0_BEV);
111         execution_hazard_barrier();
112 }
113
114 void trap_restore(void)
115 {
116         set_c0_status(ST0_BEV);
117         execution_hazard_barrier();
118
119 #ifdef CONFIG_OVERRIDE_EXCEPTION_VECTOR_BASE
120         write_c0_ebase(CONFIG_NEW_EXCEPTION_VECTOR_BASE & 0xfffff000);
121 #else
122         write_c0_ebase(saved_ebase);
123 #endif
124
125         clear_c0_status(ST0_BEV);
126         execution_hazard_barrier();
127 }