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