2 * Copyright (c) 2006 Marius Strobl <marius@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/resource.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/ofw/openfirm.h>
42 #include <machine/bus.h>
43 #include <machine/bus_common.h>
44 #include <machine/resource.h>
46 #include <sparc64/sbus/ofw_sbus.h>
47 #include <sparc64/sbus/sbusreg.h>
48 #include <sparc64/sbus/sbusvar.h>
50 struct lebuffer_devinfo {
51 struct ofw_bus_devinfo ldi_obdinfo;
52 struct resource_list ldi_rl;
55 static devclass_t lebuffer_devclass;
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;
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 *);
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),
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_adjust_resource, bus_generic_adjust_resource),
85 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
86 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
87 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
88 DEVMETHOD(bus_get_resource_list, lebuffer_get_resource_list),
89 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
91 /* ofw_bus interface */
92 DEVMETHOD(ofw_bus_get_devinfo, lebuffer_get_devinfo),
93 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
94 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
95 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
96 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
97 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
102 DEFINE_CLASS_0(lebuffer, lebuffer_driver, lebuffer_methods, 1);
103 DRIVER_MODULE(lebuffer, sbus, lebuffer_driver, lebuffer_devclass, 0, 0);
104 MODULE_DEPEND(lebuffer, sbus, 1, 1, 1);
105 MODULE_VERSION(lebuffer, 1);
108 lebuffer_probe(device_t dev)
112 name = ofw_bus_get_name(dev);
113 if (strcmp(name, "lebuffer") == 0) {
114 device_set_desc_copy(dev, name);
121 lebuffer_attach(device_t dev)
123 struct lebuffer_devinfo *ldi;
129 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
130 child = OF_peer(child)) {
131 if ((ldi = lebuffer_setup_dinfo(dev, child)) == NULL)
135 "<%s>: only one child per buffer supported\n",
136 ldi->ldi_obdinfo.obd_name);
137 lebuffer_destroy_dinfo(ldi);
140 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
141 device_printf(dev, "<%s>: device_add_child failed\n",
142 ldi->ldi_obdinfo.obd_name);
143 lebuffer_destroy_dinfo(ldi);
146 device_set_ivars(cdev, ldi);
149 return (bus_generic_attach(dev));
153 lebuffer_detach(device_t dev)
158 bus_generic_detach(dev);
159 if (device_get_children(dev, &children, &nchildren) == 0) {
160 for (i = 0; i < nchildren; i++) {
161 lebuffer_destroy_dinfo(device_get_ivars(children[i]));
162 device_delete_child(dev, children[i]);
164 free(children, M_TEMP);
169 static struct lebuffer_devinfo *
170 lebuffer_setup_dinfo(device_t dev, phandle_t node)
172 struct lebuffer_devinfo *ldi;
173 struct sbus_regs *reg;
174 uint32_t base, iv, *intr;
175 int i, nreg, nintr, slot, rslot;
177 ldi = malloc(sizeof(*ldi), M_DEVBUF, M_WAITOK | M_ZERO);
178 if (ofw_bus_gen_setup_devinfo(&ldi->ldi_obdinfo, node) != 0) {
182 resource_list_init(&ldi->ldi_rl);
184 nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®);
186 device_printf(dev, "<%s>: incomplete\n",
187 ldi->ldi_obdinfo.obd_name);
190 for (i = 0; i < nreg; i++) {
191 base = reg[i].sbr_offset;
192 if (SBUS_ABS(base)) {
193 rslot = SBUS_ABS_TO_SLOT(base);
194 base = SBUS_ABS_TO_OFFSET(base);
196 rslot = reg[i].sbr_slot;
197 if (slot != -1 && slot != rslot) {
198 device_printf(dev, "<%s>: multiple slots\n",
199 ldi->ldi_obdinfo.obd_name);
205 resource_list_add(&ldi->ldi_rl, SYS_RES_MEMORY, i, base,
206 base + reg[i].sbr_size, reg[i].sbr_size);
209 if (slot != sbus_get_slot(dev)) {
210 device_printf(dev, "<%s>: parent and child slot do not match\n",
211 ldi->ldi_obdinfo.obd_name);
216 * The `interrupts' property contains the SBus interrupt level.
218 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
221 for (i = 0; i < nintr; i++) {
224 * SBus card devices need the slot number encoded into
225 * the vector as this is generally not done.
227 if ((iv & INTMAP_OBIO_MASK) == 0)
229 /* Set the IGN as appropriate. */
230 iv |= sbus_get_ign(dev) << INTMAP_IGN_SHIFT;
231 resource_list_add(&ldi->ldi_rl, SYS_RES_IRQ, i,
239 lebuffer_destroy_dinfo(ldi);
244 lebuffer_destroy_dinfo(struct lebuffer_devinfo *dinfo)
247 resource_list_free(&dinfo->ldi_rl);
248 ofw_bus_gen_destroy_devinfo(&dinfo->ldi_obdinfo);
249 free(dinfo, M_DEVBUF);
253 lebuffer_print_child(device_t dev, device_t child)
257 rv = bus_print_child_header(dev, child);
258 rv += lebuffer_print_res(device_get_ivars(child));
259 rv += bus_print_child_footer(dev, child);
264 lebuffer_probe_nomatch(device_t dev, device_t child)
268 device_printf(dev, "<%s>", ofw_bus_get_name(child));
269 lebuffer_print_res(device_get_ivars(child));
270 type = ofw_bus_get_type(child);
271 printf(" type %s (no driver attached)\n",
272 type != NULL ? type : "unknown");
275 static struct resource_list *
276 lebuffer_get_resource_list(device_t dev, device_t child)
278 struct lebuffer_devinfo *ldi;
280 ldi = device_get_ivars(child);
281 return (&ldi->ldi_rl);
284 static const struct ofw_bus_devinfo *
285 lebuffer_get_devinfo(device_t bus, device_t child)
287 struct lebuffer_devinfo *ldi;
289 ldi = device_get_ivars(child);
290 return (&ldi->ldi_obdinfo);
294 lebuffer_print_res(struct lebuffer_devinfo *ldi)
299 rv += resource_list_print_type(&ldi->ldi_rl, "mem", SYS_RES_MEMORY,
301 rv += resource_list_print_type(&ldi->ldi_rl, "irq", SYS_RES_IRQ, "%jd");