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