paul@62 | 1 | /* |
paul@90 | 2 | * CPU-specific routines originally from U-Boot. |
paul@62 | 3 | * See: uboot-xburst/files/arch/mips/cpu/xburst/cpu.c |
paul@62 | 4 | * See: u-boot/arch/mips/include/asm/cacheops.h |
paul@62 | 5 | * |
paul@62 | 6 | * Copyright (C) 2000-2009 Wolfgang Denk, DENX Software Engineering, <wd@denx.de> |
paul@90 | 7 | * Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@62 | 8 | * |
paul@62 | 9 | * This program is free software; you can redistribute it and/or |
paul@62 | 10 | * modify it under the terms of the GNU General Public License as |
paul@62 | 11 | * published by the Free Software Foundation; either version 2 of |
paul@62 | 12 | * the License, or (at your option) any later version. |
paul@62 | 13 | * |
paul@62 | 14 | * This program is distributed in the hope that it will be useful, |
paul@62 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@62 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@62 | 17 | * GNU General Public License for more details. |
paul@62 | 18 | * |
paul@62 | 19 | * You should have received a copy of the GNU General Public License |
paul@62 | 20 | * along with this program; if not, write to the Free Software |
paul@62 | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@62 | 22 | * Boston, MA 02110-1301, USA |
paul@62 | 23 | */ |
paul@62 | 24 | |
paul@113 | 25 | #include "cpu.h" |
paul@62 | 26 | #include "sdram.h" |
paul@62 | 27 | |
paul@62 | 28 | void flush_icache_all(void) |
paul@62 | 29 | { |
paul@62 | 30 | u32 addr, t = 0; |
paul@62 | 31 | |
paul@62 | 32 | asm volatile ("mtc0 $0, $28"); /* Clear Taglo */ |
paul@62 | 33 | asm volatile ("mtc0 $0, $29"); /* Clear TagHi */ |
paul@62 | 34 | |
paul@62 | 35 | for (addr = KSEG0; addr < KSEG0 + CONFIG_SYS_ICACHE_SIZE; |
paul@62 | 36 | addr += CONFIG_SYS_CACHELINE_SIZE) { |
paul@62 | 37 | asm volatile ( |
paul@62 | 38 | ".set mips3\n\t" |
paul@62 | 39 | " cache %0, 0(%1)\n\t" |
paul@62 | 40 | ".set mips2\n\t" |
paul@62 | 41 | : |
paul@62 | 42 | : "I" (Index_Store_Tag_I), "r"(addr)); |
paul@62 | 43 | } |
paul@62 | 44 | |
paul@62 | 45 | /* invalicate btb */ |
paul@62 | 46 | asm volatile ( |
paul@62 | 47 | ".set mips32\n\t" |
paul@62 | 48 | "mfc0 %0, $16, 7\n\t" |
paul@62 | 49 | "nop\n\t" |
paul@62 | 50 | "ori %0,2\n\t" |
paul@62 | 51 | "mtc0 %0, $16, 7\n\t" |
paul@62 | 52 | ".set mips2\n\t" |
paul@62 | 53 | : |
paul@62 | 54 | : "r" (t)); |
paul@62 | 55 | } |
paul@62 | 56 | |
paul@62 | 57 | void flush_dcache_all(void) |
paul@62 | 58 | { |
paul@62 | 59 | u32 addr; |
paul@62 | 60 | |
paul@62 | 61 | for (addr = KSEG0; addr < KSEG0 + CONFIG_SYS_DCACHE_SIZE; |
paul@62 | 62 | addr += CONFIG_SYS_CACHELINE_SIZE) { |
paul@62 | 63 | asm volatile ( |
paul@62 | 64 | ".set mips3\n\t" |
paul@62 | 65 | " cache %0, 0(%1)\n\t" |
paul@62 | 66 | ".set mips2\n\t" |
paul@62 | 67 | : |
paul@62 | 68 | : "I" (Index_Writeback_Inv_D), "r"(addr)); |
paul@62 | 69 | } |
paul@62 | 70 | |
paul@62 | 71 | asm volatile ("sync"); |
paul@62 | 72 | } |
paul@62 | 73 | |
paul@62 | 74 | void flush_cache_all(void) |
paul@62 | 75 | { |
paul@62 | 76 | flush_dcache_all(); |
paul@62 | 77 | flush_icache_all(); |
paul@62 | 78 | } |
paul@67 | 79 | |
paul@73 | 80 | void handle_error_level(void) |
paul@73 | 81 | { |
paul@73 | 82 | asm volatile( |
paul@117 | 83 | "mfc0 $t3, $12\n" /* CP0_STATUS */ |
paul@117 | 84 | "li $t4, 0xfffffffb\n" /* ERL = 0 */ |
paul@115 | 85 | "and $t3, $t3, $t4\n" |
paul@115 | 86 | "mtc0 $t3, $12\n" |
paul@115 | 87 | "nop\n"); |
paul@115 | 88 | } |
paul@115 | 89 | |
paul@67 | 90 | void enable_interrupts(void) |
paul@67 | 91 | { |
paul@67 | 92 | asm volatile( |
paul@117 | 93 | "mfc0 $t3, $12\n" /* CP0_STATUS */ |
paul@117 | 94 | "li $t4, 0x0000fc01\n" /* IE = enable interrupts */ |
paul@67 | 95 | "or $t3, $t3, $t4\n" |
paul@67 | 96 | "mtc0 $t3, $12\n" |
paul@67 | 97 | "nop\n"); |
paul@67 | 98 | } |
paul@73 | 99 | |
paul@73 | 100 | void init_interrupts(void) |
paul@73 | 101 | { |
paul@73 | 102 | /* Set exception registers. */ |
paul@73 | 103 | |
paul@73 | 104 | asm volatile( |
paul@73 | 105 | "mtc0 $zero, $18\n" /* CP0_WATCHLO */ |
paul@73 | 106 | "li $t3, 0x00800000\n" /* IV = 1 (use 0x80000200 for interrupts) */ |
paul@73 | 107 | "mtc0 $t3, $13\n" /* CP0_CAUSE */ |
paul@111 | 108 | "mfc0 $t4, $12\n" /* CP0_STATUS */ |
paul@111 | 109 | "li $t3, 0xffbfffff\n" /* BEV=0 */ |
paul@84 | 110 | "and $t3, $t3, $t4\n" |
paul@84 | 111 | "mtc0 $t3, $12\n" |
paul@73 | 112 | "nop\n"); |
paul@73 | 113 | } |
paul@73 | 114 | |
paul@133 | 115 | void set_task(u8 asid) |
paul@133 | 116 | { |
paul@133 | 117 | asm volatile( |
paul@133 | 118 | |
paul@133 | 119 | /* Set the ASID. */ |
paul@133 | 120 | |
paul@133 | 121 | "mtc0 %0, $10\n" /* CP0_ENTRYHI */ |
paul@133 | 122 | "nop" |
paul@133 | 123 | : |
paul@133 | 124 | : "r" (asid) |
paul@133 | 125 | ); |
paul@133 | 126 | } |
paul@133 | 127 | |
paul@133 | 128 | void init_stack(u32 top, u32 got, void (*function)(), u32 args[], u8 nargs) |
paul@133 | 129 | { |
paul@133 | 130 | u8 i; |
paul@133 | 131 | |
paul@133 | 132 | asm volatile( |
paul@133 | 133 | "move $t3, %0\n" /* refer to the stack frame */ |
paul@133 | 134 | "addi $t3, $t3, -16\n" /* refer to the first parameter */ |
paul@133 | 135 | : |
paul@133 | 136 | : "r" (top) |
paul@133 | 137 | ); |
paul@133 | 138 | |
paul@133 | 139 | /* Provide arguments to the function. */ |
paul@133 | 140 | |
paul@133 | 141 | for (i = 0; i < nargs; i++) |
paul@133 | 142 | { |
paul@133 | 143 | asm volatile( |
paul@133 | 144 | "sw %0, 0($t3)\n" |
paul@133 | 145 | "addi $t3, $t3, -4\n" |
paul@133 | 146 | : |
paul@133 | 147 | : "r" (args[i]) |
paul@133 | 148 | ); |
paul@133 | 149 | } |
paul@133 | 150 | |
paul@133 | 151 | /* Store essential data for the function environment. */ |
paul@133 | 152 | |
paul@133 | 153 | asm volatile( |
paul@133 | 154 | "subu %1, %1, 0x80000000\n" /* obtain user mode addresses */ |
paul@133 | 155 | "subu %2, %2, 0x80000000\n" |
paul@133 | 156 | "sw %2, -100(%0)\n" /* store the function address */ |
paul@133 | 157 | "sw %1, -104(%0)\n" /* store the global pointer */ |
paul@133 | 158 | "sw %2, -112(%0)\n" /* store the function address */ |
paul@133 | 159 | : |
paul@133 | 160 | : "r" (top), "r" (got), "r" (function) |
paul@133 | 161 | ); |
paul@133 | 162 | } |
paul@133 | 163 | |
paul@117 | 164 | void enter_user_mode(void) |
paul@117 | 165 | { |
paul@117 | 166 | asm volatile( |
paul@117 | 167 | "mfc0 $t3, $12\n" /* CP0_STATUS */ |
paul@124 | 168 | "li $t4, 0x00000010\n" /* KSU = 2 (UM = 1) */ |
paul@124 | 169 | "or $t3, $t3, $t4\n" |
paul@117 | 170 | "mtc0 $t3, $12\n" |
paul@117 | 171 | "nop\n"); |
paul@117 | 172 | } |
paul@117 | 173 | |
paul@73 | 174 | void init_tlb(void) |
paul@73 | 175 | { |
paul@73 | 176 | asm volatile( |
paul@116 | 177 | "li $t1, 1\n" /* index of first randomly-replaced entry */ |
paul@111 | 178 | "mtc0 $t1, $6\n" /* CP0_WIRED */ |
paul@117 | 179 | "nop\n"); |
paul@73 | 180 | |
paul@119 | 181 | map_page_index(0x80000000, 0x00000000, 16 * 1024 * 1024, 0x1f, 0, 0); |
paul@117 | 182 | } |
paul@83 | 183 | |
paul@117 | 184 | void map_page_index(u32 virtual, u32 physical, u32 pagesize, u8 flags, u8 asid, u32 index) |
paul@117 | 185 | { |
paul@130 | 186 | u32 start = (virtual & 0xffffe000) | asid; /* VPN2 | ASID*/ |
paul@117 | 187 | u32 lower = ((physical & 0xfffff000) >> 6) | flags; |
paul@117 | 188 | u32 upper = (((physical + pagesize) & 0xfffff000) >> 6) | flags; |
paul@117 | 189 | u32 pagemask = ((pagesize - 1) & 0xfffff000) << 1; |
paul@117 | 190 | |
paul@117 | 191 | asm volatile( |
paul@117 | 192 | "mtc0 %3, $5\n" /* CP0_PAGEMASK */ |
paul@117 | 193 | |
paul@117 | 194 | /* Set the index. */ |
paul@117 | 195 | |
paul@117 | 196 | "mtc0 %4, $0\n" /* CP0_INDEX */ |
paul@83 | 197 | |
paul@73 | 198 | /* Set physical address. */ |
paul@73 | 199 | |
paul@117 | 200 | "mtc0 %0, $2\n" /* CP0_ENTRYLO0 */ |
paul@117 | 201 | "mtc0 %1, $3\n" /* CP0_ENTRYLO1 */ |
paul@73 | 202 | |
paul@73 | 203 | /* Set virtual address. */ |
paul@73 | 204 | |
paul@130 | 205 | "mtc0 %2, $10\n" /* CP0_ENTRYHI */ |
paul@73 | 206 | "nop\n" |
paul@73 | 207 | |
paul@83 | 208 | "tlbwi\n" |
paul@117 | 209 | "nop" |
paul@117 | 210 | : |
paul@117 | 211 | : "r" (lower), "r" (upper), "r" (start), "r" (pagemask), "r" (index) |
paul@117 | 212 | ); |
paul@113 | 213 | } |
paul@83 | 214 | |
paul@117 | 215 | void map_page(u32 virtual, u32 physical, u32 pagesize, u8 flags, u8 asid) |
paul@113 | 216 | { |
paul@130 | 217 | u32 start = (virtual & 0xffffe000) | asid; /* VPN2 | ASID*/ |
paul@116 | 218 | u32 lower = ((physical & 0xfffff000) >> 6) | flags; |
paul@116 | 219 | u32 upper = (((physical + pagesize) & 0xfffff000) >> 6) | flags; |
paul@113 | 220 | u32 pagemask = ((pagesize - 1) & 0xfffff000) << 1; |
paul@113 | 221 | |
paul@113 | 222 | asm volatile( |
paul@113 | 223 | "mtc0 %3, $5\n" /* CP0_PAGEMASK */ |
paul@83 | 224 | |
paul@83 | 225 | /* Set physical address. */ |
paul@83 | 226 | |
paul@113 | 227 | "mtc0 %0, $2\n" /* CP0_ENTRYLO0 */ |
paul@113 | 228 | "mtc0 %1, $3\n" /* CP0_ENTRYLO1 */ |
paul@83 | 229 | |
paul@83 | 230 | /* Set virtual address. */ |
paul@83 | 231 | |
paul@113 | 232 | "mtc0 %2, $10\n" /* CP0_ENTRYHI */ |
paul@83 | 233 | "nop\n" |
paul@83 | 234 | |
paul@113 | 235 | "tlbwr\n" |
paul@113 | 236 | "nop" |
paul@113 | 237 | : |
paul@113 | 238 | : "r" (lower), "r" (upper), "r" (start), "r" (pagemask) |
paul@113 | 239 | ); |
paul@73 | 240 | } |
paul@114 | 241 | |
paul@135 | 242 | void map_page_miss(u32 physical, u32 pagesize, u8 flags) |
paul@135 | 243 | { |
paul@135 | 244 | u32 lower = ((physical & 0xfffff000) >> 6) | flags; |
paul@135 | 245 | u32 upper = (((physical + pagesize) & 0xfffff000) >> 6) | flags; |
paul@135 | 246 | u32 pagemask = ((pagesize - 1) & 0xfffff000) << 1; |
paul@135 | 247 | |
paul@135 | 248 | asm volatile( |
paul@135 | 249 | "mtc0 %2, $5\n" /* CP0_PAGEMASK */ |
paul@135 | 250 | |
paul@135 | 251 | /* Set physical address. */ |
paul@135 | 252 | |
paul@135 | 253 | "mtc0 %0, $2\n" /* CP0_ENTRYLO0 */ |
paul@135 | 254 | "mtc0 %1, $3\n" /* CP0_ENTRYLO1 */ |
paul@135 | 255 | "nop\n" |
paul@135 | 256 | |
paul@135 | 257 | "tlbwr\n" |
paul@135 | 258 | "nop" |
paul@135 | 259 | : |
paul@135 | 260 | : "r" (lower), "r" (upper), "r" (pagemask) |
paul@135 | 261 | ); |
paul@135 | 262 | } |
paul@135 | 263 | |
paul@117 | 264 | void unmap_page(u32 virtual, u32 physical, u32 pagesize, u8 flags, u8 asid) |
paul@117 | 265 | { |
paul@130 | 266 | u32 start = (virtual & 0xffffe000) | asid; /* VPN2 | ASID*/ |
paul@117 | 267 | u32 lower = ((physical & 0xfffff000) >> 6) | flags; |
paul@117 | 268 | u32 upper = (((physical + pagesize) & 0xfffff000) >> 6) | flags; |
paul@117 | 269 | u32 pagemask = ((pagesize - 1) & 0xfffff000) << 1; |
paul@117 | 270 | u32 index = 0; |
paul@117 | 271 | |
paul@117 | 272 | asm volatile( |
paul@117 | 273 | "mtc0 %4, $5\n" /* CP0_PAGEMASK */ |
paul@117 | 274 | |
paul@117 | 275 | /* Set physical address. */ |
paul@117 | 276 | |
paul@117 | 277 | "mtc0 %1, $2\n" /* CP0_ENTRYLO0 */ |
paul@117 | 278 | "mtc0 %2, $3\n" /* CP0_ENTRYLO1 */ |
paul@117 | 279 | |
paul@117 | 280 | /* Set virtual address. */ |
paul@117 | 281 | |
paul@117 | 282 | "mtc0 %3, $10\n" /* CP0_ENTRYHI */ |
paul@117 | 283 | "nop\n" |
paul@117 | 284 | |
paul@117 | 285 | /* Find an existing mapping. */ |
paul@117 | 286 | |
paul@117 | 287 | "tlbp\n" |
paul@117 | 288 | "nop\n" |
paul@117 | 289 | |
paul@117 | 290 | /* Read the index register to see if a match was found. */ |
paul@117 | 291 | |
paul@117 | 292 | "mfc0 %0, $0\n" /* CP0_INDEX */ |
paul@117 | 293 | "nop" |
paul@117 | 294 | : "=r" (index) |
paul@117 | 295 | : "r" (lower), "r" (upper), "r" (start), "r" (pagemask) |
paul@117 | 296 | ); |
paul@117 | 297 | |
paul@117 | 298 | /* Return if the page is not mapped. */ |
paul@117 | 299 | |
paul@117 | 300 | if (index & 0x80000000) |
paul@117 | 301 | return; |
paul@117 | 302 | |
paul@117 | 303 | /* Otherwise, invalidate the mapping. */ |
paul@117 | 304 | |
paul@117 | 305 | map_page_index(virtual, physical, pagesize, flags & 0xfd, asid, index); |
paul@117 | 306 | } |