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