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