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