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