1 /* $NetBSD: machdep.c,v 1.55 2012/07/27 22:13:58 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.55 2012/07/27 22:13:58 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/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 char bootpath[256]; 141 142 void initppc(vaddr_t, vaddr_t, char *, void *); 143 144 static void dumpsys(void); 145 146 #define MEMREGIONS 8 147 struct mem_region physmemr[MEMREGIONS]; /* Hard code memory */ 148 struct mem_region availmemr[MEMREGIONS]; /* Who's supposed to set these up? */ 149 150 struct board_cfg_data board_data; 151 152 void 153 initppc(vaddr_t startkernel, vaddr_t endkernel, char *args, void *info_block) 154 { 155 /* Disable all external interrupts */ 156 mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, 0); 157 158 /* Initialize cache info for memcpy, etc. */ 159 cpu_probe_cache(); 160 161 /* Save info block */ 162 memcpy(&board_data, info_block, sizeof(board_data)); 163 164 memset(physmemr, 0, sizeof physmemr); 165 memset(availmemr, 0, sizeof availmemr); 166 physmemr[0].start = 0; 167 physmemr[0].size = board_data.mem_size & ~PGOFSET; 168 /* Lower memory reserved by eval board BIOS */ 169 availmemr[0].start = startkernel; 170 availmemr[0].size = board_data.mem_size - availmemr[0].start; 171 172 /* Linear map kernel memory */ 173 for (vaddr_t va = 0; va < endkernel; va += TLB_PG_SIZE) { 174 ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX); 175 } 176 177 /* Map console after physmem (see pmap_tlbmiss()) */ 178 ppc4xx_tlb_reserve(0xef000000, roundup(physmemr[0].size, TLB_PG_SIZE), 179 TLB_PG_SIZE, TLB_I | TLB_G); 180 181 mtspr(SPR_TCR, 0); /* disable all timers */ 182 183 ibm4xx_init(startkernel, endkernel, pic_ext_intr); 184 185 #ifdef DEBUG 186 printf("Board config data:\n"); 187 printf(" usr_config_ver = %s\n", board_data.usr_config_ver); 188 printf(" rom_sw_ver = %s\n", board_data.rom_sw_ver); 189 printf(" mem_size = %u\n", board_data.mem_size); 190 printf(" mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n", 191 board_data.mac_address_local[0], board_data.mac_address_local[1], 192 board_data.mac_address_local[2], board_data.mac_address_local[3], 193 board_data.mac_address_local[4], board_data.mac_address_local[5]); 194 printf(" mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n", 195 board_data.mac_address_pci[0], board_data.mac_address_pci[1], 196 board_data.mac_address_pci[2], board_data.mac_address_pci[3], 197 board_data.mac_address_pci[4], board_data.mac_address_pci[5]); 198 printf(" processor_speed = %u\n", board_data.processor_speed); 199 printf(" plb_speed = %u\n", board_data.plb_speed); 200 printf(" pci_speed = %u\n", board_data.pci_speed); 201 #endif 202 203 #ifdef DDB 204 if (boothowto & RB_KDB) 205 Debugger(); 206 #endif 207 #ifdef IPKDB 208 /* 209 * Now trap to IPKDB 210 */ 211 ipkdb_init(); 212 if (boothowto & RB_KDB) 213 ipkdb_connect(0); 214 #endif 215 216 /* 217 * Look for the ibm4xx modules in the right place. 218 */ 219 module_machine = module_machine_ibm4xx; 220 } 221 222 /* 223 * Machine dependent startup code. 224 */ 225 226 void 227 cpu_startup(void) 228 { 229 vaddr_t minaddr, maxaddr; 230 prop_number_t pn; 231 prop_data_t pd; 232 char pbuf[9]; 233 234 /* 235 * Initialize error message buffer (at end of core). 236 */ 237 #if 0 /* For some reason this fails... --Artem 238 * Besides, do we really have to put it at the end of core? 239 * Let's use static buffer for now 240 */ 241 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0, 242 UVM_KMF_VAONLY))) 243 panic("startup: no room for message buffer"); 244 for (i = 0; i < btoc(MSGBUFSIZE); i++) 245 pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE, 246 msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0); 247 initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE)); 248 #else 249 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE)); 250 #endif 251 252 printf("%s%s", copyright, version); 253 printf("Walnut PowerPC 405GP Evaluation Board\n"); 254 255 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 256 printf("total memory = %s\n", pbuf); 257 258 minaddr = 0; 259 /* 260 * Allocate a submap for physio 261 */ 262 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 263 VM_PHYS_SIZE, 0, false, NULL); 264 265 /* 266 * No need to allocate an mbuf cluster submap. Mbuf clusters 267 * are allocated via the pool allocator, and we use direct-mapped 268 * pool pages. 269 */ 270 271 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 272 printf("avail memory = %s\n", pbuf); 273 274 /* 275 * Set up the board properties dictionary. 276 */ 277 board_properties = prop_dictionary_create(); 278 KASSERT(board_properties != NULL); 279 280 pn = prop_number_create_integer(board_data.mem_size); 281 KASSERT(pn != NULL); 282 if (prop_dictionary_set(board_properties, "mem-size", pn) == false) 283 panic("setting mem-size"); 284 prop_object_release(pn); 285 286 pd = prop_data_create_data_nocopy(board_data.mac_address_local, 287 sizeof(board_data.mac_address_local)); 288 KASSERT(pd != NULL); 289 if (prop_dictionary_set(board_properties, "emac0-mac-addr", 290 pd) == false) 291 panic("setting emac0-mac-addr"); 292 prop_object_release(pd); 293 294 pd = prop_data_create_data_nocopy(board_data.mac_address_pci, 295 sizeof(board_data.mac_address_pci)); 296 KASSERT(pd != NULL); 297 if (prop_dictionary_set(board_properties, "sip0-mac-addr", 298 pd) == false) 299 panic("setting sip0-mac-addr"); 300 prop_object_release(pd); 301 302 pn = prop_number_create_integer(board_data.processor_speed); 303 KASSERT(pn != NULL); 304 if (prop_dictionary_set(board_properties, "processor-frequency", 305 pn) == false) 306 panic("setting processor-frequency"); 307 prop_object_release(pn); 308 309 /* 310 * Now that we have VM, malloc()s are OK in bus_space. 311 */ 312 bus_space_mallocok(); 313 fake_mapiodev = 0; 314 } 315 316 317 static void 318 dumpsys(void) 319 { 320 321 printf("dumpsys: TBD\n"); 322 } 323 324 /* 325 * Halt or reboot the machine after syncing/dumping according to howto. 326 */ 327 void 328 cpu_reboot(int howto, char *what) 329 { 330 static int syncing; 331 static char str[256]; 332 char *ap = str, *ap1 = ap; 333 334 boothowto = howto; 335 if (!cold && !(howto & RB_NOSYNC) && !syncing) { 336 syncing = 1; 337 vfs_shutdown(); /* sync */ 338 resettodr(); /* set wall clock */ 339 } 340 341 splhigh(); 342 343 if (!cold && (howto & RB_DUMP)) 344 dumpsys(); 345 346 doshutdownhooks(); 347 348 pmf_system_shutdown(boothowto); 349 350 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) { 351 /* Power off here if we know how...*/ 352 } 353 354 if (howto & RB_HALT) { 355 printf("halted\n\n"); 356 357 goto reboot; /* XXX for now... */ 358 359 #ifdef DDB 360 printf("dropping to debugger\n"); 361 while(1) 362 Debugger(); 363 #endif 364 } 365 366 printf("rebooting\n\n"); 367 if (what && *what) { 368 if (strlen(what) > sizeof str - 5) 369 printf("boot string too large, ignored\n"); 370 else { 371 strcpy(str, what); 372 ap1 = ap = str + strlen(str); 373 *ap++ = ' '; 374 } 375 } 376 *ap++ = '-'; 377 if (howto & RB_SINGLE) 378 *ap++ = 's'; 379 if (howto & RB_KDB) 380 *ap++ = 'd'; 381 *ap++ = 0; 382 if (ap[-2] == '-') 383 *ap1 = 0; 384 385 /* flush cache for msgbuf */ 386 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE)); 387 388 reboot: 389 ppc4xx_reset(); 390 391 printf("ppc4xx_reset() failed!\n"); 392 #ifdef DDB 393 while(1) 394 Debugger(); 395 #else 396 while (1) 397 /* nothing */; 398 #endif 399 } 400 401 void 402 mem_regions(struct mem_region **mem, struct mem_region **avail) 403 { 404 405 *mem = physmemr; 406 *avail = availmemr; 407 } 408 409 410 int 411 ibm4xx_pci_bus_maxdevs(void *v, int busno) 412 { 413 414 /* 415 * Bus number is irrelevant. Configuration Mechanism 1 is in 416 * use, can have devices 0-32 (i.e. the `normal' range). 417 */ 418 return 5; 419 } 420 421 int 422 ibm4xx_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 423 { 424 int pin = pa->pa_intrpin; 425 int dev = pa->pa_device; 426 427 if (pin == 0) 428 /* No IRQ used. */ 429 goto bad; 430 431 if (pin > 4) { 432 printf("%s: bad interrupt pin %d\n", __func__, pin); 433 goto bad; 434 } 435 436 /* 437 * We need to map the interrupt pin to the interrupt bit in the UIC 438 * associated with it. This is highly machine-dependent. 439 */ 440 switch(dev) { 441 case 1: 442 case 2: 443 case 3: 444 case 4: 445 *ihp = 27 + dev; 446 break; 447 default: 448 printf("Hmm.. PCI device %d should not exist on this board\n", 449 dev); 450 goto bad; 451 } 452 return 0; 453 454 bad: 455 *ihp = -1; 456 return 1; 457 } 458 459 void 460 ibm4xx_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz, 461 int *iline) 462 { 463 464 if (bus == 0) { 465 switch(dev) { 466 case 1: 467 case 2: 468 case 3: 469 case 4: 470 *iline = 31 - dev; 471 } 472 } else 473 *iline = 20 + ((swiz + dev + 1) & 3); 474 } 475