xref: /netbsd/sys/arch/hpcsh/hpcsh/kloader_machdep.c (revision 6550d01e)
1 /*	$NetBSD: kloader_machdep.c,v 1.14 2008/04/28 20:23:22 martin 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.14 2008/04/28 20:23:22 martin 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_hpcsh_jump __attribute__((__noreturn__));
50 kloader_bootfunc_t kloader_hpcsh3_boot __attribute__((__noreturn__));
51 kloader_bootfunc_t kloader_hpcsh4_boot __attribute__((__noreturn__));
52 
53 struct kloader_ops kloader_hpcsh_ops = {
54 	.jump = kloader_hpcsh_jump,
55 	.boot = NULL
56 };
57 
58 
59 void
60 kloader_reboot_setup(const char *filename)
61 {
62 
63 	kloader_hpcsh_ops.boot = CPU_IS_SH3 ? kloader_hpcsh3_boot
64 					    : kloader_hpcsh4_boot;
65 
66 	__kloader_reboot_setup(&kloader_hpcsh_ops, filename);
67 }
68 
69 void
70 kloader_hpcsh_jump(kloader_bootfunc_t func, vaddr_t sp,
71     struct kloader_bootinfo *info, struct kloader_page_tag *tag)
72 {
73 
74 	sh_icache_sync_all();	/* also flushes d-cache */
75 
76 	__asm volatile(
77 	    	"mov	%0, r4;"
78 		"mov	%1, r5;"
79 		"jmp	@%2;"
80 		" mov	%3, sp"
81 		: : "r"(info), "r"(tag), "r"(func), "r"(sp));
82 
83 	/* NOTREACHED */
84 	KLOADER_NORETURN;
85 }
86 
87 /*
88  * 2nd-stage bootloader.  Fetches new kernel out of the page tags
89  * chain and copies it to its intended location in memory.  Make sure
90  * this function is position independent and fits into a single page.
91  */
92 #define KLOADER_HPCSH_BOOT(cpu, product)				\
93 void									\
94 kloader_hpcsh ## cpu ## _boot(struct kloader_bootinfo *kbi,		\
95     struct kloader_page_tag *p)						\
96 {									\
97 	int tmp;							\
98 									\
99 	/* Disable interrupts, block exceptions. */			\
100 	__asm volatile(							\
101 		"stc	sr, %0;"					\
102 		"or	%1, %0;"					\
103 		"ldc	%0, sr"						\
104 		: "=r"(tmp)						\
105 		: "r"(PSL_MD | PSL_BL | PSL_IMASK));			\
106 									\
107 	/* We run on P1, flush and disable TLB. */			\
108 	SH ## cpu ## _TLB_DISABLE;					\
109 									\
110 	do {								\
111 		uint32_t *dst = (uint32_t *)p->dst;			\
112 		uint32_t *src = (uint32_t *)p->src;			\
113 		uint32_t sz = p->sz / sizeof (uint32_t);		\
114 		while (sz--)						\
115 			*dst++ = *src++;				\
116 	} while ((p = (struct kloader_page_tag *)p->next) != 0);	\
117 									\
118 	SH ## product ## _CACHE_FLUSH();				\
119 									\
120 	/* Jump to the kernel entry point. */				\
121 	__asm volatile(							\
122 		"mov	%0, r4;"					\
123 		"mov	%1, r5;"					\
124 		"jmp	@%3;"						\
125 		" mov	%2, r6;"					\
126 		: :							\
127 		"r"(kbi->argc),						\
128 		"r"(kbi->argv),						\
129 		"r"(&kbi->bootinfo),					\
130 		"r"(kbi->entry));					\
131 									\
132 	/* NOTREACHED */						\
133 	KLOADER_NORETURN;						\
134 }
135 
136 #ifdef SH3
137 KLOADER_HPCSH_BOOT(3, 7709A)
138 #endif
139 
140 #ifdef SH4
141 KLOADER_HPCSH_BOOT(4, 7750)
142 #endif
143