central.c revision 200815
150472Speter/*-
21042Srgrimes * Copyright (c) 2003 Jake Burkholder.
3156813Sru * All rights reserved.
4156813Sru *
5235727Smarcel * Redistribution and use in source and binary forms, with or without
61042Srgrimes * modification, are permitted provided that the following conditions
7235727Smarcel * are met:
8235727Smarcel * 1. Redistributions of source code must retain the above copyright
9235727Smarcel *    notice, this list of conditions and the following disclaimer.
10235727Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11265037Sjmmv *    notice, this list of conditions and the following disclaimer in the
12265037Sjmmv *    documentation and/or other materials provided with the distribution.
13265037Sjmmv *
14265037Sjmmv * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1597433Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1697433Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17255321Stheraven * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1897433Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1967489Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2067489Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211042Srgrimes * 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 200815 2009-12-21 21:29:16Z 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/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_alloc_resource,	central_alloc_resource),
80	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
81	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
82	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
83	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
84	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
85	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
86	DEVMETHOD(bus_get_resource_list, central_get_resource_list),
87	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
88
89	/* ofw_bus interface */
90	DEVMETHOD(ofw_bus_get_devinfo,	central_get_devinfo),
91	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
92	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
93	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
94	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
95	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
96
97	KOBJMETHOD_END
98};
99
100static driver_t central_driver = {
101	"central",
102	central_methods,
103	sizeof(struct central_softc),
104};
105
106static devclass_t central_devclass;
107
108DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0);
109MODULE_DEPEND(fhc, nexus, 1, 1, 1);
110MODULE_VERSION(central, 1);
111
112static int
113central_probe(device_t dev)
114{
115
116	if (strcmp(ofw_bus_get_name(dev), "central") == 0) {
117		device_set_desc(dev, "central");
118		return (0);
119	}
120	return (ENXIO);
121}
122
123static int
124central_attach(device_t dev)
125{
126	struct central_devinfo *cdi;
127	struct sbus_regs *reg;
128	struct central_softc *sc;
129	phandle_t child;
130	phandle_t node;
131	device_t cdev;
132	int nreg;
133	int i;
134
135	sc = device_get_softc(dev);
136	node = ofw_bus_get_node(dev);
137
138	sc->sc_nrange = OF_getprop_alloc(node, "ranges",
139	    sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges);
140	if (sc->sc_nrange == -1) {
141		device_printf(dev, "can't get ranges\n");
142		return (ENXIO);
143	}
144
145	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
146		cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
147		if (ofw_bus_gen_setup_devinfo(&cdi->cdi_obdinfo, child) != 0) {
148			free(cdi, M_DEVBUF);
149			continue;
150		}
151		nreg = OF_getprop_alloc(child, "reg", sizeof(*reg),
152		    (void **)&reg);
153		if (nreg == -1) {
154			device_printf(dev, "<%s>: incomplete\n",
155			    cdi->cdi_obdinfo.obd_name);
156			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
157			free(cdi, M_DEVBUF);
158			continue;
159		}
160		resource_list_init(&cdi->cdi_rl);
161		for (i = 0; i < nreg; i++)
162			resource_list_add(&cdi->cdi_rl, SYS_RES_MEMORY, i,
163			    reg[i].sbr_offset, reg[i].sbr_offset +
164			    reg[i].sbr_size, reg[i].sbr_size);
165		free(reg, M_OFWPROP);
166		cdev = device_add_child(dev, NULL, -1);
167		if (cdev == NULL) {
168			device_printf(dev, "<%s>: device_add_child failed\n",
169			    cdi->cdi_obdinfo.obd_name);
170			resource_list_free(&cdi->cdi_rl);
171			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
172			free(cdi, M_DEVBUF);
173			continue;
174		}
175		device_set_ivars(cdev, cdi);
176	}
177
178	return (bus_generic_attach(dev));
179}
180
181static int
182central_print_child(device_t dev, device_t child)
183{
184	int rv;
185
186	rv = bus_print_child_header(dev, child);
187	rv += central_print_res(device_get_ivars(child));
188	rv += bus_print_child_footer(dev, child);
189	return (rv);
190}
191
192static void
193central_probe_nomatch(device_t dev, device_t child)
194{
195	const char *type;
196
197	device_printf(dev, "<%s>", ofw_bus_get_name(child));
198	central_print_res(device_get_ivars(child));
199	type = ofw_bus_get_type(child);
200	printf(" type %s (no driver attached)\n",
201	    type != NULL ? type : "unknown");
202}
203
204static struct resource *
205central_alloc_resource(device_t bus, device_t child, int type, int *rid,
206    u_long start, u_long end, u_long count, u_int flags)
207{
208	struct resource_list *rl;
209	struct resource_list_entry *rle;
210	struct central_softc *sc;
211	struct resource *res;
212	bus_addr_t coffset;
213	bus_addr_t cend;
214	bus_addr_t phys;
215	int isdefault;
216	int passthrough;
217	int i;
218
219	isdefault = (start == 0UL && end == ~0UL);
220	passthrough = (device_get_parent(child) != bus);
221	res = NULL;
222	rle = NULL;
223	rl = BUS_GET_RESOURCE_LIST(bus, child);
224	sc = device_get_softc(bus);
225	switch (type) {
226	case SYS_RES_IRQ:
227		return (resource_list_alloc(rl, bus, child, type, rid, start,
228		    end, count, flags));
229	case SYS_RES_MEMORY:
230		if (!passthrough) {
231			rle = resource_list_find(rl, type, *rid);
232			if (rle == NULL)
233				return (NULL);
234			if (rle->res != NULL)
235				panic("%s: resource entry is busy", __func__);
236			if (isdefault) {
237				start = rle->start;
238				count = ulmax(count, rle->count);
239				end = ulmax(rle->end, start + count - 1);
240			}
241		}
242		for (i = 0; i < sc->sc_nrange; i++) {
243			coffset = sc->sc_ranges[i].coffset;
244			cend = coffset + sc->sc_ranges[i].size - 1;
245			if (start >= coffset && end <= cend) {
246				start -= coffset;
247				end -= coffset;
248				phys = sc->sc_ranges[i].poffset |
249				    ((bus_addr_t)sc->sc_ranges[i].pspace << 32);
250				res = bus_generic_alloc_resource(bus, child,
251				    type, rid, phys + start, phys + end,
252				    count, flags);
253				if (!passthrough)
254					rle->res = res;
255				break;
256			}
257		}
258		break;
259	}
260	return (res);
261}
262
263static struct resource_list *
264central_get_resource_list(device_t bus, device_t child)
265{
266	struct central_devinfo *cdi;
267
268	cdi = device_get_ivars(child);
269	return (&cdi->cdi_rl);
270}
271
272static const struct ofw_bus_devinfo *
273central_get_devinfo(device_t bus, device_t child)
274{
275	struct central_devinfo *cdi;
276
277	cdi = device_get_ivars(child);
278	return (&cdi->cdi_obdinfo);
279}
280
281static int
282central_print_res(struct central_devinfo *cdi)
283{
284
285	return (resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY,
286	    "%#lx"));
287}
288