diff --git a/sys/conf/files.riscv b/sys/conf/files.riscv index cf7b58bfe4e3..d46d520bd0b7 100644 --- a/sys/conf/files.riscv +++ b/sys/conf/files.riscv @@ -1,74 +1,76 @@ # $FreeBSD$ cddl/dev/dtrace/riscv/dtrace_asm.S optional dtrace compile-with "${DTRACE_S}" cddl/dev/dtrace/riscv/dtrace_subr.c optional dtrace compile-with "${DTRACE_C}" cddl/dev/fbt/riscv/fbt_isa.c optional dtrace_fbt | dtraceall compile-with "${FBT_C}" crypto/des/des_enc.c optional netsmb dev/ofw/ofw_cpu.c optional fdt dev/ofw/ofw_pcib.c optional pci fdt +dev/pci/pci_dw.c optional pci fdt +dev/pci/pci_dw_if.m optional pci fdt dev/pci/pci_host_generic.c optional pci dev/pci/pci_host_generic_fdt.c optional pci fdt dev/uart/uart_cpu_fdt.c optional uart fdt dev/uart/uart_dev_lowrisc.c optional uart_lowrisc dev/xilinx/axi_quad_spi.c optional xilinx_spi dev/xilinx/axidma.c optional axidma xdma dev/xilinx/if_xae.c optional xae dev/xilinx/xlnx_pcib.c optional pci fdt xlnx_pcib kern/msi_if.m standard kern/pic_if.m standard kern/subr_devmap.c standard kern/subr_dummy_vdso_tc.c standard kern/subr_intr.c standard kern/subr_physmem.c standard libkern/bcmp.c standard libkern/bcopy.c standard libkern/ffs.c standard libkern/ffsl.c standard libkern/ffsll.c standard libkern/fls.c standard libkern/flsl.c standard libkern/flsll.c standard libkern/memcmp.c standard libkern/memset.c standard libkern/strlen.c standard riscv/riscv/autoconf.c standard riscv/riscv/bus_machdep.c standard riscv/riscv/bus_space_asm.S standard riscv/riscv/busdma_bounce.c standard riscv/riscv/busdma_machdep.c standard riscv/riscv/clock.c standard riscv/riscv/copyinout.S standard riscv/riscv/cpufunc_asm.S standard riscv/riscv/db_disasm.c optional ddb riscv/riscv/db_interface.c optional ddb riscv/riscv/db_trace.c optional ddb riscv/riscv/dump_machdep.c standard riscv/riscv/elf_machdep.c standard riscv/riscv/exception.S standard riscv/riscv/intr_machdep.c standard riscv/riscv/in_cksum.c optional inet | inet6 riscv/riscv/identcpu.c standard riscv/riscv/locore.S standard no-obj riscv/riscv/machdep.c standard riscv/riscv/minidump_machdep.c standard riscv/riscv/mp_machdep.c optional smp riscv/riscv/mem.c standard riscv/riscv/nexus.c standard riscv/riscv/ofw_machdep.c optional fdt riscv/riscv/plic.c standard riscv/riscv/pmap.c standard riscv/riscv/riscv_console.c optional rcons riscv/riscv/riscv_syscon.c optional ext_resources syscon riscv_syscon fdt riscv/riscv/sbi.c standard riscv/riscv/stack_machdep.c optional ddb | stack riscv/riscv/support.S standard riscv/riscv/swtch.S standard riscv/riscv/sys_machdep.c standard riscv/riscv/trap.c standard riscv/riscv/timer.c standard riscv/riscv/uio_machdep.c standard riscv/riscv/uma_machdep.c standard riscv/riscv/unwind.c optional ddb | kdtrace_hooks | stack riscv/riscv/vm_machdep.c standard # Zstd contrib/zstd/lib/freebsd/zstd_kfreebsd.c optional zstdio compile-with ${ZSTD_C} diff --git a/sys/riscv/conf/GENERIC b/sys/riscv/conf/GENERIC index 360d6b163d45..09882853ada2 100644 --- a/sys/riscv/conf/GENERIC +++ b/sys/riscv/conf/GENERIC @@ -1,182 +1,183 @@ # # GENERIC -- Generic kernel configuration file for FreeBSD/RISC-V # # For more information on this file, please read the config(5) manual page, # and/or the handbook section on Kernel Configuration Files: # # https://docs.freebsd.org/en/books/handbook/kernelconfig/#kernelconfig-config # # The handbook is also available locally in /usr/share/doc/handbook # if you've installed the doc distribution, otherwise always see the # FreeBSD World Wide Web server (https://www.FreeBSD.org/) for the # latest information. # # An exhaustive list of options and more detailed explanations of the # device lines is also present in the ../../conf/NOTES and NOTES files. # If you are in doubt as to the purpose or necessity of a line, check first # in NOTES. # # $FreeBSD$ cpu RISCV ident GENERIC makeoptions DEBUG=-g # Build kernel with gdb(1) debug symbols makeoptions WITH_CTF=1 # Run ctfconvert(1) for DTrace support options SCHED_ULE # ULE scheduler options PREEMPTION # Enable kernel thread preemption options VIMAGE # Subsystem virtualization, e.g. VNET options INET # InterNETworking options INET6 # IPv6 communications protocols options TCP_HHOOK # hhook(9) framework for TCP options IPSEC_SUPPORT # Allow kldload of ipsec and tcpmd5 options ROUTE_MPATH # Multipath routing support options TCP_OFFLOAD # TCP offload options SCTP_SUPPORT # Allow kldload of SCTP options FFS # Berkeley Fast Filesystem options SOFTUPDATES # Enable FFS soft updates support options UFS_ACL # Support for access control lists options UFS_DIRHASH # Improve performance on big directories options UFS_GJOURNAL # Enable gjournal-based UFS journaling options QUOTA # Enable disk quotas for UFS options NFSCL # Network Filesystem Client options NFSD # Network Filesystem Server options NFSLOCKD # Network Lock Manager options NFS_ROOT # NFS usable as /, requires NFSCL options MSDOSFS # MSDOS Filesystem options CD9660 # ISO 9660 Filesystem options PROCFS # Process filesystem (requires PSEUDOFS) options PSEUDOFS # Pseudo-filesystem framework options TMPFS # Efficient memory filesystem options GEOM_PART_GPT # GUID Partition Tables. options GEOM_RAID # Soft RAID functionality. options GEOM_LABEL # Provides labelization options SCSI_DELAY=5000 # Delay (in ms) before probing SCSI options KTRACE # ktrace(1) support options STACK # stack(9) support options SYSVSHM # SYSV-style shared memory options SYSVMSG # SYSV-style message queues options SYSVSEM # SYSV-style semaphores options _KPOSIX_PRIORITY_SCHEDULING # POSIX P1003_1B real-time extensions options PRINTF_BUFR_SIZE=128 # Prevent printf output being interspersed. options KBD_INSTALL_CDEV # install a CDEV entry in /dev # options HWPMC_HOOKS # Necessary kernel hooks for hwpmc(4) options AUDIT # Security event auditing options CAPABILITY_MODE # Capsicum capability mode options CAPABILITIES # Capsicum capabilities options MAC # TrustedBSD MAC Framework options KDTRACE_FRAME # Ensure frames are compiled in options KDTRACE_HOOKS # Kernel DTrace hooks options DDB_CTF # Kernel ELF linker loads CTF data options FPE # Floating-point extension support options RACCT # Resource accounting framework options RACCT_DEFAULT_TO_DISABLED # Set kern.racct.enable=0 by default options RCTL # Resource limits options SMP options INTRNG # RISC-V SBI console device rcons # EXT_RESOURCES pseudo devices options EXT_RESOURCES device clk device hwreset device syscon device syscon_power device riscv_syscon # Bus drivers device pci # VirtIO support device virtio # Generic VirtIO bus (required) device virtio_pci # VirtIO PCI device device vtnet # VirtIO Ethernet device device virtio_blk # VirtIO Block device device virtio_mmio # VirtIO MMIO bus # DTrace support # device dtrace # device dtrace_profile # device dtrace_sdt # device dtrace_fbt # device dtrace_systrace # device dtrace_prototype # device dtraceall # Serial (COM) ports device uart # Generic UART driver device uart_lowrisc # lowRISC UART driver device uart_ns8250 # ns8250-type UART driver # RTC device goldfish_rtc # QEMU RTC # Ethernet drivers device cgem # Cadence GEM Gigabit Ethernet device device miibus # MII bus support device xae # Xilinx AXI Ethernet MAC # DMA support device xdma # DMA interface device axidma # Xilinx AXI DMA Controller # GPIO device gpio # SPI device spibus device spigen # Uncomment for memory disk # options MD_ROOT # options MD_ROOT_SIZE=32768 # 32MB ram disk # makeoptions MFS_IMAGE=/path/to/img # options ROOTDEVNAME=\"ufs:/dev/md0\" # Uncomment for virtio block device # options ROOTDEVNAME=\"ufs:/dev/vtbd0\" # Debugging support. Always need this: options KDB # Enable kernel debugger support. options KDB_TRACE # Print a stack trace for a panic. # For full debugger support use (turn off in stable branch): options DDB # Support DDB. # options GDB # Support remote GDB. options DEADLKRES # Enable the deadlock resolver options INVARIANTS # Enable calls of extra sanity checking options INVARIANT_SUPPORT # Extra sanity checks of internal structures, required by INVARIANTS options WITNESS # Enable checks to detect deadlocks and cycles options WITNESS_SKIPSPIN # Don't run witness on spinlocks for speed options MALLOC_DEBUG_MAXZONES=8 # Separate malloc(9) zones # options EARLY_PRINTF options VERBOSE_SYSINIT=0 # Support debug.verbose_sysinit, off by default # Kernel dump features. options ZSTDIO # zstd-compressed kernel and user dumps # Pseudo devices. device crypto # core crypto support device loop # Network loopback device ether # Ethernet support device vlan # 802.1Q VLAN support device tuntap # Packet tunnel. device md # Memory "disks" device gif # IPv6 and IPv4 tunneling device firmware # firmware assist module # The `bpf' device enables the Berkeley Packet Filter. # Be aware of the administrative consequences of enabling this! # Note that 'bpf' is required for DHCP. device bpf # Berkeley packet filter # Flattened Device Tree options FDT makeoptions MODULES_EXTRA+="dtb/sifive" # SiFive device drivers +device fu740_pci_dw device sifive_gpio device sifive_spi include "../sifive/std.sifive" diff --git a/sys/riscv/sifive/files.sifive b/sys/riscv/sifive/files.sifive index 9ea71c4fcd27..f38bacb48e3b 100644 --- a/sys/riscv/sifive/files.sifive +++ b/sys/riscv/sifive/files.sifive @@ -1,7 +1,8 @@ # $FreeBSD$ riscv/sifive/fe310_aon.c optional fe310aon +riscv/sifive/fu740_pci_dw.c optional fu740_pci_dw pci fdt riscv/sifive/sifive_gpio.c optional sifive_gpio gpio riscv/sifive/sifive_prci.c standard riscv/sifive/sifive_spi.c optional sifive_spi spibus riscv/sifive/sifive_uart.c standard diff --git a/sys/riscv/sifive/fu740_pci_dw.c b/sys/riscv/sifive/fu740_pci_dw.c new file mode 100644 index 000000000000..fbb62cc44e65 --- /dev/null +++ b/sys/riscv/sifive/fu740_pci_dw.c @@ -0,0 +1,465 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright 2021 Jessica Clarke + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/* SiFive FU740 DesignWare PCIe driver */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pcib_if.h" +#include "pci_dw_if.h" + +#define FUDW_PHYS 2 +#define FUDW_LANES_PER_PHY 4 + +#define FUDW_MGMT_PERST_N 0x0 +#define FUDW_MGMT_LTSSM_EN 0x10 +#define FUDW_MGMT_HOLD_PHY_RST 0x18 +#define FUDW_MGMT_DEVICE_TYPE 0x708 +#define FUDW_MGMT_DEVICE_TYPE_RC 0x4 +#define FUDW_MGMT_PHY_CR_PARA_REG(_n, _r) \ + (0x860 + (_n) * 0x40 + FUDW_MGMT_PHY_CR_PARA_##_r) +#define FUDW_MGMT_PHY_CR_PARA_ADDR 0x0 +#define FUDW_MGMT_PHY_CR_PARA_READ_EN 0x10 +#define FUDW_MGMT_PHY_CR_PARA_READ_DATA 0x18 +#define FUDW_MGMT_PHY_CR_PARA_SEL 0x20 +#define FUDW_MGMT_PHY_CR_PARA_WRITE_DATA 0x28 +#define FUDW_MGMT_PHY_CR_PARA_WRITE_EN 0x30 +#define FUDW_MGMT_PHY_CR_PARA_ACK 0x38 + +#define FUDW_MGMT_PHY_LANE(_n) (0x1008 + (_n) * 0x100) +#define FUDW_MGMT_PHY_LANE_CDR_TRACK_EN (1 << 0) +#define FUDW_MGMT_PHY_LANE_LOS_THRESH (1 << 5) +#define FUDW_MGMT_PHY_LANE_TERM_EN (1 << 9) +#define FUDW_MGMT_PHY_LANE_TERM_ACDC (1 << 10) +#define FUDW_MGMT_PHY_LANE_EN (1 << 11) +#define FUDW_MGMT_PHY_LANE_INIT \ + (FUDW_MGMT_PHY_LANE_CDR_TRACK_EN | FUDW_MGMT_PHY_LANE_LOS_THRESH | \ + FUDW_MGMT_PHY_LANE_TERM_EN | FUDW_MGMT_PHY_LANE_TERM_ACDC | \ + FUDW_MGMT_PHY_LANE_EN) + +#define FUDW_DBI_PORT_DBG1 0x72c +#define FUDW_DBI_PORT_DBG1_LINK_UP (1 << 4) +#define FUDW_DBI_PORT_DBG1_LINK_IN_TRAINING (1 << 29) + +struct fupci_softc { + struct pci_dw_softc dw_sc; + device_t dev; + struct resource *mgmt_res; + gpio_pin_t porst_pin; + gpio_pin_t pwren_pin; + clk_t pcie_aux_clk; + hwreset_t pcie_aux_rst; +}; + +#define FUDW_MGMT_READ(_sc, _o) bus_read_4((_sc)->mgmt_res, (_o)) +#define FUDW_MGMT_WRITE(_sc, _o, _v) bus_write_4((_sc)->mgmt_res, (_o), (_v)) + +static struct ofw_compat_data compat_data[] = { + { "sifive,fu740-pcie", 1 }, + { NULL, 0 }, +}; + +/* Currently unused; included for completeness */ +static int __unused +fupci_phy_read(struct fupci_softc *sc, int phy, uint32_t reg, uint32_t *val) +{ + unsigned timeout; + uint32_t ack; + + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ADDR), reg); + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, READ_EN), 1); + + timeout = 10; + do { + ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK)); + if (ack != 0) + break; + DELAY(10); + } while (--timeout > 0); + + if (timeout == 0) { + device_printf(sc->dev, "Timeout waiting for read ACK\n"); + return (ETIMEDOUT); + } + + *val = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, READ_DATA)); + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, READ_EN), 0); + + timeout = 10; + do { + ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK)); + if (ack == 0) + break; + DELAY(10); + } while (--timeout > 0); + + if (timeout == 0) { + device_printf(sc->dev, "Timeout waiting for read un-ACK\n"); + return (ETIMEDOUT); + } + + return (0); +} + +static int +fupci_phy_write(struct fupci_softc *sc, int phy, uint32_t reg, uint32_t val) +{ + unsigned timeout; + uint32_t ack; + + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ADDR), reg); + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, WRITE_DATA), val); + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, WRITE_EN), 1); + + timeout = 10; + do { + ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK)); + if (ack != 0) + break; + DELAY(10); + } while (--timeout > 0); + + if (timeout == 0) { + device_printf(sc->dev, "Timeout waiting for write ACK\n"); + return (ETIMEDOUT); + } + + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, WRITE_EN), 0); + + timeout = 10; + do { + ack = FUDW_MGMT_READ(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, ACK)); + if (ack == 0) + break; + DELAY(10); + } while (--timeout > 0); + + if (timeout == 0) { + device_printf(sc->dev, "Timeout waiting for write un-ACK\n"); + return (ETIMEDOUT); + } + + return (0); +} + +static int +fupci_phy_init(struct fupci_softc *sc) +{ + device_t dev; + int error, phy, lane; + + dev = sc->dev; + + /* Assert core power-on reset (active low) */ + error = gpio_pin_set_active(sc->porst_pin, false); + if (error != 0) { + device_printf(dev, "Cannot assert power-on reset: %d\n", + error); + return (error); + } + + /* Assert PERST_N */ + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PERST_N, 0); + + /* Enable power */ + error = gpio_pin_set_active(sc->pwren_pin, true); + if (error != 0) { + device_printf(dev, "Cannot enable power: %d\n", error); + return (error); + } + + /* Hold PERST for 100ms as per the PCIe spec */ + DELAY(100); + + /* Deassert PERST_N */ + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PERST_N, 1); + + /* Deassert core power-on reset (active low) */ + error = gpio_pin_set_active(sc->porst_pin, true); + if (error != 0) { + device_printf(dev, "Cannot deassert power-on reset: %d\n", + error); + return (error); + } + + /* Enable the aux clock */ + error = clk_enable(sc->pcie_aux_clk); + if (error != 0) { + device_printf(dev, "Cannot enable aux clock: %d\n", error); + return (error); + } + + /* Hold LTSSM in reset whilst initialising the PHYs */ + FUDW_MGMT_WRITE(sc, FUDW_MGMT_HOLD_PHY_RST, 1); + + /* Deassert the aux reset */ + error = hwreset_deassert(sc->pcie_aux_rst); + if (error != 0) { + device_printf(dev, "Cannot deassert aux reset: %d\n", error); + return (error); + } + + /* Enable control register interface */ + for (phy = 0; phy < FUDW_PHYS; ++phy) + FUDW_MGMT_WRITE(sc, FUDW_MGMT_PHY_CR_PARA_REG(phy, SEL), 1); + + /* Wait for enable to take effect */ + DELAY(1); + + /* Initialise lane configuration */ + for (phy = 0; phy < FUDW_PHYS; ++phy) { + for (lane = 0; lane < FUDW_LANES_PER_PHY; ++lane) + fupci_phy_write(sc, phy, FUDW_MGMT_PHY_LANE(lane), + FUDW_MGMT_PHY_LANE_INIT); + } + + /* Disable the aux clock whilst taking the LTSSM out of reset */ + error = clk_disable(sc->pcie_aux_clk); + if (error != 0) { + device_printf(dev, "Cannot disable aux clock: %d\n", error); + return (error); + } + + /* Take LTSSM out of reset */ + FUDW_MGMT_WRITE(sc, FUDW_MGMT_HOLD_PHY_RST, 0); + + /* Enable the aux clock again */ + error = clk_enable(sc->pcie_aux_clk); + if (error != 0) { + device_printf(dev, "Cannot re-enable aux clock: %d\n", error); + return (error); + } + + /* Put the controller in Root Complex mode */ + FUDW_MGMT_WRITE(sc, FUDW_MGMT_DEVICE_TYPE, FUDW_MGMT_DEVICE_TYPE_RC); + + return (0); +} + +static void +fupci_dbi_protect(struct fupci_softc *sc, bool protect) +{ + uint32_t reg; + + reg = pci_dw_dbi_rd4(sc->dev, DW_MISC_CONTROL_1); + if (protect) + reg &= ~DBI_RO_WR_EN; + else + reg |= DBI_RO_WR_EN; + pci_dw_dbi_wr4(sc->dev, DW_MISC_CONTROL_1, reg); +} + +static int +fupci_init(struct fupci_softc *sc) +{ + /* Enable 32-bit I/O window */ + fupci_dbi_protect(sc, false); + pci_dw_dbi_wr2(sc->dev, PCIR_IOBASEL_1, + (PCIM_BRIO_32 << 8) | PCIM_BRIO_32); + fupci_dbi_protect(sc, true); + + /* Enable LTSSM */ + FUDW_MGMT_WRITE(sc, FUDW_MGMT_LTSSM_EN, 1); + + return (0); +} + +static int +fupci_probe(device_t dev) +{ + if (!ofw_bus_status_okay(dev)) + return (ENXIO); + + if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) + return (ENXIO); + + device_set_desc(dev, "SiFive FU740 PCIe Controller"); + + return (BUS_PROBE_DEFAULT); +} + +static int +fupci_attach(device_t dev) +{ + struct fupci_softc *sc; + phandle_t node; + int error, rid; + + sc = device_get_softc(dev); + node = ofw_bus_get_node(dev); + sc->dev = dev; + + rid = 0; + error = ofw_bus_find_string_index(node, "reg-names", "dbi", &rid); + if (error != 0) { + device_printf(dev, "Cannot get DBI memory: %d\n", error); + goto fail; + } + sc->dw_sc.dbi_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, + RF_ACTIVE); + if (sc->dw_sc.dbi_res == NULL) { + device_printf(dev, "Cannot allocate DBI memory\n"); + error = ENXIO; + goto fail; + } + + rid = 0; + error = ofw_bus_find_string_index(node, "reg-names", "mgmt", &rid); + if (error != 0) { + device_printf(dev, "Cannot get management space memory: %d\n", + error); + goto fail; + } + sc->mgmt_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, + RF_ACTIVE); + if (sc->mgmt_res == NULL) { + device_printf(dev, "Cannot allocate management space memory\n"); + error = ENXIO; + goto fail; + } + + error = gpio_pin_get_by_ofw_property(dev, node, "reset-gpios", + &sc->porst_pin); + /* Old U-Boot device tree uses perstn-gpios */ + if (error == ENOENT) + error = gpio_pin_get_by_ofw_property(dev, node, "perstn-gpios", + &sc->porst_pin); + if (error != 0) { + device_printf(dev, "Cannot get power-on reset GPIO: %d\n", + error); + goto fail; + } + error = gpio_pin_setflags(sc->porst_pin, GPIO_PIN_OUTPUT); + if (error != 0) { + device_printf(dev, "Cannot configure power-on reset GPIO: %d\n", + error); + goto fail; + } + + error = gpio_pin_get_by_ofw_property(dev, node, "pwren-gpios", + &sc->pwren_pin); + if (error != 0) { + device_printf(dev, "Cannot get power enable GPIO: %d\n", + error); + goto fail; + } + error = gpio_pin_setflags(sc->pwren_pin, GPIO_PIN_OUTPUT); + if (error != 0) { + device_printf(dev, "Cannot configure power enable GPIO: %d\n", + error); + goto fail; + } + + error = clk_get_by_ofw_name(dev, node, "pcie_aux", &sc->pcie_aux_clk); + /* Old U-Boot device tree uses pcieaux */ + if (error == ENOENT) + error = clk_get_by_ofw_name(dev, node, "pcieaux", + &sc->pcie_aux_clk); + if (error != 0) { + device_printf(dev, "Cannot get aux clock: %d\n", error); + goto fail; + } + + error = hwreset_get_by_ofw_idx(dev, node, 0, &sc->pcie_aux_rst); + if (error != 0) { + device_printf(dev, "Cannot get aux reset: %d\n", error); + goto fail; + } + + error = fupci_phy_init(sc); + if (error != 0) + goto fail; + + error = pci_dw_init(dev); + if (error != 0) + goto fail; + + error = fupci_init(sc); + if (error != 0) + goto fail; + + return (bus_generic_attach(dev)); + +fail: + /* XXX Cleanup */ + return (error); +} + +static int +fupci_get_link(device_t dev, bool *status) +{ + uint32_t reg; + + reg = pci_dw_dbi_rd4(dev, FUDW_DBI_PORT_DBG1); + *status = (reg & FUDW_DBI_PORT_DBG1_LINK_UP) != 0 && + (reg & FUDW_DBI_PORT_DBG1_LINK_IN_TRAINING) == 0; + + return (0); +} + +static device_method_t fupci_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, fupci_probe), + DEVMETHOD(device_attach, fupci_attach), + + /* PCI DW interface */ + DEVMETHOD(pci_dw_get_link, fupci_get_link), + + DEVMETHOD_END +}; + +DEFINE_CLASS_1(pcib, fupci_driver, fupci_methods, + sizeof(struct fupci_softc), pci_dw_driver); +static devclass_t fupci_devclass; +DRIVER_MODULE(fu740_pci_dw, simplebus, fupci_driver, fupci_devclass, + NULL, NULL);