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