2 * Copyright 2006-2007 by Juniper Networks.
3 * Copyright 2008 Semihalf.
4 * Copyright 2010 The FreeBSD Foundation
7 * Portions of this software were developed by Semihalf
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * From: FreeBSD: src/sys/powerpc/mpc85xx/pci_ocp.c,v 1.9 2010/03/23 23:46:28 marcel
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/sockio.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
47 #include <sys/socket.h>
48 #include <sys/queue.h>
51 #include <sys/mutex.h>
53 #include <sys/endian.h>
58 #include <dev/fdt/fdt_common.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
61 #include <dev/pci/pcivar.h>
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcib_private.h>
65 #include "ofw_bus_if.h"
68 #include <machine/resource.h>
69 #include <machine/bus.h>
70 #include <machine/intr_machdep.h>
72 #include <powerpc/mpc85xx/mpc85xx.h>
74 #define REG_CFG_ADDR 0x0000
75 #define CONFIG_ACCESS_ENABLE 0x80000000
77 #define REG_CFG_DATA 0x0004
78 #define REG_INT_ACK 0x0008
80 #define REG_POTAR(n) (0x0c00 + 0x20 * (n))
81 #define REG_POTEAR(n) (0x0c04 + 0x20 * (n))
82 #define REG_POWBAR(n) (0x0c08 + 0x20 * (n))
83 #define REG_POWAR(n) (0x0c10 + 0x20 * (n))
85 #define REG_PITAR(n) (0x0e00 - 0x20 * (n))
86 #define REG_PIWBAR(n) (0x0e08 - 0x20 * (n))
87 #define REG_PIWBEAR(n) (0x0e0c - 0x20 * (n))
88 #define REG_PIWAR(n) (0x0e10 - 0x20 * (n))
90 #define REG_PEX_MES_DR 0x0020
91 #define REG_PEX_MES_IER 0x0028
92 #define REG_PEX_ERR_DR 0x0e00
93 #define REG_PEX_ERR_EN 0x0e08
95 #define PCIR_LTSSM 0x404
96 #define LTSSM_STAT_L0 0x16
98 #define DEVFN(b, s, f) ((b << 16) | (s << 8) | f)
100 struct fsl_pcib_softc {
103 struct rman sc_iomem;
104 bus_addr_t sc_iomem_va; /* Virtual mapping. */
105 bus_addr_t sc_iomem_size;
106 bus_addr_t sc_iomem_alloc; /* Next allocation. */
108 struct rman sc_ioport;
109 bus_addr_t sc_ioport_va; /* Virtual mapping. */
110 bus_addr_t sc_ioport_size;
111 bus_addr_t sc_ioport_alloc; /* Next allocation. */
112 int sc_ioport_target;
114 struct resource *sc_res;
115 bus_space_handle_t sc_bsh;
116 bus_space_tag_t sc_bst;
121 uint8_t sc_pcie_capreg; /* PCI-E Capability Reg Set */
123 /* Devices that need special attention. */
125 int sc_devfn_via_ide;
127 struct fdt_pci_intr sc_intr_info;
130 /* Local forward declerations. */
131 static uint32_t fsl_pcib_cfgread(struct fsl_pcib_softc *, u_int, u_int, u_int,
133 static void fsl_pcib_cfgwrite(struct fsl_pcib_softc *, u_int, u_int, u_int,
134 u_int, uint32_t, int);
135 static int fsl_pcib_decode_win(phandle_t, struct fsl_pcib_softc *);
136 static void fsl_pcib_err_init(device_t);
137 static void fsl_pcib_inbound(struct fsl_pcib_softc *, int, int, u_long,
139 static int fsl_pcib_init(struct fsl_pcib_softc *, int, int);
140 static int fsl_pcib_intr_info(phandle_t, struct fsl_pcib_softc *);
141 static int fsl_pcib_set_range(struct fsl_pcib_softc *, int, int, u_long,
143 static void fsl_pcib_outbound(struct fsl_pcib_softc *, int, int, u_long,
146 /* Forward declerations. */
147 static int fsl_pcib_attach(device_t);
148 static int fsl_pcib_detach(device_t);
149 static int fsl_pcib_probe(device_t);
151 static struct resource *fsl_pcib_alloc_resource(device_t, device_t, int, int *,
152 u_long, u_long, u_long, u_int);
153 static int fsl_pcib_read_ivar(device_t, device_t, int, uintptr_t *);
154 static int fsl_pcib_release_resource(device_t, device_t, int, int,
156 static int fsl_pcib_write_ivar(device_t, device_t, int, uintptr_t);
158 static int fsl_pcib_maxslots(device_t);
159 static uint32_t fsl_pcib_read_config(device_t, u_int, u_int, u_int, u_int, int);
160 static void fsl_pcib_write_config(device_t, u_int, u_int, u_int, u_int,
163 /* Configuration r/w mutex. */
164 struct mtx pcicfg_mtx;
165 static int mtx_initialized = 0;
168 * Bus interface definitions.
170 static device_method_t fsl_pcib_methods[] = {
171 /* Device interface */
172 DEVMETHOD(device_probe, fsl_pcib_probe),
173 DEVMETHOD(device_attach, fsl_pcib_attach),
174 DEVMETHOD(device_detach, fsl_pcib_detach),
177 DEVMETHOD(bus_print_child, bus_generic_print_child),
178 DEVMETHOD(bus_read_ivar, fsl_pcib_read_ivar),
179 DEVMETHOD(bus_write_ivar, fsl_pcib_write_ivar),
180 DEVMETHOD(bus_alloc_resource, fsl_pcib_alloc_resource),
181 DEVMETHOD(bus_release_resource, fsl_pcib_release_resource),
182 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
183 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
184 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
185 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
188 DEVMETHOD(pcib_maxslots, fsl_pcib_maxslots),
189 DEVMETHOD(pcib_read_config, fsl_pcib_read_config),
190 DEVMETHOD(pcib_write_config, fsl_pcib_write_config),
191 DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt),
193 /* OFW bus interface */
194 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
195 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
196 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
197 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
198 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
203 static driver_t fsl_pcib_driver = {
206 sizeof(struct fsl_pcib_softc),
209 devclass_t pcib_devclass;
211 DRIVER_MODULE(pcib, fdtbus, fsl_pcib_driver, pcib_devclass, 0, 0);
214 fsl_pcib_probe(device_t dev)
218 node = ofw_bus_get_node(dev);
219 if (!fdt_is_type(node, "pci"))
222 if (!(fdt_is_compatible(node, "fsl,mpc8540-pci") ||
223 fdt_is_compatible(node, "fsl,mpc8548-pcie")))
226 device_set_desc(dev, "Freescale Integrated PCI/PCI-E Controller");
227 return (BUS_PROBE_DEFAULT);
231 fsl_pcib_attach(device_t dev)
233 struct fsl_pcib_softc *sc;
237 uint8_t ltssm, capptr;
239 sc = device_get_softc(dev);
243 sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
245 if (sc->sc_res == NULL) {
246 device_printf(dev, "could not map I/O memory\n");
249 sc->sc_bst = rman_get_bustag(sc->sc_res);
250 sc->sc_bsh = rman_get_bushandle(sc->sc_res);
253 if (!mtx_initialized) {
254 mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
258 cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_VENDOR, 2);
259 if (cfgreg != 0x1057 && cfgreg != 0x1957)
262 capptr = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_CAP_PTR, 1);
263 while (capptr != 0) {
264 cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, capptr, 2);
265 switch (cfgreg & 0xff) {
270 sc->sc_pcie_capreg = capptr;
273 capptr = (cfgreg >> 8) & 0xff;
276 node = ofw_bus_get_node(dev);
278 * Get PCI interrupt info.
280 if (fsl_pcib_intr_info(node, sc) != 0) {
281 device_printf(dev, "could not retrieve interrupt info\n");
286 * Configure decode windows for PCI(E) access.
288 if (fsl_pcib_decode_win(node, sc) != 0)
291 cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_COMMAND, 2);
292 cfgreg |= PCIM_CMD_SERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN |
294 fsl_pcib_cfgwrite(sc, 0, 0, 0, PCIR_COMMAND, cfgreg, 2);
296 sc->sc_devfn_tundra = -1;
297 sc->sc_devfn_via_ide = -1;
301 * Scan bus using firmware configured, 0 based bus numbering.
304 maxslot = (sc->sc_pcie) ? 0 : PCI_SLOTMAX;
305 fsl_pcib_init(sc, sc->sc_busnr, maxslot);
308 ltssm = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_LTSSM, 1);
309 if (ltssm < LTSSM_STAT_L0) {
311 printf("PCI %d: no PCIE link, skipping\n",
312 device_get_unit(dev));
317 fsl_pcib_err_init(dev);
319 device_add_child(dev, "pci", -1);
320 return (bus_generic_attach(dev));
323 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rid, sc->sc_res);
328 fsl_pcib_cfgread(struct fsl_pcib_softc *sc, u_int bus, u_int slot, u_int func,
329 u_int reg, int bytes)
333 if (bus == sc->sc_busnr - 1)
336 addr = CONFIG_ACCESS_ENABLE;
337 addr |= (bus & 0xff) << 16;
338 addr |= (slot & 0x1f) << 11;
339 addr |= (func & 0x7) << 8;
342 addr |= (reg & 0xf00) << 16;
344 mtx_lock_spin(&pcicfg_mtx);
345 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
349 data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
350 REG_CFG_DATA + (reg & 3));
353 data = le16toh(bus_space_read_2(sc->sc_bst, sc->sc_bsh,
354 REG_CFG_DATA + (reg & 2)));
357 data = le32toh(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
364 mtx_unlock_spin(&pcicfg_mtx);
369 fsl_pcib_cfgwrite(struct fsl_pcib_softc *sc, u_int bus, u_int slot, u_int func,
370 u_int reg, uint32_t data, int bytes)
374 if (bus == sc->sc_busnr - 1)
377 addr = CONFIG_ACCESS_ENABLE;
378 addr |= (bus & 0xff) << 16;
379 addr |= (slot & 0x1f) << 11;
380 addr |= (func & 0x7) << 8;
383 addr |= (reg & 0xf00) << 16;
385 mtx_lock_spin(&pcicfg_mtx);
386 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
390 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
391 REG_CFG_DATA + (reg & 3), data);
394 bus_space_write_2(sc->sc_bst, sc->sc_bsh,
395 REG_CFG_DATA + (reg & 2), htole16(data));
398 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
399 REG_CFG_DATA, htole32(data));
402 mtx_unlock_spin(&pcicfg_mtx);
407 dump(struct fsl_pcib_softc *sc)
411 #define RD(o) bus_space_read_4(sc->sc_bst, sc->sc_bsh, o)
412 for (i = 0; i < 5; i++) {
413 printf("POTAR%u =0x%08x\n", i, RD(REG_POTAR(i)));
414 printf("POTEAR%u =0x%08x\n", i, RD(REG_POTEAR(i)));
415 printf("POWBAR%u =0x%08x\n", i, RD(REG_POWBAR(i)));
416 printf("POWAR%u =0x%08x\n", i, RD(REG_POWAR(i)));
419 for (i = 1; i < 4; i++) {
420 printf("PITAR%u =0x%08x\n", i, RD(REG_PITAR(i)));
421 printf("PIWBAR%u =0x%08x\n", i, RD(REG_PIWBAR(i)));
422 printf("PIWBEAR%u=0x%08x\n", i, RD(REG_PIWBEAR(i)));
423 printf("PIWAR%u =0x%08x\n", i, RD(REG_PIWAR(i)));
428 for (i = 0; i < 0x48; i += 4) {
429 printf("cfg%02x=0x%08x\n", i, fsl_pcib_cfgread(sc, 0, 0, 0,
436 fsl_pcib_maxslots(device_t dev)
438 struct fsl_pcib_softc *sc = device_get_softc(dev);
440 return ((sc->sc_pcie) ? 0 : PCI_SLOTMAX);
444 fsl_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
445 u_int reg, int bytes)
447 struct fsl_pcib_softc *sc = device_get_softc(dev);
450 if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
452 devfn = DEVFN(bus, slot, func);
453 if (devfn == sc->sc_devfn_tundra)
455 if (devfn == sc->sc_devfn_via_ide && reg == PCIR_INTPIN)
457 return (fsl_pcib_cfgread(sc, bus, slot, func, reg, bytes));
461 fsl_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
462 u_int reg, uint32_t val, int bytes)
464 struct fsl_pcib_softc *sc = device_get_softc(dev);
466 if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
468 fsl_pcib_cfgwrite(sc, bus, slot, func, reg, val, bytes);
472 fsl_pcib_init_via(struct fsl_pcib_softc *sc, uint16_t device, int bus,
476 if (device == 0x0686) {
477 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x52, 0x34, 1);
478 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x77, 0x00, 1);
479 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x83, 0x98, 1);
480 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x85, 0x03, 1);
481 } else if (device == 0x0571) {
482 sc->sc_devfn_via_ide = DEVFN(bus, slot, fn);
483 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x40, 0x0b, 1);
488 fsl_pcib_init_bar(struct fsl_pcib_softc *sc, int bus, int slot, int func,
492 uint32_t addr, mask, size;
495 reg = PCIR_BAR(barno);
497 if (DEVFN(bus, slot, func) == sc->sc_devfn_via_ide) {
499 case 0: addr = 0x1f0; break;
500 case 1: addr = 0x3f4; break;
501 case 2: addr = 0x170; break;
502 case 3: addr = 0x374; break;
503 case 4: addr = 0xcc0; break;
506 fsl_pcib_write_config(sc->sc_dev, bus, slot, func, reg, addr, 4);
510 fsl_pcib_write_config(sc->sc_dev, bus, slot, func, reg, ~0, 4);
511 size = fsl_pcib_read_config(sc->sc_dev, bus, slot, func, reg, 4);
514 width = ((size & 7) == 4) ? 2 : 1;
516 if (size & 1) { /* I/O port */
517 allocp = &sc->sc_ioport_alloc;
519 if ((size & 0xffff0000) == 0)
521 } else { /* memory */
522 allocp = &sc->sc_iomem_alloc;
527 /* Sanity check (must be a power of 2). */
531 addr = (*allocp + mask) & ~mask;
532 *allocp = addr + size;
535 printf("PCI %u:%u:%u:%u: reg %x: size=%08x: addr=%08x\n",
536 device_get_unit(sc->sc_dev), bus, slot, func, reg,
539 fsl_pcib_write_config(sc->sc_dev, bus, slot, func, reg, addr, 4);
541 fsl_pcib_write_config(sc->sc_dev, bus, slot, func, reg + 4,
547 fsl_pcib_route_int(struct fsl_pcib_softc *sc, u_int bus, u_int slot, u_int func,
551 u_int devfn, intline;
553 unit = device_get_unit(sc->sc_dev);
555 devfn = DEVFN(bus, slot, func);
556 if (devfn == sc->sc_devfn_via_ide)
557 intline = MAP_IRQ(0, 14);
558 else if (devfn == sc->sc_devfn_via_ide + 1)
559 intline = MAP_IRQ(0, 10);
560 else if (devfn == sc->sc_devfn_via_ide + 2)
561 intline = MAP_IRQ(0, 10);
564 err = fdt_pci_route_intr(bus, slot, func, intpin,
565 &sc->sc_intr_info, &intline);
571 printf("PCI %u:%u:%u:%u: intpin %u: intline=%u\n",
572 unit, bus, slot, func, intpin, intline);
578 fsl_pcib_init(struct fsl_pcib_softc *sc, int bus, int maxslot)
581 int old_pribus, old_secbus, old_subbus;
582 int new_pribus, new_secbus, new_subbus;
583 int slot, func, maxfunc;
585 uint16_t vendor, device;
586 uint8_t command, hdrtype, class, subclass;
587 uint8_t intline, intpin;
590 for (slot = 0; slot <= maxslot; slot++) {
592 for (func = 0; func <= maxfunc; func++) {
593 hdrtype = fsl_pcib_read_config(sc->sc_dev, bus, slot,
594 func, PCIR_HDRTYPE, 1);
596 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
599 if (func == 0 && (hdrtype & PCIM_MFDEV))
600 maxfunc = PCI_FUNCMAX;
602 vendor = fsl_pcib_read_config(sc->sc_dev, bus, slot,
603 func, PCIR_VENDOR, 2);
604 device = fsl_pcib_read_config(sc->sc_dev, bus, slot,
605 func, PCIR_DEVICE, 2);
607 if (vendor == 0x1957 && device == 0x3fff) {
608 sc->sc_devfn_tundra = DEVFN(bus, slot, func);
612 command = fsl_pcib_read_config(sc->sc_dev, bus, slot,
613 func, PCIR_COMMAND, 1);
614 command &= ~(PCIM_CMD_MEMEN | PCIM_CMD_PORTEN);
615 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
616 PCIR_COMMAND, command, 1);
618 if (vendor == 0x1106)
619 fsl_pcib_init_via(sc, device, bus, slot, func);
621 /* Program the base address registers. */
622 maxbar = (hdrtype & PCIM_HDRTYPE) ? 1 : 6;
625 bar += fsl_pcib_init_bar(sc, bus, slot, func,
628 /* Perform interrupt routing. */
629 intpin = fsl_pcib_read_config(sc->sc_dev, bus, slot,
630 func, PCIR_INTPIN, 1);
631 intline = fsl_pcib_route_int(sc, bus, slot, func,
633 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
634 PCIR_INTLINE, intline, 1);
636 command |= PCIM_CMD_MEMEN | PCIM_CMD_PORTEN;
637 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
638 PCIR_COMMAND, command, 1);
641 * Handle PCI-PCI bridges
643 class = fsl_pcib_read_config(sc->sc_dev, bus, slot,
644 func, PCIR_CLASS, 1);
645 subclass = fsl_pcib_read_config(sc->sc_dev, bus, slot,
646 func, PCIR_SUBCLASS, 1);
648 /* Allow only proper PCI-PCI briges */
649 if (class != PCIC_BRIDGE)
651 if (subclass != PCIS_BRIDGE_PCI)
656 /* Program I/O decoder. */
657 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
658 PCIR_IOBASEL_1, sc->sc_ioport.rm_start >> 8, 1);
659 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
660 PCIR_IOLIMITL_1, sc->sc_ioport.rm_end >> 8, 1);
661 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
662 PCIR_IOBASEH_1, sc->sc_ioport.rm_start >> 16, 2);
663 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
664 PCIR_IOLIMITH_1, sc->sc_ioport.rm_end >> 16, 2);
666 /* Program (non-prefetchable) memory decoder. */
667 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
668 PCIR_MEMBASE_1, sc->sc_iomem.rm_start >> 16, 2);
669 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
670 PCIR_MEMLIMIT_1, sc->sc_iomem.rm_end >> 16, 2);
672 /* Program prefetchable memory decoder. */
673 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
674 PCIR_PMBASEL_1, 0x0010, 2);
675 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
676 PCIR_PMLIMITL_1, 0x000f, 2);
677 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
678 PCIR_PMBASEH_1, 0x00000000, 4);
679 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
680 PCIR_PMLIMITH_1, 0x00000000, 4);
682 /* Read currect bus register configuration */
683 old_pribus = fsl_pcib_read_config(sc->sc_dev, bus,
684 slot, func, PCIR_PRIBUS_1, 1);
685 old_secbus = fsl_pcib_read_config(sc->sc_dev, bus,
686 slot, func, PCIR_SECBUS_1, 1);
687 old_subbus = fsl_pcib_read_config(sc->sc_dev, bus,
688 slot, func, PCIR_SUBBUS_1, 1);
691 printf("PCI: reading firmware bus numbers for "
692 "secbus = %d (bus/sec/sub) = (%d/%d/%d)\n",
693 secbus, old_pribus, old_secbus, old_subbus);
698 secbus = fsl_pcib_init(sc, secbus,
699 (subclass == PCIS_BRIDGE_PCI) ? PCI_SLOTMAX : 0);
704 printf("PCI: translate firmware bus numbers "
705 "for secbus %d (%d/%d/%d) -> (%d/%d/%d)\n",
706 secbus, old_pribus, old_secbus, old_subbus,
707 new_pribus, new_secbus, new_subbus);
709 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
710 PCIR_PRIBUS_1, new_pribus, 1);
711 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
712 PCIR_SECBUS_1, new_secbus, 1);
713 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
714 PCIR_SUBBUS_1, new_subbus, 1);
722 fsl_pcib_inbound(struct fsl_pcib_softc *sc, int wnd, int tgt, u_long start,
723 u_long size, u_long pci_start)
725 uint32_t attr, bar, tar;
727 KASSERT(wnd > 0, ("%s: inbound window 0 is invalid", __func__));
730 /* XXX OCP85XX_TGTIF_RAM2, OCP85XX_TGTIF_RAM_INTL should be handled */
731 case OCP85XX_TGTIF_RAM1:
732 attr = 0xa0f55000 | (ffsl(size) - 2);
739 bar = pci_start >> 12;
741 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PITAR(wnd), tar);
742 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBEAR(wnd), 0);
743 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBAR(wnd), bar);
744 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWAR(wnd), attr);
748 fsl_pcib_outbound(struct fsl_pcib_softc *sc, int wnd, int res, u_long start,
749 u_long size, u_long pci_start)
751 uint32_t attr, bar, tar;
755 attr = 0x80044000 | (ffsl(size) - 2);
758 attr = 0x80088000 | (ffsl(size) - 2);
765 tar = pci_start >> 12;
767 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTAR(wnd), tar);
768 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTEAR(wnd), 0);
769 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWBAR(wnd), bar);
770 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWAR(wnd), attr);
774 fsl_pcib_set_range(struct fsl_pcib_softc *sc, int type, int wnd, u_long start,
779 bus_addr_t pci_start, pci_end;
780 bus_addr_t *vap, *allocp;
783 end = start + size - 1;
791 vap = &sc->sc_ioport_va;
792 allocp = &sc->sc_ioport_alloc;
799 vap = &sc->sc_iomem_va;
800 allocp = &sc->sc_iomem_alloc;
806 rm->rm_type = RMAN_ARRAY;
807 rm->rm_start = pci_start;
808 rm->rm_end = pci_end;
809 error = rman_init(rm);
813 error = rman_manage_region(rm, pci_start, pci_end);
819 *allocp = pci_start + alloc;
820 *vap = (uintptr_t)pmap_mapdev(start, size);
821 fsl_pcib_outbound(sc, wnd, type, start, size, pci_start);
826 fsl_pcib_err_init(device_t dev)
828 struct fsl_pcib_softc *sc;
829 uint16_t sec_stat, dsr;
830 uint32_t dcr, err_en;
832 sc = device_get_softc(dev);
834 sec_stat = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_SECSTAT_1, 2);
836 fsl_pcib_cfgwrite(sc, 0, 0, 0, PCIR_SECSTAT_1, 0xffff, 2);
838 /* Clear error bits */
839 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_MES_IER,
841 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_MES_DR,
843 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR,
846 dsr = fsl_pcib_cfgread(sc, 0, 0, 0,
847 sc->sc_pcie_capreg + PCIR_EXPRESS_DEVICE_STA, 2);
849 fsl_pcib_cfgwrite(sc, 0, 0, 0,
850 sc->sc_pcie_capreg + PCIR_EXPRESS_DEVICE_STA,
853 /* Enable all errors reporting */
855 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_EN,
858 /* Enable error reporting: URR, FER, NFER */
859 dcr = fsl_pcib_cfgread(sc, 0, 0, 0,
860 sc->sc_pcie_capreg + PCIR_EXPRESS_DEVICE_CTL, 4);
861 dcr |= PCIM_EXP_CTL_URR_ENABLE | PCIM_EXP_CTL_FER_ENABLE |
862 PCIM_EXP_CTL_NFER_ENABLE;
863 fsl_pcib_cfgwrite(sc, 0, 0, 0,
864 sc->sc_pcie_capreg + PCIR_EXPRESS_DEVICE_CTL, dcr, 4);
869 fsl_pcib_detach(device_t dev)
872 if (mtx_initialized) {
873 mtx_destroy(&pcicfg_mtx);
876 return (bus_generic_detach(dev));
879 static struct resource *
880 fsl_pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
881 u_long start, u_long end, u_long count, u_int flags)
883 struct fsl_pcib_softc *sc = device_get_softc(dev);
885 struct resource *res;
891 va = sc->sc_ioport_va;
895 va = sc->sc_iomem_va;
899 device_printf(dev, "%s requested ISA interrupt %lu\n",
900 device_get_nameunit(child), start);
902 flags |= RF_SHAREABLE;
903 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
904 type, rid, start, end, count, flags));
909 res = rman_reserve_resource(rm, start, end, count, flags, child);
913 rman_set_bustag(res, &bs_le_tag);
914 rman_set_bushandle(res, va + rman_get_start(res) - rm->rm_start);
919 fsl_pcib_release_resource(device_t dev, device_t child, int type, int rid,
920 struct resource *res)
923 return (rman_release_resource(res));
927 fsl_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
929 struct fsl_pcib_softc *sc = device_get_softc(dev);
933 *result = sc->sc_busnr;
935 case PCIB_IVAR_DOMAIN:
936 *result = device_get_unit(dev);
943 fsl_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
945 struct fsl_pcib_softc *sc = device_get_softc(dev);
949 sc->sc_busnr = value;
956 fsl_pcib_intr_info(phandle_t node, struct fsl_pcib_softc *sc)
960 if ((error = fdt_pci_intr_info(node, &sc->sc_intr_info)) != 0)
967 fsl_pcib_decode_win(phandle_t node, struct fsl_pcib_softc *sc)
969 struct fdt_pci_range io_space, mem_space;
975 if ((error = fdt_pci_ranges(node, &io_space, &mem_space)) != 0) {
976 device_printf(dev, "could not retrieve 'ranges' data\n");
981 * Configure LAW decode windows.
983 error = law_pci_target(sc->sc_res, &sc->sc_iomem_target,
984 &sc->sc_ioport_target);
986 device_printf(dev, "could not retrieve PCI LAW target info\n");
989 error = law_enable(sc->sc_iomem_target, mem_space.base_parent,
992 device_printf(dev, "could not program LAW for PCI MEM range\n");
995 error = law_enable(sc->sc_ioport_target, io_space.base_parent,
998 device_printf(dev, "could not program LAW for PCI IO range\n");
1003 * Set outbout and inbound windows.
1005 fsl_pcib_outbound(sc, 0, -1, 0, 0, 0);
1006 if ((error = fsl_pcib_set_range(sc, SYS_RES_MEMORY, 1,
1007 mem_space.base_parent, mem_space.len)) != 0)
1009 if ((error = fsl_pcib_set_range(sc, SYS_RES_IOPORT, 2,
1010 io_space.base_parent, io_space.len)) != 0)
1013 fsl_pcib_outbound(sc, 3, -1, 0, 0, 0);
1014 fsl_pcib_outbound(sc, 4, -1, 0, 0, 0);
1016 fsl_pcib_inbound(sc, 1, -1, 0, 0, 0);
1017 fsl_pcib_inbound(sc, 2, -1, 0, 0, 0);
1018 fsl_pcib_inbound(sc, 3, OCP85XX_TGTIF_RAM1, 0,
1019 2U * 1024U * 1024U * 1024U, 0);