1/*	$NetBSD: integrator_machdep.c,v 1.82 2023/06/24 05:43:26 msaitoh 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.82 2023/06/24 05:43:26 msaitoh 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
110void 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
119BootConfig bootconfig;		/* Boot config storage */
120char *boot_args = NULL;
121char *boot_file = NULL;
122
123/* Prototypes */
124
125static void	integrator_sdram_bounds	(paddr_t *, psize_t *);
126
127void	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
164int comcnspeed = CONSPEED;
165int 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
191int plcomcnspeed = PLCONSPEED;
192int plcomcnmode = PLCONMODE;
193
194#if 0
195extern struct consdev kcomcons;
196static 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 */
207void
208cpu_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. */
265static const struct pmap_devmap integrator_devmap[] = {
266#if NPLCOM > 0 && defined(PLCONSOLE)
267	DEVMAP_ENTRY(
268		UART0_BOOT_BASE,
269		IFPGA_IO_BASE + IFPGA_UART0,
270		1024 * 1024
271	),
272
273	DEVMAP_ENTRY(
274		UART1_BOOT_BASE,
275		IFPGA_IO_BASE + IFPGA_UART1,
276		1024 * 1024
277	),
278#endif
279#if NPCI > 0
280	DEVMAP_ENTRY(
281		IFPGA_PCI_IO_VBASE,
282		IFPGA_PCI_IO_BASE,
283		IFPGA_PCI_IO_VSIZE
284	),
285
286	DEVMAP_ENTRY(
287		IFPGA_PCI_CONF_VBASE,
288		IFPGA_PCI_CONF_BASE,
289		IFPGA_PCI_CONF_VSIZE
290	),
291#endif
292
293	DEVMAP_ENTRY_END
294};
295
296/*
297 * vaddr_t initarm(...)
298 *
299 * Initial entry point on startup. This gets called before main() is
300 * entered.
301 * It should be responsible for setting up everything that must be
302 * in place when main is called.
303 * This includes
304 *   Taking a copy of the boot configuration structure.
305 *   Initialising the physical console so characters can be printed.
306 *   Setting up page tables for the kernel
307 *   Relocating the kernel to the bottom of physical memory
308 */
309
310vaddr_t
311initarm(void *arg)
312{
313	extern int KERNEL_BASE_phys[];
314	paddr_t memstart;
315	psize_t memsize;
316
317	/*
318	 * Heads up ... Setup the CPU / MMU / TLB functions
319	 */
320	if (set_cpufuncs())
321		panic("cpu not recognized!");
322
323	/* map some peripheral registers */
324
325	pmap_devmap_bootstrap((vaddr_t)armreg_ttbr_read() & -L1_TABLE_SIZE,
326		integrator_devmap);
327
328	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
329
330	consinit();
331
332	/* Talk to the user */
333#define BDSTR(s)        _BDSTR(s)
334#define _BDSTR(s)       #s
335	printf("\nNetBSD/evbarm (" BDSTR(EVBARM_BOARDTYPE) ") booting ...\n");
336
337	/*
338	 * Fetch the SDRAM start/size from the CM configuration registers.
339	 */
340	integrator_sdram_bounds(&memstart, &memsize);
341
342#if defined(INTEGRATOR_CP)
343	/*
344	 * XXX QEMU reports SDRAM starting at 0x100000, but presents a flat
345	 * physical memory model. Set memstart to 0x0, so arm32_bootmem_init
346	 * doesn't get fooled later.
347	 */
348	memstart = 0;
349#endif
350
351#ifdef VERBOSE_INIT_ARM
352	printf("initarm: Configuring system ...\n");
353#endif
354
355	/* Fake bootconfig structure for the benefit of pmap.c */
356	/* XXX must make the memory description h/w independent */
357	bootconfig.dramblocks = 1;
358	bootconfig.dram[0].address = memstart;
359	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
360	bootconfig.dram[0].flags = BOOT_DRAM_CAN_DMA;
361
362	arm32_bootmem_init(bootconfig.dram[0].address,
363		bootconfig.dram[0].pages * PAGE_SIZE, (unsigned int) KERNEL_BASE_phys);
364
365	arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_LOW, 0, integrator_devmap,
366		false);
367
368#ifdef VERBOSE_INIT_ARM
369	printf("done.\n");
370#endif
371
372	return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
373}
374
375void
376consinit(void)
377{
378	static int consinit_called = 0;
379#if 0
380	char *console = CONSDEVNAME;
381#endif
382
383	if (consinit_called != 0)
384		return;
385
386	consinit_called = 1;
387
388#if NPLCOM > 0 && defined(PLCONSOLE)
389	if (PLCOMCNUNIT == 0) {
390		extern struct bus_space ifpga_common_bs_tag;
391		static struct plcom_instance ifpga_pi1 = {
392#if defined(INTEGRATOR_CP)
393			.pi_type = PLCOM_TYPE_PL011,
394#else
395			.pi_type = PLCOM_TYPE_PL010,
396#endif
397			.pi_iot = &ifpga_common_bs_tag,
398			.pi_size = IFPGA_UART_SIZE,
399			.pi_iobase = IFPGA_UART0
400		};
401
402		if (plcomcnattach(&ifpga_pi1, plcomcnspeed, IFPGA_UART_CLK,
403		      plcomcnmode, PLCOMCNUNIT))
404			panic("can't init serial console");
405		return;
406	} else if (PLCOMCNUNIT == 1) {
407		extern struct bus_space ifpga_common_bs_tag;
408		static struct plcom_instance ifpga_pi1 = {
409#if defined(INTEGRATOR_CP)
410			.pi_type = PLCOM_TYPE_PL011,
411#else
412			.pi_type = PLCOM_TYPE_PL010,
413#endif
414			.pi_iot = &ifpga_common_bs_tag,
415			.pi_size = IFPGA_UART_SIZE,
416			.pi_iobase = IFPGA_UART1
417		};
418
419		if (plcomcnattach(&ifpga_pi1, plcomcnspeed, IFPGA_UART_CLK,
420		      plcomcnmode, PLCOMCNUNIT))
421			panic("can't init serial console");
422		return;
423	}
424#endif
425#if (NCOM > 0)
426	if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
427	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
428		panic("can't init serial console @%x", CONCOMADDR);
429	return;
430#endif
431	panic("No serial console configured");
432}
433
434static void
435integrator_sdram_bounds(paddr_t *memstart, psize_t *memsize)
436{
437	volatile unsigned long *cm_sdram
438	    = (volatile unsigned long *)0x10000020;
439	volatile unsigned long *cm_stat
440	    = (volatile unsigned long *)0x10000010;
441
442	*memstart = *cm_stat & 0x00ff0000;
443
444	/*
445	 * Although the SSRAM overlaps the SDRAM, we can use the wrap-around
446	 * to access the entire bank.
447	 */
448	switch ((*cm_sdram >> 2) & 0x7)
449	{
450	case 0:
451		*memsize = 16 * 1024 * 1024;
452		break;
453	case 1:
454		*memsize = 32 * 1024 * 1024;
455		break;
456	case 2:
457		*memsize = 64 * 1024 * 1024;
458		break;
459	case 3:
460		*memsize = 128 * 1024 * 1024;
461		break;
462	case 4:
463		/* With 256M of memory there is no wrap-around.  */
464		*memsize = 256 * 1024 * 1024 - *memstart;
465		break;
466	default:
467		printf("CM_SDRAM returns unknown value, using 16M\n");
468		*memsize = 16 * 1024 * 1024;
469		break;
470	}
471}
472