1 /*-
2 * Copyright (c) 2006 Marius Strobl <marius@FreeBSD.org>
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/resource.h>
36 #include <sys/rman.h>
37
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/ofw/openfirm.h>
41
42 #include <machine/bus.h>
43 #include <machine/bus_common.h>
44 #include <machine/resource.h>
45
46 #include <sparc64/sbus/ofw_sbus.h>
47 #include <sparc64/sbus/sbusreg.h>
48 #include <sparc64/sbus/sbusvar.h>
49
50 struct lebuffer_devinfo {
51 struct ofw_bus_devinfo ldi_obdinfo;
52 struct resource_list ldi_rl;
53 };
54
55 static devclass_t lebuffer_devclass;
56
57 static device_probe_t lebuffer_probe;
58 static device_attach_t lebuffer_attach;
59 static device_detach_t lebuffer_detach;
60 static bus_print_child_t lebuffer_print_child;
61 static bus_probe_nomatch_t lebuffer_probe_nomatch;
62 static bus_get_resource_list_t lebuffer_get_resource_list;
63 static ofw_bus_get_devinfo_t lebuffer_get_devinfo;
64
65 static struct lebuffer_devinfo *lebuffer_setup_dinfo(device_t, phandle_t);
66 static void lebuffer_destroy_dinfo(struct lebuffer_devinfo *);
67 static int lebuffer_print_res(struct lebuffer_devinfo *);
68
69 static device_method_t lebuffer_methods[] = {
70 /* Device interface */
71 DEVMETHOD(device_probe, lebuffer_probe),
72 DEVMETHOD(device_attach, lebuffer_attach),
73 DEVMETHOD(device_detach, lebuffer_detach),
74 DEVMETHOD(device_shutdown, bus_generic_shutdown),
75 DEVMETHOD(device_suspend, bus_generic_suspend),
76 DEVMETHOD(device_resume, bus_generic_resume),
77
78 /* Bus interface */
79 DEVMETHOD(bus_print_child, lebuffer_print_child),
80 DEVMETHOD(bus_probe_nomatch, lebuffer_probe_nomatch),
81 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
82 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
83 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
84 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
85 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
86 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
87 DEVMETHOD(bus_get_resource_list, lebuffer_get_resource_list),
88 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
89
90 /* ofw_bus interface */
91 DEVMETHOD(ofw_bus_get_devinfo, lebuffer_get_devinfo),
92 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
93 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
94 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
95 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
96 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
97
98 { 0, 0 }
99 };
100
101 DEFINE_CLASS_0(lebuffer, lebuffer_driver, lebuffer_methods, 1);
102 DRIVER_MODULE(lebuffer, sbus, lebuffer_driver, lebuffer_devclass, 0, 0);
103 MODULE_DEPEND(lebuffer, sbus, 1, 1, 1);
104 MODULE_VERSION(lebuffer, 1);
105
106 static int
107 lebuffer_probe(device_t dev)
108 {
109 const char *name;
110
111 name = ofw_bus_get_name(dev);
112 if (strcmp(name, "lebuffer") == 0) {
113 device_set_desc_copy(dev, name);
114 return (0);
115 }
116 return (ENXIO);
117 }
118
119 static int
120 lebuffer_attach(device_t dev)
121 {
122 struct lebuffer_devinfo *ldi;
123 device_t cdev;
124 phandle_t child;
125 int children;
126
127 children = 0;
128 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
129 child = OF_peer(child)) {
130 if ((ldi = lebuffer_setup_dinfo(dev, child)) == NULL)
131 continue;
132 if (children != 0) {
133 device_printf(dev,
134 "<%s>: only one child per buffer supported\n",
135 ldi->ldi_obdinfo.obd_name);
136 lebuffer_destroy_dinfo(ldi);
137 continue;
138 }
139 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
140 device_printf(dev, "<%s>: device_add_child failed\n",
141 ldi->ldi_obdinfo.obd_name);
142 lebuffer_destroy_dinfo(ldi);
143 continue;
144 }
145 device_set_ivars(cdev, ldi);
146 children++;
147 }
148 return (bus_generic_attach(dev));
149 }
150
151 static int
152 lebuffer_detach(device_t dev)
153 {
154 device_t *children;
155 int i, nchildren;
156
157 bus_generic_detach(dev);
158 if (device_get_children(dev, &children, &nchildren) == 0) {
159 for (i = 0; i < nchildren; i++) {
160 lebuffer_destroy_dinfo(device_get_ivars(children[i]));
161 device_delete_child(dev, children[i]);
162 }
163 free(children, M_TEMP);
164 }
165 return (0);
166 }
167
168 static struct lebuffer_devinfo *
169 lebuffer_setup_dinfo(device_t dev, phandle_t node)
170 {
171 struct lebuffer_devinfo *ldi;
172 struct sbus_regs *reg;
173 uint32_t base, iv, *intr;
174 int i, nreg, nintr, slot, rslot;
175
176 ldi = malloc(sizeof(*ldi), M_DEVBUF, M_WAITOK | M_ZERO);
177 if (ofw_bus_gen_setup_devinfo(&ldi->ldi_obdinfo, node) != 0) {
178 free(ldi, M_DEVBUF);
179 return (NULL);
180 }
181 resource_list_init(&ldi->ldi_rl);
182 slot = -1;
183 nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®);
184 if (nreg == -1) {
185 device_printf(dev, "<%s>: incomplete\n",
186 ldi->ldi_obdinfo.obd_name);
187 goto fail;
188 }
189 for (i = 0; i < nreg; i++) {
190 base = reg[i].sbr_offset;
191 if (SBUS_ABS(base)) {
192 rslot = SBUS_ABS_TO_SLOT(base);
193 base = SBUS_ABS_TO_OFFSET(base);
194 } else
195 rslot = reg[i].sbr_slot;
196 if (slot != -1 && slot != rslot) {
197 device_printf(dev, "<%s>: multiple slots\n",
198 ldi->ldi_obdinfo.obd_name);
199 free(reg, M_OFWPROP);
200 goto fail;
201 }
202 slot = rslot;
203
204 resource_list_add(&ldi->ldi_rl, SYS_RES_MEMORY, i, base,
205 base + reg[i].sbr_size, reg[i].sbr_size);
206 }
207 free(reg, M_OFWPROP);
208 if (slot != sbus_get_slot(dev)) {
209 device_printf(dev, "<%s>: parent and child slot do not match\n",
210 ldi->ldi_obdinfo.obd_name);
211 goto fail;
212 }
213
214 /*
215 * The `interrupts' property contains the SBus interrupt level.
216 */
217 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
218 (void **)&intr);
219 if (nintr != -1) {
220 for (i = 0; i < nintr; i++) {
221 iv = intr[i];
222 /*
223 * SBus card devices need the slot number encoded into
224 * the vector as this is generally not done.
225 */
226 if ((iv & INTMAP_OBIO_MASK) == 0)
227 iv |= slot << 3;
228 /* Set the IGN as appropriate. */
229 iv |= sbus_get_ign(dev) << INTMAP_IGN_SHIFT;
230 resource_list_add(&ldi->ldi_rl, SYS_RES_IRQ, i,
231 iv, iv, 1);
232 }
233 free(intr, M_OFWPROP);
234 }
235 return (ldi);
236
237 fail:
238 lebuffer_destroy_dinfo(ldi);
239 return (NULL);
240 }
241
242 static void
243 lebuffer_destroy_dinfo(struct lebuffer_devinfo *dinfo)
244 {
245
246 resource_list_free(&dinfo->ldi_rl);
247 ofw_bus_gen_destroy_devinfo(&dinfo->ldi_obdinfo);
248 free(dinfo, M_DEVBUF);
249 }
250
251 static int
252 lebuffer_print_child(device_t dev, device_t child)
253 {
254 int rv;
255
256 rv = bus_print_child_header(dev, child);
257 rv += lebuffer_print_res(device_get_ivars(child));
258 rv += bus_print_child_footer(dev, child);
259 return (rv);
260 }
261
262 static void
263 lebuffer_probe_nomatch(device_t dev, device_t child)
264 {
265 const char *type;
266
267 device_printf(dev, "<%s>", ofw_bus_get_name(child));
268 lebuffer_print_res(device_get_ivars(child));
269 type = ofw_bus_get_type(child);
270 printf(" type %s (no driver attached)\n",
271 type != NULL ? type : "unknown");
272 }
273
274 static struct resource_list *
275 lebuffer_get_resource_list(device_t dev, device_t child)
276 {
277 struct lebuffer_devinfo *ldi;
278
279 ldi = device_get_ivars(child);
280 return (&ldi->ldi_rl);
281 }
282
283 static const struct ofw_bus_devinfo *
284 lebuffer_get_devinfo(device_t bus, device_t child)
285 {
286 struct lebuffer_devinfo *ldi;
287
288 ldi = device_get_ivars(child);
289 return (&ldi->ldi_obdinfo);
290 }
291
292 static int
293 lebuffer_print_res(struct lebuffer_devinfo *ldi)
294 {
295 int rv;
296
297 rv = 0;
298 rv += resource_list_print_type(&ldi->ldi_rl, "mem", SYS_RES_MEMORY,
299 "%#lx");
300 rv += resource_list_print_type(&ldi->ldi_rl, "irq", SYS_RES_IRQ, "%ld");
301 return (rv);
302 }
Cache object: 276632a4e8f78b49e3b3c9e8d0311c17
|