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