xref: /f-stack/freebsd/mips/mediatek/mtk_machdep.c (revision 22ce4aff)
1 /*-
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.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ddb.h"
33 
34 #include <sys/param.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/imgact.h>
39 #include <sys/bio.h>
40 #include <sys/buf.h>
41 #include <sys/bus.h>
42 #include <sys/cpu.h>
43 #include <sys/cons.h>
44 #include <sys/exec.h>
45 #include <sys/ucontext.h>
46 #include <sys/proc.h>
47 #include <sys/kdb.h>
48 #include <sys/ptrace.h>
49 #include <sys/boot.h>
50 #include <sys/reboot.h>
51 #include <sys/signalvar.h>
52 #include <sys/sysent.h>
53 #include <sys/sysproto.h>
54 #include <sys/user.h>
55 
56 #include <vm/vm.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>
61 #include <vm/vm_dumpset.h>
62 
63 #include <machine/cache.h>
64 #include <machine/clock.h>
65 #include <machine/cpu.h>
66 #include <machine/cpuinfo.h>
67 #include <machine/cpufunc.h>
68 #include <machine/cpuregs.h>
69 #include <machine/hwfunc.h>
70 #include <machine/intr_machdep.h>
71 #include <machine/locore.h>
72 #include <machine/md_var.h>
73 #include <machine/pte.h>
74 #include <machine/sigframe.h>
75 #include <machine/trap.h>
76 
77 #include <mips/mediatek/mtk_sysctl.h>
78 #include <mips/mediatek/mtk_soc.h>
79 
80 #include "opt_platform.h"
81 #include "opt_rt305x.h"
82 
83 #include <dev/fdt/fdt_common.h>
84 #include <dev/ofw/openfirm.h>
85 
86 extern int	*edata;
87 extern int	*end;
88 static char 	boot1_env[0x1000];
89 
90 void
platform_cpu_init()91 platform_cpu_init()
92 {
93 	/* Nothing special */
94 }
95 
96 static void
mips_init(void)97 mips_init(void)
98 {
99 	struct mem_region mr[FDT_MEM_REGIONS];
100 	uint64_t val;
101 	int i, j, mr_cnt;
102 	char *memsize;
103 
104 	printf("entry: mips_init()\n");
105 
106 	bootverbose = 1;
107 
108 	for (i = 0; i < 10; i++)
109 		phys_avail[i] = 0;
110 
111 	dump_avail[0] = phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
112 
113 	/*
114 	 * The most low memory MT7621 can have. Currently MT7621 is the chip
115 	 * that supports the most memory, so that seems reasonable.
116 	 */
117 	realmem = btoc(448 * 1024 * 1024);
118 
119 	if (fdt_get_mem_regions(mr, &mr_cnt, &val) == 0) {
120 		physmem = btoc(val);
121 
122 		printf("RAM size: %ldMB (from FDT)\n",
123 		    ctob(physmem) / (1024 * 1024));
124 
125 		KASSERT((phys_avail[0] >= mr[0].mr_start) && \
126 			(phys_avail[0] < (mr[0].mr_start + mr[0].mr_size)),
127 			("First region is not within FDT memory range"));
128 
129 		/* Limit size of the first region */
130 		phys_avail[1] = (mr[0].mr_start +
131 		    MIN(mr[0].mr_size, ctob(realmem)));
132 		dump_avail[1] = phys_avail[1];
133 
134 		/* Add the rest of the regions */
135 		for (i = 1, j = 2; i < mr_cnt; i++, j+=2) {
136 			phys_avail[j] = mr[i].mr_start;
137 			phys_avail[j+1] = (mr[i].mr_start + mr[i].mr_size);
138 			dump_avail[j] = phys_avail[j];
139 			dump_avail[j+1] = phys_avail[j+1];
140 		}
141 	} else {
142 		if ((memsize = kern_getenv("memsize")) != NULL) {
143 			physmem = btoc(strtol(memsize, NULL, 0) << 20);
144 			printf("RAM size: %ldMB (from memsize)\n",
145 			    ctob(physmem) / (1024 * 1024));
146 		} else { /* All else failed, assume 32MB */
147 			physmem = btoc(32 * 1024 * 1024);
148 			printf("RAM size: %ldMB (assumed)\n",
149 			    ctob(physmem) / (1024 * 1024));
150 		}
151 
152 		if (mtk_soc_get_socid() == MTK_SOC_RT2880) {
153 			/* RT2880 memory start is 88000000 */
154 			dump_avail[1] = phys_avail[1] = ctob(physmem)
155 			    + 0x08000000;
156 		} else if (ctob(physmem) < (448 * 1024 * 1024)) {
157 			/*
158 			 * Anything up to 448MB is assumed to be directly
159 			 * mappable as low memory...
160 			 */
161 			dump_avail[1] = phys_avail[1] = ctob(physmem);
162 		} else if (mtk_soc_get_socid() == MTK_SOC_MT7621) {
163 			/*
164 			 * On MT7621 the low memory is limited to 448MB, the
165 			 * rest is high memory, mapped at 0x20000000
166 			 */
167 			phys_avail[1] = 448 * 1024 * 1024;
168 			phys_avail[2] = 0x20000000;
169 			phys_avail[3] = phys_avail[2] + ctob(physmem) -
170 			    phys_avail[1];
171 			dump_avail[1] = phys_avail[1] - phys_avail[0];
172 			dump_avail[2] = phys_avail[2];
173 			dump_avail[3] = phys_avail[3] - phys_avail[2];
174 		} else {
175 			/*
176 			 * We have > 448MB RAM and we're not MT7621? Currently
177 			 * there is no such chip, so we'll just limit the RAM to
178 			 * 32MB and let the user know...
179 			 */
180 			printf("Unknown chip, assuming 32MB RAM\n");
181 			physmem = btoc(32 * 1024 * 1024);
182 			dump_avail[1] = phys_avail[1] = ctob(physmem);
183 		}
184 	}
185 
186 	if (physmem < realmem)
187 		realmem = physmem;
188 
189 	init_param1();
190 	init_param2(physmem);
191 	mips_cpu_init();
192 	pmap_bootstrap();
193 	mips_proc0_init();
194 	mutex_init();
195 	kdb_init();
196 #ifdef KDB
197 	if (boothowto & RB_KDB)
198 		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
199 #endif
200 }
201 
202 void
platform_reset(void)203 platform_reset(void)
204 {
205 
206 	mtk_soc_reset();
207 }
208 
209 void
platform_start(__register_t a0 __unused,__register_t a1 __unused,__register_t a2 __unused,__register_t a3 __unused)210 platform_start(__register_t a0 __unused, __register_t a1 __unused,
211     __register_t a2 __unused, __register_t a3 __unused)
212 {
213 	vm_offset_t kernend;
214 	int argc = a0, i;//, res;
215 	uint32_t timer_clk;
216 	char **argv = (char **)MIPS_PHYS_TO_KSEG0(a1);
217 	char **envp = (char **)MIPS_PHYS_TO_KSEG0(a2);
218 	void *dtbp;
219 	phandle_t chosen;
220 	char buf[2048];
221 
222 	/* clear the BSS and SBSS segments */
223 	kernend = (vm_offset_t)&end;
224 	memset(&edata, 0, kernend - (vm_offset_t)(&edata));
225 
226 	mips_postboot_fixup();
227 
228 	/* Initialize pcpu stuff */
229 	mips_pcpu0_init();
230 
231 	dtbp = &fdt_static_dtb;
232 	if (OF_install(OFW_FDT, 0) == FALSE)
233 		while (1);
234 	if (OF_init((void *)dtbp) != 0)
235 		while (1);
236 
237 	mtk_soc_try_early_detect();
238 	mtk_soc_set_cpu_model();
239 
240 	if ((timer_clk = mtk_soc_get_timerclk()) == 0)
241 		timer_clk = 1000000000; /* no such speed yet */
242 
243 	mips_timer_early_init(timer_clk);
244 
245 	/* initialize console so that we have printf */
246 	boothowto |= (RB_SERIAL | RB_MULTIPLE);	/* Use multiple consoles */
247 	boothowto |= (RB_VERBOSE);
248 	cninit();
249 
250 	init_static_kenv(boot1_env, sizeof(boot1_env));
251 
252 	/*
253 	 * Get bsdbootargs from FDT if specified.
254 	 */
255 	chosen = OF_finddevice("/chosen");
256 	if (OF_getprop(chosen, "bsdbootargs", buf, sizeof(buf)) != -1)
257 		boothowto |= boot_parse_cmdline(buf);
258 
259 	printf("FDT DTB  at: 0x%08x\n", (uint32_t)dtbp);
260 
261 	printf("CPU   clock: %4dMHz\n", mtk_soc_get_cpuclk()/(1000*1000));
262 	printf("Timer clock: %4dMHz\n", timer_clk/(1000*1000));
263 	printf("UART  clock: %4dMHz\n\n", mtk_soc_get_uartclk()/(1000*1000));
264 
265 	printf("U-Boot args (from %d args):\n", argc - 1);
266 
267 	if (argc == 1)
268 		printf("\tNone\n");
269 
270 	for (i = 1; i < argc; i++) {
271 		char *n = "argv  ", *arg;
272 
273 		if (i > 99)
274 			break;
275 
276 		if (argv[i])
277 		{
278 			arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(argv[i]);
279 			printf("\targv[%d] = %s\n", i, arg);
280 			sprintf(n, "argv%d", i);
281 			kern_setenv(n, arg);
282 		}
283 	}
284 
285 	printf("Environment:\n");
286 
287 	for (i = 0; envp[i] && MIPS_IS_VALID_PTR(envp[i]); i++) {
288 		char *n, *arg;
289 
290 		arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(envp[i]);
291 		if (! MIPS_IS_VALID_PTR(arg))
292 			continue;
293 		printf("\t%s\n", arg);
294 		n = strsep(&arg, "=");
295 		if (arg == NULL)
296 			kern_setenv(n, "1");
297 		else
298 			kern_setenv(n, arg);
299 	}
300 
301 	mips_init();
302 	mips_timer_init_params(timer_clk, 0);
303 }
304