1 /* $NetBSD: kloader_machdep.c,v 1.1 2010/04/06 15:54:30 nonaka Exp $ */ 2 3 /*- 4 * Copyright (c) 2001, 2002, 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: kloader_machdep.c,v 1.1 2010/04/06 15:54:30 nonaka Exp $"); 31 32 #include "debug_kloader.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 37 #include <sh3/mmu.h> 38 #include <sh3/mmu_sh3.h> 39 #include <sh3/mmu_sh4.h> 40 #include <sh3/cache.h> 41 #include <sh3/cache_sh3.h> 42 #include <sh3/cache_sh4.h> 43 44 #include <machine/kloader.h> 45 46 /* make gcc believe __attribute__((__noreturn__)) claim */ 47 #define KLOADER_NORETURN for (;;) continue 48 49 kloader_jumpfunc_t kloader_evbsh3_jump __attribute__((__noreturn__)); 50 kloader_bootfunc_t kloader_evbsh3_sh3_boot __attribute__((__noreturn__)); 51 kloader_bootfunc_t kloader_evbsh3_sh4_boot __attribute__((__noreturn__)); 52 53 static struct kloader_ops kloader_evbsh3_ops = { 54 .jump = kloader_evbsh3_jump, 55 .boot = NULL 56 }; 57 58 void 59 kloader_reboot_setup(const char *filename) 60 { 61 62 #if defined(SH3) && defined(SH4) 63 #error "don't define both SH3 and SH4" 64 #elif defined(SH3) 65 kloader_evbsh3_ops.boot = kloader_evbsh3_sh3_boot; 66 #elif defined(SH4) 67 kloader_evbsh3_ops.boot = kloader_evbsh3_sh4_boot; 68 #endif 69 70 __kloader_reboot_setup(&kloader_evbsh3_ops, filename); 71 } 72 73 void 74 kloader_evbsh3_jump(kloader_bootfunc_t func, vaddr_t sp, 75 struct kloader_bootinfo *info, struct kloader_page_tag *tag) 76 { 77 78 sh_icache_sync_all(); /* also flushes d-cache */ 79 80 __asm volatile( 81 "mov %0, r4;" 82 "mov %1, r5;" 83 "jmp @%2;" 84 " mov %3, sp" 85 : : "r"(info), "r"(tag), "r"(func), "r"(sp)); 86 87 /* NOTREACHED */ 88 KLOADER_NORETURN; 89 } 90 91 /* 92 * 2nd-stage bootloader. Fetches new kernel out of the page tags 93 * chain and copies it to its intended location in memory. Make sure 94 * this function is position independent and fits into a single page. 95 */ 96 #define KLOADER_EVBSH3_BOOT(cpu, product) \ 97 void \ 98 kloader_evbsh3_sh ## cpu ## _boot(struct kloader_bootinfo *kbi, \ 99 struct kloader_page_tag *p) \ 100 { \ 101 int tmp; \ 102 \ 103 /* Disable interrupts, block exceptions. */ \ 104 __asm volatile( \ 105 "stc sr, %0;" \ 106 "or %1, %0;" \ 107 "ldc %0, sr" \ 108 : "=r"(tmp) \ 109 : "r"(PSL_MD | PSL_BL | PSL_IMASK)); \ 110 \ 111 /* We run on P1, flush and disable TLB. */ \ 112 SH ## cpu ## _TLB_DISABLE; \ 113 \ 114 do { \ 115 uint32_t *dst = (uint32_t *)p->dst; \ 116 uint32_t *src = (uint32_t *)p->src; \ 117 uint32_t sz = p->sz / sizeof (uint32_t); \ 118 while (sz--) \ 119 *dst++ = *src++; \ 120 } while ((p = (struct kloader_page_tag *)p->next) != 0); \ 121 \ 122 SH ## product ## _CACHE_FLUSH(); \ 123 \ 124 /* Jump to the kernel entry point. */ \ 125 __asm volatile( \ 126 "jmp @%0;" \ 127 " nop;" \ 128 : : "r"(kbi->entry)); \ 129 \ 130 /* NOTREACHED */ \ 131 KLOADER_NORETURN; \ 132 } 133 134 #ifdef SH3 135 #if defined(SH7708) 136 KLOADER_EVBSH3_BOOT(3, 7708A) 137 #elif defined(SH7708S) 138 KLOADER_EVBSH3_BOOT(3, 7708S) 139 #elif defined(SH7708R) 140 KLOADER_EVBSH3_BOOT(3, 7708R) 141 #elif defined(SH7709) 142 KLOADER_EVBSH3_BOOT(3, 7709) 143 #elif defined(SH7709A) 144 KLOADER_EVBSH3_BOOT(3, 7709A) 145 #elif defined(SH7706) 146 KLOADER_EVBSH3_BOOT(3, 7706) 147 #else 148 #error "unsupported SH3 variants" 149 #endif 150 #endif 151 152 #ifdef SH4 153 #if defined(SH7750) 154 KLOADER_EVBSH3_BOOT(4, 7750) 155 #elif defined(SH7750S) 156 KLOADER_EVBSH3_BOOT(4, 7750S) 157 #elif defined(SH7750R) 158 KLOADER_EVBSH3_BOOT(4, 7750R) 159 #elif defined(SH7751) 160 KLOADER_EVBSH3_BOOT(4, 7751) 161 #elif defined(SH7751R) 162 KLOADER_EVBSH3_BOOT(4, 7751R) 163 #else 164 #error "unsupported SH4 variants" 165 #endif 166 #endif 167