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