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$");
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);
74231003Snwhitehornstatic int		ofw_pci_adjust_resource(device_t bus, device_t child,
75231003Snwhitehorn			    int type, struct resource *res, u_long start,
76231003Snwhitehorn			    u_long end);
77230993Snwhitehorn
78230993Snwhitehorn/*
79230993Snwhitehorn * pcib interface.
80230993Snwhitehorn */
81230993Snwhitehornstatic int		ofw_pci_maxslots(device_t);
82230993Snwhitehornstatic int		ofw_pci_route_interrupt(device_t, device_t, int);
83230993Snwhitehorn
84230993Snwhitehorn/*
85230993Snwhitehorn * ofw_bus interface
86230993Snwhitehorn */
87230993Snwhitehornstatic phandle_t ofw_pci_get_node(device_t bus, device_t dev);
88230993Snwhitehorn
89230993Snwhitehorn/*
90230993Snwhitehorn * local methods
91230993Snwhitehorn */
92230993Snwhitehorn
93230993Snwhitehornstatic int ofw_pci_nranges(phandle_t node);
94230993Snwhitehornstatic int ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges);
95230993Snwhitehorn
96230993Snwhitehorn/*
97230993Snwhitehorn * Driver methods.
98230993Snwhitehorn */
99230993Snwhitehornstatic device_method_t	ofw_pci_methods[] = {
100230993Snwhitehorn	/* Device interface */
101230993Snwhitehorn	DEVMETHOD(device_attach,	ofw_pci_attach),
102230993Snwhitehorn
103230993Snwhitehorn	/* Bus interface */
104230993Snwhitehorn	DEVMETHOD(bus_print_child,	bus_generic_print_child),
105230993Snwhitehorn	DEVMETHOD(bus_read_ivar,	ofw_pci_read_ivar),
106230993Snwhitehorn	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
107230993Snwhitehorn	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
108230993Snwhitehorn	DEVMETHOD(bus_alloc_resource,	ofw_pci_alloc_resource),
109230993Snwhitehorn	DEVMETHOD(bus_release_resource,	ofw_pci_release_resource),
110230993Snwhitehorn	DEVMETHOD(bus_activate_resource,	ofw_pci_activate_resource),
111230993Snwhitehorn	DEVMETHOD(bus_deactivate_resource,	ofw_pci_deactivate_resource),
112231003Snwhitehorn	DEVMETHOD(bus_adjust_resource,	ofw_pci_adjust_resource),
113230993Snwhitehorn
114230993Snwhitehorn	/* pcib interface */
115230993Snwhitehorn	DEVMETHOD(pcib_maxslots,	ofw_pci_maxslots),
116230993Snwhitehorn	DEVMETHOD(pcib_route_interrupt,	ofw_pci_route_interrupt),
117230993Snwhitehorn
118230993Snwhitehorn	/* ofw_bus interface */
119230993Snwhitehorn	DEVMETHOD(ofw_bus_get_node,     ofw_pci_get_node),
120230993Snwhitehorn
121230993Snwhitehorn	DEVMETHOD_END
122230993Snwhitehorn};
123230993Snwhitehorn
124230993SnwhitehornDEFINE_CLASS_0(ofw_pci, ofw_pci_driver, ofw_pci_methods, 0);
125230993Snwhitehorn
126230993Snwhitehornint
127265954Sianofw_pci_init(device_t dev)
128230993Snwhitehorn{
129230993Snwhitehorn	struct		ofw_pci_softc *sc;
130230993Snwhitehorn	phandle_t	node;
131230993Snwhitehorn	u_int32_t	busrange[2];
132230993Snwhitehorn	struct		ofw_pci_range *rp;
133230993Snwhitehorn	int		error;
134230993Snwhitehorn
135230993Snwhitehorn	node = ofw_bus_get_node(dev);
136230993Snwhitehorn	sc = device_get_softc(dev);
137265954Sian	sc->sc_initialized = 1;
138230993Snwhitehorn
139230993Snwhitehorn	if (OF_getprop(node, "reg", &sc->sc_pcir, sizeof(sc->sc_pcir)) == -1)
140230993Snwhitehorn		return (ENXIO);
141230993Snwhitehorn
142230993Snwhitehorn	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
143230993Snwhitehorn		busrange[0] = 0;
144230993Snwhitehorn
145230993Snwhitehorn	sc->sc_dev = dev;
146230993Snwhitehorn	sc->sc_node = node;
147230993Snwhitehorn	sc->sc_bus = busrange[0];
148230993Snwhitehorn
149230993Snwhitehorn	if (sc->sc_quirks & OFW_PCI_QUIRK_RANGES_ON_CHILDREN) {
150230993Snwhitehorn		phandle_t c;
151230993Snwhitehorn		int n, i;
152230993Snwhitehorn
153230993Snwhitehorn		sc->sc_nrange = 0;
154230993Snwhitehorn		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
155230993Snwhitehorn			n = ofw_pci_nranges(c);
156230993Snwhitehorn			if (n > 0)
157230993Snwhitehorn				sc->sc_nrange += n;
158230993Snwhitehorn		}
159230993Snwhitehorn		if (sc->sc_nrange == 0)
160230993Snwhitehorn			return (ENXIO);
161230993Snwhitehorn		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
162230993Snwhitehorn		    M_DEVBUF, M_WAITOK);
163230993Snwhitehorn		i = 0;
164230993Snwhitehorn		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
165230993Snwhitehorn			n = ofw_pci_fill_ranges(c, &sc->sc_range[i]);
166230993Snwhitehorn			if (n > 0)
167230993Snwhitehorn				i += n;
168230993Snwhitehorn		}
169230993Snwhitehorn		KASSERT(i == sc->sc_nrange, ("range count mismatch"));
170230993Snwhitehorn	} else {
171230993Snwhitehorn		sc->sc_nrange = ofw_pci_nranges(node);
172230993Snwhitehorn		if (sc->sc_nrange <= 0) {
173230993Snwhitehorn			device_printf(dev, "could not get ranges\n");
174230993Snwhitehorn			return (ENXIO);
175230993Snwhitehorn		}
176230993Snwhitehorn		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
177230993Snwhitehorn		    M_DEVBUF, M_WAITOK);
178230993Snwhitehorn		ofw_pci_fill_ranges(node, sc->sc_range);
179230993Snwhitehorn	}
180230993Snwhitehorn
181230993Snwhitehorn	sc->sc_io_rman.rm_type = RMAN_ARRAY;
182230993Snwhitehorn	sc->sc_io_rman.rm_descr = "PCI I/O Ports";
183230993Snwhitehorn	error = rman_init(&sc->sc_io_rman);
184230993Snwhitehorn	if (error) {
185230993Snwhitehorn		device_printf(dev, "rman_init() failed. error = %d\n", error);
186230993Snwhitehorn		return (error);
187230993Snwhitehorn	}
188230993Snwhitehorn
189230993Snwhitehorn	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
190230993Snwhitehorn	sc->sc_mem_rman.rm_descr = "PCI Memory";
191230993Snwhitehorn	error = rman_init(&sc->sc_mem_rman);
192230993Snwhitehorn	if (error) {
193230993Snwhitehorn		device_printf(dev, "rman_init() failed. error = %d\n", error);
194230993Snwhitehorn		return (error);
195230993Snwhitehorn	}
196230993Snwhitehorn
197230993Snwhitehorn	for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
198230993Snwhitehorn	       rp->pci_hi != 0; rp++) {
199230993Snwhitehorn		error = 0;
200230993Snwhitehorn
201230993Snwhitehorn		switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
202230993Snwhitehorn		case OFW_PCI_PHYS_HI_SPACE_CONFIG:
203230993Snwhitehorn			break;
204230993Snwhitehorn		case OFW_PCI_PHYS_HI_SPACE_IO:
205230993Snwhitehorn			error = rman_manage_region(&sc->sc_io_rman, rp->pci,
206230993Snwhitehorn			    rp->pci + rp->size - 1);
207230993Snwhitehorn			break;
208230993Snwhitehorn		case OFW_PCI_PHYS_HI_SPACE_MEM32:
209230993Snwhitehorn		case OFW_PCI_PHYS_HI_SPACE_MEM64:
210230993Snwhitehorn			error = rman_manage_region(&sc->sc_mem_rman, rp->pci,
211230993Snwhitehorn			    rp->pci + rp->size - 1);
212230993Snwhitehorn			break;
213230993Snwhitehorn		}
214230993Snwhitehorn
215230993Snwhitehorn		if (error) {
216230993Snwhitehorn			device_printf(dev,
217230993Snwhitehorn			    "rman_manage_region(%x, %#jx, %#jx) failed. "
218230993Snwhitehorn			    "error = %d\n", rp->pci_hi &
219230993Snwhitehorn			    OFW_PCI_PHYS_HI_SPACEMASK, rp->pci,
220230993Snwhitehorn			    rp->pci + rp->size - 1, error);
221230993Snwhitehorn			return (error);
222230993Snwhitehorn		}
223230993Snwhitehorn	}
224230993Snwhitehorn
225230993Snwhitehorn	ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
226230993Snwhitehorn
227265954Sian	return (error);
228265954Sian}
229265954Sian
230265954Sianint
231265954Sianofw_pci_attach(device_t dev)
232265954Sian{
233265954Sian	struct ofw_pci_softc *sc;
234265954Sian	int error;
235265954Sian
236265954Sian	sc = device_get_softc(dev);
237265954Sian	if (!sc->sc_initialized) {
238265954Sian		error = ofw_pci_init(dev);
239265954Sian		if (error)
240265954Sian			return (error);
241265954Sian	}
242265954Sian
243230993Snwhitehorn	device_add_child(dev, "pci", device_get_unit(dev));
244230993Snwhitehorn	return (bus_generic_attach(dev));
245230993Snwhitehorn}
246230993Snwhitehorn
247230993Snwhitehornstatic int
248230993Snwhitehornofw_pci_maxslots(device_t dev)
249230993Snwhitehorn{
250230993Snwhitehorn
251230993Snwhitehorn	return (PCI_SLOTMAX);
252230993Snwhitehorn}
253230993Snwhitehorn
254230993Snwhitehornstatic int
255230993Snwhitehornofw_pci_route_interrupt(device_t bus, device_t dev, int pin)
256230993Snwhitehorn{
257230993Snwhitehorn	struct ofw_pci_softc *sc;
258230993Snwhitehorn	struct ofw_pci_register reg;
259266020Sian	uint32_t pintr, mintr[2];
260266020Sian	int intrcells;
261230993Snwhitehorn	phandle_t iparent;
262230993Snwhitehorn
263230993Snwhitehorn	sc = device_get_softc(bus);
264230993Snwhitehorn	pintr = pin;
265265954Sian
266265954Sian	/* Fabricate imap information in case this isn't an OFW device */
267265954Sian	bzero(&reg, sizeof(reg));
268265954Sian	reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) |
269265954Sian	    (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) |
270265954Sian	    (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT);
271265954Sian
272266020Sian	intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev),
273266020Sian	    &sc->sc_pci_iinfo, &reg, sizeof(reg), &pintr, sizeof(pintr),
274266020Sian	    mintr, sizeof(mintr), &iparent);
275266020Sian	if (intrcells) {
276266128Sian		pintr = ofw_bus_map_intr(dev, iparent, intrcells, mintr);
277266020Sian		return (pintr);
278266020Sian	}
279230993Snwhitehorn
280230993Snwhitehorn	/* Maybe it's a real interrupt, not an intpin */
281230993Snwhitehorn	if (pin > 4)
282230993Snwhitehorn		return (pin);
283230993Snwhitehorn
284230993Snwhitehorn	device_printf(bus, "could not route pin %d for device %d.%d\n",
285230993Snwhitehorn	    pin, pci_get_slot(dev), pci_get_function(dev));
286230993Snwhitehorn	return (PCI_INVALID_IRQ);
287230993Snwhitehorn}
288230993Snwhitehorn
289230993Snwhitehornstatic int
290230993Snwhitehornofw_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
291230993Snwhitehorn{
292230993Snwhitehorn	struct	ofw_pci_softc *sc;
293230993Snwhitehorn
294230993Snwhitehorn	sc = device_get_softc(dev);
295230993Snwhitehorn
296230993Snwhitehorn	switch (which) {
297230993Snwhitehorn	case PCIB_IVAR_DOMAIN:
298230993Snwhitehorn		*result = device_get_unit(dev);
299230993Snwhitehorn		return (0);
300230993Snwhitehorn	case PCIB_IVAR_BUS:
301230993Snwhitehorn		*result = sc->sc_bus;
302230993Snwhitehorn		return (0);
303230993Snwhitehorn	}
304230993Snwhitehorn
305230993Snwhitehorn	return (ENOENT);
306230993Snwhitehorn}
307230993Snwhitehorn
308230993Snwhitehornstatic struct resource *
309230993Snwhitehornofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
310230993Snwhitehorn    u_long start, u_long end, u_long count, u_int flags)
311230993Snwhitehorn{
312230993Snwhitehorn	struct			ofw_pci_softc *sc;
313230993Snwhitehorn	struct			resource *rv;
314230993Snwhitehorn	struct			rman *rm;
315230993Snwhitehorn	int			needactivate;
316230993Snwhitehorn
317230993Snwhitehorn	needactivate = flags & RF_ACTIVE;
318230993Snwhitehorn	flags &= ~RF_ACTIVE;
319230993Snwhitehorn
320230993Snwhitehorn	sc = device_get_softc(bus);
321230993Snwhitehorn
322230993Snwhitehorn	switch (type) {
323230993Snwhitehorn	case SYS_RES_MEMORY:
324230993Snwhitehorn		rm = &sc->sc_mem_rman;
325230993Snwhitehorn		break;
326230993Snwhitehorn
327230993Snwhitehorn	case SYS_RES_IOPORT:
328230993Snwhitehorn		rm = &sc->sc_io_rman;
329230993Snwhitehorn		break;
330230993Snwhitehorn
331230993Snwhitehorn	case SYS_RES_IRQ:
332230993Snwhitehorn		return (bus_alloc_resource(bus, type, rid, start, end, count,
333230993Snwhitehorn		    flags));
334230993Snwhitehorn
335230993Snwhitehorn	default:
336230993Snwhitehorn		device_printf(bus, "unknown resource request from %s\n",
337230993Snwhitehorn		    device_get_nameunit(child));
338230993Snwhitehorn		return (NULL);
339230993Snwhitehorn	}
340230993Snwhitehorn
341230993Snwhitehorn	rv = rman_reserve_resource(rm, start, end, count, flags, child);
342230993Snwhitehorn	if (rv == NULL) {
343230993Snwhitehorn		device_printf(bus, "failed to reserve resource for %s\n",
344230993Snwhitehorn		    device_get_nameunit(child));
345230993Snwhitehorn		return (NULL);
346230993Snwhitehorn	}
347230993Snwhitehorn
348230993Snwhitehorn	rman_set_rid(rv, *rid);
349230993Snwhitehorn
350230993Snwhitehorn	if (needactivate) {
351230993Snwhitehorn		if (bus_activate_resource(child, type, *rid, rv) != 0) {
352230993Snwhitehorn			device_printf(bus,
353230993Snwhitehorn			    "failed to activate resource for %s\n",
354230993Snwhitehorn			    device_get_nameunit(child));
355230993Snwhitehorn			rman_release_resource(rv);
356230993Snwhitehorn			return (NULL);
357230993Snwhitehorn		}
358230993Snwhitehorn	}
359230993Snwhitehorn
360230993Snwhitehorn	return (rv);
361230993Snwhitehorn}
362230993Snwhitehorn
363230993Snwhitehornstatic int
364230993Snwhitehornofw_pci_release_resource(device_t bus, device_t child, int type, int rid,
365230993Snwhitehorn    struct resource *res)
366230993Snwhitehorn{
367230993Snwhitehorn	if (rman_get_flags(res) & RF_ACTIVE) {
368230993Snwhitehorn		int error = bus_deactivate_resource(child, type, rid, res);
369230993Snwhitehorn		if (error)
370230993Snwhitehorn			return error;
371230993Snwhitehorn	}
372230993Snwhitehorn
373230993Snwhitehorn	return (rman_release_resource(res));
374230993Snwhitehorn}
375230993Snwhitehorn
376230993Snwhitehornstatic int
377230993Snwhitehornofw_pci_activate_resource(device_t bus, device_t child, int type, int rid,
378230993Snwhitehorn    struct resource *res)
379230993Snwhitehorn{
380230993Snwhitehorn	struct ofw_pci_softc *sc;
381230993Snwhitehorn	void	*p;
382230993Snwhitehorn
383230993Snwhitehorn	sc = device_get_softc(bus);
384230993Snwhitehorn
385230993Snwhitehorn	if (type == SYS_RES_IRQ) {
386230993Snwhitehorn		return (bus_activate_resource(bus, type, rid, res));
387230993Snwhitehorn	}
388230993Snwhitehorn	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
389230993Snwhitehorn		struct ofw_pci_range *rp;
390230993Snwhitehorn		vm_offset_t start;
391230993Snwhitehorn		int space;
392230993Snwhitehorn
393230993Snwhitehorn		start = (vm_offset_t)rman_get_start(res);
394230993Snwhitehorn
395230993Snwhitehorn		/*
396230993Snwhitehorn		 * Map this through the ranges list
397230993Snwhitehorn		 */
398230993Snwhitehorn		for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
399230993Snwhitehorn		       rp->pci_hi != 0; rp++) {
400230993Snwhitehorn			if (start < rp->pci || start >= rp->pci + rp->size)
401230993Snwhitehorn				continue;
402230993Snwhitehorn
403230993Snwhitehorn			switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
404230993Snwhitehorn			case OFW_PCI_PHYS_HI_SPACE_IO:
405230993Snwhitehorn				space = SYS_RES_IOPORT;
406230993Snwhitehorn				break;
407230993Snwhitehorn			case OFW_PCI_PHYS_HI_SPACE_MEM32:
408230993Snwhitehorn			case OFW_PCI_PHYS_HI_SPACE_MEM64:
409230993Snwhitehorn				space = SYS_RES_MEMORY;
410230993Snwhitehorn				break;
411230993Snwhitehorn			default:
412230993Snwhitehorn				space = -1;
413230993Snwhitehorn			}
414230993Snwhitehorn
415230993Snwhitehorn			if (type == space) {
416230993Snwhitehorn				start += (rp->host - rp->pci);
417230993Snwhitehorn				break;
418230993Snwhitehorn			}
419230993Snwhitehorn		}
420230993Snwhitehorn
421230993Snwhitehorn		if (bootverbose)
422230993Snwhitehorn			printf("ofw_pci mapdev: start %zx, len %ld\n", start,
423230993Snwhitehorn			    rman_get_size(res));
424230993Snwhitehorn
425230993Snwhitehorn		p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
426230993Snwhitehorn		if (p == NULL)
427230993Snwhitehorn			return (ENOMEM);
428230993Snwhitehorn
429230993Snwhitehorn		rman_set_virtual(res, p);
430230993Snwhitehorn		rman_set_bustag(res, &bs_le_tag);
431230993Snwhitehorn		rman_set_bushandle(res, (u_long)p);
432230993Snwhitehorn	}
433230993Snwhitehorn
434230993Snwhitehorn	return (rman_activate_resource(res));
435230993Snwhitehorn}
436230993Snwhitehorn
437230993Snwhitehornstatic int
438230993Snwhitehornofw_pci_deactivate_resource(device_t bus, device_t child, int type, int rid,
439230993Snwhitehorn    struct resource *res)
440230993Snwhitehorn{
441230993Snwhitehorn	/*
442230993Snwhitehorn	 * If this is a memory resource, unmap it.
443230993Snwhitehorn	 */
444230993Snwhitehorn	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
445230993Snwhitehorn		u_int32_t psize;
446230993Snwhitehorn
447230993Snwhitehorn		psize = rman_get_size(res);
448230993Snwhitehorn		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
449230993Snwhitehorn	}
450230993Snwhitehorn
451230993Snwhitehorn	return (rman_deactivate_resource(res));
452230993Snwhitehorn}
453230993Snwhitehorn
454231003Snwhitehornstatic int
455231003Snwhitehornofw_pci_adjust_resource(device_t bus, device_t child, int type,
456231003Snwhitehorn    struct resource *res, u_long start, u_long end)
457231003Snwhitehorn{
458231003Snwhitehorn	struct rman *rm = NULL;
459231003Snwhitehorn	struct ofw_pci_softc *sc = device_get_softc(bus);
460231003Snwhitehorn
461231149Snwhitehorn	KASSERT(!(rman_get_flags(res) & RF_ACTIVE),
462231149Snwhitehorn	    ("active resources cannot be adjusted"));
463231149Snwhitehorn	if (rman_get_flags(res) & RF_ACTIVE)
464231149Snwhitehorn		return (EINVAL);
465231149Snwhitehorn
466231003Snwhitehorn	switch (type) {
467231003Snwhitehorn	case SYS_RES_MEMORY:
468231003Snwhitehorn		rm = &sc->sc_mem_rman;
469231003Snwhitehorn		break;
470231003Snwhitehorn	case SYS_RES_IOPORT:
471231003Snwhitehorn		rm = &sc->sc_io_rman;
472231003Snwhitehorn		break;
473231003Snwhitehorn	default:
474231003Snwhitehorn		return (ENXIO);
475231003Snwhitehorn	}
476231003Snwhitehorn
477231003Snwhitehorn	if (!rman_is_region_manager(res, rm))
478231003Snwhitehorn		return (EINVAL);
479231026Snwhitehorn
480231149Snwhitehorn	return (rman_adjust_resource(res, start, end));
481231003Snwhitehorn}
482231003Snwhitehorn
483231003Snwhitehorn
484230993Snwhitehornstatic phandle_t
485230993Snwhitehornofw_pci_get_node(device_t bus, device_t dev)
486230993Snwhitehorn{
487230993Snwhitehorn	struct ofw_pci_softc *sc;
488230993Snwhitehorn
489230993Snwhitehorn	sc = device_get_softc(bus);
490230993Snwhitehorn	/* We only have one child, the PCI bus, which needs our own node. */
491230993Snwhitehorn
492230993Snwhitehorn	return (sc->sc_node);
493230993Snwhitehorn}
494230993Snwhitehorn
495230993Snwhitehornstatic int
496230993Snwhitehornofw_pci_nranges(phandle_t node)
497230993Snwhitehorn{
498230993Snwhitehorn	int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
499230993Snwhitehorn	ssize_t nbase_ranges;
500230993Snwhitehorn
501230993Snwhitehorn	OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
502230993Snwhitehorn	    sizeof(host_address_cells));
503230993Snwhitehorn	OF_getprop(node, "#address-cells", &pci_address_cells,
504230993Snwhitehorn	    sizeof(pci_address_cells));
505230993Snwhitehorn	OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
506230993Snwhitehorn
507230993Snwhitehorn	nbase_ranges = OF_getproplen(node, "ranges");
508230993Snwhitehorn	if (nbase_ranges <= 0)
509230993Snwhitehorn		return (-1);
510230993Snwhitehorn
511230993Snwhitehorn	return (nbase_ranges / sizeof(cell_t) /
512230993Snwhitehorn	    (pci_address_cells + host_address_cells + size_cells));
513230993Snwhitehorn}
514230993Snwhitehorn
515230993Snwhitehornstatic int
516230993Snwhitehornofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges)
517230993Snwhitehorn{
518230993Snwhitehorn	int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
519230993Snwhitehorn	cell_t *base_ranges;
520230993Snwhitehorn	ssize_t nbase_ranges;
521230993Snwhitehorn	int nranges;
522230993Snwhitehorn	int i, j, k;
523230993Snwhitehorn
524230993Snwhitehorn	OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
525230993Snwhitehorn	    sizeof(host_address_cells));
526230993Snwhitehorn	OF_getprop(node, "#address-cells", &pci_address_cells,
527230993Snwhitehorn	    sizeof(pci_address_cells));
528230993Snwhitehorn	OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
529230993Snwhitehorn
530230993Snwhitehorn	nbase_ranges = OF_getproplen(node, "ranges");
531230993Snwhitehorn	if (nbase_ranges <= 0)
532230993Snwhitehorn		return (-1);
533230993Snwhitehorn	nranges = nbase_ranges / sizeof(cell_t) /
534230993Snwhitehorn	    (pci_address_cells + host_address_cells + size_cells);
535230993Snwhitehorn
536230993Snwhitehorn	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
537230993Snwhitehorn	OF_getprop(node, "ranges", base_ranges, nbase_ranges);
538230993Snwhitehorn
539230993Snwhitehorn	for (i = 0, j = 0; i < nranges; i++) {
540230993Snwhitehorn		ranges[i].pci_hi = base_ranges[j++];
541230993Snwhitehorn		ranges[i].pci = 0;
542230993Snwhitehorn		for (k = 0; k < pci_address_cells - 1; k++) {
543230993Snwhitehorn			ranges[i].pci <<= 32;
544230993Snwhitehorn			ranges[i].pci |= base_ranges[j++];
545230993Snwhitehorn		}
546230993Snwhitehorn		ranges[i].host = 0;
547230993Snwhitehorn		for (k = 0; k < host_address_cells; k++) {
548230993Snwhitehorn			ranges[i].host <<= 32;
549230993Snwhitehorn			ranges[i].host |= base_ranges[j++];
550230993Snwhitehorn		}
551230993Snwhitehorn		ranges[i].size = 0;
552230993Snwhitehorn		for (k = 0; k < size_cells; k++) {
553230993Snwhitehorn			ranges[i].size <<= 32;
554230993Snwhitehorn			ranges[i].size |= base_ranges[j++];
555230993Snwhitehorn		}
556230993Snwhitehorn	}
557230993Snwhitehorn
558230993Snwhitehorn	free(base_ranges, M_DEVBUF);
559230993Snwhitehorn	return (nranges);
560230993Snwhitehorn}
561230993Snwhitehorn
562