1 /* 2 * Interrupt handling. 3 * 4 * Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifdef CONFIG_CPU_JZ4730_MINIPC 21 #include "minipc.h" 22 #else 23 #include "nanonote.h" 24 #endif 25 26 #include "board-specific.h" 27 #include "board.h" 28 #include "lcd.h" 29 #include "jzlcd.h" 30 #include "cpu.h" 31 #include "irq.h" 32 33 extern vidinfo_t panel_info; 34 35 void next_pixel(unsigned short *x, unsigned short *y) 36 { 37 (*x)++; 38 if (*x >= panel_info.vl_col) { 39 *x = 0; 40 (*y)++; 41 if (*y >= panel_info.vl_row) 42 *y = 0; 43 } 44 } 45 46 /* Task management. */ 47 48 enum { max_tasks = 3 }; 49 static u32 stack_pointers[max_tasks]; 50 u8 current_task = 0; 51 u32 current_stack_pointer; 52 extern u32 _got_copy_start; 53 54 const u32 stack_start = 0x00080000; 55 const u32 stack_size = 0x00002000; 56 const u32 pagesize = 4 * 1024; 57 const u32 framesize = 120; /* see the handlers */ 58 59 /* Tasks. */ 60 61 void plot_pattern(unsigned short pixel_type, unsigned short x, unsigned short y) 62 { 63 while (1) { 64 if (pixel_type) 65 test_pixel(x, y); 66 else 67 clear_pixel(x, y); 68 next_pixel(&x, &y); 69 udelay(100); 70 } 71 } 72 73 /* Initialisation and handling. */ 74 75 void irq_init() 76 { 77 timer_init_irq(); 78 init_interrupts(); 79 enable_interrupts(); 80 } 81 82 void irq_handle() 83 { 84 unsigned short i; 85 86 /* Check interrupt identity. */ 87 88 if (REG_INTC_IPR & (1 << TIMER_CHAN_IRQ)) { 89 90 /* Switch task. */ 91 92 /* switch_task(); */ 93 94 /* Clear interrupt status. */ 95 96 __intc_ack_irq(TIMER_CHAN_IRQ); 97 __tcu_clear_full_match_flag(TIMER_CHAN); 98 99 /* Handle other interrupts, anyway. */ 100 101 } else { 102 for (i = 0; i < 32; i++) { 103 if (REG_INTC_IPR & (1 << i)) 104 __intc_ack_irq(i); 105 } 106 } 107 } 108 109 void tlb_handle() 110 { 111 u32 asid, virtual, physical, bottom, top; 112 113 /* Obtain the bad virtual address. */ 114 115 asm volatile( 116 "mfc0 %0, $10\n" /* CP0_ENTRYHI */ 117 "nop\n" 118 : "=r" (virtual) 119 ); 120 121 /* Obtain a virtual address region with 8KB resolution. */ 122 123 asid = virtual & 0xff; 124 virtual = virtual & 0xffffe000; 125 126 /* The appropriate physical address depends on the current task. */ 127 128 bottom = (stack_start - stack_size) & 0xffffe000; 129 top = stack_start & 0xffffe000; 130 131 if ((asid != 0) && (virtual >= bottom) && (virtual < top)) 132 physical = virtual + asid * stack_size; 133 else 134 physical = virtual; 135 136 /* 137 Request a physical region mapping two 4KB pages. 138 Pages employ C=3, dirty, valid, with the task number as the ASID. 139 */ 140 141 map_page(virtual, physical, pagesize, 0x1e, asid); 142 } 143 144 void start_task(unsigned short task) 145 { 146 u32 args[] = {1, 0, (task - 1) * 120}; 147 148 /* 149 Each task employs a stack at a multiple of the given start address in 150 physical memory, but at the same address in virtual memory. 151 */ 152 153 map_page(stack_start + stack_size * task - stack_size, stack_start + stack_size * task - stack_size, pagesize, 0x1e, 0); 154 155 /* 156 Set the stack for the new task, initialising the global pointer and 157 return address. 158 */ 159 160 init_stack(stack_start + stack_size * task, _got_copy_start, (void (*)()) plot_pattern, args, 3); 161 162 /* Advance the stack pointer so that the adopted frame will be found. */ 163 164 stack_pointers[task] = stack_start - framesize; 165 } 166 167 void switch_task() 168 { 169 /* 170 Get the current stack pointer. This is obtained just after saving the 171 task's state. 172 */ 173 174 stack_pointers[current_task] = current_stack_pointer; 175 176 /* Switch the current task. */ 177 178 current_task++; 179 if (current_task == max_tasks) current_task = 1; 180 181 /* 182 Set the current stack pointer. This is actually set just before 183 restoring the task's state. 184 */ 185 186 current_stack_pointer = stack_pointers[current_task]; 187 } 188 189 void invoke_task(unsigned short task) 190 { 191 current_task = task; 192 current_stack_pointer = stack_pointers[current_task]; 193 map_page(stack_start - stack_size, stack_start + stack_size * current_task - stack_size, pagesize, 0x1e, current_task); 194 set_task(current_task); 195 196 asm volatile( 197 ".set noat\n" 198 "move $sp, %0\n" 199 "addi $sp, $sp, 120\n" 200 "lw $at, -4($sp)\n" 201 "lw $v0, -8($sp)\n" 202 "lw $v1, -12($sp)\n" 203 "lw $a0, -16($sp)\n" 204 "lw $a1, -20($sp)\n" 205 "lw $a2, -24($sp)\n" 206 "lw $a3, -28($sp)\n" 207 "lw $t0, -32($sp)\n" 208 "lw $t1, -36($sp)\n" 209 "lw $t2, -40($sp)\n" 210 "lw $t3, -44($sp)\n" 211 "lw $t4, -48($sp)\n" 212 "lw $t5, -52($sp)\n" 213 "lw $t6, -56($sp)\n" 214 "lw $t7, -60($sp)\n" 215 "lw $s0, -64($sp)\n" 216 "lw $s1, -68($sp)\n" 217 "lw $s2, -72($sp)\n" 218 "lw $s3, -76($sp)\n" 219 "lw $s4, -80($sp)\n" 220 "lw $s5, -84($sp)\n" 221 "lw $s6, -88($sp)\n" 222 "lw $s7, -92($sp)\n" 223 "lw $t8, -96($sp)\n" 224 "lw $t9, -100($sp)\n" 225 "lw $gp, -104($sp)\n" 226 "lw $fp, -108($sp)\n" 227 "lw $ra, -112($sp)\n" 228 "lw $k0, -116($sp)\n" 229 "mtc0 $k0, $14\n" /* CP0_EPC */ 230 "nop\n" 231 "jr $ra\n" 232 "nop" 233 : 234 : "r" (current_stack_pointer) 235 ); 236 }