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