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