Home | History | Annotate | Line # | Download | only in adm5120
machdep.c revision 1.13
      1 /* $NetBSD: machdep.c,v 1.13 2009/11/27 03:23:08 rmind Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or
      8  * without modification, are permitted provided that the following
      9  * conditions are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above
     13  *    copyright notice, this list of conditions and the following
     14  *    disclaimer in the documentation and/or other materials provided
     15  *    with the distribution.
     16  * 3. The names of the authors may not be used to endorse or promote
     17  *    products derived from this software without specific prior
     18  *    written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
     21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     25  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
     27  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     29  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     31  * OF SUCH DAMAGE.
     32  */
     33 /*
     34  * Copyright (c) 1992, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * This code is derived from software contributed to Berkeley by
     38  * the Systems Programming Group of the University of Utah Computer
     39  * Science Department, The Mach Operating System project at
     40  * Carnegie-Mellon University and Ralph Campbell.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
     67  * 	from: Utah Hdr: machdep.c 1.63 91/04/24
     68  */
     69 /*
     70  * Copyright (c) 1988 University of Utah.
     71  *
     72  * This code is derived from software contributed to Berkeley by
     73  * the Systems Programming Group of the University of Utah Computer
     74  * Science Department, The Mach Operating System project at
     75  * Carnegie-Mellon University and Ralph Campbell.
     76  *
     77  * Redistribution and use in source and binary forms, with or without
     78  * modification, are permitted provided that the following conditions
     79  * are met:
     80  * 1. Redistributions of source code must retain the above copyright
     81  *    notice, this list of conditions and the following disclaimer.
     82  * 2. Redistributions in binary form must reproduce the above copyright
     83  *    notice, this list of conditions and the following disclaimer in the
     84  *    documentation and/or other materials provided with the distribution.
     85  * 3. All advertising materials mentioning features or use of this software
     86  *    must display the following acknowledgement:
     87  *	This product includes software developed by the University of
     88  *	California, Berkeley and its contributors.
     89  * 4. Neither the name of the University nor the names of its contributors
     90  *    may be used to endorse or promote products derived from this software
     91  *    without specific prior written permission.
     92  *
     93  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     94  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     95  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     96  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     97  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     98  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     99  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    101  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    102  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    103  * SUCH DAMAGE.
    104  *
    105  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
    106  * 	from: Utah Hdr: machdep.c 1.63 91/04/24
    107  */
    108 
    109 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
    110 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.13 2009/11/27 03:23:08 rmind Exp $");
    111 
    112 #include "opt_ddb.h"
    113 #include "opt_kgdb.h"
    114 
    115 #include "opt_memsize.h"
    116 #include "opt_modular.h"
    117 #include "opt_ethaddr.h"
    118 
    119 #include "opt_pci.h"
    120 #include "pci.h"
    121 
    122 #include <sys/param.h>
    123 #include <sys/systm.h>
    124 #include <sys/kernel.h>
    125 #include <sys/buf.h>
    126 #include <sys/reboot.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 <net/if.h>
    135 #include <net/if_ether.h>
    136 
    137 #include <uvm/uvm_extern.h>
    138 
    139 #include <dev/cons.h>
    140 
    141 #include "ksyms.h"
    142 
    143 #if NKSYMS || defined(DDB) || defined(MODULAR)
    144 #include <machine/db_machdep.h>
    145 #include <ddb/db_extern.h>
    146 #endif
    147 
    148 #include <mips/cache.h>
    149 #include <mips/locore.h>
    150 
    151 #include <mips/adm5120/include/adm5120reg.h>
    152 #include <mips/adm5120/include/adm5120var.h>
    153 #include <mips/adm5120/include/adm5120_extiovar.h>
    154 #include <mips/adm5120/include/adm5120_obiovar.h>
    155 #include <mips/adm5120/include/adm5120_mainbusvar.h>
    156 #include <mips/adm5120/include/adm5120_pcivar.h>
    157 #include <mips/adm5120/dev/uart.h>
    158 
    159 #ifndef	MEMSIZE
    160 #define	MEMSIZE 4 * 1024 * 1024
    161 #endif /* !MEMSIZE */
    162 
    163 /* Our exported CPU info; we can have only one. */
    164 struct cpu_info cpu_info_store;
    165 
    166 /* Maps for VM objects. */
    167 struct vm_map *mb_map = NULL;
    168 struct vm_map *phys_map = NULL;
    169 
    170 int maxmem;			/* max memory per process */
    171 
    172 int mem_cluster_cnt;
    173 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    174 
    175 struct adm5120_config adm5120_configuration;
    176 
    177 void adm5120_setcpufreq(void);
    178 
    179 void
    180 adm5120_setcpufreq(void)
    181 {
    182 	uint32_t v, freq;
    183 
    184 	v = SW_READ(SW_CODE_REG);
    185 	switch (v & CLKS_MASK) {
    186 	case CLKS_175MHZ:
    187 		freq = 175 * 1000 * 1000;
    188 		break;
    189 	case CLKS_200MHZ:
    190 		freq = 200 * 1000 * 1000;
    191 		break;
    192 	default:
    193 		panic("adm5120: cannot determine CPU clock speed");
    194 	}
    195 
    196 	curcpu()->ci_cpu_freq = freq;
    197 	curcpu()->ci_cycles_per_hz = (freq + hz / 2) / hz / 2;
    198 	curcpu()->ci_divisor_delay = ((freq + 500000) / 1000000) / 2;
    199 }
    200 
    201 void	mach_init(int, char **, void *, void *); /* XXX */
    202 
    203 static void
    204 copy_args(int argc, char **argv)
    205 {
    206 	struct adm5120_config *admc = &adm5120_configuration;
    207 	int i;
    208 	char *buf;
    209 	size_t buflen, rc;
    210 
    211 	buf = admc->args;
    212 	buflen = sizeof(admc->args);
    213 
    214 	if (argc >= __arraycount(admc->argv))
    215 		panic("%s: too many boot args\n", __func__);
    216 
    217 	for (i = 0; buflen > 0 && i < argc && argv[i] != NULL; i++) {
    218 		admc->argv[i] = buf;
    219 		if ((rc = strlcpy(buf, argv[i], buflen)) >= buflen)
    220 			panic("%s: boot args too long\n", __func__);
    221 
    222 		buf += rc;
    223 		buflen -= rc;
    224 		*buf++ = '\0';
    225 		buflen--;
    226 	}
    227 	if (i < argc)
    228 		panic("%s: boot args too long\n", __func__);
    229 
    230 	admc->argc = argc;
    231 }
    232 
    233 static void
    234 parse_args(prop_dictionary_t properties, int argc, char **argv,
    235     uint32_t *memsizep)
    236 {
    237 	char buf[32];
    238 	char *key, *val, *valend;
    239 	unsigned long tmp;
    240 	int i;
    241 	uint8_t enaddr[ETHER_ADDR_LEN];
    242 
    243 	if (memsizep != NULL)
    244 		*memsizep = MEMSIZE;
    245 
    246 	for (i = 0; i < argc && argv[i] != NULL; i++) {
    247 		if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf))
    248 			goto err;
    249 		val = buf;
    250 		key = strsep(&val, "=");
    251 		if (val == NULL)
    252 			goto err;
    253 		if (strcmp(key, "mem") == 0) {
    254 			tmp = strtoul(val, &valend, 10);
    255 			if (val == valend || *valend != 'M')
    256 				goto err;
    257 			if (memsizep != NULL)
    258 				*memsizep = tmp * 1024 * 1024;
    259 		} else if (strcmp(key, "HZ") == 0)
    260 			;
    261 		else if (strcmp(key, "gpio") == 0) {
    262 			prop_number_t pn;
    263 
    264 			tmp = strtoul(val, &valend, 10);
    265 			if (val == valend || *valend != '\0')
    266 				goto err;
    267 			if (properties == NULL)
    268 				continue;
    269 			pn = prop_number_create_unsigned_integer(tmp);
    270 			if (pn == NULL) {
    271 				printf(
    272 				   "%s: prop_number_create_unsigned_integer\n",
    273 				   __func__);
    274 				continue;
    275 			}
    276 			if (!prop_dictionary_set(properties, "initial-gpio",
    277 			    pn)) {
    278 				printf("%s: prop_dictionary_set(gpio)\n",
    279 				    __func__);
    280 			}
    281 			prop_object_release(pn);
    282 		} else if (strcmp(key, "kmac") == 0) {
    283 			prop_data_t pd;
    284 
    285 			ether_nonstatic_aton(enaddr, val);
    286 			if (properties == NULL)
    287 				continue;
    288 			pd = prop_data_create_data(enaddr, sizeof(enaddr));
    289 			if (pd == NULL) {
    290 				printf("%s: prop_data_create_data\n", __func__);
    291 				continue;
    292 			}
    293 			if (!prop_dictionary_set(properties, "mac-addr", pd)) {
    294 				printf("%s: prop_dictionary_set(mac)\n",
    295 				    __func__);
    296 			}
    297 			prop_object_release(pd);
    298 		} else if (strcmp(key, "board") == 0)
    299 			printf("Routerboard %s\n", val);
    300 		else if (strcmp(key, "boot") == 0)
    301 			;
    302 		continue;
    303 	err:
    304 		printf("bad argv[%d] (%s)\n", i, argv[i]);
    305 	}
    306 }
    307 
    308 void
    309 mach_init(int argc, char **argv, void *a2, void *a3)
    310 {
    311 	struct adm5120_config *admc = &adm5120_configuration;
    312 	struct pcb *pcb0;
    313 	uint32_t memsize;
    314 	vaddr_t kernend;
    315 	u_long first, last;
    316 	vaddr_t v;
    317 
    318 	extern char edata[], end[];	/* XXX */
    319 
    320 	/* clear the BSS segment */
    321 	kernend = mips_round_page(end);
    322 	memset(edata, 0, kernend - (vaddr_t)edata);
    323 
    324 	/* set CPU model info for sysctl_hw */
    325 	strcpy(cpu_model, "Infineon ADM5120");
    326 
    327 	/*
    328 	 * Set up the exception vectors and CPU-specific function
    329 	 * vectors early on.  We need the wbflush() vector set up
    330 	 * before comcnattach() is called (or at least before the
    331 	 * first printf() after that is called).
    332 	 * Sets up mips_cpu_flags that may be queried by other
    333 	 * functions called during startup.
    334 	 * Also clears the I+D caches.
    335 	 */
    336 	mips_vector_init();
    337 
    338 	/*
    339 	 * Set the VM page size.
    340 	 */
    341 	uvm_setpagesize();
    342 
    343 	adm5120_setcpufreq();
    344 
    345 	/*
    346 	 * Initialize bus space tags.
    347 	 */
    348 	obio_bus_mem_init(&admc->obio_space, admc);
    349 	extio_bus_mem_init(&admc->extio_space, admc);
    350 #if NPCI > 0
    351 	pciio_bus_mem_init(&admc->pciio_space, admc);
    352 	pcimem_bus_mem_init(&admc->pcimem_space, admc);
    353 #endif
    354 
    355 	/*
    356 	 * Initialize bus DMA tag.
    357 	 */
    358 	obio_dma_init(&admc->obio_dmat);
    359 
    360 	/*
    361 	 * Attach serial console.
    362 	 */
    363 	uart_cnattach();
    364 
    365 	/*
    366 	 * Look at arguments passed to us and compute boothowto.
    367 	 */
    368 	boothowto = RB_AUTOBOOT;
    369 #ifdef KADB
    370 	boothowto |= RB_KDB;
    371 #endif
    372 
    373 	parse_args(NULL, argc, argv, &memsize);
    374 
    375 	/*
    376 	 * Determine the memory size.
    377 	 *
    378 	 * Note: Reserve the first page!  That's where the trap
    379 	 * vectors are located.
    380 	 */
    381 
    382 #if 0
    383 	if (GET_MEMSIZE(memsize) == 0) {
    384 		uint32_t val;
    385 
    386 		/* This does not seem to work... --dyoung */
    387 		val = SW_READ(SW_MEMCONT_REG);
    388 		printf("SW_MEMCONT_REG: 0x%08" PRIx32 "\n", val);
    389 		switch (val & SDRAM_SIZE_MASK) {
    390 		case SDRAM_SIZE_4MBYTES:
    391 			memsize = 4 * 1024 * 1024;
    392 			break;
    393 		case SDRAM_SIZE_8MBYTES:
    394 			memsize = 8 * 1024 * 1024;
    395 			break;
    396 		case SDRAM_SIZE_16MBYTES:
    397 			memsize = 16 * 1024 * 1024;
    398 			break;
    399 		case SDRAM_SIZE_64MBYTES:
    400 			memsize = 64 * 1024 * 1024;
    401 			break;
    402 		case SDRAM_SIZE_128MBYTES:
    403 			memsize = 128 * 1024 * 1024;
    404 			break;
    405 		default:
    406 			panic("adm5120: cannot determine memory size");
    407 		}
    408 	}
    409 #endif
    410 
    411 	physmem = btoc(memsize);
    412 
    413 	mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
    414 	mem_clusters[mem_cluster_cnt].size =
    415 	    memsize - mem_clusters[mem_cluster_cnt].start;
    416 	mem_cluster_cnt++;
    417 
    418 	/*
    419 	 * Load the rest of the available pages into the VM system.
    420 	 */
    421 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    422 	last = mem_clusters[0].start + mem_clusters[0].size;
    423 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    424 	    VM_FREELIST_DEFAULT);
    425 
    426 	/*
    427 	 * Initialize message buffer (at end of core).
    428 	 */
    429 	mips_init_msgbuf();
    430 
    431 	/*
    432 	 * Initialize the virtual memory system.
    433 	 */
    434 	pmap_bootstrap();
    435 
    436 	/*
    437 	 * Allocate uarea page for lwp0 and set it.
    438 	 */
    439 	v = uvm_pageboot_alloc(USPACE);
    440 	uvm_lwp_setuarea(&lwp0, v);
    441 
    442 	pcb0 = lwp_getpcb(&lwp0);
    443 	pcb0->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    444 
    445 	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
    446 
    447 	/*
    448 	 * Initialize debuggers, and break into them, if appropriate.
    449 	 */
    450 #ifdef DDB
    451 	if (boothowto & RB_KDB)
    452 		Debugger();
    453 #endif
    454 
    455 	copy_args(argc, argv);
    456 }
    457 
    458 void
    459 consinit(void)
    460 {
    461 
    462 	/*
    463 	 * Everything related to console initialization is done
    464 	 * in mach_init().
    465 	 */
    466 }
    467 
    468 void
    469 cpu_startup(void)
    470 {
    471 	struct adm5120_config *admc = &adm5120_configuration;
    472 	char pbuf[9];
    473 	vaddr_t minaddr, maxaddr;
    474 #ifdef DEBUG
    475 	extern int pmapdebug;		/* XXX */
    476 	int opmapdebug = pmapdebug;
    477 
    478 	pmapdebug = 0;		/* Shut up pmap debug during bootstrap */
    479 #endif
    480 
    481 	if ((admc->properties = prop_dictionary_create()) == NULL)
    482 		printf("%s: prop_dictionary_create\n", __func__);
    483 	parse_args(admc->properties, admc->argc, admc->argv, NULL);
    484 
    485 	/*
    486 	 * Good {morning,afternoon,evening,night}.
    487 	 */
    488 	printf("%s%s", copyright, version);
    489 	printf("%s\n", cpu_model);
    490 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    491 	printf("total memory = %s\n", pbuf);
    492 
    493 	minaddr = 0;
    494 
    495 	/*
    496 	 * Allocate a submap for physio
    497 	 */
    498 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    499 	    VM_PHYS_SIZE, 0, FALSE, NULL);
    500 
    501 	/*
    502 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    503 	 * are allocated via the pool allocator, and we use KSEG to
    504 	 * map those pages.
    505 	 */
    506 
    507 #ifdef DEBUG
    508 	pmapdebug = opmapdebug;
    509 #endif
    510 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    511 	printf("avail memory = %s\n", pbuf);
    512 }
    513 
    514 void
    515 cpu_reboot(int howto, char *bootstr)
    516 {
    517 	static int waittime = -1;
    518 
    519 	/* Take a snapshot before clobbering any registers. */
    520 	if (curproc)
    521 		savectx(curpcb);
    522 
    523 	/* If "always halt" was specified as a boot flag, obey. */
    524 	if (boothowto & RB_HALT)
    525 		howto |= RB_HALT;
    526 
    527 	boothowto = howto;
    528 
    529 	/* If system is cold, just halt. */
    530 	if (cold) {
    531 		boothowto |= RB_HALT;
    532 		goto haltsys;
    533 	}
    534 
    535 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    536 		waittime = 0;
    537 
    538 		/*
    539 		 * Synchronize the disks....
    540 		 */
    541 		vfs_shutdown();
    542 
    543 		/*
    544 		 * If we've been adjusting the clock, the todr
    545 		 * will be out of synch; adjust it now.
    546 		 */
    547 		resettodr();
    548 	}
    549 
    550 	/* Disable interrupts. */
    551 	splhigh();
    552 
    553 	if (boothowto & RB_DUMP)
    554 		dumpsys();
    555 
    556  haltsys:
    557 	/* Run any shutdown hooks. */
    558 	doshutdownhooks();
    559 
    560 	pmf_system_shutdown(boothowto);
    561 
    562 	/*
    563 	 * Routerboard BIOS may autoboot, so "pseudo-halt".
    564 	 */
    565 	if (boothowto & RB_HALT) {
    566 		printf("\n");
    567 		printf("The operating system has halted.\n");
    568 		printf("Please press any key to reboot.\n\n");
    569 		cnpollc(1);	/* For proper keyboard command handling */
    570 		cngetc();
    571 		cnpollc(0);
    572 	}
    573 
    574 	printf("reseting board...\n\n");
    575 	mips_icache_sync_all();
    576 	mips_dcache_wbinv_all();
    577 	SW_WRITE(SW_SFTRES_REG, 0);			/* reset */
    578 	for (;;)
    579 		/* spin forever */ ;	/* XXX */
    580 	/*NOTREACHED*/
    581 }
    582