Home | History | Annotate | Line # | Download | only in loongson
machdep.c revision 1.6
      1 /*	$NetBSD: machdep.c,v 1.6 2014/03/24 20:06:31 christos 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) 1988 University of Utah.
     40  * Copyright (c) 1992, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * the Systems Programming Group of the University of Utah Computer
     45  * Science Department, The Mach Operating System project at
     46  * Carnegie-Mellon University and Ralph Campbell.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     73  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     74  */
     75 
     76 /*
     77  * Copyright (c) 2009, 2010 Miodrag Vallat.
     78  *
     79  * Permission to use, copy, modify, and distribute this software for any
     80  * purpose with or without fee is hereby granted, provided that the above
     81  * copyright notice and this permission notice appear in all copies.
     82  *
     83  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     84  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     85  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     86  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     87  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     88  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     89  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     90  */
     91 
     92 #include <sys/cdefs.h>
     93 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.6 2014/03/24 20:06:31 christos Exp $");
     94 
     95 #include "opt_ddb.h"
     96 #include "opt_execfmt.h"
     97 #include "opt_modular.h"
     98 
     99 #define _MIPS_BUS_DMA_PRIVATE
    100 
    101 #include <sys/param.h>
    102 #include <sys/systm.h>
    103 #include <sys/kernel.h>
    104 #include <sys/buf.h>
    105 #include <sys/reboot.h>
    106 #include <sys/mount.h>
    107 #include <sys/kcore.h>
    108 #include <sys/boot_flag.h>
    109 #include <sys/termios.h>
    110 #include <sys/ksyms.h>
    111 #include <sys/device.h>
    112 #include <sys/cpu.h>
    113 
    114 #include <uvm/uvm_extern.h>
    115 
    116 #include <dev/cons.h>
    117 
    118 #include "ksyms.h"
    119 
    120 #if NKSYMS || defined(DDB) || defined(MODULAR)
    121 #include <machine/db_machdep.h>
    122 #include <ddb/db_extern.h>
    123 #include <sys/exec_elf.h>
    124 #endif
    125 
    126 #include <evbmips/loongson/autoconf.h>
    127 #include <evbmips/loongson/loongson_intr.h>
    128 #include <evbmips/loongson/loongson_bus_defs.h>
    129 #include <machine/cpu.h>
    130 #include <machine/psl.h>
    131 
    132 #include <mips/locore.h>
    133 
    134 #include <mips/bonito/bonitoreg.h>
    135 #include <mips/bonito/bonitovar.h>
    136 #include <mips/pmon/pmon.h>
    137 
    138 #include "sisfb.h"
    139 #if NSISFB > 0
    140 #include <dev/pci/sisfb.h>
    141 #endif
    142 
    143 #include "lynxfb.h"
    144 #if NLYNXFB > 0
    145 #include <dev/pci/lynxfbvar.h>
    146 #endif
    147 
    148 #include "pckbc.h"
    149 #if NPCKBC > 0
    150 #include <dev/isa/isareg.h>
    151 #include <dev/ic/i8042reg.h>
    152 #include <dev/ic/pckbcvar.h>
    153 #endif
    154 #include "pckbd.h"
    155 #include "ukbd.h"
    156 #if NUKBD > 0
    157 #include <dev/usb/ukbdvar.h>
    158 #endif
    159 #if NPCKBD > 0 || NUKBD > 0
    160 #include <dev/wscons/wskbdvar.h>
    161 #endif
    162 
    163 #include "com.h"
    164 #if NCOM > 0
    165 #include <dev/ic/comreg.h>
    166 #include <dev/ic/comvar.h>
    167 
    168 bus_space_tag_t comconsiot;
    169 bus_addr_t comconsaddr;
    170 int comconsrate = 0;
    171 #endif /* NCOM > 0 */
    172 
    173 #ifdef LOW_DEBUG
    174 #define DPRINTF(x) printf x
    175 #define DPPRINTF(x) pmon_printf x
    176 #else
    177 #define DPRINTF(x)
    178 #define DPPRINTF(x)
    179 #endif
    180 
    181 
    182 int ex_mallocsafe = 0;
    183 struct extent *loongson_io_ex = NULL;
    184 struct extent *loongson_mem_ex = NULL;
    185 struct mips_bus_space bonito_iot;
    186 struct mips_bus_space bonito_memt;
    187 struct mips_bus_dma_tag bonito_dmat;
    188 struct mips_pci_chipset bonito_pc;
    189 
    190 uint loongson_ver;
    191 
    192 const struct platform *sys_platform;
    193 struct bonito_flavour {
    194 	const char *prefix;
    195 	const struct platform *platform;
    196 };
    197 
    198 extern const struct platform fuloong_platform;
    199 extern const struct platform gdium_platform;
    200 extern const struct platform generic2e_platform;
    201 extern const struct platform lynloong_platform;
    202 extern const struct platform yeeloong_platform;
    203 
    204 const struct bonito_flavour bonito_flavours[] = {
    205 	/* Lemote Fuloong 2F mini-PC */
    206 	{ "LM6002",	&fuloong_platform }, /* dual Ethernet, no prefix */
    207 	{ "LM6003",	&fuloong_platform },
    208 	{ "LM6004",	&fuloong_platform },
    209 	/* EMTEC Gdium Liberty 1000 */
    210 	{ "Gdium",	&gdium_platform },
    211 	/* Lemote Yeeloong 8.9" netbook */
    212 	{ "LM8089",	&yeeloong_platform },
    213 	/* supposedly Lemote Yeeloong 10.1" netbook, but those found so far
    214 	   report themselves as LM8089 */
    215 	{ "LM8101",	&yeeloong_platform },
    216 	/* Lemote Lynloong all-in-one computer */
    217 	{ "LM9001",	&lynloong_platform },
    218 	{ NULL }
    219 };
    220 
    221 /* Maps for VM objects. */
    222 struct vm_map *phys_map = NULL;
    223 
    224 int	netboot;		/* Are we netbooting? */
    225 
    226 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    227 int mem_cluster_cnt;
    228 
    229 void	mach_init(int, int32_t, int32_t, int32_t, char *);
    230 
    231 static int  pmoncngetc(dev_t);
    232 static void pmoncnputc(dev_t, int);
    233 
    234 struct consdev pmoncons = {
    235         NULL,		/* probe */
    236 	NULL, 		/* init */
    237 	pmoncngetc, 	/* getc */
    238 	pmoncnputc,	/* putc */
    239 	nullcnpollc,	/* poolc */
    240 	NULL,		/* BELL */
    241 	makedev(0, 0),
    242 	CN_DEAD
    243 };
    244 
    245 /*
    246  * Do all the stuff that locore normally does before calling main().
    247  */
    248 void
    249 mach_init(int32_t argc, int32_t argva, int32_t enva, int32_t callvec,
    250     char *boot_esym)
    251 {
    252 	void *kernend;
    253 #ifdef NOTYET
    254 	int howto;
    255 #endif
    256 	const char *env;
    257 	int i;
    258 	psize_t memlo, memhi;
    259 	const struct bonito_flavour *f;
    260 	char *ssym = NULL, *esym = NULL;
    261 	pcireg_t reg;
    262 	pcitag_t pcitag;
    263 
    264 	extern char edata[], end[];
    265 
    266 	/*
    267 	 * Clear the BSS segment.
    268 	 */
    269 	memset(edata, 0, (char *)end - edata);
    270 
    271 	pmon_init(argc, argva, enva, callvec);
    272 	DPPRINTF(("pmon hello\n"));
    273 
    274 	cn_tab = &pmoncons;
    275 
    276 	DPRINTF(("hello 0x%x %d 0x%x 0x%x %p stack %p\n", pmon_callvec, argc, argva, enva, boot_esym, &i));
    277 
    278 	/*
    279 	 * Reserve space for the symbol table, if it exists.
    280 	 */
    281 
    282 #if NKSYMS || defined(DDB) || defined(MODULAR)
    283 	/* Attempt to locate ELF header and symbol table after kernel. */
    284 	if (end[0] == ELFMAG0 && end[1] == ELFMAG1 &&
    285 	    end[2] == ELFMAG2 && end[3] == ELFMAG3) {
    286 		/* ELF header exists directly after kernel. */
    287 		ssym = end;
    288 		esym = boot_esym;
    289 		kernend = (void *)mips_round_page(esym);
    290 	} else {
    291 		ssym = (char *)(vaddr_t)*(int32_t *)end;
    292 		if (((long)ssym - (long)end) >= 0 &&
    293 		    ((long)ssym - (long)end) <= 0x1000 &&
    294 		    ssym[0] == ELFMAG0 && ssym[1] == ELFMAG1 &&
    295 		    ssym[2] == ELFMAG2 && ssym[3] == ELFMAG3) {
    296 			/* Pointers exist directly after kernel. */
    297 			esym = (char *)(vaddr_t)*((int32_t *)end + 1);
    298 			kernend = (void *)mips_round_page(esym);
    299 		} else {
    300 			/* Pointers aren't setup either... */
    301 			ssym = NULL;
    302 			esym = NULL;
    303 			kernend = (void *)mips_round_page(end);
    304 		}
    305 	}
    306 	DPRINTF(("ssym %p esym %p\n", ssym, esym));
    307 #endif
    308 
    309 	/*
    310 	 * Set up the exception vectors and CPU-specific function
    311 	 * vectors early on.  We need the wbflush() vector set up
    312 	 * before comcnattach() is called (or at least before the
    313 	 * first printf() after that is called).
    314 	 * Also clears the I+D caches.
    315 	 */
    316 	DPRINTF(("mips_vector_init "));
    317 	mips_vector_init(NULL, false);
    318 
    319 	DPRINTF(("uvm_setpagesize\n"));
    320 	/* set the VM page size */
    321 	uvm_setpagesize();
    322 #if NKSYMS || defined(DDB) || defined(MODULAR)
    323 	//ksyms_addsyms_elf((vaddr_t)esym - (vaddr_t)ssym, ssym, esym);
    324 #endif
    325 
    326 	/*
    327 	 * Try and figure out what kind of hardware we are.
    328 	 */
    329 
    330 	env = pmon_getenv("systype");
    331 	if (env == NULL) {
    332 		printf("Unable to figure out system type!\n");
    333 		goto unsupported;
    334 	}
    335 	if (strcmp(env, "Bonito") != 0) {
    336 		printf("This kernel doesn't support system type \"%s\".\n",
    337 		    env);
    338 		goto unsupported;
    339 	}
    340 
    341 	/*
    342 	 * While the kernel supports other processor types than Loongson,
    343 	 * we are not expecting a Bonito-based system with a different
    344 	 * processor.  Just to be on the safe side, refuse to run on
    345 	 * non Loongson2 processors for now.
    346 	 */
    347 
    348 	switch ((mips_options.mips_cpu_id >> 8) & 0xff) {
    349 	case MIPS_LOONGSON2:
    350 		switch (mips_options.mips_cpu_id & 0xff) {
    351 		case 0x00:
    352 			loongson_ver = 0x2c;
    353 			break;
    354 		case 0x02:
    355 			loongson_ver = 0x2e;
    356 			break;
    357 		case 0x03:
    358 			loongson_ver = 0x2f;
    359 			break;
    360 		case 0x05:
    361 			loongson_ver = 0x3a;
    362 			break;
    363 		}
    364 		if (loongson_ver == 0x2e || loongson_ver == 0x2f)
    365 			break;
    366 		/* FALLTHROUGH */
    367 	default:
    368 		printf("This kernel doesn't support processor type 0x%x"
    369 		    ", version %d.%d.\n",
    370 		    (mips_options.mips_cpu_id >> 8) & 0xff,
    371 		    (mips_options.mips_cpu_id >> 4) & 0x0f,
    372 		    mips_options.mips_cpu_id & 0x0f);
    373 		goto unsupported;
    374 	}
    375 
    376 	/*
    377 	 * Try to figure out what particular machine we run on, depending
    378 	 * on the PMON version information.
    379 	 */
    380 
    381 	env = pmon_getenv("Version");
    382 	if (env == NULL) {
    383 		/*
    384 		 * If this is a 2E system, use the generic code and hope
    385 		 * for the best.
    386 		 */
    387 		if (loongson_ver == 0x2e) {
    388 			sys_platform = &generic2e_platform;
    389 		} else {
    390 			printf("Unable to figure out model!\n");
    391 			goto unsupported;
    392 		}
    393 	} else {
    394 		for (f = bonito_flavours; f->prefix != NULL; f++)
    395 			if (strncmp(env, f->prefix, strlen(f->prefix)) ==
    396 			    0) {
    397 				sys_platform = f->platform;
    398 				break;
    399 			}
    400 
    401 		if (sys_platform == NULL) {
    402 			/*
    403 			 * Early Lemote designs shipped without a model prefix.
    404 			 * Hopefully these well be close enough to the first
    405 			 * generation Fuloong 2F design (LM6002); let's warn
    406 			 * the user and try this if version is 1.2.something
    407 			 * (1.3 onwards are expected to have a model prefix,
    408 			 *  and there are currently no reports of 1.1 and
    409 			 *  below being 2F systems).
    410 			 *
    411 			 * Note that this could be handled by adding a
    412 			 * "1.2." machine type entry to the flavours table,
    413 			 * but I prefer have it stand out.
    414 			 * LM6002 users are encouraged to add the system
    415 			 * model prefix to the `Version' variable.
    416 			 */
    417 			if (strncmp(env, "1.2.", 4) == 0) {
    418 				printf("No model prefix in version"
    419 				    " string \"%s\".\n"
    420 				    "Attempting to match as Lemote Fuloong\n",
    421 				    env);
    422 				sys_platform = &fuloong_platform;
    423 			}
    424 		}
    425 
    426 		if (sys_platform == NULL) {
    427 			printf("This kernel doesn't support model \"%s\"."
    428 			    "\n", env);
    429 			goto unsupported;
    430 		}
    431 	}
    432 
    433 	cpu_setmodel("%s %s", sys_platform->vendor, sys_platform->product);
    434 	DPRINTF(("Found %s, setting up.\n", cpu_getmodel()));
    435 
    436 	/*
    437 	 * Figure out memory information.
    438 	 * PMON reports it in two chunks, the memory under the 256MB
    439 	 * CKSEG limit, and memory above that limit.  We need to do the
    440 	 * math ourselves.
    441 	 */
    442 
    443 	env = pmon_getenv("memsize");
    444 	if (env == NULL) {
    445 		printf("Could not get memory information"
    446 		    " from the firmware\n");
    447 		goto unsupported;
    448 	}
    449 	memlo = strtoul(env, NULL, 10);	/* size in MB */
    450 	DPRINTF(("memlo %" PRIdPSIZE, memlo));
    451 	if (memlo < 0 || memlo > 256) {
    452 		printf("Incorrect low memory size `%s'\n", env);
    453 		goto unsupported;
    454 	}
    455 
    456 	/* 3A PMON only reports up to 240MB as low memory */
    457 	if (memlo >= 240) {
    458 		env = pmon_getenv("highmemsize");
    459 		if (env == NULL)
    460 			memhi = 0;
    461 		else
    462 			memhi = strtoul(env, NULL, 10);	/* size in MB */
    463 		if (memhi < 0 || memhi > (64 * 1024) - 256) {
    464 			printf("Incorrect high memory size `%s'\n",
    465 			    env);
    466 			/* better expose the problem than limit to 256MB */
    467 			goto unsupported;
    468 		}
    469 	} else
    470 		memhi = 0;
    471 
    472 	DPRINTF(("memhi %" PRIdPSIZE "\n", memhi));
    473 	memlo = memlo * 1024 * 1024;
    474 	memhi = memhi * 1024 * 1024;
    475 
    476 	switch (loongson_ver) {
    477 	case 0x2e:
    478 		loongson2e_setup(memlo, memhi,
    479 		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
    480 		break;
    481 	default:
    482 	case 0x2f:
    483 	case 0x3a:
    484 		loongson2f_setup(memlo, memhi,
    485 		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
    486 		break;
    487 	}
    488 
    489 	DPRINTF(("bonito_pci_init "));
    490 	bonito_pci_init(&bonito_pc, sys_platform->bonito_config);
    491 	bonito_pc.pc_intr_v = __UNCONST(sys_platform->bonito_config);
    492 	bonito_pc.pc_intr_map = loongson_pci_intr_map;
    493 	bonito_pc.pc_intr_string = loongson_pci_intr_string;
    494 	bonito_pc.pc_intr_evcnt = loongson_pci_intr_evcnt;
    495 	bonito_pc.pc_intr_establish = loongson_pci_intr_establish;
    496 	bonito_pc.pc_intr_disestablish = loongson_pci_intr_disestablish;
    497 	bonito_pc.pc_conf_interrupt = loongson_pci_conf_interrupt;
    498 	bonito_pc.pc_pciide_compat_intr_establish =
    499 	    loongson_pciide_compat_intr_establish;
    500 	DPRINTF(("bonito_bus_io_init "));
    501 	bonito_bus_io_init(&bonito_iot, NULL);
    502 	/* override mapping function */
    503 	bonito_iot.bs_map = bonito_bus_io_legacy_map;
    504 	DPRINTF(("bonito_bus_mem_init\n"));
    505 	bonito_bus_mem_init(&bonito_memt, NULL);
    506 
    507 	bonito_dmat._cookie = __UNCONST(sys_platform);
    508 	bonito_dmat._dmamap_ops = mips_bus_dmamap_ops;
    509 	bonito_dmat._dmamem_ops = mips_bus_dmamem_ops;
    510 	bonito_dmat._dmatag_ops = mips_bus_dmatag_ops;
    511 
    512 	DPRINTF(("sys_platform->setup %p\n", sys_platform->setup));
    513 	if (sys_platform->setup != NULL)
    514 		(*(sys_platform->setup))();
    515 
    516 #if NCOM > 0
    517 	DPRINTF(("comconsrate %d\n", comconsrate));
    518 	if (comconsrate > 0) {
    519 		if (comcnattach(comconsiot, comconsaddr, comconsrate,
    520 		    COM_FREQ, COM_TYPE_NORMAL,
    521 		    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    522 			panic("unable to initialize serial console");
    523 	}
    524 #endif /* NCOM > 0 */
    525 
    526 	for (i = 0; i < 32 - 0x11; i++) {
    527 		pcitag = pci_make_tag(&bonito_pc, 0, i, 0);
    528 		reg = pci_conf_read(&bonito_pc, pcitag, PCI_CLASS_REG);
    529 		DPRINTF(("dev %d class 0x%x", i, reg));
    530 		reg = pci_conf_read(&bonito_pc, pcitag, PCI_ID_REG);
    531 		DPRINTF((" id 0x%x; ", reg));
    532 #if NSISFB > 0
    533 		if (cn_tab == &pmoncons)
    534 			sisfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
    535 			    pcitag, reg);
    536 #endif
    537 #if NLYNXFB > 0
    538 		if (cn_tab == &pmoncons)
    539 			lynxfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
    540 			    pcitag, reg);
    541 #endif
    542 		if (cn_tab == &pmoncons)
    543 			gdium_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
    544 			    pcitag, reg);
    545 		if (cn_tab != &pmoncons)
    546 			break;
    547 	}
    548 #if NPCKBC > 0 || NUKBD > 0
    549 	if (cn_tab != &pmoncons) {
    550 		int rc = ENXIO;
    551 #if NPCKBC > 0
    552 		if (rc != 0)
    553 			rc = pckbc_cnattach(&bonito_iot, IO_KBD, KBCMDP, 0, 0);
    554 #endif
    555 #if NUKBD > 0
    556 		if (rc != 0)
    557 			rc = ukbd_cnattach();
    558 #endif
    559 	}
    560 #endif	/* NPCKBC > 0 || NUKBD > 0 */
    561 	DPRINTF(("\n"));
    562 
    563 	/*
    564 	 * Get the timer from PMON.
    565 	 */
    566 	DPRINTF(("search cpuclock "));
    567 	env = pmon_getenv("cpuclock");
    568 	DPRINTF(("got %s ", env));
    569 	if (env != NULL) {
    570 		curcpu()->ci_cpu_freq =
    571 		    strtoul(env, NULL, 10);
    572 	}
    573 
    574 	DPRINTF(("cpuclock %ld\n", curcpu()->ci_cpu_freq));
    575 
    576 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    577 		curcpu()->ci_cpu_freq /= 2;
    578 
    579 
    580 	/* Compute the number of ticks for hz. */
    581 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    582 
    583 	/* Compute the delay divisor. */
    584 	curcpu()->ci_divisor_delay =
    585 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
    586 
    587 	/*
    588 	 * Get correct cpu frequency if the CPU runs at twice the
    589 	 * external/cp0-count frequency.
    590 	 */
    591 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    592 		curcpu()->ci_cpu_freq *= 2;
    593 
    594 #ifdef DEBUG
    595 	printf("Timer calibration: %lu cycles/sec\n",
    596 	    curcpu()->ci_cpu_freq);
    597 #endif
    598 
    599 #if NCOM > 0 && 0
    600 	/*
    601 	 * Delay to allow firmware putchars to complete.
    602 	 * FIFO depth * character time.
    603 	 * character time = (1000000 / (defaultrate / 10))
    604 	 */
    605 	delay(160000000 / comcnrate);
    606 	if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
    607 	    COM_FREQ, COM_TYPE_NORMAL,
    608 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    609 		panic("malta: unable to initialize serial console");
    610 #endif /* NCOM > 0 */
    611 
    612 	/*
    613 	 * XXX: check argv[0] - do something if "gdb"???
    614 	 */
    615 
    616 	/*
    617 	 * Look at arguments passed to us and compute boothowto.
    618 	 */
    619 	boothowto = RB_AUTOBOOT;
    620 #ifdef NOTYET
    621 	for (i = 1; i < argc; i++) {
    622 		for (cp = argv[i]; *cp; cp++) {
    623 			/* Ignore superfluous '-', if there is one */
    624 			if (*cp == '-')
    625 				continue;
    626 
    627 			howto = 0;
    628 			BOOT_FLAG(*cp, howto);
    629 			if (! howto)
    630 				printf("bootflag '%c' not recognised\n", *cp);
    631 			else
    632 				boothowto |= howto;
    633 		}
    634 	}
    635 #endif
    636 
    637 	/*
    638 	 * Load the rest of the available pages into the VM system.
    639 	 */
    640 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
    641 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    642 
    643 	/*
    644 	 * Initialize error message buffer (at end of core).
    645 	 */
    646 	DPRINTF(("mips_init_msgbuf\n"));
    647 	mips_init_msgbuf();
    648 
    649 	DPRINTF(("pmap_bootstrap\n"));
    650 	pmap_bootstrap();
    651 
    652 	/*
    653 	 * Allocate uarea page for lwp0 and set it.
    654 	 */
    655 	DPRINTF(("curlwp %p ", curlwp));
    656 	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
    657 	mips_init_lwp0_uarea();
    658 
    659 	DPRINTF(("curlwp %p ", curlwp));
    660 	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
    661 
    662 	/*
    663 	 * Initialize debuggers, and break into them, if appropriate.
    664 	 */
    665 #if defined(DDB)
    666 	if (boothowto & RB_KDB)
    667 		Debugger();
    668 #endif
    669 	DPRINTF(("return\n"));
    670 	return;
    671 unsupported:
    672 	panic("unsupported hardware\n");
    673 }
    674 
    675 void
    676 consinit(void)
    677 {
    678 
    679 	/*
    680 	 * Everything related to console initialization is done
    681 	 * in mach_init().
    682 	 */
    683 }
    684 
    685 /*
    686  * Allocate memory for variable-sized tables,
    687  */
    688 void
    689 cpu_startup(void)
    690 {
    691 	/*
    692 	 *  Do the common startup items.
    693 	 */
    694 	cpu_startup_common();
    695 
    696 	/*
    697 	 * Virtual memory is bootstrapped -- notify the bus spaces
    698 	 * that memory allocation is now safe.
    699 	 */
    700 	ex_mallocsafe = 1;
    701 }
    702 
    703 int	waittime = -1;
    704 
    705 void
    706 cpu_reboot(int howto, char *bootstr)
    707 {
    708 
    709 	/* Take a snapshot before clobbering any registers. */
    710 	savectx(curpcb);
    711 
    712 	if (cold) {
    713 		howto |= RB_HALT;
    714 		goto haltsys;
    715 	}
    716 
    717 	/* If "always halt" was specified as a boot flag, obey. */
    718 	if (boothowto & RB_HALT)
    719 		howto |= RB_HALT;
    720 
    721 	boothowto = howto;
    722 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    723 		waittime = 0;
    724 		vfs_shutdown();
    725 
    726 		/*
    727 		 * If we've been adjusting the clock, the todr
    728 		 * will be out of synch; adjust it now.
    729 		 */
    730 		resettodr();
    731 	}
    732 
    733 	splhigh();
    734 
    735 	if (howto & RB_DUMP)
    736 		dumpsys();
    737 
    738 haltsys:
    739 	doshutdownhooks();
    740 
    741 	pmf_system_shutdown(boothowto);
    742 
    743 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    744 		if (sys_platform->powerdown != NULL)
    745 			sys_platform->powerdown();
    746 	}
    747 
    748 	if (howto & RB_HALT) {
    749 		printf("\n");
    750 		printf("The operating system has halted.\n");
    751 		printf("Please press any key to reboot.\n\n");
    752 		cnpollc(1);	/* For proper keyboard command handling */
    753 		cngetc();
    754 		cnpollc(0);
    755 	}
    756 
    757 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    758 
    759 	if (sys_platform->reset != NULL)
    760 		sys_platform->reset();
    761 
    762 	__asm__ __volatile__ (
    763 		"\t.long 0x3c02bfc0\n"
    764 		"\t.long 0x00400008\n"
    765 	    ::: "v0");
    766 }
    767 
    768 /*
    769  * Early console through pmon routines.
    770  */
    771 
    772 int
    773 pmoncngetc(dev_t dev)
    774 {
    775 	/*
    776 	 * PMON does not give us a getc routine.  So try to get a whole line
    777 	 * and return it char by char, trying not to lose the \n.  Kind
    778 	 * of ugly but should work.
    779 	 *
    780 	 * Note that one could theoretically use pmon_read(STDIN, &c, 1)
    781 	 * but the value of STDIN within PMON is not a constant and there
    782 	 * does not seem to be a way of letting us know which value to use.
    783 	 */
    784 	static char buf[1 + PMON_MAXLN];
    785 	static char *bufpos = buf;
    786 	int c;
    787 
    788 	if (*bufpos == '\0') {
    789 		bufpos = buf;
    790 		if (pmon_gets(buf) == NULL) {
    791 			/* either an empty line or EOF. assume the former */
    792 			return (int)'\n';
    793 		} else {
    794 			/* put back the \n sign */
    795 			buf[strlen(buf)] = '\n';
    796 		}
    797 	}
    798 
    799 	c = (int)*bufpos++;
    800 	if (bufpos - buf > PMON_MAXLN) {
    801 		bufpos = buf;
    802 		*bufpos = '\0';
    803 	}
    804 
    805 	return c;
    806 }
    807 
    808 void
    809 pmoncnputc(dev_t dev, int c)
    810 {
    811 	if (c == '\n')
    812 		pmon_printf("\n");
    813 	else
    814 		pmon_printf("%c", c);
    815 }
    816