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>
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/cache.h>
61 #include <machine/clock.h>
62 #include <machine/cpu.h>
63 #include <machine/cpuinfo.h>
64 #include <machine/cpufunc.h>
65 #include <machine/cpuregs.h>
66 #include <machine/hwfunc.h>
67 #include <machine/intr_machdep.h>
68 #include <machine/locore.h>
69 #include <machine/md_var.h>
70 #include <machine/pte.h>
71 #include <machine/sigframe.h>
72 #include <machine/trap.h>
73 #include <machine/vmparam.h>
75 #include <mips/mediatek/mtk_sysctl.h>
76 #include <mips/mediatek/mtk_soc.h>
78 #include "opt_platform.h"
79 #include "opt_rt305x.h"
81 #include <dev/fdt/fdt_common.h>
82 #include <dev/ofw/openfirm.h>
86 static char boot1_env[0x1000];
97 struct mem_region mr[FDT_MEM_REGIONS];
102 printf("entry: mips_init()\n");
106 for (i = 0; i < 10; i++)
109 dump_avail[0] = phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
112 * The most low memory MT7621 can have. Currently MT7621 is the chip
113 * that supports the most memory, so that seems reasonable.
115 realmem = btoc(448 * 1024 * 1024);
117 if (fdt_get_mem_regions(mr, &mr_cnt, &val) == 0) {
120 printf("RAM size: %ldMB (from FDT)\n",
121 ctob(physmem) / (1024 * 1024));
123 KASSERT((phys_avail[0] >= mr[0].mr_start) && \
124 (phys_avail[0] < (mr[0].mr_start + mr[0].mr_size)),
125 ("First region is not within FDT memory range"));
127 /* Limit size of the first region */
128 phys_avail[1] = (mr[0].mr_start +
129 MIN(mr[0].mr_size, ctob(realmem)));
130 dump_avail[1] = phys_avail[1];
132 /* Add the rest of the regions */
133 for (i = 1, j = 2; i < mr_cnt; i++, j+=2) {
134 phys_avail[j] = mr[i].mr_start;
135 phys_avail[j+1] = (mr[i].mr_start + mr[i].mr_size);
136 dump_avail[j] = phys_avail[j];
137 dump_avail[j+1] = phys_avail[j+1];
140 if ((memsize = kern_getenv("memsize")) != NULL) {
141 physmem = btoc(strtol(memsize, NULL, 0) << 20);
142 printf("RAM size: %ldMB (from memsize)\n",
143 ctob(physmem) / (1024 * 1024));
144 } else { /* All else failed, assume 32MB */
145 physmem = btoc(32 * 1024 * 1024);
146 printf("RAM size: %ldMB (assumed)\n",
147 ctob(physmem) / (1024 * 1024));
150 if (mtk_soc_get_socid() == MTK_SOC_RT2880) {
151 /* RT2880 memory start is 88000000 */
152 dump_avail[1] = phys_avail[1] = ctob(physmem)
154 } else if (ctob(physmem) < (448 * 1024 * 1024)) {
156 * Anything up to 448MB is assumed to be directly
157 * mappable as low memory...
159 dump_avail[1] = phys_avail[1] = ctob(physmem);
160 } else if (mtk_soc_get_socid() == MTK_SOC_MT7621) {
162 * On MT7621 the low memory is limited to 448MB, the
163 * rest is high memory, mapped at 0x20000000
165 phys_avail[1] = 448 * 1024 * 1024;
166 phys_avail[2] = 0x20000000;
167 phys_avail[3] = phys_avail[2] + ctob(physmem) -
169 dump_avail[1] = phys_avail[1] - phys_avail[0];
170 dump_avail[2] = phys_avail[2];
171 dump_avail[3] = phys_avail[3] - phys_avail[2];
174 * We have > 448MB RAM and we're not MT7621? Currently
175 * there is no such chip, so we'll just limit the RAM to
176 * 32MB and let the user know...
178 printf("Unknown chip, assuming 32MB RAM\n");
179 physmem = btoc(32 * 1024 * 1024);
180 dump_avail[1] = phys_avail[1] = ctob(physmem);
184 if (physmem < realmem)
188 init_param2(physmem);
195 if (boothowto & RB_KDB)
196 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
208 platform_start(__register_t a0 __unused, __register_t a1 __unused,
209 __register_t a2 __unused, __register_t a3 __unused)
212 int argc = a0, i;//, res;
214 char **argv = (char **)MIPS_PHYS_TO_KSEG0(a1);
215 char **envp = (char **)MIPS_PHYS_TO_KSEG0(a2);
220 /* clear the BSS and SBSS segments */
221 kernend = (vm_offset_t)&end;
222 memset(&edata, 0, kernend - (vm_offset_t)(&edata));
224 mips_postboot_fixup();
226 /* Initialize pcpu stuff */
229 dtbp = &fdt_static_dtb;
230 if (OF_install(OFW_FDT, 0) == FALSE)
232 if (OF_init((void *)dtbp) != 0)
235 mtk_soc_try_early_detect();
236 mtk_soc_set_cpu_model();
238 if ((timer_clk = mtk_soc_get_timerclk()) == 0)
239 timer_clk = 1000000000; /* no such speed yet */
241 mips_timer_early_init(timer_clk);
243 /* initialize console so that we have printf */
244 boothowto |= (RB_SERIAL | RB_MULTIPLE); /* Use multiple consoles */
245 boothowto |= (RB_VERBOSE);
248 init_static_kenv(boot1_env, sizeof(boot1_env));
251 * Get bsdbootargs from FDT if specified.
253 chosen = OF_finddevice("/chosen");
254 if (OF_getprop(chosen, "bsdbootargs", buf, sizeof(buf)) != -1)
255 boothowto |= boot_parse_cmdline(buf);
257 printf("FDT DTB at: 0x%08x\n", (uint32_t)dtbp);
259 printf("CPU clock: %4dMHz\n", mtk_soc_get_cpuclk()/(1000*1000));
260 printf("Timer clock: %4dMHz\n", timer_clk/(1000*1000));
261 printf("UART clock: %4dMHz\n\n", mtk_soc_get_uartclk()/(1000*1000));
263 printf("U-Boot args (from %d args):\n", argc - 1);
268 for (i = 1; i < argc; i++) {
269 char *n = "argv ", *arg;
276 arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(argv[i]);
277 printf("\targv[%d] = %s\n", i, arg);
278 sprintf(n, "argv%d", i);
283 printf("Environment:\n");
285 for (i = 0; envp[i] && MIPS_IS_VALID_PTR(envp[i]); i++) {
288 arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(envp[i]);
289 if (! MIPS_IS_VALID_PTR(arg))
291 printf("\t%s\n", arg);
292 n = strsep(&arg, "=");
301 mips_timer_init_params(timer_clk, 0);