machdep.c revision 1.6.10.4 1 /* $NetBSD: machdep.c,v 1.6.10.4 2010/02/01 04:17:50 matt 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.6.10.4 2010/02/01 04:17:50 matt 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
133 #include <net/if.h>
134 #include <net/if_ether.h>
135
136 #include <uvm/uvm_extern.h>
137
138 #include <dev/cons.h>
139
140 #include "ksyms.h"
141
142 #if NKSYMS || defined(DDB) || defined(LKM)
143 #include <machine/db_machdep.h>
144 #include <ddb/db_extern.h>
145 #endif
146
147 #include <mips/cache.h>
148 #include <mips/locore.h>
149
150 #include <mips/adm5120/include/adm5120reg.h>
151 #include <mips/adm5120/include/adm5120var.h>
152 #include <mips/adm5120/include/adm5120_extiovar.h>
153 #include <mips/adm5120/include/adm5120_obiovar.h>
154 #include <mips/adm5120/include/adm5120_mainbusvar.h>
155 #include <mips/adm5120/include/adm5120_pcivar.h>
156 #include <mips/adm5120/dev/uart.h>
157
158 #ifndef MEMSIZE
159 #define MEMSIZE 4 * 1024 * 1024
160 #endif /* !MEMSIZE */
161
162 /* Maps for VM objects. */
163 struct vm_map *mb_map = NULL;
164 struct vm_map *phys_map = NULL;
165
166 int physmem; /* # pages of physical memory */
167 int maxmem; /* max memory per process */
168
169 int mem_cluster_cnt;
170 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
171
172 struct adm5120_config adm5120_configuration;
173
174 void adm5120_setcpufreq(void);
175
176 void
177 adm5120_setcpufreq(void)
178 {
179 uint32_t v, freq;
180
181 v = SW_READ(SW_CODE_REG);
182 switch (v & CLKS_MASK) {
183 case CLKS_175MHZ:
184 freq = 175 * 1000 * 1000;
185 break;
186 case CLKS_200MHZ:
187 freq = 200 * 1000 * 1000;
188 break;
189 default:
190 panic("adm5120: cannot determine CPU clock speed");
191 }
192
193 curcpu()->ci_cpu_freq = freq;
194 curcpu()->ci_cycles_per_hz = (freq + hz / 2) / hz / 2;
195 curcpu()->ci_divisor_delay = ((freq + 500000) / 1000000) / 2;
196 }
197
198 void mach_init(int, char **, void *, void *); /* XXX */
199
200 static void
201 copy_args(int argc, char **argv)
202 {
203 struct adm5120_config *admc = &adm5120_configuration;
204 int i;
205 char *buf;
206 size_t buflen, rc;
207
208 buf = admc->args;
209 buflen = sizeof(admc->args);
210
211 if (argc >= __arraycount(admc->argv))
212 panic("%s: too many boot args\n", __func__);
213
214 for (i = 0; buflen > 0 && i < argc && argv[i] != NULL; i++) {
215 admc->argv[i] = buf;
216 if ((rc = strlcpy(buf, argv[i], buflen)) >= buflen)
217 panic("%s: boot args too long\n", __func__);
218
219 buf += rc;
220 buflen -= rc;
221 *buf++ = '\0';
222 buflen--;
223 }
224 if (i < argc)
225 panic("%s: boot args too long\n", __func__);
226
227 admc->argc = argc;
228 }
229
230 static void
231 parse_args(prop_dictionary_t properties, int argc, char **argv,
232 uint32_t *memsizep)
233 {
234 char buf[32];
235 char *key, *val, *valend;
236 unsigned long tmp;
237 int i;
238 uint8_t enaddr[ETHER_ADDR_LEN];
239
240 if (memsizep != NULL)
241 *memsizep = MEMSIZE;
242
243 for (i = 0; i < argc && argv[i] != NULL; i++) {
244 if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf))
245 goto err;
246 val = buf;
247 key = strsep(&val, "=");
248 if (val == NULL)
249 goto err;
250 if (strcmp(key, "mem") == 0) {
251 tmp = strtoul(val, &valend, 10);
252 if (val == valend || *valend != 'M')
253 goto err;
254 if (memsizep != NULL)
255 *memsizep = tmp * 1024 * 1024;
256 } else if (strcmp(key, "HZ") == 0)
257 ;
258 else if (strcmp(key, "gpio") == 0) {
259 prop_number_t pn;
260
261 tmp = strtoul(val, &valend, 10);
262 if (val == valend || *valend != '\0')
263 goto err;
264 if (properties == NULL)
265 continue;
266 pn = prop_number_create_unsigned_integer(tmp);
267 if (pn == NULL) {
268 printf(
269 "%s: prop_number_create_unsigned_integer\n",
270 __func__);
271 continue;
272 }
273 if (!prop_dictionary_set(properties, "initial-gpio",
274 pn)) {
275 printf("%s: prop_dictionary_set(gpio)\n",
276 __func__);
277 }
278 prop_object_release(pn);
279 } else if (strcmp(key, "kmac") == 0) {
280 prop_data_t pd;
281
282 ether_nonstatic_aton(enaddr, val);
283 if (properties == NULL)
284 continue;
285 pd = prop_data_create_data(enaddr, sizeof(enaddr));
286 if (pd == NULL) {
287 printf("%s: prop_data_create_data\n", __func__);
288 continue;
289 }
290 if (!prop_dictionary_set(properties, "mac-addr", pd)) {
291 printf("%s: prop_dictionary_set(mac)\n",
292 __func__);
293 }
294 prop_object_release(pd);
295 } else if (strcmp(key, "board") == 0)
296 printf("Routerboard %s\n", val);
297 else if (strcmp(key, "boot") == 0)
298 ;
299 continue;
300 err:
301 printf("bad argv[%d] (%s)\n", i, argv[i]);
302 }
303 }
304
305 void
306 mach_init(int argc, char **argv, void *a2, void *a3)
307 {
308 struct adm5120_config *admc = &adm5120_configuration;
309 uint32_t memsize;
310 vaddr_t kernend;
311
312 extern char edata[], end[]; /* XXX */
313
314 /* clear the BSS segment */
315 kernend = mips_round_page(end);
316 memset(edata, 0, kernend - (vaddr_t)edata);
317
318 /* set CPU model info for sysctl_hw */
319 strcpy(cpu_model, "Infineon ADM5120");
320
321 /*
322 * Set up the exception vectors and CPU-specific function
323 * vectors early on. We need the wbflush() vector set up
324 * before comcnattach() is called (or at least before the
325 * first printf() after that is called).
326 * Sets up mips_cpu_flags that may be queried by other
327 * functions called during startup.
328 * Also clears the I+D caches.
329 */
330 mips_vector_init();
331
332 /*
333 * Set the VM page size.
334 */
335 uvm_setpagesize();
336
337 adm5120_setcpufreq();
338
339 /*
340 * Initialize bus space tags.
341 */
342 obio_bus_mem_init(&admc->obio_space, admc);
343 extio_bus_mem_init(&admc->extio_space, admc);
344 #if NPCI > 0
345 pciio_bus_mem_init(&admc->pciio_space, admc);
346 pcimem_bus_mem_init(&admc->pcimem_space, admc);
347 #endif
348
349 /*
350 * Initialize bus DMA tag.
351 */
352 obio_dma_init(&admc->obio_dmat);
353
354 /*
355 * Attach serial console.
356 */
357 uart_cnattach();
358
359 /*
360 * Look at arguments passed to us and compute boothowto.
361 */
362 boothowto = RB_AUTOBOOT;
363 #ifdef KADB
364 boothowto |= RB_KDB;
365 #endif
366
367 parse_args(NULL, argc, argv, &memsize);
368
369 /*
370 * Determine the memory size.
371 *
372 * Note: Reserve the first page! That's where the trap
373 * vectors are located.
374 */
375
376 #if 0
377 if (GET_MEMSIZE(memsize) == 0) {
378 uint32_t val;
379
380 /* This does not seem to work... --dyoung */
381 val = SW_READ(SW_MEMCONT_REG);
382 printf("SW_MEMCONT_REG: 0x%08" PRIx32 "\n", val);
383 switch (val & SDRAM_SIZE_MASK) {
384 case SDRAM_SIZE_4MBYTES:
385 memsize = 4 * 1024 * 1024;
386 break;
387 case SDRAM_SIZE_8MBYTES:
388 memsize = 8 * 1024 * 1024;
389 break;
390 case SDRAM_SIZE_16MBYTES:
391 memsize = 16 * 1024 * 1024;
392 break;
393 case SDRAM_SIZE_64MBYTES:
394 memsize = 64 * 1024 * 1024;
395 break;
396 case SDRAM_SIZE_128MBYTES:
397 memsize = 128 * 1024 * 1024;
398 break;
399 default:
400 panic("adm5120: cannot determine memory size");
401 }
402 }
403 #endif
404
405 physmem = btoc(memsize);
406
407 mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
408 mem_clusters[mem_cluster_cnt].size =
409 memsize - mem_clusters[mem_cluster_cnt].start;
410 mem_cluster_cnt++;
411
412 /*
413 * Load the rest of the available pages into the VM system.
414 */
415 mips_page_physload(MIPS_KSEG0_START, (vaddr_t) kernend,
416 mem_clusters, mem_cluster_cnt, NULL, 0);
417
418 /*
419 * Initialize message buffer (at end of core).
420 */
421 mips_init_msgbuf();
422
423 /*
424 * Initialize the virtual memory system.
425 */
426 pmap_bootstrap();
427
428 /*
429 * Init mapping for u page(s) for proc0.
430 */
431 mips_init_lwp0_uarea();
432
433 /*
434 * Initialize debuggers, and break into them, if appropriate.
435 */
436 #if NKSYMS || defined(DDB) || defined(LKM)
437 ksyms_init(0, 0, 0);
438 #endif
439 #ifdef DDB
440 if (boothowto & RB_KDB)
441 Debugger();
442 #endif
443
444 copy_args(argc, argv);
445 }
446
447 void
448 consinit(void)
449 {
450
451 /*
452 * Everything related to console initialization is done
453 * in mach_init().
454 */
455 }
456
457 void
458 cpu_startup(void)
459 {
460 struct adm5120_config *admc = &adm5120_configuration;
461 char pbuf[9];
462 vaddr_t minaddr, maxaddr;
463 #ifdef DEBUG
464 extern int pmapdebug; /* XXX */
465 int opmapdebug = pmapdebug;
466
467 pmapdebug = 0; /* Shut up pmap debug during bootstrap */
468 #endif
469
470 if ((admc->properties = prop_dictionary_create()) == NULL)
471 printf("%s: prop_dictionary_create\n", __func__);
472 parse_args(admc->properties, admc->argc, admc->argv, NULL);
473
474 /*
475 * Good {morning,afternoon,evening,night}.
476 */
477 printf("%s%s", copyright, version);
478 printf("%s\n", cpu_model);
479 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
480 printf("total memory = %s\n", pbuf);
481
482 minaddr = 0;
483
484 /*
485 * Allocate a submap for physio
486 */
487 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
488 VM_PHYS_SIZE, 0, FALSE, NULL);
489
490 /*
491 * No need to allocate an mbuf cluster submap. Mbuf clusters
492 * are allocated via the pool allocator, and we use KSEG to
493 * map those pages.
494 */
495
496 #ifdef DEBUG
497 pmapdebug = opmapdebug;
498 #endif
499 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
500 printf("avail memory = %s\n", pbuf);
501 }
502
503 void
504 cpu_reboot(int howto, char *bootstr)
505 {
506 static int waittime = -1;
507
508 /* Take a snapshot before clobbering any registers. */
509 savectx(curlwp->l_addr);
510
511 /* If "always halt" was specified as a boot flag, obey. */
512 if (boothowto & RB_HALT)
513 howto |= RB_HALT;
514
515 boothowto = howto;
516
517 /* If system is cold, just halt. */
518 if (cold) {
519 boothowto |= RB_HALT;
520 goto haltsys;
521 }
522
523 if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
524 waittime = 0;
525
526 /*
527 * Synchronize the disks....
528 */
529 vfs_shutdown();
530
531 /*
532 * If we've been adjusting the clock, the todr
533 * will be out of synch; adjust it now.
534 */
535 resettodr();
536 }
537
538 /* Disable interrupts. */
539 splhigh();
540
541 if (boothowto & RB_DUMP)
542 dumpsys();
543
544 haltsys:
545 /* Run any shutdown hooks. */
546 doshutdownhooks();
547
548 /*
549 * Routerboard BIOS may autoboot, so "pseudo-halt".
550 */
551 if (boothowto & RB_HALT) {
552 printf("\n");
553 printf("The operating system has halted.\n");
554 printf("Please press any key to reboot.\n\n");
555 cnpollc(1); /* For proper keyboard command handling */
556 cngetc();
557 cnpollc(0);
558 }
559
560 printf("reseting board...\n\n");
561 mips_icache_sync_all();
562 mips_dcache_wbinv_all();
563 SW_WRITE(SW_SFTRES_REG, 0); /* reset */
564 for (;;)
565 /* spin forever */ ; /* XXX */
566 /*NOTREACHED*/
567 }
568