2 * Copyright (c) 2008 MARVELL INTERNATIONAL LTD.
3 * Copyright (c) 2010 The FreeBSD Foundation
4 * Copyright (c) 2010-2012 Semihalf
7 * Developed by Semihalf.
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. Neither the name of MARVELL nor the names of contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Marvell integrated PCI/PCI-Express controller driver.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/queue.h>
54 #include <sys/endian.h>
56 #include <machine/intr.h>
61 #include <dev/fdt/fdt_common.h>
62 #include <dev/ofw/ofw_bus.h>
63 #include <dev/ofw/ofw_bus_subr.h>
64 #include <dev/pci/pcivar.h>
65 #include <dev/pci/pcireg.h>
66 #include <dev/pci/pcib_private.h>
68 #include "ofw_bus_if.h"
71 #include <machine/resource.h>
72 #include <machine/bus.h>
74 #include <arm/mv/mvreg.h>
75 #include <arm/mv/mvvar.h>
76 #include <arm/mv/mvwin.h>
79 #define debugf(fmt, args...) do { printf(fmt,##args); } while (0)
81 #define debugf(fmt, args...)
84 #define PCI_CFG_ENA (1U << 31)
85 #define PCI_CFG_BUS(bus) (((bus) & 0xff) << 16)
86 #define PCI_CFG_DEV(dev) (((dev) & 0x1f) << 11)
87 #define PCI_CFG_FUN(fun) (((fun) & 0x7) << 8)
88 #define PCI_CFG_PCIE_REG(reg) ((reg) & 0xfc)
90 #define PCI_REG_CFG_ADDR 0x0C78
91 #define PCI_REG_CFG_DATA 0x0C7C
93 #define PCIE_REG_CFG_ADDR 0x18F8
94 #define PCIE_REG_CFG_DATA 0x18FC
95 #define PCIE_REG_CONTROL 0x1A00
96 #define PCIE_CTRL_LINK1X 0x00000001
97 #define PCIE_REG_STATUS 0x1A04
98 #define PCIE_REG_IRQ_MASK 0x1910
100 #define PCIE_CONTROL_ROOT_CMPLX (1 << 1)
101 #define PCIE_CONTROL_HOT_RESET (1 << 24)
103 #define PCIE_LINK_TIMEOUT 1000000
105 #define PCIE_STATUS_LINK_DOWN 1
106 #define PCIE_STATUS_DEV_OFFS 16
108 /* Minimum PCI Memory and I/O allocations taken from PCI spec (in bytes) */
109 #define PCI_MIN_IO_ALLOC 4
110 #define PCI_MIN_MEM_ALLOC 16
112 #define BITS_PER_UINT32 (NBBY * sizeof(uint32_t))
114 struct mv_pcib_softc {
117 struct rman sc_mem_rman;
118 bus_addr_t sc_mem_base;
119 bus_addr_t sc_mem_size;
120 uint32_t sc_mem_map[MV_PCI_MEM_SLICE_SIZE /
121 (PCI_MIN_MEM_ALLOC * BITS_PER_UINT32)];
125 struct rman sc_io_rman;
126 bus_addr_t sc_io_base;
127 bus_addr_t sc_io_size;
128 uint32_t sc_io_map[MV_PCI_IO_SLICE_SIZE /
129 (PCI_MIN_IO_ALLOC * BITS_PER_UINT32)];
132 struct resource *sc_res;
133 bus_space_handle_t sc_bsh;
134 bus_space_tag_t sc_bst;
137 struct mtx sc_msi_mtx;
138 uint32_t sc_msi_bitmap;
140 int sc_busnr; /* Host bridge bus number */
141 int sc_devnr; /* Host bridge device number */
143 int sc_mode; /* Endpoint / Root Complex */
145 struct fdt_pci_intr sc_intr_info;
148 /* Local forward prototypes */
149 static int mv_pcib_decode_win(phandle_t, struct mv_pcib_softc *);
150 static void mv_pcib_hw_cfginit(void);
151 static uint32_t mv_pcib_hw_cfgread(struct mv_pcib_softc *, u_int, u_int,
153 static void mv_pcib_hw_cfgwrite(struct mv_pcib_softc *, u_int, u_int,
154 u_int, u_int, uint32_t, int);
155 static int mv_pcib_init(struct mv_pcib_softc *, int, int);
156 static int mv_pcib_init_all_bars(struct mv_pcib_softc *, int, int, int, int);
157 static void mv_pcib_init_bridge(struct mv_pcib_softc *, int, int, int);
158 static int mv_pcib_intr_info(phandle_t, struct mv_pcib_softc *);
159 static inline void pcib_write_irq_mask(struct mv_pcib_softc *, uint32_t);
160 static void mv_pcib_enable(struct mv_pcib_softc *, uint32_t);
161 static int mv_pcib_mem_init(struct mv_pcib_softc *);
163 /* Forward prototypes */
164 static int mv_pcib_probe(device_t);
165 static int mv_pcib_attach(device_t);
167 static struct resource *mv_pcib_alloc_resource(device_t, device_t, int, int *,
168 u_long, u_long, u_long, u_int);
169 static int mv_pcib_release_resource(device_t, device_t, int, int,
171 static int mv_pcib_read_ivar(device_t, device_t, int, uintptr_t *);
172 static int mv_pcib_write_ivar(device_t, device_t, int, uintptr_t);
174 static int mv_pcib_maxslots(device_t);
175 static uint32_t mv_pcib_read_config(device_t, u_int, u_int, u_int, u_int, int);
176 static void mv_pcib_write_config(device_t, u_int, u_int, u_int, u_int,
178 static int mv_pcib_route_interrupt(device_t, device_t, int);
179 #if defined(SOC_MV_ARMADAXP)
180 static int mv_pcib_alloc_msi(device_t, device_t, int, int, int *);
181 static int mv_pcib_map_msi(device_t, device_t, int, uint64_t *, uint32_t *);
182 static int mv_pcib_release_msi(device_t, device_t, int, int *);
186 * Bus interface definitions.
188 static device_method_t mv_pcib_methods[] = {
189 /* Device interface */
190 DEVMETHOD(device_probe, mv_pcib_probe),
191 DEVMETHOD(device_attach, mv_pcib_attach),
194 DEVMETHOD(bus_read_ivar, mv_pcib_read_ivar),
195 DEVMETHOD(bus_write_ivar, mv_pcib_write_ivar),
196 DEVMETHOD(bus_alloc_resource, mv_pcib_alloc_resource),
197 DEVMETHOD(bus_release_resource, mv_pcib_release_resource),
198 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
199 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
200 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
201 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
204 DEVMETHOD(pcib_maxslots, mv_pcib_maxslots),
205 DEVMETHOD(pcib_read_config, mv_pcib_read_config),
206 DEVMETHOD(pcib_write_config, mv_pcib_write_config),
207 DEVMETHOD(pcib_route_interrupt, mv_pcib_route_interrupt),
209 #if defined(SOC_MV_ARMADAXP)
210 DEVMETHOD(pcib_alloc_msi, mv_pcib_alloc_msi),
211 DEVMETHOD(pcib_release_msi, mv_pcib_release_msi),
212 DEVMETHOD(pcib_map_msi, mv_pcib_map_msi),
215 /* OFW bus interface */
216 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
217 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
218 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
219 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
220 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
225 static driver_t mv_pcib_driver = {
228 sizeof(struct mv_pcib_softc),
231 devclass_t pcib_devclass;
233 DRIVER_MODULE(pcib, nexus, mv_pcib_driver, pcib_devclass, 0, 0);
235 static struct mtx pcicfg_mtx;
238 mv_pcib_probe(device_t self)
242 node = ofw_bus_get_node(self);
243 if (!fdt_is_type(node, "pci"))
246 if (!(fdt_is_compatible(node, "mrvl,pcie") ||
247 fdt_is_compatible(node, "mrvl,pci")))
250 device_set_desc(self, "Marvell Integrated PCI/PCI-E Controller");
251 return (BUS_PROBE_DEFAULT);
255 mv_pcib_attach(device_t self)
257 struct mv_pcib_softc *sc;
258 phandle_t node, parnode;
262 sc = device_get_softc(self);
264 unit = fdt_get_unit(self);
267 node = ofw_bus_get_node(self);
268 parnode = OF_parent(node);
269 if (fdt_is_compatible(node, "mrvl,pcie")) {
270 sc->sc_type = MV_TYPE_PCIE;
271 sc->sc_win_target = MV_WIN_PCIE_TARGET(unit);
272 sc->sc_mem_win_attr = MV_WIN_PCIE_MEM_ATTR(unit);
273 sc->sc_io_win_attr = MV_WIN_PCIE_IO_ATTR(unit);
274 } else if (fdt_is_compatible(node, "mrvl,pci")) {
275 sc->sc_type = MV_TYPE_PCI;
276 sc->sc_win_target = MV_WIN_PCI_TARGET;
277 sc->sc_mem_win_attr = MV_WIN_PCI_MEM_ATTR;
278 sc->sc_io_win_attr = MV_WIN_PCI_IO_ATTR;
283 * Retrieve our mem-mapped registers range.
286 sc->sc_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &sc->sc_rid,
288 if (sc->sc_res == NULL) {
289 device_printf(self, "could not map memory\n");
292 sc->sc_bst = rman_get_bustag(sc->sc_res);
293 sc->sc_bsh = rman_get_bushandle(sc->sc_res);
295 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh, PCIE_REG_CONTROL);
296 sc->sc_mode = (val & PCIE_CONTROL_ROOT_CMPLX ? MV_MODE_ROOT :
300 * Get PCI interrupt info.
302 if ((sc->sc_mode == MV_MODE_ROOT) &&
303 (mv_pcib_intr_info(node, sc) != 0)) {
304 device_printf(self, "could not retrieve interrupt info\n");
309 * Configure decode windows for PCI(E) access.
311 if (mv_pcib_decode_win(node, sc) != 0)
314 mv_pcib_hw_cfginit();
317 * Enable PCIE device.
319 mv_pcib_enable(sc, unit);
324 err = mv_pcib_mem_init(sc);
328 if (sc->sc_mode == MV_MODE_ROOT) {
329 err = mv_pcib_init(sc, sc->sc_busnr,
330 mv_pcib_maxslots(sc->sc_dev));
334 device_add_child(self, "pci", -1);
337 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
338 PCIE_REG_STATUS, 1 << PCIE_STATUS_DEV_OFFS);
339 device_add_child(self, "pci_ep", -1);
342 mtx_init(&sc->sc_msi_mtx, "msi_mtx", NULL, MTX_DEF);
343 return (bus_generic_attach(self));
346 /* XXX SYS_RES_ should be released here */
347 rman_fini(&sc->sc_mem_rman);
348 rman_fini(&sc->sc_io_rman);
354 mv_pcib_enable(struct mv_pcib_softc *sc, uint32_t unit)
357 #if !defined(SOC_MV_ARMADAXP)
361 * Check if PCIE device is enabled.
363 if (read_cpu_ctrl(CPU_CONTROL) & CPU_CONTROL_PCIE_DISABLE(unit)) {
364 write_cpu_ctrl(CPU_CONTROL, read_cpu_ctrl(CPU_CONTROL) &
365 ~(CPU_CONTROL_PCIE_DISABLE(unit)));
367 timeout = PCIE_LINK_TIMEOUT;
368 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
370 while (((val & PCIE_STATUS_LINK_DOWN) == 1) && (timeout > 0)) {
373 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
380 if (sc->sc_mode == MV_MODE_ROOT) {
384 val = bus_space_read_4(sc->sc_bst, sc->sc_bsh, PCIR_COMMAND);
385 val |= PCIM_CMD_SERRESPEN | PCIM_CMD_BUSMASTEREN |
386 PCIM_CMD_MEMEN | PCIM_CMD_PORTEN;
387 bus_space_write_4(sc->sc_bst, sc->sc_bsh, PCIR_COMMAND, val);
392 mv_pcib_mem_init(struct mv_pcib_softc *sc)
399 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
400 err = rman_init(&sc->sc_mem_rman);
404 sc->sc_io_rman.rm_type = RMAN_ARRAY;
405 err = rman_init(&sc->sc_io_rman);
407 rman_fini(&sc->sc_mem_rman);
411 err = rman_manage_region(&sc->sc_mem_rman, sc->sc_mem_base,
412 sc->sc_mem_base + sc->sc_mem_size - 1);
416 err = rman_manage_region(&sc->sc_io_rman, sc->sc_io_base,
417 sc->sc_io_base + sc->sc_io_size - 1);
424 rman_fini(&sc->sc_mem_rman);
425 rman_fini(&sc->sc_io_rman);
430 static inline uint32_t
431 pcib_bit_get(uint32_t *map, uint32_t bit)
433 uint32_t n = bit / BITS_PER_UINT32;
435 bit = bit % BITS_PER_UINT32;
436 return (map[n] & (1 << bit));
440 pcib_bit_set(uint32_t *map, uint32_t bit)
442 uint32_t n = bit / BITS_PER_UINT32;
444 bit = bit % BITS_PER_UINT32;
445 map[n] |= (1 << bit);
448 static inline uint32_t
449 pcib_map_check(uint32_t *map, uint32_t start, uint32_t bits)
453 for (i = start; i < start + bits; i++)
454 if (pcib_bit_get(map, i))
461 pcib_map_set(uint32_t *map, uint32_t start, uint32_t bits)
465 for (i = start; i < start + bits; i++)
466 pcib_bit_set(map, i);
470 * The idea of this allocator is taken from ARM No-Cache memory
471 * management code (sys/arm/arm/vm_machdep.c).
474 pcib_alloc(struct mv_pcib_softc *sc, uint32_t smask)
476 uint32_t bits, bits_limit, i, *map, min_alloc, size;
481 base = sc->sc_io_base;
482 min_alloc = PCI_MIN_IO_ALLOC;
483 bits_limit = sc->sc_io_size / min_alloc;
487 base = sc->sc_mem_base;
488 min_alloc = PCI_MIN_MEM_ALLOC;
489 bits_limit = sc->sc_mem_size / min_alloc;
490 map = sc->sc_mem_map;
495 bits = size / min_alloc;
497 for (i = 0; i + bits <= bits_limit; i += bits)
498 if (pcib_map_check(map, i, bits)) {
499 pcib_map_set(map, i, bits);
500 addr = base + (i * min_alloc);
508 mv_pcib_init_bar(struct mv_pcib_softc *sc, int bus, int slot, int func,
514 reg = PCIR_BAR(barno);
517 * Need to init the BAR register with 0xffffffff before correct
520 mv_pcib_write_config(sc->sc_dev, bus, slot, func, reg, ~0, 4);
521 bar = mv_pcib_read_config(sc->sc_dev, bus, slot, func, reg, 4);
525 /* Calculate BAR size: 64 or 32 bit (in 32-bit units) */
526 width = ((bar & 7) == 4) ? 2 : 1;
528 addr = pcib_alloc(sc, bar);
533 printf("PCI %u:%u:%u: reg %x: smask=%08x: addr=%08x\n",
534 bus, slot, func, reg, bar, addr);
536 mv_pcib_write_config(sc->sc_dev, bus, slot, func, reg, addr, 4);
538 mv_pcib_write_config(sc->sc_dev, bus, slot, func, reg + 4,
545 mv_pcib_init_bridge(struct mv_pcib_softc *sc, int bus, int slot, int func)
547 bus_addr_t io_base, mem_base;
548 uint32_t io_limit, mem_limit;
551 io_base = sc->sc_io_base;
552 io_limit = io_base + sc->sc_io_size - 1;
553 mem_base = sc->sc_mem_base;
554 mem_limit = mem_base + sc->sc_mem_size - 1;
556 /* Configure I/O decode registers */
557 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_IOBASEL_1,
559 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_IOBASEH_1,
561 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_IOLIMITL_1,
563 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_IOLIMITH_1,
566 /* Configure memory decode registers */
567 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_MEMBASE_1,
569 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_MEMLIMIT_1,
572 /* Disable memory prefetch decode */
573 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_PMBASEL_1,
575 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_PMBASEH_1,
577 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_PMLIMITL_1,
579 mv_pcib_write_config(sc->sc_dev, bus, slot, func, PCIR_PMLIMITH_1,
582 secbus = mv_pcib_read_config(sc->sc_dev, bus, slot, func,
585 /* Configure buses behind the bridge */
586 mv_pcib_init(sc, secbus, PCI_SLOTMAX);
590 mv_pcib_init(struct mv_pcib_softc *sc, int bus, int maxslot)
592 int slot, func, maxfunc, error;
593 uint8_t hdrtype, command, class, subclass;
595 for (slot = 0; slot <= maxslot; slot++) {
597 for (func = 0; func <= maxfunc; func++) {
598 hdrtype = mv_pcib_read_config(sc->sc_dev, bus, slot,
599 func, PCIR_HDRTYPE, 1);
601 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
604 if (func == 0 && (hdrtype & PCIM_MFDEV))
605 maxfunc = PCI_FUNCMAX;
607 command = mv_pcib_read_config(sc->sc_dev, bus, slot,
608 func, PCIR_COMMAND, 1);
609 command &= ~(PCIM_CMD_MEMEN | PCIM_CMD_PORTEN);
610 mv_pcib_write_config(sc->sc_dev, bus, slot, func,
611 PCIR_COMMAND, command, 1);
613 error = mv_pcib_init_all_bars(sc, bus, slot, func,
619 command |= PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN |
621 mv_pcib_write_config(sc->sc_dev, bus, slot, func,
622 PCIR_COMMAND, command, 1);
624 /* Handle PCI-PCI bridges */
625 class = mv_pcib_read_config(sc->sc_dev, bus, slot,
626 func, PCIR_CLASS, 1);
627 subclass = mv_pcib_read_config(sc->sc_dev, bus, slot,
628 func, PCIR_SUBCLASS, 1);
630 if (class != PCIC_BRIDGE ||
631 subclass != PCIS_BRIDGE_PCI)
634 mv_pcib_init_bridge(sc, bus, slot, func);
638 /* Enable all ABCD interrupts */
639 pcib_write_irq_mask(sc, (0xF << 24));
645 mv_pcib_init_all_bars(struct mv_pcib_softc *sc, int bus, int slot,
646 int func, int hdrtype)
650 maxbar = (hdrtype & PCIM_HDRTYPE) ? 0 : 6;
653 /* Program the base address registers */
654 while (bar < maxbar) {
655 i = mv_pcib_init_bar(sc, bus, slot, func, bar);
658 device_printf(sc->sc_dev,
659 "PCI IO/Memory space exhausted\n");
667 static struct resource *
668 mv_pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
669 u_long start, u_long end, u_long count, u_int flags)
671 struct mv_pcib_softc *sc = device_get_softc(dev);
672 struct rman *rm = NULL;
673 struct resource *res;
677 rm = &sc->sc_io_rman;
680 rm = &sc->sc_mem_rman;
683 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
684 type, rid, start, end, count, flags));
687 if ((start == 0UL) && (end == ~0UL)) {
688 start = sc->sc_mem_base;
689 end = sc->sc_mem_base + sc->sc_mem_size - 1;
690 count = sc->sc_mem_size;
693 if ((start < sc->sc_mem_base) || (start + count - 1 != end) ||
694 (end > sc->sc_mem_base + sc->sc_mem_size - 1))
697 res = rman_reserve_resource(rm, start, end, count, flags, child);
701 rman_set_rid(res, *rid);
702 rman_set_bustag(res, fdtbus_bs_tag);
703 rman_set_bushandle(res, start);
705 if (flags & RF_ACTIVE)
706 if (bus_activate_resource(child, type, *rid, res)) {
707 rman_release_resource(res);
715 mv_pcib_release_resource(device_t dev, device_t child, int type, int rid,
716 struct resource *res)
719 if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY)
720 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
723 return (rman_release_resource(res));
727 mv_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
729 struct mv_pcib_softc *sc = device_get_softc(dev);
733 *result = sc->sc_busnr;
735 case PCIB_IVAR_DOMAIN:
736 *result = device_get_unit(dev);
744 mv_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
746 struct mv_pcib_softc *sc = device_get_softc(dev);
750 sc->sc_busnr = value;
758 pcib_write_irq_mask(struct mv_pcib_softc *sc, uint32_t mask)
761 if (!sc->sc_type != MV_TYPE_PCI)
764 bus_space_write_4(sc->sc_bst, sc->sc_bsh, PCIE_REG_IRQ_MASK, mask);
768 mv_pcib_hw_cfginit(void)
770 static int opened = 0;
775 mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
780 mv_pcib_hw_cfgread(struct mv_pcib_softc *sc, u_int bus, u_int slot,
781 u_int func, u_int reg, int bytes)
783 uint32_t addr, data, ca, cd;
785 ca = (sc->sc_type != MV_TYPE_PCI) ?
786 PCIE_REG_CFG_ADDR : PCI_REG_CFG_ADDR;
787 cd = (sc->sc_type != MV_TYPE_PCI) ?
788 PCIE_REG_CFG_DATA : PCI_REG_CFG_DATA;
789 addr = PCI_CFG_ENA | PCI_CFG_BUS(bus) | PCI_CFG_DEV(slot) |
790 PCI_CFG_FUN(func) | PCI_CFG_PCIE_REG(reg);
792 mtx_lock_spin(&pcicfg_mtx);
793 bus_space_write_4(sc->sc_bst, sc->sc_bsh, ca, addr);
798 data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
802 data = le16toh(bus_space_read_2(sc->sc_bst, sc->sc_bsh,
806 data = le32toh(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
810 mtx_unlock_spin(&pcicfg_mtx);
815 mv_pcib_hw_cfgwrite(struct mv_pcib_softc *sc, u_int bus, u_int slot,
816 u_int func, u_int reg, uint32_t data, int bytes)
818 uint32_t addr, ca, cd;
820 ca = (sc->sc_type != MV_TYPE_PCI) ?
821 PCIE_REG_CFG_ADDR : PCI_REG_CFG_ADDR;
822 cd = (sc->sc_type != MV_TYPE_PCI) ?
823 PCIE_REG_CFG_DATA : PCI_REG_CFG_DATA;
824 addr = PCI_CFG_ENA | PCI_CFG_BUS(bus) | PCI_CFG_DEV(slot) |
825 PCI_CFG_FUN(func) | PCI_CFG_PCIE_REG(reg);
827 mtx_lock_spin(&pcicfg_mtx);
828 bus_space_write_4(sc->sc_bst, sc->sc_bsh, ca, addr);
832 bus_space_write_1(sc->sc_bst, sc->sc_bsh,
833 cd + (reg & 3), data);
836 bus_space_write_2(sc->sc_bst, sc->sc_bsh,
837 cd + (reg & 2), htole16(data));
840 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
844 mtx_unlock_spin(&pcicfg_mtx);
848 mv_pcib_maxslots(device_t dev)
850 struct mv_pcib_softc *sc = device_get_softc(dev);
852 return ((sc->sc_type != MV_TYPE_PCI) ? 1 : PCI_SLOTMAX);
856 mv_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
857 u_int reg, int bytes)
859 struct mv_pcib_softc *sc = device_get_softc(dev);
861 /* Return ~0 if link is inactive or trying to read from Root */
862 if ((bus_space_read_4(sc->sc_bst, sc->sc_bsh, PCIE_REG_STATUS) &
863 PCIE_STATUS_LINK_DOWN) || (slot == 0))
866 return (mv_pcib_hw_cfgread(sc, bus, slot, func, reg, bytes));
870 mv_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
871 u_int reg, uint32_t val, int bytes)
873 struct mv_pcib_softc *sc = device_get_softc(dev);
875 /* Return if link is inactive or trying to write to Root */
876 if ((bus_space_read_4(sc->sc_bst, sc->sc_bsh, PCIE_REG_STATUS) &
877 PCIE_STATUS_LINK_DOWN) || (slot == 0))
880 mv_pcib_hw_cfgwrite(sc, bus, slot, func, reg, val, bytes);
884 mv_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
886 struct mv_pcib_softc *sc;
889 sc = device_get_softc(pcib);
891 err = fdt_pci_route_intr(pci_get_bus(dev), pci_get_slot(dev),
892 pci_get_function(dev), pin, &sc->sc_intr_info, &interrupt);
896 device_printf(pcib, "could not route pin %d for device %d.%d\n",
897 pin, pci_get_slot(dev), pci_get_function(dev));
898 return (PCI_INVALID_IRQ);
902 mv_pcib_decode_win(phandle_t node, struct mv_pcib_softc *sc)
904 struct fdt_pci_range io_space, mem_space;
910 if ((error = fdt_pci_ranges(node, &io_space, &mem_space)) != 0) {
911 device_printf(dev, "could not retrieve 'ranges' data\n");
915 /* Configure CPU decoding windows */
916 error = decode_win_cpu_set(sc->sc_win_target,
917 sc->sc_io_win_attr, io_space.base_parent, io_space.len, ~0);
919 device_printf(dev, "could not set up CPU decode "
920 "window for PCI IO\n");
923 error = decode_win_cpu_set(sc->sc_win_target,
924 sc->sc_mem_win_attr, mem_space.base_parent, mem_space.len,
925 mem_space.base_parent);
927 device_printf(dev, "could not set up CPU decode "
928 "windows for PCI MEM\n");
932 sc->sc_io_base = io_space.base_parent;
933 sc->sc_io_size = io_space.len;
935 sc->sc_mem_base = mem_space.base_parent;
936 sc->sc_mem_size = mem_space.len;
942 mv_pcib_intr_info(phandle_t node, struct mv_pcib_softc *sc)
946 if ((error = fdt_pci_intr_info(node, &sc->sc_intr_info)) != 0)
952 #if defined(SOC_MV_ARMADAXP)
954 mv_pcib_map_msi(device_t dev, device_t child, int irq, uint64_t *addr,
957 struct mv_pcib_softc *sc;
959 sc = device_get_softc(dev);
962 /* validate parameters */
963 if (isclr(&sc->sc_msi_bitmap, irq)) {
964 device_printf(dev, "invalid MSI 0x%x\n", irq);
968 mv_msi_data(irq, addr, data);
970 debugf("%s: irq: %d addr: %jx data: %x\n",
971 __func__, irq, *addr, *data);
977 mv_pcib_alloc_msi(device_t dev, device_t child, int count,
978 int maxcount __unused, int *irqs)
980 struct mv_pcib_softc *sc;
983 if (powerof2(count) == 0 || count > MSI_IRQ_NUM)
986 sc = device_get_softc(dev);
987 mtx_lock(&sc->sc_msi_mtx);
989 for (start = 0; (start + count) < MSI_IRQ_NUM; start++) {
990 for (i = start; i < start + count; i++) {
991 if (isset(&sc->sc_msi_bitmap, i))
994 if (i == start + count)
998 if ((start + count) == MSI_IRQ_NUM) {
999 mtx_unlock(&sc->sc_msi_mtx);
1003 for (i = start; i < start + count; i++) {
1004 setbit(&sc->sc_msi_bitmap, i);
1005 irqs[i] = MSI_IRQ + i;
1007 debugf("%s: start: %x count: %x\n", __func__, start, count);
1009 mtx_unlock(&sc->sc_msi_mtx);
1014 mv_pcib_release_msi(device_t dev, device_t child, int count, int *irqs)
1016 struct mv_pcib_softc *sc;
1019 sc = device_get_softc(dev);
1020 mtx_lock(&sc->sc_msi_mtx);
1022 for (i = 0; i < count; i++)
1023 clrbit(&sc->sc_msi_bitmap, irqs[i] - MSI_IRQ);
1025 mtx_unlock(&sc->sc_msi_mtx);