2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * Copyright (c) 2011, Luiz Otavio O Souza.
4 * Copyright (c) 2015, Adrian Chadd <adrian@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
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 "opt_ar71xx.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/interrupt.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
47 #include <vm/vm_extern.h>
49 #include <machine/bus.h>
50 #include <machine/cpu.h>
51 #include <machine/intr_machdep.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcib_private.h>
59 #include <mips/atheros/ar71xxreg.h> /* XXX aim to eliminate this! */
60 #include <mips/atheros/qca955xreg.h>
61 #include <mips/atheros/ar71xx_setup.h>
62 #include <mips/atheros/ar71xx_pci_bus_space.h>
64 #include <mips/atheros/ar71xx_cpudef.h>
66 #undef AR724X_PCI_DEBUG
67 //#define AR724X_PCI_DEBUG
68 #ifdef AR724X_PCI_DEBUG
69 #define dprintf printf
71 #define dprintf(x, arg...)
75 * This is a PCI controller for the QCA955x and later SoCs.
76 * It needs to be aware of >1 PCIe host endpoints.
78 * XXX TODO; it may be nice to merge this with ar724x_pci.c;
79 * they're very similar.
81 struct ar71xx_pci_irq {
82 struct ar71xx_pci_softc *sc;
86 struct ar71xx_pci_softc {
90 struct rman sc_mem_rman;
91 struct rman sc_irq_rman;
93 uint32_t sc_pci_reg_base; /* XXX until bus stuff is done */
94 uint32_t sc_pci_crp_base; /* XXX until bus stuff is done */
95 uint32_t sc_pci_ctrl_base; /* XXX until bus stuff is done */
96 uint32_t sc_pci_mem_base; /* XXX until bus stuff is done */
97 uint32_t sc_pci_membase_limit;
99 struct intr_event *sc_eventstab[AR71XX_PCI_NIRQS];
100 mips_intrcnt_t sc_intr_counter[AR71XX_PCI_NIRQS];
101 struct ar71xx_pci_irq sc_pci_irq[AR71XX_PCI_NIRQS];
102 struct resource *sc_irq;
106 static int qca955x_pci_setup_intr(device_t, device_t, struct resource *, int,
107 driver_filter_t *, driver_intr_t *, void *, void **);
108 static int qca955x_pci_teardown_intr(device_t, device_t, struct resource *,
110 static int qca955x_pci_intr(void *);
113 qca955x_pci_write(uint32_t reg, uint32_t offset, uint32_t data, int bytes)
115 uint32_t val, mask, shift;
117 /* Register access is 32-bit aligned */
118 shift = (offset & 3) * 8;
120 mask = (1 << (bytes * 8)) - 1;
124 val = ATH_READ_REG(reg + (offset & ~3));
125 val &= ~(mask << shift);
126 val |= ((data & mask) << shift);
127 ATH_WRITE_REG(reg + (offset & ~3), val);
129 dprintf("%s: %#x/%#x addr=%#x, data=%#x(%#x), bytes=%d\n", __func__,
130 reg, reg + (offset & ~3), offset, data, val, bytes);
134 qca955x_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func,
135 u_int reg, int bytes)
137 struct ar71xx_pci_softc *sc = device_get_softc(dev);
138 uint32_t data, shift, mask;
140 /* Register access is 32-bit aligned */
141 shift = (reg & 3) * 8;
143 /* Create a mask based on the width, post-shift */
151 dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot,
154 if ((bus == 0) && (slot == 0) && (func == 0))
155 data = ATH_READ_REG(sc->sc_pci_reg_base + (reg & ~3));
159 /* Get request bytes from 32-bit word */
160 data = (data >> shift) & mask;
162 dprintf("%s: read 0x%x\n", __func__, data);
168 qca955x_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func,
169 u_int reg, uint32_t data, int bytes)
171 struct ar71xx_pci_softc *sc = device_get_softc(dev);
173 dprintf("%s: tag (%x, %x, %x) reg %d(%d): %x\n", __func__, bus, slot,
174 func, reg, bytes, data);
176 if ((bus != 0) || (slot != 0) || (func != 0))
179 qca955x_pci_write(sc->sc_pci_reg_base, reg, data, bytes);
183 qca955x_pci_mask_irq(void *source)
186 struct ar71xx_pci_irq *pirq = source;
187 struct ar71xx_pci_softc *sc = pirq->sc;
189 /* XXX - Only one interrupt ? Only one device ? */
190 if (pirq->irq != AR71XX_PCI_IRQ_START)
193 /* Update the interrupt mask reg */
194 reg = ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_MASK);
195 ATH_WRITE_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_MASK,
196 reg & ~QCA955X_PCI_INTR_DEV0);
198 /* Clear any pending interrupt */
199 reg = ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_STATUS);
200 ATH_WRITE_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_STATUS,
201 reg | QCA955X_PCI_INTR_DEV0);
205 qca955x_pci_unmask_irq(void *source)
208 struct ar71xx_pci_irq *pirq = source;
209 struct ar71xx_pci_softc *sc = pirq->sc;
211 if (pirq->irq != AR71XX_PCI_IRQ_START)
214 /* Update the interrupt mask reg */
215 reg = ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_MASK);
216 ATH_WRITE_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_MASK,
217 reg | QCA955X_PCI_INTR_DEV0);
221 qca955x_pci_setup(device_t dev)
223 struct ar71xx_pci_softc *sc = device_get_softc(dev);
226 /* setup COMMAND register */
227 reg = PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN | PCIM_CMD_SERRESPEN |
228 PCIM_CMD_BACKTOBACK | PCIM_CMD_PERRESPEN | PCIM_CMD_MWRICEN;
230 qca955x_pci_write(sc->sc_pci_crp_base, PCIR_COMMAND, reg, 2);
232 /* These are the memory/prefetch base/limit parameters */
233 qca955x_pci_write(sc->sc_pci_crp_base, 0x20, sc->sc_pci_membase_limit, 4);
234 qca955x_pci_write(sc->sc_pci_crp_base, 0x24, sc->sc_pci_membase_limit, 4);
236 reg = ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_RESET);
239 ATH_WRITE_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_RESET, 0);
240 ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_RESET);
242 ATH_WRITE_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_RESET, 4);
243 ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_RESET);
247 ATH_WRITE_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_APP, 0x1ffc1);
249 (void) ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_APP);
253 reg = ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_RESET);
254 if ((reg & QCA955X_PCI_RESET_LINK_UP) == 0) {
255 device_printf(dev, "no PCIe controller found\n");
263 qca955x_pci_probe(device_t dev)
266 return (BUS_PROBE_NOWILDCARD);
270 qca955x_pci_attach(device_t dev)
272 struct ar71xx_pci_softc *sc = device_get_softc(dev);
273 int unit = device_get_unit(dev);
276 /* Dirty; maybe these could all just be hints */
278 sc->sc_pci_reg_base = QCA955X_PCI_CFG_BASE0;
279 sc->sc_pci_crp_base = QCA955X_PCI_CRP_BASE0;
280 sc->sc_pci_ctrl_base = QCA955X_PCI_CTRL_BASE0;
281 sc->sc_pci_mem_base = QCA955X_PCI_MEM_BASE0;
283 sc->sc_pci_membase_limit = 0x11f01000;
284 } else if (unit == 1) {
285 sc->sc_pci_reg_base = QCA955X_PCI_CFG_BASE1;
286 sc->sc_pci_crp_base = QCA955X_PCI_CRP_BASE1;
287 sc->sc_pci_ctrl_base = QCA955X_PCI_CTRL_BASE1;
288 sc->sc_pci_mem_base = QCA955X_PCI_MEM_BASE1;
290 sc->sc_pci_membase_limit = 0x12f01200;
292 device_printf(dev, "%s: invalid unit (%d)\n", __func__, unit);
296 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
297 sc->sc_mem_rman.rm_descr = "qca955x PCI memory window";
298 if (rman_init(&sc->sc_mem_rman) != 0 ||
299 rman_manage_region(&sc->sc_mem_rman,
301 sc->sc_pci_mem_base + QCA955X_PCI_MEM_SIZE - 1) != 0) {
302 panic("qca955x_pci_attach: failed to set up I/O rman");
305 sc->sc_irq_rman.rm_type = RMAN_ARRAY;
306 sc->sc_irq_rman.rm_descr = "qca955x PCI IRQs";
307 if (rman_init(&sc->sc_irq_rman) != 0 ||
308 rman_manage_region(&sc->sc_irq_rman, AR71XX_PCI_IRQ_START,
309 AR71XX_PCI_IRQ_END) != 0)
310 panic("qca955x_pci_attach: failed to set up IRQ rman");
312 /* Disable interrupts */
313 ATH_WRITE_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_STATUS, 0);
314 ATH_WRITE_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_MASK, 0);
316 /* Hook up our interrupt handler. */
317 if ((sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
318 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
319 device_printf(dev, "unable to allocate IRQ resource\n");
323 if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC,
324 qca955x_pci_intr, NULL, sc, &sc->sc_ih))) {
326 "WARNING: unable to register interrupt handler\n");
330 /* Reset PCIe core and PCIe PHY */
331 ar71xx_device_stop(QCA955X_RESET_PCIE);
332 ar71xx_device_stop(QCA955X_RESET_PCIE_PHY);
334 ar71xx_device_start(QCA955X_RESET_PCIE_PHY);
335 ar71xx_device_start(QCA955X_RESET_PCIE);
337 if (qca955x_pci_setup(dev))
341 * Write initial base address.
343 * I'm not yet sure why this is required and/or why it isn't
344 * initialised like this. The AR71xx PCI code initialises
345 * the PCI windows for each device, but neither it or the
346 * 724x PCI bridge modules explicitly initialise the BAR.
348 * So before this gets committed, have a chat with jhb@ or
349 * someone else who knows PCI well and figure out whether
350 * the initial BAR is supposed to be determined by /other/
353 qca955x_pci_write_config(dev, 0, 0, 0, PCIR_BAR(0),
357 /* Fixup internal PCI bridge */
358 qca955x_pci_write_config(dev, 0, 0, 0, PCIR_COMMAND,
359 PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN
360 | PCIM_CMD_SERRESPEN | PCIM_CMD_BACKTOBACK
361 | PCIM_CMD_PERRESPEN | PCIM_CMD_MWRICEN, 2);
363 device_add_child(dev, "pci", -1);
364 return (bus_generic_attach(dev));
368 qca955x_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
370 struct ar71xx_pci_softc *sc = device_get_softc(dev);
373 case PCIB_IVAR_DOMAIN:
377 *result = sc->sc_busno;
385 qca955x_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t result)
387 struct ar71xx_pci_softc * sc = device_get_softc(dev);
391 sc->sc_busno = result;
398 static struct resource *
399 qca955x_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
400 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
402 struct ar71xx_pci_softc *sc = device_get_softc(bus);
408 rm = &sc->sc_irq_rman;
411 rm = &sc->sc_mem_rman;
417 rv = rman_reserve_resource(rm, start, end, count, flags, child);
422 rman_set_rid(rv, *rid);
424 if (flags & RF_ACTIVE) {
425 if (bus_activate_resource(child, type, *rid, rv)) {
426 rman_release_resource(rv);
435 qca955x_pci_activate_resource(device_t bus, device_t child, int type, int rid,
438 int res = (BUS_ACTIVATE_RESOURCE(device_get_parent(bus),
439 child, type, rid, r));
446 rman_set_bustag(r, ar71xx_bus_space_pcimem);
455 qca955x_pci_setup_intr(device_t bus, device_t child, struct resource *ires,
456 int flags, driver_filter_t *filt, driver_intr_t *handler,
457 void *arg, void **cookiep)
459 struct ar71xx_pci_softc *sc = device_get_softc(bus);
460 struct intr_event *event;
463 irq = rman_get_start(ires);
464 if (irq > AR71XX_PCI_IRQ_END)
465 panic("%s: bad irq %d", __func__, irq);
467 event = sc->sc_eventstab[irq];
469 sc->sc_pci_irq[irq].sc = sc;
470 sc->sc_pci_irq[irq].irq = irq;
471 error = intr_event_create(&event, (void *)&sc->sc_pci_irq[irq],
473 qca955x_pci_mask_irq,
474 qca955x_pci_unmask_irq,
479 sc->sc_eventstab[irq] = event;
480 sc->sc_intr_counter[irq] =
481 mips_intrcnt_create(event->ie_name);
487 intr_event_add_handler(event, device_get_nameunit(child), filt,
488 handler, arg, intr_priority(flags), flags, cookiep);
489 mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
491 qca955x_pci_unmask_irq(&sc->sc_pci_irq[irq]);
497 qca955x_pci_teardown_intr(device_t dev, device_t child, struct resource *ires,
500 struct ar71xx_pci_softc *sc = device_get_softc(dev);
503 irq = rman_get_start(ires);
504 if (irq > AR71XX_PCI_IRQ_END)
505 panic("%s: bad irq %d", __func__, irq);
507 if (sc->sc_eventstab[irq] == NULL)
508 panic("Trying to teardown unoccupied IRQ");
510 qca955x_pci_mask_irq(&sc->sc_pci_irq[irq]);
512 result = intr_event_remove_handler(cookie);
514 sc->sc_eventstab[irq] = NULL;
520 qca955x_pci_intr(void *arg)
522 struct ar71xx_pci_softc *sc = arg;
523 struct intr_event *event;
524 uint32_t reg, irq, mask;
526 /* There's only one PCIe DDR flush for both PCIe EPs */
527 ar71xx_device_flush_ddr(AR71XX_CPU_DDR_FLUSH_PCIE);
529 reg = ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_STATUS);
530 mask = ATH_READ_REG(sc->sc_pci_ctrl_base + QCA955X_PCI_INTR_MASK);
533 * Handle only unmasked interrupts
537 * XXX TODO: handle >1 PCIe end point!
539 if (reg & QCA955X_PCI_INTR_DEV0) {
540 irq = AR71XX_PCI_IRQ_START;
541 event = sc->sc_eventstab[irq];
542 if (!event || CK_SLIST_EMPTY(&event->ie_handlers)) {
543 printf("Stray IRQ %d\n", irq);
544 return (FILTER_STRAY);
547 /* TODO: frame instead of NULL? */
548 intr_event_handle(event, NULL);
549 mips_intrcnt_inc(sc->sc_intr_counter[irq]);
552 return (FILTER_HANDLED);
556 qca955x_pci_maxslots(device_t dev)
559 return (PCI_SLOTMAX);
563 qca955x_pci_route_interrupt(device_t pcib, device_t device, int pin)
566 return (pci_get_slot(device));
569 static device_method_t qca955x_pci_methods[] = {
570 /* Device interface */
571 DEVMETHOD(device_probe, qca955x_pci_probe),
572 DEVMETHOD(device_attach, qca955x_pci_attach),
573 DEVMETHOD(device_shutdown, bus_generic_shutdown),
574 DEVMETHOD(device_suspend, bus_generic_suspend),
575 DEVMETHOD(device_resume, bus_generic_resume),
578 DEVMETHOD(bus_read_ivar, qca955x_pci_read_ivar),
579 DEVMETHOD(bus_write_ivar, qca955x_pci_write_ivar),
580 DEVMETHOD(bus_alloc_resource, qca955x_pci_alloc_resource),
581 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
582 DEVMETHOD(bus_activate_resource, qca955x_pci_activate_resource),
583 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
584 DEVMETHOD(bus_setup_intr, qca955x_pci_setup_intr),
585 DEVMETHOD(bus_teardown_intr, qca955x_pci_teardown_intr),
588 DEVMETHOD(pcib_maxslots, qca955x_pci_maxslots),
589 DEVMETHOD(pcib_read_config, qca955x_pci_read_config),
590 DEVMETHOD(pcib_write_config, qca955x_pci_write_config),
591 DEVMETHOD(pcib_route_interrupt, qca955x_pci_route_interrupt),
592 DEVMETHOD(pcib_request_feature, pcib_request_feature_allow),
597 static driver_t qca955x_pci_driver = {
600 sizeof(struct ar71xx_pci_softc),
603 static devclass_t qca955x_pci_devclass;
605 DRIVER_MODULE(qca955x_pci, nexus, qca955x_pci_driver, qca955x_pci_devclass, 0, 0);
606 DRIVER_MODULE(qca955x_pci, apb, qca955x_pci_driver, qca955x_pci_devclass, 0, 0);