Home | History | Annotate | Line # | Download | only in gdium
machdep.c revision 1.2
      1 /*	$NetBSD: machdep.c,v 1.2 2009/08/06 16:37:01 matt Exp $	*/
      2 
      3 /*
      4  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1992, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * This code is derived from software contributed to Berkeley by
     43  * the Systems Programming Group of the University of Utah Computer
     44  * Science Department, The Mach Operating System project at
     45  * Carnegie-Mellon University and Ralph Campbell.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     72  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     73  */
     74 /*
     75  * Copyright (c) 1988 University of Utah.
     76  *
     77  * This code is derived from software contributed to Berkeley by
     78  * the Systems Programming Group of the University of Utah Computer
     79  * Science Department, The Mach Operating System project at
     80  * Carnegie-Mellon University and Ralph Campbell.
     81  *
     82  * Redistribution and use in source and binary forms, with or without
     83  * modification, are permitted provided that the following conditions
     84  * are met:
     85  * 1. Redistributions of source code must retain the above copyright
     86  *    notice, this list of conditions and the following disclaimer.
     87  * 2. Redistributions in binary form must reproduce the above copyright
     88  *    notice, this list of conditions and the following disclaimer in the
     89  *    documentation and/or other materials provided with the distribution.
     90  * 3. All advertising materials mentioning features or use of this software
     91  *    must display the following acknowledgement:
     92  *	This product includes software developed by the University of
     93  *	California, Berkeley and its contributors.
     94  * 4. Neither the name of the University nor the names of its contributors
     95  *    may be used to endorse or promote products derived from this software
     96  *    without specific prior written permission.
     97  *
     98  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     99  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    101  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    102  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    103  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    104  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    108  * SUCH DAMAGE.
    109  *
    110  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
    111  *	from: Utah Hdr: machdep.c 1.63 91/04/24
    112  */
    113 
    114 #include <sys/cdefs.h>
    115 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.2 2009/08/06 16:37:01 matt Exp $");
    116 
    117 #include "opt_ddb.h"
    118 #include "opt_execfmt.h"
    119 #include "opt_modular.h"
    120 
    121 #include <sys/param.h>
    122 #include <sys/systm.h>
    123 #include <sys/kernel.h>
    124 #include <sys/buf.h>
    125 #include <sys/reboot.h>
    126 #include <sys/user.h>
    127 #include <sys/mount.h>
    128 #include <sys/kcore.h>
    129 #include <sys/boot_flag.h>
    130 #include <sys/termios.h>
    131 #include <sys/ksyms.h>
    132 #include <sys/device.h>
    133 
    134 #include <uvm/uvm_extern.h>
    135 
    136 #include <dev/cons.h>
    137 
    138 #include "ksyms.h"
    139 
    140 #if NKSYMS || defined(DDB) || defined(MODULAR)
    141 #include <machine/db_machdep.h>
    142 #include <ddb/db_extern.h>
    143 #endif
    144 
    145 #include <machine/cpu.h>
    146 #include <machine/psl.h>
    147 
    148 #include <mips/bonito/bonitoreg.h>
    149 #include <evbmips/gdium/gdiumvar.h>
    150 
    151 #include "com.h"
    152 #if NCOM > 0
    153 #include <dev/ic/comreg.h>
    154 #include <dev/ic/comvar.h>
    155 
    156 int	comcnrate = 38400;	/* XXX should be config option */
    157 #endif /* NCOM > 0 */
    158 
    159 struct gdium_config gdium_configuration = {
    160 	.gc_bonito = {
    161 		.bc_adbase = 11,	/* magic */
    162 	},
    163 };
    164 
    165 /* For sysctl_hw. */
    166 extern char cpu_model[];
    167 
    168 /* Our exported CPU info; we can have only one. */
    169 struct cpu_info cpu_info_store;
    170 
    171 /* Maps for VM objects. */
    172 struct vm_map *mb_map = NULL;
    173 struct vm_map *phys_map = NULL;
    174 
    175 int	physmem;		/* Total physical memory */
    176 int	netboot;		/* Are we netbooting? */
    177 
    178 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    179 int mem_cluster_cnt;
    180 
    181 void	configure(void);
    182 void	mach_init(int, char **, char **, void *);
    183 
    184 /*
    185  * safepri is a safe priority for sleep to set for a spin-wait during
    186  * autoconfiguration or after a panic.  Used as an argument to splx().
    187  */
    188 int	safepri = MIPS1_PSL_LOWIPL;
    189 
    190 extern struct user *proc0paddr;
    191 
    192 /*
    193  * Do all the stuff that locore normally does before calling main().
    194  */
    195 void
    196 mach_init(int argc, char **argv, char **evnp, void *syms)
    197 {
    198 	struct gdium_config *gc = &gdium_configuration;
    199 	void *kernend, *v;
    200         u_long first, last;
    201 	char *cp;
    202 	int freqok, i, howto;
    203 	psize_t memsize;
    204 
    205 	extern char edata[], end[];
    206 
    207 	/*
    208 	 * Clear the BSS segment.
    209 	 */
    210 	kernend = (void *)mips_round_page(end);
    211 	memset(edata, 0, (char *)kernend - edata);
    212 
    213 	/*
    214 	 * Set up the exception vectors and CPU-specific function
    215 	 * vectors early on.  We need the wbflush() vector set up
    216 	 * before comcnattach() is called (or at least before the
    217 	 * first printf() after that is called).
    218 	 * Also clears the I+D caches.
    219 	 */
    220 	mips_vector_init();
    221 
    222 	/* set the VM page size */
    223 	uvm_setpagesize();
    224 
    225 	memsize = 256*1024*1024;
    226 	physmem = btoc(memsize);
    227 
    228 	bonito_pci_init(&gc->gc_pc, &gc->gc_bonito);
    229 	gdium_bus_io_init(&gc->gc_iot, gc);
    230 	gdium_bus_mem_init(&gc->gc_memt, gc);
    231 	gdium_dma_init(gc);
    232 	gdium_cnattach(gc);
    233 
    234 	/*
    235 	 * Calibrate the timer if YAMON failed to tell us.
    236 	 */
    237 	if (!freqok) {
    238 #if 0
    239 		bus_space_handle_t sh;
    240 		bus_space_map(&gc->gc_iot, MALTA_RTCADR, 2, 0, &sh);
    241 		malta_cal_timer(&gc->gc_iot, sh);
    242 		bus_space_unmap(&gc->gc_iot, sh, 2);
    243 #endif
    244 	}
    245 
    246 #if NCOM > 0
    247 	/*
    248 	 * Delay to allow firmware putchars to complete.
    249 	 * FIFO depth * character time.
    250 	 * character time = (1000000 / (defaultrate / 10))
    251 	 */
    252 	delay(160000000 / comcnrate);
    253 	if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
    254 	    COM_FREQ, COM_TYPE_NORMAL,
    255 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    256 		panic("malta: unable to initialize serial console");
    257 #endif /* NCOM > 0 */
    258 
    259 	mem_clusters[0].start = 0;
    260 	mem_clusters[0].size = ctob(physmem);
    261 	mem_cluster_cnt = 1;
    262 
    263 	strcpy(cpu_model, "Gdium Liberty 1000");
    264 
    265 	/*
    266 	 * XXX: check argv[0] - do something if "gdb"???
    267 	 */
    268 
    269 	/*
    270 	 * Look at arguments passed to us and compute boothowto.
    271 	 */
    272 	boothowto = RB_AUTOBOOT;
    273 	for (i = 1; i < argc; i++) {
    274 		for (cp = argv[i]; *cp; cp++) {
    275 			/* Ignore superfluous '-', if there is one */
    276 			if (*cp == '-')
    277 				continue;
    278 
    279 			howto = 0;
    280 			BOOT_FLAG(*cp, howto);
    281 			if (! howto)
    282 				printf("bootflag '%c' not recognised\n", *cp);
    283 			else
    284 				boothowto |= howto;
    285 		}
    286 	}
    287 
    288 	/*
    289 	 * Load the rest of the available pages into the VM system.
    290 	 */
    291 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    292 	last = mem_clusters[0].start + mem_clusters[0].size;
    293 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    294 		VM_FREELIST_DEFAULT);
    295 
    296 	/*
    297 	 * Initialize error message buffer (at end of core).
    298 	 */
    299 	mips_init_msgbuf();
    300 
    301 	pmap_bootstrap();
    302 
    303 	/*
    304 	 * Allocate space for proc0's USPACE.
    305 	 */
    306 	v = (void *)uvm_pageboot_alloc(USPACE);
    307 	lwp0.l_addr = proc0paddr = (struct user *)v;
    308 	lwp0.l_md.md_regs = (struct frame *)((char *)v + USPACE) - 1;
    309 	proc0paddr->u_pcb.pcb_context[11] =
    310 	    MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    311 
    312 	/*
    313 	 * Initialize debuggers, and break into them, if appropriate.
    314 	 */
    315 #if defined(DDB)
    316 	if (boothowto & RB_KDB)
    317 		Debugger();
    318 #endif
    319 }
    320 
    321 void
    322 consinit(void)
    323 {
    324 
    325 	/*
    326 	 * Everything related to console initialization is done
    327 	 * in mach_init().
    328 	 */
    329 }
    330 
    331 /*
    332  * Allocate memory for variable-sized tables,
    333  */
    334 void
    335 cpu_startup(void)
    336 {
    337 	vaddr_t minaddr, maxaddr;
    338 	char pbuf[9];
    339 
    340 	/*
    341 	 * Good {morning,afternoon,evening,night}.
    342 	 */
    343 	printf("%s%s", copyright, version);
    344 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    345 	printf("total memory = %s\n", pbuf);
    346 
    347 	/*
    348 	 * Virtual memory is bootstrapped -- notify the bus spaces
    349 	 * that memory allocation is now safe.
    350 	 */
    351 	gdium_configuration.gc_mallocsafe = 1;
    352 
    353 	minaddr = 0;
    354 	/*
    355 	 * Allocate a submap for physio.
    356 	 */
    357 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    358 				    VM_PHYS_SIZE, 0, FALSE, NULL);
    359 
    360 	/*
    361 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
    362 	 * are allocated via the pool allocator, and we use KSEG to
    363 	 * map those pages.)
    364 	 */
    365 
    366 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    367 	printf("avail memory = %s\n", pbuf);
    368 }
    369 
    370 int	waittime = -1;
    371 
    372 void
    373 cpu_reboot(int howto, char *bootstr)
    374 {
    375 
    376 	/* Take a snapshot before clobbering any registers. */
    377 	if (curproc)
    378 		savectx((struct user *)curpcb);
    379 
    380 	if (cold) {
    381 		howto |= RB_HALT;
    382 		goto haltsys;
    383 	}
    384 
    385 	/* If "always halt" was specified as a boot flag, obey. */
    386 	if (boothowto & RB_HALT)
    387 		howto |= RB_HALT;
    388 
    389 	boothowto = howto;
    390 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    391 		waittime = 0;
    392 		vfs_shutdown();
    393 
    394 		/*
    395 		 * If we've been adjusting the clock, the todr
    396 		 * will be out of synch; adjust it now.
    397 		 */
    398 		resettodr();
    399 	}
    400 
    401 	splhigh();
    402 
    403 	if (howto & RB_DUMP)
    404 		dumpsys();
    405 
    406 haltsys:
    407 	doshutdownhooks();
    408 
    409 	pmf_system_shutdown(boothowto);
    410 
    411 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    412 		/*
    413 		 * Turning on GPIO1 as output will cause a powerdown.
    414 		 */
    415 		REGVAL(BONITO_GPIODATA) |= 2;
    416 		REGVAL(BONITO_GPIOIE) &= ~2;
    417 	}
    418 
    419 	if (howto & RB_HALT) {
    420 		printf("\n");
    421 		printf("The operating system has halted.\n");
    422 		printf("Please press any key to reboot.\n\n");
    423 		cnpollc(1);	/* For proper keyboard command handling */
    424 		cngetc();
    425 		cnpollc(0);
    426 	}
    427 
    428 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    429 
    430 	/*
    431 	 * Turning off GPIO2 as output will cause a reset.
    432 	 */
    433 	REGVAL(BONITO_GPIODATA) &= ~4;
    434 	REGVAL(BONITO_GPIOIE) &= ~4;
    435 
    436 	__asm__ __volatile__ (
    437 		"\t.long 0x3c02bfc0\n"
    438 		"\t.long 0x00400008\n"
    439 	    ::: "v0");
    440 }
    441