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