central.c revision 143141
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/sparc64/central/central.c 143141 2005-03-04 22:21:11Z marius $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/module.h>
36
37#include <dev/ofw/ofw_bus.h>
38#include <dev/ofw/openfirm.h>
39
40#include <machine/bus.h>
41#include <machine/nexusvar.h>
42#include <machine/ofw_upa.h>
43#include <machine/resource.h>
44
45#include <sys/rman.h>
46
47#include <sparc64/sbus/ofw_sbus.h>
48
49struct central_devinfo {
50	char			*cdi_compat;
51	char			*cdi_model;
52	char			*cdi_name;
53	char			*cdi_type;
54	phandle_t		cdi_node;
55	struct resource_list	cdi_rl;
56};
57
58struct central_softc {
59	phandle_t		sc_node;
60	int			sc_nrange;
61	struct sbus_ranges	*sc_ranges;
62};
63
64static device_probe_t central_probe;
65static device_attach_t central_attach;
66static bus_print_child_t central_print_child;
67static bus_probe_nomatch_t central_probe_nomatch;
68static bus_alloc_resource_t central_alloc_resource;
69static bus_release_resource_t central_release_resource;
70static ofw_bus_get_compat_t central_get_compat;
71static ofw_bus_get_model_t central_get_model;
72static ofw_bus_get_name_t central_get_name;
73static ofw_bus_get_node_t central_get_node;
74static ofw_bus_get_type_t central_get_type;
75
76static device_method_t central_methods[] = {
77	/* Device interface. */
78	DEVMETHOD(device_probe,		central_probe),
79	DEVMETHOD(device_attach,	central_attach),
80
81	/* Bus interface. */
82	DEVMETHOD(bus_print_child,	central_print_child),
83	DEVMETHOD(bus_probe_nomatch,	central_probe_nomatch),
84	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
85	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
86	DEVMETHOD(bus_alloc_resource,	central_alloc_resource),
87	DEVMETHOD(bus_release_resource,	central_release_resource),
88	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
89	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
90
91	/* ofw_bus interface */
92	DEVMETHOD(ofw_bus_get_compat,	central_get_compat),
93	DEVMETHOD(ofw_bus_get_model,	central_get_model),
94	DEVMETHOD(ofw_bus_get_name,	central_get_name),
95	DEVMETHOD(ofw_bus_get_node,	central_get_node),
96	DEVMETHOD(ofw_bus_get_type,	central_get_type),
97
98	{ NULL, NULL }
99};
100
101static driver_t central_driver = {
102	"central",
103	central_methods,
104	sizeof(struct central_softc),
105};
106
107static devclass_t central_devclass;
108
109DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0);
110
111static int
112central_probe(device_t dev)
113{
114
115	if (strcmp(nexus_get_name(dev), "central") == 0) {
116		device_set_desc(dev, "central");
117		return (0);
118	}
119	return (ENXIO);
120}
121
122static int
123central_attach(device_t dev)
124{
125	struct central_devinfo *cdi;
126	struct sbus_regs *reg;
127	struct central_softc *sc;
128	phandle_t child;
129	phandle_t node;
130	bus_addr_t size;
131	bus_addr_t off;
132	device_t cdev;
133	char *name;
134	int nreg;
135	int i;
136
137	sc = device_get_softc(dev);
138	node = nexus_get_node(dev);
139	sc->sc_node = node;
140
141	sc->sc_nrange = OF_getprop_alloc(node, "ranges",
142	    sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges);
143	if (sc->sc_nrange == -1) {
144		device_printf(dev, "can't get ranges\n");
145		return (ENXIO);
146	}
147
148	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
149		if ((OF_getprop_alloc(child, "name", 1, (void **)&name)) == -1)
150			continue;
151		cdev = device_add_child(dev, NULL, -1);
152		if (cdev != NULL) {
153			cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
154			if (cdi == NULL)
155				continue;
156			cdi->cdi_name = name;
157			cdi->cdi_node = child;
158			OF_getprop_alloc(child, "compatible", 1,
159			    (void **)&cdi->cdi_compat);
160			OF_getprop_alloc(child, "device_type", 1,
161			    (void **)&cdi->cdi_type);
162			OF_getprop_alloc(child, "model", 1,
163			    (void **)&cdi->cdi_model);
164			resource_list_init(&cdi->cdi_rl);
165			nreg = OF_getprop_alloc(child, "reg", sizeof(*reg),
166			    (void **)&reg);
167			if (nreg != -1) {
168				for (i = 0; i < nreg; i++) {
169					off = reg[i].sbr_offset;
170					size = reg[i].sbr_size;
171					resource_list_add(&cdi->cdi_rl,
172					    SYS_RES_MEMORY, i, off, off + size,
173					    size);
174				}
175				free(reg, M_OFWPROP);
176			}
177			device_set_ivars(cdev, cdi);
178		} else
179			free(name, M_OFWPROP);
180	}
181
182	return (bus_generic_attach(dev));
183}
184
185static int
186central_print_child(device_t dev, device_t child)
187{
188	struct central_devinfo *cdi;
189	int rv;
190
191	cdi = device_get_ivars(child);
192	rv = bus_print_child_header(dev, child);
193	rv += resource_list_print_type(&cdi->cdi_rl, "mem",
194	    SYS_RES_MEMORY, "%#lx");
195	rv += bus_print_child_footer(dev, child);
196	return (rv);
197}
198
199static void
200central_probe_nomatch(device_t dev, device_t child)
201{
202	struct central_devinfo *cdi;
203
204	cdi = device_get_ivars(child);
205	device_printf(dev, "<%s>", cdi->cdi_name);
206	resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY, "%#lx");
207	printf(" type %s (no driver attached)\n",
208	    cdi->cdi_type != NULL ? cdi->cdi_type : "unknown");
209}
210
211static struct resource *
212central_alloc_resource(device_t bus, device_t child, int type, int *rid,
213    u_long start, u_long end, u_long count, u_int flags)
214{
215	struct resource_list_entry *rle;
216	struct central_devinfo *cdi;
217	struct central_softc *sc;
218	struct resource *res;
219	bus_addr_t coffset;
220	bus_addr_t cend;
221	bus_addr_t phys;
222	int isdefault;
223	int i;
224
225	isdefault = (start == 0UL && end == ~0UL);
226	res = NULL;
227	sc = device_get_softc(bus);
228	cdi = device_get_ivars(child);
229	rle = resource_list_find(&cdi->cdi_rl, type, *rid);
230	if (rle == NULL)
231		return (NULL);
232	if (rle->res != NULL)
233		panic("%s: resource entry is busy", __func__);
234	if (isdefault) {
235		start = rle->start;
236		count = ulmax(count, rle->count);
237		end = ulmax(rle->end, start + count - 1);
238	}
239	switch (type) {
240	case SYS_RES_IRQ:
241		res = bus_generic_alloc_resource(bus, child, type, rid,
242		    start, end, count, flags);
243		break;
244	case SYS_RES_MEMORY:
245		for (i = 0; i < sc->sc_nrange; i++) {
246			coffset = sc->sc_ranges[i].coffset;
247			cend = coffset + sc->sc_ranges[i].size - 1;
248			if (start >= coffset && end <= cend) {
249				start -= coffset;
250				end -= coffset;
251				phys = sc->sc_ranges[i].poffset |
252				    ((bus_addr_t)sc->sc_ranges[i].pspace << 32);
253				res = bus_generic_alloc_resource(bus, child,
254				    type, rid, phys + start, phys + end,
255				    count, flags);
256				rle->res = res;
257				break;
258			}
259		}
260		break;
261	default:
262		break;
263	}
264	return (res);
265}
266
267static int
268central_release_resource(device_t bus, device_t child, int type, int rid,
269    struct resource *r)
270{
271	struct resource_list_entry *rle;
272	struct central_devinfo *cdi;
273	int error;
274
275	error = bus_generic_release_resource(bus, child, type, rid, r);
276	if (error != 0)
277		return (error);
278	cdi = device_get_ivars(child);
279	rle = resource_list_find(&cdi->cdi_rl, type, rid);
280	if (rle == NULL)
281		panic("%s: can't find resource", __func__);
282	if (rle->res == NULL)
283		panic("%s: resource entry is not busy", __func__);
284	rle->res = NULL;
285	return (error);
286}
287
288static const char *
289central_get_compat(device_t bus, device_t dev)
290{
291	struct central_devinfo *dinfo;
292
293	dinfo = device_get_ivars(dev);
294	return (dinfo->cdi_compat);
295}
296
297static const char *
298central_get_model(device_t bus, device_t dev)
299{
300	struct central_devinfo *dinfo;
301
302	dinfo = device_get_ivars(dev);
303	return (dinfo->cdi_model);
304}
305
306static const char *
307central_get_name(device_t bus, device_t dev)
308{
309	struct central_devinfo *dinfo;
310
311	dinfo = device_get_ivars(dev);
312	return (dinfo->cdi_name);
313}
314
315static phandle_t
316central_get_node(device_t bus, device_t dev)
317{
318	struct central_devinfo *dinfo;
319
320	dinfo = device_get_ivars(dev);
321	return (dinfo->cdi_node);
322}
323
324static const char *
325central_get_type(device_t bus, device_t dev)
326{
327	struct central_devinfo *dinfo;
328
329	dinfo = device_get_ivars(dev);
330	return (dinfo->cdi_type);
331}
332