paul@0 | 1 | /* |
paul@0 | 2 | * PIC32 microcontroller interrupt handling code. |
paul@0 | 3 | * |
paul@0 | 4 | * Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 5 | * |
paul@0 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@0 | 7 | * it under the terms of the GNU General Public License as published by |
paul@0 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@0 | 9 | * (at your option) any later version. |
paul@0 | 10 | * |
paul@0 | 11 | * This program is distributed in the hope that it will be useful, |
paul@0 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@0 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@0 | 14 | * GNU General Public License for more details. |
paul@0 | 15 | * |
paul@0 | 16 | * You should have received a copy of the GNU General Public License |
paul@0 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 18 | */ |
paul@0 | 19 | |
paul@0 | 20 | #include "mips.h" |
paul@0 | 21 | #include "pic32.h" |
paul@0 | 22 | #include "cpu.h" |
paul@0 | 23 | |
paul@75 | 24 | #define IRQ_STACK_LIMIT (KSEG0_BASE + IRQ_STACK_SIZE) |
paul@75 | 25 | #define IRQ_STACK_TOP (IRQ_STACK_LIMIT - 34 * 4) |
paul@75 | 26 | |
paul@0 | 27 | .globl enable_interrupts |
paul@0 | 28 | .globl handle_error_level |
paul@0 | 29 | .globl init_interrupts |
paul@0 | 30 | .extern exception_handler |
paul@0 | 31 | .extern interrupt_handler |
paul@0 | 32 | |
paul@0 | 33 | /* Put general routines in the text section. */ |
paul@0 | 34 | |
paul@0 | 35 | .text |
paul@0 | 36 | |
paul@0 | 37 | /* |
paul@0 | 38 | Clear the error and exception status flags, making interrupts and exceptions |
paul@0 | 39 | possible. |
paul@0 | 40 | */ |
paul@0 | 41 | |
paul@0 | 42 | handle_error_level: |
paul@0 | 43 | mfc0 $t3, CP0_STATUS |
paul@0 | 44 | |
paul@0 | 45 | /* Clear error level and exception level. */ |
paul@0 | 46 | |
paul@0 | 47 | li $t4, ~(STATUS_ERL | STATUS_EXL) |
paul@0 | 48 | and $t3, $t3, $t4 |
paul@0 | 49 | mtc0 $t3, CP0_STATUS |
paul@0 | 50 | |
paul@0 | 51 | jr $ra |
paul@0 | 52 | nop |
paul@0 | 53 | |
paul@0 | 54 | /* Enable interrupts and direct interrupt requests to non-bootloader vectors. */ |
paul@0 | 55 | |
paul@0 | 56 | enable_interrupts: |
paul@0 | 57 | mfc0 $t3, CP0_STATUS |
paul@0 | 58 | |
paul@0 | 59 | /* Clear interrupt priority bits. */ |
paul@0 | 60 | |
paul@0 | 61 | li $t4, ~STATUS_IRQ |
paul@0 | 62 | and $t3, $t3, $t4 |
paul@0 | 63 | |
paul@0 | 64 | /* Set interrupt priority. */ |
paul@0 | 65 | |
paul@0 | 66 | ori $t3, $t3, (CPU_INT_PRIORITY << STATUS_IRQ_SHIFT) |
paul@0 | 67 | |
paul@0 | 68 | /* CP0_STATUS &= ~STATUS_BEV (use non-bootloader vectors) */ |
paul@0 | 69 | |
paul@0 | 70 | li $t4, ~STATUS_BEV |
paul@0 | 71 | and $t3, $t3, $t4 |
paul@0 | 72 | |
paul@0 | 73 | /* Enable interrupts. */ |
paul@0 | 74 | |
paul@0 | 75 | ori $t3, $t3, STATUS_IE |
paul@0 | 76 | mtc0 $t3, CP0_STATUS |
paul@0 | 77 | |
paul@0 | 78 | jr $ra |
paul@0 | 79 | nop |
paul@0 | 80 | |
paul@0 | 81 | /* Initialise the interrupt system parameters. */ |
paul@0 | 82 | |
paul@0 | 83 | init_interrupts: |
paul@0 | 84 | /* Clear debug mode. */ |
paul@0 | 85 | |
paul@0 | 86 | mfc0 $t3, CP0_DEBUG |
paul@0 | 87 | li $t4, ~DEBUG_DM |
paul@0 | 88 | and $t3, $t3, $t4 |
paul@0 | 89 | mtc0 $t3, CP0_DEBUG |
paul@0 | 90 | |
paul@0 | 91 | /* Update the exception base. */ |
paul@0 | 92 | |
paul@0 | 93 | mfc0 $t3, CP0_STATUS |
paul@0 | 94 | li $t4, STATUS_BEV /* BEV = 1 or EBASE cannot be set */ |
paul@0 | 95 | or $t3, $t3, $t4 |
paul@0 | 96 | mtc0 $t3, CP0_STATUS |
paul@0 | 97 | |
paul@0 | 98 | la $t3, ebase |
paul@0 | 99 | mtc0 $t3, CP0_EBASE |
paul@0 | 100 | |
paul@0 | 101 | /* Set vector spacing. */ |
paul@0 | 102 | |
paul@0 | 103 | li $t3, 0x20 /* Must be non-zero or the CPU gets upset */ |
paul@0 | 104 | mtc0 $t3, CP0_INTCTL |
paul@0 | 105 | |
paul@0 | 106 | li $t3, CAUSE_IV /* IV = 1 (use EBASE+0x200 for interrupts) */ |
paul@0 | 107 | mtc0 $t3, CP0_CAUSE |
paul@0 | 108 | |
paul@0 | 109 | jr $ra |
paul@0 | 110 | nop |
paul@0 | 111 | |
paul@0 | 112 | |
paul@0 | 113 | |
paul@0 | 114 | /* Exception servicing, positioned at EBASE at the start of program memory. */ |
paul@0 | 115 | |
paul@0 | 116 | .section .vectors, "a" |
paul@0 | 117 | |
paul@0 | 118 | /* TLB error servicing. */ |
paul@0 | 119 | |
paul@0 | 120 | ebase: |
paul@0 | 121 | tlb_handler: |
paul@0 | 122 | j exception_handler |
paul@0 | 123 | nop |
paul@0 | 124 | |
paul@0 | 125 | |
paul@0 | 126 | |
paul@0 | 127 | /* General exception servicing. */ |
paul@0 | 128 | |
paul@0 | 129 | .org 0x180 |
paul@0 | 130 | |
paul@0 | 131 | exc_handler: |
paul@75 | 132 | move $k0, $sp |
paul@75 | 133 | move $k1, $t9 |
paul@75 | 134 | |
paul@75 | 135 | /* Switch to the IRQ stack. */ |
paul@75 | 136 | |
paul@75 | 137 | lui $sp, %hi(IRQ_STACK_TOP) |
paul@75 | 138 | ori $sp, $sp, %lo(IRQ_STACK_TOP) |
paul@75 | 139 | |
paul@75 | 140 | /* Obtain the address directly to avoid initialising $gp here. */ |
paul@75 | 141 | |
paul@75 | 142 | lui $t9, %hi(exception_handler) |
paul@75 | 143 | ori $t9, $t9, %lo(exception_handler) |
paul@75 | 144 | jr $t9 |
paul@0 | 145 | nop |
paul@0 | 146 | |
paul@0 | 147 | |
paul@0 | 148 | |
paul@0 | 149 | /* Interrupt servicing. */ |
paul@0 | 150 | |
paul@0 | 151 | .org 0x200 |
paul@0 | 152 | .set noat |
paul@0 | 153 | |
paul@0 | 154 | int_handler: |
paul@0 | 155 | |
paul@0 | 156 | /* Store affected registers from IRQ_STACK_LIMIT - 4 downwards. */ |
paul@0 | 157 | |
paul@22 | 158 | lui $k0, %hi(IRQ_STACK_LIMIT) |
paul@22 | 159 | ori $k0, $k0, %lo(IRQ_STACK_LIMIT) |
paul@0 | 160 | |
paul@0 | 161 | .irp reg, \ |
paul@0 | 162 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 \ |
paul@0 | 163 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, \ |
paul@0 | 164 | 28, 29, 30, 31 |
paul@0 | 165 | sw $\reg, -(\reg * 4)($k0) |
paul@0 | 166 | .endr |
paul@0 | 167 | |
paul@70 | 168 | mflo $k1 |
paul@70 | 169 | sw $k1, -(32 * 4)($k0) |
paul@70 | 170 | mfhi $k1 |
paul@70 | 171 | sw $k1, -(33 * 4)($k0) |
paul@70 | 172 | |
paul@0 | 173 | /* Switch to the IRQ stack. */ |
paul@0 | 174 | |
paul@22 | 175 | lui $sp, %hi(IRQ_STACK_TOP) |
paul@22 | 176 | ori $sp, $sp, %lo(IRQ_STACK_TOP) |
paul@0 | 177 | |
paul@75 | 178 | /* Obtain the address directly to avoid initialising $gp here. */ |
paul@75 | 179 | |
paul@75 | 180 | lui $t9, %hi(interrupt_handler) |
paul@75 | 181 | ori $t9, $t9, %lo(interrupt_handler) |
paul@75 | 182 | jalr $t9 |
paul@0 | 183 | nop |
paul@0 | 184 | |
paul@0 | 185 | /* Restore affected registers. */ |
paul@0 | 186 | |
paul@70 | 187 | lw $k1, -(32 * 4)($k0) |
paul@70 | 188 | mtlo $k1 |
paul@70 | 189 | lw $k1, -(33 * 4)($k0) |
paul@70 | 190 | mthi $k1 |
paul@70 | 191 | |
paul@0 | 192 | .irp reg, \ |
paul@0 | 193 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 \ |
paul@0 | 194 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, \ |
paul@0 | 195 | 28, 29, 30, 31 |
paul@0 | 196 | lw $\reg, -(\reg * 4)($k0) |
paul@0 | 197 | .endr |
paul@0 | 198 | |
paul@0 | 199 | eret |
paul@0 | 200 | nop |