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