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 handle_error_level(); 78 timer_init_irq(); 79 init_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 start_task(unsigned short task) 110 { 111 u32 args[] = {1, 0, (task - 1) * 120}; 112 u32 virtual, physical; 113 114 /* 115 Each task employs a stack at a multiple of the given start address in 116 physical memory, but at the same address in virtual memory. 117 */ 118 119 virtual = stack_start - pagesize * 2; 120 physical = stack_start + stack_size * task - pagesize * 2; 121 122 init_page_table(page_table_start, virtual, physical, pagesize, 0x1e, task); 123 124 /* Map the page for initialisation. */ 125 126 map_page(physical, physical, pagesize, 0x1e, 0); 127 128 /* 129 Set the stack for the new task, initialising the global pointer and 130 return address. 131 */ 132 133 init_stack(stack_start + stack_size * task, _got_copy_start, (void (*)()) plot_pattern, args, 3); 134 135 /* Advance the stack pointer so that the adopted frame will be found. */ 136 137 stack_pointers[task] = stack_start - framesize; 138 } 139 140 void switch_task() 141 { 142 /* 143 Get the current stack pointer. This is obtained just after saving the 144 task's state. 145 */ 146 147 stack_pointers[current_task] = current_stack_pointer; 148 149 /* Switch the current task. */ 150 151 current_task++; 152 if (current_task == max_tasks) current_task = 1; 153 154 /* 155 Set the current stack pointer. This is actually set just before 156 restoring the task's state. 157 */ 158 159 current_stack_pointer = stack_pointers[current_task]; 160 } 161 162 void invoke_task(unsigned short task) 163 { 164 current_task = task; 165 current_stack_pointer = stack_pointers[current_task]; 166 set_task(current_task); 167 168 asm volatile( 169 ".set noat\n" 170 "move $sp, %0\n" 171 "addi $sp, $sp, 120\n" 172 "lw $at, -4($sp)\n" 173 "lw $v0, -8($sp)\n" 174 "lw $v1, -12($sp)\n" 175 "lw $a0, -16($sp)\n" 176 "lw $a1, -20($sp)\n" 177 "lw $a2, -24($sp)\n" 178 "lw $a3, -28($sp)\n" 179 "lw $t0, -32($sp)\n" 180 "lw $t1, -36($sp)\n" 181 "lw $t2, -40($sp)\n" 182 "lw $t3, -44($sp)\n" 183 "lw $t4, -48($sp)\n" 184 "lw $t5, -52($sp)\n" 185 "lw $t6, -56($sp)\n" 186 "lw $t7, -60($sp)\n" 187 "lw $s0, -64($sp)\n" 188 "lw $s1, -68($sp)\n" 189 "lw $s2, -72($sp)\n" 190 "lw $s3, -76($sp)\n" 191 "lw $s4, -80($sp)\n" 192 "lw $s5, -84($sp)\n" 193 "lw $s6, -88($sp)\n" 194 "lw $s7, -92($sp)\n" 195 "lw $t8, -96($sp)\n" 196 "lw $t9, -100($sp)\n" 197 "lw $gp, -104($sp)\n" 198 "lw $fp, -108($sp)\n" 199 "lw $ra, -112($sp)\n" 200 "lw $k0, -116($sp)\n" 201 "mtc0 $k0, $14\n" /* CP0_EPC */ 202 "nop\n" 203 "jr $ra\n" 204 "nop" 205 : 206 : "r" (current_stack_pointer) 207 ); 208 }