Home | History | Annotate | Line # | Download | only in cortex
armperiph.c revision 1.11.6.1
      1 /*-
      2  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Matt Thomas of 3am Software Foundry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include "locators.h"
     31 
     32 #include <sys/cdefs.h>
     33 
     34 __KERNEL_RCSID(1, "$NetBSD: armperiph.c,v 1.11.6.1 2017/12/13 01:03:41 matt Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/device.h>
     38 #include <sys/lwp.h>
     39 
     40 #include "ioconf.h"
     41 
     42 #include <arm/mainbus/mainbus.h>
     43 #include <arm/cortex/mpcore_var.h>
     44 #include <arm/cortex/gtmr_intr.h>
     45 
     46 static int armperiph_match(device_t, cfdata_t, void *);
     47 static void armperiph_attach(device_t, device_t, void *);
     48 
     49 static bool attached;
     50 
     51 struct armperiph_softc {
     52 	device_t sc_dev;
     53 	bus_space_tag_t sc_memt;
     54 	bus_space_handle_t sc_memh;
     55 };
     56 
     57 struct armperiph_info {
     58 	const char pi_name[12];
     59 	bus_size_t pi_off1;
     60 	bus_size_t pi_off2;
     61 };
     62 
     63 #ifdef CPU_CORTEXA5
     64 static const struct armperiph_info a5_devices[] = {
     65 	{ "armscu", 0x0000, 0 },
     66 	{ "armgic", 0x1000, 0x0100 },
     67 	{ "a9tmr",  0x0200, 0 },
     68 	{ "a9wdt",   0x0600, 0 },
     69 	{ "arml2cc", 0, 0 },	/* external; needs "offset" property */
     70 	{ "", 0, 0 },
     71 };
     72 #endif
     73 
     74 #ifdef CPU_CORTEXA7
     75 static const struct armperiph_info a7_devices[] = {
     76 	{ "armgic",  0x1000, 0x2000 },
     77 	{ "armgtmr", 0, 0 },
     78 	{ "", 0, 0 },
     79 };
     80 #endif
     81 
     82 #ifdef CPU_CORTEXA9
     83 static const struct armperiph_info a9_devices[] = {
     84 	{ "armscu",  0x0000, 0 },
     85 	{ "arml2cc", 0x2000, 0 },
     86 	{ "armgic",  0x1000, 0x0100 },
     87 	{ "a9tmr",   0x0200, 0 },
     88 	{ "a9wdt",   0x0600, 0 },
     89 	{ "", 0, 0 },
     90 };
     91 #endif
     92 
     93 #ifdef CPU_CORTEXA15
     94 static const struct armperiph_info a15_devices[] = {
     95 	{ "armgic",  0x1000, 0x2000 },
     96 	{ "armgtmr", 0, 0 },
     97 	{ "", 0, 0 },
     98 };
     99 #endif
    100 
    101 #ifdef CPU_CORTEXA17
    102 static const struct armperiph_info a17_devices[] = {
    103 	{ "armgic",  0x1000, 0x2000 },
    104 	{ "armgtmr", 0, 0 },
    105 	{ "", 0, 0 },
    106 };
    107 #endif
    108 
    109 #ifdef CPU_CORTEXA35
    110 static const struct armperiph_info a35_devices[] = {
    111 	{ "armgic",  0, 0x4000 },
    112 	{ "armgtmr", 0, 0 },
    113 	{ "", 0, 0 },
    114 };
    115 #endif
    116 
    117 #ifdef CPU_CORTEXA57
    118 static const struct armperiph_info a57_devices[] = {
    119 	{ "armgic",  0x1000, 0x2000 },
    120 	{ "armgtmr", 0, 0 },
    121 	{ "", 0, 0 },
    122 };
    123 #endif
    124 
    125 
    126 static const struct mpcore_config {
    127 	const struct armperiph_info *cfg_devices;
    128 	uint32_t cfg_cpuid;
    129 	uint32_t cfg_cbar_size;
    130 } configs[] = {
    131 #ifdef CPU_CORTEXA5
    132 	{ a5_devices, 0x410fc050, 2*4096 },
    133 #endif
    134 #ifdef CPU_CORTEXA7
    135 	{ a7_devices, 0x410fc070, 8*4096 },
    136 #endif
    137 #ifdef CPU_CORTEXA9
    138 	{ a9_devices, 0x410fc090, 3*4096 },
    139 #endif
    140 #ifdef CPU_CORTEXA15
    141 	{ a15_devices, 0x410fc0f0, 8*4096 },
    142 #endif
    143 #ifdef CPU_CORTEXA17
    144 	{ a17_devices, 0x410fc0e0, 8*4096 },
    145 #endif
    146 #ifdef CPU_CORTEXA35
    147 	{ a35_devices, 0x410fd040, 8*4096 },
    148 #endif
    149 #ifdef CPU_CORTEXA57
    150 	{ a57_devices, 0x410fd070, 8*4096 },
    151 #endif
    152 };
    153 
    154 static const struct mpcore_config *
    155 armperiph_find_config(void)
    156 {
    157 	const uint32_t arm_cpuid = curcpu()->ci_arm_cpuid & 0xff0ff0f0;
    158 	for (size_t i = 0; i < __arraycount(configs); i++) {
    159 		if (arm_cpuid == configs[i].cfg_cpuid) {
    160 			return configs + i;
    161 		}
    162 	}
    163 
    164 	return NULL;
    165 }
    166 
    167 CFATTACH_DECL_NEW(armperiph, sizeof(struct armperiph_softc),
    168     armperiph_match, armperiph_attach, NULL, NULL);
    169 
    170 static int
    171 armperiph_match(device_t parent, cfdata_t cf, void *aux)
    172 {
    173 	struct mainbus_attach_args * const mb = aux;
    174 	const int base = cf->cf_loc[MAINBUSCF_BASE];
    175 	const int size = cf->cf_loc[MAINBUSCF_SIZE];
    176 	const int dack = cf->cf_loc[MAINBUSCF_DACK];
    177 	const int irq = cf->cf_loc[MAINBUSCF_IRQ];
    178 	const int intrbase = cf->cf_loc[MAINBUSCF_INTRBASE];
    179 
    180 	if (attached)
    181 		return 0;
    182 
    183 	if (base != MAINBUSCF_BASE_DEFAULT || base != mb->mb_iobase
    184 	    || size != MAINBUSCF_SIZE_DEFAULT || size != mb->mb_iosize
    185 	    || dack != MAINBUSCF_DACK_DEFAULT || dack != mb->mb_drq
    186 	    || irq != MAINBUSCF_IRQ_DEFAULT || irq != mb->mb_irq
    187 	    || intrbase != MAINBUSCF_INTRBASE_DEFAULT
    188 	    || intrbase != mb->mb_intrbase)
    189 		return 0;
    190 
    191 	if (!CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid))
    192 		return 0;
    193 
    194 	if (armreg_cbar_read() == 0)
    195 		return 0;
    196 
    197 	if (armperiph_find_config() == NULL)
    198 		return 0;
    199 
    200 	return 1;
    201 }
    202 
    203 static void
    204 armperiph_attach(device_t parent, device_t self, void *aux)
    205 {
    206 	struct armperiph_softc * const sc = device_private(self);
    207 	struct mainbus_attach_args * const mb = aux;
    208 	bus_addr_t cbar = armreg_cbar_read();
    209 	const struct mpcore_config * const cfg = armperiph_find_config();
    210 	prop_dictionary_t prop = device_properties(self);
    211 	uint32_t cbar_override;
    212 
    213 	if (prop_dictionary_get_uint32(prop, "cbar", &cbar_override))
    214 		cbar = (bus_addr_t)cbar_override;
    215 
    216 	/*
    217 	 * The normal mainbus bus space will not work for us so the port's
    218 	 * device_register must have replaced it with one that will work.
    219 	 */
    220 	sc->sc_dev = self;
    221 	sc->sc_memt = mb->mb_iot;
    222 
    223 	int error = bus_space_map(sc->sc_memt, cbar, cfg->cfg_cbar_size, 0,
    224 	    &sc->sc_memh);
    225 	if (error) {
    226 		aprint_normal(": error mapping registers at %#lx: %d\n",
    227 		    cbar, error);
    228 		return;
    229 	}
    230 	aprint_normal("\n");
    231 
    232 	/*
    233 	 * Let's try to attach any children we may have.
    234 	 */
    235 	for (size_t i = 0; cfg->cfg_devices[i].pi_name[0] != 0; i++) {
    236 		struct mpcore_attach_args mpcaa = {
    237 			.mpcaa_name = cfg->cfg_devices[i].pi_name,
    238 			.mpcaa_memt = sc->sc_memt,
    239 			.mpcaa_memh = sc->sc_memh,
    240 			.mpcaa_off1 = cfg->cfg_devices[i].pi_off1,
    241 			.mpcaa_off2 = cfg->cfg_devices[i].pi_off2,
    242 		};
    243 #if defined(CPU_CORTEXA7) || defined(CPU_CORTEXA15) || defined(CPU_CORTEXA57)
    244 		if (strcmp(mpcaa.mpcaa_name, "armgtmr") == 0) {
    245 			mpcaa.mpcaa_irq = IRQ_GTMR_PPI_VTIMER;
    246 		}
    247 #endif
    248 
    249 		config_found(self, &mpcaa, NULL);
    250 	}
    251 }
    252