central.c revision 152684
1111072Sjake/*-
2111072Sjake * Copyright (c) 2003 Jake Burkholder.
3111072Sjake * All rights reserved.
4111072Sjake *
5111072Sjake * Redistribution and use in source and binary forms, with or without
6111072Sjake * modification, are permitted provided that the following conditions
7111072Sjake * are met:
8111072Sjake * 1. Redistributions of source code must retain the above copyright
9111072Sjake *    notice, this list of conditions and the following disclaimer.
10111072Sjake * 2. Redistributions in binary form must reproduce the above copyright
11111072Sjake *    notice, this list of conditions and the following disclaimer in the
12111072Sjake *    documentation and/or other materials provided with the distribution.
13111072Sjake *
14111072Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15111072Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16111072Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17111072Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18111072Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19111072Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20111072Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21111072Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22111072Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23111072Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24111072Sjake * SUCH DAMAGE.
25111072Sjake */
26111072Sjake
27143128Smarius#include <sys/cdefs.h>
28143128Smarius__FBSDID("$FreeBSD: head/sys/sparc64/central/central.c 152684 2005-11-22 16:39:44Z marius $");
29143128Smarius
30111072Sjake#include <sys/param.h>
31111072Sjake#include <sys/systm.h>
32111072Sjake#include <sys/bus.h>
33111072Sjake#include <sys/kernel.h>
34111072Sjake#include <sys/malloc.h>
35130025Sphk#include <sys/module.h>
36111072Sjake
37133589Smarius#include <dev/ofw/ofw_bus.h>
38152684Smarius#include <dev/ofw/ofw_bus_subr.h>
39111072Sjake#include <dev/ofw/openfirm.h>
40111072Sjake
41111072Sjake#include <machine/bus.h>
42111072Sjake#include <machine/nexusvar.h>
43111072Sjake#include <machine/ofw_upa.h>
44111072Sjake#include <machine/resource.h>
45111072Sjake
46111072Sjake#include <sys/rman.h>
47111072Sjake
48111072Sjake#include <sparc64/sbus/ofw_sbus.h>
49111072Sjake
50111072Sjakestruct central_devinfo {
51152684Smarius	struct ofw_bus_devinfo	cdi_obdinfo;
52143141Smarius	struct resource_list	cdi_rl;
53111072Sjake};
54111072Sjake
55111072Sjakestruct central_softc {
56111072Sjake	phandle_t		sc_node;
57111072Sjake	int			sc_nrange;
58111072Sjake	struct sbus_ranges	*sc_ranges;
59111072Sjake};
60111072Sjake
61143128Smariusstatic device_probe_t central_probe;
62143128Smariusstatic device_attach_t central_attach;
63143141Smariusstatic bus_print_child_t central_print_child;
64143128Smariusstatic bus_probe_nomatch_t central_probe_nomatch;
65143128Smariusstatic bus_alloc_resource_t central_alloc_resource;
66143824Smariusstatic bus_get_resource_list_t central_get_resource_list;
67152684Smariusstatic ofw_bus_get_devinfo_t central_get_devinfo;
68111072Sjake
69152684Smariusstatic int central_print_res(struct central_devinfo *);
70152684Smarius
71111072Sjakestatic device_method_t central_methods[] = {
72111072Sjake	/* Device interface. */
73111072Sjake	DEVMETHOD(device_probe,		central_probe),
74111072Sjake	DEVMETHOD(device_attach,	central_attach),
75111072Sjake
76111072Sjake	/* Bus interface. */
77143141Smarius	DEVMETHOD(bus_print_child,	central_print_child),
78111072Sjake	DEVMETHOD(bus_probe_nomatch,	central_probe_nomatch),
79111072Sjake	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
80111072Sjake	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
81111072Sjake	DEVMETHOD(bus_alloc_resource,	central_alloc_resource),
82143824Smarius	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
83111072Sjake	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
84111072Sjake	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
85143824Smarius	DEVMETHOD(bus_get_resource_list, central_get_resource_list),
86146398Smarius	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
87111072Sjake
88133589Smarius	/* ofw_bus interface */
89152684Smarius	DEVMETHOD(ofw_bus_get_devinfo,	central_get_devinfo),
90152684Smarius	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
91152684Smarius	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
92152684Smarius	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
93152684Smarius	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
94152684Smarius	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
95133589Smarius
96111072Sjake	{ NULL, NULL }
97111072Sjake};
98111072Sjake
99111072Sjakestatic driver_t central_driver = {
100111072Sjake	"central",
101111072Sjake	central_methods,
102111072Sjake	sizeof(struct central_softc),
103111072Sjake};
104111072Sjake
105111072Sjakestatic devclass_t central_devclass;
106111072Sjake
107111072SjakeDRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0);
108111072Sjake
109111072Sjakestatic int
110111072Sjakecentral_probe(device_t dev)
111111072Sjake{
112111072Sjake
113111072Sjake	if (strcmp(nexus_get_name(dev), "central") == 0) {
114111072Sjake		device_set_desc(dev, "central");
115111072Sjake		return (0);
116111072Sjake	}
117111072Sjake	return (ENXIO);
118111072Sjake}
119111072Sjake
120111072Sjakestatic int
121111072Sjakecentral_attach(device_t dev)
122111072Sjake{
123111072Sjake	struct central_devinfo *cdi;
124143141Smarius	struct sbus_regs *reg;
125111072Sjake	struct central_softc *sc;
126111072Sjake	phandle_t child;
127111072Sjake	phandle_t node;
128111072Sjake	device_t cdev;
129143141Smarius	int nreg;
130143141Smarius	int i;
131111072Sjake
132111072Sjake	sc = device_get_softc(dev);
133111072Sjake	node = nexus_get_node(dev);
134111072Sjake	sc->sc_node = node;
135111072Sjake
136111072Sjake	sc->sc_nrange = OF_getprop_alloc(node, "ranges",
137111072Sjake	    sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges);
138111072Sjake	if (sc->sc_nrange == -1) {
139143128Smarius		device_printf(dev, "can't get ranges\n");
140111072Sjake		return (ENXIO);
141111072Sjake	}
142111072Sjake
143111072Sjake	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
144152684Smarius		cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
145152684Smarius		if (ofw_bus_gen_setup_devinfo(&cdi->cdi_obdinfo, child) != 0) {
146152684Smarius			free(cdi, M_DEVBUF);
147111072Sjake			continue;
148152684Smarius		}
149152684Smarius		nreg = OF_getprop_alloc(child, "reg", sizeof(*reg),
150152684Smarius		    (void **)&reg);
151152684Smarius		if (nreg == -1) {
152152684Smarius			device_printf(dev, "<%s>: incomplete\n",
153152684Smarius			    cdi->cdi_obdinfo.obd_name);
154152684Smarius			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
155152684Smarius			free(cdi, M_DEVBUF);
156152684Smarius			continue;
157152684Smarius		}
158152684Smarius		resource_list_init(&cdi->cdi_rl);
159152684Smarius		for (i = 0; i < nreg; i++)
160152684Smarius			resource_list_add(&cdi->cdi_rl, SYS_RES_MEMORY, i,
161152684Smarius			    reg[i].sbr_offset, reg[i].sbr_offset +
162152684Smarius			    reg[i].sbr_size, reg[i].sbr_size);
163152684Smarius    		free(reg, M_OFWPROP);
164111072Sjake		cdev = device_add_child(dev, NULL, -1);
165152684Smarius		if (cdev == NULL) {
166152684Smarius			device_printf(dev, "<%s>: device_add_child failed\n",
167152684Smarius			    cdi->cdi_obdinfo.obd_name);
168152684Smarius			resource_list_free(&cdi->cdi_rl);
169152684Smarius			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
170152684Smarius			free(cdi, M_DEVBUF);
171152684Smarius			continue;
172152684Smarius		}
173152684Smarius		device_set_ivars(cdev, cdi);
174111072Sjake	}
175111072Sjake
176111072Sjake	return (bus_generic_attach(dev));
177111072Sjake}
178111072Sjake
179143141Smariusstatic int
180143141Smariuscentral_print_child(device_t dev, device_t child)
181143141Smarius{
182143141Smarius	int rv;
183143141Smarius
184143141Smarius	rv = bus_print_child_header(dev, child);
185152684Smarius	rv += central_print_res(device_get_ivars(child));
186143141Smarius	rv += bus_print_child_footer(dev, child);
187143141Smarius	return (rv);
188143141Smarius}
189143141Smarius
190111072Sjakestatic void
191111072Sjakecentral_probe_nomatch(device_t dev, device_t child)
192111072Sjake{
193152684Smarius	const char *type;
194111072Sjake
195152684Smarius	device_printf(dev, "<%s>", ofw_bus_get_name(child));
196152684Smarius	central_print_res(device_get_ivars(child));
197152684Smarius	type = ofw_bus_get_type(child);
198143141Smarius	printf(" type %s (no driver attached)\n",
199152684Smarius	    type != NULL ? type : "unknown");
200111072Sjake}
201111072Sjake
202111072Sjakestatic struct resource *
203111072Sjakecentral_alloc_resource(device_t bus, device_t child, int type, int *rid,
204111072Sjake    u_long start, u_long end, u_long count, u_int flags)
205111072Sjake{
206143824Smarius	struct resource_list *rl;
207143141Smarius	struct resource_list_entry *rle;
208111072Sjake	struct central_softc *sc;
209111072Sjake	struct resource *res;
210111072Sjake	bus_addr_t coffset;
211111072Sjake	bus_addr_t cend;
212111072Sjake	bus_addr_t phys;
213143141Smarius	int isdefault;
214143824Smarius	int passthrough;
215111072Sjake	int i;
216111072Sjake
217143141Smarius	isdefault = (start == 0UL && end == ~0UL);
218143824Smarius	passthrough = (device_get_parent(child) != bus);
219111072Sjake	res = NULL;
220143824Smarius	rle = NULL;
221143824Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
222111072Sjake	sc = device_get_softc(bus);
223143141Smarius	switch (type) {
224143141Smarius	case SYS_RES_IRQ:
225143824Smarius		return (resource_list_alloc(rl, bus, child, type, rid, start,
226143824Smarius		    end, count, flags));
227143141Smarius	case SYS_RES_MEMORY:
228143824Smarius		if (!passthrough) {
229143824Smarius			rle = resource_list_find(rl, type, *rid);
230143824Smarius			if (rle == NULL)
231143824Smarius				return (NULL);
232143824Smarius			if (rle->res != NULL)
233143824Smarius				panic("%s: resource entry is busy", __func__);
234143824Smarius			if (isdefault) {
235143824Smarius				start = rle->start;
236143824Smarius				count = ulmax(count, rle->count);
237143824Smarius				end = ulmax(rle->end, start + count - 1);
238143824Smarius			}
239143824Smarius		}
240143141Smarius		for (i = 0; i < sc->sc_nrange; i++) {
241143141Smarius			coffset = sc->sc_ranges[i].coffset;
242143141Smarius			cend = coffset + sc->sc_ranges[i].size - 1;
243143141Smarius			if (start >= coffset && end <= cend) {
244143141Smarius				start -= coffset;
245143141Smarius				end -= coffset;
246143141Smarius				phys = sc->sc_ranges[i].poffset |
247143141Smarius				    ((bus_addr_t)sc->sc_ranges[i].pspace << 32);
248143141Smarius				res = bus_generic_alloc_resource(bus, child,
249143141Smarius				    type, rid, phys + start, phys + end,
250143141Smarius				    count, flags);
251143824Smarius				if (!passthrough)
252143824Smarius					rle->res = res;
253143141Smarius				break;
254143141Smarius			}
255111072Sjake		}
256143141Smarius		break;
257111072Sjake	}
258111072Sjake	return (res);
259111072Sjake}
260133589Smarius
261143824Smariusstatic struct resource_list *
262143824Smariuscentral_get_resource_list(device_t bus, device_t child)
263143141Smarius{
264143141Smarius	struct central_devinfo *cdi;
265143141Smarius
266143141Smarius	cdi = device_get_ivars(child);
267143824Smarius	return (&cdi->cdi_rl);
268143141Smarius}
269143141Smarius
270152684Smariusstatic const struct ofw_bus_devinfo *
271152684Smariuscentral_get_devinfo(device_t bus, device_t child)
272133589Smarius{
273152684Smarius	struct central_devinfo *cdi;
274133589Smarius
275152684Smarius	cdi = device_get_ivars(child);
276152684Smarius	return (&cdi->cdi_obdinfo);
277133589Smarius}
278133589Smarius
279152684Smariusstatic int
280152684Smariuscentral_print_res(struct central_devinfo *cdi)
281133589Smarius{
282133589Smarius
283152684Smarius	return (resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY,
284152684Smarius	    "%#lx"));
285133589Smarius}
286