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