2 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
5 * Developed by Semihalf.
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. Neither the name of MARVELL nor the names of contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Marvell integrated PCI/PCI-Express controller driver.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/queue.h>
49 #include <sys/endian.h>
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcib_private.h>
60 #include <machine/resource.h>
61 #include <machine/bus.h>
63 #include <arm/mv/mvreg.h>
64 #include <arm/mv/mvvar.h>
66 #define PCI_CFG_ENA (1 << 31)
67 #define PCI_CFG_BUS(bus) (((bus) & 0xff) << 16)
68 #define PCI_CFG_DEV(dev) (((dev) & 0x1f) << 11)
69 #define PCI_CFG_FUN(fun) (((fun) & 0x7) << 8)
70 #define PCI_CFG_PCIE_REG(reg) ((reg) & 0xfc)
72 #define PCI_REG_CFG_ADDR 0x0C78
73 #define PCI_REG_CFG_DATA 0x0C7C
74 #define PCI_REG_P2P_CONF 0x1D14
76 #define PCIE_REG_CFG_ADDR 0x18F8
77 #define PCIE_REG_CFG_DATA 0x18FC
78 #define PCIE_REG_CONTROL 0x1A00
79 #define PCIE_CTRL_LINK1X 0x00000001
80 #define PCIE_REG_STATUS 0x1A04
81 #define PCIE_REG_IRQ_MASK 0x1910
83 #define STATUS_LINK_DOWN 1
84 #define STATUS_BUS_OFFS 8
85 #define STATUS_BUS_MASK (0xFF << STATUS_BUS_OFFS)
86 #define STATUS_DEV_OFFS 16
87 #define STATUS_DEV_MASK (0x1F << STATUS_DEV_OFFS)
89 #define P2P_CONF_BUS_OFFS 16
90 #define P2P_CONF_BUS_MASK (0xFF << P2P_CONF_BUS_OFFS)
91 #define P2P_CONF_DEV_OFFS 24
92 #define P2P_CONF_DEV_MASK (0x1F << P2P_CONF_DEV_OFFS)
94 #define PCI_VENDORID_MRVL 0x11AB
96 struct pcib_mbus_softc {
99 struct rman sc_iomem_rman;
100 bus_addr_t sc_iomem_base;
101 bus_addr_t sc_iomem_size;
102 bus_addr_t sc_iomem_alloc; /* Next allocation. */
104 struct rman sc_ioport_rman;
105 bus_addr_t sc_ioport_base;
106 bus_addr_t sc_ioport_size;
107 bus_addr_t sc_ioport_alloc; /* Next allocation. */
109 struct resource *sc_res;
110 bus_space_handle_t sc_bsh;
111 bus_space_tag_t sc_bst;
114 int sc_busnr; /* Host bridge bus number */
115 int sc_devnr; /* Host bridge device number */
117 const struct obio_pci *sc_info;
120 static void pcib_mbus_identify(driver_t *driver, device_t parent);
121 static int pcib_mbus_probe(device_t);
122 static int pcib_mbus_attach(device_t);
124 static struct resource *pcib_mbus_alloc_resource(device_t, device_t, int, int *,
125 u_long, u_long, u_long, u_int);
126 static int pcib_mbus_release_resource(device_t, device_t, int, int,
128 static int pcib_mbus_read_ivar(device_t, device_t, int, uintptr_t *);
129 static int pcib_mbus_write_ivar(device_t, device_t, int, uintptr_t);
131 static int pcib_mbus_maxslots(device_t);
132 static uint32_t pcib_mbus_read_config(device_t, u_int, u_int, u_int, u_int,
134 static void pcib_mbus_write_config(device_t, u_int, u_int, u_int, u_int,
136 static int pcib_mbus_init(struct pcib_mbus_softc *sc, int bus, int maxslot);
137 static int pcib_mbus_init_bar(struct pcib_mbus_softc *sc, int bus, int slot,
138 int func, int barno);
139 static void pcib_mbus_init_bridge(struct pcib_mbus_softc *sc, int bus, int slot,
141 static int pcib_mbus_init_resources(struct pcib_mbus_softc *sc, int bus,
142 int slot, int func, int hdrtype);
145 * Bus interface definitions.
147 static device_method_t pcib_mbus_methods[] = {
148 /* Device interface */
149 DEVMETHOD(device_identify, pcib_mbus_identify),
150 DEVMETHOD(device_probe, pcib_mbus_probe),
151 DEVMETHOD(device_attach, pcib_mbus_attach),
154 DEVMETHOD(bus_read_ivar, pcib_mbus_read_ivar),
155 DEVMETHOD(bus_write_ivar, pcib_mbus_write_ivar),
156 DEVMETHOD(bus_alloc_resource, pcib_mbus_alloc_resource),
157 DEVMETHOD(bus_release_resource, pcib_mbus_release_resource),
158 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
159 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
160 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
161 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
164 DEVMETHOD(pcib_maxslots, pcib_mbus_maxslots),
165 DEVMETHOD(pcib_read_config, pcib_mbus_read_config),
166 DEVMETHOD(pcib_write_config, pcib_mbus_write_config),
167 DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt),
172 static driver_t pcib_mbus_driver = {
175 sizeof(struct pcib_mbus_softc),
178 devclass_t pcib_devclass;
180 DRIVER_MODULE(pcib, mbus, pcib_mbus_driver, pcib_devclass, 0, 0);
182 static struct mtx pcicfg_mtx;
185 pcib_write_irq_mask(struct pcib_mbus_softc *sc, uint32_t mask)
188 if (!sc->sc_info->op_type != MV_TYPE_PCI)
191 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
192 PCIE_REG_IRQ_MASK, mask);
196 pcib_mbus_hw_cfginit(void)
198 static int opened = 0;
203 mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
208 pcib_mbus_hw_cfgread(struct pcib_mbus_softc *sc, u_int bus, u_int slot,
209 u_int func, u_int reg, int bytes)
211 uint32_t addr, data, ca, cd;
213 ca = (sc->sc_info->op_type != MV_TYPE_PCI) ?
214 PCIE_REG_CFG_ADDR : PCI_REG_CFG_ADDR;
215 cd = (sc->sc_info->op_type != MV_TYPE_PCI) ?
216 PCIE_REG_CFG_DATA : PCI_REG_CFG_DATA;
217 addr = PCI_CFG_ENA | PCI_CFG_BUS(bus) | PCI_CFG_DEV(slot) |
218 PCI_CFG_FUN(func) | PCI_CFG_PCIE_REG(reg);
220 mtx_lock_spin(&pcicfg_mtx);
221 bus_space_write_4(sc->sc_bst, sc->sc_bsh, ca, addr);
226 data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
230 data = le16toh(bus_space_read_2(sc->sc_bst, sc->sc_bsh,
234 data = le32toh(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
238 mtx_unlock_spin(&pcicfg_mtx);
243 pcib_mbus_hw_cfgwrite(struct pcib_mbus_softc *sc, u_int bus, u_int slot,
244 u_int func, u_int reg, uint32_t data, int bytes)
246 uint32_t addr, ca, cd;
248 ca = (sc->sc_info->op_type != MV_TYPE_PCI) ?
249 PCIE_REG_CFG_ADDR : PCI_REG_CFG_ADDR;
250 cd = (sc->sc_info->op_type != MV_TYPE_PCI) ?
251 PCIE_REG_CFG_DATA : PCI_REG_CFG_DATA;
252 addr = PCI_CFG_ENA | PCI_CFG_BUS(bus) | PCI_CFG_DEV(slot) |
253 PCI_CFG_FUN(func) | PCI_CFG_PCIE_REG(reg);
255 mtx_lock_spin(&pcicfg_mtx);
256 bus_space_write_4(sc->sc_bst, sc->sc_bsh, ca, addr);
260 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
261 cd + (reg & 3), data);
264 bus_space_write_2(sc->sc_bst, sc->sc_bsh,
265 cd + (reg & 2), htole16(data));
268 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
272 mtx_unlock_spin(&pcicfg_mtx);
276 pcib_mbus_maxslots(device_t dev)
278 struct pcib_mbus_softc *sc = device_get_softc(dev);
280 return ((sc->sc_info->op_type != MV_TYPE_PCI) ? 1 : PCI_SLOTMAX);
284 pcib_mbus_read_config(device_t dev, u_int bus, u_int slot, u_int func,
285 u_int reg, int bytes)
287 struct pcib_mbus_softc *sc = device_get_softc(dev);
290 if (bus == sc->sc_busnr && slot == sc->sc_devnr)
293 return (pcib_mbus_hw_cfgread(sc, bus, slot, func, reg, bytes));
297 pcib_mbus_write_config(device_t dev, u_int bus, u_int slot, u_int func,
298 u_int reg, uint32_t val, int bytes)
300 struct pcib_mbus_softc *sc = device_get_softc(dev);
303 if (bus == sc->sc_busnr && slot == sc->sc_devnr)
306 pcib_mbus_hw_cfgwrite(sc, bus, slot, func, reg, val, bytes);
310 pcib_mbus_add_child(driver_t *driver, device_t parent, struct pcib_mbus_softc *sc)
315 /* Configure CPU decoding windows */
316 error = decode_win_cpu_set(sc->sc_info->op_io_win_target,
317 sc->sc_info->op_io_win_attr, sc->sc_info->op_io_base,
318 sc->sc_info->op_io_size, -1);
320 device_printf(parent, "Could not set up CPU decode "
321 "window for PCI IO\n");
324 error = decode_win_cpu_set(sc->sc_info->op_mem_win_target,
325 sc->sc_info->op_mem_win_attr, sc->sc_info->op_mem_base,
326 sc->sc_info->op_mem_size, -1);
328 device_printf(parent, "Could not set up CPU decode "
329 "windows for PCI MEM\n");
333 /* Create driver instance */
334 child = BUS_ADD_CHILD(parent, 0, driver->name, -1);
335 bus_set_resource(child, SYS_RES_MEMORY, 0,
336 sc->sc_info->op_base, sc->sc_info->op_size);
337 device_set_softc(child, sc);
341 pcib_mbus_identify(driver_t *driver, device_t parent)
343 const struct obio_pci *info = mv_pci_info;
344 struct pcib_mbus_softc *sc;
347 while (info->op_base) {
348 sc = malloc(driver->size, M_DEVBUF, M_NOWAIT | M_ZERO);
350 device_printf(parent, "Could not allocate pcib "
354 sc->sc_info = info++;
357 * PCI bridge objects are instantiated immediately. PCI-Express
358 * bridges require more complicated handling depending on
359 * platform configuration.
361 if (sc->sc_info->op_type == MV_TYPE_PCI) {
362 pcib_mbus_add_child(driver, parent, sc);
367 * Read link configuration
370 sc->sc_res = BUS_ALLOC_RESOURCE(parent, parent, SYS_RES_MEMORY,
371 &sc->sc_rid, sc->sc_info->op_base, sc->sc_info->op_base +
372 sc->sc_info->op_size - 1, sc->sc_info->op_size,
374 if (sc->sc_res == NULL) {
375 device_printf(parent, "Could not map pcib memory\n");
379 sc->sc_bst = rman_get_bustag(sc->sc_res);
380 sc->sc_bsh = rman_get_bushandle(sc->sc_res);
382 control = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
385 BUS_RELEASE_RESOURCE(parent, parent, SYS_RES_MEMORY, sc->sc_rid,
389 * If this PCI-E port (controller) is configured (by the
390 * underlying firmware) with lane width other than 1x, there
391 * are auxiliary resources defined for aggregating more width
392 * on our lane. Skip all such entries as they are not
393 * standalone ports and must not have a device object
396 if ((control & PCIE_CTRL_LINK1X) == 0)
397 while (info->op_base &&
398 info->op_type == MV_TYPE_PCIE_AGGR_LANE)
401 pcib_mbus_add_child(driver, parent, sc);
406 pcib_mbus_probe(device_t self)
409 struct pcib_mbus_softc *sc;
410 const char *id, *type;
412 int rv = ENOENT, bus, dev;
414 sc = device_get_softc(self);
417 sc->sc_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &sc->sc_rid,
419 if (sc->sc_res == NULL) {
420 device_printf(self, "Could not map memory\n");
424 sc->sc_bst = rman_get_bustag(sc->sc_res);
425 sc->sc_bsh = rman_get_bushandle(sc->sc_res);
427 pcib_mbus_hw_cfginit();
429 /* Retrieve configuration of the bridge */
430 if (sc->sc_info->op_type == MV_TYPE_PCI) {
431 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
433 bus = sc->sc_busnr = (val & P2P_CONF_BUS_MASK) >>
435 dev = sc->sc_devnr = (val & P2P_CONF_DEV_MASK) >>
438 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh, PCIE_REG_STATUS);
439 if (val & STATUS_LINK_DOWN)
441 bus = sc->sc_busnr = (val & STATUS_BUS_MASK) >> STATUS_BUS_OFFS;
442 dev = sc->sc_devnr = (val & STATUS_DEV_MASK) >> STATUS_DEV_OFFS;
445 val = pcib_mbus_hw_cfgread(sc, bus, dev, 0, PCIR_VENDOR, 2);
446 if (val != PCI_VENDORID_MRVL)
449 val = pcib_mbus_hw_cfgread(sc, bus, dev, 0, PCIR_DEVICE, 2);
468 * According to documentation ID 0x7820 is assigned to MV78200.
469 * However some MV78100 chips also use it.
471 id = "MV78100/MV78200";
474 device_printf(self, "unknown Marvell PCI bridge: %x\n", val);
479 val = pcib_mbus_hw_cfgread(sc, bus, dev, 0, PCIR_CAP_PTR, 1);
481 val = pcib_mbus_hw_cfgread(sc, bus, dev, 0, val, 2);
482 switch (val & 0xff) {
487 type = "PCI-Express";
490 val = (val >> 8) & 0xff;
493 snprintf(buf, sizeof(buf), "Marvell %s %s host controller", id,
495 device_set_desc_copy(self, buf);
496 rv = BUS_PROBE_DEFAULT;
498 bus_release_resource(self, SYS_RES_MEMORY, sc->sc_rid, sc->sc_res);
503 pcib_mbus_attach(device_t self)
505 struct pcib_mbus_softc *sc;
509 sc = device_get_softc(self);
513 sc->sc_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &sc->sc_rid,
515 if (sc->sc_res == NULL) {
516 device_printf(self, "Could not map memory\n");
519 sc->sc_bst = rman_get_bustag(sc->sc_res);
520 sc->sc_bsh = rman_get_bushandle(sc->sc_res);
522 /* Enable PCI bridge */
523 val = pcib_mbus_hw_cfgread(sc, sc->sc_busnr, sc->sc_devnr, 0,
525 val |= PCIM_CMD_SERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN |
527 pcib_mbus_hw_cfgwrite(sc, sc->sc_busnr, sc->sc_devnr, 0,
528 PCIR_COMMAND, val, 2);
530 sc->sc_iomem_base = sc->sc_info->op_mem_base;
531 sc->sc_iomem_size = sc->sc_info->op_mem_size;
532 sc->sc_iomem_alloc = sc->sc_info->op_mem_base;
534 sc->sc_ioport_base = sc->sc_info->op_io_base;
535 sc->sc_ioport_size = sc->sc_info->op_io_size;
536 sc->sc_ioport_alloc = sc->sc_info->op_io_base;
538 sc->sc_iomem_rman.rm_type = RMAN_ARRAY;
539 err = rman_init(&sc->sc_iomem_rman);
543 sc->sc_ioport_rman.rm_type = RMAN_ARRAY;
544 err = rman_init(&sc->sc_ioport_rman);
546 rman_fini(&sc->sc_iomem_rman);
550 err = rman_manage_region(&sc->sc_iomem_rman, sc->sc_iomem_base,
551 sc->sc_iomem_base + sc->sc_iomem_size - 1);
555 err = rman_manage_region(&sc->sc_ioport_rman, sc->sc_ioport_base,
556 sc->sc_ioport_base + sc->sc_ioport_size - 1);
560 err = pcib_mbus_init(sc, sc->sc_busnr, pcib_mbus_maxslots(sc->sc_dev));
564 device_add_child(self, "pci", -1);
565 return (bus_generic_attach(self));
568 rman_fini(&sc->sc_iomem_rman);
569 rman_fini(&sc->sc_ioport_rman);
574 pcib_mbus_init_bar(struct pcib_mbus_softc *sc, int bus, int slot, int func,
577 bus_addr_t *allocp, limit;
578 uint32_t addr, bar, mask, size;
581 reg = PCIR_BAR(barno);
582 bar = pcib_mbus_read_config(sc->sc_dev, bus, slot, func, reg, 4);
586 /* Calculate BAR size: 64 or 32 bit (in 32-bit units) */
587 width = ((bar & 7) == 4) ? 2 : 1;
589 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, reg, ~0, 4);
590 size = pcib_mbus_read_config(sc->sc_dev, bus, slot, func, reg, 4);
592 /* Get BAR type and size */
595 allocp = &sc->sc_ioport_alloc;
596 limit = sc->sc_ioport_base + sc->sc_ioport_size;
598 if ((size & 0xffff0000) == 0)
602 allocp = &sc->sc_iomem_alloc;
603 limit = sc->sc_iomem_base + sc->sc_iomem_size;
609 /* Sanity check (must be a power of 2) */
613 addr = (*allocp + mask) & ~mask;
614 if ((*allocp = addr + size) > limit)
618 printf("PCI %u:%u:%u: reg %x: size=%08x: addr=%08x\n",
619 bus, slot, func, reg, size, addr);
621 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, reg, addr, 4);
623 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, reg + 4,
630 pcib_mbus_init_bridge(struct pcib_mbus_softc *sc, int bus, int slot, int func)
632 bus_addr_t io_base, mem_base;
633 uint32_t io_limit, mem_limit;
636 io_base = sc->sc_info->op_io_base;
637 io_limit = io_base + sc->sc_info->op_io_size - 1;
638 mem_base = sc->sc_info->op_mem_base;
639 mem_limit = mem_base + sc->sc_info->op_mem_size - 1;
641 /* Configure I/O decode registers */
642 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_IOBASEL_1,
644 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_IOBASEH_1,
646 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_IOLIMITL_1,
648 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_IOLIMITH_1,
651 /* Configure memory decode registers */
652 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_MEMBASE_1,
654 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_MEMLIMIT_1,
657 /* Disable memory prefetch decode */
658 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_PMBASEL_1,
660 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_PMBASEH_1,
662 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_PMLIMITL_1,
664 pcib_mbus_write_config(sc->sc_dev, bus, slot, func, PCIR_PMLIMITH_1,
667 secbus = pcib_mbus_read_config(sc->sc_dev, bus, slot, func,
670 /* Configure buses behind the bridge */
671 pcib_mbus_init(sc, secbus, PCI_SLOTMAX);
675 pcib_mbus_init_resources(struct pcib_mbus_softc *sc, int bus, int slot,
676 int func, int hdrtype)
678 const struct obio_pci_irq_map *map = sc->sc_info->op_pci_irq_map;
679 int maxbar = (hdrtype & PCIM_HDRTYPE) ? 0 : 6;
680 int bar = 0, irq = -1;
683 /* Program the base address registers */
684 while (bar < maxbar) {
685 i = pcib_mbus_init_bar(sc, bus, slot, func, bar);
688 device_printf(sc->sc_dev,
689 "PCI IO/Memory space exhausted\n");
694 /* Perform interrupt routing */
695 pin = pcib_mbus_read_config(sc->sc_dev, bus, slot, func,
699 while (map->opim_irq >= 0) {
700 if ((map->opim_slot == slot || map->opim_slot < 0) &&
701 (map->opim_pin == pin || map->opim_pin < 0))
707 irq = sc->sc_info->op_irq;
710 pcib_mbus_write_config(sc->sc_dev, bus, slot, func,
711 PCIR_INTLINE, irq, 1);
713 device_printf(sc->sc_dev, "Missing IRQ routing information "
714 "for PCI device %u:%u:%u\n", bus, slot, func);
722 pcib_mbus_init(struct pcib_mbus_softc *sc, int bus, int maxslot)
724 int slot, func, maxfunc, error;
725 uint8_t hdrtype, command, class, subclass;
727 for (slot = 0; slot <= maxslot; slot++) {
729 for (func = 0; func <= maxfunc; func++) {
730 hdrtype = pcib_mbus_read_config(sc->sc_dev, bus, slot,
731 func, PCIR_HDRTYPE, 1);
733 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
736 if (func == 0 && (hdrtype & PCIM_MFDEV))
737 maxfunc = PCI_FUNCMAX;
739 command = pcib_mbus_read_config(sc->sc_dev, bus, slot,
740 func, PCIR_COMMAND, 1);
741 command &= ~(PCIM_CMD_MEMEN | PCIM_CMD_PORTEN);
742 pcib_mbus_write_config(sc->sc_dev, bus, slot, func,
743 PCIR_COMMAND, command, 1);
745 error = pcib_mbus_init_resources(sc, bus, slot, func,
751 command |= PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN |
753 pcib_mbus_write_config(sc->sc_dev, bus, slot, func,
754 PCIR_COMMAND, command, 1);
756 /* Handle PCI-PCI bridges */
757 class = pcib_mbus_read_config(sc->sc_dev, bus, slot,
758 func, PCIR_CLASS, 1);
759 subclass = pcib_mbus_read_config(sc->sc_dev, bus, slot,
760 func, PCIR_SUBCLASS, 1);
762 if (class != PCIC_BRIDGE ||
763 subclass != PCIS_BRIDGE_PCI)
766 pcib_mbus_init_bridge(sc, bus, slot, func);
770 /* Enable all ABCD interrupts */
771 pcib_write_irq_mask(sc, (0xF << 24));
776 static struct resource *
777 pcib_mbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
778 u_long start, u_long end, u_long count, u_int flags)
780 struct pcib_mbus_softc *sc = device_get_softc(dev);
781 struct rman *rm = NULL;
782 struct resource *res;
786 rm = &sc->sc_ioport_rman;
789 rm = &sc->sc_iomem_rman;
792 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
793 type, rid, start, end, count, flags));
796 res = rman_reserve_resource(rm, start, end, count, flags, child);
800 rman_set_rid(res, *rid);
801 rman_set_bustag(res, obio_tag);
802 rman_set_bushandle(res, start);
804 if (flags & RF_ACTIVE)
805 if (bus_activate_resource(child, type, *rid, res)) {
806 rman_release_resource(res);
814 pcib_mbus_release_resource(device_t dev, device_t child, int type, int rid,
815 struct resource *res)
818 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY)
819 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
822 return (rman_release_resource(res));
826 pcib_mbus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
828 struct pcib_mbus_softc *sc = device_get_softc(dev);
832 *result = sc->sc_busnr;
834 case PCIB_IVAR_DOMAIN:
835 *result = device_get_unit(dev);
843 pcib_mbus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
845 struct pcib_mbus_softc *sc = device_get_softc(dev);
849 sc->sc_busnr = value;