ar71xx_pci.c revision 234366
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: head/sys/mips/atheros/ar71xx_pci.c 234366 2012-04-17 01:34:49Z adrian $"); 30187706Sgonzo 31230195Sadrian#include "opt_ar71xx.h" 32230195Sadrian 33187706Sgonzo#include <sys/param.h> 34187706Sgonzo#include <sys/systm.h> 35187706Sgonzo 36187706Sgonzo#include <sys/bus.h> 37187706Sgonzo#include <sys/interrupt.h> 38187706Sgonzo#include <sys/malloc.h> 39187706Sgonzo#include <sys/kernel.h> 40187706Sgonzo#include <sys/module.h> 41187706Sgonzo#include <sys/rman.h> 42234365Sadrian#include <sys/lock.h> 43234365Sadrian#include <sys/mutex.h> 44187706Sgonzo 45187706Sgonzo#include <vm/vm.h> 46187706Sgonzo#include <vm/pmap.h> 47187706Sgonzo#include <vm/vm_extern.h> 48187706Sgonzo 49187706Sgonzo#include <machine/bus.h> 50187706Sgonzo#include <machine/cpu.h> 51210900Sgonzo#include <machine/intr_machdep.h> 52187706Sgonzo#include <machine/pmap.h> 53187706Sgonzo 54187706Sgonzo#include <dev/pci/pcivar.h> 55187706Sgonzo#include <dev/pci/pcireg.h> 56187706Sgonzo 57187706Sgonzo#include <dev/pci/pcib_private.h> 58187706Sgonzo#include "pcib_if.h" 59187706Sgonzo 60192161Sgonzo#include <mips/atheros/ar71xxreg.h> 61192161Sgonzo#include <mips/atheros/ar71xx_pci_bus_space.h> 62187706Sgonzo 63211478Sadrian#include <mips/atheros/ar71xx_cpudef.h> 64211478Sadrian 65234217Sadrian#ifdef AR71XX_ATH_EEPROM 66234217Sadrian#include <sys/linker.h> 67234217Sadrian#include <sys/firmware.h> 68234217Sadrian#endif /* AR71XX_ATH_EEPROM */ 69234217Sadrian 70234366Sadrian#undef AR71XX_PCI_DEBUG 71234366Sadrian#ifdef AR71XX_PCI_DEBUG 72234366Sadrian#define dprintf printf 73187706Sgonzo#else 74234366Sadrian#define dprintf(x, arg...) 75187706Sgonzo#endif 76187706Sgonzo 77234365Sadrianstruct mtx ar71xx_pci_mtx; 78234365SadrianMTX_SYSINIT(ar71xx_pci_mtx, &ar71xx_pci_mtx, "ar71xx PCI space mutex", 79234365Sadrian MTX_SPIN); 80234365Sadrian 81187706Sgonzostruct ar71xx_pci_softc { 82187706Sgonzo device_t sc_dev; 83187706Sgonzo 84187706Sgonzo int sc_busno; 85187706Sgonzo struct rman sc_mem_rman; 86187706Sgonzo struct rman sc_irq_rman; 87187706Sgonzo 88191872Sgonzo struct intr_event *sc_eventstab[AR71XX_PCI_NIRQS]; 89210900Sgonzo mips_intrcnt_t sc_intr_counter[AR71XX_PCI_NIRQS]; 90187706Sgonzo struct resource *sc_irq; 91187706Sgonzo void *sc_ih; 92187706Sgonzo}; 93187706Sgonzo 94191872Sgonzostatic int ar71xx_pci_setup_intr(device_t, device_t, struct resource *, int, 95191872Sgonzo driver_filter_t *, driver_intr_t *, void *, void **); 96191872Sgonzostatic int ar71xx_pci_teardown_intr(device_t, device_t, struct resource *, 97191872Sgonzo void *); 98191872Sgonzostatic int ar71xx_pci_intr(void *); 99191872Sgonzo 100234366Sadrianstatic void 101192822Sgonzoar71xx_pci_mask_irq(void *source) 102191872Sgonzo{ 103191872Sgonzo uint32_t reg; 104192822Sgonzo unsigned int irq = (unsigned int)source; 105191872Sgonzo 106234365Sadrian /* XXX is the PCI lock required here? */ 107191872Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 108194273Sgonzo /* flush */ 109194273Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 110191872Sgonzo ATH_WRITE_REG(AR71XX_PCI_INTR_MASK, reg & ~(1 << irq)); 111191872Sgonzo} 112191872Sgonzo 113234366Sadrianstatic void 114192822Sgonzoar71xx_pci_unmask_irq(void *source) 115191872Sgonzo{ 116191872Sgonzo uint32_t reg; 117192822Sgonzo unsigned int irq = (unsigned int)source; 118191872Sgonzo 119234365Sadrian /* XXX is the PCI lock required here? */ 120191872Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 121191872Sgonzo ATH_WRITE_REG(AR71XX_PCI_INTR_MASK, reg | (1 << irq)); 122194273Sgonzo /* flush */ 123194273Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 124191872Sgonzo} 125191872Sgonzo 126234366Sadrian/* 127234366Sadrian * get bitmask for bytes of interest: 128234366Sadrian * 0 - we want this byte, 1 - ignore it. e.g: we read 1 byte 129187706Sgonzo * from register 7. Bitmask would be: 0111 130187706Sgonzo */ 131187706Sgonzostatic uint32_t 132187706Sgonzoar71xx_get_bytes_to_read(int reg, int bytes) 133187706Sgonzo{ 134187706Sgonzo uint32_t bytes_to_read = 0; 135234366Sadrian 136187706Sgonzo if ((bytes % 4) == 0) 137187706Sgonzo bytes_to_read = 0; 138187706Sgonzo else if ((bytes % 4) == 1) 139187706Sgonzo bytes_to_read = (~(1 << (reg % 4))) & 0xf; 140187706Sgonzo else if ((bytes % 4) == 2) 141187706Sgonzo bytes_to_read = (~(3 << (reg % 4))) & 0xf; 142187706Sgonzo else 143187706Sgonzo panic("%s: wrong combination", __func__); 144187706Sgonzo 145187706Sgonzo return (bytes_to_read); 146187706Sgonzo} 147187706Sgonzo 148234366Sadrianstatic int 149187706Sgonzoar71xx_pci_check_bus_error(void) 150187706Sgonzo{ 151187706Sgonzo uint32_t error, addr, has_errors = 0; 152234365Sadrian 153234365Sadrian mtx_assert(&ar71xx_pci_mtx, MA_OWNED); 154234365Sadrian 155187706Sgonzo error = ATH_READ_REG(AR71XX_PCI_ERROR) & 0x3; 156187706Sgonzo dprintf("%s: PCI error = %02x\n", __func__, error); 157187706Sgonzo if (error) { 158187706Sgonzo addr = ATH_READ_REG(AR71XX_PCI_ERROR_ADDR); 159187706Sgonzo 160187706Sgonzo /* Do not report it yet */ 161187706Sgonzo#if 0 162187706Sgonzo printf("PCI bus error %d at addr 0x%08x\n", error, addr); 163187706Sgonzo#endif 164187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_ERROR, error); 165187706Sgonzo has_errors = 1; 166187706Sgonzo } 167187706Sgonzo 168187706Sgonzo error = ATH_READ_REG(AR71XX_PCI_AHB_ERROR) & 0x1; 169187706Sgonzo dprintf("%s: AHB error = %02x\n", __func__, error); 170187706Sgonzo if (error) { 171187706Sgonzo addr = ATH_READ_REG(AR71XX_PCI_AHB_ERROR_ADDR); 172187706Sgonzo /* Do not report it yet */ 173187706Sgonzo#if 0 174187706Sgonzo printf("AHB bus error %d at addr 0x%08x\n", error, addr); 175187706Sgonzo#endif 176187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_AHB_ERROR, error); 177187706Sgonzo has_errors = 1; 178187706Sgonzo } 179187706Sgonzo 180187706Sgonzo return (has_errors); 181187706Sgonzo} 182187706Sgonzo 183187706Sgonzostatic uint32_t 184187706Sgonzoar71xx_pci_make_addr(int bus, int slot, int func, int reg) 185187706Sgonzo{ 186187706Sgonzo if (bus == 0) { 187187706Sgonzo return ((1 << slot) | (func << 8) | (reg & ~3)); 188187706Sgonzo } else { 189234366Sadrian return ((bus << 16) | (slot << 11) | (func << 8) 190187706Sgonzo | (reg & ~3) | 1); 191187706Sgonzo } 192187706Sgonzo} 193187706Sgonzo 194187706Sgonzostatic int 195234366Sadrianar71xx_pci_conf_setup(int bus, int slot, int func, int reg, int bytes, 196187706Sgonzo uint32_t cmd) 197187706Sgonzo{ 198187706Sgonzo uint32_t addr = ar71xx_pci_make_addr(bus, slot, func, (reg & ~3)); 199234365Sadrian 200234365Sadrian mtx_assert(&ar71xx_pci_mtx, MA_OWNED); 201234365Sadrian 202234366Sadrian cmd |= (ar71xx_get_bytes_to_read(reg, bytes) << 4); 203187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_CONF_ADDR, addr); 204187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_CONF_CMD, cmd); 205187706Sgonzo 206187706Sgonzo dprintf("%s: tag (%x, %x, %x) %d/%d addr=%08x, cmd=%08x\n", __func__, 207187706Sgonzo bus, slot, func, reg, bytes, addr, cmd); 208187706Sgonzo 209187706Sgonzo return ar71xx_pci_check_bus_error(); 210187706Sgonzo} 211187706Sgonzo 212187706Sgonzostatic uint32_t 213234366Sadrianar71xx_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func, 214194059Sgonzo u_int reg, int bytes) 215187706Sgonzo{ 216187706Sgonzo uint32_t data; 217234205Sadrian uint32_t shift, mask; 218187706Sgonzo 219187706Sgonzo /* register access is 32-bit aligned */ 220187706Sgonzo shift = (reg & 3) * 8; 221234306Sadrian 222234306Sadrian /* Create a mask based on the width, post-shift */ 223234306Sadrian if (bytes == 2) 224234306Sadrian mask = 0xffff; 225234306Sadrian else if (bytes == 1) 226234306Sadrian mask = 0xff; 227187706Sgonzo else 228187706Sgonzo mask = 0xffffffff; 229187706Sgonzo 230187706Sgonzo dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot, 231187706Sgonzo func, reg, bytes); 232187706Sgonzo 233234365Sadrian mtx_lock_spin(&ar71xx_pci_mtx); 234234204Sadrian if (ar71xx_pci_conf_setup(bus, slot, func, reg, bytes, 235234204Sadrian PCI_CONF_CMD_READ) == 0) 236234204Sadrian data = ATH_READ_REG(AR71XX_PCI_CONF_READ_DATA); 237234204Sadrian else 238234204Sadrian data = -1; 239234365Sadrian mtx_unlock_spin(&ar71xx_pci_mtx); 240187706Sgonzo 241187706Sgonzo /* get request bytes from 32-bit word */ 242187706Sgonzo data = (data >> shift) & mask; 243187706Sgonzo 244234366Sadrian dprintf("%s: read 0x%x\n", __func__, data); 245187706Sgonzo 246187706Sgonzo return (data); 247187706Sgonzo} 248187706Sgonzo 249187706Sgonzostatic void 250234204Sadrianar71xx_pci_local_write(device_t dev, uint32_t reg, uint32_t data, int bytes) 251187706Sgonzo{ 252187706Sgonzo uint32_t cmd; 253187706Sgonzo 254234204Sadrian dprintf("%s: local write reg %d(%d)\n", __func__, reg, bytes); 255234204Sadrian 256234204Sadrian data = data << (8*(reg % 4)); 257234204Sadrian cmd = PCI_LCONF_CMD_WRITE | (reg & ~3); 258234204Sadrian cmd |= (ar71xx_get_bytes_to_read(reg, bytes) << 20); 259234365Sadrian mtx_lock_spin(&ar71xx_pci_mtx); 260234204Sadrian ATH_WRITE_REG(AR71XX_PCI_LCONF_CMD, cmd); 261234204Sadrian ATH_WRITE_REG(AR71XX_PCI_LCONF_WRITE_DATA, data); 262234365Sadrian mtx_unlock_spin(&ar71xx_pci_mtx); 263234204Sadrian} 264234204Sadrian 265234204Sadrianstatic void 266234204Sadrianar71xx_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func, 267234204Sadrian u_int reg, uint32_t data, int bytes) 268234204Sadrian{ 269234204Sadrian 270234204Sadrian dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot, 271187706Sgonzo func, reg, bytes); 272187706Sgonzo 273187706Sgonzo data = data << (8*(reg % 4)); 274234365Sadrian mtx_lock_spin(&ar71xx_pci_mtx); 275234204Sadrian if (ar71xx_pci_conf_setup(bus, slot, func, reg, bytes, 276234204Sadrian PCI_CONF_CMD_WRITE) == 0) 277234204Sadrian ATH_WRITE_REG(AR71XX_PCI_CONF_WRITE_DATA, data); 278234365Sadrian mtx_unlock_spin(&ar71xx_pci_mtx); 279187706Sgonzo} 280187706Sgonzo 281230148Sadrian#ifdef AR71XX_ATH_EEPROM 282230148Sadrian/* 283230148Sadrian * Some embedded boards (eg AP94) have the MAC attached via PCI but they 284230148Sadrian * don't have the MAC-attached EEPROM. The register initialisation 285230148Sadrian * values and calibration data are stored in the on-board flash. 286230148Sadrian * This routine initialises the NIC via the EEPROM register contents 287230148Sadrian * before the probe/attach routines get a go at things. 288230148Sadrian */ 289230148Sadrianstatic void 290230148Sadrianar71xx_pci_fixup(device_t dev, u_int bus, u_int slot, u_int func, 291230148Sadrian long flash_addr) 292230148Sadrian{ 293230148Sadrian uint16_t *cal_data = (uint16_t *) MIPS_PHYS_TO_KSEG1(flash_addr); 294230148Sadrian uint32_t reg, val, bar0; 295230148Sadrian 296234217Sadrian if (bootverbose) 297234217Sadrian device_printf(dev, "%s: flash_addr=%lx, cal_data=%p\n", 298234217Sadrian __func__, flash_addr, cal_data); 299230148Sadrian 300230148Sadrian /* XXX check 0xa55a */ 301230148Sadrian /* Save bar(0) address - just to flush bar(0) (SoC WAR) ? */ 302230148Sadrian bar0 = ar71xx_pci_read_config(dev, bus, slot, func, PCIR_BAR(0), 4); 303230148Sadrian ar71xx_pci_write_config(dev, bus, slot, func, PCIR_BAR(0), 304230148Sadrian AR71XX_PCI_MEM_BASE, 4); 305230148Sadrian 306230148Sadrian val = ar71xx_pci_read_config(dev, bus, slot, func, PCIR_COMMAND, 2); 307230148Sadrian val |= (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN); 308230148Sadrian ar71xx_pci_write_config(dev, bus, slot, func, PCIR_COMMAND, val, 2); 309230148Sadrian 310230148Sadrian cal_data += 3; 311230148Sadrian while (*cal_data != 0xffff) { 312230148Sadrian reg = *cal_data++; 313230148Sadrian val = *cal_data++; 314230148Sadrian val |= (*cal_data++) << 16; 315234217Sadrian if (bootverbose) 316234217Sadrian printf(" reg: %x, val=%x\n", reg, val); 317230148Sadrian 318230148Sadrian /* Write eeprom fixup data to device memory */ 319230148Sadrian ATH_WRITE_REG(AR71XX_PCI_MEM_BASE + reg, val); 320230148Sadrian DELAY(100); 321230148Sadrian } 322230148Sadrian 323230148Sadrian val = ar71xx_pci_read_config(dev, bus, slot, func, PCIR_COMMAND, 2); 324230148Sadrian val &= ~(PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN); 325230148Sadrian ar71xx_pci_write_config(dev, bus, slot, func, PCIR_COMMAND, val, 2); 326230148Sadrian 327230148Sadrian /* Write the saved bar(0) address */ 328230148Sadrian ar71xx_pci_write_config(dev, bus, slot, func, PCIR_BAR(0), bar0, 4); 329230148Sadrian} 330230148Sadrian 331234217Sadrian/* 332234217Sadrian * Take a copy of the EEPROM contents and squirrel it away in a firmware. 333234217Sadrian * The SPI flash will eventually cease to be memory-mapped, so we need 334234217Sadrian * to take a copy of this before the SPI driver initialises. 335234217Sadrian */ 336230148Sadrianstatic void 337234217Sadrianar71xx_pci_slot_create_eeprom_firmware(device_t dev, u_int bus, u_int slot, 338234217Sadrian u_int func, long int flash_addr) 339234217Sadrian{ 340234217Sadrian char buf[64]; 341234217Sadrian uint16_t *cal_data = (uint16_t *) MIPS_PHYS_TO_KSEG1(flash_addr); 342234217Sadrian void *eeprom = NULL; 343234217Sadrian const struct firmware *fw = NULL; 344234217Sadrian int len; 345234217Sadrian 346234217Sadrian snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_size", 347234217Sadrian bus, slot, func); 348234217Sadrian 349234217Sadrian if (resource_int_value(device_get_name(dev), device_get_unit(dev), 350234217Sadrian buf, &len) != 0) { 351234217Sadrian device_printf(dev, "%s: missing hint '%s', aborting EEPROM\n", 352234217Sadrian __func__, buf); 353234217Sadrian return; 354234217Sadrian } 355234217Sadrian 356234217Sadrian device_printf(dev, "EEPROM firmware: 0x%lx @ %d bytes\n", 357234217Sadrian flash_addr, len); 358234217Sadrian 359234217Sadrian eeprom = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO); 360234217Sadrian if (! eeprom) { 361234217Sadrian device_printf(dev, 362234217Sadrian "%s: malloc failed for '%s', aborting EEPROM\n", 363234217Sadrian __func__, buf); 364234217Sadrian return; 365234217Sadrian } 366234217Sadrian 367234217Sadrian memcpy(eeprom, cal_data, len); 368234217Sadrian 369234217Sadrian /* 370234217Sadrian * Generate a flash EEPROM 'firmware' from the given memory 371234217Sadrian * region. Since the SPI controller will eventually 372234217Sadrian * go into port-IO mode instead of memory-mapped IO 373234217Sadrian * mode, a copy of the EEPROM contents is required. 374234217Sadrian */ 375234217Sadrian snprintf(buf, sizeof(buf), "%s.%d.bus.%d.%d.%d.eeprom_firmware", 376234217Sadrian device_get_name(dev), device_get_unit(dev), bus, slot, func); 377234217Sadrian fw = firmware_register(buf, eeprom, len, 1, NULL); 378234217Sadrian if (fw == NULL) { 379234217Sadrian device_printf(dev, "%s: firmware_register (%s) failed\n", 380234217Sadrian __func__, buf); 381234217Sadrian free(eeprom, M_DEVBUF); 382234217Sadrian return; 383234217Sadrian } 384234217Sadrian device_printf(dev, "device EEPROM '%s' registered\n", buf); 385234217Sadrian} 386234217Sadrian 387234217Sadrianstatic void 388230148Sadrianar71xx_pci_slot_fixup(device_t dev, u_int bus, u_int slot, u_int func) 389230148Sadrian{ 390230148Sadrian long int flash_addr; 391230148Sadrian char buf[32]; 392230148Sadrian 393230148Sadrian /* 394230148Sadrian * Check whether the given slot has a hint to poke. 395230148Sadrian */ 396234217Sadrian if (bootverbose) 397234217Sadrian device_printf(dev, "%s: checking dev %s, %d/%d/%d\n", 398230148Sadrian __func__, device_get_nameunit(dev), bus, slot, func); 399230148Sadrian snprintf(buf, sizeof(buf), "bus.%d.%d.%d.ath_fixup_addr", 400230148Sadrian bus, slot, func); 401230148Sadrian 402230148Sadrian if (resource_long_value(device_get_name(dev), device_get_unit(dev), 403230148Sadrian buf, &flash_addr) == 0) { 404234217Sadrian device_printf(dev, "found EEPROM at 0x%lx on %d.%d.%d\n", 405234217Sadrian flash_addr, bus, slot, func); 406230148Sadrian ar71xx_pci_fixup(dev, bus, slot, func, flash_addr); 407234217Sadrian ar71xx_pci_slot_create_eeprom_firmware(dev, bus, slot, func, 408234217Sadrian flash_addr); 409230148Sadrian } 410230148Sadrian} 411230148Sadrian#endif /* AR71XX_ATH_EEPROM */ 412230148Sadrian 413187706Sgonzostatic int 414187706Sgonzoar71xx_pci_probe(device_t dev) 415187706Sgonzo{ 416187706Sgonzo 417187706Sgonzo return (0); 418187706Sgonzo} 419187706Sgonzo 420187706Sgonzostatic int 421187706Sgonzoar71xx_pci_attach(device_t dev) 422187706Sgonzo{ 423187706Sgonzo int busno = 0; 424187706Sgonzo int rid = 0; 425187706Sgonzo struct ar71xx_pci_softc *sc = device_get_softc(dev); 426187706Sgonzo 427187706Sgonzo sc->sc_mem_rman.rm_type = RMAN_ARRAY; 428187706Sgonzo sc->sc_mem_rman.rm_descr = "ar71xx PCI memory window"; 429187706Sgonzo if (rman_init(&sc->sc_mem_rman) != 0 || 430187706Sgonzo rman_manage_region(&sc->sc_mem_rman, AR71XX_PCI_MEM_BASE, 431187706Sgonzo AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1) != 0) { 432187706Sgonzo panic("ar71xx_pci_attach: failed to set up I/O rman"); 433187706Sgonzo } 434187706Sgonzo 435187706Sgonzo sc->sc_irq_rman.rm_type = RMAN_ARRAY; 436187706Sgonzo sc->sc_irq_rman.rm_descr = "ar71xx PCI IRQs"; 437187706Sgonzo if (rman_init(&sc->sc_irq_rman) != 0 || 438187706Sgonzo rman_manage_region(&sc->sc_irq_rman, AR71XX_PCI_IRQ_START, 439187706Sgonzo AR71XX_PCI_IRQ_END) != 0) 440187706Sgonzo panic("ar71xx_pci_attach: failed to set up IRQ rman"); 441187706Sgonzo 442187706Sgonzo 443187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_INTR_STATUS, 0); 444187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_INTR_MASK, 0); 445187706Sgonzo 446187706Sgonzo /* Hook up our interrupt handler. */ 447187706Sgonzo if ((sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 448187706Sgonzo RF_SHAREABLE | RF_ACTIVE)) == NULL) { 449187706Sgonzo device_printf(dev, "unable to allocate IRQ resource\n"); 450187706Sgonzo return ENXIO; 451187706Sgonzo } 452187706Sgonzo 453187706Sgonzo if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC, 454191872Sgonzo ar71xx_pci_intr, NULL, sc, &sc->sc_ih))) { 455234366Sadrian device_printf(dev, 456187706Sgonzo "WARNING: unable to register interrupt handler\n"); 457187706Sgonzo return ENXIO; 458187706Sgonzo } 459187706Sgonzo 460187706Sgonzo /* reset PCI core and PCI bus */ 461211478Sadrian ar71xx_device_stop(RST_RESET_PCI_CORE | RST_RESET_PCI_BUS); 462203132Sgonzo DELAY(100000); 463187706Sgonzo 464211478Sadrian ar71xx_device_start(RST_RESET_PCI_CORE | RST_RESET_PCI_BUS); 465203132Sgonzo DELAY(100000); 466187706Sgonzo 467187706Sgonzo /* Init PCI windows */ 468187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW0, PCI_WINDOW0_ADDR); 469187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW1, PCI_WINDOW1_ADDR); 470187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW2, PCI_WINDOW2_ADDR); 471187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW3, PCI_WINDOW3_ADDR); 472187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW4, PCI_WINDOW4_ADDR); 473187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW5, PCI_WINDOW5_ADDR); 474187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW6, PCI_WINDOW6_ADDR); 475187706Sgonzo ATH_WRITE_REG(AR71XX_PCI_WINDOW7, PCI_WINDOW7_CONF_ADDR); 476203132Sgonzo DELAY(100000); 477187706Sgonzo 478234365Sadrian mtx_lock_spin(&ar71xx_pci_mtx); 479187706Sgonzo ar71xx_pci_check_bus_error(); 480234365Sadrian mtx_unlock_spin(&ar71xx_pci_mtx); 481187706Sgonzo 482187706Sgonzo /* Fixup internal PCI bridge */ 483234204Sadrian ar71xx_pci_local_write(dev, PCIR_COMMAND, 484234204Sadrian PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN 485187706Sgonzo | PCIM_CMD_SERRESPEN | PCIM_CMD_BACKTOBACK 486234204Sadrian | PCIM_CMD_PERRESPEN | PCIM_CMD_MWRICEN, 4); 487187706Sgonzo 488230148Sadrian#ifdef AR71XX_ATH_EEPROM 489230148Sadrian /* 490230148Sadrian * Hard-code a check for slot 17 and 18 - these are 491230148Sadrian * the two PCI slots which may have a PCI device that 492230148Sadrian * requires "fixing". 493230148Sadrian */ 494230148Sadrian ar71xx_pci_slot_fixup(dev, 0, 17, 0); 495230148Sadrian ar71xx_pci_slot_fixup(dev, 0, 18, 0); 496230148Sadrian#endif /* AR71XX_ATH_EEPROM */ 497230148Sadrian 498187706Sgonzo device_add_child(dev, "pci", busno); 499187706Sgonzo return (bus_generic_attach(dev)); 500187706Sgonzo} 501187706Sgonzo 502187706Sgonzostatic int 503234366Sadrianar71xx_pci_read_ivar(device_t dev, device_t child, int which, 504234366Sadrian uintptr_t *result) 505187706Sgonzo{ 506187706Sgonzo struct ar71xx_pci_softc *sc = device_get_softc(dev); 507187706Sgonzo 508187706Sgonzo switch (which) { 509187706Sgonzo case PCIB_IVAR_DOMAIN: 510187706Sgonzo *result = 0; 511187706Sgonzo return (0); 512187706Sgonzo case PCIB_IVAR_BUS: 513187706Sgonzo *result = sc->sc_busno; 514187706Sgonzo return (0); 515187706Sgonzo } 516187706Sgonzo 517187706Sgonzo return (ENOENT); 518187706Sgonzo} 519187706Sgonzo 520187706Sgonzostatic int 521234366Sadrianar71xx_pci_write_ivar(device_t dev, device_t child, int which, 522234366Sadrian uintptr_t result) 523187706Sgonzo{ 524187706Sgonzo struct ar71xx_pci_softc * sc = device_get_softc(dev); 525187706Sgonzo 526187706Sgonzo switch (which) { 527187706Sgonzo case PCIB_IVAR_BUS: 528187706Sgonzo sc->sc_busno = result; 529187706Sgonzo return (0); 530187706Sgonzo } 531187706Sgonzo 532187706Sgonzo return (ENOENT); 533187706Sgonzo} 534187706Sgonzo 535187706Sgonzostatic struct resource * 536187706Sgonzoar71xx_pci_alloc_resource(device_t bus, device_t child, int type, int *rid, 537187706Sgonzo u_long start, u_long end, u_long count, u_int flags) 538187706Sgonzo{ 539187706Sgonzo 540234366Sadrian struct ar71xx_pci_softc *sc = device_get_softc(bus); 541192161Sgonzo struct resource *rv; 542187706Sgonzo struct rman *rm; 543187706Sgonzo 544187706Sgonzo switch (type) { 545187706Sgonzo case SYS_RES_IRQ: 546187706Sgonzo rm = &sc->sc_irq_rman; 547187706Sgonzo break; 548187706Sgonzo case SYS_RES_MEMORY: 549187706Sgonzo rm = &sc->sc_mem_rman; 550187706Sgonzo break; 551187706Sgonzo default: 552187706Sgonzo return (NULL); 553187706Sgonzo } 554187706Sgonzo 555187706Sgonzo rv = rman_reserve_resource(rm, start, end, count, flags, child); 556187706Sgonzo 557187706Sgonzo if (rv == NULL) 558187706Sgonzo return (NULL); 559187706Sgonzo 560187706Sgonzo rman_set_rid(rv, *rid); 561187706Sgonzo 562187706Sgonzo if (flags & RF_ACTIVE) { 563187706Sgonzo if (bus_activate_resource(child, type, *rid, rv)) { 564187706Sgonzo rman_release_resource(rv); 565187706Sgonzo return (NULL); 566187706Sgonzo } 567234366Sadrian } 568187706Sgonzo return (rv); 569187706Sgonzo} 570187706Sgonzo 571187706Sgonzostatic int 572192161Sgonzoar71xx_pci_activate_resource(device_t bus, device_t child, int type, int rid, 573192161Sgonzo struct resource *r) 574192161Sgonzo{ 575192161Sgonzo int res = (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), 576192161Sgonzo child, type, rid, r)); 577192161Sgonzo 578192161Sgonzo if (!res) { 579192161Sgonzo switch(type) { 580192161Sgonzo case SYS_RES_MEMORY: 581192161Sgonzo case SYS_RES_IOPORT: 582192161Sgonzo rman_set_bustag(r, ar71xx_bus_space_pcimem); 583192161Sgonzo break; 584192161Sgonzo } 585192161Sgonzo } 586192161Sgonzo return (res); 587192161Sgonzo} 588192161Sgonzo 589192161Sgonzostatic int 590191872Sgonzoar71xx_pci_setup_intr(device_t bus, device_t child, struct resource *ires, 591234366Sadrian int flags, driver_filter_t *filt, driver_intr_t *handler, 592234366Sadrian void *arg, void **cookiep) 593191872Sgonzo{ 594191872Sgonzo struct ar71xx_pci_softc *sc = device_get_softc(bus); 595191872Sgonzo struct intr_event *event; 596191872Sgonzo int irq, error; 597191872Sgonzo 598191872Sgonzo irq = rman_get_start(ires); 599191872Sgonzo 600191872Sgonzo if (irq > AR71XX_PCI_IRQ_END) 601191872Sgonzo panic("%s: bad irq %d", __func__, irq); 602191872Sgonzo 603191872Sgonzo event = sc->sc_eventstab[irq]; 604191872Sgonzo if (event == NULL) { 605191872Sgonzo error = intr_event_create(&event, (void *)irq, 0, irq, 606192822Sgonzo ar71xx_pci_mask_irq, ar71xx_pci_unmask_irq, NULL, NULL, 607210900Sgonzo "pci intr%d:", irq); 608191872Sgonzo 609210900Sgonzo if (error == 0) { 610210900Sgonzo sc->sc_eventstab[irq] = event; 611210900Sgonzo sc->sc_intr_counter[irq] = 612210900Sgonzo mips_intrcnt_create(event->ie_name); 613210900Sgonzo } 614210900Sgonzo else 615234366Sadrian return (error); 616191872Sgonzo } 617191872Sgonzo 618191872Sgonzo intr_event_add_handler(event, device_get_nameunit(child), filt, 619191872Sgonzo handler, arg, intr_priority(flags), flags, cookiep); 620210900Sgonzo mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname); 621191872Sgonzo 622192822Sgonzo ar71xx_pci_unmask_irq((void*)irq); 623191872Sgonzo 624191872Sgonzo return (0); 625191872Sgonzo} 626191872Sgonzo 627191872Sgonzostatic int 628191872Sgonzoar71xx_pci_teardown_intr(device_t dev, device_t child, struct resource *ires, 629187706Sgonzo void *cookie) 630187706Sgonzo{ 631191872Sgonzo struct ar71xx_pci_softc *sc = device_get_softc(dev); 632191872Sgonzo int irq, result; 633187706Sgonzo 634191872Sgonzo irq = rman_get_start(ires); 635191872Sgonzo if (irq > AR71XX_PCI_IRQ_END) 636191872Sgonzo panic("%s: bad irq %d", __func__, irq); 637191872Sgonzo 638191872Sgonzo if (sc->sc_eventstab[irq] == NULL) 639191872Sgonzo panic("Trying to teardown unoccupied IRQ"); 640191872Sgonzo 641192822Sgonzo ar71xx_pci_mask_irq((void*)irq); 642191872Sgonzo 643191872Sgonzo result = intr_event_remove_handler(cookie); 644191872Sgonzo if (!result) 645191872Sgonzo sc->sc_eventstab[irq] = NULL; 646191872Sgonzo 647191872Sgonzo return (result); 648187706Sgonzo} 649187706Sgonzo 650187706Sgonzostatic int 651191872Sgonzoar71xx_pci_intr(void *arg) 652191872Sgonzo{ 653191872Sgonzo struct ar71xx_pci_softc *sc = arg; 654191872Sgonzo struct intr_event *event; 655194273Sgonzo uint32_t reg, irq, mask; 656191872Sgonzo 657191872Sgonzo reg = ATH_READ_REG(AR71XX_PCI_INTR_STATUS); 658194273Sgonzo mask = ATH_READ_REG(AR71XX_PCI_INTR_MASK); 659194273Sgonzo /* 660194273Sgonzo * Handle only unmasked interrupts 661194273Sgonzo */ 662194273Sgonzo reg &= mask; 663191872Sgonzo for (irq = AR71XX_PCI_IRQ_START; irq <= AR71XX_PCI_IRQ_END; irq++) { 664191872Sgonzo if (reg & (1 << irq)) { 665191872Sgonzo event = sc->sc_eventstab[irq]; 666191872Sgonzo if (!event || TAILQ_EMPTY(&event->ie_handlers)) { 667191872Sgonzo /* Ignore timer interrupts */ 668191872Sgonzo if (irq != 0) 669191872Sgonzo printf("Stray IRQ %d\n", irq); 670191872Sgonzo continue; 671191872Sgonzo } 672191872Sgonzo 673221256Sadrian /* Flush DDR FIFO for IP2 */ 674221256Sadrian ar71xx_device_ddr_flush_ip2(); 675221256Sadrian 676191872Sgonzo /* TODO: frame instead of NULL? */ 677191872Sgonzo intr_event_handle(event, NULL); 678210900Sgonzo mips_intrcnt_inc(sc->sc_intr_counter[irq]); 679191872Sgonzo } 680191872Sgonzo } 681191872Sgonzo 682191872Sgonzo return (FILTER_HANDLED); 683191872Sgonzo} 684191872Sgonzo 685191872Sgonzostatic int 686187706Sgonzoar71xx_pci_maxslots(device_t dev) 687187706Sgonzo{ 688187706Sgonzo 689187706Sgonzo return (PCI_SLOTMAX); 690187706Sgonzo} 691187706Sgonzo 692187706Sgonzostatic int 693187706Sgonzoar71xx_pci_route_interrupt(device_t pcib, device_t device, int pin) 694187706Sgonzo{ 695195474Sgonzo if (pci_get_slot(device) < AR71XX_PCI_BASE_SLOT) 696195474Sgonzo panic("%s: PCI slot %d is less then AR71XX_PCI_BASE_SLOT", 697195474Sgonzo __func__, pci_get_slot(device)); 698187706Sgonzo 699195474Sgonzo return (pci_get_slot(device) - AR71XX_PCI_BASE_SLOT); 700187706Sgonzo} 701187706Sgonzo 702187706Sgonzostatic device_method_t ar71xx_pci_methods[] = { 703187706Sgonzo /* Device interface */ 704187706Sgonzo DEVMETHOD(device_probe, ar71xx_pci_probe), 705187706Sgonzo DEVMETHOD(device_attach, ar71xx_pci_attach), 706187706Sgonzo DEVMETHOD(device_shutdown, bus_generic_shutdown), 707187706Sgonzo DEVMETHOD(device_suspend, bus_generic_suspend), 708187706Sgonzo DEVMETHOD(device_resume, bus_generic_resume), 709187706Sgonzo 710187706Sgonzo /* Bus interface */ 711187706Sgonzo DEVMETHOD(bus_read_ivar, ar71xx_pci_read_ivar), 712187706Sgonzo DEVMETHOD(bus_write_ivar, ar71xx_pci_write_ivar), 713187706Sgonzo DEVMETHOD(bus_alloc_resource, ar71xx_pci_alloc_resource), 714187706Sgonzo DEVMETHOD(bus_release_resource, bus_generic_release_resource), 715192161Sgonzo DEVMETHOD(bus_activate_resource, ar71xx_pci_activate_resource), 716187706Sgonzo DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 717191872Sgonzo DEVMETHOD(bus_setup_intr, ar71xx_pci_setup_intr), 718187706Sgonzo DEVMETHOD(bus_teardown_intr, ar71xx_pci_teardown_intr), 719187706Sgonzo 720187706Sgonzo /* pcib interface */ 721187706Sgonzo DEVMETHOD(pcib_maxslots, ar71xx_pci_maxslots), 722187706Sgonzo DEVMETHOD(pcib_read_config, ar71xx_pci_read_config), 723187706Sgonzo DEVMETHOD(pcib_write_config, ar71xx_pci_write_config), 724187706Sgonzo DEVMETHOD(pcib_route_interrupt, ar71xx_pci_route_interrupt), 725187706Sgonzo 726227843Smarius DEVMETHOD_END 727187706Sgonzo}; 728187706Sgonzo 729187706Sgonzostatic driver_t ar71xx_pci_driver = { 730187706Sgonzo "pcib", 731187706Sgonzo ar71xx_pci_methods, 732187706Sgonzo sizeof(struct ar71xx_pci_softc), 733187706Sgonzo}; 734187706Sgonzo 735187706Sgonzostatic devclass_t ar71xx_pci_devclass; 736187706Sgonzo 737187706SgonzoDRIVER_MODULE(ar71xx_pci, nexus, ar71xx_pci_driver, ar71xx_pci_devclass, 0, 0); 738