1 /* $NetBSD: vchiq_netbsd_fdt.c,v 1.7 2021/08/07 16:19:18 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jared D. McNeill 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 <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: vchiq_netbsd_fdt.c,v 1.7 2021/08/07 16:19:18 thorpej Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/device.h> 38 #include <sys/kernel.h> 39 #include <sys/bus.h> 40 #include <sys/sysctl.h> 41 42 #include <arm/broadcom/bcm2835reg.h> 43 #include <arm/broadcom/bcm2835_intr.h> 44 45 #include <dev/fdt/fdtvar.h> 46 47 #include "vchiq_arm.h" 48 #include "vchiq_2835.h" 49 #include "vchiq_netbsd.h" 50 51 struct vchiq_fdt_softc { 52 struct vchiq_softc sc_vchiq; 53 int sc_phandle; 54 }; 55 56 static int vchiq_fdt_match(device_t, cfdata_t, void *); 57 static void vchiq_fdt_attach(device_t, device_t, void *); 58 59 static void vchiq_fdt_defer(device_t); 60 61 /* External functions */ 62 int vchiq_init(void); 63 64 65 CFATTACH_DECL_NEW(vchiq_fdt, sizeof(struct vchiq_fdt_softc), 66 vchiq_fdt_match, vchiq_fdt_attach, NULL, NULL); 67 68 static const struct device_compatible_entry compat_data[] = { 69 { .compat = "brcm,bcm2835-vchiq" }, 70 DEVICE_COMPAT_EOL 71 }; 72 73 static int 74 vchiq_fdt_match(device_t parent, cfdata_t match, void *aux) 75 { 76 struct fdt_attach_args * const faa = aux; 77 78 return of_compatible_match(faa->faa_phandle, compat_data); 79 } 80 81 static void 82 vchiq_fdt_attach(device_t parent, device_t self, void *aux) 83 { 84 struct vchiq_fdt_softc *fsc = device_private(self); 85 struct vchiq_softc *sc = &fsc->sc_vchiq; 86 struct fdt_attach_args * const faa = aux; 87 const int phandle = faa->faa_phandle; 88 89 aprint_naive("\n"); 90 aprint_normal(": BCM2835 VCHIQ\n"); 91 92 sc->sc_dev = self; 93 sc->sc_iot = faa->faa_bst; 94 fsc->sc_phandle = phandle; 95 96 #if BYTE_ORDER == BIG_ENDIAN 97 aprint_error_dev(sc->sc_dev, "not supported yet in big-endian mode\n"); 98 return; 99 #endif 100 101 bus_addr_t addr; 102 bus_size_t size; 103 104 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 105 aprint_error_dev(sc->sc_dev, "couldn't get register address\n"); 106 return; 107 } 108 109 if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh) != 0) { 110 aprint_error_dev(sc->sc_dev, "unable to map device\n"); 111 return; 112 } 113 114 vchiq_platform_attach(faa->faa_dmat); 115 116 vchiq_set_softc(sc); 117 118 config_mountroot(self, vchiq_fdt_defer); 119 } 120 121 static void 122 vchiq_fdt_defer(device_t self) 123 { 124 struct vchiq_attach_args vaa; 125 struct vchiq_fdt_softc *fsc = device_private(self); 126 struct vchiq_softc *sc = &fsc->sc_vchiq; 127 const int phandle = fsc->sc_phandle; 128 129 vchiq_core_initialize(); 130 131 char intrstr[128]; 132 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 133 aprint_error(": failed to decode interrupt\n"); 134 return; 135 } 136 137 sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 138 FDT_INTR_MPSAFE, vchiq_intr, sc, device_xname(self)); 139 if (sc->sc_ih == NULL) { 140 aprint_error_dev(self, "failed to establish interrupt %s\n", 141 intrstr); 142 return; 143 } 144 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 145 146 vchiq_init(); 147 148 vaa.vaa_name = "AUDS"; 149 config_found(self, &vaa, vchiq_print, CFARGS_NONE); 150 } 151