xref: /netbsd-src/sys/arch/evbmips/gdium/machdep.c (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
1 /*	$NetBSD: machdep.c,v 1.11 2010/02/08 19:02:27 joerg Exp $	*/
2 
3 /*
4  * Copyright 2001, 2002 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, 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. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Copyright (c) 1992, 1993
40  *	The Regents of the University of California.  All rights reserved.
41  *
42  * This code is derived from software contributed to Berkeley by
43  * the Systems Programming Group of the University of Utah Computer
44  * Science Department, The Mach Operating System project at
45  * Carnegie-Mellon University and Ralph Campbell.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
72  *	from: Utah Hdr: machdep.c 1.63 91/04/24
73  */
74 /*
75  * Copyright (c) 1988 University of Utah.
76  *
77  * This code is derived from software contributed to Berkeley by
78  * the Systems Programming Group of the University of Utah Computer
79  * Science Department, The Mach Operating System project at
80  * Carnegie-Mellon University and Ralph Campbell.
81  *
82  * Redistribution and use in source and binary forms, with or without
83  * modification, are permitted provided that the following conditions
84  * are met:
85  * 1. Redistributions of source code must retain the above copyright
86  *    notice, this list of conditions and the following disclaimer.
87  * 2. Redistributions in binary form must reproduce the above copyright
88  *    notice, this list of conditions and the following disclaimer in the
89  *    documentation and/or other materials provided with the distribution.
90  * 3. All advertising materials mentioning features or use of this software
91  *    must display the following acknowledgement:
92  *	This product includes software developed by the University of
93  *	California, Berkeley and its contributors.
94  * 4. Neither the name of the University nor the names of its contributors
95  *    may be used to endorse or promote products derived from this software
96  *    without specific prior written permission.
97  *
98  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
99  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108  * SUCH DAMAGE.
109  *
110  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
111  *	from: Utah Hdr: machdep.c 1.63 91/04/24
112  */
113 
114 #include <sys/cdefs.h>
115 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.11 2010/02/08 19:02:27 joerg Exp $");
116 
117 #include "opt_ddb.h"
118 #include "opt_execfmt.h"
119 #include "opt_modular.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/mount.h>
127 #include <sys/kcore.h>
128 #include <sys/boot_flag.h>
129 #include <sys/termios.h>
130 #include <sys/ksyms.h>
131 #include <sys/device.h>
132 
133 #include <uvm/uvm_extern.h>
134 
135 #include <dev/cons.h>
136 
137 #include "ksyms.h"
138 
139 #if NKSYMS || defined(DDB) || defined(MODULAR)
140 #include <machine/db_machdep.h>
141 #include <ddb/db_extern.h>
142 #endif
143 
144 #include <machine/cpu.h>
145 #include <machine/psl.h>
146 
147 #include <mips/bonito/bonitoreg.h>
148 #include <evbmips/gdium/gdiumvar.h>
149 
150 #include "com.h"
151 #if NCOM > 0
152 #include <dev/ic/comreg.h>
153 #include <dev/ic/comvar.h>
154 
155 int	comcnrate = 38400;	/* XXX should be config option */
156 #endif /* NCOM > 0 */
157 
158 struct gdium_config gdium_configuration = {
159 	.gc_bonito = {
160 		.bc_adbase = 11,	/* magic */
161 	},
162 };
163 
164 /* For sysctl_hw. */
165 extern char cpu_model[];
166 
167 /* Our exported CPU info; we can have only one. */
168 struct cpu_info cpu_info_store;
169 
170 /* Maps for VM objects. */
171 struct vm_map *phys_map = NULL;
172 
173 int	netboot;		/* Are we netbooting? */
174 
175 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
176 int mem_cluster_cnt;
177 
178 void	configure(void);
179 void	mach_init(int, char **, char **, void *);
180 
181 /*
182  * safepri is a safe priority for sleep to set for a spin-wait during
183  * autoconfiguration or after a panic.  Used as an argument to splx().
184  */
185 int	safepri = MIPS1_PSL_LOWIPL;
186 
187 /*
188  * For some reason, PMON doesn't assign a real address to the Ralink's BAR.
189  * So we have to do it.
190  */
191 static void
192 gdium_pci_attach_hook(device_t parent, device_t self,
193     struct pcibus_attach_args *pba)
194 {
195 	const pcitag_t high_dev = pci_make_tag(pba->pba_pc, 0, 17, 1);
196 	const pcitag_t ralink_dev = pci_make_tag(pba->pba_pc, 0, 13, 0);
197 	bus_size_t high_size, ralink_size;
198 	pcireg_t v;
199 
200 	/*
201 	 * Get the highest PCI addr used from the last PCI dev.
202 	 */
203 	v = pci_conf_read(pba->pba_pc, high_dev, PCI_MAPREG_START);
204 	v &= PCI_MAPREG_MEM_ADDR_MASK;
205 
206 	/*
207 	 * Get the sizes of the map registers.
208 	 */
209 	pci_mapreg_info(pba->pba_pc, high_dev, PCI_MAPREG_START,
210 	    PCI_MAPREG_MEM_TYPE_32BIT, NULL, &high_size, NULL);
211 	pci_mapreg_info(pba->pba_pc, ralink_dev, PCI_MAPREG_START,
212 	    PCI_MAPREG_MEM_TYPE_32BIT, NULL, &ralink_size, NULL);
213 
214 	/*
215 	 * Position the ralink register space after the last device.
216 	 */
217 	v = (v + high_size + ralink_size - 1) & ~(ralink_size - 1);
218 
219 	/*
220 	 * Set the mapreg.
221 	 */
222 	pci_conf_write(pba->pba_pc, ralink_dev, PCI_MAPREG_START, v);
223 
224 #if 0
225 	/*
226 	 * Why does linux do this?
227 	 */
228 	for (int dev = 15; dev <= 17; dev +=2) {
229 		for (int func = 0; func <= 1; func++) {
230 			pcitag_t usb_dev = pci_make_tag(pba->pba_pc, 0, dev, func);
231 			v = pci_conf_read(pba->pba_pc, usb_dev, 0xe0);
232 			v |= 3;
233 			pci_conf_write(pba->pba_pc, usb_dev, 0xe0, v);
234 		}
235 	}
236 #endif
237 }
238 
239 /*
240  * Do all the stuff that locore normally does before calling main().
241  */
242 void
243 mach_init(int argc, char **argv, char **envp, void *callvec)
244 {
245 	struct gdium_config *gc = &gdium_configuration;
246 	void *kernend;
247 	u_long first, last;
248 	struct pcb *pcb0;
249 	vaddr_t v;
250 #ifdef NOTYET
251 	char *cp;
252 	int howto;
253 #endif
254 	int i;
255 	psize_t memsize;
256 
257 	extern char edata[], end[];
258 
259 	/*
260 	 * Clear the BSS segment.
261 	 */
262 	kernend = (void *)mips_round_page(end);
263 	memset(edata, 0, (char *)kernend - edata);
264 
265 	/*
266 	 * Set up the exception vectors and CPU-specific function
267 	 * vectors early on.  We need the wbflush() vector set up
268 	 * before comcnattach() is called (or at least before the
269 	 * first printf() after that is called).
270 	 * Also clears the I+D caches.
271 	 */
272 	mips_vector_init();
273 
274 	/* set the VM page size */
275 	uvm_setpagesize();
276 
277 	memsize = 256*1024*1024;
278 	physmem = btoc(memsize);
279 
280 	bonito_pci_init(&gc->gc_pc, &gc->gc_bonito);
281 	/*
282 	 * Override the null bonito_pci_attach_hook with our own to we can
283 	 * fix the ralink (device 13).
284 	 */
285 	gc->gc_pc.pc_attach_hook = gdium_pci_attach_hook;
286 	gdium_bus_io_init(&gc->gc_iot, gc);
287 	gdium_bus_mem_init(&gc->gc_memt, gc);
288 	gdium_dma_init(gc);
289 	gdium_cnattach(gc);
290 
291 	/*
292 	 * Disable the 2nd PCI window since we don't need it.
293 	 */
294 	mips3_sd((uint64_t *)MIPS_PHYS_TO_KSEG1(BONITO_REGBASE + 0x158), 0xe);
295 	pci_conf_write(&gc->gc_pc, pci_make_tag(&gc->gc_pc, 0, 0, 0), 18, 0);
296 
297 	/*
298 	 * Get the timer from PMON.
299 	 */
300 	for (i = 0; envp[i] != NULL; i++) {
301 		if (!strncmp(envp[i], "cpuclock=", 9)) {
302 			curcpu()->ci_cpu_freq =
303 			    strtoul(&envp[i][9], NULL, 10);
304 			break;
305 		}
306 	}
307 
308 	if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
309 		curcpu()->ci_cpu_freq /= 2;
310 
311 	/* Compute the number of ticks for hz. */
312 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
313 
314 	/* Compute the delay divisor. */
315 	curcpu()->ci_divisor_delay =
316 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
317 
318 	/*
319 	 * Get correct cpu frequency if the CPU runs at twice the
320 	 * external/cp0-count frequency.
321 	 */
322 	if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
323 		curcpu()->ci_cpu_freq *= 2;
324 
325 #ifdef DEBUG
326 	printf("Timer calibration: %lu cycles/sec\n",
327 	    curcpu()->ci_cpu_freq);
328 #endif
329 
330 #if NCOM > 0
331 	/*
332 	 * Delay to allow firmware putchars to complete.
333 	 * FIFO depth * character time.
334 	 * character time = (1000000 / (defaultrate / 10))
335 	 */
336 	delay(160000000 / comcnrate);
337 	if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
338 	    COM_FREQ, COM_TYPE_NORMAL,
339 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
340 		panic("malta: unable to initialize serial console");
341 #endif /* NCOM > 0 */
342 
343 	mem_clusters[0].start = 0;
344 	mem_clusters[0].size = ctob(physmem);
345 	mem_cluster_cnt = 1;
346 
347 	strcpy(cpu_model, "Gdium Liberty 1000");
348 
349 	/*
350 	 * XXX: check argv[0] - do something if "gdb"???
351 	 */
352 
353 	/*
354 	 * Look at arguments passed to us and compute boothowto.
355 	 */
356 	boothowto = RB_AUTOBOOT;
357 #ifdef NOTYET
358 	for (i = 1; i < argc; i++) {
359 		for (cp = argv[i]; *cp; cp++) {
360 			/* Ignore superfluous '-', if there is one */
361 			if (*cp == '-')
362 				continue;
363 
364 			howto = 0;
365 			BOOT_FLAG(*cp, howto);
366 			if (! howto)
367 				printf("bootflag '%c' not recognised\n", *cp);
368 			else
369 				boothowto |= howto;
370 		}
371 	}
372 #endif
373 
374 	/*
375 	 * Load the rest of the available pages into the VM system.
376 	 */
377 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
378 	last = mem_clusters[0].start + mem_clusters[0].size;
379 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
380 		VM_FREELIST_DEFAULT);
381 
382 	/*
383 	 * Initialize error message buffer (at end of core).
384 	 */
385 	mips_init_msgbuf();
386 
387 	pmap_bootstrap();
388 
389 	/*
390 	 * Allocate uarea page for lwp0 and set it.
391 	 */
392 	v = uvm_pageboot_alloc(USPACE);
393 	uvm_lwp_setuarea(&lwp0, v);
394 
395 	pcb0 = lwp_getpcb(&lwp0);
396 	pcb0->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
397 
398 	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
399 
400 	/*
401 	 * Initialize debuggers, and break into them, if appropriate.
402 	 */
403 #if defined(DDB)
404 	if (boothowto & RB_KDB)
405 		Debugger();
406 #endif
407 }
408 
409 void
410 consinit(void)
411 {
412 
413 	/*
414 	 * Everything related to console initialization is done
415 	 * in mach_init().
416 	 */
417 }
418 
419 /*
420  * Allocate memory for variable-sized tables,
421  */
422 void
423 cpu_startup(void)
424 {
425 	vaddr_t minaddr, maxaddr;
426 	char pbuf[9];
427 
428 	/*
429 	 * Good {morning,afternoon,evening,night}.
430 	 */
431 	printf("%s%s", copyright, version);
432 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
433 	printf("total memory = %s\n", pbuf);
434 
435 	/*
436 	 * Virtual memory is bootstrapped -- notify the bus spaces
437 	 * that memory allocation is now safe.
438 	 */
439 	gdium_configuration.gc_mallocsafe = 1;
440 
441 	minaddr = 0;
442 	/*
443 	 * Allocate a submap for physio.
444 	 */
445 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
446 				    VM_PHYS_SIZE, 0, FALSE, NULL);
447 
448 	/*
449 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
450 	 * are allocated via the pool allocator, and we use KSEG to
451 	 * map those pages.)
452 	 */
453 
454 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
455 	printf("avail memory = %s\n", pbuf);
456 }
457 
458 int	waittime = -1;
459 
460 void
461 cpu_reboot(int howto, char *bootstr)
462 {
463 
464 	/* Take a snapshot before clobbering any registers. */
465 	if (curproc)
466 		savectx(curpcb);
467 
468 	if (cold) {
469 		howto |= RB_HALT;
470 		goto haltsys;
471 	}
472 
473 	/* If "always halt" was specified as a boot flag, obey. */
474 	if (boothowto & RB_HALT)
475 		howto |= RB_HALT;
476 
477 	boothowto = howto;
478 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
479 		waittime = 0;
480 		vfs_shutdown();
481 
482 		/*
483 		 * If we've been adjusting the clock, the todr
484 		 * will be out of synch; adjust it now.
485 		 */
486 		resettodr();
487 	}
488 
489 	splhigh();
490 
491 	if (howto & RB_DUMP)
492 		dumpsys();
493 
494 haltsys:
495 	doshutdownhooks();
496 
497 	pmf_system_shutdown(boothowto);
498 
499 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
500 		/*
501 		 * Turning on GPIO1 as output will cause a powerdown.
502 		 */
503 		REGVAL(BONITO_GPIODATA) |= 2;
504 		REGVAL(BONITO_GPIOIE) &= ~2;
505 	}
506 
507 	if (howto & RB_HALT) {
508 		printf("\n");
509 		printf("The operating system has halted.\n");
510 		printf("Please press any key to reboot.\n\n");
511 		cnpollc(1);	/* For proper keyboard command handling */
512 		cngetc();
513 		cnpollc(0);
514 	}
515 
516 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
517 
518 	/*
519 	 * Turning off GPIO2 as output will cause a reset.
520 	 */
521 	REGVAL(BONITO_GPIODATA) &= ~4;
522 	REGVAL(BONITO_GPIOIE) &= ~4;
523 
524 	__asm__ __volatile__ (
525 		"\t.long 0x3c02bfc0\n"
526 		"\t.long 0x00400008\n"
527 	    ::: "v0");
528 }
529