Home | History | Annotate | Line # | Download | only in adm5120
machdep.c revision 1.17
      1 /* $NetBSD: machdep.c,v 1.17 2010/05/19 20:41:59 christos 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.17 2010/05/19 20:41:59 christos 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 *phys_map = NULL;
    168 
    169 int maxmem;			/* max memory per process */
    170 
    171 int mem_cluster_cnt;
    172 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    173 
    174 struct adm5120_config adm5120_configuration;
    175 
    176 void adm5120_setcpufreq(void);
    177 
    178 void
    179 adm5120_setcpufreq(void)
    180 {
    181 	uint32_t v, freq;
    182 
    183 	v = SW_READ(SW_CODE_REG);
    184 	switch (v & CLKS_MASK) {
    185 	case CLKS_175MHZ:
    186 		freq = 175 * 1000 * 1000;
    187 		break;
    188 	case CLKS_200MHZ:
    189 		freq = 200 * 1000 * 1000;
    190 		break;
    191 	default:
    192 		panic("adm5120: cannot determine CPU clock speed");
    193 	}
    194 
    195 	curcpu()->ci_cpu_freq = freq;
    196 	curcpu()->ci_cycles_per_hz = (freq + hz / 2) / hz / 2;
    197 	curcpu()->ci_divisor_delay = ((freq + 500000) / 1000000) / 2;
    198 }
    199 
    200 void	mach_init(int, char **, void *, void *); /* XXX */
    201 
    202 static void
    203 copy_args(int argc, char **argv)
    204 {
    205 	struct adm5120_config *admc = &adm5120_configuration;
    206 	int i;
    207 	char *buf;
    208 	size_t buflen, rc;
    209 
    210 	buf = admc->args;
    211 	buflen = sizeof(admc->args);
    212 
    213 	if (argc >= __arraycount(admc->argv))
    214 		panic("%s: too many boot args\n", __func__);
    215 
    216 	for (i = 0; buflen > 0 && i < argc && argv[i] != NULL; i++) {
    217 		admc->argv[i] = buf;
    218 		if ((rc = strlcpy(buf, argv[i], buflen)) >= buflen)
    219 			panic("%s: boot args too long\n", __func__);
    220 
    221 		buf += rc;
    222 		buflen -= rc;
    223 		*buf++ = '\0';
    224 		buflen--;
    225 	}
    226 	if (i < argc)
    227 		panic("%s: boot args too long\n", __func__);
    228 
    229 	admc->argc = argc;
    230 }
    231 
    232 static void
    233 parse_args(prop_dictionary_t properties, int argc, char **argv,
    234     uint32_t *memsizep)
    235 {
    236 	char buf[32];
    237 	char *key, *val, *valend;
    238 	unsigned long tmp;
    239 	int i;
    240 	uint8_t enaddr[ETHER_ADDR_LEN];
    241 
    242 	if (memsizep != NULL)
    243 		*memsizep = MEMSIZE;
    244 
    245 	for (i = 0; i < argc && argv[i] != NULL; i++) {
    246 		if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf))
    247 			goto err;
    248 		val = buf;
    249 		key = strsep(&val, "=");
    250 		if (val == NULL)
    251 			goto err;
    252 		if (strcmp(key, "mem") == 0) {
    253 			tmp = strtoul(val, &valend, 10);
    254 			if (val == valend || *valend != 'M')
    255 				goto err;
    256 			if (memsizep != NULL)
    257 				*memsizep = tmp * 1024 * 1024;
    258 		} else if (strcmp(key, "HZ") == 0)
    259 			;
    260 		else if (strcmp(key, "gpio") == 0) {
    261 			prop_number_t pn;
    262 
    263 			tmp = strtoul(val, &valend, 10);
    264 			if (val == valend || *valend != '\0')
    265 				goto err;
    266 			if (properties == NULL)
    267 				continue;
    268 			pn = prop_number_create_unsigned_integer(tmp);
    269 			if (pn == NULL) {
    270 				printf(
    271 				   "%s: prop_number_create_unsigned_integer\n",
    272 				   __func__);
    273 				continue;
    274 			}
    275 			if (!prop_dictionary_set(properties, "initial-gpio",
    276 			    pn)) {
    277 				printf("%s: prop_dictionary_set(gpio)\n",
    278 				    __func__);
    279 			}
    280 			prop_object_release(pn);
    281 		} else if (strcmp(key, "kmac") == 0) {
    282 			prop_data_t pd;
    283 
    284 			(void)ether_aton_r(enaddr, sizeof(enaddr), val);
    285 			if (properties == NULL)
    286 				continue;
    287 			pd = prop_data_create_data(enaddr, sizeof(enaddr));
    288 			if (pd == NULL) {
    289 				printf("%s: prop_data_create_data\n", __func__);
    290 				continue;
    291 			}
    292 			if (!prop_dictionary_set(properties, "mac-address", pd)) {
    293 				printf("%s: prop_dictionary_set(mac)\n",
    294 				    __func__);
    295 			}
    296 			prop_object_release(pd);
    297 		} else if (strcmp(key, "board") == 0)
    298 			printf("Routerboard %s\n", val);
    299 		else if (strcmp(key, "boot") == 0)
    300 			;
    301 		continue;
    302 	err:
    303 		printf("bad argv[%d] (%s)\n", i, argv[i]);
    304 	}
    305 }
    306 
    307 void
    308 mach_init(int argc, char **argv, void *a2, void *a3)
    309 {
    310 	struct adm5120_config *admc = &adm5120_configuration;
    311 	struct pcb *pcb0;
    312 	uint32_t memsize;
    313 	vaddr_t kernend;
    314 	u_long first, last;
    315 	vaddr_t v;
    316 
    317 	extern char edata[], end[];	/* XXX */
    318 
    319 	/* clear the BSS segment */
    320 	kernend = mips_round_page(end);
    321 	memset(edata, 0, kernend - (vaddr_t)edata);
    322 
    323 	/* set CPU model info for sysctl_hw */
    324 	strcpy(cpu_model, "Infineon ADM5120");
    325 
    326 	/*
    327 	 * Set up the exception vectors and CPU-specific function
    328 	 * vectors early on.  We need the wbflush() vector set up
    329 	 * before comcnattach() is called (or at least before the
    330 	 * first printf() after that is called).
    331 	 * Sets up mips_cpu_flags that may be queried by other
    332 	 * functions called during startup.
    333 	 * Also clears the I+D caches.
    334 	 */
    335 	mips_vector_init();
    336 
    337 	/*
    338 	 * Set the VM page size.
    339 	 */
    340 	uvm_setpagesize();
    341 
    342 	adm5120_setcpufreq();
    343 
    344 	/*
    345 	 * Initialize bus space tags.
    346 	 */
    347 	obio_bus_mem_init(&admc->obio_space, admc);
    348 	extio_bus_mem_init(&admc->extio_space, admc);
    349 #if NPCI > 0
    350 	pciio_bus_mem_init(&admc->pciio_space, admc);
    351 	pcimem_bus_mem_init(&admc->pcimem_space, admc);
    352 #endif
    353 
    354 	/*
    355 	 * Initialize bus DMA tag.
    356 	 */
    357 	obio_dma_init(&admc->obio_dmat);
    358 
    359 	/*
    360 	 * Attach serial console.
    361 	 */
    362 	uart_cnattach();
    363 
    364 	/*
    365 	 * Look at arguments passed to us and compute boothowto.
    366 	 */
    367 	boothowto = RB_AUTOBOOT;
    368 #ifdef KADB
    369 	boothowto |= RB_KDB;
    370 #endif
    371 
    372 	parse_args(NULL, argc, argv, &memsize);
    373 
    374 	/*
    375 	 * Determine the memory size.
    376 	 *
    377 	 * Note: Reserve the first page!  That's where the trap
    378 	 * vectors are located.
    379 	 */
    380 
    381 #if 0
    382 	if (GET_MEMSIZE(memsize) == 0) {
    383 		uint32_t val;
    384 
    385 		/* This does not seem to work... --dyoung */
    386 		val = SW_READ(SW_MEMCONT_REG);
    387 		printf("SW_MEMCONT_REG: 0x%08" PRIx32 "\n", val);
    388 		switch (val & SDRAM_SIZE_MASK) {
    389 		case SDRAM_SIZE_4MBYTES:
    390 			memsize = 4 * 1024 * 1024;
    391 			break;
    392 		case SDRAM_SIZE_8MBYTES:
    393 			memsize = 8 * 1024 * 1024;
    394 			break;
    395 		case SDRAM_SIZE_16MBYTES:
    396 			memsize = 16 * 1024 * 1024;
    397 			break;
    398 		case SDRAM_SIZE_64MBYTES:
    399 			memsize = 64 * 1024 * 1024;
    400 			break;
    401 		case SDRAM_SIZE_128MBYTES:
    402 			memsize = 128 * 1024 * 1024;
    403 			break;
    404 		default:
    405 			panic("adm5120: cannot determine memory size");
    406 		}
    407 	}
    408 #endif
    409 
    410 	physmem = btoc(memsize);
    411 
    412 	mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
    413 	mem_clusters[mem_cluster_cnt].size =
    414 	    memsize - mem_clusters[mem_cluster_cnt].start;
    415 	mem_cluster_cnt++;
    416 
    417 	/*
    418 	 * Load the rest of the available pages into the VM system.
    419 	 */
    420 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    421 	last = mem_clusters[0].start + mem_clusters[0].size;
    422 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    423 	    VM_FREELIST_DEFAULT);
    424 
    425 	/*
    426 	 * Initialize message buffer (at end of core).
    427 	 */
    428 	mips_init_msgbuf();
    429 
    430 	/*
    431 	 * Initialize the virtual memory system.
    432 	 */
    433 	pmap_bootstrap();
    434 
    435 	/*
    436 	 * Allocate uarea page for lwp0 and set it.
    437 	 */
    438 	mips_init_lwp0_uarea();
    439 
    440 	/*
    441 	 * Initialize debuggers, and break into them, if appropriate.
    442 	 */
    443 #ifdef DDB
    444 	if (boothowto & RB_KDB)
    445 		Debugger();
    446 #endif
    447 
    448 	copy_args(argc, argv);
    449 }
    450 
    451 void
    452 consinit(void)
    453 {
    454 
    455 	/*
    456 	 * Everything related to console initialization is done
    457 	 * in mach_init().
    458 	 */
    459 }
    460 
    461 void
    462 cpu_startup(void)
    463 {
    464 	struct adm5120_config *admc = &adm5120_configuration;
    465 	char pbuf[9];
    466 	vaddr_t minaddr, maxaddr;
    467 #ifdef DEBUG
    468 	extern int pmapdebug;		/* XXX */
    469 	int opmapdebug = pmapdebug;
    470 
    471 	pmapdebug = 0;		/* Shut up pmap debug during bootstrap */
    472 #endif
    473 
    474 	if ((admc->properties = prop_dictionary_create()) == NULL)
    475 		printf("%s: prop_dictionary_create\n", __func__);
    476 	parse_args(admc->properties, admc->argc, admc->argv, NULL);
    477 
    478 	/*
    479 	 * Good {morning,afternoon,evening,night}.
    480 	 */
    481 	printf("%s%s", copyright, version);
    482 	printf("%s\n", cpu_model);
    483 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    484 	printf("total memory = %s\n", pbuf);
    485 
    486 	minaddr = 0;
    487 
    488 	/*
    489 	 * Allocate a submap for physio
    490 	 */
    491 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    492 	    VM_PHYS_SIZE, 0, FALSE, NULL);
    493 
    494 	/*
    495 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    496 	 * are allocated via the pool allocator, and we use KSEG to
    497 	 * map those pages.
    498 	 */
    499 
    500 #ifdef DEBUG
    501 	pmapdebug = opmapdebug;
    502 #endif
    503 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    504 	printf("avail memory = %s\n", pbuf);
    505 }
    506 
    507 void
    508 cpu_reboot(int howto, char *bootstr)
    509 {
    510 	static int waittime = -1;
    511 
    512 	/* Take a snapshot before clobbering any registers. */
    513 	if (curproc)
    514 		savectx(curpcb);
    515 
    516 	/* If "always halt" was specified as a boot flag, obey. */
    517 	if (boothowto & RB_HALT)
    518 		howto |= RB_HALT;
    519 
    520 	boothowto = howto;
    521 
    522 	/* If system is cold, just halt. */
    523 	if (cold) {
    524 		boothowto |= RB_HALT;
    525 		goto haltsys;
    526 	}
    527 
    528 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    529 		waittime = 0;
    530 
    531 		/*
    532 		 * Synchronize the disks....
    533 		 */
    534 		vfs_shutdown();
    535 
    536 		/*
    537 		 * If we've been adjusting the clock, the todr
    538 		 * will be out of synch; adjust it now.
    539 		 */
    540 		resettodr();
    541 	}
    542 
    543 	/* Disable interrupts. */
    544 	splhigh();
    545 
    546 	if (boothowto & RB_DUMP)
    547 		dumpsys();
    548 
    549  haltsys:
    550 	/* Run any shutdown hooks. */
    551 	doshutdownhooks();
    552 
    553 	pmf_system_shutdown(boothowto);
    554 
    555 	/*
    556 	 * Routerboard BIOS may autoboot, so "pseudo-halt".
    557 	 */
    558 	if (boothowto & RB_HALT) {
    559 		printf("\n");
    560 		printf("The operating system has halted.\n");
    561 		printf("Please press any key to reboot.\n\n");
    562 		cnpollc(1);	/* For proper keyboard command handling */
    563 		cngetc();
    564 		cnpollc(0);
    565 	}
    566 
    567 	printf("reseting board...\n\n");
    568 	mips_icache_sync_all();
    569 	mips_dcache_wbinv_all();
    570 	SW_WRITE(SW_SFTRES_REG, 0);			/* reset */
    571 	for (;;)
    572 		/* spin forever */ ;	/* XXX */
    573 	/*NOTREACHED*/
    574 }
    575