2 * Copyright (c) 2003-2009 RMI Corporation
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.
13 * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
40 #include <sys/endian.h>
44 #include <vm/vm_param.h>
47 #include <sys/pciio.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/uart/uart.h>
51 #include <dev/uart/uart_bus.h>
52 #include <dev/uart/uart_cpu.h>
54 #include <machine/bus.h>
55 #include <machine/md_var.h>
56 #include <machine/intr_machdep.h>
57 #include <machine/cpuregs.h>
59 #include <mips/nlm/hal/haldefs.h>
60 #include <mips/nlm/interrupt.h>
61 #include <mips/nlm/hal/iomap.h>
62 #include <mips/nlm/hal/mips-extns.h>
63 #include <mips/nlm/hal/pic.h>
64 #include <mips/nlm/hal/pcibus.h>
65 #include <mips/nlm/hal/uart.h>
66 #include <mips/nlm/xlp.h>
70 struct xlp_pcib_softc {
71 bus_dma_tag_t sc_pci_dmat; /* PCI DMA tag pointer */
74 static devclass_t pcib_devclass;
75 static struct rman irq_rman, port_rman, mem_rman, emul_rman;
78 xlp_pci_init_resources(void)
81 irq_rman.rm_start = 0;
82 irq_rman.rm_end = 255;
83 irq_rman.rm_type = RMAN_ARRAY;
84 irq_rman.rm_descr = "PCI Mapped Interrupts";
85 if (rman_init(&irq_rman)
86 || rman_manage_region(&irq_rman, 0, 255))
87 panic("pci_init_resources irq_rman");
89 port_rman.rm_start = 0;
90 port_rman.rm_end = ~0ul;
91 port_rman.rm_type = RMAN_ARRAY;
92 port_rman.rm_descr = "I/O ports";
93 if (rman_init(&port_rman)
94 || rman_manage_region(&port_rman, 0x14000000UL, 0x15ffffffUL))
95 panic("pci_init_resources port_rman");
97 mem_rman.rm_start = 0;
98 mem_rman.rm_end = ~0ul;
99 mem_rman.rm_type = RMAN_ARRAY;
100 mem_rman.rm_descr = "I/O memory";
101 if (rman_init(&mem_rman)
102 || rman_manage_region(&mem_rman, 0xd0000000ULL, 0xdfffffffULL))
103 panic("pci_init_resources mem_rman");
105 emul_rman.rm_start = 0;
106 emul_rman.rm_end = ~0ul;
107 emul_rman.rm_type = RMAN_ARRAY;
108 emul_rman.rm_descr = "Emulated MEMIO";
109 if (rman_init(&emul_rman)
110 || rman_manage_region(&emul_rman, 0x18000000ULL, 0x18ffffffULL))
111 panic("pci_init_resources emul_rman");
116 xlp_pcib_probe(device_t dev)
119 device_set_desc(dev, "XLP PCI bus");
121 xlp_pci_init_resources();
126 xlp_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
130 case PCIB_IVAR_DOMAIN:
141 xlp_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t result)
144 case PCIB_IVAR_DOMAIN:
153 xlp_pcib_maxslots(device_t dev)
156 return (PCI_SLOTMAX);
160 xlp_pcib_read_config(device_t dev, u_int b, u_int s, u_int f,
161 u_int reg, int width)
165 int regindex = reg/sizeof(uint32_t);
167 cfgaddr = nlm_pcicfg_base(XLP_HDR_OFFSET(0, b, s, f));
168 if ((width == 2) && (reg & 1))
170 else if ((width == 4) && (reg & 3))
173 data = nlm_read_pci_reg(cfgaddr, regindex);
176 * Fix up read data in some SoC devices
177 * to emulate complete PCIe header
182 /* Fake intpin on config read for UART/I2C, USB, SD/Flash */
183 if (regindex == 0xf &&
184 (dev == 6 || dev == 2 || dev == 7))
185 data |= 0x1 << 8; /* Fake int pin */
189 return ((data >> ((reg & 3) << 3)) & 0xff);
191 return ((data >> ((reg & 3) << 3)) & 0xffff);
197 xlp_pcib_write_config(device_t dev, u_int b, u_int s, u_int f,
198 u_int reg, u_int32_t val, int width)
202 int regindex = reg / sizeof(uint32_t);
204 cfgaddr = nlm_pcicfg_base(XLP_HDR_OFFSET(0, b, s, f));
205 if ((width == 2) && (reg & 1))
207 else if ((width == 4) && (reg & 3))
211 data = nlm_read_pci_reg(cfgaddr, regindex);
212 data = (data & ~(0xff << ((reg & 3) << 3))) |
213 (val << ((reg & 3) << 3));
214 } else if (width == 2) {
215 data = nlm_read_pci_reg(cfgaddr, regindex);
216 data = (data & ~(0xffff << ((reg & 3) << 3))) |
217 (val << ((reg & 3) << 3));
222 nlm_write_pci_reg(cfgaddr, regindex, data);
228 xlp_pcib_attach(device_t dev)
230 struct xlp_pcib_softc *sc;
231 sc = device_get_softc(dev);
233 device_add_child(dev, "pci", 0);
234 bus_generic_attach(dev);
240 xlp_pcib_identify(driver_t * driver, device_t parent)
243 BUS_ADD_CHILD(parent, 0, "pcib", 0);
247 * XLS PCIe can have upto 4 links, and each link has its on IRQ
248 * Find the link on which the device is on
251 xlp_pcie_link(device_t pcib, device_t dev)
253 device_t parent, tmp;
255 /* find the lane on which the slot is connected to */
256 printf("xlp_pcie_link : bus %s dev %s\n", device_get_nameunit(pcib),
257 device_get_nameunit(dev));
260 parent = device_get_parent(tmp);
261 if (parent == NULL || parent == pcib) {
262 device_printf(dev, "Cannot find parent bus\n");
265 if (strcmp(device_get_nameunit(parent), "pci0") == 0)
269 return (pci_get_function(tmp));
273 * Find the IRQ for the link, each link has a different interrupt
277 xlp_pcie_link_irt(int link)
280 if( (link < 0) || (link > 3))
283 return PIC_IRT_PCIE_LINK_INDEX(link);
287 xlp_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
292 * Each link has 32 MSIs that can be allocated, but for now
293 * we only support one device per link.
294 * msi_alloc() equivalent is needed when we start supporting
295 * bridges on the PCIe link.
297 link = xlp_pcie_link(pcib, dev);
302 * encode the irq so that we know it is a MSI interrupt when we
305 for (i = 0; i < count; i++)
306 irqs[i] = 64 + link * 32 + i;
312 xlp_release_msi(device_t pcib, device_t dev, int count, int *irqs)
314 device_printf(dev, "%s: msi release %d\n", device_get_nameunit(pcib),
320 xlp_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
327 *addr = MIPS_MSI_ADDR(0);
329 irt = xlp_pcie_link_irt(msi/32);
331 *data = MIPS_MSI_DATA(xlp_irt_to_irq(irt));
334 device_printf(dev, "%s: map_msi for irq %d - ignored",
335 device_get_nameunit(pcib), irq);
341 bridge_pcie_ack(int irq)
347 reg = PCIE_MSI_STATUS;
351 base = nlm_pcicfg_base(XLP_IO_PCIE0_OFFSET(node));
354 base = nlm_pcicfg_base(XLP_IO_PCIE1_OFFSET(node));
357 base = nlm_pcicfg_base(XLP_IO_PCIE2_OFFSET(node));
360 base = nlm_pcicfg_base(XLP_IO_PCIE3_OFFSET(node));
366 nlm_write_pci_reg(base, reg, 0xFFFFFFFF);
372 mips_platform_pci_setup_intr(device_t dev, device_t child,
373 struct resource *irq, int flags, driver_filter_t *filt,
374 driver_intr_t *intr, void *arg, void **cookiep)
378 int node,base,val,link;
381 error = rman_activate_resource(irq);
384 if (rman_get_start(irq) != rman_get_end(irq)) {
385 device_printf(dev, "Interrupt allocation %lu != %lu\n",
386 rman_get_start(irq), rman_get_end(irq));
389 xlpirq = rman_get_start(irq);
390 device_printf(dev, "setup intr %d\n", xlpirq);
392 if (strcmp(device_get_name(dev), "pcib") != 0) {
393 device_printf(dev, "ret 0 on dev\n");
398 * temporary hack for MSI, we support just one device per
399 * link, and assign the link interrupt to the device interrupt
403 if (xlpirq % 32 != 0)
407 link = (xlpirq / 32);
408 base = nlm_pcicfg_base(XLP_IO_PCIE_OFFSET(node,link));
410 /* MSI Interrupt Vector enable at bridge's configuration */
411 nlm_write_pci_reg(base, PCIE_MSI_EN, PCIE_MSI_VECTOR_INT_EN);
413 val = nlm_read_pci_reg(base, PCIE_INT_EN0);
414 /* MSI Interrupt enable at bridge's configuration */
415 nlm_write_pci_reg(base, PCIE_INT_EN0,
416 (val | PCIE_MSI_INT_EN));
418 /* legacy interrupt disable at bridge */
419 val = nlm_read_pci_reg(base, PCIE_BRIDGE_CMD);
420 nlm_write_pci_reg(base, PCIE_BRIDGE_CMD,
421 (val | PCIM_CMD_INTxDIS));
423 /* MSI address update at bridge */
424 val = nlm_read_pci_reg(base, PCIE_BRIDGE_MSI_ADDRL);
425 nlm_write_pci_reg(base, PCIE_BRIDGE_MSI_ADDRL,
426 (val | MSI_MIPS_ADDR_BASE));
428 val = nlm_read_pci_reg(base, PCIE_BRIDGE_MSI_CAP);
429 /* MSI capability enable at bridge */
430 nlm_write_pci_reg(base, PCIE_BRIDGE_MSI_CAP,
432 (PCIM_MSICTRL_MSI_ENABLE << 16) |
433 (PCIM_MSICTRL_MMC_32 << 16)));
435 xlpirq = xlp_pcie_link_irt(xlpirq / 32);
438 xlpirq = xlp_irt_to_irq(xlpirq);
440 /* Set all irqs to CPU 0 for now */
441 nlm_pic_write_irt_direct(xlp_pic_base, xlp_irq_to_irt(xlpirq), 1, 0,
442 PIC_LOCAL_SCHEDULING, xlpirq, 0);
444 if (xlpirq >= PIC_PCIE_0_IRQ &&
445 xlpirq <= PIC_PCIE_3_IRQ)
446 extra_ack = bridge_pcie_ack;
447 xlp_establish_intr(device_get_name(child), filt,
448 intr, arg, xlpirq, flags, cookiep, extra_ack);
454 mips_platform_pci_teardown_intr(device_t dev, device_t child,
455 struct resource *irq, void *cookie)
457 if (strcmp(device_get_name(child), "pci") == 0) {
458 /* if needed reprogram the pic to clear pcix related entry */
459 device_printf(dev, "teardown intr\n");
461 return (bus_generic_teardown_intr(dev, child, irq, cookie));
465 assign_soc_resource(device_t child, int type, u_long *startp, u_long *endp,
466 u_long *countp, struct rman **rm, bus_space_tag_t *bst, vm_offset_t *va)
468 int devid = pci_get_device(child);
469 int inst = pci_get_function(child);
470 int node = pci_get_slot(child) / 8;
476 case PCI_DEVICE_ID_NLM_UART:
479 *startp = *endp = PIC_UART_0_IRQ + inst;
483 *va = nlm_get_uart_regbase(node, inst);
484 *startp = MIPS_KSEG1_TO_PHYS(va);
487 *bst = uart_bus_space_mem;
492 case PCI_DEVICE_ID_NLM_EHCI:
493 if (type == SYS_RES_IRQ) {
495 *startp = *endp = PIC_EHCI_0_IRQ;
497 *startp = *endp = PIC_EHCI_1_IRQ;
499 device_printf(child, "bad instance %d\n", inst);
506 /* default to rmi_bus_space for SoC resources */
507 if (type == SYS_RES_MEMORY && *bst == 0)
508 *bst = rmi_bus_space;
511 static struct resource *
512 xlp_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
513 u_long start, u_long end, u_long count, u_int flags)
515 struct rman *rm = NULL;
518 int needactivate = flags & RF_ACTIVE;
519 bus_space_tag_t bst = 0;
522 * For SoC PCI devices, we have to assign resources correctly
523 * since the IRQ and MEM resources depend on the block.
524 * If the address is not from BAR0, then we use emul_rman
526 if (pci_get_bus(child) == 0 &&
527 pci_get_vendor(child) == PCI_VENDOR_NETLOGIC)
528 assign_soc_resource(child, type, &start, &end,
529 &count, &rm, &bst, &va);
549 rv = rman_reserve_resource(rm, start, end, count, flags, child);
553 rman_set_rid(rv, *rid);
555 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
557 va = (vm_offset_t)pmap_mapdev(start, count);
559 bst = rmi_pci_bus_space;
561 rman_set_bushandle(rv, va);
562 rman_set_virtual(rv, (void *)va);
563 rman_set_bustag(rv, bst);
567 if (bus_activate_resource(child, type, *rid, rv)) {
568 rman_release_resource(rv);
577 xlp_pci_release_resource(device_t bus, device_t child, int type, int rid,
581 return (rman_release_resource(r));
585 xlp_pci_get_dma_tag(device_t bus, device_t child)
587 struct xlp_pcib_softc *sc;
589 sc = device_get_softc(bus);
590 return (sc->sc_pci_dmat);
594 xlp_pci_activate_resource(device_t bus, device_t child, int type, int rid,
598 return (rman_activate_resource(r));
602 xlp_pci_deactivate_resource(device_t bus, device_t child, int type, int rid,
606 return (rman_deactivate_resource(r));
610 mips_pci_route_interrupt(device_t bus, device_t dev, int pin)
615 * Validate requested pin number.
617 device_printf(bus, "route %s %d", device_get_nameunit(dev), pin);
618 if ((pin < 1) || (pin > 4))
621 link = xlp_pcie_link(bus, dev);
622 irt = xlp_pcie_link_irt(link);
624 return (xlp_irt_to_irq(irt));
629 static device_method_t xlp_pcib_methods[] = {
630 /* Device interface */
631 DEVMETHOD(device_identify, xlp_pcib_identify),
632 DEVMETHOD(device_probe, xlp_pcib_probe),
633 DEVMETHOD(device_attach, xlp_pcib_attach),
636 DEVMETHOD(bus_read_ivar, xlp_pcib_read_ivar),
637 DEVMETHOD(bus_write_ivar, xlp_pcib_write_ivar),
638 DEVMETHOD(bus_alloc_resource, xlp_pci_alloc_resource),
639 DEVMETHOD(bus_release_resource, xlp_pci_release_resource),
640 DEVMETHOD(bus_get_dma_tag, xlp_pci_get_dma_tag),
641 DEVMETHOD(bus_activate_resource, xlp_pci_activate_resource),
642 DEVMETHOD(bus_deactivate_resource, xlp_pci_deactivate_resource),
643 DEVMETHOD(bus_setup_intr, mips_platform_pci_setup_intr),
644 DEVMETHOD(bus_teardown_intr, mips_platform_pci_teardown_intr),
647 DEVMETHOD(pcib_maxslots, xlp_pcib_maxslots),
648 DEVMETHOD(pcib_read_config, xlp_pcib_read_config),
649 DEVMETHOD(pcib_write_config, xlp_pcib_write_config),
650 DEVMETHOD(pcib_route_interrupt, mips_pci_route_interrupt),
652 DEVMETHOD(pcib_alloc_msi, xlp_alloc_msi),
653 DEVMETHOD(pcib_release_msi, xlp_release_msi),
654 DEVMETHOD(pcib_map_msi, xlp_map_msi),
659 static driver_t xlp_pcib_driver = {
662 sizeof(struct xlp_pcib_softc),
665 DRIVER_MODULE(pcib, nexus, xlp_pcib_driver, pcib_devclass, 0, 0);