1 1.13 macallan /* $NetBSD: uni-n.c,v 1.13 2022/12/28 07:18:29 macallan Exp $ */ 2 1.1 macallan 3 1.1 macallan /*- 4 1.1 macallan * Copyright (C) 2005 Michael Lorenz. 5 1.1 macallan * 6 1.1 macallan * Redistribution and use in source and binary forms, with or without 7 1.1 macallan * modification, are permitted provided that the following conditions 8 1.1 macallan * are met: 9 1.1 macallan * 1. Redistributions of source code must retain the above copyright 10 1.1 macallan * notice, this list of conditions and the following disclaimer. 11 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 macallan * notice, this list of conditions and the following disclaimer in the 13 1.1 macallan * documentation and/or other materials provided with the distribution. 14 1.1 macallan * 3. The name of the author may not be used to endorse or promote products 15 1.1 macallan * derived from this software without specific prior written permission. 16 1.1 macallan * 17 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 1.1 macallan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 1.1 macallan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 1.1 macallan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 1.1 macallan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 1.1 macallan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 1.1 macallan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 1.1 macallan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 1.1 macallan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 1.1 macallan * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 1.1 macallan */ 28 1.1 macallan 29 1.1 macallan /* 30 1.1 macallan * a driver to match the uni_n node in OF's device tree and attach its children 31 1.1 macallan */ 32 1.1 macallan 33 1.1 macallan #include <sys/cdefs.h> 34 1.13 macallan __KERNEL_RCSID(0, "$NetBSD: uni-n.c,v 1.13 2022/12/28 07:18:29 macallan Exp $"); 35 1.1 macallan 36 1.1 macallan #include <sys/param.h> 37 1.1 macallan #include <sys/systm.h> 38 1.1 macallan #include <sys/kernel.h> 39 1.1 macallan #include <sys/device.h> 40 1.1 macallan 41 1.1 macallan #include <dev/ofw/openfirm.h> 42 1.7 macallan #include <dev/ofw/ofw_pci.h> 43 1.1 macallan 44 1.1 macallan #include <machine/autoconf.h> 45 1.1 macallan 46 1.9 macallan #include "fcu.h" 47 1.9 macallan 48 1.5 matt static void uni_n_attach(device_t, device_t, void *); 49 1.5 matt static int uni_n_match(device_t, cfdata_t, void *); 50 1.1 macallan static int uni_n_print(void *, const char *); 51 1.1 macallan 52 1.1 macallan struct uni_n_softc { 53 1.6 macallan device_t sc_dev; 54 1.7 macallan struct powerpc_bus_space sc_memt; 55 1.1 macallan int sc_node; 56 1.1 macallan }; 57 1.1 macallan 58 1.6 macallan CFATTACH_DECL_NEW(uni_n, sizeof(struct uni_n_softc), 59 1.1 macallan uni_n_match, uni_n_attach, NULL, NULL); 60 1.1 macallan 61 1.9 macallan #if NFCU > 0 62 1.9 macallan /* storage for CPUID SEEPROM contents found on some G5 */ 63 1.9 macallan static uint8_t eeprom[2][160]; 64 1.9 macallan #endif 65 1.9 macallan 66 1.13 macallan static const char *skiplist[] = { 67 1.13 macallan "openpic", 68 1.13 macallan "chrp,open-pic", 69 1.13 macallan "open-pic", 70 1.13 macallan "mpic", 71 1.13 macallan "dart", 72 1.13 macallan "u3-dart", 73 1.13 macallan "u4-dart", 74 1.13 macallan NULL 75 1.13 macallan }; 76 1.13 macallan 77 1.1 macallan int 78 1.5 matt uni_n_match(device_t parent, cfdata_t cf, void *aux) 79 1.1 macallan { 80 1.1 macallan struct confargs *ca = aux; 81 1.1 macallan char compat[32]; 82 1.7 macallan if ((strcmp(ca->ca_name, "uni-n") != 0) && 83 1.8 macallan (strcmp(ca->ca_name, "u4") != 0) && 84 1.8 macallan (strcmp(ca->ca_name, "u3") != 0)) 85 1.1 macallan return 0; 86 1.1 macallan 87 1.1 macallan memset(compat, 0, sizeof(compat)); 88 1.7 macallan #if 0 89 1.1 macallan OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat)); 90 1.1 macallan if (strcmp(compat, "uni-north") != 0) 91 1.1 macallan return 0; 92 1.7 macallan #endif 93 1.1 macallan return 1; 94 1.1 macallan } 95 1.1 macallan 96 1.1 macallan /* 97 1.1 macallan * Attach all the sub-devices we can find 98 1.1 macallan */ 99 1.1 macallan void 100 1.5 matt uni_n_attach(device_t parent, device_t self, void *aux) 101 1.1 macallan { 102 1.5 matt struct uni_n_softc *sc = device_private(self); 103 1.1 macallan struct confargs *our_ca = aux; 104 1.1 macallan struct confargs ca; 105 1.1 macallan int node, child, namelen; 106 1.9 macallan #if NFCU > 0 107 1.9 macallan int cpuid; 108 1.9 macallan #endif 109 1.1 macallan u_int reg[20]; 110 1.1 macallan int intr[6]; 111 1.1 macallan char name[32]; 112 1.1 macallan 113 1.6 macallan sc->sc_dev = self; 114 1.7 macallan node = our_ca->ca_node; 115 1.1 macallan sc->sc_node = node; 116 1.9 macallan printf(" address 0x%08x\n", 117 1.9 macallan our_ca->ca_reg[our_ca->ca_nreg > 8 ? 1 : 0]); 118 1.9 macallan 119 1.9 macallan #if NFCU > 0 120 1.9 macallan /* 121 1.9 macallan * zero out eeprom blocks, then see if we have valid data 122 1.13 macallan * doing this here because the EEPROMs are dangling from our i2c bus 123 1.9 macallan * but we can get all the data just from looking at the properties 124 1.9 macallan */ 125 1.9 macallan memset(eeprom, 0, sizeof(eeprom)); 126 1.9 macallan cpuid = OF_finddevice("/u3/i2c/cpuid@a0"); 127 1.9 macallan OF_getprop(cpuid, "cpuid", eeprom[0], sizeof(eeprom[0])); 128 1.9 macallan if (eeprom[0][1] != 0) 129 1.9 macallan aprint_normal_dev(self, "found EEPROM data for CPU 0\n"); 130 1.13 macallan 131 1.9 macallan cpuid = OF_finddevice("/u3/i2c/cpuid@a2"); 132 1.9 macallan OF_getprop(cpuid, "cpuid", eeprom[1], sizeof(eeprom[1])); 133 1.9 macallan if (eeprom[1][1] != 0) 134 1.9 macallan aprint_normal_dev(self, "found EEPROM data for CPU 1\n"); 135 1.9 macallan #endif 136 1.7 macallan 137 1.7 macallan memset(&sc->sc_memt, 0, sizeof(struct powerpc_bus_space)); 138 1.7 macallan sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE; 139 1.7 macallan if (ofwoea_map_space(RANGE_TYPE_MACIO, RANGE_MEM, node, &sc->sc_memt, 140 1.7 macallan "uni-n mem-space") != 0) { 141 1.7 macallan panic("Can't init uni-n mem tag"); 142 1.7 macallan } 143 1.7 macallan 144 1.12 thorpej devhandle_t selfh = device_handle(self); 145 1.1 macallan for (child = OF_child(node); child; child = OF_peer(child)) { 146 1.13 macallan if (of_compatible(child, skiplist)) continue; 147 1.1 macallan namelen = OF_getprop(child, "name", name, sizeof(name)); 148 1.1 macallan if (namelen < 0) 149 1.1 macallan continue; 150 1.1 macallan if (namelen >= sizeof(name)) 151 1.1 macallan continue; 152 1.1 macallan 153 1.1 macallan name[namelen] = 0; 154 1.1 macallan ca.ca_name = name; 155 1.1 macallan ca.ca_node = child; 156 1.7 macallan ca.ca_tag = &sc->sc_memt; 157 1.1 macallan ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg)); 158 1.1 macallan ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr, 159 1.1 macallan sizeof(intr)); 160 1.1 macallan if (ca.ca_nintr == -1) 161 1.1 macallan ca.ca_nintr = OF_getprop(child, "interrupts", intr, 162 1.1 macallan sizeof(intr)); 163 1.1 macallan 164 1.1 macallan ca.ca_reg = reg; 165 1.1 macallan ca.ca_intr = intr; 166 1.10 thorpej config_found(self, &ca, uni_n_print, 167 1.12 thorpej CFARGS(.devhandle = devhandle_from_of(selfh, child))); 168 1.1 macallan } 169 1.1 macallan } 170 1.1 macallan 171 1.1 macallan int 172 1.3 dsl uni_n_print(void *aux, const char *uni_n) 173 1.1 macallan { 174 1.1 macallan struct confargs *ca = aux; 175 1.1 macallan if (uni_n) 176 1.1 macallan aprint_normal("%s at %s", ca->ca_name, uni_n); 177 1.1 macallan 178 1.1 macallan if (ca->ca_nreg > 0) 179 1.1 macallan aprint_normal(" address 0x%x", ca->ca_reg[0]); 180 1.1 macallan 181 1.1 macallan return UNCONF; 182 1.1 macallan } 183 1.9 macallan 184 1.9 macallan #if NFCU > 0 185 1.9 macallan int 186 1.9 macallan get_cpuid(int cpu, uint8_t *buf) 187 1.9 macallan { 188 1.9 macallan if ((cpu < 0) || (cpu > 1)) return -1; 189 1.9 macallan if (eeprom[cpu][1] == 0) return 0; 190 1.9 macallan memcpy(buf, eeprom[cpu], sizeof(eeprom[cpu])); 191 1.9 macallan return sizeof(eeprom[cpu]); 192 1.9 macallan } 193 1.9 macallan #endif 194