Home | History | Annotate | Line # | Download | only in alchemy
machdep.c revision 1.27
      1 /* $NetBSD: machdep.c,v 1.27 2006/02/23 03:51:40 gdamore Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Portions written by Garrett D'Amore for Itronix 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. The name of Itronix Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific
     19  *    prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 /*
     34  * Copyright (c) 1992, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * This code is derived from software contributed to Berkeley by
     38  * the Systems Programming Group of the University of Utah Computer
     39  * Science Department, The Mach Operating System project at
     40  * Carnegie-Mellon University and Ralph Campbell.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
     67  * 	from: Utah Hdr: machdep.c 1.63 91/04/24
     68  */
     69 /*
     70  * Copyright (c) 1988 University of Utah.
     71  *
     72  * This code is derived from software contributed to Berkeley by
     73  * the Systems Programming Group of the University of Utah Computer
     74  * Science Department, The Mach Operating System project at
     75  * Carnegie-Mellon University and Ralph Campbell.
     76  *
     77  * Redistribution and use in source and binary forms, with or without
     78  * modification, are permitted provided that the following conditions
     79  * are met:
     80  * 1. Redistributions of source code must retain the above copyright
     81  *    notice, this list of conditions and the following disclaimer.
     82  * 2. Redistributions in binary form must reproduce the above copyright
     83  *    notice, this list of conditions and the following disclaimer in the
     84  *    documentation and/or other materials provided with the distribution.
     85  * 3. All advertising materials mentioning features or use of this software
     86  *    must display the following acknowledgement:
     87  *	This product includes software developed by the University of
     88  *	California, Berkeley and its contributors.
     89  * 4. Neither the name of the University nor the names of its contributors
     90  *    may be used to endorse or promote products derived from this software
     91  *    without specific prior written permission.
     92  *
     93  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     94  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     95  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     96  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     97  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     98  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     99  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    101  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    102  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    103  * SUCH DAMAGE.
    104  *
    105  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
    106  * 	from: Utah Hdr: machdep.c 1.63 91/04/24
    107  */
    108 
    109 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
    110 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.27 2006/02/23 03:51:40 gdamore Exp $");
    111 
    112 #include "opt_ddb.h"
    113 #include "opt_kgdb.h"
    114 
    115 #include "opt_memsize.h"
    116 #include "opt_ethaddr.h"
    117 
    118 #include <sys/param.h>
    119 #include <sys/systm.h>
    120 #include <sys/kernel.h>
    121 #include <sys/buf.h>
    122 #include <sys/reboot.h>
    123 #include <sys/user.h>
    124 #include <sys/mount.h>
    125 #include <sys/kcore.h>
    126 #include <sys/boot_flag.h>
    127 #include <sys/termios.h>
    128 #include <sys/ksyms.h>
    129 
    130 #include <net/if.h>
    131 #include <net/if_ether.h>
    132 
    133 #include <uvm/uvm_extern.h>
    134 
    135 #include <dev/cons.h>
    136 
    137 #include "ksyms.h"
    138 
    139 #if NKSYMS || defined(DDB) || defined(LKM)
    140 #include <machine/db_machdep.h>
    141 #include <ddb/db_extern.h>
    142 #endif
    143 
    144 #include <mips/cache.h>
    145 #include <mips/locore.h>
    146 #include <machine/yamon.h>
    147 
    148 #include <evbmips/alchemy/board.h>
    149 #include <mips/alchemy/dev/aupcivar.h>
    150 #include <mips/alchemy/dev/aupcmciavar.h>
    151 #include <mips/alchemy/include/aureg.h>
    152 #include <mips/alchemy/include/auvar.h>
    153 #include <mips/alchemy/include/aubusvar.h>
    154 
    155 #include "aucom.h"
    156 #if NAUCOM > 0
    157 #include <mips/alchemy/dev/aucomvar.h>
    158 
    159 int	aucomcnrate = 0;
    160 #endif /* NAUCOM > 0 */
    161 
    162 #include "ohci.h"
    163 
    164 /* The following are used externally (sysctl_hw). */
    165 extern char	cpu_model[];
    166 
    167 struct	user *proc0paddr;
    168 
    169 /* Our exported CPU info; we can have only one. */
    170 struct cpu_info cpu_info_store;
    171 
    172 /* Maps for VM objects. */
    173 struct vm_map *exec_map = NULL;
    174 struct vm_map *mb_map = NULL;
    175 struct vm_map *phys_map = NULL;
    176 
    177 int physmem;		/* # pages of physical memory */
    178 int maxmem;			/* max memory per process */
    179 
    180 int mem_cluster_cnt;
    181 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    182 
    183 yamon_env_var *yamon_envp;
    184 struct mips_bus_space alchemy_cpuregt;
    185 
    186 void	mach_init(int, char **, yamon_env_var *, u_long); /* XXX */
    187 
    188 void
    189 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
    190 {
    191 	bus_space_handle_t sh;
    192 	caddr_t kernend;
    193 	const char *cp;
    194 	u_long first, last;
    195 	caddr_t v;
    196 	int freqok, howto, i;
    197 	const struct alchemy_board *board;
    198 
    199 	extern char edata[], end[];	/* XXX */
    200 
    201 	board = board_info();
    202 	KASSERT(board != NULL);
    203 
    204 	/* clear the BSS segment */
    205 	kernend = (caddr_t)mips_round_page(end);
    206 	memset(edata, 0, kernend - (caddr_t)edata);
    207 
    208 	/* set CPU model info for sysctl_hw */
    209 	strcpy(cpu_model, board->ab_name);
    210 
    211 	/* save the yamon environment pointer */
    212 	yamon_envp = envp;
    213 
    214 	/* Use YAMON callbacks for early console I/O */
    215 	cn_tab = &yamon_promcd;
    216 
    217 	/*
    218 	 * Set up the exception vectors and CPU-specific function
    219 	 * vectors early on.  We need the wbflush() vector set up
    220 	 * before comcnattach() is called (or at least before the
    221 	 * first printf() after that is called).
    222 	 * Sets up mips_cpu_flags that may be queried by other
    223 	 * functions called during startup.
    224 	 * Also clears the I+D caches.
    225 	 */
    226 	mips_vector_init();
    227 
    228 	/*
    229 	 * Set the VM page size.
    230 	 */
    231 	uvm_setpagesize();
    232 
    233 	/*
    234 	 * Use YAMON's CPU frequency if available.
    235 	 */
    236 	freqok = yamon_setcpufreq(1);
    237 
    238 	/*
    239 	 * Initialize bus space tags.
    240 	 */
    241 	au_cpureg_bus_mem_init(&alchemy_cpuregt, &alchemy_cpuregt);
    242 	aubus_st = &alchemy_cpuregt;
    243 
    244 	/*
    245 	 * Calibrate the timer if YAMON failed to tell us.
    246 	 */
    247 	if (!freqok) {
    248 		bus_space_map(aubus_st, PC_BASE, PC_SIZE, 0, &sh);
    249 		au_cal_timers(aubus_st, sh);
    250 		bus_space_unmap(aubus_st, sh, PC_SIZE);
    251 	}
    252 
    253 	/*
    254 	 * Perform board-specific initialization.
    255 	 */
    256 	board->ab_init();
    257 
    258 	/*
    259 	 * Bring up the console.
    260 	 */
    261 #if NAUCOM > 0
    262 #ifdef CONSPEED
    263 	if (aucomcnrate == 0)
    264 		aucomcnrate = CONSPEED;
    265 #else /* !CONSPEED */
    266 	/*
    267 	 * Learn default console speed.  We use the YAMON environment,
    268 	 * though we could probably also figure it out by checking the
    269 	 * aucom registers directly.
    270 	 */
    271 	if ((aucomcnrate == 0) && ((cp = yamon_getenv("modetty0")) != NULL))
    272 		aucomcnrate = strtoul(cp, NULL, 0);
    273 
    274 	if (aucomcnrate == 0) {
    275 		printf("FATAL: `modetty0' YAMON variable not set.  Set it\n");
    276 		printf("       to the speed of the console and try again.\n");
    277 		printf("       Or, build a kernel with the `CONSPEED' "
    278 		    "option.\n");
    279 		panic("mach_init");
    280 	}
    281 #endif /* CONSPEED */
    282 
    283 	/*
    284 	 * Delay to allow firmware putchars to complete.
    285 	 * FIFO depth * character time.
    286 	 * character time = (1000000 / (defaultrate / 10))
    287 	 */
    288 	delay(160000000 / aucomcnrate);
    289 	if (aucomcnattach(aubus_st, UART0_BASE, aucomcnrate,
    290 	    curcpu()->ci_cpu_freq / 4, COM_TYPE_AU1x00,
    291 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    292 		panic("mach_init: unable to initialize serial console");
    293 #else
    294 	panic("mach_init: not configured to use serial console");
    295 #endif /* NAUCOM > 0 */
    296 
    297 	/*
    298 	 * Look at arguments passed to us and compute boothowto.
    299 	 */
    300 	boothowto = RB_AUTOBOOT;
    301 #ifdef KADB
    302 	boothowto |= RB_KDB;
    303 #endif
    304 	for (i = 1; i < argc; i++) {
    305 		for (cp = argv[i]; *cp; cp++) {
    306 			/* Ignore superfluous '-', if there is one */
    307 			if (*cp == '-')
    308 				continue;
    309 
    310 			howto = 0;
    311 			BOOT_FLAG(*cp, howto);
    312 			if (! howto)
    313 				printf("bootflag '%c' not recognised\n", *cp);
    314 			else
    315 				boothowto |= howto;
    316 		}
    317 	}
    318 
    319 	/*
    320 	 * Determine the memory size.  Use the `memsize' PMON
    321 	 * variable.  If that's not available, panic.
    322 	 *
    323 	 * Note: Reserve the first page!  That's where the trap
    324 	 * vectors are located.
    325 	 */
    326 
    327 #if defined(MEMSIZE)
    328 	memsize = MEMSIZE;
    329 #else
    330 	if (memsize == 0) {
    331 		if ((cp = yamon_getenv("memsize")) != NULL)
    332 			memsize = strtoul(cp, NULL, 0);
    333 		else {
    334 			printf("FATAL: `memsize' YAMON variable not set.  Set it to\n");
    335 			printf("       the amount of memory (in MB) and try again.\n");
    336 			printf("       Or, build a kernel with the `MEMSIZE' "
    337 			    "option.\n");
    338 			panic("mach_init");
    339 		}
    340 	}
    341 #endif /* MEMSIZE */
    342 	printf("Memory size: 0x%08lx\n", memsize);
    343 	physmem = btoc(memsize);
    344 
    345 	mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
    346 	mem_clusters[mem_cluster_cnt].size =
    347 	    memsize - mem_clusters[mem_cluster_cnt].start;
    348 	mem_cluster_cnt++;
    349 
    350 	/*
    351 	 * Load the rest of the available pages into the VM system.
    352 	 */
    353 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    354 	last = mem_clusters[0].start + mem_clusters[0].size;
    355 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    356 	    VM_FREELIST_DEFAULT);
    357 
    358 	/*
    359 	 * Initialize message buffer (at end of core).
    360 	 */
    361 	mips_init_msgbuf();
    362 
    363 	/*
    364 	 * Initialize the virtual memory system.
    365 	 */
    366 	pmap_bootstrap();
    367 
    368 	/*
    369 	 * Init mapping for u page(s) for proc0.
    370 	 */
    371 	v = (caddr_t) uvm_pageboot_alloc(USPACE);
    372 	lwp0.l_addr = proc0paddr = (struct user *)v;
    373 	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
    374 	curpcb = &lwp0.l_addr->u_pcb;
    375 	curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    376 
    377 	/*
    378 	 * Initialize debuggers, and break into them, if appropriate.
    379 	 */
    380 #if NKSYMS || defined(DDB) || defined(LKM)
    381 	ksyms_init(0, 0, 0);
    382 #endif
    383 #ifdef DDB
    384 	if (boothowto & RB_KDB)
    385 		Debugger();
    386 #endif
    387 }
    388 
    389 void
    390 consinit(void)
    391 {
    392 
    393 	/*
    394 	 * Everything related to console initialization is done
    395 	 * in mach_init().
    396 	 */
    397 }
    398 
    399 void
    400 cpu_startup(void)
    401 {
    402 	char pbuf[9];
    403 	vaddr_t minaddr, maxaddr;
    404 #ifdef DEBUG
    405 	extern int pmapdebug;		/* XXX */
    406 	int opmapdebug = pmapdebug;
    407 
    408 	pmapdebug = 0;		/* Shut up pmap debug during bootstrap */
    409 #endif
    410 
    411 	/*
    412 	 * Good {morning,afternoon,evening,night}.
    413 	 */
    414 	printf("%s%s", copyright, version);
    415 	printf("%s\n", cpu_model);
    416 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    417 	printf("total memory = %s\n", pbuf);
    418 
    419 	minaddr = 0;
    420 	/*
    421 	 * Allocate a submap for exec arguments.  This map effectively
    422 	 * limits the number of processes exec'ing at any time.
    423 	 */
    424 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    425 	    16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    426 
    427 	/*
    428 	 * Allocate a submap for physio
    429 	 */
    430 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    431 	    VM_PHYS_SIZE, 0, FALSE, NULL);
    432 
    433 	/*
    434 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    435 	 * are allocated via the pool allocator, and we use KSEG to
    436 	 * map those pages.
    437 	 */
    438 
    439 #ifdef DEBUG
    440 	pmapdebug = opmapdebug;
    441 #endif
    442 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    443 	printf("avail memory = %s\n", pbuf);
    444 }
    445 
    446 void
    447 cpu_reboot(int howto, char *bootstr)
    448 {
    449 	static int waittime = -1;
    450 	const struct alchemy_board *board;
    451 
    452 	/* Take a snapshot before clobbering any registers. */
    453 	if (curproc)
    454 		savectx((struct user *)curpcb);
    455 
    456 	board = board_info();
    457 	KASSERT(board != NULL);
    458 
    459 	/* If "always halt" was specified as a boot flag, obey. */
    460 	if (boothowto & RB_HALT)
    461 		howto |= RB_HALT;
    462 
    463 	boothowto = howto;
    464 
    465 	/* If system is cold, just halt. */
    466 	if (cold) {
    467 		boothowto |= RB_HALT;
    468 		goto haltsys;
    469 	}
    470 
    471 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    472 		waittime = 0;
    473 
    474 		/*
    475 		 * Synchronize the disks....
    476 		 */
    477 		vfs_shutdown();
    478 
    479 		/*
    480 		 * If we've been adjusting the clock, the todr
    481 		 * will be out of synch; adjust it now.
    482 		 */
    483 		resettodr();
    484 	}
    485 
    486 	/* Disable interrupts. */
    487 	splhigh();
    488 
    489 	if (boothowto & RB_DUMP)
    490 		dumpsys();
    491 
    492  haltsys:
    493 	/* Run any shutdown hooks. */
    494 	doshutdownhooks();
    495 
    496 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
    497 		if (board && board->ab_poweroff)
    498 			board->ab_poweroff();
    499 
    500 	/*
    501 	 * YAMON may autoboot (depending on settings), and we cannot pass
    502 	 * flags to it (at least I haven't figured out how to yet), so
    503 	 * we "pseudo-halt" now.
    504 	 */
    505 	if (boothowto & RB_HALT) {
    506 		printf("\n");
    507 		printf("The operating system has halted.\n");
    508 		printf("Please press any key to reboot.\n\n");
    509 		cnpollc(1);	/* For proper keyboard command handling */
    510 		cngetc();
    511 		cnpollc(0);
    512 	}
    513 
    514 	/*
    515 	 * Try to use board-specific reset logic, which might involve a better
    516 	 * hardware reset.
    517 	 */
    518 	if (board->ab_reboot)
    519 		board->ab_reboot();
    520 
    521 #if 1
    522 	/* XXX
    523 	 * For some reason we are leaving the ethernet MAC in a state where
    524 	 * YAMON isn't happy with it.  So just call the reset vector (grr,
    525 	 * Alchemy YAMON doesn't have a "reset" command).
    526 	 */
    527 	printf("reseting board...\n\n");
    528 	mips_icache_sync_all();
    529 	mips_dcache_wbinv_all();
    530 	__asm volatile("jr	%0" :: "r"(MIPS_RESET_EXC_VEC));
    531 #else
    532 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    533 	yamon_exit(boothowto);
    534 	printf("Oops, back from yamon_exit()\n\nSpinning...");
    535 #endif
    536 	for (;;)
    537 		/* spin forever */ ;	/* XXX */
    538 	/*NOTREACHED*/
    539 }
    540 
    541 /*
    542  * Export our interrupt map function so aupci can find it.
    543  */
    544 int
    545 aupci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    546 {
    547 	const struct alchemy_board *board;
    548 
    549 	board = board_info();
    550 	if (board->ab_pci_intr_map != NULL)
    551 		return (board->ab_pci_intr_map(pa, ihp));
    552 	return 1;
    553 }
    554 
    555 struct aupcmcia_machdep *
    556 aupcmcia_machdep(void)
    557 {
    558 	const struct alchemy_board *board;
    559 
    560 	board = board_info();
    561 	return (board->ab_pcmcia);
    562 }
    563