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