Home | History | Annotate | Line # | Download | only in alchemy
machdep.c revision 1.44
      1 /* $NetBSD: machdep.c,v 1.44 2009/11/27 03:23:08 rmind 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.44 2009/11/27 03:23:08 rmind Exp $");
    111 
    112 #include "opt_ddb.h"
    113 #include "opt_kgdb.h"
    114 
    115 #include "opt_memsize.h"
    116 #include "opt_modular.h"
    117 #include "opt_ethaddr.h"
    118 
    119 #include <sys/param.h>
    120 #include <sys/systm.h>
    121 #include <sys/kernel.h>
    122 #include <sys/buf.h>
    123 #include <sys/reboot.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 #include <sys/device.h>
    130 
    131 #include <net/if.h>
    132 #include <net/if_ether.h>
    133 
    134 #include <uvm/uvm_extern.h>
    135 
    136 #include <dev/cons.h>
    137 
    138 #include "ksyms.h"
    139 
    140 #if NKSYMS || defined(DDB) || defined(MODULAR)
    141 #include <machine/db_machdep.h>
    142 #include <ddb/db_extern.h>
    143 #endif
    144 
    145 #include <mips/cache.h>
    146 #include <mips/locore.h>
    147 #include <machine/yamon.h>
    148 
    149 #include <evbmips/alchemy/board.h>
    150 #include <mips/alchemy/dev/aupcivar.h>
    151 #include <mips/alchemy/dev/aupcmciavar.h>
    152 #include <mips/alchemy/dev/auspivar.h>
    153 #include <mips/alchemy/include/aureg.h>
    154 #include <mips/alchemy/include/auvar.h>
    155 #include <mips/alchemy/include/aubusvar.h>
    156 
    157 #include "com.h"
    158 #if NCOM > 0
    159 
    160 int	aucomcnrate = 0;
    161 #endif /* NAUCOM > 0 */
    162 
    163 #include "ohci.h"
    164 
    165 /* Our exported CPU info; we can have only one. */
    166 struct cpu_info cpu_info_store;
    167 
    168 /* Maps for VM objects. */
    169 struct vm_map *mb_map = NULL;
    170 struct vm_map *phys_map = NULL;
    171 
    172 int maxmem;			/* max memory per process */
    173 
    174 int mem_cluster_cnt;
    175 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    176 
    177 yamon_env_var *yamon_envp;
    178 struct mips_bus_space alchemy_cpuregt;
    179 
    180 void	mach_init(int, char **, yamon_env_var *, u_long); /* XXX */
    181 
    182 void
    183 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
    184 {
    185 	bus_space_handle_t sh;
    186 	void *kernend;
    187 	const char *cp;
    188 	u_long first, last;
    189 	struct pcb *pcb0;
    190 	vaddr_t v;
    191 	int freqok, howto, i;
    192 	const struct alchemy_board *board;
    193 
    194 	extern char edata[], end[];	/* XXX */
    195 
    196 	board = board_info();
    197 	KASSERT(board != NULL);
    198 
    199 	/* clear the BSS segment */
    200 	kernend = (void *)mips_round_page(end);
    201 	memset(edata, 0, (char *)kernend - edata);
    202 
    203 	/* set CPU model info for sysctl_hw */
    204 	strcpy(cpu_model, board->ab_name);
    205 
    206 	/* save the yamon environment pointer */
    207 	yamon_envp = envp;
    208 
    209 	/* Use YAMON callbacks for early console I/O */
    210 	cn_tab = &yamon_promcd;
    211 
    212 	/*
    213 	 * Set up the exception vectors and CPU-specific function
    214 	 * vectors early on.  We need the wbflush() vector set up
    215 	 * before comcnattach() is called (or at least before the
    216 	 * first printf() after that is called).
    217 	 * Sets up mips_cpu_flags that may be queried by other
    218 	 * functions called during startup.
    219 	 * Also clears the I+D caches.
    220 	 */
    221 	mips_vector_init();
    222 
    223 	/*
    224 	 * Set the VM page size.
    225 	 */
    226 	uvm_setpagesize();
    227 
    228 	/*
    229 	 * Use YAMON's CPU frequency if available.
    230 	 */
    231 	freqok = yamon_setcpufreq(1);
    232 
    233 	/*
    234 	 * Initialize bus space tags.
    235 	 */
    236 	au_cpureg_bus_mem_init(&alchemy_cpuregt, &alchemy_cpuregt);
    237 	aubus_st = &alchemy_cpuregt;
    238 
    239 	/*
    240 	 * Calibrate the timer if YAMON failed to tell us.
    241 	 */
    242 	if (!freqok) {
    243 		bus_space_map(aubus_st, PC_BASE, PC_SIZE, 0, &sh);
    244 		au_cal_timers(aubus_st, sh);
    245 		bus_space_unmap(aubus_st, sh, PC_SIZE);
    246 	}
    247 
    248 	/*
    249 	 * Perform board-specific initialization.
    250 	 */
    251 	board->ab_init();
    252 
    253 	/*
    254 	 * Bring up the console.
    255 	 */
    256 #if NCOM > 0
    257 #ifdef CONSPEED
    258 	if (aucomcnrate == 0)
    259 		aucomcnrate = CONSPEED;
    260 #else /* !CONSPEED */
    261 	/*
    262 	 * Learn default console speed.  We use the YAMON environment,
    263 	 * though we could probably also figure it out by checking the
    264 	 * aucom registers directly.
    265 	 */
    266 	if ((aucomcnrate == 0) && ((cp = yamon_getenv("modetty0")) != NULL))
    267 		aucomcnrate = strtoul(cp, NULL, 0);
    268 
    269 	if (aucomcnrate == 0) {
    270 		printf("FATAL: `modetty0' YAMON variable not set.  Set it\n");
    271 		printf("       to the speed of the console and try again.\n");
    272 		printf("       Or, build a kernel with the `CONSPEED' "
    273 		    "option.\n");
    274 		panic("mach_init");
    275 	}
    276 #endif /* CONSPEED */
    277 
    278 	/*
    279 	 * Delay to allow firmware putchars to complete.
    280 	 * FIFO depth * character time.
    281 	 * character time = (1000000 / (defaultrate / 10))
    282 	 */
    283 	delay(160000000 / aucomcnrate);
    284 	if (com_aubus_cnattach(UART0_BASE, aucomcnrate) != 0)
    285 		panic("mach_init: unable to initialize serial console");
    286 
    287 #else /* NCOM > 0 */
    288 	panic("mach_init: not configured to use serial console");
    289 #endif /* NAUCOM > 0 */
    290 
    291 	/*
    292 	 * Look at arguments passed to us and compute boothowto.
    293 	 */
    294 	boothowto = RB_AUTOBOOT;
    295 #ifdef KADB
    296 	boothowto |= RB_KDB;
    297 #endif
    298 	for (i = 1; i < argc; i++) {
    299 		for (cp = argv[i]; *cp; cp++) {
    300 			/* Ignore superfluous '-', if there is one */
    301 			if (*cp == '-')
    302 				continue;
    303 
    304 			howto = 0;
    305 			BOOT_FLAG(*cp, howto);
    306 			if (! howto)
    307 				printf("bootflag '%c' not recognised\n", *cp);
    308 			else
    309 				boothowto |= howto;
    310 		}
    311 	}
    312 
    313 	/*
    314 	 * Determine the memory size.  Use the `memsize' PMON
    315 	 * variable.  If that's not available, panic.
    316 	 *
    317 	 * Note: Reserve the first page!  That's where the trap
    318 	 * vectors are located.
    319 	 */
    320 
    321 #if defined(MEMSIZE)
    322 	memsize = MEMSIZE;
    323 #else
    324 	if (memsize == 0) {
    325 		if ((cp = yamon_getenv("memsize")) != NULL)
    326 			memsize = strtoul(cp, NULL, 0);
    327 		else {
    328 			printf("FATAL: `memsize' YAMON variable not set.  Set it to\n");
    329 			printf("       the amount of memory (in MB) and try again.\n");
    330 			printf("       Or, build a kernel with the `MEMSIZE' "
    331 			    "option.\n");
    332 			panic("mach_init");
    333 		}
    334 	}
    335 #endif /* MEMSIZE */
    336 	printf("Memory size: 0x%08lx\n", memsize);
    337 	physmem = btoc(memsize);
    338 
    339 	mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
    340 	mem_clusters[mem_cluster_cnt].size =
    341 	    memsize - mem_clusters[mem_cluster_cnt].start;
    342 	mem_cluster_cnt++;
    343 
    344 	/*
    345 	 * Load the rest of the available pages into the VM system.
    346 	 */
    347 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    348 	last = mem_clusters[0].start + mem_clusters[0].size;
    349 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    350 	    VM_FREELIST_DEFAULT);
    351 
    352 	/*
    353 	 * Initialize message buffer (at end of core).
    354 	 */
    355 	mips_init_msgbuf();
    356 
    357 	/*
    358 	 * Initialize the virtual memory system.
    359 	 */
    360 	pmap_bootstrap();
    361 
    362 	/*
    363 	 * Allocate uarea page for lwp0 and set it.
    364 	 */
    365 	v = uvm_pageboot_alloc(USPACE);
    366 	uvm_lwp_setuarea(&lwp0, v);
    367 
    368 	pcb0 = lwp_getpcb(&lwp0);
    369 	pcb0->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    370 
    371 	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
    372 
    373 	/*
    374 	 * Initialize debuggers, and break into them, if appropriate.
    375 	 */
    376 #ifdef DDB
    377 	if (boothowto & RB_KDB)
    378 		Debugger();
    379 #endif
    380 }
    381 
    382 void
    383 consinit(void)
    384 {
    385 
    386 	/*
    387 	 * Everything related to console initialization is done
    388 	 * in mach_init().
    389 	 */
    390 }
    391 
    392 void
    393 cpu_startup(void)
    394 {
    395 	char pbuf[9];
    396 	vaddr_t minaddr, maxaddr;
    397 #ifdef DEBUG
    398 	extern int pmapdebug;		/* XXX */
    399 	int opmapdebug = pmapdebug;
    400 
    401 	pmapdebug = 0;		/* Shut up pmap debug during bootstrap */
    402 #endif
    403 
    404 	/*
    405 	 * Good {morning,afternoon,evening,night}.
    406 	 */
    407 	printf("%s%s", copyright, version);
    408 	printf("%s\n", cpu_model);
    409 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    410 	printf("total memory = %s\n", pbuf);
    411 
    412 	minaddr = 0;
    413 
    414 	/*
    415 	 * Allocate a submap for physio
    416 	 */
    417 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    418 	    VM_PHYS_SIZE, 0, false, NULL);
    419 
    420 	/*
    421 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    422 	 * are allocated via the pool allocator, and we use KSEG to
    423 	 * map those pages.
    424 	 */
    425 
    426 #ifdef DEBUG
    427 	pmapdebug = opmapdebug;
    428 #endif
    429 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    430 	printf("avail memory = %s\n", pbuf);
    431 }
    432 
    433 void
    434 cpu_reboot(int howto, char *bootstr)
    435 {
    436 	static int waittime = -1;
    437 	const struct alchemy_board *board;
    438 
    439 	/* Take a snapshot before clobbering any registers. */
    440 	if (curproc)
    441 		savectx(curpcb);
    442 
    443 	board = board_info();
    444 	KASSERT(board != NULL);
    445 
    446 	/* If "always halt" was specified as a boot flag, obey. */
    447 	if (boothowto & RB_HALT)
    448 		howto |= RB_HALT;
    449 
    450 	boothowto = howto;
    451 
    452 	/* If system is cold, just halt. */
    453 	if (cold) {
    454 		boothowto |= RB_HALT;
    455 		goto haltsys;
    456 	}
    457 
    458 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    459 		waittime = 0;
    460 
    461 		/*
    462 		 * Synchronize the disks....
    463 		 */
    464 		vfs_shutdown();
    465 
    466 		/*
    467 		 * If we've been adjusting the clock, the todr
    468 		 * will be out of synch; adjust it now.
    469 		 */
    470 		resettodr();
    471 	}
    472 
    473 	/* Disable interrupts. */
    474 	splhigh();
    475 
    476 	if (boothowto & RB_DUMP)
    477 		dumpsys();
    478 
    479  haltsys:
    480 	/* Run any shutdown hooks. */
    481 	doshutdownhooks();
    482 
    483 	pmf_system_shutdown(boothowto);
    484 
    485 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
    486 		if (board && board->ab_poweroff)
    487 			board->ab_poweroff();
    488 
    489 	/*
    490 	 * YAMON may autoboot (depending on settings), and we cannot pass
    491 	 * flags to it (at least I haven't figured out how to yet), so
    492 	 * we "pseudo-halt" now.
    493 	 */
    494 	if (boothowto & RB_HALT) {
    495 		printf("\n");
    496 		printf("The operating system has halted.\n");
    497 		printf("Please press any key to reboot.\n\n");
    498 		cnpollc(1);	/* For proper keyboard command handling */
    499 		cngetc();
    500 		cnpollc(0);
    501 	}
    502 
    503 	printf("reseting board...\n\n");
    504 
    505 	/*
    506 	 * Try to use board-specific reset logic, which might involve a better
    507 	 * hardware reset.
    508 	 */
    509 	if (board->ab_reboot)
    510 		board->ab_reboot();
    511 
    512 #if 1
    513 	/* XXX
    514 	 * For some reason we are leaving the ethernet MAC in a state where
    515 	 * YAMON isn't happy with it.  So just call the reset vector (grr,
    516 	 * Alchemy YAMON doesn't have a "reset" command).
    517 	 */
    518 	mips_icache_sync_all();
    519 	mips_dcache_wbinv_all();
    520 	__asm volatile("jr	%0" :: "r"(MIPS_RESET_EXC_VEC));
    521 #else
    522 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    523 	yamon_exit(boothowto);
    524 	printf("Oops, back from yamon_exit()\n\nSpinning...");
    525 #endif
    526 	for (;;)
    527 		/* spin forever */ ;	/* XXX */
    528 	/*NOTREACHED*/
    529 }
    530 
    531 /*
    532  * Export our interrupt map function so aupci can find it.
    533  */
    534 int
    535 aupci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    536 {
    537 	const struct alchemy_board *board;
    538 
    539 	board = board_info();
    540 	if (board->ab_pci_intr_map != NULL)
    541 		return (board->ab_pci_intr_map(pa, ihp));
    542 	return 1;
    543 }
    544 
    545 struct aupcmcia_machdep *
    546 aupcmcia_machdep(void)
    547 {
    548 	const struct alchemy_board *board;
    549 
    550 	board = board_info();
    551 	return (board->ab_pcmcia);
    552 }
    553 
    554 const struct auspi_machdep *
    555 auspi_machdep(bus_addr_t ba)
    556 {
    557 	const struct alchemy_board *board;
    558 
    559 	board = board_info();
    560 	if (board->ab_spi != NULL)
    561 		return (board->ab_spi(ba));
    562 	return NULL;
    563 }
    564