bootbus.c revision 1.8 1 /* $NetBSD: bootbus.c,v 1.8 2002/10/02 16:02:12 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Autoconfiguration support for Sun4d "bootbus".
41 */
42
43 #include <sys/param.h>
44 #include <sys/malloc.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47
48 #include <machine/autoconf.h>
49 #include <machine/bus.h>
50
51 #include <sparc/sparc/cpuunitvar.h>
52 #include <sparc/dev/bootbusvar.h>
53
54 #include "locators.h"
55
56 struct bootbus_softc {
57 struct device sc_dev;
58 int sc_node; /* our OBP node */
59
60 bus_space_tag_t sc_st; /* ours */
61 bus_space_tag_t sc_bustag; /* passed on to children */
62 };
63
64 static int bootbus_match(struct device *, struct cfdata *, void *);
65 static void bootbus_attach(struct device *, struct device *, void *);
66
67 CFATTACH_DECL(bootbus, sizeof(struct bootbus_softc),
68 bootbus_match, bootbus_attach, NULL, NULL);
69
70 static int bootbus_submatch(struct device *, struct cfdata *, void *);
71 static int bootbus_print(void *, const char *);
72
73 static int bootbus_setup_attach_args(struct bootbus_softc *, bus_space_tag_t,
74 int, struct bootbus_attach_args *);
75 static void bootbus_destroy_attach_args(struct bootbus_attach_args *);
76
77 static int
78 bootbus_match(struct device *parent, struct cfdata *cf, void *aux)
79 {
80 struct cpuunit_attach_args *cpua = aux;
81
82 if (strcmp(cpua->cpua_name, cf->cf_name) == 0)
83 return (1);
84
85 return (0);
86 }
87
88 static void
89 bootbus_attach(struct device *parent, struct device *self, void *aux)
90 {
91 struct bootbus_softc *sc = (void *) self;
92 struct cpuunit_attach_args *cpua = aux;
93 int node, error;
94
95 sc->sc_node = cpua->cpua_node;
96 sc->sc_st = cpua->cpua_bustag;
97
98 printf("\n");
99
100 /*
101 * Initialize the bus space tag we pass on to our children.
102 */
103 sc->sc_bustag = malloc(sizeof(*sc->sc_bustag), M_DEVBUF,
104 M_WAITOK|M_ZERO);
105 sc->sc_bustag->cookie = sc;
106 sc->sc_bustag->parent = sc->sc_st;
107 sc->sc_bustag->sparc_bus_map = sc->sc_st->sparc_bus_map;
108 sc->sc_bustag->sparc_bus_mmap = sc->sc_st->sparc_bus_mmap;
109
110 /*
111 * Collect address translations from the OBP.
112 */
113 error = PROM_getprop(sc->sc_node, "ranges",
114 sizeof(struct openprom_range), &sc->sc_bustag->nranges,
115 (void **) &sc->sc_bustag->ranges);
116 if (error) {
117 printf("%s: error %d getting \"ranges\" property\n",
118 sc->sc_dev.dv_xname, error);
119 panic("bootbus_attach");
120 }
121
122 /* Attach the CPU (and possibly bootbus) child nodes. */
123 for (node = firstchild(sc->sc_node); node != 0;
124 node = nextsibling(node)) {
125 struct bootbus_attach_args baa;
126
127 if (bootbus_setup_attach_args(sc, sc->sc_bustag, node, &baa))
128 panic("bootbus_attach: failed to set up attach args");
129
130 (void) config_found_sm(&sc->sc_dev, &baa, bootbus_print,
131 bootbus_submatch);
132
133 bootbus_destroy_attach_args(&baa);
134 }
135 }
136
137 static int
138 bootbus_submatch(struct device *parent, struct cfdata *cf, void *aux)
139 {
140 struct bootbus_attach_args *baa = aux;
141
142 if (cf->cf_loc[BOOTBUSCF_SLOT] != BOOTBUSCF_SLOT_DEFAULT &&
143 cf->cf_loc[BOOTBUSCF_SLOT] != baa->ba_slot)
144 return (0);
145
146 if (cf->cf_loc[BOOTBUSCF_OFFSET] != BOOTBUSCF_OFFSET_DEFAULT &&
147 cf->cf_loc[BOOTBUSCF_OFFSET] != baa->ba_offset)
148 return (0);
149
150 return (config_match(parent, cf, aux));
151 }
152
153 static int
154 bootbus_print(void *aux, const char *pnp)
155 {
156 struct bootbus_attach_args *baa = aux;
157 int i;
158
159 if (pnp)
160 printf("%s at %s", baa->ba_name, pnp);
161 printf(" slot %d offset 0x%x", baa->ba_slot, baa->ba_offset);
162 for (i = 0; i < baa->ba_nintr; i++)
163 printf(" ipl %d", baa->ba_intr[i].oi_pri);
164
165 return (UNCONF);
166 }
167
168 static int
169 bootbus_setup_attach_args(struct bootbus_softc *sc, bus_space_tag_t bustag,
170 int node, struct bootbus_attach_args *baa)
171 {
172 int n, error;
173
174 memset(baa, 0, sizeof(*baa));
175
176 error = PROM_getprop(node, "name", 1, &n, (void **) &baa->ba_name);
177 if (error)
178 return (error);
179 baa->ba_name[n] = '\0';
180
181 baa->ba_bustag = bustag;
182 baa->ba_node = node;
183
184 error = PROM_getprop(node, "reg", sizeof(struct openprom_addr),
185 &baa->ba_nreg, (void **) &baa->ba_reg);
186 if (error) {
187 bootbus_destroy_attach_args(baa);
188 return (error);
189 }
190
191 error = PROM_getprop(node, "intr", sizeof(struct openprom_intr),
192 &baa->ba_nintr, (void **) &baa->ba_intr);
193 if (error != 0 && error != ENOENT) {
194 bootbus_destroy_attach_args(baa);
195 return (error);
196 }
197
198 error = PROM_getprop(node, "address", sizeof(uint32_t),
199 &baa->ba_npromvaddrs, (void **) &baa->ba_promvaddrs);
200 if (error != 0 && error != ENOENT) {
201 bootbus_destroy_attach_args(baa);
202 return (error);
203 }
204
205 return (0);
206 }
207
208 static void
209 bootbus_destroy_attach_args(struct bootbus_attach_args *baa)
210 {
211
212 if (baa->ba_name != NULL)
213 free(baa->ba_name, M_DEVBUF);
214
215 if (baa->ba_reg != NULL)
216 free(baa->ba_reg, M_DEVBUF);
217
218 if (baa->ba_intr != NULL)
219 free(baa->ba_intr, M_DEVBUF);
220
221 if (baa->ba_promvaddrs != NULL)
222 free(baa->ba_promvaddrs, M_DEVBUF);
223 }
224