1/*	$NetBSD: boot.c,v 1.7 2022/02/16 23:49:27 riastradh 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
47char *names[] = {
48	"in()",
49};
50#define	NUMNAMES (sizeof (names) / sizeof (names[0]))
51
52#define	NAMELEN	128
53char namebuf[NAMELEN];
54char nametmp[NAMELEN];
55
56unsigned char cregs[10];
57
58char bootinfo[BOOTINFO_MAXSIZE];
59struct btinfo_iplcb btinfo_iplcb;
60struct btinfo_console btinfo_console;
61
62/*struct ipl_cb iplcb;
63struct ipl_directory ipldir;*/
64
65extern u_long ns_per_tick;
66extern char bootprog_name[], bootprog_rev[];
67
68void boot(void *, void *);
69static 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
95static void
96setled(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" ::: "memory");
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#endif
138}
139
140
141void
142boot(void *iplcb_p, void *extiplcb_p)
143{
144	extern char _end[], _edata[];
145	int n = 0;
146	int addr, speed;
147	/*unsigned int cpuvers;*/
148	char *name, *cnname, *p;
149	struct ipl_cb *iplcb_ptr;
150	struct ipl_directory *dirp;
151	struct ipl_info *infop;
152
153	//setled(0x30100000); /* we have control */
154	//for (;;);
155	iplcb_ptr = (struct ipl_cb *)iplcb_p;
156	dirp = &(iplcb_ptr->dir);
157
158	/* Clear all of BSS */
159	memset(_edata, 0, _end - _edata);
160
161	/*
162	 * console init
163	 */
164	//setled(0x30000000); /* attempting r14 setup */
165	setup_iocc();
166	//setled(0x31000000); /* attempting console init */
167	cnname = cninit(&addr, &speed);
168	printf("\n");
169	//setled(0x31100000); /* we have the console */
170
171	printf("IPLCB ptr = %p\n", iplcb_p);
172
173	infop = (struct ipl_info *)((char *)iplcb_p + dirp->iplinfo_off);
174	printf("Machine model = 0x%x\n", infop->model);
175	printf("RAM = 0x%x\n", infop->ram_size);
176
177	//dump_iplcb(iplcb_p);
178
179	/* make bootinfo */
180	/*
181	 * ipl control block
182	 */
183	btinfo_iplcb.common.next = sizeof(btinfo_iplcb);
184	btinfo_iplcb.common.type = BTINFO_IPLCB;
185	if (iplcb_ptr) {
186		btinfo_iplcb.addr = (void *)iplcb_p;
187	} else {
188		printf("Warning: no IPL Control Block.\n");
189		btinfo_iplcb.addr = 0;
190	}
191
192	/*
193	 * console
194	 */
195	btinfo_console.common.next = sizeof(btinfo_console);
196	btinfo_console.common.type = BTINFO_CONSOLE;
197	strcpy(btinfo_console.devname, cnname);
198	btinfo_console.addr = addr;
199	btinfo_console.speed = speed;
200
201	p = bootinfo;
202        memcpy(p, (void *)&btinfo_iplcb, sizeof(btinfo_iplcb));
203        p += sizeof(btinfo_iplcb);
204        memcpy(p, (void *)&btinfo_console, sizeof(btinfo_console));
205        p += sizeof(btinfo_console);
206
207	/*
208	 * load kernel if attached
209	 */
210	init_in(RELOC);
211
212	setled(0x38000000); /* attempting boot */
213	printf("\n");
214	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
215
216	for (;;) {
217		name = names[n++];
218		if (n >= NUMNAMES)
219			n = 0;
220		setled(0x38100000 + (0x100000 * n));
221		exec_kernel(name);
222		setled(0x39900000); /* boot failed! */
223	}
224}
225
226/*
227 * Exec kernel
228 */
229static void
230exec_kernel(char *name)
231{
232	int howto = 0;
233	char c, *ptr;
234	u_long marks[MARK_MAX];
235#ifdef DBMONITOR
236	int go_monitor;
237	extern int db_monitor(void);
238
239ret:
240#endif /* DBMONITOR */
241	printf("\nBoot: ");
242	memset(namebuf, 0, sizeof (namebuf));
243	if (tgets(namebuf) == -1)
244		printf("\n");
245
246	ptr = namebuf;
247#ifdef DBMONITOR
248	go_monitor = 0;
249	if (*ptr == '!') {
250		if (*(++ptr) == NULL) {
251			db_monitor();
252			printf("\n");
253			goto ret;
254		} else {
255			go_monitor++;
256		}
257	}
258#endif /* DBMONITOR */
259	while ((c = *ptr)) {
260		while (c == ' ')
261			c = *++ptr;
262		if (!c)
263			goto next;
264		if (c == '-') {
265			while ((c = *++ptr) && c != ' ')
266				BOOT_FLAG(c, howto);
267		} else {
268			name = ptr;
269			while ((c = *++ptr) && c != ' ');
270			if (c)
271				*ptr++ = 0;
272		}
273	}
274
275next:
276	printf("Loading %s", name);
277	if (howto)
278		printf(" (howto 0x%x)", howto);
279	printf("\n");
280
281	marks[MARK_START] = 0;
282	if (loadfile(name, marks, LOAD_ALL) == 0) {
283#ifdef DBMONITOR
284		if (go_monitor) {
285			db_monitor();
286			printf("\n");
287		}
288#endif /* DBMONITOR */
289
290		printf("start=0x%lx\n\n", marks[MARK_ENTRY]);
291		delay(1000);
292		__syncicache((void *)marks[MARK_ENTRY],
293			(u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
294		printf("About to run\n");
295		run((void *)marks[MARK_SYM],
296		    (void *)marks[MARK_END],
297		    (void *)howto,
298		    (void *)bootinfo,
299		    (void *)marks[MARK_ENTRY]);
300	}
301}
302