1178173Simp/*-
2178173Simp * Copyright (c) 2007 Bruce M. Simpson.
3178173Simp * All rights reserved.
4178173Simp *
5178173Simp * Redistribution and use in source and binary forms, with or without
6178173Simp * modification, are permitted provided that the following conditions
7178173Simp * are met:
8178173Simp * 1. Redistributions of source code must retain the above copyright
9178173Simp *    notice, this list of conditions and the following disclaimer.
10178173Simp * 2. Redistributions in binary form must reproduce the above copyright
11178173Simp *    notice, this list of conditions and the following disclaimer in the
12178173Simp *    documentation and/or other materials provided with the distribution.
13178173Simp *
14178173Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15178173Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16178173Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17178173Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18178173Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19178173Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20178173Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21178173Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22178173Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23178173Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24178173Simp * SUCH DAMAGE.
25178173Simp */
26178173Simp
27178173Simp#include <sys/cdefs.h>
28178173Simp__FBSDID("$FreeBSD$");
29178173Simp
30178173Simp#include "opt_ddb.h"
31178173Simp
32178173Simp#include <sys/param.h>
33178173Simp#include <sys/conf.h>
34178173Simp#include <sys/kernel.h>
35178173Simp#include <sys/systm.h>
36178173Simp#include <sys/imgact.h>
37178173Simp#include <sys/bio.h>
38178173Simp#include <sys/buf.h>
39178173Simp#include <sys/bus.h>
40178173Simp#include <sys/cpu.h>
41178173Simp#include <sys/cons.h>
42178173Simp#include <sys/exec.h>
43178173Simp#include <sys/ucontext.h>
44178173Simp#include <sys/proc.h>
45178173Simp#include <sys/kdb.h>
46178173Simp#include <sys/ptrace.h>
47178173Simp#include <sys/reboot.h>
48178173Simp#include <sys/signalvar.h>
49178173Simp#include <sys/sysent.h>
50178173Simp#include <sys/sysproto.h>
51178173Simp#include <sys/user.h>
52178173Simp
53178173Simp#include <vm/vm.h>
54178173Simp#include <vm/vm_object.h>
55178173Simp#include <vm/vm_page.h>
56178173Simp
57178173Simp#include <machine/cache.h>
58178173Simp#include <machine/clock.h>
59178173Simp#include <machine/cpu.h>
60178173Simp#include <machine/cpuinfo.h>
61178173Simp#include <machine/cpufunc.h>
62178173Simp#include <machine/cpuregs.h>
63178173Simp#include <machine/hwfunc.h>
64178173Simp#include <machine/intr_machdep.h>
65178173Simp#include <machine/locore.h>
66178173Simp#include <machine/md_var.h>
67178173Simp#include <machine/pte.h>
68178173Simp#include <machine/sigframe.h>
69178173Simp#include <machine/trap.h>
70178173Simp#include <machine/vmparam.h>
71178173Simp
72223562Skevlo#include <mips/sentry5/s5reg.h>
73223562Skevlo
74178173Simp#ifdef CFE
75178173Simp#include <dev/cfe/cfe_api.h>
76178173Simp#endif
77178173Simp
78178173Simpextern int *edata;
79178173Simpextern int *end;
80178173Simp
81202036Simpvoid
82202036Simpplatform_cpu_init()
83202036Simp{
84202036Simp	/* Nothing special */
85202036Simp}
86202036Simp
87178173Simpstatic void
88178173Simpmips_init(void)
89178173Simp{
90216318Sgonzo	int i, j;
91178173Simp
92178173Simp	printf("entry: mips_init()\n");
93178173Simp
94178173Simp#ifdef CFE
95178173Simp	/*
96178173Simp	 * Query DRAM memory map from CFE.
97178173Simp	 */
98178173Simp	physmem = 0;
99178173Simp	for (i = 0; i < 10; i += 2) {
100178173Simp		int result;
101178173Simp		uint64_t addr, len, type;
102178173Simp
103178173Simp		result = cfe_enummem(i, 0, &addr, &len, &type);
104178173Simp		if (result < 0) {
105178173Simp			phys_avail[i] = phys_avail[i + 1] = 0;
106178173Simp			break;
107178173Simp		}
108178173Simp		if (type != CFE_MI_AVAILABLE)
109178173Simp			continue;
110178173Simp
111178173Simp		phys_avail[i] = addr;
112178173Simp		if (i == 0 && addr == 0) {
113178173Simp			/*
114178173Simp			 * If this is the first physical memory segment probed
115178173Simp			 * from CFE, omit the region at the start of physical
116178173Simp			 * memory where the kernel has been loaded.
117178173Simp			 */
118202954Sgonzo			phys_avail[i] += MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
119178173Simp		}
120178173Simp		phys_avail[i + 1] = addr + len;
121178173Simp		physmem += len;
122178173Simp	}
123178173Simp
124178173Simp	realmem = btoc(physmem);
125178173Simp#endif
126178173Simp
127216320Sgonzo	for (j = 0; j < i; j++)
128216318Sgonzo		dump_avail[j] = phys_avail[j];
129216318Sgonzo
130178173Simp	physmem = realmem;
131178173Simp
132178173Simp	init_param1();
133178173Simp	init_param2(physmem);
134178173Simp	mips_cpu_init();
135178173Simp	pmap_bootstrap();
136178173Simp	mips_proc0_init();
137178173Simp	mutex_init();
138178173Simp	kdb_init();
139202849Simp#ifdef KDB
140202849Simp	if (boothowto & RB_KDB)
141202849Simp		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
142178173Simp#endif
143178173Simp}
144178173Simp
145178173Simpvoid
146178173Simpplatform_reset(void)
147178173Simp{
148178173Simp
149178173Simp#if defined(CFE)
150178173Simp	cfe_exit(0, 0);
151178173Simp#else
152178173Simp	*((volatile uint8_t *)MIPS_PHYS_TO_KSEG1(SENTRY5_EXTIFADR)) = 0x80;
153178173Simp#endif
154178173Simp}
155178173Simp
156178173Simpvoid
157202036Simpplatform_start(__register_t a0, __register_t a1, __register_t a2,
158202036Simp	       __register_t a3)
159178173Simp{
160178173Simp	vm_offset_t kernend;
161178173Simp	uint64_t platform_counter_freq;
162178173Simp
163178173Simp	/* clear the BSS and SBSS segments */
164202954Sgonzo	kernend = (vm_offset_t)&end;
165178173Simp	memset(&edata, 0, kernend - (vm_offset_t)(&edata));
166178173Simp
167202954Sgonzo	mips_postboot_fixup();
168202954Sgonzo
169202036Simp	/* Initialize pcpu stuff */
170202036Simp	mips_pcpu0_init();
171202036Simp
172178173Simp#ifdef CFE
173178173Simp	/*
174178173Simp	 * Initialize CFE firmware trampolines before
175178173Simp	 * we initialize the low-level console.
176202036Simp	 *
177202036Simp	 * CFE passes the following values in registers:
178202036Simp	 * a0: firmware handle
179202036Simp	 * a2: firmware entry point
180202036Simp	 * a3: entry point seal
181178173Simp	 */
182202036Simp	if (a3 == CFE_EPTSEAL)
183202036Simp		cfe_init(a0, a2);
184178173Simp#endif
185178173Simp	cninit();
186178173Simp
187178173Simp	mips_init();
188178173Simp
189178173Simp# if 0
190178173Simp	/*
191178173Simp	 * Probe the Broadcom Sentry5's on-chip PLL clock registers
192178173Simp	 * and discover the CPU pipeline clock and bus clock
193178173Simp	 * multipliers from this.
194178173Simp	 * XXX: Wrong place. You have to ask the ChipCommon
195178173Simp	 * or External Interface cores on the SiBa.
196178173Simp	 */
197178173Simp	uint32_t busmult, cpumult, refclock, clkcfg1;
198178173Simp#define S5_CLKCFG1_REFCLOCK_MASK	0x0000001F
199178173Simp#define S5_CLKCFG1_BUSMULT_MASK		0x000003E0
200178173Simp#define S5_CLKCFG1_BUSMULT_SHIFT	5
201178173Simp#define S5_CLKCFG1_CPUMULT_MASK		0xFFFFFC00
202178173Simp#define S5_CLKCFG1_CPUMULT_SHIFT	10
203178173Simp
204178173Simp	counter_freq = 100000000;	/* XXX */
205178173Simp
206178173Simp	clkcfg1 = s5_rd_clkcfg1();
207178173Simp	printf("clkcfg1 = 0x%08x\n", clkcfg1);
208178173Simp
209178173Simp	refclock = clkcfg1 & 0x1F;
210178173Simp	busmult = ((clkcfg1 & 0x000003E0) >> 5) + 1;
211178173Simp	cpumult = ((clkcfg1 & 0xFFFFFC00) >> 10) + 1;
212178173Simp
213178173Simp	printf("refclock = %u\n", refclock);
214178173Simp	printf("busmult = %u\n", busmult);
215178173Simp	printf("cpumult = %u\n", cpumult);
216178173Simp
217178173Simp	counter_freq = cpumult * refclock;
218178173Simp# else
219178173Simp	platform_counter_freq = 200 * 1000 * 1000; /* Sentry5 is 200MHz */
220178173Simp# endif
221178173Simp
222178173Simp	mips_timer_init_params(platform_counter_freq, 0);
223178173Simp}
224