xref: /netbsd-src/sys/arch/rs6000/stand/boot/boot.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: boot.c,v 1.6 2020/02/24 12:20:29 rin Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <lib/libsa/stand.h>
35 #include <lib/libsa/loadfile.h>
36 #include <lib/libkern/libkern.h>
37 #include <sys/reboot.h>
38 #include <sys/boot_flag.h>
39 #include <machine/bootinfo.h>
40 #include <machine/cpu.h>
41 #include <machine/iplcb.h>
42 #include <powerpc/spr.h>
43 #include <powerpc/oea/spr.h>
44 
45 #include "boot.h"
46 
47 char *names[] = {
48 	"in()",
49 };
50 #define	NUMNAMES (sizeof (names) / sizeof (names[0]))
51 
52 #define	NAMELEN	128
53 char namebuf[NAMELEN];
54 char nametmp[NAMELEN];
55 
56 unsigned char cregs[10];
57 
58 char bootinfo[BOOTINFO_MAXSIZE];
59 struct btinfo_iplcb btinfo_iplcb;
60 struct btinfo_console btinfo_console;
61 
62 /*struct ipl_cb iplcb;
63 struct ipl_directory ipldir;*/
64 
65 extern u_long ns_per_tick;
66 extern char bootprog_name[], bootprog_rev[];
67 
68 void boot(void *, void *);
69 static void exec_kernel(char *);
70 
71 #define	PSL_DR	(1<<4)
72 #define PSL_EE	0x00008000
73 #define BAT_BL_128K	0x00000000
74 #define BAT_BL_1M	0x0000001c
75 #define BAT_BL_2M	0x0000003c
76 #define BAT_W		0x00000040
77 #define BAT_I		0x00000020
78 #define BAT_M		0x00000010
79 #define BAT_G		0x00000008
80 #define BAT_X		0x00000004
81 #define BAT_Vs		0x00000002
82 #define BAT_Vu		0x00000001
83 #define BAT_PP_RW	0x00000002
84 #define BAT_RPN		(~0x1ffff)
85 #define BAT_EPI		(~0x1ffffL)
86 #define BAT_V		(BAT_Vs|BAT_Vu)
87 #define BAT_BL		0x00001ffc
88 
89 #define BATU(va, len, v)	\
90 	(((va) & BAT_EPI) | ((len) & BAT_BL) | ((v) & BAT_V))
91 #define BATL(pa, wimg, pp)	\
92 	(((pa) & BAT_RPN) | (wimg) | (pp))
93 
94 
95 static void
96 setled(uint32_t val)
97 {
98 #ifdef POWER
99 	register_t savemsr, msr, savesr15;
100 
101 	__asm volatile ("mfmsr %0" : "=r"(savemsr));
102 	msr = savemsr & ~PSL_DR;
103 	__asm volatile ("mtmsr %0" : : "r"(msr));
104 
105 	__asm volatile ("mfsr %0,15;isync" : "=r"(savesr15));
106 	__asm volatile ("mtsr 15,%0" : : "r"(0x82040080));
107 	__asm volatile ("mtmsr %0" : : "r"(msr|PSL_DR));
108 	__asm volatile ("isync");
109 	*(uint32_t *)0xF0A00300 = val;
110 	__asm volatile ("mtmsr %0" : : "r"(savemsr));
111 	__asm volatile ("mtsr 15,%0;isync" : : "r"(savesr15));
112 #else
113 #ifdef NOTYET
114 	register_t savemsr, msr, batu, batl;
115 
116 	/* turn on DR */
117 	__asm volatile ("mfmsr %0" : "=r"(savemsr));
118 	msr = savemsr|PSL_DR;
119 	__asm volatile ("mtmsr %0" : : "r"(msr));
120 	__asm volatile ("isync");
121 
122 	/* set up a bat and map the whole NVRAM chunk */
123 	batl = BATL(0xFF600000, BAT_I|BAT_G, BAT_PP_RW);
124 	batu = BATU(0xFF600000, BAT_BL_1M, BAT_Vs);
125 	__asm volatile ("mtdbatl 1,%0; mtdbatu 1,%1;"
126 	    ::  "r"(batl), "r"(batu));
127 	__asm volatile ("isync");
128 
129 	*(volatile uint32_t *)0xFF600300 = val;
130 	__asm volatile ("eieio");
131 	__asm volatile ("isync");
132 
133 	/* put back to normal */
134 	__asm volatile ("mtmsr %0" : : "r"(savemsr));
135 	__asm volatile ("isync");
136 #endif /* NOTYET */
137 
138 #endif
139 }
140 
141 
142 void
143 boot(void *iplcb_p, void *extiplcb_p)
144 {
145 	extern char _end[], _edata[];
146 	int n = 0;
147 	int addr, speed;
148 	/*unsigned int cpuvers;*/
149 	char *name, *cnname, *p;
150 	struct ipl_cb *iplcb_ptr;
151 	struct ipl_directory *dirp;
152 	struct ipl_info *infop;
153 
154 	//setled(0x30100000); /* we have control */
155 	//for (;;);
156 	iplcb_ptr = (struct ipl_cb *)iplcb_p;
157 	dirp = &(iplcb_ptr->dir);
158 
159 	/* Clear all of BSS */
160 	memset(_edata, 0, _end - _edata);
161 
162 	/*
163 	 * console init
164 	 */
165 	//setled(0x30000000); /* attempting r14 setup */
166 	setup_iocc();
167 	//setled(0x31000000); /* attempting console init */
168 	cnname = cninit(&addr, &speed);
169 	printf("\n");
170 	//setled(0x31100000); /* we have the console */
171 
172 	printf("IPLCB ptr = %p\n", iplcb_p);
173 
174 	infop = (struct ipl_info *)((char *)iplcb_p + dirp->iplinfo_off);
175 	printf("Machine model = 0x%x\n", infop->model);
176 	printf("RAM = 0x%x\n", infop->ram_size);
177 
178 	//dump_iplcb(iplcb_p);
179 
180 	/* make bootinfo */
181 	/*
182 	 * ipl control block
183 	 */
184 	btinfo_iplcb.common.next = sizeof(btinfo_iplcb);
185 	btinfo_iplcb.common.type = BTINFO_IPLCB;
186 	if (iplcb_ptr) {
187 		btinfo_iplcb.addr = (void *)iplcb_p;
188 	} else {
189 		printf("Warning: no IPL Control Block.\n");
190 		btinfo_iplcb.addr = 0;
191 	}
192 
193 	/*
194 	 * console
195 	 */
196 	btinfo_console.common.next = sizeof(btinfo_console);
197 	btinfo_console.common.type = BTINFO_CONSOLE;
198 	strcpy(btinfo_console.devname, cnname);
199 	btinfo_console.addr = addr;
200 	btinfo_console.speed = speed;
201 
202 	p = bootinfo;
203         memcpy(p, (void *)&btinfo_iplcb, sizeof(btinfo_iplcb));
204         p += sizeof(btinfo_iplcb);
205         memcpy(p, (void *)&btinfo_console, sizeof(btinfo_console));
206         p += sizeof(btinfo_console);
207 
208 	/*
209 	 * load kernel if attached
210 	 */
211 	init_in(RELOC);
212 
213 	setled(0x38000000); /* attempting boot */
214 	printf("\n");
215 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
216 
217 	for (;;) {
218 		name = names[n++];
219 		if (n >= NUMNAMES)
220 			n = 0;
221 		setled(0x38100000 + (0x100000 * n));
222 		exec_kernel(name);
223 		setled(0x39900000); /* boot failed! */
224 	}
225 }
226 
227 /*
228  * Exec kernel
229  */
230 static void
231 exec_kernel(char *name)
232 {
233 	int howto = 0;
234 	char c, *ptr;
235 	u_long marks[MARK_MAX];
236 #ifdef DBMONITOR
237 	int go_monitor;
238 	extern int db_monitor(void);
239 
240 ret:
241 #endif /* DBMONITOR */
242 	printf("\nBoot: ");
243 	memset(namebuf, 0, sizeof (namebuf));
244 	if (tgets(namebuf) == -1)
245 		printf("\n");
246 
247 	ptr = namebuf;
248 #ifdef DBMONITOR
249 	go_monitor = 0;
250 	if (*ptr == '!') {
251 		if (*(++ptr) == NULL) {
252 			db_monitor();
253 			printf("\n");
254 			goto ret;
255 		} else {
256 			go_monitor++;
257 		}
258 	}
259 #endif /* DBMONITOR */
260 	while ((c = *ptr)) {
261 		while (c == ' ')
262 			c = *++ptr;
263 		if (!c)
264 			goto next;
265 		if (c == '-') {
266 			while ((c = *++ptr) && c != ' ')
267 				BOOT_FLAG(c, howto);
268 		} else {
269 			name = ptr;
270 			while ((c = *++ptr) && c != ' ');
271 			if (c)
272 				*ptr++ = 0;
273 		}
274 	}
275 
276 next:
277 	printf("Loading %s", name);
278 	if (howto)
279 		printf(" (howto 0x%x)", howto);
280 	printf("\n");
281 
282 	marks[MARK_START] = 0;
283 	if (loadfile(name, marks, LOAD_ALL) == 0) {
284 #ifdef DBMONITOR
285 		if (go_monitor) {
286 			db_monitor();
287 			printf("\n");
288 		}
289 #endif /* DBMONITOR */
290 
291 		printf("start=0x%lx\n\n", marks[MARK_ENTRY]);
292 		delay(1000);
293 		__syncicache((void *)marks[MARK_ENTRY],
294 			(u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
295 		printf("About to run\n");
296 		run((void *)marks[MARK_SYM],
297 		    (void *)marks[MARK_END],
298 		    (void *)howto,
299 		    (void *)bootinfo,
300 		    (void *)marks[MARK_ENTRY]);
301 	}
302 }
303