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_object.h>
58 #include <vm/vm_page.h>
60 #include <machine/clock.h>
61 #include <machine/cpu.h>
62 #include <machine/cpuregs.h>
63 #include <machine/hwfunc.h>
64 #include <machine/md_var.h>
65 #include <machine/pmap.h>
66 #include <machine/trap.h>
68 #ifdef TICK_USE_YAMON_FREQ
69 #include <mips/malta/yamon.h>
72 #ifdef TICK_USE_MALTA_RTC
73 #include <mips/mips4k/malta/maltareg.h>
74 #include <dev/mc146818/mc146818reg.h>
78 #include <mips/malta/maltareg.h>
84 void lcd_puts(char *);
85 void malta_reset(void);
88 * Temporary boot environment used at startup.
90 static char boot1_env[4096];
93 * Offsets to MALTA LCD characters.
95 static int malta_lcd_offs[] = {
109 /* Nothing special */
113 * Put character to Malta LCD at given position.
116 malta_lcd_putc(int pos, char c)
121 if (pos < 0 || pos > 7)
123 addr = (void *)(MALTA_ASCII_BASE + malta_lcd_offs[pos]);
124 ch = (char *)MIPS_PHYS_TO_KSEG0(addr);
129 * Print given string on LCD.
132 malta_lcd_print(char *str)
139 for (i = 0; *str != '\0'; i++, str++)
140 malta_lcd_putc(i, *str);
146 malta_lcd_print("FreeBSD_");
155 #ifdef TICK_USE_MALTA_RTC
156 static __inline uint8_t
160 *((volatile uint8_t *)
161 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
162 return (*((volatile uint8_t *)
163 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))));
167 writertc(uint8_t addr, uint8_t val)
170 *((volatile uint8_t *)
171 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
172 *((volatile uint8_t *)
173 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))) = val;
178 mips_init(unsigned long memsize, uint64_t ememsize)
182 for (i = 0; i < PHYS_AVAIL_ENTRIES; i++) {
187 * memsize is the amount of RAM available below 256MB.
188 * ememsize is the total amount of RAM available.
190 * The second bank starts at 0x90000000.
193 /* phys_avail regions are in bytes */
194 phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
195 phys_avail[1] = memsize;
197 dump_avail[1] = phys_avail[1];
199 /* Only specify the extended region if it's set */
200 if (ememsize > memsize) {
201 phys_avail[2] = 0x90000000;
202 phys_avail[3] = 0x90000000 + (ememsize - memsize);
203 dump_avail[2] = phys_avail[2];
204 dump_avail[3] = phys_avail[3];
207 /* XXX realmem assigned in the caller of mips_init() */
211 init_param2(physmem);
218 if (boothowto & RB_KDB)
219 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
224 * Perform a board-level soft-reset.
225 * Note that this is not emulated by gxemul.
232 c = (char *)MIPS_PHYS_TO_KSEG0(MALTA_SOFTRES);
239 uint64_t platform_counter_freq = 0;
241 #if defined(TICK_USE_YAMON_FREQ)
243 * If we are running on a board which uses YAMON firmware,
244 * then query CPU pipeline clock from the syscon object.
245 * If unsuccessful, use hard-coded default.
247 platform_counter_freq = yamon_getcpufreq();
249 #elif defined(TICK_USE_MALTA_RTC)
251 * If we are running on a board with the MC146818 RTC,
252 * use it to determine CPU pipeline clock frequency.
254 u_int64_t counterval[2];
256 /* Set RTC to binary mode. */
257 writertc(RTC_STATUSB, (rtcin(RTC_STATUSB) | RTCSB_BCD));
259 /* Busy-wait for falling edge of RTC update. */
260 while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
262 while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
264 counterval[0] = mips_rd_count();
266 /* Busy-wait for falling edge of RTC update. */
267 while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
269 while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
271 counterval[1] = mips_rd_count();
273 platform_counter_freq = counterval[1] - counterval[0];
276 if (platform_counter_freq == 0)
277 platform_counter_freq = MIPS_DEFAULT_HZ;
279 return (platform_counter_freq);
283 platform_start(__register_t a0, __register_t a1, __register_t a2,
287 uint64_t platform_counter_freq;
289 int32_t *argv = (int32_t*)a1;
290 int32_t *envp = (int32_t*)a2;
291 unsigned int memsize = a3;
292 uint64_t ememsize = 0;
295 /* clear the BSS and SBSS segments */
296 kernend = (vm_offset_t)&end;
297 memset(&edata, 0, kernend - (vm_offset_t)(&edata));
299 mips_postboot_fixup();
302 platform_counter_freq = malta_cpu_freq();
303 mips_timer_early_init(platform_counter_freq);
304 init_static_kenv(boot1_env, sizeof(boot1_env));
307 printf("entry: platform_start()\n");
312 * YAMON uses 32bit pointers to strings so
313 * convert them to proper type manually
317 printf("cmd line: ");
318 for (i = 0; i < argc; i++)
319 printf("%s ", (char*)(intptr_t)argv[i]);
327 * Parse the environment for things like ememsize.
329 for (i = 0; envp[i]; i += 2) {
332 a = (char *)(intptr_t)envp[i];
333 v = (char *)(intptr_t)envp[i+1];
336 printf("\t%s = %s\n", a, v);
338 if (strcmp(a, "ememsize") == 0) {
339 ememsize = strtoul(v, NULL, 0);
344 printf("memsize = %llu (0x%08x)\n",
345 (unsigned long long) memsize, memsize);
346 printf("ememsize = %llu\n", (unsigned long long) ememsize);
350 * For O32 phys_avail[] can't address memory beyond 2^32,
351 * so cap extended memory to 2GB minus one page.
353 if (ememsize >= 2ULL * 1024 * 1024 * 1024)
354 ememsize = 2ULL * 1024 * 1024 * 1024 - PAGE_SIZE;
359 * For <= 256MB RAM amounts, ememsize should equal memsize.
360 * For > 256MB RAM amounts it's the total RAM available;
361 * split between two banks.
363 * XXX TODO: just push realmem assignment into mips_init() ?
365 realmem = btoc(ememsize);
366 mips_init(memsize, ememsize);
368 mips_timer_init_params(platform_counter_freq, 0);