ofbus.c revision 1.29 1 /* $NetBSD: ofbus.c,v 1.29 2021/04/30 02:34:12 thorpej Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ofbus.c,v 1.29 2021/04/30 02:34:12 thorpej Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40
41 #include <dev/ofw/openfirm.h>
42
43 int ofbus_match(device_t, cfdata_t, void *);
44 void ofbus_attach(device_t, device_t, void *);
45 static int ofbus_print(void *, const char *);
46
47 CFATTACH_DECL_NEW(ofbus, 0,
48 ofbus_match, ofbus_attach, NULL, NULL);
49
50 static int
51 ofbus_print(void *aux, const char *pnp)
52 {
53 struct ofbus_attach_args *oba = aux;
54
55 if (pnp)
56 aprint_normal("%s at %s", oba->oba_ofname, pnp);
57 else
58 aprint_normal(" (%s)", oba->oba_ofname);
59 return UNCONF;
60 }
61
62 int
63 ofbus_match(device_t parent, cfdata_t cf, void *aux)
64 {
65 struct ofbus_attach_args *oba = aux;
66
67 if (strcmp(oba->oba_busname, "ofw"))
68 return (0);
69 if (!OF_child(oba->oba_phandle))
70 return (0);
71 return (1);
72 }
73
74 #if defined(__arm32__) /* XXX temporary */
75 #define _OFBUS_ROOT_MACHDEP_SKIPNAMES \
76 "udp", \
77 "cpus", \
78 "mmu", \
79 "memory"
80 #endif /* __arm32__ */
81
82 /*
83 * Skip some well-known nodes in the root that contain no useful
84 * child devices.
85 */
86 static bool
87 ofbus_skip_node_in_root(int phandle, char *name, size_t namesize)
88 {
89 static const char * const skip_names[] = {
90 "aliases",
91 "options",
92 "openprom",
93 "chosen",
94 "packages",
95 #ifdef _OFBUS_ROOT_MACHDEP_SKIPNAMES
96 _OFBUS_ROOT_MACHDEP_SKIPNAMES
97 #endif
98 };
99 u_int i;
100
101 name[0] = '\0';
102 if (OF_getprop(phandle, "name", name, namesize) <= 0) {
103 return false;
104 }
105
106 for (i = 0; i < __arraycount(skip_names); i++) {
107 if (strcmp(name, skip_names[i]) == 0) {
108 return true;
109 }
110 }
111 return false;
112 }
113
114 void
115 ofbus_attach(device_t parent, device_t dev, void *aux)
116 {
117 struct ofbus_attach_args *oba = aux;
118 struct ofbus_attach_args oba2;
119 char name[64], type[64];
120 int child, units;
121 bool rootbus;
122
123 rootbus = oba->oba_phandle == OF_finddevice("/");
124
125 /*
126 * If we are the OFW root, get the banner-name and model
127 * properties and display them for informational purposes.
128 */
129 if (rootbus) {
130 int model_len, banner_len;
131
132 model_len = OF_getprop(oba->oba_phandle, "model",
133 name, sizeof(name));
134 banner_len = OF_getprop(oba->oba_phandle, "banner-name",
135 type, sizeof(type));
136
137 if (banner_len > 0 && model_len > 0) {
138 printf(": %s (%s)\n", type, name);
139 } else if (model_len > 0) {
140 printf(": %s\n", name);
141 } else {
142 printf("\n");
143 }
144 } else {
145 printf("\n");
146 }
147
148 /*
149 * This is a hack to make the probe work on the scsi (and ide) bus.
150 * YES, I THINK IT IS A BUG IN THE OPENFIRMWARE TO NOT PROBE ALL
151 * DEVICES ON THESE BUSSES.
152 */
153 units = 1;
154 name[0] = 0;
155 if (OF_getprop(oba->oba_phandle, "name", name, sizeof name) > 0) {
156 if (!strcmp(name, "scsi"))
157 units = 7; /* What about wide or hostid != 7? XXX */
158 else if (!strcmp(name, "ide"))
159 units = 2;
160 }
161
162 /* attach displays first */
163 for (child = OF_child(oba->oba_phandle); child != 0;
164 child = OF_peer(child)) {
165 oba2.oba_busname = "ofw";
166 type[0] = 0;
167 if (OF_getprop(child, "device_type", type, sizeof(type)) <= 0)
168 continue;
169 if (strncmp(type, "display", sizeof(type)) != 0)
170 continue;
171 of_packagename(child, name, sizeof name);
172 oba2.oba_phandle = child;
173 for (oba2.oba_unit = 0; oba2.oba_unit < units;
174 oba2.oba_unit++) {
175 if (units > 1) {
176 snprintf(oba2.oba_ofname,
177 sizeof(oba2.oba_ofname), "%s@%d", name,
178 oba2.oba_unit);
179 } else {
180 strlcpy(oba2.oba_ofname, name,
181 sizeof(oba2.oba_ofname));
182 }
183 config_found(dev, &oba2, ofbus_print,
184 CFARG_DEVHANDLE, devhandle_from_of(child),
185 CFARG_EOL);
186 }
187 }
188
189 /* now the rest */
190 for (child = OF_child(oba->oba_phandle); child != 0;
191 child = OF_peer(child)) {
192 oba2.oba_busname = "ofw";
193 type[0] = 0;
194 if (OF_getprop(child, "device_type", type, sizeof(type)) > 0) {
195 if (strncmp(type, "display", sizeof(type)) == 0)
196 continue;
197 }
198 if (rootbus &&
199 ofbus_skip_node_in_root(child, name, sizeof(name))) {
200 continue;
201 }
202 of_packagename(child, name, sizeof name);
203 oba2.oba_phandle = child;
204 for (oba2.oba_unit = 0; oba2.oba_unit < units;
205 oba2.oba_unit++) {
206 if (units > 1) {
207 snprintf(oba2.oba_ofname,
208 sizeof(oba2.oba_ofname), "%s@%d", name,
209 oba2.oba_unit);
210 } else {
211 strlcpy(oba2.oba_ofname, name,
212 sizeof(oba2.oba_ofname));
213 }
214 config_found(dev, &oba2, ofbus_print,
215 CFARG_DEVHANDLE, devhandle_from_of(child),
216 CFARG_EOL);
217 }
218 }
219 }
220