2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright 2006-2007 by Juniper Networks.
5 * Copyright 2008 Semihalf.
6 * Copyright 2010 The FreeBSD Foundation
9 * Portions of this software were developed by Semihalf
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * From: FreeBSD: src/sys/powerpc/mpc85xx/pci_ocp.c,v 1.9 2010/03/23 23:46:28 marcel
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/sockio.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/socket.h>
50 #include <sys/queue.h>
53 #include <sys/mutex.h>
54 #include <sys/queue.h>
56 #include <sys/endian.h>
62 #include <dev/ofw/ofw_pci.h>
63 #include <dev/ofw/ofw_bus.h>
64 #include <dev/ofw/ofw_bus_subr.h>
65 #include <dev/ofw/ofwpci.h>
66 #include <dev/pci/pcivar.h>
67 #include <dev/pci/pcireg.h>
68 #include <dev/pci/pcib_private.h>
70 #include "ofw_bus_if.h"
74 #include <machine/resource.h>
75 #include <machine/bus.h>
76 #include <machine/intr_machdep.h>
78 #include <powerpc/mpc85xx/mpc85xx.h>
80 #define REG_CFG_ADDR 0x0000
81 #define CONFIG_ACCESS_ENABLE 0x80000000
83 #define REG_CFG_DATA 0x0004
84 #define REG_INT_ACK 0x0008
86 #define REG_PEX_IP_BLK_REV1 0x0bf8
87 #define IP_MJ_M 0x0000ff00
89 #define IP_MN_M 0x000000ff
92 #define REG_POTAR(n) (0x0c00 + 0x20 * (n))
93 #define REG_POTEAR(n) (0x0c04 + 0x20 * (n))
94 #define REG_POWBAR(n) (0x0c08 + 0x20 * (n))
95 #define REG_POWAR(n) (0x0c10 + 0x20 * (n))
97 #define REG_PITAR(n) (0x0e00 - 0x20 * (n))
98 #define REG_PIWBAR(n) (0x0e08 - 0x20 * (n))
99 #define REG_PIWBEAR(n) (0x0e0c - 0x20 * (n))
100 #define REG_PIWAR(n) (0x0e10 - 0x20 * (n))
101 #define PIWAR_EN 0x80000000
102 #define PIWAR_PF 0x40000000
103 #define PIWAR_TRGT_M 0x00f00000
104 #define PIWAR_TRGT_S 20
105 #define PIWAR_TRGT_CCSR 0xe
106 #define PIWAR_TRGT_LOCAL 0xf
108 #define REG_PEX_MES_DR 0x0020
109 #define REG_PEX_MES_IER 0x0028
110 #define REG_PEX_ERR_DR 0x0e00
111 #define REG_PEX_ERR_EN 0x0e08
113 #define REG_PEX_ERR_DR 0x0e00
114 #define REG_PEX_ERR_DR_ME 0x80000000
115 #define REG_PEX_ERR_DR_PCT 0x800000
116 #define REG_PEX_ERR_DR_PAT 0x400000
117 #define REG_PEX_ERR_DR_PCAC 0x200000
118 #define REG_PEX_ERR_DR_PNM 0x100000
119 #define REG_PEX_ERR_DR_CDNSC 0x80000
120 #define REG_PEX_ERR_DR_CRSNC 0x40000
121 #define REG_PEX_ERR_DR_ICCA 0x20000
122 #define REG_PEX_ERR_DR_IACA 0x10000
123 #define REG_PEX_ERR_DR_CRST 0x8000
124 #define REG_PEX_ERR_DR_MIS 0x4000
125 #define REG_PEX_ERR_DR_IOIS 0x2000
126 #define REG_PEX_ERR_DR_CIS 0x1000
127 #define REG_PEX_ERR_DR_CIEP 0x800
128 #define REG_PEX_ERR_DR_IOIEP 0x400
129 #define REG_PEX_ERR_DR_OAC 0x200
130 #define REG_PEX_ERR_DR_IOIA 0x100
131 #define REG_PEX_ERR_DR_IMBA 0x80
132 #define REG_PEX_ERR_DR_IIOBA 0x40
133 #define REG_PEX_ERR_DR_LDDE 0x20
134 #define REG_PEX_ERR_EN 0x0e08
136 #define PCIR_LTSSM 0x404
137 #define LTSSM_STAT_L0 0x16
139 #define DEVFN(b, s, f) ((b << 16) | (s << 8) | f)
141 #define FSL_NUM_MSIS 256 /* 8 registers of 32 bits (8 hardware IRQs) */
143 struct fsl_pcib_softc {
144 struct ofw_pci_softc pci_sc;
146 struct mtx sc_cfg_mtx;
151 bus_addr_t sc_iomem_start, sc_iomem_end;
152 int sc_ioport_target;
153 bus_addr_t sc_ioport_start, sc_ioport_end;
155 struct resource *sc_res;
156 bus_space_handle_t sc_bsh;
157 bus_space_tag_t sc_bst;
160 struct resource *sc_irq_res;
165 uint8_t sc_pcie_capreg; /* PCI-E Capability Reg Set */
168 struct fsl_pcib_err_dr {
170 uint32_t err_dr_mask;
174 SLIST_ENTRY(fsl_msi_map) slist;
179 SLIST_HEAD(msi_head, fsl_msi_map) fsl_msis = SLIST_HEAD_INITIALIZER(msi_head);
181 static const struct fsl_pcib_err_dr pci_err[] = {
182 {"ME", REG_PEX_ERR_DR_ME},
183 {"PCT", REG_PEX_ERR_DR_PCT},
184 {"PAT", REG_PEX_ERR_DR_PAT},
185 {"PCAC", REG_PEX_ERR_DR_PCAC},
186 {"PNM", REG_PEX_ERR_DR_PNM},
187 {"CDNSC", REG_PEX_ERR_DR_CDNSC},
188 {"CRSNC", REG_PEX_ERR_DR_CRSNC},
189 {"ICCA", REG_PEX_ERR_DR_ICCA},
190 {"IACA", REG_PEX_ERR_DR_IACA},
191 {"CRST", REG_PEX_ERR_DR_CRST},
192 {"MIS", REG_PEX_ERR_DR_MIS},
193 {"IOIS", REG_PEX_ERR_DR_IOIS},
194 {"CIS", REG_PEX_ERR_DR_CIS},
195 {"CIEP", REG_PEX_ERR_DR_CIEP},
196 {"IOIEP", REG_PEX_ERR_DR_IOIEP},
197 {"OAC", REG_PEX_ERR_DR_OAC},
198 {"IOIA", REG_PEX_ERR_DR_IOIA},
199 {"IMBA", REG_PEX_ERR_DR_IMBA},
200 {"IIOBA", REG_PEX_ERR_DR_IIOBA},
201 {"LDDE", REG_PEX_ERR_DR_LDDE}
204 /* Local forward declerations. */
205 static uint32_t fsl_pcib_cfgread(struct fsl_pcib_softc *, u_int, u_int, u_int,
207 static void fsl_pcib_cfgwrite(struct fsl_pcib_softc *, u_int, u_int, u_int,
208 u_int, uint32_t, int);
209 static int fsl_pcib_decode_win(phandle_t, struct fsl_pcib_softc *);
210 static void fsl_pcib_err_init(device_t);
211 static void fsl_pcib_inbound(struct fsl_pcib_softc *, int, int, uint64_t,
213 static void fsl_pcib_outbound(struct fsl_pcib_softc *, int, int, uint64_t,
216 /* Forward declerations. */
217 static int fsl_pcib_attach(device_t);
218 static int fsl_pcib_detach(device_t);
219 static int fsl_pcib_probe(device_t);
221 static int fsl_pcib_maxslots(device_t);
222 static uint32_t fsl_pcib_read_config(device_t, u_int, u_int, u_int, u_int, int);
223 static void fsl_pcib_write_config(device_t, u_int, u_int, u_int, u_int,
225 static int fsl_pcib_alloc_msi(device_t dev, device_t child,
226 int count, int maxcount, int *irqs);
227 static int fsl_pcib_release_msi(device_t dev, device_t child,
228 int count, int *irqs);
229 static int fsl_pcib_alloc_msix(device_t dev, device_t child, int *irq);
230 static int fsl_pcib_release_msix(device_t dev, device_t child, int irq);
231 static int fsl_pcib_map_msi(device_t dev, device_t child,
232 int irq, uint64_t *addr, uint32_t *data);
234 static vmem_t *msi_vmem; /* Global MSI vmem, holds all MSI ranges. */
237 * Bus interface definitions.
239 static device_method_t fsl_pcib_methods[] = {
240 /* Device interface */
241 DEVMETHOD(device_probe, fsl_pcib_probe),
242 DEVMETHOD(device_attach, fsl_pcib_attach),
243 DEVMETHOD(device_detach, fsl_pcib_detach),
246 DEVMETHOD(pcib_maxslots, fsl_pcib_maxslots),
247 DEVMETHOD(pcib_read_config, fsl_pcib_read_config),
248 DEVMETHOD(pcib_write_config, fsl_pcib_write_config),
249 DEVMETHOD(pcib_alloc_msi, fsl_pcib_alloc_msi),
250 DEVMETHOD(pcib_release_msi, fsl_pcib_release_msi),
251 DEVMETHOD(pcib_alloc_msix, fsl_pcib_alloc_msix),
252 DEVMETHOD(pcib_release_msix, fsl_pcib_release_msix),
253 DEVMETHOD(pcib_map_msi, fsl_pcib_map_msi),
258 static devclass_t fsl_pcib_devclass;
260 DEFINE_CLASS_1(pcib, fsl_pcib_driver, fsl_pcib_methods,
261 sizeof(struct fsl_pcib_softc), ofw_pci_driver);
262 EARLY_DRIVER_MODULE(pcib, ofwbus, fsl_pcib_driver, fsl_pcib_devclass, 0, 0,
266 fsl_pcib_err_intr(void *v)
268 struct fsl_pcib_softc *sc;
270 uint32_t err_reg, clear_reg;
274 sc = device_get_softc(dev);
277 err_reg = bus_space_read_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR);
279 /* Check which one error occurred */
280 for (i = 0; i < sizeof(pci_err)/sizeof(struct fsl_pcib_err_dr); i++) {
281 if (err_reg & pci_err[i].err_dr_mask) {
282 device_printf(dev, "PCI %d: report %s error\n",
283 device_get_unit(dev), pci_err[i].msg);
284 clear_reg |= pci_err[i].err_dr_mask;
288 /* Clear pending errors */
289 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR, clear_reg);
293 fsl_pcib_probe(device_t dev)
296 if (ofw_bus_get_type(dev) == NULL ||
297 strcmp(ofw_bus_get_type(dev), "pci") != 0)
300 if (!(ofw_bus_is_compatible(dev, "fsl,mpc8540-pci") ||
301 ofw_bus_is_compatible(dev, "fsl,mpc8540-pcie") ||
302 ofw_bus_is_compatible(dev, "fsl,mpc8548-pcie") ||
303 ofw_bus_is_compatible(dev, "fsl,p5020-pcie") ||
304 ofw_bus_is_compatible(dev, "fsl,qoriq-pcie-v2.2") ||
305 ofw_bus_is_compatible(dev, "fsl,qoriq-pcie")))
308 device_set_desc(dev, "Freescale Integrated PCI/PCI-E Controller");
309 return (BUS_PROBE_DEFAULT);
313 fsl_pcib_attach(device_t dev)
315 struct fsl_pcib_softc *sc;
317 uint32_t cfgreg, brctl, ipreg;
319 uint8_t ltssm, capptr;
321 sc = device_get_softc(dev);
325 sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
327 if (sc->sc_res == NULL) {
328 device_printf(dev, "could not map I/O memory\n");
331 sc->sc_bst = rman_get_bustag(sc->sc_res);
332 sc->sc_bsh = rman_get_bushandle(sc->sc_res);
335 ipreg = bus_read_4(sc->sc_res, REG_PEX_IP_BLK_REV1);
336 sc->sc_ip_min = (ipreg & IP_MN_M) >> IP_MN_S;
337 sc->sc_ip_maj = (ipreg & IP_MJ_M) >> IP_MJ_S;
338 mtx_init(&sc->sc_cfg_mtx, "pcicfg", NULL, MTX_SPIN);
340 cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_VENDOR, 2);
341 if (cfgreg != 0x1057 && cfgreg != 0x1957)
344 capptr = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_CAP_PTR, 1);
345 while (capptr != 0) {
346 cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, capptr, 2);
347 switch (cfgreg & 0xff) {
352 sc->sc_pcie_capreg = capptr;
355 capptr = (cfgreg >> 8) & 0xff;
358 node = ofw_bus_get_node(dev);
361 * Initialize generic OF PCI interface (ranges, etc.)
364 error = ofw_pci_init(dev);
369 * Configure decode windows for PCI(E) access.
371 if (fsl_pcib_decode_win(node, sc) != 0)
374 cfgreg = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_COMMAND, 2);
375 cfgreg |= PCIM_CMD_SERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN |
377 fsl_pcib_cfgwrite(sc, 0, 0, 0, PCIR_COMMAND, cfgreg, 2);
379 /* Reset the bus. Needed for Radeon video cards. */
380 brctl = fsl_pcib_read_config(sc->sc_dev, 0, 0, 0,
381 PCIR_BRIDGECTL_1, 1);
382 brctl |= PCIB_BCR_SECBUS_RESET;
383 fsl_pcib_write_config(sc->sc_dev, 0, 0, 0,
384 PCIR_BRIDGECTL_1, brctl, 1);
386 brctl &= ~PCIB_BCR_SECBUS_RESET;
387 fsl_pcib_write_config(sc->sc_dev, 0, 0, 0,
388 PCIR_BRIDGECTL_1, brctl, 1);
392 ltssm = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_LTSSM, 1);
393 if (ltssm < LTSSM_STAT_L0) {
395 printf("PCI %d: no PCIE link, skipping\n",
396 device_get_unit(dev));
403 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
404 RF_ACTIVE | RF_SHAREABLE);
405 if (sc->sc_irq_res == NULL) {
406 error = fsl_pcib_detach(dev);
409 "Detach of the driver failed with error %d\n",
415 /* Setup interrupt handler */
416 error = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
417 NULL, fsl_pcib_err_intr, dev, &sc->sc_ih);
419 device_printf(dev, "Could not setup irq, %d\n", error);
421 error = fsl_pcib_detach(dev);
424 "Detach of the driver failed with error %d\n",
430 fsl_pcib_err_init(dev);
432 return (ofw_pci_attach(dev));
439 fsl_pcib_cfgread(struct fsl_pcib_softc *sc, u_int bus, u_int slot, u_int func,
440 u_int reg, int bytes)
444 addr = CONFIG_ACCESS_ENABLE;
445 addr |= (bus & 0xff) << 16;
446 addr |= (slot & 0x1f) << 11;
447 addr |= (func & 0x7) << 8;
450 addr |= (reg & 0xf00) << 16;
452 mtx_lock_spin(&sc->sc_cfg_mtx);
453 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
457 data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
458 REG_CFG_DATA + (reg & 3));
461 data = le16toh(bus_space_read_2(sc->sc_bst, sc->sc_bsh,
462 REG_CFG_DATA + (reg & 2)));
465 data = le32toh(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
472 mtx_unlock_spin(&sc->sc_cfg_mtx);
477 fsl_pcib_cfgwrite(struct fsl_pcib_softc *sc, u_int bus, u_int slot, u_int func,
478 u_int reg, uint32_t data, int bytes)
482 addr = CONFIG_ACCESS_ENABLE;
483 addr |= (bus & 0xff) << 16;
484 addr |= (slot & 0x1f) << 11;
485 addr |= (func & 0x7) << 8;
488 addr |= (reg & 0xf00) << 16;
490 mtx_lock_spin(&sc->sc_cfg_mtx);
491 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_CFG_ADDR, addr);
495 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
496 REG_CFG_DATA + (reg & 3), data);
499 bus_space_write_2(sc->sc_bst, sc->sc_bsh,
500 REG_CFG_DATA + (reg & 2), htole16(data));
503 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
504 REG_CFG_DATA, htole32(data));
507 mtx_unlock_spin(&sc->sc_cfg_mtx);
512 dump(struct fsl_pcib_softc *sc)
516 #define RD(o) bus_space_read_4(sc->sc_bst, sc->sc_bsh, o)
517 for (i = 0; i < 5; i++) {
518 printf("POTAR%u =0x%08x\n", i, RD(REG_POTAR(i)));
519 printf("POTEAR%u =0x%08x\n", i, RD(REG_POTEAR(i)));
520 printf("POWBAR%u =0x%08x\n", i, RD(REG_POWBAR(i)));
521 printf("POWAR%u =0x%08x\n", i, RD(REG_POWAR(i)));
524 for (i = 1; i < 4; i++) {
525 printf("PITAR%u =0x%08x\n", i, RD(REG_PITAR(i)));
526 printf("PIWBAR%u =0x%08x\n", i, RD(REG_PIWBAR(i)));
527 printf("PIWBEAR%u=0x%08x\n", i, RD(REG_PIWBEAR(i)));
528 printf("PIWAR%u =0x%08x\n", i, RD(REG_PIWAR(i)));
533 for (i = 0; i < 0x48; i += 4) {
534 printf("cfg%02x=0x%08x\n", i, fsl_pcib_cfgread(sc, 0, 0, 0,
541 fsl_pcib_maxslots(device_t dev)
543 struct fsl_pcib_softc *sc = device_get_softc(dev);
545 return ((sc->sc_pcie) ? 0 : PCI_SLOTMAX);
549 fsl_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
550 u_int reg, int bytes)
552 struct fsl_pcib_softc *sc = device_get_softc(dev);
555 if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
557 devfn = DEVFN(bus, slot, func);
559 return (fsl_pcib_cfgread(sc, bus, slot, func, reg, bytes));
563 fsl_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
564 u_int reg, uint32_t val, int bytes)
566 struct fsl_pcib_softc *sc = device_get_softc(dev);
568 if (bus == sc->sc_busnr && !sc->sc_pcie && slot < 10)
570 fsl_pcib_cfgwrite(sc, bus, slot, func, reg, val, bytes);
574 fsl_pcib_inbound(struct fsl_pcib_softc *sc, int wnd, int tgt, uint64_t start,
575 uint64_t size, uint64_t pci_start)
577 uint32_t attr, bar, tar;
579 KASSERT(wnd > 0, ("%s: inbound window 0 is invalid", __func__));
587 case PIWAR_TRGT_LOCAL:
588 attr |= (ffsl(size) - 2);
590 attr |= (tgt << PIWAR_TRGT_S);
594 bar = pci_start >> 12;
596 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PITAR(wnd), tar);
597 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBEAR(wnd), 0);
598 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWBAR(wnd), bar);
599 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PIWAR(wnd), attr);
603 fsl_pcib_outbound(struct fsl_pcib_softc *sc, int wnd, int res, uint64_t start,
604 uint64_t size, uint64_t pci_start)
606 uint32_t attr, bar, tar;
610 attr = 0x80044000 | (ffsll(size) - 2);
613 attr = 0x80088000 | (ffsll(size) - 2);
620 tar = pci_start >> 12;
622 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTAR(wnd), tar);
623 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POTEAR(wnd), 0);
624 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWBAR(wnd), bar);
625 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_POWAR(wnd), attr);
629 fsl_pcib_err_init(device_t dev)
631 struct fsl_pcib_softc *sc;
632 uint16_t sec_stat, dsr;
633 uint32_t dcr, err_en;
635 sc = device_get_softc(dev);
637 sec_stat = fsl_pcib_cfgread(sc, 0, 0, 0, PCIR_SECSTAT_1, 2);
639 fsl_pcib_cfgwrite(sc, 0, 0, 0, PCIR_SECSTAT_1, 0xffff, 2);
641 /* Clear error bits */
642 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_MES_IER,
644 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_MES_DR,
646 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_DR,
649 dsr = fsl_pcib_cfgread(sc, 0, 0, 0,
650 sc->sc_pcie_capreg + PCIER_DEVICE_STA, 2);
652 fsl_pcib_cfgwrite(sc, 0, 0, 0,
653 sc->sc_pcie_capreg + PCIER_DEVICE_STA,
656 /* Enable all errors reporting */
658 bus_space_write_4(sc->sc_bst, sc->sc_bsh, REG_PEX_ERR_EN,
661 /* Enable error reporting: URR, FER, NFER */
662 dcr = fsl_pcib_cfgread(sc, 0, 0, 0,
663 sc->sc_pcie_capreg + PCIER_DEVICE_CTL, 4);
664 dcr |= PCIEM_CTL_URR_ENABLE | PCIEM_CTL_FER_ENABLE |
665 PCIEM_CTL_NFER_ENABLE;
666 fsl_pcib_cfgwrite(sc, 0, 0, 0,
667 sc->sc_pcie_capreg + PCIER_DEVICE_CTL, dcr, 4);
672 fsl_pcib_detach(device_t dev)
674 struct fsl_pcib_softc *sc;
676 sc = device_get_softc(dev);
678 mtx_destroy(&sc->sc_cfg_mtx);
680 return (bus_generic_detach(dev));
684 fsl_pcib_decode_win(phandle_t node, struct fsl_pcib_softc *sc)
691 fsl_pcib_outbound(sc, 0, -1, 0, 0, 0);
694 * Configure LAW decode windows.
696 error = law_pci_target(sc->sc_res, &sc->sc_iomem_target,
697 &sc->sc_ioport_target);
699 device_printf(dev, "could not retrieve PCI LAW target info\n");
703 for (i = 0; i < sc->pci_sc.sc_nrange; i++) {
704 switch (sc->pci_sc.sc_range[i].pci_hi &
705 OFW_PCI_PHYS_HI_SPACEMASK) {
706 case OFW_PCI_PHYS_HI_SPACE_CONFIG:
708 case OFW_PCI_PHYS_HI_SPACE_IO:
709 trgt = sc->sc_ioport_target;
710 fsl_pcib_outbound(sc, 2, SYS_RES_IOPORT,
711 sc->pci_sc.sc_range[i].host,
712 sc->pci_sc.sc_range[i].size,
713 sc->pci_sc.sc_range[i].pci);
714 sc->sc_ioport_start = sc->pci_sc.sc_range[i].pci;
715 sc->sc_ioport_end = sc->pci_sc.sc_range[i].pci +
716 sc->pci_sc.sc_range[i].size - 1;
718 case OFW_PCI_PHYS_HI_SPACE_MEM32:
719 case OFW_PCI_PHYS_HI_SPACE_MEM64:
720 trgt = sc->sc_iomem_target;
721 fsl_pcib_outbound(sc, 1, SYS_RES_MEMORY,
722 sc->pci_sc.sc_range[i].host,
723 sc->pci_sc.sc_range[i].size,
724 sc->pci_sc.sc_range[i].pci);
725 sc->sc_iomem_start = sc->pci_sc.sc_range[i].pci;
726 sc->sc_iomem_end = sc->pci_sc.sc_range[i].pci +
727 sc->pci_sc.sc_range[i].size - 1;
730 panic("Unknown range type %#x\n",
731 sc->pci_sc.sc_range[i].pci_hi &
732 OFW_PCI_PHYS_HI_SPACEMASK);
734 error = law_enable(trgt, sc->pci_sc.sc_range[i].host,
735 sc->pci_sc.sc_range[i].size);
737 device_printf(dev, "could not program LAW for range "
744 * Set outbout and inbound windows.
746 fsl_pcib_outbound(sc, 3, -1, 0, 0, 0);
747 fsl_pcib_outbound(sc, 4, -1, 0, 0, 0);
749 fsl_pcib_inbound(sc, 1, -1, 0, 0, 0);
750 fsl_pcib_inbound(sc, 2, -1, 0, 0, 0);
751 fsl_pcib_inbound(sc, 3, PIWAR_TRGT_LOCAL, 0,
754 /* Direct-map the CCSR for MSIs. */
755 /* Freescale PCIe 2.x has a dedicated MSI window. */
756 /* inbound window 8 makes it hit 0xD00 offset, the MSI window. */
757 if (sc->sc_ip_maj >= 2)
758 fsl_pcib_inbound(sc, 8, PIWAR_TRGT_CCSR, ccsrbar_pa,
759 ccsrbar_size, ccsrbar_pa);
761 fsl_pcib_inbound(sc, 1, PIWAR_TRGT_CCSR, ccsrbar_pa,
762 ccsrbar_size, ccsrbar_pa);
767 static int fsl_pcib_alloc_msi(device_t dev, device_t child,
768 int count, int maxcount, int *irqs)
770 struct fsl_pcib_softc *sc;
774 sc = device_get_softc(dev);
775 if (msi_vmem == NULL)
778 err = vmem_xalloc(msi_vmem, count, powerof2(count), 0, 0,
779 VMEM_ADDR_MIN, VMEM_ADDR_MAX, M_BESTFIT | M_WAITOK, &start);
784 for (i = 0; i < count; i++)
790 static int fsl_pcib_release_msi(device_t dev, device_t child,
791 int count, int *irqs)
793 if (msi_vmem == NULL)
796 vmem_xfree(msi_vmem, irqs[0], count);
800 static int fsl_pcib_alloc_msix(device_t dev, device_t child, int *irq)
802 return (fsl_pcib_alloc_msi(dev, child, 1, 1, irq));
805 static int fsl_pcib_release_msix(device_t dev, device_t child, int irq)
807 return (fsl_pcib_release_msi(dev, child, 1, &irq));
810 static int fsl_pcib_map_msi(device_t dev, device_t child,
811 int irq, uint64_t *addr, uint32_t *data)
813 struct fsl_msi_map *mp;
815 SLIST_FOREACH(mp, &fsl_msis, slist) {
816 if (irq >= mp->irq_base && irq < mp->irq_base + FSL_NUM_MSIS)
824 *addr = ccsrbar_pa + mp->target;
830 * Linux device trees put the msi@<x> as children of the SoC, with ranges based
831 * on the CCSR. Since rman doesn't permit overlapping or sub-ranges between
832 * devices (bus_space_subregion(9) could do it, but let's not touch the PIC
833 * driver just to allocate a subregion for a sibling driver). This driver will
834 * use ccsr_write() and ccsr_read() instead.
837 #define FSL_NUM_IRQS 8
838 #define FSL_NUM_MSI_PER_IRQ 32
839 #define FSL_MSI_TARGET 0x140
841 struct fsl_msi_softc {
843 vm_offset_t sc_target;
845 struct fsl_msi_map sc_map;
847 /* This struct gets passed as the filter private data. */
848 struct fsl_msi_softc *sc_ptr; /* Pointer back to softc. */
849 struct resource *res;
852 int vectors[FSL_NUM_MSI_PER_IRQ];
854 } sc_msi_irq[FSL_NUM_IRQS];
858 fsl_msi_intr_filter(void *priv)
860 struct fsl_msi_irq *data = priv;
864 reg = ccsr_read4(ccsrbar_va + data->reg);
868 powerpc_dispatch_intr(data->vectors[i], NULL);
873 return (FILTER_HANDLED);
877 fsl_msi_probe(device_t dev)
879 if (!ofw_bus_is_compatible(dev, "fsl,mpic-msi"))
882 device_set_desc(dev, "Freescale MSI");
884 return (BUS_PROBE_DEFAULT);
888 fsl_msi_attach(device_t dev)
890 struct fsl_msi_softc *sc;
891 struct fsl_msi_irq *irq;
894 sc = device_get_softc(dev);
896 if (msi_vmem == NULL)
897 msi_vmem = vmem_create("MPIC MSI", 0, 0, 1, 0, M_BESTFIT | M_WAITOK);
899 /* Manually play with resource entries. */
900 sc->sc_base = bus_get_resource_start(dev, SYS_RES_MEMORY, 0);
901 sc->sc_map.target = bus_get_resource_start(dev, SYS_RES_MEMORY, 1);
903 if (sc->sc_map.target == 0)
904 sc->sc_map.target = sc->sc_base + FSL_MSI_TARGET;
906 for (i = 0; i < FSL_NUM_IRQS; i++) {
907 irq = &sc->sc_msi_irq[i];
909 irq->reg = sc->sc_base + 16 * i;
910 irq->res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
911 &irq->irq, RF_ACTIVE);
912 bus_setup_intr(dev, irq->res, INTR_TYPE_MISC | INTR_MPSAFE,
913 fsl_msi_intr_filter, NULL, irq, &irq->cookie);
915 sc->sc_map.irq_base = powerpc_register_pic(dev, ofw_bus_get_node(dev),
918 /* Let vmem and the IRQ subsystem work their magic for allocations. */
919 vmem_add(msi_vmem, sc->sc_map.irq_base, FSL_NUM_MSIS, M_WAITOK);
921 SLIST_INSERT_HEAD(&fsl_msis, &sc->sc_map, slist);
927 fsl_msi_enable(device_t dev, u_int irq, u_int vector, void **priv)
929 struct fsl_msi_softc *sc;
930 struct fsl_msi_irq *irqd;
932 sc = device_get_softc(dev);
934 irqd = &sc->sc_msi_irq[irq / FSL_NUM_MSI_PER_IRQ];
935 irqd->vectors[irq % FSL_NUM_MSI_PER_IRQ] = vector;
938 static device_method_t fsl_msi_methods[] = {
939 DEVMETHOD(device_probe, fsl_msi_probe),
940 DEVMETHOD(device_attach, fsl_msi_attach),
942 DEVMETHOD(pic_enable, fsl_msi_enable),
946 static devclass_t fsl_msi_devclass;
948 static driver_t fsl_msi_driver = {
951 sizeof(struct fsl_msi_softc)
954 EARLY_DRIVER_MODULE(fsl_msi, simplebus, fsl_msi_driver, fsl_msi_devclass, 0, 0,
955 BUS_PASS_INTERRUPT + 1);