Home | History | Annotate | Line # | Download | only in atheros
machdep.c revision 1.23
      1 /* $NetBSD: machdep.c,v 1.23 2010/12/15 00:04:51 matt Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5  * Copyright (c) 2006 Garrett D'Amore.
      6  * All rights reserved.
      7  *
      8  * Portions of this code were written by Garrett D'Amore for the
      9  * Champaign-Urbana Community Wireless Network Project.
     10  *
     11  * Redistribution and use in source and binary forms, with or
     12  * without modification, are permitted provided that the following
     13  * conditions are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above
     17  *    copyright notice, this list of conditions and the following
     18  *    disclaimer in the documentation and/or other materials provided
     19  *    with the distribution.
     20  * 3. All advertising materials mentioning features or use of this
     21  *    software must display the following acknowledgements:
     22  *      This product includes software developed by the Urbana-Champaign
     23  *      Independent Media Center.
     24  *	This product includes software developed by Garrett D'Amore.
     25  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26  *    D'Amore's name may not be used to endorse or promote products
     27  *    derived from this software without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42  */
     43 /*-
     44  * Copyright (c) 2006 Itronix Inc.
     45  * All rights reserved.
     46  *
     47  * Portions written by Garrett D'Amore for Itronix Inc.
     48  *
     49  * Redistribution and use in source and binary forms, with or without
     50  * modification, are permitted provided that the following conditions
     51  * are met:
     52  * 1. Redistributions of source code must retain the above copyright
     53  *    notice, this list of conditions and the following disclaimer.
     54  * 2. Redistributions in binary form must reproduce the above copyright
     55  *    notice, this list of conditions and the following disclaimer in the
     56  *    documentation and/or other materials provided with the distribution.
     57  * 3. The name of Itronix Inc. may not be used to endorse
     58  *    or promote products derived from this software without specific
     59  *    prior written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     63  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     64  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     65  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     66  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     67  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     68  * ON ANY THEORY OF LIABILITY, WHETHER IN
     69  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     70  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     71  * POSSIBILITY OF SUCH DAMAGE.
     72  */
     73 /*
     74  * Copyright (c) 1992, 1993
     75  *	The Regents of the University of California.  All rights reserved.
     76  *
     77  * This code is derived from software contributed to Berkeley by
     78  * the Systems Programming Group of the University of Utah Computer
     79  * Science Department, The Mach Operating System project at
     80  * Carnegie-Mellon University and Ralph Campbell.
     81  *
     82  * Redistribution and use in source and binary forms, with or without
     83  * modification, are permitted provided that the following conditions
     84  * are met:
     85  * 1. Redistributions of source code must retain the above copyright
     86  *    notice, this list of conditions and the following disclaimer.
     87  * 2. Redistributions in binary form must reproduce the above copyright
     88  *    notice, this list of conditions and the following disclaimer in the
     89  *    documentation and/or other materials provided with the distribution.
     90  * 3. Neither the name of the University nor the names of its contributors
     91  *    may be used to endorse or promote products derived from this software
     92  *    without specific prior written permission.
     93  *
     94  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     95  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     97  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     98  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     99  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    100  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104  * SUCH DAMAGE.
    105  *
    106  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
    107  * 	from: Utah Hdr: machdep.c 1.63 91/04/24
    108  */
    109 /*
    110  * Copyright (c) 1988 University of Utah.
    111  *
    112  * This code is derived from software contributed to Berkeley by
    113  * the Systems Programming Group of the University of Utah Computer
    114  * Science Department, The Mach Operating System project at
    115  * Carnegie-Mellon University and Ralph Campbell.
    116  *
    117  * Redistribution and use in source and binary forms, with or without
    118  * modification, are permitted provided that the following conditions
    119  * are met:
    120  * 1. Redistributions of source code must retain the above copyright
    121  *    notice, this list of conditions and the following disclaimer.
    122  * 2. Redistributions in binary form must reproduce the above copyright
    123  *    notice, this list of conditions and the following disclaimer in the
    124  *    documentation and/or other materials provided with the distribution.
    125  * 3. All advertising materials mentioning features or use of this software
    126  *    must display the following acknowledgement:
    127  *	This product includes software developed by the University of
    128  *	California, Berkeley and its contributors.
    129  * 4. Neither the name of the University nor the names of its contributors
    130  *    may be used to endorse or promote products derived from this software
    131  *    without specific prior written permission.
    132  *
    133  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    134  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    135  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    136  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    137  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    138  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    139  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    140  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    141  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    142  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    143  * SUCH DAMAGE.
    144  *
    145  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
    146  * 	from: Utah Hdr: machdep.c 1.63 91/04/24
    147  */
    148 
    149 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
    150 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.23 2010/12/15 00:04:51 matt Exp $");
    151 
    152 #include "opt_ddb.h"
    153 #include "opt_kgdb.h"
    154 #include "opt_modular.h"
    155 
    156 #include <sys/param.h>
    157 #include <sys/systm.h>
    158 #include <sys/kernel.h>
    159 #include <sys/buf.h>
    160 #include <sys/reboot.h>
    161 #include <sys/mount.h>
    162 #include <sys/kcore.h>
    163 #include <sys/boot_flag.h>
    164 #include <sys/termios.h>
    165 #include <sys/ksyms.h>
    166 #include <sys/device.h>
    167 
    168 #include <uvm/uvm_extern.h>
    169 
    170 #include <dev/cons.h>
    171 
    172 #include "ksyms.h"
    173 
    174 #if NKSYMS || defined(DDB) || defined(MODULAR)
    175 #include <machine/db_machdep.h>
    176 #include <ddb/db_extern.h>
    177 #endif
    178 
    179 #include <mips/cache.h>
    180 #include <mips/locore.h>
    181 #include <mips/cpuregs.h>
    182 
    183 #include <mips/atheros/include/ar531xvar.h>
    184 #include <mips/atheros/include/arbusvar.h>
    185 
    186 /* Our exported CPU info; we can have only one. */
    187 struct cpu_info cpu_info_store;
    188 
    189 /* Maps for VM objects. */
    190 struct vm_map *phys_map = NULL;
    191 
    192 int maxmem;			/* max memory per process */
    193 
    194 int mem_cluster_cnt;
    195 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    196 
    197 void	mach_init(void); /* XXX */
    198 
    199 static void
    200 cal_timer(void)
    201 {
    202 	uint32_t	cntfreq;
    203 
    204 	cntfreq = curcpu()->ci_cpu_freq = ar531x_cpu_freq();
    205 
    206 	/* MIPS 4Kc CP0 counts every other clock */
    207 	if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    208 		cntfreq /= 2;
    209 
    210 	curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
    211 
    212 	/* Compute number of cycles per 1us (1/MHz). 0.5MHz is for roundup. */
    213 	curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
    214 }
    215 
    216 void
    217 mach_init(void)
    218 {
    219 	void *kernend;
    220 	u_long first, last;
    221 	uint32_t memsize;
    222 
    223 	extern char edata[], end[];	/* XXX */
    224 
    225 	/* clear the BSS segment */
    226 	kernend = (void *)mips_round_page(end);
    227 
    228 	memset(edata, 0, (char *)kernend - edata);
    229 
    230 	/* setup early console */
    231 	ar531x_early_console();
    232 
    233 	/* set CPU model info for sysctl_hw */
    234 	snprintf(cpu_model, 64, "%s", ar531x_cpuname());
    235 
    236 	/*
    237 	 * Set up the exception vectors and CPU-specific function
    238 	 * vectors early on.  We need the wbflush() vector set up
    239 	 * before comcnattach() is called (or at least before the
    240 	 * first printf() after that is called).
    241 	 * Sets up mips_cpu_flags that may be queried by other
    242 	 * functions called during startup.
    243 	 * Also clears the I+D caches.
    244 	 */
    245 	mips_vector_init();
    246 
    247 	/*
    248 	 * Calibrate timers.
    249 	 */
    250 	cal_timer();
    251 
    252 	/*
    253 	 * Set the VM page size.
    254 	 */
    255 	uvm_setpagesize();
    256 
    257 	/*
    258 	 * Look at arguments passed to us and compute boothowto.
    259 	 */
    260 	boothowto = RB_AUTOBOOT;
    261 #ifdef KADB
    262 	boothowto |= RB_KDB;
    263 #endif
    264 
    265 	/*
    266 	 * This would be a good place to parse a boot command line, if
    267 	 * we got one from the bootloader.  Right now we have no way to
    268 	 * get one from e.g. vxworks.
    269 	 */
    270 
    271 	/*
    272 	 * Determine the memory size.
    273 	 *
    274 	 * Note: Reserve the first page!  That's where the trap
    275 	 * vectors are located.
    276 	 */
    277 	memsize = ar531x_memsize();
    278 
    279 	printf("Memory size: 0x%08x\n", memsize);
    280 	physmem = btoc(memsize);
    281 
    282 	mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
    283 	mem_clusters[mem_cluster_cnt].size =
    284 	    memsize - mem_clusters[mem_cluster_cnt].start;
    285 	mem_cluster_cnt++;
    286 
    287 	/*
    288 	 * Load the rest of the available pages into the VM system.
    289 	 */
    290 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    291 	last = mem_clusters[0].start + mem_clusters[0].size;
    292 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    293 	    VM_FREELIST_DEFAULT);
    294 
    295 	/*
    296 	 * Initialize message buffer (at end of core).
    297 	 */
    298 	mips_init_msgbuf();
    299 
    300 	/*
    301 	 * Initialize the virtual memory system.
    302 	 */
    303 	pmap_bootstrap();
    304 
    305 	/*
    306 	 * Allocate uarea page for lwp0 and set it.
    307 	 */
    308 	mips_init_lwp0_uarea();
    309 
    310 	/*
    311 	 * Initialize busses.
    312 	 */
    313 	ar531x_businit();
    314 
    315 	/*
    316 	 * Turn off (ignore) the hardware watchdog.  If we got this
    317 	 * far, then we shouldn't need it anymore.
    318 	 */
    319 	ar531x_wdog(0);
    320 
    321 	/*
    322 	 * Turn off watchpoint that may have been enabled by the
    323 	 * PROM.  VxWorks bootloader seems to leave one set.
    324 	 */
    325 	__asm volatile (
    326 		"mtc0	$0, $" ___STRING(MIPS_COP_0_WATCH_LO) " \n\t"
    327 		"nop\n\t"
    328 		"nop\n\t");
    329 
    330 	/*
    331 	 * Initialize debuggers, and break into them, if appropriate.
    332 	 */
    333 #ifdef DDB
    334 	if (boothowto & RB_KDB)
    335 		Debugger();
    336 #endif
    337 }
    338 
    339 void
    340 consinit(void)
    341 {
    342 
    343 	/*
    344 	 * Everything related to console initialization is done
    345 	 * in mach_init().
    346 	 */
    347 	ar531x_consinit();
    348 }
    349 
    350 void
    351 cpu_startup(void)
    352 {
    353 	char pbuf[9];
    354 	vaddr_t minaddr, maxaddr;
    355 #ifdef DEBUG
    356 	extern int pmapdebug;		/* XXX */
    357 	int opmapdebug = pmapdebug;
    358 
    359 	pmapdebug = 0;		/* Shut up pmap debug during bootstrap */
    360 #endif
    361 
    362 	/*
    363 	 * Good {morning,afternoon,evening,night}.
    364 	 */
    365 	printf("%s%s", copyright, version);
    366 	printf("%s\n", cpu_model);
    367 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    368 	printf("total memory = %s\n", pbuf);
    369 
    370 	minaddr = 0;
    371 	/*
    372 	 * Allocate a submap for physio
    373 	 */
    374 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    375 	    VM_PHYS_SIZE, 0, FALSE, NULL);
    376 
    377 	/*
    378 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    379 	 * are allocated via the pool allocator, and we use KSEG to
    380 	 * map those pages.
    381 	 */
    382 
    383 #ifdef DEBUG
    384 	pmapdebug = opmapdebug;
    385 #endif
    386 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    387 	printf("avail memory = %s\n", pbuf);
    388 }
    389 
    390 void
    391 cpu_reboot(int howto, char *bootstr)
    392 {
    393 	static int waittime = -1;
    394 
    395 	/* Take a snapshot before clobbering any registers. */
    396 	if (curproc)
    397 		savectx(curpcb);
    398 
    399 	/* If "always halt" was specified as a boot flag, obey. */
    400 	if (boothowto & RB_HALT)
    401 		howto |= RB_HALT;
    402 
    403 	boothowto = howto;
    404 
    405 	/* If system is cold, just halt. */
    406 	if (cold) {
    407 		boothowto |= RB_HALT;
    408 		goto haltsys;
    409 	}
    410 
    411 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    412 		waittime = 0;
    413 
    414 		/*
    415 		 * Synchronize the disks....
    416 		 */
    417 		vfs_shutdown();
    418 
    419 		/*
    420 		 * If we've been adjusting the clock, the todr
    421 		 * will be out of synch; adjust it now.
    422 		 */
    423 		resettodr();
    424 	}
    425 
    426 	/* Disable interrupts. */
    427 	splhigh();
    428 
    429 	if (boothowto & RB_DUMP)
    430 		dumpsys();
    431 
    432  haltsys:
    433 	/* Run any shutdown hooks. */
    434 	doshutdownhooks();
    435 
    436 	pmf_system_shutdown(boothowto);
    437 
    438 #if 0
    439 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
    440 		if (board && board->ab_poweroff)
    441 			board->ab_poweroff();
    442 #endif
    443 
    444 	/*
    445 	 * Firmware may autoboot (depending on settings), and we cannot pass
    446 	 * flags to it (at least I haven't figured out how to yet), so
    447 	 * we "pseudo-halt" now.
    448 	 */
    449 	if (boothowto & RB_HALT) {
    450 		printf("\n");
    451 		printf("The operating system has halted.\n");
    452 		printf("Please press any key to reboot.\n\n");
    453 		cnpollc(1);	/* For proper keyboard command handling */
    454 		cngetc();
    455 		cnpollc(0);
    456 	}
    457 
    458 	printf("reseting board...\n\n");
    459 	mips_icache_sync_all();
    460 	mips_dcache_wbinv_all();
    461 	__asm volatile("jr	%0" :: "r"(MIPS_RESET_EXC_VEC));
    462 	printf("Oops, back from reset\n\nSpinning...");
    463 	for (;;)
    464 		/* spin forever */ ;	/* XXX */
    465 	/*NOTREACHED*/
    466 }
    467