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