Home | History | Annotate | Line # | Download | only in gdium
machdep.c revision 1.7
      1 /*	$NetBSD: machdep.c,v 1.7 2009/08/11 04:46:21 matt 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) 1992, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * This code is derived from software contributed to Berkeley by
     43  * the Systems Programming Group of the University of Utah Computer
     44  * Science Department, The Mach Operating System project at
     45  * Carnegie-Mellon University and Ralph Campbell.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     72  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     73  */
     74 /*
     75  * Copyright (c) 1988 University of Utah.
     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. All advertising materials mentioning features or use of this software
     91  *    must display the following acknowledgement:
     92  *	This product includes software developed by the University of
     93  *	California, Berkeley and its contributors.
     94  * 4. Neither the name of the University nor the names of its contributors
     95  *    may be used to endorse or promote products derived from this software
     96  *    without specific prior written permission.
     97  *
     98  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     99  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    101  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    102  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    103  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    104  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    108  * SUCH DAMAGE.
    109  *
    110  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
    111  *	from: Utah Hdr: machdep.c 1.63 91/04/24
    112  */
    113 
    114 #include <sys/cdefs.h>
    115 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.7 2009/08/11 04:46:21 matt Exp $");
    116 
    117 #include "opt_ddb.h"
    118 #include "opt_execfmt.h"
    119 #include "opt_modular.h"
    120 
    121 #include <sys/param.h>
    122 #include <sys/systm.h>
    123 #include <sys/kernel.h>
    124 #include <sys/buf.h>
    125 #include <sys/reboot.h>
    126 #include <sys/user.h>
    127 #include <sys/mount.h>
    128 #include <sys/kcore.h>
    129 #include <sys/boot_flag.h>
    130 #include <sys/termios.h>
    131 #include <sys/ksyms.h>
    132 #include <sys/device.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 <machine/cpu.h>
    146 #include <machine/psl.h>
    147 
    148 #include <mips/bonito/bonitoreg.h>
    149 #include <evbmips/gdium/gdiumvar.h>
    150 
    151 #include "com.h"
    152 #if NCOM > 0
    153 #include <dev/ic/comreg.h>
    154 #include <dev/ic/comvar.h>
    155 
    156 int	comcnrate = 38400;	/* XXX should be config option */
    157 #endif /* NCOM > 0 */
    158 
    159 struct gdium_config gdium_configuration = {
    160 	.gc_bonito = {
    161 		.bc_adbase = 11,	/* magic */
    162 	},
    163 };
    164 
    165 /* For sysctl_hw. */
    166 extern char cpu_model[];
    167 
    168 /* Our exported CPU info; we can have only one. */
    169 struct cpu_info cpu_info_store;
    170 
    171 /* Maps for VM objects. */
    172 struct vm_map *mb_map = NULL;
    173 struct vm_map *phys_map = NULL;
    174 
    175 int	netboot;		/* Are we netbooting? */
    176 
    177 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    178 int mem_cluster_cnt;
    179 
    180 void	configure(void);
    181 void	mach_init(int, char **, char **, void *);
    182 
    183 /*
    184  * safepri is a safe priority for sleep to set for a spin-wait during
    185  * autoconfiguration or after a panic.  Used as an argument to splx().
    186  */
    187 int	safepri = MIPS1_PSL_LOWIPL;
    188 
    189 extern struct user *proc0paddr;
    190 
    191 /*
    192  * For some reason, PMON doesn't assign a real address to the Ralink's BAR.
    193  * So we have to do it.
    194  */
    195 static void
    196 gdium_pci_attach_hook(device_t parent, device_t self,
    197     struct pcibus_attach_args *pba)
    198 {
    199 	const pcitag_t high_dev = pci_make_tag(pba->pba_pc, 0, 17, 1);
    200 	const pcitag_t ralink_dev = pci_make_tag(pba->pba_pc, 0, 13, 0);
    201 	bus_size_t high_size, ralink_size;
    202 	pcireg_t v;
    203 
    204 	/*
    205 	 * Get the highest PCI addr used from the last PCI dev.
    206 	 */
    207 	v = pci_conf_read(pba->pba_pc, high_dev, PCI_MAPREG_START);
    208 	v &= PCI_MAPREG_MEM_ADDR_MASK;
    209 
    210 	/*
    211 	 * Get the sizes of the map registers.
    212 	 */
    213 	pci_mapreg_info(pba->pba_pc, high_dev, PCI_MAPREG_START,
    214 	    PCI_MAPREG_MEM_TYPE_32BIT, NULL, &high_size, NULL);
    215 	pci_mapreg_info(pba->pba_pc, ralink_dev, PCI_MAPREG_START,
    216 	    PCI_MAPREG_MEM_TYPE_32BIT, NULL, &ralink_size, NULL);
    217 
    218 	/*
    219 	 * Position the ralink register space after the last device.
    220 	 */
    221 	v = (v + high_size + ralink_size - 1) & ~(ralink_size - 1);
    222 
    223 	/*
    224 	 * Set the mapreg.
    225 	 */
    226 	pci_conf_write(pba->pba_pc, ralink_dev, PCI_MAPREG_START, v);
    227 
    228 #if 0
    229 	/*
    230 	 * Why does linux do this?
    231 	 */
    232 	for (int dev = 15; dev <= 17; dev +=2) {
    233 		for (int func = 0; func <= 1; func++) {
    234 			pcitag_t usb_dev = pci_make_tag(pba->pba_pc, 0, dev, func);
    235 			v = pci_conf_read(pba->pba_pc, usb_dev, 0xe0);
    236 			v |= 3;
    237 			pci_conf_write(pba->pba_pc, usb_dev, 0xe0, v);
    238 		}
    239 	}
    240 #endif
    241 }
    242 
    243 /*
    244  * Do all the stuff that locore normally does before calling main().
    245  */
    246 void
    247 mach_init(int argc, char **argv, char **envp, void *callvec)
    248 {
    249 	struct gdium_config *gc = &gdium_configuration;
    250 	void *kernend, *v;
    251         u_long first, last;
    252 #ifdef NOTYET
    253 	char *cp;
    254 	int howto;
    255 #endif
    256 	int i;
    257 	psize_t memsize;
    258 
    259 	extern char edata[], end[];
    260 
    261 	/*
    262 	 * Clear the BSS segment.
    263 	 */
    264 	kernend = (void *)mips_round_page(end);
    265 	memset(edata, 0, (char *)kernend - edata);
    266 
    267 	/*
    268 	 * Set up the exception vectors and CPU-specific function
    269 	 * vectors early on.  We need the wbflush() vector set up
    270 	 * before comcnattach() is called (or at least before the
    271 	 * first printf() after that is called).
    272 	 * Also clears the I+D caches.
    273 	 */
    274 	mips_vector_init();
    275 
    276 	/* set the VM page size */
    277 	uvm_setpagesize();
    278 
    279 	memsize = 256*1024*1024;
    280 	physmem = btoc(memsize);
    281 
    282 	bonito_pci_init(&gc->gc_pc, &gc->gc_bonito);
    283 	/*
    284 	 * Override the null bonito_pci_attach_hook with our own to we can
    285 	 * fix the ralink (device 13).
    286 	 */
    287 	gc->gc_pc.pc_attach_hook = gdium_pci_attach_hook;
    288 	gdium_bus_io_init(&gc->gc_iot, gc);
    289 	gdium_bus_mem_init(&gc->gc_memt, gc);
    290 	gdium_dma_init(gc);
    291 	gdium_cnattach(gc);
    292 
    293 	/*
    294 	 * Disable the 2nd PCI window since we don't need it.
    295 	 */
    296 	REGVAL(BONITO_REGBASE + 0x158) = 0xe;
    297 	REGVAL(BONITO_REGBASE + 0x15c) = 0;
    298 	pci_conf_write(&gc->gc_pc, pci_make_tag(&gc->gc_pc, 0, 0, 0), 18, 0);
    299 
    300 	/*
    301 	 * Get the timer from PMON.
    302 	 */
    303 	for (i = 0; envp[i] != NULL; i++) {
    304 		if (!strncmp(envp[i], "cpuclock=", 9)) {
    305 			curcpu()->ci_cpu_freq =
    306 			    strtoul(&envp[i][9], NULL, 10);
    307 			break;
    308 		}
    309 	}
    310 
    311 	if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    312 		curcpu()->ci_cpu_freq /= 2;
    313 
    314 	/* Compute the number of ticks for hz. */
    315 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    316 
    317 	/* Compute the delay divisor. */
    318 	curcpu()->ci_divisor_delay =
    319 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
    320 
    321 	/*
    322 	 * Get correct cpu frequency if the CPU runs at twice the
    323 	 * external/cp0-count frequency.
    324 	 */
    325 	if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    326 		curcpu()->ci_cpu_freq *= 2;
    327 
    328 #ifdef DEBUG
    329 	printf("Timer calibration: %lu cycles/sec\n",
    330 	    curcpu()->ci_cpu_freq);
    331 #endif
    332 
    333 #if NCOM > 0
    334 	/*
    335 	 * Delay to allow firmware putchars to complete.
    336 	 * FIFO depth * character time.
    337 	 * character time = (1000000 / (defaultrate / 10))
    338 	 */
    339 	delay(160000000 / comcnrate);
    340 	if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
    341 	    COM_FREQ, COM_TYPE_NORMAL,
    342 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    343 		panic("malta: unable to initialize serial console");
    344 #endif /* NCOM > 0 */
    345 
    346 	mem_clusters[0].start = 0;
    347 	mem_clusters[0].size = ctob(physmem);
    348 	mem_cluster_cnt = 1;
    349 
    350 	strcpy(cpu_model, "Gdium Liberty 1000");
    351 
    352 	/*
    353 	 * XXX: check argv[0] - do something if "gdb"???
    354 	 */
    355 
    356 	/*
    357 	 * Look at arguments passed to us and compute boothowto.
    358 	 */
    359 	boothowto = RB_AUTOBOOT;
    360 #ifdef NOTYET
    361 	for (i = 1; i < argc; i++) {
    362 		for (cp = argv[i]; *cp; cp++) {
    363 			/* Ignore superfluous '-', if there is one */
    364 			if (*cp == '-')
    365 				continue;
    366 
    367 			howto = 0;
    368 			BOOT_FLAG(*cp, howto);
    369 			if (! howto)
    370 				printf("bootflag '%c' not recognised\n", *cp);
    371 			else
    372 				boothowto |= howto;
    373 		}
    374 	}
    375 #endif
    376 
    377 	/*
    378 	 * Load the rest of the available pages into the VM system.
    379 	 */
    380 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    381 	last = mem_clusters[0].start + mem_clusters[0].size;
    382 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    383 		VM_FREELIST_DEFAULT);
    384 
    385 	/*
    386 	 * Initialize error message buffer (at end of core).
    387 	 */
    388 	mips_init_msgbuf();
    389 
    390 	pmap_bootstrap();
    391 
    392 	/*
    393 	 * Allocate space for proc0's USPACE.
    394 	 */
    395 	v = (void *)uvm_pageboot_alloc(USPACE);
    396 	lwp0.l_addr = proc0paddr = (struct user *)v;
    397 	lwp0.l_md.md_regs = (struct frame *)((char *)v + USPACE) - 1;
    398 	proc0paddr->u_pcb.pcb_context[11] =
    399 	    MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    400 
    401 	/*
    402 	 * Initialize debuggers, and break into them, if appropriate.
    403 	 */
    404 #if defined(DDB)
    405 	if (boothowto & RB_KDB)
    406 		Debugger();
    407 #endif
    408 }
    409 
    410 void
    411 consinit(void)
    412 {
    413 
    414 	/*
    415 	 * Everything related to console initialization is done
    416 	 * in mach_init().
    417 	 */
    418 }
    419 
    420 /*
    421  * Allocate memory for variable-sized tables,
    422  */
    423 void
    424 cpu_startup(void)
    425 {
    426 	vaddr_t minaddr, maxaddr;
    427 	char pbuf[9];
    428 
    429 	/*
    430 	 * Good {morning,afternoon,evening,night}.
    431 	 */
    432 	printf("%s%s", copyright, version);
    433 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    434 	printf("total memory = %s\n", pbuf);
    435 
    436 	/*
    437 	 * Virtual memory is bootstrapped -- notify the bus spaces
    438 	 * that memory allocation is now safe.
    439 	 */
    440 	gdium_configuration.gc_mallocsafe = 1;
    441 
    442 	minaddr = 0;
    443 	/*
    444 	 * Allocate a submap for physio.
    445 	 */
    446 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    447 				    VM_PHYS_SIZE, 0, FALSE, NULL);
    448 
    449 	/*
    450 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
    451 	 * are allocated via the pool allocator, and we use KSEG to
    452 	 * map those pages.)
    453 	 */
    454 
    455 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    456 	printf("avail memory = %s\n", pbuf);
    457 }
    458 
    459 int	waittime = -1;
    460 
    461 void
    462 cpu_reboot(int howto, char *bootstr)
    463 {
    464 
    465 	/* Take a snapshot before clobbering any registers. */
    466 	if (curproc)
    467 		savectx((struct user *)curpcb);
    468 
    469 	if (cold) {
    470 		howto |= RB_HALT;
    471 		goto haltsys;
    472 	}
    473 
    474 	/* If "always halt" was specified as a boot flag, obey. */
    475 	if (boothowto & RB_HALT)
    476 		howto |= RB_HALT;
    477 
    478 	boothowto = howto;
    479 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    480 		waittime = 0;
    481 		vfs_shutdown();
    482 
    483 		/*
    484 		 * If we've been adjusting the clock, the todr
    485 		 * will be out of synch; adjust it now.
    486 		 */
    487 		resettodr();
    488 	}
    489 
    490 	splhigh();
    491 
    492 	if (howto & RB_DUMP)
    493 		dumpsys();
    494 
    495 haltsys:
    496 	doshutdownhooks();
    497 
    498 	pmf_system_shutdown(boothowto);
    499 
    500 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    501 		/*
    502 		 * Turning on GPIO1 as output will cause a powerdown.
    503 		 */
    504 		REGVAL(BONITO_GPIODATA) |= 2;
    505 		REGVAL(BONITO_GPIOIE) &= ~2;
    506 	}
    507 
    508 	if (howto & RB_HALT) {
    509 		printf("\n");
    510 		printf("The operating system has halted.\n");
    511 		printf("Please press any key to reboot.\n\n");
    512 		cnpollc(1);	/* For proper keyboard command handling */
    513 		cngetc();
    514 		cnpollc(0);
    515 	}
    516 
    517 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    518 
    519 	/*
    520 	 * Turning off GPIO2 as output will cause a reset.
    521 	 */
    522 	REGVAL(BONITO_GPIODATA) &= ~4;
    523 	REGVAL(BONITO_GPIOIE) &= ~4;
    524 
    525 	__asm__ __volatile__ (
    526 		"\t.long 0x3c02bfc0\n"
    527 		"\t.long 0x00400008\n"
    528 	    ::: "v0");
    529 }
    530