xref: /netbsd-src/sys/arch/landisk/landisk/machdep.c (revision f36002f244a49908fef9cba8789032bdbf48d572)
1 /*	$NetBSD: machdep.c,v 1.24 2024/03/05 14:15:32 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
35  * All rights reserved.
36  *
37  * This code is derived from software contributed to Berkeley by
38  * William Jolitz.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)machdep.c	7.4 (Berkeley) 6/3/91
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.24 2024/03/05 14:15:32 thorpej Exp $");
69 
70 #include "opt_ddb.h"
71 #include "opt_kgdb.h"
72 #include "opt_kloader.h"
73 #include "opt_kloader_kernel_path.h"
74 #include "opt_memsize.h"
75 #include "opt_modular.h"
76 
77 #include "ksyms.h"
78 #include "scif.h"
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/mount.h>
84 #include <sys/reboot.h>
85 #include <sys/sysctl.h>
86 #include <sys/ksyms.h>
87 #include <sys/device.h>
88 #include <sys/module.h>
89 #include <sys/cpu.h>
90 
91 #include <uvm/uvm_extern.h>
92 #include <ufs/mfs/mfs_extern.h>		/* mfs_initminiroot() */
93 
94 #include <dev/cons.h>
95 
96 #include <sh3/bscreg.h>
97 #include <sh3/cpgreg.h>
98 #include <sh3/exception.h>
99 #include <sh3/wdtreg.h>
100 
101 #include <sh3/cache.h>
102 #include <sh3/cache_sh4.h>
103 #include <sh3/mmu_sh4.h>
104 
105 #include <sys/bus.h>
106 #include <machine/bootinfo.h>
107 #include <machine/pcb.h>
108 
109 #include <landisk/landisk/landiskreg.h>
110 
111 #ifdef KLOADER
112 #include <machine/kloader.h>
113 #endif
114 
115 #ifdef KGDB
116 #include <sys/kgdb.h>
117 #include <sh3/dev/scifvar.h>
118 #endif
119 
120 #if NKSYMS || defined(MODULAR) || defined(DDB)
121 #include <machine/db_machdep.h>
122 #include <ddb/db_sym.h>
123 #include <ddb/db_extern.h>
124 #include <sys/exec_elf.h>
125 #endif
126 
127 /* the following is used externally (sysctl_hw) */
128 char machine[] = MACHINE;		/* landisk */
129 char machine_arch[] = MACHINE_ARCH;	/* sh3el */
130 
131 #ifdef KLOADER
132 struct kloader_bootinfo kbootinfo;
133 #else
134 struct bootinfo _bootinfo;
135 #endif
136 struct bootinfo *bootinfo;
137 
138 void main(void) __attribute__((__noreturn__));
139 void landisk_startup(int, void *) __attribute__((__noreturn__));
140 
141 void
cpu_startup(void)142 cpu_startup(void)
143 {
144 
145 	/* XXX: show model (LANDISK/USL-5P) */
146 	cpu_setmodel("Model: I-O DATA LANDISK");
147 
148         sh_startup();
149 }
150 
151 void
landisk_startup(int howto,void * bi)152 landisk_startup(int howto, void *bi)
153 {
154 	extern char edata[], end[];
155 	vaddr_t kernend;
156 	size_t symbolsize;
157 
158 	/* Clear bss */
159 	memset(edata, 0, end - edata);
160 
161 	/* Symbol table size */
162 	symbolsize = 0;
163 #if NKSYMS || defined(MODULAR) || defined(DDB)
164 	if (memcmp(&end, ELFMAG, SELFMAG) == 0) {
165 		Elf_Ehdr *eh = (void *)end;
166 		Elf_Shdr *sh = (void *)(end + eh->e_shoff);
167 		int i;
168 
169 		for (i = 0; i < eh->e_shnum; i++, sh++) {
170 			if (sh->sh_offset > 0 &&
171 			    (sh->sh_offset + sh->sh_size) > symbolsize) {
172 				symbolsize = sh->sh_offset + sh->sh_size;
173 			}
174 		}
175 	}
176 #endif
177 
178 	/* Start to determine heap area */
179 	kernend = (vaddr_t)sh3_round_page(end + symbolsize);
180 
181 	/* Copy bootinfo */
182 #ifdef KLOADER
183 	bootinfo = &kbootinfo.bootinfo;
184 #else
185 	bootinfo = &_bootinfo;
186 #endif
187 	memcpy(bootinfo, bi, sizeof(struct bootinfo));
188 	boothowto = howto;
189 
190 	/* Initilize CPU ops. */
191 	sh_cpu_init(CPU_ARCH_SH4, CPU_PRODUCT_7751R);
192 
193 	/* Initialize console */
194 	consinit();
195 
196 	/*
197 	 * Check to see if a mini-root was loaded into memory. It resides
198 	 * at the start of the next page just after the end of BSS.
199 	 */
200 	if (boothowto & RB_MINIROOT) {
201 	        size_t fssz;
202 		fssz = sh3_round_page(mfs_initminiroot((void *)kernend));
203 #ifdef MEMORY_DISK_DYNAMIC
204 		md_root_setconf((void *)kernend, fssz);
205 #endif
206 		kernend += fssz;
207 	}
208 
209 #ifdef KLOADER
210 	/* copy boot parameter for kloader */
211 	kloader_bootinfo_set(&kbootinfo, 0, NULL, NULL, true);
212 #endif
213 
214 	/* Load memory to UVM */
215 	physmem = atop(IOM_RAM_SIZE);
216 	kernend = atop(round_page(SH3_P1SEG_TO_PHYS(kernend)));
217 	uvm_page_physload(
218 		kernend, atop(IOM_RAM_BEGIN + IOM_RAM_SIZE),
219 		kernend, atop(IOM_RAM_BEGIN + IOM_RAM_SIZE),
220 		VM_FREELIST_DEFAULT);
221 
222 	/* Initialize proc0 u-area */
223 	sh_proc0_init();
224 
225 	/* Initialize pmap and start to address translation */
226 	pmap_bootstrap();
227 
228 	/* Debugger. */
229 #if NKSYMS || defined(MODULAR) || defined(DDB)
230 	if (symbolsize != 0) {
231 		ksyms_addsyms_elf(symbolsize, &end, end + symbolsize);
232 	}
233 #endif
234 #if defined(DDB)
235 	if (boothowto & RB_KDB) {
236 		Debugger();
237 	}
238 #endif
239 #if defined(KGDB) && (NSCIF > 0)
240 	if (scif_kgdb_init() == 0) {
241 		if (boothowto & RB_KDB) {
242 			kgdb_debug_init = 1;
243 			kgdb_connect(1);
244 		}
245 	}
246 #endif /* KGDB && NSCIF > 0 */
247 
248 	/* Jump to main */
249 	__asm volatile(
250 		"jmp	@%0;"
251 		"mov	%1, sp"
252 		:: "r" (main), "r" (lwp0.l_md.md_pcb->pcb_sf.sf_r7_bank));
253 	/* NOTREACHED */
254 	for (;;) {
255 		continue;
256 	}
257 }
258 
259 void *
lookup_bootinfo(int type)260 lookup_bootinfo(int type)
261 {
262 	struct btinfo_common *help;
263 	int n = bootinfo->nentries;
264 
265 	help = (struct btinfo_common *)(bootinfo->info);
266 	while (n--) {
267 		if (help->type == type)
268 			return (help);
269 		help = (struct btinfo_common *)((char *)help + help->len);
270 	}
271 	return (NULL);
272 }
273 
274 /*
275  * machine dependent system variables.
276  */
277 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
278 {
279 
280 	sysctl_createv(clog, 0, NULL, NULL,
281 		       CTLFLAG_PERMANENT,
282 		       CTLTYPE_NODE, "machdep", NULL,
283 		       NULL, 0, NULL, 0,
284 		       CTL_MACHDEP, CTL_EOL);
285 
286 	sysctl_createv(clog, 0, NULL, NULL,
287 		       CTLFLAG_PERMANENT,
288 		       CTLTYPE_STRUCT, "console_device", NULL,
289 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
290 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
291 }
292 
293 void
cpu_reboot(int howto,char * bootstr)294 cpu_reboot(int howto, char *bootstr)
295 {
296 
297 	if (cold) {
298 		howto |= RB_HALT;
299 		goto haltsys;
300 	}
301 
302 #ifdef KLOADER
303 	if ((howto & RB_HALT) == 0) {
304 		if ((howto & RB_STRING) && (bootstr != NULL)) {
305 			kloader_reboot_setup(bootstr);
306 		}
307 #ifdef KLOADER_KERNEL_PATH
308 		else {
309 			kloader_reboot_setup(KLOADER_KERNEL_PATH);
310 		}
311 #endif
312 	}
313 #endif
314 
315 	boothowto = howto;
316 	if ((howto & RB_NOSYNC) == 0) {
317 		vfs_shutdown();
318 	}
319 
320 	/* wait 1s */
321 	delay(1 * 1000 * 1000);
322 
323 	/* Disable interrupts. */
324 	splhigh();
325 
326 	/* Do a dump if requested. */
327 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) {
328 		dumpsys();
329 	}
330 
331 haltsys:
332 	doshutdownhooks();
333 
334 	pmf_system_shutdown(boothowto);
335 
336 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
337 		_reg_write_1(LANDISK_PWRMNG, PWRMNG_POWEROFF);
338 		delay(1 * 1000 * 1000);
339 		printf("POWEROFF FAILED!\n");
340 	}
341 
342 	if (howto & RB_HALT) {
343 		printf("\n");
344 		printf("The operating system has halted.\n");
345 		printf("Please press any key to reboot.\n\n");
346 		cngetc();
347 	}
348 #ifdef KLOADER
349 	else {
350 		delay(1 * 1000 * 1000);
351 		kloader_reboot();
352 		printf("\n");
353 		printf("Failed to load a new kernel.\n");
354 		printf("Please press any key to reboot.\n\n");
355 		cngetc();
356 	}
357 #endif
358 
359 	printf("rebooting...\n");
360 	machine_reset();
361 	/*NOTREACHED*/
362 }
363 
364 void
machine_reset(void)365 machine_reset(void)
366 {
367 
368 	_cpu_exception_suspend();
369 	asm("trapa #0");
370 
371 	/*NOTREACHED*/
372 	for (;;) {
373 		continue;
374 	}
375 }
376 
377 #if !defined(DONT_INIT_BSC)
378 /*
379  * InitializeBsc
380  * : BSC(Bus State Controller)
381  */
382 void InitializeBsc(void);
383 
384 void
InitializeBsc(void)385 InitializeBsc(void)
386 {
387 
388 	/*
389 	 * Drive RAS,CAS in stand by mode and bus release mode
390 	 * Area0 = Normal memory, Area5,6=Normal(no burst)
391 	 * Area2 = Normal memory, Area3 = SDRAM, Area5 = Normal memory
392 	 * Area4 = Normal Memory
393 	 * Area6 = Normal memory
394 	 */
395 	_reg_write_4(SH4_BCR1, BSC_BCR1_VAL);
396 
397 	/*
398 	 * Bus Width
399 	 * Area4: Bus width = 16bit
400 	 * Area6,5 = 16bit
401 	 * Area1 = 8bit
402 	 * Area2,3: Bus width = 32bit
403 	 */
404 	_reg_write_2(SH4_BCR2, BSC_BCR2_VAL);
405 
406 #if defined(SH4) && defined(SH7751R)
407 	if (cpu_product == CPU_PRODUCT_7751R) {
408 #ifdef BSC_BCR3_VAL
409 		_reg_write_2(SH4_BCR3, BSC_BCR3_VAL);
410 #endif
411 #ifdef BSC_BCR4_VAL
412 		_reg_write_4(SH4_BCR4, BSC_BCR4_VAL);
413 #endif
414 	}
415 #endif	/* SH4 && SH7751R */
416 
417 	/*
418 	 * Idle cycle number in transition area and read to write
419 	 * Area6 = 3, Area5 = 3, Area4 = 3, Area3 = 3, Area2 = 3
420 	 * Area1 = 3, Area0 = 3
421 	 */
422 	_reg_write_4(SH4_WCR1, BSC_WCR1_VAL);
423 
424 	/*
425 	 * Wait cycle
426 	 * Area 6 = 6
427 	 * Area 5 = 2
428 	 * Area 4 = 10
429 	 * Area 3 = 3
430 	 * Area 2,1 = 3
431 	 * Area 0 = 6
432 	 */
433 	_reg_write_4(SH4_WCR2, BSC_WCR2_VAL);
434 
435 #ifdef BSC_WCR3_VAL
436 	_reg_write_4(SH4_WCR3, BSC_WCR3_VAL);
437 #endif
438 
439 	/*
440 	 * RAS pre-charge = 2cycle, RAS-CAS delay = 3 cycle,
441 	 * write pre-charge=1cycle
442 	 * CAS before RAS refresh RAS assert time = 3 cycle
443 	 * Disable burst, Bus size=32bit, Column Address=10bit, Refresh ON
444 	 * CAS before RAS refresh ON, EDO DRAM
445 	 */
446 	_reg_write_4(SH4_MCR, BSC_MCR_VAL);
447 
448 #ifdef BSC_SDMR2_VAL
449 	_reg_write_1(BSC_SDMR2_VAL, 0);
450 #endif
451 
452 #ifdef BSC_SDMR3_VAL
453 	_reg_write_1(BSC_SDMR3_VAL, 0);
454 #endif /* BSC_SDMR3_VAL */
455 
456 	/*
457 	 * PCMCIA Control Register
458 	 * OE/WE assert delay 3.5 cycle
459 	 * OE/WE negate-address delay 3.5 cycle
460 	 */
461 #ifdef BSC_PCR_VAL
462 	_reg_write_2(SH4_PCR, BSC_PCR_VAL);
463 #endif
464 
465 	/*
466 	 * Refresh Timer Control/Status Register
467 	 * Disable interrupt by CMF, closk 1/16, Disable OVF interrupt
468 	 * Count Limit = 1024
469 	 * In following statement, the reason why high byte = 0xa5(a4 in RFCR)
470 	 * is the rule of SH3 in writing these register.
471 	 */
472 	_reg_write_2(SH4_RTCSR, BSC_RTCSR_VAL);
473 
474 	/*
475 	 * Refresh Timer Counter
476 	 * Initialize to 0
477 	 */
478 #ifdef BSC_RTCNT_VAL
479 	_reg_write_2(SH4_RTCNT, BSC_RTCNT_VAL);
480 #endif
481 
482 	/* set Refresh Time Constant Register */
483 	_reg_write_2(SH4_RTCOR, BSC_RTCOR_VAL);
484 
485 	/* init Refresh Count Register */
486 #ifdef BSC_RFCR_VAL
487 	_reg_write_2(SH4_RFCR, BSC_RFCR_VAL);
488 #endif
489 
490 	/*
491 	 * Clock Pulse Generator
492 	 */
493 	/* Set Clock mode (make internal clock double speed) */
494 	_reg_write_2(SH4_FRQCR, FRQCR_VAL);
495 }
496 #endif /* !DONT_INIT_BSC */
497 
498 
499 #ifdef MODULAR
500 /*
501  * Push any modules loaded by the boot loader.
502  */
503 void
module_init_md(void)504 module_init_md(void)
505 {
506 }
507 #endif /* MODULAR */
508