Home | History | Annotate | Line # | Download | only in gdium
machdep.c revision 1.20
      1 /*	$NetBSD: machdep.c,v 1.20 2016/12/22 14:47:56 cherry 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.20 2016/12/22 14:47:56 cherry 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/boot_flag.h>
     85 #include <sys/buf.h>
     86 #include <sys/cpu.h>
     87 #include <sys/device.h>
     88 #include <sys/kcore.h>
     89 #include <sys/kernel.h>
     90 #include <sys/ksyms.h>
     91 #include <sys/mount.h>
     92 #include <sys/reboot.h>
     93 #include <sys/systm.h>
     94 #include <sys/termios.h>
     95 
     96 #include <uvm/uvm_extern.h>
     97 
     98 #include <dev/cons.h>
     99 
    100 #include "ksyms.h"
    101 
    102 #if NKSYMS || defined(DDB) || defined(MODULAR)
    103 #include <mips/db_machdep.h>
    104 #include <ddb/db_extern.h>
    105 #endif
    106 
    107 #include <mips/locore.h>
    108 #include <mips/psl.h>
    109 
    110 #include <mips/bonito/bonitoreg.h>
    111 #include <evbmips/gdium/gdiumvar.h>
    112 
    113 #include "com.h"
    114 #if NCOM > 0
    115 #include <dev/ic/comreg.h>
    116 #include <dev/ic/comvar.h>
    117 
    118 int	comcnrate = 38400;	/* XXX should be config option */
    119 #endif /* NCOM > 0 */
    120 
    121 struct gdium_config gdium_configuration = {
    122 	.gc_bonito = {
    123 		.bc_adbase = 11,	/* magic */
    124 	},
    125 };
    126 
    127 /* For sysctl_hw. */
    128 
    129 /* Maps for VM objects. */
    130 struct vm_map *phys_map = NULL;
    131 
    132 int	netboot;		/* Are we netbooting? */
    133 
    134 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    135 int mem_cluster_cnt;
    136 
    137 void	configure(void);
    138 void	mach_init(int, char **, char **, void *);
    139 
    140 /*
    141  * For some reason, PMON doesn't assign a real address to the Ralink's BAR.
    142  * So we have to do it.
    143  */
    144 static void
    145 gdium_pci_attach_hook(device_t parent, device_t self,
    146     struct pcibus_attach_args *pba)
    147 {
    148 	const pcitag_t high_dev = pci_make_tag(pba->pba_pc, 0, 17, 1);
    149 	const pcitag_t ralink_dev = pci_make_tag(pba->pba_pc, 0, 13, 0);
    150 	bus_size_t high_size, ralink_size;
    151 	pcireg_t v;
    152 
    153 	/*
    154 	 * Get the highest PCI addr used from the last PCI dev.
    155 	 */
    156 	v = pci_conf_read(pba->pba_pc, high_dev, PCI_MAPREG_START);
    157 	v &= PCI_MAPREG_MEM_ADDR_MASK;
    158 
    159 	/*
    160 	 * Get the sizes of the map registers.
    161 	 */
    162 	pci_mapreg_info(pba->pba_pc, high_dev, PCI_MAPREG_START,
    163 	    PCI_MAPREG_MEM_TYPE_32BIT, NULL, &high_size, NULL);
    164 	pci_mapreg_info(pba->pba_pc, ralink_dev, PCI_MAPREG_START,
    165 	    PCI_MAPREG_MEM_TYPE_32BIT, NULL, &ralink_size, NULL);
    166 
    167 	/*
    168 	 * Position the ralink register space after the last device.
    169 	 */
    170 	v = (v + high_size + ralink_size - 1) & ~(ralink_size - 1);
    171 
    172 	/*
    173 	 * Set the mapreg.
    174 	 */
    175 	pci_conf_write(pba->pba_pc, ralink_dev, PCI_MAPREG_START, v);
    176 
    177 #if 0
    178 	/*
    179 	 * Why does linux do this?
    180 	 */
    181 	for (int dev = 15; dev <= 17; dev +=2) {
    182 		for (int func = 0; func <= 1; func++) {
    183 			pcitag_t usb_dev = pci_make_tag(pba->pba_pc, 0, dev, func);
    184 			v = pci_conf_read(pba->pba_pc, usb_dev, 0xe0);
    185 			v |= 3;
    186 			pci_conf_write(pba->pba_pc, usb_dev, 0xe0, v);
    187 		}
    188 	}
    189 #endif
    190 }
    191 
    192 /*
    193  * Do all the stuff that locore normally does before calling main().
    194  */
    195 void
    196 mach_init(int argc, char **argv, char **envp32, void *callvec)
    197 {
    198 	struct gdium_config *gc = &gdium_configuration;
    199 	void *kernend;
    200 #ifdef NOTYET
    201 	char *cp;
    202 	int howto;
    203 #endif
    204 	int i;
    205 	psize_t memsize;
    206 	char *envp[128];
    207 	int32_t *eptrs = (int32_t *)envp32;
    208 	extern char edata[], end[];
    209 
    210 	/*
    211 	 * Clear the BSS segment.
    212 	 */
    213 	kernend = (void *)mips_round_page(end);
    214 	memset(edata, 0, (char *)kernend - edata);
    215 
    216 	/*
    217 	 * the pointer array in envp32 is 32bit - we need to sign extend them
    218 	 * and put them into a list of actual pointers
    219 	 * Only strictly necessary on LP64 but it doesn't hurt in LP32, runs only
    220 	 * once at startup and I'd rather not pollute this file with another
    221 	 * #ifdef orgy
    222 	 */
    223 	i = 0;
    224 	while (i < 128 && eptrs[i] != 0) {
    225 		envp[i] = (char *)(intptr_t)eptrs[i];	/* sign extend */
    226 		i++;
    227 	}
    228 
    229 	/*
    230 	 * Set up the exception vectors and CPU-specific function
    231 	 * vectors early on.  We need the wbflush() vector set up
    232 	 * before comcnattach() is called (or at least before the
    233 	 * first printf() after that is called).
    234 	 * Also clears the I+D caches.
    235 	 */
    236 	mips_vector_init(NULL, false);
    237 
    238 	uvm_md_init();
    239 
    240 	memsize = 256*1024*1024;
    241 	physmem = btoc(memsize);
    242 
    243 	bonito_pci_init(&gc->gc_pc, &gc->gc_bonito);
    244 	/*
    245 	 * Override the null bonito_pci_attach_hook with our own to we can
    246 	 * fix the ralink (device 13).
    247 	 */
    248 	gc->gc_pc.pc_attach_hook = gdium_pci_attach_hook;
    249 	gdium_bus_io_init(&gc->gc_iot, gc);
    250 	gdium_bus_mem_init(&gc->gc_memt, gc);
    251 	gdium_dma_init(gc);
    252 	gdium_cnattach(gc);
    253 
    254 	/*
    255 	 * Disable the 2nd PCI window since we don't need it.
    256 	 */
    257 	mips3_sd(MIPS_PHYS_TO_KSEG1(BONITO_REGBASE + 0x158), 0xe);
    258 	pci_conf_write(&gc->gc_pc, pci_make_tag(&gc->gc_pc, 0, 0, 0), 18, 0);
    259 
    260 	/*
    261 	 * Get the timer from PMON.
    262 	 */
    263 	for (i = 0; envp[i] != NULL; i++) {
    264 		if (!strncmp(envp[i], "cpuclock=", 9)) {
    265 			curcpu()->ci_cpu_freq =
    266 			    strtoul(&envp[i][9], NULL, 10);
    267 			break;
    268 		}
    269 	}
    270 
    271 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    272 		curcpu()->ci_cpu_freq /= 2;
    273 
    274 	/* Compute the number of ticks for hz. */
    275 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    276 
    277 	/* Compute the delay divisor. */
    278 	curcpu()->ci_divisor_delay =
    279 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
    280 
    281 	/*
    282 	 * Get correct cpu frequency if the CPU runs at twice the
    283 	 * external/cp0-count frequency.
    284 	 */
    285 	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    286 		curcpu()->ci_cpu_freq *= 2;
    287 
    288 #ifdef DEBUG
    289 	printf("Timer calibration: %lu cycles/sec\n",
    290 	    curcpu()->ci_cpu_freq);
    291 #endif
    292 
    293 #if NCOM > 0
    294 	/*
    295 	 * Delay to allow firmware putchars to complete.
    296 	 * FIFO depth * character time.
    297 	 * character time = (1000000 / (defaultrate / 10))
    298 	 */
    299 	delay(160000000 / comcnrate);
    300 	if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
    301 	    COM_FREQ, COM_TYPE_NORMAL,
    302 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    303 		panic("malta: unable to initialize serial console");
    304 #endif /* NCOM > 0 */
    305 
    306 	mem_clusters[0].start = 0;
    307 	mem_clusters[0].size = ctob(physmem);
    308 	mem_cluster_cnt = 1;
    309 
    310 	cpu_setmodel("Gdium Liberty 1000");
    311 
    312 	/*
    313 	 * XXX: check argv[0] - do something if "gdb"???
    314 	 */
    315 
    316 	/*
    317 	 * Look at arguments passed to us and compute boothowto.
    318 	 */
    319 	boothowto = RB_AUTOBOOT;
    320 #ifdef NOTYET
    321 	for (i = 1; i < argc; i++) {
    322 		for (cp = argv[i]; *cp; cp++) {
    323 			/* Ignore superfluous '-', if there is one */
    324 			if (*cp == '-')
    325 				continue;
    326 
    327 			howto = 0;
    328 			BOOT_FLAG(*cp, howto);
    329 			if (! howto)
    330 				printf("bootflag '%c' not recognised\n", *cp);
    331 			else
    332 				boothowto |= howto;
    333 		}
    334 	}
    335 #endif
    336 
    337 	/*
    338 	 * Load the rest of the available pages into the VM system.
    339 	 */
    340 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
    341 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    342 
    343 	/*
    344 	 * Initialize error message buffer (at end of core).
    345 	 */
    346 	mips_init_msgbuf();
    347 
    348 	pmap_bootstrap();
    349 
    350 	/*
    351 	 * Allocate uarea page for lwp0 and set it.
    352 	 */
    353 	mips_init_lwp0_uarea();
    354 
    355 	/*
    356 	 * Initialize debuggers, and break into them, if appropriate.
    357 	 */
    358 #if defined(DDB)
    359 	if (boothowto & RB_KDB)
    360 		Debugger();
    361 #endif
    362 }
    363 
    364 void
    365 consinit(void)
    366 {
    367 
    368 	/*
    369 	 * Everything related to console initialization is done
    370 	 * in mach_init().
    371 	 */
    372 }
    373 
    374 /*
    375  * Allocate memory for variable-sized tables,
    376  */
    377 void
    378 cpu_startup(void)
    379 {
    380 	/*
    381 	 *  Do the common startup items.
    382 	 */
    383 	cpu_startup_common();
    384 
    385 	/*
    386 	 * Virtual memory is bootstrapped -- notify the bus spaces
    387 	 * that memory allocation is now safe.
    388 	 */
    389 	gdium_configuration.gc_mallocsafe = 1;
    390 
    391 }
    392 
    393 int	waittime = -1;
    394 
    395 void
    396 cpu_reboot(int howto, char *bootstr)
    397 {
    398 
    399 	/* Take a snapshot before clobbering any registers. */
    400 	savectx(curpcb);
    401 
    402 	if (cold) {
    403 		howto |= RB_HALT;
    404 		goto haltsys;
    405 	}
    406 
    407 	/* If "always halt" was specified as a boot flag, obey. */
    408 	if (boothowto & RB_HALT)
    409 		howto |= RB_HALT;
    410 
    411 	boothowto = howto;
    412 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    413 		waittime = 0;
    414 		vfs_shutdown();
    415 
    416 		/*
    417 		 * If we've been adjusting the clock, the todr
    418 		 * will be out of synch; adjust it now.
    419 		 */
    420 		resettodr();
    421 	}
    422 
    423 	splhigh();
    424 
    425 	if (howto & RB_DUMP)
    426 		dumpsys();
    427 
    428 haltsys:
    429 	doshutdownhooks();
    430 
    431 	pmf_system_shutdown(boothowto);
    432 
    433 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    434 		/*
    435 		 * Turning on GPIO1 as output will cause a powerdown.
    436 		 */
    437 		REGVAL(BONITO_GPIODATA) |= 2;
    438 		REGVAL(BONITO_GPIOIE) &= ~2;
    439 	}
    440 
    441 	if (howto & RB_HALT) {
    442 		printf("\n");
    443 		printf("The operating system has halted.\n");
    444 		printf("Please press any key to reboot.\n\n");
    445 		cnpollc(1);	/* For proper keyboard command handling */
    446 		cngetc();
    447 		cnpollc(0);
    448 	}
    449 
    450 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    451 
    452 	/*
    453 	 * Turning off GPIO2 as output will cause a reset.
    454 	 */
    455 	REGVAL(BONITO_GPIODATA) &= ~4;
    456 	REGVAL(BONITO_GPIOIE) &= ~4;
    457 
    458 	__asm__ __volatile__ (
    459 		"\t.long 0x3c02bfc0\n"
    460 		"\t.long 0x00400008\n"
    461 	    ::: "v0");
    462 }
    463