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