Home | History | Annotate | Line # | Download | only in bcm53xx
bcm53xx_machdep.c revision 1.7.4.2
      1 /*	$NetBSD: bcm53xx_machdep.c,v 1.7.4.2 2014/02/15 16:18:37 matt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #define CCA_PRIVATE
     33 #define IDM_PRIVATE
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: bcm53xx_machdep.c,v 1.7.4.2 2014/02/15 16:18:37 matt Exp $");
     37 
     38 #include "opt_evbarm_boardtype.h"
     39 #include "opt_broadcom.h"
     40 #include "opt_kgdb.h"
     41 #include "com.h"
     42 #include "pci.h"
     43 #include "bcmrng_ccb.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/bus.h>
     47 #include <sys/atomic.h>
     48 #include <sys/device.h>
     49 #include <sys/kernel.h>
     50 #include <sys/reboot.h>
     51 #include <sys/termios.h>
     52 
     53 #include <dev/cons.h>
     54 
     55 #include <uvm/uvm_extern.h>
     56 
     57 #include <arm/db_machdep.h>
     58 #include <arm/undefined.h>
     59 #include <arm/arm32/machdep.h>
     60 
     61 #include <machine/autoconf.h>
     62 #include <machine/bootconfig.h>
     63 
     64 #define CCA_PRIVATE
     65 
     66 #include <arm/cortex/scu_reg.h>
     67 #include <arm/broadcom/bcm53xx_var.h>
     68 
     69 #include <evbarm/bcm53xx/platform.h>
     70 
     71 #if NCOM == 0
     72 #error missing COM device for console
     73 #endif
     74 
     75 #include <dev/ic/comreg.h>
     76 #include <dev/ic/comvar.h>
     77 
     78 extern int _end[];
     79 extern int KERNEL_BASE_phys[];
     80 extern int KERNEL_BASE_virt[];
     81 
     82 BootConfig bootconfig;
     83 static char bootargs[MAX_BOOT_STRING];
     84 char *boot_args = NULL;
     85 
     86 int physmem;
     87 
     88 u_int uboot_args[4] = { 0 };
     89 
     90 static void bcm53xx_system_reset(void);
     91 static size_t bcm53xx_page_to_pggroup(struct vm_page *, size_t);
     92 
     93 /*
     94  * Macros to translate between physical and virtual for a subset of the
     95  * kernel address space.  *Not* for general use.
     96  */
     97 #define	KERN_VTOPDIFF	((vaddr_t)KERNEL_BASE_phys - (vaddr_t)KERNEL_BASE_virt)
     98 #define KERN_VTOPHYS(va) ((paddr_t)((vaddr_t)va + KERN_VTOPDIFF))
     99 #define KERN_PHYSTOV(pa) ((vaddr_t)((paddr_t)pa - KERN_VTOPDIFF))
    100 
    101 #ifndef CONADDR
    102 #define CONADDR		(BCM53XX_IOREG_PBASE + CCA_UART0_BASE)
    103 #endif
    104 #ifndef CONSPEED
    105 #define CONSPEED B115200
    106 #endif
    107 #ifndef CONMODE
    108 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    109 #endif
    110 
    111 #if (NCOM > 0)
    112 static const bus_addr_t comcnaddr = (bus_addr_t)CONADDR;
    113 
    114 int comcnspeed = CONSPEED;
    115 int comcnmode = CONMODE | CLOCAL;
    116 #endif
    117 
    118 #ifdef KGDB
    119 #include <sys/kgdb.h>
    120 #endif
    121 
    122 /*
    123  * Static device mappings. These peripheral registers are mapped at
    124  * fixed virtual addresses very early in initarm() so that we can use
    125  * them while booting the kernel, and stay at the same address
    126  * throughout whole kernel's life time.
    127  *
    128  * We use this table twice; once with bootstrap page table, and once
    129  * with kernel's page table which we build up in initarm().
    130  *
    131  * Since we map these registers into the bootstrap page table using
    132  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
    133  * registers segment-aligned and segment-rounded in order to avoid
    134  * using the 2nd page tables.
    135  */
    136 
    137 static const struct pmap_devmap devmap[] = {
    138 	{
    139 		KERNEL_IO_IOREG_VBASE,
    140 		BCM53XX_IOREG_PBASE,		/* 0x18000000 */
    141 		BCM53XX_IOREG_SIZE,		/* 2MB */
    142 		VM_PROT_READ|VM_PROT_WRITE,
    143 		PTE_NOCACHE,
    144 	},
    145 	{
    146 		KERNEL_IO_ARMCORE_VBASE,
    147 		BCM53XX_ARMCORE_PBASE,		/* 0x19000000 */
    148 		BCM53XX_ARMCORE_SIZE,		/* 1MB */
    149 		VM_PROT_READ|VM_PROT_WRITE,
    150 		PTE_NOCACHE,
    151 	},
    152 #if NPCI > 0
    153 	{
    154 		KERNEL_IO_PCIE0_OWIN_VBASE,
    155 		BCM53XX_PCIE0_OWIN_PBASE,	/* 0x08000000 */
    156 		BCM53XX_PCIE0_OWIN_SIZE,	/* 4MB */
    157 		VM_PROT_READ|VM_PROT_WRITE,
    158 		PTE_NOCACHE,
    159 	},
    160 	{
    161 		KERNEL_IO_PCIE1_OWIN_VBASE,
    162 		BCM53XX_PCIE1_OWIN_PBASE,	/* 0x40000000 */
    163 		BCM53XX_PCIE1_OWIN_SIZE,	/* 4MB */
    164 		VM_PROT_READ|VM_PROT_WRITE,
    165 		PTE_NOCACHE,
    166 	},
    167 	{
    168 		KERNEL_IO_PCIE2_OWIN_VBASE,
    169 		BCM53XX_PCIE2_OWIN_PBASE,	/* 0x48000000 */
    170 		BCM53XX_PCIE2_OWIN_SIZE,	/* 4MB */
    171 		VM_PROT_READ|VM_PROT_WRITE,
    172 		PTE_NOCACHE,
    173 	},
    174 #endif /* NPCI > 0 */
    175 	{ 0, 0, 0, 0, 0 }
    176 };
    177 
    178 static const struct boot_physmem bp_first256 = {
    179 #ifdef BCM5301X
    180 	.bp_start = 0x80000000 / NBPG,
    181 	.bp_pages = 0x10000000 / NBPG,
    182 #elif defined(BCM563XX)
    183 	.bp_start = 0x60000000 / NBPG,
    184 	.bp_pages = 0x20000000 / NBPG,
    185 #endif
    186 	.bp_freelist = VM_FREELIST_ISADMA,
    187 	.bp_flags = 0,
    188 };
    189 
    190 /*
    191  * u_int initarm(...)
    192  *
    193  * Initial entry point on startup. This gets called before main() is
    194  * entered.
    195  * It should be responsible for setting up everything that must be
    196  * in place when main is called.
    197  * This includes
    198  *   Taking a copy of the boot configuration structure.
    199  *   Initialising the physical console so characters can be printed.
    200  *   Setting up page tables for the kernel
    201  */
    202 u_int
    203 initarm(void *arg)
    204 {
    205 	pmap_devmap_register(devmap);
    206 	bcm53xx_bootstrap(KERNEL_IO_IOREG_VBASE);
    207 
    208 #ifdef MULTIPROCESSOR
    209 	uint32_t scu_cfg = bus_space_read_4(bcm53xx_armcore_bst, bcm53xx_armcore_bsh,
    210 	    ARMCORE_SCU_BASE + SCU_CFG);
    211 	arm_cpu_max = scu_cfg & SCU_CFG_CPUMAX;
    212 	membar_producer();
    213 #endif
    214 	/*
    215 	 * Heads up ... Setup the CPU / MMU / TLB functions
    216 	 */
    217 	if (set_cpufuncs())		// starts PMC counter
    218 		panic("cpu not recognized!");
    219 
    220 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    221 
    222 	consinit();
    223 
    224 	bcm53xx_cpu_softc_init(curcpu());
    225 	bcm53xx_print_clocks();
    226 
    227 #if NBCMRNG_CCB > 0
    228 	/*
    229 	 * Start this early since it takes a while to startup up.
    230 	 */
    231 	bcm53xx_rng_start(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh);
    232 #endif
    233 
    234 	printf("uboot arg = %#x, %#x, %#x, %#x\n",
    235 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
    236 
    237 	/* Talk to the user */
    238 	printf("\nNetBSD/evbarm (" ___STRING(EVBARM_BOARDTYPE) ") booting ...\n");
    239 
    240 	bootargs[0] = '\0';
    241 
    242 #if defined(VERBOSE_INIT_ARM) || 1
    243 	printf("initarm: Configuring system");
    244 #ifdef MULTIPROCESSOR
    245 	printf(" (%u cpu%s, hatched %#x)",
    246 	    arm_cpu_max + 1, arm_cpu_max + 1 ? "s" : "",
    247 	    arm_cpu_hatched);
    248 #endif
    249 	printf(", CLIDR=%010o CTR=%#x PMUSERSR=%#x",
    250 	    armreg_clidr_read(), armreg_ctr_read(), armreg_pmuserenr_read());
    251 	printf("\n");
    252 #endif
    253 
    254 	psize_t memsize = bcm53xx_memprobe();
    255 #ifdef MEMSIZE
    256 	if ((memsize >> 20) > MEMSIZE)
    257 		memsize = MEMSIZE*1024*1024;
    258 #endif
    259 	const bool bigmem_p = (memsize >> PGSHIFT) > bp_first256.bp_pages;
    260 
    261 	arm32_bootmem_init(KERN_VTOPHYS(KERNEL_BASE), memsize,
    262 	    (paddr_t)KERNEL_BASE_phys);
    263 
    264 	bcm53xx_dma_bootstrap(memsize);
    265 
    266 	/*
    267 	 * This is going to do all the hard work of setting up the first and
    268 	 * and second level page tables.  Pages of memory will be allocated
    269 	 * and mapped for other structures that are required for system
    270 	 * operation.  When it returns, physical_freestart and free_pages will
    271 	 * have been updated to reflect the allocations that were made.  In
    272 	 * addition, kernel_l1pt, kernel_pt_table[], systempage, irqstack,
    273 	 * abtstack, undstack, kernelstack, msgbufphys will be set to point to
    274 	 * the memory that was allocated for them.
    275 	 */
    276 	arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0, devmap, true);
    277 
    278 	cpu_reset_address = bcm53xx_system_reset;
    279 	/* we've a specific device_register routine */
    280 	evbarm_device_register = bcm53xx_device_register;
    281 	if (bigmem_p) {
    282 		/*
    283 		 * If we have more than 256MB
    284 		 */
    285 		arm_poolpage_vmfreelist = bp_first256.bp_freelist;
    286 		arm_page_to_pggroup = bcm53xx_page_to_pggroup;
    287 	}
    288 
    289 	/*
    290 	 * If we have more than 256MB of RAM, set aside the first 256MB for
    291 	 * non-default VM allocations.
    292 	 */
    293 	return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE,
    294 	    (bigmem_p ? &bp_first256 : NULL), (bigmem_p ? 1 : 0));
    295 }
    296 
    297 static size_t
    298 bcm53xx_page_to_pggroup(struct vm_page *pg, size_t ncolors)
    299 {
    300 	const struct boot_physmem * const bp = &bp_first256;
    301 	const paddr_t pfn = VM_PAGE_TO_PHYS(pg) >> PGSHIFT;
    302 	const u_int color = VM_PGCOLOR_BUCKET(pg);
    303 
    304 	if (arm_poolpage_vmfreelist != VM_FREELIST_DEFAULT
    305 	    && bp->bp_start <= pfn && pfn < bp->bp_start + bp->bp_pages)
    306 		return VM_FREELIST_ISADMA * ncolors + color;
    307 
    308 	return VM_FREELIST_DEFAULT * ncolors + color;
    309 }
    310 
    311 void
    312 consinit(void)
    313 {
    314 	static bool consinit_called = false;
    315 	uint32_t v;
    316 	if (consinit_called)
    317 		return;
    318 
    319 	consinit_called = true;
    320 
    321 	/*
    322 	 * Force UART clock to the reference clock
    323 	 */
    324 	v = bus_space_read_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    325 	    IDM_BASE + IDM_APBX_BASE + IDM_IO_CONTROL_DIRECT);
    326 	v &= ~IO_CONTROL_DIRECT_UARTCLKSEL;
    327 	bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    328 	    IDM_BASE + IDM_APBX_BASE + IDM_IO_CONTROL_DIRECT, v);
    329 
    330 	/*
    331 	 * Switch to the reference clock
    332 	 */
    333 	v = bus_space_read_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    334 	    CCA_MISC_BASE + MISC_CORECTL);
    335 	v &= ~CORECTL_UART_CLK_OVERRIDE;
    336 	bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    337 	    CCA_MISC_BASE + MISC_CORECTL, v);
    338 
    339         if (comcnattach(bcm53xx_ioreg_bst, comcnaddr, comcnspeed,
    340                         BCM53XX_REF_CLK, COM_TYPE_NORMAL, comcnmode))
    341                 panic("Serial console can not be initialized.");
    342 }
    343 
    344 static void
    345 bcm53xx_system_reset(void)
    346 {
    347 	bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    348 	    MISC_WATCHDOG, 1);
    349 }
    350