ofw_pci.c revision 230993
1230993Snwhitehorn/*-
2230993Snwhitehorn * Copyright (c) 2011 Nathan Whitehorn
3230993Snwhitehorn * All rights reserved.
4230993Snwhitehorn *
5230993Snwhitehorn * Redistribution and use in source and binary forms, with or without
6230993Snwhitehorn * modification, are permitted provided that the following conditions
7230993Snwhitehorn * are met:
8230993Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9230993Snwhitehorn *    notice, this list of conditions and the following disclaimer.
10230993Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
11230993Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
12230993Snwhitehorn *    documentation and/or other materials provided with the distribution.
13230993Snwhitehorn *
14230993Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15230993Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16230993Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17230993Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18230993Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19230993Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20230993Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21230993Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22230993Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23230993Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24230993Snwhitehorn * SUCH DAMAGE.
25230993Snwhitehorn */
26230993Snwhitehorn
27230993Snwhitehorn#include <sys/cdefs.h>
28230993Snwhitehorn__FBSDID("$FreeBSD: head/sys/powerpc/ofw/ofw_pci.c 230993 2012-02-04 19:54:13Z nwhitehorn $");
29230993Snwhitehorn#include <sys/param.h>
30230993Snwhitehorn#include <sys/systm.h>
31230993Snwhitehorn#include <sys/module.h>
32230993Snwhitehorn#include <sys/bus.h>
33230993Snwhitehorn#include <sys/conf.h>
34230993Snwhitehorn#include <sys/kernel.h>
35230993Snwhitehorn
36230993Snwhitehorn#include <dev/ofw/openfirm.h>
37230993Snwhitehorn#include <dev/ofw/ofw_pci.h>
38230993Snwhitehorn#include <dev/ofw/ofw_bus.h>
39230993Snwhitehorn#include <dev/ofw/ofw_bus_subr.h>
40230993Snwhitehorn
41230993Snwhitehorn#include <dev/pci/pcivar.h>
42230993Snwhitehorn#include <dev/pci/pcireg.h>
43230993Snwhitehorn
44230993Snwhitehorn#include <machine/bus.h>
45230993Snwhitehorn#include <machine/intr_machdep.h>
46230993Snwhitehorn#include <machine/md_var.h>
47230993Snwhitehorn#include <machine/pio.h>
48230993Snwhitehorn#include <machine/resource.h>
49230993Snwhitehorn
50230993Snwhitehorn#include <sys/rman.h>
51230993Snwhitehorn
52230993Snwhitehorn#include <vm/vm.h>
53230993Snwhitehorn#include <vm/pmap.h>
54230993Snwhitehorn
55230993Snwhitehorn#include <powerpc/ofw/ofw_pci.h>
56230993Snwhitehorn
57230993Snwhitehorn#include "pcib_if.h"
58230993Snwhitehorn
59230993Snwhitehorn/*
60230993Snwhitehorn * Bus interface.
61230993Snwhitehorn */
62230993Snwhitehornstatic int		ofw_pci_read_ivar(device_t, device_t, int,
63230993Snwhitehorn			    uintptr_t *);
64230993Snwhitehornstatic struct		resource * ofw_pci_alloc_resource(device_t bus,
65230993Snwhitehorn			    device_t child, int type, int *rid, u_long start,
66230993Snwhitehorn			    u_long end, u_long count, u_int flags);
67230993Snwhitehornstatic int		ofw_pci_release_resource(device_t bus, device_t child,
68230993Snwhitehorn    			    int type, int rid, struct resource *res);
69230993Snwhitehornstatic int		ofw_pci_activate_resource(device_t bus, device_t child,
70230993Snwhitehorn			    int type, int rid, struct resource *res);
71230993Snwhitehornstatic int		ofw_pci_deactivate_resource(device_t bus,
72230993Snwhitehorn    			    device_t child, int type, int rid,
73230993Snwhitehorn    			    struct resource *res);
74230993Snwhitehorn
75230993Snwhitehorn/*
76230993Snwhitehorn * pcib interface.
77230993Snwhitehorn */
78230993Snwhitehornstatic int		ofw_pci_maxslots(device_t);
79230993Snwhitehornstatic int		ofw_pci_route_interrupt(device_t, device_t, int);
80230993Snwhitehorn
81230993Snwhitehorn/*
82230993Snwhitehorn * ofw_bus interface
83230993Snwhitehorn */
84230993Snwhitehornstatic phandle_t ofw_pci_get_node(device_t bus, device_t dev);
85230993Snwhitehorn
86230993Snwhitehorn/*
87230993Snwhitehorn * local methods
88230993Snwhitehorn */
89230993Snwhitehorn
90230993Snwhitehornstatic int ofw_pci_nranges(phandle_t node);
91230993Snwhitehornstatic int ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges);
92230993Snwhitehorn
93230993Snwhitehorn/*
94230993Snwhitehorn * Driver methods.
95230993Snwhitehorn */
96230993Snwhitehornstatic device_method_t	ofw_pci_methods[] = {
97230993Snwhitehorn	/* Device interface */
98230993Snwhitehorn	DEVMETHOD(device_attach,	ofw_pci_attach),
99230993Snwhitehorn
100230993Snwhitehorn	/* Bus interface */
101230993Snwhitehorn	DEVMETHOD(bus_print_child,	bus_generic_print_child),
102230993Snwhitehorn	DEVMETHOD(bus_read_ivar,	ofw_pci_read_ivar),
103230993Snwhitehorn	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
104230993Snwhitehorn	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
105230993Snwhitehorn	DEVMETHOD(bus_alloc_resource,	ofw_pci_alloc_resource),
106230993Snwhitehorn	DEVMETHOD(bus_release_resource,	ofw_pci_release_resource),
107230993Snwhitehorn	DEVMETHOD(bus_activate_resource,	ofw_pci_activate_resource),
108230993Snwhitehorn	DEVMETHOD(bus_deactivate_resource,	ofw_pci_deactivate_resource),
109230993Snwhitehorn
110230993Snwhitehorn	/* pcib interface */
111230993Snwhitehorn	DEVMETHOD(pcib_maxslots,	ofw_pci_maxslots),
112230993Snwhitehorn	DEVMETHOD(pcib_route_interrupt,	ofw_pci_route_interrupt),
113230993Snwhitehorn
114230993Snwhitehorn	/* ofw_bus interface */
115230993Snwhitehorn	DEVMETHOD(ofw_bus_get_node,     ofw_pci_get_node),
116230993Snwhitehorn
117230993Snwhitehorn	DEVMETHOD_END
118230993Snwhitehorn};
119230993Snwhitehorn
120230993SnwhitehornDEFINE_CLASS_0(ofw_pci, ofw_pci_driver, ofw_pci_methods, 0);
121230993Snwhitehorn
122230993Snwhitehornint
123230993Snwhitehornofw_pci_attach(device_t dev)
124230993Snwhitehorn{
125230993Snwhitehorn	struct		ofw_pci_softc *sc;
126230993Snwhitehorn	phandle_t	node;
127230993Snwhitehorn	u_int32_t	busrange[2];
128230993Snwhitehorn	struct		ofw_pci_range *rp;
129230993Snwhitehorn	int		error;
130230993Snwhitehorn
131230993Snwhitehorn	node = ofw_bus_get_node(dev);
132230993Snwhitehorn	sc = device_get_softc(dev);
133230993Snwhitehorn
134230993Snwhitehorn	if (OF_getprop(node, "reg", &sc->sc_pcir, sizeof(sc->sc_pcir)) == -1)
135230993Snwhitehorn		return (ENXIO);
136230993Snwhitehorn
137230993Snwhitehorn	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
138230993Snwhitehorn		busrange[0] = 0;
139230993Snwhitehorn
140230993Snwhitehorn	sc->sc_dev = dev;
141230993Snwhitehorn	sc->sc_node = node;
142230993Snwhitehorn	sc->sc_bus = busrange[0];
143230993Snwhitehorn
144230993Snwhitehorn	if (sc->sc_quirks & OFW_PCI_QUIRK_RANGES_ON_CHILDREN) {
145230993Snwhitehorn		phandle_t c;
146230993Snwhitehorn		int n, i;
147230993Snwhitehorn
148230993Snwhitehorn		sc->sc_nrange = 0;
149230993Snwhitehorn		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
150230993Snwhitehorn			n = ofw_pci_nranges(c);
151230993Snwhitehorn			if (n > 0)
152230993Snwhitehorn				sc->sc_nrange += n;
153230993Snwhitehorn		}
154230993Snwhitehorn		if (sc->sc_nrange == 0)
155230993Snwhitehorn			return (ENXIO);
156230993Snwhitehorn		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
157230993Snwhitehorn		    M_DEVBUF, M_WAITOK);
158230993Snwhitehorn		i = 0;
159230993Snwhitehorn		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
160230993Snwhitehorn			n = ofw_pci_fill_ranges(c, &sc->sc_range[i]);
161230993Snwhitehorn			if (n > 0)
162230993Snwhitehorn				i += n;
163230993Snwhitehorn		}
164230993Snwhitehorn		KASSERT(i == sc->sc_nrange, ("range count mismatch"));
165230993Snwhitehorn	} else {
166230993Snwhitehorn		sc->sc_nrange = ofw_pci_nranges(node);
167230993Snwhitehorn		if (sc->sc_nrange <= 0) {
168230993Snwhitehorn			device_printf(dev, "could not get ranges\n");
169230993Snwhitehorn			return (ENXIO);
170230993Snwhitehorn		}
171230993Snwhitehorn		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
172230993Snwhitehorn		    M_DEVBUF, M_WAITOK);
173230993Snwhitehorn		ofw_pci_fill_ranges(node, sc->sc_range);
174230993Snwhitehorn	}
175230993Snwhitehorn
176230993Snwhitehorn	sc->sc_io_rman.rm_type = RMAN_ARRAY;
177230993Snwhitehorn	sc->sc_io_rman.rm_descr = "PCI I/O Ports";
178230993Snwhitehorn	error = rman_init(&sc->sc_io_rman);
179230993Snwhitehorn	if (error) {
180230993Snwhitehorn		device_printf(dev, "rman_init() failed. error = %d\n", error);
181230993Snwhitehorn		return (error);
182230993Snwhitehorn	}
183230993Snwhitehorn
184230993Snwhitehorn	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
185230993Snwhitehorn	sc->sc_mem_rman.rm_descr = "PCI Memory";
186230993Snwhitehorn	error = rman_init(&sc->sc_mem_rman);
187230993Snwhitehorn	if (error) {
188230993Snwhitehorn		device_printf(dev, "rman_init() failed. error = %d\n", error);
189230993Snwhitehorn		return (error);
190230993Snwhitehorn	}
191230993Snwhitehorn
192230993Snwhitehorn	for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
193230993Snwhitehorn	       rp->pci_hi != 0; rp++) {
194230993Snwhitehorn		error = 0;
195230993Snwhitehorn
196230993Snwhitehorn		switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
197230993Snwhitehorn		case OFW_PCI_PHYS_HI_SPACE_CONFIG:
198230993Snwhitehorn			break;
199230993Snwhitehorn		case OFW_PCI_PHYS_HI_SPACE_IO:
200230993Snwhitehorn			error = rman_manage_region(&sc->sc_io_rman, rp->pci,
201230993Snwhitehorn			    rp->pci + rp->size - 1);
202230993Snwhitehorn			break;
203230993Snwhitehorn		case OFW_PCI_PHYS_HI_SPACE_MEM32:
204230993Snwhitehorn		case OFW_PCI_PHYS_HI_SPACE_MEM64:
205230993Snwhitehorn			error = rman_manage_region(&sc->sc_mem_rman, rp->pci,
206230993Snwhitehorn			    rp->pci + rp->size - 1);
207230993Snwhitehorn			break;
208230993Snwhitehorn		}
209230993Snwhitehorn
210230993Snwhitehorn		if (error) {
211230993Snwhitehorn			device_printf(dev,
212230993Snwhitehorn			    "rman_manage_region(%x, %#jx, %#jx) failed. "
213230993Snwhitehorn			    "error = %d\n", rp->pci_hi &
214230993Snwhitehorn			    OFW_PCI_PHYS_HI_SPACEMASK, rp->pci,
215230993Snwhitehorn			    rp->pci + rp->size - 1, error);
216230993Snwhitehorn			panic("AHOY");
217230993Snwhitehorn			return (error);
218230993Snwhitehorn		}
219230993Snwhitehorn	}
220230993Snwhitehorn
221230993Snwhitehorn	ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
222230993Snwhitehorn
223230993Snwhitehorn	device_add_child(dev, "pci", device_get_unit(dev));
224230993Snwhitehorn	return (bus_generic_attach(dev));
225230993Snwhitehorn}
226230993Snwhitehorn
227230993Snwhitehornstatic int
228230993Snwhitehornofw_pci_maxslots(device_t dev)
229230993Snwhitehorn{
230230993Snwhitehorn
231230993Snwhitehorn	return (PCI_SLOTMAX);
232230993Snwhitehorn}
233230993Snwhitehorn
234230993Snwhitehornstatic int
235230993Snwhitehornofw_pci_route_interrupt(device_t bus, device_t dev, int pin)
236230993Snwhitehorn{
237230993Snwhitehorn	struct ofw_pci_softc *sc;
238230993Snwhitehorn	struct ofw_pci_register reg;
239230993Snwhitehorn	uint32_t pintr, mintr;
240230993Snwhitehorn	phandle_t iparent;
241230993Snwhitehorn	uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
242230993Snwhitehorn
243230993Snwhitehorn	sc = device_get_softc(bus);
244230993Snwhitehorn	pintr = pin;
245230993Snwhitehorn	if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, &reg,
246230993Snwhitehorn	    sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
247230993Snwhitehorn	    &iparent, maskbuf))
248230993Snwhitehorn		return (MAP_IRQ(iparent, mintr));
249230993Snwhitehorn
250230993Snwhitehorn	/* Maybe it's a real interrupt, not an intpin */
251230993Snwhitehorn	if (pin > 4)
252230993Snwhitehorn		return (pin);
253230993Snwhitehorn
254230993Snwhitehorn	device_printf(bus, "could not route pin %d for device %d.%d\n",
255230993Snwhitehorn	    pin, pci_get_slot(dev), pci_get_function(dev));
256230993Snwhitehorn	return (PCI_INVALID_IRQ);
257230993Snwhitehorn}
258230993Snwhitehorn
259230993Snwhitehornstatic int
260230993Snwhitehornofw_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
261230993Snwhitehorn{
262230993Snwhitehorn	struct	ofw_pci_softc *sc;
263230993Snwhitehorn
264230993Snwhitehorn	sc = device_get_softc(dev);
265230993Snwhitehorn
266230993Snwhitehorn	switch (which) {
267230993Snwhitehorn	case PCIB_IVAR_DOMAIN:
268230993Snwhitehorn		*result = device_get_unit(dev);
269230993Snwhitehorn		return (0);
270230993Snwhitehorn	case PCIB_IVAR_BUS:
271230993Snwhitehorn		*result = sc->sc_bus;
272230993Snwhitehorn		return (0);
273230993Snwhitehorn	}
274230993Snwhitehorn
275230993Snwhitehorn	return (ENOENT);
276230993Snwhitehorn}
277230993Snwhitehorn
278230993Snwhitehornstatic struct resource *
279230993Snwhitehornofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
280230993Snwhitehorn    u_long start, u_long end, u_long count, u_int flags)
281230993Snwhitehorn{
282230993Snwhitehorn	struct			ofw_pci_softc *sc;
283230993Snwhitehorn	struct			resource *rv;
284230993Snwhitehorn	struct			rman *rm;
285230993Snwhitehorn	int			needactivate;
286230993Snwhitehorn
287230993Snwhitehorn	needactivate = flags & RF_ACTIVE;
288230993Snwhitehorn	flags &= ~RF_ACTIVE;
289230993Snwhitehorn
290230993Snwhitehorn	sc = device_get_softc(bus);
291230993Snwhitehorn
292230993Snwhitehorn	switch (type) {
293230993Snwhitehorn	case SYS_RES_MEMORY:
294230993Snwhitehorn		rm = &sc->sc_mem_rman;
295230993Snwhitehorn		break;
296230993Snwhitehorn
297230993Snwhitehorn	case SYS_RES_IOPORT:
298230993Snwhitehorn		rm = &sc->sc_io_rman;
299230993Snwhitehorn		break;
300230993Snwhitehorn
301230993Snwhitehorn	case SYS_RES_IRQ:
302230993Snwhitehorn		return (bus_alloc_resource(bus, type, rid, start, end, count,
303230993Snwhitehorn		    flags));
304230993Snwhitehorn
305230993Snwhitehorn	default:
306230993Snwhitehorn		device_printf(bus, "unknown resource request from %s\n",
307230993Snwhitehorn		    device_get_nameunit(child));
308230993Snwhitehorn		return (NULL);
309230993Snwhitehorn	}
310230993Snwhitehorn
311230993Snwhitehorn	rv = rman_reserve_resource(rm, start, end, count, flags, child);
312230993Snwhitehorn	if (rv == NULL) {
313230993Snwhitehorn		device_printf(bus, "failed to reserve resource for %s\n",
314230993Snwhitehorn		    device_get_nameunit(child));
315230993Snwhitehorn		return (NULL);
316230993Snwhitehorn	}
317230993Snwhitehorn
318230993Snwhitehorn	rman_set_rid(rv, *rid);
319230993Snwhitehorn
320230993Snwhitehorn	if (needactivate) {
321230993Snwhitehorn		if (bus_activate_resource(child, type, *rid, rv) != 0) {
322230993Snwhitehorn			device_printf(bus,
323230993Snwhitehorn			    "failed to activate resource for %s\n",
324230993Snwhitehorn			    device_get_nameunit(child));
325230993Snwhitehorn			rman_release_resource(rv);
326230993Snwhitehorn			return (NULL);
327230993Snwhitehorn		}
328230993Snwhitehorn	}
329230993Snwhitehorn
330230993Snwhitehorn	return (rv);
331230993Snwhitehorn}
332230993Snwhitehorn
333230993Snwhitehornstatic int
334230993Snwhitehornofw_pci_release_resource(device_t bus, device_t child, int type, int rid,
335230993Snwhitehorn    struct resource *res)
336230993Snwhitehorn{
337230993Snwhitehorn	if (rman_get_flags(res) & RF_ACTIVE) {
338230993Snwhitehorn		int error = bus_deactivate_resource(child, type, rid, res);
339230993Snwhitehorn		if (error)
340230993Snwhitehorn			return error;
341230993Snwhitehorn	}
342230993Snwhitehorn
343230993Snwhitehorn	return (rman_release_resource(res));
344230993Snwhitehorn}
345230993Snwhitehorn
346230993Snwhitehornstatic int
347230993Snwhitehornofw_pci_activate_resource(device_t bus, device_t child, int type, int rid,
348230993Snwhitehorn    struct resource *res)
349230993Snwhitehorn{
350230993Snwhitehorn	struct ofw_pci_softc *sc;
351230993Snwhitehorn	void	*p;
352230993Snwhitehorn
353230993Snwhitehorn	sc = device_get_softc(bus);
354230993Snwhitehorn
355230993Snwhitehorn	if (type == SYS_RES_IRQ) {
356230993Snwhitehorn		return (bus_activate_resource(bus, type, rid, res));
357230993Snwhitehorn	}
358230993Snwhitehorn	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
359230993Snwhitehorn		struct ofw_pci_range *rp;
360230993Snwhitehorn		vm_offset_t start;
361230993Snwhitehorn		int space;
362230993Snwhitehorn
363230993Snwhitehorn		start = (vm_offset_t)rman_get_start(res);
364230993Snwhitehorn
365230993Snwhitehorn		/*
366230993Snwhitehorn		 * Map this through the ranges list
367230993Snwhitehorn		 */
368230993Snwhitehorn		for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
369230993Snwhitehorn		       rp->pci_hi != 0; rp++) {
370230993Snwhitehorn			if (start < rp->pci || start >= rp->pci + rp->size)
371230993Snwhitehorn				continue;
372230993Snwhitehorn
373230993Snwhitehorn			switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
374230993Snwhitehorn			case OFW_PCI_PHYS_HI_SPACE_IO:
375230993Snwhitehorn				space = SYS_RES_IOPORT;
376230993Snwhitehorn				break;
377230993Snwhitehorn			case OFW_PCI_PHYS_HI_SPACE_MEM32:
378230993Snwhitehorn			case OFW_PCI_PHYS_HI_SPACE_MEM64:
379230993Snwhitehorn				space = SYS_RES_MEMORY;
380230993Snwhitehorn				break;
381230993Snwhitehorn			default:
382230993Snwhitehorn				space = -1;
383230993Snwhitehorn			}
384230993Snwhitehorn
385230993Snwhitehorn			if (type == space) {
386230993Snwhitehorn				start += (rp->host - rp->pci);
387230993Snwhitehorn				break;
388230993Snwhitehorn			}
389230993Snwhitehorn		}
390230993Snwhitehorn
391230993Snwhitehorn		if (bootverbose)
392230993Snwhitehorn			printf("ofw_pci mapdev: start %zx, len %ld\n", start,
393230993Snwhitehorn			    rman_get_size(res));
394230993Snwhitehorn
395230993Snwhitehorn		p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
396230993Snwhitehorn		if (p == NULL)
397230993Snwhitehorn			return (ENOMEM);
398230993Snwhitehorn
399230993Snwhitehorn		rman_set_virtual(res, p);
400230993Snwhitehorn		rman_set_bustag(res, &bs_le_tag);
401230993Snwhitehorn		rman_set_bushandle(res, (u_long)p);
402230993Snwhitehorn	}
403230993Snwhitehorn
404230993Snwhitehorn	return (rman_activate_resource(res));
405230993Snwhitehorn}
406230993Snwhitehorn
407230993Snwhitehornstatic int
408230993Snwhitehornofw_pci_deactivate_resource(device_t bus, device_t child, int type, int rid,
409230993Snwhitehorn    struct resource *res)
410230993Snwhitehorn{
411230993Snwhitehorn	/*
412230993Snwhitehorn	 * If this is a memory resource, unmap it.
413230993Snwhitehorn	 */
414230993Snwhitehorn	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
415230993Snwhitehorn		u_int32_t psize;
416230993Snwhitehorn
417230993Snwhitehorn		psize = rman_get_size(res);
418230993Snwhitehorn		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
419230993Snwhitehorn	}
420230993Snwhitehorn
421230993Snwhitehorn	return (rman_deactivate_resource(res));
422230993Snwhitehorn}
423230993Snwhitehorn
424230993Snwhitehornstatic phandle_t
425230993Snwhitehornofw_pci_get_node(device_t bus, device_t dev)
426230993Snwhitehorn{
427230993Snwhitehorn	struct ofw_pci_softc *sc;
428230993Snwhitehorn
429230993Snwhitehorn	sc = device_get_softc(bus);
430230993Snwhitehorn	/* We only have one child, the PCI bus, which needs our own node. */
431230993Snwhitehorn
432230993Snwhitehorn	return (sc->sc_node);
433230993Snwhitehorn}
434230993Snwhitehorn
435230993Snwhitehornstatic int
436230993Snwhitehornofw_pci_nranges(phandle_t node)
437230993Snwhitehorn{
438230993Snwhitehorn	int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
439230993Snwhitehorn	ssize_t nbase_ranges;
440230993Snwhitehorn
441230993Snwhitehorn	OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
442230993Snwhitehorn	    sizeof(host_address_cells));
443230993Snwhitehorn	OF_getprop(node, "#address-cells", &pci_address_cells,
444230993Snwhitehorn	    sizeof(pci_address_cells));
445230993Snwhitehorn	OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
446230993Snwhitehorn
447230993Snwhitehorn	nbase_ranges = OF_getproplen(node, "ranges");
448230993Snwhitehorn	if (nbase_ranges <= 0)
449230993Snwhitehorn		return (-1);
450230993Snwhitehorn
451230993Snwhitehorn	return (nbase_ranges / sizeof(cell_t) /
452230993Snwhitehorn	    (pci_address_cells + host_address_cells + size_cells));
453230993Snwhitehorn}
454230993Snwhitehorn
455230993Snwhitehornstatic int
456230993Snwhitehornofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges)
457230993Snwhitehorn{
458230993Snwhitehorn	int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
459230993Snwhitehorn	cell_t *base_ranges;
460230993Snwhitehorn	ssize_t nbase_ranges;
461230993Snwhitehorn	int nranges;
462230993Snwhitehorn	int i, j, k;
463230993Snwhitehorn
464230993Snwhitehorn	OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
465230993Snwhitehorn	    sizeof(host_address_cells));
466230993Snwhitehorn	OF_getprop(node, "#address-cells", &pci_address_cells,
467230993Snwhitehorn	    sizeof(pci_address_cells));
468230993Snwhitehorn	OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
469230993Snwhitehorn
470230993Snwhitehorn	nbase_ranges = OF_getproplen(node, "ranges");
471230993Snwhitehorn	if (nbase_ranges <= 0)
472230993Snwhitehorn		return (-1);
473230993Snwhitehorn	nranges = nbase_ranges / sizeof(cell_t) /
474230993Snwhitehorn	    (pci_address_cells + host_address_cells + size_cells);
475230993Snwhitehorn
476230993Snwhitehorn	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
477230993Snwhitehorn	OF_getprop(node, "ranges", base_ranges, nbase_ranges);
478230993Snwhitehorn
479230993Snwhitehorn	for (i = 0, j = 0; i < nranges; i++) {
480230993Snwhitehorn		ranges[i].pci_hi = base_ranges[j++];
481230993Snwhitehorn		ranges[i].pci = 0;
482230993Snwhitehorn		for (k = 0; k < pci_address_cells - 1; k++) {
483230993Snwhitehorn			ranges[i].pci <<= 32;
484230993Snwhitehorn			ranges[i].pci |= base_ranges[j++];
485230993Snwhitehorn		}
486230993Snwhitehorn		ranges[i].host = 0;
487230993Snwhitehorn		for (k = 0; k < host_address_cells; k++) {
488230993Snwhitehorn			ranges[i].host <<= 32;
489230993Snwhitehorn			ranges[i].host |= base_ranges[j++];
490230993Snwhitehorn		}
491230993Snwhitehorn		ranges[i].size = 0;
492230993Snwhitehorn		for (k = 0; k < size_cells; k++) {
493230993Snwhitehorn			ranges[i].size <<= 32;
494230993Snwhitehorn			ranges[i].size |= base_ranges[j++];
495230993Snwhitehorn		}
496230993Snwhitehorn	}
497230993Snwhitehorn
498230993Snwhitehorn	free(base_ranges, M_DEVBUF);
499230993Snwhitehorn	return (nranges);
500230993Snwhitehorn}
501230993Snwhitehorn
502