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 #include "opt_cputypes.h" 32 33 #include <sys/cdefs.h> 34 35 __KERNEL_RCSID(1, "$NetBSD: armperiph.c,v 1.16 2020/09/29 19:58:50 jmcneill Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/device.h> 39 #include <sys/lwp.h> 40 41 #include "ioconf.h" 42 43 #include <arm/mainbus/mainbus.h> 44 #include <arm/cortex/mpcore_var.h> 45 #include <arm/cortex/gtmr_intr.h> 46 47 static int armperiph_match(device_t, cfdata_t, void *); 48 static void armperiph_attach(device_t, device_t, void *); 49 50 static bool attached; 51 52 struct armperiph_softc { 53 device_t sc_dev; 54 bus_space_tag_t sc_memt; 55 bus_space_handle_t sc_memh; 56 }; 57 58 struct armperiph_info { 59 const char pi_name[12]; 60 bus_size_t pi_off1; 61 bus_size_t pi_off2; 62 }; 63 64 static const struct armperiph_info a5_devices[] = { 65 { "armscu", 0x0000, 0 }, 66 { "armgic", 0x1000, 0x0100 }, 67 { "arma9tmr", 0x0200, 0 }, 68 { "a9wdt", 0x0600, 0 }, 69 { "arml2cc", 0, 0 }, /* external; needs "offset" property */ 70 { "", 0, 0 }, 71 }; 72 73 static const struct armperiph_info a7_devices[] = { 74 { "armgic", 0x1000, 0x2000 }, 75 { "armgtmr", 0, 0 }, 76 { "", 0, 0 }, 77 }; 78 79 static const struct armperiph_info a9_devices[] = { 80 { "armscu", 0x0000, 0 }, 81 { "arml2cc", 0x2000, 0 }, 82 { "armgic", 0x1000, 0x0100 }, 83 { "arma9tmr", 0x0200, 0 }, 84 { "a9wdt", 0x0600, 0 }, 85 { "", 0, 0 }, 86 }; 87 88 static const struct armperiph_info a15_devices[] = { 89 { "armgic", 0x1000, 0x2000 }, 90 { "armgtmr", 0, 0 }, 91 { "", 0, 0 }, 92 }; 93 94 static const struct armperiph_info a17_devices[] = { 95 { "armgic", 0x1000, 0x2000 }, 96 { "armgtmr", 0, 0 }, 97 { "", 0, 0 }, 98 }; 99 100 static const struct armperiph_info a57_devices[] = { 101 { "armgic", 0x1000, 0x2000 }, 102 { "armgtmr", 0, 0 }, 103 { "", 0, 0 }, 104 }; 105 106 107 static const struct mpcore_config { 108 const struct armperiph_info *cfg_devices; 109 uint32_t cfg_cpuid; 110 uint32_t cfg_cbar_size; 111 } configs[] = { 112 { a5_devices, 0x410fc050, 2*4096 }, 113 { a7_devices, 0x410fc070, 8*4096 }, 114 { a9_devices, 0x410fc090, 3*4096 }, 115 { a15_devices, 0x410fc0f0, 8*4096 }, 116 { a17_devices, 0x410fc0e0, 8*4096 }, 117 { a57_devices, 0x410fd070, 8*4096 }, 118 }; 119 120 static const struct mpcore_config * 121 armperiph_find_config(void) 122 { 123 const uint32_t arm_cpuid = curcpu()->ci_arm_cpuid & 0xff0ff0f0; 124 for (size_t i = 0; i < __arraycount(configs); i++) { 125 if (arm_cpuid == configs[i].cfg_cpuid) { 126 return configs + i; 127 } 128 } 129 130 return NULL; 131 } 132 133 CFATTACH_DECL_NEW(armperiph, sizeof(struct armperiph_softc), 134 armperiph_match, armperiph_attach, NULL, NULL); 135 136 static int 137 armperiph_match(device_t parent, cfdata_t cf, void *aux) 138 { 139 struct mainbus_attach_args * const mb = aux; 140 const int base = cf->cf_loc[MAINBUSCF_BASE]; 141 const int size = cf->cf_loc[MAINBUSCF_SIZE]; 142 const int dack = cf->cf_loc[MAINBUSCF_DACK]; 143 const int irq = cf->cf_loc[MAINBUSCF_IRQ]; 144 const int intrbase = cf->cf_loc[MAINBUSCF_INTRBASE]; 145 146 if (attached) 147 return 0; 148 149 if (base != MAINBUSCF_BASE_DEFAULT || base != mb->mb_iobase 150 || size != MAINBUSCF_SIZE_DEFAULT || size != mb->mb_iosize 151 || dack != MAINBUSCF_DACK_DEFAULT || dack != mb->mb_drq 152 || irq != MAINBUSCF_IRQ_DEFAULT || irq != mb->mb_irq 153 || intrbase != MAINBUSCF_INTRBASE_DEFAULT 154 || intrbase != mb->mb_intrbase) 155 return 0; 156 157 if (!CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid)) 158 return 0; 159 160 if (armreg_cbar_read() == 0) 161 return 0; 162 163 if (armperiph_find_config() == NULL) 164 return 0; 165 166 return 1; 167 } 168 169 static void 170 armperiph_attach(device_t parent, device_t self, void *aux) 171 { 172 struct armperiph_softc * const sc = device_private(self); 173 struct mainbus_attach_args * const mb = aux; 174 bus_addr_t cbar = armreg_cbar_read(); 175 const struct mpcore_config * const cfg = armperiph_find_config(); 176 prop_dictionary_t prop = device_properties(self); 177 uint32_t cbar_override; 178 179 if (prop_dictionary_get_uint32(prop, "cbar", &cbar_override)) 180 cbar = (bus_addr_t)cbar_override; 181 182 /* 183 * The normal mainbus bus space will not work for us so the port's 184 * device_register must have replaced it with one that will work. 185 */ 186 sc->sc_dev = self; 187 sc->sc_memt = mb->mb_iot; 188 189 int error = bus_space_map(sc->sc_memt, cbar, cfg->cfg_cbar_size, 0, 190 &sc->sc_memh); 191 if (error) { 192 aprint_normal(": error mapping registers at %#lx: %d\n", 193 cbar, error); 194 return; 195 } 196 aprint_normal("\n"); 197 198 /* 199 * Let's try to attach any children we may have. 200 */ 201 for (size_t i = 0; cfg->cfg_devices[i].pi_name[0] != 0; i++) { 202 struct mpcore_attach_args mpcaa = { 203 .mpcaa_name = cfg->cfg_devices[i].pi_name, 204 .mpcaa_memt = sc->sc_memt, 205 .mpcaa_memh = sc->sc_memh, 206 .mpcaa_off1 = cfg->cfg_devices[i].pi_off1, 207 .mpcaa_off2 = cfg->cfg_devices[i].pi_off2, 208 }; 209 if (strcmp(mpcaa.mpcaa_name, "arma9tmr") == 0) { 210 mpcaa.mpcaa_irq = IRQ_A9TMR_PPI_GTIMER; 211 } 212 if (strcmp(mpcaa.mpcaa_name, "armgtmr") == 0) { 213 mpcaa.mpcaa_irq = IRQ_GTMR_PPI_VTIMER; 214 } 215 216 config_found(self, &mpcaa, NULL); 217 } 218 attached = true; 219 } 220