1 /* $NetBSD: mainbus.c,v 1.26 2023/05/09 10:49:46 macallan Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
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. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.26 2023/05/09 10:49:46 macallan Exp $");
35
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39
40 #include <dev/pci/pcivar.h>
41 #include <dev/ofw/openfirm.h>
42
43 #include <machine/autoconf.h>
44
45 #include <powerpc/pic/picvar.h>
46
47 int mainbus_match(device_t, cfdata_t, void *);
48 void mainbus_attach(device_t, device_t, void *);
49
50 CFATTACH_DECL_NEW(mainbus, 0,
51 mainbus_match, mainbus_attach, NULL, NULL);
52
53 /*
54 * Probe for the mainbus; always succeeds.
55 */
56 int
mainbus_match(device_t parent,cfdata_t cf,void * aux)57 mainbus_match(device_t parent, cfdata_t cf, void *aux)
58 {
59 return 1;
60 }
61
62 /*
63 * Attach the mainbus.
64 */
65 void
mainbus_attach(device_t parent,device_t self,void * aux)66 mainbus_attach(device_t parent, device_t self, void *aux)
67 {
68 struct ofbus_attach_args oba;
69 struct confargs ca;
70 int node, cpus, i;
71 u_int32_t reg[4];
72 char name[32];
73
74 printf("\n");
75
76 devhandle_t selfh = device_handle(self);
77
78 cpus = OF_finddevice("/cpus");
79 /*
80 * XXX
81 * the canonical error value is -1 but I dimly remember some OF
82 * variants returning 0 here, so check for both just in case.
83 * It's not like this is a performance critical path.
84 */
85 if ((cpus != -1) && (cpus != 0)) {
86 node = OF_child(cpus);
87 while (node != 0) {
88 ca.ca_name = "cpu";
89 ca.ca_reg = reg;
90 ca.ca_nreg = OF_getprop(node, "reg", reg, sizeof(reg));
91 config_found(self, &ca, NULL,
92 CFARGS(.devhandle = devhandle_from_of(selfh,
93 node)));
94 node = OF_peer(node);
95 }
96 } else {
97 for (i = 0; i < 2; i++) {
98 ca.ca_name = "cpu";
99 ca.ca_reg = reg;
100 reg[0] = i;
101 config_found(self, &ca, NULL, CFARGS_NONE);
102 }
103 }
104
105 pic_finish_setup();
106
107 node = OF_peer(0);
108 if (node) {
109 oba.oba_busname = "ofw";
110 oba.oba_phandle = node;
111 config_found(self, &oba, NULL,
112 CFARGS(.devhandle = devhandle_from_of(selfh, node)));
113 }
114
115 for (node = OF_child(OF_finddevice("/")); node; node = OF_peer(node)) {
116 memset(name, 0, sizeof(name));
117 if (OF_getprop(node, "name", name, sizeof(name)) == -1)
118 continue;
119
120 ca.ca_name = name;
121 ca.ca_node = node;
122 ca.ca_nreg = OF_getprop(node, "reg", reg, sizeof(reg));
123 ca.ca_reg = reg;
124 config_found(self, &ca, NULL,
125 CFARGS(.devhandle = devhandle_from_of(selfh, node)));
126 }
127
128 #ifdef MAMBO
129 ca.ca_name="com";
130 config_found(self, &ca, NULL, CFARGS_NONE);
131 #endif
132
133 }
134