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