Home | History | Annotate | Line # | Download | only in alchemy
machdep.c revision 1.7
      1 /* $NetBSD: machdep.c,v 1.7 2003/04/02 03:51:33 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1988 University of Utah.
      5  * Copyright (c) 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * the Systems Programming Group of the University of Utah Computer
     10  * Science Department, The Mach Operating System project at
     11  * Carnegie-Mellon University and Ralph Campbell.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by the University of
     24  *	California, Berkeley and its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
     42  * 	from: Utah Hdr: machdep.c 1.63 91/04/24
     43  */
     44 
     45 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     46 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.7 2003/04/02 03:51:33 thorpej Exp $");
     47 
     48 #include "opt_ddb.h"
     49 #include "opt_kgdb.h"
     50 
     51 #include "opt_memsize.h"
     52 #include "opt_ethaddr.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/kernel.h>
     57 #include <sys/buf.h>
     58 #include <sys/reboot.h>
     59 #include <sys/user.h>
     60 #include <sys/mount.h>
     61 #include <sys/kcore.h>
     62 #include <sys/boot_flag.h>
     63 #include <sys/termios.h>
     64 
     65 #include <net/if.h>
     66 #include <net/if_ether.h>
     67 
     68 #include <uvm/uvm_extern.h>
     69 
     70 #include <dev/cons.h>
     71 
     72 #ifdef DDB
     73 #include <machine/db_machdep.h>
     74 #include <ddb/db_extern.h>
     75 #endif
     76 
     77 #include <mips/cache.h>
     78 #include <mips/locore.h>
     79 #include <machine/yamon.h>
     80 
     81 #include <evbmips/alchemy/pb1000var.h>
     82 #include <mips/alchemy/include/aureg.h>
     83 #include <mips/alchemy/include/auvar.h>
     84 #include <mips/alchemy/include/aubusvar.h>
     85 
     86 #include "aucom.h"
     87 #if NAUCOM > 0
     88 #include <mips/alchemy/dev/aucomvar.h>
     89 
     90 #ifndef CONSPEED
     91 #define CONSPEED TTYDEF_SPEED
     92 #endif
     93 int	aucomcnrate = CONSPEED;
     94 #endif /* NAUCOM > 0 */
     95 
     96 #include "ohci.h"
     97 
     98 /* The following are used externally (sysctl_hw). */
     99 extern char	cpu_model[];
    100 
    101 struct	user *proc0paddr;
    102 
    103 /* Our exported CPU info; we can have only one. */
    104 struct cpu_info cpu_info_store;
    105 
    106 /* Maps for VM objects. */
    107 struct vm_map *exec_map = NULL;
    108 struct vm_map *mb_map = NULL;
    109 struct vm_map *phys_map = NULL;
    110 
    111 int physmem;		/* # pages of physical memory */
    112 int maxmem;			/* max memory per process */
    113 
    114 int mem_cluster_cnt;
    115 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    116 
    117 yamon_env_var *yamon_envp;
    118 struct pb1000_config pb1000_configuration;
    119 struct propdb *alchemy_prop_info;
    120 
    121 void	mach_init(int, char **, yamon_env_var *, u_long); /* XXX */
    122 
    123 void
    124 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
    125 {
    126 	struct pb1000_config *pbc = &pb1000_configuration;
    127 	bus_space_handle_t sh;
    128 	caddr_t kernend;
    129 	const char *cp;
    130 	u_long first, last;
    131 	caddr_t v;
    132 	int howto, i;
    133 
    134 	extern char edata[], end[];	/* XXX */
    135 
    136 	/* clear the BSS segment */
    137 	kernend = (caddr_t)mips_round_page(end);
    138 	memset(edata, 0, kernend - (caddr_t)edata);
    139 
    140 	/* set cpu model info for sysctl_hw */
    141 	strcpy(cpu_model, "Alchemy Semiconductor Pb1000");
    142 
    143 	/* save the yamon environment pointer */
    144 	yamon_envp = envp;
    145 
    146 	/* Use YAMON callbacks for early console I/O */
    147 	cn_tab = &yamon_promcd;
    148 
    149 	/*
    150 	 * Set up the exception vectors and cpu-specific function
    151 	 * vectors early on.  We need the wbflush() vector set up
    152 	 * before comcnattach() is called (or at least before the
    153 	 * first printf() after that is called).
    154 	 * Also clears the I+D caches.
    155 	 */
    156 	mips_vector_init();
    157 
    158 	/*
    159 	 * Set the VM page size.
    160 	 */
    161 	uvm_setpagesize();
    162 
    163 	/*
    164 	 * Initialize bus space tags.
    165 	 */
    166 	au_cpureg_bus_mem_init(&pbc->pc_cpuregt, pbc);
    167 	aubus_st = &pbc->pc_cpuregt;		/* XXX: for aubus.c */
    168 
    169 	/*
    170 	 * Calibrate the timer, delay() relies on this.
    171 	 */
    172 	bus_space_map(&pbc->pc_cpuregt, PC_BASE, PC_SIZE, 0, &sh);
    173 	au_cal_timers(&pbc->pc_cpuregt, sh);
    174 	bus_space_unmap(&pbc->pc_cpuregt, sh, PC_SIZE);
    175 
    176 	/*
    177 	 * Bring up the console.
    178 	 */
    179 #if NAUCOM > 0
    180 	/*
    181 	 * Delay to allow firmware putchars to complete.
    182 	 * FIFO depth * character time.
    183 	 * character time = (1000000 / (defaultrate / 10))
    184 	 */
    185 	delay(160000000 / aucomcnrate);
    186 	if (aucomcnattach(&pbc->pc_cpuregt, UART0_BASE, aucomcnrate,
    187 	    curcpu()->ci_cpu_freq / 4,
    188 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    189 		panic("pb1000: unable to initialize serial console");
    190 #else
    191 	panic("pb1000: not configured to use serial console");
    192 #endif /* NAUCOM > 0 */
    193 
    194 	/*
    195 	 * Look at arguments passed to us and compute boothowto.
    196 	 */
    197 	boothowto = RB_AUTOBOOT;
    198 #ifdef KADB
    199 	boothowto |= RB_KDB;
    200 #endif
    201 	for (i = 1; i < argc; i++) {
    202 		for (cp = argv[i]; *cp; cp++) {
    203 			/* Ignore superfluous '-', if there is one */
    204 			if (*cp == '-')
    205 				continue;
    206 
    207 			howto = 0;
    208 			BOOT_FLAG(*cp, howto);
    209 			if (! howto)
    210 				printf("bootflag '%c' not recognised\n", *cp);
    211 			else
    212 				boothowto |= howto;
    213 		}
    214 	}
    215 
    216 	/*
    217 	 * Determine the memory size.  Use the `memsize' PMON
    218 	 * variable.  If that's not available, panic.
    219 	 *
    220 	 * Note: Reserve the first page!  That's where the trap
    221 	 * vectors are located.
    222 	 */
    223 
    224 #if defined(MEMSIZE)
    225 	memsize = MEMSIZE;
    226 #else
    227 	if (memsize == 0) {
    228 		if ((cp = yamon_getenv("memsize")) != NULL)
    229 			memsize = strtoul(cp, NULL, 0);
    230 		else {
    231 			printf("FATAL: `memsize' YAMON variable not set.  Set it to\n");
    232 			printf("       the amount of memory (in MB) and try again.\n");
    233 			printf("       Or, build a kernel with the `MEMSIZE' "
    234 			    "option.\n");
    235 			panic("pb1000_init");
    236 		}
    237 	}
    238 #endif /* MEMSIZE */
    239 	printf("Memory size: 0x%08lx\n", memsize);
    240 	physmem = btoc(memsize);
    241 
    242 	mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
    243 	mem_clusters[mem_cluster_cnt].size =
    244 	    memsize - mem_clusters[mem_cluster_cnt].start;
    245 	mem_cluster_cnt++;
    246 
    247 	/*
    248 	 * Load the rest of the available pages into the VM system.
    249 	 */
    250 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    251 	last = mem_clusters[0].start + mem_clusters[0].size;
    252 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    253 	    VM_FREELIST_DEFAULT);
    254 
    255 	/*
    256 	 * Initialize message buffer (at end of core).
    257 	 */
    258 	mips_init_msgbuf();
    259 
    260 	/*
    261 	 * Compute the size of system data structures.  pmap_bootstrap()
    262 	 * needs some of this information.
    263 	 */
    264 	memsize = (u_long)allocsys(NULL, NULL);
    265 
    266 	/*
    267 	 * Initialize the virtual memory system.
    268 	 */
    269 	pmap_bootstrap();
    270 
    271 	/*
    272 	 * Init mapping for u page(s) for proc0.
    273 	 */
    274 	v = (caddr_t) uvm_pageboot_alloc(USPACE);
    275 	lwp0.l_addr = proc0paddr = (struct user *)v;
    276 	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
    277 	curpcb = &lwp0.l_addr->u_pcb;
    278 	curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    279 
    280 	/*
    281 	 * Allocate space for system data structures.  These data structures
    282 	 * are allocated here instead of cpu_startup() because physical
    283 	 * memory is directly addressable.  We don't have to map these into
    284 	 * the virtual address space.
    285 	 */
    286 	v = (caddr_t)uvm_pageboot_alloc(memsize);
    287 	if ((allocsys(v, NULL) - v) != memsize)
    288 		panic("mach_init: table size inconsistency");
    289 
    290 #if NOHCI > 0
    291 	{
    292 #define	USBH_ALL   (0x1f<<10)  /* All relevant bits in USBH portion of SYS_CLKSRC */
    293 		/*
    294 		 * Assign a clock for the USB Host controller.
    295 		 */
    296 		volatile u_int32_t *scsreg;
    297 		u_int32_t	tmp;
    298 
    299 		scsreg = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_CLKSRC));
    300 #if 0
    301 		auxpll = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_AUXPLL));
    302 		*auxpll = 8;		/* 96Mhz */
    303 		tmp = *sfc0;
    304 		tmp |= SFC_FE0|SFC_FRDIV0;
    305 		*sfc0 = tmp;
    306 #endif
    307 		tmp = *scsreg;
    308 		tmp &= ~USBH_ALL;	/* clear all USBH bits in SYS_CLKSRC first */
    309 		tmp |= (SCS_DUH|SCS_CUH|SCS_MUH(SCS_MEx_AUX));	/* 48Mhz */
    310 		*scsreg = tmp;
    311 	}
    312 #endif   /* NOHCI */
    313 	/*
    314 	 * Initialize debuggers, and break into them, if appropriate.
    315 	 */
    316 #ifdef DDB
    317 	ddb_init(0, 0, 0);
    318 	if (boothowto & RB_KDB)
    319 		Debugger();
    320 #endif
    321 }
    322 
    323 void
    324 consinit(void)
    325 {
    326 
    327 	/*
    328 	 * Everything related to console initialization is done
    329 	 * in mach_init().
    330 	 */
    331 }
    332 
    333 void
    334 cpu_startup(void)
    335 {
    336 	char pbuf[9];
    337 	vaddr_t minaddr, maxaddr;
    338 	vsize_t size;
    339 	u_int i, base, residual;
    340 #ifdef DEBUG
    341 	extern int pmapdebug;		/* XXX */
    342 	int opmapdebug = pmapdebug;
    343 
    344 	pmapdebug = 0;		/* Shut up pmap debug during bootstrap */
    345 #endif
    346 
    347 	/*
    348 	 * Good {morning,afternoon,evening,night}.
    349 	 */
    350 	printf(version);
    351 	printf("%s\n", cpu_model);
    352 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    353 	printf("total memory = %s\n", pbuf);
    354 
    355 	/*
    356 	 * Allocate virtual address space for file I/O buffers.
    357 	 * Note they are different than the array of headers, 'buf',
    358 	 * and usually occupy more virtual memory than physical.
    359 	 */
    360 	size = MAXBSIZE * nbuf;
    361 	if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(size),
    362 		    NULL, UVM_UNKNOWN_OFFSET, 0,
    363 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    364 				UVM_ADV_NORMAL, 0)) != 0)
    365 		panic("cpu_startup: cannot allocate VM for buffers");
    366 
    367 	minaddr = (vaddr_t)buffers;
    368 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
    369 		bufpages = btoc(MAXBSIZE) * nbuf; /* do not overallocate RAM */
    370 	}
    371 	base = bufpages / nbuf;
    372 	residual = bufpages % nbuf;
    373 
    374 	/* now allocate RAM for buffers */
    375 	for (i = 0; i < nbuf; i++) {
    376 		vsize_t curbufsize;
    377 		vaddr_t curbuf;
    378 		struct vm_page *pg;
    379 
    380 		/*
    381 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    382 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    383 		 * for the first "residual" buffers, and then we allocate
    384 		 * "base" pages for the rest.
    385 		 */
    386 		curbuf = (vaddr_t)buffers + (i * MAXBSIZE);
    387 		curbufsize = PAGE_SIZE * ((i < residual) ? (base+1) : base);
    388 
    389 		while (curbufsize) {
    390 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    391 			if (pg == NULL)
    392 				panic("cpu_startup: not enough memory for "
    393 				    "buffer cache");
    394 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    395 			    VM_PROT_READ|VM_PROT_WRITE);
    396 			curbuf += PAGE_SIZE;
    397 			curbufsize -= PAGE_SIZE;
    398 		}
    399 	}
    400 	pmap_update(pmap_kernel());
    401 
    402 	/*
    403 	 * Allocate a submap for exec arguments.  This map effectively
    404 	 * limits the number of processes exec'ing at any time.
    405 	 */
    406 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    407 	    16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    408 
    409 	/*
    410 	 * Allocate a submap for physio
    411 	 */
    412 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    413 	    VM_PHYS_SIZE, 0, FALSE, NULL);
    414 
    415 	/*
    416 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    417 	 * are allocated via the pool allocator, and we use KSEG to
    418 	 * map those pages.
    419 	 */
    420 
    421 #ifdef DEBUG
    422 	pmapdebug = opmapdebug;
    423 #endif
    424 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    425 	printf("avail memory = %s\n", pbuf);
    426 	format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
    427 	printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
    428 
    429 	/*
    430 	 * Set up buffers, so they can be used to read disklabels.
    431 	 */
    432 	bufinit();
    433 
    434 	/*
    435 	 * Set up the chip/board properties database.
    436 	 */
    437 	if (!(alchemy_prop_info = propdb_create("board info")))
    438 		panic("Cannot create board info database");
    439 }
    440 
    441 void
    442 cpu_reboot(int howto, char *bootstr)
    443 {
    444 	static int waittime = -1;
    445 
    446 	/* Take a snapshot before clobbering any registers. */
    447 	if (curproc)
    448 		savectx((struct user *)curpcb);
    449 
    450 	/* If "always halt" was specified as a boot flag, obey. */
    451 	if (boothowto & RB_HALT)
    452 		howto |= RB_HALT;
    453 
    454 	boothowto = howto;
    455 
    456 	/* If system is cold, just halt. */
    457 	if (cold) {
    458 		boothowto |= RB_HALT;
    459 		goto haltsys;
    460 	}
    461 
    462 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    463 		waittime = 0;
    464 
    465 		/*
    466 		 * Synchronize the disks....
    467 		 */
    468 		vfs_shutdown();
    469 
    470 		/*
    471 		 * If we've been adjusting the clock, the todr
    472 		 * will be out of synch; adjust it now.
    473 		 */
    474 		resettodr();
    475 	}
    476 
    477 	/* Disable interrupts. */
    478 	splhigh();
    479 
    480 	if (boothowto & RB_DUMP)
    481 		dumpsys();
    482 
    483  haltsys:
    484 	/* Run any shutdown hooks. */
    485 	doshutdownhooks();
    486 
    487 #if 1
    488 	/* XXX
    489 	 * For some reason we are leaving the ethernet MAC in a state where
    490 	 * YAMON isn't happy with it.  So just call the reset vector (grr,
    491 	 * Alchemy YAMON doesn't have a "reset" command).
    492 	 */
    493 	printf("reseting board...\n\n");
    494 	mips_icache_sync_all();
    495 	mips_dcache_wbinv_all();
    496 	asm volatile("jr	%0" :: "r"(MIPS_RESET_EXC_VEC));
    497 #else
    498 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    499 	yamon_exit(boothowto);
    500 	printf("Oops, back from yamon_exit()\n\nSpinning...");
    501 #endif
    502 	for (;;)
    503 		/* spin forever */ ;	/* XXX */
    504 	/*NOTREACHED*/
    505 }
    506