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$");
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/resource.h>
43111072Sjake
44111072Sjake#include <sys/rman.h>
45111072Sjake
46111072Sjake#include <sparc64/sbus/ofw_sbus.h>
47111072Sjake
48111072Sjakestruct central_devinfo {
49152684Smarius	struct ofw_bus_devinfo	cdi_obdinfo;
50143141Smarius	struct resource_list	cdi_rl;
51111072Sjake};
52111072Sjake
53111072Sjakestruct central_softc {
54111072Sjake	int			sc_nrange;
55111072Sjake	struct sbus_ranges	*sc_ranges;
56111072Sjake};
57111072Sjake
58143128Smariusstatic device_probe_t central_probe;
59143128Smariusstatic device_attach_t central_attach;
60143141Smariusstatic bus_print_child_t central_print_child;
61143128Smariusstatic bus_probe_nomatch_t central_probe_nomatch;
62143128Smariusstatic bus_alloc_resource_t central_alloc_resource;
63225931Smariusstatic bus_adjust_resource_t central_adjust_resource;
64143824Smariusstatic bus_get_resource_list_t central_get_resource_list;
65152684Smariusstatic ofw_bus_get_devinfo_t central_get_devinfo;
66111072Sjake
67152684Smariusstatic int central_print_res(struct central_devinfo *);
68152684Smarius
69111072Sjakestatic device_method_t central_methods[] = {
70167308Smarius	/* Device interface */
71111072Sjake	DEVMETHOD(device_probe,		central_probe),
72111072Sjake	DEVMETHOD(device_attach,	central_attach),
73154870Smarius	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
74154870Smarius	DEVMETHOD(device_suspend,	bus_generic_suspend),
75154870Smarius	DEVMETHOD(device_resume,	bus_generic_resume),
76111072Sjake
77167308Smarius	/* Bus interface */
78143141Smarius	DEVMETHOD(bus_print_child,	central_print_child),
79111072Sjake	DEVMETHOD(bus_probe_nomatch,	central_probe_nomatch),
80111072Sjake	DEVMETHOD(bus_alloc_resource,	central_alloc_resource),
81111072Sjake	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
82111072Sjake	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
83225931Smarius	DEVMETHOD(bus_adjust_resource,	central_adjust_resource),
84190099Smarius	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
85190099Smarius	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
86190099Smarius	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
87190099Smarius	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
88143824Smarius	DEVMETHOD(bus_get_resource_list, central_get_resource_list),
89190114Smarius	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
90111072Sjake
91133589Smarius	/* ofw_bus interface */
92152684Smarius	DEVMETHOD(ofw_bus_get_devinfo,	central_get_devinfo),
93152684Smarius	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
94152684Smarius	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
95152684Smarius	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
96152684Smarius	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
97152684Smarius	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
98133589Smarius
99227848Smarius	DEVMETHOD_END
100111072Sjake};
101111072Sjake
102111072Sjakestatic driver_t central_driver = {
103111072Sjake	"central",
104111072Sjake	central_methods,
105111072Sjake	sizeof(struct central_softc),
106111072Sjake};
107111072Sjake
108111072Sjakestatic devclass_t central_devclass;
109111072Sjake
110200874SmariusEARLY_DRIVER_MODULE(central, nexus, central_driver, central_devclass, 0, 0,
111200874Smarius    BUS_PASS_BUS);
112200815SmariusMODULE_DEPEND(fhc, nexus, 1, 1, 1);
113182070SmariusMODULE_VERSION(central, 1);
114111072Sjake
115111072Sjakestatic int
116111072Sjakecentral_probe(device_t dev)
117111072Sjake{
118111072Sjake
119167308Smarius	if (strcmp(ofw_bus_get_name(dev), "central") == 0) {
120111072Sjake		device_set_desc(dev, "central");
121111072Sjake		return (0);
122111072Sjake	}
123111072Sjake	return (ENXIO);
124111072Sjake}
125111072Sjake
126111072Sjakestatic int
127111072Sjakecentral_attach(device_t dev)
128111072Sjake{
129111072Sjake	struct central_devinfo *cdi;
130143141Smarius	struct sbus_regs *reg;
131111072Sjake	struct central_softc *sc;
132111072Sjake	phandle_t child;
133111072Sjake	phandle_t node;
134111072Sjake	device_t cdev;
135143141Smarius	int nreg;
136143141Smarius	int i;
137111072Sjake
138111072Sjake	sc = device_get_softc(dev);
139167308Smarius	node = ofw_bus_get_node(dev);
140111072Sjake
141111072Sjake	sc->sc_nrange = OF_getprop_alloc(node, "ranges",
142111072Sjake	    sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges);
143111072Sjake	if (sc->sc_nrange == -1) {
144143128Smarius		device_printf(dev, "can't get ranges\n");
145111072Sjake		return (ENXIO);
146111072Sjake	}
147111072Sjake
148111072Sjake	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
149152684Smarius		cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
150152684Smarius		if (ofw_bus_gen_setup_devinfo(&cdi->cdi_obdinfo, child) != 0) {
151152684Smarius			free(cdi, M_DEVBUF);
152111072Sjake			continue;
153152684Smarius		}
154152684Smarius		nreg = OF_getprop_alloc(child, "reg", sizeof(*reg),
155152684Smarius		    (void **)&reg);
156152684Smarius		if (nreg == -1) {
157152684Smarius			device_printf(dev, "<%s>: incomplete\n",
158152684Smarius			    cdi->cdi_obdinfo.obd_name);
159152684Smarius			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
160152684Smarius			free(cdi, M_DEVBUF);
161152684Smarius			continue;
162152684Smarius		}
163152684Smarius		resource_list_init(&cdi->cdi_rl);
164152684Smarius		for (i = 0; i < nreg; i++)
165152684Smarius			resource_list_add(&cdi->cdi_rl, SYS_RES_MEMORY, i,
166152684Smarius			    reg[i].sbr_offset, reg[i].sbr_offset +
167152684Smarius			    reg[i].sbr_size, reg[i].sbr_size);
168300173Sgonzo		OF_prop_free(reg);
169111072Sjake		cdev = device_add_child(dev, NULL, -1);
170152684Smarius		if (cdev == NULL) {
171152684Smarius			device_printf(dev, "<%s>: device_add_child failed\n",
172152684Smarius			    cdi->cdi_obdinfo.obd_name);
173152684Smarius			resource_list_free(&cdi->cdi_rl);
174152684Smarius			ofw_bus_gen_destroy_devinfo(&cdi->cdi_obdinfo);
175152684Smarius			free(cdi, M_DEVBUF);
176152684Smarius			continue;
177152684Smarius		}
178152684Smarius		device_set_ivars(cdev, cdi);
179111072Sjake	}
180111072Sjake
181111072Sjake	return (bus_generic_attach(dev));
182111072Sjake}
183111072Sjake
184143141Smariusstatic int
185225931Smariuscentral_adjust_resource(device_t bus __unused, device_t child __unused,
186294883Sjhibbits    int type __unused, struct resource *r __unused, rman_res_t start __unused,
187294883Sjhibbits    rman_res_t end __unused)
188225931Smarius{
189225931Smarius
190225931Smarius	return (ENXIO);
191225931Smarius}
192225931Smarius
193225931Smariusstatic int
194143141Smariuscentral_print_child(device_t dev, device_t child)
195143141Smarius{
196143141Smarius	int rv;
197143141Smarius
198143141Smarius	rv = bus_print_child_header(dev, child);
199152684Smarius	rv += central_print_res(device_get_ivars(child));
200143141Smarius	rv += bus_print_child_footer(dev, child);
201143141Smarius	return (rv);
202143141Smarius}
203143141Smarius
204111072Sjakestatic void
205111072Sjakecentral_probe_nomatch(device_t dev, device_t child)
206111072Sjake{
207152684Smarius	const char *type;
208111072Sjake
209152684Smarius	device_printf(dev, "<%s>", ofw_bus_get_name(child));
210152684Smarius	central_print_res(device_get_ivars(child));
211152684Smarius	type = ofw_bus_get_type(child);
212143141Smarius	printf(" type %s (no driver attached)\n",
213152684Smarius	    type != NULL ? type : "unknown");
214111072Sjake}
215111072Sjake
216111072Sjakestatic struct resource *
217111072Sjakecentral_alloc_resource(device_t bus, device_t child, int type, int *rid,
218294883Sjhibbits    rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
219111072Sjake{
220143824Smarius	struct resource_list *rl;
221143141Smarius	struct resource_list_entry *rle;
222111072Sjake	struct central_softc *sc;
223111072Sjake	struct resource *res;
224111072Sjake	bus_addr_t coffset;
225111072Sjake	bus_addr_t cend;
226111072Sjake	bus_addr_t phys;
227143141Smarius	int isdefault;
228143824Smarius	int passthrough;
229111072Sjake	int i;
230111072Sjake
231295832Sjhibbits	isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
232143824Smarius	passthrough = (device_get_parent(child) != bus);
233111072Sjake	res = NULL;
234143824Smarius	rle = NULL;
235143824Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
236111072Sjake	sc = device_get_softc(bus);
237143141Smarius	switch (type) {
238143141Smarius	case SYS_RES_IRQ:
239143824Smarius		return (resource_list_alloc(rl, bus, child, type, rid, start,
240143824Smarius		    end, count, flags));
241143141Smarius	case SYS_RES_MEMORY:
242143824Smarius		if (!passthrough) {
243143824Smarius			rle = resource_list_find(rl, type, *rid);
244143824Smarius			if (rle == NULL)
245143824Smarius				return (NULL);
246143824Smarius			if (rle->res != NULL)
247143824Smarius				panic("%s: resource entry is busy", __func__);
248143824Smarius			if (isdefault) {
249143824Smarius				start = rle->start;
250143824Smarius				count = ulmax(count, rle->count);
251143824Smarius				end = ulmax(rle->end, start + count - 1);
252143824Smarius			}
253143824Smarius		}
254143141Smarius		for (i = 0; i < sc->sc_nrange; i++) {
255143141Smarius			coffset = sc->sc_ranges[i].coffset;
256143141Smarius			cend = coffset + sc->sc_ranges[i].size - 1;
257143141Smarius			if (start >= coffset && end <= cend) {
258143141Smarius				start -= coffset;
259143141Smarius				end -= coffset;
260143141Smarius				phys = sc->sc_ranges[i].poffset |
261143141Smarius				    ((bus_addr_t)sc->sc_ranges[i].pspace << 32);
262143141Smarius				res = bus_generic_alloc_resource(bus, child,
263143141Smarius				    type, rid, phys + start, phys + end,
264143141Smarius				    count, flags);
265143824Smarius				if (!passthrough)
266143824Smarius					rle->res = res;
267143141Smarius				break;
268143141Smarius			}
269111072Sjake		}
270143141Smarius		break;
271111072Sjake	}
272111072Sjake	return (res);
273111072Sjake}
274133589Smarius
275143824Smariusstatic struct resource_list *
276143824Smariuscentral_get_resource_list(device_t bus, device_t child)
277143141Smarius{
278143141Smarius	struct central_devinfo *cdi;
279143141Smarius
280143141Smarius	cdi = device_get_ivars(child);
281143824Smarius	return (&cdi->cdi_rl);
282143141Smarius}
283143141Smarius
284152684Smariusstatic const struct ofw_bus_devinfo *
285152684Smariuscentral_get_devinfo(device_t bus, device_t child)
286133589Smarius{
287152684Smarius	struct central_devinfo *cdi;
288133589Smarius
289152684Smarius	cdi = device_get_ivars(child);
290152684Smarius	return (&cdi->cdi_obdinfo);
291133589Smarius}
292133589Smarius
293152684Smariusstatic int
294152684Smariuscentral_print_res(struct central_devinfo *cdi)
295133589Smarius{
296133589Smarius
297152684Smarius	return (resource_list_print_type(&cdi->cdi_rl, "mem", SYS_RES_MEMORY,
298297199Sjhibbits	    "%#jx"));
299133589Smarius}
300