No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / evbppc / explora / machdep.c
blobe0cb30c647735b192e225859ce443f1c9540dbe4
1 /* $NetBSD: machdep.c,v 1.28 2009/11/26 00:19:16 matt Exp $ */
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juergen Hannken-Illjes.
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.28 2009/11/26 00:19:16 matt Exp $");
35 #include "opt_explora.h"
36 #include "opt_modular.h"
37 #include "ksyms.h"
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>
50 #include <uvm/uvm_extern.h>
52 #include <prop/proplib.h>
54 #include <machine/explora.h>
55 #include <machine/bus.h>
56 #include <machine/powerpc.h>
57 #include <machine/tlb.h>
58 #include <machine/trap.h>
60 #include <powerpc/spr.h>
61 #include <powerpc/ibm4xx/dcr403cgx.h>
63 #if NKSYMS || defined(DDB) || defined(MODULAR)
64 #include <machine/db_machdep.h>
65 #include <ddb/db_extern.h>
66 #endif
68 #define MEMREGIONS 2
69 #define TLB_PG_SIZE (16*1024*1024)
71 char cpu_model[80];
72 char machine[] = MACHINE; /* from <machine/param.h> */
73 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
75 static const unsigned int cpuspeed = 66000000;
77 prop_dictionary_t board_properties;
78 struct vm_map *phys_map = NULL;
79 struct vm_map *mb_map = NULL;
80 char msgbuf[MSGBUFSIZE];
81 paddr_t msgbuf_paddr;
83 static struct mem_region phys_mem[MEMREGIONS];
84 static struct mem_region avail_mem[MEMREGIONS];
86 void bootstrap(u_int, u_int);
87 static void install_extint(void (*)(void));
88 int lcsplx(int);
91 * Trap vectors
93 extern int defaulttrap, defaultsize;
94 extern int sctrap, scsize;
95 extern int alitrap, alisize;
96 extern int dsitrap, dsisize;
97 extern int isitrap, isisize;
98 extern int mchktrap, mchksize;
99 extern int tlbimiss4xx, tlbim4size;
100 extern int tlbdmiss4xx, tlbdm4size;
101 extern int pitfitwdog, pitfitwdogsize;
102 extern int debugtrap, debugsize;
103 extern int errata51handler, errata51size;
104 #ifdef DDB
105 extern int ddblow, ddbsize;
106 #endif
107 static struct {
108 int vector;
109 void *addr;
110 void *size;
111 } trap_table[] = {
112 { EXC_SC, &sctrap, &scsize },
113 { EXC_ALI, &alitrap, &alisize },
114 { EXC_DSI, &dsitrap, &dsisize },
115 { EXC_ISI, &isitrap, &isisize },
116 { EXC_MCHK, &mchktrap, &mchksize },
117 { EXC_ITMISS, &tlbimiss4xx, &tlbim4size },
118 { EXC_DTMISS, &tlbdmiss4xx, &tlbdm4size },
119 { EXC_PIT, &pitfitwdog, &pitfitwdogsize },
120 { EXC_DEBUG, &debugtrap, &debugsize },
121 { (EXC_DTMISS|EXC_ALI), &errata51handler, &errata51size },
122 #if defined(DDB)
123 { EXC_PGM, &ddblow, &ddbsize },
124 #endif /* DDB */
128 * Install a trap vector. We cannot use memcpy because the
129 * destination may be zero.
131 static void
132 trap_copy(void *src, int dest, size_t len)
134 uint32_t *src_p = src;
135 uint32_t *dest_p = (void *)dest;
137 while (len > 0) {
138 *dest_p++ = *src_p++;
139 len -= sizeof(uint32_t);
143 void
144 bootstrap(u_int startkernel, u_int endkernel)
146 u_int i, j, t, br[4];
147 u_int maddr, msize, size;
148 struct cpu_info * const ci = &cpu_info[0];
150 br[0] = mfdcr(DCR_BR4);
151 br[1] = mfdcr(DCR_BR5);
152 br[2] = mfdcr(DCR_BR6);
153 br[3] = mfdcr(DCR_BR7);
155 for (i = 0; i < 4; i++)
156 for (j = i+1; j < 4; j++)
157 if (br[j] < br[i])
158 t = br[j], br[j] = br[i], br[i] = t;
160 for (i = 0, size = 0; i < 4; i++) {
161 if (((br[i] >> 19) & 3) != 3)
162 continue;
163 maddr = ((br[i] >> 24) & 0xff) << 20;
164 msize = 1 << (20 + ((br[i] >> 21) & 7));
165 if (maddr+msize > size)
166 size = maddr+msize;
169 phys_mem[0].start = 0;
170 phys_mem[0].size = size & ~PGOFSET;
171 avail_mem[0].start = startkernel;
172 avail_mem[0].size = size-startkernel;
174 __asm volatile(
175 " mtpid %0 \n"
176 " sync \n"
177 : : "r" (KERNEL_PID) );
180 * Setup initial tlbs.
181 * Kernel memory and console device are
182 * mapped into the first (reserved) tlbs.
185 for (maddr = 0; maddr < endkernel; maddr += TLB_PG_SIZE)
186 ppc4xx_tlb_reserve(maddr, maddr, TLB_PG_SIZE, TLB_EX);
188 /* Map PCKBC, PCKBC2, COM, LPT. This is far beyond physmem. */
189 ppc4xx_tlb_reserve(BASE_ISA, BASE_ISA, TLB_PG_SIZE, TLB_I | TLB_G);
191 #ifndef COM_IS_CONSOLE
192 ppc4xx_tlb_reserve(BASE_FB, BASE_FB, TLB_PG_SIZE, TLB_I | TLB_G);
193 ppc4xx_tlb_reserve(BASE_FB2, BASE_FB2, TLB_PG_SIZE, TLB_I | TLB_G);
194 #endif
196 consinit();
198 /* Disable all external interrupts */
199 mtdcr(DCR_EXIER, 0);
201 /* Disable all timer interrupts */
202 mtspr(SPR_TCR, 0);
204 /* Initialize cache info for memcpy, etc. */
205 cpu_probe_cache();
208 * Initialize lwp0 and current pcb and pmap pointers.
210 lwp0.l_cpu = ci;
212 curpcb = lwp_getpcb(&lwp0);
213 memset(curpcb, 0, sizeof(struct pcb)); /* XXX why? */
214 curpcb->pcb_pm = pmap_kernel();
217 * Install trap vectors.
220 for (i = EXC_RSVD; i <= EXC_LAST; i += 0x100)
221 trap_copy(&defaulttrap, i, (size_t)&defaultsize);
223 for (i = 0; i < sizeof(trap_table)/sizeof(trap_table[0]); i++) {
224 trap_copy(trap_table[i].addr, trap_table[i].vector,
225 (size_t)trap_table[i].size);
228 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
231 * Set Exception vector base.
232 * Handle trap instruction as PGM exception.
235 mtspr(SPR_EVPR, 0);
237 t = mfspr(SPR_DBCR0);
238 t &= ~DBCR0_TDE;
239 mtspr(SPR_DBCR0, t);
242 * External interrupt handler install.
245 install_extint(ext_intr);
248 * Now enable translation (and machine checks/recoverable interrupts).
250 __asm volatile (
251 " mfmsr %0 \n"
252 " ori %0,%0,%1 \n"
253 " mtmsr %0 \n"
254 " sync \n"
255 : : "r" (0), "K" (PSL_IR|PSL_DR|PSL_ME) );
257 uvm_setpagesize();
260 * Initialize pmap module.
262 pmap_bootstrap(startkernel, endkernel);
263 fake_mapiodev = 0;
266 static void
267 install_extint(void (*handler)(void))
269 extern int extint, extsize;
270 extern u_long extint_call;
271 u_long offset = (u_long)handler - (u_long)&extint_call;
272 int omsr, msr;
274 #ifdef DIAGNOSTIC
275 if (offset > 0x1ffffff)
276 panic("install_extint: too far away");
277 #endif
278 __asm volatile (
279 " mfmsr %0 \n"
280 " andi. %1,%0,%2 \n"
281 " mtmsr %1 \n"
282 : "=r" (omsr), "=r" (msr) : "K" ((u_short)~PSL_EE) );
283 extint_call = (extint_call & 0xfc000003) | offset;
284 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
285 __syncicache((void *)&extint_call, sizeof extint_call);
286 __syncicache((void *)EXC_EXI, (int)&extsize);
287 __asm volatile (
288 " mtmsr %0 \n"
289 : : "r" (omsr) );
292 void
293 cpu_startup(void)
295 vaddr_t minaddr, maxaddr;
296 prop_number_t pn;
297 char pbuf[9];
300 * Initialize error message buffer (before start of kernel)
302 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
304 printf("%s%s", copyright, version);
305 printf("NCD Explora451\n");
307 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
308 printf("total memory = %s\n", pbuf);
310 minaddr = 0;
312 * Allocate a submap for physio
314 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
315 VM_PHYS_SIZE, 0, false, NULL);
318 * No need to allocate an mbuf cluster submap. Mbuf clusters
319 * are allocated via the pool allocator, and we use direct-mapped
320 * pool pages.
323 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
324 printf("avail memory = %s\n", pbuf);
327 * Set up the board properties database.
329 board_properties = prop_dictionary_create();
330 KASSERT(board_properties != NULL);
332 pn = prop_number_create_integer(ctob(physmem));
333 KASSERT(pn != NULL);
334 if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
335 panic("setting mem-size");
336 prop_object_release(pn);
338 pn = prop_number_create_integer(cpuspeed);
339 KASSERT(pn != NULL);
340 if (prop_dictionary_set(board_properties, "processor-frequency",
341 pn) == false)
342 panic("setting processor-frequency");
343 prop_object_release(pn);
345 intr_init();
349 lcsplx(int ipl)
351 return spllower(ipl); /*XXX*/
354 void
355 cpu_reboot(int howto, char *what)
357 static int syncing = 0;
359 boothowto = howto;
360 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
361 syncing = 1;
362 vfs_shutdown();
363 resettodr();
366 splhigh();
368 if (!cold && (howto & RB_DUMP))
369 /*XXX dumpsys()*/;
371 doshutdownhooks();
373 pmf_system_shutdown(boothowto);
375 if (howto & RB_HALT) {
376 printf("halted\n\n");
378 while (1)
382 printf("rebooting\n\n");
384 /* flush cache for msgbuf */
385 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
387 ppc4xx_reset();
389 #ifdef DDB
390 while (1)
391 Debugger();
392 #else
393 while (1)
395 #endif
398 void
399 mem_regions(struct mem_region **mem, struct mem_region **avail)
401 *mem = phys_mem;
402 *avail = avail_mem;