1187706Sgonzo/*- 2187706Sgonzo * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org> 3187706Sgonzo * All rights reserved. 4187706Sgonzo * 5187706Sgonzo * Redistribution and use in source and binary forms, with or without 6187706Sgonzo * modification, are permitted provided that the following conditions 7187706Sgonzo * are met: 8187706Sgonzo * 1. Redistributions of source code must retain the above copyright 9187706Sgonzo * notice unmodified, this list of conditions, and the following 10187706Sgonzo * disclaimer. 11187706Sgonzo * 2. Redistributions in binary form must reproduce the above copyright 12187706Sgonzo * notice, this list of conditions and the following disclaimer in the 13187706Sgonzo * documentation and/or other materials provided with the distribution. 14187706Sgonzo * 15187706Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16187706Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17187706Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18187706Sgonzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19187706Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20187706Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21187706Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22187706Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23187706Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24187706Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25187706Sgonzo * SUCH DAMAGE. 26187706Sgonzo */ 27187706Sgonzo 28187706Sgonzo#include <sys/cdefs.h> 29187706Sgonzo__FBSDID("$FreeBSD$"); 30187706Sgonzo 31187706Sgonzo#include <sys/param.h> 32187706Sgonzo#include <sys/systm.h> 33187706Sgonzo 34187706Sgonzo#include <sys/bus.h> 35187706Sgonzo#include <sys/interrupt.h> 36187706Sgonzo#include <sys/malloc.h> 37187706Sgonzo#include <sys/kernel.h> 38187706Sgonzo#include <sys/module.h> 39187706Sgonzo#include <sys/rman.h> 40187706Sgonzo 41187706Sgonzo#include <vm/vm.h> 42187706Sgonzo#include <vm/pmap.h> 43187706Sgonzo#include <vm/vm_extern.h> 44187706Sgonzo 45187706Sgonzo#include <machine/bus.h> 46187706Sgonzo#include <machine/cpu.h> 47210900Sgonzo#include <machine/intr_machdep.h> 48187706Sgonzo#include <machine/pmap.h> 49187706Sgonzo 50187706Sgonzo#include <dev/pci/pcivar.h> 51187706Sgonzo#include <dev/pci/pcireg.h> 52187706Sgonzo 53187706Sgonzo#include <dev/pci/pcib_private.h> 54187706Sgonzo#include "pcib_if.h" 55187706Sgonzo 56192161Sgonzo#include <mips/atheros/ar71xxreg.h> 57192161Sgonzo#include <mips/atheros/ar71xx_pci_bus_space.h> 58187706Sgonzo 59211478Sadrian#include <mips/atheros/ar71xx_cpudef.h> 60211478Sadrian 61187706Sgonzo#undef AR71XX_PCI_DEBUG 62187706Sgonzo#ifdef AR71XX_PCI_DEBUG 63187706Sgonzo#define dprintf printf 64187706Sgonzo#else 65187706Sgonzo#define dprintf(x, arg...) 66187706Sgonzo#endif 67187706Sgonzo 68187706Sgonzostruct ar71xx_pci_softc { 69187706Sgonzo device_t sc_dev; 70187706Sgonzo 71187706Sgonzo int sc_busno; 72187706Sgonzo struct rman sc_mem_rman; 73187706Sgonzo struct rman sc_irq_rman; 74187706Sgonzo 75191872Sgonzo struct intr_event *sc_eventstab[AR71XX_PCI_NIRQS]; 76210900Sgonzo mips_intrcnt_t sc_intr_counter[AR71XX_PCI_NIRQS]; 77187706Sgonzo struct resource *sc_irq; 78187706Sgonzo void *sc_ih; 79187706Sgonzo}; 80187706Sgonzo 81191872Sgonzostatic int ar71xx_pci_setup_intr(device_t, device_t, struct resource *, int, 82191872Sgonzo driver_filter_t *, driver_intr_t *, void *, void **); 83191872Sgonzostatic int ar71xx_pci_teardown_intr(device_t, device_t, struct resource *, 84191872Sgonzo void *); 85191872Sgonzostatic int ar71xx_pci_intr(void *); 86191872Sgonzo 87192822Sgonzostatic void 88192822Sgonzoar71xx_pci_mask_irq(void *source) 89191872Sgonzo{ 90191872Sgonzo uint32_t reg; 91192822Sgonzo unsigned int irq = (unsigned int)source; 92191872Sgonzo 93191872Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 94194273Sgonzo /* flush */ 95194273Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 96191872Sgonzo ATH_WRITE_REG(AR71XX_PCI_INTR_MASK, reg & ~(1 << irq)); 97191872Sgonzo} 98191872Sgonzo 99192822Sgonzostatic void 100192822Sgonzoar71xx_pci_unmask_irq(void *source) 101191872Sgonzo{ 102191872Sgonzo uint32_t reg; 103192822Sgonzo unsigned int irq = (unsigned int)source; 104191872Sgonzo 105191872Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 106191872Sgonzo ATH_WRITE_REG(AR71XX_PCI_INTR_MASK, reg | (1 << irq)); 107194273Sgonzo /* flush */ 108194273Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 109191872Sgonzo} 110191872Sgonzo 111187706Sgonzo/* 112187706Sgonzo * get bitmask for bytes of interest: 113187706Sgonzo * 0 - we want this byte, 1 - ignore it. e.g: we read 1 byte 114187706Sgonzo * from register 7. Bitmask would be: 0111 115187706Sgonzo */ 116187706Sgonzostatic uint32_t 117187706Sgonzoar71xx_get_bytes_to_read(int reg, int bytes) 118187706Sgonzo{ 119187706Sgonzo uint32_t bytes_to_read = 0; 120187706Sgonzo if ((bytes % 4) == 0) 121187706Sgonzo bytes_to_read = 0; 122187706Sgonzo else if ((bytes % 4) == 1) 123187706Sgonzo bytes_to_read = (~(1 << (reg % 4))) & 0xf; 124187706Sgonzo else if ((bytes % 4) == 2) 125187706Sgonzo bytes_to_read = (~(3 << (reg % 4))) & 0xf; 126187706Sgonzo else 127187706Sgonzo panic("%s: wrong combination", __func__); 128187706Sgonzo 129187706Sgonzo return (bytes_to_read); 130187706Sgonzo} 131187706Sgonzo 132187706Sgonzostatic int 133187706Sgonzoar71xx_pci_check_bus_error(void) 134187706Sgonzo{ 135187706Sgonzo uint32_t error, addr, has_errors = 0; 136187706Sgonzo error = ATH_READ_REG(AR71XX_PCI_ERROR) & 0x3; 137187706Sgonzo dprintf("%s: PCI error = %02x\n", __func__, error); 138187706Sgonzo if (error) { 139187706Sgonzo addr = ATH_READ_REG(AR71XX_PCI_ERROR_ADDR); 140187706Sgonzo 141187706Sgonzo /* Do not report it yet */ 142187706Sgonzo#if 0 143187706Sgonzo printf("PCI bus error %d at addr 0x%08x\n", error, addr); 144187706Sgonzo#endif 145187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_ERROR, error); 146187706Sgonzo has_errors = 1; 147187706Sgonzo } 148187706Sgonzo 149187706Sgonzo error = ATH_READ_REG(AR71XX_PCI_AHB_ERROR) & 0x1; 150187706Sgonzo dprintf("%s: AHB error = %02x\n", __func__, error); 151187706Sgonzo if (error) { 152187706Sgonzo addr = ATH_READ_REG(AR71XX_PCI_AHB_ERROR_ADDR); 153187706Sgonzo /* Do not report it yet */ 154187706Sgonzo#if 0 155187706Sgonzo printf("AHB bus error %d at addr 0x%08x\n", error, addr); 156187706Sgonzo#endif 157187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_AHB_ERROR, error); 158187706Sgonzo has_errors = 1; 159187706Sgonzo } 160187706Sgonzo 161187706Sgonzo return (has_errors); 162187706Sgonzo} 163187706Sgonzo 164187706Sgonzostatic uint32_t 165187706Sgonzoar71xx_pci_make_addr(int bus, int slot, int func, int reg) 166187706Sgonzo{ 167187706Sgonzo if (bus == 0) { 168187706Sgonzo return ((1 << slot) | (func << 8) | (reg & ~3)); 169187706Sgonzo } else { 170187706Sgonzo return ((bus << 16) | (slot << 11) | (func << 8) 171187706Sgonzo | (reg & ~3) | 1); 172187706Sgonzo } 173187706Sgonzo} 174187706Sgonzo 175187706Sgonzostatic int 176187706Sgonzoar71xx_pci_conf_setup(int bus, int slot, int func, int reg, int bytes, 177187706Sgonzo uint32_t cmd) 178187706Sgonzo{ 179187706Sgonzo uint32_t addr = ar71xx_pci_make_addr(bus, slot, func, (reg & ~3)); 180187706Sgonzo cmd |= (ar71xx_get_bytes_to_read(reg, bytes) << 4); 181187706Sgonzo 182187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_CONF_ADDR, addr); 183187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_CONF_CMD, cmd); 184187706Sgonzo 185187706Sgonzo dprintf("%s: tag (%x, %x, %x) %d/%d addr=%08x, cmd=%08x\n", __func__, 186187706Sgonzo bus, slot, func, reg, bytes, addr, cmd); 187187706Sgonzo 188187706Sgonzo return ar71xx_pci_check_bus_error(); 189187706Sgonzo} 190187706Sgonzo 191187706Sgonzostatic uint32_t 192194059Sgonzoar71xx_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func, 193194059Sgonzo u_int reg, int bytes) 194187706Sgonzo{ 195187706Sgonzo uint32_t data; 196187706Sgonzo uint32_t cmd, shift, mask; 197187706Sgonzo 198187706Sgonzo /* register access is 32-bit aligned */ 199187706Sgonzo shift = (reg & 3) * 8; 200187706Sgonzo if (shift) 201187706Sgonzo mask = (1 << shift) - 1; 202187706Sgonzo else 203187706Sgonzo mask = 0xffffffff; 204187706Sgonzo 205187706Sgonzo dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot, 206187706Sgonzo func, reg, bytes); 207187706Sgonzo 208187706Sgonzo if ((bus == 0) && (slot == 0) && (func == 0)) { 209187706Sgonzo cmd = PCI_LCONF_CMD_READ | (reg & ~3); 210187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_LCONF_CMD, cmd); 211187706Sgonzo data = ATH_READ_REG(AR71XX_PCI_LCONF_READ_DATA); 212187706Sgonzo } else { 213187706Sgonzo if (ar71xx_pci_conf_setup(bus, slot, func, reg, bytes, 214187706Sgonzo PCI_CONF_CMD_READ) == 0) 215187706Sgonzo data = ATH_READ_REG(AR71XX_PCI_CONF_READ_DATA); 216187706Sgonzo else 217187706Sgonzo data = -1; 218187706Sgonzo } 219187706Sgonzo 220187706Sgonzo /* get request bytes from 32-bit word */ 221187706Sgonzo data = (data >> shift) & mask; 222187706Sgonzo 223187706Sgonzo dprintf("%s: read 0x%x\n", __func__, data); 224187706Sgonzo 225187706Sgonzo return (data); 226187706Sgonzo} 227187706Sgonzo 228187706Sgonzostatic void 229194059Sgonzoar71xx_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func, 230194059Sgonzo u_int reg, uint32_t data, int bytes) 231187706Sgonzo{ 232187706Sgonzo uint32_t cmd; 233187706Sgonzo 234187706Sgonzo dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot, 235187706Sgonzo func, reg, bytes); 236187706Sgonzo 237187706Sgonzo data = data << (8*(reg % 4)); 238187706Sgonzo 239187706Sgonzo if ((bus == 0) && (slot == 0) && (func == 0)) { 240187706Sgonzo cmd = PCI_LCONF_CMD_WRITE | (reg & ~3); 241187706Sgonzo cmd |= ar71xx_get_bytes_to_read(reg, bytes) << 20; 242187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_LCONF_CMD, cmd); 243187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_LCONF_WRITE_DATA, data); 244187706Sgonzo } else { 245187706Sgonzo if (ar71xx_pci_conf_setup(bus, slot, func, reg, bytes, 246187706Sgonzo PCI_CONF_CMD_WRITE) == 0) 247187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_CONF_WRITE_DATA, data); 248187706Sgonzo } 249187706Sgonzo} 250187706Sgonzo 251187706Sgonzostatic int 252187706Sgonzoar71xx_pci_probe(device_t dev) 253187706Sgonzo{ 254187706Sgonzo 255187706Sgonzo return (0); 256187706Sgonzo} 257187706Sgonzo 258187706Sgonzostatic int 259187706Sgonzoar71xx_pci_attach(device_t dev) 260187706Sgonzo{ 261187706Sgonzo int busno = 0; 262187706Sgonzo int rid = 0; 263187706Sgonzo struct ar71xx_pci_softc *sc = device_get_softc(dev); 264187706Sgonzo 265187706Sgonzo sc->sc_mem_rman.rm_type = RMAN_ARRAY; 266187706Sgonzo sc->sc_mem_rman.rm_descr = "ar71xx PCI memory window"; 267187706Sgonzo if (rman_init(&sc->sc_mem_rman) != 0 || 268187706Sgonzo rman_manage_region(&sc->sc_mem_rman, AR71XX_PCI_MEM_BASE, 269187706Sgonzo AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1) != 0) { 270187706Sgonzo panic("ar71xx_pci_attach: failed to set up I/O rman"); 271187706Sgonzo } 272187706Sgonzo 273187706Sgonzo sc->sc_irq_rman.rm_type = RMAN_ARRAY; 274187706Sgonzo sc->sc_irq_rman.rm_descr = "ar71xx PCI IRQs"; 275187706Sgonzo if (rman_init(&sc->sc_irq_rman) != 0 || 276187706Sgonzo rman_manage_region(&sc->sc_irq_rman, AR71XX_PCI_IRQ_START, 277187706Sgonzo AR71XX_PCI_IRQ_END) != 0) 278187706Sgonzo panic("ar71xx_pci_attach: failed to set up IRQ rman"); 279187706Sgonzo 280187706Sgonzo 281187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_INTR_STATUS, 0); 282187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_INTR_MASK, 0); 283187706Sgonzo 284187706Sgonzo /* Hook up our interrupt handler. */ 285187706Sgonzo if ((sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 286187706Sgonzo RF_SHAREABLE | RF_ACTIVE)) == NULL) { 287187706Sgonzo device_printf(dev, "unable to allocate IRQ resource\n"); 288187706Sgonzo return ENXIO; 289187706Sgonzo } 290187706Sgonzo 291187706Sgonzo if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC, 292191872Sgonzo ar71xx_pci_intr, NULL, sc, &sc->sc_ih))) { 293187706Sgonzo device_printf(dev, 294187706Sgonzo "WARNING: unable to register interrupt handler\n"); 295187706Sgonzo return ENXIO; 296187706Sgonzo } 297187706Sgonzo 298187706Sgonzo /* reset PCI core and PCI bus */ 299211478Sadrian ar71xx_device_stop(RST_RESET_PCI_CORE | RST_RESET_PCI_BUS); 300203132Sgonzo DELAY(100000); 301187706Sgonzo 302211478Sadrian ar71xx_device_start(RST_RESET_PCI_CORE | RST_RESET_PCI_BUS); 303203132Sgonzo DELAY(100000); 304187706Sgonzo 305187706Sgonzo /* Init PCI windows */ 306187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW0, PCI_WINDOW0_ADDR); 307187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW1, PCI_WINDOW1_ADDR); 308187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW2, PCI_WINDOW2_ADDR); 309187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW3, PCI_WINDOW3_ADDR); 310187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW4, PCI_WINDOW4_ADDR); 311187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW5, PCI_WINDOW5_ADDR); 312187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW6, PCI_WINDOW6_ADDR); 313187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW7, PCI_WINDOW7_CONF_ADDR); 314203132Sgonzo DELAY(100000); 315187706Sgonzo 316187706Sgonzo ar71xx_pci_check_bus_error(); 317187706Sgonzo 318187706Sgonzo /* Fixup internal PCI bridge */ 319187706Sgonzo ar71xx_pci_write_config(dev, 0, 0, 0, PCIR_COMMAND, 320187706Sgonzo PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN 321187706Sgonzo | PCIM_CMD_SERRESPEN | PCIM_CMD_BACKTOBACK 322187706Sgonzo | PCIM_CMD_PERRESPEN | PCIM_CMD_MWRICEN, 2); 323187706Sgonzo 324187706Sgonzo device_add_child(dev, "pci", busno); 325187706Sgonzo return (bus_generic_attach(dev)); 326187706Sgonzo} 327187706Sgonzo 328187706Sgonzostatic int 329187706Sgonzoar71xx_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 330187706Sgonzo{ 331187706Sgonzo struct ar71xx_pci_softc *sc = device_get_softc(dev); 332187706Sgonzo 333187706Sgonzo switch (which) { 334187706Sgonzo case PCIB_IVAR_DOMAIN: 335187706Sgonzo *result = 0; 336187706Sgonzo return (0); 337187706Sgonzo case PCIB_IVAR_BUS: 338187706Sgonzo *result = sc->sc_busno; 339187706Sgonzo return (0); 340187706Sgonzo } 341187706Sgonzo 342187706Sgonzo return (ENOENT); 343187706Sgonzo} 344187706Sgonzo 345187706Sgonzostatic int 346187706Sgonzoar71xx_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t result) 347187706Sgonzo{ 348187706Sgonzo struct ar71xx_pci_softc * sc = device_get_softc(dev); 349187706Sgonzo 350187706Sgonzo switch (which) { 351187706Sgonzo case PCIB_IVAR_BUS: 352187706Sgonzo sc->sc_busno = result; 353187706Sgonzo return (0); 354187706Sgonzo } 355187706Sgonzo 356187706Sgonzo return (ENOENT); 357187706Sgonzo} 358187706Sgonzo 359187706Sgonzostatic struct resource * 360187706Sgonzoar71xx_pci_alloc_resource(device_t bus, device_t child, int type, int *rid, 361187706Sgonzo u_long start, u_long end, u_long count, u_int flags) 362187706Sgonzo{ 363187706Sgonzo 364187706Sgonzo struct ar71xx_pci_softc *sc = device_get_softc(bus); 365192161Sgonzo struct resource *rv; 366187706Sgonzo struct rman *rm; 367187706Sgonzo 368187706Sgonzo switch (type) { 369187706Sgonzo case SYS_RES_IRQ: 370187706Sgonzo rm = &sc->sc_irq_rman; 371187706Sgonzo break; 372187706Sgonzo case SYS_RES_MEMORY: 373187706Sgonzo rm = &sc->sc_mem_rman; 374187706Sgonzo break; 375187706Sgonzo default: 376187706Sgonzo return (NULL); 377187706Sgonzo } 378187706Sgonzo 379187706Sgonzo rv = rman_reserve_resource(rm, start, end, count, flags, child); 380187706Sgonzo 381187706Sgonzo if (rv == NULL) 382187706Sgonzo return (NULL); 383187706Sgonzo 384187706Sgonzo rman_set_rid(rv, *rid); 385187706Sgonzo 386187706Sgonzo if (flags & RF_ACTIVE) { 387187706Sgonzo if (bus_activate_resource(child, type, *rid, rv)) { 388187706Sgonzo rman_release_resource(rv); 389187706Sgonzo return (NULL); 390187706Sgonzo } 391187706Sgonzo } 392187706Sgonzo 393192161Sgonzo 394187706Sgonzo return (rv); 395187706Sgonzo} 396187706Sgonzo 397192161Sgonzo 398187706Sgonzostatic int 399192161Sgonzoar71xx_pci_activate_resource(device_t bus, device_t child, int type, int rid, 400192161Sgonzo struct resource *r) 401192161Sgonzo{ 402192161Sgonzo int res = (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), 403192161Sgonzo child, type, rid, r)); 404192161Sgonzo 405192161Sgonzo if (!res) { 406192161Sgonzo switch(type) { 407192161Sgonzo case SYS_RES_MEMORY: 408192161Sgonzo case SYS_RES_IOPORT: 409192161Sgonzo rman_set_bustag(r, ar71xx_bus_space_pcimem); 410192161Sgonzo break; 411192161Sgonzo } 412192161Sgonzo } 413192161Sgonzo 414192161Sgonzo return (res); 415192161Sgonzo} 416192161Sgonzo 417192161Sgonzo 418192161Sgonzo 419192161Sgonzostatic int 420191872Sgonzoar71xx_pci_setup_intr(device_t bus, device_t child, struct resource *ires, 421191872Sgonzo int flags, driver_filter_t *filt, driver_intr_t *handler, 422191872Sgonzo void *arg, void **cookiep) 423191872Sgonzo{ 424191872Sgonzo struct ar71xx_pci_softc *sc = device_get_softc(bus); 425191872Sgonzo struct intr_event *event; 426191872Sgonzo int irq, error; 427191872Sgonzo 428191872Sgonzo irq = rman_get_start(ires); 429191872Sgonzo 430191872Sgonzo if (irq > AR71XX_PCI_IRQ_END) 431191872Sgonzo panic("%s: bad irq %d", __func__, irq); 432191872Sgonzo 433191872Sgonzo event = sc->sc_eventstab[irq]; 434191872Sgonzo if (event == NULL) { 435191872Sgonzo error = intr_event_create(&event, (void *)irq, 0, irq, 436192822Sgonzo ar71xx_pci_mask_irq, ar71xx_pci_unmask_irq, NULL, NULL, 437210900Sgonzo "pci intr%d:", irq); 438191872Sgonzo 439210900Sgonzo if (error == 0) { 440210900Sgonzo sc->sc_eventstab[irq] = event; 441210900Sgonzo sc->sc_intr_counter[irq] = 442210900Sgonzo mips_intrcnt_create(event->ie_name); 443210900Sgonzo } 444210900Sgonzo else 445210900Sgonzo return error; 446191872Sgonzo } 447191872Sgonzo 448191872Sgonzo intr_event_add_handler(event, device_get_nameunit(child), filt, 449191872Sgonzo handler, arg, intr_priority(flags), flags, cookiep); 450210900Sgonzo mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname); 451191872Sgonzo 452192822Sgonzo ar71xx_pci_unmask_irq((void*)irq); 453191872Sgonzo 454191872Sgonzo return (0); 455191872Sgonzo} 456191872Sgonzo 457191872Sgonzostatic int 458191872Sgonzoar71xx_pci_teardown_intr(device_t dev, device_t child, struct resource *ires, 459187706Sgonzo void *cookie) 460187706Sgonzo{ 461191872Sgonzo struct ar71xx_pci_softc *sc = device_get_softc(dev); 462191872Sgonzo int irq, result; 463187706Sgonzo 464191872Sgonzo irq = rman_get_start(ires); 465191872Sgonzo if (irq > AR71XX_PCI_IRQ_END) 466191872Sgonzo panic("%s: bad irq %d", __func__, irq); 467191872Sgonzo 468191872Sgonzo if (sc->sc_eventstab[irq] == NULL) 469191872Sgonzo panic("Trying to teardown unoccupied IRQ"); 470191872Sgonzo 471192822Sgonzo ar71xx_pci_mask_irq((void*)irq); 472191872Sgonzo 473191872Sgonzo result = intr_event_remove_handler(cookie); 474191872Sgonzo if (!result) 475191872Sgonzo sc->sc_eventstab[irq] = NULL; 476191872Sgonzo 477191872Sgonzo return (result); 478187706Sgonzo} 479187706Sgonzo 480187706Sgonzostatic int 481191872Sgonzoar71xx_pci_intr(void *arg) 482191872Sgonzo{ 483191872Sgonzo struct ar71xx_pci_softc *sc = arg; 484191872Sgonzo struct intr_event *event; 485194273Sgonzo uint32_t reg, irq, mask; 486191872Sgonzo 487191872Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_STATUS); 488194273Sgonzo mask = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 489194273Sgonzo /* 490194273Sgonzo * Handle only unmasked interrupts 491194273Sgonzo */ 492194273Sgonzo reg &= mask; 493191872Sgonzo for (irq = AR71XX_PCI_IRQ_START; irq <= AR71XX_PCI_IRQ_END; irq++) { 494191872Sgonzo if (reg & (1 << irq)) { 495191872Sgonzo event = sc->sc_eventstab[irq]; 496191872Sgonzo if (!event || TAILQ_EMPTY(&event->ie_handlers)) { 497191872Sgonzo /* Ignore timer interrupts */ 498191872Sgonzo if (irq != 0) 499191872Sgonzo printf("Stray IRQ %d\n", irq); 500191872Sgonzo continue; 501191872Sgonzo } 502191872Sgonzo 503221256Sadrian /* Flush DDR FIFO for IP2 */ 504221256Sadrian ar71xx_device_ddr_flush_ip2(); 505221256Sadrian 506191872Sgonzo /* TODO: frame instead of NULL? */ 507191872Sgonzo intr_event_handle(event, NULL); 508210900Sgonzo mips_intrcnt_inc(sc->sc_intr_counter[irq]); 509191872Sgonzo } 510191872Sgonzo } 511191872Sgonzo 512191872Sgonzo return (FILTER_HANDLED); 513191872Sgonzo} 514191872Sgonzo 515191872Sgonzostatic int 516187706Sgonzoar71xx_pci_maxslots(device_t dev) 517187706Sgonzo{ 518187706Sgonzo 519187706Sgonzo return (PCI_SLOTMAX); 520187706Sgonzo} 521187706Sgonzo 522187706Sgonzostatic int 523187706Sgonzoar71xx_pci_route_interrupt(device_t pcib, device_t device, int pin) 524187706Sgonzo{ 525195474Sgonzo if (pci_get_slot(device) < AR71XX_PCI_BASE_SLOT) 526195474Sgonzo panic("%s: PCI slot %d is less then AR71XX_PCI_BASE_SLOT", 527195474Sgonzo __func__, pci_get_slot(device)); 528187706Sgonzo 529195474Sgonzo return (pci_get_slot(device) - AR71XX_PCI_BASE_SLOT); 530187706Sgonzo} 531187706Sgonzo 532187706Sgonzostatic device_method_t ar71xx_pci_methods[] = { 533187706Sgonzo /* Device interface */ 534187706Sgonzo DEVMETHOD(device_probe, ar71xx_pci_probe), 535187706Sgonzo DEVMETHOD(device_attach, ar71xx_pci_attach), 536187706Sgonzo DEVMETHOD(device_shutdown, bus_generic_shutdown), 537187706Sgonzo DEVMETHOD(device_suspend, bus_generic_suspend), 538187706Sgonzo DEVMETHOD(device_resume, bus_generic_resume), 539187706Sgonzo 540187706Sgonzo /* Bus interface */ 541187706Sgonzo DEVMETHOD(bus_read_ivar, ar71xx_pci_read_ivar), 542187706Sgonzo DEVMETHOD(bus_write_ivar, ar71xx_pci_write_ivar), 543187706Sgonzo DEVMETHOD(bus_alloc_resource, ar71xx_pci_alloc_resource), 544187706Sgonzo DEVMETHOD(bus_release_resource, bus_generic_release_resource), 545192161Sgonzo DEVMETHOD(bus_activate_resource, ar71xx_pci_activate_resource), 546187706Sgonzo DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 547191872Sgonzo DEVMETHOD(bus_setup_intr, ar71xx_pci_setup_intr), 548187706Sgonzo DEVMETHOD(bus_teardown_intr, ar71xx_pci_teardown_intr), 549187706Sgonzo 550187706Sgonzo /* pcib interface */ 551187706Sgonzo DEVMETHOD(pcib_maxslots, ar71xx_pci_maxslots), 552187706Sgonzo DEVMETHOD(pcib_read_config, ar71xx_pci_read_config), 553187706Sgonzo DEVMETHOD(pcib_write_config, ar71xx_pci_write_config), 554187706Sgonzo DEVMETHOD(pcib_route_interrupt, ar71xx_pci_route_interrupt), 555187706Sgonzo 556229093Shselasky DEVMETHOD_END 557187706Sgonzo}; 558187706Sgonzo 559187706Sgonzostatic driver_t ar71xx_pci_driver = { 560187706Sgonzo "pcib", 561187706Sgonzo ar71xx_pci_methods, 562187706Sgonzo sizeof(struct ar71xx_pci_softc), 563187706Sgonzo}; 564187706Sgonzo 565187706Sgonzostatic devclass_t ar71xx_pci_devclass; 566187706Sgonzo 567187706SgonzoDRIVER_MODULE(ar71xx_pci, nexus, ar71xx_pci_driver, ar71xx_pci_devclass, 0, 0); 568