vexpress_platform.c revision 1.2
1/* $NetBSD: vexpress_platform.c,v 1.2 2017/06/02 20:16:05 jmcneill Exp $ */ 2 3/*- 4 * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca> 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include "opt_multiprocessor.h" 30#include "opt_fdt_arm.h" 31 32#include <sys/cdefs.h> 33__KERNEL_RCSID(0, "$NetBSD: vexpress_platform.c,v 1.2 2017/06/02 20:16:05 jmcneill Exp $"); 34 35#include <sys/param.h> 36#include <sys/bus.h> 37#include <sys/cpu.h> 38#include <sys/device.h> 39#include <sys/termios.h> 40 41#include <dev/fdt/fdtvar.h> 42 43#include <uvm/uvm_extern.h> 44 45#include <machine/bootconfig.h> 46#include <arm/cpufunc.h> 47 48#include <arm/fdt/arm_fdtvar.h> 49 50#include <arm/cortex/gtmr_var.h> 51 52#include <arm/cortex/gic_reg.h> 53 54#include <evbarm/dev/plcomvar.h> 55 56#include <arm/vexpress/vexpress_platform.h> 57 58#define VEXPRESS_REF_FREQ 24000000 59 60#define DEVMAP_ALIGN(a) ((a) & ~L1_S_OFFSET) 61#define DEVMAP_SIZE(s) roundup2((s), L1_S_SIZE) 62#define DEVMAP_ENTRY(va, pa, sz) \ 63 { \ 64 .pd_va = DEVMAP_ALIGN(va), \ 65 .pd_pa = DEVMAP_ALIGN(pa), \ 66 .pd_size = DEVMAP_SIZE(sz), \ 67 .pd_prot = VM_PROT_READ|VM_PROT_WRITE, \ 68 .pd_cache = PTE_NOCACHE \ 69 } 70#define DEVMAP_ENTRY_END { 0 } 71 72extern struct bus_space armv7_generic_bs_tag; 73extern struct bus_space armv7_generic_a4x_bs_tag; 74extern struct arm32_bus_dma_tag armv7_generic_dma_tag; 75 76#define SYSREG_BASE 0x1c010000 77#define SYSREG_SIZE 0x1000 78 79#define SYS_FLAGS 0x0030 80#define SYS_FLAGSCLR 0x0034 81#define SYS_CFGDATA 0x00a0 82#define SYS_CFGCTRL 0x00a4 83#define SYS_CFGCTRL_START __BIT(31) 84#define SYS_CFGCTRL_WRITE __BIT(30) 85#define SYS_CFGCTRL_DCC __BITS(29,26) 86#define SYS_CFGCTRL_FUNCTION __BITS(25,20) 87#define SYS_CFGCTRL_FUNCTION_SHUTDOWN 8 88#define SYS_CFGCTRL_FUNCTION_REBOOT 9 89#define SYS_CFGCTRL_SITE __BITS(17,16) 90#define SYS_CFGCTRL_POSITION __BITS(15,12) 91#define SYS_CFGCTRL_DEVICE __BITS(11,0) 92#define SYS_CFGSTAT 0x00a8 93#define SYS_CFGSTAT_ERROR __BIT(1) 94#define SYS_CFGSTAT_COMPLETE __BIT(0) 95 96static bus_space_tag_t sysreg_bst = &armv7_generic_bs_tag; 97static bus_space_handle_t sysreg_bsh; 98 99#define SYSREG_WRITE(o, v) \ 100 bus_space_write_4(sysreg_bst, sysreg_bsh, (o), (v)) 101 102 103static void 104vexpress_a15_smp_init(void) 105{ 106 extern void cortex_mpstart(void); 107 bus_space_tag_t gicd_bst = &armv7_generic_bs_tag; 108 bus_space_handle_t gicd_bsh; 109 int started = 0; 110 111 /* Bitmask of CPUs (non-BSP) to start */ 112 for (int i = 1; i < arm_cpu_max; i++) 113 started |= __BIT(i); 114 115 /* Write init vec to SYS_FLAGS register */ 116 SYSREG_WRITE(SYS_FLAGSCLR, 0xffffffff); 117 SYSREG_WRITE(SYS_FLAGS, (uint32_t)cortex_mpstart); 118 119 /* Map GIC distributor */ 120 bus_space_map(gicd_bst, VEXPRESS_GIC_PBASE + GICD_BASE, 121 0x1000, 0, &gicd_bsh); 122 123 /* Enable GIC distributor */ 124 bus_space_write_4(gicd_bst, gicd_bsh, 125 GICD_CTRL, GICD_CTRL_Enable); 126 127 /* Send sw interrupt to APs */ 128 const uint32_t sgir = GICD_SGIR_TargetListFilter_NotMe; 129 bus_space_write_4(gicd_bst, gicd_bsh, GICD_SGIR, sgir); 130 131 /* Wait for APs to start */ 132 for (u_int i = 0x10000000; i > 0; i--) { 133 arm_dmb(); 134 if (arm_cpu_hatched == started) 135 break; 136 } 137 138 /* Disable GIC distributor */ 139 bus_space_write_4(gicd_bst, gicd_bsh, GICD_CTRL, 0); 140} 141 142 143static const struct pmap_devmap * 144vexpress_platform_devmap(void) 145{ 146 static const struct pmap_devmap devmap[] = { 147 DEVMAP_ENTRY(VEXPRESS_CORE_VBASE, 148 VEXPRESS_CORE_PBASE, 149 VEXPRESS_CORE_SIZE), 150 DEVMAP_ENTRY(VEXPRESS_GIC_VBASE, 151 VEXPRESS_GIC_PBASE, 152 VEXPRESS_GIC_SIZE), 153 DEVMAP_ENTRY_END 154 }; 155 156 return devmap; 157} 158 159static void 160vexpress_platform_bootstrap(void) 161{ 162 bus_space_map(sysreg_bst, SYSREG_BASE, SYSREG_SIZE, 0, 163 &sysreg_bsh); 164 165 arm_cpu_max = 1 + __SHIFTOUT(armreg_l2ctrl_read(), L2CTRL_NUMCPU); 166 167 vexpress_a15_smp_init(); 168} 169 170static void 171vexpress_platform_init_attach_args(struct fdt_attach_args *faa) 172{ 173 faa->faa_bst = &armv7_generic_bs_tag; 174 faa->faa_a4x_bst = &armv7_generic_a4x_bs_tag; 175 faa->faa_dmat = &armv7_generic_dma_tag; 176} 177 178static void 179vexpress_platform_early_putchar(char c) 180{ 181} 182 183static void 184vexpress_platform_device_register(device_t self, void *aux) 185{ 186} 187 188static void 189vexpress_platform_reset(void) 190{ 191 SYSREG_WRITE(SYS_CFGSTAT, 0); 192 SYSREG_WRITE(SYS_CFGDATA, 0); 193 SYSREG_WRITE(SYS_CFGCTRL, 194 SYS_CFGCTRL_START | 195 SYS_CFGCTRL_WRITE | 196 __SHIFTIN(SYS_CFGCTRL_FUNCTION_REBOOT, 197 SYS_CFGCTRL_FUNCTION)); 198} 199 200static u_int 201vexpress_platform_uart_freq(void) 202{ 203 return VEXPRESS_REF_FREQ; 204} 205 206static const struct arm_platform vexpress_platform = { 207 .devmap = vexpress_platform_devmap, 208 .bootstrap = vexpress_platform_bootstrap, 209 .init_attach_args = vexpress_platform_init_attach_args, 210 .early_putchar = vexpress_platform_early_putchar, 211 .device_register = vexpress_platform_device_register, 212 .reset = vexpress_platform_reset, 213 .delay = gtmr_delay, 214 .uart_freq = vexpress_platform_uart_freq, 215}; 216 217ARM_PLATFORM(vexpress, "arm,vexpress", &vexpress_platform); 218