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