xref: /freebsd-13.1/sys/mips/malta/malta_machdep.c (revision 5731987b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Wojciech A. Koszek <[email protected]>
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/malta/maltareg.h>
77 #include <isa/rtc.h>
78 #endif
79 
80 #include <mips/malta/maltareg.h>
81 
82 extern int	*edata;
83 extern int	*end;
84 
85 void	lcd_init(void);
86 void	lcd_puts(char *);
87 void	malta_reset(void);
88 
89 /*
90  * Temporary boot environment used at startup.
91  */
92 static char boot1_env[4096];
93 
94 /*
95  * Offsets to MALTA LCD characters.
96  */
97 static int malta_lcd_offs[] = {
98 	MALTA_ASCIIPOS0,
99 	MALTA_ASCIIPOS1,
100 	MALTA_ASCIIPOS2,
101 	MALTA_ASCIIPOS3,
102 	MALTA_ASCIIPOS4,
103 	MALTA_ASCIIPOS5,
104 	MALTA_ASCIIPOS6,
105 	MALTA_ASCIIPOS7
106 };
107 
108 void
platform_cpu_init()109 platform_cpu_init()
110 {
111 	/* Nothing special */
112 }
113 
114 /*
115  * Put character to Malta LCD at given position.
116  */
117 static void
malta_lcd_putc(int pos,char c)118 malta_lcd_putc(int pos, char c)
119 {
120 	void *addr;
121 	char *ch;
122 
123 	if (pos < 0 || pos > 7)
124 		return;
125 	addr = (void *)(MALTA_ASCII_BASE + malta_lcd_offs[pos]);
126 	ch = (char *)MIPS_PHYS_TO_KSEG0(addr);
127 	*ch = c;
128 }
129 
130 /*
131  * Print given string on LCD.
132  */
133 static void
malta_lcd_print(char * str)134 malta_lcd_print(char *str)
135 {
136 	int i;
137 
138 	if (str == NULL)
139 		return;
140 
141 	for (i = 0; *str != '\0'; i++, str++)
142 		malta_lcd_putc(i, *str);
143 }
144 
145 void
lcd_init(void)146 lcd_init(void)
147 {
148 	malta_lcd_print("FreeBSD_");
149 }
150 
151 void
lcd_puts(char * s)152 lcd_puts(char *s)
153 {
154 	malta_lcd_print(s);
155 }
156 
157 #ifdef TICK_USE_MALTA_RTC
158 static __inline uint8_t
malta_rtcin(uint8_t addr)159 malta_rtcin(uint8_t addr)
160 {
161 
162 	*((volatile uint8_t *)
163 	    MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
164 	return (*((volatile uint8_t *)
165 	    MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))));
166 }
167 
168 static __inline void
malta_writertc(uint8_t addr,uint8_t val)169 malta_writertc(uint8_t addr, uint8_t val)
170 {
171 
172 	*((volatile uint8_t *)
173 	    MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCADR))) = addr;
174 	*((volatile uint8_t *)
175 	    MIPS_PHYS_TO_KSEG1(MALTA_PCI0_ADDR(MALTA_RTCDAT))) = val;
176 }
177 #endif
178 
179 static void
mips_init(unsigned long memsize,uint64_t ememsize)180 mips_init(unsigned long memsize, uint64_t ememsize)
181 {
182 	int i;
183 
184 	for (i = 0; i < PHYS_AVAIL_ENTRIES; i++) {
185 		phys_avail[i] = 0;
186 	}
187 
188 	/*
189 	 * memsize is the amount of RAM available below 256MB.
190 	 * ememsize is the total amount of RAM available.
191 	 *
192 	 * The second bank starts at 0x90000000.
193 	 */
194 
195 	/* phys_avail regions are in bytes */
196 	phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
197 	phys_avail[1] = memsize;
198 	dump_avail[0] = 0;
199 	dump_avail[1] = phys_avail[1];
200 
201 	/* Only specify the extended region if it's set */
202 	if (ememsize > memsize) {
203 		phys_avail[2] = 0x90000000;
204 		phys_avail[3] = 0x90000000 + (ememsize - memsize);
205 		dump_avail[2] = phys_avail[2];
206 		dump_avail[3] = phys_avail[3];
207 	}
208 
209 	/* XXX realmem assigned in the caller of mips_init() */
210 	physmem = realmem;
211 
212 	init_param1();
213 	init_param2(physmem);
214 	mips_cpu_init();
215 	pmap_bootstrap();
216 	mips_proc0_init();
217 	mutex_init();
218 	kdb_init();
219 #ifdef KDB
220 	if (boothowto & RB_KDB)
221 		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
222 #endif
223 }
224 
225 /*
226  * Perform a board-level soft-reset.
227  * Note that this is not emulated by gxemul.
228  */
229 void
platform_reset(void)230 platform_reset(void)
231 {
232 	char *c;
233 
234 	c = (char *)MIPS_PHYS_TO_KSEG0(MALTA_SOFTRES);
235 	*c = MALTA_GORESET;
236 }
237 
238 static uint64_t
malta_cpu_freq(void)239 malta_cpu_freq(void)
240 {
241 	uint64_t platform_counter_freq = 0;
242 
243 #if defined(TICK_USE_YAMON_FREQ)
244 	/*
245 	 * If we are running on a board which uses YAMON firmware,
246 	 * then query CPU pipeline clock from the syscon object.
247 	 * If unsuccessful, use hard-coded default.
248 	 */
249 	platform_counter_freq = yamon_getcpufreq();
250 
251 #elif defined(TICK_USE_MALTA_RTC)
252 	/*
253 	 * If we are running on a board with the MC146818 RTC,
254 	 * use it to determine CPU pipeline clock frequency.
255 	 */
256 	u_int64_t counterval[2];
257 
258 	/* Set RTC to binary mode. */
259 	malta_writertc(RTC_STATUSB, (malta_rtcin(RTC_STATUSB) | RTCSB_BCD));
260 
261 	/* Busy-wait for falling edge of RTC update. */
262 	while (((malta_rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
263 		;
264 	while (((malta_rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
265 		;
266 	counterval[0] = mips_rd_count();
267 
268 	/* Busy-wait for falling edge of RTC update. */
269 	while (((malta_rtcin(RTC_STATUSA) & RTCSA_TUP) == 0))
270 		;
271 	while (((malta_rtcin(RTC_STATUSA)& RTCSA_TUP) != 0))
272 		;
273 	counterval[1] = mips_rd_count();
274 
275 	platform_counter_freq = counterval[1] - counterval[0];
276 #endif
277 
278 	if (platform_counter_freq == 0)
279 		platform_counter_freq = MIPS_DEFAULT_HZ;
280 
281 	return (platform_counter_freq);
282 }
283 
284 void
platform_start(__register_t a0,__register_t a1,__register_t a2,__register_t a3)285 platform_start(__register_t a0, __register_t a1,  __register_t a2,
286     __register_t a3)
287 {
288 	vm_offset_t kernend;
289 	uint64_t platform_counter_freq;
290 	int argc = a0;
291 	int32_t *argv = (int32_t*)a1;
292 	int32_t *envp = (int32_t*)a2;
293 	unsigned int memsize = a3;
294 	uint64_t ememsize = 0;
295 	int i;
296 
297 	/* clear the BSS and SBSS segments */
298 	kernend = (vm_offset_t)&end;
299 	memset(&edata, 0, kernend - (vm_offset_t)(&edata));
300 
301 	mips_postboot_fixup();
302 
303 	mips_pcpu0_init();
304 	platform_counter_freq = malta_cpu_freq();
305 	mips_timer_early_init(platform_counter_freq);
306 	init_static_kenv(boot1_env, sizeof(boot1_env));
307 
308 	cninit();
309 	printf("entry: platform_start()\n");
310 
311 	bootverbose = 1;
312 
313 	/*
314 	 * YAMON uses 32bit pointers to strings so
315 	 * convert them to proper type manually
316 	 */
317 
318 	if (bootverbose) {
319 		printf("cmd line: ");
320 		for (i = 0; i < argc; i++)
321 			printf("%s ", (char*)(intptr_t)argv[i]);
322 		printf("\n");
323 	}
324 
325 	if (bootverbose)
326 		printf("envp:\n");
327 
328 	/*
329 	 * Parse the environment for things like ememsize.
330 	 */
331 	for (i = 0; envp[i]; i += 2) {
332 		const char *a, *v;
333 
334 		a = (char *)(intptr_t)envp[i];
335 		v = (char *)(intptr_t)envp[i+1];
336 
337 		if (bootverbose)
338 			printf("\t%s = %s\n", a, v);
339 
340 		if (strcmp(a, "ememsize") == 0) {
341 			ememsize = strtoul(v, NULL, 0);
342 		}
343 	}
344 
345 	if (bootverbose) {
346 		printf("memsize = %llu (0x%08x)\n",
347 		    (unsigned long long) memsize, memsize);
348 		printf("ememsize = %llu\n", (unsigned long long) ememsize);
349 
350 #ifdef __mips_o32
351 		/*
352 		 * For O32 phys_avail[] can't address memory beyond 2^32,
353 		 * so cap extended memory to 2GB minus one page.
354 		 */
355 		if (ememsize >= 2ULL * 1024 * 1024 * 1024)
356 			ememsize = 2ULL * 1024 * 1024 * 1024 - PAGE_SIZE;
357 #endif
358 	}
359 
360 	/*
361 	 * For <= 256MB RAM amounts, ememsize should equal memsize.
362 	 * For > 256MB RAM amounts it's the total RAM available;
363 	 * split between two banks.
364 	 *
365 	 * XXX TODO: just push realmem assignment into mips_init() ?
366 	 */
367 	realmem = btoc(ememsize);
368 	mips_init(memsize, ememsize);
369 
370 	mips_timer_init_params(platform_counter_freq, 0);
371 }
372