machdep.c revision 1.38 1 /* $NetBSD: machdep.c,v 1.38 2008/11/11 06:46:42 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Portions written by Garrett D'Amore for Itronix Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33 /*
34 * Copyright (c) 1992, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * the Systems Programming Group of the University of Utah Computer
39 * Science Department, The Mach Operating System project at
40 * Carnegie-Mellon University and Ralph Campbell.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
67 * from: Utah Hdr: machdep.c 1.63 91/04/24
68 */
69 /*
70 * Copyright (c) 1988 University of Utah.
71 *
72 * This code is derived from software contributed to Berkeley by
73 * the Systems Programming Group of the University of Utah Computer
74 * Science Department, The Mach Operating System project at
75 * Carnegie-Mellon University and Ralph Campbell.
76 *
77 * Redistribution and use in source and binary forms, with or without
78 * modification, are permitted provided that the following conditions
79 * are met:
80 * 1. Redistributions of source code must retain the above copyright
81 * notice, this list of conditions and the following disclaimer.
82 * 2. Redistributions in binary form must reproduce the above copyright
83 * notice, this list of conditions and the following disclaimer in the
84 * documentation and/or other materials provided with the distribution.
85 * 3. All advertising materials mentioning features or use of this software
86 * must display the following acknowledgement:
87 * This product includes software developed by the University of
88 * California, Berkeley and its contributors.
89 * 4. Neither the name of the University nor the names of its contributors
90 * may be used to endorse or promote products derived from this software
91 * without specific prior written permission.
92 *
93 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
94 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
96 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
97 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
98 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
99 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
101 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
102 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
103 * SUCH DAMAGE.
104 *
105 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
106 * from: Utah Hdr: machdep.c 1.63 91/04/24
107 */
108
109 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
110 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.38 2008/11/11 06:46:42 dyoung Exp $");
111
112 #include "opt_ddb.h"
113 #include "opt_kgdb.h"
114
115 #include "opt_memsize.h"
116 #include "opt_ethaddr.h"
117
118 #include <sys/param.h>
119 #include <sys/systm.h>
120 #include <sys/kernel.h>
121 #include <sys/buf.h>
122 #include <sys/reboot.h>
123 #include <sys/user.h>
124 #include <sys/mount.h>
125 #include <sys/kcore.h>
126 #include <sys/boot_flag.h>
127 #include <sys/termios.h>
128 #include <sys/ksyms.h>
129 #include <sys/device.h>
130
131 #include <net/if.h>
132 #include <net/if_ether.h>
133
134 #include <uvm/uvm_extern.h>
135
136 #include <dev/cons.h>
137
138 #include "ksyms.h"
139
140 #if NKSYMS || defined(DDB) || defined(LKM)
141 #include <machine/db_machdep.h>
142 #include <ddb/db_extern.h>
143 #endif
144
145 #include <mips/cache.h>
146 #include <mips/locore.h>
147 #include <machine/yamon.h>
148
149 #include <evbmips/alchemy/board.h>
150 #include <mips/alchemy/dev/aupcivar.h>
151 #include <mips/alchemy/dev/aupcmciavar.h>
152 #include <mips/alchemy/dev/auspivar.h>
153 #include <mips/alchemy/include/aureg.h>
154 #include <mips/alchemy/include/auvar.h>
155 #include <mips/alchemy/include/aubusvar.h>
156
157 #include "com.h"
158 #if NCOM > 0
159
160 int aucomcnrate = 0;
161 #endif /* NAUCOM > 0 */
162
163 #include "ohci.h"
164
165 struct user *proc0paddr;
166
167 /* Our exported CPU info; we can have only one. */
168 struct cpu_info cpu_info_store;
169
170 /* Maps for VM objects. */
171 struct vm_map *mb_map = NULL;
172 struct vm_map *phys_map = NULL;
173
174 int physmem; /* # pages of physical memory */
175 int maxmem; /* max memory per process */
176
177 int mem_cluster_cnt;
178 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
179
180 yamon_env_var *yamon_envp;
181 struct mips_bus_space alchemy_cpuregt;
182
183 void mach_init(int, char **, yamon_env_var *, u_long); /* XXX */
184
185 void
186 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
187 {
188 bus_space_handle_t sh;
189 void *kernend;
190 const char *cp;
191 u_long first, last;
192 void *v;
193 int freqok, howto, i;
194 const struct alchemy_board *board;
195
196 extern char edata[], end[]; /* XXX */
197
198 board = board_info();
199 KASSERT(board != NULL);
200
201 /* clear the BSS segment */
202 kernend = (void *)mips_round_page(end);
203 memset(edata, 0, (char *)kernend - edata);
204
205 /* set CPU model info for sysctl_hw */
206 strcpy(cpu_model, board->ab_name);
207
208 /* save the yamon environment pointer */
209 yamon_envp = envp;
210
211 /* Use YAMON callbacks for early console I/O */
212 cn_tab = &yamon_promcd;
213
214 /*
215 * Set up the exception vectors and CPU-specific function
216 * vectors early on. We need the wbflush() vector set up
217 * before comcnattach() is called (or at least before the
218 * first printf() after that is called).
219 * Sets up mips_cpu_flags that may be queried by other
220 * functions called during startup.
221 * Also clears the I+D caches.
222 */
223 mips_vector_init();
224
225 /*
226 * Set the VM page size.
227 */
228 uvm_setpagesize();
229
230 /*
231 * Use YAMON's CPU frequency if available.
232 */
233 freqok = yamon_setcpufreq(1);
234
235 /*
236 * Initialize bus space tags.
237 */
238 au_cpureg_bus_mem_init(&alchemy_cpuregt, &alchemy_cpuregt);
239 aubus_st = &alchemy_cpuregt;
240
241 /*
242 * Calibrate the timer if YAMON failed to tell us.
243 */
244 if (!freqok) {
245 bus_space_map(aubus_st, PC_BASE, PC_SIZE, 0, &sh);
246 au_cal_timers(aubus_st, sh);
247 bus_space_unmap(aubus_st, sh, PC_SIZE);
248 }
249
250 /*
251 * Perform board-specific initialization.
252 */
253 board->ab_init();
254
255 /*
256 * Bring up the console.
257 */
258 #if NCOM > 0
259 #ifdef CONSPEED
260 if (aucomcnrate == 0)
261 aucomcnrate = CONSPEED;
262 #else /* !CONSPEED */
263 /*
264 * Learn default console speed. We use the YAMON environment,
265 * though we could probably also figure it out by checking the
266 * aucom registers directly.
267 */
268 if ((aucomcnrate == 0) && ((cp = yamon_getenv("modetty0")) != NULL))
269 aucomcnrate = strtoul(cp, NULL, 0);
270
271 if (aucomcnrate == 0) {
272 printf("FATAL: `modetty0' YAMON variable not set. Set it\n");
273 printf(" to the speed of the console and try again.\n");
274 printf(" Or, build a kernel with the `CONSPEED' "
275 "option.\n");
276 panic("mach_init");
277 }
278 #endif /* CONSPEED */
279
280 /*
281 * Delay to allow firmware putchars to complete.
282 * FIFO depth * character time.
283 * character time = (1000000 / (defaultrate / 10))
284 */
285 delay(160000000 / aucomcnrate);
286 if (com_aubus_cnattach(UART0_BASE, aucomcnrate) != 0)
287 panic("mach_init: unable to initialize serial console");
288
289 #else /* NCOM > 0 */
290 panic("mach_init: not configured to use serial console");
291 #endif /* NAUCOM > 0 */
292
293 /*
294 * Look at arguments passed to us and compute boothowto.
295 */
296 boothowto = RB_AUTOBOOT;
297 #ifdef KADB
298 boothowto |= RB_KDB;
299 #endif
300 for (i = 1; i < argc; i++) {
301 for (cp = argv[i]; *cp; cp++) {
302 /* Ignore superfluous '-', if there is one */
303 if (*cp == '-')
304 continue;
305
306 howto = 0;
307 BOOT_FLAG(*cp, howto);
308 if (! howto)
309 printf("bootflag '%c' not recognised\n", *cp);
310 else
311 boothowto |= howto;
312 }
313 }
314
315 /*
316 * Determine the memory size. Use the `memsize' PMON
317 * variable. If that's not available, panic.
318 *
319 * Note: Reserve the first page! That's where the trap
320 * vectors are located.
321 */
322
323 #if defined(MEMSIZE)
324 memsize = MEMSIZE;
325 #else
326 if (memsize == 0) {
327 if ((cp = yamon_getenv("memsize")) != NULL)
328 memsize = strtoul(cp, NULL, 0);
329 else {
330 printf("FATAL: `memsize' YAMON variable not set. Set it to\n");
331 printf(" the amount of memory (in MB) and try again.\n");
332 printf(" Or, build a kernel with the `MEMSIZE' "
333 "option.\n");
334 panic("mach_init");
335 }
336 }
337 #endif /* MEMSIZE */
338 printf("Memory size: 0x%08lx\n", memsize);
339 physmem = btoc(memsize);
340
341 mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
342 mem_clusters[mem_cluster_cnt].size =
343 memsize - mem_clusters[mem_cluster_cnt].start;
344 mem_cluster_cnt++;
345
346 /*
347 * Load the rest of the available pages into the VM system.
348 */
349 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
350 last = mem_clusters[0].start + mem_clusters[0].size;
351 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
352 VM_FREELIST_DEFAULT);
353
354 /*
355 * Initialize message buffer (at end of core).
356 */
357 mips_init_msgbuf();
358
359 /*
360 * Initialize the virtual memory system.
361 */
362 pmap_bootstrap();
363
364 /*
365 * Init mapping for u page(s) for proc0.
366 */
367 v = (void *) uvm_pageboot_alloc(USPACE);
368 lwp0.l_addr = proc0paddr = (struct user *)v;
369 lwp0.l_md.md_regs = (struct frame *)((char *)v + USPACE) - 1;
370 proc0paddr->u_pcb.pcb_context[11] =
371 MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
372
373 /*
374 * Initialize debuggers, and break into them, if appropriate.
375 */
376 #if NKSYMS || defined(DDB) || defined(LKM)
377 ksyms_init(0, 0, 0);
378 #endif
379 #ifdef DDB
380 if (boothowto & RB_KDB)
381 Debugger();
382 #endif
383 }
384
385 void
386 consinit(void)
387 {
388
389 /*
390 * Everything related to console initialization is done
391 * in mach_init().
392 */
393 }
394
395 void
396 cpu_startup(void)
397 {
398 char pbuf[9];
399 vaddr_t minaddr, maxaddr;
400 #ifdef DEBUG
401 extern int pmapdebug; /* XXX */
402 int opmapdebug = pmapdebug;
403
404 pmapdebug = 0; /* Shut up pmap debug during bootstrap */
405 #endif
406
407 /*
408 * Good {morning,afternoon,evening,night}.
409 */
410 printf("%s%s", copyright, version);
411 printf("%s\n", cpu_model);
412 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
413 printf("total memory = %s\n", pbuf);
414
415 minaddr = 0;
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 const struct alchemy_board *board;
441
442 /* Take a snapshot before clobbering any registers. */
443 if (curproc)
444 savectx((struct user *)curpcb);
445
446 board = board_info();
447 KASSERT(board != NULL);
448
449 /* If "always halt" was specified as a boot flag, obey. */
450 if (boothowto & RB_HALT)
451 howto |= RB_HALT;
452
453 boothowto = howto;
454
455 /* If system is cold, just halt. */
456 if (cold) {
457 boothowto |= RB_HALT;
458 goto haltsys;
459 }
460
461 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
462 waittime = 0;
463
464 /*
465 * Synchronize the disks....
466 */
467 vfs_shutdown();
468
469 /*
470 * If we've been adjusting the clock, the todr
471 * will be out of synch; adjust it now.
472 */
473 resettodr();
474 }
475
476 /* Disable interrupts. */
477 splhigh();
478
479 if (boothowto & RB_DUMP)
480 dumpsys();
481
482 haltsys:
483 /* Run any shutdown hooks. */
484 doshutdownhooks();
485
486 pmf_system_shutdown(boothowto);
487
488 if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
489 if (board && board->ab_poweroff)
490 board->ab_poweroff();
491
492 /*
493 * YAMON may autoboot (depending on settings), and we cannot pass
494 * flags to it (at least I haven't figured out how to yet), so
495 * we "pseudo-halt" now.
496 */
497 if (boothowto & RB_HALT) {
498 printf("\n");
499 printf("The operating system has halted.\n");
500 printf("Please press any key to reboot.\n\n");
501 cnpollc(1); /* For proper keyboard command handling */
502 cngetc();
503 cnpollc(0);
504 }
505
506 printf("reseting board...\n\n");
507
508 /*
509 * Try to use board-specific reset logic, which might involve a better
510 * hardware reset.
511 */
512 if (board->ab_reboot)
513 board->ab_reboot();
514
515 #if 1
516 /* XXX
517 * For some reason we are leaving the ethernet MAC in a state where
518 * YAMON isn't happy with it. So just call the reset vector (grr,
519 * Alchemy YAMON doesn't have a "reset" command).
520 */
521 mips_icache_sync_all();
522 mips_dcache_wbinv_all();
523 __asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
524 #else
525 printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
526 yamon_exit(boothowto);
527 printf("Oops, back from yamon_exit()\n\nSpinning...");
528 #endif
529 for (;;)
530 /* spin forever */ ; /* XXX */
531 /*NOTREACHED*/
532 }
533
534 /*
535 * Export our interrupt map function so aupci can find it.
536 */
537 int
538 aupci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
539 {
540 const struct alchemy_board *board;
541
542 board = board_info();
543 if (board->ab_pci_intr_map != NULL)
544 return (board->ab_pci_intr_map(pa, ihp));
545 return 1;
546 }
547
548 struct aupcmcia_machdep *
549 aupcmcia_machdep(void)
550 {
551 const struct alchemy_board *board;
552
553 board = board_info();
554 return (board->ab_pcmcia);
555 }
556
557 const struct auspi_machdep *
558 auspi_machdep(bus_addr_t ba)
559 {
560 const struct alchemy_board *board;
561
562 board = board_info();
563 if (board->ab_spi != NULL)
564 return (board->ab_spi(ba));
565 return NULL;
566 }
567