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