xref: /netbsd-src/sys/arch/evbarm/armadillo/armadillo9_machdep.c (revision 5fdf9d1befdde982def6f1a6a9cd016f1b31fcee)
1 /*	$NetBSD: armadillo9_machdep.c,v 1.41 2024/02/20 23:36:02 andvar Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Based on code written by Jason R. Thorpe and Steve C. Woodford for
8  * Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed for the NetBSD Project by
21  *	Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1997,1998 Mark Brinicombe.
41  * Copyright (c) 1997,1998 Causality Limited.
42  * All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This product includes software developed by Mark Brinicombe
55  *	for the NetBSD Project.
56  * 4. The name of the company nor the name of the author may be used to
57  *    endorse or promote products derived from this software without specific
58  *    prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
61  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  * Machine dependent functions for kernel setup for Armadillo.
73  */
74 
75 /*	Armadillo-9 physical memory map
76 	0000 0000 - 0fff ffff	reserved
77 	1000 0000 - 1000 000f	I/O Control Register
78 	1000 0010 - 11dd ffff	reserved
79 	1200 0000 - 1200 ffff	PC/104 I/O space (8bit)
80 	1201 0000 - 12ff ffff	reserved
81 	1300 0000 - 13ff ffff	PC/104 Memory space (8bit)
82 	1400 0000 - 1fff ffff	reserved
83 	2000 0000 - 21ff ffff	reserved
84 	2200 0000 - 2200 ffff	PC/104 I/O space (16bit)
85 	2201 0000 - 22ff ffff	reserved
86 	2300 0000 - 23ff ffff	PC/104 Memory space (16bit)
87 	2400 0000 - 2fff ffff	reserved
88 	3000 0000 - 3fff ffff	reserved
89 	4000 0000 - 43ff ffff	Compact Flash I/O space
90 	4400 0000 - 47ff ffff	reserved
91 	4800 0000 - 4bff ffff	Compact Flash Attribute space
92 	4c00 0000 - 4fff ffff	Compact Flash memory space
93 	5000 0000 - 5fff ffff	reserved
94 	6000 0000 - 607f ffff	Flash Memory (8MByte)
95 	6080 0000 - 6fff ffff	reserved
96 	7000 0000 - 7fff ffff	reserved
97 	8000 0000 - 8008 ffff	EP9315 Internal Register (AHB)
98 	8009 0000 - 8009 3fff	Internal Boot ROM (16kByte)
99 	8009 4000 - 8009 ffff	reserved
100 	800a 0000 - 800f ffff	EP9315 Internal Register (AHB)
101 	8010 0000 - 807f ffff	reserved
102 	8080 0000 - 8094 ffff	EP9315 Internal Register (APB)
103 	8095 0000 - 8fff ffff	reserved
104 	9000 0000 - bfff ffff	reserved
105 	c000 0000 - c1ff ffff	SDRAM (32MByte)
106 	c200 0000 - c3ff ffff	reserved
107 	c400 0000 - c5ff ffff	SDRAM (32MByte)
108 	c600 0000 - cfff ffff	reserved
109 	d000 0000 - ffff ffff	reserved
110 */
111 
112 #include <sys/cdefs.h>
113 __KERNEL_RCSID(0, "$NetBSD: armadillo9_machdep.c,v 1.41 2024/02/20 23:36:02 andvar Exp $");
114 
115 #include "opt_arm_debug.h"
116 #include "opt_console.h"
117 #include "opt_ddb.h"
118 #include "opt_kgdb.h"
119 
120 #include <sys/param.h>
121 #include <sys/device.h>
122 #include <sys/systm.h>
123 #include <sys/kernel.h>
124 #include <sys/exec.h>
125 #include <sys/proc.h>
126 #include <sys/msgbuf.h>
127 #include <sys/reboot.h>
128 #include <sys/termios.h>
129 #include <sys/ksyms.h>
130 #include <sys/bus.h>
131 #include <sys/cpu.h>
132 
133 #include <net/if.h>
134 #include <net/if_ether.h>
135 
136 #include <uvm/uvm_extern.h>
137 
138 #include <dev/cons.h>
139 
140 #include <machine/db_machdep.h>
141 #include <ddb/db_sym.h>
142 #include <ddb/db_extern.h>
143 
144 #define	DRAM_BLOCKS	4
145 #include <machine/bootconfig.h>
146 #include <machine/autoconf.h>
147 #include <arm/locore.h>
148 #include <arm/undefined.h>
149 
150 /* Define various stack sizes in pages */
151 #define IRQ_STACK_SIZE	8
152 #define ABT_STACK_SIZE	8
153 #define UND_STACK_SIZE	8
154 
155 #include <arm/arm32/machdep.h>
156 
157 #include <arm/ep93xx/ep93xxreg.h>
158 #include <arm/ep93xx/ep93xxvar.h>
159 
160 #include "epwdog.h"
161 #if NEPWDOG > 0
162 #include <arm/ep93xx/epwdogvar.h>
163 #endif
164 #include <arm/ep93xx/epwdogreg.h>
165 
166 #include <dev/ic/comreg.h>
167 #include <dev/ic/comvar.h>
168 
169 #include "epcom.h"
170 #if NEPCOM > 0
171 #include <arm/ep93xx/epcomvar.h>
172 #endif
173 
174 #include "isa.h"
175 #if NISA > 0
176 #include <dev/isa/isareg.h>
177 #include <dev/isa/isavar.h>
178 #endif
179 
180 #include <machine/isa_machdep.h>
181 
182 #include <evbarm/armadillo/armadillo9reg.h>
183 #include <evbarm/armadillo/armadillo9var.h>
184 
185 struct armadillo_model_t *armadillo_model = 0;
186 static struct armadillo_model_t armadillo_model_table[] = {
187 	{ DEVCFG_ARMADILLO9, "Armadillo-9" },
188 	{ DEVCFG_ARMADILLO210, "Armadillo-210" },
189 	{ 0, "Armadillo(unknown model)" } };
190 
191 #include "ksyms.h"
192 
193 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
194 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
195 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
196 
197 /*
198  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
199  * Core-logic registers and I/O mappings occupy 0xf0000000 - 0xffffffff
200  */
201 #define KERNEL_VM_SIZE		0x0c000000
202 
203 
204 BootConfig bootconfig;	/* Boot config storage */
205 char *boot_args = NULL;
206 char *boot_file = NULL;
207 
208 vaddr_t physical_start;
209 vaddr_t physical_freestart;
210 vaddr_t physical_freeend;
211 vaddr_t physical_freeend_low;
212 vaddr_t physical_end;
213 u_int free_pages;
214 
215 paddr_t msgbufphys;
216 
217 static struct arm32_dma_range armadillo9_dma_ranges[4];
218 
219 #if NISA > 0
220 extern void isa_armadillo9_init(u_int, u_int);
221 #endif
222 
223 #define KERNEL_PT_SYS		0	/* L2 table for mapping vectors page */
224 
225 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
226 #define	KERNEL_PT_KERNEL_NUM	4
227 					/* L2 tables for mapping kernel VM */
228 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
229 
230 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
231 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
232 
233 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
234 
235 /* Prototypes */
236 
237 void	consinit(void);
238 /*
239  * Define the default console speed for the machine.
240  */
241 #if NEPCOM > 0
242 #ifndef CONSPEED
243 #define CONSPEED B115200
244 #endif /* ! CONSPEED */
245 
246 #ifndef CONMODE
247 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
248 #endif
249 
250 #ifndef CONUNIT
251 #define	CONUNIT	0
252 #endif
253 
254 int comcnspeed = CONSPEED;
255 int comcnmode = CONMODE;
256 const unsigned long comaddr[] = {
257 	EP93XX_APB_UART1, EP93XX_APB_UART2 };
258 #endif
259 
260 #if KGDB
261 #ifndef KGDB_DEVNAME
262 #error Must define KGDB_DEVNAME
263 #endif
264 const char kgdb_devname[] = KGDB_DEVNAME;
265 
266 #ifndef KGDB_DEVADDR
267 #error Must define KGDB_DEVADDR
268 #endif
269 unsigned long kgdb_devaddr = KGDB_DEVADDR;
270 
271 #ifndef KGDB_DEVRATE
272 #define KGDB_DEVRATE	CONSPEED
273 #endif
274 int kgdb_devrate = KGDB_DEVRATE;
275 
276 #ifndef KGDB_DEVMODE
277 #define KGDB_DEVMODE	CONMODE
278 #endif
279 int kgdb_devmode = KGDB_DEVMODE;
280 #endif /* KGDB */
281 
282 /*
283  * MAC address for the built-in Ethernet.
284  */
285 uint8_t	armadillo9_ethaddr[ETHER_ADDR_LEN];
286 
287 static void
armadillo9_device_register(device_t dev,void * aux)288 armadillo9_device_register(device_t dev, void *aux)
289 {
290 
291 	/* MAC address for the built-in Ethernet. */
292 	if (device_is_a(dev, "epe")) {
293 		prop_data_t pd = prop_data_create_data_nocopy(
294 		    armadillo9_ethaddr, ETHER_ADDR_LEN);
295 		KASSERT(pd != NULL);
296 		if (prop_dictionary_set(device_properties(dev),
297 					"mac-address", pd) == false) {
298 			printf("WARNING: unable to set mac-addr property "
299 			    "for %s\n", device_xname(dev));
300 		}
301 		prop_object_release(pd);
302 	}
303 }
304 
305 /*
306  * void cpu_reboot(int howto, char *bootstr)
307  *
308  * Reboots the system
309  *
310  * Deal with any syncing, unmounting, dumping and shutdown hooks,
311  * then reset the CPU.
312  */
313 void
cpu_reboot(int howto,char * bootstr)314 cpu_reboot(int howto, char *bootstr)
315 {
316 	/*
317 	 * If we are still cold then hit the air brakes
318 	 * and crash to earth fast
319 	 */
320 	if (cold) {
321 		doshutdownhooks();
322 		pmf_system_shutdown(boothowto);
323 		printf("\r\n");
324 		printf("The operating system has halted.\r\n");
325 		printf("Please press any key to reboot.\r\n");
326 		cngetc();
327 		printf("\r\nrebooting...\r\n");
328 		goto reset;
329 	}
330 
331 	/* Disable console buffering */
332 
333 	/*
334 	 * If RB_NOSYNC was not specified sync the discs.
335 	 * Note: Unless cold is set to 1 here, syslogd will die during the
336 	 * unmount.  It looks like syslogd is getting woken up only to find
337 	 * that it cannot page part of the binary in as the filesystem has
338 	 * been unmounted.
339 	 */
340 	if (!(howto & RB_NOSYNC))
341 		bootsync();
342 
343 	/* Say NO to interrupts */
344 	splhigh();
345 
346 	/* Do a dump if requested. */
347 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
348 		dumpsys();
349 
350 	/* Run any shutdown hooks */
351 	doshutdownhooks();
352 
353 	pmf_system_shutdown(boothowto);
354 
355 	/* Make sure IRQ's are disabled */
356 	IRQdisable;
357 
358 	if (howto & RB_HALT) {
359 		printf("\r\n");
360 		printf("The operating system has halted.\r\n");
361 		printf("Please press any key to reboot.\r\n");
362 		cngetc();
363 	}
364 
365 	printf("\r\nrebooting...\r\n");
366  reset:
367 	/*
368 	 * Make really really sure that all interrupts are disabled,
369 	 * and poke the Internal Bus and Peripheral Bus reset lines.
370 	 */
371 	(void) disable_interrupts(I32_bit|F32_bit);
372 #if NEPWDOG > 0
373 	epwdog_reset();
374 #else
375 	{
376 	uint32_t ctrl = EP93XX_APB_VBASE + EP93XX_APB_WDOG + EP93XX_WDOG_Ctrl;
377 	uint32_t val = EP93XX_WDOG_ENABLE;
378 	__asm volatile (
379 		"str %1, [%0]\n"
380 		:
381 		: "r" (ctrl), "r" (val)
382 	);
383 	}
384 #endif
385 	for (;;);
386 }
387 
388 /* Static device mappings. */
389 static const struct pmap_devmap armadillo9_devmap[] = {
390     {
391 	EP93XX_AHB_VBASE,
392 	EP93XX_AHB_HWBASE,
393 	EP93XX_AHB_SIZE,
394 	VM_PROT_READ|VM_PROT_WRITE,
395 	PTE_NOCACHE,
396     },
397 
398     {
399 	EP93XX_APB_VBASE,
400 	EP93XX_APB_HWBASE,
401 	EP93XX_APB_SIZE,
402 	VM_PROT_READ|VM_PROT_WRITE,
403 	PTE_NOCACHE,
404     },
405 
406     {
407 	EP93XX_PCMCIA0_VBASE,
408 	EP93XX_PCMCIA0_HWBASE,
409 	EP93XX_PCMCIA_SIZE,
410 	VM_PROT_READ|VM_PROT_WRITE,
411 	PTE_NOCACHE,
412     },
413 
414 	/*
415 	 * IO8 and IO16 space *must* be mapped contiguously with
416 	 * IO8_VA == IO16_VA - 64 Mbytes.  ISA busmap driver depends
417 	 * on that!
418 	 */
419     {
420 	ARMADILLO9_IO8_VBASE,
421 	ARMADILLO9_IO8_HWBASE,
422 	ARMADILLO9_IO8_SIZE,
423 	VM_PROT_READ|VM_PROT_WRITE,
424 	PTE_NOCACHE,
425     },
426 
427     {
428 	ARMADILLO9_IO16_VBASE,
429 	ARMADILLO9_IO16_HWBASE,
430 	ARMADILLO9_IO16_SIZE,
431 	VM_PROT_READ|VM_PROT_WRITE,
432 	PTE_NOCACHE,
433     },
434 
435    {
436 	0,
437 	0,
438 	0,
439 	0,
440 	0,
441     }
442 };
443 
444 /*
445  * vaddr_t initarm(...)
446  *
447  * Initial entry point on startup. This gets called before main() is
448  * entered.
449  * It should be responsible for setting up everything that must be
450  * in place when main is called.
451  * This includes
452  *   Taking a copy of the boot configuration structure.
453  *   Initialising the physical console so characters can be printed.
454  *   Setting up page tables for the kernel
455  *   Initialising interrupt controllers to a sane default state
456  */
457 vaddr_t
initarm(void * arg)458 initarm(void *arg)
459 {
460 	int loop;
461 	int loop1;
462 	u_int l1pagetable;
463 	struct bootparam_tag *bootparam_p;
464 	unsigned long devcfg;
465 
466 	/*
467 	 * Since we map the on-board devices VA==PA, and the kernel
468 	 * is running VA==PA, it's possible for us to initialize
469 	 * the console now.
470 	 */
471 	consinit();
472 
473 	/* identify model */
474 	devcfg = *((volatile unsigned long*)(EP93XX_APB_HWBASE
475 					     + EP93XX_APB_SYSCON
476 					     + EP93XX_SYSCON_DeviceCfg));
477 	for (armadillo_model = &armadillo_model_table[0];
478 				armadillo_model->devcfg; armadillo_model++)
479 		if (devcfg == armadillo_model->devcfg)
480 			break;
481 
482 	/* Talk to the user */
483 	printf("\nNetBSD/%s booting ...\n", armadillo_model->name);
484 
485 	/* set some informations from bootloader */
486 	bootparam_p = (struct bootparam_tag *)bootparam;
487 	bootconfig.dramblocks = 0;
488 	while (bootparam_p->hdr.tag != BOOTPARAM_TAG_NONE) {
489 		switch (bootparam_p->hdr.tag) {
490 		case BOOTPARAM_TAG_MEM:
491 			if (bootconfig.dramblocks < DRAM_BLOCKS) {
492 #ifdef VERBOSE_INIT_ARM
493 			printf("dram[%d]: address=0x%08lx, size=0x%08lx\n",
494 						bootconfig.dramblocks,
495 						bootparam_p->u.mem.start,
496 						bootparam_p->u.mem.size);
497 #endif
498 				bootconfig.dram[bootconfig.dramblocks].address =
499 					bootparam_p->u.mem.start;
500 				bootconfig.dram[bootconfig.dramblocks].pages =
501 					bootparam_p->u.mem.size / PAGE_SIZE;
502 				bootconfig.dramblocks++;
503 			}
504 			break;
505 		case BOOTPARAM_TAG_CMDLINE:
506 #ifdef VERBOSE_INIT_ARM
507 			printf("cmdline: %s\n", bootparam_p->u.cmdline.cmdline);
508 #endif
509 			parse_mi_bootargs(bootparam_p->u.cmdline.cmdline);
510 			break;
511 		}
512 		bootparam_p = bootparam_tag_next(bootparam_p);
513 	}
514 
515 	/*
516 	 * Heads up ... Setup the CPU / MMU / TLB functions
517 	 */
518 	if (set_cpufuncs())
519 		panic("cpu not recognized!");
520 
521 #ifdef VERBOSE_INIT_ARM
522 	printf("initarm: Configuring system ...\n");
523 #endif
524 	/*
525 	 * Set up the variables that define the availability of
526 	 * physical memory.  For now, we're going to set
527 	 * physical_freestart to 0xc0200000 (where the kernel
528 	 * was loaded), and allocate the memory we need downwards.
529 	 * If we get too close to the L1 table that we set up, we
530 	 * will panic.  We will update physical_freestart and
531 	 * physical_freeend later to reflect what pmap_bootstrap()
532 	 * wants to see.
533 	 *
534 	 * XXX pmap_bootstrap() needs an enema.
535 	 */
536 	physical_start = bootconfig.dram[0].address;
537 	physical_end = bootconfig.dram[0].address
538 			+ (bootconfig.dram[0].pages * PAGE_SIZE);
539 
540 	physical_freestart = 0xc0018000UL;
541 	physical_freeend = 0xc0200000UL;
542 
543 	physmem = (physical_end - physical_start) / PAGE_SIZE;
544 
545 #ifdef VERBOSE_INIT_ARM
546 	/* Tell the user about the memory */
547 	printf("physmemory: 0x%"PRIxPSIZE" pages at "
548 	    "0x%08"PRIxPADDR" -> 0x%08"PRIxPADDR"\n",
549 	    physmem, physical_start, physical_end - 1);
550 #endif
551 
552 	/*
553 	 * Okay, the kernel starts 2MB in from the bottom of physical
554 	 * memory.  We are going to allocate our bootstrap pages downwards
555 	 * from there.
556 	 *
557 	 * We need to allocate some fixed page tables to get the kernel
558 	 * going.  We allocate one page directory and a number of page
559 	 * tables and store the physical addresses in the kernel_pt_table
560 	 * array.
561 	 *
562 	 * The kernel page directory must be on a 16K boundary.  The page
563 	 * tables must be on 4K boundaries.  What we do is allocate the
564 	 * page directory on the first 16K boundary that we encounter, and
565 	 * the page tables on 4K boundaries otherwise.  Since we allocate
566 	 * at least 3 L2 page tables, we are guaranteed to encounter at
567 	 * least one 16K aligned region.
568 	 */
569 
570 #ifdef VERBOSE_INIT_ARM
571 	printf("Allocating page tables\n");
572 #endif
573 
574 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
575 
576 #ifdef VERBOSE_INIT_ARM
577 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
578 	       physical_freestart, free_pages, free_pages);
579 #endif
580 
581 	/* Define a macro to simplify memory allocation */
582 #define	valloc_pages(var, np)				\
583 	alloc_pages((var).pv_pa, (np));			\
584 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
585 
586 #define alloc_pages(var, np)				\
587 	physical_freeend -= ((np) * PAGE_SIZE);		\
588 	if (physical_freeend < physical_freestart)	\
589 		panic("initarm: out of memory");	\
590 	(var) = physical_freeend;			\
591 	free_pages -= (np);				\
592 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
593 
594 	loop1 = 0;
595 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
596 		/* Are we 16KB aligned for an L1 ? */
597 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
598 		    && kernel_l1pt.pv_pa == 0) {
599 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
600 		} else {
601 			valloc_pages(kernel_pt_table[loop1],
602 			    L2_TABLE_SIZE / PAGE_SIZE);
603 			++loop1;
604 		}
605 	}
606 
607 	/* This should never be able to happen but better confirm that. */
608 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
609 		panic("initarm: Failed to align the kernel page directory");
610 
611 	/*
612 	 * Allocate a page for the system vectors page
613 	 */
614 	alloc_pages(systempage.pv_pa, 1);
615 
616 	/* Allocate stacks for all modes */
617 	valloc_pages(irqstack, IRQ_STACK_SIZE);
618 	valloc_pages(abtstack, ABT_STACK_SIZE);
619 	valloc_pages(undstack, UND_STACK_SIZE);
620 	valloc_pages(kernelstack, UPAGES);
621 
622 #ifdef VERBOSE_INIT_ARM
623 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
624 	    irqstack.pv_va);
625 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
626 	    abtstack.pv_va);
627 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
628 	    undstack.pv_va);
629 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
630 	    kernelstack.pv_va);
631 #endif
632 
633 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
634 
635 	/*
636 	 * Ok we have allocated physical pages for the primary kernel
637 	 * page tables.  Save physical_freeend for when we give whats left
638 	 * of memory below 2Mbyte to UVM.
639 	 */
640 
641 	physical_freeend_low = physical_freeend;
642 
643 #ifdef VERBOSE_INIT_ARM
644 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
645 #endif
646 
647 	/*
648 	 * Now we start construction of the L1 page table
649 	 * We start by mapping the L2 page tables into the L1.
650 	 * This means that we can replace L1 mappings later on if necessary
651 	 */
652 	l1pagetable = kernel_l1pt.pv_pa;
653 
654 	/* Map the L2 pages tables in the L1 page table */
655 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
656 	    &kernel_pt_table[KERNEL_PT_SYS]);
657 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
658 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
659 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
660 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
661 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
662 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
663 
664 	/* update the top of the kernel VM */
665 	pmap_curmaxkvaddr =
666 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
667 
668 #ifdef VERBOSE_INIT_ARM
669 	printf("Mapping kernel\n");
670 #endif
671 
672 	/* Now we fill in the L2 pagetable for the kernel static code/data */
673 	{
674 		extern char etext[], _end[];
675 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
676 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
677 		u_int logical;
678 
679 		textsize = (textsize + PGOFSET) & ~PGOFSET;
680 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
681 
682 		logical = 0x00200000;	/* offset of kernel in RAM */
683 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
684 		    physical_start + logical, textsize,
685 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
686 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
687 		    physical_start + logical, totalsize - textsize,
688 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
689 	}
690 
691 #ifdef VERBOSE_INIT_ARM
692 	printf("Constructing L2 page tables\n");
693 #endif
694 
695 	/* Map the stack pages */
696 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
697 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
698 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
699 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
700 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
701 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
702 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
703 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
704 
705 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
706 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
707 
708 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
709 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
710 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
711 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
712 	}
713 
714 	/* Map the vector page. */
715 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
716 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
717 
718 	/* Map the statically mapped devices. */
719 	pmap_devmap_bootstrap(l1pagetable, armadillo9_devmap);
720 
721 	/*
722 	 * Update the physical_freestart/physical_freeend/free_pages
723 	 * variables.
724 	 */
725 	{
726 		extern char _end[];
727 
728 		physical_freestart = physical_start +
729 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
730 		     KERNEL_BASE);
731 		physical_freeend = physical_end;
732 		free_pages =
733 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
734 	}
735 
736 	/*
737 	 * Now we have the real page tables in place so we can switch to them.
738 	 * Once this is done we will be running with the REAL kernel page
739 	 * tables.
740 	 */
741 
742 	/* Switch tables */
743 #ifdef VERBOSE_INIT_ARM
744 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
745 	       physical_freestart, free_pages, free_pages);
746 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
747 #endif
748 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
749 	cpu_setttb(kernel_l1pt.pv_pa, true);
750 	cpu_tlb_flushID();
751 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
752 
753 	/*
754 	 * Moved from cpu_startup() as data_abort_handler() references
755 	 * this during uvm init
756 	 */
757 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
758 
759 #ifdef VERBOSE_INIT_ARM
760 	printf("done!\n");
761 #endif
762 
763 #ifdef VERBOSE_INIT_ARM
764 	printf("bootstrap done.\n");
765 #endif
766 
767 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
768 
769 	/*
770 	 * Pages were allocated during the secondary bootstrap for the
771 	 * stacks for different CPU modes.
772 	 * We must now set the r13 registers in the different CPU modes to
773 	 * point to these stacks.
774 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
775 	 * of the stack memory.
776 	 */
777 #ifdef VERBOSE_INIT_ARM
778 	printf("init subsystems: stacks ");
779 #endif
780 
781 	set_stackptr(PSR_IRQ32_MODE,
782 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
783 	set_stackptr(PSR_ABT32_MODE,
784 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
785 	set_stackptr(PSR_UND32_MODE,
786 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
787 
788 	/*
789 	 * Well we should set a data abort handler.
790 	 * Once things get going this will change as we will need a proper
791 	 * handler.
792 	 * Until then we will use a handler that just panics but tells us
793 	 * why.
794 	 * Initialisation of the vectors will just panic on a data abort.
795 	 * This just fills in a slightly better one.
796 	 */
797 #ifdef VERBOSE_INIT_ARM
798 	printf("vectors ");
799 #endif
800 	data_abort_handler_address = (u_int)data_abort_handler;
801 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
802 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
803 
804 	/* Initialise the undefined instruction handlers */
805 #ifdef VERBOSE_INIT_ARM
806 	printf("undefined ");
807 #endif
808 	undefined_init();
809 
810 	/* Load memory into UVM. */
811 #ifdef VERBOSE_INIT_ARM
812 	printf("page ");
813 #endif
814 	uvm_md_init();
815 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
816 	    atop(physical_freestart), atop(physical_freeend),
817 	    VM_FREELIST_DEFAULT);
818 	uvm_page_physload(atop(0xc0000000), atop(physical_freeend_low),
819 	    atop(0xc0000000), atop(physical_freeend_low),
820 	    VM_FREELIST_DEFAULT);
821 	physmem = bootconfig.dram[0].pages;
822 	for (loop = 1; loop < bootconfig.dramblocks; ++loop) {
823 		size_t start = bootconfig.dram[loop].address;
824 		size_t size = bootconfig.dram[loop].pages * PAGE_SIZE;
825 		uvm_page_physload(atop(start), atop(start + size),
826 				  atop(start), atop(start + size),
827 				  VM_FREELIST_DEFAULT);
828 		physmem += bootconfig.dram[loop].pages;
829 	}
830 
831 	/* Boot strap pmap telling it where managed kernel virtual memory is */
832 #ifdef VERBOSE_INIT_ARM
833 	printf("pmap ");
834 #endif
835 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
836 
837 	/* Setup the IRQ system */
838 #ifdef VERBOSE_INIT_ARM
839 	printf("irq ");
840 #endif
841 	ep93xx_intr_init();
842 #if NISA > 0
843 	isa_intr_init();
844 
845 #ifdef VERBOSE_INIT_ARM
846 	printf("isa ");
847 #endif
848 	isa_armadillo9_init(ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAIO,
849 		ARMADILLO9_IO16_VBASE + ARMADILLO9_ISAMEM);
850 #endif
851 
852 #ifdef VERBOSE_INIT_ARM
853 	printf("done.\n");
854 #endif
855 
856 #ifdef BOOTHOWTO
857 	boothowto = BOOTHOWTO;
858 #endif
859 
860 #ifdef DDB
861 	db_machine_init();
862 	if (boothowto & RB_KDB)
863 		Debugger();
864 #endif
865 
866 	/* We have our own device_register() */
867 	evbarm_device_register = armadillo9_device_register;
868 
869 	/* We return the new stack pointer address */
870 	return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
871 }
872 
873 void
consinit(void)874 consinit(void)
875 {
876 	static int consinit_called;
877 #if NEPCOM > 0
878 	bus_space_handle_t ioh;
879 #endif
880 
881 	if (consinit_called != 0)
882 		return;
883 
884 	consinit_called = 1;
885 
886 	/*
887 	 * Console devices are already mapped in VA.  Our devmap reflects
888 	 * this, so register it now so drivers can map the console
889 	 * device.
890 	 */
891 	pmap_devmap_register(armadillo9_devmap);
892 
893 #if NEPCOM > 0
894 	bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + comaddr[CONUNIT],
895 		EP93XX_APB_UART_SIZE, 0, &ioh);
896         if (epcomcnattach(&ep93xx_bs_tag, EP93XX_APB_HWBASE + comaddr[CONUNIT],
897 		ioh, comcnspeed, comcnmode))
898 	{
899 		panic("can't init serial console");
900 	}
901 #else
902 	panic("serial console not configured");
903 #endif
904 #if KGDB
905 #if NEPCOM > 0
906 	if (strcmp(kgdb_devname, "epcom") == 0) {
907 		epcom_kgdb_attach(&ep93xx_bs_tag, kgdb_devaddr, kgdb_devrate,
908 			kgdb_devmode);
909 	}
910 #endif	/* NEPCOM > 0 */
911 #endif	/* KGDB */
912 }
913 
914 
915 bus_dma_tag_t
ep93xx_bus_dma_init(struct arm32_bus_dma_tag * dma_tag_template)916 ep93xx_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
917 {
918 	int i;
919 	struct arm32_bus_dma_tag *dmat;
920 
921 	for (i = 0; i < bootconfig.dramblocks; i++) {
922 		armadillo9_dma_ranges[i].dr_sysbase = bootconfig.dram[i].address;
923 		armadillo9_dma_ranges[i].dr_busbase = bootconfig.dram[i].address;
924 		armadillo9_dma_ranges[i].dr_len = bootconfig.dram[i].pages *
925 			PAGE_SIZE;
926 	}
927 
928 	dmat = dma_tag_template;
929 
930 	dmat->_ranges = armadillo9_dma_ranges;
931 	dmat->_nranges = bootconfig.dramblocks;
932 
933 	return dmat;
934 }
935