xref: /netbsd-src/sys/arch/evbarm/integrator/integrator_machdep.c (revision 4f645668ed707e1f969c546666f8c8e45e6f8888)
1 /*	$NetBSD: integrator_machdep.c,v 1.80 2021/08/10 06:47:49 skrll Exp $	*/
2 
3 /*
4  * Copyright (c) 2001,2002 ARM Ltd
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  * 3. The name of the company may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ARM LTD
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1997,1998 Mark Brinicombe.
34  * Copyright (c) 1997,1998 Causality Limited.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by Mark Brinicombe
48  *	for the NetBSD Project.
49  * 4. The name of the company nor the name of the author may be used to
50  *    endorse or promote products derived from this software without specific
51  *    prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
54  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
57  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  * Machine dependent functions for kernel setup for integrator board
66  *
67  * Created      : 24/11/97
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: integrator_machdep.c,v 1.80 2021/08/10 06:47:49 skrll Exp $");
72 
73 #include "opt_arm_debug.h"
74 #include "opt_console.h"
75 #include "opt_ddb.h"
76 
77 #include <sys/param.h>
78 #include <sys/device.h>
79 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/exec.h>
82 #include <sys/proc.h>
83 #include <sys/msgbuf.h>
84 #include <sys/reboot.h>
85 #include <sys/termios.h>
86 #include <sys/ksyms.h>
87 #include <sys/bus.h>
88 #include <sys/cpu.h>
89 #include <sys/intr.h>
90 
91 #include <uvm/uvm_extern.h>
92 
93 #include <dev/cons.h>
94 
95 #include <machine/db_machdep.h>
96 #include <ddb/db_sym.h>
97 #include <ddb/db_extern.h>
98 
99 #include <machine/bootconfig.h>
100 #include <arm/locore.h>
101 #include <arm/undefined.h>
102 
103 #include <arm/arm32/machdep.h>
104 
105 #include <evbarm/integrator/integrator_boot.h>
106 
107 #include "pci.h"
108 #include "ksyms.h"
109 
110 void ifpga_reset(void) __attribute__((noreturn));
111 
112 /*
113  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
114  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
115  */
116 #define KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
117 #define KERNEL_VM_SIZE		0x0C000000
118 
119 BootConfig bootconfig;		/* Boot config storage */
120 char *boot_args = NULL;
121 char *boot_file = NULL;
122 
123 /* Prototypes */
124 
125 static void	integrator_sdram_bounds	(paddr_t *, psize_t *);
126 
127 void	consinit(void);
128 
129 /* A load of console goo. */
130 #include "vga.h"
131 #if NVGA > 0
132 #include <dev/ic/mc6845reg.h>
133 #include <dev/ic/pcdisplayvar.h>
134 #include <dev/ic/vgareg.h>
135 #include <dev/ic/vgavar.h>
136 #endif
137 
138 #include "pckbc.h"
139 #if NPCKBC > 0
140 #include <dev/ic/i8042reg.h>
141 #include <dev/ic/pckbcvar.h>
142 #endif
143 
144 #include "com.h"
145 #if NCOM > 0
146 #include <dev/ic/comreg.h>
147 #include <dev/ic/comvar.h>
148 #ifndef CONCOMADDR
149 #define CONCOMADDR 0x3f8
150 #endif
151 #endif
152 
153 /*
154  * Define the default console speed for the board.  This is generally
155  * what the firmware provided with the board defaults to.
156  */
157 #ifndef CONSPEED
158 #define CONSPEED B115200
159 #endif
160 #ifndef CONMODE
161 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
162 #endif
163 
164 int comcnspeed = CONSPEED;
165 int comcnmode = CONMODE;
166 
167 #include "plcom.h"
168 #if (NPLCOM > 0)
169 #include <evbarm/dev/plcomreg.h>
170 #include <evbarm/dev/plcomvar.h>
171 
172 #include <evbarm/ifpga/ifpgamem.h>
173 #include <evbarm/ifpga/ifpgareg.h>
174 #include <evbarm/ifpga/ifpgavar.h>
175 #endif
176 
177 #ifndef CONSDEVNAME
178 #define CONSDEVNAME "plcom"
179 #endif
180 
181 #ifndef PLCONSPEED
182 #define PLCONSPEED B38400
183 #endif
184 #ifndef PLCONMODE
185 #define PLCONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
186 #endif
187 #ifndef PLCOMCNUNIT
188 #define PLCOMCNUNIT -1
189 #endif
190 
191 int plcomcnspeed = PLCONSPEED;
192 int plcomcnmode = PLCONMODE;
193 
194 #if 0
195 extern struct consdev kcomcons;
196 static void kcomcnputc(dev_t, int);
197 #endif
198 
199 /*
200  * void cpu_reboot(int howto, char *bootstr)
201  *
202  * Reboots the system
203  *
204  * Deal with any syncing, unmounting, dumping and shutdown hooks,
205  * then reset the CPU.
206  */
207 void
208 cpu_reboot(int howto, char *bootstr)
209 {
210 
211 	/*
212 	 * If we are still cold then hit the air brakes
213 	 * and crash to earth fast
214 	 */
215 	if (cold) {
216 		doshutdownhooks();
217 		pmf_system_shutdown(boothowto);
218 		printf("The operating system has halted.\n");
219 		printf("Please press any key to reboot.\n\n");
220 		cngetc();
221 		printf("rebooting...\n");
222 		ifpga_reset();
223 		/*NOTREACHED*/
224 	}
225 
226 	/* Disable console buffering */
227 
228 	/*
229 	 * If RB_NOSYNC was not specified sync the discs.
230 	 * Note: Unless cold is set to 1 here, syslogd will die during the
231 	 * unmount.  It looks like syslogd is getting woken up only to find
232 	 * that it cannot page part of the binary in as the filesystem has
233 	 * been unmounted.
234 	 */
235 	if (!(howto & RB_NOSYNC))
236 		bootsync();
237 
238 	/* Say NO to interrupts */
239 	splhigh();
240 
241 	/* Do a dump if requested. */
242 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
243 		dumpsys();
244 
245 	/* Run any shutdown hooks */
246 	doshutdownhooks();
247 
248 	pmf_system_shutdown(boothowto);
249 
250 	/* Make sure IRQ's are disabled */
251 	IRQdisable;
252 
253 	if (howto & RB_HALT) {
254 		printf("The operating system has halted.\n");
255 		printf("Please press any key to reboot.\n\n");
256 		cngetc();
257 	}
258 
259 	printf("rebooting...\n");
260 	ifpga_reset();
261 	/*NOTREACHED*/
262 }
263 
264 /* Statically mapped devices. */
265 static const struct pmap_devmap integrator_devmap[] = {
266 #if NPLCOM > 0 && defined(PLCONSOLE)
267 	{
268 		UART0_BOOT_BASE,
269 		IFPGA_IO_BASE + IFPGA_UART0,
270 		1024 * 1024,
271 		VM_PROT_READ|VM_PROT_WRITE,
272 		PTE_NOCACHE
273 	},
274 
275 	{
276 		UART1_BOOT_BASE,
277 		IFPGA_IO_BASE + IFPGA_UART1,
278 		1024 * 1024,
279 		VM_PROT_READ|VM_PROT_WRITE,
280 		PTE_NOCACHE
281 	},
282 #endif
283 #if NPCI > 0
284 	{
285 		IFPGA_PCI_IO_VBASE,
286 		IFPGA_PCI_IO_BASE,
287 		IFPGA_PCI_IO_VSIZE,
288 		VM_PROT_READ|VM_PROT_WRITE,
289 		PTE_NOCACHE
290 	},
291 
292 	{
293 		IFPGA_PCI_CONF_VBASE,
294 		IFPGA_PCI_CONF_BASE,
295 		IFPGA_PCI_CONF_VSIZE,
296 		VM_PROT_READ|VM_PROT_WRITE,
297 		PTE_NOCACHE
298 	},
299 #endif
300 
301 	{
302 		0,
303 		0,
304 		0,
305 		0,
306 		0
307 	}
308 };
309 
310 /*
311  * vaddr_t initarm(...)
312  *
313  * Initial entry point on startup. This gets called before main() is
314  * entered.
315  * It should be responsible for setting up everything that must be
316  * in place when main is called.
317  * This includes
318  *   Taking a copy of the boot configuration structure.
319  *   Initialising the physical console so characters can be printed.
320  *   Setting up page tables for the kernel
321  *   Relocating the kernel to the bottom of physical memory
322  */
323 
324 vaddr_t
325 initarm(void *arg)
326 {
327 	extern int KERNEL_BASE_phys[];
328 	paddr_t memstart;
329 	psize_t memsize;
330 
331 	/*
332 	 * Heads up ... Setup the CPU / MMU / TLB functions
333 	 */
334 	if (set_cpufuncs())
335 		panic("cpu not recognized!");
336 
337 	/* map some peripheral registers */
338 
339 	pmap_devmap_bootstrap((vaddr_t)armreg_ttbr_read() & -L1_TABLE_SIZE,
340 		integrator_devmap);
341 
342 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
343 
344 	consinit();
345 
346 	/* Talk to the user */
347 #define BDSTR(s)        _BDSTR(s)
348 #define _BDSTR(s)       #s
349 	printf("\nNetBSD/evbarm (" BDSTR(EVBARM_BOARDTYPE) ") booting ...\n");
350 
351 	/*
352 	 * Fetch the SDRAM start/size from the CM configuration registers.
353 	 */
354 	integrator_sdram_bounds(&memstart, &memsize);
355 
356 #if defined(INTEGRATOR_CP)
357 	/*
358 	 * XXX QEMU reports SDRAM starting at 0x100000, but presents a flat
359 	 * physical memory model. Set memstart to 0x0, so arm32_bootmem_init
360 	 * doesn't get fooled later.
361 	 */
362 	memstart = 0;
363 #endif
364 
365 #ifdef VERBOSE_INIT_ARM
366 	printf("initarm: Configuring system ...\n");
367 #endif
368 
369 	/* Fake bootconfig structure for the benefit of pmap.c */
370 	/* XXX must make the memory description h/w independent */
371 	bootconfig.dramblocks = 1;
372 	bootconfig.dram[0].address = memstart;
373 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
374 	bootconfig.dram[0].flags = BOOT_DRAM_CAN_DMA;
375 
376 	arm32_bootmem_init(bootconfig.dram[0].address,
377 		bootconfig.dram[0].pages * PAGE_SIZE, (unsigned int) KERNEL_BASE_phys);
378 
379 	arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_LOW, 0, integrator_devmap,
380 		false);
381 
382 #ifdef VERBOSE_INIT_ARM
383 	printf("done.\n");
384 #endif
385 
386 	return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
387 }
388 
389 void
390 consinit(void)
391 {
392 	static int consinit_called = 0;
393 #if 0
394 	char *console = CONSDEVNAME;
395 #endif
396 
397 	if (consinit_called != 0)
398 		return;
399 
400 	consinit_called = 1;
401 
402 #if NPLCOM > 0 && defined(PLCONSOLE)
403 	if (PLCOMCNUNIT == 0) {
404 		extern struct bus_space ifpga_common_bs_tag;
405 		static struct plcom_instance ifpga_pi1 = {
406 #if defined(INTEGRATOR_CP)
407 			.pi_type = PLCOM_TYPE_PL011,
408 #else
409 			.pi_type = PLCOM_TYPE_PL010,
410 #endif
411 			.pi_iot = &ifpga_common_bs_tag,
412 			.pi_size = IFPGA_UART_SIZE,
413 			.pi_iobase = IFPGA_UART0
414 		};
415 
416 		if (plcomcnattach(&ifpga_pi1, plcomcnspeed, IFPGA_UART_CLK,
417 		      plcomcnmode, PLCOMCNUNIT))
418 			panic("can't init serial console");
419 		return;
420 	} else if (PLCOMCNUNIT == 1) {
421 		extern struct bus_space ifpga_common_bs_tag;
422 		static struct plcom_instance ifpga_pi1 = {
423 #if defined(INTEGRATOR_CP)
424 			.pi_type = PLCOM_TYPE_PL011,
425 #else
426 			.pi_type = PLCOM_TYPE_PL010,
427 #endif
428 			.pi_iot = &ifpga_common_bs_tag,
429 			.pi_size = IFPGA_UART_SIZE,
430 			.pi_iobase = IFPGA_UART1
431 		};
432 
433 		if (plcomcnattach(&ifpga_pi1, plcomcnspeed, IFPGA_UART_CLK,
434 		      plcomcnmode, PLCOMCNUNIT))
435 			panic("can't init serial console");
436 		return;
437 	}
438 #endif
439 #if (NCOM > 0)
440 	if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
441 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
442 		panic("can't init serial console @%x", CONCOMADDR);
443 	return;
444 #endif
445 	panic("No serial console configured");
446 }
447 
448 static void
449 integrator_sdram_bounds(paddr_t *memstart, psize_t *memsize)
450 {
451 	volatile unsigned long *cm_sdram
452 	    = (volatile unsigned long *)0x10000020;
453 	volatile unsigned long *cm_stat
454 	    = (volatile unsigned long *)0x10000010;
455 
456 	*memstart = *cm_stat & 0x00ff0000;
457 
458 	/*
459 	 * Although the SSRAM overlaps the SDRAM, we can use the wrap-around
460 	 * to access the entire bank.
461 	 */
462 	switch ((*cm_sdram >> 2) & 0x7)
463 	{
464 	case 0:
465 		*memsize = 16 * 1024 * 1024;
466 		break;
467 	case 1:
468 		*memsize = 32 * 1024 * 1024;
469 		break;
470 	case 2:
471 		*memsize = 64 * 1024 * 1024;
472 		break;
473 	case 3:
474 		*memsize = 128 * 1024 * 1024;
475 		break;
476 	case 4:
477 		/* With 256M of memory there is no wrap-around.  */
478 		*memsize = 256 * 1024 * 1024 - *memstart;
479 		break;
480 	default:
481 		printf("CM_SDRAM retuns unknown value, using 16M\n");
482 		*memsize = 16 * 1024 * 1024;
483 		break;
484 	}
485 }
486