machdep.c revision 1.7 1 /* $NetBSD: machdep.c,v 1.7 2008/11/11 06:46:42 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * 3. The names of the authors may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * 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.7 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 "opt_pci.h"
119 #include "pci.h"
120
121 #include <sys/param.h>
122 #include <sys/systm.h>
123 #include <sys/kernel.h>
124 #include <sys/buf.h>
125 #include <sys/reboot.h>
126 #include <sys/user.h>
127 #include <sys/mount.h>
128 #include <sys/kcore.h>
129 #include <sys/boot_flag.h>
130 #include <sys/termios.h>
131 #include <sys/ksyms.h>
132 #include <sys/device.h>
133
134 #include <net/if.h>
135 #include <net/if_ether.h>
136
137 #include <uvm/uvm_extern.h>
138
139 #include <dev/cons.h>
140
141 #include "ksyms.h"
142
143 #if NKSYMS || defined(DDB) || defined(LKM)
144 #include <machine/db_machdep.h>
145 #include <ddb/db_extern.h>
146 #endif
147
148 #include <mips/cache.h>
149 #include <mips/locore.h>
150
151 #include <mips/adm5120/include/adm5120reg.h>
152 #include <mips/adm5120/include/adm5120var.h>
153 #include <mips/adm5120/include/adm5120_extiovar.h>
154 #include <mips/adm5120/include/adm5120_obiovar.h>
155 #include <mips/adm5120/include/adm5120_mainbusvar.h>
156 #include <mips/adm5120/include/adm5120_pcivar.h>
157 #include <mips/adm5120/dev/uart.h>
158
159 #ifndef MEMSIZE
160 #define MEMSIZE 4 * 1024 * 1024
161 #endif /* !MEMSIZE */
162
163 struct user *proc0paddr;
164
165 /* Our exported CPU info; we can have only one. */
166 struct cpu_info cpu_info_store;
167
168 /* Maps for VM objects. */
169 struct vm_map *mb_map = NULL;
170 struct vm_map *phys_map = NULL;
171
172 int physmem; /* # pages of physical memory */
173 int maxmem; /* max memory per process */
174
175 int mem_cluster_cnt;
176 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
177
178 struct adm5120_config adm5120_configuration;
179
180 void adm5120_setcpufreq(void);
181
182 void
183 adm5120_setcpufreq(void)
184 {
185 uint32_t v, freq;
186
187 v = SW_READ(SW_CODE_REG);
188 switch (v & CLKS_MASK) {
189 case CLKS_175MHZ:
190 freq = 175 * 1000 * 1000;
191 break;
192 case CLKS_200MHZ:
193 freq = 200 * 1000 * 1000;
194 break;
195 default:
196 panic("adm5120: cannot determine CPU clock speed");
197 }
198
199 curcpu()->ci_cpu_freq = freq;
200 curcpu()->ci_cycles_per_hz = (freq + hz / 2) / hz / 2;
201 curcpu()->ci_divisor_delay = ((freq + 500000) / 1000000) / 2;
202 }
203
204 void mach_init(int, char **, void *, void *); /* XXX */
205
206 static void
207 copy_args(int argc, char **argv)
208 {
209 struct adm5120_config *admc = &adm5120_configuration;
210 int i;
211 char *buf;
212 size_t buflen, rc;
213
214 buf = admc->args;
215 buflen = sizeof(admc->args);
216
217 if (argc >= __arraycount(admc->argv))
218 panic("%s: too many boot args\n", __func__);
219
220 for (i = 0; buflen > 0 && i < argc && argv[i] != NULL; i++) {
221 admc->argv[i] = buf;
222 if ((rc = strlcpy(buf, argv[i], buflen)) >= buflen)
223 panic("%s: boot args too long\n", __func__);
224
225 buf += rc;
226 buflen -= rc;
227 *buf++ = '\0';
228 buflen--;
229 }
230 if (i < argc)
231 panic("%s: boot args too long\n", __func__);
232
233 admc->argc = argc;
234 }
235
236 static void
237 parse_args(prop_dictionary_t properties, int argc, char **argv,
238 uint32_t *memsizep)
239 {
240 char buf[32];
241 char *key, *val, *valend;
242 unsigned long tmp;
243 int i;
244 uint8_t enaddr[ETHER_ADDR_LEN];
245
246 if (memsizep != NULL)
247 *memsizep = MEMSIZE;
248
249 for (i = 0; i < argc && argv[i] != NULL; i++) {
250 if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf))
251 goto err;
252 val = buf;
253 key = strsep(&val, "=");
254 if (val == NULL)
255 goto err;
256 if (strcmp(key, "mem") == 0) {
257 tmp = strtoul(val, &valend, 10);
258 if (val == valend || *valend != 'M')
259 goto err;
260 if (memsizep != NULL)
261 *memsizep = tmp * 1024 * 1024;
262 } else if (strcmp(key, "HZ") == 0)
263 ;
264 else if (strcmp(key, "gpio") == 0) {
265 prop_number_t pn;
266
267 tmp = strtoul(val, &valend, 10);
268 if (val == valend || *valend != '\0')
269 goto err;
270 if (properties == NULL)
271 continue;
272 pn = prop_number_create_unsigned_integer(tmp);
273 if (pn == NULL) {
274 printf(
275 "%s: prop_number_create_unsigned_integer\n",
276 __func__);
277 continue;
278 }
279 if (!prop_dictionary_set(properties, "initial-gpio",
280 pn)) {
281 printf("%s: prop_dictionary_set(gpio)\n",
282 __func__);
283 }
284 prop_object_release(pn);
285 } else if (strcmp(key, "kmac") == 0) {
286 prop_data_t pd;
287
288 ether_nonstatic_aton(enaddr, val);
289 if (properties == NULL)
290 continue;
291 pd = prop_data_create_data(enaddr, sizeof(enaddr));
292 if (pd == NULL) {
293 printf("%s: prop_data_create_data\n", __func__);
294 continue;
295 }
296 if (!prop_dictionary_set(properties, "mac-addr", pd)) {
297 printf("%s: prop_dictionary_set(mac)\n",
298 __func__);
299 }
300 prop_object_release(pd);
301 } else if (strcmp(key, "board") == 0)
302 printf("Routerboard %s\n", val);
303 else if (strcmp(key, "boot") == 0)
304 ;
305 continue;
306 err:
307 printf("bad argv[%d] (%s)\n", i, argv[i]);
308 }
309 }
310
311 void
312 mach_init(int argc, char **argv, void *a2, void *a3)
313 {
314 struct adm5120_config *admc = &adm5120_configuration;
315 uint32_t memsize;
316 vaddr_t kernend;
317 u_long first, last;
318 vaddr_t v;
319
320 extern char edata[], end[]; /* XXX */
321
322 /* clear the BSS segment */
323 kernend = mips_round_page(end);
324 memset(edata, 0, kernend - (vaddr_t)edata);
325
326 /* set CPU model info for sysctl_hw */
327 strcpy(cpu_model, "Infineon ADM5120");
328
329 /*
330 * Set up the exception vectors and CPU-specific function
331 * vectors early on. We need the wbflush() vector set up
332 * before comcnattach() is called (or at least before the
333 * first printf() after that is called).
334 * Sets up mips_cpu_flags that may be queried by other
335 * functions called during startup.
336 * Also clears the I+D caches.
337 */
338 mips_vector_init();
339
340 /*
341 * Set the VM page size.
342 */
343 uvm_setpagesize();
344
345 adm5120_setcpufreq();
346
347 /*
348 * Initialize bus space tags.
349 */
350 obio_bus_mem_init(&admc->obio_space, admc);
351 extio_bus_mem_init(&admc->extio_space, admc);
352 #if NPCI > 0
353 pciio_bus_mem_init(&admc->pciio_space, admc);
354 pcimem_bus_mem_init(&admc->pcimem_space, admc);
355 #endif
356
357 /*
358 * Initialize bus DMA tag.
359 */
360 obio_dma_init(&admc->obio_dmat);
361
362 /*
363 * Attach serial console.
364 */
365 uart_cnattach();
366
367 /*
368 * Look at arguments passed to us and compute boothowto.
369 */
370 boothowto = RB_AUTOBOOT;
371 #ifdef KADB
372 boothowto |= RB_KDB;
373 #endif
374
375 parse_args(NULL, argc, argv, &memsize);
376
377 /*
378 * Determine the memory size.
379 *
380 * Note: Reserve the first page! That's where the trap
381 * vectors are located.
382 */
383
384 #if 0
385 if (GET_MEMSIZE(memsize) == 0) {
386 uint32_t val;
387
388 /* This does not seem to work... --dyoung */
389 val = SW_READ(SW_MEMCONT_REG);
390 printf("SW_MEMCONT_REG: 0x%08" PRIx32 "\n", val);
391 switch (val & SDRAM_SIZE_MASK) {
392 case SDRAM_SIZE_4MBYTES:
393 memsize = 4 * 1024 * 1024;
394 break;
395 case SDRAM_SIZE_8MBYTES:
396 memsize = 8 * 1024 * 1024;
397 break;
398 case SDRAM_SIZE_16MBYTES:
399 memsize = 16 * 1024 * 1024;
400 break;
401 case SDRAM_SIZE_64MBYTES:
402 memsize = 64 * 1024 * 1024;
403 break;
404 case SDRAM_SIZE_128MBYTES:
405 memsize = 128 * 1024 * 1024;
406 break;
407 default:
408 panic("adm5120: cannot determine memory size");
409 }
410 }
411 #endif
412
413 physmem = btoc(memsize);
414
415 mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
416 mem_clusters[mem_cluster_cnt].size =
417 memsize - mem_clusters[mem_cluster_cnt].start;
418 mem_cluster_cnt++;
419
420 /*
421 * Load the rest of the available pages into the VM system.
422 */
423 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
424 last = mem_clusters[0].start + mem_clusters[0].size;
425 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
426 VM_FREELIST_DEFAULT);
427
428 /*
429 * Initialize message buffer (at end of core).
430 */
431 mips_init_msgbuf();
432
433 /*
434 * Initialize the virtual memory system.
435 */
436 pmap_bootstrap();
437
438 /*
439 * Init mapping for u page(s) for proc0.
440 */
441 v = uvm_pageboot_alloc(USPACE);
442 lwp0.l_addr = proc0paddr = (struct user *)v;
443 lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
444 proc0paddr->u_pcb.pcb_context[11] =
445 MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
446
447 /*
448 * Initialize debuggers, and break into them, if appropriate.
449 */
450 #if NKSYMS || defined(DDB) || defined(LKM)
451 ksyms_init(0, 0, 0);
452 #endif
453 #ifdef DDB
454 if (boothowto & RB_KDB)
455 Debugger();
456 #endif
457
458 copy_args(argc, argv);
459 }
460
461 void
462 consinit(void)
463 {
464
465 /*
466 * Everything related to console initialization is done
467 * in mach_init().
468 */
469 }
470
471 void
472 cpu_startup(void)
473 {
474 struct adm5120_config *admc = &adm5120_configuration;
475 char pbuf[9];
476 vaddr_t minaddr, maxaddr;
477 #ifdef DEBUG
478 extern int pmapdebug; /* XXX */
479 int opmapdebug = pmapdebug;
480
481 pmapdebug = 0; /* Shut up pmap debug during bootstrap */
482 #endif
483
484 if ((admc->properties = prop_dictionary_create()) == NULL)
485 printf("%s: prop_dictionary_create\n", __func__);
486 parse_args(admc->properties, admc->argc, admc->argv, NULL);
487
488 /*
489 * Good {morning,afternoon,evening,night}.
490 */
491 printf("%s%s", copyright, version);
492 printf("%s\n", cpu_model);
493 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
494 printf("total memory = %s\n", pbuf);
495
496 minaddr = 0;
497
498 /*
499 * Allocate a submap for physio
500 */
501 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
502 VM_PHYS_SIZE, 0, FALSE, NULL);
503
504 /*
505 * No need to allocate an mbuf cluster submap. Mbuf clusters
506 * are allocated via the pool allocator, and we use KSEG to
507 * map those pages.
508 */
509
510 #ifdef DEBUG
511 pmapdebug = opmapdebug;
512 #endif
513 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
514 printf("avail memory = %s\n", pbuf);
515 }
516
517 void
518 cpu_reboot(int howto, char *bootstr)
519 {
520 static int waittime = -1;
521
522 /* Take a snapshot before clobbering any registers. */
523 if (curproc)
524 savectx((struct user *)curpcb);
525
526 /* If "always halt" was specified as a boot flag, obey. */
527 if (boothowto & RB_HALT)
528 howto |= RB_HALT;
529
530 boothowto = howto;
531
532 /* If system is cold, just halt. */
533 if (cold) {
534 boothowto |= RB_HALT;
535 goto haltsys;
536 }
537
538 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
539 waittime = 0;
540
541 /*
542 * Synchronize the disks....
543 */
544 vfs_shutdown();
545
546 /*
547 * If we've been adjusting the clock, the todr
548 * will be out of synch; adjust it now.
549 */
550 resettodr();
551 }
552
553 /* Disable interrupts. */
554 splhigh();
555
556 if (boothowto & RB_DUMP)
557 dumpsys();
558
559 haltsys:
560 /* Run any shutdown hooks. */
561 doshutdownhooks();
562
563 pmf_system_shutdown(boothowto);
564
565 /*
566 * Routerboard BIOS may autoboot, so "pseudo-halt".
567 */
568 if (boothowto & RB_HALT) {
569 printf("\n");
570 printf("The operating system has halted.\n");
571 printf("Please press any key to reboot.\n\n");
572 cnpollc(1); /* For proper keyboard command handling */
573 cngetc();
574 cnpollc(0);
575 }
576
577 printf("reseting board...\n\n");
578 mips_icache_sync_all();
579 mips_dcache_wbinv_all();
580 SW_WRITE(SW_SFTRES_REG, 0); /* reset */
581 for (;;)
582 /* spin forever */ ; /* XXX */
583 /*NOTREACHED*/
584 }
585