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