1111072Sjake/*-
2111072Sjake * Copyright (c) 2003 Jake Burkholder.
3167308Smarius * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
4111072Sjake * All rights reserved.
5111072Sjake *
6111072Sjake * Redistribution and use in source and binary forms, with or without
7111072Sjake * modification, are permitted provided that the following conditions
8111072Sjake * are met:
9111072Sjake * 1. Redistributions of source code must retain the above copyright
10111072Sjake *    notice, this list of conditions and the following disclaimer.
11111072Sjake * 2. Redistributions in binary form must reproduce the above copyright
12111072Sjake *    notice, this list of conditions and the following disclaimer in the
13111072Sjake *    documentation and/or other materials provided with the distribution.
14111072Sjake *
15111072Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16111072Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17111072Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18111072Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19111072Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20111072Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21111072Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22111072Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23111072Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24111072Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25111072Sjake * SUCH DAMAGE.
26111072Sjake */
27111072Sjake
28143129Smarius#include <sys/cdefs.h>
29143129Smarius__FBSDID("$FreeBSD$");
30143129Smarius
31111072Sjake#include <sys/param.h>
32111072Sjake#include <sys/systm.h>
33111072Sjake#include <sys/bus.h>
34111072Sjake#include <sys/kernel.h>
35111072Sjake#include <sys/malloc.h>
36167308Smarius#include <sys/module.h>
37111123Sjake#include <sys/pcpu.h>
38111072Sjake
39143826Smarius#include <dev/led/led.h>
40133589Smarius#include <dev/ofw/ofw_bus.h>
41152684Smarius#include <dev/ofw/ofw_bus_subr.h>
42111072Sjake#include <dev/ofw/openfirm.h>
43111072Sjake
44111072Sjake#include <machine/bus.h>
45111072Sjake#include <machine/bus_common.h>
46111072Sjake#include <machine/resource.h>
47111072Sjake
48111072Sjake#include <sys/rman.h>
49111072Sjake
50111072Sjake#include <sparc64/fhc/fhcreg.h>
51111072Sjake#include <sparc64/sbus/ofw_sbus.h>
52111072Sjake
53111072Sjakestruct fhc_devinfo {
54152684Smarius	struct ofw_bus_devinfo	fdi_obdinfo;
55111072Sjake	struct resource_list	fdi_rl;
56111072Sjake};
57111072Sjake
58167308Smariusstruct fhc_softc {
59172066Smarius	struct resource		*sc_memres[FHC_NREG];
60167308Smarius	int			sc_nrange;
61167308Smarius	struct sbus_ranges	*sc_ranges;
62167308Smarius	int			sc_ign;
63167308Smarius	struct cdev		*sc_led_dev;
64167308Smarius};
65167308Smarius
66167308Smariusstatic device_probe_t fhc_probe;
67167308Smariusstatic device_attach_t fhc_attach;
68167308Smariusstatic bus_print_child_t fhc_print_child;
69167308Smariusstatic bus_probe_nomatch_t fhc_probe_nomatch;
70167308Smariusstatic bus_setup_intr_t fhc_setup_intr;
71167308Smariusstatic bus_alloc_resource_t fhc_alloc_resource;
72225931Smariusstatic bus_adjust_resource_t fhc_adjust_resource;
73167308Smariusstatic bus_get_resource_list_t fhc_get_resource_list;
74167308Smariusstatic ofw_bus_get_devinfo_t fhc_get_devinfo;
75167308Smarius
76172066Smariusstatic void fhc_intr_enable(void *);
77172066Smariusstatic void fhc_intr_disable(void *);
78178443Smariusstatic void fhc_intr_assign(void *);
79178443Smariusstatic void fhc_intr_clear(void *);
80143826Smariusstatic void fhc_led_func(void *, int);
81152684Smariusstatic int fhc_print_res(struct fhc_devinfo *);
82111123Sjake
83167308Smariusstatic device_method_t fhc_methods[] = {
84167308Smarius	/* Device interface */
85167308Smarius	DEVMETHOD(device_probe,		fhc_probe),
86167308Smarius	DEVMETHOD(device_attach,	fhc_attach),
87167308Smarius	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
88167308Smarius	DEVMETHOD(device_suspend,	bus_generic_suspend),
89167308Smarius	DEVMETHOD(device_resume,	bus_generic_resume),
90167308Smarius
91167308Smarius	/* Bus interface */
92167308Smarius	DEVMETHOD(bus_print_child,	fhc_print_child),
93167308Smarius	DEVMETHOD(bus_probe_nomatch,	fhc_probe_nomatch),
94167308Smarius	DEVMETHOD(bus_alloc_resource,	fhc_alloc_resource),
95167308Smarius	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
96167308Smarius	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
97225931Smarius	DEVMETHOD(bus_adjust_resource,	fhc_adjust_resource),
98190098Smarius	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
99190098Smarius	DEVMETHOD(bus_setup_intr,	fhc_setup_intr),
100190098Smarius	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
101190098Smarius	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
102167308Smarius	DEVMETHOD(bus_get_resource_list, fhc_get_resource_list),
103190114Smarius	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
104167308Smarius
105167308Smarius	/* ofw_bus interface */
106167308Smarius	DEVMETHOD(ofw_bus_get_devinfo,	fhc_get_devinfo),
107167308Smarius	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
108167308Smarius	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
109167308Smarius	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
110167308Smarius	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
111167308Smarius	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
112167308Smarius
113227848Smarius	DEVMETHOD_END
114167308Smarius};
115167308Smarius
116167308Smariusstatic driver_t fhc_driver = {
117167308Smarius	"fhc",
118167308Smarius	fhc_methods,
119167308Smarius	sizeof(struct fhc_softc),
120167308Smarius};
121167308Smarius
122167308Smariusstatic devclass_t fhc_devclass;
123167308Smarius
124200874SmariusEARLY_DRIVER_MODULE(fhc, central, fhc_driver, fhc_devclass, 0, 0,
125200874Smarius    BUS_PASS_BUS);
126200815SmariusMODULE_DEPEND(fhc, central, 1, 1, 1);
127200874SmariusEARLY_DRIVER_MODULE(fhc, nexus, fhc_driver, fhc_devclass, 0, 0,
128200874Smarius    BUS_PASS_BUS);
129200815SmariusMODULE_DEPEND(fhc, nexus, 1, 1, 1);
130182070SmariusMODULE_VERSION(fhc, 1);
131167308Smarius
132172066Smariusstatic const struct intr_controller fhc_ic = {
133172066Smarius	fhc_intr_enable,
134172066Smarius	fhc_intr_disable,
135178443Smarius	fhc_intr_assign,
136178443Smarius	fhc_intr_clear
137172066Smarius};
138172066Smarius
139172066Smariusstruct fhc_icarg {
140172066Smarius	struct fhc_softc	*fica_sc;
141172066Smarius	struct resource		*fica_memres;
142172066Smarius};
143172066Smarius
144167308Smariusstatic int
145111072Sjakefhc_probe(device_t dev)
146111072Sjake{
147111072Sjake
148167308Smarius	if (strcmp(ofw_bus_get_name(dev), "fhc") == 0) {
149167308Smarius		device_set_desc(dev, "fhc");
150167308Smarius		return (0);
151167308Smarius	}
152167308Smarius	return (ENXIO);
153111072Sjake}
154111072Sjake
155167308Smariusstatic int
156111072Sjakefhc_attach(device_t dev)
157111072Sjake{
158143826Smarius	char ledname[sizeof("boardXX")];
159111072Sjake	struct fhc_devinfo *fdi;
160172066Smarius	struct fhc_icarg *fica;
161172066Smarius	struct fhc_softc *sc;
162111072Sjake	struct sbus_regs *reg;
163111072Sjake	phandle_t child;
164111072Sjake	phandle_t node;
165111072Sjake	device_t cdev;
166167308Smarius	uint32_t board;
167111123Sjake	uint32_t ctrl;
168143142Smarius	uint32_t *intr;
169143142Smarius	uint32_t iv;
170111072Sjake	char *name;
171172066Smarius	int central;
172167308Smarius	int error;
173167308Smarius	int i;
174190098Smarius	int j;
175111072Sjake
176111072Sjake	sc = device_get_softc(dev);
177167308Smarius	node = ofw_bus_get_node(dev);
178111072Sjake
179172066Smarius	central = 0;
180167308Smarius	if (strcmp(device_get_name(device_get_parent(dev)), "central") == 0)
181172066Smarius		central = 1;
182167308Smarius
183167308Smarius	for (i = 0; i < FHC_NREG; i++) {
184190098Smarius		j = i;
185167308Smarius		sc->sc_memres[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
186190098Smarius		    &j, RF_ACTIVE);
187167308Smarius		if (sc->sc_memres[i] == NULL) {
188167308Smarius			device_printf(dev, "cannot allocate resource %d\n", i);
189167308Smarius			error = ENXIO;
190167308Smarius			goto fail_memres;
191167308Smarius		}
192167308Smarius	}
193167308Smarius
194172066Smarius	if (central != 0) {
195172066Smarius		board = bus_read_4(sc->sc_memres[FHC_INTERNAL], FHC_BSR);
196172066Smarius		board = ((board >> 16) & 0x1) | ((board >> 12) & 0xe);
197167308Smarius	} else {
198172066Smarius		if (OF_getprop(node, "board#", &board, sizeof(board)) == -1) {
199167308Smarius			device_printf(dev, "cannot get board number\n");
200167308Smarius			error = ENXIO;
201167308Smarius			goto fail_memres;
202167308Smarius		}
203167308Smarius	}
204167308Smarius
205172066Smarius	device_printf(dev, "board %d, ", board);
206143129Smarius	if (OF_getprop_alloc(node, "board-model", 1, (void **)&name) != -1) {
207143826Smarius		printf("model %s\n", name);
208143129Smarius		free(name, M_OFWPROP);
209143826Smarius	} else
210143826Smarius		printf("model unknown\n");
211143129Smarius
212111072Sjake	for (i = FHC_FANFAIL; i <= FHC_TOD; i++) {
213206018Smarius		bus_write_4(sc->sc_memres[i], FHC_ICLR, INTCLR_IDLE);
214172066Smarius		(void)bus_read_4(sc->sc_memres[i], FHC_ICLR);
215111072Sjake	}
216111072Sjake
217172066Smarius	sc->sc_ign = board << 1;
218172066Smarius	bus_write_4(sc->sc_memres[FHC_IGN], 0x0, sc->sc_ign);
219172066Smarius	sc->sc_ign = bus_read_4(sc->sc_memres[FHC_IGN], 0x0);
220111123Sjake
221172066Smarius	ctrl = bus_read_4(sc->sc_memres[FHC_INTERNAL], FHC_CTRL);
222172066Smarius	if (central == 0)
223111123Sjake		ctrl |= FHC_CTRL_IXIST;
224111123Sjake	ctrl &= ~(FHC_CTRL_AOFF | FHC_CTRL_BOFF | FHC_CTRL_SLINE);
225172066Smarius	bus_write_4(sc->sc_memres[FHC_INTERNAL], FHC_CTRL, ctrl);
226172066Smarius	(void)bus_read_4(sc->sc_memres[FHC_INTERNAL], FHC_CTRL);
227111123Sjake
228111072Sjake	sc->sc_nrange = OF_getprop_alloc(node, "ranges",
229111072Sjake	    sizeof(*sc->sc_ranges), (void **)&sc->sc_ranges);
230111072Sjake	if (sc->sc_nrange == -1) {
231167308Smarius		device_printf(dev, "cannot get ranges\n");
232167308Smarius		error = ENXIO;
233167308Smarius		goto fail_memres;
234111072Sjake	}
235111072Sjake
236172066Smarius	/*
237172066Smarius	 * Apparently only the interrupt controller of boards hanging off
238172066Smarius	 * of central(4) is indented to be used, otherwise we would have
239172066Smarius	 * conflicts registering the interrupt controllers for all FHC
240172066Smarius	 * boards as the board number and thus the IGN isn't unique.
241172066Smarius	 */
242172066Smarius	if (central == 1) {
243172066Smarius		/*
244172066Smarius		 * Hunt through all the interrupt mapping regs and register
245172066Smarius		 * our interrupt controller for the corresponding interrupt
246190098Smarius		 * vectors.  We do this early in order to be able to catch
247190098Smarius		 * stray interrupts.
248172066Smarius		 */
249172066Smarius		for (i = FHC_FANFAIL; i <= FHC_TOD; i++) {
250172066Smarius			fica = malloc(sizeof(*fica), M_DEVBUF, M_NOWAIT);
251172066Smarius			if (fica == NULL)
252172066Smarius				panic("%s: could not allocate interrupt "
253172066Smarius				    "controller argument", __func__);
254172066Smarius			fica->fica_sc = sc;
255172066Smarius			fica->fica_memres = sc->sc_memres[i];
256172066Smarius#ifdef FHC_DEBUG
257172066Smarius			device_printf(dev, "intr map %d: %#lx, clr: %#lx\n", i,
258172066Smarius			    (u_long)bus_read_4(fica->fica_memres, FHC_IMAP),
259172066Smarius			    (u_long)bus_read_4(fica->fica_memres, FHC_ICLR));
260172066Smarius#endif
261172066Smarius			/*
262172066Smarius			 * XXX we only pick the INO rather than the INR
263172066Smarius			 * from the IMR since the firmware may not provide
264172066Smarius			 * the IGN and the IGN is constant for all devices
265172066Smarius			 * on that FireHose controller.
266172066Smarius			 */
267190098Smarius			j = intr_controller_register(INTMAP_VEC(sc->sc_ign,
268172066Smarius			    INTINO(bus_read_4(fica->fica_memres, FHC_IMAP))),
269190098Smarius			    &fhc_ic, fica);
270190098Smarius			if (j != 0)
271190098Smarius				device_printf(dev, "could not register "
272190098Smarius				    "interrupt controller for map %d (%d)\n",
273190098Smarius				    i, j);
274172066Smarius		}
275172066Smarius	} else {
276172066Smarius		snprintf(ledname, sizeof(ledname), "board%d", board);
277143826Smarius		sc->sc_led_dev = led_create(fhc_led_func, sc, ledname);
278143826Smarius	}
279143826Smarius
280111072Sjake	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
281152684Smarius		fdi = malloc(sizeof(*fdi), M_DEVBUF, M_WAITOK | M_ZERO);
282152684Smarius		if (ofw_bus_gen_setup_devinfo(&fdi->fdi_obdinfo, child) != 0) {
283152684Smarius			free(fdi, M_DEVBUF);
284111072Sjake			continue;
285152684Smarius		}
286190098Smarius		i = OF_getprop_alloc(child, "reg", sizeof(*reg),
287152684Smarius		    (void **)&reg);
288190098Smarius		if (i == -1) {
289152684Smarius			device_printf(dev, "<%s>: incomplete\n",
290152684Smarius			    fdi->fdi_obdinfo.obd_name);
291152684Smarius			ofw_bus_gen_destroy_devinfo(&fdi->fdi_obdinfo);
292152684Smarius			free(fdi, M_DEVBUF);
293152684Smarius			continue;
294152684Smarius		}
295152684Smarius		resource_list_init(&fdi->fdi_rl);
296190098Smarius		for (j = 0; j < i; j++)
297190098Smarius			resource_list_add(&fdi->fdi_rl, SYS_RES_MEMORY, j,
298190098Smarius			    reg[j].sbr_offset, reg[j].sbr_offset +
299190098Smarius			    reg[j].sbr_size, reg[j].sbr_size);
300152684Smarius		free(reg, M_OFWPROP);
301172066Smarius		if (central == 1) {
302190098Smarius			i = OF_getprop_alloc(child, "interrupts",
303172066Smarius			    sizeof(*intr), (void **)&intr);
304190098Smarius			if (i != -1) {
305190098Smarius				for (j = 0; j < i; j++) {
306190098Smarius					iv = INTMAP_VEC(sc->sc_ign, intr[j]);
307172066Smarius					resource_list_add(&fdi->fdi_rl,
308190098Smarius					    SYS_RES_IRQ, j, iv, iv, 1);
309172066Smarius				}
310172066Smarius				free(intr, M_OFWPROP);
311152684Smarius			}
312152684Smarius		}
313111072Sjake		cdev = device_add_child(dev, NULL, -1);
314152684Smarius		if (cdev == NULL) {
315152684Smarius			device_printf(dev, "<%s>: device_add_child failed\n",
316152684Smarius			    fdi->fdi_obdinfo.obd_name);
317152684Smarius			resource_list_free(&fdi->fdi_rl);
318152684Smarius			ofw_bus_gen_destroy_devinfo(&fdi->fdi_obdinfo);
319152684Smarius			free(fdi, M_DEVBUF);
320152684Smarius			continue;
321152684Smarius		}
322152684Smarius		device_set_ivars(cdev, fdi);
323111072Sjake	}
324111072Sjake
325111072Sjake	return (bus_generic_attach(dev));
326167308Smarius
327167308Smarius fail_memres:
328167308Smarius	for (i = 0; i < FHC_NREG; i++)
329167308Smarius		if (sc->sc_memres[i] != NULL)
330167308Smarius			bus_release_resource(dev, SYS_RES_MEMORY,
331167308Smarius			    rman_get_rid(sc->sc_memres[i]), sc->sc_memres[i]);
332182070Smarius	return (error);
333111072Sjake}
334111072Sjake
335167308Smariusstatic int
336111072Sjakefhc_print_child(device_t dev, device_t child)
337111072Sjake{
338111072Sjake	int rv;
339111072Sjake
340111072Sjake	rv = bus_print_child_header(dev, child);
341152684Smarius	rv += fhc_print_res(device_get_ivars(child));
342111072Sjake	rv += bus_print_child_footer(dev, child);
343111072Sjake	return (rv);
344111072Sjake}
345111072Sjake
346167308Smariusstatic void
347111072Sjakefhc_probe_nomatch(device_t dev, device_t child)
348111072Sjake{
349152684Smarius	const char *type;
350111072Sjake
351152684Smarius	device_printf(dev, "<%s>", ofw_bus_get_name(child));
352152684Smarius	fhc_print_res(device_get_ivars(child));
353152684Smarius	type = ofw_bus_get_type(child);
354111072Sjake	printf(" type %s (no driver attached)\n",
355152684Smarius	    type != NULL ? type : "unknown");
356111072Sjake}
357111072Sjake
358172066Smariusstatic void
359172066Smariusfhc_intr_enable(void *arg)
360111072Sjake{
361172066Smarius	struct intr_vector *iv = arg;
362172066Smarius	struct fhc_icarg *fica = iv->iv_icarg;
363111072Sjake
364172066Smarius	bus_write_4(fica->fica_memres, FHC_IMAP,
365172066Smarius	    INTMAP_ENABLE(iv->iv_vec, iv->iv_mid));
366172066Smarius	(void)bus_read_4(fica->fica_memres, FHC_IMAP);
367111072Sjake}
368111072Sjake
369172066Smariusstatic void
370172066Smariusfhc_intr_disable(void *arg)
371111072Sjake{
372172066Smarius	struct intr_vector *iv = arg;
373172066Smarius	struct fhc_icarg *fica = iv->iv_icarg;
374111072Sjake
375172066Smarius	bus_write_4(fica->fica_memres, FHC_IMAP, iv->iv_vec);
376172066Smarius	(void)bus_read_4(fica->fica_memres, FHC_IMAP);
377111072Sjake}
378111072Sjake
379172066Smariusstatic void
380178443Smariusfhc_intr_assign(void *arg)
381170387Spiso{
382172066Smarius	struct intr_vector *iv = arg;
383172066Smarius	struct fhc_icarg *fica = iv->iv_icarg;
384170387Spiso
385178443Smarius	bus_write_4(fica->fica_memres, FHC_IMAP, INTMAP_TID(
386178443Smarius	    bus_read_4(fica->fica_memres, FHC_IMAP), iv->iv_mid));
387178443Smarius	(void)bus_read_4(fica->fica_memres, FHC_IMAP);
388178443Smarius}
389178443Smarius
390178443Smariusstatic void
391178443Smariusfhc_intr_clear(void *arg)
392178443Smarius{
393178443Smarius	struct intr_vector *iv = arg;
394178443Smarius	struct fhc_icarg *fica = iv->iv_icarg;
395178443Smarius
396206018Smarius	bus_write_4(fica->fica_memres, FHC_ICLR, INTCLR_IDLE);
397172066Smarius	(void)bus_read_4(fica->fica_memres, FHC_ICLR);
398170387Spiso}
399170387Spiso
400172066Smariusstatic int
401172066Smariusfhc_setup_intr(device_t bus, device_t child, struct resource *r, int flags,
402172066Smarius    driver_filter_t *filt, driver_intr_t *func, void *arg, void **cookiep)
403111123Sjake{
404172066Smarius	struct fhc_softc *sc;
405172066Smarius	u_long vec;
406111123Sjake
407172066Smarius	sc = device_get_softc(bus);
408172066Smarius	/*
409172066Smarius	 * Make sure the vector is fully specified and we registered
410172066Smarius	 * our interrupt controller for it.
411182070Smarius	 */
412172066Smarius	vec = rman_get_start(r);
413172066Smarius	if (INTIGN(vec) != sc->sc_ign || intr_vectors[vec].iv_ic != &fhc_ic) {
414172066Smarius		device_printf(bus, "invalid interrupt vector 0x%lx\n", vec);
415182070Smarius		return (EINVAL);
416182070Smarius	}
417172066Smarius	return (bus_generic_setup_intr(bus, child, r, flags, filt, func,
418172066Smarius	    arg, cookiep));
419111123Sjake}
420111123Sjake
421167308Smariusstatic struct resource *
422111072Sjakefhc_alloc_resource(device_t bus, device_t child, int type, int *rid,
423111072Sjake    u_long start, u_long end, u_long count, u_int flags)
424111072Sjake{
425143826Smarius	struct resource_list *rl;
426111072Sjake	struct resource_list_entry *rle;
427111072Sjake	struct fhc_softc *sc;
428111072Sjake	struct resource *res;
429111072Sjake	bus_addr_t coffset;
430111072Sjake	bus_addr_t cend;
431111072Sjake	bus_addr_t phys;
432111072Sjake	int isdefault;
433143826Smarius	int passthrough;
434111072Sjake	int i;
435111072Sjake
436111072Sjake	isdefault = (start == 0UL && end == ~0UL);
437143826Smarius	passthrough = (device_get_parent(child) != bus);
438111072Sjake	res = NULL;
439143826Smarius	rle = NULL;
440143826Smarius	rl = BUS_GET_RESOURCE_LIST(bus, child);
441111072Sjake	sc = device_get_softc(bus);
442111072Sjake	switch (type) {
443111072Sjake	case SYS_RES_IRQ:
444143826Smarius		return (resource_list_alloc(rl, bus, child, type, rid, start,
445143826Smarius		    end, count, flags));
446111072Sjake	case SYS_RES_MEMORY:
447143826Smarius		if (!passthrough) {
448172066Smarius			rle = resource_list_find(rl, type, *rid);
449143826Smarius			if (rle == NULL)
450143826Smarius				return (NULL);
451143826Smarius			if (rle->res != NULL)
452143826Smarius				panic("%s: resource entry is busy", __func__);
453143826Smarius			if (isdefault) {
454143826Smarius				start = rle->start;
455143826Smarius				count = ulmax(count, rle->count);
456143826Smarius				end = ulmax(rle->end, start + count - 1);
457143826Smarius			}
458143826Smarius		}
459111072Sjake		for (i = 0; i < sc->sc_nrange; i++) {
460111072Sjake			coffset = sc->sc_ranges[i].coffset;
461111072Sjake			cend = coffset + sc->sc_ranges[i].size - 1;
462111072Sjake			if (start >= coffset && end <= cend) {
463111072Sjake				start -= coffset;
464111072Sjake				end -= coffset;
465111072Sjake				phys = sc->sc_ranges[i].poffset |
466111072Sjake				    ((bus_addr_t)sc->sc_ranges[i].pspace << 32);
467111072Sjake				res = bus_generic_alloc_resource(bus, child,
468111072Sjake				    type, rid, phys + start, phys + end,
469111072Sjake				    count, flags);
470143826Smarius				if (!passthrough)
471143826Smarius					rle->res = res;
472111072Sjake				break;
473111072Sjake			}
474111072Sjake		}
475111072Sjake		break;
476111072Sjake	}
477111072Sjake	return (res);
478111072Sjake}
479111072Sjake
480225931Smariusstatic int
481225931Smariusfhc_adjust_resource(device_t bus __unused, device_t child __unused,
482225931Smarius    int type __unused, struct resource *r __unused, u_long start __unused,
483225931Smarius    u_long end __unused)
484225931Smarius{
485225931Smarius
486225931Smarius	return (ENXIO);
487225931Smarius}
488225931Smarius
489167308Smariusstatic struct resource_list *
490143826Smariusfhc_get_resource_list(device_t bus, device_t child)
491111072Sjake{
492111072Sjake	struct fhc_devinfo *fdi;
493111072Sjake
494111072Sjake	fdi = device_get_ivars(child);
495143826Smarius	return (&fdi->fdi_rl);
496111072Sjake}
497133589Smarius
498167308Smariusstatic const struct ofw_bus_devinfo *
499152684Smariusfhc_get_devinfo(device_t bus, device_t child)
500152684Smarius{
501152684Smarius	struct fhc_devinfo *fdi;
502152684Smarius
503152684Smarius	fdi = device_get_ivars(child);
504152684Smarius	return (&fdi->fdi_obdinfo);
505152684Smarius}
506152684Smarius
507143826Smariusstatic void
508143826Smariusfhc_led_func(void *arg, int onoff)
509143826Smarius{
510143826Smarius	struct fhc_softc *sc;
511143826Smarius	uint32_t ctrl;
512143826Smarius
513143826Smarius	sc = (struct fhc_softc *)arg;
514143826Smarius
515172066Smarius	ctrl = bus_read_4(sc->sc_memres[FHC_INTERNAL], FHC_CTRL);
516143826Smarius	if (onoff)
517143826Smarius		ctrl |= FHC_CTRL_RLED;
518143826Smarius	else
519143826Smarius		ctrl &= ~FHC_CTRL_RLED;
520143826Smarius	ctrl &= ~(FHC_CTRL_AOFF | FHC_CTRL_BOFF | FHC_CTRL_SLINE);
521172066Smarius	bus_write_4(sc->sc_memres[FHC_INTERNAL], FHC_CTRL, ctrl);
522172066Smarius	(void)bus_read_4(sc->sc_memres[FHC_INTERNAL], FHC_CTRL);
523143826Smarius}
524143826Smarius
525152684Smariusstatic int
526152684Smariusfhc_print_res(struct fhc_devinfo *fdi)
527133589Smarius{
528152684Smarius	int rv;
529133589Smarius
530152684Smarius	rv = 0;
531152684Smarius	rv += resource_list_print_type(&fdi->fdi_rl, "mem", SYS_RES_MEMORY,
532152684Smarius	    "%#lx");
533152684Smarius	rv += resource_list_print_type(&fdi->fdi_rl, "irq", SYS_RES_IRQ, "%ld");
534152684Smarius	return (rv);
535133589Smarius}
536