central.c revision 167308
1258283Speter/*-
2258283Speter * Copyright (c) 2003 Jake Burkholder.
3258283Speter * All rights reserved.
4258283Speter *
5258283Speter * Redistribution and use in source and binary forms, with or without
6258283Speter * modification, are permitted provided that the following conditions
7258283Speter * are met:
8258283Speter * 1. Redistributions of source code must retain the above copyright
9258283Speter *    notice, this list of conditions and the following disclaimer.
10258283Speter * 2. Redistributions in binary form must reproduce the above copyright
11258283Speter *    notice, this list of conditions and the following disclaimer in the
12258283Speter *    documentation and/or other materials provided with the distribution.
13258283Speter *
14258283Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15258283Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16258283Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17258283Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18258283Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19258283Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20258283Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21258283Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22258283Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23258283Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24258283Speter * SUCH DAMAGE.
25258283Speter */
26258283Speter
27258283Speter#include <sys/cdefs.h>
28258283Speter__FBSDID("$FreeBSD: head/sys/sparc64/central/central.c 167308 2007-03-07 21:13:51Z marius $");
29258283Speter
30258283Speter#include <sys/param.h>
31258283Speter#include <sys/systm.h>
32258283Speter#include <sys/bus.h>
33258283Speter#include <sys/kernel.h>
34258283Speter#include <sys/malloc.h>
35258283Speter#include <sys/module.h>
36258283Speter
37258283Speter#include <dev/ofw/ofw_bus.h>
38#include <dev/ofw/ofw_bus_subr.h>
39#include <dev/ofw/openfirm.h>
40
41#include <machine/bus.h>
42#include <machine/resource.h>
43
44#include <sys/rman.h>
45
46#include <sparc64/sbus/ofw_sbus.h>
47
48struct central_devinfo {
49	struct ofw_bus_devinfo	cdi_obdinfo;
50	struct resource_list	cdi_rl;
51};
52
53struct central_softc {
54	int			sc_nrange;
55	struct sbus_ranges	*sc_ranges;
56};
57
58static device_probe_t central_probe;
59static device_attach_t central_attach;
60static bus_print_child_t central_print_child;
61static bus_probe_nomatch_t central_probe_nomatch;
62static bus_alloc_resource_t central_alloc_resource;
63static bus_get_resource_list_t central_get_resource_list;
64static ofw_bus_get_devinfo_t central_get_devinfo;
65
66static int central_print_res(struct central_devinfo *);
67
68static device_method_t central_methods[] = {
69	/* Device interface */
70	DEVMETHOD(device_probe,		central_probe),
71	DEVMETHOD(device_attach,	central_attach),
72	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
73	DEVMETHOD(device_suspend,	bus_generic_suspend),
74	DEVMETHOD(device_resume,	bus_generic_resume),
75
76	/* Bus interface */
77	DEVMETHOD(bus_print_child,	central_print_child),
78	DEVMETHOD(bus_probe_nomatch,	central_probe_nomatch),
79	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
80	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
81	DEVMETHOD(bus_alloc_resource,	central_alloc_resource),
82	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
83	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
84	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
85	DEVMETHOD(bus_get_resource_list, central_get_resource_list),
86	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
87
88	/* ofw_bus interface */
89	DEVMETHOD(ofw_bus_get_devinfo,	central_get_devinfo),
90	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
91	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
92	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
93	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
94	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
95
96	{ NULL, NULL }
97};
98
99static driver_t central_driver = {
100	"central",
101	central_methods,
102	sizeof(struct central_softc),
103};
104
105static devclass_t central_devclass;
106
107DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0);
108
109static int
110central_probe(device_t dev)
111{
112
113	if (strcmp(ofw_bus_get_name(dev), "central") == 0) {
114		device_set_desc(dev, "central");
115		return (0);
116	}
117	return (ENXIO);
118}
119
120static int
121central_attach(device_t dev)
122{
123	struct central_devinfo *cdi;
124	struct sbus_regs *reg;
125	struct central_softc *sc;
126	phandle_t child;
127	phandle_t node;
128	device_t cdev;
129	int nreg;
130	int i;
131
132	sc = device_get_softc(dev);
133	node = ofw_bus_get_node(dev);
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\n");
139		return (ENXIO);
140	}
141
142	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
143		cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
144		if (ofw_bus_gen_setup_devinfo(&cdi->cdi_obdinfo, child) != 0) {
145			free(cdi, M_DEVBUF);
146			continue;
147		}
148		nreg = OF_getprop_alloc(child, "reg", sizeof(*reg),
149		    (void **)&reg);
150		if (nreg == -1) {
151			device_printf(dev, "<%s>: incomplete\n",
152			    cdi->cdi_obdinfo.obd_name);
153			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
154			free(cdi, M_DEVBUF);
155			continue;
156		}
157		resource_list_init(&cdi->cdi_rl);
158		for (i = 0; i < nreg; i++)
159			resource_list_add(&cdi->cdi_rl, SYS_RES_MEMORY, i,
160			    reg[i].sbr_offset, reg[i].sbr_offset +
161			    reg[i].sbr_size, reg[i].sbr_size);
162    		free(reg, M_OFWPROP);
163		cdev = device_add_child(dev, NULL, -1);
164		if (cdev == NULL) {
165			device_printf(dev, "<%s>: device_add_child failed\n",
166			    cdi->cdi_obdinfo.obd_name);
167			resource_list_free(&cdi->cdi_rl);
168			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
169			free(cdi, M_DEVBUF);
170			continue;
171		}
172		device_set_ivars(cdev, cdi);
173	}
174
175	return (bus_generic_attach(dev));
176}
177
178static int
179central_print_child(device_t dev, device_t child)
180{
181	int rv;
182
183	rv = bus_print_child_header(dev, child);
184	rv += central_print_res(device_get_ivars(child));
185	rv += bus_print_child_footer(dev, child);
186	return (rv);
187}
188
189static void
190central_probe_nomatch(device_t dev, device_t child)
191{
192	const char *type;
193
194	device_printf(dev, "<%s>", ofw_bus_get_name(child));
195	central_print_res(device_get_ivars(child));
196	type = ofw_bus_get_type(child);
197	printf(" type %s (no driver attached)\n",
198	    type != NULL ? type : "unknown");
199}
200
201static struct resource *
202central_alloc_resource(device_t bus, device_t child, int type, int *rid,
203    u_long start, u_long end, u_long count, u_int flags)
204{
205	struct resource_list *rl;
206	struct resource_list_entry *rle;
207	struct central_softc *sc;
208	struct resource *res;
209	bus_addr_t coffset;
210	bus_addr_t cend;
211	bus_addr_t phys;
212	int isdefault;
213	int passthrough;
214	int i;
215
216	isdefault = (start == 0UL && end == ~0UL);
217	passthrough = (device_get_parent(child) != bus);
218	res = NULL;
219	rle = NULL;
220	rl = BUS_GET_RESOURCE_LIST(bus, child);
221	sc = device_get_softc(bus);
222	switch (type) {
223	case SYS_RES_IRQ:
224		return (resource_list_alloc(rl, bus, child, type, rid, start,
225		    end, count, flags));
226	case SYS_RES_MEMORY:
227		if (!passthrough) {
228			rle = resource_list_find(rl, type, *rid);
229			if (rle == NULL)
230				return (NULL);
231			if (rle->res != NULL)
232				panic("%s: resource entry is busy", __func__);
233			if (isdefault) {
234				start = rle->start;
235				count = ulmax(count, rle->count);
236				end = ulmax(rle->end, start + count - 1);
237			}
238		}
239		for (i = 0; i < sc->sc_nrange; i++) {
240			coffset = sc->sc_ranges[i].coffset;
241			cend = coffset + sc->sc_ranges[i].size - 1;
242			if (start >= coffset && end <= cend) {
243				start -= coffset;
244				end -= coffset;
245				phys = sc->sc_ranges[i].poffset |
246				    ((bus_addr_t)sc->sc_ranges[i].pspace << 32);
247				res = bus_generic_alloc_resource(bus, child,
248				    type, rid, phys + start, phys + end,
249				    count, flags);
250				if (!passthrough)
251					rle->res = res;
252				break;
253			}
254		}
255		break;
256	}
257	return (res);
258}
259
260static struct resource_list *
261central_get_resource_list(device_t bus, device_t child)
262{
263	struct central_devinfo *cdi;
264
265	cdi = device_get_ivars(child);
266	return (&cdi->cdi_rl);
267}
268
269static const struct ofw_bus_devinfo *
270central_get_devinfo(device_t bus, device_t child)
271{
272	struct central_devinfo *cdi;
273
274	cdi = device_get_ivars(child);
275	return (&cdi->cdi_obdinfo);
276}
277
278static int
279central_print_res(struct central_devinfo *cdi)
280{
281
282	return (resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY,
283	    "%#lx"));
284}
285