xref: /freebsd-12.1/sys/dev/le/lebuffer_sbus.c (revision bb487d2b)
1 /*-
2  * Copyright (c) 2006 Marius Strobl <[email protected]>
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_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),
90 
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),
98 
99 	{ 0, 0 }
100 };
101 
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);
106 
107 static int
108 lebuffer_probe(device_t dev)
109 {
110 	const char *name;
111 
112 	name = ofw_bus_get_name(dev);
113 	if (strcmp(name, "lebuffer") == 0) {
114 		device_set_desc_copy(dev, name);
115 		return (0);
116 	}
117 	return (ENXIO);
118 }
119 
120 static int
121 lebuffer_attach(device_t dev)
122 {
123 	struct lebuffer_devinfo *ldi;
124 	device_t cdev;
125 	phandle_t child;
126 	int children;
127 
128 	children = 0;
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)
132 			continue;
133 		if (children != 0) {
134 			device_printf(dev,
135 			    "<%s>: only one child per buffer supported\n",
136 			    ldi->ldi_obdinfo.obd_name);
137 			lebuffer_destroy_dinfo(ldi);
138 			continue;
139 		}
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);
144 			continue;
145 		}
146 		device_set_ivars(cdev, ldi);
147 		children++;
148 	}
149 	return (bus_generic_attach(dev));
150 }
151 
152 static int
153 lebuffer_detach(device_t dev)
154 {
155 	device_t *children;
156 	int i, nchildren;
157 
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]);
163 		}
164 		free(children, M_TEMP);
165 	}
166 	return (0);
167 }
168 
169 static struct lebuffer_devinfo *
170 lebuffer_setup_dinfo(device_t dev, phandle_t node)
171 {
172 	struct lebuffer_devinfo *ldi;
173 	struct sbus_regs *reg;
174 	uint32_t base, iv, *intr;
175 	int i, nreg, nintr, slot, rslot;
176 
177 	ldi = malloc(sizeof(*ldi), M_DEVBUF, M_WAITOK | M_ZERO);
178 	if (ofw_bus_gen_setup_devinfo(&ldi->ldi_obdinfo, node) != 0) {
179 		free(ldi, M_DEVBUF);
180 		return (NULL);
181 	}
182 	resource_list_init(&ldi->ldi_rl);
183 	slot = -1;
184 	nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
185 	if (nreg == -1) {
186 		device_printf(dev, "<%s>: incomplete\n",
187 		    ldi->ldi_obdinfo.obd_name);
188 		goto fail;
189 	}
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);
195 		} else
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);
200 			free(reg, M_OFWPROP);
201 			goto fail;
202 		}
203 		slot = rslot;
204 
205 		resource_list_add(&ldi->ldi_rl, SYS_RES_MEMORY, i, base,
206 		    base + reg[i].sbr_size, reg[i].sbr_size);
207 	}
208 	free(reg, M_OFWPROP);
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);
212 		goto fail;
213 	}
214 
215 	/*
216 	 * The `interrupts' property contains the SBus interrupt level.
217 	 */
218 	nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr),
219 	    (void **)&intr);
220 	if (nintr != -1) {
221 		for (i = 0; i < nintr; i++) {
222 			iv = intr[i];
223 			/*
224 			 * SBus card devices need the slot number encoded into
225 			 * the vector as this is generally not done.
226 			 */
227 			if ((iv & INTMAP_OBIO_MASK) == 0)
228 				iv |= slot << 3;
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,
232 			    iv, iv, 1);
233 		}
234 		free(intr, M_OFWPROP);
235 	}
236 	return (ldi);
237 
238  fail:
239 	lebuffer_destroy_dinfo(ldi);
240 	return (NULL);
241 }
242 
243 static void
244 lebuffer_destroy_dinfo(struct lebuffer_devinfo *dinfo)
245 {
246 
247 	resource_list_free(&dinfo->ldi_rl);
248 	ofw_bus_gen_destroy_devinfo(&dinfo->ldi_obdinfo);
249 	free(dinfo, M_DEVBUF);
250 }
251 
252 static int
253 lebuffer_print_child(device_t dev, device_t child)
254 {
255 	int rv;
256 
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);
260 	return (rv);
261 }
262 
263 static void
264 lebuffer_probe_nomatch(device_t dev, device_t child)
265 {
266 	const char *type;
267 
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");
273 }
274 
275 static struct resource_list *
276 lebuffer_get_resource_list(device_t dev, device_t child)
277 {
278 	struct lebuffer_devinfo *ldi;
279 
280 	ldi = device_get_ivars(child);
281 	return (&ldi->ldi_rl);
282 }
283 
284 static const struct ofw_bus_devinfo *
285 lebuffer_get_devinfo(device_t bus, device_t child)
286 {
287 	struct lebuffer_devinfo *ldi;
288 
289 	ldi = device_get_ivars(child);
290 	return (&ldi->ldi_obdinfo);
291 }
292 
293 static int
294 lebuffer_print_res(struct lebuffer_devinfo *ldi)
295 {
296 	int rv;
297 
298 	rv = 0;
299 	rv += resource_list_print_type(&ldi->ldi_rl, "mem", SYS_RES_MEMORY,
300 	    "%#lx");
301 	rv += resource_list_print_type(&ldi->ldi_rl, "irq", SYS_RES_IRQ, "%ld");
302 	return (rv);
303 }
304