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