2 * Copyright (C) 2015-2016 by Stanislav Galabov. All rights reserved.
3 * Copyright (C) 2010-2011 by Aleksandr Rybalko. All rights reserved.
4 * Copyright (C) 2007 by Oleksandr Tymoshenko. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/imgact.h>
45 #include <sys/ucontext.h>
48 #include <sys/ptrace.h>
49 #include <sys/reboot.h>
50 #include <sys/signalvar.h>
51 #include <sys/sysent.h>
52 #include <sys/sysproto.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
59 #include <machine/cache.h>
60 #include <machine/clock.h>
61 #include <machine/cpu.h>
62 #include <machine/cpuinfo.h>
63 #include <machine/cpufunc.h>
64 #include <machine/cpuregs.h>
65 #include <machine/hwfunc.h>
66 #include <machine/intr_machdep.h>
67 #include <machine/locore.h>
68 #include <machine/md_var.h>
69 #include <machine/pte.h>
70 #include <machine/sigframe.h>
71 #include <machine/trap.h>
72 #include <machine/vmparam.h>
74 #include <mips/mediatek/mtk_sysctl.h>
75 #include <mips/mediatek/mtk_soc.h>
77 #include "opt_platform.h"
78 #include "opt_rt305x.h"
80 #include <dev/fdt/fdt_common.h>
81 #include <dev/ofw/openfirm.h>
85 static char boot1_env[0x1000];
96 struct mem_region mr[FDT_MEM_REGIONS];
101 printf("entry: mips_init()\n");
105 for (i = 0; i < 10; i++)
108 dump_avail[0] = phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
111 * The most low memory MT7621 can have. Currently MT7621 is the chip
112 * that supports the most memory, so that seems reasonable.
114 realmem = btoc(448 * 1024 * 1024);
116 if (fdt_get_mem_regions(mr, &mr_cnt, &val) == 0) {
119 printf("RAM size: %ldMB (from FDT)\n",
120 ctob(physmem) / (1024 * 1024));
122 KASSERT((phys_avail[0] >= mr[0].mr_start) && \
123 (phys_avail[0] < (mr[0].mr_start + mr[0].mr_size)),
124 ("First region is not within FDT memory range"));
126 /* Limit size of the first region */
127 phys_avail[1] = (mr[0].mr_start +
128 MIN(mr[0].mr_size, ctob(realmem)));
129 dump_avail[1] = phys_avail[1];
131 /* Add the rest of the regions */
132 for (i = 1, j = 2; i < mr_cnt; i++, j+=2) {
133 phys_avail[j] = mr[i].mr_start;
134 phys_avail[j+1] = (mr[i].mr_start + mr[i].mr_size);
135 dump_avail[j] = phys_avail[j];
136 dump_avail[j+1] = phys_avail[j+1];
139 if ((memsize = kern_getenv("memsize")) != NULL) {
140 physmem = btoc(strtol(memsize, NULL, 0) << 20);
141 printf("RAM size: %ldMB (from memsize)\n",
142 ctob(physmem) / (1024 * 1024));
143 } else { /* All else failed, assume 32MB */
144 physmem = btoc(32 * 1024 * 1024);
145 printf("RAM size: %ldMB (assumed)\n",
146 ctob(physmem) / (1024 * 1024));
149 if (mtk_soc_get_socid() == MTK_SOC_RT2880) {
150 /* RT2880 memory start is 88000000 */
151 dump_avail[1] = phys_avail[1] = ctob(physmem)
153 } else if (ctob(physmem) < (448 * 1024 * 1024)) {
155 * Anything up to 448MB is assumed to be directly
156 * mappable as low memory...
158 dump_avail[1] = phys_avail[1] = ctob(physmem);
159 } else if (mtk_soc_get_socid() == MTK_SOC_MT7621) {
161 * On MT7621 the low memory is limited to 448MB, the
162 * rest is high memory, mapped at 0x20000000
164 phys_avail[1] = 448 * 1024 * 1024;
165 phys_avail[2] = 0x20000000;
166 phys_avail[3] = phys_avail[2] + ctob(physmem) -
168 dump_avail[1] = phys_avail[1] - phys_avail[0];
169 dump_avail[2] = phys_avail[2];
170 dump_avail[3] = phys_avail[3] - phys_avail[2];
173 * We have > 448MB RAM and we're not MT7621? Currently
174 * there is no such chip, so we'll just limit the RAM to
175 * 32MB and let the user know...
177 printf("Unknown chip, assuming 32MB RAM\n");
178 physmem = btoc(32 * 1024 * 1024);
179 dump_avail[1] = phys_avail[1] = ctob(physmem);
183 if (physmem < realmem)
187 init_param2(physmem);
194 if (boothowto & RB_KDB)
195 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
200 _parse_bootarg(char *v)
208 case 'a': boothowto |= RB_ASKNAME; break;
209 /* Someone should simulate that ;-) */
210 case 'C': boothowto |= RB_CDROM; break;
211 case 'd': boothowto |= RB_KDB; break;
212 case 'D': boothowto |= RB_MULTIPLE; break;
213 case 'm': boothowto |= RB_MUTE; break;
214 case 'g': boothowto |= RB_GDB; break;
215 case 'h': boothowto |= RB_SERIAL; break;
216 case 'p': boothowto |= RB_PAUSE; break;
217 case 'r': boothowto |= RB_DFLTROOT; break;
218 case 's': boothowto |= RB_SINGLE; break;
219 case 'v': boothowto |= RB_VERBOSE; break;
231 /* Parse cmd line args as env - copied from xlp_machdep. */
232 /* XXX-BZ this should really be centrally provided for all (boot) code. */
234 _parse_bootargs(char *cmdline)
238 while ((v = strsep(&cmdline, " \n")) != NULL) {
253 platform_start(__register_t a0 __unused, __register_t a1 __unused,
254 __register_t a2 __unused, __register_t a3 __unused)
257 int argc = a0, i;//, res;
259 char **argv = (char **)MIPS_PHYS_TO_KSEG0(a1);
260 char **envp = (char **)MIPS_PHYS_TO_KSEG0(a2);
265 /* clear the BSS and SBSS segments */
266 kernend = (vm_offset_t)&end;
267 memset(&edata, 0, kernend - (vm_offset_t)(&edata));
269 mips_postboot_fixup();
271 /* Initialize pcpu stuff */
274 dtbp = &fdt_static_dtb;
275 if (OF_install(OFW_FDT, 0) == FALSE)
277 if (OF_init((void *)dtbp) != 0)
280 mtk_soc_try_early_detect();
281 if ((timer_clk = mtk_soc_get_timerclk()) == 0)
282 timer_clk = 1000000000; /* no such speed yet */
284 mips_timer_early_init(timer_clk);
286 /* initialize console so that we have printf */
287 boothowto |= (RB_SERIAL | RB_MULTIPLE); /* Use multiple consoles */
288 boothowto |= (RB_VERBOSE);
291 init_static_kenv(boot1_env, sizeof(boot1_env));
294 * Get bsdbootargs from FDT if specified.
296 chosen = OF_finddevice("/chosen");
297 if (OF_getprop(chosen, "bsdbootargs", buf, sizeof(buf)) != -1)
298 _parse_bootargs(buf);
300 printf("FDT DTB at: 0x%08x\n", (uint32_t)dtbp);
302 printf("CPU clock: %4dMHz\n", mtk_soc_get_cpuclk()/(1000*1000));
303 printf("Timer clock: %4dMHz\n", timer_clk/(1000*1000));
304 printf("UART clock: %4dMHz\n\n", mtk_soc_get_uartclk()/(1000*1000));
306 printf("U-Boot args (from %d args):\n", argc - 1);
311 for (i = 1; i < argc; i++) {
312 char *n = "argv ", *arg;
319 arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(argv[i]);
320 printf("\targv[%d] = %s\n", i, arg);
321 sprintf(n, "argv%d", i);
326 printf("Environment:\n");
328 for (i = 0; envp[i] && MIPS_IS_VALID_PTR(envp[i]); i++) {
331 arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(envp[i]);
332 if (! MIPS_IS_VALID_PTR(arg))
334 printf("\t%s\n", arg);
335 n = strsep(&arg, "=");
344 mips_timer_init_params(timer_clk, 0);