Home | History | Annotate | Line # | Download | only in walnut
machdep.c revision 1.63
      1 /*	$NetBSD: machdep.c,v 1.63 2021/03/30 01:16:45 rin 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.63 2021/03/30 01:16:45 rin Exp $");
     71 
     72 #include "opt_compat_netbsd.h"
     73 #include "opt_ddb.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/boot_flag.h>
     77 #include <sys/buf.h>
     78 #include <sys/bus.h>
     79 #include <sys/cpu.h>
     80 #include <sys/device.h>
     81 #include <sys/exec.h>
     82 #include <sys/kernel.h>
     83 #include <sys/ksyms.h>
     84 #include <sys/malloc.h>
     85 #include <sys/mbuf.h>
     86 #include <sys/module.h>
     87 #include <sys/mount.h>
     88 #include <sys/msgbuf.h>
     89 #include <sys/proc.h>
     90 #include <sys/reboot.h>
     91 #include <sys/syscallargs.h>
     92 #include <sys/syslog.h>
     93 #include <sys/systm.h>
     94 
     95 #include <uvm/uvm_extern.h>
     96 
     97 #include <prop/proplib.h>
     98 
     99 #include <machine/powerpc.h>
    100 #include <machine/walnut.h>
    101 
    102 #include <powerpc/trap.h>
    103 #include <powerpc/pcb.h>
    104 
    105 #include <powerpc/spr.h>
    106 #include <powerpc/ibm4xx/spr.h>
    107 #include <powerpc/ibm4xx/cpu.h>
    108 #include <powerpc/ibm4xx/dcr4xx.h>
    109 #include <powerpc/ibm4xx/ibm405gp.h>
    110 
    111 #include <powerpc/ibm4xx/pci_machdep.h>
    112 
    113 #include <powerpc/pic/picvar.h>
    114 
    115 #include <dev/cons.h>
    116 #include <dev/pci/pcivar.h>
    117 #include <dev/pci/pciconf.h>
    118 
    119 #include "ksyms.h"
    120 
    121 #if defined(DDB)
    122 #include <powerpc/db_machdep.h>
    123 #include <ddb/db_extern.h>
    124 #endif
    125 
    126 
    127 #define TLB_PG_SIZE 	(16*1024*1024)
    128 
    129 /*
    130  * Global variables used here and there
    131  */
    132 struct vm_map *phys_map = NULL;
    133 
    134 /*
    135  * This should probably be in autoconf!				XXX
    136  */
    137 char machine[] = MACHINE;		/* from <machine/param.h> */
    138 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    139 
    140 void initppc(vaddr_t, vaddr_t, char *, void *);
    141 
    142 static void dumpsys(void);
    143 
    144 struct board_cfg_data board_data;
    145 
    146 void
    147 initppc(vaddr_t startkernel, vaddr_t endkernel, char *args, void *info_block)
    148 {
    149 	/* Disable all external interrupts */
    150 	mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, 0);
    151 
    152         /* Initialize cache info for memcpy, etc. */
    153         cpu_probe_cache();
    154 
    155 	/* Save info block */
    156 	memcpy(&board_data, info_block, sizeof(board_data));
    157 
    158 	ibm40x_memsize_init(board_data.mem_size, startkernel);
    159 
    160 	/* Linear map kernel memory */
    161 	for (vaddr_t va = 0; va < endkernel; va += TLB_PG_SIZE) {
    162 		ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
    163 	}
    164 
    165 	/* Map console after physmem (see pmap_tlbmiss()) */
    166 	ppc4xx_tlb_reserve(IBM405GP_UART0_BASE,
    167 	    roundup(board_data.mem_size, TLB_PG_SIZE),
    168 	    TLB_PG_SIZE, TLB_I | TLB_G);
    169 
    170 	mtspr(SPR_TCR, 0);	/* disable all timers */
    171 
    172 	ibm4xx_init(startkernel, endkernel, pic_ext_intr);
    173 
    174 #ifdef DEBUG
    175 	printf("Board config data:\n");
    176 	printf("  usr_config_ver = %s\n", board_data.usr_config_ver);
    177 	printf("  rom_sw_ver = %s\n", board_data.rom_sw_ver);
    178 	printf("  mem_size = %u\n", board_data.mem_size);
    179 	printf("  mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
    180 	    board_data.mac_address_local[0], board_data.mac_address_local[1],
    181 	    board_data.mac_address_local[2], board_data.mac_address_local[3],
    182 	    board_data.mac_address_local[4], board_data.mac_address_local[5]);
    183 	printf("  mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
    184 	    board_data.mac_address_pci[0], board_data.mac_address_pci[1],
    185 	    board_data.mac_address_pci[2], board_data.mac_address_pci[3],
    186 	    board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
    187 	printf("  processor_speed = %u\n", board_data.processor_speed);
    188 	printf("  plb_speed = %u\n", board_data.plb_speed);
    189 	printf("  pci_speed = %u\n", board_data.pci_speed);
    190 #endif
    191 
    192 #ifdef DDB
    193 	if (boothowto & RB_KDB)
    194 		Debugger();
    195 #endif
    196 
    197 	/*
    198 	 * Look for the ibm4xx modules in the right place.
    199 	 */
    200 	module_machine = module_machine_ibm4xx;
    201 }
    202 
    203 /*
    204  * Machine dependent startup code.
    205  */
    206 
    207 void
    208 cpu_startup(void)
    209 {
    210 	prop_number_t pn;
    211 	prop_data_t pd;
    212 
    213 	ibm4xx_cpu_startup("Walnut PowerPC 405GP Evaluation Board");
    214 
    215 	board_info_init();
    216 
    217 	pn = prop_number_create_integer(board_data.mem_size);
    218 	KASSERT(pn != NULL);
    219 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
    220 		panic("setting mem-size");
    221 	prop_object_release(pn);
    222 
    223 	pd = prop_data_create_data_nocopy(board_data.mac_address_local,
    224 					  sizeof(board_data.mac_address_local));
    225 	KASSERT(pd != NULL);
    226 	if (prop_dictionary_set(board_properties, "emac0-mac-addr",
    227 				pd) == false)
    228 		panic("setting emac0-mac-addr");
    229 	prop_object_release(pd);
    230 
    231 	pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
    232 					  sizeof(board_data.mac_address_pci));
    233 	KASSERT(pd != NULL);
    234 	if (prop_dictionary_set(board_properties, "sip0-mac-addr",
    235 				pd) == false)
    236 		panic("setting sip0-mac-addr");
    237 	prop_object_release(pd);
    238 
    239 	pn = prop_number_create_integer(board_data.processor_speed);
    240 	KASSERT(pn != NULL);
    241 	if (prop_dictionary_set(board_properties, "processor-frequency",
    242 				pn) == false)
    243 		panic("setting processor-frequency");
    244 	prop_object_release(pn);
    245 
    246 	/*
    247 	 * Now that we have VM, malloc()s are OK in bus_space.
    248 	 */
    249 	bus_space_mallocok();
    250 	fake_mapiodev = 0;
    251 }
    252 
    253 
    254 static void
    255 dumpsys(void)
    256 {
    257 
    258 	printf("dumpsys: TBD\n");
    259 }
    260 
    261 /*
    262  * Halt or reboot the machine after syncing/dumping according to howto.
    263  */
    264 void
    265 cpu_reboot(int howto, char *what)
    266 {
    267 	static int syncing;
    268 	static char str[256];
    269 	char *ap = str, *ap1 = ap;
    270 
    271 	boothowto = howto;
    272 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    273 		syncing = 1;
    274 		vfs_shutdown();		/* sync */
    275 		resettodr();		/* set wall clock */
    276 	}
    277 
    278 	splhigh();
    279 
    280 	if (!cold && (howto & RB_DUMP))
    281 		dumpsys();
    282 
    283 	doshutdownhooks();
    284 
    285 	pmf_system_shutdown(boothowto);
    286 
    287 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    288 	  /* Power off here if we know how...*/
    289 	}
    290 
    291 	if (howto & RB_HALT) {
    292 		printf("halted\n\n");
    293 
    294 		goto reboot;	/* XXX for now... */
    295 
    296 #ifdef DDB
    297 		printf("dropping to debugger\n");
    298 		while(1)
    299 			Debugger();
    300 #endif
    301 	}
    302 
    303 	printf("rebooting\n\n");
    304 	if (what && *what) {
    305 		if (strlen(what) > sizeof str - 5)
    306 			printf("boot string too large, ignored\n");
    307 		else {
    308 			strcpy(str, what);
    309 			ap1 = ap = str + strlen(str);
    310 			*ap++ = ' ';
    311 		}
    312 	}
    313 	*ap++ = '-';
    314 	if (howto & RB_SINGLE)
    315 		*ap++ = 's';
    316 	if (howto & RB_KDB)
    317 		*ap++ = 'd';
    318 	*ap++ = 0;
    319 	if (ap[-2] == '-')
    320 		*ap1 = 0;
    321 
    322 	/* flush cache for msgbuf */
    323 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
    324 
    325  reboot:
    326 	ppc4xx_reset();
    327 
    328 	printf("ppc4xx_reset() failed!\n");
    329 #ifdef DDB
    330 	while(1)
    331 		Debugger();
    332 #else
    333 	while (1)
    334 		/* nothing */;
    335 #endif
    336 }
    337 
    338 int
    339 ibm4xx_pci_bus_maxdevs(void *v, int busno)
    340 {
    341 
    342 	/*
    343 	 * Bus number is irrelevant.  Configuration Mechanism 1 is in
    344 	 * use, can have devices 0-32 (i.e. the `normal' range).
    345 	 */
    346 	return 5;
    347 }
    348 
    349 int
    350 ibm4xx_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    351 {
    352 	int pin = pa->pa_intrpin;
    353 	int dev = pa->pa_device;
    354 
    355 	if (pin == 0)
    356 		/* No IRQ used. */
    357 		goto bad;
    358 
    359 	if (pin > 4) {
    360 		printf("%s: bad interrupt pin %d\n", __func__, pin);
    361 		goto bad;
    362 	}
    363 
    364 	/*
    365 	 * We need to map the interrupt pin to the interrupt bit in the UIC
    366 	 * associated with it.  This is highly machine-dependent.
    367 	 */
    368 	switch(dev) {
    369 	case 1:
    370 	case 2:
    371 	case 3:
    372 	case 4:
    373 		*ihp = 27 + dev;
    374 		break;
    375 	default:
    376 		printf("Hmm.. PCI device %d should not exist on this board\n",
    377 			dev);
    378 		goto bad;
    379 	}
    380 	return 0;
    381 
    382 bad:
    383 	*ihp = -1;
    384 	return 1;
    385 }
    386 
    387 void
    388 ibm4xx_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
    389     int *iline)
    390 {
    391 
    392 	if (bus == 0) {
    393 		switch(dev) {
    394 		case 1:
    395 		case 2:
    396 		case 3:
    397 		case 4:
    398 			*iline = 31 - dev;
    399 		}
    400 	} else
    401 		*iline = 20 + ((swiz + dev + 1) & 3);
    402 }
    403