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>
62 #include <machine/clock.h>
63 #include <machine/cpu.h>
64 #include <machine/cpuregs.h>
65 #include <machine/hwfunc.h>
66 #include <machine/md_var.h>
67 #include <machine/pmap.h>
68 #include <machine/trap.h>
70 #ifdef TICK_USE_YAMON_FREQ
71 #include <mips/malta/yamon.h>
74 #ifdef TICK_USE_MALTA_RTC
75 #include <mips/mips4k/malta/maltareg.h>
76 #include <dev/mc146818/mc146818reg.h>
80 #include <mips/malta/maltareg.h>
86 void lcd_puts(char *);
87 void malta_reset(void);
90 * Temporary boot environment used at startup.
92 static char boot1_env[4096];
95 * Offsets to MALTA LCD characters.
97 static int malta_lcd_offs[] = {
111 /* Nothing special */
115 * Put character to Malta LCD at given position.
118 malta_lcd_putc(int pos, char c)
123 if (pos < 0 || pos > 7)
125 addr = (void *)(MALTA_ASCII_BASE + malta_lcd_offs[pos]);
126 ch = (char *)MIPS_PHYS_TO_KSEG0(addr);
131 * Print given string on LCD.
134 malta_lcd_print(char *str)
141 for (i = 0; *str != '\0'; i++, str++)
142 malta_lcd_putc(i, *str);
148 malta_lcd_print("FreeBSD_");
157 #ifdef TICK_USE_MALTA_RTC
158 static __inline uint8_t
162 *((volatile uint8_t *)
163 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
164 return (*((volatile uint8_t *)
165 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))));
169 writertc(uint8_t addr, uint8_t val)
172 *((volatile uint8_t *)
173 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
174 *((volatile uint8_t *)
175 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))) = val;
180 mips_init(unsigned long memsize, uint64_t ememsize)
184 for (i = 0; i < PHYS_AVAIL_ENTRIES; i++) {
189 * memsize is the amount of RAM available below 256MB.
190 * ememsize is the total amount of RAM available.
192 * The second bank starts at 0x90000000.
195 /* phys_avail regions are in bytes */
196 phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
197 phys_avail[1] = memsize;
199 dump_avail[1] = phys_avail[1];
201 /* Only specify the extended region if it's set */
202 if (ememsize > memsize) {
203 phys_avail[2] = 0x90000000;
204 phys_avail[3] = 0x90000000 + (ememsize - memsize);
205 dump_avail[2] = phys_avail[2];
206 dump_avail[3] = phys_avail[3];
209 /* XXX realmem assigned in the caller of mips_init() */
213 init_param2(physmem);
220 if (boothowto & RB_KDB)
221 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
226 * Perform a board-level soft-reset.
227 * Note that this is not emulated by gxemul.
234 c = (char *)MIPS_PHYS_TO_KSEG0(MALTA_SOFTRES);
241 uint64_t platform_counter_freq = 0;
243 #if defined(TICK_USE_YAMON_FREQ)
245 * If we are running on a board which uses YAMON firmware,
246 * then query CPU pipeline clock from the syscon object.
247 * If unsuccessful, use hard-coded default.
249 platform_counter_freq = yamon_getcpufreq();
251 #elif defined(TICK_USE_MALTA_RTC)
253 * If we are running on a board with the MC146818 RTC,
254 * use it to determine CPU pipeline clock frequency.
256 u_int64_t counterval[2];
258 /* Set RTC to binary mode. */
259 writertc(RTC_STATUSB, (rtcin(RTC_STATUSB) | RTCSB_BCD));
261 /* Busy-wait for falling edge of RTC update. */
262 while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
264 while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
266 counterval[0] = mips_rd_count();
268 /* Busy-wait for falling edge of RTC update. */
269 while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
271 while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
273 counterval[1] = mips_rd_count();
275 platform_counter_freq = counterval[1] - counterval[0];
278 if (platform_counter_freq == 0)
279 platform_counter_freq = MIPS_DEFAULT_HZ;
281 return (platform_counter_freq);
285 platform_start(__register_t a0, __register_t a1, __register_t a2,
289 uint64_t platform_counter_freq;
291 int32_t *argv = (int32_t*)a1;
292 int32_t *envp = (int32_t*)a2;
293 unsigned int memsize = a3;
294 uint64_t ememsize = 0;
297 /* clear the BSS and SBSS segments */
298 kernend = (vm_offset_t)&end;
299 memset(&edata, 0, kernend - (vm_offset_t)(&edata));
301 mips_postboot_fixup();
304 platform_counter_freq = malta_cpu_freq();
305 mips_timer_early_init(platform_counter_freq);
306 init_static_kenv(boot1_env, sizeof(boot1_env));
309 printf("entry: platform_start()\n");
314 * YAMON uses 32bit pointers to strings so
315 * convert them to proper type manually
319 printf("cmd line: ");
320 for (i = 0; i < argc; i++)
321 printf("%s ", (char*)(intptr_t)argv[i]);
329 * Parse the environment for things like ememsize.
331 for (i = 0; envp[i]; i += 2) {
334 a = (char *)(intptr_t)envp[i];
335 v = (char *)(intptr_t)envp[i+1];
338 printf("\t%s = %s\n", a, v);
340 if (strcmp(a, "ememsize") == 0) {
341 ememsize = strtoul(v, NULL, 0);
346 printf("memsize = %llu (0x%08x)\n",
347 (unsigned long long) memsize, memsize);
348 printf("ememsize = %llu\n", (unsigned long long) ememsize);
352 * For O32 phys_avail[] can't address memory beyond 2^32,
353 * so cap extended memory to 2GB minus one page.
355 if (ememsize >= 2ULL * 1024 * 1024 * 1024)
356 ememsize = 2ULL * 1024 * 1024 * 1024 - PAGE_SIZE;
361 * For <= 256MB RAM amounts, ememsize should equal memsize.
362 * For > 256MB RAM amounts it's the total RAM available;
363 * split between two banks.
365 * XXX TODO: just push realmem assignment into mips_init() ?
367 realmem = btoc(ememsize);
368 mips_init(memsize, ememsize);
370 mips_timer_init_params(platform_counter_freq, 0);