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/ofw/ofw_pci.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
61 #include <dev/ofw/ofwpci.h>
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pcireg.h>
64 #include <dev/pci/pcib_private.h>
66 #include "ofw_bus_if.h"
69 #include <machine/resource.h>
70 #include <machine/bus.h>
71 #include <machine/intr_machdep.h>
73 #include <powerpc/mpc85xx/mpc85xx.h>
75 #define REG_CFG_ADDR 0x0000
76 #define CONFIG_ACCESS_ENABLE 0x80000000
78 #define REG_CFG_DATA 0x0004
79 #define REG_INT_ACK 0x0008
81 #define REG_POTAR(n) (0x0c00 + 0x20 * (n))
82 #define REG_POTEAR(n) (0x0c04 + 0x20 * (n))
83 #define REG_POWBAR(n) (0x0c08 + 0x20 * (n))
84 #define REG_POWAR(n) (0x0c10 + 0x20 * (n))
86 #define REG_PITAR(n) (0x0e00 - 0x20 * (n))
87 #define REG_PIWBAR(n) (0x0e08 - 0x20 * (n))
88 #define REG_PIWBEAR(n) (0x0e0c - 0x20 * (n))
89 #define REG_PIWAR(n) (0x0e10 - 0x20 * (n))
91 #define REG_PEX_MES_DR 0x0020
92 #define REG_PEX_MES_IER 0x0028
93 #define REG_PEX_ERR_DR 0x0e00
94 #define REG_PEX_ERR_EN 0x0e08
96 #define REG_PEX_ERR_DR 0x0e00
97 #define REG_PEX_ERR_DR_ME 0x80000000
98 #define REG_PEX_ERR_DR_PCT 0x800000
99 #define REG_PEX_ERR_DR_PAT 0x400000
100 #define REG_PEX_ERR_DR_PCAC 0x200000
101 #define REG_PEX_ERR_DR_PNM 0x100000
102 #define REG_PEX_ERR_DR_CDNSC 0x80000
103 #define REG_PEX_ERR_DR_CRSNC 0x40000
104 #define REG_PEX_ERR_DR_ICCA 0x20000
105 #define REG_PEX_ERR_DR_IACA 0x10000
106 #define REG_PEX_ERR_DR_CRST 0x8000
107 #define REG_PEX_ERR_DR_MIS 0x4000
108 #define REG_PEX_ERR_DR_IOIS 0x2000
109 #define REG_PEX_ERR_DR_CIS 0x1000
110 #define REG_PEX_ERR_DR_CIEP 0x800
111 #define REG_PEX_ERR_DR_IOIEP 0x400
112 #define REG_PEX_ERR_DR_OAC 0x200
113 #define REG_PEX_ERR_DR_IOIA 0x100
114 #define REG_PEX_ERR_DR_IMBA 0x80
115 #define REG_PEX_ERR_DR_IIOBA 0x40
116 #define REG_PEX_ERR_DR_LDDE 0x20
117 #define REG_PEX_ERR_EN 0x0e08
119 #define PCIR_LTSSM 0x404
120 #define LTSSM_STAT_L0 0x16
122 #define DEVFN(b, s, f) ((b << 16) | (s << 8) | f)
124 struct fsl_pcib_softc {
125 struct ofw_pci_softc pci_sc;
129 bus_addr_t sc_iomem_start, sc_iomem_end;
130 int sc_ioport_target;
131 bus_addr_t sc_ioport_start, sc_ioport_end;
133 struct resource *sc_res;
134 bus_space_handle_t sc_bsh;
135 bus_space_tag_t sc_bst;
138 struct resource *sc_irq_res;
143 uint8_t sc_pcie_capreg; /* PCI-E Capability Reg Set */
145 /* Devices that need special attention. */
147 int sc_devfn_via_ide;
150 struct fsl_pcib_err_dr {
152 uint32_t err_dr_mask;
155 static const struct fsl_pcib_err_dr pci_err[] = {
156 {"ME", REG_PEX_ERR_DR_ME},
157 {"PCT", REG_PEX_ERR_DR_PCT},
158 {"PAT", REG_PEX_ERR_DR_PAT},
159 {"PCAC", REG_PEX_ERR_DR_PCAC},
160 {"PNM", REG_PEX_ERR_DR_PNM},
161 {"CDNSC", REG_PEX_ERR_DR_CDNSC},
162 {"CRSNC", REG_PEX_ERR_DR_CRSNC},
163 {"ICCA", REG_PEX_ERR_DR_ICCA},
164 {"IACA", REG_PEX_ERR_DR_IACA},
165 {"CRST", REG_PEX_ERR_DR_CRST},
166 {"MIS", REG_PEX_ERR_DR_MIS},
167 {"IOIS", REG_PEX_ERR_DR_IOIS},
168 {"CIS", REG_PEX_ERR_DR_CIS},
169 {"CIEP", REG_PEX_ERR_DR_CIEP},
170 {"IOIEP", REG_PEX_ERR_DR_IOIEP},
171 {"OAC", REG_PEX_ERR_DR_OAC},
172 {"IOIA", REG_PEX_ERR_DR_IOIA},
173 {"IMBA", REG_PEX_ERR_DR_IMBA},
174 {"IIOBA", REG_PEX_ERR_DR_IIOBA},
175 {"LDDE", REG_PEX_ERR_DR_LDDE}
178 /* Local forward declerations. */
179 static uint32_t fsl_pcib_cfgread(struct fsl_pcib_softc *, u_int, u_int, u_int,
181 static void fsl_pcib_cfgwrite(struct fsl_pcib_softc *, u_int, u_int, u_int,
182 u_int, uint32_t, int);
183 static int fsl_pcib_decode_win(phandle_t, struct fsl_pcib_softc *);
184 static void fsl_pcib_err_init(device_t);
185 static void fsl_pcib_inbound(struct fsl_pcib_softc *, int, int, uint64_t,
187 static int fsl_pcib_init(struct fsl_pcib_softc *, int, int);
188 static void fsl_pcib_outbound(struct fsl_pcib_softc *, int, int, uint64_t,
191 /* Forward declerations. */
192 static int fsl_pcib_attach(device_t);
193 static int fsl_pcib_detach(device_t);
194 static int fsl_pcib_probe(device_t);
196 static int fsl_pcib_maxslots(device_t);
197 static uint32_t fsl_pcib_read_config(device_t, u_int, u_int, u_int, u_int, int);
198 static void fsl_pcib_write_config(device_t, u_int, u_int, u_int, u_int,
201 /* Configuration r/w mutex. */
202 struct mtx pcicfg_mtx;
203 static int mtx_initialized = 0;
206 * Bus interface definitions.
208 static device_method_t fsl_pcib_methods[] = {
209 /* Device interface */
210 DEVMETHOD(device_probe, fsl_pcib_probe),
211 DEVMETHOD(device_attach, fsl_pcib_attach),
212 DEVMETHOD(device_detach, fsl_pcib_detach),
215 DEVMETHOD(pcib_maxslots, fsl_pcib_maxslots),
216 DEVMETHOD(pcib_read_config, fsl_pcib_read_config),
217 DEVMETHOD(pcib_write_config, fsl_pcib_write_config),
222 static devclass_t fsl_pcib_devclass;
224 DEFINE_CLASS_1(pcib, fsl_pcib_driver, fsl_pcib_methods,
225 sizeof(struct fsl_pcib_softc), ofw_pci_driver);
226 DRIVER_MODULE(pcib, ofwbus, fsl_pcib_driver, fsl_pcib_devclass, 0, 0);
229 fsl_pcib_err_intr(void *v)
231 struct fsl_pcib_softc *sc;
233 uint32_t err_reg, clear_reg;
237 sc = device_get_softc(dev);
240 err_reg = bus_space_read_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR);
242 /* Check which one error occurred */
243 for (i = 0; i < sizeof(pci_err)/sizeof(struct fsl_pcib_err_dr); i++) {
244 if (err_reg & pci_err[i].err_dr_mask) {
245 device_printf(dev, "PCI %d: report %s error\n",
246 device_get_unit(dev), pci_err[i].msg);
247 clear_reg |= pci_err[i].err_dr_mask;
251 /* Clear pending errors */
252 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR, clear_reg);
258 fsl_pcib_probe(device_t dev)
261 if (ofw_bus_get_type(dev) == NULL ||
262 strcmp(ofw_bus_get_type(dev), "pci") != 0)
265 if (!(ofw_bus_is_compatible(dev, "fsl,mpc8540-pci") ||
266 ofw_bus_is_compatible(dev, "fsl,mpc8540-pcie") ||
267 ofw_bus_is_compatible(dev, "fsl,mpc8548-pcie") ||
268 ofw_bus_is_compatible(dev, "fsl,p5020-pcie") ||
269 ofw_bus_is_compatible(dev, "fsl,qoriq-pcie-v2.2") ||
270 ofw_bus_is_compatible(dev, "fsl,qoriq-pcie")))
273 device_set_desc(dev, "Freescale Integrated PCI/PCI-E Controller");
274 return (BUS_PROBE_DEFAULT);
278 fsl_pcib_attach(device_t dev)
280 struct fsl_pcib_softc *sc;
283 int error, maxslot, rid;
284 uint8_t ltssm, capptr;
286 sc = device_get_softc(dev);
290 sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
292 if (sc->sc_res == NULL) {
293 device_printf(dev, "could not map I/O memory\n");
296 sc->sc_bst = rman_get_bustag(sc->sc_res);
297 sc->sc_bsh = rman_get_bushandle(sc->sc_res);
300 if (!mtx_initialized) {
301 mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
305 cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_VENDOR, 2);
306 if (cfgreg != 0x1057 && cfgreg != 0x1957)
309 capptr = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_CAP_PTR, 1);
310 while (capptr != 0) {
311 cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, capptr, 2);
312 switch (cfgreg & 0xff) {
317 sc->sc_pcie_capreg = capptr;
320 capptr = (cfgreg >> 8) & 0xff;
323 node = ofw_bus_get_node(dev);
326 * Initialize generic OF PCI interface (ranges, etc.)
329 error = ofw_pci_init(dev);
334 * Configure decode windows for PCI(E) access.
336 if (fsl_pcib_decode_win(node, sc) != 0)
339 cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_COMMAND, 2);
340 cfgreg |= PCIM_CMD_SERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN |
342 fsl_pcib_cfgwrite(sc, 0, 0, 0, PCIR_COMMAND, cfgreg, 2);
344 sc->sc_devfn_tundra = -1;
345 sc->sc_devfn_via_ide = -1;
349 * Scan bus using firmware configured, 0 based bus numbering.
351 maxslot = (sc->sc_pcie) ? 0 : PCI_SLOTMAX;
352 fsl_pcib_init(sc, sc->sc_busnr, maxslot);
355 ltssm = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_LTSSM, 1);
356 if (ltssm < LTSSM_STAT_L0) {
358 printf("PCI %d: no PCIE link, skipping\n",
359 device_get_unit(dev));
366 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
367 RF_ACTIVE | RF_SHAREABLE);
368 if (sc->sc_irq_res == NULL) {
369 error = fsl_pcib_detach(dev);
372 "Detach of the driver failed with error %d\n",
378 /* Setup interrupt handler */
379 error = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
380 NULL, (driver_intr_t *)fsl_pcib_err_intr, dev, &sc->sc_ih);
382 device_printf(dev, "Could not setup irq, %d\n", error);
384 error = fsl_pcib_detach(dev);
387 "Detach of the driver failed with error %d\n",
393 fsl_pcib_err_init(dev);
395 return (ofw_pci_attach(dev));
402 fsl_pcib_cfgread(struct fsl_pcib_softc *sc, u_int bus, u_int slot, u_int func,
403 u_int reg, int bytes)
407 addr = CONFIG_ACCESS_ENABLE;
408 addr |= (bus & 0xff) << 16;
409 addr |= (slot & 0x1f) << 11;
410 addr |= (func & 0x7) << 8;
413 addr |= (reg & 0xf00) << 16;
415 mtx_lock_spin(&pcicfg_mtx);
416 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
420 data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
421 REG_CFG_DATA + (reg & 3));
424 data = le16toh(bus_space_read_2(sc->sc_bst, sc->sc_bsh,
425 REG_CFG_DATA + (reg & 2)));
428 data = le32toh(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
435 mtx_unlock_spin(&pcicfg_mtx);
440 fsl_pcib_cfgwrite(struct fsl_pcib_softc *sc, u_int bus, u_int slot, u_int func,
441 u_int reg, uint32_t data, int bytes)
445 addr = CONFIG_ACCESS_ENABLE;
446 addr |= (bus & 0xff) << 16;
447 addr |= (slot & 0x1f) << 11;
448 addr |= (func & 0x7) << 8;
451 addr |= (reg & 0xf00) << 16;
453 mtx_lock_spin(&pcicfg_mtx);
454 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
458 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
459 REG_CFG_DATA + (reg & 3), data);
462 bus_space_write_2(sc->sc_bst, sc->sc_bsh,
463 REG_CFG_DATA + (reg & 2), htole16(data));
466 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
467 REG_CFG_DATA, htole32(data));
470 mtx_unlock_spin(&pcicfg_mtx);
475 dump(struct fsl_pcib_softc *sc)
479 #define RD(o) bus_space_read_4(sc->sc_bst, sc->sc_bsh, o)
480 for (i = 0; i < 5; i++) {
481 printf("POTAR%u =0x%08x\n", i, RD(REG_POTAR(i)));
482 printf("POTEAR%u =0x%08x\n", i, RD(REG_POTEAR(i)));
483 printf("POWBAR%u =0x%08x\n", i, RD(REG_POWBAR(i)));
484 printf("POWAR%u =0x%08x\n", i, RD(REG_POWAR(i)));
487 for (i = 1; i < 4; i++) {
488 printf("PITAR%u =0x%08x\n", i, RD(REG_PITAR(i)));
489 printf("PIWBAR%u =0x%08x\n", i, RD(REG_PIWBAR(i)));
490 printf("PIWBEAR%u=0x%08x\n", i, RD(REG_PIWBEAR(i)));
491 printf("PIWAR%u =0x%08x\n", i, RD(REG_PIWAR(i)));
496 for (i = 0; i < 0x48; i += 4) {
497 printf("cfg%02x=0x%08x\n", i, fsl_pcib_cfgread(sc, 0, 0, 0,
504 fsl_pcib_maxslots(device_t dev)
506 struct fsl_pcib_softc *sc = device_get_softc(dev);
508 return ((sc->sc_pcie) ? 0 : PCI_SLOTMAX);
512 fsl_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
513 u_int reg, int bytes)
515 struct fsl_pcib_softc *sc = device_get_softc(dev);
518 if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
520 devfn = DEVFN(bus, slot, func);
521 if (devfn == sc->sc_devfn_tundra)
523 if (devfn == sc->sc_devfn_via_ide && reg == PCIR_INTPIN)
525 return (fsl_pcib_cfgread(sc, bus, slot, func, reg, bytes));
529 fsl_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
530 u_int reg, uint32_t val, int bytes)
532 struct fsl_pcib_softc *sc = device_get_softc(dev);
534 if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
536 fsl_pcib_cfgwrite(sc, bus, slot, func, reg, val, bytes);
540 fsl_pcib_init_via(struct fsl_pcib_softc *sc, uint16_t device, int bus,
544 if (device == 0x0686) {
545 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x52, 0x34, 1);
546 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x77, 0x00, 1);
547 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x83, 0x98, 1);
548 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x85, 0x03, 1);
549 } else if (device == 0x0571) {
550 sc->sc_devfn_via_ide = DEVFN(bus, slot, fn);
551 fsl_pcib_write_config(sc->sc_dev, bus, slot, fn, 0x40, 0x0b, 1);
556 fsl_pcib_init(struct fsl_pcib_softc *sc, int bus, int maxslot)
559 int old_pribus, old_secbus, old_subbus;
560 int new_pribus, new_secbus, new_subbus;
561 int slot, func, maxfunc;
562 uint16_t vendor, device;
563 uint8_t command, hdrtype, subclass;
566 for (slot = 0; slot <= maxslot; slot++) {
568 for (func = 0; func <= maxfunc; func++) {
569 hdrtype = fsl_pcib_read_config(sc->sc_dev, bus, slot,
570 func, PCIR_HDRTYPE, 1);
572 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
575 if (func == 0 && (hdrtype & PCIM_MFDEV))
576 maxfunc = PCI_FUNCMAX;
578 vendor = fsl_pcib_read_config(sc->sc_dev, bus, slot,
579 func, PCIR_VENDOR, 2);
580 device = fsl_pcib_read_config(sc->sc_dev, bus, slot,
581 func, PCIR_DEVICE, 2);
583 if (vendor == 0x1957 && device == 0x3fff) {
584 sc->sc_devfn_tundra = DEVFN(bus, slot, func);
588 command = fsl_pcib_read_config(sc->sc_dev, bus, slot,
589 func, PCIR_COMMAND, 1);
590 command &= ~(PCIM_CMD_MEMEN | PCIM_CMD_PORTEN);
591 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
592 PCIR_COMMAND, command, 1);
594 if (vendor == 0x1106)
595 fsl_pcib_init_via(sc, device, bus, slot, func);
598 * Handle PCI-PCI bridges
600 subclass = fsl_pcib_read_config(sc->sc_dev, bus, slot,
601 func, PCIR_SUBCLASS, 1);
603 /* Allow all DEVTYPE 1 devices */
604 if (hdrtype != PCIM_HDRTYPE_BRIDGE)
609 /* Read currect bus register configuration */
610 old_pribus = fsl_pcib_read_config(sc->sc_dev, bus,
611 slot, func, PCIR_PRIBUS_1, 1);
612 old_secbus = fsl_pcib_read_config(sc->sc_dev, bus,
613 slot, func, PCIR_SECBUS_1, 1);
614 old_subbus = fsl_pcib_read_config(sc->sc_dev, bus,
615 slot, func, PCIR_SUBBUS_1, 1);
618 printf("PCI: reading firmware bus numbers for "
619 "secbus = %d (bus/sec/sub) = (%d/%d/%d)\n",
620 secbus, old_pribus, old_secbus, old_subbus);
625 secbus = fsl_pcib_init(sc, secbus,
626 (subclass == PCIS_BRIDGE_PCI) ? PCI_SLOTMAX : 0);
631 printf("PCI: translate firmware bus numbers "
632 "for secbus %d (%d/%d/%d) -> (%d/%d/%d)\n",
633 secbus, old_pribus, old_secbus, old_subbus,
634 new_pribus, new_secbus, new_subbus);
636 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
637 PCIR_PRIBUS_1, new_pribus, 1);
638 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
639 PCIR_SECBUS_1, new_secbus, 1);
640 fsl_pcib_write_config(sc->sc_dev, bus, slot, func,
641 PCIR_SUBBUS_1, new_subbus, 1);
649 fsl_pcib_inbound(struct fsl_pcib_softc *sc, int wnd, int tgt, uint64_t start,
650 uint64_t size, uint64_t pci_start)
652 uint32_t attr, bar, tar;
654 KASSERT(wnd > 0, ("%s: inbound window 0 is invalid", __func__));
657 /* XXX OCP85XX_TGTIF_RAM2, OCP85XX_TGTIF_RAM_INTL should be handled */
658 case OCP85XX_TGTIF_RAM1_85XX:
659 case OCP85XX_TGTIF_RAM1_QORIQ:
660 attr = 0xa0f55000 | (ffsl(size) - 2);
667 bar = pci_start >> 12;
669 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PITAR(wnd), tar);
670 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBEAR(wnd), 0);
671 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBAR(wnd), bar);
672 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWAR(wnd), attr);
676 fsl_pcib_outbound(struct fsl_pcib_softc *sc, int wnd, int res, uint64_t start,
677 uint64_t size, uint64_t pci_start)
679 uint32_t attr, bar, tar;
683 attr = 0x80044000 | (ffsll(size) - 2);
686 attr = 0x80088000 | (ffsll(size) - 2);
693 tar = pci_start >> 12;
695 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTAR(wnd), tar);
696 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTEAR(wnd), 0);
697 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWBAR(wnd), bar);
698 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWAR(wnd), attr);
703 fsl_pcib_err_init(device_t dev)
705 struct fsl_pcib_softc *sc;
706 uint16_t sec_stat, dsr;
707 uint32_t dcr, err_en;
709 sc = device_get_softc(dev);
711 sec_stat = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_SECSTAT_1, 2);
713 fsl_pcib_cfgwrite(sc, 0, 0, 0, PCIR_SECSTAT_1, 0xffff, 2);
715 /* Clear error bits */
716 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_MES_IER,
718 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_MES_DR,
720 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR,
723 dsr = fsl_pcib_cfgread(sc, 0, 0, 0,
724 sc->sc_pcie_capreg + PCIER_DEVICE_STA, 2);
726 fsl_pcib_cfgwrite(sc, 0, 0, 0,
727 sc->sc_pcie_capreg + PCIER_DEVICE_STA,
730 /* Enable all errors reporting */
732 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_EN,
735 /* Enable error reporting: URR, FER, NFER */
736 dcr = fsl_pcib_cfgread(sc, 0, 0, 0,
737 sc->sc_pcie_capreg + PCIER_DEVICE_CTL, 4);
738 dcr |= PCIEM_CTL_URR_ENABLE | PCIEM_CTL_FER_ENABLE |
739 PCIEM_CTL_NFER_ENABLE;
740 fsl_pcib_cfgwrite(sc, 0, 0, 0,
741 sc->sc_pcie_capreg + PCIER_DEVICE_CTL, dcr, 4);
746 fsl_pcib_detach(device_t dev)
749 if (mtx_initialized) {
750 mtx_destroy(&pcicfg_mtx);
753 return (bus_generic_detach(dev));
757 fsl_pcib_decode_win(phandle_t node, struct fsl_pcib_softc *sc)
764 fsl_pcib_outbound(sc, 0, -1, 0, 0, 0);
767 * Configure LAW decode windows.
769 error = law_pci_target(sc->sc_res, &sc->sc_iomem_target,
770 &sc->sc_ioport_target);
772 device_printf(dev, "could not retrieve PCI LAW target info\n");
776 for (i = 0; i < sc->pci_sc.sc_nrange; i++) {
777 switch (sc->pci_sc.sc_range[i].pci_hi &
778 OFW_PCI_PHYS_HI_SPACEMASK) {
779 case OFW_PCI_PHYS_HI_SPACE_CONFIG:
781 case OFW_PCI_PHYS_HI_SPACE_IO:
782 trgt = sc->sc_ioport_target;
783 fsl_pcib_outbound(sc, 2, SYS_RES_IOPORT,
784 sc->pci_sc.sc_range[i].host,
785 sc->pci_sc.sc_range[i].size,
786 sc->pci_sc.sc_range[i].pci);
787 sc->sc_ioport_start = sc->pci_sc.sc_range[i].pci;
788 sc->sc_ioport_end = sc->pci_sc.sc_range[i].pci +
789 sc->pci_sc.sc_range[i].size - 1;
791 case OFW_PCI_PHYS_HI_SPACE_MEM32:
792 case OFW_PCI_PHYS_HI_SPACE_MEM64:
793 trgt = sc->sc_iomem_target;
794 fsl_pcib_outbound(sc, 1, SYS_RES_MEMORY,
795 sc->pci_sc.sc_range[i].host,
796 sc->pci_sc.sc_range[i].size,
797 sc->pci_sc.sc_range[i].pci);
798 sc->sc_iomem_start = sc->pci_sc.sc_range[i].pci;
799 sc->sc_iomem_end = sc->pci_sc.sc_range[i].pci +
800 sc->pci_sc.sc_range[i].size - 1;
803 panic("Unknown range type %#x\n",
804 sc->pci_sc.sc_range[i].pci_hi &
805 OFW_PCI_PHYS_HI_SPACEMASK);
807 error = law_enable(trgt, sc->pci_sc.sc_range[i].host,
808 sc->pci_sc.sc_range[i].size);
810 device_printf(dev, "could not program LAW for range "
817 * Set outbout and inbound windows.
819 fsl_pcib_outbound(sc, 3, -1, 0, 0, 0);
820 fsl_pcib_outbound(sc, 4, -1, 0, 0, 0);
822 fsl_pcib_inbound(sc, 1, -1, 0, 0, 0);
823 fsl_pcib_inbound(sc, 2, -1, 0, 0, 0);
824 fsl_pcib_inbound(sc, 3, OCP85XX_TGTIF_RAM1, 0,
825 2U * 1024U * 1024U * 1024U, 0);