1 /* $NetBSD: machdep.c,v 1.36 2011/06/20 17:44:33 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Juergen Hannken-Illjes. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.36 2011/06/20 17:44:33 matt Exp $"); 34 35 #include "opt_explora.h" 36 #include "opt_modular.h" 37 #include "ksyms.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/buf.h> 42 #include <sys/msgbuf.h> 43 #include <sys/kernel.h> 44 #include <sys/mount.h> 45 #include <sys/proc.h> 46 #include <sys/reboot.h> 47 #include <sys/ksyms.h> 48 #include <sys/device.h> 49 #include <sys/module.h> 50 #include <sys/bus.h> 51 #include <sys/cpu.h> 52 53 #include <uvm/uvm_extern.h> 54 55 #include <prop/proplib.h> 56 57 #include <machine/explora.h> 58 #include <machine/powerpc.h> 59 #include <machine/tlb.h> 60 #include <machine/pcb.h> 61 #include <machine/trap.h> 62 63 #include <powerpc/spr.h> 64 #include <powerpc/ibm4xx/spr.h> 65 66 #include <powerpc/ibm4xx/cpu.h> 67 #include <powerpc/ibm4xx/dcr403cgx.h> 68 69 #if NKSYMS || defined(DDB) || defined(MODULAR) 70 #include <machine/db_machdep.h> 71 #include <ddb/db_extern.h> 72 #endif 73 74 #define MEMREGIONS 2 75 #define TLB_PG_SIZE (16*1024*1024) 76 77 char cpu_model[80]; 78 char machine[] = MACHINE; /* from <machine/param.h> */ 79 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */ 80 81 static const unsigned int cpuspeed = 66000000; 82 83 prop_dictionary_t board_properties; 84 struct vm_map *phys_map = NULL; 85 char msgbuf[MSGBUFSIZE]; 86 paddr_t msgbuf_paddr; 87 88 static struct mem_region phys_mem[MEMREGIONS]; 89 static struct mem_region avail_mem[MEMREGIONS]; 90 91 void initppc(vaddr_t, vaddr_t); 92 93 void 94 initppc(vaddr_t startkernel, vaddr_t endkernel) 95 { 96 u_int i, j, t, br[4]; 97 u_int maddr, msize, size; 98 99 br[0] = mfdcr(DCR_BR4); 100 br[1] = mfdcr(DCR_BR5); 101 br[2] = mfdcr(DCR_BR6); 102 br[3] = mfdcr(DCR_BR7); 103 104 for (i = 0; i < 4; i++) 105 for (j = i+1; j < 4; j++) 106 if (br[j] < br[i]) 107 t = br[j], br[j] = br[i], br[i] = t; 108 109 for (i = 0, size = 0; i < 4; i++) { 110 if (((br[i] >> 19) & 3) != 3) 111 continue; 112 maddr = ((br[i] >> 24) & 0xff) << 20; 113 msize = 1 << (20 + ((br[i] >> 21) & 7)); 114 if (maddr+msize > size) 115 size = maddr+msize; 116 } 117 118 phys_mem[0].start = 0; 119 phys_mem[0].size = size & ~PGOFSET; 120 avail_mem[0].start = startkernel; 121 avail_mem[0].size = size-startkernel; 122 123 __asm volatile( 124 " mtpid %0 \n" 125 " sync \n" 126 : : "r" (KERNEL_PID) ); 127 128 /* 129 * Setup initial tlbs. 130 * Kernel memory and console device are 131 * mapped into the first (reserved) tlbs. 132 */ 133 134 for (maddr = 0; maddr < endkernel; maddr += TLB_PG_SIZE) 135 ppc4xx_tlb_reserve(maddr, maddr, TLB_PG_SIZE, TLB_EX); 136 137 /* Map PCKBC, PCKBC2, COM, LPT. This is far beyond physmem. */ 138 ppc4xx_tlb_reserve(BASE_ISA, BASE_ISA, TLB_PG_SIZE, TLB_I | TLB_G); 139 140 #ifndef COM_IS_CONSOLE 141 ppc4xx_tlb_reserve(BASE_FB, BASE_FB, TLB_PG_SIZE, TLB_I | TLB_G); 142 ppc4xx_tlb_reserve(BASE_FB2, BASE_FB2, TLB_PG_SIZE, TLB_I | TLB_G); 143 #endif 144 145 /* Disable all external interrupts */ 146 mtdcr(DCR_EXIER, 0); 147 148 /* Disable all timer interrupts */ 149 mtspr(SPR_TCR, 0); 150 151 ibm4xx_init(startkernel, endkernel, pic_ext_intr); 152 } 153 154 void 155 cpu_startup(void) 156 { 157 vaddr_t minaddr, maxaddr; 158 prop_number_t pn; 159 char pbuf[9]; 160 161 /* 162 * Initialize error message buffer (before start of kernel) 163 */ 164 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE)); 165 166 printf("%s%s", copyright, version); 167 printf("NCD Explora451\n"); 168 169 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 170 printf("total memory = %s\n", pbuf); 171 172 minaddr = 0; 173 /* 174 * Allocate a submap for physio 175 */ 176 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 177 VM_PHYS_SIZE, 0, false, NULL); 178 179 /* 180 * No need to allocate an mbuf cluster submap. Mbuf clusters 181 * are allocated via the pool allocator, and we use direct-mapped 182 * pool pages. 183 */ 184 185 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 186 printf("avail memory = %s\n", pbuf); 187 188 /* 189 * Set up the board properties database. 190 */ 191 board_properties = prop_dictionary_create(); 192 KASSERT(board_properties != NULL); 193 194 pn = prop_number_create_integer(ctob(physmem)); 195 KASSERT(pn != NULL); 196 if (prop_dictionary_set(board_properties, "mem-size", pn) == false) 197 panic("setting mem-size"); 198 prop_object_release(pn); 199 200 pn = prop_number_create_integer(cpuspeed); 201 KASSERT(pn != NULL); 202 if (prop_dictionary_set(board_properties, "processor-frequency", 203 pn) == false) 204 panic("setting processor-frequency"); 205 prop_object_release(pn); 206 207 intr_init(); 208 209 /* 210 * Look for the ibm4xx modules in the right place. 211 */ 212 module_machine = module_machine_ibm4xx; 213 fake_mapiodev = 0; 214 } 215 216 void 217 cpu_reboot(int howto, char *what) 218 { 219 static int syncing = 0; 220 221 boothowto = howto; 222 if (!cold && !(howto & RB_NOSYNC) && !syncing) { 223 syncing = 1; 224 vfs_shutdown(); 225 resettodr(); 226 } 227 228 splhigh(); 229 230 if (!cold && (howto & RB_DUMP)) 231 /*XXX dumpsys()*/; 232 233 doshutdownhooks(); 234 235 pmf_system_shutdown(boothowto); 236 237 if (howto & RB_HALT) { 238 printf("halted\n\n"); 239 240 while (1) 241 ; 242 } 243 244 printf("rebooting\n\n"); 245 246 /* flush cache for msgbuf */ 247 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE)); 248 249 ppc4xx_reset(); 250 251 #ifdef DDB 252 while (1) 253 Debugger(); 254 #else 255 while (1) 256 ; 257 #endif 258 } 259 260 void 261 mem_regions(struct mem_region **mem, struct mem_region **avail) 262 { 263 *mem = phys_mem; 264 *avail = avail_mem; 265 } 266