2 * Copyright (c) 2006 Wojciech A. Koszek <wkoszek@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/imgact.h>
44 #include <sys/ucontext.h>
47 #include <sys/ptrace.h>
48 #include <sys/reboot.h>
49 #include <sys/signalvar.h>
50 #include <sys/sysent.h>
51 #include <sys/sysproto.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
58 #include <machine/clock.h>
59 #include <machine/cpu.h>
60 #include <machine/cpuregs.h>
61 #include <machine/hwfunc.h>
62 #include <machine/md_var.h>
63 #include <machine/pmap.h>
64 #include <machine/trap.h>
66 #ifdef TICK_USE_YAMON_FREQ
67 #include <mips/malta/yamon.h>
70 #ifdef TICK_USE_MALTA_RTC
71 #include <mips/mips4k/malta/maltareg.h>
72 #include <dev/mc146818/mc146818reg.h>
76 #include <mips/malta/maltareg.h>
82 void lcd_puts(char *);
83 void malta_reset(void);
86 * Temporary boot environment used at startup.
88 static char boot1_env[4096];
91 * Offsets to MALTA LCD characters.
93 static int malta_lcd_offs[] = {
107 /* Nothing special */
111 * Put character to Malta LCD at given position.
114 malta_lcd_putc(int pos, char c)
119 if (pos < 0 || pos > 7)
121 addr = (void *)(MALTA_ASCII_BASE + malta_lcd_offs[pos]);
122 ch = (char *)MIPS_PHYS_TO_KSEG0(addr);
127 * Print given string on LCD.
130 malta_lcd_print(char *str)
137 for (i = 0; *str != '\0'; i++, str++)
138 malta_lcd_putc(i, *str);
144 malta_lcd_print("FreeBSD_");
153 #ifdef TICK_USE_MALTA_RTC
154 static __inline uint8_t
158 *((volatile uint8_t *)
159 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
160 return (*((volatile uint8_t *)
161 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))));
165 writertc(uint8_t addr, uint8_t val)
168 *((volatile uint8_t *)
169 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
170 *((volatile uint8_t *)
171 MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))) = val;
176 mips_init(unsigned long memsize, uint64_t ememsize)
180 for (i = 0; i < PHYS_AVAIL_ENTRIES; i++) {
185 * memsize is the amount of RAM available below 256MB.
186 * ememsize is the total amount of RAM available.
188 * The second bank starts at 0x90000000.
191 /* phys_avail regions are in bytes */
192 phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
193 phys_avail[1] = memsize;
194 dump_avail[0] = phys_avail[0];
195 dump_avail[1] = phys_avail[1];
197 /* Only specify the extended region if it's set */
198 if (ememsize > memsize) {
199 phys_avail[2] = 0x90000000;
200 phys_avail[3] = 0x90000000 + (ememsize - memsize);
201 dump_avail[2] = phys_avail[2];
202 dump_avail[3] = phys_avail[3];
205 /* XXX realmem assigned in the caller of mips_init() */
209 init_param2(physmem);
216 if (boothowto & RB_KDB)
217 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
222 * Perform a board-level soft-reset.
223 * Note that this is not emulated by gxemul.
230 c = (char *)MIPS_PHYS_TO_KSEG0(MALTA_SOFTRES);
237 uint64_t platform_counter_freq = 0;
239 #if defined(TICK_USE_YAMON_FREQ)
241 * If we are running on a board which uses YAMON firmware,
242 * then query CPU pipeline clock from the syscon object.
243 * If unsuccessful, use hard-coded default.
245 platform_counter_freq = yamon_getcpufreq();
247 #elif defined(TICK_USE_MALTA_RTC)
249 * If we are running on a board with the MC146818 RTC,
250 * use it to determine CPU pipeline clock frequency.
252 u_int64_t counterval[2];
254 /* Set RTC to binary mode. */
255 writertc(RTC_STATUSB, (rtcin(RTC_STATUSB) | RTCSB_BCD));
257 /* Busy-wait for falling edge of RTC update. */
258 while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
260 while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
262 counterval[0] = mips_rd_count();
264 /* Busy-wait for falling edge of RTC update. */
265 while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
267 while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
269 counterval[1] = mips_rd_count();
271 platform_counter_freq = counterval[1] - counterval[0];
274 if (platform_counter_freq == 0)
275 platform_counter_freq = MIPS_DEFAULT_HZ;
277 return (platform_counter_freq);
281 platform_start(__register_t a0, __register_t a1, __register_t a2,
285 uint64_t platform_counter_freq;
287 int32_t *argv = (int32_t*)a1;
288 int32_t *envp = (int32_t*)a2;
289 unsigned int memsize = a3;
290 uint64_t ememsize = 0;
293 /* clear the BSS and SBSS segments */
294 kernend = (vm_offset_t)&end;
295 memset(&edata, 0, kernend - (vm_offset_t)(&edata));
297 mips_postboot_fixup();
300 platform_counter_freq = malta_cpu_freq();
301 mips_timer_early_init(platform_counter_freq);
302 init_static_kenv(boot1_env, sizeof(boot1_env));
305 printf("entry: platform_start()\n");
310 * YAMON uses 32bit pointers to strings so
311 * convert them to proper type manually
315 printf("cmd line: ");
316 for (i = 0; i < argc; i++)
317 printf("%s ", (char*)(intptr_t)argv[i]);
325 * Parse the environment for things like ememsize.
327 for (i = 0; envp[i]; i += 2) {
330 a = (char *)(intptr_t)envp[i];
331 v = (char *)(intptr_t)envp[i+1];
334 printf("\t%s = %s\n", a, v);
336 if (strcmp(a, "ememsize") == 0) {
337 ememsize = strtoul(v, NULL, 0);
342 printf("memsize = %llu (0x%08x)\n",
343 (unsigned long long) memsize, memsize);
344 printf("ememsize = %llu\n", (unsigned long long) ememsize);
348 * For <= 256MB RAM amounts, ememsize should equal memsize.
349 * For > 256MB RAM amounts it's the total RAM available;
350 * split between two banks.
352 * XXX TODO: just push realmem assignment into mips_init() ?
354 realmem = btoc(ememsize);
355 mips_init(memsize, ememsize);
357 mips_timer_init_params(platform_counter_freq, 0);