1dfd1d0fcSMichal Meloun /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3dfd1d0fcSMichal Meloun * 4dfd1d0fcSMichal Meloun * Copyright (c) 2019 Michal Meloun <mmel@FreeBSD.org> 5dfd1d0fcSMichal Meloun * 6dfd1d0fcSMichal Meloun * Redistribution and use in source and binary forms, with or without 7dfd1d0fcSMichal Meloun * modification, are permitted provided that the following conditions 8dfd1d0fcSMichal Meloun * are met: 9dfd1d0fcSMichal Meloun * 1. Redistributions of source code must retain the above copyright 10dfd1d0fcSMichal Meloun * notice, this list of conditions and the following disclaimer. 11dfd1d0fcSMichal Meloun * 2. Redistributions in binary form must reproduce the above copyright 12dfd1d0fcSMichal Meloun * notice, this list of conditions and the following disclaimer in the 13dfd1d0fcSMichal Meloun * documentation and/or other materials provided with the distribution. 14dfd1d0fcSMichal Meloun * 15dfd1d0fcSMichal Meloun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16dfd1d0fcSMichal Meloun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17dfd1d0fcSMichal Meloun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18dfd1d0fcSMichal Meloun * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19dfd1d0fcSMichal Meloun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20dfd1d0fcSMichal Meloun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21dfd1d0fcSMichal Meloun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22dfd1d0fcSMichal Meloun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23dfd1d0fcSMichal Meloun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24dfd1d0fcSMichal Meloun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25dfd1d0fcSMichal Meloun * SUCH DAMAGE. 26dfd1d0fcSMichal Meloun * 27dfd1d0fcSMichal Meloun */ 28dfd1d0fcSMichal Meloun 29dfd1d0fcSMichal Meloun /* Rockchip PCIe controller driver */ 30dfd1d0fcSMichal Meloun 31dfd1d0fcSMichal Meloun #include <sys/param.h> 32dfd1d0fcSMichal Meloun #include <sys/systm.h> 33dfd1d0fcSMichal Meloun #include <sys/bus.h> 34dfd1d0fcSMichal Meloun #include <sys/gpio.h> 35dfd1d0fcSMichal Meloun #include <sys/proc.h> 36dfd1d0fcSMichal Meloun #include <sys/kernel.h> 37dfd1d0fcSMichal Meloun #include <sys/lock.h> 38dfd1d0fcSMichal Meloun #include <sys/malloc.h> 39dfd1d0fcSMichal Meloun #include <sys/module.h> 40dfd1d0fcSMichal Meloun #include <sys/mutex.h> 41dfd1d0fcSMichal Meloun #include <sys/rman.h> 42dfd1d0fcSMichal Meloun 43dfd1d0fcSMichal Meloun #include <machine/bus.h> 44dfd1d0fcSMichal Meloun #include <machine/intr.h> 45dfd1d0fcSMichal Meloun #include <machine/resource.h> 46dfd1d0fcSMichal Meloun 47be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h> 481f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h> 49950a6087SEmmanuel Vadot #include <dev/phy/phy.h> 50b2f0caf1SEmmanuel Vadot #include <dev/regulator/regulator.h> 51dfd1d0fcSMichal Meloun #include <dev/gpio/gpiobusvar.h> 52dfd1d0fcSMichal Meloun #include <dev/ofw/ofw_bus.h> 53dfd1d0fcSMichal Meloun #include <dev/ofw/ofw_bus_subr.h> 54dfd1d0fcSMichal Meloun #include <dev/ofw/ofw_pci.h> 55dfd1d0fcSMichal Meloun #include <dev/ofw/ofwpci.h> 56dfd1d0fcSMichal Meloun #include <dev/pci/pcivar.h> 57dfd1d0fcSMichal Meloun #include <dev/pci/pcireg.h> 58dfd1d0fcSMichal Meloun #include <dev/pci/pcib_private.h> 59dfd1d0fcSMichal Meloun 60dfd1d0fcSMichal Meloun #include <dev/ofw/ofw_bus.h> 61dfd1d0fcSMichal Meloun 62dfd1d0fcSMichal Meloun #include "pcib_if.h" 63dfd1d0fcSMichal Meloun 64dfd1d0fcSMichal Meloun #define ATU_CFG_BUS(x) (((x) & 0x0ff) << 20) 65dfd1d0fcSMichal Meloun #define ATU_CFG_SLOT(x) (((x) & 0x01f) << 15) 66dfd1d0fcSMichal Meloun #define ATU_CFG_FUNC(x) (((x) & 0x007) << 12) 67dfd1d0fcSMichal Meloun #define ATU_CFG_REG(x) (((x) & 0xfff) << 0) 68dfd1d0fcSMichal Meloun 69dfd1d0fcSMichal Meloun #define ATU_TYPE_MEM 0x2 70dfd1d0fcSMichal Meloun #define ATU_TYPE_IO 0x6 71dfd1d0fcSMichal Meloun #define ATU_TYPE_CFG0 0xA 72dfd1d0fcSMichal Meloun #define ATU_TYPE_CFG1 0xB 73dfd1d0fcSMichal Meloun #define ATY_TYPE_NOR_MSG 0xC 74dfd1d0fcSMichal Meloun 75dfd1d0fcSMichal Meloun #define ATU_OB_REGIONS 33 76dfd1d0fcSMichal Meloun #define ATU_OB_REGION_SHIFT 20 77dfd1d0fcSMichal Meloun #define ATU_OB_REGION_SIZE (1 << ATU_OB_REGION_SHIFT) 78dfd1d0fcSMichal Meloun #define ATU_OB_REGION_0_SIZE (( ATU_OB_REGIONS - 1) * ATU_OB_REGION_SIZE) 79dfd1d0fcSMichal Meloun 80dfd1d0fcSMichal Meloun #define ATU_IB_REGIONS 3 81dfd1d0fcSMichal Meloun 82dfd1d0fcSMichal Meloun #define PCIE_CLIENT_BASIC_STRAP_CONF 0x000000 83dfd1d0fcSMichal Meloun #define STRAP_CONF_GEN_2 (1 << 7) 84dfd1d0fcSMichal Meloun #define STRAP_CONF_MODE_RC (1 << 6) 85dfd1d0fcSMichal Meloun #define STRAP_CONF_LANES(n) ((((n) / 2) & 0x3) << 4) 86dfd1d0fcSMichal Meloun #define STRAP_CONF_ARI_EN (1 << 3) 87dfd1d0fcSMichal Meloun #define STRAP_CONF_SR_IOV_EN (1 << 2) 88dfd1d0fcSMichal Meloun #define STRAP_CONF_LINK_TRAIN_EN (1 << 1) 89dfd1d0fcSMichal Meloun #define STRAP_CONF_CONF_EN (1 << 0) 90dfd1d0fcSMichal Meloun #define PCIE_CLIENT_HOT_RESET_CTRL 0x000018 91dfd1d0fcSMichal Meloun #define HOT_RESET_CTRL_LINK_DOWN_RESET (1 << 1) 92dfd1d0fcSMichal Meloun #define HOT_RESET_CTRL_HOT_RESET_IN (1 << 0) 93dfd1d0fcSMichal Meloun #define PCIE_CLIENT_BASIC_STATUS0 0x000044 94dfd1d0fcSMichal Meloun #define PCIE_CLIENT_BASIC_STATUS1 0x000048 95dfd1d0fcSMichal Meloun #define STATUS1_LINK_ST_GET(x) (((x) >> 20) & 0x3) 96dfd1d0fcSMichal Meloun #define STATUS1_LINK_ST_UP 3 97dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_MASK 0x00004C 98dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_STATUS 0x000050 99dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_LEGACY_DONE (1 << 15) 100dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_MSG (1 << 14) 101dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_HOT_RST (1 << 13) 102dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_DPA (1 << 12) 103dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_FATAL_ERR (1 << 11) 104dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_NFATAL_ERR (1 << 10) 105dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_CORR_ERR (1 << 9) 106dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_INTD (1 << 8) 107dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_INTC (1 << 7) 108dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_INTB (1 << 6) 109dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_INTA (1 << 5) 110dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_LOCAL (1 << 4) 111dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_UDMA (1 << 3) 112dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_PHY (1 << 2) 113dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_HOT_PLUG (1 << 1) 114dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_PWR_STCG (1 << 0) 115dfd1d0fcSMichal Meloun #define PCIE_CLIENT_INT_LEGACY (PCIE_CLIENT_INT_INTA | \ 116dfd1d0fcSMichal Meloun PCIE_CLIENT_INT_INTB | \ 117dfd1d0fcSMichal Meloun PCIE_CLIENT_INT_INTC | \ 118dfd1d0fcSMichal Meloun PCIE_CLIENT_INT_INTD) 119dfd1d0fcSMichal Meloun 120dfd1d0fcSMichal Meloun #define PCIE_CORE_CTRL0 0x900000 121dfd1d0fcSMichal Meloun #define CORE_CTRL_LANES_GET(x) (((x) >> 20) & 0x3) 122dfd1d0fcSMichal Meloun #define PCIE_CORE_CTRL1 0x900004 123dfd1d0fcSMichal Meloun #define PCIE_CORE_CONFIG_VENDOR 0x900044 124dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_STATUS 0x90020c 125dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_PRFPE (1 << 0) 126dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_CRFPE (1 << 1) 127dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_RRPE (1 << 2) 128dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_PRFO (1 << 3) 129dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_CRFO (1 << 4) 130dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_RT (1 << 5) 131dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_RTR (1 << 6) 132dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_PE (1 << 7) 133dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_MTR (1 << 8) 134dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_UCR (1 << 9) 135dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_FCE (1 << 10) 136dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_CT (1 << 11) 137dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_UTC (1 << 18) 138dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_MMVC (1 << 19) 139dfd1d0fcSMichal Meloun #define PCIE_CORE_INT_MASK 0x900210 140dfd1d0fcSMichal Meloun #define PCIE_CORE_PHY_FUNC_CONF 0x9002C0 141dfd1d0fcSMichal Meloun #define PCIE_CORE_RC_BAR_CONF 0x900300 142dfd1d0fcSMichal Meloun 143dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_STD_BASE 0x800000 144dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_PRIV_BASE 0xA00000 145dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_DCSR 0xA000C8 146dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_DCSR_MPS_MASK (0x7 << 5) 147dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_DCSR_MPS_128 (0 << 5) 148dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_DCSR_MPS_256 (1 << 5) 149dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_LINK_CAP 0xA00CC 150dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_LINK_CAP_L0S (1 << 10) 151dfd1d0fcSMichal Meloun 152dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_LCS 0xA000D0 153dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_THP_CAP 0xA00274 154dfd1d0fcSMichal Meloun #define PCIE_RC_CONFIG_THP_CAP_NEXT_MASK 0xFFF00000 155dfd1d0fcSMichal Meloun 156dfd1d0fcSMichal Meloun #define PCIE_CORE_OB_ADDR0(n) (0xC00000 + 0x20 * (n) + 0x00) 157dfd1d0fcSMichal Meloun #define PCIE_CORE_OB_ADDR1(n) (0xC00000 + 0x20 * (n) + 0x04) 158dfd1d0fcSMichal Meloun #define PCIE_CORE_OB_DESC0(n) (0xC00000 + 0x20 * (n) + 0x08) 159dfd1d0fcSMichal Meloun #define PCIE_CORE_OB_DESC1(n) (0xC00000 + 0x20 * (n) + 0x0C) 160dfd1d0fcSMichal Meloun #define PCIE_CORE_OB_DESC2(n) (0xC00000 + 0x20 * (n) + 0x10) 161dfd1d0fcSMichal Meloun #define PCIE_CORE_OB_DESC3(n) (0xC00000 + 0x20 * (n) + 0x14) 162dfd1d0fcSMichal Meloun 163dfd1d0fcSMichal Meloun #define PCIE_CORE_IB_ADDR0(n) (0xC00800 + 0x8 * (n) + 0x00) 164dfd1d0fcSMichal Meloun #define PCIE_CORE_IB_ADDR1(n) (0xC00800 + 0x8 * (n) + 0x04) 165dfd1d0fcSMichal Meloun 166dfd1d0fcSMichal Meloun #define PRIV_CFG_RD4(sc, reg) \ 167dfd1d0fcSMichal Meloun (uint32_t)rk_pcie_local_cfg_read(sc, true, reg, 4) 168dfd1d0fcSMichal Meloun #define PRIV_CFG_RD2(sc, reg) \ 169dfd1d0fcSMichal Meloun (uint16_t)rk_pcie_local_cfg_read(sc, true, reg, 2) 170dfd1d0fcSMichal Meloun #define PRIV_CFG_RD1(sc, reg) \ 171dfd1d0fcSMichal Meloun (uint8_t)rk_pcie_local_cfg_read(sc, true, reg, 1) 172dfd1d0fcSMichal Meloun #define PRIV_CFG_WR4(sc, reg, val) \ 173dfd1d0fcSMichal Meloun rk_pcie_local_cfg_write(sc, true, reg, val, 4) 174dfd1d0fcSMichal Meloun #define PRIV_CFG_WR2(sc, reg, val) \ 175dfd1d0fcSMichal Meloun rk_pcie_local_cfg_write(sc, true, reg, val, 2) 176dfd1d0fcSMichal Meloun #define PRIV_CFG_WR1(sc, reg, val) \ 177dfd1d0fcSMichal Meloun rk_pcie_local_cfg_write(sc, true, reg, val, 1) 178dfd1d0fcSMichal Meloun 179dfd1d0fcSMichal Meloun #define APB_WR4(_sc, _r, _v) bus_write_4((_sc)->apb_mem_res, (_r), (_v)) 180dfd1d0fcSMichal Meloun #define APB_RD4(_sc, _r) bus_read_4((_sc)->apb_mem_res, (_r)) 181dfd1d0fcSMichal Meloun 182dfd1d0fcSMichal Meloun #define MAX_LANES 4 183dfd1d0fcSMichal Meloun 184dfd1d0fcSMichal Meloun #define RK_PCIE_ENABLE_MSI 185dfd1d0fcSMichal Meloun #define RK_PCIE_ENABLE_MSIX 186dfd1d0fcSMichal Meloun 187dfd1d0fcSMichal Meloun struct rk_pcie_softc { 188dfd1d0fcSMichal Meloun struct ofw_pci_softc ofw_pci; /* Must be first */ 189dfd1d0fcSMichal Meloun 190dfd1d0fcSMichal Meloun struct resource *axi_mem_res; 191dfd1d0fcSMichal Meloun struct resource *apb_mem_res; 192dfd1d0fcSMichal Meloun struct resource *client_irq_res; 193dfd1d0fcSMichal Meloun struct resource *legacy_irq_res; 194dfd1d0fcSMichal Meloun struct resource *sys_irq_res; 195dfd1d0fcSMichal Meloun void *client_irq_cookie; 196dfd1d0fcSMichal Meloun void *legacy_irq_cookie; 197dfd1d0fcSMichal Meloun void *sys_irq_cookie; 198dfd1d0fcSMichal Meloun 199dfd1d0fcSMichal Meloun device_t dev; 200dfd1d0fcSMichal Meloun phandle_t node; 201dfd1d0fcSMichal Meloun struct mtx mtx; 202dfd1d0fcSMichal Meloun 203dfd1d0fcSMichal Meloun struct ofw_pci_range mem_range; 204dfd1d0fcSMichal Meloun struct ofw_pci_range pref_mem_range; 205dfd1d0fcSMichal Meloun struct ofw_pci_range io_range; 206dfd1d0fcSMichal Meloun 207dfd1d0fcSMichal Meloun bool coherent; 208dfd1d0fcSMichal Meloun bus_dma_tag_t dmat; 209dfd1d0fcSMichal Meloun 210dfd1d0fcSMichal Meloun int num_lanes; 211dfd1d0fcSMichal Meloun bool link_is_gen2; 212dfd1d0fcSMichal Meloun bool no_l0s; 213dfd1d0fcSMichal Meloun 214dfd1d0fcSMichal Meloun u_int bus_start; 215dfd1d0fcSMichal Meloun u_int bus_end; 216dfd1d0fcSMichal Meloun u_int root_bus; 217dfd1d0fcSMichal Meloun u_int sub_bus; 218dfd1d0fcSMichal Meloun 219dfd1d0fcSMichal Meloun regulator_t supply_12v; 220dfd1d0fcSMichal Meloun regulator_t supply_3v3; 221dfd1d0fcSMichal Meloun regulator_t supply_1v8; 222dfd1d0fcSMichal Meloun regulator_t supply_0v9; 223dfd1d0fcSMichal Meloun hwreset_t hwreset_core; 224dfd1d0fcSMichal Meloun hwreset_t hwreset_mgmt; 225dfd1d0fcSMichal Meloun hwreset_t hwreset_mgmt_sticky; 226dfd1d0fcSMichal Meloun hwreset_t hwreset_pipe; 227dfd1d0fcSMichal Meloun hwreset_t hwreset_pm; 228dfd1d0fcSMichal Meloun hwreset_t hwreset_aclk; 229dfd1d0fcSMichal Meloun hwreset_t hwreset_pclk; 230dfd1d0fcSMichal Meloun clk_t clk_aclk; 231dfd1d0fcSMichal Meloun clk_t clk_aclk_perf; 232dfd1d0fcSMichal Meloun clk_t clk_hclk; 233dfd1d0fcSMichal Meloun clk_t clk_pm; 234dfd1d0fcSMichal Meloun phy_t phys[MAX_LANES]; 235dfd1d0fcSMichal Meloun gpio_pin_t gpio_ep; 236dfd1d0fcSMichal Meloun }; 237dfd1d0fcSMichal Meloun 238dfd1d0fcSMichal Meloun /* Compatible devices. */ 239dfd1d0fcSMichal Meloun static struct ofw_compat_data compat_data[] = { 240dfd1d0fcSMichal Meloun {"rockchip,rk3399-pcie", 1}, 241dfd1d0fcSMichal Meloun {NULL, 0}, 242dfd1d0fcSMichal Meloun }; 243dfd1d0fcSMichal Meloun 244dfd1d0fcSMichal Meloun static uint32_t 245dfd1d0fcSMichal Meloun rk_pcie_local_cfg_read(struct rk_pcie_softc *sc, bool priv, u_int reg, 246dfd1d0fcSMichal Meloun int bytes) 247dfd1d0fcSMichal Meloun { 248dfd1d0fcSMichal Meloun uint32_t val; 249dfd1d0fcSMichal Meloun bus_addr_t base; 250dfd1d0fcSMichal Meloun 251dfd1d0fcSMichal Meloun if (priv) 252dfd1d0fcSMichal Meloun base = PCIE_RC_CONFIG_PRIV_BASE; 253dfd1d0fcSMichal Meloun else 254dfd1d0fcSMichal Meloun base = PCIE_RC_CONFIG_STD_BASE; 255dfd1d0fcSMichal Meloun 256dfd1d0fcSMichal Meloun switch (bytes) { 257dfd1d0fcSMichal Meloun case 4: 258dfd1d0fcSMichal Meloun val = bus_read_4(sc->apb_mem_res, base + reg); 259dfd1d0fcSMichal Meloun break; 260dfd1d0fcSMichal Meloun case 2: 261dfd1d0fcSMichal Meloun val = bus_read_2(sc->apb_mem_res, base + reg); 262dfd1d0fcSMichal Meloun break; 263dfd1d0fcSMichal Meloun case 1: 264dfd1d0fcSMichal Meloun val = bus_read_1(sc->apb_mem_res, base + reg); 265dfd1d0fcSMichal Meloun break; 266dfd1d0fcSMichal Meloun default: 267dfd1d0fcSMichal Meloun val = 0xFFFFFFFF; 268dfd1d0fcSMichal Meloun } 269dfd1d0fcSMichal Meloun return (val); 270dfd1d0fcSMichal Meloun } 271dfd1d0fcSMichal Meloun 272dfd1d0fcSMichal Meloun static void 273dfd1d0fcSMichal Meloun rk_pcie_local_cfg_write(struct rk_pcie_softc *sc, bool priv, u_int reg, 274dfd1d0fcSMichal Meloun uint32_t val, int bytes) 275dfd1d0fcSMichal Meloun { 276dfd1d0fcSMichal Meloun uint32_t val2; 277dfd1d0fcSMichal Meloun bus_addr_t base; 278dfd1d0fcSMichal Meloun 279dfd1d0fcSMichal Meloun if (priv) 280dfd1d0fcSMichal Meloun base = PCIE_RC_CONFIG_PRIV_BASE; 281dfd1d0fcSMichal Meloun else 282dfd1d0fcSMichal Meloun base = PCIE_RC_CONFIG_STD_BASE; 283dfd1d0fcSMichal Meloun 284dfd1d0fcSMichal Meloun switch (bytes) { 285dfd1d0fcSMichal Meloun case 4: 286dfd1d0fcSMichal Meloun bus_write_4(sc->apb_mem_res, base + reg, val); 287dfd1d0fcSMichal Meloun break; 288dfd1d0fcSMichal Meloun case 2: 289dfd1d0fcSMichal Meloun val2 = bus_read_4(sc->apb_mem_res, base + (reg & ~3)); 290dfd1d0fcSMichal Meloun val2 &= ~(0xffff << ((reg & 3) << 3)); 291dfd1d0fcSMichal Meloun val2 |= ((val & 0xffff) << ((reg & 3) << 3)); 292dfd1d0fcSMichal Meloun bus_write_4(sc->apb_mem_res, base + (reg & ~3), val2); 293dfd1d0fcSMichal Meloun break; 294dfd1d0fcSMichal Meloun case 1: 295dfd1d0fcSMichal Meloun val2 = bus_read_4(sc->apb_mem_res, base + (reg & ~3)); 296dfd1d0fcSMichal Meloun val2 &= ~(0xff << ((reg & 3) << 3)); 297dfd1d0fcSMichal Meloun val2 |= ((val & 0xff) << ((reg & 3) << 3)); 298dfd1d0fcSMichal Meloun bus_write_4(sc->apb_mem_res, base + (reg & ~3), val2); 299dfd1d0fcSMichal Meloun break; 300dfd1d0fcSMichal Meloun } 301dfd1d0fcSMichal Meloun } 302dfd1d0fcSMichal Meloun 303dfd1d0fcSMichal Meloun static bool 304dfd1d0fcSMichal Meloun rk_pcie_check_dev(struct rk_pcie_softc *sc, u_int bus, u_int slot, u_int func, 305dfd1d0fcSMichal Meloun u_int reg) 306dfd1d0fcSMichal Meloun { 307dfd1d0fcSMichal Meloun uint32_t val; 308dfd1d0fcSMichal Meloun 309dfd1d0fcSMichal Meloun if (bus < sc->bus_start || bus > sc->bus_end || slot > PCI_SLOTMAX || 310b8bfffc1SMichal Meloun func > PCI_FUNCMAX || reg > PCIE_REGMAX) 311dfd1d0fcSMichal Meloun return (false); 312dfd1d0fcSMichal Meloun 313dfd1d0fcSMichal Meloun if (bus == sc->root_bus) { 314dfd1d0fcSMichal Meloun /* we have only 1 device with 1 function root port */ 315dfd1d0fcSMichal Meloun if (slot > 0 || func > 0) 316dfd1d0fcSMichal Meloun return (false); 317dfd1d0fcSMichal Meloun return (true); 318dfd1d0fcSMichal Meloun } 319dfd1d0fcSMichal Meloun 320dfd1d0fcSMichal Meloun /* link is needed for accessing non-root busses */ 321dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_CLIENT_BASIC_STATUS1); 322dfd1d0fcSMichal Meloun if (STATUS1_LINK_ST_GET(val) != STATUS1_LINK_ST_UP) 323dfd1d0fcSMichal Meloun return (false); 324dfd1d0fcSMichal Meloun 325b8bfffc1SMichal Meloun /* only one device can be on first subordinate bus */ 326b8bfffc1SMichal Meloun if (bus == sc->sub_bus && slot != 0 ) 327dfd1d0fcSMichal Meloun return (false); 328dfd1d0fcSMichal Meloun return (true); 329dfd1d0fcSMichal Meloun } 330dfd1d0fcSMichal Meloun 331dfd1d0fcSMichal Meloun static void 332dfd1d0fcSMichal Meloun rk_pcie_map_out_atu(struct rk_pcie_softc *sc, int idx, int type, 333dfd1d0fcSMichal Meloun int num_bits, uint64_t pa) 334dfd1d0fcSMichal Meloun { 335dfd1d0fcSMichal Meloun uint32_t addr0; 3368bf0d2d3SJohn Baldwin uint64_t max_size __diagused; 337dfd1d0fcSMichal Meloun 338dfd1d0fcSMichal Meloun /* Check HW constrains */ 339dfd1d0fcSMichal Meloun max_size = idx == 0 ? ATU_OB_REGION_0_SIZE: ATU_OB_REGION_SIZE; 340dfd1d0fcSMichal Meloun KASSERT(idx < ATU_OB_REGIONS, ("Invalid region index: %d\n", idx)); 341dfd1d0fcSMichal Meloun KASSERT(num_bits >= 7 && num_bits <= 63, 342dfd1d0fcSMichal Meloun ("Bit width of region is invalid: %d\n", num_bits)); 343dfd1d0fcSMichal Meloun KASSERT(max_size <= (1ULL << (num_bits + 1)), 344dfd1d0fcSMichal Meloun ("Bit width is invalid for given region[%d]: %d\n", idx, num_bits)); 345dfd1d0fcSMichal Meloun 346dfd1d0fcSMichal Meloun addr0 = (uint32_t)pa & 0xFFFFFF00; 347dfd1d0fcSMichal Meloun addr0 |= num_bits; 348dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_OB_ADDR0(idx), addr0); 349dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_OB_ADDR1(idx), (uint32_t)(pa >> 32)); 350dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_OB_DESC0(idx), 1 << 23 | type); 351dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_OB_DESC1(idx), sc->root_bus); 352dfd1d0fcSMichal Meloun 353dfd1d0fcSMichal Meloun /* Readback for sync */ 354dfd1d0fcSMichal Meloun APB_RD4(sc, PCIE_CORE_OB_DESC1(idx)); 355dfd1d0fcSMichal Meloun } 356dfd1d0fcSMichal Meloun 357dfd1d0fcSMichal Meloun static void 358dfd1d0fcSMichal Meloun rk_pcie_map_cfg_atu(struct rk_pcie_softc *sc, int idx, int type) 359dfd1d0fcSMichal Meloun { 360dfd1d0fcSMichal Meloun 361dfd1d0fcSMichal Meloun /* Check HW constrains */ 362dfd1d0fcSMichal Meloun KASSERT(idx < ATU_OB_REGIONS, ("Invalid region index: %d\n", idx)); 363dfd1d0fcSMichal Meloun 364dfd1d0fcSMichal Meloun /* 365dfd1d0fcSMichal Meloun * Config window is only 25 bits width, so we cannot encode full bus 366dfd1d0fcSMichal Meloun * range into it. Remaining bits of bus number should be taken from 367dfd1d0fcSMichal Meloun * DESC1 field. 368dfd1d0fcSMichal Meloun */ 369dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_OB_ADDR0(idx), 25 - 1); 370dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_OB_ADDR1(idx), 0); 371dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_OB_DESC0(idx), 1 << 23 | type); 372dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_OB_DESC1(idx), sc->root_bus); 373dfd1d0fcSMichal Meloun 374dfd1d0fcSMichal Meloun /* Readback for sync */ 375dfd1d0fcSMichal Meloun APB_RD4(sc, PCIE_CORE_OB_DESC1(idx)); 376dfd1d0fcSMichal Meloun 377dfd1d0fcSMichal Meloun } 378dfd1d0fcSMichal Meloun 379dfd1d0fcSMichal Meloun static void 380dfd1d0fcSMichal Meloun rk_pcie_map_in_atu(struct rk_pcie_softc *sc, int idx, int num_bits, uint64_t pa) 381dfd1d0fcSMichal Meloun { 382dfd1d0fcSMichal Meloun uint32_t addr0; 383dfd1d0fcSMichal Meloun 384dfd1d0fcSMichal Meloun /* Check HW constrains */ 385dfd1d0fcSMichal Meloun KASSERT(idx < ATU_IB_REGIONS, ("Invalid region index: %d\n", idx)); 386dfd1d0fcSMichal Meloun KASSERT(num_bits >= 7 && num_bits <= 63, 387dfd1d0fcSMichal Meloun ("Bit width of region is invalid: %d\n", num_bits)); 388dfd1d0fcSMichal Meloun 389dfd1d0fcSMichal Meloun addr0 = (uint32_t)pa & 0xFFFFFF00; 390dfd1d0fcSMichal Meloun addr0 |= num_bits; 391dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_IB_ADDR0(idx), addr0); 392dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_IB_ADDR1(idx), (uint32_t)(pa >> 32)); 393dfd1d0fcSMichal Meloun 394dfd1d0fcSMichal Meloun /* Readback for sync */ 395dfd1d0fcSMichal Meloun APB_RD4(sc, PCIE_CORE_IB_ADDR1(idx)); 396dfd1d0fcSMichal Meloun } 397dfd1d0fcSMichal Meloun 398dfd1d0fcSMichal Meloun static int 399dfd1d0fcSMichal Meloun rk_pcie_decode_ranges(struct rk_pcie_softc *sc, struct ofw_pci_range *ranges, 400dfd1d0fcSMichal Meloun int nranges) 401dfd1d0fcSMichal Meloun { 402dfd1d0fcSMichal Meloun int i; 403dfd1d0fcSMichal Meloun 404dfd1d0fcSMichal Meloun for (i = 0; i < nranges; i++) { 4051c799a6fSAndrew Turner switch(ranges[i].pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) { 4061c799a6fSAndrew Turner case OFW_PCI_PHYS_HI_SPACE_IO: 407dfd1d0fcSMichal Meloun if (sc->io_range.size != 0) { 408dfd1d0fcSMichal Meloun device_printf(sc->dev, 409dfd1d0fcSMichal Meloun "Duplicated IO range found in DT\n"); 410dfd1d0fcSMichal Meloun return (ENXIO); 411dfd1d0fcSMichal Meloun } 412dfd1d0fcSMichal Meloun sc->io_range = ranges[i]; 4131c799a6fSAndrew Turner break; 4141c799a6fSAndrew Turner case OFW_PCI_PHYS_HI_SPACE_MEM32: 4151c799a6fSAndrew Turner case OFW_PCI_PHYS_HI_SPACE_MEM64: 416dfd1d0fcSMichal Meloun if (ranges[i].pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) { 417dfd1d0fcSMichal Meloun if (sc->pref_mem_range.size != 0) { 418dfd1d0fcSMichal Meloun device_printf(sc->dev, 419dfd1d0fcSMichal Meloun "Duplicated memory range found " 420dfd1d0fcSMichal Meloun "in DT\n"); 421dfd1d0fcSMichal Meloun return (ENXIO); 422dfd1d0fcSMichal Meloun } 423dfd1d0fcSMichal Meloun sc->pref_mem_range = ranges[i]; 424dfd1d0fcSMichal Meloun } else { 425dfd1d0fcSMichal Meloun if (sc->mem_range.size != 0) { 426dfd1d0fcSMichal Meloun device_printf(sc->dev, 427dfd1d0fcSMichal Meloun "Duplicated memory range found " 428dfd1d0fcSMichal Meloun "in DT\n"); 429dfd1d0fcSMichal Meloun return (ENXIO); 430dfd1d0fcSMichal Meloun } 431dfd1d0fcSMichal Meloun sc->mem_range = ranges[i]; 432dfd1d0fcSMichal Meloun } 433dfd1d0fcSMichal Meloun } 434dfd1d0fcSMichal Meloun } 435dfd1d0fcSMichal Meloun if (sc->mem_range.size == 0) { 436dfd1d0fcSMichal Meloun device_printf(sc->dev, 437dfd1d0fcSMichal Meloun " At least memory range should be defined in DT.\n"); 438dfd1d0fcSMichal Meloun return (ENXIO); 439dfd1d0fcSMichal Meloun } 440dfd1d0fcSMichal Meloun return (0); 441dfd1d0fcSMichal Meloun } 442dfd1d0fcSMichal Meloun 443dfd1d0fcSMichal Meloun /*----------------------------------------------------------------------------- 444dfd1d0fcSMichal Meloun * 445dfd1d0fcSMichal Meloun * P C I B I N T E R F A C E 446dfd1d0fcSMichal Meloun */ 447dfd1d0fcSMichal Meloun static uint32_t 448dfd1d0fcSMichal Meloun rk_pcie_read_config(device_t dev, u_int bus, u_int slot, 449dfd1d0fcSMichal Meloun u_int func, u_int reg, int bytes) 450dfd1d0fcSMichal Meloun { 451dfd1d0fcSMichal Meloun struct rk_pcie_softc *sc; 452b8bfffc1SMichal Meloun uint32_t d32, data; 453b8bfffc1SMichal Meloun uint16_t d16; 454b8bfffc1SMichal Meloun uint8_t d8; 455dfd1d0fcSMichal Meloun uint64_t addr; 456b8bfffc1SMichal Meloun int type, ret; 457dfd1d0fcSMichal Meloun 458dfd1d0fcSMichal Meloun sc = device_get_softc(dev); 459dfd1d0fcSMichal Meloun 460dfd1d0fcSMichal Meloun if (!rk_pcie_check_dev(sc, bus, slot, func, reg)) 461dfd1d0fcSMichal Meloun return (0xFFFFFFFFU); 462dfd1d0fcSMichal Meloun if (bus == sc->root_bus) 463dfd1d0fcSMichal Meloun return (rk_pcie_local_cfg_read(sc, false, reg, bytes)); 464dfd1d0fcSMichal Meloun 465dfd1d0fcSMichal Meloun addr = ATU_CFG_BUS(bus) | ATU_CFG_SLOT(slot) | ATU_CFG_FUNC(func) | 466dfd1d0fcSMichal Meloun ATU_CFG_REG(reg); 467b8bfffc1SMichal Meloun type = bus == sc->sub_bus ? ATU_TYPE_CFG0: ATU_TYPE_CFG1; 468dfd1d0fcSMichal Meloun rk_pcie_map_cfg_atu(sc, 0, type); 469dfd1d0fcSMichal Meloun 470b8bfffc1SMichal Meloun ret = -1; 471dfd1d0fcSMichal Meloun switch (bytes) { 472dfd1d0fcSMichal Meloun case 1: 473b8bfffc1SMichal Meloun ret = bus_peek_1(sc->axi_mem_res, addr, &d8); 474b8bfffc1SMichal Meloun data = d8; 475dfd1d0fcSMichal Meloun break; 476dfd1d0fcSMichal Meloun case 2: 477b8bfffc1SMichal Meloun ret = bus_peek_2(sc->axi_mem_res, addr, &d16); 478b8bfffc1SMichal Meloun data = d16; 479dfd1d0fcSMichal Meloun break; 480dfd1d0fcSMichal Meloun case 4: 481b8bfffc1SMichal Meloun ret = bus_peek_4(sc->axi_mem_res, addr, &d32); 482b8bfffc1SMichal Meloun data = d32; 483dfd1d0fcSMichal Meloun break; 484dfd1d0fcSMichal Meloun } 485b8bfffc1SMichal Meloun if (ret != 0) 486b8bfffc1SMichal Meloun data = 0xFFFFFFFF; 487dfd1d0fcSMichal Meloun return (data); 488dfd1d0fcSMichal Meloun } 489dfd1d0fcSMichal Meloun 490dfd1d0fcSMichal Meloun static void 491dfd1d0fcSMichal Meloun rk_pcie_write_config(device_t dev, u_int bus, u_int slot, 492dfd1d0fcSMichal Meloun u_int func, u_int reg, uint32_t val, int bytes) 493dfd1d0fcSMichal Meloun { 494dfd1d0fcSMichal Meloun struct rk_pcie_softc *sc; 495dfd1d0fcSMichal Meloun uint64_t addr; 496dfd1d0fcSMichal Meloun int type; 497dfd1d0fcSMichal Meloun 498dfd1d0fcSMichal Meloun sc = device_get_softc(dev); 499dfd1d0fcSMichal Meloun 500dfd1d0fcSMichal Meloun if (!rk_pcie_check_dev(sc, bus, slot, func, reg)) 501dfd1d0fcSMichal Meloun return; 502dfd1d0fcSMichal Meloun 503dfd1d0fcSMichal Meloun if (bus == sc->root_bus) 504dfd1d0fcSMichal Meloun return (rk_pcie_local_cfg_write(sc, false, reg, val, bytes)); 505dfd1d0fcSMichal Meloun 506dfd1d0fcSMichal Meloun addr = ATU_CFG_BUS(bus) | ATU_CFG_SLOT(slot) | ATU_CFG_FUNC(func) | 507dfd1d0fcSMichal Meloun ATU_CFG_REG(reg); 508b8bfffc1SMichal Meloun type = bus == sc->sub_bus ? ATU_TYPE_CFG0: ATU_TYPE_CFG1; 509dfd1d0fcSMichal Meloun rk_pcie_map_cfg_atu(sc, 0, type); 510dfd1d0fcSMichal Meloun 511dfd1d0fcSMichal Meloun switch (bytes) { 512dfd1d0fcSMichal Meloun case 1: 513b8bfffc1SMichal Meloun bus_poke_1(sc->axi_mem_res, addr, (uint8_t)val); 514dfd1d0fcSMichal Meloun break; 515dfd1d0fcSMichal Meloun case 2: 516b8bfffc1SMichal Meloun bus_poke_2(sc->axi_mem_res, addr, (uint16_t)val); 517dfd1d0fcSMichal Meloun break; 518dfd1d0fcSMichal Meloun case 4: 519b8bfffc1SMichal Meloun bus_poke_4(sc->axi_mem_res, addr, val); 520dfd1d0fcSMichal Meloun break; 521dfd1d0fcSMichal Meloun default: 522dfd1d0fcSMichal Meloun break; 523dfd1d0fcSMichal Meloun } 524dfd1d0fcSMichal Meloun } 525dfd1d0fcSMichal Meloun 526dfd1d0fcSMichal Meloun #ifdef RK_PCIE_ENABLE_MSI 527dfd1d0fcSMichal Meloun static int 528dfd1d0fcSMichal Meloun rk_pcie_alloc_msi(device_t pci, device_t child, int count, 529dfd1d0fcSMichal Meloun int maxcount, int *irqs) 530dfd1d0fcSMichal Meloun { 531dfd1d0fcSMichal Meloun phandle_t msi_parent; 532dfd1d0fcSMichal Meloun int rv; 533dfd1d0fcSMichal Meloun 534dfd1d0fcSMichal Meloun rv = ofw_bus_msimap(ofw_bus_get_node(pci), pci_get_rid(child), 535dfd1d0fcSMichal Meloun &msi_parent, NULL); 536dfd1d0fcSMichal Meloun if (rv != 0) 537dfd1d0fcSMichal Meloun return (rv); 538dfd1d0fcSMichal Meloun 539dfd1d0fcSMichal Meloun rv = intr_alloc_msi(pci, child, msi_parent, count, maxcount,irqs); 540dfd1d0fcSMichal Meloun return (rv); 541dfd1d0fcSMichal Meloun } 542dfd1d0fcSMichal Meloun 543dfd1d0fcSMichal Meloun static int 544dfd1d0fcSMichal Meloun rk_pcie_release_msi(device_t pci, device_t child, int count, int *irqs) 545dfd1d0fcSMichal Meloun { 546dfd1d0fcSMichal Meloun phandle_t msi_parent; 547dfd1d0fcSMichal Meloun int rv; 548dfd1d0fcSMichal Meloun 549dfd1d0fcSMichal Meloun rv = ofw_bus_msimap(ofw_bus_get_node(pci), pci_get_rid(child), 550dfd1d0fcSMichal Meloun &msi_parent, NULL); 551dfd1d0fcSMichal Meloun if (rv != 0) 552dfd1d0fcSMichal Meloun return (rv); 553dfd1d0fcSMichal Meloun rv = intr_release_msi(pci, child, msi_parent, count, irqs); 554dfd1d0fcSMichal Meloun return (rv); 555dfd1d0fcSMichal Meloun } 556dfd1d0fcSMichal Meloun #endif 557dfd1d0fcSMichal Meloun 558dfd1d0fcSMichal Meloun static int 559dfd1d0fcSMichal Meloun rk_pcie_map_msi(device_t pci, device_t child, int irq, uint64_t *addr, 560dfd1d0fcSMichal Meloun uint32_t *data) 561dfd1d0fcSMichal Meloun { 562dfd1d0fcSMichal Meloun phandle_t msi_parent; 563dfd1d0fcSMichal Meloun int rv; 564dfd1d0fcSMichal Meloun 565dfd1d0fcSMichal Meloun rv = ofw_bus_msimap(ofw_bus_get_node(pci), pci_get_rid(child), 566dfd1d0fcSMichal Meloun &msi_parent, NULL); 567dfd1d0fcSMichal Meloun if (rv != 0) 568dfd1d0fcSMichal Meloun return (rv); 569dfd1d0fcSMichal Meloun rv = intr_map_msi(pci, child, msi_parent, irq, addr, data); 570dfd1d0fcSMichal Meloun return (rv); 571dfd1d0fcSMichal Meloun } 572dfd1d0fcSMichal Meloun 573dfd1d0fcSMichal Meloun #ifdef RK_PCIE_ENABLE_MSIX 574dfd1d0fcSMichal Meloun static int 575dfd1d0fcSMichal Meloun rk_pcie_alloc_msix(device_t pci, device_t child, int *irq) 576dfd1d0fcSMichal Meloun { 577dfd1d0fcSMichal Meloun phandle_t msi_parent; 578dfd1d0fcSMichal Meloun int rv; 579dfd1d0fcSMichal Meloun 580dfd1d0fcSMichal Meloun rv = ofw_bus_msimap(ofw_bus_get_node(pci), pci_get_rid(child), 581dfd1d0fcSMichal Meloun &msi_parent, NULL); 582dfd1d0fcSMichal Meloun if (rv != 0) 583dfd1d0fcSMichal Meloun return (rv); 584dfd1d0fcSMichal Meloun rv = intr_alloc_msix(pci, child, msi_parent, irq); 585dfd1d0fcSMichal Meloun return (rv); 586dfd1d0fcSMichal Meloun } 587dfd1d0fcSMichal Meloun 588dfd1d0fcSMichal Meloun static int 589dfd1d0fcSMichal Meloun rk_pcie_release_msix(device_t pci, device_t child, int irq) 590dfd1d0fcSMichal Meloun { 591dfd1d0fcSMichal Meloun phandle_t msi_parent; 592dfd1d0fcSMichal Meloun int rv; 593dfd1d0fcSMichal Meloun 594dfd1d0fcSMichal Meloun rv = ofw_bus_msimap(ofw_bus_get_node(pci), pci_get_rid(child), 595dfd1d0fcSMichal Meloun &msi_parent, NULL); 596dfd1d0fcSMichal Meloun if (rv != 0) 597dfd1d0fcSMichal Meloun return (rv); 598dfd1d0fcSMichal Meloun rv = intr_release_msix(pci, child, msi_parent, irq); 599dfd1d0fcSMichal Meloun return (rv); 600dfd1d0fcSMichal Meloun } 601dfd1d0fcSMichal Meloun #endif 602dfd1d0fcSMichal Meloun 603dfd1d0fcSMichal Meloun static int 604dfd1d0fcSMichal Meloun rk_pcie_get_id(device_t pci, device_t child, enum pci_id_type type, 605dfd1d0fcSMichal Meloun uintptr_t *id) 606dfd1d0fcSMichal Meloun { 607dfd1d0fcSMichal Meloun phandle_t node; 608dfd1d0fcSMichal Meloun int rv; 609dfd1d0fcSMichal Meloun uint32_t rid; 610dfd1d0fcSMichal Meloun uint16_t pci_rid; 611dfd1d0fcSMichal Meloun 612dfd1d0fcSMichal Meloun if (type != PCI_ID_MSI) 613dfd1d0fcSMichal Meloun return (pcib_get_id(pci, child, type, id)); 614dfd1d0fcSMichal Meloun 615dfd1d0fcSMichal Meloun node = ofw_bus_get_node(pci); 616dfd1d0fcSMichal Meloun pci_rid = pci_get_rid(child); 617dfd1d0fcSMichal Meloun 618dfd1d0fcSMichal Meloun rv = ofw_bus_msimap(node, pci_rid, NULL, &rid); 619dfd1d0fcSMichal Meloun if (rv != 0) 620dfd1d0fcSMichal Meloun return (rv); 621dfd1d0fcSMichal Meloun 622dfd1d0fcSMichal Meloun *id = rid; 623dfd1d0fcSMichal Meloun return (0); 624dfd1d0fcSMichal Meloun } 625dfd1d0fcSMichal Meloun 626dfd1d0fcSMichal Meloun static int 627dfd1d0fcSMichal Meloun rk_pcie_route_interrupt(device_t bus, device_t dev, int pin) 628dfd1d0fcSMichal Meloun { 629dfd1d0fcSMichal Meloun struct rk_pcie_softc *sc; 630dfd1d0fcSMichal Meloun u_int irq; 631dfd1d0fcSMichal Meloun 632dfd1d0fcSMichal Meloun sc = device_get_softc(bus); 633dfd1d0fcSMichal Meloun irq = intr_map_clone_irq(rman_get_start(sc->legacy_irq_res)); 634dfd1d0fcSMichal Meloun device_printf(bus, "route pin %d for device %d.%d to %u\n", 635dfd1d0fcSMichal Meloun pin, pci_get_slot(dev), pci_get_function(dev), irq); 636dfd1d0fcSMichal Meloun 637dfd1d0fcSMichal Meloun return (irq); 638dfd1d0fcSMichal Meloun } 639dfd1d0fcSMichal Meloun 640dfd1d0fcSMichal Meloun /*----------------------------------------------------------------------------- 641dfd1d0fcSMichal Meloun * 642dfd1d0fcSMichal Meloun * B U S / D E V I C E I N T E R F A C E 643dfd1d0fcSMichal Meloun */ 644dfd1d0fcSMichal Meloun static int 645dfd1d0fcSMichal Meloun rk_pcie_parse_fdt_resources(struct rk_pcie_softc *sc) 646dfd1d0fcSMichal Meloun { 647dfd1d0fcSMichal Meloun int i, rv; 648dfd1d0fcSMichal Meloun char buf[16]; 649dfd1d0fcSMichal Meloun 650dfd1d0fcSMichal Meloun /* Regulators. All are optional. */ 651dfd1d0fcSMichal Meloun rv = regulator_get_by_ofw_property(sc->dev, 0, 652dfd1d0fcSMichal Meloun "vpcie12v-supply", &sc->supply_12v); 653dfd1d0fcSMichal Meloun if (rv != 0 && rv != ENOENT) { 654dfd1d0fcSMichal Meloun device_printf(sc->dev,"Cannot get 'vpcie12' regulator\n"); 655dfd1d0fcSMichal Meloun return (ENXIO); 656dfd1d0fcSMichal Meloun } 657dfd1d0fcSMichal Meloun rv = regulator_get_by_ofw_property(sc->dev, 0, 658dfd1d0fcSMichal Meloun "vpcie3v3-supply", &sc->supply_3v3); 659dfd1d0fcSMichal Meloun if (rv != 0 && rv != ENOENT) { 660dfd1d0fcSMichal Meloun device_printf(sc->dev,"Cannot get 'vpcie3v3' regulator\n"); 661dfd1d0fcSMichal Meloun return (ENXIO); 662dfd1d0fcSMichal Meloun } 663dfd1d0fcSMichal Meloun rv = regulator_get_by_ofw_property(sc->dev, 0, 664dfd1d0fcSMichal Meloun "vpcie1v8-supply", &sc->supply_1v8); 665dfd1d0fcSMichal Meloun if (rv != 0 && rv != ENOENT) { 666dfd1d0fcSMichal Meloun device_printf(sc->dev,"Cannot get 'vpcie1v8' regulator\n"); 667dfd1d0fcSMichal Meloun return (ENXIO); 668dfd1d0fcSMichal Meloun } 669dfd1d0fcSMichal Meloun rv = regulator_get_by_ofw_property(sc->dev, 0, 670dfd1d0fcSMichal Meloun "vpcie0v9-supply", &sc->supply_0v9); 671dfd1d0fcSMichal Meloun if (rv != 0 && rv != ENOENT) { 672dfd1d0fcSMichal Meloun device_printf(sc->dev,"Cannot get 'vpcie0v9' regulator\n"); 673dfd1d0fcSMichal Meloun return (ENXIO); 674dfd1d0fcSMichal Meloun } 675dfd1d0fcSMichal Meloun 676dfd1d0fcSMichal Meloun /* Resets. */ 677dfd1d0fcSMichal Meloun rv = hwreset_get_by_ofw_name(sc->dev, 0, "core", &sc->hwreset_core); 678dfd1d0fcSMichal Meloun if (rv != 0) { 679dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'core' reset\n"); 680dfd1d0fcSMichal Meloun return (ENXIO); 681dfd1d0fcSMichal Meloun } 682dfd1d0fcSMichal Meloun rv = hwreset_get_by_ofw_name(sc->dev, 0, "mgmt", &sc->hwreset_mgmt); 683dfd1d0fcSMichal Meloun if (rv != 0) { 684dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'mgmt' reset\n"); 685dfd1d0fcSMichal Meloun return (ENXIO); 686dfd1d0fcSMichal Meloun } 687dfd1d0fcSMichal Meloun rv = hwreset_get_by_ofw_name(sc->dev, 0, "mgmt-sticky", 688dfd1d0fcSMichal Meloun &sc->hwreset_mgmt_sticky); 689dfd1d0fcSMichal Meloun if (rv != 0) { 690dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'mgmt-sticky' reset\n"); 691dfd1d0fcSMichal Meloun return (ENXIO); 692dfd1d0fcSMichal Meloun } 693dfd1d0fcSMichal Meloun rv = hwreset_get_by_ofw_name(sc->dev, 0, "pipe", &sc->hwreset_pipe); 694dfd1d0fcSMichal Meloun if (rv != 0) { 695dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'pipe' reset\n"); 696dfd1d0fcSMichal Meloun return (ENXIO); 697dfd1d0fcSMichal Meloun } 698dfd1d0fcSMichal Meloun rv = hwreset_get_by_ofw_name(sc->dev, 0, "pm", &sc->hwreset_pm); 699dfd1d0fcSMichal Meloun if (rv != 0) { 700dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'pm' reset\n"); 701dfd1d0fcSMichal Meloun return (ENXIO); 702dfd1d0fcSMichal Meloun } 703dfd1d0fcSMichal Meloun rv = hwreset_get_by_ofw_name(sc->dev, 0, "aclk", &sc->hwreset_aclk); 704dfd1d0fcSMichal Meloun if (rv != 0) { 705dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'aclk' reset\n"); 706dfd1d0fcSMichal Meloun return (ENXIO); 707dfd1d0fcSMichal Meloun } 708dfd1d0fcSMichal Meloun rv = hwreset_get_by_ofw_name(sc->dev, 0, "pclk", &sc->hwreset_pclk); 709dfd1d0fcSMichal Meloun if (rv != 0) { 710dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'pclk' reset\n"); 711dfd1d0fcSMichal Meloun return (ENXIO); 712dfd1d0fcSMichal Meloun } 713dfd1d0fcSMichal Meloun 714dfd1d0fcSMichal Meloun /* Clocks. */ 715dfd1d0fcSMichal Meloun rv = clk_get_by_ofw_name(sc->dev, 0, "aclk", &sc->clk_aclk); 716dfd1d0fcSMichal Meloun if (rv != 0) { 717dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'aclk' clock\n"); 718dfd1d0fcSMichal Meloun return (ENXIO); 719dfd1d0fcSMichal Meloun } 720dfd1d0fcSMichal Meloun rv = clk_get_by_ofw_name(sc->dev, 0, "aclk-perf", &sc->clk_aclk_perf); 721dfd1d0fcSMichal Meloun if (rv != 0) { 722dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'aclk-perf' clock\n"); 723dfd1d0fcSMichal Meloun return (ENXIO); 724dfd1d0fcSMichal Meloun } 725dfd1d0fcSMichal Meloun rv = clk_get_by_ofw_name(sc->dev, 0, "hclk", &sc->clk_hclk); 726dfd1d0fcSMichal Meloun if (rv != 0) { 727dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'hclk' clock\n"); 728dfd1d0fcSMichal Meloun return (ENXIO); 729dfd1d0fcSMichal Meloun } 730dfd1d0fcSMichal Meloun rv = clk_get_by_ofw_name(sc->dev, 0, "pm", &sc->clk_pm); 731dfd1d0fcSMichal Meloun if (rv != 0) { 732dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'pm' clock\n"); 733dfd1d0fcSMichal Meloun return (ENXIO); 734dfd1d0fcSMichal Meloun } 735dfd1d0fcSMichal Meloun 736dfd1d0fcSMichal Meloun /* Phys. */ 737dfd1d0fcSMichal Meloun for (i = 0; i < MAX_LANES; i++ ) { 738dfd1d0fcSMichal Meloun sprintf (buf, "pcie-phy-%d", i); 739dfd1d0fcSMichal Meloun rv = phy_get_by_ofw_name(sc->dev, 0, buf, sc->phys + i); 740dfd1d0fcSMichal Meloun if (rv != 0) { 741dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get '%s' phy\n", buf); 742dfd1d0fcSMichal Meloun return (ENXIO); 743dfd1d0fcSMichal Meloun } 744dfd1d0fcSMichal Meloun } 745dfd1d0fcSMichal Meloun 746dfd1d0fcSMichal Meloun /* GPIO for PERST#. Optional */ 747dfd1d0fcSMichal Meloun rv = gpio_pin_get_by_ofw_property(sc->dev, sc->node, "ep-gpios", 748dfd1d0fcSMichal Meloun &sc->gpio_ep); 749dfd1d0fcSMichal Meloun if (rv != 0 && rv != ENOENT) { 750dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot get 'ep-gpios' gpio\n"); 751dfd1d0fcSMichal Meloun return (ENXIO); 752dfd1d0fcSMichal Meloun } 753dfd1d0fcSMichal Meloun 754dfd1d0fcSMichal Meloun return (0); 755dfd1d0fcSMichal Meloun } 756dfd1d0fcSMichal Meloun 757dfd1d0fcSMichal Meloun static int 758dfd1d0fcSMichal Meloun rk_pcie_enable_resources(struct rk_pcie_softc *sc) 759dfd1d0fcSMichal Meloun { 760dfd1d0fcSMichal Meloun int i, rv; 761dfd1d0fcSMichal Meloun uint32_t val; 762dfd1d0fcSMichal Meloun 763dfd1d0fcSMichal Meloun /* Assert all resets */ 764dfd1d0fcSMichal Meloun rv = hwreset_assert(sc->hwreset_pclk); 765dfd1d0fcSMichal Meloun if (rv != 0) { 766dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot assert 'pclk' reset\n"); 767dfd1d0fcSMichal Meloun return (rv); 768dfd1d0fcSMichal Meloun } 769dfd1d0fcSMichal Meloun rv = hwreset_assert(sc->hwreset_aclk); 770dfd1d0fcSMichal Meloun if (rv != 0) { 771dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot assert 'aclk' reset\n"); 772dfd1d0fcSMichal Meloun return (rv); 773dfd1d0fcSMichal Meloun } 774dfd1d0fcSMichal Meloun rv = hwreset_assert(sc->hwreset_pm); 775dfd1d0fcSMichal Meloun if (rv != 0) { 776dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot assert 'pm' reset\n"); 777dfd1d0fcSMichal Meloun return (rv); 778dfd1d0fcSMichal Meloun } 779dfd1d0fcSMichal Meloun rv = hwreset_assert(sc->hwreset_pipe); 780dfd1d0fcSMichal Meloun if (rv != 0) { 781dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot assert 'pipe' reset\n"); 782dfd1d0fcSMichal Meloun return (rv); 783dfd1d0fcSMichal Meloun } 784dfd1d0fcSMichal Meloun rv = hwreset_assert(sc->hwreset_mgmt_sticky); 785dfd1d0fcSMichal Meloun if (rv != 0) { 786dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot assert 'mgmt_sticky' reset\n"); 787dfd1d0fcSMichal Meloun return (rv); 788dfd1d0fcSMichal Meloun } 789dfd1d0fcSMichal Meloun rv = hwreset_assert(sc->hwreset_mgmt); 790dfd1d0fcSMichal Meloun if (rv != 0) { 791dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot assert 'hmgmt' reset\n"); 792dfd1d0fcSMichal Meloun return (rv); 793dfd1d0fcSMichal Meloun } 794dfd1d0fcSMichal Meloun rv = hwreset_assert(sc->hwreset_core); 795dfd1d0fcSMichal Meloun if (rv != 0) { 796dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot assert 'hcore' reset\n"); 797dfd1d0fcSMichal Meloun return (rv); 798dfd1d0fcSMichal Meloun } 799dfd1d0fcSMichal Meloun DELAY(10000); 800dfd1d0fcSMichal Meloun 801dfd1d0fcSMichal Meloun /* Enable clockls */ 802dfd1d0fcSMichal Meloun rv = clk_enable(sc->clk_aclk); 803dfd1d0fcSMichal Meloun if (rv != 0) { 804dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot enable 'aclk' clock\n"); 805dfd1d0fcSMichal Meloun return (rv); 806dfd1d0fcSMichal Meloun } 807dfd1d0fcSMichal Meloun rv = clk_enable(sc->clk_aclk_perf); 808dfd1d0fcSMichal Meloun if (rv != 0) { 809dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot enable 'aclk_perf' clock\n"); 810dfd1d0fcSMichal Meloun return (rv); 811dfd1d0fcSMichal Meloun } 812dfd1d0fcSMichal Meloun rv = clk_enable(sc->clk_hclk); 813dfd1d0fcSMichal Meloun if (rv != 0) { 814dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot enable 'hclk' clock\n"); 815dfd1d0fcSMichal Meloun return (rv); 816dfd1d0fcSMichal Meloun } 817dfd1d0fcSMichal Meloun rv = clk_enable(sc->clk_pm); 818dfd1d0fcSMichal Meloun if (rv != 0) { 819dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot enable 'pm' clock\n"); 820dfd1d0fcSMichal Meloun return (rv); 821dfd1d0fcSMichal Meloun } 822dfd1d0fcSMichal Meloun 823dfd1d0fcSMichal Meloun /* Power up regulators */ 824dfd1d0fcSMichal Meloun if (sc->supply_12v != NULL) { 825dfd1d0fcSMichal Meloun rv = regulator_enable(sc->supply_12v); 826dfd1d0fcSMichal Meloun if (rv != 0) { 827dfd1d0fcSMichal Meloun device_printf(sc->dev, 828dfd1d0fcSMichal Meloun "Cannot enable 'vpcie12' regulator\n"); 829dfd1d0fcSMichal Meloun return (rv); 830dfd1d0fcSMichal Meloun } 831dfd1d0fcSMichal Meloun } 832dfd1d0fcSMichal Meloun if (sc->supply_3v3 != NULL) { 833dfd1d0fcSMichal Meloun rv = regulator_enable(sc->supply_3v3); 834dfd1d0fcSMichal Meloun if (rv != 0) { 835dfd1d0fcSMichal Meloun device_printf(sc->dev, 836dfd1d0fcSMichal Meloun "Cannot enable 'vpcie3v3' regulator\n"); 837dfd1d0fcSMichal Meloun return (rv); 838dfd1d0fcSMichal Meloun } 839dfd1d0fcSMichal Meloun } 840dfd1d0fcSMichal Meloun if (sc->supply_1v8 != NULL) { 841dfd1d0fcSMichal Meloun rv = regulator_enable(sc->supply_1v8); 842dfd1d0fcSMichal Meloun if (rv != 0) { 843dfd1d0fcSMichal Meloun device_printf(sc->dev, 844dfd1d0fcSMichal Meloun "Cannot enable 'vpcie1v8' regulator\n"); 845dfd1d0fcSMichal Meloun return (rv); 846dfd1d0fcSMichal Meloun } 847dfd1d0fcSMichal Meloun } 848dfd1d0fcSMichal Meloun if (sc->supply_0v9 != NULL) { 849dfd1d0fcSMichal Meloun rv = regulator_enable(sc->supply_0v9); 850dfd1d0fcSMichal Meloun if (rv != 0) { 851dfd1d0fcSMichal Meloun device_printf(sc->dev, 852dfd1d0fcSMichal Meloun "Cannot enable 'vpcie1v8' regulator\n"); 853dfd1d0fcSMichal Meloun return (rv); 854dfd1d0fcSMichal Meloun } 855dfd1d0fcSMichal Meloun } 856dfd1d0fcSMichal Meloun DELAY(1000); 857dfd1d0fcSMichal Meloun 858dfd1d0fcSMichal Meloun /* Deassert basic resets*/ 859dfd1d0fcSMichal Meloun rv = hwreset_deassert(sc->hwreset_pm); 860dfd1d0fcSMichal Meloun if (rv != 0) { 861dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot deassert 'pm' reset\n"); 862dfd1d0fcSMichal Meloun return (rv); 863dfd1d0fcSMichal Meloun } 864dfd1d0fcSMichal Meloun rv = hwreset_deassert(sc->hwreset_aclk); 865dfd1d0fcSMichal Meloun if (rv != 0) { 866dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot deassert 'aclk' reset\n"); 867dfd1d0fcSMichal Meloun return (rv); 868dfd1d0fcSMichal Meloun } 869dfd1d0fcSMichal Meloun rv = hwreset_deassert(sc->hwreset_pclk); 870dfd1d0fcSMichal Meloun if (rv != 0) { 871dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot deassert 'pclk' reset\n"); 872dfd1d0fcSMichal Meloun return (rv); 873dfd1d0fcSMichal Meloun } 874dfd1d0fcSMichal Meloun 875dfd1d0fcSMichal Meloun /* Set basic PCIe core mode (RC, lanes, gen1 or 2) */ 876dfd1d0fcSMichal Meloun val = STRAP_CONF_GEN_2 << 16 | 877711b7264SMichal Meloun (sc->link_is_gen2 ? STRAP_CONF_GEN_2: 0); 878dfd1d0fcSMichal Meloun val |= STRAP_CONF_MODE_RC << 16 | STRAP_CONF_MODE_RC; 879dfd1d0fcSMichal Meloun val |= STRAP_CONF_LANES(~0) << 16 | STRAP_CONF_LANES(sc->num_lanes); 880dfd1d0fcSMichal Meloun val |= STRAP_CONF_ARI_EN << 16 | STRAP_CONF_ARI_EN; 881dfd1d0fcSMichal Meloun val |= STRAP_CONF_CONF_EN << 16 | STRAP_CONF_CONF_EN; 882dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CLIENT_BASIC_STRAP_CONF, val); 883dfd1d0fcSMichal Meloun 884dfd1d0fcSMichal Meloun for (i = 0; i < MAX_LANES; i++) { 885dfd1d0fcSMichal Meloun rv = phy_enable(sc->phys[i]); 886dfd1d0fcSMichal Meloun if (rv != 0) { 887dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot enable phy %d\n", i); 888dfd1d0fcSMichal Meloun return (rv); 889dfd1d0fcSMichal Meloun } 890dfd1d0fcSMichal Meloun } 891dfd1d0fcSMichal Meloun 892dfd1d0fcSMichal Meloun /* Deassert rest of resets - order is important ! */ 893dfd1d0fcSMichal Meloun rv = hwreset_deassert(sc->hwreset_mgmt_sticky); 894dfd1d0fcSMichal Meloun if (rv != 0) { 895dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot deassert 'mgmt_sticky' reset\n"); 896dfd1d0fcSMichal Meloun return (rv); 897dfd1d0fcSMichal Meloun } 898dfd1d0fcSMichal Meloun rv = hwreset_deassert(sc->hwreset_core); 899dfd1d0fcSMichal Meloun if (rv != 0) { 900dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot deassert 'core' reset\n"); 901dfd1d0fcSMichal Meloun return (rv); 902dfd1d0fcSMichal Meloun } 903dfd1d0fcSMichal Meloun rv = hwreset_deassert(sc->hwreset_mgmt); 904dfd1d0fcSMichal Meloun if (rv != 0) { 905dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot deassert 'mgmt' reset\n"); 906dfd1d0fcSMichal Meloun return (rv); 907dfd1d0fcSMichal Meloun } 908dfd1d0fcSMichal Meloun rv = hwreset_deassert(sc->hwreset_pipe); 909dfd1d0fcSMichal Meloun if (rv != 0) { 910dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot deassert 'pipe' reset\n"); 911dfd1d0fcSMichal Meloun return (rv); 912dfd1d0fcSMichal Meloun } 913dfd1d0fcSMichal Meloun return (0); 914dfd1d0fcSMichal Meloun } 915dfd1d0fcSMichal Meloun 916dfd1d0fcSMichal Meloun static int 917dfd1d0fcSMichal Meloun rk_pcie_setup_hw(struct rk_pcie_softc *sc) 918dfd1d0fcSMichal Meloun { 919dfd1d0fcSMichal Meloun uint32_t val; 920dfd1d0fcSMichal Meloun int i, rv; 921dfd1d0fcSMichal Meloun 922dfd1d0fcSMichal Meloun /* Assert PERST# if defined */ 923dfd1d0fcSMichal Meloun if (sc->gpio_ep != NULL) { 924dfd1d0fcSMichal Meloun rv = gpio_pin_set_active(sc->gpio_ep, 0); 925dfd1d0fcSMichal Meloun if (rv != 0) { 926dfd1d0fcSMichal Meloun device_printf(sc->dev, 927dfd1d0fcSMichal Meloun "Cannot clear 'gpio-ep' gpio\n"); 928dfd1d0fcSMichal Meloun return (rv); 929dfd1d0fcSMichal Meloun } 930dfd1d0fcSMichal Meloun } 931dfd1d0fcSMichal Meloun 932dfd1d0fcSMichal Meloun rv = rk_pcie_enable_resources(sc); 933dfd1d0fcSMichal Meloun if (rv != 0) 934dfd1d0fcSMichal Meloun return(rv); 935dfd1d0fcSMichal Meloun 936dfd1d0fcSMichal Meloun /* Fix wrong default value for transmited FTS for L0s exit */ 937dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_CORE_CTRL1); 938dfd1d0fcSMichal Meloun val |= 0xFFFF << 8; 939dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_CTRL1, val); 940dfd1d0fcSMichal Meloun 941dfd1d0fcSMichal Meloun /* Setup PCIE Link Status & Control register */ 942dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_RC_CONFIG_LCS); 943dfd1d0fcSMichal Meloun val |= PCIEM_LINK_CTL_COMMON_CLOCK; 944dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_RC_CONFIG_LCS, val); 945dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_RC_CONFIG_LCS); 946dfd1d0fcSMichal Meloun val |= PCIEM_LINK_CTL_RCB; 947dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_RC_CONFIG_LCS, val); 948dfd1d0fcSMichal Meloun 949dfd1d0fcSMichal Meloun /* Enable training for GEN1 */ 950dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CLIENT_BASIC_STRAP_CONF, 951dfd1d0fcSMichal Meloun STRAP_CONF_LINK_TRAIN_EN << 16 | STRAP_CONF_LINK_TRAIN_EN); 952dfd1d0fcSMichal Meloun 953dfd1d0fcSMichal Meloun /* Deassert PERST# if defined */ 954dfd1d0fcSMichal Meloun if (sc->gpio_ep != NULL) { 955dfd1d0fcSMichal Meloun rv = gpio_pin_set_active(sc->gpio_ep, 1); 956dfd1d0fcSMichal Meloun if (rv != 0) { 957dfd1d0fcSMichal Meloun device_printf(sc->dev, "Cannot set 'gpio-ep' gpio\n"); 958dfd1d0fcSMichal Meloun return (rv); 959dfd1d0fcSMichal Meloun } 960dfd1d0fcSMichal Meloun } 961dfd1d0fcSMichal Meloun 962dfd1d0fcSMichal Meloun /* Wait for link */ 963dfd1d0fcSMichal Meloun for (i = 500; i > 0; i--) { 964dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_CLIENT_BASIC_STATUS1); 965dfd1d0fcSMichal Meloun if (STATUS1_LINK_ST_GET(val) == STATUS1_LINK_ST_UP) 966dfd1d0fcSMichal Meloun break; 967dfd1d0fcSMichal Meloun DELAY(1000); 968dfd1d0fcSMichal Meloun } 969dfd1d0fcSMichal Meloun if (i <= 0) { 970dfd1d0fcSMichal Meloun device_printf(sc->dev, 971dfd1d0fcSMichal Meloun "Gen1 link training timeouted: 0x%08X.\n", val); 972dfd1d0fcSMichal Meloun return (0); 973dfd1d0fcSMichal Meloun } 974dfd1d0fcSMichal Meloun 975dfd1d0fcSMichal Meloun if (sc->link_is_gen2) { 976dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_RC_CONFIG_LCS); 977dfd1d0fcSMichal Meloun val |= PCIEM_LINK_CTL_RETRAIN_LINK; 978dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_RC_CONFIG_LCS, val); 979dfd1d0fcSMichal Meloun 980dfd1d0fcSMichal Meloun /* Wait for link */ 981dfd1d0fcSMichal Meloun for (i = 500; i > 0; i--) { 982dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_CLIENT_BASIC_STATUS1); 983dfd1d0fcSMichal Meloun if (STATUS1_LINK_ST_GET(val) == 984dfd1d0fcSMichal Meloun STATUS1_LINK_ST_UP) 985dfd1d0fcSMichal Meloun break; 986dfd1d0fcSMichal Meloun DELAY(1000); 987dfd1d0fcSMichal Meloun } 988dfd1d0fcSMichal Meloun if (i <= 0) 989dfd1d0fcSMichal Meloun device_printf(sc->dev, "Gen2 link training " 990dfd1d0fcSMichal Meloun "timeouted: 0x%08X.\n", val); 991dfd1d0fcSMichal Meloun } 992dfd1d0fcSMichal Meloun 993dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_CORE_CTRL0); 994dfd1d0fcSMichal Meloun val = CORE_CTRL_LANES_GET(val); 995dfd1d0fcSMichal Meloun if (bootverbose) 996dfd1d0fcSMichal Meloun device_printf(sc->dev, "Link width: %d\n", 1 << val); 997dfd1d0fcSMichal Meloun 998dfd1d0fcSMichal Meloun return (0); 999dfd1d0fcSMichal Meloun } 1000dfd1d0fcSMichal Meloun 1001dfd1d0fcSMichal Meloun static int 1002dfd1d0fcSMichal Meloun rk_pcie_setup_sw(struct rk_pcie_softc *sc) 1003dfd1d0fcSMichal Meloun { 1004dfd1d0fcSMichal Meloun uint32_t val; 1005dfd1d0fcSMichal Meloun int i, region; 1006dfd1d0fcSMichal Meloun 1007dfd1d0fcSMichal Meloun pcib_bridge_init(sc->dev); 1008dfd1d0fcSMichal Meloun 1009dfd1d0fcSMichal Meloun /* Setup config registers */ 1010dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_CONFIG_VENDOR, 0x1D87); /* Rockchip vendor ID*/ 1011dfd1d0fcSMichal Meloun PRIV_CFG_WR1(sc, PCIR_CLASS, PCIC_BRIDGE); 1012dfd1d0fcSMichal Meloun PRIV_CFG_WR1(sc, PCIR_SUBCLASS, PCIS_BRIDGE_PCI); 1013dfd1d0fcSMichal Meloun PRIV_CFG_WR1(sc, PCIR_PRIBUS_1, sc->root_bus); 1014dfd1d0fcSMichal Meloun PRIV_CFG_WR1(sc, PCIR_SECBUS_1, sc->sub_bus); 1015dfd1d0fcSMichal Meloun PRIV_CFG_WR1(sc, PCIR_SUBBUS_1, sc->bus_end); 1016dfd1d0fcSMichal Meloun PRIV_CFG_WR2(sc, PCIR_COMMAND, PCIM_CMD_MEMEN | 1017dfd1d0fcSMichal Meloun PCIM_CMD_BUSMASTEREN | PCIM_CMD_SERRESPEN); 1018dfd1d0fcSMichal Meloun 1019dfd1d0fcSMichal Meloun /* Don't advertise L1 power substate */ 1020dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_RC_CONFIG_THP_CAP); 1021dfd1d0fcSMichal Meloun val &= ~PCIE_RC_CONFIG_THP_CAP_NEXT_MASK; 1022dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_RC_CONFIG_THP_CAP, val); 1023dfd1d0fcSMichal Meloun 1024dfd1d0fcSMichal Meloun /* Don't advertise L0s */ 1025dfd1d0fcSMichal Meloun if (sc->no_l0s) { 1026dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_RC_CONFIG_LINK_CAP); 1027dfd1d0fcSMichal Meloun val &= ~PCIE_RC_CONFIG_THP_CAP_NEXT_MASK; 1028dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_RC_CONFIG_LINK_CAP_L0S, val); 1029dfd1d0fcSMichal Meloun } 1030dfd1d0fcSMichal Meloun 1031dfd1d0fcSMichal Meloun /*Adjust maximum payload size*/ 1032dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_RC_CONFIG_DCSR); 1033dfd1d0fcSMichal Meloun val &= ~PCIE_RC_CONFIG_DCSR_MPS_MASK; 1034dfd1d0fcSMichal Meloun val |= PCIE_RC_CONFIG_DCSR_MPS_128; 1035dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_RC_CONFIG_DCSR, val); 1036dfd1d0fcSMichal Meloun 1037dfd1d0fcSMichal Meloun /* 1038dfd1d0fcSMichal Meloun * Prepare IB ATU 1039dfd1d0fcSMichal Meloun * map whole address range in 1:1 mappings 1040dfd1d0fcSMichal Meloun */ 1041dfd1d0fcSMichal Meloun rk_pcie_map_in_atu(sc, 2, 64 - 1, 0); 1042dfd1d0fcSMichal Meloun 1043dfd1d0fcSMichal Meloun /* Prepare OB ATU */ 1044dfd1d0fcSMichal Meloun /* - region 0 (32 MB) is used for config access */ 1045dfd1d0fcSMichal Meloun region = 0; 1046dfd1d0fcSMichal Meloun rk_pcie_map_out_atu(sc, region++, ATU_TYPE_CFG0, 25 - 1, 0); 1047dfd1d0fcSMichal Meloun 1048dfd1d0fcSMichal Meloun /* - then map memory (by using 1MB regions */ 1049dfd1d0fcSMichal Meloun for (i = 0; i < sc->mem_range.size / ATU_OB_REGION_SIZE; i++) { 1050dfd1d0fcSMichal Meloun rk_pcie_map_out_atu(sc, region++, ATU_TYPE_MEM, 1051dfd1d0fcSMichal Meloun ATU_OB_REGION_SHIFT - 1, 1052dfd1d0fcSMichal Meloun sc->mem_range.pci + ATU_OB_REGION_SIZE * i); 1053dfd1d0fcSMichal Meloun } 1054dfd1d0fcSMichal Meloun 1055dfd1d0fcSMichal Meloun /* - IO space is next, one region typically*/ 1056dfd1d0fcSMichal Meloun for (i = 0; i < sc->io_range.size / ATU_OB_REGION_SIZE; i++) { 1057dfd1d0fcSMichal Meloun rk_pcie_map_out_atu(sc, region++, ATU_TYPE_IO, 1058dfd1d0fcSMichal Meloun ATU_OB_REGION_SHIFT - 1, 1059dfd1d0fcSMichal Meloun sc->io_range.pci + ATU_OB_REGION_SIZE * i); 1060dfd1d0fcSMichal Meloun } 1061dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_RC_BAR_CONF, 0); 1062dfd1d0fcSMichal Meloun return (0); 1063dfd1d0fcSMichal Meloun } 1064dfd1d0fcSMichal Meloun 1065dfd1d0fcSMichal Meloun static int 1066dfd1d0fcSMichal Meloun rk_pcie_sys_irq(void *arg) 1067dfd1d0fcSMichal Meloun { 1068dfd1d0fcSMichal Meloun struct rk_pcie_softc *sc; 1069dfd1d0fcSMichal Meloun uint32_t irq; 1070dfd1d0fcSMichal Meloun 1071dfd1d0fcSMichal Meloun sc = (struct rk_pcie_softc *)arg; 1072dfd1d0fcSMichal Meloun irq = APB_RD4(sc, PCIE_CLIENT_INT_STATUS); 1073dfd1d0fcSMichal Meloun if (irq & PCIE_CLIENT_INT_LOCAL) { 1074dfd1d0fcSMichal Meloun irq = APB_RD4(sc, PCIE_CORE_INT_STATUS); 1075dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_INT_STATUS, irq); 1076dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CLIENT_INT_STATUS, PCIE_CLIENT_INT_LOCAL); 1077dfd1d0fcSMichal Meloun 1078dfd1d0fcSMichal Meloun device_printf(sc->dev, "'sys' interrupt received: 0x%04X\n", 1079dfd1d0fcSMichal Meloun irq); 1080dfd1d0fcSMichal Meloun } 1081dfd1d0fcSMichal Meloun 1082dfd1d0fcSMichal Meloun return (FILTER_HANDLED); 1083dfd1d0fcSMichal Meloun } 1084dfd1d0fcSMichal Meloun 1085dfd1d0fcSMichal Meloun static int 1086dfd1d0fcSMichal Meloun rk_pcie_client_irq(void *arg) 1087dfd1d0fcSMichal Meloun { 1088dfd1d0fcSMichal Meloun struct rk_pcie_softc *sc; 1089dfd1d0fcSMichal Meloun uint32_t irq; 1090dfd1d0fcSMichal Meloun 1091dfd1d0fcSMichal Meloun sc = (struct rk_pcie_softc *)arg; 1092dfd1d0fcSMichal Meloun irq = APB_RD4(sc, PCIE_CLIENT_INT_STATUS); 1093dfd1d0fcSMichal Meloun /* Clear causes handled by other interrups */ 1094dfd1d0fcSMichal Meloun irq &= ~PCIE_CLIENT_INT_LOCAL; 1095dfd1d0fcSMichal Meloun irq &= ~PCIE_CLIENT_INT_LEGACY; 1096dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CLIENT_INT_STATUS, irq); 1097dfd1d0fcSMichal Meloun 1098dfd1d0fcSMichal Meloun device_printf(sc->dev, "'client' interrupt received: 0x%04X\n", irq); 1099dfd1d0fcSMichal Meloun 1100dfd1d0fcSMichal Meloun return (FILTER_HANDLED); 1101dfd1d0fcSMichal Meloun } 1102dfd1d0fcSMichal Meloun 1103dfd1d0fcSMichal Meloun static int 1104dfd1d0fcSMichal Meloun rk_pcie_legacy_irq(void *arg) 1105dfd1d0fcSMichal Meloun { 1106dfd1d0fcSMichal Meloun struct rk_pcie_softc *sc; 1107dfd1d0fcSMichal Meloun uint32_t irq; 1108dfd1d0fcSMichal Meloun 1109dfd1d0fcSMichal Meloun sc = (struct rk_pcie_softc *)arg; 1110dfd1d0fcSMichal Meloun irq = APB_RD4(sc, PCIE_CLIENT_INT_STATUS); 1111dfd1d0fcSMichal Meloun irq &= PCIE_CLIENT_INT_LEGACY; 1112dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CLIENT_INT_STATUS, irq); 1113dfd1d0fcSMichal Meloun 1114dfd1d0fcSMichal Meloun /* all legacy interrupt are shared, do nothing */ 1115dfd1d0fcSMichal Meloun return (FILTER_STRAY); 1116dfd1d0fcSMichal Meloun } 1117dfd1d0fcSMichal Meloun 1118dfd1d0fcSMichal Meloun static bus_dma_tag_t 1119dfd1d0fcSMichal Meloun rk_pcie_get_dma_tag(device_t dev, device_t child) 1120dfd1d0fcSMichal Meloun { 1121dfd1d0fcSMichal Meloun struct rk_pcie_softc *sc; 1122dfd1d0fcSMichal Meloun 1123dfd1d0fcSMichal Meloun sc = device_get_softc(dev); 1124dfd1d0fcSMichal Meloun return (sc->dmat); 1125dfd1d0fcSMichal Meloun } 1126dfd1d0fcSMichal Meloun 1127dfd1d0fcSMichal Meloun static int 1128dfd1d0fcSMichal Meloun rk_pcie_probe(device_t dev) 1129dfd1d0fcSMichal Meloun { 1130dfd1d0fcSMichal Meloun 1131dfd1d0fcSMichal Meloun if (!ofw_bus_status_okay(dev)) 1132dfd1d0fcSMichal Meloun return (ENXIO); 1133dfd1d0fcSMichal Meloun 1134dfd1d0fcSMichal Meloun if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 1135dfd1d0fcSMichal Meloun return (ENXIO); 1136dfd1d0fcSMichal Meloun 1137dfd1d0fcSMichal Meloun device_set_desc(dev, "Rockchip PCIe controller"); 1138dfd1d0fcSMichal Meloun return (BUS_PROBE_DEFAULT); 1139dfd1d0fcSMichal Meloun } 1140dfd1d0fcSMichal Meloun 1141dfd1d0fcSMichal Meloun static int 1142dfd1d0fcSMichal Meloun rk_pcie_attach(device_t dev) 1143cb894f74SAndrew Turner { 1144cb894f74SAndrew Turner struct resource_map_request req; 1145cb894f74SAndrew Turner struct resource_map map; 1146cb894f74SAndrew Turner struct rk_pcie_softc *sc; 1147dfd1d0fcSMichal Meloun uint32_t val; 1148dfd1d0fcSMichal Meloun int rv, rid, max_speed; 1149dfd1d0fcSMichal Meloun 1150dfd1d0fcSMichal Meloun sc = device_get_softc(dev); 1151dfd1d0fcSMichal Meloun sc->dev = dev; 1152dfd1d0fcSMichal Meloun sc->node = ofw_bus_get_node(dev); 1153dfd1d0fcSMichal Meloun 1154dfd1d0fcSMichal Meloun mtx_init(&sc->mtx, "rk_pcie_mtx", NULL, MTX_DEF); 1155dfd1d0fcSMichal Meloun 1156dfd1d0fcSMichal Meloun /* XXX Should not be this configurable ? */ 1157dfd1d0fcSMichal Meloun sc->bus_start = 0; 1158dfd1d0fcSMichal Meloun sc->bus_end = 0x1F; 1159dfd1d0fcSMichal Meloun sc->root_bus = sc->bus_start; 1160dfd1d0fcSMichal Meloun sc->sub_bus = 1; 1161dfd1d0fcSMichal Meloun 1162dfd1d0fcSMichal Meloun /* Read FDT properties */ 1163dfd1d0fcSMichal Meloun rv = rk_pcie_parse_fdt_resources(sc); 1164dfd1d0fcSMichal Meloun if (rv != 0) 1165ee2324aaSAndrew Turner goto out; 1166dfd1d0fcSMichal Meloun 1167dfd1d0fcSMichal Meloun sc->coherent = OF_hasprop(sc->node, "dma-coherent"); 1168dfd1d0fcSMichal Meloun sc->no_l0s = OF_hasprop(sc->node, "aspm-no-l0s"); 1169dfd1d0fcSMichal Meloun rv = OF_getencprop(sc->node, "num-lanes", &sc->num_lanes, 1170dfd1d0fcSMichal Meloun sizeof(sc->num_lanes)); 1171dfd1d0fcSMichal Meloun if (rv != sizeof(sc->num_lanes)) 1172dfd1d0fcSMichal Meloun sc->num_lanes = 1; 1173dfd1d0fcSMichal Meloun if (sc->num_lanes != 1 && sc->num_lanes != 2 && sc->num_lanes != 4) { 1174dfd1d0fcSMichal Meloun device_printf(dev, 1175dfd1d0fcSMichal Meloun "invalid number of lanes: %d\n",sc->num_lanes); 1176dfd1d0fcSMichal Meloun sc->num_lanes = 0; 1177dfd1d0fcSMichal Meloun rv = ENXIO; 1178dfd1d0fcSMichal Meloun goto out; 1179dfd1d0fcSMichal Meloun } 1180dfd1d0fcSMichal Meloun 1181dfd1d0fcSMichal Meloun rv = OF_getencprop(sc->node, "max-link-speed", &max_speed, 1182dfd1d0fcSMichal Meloun sizeof(max_speed)); 1183dfd1d0fcSMichal Meloun if (rv != sizeof(max_speed) || max_speed != 1) 1184dfd1d0fcSMichal Meloun sc->link_is_gen2 = true; 1185dfd1d0fcSMichal Meloun else 1186dfd1d0fcSMichal Meloun sc->link_is_gen2 = false; 1187dfd1d0fcSMichal Meloun 1188dfd1d0fcSMichal Meloun rv = ofw_bus_find_string_index(sc->node, "reg-names", "axi-base", &rid); 1189dfd1d0fcSMichal Meloun if (rv != 0) { 1190dfd1d0fcSMichal Meloun device_printf(dev, "Cannot get 'axi-base' memory\n"); 1191dfd1d0fcSMichal Meloun rv = ENXIO; 1192dfd1d0fcSMichal Meloun goto out; 1193dfd1d0fcSMichal Meloun } 1194dfd1d0fcSMichal Meloun sc->axi_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 1195cb894f74SAndrew Turner RF_ACTIVE | RF_UNMAPPED); 1196dfd1d0fcSMichal Meloun if (sc->axi_mem_res == NULL) { 1197dfd1d0fcSMichal Meloun device_printf(dev, "Cannot allocate 'axi-base' (rid: %d)\n", 1198dfd1d0fcSMichal Meloun rid); 1199dfd1d0fcSMichal Meloun rv = ENXIO; 1200dfd1d0fcSMichal Meloun goto out; 1201dfd1d0fcSMichal Meloun } 1202cb894f74SAndrew Turner resource_init_map_request(&req); 1203cb894f74SAndrew Turner req.memattr = VM_MEMATTR_DEVICE_NP; 1204cb894f74SAndrew Turner rv = bus_map_resource(dev, SYS_RES_MEMORY, sc->axi_mem_res, &req, 1205cb894f74SAndrew Turner &map); 1206cb894f74SAndrew Turner if (rv != 0) { 1207cb894f74SAndrew Turner device_printf(dev, "Cannot map 'axi-base' (rid: %d)\n", 1208cb894f74SAndrew Turner rid); 1209cb894f74SAndrew Turner goto out; 1210cb894f74SAndrew Turner } 1211cb894f74SAndrew Turner rman_set_mapping(sc->axi_mem_res, &map); 1212cb894f74SAndrew Turner 1213dfd1d0fcSMichal Meloun rv = ofw_bus_find_string_index(sc->node, "reg-names", "apb-base", &rid); 1214dfd1d0fcSMichal Meloun if (rv != 0) { 1215dfd1d0fcSMichal Meloun device_printf(dev, "Cannot get 'apb-base' memory\n"); 1216dfd1d0fcSMichal Meloun rv = ENXIO; 1217dfd1d0fcSMichal Meloun goto out; 1218dfd1d0fcSMichal Meloun } 1219dfd1d0fcSMichal Meloun sc->apb_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 1220dfd1d0fcSMichal Meloun RF_ACTIVE); 1221dfd1d0fcSMichal Meloun if (sc->apb_mem_res == NULL) { 1222dfd1d0fcSMichal Meloun device_printf(dev, "Cannot allocate 'apb-base' (rid: %d)\n", 1223dfd1d0fcSMichal Meloun rid); 1224dfd1d0fcSMichal Meloun rv = ENXIO; 1225dfd1d0fcSMichal Meloun goto out; 1226dfd1d0fcSMichal Meloun } 1227dfd1d0fcSMichal Meloun 1228dfd1d0fcSMichal Meloun rv = ofw_bus_find_string_index(sc->node, "interrupt-names", 1229dfd1d0fcSMichal Meloun "client", &rid); 1230dfd1d0fcSMichal Meloun if (rv != 0) { 1231dfd1d0fcSMichal Meloun device_printf(dev, "Cannot get 'client' IRQ\n"); 1232dfd1d0fcSMichal Meloun rv = ENXIO; 1233dfd1d0fcSMichal Meloun goto out; 1234dfd1d0fcSMichal Meloun } 1235dfd1d0fcSMichal Meloun sc->client_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1236dfd1d0fcSMichal Meloun RF_ACTIVE | RF_SHAREABLE); 1237dfd1d0fcSMichal Meloun if (sc->client_irq_res == NULL) { 1238dfd1d0fcSMichal Meloun device_printf(dev, "Cannot allocate 'client' IRQ resource\n"); 1239dfd1d0fcSMichal Meloun rv = ENXIO; 1240dfd1d0fcSMichal Meloun goto out; 1241dfd1d0fcSMichal Meloun } 1242dfd1d0fcSMichal Meloun 1243dfd1d0fcSMichal Meloun rv = ofw_bus_find_string_index(sc->node, "interrupt-names", 1244dfd1d0fcSMichal Meloun "legacy", &rid); 1245dfd1d0fcSMichal Meloun if (rv != 0) { 1246dfd1d0fcSMichal Meloun device_printf(dev, "Cannot get 'legacy' IRQ\n"); 1247dfd1d0fcSMichal Meloun rv = ENXIO; 1248dfd1d0fcSMichal Meloun goto out; 1249dfd1d0fcSMichal Meloun } 1250dfd1d0fcSMichal Meloun sc->legacy_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1251dfd1d0fcSMichal Meloun RF_ACTIVE | RF_SHAREABLE); 1252dfd1d0fcSMichal Meloun if (sc->legacy_irq_res == NULL) { 1253dfd1d0fcSMichal Meloun device_printf(dev, "Cannot allocate 'legacy' IRQ resource\n"); 1254dfd1d0fcSMichal Meloun rv = ENXIO; 1255dfd1d0fcSMichal Meloun goto out; 1256dfd1d0fcSMichal Meloun } 1257dfd1d0fcSMichal Meloun 1258dfd1d0fcSMichal Meloun rv = ofw_bus_find_string_index(sc->node, "interrupt-names", 1259dfd1d0fcSMichal Meloun "sys", &rid); 1260dfd1d0fcSMichal Meloun if (rv != 0) { 1261dfd1d0fcSMichal Meloun device_printf(dev, "Cannot get 'sys' IRQ\n"); 1262dfd1d0fcSMichal Meloun rv = ENXIO; 1263dfd1d0fcSMichal Meloun goto out; 1264dfd1d0fcSMichal Meloun } 1265dfd1d0fcSMichal Meloun sc->sys_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1266dfd1d0fcSMichal Meloun RF_ACTIVE | RF_SHAREABLE); 1267dfd1d0fcSMichal Meloun if (sc->sys_irq_res == NULL) { 1268dfd1d0fcSMichal Meloun device_printf(dev, "Cannot allocate 'sys' IRQ resource\n"); 1269dfd1d0fcSMichal Meloun rv = ENXIO; 1270dfd1d0fcSMichal Meloun goto out; 1271dfd1d0fcSMichal Meloun } 1272dfd1d0fcSMichal Meloun 1273dfd1d0fcSMichal Meloun if (bootverbose) 1274dfd1d0fcSMichal Meloun device_printf(dev, "Bus is%s cache-coherent\n", 1275dfd1d0fcSMichal Meloun sc->coherent ? "" : " not"); 1276dfd1d0fcSMichal Meloun rv = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 1277dfd1d0fcSMichal Meloun 1, 0, /* alignment, bounds */ 1278dfd1d0fcSMichal Meloun BUS_SPACE_MAXADDR, /* lowaddr */ 1279dfd1d0fcSMichal Meloun BUS_SPACE_MAXADDR, /* highaddr */ 1280dfd1d0fcSMichal Meloun NULL, NULL, /* filter, filterarg */ 1281dfd1d0fcSMichal Meloun BUS_SPACE_MAXSIZE, /* maxsize */ 1282dfd1d0fcSMichal Meloun BUS_SPACE_UNRESTRICTED, /* nsegments */ 1283dfd1d0fcSMichal Meloun BUS_SPACE_MAXSIZE, /* maxsegsize */ 1284dfd1d0fcSMichal Meloun sc->coherent ? BUS_DMA_COHERENT : 0, /* flags */ 1285dfd1d0fcSMichal Meloun NULL, NULL, /* lockfunc, lockarg */ 1286dfd1d0fcSMichal Meloun &sc->dmat); 1287dfd1d0fcSMichal Meloun if (rv != 0) 1288dfd1d0fcSMichal Meloun goto out; 1289dfd1d0fcSMichal Meloun 129024042910SMarcin Wojtas rv = ofw_pcib_init(dev); 1291dfd1d0fcSMichal Meloun if (rv != 0) 1292dfd1d0fcSMichal Meloun goto out; 1293dfd1d0fcSMichal Meloun 1294dfd1d0fcSMichal Meloun rv = rk_pcie_decode_ranges(sc, sc->ofw_pci.sc_range, 1295dfd1d0fcSMichal Meloun sc->ofw_pci.sc_nrange); 1296dfd1d0fcSMichal Meloun if (rv != 0) 1297ee2324aaSAndrew Turner goto out_full; 1298dfd1d0fcSMichal Meloun rv = rk_pcie_setup_hw(sc); 1299dfd1d0fcSMichal Meloun if (rv != 0) 1300ee2324aaSAndrew Turner goto out_full; 1301dfd1d0fcSMichal Meloun 1302dfd1d0fcSMichal Meloun rv = rk_pcie_setup_sw(sc); 1303dfd1d0fcSMichal Meloun if (rv != 0) 1304ee2324aaSAndrew Turner goto out_full; 1305dfd1d0fcSMichal Meloun 1306dfd1d0fcSMichal Meloun rv = bus_setup_intr(dev, sc->client_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 1307dfd1d0fcSMichal Meloun rk_pcie_client_irq, NULL, sc, &sc->client_irq_cookie); 1308dfd1d0fcSMichal Meloun if (rv != 0) { 1309dfd1d0fcSMichal Meloun device_printf(dev, "cannot setup client interrupt handler\n"); 1310dfd1d0fcSMichal Meloun rv = ENXIO; 1311ee2324aaSAndrew Turner goto out_full; 1312dfd1d0fcSMichal Meloun } 1313dfd1d0fcSMichal Meloun 1314dfd1d0fcSMichal Meloun rv = bus_setup_intr(dev, sc->legacy_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 1315dfd1d0fcSMichal Meloun rk_pcie_legacy_irq, NULL, sc, &sc->legacy_irq_cookie); 1316dfd1d0fcSMichal Meloun if (rv != 0) { 1317dfd1d0fcSMichal Meloun device_printf(dev, "cannot setup client interrupt handler\n"); 1318dfd1d0fcSMichal Meloun rv = ENXIO; 1319ee2324aaSAndrew Turner goto out_full; 1320dfd1d0fcSMichal Meloun } 1321dfd1d0fcSMichal Meloun 1322dfd1d0fcSMichal Meloun rv = bus_setup_intr(dev, sc->sys_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 1323dfd1d0fcSMichal Meloun rk_pcie_sys_irq, NULL, sc, &sc->sys_irq_cookie); 1324dfd1d0fcSMichal Meloun if (rv != 0) { 1325dfd1d0fcSMichal Meloun device_printf(dev, "cannot setup client interrupt handler\n"); 1326dfd1d0fcSMichal Meloun rv = ENXIO; 1327ee2324aaSAndrew Turner goto out_full; 1328dfd1d0fcSMichal Meloun } 1329dfd1d0fcSMichal Meloun 1330dfd1d0fcSMichal Meloun /* Enable interrupts */ 1331dfd1d0fcSMichal Meloun val = 1332dfd1d0fcSMichal Meloun PCIE_CLIENT_INT_CORR_ERR | PCIE_CLIENT_INT_NFATAL_ERR | 1333dfd1d0fcSMichal Meloun PCIE_CLIENT_INT_FATAL_ERR | PCIE_CLIENT_INT_DPA | 1334dfd1d0fcSMichal Meloun PCIE_CLIENT_INT_HOT_RST | PCIE_CLIENT_INT_MSG | 1335dfd1d0fcSMichal Meloun PCIE_CLIENT_INT_LEGACY_DONE | PCIE_CLIENT_INT_INTA | 1336dfd1d0fcSMichal Meloun PCIE_CLIENT_INT_INTB | PCIE_CLIENT_INT_INTC | 1337dfd1d0fcSMichal Meloun PCIE_CLIENT_INT_INTD | PCIE_CLIENT_INT_PHY; 1338dfd1d0fcSMichal Meloun 1339dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CLIENT_INT_MASK, (val << 16) & ~val); 1340dfd1d0fcSMichal Meloun 1341dfd1d0fcSMichal Meloun val = 1342dfd1d0fcSMichal Meloun PCIE_CORE_INT_PRFPE | PCIE_CORE_INT_CRFPE | 1343dfd1d0fcSMichal Meloun PCIE_CORE_INT_RRPE | PCIE_CORE_INT_CRFO | 1344dfd1d0fcSMichal Meloun PCIE_CORE_INT_RT | PCIE_CORE_INT_RTR | 1345dfd1d0fcSMichal Meloun PCIE_CORE_INT_PE | PCIE_CORE_INT_MTR | 1346dfd1d0fcSMichal Meloun PCIE_CORE_INT_UCR | PCIE_CORE_INT_FCE | 1347dfd1d0fcSMichal Meloun PCIE_CORE_INT_CT | PCIE_CORE_INT_UTC | 1348dfd1d0fcSMichal Meloun PCIE_CORE_INT_MMVC; 1349dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_CORE_INT_MASK, ~(val)); 1350dfd1d0fcSMichal Meloun 1351dfd1d0fcSMichal Meloun val = APB_RD4(sc, PCIE_RC_CONFIG_LCS); 1352dfd1d0fcSMichal Meloun val |= PCIEM_LINK_CTL_LBMIE | PCIEM_LINK_CTL_LABIE; 1353dfd1d0fcSMichal Meloun APB_WR4(sc, PCIE_RC_CONFIG_LCS, val); 1354dfd1d0fcSMichal Meloun 1355dfd1d0fcSMichal Meloun DELAY(250000); 1356*5b56413dSWarner Losh device_add_child(dev, "pci", DEVICE_UNIT_ANY); 1357dfd1d0fcSMichal Meloun return (bus_generic_attach(dev)); 1358ee2324aaSAndrew Turner 1359ee2324aaSAndrew Turner out_full: 1360ee2324aaSAndrew Turner bus_teardown_intr(dev, sc->sys_irq_res, sc->sys_irq_cookie); 1361ee2324aaSAndrew Turner bus_teardown_intr(dev, sc->legacy_irq_res, sc->legacy_irq_cookie); 1362ee2324aaSAndrew Turner bus_teardown_intr(dev, sc->client_irq_res, sc->client_irq_cookie); 1363ee2324aaSAndrew Turner ofw_pcib_fini(dev); 1364dfd1d0fcSMichal Meloun out: 1365ee2324aaSAndrew Turner bus_dma_tag_destroy(sc->dmat); 1366ee2324aaSAndrew Turner bus_free_resource(dev, SYS_RES_IRQ, sc->sys_irq_res); 1367ee2324aaSAndrew Turner bus_free_resource(dev, SYS_RES_IRQ, sc->legacy_irq_res); 1368ee2324aaSAndrew Turner bus_free_resource(dev, SYS_RES_IRQ, sc->client_irq_res); 1369ee2324aaSAndrew Turner bus_free_resource(dev, SYS_RES_MEMORY, sc->apb_mem_res); 1370ee2324aaSAndrew Turner bus_free_resource(dev, SYS_RES_MEMORY, sc->axi_mem_res); 1371ee2324aaSAndrew Turner /* GPIO */ 1372ee2324aaSAndrew Turner gpio_pin_release(sc->gpio_ep); 1373ee2324aaSAndrew Turner /* Phys */ 1374ee2324aaSAndrew Turner for (int i = 0; i < MAX_LANES; i++) { 1375ee2324aaSAndrew Turner phy_release(sc->phys[i]); 1376ee2324aaSAndrew Turner } 1377ee2324aaSAndrew Turner /* Clocks */ 1378ee2324aaSAndrew Turner clk_release(sc->clk_aclk); 1379ee2324aaSAndrew Turner clk_release(sc->clk_aclk_perf); 1380ee2324aaSAndrew Turner clk_release(sc->clk_hclk); 1381ee2324aaSAndrew Turner clk_release(sc->clk_pm); 1382ee2324aaSAndrew Turner /* Resets */ 1383ee2324aaSAndrew Turner hwreset_release(sc->hwreset_core); 1384ee2324aaSAndrew Turner hwreset_release(sc->hwreset_mgmt); 1385ee2324aaSAndrew Turner hwreset_release(sc->hwreset_pipe); 1386ee2324aaSAndrew Turner hwreset_release(sc->hwreset_pm); 1387ee2324aaSAndrew Turner hwreset_release(sc->hwreset_aclk); 1388ee2324aaSAndrew Turner hwreset_release(sc->hwreset_pclk); 1389ee2324aaSAndrew Turner /* Regulators */ 1390ee2324aaSAndrew Turner regulator_release(sc->supply_12v); 1391ee2324aaSAndrew Turner regulator_release(sc->supply_3v3); 1392ee2324aaSAndrew Turner regulator_release(sc->supply_1v8); 1393ee2324aaSAndrew Turner regulator_release(sc->supply_0v9); 1394dfd1d0fcSMichal Meloun return (rv); 1395dfd1d0fcSMichal Meloun } 1396dfd1d0fcSMichal Meloun 1397dfd1d0fcSMichal Meloun static device_method_t rk_pcie_methods[] = { 1398dfd1d0fcSMichal Meloun /* Device interface */ 1399dfd1d0fcSMichal Meloun DEVMETHOD(device_probe, rk_pcie_probe), 1400dfd1d0fcSMichal Meloun DEVMETHOD(device_attach, rk_pcie_attach), 1401dfd1d0fcSMichal Meloun 1402dfd1d0fcSMichal Meloun /* Bus interface */ 1403dfd1d0fcSMichal Meloun DEVMETHOD(bus_get_dma_tag, rk_pcie_get_dma_tag), 1404dfd1d0fcSMichal Meloun 1405dfd1d0fcSMichal Meloun /* pcib interface */ 1406dfd1d0fcSMichal Meloun DEVMETHOD(pcib_read_config, rk_pcie_read_config), 1407dfd1d0fcSMichal Meloun DEVMETHOD(pcib_write_config, rk_pcie_write_config), 1408dfd1d0fcSMichal Meloun DEVMETHOD(pcib_route_interrupt, rk_pcie_route_interrupt), 1409dfd1d0fcSMichal Meloun #ifdef RK_PCIE_ENABLE_MSI 1410dfd1d0fcSMichal Meloun DEVMETHOD(pcib_alloc_msi, rk_pcie_alloc_msi), 1411dfd1d0fcSMichal Meloun DEVMETHOD(pcib_release_msi, rk_pcie_release_msi), 1412dfd1d0fcSMichal Meloun #endif 1413dfd1d0fcSMichal Meloun #ifdef RK_PCIE_ENABLE_MSIX 1414dfd1d0fcSMichal Meloun DEVMETHOD(pcib_alloc_msix, rk_pcie_alloc_msix), 1415dfd1d0fcSMichal Meloun DEVMETHOD(pcib_release_msix, rk_pcie_release_msix), 1416dfd1d0fcSMichal Meloun #endif 1417dfd1d0fcSMichal Meloun DEVMETHOD(pcib_map_msi, rk_pcie_map_msi), 1418dfd1d0fcSMichal Meloun DEVMETHOD(pcib_get_id, rk_pcie_get_id), 1419dfd1d0fcSMichal Meloun 1420dfd1d0fcSMichal Meloun /* OFW bus interface */ 1421dfd1d0fcSMichal Meloun DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 1422dfd1d0fcSMichal Meloun DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 1423dfd1d0fcSMichal Meloun DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 1424dfd1d0fcSMichal Meloun DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 1425dfd1d0fcSMichal Meloun DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 1426dfd1d0fcSMichal Meloun 1427dfd1d0fcSMichal Meloun DEVMETHOD_END 1428dfd1d0fcSMichal Meloun }; 1429dfd1d0fcSMichal Meloun 1430dfd1d0fcSMichal Meloun DEFINE_CLASS_1(pcib, rk_pcie_driver, rk_pcie_methods, 143124042910SMarcin Wojtas sizeof(struct rk_pcie_softc), ofw_pcib_driver); 1432b2c1681aSJohn Baldwin DRIVER_MODULE( rk_pcie, simplebus, rk_pcie_driver, NULL, NULL); 1433