]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mediatek/mtk_machdep.c
Update to ELF Tool Chain r3668
[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/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_object.h>
58 #include <vm/vm_page.h>
59
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>
74
75 #include <mips/mediatek/mtk_sysctl.h>
76 #include <mips/mediatek/mtk_soc.h>
77
78 #include "opt_platform.h"
79 #include "opt_rt305x.h"
80
81 #include <dev/fdt/fdt_common.h>
82 #include <dev/ofw/openfirm.h>
83
84 extern int      *edata;
85 extern int      *end;
86 static char     boot1_env[0x1000];
87
88 void
89 platform_cpu_init()
90 {
91         /* Nothing special */
92 }
93
94 static void
95 mips_init(void)
96 {
97         struct mem_region mr[FDT_MEM_REGIONS];
98         uint64_t val;
99         int i, j, mr_cnt;
100         char *memsize;
101
102         printf("entry: mips_init()\n");
103
104         bootverbose = 1;
105
106         for (i = 0; i < 10; i++)
107                 phys_avail[i] = 0;
108
109         dump_avail[0] = phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
110
111         /*
112          * The most low memory MT7621 can have. Currently MT7621 is the chip
113          * that supports the most memory, so that seems reasonable.
114          */
115         realmem = btoc(448 * 1024 * 1024);
116
117         if (fdt_get_mem_regions(mr, &mr_cnt, &val) == 0) {
118                 physmem = btoc(val);
119
120                 printf("RAM size: %ldMB (from FDT)\n",
121                     ctob(physmem) / (1024 * 1024));
122
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"));
126
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];
131
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];
138                 }
139         } else {
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));
148                 }
149
150                 if (mtk_soc_get_socid() == MTK_SOC_RT2880) {
151                         /* RT2880 memory start is 88000000 */
152                         dump_avail[1] = phys_avail[1] = ctob(physmem)
153                             + 0x08000000;
154                 } else if (ctob(physmem) < (448 * 1024 * 1024)) {
155                         /*
156                          * Anything up to 448MB is assumed to be directly
157                          * mappable as low memory...
158                          */
159                         dump_avail[1] = phys_avail[1] = ctob(physmem);
160                 } else if (mtk_soc_get_socid() == MTK_SOC_MT7621) {
161                         /*
162                          * On MT7621 the low memory is limited to 448MB, the
163                          * rest is high memory, mapped at 0x20000000
164                          */
165                         phys_avail[1] = 448 * 1024 * 1024;
166                         phys_avail[2] = 0x20000000;
167                         phys_avail[3] = phys_avail[2] + ctob(physmem) -
168                             phys_avail[1];
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];
172                 } else {
173                         /*
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...
177                          */
178                         printf("Unknown chip, assuming 32MB RAM\n");
179                         physmem = btoc(32 * 1024 * 1024);
180                         dump_avail[1] = phys_avail[1] = ctob(physmem);
181                 }
182         }
183
184         if (physmem < realmem)
185                 realmem = physmem;
186
187         init_param1();
188         init_param2(physmem);
189         mips_cpu_init();
190         pmap_bootstrap();
191         mips_proc0_init();
192         mutex_init();
193         kdb_init();
194 #ifdef KDB
195         if (boothowto & RB_KDB)
196                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
197 #endif
198 }
199
200 void
201 platform_reset(void)
202 {
203
204         mtk_soc_reset();
205 }
206
207 void
208 platform_start(__register_t a0 __unused, __register_t a1 __unused, 
209     __register_t a2 __unused, __register_t a3 __unused)
210 {
211         vm_offset_t kernend;
212         int argc = a0, i;//, res;
213         uint32_t timer_clk;
214         char **argv = (char **)MIPS_PHYS_TO_KSEG0(a1);
215         char **envp = (char **)MIPS_PHYS_TO_KSEG0(a2);
216         void *dtbp;
217         phandle_t chosen;
218         char buf[2048];
219
220         /* clear the BSS and SBSS segments */
221         kernend = (vm_offset_t)&end;
222         memset(&edata, 0, kernend - (vm_offset_t)(&edata));
223
224         mips_postboot_fixup();
225
226         /* Initialize pcpu stuff */
227         mips_pcpu0_init();
228
229         dtbp = &fdt_static_dtb;
230         if (OF_install(OFW_FDT, 0) == FALSE)
231                 while (1);
232         if (OF_init((void *)dtbp) != 0)
233                 while (1);
234
235         mtk_soc_try_early_detect();
236         mtk_soc_set_cpu_model();
237
238         if ((timer_clk = mtk_soc_get_timerclk()) == 0)
239                 timer_clk = 1000000000; /* no such speed yet */
240
241         mips_timer_early_init(timer_clk);
242
243         /* initialize console so that we have printf */
244         boothowto |= (RB_SERIAL | RB_MULTIPLE); /* Use multiple consoles */
245         boothowto |= (RB_VERBOSE);
246         cninit();
247
248         init_static_kenv(boot1_env, sizeof(boot1_env));
249
250         /*
251          * Get bsdbootargs from FDT if specified.
252          */
253         chosen = OF_finddevice("/chosen");
254         if (OF_getprop(chosen, "bsdbootargs", buf, sizeof(buf)) != -1)
255                 boothowto |= boot_parse_cmdline(buf);
256
257         printf("FDT DTB  at: 0x%08x\n", (uint32_t)dtbp);
258
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));
262
263         printf("U-Boot args (from %d args):\n", argc - 1);
264
265         if (argc == 1)
266                 printf("\tNone\n");
267
268         for (i = 1; i < argc; i++) {
269                 char *n = "argv  ", *arg;
270
271                 if (i > 99)
272                         break;
273
274                 if (argv[i])
275                 {
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);
279                         kern_setenv(n, arg);
280                 }
281         }
282
283         printf("Environment:\n");
284
285         for (i = 0; envp[i] && MIPS_IS_VALID_PTR(envp[i]); i++) {
286                 char *n, *arg;
287
288                 arg = (char *)(intptr_t)MIPS_PHYS_TO_KSEG0(envp[i]);
289                 if (! MIPS_IS_VALID_PTR(arg))
290                         continue;
291                 printf("\t%s\n", arg);
292                 n = strsep(&arg, "=");
293                 if (arg == NULL)
294                         kern_setenv(n, "1");
295                 else
296                         kern_setenv(n, arg);
297         }
298
299
300         mips_init();
301         mips_timer_init_params(timer_clk, 0);
302 }