Path: blob/main/sys/arm/annapurna/alpine/alpine_pci.c
39507 views
/*-1* Copyright (c) 2015,2016 Annapurna Labs Ltd. and affiliates2* All rights reserved.3*4* Developed by Semihalf.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728/*29* Alpine PCI/PCI-Express controller driver.30*/3132#include <sys/param.h>33#include <sys/systm.h>34#include <sys/kernel.h>35#include <sys/module.h>36#include <sys/bus.h>37#include <sys/rman.h>38#include <sys/intr.h>3940#include <dev/ofw/openfirm.h>41#include <dev/ofw/ofw_bus.h>42#include <dev/ofw/ofw_bus_subr.h>43#include <dev/pci/pci_host_generic.h>44#include <dev/pci/pci_host_generic_fdt.h>45#include <dev/pci/pcivar.h>46#include <dev/pci/pcireg.h>4748#include "pcib_if.h"4950#include "contrib/alpine-hal/al_hal_unit_adapter_regs.h"51#include "contrib/alpine-hal/al_hal_pcie.h"52#include "contrib/alpine-hal/al_hal_pcie_axi_reg.h"5354#define ANNAPURNA_VENDOR_ID 0x1c365556/* Forward prototypes */57static int al_pcib_probe(device_t);58static int al_pcib_attach(device_t);59static void al_pcib_fixup(device_t);6061static struct ofw_compat_data compat_data[] = {62{"annapurna-labs,al-internal-pcie", true},63{"annapurna-labs,alpine-internal-pcie", true},64{NULL, false}65};6667/*68* Bus interface definitions.69*/70static device_method_t al_pcib_methods[] = {71/* Device interface */72DEVMETHOD(device_probe, al_pcib_probe),73DEVMETHOD(device_attach, al_pcib_attach),7475DEVMETHOD_END76};7778DEFINE_CLASS_1(pcib, al_pcib_driver, al_pcib_methods,79sizeof(struct generic_pcie_fdt_softc), generic_pcie_fdt_driver);8081DRIVER_MODULE(alpine_pcib, simplebus, al_pcib_driver, 0, 0);82DRIVER_MODULE(alpine_pcib, ofwbus, al_pcib_driver, 0, 0);8384static int85al_pcib_probe(device_t dev)86{8788if (!ofw_bus_status_okay(dev))89return (ENXIO);9091if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)92return (ENXIO);9394device_set_desc(dev,95"Annapurna-Labs Integrated Internal PCI-E Controller");96return (BUS_PROBE_DEFAULT);97}9899static int100al_pcib_attach(device_t dev)101{102int rv;103104rv = pci_host_generic_fdt_attach(dev);105106/* Annapurna quirk: configure vendor-specific registers */107if (rv == 0)108al_pcib_fixup(dev);109110return (rv);111}112113static void114al_pcib_fixup(device_t dev)115{116uint32_t val;117uint16_t vid;118uint8_t hdrtype;119int bus, slot, func, maxfunc;120121/* Fixup is only needed on bus 0 */122bus = 0;123for (slot = 0; slot <= PCI_SLOTMAX; slot++) {124maxfunc = 0;125for (func = 0; func <= maxfunc; func++) {126hdrtype = PCIB_READ_CONFIG(dev, bus, slot, func,127PCIR_HDRTYPE, 1);128129if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)130continue;131132if (func == 0 && (hdrtype & PCIM_MFDEV) != 0)133maxfunc = PCI_FUNCMAX;134135vid = PCIB_READ_CONFIG(dev, bus, slot, func,136PCIR_VENDOR, 2);137if (vid == ANNAPURNA_VENDOR_ID) {138val = PCIB_READ_CONFIG(dev, bus, slot, func,139AL_PCI_AXI_CFG_AND_CTR_0, 4);140val |= PCIE_AXI_PF_AXI_ATTR_OVRD_FUNC_CTRL_2_PF_VEC_PH_VEC_OVRD_FROM_AXUSER_MASK;141PCIB_WRITE_CONFIG(dev, bus, slot, func,142AL_PCI_AXI_CFG_AND_CTR_0, val, 4);143144val = PCIB_READ_CONFIG(dev, bus, slot, func,145AL_PCI_APP_CONTROL, 4);146val &= ~0xffff;147val |= PCIE_AXI_PF_AXI_ATTR_OVRD_FUNC_CTRL_4_PF_VEC_MEM_ADDR54_63_SEL_TGTID_MASK;148PCIB_WRITE_CONFIG(dev, bus, slot, func,149AL_PCI_APP_CONTROL, val, 4);150}151}152}153}154155156