]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mediatek/mtk_machdep.c
MFC r309362:
[FreeBSD/FreeBSD.git] / sys / mips / mediatek / mtk_machdep.c
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/reboot.h>
50 #include <sys/signalvar.h>
51 #include <sys/sysent.h>
52 #include <sys/sysproto.h>
53 #include <sys/user.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58
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>
73
74 #include <mips/mediatek/mtk_sysctl.h>
75 #include <mips/mediatek/mtk_soc.h>
76
77 #include "opt_platform.h"
78 #include "opt_rt305x.h"
79
80 #include <dev/fdt/fdt_common.h>
81 #include <dev/ofw/openfirm.h>
82
83 extern int      *edata;
84 extern int      *end;
85 static char     boot1_env[0x1000];
86
87 void
88 platform_cpu_init()
89 {
90         /* Nothing special */
91 }
92
93 static void
94 mips_init(void)
95 {
96         struct mem_region mr[FDT_MEM_REGIONS];
97         uint64_t val;
98         int i, j, mr_cnt;
99         char *memsize;
100
101         printf("entry: mips_init()\n");
102
103         bootverbose = 1;
104
105         for (i = 0; i < 10; i++)
106                 phys_avail[i] = 0;
107
108         dump_avail[0] = phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
109
110         /*
111          * The most low memory MT7621 can have. Currently MT7621 is the chip
112          * that supports the most memory, so that seems reasonable.
113          */
114         realmem = btoc(448 * 1024 * 1024);
115
116         if (fdt_get_mem_regions(mr, &mr_cnt, &val) == 0) {
117                 physmem = btoc(val);
118
119                 printf("RAM size: %ldMB (from FDT)\n",
120                     ctob(physmem) / (1024 * 1024));
121
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"));
125
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];
130
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];
137                 }
138         } else {
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));
147                 }
148
149                 if (ctob(physmem) < (448 * 1024 * 1024)) {
150                         /*
151                          * Anything up to 448MB is assumed to be directly
152                          * mappable as low memory...
153                          */
154                         dump_avail[1] = phys_avail[1] = ctob(physmem);
155                 } else if (mtk_soc_get_socid() == MTK_SOC_MT7621) {
156                         /*
157                          * On MT7621 the low memory is limited to 448MB, the
158                          * rest is high memory, mapped at 0x20000000
159                          */
160                         phys_avail[1] = 448 * 1024 * 1024;
161                         phys_avail[2] = 0x20000000;
162                         phys_avail[3] = phys_avail[2] + ctob(physmem) -
163                             phys_avail[1];
164                         dump_avail[1] = phys_avail[1] - phys_avail[0];
165                         dump_avail[2] = phys_avail[2];
166                         dump_avail[3] = phys_avail[3] - phys_avail[2];
167                 } else {
168                         /*
169                          * We have > 448MB RAM and we're not MT7621? Currently
170                          * there is no such chip, so we'll just limit the RAM to
171                          * 32MB and let the user know...
172                          */
173                         printf("Unknown chip, assuming 32MB RAM\n");
174                         physmem = btoc(32 * 1024 * 1024);
175                         dump_avail[1] = phys_avail[1] = ctob(physmem);
176                 }
177         }
178
179         if (physmem < realmem)
180                 realmem = physmem;
181
182         init_param1();
183         init_param2(physmem);
184         mips_cpu_init();
185         pmap_bootstrap();
186         mips_proc0_init();
187         mutex_init();
188         kdb_init();
189 #ifdef KDB
190         if (boothowto & RB_KDB)
191                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
192 #endif
193 }
194
195 static void
196 _parse_bootarg(char *v)
197 {
198         char *n;
199
200         if (*v == '-') {
201                 while (*v != '\0') {
202                         v++;
203                         switch (*v) {
204                         case 'a': boothowto |= RB_ASKNAME; break;
205                         /* Someone should simulate that ;-) */
206                         case 'C': boothowto |= RB_CDROM; break;
207                         case 'd': boothowto |= RB_KDB; break;
208                         case 'D': boothowto |= RB_MULTIPLE; break;
209                         case 'm': boothowto |= RB_MUTE; break;
210                         case 'g': boothowto |= RB_GDB; break;
211                         case 'h': boothowto |= RB_SERIAL; break;
212                         case 'p': boothowto |= RB_PAUSE; break;
213                         case 'r': boothowto |= RB_DFLTROOT; break;
214                         case 's': boothowto |= RB_SINGLE; break;
215                         case 'v': boothowto |= RB_VERBOSE; break;
216                         }
217                 }
218         } else {
219                 n = strsep(&v, "=");
220                 if (v == NULL)
221                         kern_setenv(n, "1");
222                 else
223                         kern_setenv(n, v);
224         }
225 }
226
227 /* Parse cmd line args as env - copied from xlp_machdep. */
228 /* XXX-BZ this should really be centrally provided for all (boot) code. */
229 static void
230 _parse_bootargs(char *cmdline)
231 {
232         char *v;
233
234         while ((v = strsep(&cmdline, " \n")) != NULL) {
235                 if (*v == '\0')
236                         continue;
237                 _parse_bootarg(v);
238         }
239 }
240
241 void
242 platform_reset(void)
243 {
244
245         mtk_soc_reset();
246 }
247
248 void
249 platform_start(__register_t a0 __unused, __register_t a1 __unused, 
250     __register_t a2 __unused, __register_t a3 __unused)
251 {
252         vm_offset_t kernend;
253         int argc = a0, i;//, res;
254         uint32_t timer_clk;
255         char **argv = (char **)MIPS_PHYS_TO_KSEG0(a1);
256         char **envp = (char **)MIPS_PHYS_TO_KSEG0(a2);
257         void *dtbp;
258         phandle_t chosen;
259         char buf[2048];
260
261         /* clear the BSS and SBSS segments */
262         kernend = (vm_offset_t)&end;
263         memset(&edata, 0, kernend - (vm_offset_t)(&edata));
264
265         mips_postboot_fixup();
266
267         /* Initialize pcpu stuff */
268         mips_pcpu0_init();
269
270         dtbp = &fdt_static_dtb;
271         if (OF_install(OFW_FDT, 0) == FALSE)
272                 while (1);
273         if (OF_init((void *)dtbp) != 0)
274                 while (1);
275
276         mtk_soc_try_early_detect();
277         if ((timer_clk = mtk_soc_get_timerclk()) == 0)
278                 timer_clk = 1000000000; /* no such speed yet */
279
280         mips_timer_early_init(timer_clk);
281
282         /* initialize console so that we have printf */
283         boothowto |= (RB_SERIAL | RB_MULTIPLE); /* Use multiple consoles */
284         boothowto |= (RB_VERBOSE);
285         cninit();
286
287         init_static_kenv(boot1_env, sizeof(boot1_env));
288
289         /*
290          * Get bsdbootargs from FDT if specified.
291          */
292         chosen = OF_finddevice("/chosen");
293         if (OF_getprop(chosen, "bsdbootargs", buf, sizeof(buf)) != -1)
294                 _parse_bootargs(buf);
295
296         printf("FDT DTB  at: 0x%08x\n", (uint32_t)dtbp);
297
298         printf("CPU   clock: %4dMHz\n", mtk_soc_get_cpuclk()/(1000*1000));
299         printf("Timer clock: %4dMHz\n", timer_clk/(1000*1000));
300         printf("UART  clock: %4dMHz\n\n", mtk_soc_get_uartclk()/(1000*1000));
301
302         printf("U-Boot args (from %d args):\n", argc - 1);
303
304         if (argc == 1)
305                 printf("\tNone\n");
306
307         for (i = 1; i < argc; i++) {
308                 char *n = "argv  ", *arg;
309
310                 if (i > 99)
311                         break;
312
313                 if (argv[i])
314                 {
315                         arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(argv[i]);
316                         printf("\targv[%d] = %s\n", i, arg);
317                         sprintf(n, "argv%d", i);
318                         kern_setenv(n, arg);
319                 }
320         }
321
322         printf("Environment:\n");
323
324         for (i = 0; envp[i] && MIPS_IS_VALID_PTR(envp[i]); i++) {
325                 char *n, *arg;
326
327                 arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(envp[i]);
328                 if (! MIPS_IS_VALID_PTR(arg))
329                         continue;
330                 printf("\t%s\n", arg);
331                 n = strsep(&arg, "=");
332                 if (arg == NULL)
333                         kern_setenv(n, "1");
334                 else
335                         kern_setenv(n, arg);
336         }
337
338
339         mips_init();
340         mips_timer_init_params(timer_clk, 0);
341 }