machdep.c revision 1.2.2.2 1 /* $NetBSD: machdep.c,v 1.2.2.2 2006/04/11 12:20:51 yamt Exp $ */
2
3 /*
4 * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 * Copyright (c) 2006 Garrett D'Amore.
6 * All rights reserved.
7 *
8 * Portions of this code were written by Garrett D'Amore for the
9 * Champaign-Urbana Community Wireless Network Project.
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgements:
22 * This product includes software developed by the Urbana-Champaign
23 * Independent Media Center.
24 * This product includes software developed by Garrett D'Amore.
25 * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 * D'Amore's name may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43 /*-
44 * Copyright (c) 2006 Itronix Inc.
45 * All rights reserved.
46 *
47 * Portions written by Garrett D'Amore for Itronix Inc.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. The name of Itronix Inc. may not be used to endorse
58 * or promote products derived from this software without specific
59 * prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
63 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
64 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
65 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
66 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
67 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
68 * ON ANY THEORY OF LIABILITY, WHETHER IN
69 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71 * POSSIBILITY OF SUCH DAMAGE.
72 */
73 /*
74 * Copyright (c) 1992, 1993
75 * The Regents of the University of California. All rights reserved.
76 *
77 * This code is derived from software contributed to Berkeley by
78 * the Systems Programming Group of the University of Utah Computer
79 * Science Department, The Mach Operating System project at
80 * Carnegie-Mellon University and Ralph Campbell.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. Neither the name of the University nor the names of its contributors
91 * may be used to endorse or promote products derived from this software
92 * without specific prior written permission.
93 *
94 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 * SUCH DAMAGE.
105 *
106 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
107 * from: Utah Hdr: machdep.c 1.63 91/04/24
108 */
109 /*
110 * Copyright (c) 1988 University of Utah.
111 *
112 * This code is derived from software contributed to Berkeley by
113 * the Systems Programming Group of the University of Utah Computer
114 * Science Department, The Mach Operating System project at
115 * Carnegie-Mellon University and Ralph Campbell.
116 *
117 * Redistribution and use in source and binary forms, with or without
118 * modification, are permitted provided that the following conditions
119 * are met:
120 * 1. Redistributions of source code must retain the above copyright
121 * notice, this list of conditions and the following disclaimer.
122 * 2. Redistributions in binary form must reproduce the above copyright
123 * notice, this list of conditions and the following disclaimer in the
124 * documentation and/or other materials provided with the distribution.
125 * 3. All advertising materials mentioning features or use of this software
126 * must display the following acknowledgement:
127 * This product includes software developed by the University of
128 * California, Berkeley and its contributors.
129 * 4. Neither the name of the University nor the names of its contributors
130 * may be used to endorse or promote products derived from this software
131 * without specific prior written permission.
132 *
133 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
134 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
135 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
136 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
137 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
138 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
139 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
140 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
141 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
142 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
143 * SUCH DAMAGE.
144 *
145 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
146 * from: Utah Hdr: machdep.c 1.63 91/04/24
147 */
148
149 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
150 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.2.2.2 2006/04/11 12:20:51 yamt Exp $");
151
152 #include "opt_ddb.h"
153 #include "opt_kgdb.h"
154
155 #include "opt_memsize.h"
156 #include "opt_ethaddr.h"
157
158 #include <sys/param.h>
159 #include <sys/systm.h>
160 #include <sys/kernel.h>
161 #include <sys/buf.h>
162 #include <sys/reboot.h>
163 #include <sys/user.h>
164 #include <sys/mount.h>
165 #include <sys/kcore.h>
166 #include <sys/boot_flag.h>
167 #include <sys/termios.h>
168 #include <sys/ksyms.h>
169
170 #include <net/if.h>
171 #include <net/if_ether.h>
172
173 #include <uvm/uvm_extern.h>
174
175 #include <dev/cons.h>
176
177 #include "ksyms.h"
178
179 #if NKSYMS || defined(DDB) || defined(LKM)
180 #include <machine/db_machdep.h>
181 #include <ddb/db_extern.h>
182 #endif
183
184 #include <mips/cache.h>
185 #include <mips/locore.h>
186 #include <mips/cpuregs.h>
187
188 #include <mips/atheros/include/ar531xreg.h>
189 #include <mips/atheros/include/ar531xvar.h>
190 #include <mips/atheros/include/arbusvar.h>
191 #include "com.h"
192
193 /* The following are used externally (sysctl_hw). */
194 extern char cpu_model[];
195
196 struct user *proc0paddr;
197
198 /* Our exported CPU info; we can have only one. */
199 struct cpu_info cpu_info_store;
200
201 /* Maps for VM objects. */
202 struct vm_map *exec_map = NULL;
203 struct vm_map *mb_map = NULL;
204 struct vm_map *phys_map = NULL;
205
206 int physmem; /* # pages of physical memory */
207 int maxmem; /* max memory per process */
208
209 int mem_cluster_cnt;
210 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
211
212 void mach_init(void); /* XXX */
213
214 void mdputc(char);
215 void mdputs(const char *);
216 void mdputx(int n);
217 void mdputn(int n);
218
219 extern struct consdev com_arbus_consdev;
220
221 void
222 mach_init(void)
223 {
224 caddr_t kernend;
225 u_long first, last;
226 caddr_t v;
227 uint32_t memcfg, bank0, bank1;
228 uint32_t memsize;
229 struct ar531x_board_info *infop;
230
231 extern char edata[], end[]; /* XXX */
232
233 /* clear the BSS segment */
234 kernend = (caddr_t)mips_round_page(end);
235
236 memset(edata, 0, kernend - (caddr_t)edata);
237
238 infop = ar531x_board_info();
239 if (infop == NULL)
240 panic("No board info!");
241
242 /* set CPU model info for sysctl_hw */
243 snprintf(cpu_model, 64, "%s", infop->ab_name);
244
245 /*
246 * Set up the exception vectors and CPU-specific function
247 * vectors early on. We need the wbflush() vector set up
248 * before comcnattach() is called (or at least before the
249 * first printf() after that is called).
250 * Sets up mips_cpu_flags that may be queried by other
251 * functions called during startup.
252 * Also clears the I+D caches.
253 */
254 mips_vector_init();
255
256 /*
257 * Calibrate timers.
258 */
259 ar531x_cal_timer();
260
261 /*
262 * Set the VM page size.
263 */
264 uvm_setpagesize();
265
266 /*
267 * Look at arguments passed to us and compute boothowto.
268 */
269 boothowto = RB_AUTOBOOT;
270 #ifdef KADB
271 boothowto |= RB_KDB;
272 #endif
273
274 /*
275 * This would be a good place to parse a boot command line, bif
276 * we got one from the bootloader. Right now we have no way to
277 * get one from e.g. vxworks.
278 */
279
280 /*
281 * Determine the memory size. Use the `memsize' PMON
282 * variable. If that's not available, panic.
283 *
284 * Note: Reserve the first page! That's where the trap
285 * vectors are located.
286 */
287 memcfg = GETSDRAMREG(AR531X_SDRAMCTL_MEM_CFG1);
288 bank0 = (memcfg & AR531X_MEM_CFG1_BANK0_MASK) >>
289 AR531X_MEM_CFG1_BANK0_SHIFT;
290 bank1 = (memcfg & AR531X_MEM_CFG1_BANK1_MASK) >>
291 AR531X_MEM_CFG1_BANK1_SHIFT;
292
293 memsize = (bank0 ? (1 << (bank0 + 1)) : 0) +
294 (bank1 ? (1 << (bank1 + 1)) : 0);
295 memsize <<= 20;
296
297 /* allow compile time override */
298 #if defined(MEMSIZE)
299 memsize = MEMSIZE;
300 #endif
301
302 printf("Memory size: 0x%08x\n", memsize);
303 physmem = btoc(memsize);
304
305 mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
306 mem_clusters[mem_cluster_cnt].size =
307 memsize - mem_clusters[mem_cluster_cnt].start;
308 mem_cluster_cnt++;
309
310 /*
311 * Load the rest of the available pages into the VM system.
312 */
313 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
314 last = mem_clusters[0].start + mem_clusters[0].size;
315 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
316 VM_FREELIST_DEFAULT);
317
318 /*
319 * Initialize message buffer (at end of core).
320 */
321 mips_init_msgbuf();
322
323 /*
324 * Initialize the virtual memory system.
325 */
326 pmap_bootstrap();
327
328 /*
329 * Init mapping for u page(s) for proc0.
330 */
331 v = (caddr_t) uvm_pageboot_alloc(USPACE);
332 lwp0.l_addr = proc0paddr = (struct user *)v;
333 lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
334 curpcb = &lwp0.l_addr->u_pcb;
335 curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
336
337 /*
338 * Clear previous AHB errors
339 */
340 GETSYSREG(AR531X_SYSREG_AHBPERR);
341 GETSYSREG(AR531X_SYSREG_AHBDMAE);
342
343 /*
344 * Turn off (ignore) the hardware watchdog. If we got this
345 * far, then we shouldn't need it anymore.
346 */
347 PUTSYSREG(AR531X_SYSREG_WDOG_CTL, AR531X_WDOG_CTL_IGNORE);
348
349 /*
350 * Turn off watchpoint that may have been enabled by the
351 * PROM. VxWorks bootloader seems to leave one set.
352 */
353 __asm volatile (
354 "mtc0 $0, $" ___STRING(MIPS_COP_0_WATCH_LO) " \n\t"
355 "nop\n\t"
356 "nop\n\t");
357
358 /*
359 * Initialize debuggers, and break into them, if appropriate.
360 */
361 #if NKSYMS || defined(DDB) || defined(LKM)
362 ksyms_init(0, 0, 0);
363 #endif
364
365 #ifdef DDB
366 if (boothowto & RB_KDB)
367 Debugger();
368 #endif
369 }
370
371 void
372 consinit(void)
373 {
374
375 /*
376 * Everything related to console initialization is done
377 * in mach_init().
378 */
379 #if NCOM > 0
380 /* Setup polled serial for early console I/O */
381 /* XXX: pass in CONSPEED? */
382 com_arbus_cnattach(AR531X_UART0_BASE);
383 #else
384 panic("Not configured to use serial console!\n");
385 /* not going to see that message now, are we? */
386 #endif
387 }
388
389 void
390 cpu_startup(void)
391 {
392 char pbuf[9];
393 vaddr_t minaddr, maxaddr;
394 #ifdef DEBUG
395 extern int pmapdebug; /* XXX */
396 int opmapdebug = pmapdebug;
397
398 pmapdebug = 0; /* Shut up pmap debug during bootstrap */
399 #endif
400
401 /*
402 * Good {morning,afternoon,evening,night}.
403 */
404 printf("%s%s", copyright, version);
405 printf("%s\n", cpu_model);
406 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
407 printf("total memory = %s\n", pbuf);
408
409 minaddr = 0;
410 /*
411 * Allocate a submap for exec arguments. This map effectively
412 * limits the number of processes exec'ing at any time.
413 */
414 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
415 16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
416
417 /*
418 * Allocate a submap for physio
419 */
420 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
421 VM_PHYS_SIZE, 0, FALSE, NULL);
422
423 /*
424 * No need to allocate an mbuf cluster submap. Mbuf clusters
425 * are allocated via the pool allocator, and we use KSEG to
426 * map those pages.
427 */
428
429 #ifdef DEBUG
430 pmapdebug = opmapdebug;
431 #endif
432 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
433 printf("avail memory = %s\n", pbuf);
434 }
435
436 void
437 cpu_reboot(int howto, char *bootstr)
438 {
439 static int waittime = -1;
440
441 /* Take a snapshot before clobbering any registers. */
442 if (curproc)
443 savectx((struct user *)curpcb);
444
445 /* If "always halt" was specified as a boot flag, obey. */
446 if (boothowto & RB_HALT)
447 howto |= RB_HALT;
448
449 boothowto = howto;
450
451 /* If system is cold, just halt. */
452 if (cold) {
453 boothowto |= RB_HALT;
454 goto haltsys;
455 }
456
457 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
458 waittime = 0;
459
460 /*
461 * Synchronize the disks....
462 */
463 vfs_shutdown();
464
465 /*
466 * If we've been adjusting the clock, the todr
467 * will be out of synch; adjust it now.
468 */
469 resettodr();
470 }
471
472 /* Disable interrupts. */
473 splhigh();
474
475 if (boothowto & RB_DUMP)
476 dumpsys();
477
478 haltsys:
479 /* Run any shutdown hooks. */
480 doshutdownhooks();
481
482 #if 0
483 if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
484 if (board && board->ab_poweroff)
485 board->ab_poweroff();
486 #endif
487
488 /*
489 * Firmware may autoboot (depending on settings), and we cannot pass
490 * flags to it (at least I haven't figured out how to yet), so
491 * we "pseudo-halt" now.
492 */
493 if (boothowto & RB_HALT) {
494 printf("\n");
495 printf("The operating system has halted.\n");
496 printf("Please press any key to reboot.\n\n");
497 cnpollc(1); /* For proper keyboard command handling */
498 cngetc();
499 cnpollc(0);
500 }
501
502 printf("reseting board...\n\n");
503 mips_icache_sync_all();
504 mips_dcache_wbinv_all();
505 __asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
506 printf("Oops, back from reset\n\nSpinning...");
507 for (;;)
508 /* spin forever */ ; /* XXX */
509 /*NOTREACHED*/
510 }
511