machdep.c revision 1.16 1 /* $NetBSD: machdep.c,v 1.16 2004/02/13 11:36:12 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department, The Mach Operating System project at
10 * Carnegie-Mellon University and Ralph Campbell.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
37 * from: Utah Hdr: machdep.c 1.63 91/04/24
38 */
39 /*
40 * Copyright (c) 1988 University of Utah.
41 *
42 * This code is derived from software contributed to Berkeley by
43 * the Systems Programming Group of the University of Utah Computer
44 * Science Department, The Mach Operating System project at
45 * Carnegie-Mellon University and Ralph Campbell.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
76 * from: Utah Hdr: machdep.c 1.63 91/04/24
77 */
78
79 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
80 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.16 2004/02/13 11:36:12 wiz Exp $");
81
82 #include "opt_ddb.h"
83 #include "opt_kgdb.h"
84
85 #include "opt_memsize.h"
86 #include "opt_ethaddr.h"
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/kernel.h>
91 #include <sys/buf.h>
92 #include <sys/reboot.h>
93 #include <sys/user.h>
94 #include <sys/mount.h>
95 #include <sys/kcore.h>
96 #include <sys/boot_flag.h>
97 #include <sys/termios.h>
98 #include <sys/ksyms.h>
99
100 #include <net/if.h>
101 #include <net/if_ether.h>
102
103 #include <uvm/uvm_extern.h>
104
105 #include <dev/cons.h>
106
107 #include "ksyms.h"
108
109 #if NKSYMS || defined(DDB) || defined(LKM)
110 #include <machine/db_machdep.h>
111 #include <ddb/db_extern.h>
112 #endif
113
114 #include <mips/cache.h>
115 #include <mips/locore.h>
116 #include <machine/yamon.h>
117
118 #include <evbmips/alchemy/pb1000var.h>
119 #include <mips/alchemy/include/aureg.h>
120 #include <mips/alchemy/include/auvar.h>
121 #include <mips/alchemy/include/aubusvar.h>
122
123 #include "aucom.h"
124 #if NAUCOM > 0
125 #include <mips/alchemy/dev/aucomvar.h>
126
127 #ifndef CONSPEED
128 #define CONSPEED TTYDEF_SPEED
129 #endif
130 int aucomcnrate = CONSPEED;
131 #endif /* NAUCOM > 0 */
132
133 #include "ohci.h"
134
135 /* The following are used externally (sysctl_hw). */
136 extern char cpu_model[];
137
138 struct user *proc0paddr;
139
140 /* Our exported CPU info; we can have only one. */
141 struct cpu_info cpu_info_store;
142
143 /* Maps for VM objects. */
144 struct vm_map *exec_map = NULL;
145 struct vm_map *mb_map = NULL;
146 struct vm_map *phys_map = NULL;
147
148 int physmem; /* # pages of physical memory */
149 int maxmem; /* max memory per process */
150
151 int mem_cluster_cnt;
152 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
153
154 yamon_env_var *yamon_envp;
155 struct pb1000_config pb1000_configuration;
156
157 void mach_init(int, char **, yamon_env_var *, u_long); /* XXX */
158
159 void
160 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
161 {
162 struct pb1000_config *pbc = &pb1000_configuration;
163 bus_space_handle_t sh;
164 caddr_t kernend;
165 const char *cp;
166 u_long first, last;
167 caddr_t v;
168 int freqok, howto, i;
169
170 extern char edata[], end[]; /* XXX */
171
172 /* clear the BSS segment */
173 kernend = (caddr_t)mips_round_page(end);
174 memset(edata, 0, kernend - (caddr_t)edata);
175
176 /* set CPU model info for sysctl_hw */
177 strcpy(cpu_model, "Alchemy Semiconductor Pb1000");
178
179 /* save the yamon environment pointer */
180 yamon_envp = envp;
181
182 /* Use YAMON callbacks for early console I/O */
183 cn_tab = &yamon_promcd;
184
185 /*
186 * Set up the exception vectors and CPU-specific function
187 * vectors early on. We need the wbflush() vector set up
188 * before comcnattach() is called (or at least before the
189 * first printf() after that is called).
190 * Sets up mips_cpu_flags that may be queried by other
191 * functions called during startup.
192 * Also clears the I+D caches.
193 */
194 mips_vector_init();
195
196 /*
197 * Set the VM page size.
198 */
199 uvm_setpagesize();
200
201 /*
202 * Use YAMON's CPU frequency if available.
203 */
204 freqok = yamon_setcpufreq(1);
205
206 /*
207 * Initialize bus space tags.
208 */
209 au_cpureg_bus_mem_init(&pbc->pc_cpuregt, pbc);
210 aubus_st = &pbc->pc_cpuregt; /* XXX: for aubus.c */
211
212 /*
213 * Calibrate the timer if YAMON failed to tell us.
214 */
215 if (!freqok) {
216 bus_space_map(&pbc->pc_cpuregt, PC_BASE, PC_SIZE, 0, &sh);
217 au_cal_timers(&pbc->pc_cpuregt, sh);
218 bus_space_unmap(&pbc->pc_cpuregt, sh, PC_SIZE);
219 }
220
221 /*
222 * Bring up the console.
223 */
224 #if NAUCOM > 0
225 /*
226 * Delay to allow firmware putchars to complete.
227 * FIFO depth * character time.
228 * character time = (1000000 / (defaultrate / 10))
229 */
230 delay(160000000 / aucomcnrate);
231 if (aucomcnattach(&pbc->pc_cpuregt, UART0_BASE, aucomcnrate,
232 curcpu()->ci_cpu_freq / 4, COM_TYPE_AU1x00,
233 (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
234 panic("pb1000: unable to initialize serial console");
235 #else
236 panic("pb1000: not configured to use serial console");
237 #endif /* NAUCOM > 0 */
238
239 /*
240 * Look at arguments passed to us and compute boothowto.
241 */
242 boothowto = RB_AUTOBOOT;
243 #ifdef KADB
244 boothowto |= RB_KDB;
245 #endif
246 for (i = 1; i < argc; i++) {
247 for (cp = argv[i]; *cp; cp++) {
248 /* Ignore superfluous '-', if there is one */
249 if (*cp == '-')
250 continue;
251
252 howto = 0;
253 BOOT_FLAG(*cp, howto);
254 if (! howto)
255 printf("bootflag '%c' not recognised\n", *cp);
256 else
257 boothowto |= howto;
258 }
259 }
260
261 /*
262 * Determine the memory size. Use the `memsize' PMON
263 * variable. If that's not available, panic.
264 *
265 * Note: Reserve the first page! That's where the trap
266 * vectors are located.
267 */
268
269 #if defined(MEMSIZE)
270 memsize = MEMSIZE;
271 #else
272 if (memsize == 0) {
273 if ((cp = yamon_getenv("memsize")) != NULL)
274 memsize = strtoul(cp, NULL, 0);
275 else {
276 printf("FATAL: `memsize' YAMON variable not set. Set it to\n");
277 printf(" the amount of memory (in MB) and try again.\n");
278 printf(" Or, build a kernel with the `MEMSIZE' "
279 "option.\n");
280 panic("pb1000_init");
281 }
282 }
283 #endif /* MEMSIZE */
284 printf("Memory size: 0x%08lx\n", memsize);
285 physmem = btoc(memsize);
286
287 mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
288 mem_clusters[mem_cluster_cnt].size =
289 memsize - mem_clusters[mem_cluster_cnt].start;
290 mem_cluster_cnt++;
291
292 /*
293 * Load the rest of the available pages into the VM system.
294 */
295 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
296 last = mem_clusters[0].start + mem_clusters[0].size;
297 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
298 VM_FREELIST_DEFAULT);
299
300 /*
301 * Initialize message buffer (at end of core).
302 */
303 mips_init_msgbuf();
304
305 /*
306 * Initialize the virtual memory system.
307 */
308 pmap_bootstrap();
309
310 /*
311 * Init mapping for u page(s) for proc0.
312 */
313 v = (caddr_t) uvm_pageboot_alloc(USPACE);
314 lwp0.l_addr = proc0paddr = (struct user *)v;
315 lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
316 curpcb = &lwp0.l_addr->u_pcb;
317 curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
318
319 #if NOHCI > 0
320 {
321 #define USBH_ALL (0x1f<<10) /* All relevant bits in USBH portion of SYS_CLKSRC */
322 /*
323 * Assign a clock for the USB Host controller.
324 */
325 volatile u_int32_t *scsreg;
326 u_int32_t tmp;
327
328 scsreg = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_CLKSRC));
329 #if 0
330 auxpll = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_AUXPLL));
331 *auxpll = 8; /* 96Mhz */
332 tmp = *sfc0;
333 tmp |= SFC_FE0|SFC_FRDIV0;
334 *sfc0 = tmp;
335 #endif
336 tmp = *scsreg;
337 tmp &= ~USBH_ALL; /* clear all USBH bits in SYS_CLKSRC first */
338 tmp |= (SCS_DUH|SCS_CUH|SCS_MUH(SCS_MEx_AUX)); /* 48Mhz */
339 *scsreg = tmp;
340 }
341 #endif /* NOHCI */
342 /*
343 * Initialize debuggers, and break into them, if appropriate.
344 */
345 #if NKSYMS || defined(DDB) || defined(LKM)
346 ksyms_init(0, 0, 0);
347 #endif
348 #ifdef DDB
349 if (boothowto & RB_KDB)
350 Debugger();
351 #endif
352 }
353
354 void
355 consinit(void)
356 {
357
358 /*
359 * Everything related to console initialization is done
360 * in mach_init().
361 */
362 }
363
364 void
365 cpu_startup(void)
366 {
367 char pbuf[9];
368 vaddr_t minaddr, maxaddr;
369 #ifdef DEBUG
370 extern int pmapdebug; /* XXX */
371 int opmapdebug = pmapdebug;
372
373 pmapdebug = 0; /* Shut up pmap debug during bootstrap */
374 #endif
375
376 /*
377 * Good {morning,afternoon,evening,night}.
378 */
379 printf(version);
380 printf("%s\n", cpu_model);
381 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
382 printf("total memory = %s\n", pbuf);
383
384 minaddr = 0;
385 /*
386 * Allocate a submap for exec arguments. This map effectively
387 * limits the number of processes exec'ing at any time.
388 */
389 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
390 16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
391
392 /*
393 * Allocate a submap for physio
394 */
395 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
396 VM_PHYS_SIZE, 0, FALSE, NULL);
397
398 /*
399 * No need to allocate an mbuf cluster submap. Mbuf clusters
400 * are allocated via the pool allocator, and we use KSEG to
401 * map those pages.
402 */
403
404 #ifdef DEBUG
405 pmapdebug = opmapdebug;
406 #endif
407 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
408 printf("avail memory = %s\n", pbuf);
409 }
410
411 void
412 cpu_reboot(int howto, char *bootstr)
413 {
414 static int waittime = -1;
415
416 /* Take a snapshot before clobbering any registers. */
417 if (curproc)
418 savectx((struct user *)curpcb);
419
420 /* If "always halt" was specified as a boot flag, obey. */
421 if (boothowto & RB_HALT)
422 howto |= RB_HALT;
423
424 boothowto = howto;
425
426 /* If system is cold, just halt. */
427 if (cold) {
428 boothowto |= RB_HALT;
429 goto haltsys;
430 }
431
432 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
433 waittime = 0;
434
435 /*
436 * Synchronize the disks....
437 */
438 vfs_shutdown();
439
440 /*
441 * If we've been adjusting the clock, the todr
442 * will be out of synch; adjust it now.
443 */
444 resettodr();
445 }
446
447 /* Disable interrupts. */
448 splhigh();
449
450 if (boothowto & RB_DUMP)
451 dumpsys();
452
453 haltsys:
454 /* Run any shutdown hooks. */
455 doshutdownhooks();
456
457 #if 1
458 /* XXX
459 * For some reason we are leaving the ethernet MAC in a state where
460 * YAMON isn't happy with it. So just call the reset vector (grr,
461 * Alchemy YAMON doesn't have a "reset" command).
462 */
463 printf("reseting board...\n\n");
464 mips_icache_sync_all();
465 mips_dcache_wbinv_all();
466 asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
467 #else
468 printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
469 yamon_exit(boothowto);
470 printf("Oops, back from yamon_exit()\n\nSpinning...");
471 #endif
472 for (;;)
473 /* spin forever */ ; /* XXX */
474 /*NOTREACHED*/
475 }
476