1/*	$NetBSD: machdep.c,v 1.15 2011/06/20 07:18:06 matt Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.15 2011/06/20 07:18:06 matt Exp $");
36
37#include "opt_compat_netbsd.h"
38
39#include <sys/param.h>
40#include <sys/buf.h>
41#include <sys/bus.h>
42#include <sys/conf.h>
43#include <sys/device.h>
44#include <sys/exec.h>
45#include <sys/extent.h>
46#include <sys/intr.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/mbuf.h>
50#include <sys/mount.h>
51#include <sys/msgbuf.h>
52#include <sys/proc.h>
53#include <sys/reboot.h>
54#include <sys/syscallargs.h>
55#include <sys/sysctl.h>
56#include <sys/syslog.h>
57#include <sys/systm.h>
58
59#include <uvm/uvm_extern.h>
60
61#include <machine/autoconf.h>
62#include <machine/powerpc.h>
63
64#include <powerpc/pmap.h>
65#include <powerpc/trap.h>
66
67#include <powerpc/oea/bat.h>
68#include <powerpc/pic/picvar.h>
69#include <powerpc/include/pio.h>
70
71#include <dev/pci/pcivar.h>
72#include <dev/ic/ibm82660reg.h>
73
74#include <dev/cons.h>
75
76void initppc(u_long, u_long, u_int, void *);
77void dumpsys(void);
78vaddr_t prep_intr_reg;			/* PReP interrupt vector register */
79
80#define	OFMEMREGIONS	32
81struct mem_region physmemr[OFMEMREGIONS], availmemr[OFMEMREGIONS];
82
83paddr_t avail_end;			/* XXX temporary */
84struct pic_ops *isa_pic;
85int isa_pcmciamask = 0x8b28;
86
87void
88initppc(u_long startkernel, u_long endkernel, u_int args, void *btinfo)
89{
90
91	uint32_t sa, ea, banks;
92	u_long memsize = 0;
93	pcitag_t tag;
94
95	/*
96	 * Set memory region by reading the memory size from the PCI
97	 * host bridge.
98	 */
99
100	tag = genppc_pci_indirect_make_tag(NULL, 0, 0, 0);
101
102	out32rb(PCI_MODE1_ADDRESS_REG, tag | IBM_82660_MEM_BANK0_START);
103	sa = in32rb(PCI_MODE1_DATA_REG);
104
105	out32rb(PCI_MODE1_ADDRESS_REG, tag | IBM_82660_MEM_BANK0_END);
106	ea = in32rb(PCI_MODE1_DATA_REG);
107
108	/* Which memory banks are enabled? */
109	out32rb(PCI_MODE1_ADDRESS_REG, tag | IBM_82660_MEM_BANK_ENABLE);
110	banks = in32rb(PCI_MODE1_DATA_REG) & 0xFF;
111
112	/* Reset the register for the next call. */
113	out32rb(PCI_MODE1_ADDRESS_REG, 0);
114
115	if (banks & IBM_82660_MEM_BANK0_ENABLED)
116		memsize += IBM_82660_BANK0_ADDR(ea) - IBM_82660_BANK0_ADDR(sa) + 1;
117
118	if (banks & IBM_82660_MEM_BANK1_ENABLED)
119		memsize += IBM_82660_BANK1_ADDR(ea) - IBM_82660_BANK1_ADDR(sa) + 1;
120
121	if (banks & IBM_82660_MEM_BANK2_ENABLED)
122		memsize += IBM_82660_BANK2_ADDR(ea) - IBM_82660_BANK2_ADDR(sa) + 1;
123
124	if (banks & IBM_82660_MEM_BANK3_ENABLED)
125		memsize += IBM_82660_BANK3_ADDR(ea) - IBM_82660_BANK3_ADDR(sa) + 1;
126
127	memsize <<= 20;
128
129	physmemr[0].start = 0;
130	physmemr[0].size = memsize & ~PGOFSET;
131	availmemr[0].start = (endkernel + PGOFSET) & ~PGOFSET;
132	availmemr[0].size = memsize - availmemr[0].start;
133
134	avail_end = physmemr[0].start + physmemr[0].size;    /* XXX temporary */
135
136	/*
137	 * Set CPU clock
138	 */
139	{
140		extern u_long ticks_per_sec, ns_per_tick;
141
142		ticks_per_sec = 16666666;		/* hardcoded */
143		ns_per_tick = 1000000000 / ticks_per_sec;
144	}
145
146	/*
147	 * boothowto
148	 */
149	boothowto = 0;		/* XXX - should make this an option */
150
151	prep_initppc(startkernel, endkernel, args);
152}
153
154/*
155 * Machine dependent startup code.
156 */
157void
158cpu_startup(void)
159{
160	/*
161	 * Mapping PReP interrput vector register.
162	 */
163	prep_intr_reg = (vaddr_t) mapiodev(PREP_INTR_REG, PAGE_SIZE, false);
164	if (!prep_intr_reg)
165		panic("startup: no room for interrupt register");
166	prep_intr_reg_off = INTR_VECTOR_REG;
167
168	/*
169	 * Do common startup.
170	 */
171	oea_startup("IBM NetworkStation 1000 (8362-XXX)");
172
173	pic_init();
174	isa_pic = setup_prepivr(PIC_IVR_IBM);
175
176        oea_install_extint(pic_ext_intr);
177
178	/*
179	 * Now allow hardware interrupts.
180	 */
181	{
182		int msr;
183
184		splraise(-1);
185		__asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0"
186			      : "=r"(msr) : "K"(PSL_EE));
187	}
188
189	/*
190	 * Now safe for bus space allocation to use malloc.
191	 */
192	bus_space_mallocok();
193}
194
195/*
196 * Halt or reboot the machine after syncing/dumping according to howto.
197 */
198void
199cpu_reboot(int howto, char *what)
200{
201	static int syncing;
202
203	if (cold) {
204		howto |= RB_HALT;
205		goto halt_sys;
206	}
207
208	boothowto = howto;
209	if ((howto & RB_NOSYNC) == 0 && syncing == 0) {
210		syncing = 1;
211		vfs_shutdown();		/* sync */
212		resettodr();		/* set wall clock */
213	}
214
215	/* Disable intr */
216	splhigh();
217
218	/* Do dump if requested */
219	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
220		oea_dumpsys();
221
222halt_sys:
223	doshutdownhooks();
224
225	pmf_system_shutdown(boothowto);
226
227	if (howto & RB_HALT) {
228                aprint_normal("\n");
229                aprint_normal("The operating system has halted.\n");
230                aprint_normal("Please press any key to reboot.\n\n");
231                cnpollc(1);	/* for proper keyboard command handling */
232                cngetc();
233                cnpollc(0);
234	}
235
236	aprint_normal("rebooting...\n\n");
237
238
239        {
240	    int msr;
241	    u_char reg;
242
243	    __asm volatile("mfmsr %0" : "=r"(msr));
244	    msr |= PSL_IP;
245	    __asm volatile("mtmsr %0" :: "r"(msr));
246
247	    reg = *(volatile u_char *)(PREP_BUS_SPACE_IO + 0x92);
248	    reg &= ~1UL;
249	    *(volatile u_char *)(PREP_BUS_SPACE_IO + 0x92) = reg;
250
251	    __asm volatile("sync; eieio\n");
252
253	    reg = *(volatile u_char *)(PREP_BUS_SPACE_IO + 0x92);
254	    reg |= 1;
255	    *(volatile u_char *)(PREP_BUS_SPACE_IO + 0x92) = reg;
256
257	    __asm volatile("sync; eieio\n");
258	}
259
260	for (;;)
261		continue;
262	/* NOTREACHED */
263}
264