Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.1
      1 /*	$NetBSD: boot.c,v 1.1 2007/12/17 19:09:45 garbled 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/residual.h>
     42 #include <powerpc/spr.h>
     43 
     44 #include "boot.h"
     45 #include "iplcb.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[], bootprog_maker[], bootprog_date[];
     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 = 0x%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 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    217 
    218 	for (;;) {
    219 		name = names[n++];
    220 		if (n >= NUMNAMES)
    221 			n = 0;
    222 		setled(0x38100000 + (0x100000 * n));
    223 		exec_kernel(name);
    224 		setled(0x39900000); /* boot failed! */
    225 	}
    226 }
    227 
    228 /*
    229  * Exec kernel
    230  */
    231 static void
    232 exec_kernel(char *name)
    233 {
    234 	int howto = 0;
    235 	char c, *ptr;
    236 	u_long marks[MARK_MAX];
    237 #ifdef DBMONITOR
    238 	int go_monitor;
    239 	extern int db_monitor __P((void));
    240 
    241 ret:
    242 #endif /* DBMONITOR */
    243 	printf("\nBoot: ");
    244 	memset(namebuf, 0, sizeof (namebuf));
    245 	if (tgets(namebuf) == -1)
    246 		printf("\n");
    247 
    248 	ptr = namebuf;
    249 #ifdef DBMONITOR
    250 	go_monitor = 0;
    251 	if (*ptr == '!') {
    252 		if (*(++ptr) == NULL) {
    253 			db_monitor();
    254 			printf("\n");
    255 			goto ret;
    256 		} else {
    257 			go_monitor++;
    258 		}
    259 	}
    260 #endif /* DBMONITOR */
    261 	while ((c = *ptr)) {
    262 		while (c == ' ')
    263 			c = *++ptr;
    264 		if (!c)
    265 			goto next;
    266 		if (c == '-') {
    267 			while ((c = *++ptr) && c != ' ')
    268 				BOOT_FLAG(c, howto);
    269 		} else {
    270 			name = ptr;
    271 			while ((c = *++ptr) && c != ' ');
    272 			if (c)
    273 				*ptr++ = 0;
    274 		}
    275 	}
    276 
    277 next:
    278 	printf("Loading %s", name);
    279 	if (howto)
    280 		printf(" (howto 0x%x)", howto);
    281 	printf("\n");
    282 
    283 	marks[MARK_START] = 0;
    284 	if (loadfile(name, marks, LOAD_ALL) == 0) {
    285 #ifdef DBMONITOR
    286 		if (go_monitor) {
    287 			db_monitor();
    288 			printf("\n");
    289 		}
    290 #endif /* DBMONITOR */
    291 
    292 		printf("start=0x%lx\n\n", marks[MARK_ENTRY]);
    293 		delay(1000);
    294 		__syncicache((void *)marks[MARK_ENTRY],
    295 			(u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
    296 		printf("About to run\n");
    297 		run((void *)marks[MARK_SYM],
    298 		    (void *)marks[MARK_END],
    299 		    (void *)howto,
    300 		    (void *)bootinfo,
    301 		    (void *)marks[MARK_ENTRY]);
    302 	}
    303 }
    304