2 * Copyright (c) 1999, 2000 Matthew R. Green
3 * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>
4 * Copyright (c) 2005 - 2015 by Marius Strobl <marius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * from: NetBSD: psycho.c,v 1.35 2001/09/10 16:17:06 eeh Exp
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_ofw_pci.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_pci.h>
46 #include <dev/ofw/openfirm.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
51 #include <machine/asi.h>
52 #include <machine/bus.h>
53 #include <machine/bus_private.h>
54 #include <machine/cpufunc.h>
55 #include <machine/fsr.h>
56 #include <machine/resource.h>
58 #include <sparc64/pci/ofw_pci.h>
61 ofw_pci_attach_common(device_t dev, bus_dma_tag_t dmat, u_long iosize,
64 struct ofw_pci_softc *sc;
65 struct ofw_pci_ranges *range;
67 uint32_t prop_array[2];
70 sc = device_get_softc(dev);
71 node = ofw_bus_get_node(dev);
73 sc->sc_pci_dmat = dmat;
75 /* Initialize memory and I/O rmans. */
76 sc->sc_pci_io_rman.rm_type = RMAN_ARRAY;
77 sc->sc_pci_io_rman.rm_descr = "PCI I/O Ports";
78 if (rman_init(&sc->sc_pci_io_rman) != 0 ||
79 rman_manage_region(&sc->sc_pci_io_rman, 0, iosize) != 0) {
80 device_printf(dev, "failed to set up I/O rman\n");
83 sc->sc_pci_mem_rman.rm_type = RMAN_ARRAY;
84 sc->sc_pci_mem_rman.rm_descr = "PCI Memory";
85 if (rman_init(&sc->sc_pci_mem_rman) != 0 ||
86 rman_manage_region(&sc->sc_pci_mem_rman, 0, memsize) != 0) {
87 device_printf(dev, "failed to set up memory rman\n");
92 * Find the addresses of the various bus spaces. The physical
93 * start addresses of the ranges are the configuration, I/O and
94 * memory handles. There should not be multiple ones of one kind.
96 nrange = OF_getprop_alloc(node, "ranges", sizeof(*range),
98 for (i = 0; i < nrange; i++) {
99 j = OFW_PCI_RANGE_CS(&range[i]);
100 if (sc->sc_pci_bh[j] != 0) {
101 device_printf(dev, "duplicate range for space %d\n",
106 sc->sc_pci_bh[j] = OFW_PCI_RANGE_PHYS(&range[i]);
111 * Make sure that the expected ranges are actually present.
112 * The OFW_PCI_CS_MEM64 one is not currently used.
114 if (sc->sc_pci_bh[OFW_PCI_CS_CONFIG] == 0) {
115 device_printf(dev, "missing CONFIG range\n");
118 if (sc->sc_pci_bh[OFW_PCI_CS_IO] == 0) {
119 device_printf(dev, "missing IO range\n");
122 if (sc->sc_pci_bh[OFW_PCI_CS_MEM32] == 0) {
123 device_printf(dev, "missing MEM32 range\n");
127 /* Allocate our tags. */
128 sc->sc_pci_iot = sparc64_alloc_bus_tag(NULL, PCI_IO_BUS_SPACE);
129 if (sc->sc_pci_iot == NULL) {
130 device_printf(dev, "could not allocate PCI I/O tag\n");
133 sc->sc_pci_cfgt = sparc64_alloc_bus_tag(NULL, PCI_CONFIG_BUS_SPACE);
134 if (sc->sc_pci_cfgt == NULL) {
136 "could not allocate PCI configuration space tag\n");
141 * Get the bus range from the firmware.
143 i = OF_getprop(node, "bus-range", (void *)prop_array,
146 device_printf(dev, "could not get bus-range\n");
149 if (i != sizeof(prop_array)) {
150 device_printf(dev, "broken bus-range (%d)", i);
153 sc->sc_pci_secbus = prop_array[0];
154 sc->sc_pci_subbus = prop_array[1];
155 if (bootverbose != 0)
156 device_printf(dev, "bus range %u to %u; PCI bus %d\n",
157 sc->sc_pci_secbus, sc->sc_pci_subbus, sc->sc_pci_secbus);
159 ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(ofw_pci_intr_t));
165 ofw_pci_read_config_common(device_t dev, u_int regmax, u_long offset,
166 u_int bus, u_int slot, u_int func, u_int reg, int width)
168 struct ofw_pci_softc *sc;
169 bus_space_handle_t bh;
175 sc = device_get_softc(dev);
176 if (bus < sc->sc_pci_secbus || bus > sc->sc_pci_subbus ||
177 slot > PCI_SLOTMAX || func > PCI_FUNCMAX || reg > regmax)
180 bh = sc->sc_pci_bh[OFW_PCI_CS_CONFIG];
183 i = bus_space_peek_1(sc->sc_pci_cfgt, bh, offset, &byte);
187 i = bus_space_peek_2(sc->sc_pci_cfgt, bh, offset, &shrt);
191 i = bus_space_peek_4(sc->sc_pci_cfgt, bh, offset, &wrd);
195 panic("%s: bad width %d", __func__, width);
201 printf("%s: read data error reading: %d.%d.%d: 0x%x\n",
202 __func__, bus, slot, func, reg);
210 ofw_pci_write_config_common(device_t dev, u_int regmax, u_long offset,
211 u_int bus, u_int slot, u_int func, u_int reg, uint32_t val, int width)
213 struct ofw_pci_softc *sc;
214 bus_space_handle_t bh;
216 sc = device_get_softc(dev);
217 if (bus < sc->sc_pci_secbus || bus > sc->sc_pci_subbus ||
218 slot > PCI_SLOTMAX || func > PCI_FUNCMAX || reg > regmax)
221 bh = sc->sc_pci_bh[OFW_PCI_CS_CONFIG];
224 bus_space_write_1(sc->sc_pci_cfgt, bh, offset, val);
227 bus_space_write_2(sc->sc_pci_cfgt, bh, offset, val);
230 bus_space_write_4(sc->sc_pci_cfgt, bh, offset, val);
233 panic("%s: bad width %d", __func__, width);
239 ofw_pci_route_interrupt_common(device_t bridge, device_t dev, int pin)
241 struct ofw_pci_softc *sc;
242 struct ofw_pci_register reg;
243 ofw_pci_intr_t pintr, mintr;
245 sc = device_get_softc(bridge);
247 if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo,
248 ®, sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
251 return (PCI_INVALID_IRQ);
255 ofw_pci_dmamap_sync_stst_order_common(void)
257 static u_char buf[VIS_BLOCKSIZE] __aligned(VIS_BLOCKSIZE);
262 wr(fprs, reg | FPRS_FEF, 0);
263 __asm __volatile("stda %%f0, [%0] %1"
264 : : "r" (buf), "n" (ASI_BLK_COMMIT_S));
271 ofw_pci_read_ivar(device_t dev, device_t child __unused, int which,
274 struct ofw_pci_softc *sc;
277 case PCIB_IVAR_DOMAIN:
278 *result = device_get_unit(dev);
281 sc = device_get_softc(dev);
282 *result = sc->sc_pci_secbus;
289 ofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
290 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
292 struct ofw_pci_softc *sc;
296 sc = device_get_softc(bus);
300 * XXX: Don't accept blank ranges for now, only single
301 * interrupts. The other case should not happen with
302 * the MI PCI code ...
303 * XXX: This may return a resource that is out of the
304 * range that was specified. Is this correct ...?
307 panic("%s: XXX: interrupt range", __func__);
308 return (bus_generic_alloc_resource(bus, child, type, rid,
309 start, end, count, flags));
311 rm = &sc->sc_pci_mem_rman;
314 rm = &sc->sc_pci_io_rman;
320 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
324 rman_set_rid(rv, *rid);
326 if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type,
328 rman_release_resource(rv);
335 ofw_pci_activate_resource(device_t bus, device_t child, int type, int rid,
338 struct ofw_pci_softc *sc;
339 struct bus_space_tag *tag;
341 sc = device_get_softc(bus);
344 return (bus_generic_activate_resource(bus, child, type, rid,
347 tag = sparc64_alloc_bus_tag(r, PCI_MEMORY_BUS_SPACE);
350 rman_set_bustag(r, tag);
351 rman_set_bushandle(r, sc->sc_pci_bh[OFW_PCI_CS_MEM32] +
355 rman_set_bustag(r, sc->sc_pci_iot);
356 rman_set_bushandle(r, sc->sc_pci_bh[OFW_PCI_CS_IO] +
360 return (rman_activate_resource(r));
364 ofw_pci_adjust_resource(device_t bus, device_t child, int type,
365 struct resource *r, rman_res_t start, rman_res_t end)
367 struct ofw_pci_softc *sc;
370 sc = device_get_softc(bus);
373 return (bus_generic_adjust_resource(bus, child, type, r,
376 rm = &sc->sc_pci_mem_rman;
379 rm = &sc->sc_pci_io_rman;
384 if (rman_is_region_manager(r, rm) == 0)
386 return (rman_adjust_resource(r, start, end));
390 ofw_pci_get_dma_tag(device_t bus, device_t child __unused)
392 struct ofw_pci_softc *sc;
394 sc = device_get_softc(bus);
395 return (sc->sc_pci_dmat);
399 ofw_pci_get_node(device_t bus, device_t child __unused)
401 struct ofw_pci_softc *sc;
403 sc = device_get_softc(bus);
404 /* We only have one child, the PCI bus, which needs our own node. */
405 return (sc->sc_node);