machdep.c revision 1.8
1/*	$NetBSD: machdep.c,v 1.8 2000/02/24 19:42:36 msaitoh Exp $	*/
2
3/*-
4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the NetBSD
22 *	Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*-
41 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
42 * All rights reserved.
43 *
44 * This code is derived from software contributed to Berkeley by
45 * William Jolitz.
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. All advertising materials mentioning features or use of this software
56 *    must display the following acknowledgement:
57 *	This product includes software developed by the University of
58 *	California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 *    may be used to endorse or promote products derived from this software
61 *    without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 *	@(#)machdep.c	7.4 (Berkeley) 6/3/91
76 */
77
78#include "opt_ddb.h"
79#include "opt_memsize.h"
80#include "opt_initbsc.h"
81
82#include <sys/param.h>
83#include <sys/systm.h>
84#include <sys/signalvar.h>
85#include <sys/kernel.h>
86#include <sys/map.h>
87#include <sys/proc.h>
88#include <sys/user.h>
89#include <sys/exec.h>
90#include <sys/buf.h>
91#include <sys/reboot.h>
92#include <sys/conf.h>
93#include <sys/file.h>
94#include <sys/malloc.h>
95#include <sys/mbuf.h>
96#include <sys/msgbuf.h>
97#include <sys/mount.h>
98#include <sys/vnode.h>
99#include <sys/device.h>
100#include <sys/extent.h>
101#include <sys/syscallargs.h>
102
103#ifdef KGDB
104#include <sys/kgdb.h>
105#endif
106
107#include <dev/cons.h>
108
109#include <vm/vm.h>
110#include <vm/vm_kern.h>
111#include <vm/vm_page.h>
112
113#include <uvm/uvm_extern.h>
114
115#include <sys/sysctl.h>
116
117#include <machine/cpu.h>
118#include <machine/cpufunc.h>
119#include <machine/psl.h>
120#include <machine/bootinfo.h>
121#include <machine/bus.h>
122#include <sh3/bscreg.h>
123#include <sh3/ccrreg.h>
124#include <sh3/cpgreg.h>
125#include <sh3/intcreg.h>
126#include <sh3/pfcreg.h>
127#include <sh3/wdtreg.h>
128
129#include <sys/termios.h>
130#include "sci.h"
131
132/* the following is used externally (sysctl_hw) */
133char machine[] = MACHINE;		/* cpu "architecture" */
134char machine_arch[] = MACHINE_ARCH;	/* machine_arch = "sh3" */
135
136#ifdef sh3_debug
137int cpu_debug_mode = 1;
138#else
139int cpu_debug_mode = 0;
140#endif
141
142char bootinfo[BOOTINFO_MAXSIZE];
143
144int physmem;
145int dumpmem_low;
146int dumpmem_high;
147vaddr_t atdevbase;	/* location of start of iomem in virtual */
148paddr_t msgbuf_paddr;
149struct user *proc0paddr;
150
151extern int boothowto;
152extern paddr_t avail_start, avail_end;
153
154#ifdef	SYSCALL_DEBUG
155#define	SCDEBUG_ALL 0x0004
156extern int	scdebug;
157#endif
158
159#define IOM_RAM_END	((paddr_t)IOM_RAM_BEGIN + IOM_RAM_SIZE - 1)
160
161/*
162 * Extent maps to manage I/O and ISA memory hole space.  Allocate
163 * storage for 8 regions in each, initially.  Later, ioport_malloc_safe
164 * will indicate that it's safe to use malloc() to dynamically allocate
165 * region descriptors.
166 *
167 * N.B. At least two regions are _always_ allocated from the iomem
168 * extent map; (0 -> ISA hole) and (end of ISA hole -> end of RAM).
169 *
170 * The extent maps are not static!  Machine-dependent ISA and EISA
171 * routines need access to them for bus address space allocation.
172 */
173static	long iomem_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8) / sizeof(long)];
174struct	extent *ioport_ex;
175struct	extent *iomem_ex;
176static	int ioport_malloc_safe;
177
178void setup_bootinfo __P((void));
179void dumpsys __P((void));
180void identifycpu __P((void));
181void initSH3 __P((void *));
182void InitializeSci  __P((unsigned char));
183void sh3_cache_on __P((void));
184void LoadAndReset __P((char *));
185void XLoadAndReset __P((char *));
186void Sh3Reset __P((void));
187
188#include <dev/ic/comreg.h>
189#include <dev/ic/comvar.h>
190
191void	consinit __P((void));
192
193/*
194 * Machine-dependent startup code
195 *
196 * This is called from main() in kern/main.c.
197 */
198void
199cpu_startup()
200{
201
202	sh3_startup();
203
204	/* Safe for i/o port allocation to use malloc now. */
205	ioport_malloc_safe = 1;
206
207#ifdef SYSCALL_DEBUG
208	scdebug |= SCDEBUG_ALL;
209#endif
210
211#ifdef FORCE_RB_SINGLE
212	boothowto |= RB_SINGLE;
213#endif
214}
215
216/*
217 * Info for CTL_HW
218 */
219extern	char version[];
220
221#define CPUDEBUG
222
223/*
224 * machine dependent system variables.
225 */
226int
227cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
228	int *name;
229	u_int namelen;
230	void *oldp;
231	size_t *oldlenp;
232	void *newp;
233	size_t newlen;
234	struct proc *p;
235{
236	dev_t consdev;
237	struct btinfo_bootpath *bibp;
238	struct trapframe *tf;
239	char *osimage;
240
241	/* all sysctl names at this level are terminal */
242	if (namelen != 1)
243		return (ENOTDIR);		/* overloaded */
244
245	switch (name[0]) {
246	case CPU_CONSDEV:
247		if (cn_tab != NULL)
248			consdev = cn_tab->cn_dev;
249		else
250			consdev = NODEV;
251		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
252		    sizeof consdev));
253
254	case CPU_NKPDE:
255		return (sysctl_rdint(oldp, oldlenp, newp, nkpde));
256
257	case CPU_BOOTED_KERNEL:
258	        bibp = lookup_bootinfo(BTINFO_BOOTPATH);
259	        if (!bibp)
260			return (ENOENT); /* ??? */
261		return (sysctl_rdstring(oldp, oldlenp, newp, bibp->bootpath));
262
263	case CPU_SETPRIVPROC:
264		if (newp == NULL)
265			return (0);
266
267		/* set current process to priviledged process */
268		tf = p->p_md.md_regs;
269		tf->tf_ssr |= PSL_MD;
270		return (0);
271
272	case CPU_DEBUGMODE:
273		return (sysctl_int(oldp, oldlenp, newp, newlen,
274				   &cpu_debug_mode));
275
276	case CPU_LOADANDRESET:
277		if (newp != NULL) {
278			osimage = (char *)(*(u_long *)newp);
279
280			LoadAndReset(osimage);
281			/* not reach here */
282		}
283		return (0);
284
285	default:
286		return (EOPNOTSUPP);
287	}
288	/* NOTREACHED */
289}
290
291int waittime = -1;
292int cold = 1;
293struct pcb dumppcb;
294
295void
296cpu_reboot(howto, bootstr)
297	int howto;
298	char *bootstr;
299{
300
301	if (cold) {
302		howto |= RB_HALT;
303		goto haltsys;
304	}
305
306	boothowto = howto;
307	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
308		waittime = 0;
309		vfs_shutdown();
310		/*
311		 * If we've been adjusting the clock, the todr
312		 * will be out of synch; adjust it now.
313		 */
314		/* resettodr(); */
315	}
316
317	/* Disable interrupts. */
318	splhigh();
319
320	/* Do a dump if requested. */
321	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
322		dumpsys();
323
324haltsys:
325	doshutdownhooks();
326
327	if (howto & RB_HALT) {
328		printf("\n");
329		printf("The operating system has halted.\n");
330		printf("Please press any key to reboot.\n\n");
331		cngetc();
332	}
333
334	printf("rebooting...\n");
335	cpu_reset();
336	for(;;)
337		;
338	/*NOTREACHED*/
339}
340
341/*
342 * These variables are needed by /sbin/savecore
343 */
344u_long	dumpmag = 0x8fca0101;	/* magic number */
345int 	dumpsize = 0;		/* pages */
346long	dumplo = 0; 		/* blocks */
347
348/*
349 * This is called by main to set dumplo and dumpsize.
350 * Dumps always skip the first CLBYTES of disk space
351 * in case there might be a disk label stored there.
352 * If there is extra space, put dump at the end to
353 * reduce the chance that swapping trashes it.
354 */
355void
356cpu_dumpconf()
357{
358#ifdef	TODO
359	int nblks;	/* size of dump area */
360	int maj;
361
362	if (dumpdev == NODEV)
363		return;
364	maj = major(dumpdev);
365	if (maj < 0 || maj >= nblkdev)
366		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
367	if (bdevsw[maj].d_psize == NULL)
368		return;
369	nblks = (*bdevsw[maj].d_psize)(dumpdev);
370	if (nblks <= ctod(1))
371		return;
372
373	dumpsize = btoc(IOM_END + ctob(dumpmem_high));
374
375	/* Always skip the first CLBYTES, in case there is a label there. */
376	if (dumplo < ctod(1))
377		dumplo = ctod(1);
378
379	/* Put dump at end of partition, and make it fit. */
380	if (dumpsize > dtoc(nblks - dumplo))
381		dumpsize = dtoc(nblks - dumplo);
382	if (dumplo < nblks - ctod(dumpsize))
383		dumplo = nblks - ctod(dumpsize);
384#endif
385}
386
387/*
388 * Doadump comes here after turning off memory management and
389 * getting on the dump stack, either when called above, or by
390 * the auto-restart code.
391 */
392#define BYTES_PER_DUMP  NBPG	/* must be a multiple of pagesize XXX small */
393static vaddr_t dumpspace;
394
395vaddr_t
396reserve_dumppages(p)
397	vaddr_t p;
398{
399
400	dumpspace = p;
401	return (p + BYTES_PER_DUMP);
402}
403
404void
405dumpsys()
406{
407#ifdef	TODO
408	unsigned bytes, i, n;
409	int maddr, psize;
410	daddr_t blkno;
411	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
412	int error;
413
414	/* Save registers. */
415	savectx(&dumppcb);
416
417	msgbufmapped = 0;	/* don't record dump msgs in msgbuf */
418	if (dumpdev == NODEV)
419		return;
420
421	/*
422	 * For dumps during autoconfiguration,
423	 * if dump device has already configured...
424	 */
425	if (dumpsize == 0)
426		cpu_dumpconf();
427	if (dumplo < 0)
428		return;
429	printf("\ndumping to dev %x, offset %ld\n", dumpdev, dumplo);
430
431	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
432	printf("dump ");
433	if (psize == -1) {
434		printf("area unavailable\n");
435		return;
436	}
437
438#if 0	/* XXX this doesn't work.  grr. */
439        /* toss any characters present prior to dump */
440	while (sget() != NULL); /*syscons and pccons differ */
441#endif
442
443	bytes = ctob(dumpmem_high) + IOM_END;
444	maddr = 0;
445	blkno = dumplo;
446	dump = bdevsw[major(dumpdev)].d_dump;
447	error = 0;
448	for (i = 0; i < bytes; i += n) {
449		/*
450		 * Avoid dumping the ISA memory hole, and areas that
451		 * BIOS claims aren't in low memory.
452		 */
453		if (i >= ctob(dumpmem_low) && i < IOM_END) {
454			n = IOM_END - i;
455			maddr += n;
456			blkno += btodb(n);
457			continue;
458		}
459
460		/* Print out how many MBs we to go. */
461		n = bytes - i;
462		if (n && (n % (1024*1024)) == 0)
463			printf("%d ", n / (1024 * 1024));
464
465		/* Limit size for next transfer. */
466		if (n > BYTES_PER_DUMP)
467			n =  BYTES_PER_DUMP;
468
469		(void) pmap_map(dumpspace, maddr, maddr + n, VM_PROT_READ);
470		error = (*dump)(dumpdev, blkno, (caddr_t)dumpspace, n);
471		if (error)
472			break;
473		maddr += n;
474		blkno += btodb(n);			/* XXX? */
475
476#if 0	/* XXX this doesn't work.  grr. */
477		/* operator aborting dump? */
478		if (sget() != NULL) {
479			error = EINTR;
480			break;
481		}
482#endif
483	}
484
485	switch (error) {
486
487	case ENXIO:
488		printf("device bad\n");
489		break;
490
491	case EFAULT:
492		printf("device not ready\n");
493		break;
494
495	case EINVAL:
496		printf("area improper\n");
497		break;
498
499	case EIO:
500		printf("i/o error\n");
501		break;
502
503	case EINTR:
504		printf("aborted from console\n");
505		break;
506
507	case 0:
508		printf("succeeded\n");
509		break;
510
511	default:
512		printf("error %d\n", error);
513		break;
514	}
515	printf("\n\n");
516	delay(5000000);		/* 5 seconds */
517#endif	/* TODO */
518}
519
520/*
521 * Initialize segments and descriptor tables
522 */
523#define VBRINIT		((char *)IOM_RAM_BEGIN)
524#define Trap100Vec	(VBRINIT + 0x100)
525#define Trap600Vec	(VBRINIT + 0x600)
526#define TLBVECTOR	(VBRINIT + 0x400)
527#define VADDRSTART	VM_MIN_KERNEL_ADDRESS
528
529extern int nkpde;
530extern char MonTrap100[], MonTrap100_end[];
531extern char MonTrap600[], MonTrap600_end[];
532extern char _start[], etext[], edata[], end[];
533extern char tlbmisshandler_stub[], tlbmisshandler_stub_end[];
534
535void
536initSH3(pc)
537	void *pc;	/* XXX return address */
538{
539	paddr_t avail;
540	pd_entry_t *pagedir;
541	pt_entry_t *pagetab, pte;
542	u_int sp;
543	int x;
544	char *p;
545
546	avail = sh3_round_page(end);
547
548	/* XXX nkpde = kernel page dir area (IOM_RAM_SIZE*2 Mbyte (why?)) */
549	nkpde = IOM_RAM_SIZE >> (PDSHIFT - 1);
550
551	/*
552	 * clear .bss, .common area, page dir area,
553	 *	process0 stack, page table area
554	 */
555	p = (char *)avail + (1 + UPAGES) * NBPG + NBPG * (1 + nkpde); /* XXX */
556	bzero(edata, p - edata);
557
558	/*
559	 * install trap handler
560	 */
561	bcopy(MonTrap100, Trap100Vec, MonTrap100_end - MonTrap100);
562	bcopy(MonTrap600, Trap600Vec, MonTrap600_end - MonTrap600);
563	__asm ("ldc %0, vbr" :: "r"(VBRINIT));
564
565/*
566 *                          edata  end
567 *	+-------------+------+-----+----------+-------------+------------+
568 *	| kernel text | data | bss | Page Dir | Proc0 Stack | Page Table |
569 *	+-------------+------+-----+----------+-------------+------------+
570 *                                     NBPG       USPACE    (1+nkpde)*NBPG
571 *                                                (= 4*NBPG)
572 *	Build initial page tables
573 */
574	pagedir = (void *)avail;
575	pagetab = (void *)(avail + SYSMAP);
576
577	/*
578	 *	Construct a page table directory
579	 *	In SH3 H/W does not support PTD,
580	 *	these structures are used by S/W.
581	 */
582	pte = (pt_entry_t)pagetab;
583	pte |= PG_KW | PG_V | PG_4K | PG_M | PG_N;
584	pagedir[KERNTEXTOFF >> PDSHIFT] = pte;
585
586	/* make pde for 0xd0000000, 0xd0400000, 0xd0800000,0xd0c00000,
587		0xd1000000, 0xd1400000, 0xd1800000, 0xd1c00000 */
588	pte += NBPG;
589	for (x = 0; x < nkpde; x++) {
590		pagedir[(VADDRSTART >> PDSHIFT) + x] = pte;
591		pte += NBPG;
592	}
593
594	/* Install a PDE recursively mapping page directory as a page table! */
595	pte = (u_int)pagedir;
596	pte |= PG_V | PG_4K | PG_KW | PG_M | PG_N;
597	pagedir[PDSLOT_PTE] = pte;
598
599	/* set PageDirReg */
600	SHREG_TTB = (u_int)pagedir;
601
602	/* Set TLB miss handler */
603	p = tlbmisshandler_stub;
604	x = tlbmisshandler_stub_end - p;
605	bcopy(p, TLBVECTOR, x);
606
607	/*
608	 * Activate MMU
609	 */
610
611#define MMUCR_AT	0x0001	/* address traslation enable */
612#define MMUCR_IX	0x0002	/* index mode */
613#define MMUCR_TF	0x0004	/* TLB flush */
614#define MMUCR_SV	0x0100	/* single virtual space mode */
615
616	SHREG_MMUCR = MMUCR_AT | MMUCR_TF | MMUCR_SV;
617
618	/*
619	 * Now here is virtual address
620	 */
621
622	/* Set proc0paddr */
623	proc0paddr = (void *)(avail + NBPG);
624
625	/* Set pcb->PageDirReg of proc0 */
626	proc0paddr->u_pcb.pageDirReg = (int)pagedir;
627
628	/* avail_start is first available physical memory address */
629	avail_start = avail + NBPG + USPACE + NBPG + NBPG * nkpde;
630
631	/* atdevbase is first available logical memory address */
632	atdevbase = VADDRSTART;
633
634	proc0.p_addr = proc0paddr; /* page dir address */
635
636	/* XXX: PMAP_NEW requires valid curpcb.   also init'd in cpu_startup */
637	curpcb = &proc0.p_addr->u_pcb;
638
639	/*
640	 * Initialize the I/O port and I/O mem extent maps.
641	 * Note: we don't have to check the return value since
642	 * creation of a fixed extent map will never fail (since
643	 * descriptor storage has already been allocated).
644	 *
645	 * N.B. The iomem extent manages _all_ physical addresses
646	 * on the machine.  When the amount of RAM is found, the two
647	 * extents of RAM are allocated from the map (0 -> ISA hole
648	 * and end of ISA hole -> end of RAM).
649	 */
650	iomem_ex = extent_create("iomem", 0x0, 0xffffffff, M_DEVBUF,
651	    (caddr_t)iomem_ex_storage, sizeof(iomem_ex_storage),
652	    EX_NOCOALESCE|EX_NOWAIT);
653
654#if 0
655	consinit();	/* XXX SHOULD NOT BE DONE HERE */
656#endif
657
658	splraise(-1);
659	enable_intr();
660
661	avail_end = sh3_trunc_page(IOM_RAM_END + 1);
662
663	printf("initSH3\r\n");
664
665	/*
666	 * Calculate check sum
667	 */
668    {
669	u_short *p, sum;
670	int size;
671
672	size = etext - _start;
673	p = (u_short *)_start;
674	sum = 0;
675	size >>= 1;
676	while (size--)
677		sum += *p++;
678	printf("Check Sum = 0x%x\r\n", sum);
679    }
680	/*
681	 * Allocate the physical addresses used by RAM from the iomem
682	 * extent map.  This is done before the addresses are
683	 * page rounded just to make sure we get them all.
684	 */
685	if (extent_alloc_region(iomem_ex, IOM_RAM_BEGIN,
686				(IOM_RAM_END-IOM_RAM_BEGIN) + 1,
687				EX_NOWAIT)) {
688		/* XXX What should we do? */
689		printf("WARNING: CAN'T ALLOCATE RAM MEMORY FROM IOMEM EXTENT MAP!\n");
690	}
691
692	/* number of pages of physmem addr space */
693	physmem = btoc(IOM_RAM_END - IOM_RAM_BEGIN +1);
694#ifdef	TODO
695	dumpmem = physmem;
696#endif
697
698	/*
699	 * Initialize for pmap_free_pages and pmap_next_page.
700	 * These guys should be page-aligned.
701	 */
702	if (physmem < btoc(2 * 1024 * 1024)) {
703		printf("warning: too little memory available; "
704		       "have %d bytes, want %d bytes\n"
705		       "running in degraded mode\n"
706		       "press a key to confirm\n\n",
707		       ctob(physmem), 2*1024*1024);
708		cngetc();
709	}
710
711	/* Call pmap initialization to make new kernel address space */
712	pmap_bootstrap(atdevbase);
713
714	/*
715	 * Initialize error message buffer (at end of core).
716	 */
717	initmsgbuf((caddr_t)msgbuf_paddr, round_page(MSGBUFSIZE));
718
719	/*
720	 * set boot device information
721	 */
722	setup_bootinfo();
723
724#if 0
725	sh3_cache_on();
726#endif
727
728	/* setup proc0 stack */
729	sp = avail + NBPG + USPACE - 16 - sizeof(struct trapframe);
730
731	/*
732	 * XXX We can't return here, because we change stack pointer.
733	 *     So jump to return address directly.
734	 */
735	__asm __volatile ("jmp @%0; mov %1, r15" :: "r"(pc), "r"(sp));
736}
737
738void
739setup_bootinfo(void)
740{
741	struct btinfo_bootdisk *help;
742
743	*(int *)bootinfo = 1;
744	help = (struct btinfo_bootdisk *)(bootinfo + sizeof(int));
745	help->biosdev = 0;
746	help->partition = 0;
747	((struct btinfo_common *)help)->len = sizeof(struct btinfo_bootdisk);
748	((struct btinfo_common *)help)->type = BTINFO_BOOTDISK;
749}
750
751void *
752lookup_bootinfo(type)
753	int type;
754{
755	struct btinfo_common *help;
756	int n = *(int*)bootinfo;
757	help = (struct btinfo_common *)(bootinfo + sizeof(int));
758	while (n--) {
759		if (help->type == type)
760			return (help);
761		help = (struct btinfo_common *)((char*)help + help->len);
762	}
763	return (0);
764}
765
766
767/*
768 * consinit:
769 * initialize the system console.
770 * XXX - shouldn't deal with this initted thing, but then,
771 * it shouldn't be called from init386 either.
772 */
773void
774consinit()
775{
776	static int initted;
777
778	if (initted)
779		return;
780	initted = 1;
781
782	cninit();
783
784#ifdef DDB
785	ddb_init();
786#endif
787}
788
789void
790cpu_reset()
791{
792
793	disable_intr();
794
795	Sh3Reset();
796	for (;;)
797		;
798}
799
800int
801bus_space_map (t, addr, size, flags, bshp)
802	bus_space_tag_t t;
803	bus_addr_t addr;
804	bus_size_t size;
805	int flags;
806	bus_space_handle_t *bshp;
807{
808
809	*bshp = (bus_space_handle_t)addr;
810
811	return 0;
812}
813
814int
815sh_memio_subregion(t, bsh, offset, size, nbshp)
816	bus_space_tag_t t;
817	bus_space_handle_t bsh;
818	bus_size_t offset, size;
819	bus_space_handle_t *nbshp;
820{
821
822	*nbshp = bsh + offset;
823	return (0);
824}
825
826int
827sh_memio_alloc(t, rstart, rend, size, alignment, boundary, flags,
828	       bpap, bshp)
829	bus_space_tag_t t;
830	bus_addr_t rstart, rend;
831	bus_size_t size, alignment, boundary;
832	int flags;
833	bus_addr_t *bpap;
834	bus_space_handle_t *bshp;
835{
836	*bshp = *bpap = rstart;
837
838	return (0);
839}
840
841void
842sh_memio_free(t, bsh, size)
843	bus_space_tag_t t;
844	bus_space_handle_t bsh;
845	bus_size_t size;
846{
847
848}
849
850void
851sh_memio_unmap(t, bsh, size)
852	bus_space_tag_t t;
853	bus_space_handle_t bsh;
854	bus_size_t size;
855{
856	return;
857}
858
859/*
860 * InitializeBsc
861 * : BSC(Bus State Controler)
862 */
863void InitializeBsc __P((void));
864
865void
866InitializeBsc()
867{
868
869	/*
870	 * Drive RAS,CAS in stand by mode and bus release mode
871	 * Area0 = Normal memory, Area5,6=Normal(no burst)
872	 * Area2 = Normal memory, Area3 = SDRAM, Area5 = Normal memory
873	 * Area4 = Normal Memory
874	 * Area6 = Normal memory
875	 */
876	SHREG_BCR1 = BSC_BCR1_VAL;
877
878	/*
879	 * Bus Width
880	 * Area4: Bus width = 16bit
881	 * Area6,5 = 16bit
882	 * Area1 = 8bit
883	 * Area2,3: Bus width = 32bit
884	 */
885	 SHREG_BCR2 = BSC_BCR2_VAL;
886
887	/*
888	 * Idle cycle number in transition area and read to write
889	 * Area6 = 3, Area5 = 3, Area4 = 3, Area3 = 3, Area2 = 3
890	 * Area1 = 3, Area0 = 3
891	 */
892	SHREG_WCR1 = BSC_WCR1_VAL;
893
894	/*
895	 * Wait cycle
896	 * Area 6 = 6
897	 * Area 5 = 2
898	 * Area 4 = 10
899	 * Area 3 = 3
900	 * Area 2,1 = 3
901	 * Area 0 = 6
902	 */
903	SHREG_WCR2 = BSC_WCR2_VAL;
904
905#ifdef SH4
906	SHREG_WCR3 = BSC_WCR3_VAL;
907#endif
908
909	/*
910	 * RAS pre-charge = 2cycle, RAS-CAS delay = 3 cycle,
911	 * write pre-charge=1cycle
912	 * CAS before RAS refresh RAS assert time = 3 cycle
913	 * Disable burst, Bus size=32bit, Column Address=10bit, Refresh ON
914	 * CAS before RAS refresh ON, EDO DRAM
915	 */
916	SHREG_MCR = BSC_MCR_VAL;
917
918#ifdef BSC_SDMR_VAL
919#if 1
920#define SDMR	(*(volatile unsigned char  *)BSC_SDMR_VAL)
921
922	SDMR = 0;
923#else
924#define ADDSET	(*(volatile unsigned short *)0x1A000000)
925#define ADDRST	(*(volatile unsigned short *)0x18000000)
926#define SDMR	(*(volatile unsigned char  *)BSC_SDMR_VAL)
927
928	ADDSET = 0;
929	SDMR = 0;
930	ADDRST = 0;
931#endif
932#endif
933
934	/*
935	 * PCMCIA Control Register
936	 * OE/WE assert delay 3.5 cycle
937	 * OE/WE negate-address delay 3.5 cycle
938	 */
939#ifdef BSC_PCR_VAL
940	SHREG_PCR = 0x00ff;
941#endif
942
943	/*
944	 * Refresh Timer Control/Status Register
945	 * Disable interrupt by CMF, closk 1/16, Disable OVF interrupt
946	 * Count Limit = 1024
947	 * In following statement, the reason why high byte = 0xa5(a4 in RFCR)
948	 * is the rule of SH3 in writing these register.
949	 */
950	SHREG_RTCSR = BSC_RTCSR_VAL;
951
952
953	/*
954	 * Refresh Timer Counter
955	 * Initialize to 0
956	 */
957	SHREG_RTCNT = BSC_RTCNT_VAL;
958
959	/* set Refresh Time Constant Register */
960	SHREG_RTCOR = BSC_RTCOR_VAL;
961
962	/* init Refresh Count Register */
963#ifdef BSC_RFCR_VAL
964	SHREG_RFCR = BSC_RFCR_VAL;
965#endif
966
967	/* Set Clock mode (make internal clock double speed) */
968
969	SHREG_FRQCR = FRQCR_VAL;
970
971#ifndef MMEYE_NO_CACHE
972	/* Cache ON */
973	SHREG_CCR = CCR_CE;
974#endif
975}
976
977void
978sh3_cache_on(void)
979{
980#ifndef MMEYE_NO_CACHE
981	/* Cache ON */
982	SHREG_CCR = CCR_CE;
983	SHREG_CCR = CCR_CF | CCR_CE;	/* cache clear */
984	SHREG_CCR = CCR_CE;		/* cache on */
985#endif
986}
987
988#include <machine/mmeye.h>
989
990 /* XXX This value depends on physical available memory */
991#define OSIMAGE_BUF_ADDR	(IOM_RAM_BEGIN + 0x00400000)
992
993void
994LoadAndReset(osimage)
995	char *osimage;
996{
997	void *buf_addr;
998	u_long size;
999	u_long *src;
1000	u_long *dest;
1001	u_long csum = 0;
1002	u_long csum2 = 0;
1003	u_long size2;
1004
1005	printf("LoadAndReset: copy start\n");
1006	buf_addr = (void *)OSIMAGE_BUF_ADDR;
1007
1008	size = *(u_long *)osimage;
1009	src = (u_long *)osimage;
1010	dest = buf_addr;
1011
1012	size = (size + sizeof(u_long) * 2 + 3) >> 2;
1013	size2 = size;
1014
1015	while (size--) {
1016		csum += *src;
1017		*dest++ = *src++;
1018	}
1019
1020	dest = buf_addr;
1021	while (size2--)
1022		csum2 += *dest++;
1023
1024	printf("LoadAndReset: copy end[%lx,%lx]\n", csum, csum2);
1025	printf("start XLoadAndReset\n");
1026
1027	/* mask all externel interrupt (XXX) */
1028
1029	XLoadAndReset(buf_addr);
1030}
1031