central.c revision 227848
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 227848 2011-11-22 21:55:40Z 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_adjust_resource_t central_adjust_resource;
64static bus_get_resource_list_t central_get_resource_list;
65static ofw_bus_get_devinfo_t central_get_devinfo;
66
67static int central_print_res(struct central_devinfo *);
68
69static device_method_t central_methods[] = {
70	/* Device interface */
71	DEVMETHOD(device_probe,		central_probe),
72	DEVMETHOD(device_attach,	central_attach),
73	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
74	DEVMETHOD(device_suspend,	bus_generic_suspend),
75	DEVMETHOD(device_resume,	bus_generic_resume),
76
77	/* Bus interface */
78	DEVMETHOD(bus_print_child,	central_print_child),
79	DEVMETHOD(bus_probe_nomatch,	central_probe_nomatch),
80	DEVMETHOD(bus_alloc_resource,	central_alloc_resource),
81	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
82	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
83	DEVMETHOD(bus_adjust_resource,	central_adjust_resource),
84	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
85	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
86	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
87	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
88	DEVMETHOD(bus_get_resource_list, central_get_resource_list),
89	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
90
91	/* ofw_bus interface */
92	DEVMETHOD(ofw_bus_get_devinfo,	central_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	DEVMETHOD_END
100};
101
102static driver_t central_driver = {
103	"central",
104	central_methods,
105	sizeof(struct central_softc),
106};
107
108static devclass_t central_devclass;
109
110EARLY_DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0,
111    BUS_PASS_BUS);
112MODULE_DEPEND(fhc, nexus, 1, 1, 1);
113MODULE_VERSION(central, 1);
114
115static int
116central_probe(device_t dev)
117{
118
119	if (strcmp(ofw_bus_get_name(dev), "central") == 0) {
120		device_set_desc(dev, "central");
121		return (0);
122	}
123	return (ENXIO);
124}
125
126static int
127central_attach(device_t dev)
128{
129	struct central_devinfo *cdi;
130	struct sbus_regs *reg;
131	struct central_softc *sc;
132	phandle_t child;
133	phandle_t node;
134	device_t cdev;
135	int nreg;
136	int i;
137
138	sc = device_get_softc(dev);
139	node = ofw_bus_get_node(dev);
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		cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
150		if (ofw_bus_gen_setup_devinfo(&cdi->cdi_obdinfo, child) != 0) {
151			free(cdi, M_DEVBUF);
152			continue;
153		}
154		nreg = OF_getprop_alloc(child, "reg", sizeof(*reg),
155		    (void **)&reg);
156		if (nreg == -1) {
157			device_printf(dev, "<%s>: incomplete\n",
158			    cdi->cdi_obdinfo.obd_name);
159			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
160			free(cdi, M_DEVBUF);
161			continue;
162		}
163		resource_list_init(&cdi->cdi_rl);
164		for (i = 0; i < nreg; i++)
165			resource_list_add(&cdi->cdi_rl, SYS_RES_MEMORY, i,
166			    reg[i].sbr_offset, reg[i].sbr_offset +
167			    reg[i].sbr_size, reg[i].sbr_size);
168		free(reg, M_OFWPROP);
169		cdev = device_add_child(dev, NULL, -1);
170		if (cdev == NULL) {
171			device_printf(dev, "<%s>: device_add_child failed\n",
172			    cdi->cdi_obdinfo.obd_name);
173			resource_list_free(&cdi->cdi_rl);
174			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
175			free(cdi, M_DEVBUF);
176			continue;
177		}
178		device_set_ivars(cdev, cdi);
179	}
180
181	return (bus_generic_attach(dev));
182}
183
184static int
185central_adjust_resource(device_t bus __unused, device_t child __unused,
186    int type __unused, struct resource *r __unused, u_long start __unused,
187    u_long end __unused)
188{
189
190	return (ENXIO);
191}
192
193static int
194central_print_child(device_t dev, device_t child)
195{
196	int rv;
197
198	rv = bus_print_child_header(dev, child);
199	rv += central_print_res(device_get_ivars(child));
200	rv += bus_print_child_footer(dev, child);
201	return (rv);
202}
203
204static void
205central_probe_nomatch(device_t dev, device_t child)
206{
207	const char *type;
208
209	device_printf(dev, "<%s>", ofw_bus_get_name(child));
210	central_print_res(device_get_ivars(child));
211	type = ofw_bus_get_type(child);
212	printf(" type %s (no driver attached)\n",
213	    type != NULL ? type : "unknown");
214}
215
216static struct resource *
217central_alloc_resource(device_t bus, device_t child, int type, int *rid,
218    u_long start, u_long end, u_long count, u_int flags)
219{
220	struct resource_list *rl;
221	struct resource_list_entry *rle;
222	struct central_softc *sc;
223	struct resource *res;
224	bus_addr_t coffset;
225	bus_addr_t cend;
226	bus_addr_t phys;
227	int isdefault;
228	int passthrough;
229	int i;
230
231	isdefault = (start == 0UL && end == ~0UL);
232	passthrough = (device_get_parent(child) != bus);
233	res = NULL;
234	rle = NULL;
235	rl = BUS_GET_RESOURCE_LIST(bus, child);
236	sc = device_get_softc(bus);
237	switch (type) {
238	case SYS_RES_IRQ:
239		return (resource_list_alloc(rl, bus, child, type, rid, start,
240		    end, count, flags));
241	case SYS_RES_MEMORY:
242		if (!passthrough) {
243			rle = resource_list_find(rl, type, *rid);
244			if (rle == NULL)
245				return (NULL);
246			if (rle->res != NULL)
247				panic("%s: resource entry is busy", __func__);
248			if (isdefault) {
249				start = rle->start;
250				count = ulmax(count, rle->count);
251				end = ulmax(rle->end, start + count - 1);
252			}
253		}
254		for (i = 0; i < sc->sc_nrange; i++) {
255			coffset = sc->sc_ranges[i].coffset;
256			cend = coffset + sc->sc_ranges[i].size - 1;
257			if (start >= coffset && end <= cend) {
258				start -= coffset;
259				end -= coffset;
260				phys = sc->sc_ranges[i].poffset |
261				    ((bus_addr_t)sc->sc_ranges[i].pspace << 32);
262				res = bus_generic_alloc_resource(bus, child,
263				    type, rid, phys + start, phys + end,
264				    count, flags);
265				if (!passthrough)
266					rle->res = res;
267				break;
268			}
269		}
270		break;
271	}
272	return (res);
273}
274
275static struct resource_list *
276central_get_resource_list(device_t bus, device_t child)
277{
278	struct central_devinfo *cdi;
279
280	cdi = device_get_ivars(child);
281	return (&cdi->cdi_rl);
282}
283
284static const struct ofw_bus_devinfo *
285central_get_devinfo(device_t bus, device_t child)
286{
287	struct central_devinfo *cdi;
288
289	cdi = device_get_ivars(child);
290	return (&cdi->cdi_obdinfo);
291}
292
293static int
294central_print_res(struct central_devinfo *cdi)
295{
296
297	return (resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY,
298	    "%#lx"));
299}
300