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