2 * Copyright (c) 2010-2011 Juli Mallett <jmallett@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/endian.h>
37 #include <sys/interrupt.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
45 #include <vm/vm_extern.h>
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
50 #include <contrib/octeon-sdk/cvmx.h>
51 #include <mips/cavium/octeon_irq.h>
52 #include <contrib/octeon-sdk/cvmx-pcie.h>
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcib_private.h>
59 #include <mips/cavium/octopcireg.h>
60 #include <mips/cavium/octopcivar.h>
64 #define NPI_WRITE(addr, value) cvmx_write64_uint32((addr) ^ 4, (value))
65 #define NPI_READ(addr) cvmx_read64_uint32((addr) ^ 4)
67 struct octopci_softc {
73 bus_addr_t sc_io_base;
77 bus_addr_t sc_mem1_base;
78 unsigned sc_mem1_next;
82 static void octopci_identify(driver_t *, device_t);
83 static int octopci_probe(device_t);
84 static int octopci_attach(device_t);
85 static int octopci_read_ivar(device_t, device_t, int,
87 static struct resource *octopci_alloc_resource(device_t, device_t, int, int *,
88 rman_res_t, rman_res_t,
90 static int octopci_activate_resource(device_t, device_t, int, int,
92 static int octopci_maxslots(device_t);
93 static uint32_t octopci_read_config(device_t, u_int, u_int, u_int, u_int, int);
94 static void octopci_write_config(device_t, u_int, u_int, u_int, u_int,
96 static int octopci_route_interrupt(device_t, device_t, int);
98 static unsigned octopci_init_bar(device_t, unsigned, unsigned, unsigned, unsigned, uint8_t *);
99 static unsigned octopci_init_device(device_t, unsigned, unsigned, unsigned, unsigned);
100 static unsigned octopci_init_bus(device_t, unsigned);
101 static void octopci_init_pci(device_t);
102 static uint64_t octopci_cs_addr(unsigned, unsigned, unsigned, unsigned);
105 octopci_identify(driver_t *drv, device_t parent)
107 BUS_ADD_CHILD(parent, 0, "pcib", 0);
108 if (octeon_has_feature(OCTEON_FEATURE_PCIE))
109 BUS_ADD_CHILD(parent, 0, "pcib", 1);
113 octopci_probe(device_t dev)
115 if (octeon_has_feature(OCTEON_FEATURE_PCIE)) {
116 device_set_desc(dev, "Cavium Octeon PCIe bridge");
120 /* Check whether we are a PCI host. */
121 if ((cvmx_sysinfo_get()->bootloader_config_flags & CVMX_BOOTINFO_CFG_FLAG_PCI_HOST) == 0)
124 if (device_get_unit(dev) != 0)
127 device_set_desc(dev, "Cavium Octeon PCI bridge");
132 octopci_attach(device_t dev)
134 struct octopci_softc *sc;
138 sc = device_get_softc(dev);
141 if (octeon_has_feature(OCTEON_FEATURE_PCIE)) {
142 sc->sc_domain = device_get_unit(dev);
144 error = cvmx_pcie_rc_initialize(sc->sc_domain);
146 device_printf(dev, "Failed to put PCIe bus in host mode.\n");
151 * In RC mode, the Simple Executive programs the first bus to
152 * be numbered as bus 1, because some IDT bridges used in
153 * Octeon systems object to being attached to bus 0.
157 sc->sc_io_base = CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address(sc->sc_domain));
158 sc->sc_io.rm_descr = "Cavium Octeon PCIe I/O Ports";
160 sc->sc_mem1_base = CVMX_ADD_IO_SEG(cvmx_pcie_get_mem_base_address(sc->sc_domain));
161 sc->sc_mem1.rm_descr = "Cavium Octeon PCIe Memory";
163 octopci_init_pci(dev);
168 sc->sc_io_base = CVMX_ADDR_DID(CVMX_FULL_DID(CVMX_OCT_DID_PCI, CVMX_OCT_SUBDID_PCI_IO));
169 sc->sc_io.rm_descr = "Cavium Octeon PCI I/O Ports";
171 sc->sc_mem1_base = CVMX_ADDR_DID(CVMX_FULL_DID(CVMX_OCT_DID_PCI, CVMX_OCT_SUBDID_PCI_MEM1));
172 sc->sc_mem1.rm_descr = "Cavium Octeon PCI Memory";
175 sc->sc_io.rm_type = RMAN_ARRAY;
176 error = rman_init(&sc->sc_io);
180 error = rman_manage_region(&sc->sc_io, CVMX_OCT_PCI_IO_BASE,
181 CVMX_OCT_PCI_IO_BASE + CVMX_OCT_PCI_IO_SIZE);
185 sc->sc_mem1.rm_type = RMAN_ARRAY;
186 error = rman_init(&sc->sc_mem1);
190 error = rman_manage_region(&sc->sc_mem1, CVMX_OCT_PCI_MEM1_BASE,
191 CVMX_OCT_PCI_MEM1_BASE + CVMX_OCT_PCI_MEM1_SIZE);
196 * Next offsets for resource allocation in octopci_init_bar.
199 sc->sc_mem1_next = 0;
204 octopci_write_config(dev, sc->sc_bus, 0, 0, PCIR_SUBBUS_1, 0xff, 1);
205 subbus = octopci_init_bus(dev, sc->sc_bus);
206 octopci_write_config(dev, sc->sc_bus, 0, 0, PCIR_SUBBUS_1, subbus, 1);
208 device_add_child(dev, "pci", -1);
210 return (bus_generic_attach(dev));
214 octopci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
216 struct octopci_softc *sc;
218 sc = device_get_softc(dev);
221 case PCIB_IVAR_DOMAIN:
222 *result = sc->sc_domain;
225 *result = sc->sc_bus;
232 static struct resource *
233 octopci_alloc_resource(device_t bus, device_t child, int type, int *rid,
234 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
236 struct octopci_softc *sc;
237 struct resource *res;
241 sc = device_get_softc(bus);
245 res = bus_generic_alloc_resource(bus, child, type, rid, start,
260 res = rman_reserve_resource(rm, start, end, count, flags, child);
264 rman_set_rid(res, *rid);
265 rman_set_bustag(res, octopci_bus_space);
269 rman_set_bushandle(res, sc->sc_mem1_base + rman_get_start(res));
272 rman_set_bushandle(res, sc->sc_io_base + rman_get_start(res));
274 rman_set_virtual(res, (void *)rman_get_bushandle(res));
278 * We can't access ports via a 32-bit pointer.
280 rman_set_virtual(res, NULL);
285 if ((flags & RF_ACTIVE) != 0) {
286 error = bus_activate_resource(child, type, *rid, res);
288 rman_release_resource(res);
297 octopci_activate_resource(device_t bus, device_t child, int type, int rid,
298 struct resource *res)
300 bus_space_handle_t bh;
305 error = bus_generic_activate_resource(bus, child, type, rid,
312 error = bus_space_map(rman_get_bustag(res),
313 rman_get_bushandle(res), rman_get_size(res), 0, &bh);
316 rman_set_bushandle(res, bh);
322 error = rman_activate_resource(res);
329 octopci_maxslots(device_t dev)
331 return (PCI_SLOTMAX);
335 octopci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
338 struct octopci_softc *sc;
342 sc = device_get_softc(dev);
344 if (octeon_has_feature(OCTEON_FEATURE_PCIE)) {
345 if (bus == 0 && slot == 0 && func == 0)
346 return ((uint32_t)-1);
350 return (cvmx_pcie_config_read32(sc->sc_domain, bus, slot, func, reg));
352 return (cvmx_pcie_config_read16(sc->sc_domain, bus, slot, func, reg));
354 return (cvmx_pcie_config_read8(sc->sc_domain, bus, slot, func, reg));
356 return ((uint32_t)-1);
360 addr = octopci_cs_addr(bus, slot, func, reg);
364 data = le32toh(cvmx_read64_uint32(addr));
367 data = le16toh(cvmx_read64_uint16(addr));
370 data = cvmx_read64_uint8(addr);
373 return ((uint32_t)-1);
378 octopci_write_config(device_t dev, u_int bus, u_int slot, u_int func,
379 u_int reg, uint32_t data, int bytes)
381 struct octopci_softc *sc;
384 sc = device_get_softc(dev);
386 if (octeon_has_feature(OCTEON_FEATURE_PCIE)) {
389 cvmx_pcie_config_write32(sc->sc_domain, bus, slot, func, reg, data);
392 cvmx_pcie_config_write16(sc->sc_domain, bus, slot, func, reg, data);
395 cvmx_pcie_config_write8(sc->sc_domain, bus, slot, func, reg, data);
402 addr = octopci_cs_addr(bus, slot, func, reg);
406 cvmx_write64_uint32(addr, htole32(data));
409 cvmx_write64_uint16(addr, htole16(data));
412 cvmx_write64_uint8(addr, data);
420 octopci_route_interrupt(device_t dev, device_t child, int pin)
422 struct octopci_softc *sc;
423 unsigned bus, slot, func;
426 sc = device_get_softc(dev);
428 if (octeon_has_feature(OCTEON_FEATURE_PCIE))
429 return (OCTEON_IRQ_PCI_INT0 + pin - 1);
431 bus = pci_get_bus(child);
432 slot = pci_get_slot(child);
433 func = pci_get_function(child);
436 * Board types we have to know at compile-time.
438 #if defined(OCTEON_BOARD_CAPK_0100ND)
439 if (bus == 0 && slot == 12 && func == 0)
440 return (OCTEON_IRQ_PCI_INT2);
444 * For board types we can determine at runtime.
446 switch (cvmx_sysinfo_get()->board_type) {
447 #if defined(OCTEON_VENDOR_LANNER)
448 case CVMX_BOARD_TYPE_CUST_LANNER_MR955:
449 return (OCTEON_IRQ_PCI_INT0 + pin - 1);
450 case CVMX_BOARD_TYPE_CUST_LANNER_MR320:
452 if (slot == 3 || slot == 9)
456 return (OCTEON_IRQ_PCI_INT0 + (irq & 3));
464 irq = slot + pin - 3;
466 return (OCTEON_IRQ_PCI_INT0 + (irq & 3));
470 octopci_init_bar(device_t dev, unsigned b, unsigned s, unsigned f, unsigned barnum, uint8_t *commandp)
472 struct octopci_softc *sc;
477 sc = device_get_softc(dev);
479 octopci_write_config(dev, b, s, f, PCIR_BAR(barnum), 0xffffffff, 4);
480 bar = octopci_read_config(dev, b, s, f, PCIR_BAR(barnum), 4);
483 /* Bar not implemented; got to next bar. */
487 if (PCI_BAR_IO(bar)) {
488 size = ~(bar & PCIM_BAR_IO_BASE) + 1;
490 sc->sc_io_next = roundup2(sc->sc_io_next, size);
491 if (sc->sc_io_next + size > CVMX_OCT_PCI_IO_SIZE) {
492 device_printf(dev, "%02x.%02x:%02x: no ports for BAR%u.\n",
496 octopci_write_config(dev, b, s, f, PCIR_BAR(barnum),
497 CVMX_OCT_PCI_IO_BASE + sc->sc_io_next, 4);
498 sc->sc_io_next += size;
503 *commandp |= PCIM_CMD_PORTEN;
507 if (PCIR_BAR(barnum) == PCIR_BIOS) {
509 * ROM BAR is always 32-bit.
513 switch (bar & PCIM_BAR_MEM_TYPE) {
514 case PCIM_BAR_MEM_64:
517 * High 32 bits are all zeroes for now.
519 octopci_write_config(dev, b, s, f, PCIR_BAR(barnum + 1), 0, 4);
528 size = ~(bar & (uint32_t)PCIM_BAR_MEM_BASE) + 1;
530 sc->sc_mem1_next = roundup2(sc->sc_mem1_next, size);
531 if (sc->sc_mem1_next + size > CVMX_OCT_PCI_MEM1_SIZE) {
532 device_printf(dev, "%02x.%02x:%02x: no memory for BAR%u.\n",
534 return (barnum + barsize);
536 octopci_write_config(dev, b, s, f, PCIR_BAR(barnum),
537 CVMX_OCT_PCI_MEM1_BASE + sc->sc_mem1_next, 4);
538 sc->sc_mem1_next += size;
541 * Enable memory access.
543 *commandp |= PCIM_CMD_MEMEN;
545 return (barnum + barsize);
550 octopci_init_device(device_t dev, unsigned b, unsigned s, unsigned f, unsigned secbus)
552 unsigned barnum, bars;
554 uint8_t class, subclass;
558 /* Read header type (again.) */
559 hdrtype = octopci_read_config(dev, b, s, f, PCIR_HDRTYPE, 1);
562 * Disable memory and I/O while programming BARs.
564 command = octopci_read_config(dev, b, s, f, PCIR_COMMAND, 1);
565 command &= ~(PCIM_CMD_MEMEN | PCIM_CMD_PORTEN);
566 octopci_write_config(dev, b, s, f, PCIR_COMMAND, command, 1);
571 switch (hdrtype & PCIM_HDRTYPE) {
572 case PCIM_HDRTYPE_NORMAL:
575 case PCIM_HDRTYPE_BRIDGE:
578 case PCIM_HDRTYPE_CARDBUS:
582 device_printf(dev, "%02x.%02x:%02x: invalid header type %#x\n",
588 while (barnum < bars)
589 barnum = octopci_init_bar(dev, b, s, f, barnum, &command);
591 /* Enable bus mastering. */
592 command |= PCIM_CMD_BUSMASTEREN;
594 /* Enable whatever facilities the BARs require. */
595 octopci_write_config(dev, b, s, f, PCIR_COMMAND, command, 1);
600 * Set cache line size. On Octeon it should be 128 bytes,
601 * but according to Linux some Intel bridges have trouble
602 * with values over 64 bytes, so use 64 bytes.
604 octopci_write_config(dev, b, s, f, PCIR_CACHELNSZ, 16, 1);
606 /* Set latency timer. */
607 octopci_write_config(dev, b, s, f, PCIR_LATTIMER, 48, 1);
609 /* Board-specific or device-specific fixups and workarounds. */
610 switch (cvmx_sysinfo_get()->board_type) {
611 #if defined(OCTEON_VENDOR_LANNER)
612 case CVMX_BOARD_TYPE_CUST_LANNER_MR955:
613 if (b == 1 && s == 7 && f == 0) {
614 bus_addr_t busaddr, unitbusaddr;
622 bar = octopci_read_config(dev, b, s, f,
624 busaddr = CVMX_ADDR_DID(CVMX_FULL_DID(CVMX_OCT_DID_PCI,
625 CVMX_OCT_SUBDID_PCI_MEM1));
626 busaddr += (bar & (uint32_t)PCIM_BAR_MEM_BASE);
627 for (unit = 0; unit < 4; unit++) {
628 unitbusaddr = busaddr + 0x430 + (unit << 8);
629 tmp = le32toh(cvmx_read64_uint32(unitbusaddr));
632 cvmx_write64_uint32(unitbusaddr, htole32(tmp));
641 /* Configure PCI-PCI bridges. */
642 class = octopci_read_config(dev, b, s, f, PCIR_CLASS, 1);
643 if (class != PCIC_BRIDGE)
646 subclass = octopci_read_config(dev, b, s, f, PCIR_SUBCLASS, 1);
647 if (subclass != PCIS_BRIDGE_PCI)
650 /* Enable memory and I/O access. */
651 command |= PCIM_CMD_MEMEN | PCIM_CMD_PORTEN;
652 octopci_write_config(dev, b, s, f, PCIR_COMMAND, command, 1);
654 /* Enable errors and parity checking. Do a bus reset. */
655 brctl = octopci_read_config(dev, b, s, f, PCIR_BRIDGECTL_1, 1);
656 brctl |= PCIB_BCR_PERR_ENABLE | PCIB_BCR_SERR_ENABLE;
658 /* Perform a secondary bus reset. */
659 brctl |= PCIB_BCR_SECBUS_RESET;
660 octopci_write_config(dev, b, s, f, PCIR_BRIDGECTL_1, brctl, 1);
662 brctl &= ~PCIB_BCR_SECBUS_RESET;
663 octopci_write_config(dev, b, s, f, PCIR_BRIDGECTL_1, brctl, 1);
667 /* Program memory and I/O ranges. */
668 octopci_write_config(dev, b, s, f, PCIR_MEMBASE_1,
669 CVMX_OCT_PCI_MEM1_BASE >> 16, 2);
670 octopci_write_config(dev, b, s, f, PCIR_MEMLIMIT_1,
671 (CVMX_OCT_PCI_MEM1_BASE + CVMX_OCT_PCI_MEM1_SIZE - 1) >> 16, 2);
673 octopci_write_config(dev, b, s, f, PCIR_IOBASEL_1,
674 CVMX_OCT_PCI_IO_BASE >> 8, 1);
675 octopci_write_config(dev, b, s, f, PCIR_IOBASEH_1,
676 CVMX_OCT_PCI_IO_BASE >> 16, 2);
678 octopci_write_config(dev, b, s, f, PCIR_IOLIMITL_1,
679 (CVMX_OCT_PCI_IO_BASE + CVMX_OCT_PCI_IO_SIZE - 1) >> 8, 1);
680 octopci_write_config(dev, b, s, f, PCIR_IOLIMITH_1,
681 (CVMX_OCT_PCI_IO_BASE + CVMX_OCT_PCI_IO_SIZE - 1) >> 16, 2);
683 /* Program prefetchable memory decoder. */
686 /* Probe secondary/subordinate buses. */
687 octopci_write_config(dev, b, s, f, PCIR_PRIBUS_1, b, 1);
688 octopci_write_config(dev, b, s, f, PCIR_SECBUS_1, secbus, 1);
689 octopci_write_config(dev, b, s, f, PCIR_SUBBUS_1, 0xff, 1);
691 /* Perform a secondary bus reset. */
692 brctl |= PCIB_BCR_SECBUS_RESET;
693 octopci_write_config(dev, b, s, f, PCIR_BRIDGECTL_1, brctl, 1);
695 brctl &= ~PCIB_BCR_SECBUS_RESET;
696 octopci_write_config(dev, b, s, f, PCIR_BRIDGECTL_1, brctl, 1);
698 /* Give the bus time to settle now before reading configspace. */
701 secbus = octopci_init_bus(dev, secbus);
703 octopci_write_config(dev, b, s, f, PCIR_SUBBUS_1, secbus, 1);
709 octopci_init_bus(device_t dev, unsigned b)
717 for (s = 0; s <= PCI_SLOTMAX; s++) {
718 for (f = 0; f <= PCI_FUNCMAX; f++) {
719 hdrtype = octopci_read_config(dev, b, s, f, PCIR_HDRTYPE, 1);
721 if (hdrtype == 0xff) {
723 break; /* Next slot. */
724 continue; /* Next function. */
727 secbus = octopci_init_device(dev, b, s, f, secbus);
729 if (f == 0 && (hdrtype & PCIM_MFDEV) == 0)
730 break; /* Next slot. */
738 octopci_cs_addr(unsigned bus, unsigned slot, unsigned func, unsigned reg)
740 octeon_pci_config_space_address_t pci_addr;
743 pci_addr.s.upper = 2;
746 pci_addr.s.subdid = CVMX_OCT_SUBDID_PCI_CFG;
747 pci_addr.s.endian_swap = 1;
748 pci_addr.s.bus = bus;
749 pci_addr.s.dev = slot;
750 pci_addr.s.func = func;
751 pci_addr.s.reg = reg;
753 return (pci_addr.u64);
757 octopci_init_pci(device_t dev)
759 cvmx_npi_mem_access_subid_t npi_mem_access_subid;
760 cvmx_npi_pci_int_arb_cfg_t npi_pci_int_arb_cfg;
761 cvmx_npi_ctl_status_t npi_ctl_status;
762 cvmx_pci_ctl_status_2_t pci_ctl_status_2;
763 cvmx_pci_cfg56_t pci_cfg56;
764 cvmx_pci_cfg22_t pci_cfg22;
765 cvmx_pci_cfg16_t pci_cfg16;
766 cvmx_pci_cfg19_t pci_cfg19;
767 cvmx_pci_cfg01_t pci_cfg01;
773 cvmx_write_csr(CVMX_CIU_SOFT_PRST, 0x1);
774 cvmx_read_csr(CVMX_CIU_SOFT_PRST);
778 npi_ctl_status.u64 = 0;
779 npi_ctl_status.s.max_word = 1;
780 npi_ctl_status.s.timer = 1;
781 cvmx_write_csr(CVMX_NPI_CTL_STATUS, npi_ctl_status.u64);
786 switch (cvmx_sysinfo_get()->board_type) {
787 #if defined(OCTEON_VENDOR_LANNER)
788 case CVMX_BOARD_TYPE_CUST_LANNER_MR320:
789 case CVMX_BOARD_TYPE_CUST_LANNER_MR955:
791 cvmx_write_csr(CVMX_CIU_SOFT_PRST, 0x0);
796 cvmx_write_csr(CVMX_CIU_SOFT_PRST, 0x4);
799 cvmx_read_csr(CVMX_CIU_SOFT_PRST);
804 * Enable BARs and configure big BAR mode.
806 pci_ctl_status_2.u32 = 0;
807 pci_ctl_status_2.s.bb1_hole = 5; /* 256MB hole in BAR1 */
808 pci_ctl_status_2.s.bb1_siz = 1; /* BAR1 is 2GB */
809 pci_ctl_status_2.s.bb_ca = 1; /* Bypass cache for big BAR */
810 pci_ctl_status_2.s.bb_es = 1; /* Do big BAR byte-swapping */
811 pci_ctl_status_2.s.bb1 = 1; /* BAR1 is big */
812 pci_ctl_status_2.s.bb0 = 1; /* BAR0 is big */
813 pci_ctl_status_2.s.bar2pres = 1; /* BAR2 present */
814 pci_ctl_status_2.s.pmo_amod = 1; /* Round-robin priority */
815 pci_ctl_status_2.s.tsr_hwm = 1;
816 pci_ctl_status_2.s.bar2_enb = 1; /* Enable BAR2 */
817 pci_ctl_status_2.s.bar2_esx = 1; /* Do BAR2 byte-swapping */
818 pci_ctl_status_2.s.bar2_cax = 1; /* Bypass cache for BAR2 */
820 NPI_WRITE(CVMX_NPI_PCI_CTL_STATUS_2, pci_ctl_status_2.u32);
824 pci_ctl_status_2.u32 = NPI_READ(CVMX_NPI_PCI_CTL_STATUS_2);
826 device_printf(dev, "%u-bit PCI%s bus.\n",
827 pci_ctl_status_2.s.ap_64ad ? 64 : 32,
828 pci_ctl_status_2.s.ap_pcix ? "-X" : "");
831 * Set up transaction splitting, etc., parameters.
834 pci_cfg19.s.mrbcm = 1;
835 if (pci_ctl_status_2.s.ap_pcix) {
836 pci_cfg19.s.mdrrmc = 0;
837 pci_cfg19.s.tdomc = 4;
839 pci_cfg19.s.mdrrmc = 2;
840 pci_cfg19.s.tdomc = 1;
842 NPI_WRITE(CVMX_NPI_PCI_CFG19, pci_cfg19.u32);
843 NPI_READ(CVMX_NPI_PCI_CFG19);
846 * Set up PCI error handling and memory access.
849 pci_cfg01.s.fbbe = 1;
853 pci_cfg01.s.msae = 1;
854 if (pci_ctl_status_2.s.ap_pcix) {
859 NPI_WRITE(CVMX_NPI_PCI_CFG01, pci_cfg01.u32);
860 NPI_READ(CVMX_NPI_PCI_CFG01);
863 * Enable the Octeon bus arbiter.
865 npi_pci_int_arb_cfg.u64 = 0;
866 npi_pci_int_arb_cfg.s.en = 1;
867 cvmx_write_csr(CVMX_NPI_PCI_INT_ARB_CFG, npi_pci_int_arb_cfg.u64);
870 * Disable master latency timer.
873 pci_cfg16.s.mltd = 1;
874 NPI_WRITE(CVMX_NPI_PCI_CFG16, pci_cfg16.u32);
875 NPI_READ(CVMX_NPI_PCI_CFG16);
878 * Configure master arbiter.
881 pci_cfg22.s.flush = 1;
882 pci_cfg22.s.mrv = 255;
883 NPI_WRITE(CVMX_NPI_PCI_CFG22, pci_cfg22.u32);
884 NPI_READ(CVMX_NPI_PCI_CFG22);
887 * Set up PCI-X capabilities.
889 if (pci_ctl_status_2.s.ap_pcix) {
891 pci_cfg56.s.most = 3;
892 pci_cfg56.s.roe = 1; /* Enable relaxed ordering */
893 pci_cfg56.s.dpere = 1;
894 pci_cfg56.s.ncp = 0xe8;
895 pci_cfg56.s.pxcid = 7;
896 NPI_WRITE(CVMX_NPI_PCI_CFG56, pci_cfg56.u32);
897 NPI_READ(CVMX_NPI_PCI_CFG56);
900 NPI_WRITE(CVMX_NPI_PCI_READ_CMD_6, 0x22);
901 NPI_READ(CVMX_NPI_PCI_READ_CMD_6);
902 NPI_WRITE(CVMX_NPI_PCI_READ_CMD_C, 0x33);
903 NPI_READ(CVMX_NPI_PCI_READ_CMD_C);
904 NPI_WRITE(CVMX_NPI_PCI_READ_CMD_E, 0x33);
905 NPI_READ(CVMX_NPI_PCI_READ_CMD_E);
908 * Configure MEM1 sub-DID access.
910 npi_mem_access_subid.u64 = 0;
911 npi_mem_access_subid.s.esr = 1; /* Byte-swap on read */
912 npi_mem_access_subid.s.esw = 1; /* Byte-swap on write */
913 switch (cvmx_sysinfo_get()->board_type) {
914 #if defined(OCTEON_VENDOR_LANNER)
915 case CVMX_BOARD_TYPE_CUST_LANNER_MR955:
916 npi_mem_access_subid.s.shortl = 1;
922 cvmx_write_csr(CVMX_NPI_MEM_ACCESS_SUBID3, npi_mem_access_subid.u64);
925 * Configure BAR2. Linux says this has to come first.
927 NPI_WRITE(CVMX_NPI_PCI_CFG08, 0x00000000);
928 NPI_READ(CVMX_NPI_PCI_CFG08);
929 NPI_WRITE(CVMX_NPI_PCI_CFG09, 0x00000080);
930 NPI_READ(CVMX_NPI_PCI_CFG09);
933 * Disable BAR1 IndexX.
935 for (i = 0; i < 32; i++) {
936 NPI_WRITE(CVMX_NPI_PCI_BAR1_INDEXX(i), 0);
937 NPI_READ(CVMX_NPI_PCI_BAR1_INDEXX(i));
941 * Configure BAR0 and BAR1.
943 NPI_WRITE(CVMX_NPI_PCI_CFG04, 0x00000000);
944 NPI_READ(CVMX_NPI_PCI_CFG04);
945 NPI_WRITE(CVMX_NPI_PCI_CFG05, 0x00000000);
946 NPI_READ(CVMX_NPI_PCI_CFG05);
948 NPI_WRITE(CVMX_NPI_PCI_CFG06, 0x80000000);
949 NPI_READ(CVMX_NPI_PCI_CFG06);
950 NPI_WRITE(CVMX_NPI_PCI_CFG07, 0x00000000);
951 NPI_READ(CVMX_NPI_PCI_CFG07);
954 * Clear PCI interrupts.
956 cvmx_write_csr(CVMX_NPI_PCI_INT_SUM2, 0xffffffffffffffffull);
959 static device_method_t octopci_methods[] = {
960 /* Device interface */
961 DEVMETHOD(device_identify, octopci_identify),
962 DEVMETHOD(device_probe, octopci_probe),
963 DEVMETHOD(device_attach, octopci_attach),
966 DEVMETHOD(bus_read_ivar, octopci_read_ivar),
967 DEVMETHOD(bus_alloc_resource, octopci_alloc_resource),
968 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
969 DEVMETHOD(bus_activate_resource,octopci_activate_resource),
970 DEVMETHOD(bus_deactivate_resource,bus_generic_deactivate_resource),
971 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
972 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
974 DEVMETHOD(bus_add_child, bus_generic_add_child),
977 DEVMETHOD(pcib_maxslots, octopci_maxslots),
978 DEVMETHOD(pcib_read_config, octopci_read_config),
979 DEVMETHOD(pcib_write_config, octopci_write_config),
980 DEVMETHOD(pcib_route_interrupt, octopci_route_interrupt),
985 static driver_t octopci_driver = {
988 sizeof(struct octopci_softc),
990 static devclass_t octopci_devclass;
991 DRIVER_MODULE(octopci, ciu, octopci_driver, octopci_devclass, 0, 0);