1 /* $NetBSD: imx31_ahb.c,v 1.8 2021/04/24 23:36:27 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 2002, 2005 Genetec Corporation. All rights reserved. 5 * Written by Hiroyuki Bessho for Genetec Corporation. 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the NetBSD Project by 18 * Genetec Corporation. 19 * 4. The name of Genetec Corporation may not be used to endorse or 20 * promote products derived from this software without specific prior 21 * written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION 27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * Autoconfiguration support for the Intel PXA2[15]0 application 37 * processor. This code is derived from arm/sa11x0/sa11x0.c 38 */ 39 40 /*- 41 * Copyright (c) 2001, The NetBSD Foundation, Inc. All rights reserved. 42 * 43 * This code is derived from software contributed to The NetBSD Foundation 44 * by IWAMOTO Toshihiro and Ichiro FUKUHARA. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 56 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 59 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 65 * POSSIBILITY OF SUCH DAMAGE. 66 */ 67 /*- 68 * Copyright (c) 1999 69 * Shin Takemura and PocketBSD Project. All rights reserved. 70 * 71 * Redistribution and use in source and binary forms, with or without 72 * modification, are permitted provided that the following conditions 73 * are met: 74 * 1. Redistributions of source code must retain the above copyright 75 * notice, this list of conditions and the following disclaimer. 76 * 2. Redistributions in binary form must reproduce the above copyright 77 * notice, this list of conditions and the following disclaimer in the 78 * documentation and/or other materials provided with the distribution. 79 * 3. All advertising materials mentioning features or use of this software 80 * must display the following acknowledgement: 81 * This product includes software developed by the PocketBSD project 82 * and its contributors. 83 * 4. Neither the name of the project nor the names of its contributors 84 * may be used to endorse or promote products derived from this software 85 * without specific prior written permission. 86 * 87 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 88 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 90 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 93 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 94 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 95 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 96 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 97 * SUCH DAMAGE. 98 * 99 */ 100 101 #include <sys/cdefs.h> 102 __KERNEL_RCSID(0, "$Id: imx31_ahb.c,v 1.8 2021/04/24 23:36:27 thorpej Exp $"); 103 104 #include "locators.h" 105 #include "avic.h" 106 107 #include <sys/param.h> 108 #include <sys/systm.h> 109 #include <sys/device.h> 110 #include <sys/kernel.h> 111 #include <sys/reboot.h> 112 113 #include <machine/cpu.h> 114 #include <sys/bus.h> 115 116 #include <arm/cpufunc.h> 117 #include <arm/mainbus/mainbus.h> 118 119 #include <machine/intr.h> 120 121 #include <arm/imx/imx31reg.h> 122 #include <arm/imx/imx31var.h> 123 124 struct ahb_softc { 125 device_t sc_dev; 126 bus_dma_tag_t sc_dmat; 127 bus_space_tag_t sc_memt; 128 bus_space_handle_t sc_memh; 129 }; 130 131 /* prototypes */ 132 static int ahb_match(device_t, cfdata_t, void *); 133 static void ahb_attach(device_t, device_t, void *); 134 static int ahb_search(device_t, cfdata_t, const int *, void *); 135 static void ahb_attach_critical(struct ahb_softc *); 136 static int ahbbus_print(void *, const char *); 137 138 /* attach structures */ 139 CFATTACH_DECL_NEW(ahb, sizeof(struct ahb_softc), 140 ahb_match, ahb_attach, NULL, NULL); 141 142 static struct ahb_softc *ahb_sc; 143 144 static int 145 ahb_match(device_t parent, cfdata_t match, void *aux) 146 { 147 return 1; 148 } 149 150 static void 151 ahb_attach(device_t parent, device_t self, void *aux) 152 { 153 struct ahb_softc *sc = device_private(self); 154 struct ahb_attach_args ahba; 155 156 ahb_sc = sc; 157 sc->sc_dev = self; 158 sc->sc_memt = &imx_bs_tag; 159 #if NBUS_DMA_GENERIC > 0 160 sc->sc_dmat = &imx_bus_dma_tag; 161 #else 162 sc->sc_dmat = 0; 163 #endif 164 165 aprint_normal(": AHB-Lite 2.v6 bus interface\n"); 166 167 /* 168 * Attach critical devices 169 */ 170 ahb_attach_critical(sc); 171 172 /* 173 * Attach all other devices 174 */ 175 ahba.ahba_name = "ahb"; 176 ahba.ahba_memt = sc->sc_memt; 177 ahba.ahba_dmat = sc->sc_dmat; 178 config_search(self, &ahba, 179 CFARG_SEARCH, ahb_search, 180 CFARG_EOL); 181 } 182 183 static int 184 ahb_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 185 { 186 struct ahb_attach_args * const ahba = aux; 187 188 ahba->ahba_addr = cf->cf_loc[AHBCF_ADDR]; 189 ahba->ahba_size = cf->cf_loc[AHBCF_SIZE]; 190 ahba->ahba_intr = cf->cf_loc[AHBCF_INTR]; 191 ahba->ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE]; 192 193 if (config_probe(parent, cf, ahba)) 194 config_attach(parent, cf, ahba, ahbbus_print, CFARG_EOL); 195 196 return 0; 197 } 198 199 static int 200 ahb_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 201 { 202 struct ahb_attach_args * const ahba = aux; 203 struct ahb_attach_args ahba0; 204 205 if (strcmp(ahba->ahba_name, "ahb")) 206 return 0; 207 if (ahba->ahba_addr != AHBCF_ADDR_DEFAULT 208 && ahba->ahba_addr != cf->cf_loc[AHBCF_ADDR]) 209 return 0; 210 if (ahba->ahba_size != AHBCF_SIZE_DEFAULT 211 && ahba->ahba_size != cf->cf_loc[AHBCF_SIZE]) 212 return 0; 213 if (ahba->ahba_intr != AHBCF_INTR_DEFAULT 214 && ahba->ahba_intr != cf->cf_loc[AHBCF_INTR]) 215 return 0; 216 if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT 217 && ahba->ahba_irqbase != cf->cf_loc[AHBCF_IRQBASE]) 218 return 0; 219 220 ahba0 = *ahba; 221 ahba0.ahba_addr = cf->cf_loc[AHBCF_ADDR]; 222 ahba0.ahba_size = cf->cf_loc[AHBCF_SIZE]; 223 ahba0.ahba_intr = cf->cf_loc[AHBCF_INTR]; 224 ahba0.ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE]; 225 226 return config_match(parent, cf, &ahba0); 227 } 228 229 #if NAVIC == 0 230 #error no avic present in config file 231 #endif 232 233 static const struct { 234 const char *name; 235 bus_addr_t addr; 236 bool required; 237 } critical_devs[] = { 238 { .name = "avic", .addr = INTC_BASE, .required = true }, 239 { .name = "gpio1", .addr = GPIO1_BASE, .required = false }, 240 { .name = "gpio2", .addr = GPIO2_BASE, .required = false }, 241 { .name = "gpio3", .addr = GPIO3_BASE, .required = false }, 242 #if 0 243 { .name = "dmac", .addr = DMAC_BASE, .required = true }, 244 #endif 245 }; 246 247 static void 248 ahb_attach_critical(struct ahb_softc *sc) 249 { 250 struct ahb_attach_args ahba; 251 cfdata_t cf; 252 size_t i; 253 254 for (i = 0; i < __arraycount(critical_devs); i++) { 255 ahba.ahba_name = "ahb"; 256 ahba.ahba_memt = sc->sc_memt; 257 ahba.ahba_dmat = sc->sc_dmat; 258 259 ahba.ahba_addr = critical_devs[i].addr; 260 ahba.ahba_size = AHBCF_SIZE_DEFAULT; 261 ahba.ahba_intr = AHBCF_INTR_DEFAULT; 262 ahba.ahba_irqbase = AHBCF_IRQBASE_DEFAULT; 263 264 cf = config_search(sc->sc_dev, &ahba, 265 CFARG_SUBMATCH, ahb_find, 266 CFARG_EOL); 267 if (cf == NULL && critical_devs[i].required) 268 panic("ahb_attach_critical: failed to find %s!", 269 critical_devs[i].name); 270 271 ahba.ahba_addr = cf->cf_loc[AHBCF_ADDR]; 272 ahba.ahba_size = cf->cf_loc[AHBCF_SIZE]; 273 ahba.ahba_intr = cf->cf_loc[AHBCF_INTR]; 274 ahba.ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE]; 275 config_attach(sc->sc_dev, cf, &ahba, ahbbus_print, CFARG_EOL); 276 } 277 } 278 279 static int 280 ahbbus_print(void *aux, const char *name) 281 { 282 struct ahb_attach_args *ahba = aux; 283 284 if (ahba->ahba_addr != AHBCF_ADDR_DEFAULT) { 285 aprint_normal(" addr 0x%lx", ahba->ahba_addr); 286 if (ahba->ahba_size > AHBCF_SIZE_DEFAULT) 287 aprint_normal("-0x%lx", 288 ahba->ahba_addr + ahba->ahba_size-1); 289 } 290 if (ahba->ahba_intr != AHBCF_INTR_DEFAULT) 291 aprint_normal(" intr %d", ahba->ahba_intr); 292 if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT) 293 aprint_normal(" irqbase %d", ahba->ahba_irqbase); 294 295 return (UNCONF); 296 } 297 298 static inline uint32_t 299 read_clock_counter_xsc1(void) 300 { 301 uint32_t x; 302 __asm volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (x) ); 303 304 return x; 305 } 306 307 static inline uint32_t 308 read_clock_counter_xsc2(void) 309 { 310 uint32_t x; 311 __asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (x) ); 312 313 return x; 314 } 315 316