1 /*-
2 * Copyright (c) 2003 Jake Burkholder.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/openfirm.h>
38
39 #include <machine/bus.h>
40 #include <machine/nexusvar.h>
41 #include <machine/ofw_upa.h>
42 #include <machine/resource.h>
43
44 #include <sys/rman.h>
45
46 #include <sparc64/sbus/ofw_sbus.h>
47 #include <sparc64/sbus/sbusreg.h>
48
49 struct central_devinfo {
50 char *cdi_compat;
51 char *cdi_model;
52 char *cdi_name;
53 char *cdi_type;
54 phandle_t cdi_node;
55 };
56
57 struct central_softc {
58 phandle_t sc_node;
59 int sc_nrange;
60 struct sbus_ranges *sc_ranges;
61 };
62
63 static int central_probe(device_t dev);
64 static int central_attach(device_t dev);
65
66 static void central_probe_nomatch(device_t dev, device_t child);
67 static struct resource *central_alloc_resource(device_t, device_t, int, int *,
68 u_long, u_long, u_long, u_int);
69 static ofw_bus_get_compat_t central_get_compat;
70 static ofw_bus_get_model_t central_get_model;
71 static ofw_bus_get_name_t central_get_name;
72 static ofw_bus_get_node_t central_get_node;
73 static ofw_bus_get_type_t central_get_type;
74
75 static device_method_t central_methods[] = {
76 /* Device interface. */
77 DEVMETHOD(device_probe, central_probe),
78 DEVMETHOD(device_attach, central_attach),
79
80 /* Bus interface. */
81 DEVMETHOD(bus_print_child, bus_generic_print_child),
82 DEVMETHOD(bus_probe_nomatch, central_probe_nomatch),
83 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
84 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
85 DEVMETHOD(bus_alloc_resource, central_alloc_resource),
86 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
87 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
88 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
89
90 /* ofw_bus interface */
91 DEVMETHOD(ofw_bus_get_compat, central_get_compat),
92 DEVMETHOD(ofw_bus_get_model, central_get_model),
93 DEVMETHOD(ofw_bus_get_name, central_get_name),
94 DEVMETHOD(ofw_bus_get_node, central_get_node),
95 DEVMETHOD(ofw_bus_get_type, central_get_type),
96
97 { NULL, NULL }
98 };
99
100 static driver_t central_driver = {
101 "central",
102 central_methods,
103 sizeof(struct central_softc),
104 };
105
106 static devclass_t central_devclass;
107
108 DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0);
109
110 static int
111 central_probe(device_t dev)
112 {
113
114 if (strcmp(nexus_get_name(dev), "central") == 0) {
115 device_set_desc(dev, "central");
116 return (0);
117 }
118 return (ENXIO);
119 }
120
121 static int
122 central_attach(device_t dev)
123 {
124 struct central_devinfo *cdi;
125 struct central_softc *sc;
126 phandle_t child;
127 phandle_t node;
128 device_t cdev;
129 char *name;
130
131 sc = device_get_softc(dev);
132 node = nexus_get_node(dev);
133 sc->sc_node = node;
134
135 sc->sc_nrange = OF_getprop_alloc(node, "ranges",
136 sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges);
137 if (sc->sc_nrange == -1) {
138 device_printf(dev, "can't get ranges");
139 return (ENXIO);
140 }
141
142 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
143 if ((OF_getprop_alloc(child, "name", 1, (void **)&name)) == -1)
144 continue;
145 cdev = device_add_child(dev, NULL, -1);
146 if (cdev != NULL) {
147 cdi = malloc(sizeof(*cdi), M_DEVBUF,
148 M_WAITOK | M_ZERO);
149 cdi->cdi_name = name;
150 cdi->cdi_node = child;
151 OF_getprop_alloc(child, "compatible", 1,
152 (void **)&cdi->cdi_compat);
153 OF_getprop_alloc(child, "device_type", 1,
154 (void **)&cdi->cdi_type);
155 OF_getprop_alloc(child, "model", 1,
156 (void **)&cdi->cdi_model);
157 device_set_ivars(cdev, cdi);
158 } else
159 free(name, M_OFWPROP);
160 }
161
162 return (bus_generic_attach(dev));
163 }
164
165 static void
166 central_probe_nomatch(device_t dev, device_t child)
167 {
168 struct central_devinfo *cdi;
169
170 cdi = device_get_ivars(child);
171 device_printf(dev, "<%s> type %s (no driver attached)\n",
172 cdi->cdi_name, cdi->cdi_type != NULL ? cdi->cdi_type : "unknown");
173 }
174
175 static struct resource *
176 central_alloc_resource(device_t bus, device_t child, int type, int *rid,
177 u_long start, u_long end, u_long count, u_int flags)
178 {
179 struct central_softc *sc;
180 struct resource *res;
181 bus_addr_t coffset;
182 bus_addr_t cend;
183 bus_addr_t phys;
184 int i;
185
186 if (type != SYS_RES_MEMORY) {
187 return (bus_generic_alloc_resource(bus, child, type, rid,
188 start, end, count, flags));
189 }
190 res = NULL;
191 sc = device_get_softc(bus);
192 for (i = 0; i < sc->sc_nrange; i++) {
193 coffset = sc->sc_ranges[i].coffset;
194 cend = coffset + sc->sc_ranges[i].size - 1;
195 if (start >= coffset && end <= cend) {
196 start -= coffset;
197 end -= coffset;
198 phys = sc->sc_ranges[i].poffset |
199 ((bus_addr_t)sc->sc_ranges[i].pspace << 32);
200 res = bus_generic_alloc_resource(bus, child, type,
201 rid, phys + start, phys + end, count, flags);
202 break;
203 }
204 }
205 return (res);
206 }
207
208 static const char *
209 central_get_compat(device_t bus, device_t dev)
210 {
211 struct central_devinfo *dinfo;
212
213 dinfo = device_get_ivars(dev);
214 return (dinfo->cdi_compat);
215 }
216
217 static const char *
218 central_get_model(device_t bus, device_t dev)
219 {
220 struct central_devinfo *dinfo;
221
222 dinfo = device_get_ivars(dev);
223 return (dinfo->cdi_model);
224 }
225
226 static const char *
227 central_get_name(device_t bus, device_t dev)
228 {
229 struct central_devinfo *dinfo;
230
231 dinfo = device_get_ivars(dev);
232 return (dinfo->cdi_name);
233 }
234
235 static phandle_t
236 central_get_node(device_t bus, device_t dev)
237 {
238 struct central_devinfo *dinfo;
239
240 dinfo = device_get_ivars(dev);
241 return (dinfo->cdi_node);
242 }
243
244 static const char *
245 central_get_type(device_t bus, device_t dev)
246 {
247 struct central_devinfo *dinfo;
248
249 dinfo = device_get_ivars(dev);
250 return (dinfo->cdi_type);
251 }
Cache object: 3b6c5b47a96b288c56a67170cd5bcb16
|