machdep.c revision 1.9 1 /* $NetBSD: machdep.c,v 1.9 2010/12/11 17:12:44 matt Exp $ */
2
3 /*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at Sandburst Corp.
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 /*
33 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
34 * Copyright (C) 1995, 1996 TooLs GmbH.
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by TooLs GmbH.
48 * 4. The name of TooLs GmbH may not be used to endorse or promote products
49 * derived from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
57 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
58 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
59 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
60 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.9 2010/12/11 17:12:44 matt Exp $");
65
66 #include "opt_compat_netbsd.h"
67 #include "opt_ddb.h"
68 #include "opt_ddbparam.h"
69 #include "opt_inet.h"
70 #include "opt_ccitt.h"
71 #include "opt_iso.h"
72 #include "opt_ns.h"
73 #include "opt_ipkdb.h"
74
75 #include <sys/param.h>
76 #include <sys/buf.h>
77 #include <sys/conf.h>
78 #include <sys/device.h>
79 #include <sys/exec.h>
80 #include <sys/extent.h>
81 #include <sys/kernel.h>
82 #include <sys/kgdb.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/mount.h>
86 #include <sys/msgbuf.h>
87 #include <sys/proc.h>
88 #include <sys/reboot.h>
89 #include <sys/syscallargs.h>
90 #include <sys/syslog.h>
91 #include <sys/sysctl.h>
92 #include <sys/systm.h>
93 #include <sys/ksyms.h>
94
95 #include <uvm/uvm.h>
96 #include <uvm/uvm_extern.h>
97
98 #include <net/netisr.h>
99
100 #include <machine/bus.h>
101 #include <machine/db_machdep.h>
102 #include <machine/intr.h>
103 #include <machine/pio.h>
104 #include <machine/pmap.h>
105 #include <machine/powerpc.h>
106 #include <machine/trap.h>
107 #include <machine/pmppc.h>
108
109 #include <powerpc/oea/bat.h>
110 #include <arch/powerpc/pic/picvar.h>
111
112 #include <ddb/db_extern.h>
113
114 #include <dev/cons.h>
115
116 #include <dev/ic/cpc700reg.h>
117 #include <dev/ic/cpc700uic.h>
118
119 #include "com.h"
120 #if (NCOM > 0)
121 #include <sys/termios.h>
122 #include <dev/ic/comreg.h>
123 #include <dev/ic/comvar.h>
124 #endif
125
126 #include "ksyms.h"
127
128 struct powerpc_bus_space pmppc_mem_tag = {
129 _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
130 0, 0, 0xffffffff,
131 NULL,
132 };
133 struct powerpc_bus_space pmppc_pci_io_tag = {
134 _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
135 0, CPC_PCI_IO_BASE, 0xffffffff,
136 NULL,
137 };
138
139 static char ex_storage[1][EXTENT_FIXED_STORAGE_SIZE(8)]
140 __attribute__((aligned(8)));
141
142
143 #ifdef KGDB
144 char kgdb_devname[] = KGDB_DEVNAME;
145 int comkgdbaddr = KGDB_DEVADDR;
146 int comkgdbrate = KGDB_DEVRATE;
147
148 #ifndef KGDB_DEVMODE
149 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
150 #endif
151 int comkgdbmode = KGDB_DEVMODE;
152
153 void kgdb_port_init(void);
154 #endif /* KGDB */
155
156 /*
157 * Global variables used here and there
158 */
159 struct mem_region physmemr[2], availmemr[2];
160
161 struct a_config a_config;
162
163 void initppc(u_int, u_int, u_int, void *); /* Called from locore */
164 void pmppc_setup(void);
165 void setleds(int leds);
166
167 void
168 initppc(u_int startkernel, u_int endkernel, u_int args, void *btinfo)
169 {
170 extern void consinit(void);
171 extern u_long ticks_per_sec;
172 extern unsigned char edata[], end[];
173
174 memset(&edata, 0, end - edata); /* clear BSS */
175
176 pmppc_setup();
177
178 physmemr[0].start = 0;
179 physmemr[0].size = a_config.a_mem_size;
180 physmemr[1].size = 0;
181 availmemr[0].start = (endkernel + PGOFSET) & ~PGOFSET;
182 availmemr[0].size = a_config.a_mem_size - availmemr[0].start;
183 availmemr[1].size = 0;
184
185 #ifdef BOOTHOWTO
186 /*
187 * boothowto
188 */
189 boothowto = BOOTHOWTO;
190 #endif
191
192 if (bus_space_init(&pmppc_mem_tag, "iomem",
193 ex_storage[0], sizeof(ex_storage[0])))
194 panic("bus_space_init failed");
195
196 /*
197 * Initialize the BAT registers
198 */
199 oea_batinit(
200 PMPPC_FLASH_BASE, BAT_BL_256M, /* flash (etc) memory 256M area */
201 CPC_PCI_MEM_BASE, BAT_BL_256M, /* PCI memory 256M area */
202 CPC_PCI_IO_BASE, BAT_BL_128M, /* PCI I/O 128M area */
203 0);
204
205 /*
206 * Set up trap vectors
207 */
208 oea_init(NULL);
209
210 /*
211 * Get CPU clock
212 */
213 ticks_per_sec = a_config.a_bus_freq;
214 ticks_per_sec /= 4; /* 4 cycles per DEC tick */
215 cpu_timebase = ticks_per_sec;
216
217 /*
218 * Set up console.
219 */
220 consinit(); /* XXX should not be here */
221
222 printf("console set up\n");
223
224 /*
225 * Set the page size.
226 */
227 uvm_setpagesize();
228
229 /*
230 * Initialize pmap module.
231 */
232 pmap_bootstrap(startkernel, endkernel);
233
234 #ifdef IPKDB
235 /*
236 * Now trap to IPKDB
237 */
238 ipkdb_init();
239 if (boothowto & RB_KDB)
240 ipkdb_connect(0);
241 #endif
242 #ifdef KGDB
243 kgdb_port_init();
244 if (boothowto & RB_KDB) {
245 kgdb_debug_init = 1;
246 kgdb_connect(1);
247 }
248 #endif
249 }
250
251 void
252 mem_regions(struct mem_region **mem, struct mem_region **avail)
253 {
254 *mem = physmemr;
255 *avail = availmemr;
256 }
257
258 /*
259 * Machine dependent startup code.
260 */
261 void
262 cpu_startup(void)
263 {
264
265 oea_startup(NULL);
266
267 /*
268 * Now that we have VM, malloc()s are OK in bus_space.
269 */
270 bus_space_mallocok();
271
272 /* Set up the PCI bus tag. */
273 if (bus_space_init(&pmppc_pci_io_tag, "pcimem", NULL, 0))
274 panic("bus_space_init pci failed");
275
276 /* Set up interrupt controller */
277 cpc700_init_intr(&pmppc_mem_tag, CPC_UIC_BASE,
278 CPC_INTR_MASK(PMPPC_I_ETH_INT), 0);
279
280 pic_init();
281 (void)setup_cpc700();
282 oea_install_extint(pic_ext_intr);
283
284 #if 0
285 /* XXX doesn't seem to be needed anymore */
286 /*
287 * Now allow hardware interrupts.
288 */
289 __asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0"
290 : "=r"(msr) : "K"(PSL_EE));
291 #endif
292 }
293
294 /*
295 * consinit
296 * Initialize system console.
297 */
298 void
299 consinit(void)
300 {
301 static int initted;
302 #if (NCOM > 0)
303 bus_space_tag_t tag;
304 #endif
305
306 if (initted)
307 return;
308 initted = 1;
309
310 #if (NCOM > 0)
311 tag = &pmppc_mem_tag;
312
313 if(comcnattach(tag, CPC_COM0, 9600, CPC_COM_SPEED(a_config.a_bus_freq),
314 COM_TYPE_NORMAL,
315 ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)))
316 panic("can't init serial console");
317 else
318 return;
319 #endif
320
321 panic("console device missing -- serial console not in kernel");
322 /* Of course, this is moot if there is no console... */
323 }
324
325 #ifdef KGDB
326 void
327 kgdb_port_init(void)
328 {
329 #if (NCOM > 0)
330 if(!strcmp(kgdb_devname, "com")) {
331 bus_space_tag_t tag = &pmppc_mem_tag;
332 com_kgdb_attach(tag, comkgdbaddr, comkgdbrate,
333 CPC_COM_SPEED(a_config.a_bus_freq),
334 COM_TYPE_NORMAL, comkgdbmode);
335 }
336 #endif
337 }
338 #endif
339
340 /*
341 * Halt or reboot the machine after syncing/dumping according to howto.
342 */
343 void
344 cpu_reboot(int howto, char *what)
345 {
346 static int syncing;
347 static char str[256];
348 char *ap = str, *ap1 = ap;
349 extern void disable_intr(void);
350
351 boothowto = howto;
352 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
353 syncing = 1;
354 vfs_shutdown(); /* sync */
355 resettodr(); /* set wall clock */
356 }
357 splhigh();
358 if (howto & RB_HALT) {
359 doshutdownhooks();
360 pmf_system_shutdown(boothowto);
361 printf("halted\n\n");
362 while(1);
363 }
364 if (!cold && (howto & RB_DUMP))
365 oea_dumpsys();
366 doshutdownhooks();
367
368 pmf_system_shutdown(boothowto);
369 printf("rebooting\n\n");
370 if (what && *what) {
371 if (strlen(what) > sizeof str - 5)
372 printf("boot string too large, ignored\n");
373 else {
374 strcpy(str, what);
375 ap1 = ap = str + strlen(str);
376 *ap++ = ' ';
377 }
378 }
379 *ap++ = '-';
380 if (howto & RB_SINGLE)
381 *ap++ = 's';
382 if (howto & RB_KDB)
383 *ap++ = 'd';
384 *ap++ = 0;
385 if (ap[-2] == '-')
386 *ap1 = 0;
387
388 disable_intr();
389
390 /* Write the two byte reset sequence to the reset register. */
391 out8(PMPPC_RESET, PMPPC_RESET_SEQ_STEP1);
392 out8(PMPPC_RESET, PMPPC_RESET_SEQ_STEP2);
393
394 while (1);
395 }
396
397 void
398 setleds(int leds)
399 {
400 out8(PMPPC_LEDS, leds);
401 }
402
403 void
404 pmppc_setup(void)
405 {
406 uint config0, config1;
407
408 config0 = in8(PMPPC_CONFIG0);
409 config1 = in8(PMPPC_CONFIG1);
410
411 /* from page 2-8 in the Artesyn User's manual */
412 a_config.a_boot_device = config1 & 0x80 ? A_BOOT_FLASH : A_BOOT_ROM;
413 a_config.a_has_ecc = (config1 & 0x40) != 0;
414 switch (config1 & 0x30) {
415 case 0x00: a_config.a_mem_size = 32 * 1024 * 1024; break;
416 case 0x10: a_config.a_mem_size = 64 * 1024 * 1024; break;
417 case 0x20: a_config.a_mem_size = 128 * 1024 * 1024; break;
418 case 0x30: a_config.a_mem_size = 256 * 1024 * 1024; break;
419 }
420 a_config.a_l2_cache = (config1 >> 2) & 3;
421 switch (config1 & 0x03) {
422 case 0x00: a_config.a_bus_freq = 66666666; break;
423 case 0x01: a_config.a_bus_freq = 83333333; break;
424 case 0x02: a_config.a_bus_freq = 100000000; break;
425 case 0x03: a_config.a_bus_freq = 0; break; /* XXX */
426 }
427 a_config.a_is_monarch = (config0 & 0x80) == 0;
428 a_config.a_has_eth = (config0 & 0x20) != 0;
429 a_config.a_has_rtc = (config0 & 0x10) == 0;
430 switch (config0 & 0x0c) {
431 case 0x00: a_config.a_flash_size = 256 * 1024 * 1024; break;
432 case 0x04: a_config.a_flash_size = 128 * 1024 * 1024; break;
433 case 0x08: a_config.a_flash_size = 64 * 1024 * 1024; break;
434 case 0x0c: a_config.a_flash_size = 32 * 1024 * 1024; break;
435 }
436 switch (config0 & 0x03) {
437 case 0x00: a_config.a_flash_width = 64; break;
438 case 0x01: a_config.a_flash_width = 32; break;
439 case 0x02: a_config.a_flash_width = 16; break;
440 case 0x03: a_config.a_flash_width = 0; break;
441 }
442 }
443