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