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