Home | History | Annotate | Line # | Download | only in bcm53xx
bcm53xx_machdep.c revision 1.2
      1 /*	$NetBSD: bcm53xx_machdep.c,v 1.2 2012/09/07 11:53:49 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.2 2012/09/07 11:53:49 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 static void bcm53xx_system_reset(void);
     87 
     88 /*
     89  * Macros to translate between physical and virtual for a subset of the
     90  * kernel address space.  *Not* for general use.
     91  */
     92 #define	KERN_VTOPDIFF	((vaddr_t)KERNEL_BASE_phys - (vaddr_t)KERNEL_BASE_virt)
     93 #define KERN_VTOPHYS(va) ((paddr_t)((vaddr_t)va + KERN_VTOPDIFF))
     94 #define KERN_PHYSTOV(pa) ((vaddr_t)((paddr_t)pa - KERN_VTOPDIFF))
     95 
     96 #ifndef CONADDR
     97 #define CONADDR		(BCM53XX_IOREG_PBASE + CCA_UART0_BASE)
     98 #endif
     99 #ifndef CONSPEED
    100 #define CONSPEED B115200
    101 #endif
    102 #ifndef CONMODE
    103 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    104 #endif
    105 
    106 #if (NCOM > 0)
    107 static const bus_addr_t comcnaddr = (bus_addr_t)CONADDR;
    108 
    109 int comcnspeed = CONSPEED;
    110 int comcnmode = CONMODE | CLOCAL;
    111 #endif
    112 
    113 #ifdef KGDB
    114 #include <sys/kgdb.h>
    115 #endif
    116 
    117 /*
    118  * Static device mappings. These peripheral registers are mapped at
    119  * fixed virtual addresses very early in initarm() so that we can use
    120  * them while booting the kernel, and stay at the same address
    121  * throughout whole kernel's life time.
    122  *
    123  * We use this table twice; once with bootstrap page table, and once
    124  * with kernel's page table which we build up in initarm().
    125  *
    126  * Since we map these registers into the bootstrap page table using
    127  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
    128  * registers segment-aligned and segment-rounded in order to avoid
    129  * using the 2nd page tables.
    130  */
    131 
    132 static const struct pmap_devmap devmap[] = {
    133 	{
    134 		KERNEL_IO_IOREG_VBASE,
    135 		BCM53XX_IOREG_PBASE,		/* 0x18000000 */
    136 		BCM53XX_IOREG_SIZE,		/* 2MB */
    137 		VM_PROT_READ|VM_PROT_WRITE,
    138 		PTE_NOCACHE,
    139 	},
    140 	{
    141 		KERNEL_IO_ARMCORE_VBASE,
    142 		BCM53XX_ARMCORE_PBASE,		/* 0x19000000 */
    143 		BCM53XX_ARMCORE_SIZE,		/* 1MB */
    144 		VM_PROT_READ|VM_PROT_WRITE,
    145 		PTE_NOCACHE,
    146 	},
    147 #if NPCI > 0
    148 	{
    149 		KERNEL_IO_PCIE0_OWIN_VBASE,
    150 		BCM53XX_PCIE0_OWIN_PBASE,	/* 0x08000000 */
    151 		BCM53XX_PCIE0_OWIN_SIZE,	/* 4MB */
    152 		VM_PROT_READ|VM_PROT_WRITE,
    153 		PTE_NOCACHE,
    154 	},
    155 	{
    156 		KERNEL_IO_PCIE1_OWIN_VBASE,
    157 		BCM53XX_PCIE1_OWIN_PBASE,	/* 0x40000000 */
    158 		BCM53XX_PCIE1_OWIN_SIZE,	/* 4MB */
    159 		VM_PROT_READ|VM_PROT_WRITE,
    160 		PTE_NOCACHE,
    161 	},
    162 	{
    163 		KERNEL_IO_PCIE2_OWIN_VBASE,
    164 		BCM53XX_PCIE2_OWIN_PBASE,	/* 0x48000000 */
    165 		BCM53XX_PCIE2_OWIN_SIZE,	/* 4MB */
    166 		VM_PROT_READ|VM_PROT_WRITE,
    167 		PTE_NOCACHE,
    168 	},
    169 #endif /* NPCI > 0 */
    170 	{ 0, 0, 0, 0, 0 }
    171 };
    172 
    173 /*
    174  * u_int initarm(...)
    175  *
    176  * Initial entry point on startup. This gets called before main() is
    177  * entered.
    178  * It should be responsible for setting up everything that must be
    179  * in place when main is called.
    180  * This includes
    181  *   Taking a copy of the boot configuration structure.
    182  *   Initialising the physical console so characters can be printed.
    183  *   Setting up page tables for the kernel
    184  */
    185 u_int
    186 initarm(void *arg)
    187 {
    188 	pmap_devmap_register(devmap);
    189 	bcm53xx_bootstrap(KERNEL_IO_IOREG_VBASE);
    190 
    191 #ifdef MULTIPROCESSOR
    192 	uint32_t scu_cfg = bus_space_read_4(bcm53xx_armcore_bst, bcm53xx_armcore_bsh,
    193 	    ARMCORE_SCU_BASE + SCU_CFG);
    194 	arm_cpu_max = scu_cfg & SCU_CFG_CPUMAX;
    195 	membar_producer();
    196 #endif
    197 	/*
    198 	 * Heads up ... Setup the CPU / MMU / TLB functions
    199 	 */
    200 	if (set_cpufuncs())		// starts PMC counter
    201 		panic("cpu not recognized!");
    202 
    203 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    204 
    205 	consinit();
    206 
    207 	bcm53xx_cpu_softc_init(curcpu());
    208 	bcm53xx_print_clocks();
    209 
    210 #if NBCMRNG_CCB > 0
    211 	/*
    212 	 * Start this early since it takes a while to startup up.
    213 	 */
    214 	bcm53xx_rng_start(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh);
    215 #endif
    216 
    217 	/* Talk to the user */
    218 	printf("\nNetBSD/evbarm (" ___STRING(EVBARM_BOARDTYPE) ") booting ...\n");
    219 
    220 	bootargs[0] = '\0';
    221 
    222 #if defined(VERBOSE_INIT_ARM) || 1
    223 	printf("initarm: Configuring system");
    224 #ifdef MULTIPROCESSOR
    225 	printf(" (%u cpu%s, hatched %#x)",
    226 	    arm_cpu_max + 1, arm_cpu_max + 1 ? "s" : "",
    227 	    arm_cpu_hatched);
    228 #endif
    229 	printf("\n");
    230 #endif
    231 
    232 	arm32_bootmem_init(KERN_VTOPHYS(KERNEL_BASE), bcm53xx_memprobe(),
    233 	    (paddr_t)KERNEL_BASE_phys);
    234 
    235 	/*
    236 	 * This is going to do all the hard work of setting up the first and
    237 	 * and second level page tables.  Pages of memory will be allocated
    238 	 * and mapped for other structures that are required for system
    239 	 * operation.  When it returns, physical_freestart and free_pages will
    240 	 * have been updated to reflect the allocations that were made.  In
    241 	 * addition, kernel_l1pt, kernel_pt_table[], systempage, irqstack,
    242 	 * abtstack, undstack, kernelstack, msgbufphys will be set to point to
    243 	 * the memory that was allocated for them.
    244 	 */
    245 	arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0, devmap, true);
    246 
    247 	cpu_reset_address = bcm53xx_system_reset;
    248 	/* we've a specific device_register routine */
    249 	evbarm_device_register = bcm53xx_device_register;
    250 
    251 	return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
    252 }
    253 
    254 void
    255 consinit(void)
    256 {
    257 	static bool consinit_called = false;
    258 	uint32_t v;
    259 	if (consinit_called)
    260 		return;
    261 
    262 	consinit_called = true;
    263 
    264 	/*
    265 	 * Force UART clock to the reference clock
    266 	 */
    267 	v = bus_space_read_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    268 	    IDM_BASE + IDM_APBX_BASE + IDM_IO_CONTROL_DIRECT);
    269 	v &= ~IO_CONTROL_DIRECT_UARTCLKSEL;
    270 	bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    271 	    IDM_BASE + IDM_APBX_BASE + IDM_IO_CONTROL_DIRECT, v);
    272 
    273 	/*
    274 	 * Switch to the reference clock
    275 	 */
    276 	v = bus_space_read_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    277 	    CCA_MISC_BASE + MISC_CORECTL);
    278 	v &= ~CORECTL_UART_CLK_OVERRIDE;
    279 	bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    280 	    CCA_MISC_BASE + MISC_CORECTL, v);
    281 
    282         if (comcnattach(bcm53xx_ioreg_bst, comcnaddr, comcnspeed,
    283                         BCM53XX_REF_CLK, COM_TYPE_NORMAL, comcnmode))
    284                 panic("Serial console can not be initialized.");
    285 }
    286 
    287 static void
    288 bcm53xx_system_reset(void)
    289 {
    290 	bus_space_write_4(bcm53xx_ioreg_bst, bcm53xx_ioreg_bsh,
    291 	    MISC_WATCHDOG, 1);
    292 }
    293