Home | History | Annotate | Line # | Download | only in loongson
      1 /*	$NetBSD: machdep.c,v 1.9 2024/03/05 14:15:30 thorpej 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.9 2024/03/05 14:15:30 thorpej 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 /* The LOONGSON kernels only support little endian */
    246 CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN);
    247 
    248 /*
    249  * Do all the stuff that locore normally does before calling main().
    250  */
    251 void
    252 mach_init(int32_t argc, int32_t argva, int32_t enva, int32_t callvec,
    253     char *boot_esym)
    254 {
    255 	void *kernend;
    256 #ifdef NOTYET
    257 	int howto;
    258 #endif
    259 	const char *env;
    260 	int i;
    261 	psize_t memlo, memhi;
    262 	const struct bonito_flavour *f;
    263 	char *ssym = NULL, *esym = NULL;
    264 	pcireg_t reg;
    265 	pcitag_t pcitag;
    266 
    267 	extern char edata[], end[];
    268 
    269 	/*
    270 	 * Clear the BSS segment.
    271 	 */
    272 	memset(edata, 0, (char *)end - edata);
    273 
    274 	pmon_init(argc, argva, enva, callvec);
    275 	DPPRINTF(("pmon hello\n"));
    276 
    277 	cn_tab = &pmoncons;
    278 
    279 	DPRINTF(("hello 0x%x %d 0x%x 0x%x %p stack %p\n", pmon_callvec, argc, argva, enva, boot_esym, &i));
    280 
    281 	/*
    282 	 * Reserve space for the symbol table, if it exists.
    283 	 */
    284 
    285 #if NKSYMS || defined(DDB) || defined(MODULAR)
    286 	/* Attempt to locate ELF header and symbol table after kernel. */
    287 	if (end[0] == ELFMAG0 && end[1] == ELFMAG1 &&
    288 	    end[2] == ELFMAG2 && end[3] == ELFMAG3) {
    289 		/* ELF header exists directly after kernel. */
    290 		ssym = end;
    291 		esym = boot_esym;
    292 		kernend = (void *)mips_round_page(esym);
    293 	} else {
    294 		ssym = (char *)(vaddr_t)*(int32_t *)end;
    295 		if (((long)ssym - (long)end) >= 0 &&
    296 		    ((long)ssym - (long)end) <= 0x1000 &&
    297 		    ssym[0] == ELFMAG0 && ssym[1] == ELFMAG1 &&
    298 		    ssym[2] == ELFMAG2 && ssym[3] == ELFMAG3) {
    299 			/* Pointers exist directly after kernel. */
    300 			esym = (char *)(vaddr_t)*((int32_t *)end + 1);
    301 			kernend = (void *)mips_round_page(esym);
    302 		} else {
    303 			/* Pointers aren't setup either... */
    304 			ssym = NULL;
    305 			esym = NULL;
    306 			kernend = (void *)mips_round_page(end);
    307 		}
    308 	}
    309 	DPRINTF(("ssym %p esym %p\n", ssym, esym));
    310 #endif
    311 
    312 	/*
    313 	 * Set up the exception vectors and CPU-specific function
    314 	 * vectors early on.  We need the wbflush() vector set up
    315 	 * before comcnattach() is called (or at least before the
    316 	 * first printf() after that is called).
    317 	 * Also clears the I+D caches.
    318 	 */
    319 	DPRINTF(("mips_vector_init "));
    320 	mips_vector_init(NULL, false);
    321 
    322 	DPRINTF(("uvm_md_init\n"));
    323 	uvm_md_init();
    324 #if NKSYMS || defined(DDB) || defined(MODULAR)
    325 	//ksyms_addsyms_elf((vaddr_t)esym - (vaddr_t)ssym, ssym, esym);
    326 #endif
    327 
    328 	/*
    329 	 * Try and figure out what kind of hardware we are.
    330 	 */
    331 
    332 	env = pmon_getenv("systype");
    333 	if (env == NULL) {
    334 		printf("Unable to figure out system type!\n");
    335 		goto unsupported;
    336 	}
    337 	if (strcmp(env, "Bonito") != 0) {
    338 		printf("This kernel doesn't support system type \"%s\".\n",
    339 		    env);
    340 		goto unsupported;
    341 	}
    342 
    343 	/*
    344 	 * While the kernel supports other processor types than Loongson,
    345 	 * we are not expecting a Bonito-based system with a different
    346 	 * processor.  Just to be on the safe side, refuse to run on
    347 	 * non Loongson2 processors for now.
    348 	 */
    349 
    350 	switch ((mips_options.mips_cpu_id >> 8) & 0xff) {
    351 	case MIPS_LOONGSON2:
    352 		switch (mips_options.mips_cpu_id & 0xff) {
    353 		case 0x00:
    354 			loongson_ver = 0x2c;
    355 			break;
    356 		case 0x02:
    357 			loongson_ver = 0x2e;
    358 			break;
    359 		case 0x03:
    360 			loongson_ver = 0x2f;
    361 			break;
    362 		case 0x05:
    363 			loongson_ver = 0x3a;
    364 			break;
    365 		}
    366 		if (loongson_ver == 0x2e || loongson_ver == 0x2f)
    367 			break;
    368 		/* FALLTHROUGH */
    369 	default:
    370 		printf("This kernel doesn't support processor type 0x%x"
    371 		    ", version %d.%d.\n",
    372 		    (mips_options.mips_cpu_id >> 8) & 0xff,
    373 		    (mips_options.mips_cpu_id >> 4) & 0x0f,
    374 		    mips_options.mips_cpu_id & 0x0f);
    375 		goto unsupported;
    376 	}
    377 
    378 	/*
    379 	 * Try to figure out what particular machine we run on, depending
    380 	 * on the PMON version information.
    381 	 */
    382 
    383 	env = pmon_getenv("Version");
    384 	if (env == NULL) {
    385 		/*
    386 		 * If this is a 2E system, use the generic code and hope
    387 		 * for the best.
    388 		 */
    389 		if (loongson_ver == 0x2e) {
    390 			sys_platform = &generic2e_platform;
    391 		} else {
    392 			printf("Unable to figure out model!\n");
    393 			goto unsupported;
    394 		}
    395 	} else {
    396 		for (f = bonito_flavours; f->prefix != NULL; f++)
    397 			if (strncmp(env, f->prefix, strlen(f->prefix)) ==
    398 			    0) {
    399 				sys_platform = f->platform;
    400 				break;
    401 			}
    402 
    403 		if (sys_platform == NULL) {
    404 			/*
    405 			 * Early Lemote designs shipped without a model prefix.
    406 			 * Hopefully these well be close enough to the first
    407 			 * generation Fuloong 2F design (LM6002); let's warn
    408 			 * the user and try this if version is 1.2.something
    409 			 * (1.3 onwards are expected to have a model prefix,
    410 			 *  and there are currently no reports of 1.1 and
    411 			 *  below being 2F systems).
    412 			 *
    413 			 * Note that this could be handled by adding a
    414 			 * "1.2." machine type entry to the flavours table,
    415 			 * but I prefer have it stand out.
    416 			 * LM6002 users are encouraged to add the system
    417 			 * model prefix to the `Version' variable.
    418 			 */
    419 			if (strncmp(env, "1.2.", 4) == 0) {
    420 				printf("No model prefix in version"
    421 				    " string \"%s\".\n"
    422 				    "Attempting to match as Lemote Fuloong\n",
    423 				    env);
    424 				sys_platform = &fuloong_platform;
    425 			}
    426 		}
    427 
    428 		if (sys_platform == NULL) {
    429 			printf("This kernel doesn't support model \"%s\"."
    430 			    "\n", env);
    431 			goto unsupported;
    432 		}
    433 	}
    434 
    435 	cpu_setmodel("%s %s", sys_platform->vendor, sys_platform->product);
    436 	DPRINTF(("Found %s, setting up.\n", cpu_getmodel()));
    437 
    438 	/*
    439 	 * Figure out memory information.
    440 	 * PMON reports it in two chunks, the memory under the 256MB
    441 	 * CKSEG limit, and memory above that limit.  We need to do the
    442 	 * math ourselves.
    443 	 */
    444 
    445 	env = pmon_getenv("memsize");
    446 	if (env == NULL) {
    447 		printf("Could not get memory information"
    448 		    " from the firmware\n");
    449 		goto unsupported;
    450 	}
    451 	memlo = strtoul(env, NULL, 10);	/* size in MB */
    452 	DPRINTF(("memlo %" PRIdPSIZE, memlo));
    453 	if (memlo < 0 || memlo > 256) {
    454 		printf("Incorrect low memory size `%s'\n", env);
    455 		goto unsupported;
    456 	}
    457 
    458 	/* 3A PMON only reports up to 240MB as low memory */
    459 	if (memlo >= 240) {
    460 		env = pmon_getenv("highmemsize");
    461 		if (env == NULL)
    462 			memhi = 0;
    463 		else
    464 			memhi = strtoul(env, NULL, 10);	/* size in MB */
    465 		if (memhi < 0 || memhi > (64 * 1024) - 256) {
    466 			printf("Incorrect high memory size `%s'\n",
    467 			    env);
    468 			/* better expose the problem than limit to 256MB */
    469 			goto unsupported;
    470 		}
    471 	} else
    472 		memhi = 0;
    473 
    474 	DPRINTF(("memhi %" PRIdPSIZE "\n", memhi));
    475 	memlo = memlo * 1024 * 1024;
    476 	memhi = memhi * 1024 * 1024;
    477 
    478 	switch (loongson_ver) {
    479 	case 0x2e:
    480 		loongson2e_setup(memlo, memhi,
    481 		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
    482 		break;
    483 	default:
    484 	case 0x2f:
    485 	case 0x3a:
    486 		loongson2f_setup(memlo, memhi,
    487 		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
    488 		break;
    489 	}
    490 
    491 	DPRINTF(("bonito_pci_init "));
    492 	bonito_pci_init(&bonito_pc, sys_platform->bonito_config);
    493 	bonito_pc.pc_intr_v = __UNCONST(sys_platform->bonito_config);
    494 	bonito_pc.pc_intr_map = loongson_pci_intr_map;
    495 	bonito_pc.pc_intr_string = loongson_pci_intr_string;
    496 	bonito_pc.pc_intr_evcnt = loongson_pci_intr_evcnt;
    497 	bonito_pc.pc_intr_establish = loongson_pci_intr_establish;
    498 	bonito_pc.pc_intr_disestablish = loongson_pci_intr_disestablish;
    499 	bonito_pc.pc_conf_interrupt = loongson_pci_conf_interrupt;
    500 	bonito_pc.pc_pciide_compat_intr_establish =
    501 	    loongson_pciide_compat_intr_establish;
    502 	DPRINTF(("bonito_bus_io_init "));
    503 	bonito_bus_io_init(&bonito_iot, NULL);
    504 	/* override mapping function */
    505 	bonito_iot.bs_map = bonito_bus_io_legacy_map;
    506 	DPRINTF(("bonito_bus_mem_init\n"));
    507 	bonito_bus_mem_init(&bonito_memt, NULL);
    508 
    509 	bonito_dmat._cookie = __UNCONST(sys_platform);
    510 	bonito_dmat._dmamap_ops = mips_bus_dmamap_ops;
    511 	bonito_dmat._dmamem_ops = mips_bus_dmamem_ops;
    512 	bonito_dmat._dmatag_ops = mips_bus_dmatag_ops;
    513 
    514 	DPRINTF(("sys_platform->setup %p\n", sys_platform->setup));
    515 	if (sys_platform->setup != NULL)
    516 		(*(sys_platform->setup))();
    517 
    518 #if NCOM > 0
    519 	DPRINTF(("comconsrate %d\n", comconsrate));
    520 	if (comconsrate > 0) {
    521 		if (comcnattach(comconsiot, comconsaddr, comconsrate,
    522 		    COM_FREQ, COM_TYPE_NORMAL,
    523 		    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    524 			panic("unable to initialize serial console");
    525 	}
    526 #endif /* NCOM > 0 */
    527 
    528 	for (i = 0; i < 32 - 0x11; i++) {
    529 		pcitag = pci_make_tag(&bonito_pc, 0, i, 0);
    530 		reg = pci_conf_read(&bonito_pc, pcitag, PCI_CLASS_REG);
    531 		DPRINTF(("dev %d class 0x%x", i, reg));
    532 		reg = pci_conf_read(&bonito_pc, pcitag, PCI_ID_REG);
    533 		DPRINTF((" id 0x%x; ", reg));
    534 #if NSISFB > 0
    535 		if (cn_tab == &pmoncons)
    536 			sisfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
    537 			    pcitag, reg);
    538 #endif
    539 #if NLYNXFB > 0
    540 		if (cn_tab == &pmoncons)
    541 			lynxfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
    542 			    pcitag, reg);
    543 #endif
    544 		if (cn_tab == &pmoncons)
    545 			gdium_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
    546 			    pcitag, reg);
    547 		if (cn_tab != &pmoncons)
    548 			break;
    549 	}
    550 #if NPCKBC > 0 || NUKBD > 0
    551 	if (cn_tab != &pmoncons) {
    552 		int rc = ENXIO;
    553 #if NPCKBC > 0
    554 		if (rc != 0)
    555 			rc = pckbc_cnattach(&bonito_iot, IO_KBD, KBCMDP, 0, 0);
    556 #endif
    557 #if NUKBD > 0
    558 		if (rc != 0)
    559 			rc = ukbd_cnattach();
    560 #endif
    561 	}
    562 #endif	/* NPCKBC > 0 || NUKBD > 0 */
    563 	DPRINTF(("\n"));
    564 
    565 	/*
    566 	 * Get the timer from PMON.
    567 	 */
    568 	DPRINTF(("search cpuclock "));
    569 	env = pmon_getenv("cpuclock");
    570 	DPRINTF(("got %s ", env));
    571 	if (env != NULL) {
    572 		curcpu()->ci_cpu_freq =
    573 		    strtoul(env, NULL, 10);
    574 	}
    575 
    576 	DPRINTF(("cpuclock %ld\n", curcpu()->ci_cpu_freq));
    577 
    578 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    579 		curcpu()->ci_cpu_freq /= 2;
    580 
    581 
    582 	/* Compute the number of ticks for hz. */
    583 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    584 
    585 	/* Compute the delay divisor. */
    586 	curcpu()->ci_divisor_delay =
    587 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
    588 
    589 	/*
    590 	 * Get correct cpu frequency if the CPU runs at twice the
    591 	 * external/cp0-count frequency.
    592 	 */
    593 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    594 		curcpu()->ci_cpu_freq *= 2;
    595 
    596 #ifdef DEBUG
    597 	printf("Timer calibration: %lu cycles/sec\n",
    598 	    curcpu()->ci_cpu_freq);
    599 #endif
    600 
    601 #if NCOM > 0 && 0
    602 	/*
    603 	 * Delay to allow firmware putchars to complete.
    604 	 * FIFO depth * character time.
    605 	 * character time = (1000000 / (defaultrate / 10))
    606 	 */
    607 	delay(160000000 / comcnrate);
    608 	if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
    609 	    COM_FREQ, COM_TYPE_NORMAL,
    610 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    611 		panic("malta: unable to initialize serial console");
    612 #endif /* NCOM > 0 */
    613 
    614 	/*
    615 	 * XXX: check argv[0] - do something if "gdb"???
    616 	 */
    617 
    618 	/*
    619 	 * Look at arguments passed to us and compute boothowto.
    620 	 */
    621 	boothowto = RB_AUTOBOOT;
    622 #ifdef NOTYET
    623 	for (i = 1; i < argc; i++) {
    624 		for (cp = argv[i]; *cp; cp++) {
    625 			/* Ignore superfluous '-', if there is one */
    626 			if (*cp == '-')
    627 				continue;
    628 
    629 			howto = 0;
    630 			BOOT_FLAG(*cp, howto);
    631 			if (! howto)
    632 				printf("bootflag '%c' not recognised\n", *cp);
    633 			else
    634 				boothowto |= howto;
    635 		}
    636 	}
    637 #endif
    638 
    639 	/*
    640 	 * Load the rest of the available pages into the VM system.
    641 	 */
    642 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
    643 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    644 
    645 	/*
    646 	 * Initialize error message buffer (at end of core).
    647 	 */
    648 	DPRINTF(("mips_init_msgbuf\n"));
    649 	mips_init_msgbuf();
    650 
    651 	DPRINTF(("pmap_bootstrap\n"));
    652 	pmap_bootstrap();
    653 
    654 	/*
    655 	 * Allocate uarea page for lwp0 and set it.
    656 	 */
    657 	DPRINTF(("curlwp %p ", curlwp));
    658 	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
    659 	mips_init_lwp0_uarea();
    660 
    661 	DPRINTF(("curlwp %p ", curlwp));
    662 	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
    663 
    664 	/*
    665 	 * Initialize debuggers, and break into them, if appropriate.
    666 	 */
    667 #if defined(DDB)
    668 	if (boothowto & RB_KDB)
    669 		Debugger();
    670 #endif
    671 	DPRINTF(("return\n"));
    672 	return;
    673 unsupported:
    674 	panic("unsupported hardware\n");
    675 }
    676 
    677 void
    678 consinit(void)
    679 {
    680 
    681 	/*
    682 	 * Everything related to console initialization is done
    683 	 * in mach_init().
    684 	 */
    685 }
    686 
    687 /*
    688  * Allocate memory for variable-sized tables,
    689  */
    690 void
    691 cpu_startup(void)
    692 {
    693 	/*
    694 	 *  Do the common startup items.
    695 	 */
    696 	cpu_startup_common();
    697 
    698 	/*
    699 	 * Virtual memory is bootstrapped -- notify the bus spaces
    700 	 * that memory allocation is now safe.
    701 	 */
    702 	ex_mallocsafe = 1;
    703 }
    704 
    705 int	waittime = -1;
    706 
    707 void
    708 cpu_reboot(int howto, char *bootstr)
    709 {
    710 
    711 	/* Take a snapshot before clobbering any registers. */
    712 	savectx(curpcb);
    713 
    714 	if (cold) {
    715 		howto |= RB_HALT;
    716 		goto haltsys;
    717 	}
    718 
    719 	/* If "always halt" was specified as a boot flag, obey. */
    720 	if (boothowto & RB_HALT)
    721 		howto |= RB_HALT;
    722 
    723 	boothowto = howto;
    724 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    725 		waittime = 0;
    726 		vfs_shutdown();
    727 	}
    728 
    729 	splhigh();
    730 
    731 	if (howto & RB_DUMP)
    732 		dumpsys();
    733 
    734 haltsys:
    735 	doshutdownhooks();
    736 
    737 	pmf_system_shutdown(boothowto);
    738 
    739 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    740 		if (sys_platform->powerdown != NULL)
    741 			sys_platform->powerdown();
    742 	}
    743 
    744 	if (howto & RB_HALT) {
    745 		printf("\n");
    746 		printf("The operating system has halted.\n");
    747 		printf("Please press any key to reboot.\n\n");
    748 		cnpollc(1);	/* For proper keyboard command handling */
    749 		cngetc();
    750 		cnpollc(0);
    751 	}
    752 
    753 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    754 
    755 	if (sys_platform->reset != NULL)
    756 		sys_platform->reset();
    757 
    758 	__asm__ __volatile__ (
    759 		"\t.long 0x3c02bfc0\n"
    760 		"\t.long 0x00400008\n"
    761 	    ::: "v0");
    762 }
    763 
    764 /*
    765  * Early console through pmon routines.
    766  */
    767 
    768 int
    769 pmoncngetc(dev_t dev)
    770 {
    771 	/*
    772 	 * PMON does not give us a getc routine.  So try to get a whole line
    773 	 * and return it char by char, trying not to lose the \n.  Kind
    774 	 * of ugly but should work.
    775 	 *
    776 	 * Note that one could theoretically use pmon_read(STDIN, &c, 1)
    777 	 * but the value of STDIN within PMON is not a constant and there
    778 	 * does not seem to be a way of letting us know which value to use.
    779 	 */
    780 	static char buf[1 + PMON_MAXLN];
    781 	static char *bufpos = buf;
    782 	int c;
    783 
    784 	if (*bufpos == '\0') {
    785 		bufpos = buf;
    786 		if (pmon_gets(buf) == NULL) {
    787 			/* either an empty line or EOF. assume the former */
    788 			return (int)'\n';
    789 		} else {
    790 			/* put back the \n sign */
    791 			buf[strlen(buf)] = '\n';
    792 		}
    793 	}
    794 
    795 	c = (int)*bufpos++;
    796 	if (bufpos - buf > PMON_MAXLN) {
    797 		bufpos = buf;
    798 		*bufpos = '\0';
    799 	}
    800 
    801 	return c;
    802 }
    803 
    804 void
    805 pmoncnputc(dev_t dev, int c)
    806 {
    807 	if (c == '\n')
    808 		pmon_printf("\n");
    809 	else
    810 		pmon_printf("%c", c);
    811 }
    812