xref: /netbsd-src/sys/arch/evbppc/explora/machdep.c (revision d25ffa98a4bfca1fe272f3c182496ec9934faac7)
1 /*	$NetBSD: machdep.c,v 1.35 2011/06/18 06:44:25 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.35 2011/06/18 06:44:25 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		bootstrap(u_int, u_int);
92 static void	install_extint(void (*)(void));
93 
94 /*
95  * Trap vectors
96  */
97 extern int defaulttrap, defaultsize;
98 extern int sctrap, scsize;
99 extern int alitrap, alisize;
100 extern int dsitrap, dsisize;
101 extern int isitrap, isisize;
102 extern int mchktrap, mchksize;
103 extern int tlbimiss4xx, tlbim4size;
104 extern int tlbdmiss4xx, tlbdm4size;
105 extern int pitfitwdog, pitfitwdogsize;
106 extern int debugtrap, debugsize;
107 extern int errata51handler, errata51size;
108 #ifdef DDB
109 extern int ddblow, ddbsize;
110 #endif
111 static struct {
112 	int vector;
113 	void *addr;
114 	void *size;
115 } trap_table[] = {
116 	{ EXC_SC,	&sctrap,	&scsize },
117 	{ EXC_ALI,	&alitrap,	&alisize },
118 	{ EXC_DSI,	&dsitrap,	&dsisize },
119 	{ EXC_ISI,	&isitrap,	&isisize },
120 	{ EXC_MCHK,	&mchktrap,	&mchksize },
121 	{ EXC_ITMISS,	&tlbimiss4xx,	&tlbim4size },
122 	{ EXC_DTMISS,	&tlbdmiss4xx,	&tlbdm4size },
123 	{ EXC_PIT,	&pitfitwdog,	&pitfitwdogsize },
124 	{ EXC_DEBUG,	&debugtrap,	&debugsize },
125 	{ (EXC_DTMISS|EXC_ALI), &errata51handler, &errata51size },
126 #if defined(DDB)
127 	{ EXC_PGM,	&ddblow,	&ddbsize },
128 #endif /* DDB */
129 };
130 
131 /*
132  * Install a trap vector. We cannot use memcpy because the
133  * destination may be zero.
134  */
135 static void
136 trap_copy(void *src, int dest, size_t len)
137 {
138 	uint32_t *src_p = src;
139 	uint32_t *dest_p = (void *)dest;
140 
141 	while (len > 0) {
142 		*dest_p++ = *src_p++;
143 		len -= sizeof(uint32_t);
144 	}
145 }
146 
147 void
148 bootstrap(u_int startkernel, u_int endkernel)
149 {
150 	u_int i, j, t, br[4];
151 	u_int maddr, msize, size;
152 	struct cpu_info * const ci = &cpu_info[0];
153 
154 	br[0] = mfdcr(DCR_BR4);
155 	br[1] = mfdcr(DCR_BR5);
156 	br[2] = mfdcr(DCR_BR6);
157 	br[3] = mfdcr(DCR_BR7);
158 
159 	for (i = 0; i < 4; i++)
160 		for (j = i+1; j < 4; j++)
161 			if (br[j] < br[i])
162 				t = br[j], br[j] = br[i], br[i] = t;
163 
164 	for (i = 0, size = 0; i < 4; i++) {
165 		if (((br[i] >> 19) & 3) != 3)
166 			continue;
167 		maddr = ((br[i] >> 24) & 0xff) << 20;
168 		msize = 1 << (20 + ((br[i] >> 21) & 7));
169 		if (maddr+msize > size)
170 			size = maddr+msize;
171 	}
172 
173 	phys_mem[0].start = 0;
174 	phys_mem[0].size = size & ~PGOFSET;
175 	avail_mem[0].start = startkernel;
176 	avail_mem[0].size = size-startkernel;
177 
178 	__asm volatile(
179 	    "	mtpid %0	\n"
180 	    "	sync		\n"
181 	    : : "r" (KERNEL_PID) );
182 
183 	/*
184 	 * Setup initial tlbs.
185 	 * Kernel memory and console device are
186 	 * mapped into the first (reserved) tlbs.
187 	 */
188 
189 	for (maddr = 0; maddr < endkernel; maddr += TLB_PG_SIZE)
190 		ppc4xx_tlb_reserve(maddr, maddr, TLB_PG_SIZE, TLB_EX);
191 
192 	/* Map PCKBC, PCKBC2, COM, LPT. This is far beyond physmem. */
193 	ppc4xx_tlb_reserve(BASE_ISA, BASE_ISA, TLB_PG_SIZE, TLB_I | TLB_G);
194 
195 #ifndef COM_IS_CONSOLE
196 	ppc4xx_tlb_reserve(BASE_FB,  BASE_FB,  TLB_PG_SIZE, TLB_I | TLB_G);
197 	ppc4xx_tlb_reserve(BASE_FB2, BASE_FB2, TLB_PG_SIZE, TLB_I | TLB_G);
198 #endif
199 
200 	consinit();
201 
202 	/* Disable all external interrupts */
203 	mtdcr(DCR_EXIER, 0);
204 
205 	/* Disable all timer interrupts */
206 	mtspr(SPR_TCR, 0);
207 
208 	/* Initialize cache info for memcpy, etc. */
209 	cpu_probe_cache();
210 
211 	/*
212 	 * Initialize lwp0 and current pcb and pmap pointers.
213 	 */
214 	lwp0.l_cpu = ci;
215 
216 	curpcb = lwp_getpcb(&lwp0);
217 	memset(curpcb, 0, sizeof(struct pcb));	/* XXX why? */
218 	curpcb->pcb_pm = pmap_kernel();
219 
220 	/*
221 	 * Install trap vectors.
222 	 */
223 
224 	for (i = EXC_RSVD; i <= EXC_LAST; i += 0x100)
225 		trap_copy(&defaulttrap, i, (size_t)&defaultsize);
226 
227 	for (i = 0; i < sizeof(trap_table)/sizeof(trap_table[0]); i++) {
228 		trap_copy(trap_table[i].addr, trap_table[i].vector,
229 		    (size_t)trap_table[i].size);
230 	}
231 
232 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
233 
234 	/*
235 	 * Set Exception vector base.
236 	 * Handle trap instruction as PGM exception.
237 	 */
238 
239 	mtspr(SPR_EVPR, 0);
240 
241 	t = mfspr(SPR_DBCR0);
242 	t &= ~DBCR0_TDE;
243 	mtspr(SPR_DBCR0, t);
244 
245 	/*
246 	 * External interrupt handler install.
247 	 */
248 
249 	install_extint(ext_intr);
250 
251 	/*
252 	 * Now enable translation (and machine checks/recoverable interrupts).
253 	 */
254 	__asm volatile (
255 	    "	mfmsr %0	\n"
256 	    "	ori %0,%0,%1	\n"
257 	    "	mtmsr %0	\n"
258 	    "	sync		\n"
259 	    : : "r" (0), "K" (PSL_IR|PSL_DR|PSL_ME) );
260 
261 	uvm_setpagesize();
262 
263 	/*
264 	 * Initialize pmap module.
265 	 */
266 	pmap_bootstrap(startkernel, endkernel);
267 	fake_mapiodev = 0;
268 }
269 
270 static void
271 install_extint(void (*handler)(void))
272 {
273 	extern int extint, extsize;
274 	extern u_long extint_call;
275 	u_long offset = (u_long)handler - (u_long)&extint_call;
276 	int omsr, msr;
277 
278 #ifdef	DIAGNOSTIC
279 	if (offset > 0x1ffffff)
280 		panic("install_extint: too far away");
281 #endif
282 	__asm volatile (
283 	    "	mfmsr %0	\n"
284 	    "	andi. %1,%0,%2	\n"
285 	    "	mtmsr %1	\n"
286 	    : "=r" (omsr), "=r" (msr) : "K" ((u_short)~PSL_EE) );
287 	extint_call = (extint_call & 0xfc000003) | offset;
288 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
289 	__syncicache((void *)&extint_call, sizeof extint_call);
290 	__syncicache((void *)EXC_EXI, (int)&extsize);
291 	__asm volatile (
292 	    "	mtmsr %0	\n"
293 	    : : "r" (omsr) );
294 }
295 
296 void
297 cpu_startup(void)
298 {
299 	vaddr_t minaddr, maxaddr;
300 	prop_number_t pn;
301 	char pbuf[9];
302 
303 	/*
304 	 * Initialize error message buffer (before start of kernel)
305 	 */
306 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
307 
308 	printf("%s%s", copyright, version);
309 	printf("NCD Explora451\n");
310 
311 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
312 	printf("total memory = %s\n", pbuf);
313 
314 	minaddr = 0;
315 	/*
316 	 * Allocate a submap for physio
317 	 */
318 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
319 				 VM_PHYS_SIZE, 0, false, NULL);
320 
321 	/*
322 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
323 	 * are allocated via the pool allocator, and we use direct-mapped
324 	 * pool pages.
325 	 */
326 
327 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
328 	printf("avail memory = %s\n", pbuf);
329 
330 	/*
331 	 * Set up the board properties database.
332 	 */
333 	board_properties = prop_dictionary_create();
334 	KASSERT(board_properties != NULL);
335 
336 	pn = prop_number_create_integer(ctob(physmem));
337 	KASSERT(pn != NULL);
338 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
339 		panic("setting mem-size");
340 	prop_object_release(pn);
341 
342 	pn = prop_number_create_integer(cpuspeed);
343 	KASSERT(pn != NULL);
344 	if (prop_dictionary_set(board_properties, "processor-frequency",
345 				pn) == false)
346 		panic("setting processor-frequency");
347 	prop_object_release(pn);
348 
349 	intr_init();
350 
351 	/*
352 	 * Look for the ibm4xx modules in the right place.
353 	 */
354 	module_machine = module_machine_ibm4xx;
355 }
356 
357 void
358 cpu_reboot(int howto, char *what)
359 {
360 	static int syncing = 0;
361 
362 	boothowto = howto;
363 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
364 		syncing = 1;
365 		vfs_shutdown();
366 		resettodr();
367 	}
368 
369 	splhigh();
370 
371 	if (!cold && (howto & RB_DUMP))
372 		/*XXX dumpsys()*/;
373 
374 	doshutdownhooks();
375 
376 	pmf_system_shutdown(boothowto);
377 
378 	if (howto & RB_HALT) {
379 		printf("halted\n\n");
380 
381 		while (1)
382 			;
383 	}
384 
385 	printf("rebooting\n\n");
386 
387 	/* flush cache for msgbuf */
388 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
389 
390 	ppc4xx_reset();
391 
392 #ifdef DDB
393 	while (1)
394 		Debugger();
395 #else
396 	while (1)
397 		;
398 #endif
399 }
400 
401 void
402 mem_regions(struct mem_region **mem, struct mem_region **avail)
403 {
404 	*mem = phys_mem;
405 	*avail = avail_mem;
406 }
407