2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Wojciech A. Koszek <wkoszek@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/imgact.h>
46 #include <sys/ucontext.h>
49 #include <sys/ptrace.h>
50 #include <sys/reboot.h>
51 #include <sys/signalvar.h>
52 #include <sys/sysent.h>
53 #include <sys/sysproto.h>
57 #include <vm/vm_param.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_phys.h>
61 #include <vm/vm_dumpset.h>
63 #include <machine/clock.h>
64 #include <machine/cpu.h>
65 #include <machine/cpuregs.h>
66 #include <machine/hwfunc.h>
67 #include <machine/md_var.h>
68 #include <machine/pmap.h>
69 #include <machine/trap.h>
71 #ifdef TICK_USE_YAMON_FREQ
72 #include <mips/malta/yamon.h>
75 #ifdef TICK_USE_MALTA_RTC
76 #include <mips/mips4k/malta/maltareg.h>
77 #include <dev/mc146818/mc146818reg.h>
81 #include <mips/malta/maltareg.h>
87 void lcd_puts(char *);
88 void malta_reset(void);
91 * Temporary boot environment used at startup.
93 static char boot1_env[4096];
96 * Offsets to MALTA LCD characters.
98 static int malta_lcd_offs[] = {
112 /* Nothing special */
116 * Put character to Malta LCD at given position.
119 malta_lcd_putc(int pos, char c)
124 if (pos < 0 || pos > 7)
126 addr = (void *)(MALTA_ASCII_BASE + malta_lcd_offs[pos]);
127 ch = (char *)MIPS_PHYS_TO_KSEG0(addr);
132 * Print given string on LCD.
135 malta_lcd_print(char *str)
142 for (i = 0; *str != '\0'; i++, str++)
143 malta_lcd_putc(i, *str);
149 malta_lcd_print("FreeBSD_");
158 #ifdef TICK_USE_MALTA_RTC
159 static __inline uint8_t
163 *((volatile uint8_t *)
164 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
165 return (*((volatile uint8_t *)
166 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))));
170 writertc(uint8_t addr, uint8_t val)
173 *((volatile uint8_t *)
174 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
175 *((volatile uint8_t *)
176 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))) = val;
181 mips_init(unsigned long memsize, uint64_t ememsize)
185 for (i = 0; i < PHYS_AVAIL_ENTRIES; i++) {
190 * memsize is the amount of RAM available below 256MB.
191 * ememsize is the total amount of RAM available.
193 * The second bank starts at 0x90000000.
196 /* phys_avail regions are in bytes */
197 phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
198 phys_avail[1] = memsize;
200 dump_avail[1] = phys_avail[1];
202 /* Only specify the extended region if it's set */
203 if (ememsize > memsize) {
204 phys_avail[2] = 0x90000000;
205 phys_avail[3] = 0x90000000 + (ememsize - memsize);
206 dump_avail[2] = phys_avail[2];
207 dump_avail[3] = phys_avail[3];
210 /* XXX realmem assigned in the caller of mips_init() */
214 init_param2(physmem);
221 if (boothowto & RB_KDB)
222 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
227 * Perform a board-level soft-reset.
228 * Note that this is not emulated by gxemul.
235 c = (char *)MIPS_PHYS_TO_KSEG0(MALTA_SOFTRES);
242 uint64_t platform_counter_freq = 0;
244 #if defined(TICK_USE_YAMON_FREQ)
246 * If we are running on a board which uses YAMON firmware,
247 * then query CPU pipeline clock from the syscon object.
248 * If unsuccessful, use hard-coded default.
250 platform_counter_freq = yamon_getcpufreq();
252 #elif defined(TICK_USE_MALTA_RTC)
254 * If we are running on a board with the MC146818 RTC,
255 * use it to determine CPU pipeline clock frequency.
257 u_int64_t counterval[2];
259 /* Set RTC to binary mode. */
260 writertc(RTC_STATUSB, (rtcin(RTC_STATUSB) | RTCSB_BCD));
262 /* Busy-wait for falling edge of RTC update. */
263 while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
265 while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
267 counterval[0] = mips_rd_count();
269 /* Busy-wait for falling edge of RTC update. */
270 while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
272 while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
274 counterval[1] = mips_rd_count();
276 platform_counter_freq = counterval[1] - counterval[0];
279 if (platform_counter_freq == 0)
280 platform_counter_freq = MIPS_DEFAULT_HZ;
282 return (platform_counter_freq);
286 platform_start(__register_t a0, __register_t a1, __register_t a2,
290 uint64_t platform_counter_freq;
292 int32_t *argv = (int32_t*)a1;
293 int32_t *envp = (int32_t*)a2;
294 unsigned int memsize = a3;
295 uint64_t ememsize = 0;
298 /* clear the BSS and SBSS segments */
299 kernend = (vm_offset_t)&end;
300 memset(&edata, 0, kernend - (vm_offset_t)(&edata));
302 mips_postboot_fixup();
305 platform_counter_freq = malta_cpu_freq();
306 mips_timer_early_init(platform_counter_freq);
307 init_static_kenv(boot1_env, sizeof(boot1_env));
310 printf("entry: platform_start()\n");
315 * YAMON uses 32bit pointers to strings so
316 * convert them to proper type manually
320 printf("cmd line: ");
321 for (i = 0; i < argc; i++)
322 printf("%s ", (char*)(intptr_t)argv[i]);
330 * Parse the environment for things like ememsize.
332 for (i = 0; envp[i]; i += 2) {
335 a = (char *)(intptr_t)envp[i];
336 v = (char *)(intptr_t)envp[i+1];
339 printf("\t%s = %s\n", a, v);
341 if (strcmp(a, "ememsize") == 0) {
342 ememsize = strtoul(v, NULL, 0);
347 printf("memsize = %llu (0x%08x)\n",
348 (unsigned long long) memsize, memsize);
349 printf("ememsize = %llu\n", (unsigned long long) ememsize);
353 * For O32 phys_avail[] can't address memory beyond 2^32,
354 * so cap extended memory to 2GB minus one page.
356 if (ememsize >= 2ULL * 1024 * 1024 * 1024)
357 ememsize = 2ULL * 1024 * 1024 * 1024 - PAGE_SIZE;
362 * For <= 256MB RAM amounts, ememsize should equal memsize.
363 * For > 256MB RAM amounts it's the total RAM available;
364 * split between two banks.
366 * XXX TODO: just push realmem assignment into mips_init() ?
368 realmem = btoc(ememsize);
369 mips_init(memsize, ememsize);
371 mips_timer_init_params(platform_counter_freq, 0);