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