Home | History | Annotate | Line # | Download | only in at91
at91bus.c revision 1.1.2.1
      1 /*	$NetBSD: at91bus.c,v 1.1.2.1 2007/11/10 02:56:28 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 Embedtronics Oy
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the NetBSD
     18  *	Foundation, Inc. and its contributors.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: at91bus.c,v 1.1.2.1 2007/11/10 02:56:28 matt Exp $");
     38 
     39 #include "opt_ddb.h"
     40 #include "opt_kgdb.h"
     41 #include "opt_pmap_debug.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/device.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/exec.h>
     48 #include <sys/proc.h>
     49 #include <sys/msgbuf.h>
     50 #include <sys/reboot.h>
     51 #include <sys/termios.h>
     52 #include <sys/ksyms.h>
     53 
     54 #include <machine/bootconfig.h>
     55 #include <uvm/uvm_extern.h>
     56 
     57 #include <dev/cons.h>
     58 
     59 #include <machine/db_machdep.h>
     60 #include <ddb/db_sym.h>
     61 #include <ddb/db_extern.h>
     62 
     63 #include <machine/bus.h>
     64 #include <machine/cpu.h>
     65 #include <machine/frame.h>
     66 #include <arm/undefined.h>
     67 
     68 #include <arm/arm32/machdep.h>
     69 #include <arm/cpufunc.h>
     70 
     71 #include <arm/at91/at91var.h>
     72 #include <arm/at91/at91busvar.h>
     73 #include <arm/at91/at91dbgureg.h>
     74 
     75 //#include <dev/cons.h>
     76 #include <sys/termios.h>
     77 
     78 #include "locators.h"
     79 
     80 /* console stuff: */
     81 #ifndef	CONSPEED
     82 #define	CONSPEED B115200
     83 #endif
     84 
     85 #ifndef	CONMODE
     86 #define	CONMODE	((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
     87 #endif
     88 
     89 int cnspeed = CONSPEED;
     90 int cnmode = CONMODE;
     91 
     92 
     93 /* kernel mapping: */
     94 #define	KERNEL_BASE_PHYS	0x20200000
     95 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
     96 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
     97 #define	KERNEL_VM_SIZE		0x0C000000
     98 
     99 
    100 
    101 /* Define various stack sizes in pages */
    102 #define IRQ_STACK_SIZE	8
    103 #define ABT_STACK_SIZE	8
    104 #ifdef IPKDB
    105 #define UND_STACK_SIZE	16
    106 #else
    107 #define UND_STACK_SIZE	8
    108 #endif
    109 
    110 /* boot configuration: */
    111 vm_offset_t physical_start;
    112 vm_offset_t physical_freestart;
    113 vm_offset_t physical_freeend;
    114 vm_offset_t physical_freeend_low;
    115 vm_offset_t physical_end;
    116 u_int free_pages;
    117 int physmem = 0;
    118 
    119 /* Physical and virtual addresses for some global pages */
    120 pv_addr_t systempage;
    121 pv_addr_t irqstack;
    122 pv_addr_t undstack;
    123 pv_addr_t abtstack;
    124 pv_addr_t kernelstack;
    125 
    126 vm_offset_t msgbufphys;
    127 
    128 //static struct arm32_dma_range dma_ranges[4];
    129 
    130 extern u_int data_abort_handler_address;
    131 extern u_int prefetch_abort_handler_address;
    132 extern u_int undefined_handler_address;
    133 
    134 #ifdef PMAP_DEBUG
    135 extern int pmap_debug_level;
    136 #endif
    137 
    138 #define KERNEL_PT_SYS		0	/* L2 table for mapping vectors page */
    139 
    140 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    141 #define	KERNEL_PT_KERNEL_NUM	4
    142 					/* L2 tables for mapping kernel VM */
    143 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    144 
    145 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    146 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    147 
    148 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    149 
    150 struct user *proc0paddr;
    151 
    152 
    153 /* prototypes: */
    154 void		consinit(void);
    155 static int	at91bus_match(struct device *, struct cfdata *, void *);
    156 static void	at91bus_attach(struct device *, struct device *, void *);
    157 static int	at91bus_search(struct device *, struct cfdata *,
    158 			       const int *, void *);
    159 static int	at91bus_print(void *, const char *);
    160 static int	at91bus_submatch(struct device *, struct cfdata *,
    161 				 const int *, void *);
    162 
    163 
    164 CFATTACH_DECL(at91bus, sizeof(struct at91bus_softc), at91bus_match, at91bus_attach, NULL, NULL);
    165 
    166 struct at91bus_clocks at91bus_clocks = {0};
    167 struct at91bus_softc *at91bus_sc = NULL;
    168 
    169 #include "opt_at91types.h"
    170 
    171 #ifdef	AT91RM9200
    172 #include <arm/at91/at91rm9200busvar.h>
    173 #endif
    174 
    175 #ifdef	AT91SAM9261
    176 #include <arm/at91/at91sam9261busvar.h>
    177 #endif
    178 
    179 static const struct {
    180 	u_int32_t	cidr;
    181 	const char *	name;
    182 	const struct at91bus_machdep *machdep;
    183 } at91_types[] = {
    184 	{
    185 		DBGU_CIDR_AT91RM9200,
    186 		"AT91RM9200"
    187 #ifdef	AT91RM9200
    188 		, &at91rm9200bus
    189 #endif
    190 	},
    191 	{
    192 		DBGU_CIDR_AT91SAM9260,
    193 		"AT91SAM9260"
    194 	},
    195 	{
    196 		DBGU_CIDR_AT91SAM9260,
    197 		"AT91SAM9261"
    198 #ifdef	AT91SAM9261
    199 		, &at91sam9261bus
    200 #endif
    201 	},
    202 	{
    203 		DBGU_CIDR_AT91SAM9260,
    204 		"AT91SAM9263"
    205 	},
    206 	{
    207 		0,
    208 		0,
    209 		0
    210 	}
    211 };
    212 
    213 u_int32_t at91_chip_id;
    214 static int at91_chip_ndx = -1;
    215 struct at91bus_machdep at91bus_machdep = { 0 };
    216 at91bus_tag_t at91bus_tag = 0;
    217 
    218 static int
    219 match_cid(void)
    220 {
    221 	u_int32_t		cidr;
    222 	int			i;
    223 
    224 	/* get chip id */
    225 	cidr = DBGUREG(DBGU_CIDR);
    226 	at91_chip_id = cidr;
    227 
    228 	/* do we know it? */
    229 	for (i = 0; at91_types[i].name; i++) {
    230 		if (cidr == at91_types[i].cidr)
    231 			return i;
    232 	}
    233 
    234 	return -1;
    235 }
    236 
    237 int
    238 at91bus_init(void)
    239 {
    240 	int i = at91_chip_ndx = match_cid();
    241 
    242 	if (i < 0)
    243 		panic("%s: unknown chip", __FUNCTION__);
    244 
    245 	if (!at91_types[i].machdep)
    246 		panic("%s: %s is not supported", __FUNCTION__, at91_types[i].name);
    247 
    248 	memcpy(&at91bus_machdep, at91_types[i].machdep, sizeof(at91bus_machdep));
    249 	at91bus_tag = &at91bus_machdep;
    250 
    251 	return 0;
    252 }
    253 
    254 u_int
    255 at91bus_setup(BootConfig *mem)
    256 {
    257 	int loop;
    258 	int loop1;
    259 	u_int l1pagetable;
    260 	pv_addr_t kernel_l1pt;
    261 
    262 	consinit();
    263 
    264 #ifdef	VERBOSE_INIT_ARM
    265 	printf("\nNetBSD/AT91 booting ...\n");
    266 #endif
    267 
    268 	// setup the CPU / MMU / TLB functions:
    269 	if (set_cpufuncs())
    270 		panic("%s: cpu not recognized", __FUNCTION__);
    271 
    272 #ifdef	VERBOSE_INIT_ARM
    273 	printf("%s: configuring system...\n", __FUNCTION__);
    274 #endif
    275 
    276 	/*
    277 	 * Setup the variables that define the availability of
    278 	 * physical memory.
    279 	 */
    280 	physical_start = mem->dram[0].address;
    281 	physical_end = mem->dram[0].address + mem->dram[0].pages * PAGE_SIZE;
    282 
    283 	physical_freestart = mem->dram[0].address + 0x9000ULL;
    284 	physical_freeend = KERNEL_BASE_PHYS;
    285 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    286 
    287 #ifdef	VERBOSE_INIT_ARM
    288 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    289 	       physical_start, physical_end - 1);
    290 #endif
    291 
    292 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    293 
    294 #ifdef	VERBOSE_INIT_ARM
    295 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    296 	       physical_freestart, free_pages, free_pages);
    297 #endif
    298 	/* Define a macro to simplify memory allocation */
    299 #define	valloc_pages(var, np)				\
    300 	alloc_pages((var).pv_pa, (np));			\
    301 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    302 
    303 #define alloc_pages(var, np)				\
    304 	physical_freeend -= ((np) * PAGE_SIZE);		\
    305 	if (physical_freeend < physical_freestart)	\
    306 		panic("initarm: out of memory");	\
    307 	(var) = physical_freeend;			\
    308 	free_pages -= (np);				\
    309 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    310 
    311 	loop1 = 0;
    312 	kernel_l1pt.pv_pa = 0;
    313 	kernel_l1pt.pv_va = 0;
    314 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    315 		/* Are we 16KB aligned for an L1 ? */
    316 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    317 		    && kernel_l1pt.pv_pa == 0) {
    318 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    319 		} else {
    320 			valloc_pages(kernel_pt_table[loop1],
    321 			    L2_TABLE_SIZE / PAGE_SIZE);
    322 			++loop1;
    323 		}
    324 	}
    325 
    326 	/* This should never be able to happen but better confirm that. */
    327 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    328 		panic("initarm: Failed to align the kernel page directory");
    329 
    330 	/*
    331 	 * Allocate a page for the system vectors page
    332 	 */
    333 	valloc_pages(systempage, 1);
    334 	systempage.pv_va = 0x00000000;
    335 
    336 	/* Allocate stacks for all modes */
    337 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    338 	valloc_pages(abtstack, ABT_STACK_SIZE);
    339 	valloc_pages(undstack, UND_STACK_SIZE);
    340 	valloc_pages(kernelstack, UPAGES);
    341 
    342 #ifdef VERBOSE_INIT_ARM
    343 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    344 	    irqstack.pv_va);
    345 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    346 	    abtstack.pv_va);
    347 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    348 	    undstack.pv_va);
    349 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    350 	    kernelstack.pv_va);
    351 #endif
    352 
    353 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    354 
    355 	/*
    356 	 * Ok we have allocated physical pages for the primary kernel
    357 	 * page tables.  Save physical_freeend for when we give whats left
    358 	 * of memory below 2Mbyte to UVM.
    359 	 */
    360 
    361 	physical_freeend_low = physical_freeend;
    362 
    363 #ifdef VERBOSE_INIT_ARM
    364 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    365 #endif
    366 
    367 	/*
    368 	 * Now we start construction of the L1 page table
    369 	 * We start by mapping the L2 page tables into the L1.
    370 	 * This means that we can replace L1 mappings later on if necessary
    371 	 */
    372 	l1pagetable = kernel_l1pt.pv_pa;
    373 
    374 	/* Map the L2 pages tables in the L1 page table */
    375 	pmap_link_l2pt(l1pagetable, 0x00000000, &kernel_pt_table[KERNEL_PT_SYS]);
    376 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    377 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    378 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    379 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    380 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    381 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    382 
    383 	/* update the top of the kernel VM */
    384 	pmap_curmaxkvaddr =
    385 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    386 
    387 #ifdef VERBOSE_INIT_ARM
    388 	printf("Mapping kernel\n");
    389 #endif
    390 
    391 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    392 	{
    393 		extern char etext[], _end[];
    394 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    395 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    396 		u_int logical;
    397 
    398 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    399 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    400 
    401 		logical = KERNEL_BASE_PHYS - mem->dram[0].address;	/* offset of kernel in RAM */
    402 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    403 		    physical_start + logical, textsize,
    404 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    405 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    406 		    physical_start + logical, totalsize - textsize,
    407 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    408 	}
    409 
    410 #ifdef VERBOSE_INIT_ARM
    411 	printf("Constructing L2 page tables\n");
    412 #endif
    413 
    414 	/* Map the stack pages */
    415 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    416 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    417 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    418 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    419 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    420 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    421 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    422 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    423 
    424 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    425 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    426 
    427 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    428 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    429 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    430 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    431 	}
    432 
    433 	/* Map the vector page. */
    434 	pmap_map_entry(l1pagetable, ARM_VECTORS_LOW, systempage.pv_pa,
    435 		       VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    436 
    437 	/* Map the statically mapped devices. */
    438 	pmap_devmap_bootstrap(l1pagetable, at91_devmap());
    439 
    440 	/*
    441 	 * Update the physical_freestart/physical_freeend/free_pages
    442 	 * variables.
    443 	 */
    444 	{
    445 		extern char _end[];
    446 
    447 		physical_freestart = physical_start +
    448 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    449 		     KERNEL_BASE);
    450 		physical_freeend = physical_end;
    451 		free_pages =
    452 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    453 	}
    454 
    455 	/*
    456 	 * Now we have the real page tables in place so we can switch to them.
    457 	 * Once this is done we will be running with the REAL kernel page
    458 	 * tables.
    459 	 */
    460 
    461 	/* Switch tables */
    462 #ifdef VERBOSE_INIT_ARM
    463 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    464 	       physical_freestart, free_pages, free_pages);
    465 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    466 #endif
    467 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    468 	setttb(kernel_l1pt.pv_pa);
    469 	cpu_tlb_flushID();
    470 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    471 
    472 	/*
    473 	 * Moved from cpu_startup() as data_abort_handler() references
    474 	 * this during uvm init
    475 	 */
    476 	proc0paddr = (struct user *)kernelstack.pv_va;
    477 	lwp0.l_addr = proc0paddr;
    478 
    479 #ifdef VERBOSE_INIT_ARM
    480 	printf("done!\n");
    481 #endif
    482 
    483 #ifdef VERBOSE_INIT_ARM
    484 	printf("bootstrap done.\n");
    485 #endif
    486 
    487 	/* @@@@ check this out: @@@ */
    488 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    489 
    490 	/*
    491 	 * Pages were allocated during the secondary bootstrap for the
    492 	 * stacks for different CPU modes.
    493 	 * We must now set the r13 registers in the different CPU modes to
    494 	 * point to these stacks.
    495 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    496 	 * of the stack memory.
    497 	 */
    498 #ifdef VERBOSE_INIT_ARM
    499 	printf("init subsystems: stacks ");
    500 #endif
    501 
    502 	set_stackptr(PSR_IRQ32_MODE,
    503 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    504 	set_stackptr(PSR_ABT32_MODE,
    505 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    506 	set_stackptr(PSR_UND32_MODE,
    507 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    508 
    509 	/*
    510 	 * Well we should set a data abort handler.
    511 	 * Once things get going this will change as we will need a proper
    512 	 * handler.
    513 	 * Until then we will use a handler that just panics but tells us
    514 	 * why.
    515 	 * Initialisation of the vectors will just panic on a data abort.
    516 	 * This just fills in a slightly better one.
    517 	 */
    518 #ifdef VERBOSE_INIT_ARM
    519 	printf("vectors ");
    520 #endif
    521 	data_abort_handler_address = (u_int)data_abort_handler;
    522 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    523 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    524 
    525 	/* Initialise the undefined instruction handlers */
    526 #ifdef VERBOSE_INIT_ARM
    527 	printf("undefined ");
    528 #endif
    529 	undefined_init();
    530 
    531 	/* Load memory into UVM. */
    532 #ifdef VERBOSE_INIT_ARM
    533 	printf("page ");
    534 #endif
    535 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    536 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    537 	    atop(physical_freestart), atop(physical_freeend),
    538 	    VM_FREELIST_DEFAULT);
    539 	uvm_page_physload(atop(physical_start), atop(physical_freeend_low),
    540 	    atop(physical_start), atop(physical_freeend_low),
    541 	    VM_FREELIST_DEFAULT);
    542 
    543 	/* Boot strap pmap telling it where the kernel page table is */
    544 #ifdef VERBOSE_INIT_ARM
    545 	printf("pmap ");
    546 #endif
    547 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
    548 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
    549 
    550 	/* Setup the IRQ system */
    551 #ifdef VERBOSE_INIT_ARM
    552 	printf("irq ");
    553 #endif
    554 	at91_intr_init();
    555 
    556 #ifdef VERBOSE_INIT_ARM
    557 	printf("done.\n");
    558 #endif
    559 
    560 #ifdef BOOTHOWTO
    561 	boothowto = BOOTHOWTO;
    562 #endif
    563 	boothowto = AB_VERBOSE | AB_DEBUG; // @@@@
    564 
    565 #ifdef IPKDB
    566 	/* Initialise ipkdb */
    567 	ipkdb_init();
    568 	if (boothowto & RB_KDB)
    569 		ipkdb_connect(0);
    570 #endif
    571 
    572 #if NKSYMS || defined(DDB) || defined(LKM)
    573 	/* Firmware doesn't load symbols. */
    574 	ksyms_init(0, NULL, NULL);
    575 #endif
    576 
    577 #ifdef DDB
    578 	db_machine_init();
    579 	if (boothowto & RB_KDB)
    580 		Debugger();
    581 #endif
    582 #if 0
    583 	printf("test data abort...\n");
    584 	*((volatile uint32_t*)(0x1234567F)) = 0xdeadbeef;
    585 #endif
    586 
    587 #ifdef VERBOSE_INIT_ARM
    588   	printf("%s: returning new stack pointer 0x%lX\n", __FUNCTION__, (kernelstack.pv_va + USPACE_SVC_STACK_TOP));
    589 #endif
    590 
    591 	/* We return the new stack pointer address */
    592 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    593 }
    594 
    595 static int
    596 at91bus_match(struct device *parent, struct cfdata *match, void *aux)
    597 {
    598 	// we could detect the device here...
    599 	if (strcmp(match->cf_name, "at91bus") == 0)
    600 		return 1;
    601 	return 0;
    602 }
    603 
    604 static device_t
    605 at91bus_found(struct device *self, bus_addr_t addr, int pid)
    606 {
    607 	int locs[AT91BUSCF_NLOCS];
    608 	struct at91bus_attach_args sa;
    609 	struct at91bus_softc *sc;
    610 
    611 	memset(&locs, 0, sizeof(locs));
    612 	memset(&sa, 0, sizeof(sa));
    613 
    614 	locs[AT91BUSCF_ADDR] = addr;
    615 	locs[AT91BUSCF_PID]  = pid;
    616 
    617 	sc = (struct at91bus_softc*) self;
    618 	sa.sa_iot = sc->sc_iot;
    619 	sa.sa_dmat = sc->sc_dmat;
    620 	sa.sa_addr = addr;
    621 	sa.sa_size = 1;
    622 	sa.sa_pid = pid;
    623 
    624 	return config_found_sm_loc(self, "at91bus", locs, &sa,
    625 				   at91bus_print, at91bus_submatch);
    626 }
    627 
    628 static void
    629 at91bus_attach(struct device *parent, struct device *self, void *aux)
    630 {
    631 	struct at91bus_softc	*sc;
    632 
    633 	if (at91_chip_ndx < 0)
    634 		panic("%s: at91bus_init() has not been called!", __FUNCTION__);
    635 
    636 	sc = (struct at91bus_softc*) self;
    637 
    638         /* initialize bus space and bus dma things... */
    639 	sc->sc_iot = &at91_bs_tag;
    640         sc->sc_dmat = at91_bus_dma_init(&at91_bd_tag);
    641 
    642 	if (at91bus_sc == NULL)
    643 		at91bus_sc = sc;
    644 
    645 	printf(": %s, sclk %u.%03u kHz, mclk %u.%03u MHz, pclk %u.%03u MHz, mstclk %u.%03u, plla %u.%03u, pllb %u.%03u MHz\n",
    646 	       at91_types[at91_chip_ndx].name,
    647 	       AT91_SCLK / 1000U, AT91_SCLK % 1000U,
    648 	       AT91_MCLK / 1000000U, (AT91_MCLK / 1000U) % 1000U,
    649 	       AT91_PCLK / 1000000U, (AT91_PCLK / 1000U) % 1000U,
    650 	       AT91_MSTCLK / 1000000U, (AT91_MSTCLK / 1000U) % 1000U,
    651 	       AT91_PLLACLK / 1000000U, (AT91_PLLACLK / 1000U) % 1000U,
    652 	       AT91_PLLBCLK / 1000000U, (AT91_PLLBCLK / 1000U) % 1000U);
    653 
    654 	/*
    655 	 *  Attach devices
    656 	 */
    657 	at91_search_peripherals(self, at91bus_found);
    658 
    659 
    660 	struct at91bus_attach_args sa;
    661 	memset(&sa, 0, sizeof(sa));
    662 	sa.sa_iot = sc->sc_iot;
    663 	sa.sa_dmat = sc->sc_dmat;
    664 	config_search_ia(at91bus_search, self, "at91bus", &sa);
    665 }
    666 
    667 int
    668 at91bus_submatch(parent, cf, ldesc, aux)
    669 	struct device *parent;
    670 	struct cfdata *cf;
    671 	const int *ldesc;
    672 	void *aux;
    673 {
    674 	struct at91bus_attach_args *sa = aux;
    675 
    676 	if (cf->cf_loc[AT91BUSCF_ADDR] == ldesc[AT91BUSCF_ADDR]
    677 	    && cf->cf_loc[AT91BUSCF_PID] == ldesc[AT91BUSCF_PID]) {
    678 		sa->sa_addr = cf->cf_loc[AT91BUSCF_ADDR];
    679 		sa->sa_size = cf->cf_loc[AT91BUSCF_SIZE];
    680 		sa->sa_pid  = cf->cf_loc[AT91BUSCF_PID];
    681 		return (config_match(parent, cf, aux));
    682 	} else
    683 		return (0);
    684 }
    685 
    686 int
    687 at91bus_search(parent, cf, ldesc, aux)
    688 	struct device *parent;
    689 	struct cfdata *cf;
    690 	const int *ldesc;
    691 	void *aux;
    692 {
    693 	struct at91bus_attach_args *sa = aux;
    694 
    695 	sa->sa_addr = cf->cf_loc[AT91BUSCF_ADDR];
    696 	sa->sa_size = cf->cf_loc[AT91BUSCF_SIZE];
    697 	sa->sa_pid  = cf->cf_loc[AT91BUSCF_PID];
    698 
    699 	if (config_match(parent, cf, aux) > 0)
    700 		config_attach(parent, cf, aux, at91bus_print);
    701 
    702 	return (0);
    703 }
    704 
    705 static int
    706 at91bus_print(aux, name)
    707 	void *aux;
    708 	const char *name;
    709 {
    710         struct at91bus_attach_args *sa = (struct at91bus_attach_args*)aux;
    711 
    712 	if (name)
    713 		aprint_normal("%s at %s", sa->sa_pid >= 0 ? at91_peripheral_name(sa->sa_pid) : "device", name);
    714 
    715 	if (sa->sa_size)
    716 		aprint_normal(" at addr 0x%lx", sa->sa_addr);
    717 	if (sa->sa_size > 1)
    718 		aprint_normal("-0x%lx", sa->sa_addr + sa->sa_size - 1);
    719 	if (sa->sa_pid >= 0)
    720 		aprint_normal(" pid %d", sa->sa_pid);
    721 
    722 	return (UNCONF);
    723 }
    724 
    725 void	consinit(void)
    726 {
    727 	static int consinit_called;
    728 
    729 	if (consinit_called != 0)
    730 		return;
    731 
    732 	consinit_called = 1;
    733 
    734 	if (at91_chip_ndx < 0)
    735 		panic("%s: at91_init() has not been called!", __FUNCTION__);
    736 
    737 	// call machine specific bus initialization code
    738 	(*at91bus_tag->init)(&at91bus_clocks);
    739 
    740 	// attach console
    741 	(*at91bus_tag->attach_cn)(&at91_bs_tag, cnspeed, cnmode);
    742 }
    743