]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/malta/malta_machdep.c
Import tzdata 2020c
[FreeBSD/FreeBSD.git] / sys / mips / malta / malta_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Wojciech A. Koszek <wkoszek@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_ddb.h"
34
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/imgact.h>
40 #include <sys/bio.h>
41 #include <sys/buf.h>
42 #include <sys/bus.h>
43 #include <sys/cpu.h>
44 #include <sys/cons.h>
45 #include <sys/exec.h>
46 #include <sys/ucontext.h>
47 #include <sys/proc.h>
48 #include <sys/kdb.h>
49 #include <sys/ptrace.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/clock.h>
64 #include <machine/cpu.h>
65 #include <machine/cpuregs.h>
66 #include <machine/hwfunc.h>
67 #include <machine/md_var.h>
68 #include <machine/pmap.h>
69 #include <machine/trap.h>
70
71 #ifdef TICK_USE_YAMON_FREQ
72 #include <mips/malta/yamon.h>
73 #endif
74
75 #ifdef TICK_USE_MALTA_RTC
76 #include <mips/mips4k/malta/maltareg.h>
77 #include <dev/mc146818/mc146818reg.h>
78 #include <isa/rtc.h>
79 #endif
80
81 #include <mips/malta/maltareg.h>
82
83 extern int      *edata;
84 extern int      *end;
85
86 void    lcd_init(void);
87 void    lcd_puts(char *);
88 void    malta_reset(void);
89
90 /*
91  * Temporary boot environment used at startup.
92  */
93 static char boot1_env[4096];
94
95 /*
96  * Offsets to MALTA LCD characters.
97  */
98 static int malta_lcd_offs[] = {
99         MALTA_ASCIIPOS0,
100         MALTA_ASCIIPOS1,
101         MALTA_ASCIIPOS2,
102         MALTA_ASCIIPOS3,
103         MALTA_ASCIIPOS4,
104         MALTA_ASCIIPOS5,
105         MALTA_ASCIIPOS6,
106         MALTA_ASCIIPOS7
107 };
108
109 void
110 platform_cpu_init()
111 {
112         /* Nothing special */
113 }
114
115 /*
116  * Put character to Malta LCD at given position.
117  */
118 static void
119 malta_lcd_putc(int pos, char c)
120 {
121         void *addr;
122         char *ch;
123
124         if (pos < 0 || pos > 7)
125                 return;
126         addr = (void *)(MALTA_ASCII_BASE + malta_lcd_offs[pos]);
127         ch = (char *)MIPS_PHYS_TO_KSEG0(addr);
128         *ch = c;
129 }
130
131 /*
132  * Print given string on LCD.
133  */
134 static void
135 malta_lcd_print(char *str)
136 {
137         int i;
138
139         if (str == NULL)
140                 return;
141
142         for (i = 0; *str != '\0'; i++, str++)
143                 malta_lcd_putc(i, *str);
144 }
145
146 void
147 lcd_init(void)
148 {
149         malta_lcd_print("FreeBSD_");
150 }
151
152 void
153 lcd_puts(char *s)
154 {
155         malta_lcd_print(s);
156 }
157
158 #ifdef TICK_USE_MALTA_RTC
159 static __inline uint8_t
160 rtcin(uint8_t addr)
161 {
162
163         *((volatile uint8_t *)
164             MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
165         return (*((volatile uint8_t *)
166             MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))));
167 }
168
169 static __inline void
170 writertc(uint8_t addr, uint8_t val)
171 {
172
173         *((volatile uint8_t *)
174             MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
175         *((volatile uint8_t *)
176             MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))) = val;
177 }
178 #endif
179
180 static void
181 mips_init(unsigned long memsize, uint64_t ememsize)
182 {
183         int i;
184
185         for (i = 0; i < PHYS_AVAIL_ENTRIES; i++) {
186                 phys_avail[i] = 0;
187         }
188
189         /*
190          * memsize is the amount of RAM available below 256MB.
191          * ememsize is the total amount of RAM available.
192          *
193          * The second bank starts at 0x90000000.
194          */
195
196         /* phys_avail regions are in bytes */
197         phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
198         phys_avail[1] = memsize;
199         dump_avail[0] = 0;
200         dump_avail[1] = phys_avail[1];
201
202         /* Only specify the extended region if it's set */
203         if (ememsize > memsize) {
204                 phys_avail[2] = 0x90000000;
205                 phys_avail[3] = 0x90000000 + (ememsize - memsize);
206                 dump_avail[2] = phys_avail[2];
207                 dump_avail[3] = phys_avail[3];
208         }
209
210         /* XXX realmem assigned in the caller of mips_init() */
211         physmem = realmem;
212
213         init_param1();
214         init_param2(physmem);
215         mips_cpu_init();
216         pmap_bootstrap();
217         mips_proc0_init();
218         mutex_init();
219         kdb_init();
220 #ifdef KDB
221         if (boothowto & RB_KDB)
222                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
223 #endif
224 }
225
226 /*
227  * Perform a board-level soft-reset.
228  * Note that this is not emulated by gxemul.
229  */
230 void
231 platform_reset(void)
232 {
233         char *c;
234
235         c = (char *)MIPS_PHYS_TO_KSEG0(MALTA_SOFTRES);
236         *c = MALTA_GORESET;
237 }
238
239 static uint64_t
240 malta_cpu_freq(void)
241 {
242         uint64_t platform_counter_freq = 0;
243
244 #if defined(TICK_USE_YAMON_FREQ)
245         /*
246          * If we are running on a board which uses YAMON firmware,
247          * then query CPU pipeline clock from the syscon object.
248          * If unsuccessful, use hard-coded default.
249          */
250         platform_counter_freq = yamon_getcpufreq();
251
252 #elif defined(TICK_USE_MALTA_RTC)
253         /*
254          * If we are running on a board with the MC146818 RTC,
255          * use it to determine CPU pipeline clock frequency.
256          */
257         u_int64_t counterval[2];
258
259         /* Set RTC to binary mode. */
260         writertc(RTC_STATUSB, (rtcin(RTC_STATUSB) | RTCSB_BCD));
261
262         /* Busy-wait for falling edge of RTC update. */
263         while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
264                 ;
265         while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
266                 ;
267         counterval[0] = mips_rd_count();
268
269         /* Busy-wait for falling edge of RTC update. */
270         while (((rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
271                 ;
272         while (((rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
273                 ;
274         counterval[1] = mips_rd_count();
275
276         platform_counter_freq = counterval[1] - counterval[0];
277 #endif
278
279         if (platform_counter_freq == 0)
280                 platform_counter_freq = MIPS_DEFAULT_HZ;
281
282         return (platform_counter_freq);
283 }
284
285 void
286 platform_start(__register_t a0, __register_t a1,  __register_t a2, 
287     __register_t a3)
288 {
289         vm_offset_t kernend;
290         uint64_t platform_counter_freq;
291         int argc = a0;
292         int32_t *argv = (int32_t*)a1;
293         int32_t *envp = (int32_t*)a2;
294         unsigned int memsize = a3;
295         uint64_t ememsize = 0;
296         int i;
297
298         /* clear the BSS and SBSS segments */
299         kernend = (vm_offset_t)&end;
300         memset(&edata, 0, kernend - (vm_offset_t)(&edata));
301
302         mips_postboot_fixup();
303
304         mips_pcpu0_init();
305         platform_counter_freq = malta_cpu_freq();
306         mips_timer_early_init(platform_counter_freq);
307         init_static_kenv(boot1_env, sizeof(boot1_env));
308
309         cninit();
310         printf("entry: platform_start()\n");
311
312         bootverbose = 1;
313
314         /* 
315          * YAMON uses 32bit pointers to strings so
316          * convert them to proper type manually
317          */
318
319         if (bootverbose) {
320                 printf("cmd line: ");
321                 for (i = 0; i < argc; i++)
322                         printf("%s ", (char*)(intptr_t)argv[i]);
323                 printf("\n");
324         }
325
326         if (bootverbose)
327                 printf("envp:\n");
328
329         /*
330          * Parse the environment for things like ememsize.
331          */
332         for (i = 0; envp[i]; i += 2) {
333                 const char *a, *v;
334
335                 a = (char *)(intptr_t)envp[i];
336                 v = (char *)(intptr_t)envp[i+1];
337
338                 if (bootverbose)
339                         printf("\t%s = %s\n", a, v);
340
341                 if (strcmp(a, "ememsize") == 0) {
342                         ememsize = strtoul(v, NULL, 0);
343                 }
344         }
345
346         if (bootverbose) {
347                 printf("memsize = %llu (0x%08x)\n",
348                     (unsigned long long) memsize, memsize);
349                 printf("ememsize = %llu\n", (unsigned long long) ememsize);
350
351 #ifdef __mips_o32
352                 /*
353                  * For O32 phys_avail[] can't address memory beyond 2^32,
354                  * so cap extended memory to 2GB minus one page.
355                  */
356                 if (ememsize >= 2ULL * 1024 * 1024 * 1024)
357                         ememsize = 2ULL * 1024 * 1024 * 1024 - PAGE_SIZE;
358 #endif
359         }
360
361         /*
362          * For <= 256MB RAM amounts, ememsize should equal memsize.
363          * For > 256MB RAM amounts it's the total RAM available;
364          * split between two banks.
365          *
366          * XXX TODO: just push realmem assignment into mips_init() ?
367          */
368         realmem = btoc(ememsize);
369         mips_init(memsize, ememsize);
370
371         mips_timer_init_params(platform_counter_freq, 0);
372 }