xref: /netbsd-src/sys/arch/evbppc/walnut/machdep.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: machdep.c,v 1.47 2011/01/14 02:06:25 rmind 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.47 2011/01/14 02:06:25 rmind Exp $");
71 
72 #include "opt_compat_netbsd.h"
73 #include "opt_ddb.h"
74 #include "opt_ipkdb.h"
75 #include "opt_modular.h"
76 
77 #include <sys/param.h>
78 #include <sys/buf.h>
79 #include <sys/exec.h>
80 #include <sys/malloc.h>
81 #include <sys/mbuf.h>
82 #include <sys/mount.h>
83 #include <sys/msgbuf.h>
84 #include <sys/proc.h>
85 #include <sys/reboot.h>
86 #include <sys/syscallargs.h>
87 #include <sys/syslog.h>
88 #include <sys/systm.h>
89 #include <sys/kernel.h>
90 #include <sys/boot_flag.h>
91 #include <sys/ksyms.h>
92 #include <sys/device.h>
93 
94 #include <uvm/uvm_extern.h>
95 
96 #include <net/netisr.h>
97 
98 #include <prop/proplib.h>
99 
100 #include <machine/bus.h>
101 #include <machine/powerpc.h>
102 #include <machine/trap.h>
103 #include <machine/walnut.h>
104 #include <machine/pcb.h>
105 
106 #include <powerpc/spr.h>
107 #include <powerpc/ibm4xx/spr.h>
108 #include <powerpc/ibm4xx/dcr4xx.h>
109 
110 #include <dev/cons.h>
111 #include <dev/pci/pcivar.h>
112 #include <dev/pci/pciconf.h>
113 
114 #include "ksyms.h"
115 
116 #if defined(DDB)
117 #include <machine/db_machdep.h>
118 #include <ddb/db_extern.h>
119 #endif
120 
121 
122 #define TLB_PG_SIZE 	(16*1024*1024)
123 
124 /*
125  * Global variables used here and there
126  */
127 struct vm_map *phys_map = NULL;
128 
129 /*
130  * This should probably be in autoconf!				XXX
131  */
132 char cpu_model[80];
133 char machine[] = MACHINE;		/* from <machine/param.h> */
134 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
135 
136 char bootpath[256];
137 paddr_t msgbuf_paddr;
138 vaddr_t msgbuf_vaddr;
139 
140 #if NKSYMS || defined(DDB) || defined(MODULAR)
141 void *startsym, *endsym;
142 #endif
143 
144 int lcsplx(int);
145 void initppc(u_int, u_int, char *, void *);
146 
147 static void dumpsys(void);
148 static void install_extint(void (*)(void));
149 
150 #define MEMREGIONS	8
151 struct mem_region physmemr[MEMREGIONS];		/* Hard code memory */
152 struct mem_region availmemr[MEMREGIONS];	/* Who's supposed to set these up? */
153 
154 struct board_cfg_data board_data;
155 
156 void
157 initppc(u_int startkernel, u_int endkernel, char *args, void *info_block)
158 {
159 	extern int defaulttrap, defaultsize;
160 	extern int sctrap, scsize;
161 	extern int alitrap, alisize;
162 	extern int dsitrap, dsisize;
163 	extern int isitrap, isisize;
164 	extern int mchktrap, mchksize;
165 	extern int tlbimiss4xx, tlbim4size;
166 	extern int tlbdmiss4xx, tlbdm4size;
167 	extern int pitfitwdog, pitfitwdogsize;
168 	extern int debugtrap, debugsize;
169 	extern int errata51handler, errata51size;
170 #ifdef DDB
171 	extern int ddblow, ddbsize;
172 #endif
173 #ifdef IPKDB
174 	extern int ipkdblow, ipkdbsize;
175 #endif
176 	vaddr_t va;
177 	int exc, dbcr0;
178 	struct cpu_info * const ci = curcpu();
179 
180 	/* Disable all external interrupts */
181 	mtdcr(DCR_UIC0_BASE + DCR_UIC_ER, 0);
182 
183         /* Initialize cache info for memcpy, etc. */
184         cpu_probe_cache();
185 
186 	/* Save info block */
187 	memcpy(&board_data, info_block, sizeof(board_data));
188 
189 	memset(physmemr, 0, sizeof physmemr);
190 	memset(availmemr, 0, sizeof availmemr);
191 	physmemr[0].start = 0;
192 	physmemr[0].size = board_data.mem_size & ~PGOFSET;
193 	/* Lower memory reserved by eval board BIOS */
194 	availmemr[0].start = startkernel;
195 	availmemr[0].size = board_data.mem_size - availmemr[0].start;
196 
197 	/* Linear map kernel memory */
198 	for (va = 0; va < endkernel; va += TLB_PG_SIZE)
199 		ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
200 
201 	/* Map console after physmem (see pmap_tlbmiss()) */
202 	ppc4xx_tlb_reserve(0xef000000, roundup(physmemr[0].size, TLB_PG_SIZE),
203 	    TLB_PG_SIZE, TLB_I | TLB_G);
204 
205 	/*
206 	 * Initialize lwp0 and current pcb and pmap pointers.
207 	 */
208 	lwp0.l_cpu = ci;
209 
210 	curpcb = lwp_getpcb(&lwp0);
211 	memset(curpcb, 0, sizeof(struct pcb));
212 	curpcb->pcb_pm = pmap_kernel();
213 
214 	/*
215 	 * Set up trap vectors
216 	 */
217 	for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
218 		switch (exc) {
219 		default:
220 			memcpy((void *)exc, &defaulttrap, (size_t)&defaultsize);
221 			break;
222 		case EXC_EXI:
223 			/*
224 			 * This one is (potentially) installed during autoconf
225 			 */
226 			break;
227 		case EXC_SC:
228 			memcpy((void *)EXC_SC, &sctrap, (size_t)&scsize);
229 			break;
230 		case EXC_ALI:
231 			memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
232 			break;
233 		case EXC_DSI:
234 			memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
235 			break;
236 		case EXC_ISI:
237 			memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
238 			break;
239 		case EXC_MCHK:
240 			memcpy((void *)EXC_MCHK, &mchktrap, (size_t)&mchksize);
241 			break;
242 		case EXC_ITMISS:
243 			memcpy((void *)EXC_ITMISS, &tlbimiss4xx,
244 				(size_t)&tlbim4size);
245 			break;
246 		case EXC_DTMISS:
247 			memcpy((void *)EXC_DTMISS, &tlbdmiss4xx,
248 				(size_t)&tlbdm4size);
249 			break;
250 		/*
251 		 * EXC_PIT, EXC_FIT, EXC_WDOG handlers
252 		 * are spaced by 0x10 bytes only..
253 		 */
254 		case EXC_PIT:
255 			memcpy((void *)EXC_PIT, &pitfitwdog,
256 				(size_t)&pitfitwdogsize);
257 			break;
258 		case EXC_DEBUG:
259 			memcpy((void *)EXC_DEBUG, &debugtrap,
260 				(size_t)&debugsize);
261 			break;
262 		case EXC_DTMISS|EXC_ALI:
263                         /* PPC405GP Rev D errata item 51 */
264 			memcpy((void *)(EXC_DTMISS|EXC_ALI), &errata51handler,
265 				(size_t)&errata51size);
266 			break;
267 #if defined(DDB) || defined(IPKDB)
268 		case EXC_PGM:
269 #if defined(DDB)
270 			memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
271 #elif defined(IPKDB)
272 			memcpy((void *)exc, &ipkdblow, (size_t)&ipkdbsize);
273 #endif
274 #endif /* DDB | IPKDB */
275 			break;
276 		}
277 
278 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
279 	mtspr(SPR_EVPR, 0);		/* Set Exception vector base */
280 
281 	consinit();
282 
283 	/* Handle trap instruction as PGM exception */
284 	__asm volatile("mfspr %0,%1":"=r"(dbcr0):"K"(SPR_DBCR0));
285 	__asm volatile("mtspr %0,%1"::"K"(SPR_DBCR0),"r"(dbcr0 & ~DBCR0_TDE));
286 
287 	/*
288 	 * external interrupt handler install
289 	 */
290 	install_extint(ext_intr);
291 
292 	/*
293 	 * Now enable translation (and machine checks/recoverable interrupts).
294 	 */
295 	__asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
296 		      : : "r"(0), "K"(PSL_IR|PSL_DR));
297 	/* XXXX PSL_ME - With ME set kernel gets stuck... */
298 
299 	uvm_setpagesize();
300 
301 	/*
302 	 * Initialize pmap module.
303 	 */
304 	pmap_bootstrap(startkernel, endkernel);
305 
306 #ifdef DEBUG
307 	printf("Board config data:\n");
308 	printf("  usr_config_ver = %s\n", board_data.usr_config_ver);
309 	printf("  rom_sw_ver = %s\n", board_data.rom_sw_ver);
310 	printf("  mem_size = %u\n", board_data.mem_size);
311 	printf("  mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
312 	    board_data.mac_address_local[0], board_data.mac_address_local[1],
313 	    board_data.mac_address_local[2], board_data.mac_address_local[3],
314 	    board_data.mac_address_local[4], board_data.mac_address_local[5]);
315 	printf("  mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
316 	    board_data.mac_address_pci[0], board_data.mac_address_pci[1],
317 	    board_data.mac_address_pci[2], board_data.mac_address_pci[3],
318 	    board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
319 	printf("  processor_speed = %u\n", board_data.processor_speed);
320 	printf("  plb_speed = %u\n", board_data.plb_speed);
321 	printf("  pci_speed = %u\n", board_data.pci_speed);
322 #endif
323 
324 #if NKSYMS || defined(DDB) || defined(MODULAR)
325 	ksyms_addsyms_elf((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
326 #endif
327 #ifdef DDB
328 	if (boothowto & RB_KDB)
329 		Debugger();
330 #endif
331 #ifdef IPKDB
332 	/*
333 	 * Now trap to IPKDB
334 	 */
335 	ipkdb_init();
336 	if (boothowto & RB_KDB)
337 		ipkdb_connect(0);
338 #endif
339 }
340 
341 static void
342 install_extint(void (*handler)(void))
343 {
344 	extern int extint, extsize;
345 	extern u_long extint_call;
346 	u_long offset = (u_long)handler - (u_long)&extint_call;
347 	int msr;
348 
349 #ifdef	DIAGNOSTIC
350 	if (offset > 0x1ffffff)
351 		panic("install_extint: too far away");
352 #endif
353 	__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
354 	extint_call = (extint_call & 0xfc000003) | offset;
355 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
356 	__syncicache((void *)&extint_call, sizeof extint_call);
357 	__syncicache((void *)EXC_EXI, (int)&extsize);
358 	__asm volatile ("mtmsr %0" :: "r"(msr));
359 }
360 
361 /*
362  * Machine dependent startup code.
363  */
364 
365 char msgbuf[MSGBUFSIZE];
366 
367 void
368 cpu_startup(void)
369 {
370 	vaddr_t minaddr, maxaddr;
371 	prop_number_t pn;
372 	prop_data_t pd;
373 	char pbuf[9];
374 
375 	/*
376 	 * Initialize error message buffer (at end of core).
377 	 */
378 #if 0	/* For some reason this fails... --Artem
379 	 * Besides, do we really have to put it at the end of core?
380 	 * Let's use static buffer for now
381 	 */
382 	if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0,
383 	    UVM_KMF_VAONLY)))
384 		panic("startup: no room for message buffer");
385 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
386 		pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
387 		    msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0);
388 	initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
389 #else
390 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
391 #endif
392 
393 	printf("%s%s", copyright, version);
394 	printf("Walnut PowerPC 405GP Evaluation Board\n");
395 
396 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
397 	printf("total memory = %s\n", pbuf);
398 
399 	minaddr = 0;
400 	/*
401 	 * Allocate a submap for physio
402 	 */
403 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
404 				 VM_PHYS_SIZE, 0, false, NULL);
405 
406 	/*
407 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
408 	 * are allocated via the pool allocator, and we use direct-mapped
409 	 * pool pages.
410 	 */
411 
412 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
413 	printf("avail memory = %s\n", pbuf);
414 
415 	/*
416 	 * Set up the board properties dictionary.
417 	 */
418 	board_properties = prop_dictionary_create();
419 	KASSERT(board_properties != NULL);
420 
421 	pn = prop_number_create_integer(board_data.mem_size);
422 	KASSERT(pn != NULL);
423 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
424 		panic("setting mem-size");
425 	prop_object_release(pn);
426 
427 	pd = prop_data_create_data_nocopy(board_data.mac_address_local,
428 					  sizeof(board_data.mac_address_local));
429 	KASSERT(pd != NULL);
430 	if (prop_dictionary_set(board_properties, "emac0-mac-addr",
431 				pd) == false)
432 		panic("setting emac0-mac-addr");
433 	prop_object_release(pd);
434 
435 	pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
436 					  sizeof(board_data.mac_address_pci));
437 	KASSERT(pd != NULL);
438 	if (prop_dictionary_set(board_properties, "sip0-mac-addr",
439 				pd) == false)
440 		panic("setting sip0-mac-addr");
441 	prop_object_release(pd);
442 
443 	pn = prop_number_create_integer(board_data.processor_speed);
444 	KASSERT(pn != NULL);
445 	if (prop_dictionary_set(board_properties, "processor-frequency",
446 				pn) == false)
447 		panic("setting processor-frequency");
448 	prop_object_release(pn);
449 
450 	/*
451 	 * Now that we have VM, malloc()s are OK in bus_space.
452 	 */
453 	bus_space_mallocok();
454 	fake_mapiodev = 0;
455 }
456 
457 
458 static void
459 dumpsys(void)
460 {
461 
462 	printf("dumpsys: TBD\n");
463 }
464 
465 /*
466  * Halt or reboot the machine after syncing/dumping according to howto.
467  */
468 void
469 cpu_reboot(int howto, char *what)
470 {
471 	static int syncing;
472 	static char str[256];
473 	char *ap = str, *ap1 = ap;
474 
475 	boothowto = howto;
476 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
477 		syncing = 1;
478 		vfs_shutdown();		/* sync */
479 		resettodr();		/* set wall clock */
480 	}
481 
482 	splhigh();
483 
484 	if (!cold && (howto & RB_DUMP))
485 		dumpsys();
486 
487 	doshutdownhooks();
488 
489 	pmf_system_shutdown(boothowto);
490 
491 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
492 	  /* Power off here if we know how...*/
493 	}
494 
495 	if (howto & RB_HALT) {
496 		printf("halted\n\n");
497 
498 		goto reboot;	/* XXX for now... */
499 
500 #ifdef DDB
501 		printf("dropping to debugger\n");
502 		while(1)
503 			Debugger();
504 #endif
505 	}
506 
507 	printf("rebooting\n\n");
508 	if (what && *what) {
509 		if (strlen(what) > sizeof str - 5)
510 			printf("boot string too large, ignored\n");
511 		else {
512 			strcpy(str, what);
513 			ap1 = ap = str + strlen(str);
514 			*ap++ = ' ';
515 		}
516 	}
517 	*ap++ = '-';
518 	if (howto & RB_SINGLE)
519 		*ap++ = 's';
520 	if (howto & RB_KDB)
521 		*ap++ = 'd';
522 	*ap++ = 0;
523 	if (ap[-2] == '-')
524 		*ap1 = 0;
525 
526 	/* flush cache for msgbuf */
527 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
528 
529  reboot:
530 	ppc4xx_reset();
531 
532 	printf("ppc4xx_reset() failed!\n");
533 #ifdef DDB
534 	while(1)
535 		Debugger();
536 #else
537 	while (1)
538 		/* nothing */;
539 #endif
540 }
541 
542 int
543 lcsplx(int ipl)
544 {
545 
546 	return spllower(ipl); 	/* XXX */
547 }
548 
549 void
550 mem_regions(struct mem_region **mem, struct mem_region **avail)
551 {
552 
553 	*mem = physmemr;
554 	*avail = availmemr;
555 }
556 
557 
558 int
559 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
560 {
561 
562 	/*
563 	 * Bus number is irrelevant.  Configuration Mechanism 1 is in
564 	 * use, can have devices 0-32 (i.e. the `normal' range).
565 	 */
566 	return 5;
567 }
568 
569 int
570 pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
571 {
572 	int pin = pa->pa_intrpin;
573 	int dev = pa->pa_device;
574 
575 	if (pin == 0)
576 		/* No IRQ used. */
577 		goto bad;
578 
579 	if (pin > 4) {
580 		printf("pci_intr_map: bad interrupt pin %d\n", pin);
581 		goto bad;
582 	}
583 
584 	/*
585 	 * We need to map the interrupt pin to the interrupt bit in the UIC
586 	 * associated with it.  This is highly machine-dependent.
587 	 */
588 	switch(dev) {
589 	case 1:
590 	case 2:
591 	case 3:
592 	case 4:
593 		*ihp = 27 + dev;
594 		break;
595 	default:
596 		printf("Hmm.. PCI device %d should not exist on this board\n",
597 			dev);
598 		goto bad;
599 	}
600 	return 0;
601 
602 bad:
603 	*ihp = -1;
604 	return 1;
605 }
606 
607 void
608 pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
609 		   int swiz, int *iline)
610 {
611 
612 	if (bus == 0)
613 		switch(dev) {
614 		case 1:
615 		case 2:
616 		case 3:
617 		case 4:
618 			*iline = 31 - dev;
619 		}
620 	else
621 		*iline = 20 + ((swiz + dev + 1) & 3);
622 }
623