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