Home | History | Annotate | Line # | Download | only in walnut
machdep.c revision 1.53
      1 /*	$NetBSD: machdep.c,v 1.53 2011/06/20 17:44:33 matt Exp $	*/
      2 
      3 /*
      4  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Eduardo Horvath 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) 1995, 1996 Wolfgang Solfrank.
     40  * Copyright (C) 1995, 1996 TooLs GmbH.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by TooLs GmbH.
     54  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     61  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     62  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     63  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     64  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     65  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     66  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.53 2011/06/20 17:44:33 matt Exp $");
     71 
     72 #include "opt_compat_netbsd.h"
     73 #include "opt_ddb.h"
     74 #include "opt_ipkdb.h"
     75 
     76 #include <sys/param.h>
     77 #include <sys/boot_flag.h>
     78 #include <sys/buf.h>
     79 #include <sys/bus.h>
     80 #include <sys/cpu.h>
     81 #include <sys/device.h>
     82 #include <sys/exec.h>
     83 #include <sys/kernel.h>
     84 #include <sys/ksyms.h>
     85 #include <sys/malloc.h>
     86 #include <sys/mbuf.h>
     87 #include <sys/module.h>
     88 #include <sys/mount.h>
     89 #include <sys/msgbuf.h>
     90 #include <sys/proc.h>
     91 #include <sys/reboot.h>
     92 #include <sys/syscallargs.h>
     93 #include <sys/syslog.h>
     94 #include <sys/systm.h>
     95 
     96 #include <uvm/uvm_extern.h>
     97 
     98 #include <prop/proplib.h>
     99 
    100 #include <machine/powerpc.h>
    101 #include <machine/walnut.h>
    102 
    103 #include <powerpc/trap.h>
    104 #include <powerpc/pcb.h>
    105 
    106 #include <powerpc/spr.h>
    107 #include <powerpc/ibm4xx/spr.h>
    108 #include <powerpc/ibm4xx/cpu.h>
    109 #include <powerpc/ibm4xx/dcr4xx.h>
    110 
    111 #include <powerpc/pic/picvar.h>
    112 
    113 #include <dev/cons.h>
    114 #include <dev/pci/pcivar.h>
    115 #include <dev/pci/pciconf.h>
    116 
    117 #include "ksyms.h"
    118 
    119 #if defined(DDB)
    120 #include <powerpc/db_machdep.h>
    121 #include <ddb/db_extern.h>
    122 #endif
    123 
    124 
    125 #define TLB_PG_SIZE 	(16*1024*1024)
    126 
    127 /*
    128  * Global variables used here and there
    129  */
    130 struct vm_map *phys_map = NULL;
    131 
    132 /*
    133  * This should probably be in autoconf!				XXX
    134  */
    135 char cpu_model[80];
    136 char machine[] = MACHINE;		/* from <machine/param.h> */
    137 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    138 
    139 char bootpath[256];
    140 paddr_t msgbuf_paddr;
    141 vaddr_t msgbuf_vaddr;
    142 
    143 void initppc(vaddr_t, vaddr_t, char *, void *);
    144 
    145 static void dumpsys(void);
    146 
    147 #define MEMREGIONS	8
    148 struct mem_region physmemr[MEMREGIONS];		/* Hard code memory */
    149 struct mem_region availmemr[MEMREGIONS];	/* Who's supposed to set these up? */
    150 
    151 struct board_cfg_data board_data;
    152 
    153 void
    154 initppc(vaddr_t startkernel, vaddr_t endkernel, char *args, void *info_block)
    155 {
    156 	/* Disable all external interrupts */
    157 	mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, 0);
    158 
    159         /* Initialize cache info for memcpy, etc. */
    160         cpu_probe_cache();
    161 
    162 	/* Save info block */
    163 	memcpy(&board_data, info_block, sizeof(board_data));
    164 
    165 	memset(physmemr, 0, sizeof physmemr);
    166 	memset(availmemr, 0, sizeof availmemr);
    167 	physmemr[0].start = 0;
    168 	physmemr[0].size = board_data.mem_size & ~PGOFSET;
    169 	/* Lower memory reserved by eval board BIOS */
    170 	availmemr[0].start = startkernel;
    171 	availmemr[0].size = board_data.mem_size - availmemr[0].start;
    172 
    173 	/* Linear map kernel memory */
    174 	for (vaddr_t va = 0; va < endkernel; va += TLB_PG_SIZE) {
    175 		ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
    176 	}
    177 
    178 	/* Map console after physmem (see pmap_tlbmiss()) */
    179 	ppc4xx_tlb_reserve(0xef000000, roundup(physmemr[0].size, TLB_PG_SIZE),
    180 	    TLB_PG_SIZE, TLB_I | TLB_G);
    181 
    182 	mtspr(SPR_TCR, 0);	/* disable all timers */
    183 
    184 	ibm4xx_init(startkernel, endkernel, pic_ext_intr);
    185 
    186 #ifdef DEBUG
    187 	printf("Board config data:\n");
    188 	printf("  usr_config_ver = %s\n", board_data.usr_config_ver);
    189 	printf("  rom_sw_ver = %s\n", board_data.rom_sw_ver);
    190 	printf("  mem_size = %u\n", board_data.mem_size);
    191 	printf("  mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
    192 	    board_data.mac_address_local[0], board_data.mac_address_local[1],
    193 	    board_data.mac_address_local[2], board_data.mac_address_local[3],
    194 	    board_data.mac_address_local[4], board_data.mac_address_local[5]);
    195 	printf("  mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
    196 	    board_data.mac_address_pci[0], board_data.mac_address_pci[1],
    197 	    board_data.mac_address_pci[2], board_data.mac_address_pci[3],
    198 	    board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
    199 	printf("  processor_speed = %u\n", board_data.processor_speed);
    200 	printf("  plb_speed = %u\n", board_data.plb_speed);
    201 	printf("  pci_speed = %u\n", board_data.pci_speed);
    202 #endif
    203 
    204 #ifdef DDB
    205 	if (boothowto & RB_KDB)
    206 		Debugger();
    207 #endif
    208 #ifdef IPKDB
    209 	/*
    210 	 * Now trap to IPKDB
    211 	 */
    212 	ipkdb_init();
    213 	if (boothowto & RB_KDB)
    214 		ipkdb_connect(0);
    215 #endif
    216 
    217 	/*
    218 	 * Look for the ibm4xx modules in the right place.
    219 	 */
    220 	module_machine = module_machine_ibm4xx;
    221 }
    222 
    223 /*
    224  * Machine dependent startup code.
    225  */
    226 
    227 char msgbuf[MSGBUFSIZE];
    228 
    229 void
    230 cpu_startup(void)
    231 {
    232 	vaddr_t minaddr, maxaddr;
    233 	prop_number_t pn;
    234 	prop_data_t pd;
    235 	char pbuf[9];
    236 
    237 	/*
    238 	 * Initialize error message buffer (at end of core).
    239 	 */
    240 #if 0	/* For some reason this fails... --Artem
    241 	 * Besides, do we really have to put it at the end of core?
    242 	 * Let's use static buffer for now
    243 	 */
    244 	if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0,
    245 	    UVM_KMF_VAONLY)))
    246 		panic("startup: no room for message buffer");
    247 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
    248 		pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
    249 		    msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0);
    250 	initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
    251 #else
    252 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
    253 #endif
    254 
    255 	printf("%s%s", copyright, version);
    256 	printf("Walnut PowerPC 405GP Evaluation Board\n");
    257 
    258 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    259 	printf("total memory = %s\n", pbuf);
    260 
    261 	minaddr = 0;
    262 	/*
    263 	 * Allocate a submap for physio
    264 	 */
    265 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    266 				 VM_PHYS_SIZE, 0, false, NULL);
    267 
    268 	/*
    269 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    270 	 * are allocated via the pool allocator, and we use direct-mapped
    271 	 * pool pages.
    272 	 */
    273 
    274 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    275 	printf("avail memory = %s\n", pbuf);
    276 
    277 	/*
    278 	 * Set up the board properties dictionary.
    279 	 */
    280 	board_properties = prop_dictionary_create();
    281 	KASSERT(board_properties != NULL);
    282 
    283 	pn = prop_number_create_integer(board_data.mem_size);
    284 	KASSERT(pn != NULL);
    285 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
    286 		panic("setting mem-size");
    287 	prop_object_release(pn);
    288 
    289 	pd = prop_data_create_data_nocopy(board_data.mac_address_local,
    290 					  sizeof(board_data.mac_address_local));
    291 	KASSERT(pd != NULL);
    292 	if (prop_dictionary_set(board_properties, "emac0-mac-addr",
    293 				pd) == false)
    294 		panic("setting emac0-mac-addr");
    295 	prop_object_release(pd);
    296 
    297 	pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
    298 					  sizeof(board_data.mac_address_pci));
    299 	KASSERT(pd != NULL);
    300 	if (prop_dictionary_set(board_properties, "sip0-mac-addr",
    301 				pd) == false)
    302 		panic("setting sip0-mac-addr");
    303 	prop_object_release(pd);
    304 
    305 	pn = prop_number_create_integer(board_data.processor_speed);
    306 	KASSERT(pn != NULL);
    307 	if (prop_dictionary_set(board_properties, "processor-frequency",
    308 				pn) == false)
    309 		panic("setting processor-frequency");
    310 	prop_object_release(pn);
    311 
    312 	/*
    313 	 * Now that we have VM, malloc()s are OK in bus_space.
    314 	 */
    315 	bus_space_mallocok();
    316 	fake_mapiodev = 0;
    317 }
    318 
    319 
    320 static void
    321 dumpsys(void)
    322 {
    323 
    324 	printf("dumpsys: TBD\n");
    325 }
    326 
    327 /*
    328  * Halt or reboot the machine after syncing/dumping according to howto.
    329  */
    330 void
    331 cpu_reboot(int howto, char *what)
    332 {
    333 	static int syncing;
    334 	static char str[256];
    335 	char *ap = str, *ap1 = ap;
    336 
    337 	boothowto = howto;
    338 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    339 		syncing = 1;
    340 		vfs_shutdown();		/* sync */
    341 		resettodr();		/* set wall clock */
    342 	}
    343 
    344 	splhigh();
    345 
    346 	if (!cold && (howto & RB_DUMP))
    347 		dumpsys();
    348 
    349 	doshutdownhooks();
    350 
    351 	pmf_system_shutdown(boothowto);
    352 
    353 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    354 	  /* Power off here if we know how...*/
    355 	}
    356 
    357 	if (howto & RB_HALT) {
    358 		printf("halted\n\n");
    359 
    360 		goto reboot;	/* XXX for now... */
    361 
    362 #ifdef DDB
    363 		printf("dropping to debugger\n");
    364 		while(1)
    365 			Debugger();
    366 #endif
    367 	}
    368 
    369 	printf("rebooting\n\n");
    370 	if (what && *what) {
    371 		if (strlen(what) > sizeof str - 5)
    372 			printf("boot string too large, ignored\n");
    373 		else {
    374 			strcpy(str, what);
    375 			ap1 = ap = str + strlen(str);
    376 			*ap++ = ' ';
    377 		}
    378 	}
    379 	*ap++ = '-';
    380 	if (howto & RB_SINGLE)
    381 		*ap++ = 's';
    382 	if (howto & RB_KDB)
    383 		*ap++ = 'd';
    384 	*ap++ = 0;
    385 	if (ap[-2] == '-')
    386 		*ap1 = 0;
    387 
    388 	/* flush cache for msgbuf */
    389 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
    390 
    391  reboot:
    392 	ppc4xx_reset();
    393 
    394 	printf("ppc4xx_reset() failed!\n");
    395 #ifdef DDB
    396 	while(1)
    397 		Debugger();
    398 #else
    399 	while (1)
    400 		/* nothing */;
    401 #endif
    402 }
    403 
    404 void
    405 mem_regions(struct mem_region **mem, struct mem_region **avail)
    406 {
    407 
    408 	*mem = physmemr;
    409 	*avail = availmemr;
    410 }
    411 
    412 
    413 int
    414 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    415 {
    416 
    417 	/*
    418 	 * Bus number is irrelevant.  Configuration Mechanism 1 is in
    419 	 * use, can have devices 0-32 (i.e. the `normal' range).
    420 	 */
    421 	return 5;
    422 }
    423 
    424 int
    425 pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    426 {
    427 	int pin = pa->pa_intrpin;
    428 	int dev = pa->pa_device;
    429 
    430 	if (pin == 0)
    431 		/* No IRQ used. */
    432 		goto bad;
    433 
    434 	if (pin > 4) {
    435 		printf("pci_intr_map: bad interrupt pin %d\n", pin);
    436 		goto bad;
    437 	}
    438 
    439 	/*
    440 	 * We need to map the interrupt pin to the interrupt bit in the UIC
    441 	 * associated with it.  This is highly machine-dependent.
    442 	 */
    443 	switch(dev) {
    444 	case 1:
    445 	case 2:
    446 	case 3:
    447 	case 4:
    448 		*ihp = 27 + dev;
    449 		break;
    450 	default:
    451 		printf("Hmm.. PCI device %d should not exist on this board\n",
    452 			dev);
    453 		goto bad;
    454 	}
    455 	return 0;
    456 
    457 bad:
    458 	*ihp = -1;
    459 	return 1;
    460 }
    461 
    462 void
    463 pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
    464 		   int swiz, int *iline)
    465 {
    466 
    467 	if (bus == 0)
    468 		switch(dev) {
    469 		case 1:
    470 		case 2:
    471 		case 3:
    472 		case 4:
    473 			*iline = 31 - dev;
    474 		}
    475 	else
    476 		*iline = 20 + ((swiz + dev + 1) & 3);
    477 }
    478