xref: /netbsd-src/sys/arch/evbmips/loongson/machdep.c (revision 9ddb6ab554e70fb9bbd90c3d96b812bc57755a14)
1 /*	$NetBSD: machdep.c,v 1.4 2012/03/02 13:20:57 nonaka 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.4 2012/03/02 13:20:57 nonaka 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 
137 #include "sisfb.h"
138 #if NSISFB > 0
139 #include <dev/pci/sisfb.h>
140 #endif
141 
142 #include "lynxfb.h"
143 #if NLYNXFB > 0
144 #include <dev/pci/lynxfbvar.h>
145 #endif
146 
147 #include "pckbc.h"
148 #if NPCKBC > 0
149 #include <dev/isa/isareg.h>
150 #include <dev/ic/i8042reg.h>
151 #include <dev/ic/pckbcvar.h>
152 #endif
153 #include "pckbd.h"
154 #include "ukbd.h"
155 #if NUKBD > 0
156 #include <dev/usb/ukbdvar.h>
157 #endif
158 #if NPCKBD > 0 || NUKBD > 0
159 #include <dev/wscons/wskbdvar.h>
160 #endif
161 
162 #include "com.h"
163 #if NCOM > 0
164 #include <dev/ic/comreg.h>
165 #include <dev/ic/comvar.h>
166 
167 bus_space_tag_t comconsiot;
168 bus_addr_t comconsaddr;
169 int comconsrate = 0;
170 #endif /* NCOM > 0 */
171 
172 #ifdef LOW_DEBUG
173 #define DPRINTF(x) printf x
174 #define DPPRINTF(x) pmon_printf x
175 #else
176 #define DPRINTF(x)
177 #define DPPRINTF(x)
178 #endif
179 
180 
181 int ex_mallocsafe = 0;
182 struct extent *loongson_io_ex = NULL;
183 struct extent *loongson_mem_ex = NULL;
184 struct mips_bus_space bonito_iot;
185 struct mips_bus_space bonito_memt;
186 struct mips_bus_dma_tag bonito_dmat;
187 struct mips_pci_chipset bonito_pc;
188 
189 uint loongson_ver;
190 
191 const struct platform *sys_platform;
192 struct bonito_flavour {
193 	const char *prefix;
194 	const struct platform *platform;
195 };
196 
197 extern const struct platform fuloong_platform;
198 extern const struct platform gdium_platform;
199 extern const struct platform generic2e_platform;
200 extern const struct platform lynloong_platform;
201 extern const struct platform yeeloong_platform;
202 
203 const struct bonito_flavour bonito_flavours[] = {
204 	/* Lemote Fuloong 2F mini-PC */
205 	{ "LM6002",	&fuloong_platform }, /* dual Ethernet, no prefix */
206 	{ "LM6003",	&fuloong_platform },
207 	{ "LM6004",	&fuloong_platform },
208 	/* EMTEC Gdium Liberty 1000 */
209 	{ "Gdium",	&gdium_platform },
210 	/* Lemote Yeeloong 8.9" netbook */
211 	{ "LM8089",	&yeeloong_platform },
212 	/* supposedly Lemote Yeeloong 10.1" netbook, but those found so far
213 	   report themselves as LM8089 */
214 	{ "LM8101",	&yeeloong_platform },
215 	/* Lemote Lynloong all-in-one computer */
216 	{ "LM9001",	&lynloong_platform },
217 	{ NULL }
218 };
219 
220 /* For sysctl_hw. */
221 extern char cpu_model[];
222 
223 /* Maps for VM objects. */
224 struct vm_map *phys_map = NULL;
225 
226 int	netboot;		/* Are we netbooting? */
227 
228 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
229 int mem_cluster_cnt;
230 
231 void	mach_init(int, int32_t, int32_t, int32_t, char *);
232 
233 static int  pmoncngetc(dev_t);
234 static void pmoncnputc(dev_t, int);
235 
236 struct consdev pmoncons = {
237         NULL,		/* probe */
238 	NULL, 		/* init */
239 	pmoncngetc, 	/* getc */
240 	pmoncnputc,	/* putc */
241 	nullcnpollc,	/* poolc */
242 	NULL,		/* BELL */
243 	makedev(0, 0),
244 	CN_DEAD
245 };
246 
247 /*
248  * Do all the stuff that locore normally does before calling main().
249  */
250 void
251 mach_init(int32_t argc, int32_t argva, int32_t enva, int32_t callvec,
252     char *boot_esym)
253 {
254 	void *kernend;
255 #ifdef NOTYET
256 	int howto;
257 #endif
258 	const char *env;
259 	int i;
260 	psize_t memlo, memhi;
261 	const struct bonito_flavour *f;
262 	char *ssym = NULL, *esym = NULL;
263 	pcireg_t reg;
264 	pcitag_t pcitag;
265 
266 	extern char edata[], end[];
267 
268 	/*
269 	 * Clear the BSS segment.
270 	 */
271 	memset(edata, 0, (char *)end - edata);
272 
273 	pmon_init(argc, argva, enva, callvec);
274 	DPPRINTF(("pmon hello\n"));
275 
276 	cn_tab = &pmoncons;
277 
278 	DPRINTF(("hello 0x%x %d 0x%x 0x%x %p stack %p\n", pmon_callvec, argc, argva, enva, boot_esym, &i));
279 
280 	/*
281 	 * Reserve space for the symbol table, if it exists.
282 	 */
283 
284 #if NKSYMS || defined(DDB) || defined(MODULAR)
285 	/* Attempt to locate ELF header and symbol table after kernel. */
286 	if (end[0] == ELFMAG0 && end[1] == ELFMAG1 &&
287 	    end[2] == ELFMAG2 && end[3] == ELFMAG3) {
288 		/* ELF header exists directly after kernel. */
289 		ssym = end;
290 		esym = boot_esym;
291 		kernend = (void *)mips_round_page(esym);
292 	} else {
293 		ssym = (char *)(vaddr_t)*(int32_t *)end;
294 		if (((long)ssym - (long)end) >= 0 &&
295 		    ((long)ssym - (long)end) <= 0x1000 &&
296 		    ssym[0] == ELFMAG0 && ssym[1] == ELFMAG1 &&
297 		    ssym[2] == ELFMAG2 && ssym[3] == ELFMAG3) {
298 			/* Pointers exist directly after kernel. */
299 			esym = (char *)(vaddr_t)*((int32_t *)end + 1);
300 			kernend = (void *)mips_round_page(esym);
301 		} else {
302 			/* Pointers aren't setup either... */
303 			ssym = NULL;
304 			esym = NULL;
305 			kernend = (void *)mips_round_page(end);
306 		}
307 	}
308 	DPRINTF(("ssym %p esym %p\n", ssym, esym));
309 #endif
310 
311 	/*
312 	 * Set up the exception vectors and CPU-specific function
313 	 * vectors early on.  We need the wbflush() vector set up
314 	 * before comcnattach() is called (or at least before the
315 	 * first printf() after that is called).
316 	 * Also clears the I+D caches.
317 	 */
318 	DPRINTF(("mips_vector_init "));
319 	mips_vector_init(NULL, false);
320 
321 	DPRINTF(("uvm_setpagesize\n"));
322 	/* set the VM page size */
323 	uvm_setpagesize();
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 	sprintf(cpu_model, "%s %s", sys_platform->vendor,
436 	    sys_platform->product);
437 	DPRINTF(("Found %s, setting up.\n", cpu_model));
438 
439 	/*
440 	 * Figure out memory information.
441 	 * PMON reports it in two chunks, the memory under the 256MB
442 	 * CKSEG limit, and memory above that limit.  We need to do the
443 	 * math ourselves.
444 	 */
445 
446 	env = pmon_getenv("memsize");
447 	if (env == NULL) {
448 		printf("Could not get memory information"
449 		    " from the firmware\n");
450 		goto unsupported;
451 	}
452 	memlo = strtoul(env, NULL, 10);	/* size in MB */
453 	DPRINTF(("memlo %" PRIdPSIZE, memlo));
454 	if (memlo < 0 || memlo > 256) {
455 		printf("Incorrect low memory size `%s'\n", env);
456 		goto unsupported;
457 	}
458 
459 	/* 3A PMON only reports up to 240MB as low memory */
460 	if (memlo >= 240) {
461 		env = pmon_getenv("highmemsize");
462 		if (env == NULL)
463 			memhi = 0;
464 		else
465 			memhi = strtoul(env, NULL, 10);	/* size in MB */
466 		if (memhi < 0 || memhi > (64 * 1024) - 256) {
467 			printf("Incorrect high memory size `%s'\n",
468 			    env);
469 			/* better expose the problem than limit to 256MB */
470 			goto unsupported;
471 		}
472 	} else
473 		memhi = 0;
474 
475 	DPRINTF(("memhi %" PRIdPSIZE "\n", memhi));
476 	memlo = memlo * 1024 * 1024;
477 	memhi = memhi * 1024 * 1024;
478 
479 	switch (loongson_ver) {
480 	case 0x2e:
481 		loongson2e_setup(memlo, memhi,
482 		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
483 		break;
484 	default:
485 	case 0x2f:
486 	case 0x3a:
487 		loongson2f_setup(memlo, memhi,
488 		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
489 		break;
490 	}
491 
492 	DPRINTF(("bonito_pci_init "));
493 	bonito_pci_init(&bonito_pc, sys_platform->bonito_config);
494 	bonito_pc.pc_intr_v = __UNCONST(sys_platform->bonito_config);
495 	bonito_pc.pc_intr_map = loongson_pci_intr_map;
496 	bonito_pc.pc_intr_string = loongson_pci_intr_string;
497 	bonito_pc.pc_intr_evcnt = loongson_pci_intr_evcnt;
498 	bonito_pc.pc_intr_establish = loongson_pci_intr_establish;
499 	bonito_pc.pc_intr_disestablish = loongson_pci_intr_disestablish;
500 	bonito_pc.pc_conf_interrupt = loongson_pci_conf_interrupt;
501 	bonito_pc.pc_pciide_compat_intr_establish =
502 	    loongson_pciide_compat_intr_establish;
503 	DPRINTF(("bonito_bus_io_init "));
504 	bonito_bus_io_init(&bonito_iot, NULL);
505 	/* override mapping function */
506 	bonito_iot.bs_map = bonito_bus_io_legacy_map;
507 	DPRINTF(("bonito_bus_mem_init\n"));
508 	bonito_bus_mem_init(&bonito_memt, NULL);
509 
510 	bonito_dmat._cookie = __UNCONST(sys_platform);
511 	bonito_dmat._dmamap_ops = mips_bus_dmamap_ops;
512 	bonito_dmat._dmamem_ops = mips_bus_dmamem_ops;
513 	bonito_dmat._dmatag_ops = mips_bus_dmatag_ops;
514 
515 	DPRINTF(("sys_platform->setup %p\n", sys_platform->setup));
516 	if (sys_platform->setup != NULL)
517 		(*(sys_platform->setup))();
518 
519 #if NCOM > 0
520 	DPRINTF(("comconsrate %d\n", comconsrate));
521 	if (comconsrate > 0) {
522 		if (comcnattach(comconsiot, comconsaddr, comconsrate,
523 		    COM_FREQ, COM_TYPE_NORMAL,
524 		    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
525 			panic("unable to initialize serial console");
526 	}
527 #endif /* NCOM > 0 */
528 
529 	for (i = 0; i < 32 - 0x11; i++) {
530 		pcitag = pci_make_tag(&bonito_pc, 0, i, 0);
531 		reg = pci_conf_read(&bonito_pc, pcitag, PCI_CLASS_REG);
532 		DPRINTF(("dev %d class 0x%x", i, reg));
533 		reg = pci_conf_read(&bonito_pc, pcitag, PCI_ID_REG);
534 		DPRINTF((" id 0x%x; ", reg));
535 #if NSISFB > 0
536 		if (cn_tab == &pmoncons)
537 			sisfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
538 			    pcitag, reg);
539 #endif
540 #if NLYNXFB > 0
541 		if (cn_tab == &pmoncons)
542 			lynxfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
543 			    pcitag, reg);
544 #endif
545 		if (cn_tab == &pmoncons)
546 			gdium_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
547 			    pcitag, reg);
548 		if (cn_tab != &pmoncons)
549 			break;
550 	}
551 #if NPCKBC > 0 || NUKBD > 0
552 	if (cn_tab != &pmoncons) {
553 		int rc = ENXIO;
554 #if NPCKBC > 0
555 		if (rc != 0)
556 			rc = pckbc_cnattach(&bonito_iot, IO_KBD, KBCMDP, 0);
557 #endif
558 #if NUKBD > 0
559 		if (rc != 0)
560 			rc = ukbd_cnattach();
561 #endif
562 	}
563 #endif	/* NPCKBC > 0 || NUKBD > 0 */
564 	DPRINTF(("\n"));
565 
566 	/*
567 	 * Get the timer from PMON.
568 	 */
569 	DPRINTF(("search cpuclock "));
570 	env = pmon_getenv("cpuclock");
571 	DPRINTF(("got %s ", env));
572 	if (env != NULL) {
573 		curcpu()->ci_cpu_freq =
574 		    strtoul(env, NULL, 10);
575 	}
576 
577 	DPRINTF(("cpuclock %ld\n", curcpu()->ci_cpu_freq));
578 
579 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
580 		curcpu()->ci_cpu_freq /= 2;
581 
582 
583 	/* Compute the number of ticks for hz. */
584 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
585 
586 	/* Compute the delay divisor. */
587 	curcpu()->ci_divisor_delay =
588 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
589 
590 	/*
591 	 * Get correct cpu frequency if the CPU runs at twice the
592 	 * external/cp0-count frequency.
593 	 */
594 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
595 		curcpu()->ci_cpu_freq *= 2;
596 
597 #ifdef DEBUG
598 	printf("Timer calibration: %lu cycles/sec\n",
599 	    curcpu()->ci_cpu_freq);
600 #endif
601 
602 #if NCOM > 0 && 0
603 	/*
604 	 * Delay to allow firmware putchars to complete.
605 	 * FIFO depth * character time.
606 	 * character time = (1000000 / (defaultrate / 10))
607 	 */
608 	delay(160000000 / comcnrate);
609 	if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
610 	    COM_FREQ, COM_TYPE_NORMAL,
611 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
612 		panic("malta: unable to initialize serial console");
613 #endif /* NCOM > 0 */
614 
615 	/*
616 	 * XXX: check argv[0] - do something if "gdb"???
617 	 */
618 
619 	/*
620 	 * Look at arguments passed to us and compute boothowto.
621 	 */
622 	boothowto = RB_AUTOBOOT;
623 #ifdef NOTYET
624 	for (i = 1; i < argc; i++) {
625 		for (cp = argv[i]; *cp; cp++) {
626 			/* Ignore superfluous '-', if there is one */
627 			if (*cp == '-')
628 				continue;
629 
630 			howto = 0;
631 			BOOT_FLAG(*cp, howto);
632 			if (! howto)
633 				printf("bootflag '%c' not recognised\n", *cp);
634 			else
635 				boothowto |= howto;
636 		}
637 	}
638 #endif
639 
640 	/*
641 	 * Load the rest of the available pages into the VM system.
642 	 */
643 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
644 	    mem_clusters, mem_cluster_cnt, NULL, 0);
645 
646 	/*
647 	 * Initialize error message buffer (at end of core).
648 	 */
649 	DPRINTF(("mips_init_msgbuf\n"));
650 	mips_init_msgbuf();
651 
652 	DPRINTF(("pmap_bootstrap\n"));
653 	pmap_bootstrap();
654 
655 	/*
656 	 * Allocate uarea page for lwp0 and set it.
657 	 */
658 	DPRINTF(("curlwp %p ", curlwp));
659 	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
660 	mips_init_lwp0_uarea();
661 
662 	DPRINTF(("curlwp %p ", curlwp));
663 	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
664 
665 	/*
666 	 * Initialize debuggers, and break into them, if appropriate.
667 	 */
668 #if defined(DDB)
669 	if (boothowto & RB_KDB)
670 		Debugger();
671 #endif
672 	DPRINTF(("return\n"));
673 	return;
674 unsupported:
675 	panic("unsupported hardware\n");
676 }
677 
678 void
679 consinit(void)
680 {
681 
682 	/*
683 	 * Everything related to console initialization is done
684 	 * in mach_init().
685 	 */
686 }
687 
688 /*
689  * Allocate memory for variable-sized tables,
690  */
691 void
692 cpu_startup(void)
693 {
694 	/*
695 	 *  Do the common startup items.
696 	 */
697 	cpu_startup_common();
698 
699 	/*
700 	 * Virtual memory is bootstrapped -- notify the bus spaces
701 	 * that memory allocation is now safe.
702 	 */
703 	ex_mallocsafe = 1;
704 }
705 
706 int	waittime = -1;
707 
708 void
709 cpu_reboot(int howto, char *bootstr)
710 {
711 
712 	/* Take a snapshot before clobbering any registers. */
713 	savectx(curpcb);
714 
715 	if (cold) {
716 		howto |= RB_HALT;
717 		goto haltsys;
718 	}
719 
720 	/* If "always halt" was specified as a boot flag, obey. */
721 	if (boothowto & RB_HALT)
722 		howto |= RB_HALT;
723 
724 	boothowto = howto;
725 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
726 		waittime = 0;
727 		vfs_shutdown();
728 
729 		/*
730 		 * If we've been adjusting the clock, the todr
731 		 * will be out of synch; adjust it now.
732 		 */
733 		resettodr();
734 	}
735 
736 	splhigh();
737 
738 	if (howto & RB_DUMP)
739 		dumpsys();
740 
741 haltsys:
742 	doshutdownhooks();
743 
744 	pmf_system_shutdown(boothowto);
745 
746 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
747 		if (sys_platform->powerdown != NULL)
748 			sys_platform->powerdown();
749 	}
750 
751 	if (howto & RB_HALT) {
752 		printf("\n");
753 		printf("The operating system has halted.\n");
754 		printf("Please press any key to reboot.\n\n");
755 		cnpollc(1);	/* For proper keyboard command handling */
756 		cngetc();
757 		cnpollc(0);
758 	}
759 
760 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
761 
762 	if (sys_platform->reset != NULL)
763 		sys_platform->reset();
764 
765 	__asm__ __volatile__ (
766 		"\t.long 0x3c02bfc0\n"
767 		"\t.long 0x00400008\n"
768 	    ::: "v0");
769 }
770 
771 /*
772  * Early console through pmon routines.
773  */
774 
775 int
776 pmoncngetc(dev_t dev)
777 {
778 	/*
779 	 * PMON does not give us a getc routine.  So try to get a whole line
780 	 * and return it char by char, trying not to lose the \n.  Kind
781 	 * of ugly but should work.
782 	 *
783 	 * Note that one could theoretically use pmon_read(STDIN, &c, 1)
784 	 * but the value of STDIN within PMON is not a constant and there
785 	 * does not seem to be a way of letting us know which value to use.
786 	 */
787 	static char buf[1 + PMON_MAXLN];
788 	static char *bufpos = buf;
789 	int c;
790 
791 	if (*bufpos == '\0') {
792 		bufpos = buf;
793 		if (pmon_gets(buf) == NULL) {
794 			/* either an empty line or EOF. assume the former */
795 			return (int)'\n';
796 		} else {
797 			/* put back the \n sign */
798 			buf[strlen(buf)] = '\n';
799 		}
800 	}
801 
802 	c = (int)*bufpos++;
803 	if (bufpos - buf > PMON_MAXLN) {
804 		bufpos = buf;
805 		*bufpos = '\0';
806 	}
807 
808 	return c;
809 }
810 
811 void
812 pmoncnputc(dev_t dev, int c)
813 {
814 	if (c == '\n')
815 		pmon_printf("\n");
816 	else
817 		pmon_printf("%c", c);
818 }
819