1 /* $NetBSD: uni-n.c,v 1.13 2022/12/28 07:18:29 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.13 2022/12/28 07:18:29 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 static const char *skiplist[] = {
67 "openpic",
68 "chrp,open-pic",
69 "open-pic",
70 "mpic",
71 "dart",
72 "u3-dart",
73 "u4-dart",
74 NULL
75 };
76
77 int
uni_n_match(device_t parent,cfdata_t cf,void * aux)78 uni_n_match(device_t parent, cfdata_t cf, void *aux)
79 {
80 struct confargs *ca = aux;
81 char compat[32];
82 if ((strcmp(ca->ca_name, "uni-n") != 0) &&
83 (strcmp(ca->ca_name, "u4") != 0) &&
84 (strcmp(ca->ca_name, "u3") != 0))
85 return 0;
86
87 memset(compat, 0, sizeof(compat));
88 #if 0
89 OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
90 if (strcmp(compat, "uni-north") != 0)
91 return 0;
92 #endif
93 return 1;
94 }
95
96 /*
97 * Attach all the sub-devices we can find
98 */
99 void
uni_n_attach(device_t parent,device_t self,void * aux)100 uni_n_attach(device_t parent, device_t self, void *aux)
101 {
102 struct uni_n_softc *sc = device_private(self);
103 struct confargs *our_ca = aux;
104 struct confargs ca;
105 int node, child, namelen;
106 #if NFCU > 0
107 int cpuid;
108 #endif
109 u_int reg[20];
110 int intr[6];
111 char name[32];
112
113 sc->sc_dev = self;
114 node = our_ca->ca_node;
115 sc->sc_node = node;
116 printf(" address 0x%08x\n",
117 our_ca->ca_reg[our_ca->ca_nreg > 8 ? 1 : 0]);
118
119 #if NFCU > 0
120 /*
121 * zero out eeprom blocks, then see if we have valid data
122 * doing this here because the EEPROMs are dangling from our i2c bus
123 * but we can get all the data just from looking at the properties
124 */
125 memset(eeprom, 0, sizeof(eeprom));
126 cpuid = OF_finddevice("/u3/i2c/cpuid@a0");
127 OF_getprop(cpuid, "cpuid", eeprom[0], sizeof(eeprom[0]));
128 if (eeprom[0][1] != 0)
129 aprint_normal_dev(self, "found EEPROM data for CPU 0\n");
130
131 cpuid = OF_finddevice("/u3/i2c/cpuid@a2");
132 OF_getprop(cpuid, "cpuid", eeprom[1], sizeof(eeprom[1]));
133 if (eeprom[1][1] != 0)
134 aprint_normal_dev(self, "found EEPROM data for CPU 1\n");
135 #endif
136
137 memset(&sc->sc_memt, 0, sizeof(struct powerpc_bus_space));
138 sc->sc_memt.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE;
139 if (ofwoea_map_space(RANGE_TYPE_MACIO, RANGE_MEM, node, &sc->sc_memt,
140 "uni-n mem-space") != 0) {
141 panic("Can't init uni-n mem tag");
142 }
143
144 devhandle_t selfh = device_handle(self);
145 for (child = OF_child(node); child; child = OF_peer(child)) {
146 if (of_compatible(child, skiplist)) continue;
147 namelen = OF_getprop(child, "name", name, sizeof(name));
148 if (namelen < 0)
149 continue;
150 if (namelen >= sizeof(name))
151 continue;
152
153 name[namelen] = 0;
154 ca.ca_name = name;
155 ca.ca_node = child;
156 ca.ca_tag = &sc->sc_memt;
157 ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
158 ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
159 sizeof(intr));
160 if (ca.ca_nintr == -1)
161 ca.ca_nintr = OF_getprop(child, "interrupts", intr,
162 sizeof(intr));
163
164 ca.ca_reg = reg;
165 ca.ca_intr = intr;
166 config_found(self, &ca, uni_n_print,
167 CFARGS(.devhandle = devhandle_from_of(selfh, child)));
168 }
169 }
170
171 int
uni_n_print(void * aux,const char * uni_n)172 uni_n_print(void *aux, const char *uni_n)
173 {
174 struct confargs *ca = aux;
175 if (uni_n)
176 aprint_normal("%s at %s", ca->ca_name, uni_n);
177
178 if (ca->ca_nreg > 0)
179 aprint_normal(" address 0x%x", ca->ca_reg[0]);
180
181 return UNCONF;
182 }
183
184 #if NFCU > 0
185 int
get_cpuid(int cpu,uint8_t * buf)186 get_cpuid(int cpu, uint8_t *buf)
187 {
188 if ((cpu < 0) || (cpu > 1)) return -1;
189 if (eeprom[cpu][1] == 0) return 0;
190 memcpy(buf, eeprom[cpu], sizeof(eeprom[cpu]));
191 return sizeof(eeprom[cpu]);
192 }
193 #endif
194