2 * Copyright (c) 2007 Bruce M. Simpson.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Child driver for PCI host bridge core.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/endian.h>
45 #include <vm/vm_extern.h>
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/pcb.h>
50 #include <machine/pmap.h>
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcib_private.h>
58 #include <dev/siba/siba_ids.h>
59 #include <dev/siba/sibareg.h>
60 #include <dev/siba/sibavar.h>
61 #include <dev/siba/siba_pcibvar.h>
64 #define MIPS_MEM_RID 0x20
67 #define SBPCI_SLOTMAX 15
69 #define SBPCI_READ_4(sc, reg) \
70 bus_space_write_4((sc)->sc_bt, (sc)->sc_bh, (reg))
72 #define SBPCI_WRITE_4(sc, reg, val) \
73 bus_space_write_4((sc)->sc_bt, (sc)->sc_bh, (reg), (val))
76 * PCI Configuration space window (64MB).
77 * contained in SBTOPCI1 window.
79 #define SBPCI_CFGBASE 0x0C000000
80 #define SBPCI_CFGSIZE 0x01000000
83 * TODO: implement type 1 config space access (ie beyond bus 0)
84 * we may need to tweak the windows to do this
85 * TODO: interrupt routing.
86 * TODO: fully implement bus allocation.
87 * TODO: implement resource managers.
91 static int siba_pcib_activate_resource(device_t, device_t, int,
92 int, struct resource *);
93 static struct resource *
94 siba_pcib_alloc_resource(device_t, device_t, int, int *,
95 u_long , u_long, u_long, u_int);
96 static int siba_pcib_attach(device_t);
97 static int siba_pcib_deactivate_resource(device_t, device_t, int,
98 int, struct resource *);
99 static int siba_pcib_maxslots(device_t);
100 static int siba_pcib_probe(device_t);
102 siba_pcib_read_config(device_t, u_int, u_int, u_int, u_int,
104 static int siba_pcib_read_ivar(device_t, device_t, int, uintptr_t *);
105 static int siba_pcib_release_resource(device_t, device_t, int, int,
107 static int siba_pcib_route_interrupt(device_t, device_t, int);
108 static int siba_pcib_setup_intr(device_t, device_t, struct resource *,
109 int, driver_filter_t *, driver_intr_t *, void *, void **);
110 static int siba_pcib_teardown_intr(device_t, device_t, struct resource *,
112 static void siba_pcib_write_config(device_t, u_int, u_int, u_int, u_int,
114 static int siba_pcib_write_ivar(device_t, device_t, int, uintptr_t);
117 siba_pcib_probe(device_t dev)
120 /* TODO: support earlier cores. */
121 /* TODO: Check if PCI host mode is enabled in the SPROM. */
122 if (siba_get_vendor(dev) == SIBA_VID_BROADCOM &&
123 siba_get_device(dev) == SIBA_DEVID_PCI) {
124 device_set_desc(dev, "SiBa-to-PCI host bridge");
125 return (BUS_PROBE_DEFAULT);
131 //extern int rman_debug;
134 siba_pcib_attach(device_t dev)
136 struct siba_pcib_softc *sc = device_get_softc(dev);
140 * Allocate the resources which the parent bus has already
143 rid = MIPS_MEM_RID; /* XXX */
145 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
147 if (sc->sc_mem == NULL) {
148 device_printf(dev, "unable to allocate memory\n");
152 sc->sc_bt = rman_get_bustag(sc->sc_mem);
153 sc->sc_bh = rman_get_bushandle(sc->sc_mem);
155 device_printf(dev, "bridge registers addr 0x%08x vaddr %p\n",
156 (uint32_t)sc->sc_bh, rman_get_virtual(sc->sc_mem));
158 SBPCI_WRITE_4(sc, 0x0000, 0x05);
159 SBPCI_WRITE_4(sc, 0x0000, 0x0D);
161 SBPCI_WRITE_4(sc, 0x0000, 0x0F);
162 SBPCI_WRITE_4(sc, 0x0010, 0x01);
165 bus_space_handle_t sc_cfg_hand;
169 * XXX this doesn't actually do anything on mips; however... should
170 * we not be mapping to KSEG1? we need to wire down the range.
172 error = bus_space_map(sc->sc_bt, SBPCI_CFGBASE, SBPCI_CFGSIZE,
175 device_printf(dev, "cannot map PCI configuration space\n");
178 device_printf(dev, "mapped pci config space at 0x%08x\n",
179 (uint32_t)sc_cfg_hand);
182 * Setup configuration, io, and dma space windows.
183 * XXX we need to be able to do type 1 too.
184 * we probably don't need to be able to do i/o cycles.
187 /* I/O read/write window */
188 SBPCI_WRITE_4(sc, SIBA_PCICORE_SBTOPCI0, 1);
189 /* type 0 configuration only */
190 SBPCI_WRITE_4(sc, SIBA_PCICORE_SBTOPCI1, 2);
191 SBPCI_WRITE_4(sc, SIBA_PCICORE_SBTOPCI2, 1 << 30); /* memory only */
194 /* XXX resource managers */
196 device_add_child(dev, "pci", -1);
197 return (bus_generic_attach(dev));
203 siba_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
205 struct siba_pcib_softc *sc;
207 sc = device_get_softc(dev);
210 *result = sc->sc_bus;
218 siba_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
220 struct siba_pcib_softc *sc;
222 sc = device_get_softc(dev);
233 siba_pcib_setup_intr(device_t dev, device_t child, struct resource *ires,
234 int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
238 return (BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags,
239 filt, intr, arg, cookiep));
243 siba_pcib_teardown_intr(device_t dev, device_t child, struct resource *vec,
247 return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, vec, cookie));
250 static struct resource *
251 siba_pcib_alloc_resource(device_t bus, device_t child, int type, int *rid,
252 u_long start, u_long end, u_long count, u_int flags)
256 //device_printf(bus, "%s: not yet implemented\n", __func__);
260 struct siba_pcib_softc *sc = device_get_softc(bus);
268 rmanp = &sc->sc_irq_rman;
272 rmanp = &sc->sc_mem_rman;
273 tag = &sc->sc_pci_memt;
280 rv = rman_reserve_resource(rmanp, start, end, count, flags, child);
282 rman_set_rid(rv, *rid);
283 if (type == SYS_RES_MEMORY) {
285 rman_set_bustag(rv, tag);
286 rman_set_bushandle(rv, rman_get_bushandle(sc->sc_mem) +
287 (rman_get_start(rv) - IXP425_PCI_MEM_HWBASE));
297 siba_pcib_activate_resource(device_t bus, device_t child, int type, int rid,
301 device_printf(bus, "%s: not yet implemented\n", __func__);
302 device_printf(bus, "%s called activate_resource\n",
303 device_get_nameunit(child));
308 siba_pcib_deactivate_resource(device_t bus, device_t child, int type, int rid,
312 device_printf(bus, "%s: not yet implemented\n", __func__);
313 device_printf(bus, "%s called deactivate_resource\n",
314 device_get_nameunit(child));
319 siba_pcib_release_resource(device_t bus, device_t child, int type, int rid,
323 device_printf(bus, "%s: not yet implemented\n", __func__);
324 device_printf(bus, "%s called release_resource\n",
325 device_get_nameunit(child));
329 /* pcib interface functions */
332 siba_pcib_maxslots(device_t dev)
335 return (SBPCI_SLOTMAX);
339 * This needs hacking and fixery. It is currently broke and hangs.
340 * Debugging it will be tricky; there seems to be no way to enable
341 * a target abort which would cause a nice target abort.
342 * Look at linux again?
345 siba_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
346 u_int reg, int bytes)
348 struct siba_pcib_softc *sc = device_get_softc(dev);
353 /* XXX anything higher than slot 2 currently seems to hang the bus.
354 * not sure why this is; look at linux again
356 if (bus != 0 || slot > 2) {
357 printf("%s: bad b/s/f %d/%d/%d\n", __func__, bus, slot, func);
358 return 0xffffffff; // XXX
361 device_printf(dev, "requested %d bytes from b/s/f %d/%d/%d reg %d\n",
362 bytes, bus, slot, func, reg);
365 * The configuration tag on the broadcom is weird.
367 SBPCI_WRITE_4(sc, SIBA_PCICORE_SBTOPCI1, 2); /* XXX again??? */
368 cfgtag = ((1 << slot) << 16) | (func << 8);
369 cfgaddr = SBPCI_CFGBASE | cfgtag | (reg & ~3);
371 /* cfg space i/o is always 32 bits on this bridge */
372 printf("reading 4 bytes from %08x\n", cfgaddr);
373 val = *(volatile uint32_t *)MIPS_PHYS_TO_KSEG1(cfgaddr); /* XXX MIPS */
375 val = bswap32(val); /* XXX seems to be needed for now */
377 /* swizzle and return what was asked for */
378 val &= 0xffffffff >> ((4 - bytes) * 8);
384 siba_pcib_write_config(device_t dev, u_int bus, u_int slot,
385 u_int func, u_int reg, u_int32_t val, int bytes)
388 /* write to pci configuration space */
389 //device_printf(dev, "%s: not yet implemented\n", __func__);
393 siba_pcib_route_interrupt(device_t bridge, device_t device, int pin)
396 //device_printf(bridge, "%s: not yet implemented\n", __func__);
400 static device_method_t siba_pcib_methods[] = {
401 /* Device interface */
402 DEVMETHOD(device_attach, siba_pcib_attach),
403 DEVMETHOD(device_probe, siba_pcib_probe),
406 DEVMETHOD(bus_read_ivar, siba_pcib_read_ivar),
407 DEVMETHOD(bus_write_ivar, siba_pcib_write_ivar),
408 DEVMETHOD(bus_setup_intr, siba_pcib_setup_intr),
409 DEVMETHOD(bus_teardown_intr, siba_pcib_teardown_intr),
410 DEVMETHOD(bus_alloc_resource, siba_pcib_alloc_resource),
411 DEVMETHOD(bus_activate_resource, siba_pcib_activate_resource),
412 DEVMETHOD(bus_deactivate_resource, siba_pcib_deactivate_resource),
413 DEVMETHOD(bus_release_resource, siba_pcib_release_resource),
416 DEVMETHOD(pcib_maxslots, siba_pcib_maxslots),
417 DEVMETHOD(pcib_read_config, siba_pcib_read_config),
418 DEVMETHOD(pcib_write_config, siba_pcib_write_config),
419 DEVMETHOD(pcib_route_interrupt, siba_pcib_route_interrupt),
424 static driver_t siba_pcib_driver = {
427 sizeof(struct siba_softc),
429 static devclass_t siba_pcib_devclass;
431 DRIVER_MODULE(siba_pcib, siba, siba_pcib_driver, siba_pcib_devclass, 0, 0);