]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mediatek/mtk_machdep.c
MFV r323530,r323533,r323534: 7431 ZFS Channel Programs, and followups
[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 (mtk_soc_get_socid() == MTK_SOC_RT2880) {
150                         /* RT2880 memory start is 88000000 */
151                         dump_avail[1] = phys_avail[1] = ctob(physmem)
152                             + 0x08000000;
153                 } else if (ctob(physmem) < (448 * 1024 * 1024)) {
154                         /*
155                          * Anything up to 448MB is assumed to be directly
156                          * mappable as low memory...
157                          */
158                         dump_avail[1] = phys_avail[1] = ctob(physmem);
159                 } else if (mtk_soc_get_socid() == MTK_SOC_MT7621) {
160                         /*
161                          * On MT7621 the low memory is limited to 448MB, the
162                          * rest is high memory, mapped at 0x20000000
163                          */
164                         phys_avail[1] = 448 * 1024 * 1024;
165                         phys_avail[2] = 0x20000000;
166                         phys_avail[3] = phys_avail[2] + ctob(physmem) -
167                             phys_avail[1];
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];
171                 } else {
172                         /*
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...
176                          */
177                         printf("Unknown chip, assuming 32MB RAM\n");
178                         physmem = btoc(32 * 1024 * 1024);
179                         dump_avail[1] = phys_avail[1] = ctob(physmem);
180                 }
181         }
182
183         if (physmem < realmem)
184                 realmem = physmem;
185
186         init_param1();
187         init_param2(physmem);
188         mips_cpu_init();
189         pmap_bootstrap();
190         mips_proc0_init();
191         mutex_init();
192         kdb_init();
193 #ifdef KDB
194         if (boothowto & RB_KDB)
195                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
196 #endif
197 }
198
199 static void
200 _parse_bootarg(char *v)
201 {
202         char *n;
203
204         if (*v == '-') {
205                 while (*v != '\0') {
206                         v++;
207                         switch (*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;
220                         }
221                 }
222         } else {
223                 n = strsep(&v, "=");
224                 if (v == NULL)
225                         kern_setenv(n, "1");
226                 else
227                         kern_setenv(n, v);
228         }
229 }
230
231 /* Parse cmd line args as env - copied from xlp_machdep. */
232 /* XXX-BZ this should really be centrally provided for all (boot) code. */
233 static void
234 _parse_bootargs(char *cmdline)
235 {
236         char *v;
237
238         while ((v = strsep(&cmdline, " \n")) != NULL) {
239                 if (*v == '\0')
240                         continue;
241                 _parse_bootarg(v);
242         }
243 }
244
245 void
246 platform_reset(void)
247 {
248
249         mtk_soc_reset();
250 }
251
252 void
253 platform_start(__register_t a0 __unused, __register_t a1 __unused, 
254     __register_t a2 __unused, __register_t a3 __unused)
255 {
256         vm_offset_t kernend;
257         int argc = a0, i;//, res;
258         uint32_t timer_clk;
259         char **argv = (char **)MIPS_PHYS_TO_KSEG0(a1);
260         char **envp = (char **)MIPS_PHYS_TO_KSEG0(a2);
261         void *dtbp;
262         phandle_t chosen;
263         char buf[2048];
264
265         /* clear the BSS and SBSS segments */
266         kernend = (vm_offset_t)&end;
267         memset(&edata, 0, kernend - (vm_offset_t)(&edata));
268
269         mips_postboot_fixup();
270
271         /* Initialize pcpu stuff */
272         mips_pcpu0_init();
273
274         dtbp = &fdt_static_dtb;
275         if (OF_install(OFW_FDT, 0) == FALSE)
276                 while (1);
277         if (OF_init((void *)dtbp) != 0)
278                 while (1);
279
280         mtk_soc_try_early_detect();
281         if ((timer_clk = mtk_soc_get_timerclk()) == 0)
282                 timer_clk = 1000000000; /* no such speed yet */
283
284         mips_timer_early_init(timer_clk);
285
286         /* initialize console so that we have printf */
287         boothowto |= (RB_SERIAL | RB_MULTIPLE); /* Use multiple consoles */
288         boothowto |= (RB_VERBOSE);
289         cninit();
290
291         init_static_kenv(boot1_env, sizeof(boot1_env));
292
293         /*
294          * Get bsdbootargs from FDT if specified.
295          */
296         chosen = OF_finddevice("/chosen");
297         if (OF_getprop(chosen, "bsdbootargs", buf, sizeof(buf)) != -1)
298                 _parse_bootargs(buf);
299
300         printf("FDT DTB  at: 0x%08x\n", (uint32_t)dtbp);
301
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));
305
306         printf("U-Boot args (from %d args):\n", argc - 1);
307
308         if (argc == 1)
309                 printf("\tNone\n");
310
311         for (i = 1; i < argc; i++) {
312                 char *n = "argv  ", *arg;
313
314                 if (i > 99)
315                         break;
316
317                 if (argv[i])
318                 {
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);
322                         kern_setenv(n, arg);
323                 }
324         }
325
326         printf("Environment:\n");
327
328         for (i = 0; envp[i] && MIPS_IS_VALID_PTR(envp[i]); i++) {
329                 char *n, *arg;
330
331                 arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(envp[i]);
332                 if (! MIPS_IS_VALID_PTR(arg))
333                         continue;
334                 printf("\t%s\n", arg);
335                 n = strsep(&arg, "=");
336                 if (arg == NULL)
337                         kern_setenv(n, "1");
338                 else
339                         kern_setenv(n, arg);
340         }
341
342
343         mips_init();
344         mips_timer_init_params(timer_clk, 0);
345 }